Merge from emacs-24; up to 2013-01-03T02:37:57Z!rgm@gnu.org
[emacs.git] / src / xdisp.c
blob47855a1de3c6d3109c50b81396a7a431a8b763e7
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);
9491 bset_cache_long_scans (current_buffer, Qnil);
9493 oldpoint = message_dolog_marker1;
9494 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9495 oldbegv = message_dolog_marker2;
9496 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9497 oldzv = message_dolog_marker3;
9498 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9499 GCPRO1 (old_deactivate_mark);
9501 if (PT == Z)
9502 point_at_end = 1;
9503 if (ZV == Z)
9504 zv_at_end = 1;
9506 BEGV = BEG;
9507 BEGV_BYTE = BEG_BYTE;
9508 ZV = Z;
9509 ZV_BYTE = Z_BYTE;
9510 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9512 /* Insert the string--maybe converting multibyte to single byte
9513 or vice versa, so that all the text fits the buffer. */
9514 if (multibyte
9515 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9517 ptrdiff_t i;
9518 int c, char_bytes;
9519 char work[1];
9521 /* Convert a multibyte string to single-byte
9522 for the *Message* buffer. */
9523 for (i = 0; i < nbytes; i += char_bytes)
9525 c = string_char_and_length (msg + i, &char_bytes);
9526 work[0] = (ASCII_CHAR_P (c)
9528 : multibyte_char_to_unibyte (c));
9529 insert_1_both (work, 1, 1, 1, 0, 0);
9532 else if (! multibyte
9533 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9535 ptrdiff_t i;
9536 int c, char_bytes;
9537 unsigned char str[MAX_MULTIBYTE_LENGTH];
9538 /* Convert a single-byte string to multibyte
9539 for the *Message* buffer. */
9540 for (i = 0; i < nbytes; i++)
9542 c = msg[i];
9543 MAKE_CHAR_MULTIBYTE (c);
9544 char_bytes = CHAR_STRING (c, str);
9545 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9548 else if (nbytes)
9549 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9551 if (nlflag)
9553 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9554 printmax_t dups;
9556 insert_1_both ("\n", 1, 1, 1, 0, 0);
9558 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9559 this_bol = PT;
9560 this_bol_byte = PT_BYTE;
9562 /* See if this line duplicates the previous one.
9563 If so, combine duplicates. */
9564 if (this_bol > BEG)
9566 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9567 prev_bol = PT;
9568 prev_bol_byte = PT_BYTE;
9570 dups = message_log_check_duplicate (prev_bol_byte,
9571 this_bol_byte);
9572 if (dups)
9574 del_range_both (prev_bol, prev_bol_byte,
9575 this_bol, this_bol_byte, 0);
9576 if (dups > 1)
9578 char dupstr[sizeof " [ times]"
9579 + INT_STRLEN_BOUND (printmax_t)];
9581 /* If you change this format, don't forget to also
9582 change message_log_check_duplicate. */
9583 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9584 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9585 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9590 /* If we have more than the desired maximum number of lines
9591 in the *Messages* buffer now, delete the oldest ones.
9592 This is safe because we don't have undo in this buffer. */
9594 if (NATNUMP (Vmessage_log_max))
9596 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9597 -XFASTINT (Vmessage_log_max) - 1, 0);
9598 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9601 BEGV = marker_position (oldbegv);
9602 BEGV_BYTE = marker_byte_position (oldbegv);
9604 if (zv_at_end)
9606 ZV = Z;
9607 ZV_BYTE = Z_BYTE;
9609 else
9611 ZV = marker_position (oldzv);
9612 ZV_BYTE = marker_byte_position (oldzv);
9615 if (point_at_end)
9616 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9617 else
9618 /* We can't do Fgoto_char (oldpoint) because it will run some
9619 Lisp code. */
9620 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9621 marker_byte_position (oldpoint));
9623 UNGCPRO;
9624 unchain_marker (XMARKER (oldpoint));
9625 unchain_marker (XMARKER (oldbegv));
9626 unchain_marker (XMARKER (oldzv));
9628 shown = buffer_window_count (current_buffer) > 0;
9629 set_buffer_internal (oldbuf);
9630 /* We called insert_1_both above with its 5th argument (PREPARE)
9631 zero, which prevents insert_1_both from calling
9632 prepare_to_modify_buffer, which in turns prevents us from
9633 incrementing windows_or_buffers_changed even if *Messages* is
9634 shown in some window. So we must manually incrementing
9635 windows_or_buffers_changed here to make up for that. */
9636 if (shown)
9637 windows_or_buffers_changed = 41;
9638 else
9639 windows_or_buffers_changed = old_windows_or_buffers_changed;
9640 message_log_need_newline = !nlflag;
9641 Vdeactivate_mark = old_deactivate_mark;
9646 /* We are at the end of the buffer after just having inserted a newline.
9647 (Note: We depend on the fact we won't be crossing the gap.)
9648 Check to see if the most recent message looks a lot like the previous one.
9649 Return 0 if different, 1 if the new one should just replace it, or a
9650 value N > 1 if we should also append " [N times]". */
9652 static intmax_t
9653 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9655 ptrdiff_t i;
9656 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9657 int seen_dots = 0;
9658 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9659 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9661 for (i = 0; i < len; i++)
9663 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9664 seen_dots = 1;
9665 if (p1[i] != p2[i])
9666 return seen_dots;
9668 p1 += len;
9669 if (*p1 == '\n')
9670 return 2;
9671 if (*p1++ == ' ' && *p1++ == '[')
9673 char *pend;
9674 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9675 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9676 return n + 1;
9678 return 0;
9682 /* Display an echo area message M with a specified length of NBYTES
9683 bytes. The string may include null characters. If M is not a
9684 string, clear out any existing message, and let the mini-buffer
9685 text show through.
9687 This function cancels echoing. */
9689 void
9690 message3 (Lisp_Object m)
9692 struct gcpro gcpro1;
9694 GCPRO1 (m);
9695 clear_message (1,1);
9696 cancel_echoing ();
9698 /* First flush out any partial line written with print. */
9699 message_log_maybe_newline ();
9700 if (STRINGP (m))
9702 ptrdiff_t nbytes = SBYTES (m);
9703 bool multibyte = STRING_MULTIBYTE (m);
9704 USE_SAFE_ALLOCA;
9705 char *buffer = SAFE_ALLOCA (nbytes);
9706 memcpy (buffer, SDATA (m), nbytes);
9707 message_dolog (buffer, nbytes, 1, multibyte);
9708 SAFE_FREE ();
9710 message3_nolog (m);
9712 UNGCPRO;
9716 /* The non-logging version of message3.
9717 This does not cancel echoing, because it is used for echoing.
9718 Perhaps we need to make a separate function for echoing
9719 and make this cancel echoing. */
9721 void
9722 message3_nolog (Lisp_Object m)
9724 struct frame *sf = SELECTED_FRAME ();
9726 if (FRAME_INITIAL_P (sf))
9728 if (noninteractive_need_newline)
9729 putc ('\n', stderr);
9730 noninteractive_need_newline = 0;
9731 if (STRINGP (m))
9733 Lisp_Object s = ENCODE_SYSTEM (m);
9735 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9737 if (cursor_in_echo_area == 0)
9738 fprintf (stderr, "\n");
9739 fflush (stderr);
9741 /* Error messages get reported properly by cmd_error, so this must be just an
9742 informative message; if the frame hasn't really been initialized yet, just
9743 toss it. */
9744 else if (INTERACTIVE && sf->glyphs_initialized_p)
9746 /* Get the frame containing the mini-buffer
9747 that the selected frame is using. */
9748 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9749 Lisp_Object frame = XWINDOW (mini_window)->frame;
9750 struct frame *f = XFRAME (frame);
9752 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9753 Fmake_frame_visible (frame);
9755 if (STRINGP (m) && SCHARS (m) > 0)
9757 set_message (m);
9758 if (minibuffer_auto_raise)
9759 Fraise_frame (frame);
9760 /* Assume we are not echoing.
9761 (If we are, echo_now will override this.) */
9762 echo_message_buffer = Qnil;
9764 else
9765 clear_message (1, 1);
9767 do_pending_window_change (0);
9768 echo_area_display (1);
9769 do_pending_window_change (0);
9770 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9771 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9776 /* Display a null-terminated echo area message M. If M is 0, clear
9777 out any existing message, and let the mini-buffer text show through.
9779 The buffer M must continue to exist until after the echo area gets
9780 cleared or some other message gets displayed there. Do not pass
9781 text that is stored in a Lisp string. Do not pass text in a buffer
9782 that was alloca'd. */
9784 void
9785 message1 (const char *m)
9787 message3 (m ? build_unibyte_string (m) : Qnil);
9791 /* The non-logging counterpart of message1. */
9793 void
9794 message1_nolog (const char *m)
9796 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9799 /* Display a message M which contains a single %s
9800 which gets replaced with STRING. */
9802 void
9803 message_with_string (const char *m, Lisp_Object string, int log)
9805 CHECK_STRING (string);
9807 if (noninteractive)
9809 if (m)
9811 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
9812 String whose data pointer might be passed to us in M. So
9813 we use a local copy. */
9814 char *fmt = xstrdup (m);
9816 if (noninteractive_need_newline)
9817 putc ('\n', stderr);
9818 noninteractive_need_newline = 0;
9819 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
9820 if (!cursor_in_echo_area)
9821 fprintf (stderr, "\n");
9822 fflush (stderr);
9823 xfree (fmt);
9826 else if (INTERACTIVE)
9828 /* The frame whose minibuffer we're going to display the message on.
9829 It may be larger than the selected frame, so we need
9830 to use its buffer, not the selected frame's buffer. */
9831 Lisp_Object mini_window;
9832 struct frame *f, *sf = SELECTED_FRAME ();
9834 /* Get the frame containing the minibuffer
9835 that the selected frame is using. */
9836 mini_window = FRAME_MINIBUF_WINDOW (sf);
9837 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9839 /* Error messages get reported properly by cmd_error, so this must be
9840 just an informative message; if the frame hasn't really been
9841 initialized yet, just toss it. */
9842 if (f->glyphs_initialized_p)
9844 Lisp_Object args[2], msg;
9845 struct gcpro gcpro1, gcpro2;
9847 args[0] = build_string (m);
9848 args[1] = msg = string;
9849 GCPRO2 (args[0], msg);
9850 gcpro1.nvars = 2;
9852 msg = Fformat (2, args);
9854 if (log)
9855 message3 (msg);
9856 else
9857 message3_nolog (msg);
9859 UNGCPRO;
9861 /* Print should start at the beginning of the message
9862 buffer next time. */
9863 message_buf_print = 0;
9869 /* Dump an informative message to the minibuf. If M is 0, clear out
9870 any existing message, and let the mini-buffer text show through. */
9872 static void
9873 vmessage (const char *m, va_list ap)
9875 if (noninteractive)
9877 if (m)
9879 if (noninteractive_need_newline)
9880 putc ('\n', stderr);
9881 noninteractive_need_newline = 0;
9882 vfprintf (stderr, m, ap);
9883 if (cursor_in_echo_area == 0)
9884 fprintf (stderr, "\n");
9885 fflush (stderr);
9888 else if (INTERACTIVE)
9890 /* The frame whose mini-buffer we're going to display the message
9891 on. It may be larger than the selected frame, so we need to
9892 use its buffer, not the selected frame's buffer. */
9893 Lisp_Object mini_window;
9894 struct frame *f, *sf = SELECTED_FRAME ();
9896 /* Get the frame containing the mini-buffer
9897 that the selected frame is using. */
9898 mini_window = FRAME_MINIBUF_WINDOW (sf);
9899 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9901 /* Error messages get reported properly by cmd_error, so this must be
9902 just an informative message; if the frame hasn't really been
9903 initialized yet, just toss it. */
9904 if (f->glyphs_initialized_p)
9906 if (m)
9908 ptrdiff_t len;
9909 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9910 char *message_buf = alloca (maxsize + 1);
9912 len = doprnt (message_buf, maxsize, m, 0, ap);
9914 message3 (make_string (message_buf, len));
9916 else
9917 message1 (0);
9919 /* Print should start at the beginning of the message
9920 buffer next time. */
9921 message_buf_print = 0;
9926 void
9927 message (const char *m, ...)
9929 va_list ap;
9930 va_start (ap, m);
9931 vmessage (m, ap);
9932 va_end (ap);
9936 #if 0
9937 /* The non-logging version of message. */
9939 void
9940 message_nolog (const char *m, ...)
9942 Lisp_Object old_log_max;
9943 va_list ap;
9944 va_start (ap, m);
9945 old_log_max = Vmessage_log_max;
9946 Vmessage_log_max = Qnil;
9947 vmessage (m, ap);
9948 Vmessage_log_max = old_log_max;
9949 va_end (ap);
9951 #endif
9954 /* Display the current message in the current mini-buffer. This is
9955 only called from error handlers in process.c, and is not time
9956 critical. */
9958 void
9959 update_echo_area (void)
9961 if (!NILP (echo_area_buffer[0]))
9963 Lisp_Object string;
9964 string = Fcurrent_message ();
9965 message3 (string);
9970 /* Make sure echo area buffers in `echo_buffers' are live.
9971 If they aren't, make new ones. */
9973 static void
9974 ensure_echo_area_buffers (void)
9976 int i;
9978 for (i = 0; i < 2; ++i)
9979 if (!BUFFERP (echo_buffer[i])
9980 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9982 char name[30];
9983 Lisp_Object old_buffer;
9984 int j;
9986 old_buffer = echo_buffer[i];
9987 echo_buffer[i] = Fget_buffer_create
9988 (make_formatted_string (name, " *Echo Area %d*", i));
9989 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9990 /* to force word wrap in echo area -
9991 it was decided to postpone this*/
9992 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9994 for (j = 0; j < 2; ++j)
9995 if (EQ (old_buffer, echo_area_buffer[j]))
9996 echo_area_buffer[j] = echo_buffer[i];
10001 /* Call FN with args A1..A2 with either the current or last displayed
10002 echo_area_buffer as current buffer.
10004 WHICH zero means use the current message buffer
10005 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10006 from echo_buffer[] and clear it.
10008 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10009 suitable buffer from echo_buffer[] and clear it.
10011 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10012 that the current message becomes the last displayed one, make
10013 choose a suitable buffer for echo_area_buffer[0], and clear it.
10015 Value is what FN returns. */
10017 static int
10018 with_echo_area_buffer (struct window *w, int which,
10019 int (*fn) (ptrdiff_t, Lisp_Object),
10020 ptrdiff_t a1, Lisp_Object a2)
10022 Lisp_Object buffer;
10023 int this_one, the_other, clear_buffer_p, rc;
10024 ptrdiff_t count = SPECPDL_INDEX ();
10026 /* If buffers aren't live, make new ones. */
10027 ensure_echo_area_buffers ();
10029 clear_buffer_p = 0;
10031 if (which == 0)
10032 this_one = 0, the_other = 1;
10033 else if (which > 0)
10034 this_one = 1, the_other = 0;
10035 else
10037 this_one = 0, the_other = 1;
10038 clear_buffer_p = 1;
10040 /* We need a fresh one in case the current echo buffer equals
10041 the one containing the last displayed echo area message. */
10042 if (!NILP (echo_area_buffer[this_one])
10043 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10044 echo_area_buffer[this_one] = Qnil;
10047 /* Choose a suitable buffer from echo_buffer[] is we don't
10048 have one. */
10049 if (NILP (echo_area_buffer[this_one]))
10051 echo_area_buffer[this_one]
10052 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10053 ? echo_buffer[the_other]
10054 : echo_buffer[this_one]);
10055 clear_buffer_p = 1;
10058 buffer = echo_area_buffer[this_one];
10060 /* Don't get confused by reusing the buffer used for echoing
10061 for a different purpose. */
10062 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10063 cancel_echoing ();
10065 record_unwind_protect (unwind_with_echo_area_buffer,
10066 with_echo_area_buffer_unwind_data (w));
10068 /* Make the echo area buffer current. Note that for display
10069 purposes, it is not necessary that the displayed window's buffer
10070 == current_buffer, except for text property lookup. So, let's
10071 only set that buffer temporarily here without doing a full
10072 Fset_window_buffer. We must also change w->pointm, though,
10073 because otherwise an assertions in unshow_buffer fails, and Emacs
10074 aborts. */
10075 set_buffer_internal_1 (XBUFFER (buffer));
10076 if (w)
10078 wset_buffer (w, buffer);
10079 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10082 bset_undo_list (current_buffer, Qt);
10083 bset_read_only (current_buffer, Qnil);
10084 specbind (Qinhibit_read_only, Qt);
10085 specbind (Qinhibit_modification_hooks, Qt);
10087 if (clear_buffer_p && Z > BEG)
10088 del_range (BEG, Z);
10090 eassert (BEGV >= BEG);
10091 eassert (ZV <= Z && ZV >= BEGV);
10093 rc = fn (a1, a2);
10095 eassert (BEGV >= BEG);
10096 eassert (ZV <= Z && ZV >= BEGV);
10098 unbind_to (count, Qnil);
10099 return rc;
10103 /* Save state that should be preserved around the call to the function
10104 FN called in with_echo_area_buffer. */
10106 static Lisp_Object
10107 with_echo_area_buffer_unwind_data (struct window *w)
10109 int i = 0;
10110 Lisp_Object vector, tmp;
10112 /* Reduce consing by keeping one vector in
10113 Vwith_echo_area_save_vector. */
10114 vector = Vwith_echo_area_save_vector;
10115 Vwith_echo_area_save_vector = Qnil;
10117 if (NILP (vector))
10118 vector = Fmake_vector (make_number (9), Qnil);
10120 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10121 ASET (vector, i, Vdeactivate_mark); ++i;
10122 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10124 if (w)
10126 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10127 ASET (vector, i, w->contents); ++i;
10128 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10129 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10130 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10131 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10133 else
10135 int end = i + 6;
10136 for (; i < end; ++i)
10137 ASET (vector, i, Qnil);
10140 eassert (i == ASIZE (vector));
10141 return vector;
10145 /* Restore global state from VECTOR which was created by
10146 with_echo_area_buffer_unwind_data. */
10148 static void
10149 unwind_with_echo_area_buffer (Lisp_Object vector)
10151 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10152 Vdeactivate_mark = AREF (vector, 1);
10153 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10155 if (WINDOWP (AREF (vector, 3)))
10157 struct window *w;
10158 Lisp_Object buffer;
10160 w = XWINDOW (AREF (vector, 3));
10161 buffer = AREF (vector, 4);
10163 wset_buffer (w, buffer);
10164 set_marker_both (w->pointm, buffer,
10165 XFASTINT (AREF (vector, 5)),
10166 XFASTINT (AREF (vector, 6)));
10167 set_marker_both (w->start, buffer,
10168 XFASTINT (AREF (vector, 7)),
10169 XFASTINT (AREF (vector, 8)));
10172 Vwith_echo_area_save_vector = vector;
10176 /* Set up the echo area for use by print functions. MULTIBYTE_P
10177 non-zero means we will print multibyte. */
10179 void
10180 setup_echo_area_for_printing (int multibyte_p)
10182 /* If we can't find an echo area any more, exit. */
10183 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10184 Fkill_emacs (Qnil);
10186 ensure_echo_area_buffers ();
10188 if (!message_buf_print)
10190 /* A message has been output since the last time we printed.
10191 Choose a fresh echo area buffer. */
10192 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10193 echo_area_buffer[0] = echo_buffer[1];
10194 else
10195 echo_area_buffer[0] = echo_buffer[0];
10197 /* Switch to that buffer and clear it. */
10198 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10199 bset_truncate_lines (current_buffer, Qnil);
10201 if (Z > BEG)
10203 ptrdiff_t count = SPECPDL_INDEX ();
10204 specbind (Qinhibit_read_only, Qt);
10205 /* Note that undo recording is always disabled. */
10206 del_range (BEG, Z);
10207 unbind_to (count, Qnil);
10209 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10211 /* Set up the buffer for the multibyteness we need. */
10212 if (multibyte_p
10213 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10214 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10216 /* Raise the frame containing the echo area. */
10217 if (minibuffer_auto_raise)
10219 struct frame *sf = SELECTED_FRAME ();
10220 Lisp_Object mini_window;
10221 mini_window = FRAME_MINIBUF_WINDOW (sf);
10222 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10225 message_log_maybe_newline ();
10226 message_buf_print = 1;
10228 else
10230 if (NILP (echo_area_buffer[0]))
10232 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10233 echo_area_buffer[0] = echo_buffer[1];
10234 else
10235 echo_area_buffer[0] = echo_buffer[0];
10238 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10240 /* Someone switched buffers between print requests. */
10241 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10242 bset_truncate_lines (current_buffer, Qnil);
10248 /* Display an echo area message in window W. Value is non-zero if W's
10249 height is changed. If display_last_displayed_message_p is
10250 non-zero, display the message that was last displayed, otherwise
10251 display the current message. */
10253 static int
10254 display_echo_area (struct window *w)
10256 int i, no_message_p, window_height_changed_p;
10258 /* Temporarily disable garbage collections while displaying the echo
10259 area. This is done because a GC can print a message itself.
10260 That message would modify the echo area buffer's contents while a
10261 redisplay of the buffer is going on, and seriously confuse
10262 redisplay. */
10263 ptrdiff_t count = inhibit_garbage_collection ();
10265 /* If there is no message, we must call display_echo_area_1
10266 nevertheless because it resizes the window. But we will have to
10267 reset the echo_area_buffer in question to nil at the end because
10268 with_echo_area_buffer will sets it to an empty buffer. */
10269 i = display_last_displayed_message_p ? 1 : 0;
10270 no_message_p = NILP (echo_area_buffer[i]);
10272 window_height_changed_p
10273 = with_echo_area_buffer (w, display_last_displayed_message_p,
10274 display_echo_area_1,
10275 (intptr_t) w, Qnil);
10277 if (no_message_p)
10278 echo_area_buffer[i] = Qnil;
10280 unbind_to (count, Qnil);
10281 return window_height_changed_p;
10285 /* Helper for display_echo_area. Display the current buffer which
10286 contains the current echo area message in window W, a mini-window,
10287 a pointer to which is passed in A1. A2..A4 are currently not used.
10288 Change the height of W so that all of the message is displayed.
10289 Value is non-zero if height of W was changed. */
10291 static int
10292 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10294 intptr_t i1 = a1;
10295 struct window *w = (struct window *) i1;
10296 Lisp_Object window;
10297 struct text_pos start;
10298 int window_height_changed_p = 0;
10300 /* Do this before displaying, so that we have a large enough glyph
10301 matrix for the display. If we can't get enough space for the
10302 whole text, display the last N lines. That works by setting w->start. */
10303 window_height_changed_p = resize_mini_window (w, 0);
10305 /* Use the starting position chosen by resize_mini_window. */
10306 SET_TEXT_POS_FROM_MARKER (start, w->start);
10308 /* Display. */
10309 clear_glyph_matrix (w->desired_matrix);
10310 XSETWINDOW (window, w);
10311 try_window (window, start, 0);
10313 return window_height_changed_p;
10317 /* Resize the echo area window to exactly the size needed for the
10318 currently displayed message, if there is one. If a mini-buffer
10319 is active, don't shrink it. */
10321 void
10322 resize_echo_area_exactly (void)
10324 if (BUFFERP (echo_area_buffer[0])
10325 && WINDOWP (echo_area_window))
10327 struct window *w = XWINDOW (echo_area_window);
10328 int resized_p;
10329 Lisp_Object resize_exactly;
10331 if (minibuf_level == 0)
10332 resize_exactly = Qt;
10333 else
10334 resize_exactly = Qnil;
10336 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10337 (intptr_t) w, resize_exactly);
10338 if (resized_p)
10340 windows_or_buffers_changed = 42;
10341 update_mode_lines = 30;
10342 redisplay_internal ();
10348 /* Callback function for with_echo_area_buffer, when used from
10349 resize_echo_area_exactly. A1 contains a pointer to the window to
10350 resize, EXACTLY non-nil means resize the mini-window exactly to the
10351 size of the text displayed. A3 and A4 are not used. Value is what
10352 resize_mini_window returns. */
10354 static int
10355 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10357 intptr_t i1 = a1;
10358 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10362 /* Resize mini-window W to fit the size of its contents. EXACT_P
10363 means size the window exactly to the size needed. Otherwise, it's
10364 only enlarged until W's buffer is empty.
10366 Set W->start to the right place to begin display. If the whole
10367 contents fit, start at the beginning. Otherwise, start so as
10368 to make the end of the contents appear. This is particularly
10369 important for y-or-n-p, but seems desirable generally.
10371 Value is non-zero if the window height has been changed. */
10374 resize_mini_window (struct window *w, int exact_p)
10376 struct frame *f = XFRAME (w->frame);
10377 int window_height_changed_p = 0;
10379 eassert (MINI_WINDOW_P (w));
10381 /* By default, start display at the beginning. */
10382 set_marker_both (w->start, w->contents,
10383 BUF_BEGV (XBUFFER (w->contents)),
10384 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10386 /* Don't resize windows while redisplaying a window; it would
10387 confuse redisplay functions when the size of the window they are
10388 displaying changes from under them. Such a resizing can happen,
10389 for instance, when which-func prints a long message while
10390 we are running fontification-functions. We're running these
10391 functions with safe_call which binds inhibit-redisplay to t. */
10392 if (!NILP (Vinhibit_redisplay))
10393 return 0;
10395 /* Nil means don't try to resize. */
10396 if (NILP (Vresize_mini_windows)
10397 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10398 return 0;
10400 if (!FRAME_MINIBUF_ONLY_P (f))
10402 struct it it;
10403 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10404 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10405 int height;
10406 EMACS_INT max_height;
10407 int unit = FRAME_LINE_HEIGHT (f);
10408 struct text_pos start;
10409 struct buffer *old_current_buffer = NULL;
10411 if (current_buffer != XBUFFER (w->contents))
10413 old_current_buffer = current_buffer;
10414 set_buffer_internal (XBUFFER (w->contents));
10417 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10419 /* Compute the max. number of lines specified by the user. */
10420 if (FLOATP (Vmax_mini_window_height))
10421 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10422 else if (INTEGERP (Vmax_mini_window_height))
10423 max_height = XINT (Vmax_mini_window_height);
10424 else
10425 max_height = total_height / 4;
10427 /* Correct that max. height if it's bogus. */
10428 max_height = clip_to_bounds (1, max_height, total_height);
10430 /* Find out the height of the text in the window. */
10431 if (it.line_wrap == TRUNCATE)
10432 height = 1;
10433 else
10435 last_height = 0;
10436 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10437 if (it.max_ascent == 0 && it.max_descent == 0)
10438 height = it.current_y + last_height;
10439 else
10440 height = it.current_y + it.max_ascent + it.max_descent;
10441 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10442 height = (height + unit - 1) / unit;
10445 /* Compute a suitable window start. */
10446 if (height > max_height)
10448 height = max_height;
10449 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10450 move_it_vertically_backward (&it, (height - 1) * unit);
10451 start = it.current.pos;
10453 else
10454 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10455 SET_MARKER_FROM_TEXT_POS (w->start, start);
10457 if (EQ (Vresize_mini_windows, Qgrow_only))
10459 /* Let it grow only, until we display an empty message, in which
10460 case the window shrinks again. */
10461 if (height > WINDOW_TOTAL_LINES (w))
10463 int old_height = WINDOW_TOTAL_LINES (w);
10465 FRAME_WINDOWS_FROZEN (f) = 1;
10466 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10467 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10469 else if (height < WINDOW_TOTAL_LINES (w)
10470 && (exact_p || BEGV == ZV))
10472 int old_height = WINDOW_TOTAL_LINES (w);
10474 FRAME_WINDOWS_FROZEN (f) = 0;
10475 shrink_mini_window (w);
10476 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10479 else
10481 /* Always resize to exact size needed. */
10482 if (height > WINDOW_TOTAL_LINES (w))
10484 int old_height = WINDOW_TOTAL_LINES (w);
10486 FRAME_WINDOWS_FROZEN (f) = 1;
10487 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10488 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10490 else if (height < WINDOW_TOTAL_LINES (w))
10492 int old_height = WINDOW_TOTAL_LINES (w);
10494 FRAME_WINDOWS_FROZEN (f) = 0;
10495 shrink_mini_window (w);
10497 if (height)
10499 FRAME_WINDOWS_FROZEN (f) = 1;
10500 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10503 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10507 if (old_current_buffer)
10508 set_buffer_internal (old_current_buffer);
10511 return window_height_changed_p;
10515 /* Value is the current message, a string, or nil if there is no
10516 current message. */
10518 Lisp_Object
10519 current_message (void)
10521 Lisp_Object msg;
10523 if (!BUFFERP (echo_area_buffer[0]))
10524 msg = Qnil;
10525 else
10527 with_echo_area_buffer (0, 0, current_message_1,
10528 (intptr_t) &msg, Qnil);
10529 if (NILP (msg))
10530 echo_area_buffer[0] = Qnil;
10533 return msg;
10537 static int
10538 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10540 intptr_t i1 = a1;
10541 Lisp_Object *msg = (Lisp_Object *) i1;
10543 if (Z > BEG)
10544 *msg = make_buffer_string (BEG, Z, 1);
10545 else
10546 *msg = Qnil;
10547 return 0;
10551 /* Push the current message on Vmessage_stack for later restoration
10552 by restore_message. Value is non-zero if the current message isn't
10553 empty. This is a relatively infrequent operation, so it's not
10554 worth optimizing. */
10556 bool
10557 push_message (void)
10559 Lisp_Object msg = current_message ();
10560 Vmessage_stack = Fcons (msg, Vmessage_stack);
10561 return STRINGP (msg);
10565 /* Restore message display from the top of Vmessage_stack. */
10567 void
10568 restore_message (void)
10570 eassert (CONSP (Vmessage_stack));
10571 message3_nolog (XCAR (Vmessage_stack));
10575 /* Handler for unwind-protect calling pop_message. */
10577 void
10578 pop_message_unwind (void)
10580 /* Pop the top-most entry off Vmessage_stack. */
10581 eassert (CONSP (Vmessage_stack));
10582 Vmessage_stack = XCDR (Vmessage_stack);
10586 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10587 exits. If the stack is not empty, we have a missing pop_message
10588 somewhere. */
10590 void
10591 check_message_stack (void)
10593 if (!NILP (Vmessage_stack))
10594 emacs_abort ();
10598 /* Truncate to NCHARS what will be displayed in the echo area the next
10599 time we display it---but don't redisplay it now. */
10601 void
10602 truncate_echo_area (ptrdiff_t nchars)
10604 if (nchars == 0)
10605 echo_area_buffer[0] = Qnil;
10606 else if (!noninteractive
10607 && INTERACTIVE
10608 && !NILP (echo_area_buffer[0]))
10610 struct frame *sf = SELECTED_FRAME ();
10611 /* Error messages get reported properly by cmd_error, so this must be
10612 just an informative message; if the frame hasn't really been
10613 initialized yet, just toss it. */
10614 if (sf->glyphs_initialized_p)
10615 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10620 /* Helper function for truncate_echo_area. Truncate the current
10621 message to at most NCHARS characters. */
10623 static int
10624 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10626 if (BEG + nchars < Z)
10627 del_range (BEG + nchars, Z);
10628 if (Z == BEG)
10629 echo_area_buffer[0] = Qnil;
10630 return 0;
10633 /* Set the current message to STRING. */
10635 static void
10636 set_message (Lisp_Object string)
10638 eassert (STRINGP (string));
10640 message_enable_multibyte = STRING_MULTIBYTE (string);
10642 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10643 message_buf_print = 0;
10644 help_echo_showing_p = 0;
10646 if (STRINGP (Vdebug_on_message)
10647 && STRINGP (string)
10648 && fast_string_match (Vdebug_on_message, string) >= 0)
10649 call_debugger (list2 (Qerror, string));
10653 /* Helper function for set_message. First argument is ignored and second
10654 argument has the same meaning as for set_message.
10655 This function is called with the echo area buffer being current. */
10657 static int
10658 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10660 eassert (STRINGP (string));
10662 /* Change multibyteness of the echo buffer appropriately. */
10663 if (message_enable_multibyte
10664 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10665 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10667 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10668 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10669 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10671 /* Insert new message at BEG. */
10672 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10674 /* This function takes care of single/multibyte conversion.
10675 We just have to ensure that the echo area buffer has the right
10676 setting of enable_multibyte_characters. */
10677 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10679 return 0;
10683 /* Clear messages. CURRENT_P non-zero means clear the current
10684 message. LAST_DISPLAYED_P non-zero means clear the message
10685 last displayed. */
10687 void
10688 clear_message (int current_p, int last_displayed_p)
10690 if (current_p)
10692 echo_area_buffer[0] = Qnil;
10693 message_cleared_p = 1;
10696 if (last_displayed_p)
10697 echo_area_buffer[1] = Qnil;
10699 message_buf_print = 0;
10702 /* Clear garbaged frames.
10704 This function is used where the old redisplay called
10705 redraw_garbaged_frames which in turn called redraw_frame which in
10706 turn called clear_frame. The call to clear_frame was a source of
10707 flickering. I believe a clear_frame is not necessary. It should
10708 suffice in the new redisplay to invalidate all current matrices,
10709 and ensure a complete redisplay of all windows. */
10711 static void
10712 clear_garbaged_frames (void)
10714 if (frame_garbaged)
10716 Lisp_Object tail, frame;
10717 int changed_count = 0;
10719 FOR_EACH_FRAME (tail, frame)
10721 struct frame *f = XFRAME (frame);
10723 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10725 if (f->resized_p)
10726 redraw_frame (f);
10727 else
10728 clear_current_matrices (f);
10729 changed_count++;
10730 f->garbaged = 0;
10731 f->resized_p = 0;
10735 frame_garbaged = 0;
10736 if (changed_count)
10737 windows_or_buffers_changed = 43;
10742 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10743 is non-zero update selected_frame. Value is non-zero if the
10744 mini-windows height has been changed. */
10746 static int
10747 echo_area_display (int update_frame_p)
10749 Lisp_Object mini_window;
10750 struct window *w;
10751 struct frame *f;
10752 int window_height_changed_p = 0;
10753 struct frame *sf = SELECTED_FRAME ();
10755 mini_window = FRAME_MINIBUF_WINDOW (sf);
10756 w = XWINDOW (mini_window);
10757 f = XFRAME (WINDOW_FRAME (w));
10759 /* Don't display if frame is invisible or not yet initialized. */
10760 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10761 return 0;
10763 #ifdef HAVE_WINDOW_SYSTEM
10764 /* When Emacs starts, selected_frame may be the initial terminal
10765 frame. If we let this through, a message would be displayed on
10766 the terminal. */
10767 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10768 return 0;
10769 #endif /* HAVE_WINDOW_SYSTEM */
10771 /* Redraw garbaged frames. */
10772 clear_garbaged_frames ();
10774 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10776 echo_area_window = mini_window;
10777 window_height_changed_p = display_echo_area (w);
10778 w->must_be_updated_p = 1;
10780 /* Update the display, unless called from redisplay_internal.
10781 Also don't update the screen during redisplay itself. The
10782 update will happen at the end of redisplay, and an update
10783 here could cause confusion. */
10784 if (update_frame_p && !redisplaying_p)
10786 int n = 0;
10788 /* If the display update has been interrupted by pending
10789 input, update mode lines in the frame. Due to the
10790 pending input, it might have been that redisplay hasn't
10791 been called, so that mode lines above the echo area are
10792 garbaged. This looks odd, so we prevent it here. */
10793 if (!display_completed)
10794 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10796 if (window_height_changed_p
10797 /* Don't do this if Emacs is shutting down. Redisplay
10798 needs to run hooks. */
10799 && !NILP (Vrun_hooks))
10801 /* Must update other windows. Likewise as in other
10802 cases, don't let this update be interrupted by
10803 pending input. */
10804 ptrdiff_t count = SPECPDL_INDEX ();
10805 specbind (Qredisplay_dont_pause, Qt);
10806 windows_or_buffers_changed = 44;
10807 redisplay_internal ();
10808 unbind_to (count, Qnil);
10810 else if (FRAME_WINDOW_P (f) && n == 0)
10812 /* Window configuration is the same as before.
10813 Can do with a display update of the echo area,
10814 unless we displayed some mode lines. */
10815 update_single_window (w, 1);
10816 flush_frame (f);
10818 else
10819 update_frame (f, 1, 1);
10821 /* If cursor is in the echo area, make sure that the next
10822 redisplay displays the minibuffer, so that the cursor will
10823 be replaced with what the minibuffer wants. */
10824 if (cursor_in_echo_area)
10825 windows_or_buffers_changed = 45;
10828 else if (!EQ (mini_window, selected_window))
10829 windows_or_buffers_changed = 46;
10831 /* Last displayed message is now the current message. */
10832 echo_area_buffer[1] = echo_area_buffer[0];
10833 /* Inform read_char that we're not echoing. */
10834 echo_message_buffer = Qnil;
10836 /* Prevent redisplay optimization in redisplay_internal by resetting
10837 this_line_start_pos. This is done because the mini-buffer now
10838 displays the message instead of its buffer text. */
10839 if (EQ (mini_window, selected_window))
10840 CHARPOS (this_line_start_pos) = 0;
10842 return window_height_changed_p;
10845 /* Nonzero if the current window's buffer is shown in more than one
10846 window and was modified since last redisplay. */
10848 static int
10849 buffer_shared_and_changed (void)
10851 return (buffer_window_count (current_buffer) > 1
10852 && UNCHANGED_MODIFIED < MODIFF);
10855 /* Nonzero if W's buffer was changed but not saved. */
10857 static int
10858 window_buffer_changed (struct window *w)
10860 struct buffer *b = XBUFFER (w->contents);
10862 eassert (BUFFER_LIVE_P (b));
10864 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
10867 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10869 static int
10870 mode_line_update_needed (struct window *w)
10872 return (w->column_number_displayed != -1
10873 && !(PT == w->last_point && !window_outdated (w))
10874 && (w->column_number_displayed != current_column ()));
10877 /* Nonzero if window start of W is frozen and may not be changed during
10878 redisplay. */
10880 static bool
10881 window_frozen_p (struct window *w)
10883 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10885 Lisp_Object window;
10887 XSETWINDOW (window, w);
10888 if (MINI_WINDOW_P (w))
10889 return 0;
10890 else if (EQ (window, selected_window))
10891 return 0;
10892 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10893 && EQ (window, Vminibuf_scroll_window))
10894 /* This special window can't be frozen too. */
10895 return 0;
10896 else
10897 return 1;
10899 return 0;
10902 /***********************************************************************
10903 Mode Lines and Frame Titles
10904 ***********************************************************************/
10906 /* A buffer for constructing non-propertized mode-line strings and
10907 frame titles in it; allocated from the heap in init_xdisp and
10908 resized as needed in store_mode_line_noprop_char. */
10910 static char *mode_line_noprop_buf;
10912 /* The buffer's end, and a current output position in it. */
10914 static char *mode_line_noprop_buf_end;
10915 static char *mode_line_noprop_ptr;
10917 #define MODE_LINE_NOPROP_LEN(start) \
10918 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10920 static enum {
10921 MODE_LINE_DISPLAY = 0,
10922 MODE_LINE_TITLE,
10923 MODE_LINE_NOPROP,
10924 MODE_LINE_STRING
10925 } mode_line_target;
10927 /* Alist that caches the results of :propertize.
10928 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10929 static Lisp_Object mode_line_proptrans_alist;
10931 /* List of strings making up the mode-line. */
10932 static Lisp_Object mode_line_string_list;
10934 /* Base face property when building propertized mode line string. */
10935 static Lisp_Object mode_line_string_face;
10936 static Lisp_Object mode_line_string_face_prop;
10939 /* Unwind data for mode line strings */
10941 static Lisp_Object Vmode_line_unwind_vector;
10943 static Lisp_Object
10944 format_mode_line_unwind_data (struct frame *target_frame,
10945 struct buffer *obuf,
10946 Lisp_Object owin,
10947 int save_proptrans)
10949 Lisp_Object vector, tmp;
10951 /* Reduce consing by keeping one vector in
10952 Vwith_echo_area_save_vector. */
10953 vector = Vmode_line_unwind_vector;
10954 Vmode_line_unwind_vector = Qnil;
10956 if (NILP (vector))
10957 vector = Fmake_vector (make_number (10), Qnil);
10959 ASET (vector, 0, make_number (mode_line_target));
10960 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10961 ASET (vector, 2, mode_line_string_list);
10962 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10963 ASET (vector, 4, mode_line_string_face);
10964 ASET (vector, 5, mode_line_string_face_prop);
10966 if (obuf)
10967 XSETBUFFER (tmp, obuf);
10968 else
10969 tmp = Qnil;
10970 ASET (vector, 6, tmp);
10971 ASET (vector, 7, owin);
10972 if (target_frame)
10974 /* Similarly to `with-selected-window', if the operation selects
10975 a window on another frame, we must restore that frame's
10976 selected window, and (for a tty) the top-frame. */
10977 ASET (vector, 8, target_frame->selected_window);
10978 if (FRAME_TERMCAP_P (target_frame))
10979 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10982 return vector;
10985 static void
10986 unwind_format_mode_line (Lisp_Object vector)
10988 Lisp_Object old_window = AREF (vector, 7);
10989 Lisp_Object target_frame_window = AREF (vector, 8);
10990 Lisp_Object old_top_frame = AREF (vector, 9);
10992 mode_line_target = XINT (AREF (vector, 0));
10993 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10994 mode_line_string_list = AREF (vector, 2);
10995 if (! EQ (AREF (vector, 3), Qt))
10996 mode_line_proptrans_alist = AREF (vector, 3);
10997 mode_line_string_face = AREF (vector, 4);
10998 mode_line_string_face_prop = AREF (vector, 5);
11000 /* Select window before buffer, since it may change the buffer. */
11001 if (!NILP (old_window))
11003 /* If the operation that we are unwinding had selected a window
11004 on a different frame, reset its frame-selected-window. For a
11005 text terminal, reset its top-frame if necessary. */
11006 if (!NILP (target_frame_window))
11008 Lisp_Object frame
11009 = WINDOW_FRAME (XWINDOW (target_frame_window));
11011 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11012 Fselect_window (target_frame_window, Qt);
11014 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11015 Fselect_frame (old_top_frame, Qt);
11018 Fselect_window (old_window, Qt);
11021 if (!NILP (AREF (vector, 6)))
11023 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11024 ASET (vector, 6, Qnil);
11027 Vmode_line_unwind_vector = vector;
11031 /* Store a single character C for the frame title in mode_line_noprop_buf.
11032 Re-allocate mode_line_noprop_buf if necessary. */
11034 static void
11035 store_mode_line_noprop_char (char c)
11037 /* If output position has reached the end of the allocated buffer,
11038 increase the buffer's size. */
11039 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11041 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11042 ptrdiff_t size = len;
11043 mode_line_noprop_buf =
11044 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11045 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11046 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11049 *mode_line_noprop_ptr++ = c;
11053 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11054 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11055 characters that yield more columns than PRECISION; PRECISION <= 0
11056 means copy the whole string. Pad with spaces until FIELD_WIDTH
11057 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11058 pad. Called from display_mode_element when it is used to build a
11059 frame title. */
11061 static int
11062 store_mode_line_noprop (const char *string, int field_width, int precision)
11064 const unsigned char *str = (const unsigned char *) string;
11065 int n = 0;
11066 ptrdiff_t dummy, nbytes;
11068 /* Copy at most PRECISION chars from STR. */
11069 nbytes = strlen (string);
11070 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11071 while (nbytes--)
11072 store_mode_line_noprop_char (*str++);
11074 /* Fill up with spaces until FIELD_WIDTH reached. */
11075 while (field_width > 0
11076 && n < field_width)
11078 store_mode_line_noprop_char (' ');
11079 ++n;
11082 return n;
11085 /***********************************************************************
11086 Frame Titles
11087 ***********************************************************************/
11089 #ifdef HAVE_WINDOW_SYSTEM
11091 /* Set the title of FRAME, if it has changed. The title format is
11092 Vicon_title_format if FRAME is iconified, otherwise it is
11093 frame_title_format. */
11095 static void
11096 x_consider_frame_title (Lisp_Object frame)
11098 struct frame *f = XFRAME (frame);
11100 if (FRAME_WINDOW_P (f)
11101 || FRAME_MINIBUF_ONLY_P (f)
11102 || f->explicit_name)
11104 /* Do we have more than one visible frame on this X display? */
11105 Lisp_Object tail, other_frame, fmt;
11106 ptrdiff_t title_start;
11107 char *title;
11108 ptrdiff_t len;
11109 struct it it;
11110 ptrdiff_t count = SPECPDL_INDEX ();
11112 FOR_EACH_FRAME (tail, other_frame)
11114 struct frame *tf = XFRAME (other_frame);
11116 if (tf != f
11117 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11118 && !FRAME_MINIBUF_ONLY_P (tf)
11119 && !EQ (other_frame, tip_frame)
11120 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11121 break;
11124 /* Set global variable indicating that multiple frames exist. */
11125 multiple_frames = CONSP (tail);
11127 /* Switch to the buffer of selected window of the frame. Set up
11128 mode_line_target so that display_mode_element will output into
11129 mode_line_noprop_buf; then display the title. */
11130 record_unwind_protect (unwind_format_mode_line,
11131 format_mode_line_unwind_data
11132 (f, current_buffer, selected_window, 0));
11134 Fselect_window (f->selected_window, Qt);
11135 set_buffer_internal_1
11136 (XBUFFER (XWINDOW (f->selected_window)->contents));
11137 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11139 mode_line_target = MODE_LINE_TITLE;
11140 title_start = MODE_LINE_NOPROP_LEN (0);
11141 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11142 NULL, DEFAULT_FACE_ID);
11143 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11144 len = MODE_LINE_NOPROP_LEN (title_start);
11145 title = mode_line_noprop_buf + title_start;
11146 unbind_to (count, Qnil);
11148 /* Set the title only if it's changed. This avoids consing in
11149 the common case where it hasn't. (If it turns out that we've
11150 already wasted too much time by walking through the list with
11151 display_mode_element, then we might need to optimize at a
11152 higher level than this.) */
11153 if (! STRINGP (f->name)
11154 || SBYTES (f->name) != len
11155 || memcmp (title, SDATA (f->name), len) != 0)
11156 x_implicitly_set_name (f, make_string (title, len), Qnil);
11160 #endif /* not HAVE_WINDOW_SYSTEM */
11163 /***********************************************************************
11164 Menu Bars
11165 ***********************************************************************/
11168 /* Prepare for redisplay by updating menu-bar item lists when
11169 appropriate. This can call eval. */
11171 static void
11172 prepare_menu_bars (void)
11174 int all_windows;
11175 struct gcpro gcpro1, gcpro2;
11176 struct frame *f;
11177 Lisp_Object tooltip_frame;
11179 #ifdef HAVE_WINDOW_SYSTEM
11180 tooltip_frame = tip_frame;
11181 #else
11182 tooltip_frame = Qnil;
11183 #endif
11185 /* Update all frame titles based on their buffer names, etc. We do
11186 this before the menu bars so that the buffer-menu will show the
11187 up-to-date frame titles. */
11188 #ifdef HAVE_WINDOW_SYSTEM
11189 if (windows_or_buffers_changed || update_mode_lines)
11191 Lisp_Object tail, frame;
11193 FOR_EACH_FRAME (tail, frame)
11195 f = XFRAME (frame);
11196 if (!EQ (frame, tooltip_frame)
11197 && (FRAME_ICONIFIED_P (f)
11198 || FRAME_VISIBLE_P (f) == 1
11199 /* Exclude TTY frames that are obscured because they
11200 are not the top frame on their console. This is
11201 because x_consider_frame_title actually switches
11202 to the frame, which for TTY frames means it is
11203 marked as garbaged, and will be completely
11204 redrawn on the next redisplay cycle. This causes
11205 TTY frames to be completely redrawn, when there
11206 are more than one of them, even though nothing
11207 should be changed on display. */
11208 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11209 x_consider_frame_title (frame);
11212 #endif /* HAVE_WINDOW_SYSTEM */
11214 /* Update the menu bar item lists, if appropriate. This has to be
11215 done before any actual redisplay or generation of display lines. */
11216 all_windows = (update_mode_lines
11217 || buffer_shared_and_changed ()
11218 || windows_or_buffers_changed);
11220 if (FUNCTIONP (Vpre_redisplay_function))
11221 safe_call1 (Vpre_redisplay_function, all_windows ? Qt : Qnil);
11223 if (all_windows)
11225 Lisp_Object tail, frame;
11226 ptrdiff_t count = SPECPDL_INDEX ();
11227 /* 1 means that update_menu_bar has run its hooks
11228 so any further calls to update_menu_bar shouldn't do so again. */
11229 int menu_bar_hooks_run = 0;
11231 record_unwind_save_match_data ();
11233 FOR_EACH_FRAME (tail, frame)
11235 f = XFRAME (frame);
11237 /* Ignore tooltip frame. */
11238 if (EQ (frame, tooltip_frame))
11239 continue;
11241 /* If a window on this frame changed size, report that to
11242 the user and clear the size-change flag. */
11243 if (FRAME_WINDOW_SIZES_CHANGED (f))
11245 Lisp_Object functions;
11247 /* Clear flag first in case we get an error below. */
11248 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11249 functions = Vwindow_size_change_functions;
11250 GCPRO2 (tail, functions);
11252 while (CONSP (functions))
11254 if (!EQ (XCAR (functions), Qt))
11255 call1 (XCAR (functions), frame);
11256 functions = XCDR (functions);
11258 UNGCPRO;
11261 GCPRO1 (tail);
11262 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11263 #ifdef HAVE_WINDOW_SYSTEM
11264 update_tool_bar (f, 0);
11265 #endif
11266 #ifdef HAVE_NS
11267 if (windows_or_buffers_changed
11268 && FRAME_NS_P (f))
11269 ns_set_doc_edited
11270 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11271 #endif
11272 UNGCPRO;
11275 unbind_to (count, Qnil);
11277 else
11279 struct frame *sf = SELECTED_FRAME ();
11280 update_menu_bar (sf, 1, 0);
11281 #ifdef HAVE_WINDOW_SYSTEM
11282 update_tool_bar (sf, 1);
11283 #endif
11288 /* Update the menu bar item list for frame F. This has to be done
11289 before we start to fill in any display lines, because it can call
11290 eval.
11292 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11294 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11295 already ran the menu bar hooks for this redisplay, so there
11296 is no need to run them again. The return value is the
11297 updated value of this flag, to pass to the next call. */
11299 static int
11300 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11302 Lisp_Object window;
11303 register struct window *w;
11305 /* If called recursively during a menu update, do nothing. This can
11306 happen when, for instance, an activate-menubar-hook causes a
11307 redisplay. */
11308 if (inhibit_menubar_update)
11309 return hooks_run;
11311 window = FRAME_SELECTED_WINDOW (f);
11312 w = XWINDOW (window);
11314 if (FRAME_WINDOW_P (f)
11316 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11317 || defined (HAVE_NS) || defined (USE_GTK)
11318 FRAME_EXTERNAL_MENU_BAR (f)
11319 #else
11320 FRAME_MENU_BAR_LINES (f) > 0
11321 #endif
11322 : FRAME_MENU_BAR_LINES (f) > 0)
11324 /* If the user has switched buffers or windows, we need to
11325 recompute to reflect the new bindings. But we'll
11326 recompute when update_mode_lines is set too; that means
11327 that people can use force-mode-line-update to request
11328 that the menu bar be recomputed. The adverse effect on
11329 the rest of the redisplay algorithm is about the same as
11330 windows_or_buffers_changed anyway. */
11331 if (windows_or_buffers_changed
11332 /* This used to test w->update_mode_line, but we believe
11333 there is no need to recompute the menu in that case. */
11334 || update_mode_lines
11335 || window_buffer_changed (w))
11337 struct buffer *prev = current_buffer;
11338 ptrdiff_t count = SPECPDL_INDEX ();
11340 specbind (Qinhibit_menubar_update, Qt);
11342 set_buffer_internal_1 (XBUFFER (w->contents));
11343 if (save_match_data)
11344 record_unwind_save_match_data ();
11345 if (NILP (Voverriding_local_map_menu_flag))
11347 specbind (Qoverriding_terminal_local_map, Qnil);
11348 specbind (Qoverriding_local_map, Qnil);
11351 if (!hooks_run)
11353 /* Run the Lucid hook. */
11354 safe_run_hooks (Qactivate_menubar_hook);
11356 /* If it has changed current-menubar from previous value,
11357 really recompute the menu-bar from the value. */
11358 if (! NILP (Vlucid_menu_bar_dirty_flag))
11359 call0 (Qrecompute_lucid_menubar);
11361 safe_run_hooks (Qmenu_bar_update_hook);
11363 hooks_run = 1;
11366 XSETFRAME (Vmenu_updating_frame, f);
11367 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11369 /* Redisplay the menu bar in case we changed it. */
11370 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11371 || defined (HAVE_NS) || defined (USE_GTK)
11372 if (FRAME_WINDOW_P (f))
11374 #if defined (HAVE_NS)
11375 /* All frames on Mac OS share the same menubar. So only
11376 the selected frame should be allowed to set it. */
11377 if (f == SELECTED_FRAME ())
11378 #endif
11379 set_frame_menubar (f, 0, 0);
11381 else
11382 /* On a terminal screen, the menu bar is an ordinary screen
11383 line, and this makes it get updated. */
11384 w->update_mode_line = 1;
11385 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11386 /* In the non-toolkit version, the menu bar is an ordinary screen
11387 line, and this makes it get updated. */
11388 w->update_mode_line = 1;
11389 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11391 unbind_to (count, Qnil);
11392 set_buffer_internal_1 (prev);
11396 return hooks_run;
11399 /***********************************************************************
11400 Tool-bars
11401 ***********************************************************************/
11403 #ifdef HAVE_WINDOW_SYSTEM
11405 /* Tool-bar item index of the item on which a mouse button was pressed
11406 or -1. */
11408 int last_tool_bar_item;
11410 /* Select `frame' temporarily without running all the code in
11411 do_switch_frame.
11412 FIXME: Maybe do_switch_frame should be trimmed down similarly
11413 when `norecord' is set. */
11414 static void
11415 fast_set_selected_frame (Lisp_Object frame)
11417 if (!EQ (selected_frame, frame))
11419 selected_frame = frame;
11420 selected_window = XFRAME (frame)->selected_window;
11424 /* Update the tool-bar item list for frame F. This has to be done
11425 before we start to fill in any display lines. Called from
11426 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11427 and restore it here. */
11429 static void
11430 update_tool_bar (struct frame *f, int save_match_data)
11432 #if defined (USE_GTK) || defined (HAVE_NS)
11433 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11434 #else
11435 int do_update = WINDOWP (f->tool_bar_window)
11436 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11437 #endif
11439 if (do_update)
11441 Lisp_Object window;
11442 struct window *w;
11444 window = FRAME_SELECTED_WINDOW (f);
11445 w = XWINDOW (window);
11447 /* If the user has switched buffers or windows, we need to
11448 recompute to reflect the new bindings. But we'll
11449 recompute when update_mode_lines is set too; that means
11450 that people can use force-mode-line-update to request
11451 that the menu bar be recomputed. The adverse effect on
11452 the rest of the redisplay algorithm is about the same as
11453 windows_or_buffers_changed anyway. */
11454 if (windows_or_buffers_changed
11455 || w->update_mode_line
11456 || update_mode_lines
11457 || window_buffer_changed (w))
11459 struct buffer *prev = current_buffer;
11460 ptrdiff_t count = SPECPDL_INDEX ();
11461 Lisp_Object frame, new_tool_bar;
11462 int new_n_tool_bar;
11463 struct gcpro gcpro1;
11465 /* Set current_buffer to the buffer of the selected
11466 window of the frame, so that we get the right local
11467 keymaps. */
11468 set_buffer_internal_1 (XBUFFER (w->contents));
11470 /* Save match data, if we must. */
11471 if (save_match_data)
11472 record_unwind_save_match_data ();
11474 /* Make sure that we don't accidentally use bogus keymaps. */
11475 if (NILP (Voverriding_local_map_menu_flag))
11477 specbind (Qoverriding_terminal_local_map, Qnil);
11478 specbind (Qoverriding_local_map, Qnil);
11481 GCPRO1 (new_tool_bar);
11483 /* We must temporarily set the selected frame to this frame
11484 before calling tool_bar_items, because the calculation of
11485 the tool-bar keymap uses the selected frame (see
11486 `tool-bar-make-keymap' in tool-bar.el). */
11487 eassert (EQ (selected_window,
11488 /* Since we only explicitly preserve selected_frame,
11489 check that selected_window would be redundant. */
11490 XFRAME (selected_frame)->selected_window));
11491 record_unwind_protect (fast_set_selected_frame, selected_frame);
11492 XSETFRAME (frame, f);
11493 fast_set_selected_frame (frame);
11495 /* Build desired tool-bar items from keymaps. */
11496 new_tool_bar
11497 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11498 &new_n_tool_bar);
11500 /* Redisplay the tool-bar if we changed it. */
11501 if (new_n_tool_bar != f->n_tool_bar_items
11502 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11504 /* Redisplay that happens asynchronously due to an expose event
11505 may access f->tool_bar_items. Make sure we update both
11506 variables within BLOCK_INPUT so no such event interrupts. */
11507 block_input ();
11508 fset_tool_bar_items (f, new_tool_bar);
11509 f->n_tool_bar_items = new_n_tool_bar;
11510 w->update_mode_line = 1;
11511 unblock_input ();
11514 UNGCPRO;
11516 unbind_to (count, Qnil);
11517 set_buffer_internal_1 (prev);
11522 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11524 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11525 F's desired tool-bar contents. F->tool_bar_items must have
11526 been set up previously by calling prepare_menu_bars. */
11528 static void
11529 build_desired_tool_bar_string (struct frame *f)
11531 int i, size, size_needed;
11532 struct gcpro gcpro1, gcpro2, gcpro3;
11533 Lisp_Object image, plist, props;
11535 image = plist = props = Qnil;
11536 GCPRO3 (image, plist, props);
11538 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11539 Otherwise, make a new string. */
11541 /* The size of the string we might be able to reuse. */
11542 size = (STRINGP (f->desired_tool_bar_string)
11543 ? SCHARS (f->desired_tool_bar_string)
11544 : 0);
11546 /* We need one space in the string for each image. */
11547 size_needed = f->n_tool_bar_items;
11549 /* Reuse f->desired_tool_bar_string, if possible. */
11550 if (size < size_needed || NILP (f->desired_tool_bar_string))
11551 fset_desired_tool_bar_string
11552 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11553 else
11555 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11556 Fremove_text_properties (make_number (0), make_number (size),
11557 props, f->desired_tool_bar_string);
11560 /* Put a `display' property on the string for the images to display,
11561 put a `menu_item' property on tool-bar items with a value that
11562 is the index of the item in F's tool-bar item vector. */
11563 for (i = 0; i < f->n_tool_bar_items; ++i)
11565 #define PROP(IDX) \
11566 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11568 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11569 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11570 int hmargin, vmargin, relief, idx, end;
11572 /* If image is a vector, choose the image according to the
11573 button state. */
11574 image = PROP (TOOL_BAR_ITEM_IMAGES);
11575 if (VECTORP (image))
11577 if (enabled_p)
11578 idx = (selected_p
11579 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11580 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11581 else
11582 idx = (selected_p
11583 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11584 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11586 eassert (ASIZE (image) >= idx);
11587 image = AREF (image, idx);
11589 else
11590 idx = -1;
11592 /* Ignore invalid image specifications. */
11593 if (!valid_image_p (image))
11594 continue;
11596 /* Display the tool-bar button pressed, or depressed. */
11597 plist = Fcopy_sequence (XCDR (image));
11599 /* Compute margin and relief to draw. */
11600 relief = (tool_bar_button_relief >= 0
11601 ? tool_bar_button_relief
11602 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11603 hmargin = vmargin = relief;
11605 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11606 INT_MAX - max (hmargin, vmargin)))
11608 hmargin += XFASTINT (Vtool_bar_button_margin);
11609 vmargin += XFASTINT (Vtool_bar_button_margin);
11611 else if (CONSP (Vtool_bar_button_margin))
11613 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11614 INT_MAX - hmargin))
11615 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11617 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11618 INT_MAX - vmargin))
11619 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11622 if (auto_raise_tool_bar_buttons_p)
11624 /* Add a `:relief' property to the image spec if the item is
11625 selected. */
11626 if (selected_p)
11628 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11629 hmargin -= relief;
11630 vmargin -= relief;
11633 else
11635 /* If image is selected, display it pressed, i.e. with a
11636 negative relief. If it's not selected, display it with a
11637 raised relief. */
11638 plist = Fplist_put (plist, QCrelief,
11639 (selected_p
11640 ? make_number (-relief)
11641 : make_number (relief)));
11642 hmargin -= relief;
11643 vmargin -= relief;
11646 /* Put a margin around the image. */
11647 if (hmargin || vmargin)
11649 if (hmargin == vmargin)
11650 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11651 else
11652 plist = Fplist_put (plist, QCmargin,
11653 Fcons (make_number (hmargin),
11654 make_number (vmargin)));
11657 /* If button is not enabled, and we don't have special images
11658 for the disabled state, make the image appear disabled by
11659 applying an appropriate algorithm to it. */
11660 if (!enabled_p && idx < 0)
11661 plist = Fplist_put (plist, QCconversion, Qdisabled);
11663 /* Put a `display' text property on the string for the image to
11664 display. Put a `menu-item' property on the string that gives
11665 the start of this item's properties in the tool-bar items
11666 vector. */
11667 image = Fcons (Qimage, plist);
11668 props = list4 (Qdisplay, image,
11669 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11671 /* Let the last image hide all remaining spaces in the tool bar
11672 string. The string can be longer than needed when we reuse a
11673 previous string. */
11674 if (i + 1 == f->n_tool_bar_items)
11675 end = SCHARS (f->desired_tool_bar_string);
11676 else
11677 end = i + 1;
11678 Fadd_text_properties (make_number (i), make_number (end),
11679 props, f->desired_tool_bar_string);
11680 #undef PROP
11683 UNGCPRO;
11687 /* Display one line of the tool-bar of frame IT->f.
11689 HEIGHT specifies the desired height of the tool-bar line.
11690 If the actual height of the glyph row is less than HEIGHT, the
11691 row's height is increased to HEIGHT, and the icons are centered
11692 vertically in the new height.
11694 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11695 count a final empty row in case the tool-bar width exactly matches
11696 the window width.
11699 static void
11700 display_tool_bar_line (struct it *it, int height)
11702 struct glyph_row *row = it->glyph_row;
11703 int max_x = it->last_visible_x;
11704 struct glyph *last;
11706 prepare_desired_row (row);
11707 row->y = it->current_y;
11709 /* Note that this isn't made use of if the face hasn't a box,
11710 so there's no need to check the face here. */
11711 it->start_of_box_run_p = 1;
11713 while (it->current_x < max_x)
11715 int x, n_glyphs_before, i, nglyphs;
11716 struct it it_before;
11718 /* Get the next display element. */
11719 if (!get_next_display_element (it))
11721 /* Don't count empty row if we are counting needed tool-bar lines. */
11722 if (height < 0 && !it->hpos)
11723 return;
11724 break;
11727 /* Produce glyphs. */
11728 n_glyphs_before = row->used[TEXT_AREA];
11729 it_before = *it;
11731 PRODUCE_GLYPHS (it);
11733 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11734 i = 0;
11735 x = it_before.current_x;
11736 while (i < nglyphs)
11738 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11740 if (x + glyph->pixel_width > max_x)
11742 /* Glyph doesn't fit on line. Backtrack. */
11743 row->used[TEXT_AREA] = n_glyphs_before;
11744 *it = it_before;
11745 /* If this is the only glyph on this line, it will never fit on the
11746 tool-bar, so skip it. But ensure there is at least one glyph,
11747 so we don't accidentally disable the tool-bar. */
11748 if (n_glyphs_before == 0
11749 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11750 break;
11751 goto out;
11754 ++it->hpos;
11755 x += glyph->pixel_width;
11756 ++i;
11759 /* Stop at line end. */
11760 if (ITERATOR_AT_END_OF_LINE_P (it))
11761 break;
11763 set_iterator_to_next (it, 1);
11766 out:;
11768 row->displays_text_p = row->used[TEXT_AREA] != 0;
11770 /* Use default face for the border below the tool bar.
11772 FIXME: When auto-resize-tool-bars is grow-only, there is
11773 no additional border below the possibly empty tool-bar lines.
11774 So to make the extra empty lines look "normal", we have to
11775 use the tool-bar face for the border too. */
11776 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11777 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11778 it->face_id = DEFAULT_FACE_ID;
11780 extend_face_to_end_of_line (it);
11781 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11782 last->right_box_line_p = 1;
11783 if (last == row->glyphs[TEXT_AREA])
11784 last->left_box_line_p = 1;
11786 /* Make line the desired height and center it vertically. */
11787 if ((height -= it->max_ascent + it->max_descent) > 0)
11789 /* Don't add more than one line height. */
11790 height %= FRAME_LINE_HEIGHT (it->f);
11791 it->max_ascent += height / 2;
11792 it->max_descent += (height + 1) / 2;
11795 compute_line_metrics (it);
11797 /* If line is empty, make it occupy the rest of the tool-bar. */
11798 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11800 row->height = row->phys_height = it->last_visible_y - row->y;
11801 row->visible_height = row->height;
11802 row->ascent = row->phys_ascent = 0;
11803 row->extra_line_spacing = 0;
11806 row->full_width_p = 1;
11807 row->continued_p = 0;
11808 row->truncated_on_left_p = 0;
11809 row->truncated_on_right_p = 0;
11811 it->current_x = it->hpos = 0;
11812 it->current_y += row->height;
11813 ++it->vpos;
11814 ++it->glyph_row;
11818 /* Max tool-bar height. */
11820 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11821 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11823 /* Value is the number of screen lines needed to make all tool-bar
11824 items of frame F visible. The number of actual rows needed is
11825 returned in *N_ROWS if non-NULL. */
11827 static int
11828 tool_bar_lines_needed (struct frame *f, int *n_rows)
11830 struct window *w = XWINDOW (f->tool_bar_window);
11831 struct it it;
11832 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11833 the desired matrix, so use (unused) mode-line row as temporary row to
11834 avoid destroying the first tool-bar row. */
11835 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11837 /* Initialize an iterator for iteration over
11838 F->desired_tool_bar_string in the tool-bar window of frame F. */
11839 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11840 it.first_visible_x = 0;
11841 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11842 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11843 it.paragraph_embedding = L2R;
11845 while (!ITERATOR_AT_END_P (&it))
11847 clear_glyph_row (temp_row);
11848 it.glyph_row = temp_row;
11849 display_tool_bar_line (&it, -1);
11851 clear_glyph_row (temp_row);
11853 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11854 if (n_rows)
11855 *n_rows = it.vpos > 0 ? it.vpos : -1;
11857 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11860 #endif /* !USE_GTK && !HAVE_NS */
11862 #if defined USE_GTK || defined HAVE_NS
11863 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
11864 #endif
11866 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11867 0, 1, 0,
11868 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11869 If FRAME is nil or omitted, use the selected frame. */)
11870 (Lisp_Object frame)
11872 int nlines = 0;
11873 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11874 struct frame *f = decode_any_frame (frame);
11875 struct window *w;
11877 if (WINDOWP (f->tool_bar_window)
11878 && (w = XWINDOW (f->tool_bar_window),
11879 WINDOW_TOTAL_LINES (w) > 0))
11881 update_tool_bar (f, 1);
11882 if (f->n_tool_bar_items)
11884 build_desired_tool_bar_string (f);
11885 nlines = tool_bar_lines_needed (f, NULL);
11888 #endif
11889 return make_number (nlines);
11893 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11894 height should be changed. */
11896 static int
11897 redisplay_tool_bar (struct frame *f)
11899 #if defined (USE_GTK) || defined (HAVE_NS)
11901 if (FRAME_EXTERNAL_TOOL_BAR (f))
11902 update_frame_tool_bar (f);
11903 return 0;
11905 #else /* !USE_GTK && !HAVE_NS */
11907 struct window *w;
11908 struct it it;
11909 struct glyph_row *row;
11911 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11912 do anything. This means you must start with tool-bar-lines
11913 non-zero to get the auto-sizing effect. Or in other words, you
11914 can turn off tool-bars by specifying tool-bar-lines zero. */
11915 if (!WINDOWP (f->tool_bar_window)
11916 || (w = XWINDOW (f->tool_bar_window),
11917 WINDOW_TOTAL_LINES (w) == 0))
11918 return 0;
11920 /* Set up an iterator for the tool-bar window. */
11921 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11922 it.first_visible_x = 0;
11923 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11924 row = it.glyph_row;
11926 /* Build a string that represents the contents of the tool-bar. */
11927 build_desired_tool_bar_string (f);
11928 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11929 /* FIXME: This should be controlled by a user option. But it
11930 doesn't make sense to have an R2L tool bar if the menu bar cannot
11931 be drawn also R2L, and making the menu bar R2L is tricky due
11932 toolkit-specific code that implements it. If an R2L tool bar is
11933 ever supported, display_tool_bar_line should also be augmented to
11934 call unproduce_glyphs like display_line and display_string
11935 do. */
11936 it.paragraph_embedding = L2R;
11938 if (f->n_tool_bar_rows == 0)
11940 int nlines;
11942 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11943 nlines != WINDOW_TOTAL_LINES (w)))
11945 Lisp_Object frame;
11946 int old_height = WINDOW_TOTAL_LINES (w);
11948 XSETFRAME (frame, f);
11949 Fmodify_frame_parameters (frame,
11950 list1 (Fcons (Qtool_bar_lines,
11951 make_number (nlines))));
11952 if (WINDOW_TOTAL_LINES (w) != old_height)
11954 clear_glyph_matrix (w->desired_matrix);
11955 f->fonts_changed = 1;
11956 return 1;
11961 /* Display as many lines as needed to display all tool-bar items. */
11963 if (f->n_tool_bar_rows > 0)
11965 int border, rows, height, extra;
11967 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11968 border = XINT (Vtool_bar_border);
11969 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11970 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11971 else if (EQ (Vtool_bar_border, Qborder_width))
11972 border = f->border_width;
11973 else
11974 border = 0;
11975 if (border < 0)
11976 border = 0;
11978 rows = f->n_tool_bar_rows;
11979 height = max (1, (it.last_visible_y - border) / rows);
11980 extra = it.last_visible_y - border - height * rows;
11982 while (it.current_y < it.last_visible_y)
11984 int h = 0;
11985 if (extra > 0 && rows-- > 0)
11987 h = (extra + rows - 1) / rows;
11988 extra -= h;
11990 display_tool_bar_line (&it, height + h);
11993 else
11995 while (it.current_y < it.last_visible_y)
11996 display_tool_bar_line (&it, 0);
11999 /* It doesn't make much sense to try scrolling in the tool-bar
12000 window, so don't do it. */
12001 w->desired_matrix->no_scrolling_p = 1;
12002 w->must_be_updated_p = 1;
12004 if (!NILP (Vauto_resize_tool_bars))
12006 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12007 int change_height_p = 0;
12009 /* If we couldn't display everything, change the tool-bar's
12010 height if there is room for more. */
12011 if (IT_STRING_CHARPOS (it) < it.end_charpos
12012 && it.current_y < max_tool_bar_height)
12013 change_height_p = 1;
12015 row = it.glyph_row - 1;
12017 /* If there are blank lines at the end, except for a partially
12018 visible blank line at the end that is smaller than
12019 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12020 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12021 && row->height >= FRAME_LINE_HEIGHT (f))
12022 change_height_p = 1;
12024 /* If row displays tool-bar items, but is partially visible,
12025 change the tool-bar's height. */
12026 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12027 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12028 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12029 change_height_p = 1;
12031 /* Resize windows as needed by changing the `tool-bar-lines'
12032 frame parameter. */
12033 if (change_height_p)
12035 Lisp_Object frame;
12036 int old_height = WINDOW_TOTAL_LINES (w);
12037 int nrows;
12038 int nlines = tool_bar_lines_needed (f, &nrows);
12040 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12041 && !f->minimize_tool_bar_window_p)
12042 ? (nlines > old_height)
12043 : (nlines != old_height));
12044 f->minimize_tool_bar_window_p = 0;
12046 if (change_height_p)
12048 XSETFRAME (frame, f);
12049 Fmodify_frame_parameters (frame,
12050 list1 (Fcons (Qtool_bar_lines,
12051 make_number (nlines))));
12052 if (WINDOW_TOTAL_LINES (w) != old_height)
12054 clear_glyph_matrix (w->desired_matrix);
12055 f->n_tool_bar_rows = nrows;
12056 f->fonts_changed = 1;
12057 return 1;
12063 f->minimize_tool_bar_window_p = 0;
12064 return 0;
12066 #endif /* USE_GTK || HAVE_NS */
12069 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12071 /* Get information about the tool-bar item which is displayed in GLYPH
12072 on frame F. Return in *PROP_IDX the index where tool-bar item
12073 properties start in F->tool_bar_items. Value is zero if
12074 GLYPH doesn't display a tool-bar item. */
12076 static int
12077 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12079 Lisp_Object prop;
12080 int success_p;
12081 int charpos;
12083 /* This function can be called asynchronously, which means we must
12084 exclude any possibility that Fget_text_property signals an
12085 error. */
12086 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12087 charpos = max (0, charpos);
12089 /* Get the text property `menu-item' at pos. The value of that
12090 property is the start index of this item's properties in
12091 F->tool_bar_items. */
12092 prop = Fget_text_property (make_number (charpos),
12093 Qmenu_item, f->current_tool_bar_string);
12094 if (INTEGERP (prop))
12096 *prop_idx = XINT (prop);
12097 success_p = 1;
12099 else
12100 success_p = 0;
12102 return success_p;
12106 /* Get information about the tool-bar item at position X/Y on frame F.
12107 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12108 the current matrix of the tool-bar window of F, or NULL if not
12109 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12110 item in F->tool_bar_items. Value is
12112 -1 if X/Y is not on a tool-bar item
12113 0 if X/Y is on the same item that was highlighted before.
12114 1 otherwise. */
12116 static int
12117 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12118 int *hpos, int *vpos, int *prop_idx)
12120 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12121 struct window *w = XWINDOW (f->tool_bar_window);
12122 int area;
12124 /* Find the glyph under X/Y. */
12125 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12126 if (*glyph == NULL)
12127 return -1;
12129 /* Get the start of this tool-bar item's properties in
12130 f->tool_bar_items. */
12131 if (!tool_bar_item_info (f, *glyph, prop_idx))
12132 return -1;
12134 /* Is mouse on the highlighted item? */
12135 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12136 && *vpos >= hlinfo->mouse_face_beg_row
12137 && *vpos <= hlinfo->mouse_face_end_row
12138 && (*vpos > hlinfo->mouse_face_beg_row
12139 || *hpos >= hlinfo->mouse_face_beg_col)
12140 && (*vpos < hlinfo->mouse_face_end_row
12141 || *hpos < hlinfo->mouse_face_end_col
12142 || hlinfo->mouse_face_past_end))
12143 return 0;
12145 return 1;
12149 /* EXPORT:
12150 Handle mouse button event on the tool-bar of frame F, at
12151 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12152 0 for button release. MODIFIERS is event modifiers for button
12153 release. */
12155 void
12156 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12157 int modifiers)
12159 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12160 struct window *w = XWINDOW (f->tool_bar_window);
12161 int hpos, vpos, prop_idx;
12162 struct glyph *glyph;
12163 Lisp_Object enabled_p;
12164 int ts;
12166 /* If not on the highlighted tool-bar item, and mouse-highlight is
12167 non-nil, return. This is so we generate the tool-bar button
12168 click only when the mouse button is released on the same item as
12169 where it was pressed. However, when mouse-highlight is disabled,
12170 generate the click when the button is released regardless of the
12171 highlight, since tool-bar items are not highlighted in that
12172 case. */
12173 frame_to_window_pixel_xy (w, &x, &y);
12174 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12175 if (ts == -1
12176 || (ts != 0 && !NILP (Vmouse_highlight)))
12177 return;
12179 /* When mouse-highlight is off, generate the click for the item
12180 where the button was pressed, disregarding where it was
12181 released. */
12182 if (NILP (Vmouse_highlight) && !down_p)
12183 prop_idx = last_tool_bar_item;
12185 /* If item is disabled, do nothing. */
12186 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12187 if (NILP (enabled_p))
12188 return;
12190 if (down_p)
12192 /* Show item in pressed state. */
12193 if (!NILP (Vmouse_highlight))
12194 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12195 last_tool_bar_item = prop_idx;
12197 else
12199 Lisp_Object key, frame;
12200 struct input_event event;
12201 EVENT_INIT (event);
12203 /* Show item in released state. */
12204 if (!NILP (Vmouse_highlight))
12205 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12207 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12209 XSETFRAME (frame, f);
12210 event.kind = TOOL_BAR_EVENT;
12211 event.frame_or_window = frame;
12212 event.arg = frame;
12213 kbd_buffer_store_event (&event);
12215 event.kind = TOOL_BAR_EVENT;
12216 event.frame_or_window = frame;
12217 event.arg = key;
12218 event.modifiers = modifiers;
12219 kbd_buffer_store_event (&event);
12220 last_tool_bar_item = -1;
12225 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12226 tool-bar window-relative coordinates X/Y. Called from
12227 note_mouse_highlight. */
12229 static void
12230 note_tool_bar_highlight (struct frame *f, int x, int y)
12232 Lisp_Object window = f->tool_bar_window;
12233 struct window *w = XWINDOW (window);
12234 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12235 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12236 int hpos, vpos;
12237 struct glyph *glyph;
12238 struct glyph_row *row;
12239 int i;
12240 Lisp_Object enabled_p;
12241 int prop_idx;
12242 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12243 int mouse_down_p, rc;
12245 /* Function note_mouse_highlight is called with negative X/Y
12246 values when mouse moves outside of the frame. */
12247 if (x <= 0 || y <= 0)
12249 clear_mouse_face (hlinfo);
12250 return;
12253 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12254 if (rc < 0)
12256 /* Not on tool-bar item. */
12257 clear_mouse_face (hlinfo);
12258 return;
12260 else if (rc == 0)
12261 /* On same tool-bar item as before. */
12262 goto set_help_echo;
12264 clear_mouse_face (hlinfo);
12266 /* Mouse is down, but on different tool-bar item? */
12267 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12268 && f == dpyinfo->last_mouse_frame);
12270 if (mouse_down_p
12271 && last_tool_bar_item != prop_idx)
12272 return;
12274 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12276 /* If tool-bar item is not enabled, don't highlight it. */
12277 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12278 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12280 /* Compute the x-position of the glyph. In front and past the
12281 image is a space. We include this in the highlighted area. */
12282 row = MATRIX_ROW (w->current_matrix, vpos);
12283 for (i = x = 0; i < hpos; ++i)
12284 x += row->glyphs[TEXT_AREA][i].pixel_width;
12286 /* Record this as the current active region. */
12287 hlinfo->mouse_face_beg_col = hpos;
12288 hlinfo->mouse_face_beg_row = vpos;
12289 hlinfo->mouse_face_beg_x = x;
12290 hlinfo->mouse_face_past_end = 0;
12292 hlinfo->mouse_face_end_col = hpos + 1;
12293 hlinfo->mouse_face_end_row = vpos;
12294 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12295 hlinfo->mouse_face_window = window;
12296 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12298 /* Display it as active. */
12299 show_mouse_face (hlinfo, draw);
12302 set_help_echo:
12304 /* Set help_echo_string to a help string to display for this tool-bar item.
12305 XTread_socket does the rest. */
12306 help_echo_object = help_echo_window = Qnil;
12307 help_echo_pos = -1;
12308 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12309 if (NILP (help_echo_string))
12310 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12313 #endif /* !USE_GTK && !HAVE_NS */
12315 #endif /* HAVE_WINDOW_SYSTEM */
12319 /************************************************************************
12320 Horizontal scrolling
12321 ************************************************************************/
12323 static int hscroll_window_tree (Lisp_Object);
12324 static int hscroll_windows (Lisp_Object);
12326 /* For all leaf windows in the window tree rooted at WINDOW, set their
12327 hscroll value so that PT is (i) visible in the window, and (ii) so
12328 that it is not within a certain margin at the window's left and
12329 right border. Value is non-zero if any window's hscroll has been
12330 changed. */
12332 static int
12333 hscroll_window_tree (Lisp_Object window)
12335 int hscrolled_p = 0;
12336 int hscroll_relative_p = FLOATP (Vhscroll_step);
12337 int hscroll_step_abs = 0;
12338 double hscroll_step_rel = 0;
12340 if (hscroll_relative_p)
12342 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12343 if (hscroll_step_rel < 0)
12345 hscroll_relative_p = 0;
12346 hscroll_step_abs = 0;
12349 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12351 hscroll_step_abs = XINT (Vhscroll_step);
12352 if (hscroll_step_abs < 0)
12353 hscroll_step_abs = 0;
12355 else
12356 hscroll_step_abs = 0;
12358 while (WINDOWP (window))
12360 struct window *w = XWINDOW (window);
12362 if (WINDOWP (w->contents))
12363 hscrolled_p |= hscroll_window_tree (w->contents);
12364 else if (w->cursor.vpos >= 0)
12366 int h_margin;
12367 int text_area_width;
12368 struct glyph_row *current_cursor_row
12369 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12370 struct glyph_row *desired_cursor_row
12371 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12372 struct glyph_row *cursor_row
12373 = (desired_cursor_row->enabled_p
12374 ? desired_cursor_row
12375 : current_cursor_row);
12376 int row_r2l_p = cursor_row->reversed_p;
12378 text_area_width = window_box_width (w, TEXT_AREA);
12380 /* Scroll when cursor is inside this scroll margin. */
12381 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12383 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12384 /* For left-to-right rows, hscroll when cursor is either
12385 (i) inside the right hscroll margin, or (ii) if it is
12386 inside the left margin and the window is already
12387 hscrolled. */
12388 && ((!row_r2l_p
12389 && ((w->hscroll
12390 && w->cursor.x <= h_margin)
12391 || (cursor_row->enabled_p
12392 && cursor_row->truncated_on_right_p
12393 && (w->cursor.x >= text_area_width - h_margin))))
12394 /* For right-to-left rows, the logic is similar,
12395 except that rules for scrolling to left and right
12396 are reversed. E.g., if cursor.x <= h_margin, we
12397 need to hscroll "to the right" unconditionally,
12398 and that will scroll the screen to the left so as
12399 to reveal the next portion of the row. */
12400 || (row_r2l_p
12401 && ((cursor_row->enabled_p
12402 /* FIXME: It is confusing to set the
12403 truncated_on_right_p flag when R2L rows
12404 are actually truncated on the left. */
12405 && cursor_row->truncated_on_right_p
12406 && w->cursor.x <= h_margin)
12407 || (w->hscroll
12408 && (w->cursor.x >= text_area_width - h_margin))))))
12410 struct it it;
12411 ptrdiff_t hscroll;
12412 struct buffer *saved_current_buffer;
12413 ptrdiff_t pt;
12414 int wanted_x;
12416 /* Find point in a display of infinite width. */
12417 saved_current_buffer = current_buffer;
12418 current_buffer = XBUFFER (w->contents);
12420 if (w == XWINDOW (selected_window))
12421 pt = PT;
12422 else
12423 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12425 /* Move iterator to pt starting at cursor_row->start in
12426 a line with infinite width. */
12427 init_to_row_start (&it, w, cursor_row);
12428 it.last_visible_x = INFINITY;
12429 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12430 current_buffer = saved_current_buffer;
12432 /* Position cursor in window. */
12433 if (!hscroll_relative_p && hscroll_step_abs == 0)
12434 hscroll = max (0, (it.current_x
12435 - (ITERATOR_AT_END_OF_LINE_P (&it)
12436 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12437 : (text_area_width / 2))))
12438 / FRAME_COLUMN_WIDTH (it.f);
12439 else if ((!row_r2l_p
12440 && w->cursor.x >= text_area_width - h_margin)
12441 || (row_r2l_p && w->cursor.x <= h_margin))
12443 if (hscroll_relative_p)
12444 wanted_x = text_area_width * (1 - hscroll_step_rel)
12445 - h_margin;
12446 else
12447 wanted_x = text_area_width
12448 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12449 - h_margin;
12450 hscroll
12451 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12453 else
12455 if (hscroll_relative_p)
12456 wanted_x = text_area_width * hscroll_step_rel
12457 + h_margin;
12458 else
12459 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12460 + h_margin;
12461 hscroll
12462 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12464 hscroll = max (hscroll, w->min_hscroll);
12466 /* Don't prevent redisplay optimizations if hscroll
12467 hasn't changed, as it will unnecessarily slow down
12468 redisplay. */
12469 if (w->hscroll != hscroll)
12471 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12472 w->hscroll = hscroll;
12473 hscrolled_p = 1;
12478 window = w->next;
12481 /* Value is non-zero if hscroll of any leaf window has been changed. */
12482 return hscrolled_p;
12486 /* Set hscroll so that cursor is visible and not inside horizontal
12487 scroll margins for all windows in the tree rooted at WINDOW. See
12488 also hscroll_window_tree above. Value is non-zero if any window's
12489 hscroll has been changed. If it has, desired matrices on the frame
12490 of WINDOW are cleared. */
12492 static int
12493 hscroll_windows (Lisp_Object window)
12495 int hscrolled_p = hscroll_window_tree (window);
12496 if (hscrolled_p)
12497 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12498 return hscrolled_p;
12503 /************************************************************************
12504 Redisplay
12505 ************************************************************************/
12507 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12508 to a non-zero value. This is sometimes handy to have in a debugger
12509 session. */
12511 #ifdef GLYPH_DEBUG
12513 /* First and last unchanged row for try_window_id. */
12515 static int debug_first_unchanged_at_end_vpos;
12516 static int debug_last_unchanged_at_beg_vpos;
12518 /* Delta vpos and y. */
12520 static int debug_dvpos, debug_dy;
12522 /* Delta in characters and bytes for try_window_id. */
12524 static ptrdiff_t debug_delta, debug_delta_bytes;
12526 /* Values of window_end_pos and window_end_vpos at the end of
12527 try_window_id. */
12529 static ptrdiff_t debug_end_vpos;
12531 /* Append a string to W->desired_matrix->method. FMT is a printf
12532 format string. If trace_redisplay_p is non-zero also printf the
12533 resulting string to stderr. */
12535 static void debug_method_add (struct window *, char const *, ...)
12536 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12538 static void
12539 debug_method_add (struct window *w, char const *fmt, ...)
12541 void *ptr = w;
12542 char *method = w->desired_matrix->method;
12543 int len = strlen (method);
12544 int size = sizeof w->desired_matrix->method;
12545 int remaining = size - len - 1;
12546 va_list ap;
12548 if (len && remaining)
12550 method[len] = '|';
12551 --remaining, ++len;
12554 va_start (ap, fmt);
12555 vsnprintf (method + len, remaining + 1, fmt, ap);
12556 va_end (ap);
12558 if (trace_redisplay_p)
12559 fprintf (stderr, "%p (%s): %s\n",
12560 ptr,
12561 ((BUFFERP (w->contents)
12562 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12563 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12564 : "no buffer"),
12565 method + len);
12568 #endif /* GLYPH_DEBUG */
12571 /* Value is non-zero if all changes in window W, which displays
12572 current_buffer, are in the text between START and END. START is a
12573 buffer position, END is given as a distance from Z. Used in
12574 redisplay_internal for display optimization. */
12576 static int
12577 text_outside_line_unchanged_p (struct window *w,
12578 ptrdiff_t start, ptrdiff_t end)
12580 int unchanged_p = 1;
12582 /* If text or overlays have changed, see where. */
12583 if (window_outdated (w))
12585 /* Gap in the line? */
12586 if (GPT < start || Z - GPT < end)
12587 unchanged_p = 0;
12589 /* Changes start in front of the line, or end after it? */
12590 if (unchanged_p
12591 && (BEG_UNCHANGED < start - 1
12592 || END_UNCHANGED < end))
12593 unchanged_p = 0;
12595 /* If selective display, can't optimize if changes start at the
12596 beginning of the line. */
12597 if (unchanged_p
12598 && INTEGERP (BVAR (current_buffer, selective_display))
12599 && XINT (BVAR (current_buffer, selective_display)) > 0
12600 && (BEG_UNCHANGED < start || GPT <= start))
12601 unchanged_p = 0;
12603 /* If there are overlays at the start or end of the line, these
12604 may have overlay strings with newlines in them. A change at
12605 START, for instance, may actually concern the display of such
12606 overlay strings as well, and they are displayed on different
12607 lines. So, quickly rule out this case. (For the future, it
12608 might be desirable to implement something more telling than
12609 just BEG/END_UNCHANGED.) */
12610 if (unchanged_p)
12612 if (BEG + BEG_UNCHANGED == start
12613 && overlay_touches_p (start))
12614 unchanged_p = 0;
12615 if (END_UNCHANGED == end
12616 && overlay_touches_p (Z - end))
12617 unchanged_p = 0;
12620 /* Under bidi reordering, adding or deleting a character in the
12621 beginning of a paragraph, before the first strong directional
12622 character, can change the base direction of the paragraph (unless
12623 the buffer specifies a fixed paragraph direction), which will
12624 require to redisplay the whole paragraph. It might be worthwhile
12625 to find the paragraph limits and widen the range of redisplayed
12626 lines to that, but for now just give up this optimization. */
12627 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12628 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12629 unchanged_p = 0;
12632 return unchanged_p;
12636 /* Do a frame update, taking possible shortcuts into account. This is
12637 the main external entry point for redisplay.
12639 If the last redisplay displayed an echo area message and that message
12640 is no longer requested, we clear the echo area or bring back the
12641 mini-buffer if that is in use. */
12643 void
12644 redisplay (void)
12646 redisplay_internal ();
12650 static Lisp_Object
12651 overlay_arrow_string_or_property (Lisp_Object var)
12653 Lisp_Object val;
12655 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12656 return val;
12658 return Voverlay_arrow_string;
12661 /* Return 1 if there are any overlay-arrows in current_buffer. */
12662 static int
12663 overlay_arrow_in_current_buffer_p (void)
12665 Lisp_Object vlist;
12667 for (vlist = Voverlay_arrow_variable_list;
12668 CONSP (vlist);
12669 vlist = XCDR (vlist))
12671 Lisp_Object var = XCAR (vlist);
12672 Lisp_Object val;
12674 if (!SYMBOLP (var))
12675 continue;
12676 val = find_symbol_value (var);
12677 if (MARKERP (val)
12678 && current_buffer == XMARKER (val)->buffer)
12679 return 1;
12681 return 0;
12685 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12686 has changed. */
12688 static int
12689 overlay_arrows_changed_p (void)
12691 Lisp_Object vlist;
12693 for (vlist = Voverlay_arrow_variable_list;
12694 CONSP (vlist);
12695 vlist = XCDR (vlist))
12697 Lisp_Object var = XCAR (vlist);
12698 Lisp_Object val, pstr;
12700 if (!SYMBOLP (var))
12701 continue;
12702 val = find_symbol_value (var);
12703 if (!MARKERP (val))
12704 continue;
12705 if (! EQ (COERCE_MARKER (val),
12706 Fget (var, Qlast_arrow_position))
12707 || ! (pstr = overlay_arrow_string_or_property (var),
12708 EQ (pstr, Fget (var, Qlast_arrow_string))))
12709 return 1;
12711 return 0;
12714 /* Mark overlay arrows to be updated on next redisplay. */
12716 static void
12717 update_overlay_arrows (int up_to_date)
12719 Lisp_Object vlist;
12721 for (vlist = Voverlay_arrow_variable_list;
12722 CONSP (vlist);
12723 vlist = XCDR (vlist))
12725 Lisp_Object var = XCAR (vlist);
12727 if (!SYMBOLP (var))
12728 continue;
12730 if (up_to_date > 0)
12732 Lisp_Object val = find_symbol_value (var);
12733 Fput (var, Qlast_arrow_position,
12734 COERCE_MARKER (val));
12735 Fput (var, Qlast_arrow_string,
12736 overlay_arrow_string_or_property (var));
12738 else if (up_to_date < 0
12739 || !NILP (Fget (var, Qlast_arrow_position)))
12741 Fput (var, Qlast_arrow_position, Qt);
12742 Fput (var, Qlast_arrow_string, Qt);
12748 /* Return overlay arrow string to display at row.
12749 Return integer (bitmap number) for arrow bitmap in left fringe.
12750 Return nil if no overlay arrow. */
12752 static Lisp_Object
12753 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12755 Lisp_Object vlist;
12757 for (vlist = Voverlay_arrow_variable_list;
12758 CONSP (vlist);
12759 vlist = XCDR (vlist))
12761 Lisp_Object var = XCAR (vlist);
12762 Lisp_Object val;
12764 if (!SYMBOLP (var))
12765 continue;
12767 val = find_symbol_value (var);
12769 if (MARKERP (val)
12770 && current_buffer == XMARKER (val)->buffer
12771 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12773 if (FRAME_WINDOW_P (it->f)
12774 /* FIXME: if ROW->reversed_p is set, this should test
12775 the right fringe, not the left one. */
12776 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12778 #ifdef HAVE_WINDOW_SYSTEM
12779 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12781 int fringe_bitmap;
12782 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12783 return make_number (fringe_bitmap);
12785 #endif
12786 return make_number (-1); /* Use default arrow bitmap. */
12788 return overlay_arrow_string_or_property (var);
12792 return Qnil;
12795 /* Return 1 if point moved out of or into a composition. Otherwise
12796 return 0. PREV_BUF and PREV_PT are the last point buffer and
12797 position. BUF and PT are the current point buffer and position. */
12799 static int
12800 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12801 struct buffer *buf, ptrdiff_t pt)
12803 ptrdiff_t start, end;
12804 Lisp_Object prop;
12805 Lisp_Object buffer;
12807 XSETBUFFER (buffer, buf);
12808 /* Check a composition at the last point if point moved within the
12809 same buffer. */
12810 if (prev_buf == buf)
12812 if (prev_pt == pt)
12813 /* Point didn't move. */
12814 return 0;
12816 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12817 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12818 && composition_valid_p (start, end, prop)
12819 && start < prev_pt && end > prev_pt)
12820 /* The last point was within the composition. Return 1 iff
12821 point moved out of the composition. */
12822 return (pt <= start || pt >= end);
12825 /* Check a composition at the current point. */
12826 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12827 && find_composition (pt, -1, &start, &end, &prop, buffer)
12828 && composition_valid_p (start, end, prop)
12829 && start < pt && end > pt);
12832 /* Reconsider the clip changes of buffer which is displayed in W. */
12834 static void
12835 reconsider_clip_changes (struct window *w)
12837 struct buffer *b = XBUFFER (w->contents);
12839 if (b->clip_changed
12840 && w->window_end_valid
12841 && w->current_matrix->buffer == b
12842 && w->current_matrix->zv == BUF_ZV (b)
12843 && w->current_matrix->begv == BUF_BEGV (b))
12844 b->clip_changed = 0;
12846 /* If display wasn't paused, and W is not a tool bar window, see if
12847 point has been moved into or out of a composition. In that case,
12848 we set b->clip_changed to 1 to force updating the screen. If
12849 b->clip_changed has already been set to 1, we can skip this
12850 check. */
12851 if (!b->clip_changed && w->window_end_valid)
12853 ptrdiff_t pt = (w == XWINDOW (selected_window)
12854 ? PT : marker_position (w->pointm));
12856 if ((w->current_matrix->buffer != b || pt != w->last_point)
12857 && check_point_in_composition (w->current_matrix->buffer,
12858 w->last_point, b, pt))
12859 b->clip_changed = 1;
12863 #define STOP_POLLING \
12864 do { if (! polling_stopped_here) stop_polling (); \
12865 polling_stopped_here = 1; } while (0)
12867 #define RESUME_POLLING \
12868 do { if (polling_stopped_here) start_polling (); \
12869 polling_stopped_here = 0; } while (0)
12872 /* Perhaps in the future avoid recentering windows if it
12873 is not necessary; currently that causes some problems. */
12875 static void
12876 redisplay_internal (void)
12878 struct window *w = XWINDOW (selected_window);
12879 struct window *sw;
12880 struct frame *fr;
12881 int pending;
12882 bool must_finish = 0, match_p;
12883 struct text_pos tlbufpos, tlendpos;
12884 int number_of_visible_frames;
12885 ptrdiff_t count;
12886 struct frame *sf;
12887 int polling_stopped_here = 0;
12888 Lisp_Object tail, frame;
12890 /* True means redisplay has to consider all windows on all
12891 frames. False, only selected_window is considered. */
12892 bool consider_all_windows_p;
12894 /* True means redisplay has to redisplay the miniwindow. */
12895 bool update_miniwindow_p = false;
12897 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12899 /* No redisplay if running in batch mode or frame is not yet fully
12900 initialized, or redisplay is explicitly turned off by setting
12901 Vinhibit_redisplay. */
12902 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12903 || !NILP (Vinhibit_redisplay))
12904 return;
12906 /* Don't examine these until after testing Vinhibit_redisplay.
12907 When Emacs is shutting down, perhaps because its connection to
12908 X has dropped, we should not look at them at all. */
12909 fr = XFRAME (w->frame);
12910 sf = SELECTED_FRAME ();
12912 if (!fr->glyphs_initialized_p)
12913 return;
12915 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12916 if (popup_activated ())
12917 return;
12918 #endif
12920 /* I don't think this happens but let's be paranoid. */
12921 if (redisplaying_p)
12922 return;
12924 /* Record a function that clears redisplaying_p
12925 when we leave this function. */
12926 count = SPECPDL_INDEX ();
12927 record_unwind_protect_void (unwind_redisplay);
12928 redisplaying_p = 1;
12929 specbind (Qinhibit_free_realized_faces, Qnil);
12931 /* Record this function, so it appears on the profiler's backtraces. */
12932 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12934 FOR_EACH_FRAME (tail, frame)
12935 XFRAME (frame)->already_hscrolled_p = 0;
12937 retry:
12938 /* Remember the currently selected window. */
12939 sw = w;
12941 pending = 0;
12942 last_escape_glyph_frame = NULL;
12943 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12944 last_glyphless_glyph_frame = NULL;
12945 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12947 /* If face_change_count is non-zero, init_iterator will free all
12948 realized faces, which includes the faces referenced from current
12949 matrices. So, we can't reuse current matrices in this case. */
12950 if (face_change_count)
12951 windows_or_buffers_changed = 47;
12953 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12954 && FRAME_TTY (sf)->previous_frame != sf)
12956 /* Since frames on a single ASCII terminal share the same
12957 display area, displaying a different frame means redisplay
12958 the whole thing. */
12959 windows_or_buffers_changed = 48;
12960 SET_FRAME_GARBAGED (sf);
12961 #ifndef DOS_NT
12962 set_tty_color_mode (FRAME_TTY (sf), sf);
12963 #endif
12964 FRAME_TTY (sf)->previous_frame = sf;
12967 /* Set the visible flags for all frames. Do this before checking for
12968 resized or garbaged frames; they want to know if their frames are
12969 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12970 number_of_visible_frames = 0;
12972 FOR_EACH_FRAME (tail, frame)
12974 struct frame *f = XFRAME (frame);
12976 if (FRAME_VISIBLE_P (f))
12978 ++number_of_visible_frames;
12979 /* Adjust matrices for visible frames only. */
12980 if (f->fonts_changed)
12982 adjust_frame_glyphs (f);
12983 f->fonts_changed = 0;
12985 /* If cursor type has been changed on the frame
12986 other than selected, consider all frames. */
12987 if (f != sf && f->cursor_type_changed)
12988 update_mode_lines = 31;
12990 clear_desired_matrices (f);
12993 /* Notice any pending interrupt request to change frame size. */
12994 do_pending_window_change (1);
12996 /* do_pending_window_change could change the selected_window due to
12997 frame resizing which makes the selected window too small. */
12998 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12999 sw = w;
13001 /* Clear frames marked as garbaged. */
13002 clear_garbaged_frames ();
13004 /* Build menubar and tool-bar items. */
13005 if (NILP (Vmemory_full))
13006 prepare_menu_bars ();
13008 if (windows_or_buffers_changed && !update_mode_lines)
13009 update_mode_lines = 32;
13011 reconsider_clip_changes (w);
13013 /* In most cases selected window displays current buffer. */
13014 match_p = XBUFFER (w->contents) == current_buffer;
13015 if (match_p)
13017 /* Detect case that we need to write or remove a star in the mode line. */
13018 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13020 w->update_mode_line = 1;
13021 if (buffer_shared_and_changed ())
13022 update_mode_lines = 33;
13025 if (mode_line_update_needed (w))
13026 w->update_mode_line = 1;
13029 consider_all_windows_p = (update_mode_lines
13030 || buffer_shared_and_changed ());
13032 /* If specs for an arrow have changed, do thorough redisplay
13033 to ensure we remove any arrow that should no longer exist. */
13034 if (overlay_arrows_changed_p ())
13036 consider_all_windows_p = true;
13037 windows_or_buffers_changed = 49;
13040 /* Normally the message* functions will have already displayed and
13041 updated the echo area, but the frame may have been trashed, or
13042 the update may have been preempted, so display the echo area
13043 again here. Checking message_cleared_p captures the case that
13044 the echo area should be cleared. */
13045 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13046 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13047 || (message_cleared_p
13048 && minibuf_level == 0
13049 /* If the mini-window is currently selected, this means the
13050 echo-area doesn't show through. */
13051 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13053 int window_height_changed_p = echo_area_display (0);
13055 if (message_cleared_p)
13056 update_miniwindow_p = true;
13058 must_finish = 1;
13060 /* If we don't display the current message, don't clear the
13061 message_cleared_p flag, because, if we did, we wouldn't clear
13062 the echo area in the next redisplay which doesn't preserve
13063 the echo area. */
13064 if (!display_last_displayed_message_p)
13065 message_cleared_p = 0;
13067 if (window_height_changed_p)
13069 consider_all_windows_p = true;
13070 update_mode_lines = 34;
13071 windows_or_buffers_changed = 50;
13073 /* If window configuration was changed, frames may have been
13074 marked garbaged. Clear them or we will experience
13075 surprises wrt scrolling. */
13076 clear_garbaged_frames ();
13079 else if (EQ (selected_window, minibuf_window)
13080 && (current_buffer->clip_changed || window_outdated (w))
13081 && resize_mini_window (w, 0))
13083 /* Resized active mini-window to fit the size of what it is
13084 showing if its contents might have changed. */
13085 must_finish = 1;
13086 /* FIXME: this causes all frames to be updated, which seems unnecessary
13087 since only the current frame needs to be considered. This function
13088 needs to be rewritten with two variables, consider_all_windows and
13089 consider_all_frames. */
13090 consider_all_windows_p = true;
13091 windows_or_buffers_changed = 51;
13092 update_mode_lines = 35;
13094 /* If window configuration was changed, frames may have been
13095 marked garbaged. Clear them or we will experience
13096 surprises wrt scrolling. */
13097 clear_garbaged_frames ();
13100 if (VECTORP (Vredisplay__all_windows_cause)
13101 && windows_or_buffers_changed >= 0
13102 && windows_or_buffers_changed < ASIZE (Vredisplay__all_windows_cause)
13103 && INTEGERP (AREF (Vredisplay__all_windows_cause,
13104 windows_or_buffers_changed)))
13105 ASET (Vredisplay__all_windows_cause, windows_or_buffers_changed,
13106 make_number (1 + XINT (AREF (Vredisplay__all_windows_cause,
13107 windows_or_buffers_changed))));
13109 if (VECTORP (Vredisplay__mode_lines_cause)
13110 && update_mode_lines >= 0
13111 && update_mode_lines < ASIZE (Vredisplay__mode_lines_cause)
13112 && INTEGERP (AREF (Vredisplay__mode_lines_cause,
13113 update_mode_lines)))
13114 ASET (Vredisplay__mode_lines_cause, update_mode_lines,
13115 make_number (1 + XINT (AREF (Vredisplay__mode_lines_cause,
13116 update_mode_lines))));
13118 /* Optimize the case that only the line containing the cursor in the
13119 selected window has changed. Variables starting with this_ are
13120 set in display_line and record information about the line
13121 containing the cursor. */
13122 tlbufpos = this_line_start_pos;
13123 tlendpos = this_line_end_pos;
13124 if (!consider_all_windows_p
13125 && CHARPOS (tlbufpos) > 0
13126 && !w->update_mode_line
13127 && !current_buffer->clip_changed
13128 && !current_buffer->prevent_redisplay_optimizations_p
13129 && FRAME_VISIBLE_P (XFRAME (w->frame))
13130 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13131 && !XFRAME (w->frame)->cursor_type_changed
13132 /* Make sure recorded data applies to current buffer, etc. */
13133 && this_line_buffer == current_buffer
13134 && match_p
13135 && !w->force_start
13136 && !w->optional_new_start
13137 /* Point must be on the line that we have info recorded about. */
13138 && PT >= CHARPOS (tlbufpos)
13139 && PT <= Z - CHARPOS (tlendpos)
13140 /* All text outside that line, including its final newline,
13141 must be unchanged. */
13142 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13143 CHARPOS (tlendpos)))
13145 if (CHARPOS (tlbufpos) > BEGV
13146 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13147 && (CHARPOS (tlbufpos) == ZV
13148 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13149 /* Former continuation line has disappeared by becoming empty. */
13150 goto cancel;
13151 else if (window_outdated (w) || MINI_WINDOW_P (w))
13153 /* We have to handle the case of continuation around a
13154 wide-column character (see the comment in indent.c around
13155 line 1340).
13157 For instance, in the following case:
13159 -------- Insert --------
13160 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13161 J_I_ ==> J_I_ `^^' are cursors.
13162 ^^ ^^
13163 -------- --------
13165 As we have to redraw the line above, we cannot use this
13166 optimization. */
13168 struct it it;
13169 int line_height_before = this_line_pixel_height;
13171 /* Note that start_display will handle the case that the
13172 line starting at tlbufpos is a continuation line. */
13173 start_display (&it, w, tlbufpos);
13175 /* Implementation note: It this still necessary? */
13176 if (it.current_x != this_line_start_x)
13177 goto cancel;
13179 TRACE ((stderr, "trying display optimization 1\n"));
13180 w->cursor.vpos = -1;
13181 overlay_arrow_seen = 0;
13182 it.vpos = this_line_vpos;
13183 it.current_y = this_line_y;
13184 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13185 display_line (&it);
13187 /* If line contains point, is not continued,
13188 and ends at same distance from eob as before, we win. */
13189 if (w->cursor.vpos >= 0
13190 /* Line is not continued, otherwise this_line_start_pos
13191 would have been set to 0 in display_line. */
13192 && CHARPOS (this_line_start_pos)
13193 /* Line ends as before. */
13194 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13195 /* Line has same height as before. Otherwise other lines
13196 would have to be shifted up or down. */
13197 && this_line_pixel_height == line_height_before)
13199 /* If this is not the window's last line, we must adjust
13200 the charstarts of the lines below. */
13201 if (it.current_y < it.last_visible_y)
13203 struct glyph_row *row
13204 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13205 ptrdiff_t delta, delta_bytes;
13207 /* We used to distinguish between two cases here,
13208 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13209 when the line ends in a newline or the end of the
13210 buffer's accessible portion. But both cases did
13211 the same, so they were collapsed. */
13212 delta = (Z
13213 - CHARPOS (tlendpos)
13214 - MATRIX_ROW_START_CHARPOS (row));
13215 delta_bytes = (Z_BYTE
13216 - BYTEPOS (tlendpos)
13217 - MATRIX_ROW_START_BYTEPOS (row));
13219 increment_matrix_positions (w->current_matrix,
13220 this_line_vpos + 1,
13221 w->current_matrix->nrows,
13222 delta, delta_bytes);
13225 /* If this row displays text now but previously didn't,
13226 or vice versa, w->window_end_vpos may have to be
13227 adjusted. */
13228 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13230 if (w->window_end_vpos < this_line_vpos)
13231 w->window_end_vpos = this_line_vpos;
13233 else if (w->window_end_vpos == this_line_vpos
13234 && this_line_vpos > 0)
13235 w->window_end_vpos = this_line_vpos - 1;
13236 w->window_end_valid = 0;
13238 /* Update hint: No need to try to scroll in update_window. */
13239 w->desired_matrix->no_scrolling_p = 1;
13241 #ifdef GLYPH_DEBUG
13242 *w->desired_matrix->method = 0;
13243 debug_method_add (w, "optimization 1");
13244 #endif
13245 #ifdef HAVE_WINDOW_SYSTEM
13246 update_window_fringes (w, 0);
13247 #endif
13248 goto update;
13250 else
13251 goto cancel;
13253 else if (/* Cursor position hasn't changed. */
13254 PT == w->last_point
13255 /* Make sure the cursor was last displayed
13256 in this window. Otherwise we have to reposition it. */
13257 && 0 <= w->cursor.vpos
13258 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13260 if (!must_finish)
13262 do_pending_window_change (1);
13263 /* If selected_window changed, redisplay again. */
13264 if (WINDOWP (selected_window)
13265 && (w = XWINDOW (selected_window)) != sw)
13266 goto retry;
13268 /* We used to always goto end_of_redisplay here, but this
13269 isn't enough if we have a blinking cursor. */
13270 if (w->cursor_off_p == w->last_cursor_off_p)
13271 goto end_of_redisplay;
13273 goto update;
13275 /* If highlighting the region, or if the cursor is in the echo area,
13276 then we can't just move the cursor. */
13277 else if (NILP (Vshow_trailing_whitespace)
13278 && !cursor_in_echo_area)
13280 struct it it;
13281 struct glyph_row *row;
13283 /* Skip from tlbufpos to PT and see where it is. Note that
13284 PT may be in invisible text. If so, we will end at the
13285 next visible position. */
13286 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13287 NULL, DEFAULT_FACE_ID);
13288 it.current_x = this_line_start_x;
13289 it.current_y = this_line_y;
13290 it.vpos = this_line_vpos;
13292 /* The call to move_it_to stops in front of PT, but
13293 moves over before-strings. */
13294 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13296 if (it.vpos == this_line_vpos
13297 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13298 row->enabled_p))
13300 eassert (this_line_vpos == it.vpos);
13301 eassert (this_line_y == it.current_y);
13302 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13303 #ifdef GLYPH_DEBUG
13304 *w->desired_matrix->method = 0;
13305 debug_method_add (w, "optimization 3");
13306 #endif
13307 goto update;
13309 else
13310 goto cancel;
13313 cancel:
13314 /* Text changed drastically or point moved off of line. */
13315 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13318 CHARPOS (this_line_start_pos) = 0;
13319 ++clear_face_cache_count;
13320 #ifdef HAVE_WINDOW_SYSTEM
13321 ++clear_image_cache_count;
13322 #endif
13324 /* Build desired matrices, and update the display. If
13325 consider_all_windows_p is non-zero, do it for all windows on all
13326 frames. Otherwise do it for selected_window, only. */
13328 if (consider_all_windows_p)
13330 FOR_EACH_FRAME (tail, frame)
13331 XFRAME (frame)->updated_p = 0;
13333 FOR_EACH_FRAME (tail, frame)
13335 struct frame *f = XFRAME (frame);
13337 /* We don't have to do anything for unselected terminal
13338 frames. */
13339 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13340 && !EQ (FRAME_TTY (f)->top_frame, frame))
13341 continue;
13343 retry_frame:
13345 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13347 /* Mark all the scroll bars to be removed; we'll redeem
13348 the ones we want when we redisplay their windows. */
13349 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13350 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13352 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13353 redisplay_windows (FRAME_ROOT_WINDOW (f));
13355 /* The X error handler may have deleted that frame. */
13356 if (!FRAME_LIVE_P (f))
13357 continue;
13359 /* Any scroll bars which redisplay_windows should have
13360 nuked should now go away. */
13361 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13362 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13364 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13366 /* If fonts changed on visible frame, display again. */
13367 if (f->fonts_changed)
13369 adjust_frame_glyphs (f);
13370 f->fonts_changed = 0;
13371 goto retry_frame;
13374 /* See if we have to hscroll. */
13375 if (!f->already_hscrolled_p)
13377 f->already_hscrolled_p = 1;
13378 if (hscroll_windows (f->root_window))
13379 goto retry_frame;
13382 /* Prevent various kinds of signals during display
13383 update. stdio is not robust about handling
13384 signals, which can cause an apparent I/O
13385 error. */
13386 if (interrupt_input)
13387 unrequest_sigio ();
13388 STOP_POLLING;
13390 /* Mark windows on frame F to update. If we decide to
13391 update all frames but windows_or_buffers_changed is
13392 zero, we assume that only the windows that shows
13393 current buffer should be really updated. */
13394 set_window_update_flags
13395 (XWINDOW (f->root_window),
13396 (windows_or_buffers_changed ? NULL : current_buffer), 1);
13397 pending |= update_frame (f, 0, 0);
13398 f->cursor_type_changed = 0;
13399 f->updated_p = 1;
13404 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13406 if (!pending)
13408 /* Do the mark_window_display_accurate after all windows have
13409 been redisplayed because this call resets flags in buffers
13410 which are needed for proper redisplay. */
13411 FOR_EACH_FRAME (tail, frame)
13413 struct frame *f = XFRAME (frame);
13414 if (f->updated_p)
13416 mark_window_display_accurate (f->root_window, 1);
13417 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13418 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13423 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13425 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13426 struct frame *mini_frame;
13428 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13429 /* Use list_of_error, not Qerror, so that
13430 we catch only errors and don't run the debugger. */
13431 internal_condition_case_1 (redisplay_window_1, selected_window,
13432 list_of_error,
13433 redisplay_window_error);
13434 if (update_miniwindow_p)
13435 internal_condition_case_1 (redisplay_window_1, mini_window,
13436 list_of_error,
13437 redisplay_window_error);
13439 /* Compare desired and current matrices, perform output. */
13441 update:
13442 /* If fonts changed, display again. */
13443 if (sf->fonts_changed)
13444 goto retry;
13446 /* Prevent various kinds of signals during display update.
13447 stdio is not robust about handling signals,
13448 which can cause an apparent I/O error. */
13449 if (interrupt_input)
13450 unrequest_sigio ();
13451 STOP_POLLING;
13453 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13455 if (hscroll_windows (selected_window))
13456 goto retry;
13458 XWINDOW (selected_window)->must_be_updated_p = 1;
13459 pending = update_frame (sf, 0, 0);
13460 sf->cursor_type_changed = 0;
13463 /* We may have called echo_area_display at the top of this
13464 function. If the echo area is on another frame, that may
13465 have put text on a frame other than the selected one, so the
13466 above call to update_frame would not have caught it. Catch
13467 it here. */
13468 mini_window = FRAME_MINIBUF_WINDOW (sf);
13469 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13471 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13473 XWINDOW (mini_window)->must_be_updated_p = 1;
13474 pending |= update_frame (mini_frame, 0, 0);
13475 mini_frame->cursor_type_changed = 0;
13476 if (!pending && hscroll_windows (mini_window))
13477 goto retry;
13481 /* If display was paused because of pending input, make sure we do a
13482 thorough update the next time. */
13483 if (pending)
13485 /* Prevent the optimization at the beginning of
13486 redisplay_internal that tries a single-line update of the
13487 line containing the cursor in the selected window. */
13488 CHARPOS (this_line_start_pos) = 0;
13490 /* Let the overlay arrow be updated the next time. */
13491 update_overlay_arrows (0);
13493 /* If we pause after scrolling, some rows in the current
13494 matrices of some windows are not valid. */
13495 if (!WINDOW_FULL_WIDTH_P (w)
13496 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13497 update_mode_lines = 36;
13499 else
13501 if (!consider_all_windows_p)
13503 /* This has already been done above if
13504 consider_all_windows_p is set. */
13505 mark_window_display_accurate_1 (w, 1);
13507 /* Say overlay arrows are up to date. */
13508 update_overlay_arrows (1);
13510 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13511 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13514 update_mode_lines = 0;
13515 windows_or_buffers_changed = 0;
13518 /* Start SIGIO interrupts coming again. Having them off during the
13519 code above makes it less likely one will discard output, but not
13520 impossible, since there might be stuff in the system buffer here.
13521 But it is much hairier to try to do anything about that. */
13522 if (interrupt_input)
13523 request_sigio ();
13524 RESUME_POLLING;
13526 /* If a frame has become visible which was not before, redisplay
13527 again, so that we display it. Expose events for such a frame
13528 (which it gets when becoming visible) don't call the parts of
13529 redisplay constructing glyphs, so simply exposing a frame won't
13530 display anything in this case. So, we have to display these
13531 frames here explicitly. */
13532 if (!pending)
13534 int new_count = 0;
13536 FOR_EACH_FRAME (tail, frame)
13538 int this_is_visible = 0;
13540 if (XFRAME (frame)->visible)
13541 this_is_visible = 1;
13543 if (this_is_visible)
13544 new_count++;
13547 if (new_count != number_of_visible_frames)
13548 windows_or_buffers_changed = 52;
13551 /* Change frame size now if a change is pending. */
13552 do_pending_window_change (1);
13554 /* If we just did a pending size change, or have additional
13555 visible frames, or selected_window changed, redisplay again. */
13556 if ((windows_or_buffers_changed && !pending)
13557 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13558 goto retry;
13560 /* Clear the face and image caches.
13562 We used to do this only if consider_all_windows_p. But the cache
13563 needs to be cleared if a timer creates images in the current
13564 buffer (e.g. the test case in Bug#6230). */
13566 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13568 clear_face_cache (0);
13569 clear_face_cache_count = 0;
13572 #ifdef HAVE_WINDOW_SYSTEM
13573 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13575 clear_image_caches (Qnil);
13576 clear_image_cache_count = 0;
13578 #endif /* HAVE_WINDOW_SYSTEM */
13580 end_of_redisplay:
13581 unbind_to (count, Qnil);
13582 RESUME_POLLING;
13586 /* Redisplay, but leave alone any recent echo area message unless
13587 another message has been requested in its place.
13589 This is useful in situations where you need to redisplay but no
13590 user action has occurred, making it inappropriate for the message
13591 area to be cleared. See tracking_off and
13592 wait_reading_process_output for examples of these situations.
13594 FROM_WHERE is an integer saying from where this function was
13595 called. This is useful for debugging. */
13597 void
13598 redisplay_preserve_echo_area (int from_where)
13600 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13602 if (!NILP (echo_area_buffer[1]))
13604 /* We have a previously displayed message, but no current
13605 message. Redisplay the previous message. */
13606 display_last_displayed_message_p = 1;
13607 redisplay_internal ();
13608 display_last_displayed_message_p = 0;
13610 else
13611 redisplay_internal ();
13613 flush_frame (SELECTED_FRAME ());
13617 /* Function registered with record_unwind_protect in redisplay_internal. */
13619 static void
13620 unwind_redisplay (void)
13622 redisplaying_p = 0;
13626 /* Mark the display of leaf window W as accurate or inaccurate.
13627 If ACCURATE_P is non-zero mark display of W as accurate. If
13628 ACCURATE_P is zero, arrange for W to be redisplayed the next
13629 time redisplay_internal is called. */
13631 static void
13632 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13634 struct buffer *b = XBUFFER (w->contents);
13636 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13637 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13638 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13640 if (accurate_p)
13642 b->clip_changed = 0;
13643 b->prevent_redisplay_optimizations_p = 0;
13645 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13646 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13647 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13648 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13650 w->current_matrix->buffer = b;
13651 w->current_matrix->begv = BUF_BEGV (b);
13652 w->current_matrix->zv = BUF_ZV (b);
13654 w->last_cursor_vpos = w->cursor.vpos;
13655 w->last_cursor_off_p = w->cursor_off_p;
13657 if (w == XWINDOW (selected_window))
13658 w->last_point = BUF_PT (b);
13659 else
13660 w->last_point = marker_position (w->pointm);
13662 w->window_end_valid = 1;
13663 w->update_mode_line = 0;
13668 /* Mark the display of windows in the window tree rooted at WINDOW as
13669 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13670 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13671 be redisplayed the next time redisplay_internal is called. */
13673 void
13674 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13676 struct window *w;
13678 for (; !NILP (window); window = w->next)
13680 w = XWINDOW (window);
13681 if (WINDOWP (w->contents))
13682 mark_window_display_accurate (w->contents, accurate_p);
13683 else
13684 mark_window_display_accurate_1 (w, accurate_p);
13687 if (accurate_p)
13688 update_overlay_arrows (1);
13689 else
13690 /* Force a thorough redisplay the next time by setting
13691 last_arrow_position and last_arrow_string to t, which is
13692 unequal to any useful value of Voverlay_arrow_... */
13693 update_overlay_arrows (-1);
13697 /* Return value in display table DP (Lisp_Char_Table *) for character
13698 C. Since a display table doesn't have any parent, we don't have to
13699 follow parent. Do not call this function directly but use the
13700 macro DISP_CHAR_VECTOR. */
13702 Lisp_Object
13703 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13705 Lisp_Object val;
13707 if (ASCII_CHAR_P (c))
13709 val = dp->ascii;
13710 if (SUB_CHAR_TABLE_P (val))
13711 val = XSUB_CHAR_TABLE (val)->contents[c];
13713 else
13715 Lisp_Object table;
13717 XSETCHAR_TABLE (table, dp);
13718 val = char_table_ref (table, c);
13720 if (NILP (val))
13721 val = dp->defalt;
13722 return val;
13727 /***********************************************************************
13728 Window Redisplay
13729 ***********************************************************************/
13731 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13733 static void
13734 redisplay_windows (Lisp_Object window)
13736 while (!NILP (window))
13738 struct window *w = XWINDOW (window);
13740 if (WINDOWP (w->contents))
13741 redisplay_windows (w->contents);
13742 else if (BUFFERP (w->contents))
13744 displayed_buffer = XBUFFER (w->contents);
13745 /* Use list_of_error, not Qerror, so that
13746 we catch only errors and don't run the debugger. */
13747 internal_condition_case_1 (redisplay_window_0, window,
13748 list_of_error,
13749 redisplay_window_error);
13752 window = w->next;
13756 static Lisp_Object
13757 redisplay_window_error (Lisp_Object ignore)
13759 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13760 return Qnil;
13763 static Lisp_Object
13764 redisplay_window_0 (Lisp_Object window)
13766 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13767 redisplay_window (window, 0);
13768 return Qnil;
13771 static Lisp_Object
13772 redisplay_window_1 (Lisp_Object window)
13774 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13775 redisplay_window (window, 1);
13776 return Qnil;
13780 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13781 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13782 which positions recorded in ROW differ from current buffer
13783 positions.
13785 Return 0 if cursor is not on this row, 1 otherwise. */
13787 static int
13788 set_cursor_from_row (struct window *w, struct glyph_row *row,
13789 struct glyph_matrix *matrix,
13790 ptrdiff_t delta, ptrdiff_t delta_bytes,
13791 int dy, int dvpos)
13793 struct glyph *glyph = row->glyphs[TEXT_AREA];
13794 struct glyph *end = glyph + row->used[TEXT_AREA];
13795 struct glyph *cursor = NULL;
13796 /* The last known character position in row. */
13797 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13798 int x = row->x;
13799 ptrdiff_t pt_old = PT - delta;
13800 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13801 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13802 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13803 /* A glyph beyond the edge of TEXT_AREA which we should never
13804 touch. */
13805 struct glyph *glyphs_end = end;
13806 /* Non-zero means we've found a match for cursor position, but that
13807 glyph has the avoid_cursor_p flag set. */
13808 int match_with_avoid_cursor = 0;
13809 /* Non-zero means we've seen at least one glyph that came from a
13810 display string. */
13811 int string_seen = 0;
13812 /* Largest and smallest buffer positions seen so far during scan of
13813 glyph row. */
13814 ptrdiff_t bpos_max = pos_before;
13815 ptrdiff_t bpos_min = pos_after;
13816 /* Last buffer position covered by an overlay string with an integer
13817 `cursor' property. */
13818 ptrdiff_t bpos_covered = 0;
13819 /* Non-zero means the display string on which to display the cursor
13820 comes from a text property, not from an overlay. */
13821 int string_from_text_prop = 0;
13823 /* Don't even try doing anything if called for a mode-line or
13824 header-line row, since the rest of the code isn't prepared to
13825 deal with such calamities. */
13826 eassert (!row->mode_line_p);
13827 if (row->mode_line_p)
13828 return 0;
13830 /* Skip over glyphs not having an object at the start and the end of
13831 the row. These are special glyphs like truncation marks on
13832 terminal frames. */
13833 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13835 if (!row->reversed_p)
13837 while (glyph < end
13838 && INTEGERP (glyph->object)
13839 && glyph->charpos < 0)
13841 x += glyph->pixel_width;
13842 ++glyph;
13844 while (end > glyph
13845 && INTEGERP ((end - 1)->object)
13846 /* CHARPOS is zero for blanks and stretch glyphs
13847 inserted by extend_face_to_end_of_line. */
13848 && (end - 1)->charpos <= 0)
13849 --end;
13850 glyph_before = glyph - 1;
13851 glyph_after = end;
13853 else
13855 struct glyph *g;
13857 /* If the glyph row is reversed, we need to process it from back
13858 to front, so swap the edge pointers. */
13859 glyphs_end = end = glyph - 1;
13860 glyph += row->used[TEXT_AREA] - 1;
13862 while (glyph > end + 1
13863 && INTEGERP (glyph->object)
13864 && glyph->charpos < 0)
13866 --glyph;
13867 x -= glyph->pixel_width;
13869 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13870 --glyph;
13871 /* By default, in reversed rows we put the cursor on the
13872 rightmost (first in the reading order) glyph. */
13873 for (g = end + 1; g < glyph; g++)
13874 x += g->pixel_width;
13875 while (end < glyph
13876 && INTEGERP ((end + 1)->object)
13877 && (end + 1)->charpos <= 0)
13878 ++end;
13879 glyph_before = glyph + 1;
13880 glyph_after = end;
13883 else if (row->reversed_p)
13885 /* In R2L rows that don't display text, put the cursor on the
13886 rightmost glyph. Case in point: an empty last line that is
13887 part of an R2L paragraph. */
13888 cursor = end - 1;
13889 /* Avoid placing the cursor on the last glyph of the row, where
13890 on terminal frames we hold the vertical border between
13891 adjacent windows. */
13892 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13893 && !WINDOW_RIGHTMOST_P (w)
13894 && cursor == row->glyphs[LAST_AREA] - 1)
13895 cursor--;
13896 x = -1; /* will be computed below, at label compute_x */
13899 /* Step 1: Try to find the glyph whose character position
13900 corresponds to point. If that's not possible, find 2 glyphs
13901 whose character positions are the closest to point, one before
13902 point, the other after it. */
13903 if (!row->reversed_p)
13904 while (/* not marched to end of glyph row */
13905 glyph < end
13906 /* glyph was not inserted by redisplay for internal purposes */
13907 && !INTEGERP (glyph->object))
13909 if (BUFFERP (glyph->object))
13911 ptrdiff_t dpos = glyph->charpos - pt_old;
13913 if (glyph->charpos > bpos_max)
13914 bpos_max = glyph->charpos;
13915 if (glyph->charpos < bpos_min)
13916 bpos_min = glyph->charpos;
13917 if (!glyph->avoid_cursor_p)
13919 /* If we hit point, we've found the glyph on which to
13920 display the cursor. */
13921 if (dpos == 0)
13923 match_with_avoid_cursor = 0;
13924 break;
13926 /* See if we've found a better approximation to
13927 POS_BEFORE or to POS_AFTER. */
13928 if (0 > dpos && dpos > pos_before - pt_old)
13930 pos_before = glyph->charpos;
13931 glyph_before = glyph;
13933 else if (0 < dpos && dpos < pos_after - pt_old)
13935 pos_after = glyph->charpos;
13936 glyph_after = glyph;
13939 else if (dpos == 0)
13940 match_with_avoid_cursor = 1;
13942 else if (STRINGP (glyph->object))
13944 Lisp_Object chprop;
13945 ptrdiff_t glyph_pos = glyph->charpos;
13947 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13948 glyph->object);
13949 if (!NILP (chprop))
13951 /* If the string came from a `display' text property,
13952 look up the buffer position of that property and
13953 use that position to update bpos_max, as if we
13954 actually saw such a position in one of the row's
13955 glyphs. This helps with supporting integer values
13956 of `cursor' property on the display string in
13957 situations where most or all of the row's buffer
13958 text is completely covered by display properties,
13959 so that no glyph with valid buffer positions is
13960 ever seen in the row. */
13961 ptrdiff_t prop_pos =
13962 string_buffer_position_lim (glyph->object, pos_before,
13963 pos_after, 0);
13965 if (prop_pos >= pos_before)
13966 bpos_max = prop_pos - 1;
13968 if (INTEGERP (chprop))
13970 bpos_covered = bpos_max + XINT (chprop);
13971 /* If the `cursor' property covers buffer positions up
13972 to and including point, we should display cursor on
13973 this glyph. Note that, if a `cursor' property on one
13974 of the string's characters has an integer value, we
13975 will break out of the loop below _before_ we get to
13976 the position match above. IOW, integer values of
13977 the `cursor' property override the "exact match for
13978 point" strategy of positioning the cursor. */
13979 /* Implementation note: bpos_max == pt_old when, e.g.,
13980 we are in an empty line, where bpos_max is set to
13981 MATRIX_ROW_START_CHARPOS, see above. */
13982 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13984 cursor = glyph;
13985 break;
13989 string_seen = 1;
13991 x += glyph->pixel_width;
13992 ++glyph;
13994 else if (glyph > end) /* row is reversed */
13995 while (!INTEGERP (glyph->object))
13997 if (BUFFERP (glyph->object))
13999 ptrdiff_t dpos = glyph->charpos - pt_old;
14001 if (glyph->charpos > bpos_max)
14002 bpos_max = glyph->charpos;
14003 if (glyph->charpos < bpos_min)
14004 bpos_min = glyph->charpos;
14005 if (!glyph->avoid_cursor_p)
14007 if (dpos == 0)
14009 match_with_avoid_cursor = 0;
14010 break;
14012 if (0 > dpos && dpos > pos_before - pt_old)
14014 pos_before = glyph->charpos;
14015 glyph_before = glyph;
14017 else if (0 < dpos && dpos < pos_after - pt_old)
14019 pos_after = glyph->charpos;
14020 glyph_after = glyph;
14023 else if (dpos == 0)
14024 match_with_avoid_cursor = 1;
14026 else if (STRINGP (glyph->object))
14028 Lisp_Object chprop;
14029 ptrdiff_t glyph_pos = glyph->charpos;
14031 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14032 glyph->object);
14033 if (!NILP (chprop))
14035 ptrdiff_t prop_pos =
14036 string_buffer_position_lim (glyph->object, pos_before,
14037 pos_after, 0);
14039 if (prop_pos >= pos_before)
14040 bpos_max = prop_pos - 1;
14042 if (INTEGERP (chprop))
14044 bpos_covered = bpos_max + XINT (chprop);
14045 /* If the `cursor' property covers buffer positions up
14046 to and including point, we should display cursor on
14047 this glyph. */
14048 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14050 cursor = glyph;
14051 break;
14054 string_seen = 1;
14056 --glyph;
14057 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14059 x--; /* can't use any pixel_width */
14060 break;
14062 x -= glyph->pixel_width;
14065 /* Step 2: If we didn't find an exact match for point, we need to
14066 look for a proper place to put the cursor among glyphs between
14067 GLYPH_BEFORE and GLYPH_AFTER. */
14068 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14069 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14070 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14072 /* An empty line has a single glyph whose OBJECT is zero and
14073 whose CHARPOS is the position of a newline on that line.
14074 Note that on a TTY, there are more glyphs after that, which
14075 were produced by extend_face_to_end_of_line, but their
14076 CHARPOS is zero or negative. */
14077 int empty_line_p =
14078 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14079 && INTEGERP (glyph->object) && glyph->charpos > 0
14080 /* On a TTY, continued and truncated rows also have a glyph at
14081 their end whose OBJECT is zero and whose CHARPOS is
14082 positive (the continuation and truncation glyphs), but such
14083 rows are obviously not "empty". */
14084 && !(row->continued_p || row->truncated_on_right_p);
14086 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14088 ptrdiff_t ellipsis_pos;
14090 /* Scan back over the ellipsis glyphs. */
14091 if (!row->reversed_p)
14093 ellipsis_pos = (glyph - 1)->charpos;
14094 while (glyph > row->glyphs[TEXT_AREA]
14095 && (glyph - 1)->charpos == ellipsis_pos)
14096 glyph--, x -= glyph->pixel_width;
14097 /* That loop always goes one position too far, including
14098 the glyph before the ellipsis. So scan forward over
14099 that one. */
14100 x += glyph->pixel_width;
14101 glyph++;
14103 else /* row is reversed */
14105 ellipsis_pos = (glyph + 1)->charpos;
14106 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14107 && (glyph + 1)->charpos == ellipsis_pos)
14108 glyph++, x += glyph->pixel_width;
14109 x -= glyph->pixel_width;
14110 glyph--;
14113 else if (match_with_avoid_cursor)
14115 cursor = glyph_after;
14116 x = -1;
14118 else if (string_seen)
14120 int incr = row->reversed_p ? -1 : +1;
14122 /* Need to find the glyph that came out of a string which is
14123 present at point. That glyph is somewhere between
14124 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14125 positioned between POS_BEFORE and POS_AFTER in the
14126 buffer. */
14127 struct glyph *start, *stop;
14128 ptrdiff_t pos = pos_before;
14130 x = -1;
14132 /* If the row ends in a newline from a display string,
14133 reordering could have moved the glyphs belonging to the
14134 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14135 in this case we extend the search to the last glyph in
14136 the row that was not inserted by redisplay. */
14137 if (row->ends_in_newline_from_string_p)
14139 glyph_after = end;
14140 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14143 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14144 correspond to POS_BEFORE and POS_AFTER, respectively. We
14145 need START and STOP in the order that corresponds to the
14146 row's direction as given by its reversed_p flag. If the
14147 directionality of characters between POS_BEFORE and
14148 POS_AFTER is the opposite of the row's base direction,
14149 these characters will have been reordered for display,
14150 and we need to reverse START and STOP. */
14151 if (!row->reversed_p)
14153 start = min (glyph_before, glyph_after);
14154 stop = max (glyph_before, glyph_after);
14156 else
14158 start = max (glyph_before, glyph_after);
14159 stop = min (glyph_before, glyph_after);
14161 for (glyph = start + incr;
14162 row->reversed_p ? glyph > stop : glyph < stop; )
14165 /* Any glyphs that come from the buffer are here because
14166 of bidi reordering. Skip them, and only pay
14167 attention to glyphs that came from some string. */
14168 if (STRINGP (glyph->object))
14170 Lisp_Object str;
14171 ptrdiff_t tem;
14172 /* If the display property covers the newline, we
14173 need to search for it one position farther. */
14174 ptrdiff_t lim = pos_after
14175 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14177 string_from_text_prop = 0;
14178 str = glyph->object;
14179 tem = string_buffer_position_lim (str, pos, lim, 0);
14180 if (tem == 0 /* from overlay */
14181 || pos <= tem)
14183 /* If the string from which this glyph came is
14184 found in the buffer at point, or at position
14185 that is closer to point than pos_after, then
14186 we've found the glyph we've been looking for.
14187 If it comes from an overlay (tem == 0), and
14188 it has the `cursor' property on one of its
14189 glyphs, record that glyph as a candidate for
14190 displaying the cursor. (As in the
14191 unidirectional version, we will display the
14192 cursor on the last candidate we find.) */
14193 if (tem == 0
14194 || tem == pt_old
14195 || (tem - pt_old > 0 && tem < pos_after))
14197 /* The glyphs from this string could have
14198 been reordered. Find the one with the
14199 smallest string position. Or there could
14200 be a character in the string with the
14201 `cursor' property, which means display
14202 cursor on that character's glyph. */
14203 ptrdiff_t strpos = glyph->charpos;
14205 if (tem)
14207 cursor = glyph;
14208 string_from_text_prop = 1;
14210 for ( ;
14211 (row->reversed_p ? glyph > stop : glyph < stop)
14212 && EQ (glyph->object, str);
14213 glyph += incr)
14215 Lisp_Object cprop;
14216 ptrdiff_t gpos = glyph->charpos;
14218 cprop = Fget_char_property (make_number (gpos),
14219 Qcursor,
14220 glyph->object);
14221 if (!NILP (cprop))
14223 cursor = glyph;
14224 break;
14226 if (tem && glyph->charpos < strpos)
14228 strpos = glyph->charpos;
14229 cursor = glyph;
14233 if (tem == pt_old
14234 || (tem - pt_old > 0 && tem < pos_after))
14235 goto compute_x;
14237 if (tem)
14238 pos = tem + 1; /* don't find previous instances */
14240 /* This string is not what we want; skip all of the
14241 glyphs that came from it. */
14242 while ((row->reversed_p ? glyph > stop : glyph < stop)
14243 && EQ (glyph->object, str))
14244 glyph += incr;
14246 else
14247 glyph += incr;
14250 /* If we reached the end of the line, and END was from a string,
14251 the cursor is not on this line. */
14252 if (cursor == NULL
14253 && (row->reversed_p ? glyph <= end : glyph >= end)
14254 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14255 && STRINGP (end->object)
14256 && row->continued_p)
14257 return 0;
14259 /* A truncated row may not include PT among its character positions.
14260 Setting the cursor inside the scroll margin will trigger
14261 recalculation of hscroll in hscroll_window_tree. But if a
14262 display string covers point, defer to the string-handling
14263 code below to figure this out. */
14264 else if (row->truncated_on_left_p && pt_old < bpos_min)
14266 cursor = glyph_before;
14267 x = -1;
14269 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14270 /* Zero-width characters produce no glyphs. */
14271 || (!empty_line_p
14272 && (row->reversed_p
14273 ? glyph_after > glyphs_end
14274 : glyph_after < glyphs_end)))
14276 cursor = glyph_after;
14277 x = -1;
14281 compute_x:
14282 if (cursor != NULL)
14283 glyph = cursor;
14284 else if (glyph == glyphs_end
14285 && pos_before == pos_after
14286 && STRINGP ((row->reversed_p
14287 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14288 : row->glyphs[TEXT_AREA])->object))
14290 /* If all the glyphs of this row came from strings, put the
14291 cursor on the first glyph of the row. This avoids having the
14292 cursor outside of the text area in this very rare and hard
14293 use case. */
14294 glyph =
14295 row->reversed_p
14296 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14297 : row->glyphs[TEXT_AREA];
14299 if (x < 0)
14301 struct glyph *g;
14303 /* Need to compute x that corresponds to GLYPH. */
14304 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14306 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14307 emacs_abort ();
14308 x += g->pixel_width;
14312 /* ROW could be part of a continued line, which, under bidi
14313 reordering, might have other rows whose start and end charpos
14314 occlude point. Only set w->cursor if we found a better
14315 approximation to the cursor position than we have from previously
14316 examined candidate rows belonging to the same continued line. */
14317 if (/* we already have a candidate row */
14318 w->cursor.vpos >= 0
14319 /* that candidate is not the row we are processing */
14320 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14321 /* Make sure cursor.vpos specifies a row whose start and end
14322 charpos occlude point, and it is valid candidate for being a
14323 cursor-row. This is because some callers of this function
14324 leave cursor.vpos at the row where the cursor was displayed
14325 during the last redisplay cycle. */
14326 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14327 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14328 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14330 struct glyph *g1 =
14331 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14333 /* Don't consider glyphs that are outside TEXT_AREA. */
14334 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14335 return 0;
14336 /* Keep the candidate whose buffer position is the closest to
14337 point or has the `cursor' property. */
14338 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14339 w->cursor.hpos >= 0
14340 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14341 && ((BUFFERP (g1->object)
14342 && (g1->charpos == pt_old /* an exact match always wins */
14343 || (BUFFERP (glyph->object)
14344 && eabs (g1->charpos - pt_old)
14345 < eabs (glyph->charpos - pt_old))))
14346 /* previous candidate is a glyph from a string that has
14347 a non-nil `cursor' property */
14348 || (STRINGP (g1->object)
14349 && (!NILP (Fget_char_property (make_number (g1->charpos),
14350 Qcursor, g1->object))
14351 /* previous candidate is from the same display
14352 string as this one, and the display string
14353 came from a text property */
14354 || (EQ (g1->object, glyph->object)
14355 && string_from_text_prop)
14356 /* this candidate is from newline and its
14357 position is not an exact match */
14358 || (INTEGERP (glyph->object)
14359 && glyph->charpos != pt_old)))))
14360 return 0;
14361 /* If this candidate gives an exact match, use that. */
14362 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14363 /* If this candidate is a glyph created for the
14364 terminating newline of a line, and point is on that
14365 newline, it wins because it's an exact match. */
14366 || (!row->continued_p
14367 && INTEGERP (glyph->object)
14368 && glyph->charpos == 0
14369 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14370 /* Otherwise, keep the candidate that comes from a row
14371 spanning less buffer positions. This may win when one or
14372 both candidate positions are on glyphs that came from
14373 display strings, for which we cannot compare buffer
14374 positions. */
14375 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14376 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14377 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14378 return 0;
14380 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14381 w->cursor.x = x;
14382 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14383 w->cursor.y = row->y + dy;
14385 if (w == XWINDOW (selected_window))
14387 if (!row->continued_p
14388 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14389 && row->x == 0)
14391 this_line_buffer = XBUFFER (w->contents);
14393 CHARPOS (this_line_start_pos)
14394 = MATRIX_ROW_START_CHARPOS (row) + delta;
14395 BYTEPOS (this_line_start_pos)
14396 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14398 CHARPOS (this_line_end_pos)
14399 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14400 BYTEPOS (this_line_end_pos)
14401 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14403 this_line_y = w->cursor.y;
14404 this_line_pixel_height = row->height;
14405 this_line_vpos = w->cursor.vpos;
14406 this_line_start_x = row->x;
14408 else
14409 CHARPOS (this_line_start_pos) = 0;
14412 return 1;
14416 /* Run window scroll functions, if any, for WINDOW with new window
14417 start STARTP. Sets the window start of WINDOW to that position.
14419 We assume that the window's buffer is really current. */
14421 static struct text_pos
14422 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14424 struct window *w = XWINDOW (window);
14425 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14427 eassert (current_buffer == XBUFFER (w->contents));
14429 if (!NILP (Vwindow_scroll_functions))
14431 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14432 make_number (CHARPOS (startp)));
14433 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14434 /* In case the hook functions switch buffers. */
14435 set_buffer_internal (XBUFFER (w->contents));
14438 return startp;
14442 /* Make sure the line containing the cursor is fully visible.
14443 A value of 1 means there is nothing to be done.
14444 (Either the line is fully visible, or it cannot be made so,
14445 or we cannot tell.)
14447 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14448 is higher than window.
14450 A value of 0 means the caller should do scrolling
14451 as if point had gone off the screen. */
14453 static int
14454 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14456 struct glyph_matrix *matrix;
14457 struct glyph_row *row;
14458 int window_height;
14460 if (!make_cursor_line_fully_visible_p)
14461 return 1;
14463 /* It's not always possible to find the cursor, e.g, when a window
14464 is full of overlay strings. Don't do anything in that case. */
14465 if (w->cursor.vpos < 0)
14466 return 1;
14468 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14469 row = MATRIX_ROW (matrix, w->cursor.vpos);
14471 /* If the cursor row is not partially visible, there's nothing to do. */
14472 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14473 return 1;
14475 /* If the row the cursor is in is taller than the window's height,
14476 it's not clear what to do, so do nothing. */
14477 window_height = window_box_height (w);
14478 if (row->height >= window_height)
14480 if (!force_p || MINI_WINDOW_P (w)
14481 || w->vscroll || w->cursor.vpos == 0)
14482 return 1;
14484 return 0;
14488 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14489 non-zero means only WINDOW is redisplayed in redisplay_internal.
14490 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14491 in redisplay_window to bring a partially visible line into view in
14492 the case that only the cursor has moved.
14494 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14495 last screen line's vertical height extends past the end of the screen.
14497 Value is
14499 1 if scrolling succeeded
14501 0 if scrolling didn't find point.
14503 -1 if new fonts have been loaded so that we must interrupt
14504 redisplay, adjust glyph matrices, and try again. */
14506 enum
14508 SCROLLING_SUCCESS,
14509 SCROLLING_FAILED,
14510 SCROLLING_NEED_LARGER_MATRICES
14513 /* If scroll-conservatively is more than this, never recenter.
14515 If you change this, don't forget to update the doc string of
14516 `scroll-conservatively' and the Emacs manual. */
14517 #define SCROLL_LIMIT 100
14519 static int
14520 try_scrolling (Lisp_Object window, int just_this_one_p,
14521 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14522 int temp_scroll_step, int last_line_misfit)
14524 struct window *w = XWINDOW (window);
14525 struct frame *f = XFRAME (w->frame);
14526 struct text_pos pos, startp;
14527 struct it it;
14528 int this_scroll_margin, scroll_max, rc, height;
14529 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14530 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14531 Lisp_Object aggressive;
14532 /* We will never try scrolling more than this number of lines. */
14533 int scroll_limit = SCROLL_LIMIT;
14534 int frame_line_height = default_line_pixel_height (w);
14535 int window_total_lines
14536 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14538 #ifdef GLYPH_DEBUG
14539 debug_method_add (w, "try_scrolling");
14540 #endif
14542 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14544 /* Compute scroll margin height in pixels. We scroll when point is
14545 within this distance from the top or bottom of the window. */
14546 if (scroll_margin > 0)
14547 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14548 * frame_line_height;
14549 else
14550 this_scroll_margin = 0;
14552 /* Force arg_scroll_conservatively to have a reasonable value, to
14553 avoid scrolling too far away with slow move_it_* functions. Note
14554 that the user can supply scroll-conservatively equal to
14555 `most-positive-fixnum', which can be larger than INT_MAX. */
14556 if (arg_scroll_conservatively > scroll_limit)
14558 arg_scroll_conservatively = scroll_limit + 1;
14559 scroll_max = scroll_limit * frame_line_height;
14561 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14562 /* Compute how much we should try to scroll maximally to bring
14563 point into view. */
14564 scroll_max = (max (scroll_step,
14565 max (arg_scroll_conservatively, temp_scroll_step))
14566 * frame_line_height);
14567 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14568 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14569 /* We're trying to scroll because of aggressive scrolling but no
14570 scroll_step is set. Choose an arbitrary one. */
14571 scroll_max = 10 * frame_line_height;
14572 else
14573 scroll_max = 0;
14575 too_near_end:
14577 /* Decide whether to scroll down. */
14578 if (PT > CHARPOS (startp))
14580 int scroll_margin_y;
14582 /* Compute the pixel ypos of the scroll margin, then move IT to
14583 either that ypos or PT, whichever comes first. */
14584 start_display (&it, w, startp);
14585 scroll_margin_y = it.last_visible_y - this_scroll_margin
14586 - frame_line_height * extra_scroll_margin_lines;
14587 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14588 (MOVE_TO_POS | MOVE_TO_Y));
14590 if (PT > CHARPOS (it.current.pos))
14592 int y0 = line_bottom_y (&it);
14593 /* Compute how many pixels below window bottom to stop searching
14594 for PT. This avoids costly search for PT that is far away if
14595 the user limited scrolling by a small number of lines, but
14596 always finds PT if scroll_conservatively is set to a large
14597 number, such as most-positive-fixnum. */
14598 int slack = max (scroll_max, 10 * frame_line_height);
14599 int y_to_move = it.last_visible_y + slack;
14601 /* Compute the distance from the scroll margin to PT or to
14602 the scroll limit, whichever comes first. This should
14603 include the height of the cursor line, to make that line
14604 fully visible. */
14605 move_it_to (&it, PT, -1, y_to_move,
14606 -1, MOVE_TO_POS | MOVE_TO_Y);
14607 dy = line_bottom_y (&it) - y0;
14609 if (dy > scroll_max)
14610 return SCROLLING_FAILED;
14612 if (dy > 0)
14613 scroll_down_p = 1;
14617 if (scroll_down_p)
14619 /* Point is in or below the bottom scroll margin, so move the
14620 window start down. If scrolling conservatively, move it just
14621 enough down to make point visible. If scroll_step is set,
14622 move it down by scroll_step. */
14623 if (arg_scroll_conservatively)
14624 amount_to_scroll
14625 = min (max (dy, frame_line_height),
14626 frame_line_height * arg_scroll_conservatively);
14627 else if (scroll_step || temp_scroll_step)
14628 amount_to_scroll = scroll_max;
14629 else
14631 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14632 height = WINDOW_BOX_TEXT_HEIGHT (w);
14633 if (NUMBERP (aggressive))
14635 double float_amount = XFLOATINT (aggressive) * height;
14636 int aggressive_scroll = float_amount;
14637 if (aggressive_scroll == 0 && float_amount > 0)
14638 aggressive_scroll = 1;
14639 /* Don't let point enter the scroll margin near top of
14640 the window. This could happen if the value of
14641 scroll_up_aggressively is too large and there are
14642 non-zero margins, because scroll_up_aggressively
14643 means put point that fraction of window height
14644 _from_the_bottom_margin_. */
14645 if (aggressive_scroll + 2*this_scroll_margin > height)
14646 aggressive_scroll = height - 2*this_scroll_margin;
14647 amount_to_scroll = dy + aggressive_scroll;
14651 if (amount_to_scroll <= 0)
14652 return SCROLLING_FAILED;
14654 start_display (&it, w, startp);
14655 if (arg_scroll_conservatively <= scroll_limit)
14656 move_it_vertically (&it, amount_to_scroll);
14657 else
14659 /* Extra precision for users who set scroll-conservatively
14660 to a large number: make sure the amount we scroll
14661 the window start is never less than amount_to_scroll,
14662 which was computed as distance from window bottom to
14663 point. This matters when lines at window top and lines
14664 below window bottom have different height. */
14665 struct it it1;
14666 void *it1data = NULL;
14667 /* We use a temporary it1 because line_bottom_y can modify
14668 its argument, if it moves one line down; see there. */
14669 int start_y;
14671 SAVE_IT (it1, it, it1data);
14672 start_y = line_bottom_y (&it1);
14673 do {
14674 RESTORE_IT (&it, &it, it1data);
14675 move_it_by_lines (&it, 1);
14676 SAVE_IT (it1, it, it1data);
14677 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14680 /* If STARTP is unchanged, move it down another screen line. */
14681 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14682 move_it_by_lines (&it, 1);
14683 startp = it.current.pos;
14685 else
14687 struct text_pos scroll_margin_pos = startp;
14688 int y_offset = 0;
14690 /* See if point is inside the scroll margin at the top of the
14691 window. */
14692 if (this_scroll_margin)
14694 int y_start;
14696 start_display (&it, w, startp);
14697 y_start = it.current_y;
14698 move_it_vertically (&it, this_scroll_margin);
14699 scroll_margin_pos = it.current.pos;
14700 /* If we didn't move enough before hitting ZV, request
14701 additional amount of scroll, to move point out of the
14702 scroll margin. */
14703 if (IT_CHARPOS (it) == ZV
14704 && it.current_y - y_start < this_scroll_margin)
14705 y_offset = this_scroll_margin - (it.current_y - y_start);
14708 if (PT < CHARPOS (scroll_margin_pos))
14710 /* Point is in the scroll margin at the top of the window or
14711 above what is displayed in the window. */
14712 int y0, y_to_move;
14714 /* Compute the vertical distance from PT to the scroll
14715 margin position. Move as far as scroll_max allows, or
14716 one screenful, or 10 screen lines, whichever is largest.
14717 Give up if distance is greater than scroll_max or if we
14718 didn't reach the scroll margin position. */
14719 SET_TEXT_POS (pos, PT, PT_BYTE);
14720 start_display (&it, w, pos);
14721 y0 = it.current_y;
14722 y_to_move = max (it.last_visible_y,
14723 max (scroll_max, 10 * frame_line_height));
14724 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14725 y_to_move, -1,
14726 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14727 dy = it.current_y - y0;
14728 if (dy > scroll_max
14729 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14730 return SCROLLING_FAILED;
14732 /* Additional scroll for when ZV was too close to point. */
14733 dy += y_offset;
14735 /* Compute new window start. */
14736 start_display (&it, w, startp);
14738 if (arg_scroll_conservatively)
14739 amount_to_scroll = max (dy, frame_line_height *
14740 max (scroll_step, temp_scroll_step));
14741 else if (scroll_step || temp_scroll_step)
14742 amount_to_scroll = scroll_max;
14743 else
14745 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14746 height = WINDOW_BOX_TEXT_HEIGHT (w);
14747 if (NUMBERP (aggressive))
14749 double float_amount = XFLOATINT (aggressive) * height;
14750 int aggressive_scroll = float_amount;
14751 if (aggressive_scroll == 0 && float_amount > 0)
14752 aggressive_scroll = 1;
14753 /* Don't let point enter the scroll margin near
14754 bottom of the window, if the value of
14755 scroll_down_aggressively happens to be too
14756 large. */
14757 if (aggressive_scroll + 2*this_scroll_margin > height)
14758 aggressive_scroll = height - 2*this_scroll_margin;
14759 amount_to_scroll = dy + aggressive_scroll;
14763 if (amount_to_scroll <= 0)
14764 return SCROLLING_FAILED;
14766 move_it_vertically_backward (&it, amount_to_scroll);
14767 startp = it.current.pos;
14771 /* Run window scroll functions. */
14772 startp = run_window_scroll_functions (window, startp);
14774 /* Display the window. Give up if new fonts are loaded, or if point
14775 doesn't appear. */
14776 if (!try_window (window, startp, 0))
14777 rc = SCROLLING_NEED_LARGER_MATRICES;
14778 else if (w->cursor.vpos < 0)
14780 clear_glyph_matrix (w->desired_matrix);
14781 rc = SCROLLING_FAILED;
14783 else
14785 /* Maybe forget recorded base line for line number display. */
14786 if (!just_this_one_p
14787 || current_buffer->clip_changed
14788 || BEG_UNCHANGED < CHARPOS (startp))
14789 w->base_line_number = 0;
14791 /* If cursor ends up on a partially visible line,
14792 treat that as being off the bottom of the screen. */
14793 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14794 /* It's possible that the cursor is on the first line of the
14795 buffer, which is partially obscured due to a vscroll
14796 (Bug#7537). In that case, avoid looping forever . */
14797 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14799 clear_glyph_matrix (w->desired_matrix);
14800 ++extra_scroll_margin_lines;
14801 goto too_near_end;
14803 rc = SCROLLING_SUCCESS;
14806 return rc;
14810 /* Compute a suitable window start for window W if display of W starts
14811 on a continuation line. Value is non-zero if a new window start
14812 was computed.
14814 The new window start will be computed, based on W's width, starting
14815 from the start of the continued line. It is the start of the
14816 screen line with the minimum distance from the old start W->start. */
14818 static int
14819 compute_window_start_on_continuation_line (struct window *w)
14821 struct text_pos pos, start_pos;
14822 int window_start_changed_p = 0;
14824 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14826 /* If window start is on a continuation line... Window start may be
14827 < BEGV in case there's invisible text at the start of the
14828 buffer (M-x rmail, for example). */
14829 if (CHARPOS (start_pos) > BEGV
14830 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14832 struct it it;
14833 struct glyph_row *row;
14835 /* Handle the case that the window start is out of range. */
14836 if (CHARPOS (start_pos) < BEGV)
14837 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14838 else if (CHARPOS (start_pos) > ZV)
14839 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14841 /* Find the start of the continued line. This should be fast
14842 because find_newline is fast (newline cache). */
14843 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14844 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14845 row, DEFAULT_FACE_ID);
14846 reseat_at_previous_visible_line_start (&it);
14848 /* If the line start is "too far" away from the window start,
14849 say it takes too much time to compute a new window start. */
14850 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14851 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14853 int min_distance, distance;
14855 /* Move forward by display lines to find the new window
14856 start. If window width was enlarged, the new start can
14857 be expected to be > the old start. If window width was
14858 decreased, the new window start will be < the old start.
14859 So, we're looking for the display line start with the
14860 minimum distance from the old window start. */
14861 pos = it.current.pos;
14862 min_distance = INFINITY;
14863 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14864 distance < min_distance)
14866 min_distance = distance;
14867 pos = it.current.pos;
14868 if (it.line_wrap == WORD_WRAP)
14870 /* Under WORD_WRAP, move_it_by_lines is likely to
14871 overshoot and stop not at the first, but the
14872 second character from the left margin. So in
14873 that case, we need a more tight control on the X
14874 coordinate of the iterator than move_it_by_lines
14875 promises in its contract. The method is to first
14876 go to the last (rightmost) visible character of a
14877 line, then move to the leftmost character on the
14878 next line in a separate call. */
14879 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14880 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14881 move_it_to (&it, ZV, 0,
14882 it.current_y + it.max_ascent + it.max_descent, -1,
14883 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14885 else
14886 move_it_by_lines (&it, 1);
14889 /* Set the window start there. */
14890 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14891 window_start_changed_p = 1;
14895 return window_start_changed_p;
14899 /* Try cursor movement in case text has not changed in window WINDOW,
14900 with window start STARTP. Value is
14902 CURSOR_MOVEMENT_SUCCESS if successful
14904 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14906 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14907 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14908 we want to scroll as if scroll-step were set to 1. See the code.
14910 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14911 which case we have to abort this redisplay, and adjust matrices
14912 first. */
14914 enum
14916 CURSOR_MOVEMENT_SUCCESS,
14917 CURSOR_MOVEMENT_CANNOT_BE_USED,
14918 CURSOR_MOVEMENT_MUST_SCROLL,
14919 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14922 static int
14923 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14925 struct window *w = XWINDOW (window);
14926 struct frame *f = XFRAME (w->frame);
14927 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14929 #ifdef GLYPH_DEBUG
14930 if (inhibit_try_cursor_movement)
14931 return rc;
14932 #endif
14934 /* Previously, there was a check for Lisp integer in the
14935 if-statement below. Now, this field is converted to
14936 ptrdiff_t, thus zero means invalid position in a buffer. */
14937 eassert (w->last_point > 0);
14938 /* Likewise there was a check whether window_end_vpos is nil or larger
14939 than the window. Now window_end_vpos is int and so never nil, but
14940 let's leave eassert to check whether it fits in the window. */
14941 eassert (w->window_end_vpos < w->current_matrix->nrows);
14943 /* Handle case where text has not changed, only point, and it has
14944 not moved off the frame. */
14945 if (/* Point may be in this window. */
14946 PT >= CHARPOS (startp)
14947 /* Selective display hasn't changed. */
14948 && !current_buffer->clip_changed
14949 /* Function force-mode-line-update is used to force a thorough
14950 redisplay. It sets either windows_or_buffers_changed or
14951 update_mode_lines. So don't take a shortcut here for these
14952 cases. */
14953 && !update_mode_lines
14954 && !windows_or_buffers_changed
14955 && !f->cursor_type_changed
14956 && NILP (Vshow_trailing_whitespace)
14957 /* This code is not used for mini-buffer for the sake of the case
14958 of redisplaying to replace an echo area message; since in
14959 that case the mini-buffer contents per se are usually
14960 unchanged. This code is of no real use in the mini-buffer
14961 since the handling of this_line_start_pos, etc., in redisplay
14962 handles the same cases. */
14963 && !EQ (window, minibuf_window)
14964 && (FRAME_WINDOW_P (f)
14965 || !overlay_arrow_in_current_buffer_p ()))
14967 int this_scroll_margin, top_scroll_margin;
14968 struct glyph_row *row = NULL;
14969 int frame_line_height = default_line_pixel_height (w);
14970 int window_total_lines
14971 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14973 #ifdef GLYPH_DEBUG
14974 debug_method_add (w, "cursor movement");
14975 #endif
14977 /* Scroll if point within this distance from the top or bottom
14978 of the window. This is a pixel value. */
14979 if (scroll_margin > 0)
14981 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
14982 this_scroll_margin *= frame_line_height;
14984 else
14985 this_scroll_margin = 0;
14987 top_scroll_margin = this_scroll_margin;
14988 if (WINDOW_WANTS_HEADER_LINE_P (w))
14989 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14991 /* Start with the row the cursor was displayed during the last
14992 not paused redisplay. Give up if that row is not valid. */
14993 if (w->last_cursor_vpos < 0
14994 || w->last_cursor_vpos >= w->current_matrix->nrows)
14995 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14996 else
14998 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
14999 if (row->mode_line_p)
15000 ++row;
15001 if (!row->enabled_p)
15002 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15005 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15007 int scroll_p = 0, must_scroll = 0;
15008 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15010 if (PT > w->last_point)
15012 /* Point has moved forward. */
15013 while (MATRIX_ROW_END_CHARPOS (row) < PT
15014 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15016 eassert (row->enabled_p);
15017 ++row;
15020 /* If the end position of a row equals the start
15021 position of the next row, and PT is at that position,
15022 we would rather display cursor in the next line. */
15023 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15024 && MATRIX_ROW_END_CHARPOS (row) == PT
15025 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15026 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15027 && !cursor_row_p (row))
15028 ++row;
15030 /* If within the scroll margin, scroll. Note that
15031 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15032 the next line would be drawn, and that
15033 this_scroll_margin can be zero. */
15034 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15035 || PT > MATRIX_ROW_END_CHARPOS (row)
15036 /* Line is completely visible last line in window
15037 and PT is to be set in the next line. */
15038 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15039 && PT == MATRIX_ROW_END_CHARPOS (row)
15040 && !row->ends_at_zv_p
15041 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15042 scroll_p = 1;
15044 else if (PT < w->last_point)
15046 /* Cursor has to be moved backward. Note that PT >=
15047 CHARPOS (startp) because of the outer if-statement. */
15048 while (!row->mode_line_p
15049 && (MATRIX_ROW_START_CHARPOS (row) > PT
15050 || (MATRIX_ROW_START_CHARPOS (row) == PT
15051 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15052 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15053 row > w->current_matrix->rows
15054 && (row-1)->ends_in_newline_from_string_p))))
15055 && (row->y > top_scroll_margin
15056 || CHARPOS (startp) == BEGV))
15058 eassert (row->enabled_p);
15059 --row;
15062 /* Consider the following case: Window starts at BEGV,
15063 there is invisible, intangible text at BEGV, so that
15064 display starts at some point START > BEGV. It can
15065 happen that we are called with PT somewhere between
15066 BEGV and START. Try to handle that case. */
15067 if (row < w->current_matrix->rows
15068 || row->mode_line_p)
15070 row = w->current_matrix->rows;
15071 if (row->mode_line_p)
15072 ++row;
15075 /* Due to newlines in overlay strings, we may have to
15076 skip forward over overlay strings. */
15077 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15078 && MATRIX_ROW_END_CHARPOS (row) == PT
15079 && !cursor_row_p (row))
15080 ++row;
15082 /* If within the scroll margin, scroll. */
15083 if (row->y < top_scroll_margin
15084 && CHARPOS (startp) != BEGV)
15085 scroll_p = 1;
15087 else
15089 /* Cursor did not move. So don't scroll even if cursor line
15090 is partially visible, as it was so before. */
15091 rc = CURSOR_MOVEMENT_SUCCESS;
15094 if (PT < MATRIX_ROW_START_CHARPOS (row)
15095 || PT > MATRIX_ROW_END_CHARPOS (row))
15097 /* if PT is not in the glyph row, give up. */
15098 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15099 must_scroll = 1;
15101 else if (rc != CURSOR_MOVEMENT_SUCCESS
15102 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15104 struct glyph_row *row1;
15106 /* If rows are bidi-reordered and point moved, back up
15107 until we find a row that does not belong to a
15108 continuation line. This is because we must consider
15109 all rows of a continued line as candidates for the
15110 new cursor positioning, since row start and end
15111 positions change non-linearly with vertical position
15112 in such rows. */
15113 /* FIXME: Revisit this when glyph ``spilling'' in
15114 continuation lines' rows is implemented for
15115 bidi-reordered rows. */
15116 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15117 MATRIX_ROW_CONTINUATION_LINE_P (row);
15118 --row)
15120 /* If we hit the beginning of the displayed portion
15121 without finding the first row of a continued
15122 line, give up. */
15123 if (row <= row1)
15125 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15126 break;
15128 eassert (row->enabled_p);
15131 if (must_scroll)
15133 else if (rc != CURSOR_MOVEMENT_SUCCESS
15134 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15135 /* Make sure this isn't a header line by any chance, since
15136 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15137 && !row->mode_line_p
15138 && make_cursor_line_fully_visible_p)
15140 if (PT == MATRIX_ROW_END_CHARPOS (row)
15141 && !row->ends_at_zv_p
15142 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15143 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15144 else if (row->height > window_box_height (w))
15146 /* If we end up in a partially visible line, let's
15147 make it fully visible, except when it's taller
15148 than the window, in which case we can't do much
15149 about it. */
15150 *scroll_step = 1;
15151 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15153 else
15155 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15156 if (!cursor_row_fully_visible_p (w, 0, 1))
15157 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15158 else
15159 rc = CURSOR_MOVEMENT_SUCCESS;
15162 else if (scroll_p)
15163 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15164 else if (rc != CURSOR_MOVEMENT_SUCCESS
15165 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15167 /* With bidi-reordered rows, there could be more than
15168 one candidate row whose start and end positions
15169 occlude point. We need to let set_cursor_from_row
15170 find the best candidate. */
15171 /* FIXME: Revisit this when glyph ``spilling'' in
15172 continuation lines' rows is implemented for
15173 bidi-reordered rows. */
15174 int rv = 0;
15178 int at_zv_p = 0, exact_match_p = 0;
15180 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15181 && PT <= MATRIX_ROW_END_CHARPOS (row)
15182 && cursor_row_p (row))
15183 rv |= set_cursor_from_row (w, row, w->current_matrix,
15184 0, 0, 0, 0);
15185 /* As soon as we've found the exact match for point,
15186 or the first suitable row whose ends_at_zv_p flag
15187 is set, we are done. */
15188 at_zv_p =
15189 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15190 if (rv && !at_zv_p
15191 && w->cursor.hpos >= 0
15192 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15193 w->cursor.vpos))
15195 struct glyph_row *candidate =
15196 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15197 struct glyph *g =
15198 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15199 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15201 exact_match_p =
15202 (BUFFERP (g->object) && g->charpos == PT)
15203 || (INTEGERP (g->object)
15204 && (g->charpos == PT
15205 || (g->charpos == 0 && endpos - 1 == PT)));
15207 if (rv && (at_zv_p || exact_match_p))
15209 rc = CURSOR_MOVEMENT_SUCCESS;
15210 break;
15212 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15213 break;
15214 ++row;
15216 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15217 || row->continued_p)
15218 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15219 || (MATRIX_ROW_START_CHARPOS (row) == PT
15220 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15221 /* If we didn't find any candidate rows, or exited the
15222 loop before all the candidates were examined, signal
15223 to the caller that this method failed. */
15224 if (rc != CURSOR_MOVEMENT_SUCCESS
15225 && !(rv
15226 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15227 && !row->continued_p))
15228 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15229 else if (rv)
15230 rc = CURSOR_MOVEMENT_SUCCESS;
15232 else
15236 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15238 rc = CURSOR_MOVEMENT_SUCCESS;
15239 break;
15241 ++row;
15243 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15244 && MATRIX_ROW_START_CHARPOS (row) == PT
15245 && cursor_row_p (row));
15250 return rc;
15253 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15254 static
15255 #endif
15256 void
15257 set_vertical_scroll_bar (struct window *w)
15259 ptrdiff_t start, end, whole;
15261 /* Calculate the start and end positions for the current window.
15262 At some point, it would be nice to choose between scrollbars
15263 which reflect the whole buffer size, with special markers
15264 indicating narrowing, and scrollbars which reflect only the
15265 visible region.
15267 Note that mini-buffers sometimes aren't displaying any text. */
15268 if (!MINI_WINDOW_P (w)
15269 || (w == XWINDOW (minibuf_window)
15270 && NILP (echo_area_buffer[0])))
15272 struct buffer *buf = XBUFFER (w->contents);
15273 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15274 start = marker_position (w->start) - BUF_BEGV (buf);
15275 /* I don't think this is guaranteed to be right. For the
15276 moment, we'll pretend it is. */
15277 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15279 if (end < start)
15280 end = start;
15281 if (whole < (end - start))
15282 whole = end - start;
15284 else
15285 start = end = whole = 0;
15287 /* Indicate what this scroll bar ought to be displaying now. */
15288 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15289 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15290 (w, end - start, whole, start);
15294 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15295 selected_window is redisplayed.
15297 We can return without actually redisplaying the window if fonts has been
15298 changed on window's frame. In that case, redisplay_internal will retry. */
15300 static void
15301 redisplay_window (Lisp_Object window, int just_this_one_p)
15303 struct window *w = XWINDOW (window);
15304 struct frame *f = XFRAME (w->frame);
15305 struct buffer *buffer = XBUFFER (w->contents);
15306 struct buffer *old = current_buffer;
15307 struct text_pos lpoint, opoint, startp;
15308 int update_mode_line;
15309 int tem;
15310 struct it it;
15311 /* Record it now because it's overwritten. */
15312 int current_matrix_up_to_date_p = 0;
15313 int used_current_matrix_p = 0;
15314 /* This is less strict than current_matrix_up_to_date_p.
15315 It indicates that the buffer contents and narrowing are unchanged. */
15316 int buffer_unchanged_p = 0;
15317 int temp_scroll_step = 0;
15318 ptrdiff_t count = SPECPDL_INDEX ();
15319 int rc;
15320 int centering_position = -1;
15321 int last_line_misfit = 0;
15322 ptrdiff_t beg_unchanged, end_unchanged;
15323 int frame_line_height;
15325 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15326 opoint = lpoint;
15328 #ifdef GLYPH_DEBUG
15329 *w->desired_matrix->method = 0;
15330 #endif
15332 /* Make sure that both W's markers are valid. */
15333 eassert (XMARKER (w->start)->buffer == buffer);
15334 eassert (XMARKER (w->pointm)->buffer == buffer);
15336 restart:
15337 reconsider_clip_changes (w);
15338 frame_line_height = default_line_pixel_height (w);
15340 /* Has the mode line to be updated? */
15341 update_mode_line = (w->update_mode_line
15342 || update_mode_lines
15343 || buffer->clip_changed
15344 || buffer->prevent_redisplay_optimizations_p);
15346 if (MINI_WINDOW_P (w))
15348 if (w == XWINDOW (echo_area_window)
15349 && !NILP (echo_area_buffer[0]))
15351 if (update_mode_line)
15352 /* We may have to update a tty frame's menu bar or a
15353 tool-bar. Example `M-x C-h C-h C-g'. */
15354 goto finish_menu_bars;
15355 else
15356 /* We've already displayed the echo area glyphs in this window. */
15357 goto finish_scroll_bars;
15359 else if ((w != XWINDOW (minibuf_window)
15360 || minibuf_level == 0)
15361 /* When buffer is nonempty, redisplay window normally. */
15362 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15363 /* Quail displays non-mini buffers in minibuffer window.
15364 In that case, redisplay the window normally. */
15365 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15367 /* W is a mini-buffer window, but it's not active, so clear
15368 it. */
15369 int yb = window_text_bottom_y (w);
15370 struct glyph_row *row;
15371 int y;
15373 for (y = 0, row = w->desired_matrix->rows;
15374 y < yb;
15375 y += row->height, ++row)
15376 blank_row (w, row, y);
15377 goto finish_scroll_bars;
15380 clear_glyph_matrix (w->desired_matrix);
15383 /* Otherwise set up data on this window; select its buffer and point
15384 value. */
15385 /* Really select the buffer, for the sake of buffer-local
15386 variables. */
15387 set_buffer_internal_1 (XBUFFER (w->contents));
15389 current_matrix_up_to_date_p
15390 = (w->window_end_valid
15391 && !current_buffer->clip_changed
15392 && !current_buffer->prevent_redisplay_optimizations_p
15393 && !window_outdated (w));
15395 /* Run the window-bottom-change-functions
15396 if it is possible that the text on the screen has changed
15397 (either due to modification of the text, or any other reason). */
15398 if (!current_matrix_up_to_date_p
15399 && !NILP (Vwindow_text_change_functions))
15401 safe_run_hooks (Qwindow_text_change_functions);
15402 goto restart;
15405 beg_unchanged = BEG_UNCHANGED;
15406 end_unchanged = END_UNCHANGED;
15408 SET_TEXT_POS (opoint, PT, PT_BYTE);
15410 specbind (Qinhibit_point_motion_hooks, Qt);
15412 buffer_unchanged_p
15413 = (w->window_end_valid
15414 && !current_buffer->clip_changed
15415 && !window_outdated (w));
15417 /* When windows_or_buffers_changed is non-zero, we can't rely
15418 on the window end being valid, so set it to zero there. */
15419 if (windows_or_buffers_changed)
15421 /* If window starts on a continuation line, maybe adjust the
15422 window start in case the window's width changed. */
15423 if (XMARKER (w->start)->buffer == current_buffer)
15424 compute_window_start_on_continuation_line (w);
15426 w->window_end_valid = 0;
15427 /* If so, we also can't rely on current matrix
15428 and should not fool try_cursor_movement below. */
15429 current_matrix_up_to_date_p = 0;
15432 /* Some sanity checks. */
15433 CHECK_WINDOW_END (w);
15434 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15435 emacs_abort ();
15436 if (BYTEPOS (opoint) < CHARPOS (opoint))
15437 emacs_abort ();
15439 if (mode_line_update_needed (w))
15440 update_mode_line = 1;
15442 /* Point refers normally to the selected window. For any other
15443 window, set up appropriate value. */
15444 if (!EQ (window, selected_window))
15446 ptrdiff_t new_pt = marker_position (w->pointm);
15447 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15448 if (new_pt < BEGV)
15450 new_pt = BEGV;
15451 new_pt_byte = BEGV_BYTE;
15452 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15454 else if (new_pt > (ZV - 1))
15456 new_pt = ZV;
15457 new_pt_byte = ZV_BYTE;
15458 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15461 /* We don't use SET_PT so that the point-motion hooks don't run. */
15462 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15465 /* If any of the character widths specified in the display table
15466 have changed, invalidate the width run cache. It's true that
15467 this may be a bit late to catch such changes, but the rest of
15468 redisplay goes (non-fatally) haywire when the display table is
15469 changed, so why should we worry about doing any better? */
15470 if (current_buffer->width_run_cache)
15472 struct Lisp_Char_Table *disptab = buffer_display_table ();
15474 if (! disptab_matches_widthtab
15475 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15477 invalidate_region_cache (current_buffer,
15478 current_buffer->width_run_cache,
15479 BEG, Z);
15480 recompute_width_table (current_buffer, disptab);
15484 /* If window-start is screwed up, choose a new one. */
15485 if (XMARKER (w->start)->buffer != current_buffer)
15486 goto recenter;
15488 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15490 /* If someone specified a new starting point but did not insist,
15491 check whether it can be used. */
15492 if (w->optional_new_start
15493 && CHARPOS (startp) >= BEGV
15494 && CHARPOS (startp) <= ZV)
15496 w->optional_new_start = 0;
15497 start_display (&it, w, startp);
15498 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15499 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15500 if (IT_CHARPOS (it) == PT)
15501 w->force_start = 1;
15502 /* IT may overshoot PT if text at PT is invisible. */
15503 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15504 w->force_start = 1;
15507 force_start:
15509 /* Handle case where place to start displaying has been specified,
15510 unless the specified location is outside the accessible range. */
15511 if (w->force_start || window_frozen_p (w))
15513 /* We set this later on if we have to adjust point. */
15514 int new_vpos = -1;
15516 w->force_start = 0;
15517 w->vscroll = 0;
15518 w->window_end_valid = 0;
15520 /* Forget any recorded base line for line number display. */
15521 if (!buffer_unchanged_p)
15522 w->base_line_number = 0;
15524 /* Redisplay the mode line. Select the buffer properly for that.
15525 Also, run the hook window-scroll-functions
15526 because we have scrolled. */
15527 /* Note, we do this after clearing force_start because
15528 if there's an error, it is better to forget about force_start
15529 than to get into an infinite loop calling the hook functions
15530 and having them get more errors. */
15531 if (!update_mode_line
15532 || ! NILP (Vwindow_scroll_functions))
15534 update_mode_line = 1;
15535 w->update_mode_line = 1;
15536 startp = run_window_scroll_functions (window, startp);
15539 if (CHARPOS (startp) < BEGV)
15540 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15541 else if (CHARPOS (startp) > ZV)
15542 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15544 /* Redisplay, then check if cursor has been set during the
15545 redisplay. Give up if new fonts were loaded. */
15546 /* We used to issue a CHECK_MARGINS argument to try_window here,
15547 but this causes scrolling to fail when point begins inside
15548 the scroll margin (bug#148) -- cyd */
15549 if (!try_window (window, startp, 0))
15551 w->force_start = 1;
15552 clear_glyph_matrix (w->desired_matrix);
15553 goto need_larger_matrices;
15556 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15558 /* If point does not appear, try to move point so it does
15559 appear. The desired matrix has been built above, so we
15560 can use it here. */
15561 new_vpos = window_box_height (w) / 2;
15564 if (!cursor_row_fully_visible_p (w, 0, 0))
15566 /* Point does appear, but on a line partly visible at end of window.
15567 Move it back to a fully-visible line. */
15568 new_vpos = window_box_height (w);
15570 else if (w->cursor.vpos >= 0)
15572 /* Some people insist on not letting point enter the scroll
15573 margin, even though this part handles windows that didn't
15574 scroll at all. */
15575 int window_total_lines
15576 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15577 int margin = min (scroll_margin, window_total_lines / 4);
15578 int pixel_margin = margin * frame_line_height;
15579 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15581 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15582 below, which finds the row to move point to, advances by
15583 the Y coordinate of the _next_ row, see the definition of
15584 MATRIX_ROW_BOTTOM_Y. */
15585 if (w->cursor.vpos < margin + header_line)
15587 w->cursor.vpos = -1;
15588 clear_glyph_matrix (w->desired_matrix);
15589 goto try_to_scroll;
15591 else
15593 int window_height = window_box_height (w);
15595 if (header_line)
15596 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15597 if (w->cursor.y >= window_height - pixel_margin)
15599 w->cursor.vpos = -1;
15600 clear_glyph_matrix (w->desired_matrix);
15601 goto try_to_scroll;
15606 /* If we need to move point for either of the above reasons,
15607 now actually do it. */
15608 if (new_vpos >= 0)
15610 struct glyph_row *row;
15612 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15613 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15614 ++row;
15616 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15617 MATRIX_ROW_START_BYTEPOS (row));
15619 if (w != XWINDOW (selected_window))
15620 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15621 else if (current_buffer == old)
15622 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15624 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15626 /* If we are highlighting the region, then we just changed
15627 the region, so redisplay to show it. */
15628 /* FIXME: We need to (re)run pre-redisplay-function! */
15629 /* if (markpos_of_region () >= 0)
15631 clear_glyph_matrix (w->desired_matrix);
15632 if (!try_window (window, startp, 0))
15633 goto need_larger_matrices;
15638 #ifdef GLYPH_DEBUG
15639 debug_method_add (w, "forced window start");
15640 #endif
15641 goto done;
15644 /* Handle case where text has not changed, only point, and it has
15645 not moved off the frame, and we are not retrying after hscroll.
15646 (current_matrix_up_to_date_p is nonzero when retrying.) */
15647 if (current_matrix_up_to_date_p
15648 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15649 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15651 switch (rc)
15653 case CURSOR_MOVEMENT_SUCCESS:
15654 used_current_matrix_p = 1;
15655 goto done;
15657 case CURSOR_MOVEMENT_MUST_SCROLL:
15658 goto try_to_scroll;
15660 default:
15661 emacs_abort ();
15664 /* If current starting point was originally the beginning of a line
15665 but no longer is, find a new starting point. */
15666 else if (w->start_at_line_beg
15667 && !(CHARPOS (startp) <= BEGV
15668 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15670 #ifdef GLYPH_DEBUG
15671 debug_method_add (w, "recenter 1");
15672 #endif
15673 goto recenter;
15676 /* Try scrolling with try_window_id. Value is > 0 if update has
15677 been done, it is -1 if we know that the same window start will
15678 not work. It is 0 if unsuccessful for some other reason. */
15679 else if ((tem = try_window_id (w)) != 0)
15681 #ifdef GLYPH_DEBUG
15682 debug_method_add (w, "try_window_id %d", tem);
15683 #endif
15685 if (f->fonts_changed)
15686 goto need_larger_matrices;
15687 if (tem > 0)
15688 goto done;
15690 /* Otherwise try_window_id has returned -1 which means that we
15691 don't want the alternative below this comment to execute. */
15693 else if (CHARPOS (startp) >= BEGV
15694 && CHARPOS (startp) <= ZV
15695 && PT >= CHARPOS (startp)
15696 && (CHARPOS (startp) < ZV
15697 /* Avoid starting at end of buffer. */
15698 || CHARPOS (startp) == BEGV
15699 || !window_outdated (w)))
15701 int d1, d2, d3, d4, d5, d6;
15703 /* If first window line is a continuation line, and window start
15704 is inside the modified region, but the first change is before
15705 current window start, we must select a new window start.
15707 However, if this is the result of a down-mouse event (e.g. by
15708 extending the mouse-drag-overlay), we don't want to select a
15709 new window start, since that would change the position under
15710 the mouse, resulting in an unwanted mouse-movement rather
15711 than a simple mouse-click. */
15712 if (!w->start_at_line_beg
15713 && NILP (do_mouse_tracking)
15714 && CHARPOS (startp) > BEGV
15715 && CHARPOS (startp) > BEG + beg_unchanged
15716 && CHARPOS (startp) <= Z - end_unchanged
15717 /* Even if w->start_at_line_beg is nil, a new window may
15718 start at a line_beg, since that's how set_buffer_window
15719 sets it. So, we need to check the return value of
15720 compute_window_start_on_continuation_line. (See also
15721 bug#197). */
15722 && XMARKER (w->start)->buffer == current_buffer
15723 && compute_window_start_on_continuation_line (w)
15724 /* It doesn't make sense to force the window start like we
15725 do at label force_start if it is already known that point
15726 will not be visible in the resulting window, because
15727 doing so will move point from its correct position
15728 instead of scrolling the window to bring point into view.
15729 See bug#9324. */
15730 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15732 w->force_start = 1;
15733 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15734 goto force_start;
15737 #ifdef GLYPH_DEBUG
15738 debug_method_add (w, "same window start");
15739 #endif
15741 /* Try to redisplay starting at same place as before.
15742 If point has not moved off frame, accept the results. */
15743 if (!current_matrix_up_to_date_p
15744 /* Don't use try_window_reusing_current_matrix in this case
15745 because a window scroll function can have changed the
15746 buffer. */
15747 || !NILP (Vwindow_scroll_functions)
15748 || MINI_WINDOW_P (w)
15749 || !(used_current_matrix_p
15750 = try_window_reusing_current_matrix (w)))
15752 IF_DEBUG (debug_method_add (w, "1"));
15753 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15754 /* -1 means we need to scroll.
15755 0 means we need new matrices, but fonts_changed
15756 is set in that case, so we will detect it below. */
15757 goto try_to_scroll;
15760 if (f->fonts_changed)
15761 goto need_larger_matrices;
15763 if (w->cursor.vpos >= 0)
15765 if (!just_this_one_p
15766 || current_buffer->clip_changed
15767 || BEG_UNCHANGED < CHARPOS (startp))
15768 /* Forget any recorded base line for line number display. */
15769 w->base_line_number = 0;
15771 if (!cursor_row_fully_visible_p (w, 1, 0))
15773 clear_glyph_matrix (w->desired_matrix);
15774 last_line_misfit = 1;
15776 /* Drop through and scroll. */
15777 else
15778 goto done;
15780 else
15781 clear_glyph_matrix (w->desired_matrix);
15784 try_to_scroll:
15786 /* Redisplay the mode line. Select the buffer properly for that. */
15787 if (!update_mode_line)
15789 update_mode_line = 1;
15790 w->update_mode_line = 1;
15793 /* Try to scroll by specified few lines. */
15794 if ((scroll_conservatively
15795 || emacs_scroll_step
15796 || temp_scroll_step
15797 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15798 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15799 && CHARPOS (startp) >= BEGV
15800 && CHARPOS (startp) <= ZV)
15802 /* The function returns -1 if new fonts were loaded, 1 if
15803 successful, 0 if not successful. */
15804 int ss = try_scrolling (window, just_this_one_p,
15805 scroll_conservatively,
15806 emacs_scroll_step,
15807 temp_scroll_step, last_line_misfit);
15808 switch (ss)
15810 case SCROLLING_SUCCESS:
15811 goto done;
15813 case SCROLLING_NEED_LARGER_MATRICES:
15814 goto need_larger_matrices;
15816 case SCROLLING_FAILED:
15817 break;
15819 default:
15820 emacs_abort ();
15824 /* Finally, just choose a place to start which positions point
15825 according to user preferences. */
15827 recenter:
15829 #ifdef GLYPH_DEBUG
15830 debug_method_add (w, "recenter");
15831 #endif
15833 /* Forget any previously recorded base line for line number display. */
15834 if (!buffer_unchanged_p)
15835 w->base_line_number = 0;
15837 /* Determine the window start relative to point. */
15838 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15839 it.current_y = it.last_visible_y;
15840 if (centering_position < 0)
15842 int window_total_lines
15843 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15844 int margin =
15845 scroll_margin > 0
15846 ? min (scroll_margin, window_total_lines / 4)
15847 : 0;
15848 ptrdiff_t margin_pos = CHARPOS (startp);
15849 Lisp_Object aggressive;
15850 int scrolling_up;
15852 /* If there is a scroll margin at the top of the window, find
15853 its character position. */
15854 if (margin
15855 /* Cannot call start_display if startp is not in the
15856 accessible region of the buffer. This can happen when we
15857 have just switched to a different buffer and/or changed
15858 its restriction. In that case, startp is initialized to
15859 the character position 1 (BEGV) because we did not yet
15860 have chance to display the buffer even once. */
15861 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15863 struct it it1;
15864 void *it1data = NULL;
15866 SAVE_IT (it1, it, it1data);
15867 start_display (&it1, w, startp);
15868 move_it_vertically (&it1, margin * frame_line_height);
15869 margin_pos = IT_CHARPOS (it1);
15870 RESTORE_IT (&it, &it, it1data);
15872 scrolling_up = PT > margin_pos;
15873 aggressive =
15874 scrolling_up
15875 ? BVAR (current_buffer, scroll_up_aggressively)
15876 : BVAR (current_buffer, scroll_down_aggressively);
15878 if (!MINI_WINDOW_P (w)
15879 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15881 int pt_offset = 0;
15883 /* Setting scroll-conservatively overrides
15884 scroll-*-aggressively. */
15885 if (!scroll_conservatively && NUMBERP (aggressive))
15887 double float_amount = XFLOATINT (aggressive);
15889 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15890 if (pt_offset == 0 && float_amount > 0)
15891 pt_offset = 1;
15892 if (pt_offset && margin > 0)
15893 margin -= 1;
15895 /* Compute how much to move the window start backward from
15896 point so that point will be displayed where the user
15897 wants it. */
15898 if (scrolling_up)
15900 centering_position = it.last_visible_y;
15901 if (pt_offset)
15902 centering_position -= pt_offset;
15903 centering_position -=
15904 frame_line_height * (1 + margin + (last_line_misfit != 0))
15905 + WINDOW_HEADER_LINE_HEIGHT (w);
15906 /* Don't let point enter the scroll margin near top of
15907 the window. */
15908 if (centering_position < margin * frame_line_height)
15909 centering_position = margin * frame_line_height;
15911 else
15912 centering_position = margin * frame_line_height + pt_offset;
15914 else
15915 /* Set the window start half the height of the window backward
15916 from point. */
15917 centering_position = window_box_height (w) / 2;
15919 move_it_vertically_backward (&it, centering_position);
15921 eassert (IT_CHARPOS (it) >= BEGV);
15923 /* The function move_it_vertically_backward may move over more
15924 than the specified y-distance. If it->w is small, e.g. a
15925 mini-buffer window, we may end up in front of the window's
15926 display area. Start displaying at the start of the line
15927 containing PT in this case. */
15928 if (it.current_y <= 0)
15930 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15931 move_it_vertically_backward (&it, 0);
15932 it.current_y = 0;
15935 it.current_x = it.hpos = 0;
15937 /* Set the window start position here explicitly, to avoid an
15938 infinite loop in case the functions in window-scroll-functions
15939 get errors. */
15940 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15942 /* Run scroll hooks. */
15943 startp = run_window_scroll_functions (window, it.current.pos);
15945 /* Redisplay the window. */
15946 if (!current_matrix_up_to_date_p
15947 || windows_or_buffers_changed
15948 || f->cursor_type_changed
15949 /* Don't use try_window_reusing_current_matrix in this case
15950 because it can have changed the buffer. */
15951 || !NILP (Vwindow_scroll_functions)
15952 || !just_this_one_p
15953 || MINI_WINDOW_P (w)
15954 || !(used_current_matrix_p
15955 = try_window_reusing_current_matrix (w)))
15956 try_window (window, startp, 0);
15958 /* If new fonts have been loaded (due to fontsets), give up. We
15959 have to start a new redisplay since we need to re-adjust glyph
15960 matrices. */
15961 if (f->fonts_changed)
15962 goto need_larger_matrices;
15964 /* If cursor did not appear assume that the middle of the window is
15965 in the first line of the window. Do it again with the next line.
15966 (Imagine a window of height 100, displaying two lines of height
15967 60. Moving back 50 from it->last_visible_y will end in the first
15968 line.) */
15969 if (w->cursor.vpos < 0)
15971 if (w->window_end_valid && PT >= Z - w->window_end_pos)
15973 clear_glyph_matrix (w->desired_matrix);
15974 move_it_by_lines (&it, 1);
15975 try_window (window, it.current.pos, 0);
15977 else if (PT < IT_CHARPOS (it))
15979 clear_glyph_matrix (w->desired_matrix);
15980 move_it_by_lines (&it, -1);
15981 try_window (window, it.current.pos, 0);
15983 else
15985 /* Not much we can do about it. */
15989 /* Consider the following case: Window starts at BEGV, there is
15990 invisible, intangible text at BEGV, so that display starts at
15991 some point START > BEGV. It can happen that we are called with
15992 PT somewhere between BEGV and START. Try to handle that case. */
15993 if (w->cursor.vpos < 0)
15995 struct glyph_row *row = w->current_matrix->rows;
15996 if (row->mode_line_p)
15997 ++row;
15998 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16001 if (!cursor_row_fully_visible_p (w, 0, 0))
16003 /* If vscroll is enabled, disable it and try again. */
16004 if (w->vscroll)
16006 w->vscroll = 0;
16007 clear_glyph_matrix (w->desired_matrix);
16008 goto recenter;
16011 /* Users who set scroll-conservatively to a large number want
16012 point just above/below the scroll margin. If we ended up
16013 with point's row partially visible, move the window start to
16014 make that row fully visible and out of the margin. */
16015 if (scroll_conservatively > SCROLL_LIMIT)
16017 int window_total_lines
16018 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16019 int margin =
16020 scroll_margin > 0
16021 ? min (scroll_margin, window_total_lines / 4)
16022 : 0;
16023 int move_down = w->cursor.vpos >= window_total_lines / 2;
16025 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16026 clear_glyph_matrix (w->desired_matrix);
16027 if (1 == try_window (window, it.current.pos,
16028 TRY_WINDOW_CHECK_MARGINS))
16029 goto done;
16032 /* If centering point failed to make the whole line visible,
16033 put point at the top instead. That has to make the whole line
16034 visible, if it can be done. */
16035 if (centering_position == 0)
16036 goto done;
16038 clear_glyph_matrix (w->desired_matrix);
16039 centering_position = 0;
16040 goto recenter;
16043 done:
16045 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16046 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16047 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16049 /* Display the mode line, if we must. */
16050 if ((update_mode_line
16051 /* If window not full width, must redo its mode line
16052 if (a) the window to its side is being redone and
16053 (b) we do a frame-based redisplay. This is a consequence
16054 of how inverted lines are drawn in frame-based redisplay. */
16055 || (!just_this_one_p
16056 && !FRAME_WINDOW_P (f)
16057 && !WINDOW_FULL_WIDTH_P (w))
16058 /* Line number to display. */
16059 || w->base_line_pos > 0
16060 /* Column number is displayed and different from the one displayed. */
16061 || (w->column_number_displayed != -1
16062 && (w->column_number_displayed != current_column ())))
16063 /* This means that the window has a mode line. */
16064 && (WINDOW_WANTS_MODELINE_P (w)
16065 || WINDOW_WANTS_HEADER_LINE_P (w)))
16067 display_mode_lines (w);
16069 /* If mode line height has changed, arrange for a thorough
16070 immediate redisplay using the correct mode line height. */
16071 if (WINDOW_WANTS_MODELINE_P (w)
16072 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16074 f->fonts_changed = 1;
16075 w->mode_line_height = -1;
16076 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16077 = DESIRED_MODE_LINE_HEIGHT (w);
16080 /* If header line height has changed, arrange for a thorough
16081 immediate redisplay using the correct header line height. */
16082 if (WINDOW_WANTS_HEADER_LINE_P (w)
16083 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16085 f->fonts_changed = 1;
16086 w->header_line_height = -1;
16087 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16088 = DESIRED_HEADER_LINE_HEIGHT (w);
16091 if (f->fonts_changed)
16092 goto need_larger_matrices;
16095 if (!line_number_displayed && w->base_line_pos != -1)
16097 w->base_line_pos = 0;
16098 w->base_line_number = 0;
16101 finish_menu_bars:
16103 /* When we reach a frame's selected window, redo the frame's menu bar. */
16104 if (update_mode_line
16105 && EQ (FRAME_SELECTED_WINDOW (f), window))
16107 int redisplay_menu_p = 0;
16109 if (FRAME_WINDOW_P (f))
16111 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16112 || defined (HAVE_NS) || defined (USE_GTK)
16113 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16114 #else
16115 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16116 #endif
16118 else
16119 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16121 if (redisplay_menu_p)
16122 display_menu_bar (w);
16124 #ifdef HAVE_WINDOW_SYSTEM
16125 if (FRAME_WINDOW_P (f))
16127 #if defined (USE_GTK) || defined (HAVE_NS)
16128 if (FRAME_EXTERNAL_TOOL_BAR (f))
16129 redisplay_tool_bar (f);
16130 #else
16131 if (WINDOWP (f->tool_bar_window)
16132 && (FRAME_TOOL_BAR_LINES (f) > 0
16133 || !NILP (Vauto_resize_tool_bars))
16134 && redisplay_tool_bar (f))
16135 ignore_mouse_drag_p = 1;
16136 #endif
16138 #endif
16141 #ifdef HAVE_WINDOW_SYSTEM
16142 if (FRAME_WINDOW_P (f)
16143 && update_window_fringes (w, (just_this_one_p
16144 || (!used_current_matrix_p && !overlay_arrow_seen)
16145 || w->pseudo_window_p)))
16147 update_begin (f);
16148 block_input ();
16149 if (draw_window_fringes (w, 1))
16150 x_draw_vertical_border (w);
16151 unblock_input ();
16152 update_end (f);
16154 #endif /* HAVE_WINDOW_SYSTEM */
16156 /* We go to this label, with fonts_changed set, if it is
16157 necessary to try again using larger glyph matrices.
16158 We have to redeem the scroll bar even in this case,
16159 because the loop in redisplay_internal expects that. */
16160 need_larger_matrices:
16162 finish_scroll_bars:
16164 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16166 /* Set the thumb's position and size. */
16167 set_vertical_scroll_bar (w);
16169 /* Note that we actually used the scroll bar attached to this
16170 window, so it shouldn't be deleted at the end of redisplay. */
16171 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16172 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16175 /* Restore current_buffer and value of point in it. The window
16176 update may have changed the buffer, so first make sure `opoint'
16177 is still valid (Bug#6177). */
16178 if (CHARPOS (opoint) < BEGV)
16179 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16180 else if (CHARPOS (opoint) > ZV)
16181 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16182 else
16183 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16185 set_buffer_internal_1 (old);
16186 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16187 shorter. This can be caused by log truncation in *Messages*. */
16188 if (CHARPOS (lpoint) <= ZV)
16189 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16191 unbind_to (count, Qnil);
16195 /* Build the complete desired matrix of WINDOW with a window start
16196 buffer position POS.
16198 Value is 1 if successful. It is zero if fonts were loaded during
16199 redisplay which makes re-adjusting glyph matrices necessary, and -1
16200 if point would appear in the scroll margins.
16201 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16202 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16203 set in FLAGS.) */
16206 try_window (Lisp_Object window, struct text_pos pos, int flags)
16208 struct window *w = XWINDOW (window);
16209 struct it it;
16210 struct glyph_row *last_text_row = NULL;
16211 struct frame *f = XFRAME (w->frame);
16212 int frame_line_height = default_line_pixel_height (w);
16214 /* Make POS the new window start. */
16215 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16217 /* Mark cursor position as unknown. No overlay arrow seen. */
16218 w->cursor.vpos = -1;
16219 overlay_arrow_seen = 0;
16221 /* Initialize iterator and info to start at POS. */
16222 start_display (&it, w, pos);
16224 /* Display all lines of W. */
16225 while (it.current_y < it.last_visible_y)
16227 if (display_line (&it))
16228 last_text_row = it.glyph_row - 1;
16229 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16230 return 0;
16233 /* Don't let the cursor end in the scroll margins. */
16234 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16235 && !MINI_WINDOW_P (w))
16237 int this_scroll_margin;
16238 int window_total_lines
16239 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16241 if (scroll_margin > 0)
16243 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16244 this_scroll_margin *= frame_line_height;
16246 else
16247 this_scroll_margin = 0;
16249 if ((w->cursor.y >= 0 /* not vscrolled */
16250 && w->cursor.y < this_scroll_margin
16251 && CHARPOS (pos) > BEGV
16252 && IT_CHARPOS (it) < ZV)
16253 /* rms: considering make_cursor_line_fully_visible_p here
16254 seems to give wrong results. We don't want to recenter
16255 when the last line is partly visible, we want to allow
16256 that case to be handled in the usual way. */
16257 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16259 w->cursor.vpos = -1;
16260 clear_glyph_matrix (w->desired_matrix);
16261 return -1;
16265 /* If bottom moved off end of frame, change mode line percentage. */
16266 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16267 w->update_mode_line = 1;
16269 /* Set window_end_pos to the offset of the last character displayed
16270 on the window from the end of current_buffer. Set
16271 window_end_vpos to its row number. */
16272 if (last_text_row)
16274 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16275 adjust_window_ends (w, last_text_row, 0);
16276 eassert
16277 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16278 w->window_end_vpos)));
16280 else
16282 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16283 w->window_end_pos = Z - ZV;
16284 w->window_end_vpos = 0;
16287 /* But that is not valid info until redisplay finishes. */
16288 w->window_end_valid = 0;
16289 return 1;
16294 /************************************************************************
16295 Window redisplay reusing current matrix when buffer has not changed
16296 ************************************************************************/
16298 /* Try redisplay of window W showing an unchanged buffer with a
16299 different window start than the last time it was displayed by
16300 reusing its current matrix. Value is non-zero if successful.
16301 W->start is the new window start. */
16303 static int
16304 try_window_reusing_current_matrix (struct window *w)
16306 struct frame *f = XFRAME (w->frame);
16307 struct glyph_row *bottom_row;
16308 struct it it;
16309 struct run run;
16310 struct text_pos start, new_start;
16311 int nrows_scrolled, i;
16312 struct glyph_row *last_text_row;
16313 struct glyph_row *last_reused_text_row;
16314 struct glyph_row *start_row;
16315 int start_vpos, min_y, max_y;
16317 #ifdef GLYPH_DEBUG
16318 if (inhibit_try_window_reusing)
16319 return 0;
16320 #endif
16322 if (/* This function doesn't handle terminal frames. */
16323 !FRAME_WINDOW_P (f)
16324 /* Don't try to reuse the display if windows have been split
16325 or such. */
16326 || windows_or_buffers_changed
16327 || f->cursor_type_changed)
16328 return 0;
16330 /* Can't do this if showing trailing whitespace. */
16331 if (!NILP (Vshow_trailing_whitespace))
16332 return 0;
16334 /* If top-line visibility has changed, give up. */
16335 if (WINDOW_WANTS_HEADER_LINE_P (w)
16336 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16337 return 0;
16339 /* Give up if old or new display is scrolled vertically. We could
16340 make this function handle this, but right now it doesn't. */
16341 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16342 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16343 return 0;
16345 /* The variable new_start now holds the new window start. The old
16346 start `start' can be determined from the current matrix. */
16347 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16348 start = start_row->minpos;
16349 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16351 /* Clear the desired matrix for the display below. */
16352 clear_glyph_matrix (w->desired_matrix);
16354 if (CHARPOS (new_start) <= CHARPOS (start))
16356 /* Don't use this method if the display starts with an ellipsis
16357 displayed for invisible text. It's not easy to handle that case
16358 below, and it's certainly not worth the effort since this is
16359 not a frequent case. */
16360 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16361 return 0;
16363 IF_DEBUG (debug_method_add (w, "twu1"));
16365 /* Display up to a row that can be reused. The variable
16366 last_text_row is set to the last row displayed that displays
16367 text. Note that it.vpos == 0 if or if not there is a
16368 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16369 start_display (&it, w, new_start);
16370 w->cursor.vpos = -1;
16371 last_text_row = last_reused_text_row = NULL;
16373 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16375 /* If we have reached into the characters in the START row,
16376 that means the line boundaries have changed. So we
16377 can't start copying with the row START. Maybe it will
16378 work to start copying with the following row. */
16379 while (IT_CHARPOS (it) > CHARPOS (start))
16381 /* Advance to the next row as the "start". */
16382 start_row++;
16383 start = start_row->minpos;
16384 /* If there are no more rows to try, or just one, give up. */
16385 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16386 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16387 || CHARPOS (start) == ZV)
16389 clear_glyph_matrix (w->desired_matrix);
16390 return 0;
16393 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16395 /* If we have reached alignment, we can copy the rest of the
16396 rows. */
16397 if (IT_CHARPOS (it) == CHARPOS (start)
16398 /* Don't accept "alignment" inside a display vector,
16399 since start_row could have started in the middle of
16400 that same display vector (thus their character
16401 positions match), and we have no way of telling if
16402 that is the case. */
16403 && it.current.dpvec_index < 0)
16404 break;
16406 if (display_line (&it))
16407 last_text_row = it.glyph_row - 1;
16411 /* A value of current_y < last_visible_y means that we stopped
16412 at the previous window start, which in turn means that we
16413 have at least one reusable row. */
16414 if (it.current_y < it.last_visible_y)
16416 struct glyph_row *row;
16418 /* IT.vpos always starts from 0; it counts text lines. */
16419 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16421 /* Find PT if not already found in the lines displayed. */
16422 if (w->cursor.vpos < 0)
16424 int dy = it.current_y - start_row->y;
16426 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16427 row = row_containing_pos (w, PT, row, NULL, dy);
16428 if (row)
16429 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16430 dy, nrows_scrolled);
16431 else
16433 clear_glyph_matrix (w->desired_matrix);
16434 return 0;
16438 /* Scroll the display. Do it before the current matrix is
16439 changed. The problem here is that update has not yet
16440 run, i.e. part of the current matrix is not up to date.
16441 scroll_run_hook will clear the cursor, and use the
16442 current matrix to get the height of the row the cursor is
16443 in. */
16444 run.current_y = start_row->y;
16445 run.desired_y = it.current_y;
16446 run.height = it.last_visible_y - it.current_y;
16448 if (run.height > 0 && run.current_y != run.desired_y)
16450 update_begin (f);
16451 FRAME_RIF (f)->update_window_begin_hook (w);
16452 FRAME_RIF (f)->clear_window_mouse_face (w);
16453 FRAME_RIF (f)->scroll_run_hook (w, &run);
16454 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16455 update_end (f);
16458 /* Shift current matrix down by nrows_scrolled lines. */
16459 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16460 rotate_matrix (w->current_matrix,
16461 start_vpos,
16462 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16463 nrows_scrolled);
16465 /* Disable lines that must be updated. */
16466 for (i = 0; i < nrows_scrolled; ++i)
16467 (start_row + i)->enabled_p = 0;
16469 /* Re-compute Y positions. */
16470 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16471 max_y = it.last_visible_y;
16472 for (row = start_row + nrows_scrolled;
16473 row < bottom_row;
16474 ++row)
16476 row->y = it.current_y;
16477 row->visible_height = row->height;
16479 if (row->y < min_y)
16480 row->visible_height -= min_y - row->y;
16481 if (row->y + row->height > max_y)
16482 row->visible_height -= row->y + row->height - max_y;
16483 if (row->fringe_bitmap_periodic_p)
16484 row->redraw_fringe_bitmaps_p = 1;
16486 it.current_y += row->height;
16488 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16489 last_reused_text_row = row;
16490 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16491 break;
16494 /* Disable lines in the current matrix which are now
16495 below the window. */
16496 for (++row; row < bottom_row; ++row)
16497 row->enabled_p = row->mode_line_p = 0;
16500 /* Update window_end_pos etc.; last_reused_text_row is the last
16501 reused row from the current matrix containing text, if any.
16502 The value of last_text_row is the last displayed line
16503 containing text. */
16504 if (last_reused_text_row)
16505 adjust_window_ends (w, last_reused_text_row, 1);
16506 else if (last_text_row)
16507 adjust_window_ends (w, last_text_row, 0);
16508 else
16510 /* This window must be completely empty. */
16511 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16512 w->window_end_pos = Z - ZV;
16513 w->window_end_vpos = 0;
16515 w->window_end_valid = 0;
16517 /* Update hint: don't try scrolling again in update_window. */
16518 w->desired_matrix->no_scrolling_p = 1;
16520 #ifdef GLYPH_DEBUG
16521 debug_method_add (w, "try_window_reusing_current_matrix 1");
16522 #endif
16523 return 1;
16525 else if (CHARPOS (new_start) > CHARPOS (start))
16527 struct glyph_row *pt_row, *row;
16528 struct glyph_row *first_reusable_row;
16529 struct glyph_row *first_row_to_display;
16530 int dy;
16531 int yb = window_text_bottom_y (w);
16533 /* Find the row starting at new_start, if there is one. Don't
16534 reuse a partially visible line at the end. */
16535 first_reusable_row = start_row;
16536 while (first_reusable_row->enabled_p
16537 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16538 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16539 < CHARPOS (new_start)))
16540 ++first_reusable_row;
16542 /* Give up if there is no row to reuse. */
16543 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16544 || !first_reusable_row->enabled_p
16545 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16546 != CHARPOS (new_start)))
16547 return 0;
16549 /* We can reuse fully visible rows beginning with
16550 first_reusable_row to the end of the window. Set
16551 first_row_to_display to the first row that cannot be reused.
16552 Set pt_row to the row containing point, if there is any. */
16553 pt_row = NULL;
16554 for (first_row_to_display = first_reusable_row;
16555 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16556 ++first_row_to_display)
16558 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16559 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16560 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16561 && first_row_to_display->ends_at_zv_p
16562 && pt_row == NULL)))
16563 pt_row = first_row_to_display;
16566 /* Start displaying at the start of first_row_to_display. */
16567 eassert (first_row_to_display->y < yb);
16568 init_to_row_start (&it, w, first_row_to_display);
16570 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16571 - start_vpos);
16572 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16573 - nrows_scrolled);
16574 it.current_y = (first_row_to_display->y - first_reusable_row->y
16575 + WINDOW_HEADER_LINE_HEIGHT (w));
16577 /* Display lines beginning with first_row_to_display in the
16578 desired matrix. Set last_text_row to the last row displayed
16579 that displays text. */
16580 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16581 if (pt_row == NULL)
16582 w->cursor.vpos = -1;
16583 last_text_row = NULL;
16584 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16585 if (display_line (&it))
16586 last_text_row = it.glyph_row - 1;
16588 /* If point is in a reused row, adjust y and vpos of the cursor
16589 position. */
16590 if (pt_row)
16592 w->cursor.vpos -= nrows_scrolled;
16593 w->cursor.y -= first_reusable_row->y - start_row->y;
16596 /* Give up if point isn't in a row displayed or reused. (This
16597 also handles the case where w->cursor.vpos < nrows_scrolled
16598 after the calls to display_line, which can happen with scroll
16599 margins. See bug#1295.) */
16600 if (w->cursor.vpos < 0)
16602 clear_glyph_matrix (w->desired_matrix);
16603 return 0;
16606 /* Scroll the display. */
16607 run.current_y = first_reusable_row->y;
16608 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16609 run.height = it.last_visible_y - run.current_y;
16610 dy = run.current_y - run.desired_y;
16612 if (run.height)
16614 update_begin (f);
16615 FRAME_RIF (f)->update_window_begin_hook (w);
16616 FRAME_RIF (f)->clear_window_mouse_face (w);
16617 FRAME_RIF (f)->scroll_run_hook (w, &run);
16618 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16619 update_end (f);
16622 /* Adjust Y positions of reused rows. */
16623 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16624 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16625 max_y = it.last_visible_y;
16626 for (row = first_reusable_row; row < first_row_to_display; ++row)
16628 row->y -= dy;
16629 row->visible_height = row->height;
16630 if (row->y < min_y)
16631 row->visible_height -= min_y - row->y;
16632 if (row->y + row->height > max_y)
16633 row->visible_height -= row->y + row->height - max_y;
16634 if (row->fringe_bitmap_periodic_p)
16635 row->redraw_fringe_bitmaps_p = 1;
16638 /* Scroll the current matrix. */
16639 eassert (nrows_scrolled > 0);
16640 rotate_matrix (w->current_matrix,
16641 start_vpos,
16642 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16643 -nrows_scrolled);
16645 /* Disable rows not reused. */
16646 for (row -= nrows_scrolled; row < bottom_row; ++row)
16647 row->enabled_p = 0;
16649 /* Point may have moved to a different line, so we cannot assume that
16650 the previous cursor position is valid; locate the correct row. */
16651 if (pt_row)
16653 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16654 row < bottom_row
16655 && PT >= MATRIX_ROW_END_CHARPOS (row)
16656 && !row->ends_at_zv_p;
16657 row++)
16659 w->cursor.vpos++;
16660 w->cursor.y = row->y;
16662 if (row < bottom_row)
16664 /* Can't simply scan the row for point with
16665 bidi-reordered glyph rows. Let set_cursor_from_row
16666 figure out where to put the cursor, and if it fails,
16667 give up. */
16668 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16670 if (!set_cursor_from_row (w, row, w->current_matrix,
16671 0, 0, 0, 0))
16673 clear_glyph_matrix (w->desired_matrix);
16674 return 0;
16677 else
16679 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16680 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16682 for (; glyph < end
16683 && (!BUFFERP (glyph->object)
16684 || glyph->charpos < PT);
16685 glyph++)
16687 w->cursor.hpos++;
16688 w->cursor.x += glyph->pixel_width;
16694 /* Adjust window end. A null value of last_text_row means that
16695 the window end is in reused rows which in turn means that
16696 only its vpos can have changed. */
16697 if (last_text_row)
16698 adjust_window_ends (w, last_text_row, 0);
16699 else
16700 w->window_end_vpos -= nrows_scrolled;
16702 w->window_end_valid = 0;
16703 w->desired_matrix->no_scrolling_p = 1;
16705 #ifdef GLYPH_DEBUG
16706 debug_method_add (w, "try_window_reusing_current_matrix 2");
16707 #endif
16708 return 1;
16711 return 0;
16716 /************************************************************************
16717 Window redisplay reusing current matrix when buffer has changed
16718 ************************************************************************/
16720 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16721 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16722 ptrdiff_t *, ptrdiff_t *);
16723 static struct glyph_row *
16724 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16725 struct glyph_row *);
16728 /* Return the last row in MATRIX displaying text. If row START is
16729 non-null, start searching with that row. IT gives the dimensions
16730 of the display. Value is null if matrix is empty; otherwise it is
16731 a pointer to the row found. */
16733 static struct glyph_row *
16734 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16735 struct glyph_row *start)
16737 struct glyph_row *row, *row_found;
16739 /* Set row_found to the last row in IT->w's current matrix
16740 displaying text. The loop looks funny but think of partially
16741 visible lines. */
16742 row_found = NULL;
16743 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16744 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16746 eassert (row->enabled_p);
16747 row_found = row;
16748 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16749 break;
16750 ++row;
16753 return row_found;
16757 /* Return the last row in the current matrix of W that is not affected
16758 by changes at the start of current_buffer that occurred since W's
16759 current matrix was built. Value is null if no such row exists.
16761 BEG_UNCHANGED us the number of characters unchanged at the start of
16762 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16763 first changed character in current_buffer. Characters at positions <
16764 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16765 when the current matrix was built. */
16767 static struct glyph_row *
16768 find_last_unchanged_at_beg_row (struct window *w)
16770 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16771 struct glyph_row *row;
16772 struct glyph_row *row_found = NULL;
16773 int yb = window_text_bottom_y (w);
16775 /* Find the last row displaying unchanged text. */
16776 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16777 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16778 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16779 ++row)
16781 if (/* If row ends before first_changed_pos, it is unchanged,
16782 except in some case. */
16783 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16784 /* When row ends in ZV and we write at ZV it is not
16785 unchanged. */
16786 && !row->ends_at_zv_p
16787 /* When first_changed_pos is the end of a continued line,
16788 row is not unchanged because it may be no longer
16789 continued. */
16790 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16791 && (row->continued_p
16792 || row->exact_window_width_line_p))
16793 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16794 needs to be recomputed, so don't consider this row as
16795 unchanged. This happens when the last line was
16796 bidi-reordered and was killed immediately before this
16797 redisplay cycle. In that case, ROW->end stores the
16798 buffer position of the first visual-order character of
16799 the killed text, which is now beyond ZV. */
16800 && CHARPOS (row->end.pos) <= ZV)
16801 row_found = row;
16803 /* Stop if last visible row. */
16804 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16805 break;
16808 return row_found;
16812 /* Find the first glyph row in the current matrix of W that is not
16813 affected by changes at the end of current_buffer since the
16814 time W's current matrix was built.
16816 Return in *DELTA the number of chars by which buffer positions in
16817 unchanged text at the end of current_buffer must be adjusted.
16819 Return in *DELTA_BYTES the corresponding number of bytes.
16821 Value is null if no such row exists, i.e. all rows are affected by
16822 changes. */
16824 static struct glyph_row *
16825 find_first_unchanged_at_end_row (struct window *w,
16826 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16828 struct glyph_row *row;
16829 struct glyph_row *row_found = NULL;
16831 *delta = *delta_bytes = 0;
16833 /* Display must not have been paused, otherwise the current matrix
16834 is not up to date. */
16835 eassert (w->window_end_valid);
16837 /* A value of window_end_pos >= END_UNCHANGED means that the window
16838 end is in the range of changed text. If so, there is no
16839 unchanged row at the end of W's current matrix. */
16840 if (w->window_end_pos >= END_UNCHANGED)
16841 return NULL;
16843 /* Set row to the last row in W's current matrix displaying text. */
16844 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16846 /* If matrix is entirely empty, no unchanged row exists. */
16847 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16849 /* The value of row is the last glyph row in the matrix having a
16850 meaningful buffer position in it. The end position of row
16851 corresponds to window_end_pos. This allows us to translate
16852 buffer positions in the current matrix to current buffer
16853 positions for characters not in changed text. */
16854 ptrdiff_t Z_old =
16855 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16856 ptrdiff_t Z_BYTE_old =
16857 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16858 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16859 struct glyph_row *first_text_row
16860 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16862 *delta = Z - Z_old;
16863 *delta_bytes = Z_BYTE - Z_BYTE_old;
16865 /* Set last_unchanged_pos to the buffer position of the last
16866 character in the buffer that has not been changed. Z is the
16867 index + 1 of the last character in current_buffer, i.e. by
16868 subtracting END_UNCHANGED we get the index of the last
16869 unchanged character, and we have to add BEG to get its buffer
16870 position. */
16871 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16872 last_unchanged_pos_old = last_unchanged_pos - *delta;
16874 /* Search backward from ROW for a row displaying a line that
16875 starts at a minimum position >= last_unchanged_pos_old. */
16876 for (; row > first_text_row; --row)
16878 /* This used to abort, but it can happen.
16879 It is ok to just stop the search instead here. KFS. */
16880 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16881 break;
16883 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16884 row_found = row;
16888 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16890 return row_found;
16894 /* Make sure that glyph rows in the current matrix of window W
16895 reference the same glyph memory as corresponding rows in the
16896 frame's frame matrix. This function is called after scrolling W's
16897 current matrix on a terminal frame in try_window_id and
16898 try_window_reusing_current_matrix. */
16900 static void
16901 sync_frame_with_window_matrix_rows (struct window *w)
16903 struct frame *f = XFRAME (w->frame);
16904 struct glyph_row *window_row, *window_row_end, *frame_row;
16906 /* Preconditions: W must be a leaf window and full-width. Its frame
16907 must have a frame matrix. */
16908 eassert (BUFFERP (w->contents));
16909 eassert (WINDOW_FULL_WIDTH_P (w));
16910 eassert (!FRAME_WINDOW_P (f));
16912 /* If W is a full-width window, glyph pointers in W's current matrix
16913 have, by definition, to be the same as glyph pointers in the
16914 corresponding frame matrix. Note that frame matrices have no
16915 marginal areas (see build_frame_matrix). */
16916 window_row = w->current_matrix->rows;
16917 window_row_end = window_row + w->current_matrix->nrows;
16918 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16919 while (window_row < window_row_end)
16921 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16922 struct glyph *end = window_row->glyphs[LAST_AREA];
16924 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16925 frame_row->glyphs[TEXT_AREA] = start;
16926 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16927 frame_row->glyphs[LAST_AREA] = end;
16929 /* Disable frame rows whose corresponding window rows have
16930 been disabled in try_window_id. */
16931 if (!window_row->enabled_p)
16932 frame_row->enabled_p = 0;
16934 ++window_row, ++frame_row;
16939 /* Find the glyph row in window W containing CHARPOS. Consider all
16940 rows between START and END (not inclusive). END null means search
16941 all rows to the end of the display area of W. Value is the row
16942 containing CHARPOS or null. */
16944 struct glyph_row *
16945 row_containing_pos (struct window *w, ptrdiff_t charpos,
16946 struct glyph_row *start, struct glyph_row *end, int dy)
16948 struct glyph_row *row = start;
16949 struct glyph_row *best_row = NULL;
16950 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16951 int last_y;
16953 /* If we happen to start on a header-line, skip that. */
16954 if (row->mode_line_p)
16955 ++row;
16957 if ((end && row >= end) || !row->enabled_p)
16958 return NULL;
16960 last_y = window_text_bottom_y (w) - dy;
16962 while (1)
16964 /* Give up if we have gone too far. */
16965 if (end && row >= end)
16966 return NULL;
16967 /* This formerly returned if they were equal.
16968 I think that both quantities are of a "last plus one" type;
16969 if so, when they are equal, the row is within the screen. -- rms. */
16970 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16971 return NULL;
16973 /* If it is in this row, return this row. */
16974 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16975 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16976 /* The end position of a row equals the start
16977 position of the next row. If CHARPOS is there, we
16978 would rather consider it displayed in the next
16979 line, except when this line ends in ZV. */
16980 && !row_for_charpos_p (row, charpos)))
16981 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16983 struct glyph *g;
16985 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
16986 || (!best_row && !row->continued_p))
16987 return row;
16988 /* In bidi-reordered rows, there could be several rows whose
16989 edges surround CHARPOS, all of these rows belonging to
16990 the same continued line. We need to find the row which
16991 fits CHARPOS the best. */
16992 for (g = row->glyphs[TEXT_AREA];
16993 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16994 g++)
16996 if (!STRINGP (g->object))
16998 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17000 mindif = eabs (g->charpos - charpos);
17001 best_row = row;
17002 /* Exact match always wins. */
17003 if (mindif == 0)
17004 return best_row;
17009 else if (best_row && !row->continued_p)
17010 return best_row;
17011 ++row;
17016 /* Try to redisplay window W by reusing its existing display. W's
17017 current matrix must be up to date when this function is called,
17018 i.e. window_end_valid must be nonzero.
17020 Value is
17022 1 if display has been updated
17023 0 if otherwise unsuccessful
17024 -1 if redisplay with same window start is known not to succeed
17026 The following steps are performed:
17028 1. Find the last row in the current matrix of W that is not
17029 affected by changes at the start of current_buffer. If no such row
17030 is found, give up.
17032 2. Find the first row in W's current matrix that is not affected by
17033 changes at the end of current_buffer. Maybe there is no such row.
17035 3. Display lines beginning with the row + 1 found in step 1 to the
17036 row found in step 2 or, if step 2 didn't find a row, to the end of
17037 the window.
17039 4. If cursor is not known to appear on the window, give up.
17041 5. If display stopped at the row found in step 2, scroll the
17042 display and current matrix as needed.
17044 6. Maybe display some lines at the end of W, if we must. This can
17045 happen under various circumstances, like a partially visible line
17046 becoming fully visible, or because newly displayed lines are displayed
17047 in smaller font sizes.
17049 7. Update W's window end information. */
17051 static int
17052 try_window_id (struct window *w)
17054 struct frame *f = XFRAME (w->frame);
17055 struct glyph_matrix *current_matrix = w->current_matrix;
17056 struct glyph_matrix *desired_matrix = w->desired_matrix;
17057 struct glyph_row *last_unchanged_at_beg_row;
17058 struct glyph_row *first_unchanged_at_end_row;
17059 struct glyph_row *row;
17060 struct glyph_row *bottom_row;
17061 int bottom_vpos;
17062 struct it it;
17063 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17064 int dvpos, dy;
17065 struct text_pos start_pos;
17066 struct run run;
17067 int first_unchanged_at_end_vpos = 0;
17068 struct glyph_row *last_text_row, *last_text_row_at_end;
17069 struct text_pos start;
17070 ptrdiff_t first_changed_charpos, last_changed_charpos;
17072 #ifdef GLYPH_DEBUG
17073 if (inhibit_try_window_id)
17074 return 0;
17075 #endif
17077 /* This is handy for debugging. */
17078 #if 0
17079 #define GIVE_UP(X) \
17080 do { \
17081 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17082 return 0; \
17083 } while (0)
17084 #else
17085 #define GIVE_UP(X) return 0
17086 #endif
17088 SET_TEXT_POS_FROM_MARKER (start, w->start);
17090 /* Don't use this for mini-windows because these can show
17091 messages and mini-buffers, and we don't handle that here. */
17092 if (MINI_WINDOW_P (w))
17093 GIVE_UP (1);
17095 /* This flag is used to prevent redisplay optimizations. */
17096 if (windows_or_buffers_changed || f->cursor_type_changed)
17097 GIVE_UP (2);
17099 /* Verify that narrowing has not changed.
17100 Also verify that we were not told to prevent redisplay optimizations.
17101 It would be nice to further
17102 reduce the number of cases where this prevents try_window_id. */
17103 if (current_buffer->clip_changed
17104 || current_buffer->prevent_redisplay_optimizations_p)
17105 GIVE_UP (3);
17107 /* Window must either use window-based redisplay or be full width. */
17108 if (!FRAME_WINDOW_P (f)
17109 && (!FRAME_LINE_INS_DEL_OK (f)
17110 || !WINDOW_FULL_WIDTH_P (w)))
17111 GIVE_UP (4);
17113 /* Give up if point is known NOT to appear in W. */
17114 if (PT < CHARPOS (start))
17115 GIVE_UP (5);
17117 /* Another way to prevent redisplay optimizations. */
17118 if (w->last_modified == 0)
17119 GIVE_UP (6);
17121 /* Verify that window is not hscrolled. */
17122 if (w->hscroll != 0)
17123 GIVE_UP (7);
17125 /* Verify that display wasn't paused. */
17126 if (!w->window_end_valid)
17127 GIVE_UP (8);
17129 /* Likewise if highlighting trailing whitespace. */
17130 if (!NILP (Vshow_trailing_whitespace))
17131 GIVE_UP (11);
17133 /* Can't use this if overlay arrow position and/or string have
17134 changed. */
17135 if (overlay_arrows_changed_p ())
17136 GIVE_UP (12);
17138 /* When word-wrap is on, adding a space to the first word of a
17139 wrapped line can change the wrap position, altering the line
17140 above it. It might be worthwhile to handle this more
17141 intelligently, but for now just redisplay from scratch. */
17142 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17143 GIVE_UP (21);
17145 /* Under bidi reordering, adding or deleting a character in the
17146 beginning of a paragraph, before the first strong directional
17147 character, can change the base direction of the paragraph (unless
17148 the buffer specifies a fixed paragraph direction), which will
17149 require to redisplay the whole paragraph. It might be worthwhile
17150 to find the paragraph limits and widen the range of redisplayed
17151 lines to that, but for now just give up this optimization and
17152 redisplay from scratch. */
17153 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17154 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17155 GIVE_UP (22);
17157 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17158 only if buffer has really changed. The reason is that the gap is
17159 initially at Z for freshly visited files. The code below would
17160 set end_unchanged to 0 in that case. */
17161 if (MODIFF > SAVE_MODIFF
17162 /* This seems to happen sometimes after saving a buffer. */
17163 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17165 if (GPT - BEG < BEG_UNCHANGED)
17166 BEG_UNCHANGED = GPT - BEG;
17167 if (Z - GPT < END_UNCHANGED)
17168 END_UNCHANGED = Z - GPT;
17171 /* The position of the first and last character that has been changed. */
17172 first_changed_charpos = BEG + BEG_UNCHANGED;
17173 last_changed_charpos = Z - END_UNCHANGED;
17175 /* If window starts after a line end, and the last change is in
17176 front of that newline, then changes don't affect the display.
17177 This case happens with stealth-fontification. Note that although
17178 the display is unchanged, glyph positions in the matrix have to
17179 be adjusted, of course. */
17180 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17181 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17182 && ((last_changed_charpos < CHARPOS (start)
17183 && CHARPOS (start) == BEGV)
17184 || (last_changed_charpos < CHARPOS (start) - 1
17185 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17187 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17188 struct glyph_row *r0;
17190 /* Compute how many chars/bytes have been added to or removed
17191 from the buffer. */
17192 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17193 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17194 Z_delta = Z - Z_old;
17195 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17197 /* Give up if PT is not in the window. Note that it already has
17198 been checked at the start of try_window_id that PT is not in
17199 front of the window start. */
17200 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17201 GIVE_UP (13);
17203 /* If window start is unchanged, we can reuse the whole matrix
17204 as is, after adjusting glyph positions. No need to compute
17205 the window end again, since its offset from Z hasn't changed. */
17206 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17207 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17208 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17209 /* PT must not be in a partially visible line. */
17210 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17211 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17213 /* Adjust positions in the glyph matrix. */
17214 if (Z_delta || Z_delta_bytes)
17216 struct glyph_row *r1
17217 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17218 increment_matrix_positions (w->current_matrix,
17219 MATRIX_ROW_VPOS (r0, current_matrix),
17220 MATRIX_ROW_VPOS (r1, current_matrix),
17221 Z_delta, Z_delta_bytes);
17224 /* Set the cursor. */
17225 row = row_containing_pos (w, PT, r0, NULL, 0);
17226 if (row)
17227 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17228 return 1;
17232 /* Handle the case that changes are all below what is displayed in
17233 the window, and that PT is in the window. This shortcut cannot
17234 be taken if ZV is visible in the window, and text has been added
17235 there that is visible in the window. */
17236 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17237 /* ZV is not visible in the window, or there are no
17238 changes at ZV, actually. */
17239 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17240 || first_changed_charpos == last_changed_charpos))
17242 struct glyph_row *r0;
17244 /* Give up if PT is not in the window. Note that it already has
17245 been checked at the start of try_window_id that PT is not in
17246 front of the window start. */
17247 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17248 GIVE_UP (14);
17250 /* If window start is unchanged, we can reuse the whole matrix
17251 as is, without changing glyph positions since no text has
17252 been added/removed in front of the window end. */
17253 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17254 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17255 /* PT must not be in a partially visible line. */
17256 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17257 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17259 /* We have to compute the window end anew since text
17260 could have been added/removed after it. */
17261 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17262 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17264 /* Set the cursor. */
17265 row = row_containing_pos (w, PT, r0, NULL, 0);
17266 if (row)
17267 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17268 return 2;
17272 /* Give up if window start is in the changed area.
17274 The condition used to read
17276 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17278 but why that was tested escapes me at the moment. */
17279 if (CHARPOS (start) >= first_changed_charpos
17280 && CHARPOS (start) <= last_changed_charpos)
17281 GIVE_UP (15);
17283 /* Check that window start agrees with the start of the first glyph
17284 row in its current matrix. Check this after we know the window
17285 start is not in changed text, otherwise positions would not be
17286 comparable. */
17287 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17288 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17289 GIVE_UP (16);
17291 /* Give up if the window ends in strings. Overlay strings
17292 at the end are difficult to handle, so don't try. */
17293 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17294 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17295 GIVE_UP (20);
17297 /* Compute the position at which we have to start displaying new
17298 lines. Some of the lines at the top of the window might be
17299 reusable because they are not displaying changed text. Find the
17300 last row in W's current matrix not affected by changes at the
17301 start of current_buffer. Value is null if changes start in the
17302 first line of window. */
17303 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17304 if (last_unchanged_at_beg_row)
17306 /* Avoid starting to display in the middle of a character, a TAB
17307 for instance. This is easier than to set up the iterator
17308 exactly, and it's not a frequent case, so the additional
17309 effort wouldn't really pay off. */
17310 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17311 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17312 && last_unchanged_at_beg_row > w->current_matrix->rows)
17313 --last_unchanged_at_beg_row;
17315 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17316 GIVE_UP (17);
17318 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17319 GIVE_UP (18);
17320 start_pos = it.current.pos;
17322 /* Start displaying new lines in the desired matrix at the same
17323 vpos we would use in the current matrix, i.e. below
17324 last_unchanged_at_beg_row. */
17325 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17326 current_matrix);
17327 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17328 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17330 eassert (it.hpos == 0 && it.current_x == 0);
17332 else
17334 /* There are no reusable lines at the start of the window.
17335 Start displaying in the first text line. */
17336 start_display (&it, w, start);
17337 it.vpos = it.first_vpos;
17338 start_pos = it.current.pos;
17341 /* Find the first row that is not affected by changes at the end of
17342 the buffer. Value will be null if there is no unchanged row, in
17343 which case we must redisplay to the end of the window. delta
17344 will be set to the value by which buffer positions beginning with
17345 first_unchanged_at_end_row have to be adjusted due to text
17346 changes. */
17347 first_unchanged_at_end_row
17348 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17349 IF_DEBUG (debug_delta = delta);
17350 IF_DEBUG (debug_delta_bytes = delta_bytes);
17352 /* Set stop_pos to the buffer position up to which we will have to
17353 display new lines. If first_unchanged_at_end_row != NULL, this
17354 is the buffer position of the start of the line displayed in that
17355 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17356 that we don't stop at a buffer position. */
17357 stop_pos = 0;
17358 if (first_unchanged_at_end_row)
17360 eassert (last_unchanged_at_beg_row == NULL
17361 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17363 /* If this is a continuation line, move forward to the next one
17364 that isn't. Changes in lines above affect this line.
17365 Caution: this may move first_unchanged_at_end_row to a row
17366 not displaying text. */
17367 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17368 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17369 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17370 < it.last_visible_y))
17371 ++first_unchanged_at_end_row;
17373 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17374 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17375 >= it.last_visible_y))
17376 first_unchanged_at_end_row = NULL;
17377 else
17379 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17380 + delta);
17381 first_unchanged_at_end_vpos
17382 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17383 eassert (stop_pos >= Z - END_UNCHANGED);
17386 else if (last_unchanged_at_beg_row == NULL)
17387 GIVE_UP (19);
17390 #ifdef GLYPH_DEBUG
17392 /* Either there is no unchanged row at the end, or the one we have
17393 now displays text. This is a necessary condition for the window
17394 end pos calculation at the end of this function. */
17395 eassert (first_unchanged_at_end_row == NULL
17396 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17398 debug_last_unchanged_at_beg_vpos
17399 = (last_unchanged_at_beg_row
17400 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17401 : -1);
17402 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17404 #endif /* GLYPH_DEBUG */
17407 /* Display new lines. Set last_text_row to the last new line
17408 displayed which has text on it, i.e. might end up as being the
17409 line where the window_end_vpos is. */
17410 w->cursor.vpos = -1;
17411 last_text_row = NULL;
17412 overlay_arrow_seen = 0;
17413 while (it.current_y < it.last_visible_y
17414 && !f->fonts_changed
17415 && (first_unchanged_at_end_row == NULL
17416 || IT_CHARPOS (it) < stop_pos))
17418 if (display_line (&it))
17419 last_text_row = it.glyph_row - 1;
17422 if (f->fonts_changed)
17423 return -1;
17426 /* Compute differences in buffer positions, y-positions etc. for
17427 lines reused at the bottom of the window. Compute what we can
17428 scroll. */
17429 if (first_unchanged_at_end_row
17430 /* No lines reused because we displayed everything up to the
17431 bottom of the window. */
17432 && it.current_y < it.last_visible_y)
17434 dvpos = (it.vpos
17435 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17436 current_matrix));
17437 dy = it.current_y - first_unchanged_at_end_row->y;
17438 run.current_y = first_unchanged_at_end_row->y;
17439 run.desired_y = run.current_y + dy;
17440 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17442 else
17444 delta = delta_bytes = dvpos = dy
17445 = run.current_y = run.desired_y = run.height = 0;
17446 first_unchanged_at_end_row = NULL;
17448 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17451 /* Find the cursor if not already found. We have to decide whether
17452 PT will appear on this window (it sometimes doesn't, but this is
17453 not a very frequent case.) This decision has to be made before
17454 the current matrix is altered. A value of cursor.vpos < 0 means
17455 that PT is either in one of the lines beginning at
17456 first_unchanged_at_end_row or below the window. Don't care for
17457 lines that might be displayed later at the window end; as
17458 mentioned, this is not a frequent case. */
17459 if (w->cursor.vpos < 0)
17461 /* Cursor in unchanged rows at the top? */
17462 if (PT < CHARPOS (start_pos)
17463 && last_unchanged_at_beg_row)
17465 row = row_containing_pos (w, PT,
17466 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17467 last_unchanged_at_beg_row + 1, 0);
17468 if (row)
17469 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17472 /* Start from first_unchanged_at_end_row looking for PT. */
17473 else if (first_unchanged_at_end_row)
17475 row = row_containing_pos (w, PT - delta,
17476 first_unchanged_at_end_row, NULL, 0);
17477 if (row)
17478 set_cursor_from_row (w, row, w->current_matrix, delta,
17479 delta_bytes, dy, dvpos);
17482 /* Give up if cursor was not found. */
17483 if (w->cursor.vpos < 0)
17485 clear_glyph_matrix (w->desired_matrix);
17486 return -1;
17490 /* Don't let the cursor end in the scroll margins. */
17492 int this_scroll_margin, cursor_height;
17493 int frame_line_height = default_line_pixel_height (w);
17494 int window_total_lines
17495 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17497 this_scroll_margin =
17498 max (0, min (scroll_margin, window_total_lines / 4));
17499 this_scroll_margin *= frame_line_height;
17500 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17502 if ((w->cursor.y < this_scroll_margin
17503 && CHARPOS (start) > BEGV)
17504 /* Old redisplay didn't take scroll margin into account at the bottom,
17505 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17506 || (w->cursor.y + (make_cursor_line_fully_visible_p
17507 ? cursor_height + this_scroll_margin
17508 : 1)) > it.last_visible_y)
17510 w->cursor.vpos = -1;
17511 clear_glyph_matrix (w->desired_matrix);
17512 return -1;
17516 /* Scroll the display. Do it before changing the current matrix so
17517 that xterm.c doesn't get confused about where the cursor glyph is
17518 found. */
17519 if (dy && run.height)
17521 update_begin (f);
17523 if (FRAME_WINDOW_P (f))
17525 FRAME_RIF (f)->update_window_begin_hook (w);
17526 FRAME_RIF (f)->clear_window_mouse_face (w);
17527 FRAME_RIF (f)->scroll_run_hook (w, &run);
17528 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17530 else
17532 /* Terminal frame. In this case, dvpos gives the number of
17533 lines to scroll by; dvpos < 0 means scroll up. */
17534 int from_vpos
17535 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17536 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17537 int end = (WINDOW_TOP_EDGE_LINE (w)
17538 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17539 + window_internal_height (w));
17541 #if defined (HAVE_GPM) || defined (MSDOS)
17542 x_clear_window_mouse_face (w);
17543 #endif
17544 /* Perform the operation on the screen. */
17545 if (dvpos > 0)
17547 /* Scroll last_unchanged_at_beg_row to the end of the
17548 window down dvpos lines. */
17549 set_terminal_window (f, end);
17551 /* On dumb terminals delete dvpos lines at the end
17552 before inserting dvpos empty lines. */
17553 if (!FRAME_SCROLL_REGION_OK (f))
17554 ins_del_lines (f, end - dvpos, -dvpos);
17556 /* Insert dvpos empty lines in front of
17557 last_unchanged_at_beg_row. */
17558 ins_del_lines (f, from, dvpos);
17560 else if (dvpos < 0)
17562 /* Scroll up last_unchanged_at_beg_vpos to the end of
17563 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17564 set_terminal_window (f, end);
17566 /* Delete dvpos lines in front of
17567 last_unchanged_at_beg_vpos. ins_del_lines will set
17568 the cursor to the given vpos and emit |dvpos| delete
17569 line sequences. */
17570 ins_del_lines (f, from + dvpos, dvpos);
17572 /* On a dumb terminal insert dvpos empty lines at the
17573 end. */
17574 if (!FRAME_SCROLL_REGION_OK (f))
17575 ins_del_lines (f, end + dvpos, -dvpos);
17578 set_terminal_window (f, 0);
17581 update_end (f);
17584 /* Shift reused rows of the current matrix to the right position.
17585 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17586 text. */
17587 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17588 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17589 if (dvpos < 0)
17591 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17592 bottom_vpos, dvpos);
17593 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17594 bottom_vpos);
17596 else if (dvpos > 0)
17598 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17599 bottom_vpos, dvpos);
17600 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17601 first_unchanged_at_end_vpos + dvpos);
17604 /* For frame-based redisplay, make sure that current frame and window
17605 matrix are in sync with respect to glyph memory. */
17606 if (!FRAME_WINDOW_P (f))
17607 sync_frame_with_window_matrix_rows (w);
17609 /* Adjust buffer positions in reused rows. */
17610 if (delta || delta_bytes)
17611 increment_matrix_positions (current_matrix,
17612 first_unchanged_at_end_vpos + dvpos,
17613 bottom_vpos, delta, delta_bytes);
17615 /* Adjust Y positions. */
17616 if (dy)
17617 shift_glyph_matrix (w, current_matrix,
17618 first_unchanged_at_end_vpos + dvpos,
17619 bottom_vpos, dy);
17621 if (first_unchanged_at_end_row)
17623 first_unchanged_at_end_row += dvpos;
17624 if (first_unchanged_at_end_row->y >= it.last_visible_y
17625 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17626 first_unchanged_at_end_row = NULL;
17629 /* If scrolling up, there may be some lines to display at the end of
17630 the window. */
17631 last_text_row_at_end = NULL;
17632 if (dy < 0)
17634 /* Scrolling up can leave for example a partially visible line
17635 at the end of the window to be redisplayed. */
17636 /* Set last_row to the glyph row in the current matrix where the
17637 window end line is found. It has been moved up or down in
17638 the matrix by dvpos. */
17639 int last_vpos = w->window_end_vpos + dvpos;
17640 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17642 /* If last_row is the window end line, it should display text. */
17643 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17645 /* If window end line was partially visible before, begin
17646 displaying at that line. Otherwise begin displaying with the
17647 line following it. */
17648 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17650 init_to_row_start (&it, w, last_row);
17651 it.vpos = last_vpos;
17652 it.current_y = last_row->y;
17654 else
17656 init_to_row_end (&it, w, last_row);
17657 it.vpos = 1 + last_vpos;
17658 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17659 ++last_row;
17662 /* We may start in a continuation line. If so, we have to
17663 get the right continuation_lines_width and current_x. */
17664 it.continuation_lines_width = last_row->continuation_lines_width;
17665 it.hpos = it.current_x = 0;
17667 /* Display the rest of the lines at the window end. */
17668 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17669 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17671 /* Is it always sure that the display agrees with lines in
17672 the current matrix? I don't think so, so we mark rows
17673 displayed invalid in the current matrix by setting their
17674 enabled_p flag to zero. */
17675 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17676 if (display_line (&it))
17677 last_text_row_at_end = it.glyph_row - 1;
17681 /* Update window_end_pos and window_end_vpos. */
17682 if (first_unchanged_at_end_row && !last_text_row_at_end)
17684 /* Window end line if one of the preserved rows from the current
17685 matrix. Set row to the last row displaying text in current
17686 matrix starting at first_unchanged_at_end_row, after
17687 scrolling. */
17688 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17689 row = find_last_row_displaying_text (w->current_matrix, &it,
17690 first_unchanged_at_end_row);
17691 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17692 adjust_window_ends (w, row, 1);
17693 eassert (w->window_end_bytepos >= 0);
17694 IF_DEBUG (debug_method_add (w, "A"));
17696 else if (last_text_row_at_end)
17698 adjust_window_ends (w, last_text_row_at_end, 0);
17699 eassert (w->window_end_bytepos >= 0);
17700 IF_DEBUG (debug_method_add (w, "B"));
17702 else if (last_text_row)
17704 /* We have displayed either to the end of the window or at the
17705 end of the window, i.e. the last row with text is to be found
17706 in the desired matrix. */
17707 adjust_window_ends (w, last_text_row, 0);
17708 eassert (w->window_end_bytepos >= 0);
17710 else if (first_unchanged_at_end_row == NULL
17711 && last_text_row == NULL
17712 && last_text_row_at_end == NULL)
17714 /* Displayed to end of window, but no line containing text was
17715 displayed. Lines were deleted at the end of the window. */
17716 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17717 int vpos = w->window_end_vpos;
17718 struct glyph_row *current_row = current_matrix->rows + vpos;
17719 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17721 for (row = NULL;
17722 row == NULL && vpos >= first_vpos;
17723 --vpos, --current_row, --desired_row)
17725 if (desired_row->enabled_p)
17727 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17728 row = desired_row;
17730 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17731 row = current_row;
17734 eassert (row != NULL);
17735 w->window_end_vpos = vpos + 1;
17736 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17737 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17738 eassert (w->window_end_bytepos >= 0);
17739 IF_DEBUG (debug_method_add (w, "C"));
17741 else
17742 emacs_abort ();
17744 IF_DEBUG (debug_end_pos = w->window_end_pos;
17745 debug_end_vpos = w->window_end_vpos);
17747 /* Record that display has not been completed. */
17748 w->window_end_valid = 0;
17749 w->desired_matrix->no_scrolling_p = 1;
17750 return 3;
17752 #undef GIVE_UP
17757 /***********************************************************************
17758 More debugging support
17759 ***********************************************************************/
17761 #ifdef GLYPH_DEBUG
17763 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17764 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17765 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17768 /* Dump the contents of glyph matrix MATRIX on stderr.
17770 GLYPHS 0 means don't show glyph contents.
17771 GLYPHS 1 means show glyphs in short form
17772 GLYPHS > 1 means show glyphs in long form. */
17774 void
17775 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17777 int i;
17778 for (i = 0; i < matrix->nrows; ++i)
17779 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17783 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17784 the glyph row and area where the glyph comes from. */
17786 void
17787 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17789 if (glyph->type == CHAR_GLYPH
17790 || glyph->type == GLYPHLESS_GLYPH)
17792 fprintf (stderr,
17793 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17794 glyph - row->glyphs[TEXT_AREA],
17795 (glyph->type == CHAR_GLYPH
17796 ? 'C'
17797 : 'G'),
17798 glyph->charpos,
17799 (BUFFERP (glyph->object)
17800 ? 'B'
17801 : (STRINGP (glyph->object)
17802 ? 'S'
17803 : (INTEGERP (glyph->object)
17804 ? '0'
17805 : '-'))),
17806 glyph->pixel_width,
17807 glyph->u.ch,
17808 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17809 ? glyph->u.ch
17810 : '.'),
17811 glyph->face_id,
17812 glyph->left_box_line_p,
17813 glyph->right_box_line_p);
17815 else if (glyph->type == STRETCH_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 'S',
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,
17831 ' ',
17832 glyph->face_id,
17833 glyph->left_box_line_p,
17834 glyph->right_box_line_p);
17836 else if (glyph->type == IMAGE_GLYPH)
17838 fprintf (stderr,
17839 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17840 glyph - row->glyphs[TEXT_AREA],
17841 'I',
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.img_id,
17852 '.',
17853 glyph->face_id,
17854 glyph->left_box_line_p,
17855 glyph->right_box_line_p);
17857 else if (glyph->type == COMPOSITE_GLYPH)
17859 fprintf (stderr,
17860 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17861 glyph - row->glyphs[TEXT_AREA],
17862 '+',
17863 glyph->charpos,
17864 (BUFFERP (glyph->object)
17865 ? 'B'
17866 : (STRINGP (glyph->object)
17867 ? 'S'
17868 : (INTEGERP (glyph->object)
17869 ? '0'
17870 : '-'))),
17871 glyph->pixel_width,
17872 glyph->u.cmp.id);
17873 if (glyph->u.cmp.automatic)
17874 fprintf (stderr,
17875 "[%d-%d]",
17876 glyph->slice.cmp.from, glyph->slice.cmp.to);
17877 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17878 glyph->face_id,
17879 glyph->left_box_line_p,
17880 glyph->right_box_line_p);
17885 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17886 GLYPHS 0 means don't show glyph contents.
17887 GLYPHS 1 means show glyphs in short form
17888 GLYPHS > 1 means show glyphs in long form. */
17890 void
17891 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17893 if (glyphs != 1)
17895 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17896 fprintf (stderr, "==============================================================================\n");
17898 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17899 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17900 vpos,
17901 MATRIX_ROW_START_CHARPOS (row),
17902 MATRIX_ROW_END_CHARPOS (row),
17903 row->used[TEXT_AREA],
17904 row->contains_overlapping_glyphs_p,
17905 row->enabled_p,
17906 row->truncated_on_left_p,
17907 row->truncated_on_right_p,
17908 row->continued_p,
17909 MATRIX_ROW_CONTINUATION_LINE_P (row),
17910 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17911 row->ends_at_zv_p,
17912 row->fill_line_p,
17913 row->ends_in_middle_of_char_p,
17914 row->starts_in_middle_of_char_p,
17915 row->mouse_face_p,
17916 row->x,
17917 row->y,
17918 row->pixel_width,
17919 row->height,
17920 row->visible_height,
17921 row->ascent,
17922 row->phys_ascent);
17923 /* The next 3 lines should align to "Start" in the header. */
17924 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17925 row->end.overlay_string_index,
17926 row->continuation_lines_width);
17927 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17928 CHARPOS (row->start.string_pos),
17929 CHARPOS (row->end.string_pos));
17930 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17931 row->end.dpvec_index);
17934 if (glyphs > 1)
17936 int area;
17938 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17940 struct glyph *glyph = row->glyphs[area];
17941 struct glyph *glyph_end = glyph + row->used[area];
17943 /* Glyph for a line end in text. */
17944 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17945 ++glyph_end;
17947 if (glyph < glyph_end)
17948 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17950 for (; glyph < glyph_end; ++glyph)
17951 dump_glyph (row, glyph, area);
17954 else if (glyphs == 1)
17956 int area;
17958 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17960 char *s = alloca (row->used[area] + 4);
17961 int i;
17963 for (i = 0; i < row->used[area]; ++i)
17965 struct glyph *glyph = row->glyphs[area] + i;
17966 if (i == row->used[area] - 1
17967 && area == TEXT_AREA
17968 && INTEGERP (glyph->object)
17969 && glyph->type == CHAR_GLYPH
17970 && glyph->u.ch == ' ')
17972 strcpy (&s[i], "[\\n]");
17973 i += 4;
17975 else if (glyph->type == CHAR_GLYPH
17976 && glyph->u.ch < 0x80
17977 && glyph->u.ch >= ' ')
17978 s[i] = glyph->u.ch;
17979 else
17980 s[i] = '.';
17983 s[i] = '\0';
17984 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17990 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17991 Sdump_glyph_matrix, 0, 1, "p",
17992 doc: /* Dump the current matrix of the selected window to stderr.
17993 Shows contents of glyph row structures. With non-nil
17994 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17995 glyphs in short form, otherwise show glyphs in long form. */)
17996 (Lisp_Object glyphs)
17998 struct window *w = XWINDOW (selected_window);
17999 struct buffer *buffer = XBUFFER (w->contents);
18001 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18002 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18003 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18004 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18005 fprintf (stderr, "=============================================\n");
18006 dump_glyph_matrix (w->current_matrix,
18007 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18008 return Qnil;
18012 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18013 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18014 (void)
18016 struct frame *f = XFRAME (selected_frame);
18017 dump_glyph_matrix (f->current_matrix, 1);
18018 return Qnil;
18022 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18023 doc: /* Dump glyph row ROW 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. */)
18027 (Lisp_Object row, Lisp_Object glyphs)
18029 struct glyph_matrix *matrix;
18030 EMACS_INT vpos;
18032 CHECK_NUMBER (row);
18033 matrix = XWINDOW (selected_window)->current_matrix;
18034 vpos = XINT (row);
18035 if (vpos >= 0 && vpos < matrix->nrows)
18036 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18037 vpos,
18038 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18039 return Qnil;
18043 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18044 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18045 GLYPH 0 means don't dump glyphs.
18046 GLYPH 1 means dump glyphs in short form.
18047 GLYPH > 1 or omitted means dump glyphs in long form.
18049 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18050 do nothing. */)
18051 (Lisp_Object row, Lisp_Object glyphs)
18053 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18054 struct frame *sf = SELECTED_FRAME ();
18055 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18056 EMACS_INT vpos;
18058 CHECK_NUMBER (row);
18059 vpos = XINT (row);
18060 if (vpos >= 0 && vpos < m->nrows)
18061 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18062 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18063 #endif
18064 return Qnil;
18068 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18069 doc: /* Toggle tracing of redisplay.
18070 With ARG, turn tracing on if and only if ARG is positive. */)
18071 (Lisp_Object arg)
18073 if (NILP (arg))
18074 trace_redisplay_p = !trace_redisplay_p;
18075 else
18077 arg = Fprefix_numeric_value (arg);
18078 trace_redisplay_p = XINT (arg) > 0;
18081 return Qnil;
18085 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18086 doc: /* Like `format', but print result to stderr.
18087 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18088 (ptrdiff_t nargs, Lisp_Object *args)
18090 Lisp_Object s = Fformat (nargs, args);
18091 fprintf (stderr, "%s", SDATA (s));
18092 return Qnil;
18095 #endif /* GLYPH_DEBUG */
18099 /***********************************************************************
18100 Building Desired Matrix Rows
18101 ***********************************************************************/
18103 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18104 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18106 static struct glyph_row *
18107 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18109 struct frame *f = XFRAME (WINDOW_FRAME (w));
18110 struct buffer *buffer = XBUFFER (w->contents);
18111 struct buffer *old = current_buffer;
18112 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18113 int arrow_len = SCHARS (overlay_arrow_string);
18114 const unsigned char *arrow_end = arrow_string + arrow_len;
18115 const unsigned char *p;
18116 struct it it;
18117 bool multibyte_p;
18118 int n_glyphs_before;
18120 set_buffer_temp (buffer);
18121 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18122 it.glyph_row->used[TEXT_AREA] = 0;
18123 SET_TEXT_POS (it.position, 0, 0);
18125 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18126 p = arrow_string;
18127 while (p < arrow_end)
18129 Lisp_Object face, ilisp;
18131 /* Get the next character. */
18132 if (multibyte_p)
18133 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18134 else
18136 it.c = it.char_to_display = *p, it.len = 1;
18137 if (! ASCII_CHAR_P (it.c))
18138 it.char_to_display = BYTE8_TO_CHAR (it.c);
18140 p += it.len;
18142 /* Get its face. */
18143 ilisp = make_number (p - arrow_string);
18144 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18145 it.face_id = compute_char_face (f, it.char_to_display, face);
18147 /* Compute its width, get its glyphs. */
18148 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18149 SET_TEXT_POS (it.position, -1, -1);
18150 PRODUCE_GLYPHS (&it);
18152 /* If this character doesn't fit any more in the line, we have
18153 to remove some glyphs. */
18154 if (it.current_x > it.last_visible_x)
18156 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18157 break;
18161 set_buffer_temp (old);
18162 return it.glyph_row;
18166 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18167 glyphs to insert is determined by produce_special_glyphs. */
18169 static void
18170 insert_left_trunc_glyphs (struct it *it)
18172 struct it truncate_it;
18173 struct glyph *from, *end, *to, *toend;
18175 eassert (!FRAME_WINDOW_P (it->f)
18176 || (!it->glyph_row->reversed_p
18177 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18178 || (it->glyph_row->reversed_p
18179 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18181 /* Get the truncation glyphs. */
18182 truncate_it = *it;
18183 truncate_it.current_x = 0;
18184 truncate_it.face_id = DEFAULT_FACE_ID;
18185 truncate_it.glyph_row = &scratch_glyph_row;
18186 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18187 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18188 truncate_it.object = make_number (0);
18189 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18191 /* Overwrite glyphs from IT with truncation glyphs. */
18192 if (!it->glyph_row->reversed_p)
18194 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18196 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18197 end = from + tused;
18198 to = it->glyph_row->glyphs[TEXT_AREA];
18199 toend = to + it->glyph_row->used[TEXT_AREA];
18200 if (FRAME_WINDOW_P (it->f))
18202 /* On GUI frames, when variable-size fonts are displayed,
18203 the truncation glyphs may need more pixels than the row's
18204 glyphs they overwrite. We overwrite more glyphs to free
18205 enough screen real estate, and enlarge the stretch glyph
18206 on the right (see display_line), if there is one, to
18207 preserve the screen position of the truncation glyphs on
18208 the right. */
18209 int w = 0;
18210 struct glyph *g = to;
18211 short used;
18213 /* The first glyph could be partially visible, in which case
18214 it->glyph_row->x will be negative. But we want the left
18215 truncation glyphs to be aligned at the left margin of the
18216 window, so we override the x coordinate at which the row
18217 will begin. */
18218 it->glyph_row->x = 0;
18219 while (g < toend && w < it->truncation_pixel_width)
18221 w += g->pixel_width;
18222 ++g;
18224 if (g - to - tused > 0)
18226 memmove (to + tused, g, (toend - g) * sizeof(*g));
18227 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18229 used = it->glyph_row->used[TEXT_AREA];
18230 if (it->glyph_row->truncated_on_right_p
18231 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18232 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18233 == STRETCH_GLYPH)
18235 int extra = w - it->truncation_pixel_width;
18237 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18241 while (from < end)
18242 *to++ = *from++;
18244 /* There may be padding glyphs left over. Overwrite them too. */
18245 if (!FRAME_WINDOW_P (it->f))
18247 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18249 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18250 while (from < end)
18251 *to++ = *from++;
18255 if (to > toend)
18256 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18258 else
18260 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18262 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18263 that back to front. */
18264 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18265 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18266 toend = it->glyph_row->glyphs[TEXT_AREA];
18267 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18268 if (FRAME_WINDOW_P (it->f))
18270 int w = 0;
18271 struct glyph *g = to;
18273 while (g >= toend && w < it->truncation_pixel_width)
18275 w += g->pixel_width;
18276 --g;
18278 if (to - g - tused > 0)
18279 to = g + tused;
18280 if (it->glyph_row->truncated_on_right_p
18281 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18282 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18284 int extra = w - it->truncation_pixel_width;
18286 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18290 while (from >= end && to >= toend)
18291 *to-- = *from--;
18292 if (!FRAME_WINDOW_P (it->f))
18294 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18296 from =
18297 truncate_it.glyph_row->glyphs[TEXT_AREA]
18298 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18299 while (from >= end && to >= toend)
18300 *to-- = *from--;
18303 if (from >= end)
18305 /* Need to free some room before prepending additional
18306 glyphs. */
18307 int move_by = from - end + 1;
18308 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18309 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18311 for ( ; g >= g0; g--)
18312 g[move_by] = *g;
18313 while (from >= end)
18314 *to-- = *from--;
18315 it->glyph_row->used[TEXT_AREA] += move_by;
18320 /* Compute the hash code for ROW. */
18321 unsigned
18322 row_hash (struct glyph_row *row)
18324 int area, k;
18325 unsigned hashval = 0;
18327 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18328 for (k = 0; k < row->used[area]; ++k)
18329 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18330 + row->glyphs[area][k].u.val
18331 + row->glyphs[area][k].face_id
18332 + row->glyphs[area][k].padding_p
18333 + (row->glyphs[area][k].type << 2));
18335 return hashval;
18338 /* Compute the pixel height and width of IT->glyph_row.
18340 Most of the time, ascent and height of a display line will be equal
18341 to the max_ascent and max_height values of the display iterator
18342 structure. This is not the case if
18344 1. We hit ZV without displaying anything. In this case, max_ascent
18345 and max_height will be zero.
18347 2. We have some glyphs that don't contribute to the line height.
18348 (The glyph row flag contributes_to_line_height_p is for future
18349 pixmap extensions).
18351 The first case is easily covered by using default values because in
18352 these cases, the line height does not really matter, except that it
18353 must not be zero. */
18355 static void
18356 compute_line_metrics (struct it *it)
18358 struct glyph_row *row = it->glyph_row;
18360 if (FRAME_WINDOW_P (it->f))
18362 int i, min_y, max_y;
18364 /* The line may consist of one space only, that was added to
18365 place the cursor on it. If so, the row's height hasn't been
18366 computed yet. */
18367 if (row->height == 0)
18369 if (it->max_ascent + it->max_descent == 0)
18370 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18371 row->ascent = it->max_ascent;
18372 row->height = it->max_ascent + it->max_descent;
18373 row->phys_ascent = it->max_phys_ascent;
18374 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18375 row->extra_line_spacing = it->max_extra_line_spacing;
18378 /* Compute the width of this line. */
18379 row->pixel_width = row->x;
18380 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18381 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18383 eassert (row->pixel_width >= 0);
18384 eassert (row->ascent >= 0 && row->height > 0);
18386 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18387 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18389 /* If first line's physical ascent is larger than its logical
18390 ascent, use the physical ascent, and make the row taller.
18391 This makes accented characters fully visible. */
18392 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18393 && row->phys_ascent > row->ascent)
18395 row->height += row->phys_ascent - row->ascent;
18396 row->ascent = row->phys_ascent;
18399 /* Compute how much of the line is visible. */
18400 row->visible_height = row->height;
18402 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18403 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18405 if (row->y < min_y)
18406 row->visible_height -= min_y - row->y;
18407 if (row->y + row->height > max_y)
18408 row->visible_height -= row->y + row->height - max_y;
18410 else
18412 row->pixel_width = row->used[TEXT_AREA];
18413 if (row->continued_p)
18414 row->pixel_width -= it->continuation_pixel_width;
18415 else if (row->truncated_on_right_p)
18416 row->pixel_width -= it->truncation_pixel_width;
18417 row->ascent = row->phys_ascent = 0;
18418 row->height = row->phys_height = row->visible_height = 1;
18419 row->extra_line_spacing = 0;
18422 /* Compute a hash code for this row. */
18423 row->hash = row_hash (row);
18425 it->max_ascent = it->max_descent = 0;
18426 it->max_phys_ascent = it->max_phys_descent = 0;
18430 /* Append one space to the glyph row of iterator IT if doing a
18431 window-based redisplay. The space has the same face as
18432 IT->face_id. Value is non-zero if a space was added.
18434 This function is called to make sure that there is always one glyph
18435 at the end of a glyph row that the cursor can be set on under
18436 window-systems. (If there weren't such a glyph we would not know
18437 how wide and tall a box cursor should be displayed).
18439 At the same time this space let's a nicely handle clearing to the
18440 end of the line if the row ends in italic text. */
18442 static int
18443 append_space_for_newline (struct it *it, int default_face_p)
18445 if (FRAME_WINDOW_P (it->f))
18447 int n = it->glyph_row->used[TEXT_AREA];
18449 if (it->glyph_row->glyphs[TEXT_AREA] + n
18450 < it->glyph_row->glyphs[1 + TEXT_AREA])
18452 /* Save some values that must not be changed.
18453 Must save IT->c and IT->len because otherwise
18454 ITERATOR_AT_END_P wouldn't work anymore after
18455 append_space_for_newline has been called. */
18456 enum display_element_type saved_what = it->what;
18457 int saved_c = it->c, saved_len = it->len;
18458 int saved_char_to_display = it->char_to_display;
18459 int saved_x = it->current_x;
18460 int saved_face_id = it->face_id;
18461 int saved_box_end = it->end_of_box_run_p;
18462 struct text_pos saved_pos;
18463 Lisp_Object saved_object;
18464 struct face *face;
18466 saved_object = it->object;
18467 saved_pos = it->position;
18469 it->what = IT_CHARACTER;
18470 memset (&it->position, 0, sizeof it->position);
18471 it->object = make_number (0);
18472 it->c = it->char_to_display = ' ';
18473 it->len = 1;
18475 /* If the default face was remapped, be sure to use the
18476 remapped face for the appended newline. */
18477 if (default_face_p)
18478 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18479 else if (it->face_before_selective_p)
18480 it->face_id = it->saved_face_id;
18481 face = FACE_FROM_ID (it->f, it->face_id);
18482 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18483 /* In R2L rows, we will prepend a stretch glyph that will
18484 have the end_of_box_run_p flag set for it, so there's no
18485 need for the appended newline glyph to have that flag
18486 set. */
18487 if (it->glyph_row->reversed_p
18488 /* But if the appended newline glyph goes all the way to
18489 the end of the row, there will be no stretch glyph,
18490 so leave the box flag set. */
18491 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18492 it->end_of_box_run_p = 0;
18494 PRODUCE_GLYPHS (it);
18496 it->override_ascent = -1;
18497 it->constrain_row_ascent_descent_p = 0;
18498 it->current_x = saved_x;
18499 it->object = saved_object;
18500 it->position = saved_pos;
18501 it->what = saved_what;
18502 it->face_id = saved_face_id;
18503 it->len = saved_len;
18504 it->c = saved_c;
18505 it->char_to_display = saved_char_to_display;
18506 it->end_of_box_run_p = saved_box_end;
18507 return 1;
18511 return 0;
18515 /* Extend the face of the last glyph in the text area of IT->glyph_row
18516 to the end of the display line. Called from display_line. If the
18517 glyph row is empty, add a space glyph to it so that we know the
18518 face to draw. Set the glyph row flag fill_line_p. If the glyph
18519 row is R2L, prepend a stretch glyph to cover the empty space to the
18520 left of the leftmost glyph. */
18522 static void
18523 extend_face_to_end_of_line (struct it *it)
18525 struct face *face, *default_face;
18526 struct frame *f = it->f;
18528 /* If line is already filled, do nothing. Non window-system frames
18529 get a grace of one more ``pixel'' because their characters are
18530 1-``pixel'' wide, so they hit the equality too early. This grace
18531 is needed only for R2L rows that are not continued, to produce
18532 one extra blank where we could display the cursor. */
18533 if (it->current_x >= it->last_visible_x
18534 + (!FRAME_WINDOW_P (f)
18535 && it->glyph_row->reversed_p
18536 && !it->glyph_row->continued_p))
18537 return;
18539 /* The default face, possibly remapped. */
18540 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18542 /* Face extension extends the background and box of IT->face_id
18543 to the end of the line. If the background equals the background
18544 of the frame, we don't have to do anything. */
18545 if (it->face_before_selective_p)
18546 face = FACE_FROM_ID (f, it->saved_face_id);
18547 else
18548 face = FACE_FROM_ID (f, it->face_id);
18550 if (FRAME_WINDOW_P (f)
18551 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18552 && face->box == FACE_NO_BOX
18553 && face->background == FRAME_BACKGROUND_PIXEL (f)
18554 #ifdef HAVE_WINDOW_SYSTEM
18555 && !face->stipple
18556 #endif
18557 && !it->glyph_row->reversed_p)
18558 return;
18560 /* Set the glyph row flag indicating that the face of the last glyph
18561 in the text area has to be drawn to the end of the text area. */
18562 it->glyph_row->fill_line_p = 1;
18564 /* If current character of IT is not ASCII, make sure we have the
18565 ASCII face. This will be automatically undone the next time
18566 get_next_display_element returns a multibyte character. Note
18567 that the character will always be single byte in unibyte
18568 text. */
18569 if (!ASCII_CHAR_P (it->c))
18571 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18574 if (FRAME_WINDOW_P (f))
18576 /* If the row is empty, add a space with the current face of IT,
18577 so that we know which face to draw. */
18578 if (it->glyph_row->used[TEXT_AREA] == 0)
18580 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18581 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18582 it->glyph_row->used[TEXT_AREA] = 1;
18584 #ifdef HAVE_WINDOW_SYSTEM
18585 if (it->glyph_row->reversed_p)
18587 /* Prepend a stretch glyph to the row, such that the
18588 rightmost glyph will be drawn flushed all the way to the
18589 right margin of the window. The stretch glyph that will
18590 occupy the empty space, if any, to the left of the
18591 glyphs. */
18592 struct font *font = face->font ? face->font : FRAME_FONT (f);
18593 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18594 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18595 struct glyph *g;
18596 int row_width, stretch_ascent, stretch_width;
18597 struct text_pos saved_pos;
18598 int saved_face_id, saved_avoid_cursor, saved_box_start;
18600 for (row_width = 0, g = row_start; g < row_end; g++)
18601 row_width += g->pixel_width;
18602 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18603 if (stretch_width > 0)
18605 stretch_ascent =
18606 (((it->ascent + it->descent)
18607 * FONT_BASE (font)) / FONT_HEIGHT (font));
18608 saved_pos = it->position;
18609 memset (&it->position, 0, sizeof it->position);
18610 saved_avoid_cursor = it->avoid_cursor_p;
18611 it->avoid_cursor_p = 1;
18612 saved_face_id = it->face_id;
18613 saved_box_start = it->start_of_box_run_p;
18614 /* The last row's stretch glyph should get the default
18615 face, to avoid painting the rest of the window with
18616 the region face, if the region ends at ZV. */
18617 if (it->glyph_row->ends_at_zv_p)
18618 it->face_id = default_face->id;
18619 else
18620 it->face_id = face->id;
18621 it->start_of_box_run_p = 0;
18622 append_stretch_glyph (it, make_number (0), stretch_width,
18623 it->ascent + it->descent, stretch_ascent);
18624 it->position = saved_pos;
18625 it->avoid_cursor_p = saved_avoid_cursor;
18626 it->face_id = saved_face_id;
18627 it->start_of_box_run_p = saved_box_start;
18630 #endif /* HAVE_WINDOW_SYSTEM */
18632 else
18634 /* Save some values that must not be changed. */
18635 int saved_x = it->current_x;
18636 struct text_pos saved_pos;
18637 Lisp_Object saved_object;
18638 enum display_element_type saved_what = it->what;
18639 int saved_face_id = it->face_id;
18641 saved_object = it->object;
18642 saved_pos = it->position;
18644 it->what = IT_CHARACTER;
18645 memset (&it->position, 0, sizeof it->position);
18646 it->object = make_number (0);
18647 it->c = it->char_to_display = ' ';
18648 it->len = 1;
18649 /* The last row's blank glyphs should get the default face, to
18650 avoid painting the rest of the window with the region face,
18651 if the region ends at ZV. */
18652 if (it->glyph_row->ends_at_zv_p)
18653 it->face_id = default_face->id;
18654 else
18655 it->face_id = face->id;
18657 PRODUCE_GLYPHS (it);
18659 while (it->current_x <= it->last_visible_x)
18660 PRODUCE_GLYPHS (it);
18662 /* Don't count these blanks really. It would let us insert a left
18663 truncation glyph below and make us set the cursor on them, maybe. */
18664 it->current_x = saved_x;
18665 it->object = saved_object;
18666 it->position = saved_pos;
18667 it->what = saved_what;
18668 it->face_id = saved_face_id;
18673 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18674 trailing whitespace. */
18676 static int
18677 trailing_whitespace_p (ptrdiff_t charpos)
18679 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18680 int c = 0;
18682 while (bytepos < ZV_BYTE
18683 && (c = FETCH_CHAR (bytepos),
18684 c == ' ' || c == '\t'))
18685 ++bytepos;
18687 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18689 if (bytepos != PT_BYTE)
18690 return 1;
18692 return 0;
18696 /* Highlight trailing whitespace, if any, in ROW. */
18698 static void
18699 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18701 int used = row->used[TEXT_AREA];
18703 if (used)
18705 struct glyph *start = row->glyphs[TEXT_AREA];
18706 struct glyph *glyph = start + used - 1;
18708 if (row->reversed_p)
18710 /* Right-to-left rows need to be processed in the opposite
18711 direction, so swap the edge pointers. */
18712 glyph = start;
18713 start = row->glyphs[TEXT_AREA] + used - 1;
18716 /* Skip over glyphs inserted to display the cursor at the
18717 end of a line, for extending the face of the last glyph
18718 to the end of the line on terminals, and for truncation
18719 and continuation glyphs. */
18720 if (!row->reversed_p)
18722 while (glyph >= start
18723 && glyph->type == CHAR_GLYPH
18724 && INTEGERP (glyph->object))
18725 --glyph;
18727 else
18729 while (glyph <= start
18730 && glyph->type == CHAR_GLYPH
18731 && INTEGERP (glyph->object))
18732 ++glyph;
18735 /* If last glyph is a space or stretch, and it's trailing
18736 whitespace, set the face of all trailing whitespace glyphs in
18737 IT->glyph_row to `trailing-whitespace'. */
18738 if ((row->reversed_p ? glyph <= start : glyph >= start)
18739 && BUFFERP (glyph->object)
18740 && (glyph->type == STRETCH_GLYPH
18741 || (glyph->type == CHAR_GLYPH
18742 && glyph->u.ch == ' '))
18743 && trailing_whitespace_p (glyph->charpos))
18745 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18746 if (face_id < 0)
18747 return;
18749 if (!row->reversed_p)
18751 while (glyph >= start
18752 && BUFFERP (glyph->object)
18753 && (glyph->type == STRETCH_GLYPH
18754 || (glyph->type == CHAR_GLYPH
18755 && glyph->u.ch == ' ')))
18756 (glyph--)->face_id = face_id;
18758 else
18760 while (glyph <= start
18761 && BUFFERP (glyph->object)
18762 && (glyph->type == STRETCH_GLYPH
18763 || (glyph->type == CHAR_GLYPH
18764 && glyph->u.ch == ' ')))
18765 (glyph++)->face_id = face_id;
18772 /* Value is non-zero if glyph row ROW should be
18773 considered to hold the buffer position CHARPOS. */
18775 static int
18776 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18778 int result = 1;
18780 if (charpos == CHARPOS (row->end.pos)
18781 || charpos == MATRIX_ROW_END_CHARPOS (row))
18783 /* Suppose the row ends on a string.
18784 Unless the row is continued, that means it ends on a newline
18785 in the string. If it's anything other than a display string
18786 (e.g., a before-string from an overlay), we don't want the
18787 cursor there. (This heuristic seems to give the optimal
18788 behavior for the various types of multi-line strings.)
18789 One exception: if the string has `cursor' property on one of
18790 its characters, we _do_ want the cursor there. */
18791 if (CHARPOS (row->end.string_pos) >= 0)
18793 if (row->continued_p)
18794 result = 1;
18795 else
18797 /* Check for `display' property. */
18798 struct glyph *beg = row->glyphs[TEXT_AREA];
18799 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18800 struct glyph *glyph;
18802 result = 0;
18803 for (glyph = end; glyph >= beg; --glyph)
18804 if (STRINGP (glyph->object))
18806 Lisp_Object prop
18807 = Fget_char_property (make_number (charpos),
18808 Qdisplay, Qnil);
18809 result =
18810 (!NILP (prop)
18811 && display_prop_string_p (prop, glyph->object));
18812 /* If there's a `cursor' property on one of the
18813 string's characters, this row is a cursor row,
18814 even though this is not a display string. */
18815 if (!result)
18817 Lisp_Object s = glyph->object;
18819 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18821 ptrdiff_t gpos = glyph->charpos;
18823 if (!NILP (Fget_char_property (make_number (gpos),
18824 Qcursor, s)))
18826 result = 1;
18827 break;
18831 break;
18835 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18837 /* If the row ends in middle of a real character,
18838 and the line is continued, we want the cursor here.
18839 That's because CHARPOS (ROW->end.pos) would equal
18840 PT if PT is before the character. */
18841 if (!row->ends_in_ellipsis_p)
18842 result = row->continued_p;
18843 else
18844 /* If the row ends in an ellipsis, then
18845 CHARPOS (ROW->end.pos) will equal point after the
18846 invisible text. We want that position to be displayed
18847 after the ellipsis. */
18848 result = 0;
18850 /* If the row ends at ZV, display the cursor at the end of that
18851 row instead of at the start of the row below. */
18852 else if (row->ends_at_zv_p)
18853 result = 1;
18854 else
18855 result = 0;
18858 return result;
18861 /* Value is non-zero if glyph row ROW should be
18862 used to hold the cursor. */
18864 static int
18865 cursor_row_p (struct glyph_row *row)
18867 return row_for_charpos_p (row, PT);
18872 /* Push the property PROP so that it will be rendered at the current
18873 position in IT. Return 1 if PROP was successfully pushed, 0
18874 otherwise. Called from handle_line_prefix to handle the
18875 `line-prefix' and `wrap-prefix' properties. */
18877 static int
18878 push_prefix_prop (struct it *it, Lisp_Object prop)
18880 struct text_pos pos =
18881 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18883 eassert (it->method == GET_FROM_BUFFER
18884 || it->method == GET_FROM_DISPLAY_VECTOR
18885 || it->method == GET_FROM_STRING);
18887 /* We need to save the current buffer/string position, so it will be
18888 restored by pop_it, because iterate_out_of_display_property
18889 depends on that being set correctly, but some situations leave
18890 it->position not yet set when this function is called. */
18891 push_it (it, &pos);
18893 if (STRINGP (prop))
18895 if (SCHARS (prop) == 0)
18897 pop_it (it);
18898 return 0;
18901 it->string = prop;
18902 it->string_from_prefix_prop_p = 1;
18903 it->multibyte_p = STRING_MULTIBYTE (it->string);
18904 it->current.overlay_string_index = -1;
18905 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18906 it->end_charpos = it->string_nchars = SCHARS (it->string);
18907 it->method = GET_FROM_STRING;
18908 it->stop_charpos = 0;
18909 it->prev_stop = 0;
18910 it->base_level_stop = 0;
18912 /* Force paragraph direction to be that of the parent
18913 buffer/string. */
18914 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18915 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18916 else
18917 it->paragraph_embedding = L2R;
18919 /* Set up the bidi iterator for this display string. */
18920 if (it->bidi_p)
18922 it->bidi_it.string.lstring = it->string;
18923 it->bidi_it.string.s = NULL;
18924 it->bidi_it.string.schars = it->end_charpos;
18925 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18926 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18927 it->bidi_it.string.unibyte = !it->multibyte_p;
18928 it->bidi_it.w = it->w;
18929 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18932 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18934 it->method = GET_FROM_STRETCH;
18935 it->object = prop;
18937 #ifdef HAVE_WINDOW_SYSTEM
18938 else if (IMAGEP (prop))
18940 it->what = IT_IMAGE;
18941 it->image_id = lookup_image (it->f, prop);
18942 it->method = GET_FROM_IMAGE;
18944 #endif /* HAVE_WINDOW_SYSTEM */
18945 else
18947 pop_it (it); /* bogus display property, give up */
18948 return 0;
18951 return 1;
18954 /* Return the character-property PROP at the current position in IT. */
18956 static Lisp_Object
18957 get_it_property (struct it *it, Lisp_Object prop)
18959 Lisp_Object position, object = it->object;
18961 if (STRINGP (object))
18962 position = make_number (IT_STRING_CHARPOS (*it));
18963 else if (BUFFERP (object))
18965 position = make_number (IT_CHARPOS (*it));
18966 object = it->window;
18968 else
18969 return Qnil;
18971 return Fget_char_property (position, prop, object);
18974 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18976 static void
18977 handle_line_prefix (struct it *it)
18979 Lisp_Object prefix;
18981 if (it->continuation_lines_width > 0)
18983 prefix = get_it_property (it, Qwrap_prefix);
18984 if (NILP (prefix))
18985 prefix = Vwrap_prefix;
18987 else
18989 prefix = get_it_property (it, Qline_prefix);
18990 if (NILP (prefix))
18991 prefix = Vline_prefix;
18993 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18995 /* If the prefix is wider than the window, and we try to wrap
18996 it, it would acquire its own wrap prefix, and so on till the
18997 iterator stack overflows. So, don't wrap the prefix. */
18998 it->line_wrap = TRUNCATE;
18999 it->avoid_cursor_p = 1;
19005 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19006 only for R2L lines from display_line and display_string, when they
19007 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19008 the line/string needs to be continued on the next glyph row. */
19009 static void
19010 unproduce_glyphs (struct it *it, int n)
19012 struct glyph *glyph, *end;
19014 eassert (it->glyph_row);
19015 eassert (it->glyph_row->reversed_p);
19016 eassert (it->area == TEXT_AREA);
19017 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19019 if (n > it->glyph_row->used[TEXT_AREA])
19020 n = it->glyph_row->used[TEXT_AREA];
19021 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19022 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19023 for ( ; glyph < end; glyph++)
19024 glyph[-n] = *glyph;
19027 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19028 and ROW->maxpos. */
19029 static void
19030 find_row_edges (struct it *it, struct glyph_row *row,
19031 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19032 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19034 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19035 lines' rows is implemented for bidi-reordered rows. */
19037 /* ROW->minpos is the value of min_pos, the minimal buffer position
19038 we have in ROW, or ROW->start.pos if that is smaller. */
19039 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19040 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19041 else
19042 /* We didn't find buffer positions smaller than ROW->start, or
19043 didn't find _any_ valid buffer positions in any of the glyphs,
19044 so we must trust the iterator's computed positions. */
19045 row->minpos = row->start.pos;
19046 if (max_pos <= 0)
19048 max_pos = CHARPOS (it->current.pos);
19049 max_bpos = BYTEPOS (it->current.pos);
19052 /* Here are the various use-cases for ending the row, and the
19053 corresponding values for ROW->maxpos:
19055 Line ends in a newline from buffer eol_pos + 1
19056 Line is continued from buffer max_pos + 1
19057 Line is truncated on right it->current.pos
19058 Line ends in a newline from string max_pos + 1(*)
19059 (*) + 1 only when line ends in a forward scan
19060 Line is continued from string max_pos
19061 Line is continued from display vector max_pos
19062 Line is entirely from a string min_pos == max_pos
19063 Line is entirely from a display vector min_pos == max_pos
19064 Line that ends at ZV ZV
19066 If you discover other use-cases, please add them here as
19067 appropriate. */
19068 if (row->ends_at_zv_p)
19069 row->maxpos = it->current.pos;
19070 else if (row->used[TEXT_AREA])
19072 int seen_this_string = 0;
19073 struct glyph_row *r1 = row - 1;
19075 /* Did we see the same display string on the previous row? */
19076 if (STRINGP (it->object)
19077 /* this is not the first row */
19078 && row > it->w->desired_matrix->rows
19079 /* previous row is not the header line */
19080 && !r1->mode_line_p
19081 /* previous row also ends in a newline from a string */
19082 && r1->ends_in_newline_from_string_p)
19084 struct glyph *start, *end;
19086 /* Search for the last glyph of the previous row that came
19087 from buffer or string. Depending on whether the row is
19088 L2R or R2L, we need to process it front to back or the
19089 other way round. */
19090 if (!r1->reversed_p)
19092 start = r1->glyphs[TEXT_AREA];
19093 end = start + r1->used[TEXT_AREA];
19094 /* Glyphs inserted by redisplay have an integer (zero)
19095 as their object. */
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 /* If all the glyphs of the previous row were inserted
19107 by redisplay, it means the previous row was
19108 produced from a single newline, which is only
19109 possible if that newline came from the same string
19110 as the one which produced this ROW. */
19111 seen_this_string = 1;
19113 else
19115 end = r1->glyphs[TEXT_AREA] - 1;
19116 start = end + r1->used[TEXT_AREA];
19117 while (end < start
19118 && INTEGERP ((end + 1)->object)
19119 && (end + 1)->charpos <= 0)
19120 ++end;
19121 if (end < start)
19123 if (EQ ((end + 1)->object, it->object))
19124 seen_this_string = 1;
19126 else
19127 seen_this_string = 1;
19130 /* Take note of each display string that covers a newline only
19131 once, the first time we see it. This is for when a display
19132 string includes more than one newline in it. */
19133 if (row->ends_in_newline_from_string_p && !seen_this_string)
19135 /* If we were scanning the buffer forward when we displayed
19136 the string, we want to account for at least one buffer
19137 position that belongs to this row (position covered by
19138 the display string), so that cursor positioning will
19139 consider this row as a candidate when point is at the end
19140 of the visual line represented by this row. This is not
19141 required when scanning back, because max_pos will already
19142 have a much larger value. */
19143 if (CHARPOS (row->end.pos) > max_pos)
19144 INC_BOTH (max_pos, max_bpos);
19145 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19147 else if (CHARPOS (it->eol_pos) > 0)
19148 SET_TEXT_POS (row->maxpos,
19149 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19150 else if (row->continued_p)
19152 /* If max_pos is different from IT's current position, it
19153 means IT->method does not belong to the display element
19154 at max_pos. However, it also means that the display
19155 element at max_pos was displayed in its entirety on this
19156 line, which is equivalent to saying that the next line
19157 starts at the next buffer position. */
19158 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19159 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19160 else
19162 INC_BOTH (max_pos, max_bpos);
19163 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19166 else if (row->truncated_on_right_p)
19167 /* display_line already called reseat_at_next_visible_line_start,
19168 which puts the iterator at the beginning of the next line, in
19169 the logical order. */
19170 row->maxpos = it->current.pos;
19171 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19172 /* A line that is entirely from a string/image/stretch... */
19173 row->maxpos = row->minpos;
19174 else
19175 emacs_abort ();
19177 else
19178 row->maxpos = it->current.pos;
19181 /* Construct the glyph row IT->glyph_row in the desired matrix of
19182 IT->w from text at the current position of IT. See dispextern.h
19183 for an overview of struct it. Value is non-zero if
19184 IT->glyph_row displays text, as opposed to a line displaying ZV
19185 only. */
19187 static int
19188 display_line (struct it *it)
19190 struct glyph_row *row = it->glyph_row;
19191 Lisp_Object overlay_arrow_string;
19192 struct it wrap_it;
19193 void *wrap_data = NULL;
19194 int may_wrap = 0, wrap_x IF_LINT (= 0);
19195 int wrap_row_used = -1;
19196 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19197 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19198 int wrap_row_extra_line_spacing IF_LINT (= 0);
19199 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19200 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19201 int cvpos;
19202 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19203 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19205 /* We always start displaying at hpos zero even if hscrolled. */
19206 eassert (it->hpos == 0 && it->current_x == 0);
19208 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19209 >= it->w->desired_matrix->nrows)
19211 it->w->nrows_scale_factor++;
19212 it->f->fonts_changed = 1;
19213 return 0;
19216 /* Clear the result glyph row and enable it. */
19217 prepare_desired_row (row);
19219 row->y = it->current_y;
19220 row->start = it->start;
19221 row->continuation_lines_width = it->continuation_lines_width;
19222 row->displays_text_p = 1;
19223 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19224 it->starts_in_middle_of_char_p = 0;
19226 /* Arrange the overlays nicely for our purposes. Usually, we call
19227 display_line on only one line at a time, in which case this
19228 can't really hurt too much, or we call it on lines which appear
19229 one after another in the buffer, in which case all calls to
19230 recenter_overlay_lists but the first will be pretty cheap. */
19231 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19233 /* Move over display elements that are not visible because we are
19234 hscrolled. This may stop at an x-position < IT->first_visible_x
19235 if the first glyph is partially visible or if we hit a line end. */
19236 if (it->current_x < it->first_visible_x)
19238 enum move_it_result move_result;
19240 this_line_min_pos = row->start.pos;
19241 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19242 MOVE_TO_POS | MOVE_TO_X);
19243 /* If we are under a large hscroll, move_it_in_display_line_to
19244 could hit the end of the line without reaching
19245 it->first_visible_x. Pretend that we did reach it. This is
19246 especially important on a TTY, where we will call
19247 extend_face_to_end_of_line, which needs to know how many
19248 blank glyphs to produce. */
19249 if (it->current_x < it->first_visible_x
19250 && (move_result == MOVE_NEWLINE_OR_CR
19251 || move_result == MOVE_POS_MATCH_OR_ZV))
19252 it->current_x = it->first_visible_x;
19254 /* Record the smallest positions seen while we moved over
19255 display elements that are not visible. This is needed by
19256 redisplay_internal for optimizing the case where the cursor
19257 stays inside the same line. The rest of this function only
19258 considers positions that are actually displayed, so
19259 RECORD_MAX_MIN_POS will not otherwise record positions that
19260 are hscrolled to the left of the left edge of the window. */
19261 min_pos = CHARPOS (this_line_min_pos);
19262 min_bpos = BYTEPOS (this_line_min_pos);
19264 else
19266 /* We only do this when not calling `move_it_in_display_line_to'
19267 above, because move_it_in_display_line_to calls
19268 handle_line_prefix itself. */
19269 handle_line_prefix (it);
19272 /* Get the initial row height. This is either the height of the
19273 text hscrolled, if there is any, or zero. */
19274 row->ascent = it->max_ascent;
19275 row->height = it->max_ascent + it->max_descent;
19276 row->phys_ascent = it->max_phys_ascent;
19277 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19278 row->extra_line_spacing = it->max_extra_line_spacing;
19280 /* Utility macro to record max and min buffer positions seen until now. */
19281 #define RECORD_MAX_MIN_POS(IT) \
19282 do \
19284 int composition_p = !STRINGP ((IT)->string) \
19285 && ((IT)->what == IT_COMPOSITION); \
19286 ptrdiff_t current_pos = \
19287 composition_p ? (IT)->cmp_it.charpos \
19288 : IT_CHARPOS (*(IT)); \
19289 ptrdiff_t current_bpos = \
19290 composition_p ? CHAR_TO_BYTE (current_pos) \
19291 : IT_BYTEPOS (*(IT)); \
19292 if (current_pos < min_pos) \
19294 min_pos = current_pos; \
19295 min_bpos = current_bpos; \
19297 if (IT_CHARPOS (*it) > max_pos) \
19299 max_pos = IT_CHARPOS (*it); \
19300 max_bpos = IT_BYTEPOS (*it); \
19303 while (0)
19305 /* Loop generating characters. The loop is left with IT on the next
19306 character to display. */
19307 while (1)
19309 int n_glyphs_before, hpos_before, x_before;
19310 int x, nglyphs;
19311 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19313 /* Retrieve the next thing to display. Value is zero if end of
19314 buffer reached. */
19315 if (!get_next_display_element (it))
19317 /* Maybe add a space at the end of this line that is used to
19318 display the cursor there under X. Set the charpos of the
19319 first glyph of blank lines not corresponding to any text
19320 to -1. */
19321 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19322 row->exact_window_width_line_p = 1;
19323 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19324 || row->used[TEXT_AREA] == 0)
19326 row->glyphs[TEXT_AREA]->charpos = -1;
19327 row->displays_text_p = 0;
19329 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19330 && (!MINI_WINDOW_P (it->w)
19331 || (minibuf_level && EQ (it->window, minibuf_window))))
19332 row->indicate_empty_line_p = 1;
19335 it->continuation_lines_width = 0;
19336 row->ends_at_zv_p = 1;
19337 /* A row that displays right-to-left text must always have
19338 its last face extended all the way to the end of line,
19339 even if this row ends in ZV, because we still write to
19340 the screen left to right. We also need to extend the
19341 last face if the default face is remapped to some
19342 different face, otherwise the functions that clear
19343 portions of the screen will clear with the default face's
19344 background color. */
19345 if (row->reversed_p
19346 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19347 extend_face_to_end_of_line (it);
19348 break;
19351 /* Now, get the metrics of what we want to display. This also
19352 generates glyphs in `row' (which is IT->glyph_row). */
19353 n_glyphs_before = row->used[TEXT_AREA];
19354 x = it->current_x;
19356 /* Remember the line height so far in case the next element doesn't
19357 fit on the line. */
19358 if (it->line_wrap != TRUNCATE)
19360 ascent = it->max_ascent;
19361 descent = it->max_descent;
19362 phys_ascent = it->max_phys_ascent;
19363 phys_descent = it->max_phys_descent;
19365 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19367 if (IT_DISPLAYING_WHITESPACE (it))
19368 may_wrap = 1;
19369 else if (may_wrap)
19371 SAVE_IT (wrap_it, *it, wrap_data);
19372 wrap_x = x;
19373 wrap_row_used = row->used[TEXT_AREA];
19374 wrap_row_ascent = row->ascent;
19375 wrap_row_height = row->height;
19376 wrap_row_phys_ascent = row->phys_ascent;
19377 wrap_row_phys_height = row->phys_height;
19378 wrap_row_extra_line_spacing = row->extra_line_spacing;
19379 wrap_row_min_pos = min_pos;
19380 wrap_row_min_bpos = min_bpos;
19381 wrap_row_max_pos = max_pos;
19382 wrap_row_max_bpos = max_bpos;
19383 may_wrap = 0;
19388 PRODUCE_GLYPHS (it);
19390 /* If this display element was in marginal areas, continue with
19391 the next one. */
19392 if (it->area != TEXT_AREA)
19394 row->ascent = max (row->ascent, it->max_ascent);
19395 row->height = max (row->height, it->max_ascent + it->max_descent);
19396 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19397 row->phys_height = max (row->phys_height,
19398 it->max_phys_ascent + it->max_phys_descent);
19399 row->extra_line_spacing = max (row->extra_line_spacing,
19400 it->max_extra_line_spacing);
19401 set_iterator_to_next (it, 1);
19402 continue;
19405 /* Does the display element fit on the line? If we truncate
19406 lines, we should draw past the right edge of the window. If
19407 we don't truncate, we want to stop so that we can display the
19408 continuation glyph before the right margin. If lines are
19409 continued, there are two possible strategies for characters
19410 resulting in more than 1 glyph (e.g. tabs): Display as many
19411 glyphs as possible in this line and leave the rest for the
19412 continuation line, or display the whole element in the next
19413 line. Original redisplay did the former, so we do it also. */
19414 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19415 hpos_before = it->hpos;
19416 x_before = x;
19418 if (/* Not a newline. */
19419 nglyphs > 0
19420 /* Glyphs produced fit entirely in the line. */
19421 && it->current_x < it->last_visible_x)
19423 it->hpos += nglyphs;
19424 row->ascent = max (row->ascent, it->max_ascent);
19425 row->height = max (row->height, it->max_ascent + it->max_descent);
19426 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19427 row->phys_height = max (row->phys_height,
19428 it->max_phys_ascent + it->max_phys_descent);
19429 row->extra_line_spacing = max (row->extra_line_spacing,
19430 it->max_extra_line_spacing);
19431 if (it->current_x - it->pixel_width < it->first_visible_x)
19432 row->x = x - it->first_visible_x;
19433 /* Record the maximum and minimum buffer positions seen so
19434 far in glyphs that will be displayed by this row. */
19435 if (it->bidi_p)
19436 RECORD_MAX_MIN_POS (it);
19438 else
19440 int i, new_x;
19441 struct glyph *glyph;
19443 for (i = 0; i < nglyphs; ++i, x = new_x)
19445 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19446 new_x = x + glyph->pixel_width;
19448 if (/* Lines are continued. */
19449 it->line_wrap != TRUNCATE
19450 && (/* Glyph doesn't fit on the line. */
19451 new_x > it->last_visible_x
19452 /* Or it fits exactly on a window system frame. */
19453 || (new_x == it->last_visible_x
19454 && FRAME_WINDOW_P (it->f)
19455 && (row->reversed_p
19456 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19457 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19459 /* End of a continued line. */
19461 if (it->hpos == 0
19462 || (new_x == it->last_visible_x
19463 && FRAME_WINDOW_P (it->f)
19464 && (row->reversed_p
19465 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19466 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19468 /* Current glyph is the only one on the line or
19469 fits exactly on the line. We must continue
19470 the line because we can't draw the cursor
19471 after the glyph. */
19472 row->continued_p = 1;
19473 it->current_x = new_x;
19474 it->continuation_lines_width += new_x;
19475 ++it->hpos;
19476 if (i == nglyphs - 1)
19478 /* If line-wrap is on, check if a previous
19479 wrap point was found. */
19480 if (wrap_row_used > 0
19481 /* Even if there is a previous wrap
19482 point, continue the line here as
19483 usual, if (i) the previous character
19484 was a space or tab AND (ii) the
19485 current character is not. */
19486 && (!may_wrap
19487 || IT_DISPLAYING_WHITESPACE (it)))
19488 goto back_to_wrap;
19490 /* Record the maximum and minimum buffer
19491 positions seen so far in glyphs that will be
19492 displayed by this row. */
19493 if (it->bidi_p)
19494 RECORD_MAX_MIN_POS (it);
19495 set_iterator_to_next (it, 1);
19496 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19498 if (!get_next_display_element (it))
19500 row->exact_window_width_line_p = 1;
19501 it->continuation_lines_width = 0;
19502 row->continued_p = 0;
19503 row->ends_at_zv_p = 1;
19505 else if (ITERATOR_AT_END_OF_LINE_P (it))
19507 row->continued_p = 0;
19508 row->exact_window_width_line_p = 1;
19512 else if (it->bidi_p)
19513 RECORD_MAX_MIN_POS (it);
19515 else if (CHAR_GLYPH_PADDING_P (*glyph)
19516 && !FRAME_WINDOW_P (it->f))
19518 /* A padding glyph that doesn't fit on this line.
19519 This means the whole character doesn't fit
19520 on the line. */
19521 if (row->reversed_p)
19522 unproduce_glyphs (it, row->used[TEXT_AREA]
19523 - n_glyphs_before);
19524 row->used[TEXT_AREA] = n_glyphs_before;
19526 /* Fill the rest of the row with continuation
19527 glyphs like in 20.x. */
19528 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19529 < row->glyphs[1 + TEXT_AREA])
19530 produce_special_glyphs (it, IT_CONTINUATION);
19532 row->continued_p = 1;
19533 it->current_x = x_before;
19534 it->continuation_lines_width += x_before;
19536 /* Restore the height to what it was before the
19537 element not fitting on the line. */
19538 it->max_ascent = ascent;
19539 it->max_descent = descent;
19540 it->max_phys_ascent = phys_ascent;
19541 it->max_phys_descent = phys_descent;
19543 else if (wrap_row_used > 0)
19545 back_to_wrap:
19546 if (row->reversed_p)
19547 unproduce_glyphs (it,
19548 row->used[TEXT_AREA] - wrap_row_used);
19549 RESTORE_IT (it, &wrap_it, wrap_data);
19550 it->continuation_lines_width += wrap_x;
19551 row->used[TEXT_AREA] = wrap_row_used;
19552 row->ascent = wrap_row_ascent;
19553 row->height = wrap_row_height;
19554 row->phys_ascent = wrap_row_phys_ascent;
19555 row->phys_height = wrap_row_phys_height;
19556 row->extra_line_spacing = wrap_row_extra_line_spacing;
19557 min_pos = wrap_row_min_pos;
19558 min_bpos = wrap_row_min_bpos;
19559 max_pos = wrap_row_max_pos;
19560 max_bpos = wrap_row_max_bpos;
19561 row->continued_p = 1;
19562 row->ends_at_zv_p = 0;
19563 row->exact_window_width_line_p = 0;
19564 it->continuation_lines_width += x;
19566 /* Make sure that a non-default face is extended
19567 up to the right margin of the window. */
19568 extend_face_to_end_of_line (it);
19570 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19572 /* A TAB that extends past the right edge of the
19573 window. This produces a single glyph on
19574 window system frames. We leave the glyph in
19575 this row and let it fill the row, but don't
19576 consume the TAB. */
19577 if ((row->reversed_p
19578 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19579 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19580 produce_special_glyphs (it, IT_CONTINUATION);
19581 it->continuation_lines_width += it->last_visible_x;
19582 row->ends_in_middle_of_char_p = 1;
19583 row->continued_p = 1;
19584 glyph->pixel_width = it->last_visible_x - x;
19585 it->starts_in_middle_of_char_p = 1;
19587 else
19589 /* Something other than a TAB that draws past
19590 the right edge of the window. Restore
19591 positions to values before the element. */
19592 if (row->reversed_p)
19593 unproduce_glyphs (it, row->used[TEXT_AREA]
19594 - (n_glyphs_before + i));
19595 row->used[TEXT_AREA] = n_glyphs_before + i;
19597 /* Display continuation glyphs. */
19598 it->current_x = x_before;
19599 it->continuation_lines_width += x;
19600 if (!FRAME_WINDOW_P (it->f)
19601 || (row->reversed_p
19602 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19603 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19604 produce_special_glyphs (it, IT_CONTINUATION);
19605 row->continued_p = 1;
19607 extend_face_to_end_of_line (it);
19609 if (nglyphs > 1 && i > 0)
19611 row->ends_in_middle_of_char_p = 1;
19612 it->starts_in_middle_of_char_p = 1;
19615 /* Restore the height to what it was before the
19616 element not fitting on the line. */
19617 it->max_ascent = ascent;
19618 it->max_descent = descent;
19619 it->max_phys_ascent = phys_ascent;
19620 it->max_phys_descent = phys_descent;
19623 break;
19625 else if (new_x > it->first_visible_x)
19627 /* Increment number of glyphs actually displayed. */
19628 ++it->hpos;
19630 /* Record the maximum and minimum buffer positions
19631 seen so far in glyphs that will be displayed by
19632 this row. */
19633 if (it->bidi_p)
19634 RECORD_MAX_MIN_POS (it);
19636 if (x < it->first_visible_x)
19637 /* Glyph is partially visible, i.e. row starts at
19638 negative X position. */
19639 row->x = x - it->first_visible_x;
19641 else
19643 /* Glyph is completely off the left margin of the
19644 window. This should not happen because of the
19645 move_it_in_display_line at the start of this
19646 function, unless the text display area of the
19647 window is empty. */
19648 eassert (it->first_visible_x <= it->last_visible_x);
19651 /* Even if this display element produced no glyphs at all,
19652 we want to record its position. */
19653 if (it->bidi_p && nglyphs == 0)
19654 RECORD_MAX_MIN_POS (it);
19656 row->ascent = max (row->ascent, it->max_ascent);
19657 row->height = max (row->height, it->max_ascent + it->max_descent);
19658 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19659 row->phys_height = max (row->phys_height,
19660 it->max_phys_ascent + it->max_phys_descent);
19661 row->extra_line_spacing = max (row->extra_line_spacing,
19662 it->max_extra_line_spacing);
19664 /* End of this display line if row is continued. */
19665 if (row->continued_p || row->ends_at_zv_p)
19666 break;
19669 at_end_of_line:
19670 /* Is this a line end? If yes, we're also done, after making
19671 sure that a non-default face is extended up to the right
19672 margin of the window. */
19673 if (ITERATOR_AT_END_OF_LINE_P (it))
19675 int used_before = row->used[TEXT_AREA];
19677 row->ends_in_newline_from_string_p = STRINGP (it->object);
19679 /* Add a space at the end of the line that is used to
19680 display the cursor there. */
19681 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19682 append_space_for_newline (it, 0);
19684 /* Extend the face to the end of the line. */
19685 extend_face_to_end_of_line (it);
19687 /* Make sure we have the position. */
19688 if (used_before == 0)
19689 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19691 /* Record the position of the newline, for use in
19692 find_row_edges. */
19693 it->eol_pos = it->current.pos;
19695 /* Consume the line end. This skips over invisible lines. */
19696 set_iterator_to_next (it, 1);
19697 it->continuation_lines_width = 0;
19698 break;
19701 /* Proceed with next display element. Note that this skips
19702 over lines invisible because of selective display. */
19703 set_iterator_to_next (it, 1);
19705 /* If we truncate lines, we are done when the last displayed
19706 glyphs reach past the right margin of the window. */
19707 if (it->line_wrap == TRUNCATE
19708 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19709 ? (it->current_x >= it->last_visible_x)
19710 : (it->current_x > it->last_visible_x)))
19712 /* Maybe add truncation glyphs. */
19713 if (!FRAME_WINDOW_P (it->f)
19714 || (row->reversed_p
19715 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19716 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19718 int i, n;
19720 if (!row->reversed_p)
19722 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19723 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19724 break;
19726 else
19728 for (i = 0; i < row->used[TEXT_AREA]; i++)
19729 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19730 break;
19731 /* Remove any padding glyphs at the front of ROW, to
19732 make room for the truncation glyphs we will be
19733 adding below. The loop below always inserts at
19734 least one truncation glyph, so also remove the
19735 last glyph added to ROW. */
19736 unproduce_glyphs (it, i + 1);
19737 /* Adjust i for the loop below. */
19738 i = row->used[TEXT_AREA] - (i + 1);
19741 it->current_x = x_before;
19742 if (!FRAME_WINDOW_P (it->f))
19744 for (n = row->used[TEXT_AREA]; i < n; ++i)
19746 row->used[TEXT_AREA] = i;
19747 produce_special_glyphs (it, IT_TRUNCATION);
19750 else
19752 row->used[TEXT_AREA] = i;
19753 produce_special_glyphs (it, IT_TRUNCATION);
19756 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19758 /* Don't truncate if we can overflow newline into fringe. */
19759 if (!get_next_display_element (it))
19761 it->continuation_lines_width = 0;
19762 row->ends_at_zv_p = 1;
19763 row->exact_window_width_line_p = 1;
19764 break;
19766 if (ITERATOR_AT_END_OF_LINE_P (it))
19768 row->exact_window_width_line_p = 1;
19769 goto at_end_of_line;
19771 it->current_x = x_before;
19774 row->truncated_on_right_p = 1;
19775 it->continuation_lines_width = 0;
19776 reseat_at_next_visible_line_start (it, 0);
19777 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19778 it->hpos = hpos_before;
19779 break;
19783 if (wrap_data)
19784 bidi_unshelve_cache (wrap_data, 1);
19786 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19787 at the left window margin. */
19788 if (it->first_visible_x
19789 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19791 if (!FRAME_WINDOW_P (it->f)
19792 || (row->reversed_p
19793 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19794 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19795 insert_left_trunc_glyphs (it);
19796 row->truncated_on_left_p = 1;
19799 /* Remember the position at which this line ends.
19801 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19802 cannot be before the call to find_row_edges below, since that is
19803 where these positions are determined. */
19804 row->end = it->current;
19805 if (!it->bidi_p)
19807 row->minpos = row->start.pos;
19808 row->maxpos = row->end.pos;
19810 else
19812 /* ROW->minpos and ROW->maxpos must be the smallest and
19813 `1 + the largest' buffer positions in ROW. But if ROW was
19814 bidi-reordered, these two positions can be anywhere in the
19815 row, so we must determine them now. */
19816 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19819 /* If the start of this line is the overlay arrow-position, then
19820 mark this glyph row as the one containing the overlay arrow.
19821 This is clearly a mess with variable size fonts. It would be
19822 better to let it be displayed like cursors under X. */
19823 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19824 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19825 !NILP (overlay_arrow_string)))
19827 /* Overlay arrow in window redisplay is a fringe bitmap. */
19828 if (STRINGP (overlay_arrow_string))
19830 struct glyph_row *arrow_row
19831 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19832 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19833 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19834 struct glyph *p = row->glyphs[TEXT_AREA];
19835 struct glyph *p2, *end;
19837 /* Copy the arrow glyphs. */
19838 while (glyph < arrow_end)
19839 *p++ = *glyph++;
19841 /* Throw away padding glyphs. */
19842 p2 = p;
19843 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19844 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19845 ++p2;
19846 if (p2 > p)
19848 while (p2 < end)
19849 *p++ = *p2++;
19850 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19853 else
19855 eassert (INTEGERP (overlay_arrow_string));
19856 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19858 overlay_arrow_seen = 1;
19861 /* Highlight trailing whitespace. */
19862 if (!NILP (Vshow_trailing_whitespace))
19863 highlight_trailing_whitespace (it->f, it->glyph_row);
19865 /* Compute pixel dimensions of this line. */
19866 compute_line_metrics (it);
19868 /* Implementation note: No changes in the glyphs of ROW or in their
19869 faces can be done past this point, because compute_line_metrics
19870 computes ROW's hash value and stores it within the glyph_row
19871 structure. */
19873 /* Record whether this row ends inside an ellipsis. */
19874 row->ends_in_ellipsis_p
19875 = (it->method == GET_FROM_DISPLAY_VECTOR
19876 && it->ellipsis_p);
19878 /* Save fringe bitmaps in this row. */
19879 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19880 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19881 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19882 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19884 it->left_user_fringe_bitmap = 0;
19885 it->left_user_fringe_face_id = 0;
19886 it->right_user_fringe_bitmap = 0;
19887 it->right_user_fringe_face_id = 0;
19889 /* Maybe set the cursor. */
19890 cvpos = it->w->cursor.vpos;
19891 if ((cvpos < 0
19892 /* In bidi-reordered rows, keep checking for proper cursor
19893 position even if one has been found already, because buffer
19894 positions in such rows change non-linearly with ROW->VPOS,
19895 when a line is continued. One exception: when we are at ZV,
19896 display cursor on the first suitable glyph row, since all
19897 the empty rows after that also have their position set to ZV. */
19898 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19899 lines' rows is implemented for bidi-reordered rows. */
19900 || (it->bidi_p
19901 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19902 && PT >= MATRIX_ROW_START_CHARPOS (row)
19903 && PT <= MATRIX_ROW_END_CHARPOS (row)
19904 && cursor_row_p (row))
19905 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19907 /* Prepare for the next line. This line starts horizontally at (X
19908 HPOS) = (0 0). Vertical positions are incremented. As a
19909 convenience for the caller, IT->glyph_row is set to the next
19910 row to be used. */
19911 it->current_x = it->hpos = 0;
19912 it->current_y += row->height;
19913 SET_TEXT_POS (it->eol_pos, 0, 0);
19914 ++it->vpos;
19915 ++it->glyph_row;
19916 /* The next row should by default use the same value of the
19917 reversed_p flag as this one. set_iterator_to_next decides when
19918 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19919 the flag accordingly. */
19920 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19921 it->glyph_row->reversed_p = row->reversed_p;
19922 it->start = row->end;
19923 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19925 #undef RECORD_MAX_MIN_POS
19928 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19929 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19930 doc: /* Return paragraph direction at point in BUFFER.
19931 Value is either `left-to-right' or `right-to-left'.
19932 If BUFFER is omitted or nil, it defaults to the current buffer.
19934 Paragraph direction determines how the text in the paragraph is displayed.
19935 In left-to-right paragraphs, text begins at the left margin of the window
19936 and the reading direction is generally left to right. In right-to-left
19937 paragraphs, text begins at the right margin and is read from right to left.
19939 See also `bidi-paragraph-direction'. */)
19940 (Lisp_Object buffer)
19942 struct buffer *buf = current_buffer;
19943 struct buffer *old = buf;
19945 if (! NILP (buffer))
19947 CHECK_BUFFER (buffer);
19948 buf = XBUFFER (buffer);
19951 if (NILP (BVAR (buf, bidi_display_reordering))
19952 || NILP (BVAR (buf, enable_multibyte_characters))
19953 /* When we are loading loadup.el, the character property tables
19954 needed for bidi iteration are not yet available. */
19955 || !NILP (Vpurify_flag))
19956 return Qleft_to_right;
19957 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19958 return BVAR (buf, bidi_paragraph_direction);
19959 else
19961 /* Determine the direction from buffer text. We could try to
19962 use current_matrix if it is up to date, but this seems fast
19963 enough as it is. */
19964 struct bidi_it itb;
19965 ptrdiff_t pos = BUF_PT (buf);
19966 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19967 int c;
19968 void *itb_data = bidi_shelve_cache ();
19970 set_buffer_temp (buf);
19971 /* bidi_paragraph_init finds the base direction of the paragraph
19972 by searching forward from paragraph start. We need the base
19973 direction of the current or _previous_ paragraph, so we need
19974 to make sure we are within that paragraph. To that end, find
19975 the previous non-empty line. */
19976 if (pos >= ZV && pos > BEGV)
19977 DEC_BOTH (pos, bytepos);
19978 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19979 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19981 while ((c = FETCH_BYTE (bytepos)) == '\n'
19982 || c == ' ' || c == '\t' || c == '\f')
19984 if (bytepos <= BEGV_BYTE)
19985 break;
19986 bytepos--;
19987 pos--;
19989 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19990 bytepos--;
19992 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19993 itb.paragraph_dir = NEUTRAL_DIR;
19994 itb.string.s = NULL;
19995 itb.string.lstring = Qnil;
19996 itb.string.bufpos = 0;
19997 itb.string.unibyte = 0;
19998 /* We have no window to use here for ignoring window-specific
19999 overlays. Using NULL for window pointer will cause
20000 compute_display_string_pos to use the current buffer. */
20001 itb.w = NULL;
20002 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20003 bidi_unshelve_cache (itb_data, 0);
20004 set_buffer_temp (old);
20005 switch (itb.paragraph_dir)
20007 case L2R:
20008 return Qleft_to_right;
20009 break;
20010 case R2L:
20011 return Qright_to_left;
20012 break;
20013 default:
20014 emacs_abort ();
20019 DEFUN ("move-point-visually", Fmove_point_visually,
20020 Smove_point_visually, 1, 1, 0,
20021 doc: /* Move point in the visual order in the specified DIRECTION.
20022 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20023 left.
20025 Value is the new character position of point. */)
20026 (Lisp_Object direction)
20028 struct window *w = XWINDOW (selected_window);
20029 struct buffer *b = XBUFFER (w->contents);
20030 struct glyph_row *row;
20031 int dir;
20032 Lisp_Object paragraph_dir;
20034 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20035 (!(ROW)->continued_p \
20036 && INTEGERP ((GLYPH)->object) \
20037 && (GLYPH)->type == CHAR_GLYPH \
20038 && (GLYPH)->u.ch == ' ' \
20039 && (GLYPH)->charpos >= 0 \
20040 && !(GLYPH)->avoid_cursor_p)
20042 CHECK_NUMBER (direction);
20043 dir = XINT (direction);
20044 if (dir > 0)
20045 dir = 1;
20046 else
20047 dir = -1;
20049 /* If current matrix is up-to-date, we can use the information
20050 recorded in the glyphs, at least as long as the goal is on the
20051 screen. */
20052 if (w->window_end_valid
20053 && !windows_or_buffers_changed
20054 && b
20055 && !b->clip_changed
20056 && !b->prevent_redisplay_optimizations_p
20057 && !window_outdated (w)
20058 && w->cursor.vpos >= 0
20059 && w->cursor.vpos < w->current_matrix->nrows
20060 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20062 struct glyph *g = row->glyphs[TEXT_AREA];
20063 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20064 struct glyph *gpt = g + w->cursor.hpos;
20066 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20068 if (BUFFERP (g->object) && g->charpos != PT)
20070 SET_PT (g->charpos);
20071 w->cursor.vpos = -1;
20072 return make_number (PT);
20074 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20076 ptrdiff_t new_pos;
20078 if (BUFFERP (gpt->object))
20080 new_pos = PT;
20081 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20082 new_pos += (row->reversed_p ? -dir : dir);
20083 else
20084 new_pos -= (row->reversed_p ? -dir : dir);;
20086 else if (BUFFERP (g->object))
20087 new_pos = g->charpos;
20088 else
20089 break;
20090 SET_PT (new_pos);
20091 w->cursor.vpos = -1;
20092 return make_number (PT);
20094 else if (ROW_GLYPH_NEWLINE_P (row, g))
20096 /* Glyphs inserted at the end of a non-empty line for
20097 positioning the cursor have zero charpos, so we must
20098 deduce the value of point by other means. */
20099 if (g->charpos > 0)
20100 SET_PT (g->charpos);
20101 else if (row->ends_at_zv_p && PT != ZV)
20102 SET_PT (ZV);
20103 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20104 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20105 else
20106 break;
20107 w->cursor.vpos = -1;
20108 return make_number (PT);
20111 if (g == e || INTEGERP (g->object))
20113 if (row->truncated_on_left_p || row->truncated_on_right_p)
20114 goto simulate_display;
20115 if (!row->reversed_p)
20116 row += dir;
20117 else
20118 row -= dir;
20119 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20120 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20121 goto simulate_display;
20123 if (dir > 0)
20125 if (row->reversed_p && !row->continued_p)
20127 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20128 w->cursor.vpos = -1;
20129 return make_number (PT);
20131 g = row->glyphs[TEXT_AREA];
20132 e = g + row->used[TEXT_AREA];
20133 for ( ; g < e; g++)
20135 if (BUFFERP (g->object)
20136 /* Empty lines have only one glyph, which stands
20137 for the newline, and whose charpos is the
20138 buffer position of the newline. */
20139 || ROW_GLYPH_NEWLINE_P (row, g)
20140 /* When the buffer ends in a newline, the line at
20141 EOB also has one glyph, but its charpos is -1. */
20142 || (row->ends_at_zv_p
20143 && !row->reversed_p
20144 && INTEGERP (g->object)
20145 && g->type == CHAR_GLYPH
20146 && g->u.ch == ' '))
20148 if (g->charpos > 0)
20149 SET_PT (g->charpos);
20150 else if (!row->reversed_p
20151 && row->ends_at_zv_p
20152 && PT != ZV)
20153 SET_PT (ZV);
20154 else
20155 continue;
20156 w->cursor.vpos = -1;
20157 return make_number (PT);
20161 else
20163 if (!row->reversed_p && !row->continued_p)
20165 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20166 w->cursor.vpos = -1;
20167 return make_number (PT);
20169 e = row->glyphs[TEXT_AREA];
20170 g = e + row->used[TEXT_AREA] - 1;
20171 for ( ; g >= e; g--)
20173 if (BUFFERP (g->object)
20174 || (ROW_GLYPH_NEWLINE_P (row, g)
20175 && g->charpos > 0)
20176 /* Empty R2L lines on GUI frames have the buffer
20177 position of the newline stored in the stretch
20178 glyph. */
20179 || g->type == STRETCH_GLYPH
20180 || (row->ends_at_zv_p
20181 && row->reversed_p
20182 && INTEGERP (g->object)
20183 && g->type == CHAR_GLYPH
20184 && g->u.ch == ' '))
20186 if (g->charpos > 0)
20187 SET_PT (g->charpos);
20188 else if (row->reversed_p
20189 && row->ends_at_zv_p
20190 && PT != ZV)
20191 SET_PT (ZV);
20192 else
20193 continue;
20194 w->cursor.vpos = -1;
20195 return make_number (PT);
20202 simulate_display:
20204 /* If we wind up here, we failed to move by using the glyphs, so we
20205 need to simulate display instead. */
20207 if (b)
20208 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20209 else
20210 paragraph_dir = Qleft_to_right;
20211 if (EQ (paragraph_dir, Qright_to_left))
20212 dir = -dir;
20213 if (PT <= BEGV && dir < 0)
20214 xsignal0 (Qbeginning_of_buffer);
20215 else if (PT >= ZV && dir > 0)
20216 xsignal0 (Qend_of_buffer);
20217 else
20219 struct text_pos pt;
20220 struct it it;
20221 int pt_x, target_x, pixel_width, pt_vpos;
20222 bool at_eol_p;
20223 bool overshoot_expected = false;
20224 bool target_is_eol_p = false;
20226 /* Setup the arena. */
20227 SET_TEXT_POS (pt, PT, PT_BYTE);
20228 start_display (&it, w, pt);
20230 if (it.cmp_it.id < 0
20231 && it.method == GET_FROM_STRING
20232 && it.area == TEXT_AREA
20233 && it.string_from_display_prop_p
20234 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20235 overshoot_expected = true;
20237 /* Find the X coordinate of point. We start from the beginning
20238 of this or previous line to make sure we are before point in
20239 the logical order (since the move_it_* functions can only
20240 move forward). */
20241 reseat_at_previous_visible_line_start (&it);
20242 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20243 if (IT_CHARPOS (it) != PT)
20244 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20245 -1, -1, -1, MOVE_TO_POS);
20246 pt_x = it.current_x;
20247 pt_vpos = it.vpos;
20248 if (dir > 0 || overshoot_expected)
20250 struct glyph_row *row = it.glyph_row;
20252 /* When point is at beginning of line, we don't have
20253 information about the glyph there loaded into struct
20254 it. Calling get_next_display_element fixes that. */
20255 if (pt_x == 0)
20256 get_next_display_element (&it);
20257 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20258 it.glyph_row = NULL;
20259 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20260 it.glyph_row = row;
20261 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20262 it, lest it will become out of sync with it's buffer
20263 position. */
20264 it.current_x = pt_x;
20266 else
20267 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20268 pixel_width = it.pixel_width;
20269 if (overshoot_expected && at_eol_p)
20270 pixel_width = 0;
20271 else if (pixel_width <= 0)
20272 pixel_width = 1;
20274 /* If there's a display string at point, we are actually at the
20275 glyph to the left of point, so we need to correct the X
20276 coordinate. */
20277 if (overshoot_expected)
20278 pt_x += pixel_width;
20280 /* Compute target X coordinate, either to the left or to the
20281 right of point. On TTY frames, all characters have the same
20282 pixel width of 1, so we can use that. On GUI frames we don't
20283 have an easy way of getting at the pixel width of the
20284 character to the left of point, so we use a different method
20285 of getting to that place. */
20286 if (dir > 0)
20287 target_x = pt_x + pixel_width;
20288 else
20289 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20291 /* Target X coordinate could be one line above or below the line
20292 of point, in which case we need to adjust the target X
20293 coordinate. Also, if moving to the left, we need to begin at
20294 the left edge of the point's screen line. */
20295 if (dir < 0)
20297 if (pt_x > 0)
20299 start_display (&it, w, pt);
20300 reseat_at_previous_visible_line_start (&it);
20301 it.current_x = it.current_y = it.hpos = 0;
20302 if (pt_vpos != 0)
20303 move_it_by_lines (&it, pt_vpos);
20305 else
20307 move_it_by_lines (&it, -1);
20308 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20309 target_is_eol_p = true;
20312 else
20314 if (at_eol_p
20315 || (target_x >= it.last_visible_x
20316 && it.line_wrap != TRUNCATE))
20318 if (pt_x > 0)
20319 move_it_by_lines (&it, 0);
20320 move_it_by_lines (&it, 1);
20321 target_x = 0;
20325 /* Move to the target X coordinate. */
20326 #ifdef HAVE_WINDOW_SYSTEM
20327 /* On GUI frames, as we don't know the X coordinate of the
20328 character to the left of point, moving point to the left
20329 requires walking, one grapheme cluster at a time, until we
20330 find ourself at a place immediately to the left of the
20331 character at point. */
20332 if (FRAME_WINDOW_P (it.f) && dir < 0)
20334 struct text_pos new_pos = it.current.pos;
20335 enum move_it_result rc = MOVE_X_REACHED;
20337 while (it.current_x + it.pixel_width <= target_x
20338 && rc == MOVE_X_REACHED)
20340 int new_x = it.current_x + it.pixel_width;
20342 new_pos = it.current.pos;
20343 if (new_x == it.current_x)
20344 new_x++;
20345 rc = move_it_in_display_line_to (&it, ZV, new_x,
20346 MOVE_TO_POS | MOVE_TO_X);
20347 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20348 break;
20350 /* If we ended up on a composed character inside
20351 bidi-reordered text (e.g., Hebrew text with diacritics),
20352 the iterator gives us the buffer position of the last (in
20353 logical order) character of the composed grapheme cluster,
20354 which is not what we want. So we cheat: we compute the
20355 character position of the character that follows (in the
20356 logical order) the one where the above loop stopped. That
20357 character will appear on display to the left of point. */
20358 if (it.bidi_p
20359 && it.bidi_it.scan_dir == -1
20360 && new_pos.charpos - IT_CHARPOS (it) > 1)
20362 new_pos.charpos = IT_CHARPOS (it) + 1;
20363 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20365 it.current.pos = new_pos;
20367 else
20368 #endif
20369 if (it.current_x != target_x)
20370 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20372 /* When lines are truncated, the above loop will stop at the
20373 window edge. But we want to get to the end of line, even if
20374 it is beyond the window edge; automatic hscroll will then
20375 scroll the window to show point as appropriate. */
20376 if (target_is_eol_p && it.line_wrap == TRUNCATE
20377 && get_next_display_element (&it))
20379 struct text_pos new_pos = it.current.pos;
20381 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20383 set_iterator_to_next (&it, 0);
20384 if (it.method == GET_FROM_BUFFER)
20385 new_pos = it.current.pos;
20386 if (!get_next_display_element (&it))
20387 break;
20390 it.current.pos = new_pos;
20393 /* If we ended up in a display string that covers point, move to
20394 buffer position to the right in the visual order. */
20395 if (dir > 0)
20397 while (IT_CHARPOS (it) == PT)
20399 set_iterator_to_next (&it, 0);
20400 if (!get_next_display_element (&it))
20401 break;
20405 /* Move point to that position. */
20406 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20409 return make_number (PT);
20411 #undef ROW_GLYPH_NEWLINE_P
20415 /***********************************************************************
20416 Menu Bar
20417 ***********************************************************************/
20419 /* Redisplay the menu bar in the frame for window W.
20421 The menu bar of X frames that don't have X toolkit support is
20422 displayed in a special window W->frame->menu_bar_window.
20424 The menu bar of terminal frames is treated specially as far as
20425 glyph matrices are concerned. Menu bar lines are not part of
20426 windows, so the update is done directly on the frame matrix rows
20427 for the menu bar. */
20429 static void
20430 display_menu_bar (struct window *w)
20432 struct frame *f = XFRAME (WINDOW_FRAME (w));
20433 struct it it;
20434 Lisp_Object items;
20435 int i;
20437 /* Don't do all this for graphical frames. */
20438 #ifdef HAVE_NTGUI
20439 if (FRAME_W32_P (f))
20440 return;
20441 #endif
20442 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20443 if (FRAME_X_P (f))
20444 return;
20445 #endif
20447 #ifdef HAVE_NS
20448 if (FRAME_NS_P (f))
20449 return;
20450 #endif /* HAVE_NS */
20452 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20453 eassert (!FRAME_WINDOW_P (f));
20454 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20455 it.first_visible_x = 0;
20456 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20457 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20458 if (FRAME_WINDOW_P (f))
20460 /* Menu bar lines are displayed in the desired matrix of the
20461 dummy window menu_bar_window. */
20462 struct window *menu_w;
20463 menu_w = XWINDOW (f->menu_bar_window);
20464 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20465 MENU_FACE_ID);
20466 it.first_visible_x = 0;
20467 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20469 else
20470 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20472 /* This is a TTY frame, i.e. character hpos/vpos are used as
20473 pixel x/y. */
20474 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20475 MENU_FACE_ID);
20476 it.first_visible_x = 0;
20477 it.last_visible_x = FRAME_COLS (f);
20480 /* FIXME: This should be controlled by a user option. See the
20481 comments in redisplay_tool_bar and display_mode_line about
20482 this. */
20483 it.paragraph_embedding = L2R;
20485 /* Clear all rows of the menu bar. */
20486 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20488 struct glyph_row *row = it.glyph_row + i;
20489 clear_glyph_row (row);
20490 row->enabled_p = 1;
20491 row->full_width_p = 1;
20494 /* Display all items of the menu bar. */
20495 items = FRAME_MENU_BAR_ITEMS (it.f);
20496 for (i = 0; i < ASIZE (items); i += 4)
20498 Lisp_Object string;
20500 /* Stop at nil string. */
20501 string = AREF (items, i + 1);
20502 if (NILP (string))
20503 break;
20505 /* Remember where item was displayed. */
20506 ASET (items, i + 3, make_number (it.hpos));
20508 /* Display the item, pad with one space. */
20509 if (it.current_x < it.last_visible_x)
20510 display_string (NULL, string, Qnil, 0, 0, &it,
20511 SCHARS (string) + 1, 0, 0, -1);
20514 /* Fill out the line with spaces. */
20515 if (it.current_x < it.last_visible_x)
20516 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20518 /* Compute the total height of the lines. */
20519 compute_line_metrics (&it);
20522 #ifdef HAVE_MENUS
20523 /* Deep copy of a glyph row, including the glyphs. */
20524 static void
20525 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20527 struct glyph *pointers[1 + LAST_AREA];
20528 int to_used = to->used[TEXT_AREA];
20530 /* Save glyph pointers of TO. */
20531 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20533 /* Do a structure assignment. */
20534 *to = *from;
20536 /* Restore original glyph pointers of TO. */
20537 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20539 /* Copy the glyphs. */
20540 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20541 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20543 /* If we filled only part of the TO row, fill the rest with
20544 space_glyph (which will display as empty space). */
20545 if (to_used > from->used[TEXT_AREA])
20546 fill_up_frame_row_with_spaces (to, to_used);
20549 /* Display one menu item on a TTY, by overwriting the glyphs in the
20550 frame F's desired glyph matrix with glyphs produced from the menu
20551 item text. Called from term.c to display TTY drop-down menus one
20552 item at a time.
20554 ITEM_TEXT is the menu item text as a C string.
20556 FACE_ID is the face ID to be used for this menu item. FACE_ID
20557 could specify one of 3 faces: a face for an enabled item, a face
20558 for a disabled item, or a face for a selected item.
20560 X and Y are coordinates of the first glyph in the frame's desired
20561 matrix to be overwritten by the menu item. Since this is a TTY, Y
20562 is the zero-based number of the glyph row and X is the zero-based
20563 glyph number in the row, starting from left, where to start
20564 displaying the item.
20566 SUBMENU non-zero means this menu item drops down a submenu, which
20567 should be indicated by displaying a proper visual cue after the
20568 item text. */
20570 void
20571 display_tty_menu_item (const char *item_text, int width, int face_id,
20572 int x, int y, int submenu)
20574 struct it it;
20575 struct frame *f = SELECTED_FRAME ();
20576 struct window *w = XWINDOW (f->selected_window);
20577 int saved_used, saved_truncated, saved_width, saved_reversed;
20578 struct glyph_row *row;
20579 size_t item_len = strlen (item_text);
20581 eassert (FRAME_TERMCAP_P (f));
20583 /* Don't write beyond the matrix's last row. This can happen for
20584 TTY screens that are not high enough to show the entire menu.
20585 (This is actually a bit of defensive programming, as
20586 tty_menu_display already limits the number of menu items to one
20587 less than the number of screen lines.) */
20588 if (y >= f->desired_matrix->nrows)
20589 return;
20591 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
20592 it.first_visible_x = 0;
20593 it.last_visible_x = FRAME_COLS (f) - 1;
20594 row = it.glyph_row;
20595 /* Start with the row contents from the current matrix. */
20596 deep_copy_glyph_row (row, f->current_matrix->rows + y);
20597 saved_width = row->full_width_p;
20598 row->full_width_p = 1;
20599 saved_reversed = row->reversed_p;
20600 row->reversed_p = 0;
20601 row->enabled_p = 1;
20603 /* Arrange for the menu item glyphs to start at (X,Y) and have the
20604 desired face. */
20605 eassert (x < f->desired_matrix->matrix_w);
20606 it.current_x = it.hpos = x;
20607 it.current_y = it.vpos = y;
20608 saved_used = row->used[TEXT_AREA];
20609 saved_truncated = row->truncated_on_right_p;
20610 row->used[TEXT_AREA] = x;
20611 it.face_id = face_id;
20612 it.line_wrap = TRUNCATE;
20614 /* FIXME: This should be controlled by a user option. See the
20615 comments in redisplay_tool_bar and display_mode_line about this.
20616 Also, if paragraph_embedding could ever be R2L, changes will be
20617 needed to avoid shifting to the right the row characters in
20618 term.c:append_glyph. */
20619 it.paragraph_embedding = L2R;
20621 /* Pad with a space on the left. */
20622 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
20623 width--;
20624 /* Display the menu item, pad with spaces to WIDTH. */
20625 if (submenu)
20627 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20628 item_len, 0, FRAME_COLS (f) - 1, -1);
20629 width -= item_len;
20630 /* Indicate with " >" that there's a submenu. */
20631 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
20632 FRAME_COLS (f) - 1, -1);
20634 else
20635 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20636 width, 0, FRAME_COLS (f) - 1, -1);
20638 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
20639 row->truncated_on_right_p = saved_truncated;
20640 row->hash = row_hash (row);
20641 row->full_width_p = saved_width;
20642 row->reversed_p = saved_reversed;
20644 #endif /* HAVE_MENUS */
20646 /***********************************************************************
20647 Mode Line
20648 ***********************************************************************/
20650 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20651 FORCE is non-zero, redisplay mode lines unconditionally.
20652 Otherwise, redisplay only mode lines that are garbaged. Value is
20653 the number of windows whose mode lines were redisplayed. */
20655 static int
20656 redisplay_mode_lines (Lisp_Object window, int force)
20658 int nwindows = 0;
20660 while (!NILP (window))
20662 struct window *w = XWINDOW (window);
20664 if (WINDOWP (w->contents))
20665 nwindows += redisplay_mode_lines (w->contents, force);
20666 else if (force
20667 || FRAME_GARBAGED_P (XFRAME (w->frame))
20668 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20670 struct text_pos lpoint;
20671 struct buffer *old = current_buffer;
20673 /* Set the window's buffer for the mode line display. */
20674 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20675 set_buffer_internal_1 (XBUFFER (w->contents));
20677 /* Point refers normally to the selected window. For any
20678 other window, set up appropriate value. */
20679 if (!EQ (window, selected_window))
20681 struct text_pos pt;
20683 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20684 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20687 /* Display mode lines. */
20688 clear_glyph_matrix (w->desired_matrix);
20689 if (display_mode_lines (w))
20691 ++nwindows;
20692 w->must_be_updated_p = 1;
20695 /* Restore old settings. */
20696 set_buffer_internal_1 (old);
20697 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20700 window = w->next;
20703 return nwindows;
20707 /* Display the mode and/or header line of window W. Value is the
20708 sum number of mode lines and header lines displayed. */
20710 static int
20711 display_mode_lines (struct window *w)
20713 Lisp_Object old_selected_window = selected_window;
20714 Lisp_Object old_selected_frame = selected_frame;
20715 Lisp_Object new_frame = w->frame;
20716 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20717 int n = 0;
20719 selected_frame = new_frame;
20720 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20721 or window's point, then we'd need select_window_1 here as well. */
20722 XSETWINDOW (selected_window, w);
20723 XFRAME (new_frame)->selected_window = selected_window;
20725 /* These will be set while the mode line specs are processed. */
20726 line_number_displayed = 0;
20727 w->column_number_displayed = -1;
20729 if (WINDOW_WANTS_MODELINE_P (w))
20731 struct window *sel_w = XWINDOW (old_selected_window);
20733 /* Select mode line face based on the real selected window. */
20734 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20735 BVAR (current_buffer, mode_line_format));
20736 ++n;
20739 if (WINDOW_WANTS_HEADER_LINE_P (w))
20741 display_mode_line (w, HEADER_LINE_FACE_ID,
20742 BVAR (current_buffer, header_line_format));
20743 ++n;
20746 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20747 selected_frame = old_selected_frame;
20748 selected_window = old_selected_window;
20749 return n;
20753 /* Display mode or header line of window W. FACE_ID specifies which
20754 line to display; it is either MODE_LINE_FACE_ID or
20755 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20756 display. Value is the pixel height of the mode/header line
20757 displayed. */
20759 static int
20760 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20762 struct it it;
20763 struct face *face;
20764 ptrdiff_t count = SPECPDL_INDEX ();
20766 init_iterator (&it, w, -1, -1, NULL, face_id);
20767 /* Don't extend on a previously drawn mode-line.
20768 This may happen if called from pos_visible_p. */
20769 it.glyph_row->enabled_p = 0;
20770 prepare_desired_row (it.glyph_row);
20772 it.glyph_row->mode_line_p = 1;
20774 /* FIXME: This should be controlled by a user option. But
20775 supporting such an option is not trivial, since the mode line is
20776 made up of many separate strings. */
20777 it.paragraph_embedding = L2R;
20779 record_unwind_protect (unwind_format_mode_line,
20780 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20782 mode_line_target = MODE_LINE_DISPLAY;
20784 /* Temporarily make frame's keyboard the current kboard so that
20785 kboard-local variables in the mode_line_format will get the right
20786 values. */
20787 push_kboard (FRAME_KBOARD (it.f));
20788 record_unwind_save_match_data ();
20789 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20790 pop_kboard ();
20792 unbind_to (count, Qnil);
20794 /* Fill up with spaces. */
20795 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20797 compute_line_metrics (&it);
20798 it.glyph_row->full_width_p = 1;
20799 it.glyph_row->continued_p = 0;
20800 it.glyph_row->truncated_on_left_p = 0;
20801 it.glyph_row->truncated_on_right_p = 0;
20803 /* Make a 3D mode-line have a shadow at its right end. */
20804 face = FACE_FROM_ID (it.f, face_id);
20805 extend_face_to_end_of_line (&it);
20806 if (face->box != FACE_NO_BOX)
20808 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20809 + it.glyph_row->used[TEXT_AREA] - 1);
20810 last->right_box_line_p = 1;
20813 return it.glyph_row->height;
20816 /* Move element ELT in LIST to the front of LIST.
20817 Return the updated list. */
20819 static Lisp_Object
20820 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20822 register Lisp_Object tail, prev;
20823 register Lisp_Object tem;
20825 tail = list;
20826 prev = Qnil;
20827 while (CONSP (tail))
20829 tem = XCAR (tail);
20831 if (EQ (elt, tem))
20833 /* Splice out the link TAIL. */
20834 if (NILP (prev))
20835 list = XCDR (tail);
20836 else
20837 Fsetcdr (prev, XCDR (tail));
20839 /* Now make it the first. */
20840 Fsetcdr (tail, list);
20841 return tail;
20843 else
20844 prev = tail;
20845 tail = XCDR (tail);
20846 QUIT;
20849 /* Not found--return unchanged LIST. */
20850 return list;
20853 /* Contribute ELT to the mode line for window IT->w. How it
20854 translates into text depends on its data type.
20856 IT describes the display environment in which we display, as usual.
20858 DEPTH is the depth in recursion. It is used to prevent
20859 infinite recursion here.
20861 FIELD_WIDTH is the number of characters the display of ELT should
20862 occupy in the mode line, and PRECISION is the maximum number of
20863 characters to display from ELT's representation. See
20864 display_string for details.
20866 Returns the hpos of the end of the text generated by ELT.
20868 PROPS is a property list to add to any string we encounter.
20870 If RISKY is nonzero, remove (disregard) any properties in any string
20871 we encounter, and ignore :eval and :propertize.
20873 The global variable `mode_line_target' determines whether the
20874 output is passed to `store_mode_line_noprop',
20875 `store_mode_line_string', or `display_string'. */
20877 static int
20878 display_mode_element (struct it *it, int depth, int field_width, int precision,
20879 Lisp_Object elt, Lisp_Object props, int risky)
20881 int n = 0, field, prec;
20882 int literal = 0;
20884 tail_recurse:
20885 if (depth > 100)
20886 elt = build_string ("*too-deep*");
20888 depth++;
20890 switch (XTYPE (elt))
20892 case Lisp_String:
20894 /* A string: output it and check for %-constructs within it. */
20895 unsigned char c;
20896 ptrdiff_t offset = 0;
20898 if (SCHARS (elt) > 0
20899 && (!NILP (props) || risky))
20901 Lisp_Object oprops, aelt;
20902 oprops = Ftext_properties_at (make_number (0), elt);
20904 /* If the starting string's properties are not what
20905 we want, translate the string. Also, if the string
20906 is risky, do that anyway. */
20908 if (NILP (Fequal (props, oprops)) || risky)
20910 /* If the starting string has properties,
20911 merge the specified ones onto the existing ones. */
20912 if (! NILP (oprops) && !risky)
20914 Lisp_Object tem;
20916 oprops = Fcopy_sequence (oprops);
20917 tem = props;
20918 while (CONSP (tem))
20920 oprops = Fplist_put (oprops, XCAR (tem),
20921 XCAR (XCDR (tem)));
20922 tem = XCDR (XCDR (tem));
20924 props = oprops;
20927 aelt = Fassoc (elt, mode_line_proptrans_alist);
20928 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20930 /* AELT is what we want. Move it to the front
20931 without consing. */
20932 elt = XCAR (aelt);
20933 mode_line_proptrans_alist
20934 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20936 else
20938 Lisp_Object tem;
20940 /* If AELT has the wrong props, it is useless.
20941 so get rid of it. */
20942 if (! NILP (aelt))
20943 mode_line_proptrans_alist
20944 = Fdelq (aelt, mode_line_proptrans_alist);
20946 elt = Fcopy_sequence (elt);
20947 Fset_text_properties (make_number (0), Flength (elt),
20948 props, elt);
20949 /* Add this item to mode_line_proptrans_alist. */
20950 mode_line_proptrans_alist
20951 = Fcons (Fcons (elt, props),
20952 mode_line_proptrans_alist);
20953 /* Truncate mode_line_proptrans_alist
20954 to at most 50 elements. */
20955 tem = Fnthcdr (make_number (50),
20956 mode_line_proptrans_alist);
20957 if (! NILP (tem))
20958 XSETCDR (tem, Qnil);
20963 offset = 0;
20965 if (literal)
20967 prec = precision - n;
20968 switch (mode_line_target)
20970 case MODE_LINE_NOPROP:
20971 case MODE_LINE_TITLE:
20972 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20973 break;
20974 case MODE_LINE_STRING:
20975 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20976 break;
20977 case MODE_LINE_DISPLAY:
20978 n += display_string (NULL, elt, Qnil, 0, 0, it,
20979 0, prec, 0, STRING_MULTIBYTE (elt));
20980 break;
20983 break;
20986 /* Handle the non-literal case. */
20988 while ((precision <= 0 || n < precision)
20989 && SREF (elt, offset) != 0
20990 && (mode_line_target != MODE_LINE_DISPLAY
20991 || it->current_x < it->last_visible_x))
20993 ptrdiff_t last_offset = offset;
20995 /* Advance to end of string or next format specifier. */
20996 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20999 if (offset - 1 != last_offset)
21001 ptrdiff_t nchars, nbytes;
21003 /* Output to end of string or up to '%'. Field width
21004 is length of string. Don't output more than
21005 PRECISION allows us. */
21006 offset--;
21008 prec = c_string_width (SDATA (elt) + last_offset,
21009 offset - last_offset, precision - n,
21010 &nchars, &nbytes);
21012 switch (mode_line_target)
21014 case MODE_LINE_NOPROP:
21015 case MODE_LINE_TITLE:
21016 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21017 break;
21018 case MODE_LINE_STRING:
21020 ptrdiff_t bytepos = last_offset;
21021 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21022 ptrdiff_t endpos = (precision <= 0
21023 ? string_byte_to_char (elt, offset)
21024 : charpos + nchars);
21026 n += store_mode_line_string (NULL,
21027 Fsubstring (elt, make_number (charpos),
21028 make_number (endpos)),
21029 0, 0, 0, Qnil);
21031 break;
21032 case MODE_LINE_DISPLAY:
21034 ptrdiff_t bytepos = last_offset;
21035 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21037 if (precision <= 0)
21038 nchars = string_byte_to_char (elt, offset) - charpos;
21039 n += display_string (NULL, elt, Qnil, 0, charpos,
21040 it, 0, nchars, 0,
21041 STRING_MULTIBYTE (elt));
21043 break;
21046 else /* c == '%' */
21048 ptrdiff_t percent_position = offset;
21050 /* Get the specified minimum width. Zero means
21051 don't pad. */
21052 field = 0;
21053 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21054 field = field * 10 + c - '0';
21056 /* Don't pad beyond the total padding allowed. */
21057 if (field_width - n > 0 && field > field_width - n)
21058 field = field_width - n;
21060 /* Note that either PRECISION <= 0 or N < PRECISION. */
21061 prec = precision - n;
21063 if (c == 'M')
21064 n += display_mode_element (it, depth, field, prec,
21065 Vglobal_mode_string, props,
21066 risky);
21067 else if (c != 0)
21069 bool multibyte;
21070 ptrdiff_t bytepos, charpos;
21071 const char *spec;
21072 Lisp_Object string;
21074 bytepos = percent_position;
21075 charpos = (STRING_MULTIBYTE (elt)
21076 ? string_byte_to_char (elt, bytepos)
21077 : bytepos);
21078 spec = decode_mode_spec (it->w, c, field, &string);
21079 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21081 switch (mode_line_target)
21083 case MODE_LINE_NOPROP:
21084 case MODE_LINE_TITLE:
21085 n += store_mode_line_noprop (spec, field, prec);
21086 break;
21087 case MODE_LINE_STRING:
21089 Lisp_Object tem = build_string (spec);
21090 props = Ftext_properties_at (make_number (charpos), elt);
21091 /* Should only keep face property in props */
21092 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21094 break;
21095 case MODE_LINE_DISPLAY:
21097 int nglyphs_before, nwritten;
21099 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21100 nwritten = display_string (spec, string, elt,
21101 charpos, 0, it,
21102 field, prec, 0,
21103 multibyte);
21105 /* Assign to the glyphs written above the
21106 string where the `%x' came from, position
21107 of the `%'. */
21108 if (nwritten > 0)
21110 struct glyph *glyph
21111 = (it->glyph_row->glyphs[TEXT_AREA]
21112 + nglyphs_before);
21113 int i;
21115 for (i = 0; i < nwritten; ++i)
21117 glyph[i].object = elt;
21118 glyph[i].charpos = charpos;
21121 n += nwritten;
21124 break;
21127 else /* c == 0 */
21128 break;
21132 break;
21134 case Lisp_Symbol:
21135 /* A symbol: process the value of the symbol recursively
21136 as if it appeared here directly. Avoid error if symbol void.
21137 Special case: if value of symbol is a string, output the string
21138 literally. */
21140 register Lisp_Object tem;
21142 /* If the variable is not marked as risky to set
21143 then its contents are risky to use. */
21144 if (NILP (Fget (elt, Qrisky_local_variable)))
21145 risky = 1;
21147 tem = Fboundp (elt);
21148 if (!NILP (tem))
21150 tem = Fsymbol_value (elt);
21151 /* If value is a string, output that string literally:
21152 don't check for % within it. */
21153 if (STRINGP (tem))
21154 literal = 1;
21156 if (!EQ (tem, elt))
21158 /* Give up right away for nil or t. */
21159 elt = tem;
21160 goto tail_recurse;
21164 break;
21166 case Lisp_Cons:
21168 register Lisp_Object car, tem;
21170 /* A cons cell: five distinct cases.
21171 If first element is :eval or :propertize, do something special.
21172 If first element is a string or a cons, process all the elements
21173 and effectively concatenate them.
21174 If first element is a negative number, truncate displaying cdr to
21175 at most that many characters. If positive, pad (with spaces)
21176 to at least that many characters.
21177 If first element is a symbol, process the cadr or caddr recursively
21178 according to whether the symbol's value is non-nil or nil. */
21179 car = XCAR (elt);
21180 if (EQ (car, QCeval))
21182 /* An element of the form (:eval FORM) means evaluate FORM
21183 and use the result as mode line elements. */
21185 if (risky)
21186 break;
21188 if (CONSP (XCDR (elt)))
21190 Lisp_Object spec;
21191 spec = safe_eval (XCAR (XCDR (elt)));
21192 n += display_mode_element (it, depth, field_width - n,
21193 precision - n, spec, props,
21194 risky);
21197 else if (EQ (car, QCpropertize))
21199 /* An element of the form (:propertize ELT PROPS...)
21200 means display ELT but applying properties PROPS. */
21202 if (risky)
21203 break;
21205 if (CONSP (XCDR (elt)))
21206 n += display_mode_element (it, depth, field_width - n,
21207 precision - n, XCAR (XCDR (elt)),
21208 XCDR (XCDR (elt)), risky);
21210 else if (SYMBOLP (car))
21212 tem = Fboundp (car);
21213 elt = XCDR (elt);
21214 if (!CONSP (elt))
21215 goto invalid;
21216 /* elt is now the cdr, and we know it is a cons cell.
21217 Use its car if CAR has a non-nil value. */
21218 if (!NILP (tem))
21220 tem = Fsymbol_value (car);
21221 if (!NILP (tem))
21223 elt = XCAR (elt);
21224 goto tail_recurse;
21227 /* Symbol's value is nil (or symbol is unbound)
21228 Get the cddr of the original list
21229 and if possible find the caddr and use that. */
21230 elt = XCDR (elt);
21231 if (NILP (elt))
21232 break;
21233 else if (!CONSP (elt))
21234 goto invalid;
21235 elt = XCAR (elt);
21236 goto tail_recurse;
21238 else if (INTEGERP (car))
21240 register int lim = XINT (car);
21241 elt = XCDR (elt);
21242 if (lim < 0)
21244 /* Negative int means reduce maximum width. */
21245 if (precision <= 0)
21246 precision = -lim;
21247 else
21248 precision = min (precision, -lim);
21250 else if (lim > 0)
21252 /* Padding specified. Don't let it be more than
21253 current maximum. */
21254 if (precision > 0)
21255 lim = min (precision, lim);
21257 /* If that's more padding than already wanted, queue it.
21258 But don't reduce padding already specified even if
21259 that is beyond the current truncation point. */
21260 field_width = max (lim, field_width);
21262 goto tail_recurse;
21264 else if (STRINGP (car) || CONSP (car))
21266 Lisp_Object halftail = elt;
21267 int len = 0;
21269 while (CONSP (elt)
21270 && (precision <= 0 || n < precision))
21272 n += display_mode_element (it, depth,
21273 /* Do padding only after the last
21274 element in the list. */
21275 (! CONSP (XCDR (elt))
21276 ? field_width - n
21277 : 0),
21278 precision - n, XCAR (elt),
21279 props, risky);
21280 elt = XCDR (elt);
21281 len++;
21282 if ((len & 1) == 0)
21283 halftail = XCDR (halftail);
21284 /* Check for cycle. */
21285 if (EQ (halftail, elt))
21286 break;
21290 break;
21292 default:
21293 invalid:
21294 elt = build_string ("*invalid*");
21295 goto tail_recurse;
21298 /* Pad to FIELD_WIDTH. */
21299 if (field_width > 0 && n < field_width)
21301 switch (mode_line_target)
21303 case MODE_LINE_NOPROP:
21304 case MODE_LINE_TITLE:
21305 n += store_mode_line_noprop ("", field_width - n, 0);
21306 break;
21307 case MODE_LINE_STRING:
21308 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21309 break;
21310 case MODE_LINE_DISPLAY:
21311 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21312 0, 0, 0);
21313 break;
21317 return n;
21320 /* Store a mode-line string element in mode_line_string_list.
21322 If STRING is non-null, display that C string. Otherwise, the Lisp
21323 string LISP_STRING is displayed.
21325 FIELD_WIDTH is the minimum number of output glyphs to produce.
21326 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21327 with spaces. FIELD_WIDTH <= 0 means don't pad.
21329 PRECISION is the maximum number of characters to output from
21330 STRING. PRECISION <= 0 means don't truncate the string.
21332 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21333 properties to the string.
21335 PROPS are the properties to add to the string.
21336 The mode_line_string_face face property is always added to the string.
21339 static int
21340 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21341 int field_width, int precision, Lisp_Object props)
21343 ptrdiff_t len;
21344 int n = 0;
21346 if (string != NULL)
21348 len = strlen (string);
21349 if (precision > 0 && len > precision)
21350 len = precision;
21351 lisp_string = make_string (string, len);
21352 if (NILP (props))
21353 props = mode_line_string_face_prop;
21354 else if (!NILP (mode_line_string_face))
21356 Lisp_Object face = Fplist_get (props, Qface);
21357 props = Fcopy_sequence (props);
21358 if (NILP (face))
21359 face = mode_line_string_face;
21360 else
21361 face = list2 (face, mode_line_string_face);
21362 props = Fplist_put (props, Qface, face);
21364 Fadd_text_properties (make_number (0), make_number (len),
21365 props, lisp_string);
21367 else
21369 len = XFASTINT (Flength (lisp_string));
21370 if (precision > 0 && len > precision)
21372 len = precision;
21373 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21374 precision = -1;
21376 if (!NILP (mode_line_string_face))
21378 Lisp_Object face;
21379 if (NILP (props))
21380 props = Ftext_properties_at (make_number (0), lisp_string);
21381 face = Fplist_get (props, Qface);
21382 if (NILP (face))
21383 face = mode_line_string_face;
21384 else
21385 face = list2 (face, mode_line_string_face);
21386 props = list2 (Qface, face);
21387 if (copy_string)
21388 lisp_string = Fcopy_sequence (lisp_string);
21390 if (!NILP (props))
21391 Fadd_text_properties (make_number (0), make_number (len),
21392 props, lisp_string);
21395 if (len > 0)
21397 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21398 n += len;
21401 if (field_width > len)
21403 field_width -= len;
21404 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21405 if (!NILP (props))
21406 Fadd_text_properties (make_number (0), make_number (field_width),
21407 props, lisp_string);
21408 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21409 n += field_width;
21412 return n;
21416 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21417 1, 4, 0,
21418 doc: /* Format a string out of a mode line format specification.
21419 First arg FORMAT specifies the mode line format (see `mode-line-format'
21420 for details) to use.
21422 By default, the format is evaluated for the currently selected window.
21424 Optional second arg FACE specifies the face property to put on all
21425 characters for which no face is specified. The value nil means the
21426 default face. The value t means whatever face the window's mode line
21427 currently uses (either `mode-line' or `mode-line-inactive',
21428 depending on whether the window is the selected window or not).
21429 An integer value means the value string has no text
21430 properties.
21432 Optional third and fourth args WINDOW and BUFFER specify the window
21433 and buffer to use as the context for the formatting (defaults
21434 are the selected window and the WINDOW's buffer). */)
21435 (Lisp_Object format, Lisp_Object face,
21436 Lisp_Object window, Lisp_Object buffer)
21438 struct it it;
21439 int len;
21440 struct window *w;
21441 struct buffer *old_buffer = NULL;
21442 int face_id;
21443 int no_props = INTEGERP (face);
21444 ptrdiff_t count = SPECPDL_INDEX ();
21445 Lisp_Object str;
21446 int string_start = 0;
21448 w = decode_any_window (window);
21449 XSETWINDOW (window, w);
21451 if (NILP (buffer))
21452 buffer = w->contents;
21453 CHECK_BUFFER (buffer);
21455 /* Make formatting the modeline a non-op when noninteractive, otherwise
21456 there will be problems later caused by a partially initialized frame. */
21457 if (NILP (format) || noninteractive)
21458 return empty_unibyte_string;
21460 if (no_props)
21461 face = Qnil;
21463 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21464 : EQ (face, Qt) ? (EQ (window, selected_window)
21465 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21466 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21467 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21468 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21469 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21470 : DEFAULT_FACE_ID;
21472 old_buffer = current_buffer;
21474 /* Save things including mode_line_proptrans_alist,
21475 and set that to nil so that we don't alter the outer value. */
21476 record_unwind_protect (unwind_format_mode_line,
21477 format_mode_line_unwind_data
21478 (XFRAME (WINDOW_FRAME (w)),
21479 old_buffer, selected_window, 1));
21480 mode_line_proptrans_alist = Qnil;
21482 Fselect_window (window, Qt);
21483 set_buffer_internal_1 (XBUFFER (buffer));
21485 init_iterator (&it, w, -1, -1, NULL, face_id);
21487 if (no_props)
21489 mode_line_target = MODE_LINE_NOPROP;
21490 mode_line_string_face_prop = Qnil;
21491 mode_line_string_list = Qnil;
21492 string_start = MODE_LINE_NOPROP_LEN (0);
21494 else
21496 mode_line_target = MODE_LINE_STRING;
21497 mode_line_string_list = Qnil;
21498 mode_line_string_face = face;
21499 mode_line_string_face_prop
21500 = NILP (face) ? Qnil : list2 (Qface, face);
21503 push_kboard (FRAME_KBOARD (it.f));
21504 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21505 pop_kboard ();
21507 if (no_props)
21509 len = MODE_LINE_NOPROP_LEN (string_start);
21510 str = make_string (mode_line_noprop_buf + string_start, len);
21512 else
21514 mode_line_string_list = Fnreverse (mode_line_string_list);
21515 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21516 empty_unibyte_string);
21519 unbind_to (count, Qnil);
21520 return str;
21523 /* Write a null-terminated, right justified decimal representation of
21524 the positive integer D to BUF using a minimal field width WIDTH. */
21526 static void
21527 pint2str (register char *buf, register int width, register ptrdiff_t d)
21529 register char *p = buf;
21531 if (d <= 0)
21532 *p++ = '0';
21533 else
21535 while (d > 0)
21537 *p++ = d % 10 + '0';
21538 d /= 10;
21542 for (width -= (int) (p - buf); width > 0; --width)
21543 *p++ = ' ';
21544 *p-- = '\0';
21545 while (p > buf)
21547 d = *buf;
21548 *buf++ = *p;
21549 *p-- = d;
21553 /* Write a null-terminated, right justified decimal and "human
21554 readable" representation of the nonnegative integer D to BUF using
21555 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21557 static const char power_letter[] =
21559 0, /* no letter */
21560 'k', /* kilo */
21561 'M', /* mega */
21562 'G', /* giga */
21563 'T', /* tera */
21564 'P', /* peta */
21565 'E', /* exa */
21566 'Z', /* zetta */
21567 'Y' /* yotta */
21570 static void
21571 pint2hrstr (char *buf, int width, ptrdiff_t d)
21573 /* We aim to represent the nonnegative integer D as
21574 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21575 ptrdiff_t quotient = d;
21576 int remainder = 0;
21577 /* -1 means: do not use TENTHS. */
21578 int tenths = -1;
21579 int exponent = 0;
21581 /* Length of QUOTIENT.TENTHS as a string. */
21582 int length;
21584 char * psuffix;
21585 char * p;
21587 if (quotient >= 1000)
21589 /* Scale to the appropriate EXPONENT. */
21592 remainder = quotient % 1000;
21593 quotient /= 1000;
21594 exponent++;
21596 while (quotient >= 1000);
21598 /* Round to nearest and decide whether to use TENTHS or not. */
21599 if (quotient <= 9)
21601 tenths = remainder / 100;
21602 if (remainder % 100 >= 50)
21604 if (tenths < 9)
21605 tenths++;
21606 else
21608 quotient++;
21609 if (quotient == 10)
21610 tenths = -1;
21611 else
21612 tenths = 0;
21616 else
21617 if (remainder >= 500)
21619 if (quotient < 999)
21620 quotient++;
21621 else
21623 quotient = 1;
21624 exponent++;
21625 tenths = 0;
21630 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21631 if (tenths == -1 && quotient <= 99)
21632 if (quotient <= 9)
21633 length = 1;
21634 else
21635 length = 2;
21636 else
21637 length = 3;
21638 p = psuffix = buf + max (width, length);
21640 /* Print EXPONENT. */
21641 *psuffix++ = power_letter[exponent];
21642 *psuffix = '\0';
21644 /* Print TENTHS. */
21645 if (tenths >= 0)
21647 *--p = '0' + tenths;
21648 *--p = '.';
21651 /* Print QUOTIENT. */
21654 int digit = quotient % 10;
21655 *--p = '0' + digit;
21657 while ((quotient /= 10) != 0);
21659 /* Print leading spaces. */
21660 while (buf < p)
21661 *--p = ' ';
21664 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21665 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21666 type of CODING_SYSTEM. Return updated pointer into BUF. */
21668 static unsigned char invalid_eol_type[] = "(*invalid*)";
21670 static char *
21671 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21673 Lisp_Object val;
21674 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21675 const unsigned char *eol_str;
21676 int eol_str_len;
21677 /* The EOL conversion we are using. */
21678 Lisp_Object eoltype;
21680 val = CODING_SYSTEM_SPEC (coding_system);
21681 eoltype = Qnil;
21683 if (!VECTORP (val)) /* Not yet decided. */
21685 *buf++ = multibyte ? '-' : ' ';
21686 if (eol_flag)
21687 eoltype = eol_mnemonic_undecided;
21688 /* Don't mention EOL conversion if it isn't decided. */
21690 else
21692 Lisp_Object attrs;
21693 Lisp_Object eolvalue;
21695 attrs = AREF (val, 0);
21696 eolvalue = AREF (val, 2);
21698 *buf++ = multibyte
21699 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21700 : ' ';
21702 if (eol_flag)
21704 /* The EOL conversion that is normal on this system. */
21706 if (NILP (eolvalue)) /* Not yet decided. */
21707 eoltype = eol_mnemonic_undecided;
21708 else if (VECTORP (eolvalue)) /* Not yet decided. */
21709 eoltype = eol_mnemonic_undecided;
21710 else /* eolvalue is Qunix, Qdos, or Qmac. */
21711 eoltype = (EQ (eolvalue, Qunix)
21712 ? eol_mnemonic_unix
21713 : (EQ (eolvalue, Qdos) == 1
21714 ? eol_mnemonic_dos : eol_mnemonic_mac));
21718 if (eol_flag)
21720 /* Mention the EOL conversion if it is not the usual one. */
21721 if (STRINGP (eoltype))
21723 eol_str = SDATA (eoltype);
21724 eol_str_len = SBYTES (eoltype);
21726 else if (CHARACTERP (eoltype))
21728 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21729 int c = XFASTINT (eoltype);
21730 eol_str_len = CHAR_STRING (c, tmp);
21731 eol_str = tmp;
21733 else
21735 eol_str = invalid_eol_type;
21736 eol_str_len = sizeof (invalid_eol_type) - 1;
21738 memcpy (buf, eol_str, eol_str_len);
21739 buf += eol_str_len;
21742 return buf;
21745 /* Return a string for the output of a mode line %-spec for window W,
21746 generated by character C. FIELD_WIDTH > 0 means pad the string
21747 returned with spaces to that value. Return a Lisp string in
21748 *STRING if the resulting string is taken from that Lisp string.
21750 Note we operate on the current buffer for most purposes. */
21752 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21754 static const char *
21755 decode_mode_spec (struct window *w, register int c, int field_width,
21756 Lisp_Object *string)
21758 Lisp_Object obj;
21759 struct frame *f = XFRAME (WINDOW_FRAME (w));
21760 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21761 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21762 produce strings from numerical values, so limit preposterously
21763 large values of FIELD_WIDTH to avoid overrunning the buffer's
21764 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21765 bytes plus the terminating null. */
21766 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21767 struct buffer *b = current_buffer;
21769 obj = Qnil;
21770 *string = Qnil;
21772 switch (c)
21774 case '*':
21775 if (!NILP (BVAR (b, read_only)))
21776 return "%";
21777 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21778 return "*";
21779 return "-";
21781 case '+':
21782 /* This differs from %* only for a modified read-only buffer. */
21783 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21784 return "*";
21785 if (!NILP (BVAR (b, read_only)))
21786 return "%";
21787 return "-";
21789 case '&':
21790 /* This differs from %* in ignoring read-only-ness. */
21791 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21792 return "*";
21793 return "-";
21795 case '%':
21796 return "%";
21798 case '[':
21800 int i;
21801 char *p;
21803 if (command_loop_level > 5)
21804 return "[[[... ";
21805 p = decode_mode_spec_buf;
21806 for (i = 0; i < command_loop_level; i++)
21807 *p++ = '[';
21808 *p = 0;
21809 return decode_mode_spec_buf;
21812 case ']':
21814 int i;
21815 char *p;
21817 if (command_loop_level > 5)
21818 return " ...]]]";
21819 p = decode_mode_spec_buf;
21820 for (i = 0; i < command_loop_level; i++)
21821 *p++ = ']';
21822 *p = 0;
21823 return decode_mode_spec_buf;
21826 case '-':
21828 register int i;
21830 /* Let lots_of_dashes be a string of infinite length. */
21831 if (mode_line_target == MODE_LINE_NOPROP
21832 || mode_line_target == MODE_LINE_STRING)
21833 return "--";
21834 if (field_width <= 0
21835 || field_width > sizeof (lots_of_dashes))
21837 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21838 decode_mode_spec_buf[i] = '-';
21839 decode_mode_spec_buf[i] = '\0';
21840 return decode_mode_spec_buf;
21842 else
21843 return lots_of_dashes;
21846 case 'b':
21847 obj = BVAR (b, name);
21848 break;
21850 case 'c':
21851 /* %c and %l are ignored in `frame-title-format'.
21852 (In redisplay_internal, the frame title is drawn _before_ the
21853 windows are updated, so the stuff which depends on actual
21854 window contents (such as %l) may fail to render properly, or
21855 even crash emacs.) */
21856 if (mode_line_target == MODE_LINE_TITLE)
21857 return "";
21858 else
21860 ptrdiff_t col = current_column ();
21861 w->column_number_displayed = col;
21862 pint2str (decode_mode_spec_buf, width, col);
21863 return decode_mode_spec_buf;
21866 case 'e':
21867 #ifndef SYSTEM_MALLOC
21869 if (NILP (Vmemory_full))
21870 return "";
21871 else
21872 return "!MEM FULL! ";
21874 #else
21875 return "";
21876 #endif
21878 case 'F':
21879 /* %F displays the frame name. */
21880 if (!NILP (f->title))
21881 return SSDATA (f->title);
21882 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21883 return SSDATA (f->name);
21884 return "Emacs";
21886 case 'f':
21887 obj = BVAR (b, filename);
21888 break;
21890 case 'i':
21892 ptrdiff_t size = ZV - BEGV;
21893 pint2str (decode_mode_spec_buf, width, size);
21894 return decode_mode_spec_buf;
21897 case 'I':
21899 ptrdiff_t size = ZV - BEGV;
21900 pint2hrstr (decode_mode_spec_buf, width, size);
21901 return decode_mode_spec_buf;
21904 case 'l':
21906 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21907 ptrdiff_t topline, nlines, height;
21908 ptrdiff_t junk;
21910 /* %c and %l are ignored in `frame-title-format'. */
21911 if (mode_line_target == MODE_LINE_TITLE)
21912 return "";
21914 startpos = marker_position (w->start);
21915 startpos_byte = marker_byte_position (w->start);
21916 height = WINDOW_TOTAL_LINES (w);
21918 /* If we decided that this buffer isn't suitable for line numbers,
21919 don't forget that too fast. */
21920 if (w->base_line_pos == -1)
21921 goto no_value;
21923 /* If the buffer is very big, don't waste time. */
21924 if (INTEGERP (Vline_number_display_limit)
21925 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21927 w->base_line_pos = 0;
21928 w->base_line_number = 0;
21929 goto no_value;
21932 if (w->base_line_number > 0
21933 && w->base_line_pos > 0
21934 && w->base_line_pos <= startpos)
21936 line = w->base_line_number;
21937 linepos = w->base_line_pos;
21938 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21940 else
21942 line = 1;
21943 linepos = BUF_BEGV (b);
21944 linepos_byte = BUF_BEGV_BYTE (b);
21947 /* Count lines from base line to window start position. */
21948 nlines = display_count_lines (linepos_byte,
21949 startpos_byte,
21950 startpos, &junk);
21952 topline = nlines + line;
21954 /* Determine a new base line, if the old one is too close
21955 or too far away, or if we did not have one.
21956 "Too close" means it's plausible a scroll-down would
21957 go back past it. */
21958 if (startpos == BUF_BEGV (b))
21960 w->base_line_number = topline;
21961 w->base_line_pos = BUF_BEGV (b);
21963 else if (nlines < height + 25 || nlines > height * 3 + 50
21964 || linepos == BUF_BEGV (b))
21966 ptrdiff_t limit = BUF_BEGV (b);
21967 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21968 ptrdiff_t position;
21969 ptrdiff_t distance =
21970 (height * 2 + 30) * line_number_display_limit_width;
21972 if (startpos - distance > limit)
21974 limit = startpos - distance;
21975 limit_byte = CHAR_TO_BYTE (limit);
21978 nlines = display_count_lines (startpos_byte,
21979 limit_byte,
21980 - (height * 2 + 30),
21981 &position);
21982 /* If we couldn't find the lines we wanted within
21983 line_number_display_limit_width chars per line,
21984 give up on line numbers for this window. */
21985 if (position == limit_byte && limit == startpos - distance)
21987 w->base_line_pos = -1;
21988 w->base_line_number = 0;
21989 goto no_value;
21992 w->base_line_number = topline - nlines;
21993 w->base_line_pos = BYTE_TO_CHAR (position);
21996 /* Now count lines from the start pos to point. */
21997 nlines = display_count_lines (startpos_byte,
21998 PT_BYTE, PT, &junk);
22000 /* Record that we did display the line number. */
22001 line_number_displayed = 1;
22003 /* Make the string to show. */
22004 pint2str (decode_mode_spec_buf, width, topline + nlines);
22005 return decode_mode_spec_buf;
22006 no_value:
22008 char* p = decode_mode_spec_buf;
22009 int pad = width - 2;
22010 while (pad-- > 0)
22011 *p++ = ' ';
22012 *p++ = '?';
22013 *p++ = '?';
22014 *p = '\0';
22015 return decode_mode_spec_buf;
22018 break;
22020 case 'm':
22021 obj = BVAR (b, mode_name);
22022 break;
22024 case 'n':
22025 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22026 return " Narrow";
22027 break;
22029 case 'p':
22031 ptrdiff_t pos = marker_position (w->start);
22032 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22034 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22036 if (pos <= BUF_BEGV (b))
22037 return "All";
22038 else
22039 return "Bottom";
22041 else if (pos <= BUF_BEGV (b))
22042 return "Top";
22043 else
22045 if (total > 1000000)
22046 /* Do it differently for a large value, to avoid overflow. */
22047 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22048 else
22049 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22050 /* We can't normally display a 3-digit number,
22051 so get us a 2-digit number that is close. */
22052 if (total == 100)
22053 total = 99;
22054 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22055 return decode_mode_spec_buf;
22059 /* Display percentage of size above the bottom of the screen. */
22060 case 'P':
22062 ptrdiff_t toppos = marker_position (w->start);
22063 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22064 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22066 if (botpos >= BUF_ZV (b))
22068 if (toppos <= BUF_BEGV (b))
22069 return "All";
22070 else
22071 return "Bottom";
22073 else
22075 if (total > 1000000)
22076 /* Do it differently for a large value, to avoid overflow. */
22077 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22078 else
22079 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22080 /* We can't normally display a 3-digit number,
22081 so get us a 2-digit number that is close. */
22082 if (total == 100)
22083 total = 99;
22084 if (toppos <= BUF_BEGV (b))
22085 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22086 else
22087 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22088 return decode_mode_spec_buf;
22092 case 's':
22093 /* status of process */
22094 obj = Fget_buffer_process (Fcurrent_buffer ());
22095 if (NILP (obj))
22096 return "no process";
22097 #ifndef MSDOS
22098 obj = Fsymbol_name (Fprocess_status (obj));
22099 #endif
22100 break;
22102 case '@':
22104 ptrdiff_t count = inhibit_garbage_collection ();
22105 Lisp_Object val = call1 (intern ("file-remote-p"),
22106 BVAR (current_buffer, directory));
22107 unbind_to (count, Qnil);
22109 if (NILP (val))
22110 return "-";
22111 else
22112 return "@";
22115 case 'z':
22116 /* coding-system (not including end-of-line format) */
22117 case 'Z':
22118 /* coding-system (including end-of-line type) */
22120 int eol_flag = (c == 'Z');
22121 char *p = decode_mode_spec_buf;
22123 if (! FRAME_WINDOW_P (f))
22125 /* No need to mention EOL here--the terminal never needs
22126 to do EOL conversion. */
22127 p = decode_mode_spec_coding (CODING_ID_NAME
22128 (FRAME_KEYBOARD_CODING (f)->id),
22129 p, 0);
22130 p = decode_mode_spec_coding (CODING_ID_NAME
22131 (FRAME_TERMINAL_CODING (f)->id),
22132 p, 0);
22134 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22135 p, eol_flag);
22137 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22138 #ifdef subprocesses
22139 obj = Fget_buffer_process (Fcurrent_buffer ());
22140 if (PROCESSP (obj))
22142 p = decode_mode_spec_coding
22143 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22144 p = decode_mode_spec_coding
22145 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22147 #endif /* subprocesses */
22148 #endif /* 0 */
22149 *p = 0;
22150 return decode_mode_spec_buf;
22154 if (STRINGP (obj))
22156 *string = obj;
22157 return SSDATA (obj);
22159 else
22160 return "";
22164 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22165 means count lines back from START_BYTE. But don't go beyond
22166 LIMIT_BYTE. Return the number of lines thus found (always
22167 nonnegative).
22169 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22170 either the position COUNT lines after/before START_BYTE, if we
22171 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22172 COUNT lines. */
22174 static ptrdiff_t
22175 display_count_lines (ptrdiff_t start_byte,
22176 ptrdiff_t limit_byte, ptrdiff_t count,
22177 ptrdiff_t *byte_pos_ptr)
22179 register unsigned char *cursor;
22180 unsigned char *base;
22182 register ptrdiff_t ceiling;
22183 register unsigned char *ceiling_addr;
22184 ptrdiff_t orig_count = count;
22186 /* If we are not in selective display mode,
22187 check only for newlines. */
22188 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22189 && !INTEGERP (BVAR (current_buffer, selective_display)));
22191 if (count > 0)
22193 while (start_byte < limit_byte)
22195 ceiling = BUFFER_CEILING_OF (start_byte);
22196 ceiling = min (limit_byte - 1, ceiling);
22197 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22198 base = (cursor = BYTE_POS_ADDR (start_byte));
22202 if (selective_display)
22204 while (*cursor != '\n' && *cursor != 015
22205 && ++cursor != ceiling_addr)
22206 continue;
22207 if (cursor == ceiling_addr)
22208 break;
22210 else
22212 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22213 if (! cursor)
22214 break;
22217 cursor++;
22219 if (--count == 0)
22221 start_byte += cursor - base;
22222 *byte_pos_ptr = start_byte;
22223 return orig_count;
22226 while (cursor < ceiling_addr);
22228 start_byte += ceiling_addr - base;
22231 else
22233 while (start_byte > limit_byte)
22235 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22236 ceiling = max (limit_byte, ceiling);
22237 ceiling_addr = BYTE_POS_ADDR (ceiling);
22238 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22239 while (1)
22241 if (selective_display)
22243 while (--cursor >= ceiling_addr
22244 && *cursor != '\n' && *cursor != 015)
22245 continue;
22246 if (cursor < ceiling_addr)
22247 break;
22249 else
22251 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22252 if (! cursor)
22253 break;
22256 if (++count == 0)
22258 start_byte += cursor - base + 1;
22259 *byte_pos_ptr = start_byte;
22260 /* When scanning backwards, we should
22261 not count the newline posterior to which we stop. */
22262 return - orig_count - 1;
22265 start_byte += ceiling_addr - base;
22269 *byte_pos_ptr = limit_byte;
22271 if (count < 0)
22272 return - orig_count + count;
22273 return orig_count - count;
22279 /***********************************************************************
22280 Displaying strings
22281 ***********************************************************************/
22283 /* Display a NUL-terminated string, starting with index START.
22285 If STRING is non-null, display that C string. Otherwise, the Lisp
22286 string LISP_STRING is displayed. There's a case that STRING is
22287 non-null and LISP_STRING is not nil. It means STRING is a string
22288 data of LISP_STRING. In that case, we display LISP_STRING while
22289 ignoring its text properties.
22291 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22292 FACE_STRING. Display STRING or LISP_STRING with the face at
22293 FACE_STRING_POS in FACE_STRING:
22295 Display the string in the environment given by IT, but use the
22296 standard display table, temporarily.
22298 FIELD_WIDTH is the minimum number of output glyphs to produce.
22299 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22300 with spaces. If STRING has more characters, more than FIELD_WIDTH
22301 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22303 PRECISION is the maximum number of characters to output from
22304 STRING. PRECISION < 0 means don't truncate the string.
22306 This is roughly equivalent to printf format specifiers:
22308 FIELD_WIDTH PRECISION PRINTF
22309 ----------------------------------------
22310 -1 -1 %s
22311 -1 10 %.10s
22312 10 -1 %10s
22313 20 10 %20.10s
22315 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22316 display them, and < 0 means obey the current buffer's value of
22317 enable_multibyte_characters.
22319 Value is the number of columns displayed. */
22321 static int
22322 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22323 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22324 int field_width, int precision, int max_x, int multibyte)
22326 int hpos_at_start = it->hpos;
22327 int saved_face_id = it->face_id;
22328 struct glyph_row *row = it->glyph_row;
22329 ptrdiff_t it_charpos;
22331 /* Initialize the iterator IT for iteration over STRING beginning
22332 with index START. */
22333 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22334 precision, field_width, multibyte);
22335 if (string && STRINGP (lisp_string))
22336 /* LISP_STRING is the one returned by decode_mode_spec. We should
22337 ignore its text properties. */
22338 it->stop_charpos = it->end_charpos;
22340 /* If displaying STRING, set up the face of the iterator from
22341 FACE_STRING, if that's given. */
22342 if (STRINGP (face_string))
22344 ptrdiff_t endptr;
22345 struct face *face;
22347 it->face_id
22348 = face_at_string_position (it->w, face_string, face_string_pos,
22349 0, &endptr, it->base_face_id, 0);
22350 face = FACE_FROM_ID (it->f, it->face_id);
22351 it->face_box_p = face->box != FACE_NO_BOX;
22354 /* Set max_x to the maximum allowed X position. Don't let it go
22355 beyond the right edge of the window. */
22356 if (max_x <= 0)
22357 max_x = it->last_visible_x;
22358 else
22359 max_x = min (max_x, it->last_visible_x);
22361 /* Skip over display elements that are not visible. because IT->w is
22362 hscrolled. */
22363 if (it->current_x < it->first_visible_x)
22364 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22365 MOVE_TO_POS | MOVE_TO_X);
22367 row->ascent = it->max_ascent;
22368 row->height = it->max_ascent + it->max_descent;
22369 row->phys_ascent = it->max_phys_ascent;
22370 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22371 row->extra_line_spacing = it->max_extra_line_spacing;
22373 if (STRINGP (it->string))
22374 it_charpos = IT_STRING_CHARPOS (*it);
22375 else
22376 it_charpos = IT_CHARPOS (*it);
22378 /* This condition is for the case that we are called with current_x
22379 past last_visible_x. */
22380 while (it->current_x < max_x)
22382 int x_before, x, n_glyphs_before, i, nglyphs;
22384 /* Get the next display element. */
22385 if (!get_next_display_element (it))
22386 break;
22388 /* Produce glyphs. */
22389 x_before = it->current_x;
22390 n_glyphs_before = row->used[TEXT_AREA];
22391 PRODUCE_GLYPHS (it);
22393 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22394 i = 0;
22395 x = x_before;
22396 while (i < nglyphs)
22398 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22400 if (it->line_wrap != TRUNCATE
22401 && x + glyph->pixel_width > max_x)
22403 /* End of continued line or max_x reached. */
22404 if (CHAR_GLYPH_PADDING_P (*glyph))
22406 /* A wide character is unbreakable. */
22407 if (row->reversed_p)
22408 unproduce_glyphs (it, row->used[TEXT_AREA]
22409 - n_glyphs_before);
22410 row->used[TEXT_AREA] = n_glyphs_before;
22411 it->current_x = x_before;
22413 else
22415 if (row->reversed_p)
22416 unproduce_glyphs (it, row->used[TEXT_AREA]
22417 - (n_glyphs_before + i));
22418 row->used[TEXT_AREA] = n_glyphs_before + i;
22419 it->current_x = x;
22421 break;
22423 else if (x + glyph->pixel_width >= it->first_visible_x)
22425 /* Glyph is at least partially visible. */
22426 ++it->hpos;
22427 if (x < it->first_visible_x)
22428 row->x = x - it->first_visible_x;
22430 else
22432 /* Glyph is off the left margin of the display area.
22433 Should not happen. */
22434 emacs_abort ();
22437 row->ascent = max (row->ascent, it->max_ascent);
22438 row->height = max (row->height, it->max_ascent + it->max_descent);
22439 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22440 row->phys_height = max (row->phys_height,
22441 it->max_phys_ascent + it->max_phys_descent);
22442 row->extra_line_spacing = max (row->extra_line_spacing,
22443 it->max_extra_line_spacing);
22444 x += glyph->pixel_width;
22445 ++i;
22448 /* Stop if max_x reached. */
22449 if (i < nglyphs)
22450 break;
22452 /* Stop at line ends. */
22453 if (ITERATOR_AT_END_OF_LINE_P (it))
22455 it->continuation_lines_width = 0;
22456 break;
22459 set_iterator_to_next (it, 1);
22460 if (STRINGP (it->string))
22461 it_charpos = IT_STRING_CHARPOS (*it);
22462 else
22463 it_charpos = IT_CHARPOS (*it);
22465 /* Stop if truncating at the right edge. */
22466 if (it->line_wrap == TRUNCATE
22467 && it->current_x >= it->last_visible_x)
22469 /* Add truncation mark, but don't do it if the line is
22470 truncated at a padding space. */
22471 if (it_charpos < it->string_nchars)
22473 if (!FRAME_WINDOW_P (it->f))
22475 int ii, n;
22477 if (it->current_x > it->last_visible_x)
22479 if (!row->reversed_p)
22481 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22482 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22483 break;
22485 else
22487 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22488 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22489 break;
22490 unproduce_glyphs (it, ii + 1);
22491 ii = row->used[TEXT_AREA] - (ii + 1);
22493 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22495 row->used[TEXT_AREA] = ii;
22496 produce_special_glyphs (it, IT_TRUNCATION);
22499 produce_special_glyphs (it, IT_TRUNCATION);
22501 row->truncated_on_right_p = 1;
22503 break;
22507 /* Maybe insert a truncation at the left. */
22508 if (it->first_visible_x
22509 && it_charpos > 0)
22511 if (!FRAME_WINDOW_P (it->f)
22512 || (row->reversed_p
22513 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22514 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22515 insert_left_trunc_glyphs (it);
22516 row->truncated_on_left_p = 1;
22519 it->face_id = saved_face_id;
22521 /* Value is number of columns displayed. */
22522 return it->hpos - hpos_at_start;
22527 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22528 appears as an element of LIST or as the car of an element of LIST.
22529 If PROPVAL is a list, compare each element against LIST in that
22530 way, and return 1/2 if any element of PROPVAL is found in LIST.
22531 Otherwise return 0. This function cannot quit.
22532 The return value is 2 if the text is invisible but with an ellipsis
22533 and 1 if it's invisible and without an ellipsis. */
22536 invisible_p (register Lisp_Object propval, Lisp_Object list)
22538 register Lisp_Object tail, proptail;
22540 for (tail = list; CONSP (tail); tail = XCDR (tail))
22542 register Lisp_Object tem;
22543 tem = XCAR (tail);
22544 if (EQ (propval, tem))
22545 return 1;
22546 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22547 return NILP (XCDR (tem)) ? 1 : 2;
22550 if (CONSP (propval))
22552 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22554 Lisp_Object propelt;
22555 propelt = XCAR (proptail);
22556 for (tail = list; CONSP (tail); tail = XCDR (tail))
22558 register Lisp_Object tem;
22559 tem = XCAR (tail);
22560 if (EQ (propelt, tem))
22561 return 1;
22562 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22563 return NILP (XCDR (tem)) ? 1 : 2;
22568 return 0;
22571 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22572 doc: /* Non-nil if the property makes the text invisible.
22573 POS-OR-PROP can be a marker or number, in which case it is taken to be
22574 a position in the current buffer and the value of the `invisible' property
22575 is checked; or it can be some other value, which is then presumed to be the
22576 value of the `invisible' property of the text of interest.
22577 The non-nil value returned can be t for truly invisible text or something
22578 else if the text is replaced by an ellipsis. */)
22579 (Lisp_Object pos_or_prop)
22581 Lisp_Object prop
22582 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22583 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22584 : pos_or_prop);
22585 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22586 return (invis == 0 ? Qnil
22587 : invis == 1 ? Qt
22588 : make_number (invis));
22591 /* Calculate a width or height in pixels from a specification using
22592 the following elements:
22594 SPEC ::=
22595 NUM - a (fractional) multiple of the default font width/height
22596 (NUM) - specifies exactly NUM pixels
22597 UNIT - a fixed number of pixels, see below.
22598 ELEMENT - size of a display element in pixels, see below.
22599 (NUM . SPEC) - equals NUM * SPEC
22600 (+ SPEC SPEC ...) - add pixel values
22601 (- SPEC SPEC ...) - subtract pixel values
22602 (- SPEC) - negate pixel value
22604 NUM ::=
22605 INT or FLOAT - a number constant
22606 SYMBOL - use symbol's (buffer local) variable binding.
22608 UNIT ::=
22609 in - pixels per inch *)
22610 mm - pixels per 1/1000 meter *)
22611 cm - pixels per 1/100 meter *)
22612 width - width of current font in pixels.
22613 height - height of current font in pixels.
22615 *) using the ratio(s) defined in display-pixels-per-inch.
22617 ELEMENT ::=
22619 left-fringe - left fringe width in pixels
22620 right-fringe - right fringe width in pixels
22622 left-margin - left margin width in pixels
22623 right-margin - right margin width in pixels
22625 scroll-bar - scroll-bar area width in pixels
22627 Examples:
22629 Pixels corresponding to 5 inches:
22630 (5 . in)
22632 Total width of non-text areas on left side of window (if scroll-bar is on left):
22633 '(space :width (+ left-fringe left-margin scroll-bar))
22635 Align to first text column (in header line):
22636 '(space :align-to 0)
22638 Align to middle of text area minus half the width of variable `my-image'
22639 containing a loaded image:
22640 '(space :align-to (0.5 . (- text my-image)))
22642 Width of left margin minus width of 1 character in the default font:
22643 '(space :width (- left-margin 1))
22645 Width of left margin minus width of 2 characters in the current font:
22646 '(space :width (- left-margin (2 . width)))
22648 Center 1 character over left-margin (in header line):
22649 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22651 Different ways to express width of left fringe plus left margin minus one pixel:
22652 '(space :width (- (+ left-fringe left-margin) (1)))
22653 '(space :width (+ left-fringe left-margin (- (1))))
22654 '(space :width (+ left-fringe left-margin (-1)))
22658 static int
22659 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22660 struct font *font, int width_p, int *align_to)
22662 double pixels;
22664 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22665 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22667 if (NILP (prop))
22668 return OK_PIXELS (0);
22670 eassert (FRAME_LIVE_P (it->f));
22672 if (SYMBOLP (prop))
22674 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22676 char *unit = SSDATA (SYMBOL_NAME (prop));
22678 if (unit[0] == 'i' && unit[1] == 'n')
22679 pixels = 1.0;
22680 else if (unit[0] == 'm' && unit[1] == 'm')
22681 pixels = 25.4;
22682 else if (unit[0] == 'c' && unit[1] == 'm')
22683 pixels = 2.54;
22684 else
22685 pixels = 0;
22686 if (pixels > 0)
22688 double ppi = (width_p ? FRAME_RES_X (it->f)
22689 : FRAME_RES_Y (it->f));
22691 if (ppi > 0)
22692 return OK_PIXELS (ppi / pixels);
22693 return 0;
22697 #ifdef HAVE_WINDOW_SYSTEM
22698 if (EQ (prop, Qheight))
22699 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22700 if (EQ (prop, Qwidth))
22701 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22702 #else
22703 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22704 return OK_PIXELS (1);
22705 #endif
22707 if (EQ (prop, Qtext))
22708 return OK_PIXELS (width_p
22709 ? window_box_width (it->w, TEXT_AREA)
22710 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22712 if (align_to && *align_to < 0)
22714 *res = 0;
22715 if (EQ (prop, Qleft))
22716 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22717 if (EQ (prop, Qright))
22718 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22719 if (EQ (prop, Qcenter))
22720 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22721 + window_box_width (it->w, TEXT_AREA) / 2);
22722 if (EQ (prop, Qleft_fringe))
22723 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22724 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22725 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22726 if (EQ (prop, Qright_fringe))
22727 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22728 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22729 : window_box_right_offset (it->w, TEXT_AREA));
22730 if (EQ (prop, Qleft_margin))
22731 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22732 if (EQ (prop, Qright_margin))
22733 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22734 if (EQ (prop, Qscroll_bar))
22735 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22737 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22738 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22739 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22740 : 0)));
22742 else
22744 if (EQ (prop, Qleft_fringe))
22745 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22746 if (EQ (prop, Qright_fringe))
22747 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22748 if (EQ (prop, Qleft_margin))
22749 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22750 if (EQ (prop, Qright_margin))
22751 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22752 if (EQ (prop, Qscroll_bar))
22753 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22756 prop = buffer_local_value_1 (prop, it->w->contents);
22757 if (EQ (prop, Qunbound))
22758 prop = Qnil;
22761 if (INTEGERP (prop) || FLOATP (prop))
22763 int base_unit = (width_p
22764 ? FRAME_COLUMN_WIDTH (it->f)
22765 : FRAME_LINE_HEIGHT (it->f));
22766 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22769 if (CONSP (prop))
22771 Lisp_Object car = XCAR (prop);
22772 Lisp_Object cdr = XCDR (prop);
22774 if (SYMBOLP (car))
22776 #ifdef HAVE_WINDOW_SYSTEM
22777 if (FRAME_WINDOW_P (it->f)
22778 && valid_image_p (prop))
22780 ptrdiff_t id = lookup_image (it->f, prop);
22781 struct image *img = IMAGE_FROM_ID (it->f, id);
22783 return OK_PIXELS (width_p ? img->width : img->height);
22785 #endif
22786 if (EQ (car, Qplus) || EQ (car, Qminus))
22788 int first = 1;
22789 double px;
22791 pixels = 0;
22792 while (CONSP (cdr))
22794 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22795 font, width_p, align_to))
22796 return 0;
22797 if (first)
22798 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22799 else
22800 pixels += px;
22801 cdr = XCDR (cdr);
22803 if (EQ (car, Qminus))
22804 pixels = -pixels;
22805 return OK_PIXELS (pixels);
22808 car = buffer_local_value_1 (car, it->w->contents);
22809 if (EQ (car, Qunbound))
22810 car = Qnil;
22813 if (INTEGERP (car) || FLOATP (car))
22815 double fact;
22816 pixels = XFLOATINT (car);
22817 if (NILP (cdr))
22818 return OK_PIXELS (pixels);
22819 if (calc_pixel_width_or_height (&fact, it, cdr,
22820 font, width_p, align_to))
22821 return OK_PIXELS (pixels * fact);
22822 return 0;
22825 return 0;
22828 return 0;
22832 /***********************************************************************
22833 Glyph Display
22834 ***********************************************************************/
22836 #ifdef HAVE_WINDOW_SYSTEM
22838 #ifdef GLYPH_DEBUG
22840 void
22841 dump_glyph_string (struct glyph_string *s)
22843 fprintf (stderr, "glyph string\n");
22844 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22845 s->x, s->y, s->width, s->height);
22846 fprintf (stderr, " ybase = %d\n", s->ybase);
22847 fprintf (stderr, " hl = %d\n", s->hl);
22848 fprintf (stderr, " left overhang = %d, right = %d\n",
22849 s->left_overhang, s->right_overhang);
22850 fprintf (stderr, " nchars = %d\n", s->nchars);
22851 fprintf (stderr, " extends to end of line = %d\n",
22852 s->extends_to_end_of_line_p);
22853 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22854 fprintf (stderr, " bg width = %d\n", s->background_width);
22857 #endif /* GLYPH_DEBUG */
22859 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22860 of XChar2b structures for S; it can't be allocated in
22861 init_glyph_string because it must be allocated via `alloca'. W
22862 is the window on which S is drawn. ROW and AREA are the glyph row
22863 and area within the row from which S is constructed. START is the
22864 index of the first glyph structure covered by S. HL is a
22865 face-override for drawing S. */
22867 #ifdef HAVE_NTGUI
22868 #define OPTIONAL_HDC(hdc) HDC hdc,
22869 #define DECLARE_HDC(hdc) HDC hdc;
22870 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22871 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22872 #endif
22874 #ifndef OPTIONAL_HDC
22875 #define OPTIONAL_HDC(hdc)
22876 #define DECLARE_HDC(hdc)
22877 #define ALLOCATE_HDC(hdc, f)
22878 #define RELEASE_HDC(hdc, f)
22879 #endif
22881 static void
22882 init_glyph_string (struct glyph_string *s,
22883 OPTIONAL_HDC (hdc)
22884 XChar2b *char2b, struct window *w, struct glyph_row *row,
22885 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22887 memset (s, 0, sizeof *s);
22888 s->w = w;
22889 s->f = XFRAME (w->frame);
22890 #ifdef HAVE_NTGUI
22891 s->hdc = hdc;
22892 #endif
22893 s->display = FRAME_X_DISPLAY (s->f);
22894 s->window = FRAME_X_WINDOW (s->f);
22895 s->char2b = char2b;
22896 s->hl = hl;
22897 s->row = row;
22898 s->area = area;
22899 s->first_glyph = row->glyphs[area] + start;
22900 s->height = row->height;
22901 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22902 s->ybase = s->y + row->ascent;
22906 /* Append the list of glyph strings with head H and tail T to the list
22907 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22909 static void
22910 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22911 struct glyph_string *h, struct glyph_string *t)
22913 if (h)
22915 if (*head)
22916 (*tail)->next = h;
22917 else
22918 *head = h;
22919 h->prev = *tail;
22920 *tail = t;
22925 /* Prepend the list of glyph strings with head H and tail T to the
22926 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22927 result. */
22929 static void
22930 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22931 struct glyph_string *h, struct glyph_string *t)
22933 if (h)
22935 if (*head)
22936 (*head)->prev = t;
22937 else
22938 *tail = t;
22939 t->next = *head;
22940 *head = h;
22945 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22946 Set *HEAD and *TAIL to the resulting list. */
22948 static void
22949 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22950 struct glyph_string *s)
22952 s->next = s->prev = NULL;
22953 append_glyph_string_lists (head, tail, s, s);
22957 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22958 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22959 make sure that X resources for the face returned are allocated.
22960 Value is a pointer to a realized face that is ready for display if
22961 DISPLAY_P is non-zero. */
22963 static struct face *
22964 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22965 XChar2b *char2b, int display_p)
22967 struct face *face = FACE_FROM_ID (f, face_id);
22968 unsigned code = 0;
22970 if (face->font)
22972 code = face->font->driver->encode_char (face->font, c);
22974 if (code == FONT_INVALID_CODE)
22975 code = 0;
22977 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22979 /* Make sure X resources of the face are allocated. */
22980 #ifdef HAVE_X_WINDOWS
22981 if (display_p)
22982 #endif
22984 eassert (face != NULL);
22985 PREPARE_FACE_FOR_DISPLAY (f, face);
22988 return face;
22992 /* Get face and two-byte form of character glyph GLYPH on frame F.
22993 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22994 a pointer to a realized face that is ready for display. */
22996 static struct face *
22997 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22998 XChar2b *char2b, int *two_byte_p)
23000 struct face *face;
23001 unsigned code = 0;
23003 eassert (glyph->type == CHAR_GLYPH);
23004 face = FACE_FROM_ID (f, glyph->face_id);
23006 /* Make sure X resources of the face are allocated. */
23007 eassert (face != NULL);
23008 PREPARE_FACE_FOR_DISPLAY (f, face);
23010 if (two_byte_p)
23011 *two_byte_p = 0;
23013 if (face->font)
23015 if (CHAR_BYTE8_P (glyph->u.ch))
23016 code = CHAR_TO_BYTE8 (glyph->u.ch);
23017 else
23018 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23020 if (code == FONT_INVALID_CODE)
23021 code = 0;
23024 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23025 return face;
23029 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23030 Return 1 if FONT has a glyph for C, otherwise return 0. */
23032 static int
23033 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23035 unsigned code;
23037 if (CHAR_BYTE8_P (c))
23038 code = CHAR_TO_BYTE8 (c);
23039 else
23040 code = font->driver->encode_char (font, c);
23042 if (code == FONT_INVALID_CODE)
23043 return 0;
23044 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23045 return 1;
23049 /* Fill glyph string S with composition components specified by S->cmp.
23051 BASE_FACE is the base face of the composition.
23052 S->cmp_from is the index of the first component for S.
23054 OVERLAPS non-zero means S should draw the foreground only, and use
23055 its physical height for clipping. See also draw_glyphs.
23057 Value is the index of a component not in S. */
23059 static int
23060 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23061 int overlaps)
23063 int i;
23064 /* For all glyphs of this composition, starting at the offset
23065 S->cmp_from, until we reach the end of the definition or encounter a
23066 glyph that requires the different face, add it to S. */
23067 struct face *face;
23069 eassert (s);
23071 s->for_overlaps = overlaps;
23072 s->face = NULL;
23073 s->font = NULL;
23074 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23076 int c = COMPOSITION_GLYPH (s->cmp, i);
23078 /* TAB in a composition means display glyphs with padding space
23079 on the left or right. */
23080 if (c != '\t')
23082 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23083 -1, Qnil);
23085 face = get_char_face_and_encoding (s->f, c, face_id,
23086 s->char2b + i, 1);
23087 if (face)
23089 if (! s->face)
23091 s->face = face;
23092 s->font = s->face->font;
23094 else if (s->face != face)
23095 break;
23098 ++s->nchars;
23100 s->cmp_to = i;
23102 if (s->face == NULL)
23104 s->face = base_face->ascii_face;
23105 s->font = s->face->font;
23108 /* All glyph strings for the same composition has the same width,
23109 i.e. the width set for the first component of the composition. */
23110 s->width = s->first_glyph->pixel_width;
23112 /* If the specified font could not be loaded, use the frame's
23113 default font, but record the fact that we couldn't load it in
23114 the glyph string so that we can draw rectangles for the
23115 characters of the glyph string. */
23116 if (s->font == NULL)
23118 s->font_not_found_p = 1;
23119 s->font = FRAME_FONT (s->f);
23122 /* Adjust base line for subscript/superscript text. */
23123 s->ybase += s->first_glyph->voffset;
23125 /* This glyph string must always be drawn with 16-bit functions. */
23126 s->two_byte_p = 1;
23128 return s->cmp_to;
23131 static int
23132 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23133 int start, int end, int overlaps)
23135 struct glyph *glyph, *last;
23136 Lisp_Object lgstring;
23137 int i;
23139 s->for_overlaps = overlaps;
23140 glyph = s->row->glyphs[s->area] + start;
23141 last = s->row->glyphs[s->area] + end;
23142 s->cmp_id = glyph->u.cmp.id;
23143 s->cmp_from = glyph->slice.cmp.from;
23144 s->cmp_to = glyph->slice.cmp.to + 1;
23145 s->face = FACE_FROM_ID (s->f, face_id);
23146 lgstring = composition_gstring_from_id (s->cmp_id);
23147 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23148 glyph++;
23149 while (glyph < last
23150 && glyph->u.cmp.automatic
23151 && glyph->u.cmp.id == s->cmp_id
23152 && s->cmp_to == glyph->slice.cmp.from)
23153 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23155 for (i = s->cmp_from; i < s->cmp_to; i++)
23157 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23158 unsigned code = LGLYPH_CODE (lglyph);
23160 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23162 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23163 return glyph - s->row->glyphs[s->area];
23167 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23168 See the comment of fill_glyph_string for arguments.
23169 Value is the index of the first glyph not in S. */
23172 static int
23173 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23174 int start, int end, int overlaps)
23176 struct glyph *glyph, *last;
23177 int voffset;
23179 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23180 s->for_overlaps = overlaps;
23181 glyph = s->row->glyphs[s->area] + start;
23182 last = s->row->glyphs[s->area] + end;
23183 voffset = glyph->voffset;
23184 s->face = FACE_FROM_ID (s->f, face_id);
23185 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23186 s->nchars = 1;
23187 s->width = glyph->pixel_width;
23188 glyph++;
23189 while (glyph < last
23190 && glyph->type == GLYPHLESS_GLYPH
23191 && glyph->voffset == voffset
23192 && glyph->face_id == face_id)
23194 s->nchars++;
23195 s->width += glyph->pixel_width;
23196 glyph++;
23198 s->ybase += voffset;
23199 return glyph - s->row->glyphs[s->area];
23203 /* Fill glyph string S from a sequence of character glyphs.
23205 FACE_ID is the face id of the string. START is the index of the
23206 first glyph to consider, END is the index of the last + 1.
23207 OVERLAPS non-zero means S should draw the foreground only, and use
23208 its physical height for clipping. See also draw_glyphs.
23210 Value is the index of the first glyph not in S. */
23212 static int
23213 fill_glyph_string (struct glyph_string *s, int face_id,
23214 int start, int end, int overlaps)
23216 struct glyph *glyph, *last;
23217 int voffset;
23218 int glyph_not_available_p;
23220 eassert (s->f == XFRAME (s->w->frame));
23221 eassert (s->nchars == 0);
23222 eassert (start >= 0 && end > start);
23224 s->for_overlaps = overlaps;
23225 glyph = s->row->glyphs[s->area] + start;
23226 last = s->row->glyphs[s->area] + end;
23227 voffset = glyph->voffset;
23228 s->padding_p = glyph->padding_p;
23229 glyph_not_available_p = glyph->glyph_not_available_p;
23231 while (glyph < last
23232 && glyph->type == CHAR_GLYPH
23233 && glyph->voffset == voffset
23234 /* Same face id implies same font, nowadays. */
23235 && glyph->face_id == face_id
23236 && glyph->glyph_not_available_p == glyph_not_available_p)
23238 int two_byte_p;
23240 s->face = get_glyph_face_and_encoding (s->f, glyph,
23241 s->char2b + s->nchars,
23242 &two_byte_p);
23243 s->two_byte_p = two_byte_p;
23244 ++s->nchars;
23245 eassert (s->nchars <= end - start);
23246 s->width += glyph->pixel_width;
23247 if (glyph++->padding_p != s->padding_p)
23248 break;
23251 s->font = s->face->font;
23253 /* If the specified font could not be loaded, use the frame's font,
23254 but record the fact that we couldn't load it in
23255 S->font_not_found_p so that we can draw rectangles for the
23256 characters of the glyph string. */
23257 if (s->font == NULL || glyph_not_available_p)
23259 s->font_not_found_p = 1;
23260 s->font = FRAME_FONT (s->f);
23263 /* Adjust base line for subscript/superscript text. */
23264 s->ybase += voffset;
23266 eassert (s->face && s->face->gc);
23267 return glyph - s->row->glyphs[s->area];
23271 /* Fill glyph string S from image glyph S->first_glyph. */
23273 static void
23274 fill_image_glyph_string (struct glyph_string *s)
23276 eassert (s->first_glyph->type == IMAGE_GLYPH);
23277 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23278 eassert (s->img);
23279 s->slice = s->first_glyph->slice.img;
23280 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23281 s->font = s->face->font;
23282 s->width = s->first_glyph->pixel_width;
23284 /* Adjust base line for subscript/superscript text. */
23285 s->ybase += s->first_glyph->voffset;
23289 /* Fill glyph string S from a sequence of stretch glyphs.
23291 START is the index of the first glyph to consider,
23292 END is the index of the last + 1.
23294 Value is the index of the first glyph not in S. */
23296 static int
23297 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23299 struct glyph *glyph, *last;
23300 int voffset, face_id;
23302 eassert (s->first_glyph->type == STRETCH_GLYPH);
23304 glyph = s->row->glyphs[s->area] + start;
23305 last = s->row->glyphs[s->area] + end;
23306 face_id = glyph->face_id;
23307 s->face = FACE_FROM_ID (s->f, face_id);
23308 s->font = s->face->font;
23309 s->width = glyph->pixel_width;
23310 s->nchars = 1;
23311 voffset = glyph->voffset;
23313 for (++glyph;
23314 (glyph < last
23315 && glyph->type == STRETCH_GLYPH
23316 && glyph->voffset == voffset
23317 && glyph->face_id == face_id);
23318 ++glyph)
23319 s->width += glyph->pixel_width;
23321 /* Adjust base line for subscript/superscript text. */
23322 s->ybase += voffset;
23324 /* The case that face->gc == 0 is handled when drawing the glyph
23325 string by calling PREPARE_FACE_FOR_DISPLAY. */
23326 eassert (s->face);
23327 return glyph - s->row->glyphs[s->area];
23330 static struct font_metrics *
23331 get_per_char_metric (struct font *font, XChar2b *char2b)
23333 static struct font_metrics metrics;
23334 unsigned code;
23336 if (! font)
23337 return NULL;
23338 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23339 if (code == FONT_INVALID_CODE)
23340 return NULL;
23341 font->driver->text_extents (font, &code, 1, &metrics);
23342 return &metrics;
23345 /* EXPORT for RIF:
23346 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23347 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23348 assumed to be zero. */
23350 void
23351 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23353 *left = *right = 0;
23355 if (glyph->type == CHAR_GLYPH)
23357 struct face *face;
23358 XChar2b char2b;
23359 struct font_metrics *pcm;
23361 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23362 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23364 if (pcm->rbearing > pcm->width)
23365 *right = pcm->rbearing - pcm->width;
23366 if (pcm->lbearing < 0)
23367 *left = -pcm->lbearing;
23370 else if (glyph->type == COMPOSITE_GLYPH)
23372 if (! glyph->u.cmp.automatic)
23374 struct composition *cmp = composition_table[glyph->u.cmp.id];
23376 if (cmp->rbearing > cmp->pixel_width)
23377 *right = cmp->rbearing - cmp->pixel_width;
23378 if (cmp->lbearing < 0)
23379 *left = - cmp->lbearing;
23381 else
23383 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23384 struct font_metrics metrics;
23386 composition_gstring_width (gstring, glyph->slice.cmp.from,
23387 glyph->slice.cmp.to + 1, &metrics);
23388 if (metrics.rbearing > metrics.width)
23389 *right = metrics.rbearing - metrics.width;
23390 if (metrics.lbearing < 0)
23391 *left = - metrics.lbearing;
23397 /* Return the index of the first glyph preceding glyph string S that
23398 is overwritten by S because of S's left overhang. Value is -1
23399 if no glyphs are overwritten. */
23401 static int
23402 left_overwritten (struct glyph_string *s)
23404 int k;
23406 if (s->left_overhang)
23408 int x = 0, i;
23409 struct glyph *glyphs = s->row->glyphs[s->area];
23410 int first = s->first_glyph - glyphs;
23412 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23413 x -= glyphs[i].pixel_width;
23415 k = i + 1;
23417 else
23418 k = -1;
23420 return k;
23424 /* Return the index of the first glyph preceding glyph string S that
23425 is overwriting S because of its right overhang. Value is -1 if no
23426 glyph in front of S overwrites S. */
23428 static int
23429 left_overwriting (struct glyph_string *s)
23431 int i, k, x;
23432 struct glyph *glyphs = s->row->glyphs[s->area];
23433 int first = s->first_glyph - glyphs;
23435 k = -1;
23436 x = 0;
23437 for (i = first - 1; i >= 0; --i)
23439 int left, right;
23440 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23441 if (x + right > 0)
23442 k = i;
23443 x -= glyphs[i].pixel_width;
23446 return k;
23450 /* Return the index of the last glyph following glyph string S that is
23451 overwritten by S because of S's right overhang. Value is -1 if
23452 no such glyph is found. */
23454 static int
23455 right_overwritten (struct glyph_string *s)
23457 int k = -1;
23459 if (s->right_overhang)
23461 int x = 0, i;
23462 struct glyph *glyphs = s->row->glyphs[s->area];
23463 int first = (s->first_glyph - glyphs
23464 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23465 int end = s->row->used[s->area];
23467 for (i = first; i < end && s->right_overhang > x; ++i)
23468 x += glyphs[i].pixel_width;
23470 k = i;
23473 return k;
23477 /* Return the index of the last glyph following glyph string S that
23478 overwrites S because of its left overhang. Value is negative
23479 if no such glyph is found. */
23481 static int
23482 right_overwriting (struct glyph_string *s)
23484 int i, k, x;
23485 int end = s->row->used[s->area];
23486 struct glyph *glyphs = s->row->glyphs[s->area];
23487 int first = (s->first_glyph - glyphs
23488 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23490 k = -1;
23491 x = 0;
23492 for (i = first; i < end; ++i)
23494 int left, right;
23495 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23496 if (x - left < 0)
23497 k = i;
23498 x += glyphs[i].pixel_width;
23501 return k;
23505 /* Set background width of glyph string S. START is the index of the
23506 first glyph following S. LAST_X is the right-most x-position + 1
23507 in the drawing area. */
23509 static void
23510 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23512 /* If the face of this glyph string has to be drawn to the end of
23513 the drawing area, set S->extends_to_end_of_line_p. */
23515 if (start == s->row->used[s->area]
23516 && s->area == TEXT_AREA
23517 && ((s->row->fill_line_p
23518 && (s->hl == DRAW_NORMAL_TEXT
23519 || s->hl == DRAW_IMAGE_RAISED
23520 || s->hl == DRAW_IMAGE_SUNKEN))
23521 || s->hl == DRAW_MOUSE_FACE))
23522 s->extends_to_end_of_line_p = 1;
23524 /* If S extends its face to the end of the line, set its
23525 background_width to the distance to the right edge of the drawing
23526 area. */
23527 if (s->extends_to_end_of_line_p)
23528 s->background_width = last_x - s->x + 1;
23529 else
23530 s->background_width = s->width;
23534 /* Compute overhangs and x-positions for glyph string S and its
23535 predecessors, or successors. X is the starting x-position for S.
23536 BACKWARD_P non-zero means process predecessors. */
23538 static void
23539 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23541 if (backward_p)
23543 while (s)
23545 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23546 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23547 x -= s->width;
23548 s->x = x;
23549 s = s->prev;
23552 else
23554 while (s)
23556 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23557 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23558 s->x = x;
23559 x += s->width;
23560 s = s->next;
23567 /* The following macros are only called from draw_glyphs below.
23568 They reference the following parameters of that function directly:
23569 `w', `row', `area', and `overlap_p'
23570 as well as the following local variables:
23571 `s', `f', and `hdc' (in W32) */
23573 #ifdef HAVE_NTGUI
23574 /* On W32, silently add local `hdc' variable to argument list of
23575 init_glyph_string. */
23576 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23577 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23578 #else
23579 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23580 init_glyph_string (s, char2b, w, row, area, start, hl)
23581 #endif
23583 /* Add a glyph string for a stretch glyph to the list of strings
23584 between HEAD and TAIL. START is the index of the stretch glyph in
23585 row area AREA of glyph row ROW. END is the index of the last glyph
23586 in that glyph row area. X is the current output position assigned
23587 to the new glyph string constructed. HL overrides that face of the
23588 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23589 is the right-most x-position of the drawing area. */
23591 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23592 and below -- keep them on one line. */
23593 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23594 do \
23596 s = alloca (sizeof *s); \
23597 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23598 START = fill_stretch_glyph_string (s, START, END); \
23599 append_glyph_string (&HEAD, &TAIL, s); \
23600 s->x = (X); \
23602 while (0)
23605 /* Add a glyph string for an image glyph to the list of strings
23606 between HEAD and TAIL. START is the index of the image glyph in
23607 row area AREA of glyph row ROW. END is the index of the last glyph
23608 in that glyph row area. X is the current output position assigned
23609 to the new glyph string constructed. HL overrides that face of the
23610 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23611 is the right-most x-position of the drawing area. */
23613 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23614 do \
23616 s = alloca (sizeof *s); \
23617 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23618 fill_image_glyph_string (s); \
23619 append_glyph_string (&HEAD, &TAIL, s); \
23620 ++START; \
23621 s->x = (X); \
23623 while (0)
23626 /* Add a glyph string for a sequence of character glyphs to the list
23627 of strings between HEAD and TAIL. START is the index of the first
23628 glyph in row area AREA of glyph row ROW that is part of the new
23629 glyph string. END is the index of the last glyph in that glyph row
23630 area. X is the current output position assigned to the new glyph
23631 string constructed. HL overrides that face of the glyph; e.g. it
23632 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23633 right-most x-position of the drawing area. */
23635 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23636 do \
23638 int face_id; \
23639 XChar2b *char2b; \
23641 face_id = (row)->glyphs[area][START].face_id; \
23643 s = alloca (sizeof *s); \
23644 char2b = alloca ((END - START) * sizeof *char2b); \
23645 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23646 append_glyph_string (&HEAD, &TAIL, s); \
23647 s->x = (X); \
23648 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23650 while (0)
23653 /* Add a glyph string for a composite sequence to the list of strings
23654 between HEAD and TAIL. START is the index of the first glyph in
23655 row area AREA of glyph row ROW that is part of the new glyph
23656 string. END is the index of the last glyph in that glyph row area.
23657 X is the current output position assigned to the new glyph string
23658 constructed. HL overrides that face of the glyph; e.g. it is
23659 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23660 x-position of the drawing area. */
23662 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23663 do { \
23664 int face_id = (row)->glyphs[area][START].face_id; \
23665 struct face *base_face = FACE_FROM_ID (f, face_id); \
23666 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23667 struct composition *cmp = composition_table[cmp_id]; \
23668 XChar2b *char2b; \
23669 struct glyph_string *first_s = NULL; \
23670 int n; \
23672 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23674 /* Make glyph_strings for each glyph sequence that is drawable by \
23675 the same face, and append them to HEAD/TAIL. */ \
23676 for (n = 0; n < cmp->glyph_len;) \
23678 s = alloca (sizeof *s); \
23679 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23680 append_glyph_string (&(HEAD), &(TAIL), s); \
23681 s->cmp = cmp; \
23682 s->cmp_from = n; \
23683 s->x = (X); \
23684 if (n == 0) \
23685 first_s = s; \
23686 n = fill_composite_glyph_string (s, base_face, overlaps); \
23689 ++START; \
23690 s = first_s; \
23691 } while (0)
23694 /* Add a glyph string for a glyph-string sequence to the list of strings
23695 between HEAD and TAIL. */
23697 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23698 do { \
23699 int face_id; \
23700 XChar2b *char2b; \
23701 Lisp_Object gstring; \
23703 face_id = (row)->glyphs[area][START].face_id; \
23704 gstring = (composition_gstring_from_id \
23705 ((row)->glyphs[area][START].u.cmp.id)); \
23706 s = alloca (sizeof *s); \
23707 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23708 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23709 append_glyph_string (&(HEAD), &(TAIL), s); \
23710 s->x = (X); \
23711 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23712 } while (0)
23715 /* Add a glyph string for a sequence of glyphless character's glyphs
23716 to the list of strings between HEAD and TAIL. The meanings of
23717 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23719 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23720 do \
23722 int face_id; \
23724 face_id = (row)->glyphs[area][START].face_id; \
23726 s = alloca (sizeof *s); \
23727 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23728 append_glyph_string (&HEAD, &TAIL, s); \
23729 s->x = (X); \
23730 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23731 overlaps); \
23733 while (0)
23736 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23737 of AREA of glyph row ROW on window W between indices START and END.
23738 HL overrides the face for drawing glyph strings, e.g. it is
23739 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23740 x-positions of the drawing area.
23742 This is an ugly monster macro construct because we must use alloca
23743 to allocate glyph strings (because draw_glyphs can be called
23744 asynchronously). */
23746 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23747 do \
23749 HEAD = TAIL = NULL; \
23750 while (START < END) \
23752 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23753 switch (first_glyph->type) \
23755 case CHAR_GLYPH: \
23756 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23757 HL, X, LAST_X); \
23758 break; \
23760 case COMPOSITE_GLYPH: \
23761 if (first_glyph->u.cmp.automatic) \
23762 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23763 HL, X, LAST_X); \
23764 else \
23765 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23766 HL, X, LAST_X); \
23767 break; \
23769 case STRETCH_GLYPH: \
23770 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23771 HL, X, LAST_X); \
23772 break; \
23774 case IMAGE_GLYPH: \
23775 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23776 HL, X, LAST_X); \
23777 break; \
23779 case GLYPHLESS_GLYPH: \
23780 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23781 HL, X, LAST_X); \
23782 break; \
23784 default: \
23785 emacs_abort (); \
23788 if (s) \
23790 set_glyph_string_background_width (s, START, LAST_X); \
23791 (X) += s->width; \
23794 } while (0)
23797 /* Draw glyphs between START and END in AREA of ROW on window W,
23798 starting at x-position X. X is relative to AREA in W. HL is a
23799 face-override with the following meaning:
23801 DRAW_NORMAL_TEXT draw normally
23802 DRAW_CURSOR draw in cursor face
23803 DRAW_MOUSE_FACE draw in mouse face.
23804 DRAW_INVERSE_VIDEO draw in mode line face
23805 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23806 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23808 If OVERLAPS is non-zero, draw only the foreground of characters and
23809 clip to the physical height of ROW. Non-zero value also defines
23810 the overlapping part to be drawn:
23812 OVERLAPS_PRED overlap with preceding rows
23813 OVERLAPS_SUCC overlap with succeeding rows
23814 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23815 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23817 Value is the x-position reached, relative to AREA of W. */
23819 static int
23820 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23821 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23822 enum draw_glyphs_face hl, int overlaps)
23824 struct glyph_string *head, *tail;
23825 struct glyph_string *s;
23826 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23827 int i, j, x_reached, last_x, area_left = 0;
23828 struct frame *f = XFRAME (WINDOW_FRAME (w));
23829 DECLARE_HDC (hdc);
23831 ALLOCATE_HDC (hdc, f);
23833 /* Let's rather be paranoid than getting a SEGV. */
23834 end = min (end, row->used[area]);
23835 start = clip_to_bounds (0, start, end);
23837 /* Translate X to frame coordinates. Set last_x to the right
23838 end of the drawing area. */
23839 if (row->full_width_p)
23841 /* X is relative to the left edge of W, without scroll bars
23842 or fringes. */
23843 area_left = WINDOW_LEFT_EDGE_X (w);
23844 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23846 else
23848 area_left = window_box_left (w, area);
23849 last_x = area_left + window_box_width (w, area);
23851 x += area_left;
23853 /* Build a doubly-linked list of glyph_string structures between
23854 head and tail from what we have to draw. Note that the macro
23855 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23856 the reason we use a separate variable `i'. */
23857 i = start;
23858 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23859 if (tail)
23860 x_reached = tail->x + tail->background_width;
23861 else
23862 x_reached = x;
23864 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23865 the row, redraw some glyphs in front or following the glyph
23866 strings built above. */
23867 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23869 struct glyph_string *h, *t;
23870 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23871 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23872 int check_mouse_face = 0;
23873 int dummy_x = 0;
23875 /* If mouse highlighting is on, we may need to draw adjacent
23876 glyphs using mouse-face highlighting. */
23877 if (area == TEXT_AREA && row->mouse_face_p
23878 && hlinfo->mouse_face_beg_row >= 0
23879 && hlinfo->mouse_face_end_row >= 0)
23881 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23883 if (row_vpos >= hlinfo->mouse_face_beg_row
23884 && row_vpos <= hlinfo->mouse_face_end_row)
23886 check_mouse_face = 1;
23887 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23888 ? hlinfo->mouse_face_beg_col : 0;
23889 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23890 ? hlinfo->mouse_face_end_col
23891 : row->used[TEXT_AREA];
23895 /* Compute overhangs for all glyph strings. */
23896 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23897 for (s = head; s; s = s->next)
23898 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23900 /* Prepend glyph strings for glyphs in front of the first glyph
23901 string that are overwritten because of the first glyph
23902 string's left overhang. The background of all strings
23903 prepended must be drawn because the first glyph string
23904 draws over it. */
23905 i = left_overwritten (head);
23906 if (i >= 0)
23908 enum draw_glyphs_face overlap_hl;
23910 /* If this row contains mouse highlighting, attempt to draw
23911 the overlapped glyphs with the correct highlight. This
23912 code fails if the overlap encompasses more than one glyph
23913 and mouse-highlight spans only some of these glyphs.
23914 However, making it work perfectly involves a lot more
23915 code, and I don't know if the pathological case occurs in
23916 practice, so we'll stick to this for now. --- cyd */
23917 if (check_mouse_face
23918 && mouse_beg_col < start && mouse_end_col > i)
23919 overlap_hl = DRAW_MOUSE_FACE;
23920 else
23921 overlap_hl = DRAW_NORMAL_TEXT;
23923 j = i;
23924 BUILD_GLYPH_STRINGS (j, start, h, t,
23925 overlap_hl, dummy_x, last_x);
23926 start = i;
23927 compute_overhangs_and_x (t, head->x, 1);
23928 prepend_glyph_string_lists (&head, &tail, h, t);
23929 clip_head = head;
23932 /* Prepend glyph strings for glyphs in front of the first glyph
23933 string that overwrite that glyph string because of their
23934 right overhang. For these strings, only the foreground must
23935 be drawn, because it draws over the glyph string at `head'.
23936 The background must not be drawn because this would overwrite
23937 right overhangs of preceding glyphs for which no glyph
23938 strings exist. */
23939 i = left_overwriting (head);
23940 if (i >= 0)
23942 enum draw_glyphs_face overlap_hl;
23944 if (check_mouse_face
23945 && mouse_beg_col < start && mouse_end_col > i)
23946 overlap_hl = DRAW_MOUSE_FACE;
23947 else
23948 overlap_hl = DRAW_NORMAL_TEXT;
23950 clip_head = head;
23951 BUILD_GLYPH_STRINGS (i, start, h, t,
23952 overlap_hl, dummy_x, last_x);
23953 for (s = h; s; s = s->next)
23954 s->background_filled_p = 1;
23955 compute_overhangs_and_x (t, head->x, 1);
23956 prepend_glyph_string_lists (&head, &tail, h, t);
23959 /* Append glyphs strings for glyphs following the last glyph
23960 string tail that are overwritten by tail. The background of
23961 these strings has to be drawn because tail's foreground draws
23962 over it. */
23963 i = right_overwritten (tail);
23964 if (i >= 0)
23966 enum draw_glyphs_face overlap_hl;
23968 if (check_mouse_face
23969 && mouse_beg_col < i && mouse_end_col > end)
23970 overlap_hl = DRAW_MOUSE_FACE;
23971 else
23972 overlap_hl = DRAW_NORMAL_TEXT;
23974 BUILD_GLYPH_STRINGS (end, i, h, t,
23975 overlap_hl, x, last_x);
23976 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23977 we don't have `end = i;' here. */
23978 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23979 append_glyph_string_lists (&head, &tail, h, t);
23980 clip_tail = tail;
23983 /* Append glyph strings for glyphs following the last glyph
23984 string tail that overwrite tail. The foreground of such
23985 glyphs has to be drawn because it writes into the background
23986 of tail. The background must not be drawn because it could
23987 paint over the foreground of following glyphs. */
23988 i = right_overwriting (tail);
23989 if (i >= 0)
23991 enum draw_glyphs_face overlap_hl;
23992 if (check_mouse_face
23993 && mouse_beg_col < i && mouse_end_col > end)
23994 overlap_hl = DRAW_MOUSE_FACE;
23995 else
23996 overlap_hl = DRAW_NORMAL_TEXT;
23998 clip_tail = tail;
23999 i++; /* We must include the Ith glyph. */
24000 BUILD_GLYPH_STRINGS (end, i, h, t,
24001 overlap_hl, x, last_x);
24002 for (s = h; s; s = s->next)
24003 s->background_filled_p = 1;
24004 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24005 append_glyph_string_lists (&head, &tail, h, t);
24007 if (clip_head || clip_tail)
24008 for (s = head; s; s = s->next)
24010 s->clip_head = clip_head;
24011 s->clip_tail = clip_tail;
24015 /* Draw all strings. */
24016 for (s = head; s; s = s->next)
24017 FRAME_RIF (f)->draw_glyph_string (s);
24019 #ifndef HAVE_NS
24020 /* When focus a sole frame and move horizontally, this sets on_p to 0
24021 causing a failure to erase prev cursor position. */
24022 if (area == TEXT_AREA
24023 && !row->full_width_p
24024 /* When drawing overlapping rows, only the glyph strings'
24025 foreground is drawn, which doesn't erase a cursor
24026 completely. */
24027 && !overlaps)
24029 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24030 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24031 : (tail ? tail->x + tail->background_width : x));
24032 x0 -= area_left;
24033 x1 -= area_left;
24035 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24036 row->y, MATRIX_ROW_BOTTOM_Y (row));
24038 #endif
24040 /* Value is the x-position up to which drawn, relative to AREA of W.
24041 This doesn't include parts drawn because of overhangs. */
24042 if (row->full_width_p)
24043 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24044 else
24045 x_reached -= area_left;
24047 RELEASE_HDC (hdc, f);
24049 return x_reached;
24052 /* Expand row matrix if too narrow. Don't expand if area
24053 is not present. */
24055 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24057 if (!it->f->fonts_changed \
24058 && (it->glyph_row->glyphs[area] \
24059 < it->glyph_row->glyphs[area + 1])) \
24061 it->w->ncols_scale_factor++; \
24062 it->f->fonts_changed = 1; \
24066 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24067 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24069 static void
24070 append_glyph (struct it *it)
24072 struct glyph *glyph;
24073 enum glyph_row_area area = it->area;
24075 eassert (it->glyph_row);
24076 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24078 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24079 if (glyph < it->glyph_row->glyphs[area + 1])
24081 /* If the glyph row is reversed, we need to prepend the glyph
24082 rather than append it. */
24083 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24085 struct glyph *g;
24087 /* Make room for the additional glyph. */
24088 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24089 g[1] = *g;
24090 glyph = it->glyph_row->glyphs[area];
24092 glyph->charpos = CHARPOS (it->position);
24093 glyph->object = it->object;
24094 if (it->pixel_width > 0)
24096 glyph->pixel_width = it->pixel_width;
24097 glyph->padding_p = 0;
24099 else
24101 /* Assure at least 1-pixel width. Otherwise, cursor can't
24102 be displayed correctly. */
24103 glyph->pixel_width = 1;
24104 glyph->padding_p = 1;
24106 glyph->ascent = it->ascent;
24107 glyph->descent = it->descent;
24108 glyph->voffset = it->voffset;
24109 glyph->type = CHAR_GLYPH;
24110 glyph->avoid_cursor_p = it->avoid_cursor_p;
24111 glyph->multibyte_p = it->multibyte_p;
24112 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24114 /* In R2L rows, the left and the right box edges need to be
24115 drawn in reverse direction. */
24116 glyph->right_box_line_p = it->start_of_box_run_p;
24117 glyph->left_box_line_p = it->end_of_box_run_p;
24119 else
24121 glyph->left_box_line_p = it->start_of_box_run_p;
24122 glyph->right_box_line_p = it->end_of_box_run_p;
24124 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24125 || it->phys_descent > it->descent);
24126 glyph->glyph_not_available_p = it->glyph_not_available_p;
24127 glyph->face_id = it->face_id;
24128 glyph->u.ch = it->char_to_display;
24129 glyph->slice.img = null_glyph_slice;
24130 glyph->font_type = FONT_TYPE_UNKNOWN;
24131 if (it->bidi_p)
24133 glyph->resolved_level = it->bidi_it.resolved_level;
24134 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24135 emacs_abort ();
24136 glyph->bidi_type = it->bidi_it.type;
24138 else
24140 glyph->resolved_level = 0;
24141 glyph->bidi_type = UNKNOWN_BT;
24143 ++it->glyph_row->used[area];
24145 else
24146 IT_EXPAND_MATRIX_WIDTH (it, area);
24149 /* Store one glyph for the composition IT->cmp_it.id in
24150 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24151 non-null. */
24153 static void
24154 append_composite_glyph (struct it *it)
24156 struct glyph *glyph;
24157 enum glyph_row_area area = it->area;
24159 eassert (it->glyph_row);
24161 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24162 if (glyph < it->glyph_row->glyphs[area + 1])
24164 /* If the glyph row is reversed, we need to prepend the glyph
24165 rather than append it. */
24166 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24168 struct glyph *g;
24170 /* Make room for the new glyph. */
24171 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24172 g[1] = *g;
24173 glyph = it->glyph_row->glyphs[it->area];
24175 glyph->charpos = it->cmp_it.charpos;
24176 glyph->object = it->object;
24177 glyph->pixel_width = it->pixel_width;
24178 glyph->ascent = it->ascent;
24179 glyph->descent = it->descent;
24180 glyph->voffset = it->voffset;
24181 glyph->type = COMPOSITE_GLYPH;
24182 if (it->cmp_it.ch < 0)
24184 glyph->u.cmp.automatic = 0;
24185 glyph->u.cmp.id = it->cmp_it.id;
24186 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24188 else
24190 glyph->u.cmp.automatic = 1;
24191 glyph->u.cmp.id = it->cmp_it.id;
24192 glyph->slice.cmp.from = it->cmp_it.from;
24193 glyph->slice.cmp.to = it->cmp_it.to - 1;
24195 glyph->avoid_cursor_p = it->avoid_cursor_p;
24196 glyph->multibyte_p = it->multibyte_p;
24197 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24199 /* In R2L rows, the left and the right box edges need to be
24200 drawn in reverse direction. */
24201 glyph->right_box_line_p = it->start_of_box_run_p;
24202 glyph->left_box_line_p = it->end_of_box_run_p;
24204 else
24206 glyph->left_box_line_p = it->start_of_box_run_p;
24207 glyph->right_box_line_p = it->end_of_box_run_p;
24209 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24210 || it->phys_descent > it->descent);
24211 glyph->padding_p = 0;
24212 glyph->glyph_not_available_p = 0;
24213 glyph->face_id = it->face_id;
24214 glyph->font_type = FONT_TYPE_UNKNOWN;
24215 if (it->bidi_p)
24217 glyph->resolved_level = it->bidi_it.resolved_level;
24218 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24219 emacs_abort ();
24220 glyph->bidi_type = it->bidi_it.type;
24222 ++it->glyph_row->used[area];
24224 else
24225 IT_EXPAND_MATRIX_WIDTH (it, area);
24229 /* Change IT->ascent and IT->height according to the setting of
24230 IT->voffset. */
24232 static void
24233 take_vertical_position_into_account (struct it *it)
24235 if (it->voffset)
24237 if (it->voffset < 0)
24238 /* Increase the ascent so that we can display the text higher
24239 in the line. */
24240 it->ascent -= it->voffset;
24241 else
24242 /* Increase the descent so that we can display the text lower
24243 in the line. */
24244 it->descent += it->voffset;
24249 /* Produce glyphs/get display metrics for the image IT is loaded with.
24250 See the description of struct display_iterator in dispextern.h for
24251 an overview of struct display_iterator. */
24253 static void
24254 produce_image_glyph (struct it *it)
24256 struct image *img;
24257 struct face *face;
24258 int glyph_ascent, crop;
24259 struct glyph_slice slice;
24261 eassert (it->what == IT_IMAGE);
24263 face = FACE_FROM_ID (it->f, it->face_id);
24264 eassert (face);
24265 /* Make sure X resources of the face is loaded. */
24266 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24268 if (it->image_id < 0)
24270 /* Fringe bitmap. */
24271 it->ascent = it->phys_ascent = 0;
24272 it->descent = it->phys_descent = 0;
24273 it->pixel_width = 0;
24274 it->nglyphs = 0;
24275 return;
24278 img = IMAGE_FROM_ID (it->f, it->image_id);
24279 eassert (img);
24280 /* Make sure X resources of the image is loaded. */
24281 prepare_image_for_display (it->f, img);
24283 slice.x = slice.y = 0;
24284 slice.width = img->width;
24285 slice.height = img->height;
24287 if (INTEGERP (it->slice.x))
24288 slice.x = XINT (it->slice.x);
24289 else if (FLOATP (it->slice.x))
24290 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24292 if (INTEGERP (it->slice.y))
24293 slice.y = XINT (it->slice.y);
24294 else if (FLOATP (it->slice.y))
24295 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24297 if (INTEGERP (it->slice.width))
24298 slice.width = XINT (it->slice.width);
24299 else if (FLOATP (it->slice.width))
24300 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24302 if (INTEGERP (it->slice.height))
24303 slice.height = XINT (it->slice.height);
24304 else if (FLOATP (it->slice.height))
24305 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24307 if (slice.x >= img->width)
24308 slice.x = img->width;
24309 if (slice.y >= img->height)
24310 slice.y = img->height;
24311 if (slice.x + slice.width >= img->width)
24312 slice.width = img->width - slice.x;
24313 if (slice.y + slice.height > img->height)
24314 slice.height = img->height - slice.y;
24316 if (slice.width == 0 || slice.height == 0)
24317 return;
24319 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24321 it->descent = slice.height - glyph_ascent;
24322 if (slice.y == 0)
24323 it->descent += img->vmargin;
24324 if (slice.y + slice.height == img->height)
24325 it->descent += img->vmargin;
24326 it->phys_descent = it->descent;
24328 it->pixel_width = slice.width;
24329 if (slice.x == 0)
24330 it->pixel_width += img->hmargin;
24331 if (slice.x + slice.width == img->width)
24332 it->pixel_width += img->hmargin;
24334 /* It's quite possible for images to have an ascent greater than
24335 their height, so don't get confused in that case. */
24336 if (it->descent < 0)
24337 it->descent = 0;
24339 it->nglyphs = 1;
24341 if (face->box != FACE_NO_BOX)
24343 if (face->box_line_width > 0)
24345 if (slice.y == 0)
24346 it->ascent += face->box_line_width;
24347 if (slice.y + slice.height == img->height)
24348 it->descent += face->box_line_width;
24351 if (it->start_of_box_run_p && slice.x == 0)
24352 it->pixel_width += eabs (face->box_line_width);
24353 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24354 it->pixel_width += eabs (face->box_line_width);
24357 take_vertical_position_into_account (it);
24359 /* Automatically crop wide image glyphs at right edge so we can
24360 draw the cursor on same display row. */
24361 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24362 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24364 it->pixel_width -= crop;
24365 slice.width -= crop;
24368 if (it->glyph_row)
24370 struct glyph *glyph;
24371 enum glyph_row_area area = it->area;
24373 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24374 if (glyph < it->glyph_row->glyphs[area + 1])
24376 glyph->charpos = CHARPOS (it->position);
24377 glyph->object = it->object;
24378 glyph->pixel_width = it->pixel_width;
24379 glyph->ascent = glyph_ascent;
24380 glyph->descent = it->descent;
24381 glyph->voffset = it->voffset;
24382 glyph->type = IMAGE_GLYPH;
24383 glyph->avoid_cursor_p = it->avoid_cursor_p;
24384 glyph->multibyte_p = it->multibyte_p;
24385 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24387 /* In R2L rows, the left and the right box edges need to be
24388 drawn in reverse direction. */
24389 glyph->right_box_line_p = it->start_of_box_run_p;
24390 glyph->left_box_line_p = it->end_of_box_run_p;
24392 else
24394 glyph->left_box_line_p = it->start_of_box_run_p;
24395 glyph->right_box_line_p = it->end_of_box_run_p;
24397 glyph->overlaps_vertically_p = 0;
24398 glyph->padding_p = 0;
24399 glyph->glyph_not_available_p = 0;
24400 glyph->face_id = it->face_id;
24401 glyph->u.img_id = img->id;
24402 glyph->slice.img = slice;
24403 glyph->font_type = FONT_TYPE_UNKNOWN;
24404 if (it->bidi_p)
24406 glyph->resolved_level = it->bidi_it.resolved_level;
24407 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24408 emacs_abort ();
24409 glyph->bidi_type = it->bidi_it.type;
24411 ++it->glyph_row->used[area];
24413 else
24414 IT_EXPAND_MATRIX_WIDTH (it, area);
24419 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24420 of the glyph, WIDTH and HEIGHT are the width and height of the
24421 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24423 static void
24424 append_stretch_glyph (struct it *it, Lisp_Object object,
24425 int width, int height, int ascent)
24427 struct glyph *glyph;
24428 enum glyph_row_area area = it->area;
24430 eassert (ascent >= 0 && ascent <= height);
24432 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24433 if (glyph < it->glyph_row->glyphs[area + 1])
24435 /* If the glyph row is reversed, we need to prepend the glyph
24436 rather than append it. */
24437 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24439 struct glyph *g;
24441 /* Make room for the additional glyph. */
24442 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24443 g[1] = *g;
24444 glyph = it->glyph_row->glyphs[area];
24446 glyph->charpos = CHARPOS (it->position);
24447 glyph->object = object;
24448 glyph->pixel_width = width;
24449 glyph->ascent = ascent;
24450 glyph->descent = height - ascent;
24451 glyph->voffset = it->voffset;
24452 glyph->type = STRETCH_GLYPH;
24453 glyph->avoid_cursor_p = it->avoid_cursor_p;
24454 glyph->multibyte_p = it->multibyte_p;
24455 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24457 /* In R2L rows, the left and the right box edges need to be
24458 drawn in reverse direction. */
24459 glyph->right_box_line_p = it->start_of_box_run_p;
24460 glyph->left_box_line_p = it->end_of_box_run_p;
24462 else
24464 glyph->left_box_line_p = it->start_of_box_run_p;
24465 glyph->right_box_line_p = it->end_of_box_run_p;
24467 glyph->overlaps_vertically_p = 0;
24468 glyph->padding_p = 0;
24469 glyph->glyph_not_available_p = 0;
24470 glyph->face_id = it->face_id;
24471 glyph->u.stretch.ascent = ascent;
24472 glyph->u.stretch.height = height;
24473 glyph->slice.img = null_glyph_slice;
24474 glyph->font_type = FONT_TYPE_UNKNOWN;
24475 if (it->bidi_p)
24477 glyph->resolved_level = it->bidi_it.resolved_level;
24478 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24479 emacs_abort ();
24480 glyph->bidi_type = it->bidi_it.type;
24482 else
24484 glyph->resolved_level = 0;
24485 glyph->bidi_type = UNKNOWN_BT;
24487 ++it->glyph_row->used[area];
24489 else
24490 IT_EXPAND_MATRIX_WIDTH (it, area);
24493 #endif /* HAVE_WINDOW_SYSTEM */
24495 /* Produce a stretch glyph for iterator IT. IT->object is the value
24496 of the glyph property displayed. The value must be a list
24497 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24498 being recognized:
24500 1. `:width WIDTH' specifies that the space should be WIDTH *
24501 canonical char width wide. WIDTH may be an integer or floating
24502 point number.
24504 2. `:relative-width FACTOR' specifies that the width of the stretch
24505 should be computed from the width of the first character having the
24506 `glyph' property, and should be FACTOR times that width.
24508 3. `:align-to HPOS' specifies that the space should be wide enough
24509 to reach HPOS, a value in canonical character units.
24511 Exactly one of the above pairs must be present.
24513 4. `:height HEIGHT' specifies that the height of the stretch produced
24514 should be HEIGHT, measured in canonical character units.
24516 5. `:relative-height FACTOR' specifies that the height of the
24517 stretch should be FACTOR times the height of the characters having
24518 the glyph property.
24520 Either none or exactly one of 4 or 5 must be present.
24522 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24523 of the stretch should be used for the ascent of the stretch.
24524 ASCENT must be in the range 0 <= ASCENT <= 100. */
24526 void
24527 produce_stretch_glyph (struct it *it)
24529 /* (space :width WIDTH :height HEIGHT ...) */
24530 Lisp_Object prop, plist;
24531 int width = 0, height = 0, align_to = -1;
24532 int zero_width_ok_p = 0;
24533 double tem;
24534 struct font *font = NULL;
24536 #ifdef HAVE_WINDOW_SYSTEM
24537 int ascent = 0;
24538 int zero_height_ok_p = 0;
24540 if (FRAME_WINDOW_P (it->f))
24542 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24543 font = face->font ? face->font : FRAME_FONT (it->f);
24544 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24546 #endif
24548 /* List should start with `space'. */
24549 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24550 plist = XCDR (it->object);
24552 /* Compute the width of the stretch. */
24553 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24554 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24556 /* Absolute width `:width WIDTH' specified and valid. */
24557 zero_width_ok_p = 1;
24558 width = (int)tem;
24560 #ifdef HAVE_WINDOW_SYSTEM
24561 else if (FRAME_WINDOW_P (it->f)
24562 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24564 /* Relative width `:relative-width FACTOR' specified and valid.
24565 Compute the width of the characters having the `glyph'
24566 property. */
24567 struct it it2;
24568 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24570 it2 = *it;
24571 if (it->multibyte_p)
24572 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24573 else
24575 it2.c = it2.char_to_display = *p, it2.len = 1;
24576 if (! ASCII_CHAR_P (it2.c))
24577 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24580 it2.glyph_row = NULL;
24581 it2.what = IT_CHARACTER;
24582 x_produce_glyphs (&it2);
24583 width = NUMVAL (prop) * it2.pixel_width;
24585 #endif /* HAVE_WINDOW_SYSTEM */
24586 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24587 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24589 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24590 align_to = (align_to < 0
24592 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24593 else if (align_to < 0)
24594 align_to = window_box_left_offset (it->w, TEXT_AREA);
24595 width = max (0, (int)tem + align_to - it->current_x);
24596 zero_width_ok_p = 1;
24598 else
24599 /* Nothing specified -> width defaults to canonical char width. */
24600 width = FRAME_COLUMN_WIDTH (it->f);
24602 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24603 width = 1;
24605 #ifdef HAVE_WINDOW_SYSTEM
24606 /* Compute height. */
24607 if (FRAME_WINDOW_P (it->f))
24609 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24610 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24612 height = (int)tem;
24613 zero_height_ok_p = 1;
24615 else if (prop = Fplist_get (plist, QCrelative_height),
24616 NUMVAL (prop) > 0)
24617 height = FONT_HEIGHT (font) * NUMVAL (prop);
24618 else
24619 height = FONT_HEIGHT (font);
24621 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24622 height = 1;
24624 /* Compute percentage of height used for ascent. If
24625 `:ascent ASCENT' is present and valid, use that. Otherwise,
24626 derive the ascent from the font in use. */
24627 if (prop = Fplist_get (plist, QCascent),
24628 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24629 ascent = height * NUMVAL (prop) / 100.0;
24630 else if (!NILP (prop)
24631 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24632 ascent = min (max (0, (int)tem), height);
24633 else
24634 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24636 else
24637 #endif /* HAVE_WINDOW_SYSTEM */
24638 height = 1;
24640 if (width > 0 && it->line_wrap != TRUNCATE
24641 && it->current_x + width > it->last_visible_x)
24643 width = it->last_visible_x - it->current_x;
24644 #ifdef HAVE_WINDOW_SYSTEM
24645 /* Subtract one more pixel from the stretch width, but only on
24646 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24647 width -= FRAME_WINDOW_P (it->f);
24648 #endif
24651 if (width > 0 && height > 0 && it->glyph_row)
24653 Lisp_Object o_object = it->object;
24654 Lisp_Object object = it->stack[it->sp - 1].string;
24655 int n = width;
24657 if (!STRINGP (object))
24658 object = it->w->contents;
24659 #ifdef HAVE_WINDOW_SYSTEM
24660 if (FRAME_WINDOW_P (it->f))
24661 append_stretch_glyph (it, object, width, height, ascent);
24662 else
24663 #endif
24665 it->object = object;
24666 it->char_to_display = ' ';
24667 it->pixel_width = it->len = 1;
24668 while (n--)
24669 tty_append_glyph (it);
24670 it->object = o_object;
24674 it->pixel_width = width;
24675 #ifdef HAVE_WINDOW_SYSTEM
24676 if (FRAME_WINDOW_P (it->f))
24678 it->ascent = it->phys_ascent = ascent;
24679 it->descent = it->phys_descent = height - it->ascent;
24680 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24681 take_vertical_position_into_account (it);
24683 else
24684 #endif
24685 it->nglyphs = width;
24688 /* Get information about special display element WHAT in an
24689 environment described by IT. WHAT is one of IT_TRUNCATION or
24690 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24691 non-null glyph_row member. This function ensures that fields like
24692 face_id, c, len of IT are left untouched. */
24694 static void
24695 produce_special_glyphs (struct it *it, enum display_element_type what)
24697 struct it temp_it;
24698 Lisp_Object gc;
24699 GLYPH glyph;
24701 temp_it = *it;
24702 temp_it.object = make_number (0);
24703 memset (&temp_it.current, 0, sizeof temp_it.current);
24705 if (what == IT_CONTINUATION)
24707 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24708 if (it->bidi_it.paragraph_dir == R2L)
24709 SET_GLYPH_FROM_CHAR (glyph, '/');
24710 else
24711 SET_GLYPH_FROM_CHAR (glyph, '\\');
24712 if (it->dp
24713 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24715 /* FIXME: Should we mirror GC for R2L lines? */
24716 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24717 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24720 else if (what == IT_TRUNCATION)
24722 /* Truncation glyph. */
24723 SET_GLYPH_FROM_CHAR (glyph, '$');
24724 if (it->dp
24725 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24727 /* FIXME: Should we mirror GC for R2L lines? */
24728 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24729 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24732 else
24733 emacs_abort ();
24735 #ifdef HAVE_WINDOW_SYSTEM
24736 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24737 is turned off, we precede the truncation/continuation glyphs by a
24738 stretch glyph whose width is computed such that these special
24739 glyphs are aligned at the window margin, even when very different
24740 fonts are used in different glyph rows. */
24741 if (FRAME_WINDOW_P (temp_it.f)
24742 /* init_iterator calls this with it->glyph_row == NULL, and it
24743 wants only the pixel width of the truncation/continuation
24744 glyphs. */
24745 && temp_it.glyph_row
24746 /* insert_left_trunc_glyphs calls us at the beginning of the
24747 row, and it has its own calculation of the stretch glyph
24748 width. */
24749 && temp_it.glyph_row->used[TEXT_AREA] > 0
24750 && (temp_it.glyph_row->reversed_p
24751 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24752 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24754 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24756 if (stretch_width > 0)
24758 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24759 struct font *font =
24760 face->font ? face->font : FRAME_FONT (temp_it.f);
24761 int stretch_ascent =
24762 (((temp_it.ascent + temp_it.descent)
24763 * FONT_BASE (font)) / FONT_HEIGHT (font));
24765 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24766 temp_it.ascent + temp_it.descent,
24767 stretch_ascent);
24770 #endif
24772 temp_it.dp = NULL;
24773 temp_it.what = IT_CHARACTER;
24774 temp_it.len = 1;
24775 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24776 temp_it.face_id = GLYPH_FACE (glyph);
24777 temp_it.len = CHAR_BYTES (temp_it.c);
24779 PRODUCE_GLYPHS (&temp_it);
24780 it->pixel_width = temp_it.pixel_width;
24781 it->nglyphs = temp_it.pixel_width;
24784 #ifdef HAVE_WINDOW_SYSTEM
24786 /* Calculate line-height and line-spacing properties.
24787 An integer value specifies explicit pixel value.
24788 A float value specifies relative value to current face height.
24789 A cons (float . face-name) specifies relative value to
24790 height of specified face font.
24792 Returns height in pixels, or nil. */
24795 static Lisp_Object
24796 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24797 int boff, int override)
24799 Lisp_Object face_name = Qnil;
24800 int ascent, descent, height;
24802 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24803 return val;
24805 if (CONSP (val))
24807 face_name = XCAR (val);
24808 val = XCDR (val);
24809 if (!NUMBERP (val))
24810 val = make_number (1);
24811 if (NILP (face_name))
24813 height = it->ascent + it->descent;
24814 goto scale;
24818 if (NILP (face_name))
24820 font = FRAME_FONT (it->f);
24821 boff = FRAME_BASELINE_OFFSET (it->f);
24823 else if (EQ (face_name, Qt))
24825 override = 0;
24827 else
24829 int face_id;
24830 struct face *face;
24832 face_id = lookup_named_face (it->f, face_name, 0);
24833 if (face_id < 0)
24834 return make_number (-1);
24836 face = FACE_FROM_ID (it->f, face_id);
24837 font = face->font;
24838 if (font == NULL)
24839 return make_number (-1);
24840 boff = font->baseline_offset;
24841 if (font->vertical_centering)
24842 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24845 ascent = FONT_BASE (font) + boff;
24846 descent = FONT_DESCENT (font) - boff;
24848 if (override)
24850 it->override_ascent = ascent;
24851 it->override_descent = descent;
24852 it->override_boff = boff;
24855 height = ascent + descent;
24857 scale:
24858 if (FLOATP (val))
24859 height = (int)(XFLOAT_DATA (val) * height);
24860 else if (INTEGERP (val))
24861 height *= XINT (val);
24863 return make_number (height);
24867 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24868 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24869 and only if this is for a character for which no font was found.
24871 If the display method (it->glyphless_method) is
24872 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24873 length of the acronym or the hexadecimal string, UPPER_XOFF and
24874 UPPER_YOFF are pixel offsets for the upper part of the string,
24875 LOWER_XOFF and LOWER_YOFF are for the lower part.
24877 For the other display methods, LEN through LOWER_YOFF are zero. */
24879 static void
24880 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24881 short upper_xoff, short upper_yoff,
24882 short lower_xoff, short lower_yoff)
24884 struct glyph *glyph;
24885 enum glyph_row_area area = it->area;
24887 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24888 if (glyph < it->glyph_row->glyphs[area + 1])
24890 /* If the glyph row is reversed, we need to prepend the glyph
24891 rather than append it. */
24892 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24894 struct glyph *g;
24896 /* Make room for the additional glyph. */
24897 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24898 g[1] = *g;
24899 glyph = it->glyph_row->glyphs[area];
24901 glyph->charpos = CHARPOS (it->position);
24902 glyph->object = it->object;
24903 glyph->pixel_width = it->pixel_width;
24904 glyph->ascent = it->ascent;
24905 glyph->descent = it->descent;
24906 glyph->voffset = it->voffset;
24907 glyph->type = GLYPHLESS_GLYPH;
24908 glyph->u.glyphless.method = it->glyphless_method;
24909 glyph->u.glyphless.for_no_font = for_no_font;
24910 glyph->u.glyphless.len = len;
24911 glyph->u.glyphless.ch = it->c;
24912 glyph->slice.glyphless.upper_xoff = upper_xoff;
24913 glyph->slice.glyphless.upper_yoff = upper_yoff;
24914 glyph->slice.glyphless.lower_xoff = lower_xoff;
24915 glyph->slice.glyphless.lower_yoff = lower_yoff;
24916 glyph->avoid_cursor_p = it->avoid_cursor_p;
24917 glyph->multibyte_p = it->multibyte_p;
24918 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24920 /* In R2L rows, the left and the right box edges need to be
24921 drawn in reverse direction. */
24922 glyph->right_box_line_p = it->start_of_box_run_p;
24923 glyph->left_box_line_p = it->end_of_box_run_p;
24925 else
24927 glyph->left_box_line_p = it->start_of_box_run_p;
24928 glyph->right_box_line_p = it->end_of_box_run_p;
24930 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24931 || it->phys_descent > it->descent);
24932 glyph->padding_p = 0;
24933 glyph->glyph_not_available_p = 0;
24934 glyph->face_id = face_id;
24935 glyph->font_type = FONT_TYPE_UNKNOWN;
24936 if (it->bidi_p)
24938 glyph->resolved_level = it->bidi_it.resolved_level;
24939 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24940 emacs_abort ();
24941 glyph->bidi_type = it->bidi_it.type;
24943 ++it->glyph_row->used[area];
24945 else
24946 IT_EXPAND_MATRIX_WIDTH (it, area);
24950 /* Produce a glyph for a glyphless character for iterator IT.
24951 IT->glyphless_method specifies which method to use for displaying
24952 the character. See the description of enum
24953 glyphless_display_method in dispextern.h for the detail.
24955 FOR_NO_FONT is nonzero if and only if this is for a character for
24956 which no font was found. ACRONYM, if non-nil, is an acronym string
24957 for the character. */
24959 static void
24960 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24962 int face_id;
24963 struct face *face;
24964 struct font *font;
24965 int base_width, base_height, width, height;
24966 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24967 int len;
24969 /* Get the metrics of the base font. We always refer to the current
24970 ASCII face. */
24971 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24972 font = face->font ? face->font : FRAME_FONT (it->f);
24973 it->ascent = FONT_BASE (font) + font->baseline_offset;
24974 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24975 base_height = it->ascent + it->descent;
24976 base_width = font->average_width;
24978 face_id = merge_glyphless_glyph_face (it);
24980 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24982 it->pixel_width = THIN_SPACE_WIDTH;
24983 len = 0;
24984 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24986 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24988 width = CHAR_WIDTH (it->c);
24989 if (width == 0)
24990 width = 1;
24991 else if (width > 4)
24992 width = 4;
24993 it->pixel_width = base_width * width;
24994 len = 0;
24995 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24997 else
24999 char buf[7];
25000 const char *str;
25001 unsigned int code[6];
25002 int upper_len;
25003 int ascent, descent;
25004 struct font_metrics metrics_upper, metrics_lower;
25006 face = FACE_FROM_ID (it->f, face_id);
25007 font = face->font ? face->font : FRAME_FONT (it->f);
25008 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25010 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25012 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25013 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25014 if (CONSP (acronym))
25015 acronym = XCAR (acronym);
25016 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25018 else
25020 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25021 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25022 str = buf;
25024 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25025 code[len] = font->driver->encode_char (font, str[len]);
25026 upper_len = (len + 1) / 2;
25027 font->driver->text_extents (font, code, upper_len,
25028 &metrics_upper);
25029 font->driver->text_extents (font, code + upper_len, len - upper_len,
25030 &metrics_lower);
25034 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25035 width = max (metrics_upper.width, metrics_lower.width) + 4;
25036 upper_xoff = upper_yoff = 2; /* the typical case */
25037 if (base_width >= width)
25039 /* Align the upper to the left, the lower to the right. */
25040 it->pixel_width = base_width;
25041 lower_xoff = base_width - 2 - metrics_lower.width;
25043 else
25045 /* Center the shorter one. */
25046 it->pixel_width = width;
25047 if (metrics_upper.width >= metrics_lower.width)
25048 lower_xoff = (width - metrics_lower.width) / 2;
25049 else
25051 /* FIXME: This code doesn't look right. It formerly was
25052 missing the "lower_xoff = 0;", which couldn't have
25053 been right since it left lower_xoff uninitialized. */
25054 lower_xoff = 0;
25055 upper_xoff = (width - metrics_upper.width) / 2;
25059 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25060 top, bottom, and between upper and lower strings. */
25061 height = (metrics_upper.ascent + metrics_upper.descent
25062 + metrics_lower.ascent + metrics_lower.descent) + 5;
25063 /* Center vertically.
25064 H:base_height, D:base_descent
25065 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25067 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25068 descent = D - H/2 + h/2;
25069 lower_yoff = descent - 2 - ld;
25070 upper_yoff = lower_yoff - la - 1 - ud; */
25071 ascent = - (it->descent - (base_height + height + 1) / 2);
25072 descent = it->descent - (base_height - height) / 2;
25073 lower_yoff = descent - 2 - metrics_lower.descent;
25074 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25075 - metrics_upper.descent);
25076 /* Don't make the height shorter than the base height. */
25077 if (height > base_height)
25079 it->ascent = ascent;
25080 it->descent = descent;
25084 it->phys_ascent = it->ascent;
25085 it->phys_descent = it->descent;
25086 if (it->glyph_row)
25087 append_glyphless_glyph (it, face_id, for_no_font, len,
25088 upper_xoff, upper_yoff,
25089 lower_xoff, lower_yoff);
25090 it->nglyphs = 1;
25091 take_vertical_position_into_account (it);
25095 /* RIF:
25096 Produce glyphs/get display metrics for the display element IT is
25097 loaded with. See the description of struct it in dispextern.h
25098 for an overview of struct it. */
25100 void
25101 x_produce_glyphs (struct it *it)
25103 int extra_line_spacing = it->extra_line_spacing;
25105 it->glyph_not_available_p = 0;
25107 if (it->what == IT_CHARACTER)
25109 XChar2b char2b;
25110 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25111 struct font *font = face->font;
25112 struct font_metrics *pcm = NULL;
25113 int boff; /* Baseline offset. */
25115 if (font == NULL)
25117 /* When no suitable font is found, display this character by
25118 the method specified in the first extra slot of
25119 Vglyphless_char_display. */
25120 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25122 eassert (it->what == IT_GLYPHLESS);
25123 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25124 goto done;
25127 boff = font->baseline_offset;
25128 if (font->vertical_centering)
25129 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25131 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25133 int stretched_p;
25135 it->nglyphs = 1;
25137 if (it->override_ascent >= 0)
25139 it->ascent = it->override_ascent;
25140 it->descent = it->override_descent;
25141 boff = it->override_boff;
25143 else
25145 it->ascent = FONT_BASE (font) + boff;
25146 it->descent = FONT_DESCENT (font) - boff;
25149 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25151 pcm = get_per_char_metric (font, &char2b);
25152 if (pcm->width == 0
25153 && pcm->rbearing == 0 && pcm->lbearing == 0)
25154 pcm = NULL;
25157 if (pcm)
25159 it->phys_ascent = pcm->ascent + boff;
25160 it->phys_descent = pcm->descent - boff;
25161 it->pixel_width = pcm->width;
25163 else
25165 it->glyph_not_available_p = 1;
25166 it->phys_ascent = it->ascent;
25167 it->phys_descent = it->descent;
25168 it->pixel_width = font->space_width;
25171 if (it->constrain_row_ascent_descent_p)
25173 if (it->descent > it->max_descent)
25175 it->ascent += it->descent - it->max_descent;
25176 it->descent = it->max_descent;
25178 if (it->ascent > it->max_ascent)
25180 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25181 it->ascent = it->max_ascent;
25183 it->phys_ascent = min (it->phys_ascent, it->ascent);
25184 it->phys_descent = min (it->phys_descent, it->descent);
25185 extra_line_spacing = 0;
25188 /* If this is a space inside a region of text with
25189 `space-width' property, change its width. */
25190 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25191 if (stretched_p)
25192 it->pixel_width *= XFLOATINT (it->space_width);
25194 /* If face has a box, add the box thickness to the character
25195 height. If character has a box line to the left and/or
25196 right, add the box line width to the character's width. */
25197 if (face->box != FACE_NO_BOX)
25199 int thick = face->box_line_width;
25201 if (thick > 0)
25203 it->ascent += thick;
25204 it->descent += thick;
25206 else
25207 thick = -thick;
25209 if (it->start_of_box_run_p)
25210 it->pixel_width += thick;
25211 if (it->end_of_box_run_p)
25212 it->pixel_width += thick;
25215 /* If face has an overline, add the height of the overline
25216 (1 pixel) and a 1 pixel margin to the character height. */
25217 if (face->overline_p)
25218 it->ascent += overline_margin;
25220 if (it->constrain_row_ascent_descent_p)
25222 if (it->ascent > it->max_ascent)
25223 it->ascent = it->max_ascent;
25224 if (it->descent > it->max_descent)
25225 it->descent = it->max_descent;
25228 take_vertical_position_into_account (it);
25230 /* If we have to actually produce glyphs, do it. */
25231 if (it->glyph_row)
25233 if (stretched_p)
25235 /* Translate a space with a `space-width' property
25236 into a stretch glyph. */
25237 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25238 / FONT_HEIGHT (font));
25239 append_stretch_glyph (it, it->object, it->pixel_width,
25240 it->ascent + it->descent, ascent);
25242 else
25243 append_glyph (it);
25245 /* If characters with lbearing or rbearing are displayed
25246 in this line, record that fact in a flag of the
25247 glyph row. This is used to optimize X output code. */
25248 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25249 it->glyph_row->contains_overlapping_glyphs_p = 1;
25251 if (! stretched_p && it->pixel_width == 0)
25252 /* We assure that all visible glyphs have at least 1-pixel
25253 width. */
25254 it->pixel_width = 1;
25256 else if (it->char_to_display == '\n')
25258 /* A newline has no width, but we need the height of the
25259 line. But if previous part of the line sets a height,
25260 don't increase that height. */
25262 Lisp_Object height;
25263 Lisp_Object total_height = Qnil;
25265 it->override_ascent = -1;
25266 it->pixel_width = 0;
25267 it->nglyphs = 0;
25269 height = get_it_property (it, Qline_height);
25270 /* Split (line-height total-height) list. */
25271 if (CONSP (height)
25272 && CONSP (XCDR (height))
25273 && NILP (XCDR (XCDR (height))))
25275 total_height = XCAR (XCDR (height));
25276 height = XCAR (height);
25278 height = calc_line_height_property (it, height, font, boff, 1);
25280 if (it->override_ascent >= 0)
25282 it->ascent = it->override_ascent;
25283 it->descent = it->override_descent;
25284 boff = it->override_boff;
25286 else
25288 it->ascent = FONT_BASE (font) + boff;
25289 it->descent = FONT_DESCENT (font) - boff;
25292 if (EQ (height, Qt))
25294 if (it->descent > it->max_descent)
25296 it->ascent += it->descent - it->max_descent;
25297 it->descent = it->max_descent;
25299 if (it->ascent > it->max_ascent)
25301 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25302 it->ascent = it->max_ascent;
25304 it->phys_ascent = min (it->phys_ascent, it->ascent);
25305 it->phys_descent = min (it->phys_descent, it->descent);
25306 it->constrain_row_ascent_descent_p = 1;
25307 extra_line_spacing = 0;
25309 else
25311 Lisp_Object spacing;
25313 it->phys_ascent = it->ascent;
25314 it->phys_descent = it->descent;
25316 if ((it->max_ascent > 0 || it->max_descent > 0)
25317 && face->box != FACE_NO_BOX
25318 && face->box_line_width > 0)
25320 it->ascent += face->box_line_width;
25321 it->descent += face->box_line_width;
25323 if (!NILP (height)
25324 && XINT (height) > it->ascent + it->descent)
25325 it->ascent = XINT (height) - it->descent;
25327 if (!NILP (total_height))
25328 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25329 else
25331 spacing = get_it_property (it, Qline_spacing);
25332 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25334 if (INTEGERP (spacing))
25336 extra_line_spacing = XINT (spacing);
25337 if (!NILP (total_height))
25338 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25342 else /* i.e. (it->char_to_display == '\t') */
25344 if (font->space_width > 0)
25346 int tab_width = it->tab_width * font->space_width;
25347 int x = it->current_x + it->continuation_lines_width;
25348 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25350 /* If the distance from the current position to the next tab
25351 stop is less than a space character width, use the
25352 tab stop after that. */
25353 if (next_tab_x - x < font->space_width)
25354 next_tab_x += tab_width;
25356 it->pixel_width = next_tab_x - x;
25357 it->nglyphs = 1;
25358 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25359 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25361 if (it->glyph_row)
25363 append_stretch_glyph (it, it->object, it->pixel_width,
25364 it->ascent + it->descent, it->ascent);
25367 else
25369 it->pixel_width = 0;
25370 it->nglyphs = 1;
25374 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25376 /* A static composition.
25378 Note: A composition is represented as one glyph in the
25379 glyph matrix. There are no padding glyphs.
25381 Important note: pixel_width, ascent, and descent are the
25382 values of what is drawn by draw_glyphs (i.e. the values of
25383 the overall glyphs composed). */
25384 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25385 int boff; /* baseline offset */
25386 struct composition *cmp = composition_table[it->cmp_it.id];
25387 int glyph_len = cmp->glyph_len;
25388 struct font *font = face->font;
25390 it->nglyphs = 1;
25392 /* If we have not yet calculated pixel size data of glyphs of
25393 the composition for the current face font, calculate them
25394 now. Theoretically, we have to check all fonts for the
25395 glyphs, but that requires much time and memory space. So,
25396 here we check only the font of the first glyph. This may
25397 lead to incorrect display, but it's very rare, and C-l
25398 (recenter-top-bottom) can correct the display anyway. */
25399 if (! cmp->font || cmp->font != font)
25401 /* Ascent and descent of the font of the first character
25402 of this composition (adjusted by baseline offset).
25403 Ascent and descent of overall glyphs should not be less
25404 than these, respectively. */
25405 int font_ascent, font_descent, font_height;
25406 /* Bounding box of the overall glyphs. */
25407 int leftmost, rightmost, lowest, highest;
25408 int lbearing, rbearing;
25409 int i, width, ascent, descent;
25410 int left_padded = 0, right_padded = 0;
25411 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25412 XChar2b char2b;
25413 struct font_metrics *pcm;
25414 int font_not_found_p;
25415 ptrdiff_t pos;
25417 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25418 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25419 break;
25420 if (glyph_len < cmp->glyph_len)
25421 right_padded = 1;
25422 for (i = 0; i < glyph_len; i++)
25424 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25425 break;
25426 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25428 if (i > 0)
25429 left_padded = 1;
25431 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25432 : IT_CHARPOS (*it));
25433 /* If no suitable font is found, use the default font. */
25434 font_not_found_p = font == NULL;
25435 if (font_not_found_p)
25437 face = face->ascii_face;
25438 font = face->font;
25440 boff = font->baseline_offset;
25441 if (font->vertical_centering)
25442 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25443 font_ascent = FONT_BASE (font) + boff;
25444 font_descent = FONT_DESCENT (font) - boff;
25445 font_height = FONT_HEIGHT (font);
25447 cmp->font = font;
25449 pcm = NULL;
25450 if (! font_not_found_p)
25452 get_char_face_and_encoding (it->f, c, it->face_id,
25453 &char2b, 0);
25454 pcm = get_per_char_metric (font, &char2b);
25457 /* Initialize the bounding box. */
25458 if (pcm)
25460 width = cmp->glyph_len > 0 ? pcm->width : 0;
25461 ascent = pcm->ascent;
25462 descent = pcm->descent;
25463 lbearing = pcm->lbearing;
25464 rbearing = pcm->rbearing;
25466 else
25468 width = cmp->glyph_len > 0 ? font->space_width : 0;
25469 ascent = FONT_BASE (font);
25470 descent = FONT_DESCENT (font);
25471 lbearing = 0;
25472 rbearing = width;
25475 rightmost = width;
25476 leftmost = 0;
25477 lowest = - descent + boff;
25478 highest = ascent + boff;
25480 if (! font_not_found_p
25481 && font->default_ascent
25482 && CHAR_TABLE_P (Vuse_default_ascent)
25483 && !NILP (Faref (Vuse_default_ascent,
25484 make_number (it->char_to_display))))
25485 highest = font->default_ascent + boff;
25487 /* Draw the first glyph at the normal position. It may be
25488 shifted to right later if some other glyphs are drawn
25489 at the left. */
25490 cmp->offsets[i * 2] = 0;
25491 cmp->offsets[i * 2 + 1] = boff;
25492 cmp->lbearing = lbearing;
25493 cmp->rbearing = rbearing;
25495 /* Set cmp->offsets for the remaining glyphs. */
25496 for (i++; i < glyph_len; i++)
25498 int left, right, btm, top;
25499 int ch = COMPOSITION_GLYPH (cmp, i);
25500 int face_id;
25501 struct face *this_face;
25503 if (ch == '\t')
25504 ch = ' ';
25505 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25506 this_face = FACE_FROM_ID (it->f, face_id);
25507 font = this_face->font;
25509 if (font == NULL)
25510 pcm = NULL;
25511 else
25513 get_char_face_and_encoding (it->f, ch, face_id,
25514 &char2b, 0);
25515 pcm = get_per_char_metric (font, &char2b);
25517 if (! pcm)
25518 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25519 else
25521 width = pcm->width;
25522 ascent = pcm->ascent;
25523 descent = pcm->descent;
25524 lbearing = pcm->lbearing;
25525 rbearing = pcm->rbearing;
25526 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25528 /* Relative composition with or without
25529 alternate chars. */
25530 left = (leftmost + rightmost - width) / 2;
25531 btm = - descent + boff;
25532 if (font->relative_compose
25533 && (! CHAR_TABLE_P (Vignore_relative_composition)
25534 || NILP (Faref (Vignore_relative_composition,
25535 make_number (ch)))))
25538 if (- descent >= font->relative_compose)
25539 /* One extra pixel between two glyphs. */
25540 btm = highest + 1;
25541 else if (ascent <= 0)
25542 /* One extra pixel between two glyphs. */
25543 btm = lowest - 1 - ascent - descent;
25546 else
25548 /* A composition rule is specified by an integer
25549 value that encodes global and new reference
25550 points (GREF and NREF). GREF and NREF are
25551 specified by numbers as below:
25553 0---1---2 -- ascent
25557 9--10--11 -- center
25559 ---3---4---5--- baseline
25561 6---7---8 -- descent
25563 int rule = COMPOSITION_RULE (cmp, i);
25564 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25566 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25567 grefx = gref % 3, nrefx = nref % 3;
25568 grefy = gref / 3, nrefy = nref / 3;
25569 if (xoff)
25570 xoff = font_height * (xoff - 128) / 256;
25571 if (yoff)
25572 yoff = font_height * (yoff - 128) / 256;
25574 left = (leftmost
25575 + grefx * (rightmost - leftmost) / 2
25576 - nrefx * width / 2
25577 + xoff);
25579 btm = ((grefy == 0 ? highest
25580 : grefy == 1 ? 0
25581 : grefy == 2 ? lowest
25582 : (highest + lowest) / 2)
25583 - (nrefy == 0 ? ascent + descent
25584 : nrefy == 1 ? descent - boff
25585 : nrefy == 2 ? 0
25586 : (ascent + descent) / 2)
25587 + yoff);
25590 cmp->offsets[i * 2] = left;
25591 cmp->offsets[i * 2 + 1] = btm + descent;
25593 /* Update the bounding box of the overall glyphs. */
25594 if (width > 0)
25596 right = left + width;
25597 if (left < leftmost)
25598 leftmost = left;
25599 if (right > rightmost)
25600 rightmost = right;
25602 top = btm + descent + ascent;
25603 if (top > highest)
25604 highest = top;
25605 if (btm < lowest)
25606 lowest = btm;
25608 if (cmp->lbearing > left + lbearing)
25609 cmp->lbearing = left + lbearing;
25610 if (cmp->rbearing < left + rbearing)
25611 cmp->rbearing = left + rbearing;
25615 /* If there are glyphs whose x-offsets are negative,
25616 shift all glyphs to the right and make all x-offsets
25617 non-negative. */
25618 if (leftmost < 0)
25620 for (i = 0; i < cmp->glyph_len; i++)
25621 cmp->offsets[i * 2] -= leftmost;
25622 rightmost -= leftmost;
25623 cmp->lbearing -= leftmost;
25624 cmp->rbearing -= leftmost;
25627 if (left_padded && cmp->lbearing < 0)
25629 for (i = 0; i < cmp->glyph_len; i++)
25630 cmp->offsets[i * 2] -= cmp->lbearing;
25631 rightmost -= cmp->lbearing;
25632 cmp->rbearing -= cmp->lbearing;
25633 cmp->lbearing = 0;
25635 if (right_padded && rightmost < cmp->rbearing)
25637 rightmost = cmp->rbearing;
25640 cmp->pixel_width = rightmost;
25641 cmp->ascent = highest;
25642 cmp->descent = - lowest;
25643 if (cmp->ascent < font_ascent)
25644 cmp->ascent = font_ascent;
25645 if (cmp->descent < font_descent)
25646 cmp->descent = font_descent;
25649 if (it->glyph_row
25650 && (cmp->lbearing < 0
25651 || cmp->rbearing > cmp->pixel_width))
25652 it->glyph_row->contains_overlapping_glyphs_p = 1;
25654 it->pixel_width = cmp->pixel_width;
25655 it->ascent = it->phys_ascent = cmp->ascent;
25656 it->descent = it->phys_descent = cmp->descent;
25657 if (face->box != FACE_NO_BOX)
25659 int thick = face->box_line_width;
25661 if (thick > 0)
25663 it->ascent += thick;
25664 it->descent += thick;
25666 else
25667 thick = - thick;
25669 if (it->start_of_box_run_p)
25670 it->pixel_width += thick;
25671 if (it->end_of_box_run_p)
25672 it->pixel_width += thick;
25675 /* If face has an overline, add the height of the overline
25676 (1 pixel) and a 1 pixel margin to the character height. */
25677 if (face->overline_p)
25678 it->ascent += overline_margin;
25680 take_vertical_position_into_account (it);
25681 if (it->ascent < 0)
25682 it->ascent = 0;
25683 if (it->descent < 0)
25684 it->descent = 0;
25686 if (it->glyph_row && cmp->glyph_len > 0)
25687 append_composite_glyph (it);
25689 else if (it->what == IT_COMPOSITION)
25691 /* A dynamic (automatic) composition. */
25692 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25693 Lisp_Object gstring;
25694 struct font_metrics metrics;
25696 it->nglyphs = 1;
25698 gstring = composition_gstring_from_id (it->cmp_it.id);
25699 it->pixel_width
25700 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25701 &metrics);
25702 if (it->glyph_row
25703 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25704 it->glyph_row->contains_overlapping_glyphs_p = 1;
25705 it->ascent = it->phys_ascent = metrics.ascent;
25706 it->descent = it->phys_descent = metrics.descent;
25707 if (face->box != FACE_NO_BOX)
25709 int thick = face->box_line_width;
25711 if (thick > 0)
25713 it->ascent += thick;
25714 it->descent += thick;
25716 else
25717 thick = - thick;
25719 if (it->start_of_box_run_p)
25720 it->pixel_width += thick;
25721 if (it->end_of_box_run_p)
25722 it->pixel_width += thick;
25724 /* If face has an overline, add the height of the overline
25725 (1 pixel) and a 1 pixel margin to the character height. */
25726 if (face->overline_p)
25727 it->ascent += overline_margin;
25728 take_vertical_position_into_account (it);
25729 if (it->ascent < 0)
25730 it->ascent = 0;
25731 if (it->descent < 0)
25732 it->descent = 0;
25734 if (it->glyph_row)
25735 append_composite_glyph (it);
25737 else if (it->what == IT_GLYPHLESS)
25738 produce_glyphless_glyph (it, 0, Qnil);
25739 else if (it->what == IT_IMAGE)
25740 produce_image_glyph (it);
25741 else if (it->what == IT_STRETCH)
25742 produce_stretch_glyph (it);
25744 done:
25745 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25746 because this isn't true for images with `:ascent 100'. */
25747 eassert (it->ascent >= 0 && it->descent >= 0);
25748 if (it->area == TEXT_AREA)
25749 it->current_x += it->pixel_width;
25751 if (extra_line_spacing > 0)
25753 it->descent += extra_line_spacing;
25754 if (extra_line_spacing > it->max_extra_line_spacing)
25755 it->max_extra_line_spacing = extra_line_spacing;
25758 it->max_ascent = max (it->max_ascent, it->ascent);
25759 it->max_descent = max (it->max_descent, it->descent);
25760 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25761 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25764 /* EXPORT for RIF:
25765 Output LEN glyphs starting at START at the nominal cursor position.
25766 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25767 being updated, and UPDATED_AREA is the area of that row being updated. */
25769 void
25770 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25771 struct glyph *start, enum glyph_row_area updated_area, int len)
25773 int x, hpos, chpos = w->phys_cursor.hpos;
25775 eassert (updated_row);
25776 /* When the window is hscrolled, cursor hpos can legitimately be out
25777 of bounds, but we draw the cursor at the corresponding window
25778 margin in that case. */
25779 if (!updated_row->reversed_p && chpos < 0)
25780 chpos = 0;
25781 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25782 chpos = updated_row->used[TEXT_AREA] - 1;
25784 block_input ();
25786 /* Write glyphs. */
25788 hpos = start - updated_row->glyphs[updated_area];
25789 x = draw_glyphs (w, w->output_cursor.x,
25790 updated_row, updated_area,
25791 hpos, hpos + len,
25792 DRAW_NORMAL_TEXT, 0);
25794 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25795 if (updated_area == TEXT_AREA
25796 && w->phys_cursor_on_p
25797 && w->phys_cursor.vpos == w->output_cursor.vpos
25798 && chpos >= hpos
25799 && chpos < hpos + len)
25800 w->phys_cursor_on_p = 0;
25802 unblock_input ();
25804 /* Advance the output cursor. */
25805 w->output_cursor.hpos += len;
25806 w->output_cursor.x = x;
25810 /* EXPORT for RIF:
25811 Insert LEN glyphs from START at the nominal cursor position. */
25813 void
25814 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25815 struct glyph *start, enum glyph_row_area updated_area, int len)
25817 struct frame *f;
25818 int line_height, shift_by_width, shifted_region_width;
25819 struct glyph_row *row;
25820 struct glyph *glyph;
25821 int frame_x, frame_y;
25822 ptrdiff_t hpos;
25824 eassert (updated_row);
25825 block_input ();
25826 f = XFRAME (WINDOW_FRAME (w));
25828 /* Get the height of the line we are in. */
25829 row = updated_row;
25830 line_height = row->height;
25832 /* Get the width of the glyphs to insert. */
25833 shift_by_width = 0;
25834 for (glyph = start; glyph < start + len; ++glyph)
25835 shift_by_width += glyph->pixel_width;
25837 /* Get the width of the region to shift right. */
25838 shifted_region_width = (window_box_width (w, updated_area)
25839 - w->output_cursor.x
25840 - shift_by_width);
25842 /* Shift right. */
25843 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25844 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25846 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25847 line_height, shift_by_width);
25849 /* Write the glyphs. */
25850 hpos = start - row->glyphs[updated_area];
25851 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25852 hpos, hpos + len,
25853 DRAW_NORMAL_TEXT, 0);
25855 /* Advance the output cursor. */
25856 w->output_cursor.hpos += len;
25857 w->output_cursor.x += shift_by_width;
25858 unblock_input ();
25862 /* EXPORT for RIF:
25863 Erase the current text line from the nominal cursor position
25864 (inclusive) to pixel column TO_X (exclusive). The idea is that
25865 everything from TO_X onward is already erased.
25867 TO_X is a pixel position relative to UPDATED_AREA of currently
25868 updated window W. TO_X == -1 means clear to the end of this area. */
25870 void
25871 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25872 enum glyph_row_area updated_area, int to_x)
25874 struct frame *f;
25875 int max_x, min_y, max_y;
25876 int from_x, from_y, to_y;
25878 eassert (updated_row);
25879 f = XFRAME (w->frame);
25881 if (updated_row->full_width_p)
25882 max_x = WINDOW_TOTAL_WIDTH (w);
25883 else
25884 max_x = window_box_width (w, updated_area);
25885 max_y = window_text_bottom_y (w);
25887 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25888 of window. For TO_X > 0, truncate to end of drawing area. */
25889 if (to_x == 0)
25890 return;
25891 else if (to_x < 0)
25892 to_x = max_x;
25893 else
25894 to_x = min (to_x, max_x);
25896 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25898 /* Notice if the cursor will be cleared by this operation. */
25899 if (!updated_row->full_width_p)
25900 notice_overwritten_cursor (w, updated_area,
25901 w->output_cursor.x, -1,
25902 updated_row->y,
25903 MATRIX_ROW_BOTTOM_Y (updated_row));
25905 from_x = w->output_cursor.x;
25907 /* Translate to frame coordinates. */
25908 if (updated_row->full_width_p)
25910 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25911 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25913 else
25915 int area_left = window_box_left (w, updated_area);
25916 from_x += area_left;
25917 to_x += area_left;
25920 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25921 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25922 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25924 /* Prevent inadvertently clearing to end of the X window. */
25925 if (to_x > from_x && to_y > from_y)
25927 block_input ();
25928 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25929 to_x - from_x, to_y - from_y);
25930 unblock_input ();
25934 #endif /* HAVE_WINDOW_SYSTEM */
25938 /***********************************************************************
25939 Cursor types
25940 ***********************************************************************/
25942 /* Value is the internal representation of the specified cursor type
25943 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25944 of the bar cursor. */
25946 static enum text_cursor_kinds
25947 get_specified_cursor_type (Lisp_Object arg, int *width)
25949 enum text_cursor_kinds type;
25951 if (NILP (arg))
25952 return NO_CURSOR;
25954 if (EQ (arg, Qbox))
25955 return FILLED_BOX_CURSOR;
25957 if (EQ (arg, Qhollow))
25958 return HOLLOW_BOX_CURSOR;
25960 if (EQ (arg, Qbar))
25962 *width = 2;
25963 return BAR_CURSOR;
25966 if (CONSP (arg)
25967 && EQ (XCAR (arg), Qbar)
25968 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25970 *width = XINT (XCDR (arg));
25971 return BAR_CURSOR;
25974 if (EQ (arg, Qhbar))
25976 *width = 2;
25977 return HBAR_CURSOR;
25980 if (CONSP (arg)
25981 && EQ (XCAR (arg), Qhbar)
25982 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25984 *width = XINT (XCDR (arg));
25985 return HBAR_CURSOR;
25988 /* Treat anything unknown as "hollow box cursor".
25989 It was bad to signal an error; people have trouble fixing
25990 .Xdefaults with Emacs, when it has something bad in it. */
25991 type = HOLLOW_BOX_CURSOR;
25993 return type;
25996 /* Set the default cursor types for specified frame. */
25997 void
25998 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26000 int width = 1;
26001 Lisp_Object tem;
26003 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26004 FRAME_CURSOR_WIDTH (f) = width;
26006 /* By default, set up the blink-off state depending on the on-state. */
26008 tem = Fassoc (arg, Vblink_cursor_alist);
26009 if (!NILP (tem))
26011 FRAME_BLINK_OFF_CURSOR (f)
26012 = get_specified_cursor_type (XCDR (tem), &width);
26013 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26015 else
26016 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26018 /* Make sure the cursor gets redrawn. */
26019 f->cursor_type_changed = 1;
26023 #ifdef HAVE_WINDOW_SYSTEM
26025 /* Return the cursor we want to be displayed in window W. Return
26026 width of bar/hbar cursor through WIDTH arg. Return with
26027 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26028 (i.e. if the `system caret' should track this cursor).
26030 In a mini-buffer window, we want the cursor only to appear if we
26031 are reading input from this window. For the selected window, we
26032 want the cursor type given by the frame parameter or buffer local
26033 setting of cursor-type. If explicitly marked off, draw no cursor.
26034 In all other cases, we want a hollow box cursor. */
26036 static enum text_cursor_kinds
26037 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26038 int *active_cursor)
26040 struct frame *f = XFRAME (w->frame);
26041 struct buffer *b = XBUFFER (w->contents);
26042 int cursor_type = DEFAULT_CURSOR;
26043 Lisp_Object alt_cursor;
26044 int non_selected = 0;
26046 *active_cursor = 1;
26048 /* Echo area */
26049 if (cursor_in_echo_area
26050 && FRAME_HAS_MINIBUF_P (f)
26051 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26053 if (w == XWINDOW (echo_area_window))
26055 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26057 *width = FRAME_CURSOR_WIDTH (f);
26058 return FRAME_DESIRED_CURSOR (f);
26060 else
26061 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26064 *active_cursor = 0;
26065 non_selected = 1;
26068 /* Detect a nonselected window or nonselected frame. */
26069 else if (w != XWINDOW (f->selected_window)
26070 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26072 *active_cursor = 0;
26074 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26075 return NO_CURSOR;
26077 non_selected = 1;
26080 /* Never display a cursor in a window in which cursor-type is nil. */
26081 if (NILP (BVAR (b, cursor_type)))
26082 return NO_CURSOR;
26084 /* Get the normal cursor type for this window. */
26085 if (EQ (BVAR (b, cursor_type), Qt))
26087 cursor_type = FRAME_DESIRED_CURSOR (f);
26088 *width = FRAME_CURSOR_WIDTH (f);
26090 else
26091 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26093 /* Use cursor-in-non-selected-windows instead
26094 for non-selected window or frame. */
26095 if (non_selected)
26097 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26098 if (!EQ (Qt, alt_cursor))
26099 return get_specified_cursor_type (alt_cursor, width);
26100 /* t means modify the normal cursor type. */
26101 if (cursor_type == FILLED_BOX_CURSOR)
26102 cursor_type = HOLLOW_BOX_CURSOR;
26103 else if (cursor_type == BAR_CURSOR && *width > 1)
26104 --*width;
26105 return cursor_type;
26108 /* Use normal cursor if not blinked off. */
26109 if (!w->cursor_off_p)
26111 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26113 if (cursor_type == FILLED_BOX_CURSOR)
26115 /* Using a block cursor on large images can be very annoying.
26116 So use a hollow cursor for "large" images.
26117 If image is not transparent (no mask), also use hollow cursor. */
26118 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26119 if (img != NULL && IMAGEP (img->spec))
26121 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26122 where N = size of default frame font size.
26123 This should cover most of the "tiny" icons people may use. */
26124 if (!img->mask
26125 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26126 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26127 cursor_type = HOLLOW_BOX_CURSOR;
26130 else if (cursor_type != NO_CURSOR)
26132 /* Display current only supports BOX and HOLLOW cursors for images.
26133 So for now, unconditionally use a HOLLOW cursor when cursor is
26134 not a solid box cursor. */
26135 cursor_type = HOLLOW_BOX_CURSOR;
26138 return cursor_type;
26141 /* Cursor is blinked off, so determine how to "toggle" it. */
26143 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26144 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26145 return get_specified_cursor_type (XCDR (alt_cursor), width);
26147 /* Then see if frame has specified a specific blink off cursor type. */
26148 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26150 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26151 return FRAME_BLINK_OFF_CURSOR (f);
26154 #if 0
26155 /* Some people liked having a permanently visible blinking cursor,
26156 while others had very strong opinions against it. So it was
26157 decided to remove it. KFS 2003-09-03 */
26159 /* Finally perform built-in cursor blinking:
26160 filled box <-> hollow box
26161 wide [h]bar <-> narrow [h]bar
26162 narrow [h]bar <-> no cursor
26163 other type <-> no cursor */
26165 if (cursor_type == FILLED_BOX_CURSOR)
26166 return HOLLOW_BOX_CURSOR;
26168 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26170 *width = 1;
26171 return cursor_type;
26173 #endif
26175 return NO_CURSOR;
26179 /* Notice when the text cursor of window W has been completely
26180 overwritten by a drawing operation that outputs glyphs in AREA
26181 starting at X0 and ending at X1 in the line starting at Y0 and
26182 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26183 the rest of the line after X0 has been written. Y coordinates
26184 are window-relative. */
26186 static void
26187 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26188 int x0, int x1, int y0, int y1)
26190 int cx0, cx1, cy0, cy1;
26191 struct glyph_row *row;
26193 if (!w->phys_cursor_on_p)
26194 return;
26195 if (area != TEXT_AREA)
26196 return;
26198 if (w->phys_cursor.vpos < 0
26199 || w->phys_cursor.vpos >= w->current_matrix->nrows
26200 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26201 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26202 return;
26204 if (row->cursor_in_fringe_p)
26206 row->cursor_in_fringe_p = 0;
26207 draw_fringe_bitmap (w, row, row->reversed_p);
26208 w->phys_cursor_on_p = 0;
26209 return;
26212 cx0 = w->phys_cursor.x;
26213 cx1 = cx0 + w->phys_cursor_width;
26214 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26215 return;
26217 /* The cursor image will be completely removed from the
26218 screen if the output area intersects the cursor area in
26219 y-direction. When we draw in [y0 y1[, and some part of
26220 the cursor is at y < y0, that part must have been drawn
26221 before. When scrolling, the cursor is erased before
26222 actually scrolling, so we don't come here. When not
26223 scrolling, the rows above the old cursor row must have
26224 changed, and in this case these rows must have written
26225 over the cursor image.
26227 Likewise if part of the cursor is below y1, with the
26228 exception of the cursor being in the first blank row at
26229 the buffer and window end because update_text_area
26230 doesn't draw that row. (Except when it does, but
26231 that's handled in update_text_area.) */
26233 cy0 = w->phys_cursor.y;
26234 cy1 = cy0 + w->phys_cursor_height;
26235 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26236 return;
26238 w->phys_cursor_on_p = 0;
26241 #endif /* HAVE_WINDOW_SYSTEM */
26244 /************************************************************************
26245 Mouse Face
26246 ************************************************************************/
26248 #ifdef HAVE_WINDOW_SYSTEM
26250 /* EXPORT for RIF:
26251 Fix the display of area AREA of overlapping row ROW in window W
26252 with respect to the overlapping part OVERLAPS. */
26254 void
26255 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26256 enum glyph_row_area area, int overlaps)
26258 int i, x;
26260 block_input ();
26262 x = 0;
26263 for (i = 0; i < row->used[area];)
26265 if (row->glyphs[area][i].overlaps_vertically_p)
26267 int start = i, start_x = x;
26271 x += row->glyphs[area][i].pixel_width;
26272 ++i;
26274 while (i < row->used[area]
26275 && row->glyphs[area][i].overlaps_vertically_p);
26277 draw_glyphs (w, start_x, row, area,
26278 start, i,
26279 DRAW_NORMAL_TEXT, overlaps);
26281 else
26283 x += row->glyphs[area][i].pixel_width;
26284 ++i;
26288 unblock_input ();
26292 /* EXPORT:
26293 Draw the cursor glyph of window W in glyph row ROW. See the
26294 comment of draw_glyphs for the meaning of HL. */
26296 void
26297 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26298 enum draw_glyphs_face hl)
26300 /* If cursor hpos is out of bounds, don't draw garbage. This can
26301 happen in mini-buffer windows when switching between echo area
26302 glyphs and mini-buffer. */
26303 if ((row->reversed_p
26304 ? (w->phys_cursor.hpos >= 0)
26305 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26307 int on_p = w->phys_cursor_on_p;
26308 int x1;
26309 int hpos = w->phys_cursor.hpos;
26311 /* When the window is hscrolled, cursor hpos can legitimately be
26312 out of bounds, but we draw the cursor at the corresponding
26313 window margin in that case. */
26314 if (!row->reversed_p && hpos < 0)
26315 hpos = 0;
26316 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26317 hpos = row->used[TEXT_AREA] - 1;
26319 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26320 hl, 0);
26321 w->phys_cursor_on_p = on_p;
26323 if (hl == DRAW_CURSOR)
26324 w->phys_cursor_width = x1 - w->phys_cursor.x;
26325 /* When we erase the cursor, and ROW is overlapped by other
26326 rows, make sure that these overlapping parts of other rows
26327 are redrawn. */
26328 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26330 w->phys_cursor_width = x1 - w->phys_cursor.x;
26332 if (row > w->current_matrix->rows
26333 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26334 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26335 OVERLAPS_ERASED_CURSOR);
26337 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26338 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26339 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26340 OVERLAPS_ERASED_CURSOR);
26346 /* Erase the image of a cursor of window W from the screen. */
26348 #ifndef HAVE_NTGUI
26349 static
26350 #endif
26351 void
26352 erase_phys_cursor (struct window *w)
26354 struct frame *f = XFRAME (w->frame);
26355 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26356 int hpos = w->phys_cursor.hpos;
26357 int vpos = w->phys_cursor.vpos;
26358 int mouse_face_here_p = 0;
26359 struct glyph_matrix *active_glyphs = w->current_matrix;
26360 struct glyph_row *cursor_row;
26361 struct glyph *cursor_glyph;
26362 enum draw_glyphs_face hl;
26364 /* No cursor displayed or row invalidated => nothing to do on the
26365 screen. */
26366 if (w->phys_cursor_type == NO_CURSOR)
26367 goto mark_cursor_off;
26369 /* VPOS >= active_glyphs->nrows means that window has been resized.
26370 Don't bother to erase the cursor. */
26371 if (vpos >= active_glyphs->nrows)
26372 goto mark_cursor_off;
26374 /* If row containing cursor is marked invalid, there is nothing we
26375 can do. */
26376 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26377 if (!cursor_row->enabled_p)
26378 goto mark_cursor_off;
26380 /* If line spacing is > 0, old cursor may only be partially visible in
26381 window after split-window. So adjust visible height. */
26382 cursor_row->visible_height = min (cursor_row->visible_height,
26383 window_text_bottom_y (w) - cursor_row->y);
26385 /* If row is completely invisible, don't attempt to delete a cursor which
26386 isn't there. This can happen if cursor is at top of a window, and
26387 we switch to a buffer with a header line in that window. */
26388 if (cursor_row->visible_height <= 0)
26389 goto mark_cursor_off;
26391 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26392 if (cursor_row->cursor_in_fringe_p)
26394 cursor_row->cursor_in_fringe_p = 0;
26395 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26396 goto mark_cursor_off;
26399 /* This can happen when the new row is shorter than the old one.
26400 In this case, either draw_glyphs or clear_end_of_line
26401 should have cleared the cursor. Note that we wouldn't be
26402 able to erase the cursor in this case because we don't have a
26403 cursor glyph at hand. */
26404 if ((cursor_row->reversed_p
26405 ? (w->phys_cursor.hpos < 0)
26406 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26407 goto mark_cursor_off;
26409 /* When the window is hscrolled, cursor hpos can legitimately be out
26410 of bounds, but we draw the cursor at the corresponding window
26411 margin in that case. */
26412 if (!cursor_row->reversed_p && hpos < 0)
26413 hpos = 0;
26414 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26415 hpos = cursor_row->used[TEXT_AREA] - 1;
26417 /* If the cursor is in the mouse face area, redisplay that when
26418 we clear the cursor. */
26419 if (! NILP (hlinfo->mouse_face_window)
26420 && coords_in_mouse_face_p (w, hpos, vpos)
26421 /* Don't redraw the cursor's spot in mouse face if it is at the
26422 end of a line (on a newline). The cursor appears there, but
26423 mouse highlighting does not. */
26424 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26425 mouse_face_here_p = 1;
26427 /* Maybe clear the display under the cursor. */
26428 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26430 int x, y, left_x;
26431 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26432 int width;
26434 cursor_glyph = get_phys_cursor_glyph (w);
26435 if (cursor_glyph == NULL)
26436 goto mark_cursor_off;
26438 width = cursor_glyph->pixel_width;
26439 left_x = window_box_left_offset (w, TEXT_AREA);
26440 x = w->phys_cursor.x;
26441 if (x < left_x)
26442 width -= left_x - x;
26443 width = min (width, window_box_width (w, TEXT_AREA) - x);
26444 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26445 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26447 if (width > 0)
26448 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26451 /* Erase the cursor by redrawing the character underneath it. */
26452 if (mouse_face_here_p)
26453 hl = DRAW_MOUSE_FACE;
26454 else
26455 hl = DRAW_NORMAL_TEXT;
26456 draw_phys_cursor_glyph (w, cursor_row, hl);
26458 mark_cursor_off:
26459 w->phys_cursor_on_p = 0;
26460 w->phys_cursor_type = NO_CURSOR;
26464 /* EXPORT:
26465 Display or clear cursor of window W. If ON is zero, clear the
26466 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26467 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26469 void
26470 display_and_set_cursor (struct window *w, bool on,
26471 int hpos, int vpos, int x, int y)
26473 struct frame *f = XFRAME (w->frame);
26474 int new_cursor_type;
26475 int new_cursor_width;
26476 int active_cursor;
26477 struct glyph_row *glyph_row;
26478 struct glyph *glyph;
26480 /* This is pointless on invisible frames, and dangerous on garbaged
26481 windows and frames; in the latter case, the frame or window may
26482 be in the midst of changing its size, and x and y may be off the
26483 window. */
26484 if (! FRAME_VISIBLE_P (f)
26485 || FRAME_GARBAGED_P (f)
26486 || vpos >= w->current_matrix->nrows
26487 || hpos >= w->current_matrix->matrix_w)
26488 return;
26490 /* If cursor is off and we want it off, return quickly. */
26491 if (!on && !w->phys_cursor_on_p)
26492 return;
26494 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26495 /* If cursor row is not enabled, we don't really know where to
26496 display the cursor. */
26497 if (!glyph_row->enabled_p)
26499 w->phys_cursor_on_p = 0;
26500 return;
26503 glyph = NULL;
26504 if (!glyph_row->exact_window_width_line_p
26505 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26506 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26508 eassert (input_blocked_p ());
26510 /* Set new_cursor_type to the cursor we want to be displayed. */
26511 new_cursor_type = get_window_cursor_type (w, glyph,
26512 &new_cursor_width, &active_cursor);
26514 /* If cursor is currently being shown and we don't want it to be or
26515 it is in the wrong place, or the cursor type is not what we want,
26516 erase it. */
26517 if (w->phys_cursor_on_p
26518 && (!on
26519 || w->phys_cursor.x != x
26520 || w->phys_cursor.y != y
26521 || new_cursor_type != w->phys_cursor_type
26522 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26523 && new_cursor_width != w->phys_cursor_width)))
26524 erase_phys_cursor (w);
26526 /* Don't check phys_cursor_on_p here because that flag is only set
26527 to zero in some cases where we know that the cursor has been
26528 completely erased, to avoid the extra work of erasing the cursor
26529 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26530 still not be visible, or it has only been partly erased. */
26531 if (on)
26533 w->phys_cursor_ascent = glyph_row->ascent;
26534 w->phys_cursor_height = glyph_row->height;
26536 /* Set phys_cursor_.* before x_draw_.* is called because some
26537 of them may need the information. */
26538 w->phys_cursor.x = x;
26539 w->phys_cursor.y = glyph_row->y;
26540 w->phys_cursor.hpos = hpos;
26541 w->phys_cursor.vpos = vpos;
26544 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26545 new_cursor_type, new_cursor_width,
26546 on, active_cursor);
26550 /* Switch the display of W's cursor on or off, according to the value
26551 of ON. */
26553 static void
26554 update_window_cursor (struct window *w, bool on)
26556 /* Don't update cursor in windows whose frame is in the process
26557 of being deleted. */
26558 if (w->current_matrix)
26560 int hpos = w->phys_cursor.hpos;
26561 int vpos = w->phys_cursor.vpos;
26562 struct glyph_row *row;
26564 if (vpos >= w->current_matrix->nrows
26565 || hpos >= w->current_matrix->matrix_w)
26566 return;
26568 row = MATRIX_ROW (w->current_matrix, vpos);
26570 /* When the window is hscrolled, cursor hpos can legitimately be
26571 out of bounds, but we draw the cursor at the corresponding
26572 window margin in that case. */
26573 if (!row->reversed_p && hpos < 0)
26574 hpos = 0;
26575 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26576 hpos = row->used[TEXT_AREA] - 1;
26578 block_input ();
26579 display_and_set_cursor (w, on, hpos, vpos,
26580 w->phys_cursor.x, w->phys_cursor.y);
26581 unblock_input ();
26586 /* Call update_window_cursor with parameter ON_P on all leaf windows
26587 in the window tree rooted at W. */
26589 static void
26590 update_cursor_in_window_tree (struct window *w, bool on_p)
26592 while (w)
26594 if (WINDOWP (w->contents))
26595 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26596 else
26597 update_window_cursor (w, on_p);
26599 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26604 /* EXPORT:
26605 Display the cursor on window W, or clear it, according to ON_P.
26606 Don't change the cursor's position. */
26608 void
26609 x_update_cursor (struct frame *f, bool on_p)
26611 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26615 /* EXPORT:
26616 Clear the cursor of window W to background color, and mark the
26617 cursor as not shown. This is used when the text where the cursor
26618 is about to be rewritten. */
26620 void
26621 x_clear_cursor (struct window *w)
26623 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26624 update_window_cursor (w, 0);
26627 #endif /* HAVE_WINDOW_SYSTEM */
26629 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26630 and MSDOS. */
26631 static void
26632 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26633 int start_hpos, int end_hpos,
26634 enum draw_glyphs_face draw)
26636 #ifdef HAVE_WINDOW_SYSTEM
26637 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26639 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26640 return;
26642 #endif
26643 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26644 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26645 #endif
26648 /* Display the active region described by mouse_face_* according to DRAW. */
26650 static void
26651 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26653 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26654 struct frame *f = XFRAME (WINDOW_FRAME (w));
26656 if (/* If window is in the process of being destroyed, don't bother
26657 to do anything. */
26658 w->current_matrix != NULL
26659 /* Don't update mouse highlight if hidden */
26660 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26661 /* Recognize when we are called to operate on rows that don't exist
26662 anymore. This can happen when a window is split. */
26663 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26665 int phys_cursor_on_p = w->phys_cursor_on_p;
26666 struct glyph_row *row, *first, *last;
26668 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26669 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26671 for (row = first; row <= last && row->enabled_p; ++row)
26673 int start_hpos, end_hpos, start_x;
26675 /* For all but the first row, the highlight starts at column 0. */
26676 if (row == first)
26678 /* R2L rows have BEG and END in reversed order, but the
26679 screen drawing geometry is always left to right. So
26680 we need to mirror the beginning and end of the
26681 highlighted area in R2L rows. */
26682 if (!row->reversed_p)
26684 start_hpos = hlinfo->mouse_face_beg_col;
26685 start_x = hlinfo->mouse_face_beg_x;
26687 else if (row == last)
26689 start_hpos = hlinfo->mouse_face_end_col;
26690 start_x = hlinfo->mouse_face_end_x;
26692 else
26694 start_hpos = 0;
26695 start_x = 0;
26698 else if (row->reversed_p && row == last)
26700 start_hpos = hlinfo->mouse_face_end_col;
26701 start_x = hlinfo->mouse_face_end_x;
26703 else
26705 start_hpos = 0;
26706 start_x = 0;
26709 if (row == last)
26711 if (!row->reversed_p)
26712 end_hpos = hlinfo->mouse_face_end_col;
26713 else if (row == first)
26714 end_hpos = hlinfo->mouse_face_beg_col;
26715 else
26717 end_hpos = row->used[TEXT_AREA];
26718 if (draw == DRAW_NORMAL_TEXT)
26719 row->fill_line_p = 1; /* Clear to end of line */
26722 else if (row->reversed_p && row == first)
26723 end_hpos = hlinfo->mouse_face_beg_col;
26724 else
26726 end_hpos = row->used[TEXT_AREA];
26727 if (draw == DRAW_NORMAL_TEXT)
26728 row->fill_line_p = 1; /* Clear to end of line */
26731 if (end_hpos > start_hpos)
26733 draw_row_with_mouse_face (w, start_x, row,
26734 start_hpos, end_hpos, draw);
26736 row->mouse_face_p
26737 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26741 #ifdef HAVE_WINDOW_SYSTEM
26742 /* When we've written over the cursor, arrange for it to
26743 be displayed again. */
26744 if (FRAME_WINDOW_P (f)
26745 && phys_cursor_on_p && !w->phys_cursor_on_p)
26747 int hpos = w->phys_cursor.hpos;
26749 /* When the window is hscrolled, cursor hpos can legitimately be
26750 out of bounds, but we draw the cursor at the corresponding
26751 window margin in that case. */
26752 if (!row->reversed_p && hpos < 0)
26753 hpos = 0;
26754 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26755 hpos = row->used[TEXT_AREA] - 1;
26757 block_input ();
26758 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26759 w->phys_cursor.x, w->phys_cursor.y);
26760 unblock_input ();
26762 #endif /* HAVE_WINDOW_SYSTEM */
26765 #ifdef HAVE_WINDOW_SYSTEM
26766 /* Change the mouse cursor. */
26767 if (FRAME_WINDOW_P (f))
26769 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
26770 if (draw == DRAW_NORMAL_TEXT
26771 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26772 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26773 else
26774 #endif
26775 if (draw == DRAW_MOUSE_FACE)
26776 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26777 else
26778 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26780 #endif /* HAVE_WINDOW_SYSTEM */
26783 /* EXPORT:
26784 Clear out the mouse-highlighted active region.
26785 Redraw it un-highlighted first. Value is non-zero if mouse
26786 face was actually drawn unhighlighted. */
26789 clear_mouse_face (Mouse_HLInfo *hlinfo)
26791 int cleared = 0;
26793 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26795 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26796 cleared = 1;
26799 reset_mouse_highlight (hlinfo);
26800 return cleared;
26803 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26804 within the mouse face on that window. */
26805 static int
26806 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26808 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26810 /* Quickly resolve the easy cases. */
26811 if (!(WINDOWP (hlinfo->mouse_face_window)
26812 && XWINDOW (hlinfo->mouse_face_window) == w))
26813 return 0;
26814 if (vpos < hlinfo->mouse_face_beg_row
26815 || vpos > hlinfo->mouse_face_end_row)
26816 return 0;
26817 if (vpos > hlinfo->mouse_face_beg_row
26818 && vpos < hlinfo->mouse_face_end_row)
26819 return 1;
26821 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26823 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26825 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26826 return 1;
26828 else if ((vpos == hlinfo->mouse_face_beg_row
26829 && hpos >= hlinfo->mouse_face_beg_col)
26830 || (vpos == hlinfo->mouse_face_end_row
26831 && hpos < hlinfo->mouse_face_end_col))
26832 return 1;
26834 else
26836 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26838 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26839 return 1;
26841 else if ((vpos == hlinfo->mouse_face_beg_row
26842 && hpos <= hlinfo->mouse_face_beg_col)
26843 || (vpos == hlinfo->mouse_face_end_row
26844 && hpos > hlinfo->mouse_face_end_col))
26845 return 1;
26847 return 0;
26851 /* EXPORT:
26852 Non-zero if physical cursor of window W is within mouse face. */
26855 cursor_in_mouse_face_p (struct window *w)
26857 int hpos = w->phys_cursor.hpos;
26858 int vpos = w->phys_cursor.vpos;
26859 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26861 /* When the window is hscrolled, cursor hpos can legitimately be out
26862 of bounds, but we draw the cursor at the corresponding window
26863 margin in that case. */
26864 if (!row->reversed_p && hpos < 0)
26865 hpos = 0;
26866 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26867 hpos = row->used[TEXT_AREA] - 1;
26869 return coords_in_mouse_face_p (w, hpos, vpos);
26874 /* Find the glyph rows START_ROW and END_ROW of window W that display
26875 characters between buffer positions START_CHARPOS and END_CHARPOS
26876 (excluding END_CHARPOS). DISP_STRING is a display string that
26877 covers these buffer positions. This is similar to
26878 row_containing_pos, but is more accurate when bidi reordering makes
26879 buffer positions change non-linearly with glyph rows. */
26880 static void
26881 rows_from_pos_range (struct window *w,
26882 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26883 Lisp_Object disp_string,
26884 struct glyph_row **start, struct glyph_row **end)
26886 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26887 int last_y = window_text_bottom_y (w);
26888 struct glyph_row *row;
26890 *start = NULL;
26891 *end = NULL;
26893 while (!first->enabled_p
26894 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26895 first++;
26897 /* Find the START row. */
26898 for (row = first;
26899 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26900 row++)
26902 /* A row can potentially be the START row if the range of the
26903 characters it displays intersects the range
26904 [START_CHARPOS..END_CHARPOS). */
26905 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26906 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26907 /* See the commentary in row_containing_pos, for the
26908 explanation of the complicated way to check whether
26909 some position is beyond the end of the characters
26910 displayed by a row. */
26911 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26912 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26913 && !row->ends_at_zv_p
26914 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26915 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26916 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26917 && !row->ends_at_zv_p
26918 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26920 /* Found a candidate row. Now make sure at least one of the
26921 glyphs it displays has a charpos from the range
26922 [START_CHARPOS..END_CHARPOS).
26924 This is not obvious because bidi reordering could make
26925 buffer positions of a row be 1,2,3,102,101,100, and if we
26926 want to highlight characters in [50..60), we don't want
26927 this row, even though [50..60) does intersect [1..103),
26928 the range of character positions given by the row's start
26929 and end positions. */
26930 struct glyph *g = row->glyphs[TEXT_AREA];
26931 struct glyph *e = g + row->used[TEXT_AREA];
26933 while (g < e)
26935 if (((BUFFERP (g->object) || INTEGERP (g->object))
26936 && start_charpos <= g->charpos && g->charpos < end_charpos)
26937 /* A glyph that comes from DISP_STRING is by
26938 definition to be highlighted. */
26939 || EQ (g->object, disp_string))
26940 *start = row;
26941 g++;
26943 if (*start)
26944 break;
26948 /* Find the END row. */
26949 if (!*start
26950 /* If the last row is partially visible, start looking for END
26951 from that row, instead of starting from FIRST. */
26952 && !(row->enabled_p
26953 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26954 row = first;
26955 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26957 struct glyph_row *next = row + 1;
26958 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26960 if (!next->enabled_p
26961 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26962 /* The first row >= START whose range of displayed characters
26963 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26964 is the row END + 1. */
26965 || (start_charpos < next_start
26966 && end_charpos < next_start)
26967 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26968 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26969 && !next->ends_at_zv_p
26970 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26971 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26972 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26973 && !next->ends_at_zv_p
26974 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26976 *end = row;
26977 break;
26979 else
26981 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26982 but none of the characters it displays are in the range, it is
26983 also END + 1. */
26984 struct glyph *g = next->glyphs[TEXT_AREA];
26985 struct glyph *s = g;
26986 struct glyph *e = g + next->used[TEXT_AREA];
26988 while (g < e)
26990 if (((BUFFERP (g->object) || INTEGERP (g->object))
26991 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26992 /* If the buffer position of the first glyph in
26993 the row is equal to END_CHARPOS, it means
26994 the last character to be highlighted is the
26995 newline of ROW, and we must consider NEXT as
26996 END, not END+1. */
26997 || (((!next->reversed_p && g == s)
26998 || (next->reversed_p && g == e - 1))
26999 && (g->charpos == end_charpos
27000 /* Special case for when NEXT is an
27001 empty line at ZV. */
27002 || (g->charpos == -1
27003 && !row->ends_at_zv_p
27004 && next_start == end_charpos)))))
27005 /* A glyph that comes from DISP_STRING is by
27006 definition to be highlighted. */
27007 || EQ (g->object, disp_string))
27008 break;
27009 g++;
27011 if (g == e)
27013 *end = row;
27014 break;
27016 /* The first row that ends at ZV must be the last to be
27017 highlighted. */
27018 else if (next->ends_at_zv_p)
27020 *end = next;
27021 break;
27027 /* This function sets the mouse_face_* elements of HLINFO, assuming
27028 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27029 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27030 for the overlay or run of text properties specifying the mouse
27031 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27032 before-string and after-string that must also be highlighted.
27033 DISP_STRING, if non-nil, is a display string that may cover some
27034 or all of the highlighted text. */
27036 static void
27037 mouse_face_from_buffer_pos (Lisp_Object window,
27038 Mouse_HLInfo *hlinfo,
27039 ptrdiff_t mouse_charpos,
27040 ptrdiff_t start_charpos,
27041 ptrdiff_t end_charpos,
27042 Lisp_Object before_string,
27043 Lisp_Object after_string,
27044 Lisp_Object disp_string)
27046 struct window *w = XWINDOW (window);
27047 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27048 struct glyph_row *r1, *r2;
27049 struct glyph *glyph, *end;
27050 ptrdiff_t ignore, pos;
27051 int x;
27053 eassert (NILP (disp_string) || STRINGP (disp_string));
27054 eassert (NILP (before_string) || STRINGP (before_string));
27055 eassert (NILP (after_string) || STRINGP (after_string));
27057 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27058 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27059 if (r1 == NULL)
27060 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27061 /* If the before-string or display-string contains newlines,
27062 rows_from_pos_range skips to its last row. Move back. */
27063 if (!NILP (before_string) || !NILP (disp_string))
27065 struct glyph_row *prev;
27066 while ((prev = r1 - 1, prev >= first)
27067 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27068 && prev->used[TEXT_AREA] > 0)
27070 struct glyph *beg = prev->glyphs[TEXT_AREA];
27071 glyph = beg + prev->used[TEXT_AREA];
27072 while (--glyph >= beg && INTEGERP (glyph->object));
27073 if (glyph < beg
27074 || !(EQ (glyph->object, before_string)
27075 || EQ (glyph->object, disp_string)))
27076 break;
27077 r1 = prev;
27080 if (r2 == NULL)
27082 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27083 hlinfo->mouse_face_past_end = 1;
27085 else if (!NILP (after_string))
27087 /* If the after-string has newlines, advance to its last row. */
27088 struct glyph_row *next;
27089 struct glyph_row *last
27090 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27092 for (next = r2 + 1;
27093 next <= last
27094 && next->used[TEXT_AREA] > 0
27095 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27096 ++next)
27097 r2 = next;
27099 /* The rest of the display engine assumes that mouse_face_beg_row is
27100 either above mouse_face_end_row or identical to it. But with
27101 bidi-reordered continued lines, the row for START_CHARPOS could
27102 be below the row for END_CHARPOS. If so, swap the rows and store
27103 them in correct order. */
27104 if (r1->y > r2->y)
27106 struct glyph_row *tem = r2;
27108 r2 = r1;
27109 r1 = tem;
27112 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27113 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27115 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27116 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27117 could be anywhere in the row and in any order. The strategy
27118 below is to find the leftmost and the rightmost glyph that
27119 belongs to either of these 3 strings, or whose position is
27120 between START_CHARPOS and END_CHARPOS, and highlight all the
27121 glyphs between those two. This may cover more than just the text
27122 between START_CHARPOS and END_CHARPOS if the range of characters
27123 strides the bidi level boundary, e.g. if the beginning is in R2L
27124 text while the end is in L2R text or vice versa. */
27125 if (!r1->reversed_p)
27127 /* This row is in a left to right paragraph. Scan it left to
27128 right. */
27129 glyph = r1->glyphs[TEXT_AREA];
27130 end = glyph + r1->used[TEXT_AREA];
27131 x = r1->x;
27133 /* Skip truncation glyphs at the start of the glyph row. */
27134 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27135 for (; glyph < end
27136 && INTEGERP (glyph->object)
27137 && glyph->charpos < 0;
27138 ++glyph)
27139 x += glyph->pixel_width;
27141 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27142 or DISP_STRING, and the first glyph from buffer whose
27143 position is between START_CHARPOS and END_CHARPOS. */
27144 for (; glyph < end
27145 && !INTEGERP (glyph->object)
27146 && !EQ (glyph->object, disp_string)
27147 && !(BUFFERP (glyph->object)
27148 && (glyph->charpos >= start_charpos
27149 && glyph->charpos < end_charpos));
27150 ++glyph)
27152 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27153 are present at buffer positions between START_CHARPOS and
27154 END_CHARPOS, or if they come from an overlay. */
27155 if (EQ (glyph->object, before_string))
27157 pos = string_buffer_position (before_string,
27158 start_charpos);
27159 /* If pos == 0, it means before_string came from an
27160 overlay, not from a buffer position. */
27161 if (!pos || (pos >= start_charpos && pos < end_charpos))
27162 break;
27164 else if (EQ (glyph->object, after_string))
27166 pos = string_buffer_position (after_string, end_charpos);
27167 if (!pos || (pos >= start_charpos && pos < end_charpos))
27168 break;
27170 x += glyph->pixel_width;
27172 hlinfo->mouse_face_beg_x = x;
27173 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27175 else
27177 /* This row is in a right to left paragraph. Scan it right to
27178 left. */
27179 struct glyph *g;
27181 end = r1->glyphs[TEXT_AREA] - 1;
27182 glyph = end + r1->used[TEXT_AREA];
27184 /* Skip truncation glyphs at the start of the glyph row. */
27185 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27186 for (; glyph > end
27187 && INTEGERP (glyph->object)
27188 && glyph->charpos < 0;
27189 --glyph)
27192 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27193 or DISP_STRING, and the first glyph from buffer whose
27194 position is between START_CHARPOS and END_CHARPOS. */
27195 for (; glyph > end
27196 && !INTEGERP (glyph->object)
27197 && !EQ (glyph->object, disp_string)
27198 && !(BUFFERP (glyph->object)
27199 && (glyph->charpos >= start_charpos
27200 && glyph->charpos < end_charpos));
27201 --glyph)
27203 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27204 are present at buffer positions between START_CHARPOS and
27205 END_CHARPOS, or if they come from an overlay. */
27206 if (EQ (glyph->object, before_string))
27208 pos = string_buffer_position (before_string, start_charpos);
27209 /* If pos == 0, it means before_string came from an
27210 overlay, not from a buffer position. */
27211 if (!pos || (pos >= start_charpos && pos < end_charpos))
27212 break;
27214 else if (EQ (glyph->object, after_string))
27216 pos = string_buffer_position (after_string, end_charpos);
27217 if (!pos || (pos >= start_charpos && pos < end_charpos))
27218 break;
27222 glyph++; /* first glyph to the right of the highlighted area */
27223 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27224 x += g->pixel_width;
27225 hlinfo->mouse_face_beg_x = x;
27226 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27229 /* If the highlight ends in a different row, compute GLYPH and END
27230 for the end row. Otherwise, reuse the values computed above for
27231 the row where the highlight begins. */
27232 if (r2 != r1)
27234 if (!r2->reversed_p)
27236 glyph = r2->glyphs[TEXT_AREA];
27237 end = glyph + r2->used[TEXT_AREA];
27238 x = r2->x;
27240 else
27242 end = r2->glyphs[TEXT_AREA] - 1;
27243 glyph = end + r2->used[TEXT_AREA];
27247 if (!r2->reversed_p)
27249 /* Skip truncation and continuation glyphs near the end of the
27250 row, and also blanks and stretch glyphs inserted by
27251 extend_face_to_end_of_line. */
27252 while (end > glyph
27253 && INTEGERP ((end - 1)->object))
27254 --end;
27255 /* Scan the rest of the glyph row from the end, looking for the
27256 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27257 DISP_STRING, or whose position is between START_CHARPOS
27258 and END_CHARPOS */
27259 for (--end;
27260 end > glyph
27261 && !INTEGERP (end->object)
27262 && !EQ (end->object, disp_string)
27263 && !(BUFFERP (end->object)
27264 && (end->charpos >= start_charpos
27265 && end->charpos < end_charpos));
27266 --end)
27268 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27269 are present at buffer positions between START_CHARPOS and
27270 END_CHARPOS, or if they come from an overlay. */
27271 if (EQ (end->object, before_string))
27273 pos = string_buffer_position (before_string, start_charpos);
27274 if (!pos || (pos >= start_charpos && pos < end_charpos))
27275 break;
27277 else if (EQ (end->object, after_string))
27279 pos = string_buffer_position (after_string, end_charpos);
27280 if (!pos || (pos >= start_charpos && pos < end_charpos))
27281 break;
27284 /* Find the X coordinate of the last glyph to be highlighted. */
27285 for (; glyph <= end; ++glyph)
27286 x += glyph->pixel_width;
27288 hlinfo->mouse_face_end_x = x;
27289 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27291 else
27293 /* Skip truncation and continuation glyphs near the end of the
27294 row, and also blanks and stretch glyphs inserted by
27295 extend_face_to_end_of_line. */
27296 x = r2->x;
27297 end++;
27298 while (end < glyph
27299 && INTEGERP (end->object))
27301 x += end->pixel_width;
27302 ++end;
27304 /* Scan the rest of the glyph row from the end, looking for the
27305 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27306 DISP_STRING, or whose position is between START_CHARPOS
27307 and END_CHARPOS */
27308 for ( ;
27309 end < glyph
27310 && !INTEGERP (end->object)
27311 && !EQ (end->object, disp_string)
27312 && !(BUFFERP (end->object)
27313 && (end->charpos >= start_charpos
27314 && end->charpos < end_charpos));
27315 ++end)
27317 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27318 are present at buffer positions between START_CHARPOS and
27319 END_CHARPOS, or if they come from an overlay. */
27320 if (EQ (end->object, before_string))
27322 pos = string_buffer_position (before_string, start_charpos);
27323 if (!pos || (pos >= start_charpos && pos < end_charpos))
27324 break;
27326 else if (EQ (end->object, after_string))
27328 pos = string_buffer_position (after_string, end_charpos);
27329 if (!pos || (pos >= start_charpos && pos < end_charpos))
27330 break;
27332 x += end->pixel_width;
27334 /* If we exited the above loop because we arrived at the last
27335 glyph of the row, and its buffer position is still not in
27336 range, it means the last character in range is the preceding
27337 newline. Bump the end column and x values to get past the
27338 last glyph. */
27339 if (end == glyph
27340 && BUFFERP (end->object)
27341 && (end->charpos < start_charpos
27342 || end->charpos >= end_charpos))
27344 x += end->pixel_width;
27345 ++end;
27347 hlinfo->mouse_face_end_x = x;
27348 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27351 hlinfo->mouse_face_window = window;
27352 hlinfo->mouse_face_face_id
27353 = face_at_buffer_position (w, mouse_charpos, &ignore,
27354 mouse_charpos + 1,
27355 !hlinfo->mouse_face_hidden, -1);
27356 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27359 /* The following function is not used anymore (replaced with
27360 mouse_face_from_string_pos), but I leave it here for the time
27361 being, in case someone would. */
27363 #if 0 /* not used */
27365 /* Find the position of the glyph for position POS in OBJECT in
27366 window W's current matrix, and return in *X, *Y the pixel
27367 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27369 RIGHT_P non-zero means return the position of the right edge of the
27370 glyph, RIGHT_P zero means return the left edge position.
27372 If no glyph for POS exists in the matrix, return the position of
27373 the glyph with the next smaller position that is in the matrix, if
27374 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27375 exists in the matrix, return the position of the glyph with the
27376 next larger position in OBJECT.
27378 Value is non-zero if a glyph was found. */
27380 static int
27381 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27382 int *hpos, int *vpos, int *x, int *y, int right_p)
27384 int yb = window_text_bottom_y (w);
27385 struct glyph_row *r;
27386 struct glyph *best_glyph = NULL;
27387 struct glyph_row *best_row = NULL;
27388 int best_x = 0;
27390 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27391 r->enabled_p && r->y < yb;
27392 ++r)
27394 struct glyph *g = r->glyphs[TEXT_AREA];
27395 struct glyph *e = g + r->used[TEXT_AREA];
27396 int gx;
27398 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27399 if (EQ (g->object, object))
27401 if (g->charpos == pos)
27403 best_glyph = g;
27404 best_x = gx;
27405 best_row = r;
27406 goto found;
27408 else if (best_glyph == NULL
27409 || ((eabs (g->charpos - pos)
27410 < eabs (best_glyph->charpos - pos))
27411 && (right_p
27412 ? g->charpos < pos
27413 : g->charpos > pos)))
27415 best_glyph = g;
27416 best_x = gx;
27417 best_row = r;
27422 found:
27424 if (best_glyph)
27426 *x = best_x;
27427 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27429 if (right_p)
27431 *x += best_glyph->pixel_width;
27432 ++*hpos;
27435 *y = best_row->y;
27436 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27439 return best_glyph != NULL;
27441 #endif /* not used */
27443 /* Find the positions of the first and the last glyphs in window W's
27444 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27445 (assumed to be a string), and return in HLINFO's mouse_face_*
27446 members the pixel and column/row coordinates of those glyphs. */
27448 static void
27449 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27450 Lisp_Object object,
27451 ptrdiff_t startpos, ptrdiff_t endpos)
27453 int yb = window_text_bottom_y (w);
27454 struct glyph_row *r;
27455 struct glyph *g, *e;
27456 int gx;
27457 int found = 0;
27459 /* Find the glyph row with at least one position in the range
27460 [STARTPOS..ENDPOS), and the first glyph in that row whose
27461 position belongs to that range. */
27462 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27463 r->enabled_p && r->y < yb;
27464 ++r)
27466 if (!r->reversed_p)
27468 g = r->glyphs[TEXT_AREA];
27469 e = g + r->used[TEXT_AREA];
27470 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27471 if (EQ (g->object, object)
27472 && startpos <= g->charpos && g->charpos < endpos)
27474 hlinfo->mouse_face_beg_row
27475 = MATRIX_ROW_VPOS (r, w->current_matrix);
27476 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27477 hlinfo->mouse_face_beg_x = gx;
27478 found = 1;
27479 break;
27482 else
27484 struct glyph *g1;
27486 e = r->glyphs[TEXT_AREA];
27487 g = e + r->used[TEXT_AREA];
27488 for ( ; g > e; --g)
27489 if (EQ ((g-1)->object, object)
27490 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27492 hlinfo->mouse_face_beg_row
27493 = MATRIX_ROW_VPOS (r, w->current_matrix);
27494 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27495 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27496 gx += g1->pixel_width;
27497 hlinfo->mouse_face_beg_x = gx;
27498 found = 1;
27499 break;
27502 if (found)
27503 break;
27506 if (!found)
27507 return;
27509 /* Starting with the next row, look for the first row which does NOT
27510 include any glyphs whose positions are in the range. */
27511 for (++r; r->enabled_p && r->y < yb; ++r)
27513 g = r->glyphs[TEXT_AREA];
27514 e = g + r->used[TEXT_AREA];
27515 found = 0;
27516 for ( ; g < e; ++g)
27517 if (EQ (g->object, object)
27518 && startpos <= g->charpos && g->charpos < endpos)
27520 found = 1;
27521 break;
27523 if (!found)
27524 break;
27527 /* The highlighted region ends on the previous row. */
27528 r--;
27530 /* Set the end row. */
27531 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27533 /* Compute and set the end column and the end column's horizontal
27534 pixel coordinate. */
27535 if (!r->reversed_p)
27537 g = r->glyphs[TEXT_AREA];
27538 e = g + r->used[TEXT_AREA];
27539 for ( ; e > g; --e)
27540 if (EQ ((e-1)->object, object)
27541 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27542 break;
27543 hlinfo->mouse_face_end_col = e - g;
27545 for (gx = r->x; g < e; ++g)
27546 gx += g->pixel_width;
27547 hlinfo->mouse_face_end_x = gx;
27549 else
27551 e = r->glyphs[TEXT_AREA];
27552 g = e + r->used[TEXT_AREA];
27553 for (gx = r->x ; e < g; ++e)
27555 if (EQ (e->object, object)
27556 && startpos <= e->charpos && e->charpos < endpos)
27557 break;
27558 gx += e->pixel_width;
27560 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27561 hlinfo->mouse_face_end_x = gx;
27565 #ifdef HAVE_WINDOW_SYSTEM
27567 /* See if position X, Y is within a hot-spot of an image. */
27569 static int
27570 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27572 if (!CONSP (hot_spot))
27573 return 0;
27575 if (EQ (XCAR (hot_spot), Qrect))
27577 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27578 Lisp_Object rect = XCDR (hot_spot);
27579 Lisp_Object tem;
27580 if (!CONSP (rect))
27581 return 0;
27582 if (!CONSP (XCAR (rect)))
27583 return 0;
27584 if (!CONSP (XCDR (rect)))
27585 return 0;
27586 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27587 return 0;
27588 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27589 return 0;
27590 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27591 return 0;
27592 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27593 return 0;
27594 return 1;
27596 else if (EQ (XCAR (hot_spot), Qcircle))
27598 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27599 Lisp_Object circ = XCDR (hot_spot);
27600 Lisp_Object lr, lx0, ly0;
27601 if (CONSP (circ)
27602 && CONSP (XCAR (circ))
27603 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27604 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27605 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27607 double r = XFLOATINT (lr);
27608 double dx = XINT (lx0) - x;
27609 double dy = XINT (ly0) - y;
27610 return (dx * dx + dy * dy <= r * r);
27613 else if (EQ (XCAR (hot_spot), Qpoly))
27615 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27616 if (VECTORP (XCDR (hot_spot)))
27618 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27619 Lisp_Object *poly = v->contents;
27620 ptrdiff_t n = v->header.size;
27621 ptrdiff_t i;
27622 int inside = 0;
27623 Lisp_Object lx, ly;
27624 int x0, y0;
27626 /* Need an even number of coordinates, and at least 3 edges. */
27627 if (n < 6 || n & 1)
27628 return 0;
27630 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27631 If count is odd, we are inside polygon. Pixels on edges
27632 may or may not be included depending on actual geometry of the
27633 polygon. */
27634 if ((lx = poly[n-2], !INTEGERP (lx))
27635 || (ly = poly[n-1], !INTEGERP (lx)))
27636 return 0;
27637 x0 = XINT (lx), y0 = XINT (ly);
27638 for (i = 0; i < n; i += 2)
27640 int x1 = x0, y1 = y0;
27641 if ((lx = poly[i], !INTEGERP (lx))
27642 || (ly = poly[i+1], !INTEGERP (ly)))
27643 return 0;
27644 x0 = XINT (lx), y0 = XINT (ly);
27646 /* Does this segment cross the X line? */
27647 if (x0 >= x)
27649 if (x1 >= x)
27650 continue;
27652 else if (x1 < x)
27653 continue;
27654 if (y > y0 && y > y1)
27655 continue;
27656 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27657 inside = !inside;
27659 return inside;
27662 return 0;
27665 Lisp_Object
27666 find_hot_spot (Lisp_Object map, int x, int y)
27668 while (CONSP (map))
27670 if (CONSP (XCAR (map))
27671 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27672 return XCAR (map);
27673 map = XCDR (map);
27676 return Qnil;
27679 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27680 3, 3, 0,
27681 doc: /* Lookup in image map MAP coordinates X and Y.
27682 An image map is an alist where each element has the format (AREA ID PLIST).
27683 An AREA is specified as either a rectangle, a circle, or a polygon:
27684 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27685 pixel coordinates of the upper left and bottom right corners.
27686 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27687 and the radius of the circle; r may be a float or integer.
27688 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27689 vector describes one corner in the polygon.
27690 Returns the alist element for the first matching AREA in MAP. */)
27691 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27693 if (NILP (map))
27694 return Qnil;
27696 CHECK_NUMBER (x);
27697 CHECK_NUMBER (y);
27699 return find_hot_spot (map,
27700 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27701 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27705 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27706 static void
27707 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27709 /* Do not change cursor shape while dragging mouse. */
27710 if (!NILP (do_mouse_tracking))
27711 return;
27713 if (!NILP (pointer))
27715 if (EQ (pointer, Qarrow))
27716 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27717 else if (EQ (pointer, Qhand))
27718 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27719 else if (EQ (pointer, Qtext))
27720 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27721 else if (EQ (pointer, intern ("hdrag")))
27722 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27723 #ifdef HAVE_X_WINDOWS
27724 else if (EQ (pointer, intern ("vdrag")))
27725 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27726 #endif
27727 else if (EQ (pointer, intern ("hourglass")))
27728 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27729 else if (EQ (pointer, Qmodeline))
27730 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27731 else
27732 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27735 if (cursor != No_Cursor)
27736 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27739 #endif /* HAVE_WINDOW_SYSTEM */
27741 /* Take proper action when mouse has moved to the mode or header line
27742 or marginal area AREA of window W, x-position X and y-position Y.
27743 X is relative to the start of the text display area of W, so the
27744 width of bitmap areas and scroll bars must be subtracted to get a
27745 position relative to the start of the mode line. */
27747 static void
27748 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27749 enum window_part area)
27751 struct window *w = XWINDOW (window);
27752 struct frame *f = XFRAME (w->frame);
27753 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27754 #ifdef HAVE_WINDOW_SYSTEM
27755 Display_Info *dpyinfo;
27756 #endif
27757 Cursor cursor = No_Cursor;
27758 Lisp_Object pointer = Qnil;
27759 int dx, dy, width, height;
27760 ptrdiff_t charpos;
27761 Lisp_Object string, object = Qnil;
27762 Lisp_Object pos IF_LINT (= Qnil), help;
27764 Lisp_Object mouse_face;
27765 int original_x_pixel = x;
27766 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27767 struct glyph_row *row IF_LINT (= 0);
27769 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27771 int x0;
27772 struct glyph *end;
27774 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27775 returns them in row/column units! */
27776 string = mode_line_string (w, area, &x, &y, &charpos,
27777 &object, &dx, &dy, &width, &height);
27779 row = (area == ON_MODE_LINE
27780 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27781 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27783 /* Find the glyph under the mouse pointer. */
27784 if (row->mode_line_p && row->enabled_p)
27786 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27787 end = glyph + row->used[TEXT_AREA];
27789 for (x0 = original_x_pixel;
27790 glyph < end && x0 >= glyph->pixel_width;
27791 ++glyph)
27792 x0 -= glyph->pixel_width;
27794 if (glyph >= end)
27795 glyph = NULL;
27798 else
27800 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27801 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27802 returns them in row/column units! */
27803 string = marginal_area_string (w, area, &x, &y, &charpos,
27804 &object, &dx, &dy, &width, &height);
27807 help = Qnil;
27809 #ifdef HAVE_WINDOW_SYSTEM
27810 if (IMAGEP (object))
27812 Lisp_Object image_map, hotspot;
27813 if ((image_map = Fplist_get (XCDR (object), QCmap),
27814 !NILP (image_map))
27815 && (hotspot = find_hot_spot (image_map, dx, dy),
27816 CONSP (hotspot))
27817 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27819 Lisp_Object plist;
27821 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27822 If so, we could look for mouse-enter, mouse-leave
27823 properties in PLIST (and do something...). */
27824 hotspot = XCDR (hotspot);
27825 if (CONSP (hotspot)
27826 && (plist = XCAR (hotspot), CONSP (plist)))
27828 pointer = Fplist_get (plist, Qpointer);
27829 if (NILP (pointer))
27830 pointer = Qhand;
27831 help = Fplist_get (plist, Qhelp_echo);
27832 if (!NILP (help))
27834 help_echo_string = help;
27835 XSETWINDOW (help_echo_window, w);
27836 help_echo_object = w->contents;
27837 help_echo_pos = charpos;
27841 if (NILP (pointer))
27842 pointer = Fplist_get (XCDR (object), QCpointer);
27844 #endif /* HAVE_WINDOW_SYSTEM */
27846 if (STRINGP (string))
27847 pos = make_number (charpos);
27849 /* Set the help text and mouse pointer. If the mouse is on a part
27850 of the mode line without any text (e.g. past the right edge of
27851 the mode line text), use the default help text and pointer. */
27852 if (STRINGP (string) || area == ON_MODE_LINE)
27854 /* Arrange to display the help by setting the global variables
27855 help_echo_string, help_echo_object, and help_echo_pos. */
27856 if (NILP (help))
27858 if (STRINGP (string))
27859 help = Fget_text_property (pos, Qhelp_echo, string);
27861 if (!NILP (help))
27863 help_echo_string = help;
27864 XSETWINDOW (help_echo_window, w);
27865 help_echo_object = string;
27866 help_echo_pos = charpos;
27868 else if (area == ON_MODE_LINE)
27870 Lisp_Object default_help
27871 = buffer_local_value_1 (Qmode_line_default_help_echo,
27872 w->contents);
27874 if (STRINGP (default_help))
27876 help_echo_string = default_help;
27877 XSETWINDOW (help_echo_window, w);
27878 help_echo_object = Qnil;
27879 help_echo_pos = -1;
27884 #ifdef HAVE_WINDOW_SYSTEM
27885 /* Change the mouse pointer according to what is under it. */
27886 if (FRAME_WINDOW_P (f))
27888 dpyinfo = FRAME_DISPLAY_INFO (f);
27889 if (STRINGP (string))
27891 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27893 if (NILP (pointer))
27894 pointer = Fget_text_property (pos, Qpointer, string);
27896 /* Change the mouse pointer according to what is under X/Y. */
27897 if (NILP (pointer)
27898 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27900 Lisp_Object map;
27901 map = Fget_text_property (pos, Qlocal_map, string);
27902 if (!KEYMAPP (map))
27903 map = Fget_text_property (pos, Qkeymap, string);
27904 if (!KEYMAPP (map))
27905 cursor = dpyinfo->vertical_scroll_bar_cursor;
27908 else
27909 /* Default mode-line pointer. */
27910 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27912 #endif
27915 /* Change the mouse face according to what is under X/Y. */
27916 if (STRINGP (string))
27918 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27919 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27920 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27921 && glyph)
27923 Lisp_Object b, e;
27925 struct glyph * tmp_glyph;
27927 int gpos;
27928 int gseq_length;
27929 int total_pixel_width;
27930 ptrdiff_t begpos, endpos, ignore;
27932 int vpos, hpos;
27934 b = Fprevious_single_property_change (make_number (charpos + 1),
27935 Qmouse_face, string, Qnil);
27936 if (NILP (b))
27937 begpos = 0;
27938 else
27939 begpos = XINT (b);
27941 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27942 if (NILP (e))
27943 endpos = SCHARS (string);
27944 else
27945 endpos = XINT (e);
27947 /* Calculate the glyph position GPOS of GLYPH in the
27948 displayed string, relative to the beginning of the
27949 highlighted part of the string.
27951 Note: GPOS is different from CHARPOS. CHARPOS is the
27952 position of GLYPH in the internal string object. A mode
27953 line string format has structures which are converted to
27954 a flattened string by the Emacs Lisp interpreter. The
27955 internal string is an element of those structures. The
27956 displayed string is the flattened string. */
27957 tmp_glyph = row_start_glyph;
27958 while (tmp_glyph < glyph
27959 && (!(EQ (tmp_glyph->object, glyph->object)
27960 && begpos <= tmp_glyph->charpos
27961 && tmp_glyph->charpos < endpos)))
27962 tmp_glyph++;
27963 gpos = glyph - tmp_glyph;
27965 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27966 the highlighted part of the displayed string to which
27967 GLYPH belongs. Note: GSEQ_LENGTH is different from
27968 SCHARS (STRING), because the latter returns the length of
27969 the internal string. */
27970 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27971 tmp_glyph > glyph
27972 && (!(EQ (tmp_glyph->object, glyph->object)
27973 && begpos <= tmp_glyph->charpos
27974 && tmp_glyph->charpos < endpos));
27975 tmp_glyph--)
27977 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27979 /* Calculate the total pixel width of all the glyphs between
27980 the beginning of the highlighted area and GLYPH. */
27981 total_pixel_width = 0;
27982 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27983 total_pixel_width += tmp_glyph->pixel_width;
27985 /* Pre calculation of re-rendering position. Note: X is in
27986 column units here, after the call to mode_line_string or
27987 marginal_area_string. */
27988 hpos = x - gpos;
27989 vpos = (area == ON_MODE_LINE
27990 ? (w->current_matrix)->nrows - 1
27991 : 0);
27993 /* If GLYPH's position is included in the region that is
27994 already drawn in mouse face, we have nothing to do. */
27995 if ( EQ (window, hlinfo->mouse_face_window)
27996 && (!row->reversed_p
27997 ? (hlinfo->mouse_face_beg_col <= hpos
27998 && hpos < hlinfo->mouse_face_end_col)
27999 /* In R2L rows we swap BEG and END, see below. */
28000 : (hlinfo->mouse_face_end_col <= hpos
28001 && hpos < hlinfo->mouse_face_beg_col))
28002 && hlinfo->mouse_face_beg_row == vpos )
28003 return;
28005 if (clear_mouse_face (hlinfo))
28006 cursor = No_Cursor;
28008 if (!row->reversed_p)
28010 hlinfo->mouse_face_beg_col = hpos;
28011 hlinfo->mouse_face_beg_x = original_x_pixel
28012 - (total_pixel_width + dx);
28013 hlinfo->mouse_face_end_col = hpos + gseq_length;
28014 hlinfo->mouse_face_end_x = 0;
28016 else
28018 /* In R2L rows, show_mouse_face expects BEG and END
28019 coordinates to be swapped. */
28020 hlinfo->mouse_face_end_col = hpos;
28021 hlinfo->mouse_face_end_x = original_x_pixel
28022 - (total_pixel_width + dx);
28023 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28024 hlinfo->mouse_face_beg_x = 0;
28027 hlinfo->mouse_face_beg_row = vpos;
28028 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28029 hlinfo->mouse_face_past_end = 0;
28030 hlinfo->mouse_face_window = window;
28032 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28033 charpos,
28034 0, &ignore,
28035 glyph->face_id,
28037 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28039 if (NILP (pointer))
28040 pointer = Qhand;
28042 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28043 clear_mouse_face (hlinfo);
28045 #ifdef HAVE_WINDOW_SYSTEM
28046 if (FRAME_WINDOW_P (f))
28047 define_frame_cursor1 (f, cursor, pointer);
28048 #endif
28052 /* EXPORT:
28053 Take proper action when the mouse has moved to position X, Y on
28054 frame F with regards to highlighting portions of display that have
28055 mouse-face properties. Also de-highlight portions of display where
28056 the mouse was before, set the mouse pointer shape as appropriate
28057 for the mouse coordinates, and activate help echo (tooltips).
28058 X and Y can be negative or out of range. */
28060 void
28061 note_mouse_highlight (struct frame *f, int x, int y)
28063 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28064 enum window_part part = ON_NOTHING;
28065 Lisp_Object window;
28066 struct window *w;
28067 Cursor cursor = No_Cursor;
28068 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28069 struct buffer *b;
28071 /* When a menu is active, don't highlight because this looks odd. */
28072 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28073 if (popup_activated ())
28074 return;
28075 #endif
28077 if (!f->glyphs_initialized_p
28078 || f->pointer_invisible)
28079 return;
28081 hlinfo->mouse_face_mouse_x = x;
28082 hlinfo->mouse_face_mouse_y = y;
28083 hlinfo->mouse_face_mouse_frame = f;
28085 if (hlinfo->mouse_face_defer)
28086 return;
28088 /* Which window is that in? */
28089 window = window_from_coordinates (f, x, y, &part, 1);
28091 /* If displaying active text in another window, clear that. */
28092 if (! EQ (window, hlinfo->mouse_face_window)
28093 /* Also clear if we move out of text area in same window. */
28094 || (!NILP (hlinfo->mouse_face_window)
28095 && !NILP (window)
28096 && part != ON_TEXT
28097 && part != ON_MODE_LINE
28098 && part != ON_HEADER_LINE))
28099 clear_mouse_face (hlinfo);
28101 /* Not on a window -> return. */
28102 if (!WINDOWP (window))
28103 return;
28105 /* Reset help_echo_string. It will get recomputed below. */
28106 help_echo_string = Qnil;
28108 /* Convert to window-relative pixel coordinates. */
28109 w = XWINDOW (window);
28110 frame_to_window_pixel_xy (w, &x, &y);
28112 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28113 /* Handle tool-bar window differently since it doesn't display a
28114 buffer. */
28115 if (EQ (window, f->tool_bar_window))
28117 note_tool_bar_highlight (f, x, y);
28118 return;
28120 #endif
28122 /* Mouse is on the mode, header line or margin? */
28123 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28124 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28126 note_mode_line_or_margin_highlight (window, x, y, part);
28127 return;
28130 #ifdef HAVE_WINDOW_SYSTEM
28131 if (part == ON_VERTICAL_BORDER)
28133 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28134 help_echo_string = build_string ("drag-mouse-1: resize");
28136 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28137 || part == ON_SCROLL_BAR)
28138 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28139 else
28140 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28141 #endif
28143 /* Are we in a window whose display is up to date?
28144 And verify the buffer's text has not changed. */
28145 b = XBUFFER (w->contents);
28146 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28148 int hpos, vpos, dx, dy, area = LAST_AREA;
28149 ptrdiff_t pos;
28150 struct glyph *glyph;
28151 Lisp_Object object;
28152 Lisp_Object mouse_face = Qnil, position;
28153 Lisp_Object *overlay_vec = NULL;
28154 ptrdiff_t i, noverlays;
28155 struct buffer *obuf;
28156 ptrdiff_t obegv, ozv;
28157 int same_region;
28159 /* Find the glyph under X/Y. */
28160 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28162 #ifdef HAVE_WINDOW_SYSTEM
28163 /* Look for :pointer property on image. */
28164 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28166 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28167 if (img != NULL && IMAGEP (img->spec))
28169 Lisp_Object image_map, hotspot;
28170 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28171 !NILP (image_map))
28172 && (hotspot = find_hot_spot (image_map,
28173 glyph->slice.img.x + dx,
28174 glyph->slice.img.y + dy),
28175 CONSP (hotspot))
28176 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28178 Lisp_Object plist;
28180 /* Could check XCAR (hotspot) to see if we enter/leave
28181 this hot-spot.
28182 If so, we could look for mouse-enter, mouse-leave
28183 properties in PLIST (and do something...). */
28184 hotspot = XCDR (hotspot);
28185 if (CONSP (hotspot)
28186 && (plist = XCAR (hotspot), CONSP (plist)))
28188 pointer = Fplist_get (plist, Qpointer);
28189 if (NILP (pointer))
28190 pointer = Qhand;
28191 help_echo_string = Fplist_get (plist, Qhelp_echo);
28192 if (!NILP (help_echo_string))
28194 help_echo_window = window;
28195 help_echo_object = glyph->object;
28196 help_echo_pos = glyph->charpos;
28200 if (NILP (pointer))
28201 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28204 #endif /* HAVE_WINDOW_SYSTEM */
28206 /* Clear mouse face if X/Y not over text. */
28207 if (glyph == NULL
28208 || area != TEXT_AREA
28209 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28210 /* Glyph's OBJECT is an integer for glyphs inserted by the
28211 display engine for its internal purposes, like truncation
28212 and continuation glyphs and blanks beyond the end of
28213 line's text on text terminals. If we are over such a
28214 glyph, we are not over any text. */
28215 || INTEGERP (glyph->object)
28216 /* R2L rows have a stretch glyph at their front, which
28217 stands for no text, whereas L2R rows have no glyphs at
28218 all beyond the end of text. Treat such stretch glyphs
28219 like we do with NULL glyphs in L2R rows. */
28220 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28221 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28222 && glyph->type == STRETCH_GLYPH
28223 && glyph->avoid_cursor_p))
28225 if (clear_mouse_face (hlinfo))
28226 cursor = No_Cursor;
28227 #ifdef HAVE_WINDOW_SYSTEM
28228 if (FRAME_WINDOW_P (f) && NILP (pointer))
28230 if (area != TEXT_AREA)
28231 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28232 else
28233 pointer = Vvoid_text_area_pointer;
28235 #endif
28236 goto set_cursor;
28239 pos = glyph->charpos;
28240 object = glyph->object;
28241 if (!STRINGP (object) && !BUFFERP (object))
28242 goto set_cursor;
28244 /* If we get an out-of-range value, return now; avoid an error. */
28245 if (BUFFERP (object) && pos > BUF_Z (b))
28246 goto set_cursor;
28248 /* Make the window's buffer temporarily current for
28249 overlays_at and compute_char_face. */
28250 obuf = current_buffer;
28251 current_buffer = b;
28252 obegv = BEGV;
28253 ozv = ZV;
28254 BEGV = BEG;
28255 ZV = Z;
28257 /* Is this char mouse-active or does it have help-echo? */
28258 position = make_number (pos);
28260 if (BUFFERP (object))
28262 /* Put all the overlays we want in a vector in overlay_vec. */
28263 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28264 /* Sort overlays into increasing priority order. */
28265 noverlays = sort_overlays (overlay_vec, noverlays, w);
28267 else
28268 noverlays = 0;
28270 if (NILP (Vmouse_highlight))
28272 clear_mouse_face (hlinfo);
28273 goto check_help_echo;
28276 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28278 if (same_region)
28279 cursor = No_Cursor;
28281 /* Check mouse-face highlighting. */
28282 if (! same_region
28283 /* If there exists an overlay with mouse-face overlapping
28284 the one we are currently highlighting, we have to
28285 check if we enter the overlapping overlay, and then
28286 highlight only that. */
28287 || (OVERLAYP (hlinfo->mouse_face_overlay)
28288 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28290 /* Find the highest priority overlay with a mouse-face. */
28291 Lisp_Object overlay = Qnil;
28292 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28294 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28295 if (!NILP (mouse_face))
28296 overlay = overlay_vec[i];
28299 /* If we're highlighting the same overlay as before, there's
28300 no need to do that again. */
28301 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28302 goto check_help_echo;
28303 hlinfo->mouse_face_overlay = overlay;
28305 /* Clear the display of the old active region, if any. */
28306 if (clear_mouse_face (hlinfo))
28307 cursor = No_Cursor;
28309 /* If no overlay applies, get a text property. */
28310 if (NILP (overlay))
28311 mouse_face = Fget_text_property (position, Qmouse_face, object);
28313 /* Next, compute the bounds of the mouse highlighting and
28314 display it. */
28315 if (!NILP (mouse_face) && STRINGP (object))
28317 /* The mouse-highlighting comes from a display string
28318 with a mouse-face. */
28319 Lisp_Object s, e;
28320 ptrdiff_t ignore;
28322 s = Fprevious_single_property_change
28323 (make_number (pos + 1), Qmouse_face, object, Qnil);
28324 e = Fnext_single_property_change
28325 (position, Qmouse_face, object, Qnil);
28326 if (NILP (s))
28327 s = make_number (0);
28328 if (NILP (e))
28329 e = make_number (SCHARS (object));
28330 mouse_face_from_string_pos (w, hlinfo, object,
28331 XINT (s), XINT (e));
28332 hlinfo->mouse_face_past_end = 0;
28333 hlinfo->mouse_face_window = window;
28334 hlinfo->mouse_face_face_id
28335 = face_at_string_position (w, object, pos, 0, &ignore,
28336 glyph->face_id, 1);
28337 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28338 cursor = No_Cursor;
28340 else
28342 /* The mouse-highlighting, if any, comes from an overlay
28343 or text property in the buffer. */
28344 Lisp_Object buffer IF_LINT (= Qnil);
28345 Lisp_Object disp_string IF_LINT (= Qnil);
28347 if (STRINGP (object))
28349 /* If we are on a display string with no mouse-face,
28350 check if the text under it has one. */
28351 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28352 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28353 pos = string_buffer_position (object, start);
28354 if (pos > 0)
28356 mouse_face = get_char_property_and_overlay
28357 (make_number (pos), Qmouse_face, w->contents, &overlay);
28358 buffer = w->contents;
28359 disp_string = object;
28362 else
28364 buffer = object;
28365 disp_string = Qnil;
28368 if (!NILP (mouse_face))
28370 Lisp_Object before, after;
28371 Lisp_Object before_string, after_string;
28372 /* To correctly find the limits of mouse highlight
28373 in a bidi-reordered buffer, we must not use the
28374 optimization of limiting the search in
28375 previous-single-property-change and
28376 next-single-property-change, because
28377 rows_from_pos_range needs the real start and end
28378 positions to DTRT in this case. That's because
28379 the first row visible in a window does not
28380 necessarily display the character whose position
28381 is the smallest. */
28382 Lisp_Object lim1
28383 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28384 ? Fmarker_position (w->start)
28385 : Qnil;
28386 Lisp_Object lim2
28387 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28388 ? make_number (BUF_Z (XBUFFER (buffer))
28389 - w->window_end_pos)
28390 : Qnil;
28392 if (NILP (overlay))
28394 /* Handle the text property case. */
28395 before = Fprevious_single_property_change
28396 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28397 after = Fnext_single_property_change
28398 (make_number (pos), Qmouse_face, buffer, lim2);
28399 before_string = after_string = Qnil;
28401 else
28403 /* Handle the overlay case. */
28404 before = Foverlay_start (overlay);
28405 after = Foverlay_end (overlay);
28406 before_string = Foverlay_get (overlay, Qbefore_string);
28407 after_string = Foverlay_get (overlay, Qafter_string);
28409 if (!STRINGP (before_string)) before_string = Qnil;
28410 if (!STRINGP (after_string)) after_string = Qnil;
28413 mouse_face_from_buffer_pos (window, hlinfo, pos,
28414 NILP (before)
28416 : XFASTINT (before),
28417 NILP (after)
28418 ? BUF_Z (XBUFFER (buffer))
28419 : XFASTINT (after),
28420 before_string, after_string,
28421 disp_string);
28422 cursor = No_Cursor;
28427 check_help_echo:
28429 /* Look for a `help-echo' property. */
28430 if (NILP (help_echo_string)) {
28431 Lisp_Object help, overlay;
28433 /* Check overlays first. */
28434 help = overlay = Qnil;
28435 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28437 overlay = overlay_vec[i];
28438 help = Foverlay_get (overlay, Qhelp_echo);
28441 if (!NILP (help))
28443 help_echo_string = help;
28444 help_echo_window = window;
28445 help_echo_object = overlay;
28446 help_echo_pos = pos;
28448 else
28450 Lisp_Object obj = glyph->object;
28451 ptrdiff_t charpos = glyph->charpos;
28453 /* Try text properties. */
28454 if (STRINGP (obj)
28455 && charpos >= 0
28456 && charpos < SCHARS (obj))
28458 help = Fget_text_property (make_number (charpos),
28459 Qhelp_echo, obj);
28460 if (NILP (help))
28462 /* If the string itself doesn't specify a help-echo,
28463 see if the buffer text ``under'' it does. */
28464 struct glyph_row *r
28465 = MATRIX_ROW (w->current_matrix, vpos);
28466 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28467 ptrdiff_t p = string_buffer_position (obj, start);
28468 if (p > 0)
28470 help = Fget_char_property (make_number (p),
28471 Qhelp_echo, w->contents);
28472 if (!NILP (help))
28474 charpos = p;
28475 obj = w->contents;
28480 else if (BUFFERP (obj)
28481 && charpos >= BEGV
28482 && charpos < ZV)
28483 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28484 obj);
28486 if (!NILP (help))
28488 help_echo_string = help;
28489 help_echo_window = window;
28490 help_echo_object = obj;
28491 help_echo_pos = charpos;
28496 #ifdef HAVE_WINDOW_SYSTEM
28497 /* Look for a `pointer' property. */
28498 if (FRAME_WINDOW_P (f) && NILP (pointer))
28500 /* Check overlays first. */
28501 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28502 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28504 if (NILP (pointer))
28506 Lisp_Object obj = glyph->object;
28507 ptrdiff_t charpos = glyph->charpos;
28509 /* Try text properties. */
28510 if (STRINGP (obj)
28511 && charpos >= 0
28512 && charpos < SCHARS (obj))
28514 pointer = Fget_text_property (make_number (charpos),
28515 Qpointer, obj);
28516 if (NILP (pointer))
28518 /* If the string itself doesn't specify a pointer,
28519 see if the buffer text ``under'' it does. */
28520 struct glyph_row *r
28521 = MATRIX_ROW (w->current_matrix, vpos);
28522 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28523 ptrdiff_t p = string_buffer_position (obj, start);
28524 if (p > 0)
28525 pointer = Fget_char_property (make_number (p),
28526 Qpointer, w->contents);
28529 else if (BUFFERP (obj)
28530 && charpos >= BEGV
28531 && charpos < ZV)
28532 pointer = Fget_text_property (make_number (charpos),
28533 Qpointer, obj);
28536 #endif /* HAVE_WINDOW_SYSTEM */
28538 BEGV = obegv;
28539 ZV = ozv;
28540 current_buffer = obuf;
28543 set_cursor:
28545 #ifdef HAVE_WINDOW_SYSTEM
28546 if (FRAME_WINDOW_P (f))
28547 define_frame_cursor1 (f, cursor, pointer);
28548 #else
28549 /* This is here to prevent a compiler error, about "label at end of
28550 compound statement". */
28551 return;
28552 #endif
28556 /* EXPORT for RIF:
28557 Clear any mouse-face on window W. This function is part of the
28558 redisplay interface, and is called from try_window_id and similar
28559 functions to ensure the mouse-highlight is off. */
28561 void
28562 x_clear_window_mouse_face (struct window *w)
28564 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28565 Lisp_Object window;
28567 block_input ();
28568 XSETWINDOW (window, w);
28569 if (EQ (window, hlinfo->mouse_face_window))
28570 clear_mouse_face (hlinfo);
28571 unblock_input ();
28575 /* EXPORT:
28576 Just discard the mouse face information for frame F, if any.
28577 This is used when the size of F is changed. */
28579 void
28580 cancel_mouse_face (struct frame *f)
28582 Lisp_Object window;
28583 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28585 window = hlinfo->mouse_face_window;
28586 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28587 reset_mouse_highlight (hlinfo);
28592 /***********************************************************************
28593 Exposure Events
28594 ***********************************************************************/
28596 #ifdef HAVE_WINDOW_SYSTEM
28598 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28599 which intersects rectangle R. R is in window-relative coordinates. */
28601 static void
28602 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28603 enum glyph_row_area area)
28605 struct glyph *first = row->glyphs[area];
28606 struct glyph *end = row->glyphs[area] + row->used[area];
28607 struct glyph *last;
28608 int first_x, start_x, x;
28610 if (area == TEXT_AREA && row->fill_line_p)
28611 /* If row extends face to end of line write the whole line. */
28612 draw_glyphs (w, 0, row, area,
28613 0, row->used[area],
28614 DRAW_NORMAL_TEXT, 0);
28615 else
28617 /* Set START_X to the window-relative start position for drawing glyphs of
28618 AREA. The first glyph of the text area can be partially visible.
28619 The first glyphs of other areas cannot. */
28620 start_x = window_box_left_offset (w, area);
28621 x = start_x;
28622 if (area == TEXT_AREA)
28623 x += row->x;
28625 /* Find the first glyph that must be redrawn. */
28626 while (first < end
28627 && x + first->pixel_width < r->x)
28629 x += first->pixel_width;
28630 ++first;
28633 /* Find the last one. */
28634 last = first;
28635 first_x = x;
28636 while (last < end
28637 && x < r->x + r->width)
28639 x += last->pixel_width;
28640 ++last;
28643 /* Repaint. */
28644 if (last > first)
28645 draw_glyphs (w, first_x - start_x, row, area,
28646 first - row->glyphs[area], last - row->glyphs[area],
28647 DRAW_NORMAL_TEXT, 0);
28652 /* Redraw the parts of the glyph row ROW on window W intersecting
28653 rectangle R. R is in window-relative coordinates. Value is
28654 non-zero if mouse-face was overwritten. */
28656 static int
28657 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28659 eassert (row->enabled_p);
28661 if (row->mode_line_p || w->pseudo_window_p)
28662 draw_glyphs (w, 0, row, TEXT_AREA,
28663 0, row->used[TEXT_AREA],
28664 DRAW_NORMAL_TEXT, 0);
28665 else
28667 if (row->used[LEFT_MARGIN_AREA])
28668 expose_area (w, row, r, LEFT_MARGIN_AREA);
28669 if (row->used[TEXT_AREA])
28670 expose_area (w, row, r, TEXT_AREA);
28671 if (row->used[RIGHT_MARGIN_AREA])
28672 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28673 draw_row_fringe_bitmaps (w, row);
28676 return row->mouse_face_p;
28680 /* Redraw those parts of glyphs rows during expose event handling that
28681 overlap other rows. Redrawing of an exposed line writes over parts
28682 of lines overlapping that exposed line; this function fixes that.
28684 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28685 row in W's current matrix that is exposed and overlaps other rows.
28686 LAST_OVERLAPPING_ROW is the last such row. */
28688 static void
28689 expose_overlaps (struct window *w,
28690 struct glyph_row *first_overlapping_row,
28691 struct glyph_row *last_overlapping_row,
28692 XRectangle *r)
28694 struct glyph_row *row;
28696 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28697 if (row->overlapping_p)
28699 eassert (row->enabled_p && !row->mode_line_p);
28701 row->clip = r;
28702 if (row->used[LEFT_MARGIN_AREA])
28703 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28705 if (row->used[TEXT_AREA])
28706 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28708 if (row->used[RIGHT_MARGIN_AREA])
28709 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28710 row->clip = NULL;
28715 /* Return non-zero if W's cursor intersects rectangle R. */
28717 static int
28718 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28720 XRectangle cr, result;
28721 struct glyph *cursor_glyph;
28722 struct glyph_row *row;
28724 if (w->phys_cursor.vpos >= 0
28725 && w->phys_cursor.vpos < w->current_matrix->nrows
28726 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28727 row->enabled_p)
28728 && row->cursor_in_fringe_p)
28730 /* Cursor is in the fringe. */
28731 cr.x = window_box_right_offset (w,
28732 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28733 ? RIGHT_MARGIN_AREA
28734 : TEXT_AREA));
28735 cr.y = row->y;
28736 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28737 cr.height = row->height;
28738 return x_intersect_rectangles (&cr, r, &result);
28741 cursor_glyph = get_phys_cursor_glyph (w);
28742 if (cursor_glyph)
28744 /* r is relative to W's box, but w->phys_cursor.x is relative
28745 to left edge of W's TEXT area. Adjust it. */
28746 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28747 cr.y = w->phys_cursor.y;
28748 cr.width = cursor_glyph->pixel_width;
28749 cr.height = w->phys_cursor_height;
28750 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28751 I assume the effect is the same -- and this is portable. */
28752 return x_intersect_rectangles (&cr, r, &result);
28754 /* If we don't understand the format, pretend we're not in the hot-spot. */
28755 return 0;
28759 /* EXPORT:
28760 Draw a vertical window border to the right of window W if W doesn't
28761 have vertical scroll bars. */
28763 void
28764 x_draw_vertical_border (struct window *w)
28766 struct frame *f = XFRAME (WINDOW_FRAME (w));
28768 /* We could do better, if we knew what type of scroll-bar the adjacent
28769 windows (on either side) have... But we don't :-(
28770 However, I think this works ok. ++KFS 2003-04-25 */
28772 /* Redraw borders between horizontally adjacent windows. Don't
28773 do it for frames with vertical scroll bars because either the
28774 right scroll bar of a window, or the left scroll bar of its
28775 neighbor will suffice as a border. */
28776 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28777 return;
28779 /* Note: It is necessary to redraw both the left and the right
28780 borders, for when only this single window W is being
28781 redisplayed. */
28782 if (!WINDOW_RIGHTMOST_P (w)
28783 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28785 int x0, x1, y0, y1;
28787 window_box_edges (w, &x0, &y0, &x1, &y1);
28788 y1 -= 1;
28790 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28791 x1 -= 1;
28793 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28795 if (!WINDOW_LEFTMOST_P (w)
28796 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28798 int x0, x1, y0, y1;
28800 window_box_edges (w, &x0, &y0, &x1, &y1);
28801 y1 -= 1;
28803 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28804 x0 -= 1;
28806 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28811 /* Redraw the part of window W intersection rectangle FR. Pixel
28812 coordinates in FR are frame-relative. Call this function with
28813 input blocked. Value is non-zero if the exposure overwrites
28814 mouse-face. */
28816 static int
28817 expose_window (struct window *w, XRectangle *fr)
28819 struct frame *f = XFRAME (w->frame);
28820 XRectangle wr, r;
28821 int mouse_face_overwritten_p = 0;
28823 /* If window is not yet fully initialized, do nothing. This can
28824 happen when toolkit scroll bars are used and a window is split.
28825 Reconfiguring the scroll bar will generate an expose for a newly
28826 created window. */
28827 if (w->current_matrix == NULL)
28828 return 0;
28830 /* When we're currently updating the window, display and current
28831 matrix usually don't agree. Arrange for a thorough display
28832 later. */
28833 if (w->must_be_updated_p)
28835 SET_FRAME_GARBAGED (f);
28836 return 0;
28839 /* Frame-relative pixel rectangle of W. */
28840 wr.x = WINDOW_LEFT_EDGE_X (w);
28841 wr.y = WINDOW_TOP_EDGE_Y (w);
28842 wr.width = WINDOW_TOTAL_WIDTH (w);
28843 wr.height = WINDOW_TOTAL_HEIGHT (w);
28845 if (x_intersect_rectangles (fr, &wr, &r))
28847 int yb = window_text_bottom_y (w);
28848 struct glyph_row *row;
28849 int cursor_cleared_p, phys_cursor_on_p;
28850 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28852 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28853 r.x, r.y, r.width, r.height));
28855 /* Convert to window coordinates. */
28856 r.x -= WINDOW_LEFT_EDGE_X (w);
28857 r.y -= WINDOW_TOP_EDGE_Y (w);
28859 /* Turn off the cursor. */
28860 if (!w->pseudo_window_p
28861 && phys_cursor_in_rect_p (w, &r))
28863 x_clear_cursor (w);
28864 cursor_cleared_p = 1;
28866 else
28867 cursor_cleared_p = 0;
28869 /* If the row containing the cursor extends face to end of line,
28870 then expose_area might overwrite the cursor outside the
28871 rectangle and thus notice_overwritten_cursor might clear
28872 w->phys_cursor_on_p. We remember the original value and
28873 check later if it is changed. */
28874 phys_cursor_on_p = w->phys_cursor_on_p;
28876 /* Update lines intersecting rectangle R. */
28877 first_overlapping_row = last_overlapping_row = NULL;
28878 for (row = w->current_matrix->rows;
28879 row->enabled_p;
28880 ++row)
28882 int y0 = row->y;
28883 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28885 if ((y0 >= r.y && y0 < r.y + r.height)
28886 || (y1 > r.y && y1 < r.y + r.height)
28887 || (r.y >= y0 && r.y < y1)
28888 || (r.y + r.height > y0 && r.y + r.height < y1))
28890 /* A header line may be overlapping, but there is no need
28891 to fix overlapping areas for them. KFS 2005-02-12 */
28892 if (row->overlapping_p && !row->mode_line_p)
28894 if (first_overlapping_row == NULL)
28895 first_overlapping_row = row;
28896 last_overlapping_row = row;
28899 row->clip = fr;
28900 if (expose_line (w, row, &r))
28901 mouse_face_overwritten_p = 1;
28902 row->clip = NULL;
28904 else if (row->overlapping_p)
28906 /* We must redraw a row overlapping the exposed area. */
28907 if (y0 < r.y
28908 ? y0 + row->phys_height > r.y
28909 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28911 if (first_overlapping_row == NULL)
28912 first_overlapping_row = row;
28913 last_overlapping_row = row;
28917 if (y1 >= yb)
28918 break;
28921 /* Display the mode line if there is one. */
28922 if (WINDOW_WANTS_MODELINE_P (w)
28923 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28924 row->enabled_p)
28925 && row->y < r.y + r.height)
28927 if (expose_line (w, row, &r))
28928 mouse_face_overwritten_p = 1;
28931 if (!w->pseudo_window_p)
28933 /* Fix the display of overlapping rows. */
28934 if (first_overlapping_row)
28935 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28936 fr);
28938 /* Draw border between windows. */
28939 x_draw_vertical_border (w);
28941 /* Turn the cursor on again. */
28942 if (cursor_cleared_p
28943 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28944 update_window_cursor (w, 1);
28948 return mouse_face_overwritten_p;
28953 /* Redraw (parts) of all windows in the window tree rooted at W that
28954 intersect R. R contains frame pixel coordinates. Value is
28955 non-zero if the exposure overwrites mouse-face. */
28957 static int
28958 expose_window_tree (struct window *w, XRectangle *r)
28960 struct frame *f = XFRAME (w->frame);
28961 int mouse_face_overwritten_p = 0;
28963 while (w && !FRAME_GARBAGED_P (f))
28965 if (WINDOWP (w->contents))
28966 mouse_face_overwritten_p
28967 |= expose_window_tree (XWINDOW (w->contents), r);
28968 else
28969 mouse_face_overwritten_p |= expose_window (w, r);
28971 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28974 return mouse_face_overwritten_p;
28978 /* EXPORT:
28979 Redisplay an exposed area of frame F. X and Y are the upper-left
28980 corner of the exposed rectangle. W and H are width and height of
28981 the exposed area. All are pixel values. W or H zero means redraw
28982 the entire frame. */
28984 void
28985 expose_frame (struct frame *f, int x, int y, int w, int h)
28987 XRectangle r;
28988 int mouse_face_overwritten_p = 0;
28990 TRACE ((stderr, "expose_frame "));
28992 /* No need to redraw if frame will be redrawn soon. */
28993 if (FRAME_GARBAGED_P (f))
28995 TRACE ((stderr, " garbaged\n"));
28996 return;
28999 /* If basic faces haven't been realized yet, there is no point in
29000 trying to redraw anything. This can happen when we get an expose
29001 event while Emacs is starting, e.g. by moving another window. */
29002 if (FRAME_FACE_CACHE (f) == NULL
29003 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29005 TRACE ((stderr, " no faces\n"));
29006 return;
29009 if (w == 0 || h == 0)
29011 r.x = r.y = 0;
29012 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29013 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29015 else
29017 r.x = x;
29018 r.y = y;
29019 r.width = w;
29020 r.height = h;
29023 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29024 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29026 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29027 if (WINDOWP (f->tool_bar_window))
29028 mouse_face_overwritten_p
29029 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29030 #endif
29032 #ifdef HAVE_X_WINDOWS
29033 #ifndef MSDOS
29034 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29035 if (WINDOWP (f->menu_bar_window))
29036 mouse_face_overwritten_p
29037 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29038 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29039 #endif
29040 #endif
29042 /* Some window managers support a focus-follows-mouse style with
29043 delayed raising of frames. Imagine a partially obscured frame,
29044 and moving the mouse into partially obscured mouse-face on that
29045 frame. The visible part of the mouse-face will be highlighted,
29046 then the WM raises the obscured frame. With at least one WM, KDE
29047 2.1, Emacs is not getting any event for the raising of the frame
29048 (even tried with SubstructureRedirectMask), only Expose events.
29049 These expose events will draw text normally, i.e. not
29050 highlighted. Which means we must redo the highlight here.
29051 Subsume it under ``we love X''. --gerd 2001-08-15 */
29052 /* Included in Windows version because Windows most likely does not
29053 do the right thing if any third party tool offers
29054 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29055 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29057 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29058 if (f == hlinfo->mouse_face_mouse_frame)
29060 int mouse_x = hlinfo->mouse_face_mouse_x;
29061 int mouse_y = hlinfo->mouse_face_mouse_y;
29062 clear_mouse_face (hlinfo);
29063 note_mouse_highlight (f, mouse_x, mouse_y);
29069 /* EXPORT:
29070 Determine the intersection of two rectangles R1 and R2. Return
29071 the intersection in *RESULT. Value is non-zero if RESULT is not
29072 empty. */
29075 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29077 XRectangle *left, *right;
29078 XRectangle *upper, *lower;
29079 int intersection_p = 0;
29081 /* Rearrange so that R1 is the left-most rectangle. */
29082 if (r1->x < r2->x)
29083 left = r1, right = r2;
29084 else
29085 left = r2, right = r1;
29087 /* X0 of the intersection is right.x0, if this is inside R1,
29088 otherwise there is no intersection. */
29089 if (right->x <= left->x + left->width)
29091 result->x = right->x;
29093 /* The right end of the intersection is the minimum of
29094 the right ends of left and right. */
29095 result->width = (min (left->x + left->width, right->x + right->width)
29096 - result->x);
29098 /* Same game for Y. */
29099 if (r1->y < r2->y)
29100 upper = r1, lower = r2;
29101 else
29102 upper = r2, lower = r1;
29104 /* The upper end of the intersection is lower.y0, if this is inside
29105 of upper. Otherwise, there is no intersection. */
29106 if (lower->y <= upper->y + upper->height)
29108 result->y = lower->y;
29110 /* The lower end of the intersection is the minimum of the lower
29111 ends of upper and lower. */
29112 result->height = (min (lower->y + lower->height,
29113 upper->y + upper->height)
29114 - result->y);
29115 intersection_p = 1;
29119 return intersection_p;
29122 #endif /* HAVE_WINDOW_SYSTEM */
29125 /***********************************************************************
29126 Initialization
29127 ***********************************************************************/
29129 void
29130 syms_of_xdisp (void)
29132 Vwith_echo_area_save_vector = Qnil;
29133 staticpro (&Vwith_echo_area_save_vector);
29135 Vmessage_stack = Qnil;
29136 staticpro (&Vmessage_stack);
29138 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29139 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29141 message_dolog_marker1 = Fmake_marker ();
29142 staticpro (&message_dolog_marker1);
29143 message_dolog_marker2 = Fmake_marker ();
29144 staticpro (&message_dolog_marker2);
29145 message_dolog_marker3 = Fmake_marker ();
29146 staticpro (&message_dolog_marker3);
29148 #ifdef GLYPH_DEBUG
29149 defsubr (&Sdump_frame_glyph_matrix);
29150 defsubr (&Sdump_glyph_matrix);
29151 defsubr (&Sdump_glyph_row);
29152 defsubr (&Sdump_tool_bar_row);
29153 defsubr (&Strace_redisplay);
29154 defsubr (&Strace_to_stderr);
29155 #endif
29156 #ifdef HAVE_WINDOW_SYSTEM
29157 defsubr (&Stool_bar_lines_needed);
29158 defsubr (&Slookup_image_map);
29159 #endif
29160 defsubr (&Sline_pixel_height);
29161 defsubr (&Sformat_mode_line);
29162 defsubr (&Sinvisible_p);
29163 defsubr (&Scurrent_bidi_paragraph_direction);
29164 defsubr (&Smove_point_visually);
29166 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29167 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29168 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29169 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29170 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29171 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29172 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29173 DEFSYM (Qeval, "eval");
29174 DEFSYM (QCdata, ":data");
29175 DEFSYM (Qdisplay, "display");
29176 DEFSYM (Qspace_width, "space-width");
29177 DEFSYM (Qraise, "raise");
29178 DEFSYM (Qslice, "slice");
29179 DEFSYM (Qspace, "space");
29180 DEFSYM (Qmargin, "margin");
29181 DEFSYM (Qpointer, "pointer");
29182 DEFSYM (Qleft_margin, "left-margin");
29183 DEFSYM (Qright_margin, "right-margin");
29184 DEFSYM (Qcenter, "center");
29185 DEFSYM (Qline_height, "line-height");
29186 DEFSYM (QCalign_to, ":align-to");
29187 DEFSYM (QCrelative_width, ":relative-width");
29188 DEFSYM (QCrelative_height, ":relative-height");
29189 DEFSYM (QCeval, ":eval");
29190 DEFSYM (QCpropertize, ":propertize");
29191 DEFSYM (QCfile, ":file");
29192 DEFSYM (Qfontified, "fontified");
29193 DEFSYM (Qfontification_functions, "fontification-functions");
29194 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29195 DEFSYM (Qescape_glyph, "escape-glyph");
29196 DEFSYM (Qnobreak_space, "nobreak-space");
29197 DEFSYM (Qimage, "image");
29198 DEFSYM (Qtext, "text");
29199 DEFSYM (Qboth, "both");
29200 DEFSYM (Qboth_horiz, "both-horiz");
29201 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29202 DEFSYM (QCmap, ":map");
29203 DEFSYM (QCpointer, ":pointer");
29204 DEFSYM (Qrect, "rect");
29205 DEFSYM (Qcircle, "circle");
29206 DEFSYM (Qpoly, "poly");
29207 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29208 DEFSYM (Qgrow_only, "grow-only");
29209 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29210 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29211 DEFSYM (Qposition, "position");
29212 DEFSYM (Qbuffer_position, "buffer-position");
29213 DEFSYM (Qobject, "object");
29214 DEFSYM (Qbar, "bar");
29215 DEFSYM (Qhbar, "hbar");
29216 DEFSYM (Qbox, "box");
29217 DEFSYM (Qhollow, "hollow");
29218 DEFSYM (Qhand, "hand");
29219 DEFSYM (Qarrow, "arrow");
29220 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29222 list_of_error = list1 (list2 (intern_c_string ("error"),
29223 intern_c_string ("void-variable")));
29224 staticpro (&list_of_error);
29226 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29227 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29228 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29229 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29231 echo_buffer[0] = echo_buffer[1] = Qnil;
29232 staticpro (&echo_buffer[0]);
29233 staticpro (&echo_buffer[1]);
29235 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29236 staticpro (&echo_area_buffer[0]);
29237 staticpro (&echo_area_buffer[1]);
29239 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29240 staticpro (&Vmessages_buffer_name);
29242 mode_line_proptrans_alist = Qnil;
29243 staticpro (&mode_line_proptrans_alist);
29244 mode_line_string_list = Qnil;
29245 staticpro (&mode_line_string_list);
29246 mode_line_string_face = Qnil;
29247 staticpro (&mode_line_string_face);
29248 mode_line_string_face_prop = Qnil;
29249 staticpro (&mode_line_string_face_prop);
29250 Vmode_line_unwind_vector = Qnil;
29251 staticpro (&Vmode_line_unwind_vector);
29253 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29255 help_echo_string = Qnil;
29256 staticpro (&help_echo_string);
29257 help_echo_object = Qnil;
29258 staticpro (&help_echo_object);
29259 help_echo_window = Qnil;
29260 staticpro (&help_echo_window);
29261 previous_help_echo_string = Qnil;
29262 staticpro (&previous_help_echo_string);
29263 help_echo_pos = -1;
29265 DEFSYM (Qright_to_left, "right-to-left");
29266 DEFSYM (Qleft_to_right, "left-to-right");
29268 #ifdef HAVE_WINDOW_SYSTEM
29269 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29270 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29271 For example, if a block cursor is over a tab, it will be drawn as
29272 wide as that tab on the display. */);
29273 x_stretch_cursor_p = 0;
29274 #endif
29276 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29277 doc: /* Non-nil means highlight trailing whitespace.
29278 The face used for trailing whitespace is `trailing-whitespace'. */);
29279 Vshow_trailing_whitespace = Qnil;
29281 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29282 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29283 If the value is t, Emacs highlights non-ASCII chars which have the
29284 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29285 or `escape-glyph' face respectively.
29287 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29288 U+2011 (non-breaking hyphen) are affected.
29290 Any other non-nil value means to display these characters as a escape
29291 glyph followed by an ordinary space or hyphen.
29293 A value of nil means no special handling of these characters. */);
29294 Vnobreak_char_display = Qt;
29296 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29297 doc: /* The pointer shape to show in void text areas.
29298 A value of nil means to show the text pointer. Other options are `arrow',
29299 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29300 Vvoid_text_area_pointer = Qarrow;
29302 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29303 doc: /* Non-nil means don't actually do any redisplay.
29304 This is used for internal purposes. */);
29305 Vinhibit_redisplay = Qnil;
29307 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29308 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29309 Vglobal_mode_string = Qnil;
29311 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29312 doc: /* Marker for where to display an arrow on top of the buffer text.
29313 This must be the beginning of a line in order to work.
29314 See also `overlay-arrow-string'. */);
29315 Voverlay_arrow_position = Qnil;
29317 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29318 doc: /* String to display as an arrow in non-window frames.
29319 See also `overlay-arrow-position'. */);
29320 Voverlay_arrow_string = build_pure_c_string ("=>");
29322 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29323 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29324 The symbols on this list are examined during redisplay to determine
29325 where to display overlay arrows. */);
29326 Voverlay_arrow_variable_list
29327 = list1 (intern_c_string ("overlay-arrow-position"));
29329 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29330 doc: /* The number of lines to try scrolling a window by when point moves out.
29331 If that fails to bring point back on frame, point is centered instead.
29332 If this is zero, point is always centered after it moves off frame.
29333 If you want scrolling to always be a line at a time, you should set
29334 `scroll-conservatively' to a large value rather than set this to 1. */);
29336 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29337 doc: /* Scroll up to this many lines, to bring point back on screen.
29338 If point moves off-screen, redisplay will scroll by up to
29339 `scroll-conservatively' lines in order to bring point just barely
29340 onto the screen again. If that cannot be done, then redisplay
29341 recenters point as usual.
29343 If the value is greater than 100, redisplay will never recenter point,
29344 but will always scroll just enough text to bring point into view, even
29345 if you move far away.
29347 A value of zero means always recenter point if it moves off screen. */);
29348 scroll_conservatively = 0;
29350 DEFVAR_INT ("scroll-margin", scroll_margin,
29351 doc: /* Number of lines of margin at the top and bottom of a window.
29352 Recenter the window whenever point gets within this many lines
29353 of the top or bottom of the window. */);
29354 scroll_margin = 0;
29356 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29357 doc: /* Pixels per inch value for non-window system displays.
29358 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29359 Vdisplay_pixels_per_inch = make_float (72.0);
29361 #ifdef GLYPH_DEBUG
29362 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29363 #endif
29365 DEFVAR_LISP ("truncate-partial-width-windows",
29366 Vtruncate_partial_width_windows,
29367 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29368 For an integer value, truncate lines in each window narrower than the
29369 full frame width, provided the window width is less than that integer;
29370 otherwise, respect the value of `truncate-lines'.
29372 For any other non-nil value, truncate lines in all windows that do
29373 not span the full frame width.
29375 A value of nil means to respect the value of `truncate-lines'.
29377 If `word-wrap' is enabled, you might want to reduce this. */);
29378 Vtruncate_partial_width_windows = make_number (50);
29380 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29381 doc: /* Maximum buffer size for which line number should be displayed.
29382 If the buffer is bigger than this, the line number does not appear
29383 in the mode line. A value of nil means no limit. */);
29384 Vline_number_display_limit = Qnil;
29386 DEFVAR_INT ("line-number-display-limit-width",
29387 line_number_display_limit_width,
29388 doc: /* Maximum line width (in characters) for line number display.
29389 If the average length of the lines near point is bigger than this, then the
29390 line number may be omitted from the mode line. */);
29391 line_number_display_limit_width = 200;
29393 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29394 doc: /* Non-nil means highlight region even in nonselected windows. */);
29395 highlight_nonselected_windows = 0;
29397 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29398 doc: /* Non-nil if more than one frame is visible on this display.
29399 Minibuffer-only frames don't count, but iconified frames do.
29400 This variable is not guaranteed to be accurate except while processing
29401 `frame-title-format' and `icon-title-format'. */);
29403 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29404 doc: /* Template for displaying the title bar of visible frames.
29405 \(Assuming the window manager supports this feature.)
29407 This variable has the same structure as `mode-line-format', except that
29408 the %c and %l constructs are ignored. It is used only on frames for
29409 which no explicit name has been set \(see `modify-frame-parameters'). */);
29411 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29412 doc: /* Template for displaying the title bar of an iconified frame.
29413 \(Assuming the window manager supports this feature.)
29414 This variable has the same structure as `mode-line-format' (which see),
29415 and is used only on frames for which no explicit name has been set
29416 \(see `modify-frame-parameters'). */);
29417 Vicon_title_format
29418 = Vframe_title_format
29419 = listn (CONSTYPE_PURE, 3,
29420 intern_c_string ("multiple-frames"),
29421 build_pure_c_string ("%b"),
29422 listn (CONSTYPE_PURE, 4,
29423 empty_unibyte_string,
29424 intern_c_string ("invocation-name"),
29425 build_pure_c_string ("@"),
29426 intern_c_string ("system-name")));
29428 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29429 doc: /* Maximum number of lines to keep in the message log buffer.
29430 If nil, disable message logging. If t, log messages but don't truncate
29431 the buffer when it becomes large. */);
29432 Vmessage_log_max = make_number (1000);
29434 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29435 doc: /* Functions called before redisplay, if window sizes have changed.
29436 The value should be a list of functions that take one argument.
29437 Just before redisplay, for each frame, if any of its windows have changed
29438 size since the last redisplay, or have been split or deleted,
29439 all the functions in the list are called, with the frame as argument. */);
29440 Vwindow_size_change_functions = Qnil;
29442 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29443 doc: /* List of functions to call before redisplaying a window with scrolling.
29444 Each function is called with two arguments, the window and its new
29445 display-start position. Note that these functions are also called by
29446 `set-window-buffer'. Also note that the value of `window-end' is not
29447 valid when these functions are called.
29449 Warning: Do not use this feature to alter the way the window
29450 is scrolled. It is not designed for that, and such use probably won't
29451 work. */);
29452 Vwindow_scroll_functions = Qnil;
29454 DEFVAR_LISP ("window-text-change-functions",
29455 Vwindow_text_change_functions,
29456 doc: /* Functions to call in redisplay when text in the window might change. */);
29457 Vwindow_text_change_functions = Qnil;
29459 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29460 doc: /* Functions called when redisplay of a window reaches the end trigger.
29461 Each function is called with two arguments, the window and the end trigger value.
29462 See `set-window-redisplay-end-trigger'. */);
29463 Vredisplay_end_trigger_functions = Qnil;
29465 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29466 doc: /* Non-nil means autoselect window with mouse pointer.
29467 If nil, do not autoselect windows.
29468 A positive number means delay autoselection by that many seconds: a
29469 window is autoselected only after the mouse has remained in that
29470 window for the duration of the delay.
29471 A negative number has a similar effect, but causes windows to be
29472 autoselected only after the mouse has stopped moving. \(Because of
29473 the way Emacs compares mouse events, you will occasionally wait twice
29474 that time before the window gets selected.\)
29475 Any other value means to autoselect window instantaneously when the
29476 mouse pointer enters it.
29478 Autoselection selects the minibuffer only if it is active, and never
29479 unselects the minibuffer if it is active.
29481 When customizing this variable make sure that the actual value of
29482 `focus-follows-mouse' matches the behavior of your window manager. */);
29483 Vmouse_autoselect_window = Qnil;
29485 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29486 doc: /* Non-nil means automatically resize tool-bars.
29487 This dynamically changes the tool-bar's height to the minimum height
29488 that is needed to make all tool-bar items visible.
29489 If value is `grow-only', the tool-bar's height is only increased
29490 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29491 Vauto_resize_tool_bars = Qt;
29493 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29494 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29495 auto_raise_tool_bar_buttons_p = 1;
29497 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29498 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29499 make_cursor_line_fully_visible_p = 1;
29501 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29502 doc: /* Border below tool-bar in pixels.
29503 If an integer, use it as the height of the border.
29504 If it is one of `internal-border-width' or `border-width', use the
29505 value of the corresponding frame parameter.
29506 Otherwise, no border is added below the tool-bar. */);
29507 Vtool_bar_border = Qinternal_border_width;
29509 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29510 doc: /* Margin around tool-bar buttons in pixels.
29511 If an integer, use that for both horizontal and vertical margins.
29512 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29513 HORZ specifying the horizontal margin, and VERT specifying the
29514 vertical margin. */);
29515 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29517 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29518 doc: /* Relief thickness of tool-bar buttons. */);
29519 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29521 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29522 doc: /* Tool bar style to use.
29523 It can be one of
29524 image - show images only
29525 text - show text only
29526 both - show both, text below image
29527 both-horiz - show text to the right of the image
29528 text-image-horiz - show text to the left of the image
29529 any other - use system default or image if no system default.
29531 This variable only affects the GTK+ toolkit version of Emacs. */);
29532 Vtool_bar_style = Qnil;
29534 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29535 doc: /* Maximum number of characters a label can have to be shown.
29536 The tool bar style must also show labels for this to have any effect, see
29537 `tool-bar-style'. */);
29538 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29540 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29541 doc: /* List of functions to call to fontify regions of text.
29542 Each function is called with one argument POS. Functions must
29543 fontify a region starting at POS in the current buffer, and give
29544 fontified regions the property `fontified'. */);
29545 Vfontification_functions = Qnil;
29546 Fmake_variable_buffer_local (Qfontification_functions);
29548 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29549 unibyte_display_via_language_environment,
29550 doc: /* Non-nil means display unibyte text according to language environment.
29551 Specifically, this means that raw bytes in the range 160-255 decimal
29552 are displayed by converting them to the equivalent multibyte characters
29553 according to the current language environment. As a result, they are
29554 displayed according to the current fontset.
29556 Note that this variable affects only how these bytes are displayed,
29557 but does not change the fact they are interpreted as raw bytes. */);
29558 unibyte_display_via_language_environment = 0;
29560 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29561 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29562 If a float, it specifies a fraction of the mini-window frame's height.
29563 If an integer, it specifies a number of lines. */);
29564 Vmax_mini_window_height = make_float (0.25);
29566 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29567 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29568 A value of nil means don't automatically resize mini-windows.
29569 A value of t means resize them to fit the text displayed in them.
29570 A value of `grow-only', the default, means let mini-windows grow only;
29571 they return to their normal size when the minibuffer is closed, or the
29572 echo area becomes empty. */);
29573 Vresize_mini_windows = Qgrow_only;
29575 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29576 doc: /* Alist specifying how to blink the cursor off.
29577 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29578 `cursor-type' frame-parameter or variable equals ON-STATE,
29579 comparing using `equal', Emacs uses OFF-STATE to specify
29580 how to blink it off. ON-STATE and OFF-STATE are values for
29581 the `cursor-type' frame parameter.
29583 If a frame's ON-STATE has no entry in this list,
29584 the frame's other specifications determine how to blink the cursor off. */);
29585 Vblink_cursor_alist = Qnil;
29587 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29588 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29589 If non-nil, windows are automatically scrolled horizontally to make
29590 point visible. */);
29591 automatic_hscrolling_p = 1;
29592 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29594 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29595 doc: /* How many columns away from the window edge point is allowed to get
29596 before automatic hscrolling will horizontally scroll the window. */);
29597 hscroll_margin = 5;
29599 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29600 doc: /* How many columns to scroll the window when point gets too close to the edge.
29601 When point is less than `hscroll-margin' columns from the window
29602 edge, automatic hscrolling will scroll the window by the amount of columns
29603 determined by this variable. If its value is a positive integer, scroll that
29604 many columns. If it's a positive floating-point number, it specifies the
29605 fraction of the window's width to scroll. If it's nil or zero, point will be
29606 centered horizontally after the scroll. Any other value, including negative
29607 numbers, are treated as if the value were zero.
29609 Automatic hscrolling always moves point outside the scroll margin, so if
29610 point was more than scroll step columns inside the margin, the window will
29611 scroll more than the value given by the scroll step.
29613 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29614 and `scroll-right' overrides this variable's effect. */);
29615 Vhscroll_step = make_number (0);
29617 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29618 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29619 Bind this around calls to `message' to let it take effect. */);
29620 message_truncate_lines = 0;
29622 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29623 doc: /* Normal hook run to update the menu bar definitions.
29624 Redisplay runs this hook before it redisplays the menu bar.
29625 This is used to update submenus such as Buffers,
29626 whose contents depend on various data. */);
29627 Vmenu_bar_update_hook = Qnil;
29629 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29630 doc: /* Frame for which we are updating a menu.
29631 The enable predicate for a menu binding should check this variable. */);
29632 Vmenu_updating_frame = Qnil;
29634 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29635 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29636 inhibit_menubar_update = 0;
29638 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29639 doc: /* Prefix prepended to all continuation lines at display time.
29640 The value may be a string, an image, or a stretch-glyph; it is
29641 interpreted in the same way as the value of a `display' text property.
29643 This variable is overridden by any `wrap-prefix' text or overlay
29644 property.
29646 To add a prefix to non-continuation lines, use `line-prefix'. */);
29647 Vwrap_prefix = Qnil;
29648 DEFSYM (Qwrap_prefix, "wrap-prefix");
29649 Fmake_variable_buffer_local (Qwrap_prefix);
29651 DEFVAR_LISP ("line-prefix", Vline_prefix,
29652 doc: /* Prefix prepended to all non-continuation lines at display time.
29653 The value may be a string, an image, or a stretch-glyph; it is
29654 interpreted in the same way as the value of a `display' text property.
29656 This variable is overridden by any `line-prefix' text or overlay
29657 property.
29659 To add a prefix to continuation lines, use `wrap-prefix'. */);
29660 Vline_prefix = Qnil;
29661 DEFSYM (Qline_prefix, "line-prefix");
29662 Fmake_variable_buffer_local (Qline_prefix);
29664 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29665 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29666 inhibit_eval_during_redisplay = 0;
29668 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29669 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29670 inhibit_free_realized_faces = 0;
29672 #ifdef GLYPH_DEBUG
29673 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29674 doc: /* Inhibit try_window_id display optimization. */);
29675 inhibit_try_window_id = 0;
29677 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29678 doc: /* Inhibit try_window_reusing display optimization. */);
29679 inhibit_try_window_reusing = 0;
29681 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29682 doc: /* Inhibit try_cursor_movement display optimization. */);
29683 inhibit_try_cursor_movement = 0;
29684 #endif /* GLYPH_DEBUG */
29686 DEFVAR_INT ("overline-margin", overline_margin,
29687 doc: /* Space between overline and text, in pixels.
29688 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29689 margin to the character height. */);
29690 overline_margin = 2;
29692 DEFVAR_INT ("underline-minimum-offset",
29693 underline_minimum_offset,
29694 doc: /* Minimum distance between baseline and underline.
29695 This can improve legibility of underlined text at small font sizes,
29696 particularly when using variable `x-use-underline-position-properties'
29697 with fonts that specify an UNDERLINE_POSITION relatively close to the
29698 baseline. The default value is 1. */);
29699 underline_minimum_offset = 1;
29701 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29702 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29703 This feature only works when on a window system that can change
29704 cursor shapes. */);
29705 display_hourglass_p = 1;
29707 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29708 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29709 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29711 #ifdef HAVE_WINDOW_SYSTEM
29712 hourglass_atimer = NULL;
29713 hourglass_shown_p = 0;
29714 #endif /* HAVE_WINDOW_SYSTEM */
29716 DEFSYM (Qglyphless_char, "glyphless-char");
29717 DEFSYM (Qhex_code, "hex-code");
29718 DEFSYM (Qempty_box, "empty-box");
29719 DEFSYM (Qthin_space, "thin-space");
29720 DEFSYM (Qzero_width, "zero-width");
29722 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
29723 doc: /* Function run just before redisplay.
29724 It is called with one argument, which is the set of windows that are to
29725 be redisplayed. This set can be nil (meaning, only the selected window),
29726 or t (meaning all windows). */);
29727 Vpre_redisplay_function = intern ("ignore");
29729 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29730 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29732 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29733 doc: /* Char-table defining glyphless characters.
29734 Each element, if non-nil, should be one of the following:
29735 an ASCII acronym string: display this string in a box
29736 `hex-code': display the hexadecimal code of a character in a box
29737 `empty-box': display as an empty box
29738 `thin-space': display as 1-pixel width space
29739 `zero-width': don't display
29740 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29741 display method for graphical terminals and text terminals respectively.
29742 GRAPHICAL and TEXT should each have one of the values listed above.
29744 The char-table has one extra slot to control the display of a character for
29745 which no font is found. This slot only takes effect on graphical terminals.
29746 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29747 `thin-space'. The default is `empty-box'.
29749 If a character has a non-nil entry in an active display table, the
29750 display table takes effect; in this case, Emacs does not consult
29751 `glyphless-char-display' at all. */);
29752 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29753 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29754 Qempty_box);
29756 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29757 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29758 Vdebug_on_message = Qnil;
29760 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
29761 doc: /* */);
29762 Vredisplay__all_windows_cause
29763 = Fmake_vector (make_number (100), make_number (0));
29765 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
29766 doc: /* */);
29767 Vredisplay__mode_lines_cause
29768 = Fmake_vector (make_number (100), make_number (0));
29772 /* Initialize this module when Emacs starts. */
29774 void
29775 init_xdisp (void)
29777 CHARPOS (this_line_start_pos) = 0;
29779 if (!noninteractive)
29781 struct window *m = XWINDOW (minibuf_window);
29782 Lisp_Object frame = m->frame;
29783 struct frame *f = XFRAME (frame);
29784 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29785 struct window *r = XWINDOW (root);
29786 int i;
29788 echo_area_window = minibuf_window;
29790 r->top_line = FRAME_TOP_MARGIN (f);
29791 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29792 r->total_cols = FRAME_COLS (f);
29794 m->top_line = FRAME_LINES (f) - 1;
29795 m->total_lines = 1;
29796 m->total_cols = FRAME_COLS (f);
29798 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29799 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29800 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29802 /* The default ellipsis glyphs `...'. */
29803 for (i = 0; i < 3; ++i)
29804 default_invis_vector[i] = make_number ('.');
29808 /* Allocate the buffer for frame titles.
29809 Also used for `format-mode-line'. */
29810 int size = 100;
29811 mode_line_noprop_buf = xmalloc (size);
29812 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29813 mode_line_noprop_ptr = mode_line_noprop_buf;
29814 mode_line_target = MODE_LINE_DISPLAY;
29817 help_echo_showing_p = 0;
29820 #ifdef HAVE_WINDOW_SYSTEM
29822 /* Platform-independent portion of hourglass implementation. */
29824 /* Cancel a currently active hourglass timer, and start a new one. */
29825 void
29826 start_hourglass (void)
29828 struct timespec delay;
29830 cancel_hourglass ();
29832 if (INTEGERP (Vhourglass_delay)
29833 && XINT (Vhourglass_delay) > 0)
29834 delay = make_timespec (min (XINT (Vhourglass_delay),
29835 TYPE_MAXIMUM (time_t)),
29837 else if (FLOATP (Vhourglass_delay)
29838 && XFLOAT_DATA (Vhourglass_delay) > 0)
29839 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29840 else
29841 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29843 #ifdef HAVE_NTGUI
29845 extern void w32_note_current_window (void);
29846 w32_note_current_window ();
29848 #endif /* HAVE_NTGUI */
29850 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29851 show_hourglass, NULL);
29855 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29856 shown. */
29857 void
29858 cancel_hourglass (void)
29860 if (hourglass_atimer)
29862 cancel_atimer (hourglass_atimer);
29863 hourglass_atimer = NULL;
29866 if (hourglass_shown_p)
29867 hide_hourglass ();
29870 #endif /* HAVE_WINDOW_SYSTEM */