* mouse.el (mouse-yank-primary): Reorder to silence --without-x compilation.
[emacs.git] / src / xdisp.c
blobb07aad51bc9886f41cbcb5ff03e05d8c0bdf37b4
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 int noninteractive_need_newline;
420 /* Non-zero means print newline to message log before next message. */
422 static int 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 int 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 int 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 int 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 int display_last_displayed_message_p;
541 /* Nonzero if echo area is being used by print; zero if being used by
542 message. */
544 static int 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 int 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 int 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 int 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 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
1886 struct glyph *
1887 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1888 int *dx, int *dy, int *area)
1890 struct glyph *glyph, *end;
1891 struct glyph_row *row = NULL;
1892 int x0, i;
1894 /* Find row containing Y. Give up if some row is not enabled. */
1895 for (i = 0; i < w->current_matrix->nrows; ++i)
1897 row = MATRIX_ROW (w->current_matrix, i);
1898 if (!row->enabled_p)
1899 return NULL;
1900 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1901 break;
1904 *vpos = i;
1905 *hpos = 0;
1907 /* Give up if Y is not in the window. */
1908 if (i == w->current_matrix->nrows)
1909 return NULL;
1911 /* Get the glyph area containing X. */
1912 if (w->pseudo_window_p)
1914 *area = TEXT_AREA;
1915 x0 = 0;
1917 else
1919 if (x < window_box_left_offset (w, TEXT_AREA))
1921 *area = LEFT_MARGIN_AREA;
1922 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1924 else if (x < window_box_right_offset (w, TEXT_AREA))
1926 *area = TEXT_AREA;
1927 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1929 else
1931 *area = RIGHT_MARGIN_AREA;
1932 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1936 /* Find glyph containing X. */
1937 glyph = row->glyphs[*area];
1938 end = glyph + row->used[*area];
1939 x -= x0;
1940 while (glyph < end && x >= glyph->pixel_width)
1942 x -= glyph->pixel_width;
1943 ++glyph;
1946 if (glyph == end)
1947 return NULL;
1949 if (dx)
1951 *dx = x;
1952 *dy = y - (row->y + row->ascent - glyph->ascent);
1955 *hpos = glyph - row->glyphs[*area];
1956 return glyph;
1959 /* Convert frame-relative x/y to coordinates relative to window W.
1960 Takes pseudo-windows into account. */
1962 static void
1963 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1965 if (w->pseudo_window_p)
1967 /* A pseudo-window is always full-width, and starts at the
1968 left edge of the frame, plus a frame border. */
1969 struct frame *f = XFRAME (w->frame);
1970 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1971 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1973 else
1975 *x -= WINDOW_LEFT_EDGE_X (w);
1976 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1980 #ifdef HAVE_WINDOW_SYSTEM
1982 /* EXPORT:
1983 Return in RECTS[] at most N clipping rectangles for glyph string S.
1984 Return the number of stored rectangles. */
1987 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1989 XRectangle r;
1991 if (n <= 0)
1992 return 0;
1994 if (s->row->full_width_p)
1996 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1997 r.x = WINDOW_LEFT_EDGE_X (s->w);
1998 r.width = WINDOW_TOTAL_WIDTH (s->w);
2000 /* Unless displaying a mode or menu bar line, which are always
2001 fully visible, clip to the visible part of the row. */
2002 if (s->w->pseudo_window_p)
2003 r.height = s->row->visible_height;
2004 else
2005 r.height = s->height;
2007 else
2009 /* This is a text line that may be partially visible. */
2010 r.x = window_box_left (s->w, s->area);
2011 r.width = window_box_width (s->w, s->area);
2012 r.height = s->row->visible_height;
2015 if (s->clip_head)
2016 if (r.x < s->clip_head->x)
2018 if (r.width >= s->clip_head->x - r.x)
2019 r.width -= s->clip_head->x - r.x;
2020 else
2021 r.width = 0;
2022 r.x = s->clip_head->x;
2024 if (s->clip_tail)
2025 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2027 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2028 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2029 else
2030 r.width = 0;
2033 /* If S draws overlapping rows, it's sufficient to use the top and
2034 bottom of the window for clipping because this glyph string
2035 intentionally draws over other lines. */
2036 if (s->for_overlaps)
2038 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2039 r.height = window_text_bottom_y (s->w) - r.y;
2041 /* Alas, the above simple strategy does not work for the
2042 environments with anti-aliased text: if the same text is
2043 drawn onto the same place multiple times, it gets thicker.
2044 If the overlap we are processing is for the erased cursor, we
2045 take the intersection with the rectangle of the cursor. */
2046 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2048 XRectangle rc, r_save = r;
2050 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2051 rc.y = s->w->phys_cursor.y;
2052 rc.width = s->w->phys_cursor_width;
2053 rc.height = s->w->phys_cursor_height;
2055 x_intersect_rectangles (&r_save, &rc, &r);
2058 else
2060 /* Don't use S->y for clipping because it doesn't take partially
2061 visible lines into account. For example, it can be negative for
2062 partially visible lines at the top of a window. */
2063 if (!s->row->full_width_p
2064 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2065 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2066 else
2067 r.y = max (0, s->row->y);
2070 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2072 /* If drawing the cursor, don't let glyph draw outside its
2073 advertised boundaries. Cleartype does this under some circumstances. */
2074 if (s->hl == DRAW_CURSOR)
2076 struct glyph *glyph = s->first_glyph;
2077 int height, max_y;
2079 if (s->x > r.x)
2081 r.width -= s->x - r.x;
2082 r.x = s->x;
2084 r.width = min (r.width, glyph->pixel_width);
2086 /* If r.y is below window bottom, ensure that we still see a cursor. */
2087 height = min (glyph->ascent + glyph->descent,
2088 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2089 max_y = window_text_bottom_y (s->w) - height;
2090 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2091 if (s->ybase - glyph->ascent > max_y)
2093 r.y = max_y;
2094 r.height = height;
2096 else
2098 /* Don't draw cursor glyph taller than our actual glyph. */
2099 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2100 if (height < r.height)
2102 max_y = r.y + r.height;
2103 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2104 r.height = min (max_y - r.y, height);
2109 if (s->row->clip)
2111 XRectangle r_save = r;
2113 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2114 r.width = 0;
2117 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2118 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2120 #ifdef CONVERT_FROM_XRECT
2121 CONVERT_FROM_XRECT (r, *rects);
2122 #else
2123 *rects = r;
2124 #endif
2125 return 1;
2127 else
2129 /* If we are processing overlapping and allowed to return
2130 multiple clipping rectangles, we exclude the row of the glyph
2131 string from the clipping rectangle. This is to avoid drawing
2132 the same text on the environment with anti-aliasing. */
2133 #ifdef CONVERT_FROM_XRECT
2134 XRectangle rs[2];
2135 #else
2136 XRectangle *rs = rects;
2137 #endif
2138 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2140 if (s->for_overlaps & OVERLAPS_PRED)
2142 rs[i] = r;
2143 if (r.y + r.height > row_y)
2145 if (r.y < row_y)
2146 rs[i].height = row_y - r.y;
2147 else
2148 rs[i].height = 0;
2150 i++;
2152 if (s->for_overlaps & OVERLAPS_SUCC)
2154 rs[i] = r;
2155 if (r.y < row_y + s->row->visible_height)
2157 if (r.y + r.height > row_y + s->row->visible_height)
2159 rs[i].y = row_y + s->row->visible_height;
2160 rs[i].height = r.y + r.height - rs[i].y;
2162 else
2163 rs[i].height = 0;
2165 i++;
2168 n = i;
2169 #ifdef CONVERT_FROM_XRECT
2170 for (i = 0; i < n; i++)
2171 CONVERT_FROM_XRECT (rs[i], rects[i]);
2172 #endif
2173 return n;
2177 /* EXPORT:
2178 Return in *NR the clipping rectangle for glyph string S. */
2180 void
2181 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2183 get_glyph_string_clip_rects (s, nr, 1);
2187 /* EXPORT:
2188 Return the position and height of the phys cursor in window W.
2189 Set w->phys_cursor_width to width of phys cursor.
2192 void
2193 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2194 struct glyph *glyph, int *xp, int *yp, int *heightp)
2196 struct frame *f = XFRAME (WINDOW_FRAME (w));
2197 int x, y, wd, h, h0, y0;
2199 /* Compute the width of the rectangle to draw. If on a stretch
2200 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2201 rectangle as wide as the glyph, but use a canonical character
2202 width instead. */
2203 wd = glyph->pixel_width - 1;
2204 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2205 wd++; /* Why? */
2206 #endif
2208 x = w->phys_cursor.x;
2209 if (x < 0)
2211 wd += x;
2212 x = 0;
2215 if (glyph->type == STRETCH_GLYPH
2216 && !x_stretch_cursor_p)
2217 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2218 w->phys_cursor_width = wd;
2220 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2222 /* If y is below window bottom, ensure that we still see a cursor. */
2223 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2225 h = max (h0, glyph->ascent + glyph->descent);
2226 h0 = min (h0, glyph->ascent + glyph->descent);
2228 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2229 if (y < y0)
2231 h = max (h - (y0 - y) + 1, h0);
2232 y = y0 - 1;
2234 else
2236 y0 = window_text_bottom_y (w) - h0;
2237 if (y > y0)
2239 h += y - y0;
2240 y = y0;
2244 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2245 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2246 *heightp = h;
2250 * Remember which glyph the mouse is over.
2253 void
2254 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2256 Lisp_Object window;
2257 struct window *w;
2258 struct glyph_row *r, *gr, *end_row;
2259 enum window_part part;
2260 enum glyph_row_area area;
2261 int x, y, width, height;
2263 /* Try to determine frame pixel position and size of the glyph under
2264 frame pixel coordinates X/Y on frame F. */
2266 if (!f->glyphs_initialized_p
2267 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2268 NILP (window)))
2270 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2271 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2272 goto virtual_glyph;
2275 w = XWINDOW (window);
2276 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2277 height = WINDOW_FRAME_LINE_HEIGHT (w);
2279 x = window_relative_x_coord (w, part, gx);
2280 y = gy - WINDOW_TOP_EDGE_Y (w);
2282 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2283 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2285 if (w->pseudo_window_p)
2287 area = TEXT_AREA;
2288 part = ON_MODE_LINE; /* Don't adjust margin. */
2289 goto text_glyph;
2292 switch (part)
2294 case ON_LEFT_MARGIN:
2295 area = LEFT_MARGIN_AREA;
2296 goto text_glyph;
2298 case ON_RIGHT_MARGIN:
2299 area = RIGHT_MARGIN_AREA;
2300 goto text_glyph;
2302 case ON_HEADER_LINE:
2303 case ON_MODE_LINE:
2304 gr = (part == ON_HEADER_LINE
2305 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2306 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2307 gy = gr->y;
2308 area = TEXT_AREA;
2309 goto text_glyph_row_found;
2311 case ON_TEXT:
2312 area = TEXT_AREA;
2314 text_glyph:
2315 gr = 0; gy = 0;
2316 for (; r <= end_row && r->enabled_p; ++r)
2317 if (r->y + r->height > y)
2319 gr = r; gy = r->y;
2320 break;
2323 text_glyph_row_found:
2324 if (gr && gy <= y)
2326 struct glyph *g = gr->glyphs[area];
2327 struct glyph *end = g + gr->used[area];
2329 height = gr->height;
2330 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2331 if (gx + g->pixel_width > x)
2332 break;
2334 if (g < end)
2336 if (g->type == IMAGE_GLYPH)
2338 /* Don't remember when mouse is over image, as
2339 image may have hot-spots. */
2340 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2341 return;
2343 width = g->pixel_width;
2345 else
2347 /* Use nominal char spacing at end of line. */
2348 x -= gx;
2349 gx += (x / width) * width;
2352 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2353 gx += window_box_left_offset (w, area);
2355 else
2357 /* Use nominal line height at end of window. */
2358 gx = (x / width) * width;
2359 y -= gy;
2360 gy += (y / height) * height;
2362 break;
2364 case ON_LEFT_FRINGE:
2365 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2366 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2367 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2368 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2369 goto row_glyph;
2371 case ON_RIGHT_FRINGE:
2372 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2373 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2374 : window_box_right_offset (w, TEXT_AREA));
2375 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2376 goto row_glyph;
2378 case ON_SCROLL_BAR:
2379 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2381 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2382 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2383 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2384 : 0)));
2385 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2387 row_glyph:
2388 gr = 0, gy = 0;
2389 for (; r <= end_row && r->enabled_p; ++r)
2390 if (r->y + r->height > y)
2392 gr = r; gy = r->y;
2393 break;
2396 if (gr && gy <= y)
2397 height = gr->height;
2398 else
2400 /* Use nominal line height at end of window. */
2401 y -= gy;
2402 gy += (y / height) * height;
2404 break;
2406 default:
2408 virtual_glyph:
2409 /* If there is no glyph under the mouse, then we divide the screen
2410 into a grid of the smallest glyph in the frame, and use that
2411 as our "glyph". */
2413 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2414 round down even for negative values. */
2415 if (gx < 0)
2416 gx -= width - 1;
2417 if (gy < 0)
2418 gy -= height - 1;
2420 gx = (gx / width) * width;
2421 gy = (gy / height) * height;
2423 goto store_rect;
2426 gx += WINDOW_LEFT_EDGE_X (w);
2427 gy += WINDOW_TOP_EDGE_Y (w);
2429 store_rect:
2430 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2432 /* Visible feedback for debugging. */
2433 #if 0
2434 #if HAVE_X_WINDOWS
2435 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2436 f->output_data.x->normal_gc,
2437 gx, gy, width, height);
2438 #endif
2439 #endif
2443 #endif /* HAVE_WINDOW_SYSTEM */
2445 static void
2446 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2448 eassert (w);
2449 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2450 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2451 w->window_end_vpos
2452 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2455 /***********************************************************************
2456 Lisp form evaluation
2457 ***********************************************************************/
2459 /* Error handler for safe_eval and safe_call. */
2461 static Lisp_Object
2462 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2464 add_to_log ("Error during redisplay: %S signaled %S",
2465 Flist (nargs, args), arg);
2466 return Qnil;
2469 /* Call function FUNC with the rest of NARGS - 1 arguments
2470 following. Return the result, or nil if something went
2471 wrong. Prevent redisplay during the evaluation. */
2473 Lisp_Object
2474 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2476 Lisp_Object val;
2478 if (inhibit_eval_during_redisplay)
2479 val = Qnil;
2480 else
2482 va_list ap;
2483 ptrdiff_t i;
2484 ptrdiff_t count = SPECPDL_INDEX ();
2485 struct gcpro gcpro1;
2486 Lisp_Object *args = alloca (nargs * word_size);
2488 args[0] = func;
2489 va_start (ap, func);
2490 for (i = 1; i < nargs; i++)
2491 args[i] = va_arg (ap, Lisp_Object);
2492 va_end (ap);
2494 GCPRO1 (args[0]);
2495 gcpro1.nvars = nargs;
2496 specbind (Qinhibit_redisplay, Qt);
2497 /* Use Qt to ensure debugger does not run,
2498 so there is no possibility of wanting to redisplay. */
2499 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2500 safe_eval_handler);
2501 UNGCPRO;
2502 val = unbind_to (count, val);
2505 return val;
2509 /* Call function FN with one argument ARG.
2510 Return the result, or nil if something went wrong. */
2512 Lisp_Object
2513 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2515 return safe_call (2, fn, arg);
2518 static Lisp_Object Qeval;
2520 Lisp_Object
2521 safe_eval (Lisp_Object sexpr)
2523 return safe_call1 (Qeval, sexpr);
2526 /* Call function FN with two arguments ARG1 and ARG2.
2527 Return the result, or nil if something went wrong. */
2529 Lisp_Object
2530 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2532 return safe_call (3, fn, arg1, arg2);
2537 /***********************************************************************
2538 Debugging
2539 ***********************************************************************/
2541 #if 0
2543 /* Define CHECK_IT to perform sanity checks on iterators.
2544 This is for debugging. It is too slow to do unconditionally. */
2546 static void
2547 check_it (struct it *it)
2549 if (it->method == GET_FROM_STRING)
2551 eassert (STRINGP (it->string));
2552 eassert (IT_STRING_CHARPOS (*it) >= 0);
2554 else
2556 eassert (IT_STRING_CHARPOS (*it) < 0);
2557 if (it->method == GET_FROM_BUFFER)
2559 /* Check that character and byte positions agree. */
2560 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2564 if (it->dpvec)
2565 eassert (it->current.dpvec_index >= 0);
2566 else
2567 eassert (it->current.dpvec_index < 0);
2570 #define CHECK_IT(IT) check_it ((IT))
2572 #else /* not 0 */
2574 #define CHECK_IT(IT) (void) 0
2576 #endif /* not 0 */
2579 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2581 /* Check that the window end of window W is what we expect it
2582 to be---the last row in the current matrix displaying text. */
2584 static void
2585 check_window_end (struct window *w)
2587 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2589 struct glyph_row *row;
2590 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2591 !row->enabled_p
2592 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2593 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2597 #define CHECK_WINDOW_END(W) check_window_end ((W))
2599 #else
2601 #define CHECK_WINDOW_END(W) (void) 0
2603 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2605 /* Return mark position if current buffer has the region of non-zero length,
2606 or -1 otherwise. */
2608 static ptrdiff_t
2609 markpos_of_region (void)
2611 if (!NILP (Vtransient_mark_mode)
2612 && !NILP (BVAR (current_buffer, mark_active))
2613 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2615 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2617 if (markpos != PT)
2618 return markpos;
2620 return -1;
2623 /***********************************************************************
2624 Iterator initialization
2625 ***********************************************************************/
2627 /* Initialize IT for displaying current_buffer in window W, starting
2628 at character position CHARPOS. CHARPOS < 0 means that no buffer
2629 position is specified which is useful when the iterator is assigned
2630 a position later. BYTEPOS is the byte position corresponding to
2631 CHARPOS.
2633 If ROW is not null, calls to produce_glyphs with IT as parameter
2634 will produce glyphs in that row.
2636 BASE_FACE_ID is the id of a base face to use. It must be one of
2637 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2638 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2639 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2641 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2642 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2643 will be initialized to use the corresponding mode line glyph row of
2644 the desired matrix of W. */
2646 void
2647 init_iterator (struct it *it, struct window *w,
2648 ptrdiff_t charpos, ptrdiff_t bytepos,
2649 struct glyph_row *row, enum face_id base_face_id)
2651 ptrdiff_t markpos;
2652 enum face_id remapped_base_face_id = base_face_id;
2654 /* Some precondition checks. */
2655 eassert (w != NULL && it != NULL);
2656 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2657 && charpos <= ZV));
2659 /* If face attributes have been changed since the last redisplay,
2660 free realized faces now because they depend on face definitions
2661 that might have changed. Don't free faces while there might be
2662 desired matrices pending which reference these faces. */
2663 if (face_change_count && !inhibit_free_realized_faces)
2665 face_change_count = 0;
2666 free_all_realized_faces (Qnil);
2669 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2670 if (! NILP (Vface_remapping_alist))
2671 remapped_base_face_id
2672 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2674 /* Use one of the mode line rows of W's desired matrix if
2675 appropriate. */
2676 if (row == NULL)
2678 if (base_face_id == MODE_LINE_FACE_ID
2679 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2680 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2681 else if (base_face_id == HEADER_LINE_FACE_ID)
2682 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2685 /* Clear IT. */
2686 memset (it, 0, sizeof *it);
2687 it->current.overlay_string_index = -1;
2688 it->current.dpvec_index = -1;
2689 it->base_face_id = remapped_base_face_id;
2690 it->string = Qnil;
2691 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2692 it->paragraph_embedding = L2R;
2693 it->bidi_it.string.lstring = Qnil;
2694 it->bidi_it.string.s = NULL;
2695 it->bidi_it.string.bufpos = 0;
2696 it->bidi_it.w = w;
2698 /* The window in which we iterate over current_buffer: */
2699 XSETWINDOW (it->window, w);
2700 it->w = w;
2701 it->f = XFRAME (w->frame);
2703 it->cmp_it.id = -1;
2705 /* Extra space between lines (on window systems only). */
2706 if (base_face_id == DEFAULT_FACE_ID
2707 && FRAME_WINDOW_P (it->f))
2709 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2710 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2711 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2712 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2713 * FRAME_LINE_HEIGHT (it->f));
2714 else if (it->f->extra_line_spacing > 0)
2715 it->extra_line_spacing = it->f->extra_line_spacing;
2716 it->max_extra_line_spacing = 0;
2719 /* If realized faces have been removed, e.g. because of face
2720 attribute changes of named faces, recompute them. When running
2721 in batch mode, the face cache of the initial frame is null. If
2722 we happen to get called, make a dummy face cache. */
2723 if (FRAME_FACE_CACHE (it->f) == NULL)
2724 init_frame_faces (it->f);
2725 if (FRAME_FACE_CACHE (it->f)->used == 0)
2726 recompute_basic_faces (it->f);
2728 /* Current value of the `slice', `space-width', and 'height' properties. */
2729 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2730 it->space_width = Qnil;
2731 it->font_height = Qnil;
2732 it->override_ascent = -1;
2734 /* Are control characters displayed as `^C'? */
2735 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2737 /* -1 means everything between a CR and the following line end
2738 is invisible. >0 means lines indented more than this value are
2739 invisible. */
2740 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2741 ? (clip_to_bounds
2742 (-1, XINT (BVAR (current_buffer, selective_display)),
2743 PTRDIFF_MAX))
2744 : (!NILP (BVAR (current_buffer, selective_display))
2745 ? -1 : 0));
2746 it->selective_display_ellipsis_p
2747 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2749 /* Display table to use. */
2750 it->dp = window_display_table (w);
2752 /* Are multibyte characters enabled in current_buffer? */
2753 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2755 /* If visible region is of non-zero length, set IT->region_beg_charpos
2756 and IT->region_end_charpos to the start and end of a visible region
2757 in window IT->w. Set both to -1 to indicate no region. */
2758 markpos = markpos_of_region ();
2759 if (markpos >= 0
2760 /* Maybe highlight only in selected window. */
2761 && (/* Either show region everywhere. */
2762 highlight_nonselected_windows
2763 /* Or show region in the selected window. */
2764 || w == XWINDOW (selected_window)
2765 /* Or show the region if we are in the mini-buffer and W is
2766 the window the mini-buffer refers to. */
2767 || (MINI_WINDOW_P (XWINDOW (selected_window))
2768 && WINDOWP (minibuf_selected_window)
2769 && w == XWINDOW (minibuf_selected_window))))
2771 it->region_beg_charpos = min (PT, markpos);
2772 it->region_end_charpos = max (PT, markpos);
2774 else
2775 it->region_beg_charpos = it->region_end_charpos = -1;
2777 /* Get the position at which the redisplay_end_trigger hook should
2778 be run, if it is to be run at all. */
2779 if (MARKERP (w->redisplay_end_trigger)
2780 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2781 it->redisplay_end_trigger_charpos
2782 = marker_position (w->redisplay_end_trigger);
2783 else if (INTEGERP (w->redisplay_end_trigger))
2784 it->redisplay_end_trigger_charpos =
2785 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2787 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2789 /* Are lines in the display truncated? */
2790 if (base_face_id != DEFAULT_FACE_ID
2791 || it->w->hscroll
2792 || (! WINDOW_FULL_WIDTH_P (it->w)
2793 && ((!NILP (Vtruncate_partial_width_windows)
2794 && !INTEGERP (Vtruncate_partial_width_windows))
2795 || (INTEGERP (Vtruncate_partial_width_windows)
2796 && (WINDOW_TOTAL_COLS (it->w)
2797 < XINT (Vtruncate_partial_width_windows))))))
2798 it->line_wrap = TRUNCATE;
2799 else if (NILP (BVAR (current_buffer, truncate_lines)))
2800 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2801 ? WINDOW_WRAP : WORD_WRAP;
2802 else
2803 it->line_wrap = TRUNCATE;
2805 /* Get dimensions of truncation and continuation glyphs. These are
2806 displayed as fringe bitmaps under X, but we need them for such
2807 frames when the fringes are turned off. But leave the dimensions
2808 zero for tooltip frames, as these glyphs look ugly there and also
2809 sabotage calculations of tooltip dimensions in x-show-tip. */
2810 #ifdef HAVE_WINDOW_SYSTEM
2811 if (!(FRAME_WINDOW_P (it->f)
2812 && FRAMEP (tip_frame)
2813 && it->f == XFRAME (tip_frame)))
2814 #endif
2816 if (it->line_wrap == TRUNCATE)
2818 /* We will need the truncation glyph. */
2819 eassert (it->glyph_row == NULL);
2820 produce_special_glyphs (it, IT_TRUNCATION);
2821 it->truncation_pixel_width = it->pixel_width;
2823 else
2825 /* We will need the continuation glyph. */
2826 eassert (it->glyph_row == NULL);
2827 produce_special_glyphs (it, IT_CONTINUATION);
2828 it->continuation_pixel_width = it->pixel_width;
2832 /* Reset these values to zero because the produce_special_glyphs
2833 above has changed them. */
2834 it->pixel_width = it->ascent = it->descent = 0;
2835 it->phys_ascent = it->phys_descent = 0;
2837 /* Set this after getting the dimensions of truncation and
2838 continuation glyphs, so that we don't produce glyphs when calling
2839 produce_special_glyphs, above. */
2840 it->glyph_row = row;
2841 it->area = TEXT_AREA;
2843 /* Forget any previous info about this row being reversed. */
2844 if (it->glyph_row)
2845 it->glyph_row->reversed_p = 0;
2847 /* Get the dimensions of the display area. The display area
2848 consists of the visible window area plus a horizontally scrolled
2849 part to the left of the window. All x-values are relative to the
2850 start of this total display area. */
2851 if (base_face_id != DEFAULT_FACE_ID)
2853 /* Mode lines, menu bar in terminal frames. */
2854 it->first_visible_x = 0;
2855 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2857 else
2859 it->first_visible_x =
2860 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2861 it->last_visible_x = (it->first_visible_x
2862 + window_box_width (w, TEXT_AREA));
2864 /* If we truncate lines, leave room for the truncation glyph(s) at
2865 the right margin. Otherwise, leave room for the continuation
2866 glyph(s). Done only if the window has no fringes. Since we
2867 don't know at this point whether there will be any R2L lines in
2868 the window, we reserve space for truncation/continuation glyphs
2869 even if only one of the fringes is absent. */
2870 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2871 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2873 if (it->line_wrap == TRUNCATE)
2874 it->last_visible_x -= it->truncation_pixel_width;
2875 else
2876 it->last_visible_x -= it->continuation_pixel_width;
2879 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2880 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2883 /* Leave room for a border glyph. */
2884 if (!FRAME_WINDOW_P (it->f)
2885 && !WINDOW_RIGHTMOST_P (it->w))
2886 it->last_visible_x -= 1;
2888 it->last_visible_y = window_text_bottom_y (w);
2890 /* For mode lines and alike, arrange for the first glyph having a
2891 left box line if the face specifies a box. */
2892 if (base_face_id != DEFAULT_FACE_ID)
2894 struct face *face;
2896 it->face_id = remapped_base_face_id;
2898 /* If we have a boxed mode line, make the first character appear
2899 with a left box line. */
2900 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2901 if (face->box != FACE_NO_BOX)
2902 it->start_of_box_run_p = 1;
2905 /* If a buffer position was specified, set the iterator there,
2906 getting overlays and face properties from that position. */
2907 if (charpos >= BUF_BEG (current_buffer))
2909 it->end_charpos = ZV;
2910 eassert (charpos == BYTE_TO_CHAR (bytepos));
2911 IT_CHARPOS (*it) = charpos;
2912 IT_BYTEPOS (*it) = bytepos;
2914 /* We will rely on `reseat' to set this up properly, via
2915 handle_face_prop. */
2916 it->face_id = it->base_face_id;
2918 it->start = it->current;
2919 /* Do we need to reorder bidirectional text? Not if this is a
2920 unibyte buffer: by definition, none of the single-byte
2921 characters are strong R2L, so no reordering is needed. And
2922 bidi.c doesn't support unibyte buffers anyway. Also, don't
2923 reorder while we are loading loadup.el, since the tables of
2924 character properties needed for reordering are not yet
2925 available. */
2926 it->bidi_p =
2927 NILP (Vpurify_flag)
2928 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2929 && it->multibyte_p;
2931 /* If we are to reorder bidirectional text, init the bidi
2932 iterator. */
2933 if (it->bidi_p)
2935 /* Note the paragraph direction that this buffer wants to
2936 use. */
2937 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2938 Qleft_to_right))
2939 it->paragraph_embedding = L2R;
2940 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2941 Qright_to_left))
2942 it->paragraph_embedding = R2L;
2943 else
2944 it->paragraph_embedding = NEUTRAL_DIR;
2945 bidi_unshelve_cache (NULL, 0);
2946 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2947 &it->bidi_it);
2950 /* Compute faces etc. */
2951 reseat (it, it->current.pos, 1);
2954 CHECK_IT (it);
2958 /* Initialize IT for the display of window W with window start POS. */
2960 void
2961 start_display (struct it *it, struct window *w, struct text_pos pos)
2963 struct glyph_row *row;
2964 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2966 row = w->desired_matrix->rows + first_vpos;
2967 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2968 it->first_vpos = first_vpos;
2970 /* Don't reseat to previous visible line start if current start
2971 position is in a string or image. */
2972 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2974 int start_at_line_beg_p;
2975 int first_y = it->current_y;
2977 /* If window start is not at a line start, skip forward to POS to
2978 get the correct continuation lines width. */
2979 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2980 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2981 if (!start_at_line_beg_p)
2983 int new_x;
2985 reseat_at_previous_visible_line_start (it);
2986 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2988 new_x = it->current_x + it->pixel_width;
2990 /* If lines are continued, this line may end in the middle
2991 of a multi-glyph character (e.g. a control character
2992 displayed as \003, or in the middle of an overlay
2993 string). In this case move_it_to above will not have
2994 taken us to the start of the continuation line but to the
2995 end of the continued line. */
2996 if (it->current_x > 0
2997 && it->line_wrap != TRUNCATE /* Lines are continued. */
2998 && (/* And glyph doesn't fit on the line. */
2999 new_x > it->last_visible_x
3000 /* Or it fits exactly and we're on a window
3001 system frame. */
3002 || (new_x == it->last_visible_x
3003 && FRAME_WINDOW_P (it->f)
3004 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3005 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3006 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3008 if ((it->current.dpvec_index >= 0
3009 || it->current.overlay_string_index >= 0)
3010 /* If we are on a newline from a display vector or
3011 overlay string, then we are already at the end of
3012 a screen line; no need to go to the next line in
3013 that case, as this line is not really continued.
3014 (If we do go to the next line, C-e will not DTRT.) */
3015 && it->c != '\n')
3017 set_iterator_to_next (it, 1);
3018 move_it_in_display_line_to (it, -1, -1, 0);
3021 it->continuation_lines_width += it->current_x;
3023 /* If the character at POS is displayed via a display
3024 vector, move_it_to above stops at the final glyph of
3025 IT->dpvec. To make the caller redisplay that character
3026 again (a.k.a. start at POS), we need to reset the
3027 dpvec_index to the beginning of IT->dpvec. */
3028 else if (it->current.dpvec_index >= 0)
3029 it->current.dpvec_index = 0;
3031 /* We're starting a new display line, not affected by the
3032 height of the continued line, so clear the appropriate
3033 fields in the iterator structure. */
3034 it->max_ascent = it->max_descent = 0;
3035 it->max_phys_ascent = it->max_phys_descent = 0;
3037 it->current_y = first_y;
3038 it->vpos = 0;
3039 it->current_x = it->hpos = 0;
3045 /* Return 1 if POS is a position in ellipses displayed for invisible
3046 text. W is the window we display, for text property lookup. */
3048 static int
3049 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3051 Lisp_Object prop, window;
3052 int ellipses_p = 0;
3053 ptrdiff_t charpos = CHARPOS (pos->pos);
3055 /* If POS specifies a position in a display vector, this might
3056 be for an ellipsis displayed for invisible text. We won't
3057 get the iterator set up for delivering that ellipsis unless
3058 we make sure that it gets aware of the invisible text. */
3059 if (pos->dpvec_index >= 0
3060 && pos->overlay_string_index < 0
3061 && CHARPOS (pos->string_pos) < 0
3062 && charpos > BEGV
3063 && (XSETWINDOW (window, w),
3064 prop = Fget_char_property (make_number (charpos),
3065 Qinvisible, window),
3066 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3068 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3069 window);
3070 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3073 return ellipses_p;
3077 /* Initialize IT for stepping through current_buffer in window W,
3078 starting at position POS that includes overlay string and display
3079 vector/ control character translation position information. Value
3080 is zero if there are overlay strings with newlines at POS. */
3082 static int
3083 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3085 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3086 int i, overlay_strings_with_newlines = 0;
3088 /* If POS specifies a position in a display vector, this might
3089 be for an ellipsis displayed for invisible text. We won't
3090 get the iterator set up for delivering that ellipsis unless
3091 we make sure that it gets aware of the invisible text. */
3092 if (in_ellipses_for_invisible_text_p (pos, w))
3094 --charpos;
3095 bytepos = 0;
3098 /* Keep in mind: the call to reseat in init_iterator skips invisible
3099 text, so we might end up at a position different from POS. This
3100 is only a problem when POS is a row start after a newline and an
3101 overlay starts there with an after-string, and the overlay has an
3102 invisible property. Since we don't skip invisible text in
3103 display_line and elsewhere immediately after consuming the
3104 newline before the row start, such a POS will not be in a string,
3105 but the call to init_iterator below will move us to the
3106 after-string. */
3107 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3109 /* This only scans the current chunk -- it should scan all chunks.
3110 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3111 to 16 in 22.1 to make this a lesser problem. */
3112 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3114 const char *s = SSDATA (it->overlay_strings[i]);
3115 const char *e = s + SBYTES (it->overlay_strings[i]);
3117 while (s < e && *s != '\n')
3118 ++s;
3120 if (s < e)
3122 overlay_strings_with_newlines = 1;
3123 break;
3127 /* If position is within an overlay string, set up IT to the right
3128 overlay string. */
3129 if (pos->overlay_string_index >= 0)
3131 int relative_index;
3133 /* If the first overlay string happens to have a `display'
3134 property for an image, the iterator will be set up for that
3135 image, and we have to undo that setup first before we can
3136 correct the overlay string index. */
3137 if (it->method == GET_FROM_IMAGE)
3138 pop_it (it);
3140 /* We already have the first chunk of overlay strings in
3141 IT->overlay_strings. Load more until the one for
3142 pos->overlay_string_index is in IT->overlay_strings. */
3143 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3145 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3146 it->current.overlay_string_index = 0;
3147 while (n--)
3149 load_overlay_strings (it, 0);
3150 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3154 it->current.overlay_string_index = pos->overlay_string_index;
3155 relative_index = (it->current.overlay_string_index
3156 % OVERLAY_STRING_CHUNK_SIZE);
3157 it->string = it->overlay_strings[relative_index];
3158 eassert (STRINGP (it->string));
3159 it->current.string_pos = pos->string_pos;
3160 it->method = GET_FROM_STRING;
3161 it->end_charpos = SCHARS (it->string);
3162 /* Set up the bidi iterator for this overlay string. */
3163 if (it->bidi_p)
3165 it->bidi_it.string.lstring = it->string;
3166 it->bidi_it.string.s = NULL;
3167 it->bidi_it.string.schars = SCHARS (it->string);
3168 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3169 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3170 it->bidi_it.string.unibyte = !it->multibyte_p;
3171 it->bidi_it.w = it->w;
3172 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3173 FRAME_WINDOW_P (it->f), &it->bidi_it);
3175 /* Synchronize the state of the bidi iterator with
3176 pos->string_pos. For any string position other than
3177 zero, this will be done automagically when we resume
3178 iteration over the string and get_visually_first_element
3179 is called. But if string_pos is zero, and the string is
3180 to be reordered for display, we need to resync manually,
3181 since it could be that the iteration state recorded in
3182 pos ended at string_pos of 0 moving backwards in string. */
3183 if (CHARPOS (pos->string_pos) == 0)
3185 get_visually_first_element (it);
3186 if (IT_STRING_CHARPOS (*it) != 0)
3187 do {
3188 /* Paranoia. */
3189 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3190 bidi_move_to_visually_next (&it->bidi_it);
3191 } while (it->bidi_it.charpos != 0);
3193 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3194 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3198 if (CHARPOS (pos->string_pos) >= 0)
3200 /* Recorded position is not in an overlay string, but in another
3201 string. This can only be a string from a `display' property.
3202 IT should already be filled with that string. */
3203 it->current.string_pos = pos->string_pos;
3204 eassert (STRINGP (it->string));
3205 if (it->bidi_p)
3206 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3207 FRAME_WINDOW_P (it->f), &it->bidi_it);
3210 /* Restore position in display vector translations, control
3211 character translations or ellipses. */
3212 if (pos->dpvec_index >= 0)
3214 if (it->dpvec == NULL)
3215 get_next_display_element (it);
3216 eassert (it->dpvec && it->current.dpvec_index == 0);
3217 it->current.dpvec_index = pos->dpvec_index;
3220 CHECK_IT (it);
3221 return !overlay_strings_with_newlines;
3225 /* Initialize IT for stepping through current_buffer in window W
3226 starting at ROW->start. */
3228 static void
3229 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3231 init_from_display_pos (it, w, &row->start);
3232 it->start = row->start;
3233 it->continuation_lines_width = row->continuation_lines_width;
3234 CHECK_IT (it);
3238 /* Initialize IT for stepping through current_buffer in window W
3239 starting in the line following ROW, i.e. starting at ROW->end.
3240 Value is zero if there are overlay strings with newlines at ROW's
3241 end position. */
3243 static int
3244 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3246 int success = 0;
3248 if (init_from_display_pos (it, w, &row->end))
3250 if (row->continued_p)
3251 it->continuation_lines_width
3252 = row->continuation_lines_width + row->pixel_width;
3253 CHECK_IT (it);
3254 success = 1;
3257 return success;
3263 /***********************************************************************
3264 Text properties
3265 ***********************************************************************/
3267 /* Called when IT reaches IT->stop_charpos. Handle text property and
3268 overlay changes. Set IT->stop_charpos to the next position where
3269 to stop. */
3271 static void
3272 handle_stop (struct it *it)
3274 enum prop_handled handled;
3275 int handle_overlay_change_p;
3276 struct props *p;
3278 it->dpvec = NULL;
3279 it->current.dpvec_index = -1;
3280 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3281 it->ignore_overlay_strings_at_pos_p = 0;
3282 it->ellipsis_p = 0;
3284 /* Use face of preceding text for ellipsis (if invisible) */
3285 if (it->selective_display_ellipsis_p)
3286 it->saved_face_id = it->face_id;
3290 handled = HANDLED_NORMALLY;
3292 /* Call text property handlers. */
3293 for (p = it_props; p->handler; ++p)
3295 handled = p->handler (it);
3297 if (handled == HANDLED_RECOMPUTE_PROPS)
3298 break;
3299 else if (handled == HANDLED_RETURN)
3301 /* We still want to show before and after strings from
3302 overlays even if the actual buffer text is replaced. */
3303 if (!handle_overlay_change_p
3304 || it->sp > 1
3305 /* Don't call get_overlay_strings_1 if we already
3306 have overlay strings loaded, because doing so
3307 will load them again and push the iterator state
3308 onto the stack one more time, which is not
3309 expected by the rest of the code that processes
3310 overlay strings. */
3311 || (it->current.overlay_string_index < 0
3312 ? !get_overlay_strings_1 (it, 0, 0)
3313 : 0))
3315 if (it->ellipsis_p)
3316 setup_for_ellipsis (it, 0);
3317 /* When handling a display spec, we might load an
3318 empty string. In that case, discard it here. We
3319 used to discard it in handle_single_display_spec,
3320 but that causes get_overlay_strings_1, above, to
3321 ignore overlay strings that we must check. */
3322 if (STRINGP (it->string) && !SCHARS (it->string))
3323 pop_it (it);
3324 return;
3326 else if (STRINGP (it->string) && !SCHARS (it->string))
3327 pop_it (it);
3328 else
3330 it->ignore_overlay_strings_at_pos_p = 1;
3331 it->string_from_display_prop_p = 0;
3332 it->from_disp_prop_p = 0;
3333 handle_overlay_change_p = 0;
3335 handled = HANDLED_RECOMPUTE_PROPS;
3336 break;
3338 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3339 handle_overlay_change_p = 0;
3342 if (handled != HANDLED_RECOMPUTE_PROPS)
3344 /* Don't check for overlay strings below when set to deliver
3345 characters from a display vector. */
3346 if (it->method == GET_FROM_DISPLAY_VECTOR)
3347 handle_overlay_change_p = 0;
3349 /* Handle overlay changes.
3350 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3351 if it finds overlays. */
3352 if (handle_overlay_change_p)
3353 handled = handle_overlay_change (it);
3356 if (it->ellipsis_p)
3358 setup_for_ellipsis (it, 0);
3359 break;
3362 while (handled == HANDLED_RECOMPUTE_PROPS);
3364 /* Determine where to stop next. */
3365 if (handled == HANDLED_NORMALLY)
3366 compute_stop_pos (it);
3370 /* Compute IT->stop_charpos from text property and overlay change
3371 information for IT's current position. */
3373 static void
3374 compute_stop_pos (struct it *it)
3376 register INTERVAL iv, next_iv;
3377 Lisp_Object object, limit, position;
3378 ptrdiff_t charpos, bytepos;
3380 if (STRINGP (it->string))
3382 /* Strings are usually short, so don't limit the search for
3383 properties. */
3384 it->stop_charpos = it->end_charpos;
3385 object = it->string;
3386 limit = Qnil;
3387 charpos = IT_STRING_CHARPOS (*it);
3388 bytepos = IT_STRING_BYTEPOS (*it);
3390 else
3392 ptrdiff_t pos;
3394 /* If end_charpos is out of range for some reason, such as a
3395 misbehaving display function, rationalize it (Bug#5984). */
3396 if (it->end_charpos > ZV)
3397 it->end_charpos = ZV;
3398 it->stop_charpos = it->end_charpos;
3400 /* If next overlay change is in front of the current stop pos
3401 (which is IT->end_charpos), stop there. Note: value of
3402 next_overlay_change is point-max if no overlay change
3403 follows. */
3404 charpos = IT_CHARPOS (*it);
3405 bytepos = IT_BYTEPOS (*it);
3406 pos = next_overlay_change (charpos);
3407 if (pos < it->stop_charpos)
3408 it->stop_charpos = pos;
3410 /* If showing the region, we have to stop at the region
3411 start or end because the face might change there. */
3412 if (it->region_beg_charpos > 0)
3414 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3415 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3416 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3417 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3420 /* Set up variables for computing the stop position from text
3421 property changes. */
3422 XSETBUFFER (object, current_buffer);
3423 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3426 /* Get the interval containing IT's position. Value is a null
3427 interval if there isn't such an interval. */
3428 position = make_number (charpos);
3429 iv = validate_interval_range (object, &position, &position, 0);
3430 if (iv)
3432 Lisp_Object values_here[LAST_PROP_IDX];
3433 struct props *p;
3435 /* Get properties here. */
3436 for (p = it_props; p->handler; ++p)
3437 values_here[p->idx] = textget (iv->plist, *p->name);
3439 /* Look for an interval following iv that has different
3440 properties. */
3441 for (next_iv = next_interval (iv);
3442 (next_iv
3443 && (NILP (limit)
3444 || XFASTINT (limit) > next_iv->position));
3445 next_iv = next_interval (next_iv))
3447 for (p = it_props; p->handler; ++p)
3449 Lisp_Object new_value;
3451 new_value = textget (next_iv->plist, *p->name);
3452 if (!EQ (values_here[p->idx], new_value))
3453 break;
3456 if (p->handler)
3457 break;
3460 if (next_iv)
3462 if (INTEGERP (limit)
3463 && next_iv->position >= XFASTINT (limit))
3464 /* No text property change up to limit. */
3465 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3466 else
3467 /* Text properties change in next_iv. */
3468 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3472 if (it->cmp_it.id < 0)
3474 ptrdiff_t stoppos = it->end_charpos;
3476 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3477 stoppos = -1;
3478 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3479 stoppos, it->string);
3482 eassert (STRINGP (it->string)
3483 || (it->stop_charpos >= BEGV
3484 && it->stop_charpos >= IT_CHARPOS (*it)));
3488 /* Return the position of the next overlay change after POS in
3489 current_buffer. Value is point-max if no overlay change
3490 follows. This is like `next-overlay-change' but doesn't use
3491 xmalloc. */
3493 static ptrdiff_t
3494 next_overlay_change (ptrdiff_t pos)
3496 ptrdiff_t i, noverlays;
3497 ptrdiff_t endpos;
3498 Lisp_Object *overlays;
3500 /* Get all overlays at the given position. */
3501 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3503 /* If any of these overlays ends before endpos,
3504 use its ending point instead. */
3505 for (i = 0; i < noverlays; ++i)
3507 Lisp_Object oend;
3508 ptrdiff_t oendpos;
3510 oend = OVERLAY_END (overlays[i]);
3511 oendpos = OVERLAY_POSITION (oend);
3512 endpos = min (endpos, oendpos);
3515 return endpos;
3518 /* How many characters forward to search for a display property or
3519 display string. Searching too far forward makes the bidi display
3520 sluggish, especially in small windows. */
3521 #define MAX_DISP_SCAN 250
3523 /* Return the character position of a display string at or after
3524 position specified by POSITION. If no display string exists at or
3525 after POSITION, return ZV. A display string is either an overlay
3526 with `display' property whose value is a string, or a `display'
3527 text property whose value is a string. STRING is data about the
3528 string to iterate; if STRING->lstring is nil, we are iterating a
3529 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3530 on a GUI frame. DISP_PROP is set to zero if we searched
3531 MAX_DISP_SCAN characters forward without finding any display
3532 strings, non-zero otherwise. It is set to 2 if the display string
3533 uses any kind of `(space ...)' spec that will produce a stretch of
3534 white space in the text area. */
3535 ptrdiff_t
3536 compute_display_string_pos (struct text_pos *position,
3537 struct bidi_string_data *string,
3538 struct window *w,
3539 int frame_window_p, int *disp_prop)
3541 /* OBJECT = nil means current buffer. */
3542 Lisp_Object object, object1;
3543 Lisp_Object pos, spec, limpos;
3544 int string_p = (string && (STRINGP (string->lstring) || string->s));
3545 ptrdiff_t eob = string_p ? string->schars : ZV;
3546 ptrdiff_t begb = string_p ? 0 : BEGV;
3547 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3548 ptrdiff_t lim =
3549 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3550 struct text_pos tpos;
3551 int rv = 0;
3553 if (string && STRINGP (string->lstring))
3554 object1 = object = string->lstring;
3555 else if (w && !string_p)
3557 XSETWINDOW (object, w);
3558 object1 = Qnil;
3560 else
3561 object1 = object = Qnil;
3563 *disp_prop = 1;
3565 if (charpos >= eob
3566 /* We don't support display properties whose values are strings
3567 that have display string properties. */
3568 || string->from_disp_str
3569 /* C strings cannot have display properties. */
3570 || (string->s && !STRINGP (object)))
3572 *disp_prop = 0;
3573 return eob;
3576 /* If the character at CHARPOS is where the display string begins,
3577 return CHARPOS. */
3578 pos = make_number (charpos);
3579 if (STRINGP (object))
3580 bufpos = string->bufpos;
3581 else
3582 bufpos = charpos;
3583 tpos = *position;
3584 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3585 && (charpos <= begb
3586 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3587 object),
3588 spec))
3589 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3590 frame_window_p)))
3592 if (rv == 2)
3593 *disp_prop = 2;
3594 return charpos;
3597 /* Look forward for the first character with a `display' property
3598 that will replace the underlying text when displayed. */
3599 limpos = make_number (lim);
3600 do {
3601 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3602 CHARPOS (tpos) = XFASTINT (pos);
3603 if (CHARPOS (tpos) >= lim)
3605 *disp_prop = 0;
3606 break;
3608 if (STRINGP (object))
3609 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3610 else
3611 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3612 spec = Fget_char_property (pos, Qdisplay, object);
3613 if (!STRINGP (object))
3614 bufpos = CHARPOS (tpos);
3615 } while (NILP (spec)
3616 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3617 bufpos, frame_window_p)));
3618 if (rv == 2)
3619 *disp_prop = 2;
3621 return CHARPOS (tpos);
3624 /* Return the character position of the end of the display string that
3625 started at CHARPOS. If there's no display string at CHARPOS,
3626 return -1. A display string is either an overlay with `display'
3627 property whose value is a string or a `display' text property whose
3628 value is a string. */
3629 ptrdiff_t
3630 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3632 /* OBJECT = nil means current buffer. */
3633 Lisp_Object object =
3634 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3635 Lisp_Object pos = make_number (charpos);
3636 ptrdiff_t eob =
3637 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3639 if (charpos >= eob || (string->s && !STRINGP (object)))
3640 return eob;
3642 /* It could happen that the display property or overlay was removed
3643 since we found it in compute_display_string_pos above. One way
3644 this can happen is if JIT font-lock was called (through
3645 handle_fontified_prop), and jit-lock-functions remove text
3646 properties or overlays from the portion of buffer that includes
3647 CHARPOS. Muse mode is known to do that, for example. In this
3648 case, we return -1 to the caller, to signal that no display
3649 string is actually present at CHARPOS. See bidi_fetch_char for
3650 how this is handled.
3652 An alternative would be to never look for display properties past
3653 it->stop_charpos. But neither compute_display_string_pos nor
3654 bidi_fetch_char that calls it know or care where the next
3655 stop_charpos is. */
3656 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3657 return -1;
3659 /* Look forward for the first character where the `display' property
3660 changes. */
3661 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3663 return XFASTINT (pos);
3668 /***********************************************************************
3669 Fontification
3670 ***********************************************************************/
3672 /* Handle changes in the `fontified' property of the current buffer by
3673 calling hook functions from Qfontification_functions to fontify
3674 regions of text. */
3676 static enum prop_handled
3677 handle_fontified_prop (struct it *it)
3679 Lisp_Object prop, pos;
3680 enum prop_handled handled = HANDLED_NORMALLY;
3682 if (!NILP (Vmemory_full))
3683 return handled;
3685 /* Get the value of the `fontified' property at IT's current buffer
3686 position. (The `fontified' property doesn't have a special
3687 meaning in strings.) If the value is nil, call functions from
3688 Qfontification_functions. */
3689 if (!STRINGP (it->string)
3690 && it->s == NULL
3691 && !NILP (Vfontification_functions)
3692 && !NILP (Vrun_hooks)
3693 && (pos = make_number (IT_CHARPOS (*it)),
3694 prop = Fget_char_property (pos, Qfontified, Qnil),
3695 /* Ignore the special cased nil value always present at EOB since
3696 no amount of fontifying will be able to change it. */
3697 NILP (prop) && IT_CHARPOS (*it) < Z))
3699 ptrdiff_t count = SPECPDL_INDEX ();
3700 Lisp_Object val;
3701 struct buffer *obuf = current_buffer;
3702 int begv = BEGV, zv = ZV;
3703 int old_clip_changed = current_buffer->clip_changed;
3705 val = Vfontification_functions;
3706 specbind (Qfontification_functions, Qnil);
3708 eassert (it->end_charpos == ZV);
3710 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3711 safe_call1 (val, pos);
3712 else
3714 Lisp_Object fns, fn;
3715 struct gcpro gcpro1, gcpro2;
3717 fns = Qnil;
3718 GCPRO2 (val, fns);
3720 for (; CONSP (val); val = XCDR (val))
3722 fn = XCAR (val);
3724 if (EQ (fn, Qt))
3726 /* A value of t indicates this hook has a local
3727 binding; it means to run the global binding too.
3728 In a global value, t should not occur. If it
3729 does, we must ignore it to avoid an endless
3730 loop. */
3731 for (fns = Fdefault_value (Qfontification_functions);
3732 CONSP (fns);
3733 fns = XCDR (fns))
3735 fn = XCAR (fns);
3736 if (!EQ (fn, Qt))
3737 safe_call1 (fn, pos);
3740 else
3741 safe_call1 (fn, pos);
3744 UNGCPRO;
3747 unbind_to (count, Qnil);
3749 /* Fontification functions routinely call `save-restriction'.
3750 Normally, this tags clip_changed, which can confuse redisplay
3751 (see discussion in Bug#6671). Since we don't perform any
3752 special handling of fontification changes in the case where
3753 `save-restriction' isn't called, there's no point doing so in
3754 this case either. So, if the buffer's restrictions are
3755 actually left unchanged, reset clip_changed. */
3756 if (obuf == current_buffer)
3758 if (begv == BEGV && zv == ZV)
3759 current_buffer->clip_changed = old_clip_changed;
3761 /* There isn't much we can reasonably do to protect against
3762 misbehaving fontification, but here's a fig leaf. */
3763 else if (BUFFER_LIVE_P (obuf))
3764 set_buffer_internal_1 (obuf);
3766 /* The fontification code may have added/removed text.
3767 It could do even a lot worse, but let's at least protect against
3768 the most obvious case where only the text past `pos' gets changed',
3769 as is/was done in grep.el where some escapes sequences are turned
3770 into face properties (bug#7876). */
3771 it->end_charpos = ZV;
3773 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3774 something. This avoids an endless loop if they failed to
3775 fontify the text for which reason ever. */
3776 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3777 handled = HANDLED_RECOMPUTE_PROPS;
3780 return handled;
3785 /***********************************************************************
3786 Faces
3787 ***********************************************************************/
3789 /* Set up iterator IT from face properties at its current position.
3790 Called from handle_stop. */
3792 static enum prop_handled
3793 handle_face_prop (struct it *it)
3795 int new_face_id;
3796 ptrdiff_t next_stop;
3798 if (!STRINGP (it->string))
3800 new_face_id
3801 = face_at_buffer_position (it->w,
3802 IT_CHARPOS (*it),
3803 it->region_beg_charpos,
3804 it->region_end_charpos,
3805 &next_stop,
3806 (IT_CHARPOS (*it)
3807 + TEXT_PROP_DISTANCE_LIMIT),
3808 0, it->base_face_id);
3810 /* Is this a start of a run of characters with box face?
3811 Caveat: this can be called for a freshly initialized
3812 iterator; face_id is -1 in this case. We know that the new
3813 face will not change until limit, i.e. if the new face has a
3814 box, all characters up to limit will have one. But, as
3815 usual, we don't know whether limit is really the end. */
3816 if (new_face_id != it->face_id)
3818 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3819 /* If it->face_id is -1, old_face below will be NULL, see
3820 the definition of FACE_FROM_ID. This will happen if this
3821 is the initial call that gets the face. */
3822 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3824 /* If the value of face_id of the iterator is -1, we have to
3825 look in front of IT's position and see whether there is a
3826 face there that's different from new_face_id. */
3827 if (!old_face && IT_CHARPOS (*it) > BEG)
3829 int prev_face_id = face_before_it_pos (it);
3831 old_face = FACE_FROM_ID (it->f, prev_face_id);
3834 /* If the new face has a box, but the old face does not,
3835 this is the start of a run of characters with box face,
3836 i.e. this character has a shadow on the left side. */
3837 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3838 && (old_face == NULL || !old_face->box));
3839 it->face_box_p = new_face->box != FACE_NO_BOX;
3842 else
3844 int base_face_id;
3845 ptrdiff_t bufpos;
3846 int i;
3847 Lisp_Object from_overlay
3848 = (it->current.overlay_string_index >= 0
3849 ? it->string_overlays[it->current.overlay_string_index
3850 % OVERLAY_STRING_CHUNK_SIZE]
3851 : Qnil);
3853 /* See if we got to this string directly or indirectly from
3854 an overlay property. That includes the before-string or
3855 after-string of an overlay, strings in display properties
3856 provided by an overlay, their text properties, etc.
3858 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3859 if (! NILP (from_overlay))
3860 for (i = it->sp - 1; i >= 0; i--)
3862 if (it->stack[i].current.overlay_string_index >= 0)
3863 from_overlay
3864 = it->string_overlays[it->stack[i].current.overlay_string_index
3865 % OVERLAY_STRING_CHUNK_SIZE];
3866 else if (! NILP (it->stack[i].from_overlay))
3867 from_overlay = it->stack[i].from_overlay;
3869 if (!NILP (from_overlay))
3870 break;
3873 if (! NILP (from_overlay))
3875 bufpos = IT_CHARPOS (*it);
3876 /* For a string from an overlay, the base face depends
3877 only on text properties and ignores overlays. */
3878 base_face_id
3879 = face_for_overlay_string (it->w,
3880 IT_CHARPOS (*it),
3881 it->region_beg_charpos,
3882 it->region_end_charpos,
3883 &next_stop,
3884 (IT_CHARPOS (*it)
3885 + TEXT_PROP_DISTANCE_LIMIT),
3887 from_overlay);
3889 else
3891 bufpos = 0;
3893 /* For strings from a `display' property, use the face at
3894 IT's current buffer position as the base face to merge
3895 with, so that overlay strings appear in the same face as
3896 surrounding text, unless they specify their own faces.
3897 For strings from wrap-prefix and line-prefix properties,
3898 use the default face, possibly remapped via
3899 Vface_remapping_alist. */
3900 base_face_id = it->string_from_prefix_prop_p
3901 ? (!NILP (Vface_remapping_alist)
3902 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3903 : DEFAULT_FACE_ID)
3904 : underlying_face_id (it);
3907 new_face_id = face_at_string_position (it->w,
3908 it->string,
3909 IT_STRING_CHARPOS (*it),
3910 bufpos,
3911 it->region_beg_charpos,
3912 it->region_end_charpos,
3913 &next_stop,
3914 base_face_id, 0);
3916 /* Is this a start of a run of characters with box? Caveat:
3917 this can be called for a freshly allocated iterator; face_id
3918 is -1 is this case. We know that the new face will not
3919 change until the next check pos, i.e. if the new face has a
3920 box, all characters up to that position will have a
3921 box. But, as usual, we don't know whether that position
3922 is really the end. */
3923 if (new_face_id != it->face_id)
3925 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3926 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3928 /* If new face has a box but old face hasn't, this is the
3929 start of a run of characters with box, i.e. it has a
3930 shadow on the left side. */
3931 it->start_of_box_run_p
3932 = new_face->box && (old_face == NULL || !old_face->box);
3933 it->face_box_p = new_face->box != FACE_NO_BOX;
3937 it->face_id = new_face_id;
3938 return HANDLED_NORMALLY;
3942 /* Return the ID of the face ``underlying'' IT's current position,
3943 which is in a string. If the iterator is associated with a
3944 buffer, return the face at IT's current buffer position.
3945 Otherwise, use the iterator's base_face_id. */
3947 static int
3948 underlying_face_id (struct it *it)
3950 int face_id = it->base_face_id, i;
3952 eassert (STRINGP (it->string));
3954 for (i = it->sp - 1; i >= 0; --i)
3955 if (NILP (it->stack[i].string))
3956 face_id = it->stack[i].face_id;
3958 return face_id;
3962 /* Compute the face one character before or after the current position
3963 of IT, in the visual order. BEFORE_P non-zero means get the face
3964 in front (to the left in L2R paragraphs, to the right in R2L
3965 paragraphs) of IT's screen position. Value is the ID of the face. */
3967 static int
3968 face_before_or_after_it_pos (struct it *it, int before_p)
3970 int face_id, limit;
3971 ptrdiff_t next_check_charpos;
3972 struct it it_copy;
3973 void *it_copy_data = NULL;
3975 eassert (it->s == NULL);
3977 if (STRINGP (it->string))
3979 ptrdiff_t bufpos, charpos;
3980 int base_face_id;
3982 /* No face change past the end of the string (for the case
3983 we are padding with spaces). No face change before the
3984 string start. */
3985 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3986 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3987 return it->face_id;
3989 if (!it->bidi_p)
3991 /* Set charpos to the position before or after IT's current
3992 position, in the logical order, which in the non-bidi
3993 case is the same as the visual order. */
3994 if (before_p)
3995 charpos = IT_STRING_CHARPOS (*it) - 1;
3996 else if (it->what == IT_COMPOSITION)
3997 /* For composition, we must check the character after the
3998 composition. */
3999 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4000 else
4001 charpos = IT_STRING_CHARPOS (*it) + 1;
4003 else
4005 if (before_p)
4007 /* With bidi iteration, the character before the current
4008 in the visual order cannot be found by simple
4009 iteration, because "reverse" reordering is not
4010 supported. Instead, we need to use the move_it_*
4011 family of functions. */
4012 /* Ignore face changes before the first visible
4013 character on this display line. */
4014 if (it->current_x <= it->first_visible_x)
4015 return it->face_id;
4016 SAVE_IT (it_copy, *it, it_copy_data);
4017 /* Implementation note: Since move_it_in_display_line
4018 works in the iterator geometry, and thinks the first
4019 character is always the leftmost, even in R2L lines,
4020 we don't need to distinguish between the R2L and L2R
4021 cases here. */
4022 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4023 it_copy.current_x - 1, MOVE_TO_X);
4024 charpos = IT_STRING_CHARPOS (it_copy);
4025 RESTORE_IT (it, it, it_copy_data);
4027 else
4029 /* Set charpos to the string position of the character
4030 that comes after IT's current position in the visual
4031 order. */
4032 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4034 it_copy = *it;
4035 while (n--)
4036 bidi_move_to_visually_next (&it_copy.bidi_it);
4038 charpos = it_copy.bidi_it.charpos;
4041 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4043 if (it->current.overlay_string_index >= 0)
4044 bufpos = IT_CHARPOS (*it);
4045 else
4046 bufpos = 0;
4048 base_face_id = underlying_face_id (it);
4050 /* Get the face for ASCII, or unibyte. */
4051 face_id = face_at_string_position (it->w,
4052 it->string,
4053 charpos,
4054 bufpos,
4055 it->region_beg_charpos,
4056 it->region_end_charpos,
4057 &next_check_charpos,
4058 base_face_id, 0);
4060 /* Correct the face for charsets different from ASCII. Do it
4061 for the multibyte case only. The face returned above is
4062 suitable for unibyte text if IT->string is unibyte. */
4063 if (STRING_MULTIBYTE (it->string))
4065 struct text_pos pos1 = string_pos (charpos, it->string);
4066 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4067 int c, len;
4068 struct face *face = FACE_FROM_ID (it->f, face_id);
4070 c = string_char_and_length (p, &len);
4071 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4074 else
4076 struct text_pos pos;
4078 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4079 || (IT_CHARPOS (*it) <= BEGV && before_p))
4080 return it->face_id;
4082 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4083 pos = it->current.pos;
4085 if (!it->bidi_p)
4087 if (before_p)
4088 DEC_TEXT_POS (pos, it->multibyte_p);
4089 else
4091 if (it->what == IT_COMPOSITION)
4093 /* For composition, we must check the position after
4094 the composition. */
4095 pos.charpos += it->cmp_it.nchars;
4096 pos.bytepos += it->len;
4098 else
4099 INC_TEXT_POS (pos, it->multibyte_p);
4102 else
4104 if (before_p)
4106 /* With bidi iteration, the character before the current
4107 in the visual order cannot be found by simple
4108 iteration, because "reverse" reordering is not
4109 supported. Instead, we need to use the move_it_*
4110 family of functions. */
4111 /* Ignore face changes before the first visible
4112 character on this display line. */
4113 if (it->current_x <= it->first_visible_x)
4114 return it->face_id;
4115 SAVE_IT (it_copy, *it, it_copy_data);
4116 /* Implementation note: Since move_it_in_display_line
4117 works in the iterator geometry, and thinks the first
4118 character is always the leftmost, even in R2L lines,
4119 we don't need to distinguish between the R2L and L2R
4120 cases here. */
4121 move_it_in_display_line (&it_copy, ZV,
4122 it_copy.current_x - 1, MOVE_TO_X);
4123 pos = it_copy.current.pos;
4124 RESTORE_IT (it, it, it_copy_data);
4126 else
4128 /* Set charpos to the buffer position of the character
4129 that comes after IT's current position in the visual
4130 order. */
4131 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4133 it_copy = *it;
4134 while (n--)
4135 bidi_move_to_visually_next (&it_copy.bidi_it);
4137 SET_TEXT_POS (pos,
4138 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4141 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4143 /* Determine face for CHARSET_ASCII, or unibyte. */
4144 face_id = face_at_buffer_position (it->w,
4145 CHARPOS (pos),
4146 it->region_beg_charpos,
4147 it->region_end_charpos,
4148 &next_check_charpos,
4149 limit, 0, -1);
4151 /* Correct the face for charsets different from ASCII. Do it
4152 for the multibyte case only. The face returned above is
4153 suitable for unibyte text if current_buffer is unibyte. */
4154 if (it->multibyte_p)
4156 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4157 struct face *face = FACE_FROM_ID (it->f, face_id);
4158 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4162 return face_id;
4167 /***********************************************************************
4168 Invisible text
4169 ***********************************************************************/
4171 /* Set up iterator IT from invisible properties at its current
4172 position. Called from handle_stop. */
4174 static enum prop_handled
4175 handle_invisible_prop (struct it *it)
4177 enum prop_handled handled = HANDLED_NORMALLY;
4178 int invis_p;
4179 Lisp_Object prop;
4181 if (STRINGP (it->string))
4183 Lisp_Object end_charpos, limit, charpos;
4185 /* Get the value of the invisible text property at the
4186 current position. Value will be nil if there is no such
4187 property. */
4188 charpos = make_number (IT_STRING_CHARPOS (*it));
4189 prop = Fget_text_property (charpos, Qinvisible, it->string);
4190 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4192 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4194 /* Record whether we have to display an ellipsis for the
4195 invisible text. */
4196 int display_ellipsis_p = (invis_p == 2);
4197 ptrdiff_t len, endpos;
4199 handled = HANDLED_RECOMPUTE_PROPS;
4201 /* Get the position at which the next visible text can be
4202 found in IT->string, if any. */
4203 endpos = len = SCHARS (it->string);
4204 XSETINT (limit, len);
4207 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4208 it->string, limit);
4209 if (INTEGERP (end_charpos))
4211 endpos = XFASTINT (end_charpos);
4212 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4213 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4214 if (invis_p == 2)
4215 display_ellipsis_p = 1;
4218 while (invis_p && endpos < len);
4220 if (display_ellipsis_p)
4221 it->ellipsis_p = 1;
4223 if (endpos < len)
4225 /* Text at END_CHARPOS is visible. Move IT there. */
4226 struct text_pos old;
4227 ptrdiff_t oldpos;
4229 old = it->current.string_pos;
4230 oldpos = CHARPOS (old);
4231 if (it->bidi_p)
4233 if (it->bidi_it.first_elt
4234 && it->bidi_it.charpos < SCHARS (it->string))
4235 bidi_paragraph_init (it->paragraph_embedding,
4236 &it->bidi_it, 1);
4237 /* Bidi-iterate out of the invisible text. */
4240 bidi_move_to_visually_next (&it->bidi_it);
4242 while (oldpos <= it->bidi_it.charpos
4243 && it->bidi_it.charpos < endpos);
4245 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4246 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4247 if (IT_CHARPOS (*it) >= endpos)
4248 it->prev_stop = endpos;
4250 else
4252 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4253 compute_string_pos (&it->current.string_pos, old, it->string);
4256 else
4258 /* The rest of the string is invisible. If this is an
4259 overlay string, proceed with the next overlay string
4260 or whatever comes and return a character from there. */
4261 if (it->current.overlay_string_index >= 0
4262 && !display_ellipsis_p)
4264 next_overlay_string (it);
4265 /* Don't check for overlay strings when we just
4266 finished processing them. */
4267 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4269 else
4271 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4272 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4277 else
4279 ptrdiff_t newpos, next_stop, start_charpos, tem;
4280 Lisp_Object pos, overlay;
4282 /* First of all, is there invisible text at this position? */
4283 tem = start_charpos = IT_CHARPOS (*it);
4284 pos = make_number (tem);
4285 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4286 &overlay);
4287 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4289 /* If we are on invisible text, skip over it. */
4290 if (invis_p && start_charpos < it->end_charpos)
4292 /* Record whether we have to display an ellipsis for the
4293 invisible text. */
4294 int display_ellipsis_p = invis_p == 2;
4296 handled = HANDLED_RECOMPUTE_PROPS;
4298 /* Loop skipping over invisible text. The loop is left at
4299 ZV or with IT on the first char being visible again. */
4302 /* Try to skip some invisible text. Return value is the
4303 position reached which can be equal to where we start
4304 if there is nothing invisible there. This skips both
4305 over invisible text properties and overlays with
4306 invisible property. */
4307 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4309 /* If we skipped nothing at all we weren't at invisible
4310 text in the first place. If everything to the end of
4311 the buffer was skipped, end the loop. */
4312 if (newpos == tem || newpos >= ZV)
4313 invis_p = 0;
4314 else
4316 /* We skipped some characters but not necessarily
4317 all there are. Check if we ended up on visible
4318 text. Fget_char_property returns the property of
4319 the char before the given position, i.e. if we
4320 get invis_p = 0, this means that the char at
4321 newpos is visible. */
4322 pos = make_number (newpos);
4323 prop = Fget_char_property (pos, Qinvisible, it->window);
4324 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4327 /* If we ended up on invisible text, proceed to
4328 skip starting with next_stop. */
4329 if (invis_p)
4330 tem = next_stop;
4332 /* If there are adjacent invisible texts, don't lose the
4333 second one's ellipsis. */
4334 if (invis_p == 2)
4335 display_ellipsis_p = 1;
4337 while (invis_p);
4339 /* The position newpos is now either ZV or on visible text. */
4340 if (it->bidi_p)
4342 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4343 int on_newline =
4344 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4345 int after_newline =
4346 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4348 /* If the invisible text ends on a newline or on a
4349 character after a newline, we can avoid the costly,
4350 character by character, bidi iteration to NEWPOS, and
4351 instead simply reseat the iterator there. That's
4352 because all bidi reordering information is tossed at
4353 the newline. This is a big win for modes that hide
4354 complete lines, like Outline, Org, etc. */
4355 if (on_newline || after_newline)
4357 struct text_pos tpos;
4358 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4360 SET_TEXT_POS (tpos, newpos, bpos);
4361 reseat_1 (it, tpos, 0);
4362 /* If we reseat on a newline/ZV, we need to prep the
4363 bidi iterator for advancing to the next character
4364 after the newline/EOB, keeping the current paragraph
4365 direction (so that PRODUCE_GLYPHS does TRT wrt
4366 prepending/appending glyphs to a glyph row). */
4367 if (on_newline)
4369 it->bidi_it.first_elt = 0;
4370 it->bidi_it.paragraph_dir = pdir;
4371 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4372 it->bidi_it.nchars = 1;
4373 it->bidi_it.ch_len = 1;
4376 else /* Must use the slow method. */
4378 /* With bidi iteration, the region of invisible text
4379 could start and/or end in the middle of a
4380 non-base embedding level. Therefore, we need to
4381 skip invisible text using the bidi iterator,
4382 starting at IT's current position, until we find
4383 ourselves outside of the invisible text.
4384 Skipping invisible text _after_ bidi iteration
4385 avoids affecting the visual order of the
4386 displayed text when invisible properties are
4387 added or removed. */
4388 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4390 /* If we were `reseat'ed to a new paragraph,
4391 determine the paragraph base direction. We
4392 need to do it now because
4393 next_element_from_buffer may not have a
4394 chance to do it, if we are going to skip any
4395 text at the beginning, which resets the
4396 FIRST_ELT flag. */
4397 bidi_paragraph_init (it->paragraph_embedding,
4398 &it->bidi_it, 1);
4402 bidi_move_to_visually_next (&it->bidi_it);
4404 while (it->stop_charpos <= it->bidi_it.charpos
4405 && it->bidi_it.charpos < newpos);
4406 IT_CHARPOS (*it) = it->bidi_it.charpos;
4407 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4408 /* If we overstepped NEWPOS, record its position in
4409 the iterator, so that we skip invisible text if
4410 later the bidi iteration lands us in the
4411 invisible region again. */
4412 if (IT_CHARPOS (*it) >= newpos)
4413 it->prev_stop = newpos;
4416 else
4418 IT_CHARPOS (*it) = newpos;
4419 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4422 /* If there are before-strings at the start of invisible
4423 text, and the text is invisible because of a text
4424 property, arrange to show before-strings because 20.x did
4425 it that way. (If the text is invisible because of an
4426 overlay property instead of a text property, this is
4427 already handled in the overlay code.) */
4428 if (NILP (overlay)
4429 && get_overlay_strings (it, it->stop_charpos))
4431 handled = HANDLED_RECOMPUTE_PROPS;
4432 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4434 else if (display_ellipsis_p)
4436 /* Make sure that the glyphs of the ellipsis will get
4437 correct `charpos' values. If we would not update
4438 it->position here, the glyphs would belong to the
4439 last visible character _before_ the invisible
4440 text, which confuses `set_cursor_from_row'.
4442 We use the last invisible position instead of the
4443 first because this way the cursor is always drawn on
4444 the first "." of the ellipsis, whenever PT is inside
4445 the invisible text. Otherwise the cursor would be
4446 placed _after_ the ellipsis when the point is after the
4447 first invisible character. */
4448 if (!STRINGP (it->object))
4450 it->position.charpos = newpos - 1;
4451 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4453 it->ellipsis_p = 1;
4454 /* Let the ellipsis display before
4455 considering any properties of the following char.
4456 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4457 handled = HANDLED_RETURN;
4462 return handled;
4466 /* Make iterator IT return `...' next.
4467 Replaces LEN characters from buffer. */
4469 static void
4470 setup_for_ellipsis (struct it *it, int len)
4472 /* Use the display table definition for `...'. Invalid glyphs
4473 will be handled by the method returning elements from dpvec. */
4474 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4476 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4477 it->dpvec = v->contents;
4478 it->dpend = v->contents + v->header.size;
4480 else
4482 /* Default `...'. */
4483 it->dpvec = default_invis_vector;
4484 it->dpend = default_invis_vector + 3;
4487 it->dpvec_char_len = len;
4488 it->current.dpvec_index = 0;
4489 it->dpvec_face_id = -1;
4491 /* Remember the current face id in case glyphs specify faces.
4492 IT's face is restored in set_iterator_to_next.
4493 saved_face_id was set to preceding char's face in handle_stop. */
4494 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4495 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4497 it->method = GET_FROM_DISPLAY_VECTOR;
4498 it->ellipsis_p = 1;
4503 /***********************************************************************
4504 'display' property
4505 ***********************************************************************/
4507 /* Set up iterator IT from `display' property at its current position.
4508 Called from handle_stop.
4509 We return HANDLED_RETURN if some part of the display property
4510 overrides the display of the buffer text itself.
4511 Otherwise we return HANDLED_NORMALLY. */
4513 static enum prop_handled
4514 handle_display_prop (struct it *it)
4516 Lisp_Object propval, object, overlay;
4517 struct text_pos *position;
4518 ptrdiff_t bufpos;
4519 /* Nonzero if some property replaces the display of the text itself. */
4520 int display_replaced_p = 0;
4522 if (STRINGP (it->string))
4524 object = it->string;
4525 position = &it->current.string_pos;
4526 bufpos = CHARPOS (it->current.pos);
4528 else
4530 XSETWINDOW (object, it->w);
4531 position = &it->current.pos;
4532 bufpos = CHARPOS (*position);
4535 /* Reset those iterator values set from display property values. */
4536 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4537 it->space_width = Qnil;
4538 it->font_height = Qnil;
4539 it->voffset = 0;
4541 /* We don't support recursive `display' properties, i.e. string
4542 values that have a string `display' property, that have a string
4543 `display' property etc. */
4544 if (!it->string_from_display_prop_p)
4545 it->area = TEXT_AREA;
4547 propval = get_char_property_and_overlay (make_number (position->charpos),
4548 Qdisplay, object, &overlay);
4549 if (NILP (propval))
4550 return HANDLED_NORMALLY;
4551 /* Now OVERLAY is the overlay that gave us this property, or nil
4552 if it was a text property. */
4554 if (!STRINGP (it->string))
4555 object = it->w->contents;
4557 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4558 position, bufpos,
4559 FRAME_WINDOW_P (it->f));
4561 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4564 /* Subroutine of handle_display_prop. Returns non-zero if the display
4565 specification in SPEC is a replacing specification, i.e. it would
4566 replace the text covered by `display' property with something else,
4567 such as an image or a display string. If SPEC includes any kind or
4568 `(space ...) specification, the value is 2; this is used by
4569 compute_display_string_pos, which see.
4571 See handle_single_display_spec for documentation of arguments.
4572 frame_window_p is non-zero if the window being redisplayed is on a
4573 GUI frame; this argument is used only if IT is NULL, see below.
4575 IT can be NULL, if this is called by the bidi reordering code
4576 through compute_display_string_pos, which see. In that case, this
4577 function only examines SPEC, but does not otherwise "handle" it, in
4578 the sense that it doesn't set up members of IT from the display
4579 spec. */
4580 static int
4581 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4582 Lisp_Object overlay, struct text_pos *position,
4583 ptrdiff_t bufpos, int frame_window_p)
4585 int replacing_p = 0;
4586 int rv;
4588 if (CONSP (spec)
4589 /* Simple specifications. */
4590 && !EQ (XCAR (spec), Qimage)
4591 && !EQ (XCAR (spec), Qspace)
4592 && !EQ (XCAR (spec), Qwhen)
4593 && !EQ (XCAR (spec), Qslice)
4594 && !EQ (XCAR (spec), Qspace_width)
4595 && !EQ (XCAR (spec), Qheight)
4596 && !EQ (XCAR (spec), Qraise)
4597 /* Marginal area specifications. */
4598 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4599 && !EQ (XCAR (spec), Qleft_fringe)
4600 && !EQ (XCAR (spec), Qright_fringe)
4601 && !NILP (XCAR (spec)))
4603 for (; CONSP (spec); spec = XCDR (spec))
4605 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4606 overlay, position, bufpos,
4607 replacing_p, frame_window_p)))
4609 replacing_p = rv;
4610 /* If some text in a string is replaced, `position' no
4611 longer points to the position of `object'. */
4612 if (!it || STRINGP (object))
4613 break;
4617 else if (VECTORP (spec))
4619 ptrdiff_t i;
4620 for (i = 0; i < ASIZE (spec); ++i)
4621 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4622 overlay, position, bufpos,
4623 replacing_p, frame_window_p)))
4625 replacing_p = rv;
4626 /* If some text in a string is replaced, `position' no
4627 longer points to the position of `object'. */
4628 if (!it || STRINGP (object))
4629 break;
4632 else
4634 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4635 position, bufpos, 0,
4636 frame_window_p)))
4637 replacing_p = rv;
4640 return replacing_p;
4643 /* Value is the position of the end of the `display' property starting
4644 at START_POS in OBJECT. */
4646 static struct text_pos
4647 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4649 Lisp_Object end;
4650 struct text_pos end_pos;
4652 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4653 Qdisplay, object, Qnil);
4654 CHARPOS (end_pos) = XFASTINT (end);
4655 if (STRINGP (object))
4656 compute_string_pos (&end_pos, start_pos, it->string);
4657 else
4658 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4660 return end_pos;
4664 /* Set up IT from a single `display' property specification SPEC. OBJECT
4665 is the object in which the `display' property was found. *POSITION
4666 is the position in OBJECT at which the `display' property was found.
4667 BUFPOS is the buffer position of OBJECT (different from POSITION if
4668 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4669 previously saw a display specification which already replaced text
4670 display with something else, for example an image; we ignore such
4671 properties after the first one has been processed.
4673 OVERLAY is the overlay this `display' property came from,
4674 or nil if it was a text property.
4676 If SPEC is a `space' or `image' specification, and in some other
4677 cases too, set *POSITION to the position where the `display'
4678 property ends.
4680 If IT is NULL, only examine the property specification in SPEC, but
4681 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4682 is intended to be displayed in a window on a GUI frame.
4684 Value is non-zero if something was found which replaces the display
4685 of buffer or string text. */
4687 static int
4688 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4689 Lisp_Object overlay, struct text_pos *position,
4690 ptrdiff_t bufpos, int display_replaced_p,
4691 int frame_window_p)
4693 Lisp_Object form;
4694 Lisp_Object location, value;
4695 struct text_pos start_pos = *position;
4696 int valid_p;
4698 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4699 If the result is non-nil, use VALUE instead of SPEC. */
4700 form = Qt;
4701 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4703 spec = XCDR (spec);
4704 if (!CONSP (spec))
4705 return 0;
4706 form = XCAR (spec);
4707 spec = XCDR (spec);
4710 if (!NILP (form) && !EQ (form, Qt))
4712 ptrdiff_t count = SPECPDL_INDEX ();
4713 struct gcpro gcpro1;
4715 /* Bind `object' to the object having the `display' property, a
4716 buffer or string. Bind `position' to the position in the
4717 object where the property was found, and `buffer-position'
4718 to the current position in the buffer. */
4720 if (NILP (object))
4721 XSETBUFFER (object, current_buffer);
4722 specbind (Qobject, object);
4723 specbind (Qposition, make_number (CHARPOS (*position)));
4724 specbind (Qbuffer_position, make_number (bufpos));
4725 GCPRO1 (form);
4726 form = safe_eval (form);
4727 UNGCPRO;
4728 unbind_to (count, Qnil);
4731 if (NILP (form))
4732 return 0;
4734 /* Handle `(height HEIGHT)' specifications. */
4735 if (CONSP (spec)
4736 && EQ (XCAR (spec), Qheight)
4737 && CONSP (XCDR (spec)))
4739 if (it)
4741 if (!FRAME_WINDOW_P (it->f))
4742 return 0;
4744 it->font_height = XCAR (XCDR (spec));
4745 if (!NILP (it->font_height))
4747 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4748 int new_height = -1;
4750 if (CONSP (it->font_height)
4751 && (EQ (XCAR (it->font_height), Qplus)
4752 || EQ (XCAR (it->font_height), Qminus))
4753 && CONSP (XCDR (it->font_height))
4754 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4756 /* `(+ N)' or `(- N)' where N is an integer. */
4757 int steps = XINT (XCAR (XCDR (it->font_height)));
4758 if (EQ (XCAR (it->font_height), Qplus))
4759 steps = - steps;
4760 it->face_id = smaller_face (it->f, it->face_id, steps);
4762 else if (FUNCTIONP (it->font_height))
4764 /* Call function with current height as argument.
4765 Value is the new height. */
4766 Lisp_Object height;
4767 height = safe_call1 (it->font_height,
4768 face->lface[LFACE_HEIGHT_INDEX]);
4769 if (NUMBERP (height))
4770 new_height = XFLOATINT (height);
4772 else if (NUMBERP (it->font_height))
4774 /* Value is a multiple of the canonical char height. */
4775 struct face *f;
4777 f = FACE_FROM_ID (it->f,
4778 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4779 new_height = (XFLOATINT (it->font_height)
4780 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4782 else
4784 /* Evaluate IT->font_height with `height' bound to the
4785 current specified height to get the new height. */
4786 ptrdiff_t count = SPECPDL_INDEX ();
4788 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4789 value = safe_eval (it->font_height);
4790 unbind_to (count, Qnil);
4792 if (NUMBERP (value))
4793 new_height = XFLOATINT (value);
4796 if (new_height > 0)
4797 it->face_id = face_with_height (it->f, it->face_id, new_height);
4801 return 0;
4804 /* Handle `(space-width WIDTH)'. */
4805 if (CONSP (spec)
4806 && EQ (XCAR (spec), Qspace_width)
4807 && CONSP (XCDR (spec)))
4809 if (it)
4811 if (!FRAME_WINDOW_P (it->f))
4812 return 0;
4814 value = XCAR (XCDR (spec));
4815 if (NUMBERP (value) && XFLOATINT (value) > 0)
4816 it->space_width = value;
4819 return 0;
4822 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4823 if (CONSP (spec)
4824 && EQ (XCAR (spec), Qslice))
4826 Lisp_Object tem;
4828 if (it)
4830 if (!FRAME_WINDOW_P (it->f))
4831 return 0;
4833 if (tem = XCDR (spec), CONSP (tem))
4835 it->slice.x = XCAR (tem);
4836 if (tem = XCDR (tem), CONSP (tem))
4838 it->slice.y = XCAR (tem);
4839 if (tem = XCDR (tem), CONSP (tem))
4841 it->slice.width = XCAR (tem);
4842 if (tem = XCDR (tem), CONSP (tem))
4843 it->slice.height = XCAR (tem);
4849 return 0;
4852 /* Handle `(raise FACTOR)'. */
4853 if (CONSP (spec)
4854 && EQ (XCAR (spec), Qraise)
4855 && CONSP (XCDR (spec)))
4857 if (it)
4859 if (!FRAME_WINDOW_P (it->f))
4860 return 0;
4862 #ifdef HAVE_WINDOW_SYSTEM
4863 value = XCAR (XCDR (spec));
4864 if (NUMBERP (value))
4866 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4867 it->voffset = - (XFLOATINT (value)
4868 * (FONT_HEIGHT (face->font)));
4870 #endif /* HAVE_WINDOW_SYSTEM */
4873 return 0;
4876 /* Don't handle the other kinds of display specifications
4877 inside a string that we got from a `display' property. */
4878 if (it && it->string_from_display_prop_p)
4879 return 0;
4881 /* Characters having this form of property are not displayed, so
4882 we have to find the end of the property. */
4883 if (it)
4885 start_pos = *position;
4886 *position = display_prop_end (it, object, start_pos);
4888 value = Qnil;
4890 /* Stop the scan at that end position--we assume that all
4891 text properties change there. */
4892 if (it)
4893 it->stop_charpos = position->charpos;
4895 /* Handle `(left-fringe BITMAP [FACE])'
4896 and `(right-fringe BITMAP [FACE])'. */
4897 if (CONSP (spec)
4898 && (EQ (XCAR (spec), Qleft_fringe)
4899 || EQ (XCAR (spec), Qright_fringe))
4900 && CONSP (XCDR (spec)))
4902 int fringe_bitmap;
4904 if (it)
4906 if (!FRAME_WINDOW_P (it->f))
4907 /* If we return here, POSITION has been advanced
4908 across the text with this property. */
4910 /* Synchronize the bidi iterator with POSITION. This is
4911 needed because we are not going to push the iterator
4912 on behalf of this display property, so there will be
4913 no pop_it call to do this synchronization for us. */
4914 if (it->bidi_p)
4916 it->position = *position;
4917 iterate_out_of_display_property (it);
4918 *position = it->position;
4920 return 1;
4923 else if (!frame_window_p)
4924 return 1;
4926 #ifdef HAVE_WINDOW_SYSTEM
4927 value = XCAR (XCDR (spec));
4928 if (!SYMBOLP (value)
4929 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4930 /* If we return here, POSITION has been advanced
4931 across the text with this property. */
4933 if (it && it->bidi_p)
4935 it->position = *position;
4936 iterate_out_of_display_property (it);
4937 *position = it->position;
4939 return 1;
4942 if (it)
4944 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4946 if (CONSP (XCDR (XCDR (spec))))
4948 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4949 int face_id2 = lookup_derived_face (it->f, face_name,
4950 FRINGE_FACE_ID, 0);
4951 if (face_id2 >= 0)
4952 face_id = face_id2;
4955 /* Save current settings of IT so that we can restore them
4956 when we are finished with the glyph property value. */
4957 push_it (it, position);
4959 it->area = TEXT_AREA;
4960 it->what = IT_IMAGE;
4961 it->image_id = -1; /* no image */
4962 it->position = start_pos;
4963 it->object = NILP (object) ? it->w->contents : object;
4964 it->method = GET_FROM_IMAGE;
4965 it->from_overlay = Qnil;
4966 it->face_id = face_id;
4967 it->from_disp_prop_p = 1;
4969 /* Say that we haven't consumed the characters with
4970 `display' property yet. The call to pop_it in
4971 set_iterator_to_next will clean this up. */
4972 *position = start_pos;
4974 if (EQ (XCAR (spec), Qleft_fringe))
4976 it->left_user_fringe_bitmap = fringe_bitmap;
4977 it->left_user_fringe_face_id = face_id;
4979 else
4981 it->right_user_fringe_bitmap = fringe_bitmap;
4982 it->right_user_fringe_face_id = face_id;
4985 #endif /* HAVE_WINDOW_SYSTEM */
4986 return 1;
4989 /* Prepare to handle `((margin left-margin) ...)',
4990 `((margin right-margin) ...)' and `((margin nil) ...)'
4991 prefixes for display specifications. */
4992 location = Qunbound;
4993 if (CONSP (spec) && CONSP (XCAR (spec)))
4995 Lisp_Object tem;
4997 value = XCDR (spec);
4998 if (CONSP (value))
4999 value = XCAR (value);
5001 tem = XCAR (spec);
5002 if (EQ (XCAR (tem), Qmargin)
5003 && (tem = XCDR (tem),
5004 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5005 (NILP (tem)
5006 || EQ (tem, Qleft_margin)
5007 || EQ (tem, Qright_margin))))
5008 location = tem;
5011 if (EQ (location, Qunbound))
5013 location = Qnil;
5014 value = spec;
5017 /* After this point, VALUE is the property after any
5018 margin prefix has been stripped. It must be a string,
5019 an image specification, or `(space ...)'.
5021 LOCATION specifies where to display: `left-margin',
5022 `right-margin' or nil. */
5024 valid_p = (STRINGP (value)
5025 #ifdef HAVE_WINDOW_SYSTEM
5026 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5027 && valid_image_p (value))
5028 #endif /* not HAVE_WINDOW_SYSTEM */
5029 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5031 if (valid_p && !display_replaced_p)
5033 int retval = 1;
5035 if (!it)
5037 /* Callers need to know whether the display spec is any kind
5038 of `(space ...)' spec that is about to affect text-area
5039 display. */
5040 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5041 retval = 2;
5042 return retval;
5045 /* Save current settings of IT so that we can restore them
5046 when we are finished with the glyph property value. */
5047 push_it (it, position);
5048 it->from_overlay = overlay;
5049 it->from_disp_prop_p = 1;
5051 if (NILP (location))
5052 it->area = TEXT_AREA;
5053 else if (EQ (location, Qleft_margin))
5054 it->area = LEFT_MARGIN_AREA;
5055 else
5056 it->area = RIGHT_MARGIN_AREA;
5058 if (STRINGP (value))
5060 it->string = value;
5061 it->multibyte_p = STRING_MULTIBYTE (it->string);
5062 it->current.overlay_string_index = -1;
5063 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5064 it->end_charpos = it->string_nchars = SCHARS (it->string);
5065 it->method = GET_FROM_STRING;
5066 it->stop_charpos = 0;
5067 it->prev_stop = 0;
5068 it->base_level_stop = 0;
5069 it->string_from_display_prop_p = 1;
5070 /* Say that we haven't consumed the characters with
5071 `display' property yet. The call to pop_it in
5072 set_iterator_to_next will clean this up. */
5073 if (BUFFERP (object))
5074 *position = start_pos;
5076 /* Force paragraph direction to be that of the parent
5077 object. If the parent object's paragraph direction is
5078 not yet determined, default to L2R. */
5079 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5080 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5081 else
5082 it->paragraph_embedding = L2R;
5084 /* Set up the bidi iterator for this display string. */
5085 if (it->bidi_p)
5087 it->bidi_it.string.lstring = it->string;
5088 it->bidi_it.string.s = NULL;
5089 it->bidi_it.string.schars = it->end_charpos;
5090 it->bidi_it.string.bufpos = bufpos;
5091 it->bidi_it.string.from_disp_str = 1;
5092 it->bidi_it.string.unibyte = !it->multibyte_p;
5093 it->bidi_it.w = it->w;
5094 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5097 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5099 it->method = GET_FROM_STRETCH;
5100 it->object = value;
5101 *position = it->position = start_pos;
5102 retval = 1 + (it->area == TEXT_AREA);
5104 #ifdef HAVE_WINDOW_SYSTEM
5105 else
5107 it->what = IT_IMAGE;
5108 it->image_id = lookup_image (it->f, value);
5109 it->position = start_pos;
5110 it->object = NILP (object) ? it->w->contents : object;
5111 it->method = GET_FROM_IMAGE;
5113 /* Say that we haven't consumed the characters with
5114 `display' property yet. The call to pop_it in
5115 set_iterator_to_next will clean this up. */
5116 *position = start_pos;
5118 #endif /* HAVE_WINDOW_SYSTEM */
5120 return retval;
5123 /* Invalid property or property not supported. Restore
5124 POSITION to what it was before. */
5125 *position = start_pos;
5126 return 0;
5129 /* Check if PROP is a display property value whose text should be
5130 treated as intangible. OVERLAY is the overlay from which PROP
5131 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5132 specify the buffer position covered by PROP. */
5135 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5136 ptrdiff_t charpos, ptrdiff_t bytepos)
5138 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5139 struct text_pos position;
5141 SET_TEXT_POS (position, charpos, bytepos);
5142 return handle_display_spec (NULL, prop, Qnil, overlay,
5143 &position, charpos, frame_window_p);
5147 /* Return 1 if PROP is a display sub-property value containing STRING.
5149 Implementation note: this and the following function are really
5150 special cases of handle_display_spec and
5151 handle_single_display_spec, and should ideally use the same code.
5152 Until they do, these two pairs must be consistent and must be
5153 modified in sync. */
5155 static int
5156 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5158 if (EQ (string, prop))
5159 return 1;
5161 /* Skip over `when FORM'. */
5162 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5164 prop = XCDR (prop);
5165 if (!CONSP (prop))
5166 return 0;
5167 /* Actually, the condition following `when' should be eval'ed,
5168 like handle_single_display_spec does, and we should return
5169 zero if it evaluates to nil. However, this function is
5170 called only when the buffer was already displayed and some
5171 glyph in the glyph matrix was found to come from a display
5172 string. Therefore, the condition was already evaluated, and
5173 the result was non-nil, otherwise the display string wouldn't
5174 have been displayed and we would have never been called for
5175 this property. Thus, we can skip the evaluation and assume
5176 its result is non-nil. */
5177 prop = XCDR (prop);
5180 if (CONSP (prop))
5181 /* Skip over `margin LOCATION'. */
5182 if (EQ (XCAR (prop), Qmargin))
5184 prop = XCDR (prop);
5185 if (!CONSP (prop))
5186 return 0;
5188 prop = XCDR (prop);
5189 if (!CONSP (prop))
5190 return 0;
5193 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5197 /* Return 1 if STRING appears in the `display' property PROP. */
5199 static int
5200 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5202 if (CONSP (prop)
5203 && !EQ (XCAR (prop), Qwhen)
5204 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5206 /* A list of sub-properties. */
5207 while (CONSP (prop))
5209 if (single_display_spec_string_p (XCAR (prop), string))
5210 return 1;
5211 prop = XCDR (prop);
5214 else if (VECTORP (prop))
5216 /* A vector of sub-properties. */
5217 ptrdiff_t i;
5218 for (i = 0; i < ASIZE (prop); ++i)
5219 if (single_display_spec_string_p (AREF (prop, i), string))
5220 return 1;
5222 else
5223 return single_display_spec_string_p (prop, string);
5225 return 0;
5228 /* Look for STRING in overlays and text properties in the current
5229 buffer, between character positions FROM and TO (excluding TO).
5230 BACK_P non-zero means look back (in this case, TO is supposed to be
5231 less than FROM).
5232 Value is the first character position where STRING was found, or
5233 zero if it wasn't found before hitting TO.
5235 This function may only use code that doesn't eval because it is
5236 called asynchronously from note_mouse_highlight. */
5238 static ptrdiff_t
5239 string_buffer_position_lim (Lisp_Object string,
5240 ptrdiff_t from, ptrdiff_t to, int back_p)
5242 Lisp_Object limit, prop, pos;
5243 int found = 0;
5245 pos = make_number (max (from, BEGV));
5247 if (!back_p) /* looking forward */
5249 limit = make_number (min (to, ZV));
5250 while (!found && !EQ (pos, limit))
5252 prop = Fget_char_property (pos, Qdisplay, Qnil);
5253 if (!NILP (prop) && display_prop_string_p (prop, string))
5254 found = 1;
5255 else
5256 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5257 limit);
5260 else /* looking back */
5262 limit = make_number (max (to, BEGV));
5263 while (!found && !EQ (pos, limit))
5265 prop = Fget_char_property (pos, Qdisplay, Qnil);
5266 if (!NILP (prop) && display_prop_string_p (prop, string))
5267 found = 1;
5268 else
5269 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5270 limit);
5274 return found ? XINT (pos) : 0;
5277 /* Determine which buffer position in current buffer STRING comes from.
5278 AROUND_CHARPOS is an approximate position where it could come from.
5279 Value is the buffer position or 0 if it couldn't be determined.
5281 This function is necessary because we don't record buffer positions
5282 in glyphs generated from strings (to keep struct glyph small).
5283 This function may only use code that doesn't eval because it is
5284 called asynchronously from note_mouse_highlight. */
5286 static ptrdiff_t
5287 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5289 const int MAX_DISTANCE = 1000;
5290 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5291 around_charpos + MAX_DISTANCE,
5294 if (!found)
5295 found = string_buffer_position_lim (string, around_charpos,
5296 around_charpos - MAX_DISTANCE, 1);
5297 return found;
5302 /***********************************************************************
5303 `composition' property
5304 ***********************************************************************/
5306 /* Set up iterator IT from `composition' property at its current
5307 position. Called from handle_stop. */
5309 static enum prop_handled
5310 handle_composition_prop (struct it *it)
5312 Lisp_Object prop, string;
5313 ptrdiff_t pos, pos_byte, start, end;
5315 if (STRINGP (it->string))
5317 unsigned char *s;
5319 pos = IT_STRING_CHARPOS (*it);
5320 pos_byte = IT_STRING_BYTEPOS (*it);
5321 string = it->string;
5322 s = SDATA (string) + pos_byte;
5323 it->c = STRING_CHAR (s);
5325 else
5327 pos = IT_CHARPOS (*it);
5328 pos_byte = IT_BYTEPOS (*it);
5329 string = Qnil;
5330 it->c = FETCH_CHAR (pos_byte);
5333 /* If there's a valid composition and point is not inside of the
5334 composition (in the case that the composition is from the current
5335 buffer), draw a glyph composed from the composition components. */
5336 if (find_composition (pos, -1, &start, &end, &prop, string)
5337 && composition_valid_p (start, end, prop)
5338 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5340 if (start < pos)
5341 /* As we can't handle this situation (perhaps font-lock added
5342 a new composition), we just return here hoping that next
5343 redisplay will detect this composition much earlier. */
5344 return HANDLED_NORMALLY;
5345 if (start != pos)
5347 if (STRINGP (it->string))
5348 pos_byte = string_char_to_byte (it->string, start);
5349 else
5350 pos_byte = CHAR_TO_BYTE (start);
5352 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5353 prop, string);
5355 if (it->cmp_it.id >= 0)
5357 it->cmp_it.ch = -1;
5358 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5359 it->cmp_it.nglyphs = -1;
5363 return HANDLED_NORMALLY;
5368 /***********************************************************************
5369 Overlay strings
5370 ***********************************************************************/
5372 /* The following structure is used to record overlay strings for
5373 later sorting in load_overlay_strings. */
5375 struct overlay_entry
5377 Lisp_Object overlay;
5378 Lisp_Object string;
5379 EMACS_INT priority;
5380 int after_string_p;
5384 /* Set up iterator IT from overlay strings at its current position.
5385 Called from handle_stop. */
5387 static enum prop_handled
5388 handle_overlay_change (struct it *it)
5390 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5391 return HANDLED_RECOMPUTE_PROPS;
5392 else
5393 return HANDLED_NORMALLY;
5397 /* Set up the next overlay string for delivery by IT, if there is an
5398 overlay string to deliver. Called by set_iterator_to_next when the
5399 end of the current overlay string is reached. If there are more
5400 overlay strings to display, IT->string and
5401 IT->current.overlay_string_index are set appropriately here.
5402 Otherwise IT->string is set to nil. */
5404 static void
5405 next_overlay_string (struct it *it)
5407 ++it->current.overlay_string_index;
5408 if (it->current.overlay_string_index == it->n_overlay_strings)
5410 /* No more overlay strings. Restore IT's settings to what
5411 they were before overlay strings were processed, and
5412 continue to deliver from current_buffer. */
5414 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5415 pop_it (it);
5416 eassert (it->sp > 0
5417 || (NILP (it->string)
5418 && it->method == GET_FROM_BUFFER
5419 && it->stop_charpos >= BEGV
5420 && it->stop_charpos <= it->end_charpos));
5421 it->current.overlay_string_index = -1;
5422 it->n_overlay_strings = 0;
5423 it->overlay_strings_charpos = -1;
5424 /* If there's an empty display string on the stack, pop the
5425 stack, to resync the bidi iterator with IT's position. Such
5426 empty strings are pushed onto the stack in
5427 get_overlay_strings_1. */
5428 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5429 pop_it (it);
5431 /* If we're at the end of the buffer, record that we have
5432 processed the overlay strings there already, so that
5433 next_element_from_buffer doesn't try it again. */
5434 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5435 it->overlay_strings_at_end_processed_p = 1;
5437 else
5439 /* There are more overlay strings to process. If
5440 IT->current.overlay_string_index has advanced to a position
5441 where we must load IT->overlay_strings with more strings, do
5442 it. We must load at the IT->overlay_strings_charpos where
5443 IT->n_overlay_strings was originally computed; when invisible
5444 text is present, this might not be IT_CHARPOS (Bug#7016). */
5445 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5447 if (it->current.overlay_string_index && i == 0)
5448 load_overlay_strings (it, it->overlay_strings_charpos);
5450 /* Initialize IT to deliver display elements from the overlay
5451 string. */
5452 it->string = it->overlay_strings[i];
5453 it->multibyte_p = STRING_MULTIBYTE (it->string);
5454 SET_TEXT_POS (it->current.string_pos, 0, 0);
5455 it->method = GET_FROM_STRING;
5456 it->stop_charpos = 0;
5457 it->end_charpos = SCHARS (it->string);
5458 if (it->cmp_it.stop_pos >= 0)
5459 it->cmp_it.stop_pos = 0;
5460 it->prev_stop = 0;
5461 it->base_level_stop = 0;
5463 /* Set up the bidi iterator for this overlay string. */
5464 if (it->bidi_p)
5466 it->bidi_it.string.lstring = it->string;
5467 it->bidi_it.string.s = NULL;
5468 it->bidi_it.string.schars = SCHARS (it->string);
5469 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5470 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5471 it->bidi_it.string.unibyte = !it->multibyte_p;
5472 it->bidi_it.w = it->w;
5473 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5477 CHECK_IT (it);
5481 /* Compare two overlay_entry structures E1 and E2. Used as a
5482 comparison function for qsort in load_overlay_strings. Overlay
5483 strings for the same position are sorted so that
5485 1. All after-strings come in front of before-strings, except
5486 when they come from the same overlay.
5488 2. Within after-strings, strings are sorted so that overlay strings
5489 from overlays with higher priorities come first.
5491 2. Within before-strings, strings are sorted so that overlay
5492 strings from overlays with higher priorities come last.
5494 Value is analogous to strcmp. */
5497 static int
5498 compare_overlay_entries (const void *e1, const void *e2)
5500 struct overlay_entry const *entry1 = e1;
5501 struct overlay_entry const *entry2 = e2;
5502 int result;
5504 if (entry1->after_string_p != entry2->after_string_p)
5506 /* Let after-strings appear in front of before-strings if
5507 they come from different overlays. */
5508 if (EQ (entry1->overlay, entry2->overlay))
5509 result = entry1->after_string_p ? 1 : -1;
5510 else
5511 result = entry1->after_string_p ? -1 : 1;
5513 else if (entry1->priority != entry2->priority)
5515 if (entry1->after_string_p)
5516 /* After-strings sorted in order of decreasing priority. */
5517 result = entry2->priority < entry1->priority ? -1 : 1;
5518 else
5519 /* Before-strings sorted in order of increasing priority. */
5520 result = entry1->priority < entry2->priority ? -1 : 1;
5522 else
5523 result = 0;
5525 return result;
5529 /* Load the vector IT->overlay_strings with overlay strings from IT's
5530 current buffer position, or from CHARPOS if that is > 0. Set
5531 IT->n_overlays to the total number of overlay strings found.
5533 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5534 a time. On entry into load_overlay_strings,
5535 IT->current.overlay_string_index gives the number of overlay
5536 strings that have already been loaded by previous calls to this
5537 function.
5539 IT->add_overlay_start contains an additional overlay start
5540 position to consider for taking overlay strings from, if non-zero.
5541 This position comes into play when the overlay has an `invisible'
5542 property, and both before and after-strings. When we've skipped to
5543 the end of the overlay, because of its `invisible' property, we
5544 nevertheless want its before-string to appear.
5545 IT->add_overlay_start will contain the overlay start position
5546 in this case.
5548 Overlay strings are sorted so that after-string strings come in
5549 front of before-string strings. Within before and after-strings,
5550 strings are sorted by overlay priority. See also function
5551 compare_overlay_entries. */
5553 static void
5554 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5556 Lisp_Object overlay, window, str, invisible;
5557 struct Lisp_Overlay *ov;
5558 ptrdiff_t start, end;
5559 ptrdiff_t size = 20;
5560 ptrdiff_t n = 0, i, j;
5561 int invis_p;
5562 struct overlay_entry *entries = alloca (size * sizeof *entries);
5563 USE_SAFE_ALLOCA;
5565 if (charpos <= 0)
5566 charpos = IT_CHARPOS (*it);
5568 /* Append the overlay string STRING of overlay OVERLAY to vector
5569 `entries' which has size `size' and currently contains `n'
5570 elements. AFTER_P non-zero means STRING is an after-string of
5571 OVERLAY. */
5572 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5573 do \
5575 Lisp_Object priority; \
5577 if (n == size) \
5579 struct overlay_entry *old = entries; \
5580 SAFE_NALLOCA (entries, 2, size); \
5581 memcpy (entries, old, size * sizeof *entries); \
5582 size *= 2; \
5585 entries[n].string = (STRING); \
5586 entries[n].overlay = (OVERLAY); \
5587 priority = Foverlay_get ((OVERLAY), Qpriority); \
5588 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5589 entries[n].after_string_p = (AFTER_P); \
5590 ++n; \
5592 while (0)
5594 /* Process overlay before the overlay center. */
5595 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5597 XSETMISC (overlay, ov);
5598 eassert (OVERLAYP (overlay));
5599 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5600 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5602 if (end < charpos)
5603 break;
5605 /* Skip this overlay if it doesn't start or end at IT's current
5606 position. */
5607 if (end != charpos && start != charpos)
5608 continue;
5610 /* Skip this overlay if it doesn't apply to IT->w. */
5611 window = Foverlay_get (overlay, Qwindow);
5612 if (WINDOWP (window) && XWINDOW (window) != it->w)
5613 continue;
5615 /* If the text ``under'' the overlay is invisible, both before-
5616 and after-strings from this overlay are visible; start and
5617 end position are indistinguishable. */
5618 invisible = Foverlay_get (overlay, Qinvisible);
5619 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5621 /* If overlay has a non-empty before-string, record it. */
5622 if ((start == charpos || (end == charpos && invis_p))
5623 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5624 && SCHARS (str))
5625 RECORD_OVERLAY_STRING (overlay, str, 0);
5627 /* If overlay has a non-empty after-string, record it. */
5628 if ((end == charpos || (start == charpos && invis_p))
5629 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5630 && SCHARS (str))
5631 RECORD_OVERLAY_STRING (overlay, str, 1);
5634 /* Process overlays after the overlay center. */
5635 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5637 XSETMISC (overlay, ov);
5638 eassert (OVERLAYP (overlay));
5639 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5640 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5642 if (start > charpos)
5643 break;
5645 /* Skip this overlay if it doesn't start or end at IT's current
5646 position. */
5647 if (end != charpos && start != charpos)
5648 continue;
5650 /* Skip this overlay if it doesn't apply to IT->w. */
5651 window = Foverlay_get (overlay, Qwindow);
5652 if (WINDOWP (window) && XWINDOW (window) != it->w)
5653 continue;
5655 /* If the text ``under'' the overlay is invisible, it has a zero
5656 dimension, and both before- and after-strings apply. */
5657 invisible = Foverlay_get (overlay, Qinvisible);
5658 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5660 /* If overlay has a non-empty before-string, record it. */
5661 if ((start == charpos || (end == charpos && invis_p))
5662 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5663 && SCHARS (str))
5664 RECORD_OVERLAY_STRING (overlay, str, 0);
5666 /* If overlay has a non-empty after-string, record it. */
5667 if ((end == charpos || (start == charpos && invis_p))
5668 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5669 && SCHARS (str))
5670 RECORD_OVERLAY_STRING (overlay, str, 1);
5673 #undef RECORD_OVERLAY_STRING
5675 /* Sort entries. */
5676 if (n > 1)
5677 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5679 /* Record number of overlay strings, and where we computed it. */
5680 it->n_overlay_strings = n;
5681 it->overlay_strings_charpos = charpos;
5683 /* IT->current.overlay_string_index is the number of overlay strings
5684 that have already been consumed by IT. Copy some of the
5685 remaining overlay strings to IT->overlay_strings. */
5686 i = 0;
5687 j = it->current.overlay_string_index;
5688 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5690 it->overlay_strings[i] = entries[j].string;
5691 it->string_overlays[i++] = entries[j++].overlay;
5694 CHECK_IT (it);
5695 SAFE_FREE ();
5699 /* Get the first chunk of overlay strings at IT's current buffer
5700 position, or at CHARPOS if that is > 0. Value is non-zero if at
5701 least one overlay string was found. */
5703 static int
5704 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5706 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5707 process. This fills IT->overlay_strings with strings, and sets
5708 IT->n_overlay_strings to the total number of strings to process.
5709 IT->pos.overlay_string_index has to be set temporarily to zero
5710 because load_overlay_strings needs this; it must be set to -1
5711 when no overlay strings are found because a zero value would
5712 indicate a position in the first overlay string. */
5713 it->current.overlay_string_index = 0;
5714 load_overlay_strings (it, charpos);
5716 /* If we found overlay strings, set up IT to deliver display
5717 elements from the first one. Otherwise set up IT to deliver
5718 from current_buffer. */
5719 if (it->n_overlay_strings)
5721 /* Make sure we know settings in current_buffer, so that we can
5722 restore meaningful values when we're done with the overlay
5723 strings. */
5724 if (compute_stop_p)
5725 compute_stop_pos (it);
5726 eassert (it->face_id >= 0);
5728 /* Save IT's settings. They are restored after all overlay
5729 strings have been processed. */
5730 eassert (!compute_stop_p || it->sp == 0);
5732 /* When called from handle_stop, there might be an empty display
5733 string loaded. In that case, don't bother saving it. But
5734 don't use this optimization with the bidi iterator, since we
5735 need the corresponding pop_it call to resync the bidi
5736 iterator's position with IT's position, after we are done
5737 with the overlay strings. (The corresponding call to pop_it
5738 in case of an empty display string is in
5739 next_overlay_string.) */
5740 if (!(!it->bidi_p
5741 && STRINGP (it->string) && !SCHARS (it->string)))
5742 push_it (it, NULL);
5744 /* Set up IT to deliver display elements from the first overlay
5745 string. */
5746 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5747 it->string = it->overlay_strings[0];
5748 it->from_overlay = Qnil;
5749 it->stop_charpos = 0;
5750 eassert (STRINGP (it->string));
5751 it->end_charpos = SCHARS (it->string);
5752 it->prev_stop = 0;
5753 it->base_level_stop = 0;
5754 it->multibyte_p = STRING_MULTIBYTE (it->string);
5755 it->method = GET_FROM_STRING;
5756 it->from_disp_prop_p = 0;
5758 /* Force paragraph direction to be that of the parent
5759 buffer. */
5760 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5761 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5762 else
5763 it->paragraph_embedding = L2R;
5765 /* Set up the bidi iterator for this overlay string. */
5766 if (it->bidi_p)
5768 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5770 it->bidi_it.string.lstring = it->string;
5771 it->bidi_it.string.s = NULL;
5772 it->bidi_it.string.schars = SCHARS (it->string);
5773 it->bidi_it.string.bufpos = pos;
5774 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5775 it->bidi_it.string.unibyte = !it->multibyte_p;
5776 it->bidi_it.w = it->w;
5777 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5779 return 1;
5782 it->current.overlay_string_index = -1;
5783 return 0;
5786 static int
5787 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5789 it->string = Qnil;
5790 it->method = GET_FROM_BUFFER;
5792 (void) get_overlay_strings_1 (it, charpos, 1);
5794 CHECK_IT (it);
5796 /* Value is non-zero if we found at least one overlay string. */
5797 return STRINGP (it->string);
5802 /***********************************************************************
5803 Saving and restoring state
5804 ***********************************************************************/
5806 /* Save current settings of IT on IT->stack. Called, for example,
5807 before setting up IT for an overlay string, to be able to restore
5808 IT's settings to what they were after the overlay string has been
5809 processed. If POSITION is non-NULL, it is the position to save on
5810 the stack instead of IT->position. */
5812 static void
5813 push_it (struct it *it, struct text_pos *position)
5815 struct iterator_stack_entry *p;
5817 eassert (it->sp < IT_STACK_SIZE);
5818 p = it->stack + it->sp;
5820 p->stop_charpos = it->stop_charpos;
5821 p->prev_stop = it->prev_stop;
5822 p->base_level_stop = it->base_level_stop;
5823 p->cmp_it = it->cmp_it;
5824 eassert (it->face_id >= 0);
5825 p->face_id = it->face_id;
5826 p->string = it->string;
5827 p->method = it->method;
5828 p->from_overlay = it->from_overlay;
5829 switch (p->method)
5831 case GET_FROM_IMAGE:
5832 p->u.image.object = it->object;
5833 p->u.image.image_id = it->image_id;
5834 p->u.image.slice = it->slice;
5835 break;
5836 case GET_FROM_STRETCH:
5837 p->u.stretch.object = it->object;
5838 break;
5840 p->position = position ? *position : it->position;
5841 p->current = it->current;
5842 p->end_charpos = it->end_charpos;
5843 p->string_nchars = it->string_nchars;
5844 p->area = it->area;
5845 p->multibyte_p = it->multibyte_p;
5846 p->avoid_cursor_p = it->avoid_cursor_p;
5847 p->space_width = it->space_width;
5848 p->font_height = it->font_height;
5849 p->voffset = it->voffset;
5850 p->string_from_display_prop_p = it->string_from_display_prop_p;
5851 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5852 p->display_ellipsis_p = 0;
5853 p->line_wrap = it->line_wrap;
5854 p->bidi_p = it->bidi_p;
5855 p->paragraph_embedding = it->paragraph_embedding;
5856 p->from_disp_prop_p = it->from_disp_prop_p;
5857 ++it->sp;
5859 /* Save the state of the bidi iterator as well. */
5860 if (it->bidi_p)
5861 bidi_push_it (&it->bidi_it);
5864 static void
5865 iterate_out_of_display_property (struct it *it)
5867 int buffer_p = !STRINGP (it->string);
5868 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5869 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5871 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5873 /* Maybe initialize paragraph direction. If we are at the beginning
5874 of a new paragraph, next_element_from_buffer may not have a
5875 chance to do that. */
5876 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5877 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5878 /* prev_stop can be zero, so check against BEGV as well. */
5879 while (it->bidi_it.charpos >= bob
5880 && it->prev_stop <= it->bidi_it.charpos
5881 && it->bidi_it.charpos < CHARPOS (it->position)
5882 && it->bidi_it.charpos < eob)
5883 bidi_move_to_visually_next (&it->bidi_it);
5884 /* Record the stop_pos we just crossed, for when we cross it
5885 back, maybe. */
5886 if (it->bidi_it.charpos > CHARPOS (it->position))
5887 it->prev_stop = CHARPOS (it->position);
5888 /* If we ended up not where pop_it put us, resync IT's
5889 positional members with the bidi iterator. */
5890 if (it->bidi_it.charpos != CHARPOS (it->position))
5891 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5892 if (buffer_p)
5893 it->current.pos = it->position;
5894 else
5895 it->current.string_pos = it->position;
5898 /* Restore IT's settings from IT->stack. Called, for example, when no
5899 more overlay strings must be processed, and we return to delivering
5900 display elements from a buffer, or when the end of a string from a
5901 `display' property is reached and we return to delivering display
5902 elements from an overlay string, or from a buffer. */
5904 static void
5905 pop_it (struct it *it)
5907 struct iterator_stack_entry *p;
5908 int from_display_prop = it->from_disp_prop_p;
5910 eassert (it->sp > 0);
5911 --it->sp;
5912 p = it->stack + it->sp;
5913 it->stop_charpos = p->stop_charpos;
5914 it->prev_stop = p->prev_stop;
5915 it->base_level_stop = p->base_level_stop;
5916 it->cmp_it = p->cmp_it;
5917 it->face_id = p->face_id;
5918 it->current = p->current;
5919 it->position = p->position;
5920 it->string = p->string;
5921 it->from_overlay = p->from_overlay;
5922 if (NILP (it->string))
5923 SET_TEXT_POS (it->current.string_pos, -1, -1);
5924 it->method = p->method;
5925 switch (it->method)
5927 case GET_FROM_IMAGE:
5928 it->image_id = p->u.image.image_id;
5929 it->object = p->u.image.object;
5930 it->slice = p->u.image.slice;
5931 break;
5932 case GET_FROM_STRETCH:
5933 it->object = p->u.stretch.object;
5934 break;
5935 case GET_FROM_BUFFER:
5936 it->object = it->w->contents;
5937 break;
5938 case GET_FROM_STRING:
5939 it->object = it->string;
5940 break;
5941 case GET_FROM_DISPLAY_VECTOR:
5942 if (it->s)
5943 it->method = GET_FROM_C_STRING;
5944 else if (STRINGP (it->string))
5945 it->method = GET_FROM_STRING;
5946 else
5948 it->method = GET_FROM_BUFFER;
5949 it->object = it->w->contents;
5952 it->end_charpos = p->end_charpos;
5953 it->string_nchars = p->string_nchars;
5954 it->area = p->area;
5955 it->multibyte_p = p->multibyte_p;
5956 it->avoid_cursor_p = p->avoid_cursor_p;
5957 it->space_width = p->space_width;
5958 it->font_height = p->font_height;
5959 it->voffset = p->voffset;
5960 it->string_from_display_prop_p = p->string_from_display_prop_p;
5961 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5962 it->line_wrap = p->line_wrap;
5963 it->bidi_p = p->bidi_p;
5964 it->paragraph_embedding = p->paragraph_embedding;
5965 it->from_disp_prop_p = p->from_disp_prop_p;
5966 if (it->bidi_p)
5968 bidi_pop_it (&it->bidi_it);
5969 /* Bidi-iterate until we get out of the portion of text, if any,
5970 covered by a `display' text property or by an overlay with
5971 `display' property. (We cannot just jump there, because the
5972 internal coherency of the bidi iterator state can not be
5973 preserved across such jumps.) We also must determine the
5974 paragraph base direction if the overlay we just processed is
5975 at the beginning of a new paragraph. */
5976 if (from_display_prop
5977 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5978 iterate_out_of_display_property (it);
5980 eassert ((BUFFERP (it->object)
5981 && IT_CHARPOS (*it) == it->bidi_it.charpos
5982 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5983 || (STRINGP (it->object)
5984 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5985 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5986 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5992 /***********************************************************************
5993 Moving over lines
5994 ***********************************************************************/
5996 /* Set IT's current position to the previous line start. */
5998 static void
5999 back_to_previous_line_start (struct it *it)
6001 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6003 DEC_BOTH (cp, bp);
6004 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6008 /* Move IT to the next line start.
6010 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6011 we skipped over part of the text (as opposed to moving the iterator
6012 continuously over the text). Otherwise, don't change the value
6013 of *SKIPPED_P.
6015 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6016 iterator on the newline, if it was found.
6018 Newlines may come from buffer text, overlay strings, or strings
6019 displayed via the `display' property. That's the reason we can't
6020 simply use find_newline_no_quit.
6022 Note that this function may not skip over invisible text that is so
6023 because of text properties and immediately follows a newline. If
6024 it would, function reseat_at_next_visible_line_start, when called
6025 from set_iterator_to_next, would effectively make invisible
6026 characters following a newline part of the wrong glyph row, which
6027 leads to wrong cursor motion. */
6029 static int
6030 forward_to_next_line_start (struct it *it, int *skipped_p,
6031 struct bidi_it *bidi_it_prev)
6033 ptrdiff_t old_selective;
6034 int newline_found_p, n;
6035 const int MAX_NEWLINE_DISTANCE = 500;
6037 /* If already on a newline, just consume it to avoid unintended
6038 skipping over invisible text below. */
6039 if (it->what == IT_CHARACTER
6040 && it->c == '\n'
6041 && CHARPOS (it->position) == IT_CHARPOS (*it))
6043 if (it->bidi_p && bidi_it_prev)
6044 *bidi_it_prev = it->bidi_it;
6045 set_iterator_to_next (it, 0);
6046 it->c = 0;
6047 return 1;
6050 /* Don't handle selective display in the following. It's (a)
6051 unnecessary because it's done by the caller, and (b) leads to an
6052 infinite recursion because next_element_from_ellipsis indirectly
6053 calls this function. */
6054 old_selective = it->selective;
6055 it->selective = 0;
6057 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6058 from buffer text. */
6059 for (n = newline_found_p = 0;
6060 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6061 n += STRINGP (it->string) ? 0 : 1)
6063 if (!get_next_display_element (it))
6064 return 0;
6065 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6066 if (newline_found_p && it->bidi_p && bidi_it_prev)
6067 *bidi_it_prev = it->bidi_it;
6068 set_iterator_to_next (it, 0);
6071 /* If we didn't find a newline near enough, see if we can use a
6072 short-cut. */
6073 if (!newline_found_p)
6075 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6076 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6077 1, &bytepos);
6078 Lisp_Object pos;
6080 eassert (!STRINGP (it->string));
6082 /* If there isn't any `display' property in sight, and no
6083 overlays, we can just use the position of the newline in
6084 buffer text. */
6085 if (it->stop_charpos >= limit
6086 || ((pos = Fnext_single_property_change (make_number (start),
6087 Qdisplay, Qnil,
6088 make_number (limit)),
6089 NILP (pos))
6090 && next_overlay_change (start) == ZV))
6092 if (!it->bidi_p)
6094 IT_CHARPOS (*it) = limit;
6095 IT_BYTEPOS (*it) = bytepos;
6097 else
6099 struct bidi_it bprev;
6101 /* Help bidi.c avoid expensive searches for display
6102 properties and overlays, by telling it that there are
6103 none up to `limit'. */
6104 if (it->bidi_it.disp_pos < limit)
6106 it->bidi_it.disp_pos = limit;
6107 it->bidi_it.disp_prop = 0;
6109 do {
6110 bprev = it->bidi_it;
6111 bidi_move_to_visually_next (&it->bidi_it);
6112 } while (it->bidi_it.charpos != limit);
6113 IT_CHARPOS (*it) = limit;
6114 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6115 if (bidi_it_prev)
6116 *bidi_it_prev = bprev;
6118 *skipped_p = newline_found_p = 1;
6120 else
6122 while (get_next_display_element (it)
6123 && !newline_found_p)
6125 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6126 if (newline_found_p && it->bidi_p && bidi_it_prev)
6127 *bidi_it_prev = it->bidi_it;
6128 set_iterator_to_next (it, 0);
6133 it->selective = old_selective;
6134 return newline_found_p;
6138 /* Set IT's current position to the previous visible line start. Skip
6139 invisible text that is so either due to text properties or due to
6140 selective display. Caution: this does not change IT->current_x and
6141 IT->hpos. */
6143 static void
6144 back_to_previous_visible_line_start (struct it *it)
6146 while (IT_CHARPOS (*it) > BEGV)
6148 back_to_previous_line_start (it);
6150 if (IT_CHARPOS (*it) <= BEGV)
6151 break;
6153 /* If selective > 0, then lines indented more than its value are
6154 invisible. */
6155 if (it->selective > 0
6156 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6157 it->selective))
6158 continue;
6160 /* Check the newline before point for invisibility. */
6162 Lisp_Object prop;
6163 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6164 Qinvisible, it->window);
6165 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6166 continue;
6169 if (IT_CHARPOS (*it) <= BEGV)
6170 break;
6173 struct it it2;
6174 void *it2data = NULL;
6175 ptrdiff_t pos;
6176 ptrdiff_t beg, end;
6177 Lisp_Object val, overlay;
6179 SAVE_IT (it2, *it, it2data);
6181 /* If newline is part of a composition, continue from start of composition */
6182 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6183 && beg < IT_CHARPOS (*it))
6184 goto replaced;
6186 /* If newline is replaced by a display property, find start of overlay
6187 or interval and continue search from that point. */
6188 pos = --IT_CHARPOS (it2);
6189 --IT_BYTEPOS (it2);
6190 it2.sp = 0;
6191 bidi_unshelve_cache (NULL, 0);
6192 it2.string_from_display_prop_p = 0;
6193 it2.from_disp_prop_p = 0;
6194 if (handle_display_prop (&it2) == HANDLED_RETURN
6195 && !NILP (val = get_char_property_and_overlay
6196 (make_number (pos), Qdisplay, Qnil, &overlay))
6197 && (OVERLAYP (overlay)
6198 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6199 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6201 RESTORE_IT (it, it, it2data);
6202 goto replaced;
6205 /* Newline is not replaced by anything -- so we are done. */
6206 RESTORE_IT (it, it, it2data);
6207 break;
6209 replaced:
6210 if (beg < BEGV)
6211 beg = BEGV;
6212 IT_CHARPOS (*it) = beg;
6213 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6217 it->continuation_lines_width = 0;
6219 eassert (IT_CHARPOS (*it) >= BEGV);
6220 eassert (IT_CHARPOS (*it) == BEGV
6221 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6222 CHECK_IT (it);
6226 /* Reseat iterator IT at the previous visible line start. Skip
6227 invisible text that is so either due to text properties or due to
6228 selective display. At the end, update IT's overlay information,
6229 face information etc. */
6231 void
6232 reseat_at_previous_visible_line_start (struct it *it)
6234 back_to_previous_visible_line_start (it);
6235 reseat (it, it->current.pos, 1);
6236 CHECK_IT (it);
6240 /* Reseat iterator IT on the next visible line start in the current
6241 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6242 preceding the line start. Skip over invisible text that is so
6243 because of selective display. Compute faces, overlays etc at the
6244 new position. Note that this function does not skip over text that
6245 is invisible because of text properties. */
6247 static void
6248 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6250 int newline_found_p, skipped_p = 0;
6251 struct bidi_it bidi_it_prev;
6253 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6255 /* Skip over lines that are invisible because they are indented
6256 more than the value of IT->selective. */
6257 if (it->selective > 0)
6258 while (IT_CHARPOS (*it) < ZV
6259 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6260 it->selective))
6262 eassert (IT_BYTEPOS (*it) == BEGV
6263 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6264 newline_found_p =
6265 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6268 /* Position on the newline if that's what's requested. */
6269 if (on_newline_p && newline_found_p)
6271 if (STRINGP (it->string))
6273 if (IT_STRING_CHARPOS (*it) > 0)
6275 if (!it->bidi_p)
6277 --IT_STRING_CHARPOS (*it);
6278 --IT_STRING_BYTEPOS (*it);
6280 else
6282 /* We need to restore the bidi iterator to the state
6283 it had on the newline, and resync the IT's
6284 position with that. */
6285 it->bidi_it = bidi_it_prev;
6286 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6287 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6291 else if (IT_CHARPOS (*it) > BEGV)
6293 if (!it->bidi_p)
6295 --IT_CHARPOS (*it);
6296 --IT_BYTEPOS (*it);
6298 else
6300 /* We need to restore the bidi iterator to the state it
6301 had on the newline and resync IT with that. */
6302 it->bidi_it = bidi_it_prev;
6303 IT_CHARPOS (*it) = it->bidi_it.charpos;
6304 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6306 reseat (it, it->current.pos, 0);
6309 else if (skipped_p)
6310 reseat (it, it->current.pos, 0);
6312 CHECK_IT (it);
6317 /***********************************************************************
6318 Changing an iterator's position
6319 ***********************************************************************/
6321 /* Change IT's current position to POS in current_buffer. If FORCE_P
6322 is non-zero, always check for text properties at the new position.
6323 Otherwise, text properties are only looked up if POS >=
6324 IT->check_charpos of a property. */
6326 static void
6327 reseat (struct it *it, struct text_pos pos, int force_p)
6329 ptrdiff_t original_pos = IT_CHARPOS (*it);
6331 reseat_1 (it, pos, 0);
6333 /* Determine where to check text properties. Avoid doing it
6334 where possible because text property lookup is very expensive. */
6335 if (force_p
6336 || CHARPOS (pos) > it->stop_charpos
6337 || CHARPOS (pos) < original_pos)
6339 if (it->bidi_p)
6341 /* For bidi iteration, we need to prime prev_stop and
6342 base_level_stop with our best estimations. */
6343 /* Implementation note: Of course, POS is not necessarily a
6344 stop position, so assigning prev_pos to it is a lie; we
6345 should have called compute_stop_backwards. However, if
6346 the current buffer does not include any R2L characters,
6347 that call would be a waste of cycles, because the
6348 iterator will never move back, and thus never cross this
6349 "fake" stop position. So we delay that backward search
6350 until the time we really need it, in next_element_from_buffer. */
6351 if (CHARPOS (pos) != it->prev_stop)
6352 it->prev_stop = CHARPOS (pos);
6353 if (CHARPOS (pos) < it->base_level_stop)
6354 it->base_level_stop = 0; /* meaning it's unknown */
6355 handle_stop (it);
6357 else
6359 handle_stop (it);
6360 it->prev_stop = it->base_level_stop = 0;
6365 CHECK_IT (it);
6369 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6370 IT->stop_pos to POS, also. */
6372 static void
6373 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6375 /* Don't call this function when scanning a C string. */
6376 eassert (it->s == NULL);
6378 /* POS must be a reasonable value. */
6379 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6381 it->current.pos = it->position = pos;
6382 it->end_charpos = ZV;
6383 it->dpvec = NULL;
6384 it->current.dpvec_index = -1;
6385 it->current.overlay_string_index = -1;
6386 IT_STRING_CHARPOS (*it) = -1;
6387 IT_STRING_BYTEPOS (*it) = -1;
6388 it->string = Qnil;
6389 it->method = GET_FROM_BUFFER;
6390 it->object = it->w->contents;
6391 it->area = TEXT_AREA;
6392 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6393 it->sp = 0;
6394 it->string_from_display_prop_p = 0;
6395 it->string_from_prefix_prop_p = 0;
6397 it->from_disp_prop_p = 0;
6398 it->face_before_selective_p = 0;
6399 if (it->bidi_p)
6401 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6402 &it->bidi_it);
6403 bidi_unshelve_cache (NULL, 0);
6404 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6405 it->bidi_it.string.s = NULL;
6406 it->bidi_it.string.lstring = Qnil;
6407 it->bidi_it.string.bufpos = 0;
6408 it->bidi_it.string.unibyte = 0;
6409 it->bidi_it.w = it->w;
6412 if (set_stop_p)
6414 it->stop_charpos = CHARPOS (pos);
6415 it->base_level_stop = CHARPOS (pos);
6417 /* This make the information stored in it->cmp_it invalidate. */
6418 it->cmp_it.id = -1;
6422 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6423 If S is non-null, it is a C string to iterate over. Otherwise,
6424 STRING gives a Lisp string to iterate over.
6426 If PRECISION > 0, don't return more then PRECISION number of
6427 characters from the string.
6429 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6430 characters have been returned. FIELD_WIDTH < 0 means an infinite
6431 field width.
6433 MULTIBYTE = 0 means disable processing of multibyte characters,
6434 MULTIBYTE > 0 means enable it,
6435 MULTIBYTE < 0 means use IT->multibyte_p.
6437 IT must be initialized via a prior call to init_iterator before
6438 calling this function. */
6440 static void
6441 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6442 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6443 int multibyte)
6445 /* No region in strings. */
6446 it->region_beg_charpos = it->region_end_charpos = -1;
6448 /* No text property checks performed by default, but see below. */
6449 it->stop_charpos = -1;
6451 /* Set iterator position and end position. */
6452 memset (&it->current, 0, sizeof it->current);
6453 it->current.overlay_string_index = -1;
6454 it->current.dpvec_index = -1;
6455 eassert (charpos >= 0);
6457 /* If STRING is specified, use its multibyteness, otherwise use the
6458 setting of MULTIBYTE, if specified. */
6459 if (multibyte >= 0)
6460 it->multibyte_p = multibyte > 0;
6462 /* Bidirectional reordering of strings is controlled by the default
6463 value of bidi-display-reordering. Don't try to reorder while
6464 loading loadup.el, as the necessary character property tables are
6465 not yet available. */
6466 it->bidi_p =
6467 NILP (Vpurify_flag)
6468 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6470 if (s == NULL)
6472 eassert (STRINGP (string));
6473 it->string = string;
6474 it->s = NULL;
6475 it->end_charpos = it->string_nchars = SCHARS (string);
6476 it->method = GET_FROM_STRING;
6477 it->current.string_pos = string_pos (charpos, string);
6479 if (it->bidi_p)
6481 it->bidi_it.string.lstring = string;
6482 it->bidi_it.string.s = NULL;
6483 it->bidi_it.string.schars = it->end_charpos;
6484 it->bidi_it.string.bufpos = 0;
6485 it->bidi_it.string.from_disp_str = 0;
6486 it->bidi_it.string.unibyte = !it->multibyte_p;
6487 it->bidi_it.w = it->w;
6488 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6489 FRAME_WINDOW_P (it->f), &it->bidi_it);
6492 else
6494 it->s = (const unsigned char *) s;
6495 it->string = Qnil;
6497 /* Note that we use IT->current.pos, not it->current.string_pos,
6498 for displaying C strings. */
6499 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6500 if (it->multibyte_p)
6502 it->current.pos = c_string_pos (charpos, s, 1);
6503 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6505 else
6507 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6508 it->end_charpos = it->string_nchars = strlen (s);
6511 if (it->bidi_p)
6513 it->bidi_it.string.lstring = Qnil;
6514 it->bidi_it.string.s = (const unsigned char *) s;
6515 it->bidi_it.string.schars = it->end_charpos;
6516 it->bidi_it.string.bufpos = 0;
6517 it->bidi_it.string.from_disp_str = 0;
6518 it->bidi_it.string.unibyte = !it->multibyte_p;
6519 it->bidi_it.w = it->w;
6520 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6521 &it->bidi_it);
6523 it->method = GET_FROM_C_STRING;
6526 /* PRECISION > 0 means don't return more than PRECISION characters
6527 from the string. */
6528 if (precision > 0 && it->end_charpos - charpos > precision)
6530 it->end_charpos = it->string_nchars = charpos + precision;
6531 if (it->bidi_p)
6532 it->bidi_it.string.schars = it->end_charpos;
6535 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6536 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6537 FIELD_WIDTH < 0 means infinite field width. This is useful for
6538 padding with `-' at the end of a mode line. */
6539 if (field_width < 0)
6540 field_width = INFINITY;
6541 /* Implementation note: We deliberately don't enlarge
6542 it->bidi_it.string.schars here to fit it->end_charpos, because
6543 the bidi iterator cannot produce characters out of thin air. */
6544 if (field_width > it->end_charpos - charpos)
6545 it->end_charpos = charpos + field_width;
6547 /* Use the standard display table for displaying strings. */
6548 if (DISP_TABLE_P (Vstandard_display_table))
6549 it->dp = XCHAR_TABLE (Vstandard_display_table);
6551 it->stop_charpos = charpos;
6552 it->prev_stop = charpos;
6553 it->base_level_stop = 0;
6554 if (it->bidi_p)
6556 it->bidi_it.first_elt = 1;
6557 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6558 it->bidi_it.disp_pos = -1;
6560 if (s == NULL && it->multibyte_p)
6562 ptrdiff_t endpos = SCHARS (it->string);
6563 if (endpos > it->end_charpos)
6564 endpos = it->end_charpos;
6565 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6566 it->string);
6568 CHECK_IT (it);
6573 /***********************************************************************
6574 Iteration
6575 ***********************************************************************/
6577 /* Map enum it_method value to corresponding next_element_from_* function. */
6579 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6581 next_element_from_buffer,
6582 next_element_from_display_vector,
6583 next_element_from_string,
6584 next_element_from_c_string,
6585 next_element_from_image,
6586 next_element_from_stretch
6589 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6592 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6593 (possibly with the following characters). */
6595 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6596 ((IT)->cmp_it.id >= 0 \
6597 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6598 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6599 END_CHARPOS, (IT)->w, \
6600 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6601 (IT)->string)))
6604 /* Lookup the char-table Vglyphless_char_display for character C (-1
6605 if we want information for no-font case), and return the display
6606 method symbol. By side-effect, update it->what and
6607 it->glyphless_method. This function is called from
6608 get_next_display_element for each character element, and from
6609 x_produce_glyphs when no suitable font was found. */
6611 Lisp_Object
6612 lookup_glyphless_char_display (int c, struct it *it)
6614 Lisp_Object glyphless_method = Qnil;
6616 if (CHAR_TABLE_P (Vglyphless_char_display)
6617 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6619 if (c >= 0)
6621 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6622 if (CONSP (glyphless_method))
6623 glyphless_method = FRAME_WINDOW_P (it->f)
6624 ? XCAR (glyphless_method)
6625 : XCDR (glyphless_method);
6627 else
6628 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6631 retry:
6632 if (NILP (glyphless_method))
6634 if (c >= 0)
6635 /* The default is to display the character by a proper font. */
6636 return Qnil;
6637 /* The default for the no-font case is to display an empty box. */
6638 glyphless_method = Qempty_box;
6640 if (EQ (glyphless_method, Qzero_width))
6642 if (c >= 0)
6643 return glyphless_method;
6644 /* This method can't be used for the no-font case. */
6645 glyphless_method = Qempty_box;
6647 if (EQ (glyphless_method, Qthin_space))
6648 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6649 else if (EQ (glyphless_method, Qempty_box))
6650 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6651 else if (EQ (glyphless_method, Qhex_code))
6652 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6653 else if (STRINGP (glyphless_method))
6654 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6655 else
6657 /* Invalid value. We use the default method. */
6658 glyphless_method = Qnil;
6659 goto retry;
6661 it->what = IT_GLYPHLESS;
6662 return glyphless_method;
6665 /* Merge escape glyph face and cache the result. */
6667 static struct frame *last_escape_glyph_frame = NULL;
6668 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6669 static int last_escape_glyph_merged_face_id = 0;
6671 static int
6672 merge_escape_glyph_face (struct it *it)
6674 int face_id;
6676 if (it->f == last_escape_glyph_frame
6677 && it->face_id == last_escape_glyph_face_id)
6678 face_id = last_escape_glyph_merged_face_id;
6679 else
6681 /* Merge the `escape-glyph' face into the current face. */
6682 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6683 last_escape_glyph_frame = it->f;
6684 last_escape_glyph_face_id = it->face_id;
6685 last_escape_glyph_merged_face_id = face_id;
6687 return face_id;
6690 /* Likewise for glyphless glyph face. */
6692 static struct frame *last_glyphless_glyph_frame = NULL;
6693 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6694 static int last_glyphless_glyph_merged_face_id = 0;
6697 merge_glyphless_glyph_face (struct it *it)
6699 int face_id;
6701 if (it->f == last_glyphless_glyph_frame
6702 && it->face_id == last_glyphless_glyph_face_id)
6703 face_id = last_glyphless_glyph_merged_face_id;
6704 else
6706 /* Merge the `glyphless-char' face into the current face. */
6707 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6708 last_glyphless_glyph_frame = it->f;
6709 last_glyphless_glyph_face_id = it->face_id;
6710 last_glyphless_glyph_merged_face_id = face_id;
6712 return face_id;
6715 /* Load IT's display element fields with information about the next
6716 display element from the current position of IT. Value is zero if
6717 end of buffer (or C string) is reached. */
6719 static int
6720 get_next_display_element (struct it *it)
6722 /* Non-zero means that we found a display element. Zero means that
6723 we hit the end of what we iterate over. Performance note: the
6724 function pointer `method' used here turns out to be faster than
6725 using a sequence of if-statements. */
6726 int success_p;
6728 get_next:
6729 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6731 if (it->what == IT_CHARACTER)
6733 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6734 and only if (a) the resolved directionality of that character
6735 is R..." */
6736 /* FIXME: Do we need an exception for characters from display
6737 tables? */
6738 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6739 it->c = bidi_mirror_char (it->c);
6740 /* Map via display table or translate control characters.
6741 IT->c, IT->len etc. have been set to the next character by
6742 the function call above. If we have a display table, and it
6743 contains an entry for IT->c, translate it. Don't do this if
6744 IT->c itself comes from a display table, otherwise we could
6745 end up in an infinite recursion. (An alternative could be to
6746 count the recursion depth of this function and signal an
6747 error when a certain maximum depth is reached.) Is it worth
6748 it? */
6749 if (success_p && it->dpvec == NULL)
6751 Lisp_Object dv;
6752 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6753 int nonascii_space_p = 0;
6754 int nonascii_hyphen_p = 0;
6755 int c = it->c; /* This is the character to display. */
6757 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6759 eassert (SINGLE_BYTE_CHAR_P (c));
6760 if (unibyte_display_via_language_environment)
6762 c = DECODE_CHAR (unibyte, c);
6763 if (c < 0)
6764 c = BYTE8_TO_CHAR (it->c);
6766 else
6767 c = BYTE8_TO_CHAR (it->c);
6770 if (it->dp
6771 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6772 VECTORP (dv)))
6774 struct Lisp_Vector *v = XVECTOR (dv);
6776 /* Return the first character from the display table
6777 entry, if not empty. If empty, don't display the
6778 current character. */
6779 if (v->header.size)
6781 it->dpvec_char_len = it->len;
6782 it->dpvec = v->contents;
6783 it->dpend = v->contents + v->header.size;
6784 it->current.dpvec_index = 0;
6785 it->dpvec_face_id = -1;
6786 it->saved_face_id = it->face_id;
6787 it->method = GET_FROM_DISPLAY_VECTOR;
6788 it->ellipsis_p = 0;
6790 else
6792 set_iterator_to_next (it, 0);
6794 goto get_next;
6797 if (! NILP (lookup_glyphless_char_display (c, it)))
6799 if (it->what == IT_GLYPHLESS)
6800 goto done;
6801 /* Don't display this character. */
6802 set_iterator_to_next (it, 0);
6803 goto get_next;
6806 /* If `nobreak-char-display' is non-nil, we display
6807 non-ASCII spaces and hyphens specially. */
6808 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6810 if (c == 0xA0)
6811 nonascii_space_p = 1;
6812 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6813 nonascii_hyphen_p = 1;
6816 /* Translate control characters into `\003' or `^C' form.
6817 Control characters coming from a display table entry are
6818 currently not translated because we use IT->dpvec to hold
6819 the translation. This could easily be changed but I
6820 don't believe that it is worth doing.
6822 The characters handled by `nobreak-char-display' must be
6823 translated too.
6825 Non-printable characters and raw-byte characters are also
6826 translated to octal form. */
6827 if (((c < ' ' || c == 127) /* ASCII control chars */
6828 ? (it->area != TEXT_AREA
6829 /* In mode line, treat \n, \t like other crl chars. */
6830 || (c != '\t'
6831 && it->glyph_row
6832 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6833 || (c != '\n' && c != '\t'))
6834 : (nonascii_space_p
6835 || nonascii_hyphen_p
6836 || CHAR_BYTE8_P (c)
6837 || ! CHAR_PRINTABLE_P (c))))
6839 /* C is a control character, non-ASCII space/hyphen,
6840 raw-byte, or a non-printable character which must be
6841 displayed either as '\003' or as `^C' where the '\\'
6842 and '^' can be defined in the display table. Fill
6843 IT->ctl_chars with glyphs for what we have to
6844 display. Then, set IT->dpvec to these glyphs. */
6845 Lisp_Object gc;
6846 int ctl_len;
6847 int face_id;
6848 int lface_id = 0;
6849 int escape_glyph;
6851 /* Handle control characters with ^. */
6853 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6855 int g;
6857 g = '^'; /* default glyph for Control */
6858 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6859 if (it->dp
6860 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6862 g = GLYPH_CODE_CHAR (gc);
6863 lface_id = GLYPH_CODE_FACE (gc);
6866 face_id = (lface_id
6867 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6868 : merge_escape_glyph_face (it));
6870 XSETINT (it->ctl_chars[0], g);
6871 XSETINT (it->ctl_chars[1], c ^ 0100);
6872 ctl_len = 2;
6873 goto display_control;
6876 /* Handle non-ascii space in the mode where it only gets
6877 highlighting. */
6879 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6881 /* Merge `nobreak-space' into the current face. */
6882 face_id = merge_faces (it->f, Qnobreak_space, 0,
6883 it->face_id);
6884 XSETINT (it->ctl_chars[0], ' ');
6885 ctl_len = 1;
6886 goto display_control;
6889 /* Handle sequences that start with the "escape glyph". */
6891 /* the default escape glyph is \. */
6892 escape_glyph = '\\';
6894 if (it->dp
6895 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6897 escape_glyph = GLYPH_CODE_CHAR (gc);
6898 lface_id = GLYPH_CODE_FACE (gc);
6901 face_id = (lface_id
6902 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6903 : merge_escape_glyph_face (it));
6905 /* Draw non-ASCII hyphen with just highlighting: */
6907 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6909 XSETINT (it->ctl_chars[0], '-');
6910 ctl_len = 1;
6911 goto display_control;
6914 /* Draw non-ASCII space/hyphen with escape glyph: */
6916 if (nonascii_space_p || nonascii_hyphen_p)
6918 XSETINT (it->ctl_chars[0], escape_glyph);
6919 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6920 ctl_len = 2;
6921 goto display_control;
6925 char str[10];
6926 int len, i;
6928 if (CHAR_BYTE8_P (c))
6929 /* Display \200 instead of \17777600. */
6930 c = CHAR_TO_BYTE8 (c);
6931 len = sprintf (str, "%03o", c);
6933 XSETINT (it->ctl_chars[0], escape_glyph);
6934 for (i = 0; i < len; i++)
6935 XSETINT (it->ctl_chars[i + 1], str[i]);
6936 ctl_len = len + 1;
6939 display_control:
6940 /* Set up IT->dpvec and return first character from it. */
6941 it->dpvec_char_len = it->len;
6942 it->dpvec = it->ctl_chars;
6943 it->dpend = it->dpvec + ctl_len;
6944 it->current.dpvec_index = 0;
6945 it->dpvec_face_id = face_id;
6946 it->saved_face_id = it->face_id;
6947 it->method = GET_FROM_DISPLAY_VECTOR;
6948 it->ellipsis_p = 0;
6949 goto get_next;
6951 it->char_to_display = c;
6953 else if (success_p)
6955 it->char_to_display = it->c;
6959 /* Adjust face id for a multibyte character. There are no multibyte
6960 character in unibyte text. */
6961 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6962 && it->multibyte_p
6963 && success_p
6964 && FRAME_WINDOW_P (it->f))
6966 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6968 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6970 /* Automatic composition with glyph-string. */
6971 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6973 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6975 else
6977 ptrdiff_t pos = (it->s ? -1
6978 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6979 : IT_CHARPOS (*it));
6980 int c;
6982 if (it->what == IT_CHARACTER)
6983 c = it->char_to_display;
6984 else
6986 struct composition *cmp = composition_table[it->cmp_it.id];
6987 int i;
6989 c = ' ';
6990 for (i = 0; i < cmp->glyph_len; i++)
6991 /* TAB in a composition means display glyphs with
6992 padding space on the left or right. */
6993 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6994 break;
6996 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7000 done:
7001 /* Is this character the last one of a run of characters with
7002 box? If yes, set IT->end_of_box_run_p to 1. */
7003 if (it->face_box_p
7004 && it->s == NULL)
7006 if (it->method == GET_FROM_STRING && it->sp)
7008 int face_id = underlying_face_id (it);
7009 struct face *face = FACE_FROM_ID (it->f, face_id);
7011 if (face)
7013 if (face->box == FACE_NO_BOX)
7015 /* If the box comes from face properties in a
7016 display string, check faces in that string. */
7017 int string_face_id = face_after_it_pos (it);
7018 it->end_of_box_run_p
7019 = (FACE_FROM_ID (it->f, string_face_id)->box
7020 == FACE_NO_BOX);
7022 /* Otherwise, the box comes from the underlying face.
7023 If this is the last string character displayed, check
7024 the next buffer location. */
7025 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7026 && (it->current.overlay_string_index
7027 == it->n_overlay_strings - 1))
7029 ptrdiff_t ignore;
7030 int next_face_id;
7031 struct text_pos pos = it->current.pos;
7032 INC_TEXT_POS (pos, it->multibyte_p);
7034 next_face_id = face_at_buffer_position
7035 (it->w, CHARPOS (pos), it->region_beg_charpos,
7036 it->region_end_charpos, &ignore,
7037 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7038 -1);
7039 it->end_of_box_run_p
7040 = (FACE_FROM_ID (it->f, next_face_id)->box
7041 == FACE_NO_BOX);
7045 /* next_element_from_display_vector sets this flag according to
7046 faces of the display vector glyphs, see there. */
7047 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7049 int face_id = face_after_it_pos (it);
7050 it->end_of_box_run_p
7051 = (face_id != it->face_id
7052 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7055 /* If we reached the end of the object we've been iterating (e.g., a
7056 display string or an overlay string), and there's something on
7057 IT->stack, proceed with what's on the stack. It doesn't make
7058 sense to return zero if there's unprocessed stuff on the stack,
7059 because otherwise that stuff will never be displayed. */
7060 if (!success_p && it->sp > 0)
7062 set_iterator_to_next (it, 0);
7063 success_p = get_next_display_element (it);
7066 /* Value is 0 if end of buffer or string reached. */
7067 return success_p;
7071 /* Move IT to the next display element.
7073 RESEAT_P non-zero means if called on a newline in buffer text,
7074 skip to the next visible line start.
7076 Functions get_next_display_element and set_iterator_to_next are
7077 separate because I find this arrangement easier to handle than a
7078 get_next_display_element function that also increments IT's
7079 position. The way it is we can first look at an iterator's current
7080 display element, decide whether it fits on a line, and if it does,
7081 increment the iterator position. The other way around we probably
7082 would either need a flag indicating whether the iterator has to be
7083 incremented the next time, or we would have to implement a
7084 decrement position function which would not be easy to write. */
7086 void
7087 set_iterator_to_next (struct it *it, int reseat_p)
7089 /* Reset flags indicating start and end of a sequence of characters
7090 with box. Reset them at the start of this function because
7091 moving the iterator to a new position might set them. */
7092 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7094 switch (it->method)
7096 case GET_FROM_BUFFER:
7097 /* The current display element of IT is a character from
7098 current_buffer. Advance in the buffer, and maybe skip over
7099 invisible lines that are so because of selective display. */
7100 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7101 reseat_at_next_visible_line_start (it, 0);
7102 else if (it->cmp_it.id >= 0)
7104 /* We are currently getting glyphs from a composition. */
7105 int i;
7107 if (! it->bidi_p)
7109 IT_CHARPOS (*it) += it->cmp_it.nchars;
7110 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7111 if (it->cmp_it.to < it->cmp_it.nglyphs)
7113 it->cmp_it.from = it->cmp_it.to;
7115 else
7117 it->cmp_it.id = -1;
7118 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7119 IT_BYTEPOS (*it),
7120 it->end_charpos, Qnil);
7123 else if (! it->cmp_it.reversed_p)
7125 /* Composition created while scanning forward. */
7126 /* Update IT's char/byte positions to point to the first
7127 character of the next grapheme cluster, or to the
7128 character visually after the current composition. */
7129 for (i = 0; i < it->cmp_it.nchars; i++)
7130 bidi_move_to_visually_next (&it->bidi_it);
7131 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7132 IT_CHARPOS (*it) = it->bidi_it.charpos;
7134 if (it->cmp_it.to < it->cmp_it.nglyphs)
7136 /* Proceed to the next grapheme cluster. */
7137 it->cmp_it.from = it->cmp_it.to;
7139 else
7141 /* No more grapheme clusters in this composition.
7142 Find the next stop position. */
7143 ptrdiff_t stop = it->end_charpos;
7144 if (it->bidi_it.scan_dir < 0)
7145 /* Now we are scanning backward and don't know
7146 where to stop. */
7147 stop = -1;
7148 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7149 IT_BYTEPOS (*it), stop, Qnil);
7152 else
7154 /* Composition created while scanning backward. */
7155 /* Update IT's char/byte positions to point to the last
7156 character of the previous grapheme cluster, or the
7157 character visually after the current composition. */
7158 for (i = 0; i < it->cmp_it.nchars; i++)
7159 bidi_move_to_visually_next (&it->bidi_it);
7160 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7161 IT_CHARPOS (*it) = it->bidi_it.charpos;
7162 if (it->cmp_it.from > 0)
7164 /* Proceed to the previous grapheme cluster. */
7165 it->cmp_it.to = it->cmp_it.from;
7167 else
7169 /* No more grapheme clusters in this composition.
7170 Find the next stop position. */
7171 ptrdiff_t stop = it->end_charpos;
7172 if (it->bidi_it.scan_dir < 0)
7173 /* Now we are scanning backward and don't know
7174 where to stop. */
7175 stop = -1;
7176 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7177 IT_BYTEPOS (*it), stop, Qnil);
7181 else
7183 eassert (it->len != 0);
7185 if (!it->bidi_p)
7187 IT_BYTEPOS (*it) += it->len;
7188 IT_CHARPOS (*it) += 1;
7190 else
7192 int prev_scan_dir = it->bidi_it.scan_dir;
7193 /* If this is a new paragraph, determine its base
7194 direction (a.k.a. its base embedding level). */
7195 if (it->bidi_it.new_paragraph)
7196 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7197 bidi_move_to_visually_next (&it->bidi_it);
7198 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7199 IT_CHARPOS (*it) = it->bidi_it.charpos;
7200 if (prev_scan_dir != it->bidi_it.scan_dir)
7202 /* As the scan direction was changed, we must
7203 re-compute the stop position for composition. */
7204 ptrdiff_t stop = it->end_charpos;
7205 if (it->bidi_it.scan_dir < 0)
7206 stop = -1;
7207 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7208 IT_BYTEPOS (*it), stop, Qnil);
7211 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7213 break;
7215 case GET_FROM_C_STRING:
7216 /* Current display element of IT is from a C string. */
7217 if (!it->bidi_p
7218 /* If the string position is beyond string's end, it means
7219 next_element_from_c_string is padding the string with
7220 blanks, in which case we bypass the bidi iterator,
7221 because it cannot deal with such virtual characters. */
7222 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7224 IT_BYTEPOS (*it) += it->len;
7225 IT_CHARPOS (*it) += 1;
7227 else
7229 bidi_move_to_visually_next (&it->bidi_it);
7230 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7231 IT_CHARPOS (*it) = it->bidi_it.charpos;
7233 break;
7235 case GET_FROM_DISPLAY_VECTOR:
7236 /* Current display element of IT is from a display table entry.
7237 Advance in the display table definition. Reset it to null if
7238 end reached, and continue with characters from buffers/
7239 strings. */
7240 ++it->current.dpvec_index;
7242 /* Restore face of the iterator to what they were before the
7243 display vector entry (these entries may contain faces). */
7244 it->face_id = it->saved_face_id;
7246 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7248 int recheck_faces = it->ellipsis_p;
7250 if (it->s)
7251 it->method = GET_FROM_C_STRING;
7252 else if (STRINGP (it->string))
7253 it->method = GET_FROM_STRING;
7254 else
7256 it->method = GET_FROM_BUFFER;
7257 it->object = it->w->contents;
7260 it->dpvec = NULL;
7261 it->current.dpvec_index = -1;
7263 /* Skip over characters which were displayed via IT->dpvec. */
7264 if (it->dpvec_char_len < 0)
7265 reseat_at_next_visible_line_start (it, 1);
7266 else if (it->dpvec_char_len > 0)
7268 if (it->method == GET_FROM_STRING
7269 && it->current.overlay_string_index >= 0
7270 && it->n_overlay_strings > 0)
7271 it->ignore_overlay_strings_at_pos_p = 1;
7272 it->len = it->dpvec_char_len;
7273 set_iterator_to_next (it, reseat_p);
7276 /* Maybe recheck faces after display vector */
7277 if (recheck_faces)
7278 it->stop_charpos = IT_CHARPOS (*it);
7280 break;
7282 case GET_FROM_STRING:
7283 /* Current display element is a character from a Lisp string. */
7284 eassert (it->s == NULL && STRINGP (it->string));
7285 /* Don't advance past string end. These conditions are true
7286 when set_iterator_to_next is called at the end of
7287 get_next_display_element, in which case the Lisp string is
7288 already exhausted, and all we want is pop the iterator
7289 stack. */
7290 if (it->current.overlay_string_index >= 0)
7292 /* This is an overlay string, so there's no padding with
7293 spaces, and the number of characters in the string is
7294 where the string ends. */
7295 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7296 goto consider_string_end;
7298 else
7300 /* Not an overlay string. There could be padding, so test
7301 against it->end_charpos . */
7302 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7303 goto consider_string_end;
7305 if (it->cmp_it.id >= 0)
7307 int i;
7309 if (! it->bidi_p)
7311 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7312 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7313 if (it->cmp_it.to < it->cmp_it.nglyphs)
7314 it->cmp_it.from = it->cmp_it.to;
7315 else
7317 it->cmp_it.id = -1;
7318 composition_compute_stop_pos (&it->cmp_it,
7319 IT_STRING_CHARPOS (*it),
7320 IT_STRING_BYTEPOS (*it),
7321 it->end_charpos, it->string);
7324 else if (! it->cmp_it.reversed_p)
7326 for (i = 0; i < it->cmp_it.nchars; i++)
7327 bidi_move_to_visually_next (&it->bidi_it);
7328 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7329 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7331 if (it->cmp_it.to < it->cmp_it.nglyphs)
7332 it->cmp_it.from = it->cmp_it.to;
7333 else
7335 ptrdiff_t stop = it->end_charpos;
7336 if (it->bidi_it.scan_dir < 0)
7337 stop = -1;
7338 composition_compute_stop_pos (&it->cmp_it,
7339 IT_STRING_CHARPOS (*it),
7340 IT_STRING_BYTEPOS (*it), stop,
7341 it->string);
7344 else
7346 for (i = 0; i < it->cmp_it.nchars; i++)
7347 bidi_move_to_visually_next (&it->bidi_it);
7348 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7349 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7350 if (it->cmp_it.from > 0)
7351 it->cmp_it.to = it->cmp_it.from;
7352 else
7354 ptrdiff_t stop = it->end_charpos;
7355 if (it->bidi_it.scan_dir < 0)
7356 stop = -1;
7357 composition_compute_stop_pos (&it->cmp_it,
7358 IT_STRING_CHARPOS (*it),
7359 IT_STRING_BYTEPOS (*it), stop,
7360 it->string);
7364 else
7366 if (!it->bidi_p
7367 /* If the string position is beyond string's end, it
7368 means next_element_from_string is padding the string
7369 with blanks, in which case we bypass the bidi
7370 iterator, because it cannot deal with such virtual
7371 characters. */
7372 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7374 IT_STRING_BYTEPOS (*it) += it->len;
7375 IT_STRING_CHARPOS (*it) += 1;
7377 else
7379 int prev_scan_dir = it->bidi_it.scan_dir;
7381 bidi_move_to_visually_next (&it->bidi_it);
7382 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7383 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7384 if (prev_scan_dir != it->bidi_it.scan_dir)
7386 ptrdiff_t stop = it->end_charpos;
7388 if (it->bidi_it.scan_dir < 0)
7389 stop = -1;
7390 composition_compute_stop_pos (&it->cmp_it,
7391 IT_STRING_CHARPOS (*it),
7392 IT_STRING_BYTEPOS (*it), stop,
7393 it->string);
7398 consider_string_end:
7400 if (it->current.overlay_string_index >= 0)
7402 /* IT->string is an overlay string. Advance to the
7403 next, if there is one. */
7404 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7406 it->ellipsis_p = 0;
7407 next_overlay_string (it);
7408 if (it->ellipsis_p)
7409 setup_for_ellipsis (it, 0);
7412 else
7414 /* IT->string is not an overlay string. If we reached
7415 its end, and there is something on IT->stack, proceed
7416 with what is on the stack. This can be either another
7417 string, this time an overlay string, or a buffer. */
7418 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7419 && it->sp > 0)
7421 pop_it (it);
7422 if (it->method == GET_FROM_STRING)
7423 goto consider_string_end;
7426 break;
7428 case GET_FROM_IMAGE:
7429 case GET_FROM_STRETCH:
7430 /* The position etc with which we have to proceed are on
7431 the stack. The position may be at the end of a string,
7432 if the `display' property takes up the whole string. */
7433 eassert (it->sp > 0);
7434 pop_it (it);
7435 if (it->method == GET_FROM_STRING)
7436 goto consider_string_end;
7437 break;
7439 default:
7440 /* There are no other methods defined, so this should be a bug. */
7441 emacs_abort ();
7444 eassert (it->method != GET_FROM_STRING
7445 || (STRINGP (it->string)
7446 && IT_STRING_CHARPOS (*it) >= 0));
7449 /* Load IT's display element fields with information about the next
7450 display element which comes from a display table entry or from the
7451 result of translating a control character to one of the forms `^C'
7452 or `\003'.
7454 IT->dpvec holds the glyphs to return as characters.
7455 IT->saved_face_id holds the face id before the display vector--it
7456 is restored into IT->face_id in set_iterator_to_next. */
7458 static int
7459 next_element_from_display_vector (struct it *it)
7461 Lisp_Object gc;
7462 int prev_face_id = it->face_id;
7463 int next_face_id;
7465 /* Precondition. */
7466 eassert (it->dpvec && it->current.dpvec_index >= 0);
7468 it->face_id = it->saved_face_id;
7470 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7471 That seemed totally bogus - so I changed it... */
7472 gc = it->dpvec[it->current.dpvec_index];
7474 if (GLYPH_CODE_P (gc))
7476 struct face *this_face, *prev_face, *next_face;
7478 it->c = GLYPH_CODE_CHAR (gc);
7479 it->len = CHAR_BYTES (it->c);
7481 /* The entry may contain a face id to use. Such a face id is
7482 the id of a Lisp face, not a realized face. A face id of
7483 zero means no face is specified. */
7484 if (it->dpvec_face_id >= 0)
7485 it->face_id = it->dpvec_face_id;
7486 else
7488 int lface_id = GLYPH_CODE_FACE (gc);
7489 if (lface_id > 0)
7490 it->face_id = merge_faces (it->f, Qt, lface_id,
7491 it->saved_face_id);
7494 /* Glyphs in the display vector could have the box face, so we
7495 need to set the related flags in the iterator, as
7496 appropriate. */
7497 this_face = FACE_FROM_ID (it->f, it->face_id);
7498 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7500 /* Is this character the first character of a box-face run? */
7501 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7502 && (!prev_face
7503 || prev_face->box == FACE_NO_BOX));
7505 /* For the last character of the box-face run, we need to look
7506 either at the next glyph from the display vector, or at the
7507 face we saw before the display vector. */
7508 next_face_id = it->saved_face_id;
7509 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7511 if (it->dpvec_face_id >= 0)
7512 next_face_id = it->dpvec_face_id;
7513 else
7515 int lface_id =
7516 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7518 if (lface_id > 0)
7519 next_face_id = merge_faces (it->f, Qt, lface_id,
7520 it->saved_face_id);
7523 next_face = FACE_FROM_ID (it->f, next_face_id);
7524 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7525 && (!next_face
7526 || next_face->box == FACE_NO_BOX));
7527 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7529 else
7530 /* Display table entry is invalid. Return a space. */
7531 it->c = ' ', it->len = 1;
7533 /* Don't change position and object of the iterator here. They are
7534 still the values of the character that had this display table
7535 entry or was translated, and that's what we want. */
7536 it->what = IT_CHARACTER;
7537 return 1;
7540 /* Get the first element of string/buffer in the visual order, after
7541 being reseated to a new position in a string or a buffer. */
7542 static void
7543 get_visually_first_element (struct it *it)
7545 int string_p = STRINGP (it->string) || it->s;
7546 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7547 ptrdiff_t bob = (string_p ? 0 : BEGV);
7549 if (STRINGP (it->string))
7551 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7552 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7554 else
7556 it->bidi_it.charpos = IT_CHARPOS (*it);
7557 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7560 if (it->bidi_it.charpos == eob)
7562 /* Nothing to do, but reset the FIRST_ELT flag, like
7563 bidi_paragraph_init does, because we are not going to
7564 call it. */
7565 it->bidi_it.first_elt = 0;
7567 else if (it->bidi_it.charpos == bob
7568 || (!string_p
7569 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7570 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7572 /* If we are at the beginning of a line/string, we can produce
7573 the next element right away. */
7574 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7575 bidi_move_to_visually_next (&it->bidi_it);
7577 else
7579 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7581 /* We need to prime the bidi iterator starting at the line's or
7582 string's beginning, before we will be able to produce the
7583 next element. */
7584 if (string_p)
7585 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7586 else
7587 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7588 IT_BYTEPOS (*it), -1,
7589 &it->bidi_it.bytepos);
7590 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7593 /* Now return to buffer/string position where we were asked
7594 to get the next display element, and produce that. */
7595 bidi_move_to_visually_next (&it->bidi_it);
7597 while (it->bidi_it.bytepos != orig_bytepos
7598 && it->bidi_it.charpos < eob);
7601 /* Adjust IT's position information to where we ended up. */
7602 if (STRINGP (it->string))
7604 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7605 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7607 else
7609 IT_CHARPOS (*it) = it->bidi_it.charpos;
7610 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7613 if (STRINGP (it->string) || !it->s)
7615 ptrdiff_t stop, charpos, bytepos;
7617 if (STRINGP (it->string))
7619 eassert (!it->s);
7620 stop = SCHARS (it->string);
7621 if (stop > it->end_charpos)
7622 stop = it->end_charpos;
7623 charpos = IT_STRING_CHARPOS (*it);
7624 bytepos = IT_STRING_BYTEPOS (*it);
7626 else
7628 stop = it->end_charpos;
7629 charpos = IT_CHARPOS (*it);
7630 bytepos = IT_BYTEPOS (*it);
7632 if (it->bidi_it.scan_dir < 0)
7633 stop = -1;
7634 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7635 it->string);
7639 /* Load IT with the next display element from Lisp string IT->string.
7640 IT->current.string_pos is the current position within the string.
7641 If IT->current.overlay_string_index >= 0, the Lisp string is an
7642 overlay string. */
7644 static int
7645 next_element_from_string (struct it *it)
7647 struct text_pos position;
7649 eassert (STRINGP (it->string));
7650 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7651 eassert (IT_STRING_CHARPOS (*it) >= 0);
7652 position = it->current.string_pos;
7654 /* With bidi reordering, the character to display might not be the
7655 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7656 that we were reseat()ed to a new string, whose paragraph
7657 direction is not known. */
7658 if (it->bidi_p && it->bidi_it.first_elt)
7660 get_visually_first_element (it);
7661 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7664 /* Time to check for invisible text? */
7665 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7667 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7669 if (!(!it->bidi_p
7670 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7671 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7673 /* With bidi non-linear iteration, we could find
7674 ourselves far beyond the last computed stop_charpos,
7675 with several other stop positions in between that we
7676 missed. Scan them all now, in buffer's logical
7677 order, until we find and handle the last stop_charpos
7678 that precedes our current position. */
7679 handle_stop_backwards (it, it->stop_charpos);
7680 return GET_NEXT_DISPLAY_ELEMENT (it);
7682 else
7684 if (it->bidi_p)
7686 /* Take note of the stop position we just moved
7687 across, for when we will move back across it. */
7688 it->prev_stop = it->stop_charpos;
7689 /* If we are at base paragraph embedding level, take
7690 note of the last stop position seen at this
7691 level. */
7692 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7693 it->base_level_stop = it->stop_charpos;
7695 handle_stop (it);
7697 /* Since a handler may have changed IT->method, we must
7698 recurse here. */
7699 return GET_NEXT_DISPLAY_ELEMENT (it);
7702 else if (it->bidi_p
7703 /* If we are before prev_stop, we may have overstepped
7704 on our way backwards a stop_pos, and if so, we need
7705 to handle that stop_pos. */
7706 && IT_STRING_CHARPOS (*it) < it->prev_stop
7707 /* We can sometimes back up for reasons that have nothing
7708 to do with bidi reordering. E.g., compositions. The
7709 code below is only needed when we are above the base
7710 embedding level, so test for that explicitly. */
7711 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7713 /* If we lost track of base_level_stop, we have no better
7714 place for handle_stop_backwards to start from than string
7715 beginning. This happens, e.g., when we were reseated to
7716 the previous screenful of text by vertical-motion. */
7717 if (it->base_level_stop <= 0
7718 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7719 it->base_level_stop = 0;
7720 handle_stop_backwards (it, it->base_level_stop);
7721 return GET_NEXT_DISPLAY_ELEMENT (it);
7725 if (it->current.overlay_string_index >= 0)
7727 /* Get the next character from an overlay string. In overlay
7728 strings, there is no field width or padding with spaces to
7729 do. */
7730 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7732 it->what = IT_EOB;
7733 return 0;
7735 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7736 IT_STRING_BYTEPOS (*it),
7737 it->bidi_it.scan_dir < 0
7738 ? -1
7739 : SCHARS (it->string))
7740 && next_element_from_composition (it))
7742 return 1;
7744 else if (STRING_MULTIBYTE (it->string))
7746 const unsigned char *s = (SDATA (it->string)
7747 + IT_STRING_BYTEPOS (*it));
7748 it->c = string_char_and_length (s, &it->len);
7750 else
7752 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7753 it->len = 1;
7756 else
7758 /* Get the next character from a Lisp string that is not an
7759 overlay string. Such strings come from the mode line, for
7760 example. We may have to pad with spaces, or truncate the
7761 string. See also next_element_from_c_string. */
7762 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7764 it->what = IT_EOB;
7765 return 0;
7767 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7769 /* Pad with spaces. */
7770 it->c = ' ', it->len = 1;
7771 CHARPOS (position) = BYTEPOS (position) = -1;
7773 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7774 IT_STRING_BYTEPOS (*it),
7775 it->bidi_it.scan_dir < 0
7776 ? -1
7777 : it->string_nchars)
7778 && next_element_from_composition (it))
7780 return 1;
7782 else if (STRING_MULTIBYTE (it->string))
7784 const unsigned char *s = (SDATA (it->string)
7785 + IT_STRING_BYTEPOS (*it));
7786 it->c = string_char_and_length (s, &it->len);
7788 else
7790 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7791 it->len = 1;
7795 /* Record what we have and where it came from. */
7796 it->what = IT_CHARACTER;
7797 it->object = it->string;
7798 it->position = position;
7799 return 1;
7803 /* Load IT with next display element from C string IT->s.
7804 IT->string_nchars is the maximum number of characters to return
7805 from the string. IT->end_charpos may be greater than
7806 IT->string_nchars when this function is called, in which case we
7807 may have to return padding spaces. Value is zero if end of string
7808 reached, including padding spaces. */
7810 static int
7811 next_element_from_c_string (struct it *it)
7813 int success_p = 1;
7815 eassert (it->s);
7816 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7817 it->what = IT_CHARACTER;
7818 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7819 it->object = Qnil;
7821 /* With bidi reordering, the character to display might not be the
7822 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7823 we were reseated to a new string, whose paragraph direction is
7824 not known. */
7825 if (it->bidi_p && it->bidi_it.first_elt)
7826 get_visually_first_element (it);
7828 /* IT's position can be greater than IT->string_nchars in case a
7829 field width or precision has been specified when the iterator was
7830 initialized. */
7831 if (IT_CHARPOS (*it) >= it->end_charpos)
7833 /* End of the game. */
7834 it->what = IT_EOB;
7835 success_p = 0;
7837 else if (IT_CHARPOS (*it) >= it->string_nchars)
7839 /* Pad with spaces. */
7840 it->c = ' ', it->len = 1;
7841 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7843 else if (it->multibyte_p)
7844 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7845 else
7846 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7848 return success_p;
7852 /* Set up IT to return characters from an ellipsis, if appropriate.
7853 The definition of the ellipsis glyphs may come from a display table
7854 entry. This function fills IT with the first glyph from the
7855 ellipsis if an ellipsis is to be displayed. */
7857 static int
7858 next_element_from_ellipsis (struct it *it)
7860 if (it->selective_display_ellipsis_p)
7861 setup_for_ellipsis (it, it->len);
7862 else
7864 /* The face at the current position may be different from the
7865 face we find after the invisible text. Remember what it
7866 was in IT->saved_face_id, and signal that it's there by
7867 setting face_before_selective_p. */
7868 it->saved_face_id = it->face_id;
7869 it->method = GET_FROM_BUFFER;
7870 it->object = it->w->contents;
7871 reseat_at_next_visible_line_start (it, 1);
7872 it->face_before_selective_p = 1;
7875 return GET_NEXT_DISPLAY_ELEMENT (it);
7879 /* Deliver an image display element. The iterator IT is already
7880 filled with image information (done in handle_display_prop). Value
7881 is always 1. */
7884 static int
7885 next_element_from_image (struct it *it)
7887 it->what = IT_IMAGE;
7888 it->ignore_overlay_strings_at_pos_p = 0;
7889 return 1;
7893 /* Fill iterator IT with next display element from a stretch glyph
7894 property. IT->object is the value of the text property. Value is
7895 always 1. */
7897 static int
7898 next_element_from_stretch (struct it *it)
7900 it->what = IT_STRETCH;
7901 return 1;
7904 /* Scan backwards from IT's current position until we find a stop
7905 position, or until BEGV. This is called when we find ourself
7906 before both the last known prev_stop and base_level_stop while
7907 reordering bidirectional text. */
7909 static void
7910 compute_stop_pos_backwards (struct it *it)
7912 const int SCAN_BACK_LIMIT = 1000;
7913 struct text_pos pos;
7914 struct display_pos save_current = it->current;
7915 struct text_pos save_position = it->position;
7916 ptrdiff_t charpos = IT_CHARPOS (*it);
7917 ptrdiff_t where_we_are = charpos;
7918 ptrdiff_t save_stop_pos = it->stop_charpos;
7919 ptrdiff_t save_end_pos = it->end_charpos;
7921 eassert (NILP (it->string) && !it->s);
7922 eassert (it->bidi_p);
7923 it->bidi_p = 0;
7926 it->end_charpos = min (charpos + 1, ZV);
7927 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7928 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7929 reseat_1 (it, pos, 0);
7930 compute_stop_pos (it);
7931 /* We must advance forward, right? */
7932 if (it->stop_charpos <= charpos)
7933 emacs_abort ();
7935 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7937 if (it->stop_charpos <= where_we_are)
7938 it->prev_stop = it->stop_charpos;
7939 else
7940 it->prev_stop = BEGV;
7941 it->bidi_p = 1;
7942 it->current = save_current;
7943 it->position = save_position;
7944 it->stop_charpos = save_stop_pos;
7945 it->end_charpos = save_end_pos;
7948 /* Scan forward from CHARPOS in the current buffer/string, until we
7949 find a stop position > current IT's position. Then handle the stop
7950 position before that. This is called when we bump into a stop
7951 position while reordering bidirectional text. CHARPOS should be
7952 the last previously processed stop_pos (or BEGV/0, if none were
7953 processed yet) whose position is less that IT's current
7954 position. */
7956 static void
7957 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7959 int bufp = !STRINGP (it->string);
7960 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7961 struct display_pos save_current = it->current;
7962 struct text_pos save_position = it->position;
7963 struct text_pos pos1;
7964 ptrdiff_t next_stop;
7966 /* Scan in strict logical order. */
7967 eassert (it->bidi_p);
7968 it->bidi_p = 0;
7971 it->prev_stop = charpos;
7972 if (bufp)
7974 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7975 reseat_1 (it, pos1, 0);
7977 else
7978 it->current.string_pos = string_pos (charpos, it->string);
7979 compute_stop_pos (it);
7980 /* We must advance forward, right? */
7981 if (it->stop_charpos <= it->prev_stop)
7982 emacs_abort ();
7983 charpos = it->stop_charpos;
7985 while (charpos <= where_we_are);
7987 it->bidi_p = 1;
7988 it->current = save_current;
7989 it->position = save_position;
7990 next_stop = it->stop_charpos;
7991 it->stop_charpos = it->prev_stop;
7992 handle_stop (it);
7993 it->stop_charpos = next_stop;
7996 /* Load IT with the next display element from current_buffer. Value
7997 is zero if end of buffer reached. IT->stop_charpos is the next
7998 position at which to stop and check for text properties or buffer
7999 end. */
8001 static int
8002 next_element_from_buffer (struct it *it)
8004 int success_p = 1;
8006 eassert (IT_CHARPOS (*it) >= BEGV);
8007 eassert (NILP (it->string) && !it->s);
8008 eassert (!it->bidi_p
8009 || (EQ (it->bidi_it.string.lstring, Qnil)
8010 && it->bidi_it.string.s == NULL));
8012 /* With bidi reordering, the character to display might not be the
8013 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8014 we were reseat()ed to a new buffer position, which is potentially
8015 a different paragraph. */
8016 if (it->bidi_p && it->bidi_it.first_elt)
8018 get_visually_first_element (it);
8019 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8022 if (IT_CHARPOS (*it) >= it->stop_charpos)
8024 if (IT_CHARPOS (*it) >= it->end_charpos)
8026 int overlay_strings_follow_p;
8028 /* End of the game, except when overlay strings follow that
8029 haven't been returned yet. */
8030 if (it->overlay_strings_at_end_processed_p)
8031 overlay_strings_follow_p = 0;
8032 else
8034 it->overlay_strings_at_end_processed_p = 1;
8035 overlay_strings_follow_p = get_overlay_strings (it, 0);
8038 if (overlay_strings_follow_p)
8039 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8040 else
8042 it->what = IT_EOB;
8043 it->position = it->current.pos;
8044 success_p = 0;
8047 else if (!(!it->bidi_p
8048 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8049 || IT_CHARPOS (*it) == it->stop_charpos))
8051 /* With bidi non-linear iteration, we could find ourselves
8052 far beyond the last computed stop_charpos, with several
8053 other stop positions in between that we missed. Scan
8054 them all now, in buffer's logical order, until we find
8055 and handle the last stop_charpos that precedes our
8056 current position. */
8057 handle_stop_backwards (it, it->stop_charpos);
8058 return GET_NEXT_DISPLAY_ELEMENT (it);
8060 else
8062 if (it->bidi_p)
8064 /* Take note of the stop position we just moved across,
8065 for when we will move back across it. */
8066 it->prev_stop = it->stop_charpos;
8067 /* If we are at base paragraph embedding level, take
8068 note of the last stop position seen at this
8069 level. */
8070 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8071 it->base_level_stop = it->stop_charpos;
8073 handle_stop (it);
8074 return GET_NEXT_DISPLAY_ELEMENT (it);
8077 else if (it->bidi_p
8078 /* If we are before prev_stop, we may have overstepped on
8079 our way backwards a stop_pos, and if so, we need to
8080 handle that stop_pos. */
8081 && IT_CHARPOS (*it) < it->prev_stop
8082 /* We can sometimes back up for reasons that have nothing
8083 to do with bidi reordering. E.g., compositions. The
8084 code below is only needed when we are above the base
8085 embedding level, so test for that explicitly. */
8086 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8088 if (it->base_level_stop <= 0
8089 || IT_CHARPOS (*it) < it->base_level_stop)
8091 /* If we lost track of base_level_stop, we need to find
8092 prev_stop by looking backwards. This happens, e.g., when
8093 we were reseated to the previous screenful of text by
8094 vertical-motion. */
8095 it->base_level_stop = BEGV;
8096 compute_stop_pos_backwards (it);
8097 handle_stop_backwards (it, it->prev_stop);
8099 else
8100 handle_stop_backwards (it, it->base_level_stop);
8101 return GET_NEXT_DISPLAY_ELEMENT (it);
8103 else
8105 /* No face changes, overlays etc. in sight, so just return a
8106 character from current_buffer. */
8107 unsigned char *p;
8108 ptrdiff_t stop;
8110 /* Maybe run the redisplay end trigger hook. Performance note:
8111 This doesn't seem to cost measurable time. */
8112 if (it->redisplay_end_trigger_charpos
8113 && it->glyph_row
8114 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8115 run_redisplay_end_trigger_hook (it);
8117 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8118 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8119 stop)
8120 && next_element_from_composition (it))
8122 return 1;
8125 /* Get the next character, maybe multibyte. */
8126 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8127 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8128 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8129 else
8130 it->c = *p, it->len = 1;
8132 /* Record what we have and where it came from. */
8133 it->what = IT_CHARACTER;
8134 it->object = it->w->contents;
8135 it->position = it->current.pos;
8137 /* Normally we return the character found above, except when we
8138 really want to return an ellipsis for selective display. */
8139 if (it->selective)
8141 if (it->c == '\n')
8143 /* A value of selective > 0 means hide lines indented more
8144 than that number of columns. */
8145 if (it->selective > 0
8146 && IT_CHARPOS (*it) + 1 < ZV
8147 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8148 IT_BYTEPOS (*it) + 1,
8149 it->selective))
8151 success_p = next_element_from_ellipsis (it);
8152 it->dpvec_char_len = -1;
8155 else if (it->c == '\r' && it->selective == -1)
8157 /* A value of selective == -1 means that everything from the
8158 CR to the end of the line is invisible, with maybe an
8159 ellipsis displayed for it. */
8160 success_p = next_element_from_ellipsis (it);
8161 it->dpvec_char_len = -1;
8166 /* Value is zero if end of buffer reached. */
8167 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8168 return success_p;
8172 /* Run the redisplay end trigger hook for IT. */
8174 static void
8175 run_redisplay_end_trigger_hook (struct it *it)
8177 Lisp_Object args[3];
8179 /* IT->glyph_row should be non-null, i.e. we should be actually
8180 displaying something, or otherwise we should not run the hook. */
8181 eassert (it->glyph_row);
8183 /* Set up hook arguments. */
8184 args[0] = Qredisplay_end_trigger_functions;
8185 args[1] = it->window;
8186 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8187 it->redisplay_end_trigger_charpos = 0;
8189 /* Since we are *trying* to run these functions, don't try to run
8190 them again, even if they get an error. */
8191 wset_redisplay_end_trigger (it->w, Qnil);
8192 Frun_hook_with_args (3, args);
8194 /* Notice if it changed the face of the character we are on. */
8195 handle_face_prop (it);
8199 /* Deliver a composition display element. Unlike the other
8200 next_element_from_XXX, this function is not registered in the array
8201 get_next_element[]. It is called from next_element_from_buffer and
8202 next_element_from_string when necessary. */
8204 static int
8205 next_element_from_composition (struct it *it)
8207 it->what = IT_COMPOSITION;
8208 it->len = it->cmp_it.nbytes;
8209 if (STRINGP (it->string))
8211 if (it->c < 0)
8213 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8214 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8215 return 0;
8217 it->position = it->current.string_pos;
8218 it->object = it->string;
8219 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8220 IT_STRING_BYTEPOS (*it), it->string);
8222 else
8224 if (it->c < 0)
8226 IT_CHARPOS (*it) += it->cmp_it.nchars;
8227 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8228 if (it->bidi_p)
8230 if (it->bidi_it.new_paragraph)
8231 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8232 /* Resync the bidi iterator with IT's new position.
8233 FIXME: this doesn't support bidirectional text. */
8234 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8235 bidi_move_to_visually_next (&it->bidi_it);
8237 return 0;
8239 it->position = it->current.pos;
8240 it->object = it->w->contents;
8241 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8242 IT_BYTEPOS (*it), Qnil);
8244 return 1;
8249 /***********************************************************************
8250 Moving an iterator without producing glyphs
8251 ***********************************************************************/
8253 /* Check if iterator is at a position corresponding to a valid buffer
8254 position after some move_it_ call. */
8256 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8257 ((it)->method == GET_FROM_STRING \
8258 ? IT_STRING_CHARPOS (*it) == 0 \
8259 : 1)
8262 /* Move iterator IT to a specified buffer or X position within one
8263 line on the display without producing glyphs.
8265 OP should be a bit mask including some or all of these bits:
8266 MOVE_TO_X: Stop upon reaching x-position TO_X.
8267 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8268 Regardless of OP's value, stop upon reaching the end of the display line.
8270 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8271 This means, in particular, that TO_X includes window's horizontal
8272 scroll amount.
8274 The return value has several possible values that
8275 say what condition caused the scan to stop:
8277 MOVE_POS_MATCH_OR_ZV
8278 - when TO_POS or ZV was reached.
8280 MOVE_X_REACHED
8281 -when TO_X was reached before TO_POS or ZV were reached.
8283 MOVE_LINE_CONTINUED
8284 - when we reached the end of the display area and the line must
8285 be continued.
8287 MOVE_LINE_TRUNCATED
8288 - when we reached the end of the display area and the line is
8289 truncated.
8291 MOVE_NEWLINE_OR_CR
8292 - when we stopped at a line end, i.e. a newline or a CR and selective
8293 display is on. */
8295 static enum move_it_result
8296 move_it_in_display_line_to (struct it *it,
8297 ptrdiff_t to_charpos, int to_x,
8298 enum move_operation_enum op)
8300 enum move_it_result result = MOVE_UNDEFINED;
8301 struct glyph_row *saved_glyph_row;
8302 struct it wrap_it, atpos_it, atx_it, ppos_it;
8303 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8304 void *ppos_data = NULL;
8305 int may_wrap = 0;
8306 enum it_method prev_method = it->method;
8307 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8308 int saw_smaller_pos = prev_pos < to_charpos;
8310 /* Don't produce glyphs in produce_glyphs. */
8311 saved_glyph_row = it->glyph_row;
8312 it->glyph_row = NULL;
8314 /* Use wrap_it to save a copy of IT wherever a word wrap could
8315 occur. Use atpos_it to save a copy of IT at the desired buffer
8316 position, if found, so that we can scan ahead and check if the
8317 word later overshoots the window edge. Use atx_it similarly, for
8318 pixel positions. */
8319 wrap_it.sp = -1;
8320 atpos_it.sp = -1;
8321 atx_it.sp = -1;
8323 /* Use ppos_it under bidi reordering to save a copy of IT for the
8324 position > CHARPOS that is the closest to CHARPOS. We restore
8325 that position in IT when we have scanned the entire display line
8326 without finding a match for CHARPOS and all the character
8327 positions are greater than CHARPOS. */
8328 if (it->bidi_p)
8330 SAVE_IT (ppos_it, *it, ppos_data);
8331 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8332 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8333 SAVE_IT (ppos_it, *it, ppos_data);
8336 #define BUFFER_POS_REACHED_P() \
8337 ((op & MOVE_TO_POS) != 0 \
8338 && BUFFERP (it->object) \
8339 && (IT_CHARPOS (*it) == to_charpos \
8340 || ((!it->bidi_p \
8341 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8342 && IT_CHARPOS (*it) > to_charpos) \
8343 || (it->what == IT_COMPOSITION \
8344 && ((IT_CHARPOS (*it) > to_charpos \
8345 && to_charpos >= it->cmp_it.charpos) \
8346 || (IT_CHARPOS (*it) < to_charpos \
8347 && to_charpos <= it->cmp_it.charpos)))) \
8348 && (it->method == GET_FROM_BUFFER \
8349 || (it->method == GET_FROM_DISPLAY_VECTOR \
8350 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8352 /* If there's a line-/wrap-prefix, handle it. */
8353 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8354 && it->current_y < it->last_visible_y)
8355 handle_line_prefix (it);
8357 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8358 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8360 while (1)
8362 int x, i, ascent = 0, descent = 0;
8364 /* Utility macro to reset an iterator with x, ascent, and descent. */
8365 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8366 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8367 (IT)->max_descent = descent)
8369 /* Stop if we move beyond TO_CHARPOS (after an image or a
8370 display string or stretch glyph). */
8371 if ((op & MOVE_TO_POS) != 0
8372 && BUFFERP (it->object)
8373 && it->method == GET_FROM_BUFFER
8374 && (((!it->bidi_p
8375 /* When the iterator is at base embedding level, we
8376 are guaranteed that characters are delivered for
8377 display in strictly increasing order of their
8378 buffer positions. */
8379 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8380 && IT_CHARPOS (*it) > to_charpos)
8381 || (it->bidi_p
8382 && (prev_method == GET_FROM_IMAGE
8383 || prev_method == GET_FROM_STRETCH
8384 || prev_method == GET_FROM_STRING)
8385 /* Passed TO_CHARPOS from left to right. */
8386 && ((prev_pos < to_charpos
8387 && IT_CHARPOS (*it) > to_charpos)
8388 /* Passed TO_CHARPOS from right to left. */
8389 || (prev_pos > to_charpos
8390 && IT_CHARPOS (*it) < to_charpos)))))
8392 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8394 result = MOVE_POS_MATCH_OR_ZV;
8395 break;
8397 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8398 /* If wrap_it is valid, the current position might be in a
8399 word that is wrapped. So, save the iterator in
8400 atpos_it and continue to see if wrapping happens. */
8401 SAVE_IT (atpos_it, *it, atpos_data);
8404 /* Stop when ZV reached.
8405 We used to stop here when TO_CHARPOS reached as well, but that is
8406 too soon if this glyph does not fit on this line. So we handle it
8407 explicitly below. */
8408 if (!get_next_display_element (it))
8410 result = MOVE_POS_MATCH_OR_ZV;
8411 break;
8414 if (it->line_wrap == TRUNCATE)
8416 if (BUFFER_POS_REACHED_P ())
8418 result = MOVE_POS_MATCH_OR_ZV;
8419 break;
8422 else
8424 if (it->line_wrap == WORD_WRAP)
8426 if (IT_DISPLAYING_WHITESPACE (it))
8427 may_wrap = 1;
8428 else if (may_wrap)
8430 /* We have reached a glyph that follows one or more
8431 whitespace characters. If the position is
8432 already found, we are done. */
8433 if (atpos_it.sp >= 0)
8435 RESTORE_IT (it, &atpos_it, atpos_data);
8436 result = MOVE_POS_MATCH_OR_ZV;
8437 goto done;
8439 if (atx_it.sp >= 0)
8441 RESTORE_IT (it, &atx_it, atx_data);
8442 result = MOVE_X_REACHED;
8443 goto done;
8445 /* Otherwise, we can wrap here. */
8446 SAVE_IT (wrap_it, *it, wrap_data);
8447 may_wrap = 0;
8452 /* Remember the line height for the current line, in case
8453 the next element doesn't fit on the line. */
8454 ascent = it->max_ascent;
8455 descent = it->max_descent;
8457 /* The call to produce_glyphs will get the metrics of the
8458 display element IT is loaded with. Record the x-position
8459 before this display element, in case it doesn't fit on the
8460 line. */
8461 x = it->current_x;
8463 PRODUCE_GLYPHS (it);
8465 if (it->area != TEXT_AREA)
8467 prev_method = it->method;
8468 if (it->method == GET_FROM_BUFFER)
8469 prev_pos = IT_CHARPOS (*it);
8470 set_iterator_to_next (it, 1);
8471 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8472 SET_TEXT_POS (this_line_min_pos,
8473 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8474 if (it->bidi_p
8475 && (op & MOVE_TO_POS)
8476 && IT_CHARPOS (*it) > to_charpos
8477 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8478 SAVE_IT (ppos_it, *it, ppos_data);
8479 continue;
8482 /* The number of glyphs we get back in IT->nglyphs will normally
8483 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8484 character on a terminal frame, or (iii) a line end. For the
8485 second case, IT->nglyphs - 1 padding glyphs will be present.
8486 (On X frames, there is only one glyph produced for a
8487 composite character.)
8489 The behavior implemented below means, for continuation lines,
8490 that as many spaces of a TAB as fit on the current line are
8491 displayed there. For terminal frames, as many glyphs of a
8492 multi-glyph character are displayed in the current line, too.
8493 This is what the old redisplay code did, and we keep it that
8494 way. Under X, the whole shape of a complex character must
8495 fit on the line or it will be completely displayed in the
8496 next line.
8498 Note that both for tabs and padding glyphs, all glyphs have
8499 the same width. */
8500 if (it->nglyphs)
8502 /* More than one glyph or glyph doesn't fit on line. All
8503 glyphs have the same width. */
8504 int single_glyph_width = it->pixel_width / it->nglyphs;
8505 int new_x;
8506 int x_before_this_char = x;
8507 int hpos_before_this_char = it->hpos;
8509 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8511 new_x = x + single_glyph_width;
8513 /* We want to leave anything reaching TO_X to the caller. */
8514 if ((op & MOVE_TO_X) && new_x > to_x)
8516 if (BUFFER_POS_REACHED_P ())
8518 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8519 goto buffer_pos_reached;
8520 if (atpos_it.sp < 0)
8522 SAVE_IT (atpos_it, *it, atpos_data);
8523 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8526 else
8528 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8530 it->current_x = x;
8531 result = MOVE_X_REACHED;
8532 break;
8534 if (atx_it.sp < 0)
8536 SAVE_IT (atx_it, *it, atx_data);
8537 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8542 if (/* Lines are continued. */
8543 it->line_wrap != TRUNCATE
8544 && (/* And glyph doesn't fit on the line. */
8545 new_x > it->last_visible_x
8546 /* Or it fits exactly and we're on a window
8547 system frame. */
8548 || (new_x == it->last_visible_x
8549 && FRAME_WINDOW_P (it->f)
8550 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8551 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8552 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8554 if (/* IT->hpos == 0 means the very first glyph
8555 doesn't fit on the line, e.g. a wide image. */
8556 it->hpos == 0
8557 || (new_x == it->last_visible_x
8558 && FRAME_WINDOW_P (it->f)))
8560 ++it->hpos;
8561 it->current_x = new_x;
8563 /* The character's last glyph just barely fits
8564 in this row. */
8565 if (i == it->nglyphs - 1)
8567 /* If this is the destination position,
8568 return a position *before* it in this row,
8569 now that we know it fits in this row. */
8570 if (BUFFER_POS_REACHED_P ())
8572 if (it->line_wrap != WORD_WRAP
8573 || wrap_it.sp < 0)
8575 it->hpos = hpos_before_this_char;
8576 it->current_x = x_before_this_char;
8577 result = MOVE_POS_MATCH_OR_ZV;
8578 break;
8580 if (it->line_wrap == WORD_WRAP
8581 && atpos_it.sp < 0)
8583 SAVE_IT (atpos_it, *it, atpos_data);
8584 atpos_it.current_x = x_before_this_char;
8585 atpos_it.hpos = hpos_before_this_char;
8589 prev_method = it->method;
8590 if (it->method == GET_FROM_BUFFER)
8591 prev_pos = IT_CHARPOS (*it);
8592 set_iterator_to_next (it, 1);
8593 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8594 SET_TEXT_POS (this_line_min_pos,
8595 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8596 /* On graphical terminals, newlines may
8597 "overflow" into the fringe if
8598 overflow-newline-into-fringe is non-nil.
8599 On text terminals, and on graphical
8600 terminals with no right margin, newlines
8601 may overflow into the last glyph on the
8602 display line.*/
8603 if (!FRAME_WINDOW_P (it->f)
8604 || ((it->bidi_p
8605 && it->bidi_it.paragraph_dir == R2L)
8606 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8607 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8608 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8610 if (!get_next_display_element (it))
8612 result = MOVE_POS_MATCH_OR_ZV;
8613 break;
8615 if (BUFFER_POS_REACHED_P ())
8617 if (ITERATOR_AT_END_OF_LINE_P (it))
8618 result = MOVE_POS_MATCH_OR_ZV;
8619 else
8620 result = MOVE_LINE_CONTINUED;
8621 break;
8623 if (ITERATOR_AT_END_OF_LINE_P (it)
8624 && (it->line_wrap != WORD_WRAP
8625 || wrap_it.sp < 0))
8627 result = MOVE_NEWLINE_OR_CR;
8628 break;
8633 else
8634 IT_RESET_X_ASCENT_DESCENT (it);
8636 if (wrap_it.sp >= 0)
8638 RESTORE_IT (it, &wrap_it, wrap_data);
8639 atpos_it.sp = -1;
8640 atx_it.sp = -1;
8643 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8644 IT_CHARPOS (*it)));
8645 result = MOVE_LINE_CONTINUED;
8646 break;
8649 if (BUFFER_POS_REACHED_P ())
8651 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8652 goto buffer_pos_reached;
8653 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8655 SAVE_IT (atpos_it, *it, atpos_data);
8656 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8660 if (new_x > it->first_visible_x)
8662 /* Glyph is visible. Increment number of glyphs that
8663 would be displayed. */
8664 ++it->hpos;
8668 if (result != MOVE_UNDEFINED)
8669 break;
8671 else if (BUFFER_POS_REACHED_P ())
8673 buffer_pos_reached:
8674 IT_RESET_X_ASCENT_DESCENT (it);
8675 result = MOVE_POS_MATCH_OR_ZV;
8676 break;
8678 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8680 /* Stop when TO_X specified and reached. This check is
8681 necessary here because of lines consisting of a line end,
8682 only. The line end will not produce any glyphs and we
8683 would never get MOVE_X_REACHED. */
8684 eassert (it->nglyphs == 0);
8685 result = MOVE_X_REACHED;
8686 break;
8689 /* Is this a line end? If yes, we're done. */
8690 if (ITERATOR_AT_END_OF_LINE_P (it))
8692 /* If we are past TO_CHARPOS, but never saw any character
8693 positions smaller than TO_CHARPOS, return
8694 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8695 did. */
8696 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8698 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8700 if (IT_CHARPOS (ppos_it) < ZV)
8702 RESTORE_IT (it, &ppos_it, ppos_data);
8703 result = MOVE_POS_MATCH_OR_ZV;
8705 else
8706 goto buffer_pos_reached;
8708 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8709 && IT_CHARPOS (*it) > to_charpos)
8710 goto buffer_pos_reached;
8711 else
8712 result = MOVE_NEWLINE_OR_CR;
8714 else
8715 result = MOVE_NEWLINE_OR_CR;
8716 break;
8719 prev_method = it->method;
8720 if (it->method == GET_FROM_BUFFER)
8721 prev_pos = IT_CHARPOS (*it);
8722 /* The current display element has been consumed. Advance
8723 to the next. */
8724 set_iterator_to_next (it, 1);
8725 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8726 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8727 if (IT_CHARPOS (*it) < to_charpos)
8728 saw_smaller_pos = 1;
8729 if (it->bidi_p
8730 && (op & MOVE_TO_POS)
8731 && IT_CHARPOS (*it) >= to_charpos
8732 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8733 SAVE_IT (ppos_it, *it, ppos_data);
8735 /* Stop if lines are truncated and IT's current x-position is
8736 past the right edge of the window now. */
8737 if (it->line_wrap == TRUNCATE
8738 && it->current_x >= it->last_visible_x)
8740 if (!FRAME_WINDOW_P (it->f)
8741 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8742 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8743 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8744 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8746 int at_eob_p = 0;
8748 if ((at_eob_p = !get_next_display_element (it))
8749 || BUFFER_POS_REACHED_P ()
8750 /* If we are past TO_CHARPOS, but never saw any
8751 character positions smaller than TO_CHARPOS,
8752 return MOVE_POS_MATCH_OR_ZV, like the
8753 unidirectional display did. */
8754 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8755 && !saw_smaller_pos
8756 && IT_CHARPOS (*it) > to_charpos))
8758 if (it->bidi_p
8759 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8760 RESTORE_IT (it, &ppos_it, ppos_data);
8761 result = MOVE_POS_MATCH_OR_ZV;
8762 break;
8764 if (ITERATOR_AT_END_OF_LINE_P (it))
8766 result = MOVE_NEWLINE_OR_CR;
8767 break;
8770 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8771 && !saw_smaller_pos
8772 && IT_CHARPOS (*it) > to_charpos)
8774 if (IT_CHARPOS (ppos_it) < ZV)
8775 RESTORE_IT (it, &ppos_it, ppos_data);
8776 result = MOVE_POS_MATCH_OR_ZV;
8777 break;
8779 result = MOVE_LINE_TRUNCATED;
8780 break;
8782 #undef IT_RESET_X_ASCENT_DESCENT
8785 #undef BUFFER_POS_REACHED_P
8787 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8788 restore the saved iterator. */
8789 if (atpos_it.sp >= 0)
8790 RESTORE_IT (it, &atpos_it, atpos_data);
8791 else if (atx_it.sp >= 0)
8792 RESTORE_IT (it, &atx_it, atx_data);
8794 done:
8796 if (atpos_data)
8797 bidi_unshelve_cache (atpos_data, 1);
8798 if (atx_data)
8799 bidi_unshelve_cache (atx_data, 1);
8800 if (wrap_data)
8801 bidi_unshelve_cache (wrap_data, 1);
8802 if (ppos_data)
8803 bidi_unshelve_cache (ppos_data, 1);
8805 /* Restore the iterator settings altered at the beginning of this
8806 function. */
8807 it->glyph_row = saved_glyph_row;
8808 return result;
8811 /* For external use. */
8812 void
8813 move_it_in_display_line (struct it *it,
8814 ptrdiff_t to_charpos, int to_x,
8815 enum move_operation_enum op)
8817 if (it->line_wrap == WORD_WRAP
8818 && (op & MOVE_TO_X))
8820 struct it save_it;
8821 void *save_data = NULL;
8822 int skip;
8824 SAVE_IT (save_it, *it, save_data);
8825 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8826 /* When word-wrap is on, TO_X may lie past the end
8827 of a wrapped line. Then it->current is the
8828 character on the next line, so backtrack to the
8829 space before the wrap point. */
8830 if (skip == MOVE_LINE_CONTINUED)
8832 int prev_x = max (it->current_x - 1, 0);
8833 RESTORE_IT (it, &save_it, save_data);
8834 move_it_in_display_line_to
8835 (it, -1, prev_x, MOVE_TO_X);
8837 else
8838 bidi_unshelve_cache (save_data, 1);
8840 else
8841 move_it_in_display_line_to (it, to_charpos, to_x, op);
8845 /* Move IT forward until it satisfies one or more of the criteria in
8846 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8848 OP is a bit-mask that specifies where to stop, and in particular,
8849 which of those four position arguments makes a difference. See the
8850 description of enum move_operation_enum.
8852 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8853 screen line, this function will set IT to the next position that is
8854 displayed to the right of TO_CHARPOS on the screen. */
8856 void
8857 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8859 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8860 int line_height, line_start_x = 0, reached = 0;
8861 void *backup_data = NULL;
8863 for (;;)
8865 if (op & MOVE_TO_VPOS)
8867 /* If no TO_CHARPOS and no TO_X specified, stop at the
8868 start of the line TO_VPOS. */
8869 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8871 if (it->vpos == to_vpos)
8873 reached = 1;
8874 break;
8876 else
8877 skip = move_it_in_display_line_to (it, -1, -1, 0);
8879 else
8881 /* TO_VPOS >= 0 means stop at TO_X in the line at
8882 TO_VPOS, or at TO_POS, whichever comes first. */
8883 if (it->vpos == to_vpos)
8885 reached = 2;
8886 break;
8889 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8891 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8893 reached = 3;
8894 break;
8896 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8898 /* We have reached TO_X but not in the line we want. */
8899 skip = move_it_in_display_line_to (it, to_charpos,
8900 -1, MOVE_TO_POS);
8901 if (skip == MOVE_POS_MATCH_OR_ZV)
8903 reached = 4;
8904 break;
8909 else if (op & MOVE_TO_Y)
8911 struct it it_backup;
8913 if (it->line_wrap == WORD_WRAP)
8914 SAVE_IT (it_backup, *it, backup_data);
8916 /* TO_Y specified means stop at TO_X in the line containing
8917 TO_Y---or at TO_CHARPOS if this is reached first. The
8918 problem is that we can't really tell whether the line
8919 contains TO_Y before we have completely scanned it, and
8920 this may skip past TO_X. What we do is to first scan to
8921 TO_X.
8923 If TO_X is not specified, use a TO_X of zero. The reason
8924 is to make the outcome of this function more predictable.
8925 If we didn't use TO_X == 0, we would stop at the end of
8926 the line which is probably not what a caller would expect
8927 to happen. */
8928 skip = move_it_in_display_line_to
8929 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8930 (MOVE_TO_X | (op & MOVE_TO_POS)));
8932 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8933 if (skip == MOVE_POS_MATCH_OR_ZV)
8934 reached = 5;
8935 else if (skip == MOVE_X_REACHED)
8937 /* If TO_X was reached, we want to know whether TO_Y is
8938 in the line. We know this is the case if the already
8939 scanned glyphs make the line tall enough. Otherwise,
8940 we must check by scanning the rest of the line. */
8941 line_height = it->max_ascent + it->max_descent;
8942 if (to_y >= it->current_y
8943 && to_y < it->current_y + line_height)
8945 reached = 6;
8946 break;
8948 SAVE_IT (it_backup, *it, backup_data);
8949 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8950 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8951 op & MOVE_TO_POS);
8952 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8953 line_height = it->max_ascent + it->max_descent;
8954 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8956 if (to_y >= it->current_y
8957 && to_y < it->current_y + line_height)
8959 /* If TO_Y is in this line and TO_X was reached
8960 above, we scanned too far. We have to restore
8961 IT's settings to the ones before skipping. But
8962 keep the more accurate values of max_ascent and
8963 max_descent we've found while skipping the rest
8964 of the line, for the sake of callers, such as
8965 pos_visible_p, that need to know the line
8966 height. */
8967 int max_ascent = it->max_ascent;
8968 int max_descent = it->max_descent;
8970 RESTORE_IT (it, &it_backup, backup_data);
8971 it->max_ascent = max_ascent;
8972 it->max_descent = max_descent;
8973 reached = 6;
8975 else
8977 skip = skip2;
8978 if (skip == MOVE_POS_MATCH_OR_ZV)
8979 reached = 7;
8982 else
8984 /* Check whether TO_Y is in this line. */
8985 line_height = it->max_ascent + it->max_descent;
8986 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8988 if (to_y >= it->current_y
8989 && to_y < it->current_y + line_height)
8991 /* When word-wrap is on, TO_X may lie past the end
8992 of a wrapped line. Then it->current is the
8993 character on the next line, so backtrack to the
8994 space before the wrap point. */
8995 if (skip == MOVE_LINE_CONTINUED
8996 && it->line_wrap == WORD_WRAP)
8998 int prev_x = max (it->current_x - 1, 0);
8999 RESTORE_IT (it, &it_backup, backup_data);
9000 skip = move_it_in_display_line_to
9001 (it, -1, prev_x, MOVE_TO_X);
9003 reached = 6;
9007 if (reached)
9008 break;
9010 else if (BUFFERP (it->object)
9011 && (it->method == GET_FROM_BUFFER
9012 || it->method == GET_FROM_STRETCH)
9013 && IT_CHARPOS (*it) >= to_charpos
9014 /* Under bidi iteration, a call to set_iterator_to_next
9015 can scan far beyond to_charpos if the initial
9016 portion of the next line needs to be reordered. In
9017 that case, give move_it_in_display_line_to another
9018 chance below. */
9019 && !(it->bidi_p
9020 && it->bidi_it.scan_dir == -1))
9021 skip = MOVE_POS_MATCH_OR_ZV;
9022 else
9023 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9025 switch (skip)
9027 case MOVE_POS_MATCH_OR_ZV:
9028 reached = 8;
9029 goto out;
9031 case MOVE_NEWLINE_OR_CR:
9032 set_iterator_to_next (it, 1);
9033 it->continuation_lines_width = 0;
9034 break;
9036 case MOVE_LINE_TRUNCATED:
9037 it->continuation_lines_width = 0;
9038 reseat_at_next_visible_line_start (it, 0);
9039 if ((op & MOVE_TO_POS) != 0
9040 && IT_CHARPOS (*it) > to_charpos)
9042 reached = 9;
9043 goto out;
9045 break;
9047 case MOVE_LINE_CONTINUED:
9048 /* For continued lines ending in a tab, some of the glyphs
9049 associated with the tab are displayed on the current
9050 line. Since it->current_x does not include these glyphs,
9051 we use it->last_visible_x instead. */
9052 if (it->c == '\t')
9054 it->continuation_lines_width += it->last_visible_x;
9055 /* When moving by vpos, ensure that the iterator really
9056 advances to the next line (bug#847, bug#969). Fixme:
9057 do we need to do this in other circumstances? */
9058 if (it->current_x != it->last_visible_x
9059 && (op & MOVE_TO_VPOS)
9060 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9062 line_start_x = it->current_x + it->pixel_width
9063 - it->last_visible_x;
9064 set_iterator_to_next (it, 0);
9067 else
9068 it->continuation_lines_width += it->current_x;
9069 break;
9071 default:
9072 emacs_abort ();
9075 /* Reset/increment for the next run. */
9076 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9077 it->current_x = line_start_x;
9078 line_start_x = 0;
9079 it->hpos = 0;
9080 it->current_y += it->max_ascent + it->max_descent;
9081 ++it->vpos;
9082 last_height = it->max_ascent + it->max_descent;
9083 it->max_ascent = it->max_descent = 0;
9086 out:
9088 /* On text terminals, we may stop at the end of a line in the middle
9089 of a multi-character glyph. If the glyph itself is continued,
9090 i.e. it is actually displayed on the next line, don't treat this
9091 stopping point as valid; move to the next line instead (unless
9092 that brings us offscreen). */
9093 if (!FRAME_WINDOW_P (it->f)
9094 && op & MOVE_TO_POS
9095 && IT_CHARPOS (*it) == to_charpos
9096 && it->what == IT_CHARACTER
9097 && it->nglyphs > 1
9098 && it->line_wrap == WINDOW_WRAP
9099 && it->current_x == it->last_visible_x - 1
9100 && it->c != '\n'
9101 && it->c != '\t'
9102 && it->vpos < it->w->window_end_vpos)
9104 it->continuation_lines_width += it->current_x;
9105 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9106 it->current_y += it->max_ascent + it->max_descent;
9107 ++it->vpos;
9108 last_height = it->max_ascent + it->max_descent;
9111 if (backup_data)
9112 bidi_unshelve_cache (backup_data, 1);
9114 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9118 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9120 If DY > 0, move IT backward at least that many pixels. DY = 0
9121 means move IT backward to the preceding line start or BEGV. This
9122 function may move over more than DY pixels if IT->current_y - DY
9123 ends up in the middle of a line; in this case IT->current_y will be
9124 set to the top of the line moved to. */
9126 void
9127 move_it_vertically_backward (struct it *it, int dy)
9129 int nlines, h;
9130 struct it it2, it3;
9131 void *it2data = NULL, *it3data = NULL;
9132 ptrdiff_t start_pos;
9133 int nchars_per_row
9134 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9135 ptrdiff_t pos_limit;
9137 move_further_back:
9138 eassert (dy >= 0);
9140 start_pos = IT_CHARPOS (*it);
9142 /* Estimate how many newlines we must move back. */
9143 nlines = max (1, dy / default_line_pixel_height (it->w));
9144 if (it->line_wrap == TRUNCATE)
9145 pos_limit = BEGV;
9146 else
9147 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9149 /* Set the iterator's position that many lines back. But don't go
9150 back more than NLINES full screen lines -- this wins a day with
9151 buffers which have very long lines. */
9152 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9153 back_to_previous_visible_line_start (it);
9155 /* Reseat the iterator here. When moving backward, we don't want
9156 reseat to skip forward over invisible text, set up the iterator
9157 to deliver from overlay strings at the new position etc. So,
9158 use reseat_1 here. */
9159 reseat_1 (it, it->current.pos, 1);
9161 /* We are now surely at a line start. */
9162 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9163 reordering is in effect. */
9164 it->continuation_lines_width = 0;
9166 /* Move forward and see what y-distance we moved. First move to the
9167 start of the next line so that we get its height. We need this
9168 height to be able to tell whether we reached the specified
9169 y-distance. */
9170 SAVE_IT (it2, *it, it2data);
9171 it2.max_ascent = it2.max_descent = 0;
9174 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9175 MOVE_TO_POS | MOVE_TO_VPOS);
9177 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9178 /* If we are in a display string which starts at START_POS,
9179 and that display string includes a newline, and we are
9180 right after that newline (i.e. at the beginning of a
9181 display line), exit the loop, because otherwise we will
9182 infloop, since move_it_to will see that it is already at
9183 START_POS and will not move. */
9184 || (it2.method == GET_FROM_STRING
9185 && IT_CHARPOS (it2) == start_pos
9186 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9187 eassert (IT_CHARPOS (*it) >= BEGV);
9188 SAVE_IT (it3, it2, it3data);
9190 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9191 eassert (IT_CHARPOS (*it) >= BEGV);
9192 /* H is the actual vertical distance from the position in *IT
9193 and the starting position. */
9194 h = it2.current_y - it->current_y;
9195 /* NLINES is the distance in number of lines. */
9196 nlines = it2.vpos - it->vpos;
9198 /* Correct IT's y and vpos position
9199 so that they are relative to the starting point. */
9200 it->vpos -= nlines;
9201 it->current_y -= h;
9203 if (dy == 0)
9205 /* DY == 0 means move to the start of the screen line. The
9206 value of nlines is > 0 if continuation lines were involved,
9207 or if the original IT position was at start of a line. */
9208 RESTORE_IT (it, it, it2data);
9209 if (nlines > 0)
9210 move_it_by_lines (it, nlines);
9211 /* The above code moves us to some position NLINES down,
9212 usually to its first glyph (leftmost in an L2R line), but
9213 that's not necessarily the start of the line, under bidi
9214 reordering. We want to get to the character position
9215 that is immediately after the newline of the previous
9216 line. */
9217 if (it->bidi_p
9218 && !it->continuation_lines_width
9219 && !STRINGP (it->string)
9220 && IT_CHARPOS (*it) > BEGV
9221 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9223 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9225 DEC_BOTH (cp, bp);
9226 cp = find_newline_no_quit (cp, bp, -1, NULL);
9227 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9229 bidi_unshelve_cache (it3data, 1);
9231 else
9233 /* The y-position we try to reach, relative to *IT.
9234 Note that H has been subtracted in front of the if-statement. */
9235 int target_y = it->current_y + h - dy;
9236 int y0 = it3.current_y;
9237 int y1;
9238 int line_height;
9240 RESTORE_IT (&it3, &it3, it3data);
9241 y1 = line_bottom_y (&it3);
9242 line_height = y1 - y0;
9243 RESTORE_IT (it, it, it2data);
9244 /* If we did not reach target_y, try to move further backward if
9245 we can. If we moved too far backward, try to move forward. */
9246 if (target_y < it->current_y
9247 /* This is heuristic. In a window that's 3 lines high, with
9248 a line height of 13 pixels each, recentering with point
9249 on the bottom line will try to move -39/2 = 19 pixels
9250 backward. Try to avoid moving into the first line. */
9251 && (it->current_y - target_y
9252 > min (window_box_height (it->w), line_height * 2 / 3))
9253 && IT_CHARPOS (*it) > BEGV)
9255 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9256 target_y - it->current_y));
9257 dy = it->current_y - target_y;
9258 goto move_further_back;
9260 else if (target_y >= it->current_y + line_height
9261 && IT_CHARPOS (*it) < ZV)
9263 /* Should move forward by at least one line, maybe more.
9265 Note: Calling move_it_by_lines can be expensive on
9266 terminal frames, where compute_motion is used (via
9267 vmotion) to do the job, when there are very long lines
9268 and truncate-lines is nil. That's the reason for
9269 treating terminal frames specially here. */
9271 if (!FRAME_WINDOW_P (it->f))
9272 move_it_vertically (it, target_y - (it->current_y + line_height));
9273 else
9277 move_it_by_lines (it, 1);
9279 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9286 /* Move IT by a specified amount of pixel lines DY. DY negative means
9287 move backwards. DY = 0 means move to start of screen line. At the
9288 end, IT will be on the start of a screen line. */
9290 void
9291 move_it_vertically (struct it *it, int dy)
9293 if (dy <= 0)
9294 move_it_vertically_backward (it, -dy);
9295 else
9297 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9298 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9299 MOVE_TO_POS | MOVE_TO_Y);
9300 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9302 /* If buffer ends in ZV without a newline, move to the start of
9303 the line to satisfy the post-condition. */
9304 if (IT_CHARPOS (*it) == ZV
9305 && ZV > BEGV
9306 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9307 move_it_by_lines (it, 0);
9312 /* Move iterator IT past the end of the text line it is in. */
9314 void
9315 move_it_past_eol (struct it *it)
9317 enum move_it_result rc;
9319 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9320 if (rc == MOVE_NEWLINE_OR_CR)
9321 set_iterator_to_next (it, 0);
9325 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9326 negative means move up. DVPOS == 0 means move to the start of the
9327 screen line.
9329 Optimization idea: If we would know that IT->f doesn't use
9330 a face with proportional font, we could be faster for
9331 truncate-lines nil. */
9333 void
9334 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9337 /* The commented-out optimization uses vmotion on terminals. This
9338 gives bad results, because elements like it->what, on which
9339 callers such as pos_visible_p rely, aren't updated. */
9340 /* struct position pos;
9341 if (!FRAME_WINDOW_P (it->f))
9343 struct text_pos textpos;
9345 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9346 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9347 reseat (it, textpos, 1);
9348 it->vpos += pos.vpos;
9349 it->current_y += pos.vpos;
9351 else */
9353 if (dvpos == 0)
9355 /* DVPOS == 0 means move to the start of the screen line. */
9356 move_it_vertically_backward (it, 0);
9357 /* Let next call to line_bottom_y calculate real line height */
9358 last_height = 0;
9360 else if (dvpos > 0)
9362 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9363 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9365 /* Only move to the next buffer position if we ended up in a
9366 string from display property, not in an overlay string
9367 (before-string or after-string). That is because the
9368 latter don't conceal the underlying buffer position, so
9369 we can ask to move the iterator to the exact position we
9370 are interested in. Note that, even if we are already at
9371 IT_CHARPOS (*it), the call below is not a no-op, as it
9372 will detect that we are at the end of the string, pop the
9373 iterator, and compute it->current_x and it->hpos
9374 correctly. */
9375 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9376 -1, -1, -1, MOVE_TO_POS);
9379 else
9381 struct it it2;
9382 void *it2data = NULL;
9383 ptrdiff_t start_charpos, i;
9384 int nchars_per_row
9385 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9386 ptrdiff_t pos_limit;
9388 /* Start at the beginning of the screen line containing IT's
9389 position. This may actually move vertically backwards,
9390 in case of overlays, so adjust dvpos accordingly. */
9391 dvpos += it->vpos;
9392 move_it_vertically_backward (it, 0);
9393 dvpos -= it->vpos;
9395 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9396 screen lines, and reseat the iterator there. */
9397 start_charpos = IT_CHARPOS (*it);
9398 if (it->line_wrap == TRUNCATE)
9399 pos_limit = BEGV;
9400 else
9401 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9402 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9403 back_to_previous_visible_line_start (it);
9404 reseat (it, it->current.pos, 1);
9406 /* Move further back if we end up in a string or an image. */
9407 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9409 /* First try to move to start of display line. */
9410 dvpos += it->vpos;
9411 move_it_vertically_backward (it, 0);
9412 dvpos -= it->vpos;
9413 if (IT_POS_VALID_AFTER_MOVE_P (it))
9414 break;
9415 /* If start of line is still in string or image,
9416 move further back. */
9417 back_to_previous_visible_line_start (it);
9418 reseat (it, it->current.pos, 1);
9419 dvpos--;
9422 it->current_x = it->hpos = 0;
9424 /* Above call may have moved too far if continuation lines
9425 are involved. Scan forward and see if it did. */
9426 SAVE_IT (it2, *it, it2data);
9427 it2.vpos = it2.current_y = 0;
9428 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9429 it->vpos -= it2.vpos;
9430 it->current_y -= it2.current_y;
9431 it->current_x = it->hpos = 0;
9433 /* If we moved too far back, move IT some lines forward. */
9434 if (it2.vpos > -dvpos)
9436 int delta = it2.vpos + dvpos;
9438 RESTORE_IT (&it2, &it2, it2data);
9439 SAVE_IT (it2, *it, it2data);
9440 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9441 /* Move back again if we got too far ahead. */
9442 if (IT_CHARPOS (*it) >= start_charpos)
9443 RESTORE_IT (it, &it2, it2data);
9444 else
9445 bidi_unshelve_cache (it2data, 1);
9447 else
9448 RESTORE_IT (it, it, it2data);
9452 /* Return 1 if IT points into the middle of a display vector. */
9455 in_display_vector_p (struct it *it)
9457 return (it->method == GET_FROM_DISPLAY_VECTOR
9458 && it->current.dpvec_index > 0
9459 && it->dpvec + it->current.dpvec_index != it->dpend);
9463 /***********************************************************************
9464 Messages
9465 ***********************************************************************/
9468 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9469 to *Messages*. */
9471 void
9472 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9474 Lisp_Object args[3];
9475 Lisp_Object msg, fmt;
9476 char *buffer;
9477 ptrdiff_t len;
9478 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9479 USE_SAFE_ALLOCA;
9481 fmt = msg = Qnil;
9482 GCPRO4 (fmt, msg, arg1, arg2);
9484 args[0] = fmt = build_string (format);
9485 args[1] = arg1;
9486 args[2] = arg2;
9487 msg = Fformat (3, args);
9489 len = SBYTES (msg) + 1;
9490 buffer = SAFE_ALLOCA (len);
9491 memcpy (buffer, SDATA (msg), len);
9493 message_dolog (buffer, len - 1, 1, 0);
9494 SAFE_FREE ();
9496 UNGCPRO;
9500 /* Output a newline in the *Messages* buffer if "needs" one. */
9502 void
9503 message_log_maybe_newline (void)
9505 if (message_log_need_newline)
9506 message_dolog ("", 0, 1, 0);
9510 /* Add a string M of length NBYTES to the message log, optionally
9511 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9512 true, means interpret the contents of M as multibyte. This
9513 function calls low-level routines in order to bypass text property
9514 hooks, etc. which might not be safe to run.
9516 This may GC (insert may run before/after change hooks),
9517 so the buffer M must NOT point to a Lisp string. */
9519 void
9520 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9522 const unsigned char *msg = (const unsigned char *) m;
9524 if (!NILP (Vmemory_full))
9525 return;
9527 if (!NILP (Vmessage_log_max))
9529 struct buffer *oldbuf;
9530 Lisp_Object oldpoint, oldbegv, oldzv;
9531 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9532 ptrdiff_t point_at_end = 0;
9533 ptrdiff_t zv_at_end = 0;
9534 Lisp_Object old_deactivate_mark;
9535 bool shown;
9536 struct gcpro gcpro1;
9538 old_deactivate_mark = Vdeactivate_mark;
9539 oldbuf = current_buffer;
9541 /* Ensure the Messages buffer exists, and switch to it.
9542 If we created it, set the major-mode. */
9544 int newbuffer = 0;
9545 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9547 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9549 if (newbuffer &&
9550 !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9551 call0 (intern ("messages-buffer-mode"));
9554 bset_undo_list (current_buffer, Qt);
9556 oldpoint = message_dolog_marker1;
9557 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9558 oldbegv = message_dolog_marker2;
9559 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9560 oldzv = message_dolog_marker3;
9561 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9562 GCPRO1 (old_deactivate_mark);
9564 if (PT == Z)
9565 point_at_end = 1;
9566 if (ZV == Z)
9567 zv_at_end = 1;
9569 BEGV = BEG;
9570 BEGV_BYTE = BEG_BYTE;
9571 ZV = Z;
9572 ZV_BYTE = Z_BYTE;
9573 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9575 /* Insert the string--maybe converting multibyte to single byte
9576 or vice versa, so that all the text fits the buffer. */
9577 if (multibyte
9578 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9580 ptrdiff_t i;
9581 int c, char_bytes;
9582 char work[1];
9584 /* Convert a multibyte string to single-byte
9585 for the *Message* buffer. */
9586 for (i = 0; i < nbytes; i += char_bytes)
9588 c = string_char_and_length (msg + i, &char_bytes);
9589 work[0] = (ASCII_CHAR_P (c)
9591 : multibyte_char_to_unibyte (c));
9592 insert_1_both (work, 1, 1, 1, 0, 0);
9595 else if (! multibyte
9596 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9598 ptrdiff_t i;
9599 int c, char_bytes;
9600 unsigned char str[MAX_MULTIBYTE_LENGTH];
9601 /* Convert a single-byte string to multibyte
9602 for the *Message* buffer. */
9603 for (i = 0; i < nbytes; i++)
9605 c = msg[i];
9606 MAKE_CHAR_MULTIBYTE (c);
9607 char_bytes = CHAR_STRING (c, str);
9608 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9611 else if (nbytes)
9612 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9614 if (nlflag)
9616 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9617 printmax_t dups;
9619 insert_1_both ("\n", 1, 1, 1, 0, 0);
9621 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9622 this_bol = PT;
9623 this_bol_byte = PT_BYTE;
9625 /* See if this line duplicates the previous one.
9626 If so, combine duplicates. */
9627 if (this_bol > BEG)
9629 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9630 prev_bol = PT;
9631 prev_bol_byte = PT_BYTE;
9633 dups = message_log_check_duplicate (prev_bol_byte,
9634 this_bol_byte);
9635 if (dups)
9637 del_range_both (prev_bol, prev_bol_byte,
9638 this_bol, this_bol_byte, 0);
9639 if (dups > 1)
9641 char dupstr[sizeof " [ times]"
9642 + INT_STRLEN_BOUND (printmax_t)];
9644 /* If you change this format, don't forget to also
9645 change message_log_check_duplicate. */
9646 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9647 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9648 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9653 /* If we have more than the desired maximum number of lines
9654 in the *Messages* buffer now, delete the oldest ones.
9655 This is safe because we don't have undo in this buffer. */
9657 if (NATNUMP (Vmessage_log_max))
9659 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9660 -XFASTINT (Vmessage_log_max) - 1, 0);
9661 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9664 BEGV = marker_position (oldbegv);
9665 BEGV_BYTE = marker_byte_position (oldbegv);
9667 if (zv_at_end)
9669 ZV = Z;
9670 ZV_BYTE = Z_BYTE;
9672 else
9674 ZV = marker_position (oldzv);
9675 ZV_BYTE = marker_byte_position (oldzv);
9678 if (point_at_end)
9679 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9680 else
9681 /* We can't do Fgoto_char (oldpoint) because it will run some
9682 Lisp code. */
9683 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9684 marker_byte_position (oldpoint));
9686 UNGCPRO;
9687 unchain_marker (XMARKER (oldpoint));
9688 unchain_marker (XMARKER (oldbegv));
9689 unchain_marker (XMARKER (oldzv));
9691 shown = buffer_window_count (current_buffer) > 0;
9692 set_buffer_internal (oldbuf);
9693 /* We called insert_1_both above with its 5th argument (PREPARE)
9694 zero, which prevents insert_1_both from calling
9695 prepare_to_modify_buffer, which in turns prevents us from
9696 incrementing windows_or_buffers_changed even if *Messages* is
9697 shown in some window. So we must manually incrementing
9698 windows_or_buffers_changed here to make up for that. */
9699 if (shown)
9700 windows_or_buffers_changed++;
9701 else
9702 windows_or_buffers_changed = old_windows_or_buffers_changed;
9703 message_log_need_newline = !nlflag;
9704 Vdeactivate_mark = old_deactivate_mark;
9709 /* We are at the end of the buffer after just having inserted a newline.
9710 (Note: We depend on the fact we won't be crossing the gap.)
9711 Check to see if the most recent message looks a lot like the previous one.
9712 Return 0 if different, 1 if the new one should just replace it, or a
9713 value N > 1 if we should also append " [N times]". */
9715 static intmax_t
9716 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9718 ptrdiff_t i;
9719 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9720 int seen_dots = 0;
9721 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9722 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9724 for (i = 0; i < len; i++)
9726 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9727 seen_dots = 1;
9728 if (p1[i] != p2[i])
9729 return seen_dots;
9731 p1 += len;
9732 if (*p1 == '\n')
9733 return 2;
9734 if (*p1++ == ' ' && *p1++ == '[')
9736 char *pend;
9737 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9738 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9739 return n + 1;
9741 return 0;
9745 /* Display an echo area message M with a specified length of NBYTES
9746 bytes. The string may include null characters. If M is not a
9747 string, clear out any existing message, and let the mini-buffer
9748 text show through.
9750 This function cancels echoing. */
9752 void
9753 message3 (Lisp_Object m)
9755 struct gcpro gcpro1;
9757 GCPRO1 (m);
9758 clear_message (1,1);
9759 cancel_echoing ();
9761 /* First flush out any partial line written with print. */
9762 message_log_maybe_newline ();
9763 if (STRINGP (m))
9765 ptrdiff_t nbytes = SBYTES (m);
9766 bool multibyte = STRING_MULTIBYTE (m);
9767 USE_SAFE_ALLOCA;
9768 char *buffer = SAFE_ALLOCA (nbytes);
9769 memcpy (buffer, SDATA (m), nbytes);
9770 message_dolog (buffer, nbytes, 1, multibyte);
9771 SAFE_FREE ();
9773 message3_nolog (m);
9775 UNGCPRO;
9779 /* The non-logging version of message3.
9780 This does not cancel echoing, because it is used for echoing.
9781 Perhaps we need to make a separate function for echoing
9782 and make this cancel echoing. */
9784 void
9785 message3_nolog (Lisp_Object m)
9787 struct frame *sf = SELECTED_FRAME ();
9789 if (FRAME_INITIAL_P (sf))
9791 if (noninteractive_need_newline)
9792 putc ('\n', stderr);
9793 noninteractive_need_newline = 0;
9794 if (STRINGP (m))
9795 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9796 if (cursor_in_echo_area == 0)
9797 fprintf (stderr, "\n");
9798 fflush (stderr);
9800 /* Error messages get reported properly by cmd_error, so this must be just an
9801 informative message; if the frame hasn't really been initialized yet, just
9802 toss it. */
9803 else if (INTERACTIVE && sf->glyphs_initialized_p)
9805 /* Get the frame containing the mini-buffer
9806 that the selected frame is using. */
9807 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9808 Lisp_Object frame = XWINDOW (mini_window)->frame;
9809 struct frame *f = XFRAME (frame);
9811 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9812 Fmake_frame_visible (frame);
9814 if (STRINGP (m) && SCHARS (m) > 0)
9816 set_message (m);
9817 if (minibuffer_auto_raise)
9818 Fraise_frame (frame);
9819 /* Assume we are not echoing.
9820 (If we are, echo_now will override this.) */
9821 echo_message_buffer = Qnil;
9823 else
9824 clear_message (1, 1);
9826 do_pending_window_change (0);
9827 echo_area_display (1);
9828 do_pending_window_change (0);
9829 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9830 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9835 /* Display a null-terminated echo area message M. If M is 0, clear
9836 out any existing message, and let the mini-buffer text show through.
9838 The buffer M must continue to exist until after the echo area gets
9839 cleared or some other message gets displayed there. Do not pass
9840 text that is stored in a Lisp string. Do not pass text in a buffer
9841 that was alloca'd. */
9843 void
9844 message1 (const char *m)
9846 message3 (m ? build_unibyte_string (m) : Qnil);
9850 /* The non-logging counterpart of message1. */
9852 void
9853 message1_nolog (const char *m)
9855 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9858 /* Display a message M which contains a single %s
9859 which gets replaced with STRING. */
9861 void
9862 message_with_string (const char *m, Lisp_Object string, int log)
9864 CHECK_STRING (string);
9866 if (noninteractive)
9868 if (m)
9870 if (noninteractive_need_newline)
9871 putc ('\n', stderr);
9872 noninteractive_need_newline = 0;
9873 fprintf (stderr, m, SDATA (string));
9874 if (!cursor_in_echo_area)
9875 fprintf (stderr, "\n");
9876 fflush (stderr);
9879 else if (INTERACTIVE)
9881 /* The frame whose minibuffer we're going to display the message on.
9882 It may be larger than the selected frame, so we need
9883 to use its buffer, not the selected frame's buffer. */
9884 Lisp_Object mini_window;
9885 struct frame *f, *sf = SELECTED_FRAME ();
9887 /* Get the frame containing the minibuffer
9888 that the selected frame is using. */
9889 mini_window = FRAME_MINIBUF_WINDOW (sf);
9890 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9892 /* Error messages get reported properly by cmd_error, so this must be
9893 just an informative message; if the frame hasn't really been
9894 initialized yet, just toss it. */
9895 if (f->glyphs_initialized_p)
9897 Lisp_Object args[2], msg;
9898 struct gcpro gcpro1, gcpro2;
9900 args[0] = build_string (m);
9901 args[1] = msg = string;
9902 GCPRO2 (args[0], msg);
9903 gcpro1.nvars = 2;
9905 msg = Fformat (2, args);
9907 if (log)
9908 message3 (msg);
9909 else
9910 message3_nolog (msg);
9912 UNGCPRO;
9914 /* Print should start at the beginning of the message
9915 buffer next time. */
9916 message_buf_print = 0;
9922 /* Dump an informative message to the minibuf. If M is 0, clear out
9923 any existing message, and let the mini-buffer text show through. */
9925 static void
9926 vmessage (const char *m, va_list ap)
9928 if (noninteractive)
9930 if (m)
9932 if (noninteractive_need_newline)
9933 putc ('\n', stderr);
9934 noninteractive_need_newline = 0;
9935 vfprintf (stderr, m, ap);
9936 if (cursor_in_echo_area == 0)
9937 fprintf (stderr, "\n");
9938 fflush (stderr);
9941 else if (INTERACTIVE)
9943 /* The frame whose mini-buffer we're going to display the message
9944 on. It may be larger than the selected frame, so we need to
9945 use its buffer, not the selected frame's buffer. */
9946 Lisp_Object mini_window;
9947 struct frame *f, *sf = SELECTED_FRAME ();
9949 /* Get the frame containing the mini-buffer
9950 that the selected frame is using. */
9951 mini_window = FRAME_MINIBUF_WINDOW (sf);
9952 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9954 /* Error messages get reported properly by cmd_error, so this must be
9955 just an informative message; if the frame hasn't really been
9956 initialized yet, just toss it. */
9957 if (f->glyphs_initialized_p)
9959 if (m)
9961 ptrdiff_t len;
9962 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9963 char *message_buf = alloca (maxsize + 1);
9965 len = doprnt (message_buf, maxsize, m, 0, ap);
9967 message3 (make_string (message_buf, len));
9969 else
9970 message1 (0);
9972 /* Print should start at the beginning of the message
9973 buffer next time. */
9974 message_buf_print = 0;
9979 void
9980 message (const char *m, ...)
9982 va_list ap;
9983 va_start (ap, m);
9984 vmessage (m, ap);
9985 va_end (ap);
9989 #if 0
9990 /* The non-logging version of message. */
9992 void
9993 message_nolog (const char *m, ...)
9995 Lisp_Object old_log_max;
9996 va_list ap;
9997 va_start (ap, m);
9998 old_log_max = Vmessage_log_max;
9999 Vmessage_log_max = Qnil;
10000 vmessage (m, ap);
10001 Vmessage_log_max = old_log_max;
10002 va_end (ap);
10004 #endif
10007 /* Display the current message in the current mini-buffer. This is
10008 only called from error handlers in process.c, and is not time
10009 critical. */
10011 void
10012 update_echo_area (void)
10014 if (!NILP (echo_area_buffer[0]))
10016 Lisp_Object string;
10017 string = Fcurrent_message ();
10018 message3 (string);
10023 /* Make sure echo area buffers in `echo_buffers' are live.
10024 If they aren't, make new ones. */
10026 static void
10027 ensure_echo_area_buffers (void)
10029 int i;
10031 for (i = 0; i < 2; ++i)
10032 if (!BUFFERP (echo_buffer[i])
10033 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10035 char name[30];
10036 Lisp_Object old_buffer;
10037 int j;
10039 old_buffer = echo_buffer[i];
10040 echo_buffer[i] = Fget_buffer_create
10041 (make_formatted_string (name, " *Echo Area %d*", i));
10042 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10043 /* to force word wrap in echo area -
10044 it was decided to postpone this*/
10045 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10047 for (j = 0; j < 2; ++j)
10048 if (EQ (old_buffer, echo_area_buffer[j]))
10049 echo_area_buffer[j] = echo_buffer[i];
10054 /* Call FN with args A1..A2 with either the current or last displayed
10055 echo_area_buffer as current buffer.
10057 WHICH zero means use the current message buffer
10058 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10059 from echo_buffer[] and clear it.
10061 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10062 suitable buffer from echo_buffer[] and clear it.
10064 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10065 that the current message becomes the last displayed one, make
10066 choose a suitable buffer for echo_area_buffer[0], and clear it.
10068 Value is what FN returns. */
10070 static int
10071 with_echo_area_buffer (struct window *w, int which,
10072 int (*fn) (ptrdiff_t, Lisp_Object),
10073 ptrdiff_t a1, Lisp_Object a2)
10075 Lisp_Object buffer;
10076 int this_one, the_other, clear_buffer_p, rc;
10077 ptrdiff_t count = SPECPDL_INDEX ();
10079 /* If buffers aren't live, make new ones. */
10080 ensure_echo_area_buffers ();
10082 clear_buffer_p = 0;
10084 if (which == 0)
10085 this_one = 0, the_other = 1;
10086 else if (which > 0)
10087 this_one = 1, the_other = 0;
10088 else
10090 this_one = 0, the_other = 1;
10091 clear_buffer_p = 1;
10093 /* We need a fresh one in case the current echo buffer equals
10094 the one containing the last displayed echo area message. */
10095 if (!NILP (echo_area_buffer[this_one])
10096 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10097 echo_area_buffer[this_one] = Qnil;
10100 /* Choose a suitable buffer from echo_buffer[] is we don't
10101 have one. */
10102 if (NILP (echo_area_buffer[this_one]))
10104 echo_area_buffer[this_one]
10105 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10106 ? echo_buffer[the_other]
10107 : echo_buffer[this_one]);
10108 clear_buffer_p = 1;
10111 buffer = echo_area_buffer[this_one];
10113 /* Don't get confused by reusing the buffer used for echoing
10114 for a different purpose. */
10115 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10116 cancel_echoing ();
10118 record_unwind_protect (unwind_with_echo_area_buffer,
10119 with_echo_area_buffer_unwind_data (w));
10121 /* Make the echo area buffer current. Note that for display
10122 purposes, it is not necessary that the displayed window's buffer
10123 == current_buffer, except for text property lookup. So, let's
10124 only set that buffer temporarily here without doing a full
10125 Fset_window_buffer. We must also change w->pointm, though,
10126 because otherwise an assertions in unshow_buffer fails, and Emacs
10127 aborts. */
10128 set_buffer_internal_1 (XBUFFER (buffer));
10129 if (w)
10131 wset_buffer (w, buffer);
10132 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10135 bset_undo_list (current_buffer, Qt);
10136 bset_read_only (current_buffer, Qnil);
10137 specbind (Qinhibit_read_only, Qt);
10138 specbind (Qinhibit_modification_hooks, Qt);
10140 if (clear_buffer_p && Z > BEG)
10141 del_range (BEG, Z);
10143 eassert (BEGV >= BEG);
10144 eassert (ZV <= Z && ZV >= BEGV);
10146 rc = fn (a1, a2);
10148 eassert (BEGV >= BEG);
10149 eassert (ZV <= Z && ZV >= BEGV);
10151 unbind_to (count, Qnil);
10152 return rc;
10156 /* Save state that should be preserved around the call to the function
10157 FN called in with_echo_area_buffer. */
10159 static Lisp_Object
10160 with_echo_area_buffer_unwind_data (struct window *w)
10162 int i = 0;
10163 Lisp_Object vector, tmp;
10165 /* Reduce consing by keeping one vector in
10166 Vwith_echo_area_save_vector. */
10167 vector = Vwith_echo_area_save_vector;
10168 Vwith_echo_area_save_vector = Qnil;
10170 if (NILP (vector))
10171 vector = Fmake_vector (make_number (9), Qnil);
10173 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10174 ASET (vector, i, Vdeactivate_mark); ++i;
10175 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10177 if (w)
10179 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10180 ASET (vector, i, w->contents); ++i;
10181 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10182 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10183 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10184 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10186 else
10188 int end = i + 6;
10189 for (; i < end; ++i)
10190 ASET (vector, i, Qnil);
10193 eassert (i == ASIZE (vector));
10194 return vector;
10198 /* Restore global state from VECTOR which was created by
10199 with_echo_area_buffer_unwind_data. */
10201 static void
10202 unwind_with_echo_area_buffer (Lisp_Object vector)
10204 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10205 Vdeactivate_mark = AREF (vector, 1);
10206 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10208 if (WINDOWP (AREF (vector, 3)))
10210 struct window *w;
10211 Lisp_Object buffer;
10213 w = XWINDOW (AREF (vector, 3));
10214 buffer = AREF (vector, 4);
10216 wset_buffer (w, buffer);
10217 set_marker_both (w->pointm, buffer,
10218 XFASTINT (AREF (vector, 5)),
10219 XFASTINT (AREF (vector, 6)));
10220 set_marker_both (w->start, buffer,
10221 XFASTINT (AREF (vector, 7)),
10222 XFASTINT (AREF (vector, 8)));
10225 Vwith_echo_area_save_vector = vector;
10229 /* Set up the echo area for use by print functions. MULTIBYTE_P
10230 non-zero means we will print multibyte. */
10232 void
10233 setup_echo_area_for_printing (int multibyte_p)
10235 /* If we can't find an echo area any more, exit. */
10236 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10237 Fkill_emacs (Qnil);
10239 ensure_echo_area_buffers ();
10241 if (!message_buf_print)
10243 /* A message has been output since the last time we printed.
10244 Choose a fresh echo area buffer. */
10245 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10246 echo_area_buffer[0] = echo_buffer[1];
10247 else
10248 echo_area_buffer[0] = echo_buffer[0];
10250 /* Switch to that buffer and clear it. */
10251 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10252 bset_truncate_lines (current_buffer, Qnil);
10254 if (Z > BEG)
10256 ptrdiff_t count = SPECPDL_INDEX ();
10257 specbind (Qinhibit_read_only, Qt);
10258 /* Note that undo recording is always disabled. */
10259 del_range (BEG, Z);
10260 unbind_to (count, Qnil);
10262 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10264 /* Set up the buffer for the multibyteness we need. */
10265 if (multibyte_p
10266 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10267 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10269 /* Raise the frame containing the echo area. */
10270 if (minibuffer_auto_raise)
10272 struct frame *sf = SELECTED_FRAME ();
10273 Lisp_Object mini_window;
10274 mini_window = FRAME_MINIBUF_WINDOW (sf);
10275 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10278 message_log_maybe_newline ();
10279 message_buf_print = 1;
10281 else
10283 if (NILP (echo_area_buffer[0]))
10285 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10286 echo_area_buffer[0] = echo_buffer[1];
10287 else
10288 echo_area_buffer[0] = echo_buffer[0];
10291 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10293 /* Someone switched buffers between print requests. */
10294 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10295 bset_truncate_lines (current_buffer, Qnil);
10301 /* Display an echo area message in window W. Value is non-zero if W's
10302 height is changed. If display_last_displayed_message_p is
10303 non-zero, display the message that was last displayed, otherwise
10304 display the current message. */
10306 static int
10307 display_echo_area (struct window *w)
10309 int i, no_message_p, window_height_changed_p;
10311 /* Temporarily disable garbage collections while displaying the echo
10312 area. This is done because a GC can print a message itself.
10313 That message would modify the echo area buffer's contents while a
10314 redisplay of the buffer is going on, and seriously confuse
10315 redisplay. */
10316 ptrdiff_t count = inhibit_garbage_collection ();
10318 /* If there is no message, we must call display_echo_area_1
10319 nevertheless because it resizes the window. But we will have to
10320 reset the echo_area_buffer in question to nil at the end because
10321 with_echo_area_buffer will sets it to an empty buffer. */
10322 i = display_last_displayed_message_p ? 1 : 0;
10323 no_message_p = NILP (echo_area_buffer[i]);
10325 window_height_changed_p
10326 = with_echo_area_buffer (w, display_last_displayed_message_p,
10327 display_echo_area_1,
10328 (intptr_t) w, Qnil);
10330 if (no_message_p)
10331 echo_area_buffer[i] = Qnil;
10333 unbind_to (count, Qnil);
10334 return window_height_changed_p;
10338 /* Helper for display_echo_area. Display the current buffer which
10339 contains the current echo area message in window W, a mini-window,
10340 a pointer to which is passed in A1. A2..A4 are currently not used.
10341 Change the height of W so that all of the message is displayed.
10342 Value is non-zero if height of W was changed. */
10344 static int
10345 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10347 intptr_t i1 = a1;
10348 struct window *w = (struct window *) i1;
10349 Lisp_Object window;
10350 struct text_pos start;
10351 int window_height_changed_p = 0;
10353 /* Do this before displaying, so that we have a large enough glyph
10354 matrix for the display. If we can't get enough space for the
10355 whole text, display the last N lines. That works by setting w->start. */
10356 window_height_changed_p = resize_mini_window (w, 0);
10358 /* Use the starting position chosen by resize_mini_window. */
10359 SET_TEXT_POS_FROM_MARKER (start, w->start);
10361 /* Display. */
10362 clear_glyph_matrix (w->desired_matrix);
10363 XSETWINDOW (window, w);
10364 try_window (window, start, 0);
10366 return window_height_changed_p;
10370 /* Resize the echo area window to exactly the size needed for the
10371 currently displayed message, if there is one. If a mini-buffer
10372 is active, don't shrink it. */
10374 void
10375 resize_echo_area_exactly (void)
10377 if (BUFFERP (echo_area_buffer[0])
10378 && WINDOWP (echo_area_window))
10380 struct window *w = XWINDOW (echo_area_window);
10381 int resized_p;
10382 Lisp_Object resize_exactly;
10384 if (minibuf_level == 0)
10385 resize_exactly = Qt;
10386 else
10387 resize_exactly = Qnil;
10389 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10390 (intptr_t) w, resize_exactly);
10391 if (resized_p)
10393 ++windows_or_buffers_changed;
10394 ++update_mode_lines;
10395 redisplay_internal ();
10401 /* Callback function for with_echo_area_buffer, when used from
10402 resize_echo_area_exactly. A1 contains a pointer to the window to
10403 resize, EXACTLY non-nil means resize the mini-window exactly to the
10404 size of the text displayed. A3 and A4 are not used. Value is what
10405 resize_mini_window returns. */
10407 static int
10408 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10410 intptr_t i1 = a1;
10411 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10415 /* Resize mini-window W to fit the size of its contents. EXACT_P
10416 means size the window exactly to the size needed. Otherwise, it's
10417 only enlarged until W's buffer is empty.
10419 Set W->start to the right place to begin display. If the whole
10420 contents fit, start at the beginning. Otherwise, start so as
10421 to make the end of the contents appear. This is particularly
10422 important for y-or-n-p, but seems desirable generally.
10424 Value is non-zero if the window height has been changed. */
10427 resize_mini_window (struct window *w, int exact_p)
10429 struct frame *f = XFRAME (w->frame);
10430 int window_height_changed_p = 0;
10432 eassert (MINI_WINDOW_P (w));
10434 /* By default, start display at the beginning. */
10435 set_marker_both (w->start, w->contents,
10436 BUF_BEGV (XBUFFER (w->contents)),
10437 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10439 /* Don't resize windows while redisplaying a window; it would
10440 confuse redisplay functions when the size of the window they are
10441 displaying changes from under them. Such a resizing can happen,
10442 for instance, when which-func prints a long message while
10443 we are running fontification-functions. We're running these
10444 functions with safe_call which binds inhibit-redisplay to t. */
10445 if (!NILP (Vinhibit_redisplay))
10446 return 0;
10448 /* Nil means don't try to resize. */
10449 if (NILP (Vresize_mini_windows)
10450 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10451 return 0;
10453 if (!FRAME_MINIBUF_ONLY_P (f))
10455 struct it it;
10456 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10457 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10458 int height;
10459 EMACS_INT max_height;
10460 int unit = FRAME_LINE_HEIGHT (f);
10461 struct text_pos start;
10462 struct buffer *old_current_buffer = NULL;
10464 if (current_buffer != XBUFFER (w->contents))
10466 old_current_buffer = current_buffer;
10467 set_buffer_internal (XBUFFER (w->contents));
10470 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10472 /* Compute the max. number of lines specified by the user. */
10473 if (FLOATP (Vmax_mini_window_height))
10474 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10475 else if (INTEGERP (Vmax_mini_window_height))
10476 max_height = XINT (Vmax_mini_window_height);
10477 else
10478 max_height = total_height / 4;
10480 /* Correct that max. height if it's bogus. */
10481 max_height = clip_to_bounds (1, max_height, total_height);
10483 /* Find out the height of the text in the window. */
10484 if (it.line_wrap == TRUNCATE)
10485 height = 1;
10486 else
10488 last_height = 0;
10489 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10490 if (it.max_ascent == 0 && it.max_descent == 0)
10491 height = it.current_y + last_height;
10492 else
10493 height = it.current_y + it.max_ascent + it.max_descent;
10494 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10495 height = (height + unit - 1) / unit;
10498 /* Compute a suitable window start. */
10499 if (height > max_height)
10501 height = max_height;
10502 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10503 move_it_vertically_backward (&it, (height - 1) * unit);
10504 start = it.current.pos;
10506 else
10507 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10508 SET_MARKER_FROM_TEXT_POS (w->start, start);
10510 if (EQ (Vresize_mini_windows, Qgrow_only))
10512 /* Let it grow only, until we display an empty message, in which
10513 case the window shrinks again. */
10514 if (height > WINDOW_TOTAL_LINES (w))
10516 int old_height = WINDOW_TOTAL_LINES (w);
10518 FRAME_WINDOWS_FROZEN (f) = 1;
10519 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10520 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10522 else if (height < WINDOW_TOTAL_LINES (w)
10523 && (exact_p || BEGV == ZV))
10525 int old_height = WINDOW_TOTAL_LINES (w);
10527 FRAME_WINDOWS_FROZEN (f) = 0;
10528 shrink_mini_window (w);
10529 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10532 else
10534 /* Always resize to exact size needed. */
10535 if (height > WINDOW_TOTAL_LINES (w))
10537 int old_height = WINDOW_TOTAL_LINES (w);
10539 FRAME_WINDOWS_FROZEN (f) = 1;
10540 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10541 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10543 else if (height < WINDOW_TOTAL_LINES (w))
10545 int old_height = WINDOW_TOTAL_LINES (w);
10547 FRAME_WINDOWS_FROZEN (f) = 0;
10548 shrink_mini_window (w);
10550 if (height)
10552 FRAME_WINDOWS_FROZEN (f) = 1;
10553 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10556 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10560 if (old_current_buffer)
10561 set_buffer_internal (old_current_buffer);
10564 return window_height_changed_p;
10568 /* Value is the current message, a string, or nil if there is no
10569 current message. */
10571 Lisp_Object
10572 current_message (void)
10574 Lisp_Object msg;
10576 if (!BUFFERP (echo_area_buffer[0]))
10577 msg = Qnil;
10578 else
10580 with_echo_area_buffer (0, 0, current_message_1,
10581 (intptr_t) &msg, Qnil);
10582 if (NILP (msg))
10583 echo_area_buffer[0] = Qnil;
10586 return msg;
10590 static int
10591 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10593 intptr_t i1 = a1;
10594 Lisp_Object *msg = (Lisp_Object *) i1;
10596 if (Z > BEG)
10597 *msg = make_buffer_string (BEG, Z, 1);
10598 else
10599 *msg = Qnil;
10600 return 0;
10604 /* Push the current message on Vmessage_stack for later restoration
10605 by restore_message. Value is non-zero if the current message isn't
10606 empty. This is a relatively infrequent operation, so it's not
10607 worth optimizing. */
10609 bool
10610 push_message (void)
10612 Lisp_Object msg = current_message ();
10613 Vmessage_stack = Fcons (msg, Vmessage_stack);
10614 return STRINGP (msg);
10618 /* Restore message display from the top of Vmessage_stack. */
10620 void
10621 restore_message (void)
10623 eassert (CONSP (Vmessage_stack));
10624 message3_nolog (XCAR (Vmessage_stack));
10628 /* Handler for unwind-protect calling pop_message. */
10630 void
10631 pop_message_unwind (void)
10633 /* Pop the top-most entry off Vmessage_stack. */
10634 eassert (CONSP (Vmessage_stack));
10635 Vmessage_stack = XCDR (Vmessage_stack);
10639 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10640 exits. If the stack is not empty, we have a missing pop_message
10641 somewhere. */
10643 void
10644 check_message_stack (void)
10646 if (!NILP (Vmessage_stack))
10647 emacs_abort ();
10651 /* Truncate to NCHARS what will be displayed in the echo area the next
10652 time we display it---but don't redisplay it now. */
10654 void
10655 truncate_echo_area (ptrdiff_t nchars)
10657 if (nchars == 0)
10658 echo_area_buffer[0] = Qnil;
10659 else if (!noninteractive
10660 && INTERACTIVE
10661 && !NILP (echo_area_buffer[0]))
10663 struct frame *sf = SELECTED_FRAME ();
10664 /* Error messages get reported properly by cmd_error, so this must be
10665 just an informative message; if the frame hasn't really been
10666 initialized yet, just toss it. */
10667 if (sf->glyphs_initialized_p)
10668 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10673 /* Helper function for truncate_echo_area. Truncate the current
10674 message to at most NCHARS characters. */
10676 static int
10677 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10679 if (BEG + nchars < Z)
10680 del_range (BEG + nchars, Z);
10681 if (Z == BEG)
10682 echo_area_buffer[0] = Qnil;
10683 return 0;
10686 /* Set the current message to STRING. */
10688 static void
10689 set_message (Lisp_Object string)
10691 eassert (STRINGP (string));
10693 message_enable_multibyte = STRING_MULTIBYTE (string);
10695 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10696 message_buf_print = 0;
10697 help_echo_showing_p = 0;
10699 if (STRINGP (Vdebug_on_message)
10700 && STRINGP (string)
10701 && fast_string_match (Vdebug_on_message, string) >= 0)
10702 call_debugger (list2 (Qerror, string));
10706 /* Helper function for set_message. First argument is ignored and second
10707 argument has the same meaning as for set_message.
10708 This function is called with the echo area buffer being current. */
10710 static int
10711 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10713 eassert (STRINGP (string));
10715 /* Change multibyteness of the echo buffer appropriately. */
10716 if (message_enable_multibyte
10717 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10718 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10720 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10721 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10722 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10724 /* Insert new message at BEG. */
10725 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10727 /* This function takes care of single/multibyte conversion.
10728 We just have to ensure that the echo area buffer has the right
10729 setting of enable_multibyte_characters. */
10730 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10732 return 0;
10736 /* Clear messages. CURRENT_P non-zero means clear the current
10737 message. LAST_DISPLAYED_P non-zero means clear the message
10738 last displayed. */
10740 void
10741 clear_message (int current_p, int last_displayed_p)
10743 if (current_p)
10745 echo_area_buffer[0] = Qnil;
10746 message_cleared_p = 1;
10749 if (last_displayed_p)
10750 echo_area_buffer[1] = Qnil;
10752 message_buf_print = 0;
10755 /* Clear garbaged frames.
10757 This function is used where the old redisplay called
10758 redraw_garbaged_frames which in turn called redraw_frame which in
10759 turn called clear_frame. The call to clear_frame was a source of
10760 flickering. I believe a clear_frame is not necessary. It should
10761 suffice in the new redisplay to invalidate all current matrices,
10762 and ensure a complete redisplay of all windows. */
10764 static void
10765 clear_garbaged_frames (void)
10767 if (frame_garbaged)
10769 Lisp_Object tail, frame;
10770 int changed_count = 0;
10772 FOR_EACH_FRAME (tail, frame)
10774 struct frame *f = XFRAME (frame);
10776 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10778 if (f->resized_p)
10779 redraw_frame (f);
10780 else
10781 clear_current_matrices (f);
10782 changed_count++;
10783 f->garbaged = 0;
10784 f->resized_p = 0;
10788 frame_garbaged = 0;
10789 if (changed_count)
10790 ++windows_or_buffers_changed;
10795 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10796 is non-zero update selected_frame. Value is non-zero if the
10797 mini-windows height has been changed. */
10799 static int
10800 echo_area_display (int update_frame_p)
10802 Lisp_Object mini_window;
10803 struct window *w;
10804 struct frame *f;
10805 int window_height_changed_p = 0;
10806 struct frame *sf = SELECTED_FRAME ();
10808 mini_window = FRAME_MINIBUF_WINDOW (sf);
10809 w = XWINDOW (mini_window);
10810 f = XFRAME (WINDOW_FRAME (w));
10812 /* Don't display if frame is invisible or not yet initialized. */
10813 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10814 return 0;
10816 #ifdef HAVE_WINDOW_SYSTEM
10817 /* When Emacs starts, selected_frame may be the initial terminal
10818 frame. If we let this through, a message would be displayed on
10819 the terminal. */
10820 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10821 return 0;
10822 #endif /* HAVE_WINDOW_SYSTEM */
10824 /* Redraw garbaged frames. */
10825 clear_garbaged_frames ();
10827 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10829 echo_area_window = mini_window;
10830 window_height_changed_p = display_echo_area (w);
10831 w->must_be_updated_p = 1;
10833 /* Update the display, unless called from redisplay_internal.
10834 Also don't update the screen during redisplay itself. The
10835 update will happen at the end of redisplay, and an update
10836 here could cause confusion. */
10837 if (update_frame_p && !redisplaying_p)
10839 int n = 0;
10841 /* If the display update has been interrupted by pending
10842 input, update mode lines in the frame. Due to the
10843 pending input, it might have been that redisplay hasn't
10844 been called, so that mode lines above the echo area are
10845 garbaged. This looks odd, so we prevent it here. */
10846 if (!display_completed)
10847 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10849 if (window_height_changed_p
10850 /* Don't do this if Emacs is shutting down. Redisplay
10851 needs to run hooks. */
10852 && !NILP (Vrun_hooks))
10854 /* Must update other windows. Likewise as in other
10855 cases, don't let this update be interrupted by
10856 pending input. */
10857 ptrdiff_t count = SPECPDL_INDEX ();
10858 specbind (Qredisplay_dont_pause, Qt);
10859 windows_or_buffers_changed = 1;
10860 redisplay_internal ();
10861 unbind_to (count, Qnil);
10863 else if (FRAME_WINDOW_P (f) && n == 0)
10865 /* Window configuration is the same as before.
10866 Can do with a display update of the echo area,
10867 unless we displayed some mode lines. */
10868 update_single_window (w, 1);
10869 flush_frame (f);
10871 else
10872 update_frame (f, 1, 1);
10874 /* If cursor is in the echo area, make sure that the next
10875 redisplay displays the minibuffer, so that the cursor will
10876 be replaced with what the minibuffer wants. */
10877 if (cursor_in_echo_area)
10878 ++windows_or_buffers_changed;
10881 else if (!EQ (mini_window, selected_window))
10882 windows_or_buffers_changed++;
10884 /* Last displayed message is now the current message. */
10885 echo_area_buffer[1] = echo_area_buffer[0];
10886 /* Inform read_char that we're not echoing. */
10887 echo_message_buffer = Qnil;
10889 /* Prevent redisplay optimization in redisplay_internal by resetting
10890 this_line_start_pos. This is done because the mini-buffer now
10891 displays the message instead of its buffer text. */
10892 if (EQ (mini_window, selected_window))
10893 CHARPOS (this_line_start_pos) = 0;
10895 return window_height_changed_p;
10898 /* Nonzero if the current window's buffer is shown in more than one
10899 window and was modified since last redisplay. */
10901 static int
10902 buffer_shared_and_changed (void)
10904 return (buffer_window_count (current_buffer) > 1
10905 && UNCHANGED_MODIFIED < MODIFF);
10908 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10909 is enabled and mark of W's buffer was changed since last W's update. */
10911 static int
10912 window_buffer_changed (struct window *w)
10914 struct buffer *b = XBUFFER (w->contents);
10916 eassert (BUFFER_LIVE_P (b));
10918 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10919 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10920 != (w->region_showing != 0)));
10923 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10925 static int
10926 mode_line_update_needed (struct window *w)
10928 return (w->column_number_displayed != -1
10929 && !(PT == w->last_point && !window_outdated (w))
10930 && (w->column_number_displayed != current_column ()));
10933 /* Nonzero if window start of W is frozen and may not be changed during
10934 redisplay. */
10936 static bool
10937 window_frozen_p (struct window *w)
10939 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10941 Lisp_Object window;
10943 XSETWINDOW (window, w);
10944 if (MINI_WINDOW_P (w))
10945 return 0;
10946 else if (EQ (window, selected_window))
10947 return 0;
10948 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10949 && EQ (window, Vminibuf_scroll_window))
10950 /* This special window can't be frozen too. */
10951 return 0;
10952 else
10953 return 1;
10955 return 0;
10958 /***********************************************************************
10959 Mode Lines and Frame Titles
10960 ***********************************************************************/
10962 /* A buffer for constructing non-propertized mode-line strings and
10963 frame titles in it; allocated from the heap in init_xdisp and
10964 resized as needed in store_mode_line_noprop_char. */
10966 static char *mode_line_noprop_buf;
10968 /* The buffer's end, and a current output position in it. */
10970 static char *mode_line_noprop_buf_end;
10971 static char *mode_line_noprop_ptr;
10973 #define MODE_LINE_NOPROP_LEN(start) \
10974 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10976 static enum {
10977 MODE_LINE_DISPLAY = 0,
10978 MODE_LINE_TITLE,
10979 MODE_LINE_NOPROP,
10980 MODE_LINE_STRING
10981 } mode_line_target;
10983 /* Alist that caches the results of :propertize.
10984 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10985 static Lisp_Object mode_line_proptrans_alist;
10987 /* List of strings making up the mode-line. */
10988 static Lisp_Object mode_line_string_list;
10990 /* Base face property when building propertized mode line string. */
10991 static Lisp_Object mode_line_string_face;
10992 static Lisp_Object mode_line_string_face_prop;
10995 /* Unwind data for mode line strings */
10997 static Lisp_Object Vmode_line_unwind_vector;
10999 static Lisp_Object
11000 format_mode_line_unwind_data (struct frame *target_frame,
11001 struct buffer *obuf,
11002 Lisp_Object owin,
11003 int save_proptrans)
11005 Lisp_Object vector, tmp;
11007 /* Reduce consing by keeping one vector in
11008 Vwith_echo_area_save_vector. */
11009 vector = Vmode_line_unwind_vector;
11010 Vmode_line_unwind_vector = Qnil;
11012 if (NILP (vector))
11013 vector = Fmake_vector (make_number (10), Qnil);
11015 ASET (vector, 0, make_number (mode_line_target));
11016 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11017 ASET (vector, 2, mode_line_string_list);
11018 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11019 ASET (vector, 4, mode_line_string_face);
11020 ASET (vector, 5, mode_line_string_face_prop);
11022 if (obuf)
11023 XSETBUFFER (tmp, obuf);
11024 else
11025 tmp = Qnil;
11026 ASET (vector, 6, tmp);
11027 ASET (vector, 7, owin);
11028 if (target_frame)
11030 /* Similarly to `with-selected-window', if the operation selects
11031 a window on another frame, we must restore that frame's
11032 selected window, and (for a tty) the top-frame. */
11033 ASET (vector, 8, target_frame->selected_window);
11034 if (FRAME_TERMCAP_P (target_frame))
11035 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11038 return vector;
11041 static void
11042 unwind_format_mode_line (Lisp_Object vector)
11044 Lisp_Object old_window = AREF (vector, 7);
11045 Lisp_Object target_frame_window = AREF (vector, 8);
11046 Lisp_Object old_top_frame = AREF (vector, 9);
11048 mode_line_target = XINT (AREF (vector, 0));
11049 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11050 mode_line_string_list = AREF (vector, 2);
11051 if (! EQ (AREF (vector, 3), Qt))
11052 mode_line_proptrans_alist = AREF (vector, 3);
11053 mode_line_string_face = AREF (vector, 4);
11054 mode_line_string_face_prop = AREF (vector, 5);
11056 /* Select window before buffer, since it may change the buffer. */
11057 if (!NILP (old_window))
11059 /* If the operation that we are unwinding had selected a window
11060 on a different frame, reset its frame-selected-window. For a
11061 text terminal, reset its top-frame if necessary. */
11062 if (!NILP (target_frame_window))
11064 Lisp_Object frame
11065 = WINDOW_FRAME (XWINDOW (target_frame_window));
11067 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11068 Fselect_window (target_frame_window, Qt);
11070 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11071 Fselect_frame (old_top_frame, Qt);
11074 Fselect_window (old_window, Qt);
11077 if (!NILP (AREF (vector, 6)))
11079 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11080 ASET (vector, 6, Qnil);
11083 Vmode_line_unwind_vector = vector;
11087 /* Store a single character C for the frame title in mode_line_noprop_buf.
11088 Re-allocate mode_line_noprop_buf if necessary. */
11090 static void
11091 store_mode_line_noprop_char (char c)
11093 /* If output position has reached the end of the allocated buffer,
11094 increase the buffer's size. */
11095 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11097 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11098 ptrdiff_t size = len;
11099 mode_line_noprop_buf =
11100 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11101 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11102 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11105 *mode_line_noprop_ptr++ = c;
11109 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11110 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11111 characters that yield more columns than PRECISION; PRECISION <= 0
11112 means copy the whole string. Pad with spaces until FIELD_WIDTH
11113 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11114 pad. Called from display_mode_element when it is used to build a
11115 frame title. */
11117 static int
11118 store_mode_line_noprop (const char *string, int field_width, int precision)
11120 const unsigned char *str = (const unsigned char *) string;
11121 int n = 0;
11122 ptrdiff_t dummy, nbytes;
11124 /* Copy at most PRECISION chars from STR. */
11125 nbytes = strlen (string);
11126 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11127 while (nbytes--)
11128 store_mode_line_noprop_char (*str++);
11130 /* Fill up with spaces until FIELD_WIDTH reached. */
11131 while (field_width > 0
11132 && n < field_width)
11134 store_mode_line_noprop_char (' ');
11135 ++n;
11138 return n;
11141 /***********************************************************************
11142 Frame Titles
11143 ***********************************************************************/
11145 #ifdef HAVE_WINDOW_SYSTEM
11147 /* Set the title of FRAME, if it has changed. The title format is
11148 Vicon_title_format if FRAME is iconified, otherwise it is
11149 frame_title_format. */
11151 static void
11152 x_consider_frame_title (Lisp_Object frame)
11154 struct frame *f = XFRAME (frame);
11156 if (FRAME_WINDOW_P (f)
11157 || FRAME_MINIBUF_ONLY_P (f)
11158 || f->explicit_name)
11160 /* Do we have more than one visible frame on this X display? */
11161 Lisp_Object tail, other_frame, fmt;
11162 ptrdiff_t title_start;
11163 char *title;
11164 ptrdiff_t len;
11165 struct it it;
11166 ptrdiff_t count = SPECPDL_INDEX ();
11168 FOR_EACH_FRAME (tail, other_frame)
11170 struct frame *tf = XFRAME (other_frame);
11172 if (tf != f
11173 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11174 && !FRAME_MINIBUF_ONLY_P (tf)
11175 && !EQ (other_frame, tip_frame)
11176 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11177 break;
11180 /* Set global variable indicating that multiple frames exist. */
11181 multiple_frames = CONSP (tail);
11183 /* Switch to the buffer of selected window of the frame. Set up
11184 mode_line_target so that display_mode_element will output into
11185 mode_line_noprop_buf; then display the title. */
11186 record_unwind_protect (unwind_format_mode_line,
11187 format_mode_line_unwind_data
11188 (f, current_buffer, selected_window, 0));
11190 Fselect_window (f->selected_window, Qt);
11191 set_buffer_internal_1
11192 (XBUFFER (XWINDOW (f->selected_window)->contents));
11193 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11195 mode_line_target = MODE_LINE_TITLE;
11196 title_start = MODE_LINE_NOPROP_LEN (0);
11197 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11198 NULL, DEFAULT_FACE_ID);
11199 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11200 len = MODE_LINE_NOPROP_LEN (title_start);
11201 title = mode_line_noprop_buf + title_start;
11202 unbind_to (count, Qnil);
11204 /* Set the title only if it's changed. This avoids consing in
11205 the common case where it hasn't. (If it turns out that we've
11206 already wasted too much time by walking through the list with
11207 display_mode_element, then we might need to optimize at a
11208 higher level than this.) */
11209 if (! STRINGP (f->name)
11210 || SBYTES (f->name) != len
11211 || memcmp (title, SDATA (f->name), len) != 0)
11212 x_implicitly_set_name (f, make_string (title, len), Qnil);
11216 #endif /* not HAVE_WINDOW_SYSTEM */
11219 /***********************************************************************
11220 Menu Bars
11221 ***********************************************************************/
11224 /* Prepare for redisplay by updating menu-bar item lists when
11225 appropriate. This can call eval. */
11227 void
11228 prepare_menu_bars (void)
11230 int all_windows;
11231 struct gcpro gcpro1, gcpro2;
11232 struct frame *f;
11233 Lisp_Object tooltip_frame;
11235 #ifdef HAVE_WINDOW_SYSTEM
11236 tooltip_frame = tip_frame;
11237 #else
11238 tooltip_frame = Qnil;
11239 #endif
11241 /* Update all frame titles based on their buffer names, etc. We do
11242 this before the menu bars so that the buffer-menu will show the
11243 up-to-date frame titles. */
11244 #ifdef HAVE_WINDOW_SYSTEM
11245 if (windows_or_buffers_changed || update_mode_lines)
11247 Lisp_Object tail, frame;
11249 FOR_EACH_FRAME (tail, frame)
11251 f = XFRAME (frame);
11252 if (!EQ (frame, tooltip_frame)
11253 && (FRAME_ICONIFIED_P (f)
11254 || FRAME_VISIBLE_P (f) == 1
11255 /* Exclude TTY frames that are obscured because they
11256 are not the top frame on their console. This is
11257 because x_consider_frame_title actually switches
11258 to the frame, which for TTY frames means it is
11259 marked as garbaged, and will be completely
11260 redrawn on the next redisplay cycle. This causes
11261 TTY frames to be completely redrawn, when there
11262 are more than one of them, even though nothing
11263 should be changed on display. */
11264 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11265 x_consider_frame_title (frame);
11268 #endif /* HAVE_WINDOW_SYSTEM */
11270 /* Update the menu bar item lists, if appropriate. This has to be
11271 done before any actual redisplay or generation of display lines. */
11272 all_windows = (update_mode_lines
11273 || buffer_shared_and_changed ()
11274 || windows_or_buffers_changed);
11275 if (all_windows)
11277 Lisp_Object tail, frame;
11278 ptrdiff_t count = SPECPDL_INDEX ();
11279 /* 1 means that update_menu_bar has run its hooks
11280 so any further calls to update_menu_bar shouldn't do so again. */
11281 int menu_bar_hooks_run = 0;
11283 record_unwind_save_match_data ();
11285 FOR_EACH_FRAME (tail, frame)
11287 f = XFRAME (frame);
11289 /* Ignore tooltip frame. */
11290 if (EQ (frame, tooltip_frame))
11291 continue;
11293 /* If a window on this frame changed size, report that to
11294 the user and clear the size-change flag. */
11295 if (FRAME_WINDOW_SIZES_CHANGED (f))
11297 Lisp_Object functions;
11299 /* Clear flag first in case we get an error below. */
11300 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11301 functions = Vwindow_size_change_functions;
11302 GCPRO2 (tail, functions);
11304 while (CONSP (functions))
11306 if (!EQ (XCAR (functions), Qt))
11307 call1 (XCAR (functions), frame);
11308 functions = XCDR (functions);
11310 UNGCPRO;
11313 GCPRO1 (tail);
11314 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11315 #ifdef HAVE_WINDOW_SYSTEM
11316 update_tool_bar (f, 0);
11317 #endif
11318 #ifdef HAVE_NS
11319 if (windows_or_buffers_changed
11320 && FRAME_NS_P (f))
11321 ns_set_doc_edited
11322 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11323 #endif
11324 UNGCPRO;
11327 unbind_to (count, Qnil);
11329 else
11331 struct frame *sf = SELECTED_FRAME ();
11332 update_menu_bar (sf, 1, 0);
11333 #ifdef HAVE_WINDOW_SYSTEM
11334 update_tool_bar (sf, 1);
11335 #endif
11340 /* Update the menu bar item list for frame F. This has to be done
11341 before we start to fill in any display lines, because it can call
11342 eval.
11344 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11346 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11347 already ran the menu bar hooks for this redisplay, so there
11348 is no need to run them again. The return value is the
11349 updated value of this flag, to pass to the next call. */
11351 static int
11352 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11354 Lisp_Object window;
11355 register struct window *w;
11357 /* If called recursively during a menu update, do nothing. This can
11358 happen when, for instance, an activate-menubar-hook causes a
11359 redisplay. */
11360 if (inhibit_menubar_update)
11361 return hooks_run;
11363 window = FRAME_SELECTED_WINDOW (f);
11364 w = XWINDOW (window);
11366 if (FRAME_WINDOW_P (f)
11368 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11369 || defined (HAVE_NS) || defined (USE_GTK)
11370 FRAME_EXTERNAL_MENU_BAR (f)
11371 #else
11372 FRAME_MENU_BAR_LINES (f) > 0
11373 #endif
11374 : FRAME_MENU_BAR_LINES (f) > 0)
11376 /* If the user has switched buffers or windows, we need to
11377 recompute to reflect the new bindings. But we'll
11378 recompute when update_mode_lines is set too; that means
11379 that people can use force-mode-line-update to request
11380 that the menu bar be recomputed. The adverse effect on
11381 the rest of the redisplay algorithm is about the same as
11382 windows_or_buffers_changed anyway. */
11383 if (windows_or_buffers_changed
11384 /* This used to test w->update_mode_line, but we believe
11385 there is no need to recompute the menu in that case. */
11386 || update_mode_lines
11387 || window_buffer_changed (w))
11389 struct buffer *prev = current_buffer;
11390 ptrdiff_t count = SPECPDL_INDEX ();
11392 specbind (Qinhibit_menubar_update, Qt);
11394 set_buffer_internal_1 (XBUFFER (w->contents));
11395 if (save_match_data)
11396 record_unwind_save_match_data ();
11397 if (NILP (Voverriding_local_map_menu_flag))
11399 specbind (Qoverriding_terminal_local_map, Qnil);
11400 specbind (Qoverriding_local_map, Qnil);
11403 if (!hooks_run)
11405 /* Run the Lucid hook. */
11406 safe_run_hooks (Qactivate_menubar_hook);
11408 /* If it has changed current-menubar from previous value,
11409 really recompute the menu-bar from the value. */
11410 if (! NILP (Vlucid_menu_bar_dirty_flag))
11411 call0 (Qrecompute_lucid_menubar);
11413 safe_run_hooks (Qmenu_bar_update_hook);
11415 hooks_run = 1;
11418 XSETFRAME (Vmenu_updating_frame, f);
11419 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11421 /* Redisplay the menu bar in case we changed it. */
11422 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11423 || defined (HAVE_NS) || defined (USE_GTK)
11424 if (FRAME_WINDOW_P (f))
11426 #if defined (HAVE_NS)
11427 /* All frames on Mac OS share the same menubar. So only
11428 the selected frame should be allowed to set it. */
11429 if (f == SELECTED_FRAME ())
11430 #endif
11431 set_frame_menubar (f, 0, 0);
11433 else
11434 /* On a terminal screen, the menu bar is an ordinary screen
11435 line, and this makes it get updated. */
11436 w->update_mode_line = 1;
11437 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11438 /* In the non-toolkit version, the menu bar is an ordinary screen
11439 line, and this makes it get updated. */
11440 w->update_mode_line = 1;
11441 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11443 unbind_to (count, Qnil);
11444 set_buffer_internal_1 (prev);
11448 return hooks_run;
11451 /***********************************************************************
11452 Tool-bars
11453 ***********************************************************************/
11455 #ifdef HAVE_WINDOW_SYSTEM
11457 /* Where the mouse was last time we reported a mouse event. */
11459 struct frame *last_mouse_frame;
11461 /* Tool-bar item index of the item on which a mouse button was pressed
11462 or -1. */
11464 int last_tool_bar_item;
11466 /* Select `frame' temporarily without running all the code in
11467 do_switch_frame.
11468 FIXME: Maybe do_switch_frame should be trimmed down similarly
11469 when `norecord' is set. */
11470 static void
11471 fast_set_selected_frame (Lisp_Object frame)
11473 if (!EQ (selected_frame, frame))
11475 selected_frame = frame;
11476 selected_window = XFRAME (frame)->selected_window;
11480 /* Update the tool-bar item list for frame F. This has to be done
11481 before we start to fill in any display lines. Called from
11482 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11483 and restore it here. */
11485 static void
11486 update_tool_bar (struct frame *f, int save_match_data)
11488 #if defined (USE_GTK) || defined (HAVE_NS)
11489 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11490 #else
11491 int do_update = WINDOWP (f->tool_bar_window)
11492 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11493 #endif
11495 if (do_update)
11497 Lisp_Object window;
11498 struct window *w;
11500 window = FRAME_SELECTED_WINDOW (f);
11501 w = XWINDOW (window);
11503 /* If the user has switched buffers or windows, we need to
11504 recompute to reflect the new bindings. But we'll
11505 recompute when update_mode_lines is set too; that means
11506 that people can use force-mode-line-update to request
11507 that the menu bar be recomputed. The adverse effect on
11508 the rest of the redisplay algorithm is about the same as
11509 windows_or_buffers_changed anyway. */
11510 if (windows_or_buffers_changed
11511 || w->update_mode_line
11512 || update_mode_lines
11513 || window_buffer_changed (w))
11515 struct buffer *prev = current_buffer;
11516 ptrdiff_t count = SPECPDL_INDEX ();
11517 Lisp_Object frame, new_tool_bar;
11518 int new_n_tool_bar;
11519 struct gcpro gcpro1;
11521 /* Set current_buffer to the buffer of the selected
11522 window of the frame, so that we get the right local
11523 keymaps. */
11524 set_buffer_internal_1 (XBUFFER (w->contents));
11526 /* Save match data, if we must. */
11527 if (save_match_data)
11528 record_unwind_save_match_data ();
11530 /* Make sure that we don't accidentally use bogus keymaps. */
11531 if (NILP (Voverriding_local_map_menu_flag))
11533 specbind (Qoverriding_terminal_local_map, Qnil);
11534 specbind (Qoverriding_local_map, Qnil);
11537 GCPRO1 (new_tool_bar);
11539 /* We must temporarily set the selected frame to this frame
11540 before calling tool_bar_items, because the calculation of
11541 the tool-bar keymap uses the selected frame (see
11542 `tool-bar-make-keymap' in tool-bar.el). */
11543 eassert (EQ (selected_window,
11544 /* Since we only explicitly preserve selected_frame,
11545 check that selected_window would be redundant. */
11546 XFRAME (selected_frame)->selected_window));
11547 record_unwind_protect (fast_set_selected_frame, selected_frame);
11548 XSETFRAME (frame, f);
11549 fast_set_selected_frame (frame);
11551 /* Build desired tool-bar items from keymaps. */
11552 new_tool_bar
11553 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11554 &new_n_tool_bar);
11556 /* Redisplay the tool-bar if we changed it. */
11557 if (new_n_tool_bar != f->n_tool_bar_items
11558 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11560 /* Redisplay that happens asynchronously due to an expose event
11561 may access f->tool_bar_items. Make sure we update both
11562 variables within BLOCK_INPUT so no such event interrupts. */
11563 block_input ();
11564 fset_tool_bar_items (f, new_tool_bar);
11565 f->n_tool_bar_items = new_n_tool_bar;
11566 w->update_mode_line = 1;
11567 unblock_input ();
11570 UNGCPRO;
11572 unbind_to (count, Qnil);
11573 set_buffer_internal_1 (prev);
11578 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11580 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11581 F's desired tool-bar contents. F->tool_bar_items must have
11582 been set up previously by calling prepare_menu_bars. */
11584 static void
11585 build_desired_tool_bar_string (struct frame *f)
11587 int i, size, size_needed;
11588 struct gcpro gcpro1, gcpro2, gcpro3;
11589 Lisp_Object image, plist, props;
11591 image = plist = props = Qnil;
11592 GCPRO3 (image, plist, props);
11594 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11595 Otherwise, make a new string. */
11597 /* The size of the string we might be able to reuse. */
11598 size = (STRINGP (f->desired_tool_bar_string)
11599 ? SCHARS (f->desired_tool_bar_string)
11600 : 0);
11602 /* We need one space in the string for each image. */
11603 size_needed = f->n_tool_bar_items;
11605 /* Reuse f->desired_tool_bar_string, if possible. */
11606 if (size < size_needed || NILP (f->desired_tool_bar_string))
11607 fset_desired_tool_bar_string
11608 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11609 else
11611 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11612 Fremove_text_properties (make_number (0), make_number (size),
11613 props, f->desired_tool_bar_string);
11616 /* Put a `display' property on the string for the images to display,
11617 put a `menu_item' property on tool-bar items with a value that
11618 is the index of the item in F's tool-bar item vector. */
11619 for (i = 0; i < f->n_tool_bar_items; ++i)
11621 #define PROP(IDX) \
11622 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11624 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11625 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11626 int hmargin, vmargin, relief, idx, end;
11628 /* If image is a vector, choose the image according to the
11629 button state. */
11630 image = PROP (TOOL_BAR_ITEM_IMAGES);
11631 if (VECTORP (image))
11633 if (enabled_p)
11634 idx = (selected_p
11635 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11636 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11637 else
11638 idx = (selected_p
11639 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11640 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11642 eassert (ASIZE (image) >= idx);
11643 image = AREF (image, idx);
11645 else
11646 idx = -1;
11648 /* Ignore invalid image specifications. */
11649 if (!valid_image_p (image))
11650 continue;
11652 /* Display the tool-bar button pressed, or depressed. */
11653 plist = Fcopy_sequence (XCDR (image));
11655 /* Compute margin and relief to draw. */
11656 relief = (tool_bar_button_relief >= 0
11657 ? tool_bar_button_relief
11658 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11659 hmargin = vmargin = relief;
11661 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11662 INT_MAX - max (hmargin, vmargin)))
11664 hmargin += XFASTINT (Vtool_bar_button_margin);
11665 vmargin += XFASTINT (Vtool_bar_button_margin);
11667 else if (CONSP (Vtool_bar_button_margin))
11669 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11670 INT_MAX - hmargin))
11671 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11673 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11674 INT_MAX - vmargin))
11675 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11678 if (auto_raise_tool_bar_buttons_p)
11680 /* Add a `:relief' property to the image spec if the item is
11681 selected. */
11682 if (selected_p)
11684 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11685 hmargin -= relief;
11686 vmargin -= relief;
11689 else
11691 /* If image is selected, display it pressed, i.e. with a
11692 negative relief. If it's not selected, display it with a
11693 raised relief. */
11694 plist = Fplist_put (plist, QCrelief,
11695 (selected_p
11696 ? make_number (-relief)
11697 : make_number (relief)));
11698 hmargin -= relief;
11699 vmargin -= relief;
11702 /* Put a margin around the image. */
11703 if (hmargin || vmargin)
11705 if (hmargin == vmargin)
11706 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11707 else
11708 plist = Fplist_put (plist, QCmargin,
11709 Fcons (make_number (hmargin),
11710 make_number (vmargin)));
11713 /* If button is not enabled, and we don't have special images
11714 for the disabled state, make the image appear disabled by
11715 applying an appropriate algorithm to it. */
11716 if (!enabled_p && idx < 0)
11717 plist = Fplist_put (plist, QCconversion, Qdisabled);
11719 /* Put a `display' text property on the string for the image to
11720 display. Put a `menu-item' property on the string that gives
11721 the start of this item's properties in the tool-bar items
11722 vector. */
11723 image = Fcons (Qimage, plist);
11724 props = list4 (Qdisplay, image,
11725 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11727 /* Let the last image hide all remaining spaces in the tool bar
11728 string. The string can be longer than needed when we reuse a
11729 previous string. */
11730 if (i + 1 == f->n_tool_bar_items)
11731 end = SCHARS (f->desired_tool_bar_string);
11732 else
11733 end = i + 1;
11734 Fadd_text_properties (make_number (i), make_number (end),
11735 props, f->desired_tool_bar_string);
11736 #undef PROP
11739 UNGCPRO;
11743 /* Display one line of the tool-bar of frame IT->f.
11745 HEIGHT specifies the desired height of the tool-bar line.
11746 If the actual height of the glyph row is less than HEIGHT, the
11747 row's height is increased to HEIGHT, and the icons are centered
11748 vertically in the new height.
11750 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11751 count a final empty row in case the tool-bar width exactly matches
11752 the window width.
11755 static void
11756 display_tool_bar_line (struct it *it, int height)
11758 struct glyph_row *row = it->glyph_row;
11759 int max_x = it->last_visible_x;
11760 struct glyph *last;
11762 prepare_desired_row (row);
11763 row->y = it->current_y;
11765 /* Note that this isn't made use of if the face hasn't a box,
11766 so there's no need to check the face here. */
11767 it->start_of_box_run_p = 1;
11769 while (it->current_x < max_x)
11771 int x, n_glyphs_before, i, nglyphs;
11772 struct it it_before;
11774 /* Get the next display element. */
11775 if (!get_next_display_element (it))
11777 /* Don't count empty row if we are counting needed tool-bar lines. */
11778 if (height < 0 && !it->hpos)
11779 return;
11780 break;
11783 /* Produce glyphs. */
11784 n_glyphs_before = row->used[TEXT_AREA];
11785 it_before = *it;
11787 PRODUCE_GLYPHS (it);
11789 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11790 i = 0;
11791 x = it_before.current_x;
11792 while (i < nglyphs)
11794 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11796 if (x + glyph->pixel_width > max_x)
11798 /* Glyph doesn't fit on line. Backtrack. */
11799 row->used[TEXT_AREA] = n_glyphs_before;
11800 *it = it_before;
11801 /* If this is the only glyph on this line, it will never fit on the
11802 tool-bar, so skip it. But ensure there is at least one glyph,
11803 so we don't accidentally disable the tool-bar. */
11804 if (n_glyphs_before == 0
11805 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11806 break;
11807 goto out;
11810 ++it->hpos;
11811 x += glyph->pixel_width;
11812 ++i;
11815 /* Stop at line end. */
11816 if (ITERATOR_AT_END_OF_LINE_P (it))
11817 break;
11819 set_iterator_to_next (it, 1);
11822 out:;
11824 row->displays_text_p = row->used[TEXT_AREA] != 0;
11826 /* Use default face for the border below the tool bar.
11828 FIXME: When auto-resize-tool-bars is grow-only, there is
11829 no additional border below the possibly empty tool-bar lines.
11830 So to make the extra empty lines look "normal", we have to
11831 use the tool-bar face for the border too. */
11832 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11833 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11834 it->face_id = DEFAULT_FACE_ID;
11836 extend_face_to_end_of_line (it);
11837 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11838 last->right_box_line_p = 1;
11839 if (last == row->glyphs[TEXT_AREA])
11840 last->left_box_line_p = 1;
11842 /* Make line the desired height and center it vertically. */
11843 if ((height -= it->max_ascent + it->max_descent) > 0)
11845 /* Don't add more than one line height. */
11846 height %= FRAME_LINE_HEIGHT (it->f);
11847 it->max_ascent += height / 2;
11848 it->max_descent += (height + 1) / 2;
11851 compute_line_metrics (it);
11853 /* If line is empty, make it occupy the rest of the tool-bar. */
11854 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11856 row->height = row->phys_height = it->last_visible_y - row->y;
11857 row->visible_height = row->height;
11858 row->ascent = row->phys_ascent = 0;
11859 row->extra_line_spacing = 0;
11862 row->full_width_p = 1;
11863 row->continued_p = 0;
11864 row->truncated_on_left_p = 0;
11865 row->truncated_on_right_p = 0;
11867 it->current_x = it->hpos = 0;
11868 it->current_y += row->height;
11869 ++it->vpos;
11870 ++it->glyph_row;
11874 /* Max tool-bar height. */
11876 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11877 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11879 /* Value is the number of screen lines needed to make all tool-bar
11880 items of frame F visible. The number of actual rows needed is
11881 returned in *N_ROWS if non-NULL. */
11883 static int
11884 tool_bar_lines_needed (struct frame *f, int *n_rows)
11886 struct window *w = XWINDOW (f->tool_bar_window);
11887 struct it it;
11888 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11889 the desired matrix, so use (unused) mode-line row as temporary row to
11890 avoid destroying the first tool-bar row. */
11891 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11893 /* Initialize an iterator for iteration over
11894 F->desired_tool_bar_string in the tool-bar window of frame F. */
11895 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11896 it.first_visible_x = 0;
11897 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11898 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11899 it.paragraph_embedding = L2R;
11901 while (!ITERATOR_AT_END_P (&it))
11903 clear_glyph_row (temp_row);
11904 it.glyph_row = temp_row;
11905 display_tool_bar_line (&it, -1);
11907 clear_glyph_row (temp_row);
11909 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11910 if (n_rows)
11911 *n_rows = it.vpos > 0 ? it.vpos : -1;
11913 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11916 #endif /* !USE_GTK && !HAVE_NS */
11918 #if defined USE_GTK || defined HAVE_NS
11919 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
11920 #endif
11922 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11923 0, 1, 0,
11924 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11925 If FRAME is nil or omitted, use the selected frame. */)
11926 (Lisp_Object frame)
11928 int nlines = 0;
11929 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11930 struct frame *f = decode_any_frame (frame);
11931 struct window *w;
11933 if (WINDOWP (f->tool_bar_window)
11934 && (w = XWINDOW (f->tool_bar_window),
11935 WINDOW_TOTAL_LINES (w) > 0))
11937 update_tool_bar (f, 1);
11938 if (f->n_tool_bar_items)
11940 build_desired_tool_bar_string (f);
11941 nlines = tool_bar_lines_needed (f, NULL);
11944 #endif
11945 return make_number (nlines);
11949 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11950 height should be changed. */
11952 static int
11953 redisplay_tool_bar (struct frame *f)
11955 #if defined (USE_GTK) || defined (HAVE_NS)
11957 if (FRAME_EXTERNAL_TOOL_BAR (f))
11958 update_frame_tool_bar (f);
11959 return 0;
11961 #else /* !USE_GTK && !HAVE_NS */
11963 struct window *w;
11964 struct it it;
11965 struct glyph_row *row;
11967 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11968 do anything. This means you must start with tool-bar-lines
11969 non-zero to get the auto-sizing effect. Or in other words, you
11970 can turn off tool-bars by specifying tool-bar-lines zero. */
11971 if (!WINDOWP (f->tool_bar_window)
11972 || (w = XWINDOW (f->tool_bar_window),
11973 WINDOW_TOTAL_LINES (w) == 0))
11974 return 0;
11976 /* Set up an iterator for the tool-bar window. */
11977 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11978 it.first_visible_x = 0;
11979 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11980 row = it.glyph_row;
11982 /* Build a string that represents the contents of the tool-bar. */
11983 build_desired_tool_bar_string (f);
11984 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11985 /* FIXME: This should be controlled by a user option. But it
11986 doesn't make sense to have an R2L tool bar if the menu bar cannot
11987 be drawn also R2L, and making the menu bar R2L is tricky due
11988 toolkit-specific code that implements it. If an R2L tool bar is
11989 ever supported, display_tool_bar_line should also be augmented to
11990 call unproduce_glyphs like display_line and display_string
11991 do. */
11992 it.paragraph_embedding = L2R;
11994 if (f->n_tool_bar_rows == 0)
11996 int nlines;
11998 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11999 nlines != WINDOW_TOTAL_LINES (w)))
12001 Lisp_Object frame;
12002 int old_height = WINDOW_TOTAL_LINES (w);
12004 XSETFRAME (frame, f);
12005 Fmodify_frame_parameters (frame,
12006 list1 (Fcons (Qtool_bar_lines,
12007 make_number (nlines))));
12008 if (WINDOW_TOTAL_LINES (w) != old_height)
12010 clear_glyph_matrix (w->desired_matrix);
12011 f->fonts_changed = 1;
12012 return 1;
12017 /* Display as many lines as needed to display all tool-bar items. */
12019 if (f->n_tool_bar_rows > 0)
12021 int border, rows, height, extra;
12023 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12024 border = XINT (Vtool_bar_border);
12025 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12026 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12027 else if (EQ (Vtool_bar_border, Qborder_width))
12028 border = f->border_width;
12029 else
12030 border = 0;
12031 if (border < 0)
12032 border = 0;
12034 rows = f->n_tool_bar_rows;
12035 height = max (1, (it.last_visible_y - border) / rows);
12036 extra = it.last_visible_y - border - height * rows;
12038 while (it.current_y < it.last_visible_y)
12040 int h = 0;
12041 if (extra > 0 && rows-- > 0)
12043 h = (extra + rows - 1) / rows;
12044 extra -= h;
12046 display_tool_bar_line (&it, height + h);
12049 else
12051 while (it.current_y < it.last_visible_y)
12052 display_tool_bar_line (&it, 0);
12055 /* It doesn't make much sense to try scrolling in the tool-bar
12056 window, so don't do it. */
12057 w->desired_matrix->no_scrolling_p = 1;
12058 w->must_be_updated_p = 1;
12060 if (!NILP (Vauto_resize_tool_bars))
12062 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12063 int change_height_p = 0;
12065 /* If we couldn't display everything, change the tool-bar's
12066 height if there is room for more. */
12067 if (IT_STRING_CHARPOS (it) < it.end_charpos
12068 && it.current_y < max_tool_bar_height)
12069 change_height_p = 1;
12071 row = it.glyph_row - 1;
12073 /* If there are blank lines at the end, except for a partially
12074 visible blank line at the end that is smaller than
12075 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12076 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12077 && row->height >= FRAME_LINE_HEIGHT (f))
12078 change_height_p = 1;
12080 /* If row displays tool-bar items, but is partially visible,
12081 change the tool-bar's height. */
12082 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12083 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12084 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12085 change_height_p = 1;
12087 /* Resize windows as needed by changing the `tool-bar-lines'
12088 frame parameter. */
12089 if (change_height_p)
12091 Lisp_Object frame;
12092 int old_height = WINDOW_TOTAL_LINES (w);
12093 int nrows;
12094 int nlines = tool_bar_lines_needed (f, &nrows);
12096 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12097 && !f->minimize_tool_bar_window_p)
12098 ? (nlines > old_height)
12099 : (nlines != old_height));
12100 f->minimize_tool_bar_window_p = 0;
12102 if (change_height_p)
12104 XSETFRAME (frame, f);
12105 Fmodify_frame_parameters (frame,
12106 list1 (Fcons (Qtool_bar_lines,
12107 make_number (nlines))));
12108 if (WINDOW_TOTAL_LINES (w) != old_height)
12110 clear_glyph_matrix (w->desired_matrix);
12111 f->n_tool_bar_rows = nrows;
12112 f->fonts_changed = 1;
12113 return 1;
12119 f->minimize_tool_bar_window_p = 0;
12120 return 0;
12122 #endif /* USE_GTK || HAVE_NS */
12125 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12127 /* Get information about the tool-bar item which is displayed in GLYPH
12128 on frame F. Return in *PROP_IDX the index where tool-bar item
12129 properties start in F->tool_bar_items. Value is zero if
12130 GLYPH doesn't display a tool-bar item. */
12132 static int
12133 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12135 Lisp_Object prop;
12136 int success_p;
12137 int charpos;
12139 /* This function can be called asynchronously, which means we must
12140 exclude any possibility that Fget_text_property signals an
12141 error. */
12142 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12143 charpos = max (0, charpos);
12145 /* Get the text property `menu-item' at pos. The value of that
12146 property is the start index of this item's properties in
12147 F->tool_bar_items. */
12148 prop = Fget_text_property (make_number (charpos),
12149 Qmenu_item, f->current_tool_bar_string);
12150 if (INTEGERP (prop))
12152 *prop_idx = XINT (prop);
12153 success_p = 1;
12155 else
12156 success_p = 0;
12158 return success_p;
12162 /* Get information about the tool-bar item at position X/Y on frame F.
12163 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12164 the current matrix of the tool-bar window of F, or NULL if not
12165 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12166 item in F->tool_bar_items. Value is
12168 -1 if X/Y is not on a tool-bar item
12169 0 if X/Y is on the same item that was highlighted before.
12170 1 otherwise. */
12172 static int
12173 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12174 int *hpos, int *vpos, int *prop_idx)
12176 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12177 struct window *w = XWINDOW (f->tool_bar_window);
12178 int area;
12180 /* Find the glyph under X/Y. */
12181 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12182 if (*glyph == NULL)
12183 return -1;
12185 /* Get the start of this tool-bar item's properties in
12186 f->tool_bar_items. */
12187 if (!tool_bar_item_info (f, *glyph, prop_idx))
12188 return -1;
12190 /* Is mouse on the highlighted item? */
12191 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12192 && *vpos >= hlinfo->mouse_face_beg_row
12193 && *vpos <= hlinfo->mouse_face_end_row
12194 && (*vpos > hlinfo->mouse_face_beg_row
12195 || *hpos >= hlinfo->mouse_face_beg_col)
12196 && (*vpos < hlinfo->mouse_face_end_row
12197 || *hpos < hlinfo->mouse_face_end_col
12198 || hlinfo->mouse_face_past_end))
12199 return 0;
12201 return 1;
12205 /* EXPORT:
12206 Handle mouse button event on the tool-bar of frame F, at
12207 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12208 0 for button release. MODIFIERS is event modifiers for button
12209 release. */
12211 void
12212 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12213 int modifiers)
12215 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12216 struct window *w = XWINDOW (f->tool_bar_window);
12217 int hpos, vpos, prop_idx;
12218 struct glyph *glyph;
12219 Lisp_Object enabled_p;
12220 int ts;
12222 /* If not on the highlighted tool-bar item, and mouse-highlight is
12223 non-nil, return. This is so we generate the tool-bar button
12224 click only when the mouse button is released on the same item as
12225 where it was pressed. However, when mouse-highlight is disabled,
12226 generate the click when the button is released regardless of the
12227 highlight, since tool-bar items are not highlighted in that
12228 case. */
12229 frame_to_window_pixel_xy (w, &x, &y);
12230 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12231 if (ts == -1
12232 || (ts != 0 && !NILP (Vmouse_highlight)))
12233 return;
12235 /* When mouse-highlight is off, generate the click for the item
12236 where the button was pressed, disregarding where it was
12237 released. */
12238 if (NILP (Vmouse_highlight) && !down_p)
12239 prop_idx = last_tool_bar_item;
12241 /* If item is disabled, do nothing. */
12242 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12243 if (NILP (enabled_p))
12244 return;
12246 if (down_p)
12248 /* Show item in pressed state. */
12249 if (!NILP (Vmouse_highlight))
12250 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12251 last_tool_bar_item = prop_idx;
12253 else
12255 Lisp_Object key, frame;
12256 struct input_event event;
12257 EVENT_INIT (event);
12259 /* Show item in released state. */
12260 if (!NILP (Vmouse_highlight))
12261 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12263 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12265 XSETFRAME (frame, f);
12266 event.kind = TOOL_BAR_EVENT;
12267 event.frame_or_window = frame;
12268 event.arg = frame;
12269 kbd_buffer_store_event (&event);
12271 event.kind = TOOL_BAR_EVENT;
12272 event.frame_or_window = frame;
12273 event.arg = key;
12274 event.modifiers = modifiers;
12275 kbd_buffer_store_event (&event);
12276 last_tool_bar_item = -1;
12281 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12282 tool-bar window-relative coordinates X/Y. Called from
12283 note_mouse_highlight. */
12285 static void
12286 note_tool_bar_highlight (struct frame *f, int x, int y)
12288 Lisp_Object window = f->tool_bar_window;
12289 struct window *w = XWINDOW (window);
12290 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12291 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12292 int hpos, vpos;
12293 struct glyph *glyph;
12294 struct glyph_row *row;
12295 int i;
12296 Lisp_Object enabled_p;
12297 int prop_idx;
12298 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12299 int mouse_down_p, rc;
12301 /* Function note_mouse_highlight is called with negative X/Y
12302 values when mouse moves outside of the frame. */
12303 if (x <= 0 || y <= 0)
12305 clear_mouse_face (hlinfo);
12306 return;
12309 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12310 if (rc < 0)
12312 /* Not on tool-bar item. */
12313 clear_mouse_face (hlinfo);
12314 return;
12316 else if (rc == 0)
12317 /* On same tool-bar item as before. */
12318 goto set_help_echo;
12320 clear_mouse_face (hlinfo);
12322 /* Mouse is down, but on different tool-bar item? */
12323 mouse_down_p = (dpyinfo->grabbed
12324 && f == last_mouse_frame
12325 && FRAME_LIVE_P (f));
12326 if (mouse_down_p
12327 && last_tool_bar_item != prop_idx)
12328 return;
12330 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12332 /* If tool-bar item is not enabled, don't highlight it. */
12333 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12334 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12336 /* Compute the x-position of the glyph. In front and past the
12337 image is a space. We include this in the highlighted area. */
12338 row = MATRIX_ROW (w->current_matrix, vpos);
12339 for (i = x = 0; i < hpos; ++i)
12340 x += row->glyphs[TEXT_AREA][i].pixel_width;
12342 /* Record this as the current active region. */
12343 hlinfo->mouse_face_beg_col = hpos;
12344 hlinfo->mouse_face_beg_row = vpos;
12345 hlinfo->mouse_face_beg_x = x;
12346 hlinfo->mouse_face_past_end = 0;
12348 hlinfo->mouse_face_end_col = hpos + 1;
12349 hlinfo->mouse_face_end_row = vpos;
12350 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12351 hlinfo->mouse_face_window = window;
12352 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12354 /* Display it as active. */
12355 show_mouse_face (hlinfo, draw);
12358 set_help_echo:
12360 /* Set help_echo_string to a help string to display for this tool-bar item.
12361 XTread_socket does the rest. */
12362 help_echo_object = help_echo_window = Qnil;
12363 help_echo_pos = -1;
12364 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12365 if (NILP (help_echo_string))
12366 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12369 #endif /* !USE_GTK && !HAVE_NS */
12371 #endif /* HAVE_WINDOW_SYSTEM */
12375 /************************************************************************
12376 Horizontal scrolling
12377 ************************************************************************/
12379 static int hscroll_window_tree (Lisp_Object);
12380 static int hscroll_windows (Lisp_Object);
12382 /* For all leaf windows in the window tree rooted at WINDOW, set their
12383 hscroll value so that PT is (i) visible in the window, and (ii) so
12384 that it is not within a certain margin at the window's left and
12385 right border. Value is non-zero if any window's hscroll has been
12386 changed. */
12388 static int
12389 hscroll_window_tree (Lisp_Object window)
12391 int hscrolled_p = 0;
12392 int hscroll_relative_p = FLOATP (Vhscroll_step);
12393 int hscroll_step_abs = 0;
12394 double hscroll_step_rel = 0;
12396 if (hscroll_relative_p)
12398 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12399 if (hscroll_step_rel < 0)
12401 hscroll_relative_p = 0;
12402 hscroll_step_abs = 0;
12405 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12407 hscroll_step_abs = XINT (Vhscroll_step);
12408 if (hscroll_step_abs < 0)
12409 hscroll_step_abs = 0;
12411 else
12412 hscroll_step_abs = 0;
12414 while (WINDOWP (window))
12416 struct window *w = XWINDOW (window);
12418 if (WINDOWP (w->contents))
12419 hscrolled_p |= hscroll_window_tree (w->contents);
12420 else if (w->cursor.vpos >= 0)
12422 int h_margin;
12423 int text_area_width;
12424 struct glyph_row *current_cursor_row
12425 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12426 struct glyph_row *desired_cursor_row
12427 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12428 struct glyph_row *cursor_row
12429 = (desired_cursor_row->enabled_p
12430 ? desired_cursor_row
12431 : current_cursor_row);
12432 int row_r2l_p = cursor_row->reversed_p;
12434 text_area_width = window_box_width (w, TEXT_AREA);
12436 /* Scroll when cursor is inside this scroll margin. */
12437 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12439 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12440 /* For left-to-right rows, hscroll when cursor is either
12441 (i) inside the right hscroll margin, or (ii) if it is
12442 inside the left margin and the window is already
12443 hscrolled. */
12444 && ((!row_r2l_p
12445 && ((w->hscroll
12446 && w->cursor.x <= h_margin)
12447 || (cursor_row->enabled_p
12448 && cursor_row->truncated_on_right_p
12449 && (w->cursor.x >= text_area_width - h_margin))))
12450 /* For right-to-left rows, the logic is similar,
12451 except that rules for scrolling to left and right
12452 are reversed. E.g., if cursor.x <= h_margin, we
12453 need to hscroll "to the right" unconditionally,
12454 and that will scroll the screen to the left so as
12455 to reveal the next portion of the row. */
12456 || (row_r2l_p
12457 && ((cursor_row->enabled_p
12458 /* FIXME: It is confusing to set the
12459 truncated_on_right_p flag when R2L rows
12460 are actually truncated on the left. */
12461 && cursor_row->truncated_on_right_p
12462 && w->cursor.x <= h_margin)
12463 || (w->hscroll
12464 && (w->cursor.x >= text_area_width - h_margin))))))
12466 struct it it;
12467 ptrdiff_t hscroll;
12468 struct buffer *saved_current_buffer;
12469 ptrdiff_t pt;
12470 int wanted_x;
12472 /* Find point in a display of infinite width. */
12473 saved_current_buffer = current_buffer;
12474 current_buffer = XBUFFER (w->contents);
12476 if (w == XWINDOW (selected_window))
12477 pt = PT;
12478 else
12479 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12481 /* Move iterator to pt starting at cursor_row->start in
12482 a line with infinite width. */
12483 init_to_row_start (&it, w, cursor_row);
12484 it.last_visible_x = INFINITY;
12485 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12486 current_buffer = saved_current_buffer;
12488 /* Position cursor in window. */
12489 if (!hscroll_relative_p && hscroll_step_abs == 0)
12490 hscroll = max (0, (it.current_x
12491 - (ITERATOR_AT_END_OF_LINE_P (&it)
12492 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12493 : (text_area_width / 2))))
12494 / FRAME_COLUMN_WIDTH (it.f);
12495 else if ((!row_r2l_p
12496 && w->cursor.x >= text_area_width - h_margin)
12497 || (row_r2l_p && w->cursor.x <= h_margin))
12499 if (hscroll_relative_p)
12500 wanted_x = text_area_width * (1 - hscroll_step_rel)
12501 - h_margin;
12502 else
12503 wanted_x = text_area_width
12504 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12505 - h_margin;
12506 hscroll
12507 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12509 else
12511 if (hscroll_relative_p)
12512 wanted_x = text_area_width * hscroll_step_rel
12513 + h_margin;
12514 else
12515 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12516 + h_margin;
12517 hscroll
12518 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12520 hscroll = max (hscroll, w->min_hscroll);
12522 /* Don't prevent redisplay optimizations if hscroll
12523 hasn't changed, as it will unnecessarily slow down
12524 redisplay. */
12525 if (w->hscroll != hscroll)
12527 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12528 w->hscroll = hscroll;
12529 hscrolled_p = 1;
12534 window = w->next;
12537 /* Value is non-zero if hscroll of any leaf window has been changed. */
12538 return hscrolled_p;
12542 /* Set hscroll so that cursor is visible and not inside horizontal
12543 scroll margins for all windows in the tree rooted at WINDOW. See
12544 also hscroll_window_tree above. Value is non-zero if any window's
12545 hscroll has been changed. If it has, desired matrices on the frame
12546 of WINDOW are cleared. */
12548 static int
12549 hscroll_windows (Lisp_Object window)
12551 int hscrolled_p = hscroll_window_tree (window);
12552 if (hscrolled_p)
12553 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12554 return hscrolled_p;
12559 /************************************************************************
12560 Redisplay
12561 ************************************************************************/
12563 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12564 to a non-zero value. This is sometimes handy to have in a debugger
12565 session. */
12567 #ifdef GLYPH_DEBUG
12569 /* First and last unchanged row for try_window_id. */
12571 static int debug_first_unchanged_at_end_vpos;
12572 static int debug_last_unchanged_at_beg_vpos;
12574 /* Delta vpos and y. */
12576 static int debug_dvpos, debug_dy;
12578 /* Delta in characters and bytes for try_window_id. */
12580 static ptrdiff_t debug_delta, debug_delta_bytes;
12582 /* Values of window_end_pos and window_end_vpos at the end of
12583 try_window_id. */
12585 static ptrdiff_t debug_end_vpos;
12587 /* Append a string to W->desired_matrix->method. FMT is a printf
12588 format string. If trace_redisplay_p is non-zero also printf the
12589 resulting string to stderr. */
12591 static void debug_method_add (struct window *, char const *, ...)
12592 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12594 static void
12595 debug_method_add (struct window *w, char const *fmt, ...)
12597 void *ptr = w;
12598 char *method = w->desired_matrix->method;
12599 int len = strlen (method);
12600 int size = sizeof w->desired_matrix->method;
12601 int remaining = size - len - 1;
12602 va_list ap;
12604 if (len && remaining)
12606 method[len] = '|';
12607 --remaining, ++len;
12610 va_start (ap, fmt);
12611 vsnprintf (method + len, remaining + 1, fmt, ap);
12612 va_end (ap);
12614 if (trace_redisplay_p)
12615 fprintf (stderr, "%p (%s): %s\n",
12616 ptr,
12617 ((BUFFERP (w->contents)
12618 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12619 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12620 : "no buffer"),
12621 method + len);
12624 #endif /* GLYPH_DEBUG */
12627 /* Value is non-zero if all changes in window W, which displays
12628 current_buffer, are in the text between START and END. START is a
12629 buffer position, END is given as a distance from Z. Used in
12630 redisplay_internal for display optimization. */
12632 static int
12633 text_outside_line_unchanged_p (struct window *w,
12634 ptrdiff_t start, ptrdiff_t end)
12636 int unchanged_p = 1;
12638 /* If text or overlays have changed, see where. */
12639 if (window_outdated (w))
12641 /* Gap in the line? */
12642 if (GPT < start || Z - GPT < end)
12643 unchanged_p = 0;
12645 /* Changes start in front of the line, or end after it? */
12646 if (unchanged_p
12647 && (BEG_UNCHANGED < start - 1
12648 || END_UNCHANGED < end))
12649 unchanged_p = 0;
12651 /* If selective display, can't optimize if changes start at the
12652 beginning of the line. */
12653 if (unchanged_p
12654 && INTEGERP (BVAR (current_buffer, selective_display))
12655 && XINT (BVAR (current_buffer, selective_display)) > 0
12656 && (BEG_UNCHANGED < start || GPT <= start))
12657 unchanged_p = 0;
12659 /* If there are overlays at the start or end of the line, these
12660 may have overlay strings with newlines in them. A change at
12661 START, for instance, may actually concern the display of such
12662 overlay strings as well, and they are displayed on different
12663 lines. So, quickly rule out this case. (For the future, it
12664 might be desirable to implement something more telling than
12665 just BEG/END_UNCHANGED.) */
12666 if (unchanged_p)
12668 if (BEG + BEG_UNCHANGED == start
12669 && overlay_touches_p (start))
12670 unchanged_p = 0;
12671 if (END_UNCHANGED == end
12672 && overlay_touches_p (Z - end))
12673 unchanged_p = 0;
12676 /* Under bidi reordering, adding or deleting a character in the
12677 beginning of a paragraph, before the first strong directional
12678 character, can change the base direction of the paragraph (unless
12679 the buffer specifies a fixed paragraph direction), which will
12680 require to redisplay the whole paragraph. It might be worthwhile
12681 to find the paragraph limits and widen the range of redisplayed
12682 lines to that, but for now just give up this optimization. */
12683 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12684 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12685 unchanged_p = 0;
12688 return unchanged_p;
12692 /* Do a frame update, taking possible shortcuts into account. This is
12693 the main external entry point for redisplay.
12695 If the last redisplay displayed an echo area message and that message
12696 is no longer requested, we clear the echo area or bring back the
12697 mini-buffer if that is in use. */
12699 void
12700 redisplay (void)
12702 redisplay_internal ();
12706 static Lisp_Object
12707 overlay_arrow_string_or_property (Lisp_Object var)
12709 Lisp_Object val;
12711 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12712 return val;
12714 return Voverlay_arrow_string;
12717 /* Return 1 if there are any overlay-arrows in current_buffer. */
12718 static int
12719 overlay_arrow_in_current_buffer_p (void)
12721 Lisp_Object vlist;
12723 for (vlist = Voverlay_arrow_variable_list;
12724 CONSP (vlist);
12725 vlist = XCDR (vlist))
12727 Lisp_Object var = XCAR (vlist);
12728 Lisp_Object val;
12730 if (!SYMBOLP (var))
12731 continue;
12732 val = find_symbol_value (var);
12733 if (MARKERP (val)
12734 && current_buffer == XMARKER (val)->buffer)
12735 return 1;
12737 return 0;
12741 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12742 has changed. */
12744 static int
12745 overlay_arrows_changed_p (void)
12747 Lisp_Object vlist;
12749 for (vlist = Voverlay_arrow_variable_list;
12750 CONSP (vlist);
12751 vlist = XCDR (vlist))
12753 Lisp_Object var = XCAR (vlist);
12754 Lisp_Object val, pstr;
12756 if (!SYMBOLP (var))
12757 continue;
12758 val = find_symbol_value (var);
12759 if (!MARKERP (val))
12760 continue;
12761 if (! EQ (COERCE_MARKER (val),
12762 Fget (var, Qlast_arrow_position))
12763 || ! (pstr = overlay_arrow_string_or_property (var),
12764 EQ (pstr, Fget (var, Qlast_arrow_string))))
12765 return 1;
12767 return 0;
12770 /* Mark overlay arrows to be updated on next redisplay. */
12772 static void
12773 update_overlay_arrows (int up_to_date)
12775 Lisp_Object vlist;
12777 for (vlist = Voverlay_arrow_variable_list;
12778 CONSP (vlist);
12779 vlist = XCDR (vlist))
12781 Lisp_Object var = XCAR (vlist);
12783 if (!SYMBOLP (var))
12784 continue;
12786 if (up_to_date > 0)
12788 Lisp_Object val = find_symbol_value (var);
12789 Fput (var, Qlast_arrow_position,
12790 COERCE_MARKER (val));
12791 Fput (var, Qlast_arrow_string,
12792 overlay_arrow_string_or_property (var));
12794 else if (up_to_date < 0
12795 || !NILP (Fget (var, Qlast_arrow_position)))
12797 Fput (var, Qlast_arrow_position, Qt);
12798 Fput (var, Qlast_arrow_string, Qt);
12804 /* Return overlay arrow string to display at row.
12805 Return integer (bitmap number) for arrow bitmap in left fringe.
12806 Return nil if no overlay arrow. */
12808 static Lisp_Object
12809 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12811 Lisp_Object vlist;
12813 for (vlist = Voverlay_arrow_variable_list;
12814 CONSP (vlist);
12815 vlist = XCDR (vlist))
12817 Lisp_Object var = XCAR (vlist);
12818 Lisp_Object val;
12820 if (!SYMBOLP (var))
12821 continue;
12823 val = find_symbol_value (var);
12825 if (MARKERP (val)
12826 && current_buffer == XMARKER (val)->buffer
12827 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12829 if (FRAME_WINDOW_P (it->f)
12830 /* FIXME: if ROW->reversed_p is set, this should test
12831 the right fringe, not the left one. */
12832 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12834 #ifdef HAVE_WINDOW_SYSTEM
12835 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12837 int fringe_bitmap;
12838 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12839 return make_number (fringe_bitmap);
12841 #endif
12842 return make_number (-1); /* Use default arrow bitmap. */
12844 return overlay_arrow_string_or_property (var);
12848 return Qnil;
12851 /* Return 1 if point moved out of or into a composition. Otherwise
12852 return 0. PREV_BUF and PREV_PT are the last point buffer and
12853 position. BUF and PT are the current point buffer and position. */
12855 static int
12856 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12857 struct buffer *buf, ptrdiff_t pt)
12859 ptrdiff_t start, end;
12860 Lisp_Object prop;
12861 Lisp_Object buffer;
12863 XSETBUFFER (buffer, buf);
12864 /* Check a composition at the last point if point moved within the
12865 same buffer. */
12866 if (prev_buf == buf)
12868 if (prev_pt == pt)
12869 /* Point didn't move. */
12870 return 0;
12872 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12873 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12874 && composition_valid_p (start, end, prop)
12875 && start < prev_pt && end > prev_pt)
12876 /* The last point was within the composition. Return 1 iff
12877 point moved out of the composition. */
12878 return (pt <= start || pt >= end);
12881 /* Check a composition at the current point. */
12882 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12883 && find_composition (pt, -1, &start, &end, &prop, buffer)
12884 && composition_valid_p (start, end, prop)
12885 && start < pt && end > pt);
12888 /* Reconsider the clip changes of buffer which is displayed in W. */
12890 static void
12891 reconsider_clip_changes (struct window *w)
12893 struct buffer *b = XBUFFER (w->contents);
12895 if (b->clip_changed
12896 && w->window_end_valid
12897 && w->current_matrix->buffer == b
12898 && w->current_matrix->zv == BUF_ZV (b)
12899 && w->current_matrix->begv == BUF_BEGV (b))
12900 b->clip_changed = 0;
12902 /* If display wasn't paused, and W is not a tool bar window, see if
12903 point has been moved into or out of a composition. In that case,
12904 we set b->clip_changed to 1 to force updating the screen. If
12905 b->clip_changed has already been set to 1, we can skip this
12906 check. */
12907 if (!b->clip_changed && w->window_end_valid)
12909 ptrdiff_t pt = (w == XWINDOW (selected_window)
12910 ? PT : marker_position (w->pointm));
12912 if ((w->current_matrix->buffer != b || pt != w->last_point)
12913 && check_point_in_composition (w->current_matrix->buffer,
12914 w->last_point, b, pt))
12915 b->clip_changed = 1;
12919 #define STOP_POLLING \
12920 do { if (! polling_stopped_here) stop_polling (); \
12921 polling_stopped_here = 1; } while (0)
12923 #define RESUME_POLLING \
12924 do { if (polling_stopped_here) start_polling (); \
12925 polling_stopped_here = 0; } while (0)
12928 /* Perhaps in the future avoid recentering windows if it
12929 is not necessary; currently that causes some problems. */
12931 static void
12932 redisplay_internal (void)
12934 struct window *w = XWINDOW (selected_window);
12935 struct window *sw;
12936 struct frame *fr;
12937 int pending;
12938 bool must_finish = 0, match_p;
12939 struct text_pos tlbufpos, tlendpos;
12940 int number_of_visible_frames;
12941 ptrdiff_t count;
12942 struct frame *sf;
12943 int polling_stopped_here = 0;
12944 Lisp_Object tail, frame;
12946 /* Non-zero means redisplay has to consider all windows on all
12947 frames. Zero means, only selected_window is considered. */
12948 int consider_all_windows_p;
12950 /* Non-zero means redisplay has to redisplay the miniwindow. */
12951 int update_miniwindow_p = 0;
12953 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12955 /* No redisplay if running in batch mode or frame is not yet fully
12956 initialized, or redisplay is explicitly turned off by setting
12957 Vinhibit_redisplay. */
12958 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12959 || !NILP (Vinhibit_redisplay))
12960 return;
12962 /* Don't examine these until after testing Vinhibit_redisplay.
12963 When Emacs is shutting down, perhaps because its connection to
12964 X has dropped, we should not look at them at all. */
12965 fr = XFRAME (w->frame);
12966 sf = SELECTED_FRAME ();
12968 if (!fr->glyphs_initialized_p)
12969 return;
12971 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12972 if (popup_activated ())
12973 return;
12974 #endif
12976 /* I don't think this happens but let's be paranoid. */
12977 if (redisplaying_p)
12978 return;
12980 /* Record a function that clears redisplaying_p
12981 when we leave this function. */
12982 count = SPECPDL_INDEX ();
12983 record_unwind_protect_void (unwind_redisplay);
12984 redisplaying_p = 1;
12985 specbind (Qinhibit_free_realized_faces, Qnil);
12987 /* Record this function, so it appears on the profiler's backtraces. */
12988 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12990 FOR_EACH_FRAME (tail, frame)
12991 XFRAME (frame)->already_hscrolled_p = 0;
12993 retry:
12994 /* Remember the currently selected window. */
12995 sw = w;
12997 pending = 0;
12998 last_escape_glyph_frame = NULL;
12999 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13000 last_glyphless_glyph_frame = NULL;
13001 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13003 /* If face_change_count is non-zero, init_iterator will free all
13004 realized faces, which includes the faces referenced from current
13005 matrices. So, we can't reuse current matrices in this case. */
13006 if (face_change_count)
13007 ++windows_or_buffers_changed;
13009 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13010 && FRAME_TTY (sf)->previous_frame != sf)
13012 /* Since frames on a single ASCII terminal share the same
13013 display area, displaying a different frame means redisplay
13014 the whole thing. */
13015 windows_or_buffers_changed++;
13016 SET_FRAME_GARBAGED (sf);
13017 #ifndef DOS_NT
13018 set_tty_color_mode (FRAME_TTY (sf), sf);
13019 #endif
13020 FRAME_TTY (sf)->previous_frame = sf;
13023 /* Set the visible flags for all frames. Do this before checking for
13024 resized or garbaged frames; they want to know if their frames are
13025 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13026 number_of_visible_frames = 0;
13028 FOR_EACH_FRAME (tail, frame)
13030 struct frame *f = XFRAME (frame);
13032 if (FRAME_VISIBLE_P (f))
13034 ++number_of_visible_frames;
13035 /* Adjust matrices for visible frames only. */
13036 if (f->fonts_changed)
13038 adjust_frame_glyphs (f);
13039 f->fonts_changed = 0;
13041 /* If cursor type has been changed on the frame
13042 other than selected, consider all frames. */
13043 if (f != sf && f->cursor_type_changed)
13044 update_mode_lines++;
13046 clear_desired_matrices (f);
13049 /* Notice any pending interrupt request to change frame size. */
13050 do_pending_window_change (1);
13052 /* do_pending_window_change could change the selected_window due to
13053 frame resizing which makes the selected window too small. */
13054 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13055 sw = w;
13057 /* Clear frames marked as garbaged. */
13058 clear_garbaged_frames ();
13060 /* Build menubar and tool-bar items. */
13061 if (NILP (Vmemory_full))
13062 prepare_menu_bars ();
13064 if (windows_or_buffers_changed)
13065 update_mode_lines++;
13067 reconsider_clip_changes (w);
13069 /* In most cases selected window displays current buffer. */
13070 match_p = XBUFFER (w->contents) == current_buffer;
13071 if (match_p)
13073 ptrdiff_t count1;
13075 /* Detect case that we need to write or remove a star in the mode line. */
13076 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13078 w->update_mode_line = 1;
13079 if (buffer_shared_and_changed ())
13080 update_mode_lines++;
13083 /* Avoid invocation of point motion hooks by `current_column' below. */
13084 count1 = SPECPDL_INDEX ();
13085 specbind (Qinhibit_point_motion_hooks, Qt);
13087 if (mode_line_update_needed (w))
13088 w->update_mode_line = 1;
13090 unbind_to (count1, Qnil);
13093 consider_all_windows_p = (update_mode_lines
13094 || buffer_shared_and_changed ());
13096 /* If specs for an arrow have changed, do thorough redisplay
13097 to ensure we remove any arrow that should no longer exist. */
13098 if (overlay_arrows_changed_p ())
13099 consider_all_windows_p = windows_or_buffers_changed = 1;
13101 /* Normally the message* functions will have already displayed and
13102 updated the echo area, but the frame may have been trashed, or
13103 the update may have been preempted, so display the echo area
13104 again here. Checking message_cleared_p captures the case that
13105 the echo area should be cleared. */
13106 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13107 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13108 || (message_cleared_p
13109 && minibuf_level == 0
13110 /* If the mini-window is currently selected, this means the
13111 echo-area doesn't show through. */
13112 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13114 int window_height_changed_p = echo_area_display (0);
13116 if (message_cleared_p)
13117 update_miniwindow_p = 1;
13119 must_finish = 1;
13121 /* If we don't display the current message, don't clear the
13122 message_cleared_p flag, because, if we did, we wouldn't clear
13123 the echo area in the next redisplay which doesn't preserve
13124 the echo area. */
13125 if (!display_last_displayed_message_p)
13126 message_cleared_p = 0;
13128 if (window_height_changed_p)
13130 consider_all_windows_p = 1;
13131 ++update_mode_lines;
13132 ++windows_or_buffers_changed;
13134 /* If window configuration was changed, frames may have been
13135 marked garbaged. Clear them or we will experience
13136 surprises wrt scrolling. */
13137 clear_garbaged_frames ();
13140 else if (EQ (selected_window, minibuf_window)
13141 && (current_buffer->clip_changed || window_outdated (w))
13142 && resize_mini_window (w, 0))
13144 /* Resized active mini-window to fit the size of what it is
13145 showing if its contents might have changed. */
13146 must_finish = 1;
13147 /* FIXME: this causes all frames to be updated, which seems unnecessary
13148 since only the current frame needs to be considered. This function
13149 needs to be rewritten with two variables, consider_all_windows and
13150 consider_all_frames. */
13151 consider_all_windows_p = 1;
13152 ++windows_or_buffers_changed;
13153 ++update_mode_lines;
13155 /* If window configuration was changed, frames may have been
13156 marked garbaged. Clear them or we will experience
13157 surprises wrt scrolling. */
13158 clear_garbaged_frames ();
13161 /* If showing the region, and mark has changed, we must redisplay
13162 the whole window. The assignment to this_line_start_pos prevents
13163 the optimization directly below this if-statement. */
13164 if (((!NILP (Vtransient_mark_mode)
13165 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13166 != (w->region_showing > 0))
13167 || (w->region_showing
13168 && w->region_showing
13169 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13170 CHARPOS (this_line_start_pos) = 0;
13172 /* Optimize the case that only the line containing the cursor in the
13173 selected window has changed. Variables starting with this_ are
13174 set in display_line and record information about the line
13175 containing the cursor. */
13176 tlbufpos = this_line_start_pos;
13177 tlendpos = this_line_end_pos;
13178 if (!consider_all_windows_p
13179 && CHARPOS (tlbufpos) > 0
13180 && !w->update_mode_line
13181 && !current_buffer->clip_changed
13182 && !current_buffer->prevent_redisplay_optimizations_p
13183 && FRAME_VISIBLE_P (XFRAME (w->frame))
13184 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13185 && !XFRAME (w->frame)->cursor_type_changed
13186 /* Make sure recorded data applies to current buffer, etc. */
13187 && this_line_buffer == current_buffer
13188 && match_p
13189 && !w->force_start
13190 && !w->optional_new_start
13191 /* Point must be on the line that we have info recorded about. */
13192 && PT >= CHARPOS (tlbufpos)
13193 && PT <= Z - CHARPOS (tlendpos)
13194 /* All text outside that line, including its final newline,
13195 must be unchanged. */
13196 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13197 CHARPOS (tlendpos)))
13199 if (CHARPOS (tlbufpos) > BEGV
13200 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13201 && (CHARPOS (tlbufpos) == ZV
13202 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13203 /* Former continuation line has disappeared by becoming empty. */
13204 goto cancel;
13205 else if (window_outdated (w) || MINI_WINDOW_P (w))
13207 /* We have to handle the case of continuation around a
13208 wide-column character (see the comment in indent.c around
13209 line 1340).
13211 For instance, in the following case:
13213 -------- Insert --------
13214 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13215 J_I_ ==> J_I_ `^^' are cursors.
13216 ^^ ^^
13217 -------- --------
13219 As we have to redraw the line above, we cannot use this
13220 optimization. */
13222 struct it it;
13223 int line_height_before = this_line_pixel_height;
13225 /* Note that start_display will handle the case that the
13226 line starting at tlbufpos is a continuation line. */
13227 start_display (&it, w, tlbufpos);
13229 /* Implementation note: It this still necessary? */
13230 if (it.current_x != this_line_start_x)
13231 goto cancel;
13233 TRACE ((stderr, "trying display optimization 1\n"));
13234 w->cursor.vpos = -1;
13235 overlay_arrow_seen = 0;
13236 it.vpos = this_line_vpos;
13237 it.current_y = this_line_y;
13238 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13239 display_line (&it);
13241 /* If line contains point, is not continued,
13242 and ends at same distance from eob as before, we win. */
13243 if (w->cursor.vpos >= 0
13244 /* Line is not continued, otherwise this_line_start_pos
13245 would have been set to 0 in display_line. */
13246 && CHARPOS (this_line_start_pos)
13247 /* Line ends as before. */
13248 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13249 /* Line has same height as before. Otherwise other lines
13250 would have to be shifted up or down. */
13251 && this_line_pixel_height == line_height_before)
13253 /* If this is not the window's last line, we must adjust
13254 the charstarts of the lines below. */
13255 if (it.current_y < it.last_visible_y)
13257 struct glyph_row *row
13258 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13259 ptrdiff_t delta, delta_bytes;
13261 /* We used to distinguish between two cases here,
13262 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13263 when the line ends in a newline or the end of the
13264 buffer's accessible portion. But both cases did
13265 the same, so they were collapsed. */
13266 delta = (Z
13267 - CHARPOS (tlendpos)
13268 - MATRIX_ROW_START_CHARPOS (row));
13269 delta_bytes = (Z_BYTE
13270 - BYTEPOS (tlendpos)
13271 - MATRIX_ROW_START_BYTEPOS (row));
13273 increment_matrix_positions (w->current_matrix,
13274 this_line_vpos + 1,
13275 w->current_matrix->nrows,
13276 delta, delta_bytes);
13279 /* If this row displays text now but previously didn't,
13280 or vice versa, w->window_end_vpos may have to be
13281 adjusted. */
13282 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13284 if (w->window_end_vpos < this_line_vpos)
13285 w->window_end_vpos = this_line_vpos;
13287 else if (w->window_end_vpos == this_line_vpos
13288 && this_line_vpos > 0)
13289 w->window_end_vpos = this_line_vpos - 1;
13290 w->window_end_valid = 0;
13292 /* Update hint: No need to try to scroll in update_window. */
13293 w->desired_matrix->no_scrolling_p = 1;
13295 #ifdef GLYPH_DEBUG
13296 *w->desired_matrix->method = 0;
13297 debug_method_add (w, "optimization 1");
13298 #endif
13299 #ifdef HAVE_WINDOW_SYSTEM
13300 update_window_fringes (w, 0);
13301 #endif
13302 goto update;
13304 else
13305 goto cancel;
13307 else if (/* Cursor position hasn't changed. */
13308 PT == w->last_point
13309 /* Make sure the cursor was last displayed
13310 in this window. Otherwise we have to reposition it. */
13311 && 0 <= w->cursor.vpos
13312 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13314 if (!must_finish)
13316 do_pending_window_change (1);
13317 /* If selected_window changed, redisplay again. */
13318 if (WINDOWP (selected_window)
13319 && (w = XWINDOW (selected_window)) != sw)
13320 goto retry;
13322 /* We used to always goto end_of_redisplay here, but this
13323 isn't enough if we have a blinking cursor. */
13324 if (w->cursor_off_p == w->last_cursor_off_p)
13325 goto end_of_redisplay;
13327 goto update;
13329 /* If highlighting the region, or if the cursor is in the echo area,
13330 then we can't just move the cursor. */
13331 else if (! (!NILP (Vtransient_mark_mode)
13332 && !NILP (BVAR (current_buffer, mark_active)))
13333 && (EQ (selected_window,
13334 BVAR (current_buffer, last_selected_window))
13335 || highlight_nonselected_windows)
13336 && !w->region_showing
13337 && NILP (Vshow_trailing_whitespace)
13338 && !cursor_in_echo_area)
13340 struct it it;
13341 struct glyph_row *row;
13343 /* Skip from tlbufpos to PT and see where it is. Note that
13344 PT may be in invisible text. If so, we will end at the
13345 next visible position. */
13346 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13347 NULL, DEFAULT_FACE_ID);
13348 it.current_x = this_line_start_x;
13349 it.current_y = this_line_y;
13350 it.vpos = this_line_vpos;
13352 /* The call to move_it_to stops in front of PT, but
13353 moves over before-strings. */
13354 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13356 if (it.vpos == this_line_vpos
13357 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13358 row->enabled_p))
13360 eassert (this_line_vpos == it.vpos);
13361 eassert (this_line_y == it.current_y);
13362 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13363 #ifdef GLYPH_DEBUG
13364 *w->desired_matrix->method = 0;
13365 debug_method_add (w, "optimization 3");
13366 #endif
13367 goto update;
13369 else
13370 goto cancel;
13373 cancel:
13374 /* Text changed drastically or point moved off of line. */
13375 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13378 CHARPOS (this_line_start_pos) = 0;
13379 consider_all_windows_p |= buffer_shared_and_changed ();
13380 ++clear_face_cache_count;
13381 #ifdef HAVE_WINDOW_SYSTEM
13382 ++clear_image_cache_count;
13383 #endif
13385 /* Build desired matrices, and update the display. If
13386 consider_all_windows_p is non-zero, do it for all windows on all
13387 frames. Otherwise do it for selected_window, only. */
13389 if (consider_all_windows_p)
13391 FOR_EACH_FRAME (tail, frame)
13392 XFRAME (frame)->updated_p = 0;
13394 FOR_EACH_FRAME (tail, frame)
13396 struct frame *f = XFRAME (frame);
13398 /* We don't have to do anything for unselected terminal
13399 frames. */
13400 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13401 && !EQ (FRAME_TTY (f)->top_frame, frame))
13402 continue;
13404 retry_frame:
13406 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13408 /* Mark all the scroll bars to be removed; we'll redeem
13409 the ones we want when we redisplay their windows. */
13410 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13411 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13413 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13414 redisplay_windows (FRAME_ROOT_WINDOW (f));
13416 /* The X error handler may have deleted that frame. */
13417 if (!FRAME_LIVE_P (f))
13418 continue;
13420 /* Any scroll bars which redisplay_windows should have
13421 nuked should now go away. */
13422 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13423 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13425 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13427 /* If fonts changed on visible frame, display again. */
13428 if (f->fonts_changed)
13430 adjust_frame_glyphs (f);
13431 f->fonts_changed = 0;
13432 goto retry_frame;
13435 /* See if we have to hscroll. */
13436 if (!f->already_hscrolled_p)
13438 f->already_hscrolled_p = 1;
13439 if (hscroll_windows (f->root_window))
13440 goto retry_frame;
13443 /* Prevent various kinds of signals during display
13444 update. stdio is not robust about handling
13445 signals, which can cause an apparent I/O
13446 error. */
13447 if (interrupt_input)
13448 unrequest_sigio ();
13449 STOP_POLLING;
13451 /* Update the display. */
13452 set_window_update_flags (XWINDOW (f->root_window), 1);
13453 pending |= update_frame (f, 0, 0);
13454 f->cursor_type_changed = 0;
13455 f->updated_p = 1;
13460 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13462 if (!pending)
13464 /* Do the mark_window_display_accurate after all windows have
13465 been redisplayed because this call resets flags in buffers
13466 which are needed for proper redisplay. */
13467 FOR_EACH_FRAME (tail, frame)
13469 struct frame *f = XFRAME (frame);
13470 if (f->updated_p)
13472 mark_window_display_accurate (f->root_window, 1);
13473 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13474 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13479 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13481 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13482 struct frame *mini_frame;
13484 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13485 /* Use list_of_error, not Qerror, so that
13486 we catch only errors and don't run the debugger. */
13487 internal_condition_case_1 (redisplay_window_1, selected_window,
13488 list_of_error,
13489 redisplay_window_error);
13490 if (update_miniwindow_p)
13491 internal_condition_case_1 (redisplay_window_1, mini_window,
13492 list_of_error,
13493 redisplay_window_error);
13495 /* Compare desired and current matrices, perform output. */
13497 update:
13498 /* If fonts changed, display again. */
13499 if (sf->fonts_changed)
13500 goto retry;
13502 /* Prevent various kinds of signals during display update.
13503 stdio is not robust about handling signals,
13504 which can cause an apparent I/O error. */
13505 if (interrupt_input)
13506 unrequest_sigio ();
13507 STOP_POLLING;
13509 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13511 if (hscroll_windows (selected_window))
13512 goto retry;
13514 XWINDOW (selected_window)->must_be_updated_p = 1;
13515 pending = update_frame (sf, 0, 0);
13516 sf->cursor_type_changed = 0;
13519 /* We may have called echo_area_display at the top of this
13520 function. If the echo area is on another frame, that may
13521 have put text on a frame other than the selected one, so the
13522 above call to update_frame would not have caught it. Catch
13523 it here. */
13524 mini_window = FRAME_MINIBUF_WINDOW (sf);
13525 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13527 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13529 XWINDOW (mini_window)->must_be_updated_p = 1;
13530 pending |= update_frame (mini_frame, 0, 0);
13531 mini_frame->cursor_type_changed = 0;
13532 if (!pending && hscroll_windows (mini_window))
13533 goto retry;
13537 /* If display was paused because of pending input, make sure we do a
13538 thorough update the next time. */
13539 if (pending)
13541 /* Prevent the optimization at the beginning of
13542 redisplay_internal that tries a single-line update of the
13543 line containing the cursor in the selected window. */
13544 CHARPOS (this_line_start_pos) = 0;
13546 /* Let the overlay arrow be updated the next time. */
13547 update_overlay_arrows (0);
13549 /* If we pause after scrolling, some rows in the current
13550 matrices of some windows are not valid. */
13551 if (!WINDOW_FULL_WIDTH_P (w)
13552 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13553 update_mode_lines = 1;
13555 else
13557 if (!consider_all_windows_p)
13559 /* This has already been done above if
13560 consider_all_windows_p is set. */
13561 mark_window_display_accurate_1 (w, 1);
13563 /* Say overlay arrows are up to date. */
13564 update_overlay_arrows (1);
13566 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13567 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13570 update_mode_lines = 0;
13571 windows_or_buffers_changed = 0;
13574 /* Start SIGIO interrupts coming again. Having them off during the
13575 code above makes it less likely one will discard output, but not
13576 impossible, since there might be stuff in the system buffer here.
13577 But it is much hairier to try to do anything about that. */
13578 if (interrupt_input)
13579 request_sigio ();
13580 RESUME_POLLING;
13582 /* If a frame has become visible which was not before, redisplay
13583 again, so that we display it. Expose events for such a frame
13584 (which it gets when becoming visible) don't call the parts of
13585 redisplay constructing glyphs, so simply exposing a frame won't
13586 display anything in this case. So, we have to display these
13587 frames here explicitly. */
13588 if (!pending)
13590 int new_count = 0;
13592 FOR_EACH_FRAME (tail, frame)
13594 int this_is_visible = 0;
13596 if (XFRAME (frame)->visible)
13597 this_is_visible = 1;
13599 if (this_is_visible)
13600 new_count++;
13603 if (new_count != number_of_visible_frames)
13604 windows_or_buffers_changed++;
13607 /* Change frame size now if a change is pending. */
13608 do_pending_window_change (1);
13610 /* If we just did a pending size change, or have additional
13611 visible frames, or selected_window changed, redisplay again. */
13612 if ((windows_or_buffers_changed && !pending)
13613 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13614 goto retry;
13616 /* Clear the face and image caches.
13618 We used to do this only if consider_all_windows_p. But the cache
13619 needs to be cleared if a timer creates images in the current
13620 buffer (e.g. the test case in Bug#6230). */
13622 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13624 clear_face_cache (0);
13625 clear_face_cache_count = 0;
13628 #ifdef HAVE_WINDOW_SYSTEM
13629 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13631 clear_image_caches (Qnil);
13632 clear_image_cache_count = 0;
13634 #endif /* HAVE_WINDOW_SYSTEM */
13636 end_of_redisplay:
13637 unbind_to (count, Qnil);
13638 RESUME_POLLING;
13642 /* Redisplay, but leave alone any recent echo area message unless
13643 another message has been requested in its place.
13645 This is useful in situations where you need to redisplay but no
13646 user action has occurred, making it inappropriate for the message
13647 area to be cleared. See tracking_off and
13648 wait_reading_process_output for examples of these situations.
13650 FROM_WHERE is an integer saying from where this function was
13651 called. This is useful for debugging. */
13653 void
13654 redisplay_preserve_echo_area (int from_where)
13656 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13658 if (!NILP (echo_area_buffer[1]))
13660 /* We have a previously displayed message, but no current
13661 message. Redisplay the previous message. */
13662 display_last_displayed_message_p = 1;
13663 redisplay_internal ();
13664 display_last_displayed_message_p = 0;
13666 else
13667 redisplay_internal ();
13669 flush_frame (SELECTED_FRAME ());
13673 /* Function registered with record_unwind_protect in redisplay_internal. */
13675 static void
13676 unwind_redisplay (void)
13678 redisplaying_p = 0;
13682 /* Mark the display of leaf window W as accurate or inaccurate.
13683 If ACCURATE_P is non-zero mark display of W as accurate. If
13684 ACCURATE_P is zero, arrange for W to be redisplayed the next
13685 time redisplay_internal is called. */
13687 static void
13688 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13690 struct buffer *b = XBUFFER (w->contents);
13692 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13693 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13694 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13696 if (accurate_p)
13698 b->clip_changed = 0;
13699 b->prevent_redisplay_optimizations_p = 0;
13701 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13702 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13703 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13704 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13706 w->current_matrix->buffer = b;
13707 w->current_matrix->begv = BUF_BEGV (b);
13708 w->current_matrix->zv = BUF_ZV (b);
13710 w->last_cursor_vpos = w->cursor.vpos;
13711 w->last_cursor_off_p = w->cursor_off_p;
13713 if (w == XWINDOW (selected_window))
13714 w->last_point = BUF_PT (b);
13715 else
13716 w->last_point = marker_position (w->pointm);
13718 w->window_end_valid = 1;
13719 w->update_mode_line = 0;
13724 /* Mark the display of windows in the window tree rooted at WINDOW as
13725 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13726 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13727 be redisplayed the next time redisplay_internal is called. */
13729 void
13730 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13732 struct window *w;
13734 for (; !NILP (window); window = w->next)
13736 w = XWINDOW (window);
13737 if (WINDOWP (w->contents))
13738 mark_window_display_accurate (w->contents, accurate_p);
13739 else
13740 mark_window_display_accurate_1 (w, accurate_p);
13743 if (accurate_p)
13744 update_overlay_arrows (1);
13745 else
13746 /* Force a thorough redisplay the next time by setting
13747 last_arrow_position and last_arrow_string to t, which is
13748 unequal to any useful value of Voverlay_arrow_... */
13749 update_overlay_arrows (-1);
13753 /* Return value in display table DP (Lisp_Char_Table *) for character
13754 C. Since a display table doesn't have any parent, we don't have to
13755 follow parent. Do not call this function directly but use the
13756 macro DISP_CHAR_VECTOR. */
13758 Lisp_Object
13759 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13761 Lisp_Object val;
13763 if (ASCII_CHAR_P (c))
13765 val = dp->ascii;
13766 if (SUB_CHAR_TABLE_P (val))
13767 val = XSUB_CHAR_TABLE (val)->contents[c];
13769 else
13771 Lisp_Object table;
13773 XSETCHAR_TABLE (table, dp);
13774 val = char_table_ref (table, c);
13776 if (NILP (val))
13777 val = dp->defalt;
13778 return val;
13783 /***********************************************************************
13784 Window Redisplay
13785 ***********************************************************************/
13787 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13789 static void
13790 redisplay_windows (Lisp_Object window)
13792 while (!NILP (window))
13794 struct window *w = XWINDOW (window);
13796 if (WINDOWP (w->contents))
13797 redisplay_windows (w->contents);
13798 else if (BUFFERP (w->contents))
13800 displayed_buffer = XBUFFER (w->contents);
13801 /* Use list_of_error, not Qerror, so that
13802 we catch only errors and don't run the debugger. */
13803 internal_condition_case_1 (redisplay_window_0, window,
13804 list_of_error,
13805 redisplay_window_error);
13808 window = w->next;
13812 static Lisp_Object
13813 redisplay_window_error (Lisp_Object ignore)
13815 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13816 return Qnil;
13819 static Lisp_Object
13820 redisplay_window_0 (Lisp_Object window)
13822 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13823 redisplay_window (window, 0);
13824 return Qnil;
13827 static Lisp_Object
13828 redisplay_window_1 (Lisp_Object window)
13830 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13831 redisplay_window (window, 1);
13832 return Qnil;
13836 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13837 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13838 which positions recorded in ROW differ from current buffer
13839 positions.
13841 Return 0 if cursor is not on this row, 1 otherwise. */
13843 static int
13844 set_cursor_from_row (struct window *w, struct glyph_row *row,
13845 struct glyph_matrix *matrix,
13846 ptrdiff_t delta, ptrdiff_t delta_bytes,
13847 int dy, int dvpos)
13849 struct glyph *glyph = row->glyphs[TEXT_AREA];
13850 struct glyph *end = glyph + row->used[TEXT_AREA];
13851 struct glyph *cursor = NULL;
13852 /* The last known character position in row. */
13853 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13854 int x = row->x;
13855 ptrdiff_t pt_old = PT - delta;
13856 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13857 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13858 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13859 /* A glyph beyond the edge of TEXT_AREA which we should never
13860 touch. */
13861 struct glyph *glyphs_end = end;
13862 /* Non-zero means we've found a match for cursor position, but that
13863 glyph has the avoid_cursor_p flag set. */
13864 int match_with_avoid_cursor = 0;
13865 /* Non-zero means we've seen at least one glyph that came from a
13866 display string. */
13867 int string_seen = 0;
13868 /* Largest and smallest buffer positions seen so far during scan of
13869 glyph row. */
13870 ptrdiff_t bpos_max = pos_before;
13871 ptrdiff_t bpos_min = pos_after;
13872 /* Last buffer position covered by an overlay string with an integer
13873 `cursor' property. */
13874 ptrdiff_t bpos_covered = 0;
13875 /* Non-zero means the display string on which to display the cursor
13876 comes from a text property, not from an overlay. */
13877 int string_from_text_prop = 0;
13879 /* Don't even try doing anything if called for a mode-line or
13880 header-line row, since the rest of the code isn't prepared to
13881 deal with such calamities. */
13882 eassert (!row->mode_line_p);
13883 if (row->mode_line_p)
13884 return 0;
13886 /* Skip over glyphs not having an object at the start and the end of
13887 the row. These are special glyphs like truncation marks on
13888 terminal frames. */
13889 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13891 if (!row->reversed_p)
13893 while (glyph < end
13894 && INTEGERP (glyph->object)
13895 && glyph->charpos < 0)
13897 x += glyph->pixel_width;
13898 ++glyph;
13900 while (end > glyph
13901 && INTEGERP ((end - 1)->object)
13902 /* CHARPOS is zero for blanks and stretch glyphs
13903 inserted by extend_face_to_end_of_line. */
13904 && (end - 1)->charpos <= 0)
13905 --end;
13906 glyph_before = glyph - 1;
13907 glyph_after = end;
13909 else
13911 struct glyph *g;
13913 /* If the glyph row is reversed, we need to process it from back
13914 to front, so swap the edge pointers. */
13915 glyphs_end = end = glyph - 1;
13916 glyph += row->used[TEXT_AREA] - 1;
13918 while (glyph > end + 1
13919 && INTEGERP (glyph->object)
13920 && glyph->charpos < 0)
13922 --glyph;
13923 x -= glyph->pixel_width;
13925 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13926 --glyph;
13927 /* By default, in reversed rows we put the cursor on the
13928 rightmost (first in the reading order) glyph. */
13929 for (g = end + 1; g < glyph; g++)
13930 x += g->pixel_width;
13931 while (end < glyph
13932 && INTEGERP ((end + 1)->object)
13933 && (end + 1)->charpos <= 0)
13934 ++end;
13935 glyph_before = glyph + 1;
13936 glyph_after = end;
13939 else if (row->reversed_p)
13941 /* In R2L rows that don't display text, put the cursor on the
13942 rightmost glyph. Case in point: an empty last line that is
13943 part of an R2L paragraph. */
13944 cursor = end - 1;
13945 /* Avoid placing the cursor on the last glyph of the row, where
13946 on terminal frames we hold the vertical border between
13947 adjacent windows. */
13948 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13949 && !WINDOW_RIGHTMOST_P (w)
13950 && cursor == row->glyphs[LAST_AREA] - 1)
13951 cursor--;
13952 x = -1; /* will be computed below, at label compute_x */
13955 /* Step 1: Try to find the glyph whose character position
13956 corresponds to point. If that's not possible, find 2 glyphs
13957 whose character positions are the closest to point, one before
13958 point, the other after it. */
13959 if (!row->reversed_p)
13960 while (/* not marched to end of glyph row */
13961 glyph < end
13962 /* glyph was not inserted by redisplay for internal purposes */
13963 && !INTEGERP (glyph->object))
13965 if (BUFFERP (glyph->object))
13967 ptrdiff_t dpos = glyph->charpos - pt_old;
13969 if (glyph->charpos > bpos_max)
13970 bpos_max = glyph->charpos;
13971 if (glyph->charpos < bpos_min)
13972 bpos_min = glyph->charpos;
13973 if (!glyph->avoid_cursor_p)
13975 /* If we hit point, we've found the glyph on which to
13976 display the cursor. */
13977 if (dpos == 0)
13979 match_with_avoid_cursor = 0;
13980 break;
13982 /* See if we've found a better approximation to
13983 POS_BEFORE or to POS_AFTER. */
13984 if (0 > dpos && dpos > pos_before - pt_old)
13986 pos_before = glyph->charpos;
13987 glyph_before = glyph;
13989 else if (0 < dpos && dpos < pos_after - pt_old)
13991 pos_after = glyph->charpos;
13992 glyph_after = glyph;
13995 else if (dpos == 0)
13996 match_with_avoid_cursor = 1;
13998 else if (STRINGP (glyph->object))
14000 Lisp_Object chprop;
14001 ptrdiff_t glyph_pos = glyph->charpos;
14003 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14004 glyph->object);
14005 if (!NILP (chprop))
14007 /* If the string came from a `display' text property,
14008 look up the buffer position of that property and
14009 use that position to update bpos_max, as if we
14010 actually saw such a position in one of the row's
14011 glyphs. This helps with supporting integer values
14012 of `cursor' property on the display string in
14013 situations where most or all of the row's buffer
14014 text is completely covered by display properties,
14015 so that no glyph with valid buffer positions is
14016 ever seen in the row. */
14017 ptrdiff_t prop_pos =
14018 string_buffer_position_lim (glyph->object, pos_before,
14019 pos_after, 0);
14021 if (prop_pos >= pos_before)
14022 bpos_max = prop_pos - 1;
14024 if (INTEGERP (chprop))
14026 bpos_covered = bpos_max + XINT (chprop);
14027 /* If the `cursor' property covers buffer positions up
14028 to and including point, we should display cursor on
14029 this glyph. Note that, if a `cursor' property on one
14030 of the string's characters has an integer value, we
14031 will break out of the loop below _before_ we get to
14032 the position match above. IOW, integer values of
14033 the `cursor' property override the "exact match for
14034 point" strategy of positioning the cursor. */
14035 /* Implementation note: bpos_max == pt_old when, e.g.,
14036 we are in an empty line, where bpos_max is set to
14037 MATRIX_ROW_START_CHARPOS, see above. */
14038 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14040 cursor = glyph;
14041 break;
14045 string_seen = 1;
14047 x += glyph->pixel_width;
14048 ++glyph;
14050 else if (glyph > end) /* row is reversed */
14051 while (!INTEGERP (glyph->object))
14053 if (BUFFERP (glyph->object))
14055 ptrdiff_t dpos = glyph->charpos - pt_old;
14057 if (glyph->charpos > bpos_max)
14058 bpos_max = glyph->charpos;
14059 if (glyph->charpos < bpos_min)
14060 bpos_min = glyph->charpos;
14061 if (!glyph->avoid_cursor_p)
14063 if (dpos == 0)
14065 match_with_avoid_cursor = 0;
14066 break;
14068 if (0 > dpos && dpos > pos_before - pt_old)
14070 pos_before = glyph->charpos;
14071 glyph_before = glyph;
14073 else if (0 < dpos && dpos < pos_after - pt_old)
14075 pos_after = glyph->charpos;
14076 glyph_after = glyph;
14079 else if (dpos == 0)
14080 match_with_avoid_cursor = 1;
14082 else if (STRINGP (glyph->object))
14084 Lisp_Object chprop;
14085 ptrdiff_t glyph_pos = glyph->charpos;
14087 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14088 glyph->object);
14089 if (!NILP (chprop))
14091 ptrdiff_t prop_pos =
14092 string_buffer_position_lim (glyph->object, pos_before,
14093 pos_after, 0);
14095 if (prop_pos >= pos_before)
14096 bpos_max = prop_pos - 1;
14098 if (INTEGERP (chprop))
14100 bpos_covered = bpos_max + XINT (chprop);
14101 /* If the `cursor' property covers buffer positions up
14102 to and including point, we should display cursor on
14103 this glyph. */
14104 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14106 cursor = glyph;
14107 break;
14110 string_seen = 1;
14112 --glyph;
14113 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14115 x--; /* can't use any pixel_width */
14116 break;
14118 x -= glyph->pixel_width;
14121 /* Step 2: If we didn't find an exact match for point, we need to
14122 look for a proper place to put the cursor among glyphs between
14123 GLYPH_BEFORE and GLYPH_AFTER. */
14124 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14125 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14126 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14128 /* An empty line has a single glyph whose OBJECT is zero and
14129 whose CHARPOS is the position of a newline on that line.
14130 Note that on a TTY, there are more glyphs after that, which
14131 were produced by extend_face_to_end_of_line, but their
14132 CHARPOS is zero or negative. */
14133 int empty_line_p =
14134 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14135 && INTEGERP (glyph->object) && glyph->charpos > 0
14136 /* On a TTY, continued and truncated rows also have a glyph at
14137 their end whose OBJECT is zero and whose CHARPOS is
14138 positive (the continuation and truncation glyphs), but such
14139 rows are obviously not "empty". */
14140 && !(row->continued_p || row->truncated_on_right_p);
14142 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14144 ptrdiff_t ellipsis_pos;
14146 /* Scan back over the ellipsis glyphs. */
14147 if (!row->reversed_p)
14149 ellipsis_pos = (glyph - 1)->charpos;
14150 while (glyph > row->glyphs[TEXT_AREA]
14151 && (glyph - 1)->charpos == ellipsis_pos)
14152 glyph--, x -= glyph->pixel_width;
14153 /* That loop always goes one position too far, including
14154 the glyph before the ellipsis. So scan forward over
14155 that one. */
14156 x += glyph->pixel_width;
14157 glyph++;
14159 else /* row is reversed */
14161 ellipsis_pos = (glyph + 1)->charpos;
14162 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14163 && (glyph + 1)->charpos == ellipsis_pos)
14164 glyph++, x += glyph->pixel_width;
14165 x -= glyph->pixel_width;
14166 glyph--;
14169 else if (match_with_avoid_cursor)
14171 cursor = glyph_after;
14172 x = -1;
14174 else if (string_seen)
14176 int incr = row->reversed_p ? -1 : +1;
14178 /* Need to find the glyph that came out of a string which is
14179 present at point. That glyph is somewhere between
14180 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14181 positioned between POS_BEFORE and POS_AFTER in the
14182 buffer. */
14183 struct glyph *start, *stop;
14184 ptrdiff_t pos = pos_before;
14186 x = -1;
14188 /* If the row ends in a newline from a display string,
14189 reordering could have moved the glyphs belonging to the
14190 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14191 in this case we extend the search to the last glyph in
14192 the row that was not inserted by redisplay. */
14193 if (row->ends_in_newline_from_string_p)
14195 glyph_after = end;
14196 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14199 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14200 correspond to POS_BEFORE and POS_AFTER, respectively. We
14201 need START and STOP in the order that corresponds to the
14202 row's direction as given by its reversed_p flag. If the
14203 directionality of characters between POS_BEFORE and
14204 POS_AFTER is the opposite of the row's base direction,
14205 these characters will have been reordered for display,
14206 and we need to reverse START and STOP. */
14207 if (!row->reversed_p)
14209 start = min (glyph_before, glyph_after);
14210 stop = max (glyph_before, glyph_after);
14212 else
14214 start = max (glyph_before, glyph_after);
14215 stop = min (glyph_before, glyph_after);
14217 for (glyph = start + incr;
14218 row->reversed_p ? glyph > stop : glyph < stop; )
14221 /* Any glyphs that come from the buffer are here because
14222 of bidi reordering. Skip them, and only pay
14223 attention to glyphs that came from some string. */
14224 if (STRINGP (glyph->object))
14226 Lisp_Object str;
14227 ptrdiff_t tem;
14228 /* If the display property covers the newline, we
14229 need to search for it one position farther. */
14230 ptrdiff_t lim = pos_after
14231 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14233 string_from_text_prop = 0;
14234 str = glyph->object;
14235 tem = string_buffer_position_lim (str, pos, lim, 0);
14236 if (tem == 0 /* from overlay */
14237 || pos <= tem)
14239 /* If the string from which this glyph came is
14240 found in the buffer at point, or at position
14241 that is closer to point than pos_after, then
14242 we've found the glyph we've been looking for.
14243 If it comes from an overlay (tem == 0), and
14244 it has the `cursor' property on one of its
14245 glyphs, record that glyph as a candidate for
14246 displaying the cursor. (As in the
14247 unidirectional version, we will display the
14248 cursor on the last candidate we find.) */
14249 if (tem == 0
14250 || tem == pt_old
14251 || (tem - pt_old > 0 && tem < pos_after))
14253 /* The glyphs from this string could have
14254 been reordered. Find the one with the
14255 smallest string position. Or there could
14256 be a character in the string with the
14257 `cursor' property, which means display
14258 cursor on that character's glyph. */
14259 ptrdiff_t strpos = glyph->charpos;
14261 if (tem)
14263 cursor = glyph;
14264 string_from_text_prop = 1;
14266 for ( ;
14267 (row->reversed_p ? glyph > stop : glyph < stop)
14268 && EQ (glyph->object, str);
14269 glyph += incr)
14271 Lisp_Object cprop;
14272 ptrdiff_t gpos = glyph->charpos;
14274 cprop = Fget_char_property (make_number (gpos),
14275 Qcursor,
14276 glyph->object);
14277 if (!NILP (cprop))
14279 cursor = glyph;
14280 break;
14282 if (tem && glyph->charpos < strpos)
14284 strpos = glyph->charpos;
14285 cursor = glyph;
14289 if (tem == pt_old
14290 || (tem - pt_old > 0 && tem < pos_after))
14291 goto compute_x;
14293 if (tem)
14294 pos = tem + 1; /* don't find previous instances */
14296 /* This string is not what we want; skip all of the
14297 glyphs that came from it. */
14298 while ((row->reversed_p ? glyph > stop : glyph < stop)
14299 && EQ (glyph->object, str))
14300 glyph += incr;
14302 else
14303 glyph += incr;
14306 /* If we reached the end of the line, and END was from a string,
14307 the cursor is not on this line. */
14308 if (cursor == NULL
14309 && (row->reversed_p ? glyph <= end : glyph >= end)
14310 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14311 && STRINGP (end->object)
14312 && row->continued_p)
14313 return 0;
14315 /* A truncated row may not include PT among its character positions.
14316 Setting the cursor inside the scroll margin will trigger
14317 recalculation of hscroll in hscroll_window_tree. But if a
14318 display string covers point, defer to the string-handling
14319 code below to figure this out. */
14320 else if (row->truncated_on_left_p && pt_old < bpos_min)
14322 cursor = glyph_before;
14323 x = -1;
14325 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14326 /* Zero-width characters produce no glyphs. */
14327 || (!empty_line_p
14328 && (row->reversed_p
14329 ? glyph_after > glyphs_end
14330 : glyph_after < glyphs_end)))
14332 cursor = glyph_after;
14333 x = -1;
14337 compute_x:
14338 if (cursor != NULL)
14339 glyph = cursor;
14340 else if (glyph == glyphs_end
14341 && pos_before == pos_after
14342 && STRINGP ((row->reversed_p
14343 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14344 : row->glyphs[TEXT_AREA])->object))
14346 /* If all the glyphs of this row came from strings, put the
14347 cursor on the first glyph of the row. This avoids having the
14348 cursor outside of the text area in this very rare and hard
14349 use case. */
14350 glyph =
14351 row->reversed_p
14352 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14353 : row->glyphs[TEXT_AREA];
14355 if (x < 0)
14357 struct glyph *g;
14359 /* Need to compute x that corresponds to GLYPH. */
14360 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14362 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14363 emacs_abort ();
14364 x += g->pixel_width;
14368 /* ROW could be part of a continued line, which, under bidi
14369 reordering, might have other rows whose start and end charpos
14370 occlude point. Only set w->cursor if we found a better
14371 approximation to the cursor position than we have from previously
14372 examined candidate rows belonging to the same continued line. */
14373 if (/* we already have a candidate row */
14374 w->cursor.vpos >= 0
14375 /* that candidate is not the row we are processing */
14376 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14377 /* Make sure cursor.vpos specifies a row whose start and end
14378 charpos occlude point, and it is valid candidate for being a
14379 cursor-row. This is because some callers of this function
14380 leave cursor.vpos at the row where the cursor was displayed
14381 during the last redisplay cycle. */
14382 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14383 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14384 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14386 struct glyph *g1 =
14387 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14389 /* Don't consider glyphs that are outside TEXT_AREA. */
14390 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14391 return 0;
14392 /* Keep the candidate whose buffer position is the closest to
14393 point or has the `cursor' property. */
14394 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14395 w->cursor.hpos >= 0
14396 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14397 && ((BUFFERP (g1->object)
14398 && (g1->charpos == pt_old /* an exact match always wins */
14399 || (BUFFERP (glyph->object)
14400 && eabs (g1->charpos - pt_old)
14401 < eabs (glyph->charpos - pt_old))))
14402 /* previous candidate is a glyph from a string that has
14403 a non-nil `cursor' property */
14404 || (STRINGP (g1->object)
14405 && (!NILP (Fget_char_property (make_number (g1->charpos),
14406 Qcursor, g1->object))
14407 /* previous candidate is from the same display
14408 string as this one, and the display string
14409 came from a text property */
14410 || (EQ (g1->object, glyph->object)
14411 && string_from_text_prop)
14412 /* this candidate is from newline and its
14413 position is not an exact match */
14414 || (INTEGERP (glyph->object)
14415 && glyph->charpos != pt_old)))))
14416 return 0;
14417 /* If this candidate gives an exact match, use that. */
14418 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14419 /* If this candidate is a glyph created for the
14420 terminating newline of a line, and point is on that
14421 newline, it wins because it's an exact match. */
14422 || (!row->continued_p
14423 && INTEGERP (glyph->object)
14424 && glyph->charpos == 0
14425 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14426 /* Otherwise, keep the candidate that comes from a row
14427 spanning less buffer positions. This may win when one or
14428 both candidate positions are on glyphs that came from
14429 display strings, for which we cannot compare buffer
14430 positions. */
14431 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14432 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14433 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14434 return 0;
14436 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14437 w->cursor.x = x;
14438 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14439 w->cursor.y = row->y + dy;
14441 if (w == XWINDOW (selected_window))
14443 if (!row->continued_p
14444 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14445 && row->x == 0)
14447 this_line_buffer = XBUFFER (w->contents);
14449 CHARPOS (this_line_start_pos)
14450 = MATRIX_ROW_START_CHARPOS (row) + delta;
14451 BYTEPOS (this_line_start_pos)
14452 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14454 CHARPOS (this_line_end_pos)
14455 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14456 BYTEPOS (this_line_end_pos)
14457 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14459 this_line_y = w->cursor.y;
14460 this_line_pixel_height = row->height;
14461 this_line_vpos = w->cursor.vpos;
14462 this_line_start_x = row->x;
14464 else
14465 CHARPOS (this_line_start_pos) = 0;
14468 return 1;
14472 /* Run window scroll functions, if any, for WINDOW with new window
14473 start STARTP. Sets the window start of WINDOW to that position.
14475 We assume that the window's buffer is really current. */
14477 static struct text_pos
14478 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14480 struct window *w = XWINDOW (window);
14481 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14483 eassert (current_buffer == XBUFFER (w->contents));
14485 if (!NILP (Vwindow_scroll_functions))
14487 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14488 make_number (CHARPOS (startp)));
14489 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14490 /* In case the hook functions switch buffers. */
14491 set_buffer_internal (XBUFFER (w->contents));
14494 return startp;
14498 /* Make sure the line containing the cursor is fully visible.
14499 A value of 1 means there is nothing to be done.
14500 (Either the line is fully visible, or it cannot be made so,
14501 or we cannot tell.)
14503 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14504 is higher than window.
14506 A value of 0 means the caller should do scrolling
14507 as if point had gone off the screen. */
14509 static int
14510 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14512 struct glyph_matrix *matrix;
14513 struct glyph_row *row;
14514 int window_height;
14516 if (!make_cursor_line_fully_visible_p)
14517 return 1;
14519 /* It's not always possible to find the cursor, e.g, when a window
14520 is full of overlay strings. Don't do anything in that case. */
14521 if (w->cursor.vpos < 0)
14522 return 1;
14524 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14525 row = MATRIX_ROW (matrix, w->cursor.vpos);
14527 /* If the cursor row is not partially visible, there's nothing to do. */
14528 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14529 return 1;
14531 /* If the row the cursor is in is taller than the window's height,
14532 it's not clear what to do, so do nothing. */
14533 window_height = window_box_height (w);
14534 if (row->height >= window_height)
14536 if (!force_p || MINI_WINDOW_P (w)
14537 || w->vscroll || w->cursor.vpos == 0)
14538 return 1;
14540 return 0;
14544 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14545 non-zero means only WINDOW is redisplayed in redisplay_internal.
14546 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14547 in redisplay_window to bring a partially visible line into view in
14548 the case that only the cursor has moved.
14550 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14551 last screen line's vertical height extends past the end of the screen.
14553 Value is
14555 1 if scrolling succeeded
14557 0 if scrolling didn't find point.
14559 -1 if new fonts have been loaded so that we must interrupt
14560 redisplay, adjust glyph matrices, and try again. */
14562 enum
14564 SCROLLING_SUCCESS,
14565 SCROLLING_FAILED,
14566 SCROLLING_NEED_LARGER_MATRICES
14569 /* If scroll-conservatively is more than this, never recenter.
14571 If you change this, don't forget to update the doc string of
14572 `scroll-conservatively' and the Emacs manual. */
14573 #define SCROLL_LIMIT 100
14575 static int
14576 try_scrolling (Lisp_Object window, int just_this_one_p,
14577 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14578 int temp_scroll_step, int last_line_misfit)
14580 struct window *w = XWINDOW (window);
14581 struct frame *f = XFRAME (w->frame);
14582 struct text_pos pos, startp;
14583 struct it it;
14584 int this_scroll_margin, scroll_max, rc, height;
14585 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14586 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14587 Lisp_Object aggressive;
14588 /* We will never try scrolling more than this number of lines. */
14589 int scroll_limit = SCROLL_LIMIT;
14590 int frame_line_height = default_line_pixel_height (w);
14591 int window_total_lines
14592 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14594 #ifdef GLYPH_DEBUG
14595 debug_method_add (w, "try_scrolling");
14596 #endif
14598 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14600 /* Compute scroll margin height in pixels. We scroll when point is
14601 within this distance from the top or bottom of the window. */
14602 if (scroll_margin > 0)
14603 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14604 * frame_line_height;
14605 else
14606 this_scroll_margin = 0;
14608 /* Force arg_scroll_conservatively to have a reasonable value, to
14609 avoid scrolling too far away with slow move_it_* functions. Note
14610 that the user can supply scroll-conservatively equal to
14611 `most-positive-fixnum', which can be larger than INT_MAX. */
14612 if (arg_scroll_conservatively > scroll_limit)
14614 arg_scroll_conservatively = scroll_limit + 1;
14615 scroll_max = scroll_limit * frame_line_height;
14617 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14618 /* Compute how much we should try to scroll maximally to bring
14619 point into view. */
14620 scroll_max = (max (scroll_step,
14621 max (arg_scroll_conservatively, temp_scroll_step))
14622 * frame_line_height);
14623 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14624 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14625 /* We're trying to scroll because of aggressive scrolling but no
14626 scroll_step is set. Choose an arbitrary one. */
14627 scroll_max = 10 * frame_line_height;
14628 else
14629 scroll_max = 0;
14631 too_near_end:
14633 /* Decide whether to scroll down. */
14634 if (PT > CHARPOS (startp))
14636 int scroll_margin_y;
14638 /* Compute the pixel ypos of the scroll margin, then move IT to
14639 either that ypos or PT, whichever comes first. */
14640 start_display (&it, w, startp);
14641 scroll_margin_y = it.last_visible_y - this_scroll_margin
14642 - frame_line_height * extra_scroll_margin_lines;
14643 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14644 (MOVE_TO_POS | MOVE_TO_Y));
14646 if (PT > CHARPOS (it.current.pos))
14648 int y0 = line_bottom_y (&it);
14649 /* Compute how many pixels below window bottom to stop searching
14650 for PT. This avoids costly search for PT that is far away if
14651 the user limited scrolling by a small number of lines, but
14652 always finds PT if scroll_conservatively is set to a large
14653 number, such as most-positive-fixnum. */
14654 int slack = max (scroll_max, 10 * frame_line_height);
14655 int y_to_move = it.last_visible_y + slack;
14657 /* Compute the distance from the scroll margin to PT or to
14658 the scroll limit, whichever comes first. This should
14659 include the height of the cursor line, to make that line
14660 fully visible. */
14661 move_it_to (&it, PT, -1, y_to_move,
14662 -1, MOVE_TO_POS | MOVE_TO_Y);
14663 dy = line_bottom_y (&it) - y0;
14665 if (dy > scroll_max)
14666 return SCROLLING_FAILED;
14668 if (dy > 0)
14669 scroll_down_p = 1;
14673 if (scroll_down_p)
14675 /* Point is in or below the bottom scroll margin, so move the
14676 window start down. If scrolling conservatively, move it just
14677 enough down to make point visible. If scroll_step is set,
14678 move it down by scroll_step. */
14679 if (arg_scroll_conservatively)
14680 amount_to_scroll
14681 = min (max (dy, frame_line_height),
14682 frame_line_height * arg_scroll_conservatively);
14683 else if (scroll_step || temp_scroll_step)
14684 amount_to_scroll = scroll_max;
14685 else
14687 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14688 height = WINDOW_BOX_TEXT_HEIGHT (w);
14689 if (NUMBERP (aggressive))
14691 double float_amount = XFLOATINT (aggressive) * height;
14692 int aggressive_scroll = float_amount;
14693 if (aggressive_scroll == 0 && float_amount > 0)
14694 aggressive_scroll = 1;
14695 /* Don't let point enter the scroll margin near top of
14696 the window. This could happen if the value of
14697 scroll_up_aggressively is too large and there are
14698 non-zero margins, because scroll_up_aggressively
14699 means put point that fraction of window height
14700 _from_the_bottom_margin_. */
14701 if (aggressive_scroll + 2*this_scroll_margin > height)
14702 aggressive_scroll = height - 2*this_scroll_margin;
14703 amount_to_scroll = dy + aggressive_scroll;
14707 if (amount_to_scroll <= 0)
14708 return SCROLLING_FAILED;
14710 start_display (&it, w, startp);
14711 if (arg_scroll_conservatively <= scroll_limit)
14712 move_it_vertically (&it, amount_to_scroll);
14713 else
14715 /* Extra precision for users who set scroll-conservatively
14716 to a large number: make sure the amount we scroll
14717 the window start is never less than amount_to_scroll,
14718 which was computed as distance from window bottom to
14719 point. This matters when lines at window top and lines
14720 below window bottom have different height. */
14721 struct it it1;
14722 void *it1data = NULL;
14723 /* We use a temporary it1 because line_bottom_y can modify
14724 its argument, if it moves one line down; see there. */
14725 int start_y;
14727 SAVE_IT (it1, it, it1data);
14728 start_y = line_bottom_y (&it1);
14729 do {
14730 RESTORE_IT (&it, &it, it1data);
14731 move_it_by_lines (&it, 1);
14732 SAVE_IT (it1, it, it1data);
14733 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14736 /* If STARTP is unchanged, move it down another screen line. */
14737 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14738 move_it_by_lines (&it, 1);
14739 startp = it.current.pos;
14741 else
14743 struct text_pos scroll_margin_pos = startp;
14744 int y_offset = 0;
14746 /* See if point is inside the scroll margin at the top of the
14747 window. */
14748 if (this_scroll_margin)
14750 int y_start;
14752 start_display (&it, w, startp);
14753 y_start = it.current_y;
14754 move_it_vertically (&it, this_scroll_margin);
14755 scroll_margin_pos = it.current.pos;
14756 /* If we didn't move enough before hitting ZV, request
14757 additional amount of scroll, to move point out of the
14758 scroll margin. */
14759 if (IT_CHARPOS (it) == ZV
14760 && it.current_y - y_start < this_scroll_margin)
14761 y_offset = this_scroll_margin - (it.current_y - y_start);
14764 if (PT < CHARPOS (scroll_margin_pos))
14766 /* Point is in the scroll margin at the top of the window or
14767 above what is displayed in the window. */
14768 int y0, y_to_move;
14770 /* Compute the vertical distance from PT to the scroll
14771 margin position. Move as far as scroll_max allows, or
14772 one screenful, or 10 screen lines, whichever is largest.
14773 Give up if distance is greater than scroll_max or if we
14774 didn't reach the scroll margin position. */
14775 SET_TEXT_POS (pos, PT, PT_BYTE);
14776 start_display (&it, w, pos);
14777 y0 = it.current_y;
14778 y_to_move = max (it.last_visible_y,
14779 max (scroll_max, 10 * frame_line_height));
14780 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14781 y_to_move, -1,
14782 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14783 dy = it.current_y - y0;
14784 if (dy > scroll_max
14785 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14786 return SCROLLING_FAILED;
14788 /* Additional scroll for when ZV was too close to point. */
14789 dy += y_offset;
14791 /* Compute new window start. */
14792 start_display (&it, w, startp);
14794 if (arg_scroll_conservatively)
14795 amount_to_scroll = max (dy, frame_line_height *
14796 max (scroll_step, temp_scroll_step));
14797 else if (scroll_step || temp_scroll_step)
14798 amount_to_scroll = scroll_max;
14799 else
14801 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14802 height = WINDOW_BOX_TEXT_HEIGHT (w);
14803 if (NUMBERP (aggressive))
14805 double float_amount = XFLOATINT (aggressive) * height;
14806 int aggressive_scroll = float_amount;
14807 if (aggressive_scroll == 0 && float_amount > 0)
14808 aggressive_scroll = 1;
14809 /* Don't let point enter the scroll margin near
14810 bottom of the window, if the value of
14811 scroll_down_aggressively happens to be too
14812 large. */
14813 if (aggressive_scroll + 2*this_scroll_margin > height)
14814 aggressive_scroll = height - 2*this_scroll_margin;
14815 amount_to_scroll = dy + aggressive_scroll;
14819 if (amount_to_scroll <= 0)
14820 return SCROLLING_FAILED;
14822 move_it_vertically_backward (&it, amount_to_scroll);
14823 startp = it.current.pos;
14827 /* Run window scroll functions. */
14828 startp = run_window_scroll_functions (window, startp);
14830 /* Display the window. Give up if new fonts are loaded, or if point
14831 doesn't appear. */
14832 if (!try_window (window, startp, 0))
14833 rc = SCROLLING_NEED_LARGER_MATRICES;
14834 else if (w->cursor.vpos < 0)
14836 clear_glyph_matrix (w->desired_matrix);
14837 rc = SCROLLING_FAILED;
14839 else
14841 /* Maybe forget recorded base line for line number display. */
14842 if (!just_this_one_p
14843 || current_buffer->clip_changed
14844 || BEG_UNCHANGED < CHARPOS (startp))
14845 w->base_line_number = 0;
14847 /* If cursor ends up on a partially visible line,
14848 treat that as being off the bottom of the screen. */
14849 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14850 /* It's possible that the cursor is on the first line of the
14851 buffer, which is partially obscured due to a vscroll
14852 (Bug#7537). In that case, avoid looping forever . */
14853 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14855 clear_glyph_matrix (w->desired_matrix);
14856 ++extra_scroll_margin_lines;
14857 goto too_near_end;
14859 rc = SCROLLING_SUCCESS;
14862 return rc;
14866 /* Compute a suitable window start for window W if display of W starts
14867 on a continuation line. Value is non-zero if a new window start
14868 was computed.
14870 The new window start will be computed, based on W's width, starting
14871 from the start of the continued line. It is the start of the
14872 screen line with the minimum distance from the old start W->start. */
14874 static int
14875 compute_window_start_on_continuation_line (struct window *w)
14877 struct text_pos pos, start_pos;
14878 int window_start_changed_p = 0;
14880 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14882 /* If window start is on a continuation line... Window start may be
14883 < BEGV in case there's invisible text at the start of the
14884 buffer (M-x rmail, for example). */
14885 if (CHARPOS (start_pos) > BEGV
14886 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14888 struct it it;
14889 struct glyph_row *row;
14891 /* Handle the case that the window start is out of range. */
14892 if (CHARPOS (start_pos) < BEGV)
14893 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14894 else if (CHARPOS (start_pos) > ZV)
14895 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14897 /* Find the start of the continued line. This should be fast
14898 because find_newline is fast (newline cache). */
14899 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14900 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14901 row, DEFAULT_FACE_ID);
14902 reseat_at_previous_visible_line_start (&it);
14904 /* If the line start is "too far" away from the window start,
14905 say it takes too much time to compute a new window start. */
14906 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14907 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14909 int min_distance, distance;
14911 /* Move forward by display lines to find the new window
14912 start. If window width was enlarged, the new start can
14913 be expected to be > the old start. If window width was
14914 decreased, the new window start will be < the old start.
14915 So, we're looking for the display line start with the
14916 minimum distance from the old window start. */
14917 pos = it.current.pos;
14918 min_distance = INFINITY;
14919 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14920 distance < min_distance)
14922 min_distance = distance;
14923 pos = it.current.pos;
14924 if (it.line_wrap == WORD_WRAP)
14926 /* Under WORD_WRAP, move_it_by_lines is likely to
14927 overshoot and stop not at the first, but the
14928 second character from the left margin. So in
14929 that case, we need a more tight control on the X
14930 coordinate of the iterator than move_it_by_lines
14931 promises in its contract. The method is to first
14932 go to the last (rightmost) visible character of a
14933 line, then move to the leftmost character on the
14934 next line in a separate call. */
14935 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14936 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14937 move_it_to (&it, ZV, 0,
14938 it.current_y + it.max_ascent + it.max_descent, -1,
14939 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14941 else
14942 move_it_by_lines (&it, 1);
14945 /* Set the window start there. */
14946 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14947 window_start_changed_p = 1;
14951 return window_start_changed_p;
14955 /* Try cursor movement in case text has not changed in window WINDOW,
14956 with window start STARTP. Value is
14958 CURSOR_MOVEMENT_SUCCESS if successful
14960 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14962 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14963 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14964 we want to scroll as if scroll-step were set to 1. See the code.
14966 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14967 which case we have to abort this redisplay, and adjust matrices
14968 first. */
14970 enum
14972 CURSOR_MOVEMENT_SUCCESS,
14973 CURSOR_MOVEMENT_CANNOT_BE_USED,
14974 CURSOR_MOVEMENT_MUST_SCROLL,
14975 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14978 static int
14979 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14981 struct window *w = XWINDOW (window);
14982 struct frame *f = XFRAME (w->frame);
14983 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14985 #ifdef GLYPH_DEBUG
14986 if (inhibit_try_cursor_movement)
14987 return rc;
14988 #endif
14990 /* Previously, there was a check for Lisp integer in the
14991 if-statement below. Now, this field is converted to
14992 ptrdiff_t, thus zero means invalid position in a buffer. */
14993 eassert (w->last_point > 0);
14994 /* Likewise there was a check whether window_end_vpos is nil or larger
14995 than the window. Now window_end_vpos is int and so never nil, but
14996 let's leave eassert to check whether it fits in the window. */
14997 eassert (w->window_end_vpos < w->current_matrix->nrows);
14999 /* Handle case where text has not changed, only point, and it has
15000 not moved off the frame. */
15001 if (/* Point may be in this window. */
15002 PT >= CHARPOS (startp)
15003 /* Selective display hasn't changed. */
15004 && !current_buffer->clip_changed
15005 /* Function force-mode-line-update is used to force a thorough
15006 redisplay. It sets either windows_or_buffers_changed or
15007 update_mode_lines. So don't take a shortcut here for these
15008 cases. */
15009 && !update_mode_lines
15010 && !windows_or_buffers_changed
15011 && !f->cursor_type_changed
15012 /* Can't use this case if highlighting a region. When a
15013 region exists, cursor movement has to do more than just
15014 set the cursor. */
15015 && markpos_of_region () < 0
15016 && !w->region_showing
15017 && NILP (Vshow_trailing_whitespace)
15018 /* This code is not used for mini-buffer for the sake of the case
15019 of redisplaying to replace an echo area message; since in
15020 that case the mini-buffer contents per se are usually
15021 unchanged. This code is of no real use in the mini-buffer
15022 since the handling of this_line_start_pos, etc., in redisplay
15023 handles the same cases. */
15024 && !EQ (window, minibuf_window)
15025 && (FRAME_WINDOW_P (f)
15026 || !overlay_arrow_in_current_buffer_p ()))
15028 int this_scroll_margin, top_scroll_margin;
15029 struct glyph_row *row = NULL;
15030 int frame_line_height = default_line_pixel_height (w);
15031 int window_total_lines
15032 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15034 #ifdef GLYPH_DEBUG
15035 debug_method_add (w, "cursor movement");
15036 #endif
15038 /* Scroll if point within this distance from the top or bottom
15039 of the window. This is a pixel value. */
15040 if (scroll_margin > 0)
15042 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15043 this_scroll_margin *= frame_line_height;
15045 else
15046 this_scroll_margin = 0;
15048 top_scroll_margin = this_scroll_margin;
15049 if (WINDOW_WANTS_HEADER_LINE_P (w))
15050 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15052 /* Start with the row the cursor was displayed during the last
15053 not paused redisplay. Give up if that row is not valid. */
15054 if (w->last_cursor_vpos < 0
15055 || w->last_cursor_vpos >= w->current_matrix->nrows)
15056 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15057 else
15059 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15060 if (row->mode_line_p)
15061 ++row;
15062 if (!row->enabled_p)
15063 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15066 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15068 int scroll_p = 0, must_scroll = 0;
15069 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15071 if (PT > w->last_point)
15073 /* Point has moved forward. */
15074 while (MATRIX_ROW_END_CHARPOS (row) < PT
15075 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15077 eassert (row->enabled_p);
15078 ++row;
15081 /* If the end position of a row equals the start
15082 position of the next row, and PT is at that position,
15083 we would rather display cursor in the next line. */
15084 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15085 && MATRIX_ROW_END_CHARPOS (row) == PT
15086 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15087 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15088 && !cursor_row_p (row))
15089 ++row;
15091 /* If within the scroll margin, scroll. Note that
15092 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15093 the next line would be drawn, and that
15094 this_scroll_margin can be zero. */
15095 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15096 || PT > MATRIX_ROW_END_CHARPOS (row)
15097 /* Line is completely visible last line in window
15098 and PT is to be set in the next line. */
15099 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15100 && PT == MATRIX_ROW_END_CHARPOS (row)
15101 && !row->ends_at_zv_p
15102 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15103 scroll_p = 1;
15105 else if (PT < w->last_point)
15107 /* Cursor has to be moved backward. Note that PT >=
15108 CHARPOS (startp) because of the outer if-statement. */
15109 while (!row->mode_line_p
15110 && (MATRIX_ROW_START_CHARPOS (row) > PT
15111 || (MATRIX_ROW_START_CHARPOS (row) == PT
15112 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15113 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15114 row > w->current_matrix->rows
15115 && (row-1)->ends_in_newline_from_string_p))))
15116 && (row->y > top_scroll_margin
15117 || CHARPOS (startp) == BEGV))
15119 eassert (row->enabled_p);
15120 --row;
15123 /* Consider the following case: Window starts at BEGV,
15124 there is invisible, intangible text at BEGV, so that
15125 display starts at some point START > BEGV. It can
15126 happen that we are called with PT somewhere between
15127 BEGV and START. Try to handle that case. */
15128 if (row < w->current_matrix->rows
15129 || row->mode_line_p)
15131 row = w->current_matrix->rows;
15132 if (row->mode_line_p)
15133 ++row;
15136 /* Due to newlines in overlay strings, we may have to
15137 skip forward over overlay strings. */
15138 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15139 && MATRIX_ROW_END_CHARPOS (row) == PT
15140 && !cursor_row_p (row))
15141 ++row;
15143 /* If within the scroll margin, scroll. */
15144 if (row->y < top_scroll_margin
15145 && CHARPOS (startp) != BEGV)
15146 scroll_p = 1;
15148 else
15150 /* Cursor did not move. So don't scroll even if cursor line
15151 is partially visible, as it was so before. */
15152 rc = CURSOR_MOVEMENT_SUCCESS;
15155 if (PT < MATRIX_ROW_START_CHARPOS (row)
15156 || PT > MATRIX_ROW_END_CHARPOS (row))
15158 /* if PT is not in the glyph row, give up. */
15159 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15160 must_scroll = 1;
15162 else if (rc != CURSOR_MOVEMENT_SUCCESS
15163 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15165 struct glyph_row *row1;
15167 /* If rows are bidi-reordered and point moved, back up
15168 until we find a row that does not belong to a
15169 continuation line. This is because we must consider
15170 all rows of a continued line as candidates for the
15171 new cursor positioning, since row start and end
15172 positions change non-linearly with vertical position
15173 in such rows. */
15174 /* FIXME: Revisit this when glyph ``spilling'' in
15175 continuation lines' rows is implemented for
15176 bidi-reordered rows. */
15177 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15178 MATRIX_ROW_CONTINUATION_LINE_P (row);
15179 --row)
15181 /* If we hit the beginning of the displayed portion
15182 without finding the first row of a continued
15183 line, give up. */
15184 if (row <= row1)
15186 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15187 break;
15189 eassert (row->enabled_p);
15192 if (must_scroll)
15194 else if (rc != CURSOR_MOVEMENT_SUCCESS
15195 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15196 /* Make sure this isn't a header line by any chance, since
15197 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15198 && !row->mode_line_p
15199 && make_cursor_line_fully_visible_p)
15201 if (PT == MATRIX_ROW_END_CHARPOS (row)
15202 && !row->ends_at_zv_p
15203 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15204 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15205 else if (row->height > window_box_height (w))
15207 /* If we end up in a partially visible line, let's
15208 make it fully visible, except when it's taller
15209 than the window, in which case we can't do much
15210 about it. */
15211 *scroll_step = 1;
15212 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15214 else
15216 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15217 if (!cursor_row_fully_visible_p (w, 0, 1))
15218 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15219 else
15220 rc = CURSOR_MOVEMENT_SUCCESS;
15223 else if (scroll_p)
15224 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15225 else if (rc != CURSOR_MOVEMENT_SUCCESS
15226 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15228 /* With bidi-reordered rows, there could be more than
15229 one candidate row whose start and end positions
15230 occlude point. We need to let set_cursor_from_row
15231 find the best candidate. */
15232 /* FIXME: Revisit this when glyph ``spilling'' in
15233 continuation lines' rows is implemented for
15234 bidi-reordered rows. */
15235 int rv = 0;
15239 int at_zv_p = 0, exact_match_p = 0;
15241 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15242 && PT <= MATRIX_ROW_END_CHARPOS (row)
15243 && cursor_row_p (row))
15244 rv |= set_cursor_from_row (w, row, w->current_matrix,
15245 0, 0, 0, 0);
15246 /* As soon as we've found the exact match for point,
15247 or the first suitable row whose ends_at_zv_p flag
15248 is set, we are done. */
15249 at_zv_p =
15250 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15251 if (rv && !at_zv_p
15252 && w->cursor.hpos >= 0
15253 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15254 w->cursor.vpos))
15256 struct glyph_row *candidate =
15257 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15258 struct glyph *g =
15259 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15260 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15262 exact_match_p =
15263 (BUFFERP (g->object) && g->charpos == PT)
15264 || (INTEGERP (g->object)
15265 && (g->charpos == PT
15266 || (g->charpos == 0 && endpos - 1 == PT)));
15268 if (rv && (at_zv_p || exact_match_p))
15270 rc = CURSOR_MOVEMENT_SUCCESS;
15271 break;
15273 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15274 break;
15275 ++row;
15277 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15278 || row->continued_p)
15279 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15280 || (MATRIX_ROW_START_CHARPOS (row) == PT
15281 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15282 /* If we didn't find any candidate rows, or exited the
15283 loop before all the candidates were examined, signal
15284 to the caller that this method failed. */
15285 if (rc != CURSOR_MOVEMENT_SUCCESS
15286 && !(rv
15287 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15288 && !row->continued_p))
15289 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15290 else if (rv)
15291 rc = CURSOR_MOVEMENT_SUCCESS;
15293 else
15297 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15299 rc = CURSOR_MOVEMENT_SUCCESS;
15300 break;
15302 ++row;
15304 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15305 && MATRIX_ROW_START_CHARPOS (row) == PT
15306 && cursor_row_p (row));
15311 return rc;
15314 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15315 static
15316 #endif
15317 void
15318 set_vertical_scroll_bar (struct window *w)
15320 ptrdiff_t start, end, whole;
15322 /* Calculate the start and end positions for the current window.
15323 At some point, it would be nice to choose between scrollbars
15324 which reflect the whole buffer size, with special markers
15325 indicating narrowing, and scrollbars which reflect only the
15326 visible region.
15328 Note that mini-buffers sometimes aren't displaying any text. */
15329 if (!MINI_WINDOW_P (w)
15330 || (w == XWINDOW (minibuf_window)
15331 && NILP (echo_area_buffer[0])))
15333 struct buffer *buf = XBUFFER (w->contents);
15334 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15335 start = marker_position (w->start) - BUF_BEGV (buf);
15336 /* I don't think this is guaranteed to be right. For the
15337 moment, we'll pretend it is. */
15338 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15340 if (end < start)
15341 end = start;
15342 if (whole < (end - start))
15343 whole = end - start;
15345 else
15346 start = end = whole = 0;
15348 /* Indicate what this scroll bar ought to be displaying now. */
15349 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15350 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15351 (w, end - start, whole, start);
15355 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15356 selected_window is redisplayed.
15358 We can return without actually redisplaying the window if fonts has been
15359 changed on window's frame. In that case, redisplay_internal will retry. */
15361 static void
15362 redisplay_window (Lisp_Object window, int just_this_one_p)
15364 struct window *w = XWINDOW (window);
15365 struct frame *f = XFRAME (w->frame);
15366 struct buffer *buffer = XBUFFER (w->contents);
15367 struct buffer *old = current_buffer;
15368 struct text_pos lpoint, opoint, startp;
15369 int update_mode_line;
15370 int tem;
15371 struct it it;
15372 /* Record it now because it's overwritten. */
15373 int current_matrix_up_to_date_p = 0;
15374 int used_current_matrix_p = 0;
15375 /* This is less strict than current_matrix_up_to_date_p.
15376 It indicates that the buffer contents and narrowing are unchanged. */
15377 int buffer_unchanged_p = 0;
15378 int temp_scroll_step = 0;
15379 ptrdiff_t count = SPECPDL_INDEX ();
15380 int rc;
15381 int centering_position = -1;
15382 int last_line_misfit = 0;
15383 ptrdiff_t beg_unchanged, end_unchanged;
15384 int frame_line_height;
15386 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15387 opoint = lpoint;
15389 #ifdef GLYPH_DEBUG
15390 *w->desired_matrix->method = 0;
15391 #endif
15393 /* Make sure that both W's markers are valid. */
15394 eassert (XMARKER (w->start)->buffer == buffer);
15395 eassert (XMARKER (w->pointm)->buffer == buffer);
15397 restart:
15398 reconsider_clip_changes (w);
15399 frame_line_height = default_line_pixel_height (w);
15401 /* Has the mode line to be updated? */
15402 update_mode_line = (w->update_mode_line
15403 || update_mode_lines
15404 || buffer->clip_changed
15405 || buffer->prevent_redisplay_optimizations_p);
15407 if (MINI_WINDOW_P (w))
15409 if (w == XWINDOW (echo_area_window)
15410 && !NILP (echo_area_buffer[0]))
15412 if (update_mode_line)
15413 /* We may have to update a tty frame's menu bar or a
15414 tool-bar. Example `M-x C-h C-h C-g'. */
15415 goto finish_menu_bars;
15416 else
15417 /* We've already displayed the echo area glyphs in this window. */
15418 goto finish_scroll_bars;
15420 else if ((w != XWINDOW (minibuf_window)
15421 || minibuf_level == 0)
15422 /* When buffer is nonempty, redisplay window normally. */
15423 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15424 /* Quail displays non-mini buffers in minibuffer window.
15425 In that case, redisplay the window normally. */
15426 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15428 /* W is a mini-buffer window, but it's not active, so clear
15429 it. */
15430 int yb = window_text_bottom_y (w);
15431 struct glyph_row *row;
15432 int y;
15434 for (y = 0, row = w->desired_matrix->rows;
15435 y < yb;
15436 y += row->height, ++row)
15437 blank_row (w, row, y);
15438 goto finish_scroll_bars;
15441 clear_glyph_matrix (w->desired_matrix);
15444 /* Otherwise set up data on this window; select its buffer and point
15445 value. */
15446 /* Really select the buffer, for the sake of buffer-local
15447 variables. */
15448 set_buffer_internal_1 (XBUFFER (w->contents));
15450 current_matrix_up_to_date_p
15451 = (w->window_end_valid
15452 && !current_buffer->clip_changed
15453 && !current_buffer->prevent_redisplay_optimizations_p
15454 && !window_outdated (w));
15456 /* Run the window-bottom-change-functions
15457 if it is possible that the text on the screen has changed
15458 (either due to modification of the text, or any other reason). */
15459 if (!current_matrix_up_to_date_p
15460 && !NILP (Vwindow_text_change_functions))
15462 safe_run_hooks (Qwindow_text_change_functions);
15463 goto restart;
15466 beg_unchanged = BEG_UNCHANGED;
15467 end_unchanged = END_UNCHANGED;
15469 SET_TEXT_POS (opoint, PT, PT_BYTE);
15471 specbind (Qinhibit_point_motion_hooks, Qt);
15473 buffer_unchanged_p
15474 = (w->window_end_valid
15475 && !current_buffer->clip_changed
15476 && !window_outdated (w));
15478 /* When windows_or_buffers_changed is non-zero, we can't rely
15479 on the window end being valid, so set it to zero there. */
15480 if (windows_or_buffers_changed)
15482 /* If window starts on a continuation line, maybe adjust the
15483 window start in case the window's width changed. */
15484 if (XMARKER (w->start)->buffer == current_buffer)
15485 compute_window_start_on_continuation_line (w);
15487 w->window_end_valid = 0;
15488 /* If so, we also can't rely on current matrix
15489 and should not fool try_cursor_movement below. */
15490 current_matrix_up_to_date_p = 0;
15493 /* Some sanity checks. */
15494 CHECK_WINDOW_END (w);
15495 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15496 emacs_abort ();
15497 if (BYTEPOS (opoint) < CHARPOS (opoint))
15498 emacs_abort ();
15500 if (mode_line_update_needed (w))
15501 update_mode_line = 1;
15503 /* Point refers normally to the selected window. For any other
15504 window, set up appropriate value. */
15505 if (!EQ (window, selected_window))
15507 ptrdiff_t new_pt = marker_position (w->pointm);
15508 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15509 if (new_pt < BEGV)
15511 new_pt = BEGV;
15512 new_pt_byte = BEGV_BYTE;
15513 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15515 else if (new_pt > (ZV - 1))
15517 new_pt = ZV;
15518 new_pt_byte = ZV_BYTE;
15519 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15522 /* We don't use SET_PT so that the point-motion hooks don't run. */
15523 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15526 /* If any of the character widths specified in the display table
15527 have changed, invalidate the width run cache. It's true that
15528 this may be a bit late to catch such changes, but the rest of
15529 redisplay goes (non-fatally) haywire when the display table is
15530 changed, so why should we worry about doing any better? */
15531 if (current_buffer->width_run_cache)
15533 struct Lisp_Char_Table *disptab = buffer_display_table ();
15535 if (! disptab_matches_widthtab
15536 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15538 invalidate_region_cache (current_buffer,
15539 current_buffer->width_run_cache,
15540 BEG, Z);
15541 recompute_width_table (current_buffer, disptab);
15545 /* If window-start is screwed up, choose a new one. */
15546 if (XMARKER (w->start)->buffer != current_buffer)
15547 goto recenter;
15549 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15551 /* If someone specified a new starting point but did not insist,
15552 check whether it can be used. */
15553 if (w->optional_new_start
15554 && CHARPOS (startp) >= BEGV
15555 && CHARPOS (startp) <= ZV)
15557 w->optional_new_start = 0;
15558 start_display (&it, w, startp);
15559 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15560 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15561 if (IT_CHARPOS (it) == PT)
15562 w->force_start = 1;
15563 /* IT may overshoot PT if text at PT is invisible. */
15564 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15565 w->force_start = 1;
15568 force_start:
15570 /* Handle case where place to start displaying has been specified,
15571 unless the specified location is outside the accessible range. */
15572 if (w->force_start || window_frozen_p (w))
15574 /* We set this later on if we have to adjust point. */
15575 int new_vpos = -1;
15577 w->force_start = 0;
15578 w->vscroll = 0;
15579 w->window_end_valid = 0;
15581 /* Forget any recorded base line for line number display. */
15582 if (!buffer_unchanged_p)
15583 w->base_line_number = 0;
15585 /* Redisplay the mode line. Select the buffer properly for that.
15586 Also, run the hook window-scroll-functions
15587 because we have scrolled. */
15588 /* Note, we do this after clearing force_start because
15589 if there's an error, it is better to forget about force_start
15590 than to get into an infinite loop calling the hook functions
15591 and having them get more errors. */
15592 if (!update_mode_line
15593 || ! NILP (Vwindow_scroll_functions))
15595 update_mode_line = 1;
15596 w->update_mode_line = 1;
15597 startp = run_window_scroll_functions (window, startp);
15600 if (CHARPOS (startp) < BEGV)
15601 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15602 else if (CHARPOS (startp) > ZV)
15603 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15605 /* Redisplay, then check if cursor has been set during the
15606 redisplay. Give up if new fonts were loaded. */
15607 /* We used to issue a CHECK_MARGINS argument to try_window here,
15608 but this causes scrolling to fail when point begins inside
15609 the scroll margin (bug#148) -- cyd */
15610 if (!try_window (window, startp, 0))
15612 w->force_start = 1;
15613 clear_glyph_matrix (w->desired_matrix);
15614 goto need_larger_matrices;
15617 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15619 /* If point does not appear, try to move point so it does
15620 appear. The desired matrix has been built above, so we
15621 can use it here. */
15622 new_vpos = window_box_height (w) / 2;
15625 if (!cursor_row_fully_visible_p (w, 0, 0))
15627 /* Point does appear, but on a line partly visible at end of window.
15628 Move it back to a fully-visible line. */
15629 new_vpos = window_box_height (w);
15631 else if (w->cursor.vpos >=0)
15633 /* Some people insist on not letting point enter the scroll
15634 margin, even though this part handles windows that didn't
15635 scroll at all. */
15636 int window_total_lines
15637 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15638 int margin = min (scroll_margin, window_total_lines / 4);
15639 int pixel_margin = margin * frame_line_height;
15640 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15642 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15643 below, which finds the row to move point to, advances by
15644 the Y coordinate of the _next_ row, see the definition of
15645 MATRIX_ROW_BOTTOM_Y. */
15646 if (w->cursor.vpos < margin + header_line)
15648 w->cursor.vpos = -1;
15649 clear_glyph_matrix (w->desired_matrix);
15650 goto try_to_scroll;
15652 else
15654 int window_height = window_box_height (w);
15656 if (header_line)
15657 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15658 if (w->cursor.y >= window_height - pixel_margin)
15660 w->cursor.vpos = -1;
15661 clear_glyph_matrix (w->desired_matrix);
15662 goto try_to_scroll;
15667 /* If we need to move point for either of the above reasons,
15668 now actually do it. */
15669 if (new_vpos >= 0)
15671 struct glyph_row *row;
15673 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15674 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15675 ++row;
15677 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15678 MATRIX_ROW_START_BYTEPOS (row));
15680 if (w != XWINDOW (selected_window))
15681 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15682 else if (current_buffer == old)
15683 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15685 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15687 /* If we are highlighting the region, then we just changed
15688 the region, so redisplay to show it. */
15689 if (markpos_of_region () >= 0)
15691 clear_glyph_matrix (w->desired_matrix);
15692 if (!try_window (window, startp, 0))
15693 goto need_larger_matrices;
15697 #ifdef GLYPH_DEBUG
15698 debug_method_add (w, "forced window start");
15699 #endif
15700 goto done;
15703 /* Handle case where text has not changed, only point, and it has
15704 not moved off the frame, and we are not retrying after hscroll.
15705 (current_matrix_up_to_date_p is nonzero when retrying.) */
15706 if (current_matrix_up_to_date_p
15707 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15708 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15710 switch (rc)
15712 case CURSOR_MOVEMENT_SUCCESS:
15713 used_current_matrix_p = 1;
15714 goto done;
15716 case CURSOR_MOVEMENT_MUST_SCROLL:
15717 goto try_to_scroll;
15719 default:
15720 emacs_abort ();
15723 /* If current starting point was originally the beginning of a line
15724 but no longer is, find a new starting point. */
15725 else if (w->start_at_line_beg
15726 && !(CHARPOS (startp) <= BEGV
15727 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15729 #ifdef GLYPH_DEBUG
15730 debug_method_add (w, "recenter 1");
15731 #endif
15732 goto recenter;
15735 /* Try scrolling with try_window_id. Value is > 0 if update has
15736 been done, it is -1 if we know that the same window start will
15737 not work. It is 0 if unsuccessful for some other reason. */
15738 else if ((tem = try_window_id (w)) != 0)
15740 #ifdef GLYPH_DEBUG
15741 debug_method_add (w, "try_window_id %d", tem);
15742 #endif
15744 if (f->fonts_changed)
15745 goto need_larger_matrices;
15746 if (tem > 0)
15747 goto done;
15749 /* Otherwise try_window_id has returned -1 which means that we
15750 don't want the alternative below this comment to execute. */
15752 else if (CHARPOS (startp) >= BEGV
15753 && CHARPOS (startp) <= ZV
15754 && PT >= CHARPOS (startp)
15755 && (CHARPOS (startp) < ZV
15756 /* Avoid starting at end of buffer. */
15757 || CHARPOS (startp) == BEGV
15758 || !window_outdated (w)))
15760 int d1, d2, d3, d4, d5, d6;
15762 /* If first window line is a continuation line, and window start
15763 is inside the modified region, but the first change is before
15764 current window start, we must select a new window start.
15766 However, if this is the result of a down-mouse event (e.g. by
15767 extending the mouse-drag-overlay), we don't want to select a
15768 new window start, since that would change the position under
15769 the mouse, resulting in an unwanted mouse-movement rather
15770 than a simple mouse-click. */
15771 if (!w->start_at_line_beg
15772 && NILP (do_mouse_tracking)
15773 && CHARPOS (startp) > BEGV
15774 && CHARPOS (startp) > BEG + beg_unchanged
15775 && CHARPOS (startp) <= Z - end_unchanged
15776 /* Even if w->start_at_line_beg is nil, a new window may
15777 start at a line_beg, since that's how set_buffer_window
15778 sets it. So, we need to check the return value of
15779 compute_window_start_on_continuation_line. (See also
15780 bug#197). */
15781 && XMARKER (w->start)->buffer == current_buffer
15782 && compute_window_start_on_continuation_line (w)
15783 /* It doesn't make sense to force the window start like we
15784 do at label force_start if it is already known that point
15785 will not be visible in the resulting window, because
15786 doing so will move point from its correct position
15787 instead of scrolling the window to bring point into view.
15788 See bug#9324. */
15789 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15791 w->force_start = 1;
15792 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15793 goto force_start;
15796 #ifdef GLYPH_DEBUG
15797 debug_method_add (w, "same window start");
15798 #endif
15800 /* Try to redisplay starting at same place as before.
15801 If point has not moved off frame, accept the results. */
15802 if (!current_matrix_up_to_date_p
15803 /* Don't use try_window_reusing_current_matrix in this case
15804 because a window scroll function can have changed the
15805 buffer. */
15806 || !NILP (Vwindow_scroll_functions)
15807 || MINI_WINDOW_P (w)
15808 || !(used_current_matrix_p
15809 = try_window_reusing_current_matrix (w)))
15811 IF_DEBUG (debug_method_add (w, "1"));
15812 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15813 /* -1 means we need to scroll.
15814 0 means we need new matrices, but fonts_changed
15815 is set in that case, so we will detect it below. */
15816 goto try_to_scroll;
15819 if (f->fonts_changed)
15820 goto need_larger_matrices;
15822 if (w->cursor.vpos >= 0)
15824 if (!just_this_one_p
15825 || current_buffer->clip_changed
15826 || BEG_UNCHANGED < CHARPOS (startp))
15827 /* Forget any recorded base line for line number display. */
15828 w->base_line_number = 0;
15830 if (!cursor_row_fully_visible_p (w, 1, 0))
15832 clear_glyph_matrix (w->desired_matrix);
15833 last_line_misfit = 1;
15835 /* Drop through and scroll. */
15836 else
15837 goto done;
15839 else
15840 clear_glyph_matrix (w->desired_matrix);
15843 try_to_scroll:
15845 /* Redisplay the mode line. Select the buffer properly for that. */
15846 if (!update_mode_line)
15848 update_mode_line = 1;
15849 w->update_mode_line = 1;
15852 /* Try to scroll by specified few lines. */
15853 if ((scroll_conservatively
15854 || emacs_scroll_step
15855 || temp_scroll_step
15856 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15857 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15858 && CHARPOS (startp) >= BEGV
15859 && CHARPOS (startp) <= ZV)
15861 /* The function returns -1 if new fonts were loaded, 1 if
15862 successful, 0 if not successful. */
15863 int ss = try_scrolling (window, just_this_one_p,
15864 scroll_conservatively,
15865 emacs_scroll_step,
15866 temp_scroll_step, last_line_misfit);
15867 switch (ss)
15869 case SCROLLING_SUCCESS:
15870 goto done;
15872 case SCROLLING_NEED_LARGER_MATRICES:
15873 goto need_larger_matrices;
15875 case SCROLLING_FAILED:
15876 break;
15878 default:
15879 emacs_abort ();
15883 /* Finally, just choose a place to start which positions point
15884 according to user preferences. */
15886 recenter:
15888 #ifdef GLYPH_DEBUG
15889 debug_method_add (w, "recenter");
15890 #endif
15892 /* Forget any previously recorded base line for line number display. */
15893 if (!buffer_unchanged_p)
15894 w->base_line_number = 0;
15896 /* Determine the window start relative to point. */
15897 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15898 it.current_y = it.last_visible_y;
15899 if (centering_position < 0)
15901 int window_total_lines
15902 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15903 int margin =
15904 scroll_margin > 0
15905 ? min (scroll_margin, window_total_lines / 4)
15906 : 0;
15907 ptrdiff_t margin_pos = CHARPOS (startp);
15908 Lisp_Object aggressive;
15909 int scrolling_up;
15911 /* If there is a scroll margin at the top of the window, find
15912 its character position. */
15913 if (margin
15914 /* Cannot call start_display if startp is not in the
15915 accessible region of the buffer. This can happen when we
15916 have just switched to a different buffer and/or changed
15917 its restriction. In that case, startp is initialized to
15918 the character position 1 (BEGV) because we did not yet
15919 have chance to display the buffer even once. */
15920 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15922 struct it it1;
15923 void *it1data = NULL;
15925 SAVE_IT (it1, it, it1data);
15926 start_display (&it1, w, startp);
15927 move_it_vertically (&it1, margin * frame_line_height);
15928 margin_pos = IT_CHARPOS (it1);
15929 RESTORE_IT (&it, &it, it1data);
15931 scrolling_up = PT > margin_pos;
15932 aggressive =
15933 scrolling_up
15934 ? BVAR (current_buffer, scroll_up_aggressively)
15935 : BVAR (current_buffer, scroll_down_aggressively);
15937 if (!MINI_WINDOW_P (w)
15938 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15940 int pt_offset = 0;
15942 /* Setting scroll-conservatively overrides
15943 scroll-*-aggressively. */
15944 if (!scroll_conservatively && NUMBERP (aggressive))
15946 double float_amount = XFLOATINT (aggressive);
15948 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15949 if (pt_offset == 0 && float_amount > 0)
15950 pt_offset = 1;
15951 if (pt_offset && margin > 0)
15952 margin -= 1;
15954 /* Compute how much to move the window start backward from
15955 point so that point will be displayed where the user
15956 wants it. */
15957 if (scrolling_up)
15959 centering_position = it.last_visible_y;
15960 if (pt_offset)
15961 centering_position -= pt_offset;
15962 centering_position -=
15963 frame_line_height * (1 + margin + (last_line_misfit != 0))
15964 + WINDOW_HEADER_LINE_HEIGHT (w);
15965 /* Don't let point enter the scroll margin near top of
15966 the window. */
15967 if (centering_position < margin * frame_line_height)
15968 centering_position = margin * frame_line_height;
15970 else
15971 centering_position = margin * frame_line_height + pt_offset;
15973 else
15974 /* Set the window start half the height of the window backward
15975 from point. */
15976 centering_position = window_box_height (w) / 2;
15978 move_it_vertically_backward (&it, centering_position);
15980 eassert (IT_CHARPOS (it) >= BEGV);
15982 /* The function move_it_vertically_backward may move over more
15983 than the specified y-distance. If it->w is small, e.g. a
15984 mini-buffer window, we may end up in front of the window's
15985 display area. Start displaying at the start of the line
15986 containing PT in this case. */
15987 if (it.current_y <= 0)
15989 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15990 move_it_vertically_backward (&it, 0);
15991 it.current_y = 0;
15994 it.current_x = it.hpos = 0;
15996 /* Set the window start position here explicitly, to avoid an
15997 infinite loop in case the functions in window-scroll-functions
15998 get errors. */
15999 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16001 /* Run scroll hooks. */
16002 startp = run_window_scroll_functions (window, it.current.pos);
16004 /* Redisplay the window. */
16005 if (!current_matrix_up_to_date_p
16006 || windows_or_buffers_changed
16007 || f->cursor_type_changed
16008 /* Don't use try_window_reusing_current_matrix in this case
16009 because it can have changed the buffer. */
16010 || !NILP (Vwindow_scroll_functions)
16011 || !just_this_one_p
16012 || MINI_WINDOW_P (w)
16013 || !(used_current_matrix_p
16014 = try_window_reusing_current_matrix (w)))
16015 try_window (window, startp, 0);
16017 /* If new fonts have been loaded (due to fontsets), give up. We
16018 have to start a new redisplay since we need to re-adjust glyph
16019 matrices. */
16020 if (f->fonts_changed)
16021 goto need_larger_matrices;
16023 /* If cursor did not appear assume that the middle of the window is
16024 in the first line of the window. Do it again with the next line.
16025 (Imagine a window of height 100, displaying two lines of height
16026 60. Moving back 50 from it->last_visible_y will end in the first
16027 line.) */
16028 if (w->cursor.vpos < 0)
16030 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16032 clear_glyph_matrix (w->desired_matrix);
16033 move_it_by_lines (&it, 1);
16034 try_window (window, it.current.pos, 0);
16036 else if (PT < IT_CHARPOS (it))
16038 clear_glyph_matrix (w->desired_matrix);
16039 move_it_by_lines (&it, -1);
16040 try_window (window, it.current.pos, 0);
16042 else
16044 /* Not much we can do about it. */
16048 /* Consider the following case: Window starts at BEGV, there is
16049 invisible, intangible text at BEGV, so that display starts at
16050 some point START > BEGV. It can happen that we are called with
16051 PT somewhere between BEGV and START. Try to handle that case. */
16052 if (w->cursor.vpos < 0)
16054 struct glyph_row *row = w->current_matrix->rows;
16055 if (row->mode_line_p)
16056 ++row;
16057 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16060 if (!cursor_row_fully_visible_p (w, 0, 0))
16062 /* If vscroll is enabled, disable it and try again. */
16063 if (w->vscroll)
16065 w->vscroll = 0;
16066 clear_glyph_matrix (w->desired_matrix);
16067 goto recenter;
16070 /* Users who set scroll-conservatively to a large number want
16071 point just above/below the scroll margin. If we ended up
16072 with point's row partially visible, move the window start to
16073 make that row fully visible and out of the margin. */
16074 if (scroll_conservatively > SCROLL_LIMIT)
16076 int window_total_lines
16077 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16078 int margin =
16079 scroll_margin > 0
16080 ? min (scroll_margin, window_total_lines / 4)
16081 : 0;
16082 int move_down = w->cursor.vpos >= window_total_lines / 2;
16084 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16085 clear_glyph_matrix (w->desired_matrix);
16086 if (1 == try_window (window, it.current.pos,
16087 TRY_WINDOW_CHECK_MARGINS))
16088 goto done;
16091 /* If centering point failed to make the whole line visible,
16092 put point at the top instead. That has to make the whole line
16093 visible, if it can be done. */
16094 if (centering_position == 0)
16095 goto done;
16097 clear_glyph_matrix (w->desired_matrix);
16098 centering_position = 0;
16099 goto recenter;
16102 done:
16104 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16105 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16106 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16108 /* Display the mode line, if we must. */
16109 if ((update_mode_line
16110 /* If window not full width, must redo its mode line
16111 if (a) the window to its side is being redone and
16112 (b) we do a frame-based redisplay. This is a consequence
16113 of how inverted lines are drawn in frame-based redisplay. */
16114 || (!just_this_one_p
16115 && !FRAME_WINDOW_P (f)
16116 && !WINDOW_FULL_WIDTH_P (w))
16117 /* Line number to display. */
16118 || w->base_line_pos > 0
16119 /* Column number is displayed and different from the one displayed. */
16120 || (w->column_number_displayed != -1
16121 && (w->column_number_displayed != current_column ())))
16122 /* This means that the window has a mode line. */
16123 && (WINDOW_WANTS_MODELINE_P (w)
16124 || WINDOW_WANTS_HEADER_LINE_P (w)))
16126 display_mode_lines (w);
16128 /* If mode line height has changed, arrange for a thorough
16129 immediate redisplay using the correct mode line height. */
16130 if (WINDOW_WANTS_MODELINE_P (w)
16131 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16133 f->fonts_changed = 1;
16134 w->mode_line_height = -1;
16135 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16136 = DESIRED_MODE_LINE_HEIGHT (w);
16139 /* If header line height has changed, arrange for a thorough
16140 immediate redisplay using the correct header line height. */
16141 if (WINDOW_WANTS_HEADER_LINE_P (w)
16142 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16144 f->fonts_changed = 1;
16145 w->header_line_height = -1;
16146 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16147 = DESIRED_HEADER_LINE_HEIGHT (w);
16150 if (f->fonts_changed)
16151 goto need_larger_matrices;
16154 if (!line_number_displayed && w->base_line_pos != -1)
16156 w->base_line_pos = 0;
16157 w->base_line_number = 0;
16160 finish_menu_bars:
16162 /* When we reach a frame's selected window, redo the frame's menu bar. */
16163 if (update_mode_line
16164 && EQ (FRAME_SELECTED_WINDOW (f), window))
16166 int redisplay_menu_p = 0;
16168 if (FRAME_WINDOW_P (f))
16170 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16171 || defined (HAVE_NS) || defined (USE_GTK)
16172 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16173 #else
16174 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16175 #endif
16177 else
16178 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16180 if (redisplay_menu_p)
16181 display_menu_bar (w);
16183 #ifdef HAVE_WINDOW_SYSTEM
16184 if (FRAME_WINDOW_P (f))
16186 #if defined (USE_GTK) || defined (HAVE_NS)
16187 if (FRAME_EXTERNAL_TOOL_BAR (f))
16188 redisplay_tool_bar (f);
16189 #else
16190 if (WINDOWP (f->tool_bar_window)
16191 && (FRAME_TOOL_BAR_LINES (f) > 0
16192 || !NILP (Vauto_resize_tool_bars))
16193 && redisplay_tool_bar (f))
16194 ignore_mouse_drag_p = 1;
16195 #endif
16197 #endif
16200 #ifdef HAVE_WINDOW_SYSTEM
16201 if (FRAME_WINDOW_P (f)
16202 && update_window_fringes (w, (just_this_one_p
16203 || (!used_current_matrix_p && !overlay_arrow_seen)
16204 || w->pseudo_window_p)))
16206 update_begin (f);
16207 block_input ();
16208 if (draw_window_fringes (w, 1))
16209 x_draw_vertical_border (w);
16210 unblock_input ();
16211 update_end (f);
16213 #endif /* HAVE_WINDOW_SYSTEM */
16215 /* We go to this label, with fonts_changed set, if it is
16216 necessary to try again using larger glyph matrices.
16217 We have to redeem the scroll bar even in this case,
16218 because the loop in redisplay_internal expects that. */
16219 need_larger_matrices:
16221 finish_scroll_bars:
16223 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16225 /* Set the thumb's position and size. */
16226 set_vertical_scroll_bar (w);
16228 /* Note that we actually used the scroll bar attached to this
16229 window, so it shouldn't be deleted at the end of redisplay. */
16230 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16231 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16234 /* Restore current_buffer and value of point in it. The window
16235 update may have changed the buffer, so first make sure `opoint'
16236 is still valid (Bug#6177). */
16237 if (CHARPOS (opoint) < BEGV)
16238 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16239 else if (CHARPOS (opoint) > ZV)
16240 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16241 else
16242 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16244 set_buffer_internal_1 (old);
16245 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16246 shorter. This can be caused by log truncation in *Messages*. */
16247 if (CHARPOS (lpoint) <= ZV)
16248 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16250 unbind_to (count, Qnil);
16254 /* Build the complete desired matrix of WINDOW with a window start
16255 buffer position POS.
16257 Value is 1 if successful. It is zero if fonts were loaded during
16258 redisplay which makes re-adjusting glyph matrices necessary, and -1
16259 if point would appear in the scroll margins.
16260 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16261 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16262 set in FLAGS.) */
16265 try_window (Lisp_Object window, struct text_pos pos, int flags)
16267 struct window *w = XWINDOW (window);
16268 struct it it;
16269 struct glyph_row *last_text_row = NULL;
16270 struct frame *f = XFRAME (w->frame);
16271 int frame_line_height = default_line_pixel_height (w);
16273 /* Make POS the new window start. */
16274 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16276 /* Mark cursor position as unknown. No overlay arrow seen. */
16277 w->cursor.vpos = -1;
16278 overlay_arrow_seen = 0;
16280 /* Initialize iterator and info to start at POS. */
16281 start_display (&it, w, pos);
16283 /* Display all lines of W. */
16284 while (it.current_y < it.last_visible_y)
16286 if (display_line (&it))
16287 last_text_row = it.glyph_row - 1;
16288 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16289 return 0;
16292 /* Don't let the cursor end in the scroll margins. */
16293 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16294 && !MINI_WINDOW_P (w))
16296 int this_scroll_margin;
16297 int window_total_lines
16298 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16300 if (scroll_margin > 0)
16302 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16303 this_scroll_margin *= frame_line_height;
16305 else
16306 this_scroll_margin = 0;
16308 if ((w->cursor.y >= 0 /* not vscrolled */
16309 && w->cursor.y < this_scroll_margin
16310 && CHARPOS (pos) > BEGV
16311 && IT_CHARPOS (it) < ZV)
16312 /* rms: considering make_cursor_line_fully_visible_p here
16313 seems to give wrong results. We don't want to recenter
16314 when the last line is partly visible, we want to allow
16315 that case to be handled in the usual way. */
16316 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16318 w->cursor.vpos = -1;
16319 clear_glyph_matrix (w->desired_matrix);
16320 return -1;
16324 /* If bottom moved off end of frame, change mode line percentage. */
16325 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16326 w->update_mode_line = 1;
16328 /* Set window_end_pos to the offset of the last character displayed
16329 on the window from the end of current_buffer. Set
16330 window_end_vpos to its row number. */
16331 if (last_text_row)
16333 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16334 adjust_window_ends (w, last_text_row, 0);
16335 eassert
16336 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16337 w->window_end_vpos)));
16339 else
16341 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16342 w->window_end_pos = Z - ZV;
16343 w->window_end_vpos = 0;
16346 /* But that is not valid info until redisplay finishes. */
16347 w->window_end_valid = 0;
16348 return 1;
16353 /************************************************************************
16354 Window redisplay reusing current matrix when buffer has not changed
16355 ************************************************************************/
16357 /* Try redisplay of window W showing an unchanged buffer with a
16358 different window start than the last time it was displayed by
16359 reusing its current matrix. Value is non-zero if successful.
16360 W->start is the new window start. */
16362 static int
16363 try_window_reusing_current_matrix (struct window *w)
16365 struct frame *f = XFRAME (w->frame);
16366 struct glyph_row *bottom_row;
16367 struct it it;
16368 struct run run;
16369 struct text_pos start, new_start;
16370 int nrows_scrolled, i;
16371 struct glyph_row *last_text_row;
16372 struct glyph_row *last_reused_text_row;
16373 struct glyph_row *start_row;
16374 int start_vpos, min_y, max_y;
16376 #ifdef GLYPH_DEBUG
16377 if (inhibit_try_window_reusing)
16378 return 0;
16379 #endif
16381 if (/* This function doesn't handle terminal frames. */
16382 !FRAME_WINDOW_P (f)
16383 /* Don't try to reuse the display if windows have been split
16384 or such. */
16385 || windows_or_buffers_changed
16386 || f->cursor_type_changed)
16387 return 0;
16389 /* Can't do this if region may have changed. */
16390 if (markpos_of_region () >= 0
16391 || w->region_showing
16392 || !NILP (Vshow_trailing_whitespace))
16393 return 0;
16395 /* If top-line visibility has changed, give up. */
16396 if (WINDOW_WANTS_HEADER_LINE_P (w)
16397 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16398 return 0;
16400 /* Give up if old or new display is scrolled vertically. We could
16401 make this function handle this, but right now it doesn't. */
16402 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16403 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16404 return 0;
16406 /* The variable new_start now holds the new window start. The old
16407 start `start' can be determined from the current matrix. */
16408 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16409 start = start_row->minpos;
16410 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16412 /* Clear the desired matrix for the display below. */
16413 clear_glyph_matrix (w->desired_matrix);
16415 if (CHARPOS (new_start) <= CHARPOS (start))
16417 /* Don't use this method if the display starts with an ellipsis
16418 displayed for invisible text. It's not easy to handle that case
16419 below, and it's certainly not worth the effort since this is
16420 not a frequent case. */
16421 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16422 return 0;
16424 IF_DEBUG (debug_method_add (w, "twu1"));
16426 /* Display up to a row that can be reused. The variable
16427 last_text_row is set to the last row displayed that displays
16428 text. Note that it.vpos == 0 if or if not there is a
16429 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16430 start_display (&it, w, new_start);
16431 w->cursor.vpos = -1;
16432 last_text_row = last_reused_text_row = NULL;
16434 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16436 /* If we have reached into the characters in the START row,
16437 that means the line boundaries have changed. So we
16438 can't start copying with the row START. Maybe it will
16439 work to start copying with the following row. */
16440 while (IT_CHARPOS (it) > CHARPOS (start))
16442 /* Advance to the next row as the "start". */
16443 start_row++;
16444 start = start_row->minpos;
16445 /* If there are no more rows to try, or just one, give up. */
16446 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16447 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16448 || CHARPOS (start) == ZV)
16450 clear_glyph_matrix (w->desired_matrix);
16451 return 0;
16454 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16456 /* If we have reached alignment, we can copy the rest of the
16457 rows. */
16458 if (IT_CHARPOS (it) == CHARPOS (start)
16459 /* Don't accept "alignment" inside a display vector,
16460 since start_row could have started in the middle of
16461 that same display vector (thus their character
16462 positions match), and we have no way of telling if
16463 that is the case. */
16464 && it.current.dpvec_index < 0)
16465 break;
16467 if (display_line (&it))
16468 last_text_row = it.glyph_row - 1;
16472 /* A value of current_y < last_visible_y means that we stopped
16473 at the previous window start, which in turn means that we
16474 have at least one reusable row. */
16475 if (it.current_y < it.last_visible_y)
16477 struct glyph_row *row;
16479 /* IT.vpos always starts from 0; it counts text lines. */
16480 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16482 /* Find PT if not already found in the lines displayed. */
16483 if (w->cursor.vpos < 0)
16485 int dy = it.current_y - start_row->y;
16487 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16488 row = row_containing_pos (w, PT, row, NULL, dy);
16489 if (row)
16490 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16491 dy, nrows_scrolled);
16492 else
16494 clear_glyph_matrix (w->desired_matrix);
16495 return 0;
16499 /* Scroll the display. Do it before the current matrix is
16500 changed. The problem here is that update has not yet
16501 run, i.e. part of the current matrix is not up to date.
16502 scroll_run_hook will clear the cursor, and use the
16503 current matrix to get the height of the row the cursor is
16504 in. */
16505 run.current_y = start_row->y;
16506 run.desired_y = it.current_y;
16507 run.height = it.last_visible_y - it.current_y;
16509 if (run.height > 0 && run.current_y != run.desired_y)
16511 update_begin (f);
16512 FRAME_RIF (f)->update_window_begin_hook (w);
16513 FRAME_RIF (f)->clear_window_mouse_face (w);
16514 FRAME_RIF (f)->scroll_run_hook (w, &run);
16515 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16516 update_end (f);
16519 /* Shift current matrix down by nrows_scrolled lines. */
16520 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16521 rotate_matrix (w->current_matrix,
16522 start_vpos,
16523 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16524 nrows_scrolled);
16526 /* Disable lines that must be updated. */
16527 for (i = 0; i < nrows_scrolled; ++i)
16528 (start_row + i)->enabled_p = 0;
16530 /* Re-compute Y positions. */
16531 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16532 max_y = it.last_visible_y;
16533 for (row = start_row + nrows_scrolled;
16534 row < bottom_row;
16535 ++row)
16537 row->y = it.current_y;
16538 row->visible_height = row->height;
16540 if (row->y < min_y)
16541 row->visible_height -= min_y - row->y;
16542 if (row->y + row->height > max_y)
16543 row->visible_height -= row->y + row->height - max_y;
16544 if (row->fringe_bitmap_periodic_p)
16545 row->redraw_fringe_bitmaps_p = 1;
16547 it.current_y += row->height;
16549 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16550 last_reused_text_row = row;
16551 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16552 break;
16555 /* Disable lines in the current matrix which are now
16556 below the window. */
16557 for (++row; row < bottom_row; ++row)
16558 row->enabled_p = row->mode_line_p = 0;
16561 /* Update window_end_pos etc.; last_reused_text_row is the last
16562 reused row from the current matrix containing text, if any.
16563 The value of last_text_row is the last displayed line
16564 containing text. */
16565 if (last_reused_text_row)
16566 adjust_window_ends (w, last_reused_text_row, 1);
16567 else if (last_text_row)
16568 adjust_window_ends (w, last_text_row, 0);
16569 else
16571 /* This window must be completely empty. */
16572 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16573 w->window_end_pos = Z - ZV;
16574 w->window_end_vpos = 0;
16576 w->window_end_valid = 0;
16578 /* Update hint: don't try scrolling again in update_window. */
16579 w->desired_matrix->no_scrolling_p = 1;
16581 #ifdef GLYPH_DEBUG
16582 debug_method_add (w, "try_window_reusing_current_matrix 1");
16583 #endif
16584 return 1;
16586 else if (CHARPOS (new_start) > CHARPOS (start))
16588 struct glyph_row *pt_row, *row;
16589 struct glyph_row *first_reusable_row;
16590 struct glyph_row *first_row_to_display;
16591 int dy;
16592 int yb = window_text_bottom_y (w);
16594 /* Find the row starting at new_start, if there is one. Don't
16595 reuse a partially visible line at the end. */
16596 first_reusable_row = start_row;
16597 while (first_reusable_row->enabled_p
16598 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16599 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16600 < CHARPOS (new_start)))
16601 ++first_reusable_row;
16603 /* Give up if there is no row to reuse. */
16604 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16605 || !first_reusable_row->enabled_p
16606 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16607 != CHARPOS (new_start)))
16608 return 0;
16610 /* We can reuse fully visible rows beginning with
16611 first_reusable_row to the end of the window. Set
16612 first_row_to_display to the first row that cannot be reused.
16613 Set pt_row to the row containing point, if there is any. */
16614 pt_row = NULL;
16615 for (first_row_to_display = first_reusable_row;
16616 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16617 ++first_row_to_display)
16619 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16620 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16621 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16622 && first_row_to_display->ends_at_zv_p
16623 && pt_row == NULL)))
16624 pt_row = first_row_to_display;
16627 /* Start displaying at the start of first_row_to_display. */
16628 eassert (first_row_to_display->y < yb);
16629 init_to_row_start (&it, w, first_row_to_display);
16631 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16632 - start_vpos);
16633 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16634 - nrows_scrolled);
16635 it.current_y = (first_row_to_display->y - first_reusable_row->y
16636 + WINDOW_HEADER_LINE_HEIGHT (w));
16638 /* Display lines beginning with first_row_to_display in the
16639 desired matrix. Set last_text_row to the last row displayed
16640 that displays text. */
16641 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16642 if (pt_row == NULL)
16643 w->cursor.vpos = -1;
16644 last_text_row = NULL;
16645 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16646 if (display_line (&it))
16647 last_text_row = it.glyph_row - 1;
16649 /* If point is in a reused row, adjust y and vpos of the cursor
16650 position. */
16651 if (pt_row)
16653 w->cursor.vpos -= nrows_scrolled;
16654 w->cursor.y -= first_reusable_row->y - start_row->y;
16657 /* Give up if point isn't in a row displayed or reused. (This
16658 also handles the case where w->cursor.vpos < nrows_scrolled
16659 after the calls to display_line, which can happen with scroll
16660 margins. See bug#1295.) */
16661 if (w->cursor.vpos < 0)
16663 clear_glyph_matrix (w->desired_matrix);
16664 return 0;
16667 /* Scroll the display. */
16668 run.current_y = first_reusable_row->y;
16669 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16670 run.height = it.last_visible_y - run.current_y;
16671 dy = run.current_y - run.desired_y;
16673 if (run.height)
16675 update_begin (f);
16676 FRAME_RIF (f)->update_window_begin_hook (w);
16677 FRAME_RIF (f)->clear_window_mouse_face (w);
16678 FRAME_RIF (f)->scroll_run_hook (w, &run);
16679 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16680 update_end (f);
16683 /* Adjust Y positions of reused rows. */
16684 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16685 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16686 max_y = it.last_visible_y;
16687 for (row = first_reusable_row; row < first_row_to_display; ++row)
16689 row->y -= dy;
16690 row->visible_height = row->height;
16691 if (row->y < min_y)
16692 row->visible_height -= min_y - row->y;
16693 if (row->y + row->height > max_y)
16694 row->visible_height -= row->y + row->height - max_y;
16695 if (row->fringe_bitmap_periodic_p)
16696 row->redraw_fringe_bitmaps_p = 1;
16699 /* Scroll the current matrix. */
16700 eassert (nrows_scrolled > 0);
16701 rotate_matrix (w->current_matrix,
16702 start_vpos,
16703 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16704 -nrows_scrolled);
16706 /* Disable rows not reused. */
16707 for (row -= nrows_scrolled; row < bottom_row; ++row)
16708 row->enabled_p = 0;
16710 /* Point may have moved to a different line, so we cannot assume that
16711 the previous cursor position is valid; locate the correct row. */
16712 if (pt_row)
16714 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16715 row < bottom_row
16716 && PT >= MATRIX_ROW_END_CHARPOS (row)
16717 && !row->ends_at_zv_p;
16718 row++)
16720 w->cursor.vpos++;
16721 w->cursor.y = row->y;
16723 if (row < bottom_row)
16725 /* Can't simply scan the row for point with
16726 bidi-reordered glyph rows. Let set_cursor_from_row
16727 figure out where to put the cursor, and if it fails,
16728 give up. */
16729 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16731 if (!set_cursor_from_row (w, row, w->current_matrix,
16732 0, 0, 0, 0))
16734 clear_glyph_matrix (w->desired_matrix);
16735 return 0;
16738 else
16740 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16741 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16743 for (; glyph < end
16744 && (!BUFFERP (glyph->object)
16745 || glyph->charpos < PT);
16746 glyph++)
16748 w->cursor.hpos++;
16749 w->cursor.x += glyph->pixel_width;
16755 /* Adjust window end. A null value of last_text_row means that
16756 the window end is in reused rows which in turn means that
16757 only its vpos can have changed. */
16758 if (last_text_row)
16759 adjust_window_ends (w, last_text_row, 0);
16760 else
16761 w->window_end_vpos -= nrows_scrolled;
16763 w->window_end_valid = 0;
16764 w->desired_matrix->no_scrolling_p = 1;
16766 #ifdef GLYPH_DEBUG
16767 debug_method_add (w, "try_window_reusing_current_matrix 2");
16768 #endif
16769 return 1;
16772 return 0;
16777 /************************************************************************
16778 Window redisplay reusing current matrix when buffer has changed
16779 ************************************************************************/
16781 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16782 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16783 ptrdiff_t *, ptrdiff_t *);
16784 static struct glyph_row *
16785 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16786 struct glyph_row *);
16789 /* Return the last row in MATRIX displaying text. If row START is
16790 non-null, start searching with that row. IT gives the dimensions
16791 of the display. Value is null if matrix is empty; otherwise it is
16792 a pointer to the row found. */
16794 static struct glyph_row *
16795 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16796 struct glyph_row *start)
16798 struct glyph_row *row, *row_found;
16800 /* Set row_found to the last row in IT->w's current matrix
16801 displaying text. The loop looks funny but think of partially
16802 visible lines. */
16803 row_found = NULL;
16804 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16805 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16807 eassert (row->enabled_p);
16808 row_found = row;
16809 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16810 break;
16811 ++row;
16814 return row_found;
16818 /* Return the last row in the current matrix of W that is not affected
16819 by changes at the start of current_buffer that occurred since W's
16820 current matrix was built. Value is null if no such row exists.
16822 BEG_UNCHANGED us the number of characters unchanged at the start of
16823 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16824 first changed character in current_buffer. Characters at positions <
16825 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16826 when the current matrix was built. */
16828 static struct glyph_row *
16829 find_last_unchanged_at_beg_row (struct window *w)
16831 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16832 struct glyph_row *row;
16833 struct glyph_row *row_found = NULL;
16834 int yb = window_text_bottom_y (w);
16836 /* Find the last row displaying unchanged text. */
16837 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16838 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16839 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16840 ++row)
16842 if (/* If row ends before first_changed_pos, it is unchanged,
16843 except in some case. */
16844 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16845 /* When row ends in ZV and we write at ZV it is not
16846 unchanged. */
16847 && !row->ends_at_zv_p
16848 /* When first_changed_pos is the end of a continued line,
16849 row is not unchanged because it may be no longer
16850 continued. */
16851 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16852 && (row->continued_p
16853 || row->exact_window_width_line_p))
16854 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16855 needs to be recomputed, so don't consider this row as
16856 unchanged. This happens when the last line was
16857 bidi-reordered and was killed immediately before this
16858 redisplay cycle. In that case, ROW->end stores the
16859 buffer position of the first visual-order character of
16860 the killed text, which is now beyond ZV. */
16861 && CHARPOS (row->end.pos) <= ZV)
16862 row_found = row;
16864 /* Stop if last visible row. */
16865 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16866 break;
16869 return row_found;
16873 /* Find the first glyph row in the current matrix of W that is not
16874 affected by changes at the end of current_buffer since the
16875 time W's current matrix was built.
16877 Return in *DELTA the number of chars by which buffer positions in
16878 unchanged text at the end of current_buffer must be adjusted.
16880 Return in *DELTA_BYTES the corresponding number of bytes.
16882 Value is null if no such row exists, i.e. all rows are affected by
16883 changes. */
16885 static struct glyph_row *
16886 find_first_unchanged_at_end_row (struct window *w,
16887 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16889 struct glyph_row *row;
16890 struct glyph_row *row_found = NULL;
16892 *delta = *delta_bytes = 0;
16894 /* Display must not have been paused, otherwise the current matrix
16895 is not up to date. */
16896 eassert (w->window_end_valid);
16898 /* A value of window_end_pos >= END_UNCHANGED means that the window
16899 end is in the range of changed text. If so, there is no
16900 unchanged row at the end of W's current matrix. */
16901 if (w->window_end_pos >= END_UNCHANGED)
16902 return NULL;
16904 /* Set row to the last row in W's current matrix displaying text. */
16905 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16907 /* If matrix is entirely empty, no unchanged row exists. */
16908 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16910 /* The value of row is the last glyph row in the matrix having a
16911 meaningful buffer position in it. The end position of row
16912 corresponds to window_end_pos. This allows us to translate
16913 buffer positions in the current matrix to current buffer
16914 positions for characters not in changed text. */
16915 ptrdiff_t Z_old =
16916 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16917 ptrdiff_t Z_BYTE_old =
16918 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16919 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16920 struct glyph_row *first_text_row
16921 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16923 *delta = Z - Z_old;
16924 *delta_bytes = Z_BYTE - Z_BYTE_old;
16926 /* Set last_unchanged_pos to the buffer position of the last
16927 character in the buffer that has not been changed. Z is the
16928 index + 1 of the last character in current_buffer, i.e. by
16929 subtracting END_UNCHANGED we get the index of the last
16930 unchanged character, and we have to add BEG to get its buffer
16931 position. */
16932 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16933 last_unchanged_pos_old = last_unchanged_pos - *delta;
16935 /* Search backward from ROW for a row displaying a line that
16936 starts at a minimum position >= last_unchanged_pos_old. */
16937 for (; row > first_text_row; --row)
16939 /* This used to abort, but it can happen.
16940 It is ok to just stop the search instead here. KFS. */
16941 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16942 break;
16944 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16945 row_found = row;
16949 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16951 return row_found;
16955 /* Make sure that glyph rows in the current matrix of window W
16956 reference the same glyph memory as corresponding rows in the
16957 frame's frame matrix. This function is called after scrolling W's
16958 current matrix on a terminal frame in try_window_id and
16959 try_window_reusing_current_matrix. */
16961 static void
16962 sync_frame_with_window_matrix_rows (struct window *w)
16964 struct frame *f = XFRAME (w->frame);
16965 struct glyph_row *window_row, *window_row_end, *frame_row;
16967 /* Preconditions: W must be a leaf window and full-width. Its frame
16968 must have a frame matrix. */
16969 eassert (BUFFERP (w->contents));
16970 eassert (WINDOW_FULL_WIDTH_P (w));
16971 eassert (!FRAME_WINDOW_P (f));
16973 /* If W is a full-width window, glyph pointers in W's current matrix
16974 have, by definition, to be the same as glyph pointers in the
16975 corresponding frame matrix. Note that frame matrices have no
16976 marginal areas (see build_frame_matrix). */
16977 window_row = w->current_matrix->rows;
16978 window_row_end = window_row + w->current_matrix->nrows;
16979 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16980 while (window_row < window_row_end)
16982 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16983 struct glyph *end = window_row->glyphs[LAST_AREA];
16985 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16986 frame_row->glyphs[TEXT_AREA] = start;
16987 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16988 frame_row->glyphs[LAST_AREA] = end;
16990 /* Disable frame rows whose corresponding window rows have
16991 been disabled in try_window_id. */
16992 if (!window_row->enabled_p)
16993 frame_row->enabled_p = 0;
16995 ++window_row, ++frame_row;
17000 /* Find the glyph row in window W containing CHARPOS. Consider all
17001 rows between START and END (not inclusive). END null means search
17002 all rows to the end of the display area of W. Value is the row
17003 containing CHARPOS or null. */
17005 struct glyph_row *
17006 row_containing_pos (struct window *w, ptrdiff_t charpos,
17007 struct glyph_row *start, struct glyph_row *end, int dy)
17009 struct glyph_row *row = start;
17010 struct glyph_row *best_row = NULL;
17011 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17012 int last_y;
17014 /* If we happen to start on a header-line, skip that. */
17015 if (row->mode_line_p)
17016 ++row;
17018 if ((end && row >= end) || !row->enabled_p)
17019 return NULL;
17021 last_y = window_text_bottom_y (w) - dy;
17023 while (1)
17025 /* Give up if we have gone too far. */
17026 if (end && row >= end)
17027 return NULL;
17028 /* This formerly returned if they were equal.
17029 I think that both quantities are of a "last plus one" type;
17030 if so, when they are equal, the row is within the screen. -- rms. */
17031 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17032 return NULL;
17034 /* If it is in this row, return this row. */
17035 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17036 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17037 /* The end position of a row equals the start
17038 position of the next row. If CHARPOS is there, we
17039 would rather consider it displayed in the next
17040 line, except when this line ends in ZV. */
17041 && !row_for_charpos_p (row, charpos)))
17042 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17044 struct glyph *g;
17046 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17047 || (!best_row && !row->continued_p))
17048 return row;
17049 /* In bidi-reordered rows, there could be several rows whose
17050 edges surround CHARPOS, all of these rows belonging to
17051 the same continued line. We need to find the row which
17052 fits CHARPOS the best. */
17053 for (g = row->glyphs[TEXT_AREA];
17054 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17055 g++)
17057 if (!STRINGP (g->object))
17059 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17061 mindif = eabs (g->charpos - charpos);
17062 best_row = row;
17063 /* Exact match always wins. */
17064 if (mindif == 0)
17065 return best_row;
17070 else if (best_row && !row->continued_p)
17071 return best_row;
17072 ++row;
17077 /* Try to redisplay window W by reusing its existing display. W's
17078 current matrix must be up to date when this function is called,
17079 i.e. window_end_valid must be nonzero.
17081 Value is
17083 1 if display has been updated
17084 0 if otherwise unsuccessful
17085 -1 if redisplay with same window start is known not to succeed
17087 The following steps are performed:
17089 1. Find the last row in the current matrix of W that is not
17090 affected by changes at the start of current_buffer. If no such row
17091 is found, give up.
17093 2. Find the first row in W's current matrix that is not affected by
17094 changes at the end of current_buffer. Maybe there is no such row.
17096 3. Display lines beginning with the row + 1 found in step 1 to the
17097 row found in step 2 or, if step 2 didn't find a row, to the end of
17098 the window.
17100 4. If cursor is not known to appear on the window, give up.
17102 5. If display stopped at the row found in step 2, scroll the
17103 display and current matrix as needed.
17105 6. Maybe display some lines at the end of W, if we must. This can
17106 happen under various circumstances, like a partially visible line
17107 becoming fully visible, or because newly displayed lines are displayed
17108 in smaller font sizes.
17110 7. Update W's window end information. */
17112 static int
17113 try_window_id (struct window *w)
17115 struct frame *f = XFRAME (w->frame);
17116 struct glyph_matrix *current_matrix = w->current_matrix;
17117 struct glyph_matrix *desired_matrix = w->desired_matrix;
17118 struct glyph_row *last_unchanged_at_beg_row;
17119 struct glyph_row *first_unchanged_at_end_row;
17120 struct glyph_row *row;
17121 struct glyph_row *bottom_row;
17122 int bottom_vpos;
17123 struct it it;
17124 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17125 int dvpos, dy;
17126 struct text_pos start_pos;
17127 struct run run;
17128 int first_unchanged_at_end_vpos = 0;
17129 struct glyph_row *last_text_row, *last_text_row_at_end;
17130 struct text_pos start;
17131 ptrdiff_t first_changed_charpos, last_changed_charpos;
17133 #ifdef GLYPH_DEBUG
17134 if (inhibit_try_window_id)
17135 return 0;
17136 #endif
17138 /* This is handy for debugging. */
17139 #if 0
17140 #define GIVE_UP(X) \
17141 do { \
17142 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17143 return 0; \
17144 } while (0)
17145 #else
17146 #define GIVE_UP(X) return 0
17147 #endif
17149 SET_TEXT_POS_FROM_MARKER (start, w->start);
17151 /* Don't use this for mini-windows because these can show
17152 messages and mini-buffers, and we don't handle that here. */
17153 if (MINI_WINDOW_P (w))
17154 GIVE_UP (1);
17156 /* This flag is used to prevent redisplay optimizations. */
17157 if (windows_or_buffers_changed || f->cursor_type_changed)
17158 GIVE_UP (2);
17160 /* Verify that narrowing has not changed.
17161 Also verify that we were not told to prevent redisplay optimizations.
17162 It would be nice to further
17163 reduce the number of cases where this prevents try_window_id. */
17164 if (current_buffer->clip_changed
17165 || current_buffer->prevent_redisplay_optimizations_p)
17166 GIVE_UP (3);
17168 /* Window must either use window-based redisplay or be full width. */
17169 if (!FRAME_WINDOW_P (f)
17170 && (!FRAME_LINE_INS_DEL_OK (f)
17171 || !WINDOW_FULL_WIDTH_P (w)))
17172 GIVE_UP (4);
17174 /* Give up if point is known NOT to appear in W. */
17175 if (PT < CHARPOS (start))
17176 GIVE_UP (5);
17178 /* Another way to prevent redisplay optimizations. */
17179 if (w->last_modified == 0)
17180 GIVE_UP (6);
17182 /* Verify that window is not hscrolled. */
17183 if (w->hscroll != 0)
17184 GIVE_UP (7);
17186 /* Verify that display wasn't paused. */
17187 if (!w->window_end_valid)
17188 GIVE_UP (8);
17190 /* Can't use this if highlighting a region because a cursor movement
17191 will do more than just set the cursor. */
17192 if (markpos_of_region () >= 0)
17193 GIVE_UP (9);
17195 /* Likewise if highlighting trailing whitespace. */
17196 if (!NILP (Vshow_trailing_whitespace))
17197 GIVE_UP (11);
17199 /* Likewise if showing a region. */
17200 if (w->region_showing)
17201 GIVE_UP (10);
17203 /* Can't use this if overlay arrow position and/or string have
17204 changed. */
17205 if (overlay_arrows_changed_p ())
17206 GIVE_UP (12);
17208 /* When word-wrap is on, adding a space to the first word of a
17209 wrapped line can change the wrap position, altering the line
17210 above it. It might be worthwhile to handle this more
17211 intelligently, but for now just redisplay from scratch. */
17212 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17213 GIVE_UP (21);
17215 /* Under bidi reordering, adding or deleting a character in the
17216 beginning of a paragraph, before the first strong directional
17217 character, can change the base direction of the paragraph (unless
17218 the buffer specifies a fixed paragraph direction), which will
17219 require to redisplay the whole paragraph. It might be worthwhile
17220 to find the paragraph limits and widen the range of redisplayed
17221 lines to that, but for now just give up this optimization and
17222 redisplay from scratch. */
17223 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17224 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17225 GIVE_UP (22);
17227 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17228 only if buffer has really changed. The reason is that the gap is
17229 initially at Z for freshly visited files. The code below would
17230 set end_unchanged to 0 in that case. */
17231 if (MODIFF > SAVE_MODIFF
17232 /* This seems to happen sometimes after saving a buffer. */
17233 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17235 if (GPT - BEG < BEG_UNCHANGED)
17236 BEG_UNCHANGED = GPT - BEG;
17237 if (Z - GPT < END_UNCHANGED)
17238 END_UNCHANGED = Z - GPT;
17241 /* The position of the first and last character that has been changed. */
17242 first_changed_charpos = BEG + BEG_UNCHANGED;
17243 last_changed_charpos = Z - END_UNCHANGED;
17245 /* If window starts after a line end, and the last change is in
17246 front of that newline, then changes don't affect the display.
17247 This case happens with stealth-fontification. Note that although
17248 the display is unchanged, glyph positions in the matrix have to
17249 be adjusted, of course. */
17250 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17251 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17252 && ((last_changed_charpos < CHARPOS (start)
17253 && CHARPOS (start) == BEGV)
17254 || (last_changed_charpos < CHARPOS (start) - 1
17255 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17257 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17258 struct glyph_row *r0;
17260 /* Compute how many chars/bytes have been added to or removed
17261 from the buffer. */
17262 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17263 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17264 Z_delta = Z - Z_old;
17265 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17267 /* Give up if PT is not in the window. Note that it already has
17268 been checked at the start of try_window_id that PT is not in
17269 front of the window start. */
17270 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17271 GIVE_UP (13);
17273 /* If window start is unchanged, we can reuse the whole matrix
17274 as is, after adjusting glyph positions. No need to compute
17275 the window end again, since its offset from Z hasn't changed. */
17276 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17277 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17278 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17279 /* PT must not be in a partially visible line. */
17280 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17281 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17283 /* Adjust positions in the glyph matrix. */
17284 if (Z_delta || Z_delta_bytes)
17286 struct glyph_row *r1
17287 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17288 increment_matrix_positions (w->current_matrix,
17289 MATRIX_ROW_VPOS (r0, current_matrix),
17290 MATRIX_ROW_VPOS (r1, current_matrix),
17291 Z_delta, Z_delta_bytes);
17294 /* Set the cursor. */
17295 row = row_containing_pos (w, PT, r0, NULL, 0);
17296 if (row)
17297 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17298 else
17299 emacs_abort ();
17300 return 1;
17304 /* Handle the case that changes are all below what is displayed in
17305 the window, and that PT is in the window. This shortcut cannot
17306 be taken if ZV is visible in the window, and text has been added
17307 there that is visible in the window. */
17308 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17309 /* ZV is not visible in the window, or there are no
17310 changes at ZV, actually. */
17311 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17312 || first_changed_charpos == last_changed_charpos))
17314 struct glyph_row *r0;
17316 /* Give up if PT is not in the window. Note that it already has
17317 been checked at the start of try_window_id that PT is not in
17318 front of the window start. */
17319 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17320 GIVE_UP (14);
17322 /* If window start is unchanged, we can reuse the whole matrix
17323 as is, without changing glyph positions since no text has
17324 been added/removed in front of the window end. */
17325 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17326 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17327 /* PT must not be in a partially visible line. */
17328 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17329 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17331 /* We have to compute the window end anew since text
17332 could have been added/removed after it. */
17333 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17334 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17336 /* Set the cursor. */
17337 row = row_containing_pos (w, PT, r0, NULL, 0);
17338 if (row)
17339 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17340 else
17341 emacs_abort ();
17342 return 2;
17346 /* Give up if window start is in the changed area.
17348 The condition used to read
17350 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17352 but why that was tested escapes me at the moment. */
17353 if (CHARPOS (start) >= first_changed_charpos
17354 && CHARPOS (start) <= last_changed_charpos)
17355 GIVE_UP (15);
17357 /* Check that window start agrees with the start of the first glyph
17358 row in its current matrix. Check this after we know the window
17359 start is not in changed text, otherwise positions would not be
17360 comparable. */
17361 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17362 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17363 GIVE_UP (16);
17365 /* Give up if the window ends in strings. Overlay strings
17366 at the end are difficult to handle, so don't try. */
17367 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17368 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17369 GIVE_UP (20);
17371 /* Compute the position at which we have to start displaying new
17372 lines. Some of the lines at the top of the window might be
17373 reusable because they are not displaying changed text. Find the
17374 last row in W's current matrix not affected by changes at the
17375 start of current_buffer. Value is null if changes start in the
17376 first line of window. */
17377 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17378 if (last_unchanged_at_beg_row)
17380 /* Avoid starting to display in the middle of a character, a TAB
17381 for instance. This is easier than to set up the iterator
17382 exactly, and it's not a frequent case, so the additional
17383 effort wouldn't really pay off. */
17384 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17385 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17386 && last_unchanged_at_beg_row > w->current_matrix->rows)
17387 --last_unchanged_at_beg_row;
17389 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17390 GIVE_UP (17);
17392 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17393 GIVE_UP (18);
17394 start_pos = it.current.pos;
17396 /* Start displaying new lines in the desired matrix at the same
17397 vpos we would use in the current matrix, i.e. below
17398 last_unchanged_at_beg_row. */
17399 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17400 current_matrix);
17401 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17402 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17404 eassert (it.hpos == 0 && it.current_x == 0);
17406 else
17408 /* There are no reusable lines at the start of the window.
17409 Start displaying in the first text line. */
17410 start_display (&it, w, start);
17411 it.vpos = it.first_vpos;
17412 start_pos = it.current.pos;
17415 /* Find the first row that is not affected by changes at the end of
17416 the buffer. Value will be null if there is no unchanged row, in
17417 which case we must redisplay to the end of the window. delta
17418 will be set to the value by which buffer positions beginning with
17419 first_unchanged_at_end_row have to be adjusted due to text
17420 changes. */
17421 first_unchanged_at_end_row
17422 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17423 IF_DEBUG (debug_delta = delta);
17424 IF_DEBUG (debug_delta_bytes = delta_bytes);
17426 /* Set stop_pos to the buffer position up to which we will have to
17427 display new lines. If first_unchanged_at_end_row != NULL, this
17428 is the buffer position of the start of the line displayed in that
17429 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17430 that we don't stop at a buffer position. */
17431 stop_pos = 0;
17432 if (first_unchanged_at_end_row)
17434 eassert (last_unchanged_at_beg_row == NULL
17435 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17437 /* If this is a continuation line, move forward to the next one
17438 that isn't. Changes in lines above affect this line.
17439 Caution: this may move first_unchanged_at_end_row to a row
17440 not displaying text. */
17441 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17442 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17443 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17444 < it.last_visible_y))
17445 ++first_unchanged_at_end_row;
17447 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17448 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17449 >= it.last_visible_y))
17450 first_unchanged_at_end_row = NULL;
17451 else
17453 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17454 + delta);
17455 first_unchanged_at_end_vpos
17456 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17457 eassert (stop_pos >= Z - END_UNCHANGED);
17460 else if (last_unchanged_at_beg_row == NULL)
17461 GIVE_UP (19);
17464 #ifdef GLYPH_DEBUG
17466 /* Either there is no unchanged row at the end, or the one we have
17467 now displays text. This is a necessary condition for the window
17468 end pos calculation at the end of this function. */
17469 eassert (first_unchanged_at_end_row == NULL
17470 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17472 debug_last_unchanged_at_beg_vpos
17473 = (last_unchanged_at_beg_row
17474 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17475 : -1);
17476 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17478 #endif /* GLYPH_DEBUG */
17481 /* Display new lines. Set last_text_row to the last new line
17482 displayed which has text on it, i.e. might end up as being the
17483 line where the window_end_vpos is. */
17484 w->cursor.vpos = -1;
17485 last_text_row = NULL;
17486 overlay_arrow_seen = 0;
17487 while (it.current_y < it.last_visible_y
17488 && !f->fonts_changed
17489 && (first_unchanged_at_end_row == NULL
17490 || IT_CHARPOS (it) < stop_pos))
17492 if (display_line (&it))
17493 last_text_row = it.glyph_row - 1;
17496 if (f->fonts_changed)
17497 return -1;
17500 /* Compute differences in buffer positions, y-positions etc. for
17501 lines reused at the bottom of the window. Compute what we can
17502 scroll. */
17503 if (first_unchanged_at_end_row
17504 /* No lines reused because we displayed everything up to the
17505 bottom of the window. */
17506 && it.current_y < it.last_visible_y)
17508 dvpos = (it.vpos
17509 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17510 current_matrix));
17511 dy = it.current_y - first_unchanged_at_end_row->y;
17512 run.current_y = first_unchanged_at_end_row->y;
17513 run.desired_y = run.current_y + dy;
17514 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17516 else
17518 delta = delta_bytes = dvpos = dy
17519 = run.current_y = run.desired_y = run.height = 0;
17520 first_unchanged_at_end_row = NULL;
17522 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17525 /* Find the cursor if not already found. We have to decide whether
17526 PT will appear on this window (it sometimes doesn't, but this is
17527 not a very frequent case.) This decision has to be made before
17528 the current matrix is altered. A value of cursor.vpos < 0 means
17529 that PT is either in one of the lines beginning at
17530 first_unchanged_at_end_row or below the window. Don't care for
17531 lines that might be displayed later at the window end; as
17532 mentioned, this is not a frequent case. */
17533 if (w->cursor.vpos < 0)
17535 /* Cursor in unchanged rows at the top? */
17536 if (PT < CHARPOS (start_pos)
17537 && last_unchanged_at_beg_row)
17539 row = row_containing_pos (w, PT,
17540 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17541 last_unchanged_at_beg_row + 1, 0);
17542 if (row)
17543 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17546 /* Start from first_unchanged_at_end_row looking for PT. */
17547 else if (first_unchanged_at_end_row)
17549 row = row_containing_pos (w, PT - delta,
17550 first_unchanged_at_end_row, NULL, 0);
17551 if (row)
17552 set_cursor_from_row (w, row, w->current_matrix, delta,
17553 delta_bytes, dy, dvpos);
17556 /* Give up if cursor was not found. */
17557 if (w->cursor.vpos < 0)
17559 clear_glyph_matrix (w->desired_matrix);
17560 return -1;
17564 /* Don't let the cursor end in the scroll margins. */
17566 int this_scroll_margin, cursor_height;
17567 int frame_line_height = default_line_pixel_height (w);
17568 int window_total_lines
17569 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17571 this_scroll_margin =
17572 max (0, min (scroll_margin, window_total_lines / 4));
17573 this_scroll_margin *= frame_line_height;
17574 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17576 if ((w->cursor.y < this_scroll_margin
17577 && CHARPOS (start) > BEGV)
17578 /* Old redisplay didn't take scroll margin into account at the bottom,
17579 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17580 || (w->cursor.y + (make_cursor_line_fully_visible_p
17581 ? cursor_height + this_scroll_margin
17582 : 1)) > it.last_visible_y)
17584 w->cursor.vpos = -1;
17585 clear_glyph_matrix (w->desired_matrix);
17586 return -1;
17590 /* Scroll the display. Do it before changing the current matrix so
17591 that xterm.c doesn't get confused about where the cursor glyph is
17592 found. */
17593 if (dy && run.height)
17595 update_begin (f);
17597 if (FRAME_WINDOW_P (f))
17599 FRAME_RIF (f)->update_window_begin_hook (w);
17600 FRAME_RIF (f)->clear_window_mouse_face (w);
17601 FRAME_RIF (f)->scroll_run_hook (w, &run);
17602 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17604 else
17606 /* Terminal frame. In this case, dvpos gives the number of
17607 lines to scroll by; dvpos < 0 means scroll up. */
17608 int from_vpos
17609 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17610 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17611 int end = (WINDOW_TOP_EDGE_LINE (w)
17612 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17613 + window_internal_height (w));
17615 #if defined (HAVE_GPM) || defined (MSDOS)
17616 x_clear_window_mouse_face (w);
17617 #endif
17618 /* Perform the operation on the screen. */
17619 if (dvpos > 0)
17621 /* Scroll last_unchanged_at_beg_row to the end of the
17622 window down dvpos lines. */
17623 set_terminal_window (f, end);
17625 /* On dumb terminals delete dvpos lines at the end
17626 before inserting dvpos empty lines. */
17627 if (!FRAME_SCROLL_REGION_OK (f))
17628 ins_del_lines (f, end - dvpos, -dvpos);
17630 /* Insert dvpos empty lines in front of
17631 last_unchanged_at_beg_row. */
17632 ins_del_lines (f, from, dvpos);
17634 else if (dvpos < 0)
17636 /* Scroll up last_unchanged_at_beg_vpos to the end of
17637 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17638 set_terminal_window (f, end);
17640 /* Delete dvpos lines in front of
17641 last_unchanged_at_beg_vpos. ins_del_lines will set
17642 the cursor to the given vpos and emit |dvpos| delete
17643 line sequences. */
17644 ins_del_lines (f, from + dvpos, dvpos);
17646 /* On a dumb terminal insert dvpos empty lines at the
17647 end. */
17648 if (!FRAME_SCROLL_REGION_OK (f))
17649 ins_del_lines (f, end + dvpos, -dvpos);
17652 set_terminal_window (f, 0);
17655 update_end (f);
17658 /* Shift reused rows of the current matrix to the right position.
17659 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17660 text. */
17661 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17662 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17663 if (dvpos < 0)
17665 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17666 bottom_vpos, dvpos);
17667 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17668 bottom_vpos);
17670 else if (dvpos > 0)
17672 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17673 bottom_vpos, dvpos);
17674 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17675 first_unchanged_at_end_vpos + dvpos);
17678 /* For frame-based redisplay, make sure that current frame and window
17679 matrix are in sync with respect to glyph memory. */
17680 if (!FRAME_WINDOW_P (f))
17681 sync_frame_with_window_matrix_rows (w);
17683 /* Adjust buffer positions in reused rows. */
17684 if (delta || delta_bytes)
17685 increment_matrix_positions (current_matrix,
17686 first_unchanged_at_end_vpos + dvpos,
17687 bottom_vpos, delta, delta_bytes);
17689 /* Adjust Y positions. */
17690 if (dy)
17691 shift_glyph_matrix (w, current_matrix,
17692 first_unchanged_at_end_vpos + dvpos,
17693 bottom_vpos, dy);
17695 if (first_unchanged_at_end_row)
17697 first_unchanged_at_end_row += dvpos;
17698 if (first_unchanged_at_end_row->y >= it.last_visible_y
17699 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17700 first_unchanged_at_end_row = NULL;
17703 /* If scrolling up, there may be some lines to display at the end of
17704 the window. */
17705 last_text_row_at_end = NULL;
17706 if (dy < 0)
17708 /* Scrolling up can leave for example a partially visible line
17709 at the end of the window to be redisplayed. */
17710 /* Set last_row to the glyph row in the current matrix where the
17711 window end line is found. It has been moved up or down in
17712 the matrix by dvpos. */
17713 int last_vpos = w->window_end_vpos + dvpos;
17714 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17716 /* If last_row is the window end line, it should display text. */
17717 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17719 /* If window end line was partially visible before, begin
17720 displaying at that line. Otherwise begin displaying with the
17721 line following it. */
17722 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17724 init_to_row_start (&it, w, last_row);
17725 it.vpos = last_vpos;
17726 it.current_y = last_row->y;
17728 else
17730 init_to_row_end (&it, w, last_row);
17731 it.vpos = 1 + last_vpos;
17732 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17733 ++last_row;
17736 /* We may start in a continuation line. If so, we have to
17737 get the right continuation_lines_width and current_x. */
17738 it.continuation_lines_width = last_row->continuation_lines_width;
17739 it.hpos = it.current_x = 0;
17741 /* Display the rest of the lines at the window end. */
17742 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17743 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17745 /* Is it always sure that the display agrees with lines in
17746 the current matrix? I don't think so, so we mark rows
17747 displayed invalid in the current matrix by setting their
17748 enabled_p flag to zero. */
17749 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17750 if (display_line (&it))
17751 last_text_row_at_end = it.glyph_row - 1;
17755 /* Update window_end_pos and window_end_vpos. */
17756 if (first_unchanged_at_end_row && !last_text_row_at_end)
17758 /* Window end line if one of the preserved rows from the current
17759 matrix. Set row to the last row displaying text in current
17760 matrix starting at first_unchanged_at_end_row, after
17761 scrolling. */
17762 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17763 row = find_last_row_displaying_text (w->current_matrix, &it,
17764 first_unchanged_at_end_row);
17765 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17766 adjust_window_ends (w, row, 1);
17767 eassert (w->window_end_bytepos >= 0);
17768 IF_DEBUG (debug_method_add (w, "A"));
17770 else if (last_text_row_at_end)
17772 adjust_window_ends (w, last_text_row_at_end, 0);
17773 eassert (w->window_end_bytepos >= 0);
17774 IF_DEBUG (debug_method_add (w, "B"));
17776 else if (last_text_row)
17778 /* We have displayed either to the end of the window or at the
17779 end of the window, i.e. the last row with text is to be found
17780 in the desired matrix. */
17781 adjust_window_ends (w, last_text_row, 0);
17782 eassert (w->window_end_bytepos >= 0);
17784 else if (first_unchanged_at_end_row == NULL
17785 && last_text_row == NULL
17786 && last_text_row_at_end == NULL)
17788 /* Displayed to end of window, but no line containing text was
17789 displayed. Lines were deleted at the end of the window. */
17790 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17791 int vpos = w->window_end_vpos;
17792 struct glyph_row *current_row = current_matrix->rows + vpos;
17793 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17795 for (row = NULL;
17796 row == NULL && vpos >= first_vpos;
17797 --vpos, --current_row, --desired_row)
17799 if (desired_row->enabled_p)
17801 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17802 row = desired_row;
17804 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17805 row = current_row;
17808 eassert (row != NULL);
17809 w->window_end_vpos = vpos + 1;
17810 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17811 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17812 eassert (w->window_end_bytepos >= 0);
17813 IF_DEBUG (debug_method_add (w, "C"));
17815 else
17816 emacs_abort ();
17818 IF_DEBUG (debug_end_pos = w->window_end_pos;
17819 debug_end_vpos = w->window_end_vpos);
17821 /* Record that display has not been completed. */
17822 w->window_end_valid = 0;
17823 w->desired_matrix->no_scrolling_p = 1;
17824 return 3;
17826 #undef GIVE_UP
17831 /***********************************************************************
17832 More debugging support
17833 ***********************************************************************/
17835 #ifdef GLYPH_DEBUG
17837 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17838 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17839 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17842 /* Dump the contents of glyph matrix MATRIX on stderr.
17844 GLYPHS 0 means don't show glyph contents.
17845 GLYPHS 1 means show glyphs in short form
17846 GLYPHS > 1 means show glyphs in long form. */
17848 void
17849 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17851 int i;
17852 for (i = 0; i < matrix->nrows; ++i)
17853 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17857 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17858 the glyph row and area where the glyph comes from. */
17860 void
17861 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17863 if (glyph->type == CHAR_GLYPH
17864 || glyph->type == GLYPHLESS_GLYPH)
17866 fprintf (stderr,
17867 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17868 glyph - row->glyphs[TEXT_AREA],
17869 (glyph->type == CHAR_GLYPH
17870 ? 'C'
17871 : 'G'),
17872 glyph->charpos,
17873 (BUFFERP (glyph->object)
17874 ? 'B'
17875 : (STRINGP (glyph->object)
17876 ? 'S'
17877 : (INTEGERP (glyph->object)
17878 ? '0'
17879 : '-'))),
17880 glyph->pixel_width,
17881 glyph->u.ch,
17882 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17883 ? glyph->u.ch
17884 : '.'),
17885 glyph->face_id,
17886 glyph->left_box_line_p,
17887 glyph->right_box_line_p);
17889 else if (glyph->type == STRETCH_GLYPH)
17891 fprintf (stderr,
17892 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17893 glyph - row->glyphs[TEXT_AREA],
17894 'S',
17895 glyph->charpos,
17896 (BUFFERP (glyph->object)
17897 ? 'B'
17898 : (STRINGP (glyph->object)
17899 ? 'S'
17900 : (INTEGERP (glyph->object)
17901 ? '0'
17902 : '-'))),
17903 glyph->pixel_width,
17905 ' ',
17906 glyph->face_id,
17907 glyph->left_box_line_p,
17908 glyph->right_box_line_p);
17910 else if (glyph->type == IMAGE_GLYPH)
17912 fprintf (stderr,
17913 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17914 glyph - row->glyphs[TEXT_AREA],
17915 'I',
17916 glyph->charpos,
17917 (BUFFERP (glyph->object)
17918 ? 'B'
17919 : (STRINGP (glyph->object)
17920 ? 'S'
17921 : (INTEGERP (glyph->object)
17922 ? '0'
17923 : '-'))),
17924 glyph->pixel_width,
17925 glyph->u.img_id,
17926 '.',
17927 glyph->face_id,
17928 glyph->left_box_line_p,
17929 glyph->right_box_line_p);
17931 else if (glyph->type == COMPOSITE_GLYPH)
17933 fprintf (stderr,
17934 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17935 glyph - row->glyphs[TEXT_AREA],
17936 '+',
17937 glyph->charpos,
17938 (BUFFERP (glyph->object)
17939 ? 'B'
17940 : (STRINGP (glyph->object)
17941 ? 'S'
17942 : (INTEGERP (glyph->object)
17943 ? '0'
17944 : '-'))),
17945 glyph->pixel_width,
17946 glyph->u.cmp.id);
17947 if (glyph->u.cmp.automatic)
17948 fprintf (stderr,
17949 "[%d-%d]",
17950 glyph->slice.cmp.from, glyph->slice.cmp.to);
17951 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17952 glyph->face_id,
17953 glyph->left_box_line_p,
17954 glyph->right_box_line_p);
17959 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17960 GLYPHS 0 means don't show glyph contents.
17961 GLYPHS 1 means show glyphs in short form
17962 GLYPHS > 1 means show glyphs in long form. */
17964 void
17965 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17967 if (glyphs != 1)
17969 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17970 fprintf (stderr, "==============================================================================\n");
17972 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17973 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17974 vpos,
17975 MATRIX_ROW_START_CHARPOS (row),
17976 MATRIX_ROW_END_CHARPOS (row),
17977 row->used[TEXT_AREA],
17978 row->contains_overlapping_glyphs_p,
17979 row->enabled_p,
17980 row->truncated_on_left_p,
17981 row->truncated_on_right_p,
17982 row->continued_p,
17983 MATRIX_ROW_CONTINUATION_LINE_P (row),
17984 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17985 row->ends_at_zv_p,
17986 row->fill_line_p,
17987 row->ends_in_middle_of_char_p,
17988 row->starts_in_middle_of_char_p,
17989 row->mouse_face_p,
17990 row->x,
17991 row->y,
17992 row->pixel_width,
17993 row->height,
17994 row->visible_height,
17995 row->ascent,
17996 row->phys_ascent);
17997 /* The next 3 lines should align to "Start" in the header. */
17998 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17999 row->end.overlay_string_index,
18000 row->continuation_lines_width);
18001 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18002 CHARPOS (row->start.string_pos),
18003 CHARPOS (row->end.string_pos));
18004 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18005 row->end.dpvec_index);
18008 if (glyphs > 1)
18010 int area;
18012 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18014 struct glyph *glyph = row->glyphs[area];
18015 struct glyph *glyph_end = glyph + row->used[area];
18017 /* Glyph for a line end in text. */
18018 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18019 ++glyph_end;
18021 if (glyph < glyph_end)
18022 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18024 for (; glyph < glyph_end; ++glyph)
18025 dump_glyph (row, glyph, area);
18028 else if (glyphs == 1)
18030 int area;
18032 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18034 char *s = alloca (row->used[area] + 4);
18035 int i;
18037 for (i = 0; i < row->used[area]; ++i)
18039 struct glyph *glyph = row->glyphs[area] + i;
18040 if (i == row->used[area] - 1
18041 && area == TEXT_AREA
18042 && INTEGERP (glyph->object)
18043 && glyph->type == CHAR_GLYPH
18044 && glyph->u.ch == ' ')
18046 strcpy (&s[i], "[\\n]");
18047 i += 4;
18049 else if (glyph->type == CHAR_GLYPH
18050 && glyph->u.ch < 0x80
18051 && glyph->u.ch >= ' ')
18052 s[i] = glyph->u.ch;
18053 else
18054 s[i] = '.';
18057 s[i] = '\0';
18058 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18064 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18065 Sdump_glyph_matrix, 0, 1, "p",
18066 doc: /* Dump the current matrix of the selected window to stderr.
18067 Shows contents of glyph row structures. With non-nil
18068 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18069 glyphs in short form, otherwise show glyphs in long form. */)
18070 (Lisp_Object glyphs)
18072 struct window *w = XWINDOW (selected_window);
18073 struct buffer *buffer = XBUFFER (w->contents);
18075 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18076 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18077 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18078 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18079 fprintf (stderr, "=============================================\n");
18080 dump_glyph_matrix (w->current_matrix,
18081 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18082 return Qnil;
18086 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18087 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18088 (void)
18090 struct frame *f = XFRAME (selected_frame);
18091 dump_glyph_matrix (f->current_matrix, 1);
18092 return Qnil;
18096 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18097 doc: /* Dump glyph row ROW to stderr.
18098 GLYPH 0 means don't dump glyphs.
18099 GLYPH 1 means dump glyphs in short form.
18100 GLYPH > 1 or omitted means dump glyphs in long form. */)
18101 (Lisp_Object row, Lisp_Object glyphs)
18103 struct glyph_matrix *matrix;
18104 EMACS_INT vpos;
18106 CHECK_NUMBER (row);
18107 matrix = XWINDOW (selected_window)->current_matrix;
18108 vpos = XINT (row);
18109 if (vpos >= 0 && vpos < matrix->nrows)
18110 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18111 vpos,
18112 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18113 return Qnil;
18117 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18118 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18119 GLYPH 0 means don't dump glyphs.
18120 GLYPH 1 means dump glyphs in short form.
18121 GLYPH > 1 or omitted means dump glyphs in long form. */)
18122 (Lisp_Object row, Lisp_Object glyphs)
18124 struct frame *sf = SELECTED_FRAME ();
18125 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18126 EMACS_INT vpos;
18128 CHECK_NUMBER (row);
18129 vpos = XINT (row);
18130 if (vpos >= 0 && vpos < m->nrows)
18131 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18132 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18133 return Qnil;
18137 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18138 doc: /* Toggle tracing of redisplay.
18139 With ARG, turn tracing on if and only if ARG is positive. */)
18140 (Lisp_Object arg)
18142 if (NILP (arg))
18143 trace_redisplay_p = !trace_redisplay_p;
18144 else
18146 arg = Fprefix_numeric_value (arg);
18147 trace_redisplay_p = XINT (arg) > 0;
18150 return Qnil;
18154 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18155 doc: /* Like `format', but print result to stderr.
18156 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18157 (ptrdiff_t nargs, Lisp_Object *args)
18159 Lisp_Object s = Fformat (nargs, args);
18160 fprintf (stderr, "%s", SDATA (s));
18161 return Qnil;
18164 #endif /* GLYPH_DEBUG */
18168 /***********************************************************************
18169 Building Desired Matrix Rows
18170 ***********************************************************************/
18172 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18173 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18175 static struct glyph_row *
18176 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18178 struct frame *f = XFRAME (WINDOW_FRAME (w));
18179 struct buffer *buffer = XBUFFER (w->contents);
18180 struct buffer *old = current_buffer;
18181 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18182 int arrow_len = SCHARS (overlay_arrow_string);
18183 const unsigned char *arrow_end = arrow_string + arrow_len;
18184 const unsigned char *p;
18185 struct it it;
18186 bool multibyte_p;
18187 int n_glyphs_before;
18189 set_buffer_temp (buffer);
18190 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18191 it.glyph_row->used[TEXT_AREA] = 0;
18192 SET_TEXT_POS (it.position, 0, 0);
18194 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18195 p = arrow_string;
18196 while (p < arrow_end)
18198 Lisp_Object face, ilisp;
18200 /* Get the next character. */
18201 if (multibyte_p)
18202 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18203 else
18205 it.c = it.char_to_display = *p, it.len = 1;
18206 if (! ASCII_CHAR_P (it.c))
18207 it.char_to_display = BYTE8_TO_CHAR (it.c);
18209 p += it.len;
18211 /* Get its face. */
18212 ilisp = make_number (p - arrow_string);
18213 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18214 it.face_id = compute_char_face (f, it.char_to_display, face);
18216 /* Compute its width, get its glyphs. */
18217 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18218 SET_TEXT_POS (it.position, -1, -1);
18219 PRODUCE_GLYPHS (&it);
18221 /* If this character doesn't fit any more in the line, we have
18222 to remove some glyphs. */
18223 if (it.current_x > it.last_visible_x)
18225 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18226 break;
18230 set_buffer_temp (old);
18231 return it.glyph_row;
18235 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18236 glyphs to insert is determined by produce_special_glyphs. */
18238 static void
18239 insert_left_trunc_glyphs (struct it *it)
18241 struct it truncate_it;
18242 struct glyph *from, *end, *to, *toend;
18244 eassert (!FRAME_WINDOW_P (it->f)
18245 || (!it->glyph_row->reversed_p
18246 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18247 || (it->glyph_row->reversed_p
18248 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18250 /* Get the truncation glyphs. */
18251 truncate_it = *it;
18252 truncate_it.current_x = 0;
18253 truncate_it.face_id = DEFAULT_FACE_ID;
18254 truncate_it.glyph_row = &scratch_glyph_row;
18255 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18256 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18257 truncate_it.object = make_number (0);
18258 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18260 /* Overwrite glyphs from IT with truncation glyphs. */
18261 if (!it->glyph_row->reversed_p)
18263 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18265 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18266 end = from + tused;
18267 to = it->glyph_row->glyphs[TEXT_AREA];
18268 toend = to + it->glyph_row->used[TEXT_AREA];
18269 if (FRAME_WINDOW_P (it->f))
18271 /* On GUI frames, when variable-size fonts are displayed,
18272 the truncation glyphs may need more pixels than the row's
18273 glyphs they overwrite. We overwrite more glyphs to free
18274 enough screen real estate, and enlarge the stretch glyph
18275 on the right (see display_line), if there is one, to
18276 preserve the screen position of the truncation glyphs on
18277 the right. */
18278 int w = 0;
18279 struct glyph *g = to;
18280 short used;
18282 /* The first glyph could be partially visible, in which case
18283 it->glyph_row->x will be negative. But we want the left
18284 truncation glyphs to be aligned at the left margin of the
18285 window, so we override the x coordinate at which the row
18286 will begin. */
18287 it->glyph_row->x = 0;
18288 while (g < toend && w < it->truncation_pixel_width)
18290 w += g->pixel_width;
18291 ++g;
18293 if (g - to - tused > 0)
18295 memmove (to + tused, g, (toend - g) * sizeof(*g));
18296 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18298 used = it->glyph_row->used[TEXT_AREA];
18299 if (it->glyph_row->truncated_on_right_p
18300 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18301 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18302 == STRETCH_GLYPH)
18304 int extra = w - it->truncation_pixel_width;
18306 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18310 while (from < end)
18311 *to++ = *from++;
18313 /* There may be padding glyphs left over. Overwrite them too. */
18314 if (!FRAME_WINDOW_P (it->f))
18316 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18318 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18319 while (from < end)
18320 *to++ = *from++;
18324 if (to > toend)
18325 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18327 else
18329 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18331 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18332 that back to front. */
18333 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18334 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18335 toend = it->glyph_row->glyphs[TEXT_AREA];
18336 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18337 if (FRAME_WINDOW_P (it->f))
18339 int w = 0;
18340 struct glyph *g = to;
18342 while (g >= toend && w < it->truncation_pixel_width)
18344 w += g->pixel_width;
18345 --g;
18347 if (to - g - tused > 0)
18348 to = g + tused;
18349 if (it->glyph_row->truncated_on_right_p
18350 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18351 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18353 int extra = w - it->truncation_pixel_width;
18355 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18359 while (from >= end && to >= toend)
18360 *to-- = *from--;
18361 if (!FRAME_WINDOW_P (it->f))
18363 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18365 from =
18366 truncate_it.glyph_row->glyphs[TEXT_AREA]
18367 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18368 while (from >= end && to >= toend)
18369 *to-- = *from--;
18372 if (from >= end)
18374 /* Need to free some room before prepending additional
18375 glyphs. */
18376 int move_by = from - end + 1;
18377 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18378 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18380 for ( ; g >= g0; g--)
18381 g[move_by] = *g;
18382 while (from >= end)
18383 *to-- = *from--;
18384 it->glyph_row->used[TEXT_AREA] += move_by;
18389 /* Compute the hash code for ROW. */
18390 unsigned
18391 row_hash (struct glyph_row *row)
18393 int area, k;
18394 unsigned hashval = 0;
18396 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18397 for (k = 0; k < row->used[area]; ++k)
18398 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18399 + row->glyphs[area][k].u.val
18400 + row->glyphs[area][k].face_id
18401 + row->glyphs[area][k].padding_p
18402 + (row->glyphs[area][k].type << 2));
18404 return hashval;
18407 /* Compute the pixel height and width of IT->glyph_row.
18409 Most of the time, ascent and height of a display line will be equal
18410 to the max_ascent and max_height values of the display iterator
18411 structure. This is not the case if
18413 1. We hit ZV without displaying anything. In this case, max_ascent
18414 and max_height will be zero.
18416 2. We have some glyphs that don't contribute to the line height.
18417 (The glyph row flag contributes_to_line_height_p is for future
18418 pixmap extensions).
18420 The first case is easily covered by using default values because in
18421 these cases, the line height does not really matter, except that it
18422 must not be zero. */
18424 static void
18425 compute_line_metrics (struct it *it)
18427 struct glyph_row *row = it->glyph_row;
18429 if (FRAME_WINDOW_P (it->f))
18431 int i, min_y, max_y;
18433 /* The line may consist of one space only, that was added to
18434 place the cursor on it. If so, the row's height hasn't been
18435 computed yet. */
18436 if (row->height == 0)
18438 if (it->max_ascent + it->max_descent == 0)
18439 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18440 row->ascent = it->max_ascent;
18441 row->height = it->max_ascent + it->max_descent;
18442 row->phys_ascent = it->max_phys_ascent;
18443 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18444 row->extra_line_spacing = it->max_extra_line_spacing;
18447 /* Compute the width of this line. */
18448 row->pixel_width = row->x;
18449 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18450 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18452 eassert (row->pixel_width >= 0);
18453 eassert (row->ascent >= 0 && row->height > 0);
18455 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18456 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18458 /* If first line's physical ascent is larger than its logical
18459 ascent, use the physical ascent, and make the row taller.
18460 This makes accented characters fully visible. */
18461 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18462 && row->phys_ascent > row->ascent)
18464 row->height += row->phys_ascent - row->ascent;
18465 row->ascent = row->phys_ascent;
18468 /* Compute how much of the line is visible. */
18469 row->visible_height = row->height;
18471 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18472 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18474 if (row->y < min_y)
18475 row->visible_height -= min_y - row->y;
18476 if (row->y + row->height > max_y)
18477 row->visible_height -= row->y + row->height - max_y;
18479 else
18481 row->pixel_width = row->used[TEXT_AREA];
18482 if (row->continued_p)
18483 row->pixel_width -= it->continuation_pixel_width;
18484 else if (row->truncated_on_right_p)
18485 row->pixel_width -= it->truncation_pixel_width;
18486 row->ascent = row->phys_ascent = 0;
18487 row->height = row->phys_height = row->visible_height = 1;
18488 row->extra_line_spacing = 0;
18491 /* Compute a hash code for this row. */
18492 row->hash = row_hash (row);
18494 it->max_ascent = it->max_descent = 0;
18495 it->max_phys_ascent = it->max_phys_descent = 0;
18499 /* Append one space to the glyph row of iterator IT if doing a
18500 window-based redisplay. The space has the same face as
18501 IT->face_id. Value is non-zero if a space was added.
18503 This function is called to make sure that there is always one glyph
18504 at the end of a glyph row that the cursor can be set on under
18505 window-systems. (If there weren't such a glyph we would not know
18506 how wide and tall a box cursor should be displayed).
18508 At the same time this space let's a nicely handle clearing to the
18509 end of the line if the row ends in italic text. */
18511 static int
18512 append_space_for_newline (struct it *it, int default_face_p)
18514 if (FRAME_WINDOW_P (it->f))
18516 int n = it->glyph_row->used[TEXT_AREA];
18518 if (it->glyph_row->glyphs[TEXT_AREA] + n
18519 < it->glyph_row->glyphs[1 + TEXT_AREA])
18521 /* Save some values that must not be changed.
18522 Must save IT->c and IT->len because otherwise
18523 ITERATOR_AT_END_P wouldn't work anymore after
18524 append_space_for_newline has been called. */
18525 enum display_element_type saved_what = it->what;
18526 int saved_c = it->c, saved_len = it->len;
18527 int saved_char_to_display = it->char_to_display;
18528 int saved_x = it->current_x;
18529 int saved_face_id = it->face_id;
18530 int saved_box_end = it->end_of_box_run_p;
18531 struct text_pos saved_pos;
18532 Lisp_Object saved_object;
18533 struct face *face;
18535 saved_object = it->object;
18536 saved_pos = it->position;
18538 it->what = IT_CHARACTER;
18539 memset (&it->position, 0, sizeof it->position);
18540 it->object = make_number (0);
18541 it->c = it->char_to_display = ' ';
18542 it->len = 1;
18544 /* If the default face was remapped, be sure to use the
18545 remapped face for the appended newline. */
18546 if (default_face_p)
18547 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18548 else if (it->face_before_selective_p)
18549 it->face_id = it->saved_face_id;
18550 face = FACE_FROM_ID (it->f, it->face_id);
18551 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18552 /* In R2L rows, we will prepend a stretch glyph that will
18553 have the end_of_box_run_p flag set for it, so there's no
18554 need for the appended newline glyph to have that flag
18555 set. */
18556 if (it->glyph_row->reversed_p
18557 /* But if the appended newline glyph goes all the way to
18558 the end of the row, there will be no stretch glyph,
18559 so leave the box flag set. */
18560 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18561 it->end_of_box_run_p = 0;
18563 PRODUCE_GLYPHS (it);
18565 it->override_ascent = -1;
18566 it->constrain_row_ascent_descent_p = 0;
18567 it->current_x = saved_x;
18568 it->object = saved_object;
18569 it->position = saved_pos;
18570 it->what = saved_what;
18571 it->face_id = saved_face_id;
18572 it->len = saved_len;
18573 it->c = saved_c;
18574 it->char_to_display = saved_char_to_display;
18575 it->end_of_box_run_p = saved_box_end;
18576 return 1;
18580 return 0;
18584 /* Extend the face of the last glyph in the text area of IT->glyph_row
18585 to the end of the display line. Called from display_line. If the
18586 glyph row is empty, add a space glyph to it so that we know the
18587 face to draw. Set the glyph row flag fill_line_p. If the glyph
18588 row is R2L, prepend a stretch glyph to cover the empty space to the
18589 left of the leftmost glyph. */
18591 static void
18592 extend_face_to_end_of_line (struct it *it)
18594 struct face *face, *default_face;
18595 struct frame *f = it->f;
18597 /* If line is already filled, do nothing. Non window-system frames
18598 get a grace of one more ``pixel'' because their characters are
18599 1-``pixel'' wide, so they hit the equality too early. This grace
18600 is needed only for R2L rows that are not continued, to produce
18601 one extra blank where we could display the cursor. */
18602 if (it->current_x >= it->last_visible_x
18603 + (!FRAME_WINDOW_P (f)
18604 && it->glyph_row->reversed_p
18605 && !it->glyph_row->continued_p))
18606 return;
18608 /* The default face, possibly remapped. */
18609 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18611 /* Face extension extends the background and box of IT->face_id
18612 to the end of the line. If the background equals the background
18613 of the frame, we don't have to do anything. */
18614 if (it->face_before_selective_p)
18615 face = FACE_FROM_ID (f, it->saved_face_id);
18616 else
18617 face = FACE_FROM_ID (f, it->face_id);
18619 if (FRAME_WINDOW_P (f)
18620 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18621 && face->box == FACE_NO_BOX
18622 && face->background == FRAME_BACKGROUND_PIXEL (f)
18623 && !face->stipple
18624 && !it->glyph_row->reversed_p)
18625 return;
18627 /* Set the glyph row flag indicating that the face of the last glyph
18628 in the text area has to be drawn to the end of the text area. */
18629 it->glyph_row->fill_line_p = 1;
18631 /* If current character of IT is not ASCII, make sure we have the
18632 ASCII face. This will be automatically undone the next time
18633 get_next_display_element returns a multibyte character. Note
18634 that the character will always be single byte in unibyte
18635 text. */
18636 if (!ASCII_CHAR_P (it->c))
18638 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18641 if (FRAME_WINDOW_P (f))
18643 /* If the row is empty, add a space with the current face of IT,
18644 so that we know which face to draw. */
18645 if (it->glyph_row->used[TEXT_AREA] == 0)
18647 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18648 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18649 it->glyph_row->used[TEXT_AREA] = 1;
18651 #ifdef HAVE_WINDOW_SYSTEM
18652 if (it->glyph_row->reversed_p)
18654 /* Prepend a stretch glyph to the row, such that the
18655 rightmost glyph will be drawn flushed all the way to the
18656 right margin of the window. The stretch glyph that will
18657 occupy the empty space, if any, to the left of the
18658 glyphs. */
18659 struct font *font = face->font ? face->font : FRAME_FONT (f);
18660 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18661 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18662 struct glyph *g;
18663 int row_width, stretch_ascent, stretch_width;
18664 struct text_pos saved_pos;
18665 int saved_face_id, saved_avoid_cursor, saved_box_start;
18667 for (row_width = 0, g = row_start; g < row_end; g++)
18668 row_width += g->pixel_width;
18669 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18670 if (stretch_width > 0)
18672 stretch_ascent =
18673 (((it->ascent + it->descent)
18674 * FONT_BASE (font)) / FONT_HEIGHT (font));
18675 saved_pos = it->position;
18676 memset (&it->position, 0, sizeof it->position);
18677 saved_avoid_cursor = it->avoid_cursor_p;
18678 it->avoid_cursor_p = 1;
18679 saved_face_id = it->face_id;
18680 saved_box_start = it->start_of_box_run_p;
18681 /* The last row's stretch glyph should get the default
18682 face, to avoid painting the rest of the window with
18683 the region face, if the region ends at ZV. */
18684 if (it->glyph_row->ends_at_zv_p)
18685 it->face_id = default_face->id;
18686 else
18687 it->face_id = face->id;
18688 it->start_of_box_run_p = 0;
18689 append_stretch_glyph (it, make_number (0), stretch_width,
18690 it->ascent + it->descent, stretch_ascent);
18691 it->position = saved_pos;
18692 it->avoid_cursor_p = saved_avoid_cursor;
18693 it->face_id = saved_face_id;
18694 it->start_of_box_run_p = saved_box_start;
18697 #endif /* HAVE_WINDOW_SYSTEM */
18699 else
18701 /* Save some values that must not be changed. */
18702 int saved_x = it->current_x;
18703 struct text_pos saved_pos;
18704 Lisp_Object saved_object;
18705 enum display_element_type saved_what = it->what;
18706 int saved_face_id = it->face_id;
18708 saved_object = it->object;
18709 saved_pos = it->position;
18711 it->what = IT_CHARACTER;
18712 memset (&it->position, 0, sizeof it->position);
18713 it->object = make_number (0);
18714 it->c = it->char_to_display = ' ';
18715 it->len = 1;
18716 /* The last row's blank glyphs should get the default face, to
18717 avoid painting the rest of the window with the region face,
18718 if the region ends at ZV. */
18719 if (it->glyph_row->ends_at_zv_p)
18720 it->face_id = default_face->id;
18721 else
18722 it->face_id = face->id;
18724 PRODUCE_GLYPHS (it);
18726 while (it->current_x <= it->last_visible_x)
18727 PRODUCE_GLYPHS (it);
18729 /* Don't count these blanks really. It would let us insert a left
18730 truncation glyph below and make us set the cursor on them, maybe. */
18731 it->current_x = saved_x;
18732 it->object = saved_object;
18733 it->position = saved_pos;
18734 it->what = saved_what;
18735 it->face_id = saved_face_id;
18740 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18741 trailing whitespace. */
18743 static int
18744 trailing_whitespace_p (ptrdiff_t charpos)
18746 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18747 int c = 0;
18749 while (bytepos < ZV_BYTE
18750 && (c = FETCH_CHAR (bytepos),
18751 c == ' ' || c == '\t'))
18752 ++bytepos;
18754 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18756 if (bytepos != PT_BYTE)
18757 return 1;
18759 return 0;
18763 /* Highlight trailing whitespace, if any, in ROW. */
18765 static void
18766 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18768 int used = row->used[TEXT_AREA];
18770 if (used)
18772 struct glyph *start = row->glyphs[TEXT_AREA];
18773 struct glyph *glyph = start + used - 1;
18775 if (row->reversed_p)
18777 /* Right-to-left rows need to be processed in the opposite
18778 direction, so swap the edge pointers. */
18779 glyph = start;
18780 start = row->glyphs[TEXT_AREA] + used - 1;
18783 /* Skip over glyphs inserted to display the cursor at the
18784 end of a line, for extending the face of the last glyph
18785 to the end of the line on terminals, and for truncation
18786 and continuation glyphs. */
18787 if (!row->reversed_p)
18789 while (glyph >= start
18790 && glyph->type == CHAR_GLYPH
18791 && INTEGERP (glyph->object))
18792 --glyph;
18794 else
18796 while (glyph <= start
18797 && glyph->type == CHAR_GLYPH
18798 && INTEGERP (glyph->object))
18799 ++glyph;
18802 /* If last glyph is a space or stretch, and it's trailing
18803 whitespace, set the face of all trailing whitespace glyphs in
18804 IT->glyph_row to `trailing-whitespace'. */
18805 if ((row->reversed_p ? glyph <= start : glyph >= start)
18806 && BUFFERP (glyph->object)
18807 && (glyph->type == STRETCH_GLYPH
18808 || (glyph->type == CHAR_GLYPH
18809 && glyph->u.ch == ' '))
18810 && trailing_whitespace_p (glyph->charpos))
18812 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18813 if (face_id < 0)
18814 return;
18816 if (!row->reversed_p)
18818 while (glyph >= start
18819 && BUFFERP (glyph->object)
18820 && (glyph->type == STRETCH_GLYPH
18821 || (glyph->type == CHAR_GLYPH
18822 && glyph->u.ch == ' ')))
18823 (glyph--)->face_id = face_id;
18825 else
18827 while (glyph <= start
18828 && BUFFERP (glyph->object)
18829 && (glyph->type == STRETCH_GLYPH
18830 || (glyph->type == CHAR_GLYPH
18831 && glyph->u.ch == ' ')))
18832 (glyph++)->face_id = face_id;
18839 /* Value is non-zero if glyph row ROW should be
18840 considered to hold the buffer position CHARPOS. */
18842 static int
18843 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18845 int result = 1;
18847 if (charpos == CHARPOS (row->end.pos)
18848 || charpos == MATRIX_ROW_END_CHARPOS (row))
18850 /* Suppose the row ends on a string.
18851 Unless the row is continued, that means it ends on a newline
18852 in the string. If it's anything other than a display string
18853 (e.g., a before-string from an overlay), we don't want the
18854 cursor there. (This heuristic seems to give the optimal
18855 behavior for the various types of multi-line strings.)
18856 One exception: if the string has `cursor' property on one of
18857 its characters, we _do_ want the cursor there. */
18858 if (CHARPOS (row->end.string_pos) >= 0)
18860 if (row->continued_p)
18861 result = 1;
18862 else
18864 /* Check for `display' property. */
18865 struct glyph *beg = row->glyphs[TEXT_AREA];
18866 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18867 struct glyph *glyph;
18869 result = 0;
18870 for (glyph = end; glyph >= beg; --glyph)
18871 if (STRINGP (glyph->object))
18873 Lisp_Object prop
18874 = Fget_char_property (make_number (charpos),
18875 Qdisplay, Qnil);
18876 result =
18877 (!NILP (prop)
18878 && display_prop_string_p (prop, glyph->object));
18879 /* If there's a `cursor' property on one of the
18880 string's characters, this row is a cursor row,
18881 even though this is not a display string. */
18882 if (!result)
18884 Lisp_Object s = glyph->object;
18886 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18888 ptrdiff_t gpos = glyph->charpos;
18890 if (!NILP (Fget_char_property (make_number (gpos),
18891 Qcursor, s)))
18893 result = 1;
18894 break;
18898 break;
18902 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18904 /* If the row ends in middle of a real character,
18905 and the line is continued, we want the cursor here.
18906 That's because CHARPOS (ROW->end.pos) would equal
18907 PT if PT is before the character. */
18908 if (!row->ends_in_ellipsis_p)
18909 result = row->continued_p;
18910 else
18911 /* If the row ends in an ellipsis, then
18912 CHARPOS (ROW->end.pos) will equal point after the
18913 invisible text. We want that position to be displayed
18914 after the ellipsis. */
18915 result = 0;
18917 /* If the row ends at ZV, display the cursor at the end of that
18918 row instead of at the start of the row below. */
18919 else if (row->ends_at_zv_p)
18920 result = 1;
18921 else
18922 result = 0;
18925 return result;
18928 /* Value is non-zero if glyph row ROW should be
18929 used to hold the cursor. */
18931 static int
18932 cursor_row_p (struct glyph_row *row)
18934 return row_for_charpos_p (row, PT);
18939 /* Push the property PROP so that it will be rendered at the current
18940 position in IT. Return 1 if PROP was successfully pushed, 0
18941 otherwise. Called from handle_line_prefix to handle the
18942 `line-prefix' and `wrap-prefix' properties. */
18944 static int
18945 push_prefix_prop (struct it *it, Lisp_Object prop)
18947 struct text_pos pos =
18948 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18950 eassert (it->method == GET_FROM_BUFFER
18951 || it->method == GET_FROM_DISPLAY_VECTOR
18952 || it->method == GET_FROM_STRING);
18954 /* We need to save the current buffer/string position, so it will be
18955 restored by pop_it, because iterate_out_of_display_property
18956 depends on that being set correctly, but some situations leave
18957 it->position not yet set when this function is called. */
18958 push_it (it, &pos);
18960 if (STRINGP (prop))
18962 if (SCHARS (prop) == 0)
18964 pop_it (it);
18965 return 0;
18968 it->string = prop;
18969 it->string_from_prefix_prop_p = 1;
18970 it->multibyte_p = STRING_MULTIBYTE (it->string);
18971 it->current.overlay_string_index = -1;
18972 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18973 it->end_charpos = it->string_nchars = SCHARS (it->string);
18974 it->method = GET_FROM_STRING;
18975 it->stop_charpos = 0;
18976 it->prev_stop = 0;
18977 it->base_level_stop = 0;
18979 /* Force paragraph direction to be that of the parent
18980 buffer/string. */
18981 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18982 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18983 else
18984 it->paragraph_embedding = L2R;
18986 /* Set up the bidi iterator for this display string. */
18987 if (it->bidi_p)
18989 it->bidi_it.string.lstring = it->string;
18990 it->bidi_it.string.s = NULL;
18991 it->bidi_it.string.schars = it->end_charpos;
18992 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18993 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18994 it->bidi_it.string.unibyte = !it->multibyte_p;
18995 it->bidi_it.w = it->w;
18996 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18999 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19001 it->method = GET_FROM_STRETCH;
19002 it->object = prop;
19004 #ifdef HAVE_WINDOW_SYSTEM
19005 else if (IMAGEP (prop))
19007 it->what = IT_IMAGE;
19008 it->image_id = lookup_image (it->f, prop);
19009 it->method = GET_FROM_IMAGE;
19011 #endif /* HAVE_WINDOW_SYSTEM */
19012 else
19014 pop_it (it); /* bogus display property, give up */
19015 return 0;
19018 return 1;
19021 /* Return the character-property PROP at the current position in IT. */
19023 static Lisp_Object
19024 get_it_property (struct it *it, Lisp_Object prop)
19026 Lisp_Object position, object = it->object;
19028 if (STRINGP (object))
19029 position = make_number (IT_STRING_CHARPOS (*it));
19030 else if (BUFFERP (object))
19032 position = make_number (IT_CHARPOS (*it));
19033 object = it->window;
19035 else
19036 return Qnil;
19038 return Fget_char_property (position, prop, object);
19041 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19043 static void
19044 handle_line_prefix (struct it *it)
19046 Lisp_Object prefix;
19048 if (it->continuation_lines_width > 0)
19050 prefix = get_it_property (it, Qwrap_prefix);
19051 if (NILP (prefix))
19052 prefix = Vwrap_prefix;
19054 else
19056 prefix = get_it_property (it, Qline_prefix);
19057 if (NILP (prefix))
19058 prefix = Vline_prefix;
19060 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19062 /* If the prefix is wider than the window, and we try to wrap
19063 it, it would acquire its own wrap prefix, and so on till the
19064 iterator stack overflows. So, don't wrap the prefix. */
19065 it->line_wrap = TRUNCATE;
19066 it->avoid_cursor_p = 1;
19072 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19073 only for R2L lines from display_line and display_string, when they
19074 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19075 the line/string needs to be continued on the next glyph row. */
19076 static void
19077 unproduce_glyphs (struct it *it, int n)
19079 struct glyph *glyph, *end;
19081 eassert (it->glyph_row);
19082 eassert (it->glyph_row->reversed_p);
19083 eassert (it->area == TEXT_AREA);
19084 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19086 if (n > it->glyph_row->used[TEXT_AREA])
19087 n = it->glyph_row->used[TEXT_AREA];
19088 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19089 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19090 for ( ; glyph < end; glyph++)
19091 glyph[-n] = *glyph;
19094 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19095 and ROW->maxpos. */
19096 static void
19097 find_row_edges (struct it *it, struct glyph_row *row,
19098 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19099 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19101 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19102 lines' rows is implemented for bidi-reordered rows. */
19104 /* ROW->minpos is the value of min_pos, the minimal buffer position
19105 we have in ROW, or ROW->start.pos if that is smaller. */
19106 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19107 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19108 else
19109 /* We didn't find buffer positions smaller than ROW->start, or
19110 didn't find _any_ valid buffer positions in any of the glyphs,
19111 so we must trust the iterator's computed positions. */
19112 row->minpos = row->start.pos;
19113 if (max_pos <= 0)
19115 max_pos = CHARPOS (it->current.pos);
19116 max_bpos = BYTEPOS (it->current.pos);
19119 /* Here are the various use-cases for ending the row, and the
19120 corresponding values for ROW->maxpos:
19122 Line ends in a newline from buffer eol_pos + 1
19123 Line is continued from buffer max_pos + 1
19124 Line is truncated on right it->current.pos
19125 Line ends in a newline from string max_pos + 1(*)
19126 (*) + 1 only when line ends in a forward scan
19127 Line is continued from string max_pos
19128 Line is continued from display vector max_pos
19129 Line is entirely from a string min_pos == max_pos
19130 Line is entirely from a display vector min_pos == max_pos
19131 Line that ends at ZV ZV
19133 If you discover other use-cases, please add them here as
19134 appropriate. */
19135 if (row->ends_at_zv_p)
19136 row->maxpos = it->current.pos;
19137 else if (row->used[TEXT_AREA])
19139 int seen_this_string = 0;
19140 struct glyph_row *r1 = row - 1;
19142 /* Did we see the same display string on the previous row? */
19143 if (STRINGP (it->object)
19144 /* this is not the first row */
19145 && row > it->w->desired_matrix->rows
19146 /* previous row is not the header line */
19147 && !r1->mode_line_p
19148 /* previous row also ends in a newline from a string */
19149 && r1->ends_in_newline_from_string_p)
19151 struct glyph *start, *end;
19153 /* Search for the last glyph of the previous row that came
19154 from buffer or string. Depending on whether the row is
19155 L2R or R2L, we need to process it front to back or the
19156 other way round. */
19157 if (!r1->reversed_p)
19159 start = r1->glyphs[TEXT_AREA];
19160 end = start + r1->used[TEXT_AREA];
19161 /* Glyphs inserted by redisplay have an integer (zero)
19162 as their object. */
19163 while (end > start
19164 && INTEGERP ((end - 1)->object)
19165 && (end - 1)->charpos <= 0)
19166 --end;
19167 if (end > start)
19169 if (EQ ((end - 1)->object, it->object))
19170 seen_this_string = 1;
19172 else
19173 /* If all the glyphs of the previous row were inserted
19174 by redisplay, it means the previous row was
19175 produced from a single newline, which is only
19176 possible if that newline came from the same string
19177 as the one which produced this ROW. */
19178 seen_this_string = 1;
19180 else
19182 end = r1->glyphs[TEXT_AREA] - 1;
19183 start = end + r1->used[TEXT_AREA];
19184 while (end < start
19185 && INTEGERP ((end + 1)->object)
19186 && (end + 1)->charpos <= 0)
19187 ++end;
19188 if (end < start)
19190 if (EQ ((end + 1)->object, it->object))
19191 seen_this_string = 1;
19193 else
19194 seen_this_string = 1;
19197 /* Take note of each display string that covers a newline only
19198 once, the first time we see it. This is for when a display
19199 string includes more than one newline in it. */
19200 if (row->ends_in_newline_from_string_p && !seen_this_string)
19202 /* If we were scanning the buffer forward when we displayed
19203 the string, we want to account for at least one buffer
19204 position that belongs to this row (position covered by
19205 the display string), so that cursor positioning will
19206 consider this row as a candidate when point is at the end
19207 of the visual line represented by this row. This is not
19208 required when scanning back, because max_pos will already
19209 have a much larger value. */
19210 if (CHARPOS (row->end.pos) > max_pos)
19211 INC_BOTH (max_pos, max_bpos);
19212 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19214 else if (CHARPOS (it->eol_pos) > 0)
19215 SET_TEXT_POS (row->maxpos,
19216 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19217 else if (row->continued_p)
19219 /* If max_pos is different from IT's current position, it
19220 means IT->method does not belong to the display element
19221 at max_pos. However, it also means that the display
19222 element at max_pos was displayed in its entirety on this
19223 line, which is equivalent to saying that the next line
19224 starts at the next buffer position. */
19225 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19226 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19227 else
19229 INC_BOTH (max_pos, max_bpos);
19230 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19233 else if (row->truncated_on_right_p)
19234 /* display_line already called reseat_at_next_visible_line_start,
19235 which puts the iterator at the beginning of the next line, in
19236 the logical order. */
19237 row->maxpos = it->current.pos;
19238 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19239 /* A line that is entirely from a string/image/stretch... */
19240 row->maxpos = row->minpos;
19241 else
19242 emacs_abort ();
19244 else
19245 row->maxpos = it->current.pos;
19248 /* Construct the glyph row IT->glyph_row in the desired matrix of
19249 IT->w from text at the current position of IT. See dispextern.h
19250 for an overview of struct it. Value is non-zero if
19251 IT->glyph_row displays text, as opposed to a line displaying ZV
19252 only. */
19254 static int
19255 display_line (struct it *it)
19257 struct glyph_row *row = it->glyph_row;
19258 Lisp_Object overlay_arrow_string;
19259 struct it wrap_it;
19260 void *wrap_data = NULL;
19261 int may_wrap = 0, wrap_x IF_LINT (= 0);
19262 int wrap_row_used = -1;
19263 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19264 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19265 int wrap_row_extra_line_spacing IF_LINT (= 0);
19266 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19267 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19268 int cvpos;
19269 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19270 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19272 /* We always start displaying at hpos zero even if hscrolled. */
19273 eassert (it->hpos == 0 && it->current_x == 0);
19275 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19276 >= it->w->desired_matrix->nrows)
19278 it->w->nrows_scale_factor++;
19279 it->f->fonts_changed = 1;
19280 return 0;
19283 /* Is IT->w showing the region? */
19284 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19286 /* Clear the result glyph row and enable it. */
19287 prepare_desired_row (row);
19289 row->y = it->current_y;
19290 row->start = it->start;
19291 row->continuation_lines_width = it->continuation_lines_width;
19292 row->displays_text_p = 1;
19293 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19294 it->starts_in_middle_of_char_p = 0;
19296 /* Arrange the overlays nicely for our purposes. Usually, we call
19297 display_line on only one line at a time, in which case this
19298 can't really hurt too much, or we call it on lines which appear
19299 one after another in the buffer, in which case all calls to
19300 recenter_overlay_lists but the first will be pretty cheap. */
19301 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19303 /* Move over display elements that are not visible because we are
19304 hscrolled. This may stop at an x-position < IT->first_visible_x
19305 if the first glyph is partially visible or if we hit a line end. */
19306 if (it->current_x < it->first_visible_x)
19308 enum move_it_result move_result;
19310 this_line_min_pos = row->start.pos;
19311 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19312 MOVE_TO_POS | MOVE_TO_X);
19313 /* If we are under a large hscroll, move_it_in_display_line_to
19314 could hit the end of the line without reaching
19315 it->first_visible_x. Pretend that we did reach it. This is
19316 especially important on a TTY, where we will call
19317 extend_face_to_end_of_line, which needs to know how many
19318 blank glyphs to produce. */
19319 if (it->current_x < it->first_visible_x
19320 && (move_result == MOVE_NEWLINE_OR_CR
19321 || move_result == MOVE_POS_MATCH_OR_ZV))
19322 it->current_x = it->first_visible_x;
19324 /* Record the smallest positions seen while we moved over
19325 display elements that are not visible. This is needed by
19326 redisplay_internal for optimizing the case where the cursor
19327 stays inside the same line. The rest of this function only
19328 considers positions that are actually displayed, so
19329 RECORD_MAX_MIN_POS will not otherwise record positions that
19330 are hscrolled to the left of the left edge of the window. */
19331 min_pos = CHARPOS (this_line_min_pos);
19332 min_bpos = BYTEPOS (this_line_min_pos);
19334 else
19336 /* We only do this when not calling `move_it_in_display_line_to'
19337 above, because move_it_in_display_line_to calls
19338 handle_line_prefix itself. */
19339 handle_line_prefix (it);
19342 /* Get the initial row height. This is either the height of the
19343 text hscrolled, if there is any, or zero. */
19344 row->ascent = it->max_ascent;
19345 row->height = it->max_ascent + it->max_descent;
19346 row->phys_ascent = it->max_phys_ascent;
19347 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19348 row->extra_line_spacing = it->max_extra_line_spacing;
19350 /* Utility macro to record max and min buffer positions seen until now. */
19351 #define RECORD_MAX_MIN_POS(IT) \
19352 do \
19354 int composition_p = !STRINGP ((IT)->string) \
19355 && ((IT)->what == IT_COMPOSITION); \
19356 ptrdiff_t current_pos = \
19357 composition_p ? (IT)->cmp_it.charpos \
19358 : IT_CHARPOS (*(IT)); \
19359 ptrdiff_t current_bpos = \
19360 composition_p ? CHAR_TO_BYTE (current_pos) \
19361 : IT_BYTEPOS (*(IT)); \
19362 if (current_pos < min_pos) \
19364 min_pos = current_pos; \
19365 min_bpos = current_bpos; \
19367 if (IT_CHARPOS (*it) > max_pos) \
19369 max_pos = IT_CHARPOS (*it); \
19370 max_bpos = IT_BYTEPOS (*it); \
19373 while (0)
19375 /* Loop generating characters. The loop is left with IT on the next
19376 character to display. */
19377 while (1)
19379 int n_glyphs_before, hpos_before, x_before;
19380 int x, nglyphs;
19381 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19383 /* Retrieve the next thing to display. Value is zero if end of
19384 buffer reached. */
19385 if (!get_next_display_element (it))
19387 /* Maybe add a space at the end of this line that is used to
19388 display the cursor there under X. Set the charpos of the
19389 first glyph of blank lines not corresponding to any text
19390 to -1. */
19391 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19392 row->exact_window_width_line_p = 1;
19393 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19394 || row->used[TEXT_AREA] == 0)
19396 row->glyphs[TEXT_AREA]->charpos = -1;
19397 row->displays_text_p = 0;
19399 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19400 && (!MINI_WINDOW_P (it->w)
19401 || (minibuf_level && EQ (it->window, minibuf_window))))
19402 row->indicate_empty_line_p = 1;
19405 it->continuation_lines_width = 0;
19406 row->ends_at_zv_p = 1;
19407 /* A row that displays right-to-left text must always have
19408 its last face extended all the way to the end of line,
19409 even if this row ends in ZV, because we still write to
19410 the screen left to right. We also need to extend the
19411 last face if the default face is remapped to some
19412 different face, otherwise the functions that clear
19413 portions of the screen will clear with the default face's
19414 background color. */
19415 if (row->reversed_p
19416 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19417 extend_face_to_end_of_line (it);
19418 break;
19421 /* Now, get the metrics of what we want to display. This also
19422 generates glyphs in `row' (which is IT->glyph_row). */
19423 n_glyphs_before = row->used[TEXT_AREA];
19424 x = it->current_x;
19426 /* Remember the line height so far in case the next element doesn't
19427 fit on the line. */
19428 if (it->line_wrap != TRUNCATE)
19430 ascent = it->max_ascent;
19431 descent = it->max_descent;
19432 phys_ascent = it->max_phys_ascent;
19433 phys_descent = it->max_phys_descent;
19435 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19437 if (IT_DISPLAYING_WHITESPACE (it))
19438 may_wrap = 1;
19439 else if (may_wrap)
19441 SAVE_IT (wrap_it, *it, wrap_data);
19442 wrap_x = x;
19443 wrap_row_used = row->used[TEXT_AREA];
19444 wrap_row_ascent = row->ascent;
19445 wrap_row_height = row->height;
19446 wrap_row_phys_ascent = row->phys_ascent;
19447 wrap_row_phys_height = row->phys_height;
19448 wrap_row_extra_line_spacing = row->extra_line_spacing;
19449 wrap_row_min_pos = min_pos;
19450 wrap_row_min_bpos = min_bpos;
19451 wrap_row_max_pos = max_pos;
19452 wrap_row_max_bpos = max_bpos;
19453 may_wrap = 0;
19458 PRODUCE_GLYPHS (it);
19460 /* If this display element was in marginal areas, continue with
19461 the next one. */
19462 if (it->area != TEXT_AREA)
19464 row->ascent = max (row->ascent, it->max_ascent);
19465 row->height = max (row->height, it->max_ascent + it->max_descent);
19466 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19467 row->phys_height = max (row->phys_height,
19468 it->max_phys_ascent + it->max_phys_descent);
19469 row->extra_line_spacing = max (row->extra_line_spacing,
19470 it->max_extra_line_spacing);
19471 set_iterator_to_next (it, 1);
19472 continue;
19475 /* Does the display element fit on the line? If we truncate
19476 lines, we should draw past the right edge of the window. If
19477 we don't truncate, we want to stop so that we can display the
19478 continuation glyph before the right margin. If lines are
19479 continued, there are two possible strategies for characters
19480 resulting in more than 1 glyph (e.g. tabs): Display as many
19481 glyphs as possible in this line and leave the rest for the
19482 continuation line, or display the whole element in the next
19483 line. Original redisplay did the former, so we do it also. */
19484 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19485 hpos_before = it->hpos;
19486 x_before = x;
19488 if (/* Not a newline. */
19489 nglyphs > 0
19490 /* Glyphs produced fit entirely in the line. */
19491 && it->current_x < it->last_visible_x)
19493 it->hpos += nglyphs;
19494 row->ascent = max (row->ascent, it->max_ascent);
19495 row->height = max (row->height, it->max_ascent + it->max_descent);
19496 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19497 row->phys_height = max (row->phys_height,
19498 it->max_phys_ascent + it->max_phys_descent);
19499 row->extra_line_spacing = max (row->extra_line_spacing,
19500 it->max_extra_line_spacing);
19501 if (it->current_x - it->pixel_width < it->first_visible_x)
19502 row->x = x - it->first_visible_x;
19503 /* Record the maximum and minimum buffer positions seen so
19504 far in glyphs that will be displayed by this row. */
19505 if (it->bidi_p)
19506 RECORD_MAX_MIN_POS (it);
19508 else
19510 int i, new_x;
19511 struct glyph *glyph;
19513 for (i = 0; i < nglyphs; ++i, x = new_x)
19515 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19516 new_x = x + glyph->pixel_width;
19518 if (/* Lines are continued. */
19519 it->line_wrap != TRUNCATE
19520 && (/* Glyph doesn't fit on the line. */
19521 new_x > it->last_visible_x
19522 /* Or it fits exactly on a window system frame. */
19523 || (new_x == it->last_visible_x
19524 && FRAME_WINDOW_P (it->f)
19525 && (row->reversed_p
19526 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19527 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19529 /* End of a continued line. */
19531 if (it->hpos == 0
19532 || (new_x == it->last_visible_x
19533 && FRAME_WINDOW_P (it->f)
19534 && (row->reversed_p
19535 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19536 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19538 /* Current glyph is the only one on the line or
19539 fits exactly on the line. We must continue
19540 the line because we can't draw the cursor
19541 after the glyph. */
19542 row->continued_p = 1;
19543 it->current_x = new_x;
19544 it->continuation_lines_width += new_x;
19545 ++it->hpos;
19546 if (i == nglyphs - 1)
19548 /* If line-wrap is on, check if a previous
19549 wrap point was found. */
19550 if (wrap_row_used > 0
19551 /* Even if there is a previous wrap
19552 point, continue the line here as
19553 usual, if (i) the previous character
19554 was a space or tab AND (ii) the
19555 current character is not. */
19556 && (!may_wrap
19557 || IT_DISPLAYING_WHITESPACE (it)))
19558 goto back_to_wrap;
19560 /* Record the maximum and minimum buffer
19561 positions seen so far in glyphs that will be
19562 displayed by this row. */
19563 if (it->bidi_p)
19564 RECORD_MAX_MIN_POS (it);
19565 set_iterator_to_next (it, 1);
19566 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19568 if (!get_next_display_element (it))
19570 row->exact_window_width_line_p = 1;
19571 it->continuation_lines_width = 0;
19572 row->continued_p = 0;
19573 row->ends_at_zv_p = 1;
19575 else if (ITERATOR_AT_END_OF_LINE_P (it))
19577 row->continued_p = 0;
19578 row->exact_window_width_line_p = 1;
19582 else if (it->bidi_p)
19583 RECORD_MAX_MIN_POS (it);
19585 else if (CHAR_GLYPH_PADDING_P (*glyph)
19586 && !FRAME_WINDOW_P (it->f))
19588 /* A padding glyph that doesn't fit on this line.
19589 This means the whole character doesn't fit
19590 on the line. */
19591 if (row->reversed_p)
19592 unproduce_glyphs (it, row->used[TEXT_AREA]
19593 - n_glyphs_before);
19594 row->used[TEXT_AREA] = n_glyphs_before;
19596 /* Fill the rest of the row with continuation
19597 glyphs like in 20.x. */
19598 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19599 < row->glyphs[1 + TEXT_AREA])
19600 produce_special_glyphs (it, IT_CONTINUATION);
19602 row->continued_p = 1;
19603 it->current_x = x_before;
19604 it->continuation_lines_width += x_before;
19606 /* Restore the height to what it was before the
19607 element not fitting on the line. */
19608 it->max_ascent = ascent;
19609 it->max_descent = descent;
19610 it->max_phys_ascent = phys_ascent;
19611 it->max_phys_descent = phys_descent;
19613 else if (wrap_row_used > 0)
19615 back_to_wrap:
19616 if (row->reversed_p)
19617 unproduce_glyphs (it,
19618 row->used[TEXT_AREA] - wrap_row_used);
19619 RESTORE_IT (it, &wrap_it, wrap_data);
19620 it->continuation_lines_width += wrap_x;
19621 row->used[TEXT_AREA] = wrap_row_used;
19622 row->ascent = wrap_row_ascent;
19623 row->height = wrap_row_height;
19624 row->phys_ascent = wrap_row_phys_ascent;
19625 row->phys_height = wrap_row_phys_height;
19626 row->extra_line_spacing = wrap_row_extra_line_spacing;
19627 min_pos = wrap_row_min_pos;
19628 min_bpos = wrap_row_min_bpos;
19629 max_pos = wrap_row_max_pos;
19630 max_bpos = wrap_row_max_bpos;
19631 row->continued_p = 1;
19632 row->ends_at_zv_p = 0;
19633 row->exact_window_width_line_p = 0;
19634 it->continuation_lines_width += x;
19636 /* Make sure that a non-default face is extended
19637 up to the right margin of the window. */
19638 extend_face_to_end_of_line (it);
19640 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19642 /* A TAB that extends past the right edge of the
19643 window. This produces a single glyph on
19644 window system frames. We leave the glyph in
19645 this row and let it fill the row, but don't
19646 consume the TAB. */
19647 if ((row->reversed_p
19648 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19649 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19650 produce_special_glyphs (it, IT_CONTINUATION);
19651 it->continuation_lines_width += it->last_visible_x;
19652 row->ends_in_middle_of_char_p = 1;
19653 row->continued_p = 1;
19654 glyph->pixel_width = it->last_visible_x - x;
19655 it->starts_in_middle_of_char_p = 1;
19657 else
19659 /* Something other than a TAB that draws past
19660 the right edge of the window. Restore
19661 positions to values before the element. */
19662 if (row->reversed_p)
19663 unproduce_glyphs (it, row->used[TEXT_AREA]
19664 - (n_glyphs_before + i));
19665 row->used[TEXT_AREA] = n_glyphs_before + i;
19667 /* Display continuation glyphs. */
19668 it->current_x = x_before;
19669 it->continuation_lines_width += x;
19670 if (!FRAME_WINDOW_P (it->f)
19671 || (row->reversed_p
19672 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19673 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19674 produce_special_glyphs (it, IT_CONTINUATION);
19675 row->continued_p = 1;
19677 extend_face_to_end_of_line (it);
19679 if (nglyphs > 1 && i > 0)
19681 row->ends_in_middle_of_char_p = 1;
19682 it->starts_in_middle_of_char_p = 1;
19685 /* Restore the height to what it was before the
19686 element not fitting on the line. */
19687 it->max_ascent = ascent;
19688 it->max_descent = descent;
19689 it->max_phys_ascent = phys_ascent;
19690 it->max_phys_descent = phys_descent;
19693 break;
19695 else if (new_x > it->first_visible_x)
19697 /* Increment number of glyphs actually displayed. */
19698 ++it->hpos;
19700 /* Record the maximum and minimum buffer positions
19701 seen so far in glyphs that will be displayed by
19702 this row. */
19703 if (it->bidi_p)
19704 RECORD_MAX_MIN_POS (it);
19706 if (x < it->first_visible_x)
19707 /* Glyph is partially visible, i.e. row starts at
19708 negative X position. */
19709 row->x = x - it->first_visible_x;
19711 else
19713 /* Glyph is completely off the left margin of the
19714 window. This should not happen because of the
19715 move_it_in_display_line at the start of this
19716 function, unless the text display area of the
19717 window is empty. */
19718 eassert (it->first_visible_x <= it->last_visible_x);
19721 /* Even if this display element produced no glyphs at all,
19722 we want to record its position. */
19723 if (it->bidi_p && nglyphs == 0)
19724 RECORD_MAX_MIN_POS (it);
19726 row->ascent = max (row->ascent, it->max_ascent);
19727 row->height = max (row->height, it->max_ascent + it->max_descent);
19728 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19729 row->phys_height = max (row->phys_height,
19730 it->max_phys_ascent + it->max_phys_descent);
19731 row->extra_line_spacing = max (row->extra_line_spacing,
19732 it->max_extra_line_spacing);
19734 /* End of this display line if row is continued. */
19735 if (row->continued_p || row->ends_at_zv_p)
19736 break;
19739 at_end_of_line:
19740 /* Is this a line end? If yes, we're also done, after making
19741 sure that a non-default face is extended up to the right
19742 margin of the window. */
19743 if (ITERATOR_AT_END_OF_LINE_P (it))
19745 int used_before = row->used[TEXT_AREA];
19747 row->ends_in_newline_from_string_p = STRINGP (it->object);
19749 /* Add a space at the end of the line that is used to
19750 display the cursor there. */
19751 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19752 append_space_for_newline (it, 0);
19754 /* Extend the face to the end of the line. */
19755 extend_face_to_end_of_line (it);
19757 /* Make sure we have the position. */
19758 if (used_before == 0)
19759 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19761 /* Record the position of the newline, for use in
19762 find_row_edges. */
19763 it->eol_pos = it->current.pos;
19765 /* Consume the line end. This skips over invisible lines. */
19766 set_iterator_to_next (it, 1);
19767 it->continuation_lines_width = 0;
19768 break;
19771 /* Proceed with next display element. Note that this skips
19772 over lines invisible because of selective display. */
19773 set_iterator_to_next (it, 1);
19775 /* If we truncate lines, we are done when the last displayed
19776 glyphs reach past the right margin of the window. */
19777 if (it->line_wrap == TRUNCATE
19778 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19779 ? (it->current_x >= it->last_visible_x)
19780 : (it->current_x > it->last_visible_x)))
19782 /* Maybe add truncation glyphs. */
19783 if (!FRAME_WINDOW_P (it->f)
19784 || (row->reversed_p
19785 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19786 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19788 int i, n;
19790 if (!row->reversed_p)
19792 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19793 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19794 break;
19796 else
19798 for (i = 0; i < row->used[TEXT_AREA]; i++)
19799 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19800 break;
19801 /* Remove any padding glyphs at the front of ROW, to
19802 make room for the truncation glyphs we will be
19803 adding below. The loop below always inserts at
19804 least one truncation glyph, so also remove the
19805 last glyph added to ROW. */
19806 unproduce_glyphs (it, i + 1);
19807 /* Adjust i for the loop below. */
19808 i = row->used[TEXT_AREA] - (i + 1);
19811 it->current_x = x_before;
19812 if (!FRAME_WINDOW_P (it->f))
19814 for (n = row->used[TEXT_AREA]; i < n; ++i)
19816 row->used[TEXT_AREA] = i;
19817 produce_special_glyphs (it, IT_TRUNCATION);
19820 else
19822 row->used[TEXT_AREA] = i;
19823 produce_special_glyphs (it, IT_TRUNCATION);
19826 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19828 /* Don't truncate if we can overflow newline into fringe. */
19829 if (!get_next_display_element (it))
19831 it->continuation_lines_width = 0;
19832 row->ends_at_zv_p = 1;
19833 row->exact_window_width_line_p = 1;
19834 break;
19836 if (ITERATOR_AT_END_OF_LINE_P (it))
19838 row->exact_window_width_line_p = 1;
19839 goto at_end_of_line;
19841 it->current_x = x_before;
19844 row->truncated_on_right_p = 1;
19845 it->continuation_lines_width = 0;
19846 reseat_at_next_visible_line_start (it, 0);
19847 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19848 it->hpos = hpos_before;
19849 break;
19853 if (wrap_data)
19854 bidi_unshelve_cache (wrap_data, 1);
19856 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19857 at the left window margin. */
19858 if (it->first_visible_x
19859 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19861 if (!FRAME_WINDOW_P (it->f)
19862 || (row->reversed_p
19863 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19864 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19865 insert_left_trunc_glyphs (it);
19866 row->truncated_on_left_p = 1;
19869 /* Remember the position at which this line ends.
19871 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19872 cannot be before the call to find_row_edges below, since that is
19873 where these positions are determined. */
19874 row->end = it->current;
19875 if (!it->bidi_p)
19877 row->minpos = row->start.pos;
19878 row->maxpos = row->end.pos;
19880 else
19882 /* ROW->minpos and ROW->maxpos must be the smallest and
19883 `1 + the largest' buffer positions in ROW. But if ROW was
19884 bidi-reordered, these two positions can be anywhere in the
19885 row, so we must determine them now. */
19886 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19889 /* If the start of this line is the overlay arrow-position, then
19890 mark this glyph row as the one containing the overlay arrow.
19891 This is clearly a mess with variable size fonts. It would be
19892 better to let it be displayed like cursors under X. */
19893 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19894 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19895 !NILP (overlay_arrow_string)))
19897 /* Overlay arrow in window redisplay is a fringe bitmap. */
19898 if (STRINGP (overlay_arrow_string))
19900 struct glyph_row *arrow_row
19901 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19902 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19903 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19904 struct glyph *p = row->glyphs[TEXT_AREA];
19905 struct glyph *p2, *end;
19907 /* Copy the arrow glyphs. */
19908 while (glyph < arrow_end)
19909 *p++ = *glyph++;
19911 /* Throw away padding glyphs. */
19912 p2 = p;
19913 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19914 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19915 ++p2;
19916 if (p2 > p)
19918 while (p2 < end)
19919 *p++ = *p2++;
19920 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19923 else
19925 eassert (INTEGERP (overlay_arrow_string));
19926 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19928 overlay_arrow_seen = 1;
19931 /* Highlight trailing whitespace. */
19932 if (!NILP (Vshow_trailing_whitespace))
19933 highlight_trailing_whitespace (it->f, it->glyph_row);
19935 /* Compute pixel dimensions of this line. */
19936 compute_line_metrics (it);
19938 /* Implementation note: No changes in the glyphs of ROW or in their
19939 faces can be done past this point, because compute_line_metrics
19940 computes ROW's hash value and stores it within the glyph_row
19941 structure. */
19943 /* Record whether this row ends inside an ellipsis. */
19944 row->ends_in_ellipsis_p
19945 = (it->method == GET_FROM_DISPLAY_VECTOR
19946 && it->ellipsis_p);
19948 /* Save fringe bitmaps in this row. */
19949 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19950 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19951 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19952 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19954 it->left_user_fringe_bitmap = 0;
19955 it->left_user_fringe_face_id = 0;
19956 it->right_user_fringe_bitmap = 0;
19957 it->right_user_fringe_face_id = 0;
19959 /* Maybe set the cursor. */
19960 cvpos = it->w->cursor.vpos;
19961 if ((cvpos < 0
19962 /* In bidi-reordered rows, keep checking for proper cursor
19963 position even if one has been found already, because buffer
19964 positions in such rows change non-linearly with ROW->VPOS,
19965 when a line is continued. One exception: when we are at ZV,
19966 display cursor on the first suitable glyph row, since all
19967 the empty rows after that also have their position set to ZV. */
19968 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19969 lines' rows is implemented for bidi-reordered rows. */
19970 || (it->bidi_p
19971 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19972 && PT >= MATRIX_ROW_START_CHARPOS (row)
19973 && PT <= MATRIX_ROW_END_CHARPOS (row)
19974 && cursor_row_p (row))
19975 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19977 /* Prepare for the next line. This line starts horizontally at (X
19978 HPOS) = (0 0). Vertical positions are incremented. As a
19979 convenience for the caller, IT->glyph_row is set to the next
19980 row to be used. */
19981 it->current_x = it->hpos = 0;
19982 it->current_y += row->height;
19983 SET_TEXT_POS (it->eol_pos, 0, 0);
19984 ++it->vpos;
19985 ++it->glyph_row;
19986 /* The next row should by default use the same value of the
19987 reversed_p flag as this one. set_iterator_to_next decides when
19988 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19989 the flag accordingly. */
19990 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19991 it->glyph_row->reversed_p = row->reversed_p;
19992 it->start = row->end;
19993 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19995 #undef RECORD_MAX_MIN_POS
19998 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19999 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20000 doc: /* Return paragraph direction at point in BUFFER.
20001 Value is either `left-to-right' or `right-to-left'.
20002 If BUFFER is omitted or nil, it defaults to the current buffer.
20004 Paragraph direction determines how the text in the paragraph is displayed.
20005 In left-to-right paragraphs, text begins at the left margin of the window
20006 and the reading direction is generally left to right. In right-to-left
20007 paragraphs, text begins at the right margin and is read from right to left.
20009 See also `bidi-paragraph-direction'. */)
20010 (Lisp_Object buffer)
20012 struct buffer *buf = current_buffer;
20013 struct buffer *old = buf;
20015 if (! NILP (buffer))
20017 CHECK_BUFFER (buffer);
20018 buf = XBUFFER (buffer);
20021 if (NILP (BVAR (buf, bidi_display_reordering))
20022 || NILP (BVAR (buf, enable_multibyte_characters))
20023 /* When we are loading loadup.el, the character property tables
20024 needed for bidi iteration are not yet available. */
20025 || !NILP (Vpurify_flag))
20026 return Qleft_to_right;
20027 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20028 return BVAR (buf, bidi_paragraph_direction);
20029 else
20031 /* Determine the direction from buffer text. We could try to
20032 use current_matrix if it is up to date, but this seems fast
20033 enough as it is. */
20034 struct bidi_it itb;
20035 ptrdiff_t pos = BUF_PT (buf);
20036 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20037 int c;
20038 void *itb_data = bidi_shelve_cache ();
20040 set_buffer_temp (buf);
20041 /* bidi_paragraph_init finds the base direction of the paragraph
20042 by searching forward from paragraph start. We need the base
20043 direction of the current or _previous_ paragraph, so we need
20044 to make sure we are within that paragraph. To that end, find
20045 the previous non-empty line. */
20046 if (pos >= ZV && pos > BEGV)
20047 DEC_BOTH (pos, bytepos);
20048 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20049 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20051 while ((c = FETCH_BYTE (bytepos)) == '\n'
20052 || c == ' ' || c == '\t' || c == '\f')
20054 if (bytepos <= BEGV_BYTE)
20055 break;
20056 bytepos--;
20057 pos--;
20059 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20060 bytepos--;
20062 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20063 itb.paragraph_dir = NEUTRAL_DIR;
20064 itb.string.s = NULL;
20065 itb.string.lstring = Qnil;
20066 itb.string.bufpos = 0;
20067 itb.string.unibyte = 0;
20068 /* We have no window to use here for ignoring window-specific
20069 overlays. Using NULL for window pointer will cause
20070 compute_display_string_pos to use the current buffer. */
20071 itb.w = NULL;
20072 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20073 bidi_unshelve_cache (itb_data, 0);
20074 set_buffer_temp (old);
20075 switch (itb.paragraph_dir)
20077 case L2R:
20078 return Qleft_to_right;
20079 break;
20080 case R2L:
20081 return Qright_to_left;
20082 break;
20083 default:
20084 emacs_abort ();
20089 DEFUN ("move-point-visually", Fmove_point_visually,
20090 Smove_point_visually, 1, 1, 0,
20091 doc: /* Move point in the visual order in the specified DIRECTION.
20092 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20093 left.
20095 Value is the new character position of point. */)
20096 (Lisp_Object direction)
20098 struct window *w = XWINDOW (selected_window);
20099 struct buffer *b = XBUFFER (w->contents);
20100 struct glyph_row *row;
20101 int dir;
20102 Lisp_Object paragraph_dir;
20104 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20105 (!(ROW)->continued_p \
20106 && INTEGERP ((GLYPH)->object) \
20107 && (GLYPH)->type == CHAR_GLYPH \
20108 && (GLYPH)->u.ch == ' ' \
20109 && (GLYPH)->charpos >= 0 \
20110 && !(GLYPH)->avoid_cursor_p)
20112 CHECK_NUMBER (direction);
20113 dir = XINT (direction);
20114 if (dir > 0)
20115 dir = 1;
20116 else
20117 dir = -1;
20119 /* If current matrix is up-to-date, we can use the information
20120 recorded in the glyphs, at least as long as the goal is on the
20121 screen. */
20122 if (w->window_end_valid
20123 && !windows_or_buffers_changed
20124 && b
20125 && !b->clip_changed
20126 && !b->prevent_redisplay_optimizations_p
20127 && !window_outdated (w)
20128 && w->cursor.vpos >= 0
20129 && w->cursor.vpos < w->current_matrix->nrows
20130 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20132 struct glyph *g = row->glyphs[TEXT_AREA];
20133 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20134 struct glyph *gpt = g + w->cursor.hpos;
20136 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20138 if (BUFFERP (g->object) && g->charpos != PT)
20140 SET_PT (g->charpos);
20141 w->cursor.vpos = -1;
20142 return make_number (PT);
20144 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20146 ptrdiff_t new_pos;
20148 if (BUFFERP (gpt->object))
20150 new_pos = PT;
20151 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20152 new_pos += (row->reversed_p ? -dir : dir);
20153 else
20154 new_pos -= (row->reversed_p ? -dir : dir);;
20156 else if (BUFFERP (g->object))
20157 new_pos = g->charpos;
20158 else
20159 break;
20160 SET_PT (new_pos);
20161 w->cursor.vpos = -1;
20162 return make_number (PT);
20164 else if (ROW_GLYPH_NEWLINE_P (row, g))
20166 /* Glyphs inserted at the end of a non-empty line for
20167 positioning the cursor have zero charpos, so we must
20168 deduce the value of point by other means. */
20169 if (g->charpos > 0)
20170 SET_PT (g->charpos);
20171 else if (row->ends_at_zv_p && PT != ZV)
20172 SET_PT (ZV);
20173 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20174 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20175 else
20176 break;
20177 w->cursor.vpos = -1;
20178 return make_number (PT);
20181 if (g == e || INTEGERP (g->object))
20183 if (row->truncated_on_left_p || row->truncated_on_right_p)
20184 goto simulate_display;
20185 if (!row->reversed_p)
20186 row += dir;
20187 else
20188 row -= dir;
20189 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20190 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20191 goto simulate_display;
20193 if (dir > 0)
20195 if (row->reversed_p && !row->continued_p)
20197 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20198 w->cursor.vpos = -1;
20199 return make_number (PT);
20201 g = row->glyphs[TEXT_AREA];
20202 e = g + row->used[TEXT_AREA];
20203 for ( ; g < e; g++)
20205 if (BUFFERP (g->object)
20206 /* Empty lines have only one glyph, which stands
20207 for the newline, and whose charpos is the
20208 buffer position of the newline. */
20209 || ROW_GLYPH_NEWLINE_P (row, g)
20210 /* When the buffer ends in a newline, the line at
20211 EOB also has one glyph, but its charpos is -1. */
20212 || (row->ends_at_zv_p
20213 && !row->reversed_p
20214 && INTEGERP (g->object)
20215 && g->type == CHAR_GLYPH
20216 && g->u.ch == ' '))
20218 if (g->charpos > 0)
20219 SET_PT (g->charpos);
20220 else if (!row->reversed_p
20221 && row->ends_at_zv_p
20222 && PT != ZV)
20223 SET_PT (ZV);
20224 else
20225 continue;
20226 w->cursor.vpos = -1;
20227 return make_number (PT);
20231 else
20233 if (!row->reversed_p && !row->continued_p)
20235 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20236 w->cursor.vpos = -1;
20237 return make_number (PT);
20239 e = row->glyphs[TEXT_AREA];
20240 g = e + row->used[TEXT_AREA] - 1;
20241 for ( ; g >= e; g--)
20243 if (BUFFERP (g->object)
20244 || (ROW_GLYPH_NEWLINE_P (row, g)
20245 && g->charpos > 0)
20246 /* Empty R2L lines on GUI frames have the buffer
20247 position of the newline stored in the stretch
20248 glyph. */
20249 || g->type == STRETCH_GLYPH
20250 || (row->ends_at_zv_p
20251 && row->reversed_p
20252 && INTEGERP (g->object)
20253 && g->type == CHAR_GLYPH
20254 && g->u.ch == ' '))
20256 if (g->charpos > 0)
20257 SET_PT (g->charpos);
20258 else if (row->reversed_p
20259 && row->ends_at_zv_p
20260 && PT != ZV)
20261 SET_PT (ZV);
20262 else
20263 continue;
20264 w->cursor.vpos = -1;
20265 return make_number (PT);
20272 simulate_display:
20274 /* If we wind up here, we failed to move by using the glyphs, so we
20275 need to simulate display instead. */
20277 if (b)
20278 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20279 else
20280 paragraph_dir = Qleft_to_right;
20281 if (EQ (paragraph_dir, Qright_to_left))
20282 dir = -dir;
20283 if (PT <= BEGV && dir < 0)
20284 xsignal0 (Qbeginning_of_buffer);
20285 else if (PT >= ZV && dir > 0)
20286 xsignal0 (Qend_of_buffer);
20287 else
20289 struct text_pos pt;
20290 struct it it;
20291 int pt_x, target_x, pixel_width, pt_vpos;
20292 bool at_eol_p;
20293 bool overshoot_expected = false;
20294 bool target_is_eol_p = false;
20296 /* Setup the arena. */
20297 SET_TEXT_POS (pt, PT, PT_BYTE);
20298 start_display (&it, w, pt);
20300 if (it.cmp_it.id < 0
20301 && it.method == GET_FROM_STRING
20302 && it.area == TEXT_AREA
20303 && it.string_from_display_prop_p
20304 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20305 overshoot_expected = true;
20307 /* Find the X coordinate of point. We start from the beginning
20308 of this or previous line to make sure we are before point in
20309 the logical order (since the move_it_* functions can only
20310 move forward). */
20311 reseat_at_previous_visible_line_start (&it);
20312 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20313 if (IT_CHARPOS (it) != PT)
20314 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20315 -1, -1, -1, MOVE_TO_POS);
20316 pt_x = it.current_x;
20317 pt_vpos = it.vpos;
20318 if (dir > 0 || overshoot_expected)
20320 struct glyph_row *row = it.glyph_row;
20322 /* When point is at beginning of line, we don't have
20323 information about the glyph there loaded into struct
20324 it. Calling get_next_display_element fixes that. */
20325 if (pt_x == 0)
20326 get_next_display_element (&it);
20327 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20328 it.glyph_row = NULL;
20329 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20330 it.glyph_row = row;
20331 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20332 it, lest it will become out of sync with it's buffer
20333 position. */
20334 it.current_x = pt_x;
20336 else
20337 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20338 pixel_width = it.pixel_width;
20339 if (overshoot_expected && at_eol_p)
20340 pixel_width = 0;
20341 else if (pixel_width <= 0)
20342 pixel_width = 1;
20344 /* If there's a display string at point, we are actually at the
20345 glyph to the left of point, so we need to correct the X
20346 coordinate. */
20347 if (overshoot_expected)
20348 pt_x += pixel_width;
20350 /* Compute target X coordinate, either to the left or to the
20351 right of point. On TTY frames, all characters have the same
20352 pixel width of 1, so we can use that. On GUI frames we don't
20353 have an easy way of getting at the pixel width of the
20354 character to the left of point, so we use a different method
20355 of getting to that place. */
20356 if (dir > 0)
20357 target_x = pt_x + pixel_width;
20358 else
20359 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20361 /* Target X coordinate could be one line above or below the line
20362 of point, in which case we need to adjust the target X
20363 coordinate. Also, if moving to the left, we need to begin at
20364 the left edge of the point's screen line. */
20365 if (dir < 0)
20367 if (pt_x > 0)
20369 start_display (&it, w, pt);
20370 reseat_at_previous_visible_line_start (&it);
20371 it.current_x = it.current_y = it.hpos = 0;
20372 if (pt_vpos != 0)
20373 move_it_by_lines (&it, pt_vpos);
20375 else
20377 move_it_by_lines (&it, -1);
20378 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20379 target_is_eol_p = true;
20382 else
20384 if (at_eol_p
20385 || (target_x >= it.last_visible_x
20386 && it.line_wrap != TRUNCATE))
20388 if (pt_x > 0)
20389 move_it_by_lines (&it, 0);
20390 move_it_by_lines (&it, 1);
20391 target_x = 0;
20395 /* Move to the target X coordinate. */
20396 #ifdef HAVE_WINDOW_SYSTEM
20397 /* On GUI frames, as we don't know the X coordinate of the
20398 character to the left of point, moving point to the left
20399 requires walking, one grapheme cluster at a time, until we
20400 find ourself at a place immediately to the left of the
20401 character at point. */
20402 if (FRAME_WINDOW_P (it.f) && dir < 0)
20404 struct text_pos new_pos = it.current.pos;
20405 enum move_it_result rc = MOVE_X_REACHED;
20407 while (it.current_x + it.pixel_width <= target_x
20408 && rc == MOVE_X_REACHED)
20410 int new_x = it.current_x + it.pixel_width;
20412 new_pos = it.current.pos;
20413 if (new_x == it.current_x)
20414 new_x++;
20415 rc = move_it_in_display_line_to (&it, ZV, new_x,
20416 MOVE_TO_POS | MOVE_TO_X);
20417 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20418 break;
20420 /* If we ended up on a composed character inside
20421 bidi-reordered text (e.g., Hebrew text with diacritics),
20422 the iterator gives us the buffer position of the last (in
20423 logical order) character of the composed grapheme cluster,
20424 which is not what we want. So we cheat: we compute the
20425 character position of the character that follows (in the
20426 logical order) the one where the above loop stopped. That
20427 character will appear on display to the left of point. */
20428 if (it.bidi_p
20429 && it.bidi_it.scan_dir == -1
20430 && new_pos.charpos - IT_CHARPOS (it) > 1)
20432 new_pos.charpos = IT_CHARPOS (it) + 1;
20433 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20435 it.current.pos = new_pos;
20437 else
20438 #endif
20439 if (it.current_x != target_x)
20440 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20442 /* When lines are truncated, the above loop will stop at the
20443 window edge. But we want to get to the end of line, even if
20444 it is beyond the window edge; automatic hscroll will then
20445 scroll the window to show point as appropriate. */
20446 if (target_is_eol_p && it.line_wrap == TRUNCATE
20447 && get_next_display_element (&it))
20449 struct text_pos new_pos = it.current.pos;
20451 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20453 set_iterator_to_next (&it, 0);
20454 if (it.method == GET_FROM_BUFFER)
20455 new_pos = it.current.pos;
20456 if (!get_next_display_element (&it))
20457 break;
20460 it.current.pos = new_pos;
20463 /* If we ended up in a display string that covers point, move to
20464 buffer position to the right in the visual order. */
20465 if (dir > 0)
20467 while (IT_CHARPOS (it) == PT)
20469 set_iterator_to_next (&it, 0);
20470 if (!get_next_display_element (&it))
20471 break;
20475 /* Move point to that position. */
20476 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20479 return make_number (PT);
20481 #undef ROW_GLYPH_NEWLINE_P
20485 /***********************************************************************
20486 Menu Bar
20487 ***********************************************************************/
20489 /* Redisplay the menu bar in the frame for window W.
20491 The menu bar of X frames that don't have X toolkit support is
20492 displayed in a special window W->frame->menu_bar_window.
20494 The menu bar of terminal frames is treated specially as far as
20495 glyph matrices are concerned. Menu bar lines are not part of
20496 windows, so the update is done directly on the frame matrix rows
20497 for the menu bar. */
20499 static void
20500 display_menu_bar (struct window *w)
20502 struct frame *f = XFRAME (WINDOW_FRAME (w));
20503 struct it it;
20504 Lisp_Object items;
20505 int i;
20507 /* Don't do all this for graphical frames. */
20508 #ifdef HAVE_NTGUI
20509 if (FRAME_W32_P (f))
20510 return;
20511 #endif
20512 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20513 if (FRAME_X_P (f))
20514 return;
20515 #endif
20517 #ifdef HAVE_NS
20518 if (FRAME_NS_P (f))
20519 return;
20520 #endif /* HAVE_NS */
20522 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20523 eassert (!FRAME_WINDOW_P (f));
20524 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20525 it.first_visible_x = 0;
20526 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20527 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20528 if (FRAME_WINDOW_P (f))
20530 /* Menu bar lines are displayed in the desired matrix of the
20531 dummy window menu_bar_window. */
20532 struct window *menu_w;
20533 menu_w = XWINDOW (f->menu_bar_window);
20534 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20535 MENU_FACE_ID);
20536 it.first_visible_x = 0;
20537 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20539 else
20540 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20542 /* This is a TTY frame, i.e. character hpos/vpos are used as
20543 pixel x/y. */
20544 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20545 MENU_FACE_ID);
20546 it.first_visible_x = 0;
20547 it.last_visible_x = FRAME_COLS (f);
20550 /* FIXME: This should be controlled by a user option. See the
20551 comments in redisplay_tool_bar and display_mode_line about
20552 this. */
20553 it.paragraph_embedding = L2R;
20555 /* Clear all rows of the menu bar. */
20556 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20558 struct glyph_row *row = it.glyph_row + i;
20559 clear_glyph_row (row);
20560 row->enabled_p = 1;
20561 row->full_width_p = 1;
20564 /* Display all items of the menu bar. */
20565 items = FRAME_MENU_BAR_ITEMS (it.f);
20566 for (i = 0; i < ASIZE (items); i += 4)
20568 Lisp_Object string;
20570 /* Stop at nil string. */
20571 string = AREF (items, i + 1);
20572 if (NILP (string))
20573 break;
20575 /* Remember where item was displayed. */
20576 ASET (items, i + 3, make_number (it.hpos));
20578 /* Display the item, pad with one space. */
20579 if (it.current_x < it.last_visible_x)
20580 display_string (NULL, string, Qnil, 0, 0, &it,
20581 SCHARS (string) + 1, 0, 0, -1);
20584 /* Fill out the line with spaces. */
20585 if (it.current_x < it.last_visible_x)
20586 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20588 /* Compute the total height of the lines. */
20589 compute_line_metrics (&it);
20594 /***********************************************************************
20595 Mode Line
20596 ***********************************************************************/
20598 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20599 FORCE is non-zero, redisplay mode lines unconditionally.
20600 Otherwise, redisplay only mode lines that are garbaged. Value is
20601 the number of windows whose mode lines were redisplayed. */
20603 static int
20604 redisplay_mode_lines (Lisp_Object window, int force)
20606 int nwindows = 0;
20608 while (!NILP (window))
20610 struct window *w = XWINDOW (window);
20612 if (WINDOWP (w->contents))
20613 nwindows += redisplay_mode_lines (w->contents, force);
20614 else if (force
20615 || FRAME_GARBAGED_P (XFRAME (w->frame))
20616 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20618 struct text_pos lpoint;
20619 struct buffer *old = current_buffer;
20621 /* Set the window's buffer for the mode line display. */
20622 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20623 set_buffer_internal_1 (XBUFFER (w->contents));
20625 /* Point refers normally to the selected window. For any
20626 other window, set up appropriate value. */
20627 if (!EQ (window, selected_window))
20629 struct text_pos pt;
20631 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20632 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20635 /* Display mode lines. */
20636 clear_glyph_matrix (w->desired_matrix);
20637 if (display_mode_lines (w))
20639 ++nwindows;
20640 w->must_be_updated_p = 1;
20643 /* Restore old settings. */
20644 set_buffer_internal_1 (old);
20645 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20648 window = w->next;
20651 return nwindows;
20655 /* Display the mode and/or header line of window W. Value is the
20656 sum number of mode lines and header lines displayed. */
20658 static int
20659 display_mode_lines (struct window *w)
20661 Lisp_Object old_selected_window = selected_window;
20662 Lisp_Object old_selected_frame = selected_frame;
20663 Lisp_Object new_frame = w->frame;
20664 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20665 int n = 0;
20667 selected_frame = new_frame;
20668 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20669 or window's point, then we'd need select_window_1 here as well. */
20670 XSETWINDOW (selected_window, w);
20671 XFRAME (new_frame)->selected_window = selected_window;
20673 /* These will be set while the mode line specs are processed. */
20674 line_number_displayed = 0;
20675 w->column_number_displayed = -1;
20677 if (WINDOW_WANTS_MODELINE_P (w))
20679 struct window *sel_w = XWINDOW (old_selected_window);
20681 /* Select mode line face based on the real selected window. */
20682 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20683 BVAR (current_buffer, mode_line_format));
20684 ++n;
20687 if (WINDOW_WANTS_HEADER_LINE_P (w))
20689 display_mode_line (w, HEADER_LINE_FACE_ID,
20690 BVAR (current_buffer, header_line_format));
20691 ++n;
20694 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20695 selected_frame = old_selected_frame;
20696 selected_window = old_selected_window;
20697 return n;
20701 /* Display mode or header line of window W. FACE_ID specifies which
20702 line to display; it is either MODE_LINE_FACE_ID or
20703 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20704 display. Value is the pixel height of the mode/header line
20705 displayed. */
20707 static int
20708 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20710 struct it it;
20711 struct face *face;
20712 ptrdiff_t count = SPECPDL_INDEX ();
20714 init_iterator (&it, w, -1, -1, NULL, face_id);
20715 /* Don't extend on a previously drawn mode-line.
20716 This may happen if called from pos_visible_p. */
20717 it.glyph_row->enabled_p = 0;
20718 prepare_desired_row (it.glyph_row);
20720 it.glyph_row->mode_line_p = 1;
20722 /* FIXME: This should be controlled by a user option. But
20723 supporting such an option is not trivial, since the mode line is
20724 made up of many separate strings. */
20725 it.paragraph_embedding = L2R;
20727 record_unwind_protect (unwind_format_mode_line,
20728 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20730 mode_line_target = MODE_LINE_DISPLAY;
20732 /* Temporarily make frame's keyboard the current kboard so that
20733 kboard-local variables in the mode_line_format will get the right
20734 values. */
20735 push_kboard (FRAME_KBOARD (it.f));
20736 record_unwind_save_match_data ();
20737 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20738 pop_kboard ();
20740 unbind_to (count, Qnil);
20742 /* Fill up with spaces. */
20743 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20745 compute_line_metrics (&it);
20746 it.glyph_row->full_width_p = 1;
20747 it.glyph_row->continued_p = 0;
20748 it.glyph_row->truncated_on_left_p = 0;
20749 it.glyph_row->truncated_on_right_p = 0;
20751 /* Make a 3D mode-line have a shadow at its right end. */
20752 face = FACE_FROM_ID (it.f, face_id);
20753 extend_face_to_end_of_line (&it);
20754 if (face->box != FACE_NO_BOX)
20756 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20757 + it.glyph_row->used[TEXT_AREA] - 1);
20758 last->right_box_line_p = 1;
20761 return it.glyph_row->height;
20764 /* Move element ELT in LIST to the front of LIST.
20765 Return the updated list. */
20767 static Lisp_Object
20768 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20770 register Lisp_Object tail, prev;
20771 register Lisp_Object tem;
20773 tail = list;
20774 prev = Qnil;
20775 while (CONSP (tail))
20777 tem = XCAR (tail);
20779 if (EQ (elt, tem))
20781 /* Splice out the link TAIL. */
20782 if (NILP (prev))
20783 list = XCDR (tail);
20784 else
20785 Fsetcdr (prev, XCDR (tail));
20787 /* Now make it the first. */
20788 Fsetcdr (tail, list);
20789 return tail;
20791 else
20792 prev = tail;
20793 tail = XCDR (tail);
20794 QUIT;
20797 /* Not found--return unchanged LIST. */
20798 return list;
20801 /* Contribute ELT to the mode line for window IT->w. How it
20802 translates into text depends on its data type.
20804 IT describes the display environment in which we display, as usual.
20806 DEPTH is the depth in recursion. It is used to prevent
20807 infinite recursion here.
20809 FIELD_WIDTH is the number of characters the display of ELT should
20810 occupy in the mode line, and PRECISION is the maximum number of
20811 characters to display from ELT's representation. See
20812 display_string for details.
20814 Returns the hpos of the end of the text generated by ELT.
20816 PROPS is a property list to add to any string we encounter.
20818 If RISKY is nonzero, remove (disregard) any properties in any string
20819 we encounter, and ignore :eval and :propertize.
20821 The global variable `mode_line_target' determines whether the
20822 output is passed to `store_mode_line_noprop',
20823 `store_mode_line_string', or `display_string'. */
20825 static int
20826 display_mode_element (struct it *it, int depth, int field_width, int precision,
20827 Lisp_Object elt, Lisp_Object props, int risky)
20829 int n = 0, field, prec;
20830 int literal = 0;
20832 tail_recurse:
20833 if (depth > 100)
20834 elt = build_string ("*too-deep*");
20836 depth++;
20838 switch (XTYPE (elt))
20840 case Lisp_String:
20842 /* A string: output it and check for %-constructs within it. */
20843 unsigned char c;
20844 ptrdiff_t offset = 0;
20846 if (SCHARS (elt) > 0
20847 && (!NILP (props) || risky))
20849 Lisp_Object oprops, aelt;
20850 oprops = Ftext_properties_at (make_number (0), elt);
20852 /* If the starting string's properties are not what
20853 we want, translate the string. Also, if the string
20854 is risky, do that anyway. */
20856 if (NILP (Fequal (props, oprops)) || risky)
20858 /* If the starting string has properties,
20859 merge the specified ones onto the existing ones. */
20860 if (! NILP (oprops) && !risky)
20862 Lisp_Object tem;
20864 oprops = Fcopy_sequence (oprops);
20865 tem = props;
20866 while (CONSP (tem))
20868 oprops = Fplist_put (oprops, XCAR (tem),
20869 XCAR (XCDR (tem)));
20870 tem = XCDR (XCDR (tem));
20872 props = oprops;
20875 aelt = Fassoc (elt, mode_line_proptrans_alist);
20876 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20878 /* AELT is what we want. Move it to the front
20879 without consing. */
20880 elt = XCAR (aelt);
20881 mode_line_proptrans_alist
20882 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20884 else
20886 Lisp_Object tem;
20888 /* If AELT has the wrong props, it is useless.
20889 so get rid of it. */
20890 if (! NILP (aelt))
20891 mode_line_proptrans_alist
20892 = Fdelq (aelt, mode_line_proptrans_alist);
20894 elt = Fcopy_sequence (elt);
20895 Fset_text_properties (make_number (0), Flength (elt),
20896 props, elt);
20897 /* Add this item to mode_line_proptrans_alist. */
20898 mode_line_proptrans_alist
20899 = Fcons (Fcons (elt, props),
20900 mode_line_proptrans_alist);
20901 /* Truncate mode_line_proptrans_alist
20902 to at most 50 elements. */
20903 tem = Fnthcdr (make_number (50),
20904 mode_line_proptrans_alist);
20905 if (! NILP (tem))
20906 XSETCDR (tem, Qnil);
20911 offset = 0;
20913 if (literal)
20915 prec = precision - n;
20916 switch (mode_line_target)
20918 case MODE_LINE_NOPROP:
20919 case MODE_LINE_TITLE:
20920 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20921 break;
20922 case MODE_LINE_STRING:
20923 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20924 break;
20925 case MODE_LINE_DISPLAY:
20926 n += display_string (NULL, elt, Qnil, 0, 0, it,
20927 0, prec, 0, STRING_MULTIBYTE (elt));
20928 break;
20931 break;
20934 /* Handle the non-literal case. */
20936 while ((precision <= 0 || n < precision)
20937 && SREF (elt, offset) != 0
20938 && (mode_line_target != MODE_LINE_DISPLAY
20939 || it->current_x < it->last_visible_x))
20941 ptrdiff_t last_offset = offset;
20943 /* Advance to end of string or next format specifier. */
20944 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20947 if (offset - 1 != last_offset)
20949 ptrdiff_t nchars, nbytes;
20951 /* Output to end of string or up to '%'. Field width
20952 is length of string. Don't output more than
20953 PRECISION allows us. */
20954 offset--;
20956 prec = c_string_width (SDATA (elt) + last_offset,
20957 offset - last_offset, precision - n,
20958 &nchars, &nbytes);
20960 switch (mode_line_target)
20962 case MODE_LINE_NOPROP:
20963 case MODE_LINE_TITLE:
20964 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20965 break;
20966 case MODE_LINE_STRING:
20968 ptrdiff_t bytepos = last_offset;
20969 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20970 ptrdiff_t endpos = (precision <= 0
20971 ? string_byte_to_char (elt, offset)
20972 : charpos + nchars);
20974 n += store_mode_line_string (NULL,
20975 Fsubstring (elt, make_number (charpos),
20976 make_number (endpos)),
20977 0, 0, 0, Qnil);
20979 break;
20980 case MODE_LINE_DISPLAY:
20982 ptrdiff_t bytepos = last_offset;
20983 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20985 if (precision <= 0)
20986 nchars = string_byte_to_char (elt, offset) - charpos;
20987 n += display_string (NULL, elt, Qnil, 0, charpos,
20988 it, 0, nchars, 0,
20989 STRING_MULTIBYTE (elt));
20991 break;
20994 else /* c == '%' */
20996 ptrdiff_t percent_position = offset;
20998 /* Get the specified minimum width. Zero means
20999 don't pad. */
21000 field = 0;
21001 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21002 field = field * 10 + c - '0';
21004 /* Don't pad beyond the total padding allowed. */
21005 if (field_width - n > 0 && field > field_width - n)
21006 field = field_width - n;
21008 /* Note that either PRECISION <= 0 or N < PRECISION. */
21009 prec = precision - n;
21011 if (c == 'M')
21012 n += display_mode_element (it, depth, field, prec,
21013 Vglobal_mode_string, props,
21014 risky);
21015 else if (c != 0)
21017 bool multibyte;
21018 ptrdiff_t bytepos, charpos;
21019 const char *spec;
21020 Lisp_Object string;
21022 bytepos = percent_position;
21023 charpos = (STRING_MULTIBYTE (elt)
21024 ? string_byte_to_char (elt, bytepos)
21025 : bytepos);
21026 spec = decode_mode_spec (it->w, c, field, &string);
21027 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21029 switch (mode_line_target)
21031 case MODE_LINE_NOPROP:
21032 case MODE_LINE_TITLE:
21033 n += store_mode_line_noprop (spec, field, prec);
21034 break;
21035 case MODE_LINE_STRING:
21037 Lisp_Object tem = build_string (spec);
21038 props = Ftext_properties_at (make_number (charpos), elt);
21039 /* Should only keep face property in props */
21040 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21042 break;
21043 case MODE_LINE_DISPLAY:
21045 int nglyphs_before, nwritten;
21047 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21048 nwritten = display_string (spec, string, elt,
21049 charpos, 0, it,
21050 field, prec, 0,
21051 multibyte);
21053 /* Assign to the glyphs written above the
21054 string where the `%x' came from, position
21055 of the `%'. */
21056 if (nwritten > 0)
21058 struct glyph *glyph
21059 = (it->glyph_row->glyphs[TEXT_AREA]
21060 + nglyphs_before);
21061 int i;
21063 for (i = 0; i < nwritten; ++i)
21065 glyph[i].object = elt;
21066 glyph[i].charpos = charpos;
21069 n += nwritten;
21072 break;
21075 else /* c == 0 */
21076 break;
21080 break;
21082 case Lisp_Symbol:
21083 /* A symbol: process the value of the symbol recursively
21084 as if it appeared here directly. Avoid error if symbol void.
21085 Special case: if value of symbol is a string, output the string
21086 literally. */
21088 register Lisp_Object tem;
21090 /* If the variable is not marked as risky to set
21091 then its contents are risky to use. */
21092 if (NILP (Fget (elt, Qrisky_local_variable)))
21093 risky = 1;
21095 tem = Fboundp (elt);
21096 if (!NILP (tem))
21098 tem = Fsymbol_value (elt);
21099 /* If value is a string, output that string literally:
21100 don't check for % within it. */
21101 if (STRINGP (tem))
21102 literal = 1;
21104 if (!EQ (tem, elt))
21106 /* Give up right away for nil or t. */
21107 elt = tem;
21108 goto tail_recurse;
21112 break;
21114 case Lisp_Cons:
21116 register Lisp_Object car, tem;
21118 /* A cons cell: five distinct cases.
21119 If first element is :eval or :propertize, do something special.
21120 If first element is a string or a cons, process all the elements
21121 and effectively concatenate them.
21122 If first element is a negative number, truncate displaying cdr to
21123 at most that many characters. If positive, pad (with spaces)
21124 to at least that many characters.
21125 If first element is a symbol, process the cadr or caddr recursively
21126 according to whether the symbol's value is non-nil or nil. */
21127 car = XCAR (elt);
21128 if (EQ (car, QCeval))
21130 /* An element of the form (:eval FORM) means evaluate FORM
21131 and use the result as mode line elements. */
21133 if (risky)
21134 break;
21136 if (CONSP (XCDR (elt)))
21138 Lisp_Object spec;
21139 spec = safe_eval (XCAR (XCDR (elt)));
21140 n += display_mode_element (it, depth, field_width - n,
21141 precision - n, spec, props,
21142 risky);
21145 else if (EQ (car, QCpropertize))
21147 /* An element of the form (:propertize ELT PROPS...)
21148 means display ELT but applying properties PROPS. */
21150 if (risky)
21151 break;
21153 if (CONSP (XCDR (elt)))
21154 n += display_mode_element (it, depth, field_width - n,
21155 precision - n, XCAR (XCDR (elt)),
21156 XCDR (XCDR (elt)), risky);
21158 else if (SYMBOLP (car))
21160 tem = Fboundp (car);
21161 elt = XCDR (elt);
21162 if (!CONSP (elt))
21163 goto invalid;
21164 /* elt is now the cdr, and we know it is a cons cell.
21165 Use its car if CAR has a non-nil value. */
21166 if (!NILP (tem))
21168 tem = Fsymbol_value (car);
21169 if (!NILP (tem))
21171 elt = XCAR (elt);
21172 goto tail_recurse;
21175 /* Symbol's value is nil (or symbol is unbound)
21176 Get the cddr of the original list
21177 and if possible find the caddr and use that. */
21178 elt = XCDR (elt);
21179 if (NILP (elt))
21180 break;
21181 else if (!CONSP (elt))
21182 goto invalid;
21183 elt = XCAR (elt);
21184 goto tail_recurse;
21186 else if (INTEGERP (car))
21188 register int lim = XINT (car);
21189 elt = XCDR (elt);
21190 if (lim < 0)
21192 /* Negative int means reduce maximum width. */
21193 if (precision <= 0)
21194 precision = -lim;
21195 else
21196 precision = min (precision, -lim);
21198 else if (lim > 0)
21200 /* Padding specified. Don't let it be more than
21201 current maximum. */
21202 if (precision > 0)
21203 lim = min (precision, lim);
21205 /* If that's more padding than already wanted, queue it.
21206 But don't reduce padding already specified even if
21207 that is beyond the current truncation point. */
21208 field_width = max (lim, field_width);
21210 goto tail_recurse;
21212 else if (STRINGP (car) || CONSP (car))
21214 Lisp_Object halftail = elt;
21215 int len = 0;
21217 while (CONSP (elt)
21218 && (precision <= 0 || n < precision))
21220 n += display_mode_element (it, depth,
21221 /* Do padding only after the last
21222 element in the list. */
21223 (! CONSP (XCDR (elt))
21224 ? field_width - n
21225 : 0),
21226 precision - n, XCAR (elt),
21227 props, risky);
21228 elt = XCDR (elt);
21229 len++;
21230 if ((len & 1) == 0)
21231 halftail = XCDR (halftail);
21232 /* Check for cycle. */
21233 if (EQ (halftail, elt))
21234 break;
21238 break;
21240 default:
21241 invalid:
21242 elt = build_string ("*invalid*");
21243 goto tail_recurse;
21246 /* Pad to FIELD_WIDTH. */
21247 if (field_width > 0 && n < field_width)
21249 switch (mode_line_target)
21251 case MODE_LINE_NOPROP:
21252 case MODE_LINE_TITLE:
21253 n += store_mode_line_noprop ("", field_width - n, 0);
21254 break;
21255 case MODE_LINE_STRING:
21256 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21257 break;
21258 case MODE_LINE_DISPLAY:
21259 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21260 0, 0, 0);
21261 break;
21265 return n;
21268 /* Store a mode-line string element in mode_line_string_list.
21270 If STRING is non-null, display that C string. Otherwise, the Lisp
21271 string LISP_STRING is displayed.
21273 FIELD_WIDTH is the minimum number of output glyphs to produce.
21274 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21275 with spaces. FIELD_WIDTH <= 0 means don't pad.
21277 PRECISION is the maximum number of characters to output from
21278 STRING. PRECISION <= 0 means don't truncate the string.
21280 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21281 properties to the string.
21283 PROPS are the properties to add to the string.
21284 The mode_line_string_face face property is always added to the string.
21287 static int
21288 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21289 int field_width, int precision, Lisp_Object props)
21291 ptrdiff_t len;
21292 int n = 0;
21294 if (string != NULL)
21296 len = strlen (string);
21297 if (precision > 0 && len > precision)
21298 len = precision;
21299 lisp_string = make_string (string, len);
21300 if (NILP (props))
21301 props = mode_line_string_face_prop;
21302 else if (!NILP (mode_line_string_face))
21304 Lisp_Object face = Fplist_get (props, Qface);
21305 props = Fcopy_sequence (props);
21306 if (NILP (face))
21307 face = mode_line_string_face;
21308 else
21309 face = list2 (face, mode_line_string_face);
21310 props = Fplist_put (props, Qface, face);
21312 Fadd_text_properties (make_number (0), make_number (len),
21313 props, lisp_string);
21315 else
21317 len = XFASTINT (Flength (lisp_string));
21318 if (precision > 0 && len > precision)
21320 len = precision;
21321 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21322 precision = -1;
21324 if (!NILP (mode_line_string_face))
21326 Lisp_Object face;
21327 if (NILP (props))
21328 props = Ftext_properties_at (make_number (0), lisp_string);
21329 face = Fplist_get (props, Qface);
21330 if (NILP (face))
21331 face = mode_line_string_face;
21332 else
21333 face = list2 (face, mode_line_string_face);
21334 props = list2 (Qface, face);
21335 if (copy_string)
21336 lisp_string = Fcopy_sequence (lisp_string);
21338 if (!NILP (props))
21339 Fadd_text_properties (make_number (0), make_number (len),
21340 props, lisp_string);
21343 if (len > 0)
21345 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21346 n += len;
21349 if (field_width > len)
21351 field_width -= len;
21352 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21353 if (!NILP (props))
21354 Fadd_text_properties (make_number (0), make_number (field_width),
21355 props, lisp_string);
21356 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21357 n += field_width;
21360 return n;
21364 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21365 1, 4, 0,
21366 doc: /* Format a string out of a mode line format specification.
21367 First arg FORMAT specifies the mode line format (see `mode-line-format'
21368 for details) to use.
21370 By default, the format is evaluated for the currently selected window.
21372 Optional second arg FACE specifies the face property to put on all
21373 characters for which no face is specified. The value nil means the
21374 default face. The value t means whatever face the window's mode line
21375 currently uses (either `mode-line' or `mode-line-inactive',
21376 depending on whether the window is the selected window or not).
21377 An integer value means the value string has no text
21378 properties.
21380 Optional third and fourth args WINDOW and BUFFER specify the window
21381 and buffer to use as the context for the formatting (defaults
21382 are the selected window and the WINDOW's buffer). */)
21383 (Lisp_Object format, Lisp_Object face,
21384 Lisp_Object window, Lisp_Object buffer)
21386 struct it it;
21387 int len;
21388 struct window *w;
21389 struct buffer *old_buffer = NULL;
21390 int face_id;
21391 int no_props = INTEGERP (face);
21392 ptrdiff_t count = SPECPDL_INDEX ();
21393 Lisp_Object str;
21394 int string_start = 0;
21396 w = decode_any_window (window);
21397 XSETWINDOW (window, w);
21399 if (NILP (buffer))
21400 buffer = w->contents;
21401 CHECK_BUFFER (buffer);
21403 /* Make formatting the modeline a non-op when noninteractive, otherwise
21404 there will be problems later caused by a partially initialized frame. */
21405 if (NILP (format) || noninteractive)
21406 return empty_unibyte_string;
21408 if (no_props)
21409 face = Qnil;
21411 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21412 : EQ (face, Qt) ? (EQ (window, selected_window)
21413 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21414 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21415 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21416 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21417 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21418 : DEFAULT_FACE_ID;
21420 old_buffer = current_buffer;
21422 /* Save things including mode_line_proptrans_alist,
21423 and set that to nil so that we don't alter the outer value. */
21424 record_unwind_protect (unwind_format_mode_line,
21425 format_mode_line_unwind_data
21426 (XFRAME (WINDOW_FRAME (w)),
21427 old_buffer, selected_window, 1));
21428 mode_line_proptrans_alist = Qnil;
21430 Fselect_window (window, Qt);
21431 set_buffer_internal_1 (XBUFFER (buffer));
21433 init_iterator (&it, w, -1, -1, NULL, face_id);
21435 if (no_props)
21437 mode_line_target = MODE_LINE_NOPROP;
21438 mode_line_string_face_prop = Qnil;
21439 mode_line_string_list = Qnil;
21440 string_start = MODE_LINE_NOPROP_LEN (0);
21442 else
21444 mode_line_target = MODE_LINE_STRING;
21445 mode_line_string_list = Qnil;
21446 mode_line_string_face = face;
21447 mode_line_string_face_prop
21448 = NILP (face) ? Qnil : list2 (Qface, face);
21451 push_kboard (FRAME_KBOARD (it.f));
21452 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21453 pop_kboard ();
21455 if (no_props)
21457 len = MODE_LINE_NOPROP_LEN (string_start);
21458 str = make_string (mode_line_noprop_buf + string_start, len);
21460 else
21462 mode_line_string_list = Fnreverse (mode_line_string_list);
21463 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21464 empty_unibyte_string);
21467 unbind_to (count, Qnil);
21468 return str;
21471 /* Write a null-terminated, right justified decimal representation of
21472 the positive integer D to BUF using a minimal field width WIDTH. */
21474 static void
21475 pint2str (register char *buf, register int width, register ptrdiff_t d)
21477 register char *p = buf;
21479 if (d <= 0)
21480 *p++ = '0';
21481 else
21483 while (d > 0)
21485 *p++ = d % 10 + '0';
21486 d /= 10;
21490 for (width -= (int) (p - buf); width > 0; --width)
21491 *p++ = ' ';
21492 *p-- = '\0';
21493 while (p > buf)
21495 d = *buf;
21496 *buf++ = *p;
21497 *p-- = d;
21501 /* Write a null-terminated, right justified decimal and "human
21502 readable" representation of the nonnegative integer D to BUF using
21503 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21505 static const char power_letter[] =
21507 0, /* no letter */
21508 'k', /* kilo */
21509 'M', /* mega */
21510 'G', /* giga */
21511 'T', /* tera */
21512 'P', /* peta */
21513 'E', /* exa */
21514 'Z', /* zetta */
21515 'Y' /* yotta */
21518 static void
21519 pint2hrstr (char *buf, int width, ptrdiff_t d)
21521 /* We aim to represent the nonnegative integer D as
21522 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21523 ptrdiff_t quotient = d;
21524 int remainder = 0;
21525 /* -1 means: do not use TENTHS. */
21526 int tenths = -1;
21527 int exponent = 0;
21529 /* Length of QUOTIENT.TENTHS as a string. */
21530 int length;
21532 char * psuffix;
21533 char * p;
21535 if (quotient >= 1000)
21537 /* Scale to the appropriate EXPONENT. */
21540 remainder = quotient % 1000;
21541 quotient /= 1000;
21542 exponent++;
21544 while (quotient >= 1000);
21546 /* Round to nearest and decide whether to use TENTHS or not. */
21547 if (quotient <= 9)
21549 tenths = remainder / 100;
21550 if (remainder % 100 >= 50)
21552 if (tenths < 9)
21553 tenths++;
21554 else
21556 quotient++;
21557 if (quotient == 10)
21558 tenths = -1;
21559 else
21560 tenths = 0;
21564 else
21565 if (remainder >= 500)
21567 if (quotient < 999)
21568 quotient++;
21569 else
21571 quotient = 1;
21572 exponent++;
21573 tenths = 0;
21578 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21579 if (tenths == -1 && quotient <= 99)
21580 if (quotient <= 9)
21581 length = 1;
21582 else
21583 length = 2;
21584 else
21585 length = 3;
21586 p = psuffix = buf + max (width, length);
21588 /* Print EXPONENT. */
21589 *psuffix++ = power_letter[exponent];
21590 *psuffix = '\0';
21592 /* Print TENTHS. */
21593 if (tenths >= 0)
21595 *--p = '0' + tenths;
21596 *--p = '.';
21599 /* Print QUOTIENT. */
21602 int digit = quotient % 10;
21603 *--p = '0' + digit;
21605 while ((quotient /= 10) != 0);
21607 /* Print leading spaces. */
21608 while (buf < p)
21609 *--p = ' ';
21612 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21613 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21614 type of CODING_SYSTEM. Return updated pointer into BUF. */
21616 static unsigned char invalid_eol_type[] = "(*invalid*)";
21618 static char *
21619 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21621 Lisp_Object val;
21622 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21623 const unsigned char *eol_str;
21624 int eol_str_len;
21625 /* The EOL conversion we are using. */
21626 Lisp_Object eoltype;
21628 val = CODING_SYSTEM_SPEC (coding_system);
21629 eoltype = Qnil;
21631 if (!VECTORP (val)) /* Not yet decided. */
21633 *buf++ = multibyte ? '-' : ' ';
21634 if (eol_flag)
21635 eoltype = eol_mnemonic_undecided;
21636 /* Don't mention EOL conversion if it isn't decided. */
21638 else
21640 Lisp_Object attrs;
21641 Lisp_Object eolvalue;
21643 attrs = AREF (val, 0);
21644 eolvalue = AREF (val, 2);
21646 *buf++ = multibyte
21647 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21648 : ' ';
21650 if (eol_flag)
21652 /* The EOL conversion that is normal on this system. */
21654 if (NILP (eolvalue)) /* Not yet decided. */
21655 eoltype = eol_mnemonic_undecided;
21656 else if (VECTORP (eolvalue)) /* Not yet decided. */
21657 eoltype = eol_mnemonic_undecided;
21658 else /* eolvalue is Qunix, Qdos, or Qmac. */
21659 eoltype = (EQ (eolvalue, Qunix)
21660 ? eol_mnemonic_unix
21661 : (EQ (eolvalue, Qdos) == 1
21662 ? eol_mnemonic_dos : eol_mnemonic_mac));
21666 if (eol_flag)
21668 /* Mention the EOL conversion if it is not the usual one. */
21669 if (STRINGP (eoltype))
21671 eol_str = SDATA (eoltype);
21672 eol_str_len = SBYTES (eoltype);
21674 else if (CHARACTERP (eoltype))
21676 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21677 int c = XFASTINT (eoltype);
21678 eol_str_len = CHAR_STRING (c, tmp);
21679 eol_str = tmp;
21681 else
21683 eol_str = invalid_eol_type;
21684 eol_str_len = sizeof (invalid_eol_type) - 1;
21686 memcpy (buf, eol_str, eol_str_len);
21687 buf += eol_str_len;
21690 return buf;
21693 /* Return a string for the output of a mode line %-spec for window W,
21694 generated by character C. FIELD_WIDTH > 0 means pad the string
21695 returned with spaces to that value. Return a Lisp string in
21696 *STRING if the resulting string is taken from that Lisp string.
21698 Note we operate on the current buffer for most purposes. */
21700 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21702 static const char *
21703 decode_mode_spec (struct window *w, register int c, int field_width,
21704 Lisp_Object *string)
21706 Lisp_Object obj;
21707 struct frame *f = XFRAME (WINDOW_FRAME (w));
21708 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21709 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21710 produce strings from numerical values, so limit preposterously
21711 large values of FIELD_WIDTH to avoid overrunning the buffer's
21712 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21713 bytes plus the terminating null. */
21714 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21715 struct buffer *b = current_buffer;
21717 obj = Qnil;
21718 *string = Qnil;
21720 switch (c)
21722 case '*':
21723 if (!NILP (BVAR (b, read_only)))
21724 return "%";
21725 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21726 return "*";
21727 return "-";
21729 case '+':
21730 /* This differs from %* only for a modified read-only buffer. */
21731 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21732 return "*";
21733 if (!NILP (BVAR (b, read_only)))
21734 return "%";
21735 return "-";
21737 case '&':
21738 /* This differs from %* in ignoring read-only-ness. */
21739 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21740 return "*";
21741 return "-";
21743 case '%':
21744 return "%";
21746 case '[':
21748 int i;
21749 char *p;
21751 if (command_loop_level > 5)
21752 return "[[[... ";
21753 p = decode_mode_spec_buf;
21754 for (i = 0; i < command_loop_level; i++)
21755 *p++ = '[';
21756 *p = 0;
21757 return decode_mode_spec_buf;
21760 case ']':
21762 int i;
21763 char *p;
21765 if (command_loop_level > 5)
21766 return " ...]]]";
21767 p = decode_mode_spec_buf;
21768 for (i = 0; i < command_loop_level; i++)
21769 *p++ = ']';
21770 *p = 0;
21771 return decode_mode_spec_buf;
21774 case '-':
21776 register int i;
21778 /* Let lots_of_dashes be a string of infinite length. */
21779 if (mode_line_target == MODE_LINE_NOPROP
21780 || mode_line_target == MODE_LINE_STRING)
21781 return "--";
21782 if (field_width <= 0
21783 || field_width > sizeof (lots_of_dashes))
21785 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21786 decode_mode_spec_buf[i] = '-';
21787 decode_mode_spec_buf[i] = '\0';
21788 return decode_mode_spec_buf;
21790 else
21791 return lots_of_dashes;
21794 case 'b':
21795 obj = BVAR (b, name);
21796 break;
21798 case 'c':
21799 /* %c and %l are ignored in `frame-title-format'.
21800 (In redisplay_internal, the frame title is drawn _before_ the
21801 windows are updated, so the stuff which depends on actual
21802 window contents (such as %l) may fail to render properly, or
21803 even crash emacs.) */
21804 if (mode_line_target == MODE_LINE_TITLE)
21805 return "";
21806 else
21808 ptrdiff_t col = current_column ();
21809 w->column_number_displayed = col;
21810 pint2str (decode_mode_spec_buf, width, col);
21811 return decode_mode_spec_buf;
21814 case 'e':
21815 #ifndef SYSTEM_MALLOC
21817 if (NILP (Vmemory_full))
21818 return "";
21819 else
21820 return "!MEM FULL! ";
21822 #else
21823 return "";
21824 #endif
21826 case 'F':
21827 /* %F displays the frame name. */
21828 if (!NILP (f->title))
21829 return SSDATA (f->title);
21830 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21831 return SSDATA (f->name);
21832 return "Emacs";
21834 case 'f':
21835 obj = BVAR (b, filename);
21836 break;
21838 case 'i':
21840 ptrdiff_t size = ZV - BEGV;
21841 pint2str (decode_mode_spec_buf, width, size);
21842 return decode_mode_spec_buf;
21845 case 'I':
21847 ptrdiff_t size = ZV - BEGV;
21848 pint2hrstr (decode_mode_spec_buf, width, size);
21849 return decode_mode_spec_buf;
21852 case 'l':
21854 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21855 ptrdiff_t topline, nlines, height;
21856 ptrdiff_t junk;
21858 /* %c and %l are ignored in `frame-title-format'. */
21859 if (mode_line_target == MODE_LINE_TITLE)
21860 return "";
21862 startpos = marker_position (w->start);
21863 startpos_byte = marker_byte_position (w->start);
21864 height = WINDOW_TOTAL_LINES (w);
21866 /* If we decided that this buffer isn't suitable for line numbers,
21867 don't forget that too fast. */
21868 if (w->base_line_pos == -1)
21869 goto no_value;
21871 /* If the buffer is very big, don't waste time. */
21872 if (INTEGERP (Vline_number_display_limit)
21873 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21875 w->base_line_pos = 0;
21876 w->base_line_number = 0;
21877 goto no_value;
21880 if (w->base_line_number > 0
21881 && w->base_line_pos > 0
21882 && w->base_line_pos <= startpos)
21884 line = w->base_line_number;
21885 linepos = w->base_line_pos;
21886 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21888 else
21890 line = 1;
21891 linepos = BUF_BEGV (b);
21892 linepos_byte = BUF_BEGV_BYTE (b);
21895 /* Count lines from base line to window start position. */
21896 nlines = display_count_lines (linepos_byte,
21897 startpos_byte,
21898 startpos, &junk);
21900 topline = nlines + line;
21902 /* Determine a new base line, if the old one is too close
21903 or too far away, or if we did not have one.
21904 "Too close" means it's plausible a scroll-down would
21905 go back past it. */
21906 if (startpos == BUF_BEGV (b))
21908 w->base_line_number = topline;
21909 w->base_line_pos = BUF_BEGV (b);
21911 else if (nlines < height + 25 || nlines > height * 3 + 50
21912 || linepos == BUF_BEGV (b))
21914 ptrdiff_t limit = BUF_BEGV (b);
21915 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21916 ptrdiff_t position;
21917 ptrdiff_t distance =
21918 (height * 2 + 30) * line_number_display_limit_width;
21920 if (startpos - distance > limit)
21922 limit = startpos - distance;
21923 limit_byte = CHAR_TO_BYTE (limit);
21926 nlines = display_count_lines (startpos_byte,
21927 limit_byte,
21928 - (height * 2 + 30),
21929 &position);
21930 /* If we couldn't find the lines we wanted within
21931 line_number_display_limit_width chars per line,
21932 give up on line numbers for this window. */
21933 if (position == limit_byte && limit == startpos - distance)
21935 w->base_line_pos = -1;
21936 w->base_line_number = 0;
21937 goto no_value;
21940 w->base_line_number = topline - nlines;
21941 w->base_line_pos = BYTE_TO_CHAR (position);
21944 /* Now count lines from the start pos to point. */
21945 nlines = display_count_lines (startpos_byte,
21946 PT_BYTE, PT, &junk);
21948 /* Record that we did display the line number. */
21949 line_number_displayed = 1;
21951 /* Make the string to show. */
21952 pint2str (decode_mode_spec_buf, width, topline + nlines);
21953 return decode_mode_spec_buf;
21954 no_value:
21956 char* p = decode_mode_spec_buf;
21957 int pad = width - 2;
21958 while (pad-- > 0)
21959 *p++ = ' ';
21960 *p++ = '?';
21961 *p++ = '?';
21962 *p = '\0';
21963 return decode_mode_spec_buf;
21966 break;
21968 case 'm':
21969 obj = BVAR (b, mode_name);
21970 break;
21972 case 'n':
21973 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21974 return " Narrow";
21975 break;
21977 case 'p':
21979 ptrdiff_t pos = marker_position (w->start);
21980 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21982 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
21984 if (pos <= BUF_BEGV (b))
21985 return "All";
21986 else
21987 return "Bottom";
21989 else if (pos <= BUF_BEGV (b))
21990 return "Top";
21991 else
21993 if (total > 1000000)
21994 /* Do it differently for a large value, to avoid overflow. */
21995 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21996 else
21997 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21998 /* We can't normally display a 3-digit number,
21999 so get us a 2-digit number that is close. */
22000 if (total == 100)
22001 total = 99;
22002 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22003 return decode_mode_spec_buf;
22007 /* Display percentage of size above the bottom of the screen. */
22008 case 'P':
22010 ptrdiff_t toppos = marker_position (w->start);
22011 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22012 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22014 if (botpos >= BUF_ZV (b))
22016 if (toppos <= BUF_BEGV (b))
22017 return "All";
22018 else
22019 return "Bottom";
22021 else
22023 if (total > 1000000)
22024 /* Do it differently for a large value, to avoid overflow. */
22025 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22026 else
22027 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22028 /* We can't normally display a 3-digit number,
22029 so get us a 2-digit number that is close. */
22030 if (total == 100)
22031 total = 99;
22032 if (toppos <= BUF_BEGV (b))
22033 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22034 else
22035 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22036 return decode_mode_spec_buf;
22040 case 's':
22041 /* status of process */
22042 obj = Fget_buffer_process (Fcurrent_buffer ());
22043 if (NILP (obj))
22044 return "no process";
22045 #ifndef MSDOS
22046 obj = Fsymbol_name (Fprocess_status (obj));
22047 #endif
22048 break;
22050 case '@':
22052 ptrdiff_t count = inhibit_garbage_collection ();
22053 Lisp_Object val = call1 (intern ("file-remote-p"),
22054 BVAR (current_buffer, directory));
22055 unbind_to (count, Qnil);
22057 if (NILP (val))
22058 return "-";
22059 else
22060 return "@";
22063 case 'z':
22064 /* coding-system (not including end-of-line format) */
22065 case 'Z':
22066 /* coding-system (including end-of-line type) */
22068 int eol_flag = (c == 'Z');
22069 char *p = decode_mode_spec_buf;
22071 if (! FRAME_WINDOW_P (f))
22073 /* No need to mention EOL here--the terminal never needs
22074 to do EOL conversion. */
22075 p = decode_mode_spec_coding (CODING_ID_NAME
22076 (FRAME_KEYBOARD_CODING (f)->id),
22077 p, 0);
22078 p = decode_mode_spec_coding (CODING_ID_NAME
22079 (FRAME_TERMINAL_CODING (f)->id),
22080 p, 0);
22082 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22083 p, eol_flag);
22085 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22086 #ifdef subprocesses
22087 obj = Fget_buffer_process (Fcurrent_buffer ());
22088 if (PROCESSP (obj))
22090 p = decode_mode_spec_coding
22091 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22092 p = decode_mode_spec_coding
22093 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22095 #endif /* subprocesses */
22096 #endif /* 0 */
22097 *p = 0;
22098 return decode_mode_spec_buf;
22102 if (STRINGP (obj))
22104 *string = obj;
22105 return SSDATA (obj);
22107 else
22108 return "";
22112 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22113 means count lines back from START_BYTE. But don't go beyond
22114 LIMIT_BYTE. Return the number of lines thus found (always
22115 nonnegative).
22117 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22118 either the position COUNT lines after/before START_BYTE, if we
22119 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22120 COUNT lines. */
22122 static ptrdiff_t
22123 display_count_lines (ptrdiff_t start_byte,
22124 ptrdiff_t limit_byte, ptrdiff_t count,
22125 ptrdiff_t *byte_pos_ptr)
22127 register unsigned char *cursor;
22128 unsigned char *base;
22130 register ptrdiff_t ceiling;
22131 register unsigned char *ceiling_addr;
22132 ptrdiff_t orig_count = count;
22134 /* If we are not in selective display mode,
22135 check only for newlines. */
22136 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22137 && !INTEGERP (BVAR (current_buffer, selective_display)));
22139 if (count > 0)
22141 while (start_byte < limit_byte)
22143 ceiling = BUFFER_CEILING_OF (start_byte);
22144 ceiling = min (limit_byte - 1, ceiling);
22145 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22146 base = (cursor = BYTE_POS_ADDR (start_byte));
22150 if (selective_display)
22152 while (*cursor != '\n' && *cursor != 015
22153 && ++cursor != ceiling_addr)
22154 continue;
22155 if (cursor == ceiling_addr)
22156 break;
22158 else
22160 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22161 if (! cursor)
22162 break;
22165 cursor++;
22167 if (--count == 0)
22169 start_byte += cursor - base;
22170 *byte_pos_ptr = start_byte;
22171 return orig_count;
22174 while (cursor < ceiling_addr);
22176 start_byte += ceiling_addr - base;
22179 else
22181 while (start_byte > limit_byte)
22183 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22184 ceiling = max (limit_byte, ceiling);
22185 ceiling_addr = BYTE_POS_ADDR (ceiling);
22186 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22187 while (1)
22189 if (selective_display)
22191 while (--cursor >= ceiling_addr
22192 && *cursor != '\n' && *cursor != 015)
22193 continue;
22194 if (cursor < ceiling_addr)
22195 break;
22197 else
22199 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22200 if (! cursor)
22201 break;
22204 if (++count == 0)
22206 start_byte += cursor - base + 1;
22207 *byte_pos_ptr = start_byte;
22208 /* When scanning backwards, we should
22209 not count the newline posterior to which we stop. */
22210 return - orig_count - 1;
22213 start_byte += ceiling_addr - base;
22217 *byte_pos_ptr = limit_byte;
22219 if (count < 0)
22220 return - orig_count + count;
22221 return orig_count - count;
22227 /***********************************************************************
22228 Displaying strings
22229 ***********************************************************************/
22231 /* Display a NUL-terminated string, starting with index START.
22233 If STRING is non-null, display that C string. Otherwise, the Lisp
22234 string LISP_STRING is displayed. There's a case that STRING is
22235 non-null and LISP_STRING is not nil. It means STRING is a string
22236 data of LISP_STRING. In that case, we display LISP_STRING while
22237 ignoring its text properties.
22239 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22240 FACE_STRING. Display STRING or LISP_STRING with the face at
22241 FACE_STRING_POS in FACE_STRING:
22243 Display the string in the environment given by IT, but use the
22244 standard display table, temporarily.
22246 FIELD_WIDTH is the minimum number of output glyphs to produce.
22247 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22248 with spaces. If STRING has more characters, more than FIELD_WIDTH
22249 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22251 PRECISION is the maximum number of characters to output from
22252 STRING. PRECISION < 0 means don't truncate the string.
22254 This is roughly equivalent to printf format specifiers:
22256 FIELD_WIDTH PRECISION PRINTF
22257 ----------------------------------------
22258 -1 -1 %s
22259 -1 10 %.10s
22260 10 -1 %10s
22261 20 10 %20.10s
22263 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22264 display them, and < 0 means obey the current buffer's value of
22265 enable_multibyte_characters.
22267 Value is the number of columns displayed. */
22269 static int
22270 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22271 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22272 int field_width, int precision, int max_x, int multibyte)
22274 int hpos_at_start = it->hpos;
22275 int saved_face_id = it->face_id;
22276 struct glyph_row *row = it->glyph_row;
22277 ptrdiff_t it_charpos;
22279 /* Initialize the iterator IT for iteration over STRING beginning
22280 with index START. */
22281 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22282 precision, field_width, multibyte);
22283 if (string && STRINGP (lisp_string))
22284 /* LISP_STRING is the one returned by decode_mode_spec. We should
22285 ignore its text properties. */
22286 it->stop_charpos = it->end_charpos;
22288 /* If displaying STRING, set up the face of the iterator from
22289 FACE_STRING, if that's given. */
22290 if (STRINGP (face_string))
22292 ptrdiff_t endptr;
22293 struct face *face;
22295 it->face_id
22296 = face_at_string_position (it->w, face_string, face_string_pos,
22297 0, it->region_beg_charpos,
22298 it->region_end_charpos,
22299 &endptr, it->base_face_id, 0);
22300 face = FACE_FROM_ID (it->f, it->face_id);
22301 it->face_box_p = face->box != FACE_NO_BOX;
22304 /* Set max_x to the maximum allowed X position. Don't let it go
22305 beyond the right edge of the window. */
22306 if (max_x <= 0)
22307 max_x = it->last_visible_x;
22308 else
22309 max_x = min (max_x, it->last_visible_x);
22311 /* Skip over display elements that are not visible. because IT->w is
22312 hscrolled. */
22313 if (it->current_x < it->first_visible_x)
22314 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22315 MOVE_TO_POS | MOVE_TO_X);
22317 row->ascent = it->max_ascent;
22318 row->height = it->max_ascent + it->max_descent;
22319 row->phys_ascent = it->max_phys_ascent;
22320 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22321 row->extra_line_spacing = it->max_extra_line_spacing;
22323 if (STRINGP (it->string))
22324 it_charpos = IT_STRING_CHARPOS (*it);
22325 else
22326 it_charpos = IT_CHARPOS (*it);
22328 /* This condition is for the case that we are called with current_x
22329 past last_visible_x. */
22330 while (it->current_x < max_x)
22332 int x_before, x, n_glyphs_before, i, nglyphs;
22334 /* Get the next display element. */
22335 if (!get_next_display_element (it))
22336 break;
22338 /* Produce glyphs. */
22339 x_before = it->current_x;
22340 n_glyphs_before = row->used[TEXT_AREA];
22341 PRODUCE_GLYPHS (it);
22343 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22344 i = 0;
22345 x = x_before;
22346 while (i < nglyphs)
22348 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22350 if (it->line_wrap != TRUNCATE
22351 && x + glyph->pixel_width > max_x)
22353 /* End of continued line or max_x reached. */
22354 if (CHAR_GLYPH_PADDING_P (*glyph))
22356 /* A wide character is unbreakable. */
22357 if (row->reversed_p)
22358 unproduce_glyphs (it, row->used[TEXT_AREA]
22359 - n_glyphs_before);
22360 row->used[TEXT_AREA] = n_glyphs_before;
22361 it->current_x = x_before;
22363 else
22365 if (row->reversed_p)
22366 unproduce_glyphs (it, row->used[TEXT_AREA]
22367 - (n_glyphs_before + i));
22368 row->used[TEXT_AREA] = n_glyphs_before + i;
22369 it->current_x = x;
22371 break;
22373 else if (x + glyph->pixel_width >= it->first_visible_x)
22375 /* Glyph is at least partially visible. */
22376 ++it->hpos;
22377 if (x < it->first_visible_x)
22378 row->x = x - it->first_visible_x;
22380 else
22382 /* Glyph is off the left margin of the display area.
22383 Should not happen. */
22384 emacs_abort ();
22387 row->ascent = max (row->ascent, it->max_ascent);
22388 row->height = max (row->height, it->max_ascent + it->max_descent);
22389 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22390 row->phys_height = max (row->phys_height,
22391 it->max_phys_ascent + it->max_phys_descent);
22392 row->extra_line_spacing = max (row->extra_line_spacing,
22393 it->max_extra_line_spacing);
22394 x += glyph->pixel_width;
22395 ++i;
22398 /* Stop if max_x reached. */
22399 if (i < nglyphs)
22400 break;
22402 /* Stop at line ends. */
22403 if (ITERATOR_AT_END_OF_LINE_P (it))
22405 it->continuation_lines_width = 0;
22406 break;
22409 set_iterator_to_next (it, 1);
22410 if (STRINGP (it->string))
22411 it_charpos = IT_STRING_CHARPOS (*it);
22412 else
22413 it_charpos = IT_CHARPOS (*it);
22415 /* Stop if truncating at the right edge. */
22416 if (it->line_wrap == TRUNCATE
22417 && it->current_x >= it->last_visible_x)
22419 /* Add truncation mark, but don't do it if the line is
22420 truncated at a padding space. */
22421 if (it_charpos < it->string_nchars)
22423 if (!FRAME_WINDOW_P (it->f))
22425 int ii, n;
22427 if (it->current_x > it->last_visible_x)
22429 if (!row->reversed_p)
22431 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22432 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22433 break;
22435 else
22437 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22438 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22439 break;
22440 unproduce_glyphs (it, ii + 1);
22441 ii = row->used[TEXT_AREA] - (ii + 1);
22443 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22445 row->used[TEXT_AREA] = ii;
22446 produce_special_glyphs (it, IT_TRUNCATION);
22449 produce_special_glyphs (it, IT_TRUNCATION);
22451 row->truncated_on_right_p = 1;
22453 break;
22457 /* Maybe insert a truncation at the left. */
22458 if (it->first_visible_x
22459 && it_charpos > 0)
22461 if (!FRAME_WINDOW_P (it->f)
22462 || (row->reversed_p
22463 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22464 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22465 insert_left_trunc_glyphs (it);
22466 row->truncated_on_left_p = 1;
22469 it->face_id = saved_face_id;
22471 /* Value is number of columns displayed. */
22472 return it->hpos - hpos_at_start;
22477 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22478 appears as an element of LIST or as the car of an element of LIST.
22479 If PROPVAL is a list, compare each element against LIST in that
22480 way, and return 1/2 if any element of PROPVAL is found in LIST.
22481 Otherwise return 0. This function cannot quit.
22482 The return value is 2 if the text is invisible but with an ellipsis
22483 and 1 if it's invisible and without an ellipsis. */
22486 invisible_p (register Lisp_Object propval, Lisp_Object list)
22488 register Lisp_Object tail, proptail;
22490 for (tail = list; CONSP (tail); tail = XCDR (tail))
22492 register Lisp_Object tem;
22493 tem = XCAR (tail);
22494 if (EQ (propval, tem))
22495 return 1;
22496 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22497 return NILP (XCDR (tem)) ? 1 : 2;
22500 if (CONSP (propval))
22502 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22504 Lisp_Object propelt;
22505 propelt = XCAR (proptail);
22506 for (tail = list; CONSP (tail); tail = XCDR (tail))
22508 register Lisp_Object tem;
22509 tem = XCAR (tail);
22510 if (EQ (propelt, tem))
22511 return 1;
22512 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22513 return NILP (XCDR (tem)) ? 1 : 2;
22518 return 0;
22521 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22522 doc: /* Non-nil if the property makes the text invisible.
22523 POS-OR-PROP can be a marker or number, in which case it is taken to be
22524 a position in the current buffer and the value of the `invisible' property
22525 is checked; or it can be some other value, which is then presumed to be the
22526 value of the `invisible' property of the text of interest.
22527 The non-nil value returned can be t for truly invisible text or something
22528 else if the text is replaced by an ellipsis. */)
22529 (Lisp_Object pos_or_prop)
22531 Lisp_Object prop
22532 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22533 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22534 : pos_or_prop);
22535 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22536 return (invis == 0 ? Qnil
22537 : invis == 1 ? Qt
22538 : make_number (invis));
22541 /* Calculate a width or height in pixels from a specification using
22542 the following elements:
22544 SPEC ::=
22545 NUM - a (fractional) multiple of the default font width/height
22546 (NUM) - specifies exactly NUM pixels
22547 UNIT - a fixed number of pixels, see below.
22548 ELEMENT - size of a display element in pixels, see below.
22549 (NUM . SPEC) - equals NUM * SPEC
22550 (+ SPEC SPEC ...) - add pixel values
22551 (- SPEC SPEC ...) - subtract pixel values
22552 (- SPEC) - negate pixel value
22554 NUM ::=
22555 INT or FLOAT - a number constant
22556 SYMBOL - use symbol's (buffer local) variable binding.
22558 UNIT ::=
22559 in - pixels per inch *)
22560 mm - pixels per 1/1000 meter *)
22561 cm - pixels per 1/100 meter *)
22562 width - width of current font in pixels.
22563 height - height of current font in pixels.
22565 *) using the ratio(s) defined in display-pixels-per-inch.
22567 ELEMENT ::=
22569 left-fringe - left fringe width in pixels
22570 right-fringe - right fringe width in pixels
22572 left-margin - left margin width in pixels
22573 right-margin - right margin width in pixels
22575 scroll-bar - scroll-bar area width in pixels
22577 Examples:
22579 Pixels corresponding to 5 inches:
22580 (5 . in)
22582 Total width of non-text areas on left side of window (if scroll-bar is on left):
22583 '(space :width (+ left-fringe left-margin scroll-bar))
22585 Align to first text column (in header line):
22586 '(space :align-to 0)
22588 Align to middle of text area minus half the width of variable `my-image'
22589 containing a loaded image:
22590 '(space :align-to (0.5 . (- text my-image)))
22592 Width of left margin minus width of 1 character in the default font:
22593 '(space :width (- left-margin 1))
22595 Width of left margin minus width of 2 characters in the current font:
22596 '(space :width (- left-margin (2 . width)))
22598 Center 1 character over left-margin (in header line):
22599 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22601 Different ways to express width of left fringe plus left margin minus one pixel:
22602 '(space :width (- (+ left-fringe left-margin) (1)))
22603 '(space :width (+ left-fringe left-margin (- (1))))
22604 '(space :width (+ left-fringe left-margin (-1)))
22608 static int
22609 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22610 struct font *font, int width_p, int *align_to)
22612 double pixels;
22614 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22615 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22617 if (NILP (prop))
22618 return OK_PIXELS (0);
22620 eassert (FRAME_LIVE_P (it->f));
22622 if (SYMBOLP (prop))
22624 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22626 char *unit = SSDATA (SYMBOL_NAME (prop));
22628 if (unit[0] == 'i' && unit[1] == 'n')
22629 pixels = 1.0;
22630 else if (unit[0] == 'm' && unit[1] == 'm')
22631 pixels = 25.4;
22632 else if (unit[0] == 'c' && unit[1] == 'm')
22633 pixels = 2.54;
22634 else
22635 pixels = 0;
22636 if (pixels > 0)
22638 double ppi = (width_p ? FRAME_RES_X (it->f)
22639 : FRAME_RES_Y (it->f));
22641 if (ppi > 0)
22642 return OK_PIXELS (ppi / pixels);
22643 return 0;
22647 #ifdef HAVE_WINDOW_SYSTEM
22648 if (EQ (prop, Qheight))
22649 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22650 if (EQ (prop, Qwidth))
22651 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22652 #else
22653 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22654 return OK_PIXELS (1);
22655 #endif
22657 if (EQ (prop, Qtext))
22658 return OK_PIXELS (width_p
22659 ? window_box_width (it->w, TEXT_AREA)
22660 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22662 if (align_to && *align_to < 0)
22664 *res = 0;
22665 if (EQ (prop, Qleft))
22666 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22667 if (EQ (prop, Qright))
22668 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22669 if (EQ (prop, Qcenter))
22670 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22671 + window_box_width (it->w, TEXT_AREA) / 2);
22672 if (EQ (prop, Qleft_fringe))
22673 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22674 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22675 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22676 if (EQ (prop, Qright_fringe))
22677 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22678 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22679 : window_box_right_offset (it->w, TEXT_AREA));
22680 if (EQ (prop, Qleft_margin))
22681 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22682 if (EQ (prop, Qright_margin))
22683 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22684 if (EQ (prop, Qscroll_bar))
22685 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22687 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22688 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22689 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22690 : 0)));
22692 else
22694 if (EQ (prop, Qleft_fringe))
22695 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22696 if (EQ (prop, Qright_fringe))
22697 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22698 if (EQ (prop, Qleft_margin))
22699 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22700 if (EQ (prop, Qright_margin))
22701 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22702 if (EQ (prop, Qscroll_bar))
22703 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22706 prop = buffer_local_value_1 (prop, it->w->contents);
22707 if (EQ (prop, Qunbound))
22708 prop = Qnil;
22711 if (INTEGERP (prop) || FLOATP (prop))
22713 int base_unit = (width_p
22714 ? FRAME_COLUMN_WIDTH (it->f)
22715 : FRAME_LINE_HEIGHT (it->f));
22716 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22719 if (CONSP (prop))
22721 Lisp_Object car = XCAR (prop);
22722 Lisp_Object cdr = XCDR (prop);
22724 if (SYMBOLP (car))
22726 #ifdef HAVE_WINDOW_SYSTEM
22727 if (FRAME_WINDOW_P (it->f)
22728 && valid_image_p (prop))
22730 ptrdiff_t id = lookup_image (it->f, prop);
22731 struct image *img = IMAGE_FROM_ID (it->f, id);
22733 return OK_PIXELS (width_p ? img->width : img->height);
22735 #endif
22736 if (EQ (car, Qplus) || EQ (car, Qminus))
22738 int first = 1;
22739 double px;
22741 pixels = 0;
22742 while (CONSP (cdr))
22744 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22745 font, width_p, align_to))
22746 return 0;
22747 if (first)
22748 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22749 else
22750 pixels += px;
22751 cdr = XCDR (cdr);
22753 if (EQ (car, Qminus))
22754 pixels = -pixels;
22755 return OK_PIXELS (pixels);
22758 car = buffer_local_value_1 (car, it->w->contents);
22759 if (EQ (car, Qunbound))
22760 car = Qnil;
22763 if (INTEGERP (car) || FLOATP (car))
22765 double fact;
22766 pixels = XFLOATINT (car);
22767 if (NILP (cdr))
22768 return OK_PIXELS (pixels);
22769 if (calc_pixel_width_or_height (&fact, it, cdr,
22770 font, width_p, align_to))
22771 return OK_PIXELS (pixels * fact);
22772 return 0;
22775 return 0;
22778 return 0;
22782 /***********************************************************************
22783 Glyph Display
22784 ***********************************************************************/
22786 #ifdef HAVE_WINDOW_SYSTEM
22788 #ifdef GLYPH_DEBUG
22790 void
22791 dump_glyph_string (struct glyph_string *s)
22793 fprintf (stderr, "glyph string\n");
22794 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22795 s->x, s->y, s->width, s->height);
22796 fprintf (stderr, " ybase = %d\n", s->ybase);
22797 fprintf (stderr, " hl = %d\n", s->hl);
22798 fprintf (stderr, " left overhang = %d, right = %d\n",
22799 s->left_overhang, s->right_overhang);
22800 fprintf (stderr, " nchars = %d\n", s->nchars);
22801 fprintf (stderr, " extends to end of line = %d\n",
22802 s->extends_to_end_of_line_p);
22803 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22804 fprintf (stderr, " bg width = %d\n", s->background_width);
22807 #endif /* GLYPH_DEBUG */
22809 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22810 of XChar2b structures for S; it can't be allocated in
22811 init_glyph_string because it must be allocated via `alloca'. W
22812 is the window on which S is drawn. ROW and AREA are the glyph row
22813 and area within the row from which S is constructed. START is the
22814 index of the first glyph structure covered by S. HL is a
22815 face-override for drawing S. */
22817 #ifdef HAVE_NTGUI
22818 #define OPTIONAL_HDC(hdc) HDC hdc,
22819 #define DECLARE_HDC(hdc) HDC hdc;
22820 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22821 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22822 #endif
22824 #ifndef OPTIONAL_HDC
22825 #define OPTIONAL_HDC(hdc)
22826 #define DECLARE_HDC(hdc)
22827 #define ALLOCATE_HDC(hdc, f)
22828 #define RELEASE_HDC(hdc, f)
22829 #endif
22831 static void
22832 init_glyph_string (struct glyph_string *s,
22833 OPTIONAL_HDC (hdc)
22834 XChar2b *char2b, struct window *w, struct glyph_row *row,
22835 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22837 memset (s, 0, sizeof *s);
22838 s->w = w;
22839 s->f = XFRAME (w->frame);
22840 #ifdef HAVE_NTGUI
22841 s->hdc = hdc;
22842 #endif
22843 s->display = FRAME_X_DISPLAY (s->f);
22844 s->window = FRAME_X_WINDOW (s->f);
22845 s->char2b = char2b;
22846 s->hl = hl;
22847 s->row = row;
22848 s->area = area;
22849 s->first_glyph = row->glyphs[area] + start;
22850 s->height = row->height;
22851 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22852 s->ybase = s->y + row->ascent;
22856 /* Append the list of glyph strings with head H and tail T to the list
22857 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22859 static void
22860 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22861 struct glyph_string *h, struct glyph_string *t)
22863 if (h)
22865 if (*head)
22866 (*tail)->next = h;
22867 else
22868 *head = h;
22869 h->prev = *tail;
22870 *tail = t;
22875 /* Prepend the list of glyph strings with head H and tail T to the
22876 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22877 result. */
22879 static void
22880 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22881 struct glyph_string *h, struct glyph_string *t)
22883 if (h)
22885 if (*head)
22886 (*head)->prev = t;
22887 else
22888 *tail = t;
22889 t->next = *head;
22890 *head = h;
22895 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22896 Set *HEAD and *TAIL to the resulting list. */
22898 static void
22899 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22900 struct glyph_string *s)
22902 s->next = s->prev = NULL;
22903 append_glyph_string_lists (head, tail, s, s);
22907 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22908 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22909 make sure that X resources for the face returned are allocated.
22910 Value is a pointer to a realized face that is ready for display if
22911 DISPLAY_P is non-zero. */
22913 static struct face *
22914 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22915 XChar2b *char2b, int display_p)
22917 struct face *face = FACE_FROM_ID (f, face_id);
22918 unsigned code = 0;
22920 if (face->font)
22922 code = face->font->driver->encode_char (face->font, c);
22924 if (code == FONT_INVALID_CODE)
22925 code = 0;
22927 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22929 /* Make sure X resources of the face are allocated. */
22930 #ifdef HAVE_X_WINDOWS
22931 if (display_p)
22932 #endif
22934 eassert (face != NULL);
22935 PREPARE_FACE_FOR_DISPLAY (f, face);
22938 return face;
22942 /* Get face and two-byte form of character glyph GLYPH on frame F.
22943 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22944 a pointer to a realized face that is ready for display. */
22946 static struct face *
22947 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22948 XChar2b *char2b, int *two_byte_p)
22950 struct face *face;
22951 unsigned code = 0;
22953 eassert (glyph->type == CHAR_GLYPH);
22954 face = FACE_FROM_ID (f, glyph->face_id);
22956 /* Make sure X resources of the face are allocated. */
22957 eassert (face != NULL);
22958 PREPARE_FACE_FOR_DISPLAY (f, face);
22960 if (two_byte_p)
22961 *two_byte_p = 0;
22963 if (face->font)
22965 if (CHAR_BYTE8_P (glyph->u.ch))
22966 code = CHAR_TO_BYTE8 (glyph->u.ch);
22967 else
22968 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22970 if (code == FONT_INVALID_CODE)
22971 code = 0;
22974 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22975 return face;
22979 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22980 Return 1 if FONT has a glyph for C, otherwise return 0. */
22982 static int
22983 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22985 unsigned code;
22987 if (CHAR_BYTE8_P (c))
22988 code = CHAR_TO_BYTE8 (c);
22989 else
22990 code = font->driver->encode_char (font, c);
22992 if (code == FONT_INVALID_CODE)
22993 return 0;
22994 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22995 return 1;
22999 /* Fill glyph string S with composition components specified by S->cmp.
23001 BASE_FACE is the base face of the composition.
23002 S->cmp_from is the index of the first component for S.
23004 OVERLAPS non-zero means S should draw the foreground only, and use
23005 its physical height for clipping. See also draw_glyphs.
23007 Value is the index of a component not in S. */
23009 static int
23010 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23011 int overlaps)
23013 int i;
23014 /* For all glyphs of this composition, starting at the offset
23015 S->cmp_from, until we reach the end of the definition or encounter a
23016 glyph that requires the different face, add it to S. */
23017 struct face *face;
23019 eassert (s);
23021 s->for_overlaps = overlaps;
23022 s->face = NULL;
23023 s->font = NULL;
23024 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23026 int c = COMPOSITION_GLYPH (s->cmp, i);
23028 /* TAB in a composition means display glyphs with padding space
23029 on the left or right. */
23030 if (c != '\t')
23032 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23033 -1, Qnil);
23035 face = get_char_face_and_encoding (s->f, c, face_id,
23036 s->char2b + i, 1);
23037 if (face)
23039 if (! s->face)
23041 s->face = face;
23042 s->font = s->face->font;
23044 else if (s->face != face)
23045 break;
23048 ++s->nchars;
23050 s->cmp_to = i;
23052 if (s->face == NULL)
23054 s->face = base_face->ascii_face;
23055 s->font = s->face->font;
23058 /* All glyph strings for the same composition has the same width,
23059 i.e. the width set for the first component of the composition. */
23060 s->width = s->first_glyph->pixel_width;
23062 /* If the specified font could not be loaded, use the frame's
23063 default font, but record the fact that we couldn't load it in
23064 the glyph string so that we can draw rectangles for the
23065 characters of the glyph string. */
23066 if (s->font == NULL)
23068 s->font_not_found_p = 1;
23069 s->font = FRAME_FONT (s->f);
23072 /* Adjust base line for subscript/superscript text. */
23073 s->ybase += s->first_glyph->voffset;
23075 /* This glyph string must always be drawn with 16-bit functions. */
23076 s->two_byte_p = 1;
23078 return s->cmp_to;
23081 static int
23082 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23083 int start, int end, int overlaps)
23085 struct glyph *glyph, *last;
23086 Lisp_Object lgstring;
23087 int i;
23089 s->for_overlaps = overlaps;
23090 glyph = s->row->glyphs[s->area] + start;
23091 last = s->row->glyphs[s->area] + end;
23092 s->cmp_id = glyph->u.cmp.id;
23093 s->cmp_from = glyph->slice.cmp.from;
23094 s->cmp_to = glyph->slice.cmp.to + 1;
23095 s->face = FACE_FROM_ID (s->f, face_id);
23096 lgstring = composition_gstring_from_id (s->cmp_id);
23097 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23098 glyph++;
23099 while (glyph < last
23100 && glyph->u.cmp.automatic
23101 && glyph->u.cmp.id == s->cmp_id
23102 && s->cmp_to == glyph->slice.cmp.from)
23103 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23105 for (i = s->cmp_from; i < s->cmp_to; i++)
23107 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23108 unsigned code = LGLYPH_CODE (lglyph);
23110 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23112 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23113 return glyph - s->row->glyphs[s->area];
23117 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23118 See the comment of fill_glyph_string for arguments.
23119 Value is the index of the first glyph not in S. */
23122 static int
23123 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23124 int start, int end, int overlaps)
23126 struct glyph *glyph, *last;
23127 int voffset;
23129 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23130 s->for_overlaps = overlaps;
23131 glyph = s->row->glyphs[s->area] + start;
23132 last = s->row->glyphs[s->area] + end;
23133 voffset = glyph->voffset;
23134 s->face = FACE_FROM_ID (s->f, face_id);
23135 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23136 s->nchars = 1;
23137 s->width = glyph->pixel_width;
23138 glyph++;
23139 while (glyph < last
23140 && glyph->type == GLYPHLESS_GLYPH
23141 && glyph->voffset == voffset
23142 && glyph->face_id == face_id)
23144 s->nchars++;
23145 s->width += glyph->pixel_width;
23146 glyph++;
23148 s->ybase += voffset;
23149 return glyph - s->row->glyphs[s->area];
23153 /* Fill glyph string S from a sequence of character glyphs.
23155 FACE_ID is the face id of the string. START is the index of the
23156 first glyph to consider, END is the index of the last + 1.
23157 OVERLAPS non-zero means S should draw the foreground only, and use
23158 its physical height for clipping. See also draw_glyphs.
23160 Value is the index of the first glyph not in S. */
23162 static int
23163 fill_glyph_string (struct glyph_string *s, int face_id,
23164 int start, int end, int overlaps)
23166 struct glyph *glyph, *last;
23167 int voffset;
23168 int glyph_not_available_p;
23170 eassert (s->f == XFRAME (s->w->frame));
23171 eassert (s->nchars == 0);
23172 eassert (start >= 0 && end > start);
23174 s->for_overlaps = overlaps;
23175 glyph = s->row->glyphs[s->area] + start;
23176 last = s->row->glyphs[s->area] + end;
23177 voffset = glyph->voffset;
23178 s->padding_p = glyph->padding_p;
23179 glyph_not_available_p = glyph->glyph_not_available_p;
23181 while (glyph < last
23182 && glyph->type == CHAR_GLYPH
23183 && glyph->voffset == voffset
23184 /* Same face id implies same font, nowadays. */
23185 && glyph->face_id == face_id
23186 && glyph->glyph_not_available_p == glyph_not_available_p)
23188 int two_byte_p;
23190 s->face = get_glyph_face_and_encoding (s->f, glyph,
23191 s->char2b + s->nchars,
23192 &two_byte_p);
23193 s->two_byte_p = two_byte_p;
23194 ++s->nchars;
23195 eassert (s->nchars <= end - start);
23196 s->width += glyph->pixel_width;
23197 if (glyph++->padding_p != s->padding_p)
23198 break;
23201 s->font = s->face->font;
23203 /* If the specified font could not be loaded, use the frame's font,
23204 but record the fact that we couldn't load it in
23205 S->font_not_found_p so that we can draw rectangles for the
23206 characters of the glyph string. */
23207 if (s->font == NULL || glyph_not_available_p)
23209 s->font_not_found_p = 1;
23210 s->font = FRAME_FONT (s->f);
23213 /* Adjust base line for subscript/superscript text. */
23214 s->ybase += voffset;
23216 eassert (s->face && s->face->gc);
23217 return glyph - s->row->glyphs[s->area];
23221 /* Fill glyph string S from image glyph S->first_glyph. */
23223 static void
23224 fill_image_glyph_string (struct glyph_string *s)
23226 eassert (s->first_glyph->type == IMAGE_GLYPH);
23227 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23228 eassert (s->img);
23229 s->slice = s->first_glyph->slice.img;
23230 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23231 s->font = s->face->font;
23232 s->width = s->first_glyph->pixel_width;
23234 /* Adjust base line for subscript/superscript text. */
23235 s->ybase += s->first_glyph->voffset;
23239 /* Fill glyph string S from a sequence of stretch glyphs.
23241 START is the index of the first glyph to consider,
23242 END is the index of the last + 1.
23244 Value is the index of the first glyph not in S. */
23246 static int
23247 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23249 struct glyph *glyph, *last;
23250 int voffset, face_id;
23252 eassert (s->first_glyph->type == STRETCH_GLYPH);
23254 glyph = s->row->glyphs[s->area] + start;
23255 last = s->row->glyphs[s->area] + end;
23256 face_id = glyph->face_id;
23257 s->face = FACE_FROM_ID (s->f, face_id);
23258 s->font = s->face->font;
23259 s->width = glyph->pixel_width;
23260 s->nchars = 1;
23261 voffset = glyph->voffset;
23263 for (++glyph;
23264 (glyph < last
23265 && glyph->type == STRETCH_GLYPH
23266 && glyph->voffset == voffset
23267 && glyph->face_id == face_id);
23268 ++glyph)
23269 s->width += glyph->pixel_width;
23271 /* Adjust base line for subscript/superscript text. */
23272 s->ybase += voffset;
23274 /* The case that face->gc == 0 is handled when drawing the glyph
23275 string by calling PREPARE_FACE_FOR_DISPLAY. */
23276 eassert (s->face);
23277 return glyph - s->row->glyphs[s->area];
23280 static struct font_metrics *
23281 get_per_char_metric (struct font *font, XChar2b *char2b)
23283 static struct font_metrics metrics;
23284 unsigned code;
23286 if (! font)
23287 return NULL;
23288 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23289 if (code == FONT_INVALID_CODE)
23290 return NULL;
23291 font->driver->text_extents (font, &code, 1, &metrics);
23292 return &metrics;
23295 /* EXPORT for RIF:
23296 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23297 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23298 assumed to be zero. */
23300 void
23301 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23303 *left = *right = 0;
23305 if (glyph->type == CHAR_GLYPH)
23307 struct face *face;
23308 XChar2b char2b;
23309 struct font_metrics *pcm;
23311 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23312 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23314 if (pcm->rbearing > pcm->width)
23315 *right = pcm->rbearing - pcm->width;
23316 if (pcm->lbearing < 0)
23317 *left = -pcm->lbearing;
23320 else if (glyph->type == COMPOSITE_GLYPH)
23322 if (! glyph->u.cmp.automatic)
23324 struct composition *cmp = composition_table[glyph->u.cmp.id];
23326 if (cmp->rbearing > cmp->pixel_width)
23327 *right = cmp->rbearing - cmp->pixel_width;
23328 if (cmp->lbearing < 0)
23329 *left = - cmp->lbearing;
23331 else
23333 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23334 struct font_metrics metrics;
23336 composition_gstring_width (gstring, glyph->slice.cmp.from,
23337 glyph->slice.cmp.to + 1, &metrics);
23338 if (metrics.rbearing > metrics.width)
23339 *right = metrics.rbearing - metrics.width;
23340 if (metrics.lbearing < 0)
23341 *left = - metrics.lbearing;
23347 /* Return the index of the first glyph preceding glyph string S that
23348 is overwritten by S because of S's left overhang. Value is -1
23349 if no glyphs are overwritten. */
23351 static int
23352 left_overwritten (struct glyph_string *s)
23354 int k;
23356 if (s->left_overhang)
23358 int x = 0, i;
23359 struct glyph *glyphs = s->row->glyphs[s->area];
23360 int first = s->first_glyph - glyphs;
23362 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23363 x -= glyphs[i].pixel_width;
23365 k = i + 1;
23367 else
23368 k = -1;
23370 return k;
23374 /* Return the index of the first glyph preceding glyph string S that
23375 is overwriting S because of its right overhang. Value is -1 if no
23376 glyph in front of S overwrites S. */
23378 static int
23379 left_overwriting (struct glyph_string *s)
23381 int i, k, x;
23382 struct glyph *glyphs = s->row->glyphs[s->area];
23383 int first = s->first_glyph - glyphs;
23385 k = -1;
23386 x = 0;
23387 for (i = first - 1; i >= 0; --i)
23389 int left, right;
23390 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23391 if (x + right > 0)
23392 k = i;
23393 x -= glyphs[i].pixel_width;
23396 return k;
23400 /* Return the index of the last glyph following glyph string S that is
23401 overwritten by S because of S's right overhang. Value is -1 if
23402 no such glyph is found. */
23404 static int
23405 right_overwritten (struct glyph_string *s)
23407 int k = -1;
23409 if (s->right_overhang)
23411 int x = 0, i;
23412 struct glyph *glyphs = s->row->glyphs[s->area];
23413 int first = (s->first_glyph - glyphs
23414 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23415 int end = s->row->used[s->area];
23417 for (i = first; i < end && s->right_overhang > x; ++i)
23418 x += glyphs[i].pixel_width;
23420 k = i;
23423 return k;
23427 /* Return the index of the last glyph following glyph string S that
23428 overwrites S because of its left overhang. Value is negative
23429 if no such glyph is found. */
23431 static int
23432 right_overwriting (struct glyph_string *s)
23434 int i, k, x;
23435 int end = s->row->used[s->area];
23436 struct glyph *glyphs = s->row->glyphs[s->area];
23437 int first = (s->first_glyph - glyphs
23438 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23440 k = -1;
23441 x = 0;
23442 for (i = first; i < end; ++i)
23444 int left, right;
23445 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23446 if (x - left < 0)
23447 k = i;
23448 x += glyphs[i].pixel_width;
23451 return k;
23455 /* Set background width of glyph string S. START is the index of the
23456 first glyph following S. LAST_X is the right-most x-position + 1
23457 in the drawing area. */
23459 static void
23460 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23462 /* If the face of this glyph string has to be drawn to the end of
23463 the drawing area, set S->extends_to_end_of_line_p. */
23465 if (start == s->row->used[s->area]
23466 && s->area == TEXT_AREA
23467 && ((s->row->fill_line_p
23468 && (s->hl == DRAW_NORMAL_TEXT
23469 || s->hl == DRAW_IMAGE_RAISED
23470 || s->hl == DRAW_IMAGE_SUNKEN))
23471 || s->hl == DRAW_MOUSE_FACE))
23472 s->extends_to_end_of_line_p = 1;
23474 /* If S extends its face to the end of the line, set its
23475 background_width to the distance to the right edge of the drawing
23476 area. */
23477 if (s->extends_to_end_of_line_p)
23478 s->background_width = last_x - s->x + 1;
23479 else
23480 s->background_width = s->width;
23484 /* Compute overhangs and x-positions for glyph string S and its
23485 predecessors, or successors. X is the starting x-position for S.
23486 BACKWARD_P non-zero means process predecessors. */
23488 static void
23489 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23491 if (backward_p)
23493 while (s)
23495 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23496 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23497 x -= s->width;
23498 s->x = x;
23499 s = s->prev;
23502 else
23504 while (s)
23506 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23507 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23508 s->x = x;
23509 x += s->width;
23510 s = s->next;
23517 /* The following macros are only called from draw_glyphs below.
23518 They reference the following parameters of that function directly:
23519 `w', `row', `area', and `overlap_p'
23520 as well as the following local variables:
23521 `s', `f', and `hdc' (in W32) */
23523 #ifdef HAVE_NTGUI
23524 /* On W32, silently add local `hdc' variable to argument list of
23525 init_glyph_string. */
23526 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23527 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23528 #else
23529 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23530 init_glyph_string (s, char2b, w, row, area, start, hl)
23531 #endif
23533 /* Add a glyph string for a stretch glyph to the list of strings
23534 between HEAD and TAIL. START is the index of the stretch glyph in
23535 row area AREA of glyph row ROW. END is the index of the last glyph
23536 in that glyph row area. X is the current output position assigned
23537 to the new glyph string constructed. HL overrides that face of the
23538 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23539 is the right-most x-position of the drawing area. */
23541 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23542 and below -- keep them on one line. */
23543 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23544 do \
23546 s = alloca (sizeof *s); \
23547 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23548 START = fill_stretch_glyph_string (s, START, END); \
23549 append_glyph_string (&HEAD, &TAIL, s); \
23550 s->x = (X); \
23552 while (0)
23555 /* Add a glyph string for an image glyph to the list of strings
23556 between HEAD and TAIL. START is the index of the image glyph in
23557 row area AREA of glyph row ROW. END is the index of the last glyph
23558 in that glyph row area. X is the current output position assigned
23559 to the new glyph string constructed. HL overrides that face of the
23560 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23561 is the right-most x-position of the drawing area. */
23563 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23564 do \
23566 s = alloca (sizeof *s); \
23567 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23568 fill_image_glyph_string (s); \
23569 append_glyph_string (&HEAD, &TAIL, s); \
23570 ++START; \
23571 s->x = (X); \
23573 while (0)
23576 /* Add a glyph string for a sequence of character glyphs to the list
23577 of strings between HEAD and TAIL. START is the index of the first
23578 glyph in row area AREA of glyph row ROW that is part of the new
23579 glyph string. END is the index of the last glyph in that glyph row
23580 area. X is the current output position assigned to the new glyph
23581 string constructed. HL overrides that face of the glyph; e.g. it
23582 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23583 right-most x-position of the drawing area. */
23585 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23586 do \
23588 int face_id; \
23589 XChar2b *char2b; \
23591 face_id = (row)->glyphs[area][START].face_id; \
23593 s = alloca (sizeof *s); \
23594 char2b = alloca ((END - START) * sizeof *char2b); \
23595 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23596 append_glyph_string (&HEAD, &TAIL, s); \
23597 s->x = (X); \
23598 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23600 while (0)
23603 /* Add a glyph string for a composite sequence to the list of strings
23604 between HEAD and TAIL. START is the index of the first glyph in
23605 row area AREA of glyph row ROW that is part of the new glyph
23606 string. END is the index of the last glyph in that glyph row area.
23607 X is the current output position assigned to the new glyph string
23608 constructed. HL overrides that face of the glyph; e.g. it is
23609 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23610 x-position of the drawing area. */
23612 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23613 do { \
23614 int face_id = (row)->glyphs[area][START].face_id; \
23615 struct face *base_face = FACE_FROM_ID (f, face_id); \
23616 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23617 struct composition *cmp = composition_table[cmp_id]; \
23618 XChar2b *char2b; \
23619 struct glyph_string *first_s = NULL; \
23620 int n; \
23622 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23624 /* Make glyph_strings for each glyph sequence that is drawable by \
23625 the same face, and append them to HEAD/TAIL. */ \
23626 for (n = 0; n < cmp->glyph_len;) \
23628 s = alloca (sizeof *s); \
23629 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23630 append_glyph_string (&(HEAD), &(TAIL), s); \
23631 s->cmp = cmp; \
23632 s->cmp_from = n; \
23633 s->x = (X); \
23634 if (n == 0) \
23635 first_s = s; \
23636 n = fill_composite_glyph_string (s, base_face, overlaps); \
23639 ++START; \
23640 s = first_s; \
23641 } while (0)
23644 /* Add a glyph string for a glyph-string sequence to the list of strings
23645 between HEAD and TAIL. */
23647 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23648 do { \
23649 int face_id; \
23650 XChar2b *char2b; \
23651 Lisp_Object gstring; \
23653 face_id = (row)->glyphs[area][START].face_id; \
23654 gstring = (composition_gstring_from_id \
23655 ((row)->glyphs[area][START].u.cmp.id)); \
23656 s = alloca (sizeof *s); \
23657 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23658 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23659 append_glyph_string (&(HEAD), &(TAIL), s); \
23660 s->x = (X); \
23661 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23662 } while (0)
23665 /* Add a glyph string for a sequence of glyphless character's glyphs
23666 to the list of strings between HEAD and TAIL. The meanings of
23667 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23669 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23670 do \
23672 int face_id; \
23674 face_id = (row)->glyphs[area][START].face_id; \
23676 s = alloca (sizeof *s); \
23677 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23678 append_glyph_string (&HEAD, &TAIL, s); \
23679 s->x = (X); \
23680 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23681 overlaps); \
23683 while (0)
23686 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23687 of AREA of glyph row ROW on window W between indices START and END.
23688 HL overrides the face for drawing glyph strings, e.g. it is
23689 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23690 x-positions of the drawing area.
23692 This is an ugly monster macro construct because we must use alloca
23693 to allocate glyph strings (because draw_glyphs can be called
23694 asynchronously). */
23696 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23697 do \
23699 HEAD = TAIL = NULL; \
23700 while (START < END) \
23702 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23703 switch (first_glyph->type) \
23705 case CHAR_GLYPH: \
23706 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23707 HL, X, LAST_X); \
23708 break; \
23710 case COMPOSITE_GLYPH: \
23711 if (first_glyph->u.cmp.automatic) \
23712 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23713 HL, X, LAST_X); \
23714 else \
23715 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23716 HL, X, LAST_X); \
23717 break; \
23719 case STRETCH_GLYPH: \
23720 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23721 HL, X, LAST_X); \
23722 break; \
23724 case IMAGE_GLYPH: \
23725 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23726 HL, X, LAST_X); \
23727 break; \
23729 case GLYPHLESS_GLYPH: \
23730 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23731 HL, X, LAST_X); \
23732 break; \
23734 default: \
23735 emacs_abort (); \
23738 if (s) \
23740 set_glyph_string_background_width (s, START, LAST_X); \
23741 (X) += s->width; \
23744 } while (0)
23747 /* Draw glyphs between START and END in AREA of ROW on window W,
23748 starting at x-position X. X is relative to AREA in W. HL is a
23749 face-override with the following meaning:
23751 DRAW_NORMAL_TEXT draw normally
23752 DRAW_CURSOR draw in cursor face
23753 DRAW_MOUSE_FACE draw in mouse face.
23754 DRAW_INVERSE_VIDEO draw in mode line face
23755 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23756 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23758 If OVERLAPS is non-zero, draw only the foreground of characters and
23759 clip to the physical height of ROW. Non-zero value also defines
23760 the overlapping part to be drawn:
23762 OVERLAPS_PRED overlap with preceding rows
23763 OVERLAPS_SUCC overlap with succeeding rows
23764 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23765 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23767 Value is the x-position reached, relative to AREA of W. */
23769 static int
23770 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23771 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23772 enum draw_glyphs_face hl, int overlaps)
23774 struct glyph_string *head, *tail;
23775 struct glyph_string *s;
23776 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23777 int i, j, x_reached, last_x, area_left = 0;
23778 struct frame *f = XFRAME (WINDOW_FRAME (w));
23779 DECLARE_HDC (hdc);
23781 ALLOCATE_HDC (hdc, f);
23783 /* Let's rather be paranoid than getting a SEGV. */
23784 end = min (end, row->used[area]);
23785 start = clip_to_bounds (0, start, end);
23787 /* Translate X to frame coordinates. Set last_x to the right
23788 end of the drawing area. */
23789 if (row->full_width_p)
23791 /* X is relative to the left edge of W, without scroll bars
23792 or fringes. */
23793 area_left = WINDOW_LEFT_EDGE_X (w);
23794 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23796 else
23798 area_left = window_box_left (w, area);
23799 last_x = area_left + window_box_width (w, area);
23801 x += area_left;
23803 /* Build a doubly-linked list of glyph_string structures between
23804 head and tail from what we have to draw. Note that the macro
23805 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23806 the reason we use a separate variable `i'. */
23807 i = start;
23808 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23809 if (tail)
23810 x_reached = tail->x + tail->background_width;
23811 else
23812 x_reached = x;
23814 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23815 the row, redraw some glyphs in front or following the glyph
23816 strings built above. */
23817 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23819 struct glyph_string *h, *t;
23820 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23821 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23822 int check_mouse_face = 0;
23823 int dummy_x = 0;
23825 /* If mouse highlighting is on, we may need to draw adjacent
23826 glyphs using mouse-face highlighting. */
23827 if (area == TEXT_AREA && row->mouse_face_p
23828 && hlinfo->mouse_face_beg_row >= 0
23829 && hlinfo->mouse_face_end_row >= 0)
23831 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23833 if (row_vpos >= hlinfo->mouse_face_beg_row
23834 && row_vpos <= hlinfo->mouse_face_end_row)
23836 check_mouse_face = 1;
23837 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23838 ? hlinfo->mouse_face_beg_col : 0;
23839 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23840 ? hlinfo->mouse_face_end_col
23841 : row->used[TEXT_AREA];
23845 /* Compute overhangs for all glyph strings. */
23846 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23847 for (s = head; s; s = s->next)
23848 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23850 /* Prepend glyph strings for glyphs in front of the first glyph
23851 string that are overwritten because of the first glyph
23852 string's left overhang. The background of all strings
23853 prepended must be drawn because the first glyph string
23854 draws over it. */
23855 i = left_overwritten (head);
23856 if (i >= 0)
23858 enum draw_glyphs_face overlap_hl;
23860 /* If this row contains mouse highlighting, attempt to draw
23861 the overlapped glyphs with the correct highlight. This
23862 code fails if the overlap encompasses more than one glyph
23863 and mouse-highlight spans only some of these glyphs.
23864 However, making it work perfectly involves a lot more
23865 code, and I don't know if the pathological case occurs in
23866 practice, so we'll stick to this for now. --- cyd */
23867 if (check_mouse_face
23868 && mouse_beg_col < start && mouse_end_col > i)
23869 overlap_hl = DRAW_MOUSE_FACE;
23870 else
23871 overlap_hl = DRAW_NORMAL_TEXT;
23873 j = i;
23874 BUILD_GLYPH_STRINGS (j, start, h, t,
23875 overlap_hl, dummy_x, last_x);
23876 start = i;
23877 compute_overhangs_and_x (t, head->x, 1);
23878 prepend_glyph_string_lists (&head, &tail, h, t);
23879 clip_head = head;
23882 /* Prepend glyph strings for glyphs in front of the first glyph
23883 string that overwrite that glyph string because of their
23884 right overhang. For these strings, only the foreground must
23885 be drawn, because it draws over the glyph string at `head'.
23886 The background must not be drawn because this would overwrite
23887 right overhangs of preceding glyphs for which no glyph
23888 strings exist. */
23889 i = left_overwriting (head);
23890 if (i >= 0)
23892 enum draw_glyphs_face overlap_hl;
23894 if (check_mouse_face
23895 && mouse_beg_col < start && mouse_end_col > i)
23896 overlap_hl = DRAW_MOUSE_FACE;
23897 else
23898 overlap_hl = DRAW_NORMAL_TEXT;
23900 clip_head = head;
23901 BUILD_GLYPH_STRINGS (i, start, h, t,
23902 overlap_hl, dummy_x, last_x);
23903 for (s = h; s; s = s->next)
23904 s->background_filled_p = 1;
23905 compute_overhangs_and_x (t, head->x, 1);
23906 prepend_glyph_string_lists (&head, &tail, h, t);
23909 /* Append glyphs strings for glyphs following the last glyph
23910 string tail that are overwritten by tail. The background of
23911 these strings has to be drawn because tail's foreground draws
23912 over it. */
23913 i = right_overwritten (tail);
23914 if (i >= 0)
23916 enum draw_glyphs_face overlap_hl;
23918 if (check_mouse_face
23919 && mouse_beg_col < i && mouse_end_col > end)
23920 overlap_hl = DRAW_MOUSE_FACE;
23921 else
23922 overlap_hl = DRAW_NORMAL_TEXT;
23924 BUILD_GLYPH_STRINGS (end, i, h, t,
23925 overlap_hl, x, last_x);
23926 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23927 we don't have `end = i;' here. */
23928 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23929 append_glyph_string_lists (&head, &tail, h, t);
23930 clip_tail = tail;
23933 /* Append glyph strings for glyphs following the last glyph
23934 string tail that overwrite tail. The foreground of such
23935 glyphs has to be drawn because it writes into the background
23936 of tail. The background must not be drawn because it could
23937 paint over the foreground of following glyphs. */
23938 i = right_overwriting (tail);
23939 if (i >= 0)
23941 enum draw_glyphs_face overlap_hl;
23942 if (check_mouse_face
23943 && mouse_beg_col < i && mouse_end_col > end)
23944 overlap_hl = DRAW_MOUSE_FACE;
23945 else
23946 overlap_hl = DRAW_NORMAL_TEXT;
23948 clip_tail = tail;
23949 i++; /* We must include the Ith glyph. */
23950 BUILD_GLYPH_STRINGS (end, i, h, t,
23951 overlap_hl, x, last_x);
23952 for (s = h; s; s = s->next)
23953 s->background_filled_p = 1;
23954 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23955 append_glyph_string_lists (&head, &tail, h, t);
23957 if (clip_head || clip_tail)
23958 for (s = head; s; s = s->next)
23960 s->clip_head = clip_head;
23961 s->clip_tail = clip_tail;
23965 /* Draw all strings. */
23966 for (s = head; s; s = s->next)
23967 FRAME_RIF (f)->draw_glyph_string (s);
23969 #ifndef HAVE_NS
23970 /* When focus a sole frame and move horizontally, this sets on_p to 0
23971 causing a failure to erase prev cursor position. */
23972 if (area == TEXT_AREA
23973 && !row->full_width_p
23974 /* When drawing overlapping rows, only the glyph strings'
23975 foreground is drawn, which doesn't erase a cursor
23976 completely. */
23977 && !overlaps)
23979 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23980 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23981 : (tail ? tail->x + tail->background_width : x));
23982 x0 -= area_left;
23983 x1 -= area_left;
23985 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23986 row->y, MATRIX_ROW_BOTTOM_Y (row));
23988 #endif
23990 /* Value is the x-position up to which drawn, relative to AREA of W.
23991 This doesn't include parts drawn because of overhangs. */
23992 if (row->full_width_p)
23993 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23994 else
23995 x_reached -= area_left;
23997 RELEASE_HDC (hdc, f);
23999 return x_reached;
24002 /* Expand row matrix if too narrow. Don't expand if area
24003 is not present. */
24005 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24007 if (!it->f->fonts_changed \
24008 && (it->glyph_row->glyphs[area] \
24009 < it->glyph_row->glyphs[area + 1])) \
24011 it->w->ncols_scale_factor++; \
24012 it->f->fonts_changed = 1; \
24016 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24017 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24019 static void
24020 append_glyph (struct it *it)
24022 struct glyph *glyph;
24023 enum glyph_row_area area = it->area;
24025 eassert (it->glyph_row);
24026 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24028 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24029 if (glyph < it->glyph_row->glyphs[area + 1])
24031 /* If the glyph row is reversed, we need to prepend the glyph
24032 rather than append it. */
24033 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24035 struct glyph *g;
24037 /* Make room for the additional glyph. */
24038 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24039 g[1] = *g;
24040 glyph = it->glyph_row->glyphs[area];
24042 glyph->charpos = CHARPOS (it->position);
24043 glyph->object = it->object;
24044 if (it->pixel_width > 0)
24046 glyph->pixel_width = it->pixel_width;
24047 glyph->padding_p = 0;
24049 else
24051 /* Assure at least 1-pixel width. Otherwise, cursor can't
24052 be displayed correctly. */
24053 glyph->pixel_width = 1;
24054 glyph->padding_p = 1;
24056 glyph->ascent = it->ascent;
24057 glyph->descent = it->descent;
24058 glyph->voffset = it->voffset;
24059 glyph->type = CHAR_GLYPH;
24060 glyph->avoid_cursor_p = it->avoid_cursor_p;
24061 glyph->multibyte_p = it->multibyte_p;
24062 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24064 /* In R2L rows, the left and the right box edges need to be
24065 drawn in reverse direction. */
24066 glyph->right_box_line_p = it->start_of_box_run_p;
24067 glyph->left_box_line_p = it->end_of_box_run_p;
24069 else
24071 glyph->left_box_line_p = it->start_of_box_run_p;
24072 glyph->right_box_line_p = it->end_of_box_run_p;
24074 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24075 || it->phys_descent > it->descent);
24076 glyph->glyph_not_available_p = it->glyph_not_available_p;
24077 glyph->face_id = it->face_id;
24078 glyph->u.ch = it->char_to_display;
24079 glyph->slice.img = null_glyph_slice;
24080 glyph->font_type = FONT_TYPE_UNKNOWN;
24081 if (it->bidi_p)
24083 glyph->resolved_level = it->bidi_it.resolved_level;
24084 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24085 emacs_abort ();
24086 glyph->bidi_type = it->bidi_it.type;
24088 else
24090 glyph->resolved_level = 0;
24091 glyph->bidi_type = UNKNOWN_BT;
24093 ++it->glyph_row->used[area];
24095 else
24096 IT_EXPAND_MATRIX_WIDTH (it, area);
24099 /* Store one glyph for the composition IT->cmp_it.id in
24100 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24101 non-null. */
24103 static void
24104 append_composite_glyph (struct it *it)
24106 struct glyph *glyph;
24107 enum glyph_row_area area = it->area;
24109 eassert (it->glyph_row);
24111 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24112 if (glyph < it->glyph_row->glyphs[area + 1])
24114 /* If the glyph row is reversed, we need to prepend the glyph
24115 rather than append it. */
24116 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24118 struct glyph *g;
24120 /* Make room for the new glyph. */
24121 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24122 g[1] = *g;
24123 glyph = it->glyph_row->glyphs[it->area];
24125 glyph->charpos = it->cmp_it.charpos;
24126 glyph->object = it->object;
24127 glyph->pixel_width = it->pixel_width;
24128 glyph->ascent = it->ascent;
24129 glyph->descent = it->descent;
24130 glyph->voffset = it->voffset;
24131 glyph->type = COMPOSITE_GLYPH;
24132 if (it->cmp_it.ch < 0)
24134 glyph->u.cmp.automatic = 0;
24135 glyph->u.cmp.id = it->cmp_it.id;
24136 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24138 else
24140 glyph->u.cmp.automatic = 1;
24141 glyph->u.cmp.id = it->cmp_it.id;
24142 glyph->slice.cmp.from = it->cmp_it.from;
24143 glyph->slice.cmp.to = it->cmp_it.to - 1;
24145 glyph->avoid_cursor_p = it->avoid_cursor_p;
24146 glyph->multibyte_p = it->multibyte_p;
24147 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24149 /* In R2L rows, the left and the right box edges need to be
24150 drawn in reverse direction. */
24151 glyph->right_box_line_p = it->start_of_box_run_p;
24152 glyph->left_box_line_p = it->end_of_box_run_p;
24154 else
24156 glyph->left_box_line_p = it->start_of_box_run_p;
24157 glyph->right_box_line_p = it->end_of_box_run_p;
24159 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24160 || it->phys_descent > it->descent);
24161 glyph->padding_p = 0;
24162 glyph->glyph_not_available_p = 0;
24163 glyph->face_id = it->face_id;
24164 glyph->font_type = FONT_TYPE_UNKNOWN;
24165 if (it->bidi_p)
24167 glyph->resolved_level = it->bidi_it.resolved_level;
24168 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24169 emacs_abort ();
24170 glyph->bidi_type = it->bidi_it.type;
24172 ++it->glyph_row->used[area];
24174 else
24175 IT_EXPAND_MATRIX_WIDTH (it, area);
24179 /* Change IT->ascent and IT->height according to the setting of
24180 IT->voffset. */
24182 static void
24183 take_vertical_position_into_account (struct it *it)
24185 if (it->voffset)
24187 if (it->voffset < 0)
24188 /* Increase the ascent so that we can display the text higher
24189 in the line. */
24190 it->ascent -= it->voffset;
24191 else
24192 /* Increase the descent so that we can display the text lower
24193 in the line. */
24194 it->descent += it->voffset;
24199 /* Produce glyphs/get display metrics for the image IT is loaded with.
24200 See the description of struct display_iterator in dispextern.h for
24201 an overview of struct display_iterator. */
24203 static void
24204 produce_image_glyph (struct it *it)
24206 struct image *img;
24207 struct face *face;
24208 int glyph_ascent, crop;
24209 struct glyph_slice slice;
24211 eassert (it->what == IT_IMAGE);
24213 face = FACE_FROM_ID (it->f, it->face_id);
24214 eassert (face);
24215 /* Make sure X resources of the face is loaded. */
24216 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24218 if (it->image_id < 0)
24220 /* Fringe bitmap. */
24221 it->ascent = it->phys_ascent = 0;
24222 it->descent = it->phys_descent = 0;
24223 it->pixel_width = 0;
24224 it->nglyphs = 0;
24225 return;
24228 img = IMAGE_FROM_ID (it->f, it->image_id);
24229 eassert (img);
24230 /* Make sure X resources of the image is loaded. */
24231 prepare_image_for_display (it->f, img);
24233 slice.x = slice.y = 0;
24234 slice.width = img->width;
24235 slice.height = img->height;
24237 if (INTEGERP (it->slice.x))
24238 slice.x = XINT (it->slice.x);
24239 else if (FLOATP (it->slice.x))
24240 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24242 if (INTEGERP (it->slice.y))
24243 slice.y = XINT (it->slice.y);
24244 else if (FLOATP (it->slice.y))
24245 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24247 if (INTEGERP (it->slice.width))
24248 slice.width = XINT (it->slice.width);
24249 else if (FLOATP (it->slice.width))
24250 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24252 if (INTEGERP (it->slice.height))
24253 slice.height = XINT (it->slice.height);
24254 else if (FLOATP (it->slice.height))
24255 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24257 if (slice.x >= img->width)
24258 slice.x = img->width;
24259 if (slice.y >= img->height)
24260 slice.y = img->height;
24261 if (slice.x + slice.width >= img->width)
24262 slice.width = img->width - slice.x;
24263 if (slice.y + slice.height > img->height)
24264 slice.height = img->height - slice.y;
24266 if (slice.width == 0 || slice.height == 0)
24267 return;
24269 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24271 it->descent = slice.height - glyph_ascent;
24272 if (slice.y == 0)
24273 it->descent += img->vmargin;
24274 if (slice.y + slice.height == img->height)
24275 it->descent += img->vmargin;
24276 it->phys_descent = it->descent;
24278 it->pixel_width = slice.width;
24279 if (slice.x == 0)
24280 it->pixel_width += img->hmargin;
24281 if (slice.x + slice.width == img->width)
24282 it->pixel_width += img->hmargin;
24284 /* It's quite possible for images to have an ascent greater than
24285 their height, so don't get confused in that case. */
24286 if (it->descent < 0)
24287 it->descent = 0;
24289 it->nglyphs = 1;
24291 if (face->box != FACE_NO_BOX)
24293 if (face->box_line_width > 0)
24295 if (slice.y == 0)
24296 it->ascent += face->box_line_width;
24297 if (slice.y + slice.height == img->height)
24298 it->descent += face->box_line_width;
24301 if (it->start_of_box_run_p && slice.x == 0)
24302 it->pixel_width += eabs (face->box_line_width);
24303 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24304 it->pixel_width += eabs (face->box_line_width);
24307 take_vertical_position_into_account (it);
24309 /* Automatically crop wide image glyphs at right edge so we can
24310 draw the cursor on same display row. */
24311 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24312 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24314 it->pixel_width -= crop;
24315 slice.width -= crop;
24318 if (it->glyph_row)
24320 struct glyph *glyph;
24321 enum glyph_row_area area = it->area;
24323 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24324 if (glyph < it->glyph_row->glyphs[area + 1])
24326 glyph->charpos = CHARPOS (it->position);
24327 glyph->object = it->object;
24328 glyph->pixel_width = it->pixel_width;
24329 glyph->ascent = glyph_ascent;
24330 glyph->descent = it->descent;
24331 glyph->voffset = it->voffset;
24332 glyph->type = IMAGE_GLYPH;
24333 glyph->avoid_cursor_p = it->avoid_cursor_p;
24334 glyph->multibyte_p = it->multibyte_p;
24335 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24337 /* In R2L rows, the left and the right box edges need to be
24338 drawn in reverse direction. */
24339 glyph->right_box_line_p = it->start_of_box_run_p;
24340 glyph->left_box_line_p = it->end_of_box_run_p;
24342 else
24344 glyph->left_box_line_p = it->start_of_box_run_p;
24345 glyph->right_box_line_p = it->end_of_box_run_p;
24347 glyph->overlaps_vertically_p = 0;
24348 glyph->padding_p = 0;
24349 glyph->glyph_not_available_p = 0;
24350 glyph->face_id = it->face_id;
24351 glyph->u.img_id = img->id;
24352 glyph->slice.img = slice;
24353 glyph->font_type = FONT_TYPE_UNKNOWN;
24354 if (it->bidi_p)
24356 glyph->resolved_level = it->bidi_it.resolved_level;
24357 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24358 emacs_abort ();
24359 glyph->bidi_type = it->bidi_it.type;
24361 ++it->glyph_row->used[area];
24363 else
24364 IT_EXPAND_MATRIX_WIDTH (it, area);
24369 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24370 of the glyph, WIDTH and HEIGHT are the width and height of the
24371 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24373 static void
24374 append_stretch_glyph (struct it *it, Lisp_Object object,
24375 int width, int height, int ascent)
24377 struct glyph *glyph;
24378 enum glyph_row_area area = it->area;
24380 eassert (ascent >= 0 && ascent <= height);
24382 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24383 if (glyph < it->glyph_row->glyphs[area + 1])
24385 /* If the glyph row is reversed, we need to prepend the glyph
24386 rather than append it. */
24387 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24389 struct glyph *g;
24391 /* Make room for the additional glyph. */
24392 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24393 g[1] = *g;
24394 glyph = it->glyph_row->glyphs[area];
24396 glyph->charpos = CHARPOS (it->position);
24397 glyph->object = object;
24398 glyph->pixel_width = width;
24399 glyph->ascent = ascent;
24400 glyph->descent = height - ascent;
24401 glyph->voffset = it->voffset;
24402 glyph->type = STRETCH_GLYPH;
24403 glyph->avoid_cursor_p = it->avoid_cursor_p;
24404 glyph->multibyte_p = it->multibyte_p;
24405 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24407 /* In R2L rows, the left and the right box edges need to be
24408 drawn in reverse direction. */
24409 glyph->right_box_line_p = it->start_of_box_run_p;
24410 glyph->left_box_line_p = it->end_of_box_run_p;
24412 else
24414 glyph->left_box_line_p = it->start_of_box_run_p;
24415 glyph->right_box_line_p = it->end_of_box_run_p;
24417 glyph->overlaps_vertically_p = 0;
24418 glyph->padding_p = 0;
24419 glyph->glyph_not_available_p = 0;
24420 glyph->face_id = it->face_id;
24421 glyph->u.stretch.ascent = ascent;
24422 glyph->u.stretch.height = height;
24423 glyph->slice.img = null_glyph_slice;
24424 glyph->font_type = FONT_TYPE_UNKNOWN;
24425 if (it->bidi_p)
24427 glyph->resolved_level = it->bidi_it.resolved_level;
24428 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24429 emacs_abort ();
24430 glyph->bidi_type = it->bidi_it.type;
24432 else
24434 glyph->resolved_level = 0;
24435 glyph->bidi_type = UNKNOWN_BT;
24437 ++it->glyph_row->used[area];
24439 else
24440 IT_EXPAND_MATRIX_WIDTH (it, area);
24443 #endif /* HAVE_WINDOW_SYSTEM */
24445 /* Produce a stretch glyph for iterator IT. IT->object is the value
24446 of the glyph property displayed. The value must be a list
24447 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24448 being recognized:
24450 1. `:width WIDTH' specifies that the space should be WIDTH *
24451 canonical char width wide. WIDTH may be an integer or floating
24452 point number.
24454 2. `:relative-width FACTOR' specifies that the width of the stretch
24455 should be computed from the width of the first character having the
24456 `glyph' property, and should be FACTOR times that width.
24458 3. `:align-to HPOS' specifies that the space should be wide enough
24459 to reach HPOS, a value in canonical character units.
24461 Exactly one of the above pairs must be present.
24463 4. `:height HEIGHT' specifies that the height of the stretch produced
24464 should be HEIGHT, measured in canonical character units.
24466 5. `:relative-height FACTOR' specifies that the height of the
24467 stretch should be FACTOR times the height of the characters having
24468 the glyph property.
24470 Either none or exactly one of 4 or 5 must be present.
24472 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24473 of the stretch should be used for the ascent of the stretch.
24474 ASCENT must be in the range 0 <= ASCENT <= 100. */
24476 void
24477 produce_stretch_glyph (struct it *it)
24479 /* (space :width WIDTH :height HEIGHT ...) */
24480 Lisp_Object prop, plist;
24481 int width = 0, height = 0, align_to = -1;
24482 int zero_width_ok_p = 0;
24483 double tem;
24484 struct font *font = NULL;
24486 #ifdef HAVE_WINDOW_SYSTEM
24487 int ascent = 0;
24488 int zero_height_ok_p = 0;
24490 if (FRAME_WINDOW_P (it->f))
24492 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24493 font = face->font ? face->font : FRAME_FONT (it->f);
24494 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24496 #endif
24498 /* List should start with `space'. */
24499 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24500 plist = XCDR (it->object);
24502 /* Compute the width of the stretch. */
24503 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24504 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24506 /* Absolute width `:width WIDTH' specified and valid. */
24507 zero_width_ok_p = 1;
24508 width = (int)tem;
24510 #ifdef HAVE_WINDOW_SYSTEM
24511 else if (FRAME_WINDOW_P (it->f)
24512 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24514 /* Relative width `:relative-width FACTOR' specified and valid.
24515 Compute the width of the characters having the `glyph'
24516 property. */
24517 struct it it2;
24518 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24520 it2 = *it;
24521 if (it->multibyte_p)
24522 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24523 else
24525 it2.c = it2.char_to_display = *p, it2.len = 1;
24526 if (! ASCII_CHAR_P (it2.c))
24527 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24530 it2.glyph_row = NULL;
24531 it2.what = IT_CHARACTER;
24532 x_produce_glyphs (&it2);
24533 width = NUMVAL (prop) * it2.pixel_width;
24535 #endif /* HAVE_WINDOW_SYSTEM */
24536 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24537 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24539 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24540 align_to = (align_to < 0
24542 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24543 else if (align_to < 0)
24544 align_to = window_box_left_offset (it->w, TEXT_AREA);
24545 width = max (0, (int)tem + align_to - it->current_x);
24546 zero_width_ok_p = 1;
24548 else
24549 /* Nothing specified -> width defaults to canonical char width. */
24550 width = FRAME_COLUMN_WIDTH (it->f);
24552 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24553 width = 1;
24555 #ifdef HAVE_WINDOW_SYSTEM
24556 /* Compute height. */
24557 if (FRAME_WINDOW_P (it->f))
24559 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24560 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24562 height = (int)tem;
24563 zero_height_ok_p = 1;
24565 else if (prop = Fplist_get (plist, QCrelative_height),
24566 NUMVAL (prop) > 0)
24567 height = FONT_HEIGHT (font) * NUMVAL (prop);
24568 else
24569 height = FONT_HEIGHT (font);
24571 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24572 height = 1;
24574 /* Compute percentage of height used for ascent. If
24575 `:ascent ASCENT' is present and valid, use that. Otherwise,
24576 derive the ascent from the font in use. */
24577 if (prop = Fplist_get (plist, QCascent),
24578 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24579 ascent = height * NUMVAL (prop) / 100.0;
24580 else if (!NILP (prop)
24581 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24582 ascent = min (max (0, (int)tem), height);
24583 else
24584 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24586 else
24587 #endif /* HAVE_WINDOW_SYSTEM */
24588 height = 1;
24590 if (width > 0 && it->line_wrap != TRUNCATE
24591 && it->current_x + width > it->last_visible_x)
24593 width = it->last_visible_x - it->current_x;
24594 #ifdef HAVE_WINDOW_SYSTEM
24595 /* Subtract one more pixel from the stretch width, but only on
24596 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24597 width -= FRAME_WINDOW_P (it->f);
24598 #endif
24601 if (width > 0 && height > 0 && it->glyph_row)
24603 Lisp_Object o_object = it->object;
24604 Lisp_Object object = it->stack[it->sp - 1].string;
24605 int n = width;
24607 if (!STRINGP (object))
24608 object = it->w->contents;
24609 #ifdef HAVE_WINDOW_SYSTEM
24610 if (FRAME_WINDOW_P (it->f))
24611 append_stretch_glyph (it, object, width, height, ascent);
24612 else
24613 #endif
24615 it->object = object;
24616 it->char_to_display = ' ';
24617 it->pixel_width = it->len = 1;
24618 while (n--)
24619 tty_append_glyph (it);
24620 it->object = o_object;
24624 it->pixel_width = width;
24625 #ifdef HAVE_WINDOW_SYSTEM
24626 if (FRAME_WINDOW_P (it->f))
24628 it->ascent = it->phys_ascent = ascent;
24629 it->descent = it->phys_descent = height - it->ascent;
24630 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24631 take_vertical_position_into_account (it);
24633 else
24634 #endif
24635 it->nglyphs = width;
24638 /* Get information about special display element WHAT in an
24639 environment described by IT. WHAT is one of IT_TRUNCATION or
24640 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24641 non-null glyph_row member. This function ensures that fields like
24642 face_id, c, len of IT are left untouched. */
24644 static void
24645 produce_special_glyphs (struct it *it, enum display_element_type what)
24647 struct it temp_it;
24648 Lisp_Object gc;
24649 GLYPH glyph;
24651 temp_it = *it;
24652 temp_it.object = make_number (0);
24653 memset (&temp_it.current, 0, sizeof temp_it.current);
24655 if (what == IT_CONTINUATION)
24657 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24658 if (it->bidi_it.paragraph_dir == R2L)
24659 SET_GLYPH_FROM_CHAR (glyph, '/');
24660 else
24661 SET_GLYPH_FROM_CHAR (glyph, '\\');
24662 if (it->dp
24663 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24665 /* FIXME: Should we mirror GC for R2L lines? */
24666 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24667 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24670 else if (what == IT_TRUNCATION)
24672 /* Truncation glyph. */
24673 SET_GLYPH_FROM_CHAR (glyph, '$');
24674 if (it->dp
24675 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24677 /* FIXME: Should we mirror GC for R2L lines? */
24678 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24679 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24682 else
24683 emacs_abort ();
24685 #ifdef HAVE_WINDOW_SYSTEM
24686 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24687 is turned off, we precede the truncation/continuation glyphs by a
24688 stretch glyph whose width is computed such that these special
24689 glyphs are aligned at the window margin, even when very different
24690 fonts are used in different glyph rows. */
24691 if (FRAME_WINDOW_P (temp_it.f)
24692 /* init_iterator calls this with it->glyph_row == NULL, and it
24693 wants only the pixel width of the truncation/continuation
24694 glyphs. */
24695 && temp_it.glyph_row
24696 /* insert_left_trunc_glyphs calls us at the beginning of the
24697 row, and it has its own calculation of the stretch glyph
24698 width. */
24699 && temp_it.glyph_row->used[TEXT_AREA] > 0
24700 && (temp_it.glyph_row->reversed_p
24701 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24702 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24704 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24706 if (stretch_width > 0)
24708 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24709 struct font *font =
24710 face->font ? face->font : FRAME_FONT (temp_it.f);
24711 int stretch_ascent =
24712 (((temp_it.ascent + temp_it.descent)
24713 * FONT_BASE (font)) / FONT_HEIGHT (font));
24715 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24716 temp_it.ascent + temp_it.descent,
24717 stretch_ascent);
24720 #endif
24722 temp_it.dp = NULL;
24723 temp_it.what = IT_CHARACTER;
24724 temp_it.len = 1;
24725 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24726 temp_it.face_id = GLYPH_FACE (glyph);
24727 temp_it.len = CHAR_BYTES (temp_it.c);
24729 PRODUCE_GLYPHS (&temp_it);
24730 it->pixel_width = temp_it.pixel_width;
24731 it->nglyphs = temp_it.pixel_width;
24734 #ifdef HAVE_WINDOW_SYSTEM
24736 /* Calculate line-height and line-spacing properties.
24737 An integer value specifies explicit pixel value.
24738 A float value specifies relative value to current face height.
24739 A cons (float . face-name) specifies relative value to
24740 height of specified face font.
24742 Returns height in pixels, or nil. */
24745 static Lisp_Object
24746 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24747 int boff, int override)
24749 Lisp_Object face_name = Qnil;
24750 int ascent, descent, height;
24752 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24753 return val;
24755 if (CONSP (val))
24757 face_name = XCAR (val);
24758 val = XCDR (val);
24759 if (!NUMBERP (val))
24760 val = make_number (1);
24761 if (NILP (face_name))
24763 height = it->ascent + it->descent;
24764 goto scale;
24768 if (NILP (face_name))
24770 font = FRAME_FONT (it->f);
24771 boff = FRAME_BASELINE_OFFSET (it->f);
24773 else if (EQ (face_name, Qt))
24775 override = 0;
24777 else
24779 int face_id;
24780 struct face *face;
24782 face_id = lookup_named_face (it->f, face_name, 0);
24783 if (face_id < 0)
24784 return make_number (-1);
24786 face = FACE_FROM_ID (it->f, face_id);
24787 font = face->font;
24788 if (font == NULL)
24789 return make_number (-1);
24790 boff = font->baseline_offset;
24791 if (font->vertical_centering)
24792 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24795 ascent = FONT_BASE (font) + boff;
24796 descent = FONT_DESCENT (font) - boff;
24798 if (override)
24800 it->override_ascent = ascent;
24801 it->override_descent = descent;
24802 it->override_boff = boff;
24805 height = ascent + descent;
24807 scale:
24808 if (FLOATP (val))
24809 height = (int)(XFLOAT_DATA (val) * height);
24810 else if (INTEGERP (val))
24811 height *= XINT (val);
24813 return make_number (height);
24817 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24818 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24819 and only if this is for a character for which no font was found.
24821 If the display method (it->glyphless_method) is
24822 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24823 length of the acronym or the hexadecimal string, UPPER_XOFF and
24824 UPPER_YOFF are pixel offsets for the upper part of the string,
24825 LOWER_XOFF and LOWER_YOFF are for the lower part.
24827 For the other display methods, LEN through LOWER_YOFF are zero. */
24829 static void
24830 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24831 short upper_xoff, short upper_yoff,
24832 short lower_xoff, short lower_yoff)
24834 struct glyph *glyph;
24835 enum glyph_row_area area = it->area;
24837 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24838 if (glyph < it->glyph_row->glyphs[area + 1])
24840 /* If the glyph row is reversed, we need to prepend the glyph
24841 rather than append it. */
24842 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24844 struct glyph *g;
24846 /* Make room for the additional glyph. */
24847 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24848 g[1] = *g;
24849 glyph = it->glyph_row->glyphs[area];
24851 glyph->charpos = CHARPOS (it->position);
24852 glyph->object = it->object;
24853 glyph->pixel_width = it->pixel_width;
24854 glyph->ascent = it->ascent;
24855 glyph->descent = it->descent;
24856 glyph->voffset = it->voffset;
24857 glyph->type = GLYPHLESS_GLYPH;
24858 glyph->u.glyphless.method = it->glyphless_method;
24859 glyph->u.glyphless.for_no_font = for_no_font;
24860 glyph->u.glyphless.len = len;
24861 glyph->u.glyphless.ch = it->c;
24862 glyph->slice.glyphless.upper_xoff = upper_xoff;
24863 glyph->slice.glyphless.upper_yoff = upper_yoff;
24864 glyph->slice.glyphless.lower_xoff = lower_xoff;
24865 glyph->slice.glyphless.lower_yoff = lower_yoff;
24866 glyph->avoid_cursor_p = it->avoid_cursor_p;
24867 glyph->multibyte_p = it->multibyte_p;
24868 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24870 /* In R2L rows, the left and the right box edges need to be
24871 drawn in reverse direction. */
24872 glyph->right_box_line_p = it->start_of_box_run_p;
24873 glyph->left_box_line_p = it->end_of_box_run_p;
24875 else
24877 glyph->left_box_line_p = it->start_of_box_run_p;
24878 glyph->right_box_line_p = it->end_of_box_run_p;
24880 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24881 || it->phys_descent > it->descent);
24882 glyph->padding_p = 0;
24883 glyph->glyph_not_available_p = 0;
24884 glyph->face_id = face_id;
24885 glyph->font_type = FONT_TYPE_UNKNOWN;
24886 if (it->bidi_p)
24888 glyph->resolved_level = it->bidi_it.resolved_level;
24889 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24890 emacs_abort ();
24891 glyph->bidi_type = it->bidi_it.type;
24893 ++it->glyph_row->used[area];
24895 else
24896 IT_EXPAND_MATRIX_WIDTH (it, area);
24900 /* Produce a glyph for a glyphless character for iterator IT.
24901 IT->glyphless_method specifies which method to use for displaying
24902 the character. See the description of enum
24903 glyphless_display_method in dispextern.h for the detail.
24905 FOR_NO_FONT is nonzero if and only if this is for a character for
24906 which no font was found. ACRONYM, if non-nil, is an acronym string
24907 for the character. */
24909 static void
24910 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24912 int face_id;
24913 struct face *face;
24914 struct font *font;
24915 int base_width, base_height, width, height;
24916 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24917 int len;
24919 /* Get the metrics of the base font. We always refer to the current
24920 ASCII face. */
24921 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24922 font = face->font ? face->font : FRAME_FONT (it->f);
24923 it->ascent = FONT_BASE (font) + font->baseline_offset;
24924 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24925 base_height = it->ascent + it->descent;
24926 base_width = font->average_width;
24928 face_id = merge_glyphless_glyph_face (it);
24930 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24932 it->pixel_width = THIN_SPACE_WIDTH;
24933 len = 0;
24934 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24936 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24938 width = CHAR_WIDTH (it->c);
24939 if (width == 0)
24940 width = 1;
24941 else if (width > 4)
24942 width = 4;
24943 it->pixel_width = base_width * width;
24944 len = 0;
24945 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24947 else
24949 char buf[7];
24950 const char *str;
24951 unsigned int code[6];
24952 int upper_len;
24953 int ascent, descent;
24954 struct font_metrics metrics_upper, metrics_lower;
24956 face = FACE_FROM_ID (it->f, face_id);
24957 font = face->font ? face->font : FRAME_FONT (it->f);
24958 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24960 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24962 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24963 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24964 if (CONSP (acronym))
24965 acronym = XCAR (acronym);
24966 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24968 else
24970 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24971 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24972 str = buf;
24974 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24975 code[len] = font->driver->encode_char (font, str[len]);
24976 upper_len = (len + 1) / 2;
24977 font->driver->text_extents (font, code, upper_len,
24978 &metrics_upper);
24979 font->driver->text_extents (font, code + upper_len, len - upper_len,
24980 &metrics_lower);
24984 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24985 width = max (metrics_upper.width, metrics_lower.width) + 4;
24986 upper_xoff = upper_yoff = 2; /* the typical case */
24987 if (base_width >= width)
24989 /* Align the upper to the left, the lower to the right. */
24990 it->pixel_width = base_width;
24991 lower_xoff = base_width - 2 - metrics_lower.width;
24993 else
24995 /* Center the shorter one. */
24996 it->pixel_width = width;
24997 if (metrics_upper.width >= metrics_lower.width)
24998 lower_xoff = (width - metrics_lower.width) / 2;
24999 else
25001 /* FIXME: This code doesn't look right. It formerly was
25002 missing the "lower_xoff = 0;", which couldn't have
25003 been right since it left lower_xoff uninitialized. */
25004 lower_xoff = 0;
25005 upper_xoff = (width - metrics_upper.width) / 2;
25009 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25010 top, bottom, and between upper and lower strings. */
25011 height = (metrics_upper.ascent + metrics_upper.descent
25012 + metrics_lower.ascent + metrics_lower.descent) + 5;
25013 /* Center vertically.
25014 H:base_height, D:base_descent
25015 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25017 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25018 descent = D - H/2 + h/2;
25019 lower_yoff = descent - 2 - ld;
25020 upper_yoff = lower_yoff - la - 1 - ud; */
25021 ascent = - (it->descent - (base_height + height + 1) / 2);
25022 descent = it->descent - (base_height - height) / 2;
25023 lower_yoff = descent - 2 - metrics_lower.descent;
25024 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25025 - metrics_upper.descent);
25026 /* Don't make the height shorter than the base height. */
25027 if (height > base_height)
25029 it->ascent = ascent;
25030 it->descent = descent;
25034 it->phys_ascent = it->ascent;
25035 it->phys_descent = it->descent;
25036 if (it->glyph_row)
25037 append_glyphless_glyph (it, face_id, for_no_font, len,
25038 upper_xoff, upper_yoff,
25039 lower_xoff, lower_yoff);
25040 it->nglyphs = 1;
25041 take_vertical_position_into_account (it);
25045 /* RIF:
25046 Produce glyphs/get display metrics for the display element IT is
25047 loaded with. See the description of struct it in dispextern.h
25048 for an overview of struct it. */
25050 void
25051 x_produce_glyphs (struct it *it)
25053 int extra_line_spacing = it->extra_line_spacing;
25055 it->glyph_not_available_p = 0;
25057 if (it->what == IT_CHARACTER)
25059 XChar2b char2b;
25060 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25061 struct font *font = face->font;
25062 struct font_metrics *pcm = NULL;
25063 int boff; /* baseline offset */
25065 if (font == NULL)
25067 /* When no suitable font is found, display this character by
25068 the method specified in the first extra slot of
25069 Vglyphless_char_display. */
25070 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25072 eassert (it->what == IT_GLYPHLESS);
25073 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25074 goto done;
25077 boff = font->baseline_offset;
25078 if (font->vertical_centering)
25079 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25081 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25083 int stretched_p;
25085 it->nglyphs = 1;
25087 if (it->override_ascent >= 0)
25089 it->ascent = it->override_ascent;
25090 it->descent = it->override_descent;
25091 boff = it->override_boff;
25093 else
25095 it->ascent = FONT_BASE (font) + boff;
25096 it->descent = FONT_DESCENT (font) - boff;
25099 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25101 pcm = get_per_char_metric (font, &char2b);
25102 if (pcm->width == 0
25103 && pcm->rbearing == 0 && pcm->lbearing == 0)
25104 pcm = NULL;
25107 if (pcm)
25109 it->phys_ascent = pcm->ascent + boff;
25110 it->phys_descent = pcm->descent - boff;
25111 it->pixel_width = pcm->width;
25113 else
25115 it->glyph_not_available_p = 1;
25116 it->phys_ascent = it->ascent;
25117 it->phys_descent = it->descent;
25118 it->pixel_width = font->space_width;
25121 if (it->constrain_row_ascent_descent_p)
25123 if (it->descent > it->max_descent)
25125 it->ascent += it->descent - it->max_descent;
25126 it->descent = it->max_descent;
25128 if (it->ascent > it->max_ascent)
25130 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25131 it->ascent = it->max_ascent;
25133 it->phys_ascent = min (it->phys_ascent, it->ascent);
25134 it->phys_descent = min (it->phys_descent, it->descent);
25135 extra_line_spacing = 0;
25138 /* If this is a space inside a region of text with
25139 `space-width' property, change its width. */
25140 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25141 if (stretched_p)
25142 it->pixel_width *= XFLOATINT (it->space_width);
25144 /* If face has a box, add the box thickness to the character
25145 height. If character has a box line to the left and/or
25146 right, add the box line width to the character's width. */
25147 if (face->box != FACE_NO_BOX)
25149 int thick = face->box_line_width;
25151 if (thick > 0)
25153 it->ascent += thick;
25154 it->descent += thick;
25156 else
25157 thick = -thick;
25159 if (it->start_of_box_run_p)
25160 it->pixel_width += thick;
25161 if (it->end_of_box_run_p)
25162 it->pixel_width += thick;
25165 /* If face has an overline, add the height of the overline
25166 (1 pixel) and a 1 pixel margin to the character height. */
25167 if (face->overline_p)
25168 it->ascent += overline_margin;
25170 if (it->constrain_row_ascent_descent_p)
25172 if (it->ascent > it->max_ascent)
25173 it->ascent = it->max_ascent;
25174 if (it->descent > it->max_descent)
25175 it->descent = it->max_descent;
25178 take_vertical_position_into_account (it);
25180 /* If we have to actually produce glyphs, do it. */
25181 if (it->glyph_row)
25183 if (stretched_p)
25185 /* Translate a space with a `space-width' property
25186 into a stretch glyph. */
25187 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25188 / FONT_HEIGHT (font));
25189 append_stretch_glyph (it, it->object, it->pixel_width,
25190 it->ascent + it->descent, ascent);
25192 else
25193 append_glyph (it);
25195 /* If characters with lbearing or rbearing are displayed
25196 in this line, record that fact in a flag of the
25197 glyph row. This is used to optimize X output code. */
25198 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25199 it->glyph_row->contains_overlapping_glyphs_p = 1;
25201 if (! stretched_p && it->pixel_width == 0)
25202 /* We assure that all visible glyphs have at least 1-pixel
25203 width. */
25204 it->pixel_width = 1;
25206 else if (it->char_to_display == '\n')
25208 /* A newline has no width, but we need the height of the
25209 line. But if previous part of the line sets a height,
25210 don't increase that height */
25212 Lisp_Object height;
25213 Lisp_Object total_height = Qnil;
25215 it->override_ascent = -1;
25216 it->pixel_width = 0;
25217 it->nglyphs = 0;
25219 height = get_it_property (it, Qline_height);
25220 /* Split (line-height total-height) list */
25221 if (CONSP (height)
25222 && CONSP (XCDR (height))
25223 && NILP (XCDR (XCDR (height))))
25225 total_height = XCAR (XCDR (height));
25226 height = XCAR (height);
25228 height = calc_line_height_property (it, height, font, boff, 1);
25230 if (it->override_ascent >= 0)
25232 it->ascent = it->override_ascent;
25233 it->descent = it->override_descent;
25234 boff = it->override_boff;
25236 else
25238 it->ascent = FONT_BASE (font) + boff;
25239 it->descent = FONT_DESCENT (font) - boff;
25242 if (EQ (height, Qt))
25244 if (it->descent > it->max_descent)
25246 it->ascent += it->descent - it->max_descent;
25247 it->descent = it->max_descent;
25249 if (it->ascent > it->max_ascent)
25251 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25252 it->ascent = it->max_ascent;
25254 it->phys_ascent = min (it->phys_ascent, it->ascent);
25255 it->phys_descent = min (it->phys_descent, it->descent);
25256 it->constrain_row_ascent_descent_p = 1;
25257 extra_line_spacing = 0;
25259 else
25261 Lisp_Object spacing;
25263 it->phys_ascent = it->ascent;
25264 it->phys_descent = it->descent;
25266 if ((it->max_ascent > 0 || it->max_descent > 0)
25267 && face->box != FACE_NO_BOX
25268 && face->box_line_width > 0)
25270 it->ascent += face->box_line_width;
25271 it->descent += face->box_line_width;
25273 if (!NILP (height)
25274 && XINT (height) > it->ascent + it->descent)
25275 it->ascent = XINT (height) - it->descent;
25277 if (!NILP (total_height))
25278 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25279 else
25281 spacing = get_it_property (it, Qline_spacing);
25282 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25284 if (INTEGERP (spacing))
25286 extra_line_spacing = XINT (spacing);
25287 if (!NILP (total_height))
25288 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25292 else /* i.e. (it->char_to_display == '\t') */
25294 if (font->space_width > 0)
25296 int tab_width = it->tab_width * font->space_width;
25297 int x = it->current_x + it->continuation_lines_width;
25298 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25300 /* If the distance from the current position to the next tab
25301 stop is less than a space character width, use the
25302 tab stop after that. */
25303 if (next_tab_x - x < font->space_width)
25304 next_tab_x += tab_width;
25306 it->pixel_width = next_tab_x - x;
25307 it->nglyphs = 1;
25308 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25309 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25311 if (it->glyph_row)
25313 append_stretch_glyph (it, it->object, it->pixel_width,
25314 it->ascent + it->descent, it->ascent);
25317 else
25319 it->pixel_width = 0;
25320 it->nglyphs = 1;
25324 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25326 /* A static composition.
25328 Note: A composition is represented as one glyph in the
25329 glyph matrix. There are no padding glyphs.
25331 Important note: pixel_width, ascent, and descent are the
25332 values of what is drawn by draw_glyphs (i.e. the values of
25333 the overall glyphs composed). */
25334 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25335 int boff; /* baseline offset */
25336 struct composition *cmp = composition_table[it->cmp_it.id];
25337 int glyph_len = cmp->glyph_len;
25338 struct font *font = face->font;
25340 it->nglyphs = 1;
25342 /* If we have not yet calculated pixel size data of glyphs of
25343 the composition for the current face font, calculate them
25344 now. Theoretically, we have to check all fonts for the
25345 glyphs, but that requires much time and memory space. So,
25346 here we check only the font of the first glyph. This may
25347 lead to incorrect display, but it's very rare, and C-l
25348 (recenter-top-bottom) can correct the display anyway. */
25349 if (! cmp->font || cmp->font != font)
25351 /* Ascent and descent of the font of the first character
25352 of this composition (adjusted by baseline offset).
25353 Ascent and descent of overall glyphs should not be less
25354 than these, respectively. */
25355 int font_ascent, font_descent, font_height;
25356 /* Bounding box of the overall glyphs. */
25357 int leftmost, rightmost, lowest, highest;
25358 int lbearing, rbearing;
25359 int i, width, ascent, descent;
25360 int left_padded = 0, right_padded = 0;
25361 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25362 XChar2b char2b;
25363 struct font_metrics *pcm;
25364 int font_not_found_p;
25365 ptrdiff_t pos;
25367 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25368 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25369 break;
25370 if (glyph_len < cmp->glyph_len)
25371 right_padded = 1;
25372 for (i = 0; i < glyph_len; i++)
25374 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25375 break;
25376 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25378 if (i > 0)
25379 left_padded = 1;
25381 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25382 : IT_CHARPOS (*it));
25383 /* If no suitable font is found, use the default font. */
25384 font_not_found_p = font == NULL;
25385 if (font_not_found_p)
25387 face = face->ascii_face;
25388 font = face->font;
25390 boff = font->baseline_offset;
25391 if (font->vertical_centering)
25392 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25393 font_ascent = FONT_BASE (font) + boff;
25394 font_descent = FONT_DESCENT (font) - boff;
25395 font_height = FONT_HEIGHT (font);
25397 cmp->font = font;
25399 pcm = NULL;
25400 if (! font_not_found_p)
25402 get_char_face_and_encoding (it->f, c, it->face_id,
25403 &char2b, 0);
25404 pcm = get_per_char_metric (font, &char2b);
25407 /* Initialize the bounding box. */
25408 if (pcm)
25410 width = cmp->glyph_len > 0 ? pcm->width : 0;
25411 ascent = pcm->ascent;
25412 descent = pcm->descent;
25413 lbearing = pcm->lbearing;
25414 rbearing = pcm->rbearing;
25416 else
25418 width = cmp->glyph_len > 0 ? font->space_width : 0;
25419 ascent = FONT_BASE (font);
25420 descent = FONT_DESCENT (font);
25421 lbearing = 0;
25422 rbearing = width;
25425 rightmost = width;
25426 leftmost = 0;
25427 lowest = - descent + boff;
25428 highest = ascent + boff;
25430 if (! font_not_found_p
25431 && font->default_ascent
25432 && CHAR_TABLE_P (Vuse_default_ascent)
25433 && !NILP (Faref (Vuse_default_ascent,
25434 make_number (it->char_to_display))))
25435 highest = font->default_ascent + boff;
25437 /* Draw the first glyph at the normal position. It may be
25438 shifted to right later if some other glyphs are drawn
25439 at the left. */
25440 cmp->offsets[i * 2] = 0;
25441 cmp->offsets[i * 2 + 1] = boff;
25442 cmp->lbearing = lbearing;
25443 cmp->rbearing = rbearing;
25445 /* Set cmp->offsets for the remaining glyphs. */
25446 for (i++; i < glyph_len; i++)
25448 int left, right, btm, top;
25449 int ch = COMPOSITION_GLYPH (cmp, i);
25450 int face_id;
25451 struct face *this_face;
25453 if (ch == '\t')
25454 ch = ' ';
25455 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25456 this_face = FACE_FROM_ID (it->f, face_id);
25457 font = this_face->font;
25459 if (font == NULL)
25460 pcm = NULL;
25461 else
25463 get_char_face_and_encoding (it->f, ch, face_id,
25464 &char2b, 0);
25465 pcm = get_per_char_metric (font, &char2b);
25467 if (! pcm)
25468 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25469 else
25471 width = pcm->width;
25472 ascent = pcm->ascent;
25473 descent = pcm->descent;
25474 lbearing = pcm->lbearing;
25475 rbearing = pcm->rbearing;
25476 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25478 /* Relative composition with or without
25479 alternate chars. */
25480 left = (leftmost + rightmost - width) / 2;
25481 btm = - descent + boff;
25482 if (font->relative_compose
25483 && (! CHAR_TABLE_P (Vignore_relative_composition)
25484 || NILP (Faref (Vignore_relative_composition,
25485 make_number (ch)))))
25488 if (- descent >= font->relative_compose)
25489 /* One extra pixel between two glyphs. */
25490 btm = highest + 1;
25491 else if (ascent <= 0)
25492 /* One extra pixel between two glyphs. */
25493 btm = lowest - 1 - ascent - descent;
25496 else
25498 /* A composition rule is specified by an integer
25499 value that encodes global and new reference
25500 points (GREF and NREF). GREF and NREF are
25501 specified by numbers as below:
25503 0---1---2 -- ascent
25507 9--10--11 -- center
25509 ---3---4---5--- baseline
25511 6---7---8 -- descent
25513 int rule = COMPOSITION_RULE (cmp, i);
25514 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25516 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25517 grefx = gref % 3, nrefx = nref % 3;
25518 grefy = gref / 3, nrefy = nref / 3;
25519 if (xoff)
25520 xoff = font_height * (xoff - 128) / 256;
25521 if (yoff)
25522 yoff = font_height * (yoff - 128) / 256;
25524 left = (leftmost
25525 + grefx * (rightmost - leftmost) / 2
25526 - nrefx * width / 2
25527 + xoff);
25529 btm = ((grefy == 0 ? highest
25530 : grefy == 1 ? 0
25531 : grefy == 2 ? lowest
25532 : (highest + lowest) / 2)
25533 - (nrefy == 0 ? ascent + descent
25534 : nrefy == 1 ? descent - boff
25535 : nrefy == 2 ? 0
25536 : (ascent + descent) / 2)
25537 + yoff);
25540 cmp->offsets[i * 2] = left;
25541 cmp->offsets[i * 2 + 1] = btm + descent;
25543 /* Update the bounding box of the overall glyphs. */
25544 if (width > 0)
25546 right = left + width;
25547 if (left < leftmost)
25548 leftmost = left;
25549 if (right > rightmost)
25550 rightmost = right;
25552 top = btm + descent + ascent;
25553 if (top > highest)
25554 highest = top;
25555 if (btm < lowest)
25556 lowest = btm;
25558 if (cmp->lbearing > left + lbearing)
25559 cmp->lbearing = left + lbearing;
25560 if (cmp->rbearing < left + rbearing)
25561 cmp->rbearing = left + rbearing;
25565 /* If there are glyphs whose x-offsets are negative,
25566 shift all glyphs to the right and make all x-offsets
25567 non-negative. */
25568 if (leftmost < 0)
25570 for (i = 0; i < cmp->glyph_len; i++)
25571 cmp->offsets[i * 2] -= leftmost;
25572 rightmost -= leftmost;
25573 cmp->lbearing -= leftmost;
25574 cmp->rbearing -= leftmost;
25577 if (left_padded && cmp->lbearing < 0)
25579 for (i = 0; i < cmp->glyph_len; i++)
25580 cmp->offsets[i * 2] -= cmp->lbearing;
25581 rightmost -= cmp->lbearing;
25582 cmp->rbearing -= cmp->lbearing;
25583 cmp->lbearing = 0;
25585 if (right_padded && rightmost < cmp->rbearing)
25587 rightmost = cmp->rbearing;
25590 cmp->pixel_width = rightmost;
25591 cmp->ascent = highest;
25592 cmp->descent = - lowest;
25593 if (cmp->ascent < font_ascent)
25594 cmp->ascent = font_ascent;
25595 if (cmp->descent < font_descent)
25596 cmp->descent = font_descent;
25599 if (it->glyph_row
25600 && (cmp->lbearing < 0
25601 || cmp->rbearing > cmp->pixel_width))
25602 it->glyph_row->contains_overlapping_glyphs_p = 1;
25604 it->pixel_width = cmp->pixel_width;
25605 it->ascent = it->phys_ascent = cmp->ascent;
25606 it->descent = it->phys_descent = cmp->descent;
25607 if (face->box != FACE_NO_BOX)
25609 int thick = face->box_line_width;
25611 if (thick > 0)
25613 it->ascent += thick;
25614 it->descent += thick;
25616 else
25617 thick = - thick;
25619 if (it->start_of_box_run_p)
25620 it->pixel_width += thick;
25621 if (it->end_of_box_run_p)
25622 it->pixel_width += thick;
25625 /* If face has an overline, add the height of the overline
25626 (1 pixel) and a 1 pixel margin to the character height. */
25627 if (face->overline_p)
25628 it->ascent += overline_margin;
25630 take_vertical_position_into_account (it);
25631 if (it->ascent < 0)
25632 it->ascent = 0;
25633 if (it->descent < 0)
25634 it->descent = 0;
25636 if (it->glyph_row && cmp->glyph_len > 0)
25637 append_composite_glyph (it);
25639 else if (it->what == IT_COMPOSITION)
25641 /* A dynamic (automatic) composition. */
25642 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25643 Lisp_Object gstring;
25644 struct font_metrics metrics;
25646 it->nglyphs = 1;
25648 gstring = composition_gstring_from_id (it->cmp_it.id);
25649 it->pixel_width
25650 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25651 &metrics);
25652 if (it->glyph_row
25653 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25654 it->glyph_row->contains_overlapping_glyphs_p = 1;
25655 it->ascent = it->phys_ascent = metrics.ascent;
25656 it->descent = it->phys_descent = metrics.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;
25674 /* If face has an overline, add the height of the overline
25675 (1 pixel) and a 1 pixel margin to the character height. */
25676 if (face->overline_p)
25677 it->ascent += overline_margin;
25678 take_vertical_position_into_account (it);
25679 if (it->ascent < 0)
25680 it->ascent = 0;
25681 if (it->descent < 0)
25682 it->descent = 0;
25684 if (it->glyph_row)
25685 append_composite_glyph (it);
25687 else if (it->what == IT_GLYPHLESS)
25688 produce_glyphless_glyph (it, 0, Qnil);
25689 else if (it->what == IT_IMAGE)
25690 produce_image_glyph (it);
25691 else if (it->what == IT_STRETCH)
25692 produce_stretch_glyph (it);
25694 done:
25695 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25696 because this isn't true for images with `:ascent 100'. */
25697 eassert (it->ascent >= 0 && it->descent >= 0);
25698 if (it->area == TEXT_AREA)
25699 it->current_x += it->pixel_width;
25701 if (extra_line_spacing > 0)
25703 it->descent += extra_line_spacing;
25704 if (extra_line_spacing > it->max_extra_line_spacing)
25705 it->max_extra_line_spacing = extra_line_spacing;
25708 it->max_ascent = max (it->max_ascent, it->ascent);
25709 it->max_descent = max (it->max_descent, it->descent);
25710 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25711 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25714 /* EXPORT for RIF:
25715 Output LEN glyphs starting at START at the nominal cursor position.
25716 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25717 being updated, and UPDATED_AREA is the area of that row being updated. */
25719 void
25720 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25721 struct glyph *start, enum glyph_row_area updated_area, int len)
25723 int x, hpos, chpos = w->phys_cursor.hpos;
25725 eassert (updated_row);
25726 /* When the window is hscrolled, cursor hpos can legitimately be out
25727 of bounds, but we draw the cursor at the corresponding window
25728 margin in that case. */
25729 if (!updated_row->reversed_p && chpos < 0)
25730 chpos = 0;
25731 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25732 chpos = updated_row->used[TEXT_AREA] - 1;
25734 block_input ();
25736 /* Write glyphs. */
25738 hpos = start - updated_row->glyphs[updated_area];
25739 x = draw_glyphs (w, w->output_cursor.x,
25740 updated_row, updated_area,
25741 hpos, hpos + len,
25742 DRAW_NORMAL_TEXT, 0);
25744 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25745 if (updated_area == TEXT_AREA
25746 && w->phys_cursor_on_p
25747 && w->phys_cursor.vpos == w->output_cursor.vpos
25748 && chpos >= hpos
25749 && chpos < hpos + len)
25750 w->phys_cursor_on_p = 0;
25752 unblock_input ();
25754 /* Advance the output cursor. */
25755 w->output_cursor.hpos += len;
25756 w->output_cursor.x = x;
25760 /* EXPORT for RIF:
25761 Insert LEN glyphs from START at the nominal cursor position. */
25763 void
25764 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25765 struct glyph *start, enum glyph_row_area updated_area, int len)
25767 struct frame *f;
25768 int line_height, shift_by_width, shifted_region_width;
25769 struct glyph_row *row;
25770 struct glyph *glyph;
25771 int frame_x, frame_y;
25772 ptrdiff_t hpos;
25774 eassert (updated_row);
25775 block_input ();
25776 f = XFRAME (WINDOW_FRAME (w));
25778 /* Get the height of the line we are in. */
25779 row = updated_row;
25780 line_height = row->height;
25782 /* Get the width of the glyphs to insert. */
25783 shift_by_width = 0;
25784 for (glyph = start; glyph < start + len; ++glyph)
25785 shift_by_width += glyph->pixel_width;
25787 /* Get the width of the region to shift right. */
25788 shifted_region_width = (window_box_width (w, updated_area)
25789 - w->output_cursor.x
25790 - shift_by_width);
25792 /* Shift right. */
25793 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25794 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25796 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25797 line_height, shift_by_width);
25799 /* Write the glyphs. */
25800 hpos = start - row->glyphs[updated_area];
25801 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25802 hpos, hpos + len,
25803 DRAW_NORMAL_TEXT, 0);
25805 /* Advance the output cursor. */
25806 w->output_cursor.hpos += len;
25807 w->output_cursor.x += shift_by_width;
25808 unblock_input ();
25812 /* EXPORT for RIF:
25813 Erase the current text line from the nominal cursor position
25814 (inclusive) to pixel column TO_X (exclusive). The idea is that
25815 everything from TO_X onward is already erased.
25817 TO_X is a pixel position relative to UPDATED_AREA of currently
25818 updated window W. TO_X == -1 means clear to the end of this area. */
25820 void
25821 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25822 enum glyph_row_area updated_area, int to_x)
25824 struct frame *f;
25825 int max_x, min_y, max_y;
25826 int from_x, from_y, to_y;
25828 eassert (updated_row);
25829 f = XFRAME (w->frame);
25831 if (updated_row->full_width_p)
25832 max_x = WINDOW_TOTAL_WIDTH (w);
25833 else
25834 max_x = window_box_width (w, updated_area);
25835 max_y = window_text_bottom_y (w);
25837 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25838 of window. For TO_X > 0, truncate to end of drawing area. */
25839 if (to_x == 0)
25840 return;
25841 else if (to_x < 0)
25842 to_x = max_x;
25843 else
25844 to_x = min (to_x, max_x);
25846 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25848 /* Notice if the cursor will be cleared by this operation. */
25849 if (!updated_row->full_width_p)
25850 notice_overwritten_cursor (w, updated_area,
25851 w->output_cursor.x, -1,
25852 updated_row->y,
25853 MATRIX_ROW_BOTTOM_Y (updated_row));
25855 from_x = w->output_cursor.x;
25857 /* Translate to frame coordinates. */
25858 if (updated_row->full_width_p)
25860 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25861 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25863 else
25865 int area_left = window_box_left (w, updated_area);
25866 from_x += area_left;
25867 to_x += area_left;
25870 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25871 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25872 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25874 /* Prevent inadvertently clearing to end of the X window. */
25875 if (to_x > from_x && to_y > from_y)
25877 block_input ();
25878 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25879 to_x - from_x, to_y - from_y);
25880 unblock_input ();
25884 #endif /* HAVE_WINDOW_SYSTEM */
25888 /***********************************************************************
25889 Cursor types
25890 ***********************************************************************/
25892 /* Value is the internal representation of the specified cursor type
25893 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25894 of the bar cursor. */
25896 static enum text_cursor_kinds
25897 get_specified_cursor_type (Lisp_Object arg, int *width)
25899 enum text_cursor_kinds type;
25901 if (NILP (arg))
25902 return NO_CURSOR;
25904 if (EQ (arg, Qbox))
25905 return FILLED_BOX_CURSOR;
25907 if (EQ (arg, Qhollow))
25908 return HOLLOW_BOX_CURSOR;
25910 if (EQ (arg, Qbar))
25912 *width = 2;
25913 return BAR_CURSOR;
25916 if (CONSP (arg)
25917 && EQ (XCAR (arg), Qbar)
25918 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25920 *width = XINT (XCDR (arg));
25921 return BAR_CURSOR;
25924 if (EQ (arg, Qhbar))
25926 *width = 2;
25927 return HBAR_CURSOR;
25930 if (CONSP (arg)
25931 && EQ (XCAR (arg), Qhbar)
25932 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25934 *width = XINT (XCDR (arg));
25935 return HBAR_CURSOR;
25938 /* Treat anything unknown as "hollow box cursor".
25939 It was bad to signal an error; people have trouble fixing
25940 .Xdefaults with Emacs, when it has something bad in it. */
25941 type = HOLLOW_BOX_CURSOR;
25943 return type;
25946 /* Set the default cursor types for specified frame. */
25947 void
25948 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25950 int width = 1;
25951 Lisp_Object tem;
25953 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25954 FRAME_CURSOR_WIDTH (f) = width;
25956 /* By default, set up the blink-off state depending on the on-state. */
25958 tem = Fassoc (arg, Vblink_cursor_alist);
25959 if (!NILP (tem))
25961 FRAME_BLINK_OFF_CURSOR (f)
25962 = get_specified_cursor_type (XCDR (tem), &width);
25963 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25965 else
25966 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25968 /* Make sure the cursor gets redrawn. */
25969 f->cursor_type_changed = 1;
25973 #ifdef HAVE_WINDOW_SYSTEM
25975 /* Return the cursor we want to be displayed in window W. Return
25976 width of bar/hbar cursor through WIDTH arg. Return with
25977 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25978 (i.e. if the `system caret' should track this cursor).
25980 In a mini-buffer window, we want the cursor only to appear if we
25981 are reading input from this window. For the selected window, we
25982 want the cursor type given by the frame parameter or buffer local
25983 setting of cursor-type. If explicitly marked off, draw no cursor.
25984 In all other cases, we want a hollow box cursor. */
25986 static enum text_cursor_kinds
25987 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25988 int *active_cursor)
25990 struct frame *f = XFRAME (w->frame);
25991 struct buffer *b = XBUFFER (w->contents);
25992 int cursor_type = DEFAULT_CURSOR;
25993 Lisp_Object alt_cursor;
25994 int non_selected = 0;
25996 *active_cursor = 1;
25998 /* Echo area */
25999 if (cursor_in_echo_area
26000 && FRAME_HAS_MINIBUF_P (f)
26001 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26003 if (w == XWINDOW (echo_area_window))
26005 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26007 *width = FRAME_CURSOR_WIDTH (f);
26008 return FRAME_DESIRED_CURSOR (f);
26010 else
26011 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26014 *active_cursor = 0;
26015 non_selected = 1;
26018 /* Detect a nonselected window or nonselected frame. */
26019 else if (w != XWINDOW (f->selected_window)
26020 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26022 *active_cursor = 0;
26024 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26025 return NO_CURSOR;
26027 non_selected = 1;
26030 /* Never display a cursor in a window in which cursor-type is nil. */
26031 if (NILP (BVAR (b, cursor_type)))
26032 return NO_CURSOR;
26034 /* Get the normal cursor type for this window. */
26035 if (EQ (BVAR (b, cursor_type), Qt))
26037 cursor_type = FRAME_DESIRED_CURSOR (f);
26038 *width = FRAME_CURSOR_WIDTH (f);
26040 else
26041 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26043 /* Use cursor-in-non-selected-windows instead
26044 for non-selected window or frame. */
26045 if (non_selected)
26047 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26048 if (!EQ (Qt, alt_cursor))
26049 return get_specified_cursor_type (alt_cursor, width);
26050 /* t means modify the normal cursor type. */
26051 if (cursor_type == FILLED_BOX_CURSOR)
26052 cursor_type = HOLLOW_BOX_CURSOR;
26053 else if (cursor_type == BAR_CURSOR && *width > 1)
26054 --*width;
26055 return cursor_type;
26058 /* Use normal cursor if not blinked off. */
26059 if (!w->cursor_off_p)
26061 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26063 if (cursor_type == FILLED_BOX_CURSOR)
26065 /* Using a block cursor on large images can be very annoying.
26066 So use a hollow cursor for "large" images.
26067 If image is not transparent (no mask), also use hollow cursor. */
26068 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26069 if (img != NULL && IMAGEP (img->spec))
26071 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26072 where N = size of default frame font size.
26073 This should cover most of the "tiny" icons people may use. */
26074 if (!img->mask
26075 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26076 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26077 cursor_type = HOLLOW_BOX_CURSOR;
26080 else if (cursor_type != NO_CURSOR)
26082 /* Display current only supports BOX and HOLLOW cursors for images.
26083 So for now, unconditionally use a HOLLOW cursor when cursor is
26084 not a solid box cursor. */
26085 cursor_type = HOLLOW_BOX_CURSOR;
26088 return cursor_type;
26091 /* Cursor is blinked off, so determine how to "toggle" it. */
26093 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26094 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26095 return get_specified_cursor_type (XCDR (alt_cursor), width);
26097 /* Then see if frame has specified a specific blink off cursor type. */
26098 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26100 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26101 return FRAME_BLINK_OFF_CURSOR (f);
26104 #if 0
26105 /* Some people liked having a permanently visible blinking cursor,
26106 while others had very strong opinions against it. So it was
26107 decided to remove it. KFS 2003-09-03 */
26109 /* Finally perform built-in cursor blinking:
26110 filled box <-> hollow box
26111 wide [h]bar <-> narrow [h]bar
26112 narrow [h]bar <-> no cursor
26113 other type <-> no cursor */
26115 if (cursor_type == FILLED_BOX_CURSOR)
26116 return HOLLOW_BOX_CURSOR;
26118 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26120 *width = 1;
26121 return cursor_type;
26123 #endif
26125 return NO_CURSOR;
26129 /* Notice when the text cursor of window W has been completely
26130 overwritten by a drawing operation that outputs glyphs in AREA
26131 starting at X0 and ending at X1 in the line starting at Y0 and
26132 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26133 the rest of the line after X0 has been written. Y coordinates
26134 are window-relative. */
26136 static void
26137 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26138 int x0, int x1, int y0, int y1)
26140 int cx0, cx1, cy0, cy1;
26141 struct glyph_row *row;
26143 if (!w->phys_cursor_on_p)
26144 return;
26145 if (area != TEXT_AREA)
26146 return;
26148 if (w->phys_cursor.vpos < 0
26149 || w->phys_cursor.vpos >= w->current_matrix->nrows
26150 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26151 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26152 return;
26154 if (row->cursor_in_fringe_p)
26156 row->cursor_in_fringe_p = 0;
26157 draw_fringe_bitmap (w, row, row->reversed_p);
26158 w->phys_cursor_on_p = 0;
26159 return;
26162 cx0 = w->phys_cursor.x;
26163 cx1 = cx0 + w->phys_cursor_width;
26164 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26165 return;
26167 /* The cursor image will be completely removed from the
26168 screen if the output area intersects the cursor area in
26169 y-direction. When we draw in [y0 y1[, and some part of
26170 the cursor is at y < y0, that part must have been drawn
26171 before. When scrolling, the cursor is erased before
26172 actually scrolling, so we don't come here. When not
26173 scrolling, the rows above the old cursor row must have
26174 changed, and in this case these rows must have written
26175 over the cursor image.
26177 Likewise if part of the cursor is below y1, with the
26178 exception of the cursor being in the first blank row at
26179 the buffer and window end because update_text_area
26180 doesn't draw that row. (Except when it does, but
26181 that's handled in update_text_area.) */
26183 cy0 = w->phys_cursor.y;
26184 cy1 = cy0 + w->phys_cursor_height;
26185 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26186 return;
26188 w->phys_cursor_on_p = 0;
26191 #endif /* HAVE_WINDOW_SYSTEM */
26194 /************************************************************************
26195 Mouse Face
26196 ************************************************************************/
26198 #ifdef HAVE_WINDOW_SYSTEM
26200 /* EXPORT for RIF:
26201 Fix the display of area AREA of overlapping row ROW in window W
26202 with respect to the overlapping part OVERLAPS. */
26204 void
26205 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26206 enum glyph_row_area area, int overlaps)
26208 int i, x;
26210 block_input ();
26212 x = 0;
26213 for (i = 0; i < row->used[area];)
26215 if (row->glyphs[area][i].overlaps_vertically_p)
26217 int start = i, start_x = x;
26221 x += row->glyphs[area][i].pixel_width;
26222 ++i;
26224 while (i < row->used[area]
26225 && row->glyphs[area][i].overlaps_vertically_p);
26227 draw_glyphs (w, start_x, row, area,
26228 start, i,
26229 DRAW_NORMAL_TEXT, overlaps);
26231 else
26233 x += row->glyphs[area][i].pixel_width;
26234 ++i;
26238 unblock_input ();
26242 /* EXPORT:
26243 Draw the cursor glyph of window W in glyph row ROW. See the
26244 comment of draw_glyphs for the meaning of HL. */
26246 void
26247 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26248 enum draw_glyphs_face hl)
26250 /* If cursor hpos is out of bounds, don't draw garbage. This can
26251 happen in mini-buffer windows when switching between echo area
26252 glyphs and mini-buffer. */
26253 if ((row->reversed_p
26254 ? (w->phys_cursor.hpos >= 0)
26255 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26257 int on_p = w->phys_cursor_on_p;
26258 int x1;
26259 int hpos = w->phys_cursor.hpos;
26261 /* When the window is hscrolled, cursor hpos can legitimately be
26262 out of bounds, but we draw the cursor at the corresponding
26263 window margin in that case. */
26264 if (!row->reversed_p && hpos < 0)
26265 hpos = 0;
26266 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26267 hpos = row->used[TEXT_AREA] - 1;
26269 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26270 hl, 0);
26271 w->phys_cursor_on_p = on_p;
26273 if (hl == DRAW_CURSOR)
26274 w->phys_cursor_width = x1 - w->phys_cursor.x;
26275 /* When we erase the cursor, and ROW is overlapped by other
26276 rows, make sure that these overlapping parts of other rows
26277 are redrawn. */
26278 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26280 w->phys_cursor_width = x1 - w->phys_cursor.x;
26282 if (row > w->current_matrix->rows
26283 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26284 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26285 OVERLAPS_ERASED_CURSOR);
26287 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26288 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26289 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26290 OVERLAPS_ERASED_CURSOR);
26296 /* EXPORT:
26297 Erase the image of a cursor of window W from the screen. */
26299 void
26300 erase_phys_cursor (struct window *w)
26302 struct frame *f = XFRAME (w->frame);
26303 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26304 int hpos = w->phys_cursor.hpos;
26305 int vpos = w->phys_cursor.vpos;
26306 int mouse_face_here_p = 0;
26307 struct glyph_matrix *active_glyphs = w->current_matrix;
26308 struct glyph_row *cursor_row;
26309 struct glyph *cursor_glyph;
26310 enum draw_glyphs_face hl;
26312 /* No cursor displayed or row invalidated => nothing to do on the
26313 screen. */
26314 if (w->phys_cursor_type == NO_CURSOR)
26315 goto mark_cursor_off;
26317 /* VPOS >= active_glyphs->nrows means that window has been resized.
26318 Don't bother to erase the cursor. */
26319 if (vpos >= active_glyphs->nrows)
26320 goto mark_cursor_off;
26322 /* If row containing cursor is marked invalid, there is nothing we
26323 can do. */
26324 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26325 if (!cursor_row->enabled_p)
26326 goto mark_cursor_off;
26328 /* If line spacing is > 0, old cursor may only be partially visible in
26329 window after split-window. So adjust visible height. */
26330 cursor_row->visible_height = min (cursor_row->visible_height,
26331 window_text_bottom_y (w) - cursor_row->y);
26333 /* If row is completely invisible, don't attempt to delete a cursor which
26334 isn't there. This can happen if cursor is at top of a window, and
26335 we switch to a buffer with a header line in that window. */
26336 if (cursor_row->visible_height <= 0)
26337 goto mark_cursor_off;
26339 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26340 if (cursor_row->cursor_in_fringe_p)
26342 cursor_row->cursor_in_fringe_p = 0;
26343 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26344 goto mark_cursor_off;
26347 /* This can happen when the new row is shorter than the old one.
26348 In this case, either draw_glyphs or clear_end_of_line
26349 should have cleared the cursor. Note that we wouldn't be
26350 able to erase the cursor in this case because we don't have a
26351 cursor glyph at hand. */
26352 if ((cursor_row->reversed_p
26353 ? (w->phys_cursor.hpos < 0)
26354 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26355 goto mark_cursor_off;
26357 /* When the window is hscrolled, cursor hpos can legitimately be out
26358 of bounds, but we draw the cursor at the corresponding window
26359 margin in that case. */
26360 if (!cursor_row->reversed_p && hpos < 0)
26361 hpos = 0;
26362 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26363 hpos = cursor_row->used[TEXT_AREA] - 1;
26365 /* If the cursor is in the mouse face area, redisplay that when
26366 we clear the cursor. */
26367 if (! NILP (hlinfo->mouse_face_window)
26368 && coords_in_mouse_face_p (w, hpos, vpos)
26369 /* Don't redraw the cursor's spot in mouse face if it is at the
26370 end of a line (on a newline). The cursor appears there, but
26371 mouse highlighting does not. */
26372 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26373 mouse_face_here_p = 1;
26375 /* Maybe clear the display under the cursor. */
26376 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26378 int x, y, left_x;
26379 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26380 int width;
26382 cursor_glyph = get_phys_cursor_glyph (w);
26383 if (cursor_glyph == NULL)
26384 goto mark_cursor_off;
26386 width = cursor_glyph->pixel_width;
26387 left_x = window_box_left_offset (w, TEXT_AREA);
26388 x = w->phys_cursor.x;
26389 if (x < left_x)
26390 width -= left_x - x;
26391 width = min (width, window_box_width (w, TEXT_AREA) - x);
26392 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26393 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26395 if (width > 0)
26396 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26399 /* Erase the cursor by redrawing the character underneath it. */
26400 if (mouse_face_here_p)
26401 hl = DRAW_MOUSE_FACE;
26402 else
26403 hl = DRAW_NORMAL_TEXT;
26404 draw_phys_cursor_glyph (w, cursor_row, hl);
26406 mark_cursor_off:
26407 w->phys_cursor_on_p = 0;
26408 w->phys_cursor_type = NO_CURSOR;
26412 /* EXPORT:
26413 Display or clear cursor of window W. If ON is zero, clear the
26414 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26415 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26417 void
26418 display_and_set_cursor (struct window *w, bool on,
26419 int hpos, int vpos, int x, int y)
26421 struct frame *f = XFRAME (w->frame);
26422 int new_cursor_type;
26423 int new_cursor_width;
26424 int active_cursor;
26425 struct glyph_row *glyph_row;
26426 struct glyph *glyph;
26428 /* This is pointless on invisible frames, and dangerous on garbaged
26429 windows and frames; in the latter case, the frame or window may
26430 be in the midst of changing its size, and x and y may be off the
26431 window. */
26432 if (! FRAME_VISIBLE_P (f)
26433 || FRAME_GARBAGED_P (f)
26434 || vpos >= w->current_matrix->nrows
26435 || hpos >= w->current_matrix->matrix_w)
26436 return;
26438 /* If cursor is off and we want it off, return quickly. */
26439 if (!on && !w->phys_cursor_on_p)
26440 return;
26442 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26443 /* If cursor row is not enabled, we don't really know where to
26444 display the cursor. */
26445 if (!glyph_row->enabled_p)
26447 w->phys_cursor_on_p = 0;
26448 return;
26451 glyph = NULL;
26452 if (!glyph_row->exact_window_width_line_p
26453 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26454 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26456 eassert (input_blocked_p ());
26458 /* Set new_cursor_type to the cursor we want to be displayed. */
26459 new_cursor_type = get_window_cursor_type (w, glyph,
26460 &new_cursor_width, &active_cursor);
26462 /* If cursor is currently being shown and we don't want it to be or
26463 it is in the wrong place, or the cursor type is not what we want,
26464 erase it. */
26465 if (w->phys_cursor_on_p
26466 && (!on
26467 || w->phys_cursor.x != x
26468 || w->phys_cursor.y != y
26469 || new_cursor_type != w->phys_cursor_type
26470 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26471 && new_cursor_width != w->phys_cursor_width)))
26472 erase_phys_cursor (w);
26474 /* Don't check phys_cursor_on_p here because that flag is only set
26475 to zero in some cases where we know that the cursor has been
26476 completely erased, to avoid the extra work of erasing the cursor
26477 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26478 still not be visible, or it has only been partly erased. */
26479 if (on)
26481 w->phys_cursor_ascent = glyph_row->ascent;
26482 w->phys_cursor_height = glyph_row->height;
26484 /* Set phys_cursor_.* before x_draw_.* is called because some
26485 of them may need the information. */
26486 w->phys_cursor.x = x;
26487 w->phys_cursor.y = glyph_row->y;
26488 w->phys_cursor.hpos = hpos;
26489 w->phys_cursor.vpos = vpos;
26492 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26493 new_cursor_type, new_cursor_width,
26494 on, active_cursor);
26498 /* Switch the display of W's cursor on or off, according to the value
26499 of ON. */
26501 static void
26502 update_window_cursor (struct window *w, bool on)
26504 /* Don't update cursor in windows whose frame is in the process
26505 of being deleted. */
26506 if (w->current_matrix)
26508 int hpos = w->phys_cursor.hpos;
26509 int vpos = w->phys_cursor.vpos;
26510 struct glyph_row *row;
26512 if (vpos >= w->current_matrix->nrows
26513 || hpos >= w->current_matrix->matrix_w)
26514 return;
26516 row = MATRIX_ROW (w->current_matrix, vpos);
26518 /* When the window is hscrolled, cursor hpos can legitimately be
26519 out of bounds, but we draw the cursor at the corresponding
26520 window margin in that case. */
26521 if (!row->reversed_p && hpos < 0)
26522 hpos = 0;
26523 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26524 hpos = row->used[TEXT_AREA] - 1;
26526 block_input ();
26527 display_and_set_cursor (w, on, hpos, vpos,
26528 w->phys_cursor.x, w->phys_cursor.y);
26529 unblock_input ();
26534 /* Call update_window_cursor with parameter ON_P on all leaf windows
26535 in the window tree rooted at W. */
26537 static void
26538 update_cursor_in_window_tree (struct window *w, bool on_p)
26540 while (w)
26542 if (WINDOWP (w->contents))
26543 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26544 else
26545 update_window_cursor (w, on_p);
26547 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26552 /* EXPORT:
26553 Display the cursor on window W, or clear it, according to ON_P.
26554 Don't change the cursor's position. */
26556 void
26557 x_update_cursor (struct frame *f, bool on_p)
26559 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26563 /* EXPORT:
26564 Clear the cursor of window W to background color, and mark the
26565 cursor as not shown. This is used when the text where the cursor
26566 is about to be rewritten. */
26568 void
26569 x_clear_cursor (struct window *w)
26571 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26572 update_window_cursor (w, 0);
26575 #endif /* HAVE_WINDOW_SYSTEM */
26577 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26578 and MSDOS. */
26579 static void
26580 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26581 int start_hpos, int end_hpos,
26582 enum draw_glyphs_face draw)
26584 #ifdef HAVE_WINDOW_SYSTEM
26585 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26587 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26588 return;
26590 #endif
26591 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26592 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26593 #endif
26596 /* Display the active region described by mouse_face_* according to DRAW. */
26598 static void
26599 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26601 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26602 struct frame *f = XFRAME (WINDOW_FRAME (w));
26604 if (/* If window is in the process of being destroyed, don't bother
26605 to do anything. */
26606 w->current_matrix != NULL
26607 /* Don't update mouse highlight if hidden */
26608 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26609 /* Recognize when we are called to operate on rows that don't exist
26610 anymore. This can happen when a window is split. */
26611 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26613 int phys_cursor_on_p = w->phys_cursor_on_p;
26614 struct glyph_row *row, *first, *last;
26616 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26617 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26619 for (row = first; row <= last && row->enabled_p; ++row)
26621 int start_hpos, end_hpos, start_x;
26623 /* For all but the first row, the highlight starts at column 0. */
26624 if (row == first)
26626 /* R2L rows have BEG and END in reversed order, but the
26627 screen drawing geometry is always left to right. So
26628 we need to mirror the beginning and end of the
26629 highlighted area in R2L rows. */
26630 if (!row->reversed_p)
26632 start_hpos = hlinfo->mouse_face_beg_col;
26633 start_x = hlinfo->mouse_face_beg_x;
26635 else if (row == last)
26637 start_hpos = hlinfo->mouse_face_end_col;
26638 start_x = hlinfo->mouse_face_end_x;
26640 else
26642 start_hpos = 0;
26643 start_x = 0;
26646 else if (row->reversed_p && row == last)
26648 start_hpos = hlinfo->mouse_face_end_col;
26649 start_x = hlinfo->mouse_face_end_x;
26651 else
26653 start_hpos = 0;
26654 start_x = 0;
26657 if (row == last)
26659 if (!row->reversed_p)
26660 end_hpos = hlinfo->mouse_face_end_col;
26661 else if (row == first)
26662 end_hpos = hlinfo->mouse_face_beg_col;
26663 else
26665 end_hpos = row->used[TEXT_AREA];
26666 if (draw == DRAW_NORMAL_TEXT)
26667 row->fill_line_p = 1; /* Clear to end of line */
26670 else if (row->reversed_p && row == first)
26671 end_hpos = hlinfo->mouse_face_beg_col;
26672 else
26674 end_hpos = row->used[TEXT_AREA];
26675 if (draw == DRAW_NORMAL_TEXT)
26676 row->fill_line_p = 1; /* Clear to end of line */
26679 if (end_hpos > start_hpos)
26681 draw_row_with_mouse_face (w, start_x, row,
26682 start_hpos, end_hpos, draw);
26684 row->mouse_face_p
26685 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26689 #ifdef HAVE_WINDOW_SYSTEM
26690 /* When we've written over the cursor, arrange for it to
26691 be displayed again. */
26692 if (FRAME_WINDOW_P (f)
26693 && phys_cursor_on_p && !w->phys_cursor_on_p)
26695 int hpos = w->phys_cursor.hpos;
26697 /* When the window is hscrolled, cursor hpos can legitimately be
26698 out of bounds, but we draw the cursor at the corresponding
26699 window margin in that case. */
26700 if (!row->reversed_p && hpos < 0)
26701 hpos = 0;
26702 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26703 hpos = row->used[TEXT_AREA] - 1;
26705 block_input ();
26706 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26707 w->phys_cursor.x, w->phys_cursor.y);
26708 unblock_input ();
26710 #endif /* HAVE_WINDOW_SYSTEM */
26713 #ifdef HAVE_WINDOW_SYSTEM
26714 /* Change the mouse cursor. */
26715 if (FRAME_WINDOW_P (f))
26717 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
26718 if (draw == DRAW_NORMAL_TEXT
26719 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26720 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26721 else
26722 #endif
26723 if (draw == DRAW_MOUSE_FACE)
26724 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26725 else
26726 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26728 #endif /* HAVE_WINDOW_SYSTEM */
26731 /* EXPORT:
26732 Clear out the mouse-highlighted active region.
26733 Redraw it un-highlighted first. Value is non-zero if mouse
26734 face was actually drawn unhighlighted. */
26737 clear_mouse_face (Mouse_HLInfo *hlinfo)
26739 int cleared = 0;
26741 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26743 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26744 cleared = 1;
26747 reset_mouse_highlight (hlinfo);
26748 return cleared;
26751 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26752 within the mouse face on that window. */
26753 static int
26754 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26756 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26758 /* Quickly resolve the easy cases. */
26759 if (!(WINDOWP (hlinfo->mouse_face_window)
26760 && XWINDOW (hlinfo->mouse_face_window) == w))
26761 return 0;
26762 if (vpos < hlinfo->mouse_face_beg_row
26763 || vpos > hlinfo->mouse_face_end_row)
26764 return 0;
26765 if (vpos > hlinfo->mouse_face_beg_row
26766 && vpos < hlinfo->mouse_face_end_row)
26767 return 1;
26769 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26771 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26773 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26774 return 1;
26776 else if ((vpos == hlinfo->mouse_face_beg_row
26777 && hpos >= hlinfo->mouse_face_beg_col)
26778 || (vpos == hlinfo->mouse_face_end_row
26779 && hpos < hlinfo->mouse_face_end_col))
26780 return 1;
26782 else
26784 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26786 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26787 return 1;
26789 else if ((vpos == hlinfo->mouse_face_beg_row
26790 && hpos <= hlinfo->mouse_face_beg_col)
26791 || (vpos == hlinfo->mouse_face_end_row
26792 && hpos > hlinfo->mouse_face_end_col))
26793 return 1;
26795 return 0;
26799 /* EXPORT:
26800 Non-zero if physical cursor of window W is within mouse face. */
26803 cursor_in_mouse_face_p (struct window *w)
26805 int hpos = w->phys_cursor.hpos;
26806 int vpos = w->phys_cursor.vpos;
26807 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26809 /* When the window is hscrolled, cursor hpos can legitimately be out
26810 of bounds, but we draw the cursor at the corresponding window
26811 margin in that case. */
26812 if (!row->reversed_p && hpos < 0)
26813 hpos = 0;
26814 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26815 hpos = row->used[TEXT_AREA] - 1;
26817 return coords_in_mouse_face_p (w, hpos, vpos);
26822 /* Find the glyph rows START_ROW and END_ROW of window W that display
26823 characters between buffer positions START_CHARPOS and END_CHARPOS
26824 (excluding END_CHARPOS). DISP_STRING is a display string that
26825 covers these buffer positions. This is similar to
26826 row_containing_pos, but is more accurate when bidi reordering makes
26827 buffer positions change non-linearly with glyph rows. */
26828 static void
26829 rows_from_pos_range (struct window *w,
26830 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26831 Lisp_Object disp_string,
26832 struct glyph_row **start, struct glyph_row **end)
26834 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26835 int last_y = window_text_bottom_y (w);
26836 struct glyph_row *row;
26838 *start = NULL;
26839 *end = NULL;
26841 while (!first->enabled_p
26842 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26843 first++;
26845 /* Find the START row. */
26846 for (row = first;
26847 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26848 row++)
26850 /* A row can potentially be the START row if the range of the
26851 characters it displays intersects the range
26852 [START_CHARPOS..END_CHARPOS). */
26853 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26854 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26855 /* See the commentary in row_containing_pos, for the
26856 explanation of the complicated way to check whether
26857 some position is beyond the end of the characters
26858 displayed by a row. */
26859 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26860 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26861 && !row->ends_at_zv_p
26862 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26863 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26864 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26865 && !row->ends_at_zv_p
26866 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26868 /* Found a candidate row. Now make sure at least one of the
26869 glyphs it displays has a charpos from the range
26870 [START_CHARPOS..END_CHARPOS).
26872 This is not obvious because bidi reordering could make
26873 buffer positions of a row be 1,2,3,102,101,100, and if we
26874 want to highlight characters in [50..60), we don't want
26875 this row, even though [50..60) does intersect [1..103),
26876 the range of character positions given by the row's start
26877 and end positions. */
26878 struct glyph *g = row->glyphs[TEXT_AREA];
26879 struct glyph *e = g + row->used[TEXT_AREA];
26881 while (g < e)
26883 if (((BUFFERP (g->object) || INTEGERP (g->object))
26884 && start_charpos <= g->charpos && g->charpos < end_charpos)
26885 /* A glyph that comes from DISP_STRING is by
26886 definition to be highlighted. */
26887 || EQ (g->object, disp_string))
26888 *start = row;
26889 g++;
26891 if (*start)
26892 break;
26896 /* Find the END row. */
26897 if (!*start
26898 /* If the last row is partially visible, start looking for END
26899 from that row, instead of starting from FIRST. */
26900 && !(row->enabled_p
26901 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26902 row = first;
26903 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26905 struct glyph_row *next = row + 1;
26906 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26908 if (!next->enabled_p
26909 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26910 /* The first row >= START whose range of displayed characters
26911 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26912 is the row END + 1. */
26913 || (start_charpos < next_start
26914 && end_charpos < next_start)
26915 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26916 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26917 && !next->ends_at_zv_p
26918 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26919 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26920 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26921 && !next->ends_at_zv_p
26922 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26924 *end = row;
26925 break;
26927 else
26929 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26930 but none of the characters it displays are in the range, it is
26931 also END + 1. */
26932 struct glyph *g = next->glyphs[TEXT_AREA];
26933 struct glyph *s = g;
26934 struct glyph *e = g + next->used[TEXT_AREA];
26936 while (g < e)
26938 if (((BUFFERP (g->object) || INTEGERP (g->object))
26939 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26940 /* If the buffer position of the first glyph in
26941 the row is equal to END_CHARPOS, it means
26942 the last character to be highlighted is the
26943 newline of ROW, and we must consider NEXT as
26944 END, not END+1. */
26945 || (((!next->reversed_p && g == s)
26946 || (next->reversed_p && g == e - 1))
26947 && (g->charpos == end_charpos
26948 /* Special case for when NEXT is an
26949 empty line at ZV. */
26950 || (g->charpos == -1
26951 && !row->ends_at_zv_p
26952 && next_start == end_charpos)))))
26953 /* A glyph that comes from DISP_STRING is by
26954 definition to be highlighted. */
26955 || EQ (g->object, disp_string))
26956 break;
26957 g++;
26959 if (g == e)
26961 *end = row;
26962 break;
26964 /* The first row that ends at ZV must be the last to be
26965 highlighted. */
26966 else if (next->ends_at_zv_p)
26968 *end = next;
26969 break;
26975 /* This function sets the mouse_face_* elements of HLINFO, assuming
26976 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26977 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26978 for the overlay or run of text properties specifying the mouse
26979 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26980 before-string and after-string that must also be highlighted.
26981 DISP_STRING, if non-nil, is a display string that may cover some
26982 or all of the highlighted text. */
26984 static void
26985 mouse_face_from_buffer_pos (Lisp_Object window,
26986 Mouse_HLInfo *hlinfo,
26987 ptrdiff_t mouse_charpos,
26988 ptrdiff_t start_charpos,
26989 ptrdiff_t end_charpos,
26990 Lisp_Object before_string,
26991 Lisp_Object after_string,
26992 Lisp_Object disp_string)
26994 struct window *w = XWINDOW (window);
26995 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26996 struct glyph_row *r1, *r2;
26997 struct glyph *glyph, *end;
26998 ptrdiff_t ignore, pos;
26999 int x;
27001 eassert (NILP (disp_string) || STRINGP (disp_string));
27002 eassert (NILP (before_string) || STRINGP (before_string));
27003 eassert (NILP (after_string) || STRINGP (after_string));
27005 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27006 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27007 if (r1 == NULL)
27008 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27009 /* If the before-string or display-string contains newlines,
27010 rows_from_pos_range skips to its last row. Move back. */
27011 if (!NILP (before_string) || !NILP (disp_string))
27013 struct glyph_row *prev;
27014 while ((prev = r1 - 1, prev >= first)
27015 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27016 && prev->used[TEXT_AREA] > 0)
27018 struct glyph *beg = prev->glyphs[TEXT_AREA];
27019 glyph = beg + prev->used[TEXT_AREA];
27020 while (--glyph >= beg && INTEGERP (glyph->object));
27021 if (glyph < beg
27022 || !(EQ (glyph->object, before_string)
27023 || EQ (glyph->object, disp_string)))
27024 break;
27025 r1 = prev;
27028 if (r2 == NULL)
27030 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27031 hlinfo->mouse_face_past_end = 1;
27033 else if (!NILP (after_string))
27035 /* If the after-string has newlines, advance to its last row. */
27036 struct glyph_row *next;
27037 struct glyph_row *last
27038 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27040 for (next = r2 + 1;
27041 next <= last
27042 && next->used[TEXT_AREA] > 0
27043 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27044 ++next)
27045 r2 = next;
27047 /* The rest of the display engine assumes that mouse_face_beg_row is
27048 either above mouse_face_end_row or identical to it. But with
27049 bidi-reordered continued lines, the row for START_CHARPOS could
27050 be below the row for END_CHARPOS. If so, swap the rows and store
27051 them in correct order. */
27052 if (r1->y > r2->y)
27054 struct glyph_row *tem = r2;
27056 r2 = r1;
27057 r1 = tem;
27060 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27061 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27063 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27064 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27065 could be anywhere in the row and in any order. The strategy
27066 below is to find the leftmost and the rightmost glyph that
27067 belongs to either of these 3 strings, or whose position is
27068 between START_CHARPOS and END_CHARPOS, and highlight all the
27069 glyphs between those two. This may cover more than just the text
27070 between START_CHARPOS and END_CHARPOS if the range of characters
27071 strides the bidi level boundary, e.g. if the beginning is in R2L
27072 text while the end is in L2R text or vice versa. */
27073 if (!r1->reversed_p)
27075 /* This row is in a left to right paragraph. Scan it left to
27076 right. */
27077 glyph = r1->glyphs[TEXT_AREA];
27078 end = glyph + r1->used[TEXT_AREA];
27079 x = r1->x;
27081 /* Skip truncation glyphs at the start of the glyph row. */
27082 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27083 for (; glyph < end
27084 && INTEGERP (glyph->object)
27085 && glyph->charpos < 0;
27086 ++glyph)
27087 x += glyph->pixel_width;
27089 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27090 or DISP_STRING, and the first glyph from buffer whose
27091 position is between START_CHARPOS and END_CHARPOS. */
27092 for (; glyph < end
27093 && !INTEGERP (glyph->object)
27094 && !EQ (glyph->object, disp_string)
27095 && !(BUFFERP (glyph->object)
27096 && (glyph->charpos >= start_charpos
27097 && glyph->charpos < end_charpos));
27098 ++glyph)
27100 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27101 are present at buffer positions between START_CHARPOS and
27102 END_CHARPOS, or if they come from an overlay. */
27103 if (EQ (glyph->object, before_string))
27105 pos = string_buffer_position (before_string,
27106 start_charpos);
27107 /* If pos == 0, it means before_string came from an
27108 overlay, not from a buffer position. */
27109 if (!pos || (pos >= start_charpos && pos < end_charpos))
27110 break;
27112 else if (EQ (glyph->object, after_string))
27114 pos = string_buffer_position (after_string, end_charpos);
27115 if (!pos || (pos >= start_charpos && pos < end_charpos))
27116 break;
27118 x += glyph->pixel_width;
27120 hlinfo->mouse_face_beg_x = x;
27121 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27123 else
27125 /* This row is in a right to left paragraph. Scan it right to
27126 left. */
27127 struct glyph *g;
27129 end = r1->glyphs[TEXT_AREA] - 1;
27130 glyph = end + r1->used[TEXT_AREA];
27132 /* Skip truncation glyphs at the start of the glyph row. */
27133 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27134 for (; glyph > end
27135 && INTEGERP (glyph->object)
27136 && glyph->charpos < 0;
27137 --glyph)
27140 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27141 or DISP_STRING, and the first glyph from buffer whose
27142 position is between START_CHARPOS and END_CHARPOS. */
27143 for (; glyph > end
27144 && !INTEGERP (glyph->object)
27145 && !EQ (glyph->object, disp_string)
27146 && !(BUFFERP (glyph->object)
27147 && (glyph->charpos >= start_charpos
27148 && glyph->charpos < end_charpos));
27149 --glyph)
27151 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27152 are present at buffer positions between START_CHARPOS and
27153 END_CHARPOS, or if they come from an overlay. */
27154 if (EQ (glyph->object, before_string))
27156 pos = string_buffer_position (before_string, start_charpos);
27157 /* If pos == 0, it means before_string came from an
27158 overlay, not from a buffer position. */
27159 if (!pos || (pos >= start_charpos && pos < end_charpos))
27160 break;
27162 else if (EQ (glyph->object, after_string))
27164 pos = string_buffer_position (after_string, end_charpos);
27165 if (!pos || (pos >= start_charpos && pos < end_charpos))
27166 break;
27170 glyph++; /* first glyph to the right of the highlighted area */
27171 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27172 x += g->pixel_width;
27173 hlinfo->mouse_face_beg_x = x;
27174 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27177 /* If the highlight ends in a different row, compute GLYPH and END
27178 for the end row. Otherwise, reuse the values computed above for
27179 the row where the highlight begins. */
27180 if (r2 != r1)
27182 if (!r2->reversed_p)
27184 glyph = r2->glyphs[TEXT_AREA];
27185 end = glyph + r2->used[TEXT_AREA];
27186 x = r2->x;
27188 else
27190 end = r2->glyphs[TEXT_AREA] - 1;
27191 glyph = end + r2->used[TEXT_AREA];
27195 if (!r2->reversed_p)
27197 /* Skip truncation and continuation glyphs near the end of the
27198 row, and also blanks and stretch glyphs inserted by
27199 extend_face_to_end_of_line. */
27200 while (end > glyph
27201 && INTEGERP ((end - 1)->object))
27202 --end;
27203 /* Scan the rest of the glyph row from the end, looking for the
27204 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27205 DISP_STRING, or whose position is between START_CHARPOS
27206 and END_CHARPOS */
27207 for (--end;
27208 end > glyph
27209 && !INTEGERP (end->object)
27210 && !EQ (end->object, disp_string)
27211 && !(BUFFERP (end->object)
27212 && (end->charpos >= start_charpos
27213 && end->charpos < end_charpos));
27214 --end)
27216 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27217 are present at buffer positions between START_CHARPOS and
27218 END_CHARPOS, or if they come from an overlay. */
27219 if (EQ (end->object, before_string))
27221 pos = string_buffer_position (before_string, start_charpos);
27222 if (!pos || (pos >= start_charpos && pos < end_charpos))
27223 break;
27225 else if (EQ (end->object, after_string))
27227 pos = string_buffer_position (after_string, end_charpos);
27228 if (!pos || (pos >= start_charpos && pos < end_charpos))
27229 break;
27232 /* Find the X coordinate of the last glyph to be highlighted. */
27233 for (; glyph <= end; ++glyph)
27234 x += glyph->pixel_width;
27236 hlinfo->mouse_face_end_x = x;
27237 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27239 else
27241 /* Skip truncation and continuation glyphs near the end of the
27242 row, and also blanks and stretch glyphs inserted by
27243 extend_face_to_end_of_line. */
27244 x = r2->x;
27245 end++;
27246 while (end < glyph
27247 && INTEGERP (end->object))
27249 x += end->pixel_width;
27250 ++end;
27252 /* Scan the rest of the glyph row from the end, looking for the
27253 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27254 DISP_STRING, or whose position is between START_CHARPOS
27255 and END_CHARPOS */
27256 for ( ;
27257 end < glyph
27258 && !INTEGERP (end->object)
27259 && !EQ (end->object, disp_string)
27260 && !(BUFFERP (end->object)
27261 && (end->charpos >= start_charpos
27262 && end->charpos < end_charpos));
27263 ++end)
27265 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27266 are present at buffer positions between START_CHARPOS and
27267 END_CHARPOS, or if they come from an overlay. */
27268 if (EQ (end->object, before_string))
27270 pos = string_buffer_position (before_string, start_charpos);
27271 if (!pos || (pos >= start_charpos && pos < end_charpos))
27272 break;
27274 else if (EQ (end->object, after_string))
27276 pos = string_buffer_position (after_string, end_charpos);
27277 if (!pos || (pos >= start_charpos && pos < end_charpos))
27278 break;
27280 x += end->pixel_width;
27282 /* If we exited the above loop because we arrived at the last
27283 glyph of the row, and its buffer position is still not in
27284 range, it means the last character in range is the preceding
27285 newline. Bump the end column and x values to get past the
27286 last glyph. */
27287 if (end == glyph
27288 && BUFFERP (end->object)
27289 && (end->charpos < start_charpos
27290 || end->charpos >= end_charpos))
27292 x += end->pixel_width;
27293 ++end;
27295 hlinfo->mouse_face_end_x = x;
27296 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27299 hlinfo->mouse_face_window = window;
27300 hlinfo->mouse_face_face_id
27301 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27302 mouse_charpos + 1,
27303 !hlinfo->mouse_face_hidden, -1);
27304 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27307 /* The following function is not used anymore (replaced with
27308 mouse_face_from_string_pos), but I leave it here for the time
27309 being, in case someone would. */
27311 #if 0 /* not used */
27313 /* Find the position of the glyph for position POS in OBJECT in
27314 window W's current matrix, and return in *X, *Y the pixel
27315 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27317 RIGHT_P non-zero means return the position of the right edge of the
27318 glyph, RIGHT_P zero means return the left edge position.
27320 If no glyph for POS exists in the matrix, return the position of
27321 the glyph with the next smaller position that is in the matrix, if
27322 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27323 exists in the matrix, return the position of the glyph with the
27324 next larger position in OBJECT.
27326 Value is non-zero if a glyph was found. */
27328 static int
27329 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27330 int *hpos, int *vpos, int *x, int *y, int right_p)
27332 int yb = window_text_bottom_y (w);
27333 struct glyph_row *r;
27334 struct glyph *best_glyph = NULL;
27335 struct glyph_row *best_row = NULL;
27336 int best_x = 0;
27338 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27339 r->enabled_p && r->y < yb;
27340 ++r)
27342 struct glyph *g = r->glyphs[TEXT_AREA];
27343 struct glyph *e = g + r->used[TEXT_AREA];
27344 int gx;
27346 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27347 if (EQ (g->object, object))
27349 if (g->charpos == pos)
27351 best_glyph = g;
27352 best_x = gx;
27353 best_row = r;
27354 goto found;
27356 else if (best_glyph == NULL
27357 || ((eabs (g->charpos - pos)
27358 < eabs (best_glyph->charpos - pos))
27359 && (right_p
27360 ? g->charpos < pos
27361 : g->charpos > pos)))
27363 best_glyph = g;
27364 best_x = gx;
27365 best_row = r;
27370 found:
27372 if (best_glyph)
27374 *x = best_x;
27375 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27377 if (right_p)
27379 *x += best_glyph->pixel_width;
27380 ++*hpos;
27383 *y = best_row->y;
27384 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27387 return best_glyph != NULL;
27389 #endif /* not used */
27391 /* Find the positions of the first and the last glyphs in window W's
27392 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27393 (assumed to be a string), and return in HLINFO's mouse_face_*
27394 members the pixel and column/row coordinates of those glyphs. */
27396 static void
27397 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27398 Lisp_Object object,
27399 ptrdiff_t startpos, ptrdiff_t endpos)
27401 int yb = window_text_bottom_y (w);
27402 struct glyph_row *r;
27403 struct glyph *g, *e;
27404 int gx;
27405 int found = 0;
27407 /* Find the glyph row with at least one position in the range
27408 [STARTPOS..ENDPOS], and the first glyph in that row whose
27409 position belongs to that range. */
27410 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27411 r->enabled_p && r->y < yb;
27412 ++r)
27414 if (!r->reversed_p)
27416 g = r->glyphs[TEXT_AREA];
27417 e = g + r->used[TEXT_AREA];
27418 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27419 if (EQ (g->object, object)
27420 && startpos <= g->charpos && g->charpos <= endpos)
27422 hlinfo->mouse_face_beg_row
27423 = MATRIX_ROW_VPOS (r, w->current_matrix);
27424 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27425 hlinfo->mouse_face_beg_x = gx;
27426 found = 1;
27427 break;
27430 else
27432 struct glyph *g1;
27434 e = r->glyphs[TEXT_AREA];
27435 g = e + r->used[TEXT_AREA];
27436 for ( ; g > e; --g)
27437 if (EQ ((g-1)->object, object)
27438 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27440 hlinfo->mouse_face_beg_row
27441 = MATRIX_ROW_VPOS (r, w->current_matrix);
27442 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27443 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27444 gx += g1->pixel_width;
27445 hlinfo->mouse_face_beg_x = gx;
27446 found = 1;
27447 break;
27450 if (found)
27451 break;
27454 if (!found)
27455 return;
27457 /* Starting with the next row, look for the first row which does NOT
27458 include any glyphs whose positions are in the range. */
27459 for (++r; r->enabled_p && r->y < yb; ++r)
27461 g = r->glyphs[TEXT_AREA];
27462 e = g + r->used[TEXT_AREA];
27463 found = 0;
27464 for ( ; g < e; ++g)
27465 if (EQ (g->object, object)
27466 && startpos <= g->charpos && g->charpos <= endpos)
27468 found = 1;
27469 break;
27471 if (!found)
27472 break;
27475 /* The highlighted region ends on the previous row. */
27476 r--;
27478 /* Set the end row. */
27479 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27481 /* Compute and set the end column and the end column's horizontal
27482 pixel coordinate. */
27483 if (!r->reversed_p)
27485 g = r->glyphs[TEXT_AREA];
27486 e = g + r->used[TEXT_AREA];
27487 for ( ; e > g; --e)
27488 if (EQ ((e-1)->object, object)
27489 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27490 break;
27491 hlinfo->mouse_face_end_col = e - g;
27493 for (gx = r->x; g < e; ++g)
27494 gx += g->pixel_width;
27495 hlinfo->mouse_face_end_x = gx;
27497 else
27499 e = r->glyphs[TEXT_AREA];
27500 g = e + r->used[TEXT_AREA];
27501 for (gx = r->x ; e < g; ++e)
27503 if (EQ (e->object, object)
27504 && startpos <= e->charpos && e->charpos <= endpos)
27505 break;
27506 gx += e->pixel_width;
27508 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27509 hlinfo->mouse_face_end_x = gx;
27513 #ifdef HAVE_WINDOW_SYSTEM
27515 /* See if position X, Y is within a hot-spot of an image. */
27517 static int
27518 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27520 if (!CONSP (hot_spot))
27521 return 0;
27523 if (EQ (XCAR (hot_spot), Qrect))
27525 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27526 Lisp_Object rect = XCDR (hot_spot);
27527 Lisp_Object tem;
27528 if (!CONSP (rect))
27529 return 0;
27530 if (!CONSP (XCAR (rect)))
27531 return 0;
27532 if (!CONSP (XCDR (rect)))
27533 return 0;
27534 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27535 return 0;
27536 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27537 return 0;
27538 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27539 return 0;
27540 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27541 return 0;
27542 return 1;
27544 else if (EQ (XCAR (hot_spot), Qcircle))
27546 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27547 Lisp_Object circ = XCDR (hot_spot);
27548 Lisp_Object lr, lx0, ly0;
27549 if (CONSP (circ)
27550 && CONSP (XCAR (circ))
27551 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27552 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27553 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27555 double r = XFLOATINT (lr);
27556 double dx = XINT (lx0) - x;
27557 double dy = XINT (ly0) - y;
27558 return (dx * dx + dy * dy <= r * r);
27561 else if (EQ (XCAR (hot_spot), Qpoly))
27563 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27564 if (VECTORP (XCDR (hot_spot)))
27566 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27567 Lisp_Object *poly = v->contents;
27568 ptrdiff_t n = v->header.size;
27569 ptrdiff_t i;
27570 int inside = 0;
27571 Lisp_Object lx, ly;
27572 int x0, y0;
27574 /* Need an even number of coordinates, and at least 3 edges. */
27575 if (n < 6 || n & 1)
27576 return 0;
27578 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27579 If count is odd, we are inside polygon. Pixels on edges
27580 may or may not be included depending on actual geometry of the
27581 polygon. */
27582 if ((lx = poly[n-2], !INTEGERP (lx))
27583 || (ly = poly[n-1], !INTEGERP (lx)))
27584 return 0;
27585 x0 = XINT (lx), y0 = XINT (ly);
27586 for (i = 0; i < n; i += 2)
27588 int x1 = x0, y1 = y0;
27589 if ((lx = poly[i], !INTEGERP (lx))
27590 || (ly = poly[i+1], !INTEGERP (ly)))
27591 return 0;
27592 x0 = XINT (lx), y0 = XINT (ly);
27594 /* Does this segment cross the X line? */
27595 if (x0 >= x)
27597 if (x1 >= x)
27598 continue;
27600 else if (x1 < x)
27601 continue;
27602 if (y > y0 && y > y1)
27603 continue;
27604 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27605 inside = !inside;
27607 return inside;
27610 return 0;
27613 Lisp_Object
27614 find_hot_spot (Lisp_Object map, int x, int y)
27616 while (CONSP (map))
27618 if (CONSP (XCAR (map))
27619 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27620 return XCAR (map);
27621 map = XCDR (map);
27624 return Qnil;
27627 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27628 3, 3, 0,
27629 doc: /* Lookup in image map MAP coordinates X and Y.
27630 An image map is an alist where each element has the format (AREA ID PLIST).
27631 An AREA is specified as either a rectangle, a circle, or a polygon:
27632 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27633 pixel coordinates of the upper left and bottom right corners.
27634 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27635 and the radius of the circle; r may be a float or integer.
27636 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27637 vector describes one corner in the polygon.
27638 Returns the alist element for the first matching AREA in MAP. */)
27639 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27641 if (NILP (map))
27642 return Qnil;
27644 CHECK_NUMBER (x);
27645 CHECK_NUMBER (y);
27647 return find_hot_spot (map,
27648 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27649 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27653 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27654 static void
27655 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27657 /* Do not change cursor shape while dragging mouse. */
27658 if (!NILP (do_mouse_tracking))
27659 return;
27661 if (!NILP (pointer))
27663 if (EQ (pointer, Qarrow))
27664 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27665 else if (EQ (pointer, Qhand))
27666 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27667 else if (EQ (pointer, Qtext))
27668 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27669 else if (EQ (pointer, intern ("hdrag")))
27670 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27671 #ifdef HAVE_X_WINDOWS
27672 else if (EQ (pointer, intern ("vdrag")))
27673 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27674 #endif
27675 else if (EQ (pointer, intern ("hourglass")))
27676 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27677 else if (EQ (pointer, Qmodeline))
27678 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27679 else
27680 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27683 if (cursor != No_Cursor)
27684 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27687 #endif /* HAVE_WINDOW_SYSTEM */
27689 /* Take proper action when mouse has moved to the mode or header line
27690 or marginal area AREA of window W, x-position X and y-position Y.
27691 X is relative to the start of the text display area of W, so the
27692 width of bitmap areas and scroll bars must be subtracted to get a
27693 position relative to the start of the mode line. */
27695 static void
27696 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27697 enum window_part area)
27699 struct window *w = XWINDOW (window);
27700 struct frame *f = XFRAME (w->frame);
27701 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27702 #ifdef HAVE_WINDOW_SYSTEM
27703 Display_Info *dpyinfo;
27704 #endif
27705 Cursor cursor = No_Cursor;
27706 Lisp_Object pointer = Qnil;
27707 int dx, dy, width, height;
27708 ptrdiff_t charpos;
27709 Lisp_Object string, object = Qnil;
27710 Lisp_Object pos IF_LINT (= Qnil), help;
27712 Lisp_Object mouse_face;
27713 int original_x_pixel = x;
27714 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27715 struct glyph_row *row IF_LINT (= 0);
27717 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27719 int x0;
27720 struct glyph *end;
27722 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27723 returns them in row/column units! */
27724 string = mode_line_string (w, area, &x, &y, &charpos,
27725 &object, &dx, &dy, &width, &height);
27727 row = (area == ON_MODE_LINE
27728 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27729 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27731 /* Find the glyph under the mouse pointer. */
27732 if (row->mode_line_p && row->enabled_p)
27734 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27735 end = glyph + row->used[TEXT_AREA];
27737 for (x0 = original_x_pixel;
27738 glyph < end && x0 >= glyph->pixel_width;
27739 ++glyph)
27740 x0 -= glyph->pixel_width;
27742 if (glyph >= end)
27743 glyph = NULL;
27746 else
27748 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27749 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27750 returns them in row/column units! */
27751 string = marginal_area_string (w, area, &x, &y, &charpos,
27752 &object, &dx, &dy, &width, &height);
27755 help = Qnil;
27757 #ifdef HAVE_WINDOW_SYSTEM
27758 if (IMAGEP (object))
27760 Lisp_Object image_map, hotspot;
27761 if ((image_map = Fplist_get (XCDR (object), QCmap),
27762 !NILP (image_map))
27763 && (hotspot = find_hot_spot (image_map, dx, dy),
27764 CONSP (hotspot))
27765 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27767 Lisp_Object plist;
27769 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27770 If so, we could look for mouse-enter, mouse-leave
27771 properties in PLIST (and do something...). */
27772 hotspot = XCDR (hotspot);
27773 if (CONSP (hotspot)
27774 && (plist = XCAR (hotspot), CONSP (plist)))
27776 pointer = Fplist_get (plist, Qpointer);
27777 if (NILP (pointer))
27778 pointer = Qhand;
27779 help = Fplist_get (plist, Qhelp_echo);
27780 if (!NILP (help))
27782 help_echo_string = help;
27783 XSETWINDOW (help_echo_window, w);
27784 help_echo_object = w->contents;
27785 help_echo_pos = charpos;
27789 if (NILP (pointer))
27790 pointer = Fplist_get (XCDR (object), QCpointer);
27792 #endif /* HAVE_WINDOW_SYSTEM */
27794 if (STRINGP (string))
27795 pos = make_number (charpos);
27797 /* Set the help text and mouse pointer. If the mouse is on a part
27798 of the mode line without any text (e.g. past the right edge of
27799 the mode line text), use the default help text and pointer. */
27800 if (STRINGP (string) || area == ON_MODE_LINE)
27802 /* Arrange to display the help by setting the global variables
27803 help_echo_string, help_echo_object, and help_echo_pos. */
27804 if (NILP (help))
27806 if (STRINGP (string))
27807 help = Fget_text_property (pos, Qhelp_echo, string);
27809 if (!NILP (help))
27811 help_echo_string = help;
27812 XSETWINDOW (help_echo_window, w);
27813 help_echo_object = string;
27814 help_echo_pos = charpos;
27816 else if (area == ON_MODE_LINE)
27818 Lisp_Object default_help
27819 = buffer_local_value_1 (Qmode_line_default_help_echo,
27820 w->contents);
27822 if (STRINGP (default_help))
27824 help_echo_string = default_help;
27825 XSETWINDOW (help_echo_window, w);
27826 help_echo_object = Qnil;
27827 help_echo_pos = -1;
27832 #ifdef HAVE_WINDOW_SYSTEM
27833 /* Change the mouse pointer according to what is under it. */
27834 if (FRAME_WINDOW_P (f))
27836 dpyinfo = FRAME_DISPLAY_INFO (f);
27837 if (STRINGP (string))
27839 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27841 if (NILP (pointer))
27842 pointer = Fget_text_property (pos, Qpointer, string);
27844 /* Change the mouse pointer according to what is under X/Y. */
27845 if (NILP (pointer)
27846 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27848 Lisp_Object map;
27849 map = Fget_text_property (pos, Qlocal_map, string);
27850 if (!KEYMAPP (map))
27851 map = Fget_text_property (pos, Qkeymap, string);
27852 if (!KEYMAPP (map))
27853 cursor = dpyinfo->vertical_scroll_bar_cursor;
27856 else
27857 /* Default mode-line pointer. */
27858 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27860 #endif
27863 /* Change the mouse face according to what is under X/Y. */
27864 if (STRINGP (string))
27866 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27867 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27868 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27869 && glyph)
27871 Lisp_Object b, e;
27873 struct glyph * tmp_glyph;
27875 int gpos;
27876 int gseq_length;
27877 int total_pixel_width;
27878 ptrdiff_t begpos, endpos, ignore;
27880 int vpos, hpos;
27882 b = Fprevious_single_property_change (make_number (charpos + 1),
27883 Qmouse_face, string, Qnil);
27884 if (NILP (b))
27885 begpos = 0;
27886 else
27887 begpos = XINT (b);
27889 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27890 if (NILP (e))
27891 endpos = SCHARS (string);
27892 else
27893 endpos = XINT (e);
27895 /* Calculate the glyph position GPOS of GLYPH in the
27896 displayed string, relative to the beginning of the
27897 highlighted part of the string.
27899 Note: GPOS is different from CHARPOS. CHARPOS is the
27900 position of GLYPH in the internal string object. A mode
27901 line string format has structures which are converted to
27902 a flattened string by the Emacs Lisp interpreter. The
27903 internal string is an element of those structures. The
27904 displayed string is the flattened string. */
27905 tmp_glyph = row_start_glyph;
27906 while (tmp_glyph < glyph
27907 && (!(EQ (tmp_glyph->object, glyph->object)
27908 && begpos <= tmp_glyph->charpos
27909 && tmp_glyph->charpos < endpos)))
27910 tmp_glyph++;
27911 gpos = glyph - tmp_glyph;
27913 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27914 the highlighted part of the displayed string to which
27915 GLYPH belongs. Note: GSEQ_LENGTH is different from
27916 SCHARS (STRING), because the latter returns the length of
27917 the internal string. */
27918 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27919 tmp_glyph > glyph
27920 && (!(EQ (tmp_glyph->object, glyph->object)
27921 && begpos <= tmp_glyph->charpos
27922 && tmp_glyph->charpos < endpos));
27923 tmp_glyph--)
27925 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27927 /* Calculate the total pixel width of all the glyphs between
27928 the beginning of the highlighted area and GLYPH. */
27929 total_pixel_width = 0;
27930 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27931 total_pixel_width += tmp_glyph->pixel_width;
27933 /* Pre calculation of re-rendering position. Note: X is in
27934 column units here, after the call to mode_line_string or
27935 marginal_area_string. */
27936 hpos = x - gpos;
27937 vpos = (area == ON_MODE_LINE
27938 ? (w->current_matrix)->nrows - 1
27939 : 0);
27941 /* If GLYPH's position is included in the region that is
27942 already drawn in mouse face, we have nothing to do. */
27943 if ( EQ (window, hlinfo->mouse_face_window)
27944 && (!row->reversed_p
27945 ? (hlinfo->mouse_face_beg_col <= hpos
27946 && hpos < hlinfo->mouse_face_end_col)
27947 /* In R2L rows we swap BEG and END, see below. */
27948 : (hlinfo->mouse_face_end_col <= hpos
27949 && hpos < hlinfo->mouse_face_beg_col))
27950 && hlinfo->mouse_face_beg_row == vpos )
27951 return;
27953 if (clear_mouse_face (hlinfo))
27954 cursor = No_Cursor;
27956 if (!row->reversed_p)
27958 hlinfo->mouse_face_beg_col = hpos;
27959 hlinfo->mouse_face_beg_x = original_x_pixel
27960 - (total_pixel_width + dx);
27961 hlinfo->mouse_face_end_col = hpos + gseq_length;
27962 hlinfo->mouse_face_end_x = 0;
27964 else
27966 /* In R2L rows, show_mouse_face expects BEG and END
27967 coordinates to be swapped. */
27968 hlinfo->mouse_face_end_col = hpos;
27969 hlinfo->mouse_face_end_x = original_x_pixel
27970 - (total_pixel_width + dx);
27971 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27972 hlinfo->mouse_face_beg_x = 0;
27975 hlinfo->mouse_face_beg_row = vpos;
27976 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27977 hlinfo->mouse_face_past_end = 0;
27978 hlinfo->mouse_face_window = window;
27980 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27981 charpos,
27982 0, 0, 0,
27983 &ignore,
27984 glyph->face_id,
27986 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27988 if (NILP (pointer))
27989 pointer = Qhand;
27991 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27992 clear_mouse_face (hlinfo);
27994 #ifdef HAVE_WINDOW_SYSTEM
27995 if (FRAME_WINDOW_P (f))
27996 define_frame_cursor1 (f, cursor, pointer);
27997 #endif
28001 /* EXPORT:
28002 Take proper action when the mouse has moved to position X, Y on
28003 frame F with regards to highlighting portions of display that have
28004 mouse-face properties. Also de-highlight portions of display where
28005 the mouse was before, set the mouse pointer shape as appropriate
28006 for the mouse coordinates, and activate help echo (tooltips).
28007 X and Y can be negative or out of range. */
28009 void
28010 note_mouse_highlight (struct frame *f, int x, int y)
28012 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28013 enum window_part part = ON_NOTHING;
28014 Lisp_Object window;
28015 struct window *w;
28016 Cursor cursor = No_Cursor;
28017 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28018 struct buffer *b;
28020 /* When a menu is active, don't highlight because this looks odd. */
28021 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28022 if (popup_activated ())
28023 return;
28024 #endif
28026 if (!f->glyphs_initialized_p
28027 || f->pointer_invisible)
28028 return;
28030 hlinfo->mouse_face_mouse_x = x;
28031 hlinfo->mouse_face_mouse_y = y;
28032 hlinfo->mouse_face_mouse_frame = f;
28034 if (hlinfo->mouse_face_defer)
28035 return;
28037 /* Which window is that in? */
28038 window = window_from_coordinates (f, x, y, &part, 1);
28040 /* If displaying active text in another window, clear that. */
28041 if (! EQ (window, hlinfo->mouse_face_window)
28042 /* Also clear if we move out of text area in same window. */
28043 || (!NILP (hlinfo->mouse_face_window)
28044 && !NILP (window)
28045 && part != ON_TEXT
28046 && part != ON_MODE_LINE
28047 && part != ON_HEADER_LINE))
28048 clear_mouse_face (hlinfo);
28050 /* Not on a window -> return. */
28051 if (!WINDOWP (window))
28052 return;
28054 /* Reset help_echo_string. It will get recomputed below. */
28055 help_echo_string = Qnil;
28057 /* Convert to window-relative pixel coordinates. */
28058 w = XWINDOW (window);
28059 frame_to_window_pixel_xy (w, &x, &y);
28061 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28062 /* Handle tool-bar window differently since it doesn't display a
28063 buffer. */
28064 if (EQ (window, f->tool_bar_window))
28066 note_tool_bar_highlight (f, x, y);
28067 return;
28069 #endif
28071 /* Mouse is on the mode, header line or margin? */
28072 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28073 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28075 note_mode_line_or_margin_highlight (window, x, y, part);
28076 return;
28079 #ifdef HAVE_WINDOW_SYSTEM
28080 if (part == ON_VERTICAL_BORDER)
28082 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28083 help_echo_string = build_string ("drag-mouse-1: resize");
28085 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28086 || part == ON_SCROLL_BAR)
28087 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28088 else
28089 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28090 #endif
28092 /* Are we in a window whose display is up to date?
28093 And verify the buffer's text has not changed. */
28094 b = XBUFFER (w->contents);
28095 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28097 int hpos, vpos, dx, dy, area = LAST_AREA;
28098 ptrdiff_t pos;
28099 struct glyph *glyph;
28100 Lisp_Object object;
28101 Lisp_Object mouse_face = Qnil, position;
28102 Lisp_Object *overlay_vec = NULL;
28103 ptrdiff_t i, noverlays;
28104 struct buffer *obuf;
28105 ptrdiff_t obegv, ozv;
28106 int same_region;
28108 /* Find the glyph under X/Y. */
28109 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28111 #ifdef HAVE_WINDOW_SYSTEM
28112 /* Look for :pointer property on image. */
28113 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28115 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28116 if (img != NULL && IMAGEP (img->spec))
28118 Lisp_Object image_map, hotspot;
28119 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28120 !NILP (image_map))
28121 && (hotspot = find_hot_spot (image_map,
28122 glyph->slice.img.x + dx,
28123 glyph->slice.img.y + dy),
28124 CONSP (hotspot))
28125 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28127 Lisp_Object plist;
28129 /* Could check XCAR (hotspot) to see if we enter/leave
28130 this hot-spot.
28131 If so, we could look for mouse-enter, mouse-leave
28132 properties in PLIST (and do something...). */
28133 hotspot = XCDR (hotspot);
28134 if (CONSP (hotspot)
28135 && (plist = XCAR (hotspot), CONSP (plist)))
28137 pointer = Fplist_get (plist, Qpointer);
28138 if (NILP (pointer))
28139 pointer = Qhand;
28140 help_echo_string = Fplist_get (plist, Qhelp_echo);
28141 if (!NILP (help_echo_string))
28143 help_echo_window = window;
28144 help_echo_object = glyph->object;
28145 help_echo_pos = glyph->charpos;
28149 if (NILP (pointer))
28150 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28153 #endif /* HAVE_WINDOW_SYSTEM */
28155 /* Clear mouse face if X/Y not over text. */
28156 if (glyph == NULL
28157 || area != TEXT_AREA
28158 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28159 /* Glyph's OBJECT is an integer for glyphs inserted by the
28160 display engine for its internal purposes, like truncation
28161 and continuation glyphs and blanks beyond the end of
28162 line's text on text terminals. If we are over such a
28163 glyph, we are not over any text. */
28164 || INTEGERP (glyph->object)
28165 /* R2L rows have a stretch glyph at their front, which
28166 stands for no text, whereas L2R rows have no glyphs at
28167 all beyond the end of text. Treat such stretch glyphs
28168 like we do with NULL glyphs in L2R rows. */
28169 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28170 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28171 && glyph->type == STRETCH_GLYPH
28172 && glyph->avoid_cursor_p))
28174 if (clear_mouse_face (hlinfo))
28175 cursor = No_Cursor;
28176 #ifdef HAVE_WINDOW_SYSTEM
28177 if (FRAME_WINDOW_P (f) && NILP (pointer))
28179 if (area != TEXT_AREA)
28180 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28181 else
28182 pointer = Vvoid_text_area_pointer;
28184 #endif
28185 goto set_cursor;
28188 pos = glyph->charpos;
28189 object = glyph->object;
28190 if (!STRINGP (object) && !BUFFERP (object))
28191 goto set_cursor;
28193 /* If we get an out-of-range value, return now; avoid an error. */
28194 if (BUFFERP (object) && pos > BUF_Z (b))
28195 goto set_cursor;
28197 /* Make the window's buffer temporarily current for
28198 overlays_at and compute_char_face. */
28199 obuf = current_buffer;
28200 current_buffer = b;
28201 obegv = BEGV;
28202 ozv = ZV;
28203 BEGV = BEG;
28204 ZV = Z;
28206 /* Is this char mouse-active or does it have help-echo? */
28207 position = make_number (pos);
28209 if (BUFFERP (object))
28211 /* Put all the overlays we want in a vector in overlay_vec. */
28212 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28213 /* Sort overlays into increasing priority order. */
28214 noverlays = sort_overlays (overlay_vec, noverlays, w);
28216 else
28217 noverlays = 0;
28219 if (NILP (Vmouse_highlight))
28221 clear_mouse_face (hlinfo);
28222 goto check_help_echo;
28225 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28227 if (same_region)
28228 cursor = No_Cursor;
28230 /* Check mouse-face highlighting. */
28231 if (! same_region
28232 /* If there exists an overlay with mouse-face overlapping
28233 the one we are currently highlighting, we have to
28234 check if we enter the overlapping overlay, and then
28235 highlight only that. */
28236 || (OVERLAYP (hlinfo->mouse_face_overlay)
28237 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28239 /* Find the highest priority overlay with a mouse-face. */
28240 Lisp_Object overlay = Qnil;
28241 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28243 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28244 if (!NILP (mouse_face))
28245 overlay = overlay_vec[i];
28248 /* If we're highlighting the same overlay as before, there's
28249 no need to do that again. */
28250 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28251 goto check_help_echo;
28252 hlinfo->mouse_face_overlay = overlay;
28254 /* Clear the display of the old active region, if any. */
28255 if (clear_mouse_face (hlinfo))
28256 cursor = No_Cursor;
28258 /* If no overlay applies, get a text property. */
28259 if (NILP (overlay))
28260 mouse_face = Fget_text_property (position, Qmouse_face, object);
28262 /* Next, compute the bounds of the mouse highlighting and
28263 display it. */
28264 if (!NILP (mouse_face) && STRINGP (object))
28266 /* The mouse-highlighting comes from a display string
28267 with a mouse-face. */
28268 Lisp_Object s, e;
28269 ptrdiff_t ignore;
28271 s = Fprevious_single_property_change
28272 (make_number (pos + 1), Qmouse_face, object, Qnil);
28273 e = Fnext_single_property_change
28274 (position, Qmouse_face, object, Qnil);
28275 if (NILP (s))
28276 s = make_number (0);
28277 if (NILP (e))
28278 e = make_number (SCHARS (object) - 1);
28279 mouse_face_from_string_pos (w, hlinfo, object,
28280 XINT (s), XINT (e));
28281 hlinfo->mouse_face_past_end = 0;
28282 hlinfo->mouse_face_window = window;
28283 hlinfo->mouse_face_face_id
28284 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28285 glyph->face_id, 1);
28286 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28287 cursor = No_Cursor;
28289 else
28291 /* The mouse-highlighting, if any, comes from an overlay
28292 or text property in the buffer. */
28293 Lisp_Object buffer IF_LINT (= Qnil);
28294 Lisp_Object disp_string IF_LINT (= Qnil);
28296 if (STRINGP (object))
28298 /* If we are on a display string with no mouse-face,
28299 check if the text under it has one. */
28300 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28301 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28302 pos = string_buffer_position (object, start);
28303 if (pos > 0)
28305 mouse_face = get_char_property_and_overlay
28306 (make_number (pos), Qmouse_face, w->contents, &overlay);
28307 buffer = w->contents;
28308 disp_string = object;
28311 else
28313 buffer = object;
28314 disp_string = Qnil;
28317 if (!NILP (mouse_face))
28319 Lisp_Object before, after;
28320 Lisp_Object before_string, after_string;
28321 /* To correctly find the limits of mouse highlight
28322 in a bidi-reordered buffer, we must not use the
28323 optimization of limiting the search in
28324 previous-single-property-change and
28325 next-single-property-change, because
28326 rows_from_pos_range needs the real start and end
28327 positions to DTRT in this case. That's because
28328 the first row visible in a window does not
28329 necessarily display the character whose position
28330 is the smallest. */
28331 Lisp_Object lim1 =
28332 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28333 ? Fmarker_position (w->start)
28334 : Qnil;
28335 Lisp_Object lim2 =
28336 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28337 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28338 : Qnil;
28340 if (NILP (overlay))
28342 /* Handle the text property case. */
28343 before = Fprevious_single_property_change
28344 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28345 after = Fnext_single_property_change
28346 (make_number (pos), Qmouse_face, buffer, lim2);
28347 before_string = after_string = Qnil;
28349 else
28351 /* Handle the overlay case. */
28352 before = Foverlay_start (overlay);
28353 after = Foverlay_end (overlay);
28354 before_string = Foverlay_get (overlay, Qbefore_string);
28355 after_string = Foverlay_get (overlay, Qafter_string);
28357 if (!STRINGP (before_string)) before_string = Qnil;
28358 if (!STRINGP (after_string)) after_string = Qnil;
28361 mouse_face_from_buffer_pos (window, hlinfo, pos,
28362 NILP (before)
28364 : XFASTINT (before),
28365 NILP (after)
28366 ? BUF_Z (XBUFFER (buffer))
28367 : XFASTINT (after),
28368 before_string, after_string,
28369 disp_string);
28370 cursor = No_Cursor;
28375 check_help_echo:
28377 /* Look for a `help-echo' property. */
28378 if (NILP (help_echo_string)) {
28379 Lisp_Object help, overlay;
28381 /* Check overlays first. */
28382 help = overlay = Qnil;
28383 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28385 overlay = overlay_vec[i];
28386 help = Foverlay_get (overlay, Qhelp_echo);
28389 if (!NILP (help))
28391 help_echo_string = help;
28392 help_echo_window = window;
28393 help_echo_object = overlay;
28394 help_echo_pos = pos;
28396 else
28398 Lisp_Object obj = glyph->object;
28399 ptrdiff_t charpos = glyph->charpos;
28401 /* Try text properties. */
28402 if (STRINGP (obj)
28403 && charpos >= 0
28404 && charpos < SCHARS (obj))
28406 help = Fget_text_property (make_number (charpos),
28407 Qhelp_echo, obj);
28408 if (NILP (help))
28410 /* If the string itself doesn't specify a help-echo,
28411 see if the buffer text ``under'' it does. */
28412 struct glyph_row *r
28413 = MATRIX_ROW (w->current_matrix, vpos);
28414 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28415 ptrdiff_t p = string_buffer_position (obj, start);
28416 if (p > 0)
28418 help = Fget_char_property (make_number (p),
28419 Qhelp_echo, w->contents);
28420 if (!NILP (help))
28422 charpos = p;
28423 obj = w->contents;
28428 else if (BUFFERP (obj)
28429 && charpos >= BEGV
28430 && charpos < ZV)
28431 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28432 obj);
28434 if (!NILP (help))
28436 help_echo_string = help;
28437 help_echo_window = window;
28438 help_echo_object = obj;
28439 help_echo_pos = charpos;
28444 #ifdef HAVE_WINDOW_SYSTEM
28445 /* Look for a `pointer' property. */
28446 if (FRAME_WINDOW_P (f) && NILP (pointer))
28448 /* Check overlays first. */
28449 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28450 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28452 if (NILP (pointer))
28454 Lisp_Object obj = glyph->object;
28455 ptrdiff_t charpos = glyph->charpos;
28457 /* Try text properties. */
28458 if (STRINGP (obj)
28459 && charpos >= 0
28460 && charpos < SCHARS (obj))
28462 pointer = Fget_text_property (make_number (charpos),
28463 Qpointer, obj);
28464 if (NILP (pointer))
28466 /* If the string itself doesn't specify a pointer,
28467 see if the buffer text ``under'' it does. */
28468 struct glyph_row *r
28469 = MATRIX_ROW (w->current_matrix, vpos);
28470 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28471 ptrdiff_t p = string_buffer_position (obj, start);
28472 if (p > 0)
28473 pointer = Fget_char_property (make_number (p),
28474 Qpointer, w->contents);
28477 else if (BUFFERP (obj)
28478 && charpos >= BEGV
28479 && charpos < ZV)
28480 pointer = Fget_text_property (make_number (charpos),
28481 Qpointer, obj);
28484 #endif /* HAVE_WINDOW_SYSTEM */
28486 BEGV = obegv;
28487 ZV = ozv;
28488 current_buffer = obuf;
28491 set_cursor:
28493 #ifdef HAVE_WINDOW_SYSTEM
28494 if (FRAME_WINDOW_P (f))
28495 define_frame_cursor1 (f, cursor, pointer);
28496 #else
28497 /* This is here to prevent a compiler error, about "label at end of
28498 compound statement". */
28499 return;
28500 #endif
28504 /* EXPORT for RIF:
28505 Clear any mouse-face on window W. This function is part of the
28506 redisplay interface, and is called from try_window_id and similar
28507 functions to ensure the mouse-highlight is off. */
28509 void
28510 x_clear_window_mouse_face (struct window *w)
28512 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28513 Lisp_Object window;
28515 block_input ();
28516 XSETWINDOW (window, w);
28517 if (EQ (window, hlinfo->mouse_face_window))
28518 clear_mouse_face (hlinfo);
28519 unblock_input ();
28523 /* EXPORT:
28524 Just discard the mouse face information for frame F, if any.
28525 This is used when the size of F is changed. */
28527 void
28528 cancel_mouse_face (struct frame *f)
28530 Lisp_Object window;
28531 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28533 window = hlinfo->mouse_face_window;
28534 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28535 reset_mouse_highlight (hlinfo);
28540 /***********************************************************************
28541 Exposure Events
28542 ***********************************************************************/
28544 #ifdef HAVE_WINDOW_SYSTEM
28546 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28547 which intersects rectangle R. R is in window-relative coordinates. */
28549 static void
28550 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28551 enum glyph_row_area area)
28553 struct glyph *first = row->glyphs[area];
28554 struct glyph *end = row->glyphs[area] + row->used[area];
28555 struct glyph *last;
28556 int first_x, start_x, x;
28558 if (area == TEXT_AREA && row->fill_line_p)
28559 /* If row extends face to end of line write the whole line. */
28560 draw_glyphs (w, 0, row, area,
28561 0, row->used[area],
28562 DRAW_NORMAL_TEXT, 0);
28563 else
28565 /* Set START_X to the window-relative start position for drawing glyphs of
28566 AREA. The first glyph of the text area can be partially visible.
28567 The first glyphs of other areas cannot. */
28568 start_x = window_box_left_offset (w, area);
28569 x = start_x;
28570 if (area == TEXT_AREA)
28571 x += row->x;
28573 /* Find the first glyph that must be redrawn. */
28574 while (first < end
28575 && x + first->pixel_width < r->x)
28577 x += first->pixel_width;
28578 ++first;
28581 /* Find the last one. */
28582 last = first;
28583 first_x = x;
28584 while (last < end
28585 && x < r->x + r->width)
28587 x += last->pixel_width;
28588 ++last;
28591 /* Repaint. */
28592 if (last > first)
28593 draw_glyphs (w, first_x - start_x, row, area,
28594 first - row->glyphs[area], last - row->glyphs[area],
28595 DRAW_NORMAL_TEXT, 0);
28600 /* Redraw the parts of the glyph row ROW on window W intersecting
28601 rectangle R. R is in window-relative coordinates. Value is
28602 non-zero if mouse-face was overwritten. */
28604 static int
28605 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28607 eassert (row->enabled_p);
28609 if (row->mode_line_p || w->pseudo_window_p)
28610 draw_glyphs (w, 0, row, TEXT_AREA,
28611 0, row->used[TEXT_AREA],
28612 DRAW_NORMAL_TEXT, 0);
28613 else
28615 if (row->used[LEFT_MARGIN_AREA])
28616 expose_area (w, row, r, LEFT_MARGIN_AREA);
28617 if (row->used[TEXT_AREA])
28618 expose_area (w, row, r, TEXT_AREA);
28619 if (row->used[RIGHT_MARGIN_AREA])
28620 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28621 draw_row_fringe_bitmaps (w, row);
28624 return row->mouse_face_p;
28628 /* Redraw those parts of glyphs rows during expose event handling that
28629 overlap other rows. Redrawing of an exposed line writes over parts
28630 of lines overlapping that exposed line; this function fixes that.
28632 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28633 row in W's current matrix that is exposed and overlaps other rows.
28634 LAST_OVERLAPPING_ROW is the last such row. */
28636 static void
28637 expose_overlaps (struct window *w,
28638 struct glyph_row *first_overlapping_row,
28639 struct glyph_row *last_overlapping_row,
28640 XRectangle *r)
28642 struct glyph_row *row;
28644 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28645 if (row->overlapping_p)
28647 eassert (row->enabled_p && !row->mode_line_p);
28649 row->clip = r;
28650 if (row->used[LEFT_MARGIN_AREA])
28651 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28653 if (row->used[TEXT_AREA])
28654 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28656 if (row->used[RIGHT_MARGIN_AREA])
28657 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28658 row->clip = NULL;
28663 /* Return non-zero if W's cursor intersects rectangle R. */
28665 static int
28666 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28668 XRectangle cr, result;
28669 struct glyph *cursor_glyph;
28670 struct glyph_row *row;
28672 if (w->phys_cursor.vpos >= 0
28673 && w->phys_cursor.vpos < w->current_matrix->nrows
28674 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28675 row->enabled_p)
28676 && row->cursor_in_fringe_p)
28678 /* Cursor is in the fringe. */
28679 cr.x = window_box_right_offset (w,
28680 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28681 ? RIGHT_MARGIN_AREA
28682 : TEXT_AREA));
28683 cr.y = row->y;
28684 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28685 cr.height = row->height;
28686 return x_intersect_rectangles (&cr, r, &result);
28689 cursor_glyph = get_phys_cursor_glyph (w);
28690 if (cursor_glyph)
28692 /* r is relative to W's box, but w->phys_cursor.x is relative
28693 to left edge of W's TEXT area. Adjust it. */
28694 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28695 cr.y = w->phys_cursor.y;
28696 cr.width = cursor_glyph->pixel_width;
28697 cr.height = w->phys_cursor_height;
28698 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28699 I assume the effect is the same -- and this is portable. */
28700 return x_intersect_rectangles (&cr, r, &result);
28702 /* If we don't understand the format, pretend we're not in the hot-spot. */
28703 return 0;
28707 /* EXPORT:
28708 Draw a vertical window border to the right of window W if W doesn't
28709 have vertical scroll bars. */
28711 void
28712 x_draw_vertical_border (struct window *w)
28714 struct frame *f = XFRAME (WINDOW_FRAME (w));
28716 /* We could do better, if we knew what type of scroll-bar the adjacent
28717 windows (on either side) have... But we don't :-(
28718 However, I think this works ok. ++KFS 2003-04-25 */
28720 /* Redraw borders between horizontally adjacent windows. Don't
28721 do it for frames with vertical scroll bars because either the
28722 right scroll bar of a window, or the left scroll bar of its
28723 neighbor will suffice as a border. */
28724 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28725 return;
28727 /* Note: It is necessary to redraw both the left and the right
28728 borders, for when only this single window W is being
28729 redisplayed. */
28730 if (!WINDOW_RIGHTMOST_P (w)
28731 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28733 int x0, x1, y0, y1;
28735 window_box_edges (w, &x0, &y0, &x1, &y1);
28736 y1 -= 1;
28738 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28739 x1 -= 1;
28741 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28743 if (!WINDOW_LEFTMOST_P (w)
28744 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28746 int x0, x1, y0, y1;
28748 window_box_edges (w, &x0, &y0, &x1, &y1);
28749 y1 -= 1;
28751 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28752 x0 -= 1;
28754 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28759 /* Redraw the part of window W intersection rectangle FR. Pixel
28760 coordinates in FR are frame-relative. Call this function with
28761 input blocked. Value is non-zero if the exposure overwrites
28762 mouse-face. */
28764 static int
28765 expose_window (struct window *w, XRectangle *fr)
28767 struct frame *f = XFRAME (w->frame);
28768 XRectangle wr, r;
28769 int mouse_face_overwritten_p = 0;
28771 /* If window is not yet fully initialized, do nothing. This can
28772 happen when toolkit scroll bars are used and a window is split.
28773 Reconfiguring the scroll bar will generate an expose for a newly
28774 created window. */
28775 if (w->current_matrix == NULL)
28776 return 0;
28778 /* When we're currently updating the window, display and current
28779 matrix usually don't agree. Arrange for a thorough display
28780 later. */
28781 if (w->must_be_updated_p)
28783 SET_FRAME_GARBAGED (f);
28784 return 0;
28787 /* Frame-relative pixel rectangle of W. */
28788 wr.x = WINDOW_LEFT_EDGE_X (w);
28789 wr.y = WINDOW_TOP_EDGE_Y (w);
28790 wr.width = WINDOW_TOTAL_WIDTH (w);
28791 wr.height = WINDOW_TOTAL_HEIGHT (w);
28793 if (x_intersect_rectangles (fr, &wr, &r))
28795 int yb = window_text_bottom_y (w);
28796 struct glyph_row *row;
28797 int cursor_cleared_p, phys_cursor_on_p;
28798 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28800 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28801 r.x, r.y, r.width, r.height));
28803 /* Convert to window coordinates. */
28804 r.x -= WINDOW_LEFT_EDGE_X (w);
28805 r.y -= WINDOW_TOP_EDGE_Y (w);
28807 /* Turn off the cursor. */
28808 if (!w->pseudo_window_p
28809 && phys_cursor_in_rect_p (w, &r))
28811 x_clear_cursor (w);
28812 cursor_cleared_p = 1;
28814 else
28815 cursor_cleared_p = 0;
28817 /* If the row containing the cursor extends face to end of line,
28818 then expose_area might overwrite the cursor outside the
28819 rectangle and thus notice_overwritten_cursor might clear
28820 w->phys_cursor_on_p. We remember the original value and
28821 check later if it is changed. */
28822 phys_cursor_on_p = w->phys_cursor_on_p;
28824 /* Update lines intersecting rectangle R. */
28825 first_overlapping_row = last_overlapping_row = NULL;
28826 for (row = w->current_matrix->rows;
28827 row->enabled_p;
28828 ++row)
28830 int y0 = row->y;
28831 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28833 if ((y0 >= r.y && y0 < r.y + r.height)
28834 || (y1 > r.y && y1 < r.y + r.height)
28835 || (r.y >= y0 && r.y < y1)
28836 || (r.y + r.height > y0 && r.y + r.height < y1))
28838 /* A header line may be overlapping, but there is no need
28839 to fix overlapping areas for them. KFS 2005-02-12 */
28840 if (row->overlapping_p && !row->mode_line_p)
28842 if (first_overlapping_row == NULL)
28843 first_overlapping_row = row;
28844 last_overlapping_row = row;
28847 row->clip = fr;
28848 if (expose_line (w, row, &r))
28849 mouse_face_overwritten_p = 1;
28850 row->clip = NULL;
28852 else if (row->overlapping_p)
28854 /* We must redraw a row overlapping the exposed area. */
28855 if (y0 < r.y
28856 ? y0 + row->phys_height > r.y
28857 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28859 if (first_overlapping_row == NULL)
28860 first_overlapping_row = row;
28861 last_overlapping_row = row;
28865 if (y1 >= yb)
28866 break;
28869 /* Display the mode line if there is one. */
28870 if (WINDOW_WANTS_MODELINE_P (w)
28871 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28872 row->enabled_p)
28873 && row->y < r.y + r.height)
28875 if (expose_line (w, row, &r))
28876 mouse_face_overwritten_p = 1;
28879 if (!w->pseudo_window_p)
28881 /* Fix the display of overlapping rows. */
28882 if (first_overlapping_row)
28883 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28884 fr);
28886 /* Draw border between windows. */
28887 x_draw_vertical_border (w);
28889 /* Turn the cursor on again. */
28890 if (cursor_cleared_p
28891 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28892 update_window_cursor (w, 1);
28896 return mouse_face_overwritten_p;
28901 /* Redraw (parts) of all windows in the window tree rooted at W that
28902 intersect R. R contains frame pixel coordinates. Value is
28903 non-zero if the exposure overwrites mouse-face. */
28905 static int
28906 expose_window_tree (struct window *w, XRectangle *r)
28908 struct frame *f = XFRAME (w->frame);
28909 int mouse_face_overwritten_p = 0;
28911 while (w && !FRAME_GARBAGED_P (f))
28913 if (WINDOWP (w->contents))
28914 mouse_face_overwritten_p
28915 |= expose_window_tree (XWINDOW (w->contents), r);
28916 else
28917 mouse_face_overwritten_p |= expose_window (w, r);
28919 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28922 return mouse_face_overwritten_p;
28926 /* EXPORT:
28927 Redisplay an exposed area of frame F. X and Y are the upper-left
28928 corner of the exposed rectangle. W and H are width and height of
28929 the exposed area. All are pixel values. W or H zero means redraw
28930 the entire frame. */
28932 void
28933 expose_frame (struct frame *f, int x, int y, int w, int h)
28935 XRectangle r;
28936 int mouse_face_overwritten_p = 0;
28938 TRACE ((stderr, "expose_frame "));
28940 /* No need to redraw if frame will be redrawn soon. */
28941 if (FRAME_GARBAGED_P (f))
28943 TRACE ((stderr, " garbaged\n"));
28944 return;
28947 /* If basic faces haven't been realized yet, there is no point in
28948 trying to redraw anything. This can happen when we get an expose
28949 event while Emacs is starting, e.g. by moving another window. */
28950 if (FRAME_FACE_CACHE (f) == NULL
28951 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28953 TRACE ((stderr, " no faces\n"));
28954 return;
28957 if (w == 0 || h == 0)
28959 r.x = r.y = 0;
28960 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28961 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28963 else
28965 r.x = x;
28966 r.y = y;
28967 r.width = w;
28968 r.height = h;
28971 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28972 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28974 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28975 if (WINDOWP (f->tool_bar_window))
28976 mouse_face_overwritten_p
28977 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28978 #endif
28980 #ifdef HAVE_X_WINDOWS
28981 #ifndef MSDOS
28982 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28983 if (WINDOWP (f->menu_bar_window))
28984 mouse_face_overwritten_p
28985 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28986 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28987 #endif
28988 #endif
28990 /* Some window managers support a focus-follows-mouse style with
28991 delayed raising of frames. Imagine a partially obscured frame,
28992 and moving the mouse into partially obscured mouse-face on that
28993 frame. The visible part of the mouse-face will be highlighted,
28994 then the WM raises the obscured frame. With at least one WM, KDE
28995 2.1, Emacs is not getting any event for the raising of the frame
28996 (even tried with SubstructureRedirectMask), only Expose events.
28997 These expose events will draw text normally, i.e. not
28998 highlighted. Which means we must redo the highlight here.
28999 Subsume it under ``we love X''. --gerd 2001-08-15 */
29000 /* Included in Windows version because Windows most likely does not
29001 do the right thing if any third party tool offers
29002 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29003 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29005 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29006 if (f == hlinfo->mouse_face_mouse_frame)
29008 int mouse_x = hlinfo->mouse_face_mouse_x;
29009 int mouse_y = hlinfo->mouse_face_mouse_y;
29010 clear_mouse_face (hlinfo);
29011 note_mouse_highlight (f, mouse_x, mouse_y);
29017 /* EXPORT:
29018 Determine the intersection of two rectangles R1 and R2. Return
29019 the intersection in *RESULT. Value is non-zero if RESULT is not
29020 empty. */
29023 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29025 XRectangle *left, *right;
29026 XRectangle *upper, *lower;
29027 int intersection_p = 0;
29029 /* Rearrange so that R1 is the left-most rectangle. */
29030 if (r1->x < r2->x)
29031 left = r1, right = r2;
29032 else
29033 left = r2, right = r1;
29035 /* X0 of the intersection is right.x0, if this is inside R1,
29036 otherwise there is no intersection. */
29037 if (right->x <= left->x + left->width)
29039 result->x = right->x;
29041 /* The right end of the intersection is the minimum of
29042 the right ends of left and right. */
29043 result->width = (min (left->x + left->width, right->x + right->width)
29044 - result->x);
29046 /* Same game for Y. */
29047 if (r1->y < r2->y)
29048 upper = r1, lower = r2;
29049 else
29050 upper = r2, lower = r1;
29052 /* The upper end of the intersection is lower.y0, if this is inside
29053 of upper. Otherwise, there is no intersection. */
29054 if (lower->y <= upper->y + upper->height)
29056 result->y = lower->y;
29058 /* The lower end of the intersection is the minimum of the lower
29059 ends of upper and lower. */
29060 result->height = (min (lower->y + lower->height,
29061 upper->y + upper->height)
29062 - result->y);
29063 intersection_p = 1;
29067 return intersection_p;
29070 #endif /* HAVE_WINDOW_SYSTEM */
29073 /***********************************************************************
29074 Initialization
29075 ***********************************************************************/
29077 void
29078 syms_of_xdisp (void)
29080 Vwith_echo_area_save_vector = Qnil;
29081 staticpro (&Vwith_echo_area_save_vector);
29083 Vmessage_stack = Qnil;
29084 staticpro (&Vmessage_stack);
29086 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29087 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29089 message_dolog_marker1 = Fmake_marker ();
29090 staticpro (&message_dolog_marker1);
29091 message_dolog_marker2 = Fmake_marker ();
29092 staticpro (&message_dolog_marker2);
29093 message_dolog_marker3 = Fmake_marker ();
29094 staticpro (&message_dolog_marker3);
29096 #ifdef GLYPH_DEBUG
29097 defsubr (&Sdump_frame_glyph_matrix);
29098 defsubr (&Sdump_glyph_matrix);
29099 defsubr (&Sdump_glyph_row);
29100 defsubr (&Sdump_tool_bar_row);
29101 defsubr (&Strace_redisplay);
29102 defsubr (&Strace_to_stderr);
29103 #endif
29104 #ifdef HAVE_WINDOW_SYSTEM
29105 defsubr (&Stool_bar_lines_needed);
29106 defsubr (&Slookup_image_map);
29107 #endif
29108 defsubr (&Sline_pixel_height);
29109 defsubr (&Sformat_mode_line);
29110 defsubr (&Sinvisible_p);
29111 defsubr (&Scurrent_bidi_paragraph_direction);
29112 defsubr (&Smove_point_visually);
29114 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29115 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29116 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29117 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29118 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29119 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29120 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29121 DEFSYM (Qeval, "eval");
29122 DEFSYM (QCdata, ":data");
29123 DEFSYM (Qdisplay, "display");
29124 DEFSYM (Qspace_width, "space-width");
29125 DEFSYM (Qraise, "raise");
29126 DEFSYM (Qslice, "slice");
29127 DEFSYM (Qspace, "space");
29128 DEFSYM (Qmargin, "margin");
29129 DEFSYM (Qpointer, "pointer");
29130 DEFSYM (Qleft_margin, "left-margin");
29131 DEFSYM (Qright_margin, "right-margin");
29132 DEFSYM (Qcenter, "center");
29133 DEFSYM (Qline_height, "line-height");
29134 DEFSYM (QCalign_to, ":align-to");
29135 DEFSYM (QCrelative_width, ":relative-width");
29136 DEFSYM (QCrelative_height, ":relative-height");
29137 DEFSYM (QCeval, ":eval");
29138 DEFSYM (QCpropertize, ":propertize");
29139 DEFSYM (QCfile, ":file");
29140 DEFSYM (Qfontified, "fontified");
29141 DEFSYM (Qfontification_functions, "fontification-functions");
29142 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29143 DEFSYM (Qescape_glyph, "escape-glyph");
29144 DEFSYM (Qnobreak_space, "nobreak-space");
29145 DEFSYM (Qimage, "image");
29146 DEFSYM (Qtext, "text");
29147 DEFSYM (Qboth, "both");
29148 DEFSYM (Qboth_horiz, "both-horiz");
29149 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29150 DEFSYM (QCmap, ":map");
29151 DEFSYM (QCpointer, ":pointer");
29152 DEFSYM (Qrect, "rect");
29153 DEFSYM (Qcircle, "circle");
29154 DEFSYM (Qpoly, "poly");
29155 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29156 DEFSYM (Qgrow_only, "grow-only");
29157 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29158 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29159 DEFSYM (Qposition, "position");
29160 DEFSYM (Qbuffer_position, "buffer-position");
29161 DEFSYM (Qobject, "object");
29162 DEFSYM (Qbar, "bar");
29163 DEFSYM (Qhbar, "hbar");
29164 DEFSYM (Qbox, "box");
29165 DEFSYM (Qhollow, "hollow");
29166 DEFSYM (Qhand, "hand");
29167 DEFSYM (Qarrow, "arrow");
29168 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29170 list_of_error = list1 (list2 (intern_c_string ("error"),
29171 intern_c_string ("void-variable")));
29172 staticpro (&list_of_error);
29174 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29175 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29176 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29177 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29179 echo_buffer[0] = echo_buffer[1] = Qnil;
29180 staticpro (&echo_buffer[0]);
29181 staticpro (&echo_buffer[1]);
29183 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29184 staticpro (&echo_area_buffer[0]);
29185 staticpro (&echo_area_buffer[1]);
29187 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29188 staticpro (&Vmessages_buffer_name);
29190 mode_line_proptrans_alist = Qnil;
29191 staticpro (&mode_line_proptrans_alist);
29192 mode_line_string_list = Qnil;
29193 staticpro (&mode_line_string_list);
29194 mode_line_string_face = Qnil;
29195 staticpro (&mode_line_string_face);
29196 mode_line_string_face_prop = Qnil;
29197 staticpro (&mode_line_string_face_prop);
29198 Vmode_line_unwind_vector = Qnil;
29199 staticpro (&Vmode_line_unwind_vector);
29201 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29203 help_echo_string = Qnil;
29204 staticpro (&help_echo_string);
29205 help_echo_object = Qnil;
29206 staticpro (&help_echo_object);
29207 help_echo_window = Qnil;
29208 staticpro (&help_echo_window);
29209 previous_help_echo_string = Qnil;
29210 staticpro (&previous_help_echo_string);
29211 help_echo_pos = -1;
29213 DEFSYM (Qright_to_left, "right-to-left");
29214 DEFSYM (Qleft_to_right, "left-to-right");
29216 #ifdef HAVE_WINDOW_SYSTEM
29217 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29218 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29219 For example, if a block cursor is over a tab, it will be drawn as
29220 wide as that tab on the display. */);
29221 x_stretch_cursor_p = 0;
29222 #endif
29224 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29225 doc: /* Non-nil means highlight trailing whitespace.
29226 The face used for trailing whitespace is `trailing-whitespace'. */);
29227 Vshow_trailing_whitespace = Qnil;
29229 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29230 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29231 If the value is t, Emacs highlights non-ASCII chars which have the
29232 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29233 or `escape-glyph' face respectively.
29235 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29236 U+2011 (non-breaking hyphen) are affected.
29238 Any other non-nil value means to display these characters as a escape
29239 glyph followed by an ordinary space or hyphen.
29241 A value of nil means no special handling of these characters. */);
29242 Vnobreak_char_display = Qt;
29244 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29245 doc: /* The pointer shape to show in void text areas.
29246 A value of nil means to show the text pointer. Other options are `arrow',
29247 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29248 Vvoid_text_area_pointer = Qarrow;
29250 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29251 doc: /* Non-nil means don't actually do any redisplay.
29252 This is used for internal purposes. */);
29253 Vinhibit_redisplay = Qnil;
29255 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29256 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29257 Vglobal_mode_string = Qnil;
29259 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29260 doc: /* Marker for where to display an arrow on top of the buffer text.
29261 This must be the beginning of a line in order to work.
29262 See also `overlay-arrow-string'. */);
29263 Voverlay_arrow_position = Qnil;
29265 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29266 doc: /* String to display as an arrow in non-window frames.
29267 See also `overlay-arrow-position'. */);
29268 Voverlay_arrow_string = build_pure_c_string ("=>");
29270 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29271 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29272 The symbols on this list are examined during redisplay to determine
29273 where to display overlay arrows. */);
29274 Voverlay_arrow_variable_list
29275 = list1 (intern_c_string ("overlay-arrow-position"));
29277 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29278 doc: /* The number of lines to try scrolling a window by when point moves out.
29279 If that fails to bring point back on frame, point is centered instead.
29280 If this is zero, point is always centered after it moves off frame.
29281 If you want scrolling to always be a line at a time, you should set
29282 `scroll-conservatively' to a large value rather than set this to 1. */);
29284 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29285 doc: /* Scroll up to this many lines, to bring point back on screen.
29286 If point moves off-screen, redisplay will scroll by up to
29287 `scroll-conservatively' lines in order to bring point just barely
29288 onto the screen again. If that cannot be done, then redisplay
29289 recenters point as usual.
29291 If the value is greater than 100, redisplay will never recenter point,
29292 but will always scroll just enough text to bring point into view, even
29293 if you move far away.
29295 A value of zero means always recenter point if it moves off screen. */);
29296 scroll_conservatively = 0;
29298 DEFVAR_INT ("scroll-margin", scroll_margin,
29299 doc: /* Number of lines of margin at the top and bottom of a window.
29300 Recenter the window whenever point gets within this many lines
29301 of the top or bottom of the window. */);
29302 scroll_margin = 0;
29304 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29305 doc: /* Pixels per inch value for non-window system displays.
29306 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29307 Vdisplay_pixels_per_inch = make_float (72.0);
29309 #ifdef GLYPH_DEBUG
29310 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29311 #endif
29313 DEFVAR_LISP ("truncate-partial-width-windows",
29314 Vtruncate_partial_width_windows,
29315 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29316 For an integer value, truncate lines in each window narrower than the
29317 full frame width, provided the window width is less than that integer;
29318 otherwise, respect the value of `truncate-lines'.
29320 For any other non-nil value, truncate lines in all windows that do
29321 not span the full frame width.
29323 A value of nil means to respect the value of `truncate-lines'.
29325 If `word-wrap' is enabled, you might want to reduce this. */);
29326 Vtruncate_partial_width_windows = make_number (50);
29328 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29329 doc: /* Maximum buffer size for which line number should be displayed.
29330 If the buffer is bigger than this, the line number does not appear
29331 in the mode line. A value of nil means no limit. */);
29332 Vline_number_display_limit = Qnil;
29334 DEFVAR_INT ("line-number-display-limit-width",
29335 line_number_display_limit_width,
29336 doc: /* Maximum line width (in characters) for line number display.
29337 If the average length of the lines near point is bigger than this, then the
29338 line number may be omitted from the mode line. */);
29339 line_number_display_limit_width = 200;
29341 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29342 doc: /* Non-nil means highlight region even in nonselected windows. */);
29343 highlight_nonselected_windows = 0;
29345 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29346 doc: /* Non-nil if more than one frame is visible on this display.
29347 Minibuffer-only frames don't count, but iconified frames do.
29348 This variable is not guaranteed to be accurate except while processing
29349 `frame-title-format' and `icon-title-format'. */);
29351 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29352 doc: /* Template for displaying the title bar of visible frames.
29353 \(Assuming the window manager supports this feature.)
29355 This variable has the same structure as `mode-line-format', except that
29356 the %c and %l constructs are ignored. It is used only on frames for
29357 which no explicit name has been set \(see `modify-frame-parameters'). */);
29359 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29360 doc: /* Template for displaying the title bar of an iconified frame.
29361 \(Assuming the window manager supports this feature.)
29362 This variable has the same structure as `mode-line-format' (which see),
29363 and is used only on frames for which no explicit name has been set
29364 \(see `modify-frame-parameters'). */);
29365 Vicon_title_format
29366 = Vframe_title_format
29367 = listn (CONSTYPE_PURE, 3,
29368 intern_c_string ("multiple-frames"),
29369 build_pure_c_string ("%b"),
29370 listn (CONSTYPE_PURE, 4,
29371 empty_unibyte_string,
29372 intern_c_string ("invocation-name"),
29373 build_pure_c_string ("@"),
29374 intern_c_string ("system-name")));
29376 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29377 doc: /* Maximum number of lines to keep in the message log buffer.
29378 If nil, disable message logging. If t, log messages but don't truncate
29379 the buffer when it becomes large. */);
29380 Vmessage_log_max = make_number (1000);
29382 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29383 doc: /* Functions called before redisplay, if window sizes have changed.
29384 The value should be a list of functions that take one argument.
29385 Just before redisplay, for each frame, if any of its windows have changed
29386 size since the last redisplay, or have been split or deleted,
29387 all the functions in the list are called, with the frame as argument. */);
29388 Vwindow_size_change_functions = Qnil;
29390 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29391 doc: /* List of functions to call before redisplaying a window with scrolling.
29392 Each function is called with two arguments, the window and its new
29393 display-start position. Note that these functions are also called by
29394 `set-window-buffer'. Also note that the value of `window-end' is not
29395 valid when these functions are called.
29397 Warning: Do not use this feature to alter the way the window
29398 is scrolled. It is not designed for that, and such use probably won't
29399 work. */);
29400 Vwindow_scroll_functions = Qnil;
29402 DEFVAR_LISP ("window-text-change-functions",
29403 Vwindow_text_change_functions,
29404 doc: /* Functions to call in redisplay when text in the window might change. */);
29405 Vwindow_text_change_functions = Qnil;
29407 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29408 doc: /* Functions called when redisplay of a window reaches the end trigger.
29409 Each function is called with two arguments, the window and the end trigger value.
29410 See `set-window-redisplay-end-trigger'. */);
29411 Vredisplay_end_trigger_functions = Qnil;
29413 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29414 doc: /* Non-nil means autoselect window with mouse pointer.
29415 If nil, do not autoselect windows.
29416 A positive number means delay autoselection by that many seconds: a
29417 window is autoselected only after the mouse has remained in that
29418 window for the duration of the delay.
29419 A negative number has a similar effect, but causes windows to be
29420 autoselected only after the mouse has stopped moving. \(Because of
29421 the way Emacs compares mouse events, you will occasionally wait twice
29422 that time before the window gets selected.\)
29423 Any other value means to autoselect window instantaneously when the
29424 mouse pointer enters it.
29426 Autoselection selects the minibuffer only if it is active, and never
29427 unselects the minibuffer if it is active.
29429 When customizing this variable make sure that the actual value of
29430 `focus-follows-mouse' matches the behavior of your window manager. */);
29431 Vmouse_autoselect_window = Qnil;
29433 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29434 doc: /* Non-nil means automatically resize tool-bars.
29435 This dynamically changes the tool-bar's height to the minimum height
29436 that is needed to make all tool-bar items visible.
29437 If value is `grow-only', the tool-bar's height is only increased
29438 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29439 Vauto_resize_tool_bars = Qt;
29441 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29442 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29443 auto_raise_tool_bar_buttons_p = 1;
29445 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29446 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29447 make_cursor_line_fully_visible_p = 1;
29449 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29450 doc: /* Border below tool-bar in pixels.
29451 If an integer, use it as the height of the border.
29452 If it is one of `internal-border-width' or `border-width', use the
29453 value of the corresponding frame parameter.
29454 Otherwise, no border is added below the tool-bar. */);
29455 Vtool_bar_border = Qinternal_border_width;
29457 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29458 doc: /* Margin around tool-bar buttons in pixels.
29459 If an integer, use that for both horizontal and vertical margins.
29460 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29461 HORZ specifying the horizontal margin, and VERT specifying the
29462 vertical margin. */);
29463 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29465 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29466 doc: /* Relief thickness of tool-bar buttons. */);
29467 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29469 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29470 doc: /* Tool bar style to use.
29471 It can be one of
29472 image - show images only
29473 text - show text only
29474 both - show both, text below image
29475 both-horiz - show text to the right of the image
29476 text-image-horiz - show text to the left of the image
29477 any other - use system default or image if no system default.
29479 This variable only affects the GTK+ toolkit version of Emacs. */);
29480 Vtool_bar_style = Qnil;
29482 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29483 doc: /* Maximum number of characters a label can have to be shown.
29484 The tool bar style must also show labels for this to have any effect, see
29485 `tool-bar-style'. */);
29486 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29488 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29489 doc: /* List of functions to call to fontify regions of text.
29490 Each function is called with one argument POS. Functions must
29491 fontify a region starting at POS in the current buffer, and give
29492 fontified regions the property `fontified'. */);
29493 Vfontification_functions = Qnil;
29494 Fmake_variable_buffer_local (Qfontification_functions);
29496 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29497 unibyte_display_via_language_environment,
29498 doc: /* Non-nil means display unibyte text according to language environment.
29499 Specifically, this means that raw bytes in the range 160-255 decimal
29500 are displayed by converting them to the equivalent multibyte characters
29501 according to the current language environment. As a result, they are
29502 displayed according to the current fontset.
29504 Note that this variable affects only how these bytes are displayed,
29505 but does not change the fact they are interpreted as raw bytes. */);
29506 unibyte_display_via_language_environment = 0;
29508 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29509 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29510 If a float, it specifies a fraction of the mini-window frame's height.
29511 If an integer, it specifies a number of lines. */);
29512 Vmax_mini_window_height = make_float (0.25);
29514 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29515 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29516 A value of nil means don't automatically resize mini-windows.
29517 A value of t means resize them to fit the text displayed in them.
29518 A value of `grow-only', the default, means let mini-windows grow only;
29519 they return to their normal size when the minibuffer is closed, or the
29520 echo area becomes empty. */);
29521 Vresize_mini_windows = Qgrow_only;
29523 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29524 doc: /* Alist specifying how to blink the cursor off.
29525 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29526 `cursor-type' frame-parameter or variable equals ON-STATE,
29527 comparing using `equal', Emacs uses OFF-STATE to specify
29528 how to blink it off. ON-STATE and OFF-STATE are values for
29529 the `cursor-type' frame parameter.
29531 If a frame's ON-STATE has no entry in this list,
29532 the frame's other specifications determine how to blink the cursor off. */);
29533 Vblink_cursor_alist = Qnil;
29535 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29536 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29537 If non-nil, windows are automatically scrolled horizontally to make
29538 point visible. */);
29539 automatic_hscrolling_p = 1;
29540 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29542 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29543 doc: /* How many columns away from the window edge point is allowed to get
29544 before automatic hscrolling will horizontally scroll the window. */);
29545 hscroll_margin = 5;
29547 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29548 doc: /* How many columns to scroll the window when point gets too close to the edge.
29549 When point is less than `hscroll-margin' columns from the window
29550 edge, automatic hscrolling will scroll the window by the amount of columns
29551 determined by this variable. If its value is a positive integer, scroll that
29552 many columns. If it's a positive floating-point number, it specifies the
29553 fraction of the window's width to scroll. If it's nil or zero, point will be
29554 centered horizontally after the scroll. Any other value, including negative
29555 numbers, are treated as if the value were zero.
29557 Automatic hscrolling always moves point outside the scroll margin, so if
29558 point was more than scroll step columns inside the margin, the window will
29559 scroll more than the value given by the scroll step.
29561 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29562 and `scroll-right' overrides this variable's effect. */);
29563 Vhscroll_step = make_number (0);
29565 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29566 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29567 Bind this around calls to `message' to let it take effect. */);
29568 message_truncate_lines = 0;
29570 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29571 doc: /* Normal hook run to update the menu bar definitions.
29572 Redisplay runs this hook before it redisplays the menu bar.
29573 This is used to update submenus such as Buffers,
29574 whose contents depend on various data. */);
29575 Vmenu_bar_update_hook = Qnil;
29577 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29578 doc: /* Frame for which we are updating a menu.
29579 The enable predicate for a menu binding should check this variable. */);
29580 Vmenu_updating_frame = Qnil;
29582 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29583 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29584 inhibit_menubar_update = 0;
29586 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29587 doc: /* Prefix prepended to all continuation lines at display time.
29588 The value may be a string, an image, or a stretch-glyph; it is
29589 interpreted in the same way as the value of a `display' text property.
29591 This variable is overridden by any `wrap-prefix' text or overlay
29592 property.
29594 To add a prefix to non-continuation lines, use `line-prefix'. */);
29595 Vwrap_prefix = Qnil;
29596 DEFSYM (Qwrap_prefix, "wrap-prefix");
29597 Fmake_variable_buffer_local (Qwrap_prefix);
29599 DEFVAR_LISP ("line-prefix", Vline_prefix,
29600 doc: /* Prefix prepended to all non-continuation lines at display time.
29601 The value may be a string, an image, or a stretch-glyph; it is
29602 interpreted in the same way as the value of a `display' text property.
29604 This variable is overridden by any `line-prefix' text or overlay
29605 property.
29607 To add a prefix to continuation lines, use `wrap-prefix'. */);
29608 Vline_prefix = Qnil;
29609 DEFSYM (Qline_prefix, "line-prefix");
29610 Fmake_variable_buffer_local (Qline_prefix);
29612 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29613 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29614 inhibit_eval_during_redisplay = 0;
29616 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29617 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29618 inhibit_free_realized_faces = 0;
29620 #ifdef GLYPH_DEBUG
29621 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29622 doc: /* Inhibit try_window_id display optimization. */);
29623 inhibit_try_window_id = 0;
29625 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29626 doc: /* Inhibit try_window_reusing display optimization. */);
29627 inhibit_try_window_reusing = 0;
29629 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29630 doc: /* Inhibit try_cursor_movement display optimization. */);
29631 inhibit_try_cursor_movement = 0;
29632 #endif /* GLYPH_DEBUG */
29634 DEFVAR_INT ("overline-margin", overline_margin,
29635 doc: /* Space between overline and text, in pixels.
29636 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29637 margin to the character height. */);
29638 overline_margin = 2;
29640 DEFVAR_INT ("underline-minimum-offset",
29641 underline_minimum_offset,
29642 doc: /* Minimum distance between baseline and underline.
29643 This can improve legibility of underlined text at small font sizes,
29644 particularly when using variable `x-use-underline-position-properties'
29645 with fonts that specify an UNDERLINE_POSITION relatively close to the
29646 baseline. The default value is 1. */);
29647 underline_minimum_offset = 1;
29649 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29650 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29651 This feature only works when on a window system that can change
29652 cursor shapes. */);
29653 display_hourglass_p = 1;
29655 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29656 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29657 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29659 #ifdef HAVE_WINDOW_SYSTEM
29660 hourglass_atimer = NULL;
29661 hourglass_shown_p = 0;
29662 #endif /* HAVE_WINDOW_SYSTEM */
29664 DEFSYM (Qglyphless_char, "glyphless-char");
29665 DEFSYM (Qhex_code, "hex-code");
29666 DEFSYM (Qempty_box, "empty-box");
29667 DEFSYM (Qthin_space, "thin-space");
29668 DEFSYM (Qzero_width, "zero-width");
29670 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29671 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29673 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29674 doc: /* Char-table defining glyphless characters.
29675 Each element, if non-nil, should be one of the following:
29676 an ASCII acronym string: display this string in a box
29677 `hex-code': display the hexadecimal code of a character in a box
29678 `empty-box': display as an empty box
29679 `thin-space': display as 1-pixel width space
29680 `zero-width': don't display
29681 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29682 display method for graphical terminals and text terminals respectively.
29683 GRAPHICAL and TEXT should each have one of the values listed above.
29685 The char-table has one extra slot to control the display of a character for
29686 which no font is found. This slot only takes effect on graphical terminals.
29687 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29688 `thin-space'. The default is `empty-box'. */);
29689 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29690 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29691 Qempty_box);
29693 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29694 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29695 Vdebug_on_message = Qnil;
29699 /* Initialize this module when Emacs starts. */
29701 void
29702 init_xdisp (void)
29704 CHARPOS (this_line_start_pos) = 0;
29706 if (!noninteractive)
29708 struct window *m = XWINDOW (minibuf_window);
29709 Lisp_Object frame = m->frame;
29710 struct frame *f = XFRAME (frame);
29711 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29712 struct window *r = XWINDOW (root);
29713 int i;
29715 echo_area_window = minibuf_window;
29717 r->top_line = FRAME_TOP_MARGIN (f);
29718 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29719 r->total_cols = FRAME_COLS (f);
29721 m->top_line = FRAME_LINES (f) - 1;
29722 m->total_lines = 1;
29723 m->total_cols = FRAME_COLS (f);
29725 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29726 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29727 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29729 /* The default ellipsis glyphs `...'. */
29730 for (i = 0; i < 3; ++i)
29731 default_invis_vector[i] = make_number ('.');
29735 /* Allocate the buffer for frame titles.
29736 Also used for `format-mode-line'. */
29737 int size = 100;
29738 mode_line_noprop_buf = xmalloc (size);
29739 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29740 mode_line_noprop_ptr = mode_line_noprop_buf;
29741 mode_line_target = MODE_LINE_DISPLAY;
29744 help_echo_showing_p = 0;
29747 #ifdef HAVE_WINDOW_SYSTEM
29749 /* Platform-independent portion of hourglass implementation. */
29751 /* Cancel a currently active hourglass timer, and start a new one. */
29752 void
29753 start_hourglass (void)
29755 struct timespec delay;
29757 cancel_hourglass ();
29759 if (INTEGERP (Vhourglass_delay)
29760 && XINT (Vhourglass_delay) > 0)
29761 delay = make_timespec (min (XINT (Vhourglass_delay),
29762 TYPE_MAXIMUM (time_t)),
29764 else if (FLOATP (Vhourglass_delay)
29765 && XFLOAT_DATA (Vhourglass_delay) > 0)
29766 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29767 else
29768 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29770 #ifdef HAVE_NTGUI
29772 extern void w32_note_current_window (void);
29773 w32_note_current_window ();
29775 #endif /* HAVE_NTGUI */
29777 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29778 show_hourglass, NULL);
29782 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29783 shown. */
29784 void
29785 cancel_hourglass (void)
29787 if (hourglass_atimer)
29789 cancel_atimer (hourglass_atimer);
29790 hourglass_atimer = NULL;
29793 if (hourglass_shown_p)
29794 hide_hourglass ();
29797 #endif /* HAVE_WINDOW_SYSTEM */