Add pre-redisplay-function and rectangular region
[emacs.git] / src / xdisp.c
blob5cd2c2badcc8aa941740029c63474358faeb843e
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22 Redisplay.
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
53 expose_window (asynchronous) |
55 X expose events -----+
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
84 . try_cursor_movement
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
90 . try_window_reusing_current_matrix
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
96 . try_window_id
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
102 . try_window
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
115 Desired matrices.
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
160 Frame matrices.
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
181 Bidirectional display.
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
224 Bidirectional display and character compositions
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
251 Moving an iterator in bidirectional text
252 without producing glyphs
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
277 #include "lisp.h"
278 #include "atimer.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301 #ifdef HAVE_WINDOW_SYSTEM
302 #include TERM_HEADER
303 #endif /* HAVE_WINDOW_SYSTEM */
305 #ifndef FRAME_X_OUTPUT
306 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
307 #endif
309 #define INFINITY 10000000
311 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
312 Lisp_Object Qwindow_scroll_functions;
313 static Lisp_Object Qwindow_text_change_functions;
314 static Lisp_Object Qredisplay_end_trigger_functions;
315 Lisp_Object Qinhibit_point_motion_hooks;
316 static Lisp_Object QCeval, QCpropertize;
317 Lisp_Object QCfile, QCdata;
318 static Lisp_Object Qfontified;
319 static Lisp_Object Qgrow_only;
320 static Lisp_Object Qinhibit_eval_during_redisplay;
321 static Lisp_Object Qbuffer_position, Qposition, Qobject;
322 static Lisp_Object Qright_to_left, Qleft_to_right;
324 /* Cursor shapes. */
325 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
327 /* Pointer shapes. */
328 static Lisp_Object Qarrow, Qhand;
329 Lisp_Object Qtext;
331 /* Holds the list (error). */
332 static Lisp_Object list_of_error;
334 static Lisp_Object Qfontification_functions;
336 static Lisp_Object Qwrap_prefix;
337 static Lisp_Object Qline_prefix;
338 static Lisp_Object Qredisplay_internal;
340 /* Non-nil means don't actually do any redisplay. */
342 Lisp_Object Qinhibit_redisplay;
344 /* Names of text properties relevant for redisplay. */
346 Lisp_Object Qdisplay;
348 Lisp_Object Qspace, QCalign_to;
349 static Lisp_Object QCrelative_width, QCrelative_height;
350 Lisp_Object Qleft_margin, Qright_margin;
351 static Lisp_Object Qspace_width, Qraise;
352 static Lisp_Object Qslice;
353 Lisp_Object Qcenter;
354 static Lisp_Object Qmargin, Qpointer;
355 static Lisp_Object Qline_height;
357 #ifdef HAVE_WINDOW_SYSTEM
359 /* Test if overflow newline into fringe. Called with iterator IT
360 at or past right window margin, and with IT->current_x set. */
362 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
363 (!NILP (Voverflow_newline_into_fringe) \
364 && FRAME_WINDOW_P ((IT)->f) \
365 && ((IT)->bidi_it.paragraph_dir == R2L \
366 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
367 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
368 && (IT)->current_x == (IT)->last_visible_x)
370 #else /* !HAVE_WINDOW_SYSTEM */
371 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
372 #endif /* HAVE_WINDOW_SYSTEM */
374 /* Test if the display element loaded in IT, or the underlying buffer
375 or string character, is a space or a TAB character. This is used
376 to determine where word wrapping can occur. */
378 #define IT_DISPLAYING_WHITESPACE(it) \
379 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
380 || ((STRINGP (it->string) \
381 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
382 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
383 || (it->s \
384 && (it->s[IT_BYTEPOS (*it)] == ' ' \
385 || it->s[IT_BYTEPOS (*it)] == '\t')) \
386 || (IT_BYTEPOS (*it) < ZV_BYTE \
387 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
388 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
390 /* Name of the face used to highlight trailing whitespace. */
392 static Lisp_Object Qtrailing_whitespace;
394 /* Name and number of the face used to highlight escape glyphs. */
396 static Lisp_Object Qescape_glyph;
398 /* Name and number of the face used to highlight non-breaking spaces. */
400 static Lisp_Object Qnobreak_space;
402 /* The symbol `image' which is the car of the lists used to represent
403 images in Lisp. Also a tool bar style. */
405 Lisp_Object Qimage;
407 /* The image map types. */
408 Lisp_Object QCmap;
409 static Lisp_Object QCpointer;
410 static Lisp_Object Qrect, Qcircle, Qpoly;
412 /* Tool bar styles */
413 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
415 /* Non-zero means print newline to stdout before next mini-buffer
416 message. */
418 bool noninteractive_need_newline;
420 /* Non-zero means print newline to message log before next message. */
422 static bool message_log_need_newline;
424 /* Three markers that message_dolog uses.
425 It could allocate them itself, but that causes trouble
426 in handling memory-full errors. */
427 static Lisp_Object message_dolog_marker1;
428 static Lisp_Object message_dolog_marker2;
429 static Lisp_Object message_dolog_marker3;
431 /* The buffer position of the first character appearing entirely or
432 partially on the line of the selected window which contains the
433 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
434 redisplay optimization in redisplay_internal. */
436 static struct text_pos this_line_start_pos;
438 /* Number of characters past the end of the line above, including the
439 terminating newline. */
441 static struct text_pos this_line_end_pos;
443 /* The vertical positions and the height of this line. */
445 static int this_line_vpos;
446 static int this_line_y;
447 static int this_line_pixel_height;
449 /* X position at which this display line starts. Usually zero;
450 negative if first character is partially visible. */
452 static int this_line_start_x;
454 /* The smallest character position seen by move_it_* functions as they
455 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
456 hscrolled lines, see display_line. */
458 static struct text_pos this_line_min_pos;
460 /* Buffer that this_line_.* variables are referring to. */
462 static struct buffer *this_line_buffer;
465 /* Values of those variables at last redisplay are stored as
466 properties on `overlay-arrow-position' symbol. However, if
467 Voverlay_arrow_position is a marker, last-arrow-position is its
468 numerical position. */
470 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
472 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
473 properties on a symbol in overlay-arrow-variable-list. */
475 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
477 Lisp_Object Qmenu_bar_update_hook;
479 /* Nonzero if an overlay arrow has been displayed in this window. */
481 static bool overlay_arrow_seen;
483 /* Vector containing glyphs for an ellipsis `...'. */
485 static Lisp_Object default_invis_vector[3];
487 /* This is the window where the echo area message was displayed. It
488 is always a mini-buffer window, but it may not be the same window
489 currently active as a mini-buffer. */
491 Lisp_Object echo_area_window;
493 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
494 pushes the current message and the value of
495 message_enable_multibyte on the stack, the function restore_message
496 pops the stack and displays MESSAGE again. */
498 static Lisp_Object Vmessage_stack;
500 /* Nonzero means multibyte characters were enabled when the echo area
501 message was specified. */
503 static bool message_enable_multibyte;
505 /* Nonzero if we should redraw the mode lines on the next redisplay. */
507 int update_mode_lines;
509 /* Nonzero if window sizes or contents have changed since last
510 redisplay that finished. */
512 int windows_or_buffers_changed;
514 /* Nonzero after display_mode_line if %l was used and it displayed a
515 line number. */
517 static bool line_number_displayed;
519 /* The name of the *Messages* buffer, a string. */
521 static Lisp_Object Vmessages_buffer_name;
523 /* Current, index 0, and last displayed echo area message. Either
524 buffers from echo_buffers, or nil to indicate no message. */
526 Lisp_Object echo_area_buffer[2];
528 /* The buffers referenced from echo_area_buffer. */
530 static Lisp_Object echo_buffer[2];
532 /* A vector saved used in with_area_buffer to reduce consing. */
534 static Lisp_Object Vwith_echo_area_save_vector;
536 /* Non-zero means display_echo_area should display the last echo area
537 message again. Set by redisplay_preserve_echo_area. */
539 static bool display_last_displayed_message_p;
541 /* Nonzero if echo area is being used by print; zero if being used by
542 message. */
544 static bool message_buf_print;
546 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
548 static Lisp_Object Qinhibit_menubar_update;
549 static Lisp_Object Qmessage_truncate_lines;
551 /* Set to 1 in clear_message to make redisplay_internal aware
552 of an emptied echo area. */
554 static bool message_cleared_p;
556 /* A scratch glyph row with contents used for generating truncation
557 glyphs. Also used in direct_output_for_insert. */
559 #define MAX_SCRATCH_GLYPHS 100
560 static struct glyph_row scratch_glyph_row;
561 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
563 /* Ascent and height of the last line processed by move_it_to. */
565 static int last_height;
567 /* Non-zero if there's a help-echo in the echo area. */
569 bool help_echo_showing_p;
571 /* The maximum distance to look ahead for text properties. Values
572 that are too small let us call compute_char_face and similar
573 functions too often which is expensive. Values that are too large
574 let us call compute_char_face and alike too often because we
575 might not be interested in text properties that far away. */
577 #define TEXT_PROP_DISTANCE_LIMIT 100
579 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
580 iterator state and later restore it. This is needed because the
581 bidi iterator on bidi.c keeps a stacked cache of its states, which
582 is really a singleton. When we use scratch iterator objects to
583 move around the buffer, we can cause the bidi cache to be pushed or
584 popped, and therefore we need to restore the cache state when we
585 return to the original iterator. */
586 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
587 do { \
588 if (CACHE) \
589 bidi_unshelve_cache (CACHE, 1); \
590 ITCOPY = ITORIG; \
591 CACHE = bidi_shelve_cache (); \
592 } while (0)
594 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
595 do { \
596 if (pITORIG != pITCOPY) \
597 *(pITORIG) = *(pITCOPY); \
598 bidi_unshelve_cache (CACHE, 0); \
599 CACHE = NULL; \
600 } while (0)
602 #ifdef GLYPH_DEBUG
604 /* Non-zero means print traces of redisplay if compiled with
605 GLYPH_DEBUG defined. */
607 int trace_redisplay_p;
609 #endif /* GLYPH_DEBUG */
611 #ifdef DEBUG_TRACE_MOVE
612 /* Non-zero means trace with TRACE_MOVE to stderr. */
613 int trace_move;
615 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
616 #else
617 #define TRACE_MOVE(x) (void) 0
618 #endif
620 static Lisp_Object Qauto_hscroll_mode;
622 /* Buffer being redisplayed -- for redisplay_window_error. */
624 static struct buffer *displayed_buffer;
626 /* Value returned from text property handlers (see below). */
628 enum prop_handled
630 HANDLED_NORMALLY,
631 HANDLED_RECOMPUTE_PROPS,
632 HANDLED_OVERLAY_STRING_CONSUMED,
633 HANDLED_RETURN
636 /* A description of text properties that redisplay is interested
637 in. */
639 struct props
641 /* The name of the property. */
642 Lisp_Object *name;
644 /* A unique index for the property. */
645 enum prop_idx idx;
647 /* A handler function called to set up iterator IT from the property
648 at IT's current position. Value is used to steer handle_stop. */
649 enum prop_handled (*handler) (struct it *it);
652 static enum prop_handled handle_face_prop (struct it *);
653 static enum prop_handled handle_invisible_prop (struct it *);
654 static enum prop_handled handle_display_prop (struct it *);
655 static enum prop_handled handle_composition_prop (struct it *);
656 static enum prop_handled handle_overlay_change (struct it *);
657 static enum prop_handled handle_fontified_prop (struct it *);
659 /* Properties handled by iterators. */
661 static struct props it_props[] =
663 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
664 /* Handle `face' before `display' because some sub-properties of
665 `display' need to know the face. */
666 {&Qface, FACE_PROP_IDX, handle_face_prop},
667 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
668 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
669 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
670 {NULL, 0, NULL}
673 /* Value is the position described by X. If X is a marker, value is
674 the marker_position of X. Otherwise, value is X. */
676 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
678 /* Enumeration returned by some move_it_.* functions internally. */
680 enum move_it_result
682 /* Not used. Undefined value. */
683 MOVE_UNDEFINED,
685 /* Move ended at the requested buffer position or ZV. */
686 MOVE_POS_MATCH_OR_ZV,
688 /* Move ended at the requested X pixel position. */
689 MOVE_X_REACHED,
691 /* Move within a line ended at the end of a line that must be
692 continued. */
693 MOVE_LINE_CONTINUED,
695 /* Move within a line ended at the end of a line that would
696 be displayed truncated. */
697 MOVE_LINE_TRUNCATED,
699 /* Move within a line ended at a line end. */
700 MOVE_NEWLINE_OR_CR
703 /* This counter is used to clear the face cache every once in a while
704 in redisplay_internal. It is incremented for each redisplay.
705 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
706 cleared. */
708 #define CLEAR_FACE_CACHE_COUNT 500
709 static int clear_face_cache_count;
711 /* Similarly for the image cache. */
713 #ifdef HAVE_WINDOW_SYSTEM
714 #define CLEAR_IMAGE_CACHE_COUNT 101
715 static int clear_image_cache_count;
717 /* Null glyph slice */
718 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
719 #endif
721 /* True while redisplay_internal is in progress. */
723 bool redisplaying_p;
725 static Lisp_Object Qinhibit_free_realized_faces;
726 static Lisp_Object Qmode_line_default_help_echo;
728 /* If a string, XTread_socket generates an event to display that string.
729 (The display is done in read_char.) */
731 Lisp_Object help_echo_string;
732 Lisp_Object help_echo_window;
733 Lisp_Object help_echo_object;
734 ptrdiff_t help_echo_pos;
736 /* Temporary variable for XTread_socket. */
738 Lisp_Object previous_help_echo_string;
740 /* Platform-independent portion of hourglass implementation. */
742 #ifdef HAVE_WINDOW_SYSTEM
744 /* Non-zero means an hourglass cursor is currently shown. */
745 bool hourglass_shown_p;
747 /* If non-null, an asynchronous timer that, when it expires, displays
748 an hourglass cursor on all frames. */
749 struct atimer *hourglass_atimer;
751 #endif /* HAVE_WINDOW_SYSTEM */
753 /* Name of the face used to display glyphless characters. */
754 static Lisp_Object Qglyphless_char;
756 /* Symbol for the purpose of Vglyphless_char_display. */
757 static Lisp_Object Qglyphless_char_display;
759 /* Method symbols for Vglyphless_char_display. */
760 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
762 /* Default number of seconds to wait before displaying an hourglass
763 cursor. */
764 #define DEFAULT_HOURGLASS_DELAY 1
766 #ifdef HAVE_WINDOW_SYSTEM
768 /* Default pixel width of `thin-space' display method. */
769 #define THIN_SPACE_WIDTH 1
771 #endif /* HAVE_WINDOW_SYSTEM */
773 /* Function prototypes. */
775 static void setup_for_ellipsis (struct it *, int);
776 static void set_iterator_to_next (struct it *, int);
777 static void mark_window_display_accurate_1 (struct window *, int);
778 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
779 static int display_prop_string_p (Lisp_Object, Lisp_Object);
780 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
781 static int cursor_row_p (struct glyph_row *);
782 static int redisplay_mode_lines (Lisp_Object, int);
783 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
785 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
787 static void handle_line_prefix (struct it *);
789 static void pint2str (char *, int, ptrdiff_t);
790 static void pint2hrstr (char *, int, ptrdiff_t);
791 static struct text_pos run_window_scroll_functions (Lisp_Object,
792 struct text_pos);
793 static int text_outside_line_unchanged_p (struct window *,
794 ptrdiff_t, ptrdiff_t);
795 static void store_mode_line_noprop_char (char);
796 static int store_mode_line_noprop (const char *, int, int);
797 static void handle_stop (struct it *);
798 static void handle_stop_backwards (struct it *, ptrdiff_t);
799 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
800 static void ensure_echo_area_buffers (void);
801 static void unwind_with_echo_area_buffer (Lisp_Object);
802 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
803 static int with_echo_area_buffer (struct window *, int,
804 int (*) (ptrdiff_t, Lisp_Object),
805 ptrdiff_t, Lisp_Object);
806 static void clear_garbaged_frames (void);
807 static int current_message_1 (ptrdiff_t, Lisp_Object);
808 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
809 static void set_message (Lisp_Object);
810 static int set_message_1 (ptrdiff_t, Lisp_Object);
811 static int display_echo_area (struct window *);
812 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
813 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
814 static void unwind_redisplay (void);
815 static int string_char_and_length (const unsigned char *, int *);
816 static struct text_pos display_prop_end (struct it *, Lisp_Object,
817 struct text_pos);
818 static int compute_window_start_on_continuation_line (struct window *);
819 static void insert_left_trunc_glyphs (struct it *);
820 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
821 Lisp_Object);
822 static void extend_face_to_end_of_line (struct it *);
823 static int append_space_for_newline (struct it *, int);
824 static int cursor_row_fully_visible_p (struct window *, int, int);
825 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
826 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
827 static int trailing_whitespace_p (ptrdiff_t);
828 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
829 static void push_it (struct it *, struct text_pos *);
830 static void iterate_out_of_display_property (struct it *);
831 static void pop_it (struct it *);
832 static void sync_frame_with_window_matrix_rows (struct window *);
833 static void redisplay_internal (void);
834 static int echo_area_display (int);
835 static void redisplay_windows (Lisp_Object);
836 static void redisplay_window (Lisp_Object, int);
837 static Lisp_Object redisplay_window_error (Lisp_Object);
838 static Lisp_Object redisplay_window_0 (Lisp_Object);
839 static Lisp_Object redisplay_window_1 (Lisp_Object);
840 static int set_cursor_from_row (struct window *, struct glyph_row *,
841 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
842 int, int);
843 static int update_menu_bar (struct frame *, int, int);
844 static int try_window_reusing_current_matrix (struct window *);
845 static int try_window_id (struct window *);
846 static int display_line (struct it *);
847 static int display_mode_lines (struct window *);
848 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
849 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
850 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
851 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
852 static void display_menu_bar (struct window *);
853 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
854 ptrdiff_t *);
855 static int display_string (const char *, Lisp_Object, Lisp_Object,
856 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
857 static void compute_line_metrics (struct it *);
858 static void run_redisplay_end_trigger_hook (struct it *);
859 static int get_overlay_strings (struct it *, ptrdiff_t);
860 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
861 static void next_overlay_string (struct it *);
862 static void reseat (struct it *, struct text_pos, int);
863 static void reseat_1 (struct it *, struct text_pos, int);
864 static void back_to_previous_visible_line_start (struct it *);
865 static void reseat_at_next_visible_line_start (struct it *, int);
866 static int next_element_from_ellipsis (struct it *);
867 static int next_element_from_display_vector (struct it *);
868 static int next_element_from_string (struct it *);
869 static int next_element_from_c_string (struct it *);
870 static int next_element_from_buffer (struct it *);
871 static int next_element_from_composition (struct it *);
872 static int next_element_from_image (struct it *);
873 static int next_element_from_stretch (struct it *);
874 static void load_overlay_strings (struct it *, ptrdiff_t);
875 static int init_from_display_pos (struct it *, struct window *,
876 struct display_pos *);
877 static void reseat_to_string (struct it *, const char *,
878 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
879 static int get_next_display_element (struct it *);
880 static enum move_it_result
881 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
882 enum move_operation_enum);
883 static void get_visually_first_element (struct it *);
884 static void init_to_row_start (struct it *, struct window *,
885 struct glyph_row *);
886 static int init_to_row_end (struct it *, struct window *,
887 struct glyph_row *);
888 static void back_to_previous_line_start (struct it *);
889 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
890 static struct text_pos string_pos_nchars_ahead (struct text_pos,
891 Lisp_Object, ptrdiff_t);
892 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
893 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
894 static ptrdiff_t number_of_chars (const char *, bool);
895 static void compute_stop_pos (struct it *);
896 static void compute_string_pos (struct text_pos *, struct text_pos,
897 Lisp_Object);
898 static int face_before_or_after_it_pos (struct it *, int);
899 static ptrdiff_t next_overlay_change (ptrdiff_t);
900 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
901 Lisp_Object, struct text_pos *, ptrdiff_t, int);
902 static int handle_single_display_spec (struct it *, Lisp_Object,
903 Lisp_Object, Lisp_Object,
904 struct text_pos *, ptrdiff_t, int, int);
905 static int underlying_face_id (struct it *);
906 static int in_ellipses_for_invisible_text_p (struct display_pos *,
907 struct window *);
909 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
910 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
912 #ifdef HAVE_WINDOW_SYSTEM
914 static void x_consider_frame_title (Lisp_Object);
915 static void update_tool_bar (struct frame *, int);
916 static int redisplay_tool_bar (struct frame *);
917 static void notice_overwritten_cursor (struct window *,
918 enum glyph_row_area,
919 int, int, int, int);
920 static void append_stretch_glyph (struct it *, Lisp_Object,
921 int, int, int);
924 #endif /* HAVE_WINDOW_SYSTEM */
926 static void produce_special_glyphs (struct it *, enum display_element_type);
927 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
928 static int coords_in_mouse_face_p (struct window *, int, int);
932 /***********************************************************************
933 Window display dimensions
934 ***********************************************************************/
936 /* Return the bottom boundary y-position for text lines in window W.
937 This is the first y position at which a line cannot start.
938 It is relative to the top of the window.
940 This is the height of W minus the height of a mode line, if any. */
943 window_text_bottom_y (struct window *w)
945 int height = WINDOW_TOTAL_HEIGHT (w);
947 if (WINDOW_WANTS_MODELINE_P (w))
948 height -= CURRENT_MODE_LINE_HEIGHT (w);
949 return height;
952 /* Return the pixel width of display area AREA of window W.
953 ANY_AREA means return the total width of W, not including
954 fringes to the left and right of the window. */
957 window_box_width (struct window *w, enum glyph_row_area area)
959 int cols = w->total_cols;
960 int pixels = 0;
962 if (!w->pseudo_window_p)
964 cols -= WINDOW_SCROLL_BAR_COLS (w);
966 if (area == TEXT_AREA)
968 cols -= max (0, w->left_margin_cols);
969 cols -= max (0, w->right_margin_cols);
970 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
972 else if (area == LEFT_MARGIN_AREA)
974 cols = max (0, w->left_margin_cols);
975 pixels = 0;
977 else if (area == RIGHT_MARGIN_AREA)
979 cols = max (0, w->right_margin_cols);
980 pixels = 0;
984 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
988 /* Return the pixel height of the display area of window W, not
989 including mode lines of W, if any. */
992 window_box_height (struct window *w)
994 struct frame *f = XFRAME (w->frame);
995 int height = WINDOW_TOTAL_HEIGHT (w);
997 eassert (height >= 0);
999 /* Note: the code below that determines the mode-line/header-line
1000 height is essentially the same as that contained in the macro
1001 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1002 the appropriate glyph row has its `mode_line_p' flag set,
1003 and if it doesn't, uses estimate_mode_line_height instead. */
1005 if (WINDOW_WANTS_MODELINE_P (w))
1007 struct glyph_row *ml_row
1008 = (w->current_matrix && w->current_matrix->rows
1009 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1010 : 0);
1011 if (ml_row && ml_row->mode_line_p)
1012 height -= ml_row->height;
1013 else
1014 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1017 if (WINDOW_WANTS_HEADER_LINE_P (w))
1019 struct glyph_row *hl_row
1020 = (w->current_matrix && w->current_matrix->rows
1021 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1022 : 0);
1023 if (hl_row && hl_row->mode_line_p)
1024 height -= hl_row->height;
1025 else
1026 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1029 /* With a very small font and a mode-line that's taller than
1030 default, we might end up with a negative height. */
1031 return max (0, height);
1034 /* Return the window-relative coordinate of the left edge of display
1035 area AREA of window W. ANY_AREA means return the left edge of the
1036 whole window, to the right of the left fringe of W. */
1039 window_box_left_offset (struct window *w, enum glyph_row_area area)
1041 int x;
1043 if (w->pseudo_window_p)
1044 return 0;
1046 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1048 if (area == TEXT_AREA)
1049 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1050 + window_box_width (w, LEFT_MARGIN_AREA));
1051 else if (area == RIGHT_MARGIN_AREA)
1052 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1053 + window_box_width (w, LEFT_MARGIN_AREA)
1054 + window_box_width (w, TEXT_AREA)
1055 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1057 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1058 else if (area == LEFT_MARGIN_AREA
1059 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1060 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1062 return x;
1066 /* Return the window-relative coordinate of the right edge of display
1067 area AREA of window W. ANY_AREA means return the right edge of the
1068 whole window, to the left of the right fringe of W. */
1071 window_box_right_offset (struct window *w, enum glyph_row_area area)
1073 return window_box_left_offset (w, area) + window_box_width (w, area);
1076 /* Return the frame-relative coordinate of the left edge of display
1077 area AREA of window W. ANY_AREA means return the left edge of the
1078 whole window, to the right of the left fringe of W. */
1081 window_box_left (struct window *w, enum glyph_row_area area)
1083 struct frame *f = XFRAME (w->frame);
1084 int x;
1086 if (w->pseudo_window_p)
1087 return FRAME_INTERNAL_BORDER_WIDTH (f);
1089 x = (WINDOW_LEFT_EDGE_X (w)
1090 + window_box_left_offset (w, area));
1092 return x;
1096 /* Return the frame-relative coordinate of the right edge of display
1097 area AREA of window W. ANY_AREA means return the right edge of the
1098 whole window, to the left of the right fringe of W. */
1101 window_box_right (struct window *w, enum glyph_row_area area)
1103 return window_box_left (w, area) + window_box_width (w, area);
1106 /* Get the bounding box of the display area AREA of window W, without
1107 mode lines, in frame-relative coordinates. ANY_AREA means the
1108 whole window, not including the left and right fringes of
1109 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1110 coordinates of the upper-left corner of the box. Return in
1111 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1113 void
1114 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1115 int *box_y, int *box_width, int *box_height)
1117 if (box_width)
1118 *box_width = window_box_width (w, area);
1119 if (box_height)
1120 *box_height = window_box_height (w);
1121 if (box_x)
1122 *box_x = window_box_left (w, area);
1123 if (box_y)
1125 *box_y = WINDOW_TOP_EDGE_Y (w);
1126 if (WINDOW_WANTS_HEADER_LINE_P (w))
1127 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1131 #ifdef HAVE_WINDOW_SYSTEM
1133 /* Get the bounding box of the display area AREA of window W, without
1134 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1135 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1136 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1137 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1138 box. */
1140 static void
1141 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1142 int *bottom_right_x, int *bottom_right_y)
1144 window_box (w, ANY_AREA, top_left_x, top_left_y,
1145 bottom_right_x, bottom_right_y);
1146 *bottom_right_x += *top_left_x;
1147 *bottom_right_y += *top_left_y;
1150 #endif /* HAVE_WINDOW_SYSTEM */
1152 /***********************************************************************
1153 Utilities
1154 ***********************************************************************/
1156 /* Return the bottom y-position of the line the iterator IT is in.
1157 This can modify IT's settings. */
1160 line_bottom_y (struct it *it)
1162 int line_height = it->max_ascent + it->max_descent;
1163 int line_top_y = it->current_y;
1165 if (line_height == 0)
1167 if (last_height)
1168 line_height = last_height;
1169 else if (IT_CHARPOS (*it) < ZV)
1171 move_it_by_lines (it, 1);
1172 line_height = (it->max_ascent || it->max_descent
1173 ? it->max_ascent + it->max_descent
1174 : last_height);
1176 else
1178 struct glyph_row *row = it->glyph_row;
1180 /* Use the default character height. */
1181 it->glyph_row = NULL;
1182 it->what = IT_CHARACTER;
1183 it->c = ' ';
1184 it->len = 1;
1185 PRODUCE_GLYPHS (it);
1186 line_height = it->ascent + it->descent;
1187 it->glyph_row = row;
1191 return line_top_y + line_height;
1194 DEFUN ("line-pixel-height", Fline_pixel_height,
1195 Sline_pixel_height, 0, 0, 0,
1196 doc: /* Return height in pixels of text line in the selected window.
1198 Value is the height in pixels of the line at point. */)
1199 (void)
1201 struct it it;
1202 struct text_pos pt;
1203 struct window *w = XWINDOW (selected_window);
1205 SET_TEXT_POS (pt, PT, PT_BYTE);
1206 start_display (&it, w, pt);
1207 it.vpos = it.current_y = 0;
1208 last_height = 0;
1209 return make_number (line_bottom_y (&it));
1212 /* Return the default pixel height of text lines in window W. The
1213 value is the canonical height of the W frame's default font, plus
1214 any extra space required by the line-spacing variable or frame
1215 parameter.
1217 Implementation note: this ignores any line-spacing text properties
1218 put on the newline characters. This is because those properties
1219 only affect the _screen_ line ending in the newline (i.e., in a
1220 continued line, only the last screen line will be affected), which
1221 means only a small number of lines in a buffer can ever use this
1222 feature. Since this function is used to compute the default pixel
1223 equivalent of text lines in a window, we can safely ignore those
1224 few lines. For the same reasons, we ignore the line-height
1225 properties. */
1227 default_line_pixel_height (struct window *w)
1229 struct frame *f = WINDOW_XFRAME (w);
1230 int height = FRAME_LINE_HEIGHT (f);
1232 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1234 struct buffer *b = XBUFFER (w->contents);
1235 Lisp_Object val = BVAR (b, extra_line_spacing);
1237 if (NILP (val))
1238 val = BVAR (&buffer_defaults, extra_line_spacing);
1239 if (!NILP (val))
1241 if (RANGED_INTEGERP (0, val, INT_MAX))
1242 height += XFASTINT (val);
1243 else if (FLOATP (val))
1245 int addon = XFLOAT_DATA (val) * height + 0.5;
1247 if (addon >= 0)
1248 height += addon;
1251 else
1252 height += f->extra_line_spacing;
1255 return height;
1258 /* Subroutine of pos_visible_p below. Extracts a display string, if
1259 any, from the display spec given as its argument. */
1260 static Lisp_Object
1261 string_from_display_spec (Lisp_Object spec)
1263 if (CONSP (spec))
1265 while (CONSP (spec))
1267 if (STRINGP (XCAR (spec)))
1268 return XCAR (spec);
1269 spec = XCDR (spec);
1272 else if (VECTORP (spec))
1274 ptrdiff_t i;
1276 for (i = 0; i < ASIZE (spec); i++)
1278 if (STRINGP (AREF (spec, i)))
1279 return AREF (spec, i);
1281 return Qnil;
1284 return spec;
1288 /* Limit insanely large values of W->hscroll on frame F to the largest
1289 value that will still prevent first_visible_x and last_visible_x of
1290 'struct it' from overflowing an int. */
1291 static int
1292 window_hscroll_limited (struct window *w, struct frame *f)
1294 ptrdiff_t window_hscroll = w->hscroll;
1295 int window_text_width = window_box_width (w, TEXT_AREA);
1296 int colwidth = FRAME_COLUMN_WIDTH (f);
1298 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1299 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1301 return window_hscroll;
1304 /* Return 1 if position CHARPOS is visible in window W.
1305 CHARPOS < 0 means return info about WINDOW_END position.
1306 If visible, set *X and *Y to pixel coordinates of top left corner.
1307 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1308 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1311 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1312 int *rtop, int *rbot, int *rowh, int *vpos)
1314 struct it it;
1315 void *itdata = bidi_shelve_cache ();
1316 struct text_pos top;
1317 int visible_p = 0;
1318 struct buffer *old_buffer = NULL;
1320 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1321 return visible_p;
1323 if (XBUFFER (w->contents) != current_buffer)
1325 old_buffer = current_buffer;
1326 set_buffer_internal_1 (XBUFFER (w->contents));
1329 SET_TEXT_POS_FROM_MARKER (top, w->start);
1330 /* Scrolling a minibuffer window via scroll bar when the echo area
1331 shows long text sometimes resets the minibuffer contents behind
1332 our backs. */
1333 if (CHARPOS (top) > ZV)
1334 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1336 /* Compute exact mode line heights. */
1337 if (WINDOW_WANTS_MODELINE_P (w))
1338 w->mode_line_height
1339 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1340 BVAR (current_buffer, mode_line_format));
1342 if (WINDOW_WANTS_HEADER_LINE_P (w))
1343 w->header_line_height
1344 = display_mode_line (w, HEADER_LINE_FACE_ID,
1345 BVAR (current_buffer, header_line_format));
1347 start_display (&it, w, top);
1348 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1349 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1351 if (charpos >= 0
1352 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1353 && IT_CHARPOS (it) >= charpos)
1354 /* When scanning backwards under bidi iteration, move_it_to
1355 stops at or _before_ CHARPOS, because it stops at or to
1356 the _right_ of the character at CHARPOS. */
1357 || (it.bidi_p && it.bidi_it.scan_dir == -1
1358 && IT_CHARPOS (it) <= charpos)))
1360 /* We have reached CHARPOS, or passed it. How the call to
1361 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1362 or covered by a display property, move_it_to stops at the end
1363 of the invisible text, to the right of CHARPOS. (ii) If
1364 CHARPOS is in a display vector, move_it_to stops on its last
1365 glyph. */
1366 int top_x = it.current_x;
1367 int top_y = it.current_y;
1368 /* Calling line_bottom_y may change it.method, it.position, etc. */
1369 enum it_method it_method = it.method;
1370 int bottom_y = (last_height = 0, line_bottom_y (&it));
1371 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1373 if (top_y < window_top_y)
1374 visible_p = bottom_y > window_top_y;
1375 else if (top_y < it.last_visible_y)
1376 visible_p = 1;
1377 if (bottom_y >= it.last_visible_y
1378 && it.bidi_p && it.bidi_it.scan_dir == -1
1379 && IT_CHARPOS (it) < charpos)
1381 /* When the last line of the window is scanned backwards
1382 under bidi iteration, we could be duped into thinking
1383 that we have passed CHARPOS, when in fact move_it_to
1384 simply stopped short of CHARPOS because it reached
1385 last_visible_y. To see if that's what happened, we call
1386 move_it_to again with a slightly larger vertical limit,
1387 and see if it actually moved vertically; if it did, we
1388 didn't really reach CHARPOS, which is beyond window end. */
1389 struct it save_it = it;
1390 /* Why 10? because we don't know how many canonical lines
1391 will the height of the next line(s) be. So we guess. */
1392 int ten_more_lines = 10 * default_line_pixel_height (w);
1394 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1395 MOVE_TO_POS | MOVE_TO_Y);
1396 if (it.current_y > top_y)
1397 visible_p = 0;
1399 it = save_it;
1401 if (visible_p)
1403 if (it_method == GET_FROM_DISPLAY_VECTOR)
1405 /* We stopped on the last glyph of a display vector.
1406 Try and recompute. Hack alert! */
1407 if (charpos < 2 || top.charpos >= charpos)
1408 top_x = it.glyph_row->x;
1409 else
1411 struct it it2, it2_prev;
1412 /* The idea is to get to the previous buffer
1413 position, consume the character there, and use
1414 the pixel coordinates we get after that. But if
1415 the previous buffer position is also displayed
1416 from a display vector, we need to consume all of
1417 the glyphs from that display vector. */
1418 start_display (&it2, w, top);
1419 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1420 /* If we didn't get to CHARPOS - 1, there's some
1421 replacing display property at that position, and
1422 we stopped after it. That is exactly the place
1423 whose coordinates we want. */
1424 if (IT_CHARPOS (it2) != charpos - 1)
1425 it2_prev = it2;
1426 else
1428 /* Iterate until we get out of the display
1429 vector that displays the character at
1430 CHARPOS - 1. */
1431 do {
1432 get_next_display_element (&it2);
1433 PRODUCE_GLYPHS (&it2);
1434 it2_prev = it2;
1435 set_iterator_to_next (&it2, 1);
1436 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1437 && IT_CHARPOS (it2) < charpos);
1439 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1440 || it2_prev.current_x > it2_prev.last_visible_x)
1441 top_x = it.glyph_row->x;
1442 else
1444 top_x = it2_prev.current_x;
1445 top_y = it2_prev.current_y;
1449 else if (IT_CHARPOS (it) != charpos)
1451 Lisp_Object cpos = make_number (charpos);
1452 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1453 Lisp_Object string = string_from_display_spec (spec);
1454 struct text_pos tpos;
1455 int replacing_spec_p;
1456 bool newline_in_string
1457 = (STRINGP (string)
1458 && memchr (SDATA (string), '\n', SBYTES (string)));
1460 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1461 replacing_spec_p
1462 = (!NILP (spec)
1463 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1464 charpos, FRAME_WINDOW_P (it.f)));
1465 /* The tricky code below is needed because there's a
1466 discrepancy between move_it_to and how we set cursor
1467 when PT is at the beginning of a portion of text
1468 covered by a display property or an overlay with a
1469 display property, or the display line ends in a
1470 newline from a display string. move_it_to will stop
1471 _after_ such display strings, whereas
1472 set_cursor_from_row conspires with cursor_row_p to
1473 place the cursor on the first glyph produced from the
1474 display string. */
1476 /* We have overshoot PT because it is covered by a
1477 display property that replaces the text it covers.
1478 If the string includes embedded newlines, we are also
1479 in the wrong display line. Backtrack to the correct
1480 line, where the display property begins. */
1481 if (replacing_spec_p)
1483 Lisp_Object startpos, endpos;
1484 EMACS_INT start, end;
1485 struct it it3;
1486 int it3_moved;
1488 /* Find the first and the last buffer positions
1489 covered by the display string. */
1490 endpos =
1491 Fnext_single_char_property_change (cpos, Qdisplay,
1492 Qnil, Qnil);
1493 startpos =
1494 Fprevious_single_char_property_change (endpos, Qdisplay,
1495 Qnil, Qnil);
1496 start = XFASTINT (startpos);
1497 end = XFASTINT (endpos);
1498 /* Move to the last buffer position before the
1499 display property. */
1500 start_display (&it3, w, top);
1501 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1502 /* Move forward one more line if the position before
1503 the display string is a newline or if it is the
1504 rightmost character on a line that is
1505 continued or word-wrapped. */
1506 if (it3.method == GET_FROM_BUFFER
1507 && (it3.c == '\n'
1508 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1509 move_it_by_lines (&it3, 1);
1510 else if (move_it_in_display_line_to (&it3, -1,
1511 it3.current_x
1512 + it3.pixel_width,
1513 MOVE_TO_X)
1514 == MOVE_LINE_CONTINUED)
1516 move_it_by_lines (&it3, 1);
1517 /* When we are under word-wrap, the #$@%!
1518 move_it_by_lines moves 2 lines, so we need to
1519 fix that up. */
1520 if (it3.line_wrap == WORD_WRAP)
1521 move_it_by_lines (&it3, -1);
1524 /* Record the vertical coordinate of the display
1525 line where we wound up. */
1526 top_y = it3.current_y;
1527 if (it3.bidi_p)
1529 /* When characters are reordered for display,
1530 the character displayed to the left of the
1531 display string could be _after_ the display
1532 property in the logical order. Use the
1533 smallest vertical position of these two. */
1534 start_display (&it3, w, top);
1535 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1536 if (it3.current_y < top_y)
1537 top_y = it3.current_y;
1539 /* Move from the top of the window to the beginning
1540 of the display line where the display string
1541 begins. */
1542 start_display (&it3, w, top);
1543 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1544 /* If it3_moved stays zero after the 'while' loop
1545 below, that means we already were at a newline
1546 before the loop (e.g., the display string begins
1547 with a newline), so we don't need to (and cannot)
1548 inspect the glyphs of it3.glyph_row, because
1549 PRODUCE_GLYPHS will not produce anything for a
1550 newline, and thus it3.glyph_row stays at its
1551 stale content it got at top of the window. */
1552 it3_moved = 0;
1553 /* Finally, advance the iterator until we hit the
1554 first display element whose character position is
1555 CHARPOS, or until the first newline from the
1556 display string, which signals the end of the
1557 display line. */
1558 while (get_next_display_element (&it3))
1560 PRODUCE_GLYPHS (&it3);
1561 if (IT_CHARPOS (it3) == charpos
1562 || ITERATOR_AT_END_OF_LINE_P (&it3))
1563 break;
1564 it3_moved = 1;
1565 set_iterator_to_next (&it3, 0);
1567 top_x = it3.current_x - it3.pixel_width;
1568 /* Normally, we would exit the above loop because we
1569 found the display element whose character
1570 position is CHARPOS. For the contingency that we
1571 didn't, and stopped at the first newline from the
1572 display string, move back over the glyphs
1573 produced from the string, until we find the
1574 rightmost glyph not from the string. */
1575 if (it3_moved
1576 && newline_in_string
1577 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1579 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1580 + it3.glyph_row->used[TEXT_AREA];
1582 while (EQ ((g - 1)->object, string))
1584 --g;
1585 top_x -= g->pixel_width;
1587 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1588 + it3.glyph_row->used[TEXT_AREA]);
1593 *x = top_x;
1594 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1595 *rtop = max (0, window_top_y - top_y);
1596 *rbot = max (0, bottom_y - it.last_visible_y);
1597 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1598 - max (top_y, window_top_y)));
1599 *vpos = it.vpos;
1602 else
1604 /* We were asked to provide info about WINDOW_END. */
1605 struct it it2;
1606 void *it2data = NULL;
1608 SAVE_IT (it2, it, it2data);
1609 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1610 move_it_by_lines (&it, 1);
1611 if (charpos < IT_CHARPOS (it)
1612 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1614 visible_p = 1;
1615 RESTORE_IT (&it2, &it2, it2data);
1616 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1617 *x = it2.current_x;
1618 *y = it2.current_y + it2.max_ascent - it2.ascent;
1619 *rtop = max (0, -it2.current_y);
1620 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1621 - it.last_visible_y));
1622 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1623 it.last_visible_y)
1624 - max (it2.current_y,
1625 WINDOW_HEADER_LINE_HEIGHT (w))));
1626 *vpos = it2.vpos;
1628 else
1629 bidi_unshelve_cache (it2data, 1);
1631 bidi_unshelve_cache (itdata, 0);
1633 if (old_buffer)
1634 set_buffer_internal_1 (old_buffer);
1636 if (visible_p && w->hscroll > 0)
1637 *x -=
1638 window_hscroll_limited (w, WINDOW_XFRAME (w))
1639 * WINDOW_FRAME_COLUMN_WIDTH (w);
1641 #if 0
1642 /* Debugging code. */
1643 if (visible_p)
1644 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1645 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1646 else
1647 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1648 #endif
1650 return visible_p;
1654 /* Return the next character from STR. Return in *LEN the length of
1655 the character. This is like STRING_CHAR_AND_LENGTH but never
1656 returns an invalid character. If we find one, we return a `?', but
1657 with the length of the invalid character. */
1659 static int
1660 string_char_and_length (const unsigned char *str, int *len)
1662 int c;
1664 c = STRING_CHAR_AND_LENGTH (str, *len);
1665 if (!CHAR_VALID_P (c))
1666 /* We may not change the length here because other places in Emacs
1667 don't use this function, i.e. they silently accept invalid
1668 characters. */
1669 c = '?';
1671 return c;
1676 /* Given a position POS containing a valid character and byte position
1677 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1679 static struct text_pos
1680 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1682 eassert (STRINGP (string) && nchars >= 0);
1684 if (STRING_MULTIBYTE (string))
1686 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1687 int len;
1689 while (nchars--)
1691 string_char_and_length (p, &len);
1692 p += len;
1693 CHARPOS (pos) += 1;
1694 BYTEPOS (pos) += len;
1697 else
1698 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1700 return pos;
1704 /* Value is the text position, i.e. character and byte position,
1705 for character position CHARPOS in STRING. */
1707 static struct text_pos
1708 string_pos (ptrdiff_t charpos, Lisp_Object string)
1710 struct text_pos pos;
1711 eassert (STRINGP (string));
1712 eassert (charpos >= 0);
1713 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1714 return pos;
1718 /* Value is a text position, i.e. character and byte position, for
1719 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1720 means recognize multibyte characters. */
1722 static struct text_pos
1723 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1725 struct text_pos pos;
1727 eassert (s != NULL);
1728 eassert (charpos >= 0);
1730 if (multibyte_p)
1732 int len;
1734 SET_TEXT_POS (pos, 0, 0);
1735 while (charpos--)
1737 string_char_and_length ((const unsigned char *) s, &len);
1738 s += len;
1739 CHARPOS (pos) += 1;
1740 BYTEPOS (pos) += len;
1743 else
1744 SET_TEXT_POS (pos, charpos, charpos);
1746 return pos;
1750 /* Value is the number of characters in C string S. MULTIBYTE_P
1751 non-zero means recognize multibyte characters. */
1753 static ptrdiff_t
1754 number_of_chars (const char *s, bool multibyte_p)
1756 ptrdiff_t nchars;
1758 if (multibyte_p)
1760 ptrdiff_t rest = strlen (s);
1761 int len;
1762 const unsigned char *p = (const unsigned char *) s;
1764 for (nchars = 0; rest > 0; ++nchars)
1766 string_char_and_length (p, &len);
1767 rest -= len, p += len;
1770 else
1771 nchars = strlen (s);
1773 return nchars;
1777 /* Compute byte position NEWPOS->bytepos corresponding to
1778 NEWPOS->charpos. POS is a known position in string STRING.
1779 NEWPOS->charpos must be >= POS.charpos. */
1781 static void
1782 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1784 eassert (STRINGP (string));
1785 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1787 if (STRING_MULTIBYTE (string))
1788 *newpos = string_pos_nchars_ahead (pos, string,
1789 CHARPOS (*newpos) - CHARPOS (pos));
1790 else
1791 BYTEPOS (*newpos) = CHARPOS (*newpos);
1794 /* EXPORT:
1795 Return an estimation of the pixel height of mode or header lines on
1796 frame F. FACE_ID specifies what line's height to estimate. */
1799 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1801 #ifdef HAVE_WINDOW_SYSTEM
1802 if (FRAME_WINDOW_P (f))
1804 int height = FONT_HEIGHT (FRAME_FONT (f));
1806 /* This function is called so early when Emacs starts that the face
1807 cache and mode line face are not yet initialized. */
1808 if (FRAME_FACE_CACHE (f))
1810 struct face *face = FACE_FROM_ID (f, face_id);
1811 if (face)
1813 if (face->font)
1814 height = FONT_HEIGHT (face->font);
1815 if (face->box_line_width > 0)
1816 height += 2 * face->box_line_width;
1820 return height;
1822 #endif
1824 return 1;
1827 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1828 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1829 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1830 not force the value into range. */
1832 void
1833 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1834 int *x, int *y, NativeRectangle *bounds, int noclip)
1837 #ifdef HAVE_WINDOW_SYSTEM
1838 if (FRAME_WINDOW_P (f))
1840 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1841 even for negative values. */
1842 if (pix_x < 0)
1843 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1844 if (pix_y < 0)
1845 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1847 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1848 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1850 if (bounds)
1851 STORE_NATIVE_RECT (*bounds,
1852 FRAME_COL_TO_PIXEL_X (f, pix_x),
1853 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1854 FRAME_COLUMN_WIDTH (f) - 1,
1855 FRAME_LINE_HEIGHT (f) - 1);
1857 if (!noclip)
1859 if (pix_x < 0)
1860 pix_x = 0;
1861 else if (pix_x > FRAME_TOTAL_COLS (f))
1862 pix_x = FRAME_TOTAL_COLS (f);
1864 if (pix_y < 0)
1865 pix_y = 0;
1866 else if (pix_y > FRAME_LINES (f))
1867 pix_y = FRAME_LINES (f);
1870 #endif
1872 *x = pix_x;
1873 *y = pix_y;
1877 /* Find the glyph under window-relative coordinates X/Y in window W.
1878 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1879 strings. Return in *HPOS and *VPOS the row and column number of
1880 the glyph found. Return in *AREA the glyph area containing X.
1881 Value is a pointer to the glyph found or null if X/Y is not on
1882 text, or we can't tell because W's current matrix is not up to
1883 date. */
1885 static struct glyph *
1886 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1887 int *dx, int *dy, int *area)
1889 struct glyph *glyph, *end;
1890 struct glyph_row *row = NULL;
1891 int x0, i;
1893 /* Find row containing Y. Give up if some row is not enabled. */
1894 for (i = 0; i < w->current_matrix->nrows; ++i)
1896 row = MATRIX_ROW (w->current_matrix, i);
1897 if (!row->enabled_p)
1898 return NULL;
1899 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1900 break;
1903 *vpos = i;
1904 *hpos = 0;
1906 /* Give up if Y is not in the window. */
1907 if (i == w->current_matrix->nrows)
1908 return NULL;
1910 /* Get the glyph area containing X. */
1911 if (w->pseudo_window_p)
1913 *area = TEXT_AREA;
1914 x0 = 0;
1916 else
1918 if (x < window_box_left_offset (w, TEXT_AREA))
1920 *area = LEFT_MARGIN_AREA;
1921 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1923 else if (x < window_box_right_offset (w, TEXT_AREA))
1925 *area = TEXT_AREA;
1926 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1928 else
1930 *area = RIGHT_MARGIN_AREA;
1931 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1935 /* Find glyph containing X. */
1936 glyph = row->glyphs[*area];
1937 end = glyph + row->used[*area];
1938 x -= x0;
1939 while (glyph < end && x >= glyph->pixel_width)
1941 x -= glyph->pixel_width;
1942 ++glyph;
1945 if (glyph == end)
1946 return NULL;
1948 if (dx)
1950 *dx = x;
1951 *dy = y - (row->y + row->ascent - glyph->ascent);
1954 *hpos = glyph - row->glyphs[*area];
1955 return glyph;
1958 /* Convert frame-relative x/y to coordinates relative to window W.
1959 Takes pseudo-windows into account. */
1961 static void
1962 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1964 if (w->pseudo_window_p)
1966 /* A pseudo-window is always full-width, and starts at the
1967 left edge of the frame, plus a frame border. */
1968 struct frame *f = XFRAME (w->frame);
1969 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1970 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1972 else
1974 *x -= WINDOW_LEFT_EDGE_X (w);
1975 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1979 #ifdef HAVE_WINDOW_SYSTEM
1981 /* EXPORT:
1982 Return in RECTS[] at most N clipping rectangles for glyph string S.
1983 Return the number of stored rectangles. */
1986 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1988 XRectangle r;
1990 if (n <= 0)
1991 return 0;
1993 if (s->row->full_width_p)
1995 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1996 r.x = WINDOW_LEFT_EDGE_X (s->w);
1997 r.width = WINDOW_TOTAL_WIDTH (s->w);
1999 /* Unless displaying a mode or menu bar line, which are always
2000 fully visible, clip to the visible part of the row. */
2001 if (s->w->pseudo_window_p)
2002 r.height = s->row->visible_height;
2003 else
2004 r.height = s->height;
2006 else
2008 /* This is a text line that may be partially visible. */
2009 r.x = window_box_left (s->w, s->area);
2010 r.width = window_box_width (s->w, s->area);
2011 r.height = s->row->visible_height;
2014 if (s->clip_head)
2015 if (r.x < s->clip_head->x)
2017 if (r.width >= s->clip_head->x - r.x)
2018 r.width -= s->clip_head->x - r.x;
2019 else
2020 r.width = 0;
2021 r.x = s->clip_head->x;
2023 if (s->clip_tail)
2024 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2026 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2027 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2028 else
2029 r.width = 0;
2032 /* If S draws overlapping rows, it's sufficient to use the top and
2033 bottom of the window for clipping because this glyph string
2034 intentionally draws over other lines. */
2035 if (s->for_overlaps)
2037 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2038 r.height = window_text_bottom_y (s->w) - r.y;
2040 /* Alas, the above simple strategy does not work for the
2041 environments with anti-aliased text: if the same text is
2042 drawn onto the same place multiple times, it gets thicker.
2043 If the overlap we are processing is for the erased cursor, we
2044 take the intersection with the rectangle of the cursor. */
2045 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2047 XRectangle rc, r_save = r;
2049 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2050 rc.y = s->w->phys_cursor.y;
2051 rc.width = s->w->phys_cursor_width;
2052 rc.height = s->w->phys_cursor_height;
2054 x_intersect_rectangles (&r_save, &rc, &r);
2057 else
2059 /* Don't use S->y for clipping because it doesn't take partially
2060 visible lines into account. For example, it can be negative for
2061 partially visible lines at the top of a window. */
2062 if (!s->row->full_width_p
2063 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2064 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2065 else
2066 r.y = max (0, s->row->y);
2069 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2071 /* If drawing the cursor, don't let glyph draw outside its
2072 advertised boundaries. Cleartype does this under some circumstances. */
2073 if (s->hl == DRAW_CURSOR)
2075 struct glyph *glyph = s->first_glyph;
2076 int height, max_y;
2078 if (s->x > r.x)
2080 r.width -= s->x - r.x;
2081 r.x = s->x;
2083 r.width = min (r.width, glyph->pixel_width);
2085 /* If r.y is below window bottom, ensure that we still see a cursor. */
2086 height = min (glyph->ascent + glyph->descent,
2087 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2088 max_y = window_text_bottom_y (s->w) - height;
2089 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2090 if (s->ybase - glyph->ascent > max_y)
2092 r.y = max_y;
2093 r.height = height;
2095 else
2097 /* Don't draw cursor glyph taller than our actual glyph. */
2098 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2099 if (height < r.height)
2101 max_y = r.y + r.height;
2102 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2103 r.height = min (max_y - r.y, height);
2108 if (s->row->clip)
2110 XRectangle r_save = r;
2112 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2113 r.width = 0;
2116 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2117 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2119 #ifdef CONVERT_FROM_XRECT
2120 CONVERT_FROM_XRECT (r, *rects);
2121 #else
2122 *rects = r;
2123 #endif
2124 return 1;
2126 else
2128 /* If we are processing overlapping and allowed to return
2129 multiple clipping rectangles, we exclude the row of the glyph
2130 string from the clipping rectangle. This is to avoid drawing
2131 the same text on the environment with anti-aliasing. */
2132 #ifdef CONVERT_FROM_XRECT
2133 XRectangle rs[2];
2134 #else
2135 XRectangle *rs = rects;
2136 #endif
2137 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2139 if (s->for_overlaps & OVERLAPS_PRED)
2141 rs[i] = r;
2142 if (r.y + r.height > row_y)
2144 if (r.y < row_y)
2145 rs[i].height = row_y - r.y;
2146 else
2147 rs[i].height = 0;
2149 i++;
2151 if (s->for_overlaps & OVERLAPS_SUCC)
2153 rs[i] = r;
2154 if (r.y < row_y + s->row->visible_height)
2156 if (r.y + r.height > row_y + s->row->visible_height)
2158 rs[i].y = row_y + s->row->visible_height;
2159 rs[i].height = r.y + r.height - rs[i].y;
2161 else
2162 rs[i].height = 0;
2164 i++;
2167 n = i;
2168 #ifdef CONVERT_FROM_XRECT
2169 for (i = 0; i < n; i++)
2170 CONVERT_FROM_XRECT (rs[i], rects[i]);
2171 #endif
2172 return n;
2176 /* EXPORT:
2177 Return in *NR the clipping rectangle for glyph string S. */
2179 void
2180 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2182 get_glyph_string_clip_rects (s, nr, 1);
2186 /* EXPORT:
2187 Return the position and height of the phys cursor in window W.
2188 Set w->phys_cursor_width to width of phys cursor.
2191 void
2192 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2193 struct glyph *glyph, int *xp, int *yp, int *heightp)
2195 struct frame *f = XFRAME (WINDOW_FRAME (w));
2196 int x, y, wd, h, h0, y0;
2198 /* Compute the width of the rectangle to draw. If on a stretch
2199 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2200 rectangle as wide as the glyph, but use a canonical character
2201 width instead. */
2202 wd = glyph->pixel_width - 1;
2203 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2204 wd++; /* Why? */
2205 #endif
2207 x = w->phys_cursor.x;
2208 if (x < 0)
2210 wd += x;
2211 x = 0;
2214 if (glyph->type == STRETCH_GLYPH
2215 && !x_stretch_cursor_p)
2216 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2217 w->phys_cursor_width = wd;
2219 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2221 /* If y is below window bottom, ensure that we still see a cursor. */
2222 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2224 h = max (h0, glyph->ascent + glyph->descent);
2225 h0 = min (h0, glyph->ascent + glyph->descent);
2227 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2228 if (y < y0)
2230 h = max (h - (y0 - y) + 1, h0);
2231 y = y0 - 1;
2233 else
2235 y0 = window_text_bottom_y (w) - h0;
2236 if (y > y0)
2238 h += y - y0;
2239 y = y0;
2243 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2244 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2245 *heightp = h;
2249 * Remember which glyph the mouse is over.
2252 void
2253 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2255 Lisp_Object window;
2256 struct window *w;
2257 struct glyph_row *r, *gr, *end_row;
2258 enum window_part part;
2259 enum glyph_row_area area;
2260 int x, y, width, height;
2262 /* Try to determine frame pixel position and size of the glyph under
2263 frame pixel coordinates X/Y on frame F. */
2265 if (!f->glyphs_initialized_p
2266 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2267 NILP (window)))
2269 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2270 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2271 goto virtual_glyph;
2274 w = XWINDOW (window);
2275 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2276 height = WINDOW_FRAME_LINE_HEIGHT (w);
2278 x = window_relative_x_coord (w, part, gx);
2279 y = gy - WINDOW_TOP_EDGE_Y (w);
2281 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2282 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2284 if (w->pseudo_window_p)
2286 area = TEXT_AREA;
2287 part = ON_MODE_LINE; /* Don't adjust margin. */
2288 goto text_glyph;
2291 switch (part)
2293 case ON_LEFT_MARGIN:
2294 area = LEFT_MARGIN_AREA;
2295 goto text_glyph;
2297 case ON_RIGHT_MARGIN:
2298 area = RIGHT_MARGIN_AREA;
2299 goto text_glyph;
2301 case ON_HEADER_LINE:
2302 case ON_MODE_LINE:
2303 gr = (part == ON_HEADER_LINE
2304 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2305 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2306 gy = gr->y;
2307 area = TEXT_AREA;
2308 goto text_glyph_row_found;
2310 case ON_TEXT:
2311 area = TEXT_AREA;
2313 text_glyph:
2314 gr = 0; gy = 0;
2315 for (; r <= end_row && r->enabled_p; ++r)
2316 if (r->y + r->height > y)
2318 gr = r; gy = r->y;
2319 break;
2322 text_glyph_row_found:
2323 if (gr && gy <= y)
2325 struct glyph *g = gr->glyphs[area];
2326 struct glyph *end = g + gr->used[area];
2328 height = gr->height;
2329 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2330 if (gx + g->pixel_width > x)
2331 break;
2333 if (g < end)
2335 if (g->type == IMAGE_GLYPH)
2337 /* Don't remember when mouse is over image, as
2338 image may have hot-spots. */
2339 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2340 return;
2342 width = g->pixel_width;
2344 else
2346 /* Use nominal char spacing at end of line. */
2347 x -= gx;
2348 gx += (x / width) * width;
2351 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2352 gx += window_box_left_offset (w, area);
2354 else
2356 /* Use nominal line height at end of window. */
2357 gx = (x / width) * width;
2358 y -= gy;
2359 gy += (y / height) * height;
2361 break;
2363 case ON_LEFT_FRINGE:
2364 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2365 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2366 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2367 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2368 goto row_glyph;
2370 case ON_RIGHT_FRINGE:
2371 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2372 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2373 : window_box_right_offset (w, TEXT_AREA));
2374 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2375 goto row_glyph;
2377 case ON_SCROLL_BAR:
2378 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2380 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2381 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2382 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2383 : 0)));
2384 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2386 row_glyph:
2387 gr = 0, gy = 0;
2388 for (; r <= end_row && r->enabled_p; ++r)
2389 if (r->y + r->height > y)
2391 gr = r; gy = r->y;
2392 break;
2395 if (gr && gy <= y)
2396 height = gr->height;
2397 else
2399 /* Use nominal line height at end of window. */
2400 y -= gy;
2401 gy += (y / height) * height;
2403 break;
2405 default:
2407 virtual_glyph:
2408 /* If there is no glyph under the mouse, then we divide the screen
2409 into a grid of the smallest glyph in the frame, and use that
2410 as our "glyph". */
2412 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2413 round down even for negative values. */
2414 if (gx < 0)
2415 gx -= width - 1;
2416 if (gy < 0)
2417 gy -= height - 1;
2419 gx = (gx / width) * width;
2420 gy = (gy / height) * height;
2422 goto store_rect;
2425 gx += WINDOW_LEFT_EDGE_X (w);
2426 gy += WINDOW_TOP_EDGE_Y (w);
2428 store_rect:
2429 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2431 /* Visible feedback for debugging. */
2432 #if 0
2433 #if HAVE_X_WINDOWS
2434 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2435 f->output_data.x->normal_gc,
2436 gx, gy, width, height);
2437 #endif
2438 #endif
2442 #endif /* HAVE_WINDOW_SYSTEM */
2444 static void
2445 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2447 eassert (w);
2448 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2449 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2450 w->window_end_vpos
2451 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2454 /***********************************************************************
2455 Lisp form evaluation
2456 ***********************************************************************/
2458 /* Error handler for safe_eval and safe_call. */
2460 static Lisp_Object
2461 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2463 add_to_log ("Error during redisplay: %S signaled %S",
2464 Flist (nargs, args), arg);
2465 return Qnil;
2468 /* Call function FUNC with the rest of NARGS - 1 arguments
2469 following. Return the result, or nil if something went
2470 wrong. Prevent redisplay during the evaluation. */
2472 Lisp_Object
2473 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2475 Lisp_Object val;
2477 if (inhibit_eval_during_redisplay)
2478 val = Qnil;
2479 else
2481 va_list ap;
2482 ptrdiff_t i;
2483 ptrdiff_t count = SPECPDL_INDEX ();
2484 struct gcpro gcpro1;
2485 Lisp_Object *args = alloca (nargs * word_size);
2487 args[0] = func;
2488 va_start (ap, func);
2489 for (i = 1; i < nargs; i++)
2490 args[i] = va_arg (ap, Lisp_Object);
2491 va_end (ap);
2493 GCPRO1 (args[0]);
2494 gcpro1.nvars = nargs;
2495 specbind (Qinhibit_redisplay, Qt);
2496 /* Use Qt to ensure debugger does not run,
2497 so there is no possibility of wanting to redisplay. */
2498 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2499 safe_eval_handler);
2500 UNGCPRO;
2501 val = unbind_to (count, val);
2504 return val;
2508 /* Call function FN with one argument ARG.
2509 Return the result, or nil if something went wrong. */
2511 Lisp_Object
2512 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2514 return safe_call (2, fn, arg);
2517 static Lisp_Object Qeval;
2519 Lisp_Object
2520 safe_eval (Lisp_Object sexpr)
2522 return safe_call1 (Qeval, sexpr);
2525 /* Call function FN with two arguments ARG1 and ARG2.
2526 Return the result, or nil if something went wrong. */
2528 Lisp_Object
2529 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2531 return safe_call (3, fn, arg1, arg2);
2536 /***********************************************************************
2537 Debugging
2538 ***********************************************************************/
2540 #if 0
2542 /* Define CHECK_IT to perform sanity checks on iterators.
2543 This is for debugging. It is too slow to do unconditionally. */
2545 static void
2546 check_it (struct it *it)
2548 if (it->method == GET_FROM_STRING)
2550 eassert (STRINGP (it->string));
2551 eassert (IT_STRING_CHARPOS (*it) >= 0);
2553 else
2555 eassert (IT_STRING_CHARPOS (*it) < 0);
2556 if (it->method == GET_FROM_BUFFER)
2558 /* Check that character and byte positions agree. */
2559 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2563 if (it->dpvec)
2564 eassert (it->current.dpvec_index >= 0);
2565 else
2566 eassert (it->current.dpvec_index < 0);
2569 #define CHECK_IT(IT) check_it ((IT))
2571 #else /* not 0 */
2573 #define CHECK_IT(IT) (void) 0
2575 #endif /* not 0 */
2578 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2580 /* Check that the window end of window W is what we expect it
2581 to be---the last row in the current matrix displaying text. */
2583 static void
2584 check_window_end (struct window *w)
2586 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2588 struct glyph_row *row;
2589 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2590 !row->enabled_p
2591 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2592 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2596 #define CHECK_WINDOW_END(W) check_window_end ((W))
2598 #else
2600 #define CHECK_WINDOW_END(W) (void) 0
2602 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2604 /***********************************************************************
2605 Iterator initialization
2606 ***********************************************************************/
2608 /* Initialize IT for displaying current_buffer in window W, starting
2609 at character position CHARPOS. CHARPOS < 0 means that no buffer
2610 position is specified which is useful when the iterator is assigned
2611 a position later. BYTEPOS is the byte position corresponding to
2612 CHARPOS.
2614 If ROW is not null, calls to produce_glyphs with IT as parameter
2615 will produce glyphs in that row.
2617 BASE_FACE_ID is the id of a base face to use. It must be one of
2618 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2619 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2620 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2622 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2623 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2624 will be initialized to use the corresponding mode line glyph row of
2625 the desired matrix of W. */
2627 void
2628 init_iterator (struct it *it, struct window *w,
2629 ptrdiff_t charpos, ptrdiff_t bytepos,
2630 struct glyph_row *row, enum face_id base_face_id)
2632 enum face_id remapped_base_face_id = base_face_id;
2634 /* Some precondition checks. */
2635 eassert (w != NULL && it != NULL);
2636 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2637 && charpos <= ZV));
2639 /* If face attributes have been changed since the last redisplay,
2640 free realized faces now because they depend on face definitions
2641 that might have changed. Don't free faces while there might be
2642 desired matrices pending which reference these faces. */
2643 if (face_change_count && !inhibit_free_realized_faces)
2645 face_change_count = 0;
2646 free_all_realized_faces (Qnil);
2649 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2650 if (! NILP (Vface_remapping_alist))
2651 remapped_base_face_id
2652 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2654 /* Use one of the mode line rows of W's desired matrix if
2655 appropriate. */
2656 if (row == NULL)
2658 if (base_face_id == MODE_LINE_FACE_ID
2659 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2660 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2661 else if (base_face_id == HEADER_LINE_FACE_ID)
2662 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2665 /* Clear IT. */
2666 memset (it, 0, sizeof *it);
2667 it->current.overlay_string_index = -1;
2668 it->current.dpvec_index = -1;
2669 it->base_face_id = remapped_base_face_id;
2670 it->string = Qnil;
2671 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2672 it->paragraph_embedding = L2R;
2673 it->bidi_it.string.lstring = Qnil;
2674 it->bidi_it.string.s = NULL;
2675 it->bidi_it.string.bufpos = 0;
2676 it->bidi_it.w = w;
2678 /* The window in which we iterate over current_buffer: */
2679 XSETWINDOW (it->window, w);
2680 it->w = w;
2681 it->f = XFRAME (w->frame);
2683 it->cmp_it.id = -1;
2685 /* Extra space between lines (on window systems only). */
2686 if (base_face_id == DEFAULT_FACE_ID
2687 && FRAME_WINDOW_P (it->f))
2689 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2690 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2691 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2692 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2693 * FRAME_LINE_HEIGHT (it->f));
2694 else if (it->f->extra_line_spacing > 0)
2695 it->extra_line_spacing = it->f->extra_line_spacing;
2696 it->max_extra_line_spacing = 0;
2699 /* If realized faces have been removed, e.g. because of face
2700 attribute changes of named faces, recompute them. When running
2701 in batch mode, the face cache of the initial frame is null. If
2702 we happen to get called, make a dummy face cache. */
2703 if (FRAME_FACE_CACHE (it->f) == NULL)
2704 init_frame_faces (it->f);
2705 if (FRAME_FACE_CACHE (it->f)->used == 0)
2706 recompute_basic_faces (it->f);
2708 /* Current value of the `slice', `space-width', and 'height' properties. */
2709 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2710 it->space_width = Qnil;
2711 it->font_height = Qnil;
2712 it->override_ascent = -1;
2714 /* Are control characters displayed as `^C'? */
2715 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2717 /* -1 means everything between a CR and the following line end
2718 is invisible. >0 means lines indented more than this value are
2719 invisible. */
2720 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2721 ? (clip_to_bounds
2722 (-1, XINT (BVAR (current_buffer, selective_display)),
2723 PTRDIFF_MAX))
2724 : (!NILP (BVAR (current_buffer, selective_display))
2725 ? -1 : 0));
2726 it->selective_display_ellipsis_p
2727 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2729 /* Display table to use. */
2730 it->dp = window_display_table (w);
2732 /* Are multibyte characters enabled in current_buffer? */
2733 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2735 /* Get the position at which the redisplay_end_trigger hook should
2736 be run, if it is to be run at all. */
2737 if (MARKERP (w->redisplay_end_trigger)
2738 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2739 it->redisplay_end_trigger_charpos
2740 = marker_position (w->redisplay_end_trigger);
2741 else if (INTEGERP (w->redisplay_end_trigger))
2742 it->redisplay_end_trigger_charpos =
2743 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2745 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2747 /* Are lines in the display truncated? */
2748 if (base_face_id != DEFAULT_FACE_ID
2749 || it->w->hscroll
2750 || (! WINDOW_FULL_WIDTH_P (it->w)
2751 && ((!NILP (Vtruncate_partial_width_windows)
2752 && !INTEGERP (Vtruncate_partial_width_windows))
2753 || (INTEGERP (Vtruncate_partial_width_windows)
2754 && (WINDOW_TOTAL_COLS (it->w)
2755 < XINT (Vtruncate_partial_width_windows))))))
2756 it->line_wrap = TRUNCATE;
2757 else if (NILP (BVAR (current_buffer, truncate_lines)))
2758 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2759 ? WINDOW_WRAP : WORD_WRAP;
2760 else
2761 it->line_wrap = TRUNCATE;
2763 /* Get dimensions of truncation and continuation glyphs. These are
2764 displayed as fringe bitmaps under X, but we need them for such
2765 frames when the fringes are turned off. But leave the dimensions
2766 zero for tooltip frames, as these glyphs look ugly there and also
2767 sabotage calculations of tooltip dimensions in x-show-tip. */
2768 #ifdef HAVE_WINDOW_SYSTEM
2769 if (!(FRAME_WINDOW_P (it->f)
2770 && FRAMEP (tip_frame)
2771 && it->f == XFRAME (tip_frame)))
2772 #endif
2774 if (it->line_wrap == TRUNCATE)
2776 /* We will need the truncation glyph. */
2777 eassert (it->glyph_row == NULL);
2778 produce_special_glyphs (it, IT_TRUNCATION);
2779 it->truncation_pixel_width = it->pixel_width;
2781 else
2783 /* We will need the continuation glyph. */
2784 eassert (it->glyph_row == NULL);
2785 produce_special_glyphs (it, IT_CONTINUATION);
2786 it->continuation_pixel_width = it->pixel_width;
2790 /* Reset these values to zero because the produce_special_glyphs
2791 above has changed them. */
2792 it->pixel_width = it->ascent = it->descent = 0;
2793 it->phys_ascent = it->phys_descent = 0;
2795 /* Set this after getting the dimensions of truncation and
2796 continuation glyphs, so that we don't produce glyphs when calling
2797 produce_special_glyphs, above. */
2798 it->glyph_row = row;
2799 it->area = TEXT_AREA;
2801 /* Forget any previous info about this row being reversed. */
2802 if (it->glyph_row)
2803 it->glyph_row->reversed_p = 0;
2805 /* Get the dimensions of the display area. The display area
2806 consists of the visible window area plus a horizontally scrolled
2807 part to the left of the window. All x-values are relative to the
2808 start of this total display area. */
2809 if (base_face_id != DEFAULT_FACE_ID)
2811 /* Mode lines, menu bar in terminal frames. */
2812 it->first_visible_x = 0;
2813 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2815 else
2817 it->first_visible_x =
2818 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2819 it->last_visible_x = (it->first_visible_x
2820 + window_box_width (w, TEXT_AREA));
2822 /* If we truncate lines, leave room for the truncation glyph(s) at
2823 the right margin. Otherwise, leave room for the continuation
2824 glyph(s). Done only if the window has no fringes. Since we
2825 don't know at this point whether there will be any R2L lines in
2826 the window, we reserve space for truncation/continuation glyphs
2827 even if only one of the fringes is absent. */
2828 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2829 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2831 if (it->line_wrap == TRUNCATE)
2832 it->last_visible_x -= it->truncation_pixel_width;
2833 else
2834 it->last_visible_x -= it->continuation_pixel_width;
2837 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2838 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2841 /* Leave room for a border glyph. */
2842 if (!FRAME_WINDOW_P (it->f)
2843 && !WINDOW_RIGHTMOST_P (it->w))
2844 it->last_visible_x -= 1;
2846 it->last_visible_y = window_text_bottom_y (w);
2848 /* For mode lines and alike, arrange for the first glyph having a
2849 left box line if the face specifies a box. */
2850 if (base_face_id != DEFAULT_FACE_ID)
2852 struct face *face;
2854 it->face_id = remapped_base_face_id;
2856 /* If we have a boxed mode line, make the first character appear
2857 with a left box line. */
2858 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2859 if (face->box != FACE_NO_BOX)
2860 it->start_of_box_run_p = 1;
2863 /* If a buffer position was specified, set the iterator there,
2864 getting overlays and face properties from that position. */
2865 if (charpos >= BUF_BEG (current_buffer))
2867 it->end_charpos = ZV;
2868 eassert (charpos == BYTE_TO_CHAR (bytepos));
2869 IT_CHARPOS (*it) = charpos;
2870 IT_BYTEPOS (*it) = bytepos;
2872 /* We will rely on `reseat' to set this up properly, via
2873 handle_face_prop. */
2874 it->face_id = it->base_face_id;
2876 it->start = it->current;
2877 /* Do we need to reorder bidirectional text? Not if this is a
2878 unibyte buffer: by definition, none of the single-byte
2879 characters are strong R2L, so no reordering is needed. And
2880 bidi.c doesn't support unibyte buffers anyway. Also, don't
2881 reorder while we are loading loadup.el, since the tables of
2882 character properties needed for reordering are not yet
2883 available. */
2884 it->bidi_p =
2885 NILP (Vpurify_flag)
2886 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2887 && it->multibyte_p;
2889 /* If we are to reorder bidirectional text, init the bidi
2890 iterator. */
2891 if (it->bidi_p)
2893 /* Note the paragraph direction that this buffer wants to
2894 use. */
2895 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2896 Qleft_to_right))
2897 it->paragraph_embedding = L2R;
2898 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2899 Qright_to_left))
2900 it->paragraph_embedding = R2L;
2901 else
2902 it->paragraph_embedding = NEUTRAL_DIR;
2903 bidi_unshelve_cache (NULL, 0);
2904 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2905 &it->bidi_it);
2908 /* Compute faces etc. */
2909 reseat (it, it->current.pos, 1);
2912 CHECK_IT (it);
2916 /* Initialize IT for the display of window W with window start POS. */
2918 void
2919 start_display (struct it *it, struct window *w, struct text_pos pos)
2921 struct glyph_row *row;
2922 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2924 row = w->desired_matrix->rows + first_vpos;
2925 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2926 it->first_vpos = first_vpos;
2928 /* Don't reseat to previous visible line start if current start
2929 position is in a string or image. */
2930 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2932 int start_at_line_beg_p;
2933 int first_y = it->current_y;
2935 /* If window start is not at a line start, skip forward to POS to
2936 get the correct continuation lines width. */
2937 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2938 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2939 if (!start_at_line_beg_p)
2941 int new_x;
2943 reseat_at_previous_visible_line_start (it);
2944 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2946 new_x = it->current_x + it->pixel_width;
2948 /* If lines are continued, this line may end in the middle
2949 of a multi-glyph character (e.g. a control character
2950 displayed as \003, or in the middle of an overlay
2951 string). In this case move_it_to above will not have
2952 taken us to the start of the continuation line but to the
2953 end of the continued line. */
2954 if (it->current_x > 0
2955 && it->line_wrap != TRUNCATE /* Lines are continued. */
2956 && (/* And glyph doesn't fit on the line. */
2957 new_x > it->last_visible_x
2958 /* Or it fits exactly and we're on a window
2959 system frame. */
2960 || (new_x == it->last_visible_x
2961 && FRAME_WINDOW_P (it->f)
2962 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2963 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2964 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2966 if ((it->current.dpvec_index >= 0
2967 || it->current.overlay_string_index >= 0)
2968 /* If we are on a newline from a display vector or
2969 overlay string, then we are already at the end of
2970 a screen line; no need to go to the next line in
2971 that case, as this line is not really continued.
2972 (If we do go to the next line, C-e will not DTRT.) */
2973 && it->c != '\n')
2975 set_iterator_to_next (it, 1);
2976 move_it_in_display_line_to (it, -1, -1, 0);
2979 it->continuation_lines_width += it->current_x;
2981 /* If the character at POS is displayed via a display
2982 vector, move_it_to above stops at the final glyph of
2983 IT->dpvec. To make the caller redisplay that character
2984 again (a.k.a. start at POS), we need to reset the
2985 dpvec_index to the beginning of IT->dpvec. */
2986 else if (it->current.dpvec_index >= 0)
2987 it->current.dpvec_index = 0;
2989 /* We're starting a new display line, not affected by the
2990 height of the continued line, so clear the appropriate
2991 fields in the iterator structure. */
2992 it->max_ascent = it->max_descent = 0;
2993 it->max_phys_ascent = it->max_phys_descent = 0;
2995 it->current_y = first_y;
2996 it->vpos = 0;
2997 it->current_x = it->hpos = 0;
3003 /* Return 1 if POS is a position in ellipses displayed for invisible
3004 text. W is the window we display, for text property lookup. */
3006 static int
3007 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3009 Lisp_Object prop, window;
3010 int ellipses_p = 0;
3011 ptrdiff_t charpos = CHARPOS (pos->pos);
3013 /* If POS specifies a position in a display vector, this might
3014 be for an ellipsis displayed for invisible text. We won't
3015 get the iterator set up for delivering that ellipsis unless
3016 we make sure that it gets aware of the invisible text. */
3017 if (pos->dpvec_index >= 0
3018 && pos->overlay_string_index < 0
3019 && CHARPOS (pos->string_pos) < 0
3020 && charpos > BEGV
3021 && (XSETWINDOW (window, w),
3022 prop = Fget_char_property (make_number (charpos),
3023 Qinvisible, window),
3024 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3026 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3027 window);
3028 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3031 return ellipses_p;
3035 /* Initialize IT for stepping through current_buffer in window W,
3036 starting at position POS that includes overlay string and display
3037 vector/ control character translation position information. Value
3038 is zero if there are overlay strings with newlines at POS. */
3040 static int
3041 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3043 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3044 int i, overlay_strings_with_newlines = 0;
3046 /* If POS specifies a position in a display vector, this might
3047 be for an ellipsis displayed for invisible text. We won't
3048 get the iterator set up for delivering that ellipsis unless
3049 we make sure that it gets aware of the invisible text. */
3050 if (in_ellipses_for_invisible_text_p (pos, w))
3052 --charpos;
3053 bytepos = 0;
3056 /* Keep in mind: the call to reseat in init_iterator skips invisible
3057 text, so we might end up at a position different from POS. This
3058 is only a problem when POS is a row start after a newline and an
3059 overlay starts there with an after-string, and the overlay has an
3060 invisible property. Since we don't skip invisible text in
3061 display_line and elsewhere immediately after consuming the
3062 newline before the row start, such a POS will not be in a string,
3063 but the call to init_iterator below will move us to the
3064 after-string. */
3065 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3067 /* This only scans the current chunk -- it should scan all chunks.
3068 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3069 to 16 in 22.1 to make this a lesser problem. */
3070 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3072 const char *s = SSDATA (it->overlay_strings[i]);
3073 const char *e = s + SBYTES (it->overlay_strings[i]);
3075 while (s < e && *s != '\n')
3076 ++s;
3078 if (s < e)
3080 overlay_strings_with_newlines = 1;
3081 break;
3085 /* If position is within an overlay string, set up IT to the right
3086 overlay string. */
3087 if (pos->overlay_string_index >= 0)
3089 int relative_index;
3091 /* If the first overlay string happens to have a `display'
3092 property for an image, the iterator will be set up for that
3093 image, and we have to undo that setup first before we can
3094 correct the overlay string index. */
3095 if (it->method == GET_FROM_IMAGE)
3096 pop_it (it);
3098 /* We already have the first chunk of overlay strings in
3099 IT->overlay_strings. Load more until the one for
3100 pos->overlay_string_index is in IT->overlay_strings. */
3101 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3103 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3104 it->current.overlay_string_index = 0;
3105 while (n--)
3107 load_overlay_strings (it, 0);
3108 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3112 it->current.overlay_string_index = pos->overlay_string_index;
3113 relative_index = (it->current.overlay_string_index
3114 % OVERLAY_STRING_CHUNK_SIZE);
3115 it->string = it->overlay_strings[relative_index];
3116 eassert (STRINGP (it->string));
3117 it->current.string_pos = pos->string_pos;
3118 it->method = GET_FROM_STRING;
3119 it->end_charpos = SCHARS (it->string);
3120 /* Set up the bidi iterator for this overlay string. */
3121 if (it->bidi_p)
3123 it->bidi_it.string.lstring = it->string;
3124 it->bidi_it.string.s = NULL;
3125 it->bidi_it.string.schars = SCHARS (it->string);
3126 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3127 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3128 it->bidi_it.string.unibyte = !it->multibyte_p;
3129 it->bidi_it.w = it->w;
3130 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3131 FRAME_WINDOW_P (it->f), &it->bidi_it);
3133 /* Synchronize the state of the bidi iterator with
3134 pos->string_pos. For any string position other than
3135 zero, this will be done automagically when we resume
3136 iteration over the string and get_visually_first_element
3137 is called. But if string_pos is zero, and the string is
3138 to be reordered for display, we need to resync manually,
3139 since it could be that the iteration state recorded in
3140 pos ended at string_pos of 0 moving backwards in string. */
3141 if (CHARPOS (pos->string_pos) == 0)
3143 get_visually_first_element (it);
3144 if (IT_STRING_CHARPOS (*it) != 0)
3145 do {
3146 /* Paranoia. */
3147 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3148 bidi_move_to_visually_next (&it->bidi_it);
3149 } while (it->bidi_it.charpos != 0);
3151 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3152 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3156 if (CHARPOS (pos->string_pos) >= 0)
3158 /* Recorded position is not in an overlay string, but in another
3159 string. This can only be a string from a `display' property.
3160 IT should already be filled with that string. */
3161 it->current.string_pos = pos->string_pos;
3162 eassert (STRINGP (it->string));
3163 if (it->bidi_p)
3164 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3165 FRAME_WINDOW_P (it->f), &it->bidi_it);
3168 /* Restore position in display vector translations, control
3169 character translations or ellipses. */
3170 if (pos->dpvec_index >= 0)
3172 if (it->dpvec == NULL)
3173 get_next_display_element (it);
3174 eassert (it->dpvec && it->current.dpvec_index == 0);
3175 it->current.dpvec_index = pos->dpvec_index;
3178 CHECK_IT (it);
3179 return !overlay_strings_with_newlines;
3183 /* Initialize IT for stepping through current_buffer in window W
3184 starting at ROW->start. */
3186 static void
3187 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3189 init_from_display_pos (it, w, &row->start);
3190 it->start = row->start;
3191 it->continuation_lines_width = row->continuation_lines_width;
3192 CHECK_IT (it);
3196 /* Initialize IT for stepping through current_buffer in window W
3197 starting in the line following ROW, i.e. starting at ROW->end.
3198 Value is zero if there are overlay strings with newlines at ROW's
3199 end position. */
3201 static int
3202 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3204 int success = 0;
3206 if (init_from_display_pos (it, w, &row->end))
3208 if (row->continued_p)
3209 it->continuation_lines_width
3210 = row->continuation_lines_width + row->pixel_width;
3211 CHECK_IT (it);
3212 success = 1;
3215 return success;
3221 /***********************************************************************
3222 Text properties
3223 ***********************************************************************/
3225 /* Called when IT reaches IT->stop_charpos. Handle text property and
3226 overlay changes. Set IT->stop_charpos to the next position where
3227 to stop. */
3229 static void
3230 handle_stop (struct it *it)
3232 enum prop_handled handled;
3233 int handle_overlay_change_p;
3234 struct props *p;
3236 it->dpvec = NULL;
3237 it->current.dpvec_index = -1;
3238 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3239 it->ignore_overlay_strings_at_pos_p = 0;
3240 it->ellipsis_p = 0;
3242 /* Use face of preceding text for ellipsis (if invisible) */
3243 if (it->selective_display_ellipsis_p)
3244 it->saved_face_id = it->face_id;
3248 handled = HANDLED_NORMALLY;
3250 /* Call text property handlers. */
3251 for (p = it_props; p->handler; ++p)
3253 handled = p->handler (it);
3255 if (handled == HANDLED_RECOMPUTE_PROPS)
3256 break;
3257 else if (handled == HANDLED_RETURN)
3259 /* We still want to show before and after strings from
3260 overlays even if the actual buffer text is replaced. */
3261 if (!handle_overlay_change_p
3262 || it->sp > 1
3263 /* Don't call get_overlay_strings_1 if we already
3264 have overlay strings loaded, because doing so
3265 will load them again and push the iterator state
3266 onto the stack one more time, which is not
3267 expected by the rest of the code that processes
3268 overlay strings. */
3269 || (it->current.overlay_string_index < 0
3270 ? !get_overlay_strings_1 (it, 0, 0)
3271 : 0))
3273 if (it->ellipsis_p)
3274 setup_for_ellipsis (it, 0);
3275 /* When handling a display spec, we might load an
3276 empty string. In that case, discard it here. We
3277 used to discard it in handle_single_display_spec,
3278 but that causes get_overlay_strings_1, above, to
3279 ignore overlay strings that we must check. */
3280 if (STRINGP (it->string) && !SCHARS (it->string))
3281 pop_it (it);
3282 return;
3284 else if (STRINGP (it->string) && !SCHARS (it->string))
3285 pop_it (it);
3286 else
3288 it->ignore_overlay_strings_at_pos_p = 1;
3289 it->string_from_display_prop_p = 0;
3290 it->from_disp_prop_p = 0;
3291 handle_overlay_change_p = 0;
3293 handled = HANDLED_RECOMPUTE_PROPS;
3294 break;
3296 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3297 handle_overlay_change_p = 0;
3300 if (handled != HANDLED_RECOMPUTE_PROPS)
3302 /* Don't check for overlay strings below when set to deliver
3303 characters from a display vector. */
3304 if (it->method == GET_FROM_DISPLAY_VECTOR)
3305 handle_overlay_change_p = 0;
3307 /* Handle overlay changes.
3308 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3309 if it finds overlays. */
3310 if (handle_overlay_change_p)
3311 handled = handle_overlay_change (it);
3314 if (it->ellipsis_p)
3316 setup_for_ellipsis (it, 0);
3317 break;
3320 while (handled == HANDLED_RECOMPUTE_PROPS);
3322 /* Determine where to stop next. */
3323 if (handled == HANDLED_NORMALLY)
3324 compute_stop_pos (it);
3328 /* Compute IT->stop_charpos from text property and overlay change
3329 information for IT's current position. */
3331 static void
3332 compute_stop_pos (struct it *it)
3334 register INTERVAL iv, next_iv;
3335 Lisp_Object object, limit, position;
3336 ptrdiff_t charpos, bytepos;
3338 if (STRINGP (it->string))
3340 /* Strings are usually short, so don't limit the search for
3341 properties. */
3342 it->stop_charpos = it->end_charpos;
3343 object = it->string;
3344 limit = Qnil;
3345 charpos = IT_STRING_CHARPOS (*it);
3346 bytepos = IT_STRING_BYTEPOS (*it);
3348 else
3350 ptrdiff_t pos;
3352 /* If end_charpos is out of range for some reason, such as a
3353 misbehaving display function, rationalize it (Bug#5984). */
3354 if (it->end_charpos > ZV)
3355 it->end_charpos = ZV;
3356 it->stop_charpos = it->end_charpos;
3358 /* If next overlay change is in front of the current stop pos
3359 (which is IT->end_charpos), stop there. Note: value of
3360 next_overlay_change is point-max if no overlay change
3361 follows. */
3362 charpos = IT_CHARPOS (*it);
3363 bytepos = IT_BYTEPOS (*it);
3364 pos = next_overlay_change (charpos);
3365 if (pos < it->stop_charpos)
3366 it->stop_charpos = pos;
3368 /* Set up variables for computing the stop position from text
3369 property changes. */
3370 XSETBUFFER (object, current_buffer);
3371 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3374 /* Get the interval containing IT's position. Value is a null
3375 interval if there isn't such an interval. */
3376 position = make_number (charpos);
3377 iv = validate_interval_range (object, &position, &position, 0);
3378 if (iv)
3380 Lisp_Object values_here[LAST_PROP_IDX];
3381 struct props *p;
3383 /* Get properties here. */
3384 for (p = it_props; p->handler; ++p)
3385 values_here[p->idx] = textget (iv->plist, *p->name);
3387 /* Look for an interval following iv that has different
3388 properties. */
3389 for (next_iv = next_interval (iv);
3390 (next_iv
3391 && (NILP (limit)
3392 || XFASTINT (limit) > next_iv->position));
3393 next_iv = next_interval (next_iv))
3395 for (p = it_props; p->handler; ++p)
3397 Lisp_Object new_value;
3399 new_value = textget (next_iv->plist, *p->name);
3400 if (!EQ (values_here[p->idx], new_value))
3401 break;
3404 if (p->handler)
3405 break;
3408 if (next_iv)
3410 if (INTEGERP (limit)
3411 && next_iv->position >= XFASTINT (limit))
3412 /* No text property change up to limit. */
3413 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3414 else
3415 /* Text properties change in next_iv. */
3416 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3420 if (it->cmp_it.id < 0)
3422 ptrdiff_t stoppos = it->end_charpos;
3424 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3425 stoppos = -1;
3426 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3427 stoppos, it->string);
3430 eassert (STRINGP (it->string)
3431 || (it->stop_charpos >= BEGV
3432 && it->stop_charpos >= IT_CHARPOS (*it)));
3436 /* Return the position of the next overlay change after POS in
3437 current_buffer. Value is point-max if no overlay change
3438 follows. This is like `next-overlay-change' but doesn't use
3439 xmalloc. */
3441 static ptrdiff_t
3442 next_overlay_change (ptrdiff_t pos)
3444 ptrdiff_t i, noverlays;
3445 ptrdiff_t endpos;
3446 Lisp_Object *overlays;
3448 /* Get all overlays at the given position. */
3449 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3451 /* If any of these overlays ends before endpos,
3452 use its ending point instead. */
3453 for (i = 0; i < noverlays; ++i)
3455 Lisp_Object oend;
3456 ptrdiff_t oendpos;
3458 oend = OVERLAY_END (overlays[i]);
3459 oendpos = OVERLAY_POSITION (oend);
3460 endpos = min (endpos, oendpos);
3463 return endpos;
3466 /* How many characters forward to search for a display property or
3467 display string. Searching too far forward makes the bidi display
3468 sluggish, especially in small windows. */
3469 #define MAX_DISP_SCAN 250
3471 /* Return the character position of a display string at or after
3472 position specified by POSITION. If no display string exists at or
3473 after POSITION, return ZV. A display string is either an overlay
3474 with `display' property whose value is a string, or a `display'
3475 text property whose value is a string. STRING is data about the
3476 string to iterate; if STRING->lstring is nil, we are iterating a
3477 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3478 on a GUI frame. DISP_PROP is set to zero if we searched
3479 MAX_DISP_SCAN characters forward without finding any display
3480 strings, non-zero otherwise. It is set to 2 if the display string
3481 uses any kind of `(space ...)' spec that will produce a stretch of
3482 white space in the text area. */
3483 ptrdiff_t
3484 compute_display_string_pos (struct text_pos *position,
3485 struct bidi_string_data *string,
3486 struct window *w,
3487 int frame_window_p, int *disp_prop)
3489 /* OBJECT = nil means current buffer. */
3490 Lisp_Object object, object1;
3491 Lisp_Object pos, spec, limpos;
3492 int string_p = (string && (STRINGP (string->lstring) || string->s));
3493 ptrdiff_t eob = string_p ? string->schars : ZV;
3494 ptrdiff_t begb = string_p ? 0 : BEGV;
3495 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3496 ptrdiff_t lim =
3497 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3498 struct text_pos tpos;
3499 int rv = 0;
3501 if (string && STRINGP (string->lstring))
3502 object1 = object = string->lstring;
3503 else if (w && !string_p)
3505 XSETWINDOW (object, w);
3506 object1 = Qnil;
3508 else
3509 object1 = object = Qnil;
3511 *disp_prop = 1;
3513 if (charpos >= eob
3514 /* We don't support display properties whose values are strings
3515 that have display string properties. */
3516 || string->from_disp_str
3517 /* C strings cannot have display properties. */
3518 || (string->s && !STRINGP (object)))
3520 *disp_prop = 0;
3521 return eob;
3524 /* If the character at CHARPOS is where the display string begins,
3525 return CHARPOS. */
3526 pos = make_number (charpos);
3527 if (STRINGP (object))
3528 bufpos = string->bufpos;
3529 else
3530 bufpos = charpos;
3531 tpos = *position;
3532 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3533 && (charpos <= begb
3534 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3535 object),
3536 spec))
3537 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3538 frame_window_p)))
3540 if (rv == 2)
3541 *disp_prop = 2;
3542 return charpos;
3545 /* Look forward for the first character with a `display' property
3546 that will replace the underlying text when displayed. */
3547 limpos = make_number (lim);
3548 do {
3549 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3550 CHARPOS (tpos) = XFASTINT (pos);
3551 if (CHARPOS (tpos) >= lim)
3553 *disp_prop = 0;
3554 break;
3556 if (STRINGP (object))
3557 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3558 else
3559 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3560 spec = Fget_char_property (pos, Qdisplay, object);
3561 if (!STRINGP (object))
3562 bufpos = CHARPOS (tpos);
3563 } while (NILP (spec)
3564 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3565 bufpos, frame_window_p)));
3566 if (rv == 2)
3567 *disp_prop = 2;
3569 return CHARPOS (tpos);
3572 /* Return the character position of the end of the display string that
3573 started at CHARPOS. If there's no display string at CHARPOS,
3574 return -1. A display string is either an overlay with `display'
3575 property whose value is a string or a `display' text property whose
3576 value is a string. */
3577 ptrdiff_t
3578 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3580 /* OBJECT = nil means current buffer. */
3581 Lisp_Object object =
3582 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3583 Lisp_Object pos = make_number (charpos);
3584 ptrdiff_t eob =
3585 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3587 if (charpos >= eob || (string->s && !STRINGP (object)))
3588 return eob;
3590 /* It could happen that the display property or overlay was removed
3591 since we found it in compute_display_string_pos above. One way
3592 this can happen is if JIT font-lock was called (through
3593 handle_fontified_prop), and jit-lock-functions remove text
3594 properties or overlays from the portion of buffer that includes
3595 CHARPOS. Muse mode is known to do that, for example. In this
3596 case, we return -1 to the caller, to signal that no display
3597 string is actually present at CHARPOS. See bidi_fetch_char for
3598 how this is handled.
3600 An alternative would be to never look for display properties past
3601 it->stop_charpos. But neither compute_display_string_pos nor
3602 bidi_fetch_char that calls it know or care where the next
3603 stop_charpos is. */
3604 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3605 return -1;
3607 /* Look forward for the first character where the `display' property
3608 changes. */
3609 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3611 return XFASTINT (pos);
3616 /***********************************************************************
3617 Fontification
3618 ***********************************************************************/
3620 /* Handle changes in the `fontified' property of the current buffer by
3621 calling hook functions from Qfontification_functions to fontify
3622 regions of text. */
3624 static enum prop_handled
3625 handle_fontified_prop (struct it *it)
3627 Lisp_Object prop, pos;
3628 enum prop_handled handled = HANDLED_NORMALLY;
3630 if (!NILP (Vmemory_full))
3631 return handled;
3633 /* Get the value of the `fontified' property at IT's current buffer
3634 position. (The `fontified' property doesn't have a special
3635 meaning in strings.) If the value is nil, call functions from
3636 Qfontification_functions. */
3637 if (!STRINGP (it->string)
3638 && it->s == NULL
3639 && !NILP (Vfontification_functions)
3640 && !NILP (Vrun_hooks)
3641 && (pos = make_number (IT_CHARPOS (*it)),
3642 prop = Fget_char_property (pos, Qfontified, Qnil),
3643 /* Ignore the special cased nil value always present at EOB since
3644 no amount of fontifying will be able to change it. */
3645 NILP (prop) && IT_CHARPOS (*it) < Z))
3647 ptrdiff_t count = SPECPDL_INDEX ();
3648 Lisp_Object val;
3649 struct buffer *obuf = current_buffer;
3650 ptrdiff_t begv = BEGV, zv = ZV;
3651 bool old_clip_changed = current_buffer->clip_changed;
3653 val = Vfontification_functions;
3654 specbind (Qfontification_functions, Qnil);
3656 eassert (it->end_charpos == ZV);
3658 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3659 safe_call1 (val, pos);
3660 else
3662 Lisp_Object fns, fn;
3663 struct gcpro gcpro1, gcpro2;
3665 fns = Qnil;
3666 GCPRO2 (val, fns);
3668 for (; CONSP (val); val = XCDR (val))
3670 fn = XCAR (val);
3672 if (EQ (fn, Qt))
3674 /* A value of t indicates this hook has a local
3675 binding; it means to run the global binding too.
3676 In a global value, t should not occur. If it
3677 does, we must ignore it to avoid an endless
3678 loop. */
3679 for (fns = Fdefault_value (Qfontification_functions);
3680 CONSP (fns);
3681 fns = XCDR (fns))
3683 fn = XCAR (fns);
3684 if (!EQ (fn, Qt))
3685 safe_call1 (fn, pos);
3688 else
3689 safe_call1 (fn, pos);
3692 UNGCPRO;
3695 unbind_to (count, Qnil);
3697 /* Fontification functions routinely call `save-restriction'.
3698 Normally, this tags clip_changed, which can confuse redisplay
3699 (see discussion in Bug#6671). Since we don't perform any
3700 special handling of fontification changes in the case where
3701 `save-restriction' isn't called, there's no point doing so in
3702 this case either. So, if the buffer's restrictions are
3703 actually left unchanged, reset clip_changed. */
3704 if (obuf == current_buffer)
3706 if (begv == BEGV && zv == ZV)
3707 current_buffer->clip_changed = old_clip_changed;
3709 /* There isn't much we can reasonably do to protect against
3710 misbehaving fontification, but here's a fig leaf. */
3711 else if (BUFFER_LIVE_P (obuf))
3712 set_buffer_internal_1 (obuf);
3714 /* The fontification code may have added/removed text.
3715 It could do even a lot worse, but let's at least protect against
3716 the most obvious case where only the text past `pos' gets changed',
3717 as is/was done in grep.el where some escapes sequences are turned
3718 into face properties (bug#7876). */
3719 it->end_charpos = ZV;
3721 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3722 something. This avoids an endless loop if they failed to
3723 fontify the text for which reason ever. */
3724 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3725 handled = HANDLED_RECOMPUTE_PROPS;
3728 return handled;
3733 /***********************************************************************
3734 Faces
3735 ***********************************************************************/
3737 /* Set up iterator IT from face properties at its current position.
3738 Called from handle_stop. */
3740 static enum prop_handled
3741 handle_face_prop (struct it *it)
3743 int new_face_id;
3744 ptrdiff_t next_stop;
3746 if (!STRINGP (it->string))
3748 new_face_id
3749 = face_at_buffer_position (it->w,
3750 IT_CHARPOS (*it),
3751 &next_stop,
3752 (IT_CHARPOS (*it)
3753 + TEXT_PROP_DISTANCE_LIMIT),
3754 0, it->base_face_id);
3756 /* Is this a start of a run of characters with box face?
3757 Caveat: this can be called for a freshly initialized
3758 iterator; face_id is -1 in this case. We know that the new
3759 face will not change until limit, i.e. if the new face has a
3760 box, all characters up to limit will have one. But, as
3761 usual, we don't know whether limit is really the end. */
3762 if (new_face_id != it->face_id)
3764 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3765 /* If it->face_id is -1, old_face below will be NULL, see
3766 the definition of FACE_FROM_ID. This will happen if this
3767 is the initial call that gets the face. */
3768 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3770 /* If the value of face_id of the iterator is -1, we have to
3771 look in front of IT's position and see whether there is a
3772 face there that's different from new_face_id. */
3773 if (!old_face && IT_CHARPOS (*it) > BEG)
3775 int prev_face_id = face_before_it_pos (it);
3777 old_face = FACE_FROM_ID (it->f, prev_face_id);
3780 /* If the new face has a box, but the old face does not,
3781 this is the start of a run of characters with box face,
3782 i.e. this character has a shadow on the left side. */
3783 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3784 && (old_face == NULL || !old_face->box));
3785 it->face_box_p = new_face->box != FACE_NO_BOX;
3788 else
3790 int base_face_id;
3791 ptrdiff_t bufpos;
3792 int i;
3793 Lisp_Object from_overlay
3794 = (it->current.overlay_string_index >= 0
3795 ? it->string_overlays[it->current.overlay_string_index
3796 % OVERLAY_STRING_CHUNK_SIZE]
3797 : Qnil);
3799 /* See if we got to this string directly or indirectly from
3800 an overlay property. That includes the before-string or
3801 after-string of an overlay, strings in display properties
3802 provided by an overlay, their text properties, etc.
3804 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3805 if (! NILP (from_overlay))
3806 for (i = it->sp - 1; i >= 0; i--)
3808 if (it->stack[i].current.overlay_string_index >= 0)
3809 from_overlay
3810 = it->string_overlays[it->stack[i].current.overlay_string_index
3811 % OVERLAY_STRING_CHUNK_SIZE];
3812 else if (! NILP (it->stack[i].from_overlay))
3813 from_overlay = it->stack[i].from_overlay;
3815 if (!NILP (from_overlay))
3816 break;
3819 if (! NILP (from_overlay))
3821 bufpos = IT_CHARPOS (*it);
3822 /* For a string from an overlay, the base face depends
3823 only on text properties and ignores overlays. */
3824 base_face_id
3825 = face_for_overlay_string (it->w,
3826 IT_CHARPOS (*it),
3827 &next_stop,
3828 (IT_CHARPOS (*it)
3829 + TEXT_PROP_DISTANCE_LIMIT),
3831 from_overlay);
3833 else
3835 bufpos = 0;
3837 /* For strings from a `display' property, use the face at
3838 IT's current buffer position as the base face to merge
3839 with, so that overlay strings appear in the same face as
3840 surrounding text, unless they specify their own faces.
3841 For strings from wrap-prefix and line-prefix properties,
3842 use the default face, possibly remapped via
3843 Vface_remapping_alist. */
3844 base_face_id = it->string_from_prefix_prop_p
3845 ? (!NILP (Vface_remapping_alist)
3846 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3847 : DEFAULT_FACE_ID)
3848 : underlying_face_id (it);
3851 new_face_id = face_at_string_position (it->w,
3852 it->string,
3853 IT_STRING_CHARPOS (*it),
3854 bufpos,
3855 &next_stop,
3856 base_face_id, 0);
3858 /* Is this a start of a run of characters with box? Caveat:
3859 this can be called for a freshly allocated iterator; face_id
3860 is -1 is this case. We know that the new face will not
3861 change until the next check pos, i.e. if the new face has a
3862 box, all characters up to that position will have a
3863 box. But, as usual, we don't know whether that position
3864 is really the end. */
3865 if (new_face_id != it->face_id)
3867 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3868 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3870 /* If new face has a box but old face hasn't, this is the
3871 start of a run of characters with box, i.e. it has a
3872 shadow on the left side. */
3873 it->start_of_box_run_p
3874 = new_face->box && (old_face == NULL || !old_face->box);
3875 it->face_box_p = new_face->box != FACE_NO_BOX;
3879 it->face_id = new_face_id;
3880 return HANDLED_NORMALLY;
3884 /* Return the ID of the face ``underlying'' IT's current position,
3885 which is in a string. If the iterator is associated with a
3886 buffer, return the face at IT's current buffer position.
3887 Otherwise, use the iterator's base_face_id. */
3889 static int
3890 underlying_face_id (struct it *it)
3892 int face_id = it->base_face_id, i;
3894 eassert (STRINGP (it->string));
3896 for (i = it->sp - 1; i >= 0; --i)
3897 if (NILP (it->stack[i].string))
3898 face_id = it->stack[i].face_id;
3900 return face_id;
3904 /* Compute the face one character before or after the current position
3905 of IT, in the visual order. BEFORE_P non-zero means get the face
3906 in front (to the left in L2R paragraphs, to the right in R2L
3907 paragraphs) of IT's screen position. Value is the ID of the face. */
3909 static int
3910 face_before_or_after_it_pos (struct it *it, int before_p)
3912 int face_id, limit;
3913 ptrdiff_t next_check_charpos;
3914 struct it it_copy;
3915 void *it_copy_data = NULL;
3917 eassert (it->s == NULL);
3919 if (STRINGP (it->string))
3921 ptrdiff_t bufpos, charpos;
3922 int base_face_id;
3924 /* No face change past the end of the string (for the case
3925 we are padding with spaces). No face change before the
3926 string start. */
3927 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3928 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3929 return it->face_id;
3931 if (!it->bidi_p)
3933 /* Set charpos to the position before or after IT's current
3934 position, in the logical order, which in the non-bidi
3935 case is the same as the visual order. */
3936 if (before_p)
3937 charpos = IT_STRING_CHARPOS (*it) - 1;
3938 else if (it->what == IT_COMPOSITION)
3939 /* For composition, we must check the character after the
3940 composition. */
3941 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3942 else
3943 charpos = IT_STRING_CHARPOS (*it) + 1;
3945 else
3947 if (before_p)
3949 /* With bidi iteration, the character before the current
3950 in the visual order cannot be found by simple
3951 iteration, because "reverse" reordering is not
3952 supported. Instead, we need to use the move_it_*
3953 family of functions. */
3954 /* Ignore face changes before the first visible
3955 character on this display line. */
3956 if (it->current_x <= it->first_visible_x)
3957 return it->face_id;
3958 SAVE_IT (it_copy, *it, it_copy_data);
3959 /* Implementation note: Since move_it_in_display_line
3960 works in the iterator geometry, and thinks the first
3961 character is always the leftmost, even in R2L lines,
3962 we don't need to distinguish between the R2L and L2R
3963 cases here. */
3964 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3965 it_copy.current_x - 1, MOVE_TO_X);
3966 charpos = IT_STRING_CHARPOS (it_copy);
3967 RESTORE_IT (it, it, it_copy_data);
3969 else
3971 /* Set charpos to the string position of the character
3972 that comes after IT's current position in the visual
3973 order. */
3974 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3976 it_copy = *it;
3977 while (n--)
3978 bidi_move_to_visually_next (&it_copy.bidi_it);
3980 charpos = it_copy.bidi_it.charpos;
3983 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3985 if (it->current.overlay_string_index >= 0)
3986 bufpos = IT_CHARPOS (*it);
3987 else
3988 bufpos = 0;
3990 base_face_id = underlying_face_id (it);
3992 /* Get the face for ASCII, or unibyte. */
3993 face_id = face_at_string_position (it->w,
3994 it->string,
3995 charpos,
3996 bufpos,
3997 &next_check_charpos,
3998 base_face_id, 0);
4000 /* Correct the face for charsets different from ASCII. Do it
4001 for the multibyte case only. The face returned above is
4002 suitable for unibyte text if IT->string is unibyte. */
4003 if (STRING_MULTIBYTE (it->string))
4005 struct text_pos pos1 = string_pos (charpos, it->string);
4006 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4007 int c, len;
4008 struct face *face = FACE_FROM_ID (it->f, face_id);
4010 c = string_char_and_length (p, &len);
4011 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4014 else
4016 struct text_pos pos;
4018 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4019 || (IT_CHARPOS (*it) <= BEGV && before_p))
4020 return it->face_id;
4022 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4023 pos = it->current.pos;
4025 if (!it->bidi_p)
4027 if (before_p)
4028 DEC_TEXT_POS (pos, it->multibyte_p);
4029 else
4031 if (it->what == IT_COMPOSITION)
4033 /* For composition, we must check the position after
4034 the composition. */
4035 pos.charpos += it->cmp_it.nchars;
4036 pos.bytepos += it->len;
4038 else
4039 INC_TEXT_POS (pos, it->multibyte_p);
4042 else
4044 if (before_p)
4046 /* With bidi iteration, the character before the current
4047 in the visual order cannot be found by simple
4048 iteration, because "reverse" reordering is not
4049 supported. Instead, we need to use the move_it_*
4050 family of functions. */
4051 /* Ignore face changes before the first visible
4052 character on this display line. */
4053 if (it->current_x <= it->first_visible_x)
4054 return it->face_id;
4055 SAVE_IT (it_copy, *it, it_copy_data);
4056 /* Implementation note: Since move_it_in_display_line
4057 works in the iterator geometry, and thinks the first
4058 character is always the leftmost, even in R2L lines,
4059 we don't need to distinguish between the R2L and L2R
4060 cases here. */
4061 move_it_in_display_line (&it_copy, ZV,
4062 it_copy.current_x - 1, MOVE_TO_X);
4063 pos = it_copy.current.pos;
4064 RESTORE_IT (it, it, it_copy_data);
4066 else
4068 /* Set charpos to the buffer position of the character
4069 that comes after IT's current position in the visual
4070 order. */
4071 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4073 it_copy = *it;
4074 while (n--)
4075 bidi_move_to_visually_next (&it_copy.bidi_it);
4077 SET_TEXT_POS (pos,
4078 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4081 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4083 /* Determine face for CHARSET_ASCII, or unibyte. */
4084 face_id = face_at_buffer_position (it->w,
4085 CHARPOS (pos),
4086 &next_check_charpos,
4087 limit, 0, -1);
4089 /* Correct the face for charsets different from ASCII. Do it
4090 for the multibyte case only. The face returned above is
4091 suitable for unibyte text if current_buffer is unibyte. */
4092 if (it->multibyte_p)
4094 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4095 struct face *face = FACE_FROM_ID (it->f, face_id);
4096 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4100 return face_id;
4105 /***********************************************************************
4106 Invisible text
4107 ***********************************************************************/
4109 /* Set up iterator IT from invisible properties at its current
4110 position. Called from handle_stop. */
4112 static enum prop_handled
4113 handle_invisible_prop (struct it *it)
4115 enum prop_handled handled = HANDLED_NORMALLY;
4116 int invis_p;
4117 Lisp_Object prop;
4119 if (STRINGP (it->string))
4121 Lisp_Object end_charpos, limit, charpos;
4123 /* Get the value of the invisible text property at the
4124 current position. Value will be nil if there is no such
4125 property. */
4126 charpos = make_number (IT_STRING_CHARPOS (*it));
4127 prop = Fget_text_property (charpos, Qinvisible, it->string);
4128 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4130 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4132 /* Record whether we have to display an ellipsis for the
4133 invisible text. */
4134 int display_ellipsis_p = (invis_p == 2);
4135 ptrdiff_t len, endpos;
4137 handled = HANDLED_RECOMPUTE_PROPS;
4139 /* Get the position at which the next visible text can be
4140 found in IT->string, if any. */
4141 endpos = len = SCHARS (it->string);
4142 XSETINT (limit, len);
4145 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4146 it->string, limit);
4147 if (INTEGERP (end_charpos))
4149 endpos = XFASTINT (end_charpos);
4150 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4151 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4152 if (invis_p == 2)
4153 display_ellipsis_p = 1;
4156 while (invis_p && endpos < len);
4158 if (display_ellipsis_p)
4159 it->ellipsis_p = 1;
4161 if (endpos < len)
4163 /* Text at END_CHARPOS is visible. Move IT there. */
4164 struct text_pos old;
4165 ptrdiff_t oldpos;
4167 old = it->current.string_pos;
4168 oldpos = CHARPOS (old);
4169 if (it->bidi_p)
4171 if (it->bidi_it.first_elt
4172 && it->bidi_it.charpos < SCHARS (it->string))
4173 bidi_paragraph_init (it->paragraph_embedding,
4174 &it->bidi_it, 1);
4175 /* Bidi-iterate out of the invisible text. */
4178 bidi_move_to_visually_next (&it->bidi_it);
4180 while (oldpos <= it->bidi_it.charpos
4181 && it->bidi_it.charpos < endpos);
4183 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4184 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4185 if (IT_CHARPOS (*it) >= endpos)
4186 it->prev_stop = endpos;
4188 else
4190 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4191 compute_string_pos (&it->current.string_pos, old, it->string);
4194 else
4196 /* The rest of the string is invisible. If this is an
4197 overlay string, proceed with the next overlay string
4198 or whatever comes and return a character from there. */
4199 if (it->current.overlay_string_index >= 0
4200 && !display_ellipsis_p)
4202 next_overlay_string (it);
4203 /* Don't check for overlay strings when we just
4204 finished processing them. */
4205 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4207 else
4209 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4210 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4215 else
4217 ptrdiff_t newpos, next_stop, start_charpos, tem;
4218 Lisp_Object pos, overlay;
4220 /* First of all, is there invisible text at this position? */
4221 tem = start_charpos = IT_CHARPOS (*it);
4222 pos = make_number (tem);
4223 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4224 &overlay);
4225 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4227 /* If we are on invisible text, skip over it. */
4228 if (invis_p && start_charpos < it->end_charpos)
4230 /* Record whether we have to display an ellipsis for the
4231 invisible text. */
4232 int display_ellipsis_p = invis_p == 2;
4234 handled = HANDLED_RECOMPUTE_PROPS;
4236 /* Loop skipping over invisible text. The loop is left at
4237 ZV or with IT on the first char being visible again. */
4240 /* Try to skip some invisible text. Return value is the
4241 position reached which can be equal to where we start
4242 if there is nothing invisible there. This skips both
4243 over invisible text properties and overlays with
4244 invisible property. */
4245 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4247 /* If we skipped nothing at all we weren't at invisible
4248 text in the first place. If everything to the end of
4249 the buffer was skipped, end the loop. */
4250 if (newpos == tem || newpos >= ZV)
4251 invis_p = 0;
4252 else
4254 /* We skipped some characters but not necessarily
4255 all there are. Check if we ended up on visible
4256 text. Fget_char_property returns the property of
4257 the char before the given position, i.e. if we
4258 get invis_p = 0, this means that the char at
4259 newpos is visible. */
4260 pos = make_number (newpos);
4261 prop = Fget_char_property (pos, Qinvisible, it->window);
4262 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4265 /* If we ended up on invisible text, proceed to
4266 skip starting with next_stop. */
4267 if (invis_p)
4268 tem = next_stop;
4270 /* If there are adjacent invisible texts, don't lose the
4271 second one's ellipsis. */
4272 if (invis_p == 2)
4273 display_ellipsis_p = 1;
4275 while (invis_p);
4277 /* The position newpos is now either ZV or on visible text. */
4278 if (it->bidi_p)
4280 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4281 int on_newline =
4282 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4283 int after_newline =
4284 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4286 /* If the invisible text ends on a newline or on a
4287 character after a newline, we can avoid the costly,
4288 character by character, bidi iteration to NEWPOS, and
4289 instead simply reseat the iterator there. That's
4290 because all bidi reordering information is tossed at
4291 the newline. This is a big win for modes that hide
4292 complete lines, like Outline, Org, etc. */
4293 if (on_newline || after_newline)
4295 struct text_pos tpos;
4296 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4298 SET_TEXT_POS (tpos, newpos, bpos);
4299 reseat_1 (it, tpos, 0);
4300 /* If we reseat on a newline/ZV, we need to prep the
4301 bidi iterator for advancing to the next character
4302 after the newline/EOB, keeping the current paragraph
4303 direction (so that PRODUCE_GLYPHS does TRT wrt
4304 prepending/appending glyphs to a glyph row). */
4305 if (on_newline)
4307 it->bidi_it.first_elt = 0;
4308 it->bidi_it.paragraph_dir = pdir;
4309 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4310 it->bidi_it.nchars = 1;
4311 it->bidi_it.ch_len = 1;
4314 else /* Must use the slow method. */
4316 /* With bidi iteration, the region of invisible text
4317 could start and/or end in the middle of a
4318 non-base embedding level. Therefore, we need to
4319 skip invisible text using the bidi iterator,
4320 starting at IT's current position, until we find
4321 ourselves outside of the invisible text.
4322 Skipping invisible text _after_ bidi iteration
4323 avoids affecting the visual order of the
4324 displayed text when invisible properties are
4325 added or removed. */
4326 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4328 /* If we were `reseat'ed to a new paragraph,
4329 determine the paragraph base direction. We
4330 need to do it now because
4331 next_element_from_buffer may not have a
4332 chance to do it, if we are going to skip any
4333 text at the beginning, which resets the
4334 FIRST_ELT flag. */
4335 bidi_paragraph_init (it->paragraph_embedding,
4336 &it->bidi_it, 1);
4340 bidi_move_to_visually_next (&it->bidi_it);
4342 while (it->stop_charpos <= it->bidi_it.charpos
4343 && it->bidi_it.charpos < newpos);
4344 IT_CHARPOS (*it) = it->bidi_it.charpos;
4345 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4346 /* If we overstepped NEWPOS, record its position in
4347 the iterator, so that we skip invisible text if
4348 later the bidi iteration lands us in the
4349 invisible region again. */
4350 if (IT_CHARPOS (*it) >= newpos)
4351 it->prev_stop = newpos;
4354 else
4356 IT_CHARPOS (*it) = newpos;
4357 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4360 /* If there are before-strings at the start of invisible
4361 text, and the text is invisible because of a text
4362 property, arrange to show before-strings because 20.x did
4363 it that way. (If the text is invisible because of an
4364 overlay property instead of a text property, this is
4365 already handled in the overlay code.) */
4366 if (NILP (overlay)
4367 && get_overlay_strings (it, it->stop_charpos))
4369 handled = HANDLED_RECOMPUTE_PROPS;
4370 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4372 else if (display_ellipsis_p)
4374 /* Make sure that the glyphs of the ellipsis will get
4375 correct `charpos' values. If we would not update
4376 it->position here, the glyphs would belong to the
4377 last visible character _before_ the invisible
4378 text, which confuses `set_cursor_from_row'.
4380 We use the last invisible position instead of the
4381 first because this way the cursor is always drawn on
4382 the first "." of the ellipsis, whenever PT is inside
4383 the invisible text. Otherwise the cursor would be
4384 placed _after_ the ellipsis when the point is after the
4385 first invisible character. */
4386 if (!STRINGP (it->object))
4388 it->position.charpos = newpos - 1;
4389 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4391 it->ellipsis_p = 1;
4392 /* Let the ellipsis display before
4393 considering any properties of the following char.
4394 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4395 handled = HANDLED_RETURN;
4400 return handled;
4404 /* Make iterator IT return `...' next.
4405 Replaces LEN characters from buffer. */
4407 static void
4408 setup_for_ellipsis (struct it *it, int len)
4410 /* Use the display table definition for `...'. Invalid glyphs
4411 will be handled by the method returning elements from dpvec. */
4412 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4414 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4415 it->dpvec = v->u.contents;
4416 it->dpend = v->u.contents + v->header.size;
4418 else
4420 /* Default `...'. */
4421 it->dpvec = default_invis_vector;
4422 it->dpend = default_invis_vector + 3;
4425 it->dpvec_char_len = len;
4426 it->current.dpvec_index = 0;
4427 it->dpvec_face_id = -1;
4429 /* Remember the current face id in case glyphs specify faces.
4430 IT's face is restored in set_iterator_to_next.
4431 saved_face_id was set to preceding char's face in handle_stop. */
4432 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4433 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4435 it->method = GET_FROM_DISPLAY_VECTOR;
4436 it->ellipsis_p = 1;
4441 /***********************************************************************
4442 'display' property
4443 ***********************************************************************/
4445 /* Set up iterator IT from `display' property at its current position.
4446 Called from handle_stop.
4447 We return HANDLED_RETURN if some part of the display property
4448 overrides the display of the buffer text itself.
4449 Otherwise we return HANDLED_NORMALLY. */
4451 static enum prop_handled
4452 handle_display_prop (struct it *it)
4454 Lisp_Object propval, object, overlay;
4455 struct text_pos *position;
4456 ptrdiff_t bufpos;
4457 /* Nonzero if some property replaces the display of the text itself. */
4458 int display_replaced_p = 0;
4460 if (STRINGP (it->string))
4462 object = it->string;
4463 position = &it->current.string_pos;
4464 bufpos = CHARPOS (it->current.pos);
4466 else
4468 XSETWINDOW (object, it->w);
4469 position = &it->current.pos;
4470 bufpos = CHARPOS (*position);
4473 /* Reset those iterator values set from display property values. */
4474 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4475 it->space_width = Qnil;
4476 it->font_height = Qnil;
4477 it->voffset = 0;
4479 /* We don't support recursive `display' properties, i.e. string
4480 values that have a string `display' property, that have a string
4481 `display' property etc. */
4482 if (!it->string_from_display_prop_p)
4483 it->area = TEXT_AREA;
4485 propval = get_char_property_and_overlay (make_number (position->charpos),
4486 Qdisplay, object, &overlay);
4487 if (NILP (propval))
4488 return HANDLED_NORMALLY;
4489 /* Now OVERLAY is the overlay that gave us this property, or nil
4490 if it was a text property. */
4492 if (!STRINGP (it->string))
4493 object = it->w->contents;
4495 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4496 position, bufpos,
4497 FRAME_WINDOW_P (it->f));
4499 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4502 /* Subroutine of handle_display_prop. Returns non-zero if the display
4503 specification in SPEC is a replacing specification, i.e. it would
4504 replace the text covered by `display' property with something else,
4505 such as an image or a display string. If SPEC includes any kind or
4506 `(space ...) specification, the value is 2; this is used by
4507 compute_display_string_pos, which see.
4509 See handle_single_display_spec for documentation of arguments.
4510 frame_window_p is non-zero if the window being redisplayed is on a
4511 GUI frame; this argument is used only if IT is NULL, see below.
4513 IT can be NULL, if this is called by the bidi reordering code
4514 through compute_display_string_pos, which see. In that case, this
4515 function only examines SPEC, but does not otherwise "handle" it, in
4516 the sense that it doesn't set up members of IT from the display
4517 spec. */
4518 static int
4519 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4520 Lisp_Object overlay, struct text_pos *position,
4521 ptrdiff_t bufpos, int frame_window_p)
4523 int replacing_p = 0;
4524 int rv;
4526 if (CONSP (spec)
4527 /* Simple specifications. */
4528 && !EQ (XCAR (spec), Qimage)
4529 && !EQ (XCAR (spec), Qspace)
4530 && !EQ (XCAR (spec), Qwhen)
4531 && !EQ (XCAR (spec), Qslice)
4532 && !EQ (XCAR (spec), Qspace_width)
4533 && !EQ (XCAR (spec), Qheight)
4534 && !EQ (XCAR (spec), Qraise)
4535 /* Marginal area specifications. */
4536 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4537 && !EQ (XCAR (spec), Qleft_fringe)
4538 && !EQ (XCAR (spec), Qright_fringe)
4539 && !NILP (XCAR (spec)))
4541 for (; CONSP (spec); spec = XCDR (spec))
4543 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4544 overlay, position, bufpos,
4545 replacing_p, frame_window_p)))
4547 replacing_p = rv;
4548 /* If some text in a string is replaced, `position' no
4549 longer points to the position of `object'. */
4550 if (!it || STRINGP (object))
4551 break;
4555 else if (VECTORP (spec))
4557 ptrdiff_t i;
4558 for (i = 0; i < ASIZE (spec); ++i)
4559 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4560 overlay, position, bufpos,
4561 replacing_p, frame_window_p)))
4563 replacing_p = rv;
4564 /* If some text in a string is replaced, `position' no
4565 longer points to the position of `object'. */
4566 if (!it || STRINGP (object))
4567 break;
4570 else
4572 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4573 position, bufpos, 0,
4574 frame_window_p)))
4575 replacing_p = rv;
4578 return replacing_p;
4581 /* Value is the position of the end of the `display' property starting
4582 at START_POS in OBJECT. */
4584 static struct text_pos
4585 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4587 Lisp_Object end;
4588 struct text_pos end_pos;
4590 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4591 Qdisplay, object, Qnil);
4592 CHARPOS (end_pos) = XFASTINT (end);
4593 if (STRINGP (object))
4594 compute_string_pos (&end_pos, start_pos, it->string);
4595 else
4596 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4598 return end_pos;
4602 /* Set up IT from a single `display' property specification SPEC. OBJECT
4603 is the object in which the `display' property was found. *POSITION
4604 is the position in OBJECT at which the `display' property was found.
4605 BUFPOS is the buffer position of OBJECT (different from POSITION if
4606 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4607 previously saw a display specification which already replaced text
4608 display with something else, for example an image; we ignore such
4609 properties after the first one has been processed.
4611 OVERLAY is the overlay this `display' property came from,
4612 or nil if it was a text property.
4614 If SPEC is a `space' or `image' specification, and in some other
4615 cases too, set *POSITION to the position where the `display'
4616 property ends.
4618 If IT is NULL, only examine the property specification in SPEC, but
4619 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4620 is intended to be displayed in a window on a GUI frame.
4622 Value is non-zero if something was found which replaces the display
4623 of buffer or string text. */
4625 static int
4626 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4627 Lisp_Object overlay, struct text_pos *position,
4628 ptrdiff_t bufpos, int display_replaced_p,
4629 int frame_window_p)
4631 Lisp_Object form;
4632 Lisp_Object location, value;
4633 struct text_pos start_pos = *position;
4634 int valid_p;
4636 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4637 If the result is non-nil, use VALUE instead of SPEC. */
4638 form = Qt;
4639 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4641 spec = XCDR (spec);
4642 if (!CONSP (spec))
4643 return 0;
4644 form = XCAR (spec);
4645 spec = XCDR (spec);
4648 if (!NILP (form) && !EQ (form, Qt))
4650 ptrdiff_t count = SPECPDL_INDEX ();
4651 struct gcpro gcpro1;
4653 /* Bind `object' to the object having the `display' property, a
4654 buffer or string. Bind `position' to the position in the
4655 object where the property was found, and `buffer-position'
4656 to the current position in the buffer. */
4658 if (NILP (object))
4659 XSETBUFFER (object, current_buffer);
4660 specbind (Qobject, object);
4661 specbind (Qposition, make_number (CHARPOS (*position)));
4662 specbind (Qbuffer_position, make_number (bufpos));
4663 GCPRO1 (form);
4664 form = safe_eval (form);
4665 UNGCPRO;
4666 unbind_to (count, Qnil);
4669 if (NILP (form))
4670 return 0;
4672 /* Handle `(height HEIGHT)' specifications. */
4673 if (CONSP (spec)
4674 && EQ (XCAR (spec), Qheight)
4675 && CONSP (XCDR (spec)))
4677 if (it)
4679 if (!FRAME_WINDOW_P (it->f))
4680 return 0;
4682 it->font_height = XCAR (XCDR (spec));
4683 if (!NILP (it->font_height))
4685 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4686 int new_height = -1;
4688 if (CONSP (it->font_height)
4689 && (EQ (XCAR (it->font_height), Qplus)
4690 || EQ (XCAR (it->font_height), Qminus))
4691 && CONSP (XCDR (it->font_height))
4692 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4694 /* `(+ N)' or `(- N)' where N is an integer. */
4695 int steps = XINT (XCAR (XCDR (it->font_height)));
4696 if (EQ (XCAR (it->font_height), Qplus))
4697 steps = - steps;
4698 it->face_id = smaller_face (it->f, it->face_id, steps);
4700 else if (FUNCTIONP (it->font_height))
4702 /* Call function with current height as argument.
4703 Value is the new height. */
4704 Lisp_Object height;
4705 height = safe_call1 (it->font_height,
4706 face->lface[LFACE_HEIGHT_INDEX]);
4707 if (NUMBERP (height))
4708 new_height = XFLOATINT (height);
4710 else if (NUMBERP (it->font_height))
4712 /* Value is a multiple of the canonical char height. */
4713 struct face *f;
4715 f = FACE_FROM_ID (it->f,
4716 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4717 new_height = (XFLOATINT (it->font_height)
4718 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4720 else
4722 /* Evaluate IT->font_height with `height' bound to the
4723 current specified height to get the new height. */
4724 ptrdiff_t count = SPECPDL_INDEX ();
4726 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4727 value = safe_eval (it->font_height);
4728 unbind_to (count, Qnil);
4730 if (NUMBERP (value))
4731 new_height = XFLOATINT (value);
4734 if (new_height > 0)
4735 it->face_id = face_with_height (it->f, it->face_id, new_height);
4739 return 0;
4742 /* Handle `(space-width WIDTH)'. */
4743 if (CONSP (spec)
4744 && EQ (XCAR (spec), Qspace_width)
4745 && CONSP (XCDR (spec)))
4747 if (it)
4749 if (!FRAME_WINDOW_P (it->f))
4750 return 0;
4752 value = XCAR (XCDR (spec));
4753 if (NUMBERP (value) && XFLOATINT (value) > 0)
4754 it->space_width = value;
4757 return 0;
4760 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4761 if (CONSP (spec)
4762 && EQ (XCAR (spec), Qslice))
4764 Lisp_Object tem;
4766 if (it)
4768 if (!FRAME_WINDOW_P (it->f))
4769 return 0;
4771 if (tem = XCDR (spec), CONSP (tem))
4773 it->slice.x = XCAR (tem);
4774 if (tem = XCDR (tem), CONSP (tem))
4776 it->slice.y = XCAR (tem);
4777 if (tem = XCDR (tem), CONSP (tem))
4779 it->slice.width = XCAR (tem);
4780 if (tem = XCDR (tem), CONSP (tem))
4781 it->slice.height = XCAR (tem);
4787 return 0;
4790 /* Handle `(raise FACTOR)'. */
4791 if (CONSP (spec)
4792 && EQ (XCAR (spec), Qraise)
4793 && CONSP (XCDR (spec)))
4795 if (it)
4797 if (!FRAME_WINDOW_P (it->f))
4798 return 0;
4800 #ifdef HAVE_WINDOW_SYSTEM
4801 value = XCAR (XCDR (spec));
4802 if (NUMBERP (value))
4804 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4805 it->voffset = - (XFLOATINT (value)
4806 * (FONT_HEIGHT (face->font)));
4808 #endif /* HAVE_WINDOW_SYSTEM */
4811 return 0;
4814 /* Don't handle the other kinds of display specifications
4815 inside a string that we got from a `display' property. */
4816 if (it && it->string_from_display_prop_p)
4817 return 0;
4819 /* Characters having this form of property are not displayed, so
4820 we have to find the end of the property. */
4821 if (it)
4823 start_pos = *position;
4824 *position = display_prop_end (it, object, start_pos);
4826 value = Qnil;
4828 /* Stop the scan at that end position--we assume that all
4829 text properties change there. */
4830 if (it)
4831 it->stop_charpos = position->charpos;
4833 /* Handle `(left-fringe BITMAP [FACE])'
4834 and `(right-fringe BITMAP [FACE])'. */
4835 if (CONSP (spec)
4836 && (EQ (XCAR (spec), Qleft_fringe)
4837 || EQ (XCAR (spec), Qright_fringe))
4838 && CONSP (XCDR (spec)))
4840 int fringe_bitmap;
4842 if (it)
4844 if (!FRAME_WINDOW_P (it->f))
4845 /* If we return here, POSITION has been advanced
4846 across the text with this property. */
4848 /* Synchronize the bidi iterator with POSITION. This is
4849 needed because we are not going to push the iterator
4850 on behalf of this display property, so there will be
4851 no pop_it call to do this synchronization for us. */
4852 if (it->bidi_p)
4854 it->position = *position;
4855 iterate_out_of_display_property (it);
4856 *position = it->position;
4858 return 1;
4861 else if (!frame_window_p)
4862 return 1;
4864 #ifdef HAVE_WINDOW_SYSTEM
4865 value = XCAR (XCDR (spec));
4866 if (!SYMBOLP (value)
4867 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4868 /* If we return here, POSITION has been advanced
4869 across the text with this property. */
4871 if (it && it->bidi_p)
4873 it->position = *position;
4874 iterate_out_of_display_property (it);
4875 *position = it->position;
4877 return 1;
4880 if (it)
4882 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4884 if (CONSP (XCDR (XCDR (spec))))
4886 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4887 int face_id2 = lookup_derived_face (it->f, face_name,
4888 FRINGE_FACE_ID, 0);
4889 if (face_id2 >= 0)
4890 face_id = face_id2;
4893 /* Save current settings of IT so that we can restore them
4894 when we are finished with the glyph property value. */
4895 push_it (it, position);
4897 it->area = TEXT_AREA;
4898 it->what = IT_IMAGE;
4899 it->image_id = -1; /* no image */
4900 it->position = start_pos;
4901 it->object = NILP (object) ? it->w->contents : object;
4902 it->method = GET_FROM_IMAGE;
4903 it->from_overlay = Qnil;
4904 it->face_id = face_id;
4905 it->from_disp_prop_p = 1;
4907 /* Say that we haven't consumed the characters with
4908 `display' property yet. The call to pop_it in
4909 set_iterator_to_next will clean this up. */
4910 *position = start_pos;
4912 if (EQ (XCAR (spec), Qleft_fringe))
4914 it->left_user_fringe_bitmap = fringe_bitmap;
4915 it->left_user_fringe_face_id = face_id;
4917 else
4919 it->right_user_fringe_bitmap = fringe_bitmap;
4920 it->right_user_fringe_face_id = face_id;
4923 #endif /* HAVE_WINDOW_SYSTEM */
4924 return 1;
4927 /* Prepare to handle `((margin left-margin) ...)',
4928 `((margin right-margin) ...)' and `((margin nil) ...)'
4929 prefixes for display specifications. */
4930 location = Qunbound;
4931 if (CONSP (spec) && CONSP (XCAR (spec)))
4933 Lisp_Object tem;
4935 value = XCDR (spec);
4936 if (CONSP (value))
4937 value = XCAR (value);
4939 tem = XCAR (spec);
4940 if (EQ (XCAR (tem), Qmargin)
4941 && (tem = XCDR (tem),
4942 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4943 (NILP (tem)
4944 || EQ (tem, Qleft_margin)
4945 || EQ (tem, Qright_margin))))
4946 location = tem;
4949 if (EQ (location, Qunbound))
4951 location = Qnil;
4952 value = spec;
4955 /* After this point, VALUE is the property after any
4956 margin prefix has been stripped. It must be a string,
4957 an image specification, or `(space ...)'.
4959 LOCATION specifies where to display: `left-margin',
4960 `right-margin' or nil. */
4962 valid_p = (STRINGP (value)
4963 #ifdef HAVE_WINDOW_SYSTEM
4964 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4965 && valid_image_p (value))
4966 #endif /* not HAVE_WINDOW_SYSTEM */
4967 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4969 if (valid_p && !display_replaced_p)
4971 int retval = 1;
4973 if (!it)
4975 /* Callers need to know whether the display spec is any kind
4976 of `(space ...)' spec that is about to affect text-area
4977 display. */
4978 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4979 retval = 2;
4980 return retval;
4983 /* Save current settings of IT so that we can restore them
4984 when we are finished with the glyph property value. */
4985 push_it (it, position);
4986 it->from_overlay = overlay;
4987 it->from_disp_prop_p = 1;
4989 if (NILP (location))
4990 it->area = TEXT_AREA;
4991 else if (EQ (location, Qleft_margin))
4992 it->area = LEFT_MARGIN_AREA;
4993 else
4994 it->area = RIGHT_MARGIN_AREA;
4996 if (STRINGP (value))
4998 it->string = value;
4999 it->multibyte_p = STRING_MULTIBYTE (it->string);
5000 it->current.overlay_string_index = -1;
5001 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5002 it->end_charpos = it->string_nchars = SCHARS (it->string);
5003 it->method = GET_FROM_STRING;
5004 it->stop_charpos = 0;
5005 it->prev_stop = 0;
5006 it->base_level_stop = 0;
5007 it->string_from_display_prop_p = 1;
5008 /* Say that we haven't consumed the characters with
5009 `display' property yet. The call to pop_it in
5010 set_iterator_to_next will clean this up. */
5011 if (BUFFERP (object))
5012 *position = start_pos;
5014 /* Force paragraph direction to be that of the parent
5015 object. If the parent object's paragraph direction is
5016 not yet determined, default to L2R. */
5017 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5018 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5019 else
5020 it->paragraph_embedding = L2R;
5022 /* Set up the bidi iterator for this display string. */
5023 if (it->bidi_p)
5025 it->bidi_it.string.lstring = it->string;
5026 it->bidi_it.string.s = NULL;
5027 it->bidi_it.string.schars = it->end_charpos;
5028 it->bidi_it.string.bufpos = bufpos;
5029 it->bidi_it.string.from_disp_str = 1;
5030 it->bidi_it.string.unibyte = !it->multibyte_p;
5031 it->bidi_it.w = it->w;
5032 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5035 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5037 it->method = GET_FROM_STRETCH;
5038 it->object = value;
5039 *position = it->position = start_pos;
5040 retval = 1 + (it->area == TEXT_AREA);
5042 #ifdef HAVE_WINDOW_SYSTEM
5043 else
5045 it->what = IT_IMAGE;
5046 it->image_id = lookup_image (it->f, value);
5047 it->position = start_pos;
5048 it->object = NILP (object) ? it->w->contents : object;
5049 it->method = GET_FROM_IMAGE;
5051 /* Say that we haven't consumed the characters with
5052 `display' property yet. The call to pop_it in
5053 set_iterator_to_next will clean this up. */
5054 *position = start_pos;
5056 #endif /* HAVE_WINDOW_SYSTEM */
5058 return retval;
5061 /* Invalid property or property not supported. Restore
5062 POSITION to what it was before. */
5063 *position = start_pos;
5064 return 0;
5067 /* Check if PROP is a display property value whose text should be
5068 treated as intangible. OVERLAY is the overlay from which PROP
5069 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5070 specify the buffer position covered by PROP. */
5073 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5074 ptrdiff_t charpos, ptrdiff_t bytepos)
5076 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5077 struct text_pos position;
5079 SET_TEXT_POS (position, charpos, bytepos);
5080 return handle_display_spec (NULL, prop, Qnil, overlay,
5081 &position, charpos, frame_window_p);
5085 /* Return 1 if PROP is a display sub-property value containing STRING.
5087 Implementation note: this and the following function are really
5088 special cases of handle_display_spec and
5089 handle_single_display_spec, and should ideally use the same code.
5090 Until they do, these two pairs must be consistent and must be
5091 modified in sync. */
5093 static int
5094 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5096 if (EQ (string, prop))
5097 return 1;
5099 /* Skip over `when FORM'. */
5100 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5102 prop = XCDR (prop);
5103 if (!CONSP (prop))
5104 return 0;
5105 /* Actually, the condition following `when' should be eval'ed,
5106 like handle_single_display_spec does, and we should return
5107 zero if it evaluates to nil. However, this function is
5108 called only when the buffer was already displayed and some
5109 glyph in the glyph matrix was found to come from a display
5110 string. Therefore, the condition was already evaluated, and
5111 the result was non-nil, otherwise the display string wouldn't
5112 have been displayed and we would have never been called for
5113 this property. Thus, we can skip the evaluation and assume
5114 its result is non-nil. */
5115 prop = XCDR (prop);
5118 if (CONSP (prop))
5119 /* Skip over `margin LOCATION'. */
5120 if (EQ (XCAR (prop), Qmargin))
5122 prop = XCDR (prop);
5123 if (!CONSP (prop))
5124 return 0;
5126 prop = XCDR (prop);
5127 if (!CONSP (prop))
5128 return 0;
5131 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5135 /* Return 1 if STRING appears in the `display' property PROP. */
5137 static int
5138 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5140 if (CONSP (prop)
5141 && !EQ (XCAR (prop), Qwhen)
5142 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5144 /* A list of sub-properties. */
5145 while (CONSP (prop))
5147 if (single_display_spec_string_p (XCAR (prop), string))
5148 return 1;
5149 prop = XCDR (prop);
5152 else if (VECTORP (prop))
5154 /* A vector of sub-properties. */
5155 ptrdiff_t i;
5156 for (i = 0; i < ASIZE (prop); ++i)
5157 if (single_display_spec_string_p (AREF (prop, i), string))
5158 return 1;
5160 else
5161 return single_display_spec_string_p (prop, string);
5163 return 0;
5166 /* Look for STRING in overlays and text properties in the current
5167 buffer, between character positions FROM and TO (excluding TO).
5168 BACK_P non-zero means look back (in this case, TO is supposed to be
5169 less than FROM).
5170 Value is the first character position where STRING was found, or
5171 zero if it wasn't found before hitting TO.
5173 This function may only use code that doesn't eval because it is
5174 called asynchronously from note_mouse_highlight. */
5176 static ptrdiff_t
5177 string_buffer_position_lim (Lisp_Object string,
5178 ptrdiff_t from, ptrdiff_t to, int back_p)
5180 Lisp_Object limit, prop, pos;
5181 int found = 0;
5183 pos = make_number (max (from, BEGV));
5185 if (!back_p) /* looking forward */
5187 limit = make_number (min (to, ZV));
5188 while (!found && !EQ (pos, limit))
5190 prop = Fget_char_property (pos, Qdisplay, Qnil);
5191 if (!NILP (prop) && display_prop_string_p (prop, string))
5192 found = 1;
5193 else
5194 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5195 limit);
5198 else /* looking back */
5200 limit = make_number (max (to, BEGV));
5201 while (!found && !EQ (pos, limit))
5203 prop = Fget_char_property (pos, Qdisplay, Qnil);
5204 if (!NILP (prop) && display_prop_string_p (prop, string))
5205 found = 1;
5206 else
5207 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5208 limit);
5212 return found ? XINT (pos) : 0;
5215 /* Determine which buffer position in current buffer STRING comes from.
5216 AROUND_CHARPOS is an approximate position where it could come from.
5217 Value is the buffer position or 0 if it couldn't be determined.
5219 This function is necessary because we don't record buffer positions
5220 in glyphs generated from strings (to keep struct glyph small).
5221 This function may only use code that doesn't eval because it is
5222 called asynchronously from note_mouse_highlight. */
5224 static ptrdiff_t
5225 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5227 const int MAX_DISTANCE = 1000;
5228 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5229 around_charpos + MAX_DISTANCE,
5232 if (!found)
5233 found = string_buffer_position_lim (string, around_charpos,
5234 around_charpos - MAX_DISTANCE, 1);
5235 return found;
5240 /***********************************************************************
5241 `composition' property
5242 ***********************************************************************/
5244 /* Set up iterator IT from `composition' property at its current
5245 position. Called from handle_stop. */
5247 static enum prop_handled
5248 handle_composition_prop (struct it *it)
5250 Lisp_Object prop, string;
5251 ptrdiff_t pos, pos_byte, start, end;
5253 if (STRINGP (it->string))
5255 unsigned char *s;
5257 pos = IT_STRING_CHARPOS (*it);
5258 pos_byte = IT_STRING_BYTEPOS (*it);
5259 string = it->string;
5260 s = SDATA (string) + pos_byte;
5261 it->c = STRING_CHAR (s);
5263 else
5265 pos = IT_CHARPOS (*it);
5266 pos_byte = IT_BYTEPOS (*it);
5267 string = Qnil;
5268 it->c = FETCH_CHAR (pos_byte);
5271 /* If there's a valid composition and point is not inside of the
5272 composition (in the case that the composition is from the current
5273 buffer), draw a glyph composed from the composition components. */
5274 if (find_composition (pos, -1, &start, &end, &prop, string)
5275 && composition_valid_p (start, end, prop)
5276 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5278 if (start < pos)
5279 /* As we can't handle this situation (perhaps font-lock added
5280 a new composition), we just return here hoping that next
5281 redisplay will detect this composition much earlier. */
5282 return HANDLED_NORMALLY;
5283 if (start != pos)
5285 if (STRINGP (it->string))
5286 pos_byte = string_char_to_byte (it->string, start);
5287 else
5288 pos_byte = CHAR_TO_BYTE (start);
5290 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5291 prop, string);
5293 if (it->cmp_it.id >= 0)
5295 it->cmp_it.ch = -1;
5296 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5297 it->cmp_it.nglyphs = -1;
5301 return HANDLED_NORMALLY;
5306 /***********************************************************************
5307 Overlay strings
5308 ***********************************************************************/
5310 /* The following structure is used to record overlay strings for
5311 later sorting in load_overlay_strings. */
5313 struct overlay_entry
5315 Lisp_Object overlay;
5316 Lisp_Object string;
5317 EMACS_INT priority;
5318 int after_string_p;
5322 /* Set up iterator IT from overlay strings at its current position.
5323 Called from handle_stop. */
5325 static enum prop_handled
5326 handle_overlay_change (struct it *it)
5328 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5329 return HANDLED_RECOMPUTE_PROPS;
5330 else
5331 return HANDLED_NORMALLY;
5335 /* Set up the next overlay string for delivery by IT, if there is an
5336 overlay string to deliver. Called by set_iterator_to_next when the
5337 end of the current overlay string is reached. If there are more
5338 overlay strings to display, IT->string and
5339 IT->current.overlay_string_index are set appropriately here.
5340 Otherwise IT->string is set to nil. */
5342 static void
5343 next_overlay_string (struct it *it)
5345 ++it->current.overlay_string_index;
5346 if (it->current.overlay_string_index == it->n_overlay_strings)
5348 /* No more overlay strings. Restore IT's settings to what
5349 they were before overlay strings were processed, and
5350 continue to deliver from current_buffer. */
5352 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5353 pop_it (it);
5354 eassert (it->sp > 0
5355 || (NILP (it->string)
5356 && it->method == GET_FROM_BUFFER
5357 && it->stop_charpos >= BEGV
5358 && it->stop_charpos <= it->end_charpos));
5359 it->current.overlay_string_index = -1;
5360 it->n_overlay_strings = 0;
5361 it->overlay_strings_charpos = -1;
5362 /* If there's an empty display string on the stack, pop the
5363 stack, to resync the bidi iterator with IT's position. Such
5364 empty strings are pushed onto the stack in
5365 get_overlay_strings_1. */
5366 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5367 pop_it (it);
5369 /* If we're at the end of the buffer, record that we have
5370 processed the overlay strings there already, so that
5371 next_element_from_buffer doesn't try it again. */
5372 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5373 it->overlay_strings_at_end_processed_p = 1;
5375 else
5377 /* There are more overlay strings to process. If
5378 IT->current.overlay_string_index has advanced to a position
5379 where we must load IT->overlay_strings with more strings, do
5380 it. We must load at the IT->overlay_strings_charpos where
5381 IT->n_overlay_strings was originally computed; when invisible
5382 text is present, this might not be IT_CHARPOS (Bug#7016). */
5383 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5385 if (it->current.overlay_string_index && i == 0)
5386 load_overlay_strings (it, it->overlay_strings_charpos);
5388 /* Initialize IT to deliver display elements from the overlay
5389 string. */
5390 it->string = it->overlay_strings[i];
5391 it->multibyte_p = STRING_MULTIBYTE (it->string);
5392 SET_TEXT_POS (it->current.string_pos, 0, 0);
5393 it->method = GET_FROM_STRING;
5394 it->stop_charpos = 0;
5395 it->end_charpos = SCHARS (it->string);
5396 if (it->cmp_it.stop_pos >= 0)
5397 it->cmp_it.stop_pos = 0;
5398 it->prev_stop = 0;
5399 it->base_level_stop = 0;
5401 /* Set up the bidi iterator for this overlay string. */
5402 if (it->bidi_p)
5404 it->bidi_it.string.lstring = it->string;
5405 it->bidi_it.string.s = NULL;
5406 it->bidi_it.string.schars = SCHARS (it->string);
5407 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5408 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5409 it->bidi_it.string.unibyte = !it->multibyte_p;
5410 it->bidi_it.w = it->w;
5411 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5415 CHECK_IT (it);
5419 /* Compare two overlay_entry structures E1 and E2. Used as a
5420 comparison function for qsort in load_overlay_strings. Overlay
5421 strings for the same position are sorted so that
5423 1. All after-strings come in front of before-strings, except
5424 when they come from the same overlay.
5426 2. Within after-strings, strings are sorted so that overlay strings
5427 from overlays with higher priorities come first.
5429 2. Within before-strings, strings are sorted so that overlay
5430 strings from overlays with higher priorities come last.
5432 Value is analogous to strcmp. */
5435 static int
5436 compare_overlay_entries (const void *e1, const void *e2)
5438 struct overlay_entry const *entry1 = e1;
5439 struct overlay_entry const *entry2 = e2;
5440 int result;
5442 if (entry1->after_string_p != entry2->after_string_p)
5444 /* Let after-strings appear in front of before-strings if
5445 they come from different overlays. */
5446 if (EQ (entry1->overlay, entry2->overlay))
5447 result = entry1->after_string_p ? 1 : -1;
5448 else
5449 result = entry1->after_string_p ? -1 : 1;
5451 else if (entry1->priority != entry2->priority)
5453 if (entry1->after_string_p)
5454 /* After-strings sorted in order of decreasing priority. */
5455 result = entry2->priority < entry1->priority ? -1 : 1;
5456 else
5457 /* Before-strings sorted in order of increasing priority. */
5458 result = entry1->priority < entry2->priority ? -1 : 1;
5460 else
5461 result = 0;
5463 return result;
5467 /* Load the vector IT->overlay_strings with overlay strings from IT's
5468 current buffer position, or from CHARPOS if that is > 0. Set
5469 IT->n_overlays to the total number of overlay strings found.
5471 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5472 a time. On entry into load_overlay_strings,
5473 IT->current.overlay_string_index gives the number of overlay
5474 strings that have already been loaded by previous calls to this
5475 function.
5477 IT->add_overlay_start contains an additional overlay start
5478 position to consider for taking overlay strings from, if non-zero.
5479 This position comes into play when the overlay has an `invisible'
5480 property, and both before and after-strings. When we've skipped to
5481 the end of the overlay, because of its `invisible' property, we
5482 nevertheless want its before-string to appear.
5483 IT->add_overlay_start will contain the overlay start position
5484 in this case.
5486 Overlay strings are sorted so that after-string strings come in
5487 front of before-string strings. Within before and after-strings,
5488 strings are sorted by overlay priority. See also function
5489 compare_overlay_entries. */
5491 static void
5492 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5494 Lisp_Object overlay, window, str, invisible;
5495 struct Lisp_Overlay *ov;
5496 ptrdiff_t start, end;
5497 ptrdiff_t size = 20;
5498 ptrdiff_t n = 0, i, j;
5499 int invis_p;
5500 struct overlay_entry *entries = alloca (size * sizeof *entries);
5501 USE_SAFE_ALLOCA;
5503 if (charpos <= 0)
5504 charpos = IT_CHARPOS (*it);
5506 /* Append the overlay string STRING of overlay OVERLAY to vector
5507 `entries' which has size `size' and currently contains `n'
5508 elements. AFTER_P non-zero means STRING is an after-string of
5509 OVERLAY. */
5510 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5511 do \
5513 Lisp_Object priority; \
5515 if (n == size) \
5517 struct overlay_entry *old = entries; \
5518 SAFE_NALLOCA (entries, 2, size); \
5519 memcpy (entries, old, size * sizeof *entries); \
5520 size *= 2; \
5523 entries[n].string = (STRING); \
5524 entries[n].overlay = (OVERLAY); \
5525 priority = Foverlay_get ((OVERLAY), Qpriority); \
5526 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5527 entries[n].after_string_p = (AFTER_P); \
5528 ++n; \
5530 while (0)
5532 /* Process overlay before the overlay center. */
5533 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5535 XSETMISC (overlay, ov);
5536 eassert (OVERLAYP (overlay));
5537 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5538 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5540 if (end < charpos)
5541 break;
5543 /* Skip this overlay if it doesn't start or end at IT's current
5544 position. */
5545 if (end != charpos && start != charpos)
5546 continue;
5548 /* Skip this overlay if it doesn't apply to IT->w. */
5549 window = Foverlay_get (overlay, Qwindow);
5550 if (WINDOWP (window) && XWINDOW (window) != it->w)
5551 continue;
5553 /* If the text ``under'' the overlay is invisible, both before-
5554 and after-strings from this overlay are visible; start and
5555 end position are indistinguishable. */
5556 invisible = Foverlay_get (overlay, Qinvisible);
5557 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5559 /* If overlay has a non-empty before-string, record it. */
5560 if ((start == charpos || (end == charpos && invis_p))
5561 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5562 && SCHARS (str))
5563 RECORD_OVERLAY_STRING (overlay, str, 0);
5565 /* If overlay has a non-empty after-string, record it. */
5566 if ((end == charpos || (start == charpos && invis_p))
5567 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5568 && SCHARS (str))
5569 RECORD_OVERLAY_STRING (overlay, str, 1);
5572 /* Process overlays after the overlay center. */
5573 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5575 XSETMISC (overlay, ov);
5576 eassert (OVERLAYP (overlay));
5577 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5578 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5580 if (start > charpos)
5581 break;
5583 /* Skip this overlay if it doesn't start or end at IT's current
5584 position. */
5585 if (end != charpos && start != charpos)
5586 continue;
5588 /* Skip this overlay if it doesn't apply to IT->w. */
5589 window = Foverlay_get (overlay, Qwindow);
5590 if (WINDOWP (window) && XWINDOW (window) != it->w)
5591 continue;
5593 /* If the text ``under'' the overlay is invisible, it has a zero
5594 dimension, and both before- and after-strings apply. */
5595 invisible = Foverlay_get (overlay, Qinvisible);
5596 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5598 /* If overlay has a non-empty before-string, record it. */
5599 if ((start == charpos || (end == charpos && invis_p))
5600 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5601 && SCHARS (str))
5602 RECORD_OVERLAY_STRING (overlay, str, 0);
5604 /* If overlay has a non-empty after-string, record it. */
5605 if ((end == charpos || (start == charpos && invis_p))
5606 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5607 && SCHARS (str))
5608 RECORD_OVERLAY_STRING (overlay, str, 1);
5611 #undef RECORD_OVERLAY_STRING
5613 /* Sort entries. */
5614 if (n > 1)
5615 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5617 /* Record number of overlay strings, and where we computed it. */
5618 it->n_overlay_strings = n;
5619 it->overlay_strings_charpos = charpos;
5621 /* IT->current.overlay_string_index is the number of overlay strings
5622 that have already been consumed by IT. Copy some of the
5623 remaining overlay strings to IT->overlay_strings. */
5624 i = 0;
5625 j = it->current.overlay_string_index;
5626 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5628 it->overlay_strings[i] = entries[j].string;
5629 it->string_overlays[i++] = entries[j++].overlay;
5632 CHECK_IT (it);
5633 SAFE_FREE ();
5637 /* Get the first chunk of overlay strings at IT's current buffer
5638 position, or at CHARPOS if that is > 0. Value is non-zero if at
5639 least one overlay string was found. */
5641 static int
5642 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5644 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5645 process. This fills IT->overlay_strings with strings, and sets
5646 IT->n_overlay_strings to the total number of strings to process.
5647 IT->pos.overlay_string_index has to be set temporarily to zero
5648 because load_overlay_strings needs this; it must be set to -1
5649 when no overlay strings are found because a zero value would
5650 indicate a position in the first overlay string. */
5651 it->current.overlay_string_index = 0;
5652 load_overlay_strings (it, charpos);
5654 /* If we found overlay strings, set up IT to deliver display
5655 elements from the first one. Otherwise set up IT to deliver
5656 from current_buffer. */
5657 if (it->n_overlay_strings)
5659 /* Make sure we know settings in current_buffer, so that we can
5660 restore meaningful values when we're done with the overlay
5661 strings. */
5662 if (compute_stop_p)
5663 compute_stop_pos (it);
5664 eassert (it->face_id >= 0);
5666 /* Save IT's settings. They are restored after all overlay
5667 strings have been processed. */
5668 eassert (!compute_stop_p || it->sp == 0);
5670 /* When called from handle_stop, there might be an empty display
5671 string loaded. In that case, don't bother saving it. But
5672 don't use this optimization with the bidi iterator, since we
5673 need the corresponding pop_it call to resync the bidi
5674 iterator's position with IT's position, after we are done
5675 with the overlay strings. (The corresponding call to pop_it
5676 in case of an empty display string is in
5677 next_overlay_string.) */
5678 if (!(!it->bidi_p
5679 && STRINGP (it->string) && !SCHARS (it->string)))
5680 push_it (it, NULL);
5682 /* Set up IT to deliver display elements from the first overlay
5683 string. */
5684 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5685 it->string = it->overlay_strings[0];
5686 it->from_overlay = Qnil;
5687 it->stop_charpos = 0;
5688 eassert (STRINGP (it->string));
5689 it->end_charpos = SCHARS (it->string);
5690 it->prev_stop = 0;
5691 it->base_level_stop = 0;
5692 it->multibyte_p = STRING_MULTIBYTE (it->string);
5693 it->method = GET_FROM_STRING;
5694 it->from_disp_prop_p = 0;
5696 /* Force paragraph direction to be that of the parent
5697 buffer. */
5698 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5699 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5700 else
5701 it->paragraph_embedding = L2R;
5703 /* Set up the bidi iterator for this overlay string. */
5704 if (it->bidi_p)
5706 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5708 it->bidi_it.string.lstring = it->string;
5709 it->bidi_it.string.s = NULL;
5710 it->bidi_it.string.schars = SCHARS (it->string);
5711 it->bidi_it.string.bufpos = pos;
5712 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5713 it->bidi_it.string.unibyte = !it->multibyte_p;
5714 it->bidi_it.w = it->w;
5715 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5717 return 1;
5720 it->current.overlay_string_index = -1;
5721 return 0;
5724 static int
5725 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5727 it->string = Qnil;
5728 it->method = GET_FROM_BUFFER;
5730 (void) get_overlay_strings_1 (it, charpos, 1);
5732 CHECK_IT (it);
5734 /* Value is non-zero if we found at least one overlay string. */
5735 return STRINGP (it->string);
5740 /***********************************************************************
5741 Saving and restoring state
5742 ***********************************************************************/
5744 /* Save current settings of IT on IT->stack. Called, for example,
5745 before setting up IT for an overlay string, to be able to restore
5746 IT's settings to what they were after the overlay string has been
5747 processed. If POSITION is non-NULL, it is the position to save on
5748 the stack instead of IT->position. */
5750 static void
5751 push_it (struct it *it, struct text_pos *position)
5753 struct iterator_stack_entry *p;
5755 eassert (it->sp < IT_STACK_SIZE);
5756 p = it->stack + it->sp;
5758 p->stop_charpos = it->stop_charpos;
5759 p->prev_stop = it->prev_stop;
5760 p->base_level_stop = it->base_level_stop;
5761 p->cmp_it = it->cmp_it;
5762 eassert (it->face_id >= 0);
5763 p->face_id = it->face_id;
5764 p->string = it->string;
5765 p->method = it->method;
5766 p->from_overlay = it->from_overlay;
5767 switch (p->method)
5769 case GET_FROM_IMAGE:
5770 p->u.image.object = it->object;
5771 p->u.image.image_id = it->image_id;
5772 p->u.image.slice = it->slice;
5773 break;
5774 case GET_FROM_STRETCH:
5775 p->u.stretch.object = it->object;
5776 break;
5778 p->position = position ? *position : it->position;
5779 p->current = it->current;
5780 p->end_charpos = it->end_charpos;
5781 p->string_nchars = it->string_nchars;
5782 p->area = it->area;
5783 p->multibyte_p = it->multibyte_p;
5784 p->avoid_cursor_p = it->avoid_cursor_p;
5785 p->space_width = it->space_width;
5786 p->font_height = it->font_height;
5787 p->voffset = it->voffset;
5788 p->string_from_display_prop_p = it->string_from_display_prop_p;
5789 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5790 p->display_ellipsis_p = 0;
5791 p->line_wrap = it->line_wrap;
5792 p->bidi_p = it->bidi_p;
5793 p->paragraph_embedding = it->paragraph_embedding;
5794 p->from_disp_prop_p = it->from_disp_prop_p;
5795 ++it->sp;
5797 /* Save the state of the bidi iterator as well. */
5798 if (it->bidi_p)
5799 bidi_push_it (&it->bidi_it);
5802 static void
5803 iterate_out_of_display_property (struct it *it)
5805 int buffer_p = !STRINGP (it->string);
5806 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5807 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5809 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5811 /* Maybe initialize paragraph direction. If we are at the beginning
5812 of a new paragraph, next_element_from_buffer may not have a
5813 chance to do that. */
5814 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5815 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5816 /* prev_stop can be zero, so check against BEGV as well. */
5817 while (it->bidi_it.charpos >= bob
5818 && it->prev_stop <= it->bidi_it.charpos
5819 && it->bidi_it.charpos < CHARPOS (it->position)
5820 && it->bidi_it.charpos < eob)
5821 bidi_move_to_visually_next (&it->bidi_it);
5822 /* Record the stop_pos we just crossed, for when we cross it
5823 back, maybe. */
5824 if (it->bidi_it.charpos > CHARPOS (it->position))
5825 it->prev_stop = CHARPOS (it->position);
5826 /* If we ended up not where pop_it put us, resync IT's
5827 positional members with the bidi iterator. */
5828 if (it->bidi_it.charpos != CHARPOS (it->position))
5829 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5830 if (buffer_p)
5831 it->current.pos = it->position;
5832 else
5833 it->current.string_pos = it->position;
5836 /* Restore IT's settings from IT->stack. Called, for example, when no
5837 more overlay strings must be processed, and we return to delivering
5838 display elements from a buffer, or when the end of a string from a
5839 `display' property is reached and we return to delivering display
5840 elements from an overlay string, or from a buffer. */
5842 static void
5843 pop_it (struct it *it)
5845 struct iterator_stack_entry *p;
5846 int from_display_prop = it->from_disp_prop_p;
5848 eassert (it->sp > 0);
5849 --it->sp;
5850 p = it->stack + it->sp;
5851 it->stop_charpos = p->stop_charpos;
5852 it->prev_stop = p->prev_stop;
5853 it->base_level_stop = p->base_level_stop;
5854 it->cmp_it = p->cmp_it;
5855 it->face_id = p->face_id;
5856 it->current = p->current;
5857 it->position = p->position;
5858 it->string = p->string;
5859 it->from_overlay = p->from_overlay;
5860 if (NILP (it->string))
5861 SET_TEXT_POS (it->current.string_pos, -1, -1);
5862 it->method = p->method;
5863 switch (it->method)
5865 case GET_FROM_IMAGE:
5866 it->image_id = p->u.image.image_id;
5867 it->object = p->u.image.object;
5868 it->slice = p->u.image.slice;
5869 break;
5870 case GET_FROM_STRETCH:
5871 it->object = p->u.stretch.object;
5872 break;
5873 case GET_FROM_BUFFER:
5874 it->object = it->w->contents;
5875 break;
5876 case GET_FROM_STRING:
5877 it->object = it->string;
5878 break;
5879 case GET_FROM_DISPLAY_VECTOR:
5880 if (it->s)
5881 it->method = GET_FROM_C_STRING;
5882 else if (STRINGP (it->string))
5883 it->method = GET_FROM_STRING;
5884 else
5886 it->method = GET_FROM_BUFFER;
5887 it->object = it->w->contents;
5890 it->end_charpos = p->end_charpos;
5891 it->string_nchars = p->string_nchars;
5892 it->area = p->area;
5893 it->multibyte_p = p->multibyte_p;
5894 it->avoid_cursor_p = p->avoid_cursor_p;
5895 it->space_width = p->space_width;
5896 it->font_height = p->font_height;
5897 it->voffset = p->voffset;
5898 it->string_from_display_prop_p = p->string_from_display_prop_p;
5899 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5900 it->line_wrap = p->line_wrap;
5901 it->bidi_p = p->bidi_p;
5902 it->paragraph_embedding = p->paragraph_embedding;
5903 it->from_disp_prop_p = p->from_disp_prop_p;
5904 if (it->bidi_p)
5906 bidi_pop_it (&it->bidi_it);
5907 /* Bidi-iterate until we get out of the portion of text, if any,
5908 covered by a `display' text property or by an overlay with
5909 `display' property. (We cannot just jump there, because the
5910 internal coherency of the bidi iterator state can not be
5911 preserved across such jumps.) We also must determine the
5912 paragraph base direction if the overlay we just processed is
5913 at the beginning of a new paragraph. */
5914 if (from_display_prop
5915 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5916 iterate_out_of_display_property (it);
5918 eassert ((BUFFERP (it->object)
5919 && IT_CHARPOS (*it) == it->bidi_it.charpos
5920 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5921 || (STRINGP (it->object)
5922 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5923 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5924 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5930 /***********************************************************************
5931 Moving over lines
5932 ***********************************************************************/
5934 /* Set IT's current position to the previous line start. */
5936 static void
5937 back_to_previous_line_start (struct it *it)
5939 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
5941 DEC_BOTH (cp, bp);
5942 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
5946 /* Move IT to the next line start.
5948 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5949 we skipped over part of the text (as opposed to moving the iterator
5950 continuously over the text). Otherwise, don't change the value
5951 of *SKIPPED_P.
5953 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5954 iterator on the newline, if it was found.
5956 Newlines may come from buffer text, overlay strings, or strings
5957 displayed via the `display' property. That's the reason we can't
5958 simply use find_newline_no_quit.
5960 Note that this function may not skip over invisible text that is so
5961 because of text properties and immediately follows a newline. If
5962 it would, function reseat_at_next_visible_line_start, when called
5963 from set_iterator_to_next, would effectively make invisible
5964 characters following a newline part of the wrong glyph row, which
5965 leads to wrong cursor motion. */
5967 static int
5968 forward_to_next_line_start (struct it *it, int *skipped_p,
5969 struct bidi_it *bidi_it_prev)
5971 ptrdiff_t old_selective;
5972 int newline_found_p, n;
5973 const int MAX_NEWLINE_DISTANCE = 500;
5975 /* If already on a newline, just consume it to avoid unintended
5976 skipping over invisible text below. */
5977 if (it->what == IT_CHARACTER
5978 && it->c == '\n'
5979 && CHARPOS (it->position) == IT_CHARPOS (*it))
5981 if (it->bidi_p && bidi_it_prev)
5982 *bidi_it_prev = it->bidi_it;
5983 set_iterator_to_next (it, 0);
5984 it->c = 0;
5985 return 1;
5988 /* Don't handle selective display in the following. It's (a)
5989 unnecessary because it's done by the caller, and (b) leads to an
5990 infinite recursion because next_element_from_ellipsis indirectly
5991 calls this function. */
5992 old_selective = it->selective;
5993 it->selective = 0;
5995 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5996 from buffer text. */
5997 for (n = newline_found_p = 0;
5998 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5999 n += STRINGP (it->string) ? 0 : 1)
6001 if (!get_next_display_element (it))
6002 return 0;
6003 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6004 if (newline_found_p && it->bidi_p && bidi_it_prev)
6005 *bidi_it_prev = it->bidi_it;
6006 set_iterator_to_next (it, 0);
6009 /* If we didn't find a newline near enough, see if we can use a
6010 short-cut. */
6011 if (!newline_found_p)
6013 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6014 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6015 1, &bytepos);
6016 Lisp_Object pos;
6018 eassert (!STRINGP (it->string));
6020 /* If there isn't any `display' property in sight, and no
6021 overlays, we can just use the position of the newline in
6022 buffer text. */
6023 if (it->stop_charpos >= limit
6024 || ((pos = Fnext_single_property_change (make_number (start),
6025 Qdisplay, Qnil,
6026 make_number (limit)),
6027 NILP (pos))
6028 && next_overlay_change (start) == ZV))
6030 if (!it->bidi_p)
6032 IT_CHARPOS (*it) = limit;
6033 IT_BYTEPOS (*it) = bytepos;
6035 else
6037 struct bidi_it bprev;
6039 /* Help bidi.c avoid expensive searches for display
6040 properties and overlays, by telling it that there are
6041 none up to `limit'. */
6042 if (it->bidi_it.disp_pos < limit)
6044 it->bidi_it.disp_pos = limit;
6045 it->bidi_it.disp_prop = 0;
6047 do {
6048 bprev = it->bidi_it;
6049 bidi_move_to_visually_next (&it->bidi_it);
6050 } while (it->bidi_it.charpos != limit);
6051 IT_CHARPOS (*it) = limit;
6052 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6053 if (bidi_it_prev)
6054 *bidi_it_prev = bprev;
6056 *skipped_p = newline_found_p = 1;
6058 else
6060 while (get_next_display_element (it)
6061 && !newline_found_p)
6063 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6064 if (newline_found_p && it->bidi_p && bidi_it_prev)
6065 *bidi_it_prev = it->bidi_it;
6066 set_iterator_to_next (it, 0);
6071 it->selective = old_selective;
6072 return newline_found_p;
6076 /* Set IT's current position to the previous visible line start. Skip
6077 invisible text that is so either due to text properties or due to
6078 selective display. Caution: this does not change IT->current_x and
6079 IT->hpos. */
6081 static void
6082 back_to_previous_visible_line_start (struct it *it)
6084 while (IT_CHARPOS (*it) > BEGV)
6086 back_to_previous_line_start (it);
6088 if (IT_CHARPOS (*it) <= BEGV)
6089 break;
6091 /* If selective > 0, then lines indented more than its value are
6092 invisible. */
6093 if (it->selective > 0
6094 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6095 it->selective))
6096 continue;
6098 /* Check the newline before point for invisibility. */
6100 Lisp_Object prop;
6101 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6102 Qinvisible, it->window);
6103 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6104 continue;
6107 if (IT_CHARPOS (*it) <= BEGV)
6108 break;
6111 struct it it2;
6112 void *it2data = NULL;
6113 ptrdiff_t pos;
6114 ptrdiff_t beg, end;
6115 Lisp_Object val, overlay;
6117 SAVE_IT (it2, *it, it2data);
6119 /* If newline is part of a composition, continue from start of composition */
6120 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6121 && beg < IT_CHARPOS (*it))
6122 goto replaced;
6124 /* If newline is replaced by a display property, find start of overlay
6125 or interval and continue search from that point. */
6126 pos = --IT_CHARPOS (it2);
6127 --IT_BYTEPOS (it2);
6128 it2.sp = 0;
6129 bidi_unshelve_cache (NULL, 0);
6130 it2.string_from_display_prop_p = 0;
6131 it2.from_disp_prop_p = 0;
6132 if (handle_display_prop (&it2) == HANDLED_RETURN
6133 && !NILP (val = get_char_property_and_overlay
6134 (make_number (pos), Qdisplay, Qnil, &overlay))
6135 && (OVERLAYP (overlay)
6136 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6137 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6139 RESTORE_IT (it, it, it2data);
6140 goto replaced;
6143 /* Newline is not replaced by anything -- so we are done. */
6144 RESTORE_IT (it, it, it2data);
6145 break;
6147 replaced:
6148 if (beg < BEGV)
6149 beg = BEGV;
6150 IT_CHARPOS (*it) = beg;
6151 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6155 it->continuation_lines_width = 0;
6157 eassert (IT_CHARPOS (*it) >= BEGV);
6158 eassert (IT_CHARPOS (*it) == BEGV
6159 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6160 CHECK_IT (it);
6164 /* Reseat iterator IT at the previous visible line start. Skip
6165 invisible text that is so either due to text properties or due to
6166 selective display. At the end, update IT's overlay information,
6167 face information etc. */
6169 void
6170 reseat_at_previous_visible_line_start (struct it *it)
6172 back_to_previous_visible_line_start (it);
6173 reseat (it, it->current.pos, 1);
6174 CHECK_IT (it);
6178 /* Reseat iterator IT on the next visible line start in the current
6179 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6180 preceding the line start. Skip over invisible text that is so
6181 because of selective display. Compute faces, overlays etc at the
6182 new position. Note that this function does not skip over text that
6183 is invisible because of text properties. */
6185 static void
6186 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6188 int newline_found_p, skipped_p = 0;
6189 struct bidi_it bidi_it_prev;
6191 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6193 /* Skip over lines that are invisible because they are indented
6194 more than the value of IT->selective. */
6195 if (it->selective > 0)
6196 while (IT_CHARPOS (*it) < ZV
6197 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6198 it->selective))
6200 eassert (IT_BYTEPOS (*it) == BEGV
6201 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6202 newline_found_p =
6203 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6206 /* Position on the newline if that's what's requested. */
6207 if (on_newline_p && newline_found_p)
6209 if (STRINGP (it->string))
6211 if (IT_STRING_CHARPOS (*it) > 0)
6213 if (!it->bidi_p)
6215 --IT_STRING_CHARPOS (*it);
6216 --IT_STRING_BYTEPOS (*it);
6218 else
6220 /* We need to restore the bidi iterator to the state
6221 it had on the newline, and resync the IT's
6222 position with that. */
6223 it->bidi_it = bidi_it_prev;
6224 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6225 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6229 else if (IT_CHARPOS (*it) > BEGV)
6231 if (!it->bidi_p)
6233 --IT_CHARPOS (*it);
6234 --IT_BYTEPOS (*it);
6236 else
6238 /* We need to restore the bidi iterator to the state it
6239 had on the newline and resync IT with that. */
6240 it->bidi_it = bidi_it_prev;
6241 IT_CHARPOS (*it) = it->bidi_it.charpos;
6242 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6244 reseat (it, it->current.pos, 0);
6247 else if (skipped_p)
6248 reseat (it, it->current.pos, 0);
6250 CHECK_IT (it);
6255 /***********************************************************************
6256 Changing an iterator's position
6257 ***********************************************************************/
6259 /* Change IT's current position to POS in current_buffer. If FORCE_P
6260 is non-zero, always check for text properties at the new position.
6261 Otherwise, text properties are only looked up if POS >=
6262 IT->check_charpos of a property. */
6264 static void
6265 reseat (struct it *it, struct text_pos pos, int force_p)
6267 ptrdiff_t original_pos = IT_CHARPOS (*it);
6269 reseat_1 (it, pos, 0);
6271 /* Determine where to check text properties. Avoid doing it
6272 where possible because text property lookup is very expensive. */
6273 if (force_p
6274 || CHARPOS (pos) > it->stop_charpos
6275 || CHARPOS (pos) < original_pos)
6277 if (it->bidi_p)
6279 /* For bidi iteration, we need to prime prev_stop and
6280 base_level_stop with our best estimations. */
6281 /* Implementation note: Of course, POS is not necessarily a
6282 stop position, so assigning prev_pos to it is a lie; we
6283 should have called compute_stop_backwards. However, if
6284 the current buffer does not include any R2L characters,
6285 that call would be a waste of cycles, because the
6286 iterator will never move back, and thus never cross this
6287 "fake" stop position. So we delay that backward search
6288 until the time we really need it, in next_element_from_buffer. */
6289 if (CHARPOS (pos) != it->prev_stop)
6290 it->prev_stop = CHARPOS (pos);
6291 if (CHARPOS (pos) < it->base_level_stop)
6292 it->base_level_stop = 0; /* meaning it's unknown */
6293 handle_stop (it);
6295 else
6297 handle_stop (it);
6298 it->prev_stop = it->base_level_stop = 0;
6303 CHECK_IT (it);
6307 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6308 IT->stop_pos to POS, also. */
6310 static void
6311 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6313 /* Don't call this function when scanning a C string. */
6314 eassert (it->s == NULL);
6316 /* POS must be a reasonable value. */
6317 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6319 it->current.pos = it->position = pos;
6320 it->end_charpos = ZV;
6321 it->dpvec = NULL;
6322 it->current.dpvec_index = -1;
6323 it->current.overlay_string_index = -1;
6324 IT_STRING_CHARPOS (*it) = -1;
6325 IT_STRING_BYTEPOS (*it) = -1;
6326 it->string = Qnil;
6327 it->method = GET_FROM_BUFFER;
6328 it->object = it->w->contents;
6329 it->area = TEXT_AREA;
6330 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6331 it->sp = 0;
6332 it->string_from_display_prop_p = 0;
6333 it->string_from_prefix_prop_p = 0;
6335 it->from_disp_prop_p = 0;
6336 it->face_before_selective_p = 0;
6337 if (it->bidi_p)
6339 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6340 &it->bidi_it);
6341 bidi_unshelve_cache (NULL, 0);
6342 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6343 it->bidi_it.string.s = NULL;
6344 it->bidi_it.string.lstring = Qnil;
6345 it->bidi_it.string.bufpos = 0;
6346 it->bidi_it.string.unibyte = 0;
6347 it->bidi_it.w = it->w;
6350 if (set_stop_p)
6352 it->stop_charpos = CHARPOS (pos);
6353 it->base_level_stop = CHARPOS (pos);
6355 /* This make the information stored in it->cmp_it invalidate. */
6356 it->cmp_it.id = -1;
6360 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6361 If S is non-null, it is a C string to iterate over. Otherwise,
6362 STRING gives a Lisp string to iterate over.
6364 If PRECISION > 0, don't return more then PRECISION number of
6365 characters from the string.
6367 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6368 characters have been returned. FIELD_WIDTH < 0 means an infinite
6369 field width.
6371 MULTIBYTE = 0 means disable processing of multibyte characters,
6372 MULTIBYTE > 0 means enable it,
6373 MULTIBYTE < 0 means use IT->multibyte_p.
6375 IT must be initialized via a prior call to init_iterator before
6376 calling this function. */
6378 static void
6379 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6380 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6381 int multibyte)
6383 /* No text property checks performed by default, but see below. */
6384 it->stop_charpos = -1;
6386 /* Set iterator position and end position. */
6387 memset (&it->current, 0, sizeof it->current);
6388 it->current.overlay_string_index = -1;
6389 it->current.dpvec_index = -1;
6390 eassert (charpos >= 0);
6392 /* If STRING is specified, use its multibyteness, otherwise use the
6393 setting of MULTIBYTE, if specified. */
6394 if (multibyte >= 0)
6395 it->multibyte_p = multibyte > 0;
6397 /* Bidirectional reordering of strings is controlled by the default
6398 value of bidi-display-reordering. Don't try to reorder while
6399 loading loadup.el, as the necessary character property tables are
6400 not yet available. */
6401 it->bidi_p =
6402 NILP (Vpurify_flag)
6403 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6405 if (s == NULL)
6407 eassert (STRINGP (string));
6408 it->string = string;
6409 it->s = NULL;
6410 it->end_charpos = it->string_nchars = SCHARS (string);
6411 it->method = GET_FROM_STRING;
6412 it->current.string_pos = string_pos (charpos, string);
6414 if (it->bidi_p)
6416 it->bidi_it.string.lstring = string;
6417 it->bidi_it.string.s = NULL;
6418 it->bidi_it.string.schars = it->end_charpos;
6419 it->bidi_it.string.bufpos = 0;
6420 it->bidi_it.string.from_disp_str = 0;
6421 it->bidi_it.string.unibyte = !it->multibyte_p;
6422 it->bidi_it.w = it->w;
6423 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6424 FRAME_WINDOW_P (it->f), &it->bidi_it);
6427 else
6429 it->s = (const unsigned char *) s;
6430 it->string = Qnil;
6432 /* Note that we use IT->current.pos, not it->current.string_pos,
6433 for displaying C strings. */
6434 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6435 if (it->multibyte_p)
6437 it->current.pos = c_string_pos (charpos, s, 1);
6438 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6440 else
6442 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6443 it->end_charpos = it->string_nchars = strlen (s);
6446 if (it->bidi_p)
6448 it->bidi_it.string.lstring = Qnil;
6449 it->bidi_it.string.s = (const unsigned char *) s;
6450 it->bidi_it.string.schars = it->end_charpos;
6451 it->bidi_it.string.bufpos = 0;
6452 it->bidi_it.string.from_disp_str = 0;
6453 it->bidi_it.string.unibyte = !it->multibyte_p;
6454 it->bidi_it.w = it->w;
6455 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6456 &it->bidi_it);
6458 it->method = GET_FROM_C_STRING;
6461 /* PRECISION > 0 means don't return more than PRECISION characters
6462 from the string. */
6463 if (precision > 0 && it->end_charpos - charpos > precision)
6465 it->end_charpos = it->string_nchars = charpos + precision;
6466 if (it->bidi_p)
6467 it->bidi_it.string.schars = it->end_charpos;
6470 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6471 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6472 FIELD_WIDTH < 0 means infinite field width. This is useful for
6473 padding with `-' at the end of a mode line. */
6474 if (field_width < 0)
6475 field_width = INFINITY;
6476 /* Implementation note: We deliberately don't enlarge
6477 it->bidi_it.string.schars here to fit it->end_charpos, because
6478 the bidi iterator cannot produce characters out of thin air. */
6479 if (field_width > it->end_charpos - charpos)
6480 it->end_charpos = charpos + field_width;
6482 /* Use the standard display table for displaying strings. */
6483 if (DISP_TABLE_P (Vstandard_display_table))
6484 it->dp = XCHAR_TABLE (Vstandard_display_table);
6486 it->stop_charpos = charpos;
6487 it->prev_stop = charpos;
6488 it->base_level_stop = 0;
6489 if (it->bidi_p)
6491 it->bidi_it.first_elt = 1;
6492 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6493 it->bidi_it.disp_pos = -1;
6495 if (s == NULL && it->multibyte_p)
6497 ptrdiff_t endpos = SCHARS (it->string);
6498 if (endpos > it->end_charpos)
6499 endpos = it->end_charpos;
6500 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6501 it->string);
6503 CHECK_IT (it);
6508 /***********************************************************************
6509 Iteration
6510 ***********************************************************************/
6512 /* Map enum it_method value to corresponding next_element_from_* function. */
6514 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6516 next_element_from_buffer,
6517 next_element_from_display_vector,
6518 next_element_from_string,
6519 next_element_from_c_string,
6520 next_element_from_image,
6521 next_element_from_stretch
6524 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6527 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6528 (possibly with the following characters). */
6530 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6531 ((IT)->cmp_it.id >= 0 \
6532 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6533 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6534 END_CHARPOS, (IT)->w, \
6535 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6536 (IT)->string)))
6539 /* Lookup the char-table Vglyphless_char_display for character C (-1
6540 if we want information for no-font case), and return the display
6541 method symbol. By side-effect, update it->what and
6542 it->glyphless_method. This function is called from
6543 get_next_display_element for each character element, and from
6544 x_produce_glyphs when no suitable font was found. */
6546 Lisp_Object
6547 lookup_glyphless_char_display (int c, struct it *it)
6549 Lisp_Object glyphless_method = Qnil;
6551 if (CHAR_TABLE_P (Vglyphless_char_display)
6552 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6554 if (c >= 0)
6556 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6557 if (CONSP (glyphless_method))
6558 glyphless_method = FRAME_WINDOW_P (it->f)
6559 ? XCAR (glyphless_method)
6560 : XCDR (glyphless_method);
6562 else
6563 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6566 retry:
6567 if (NILP (glyphless_method))
6569 if (c >= 0)
6570 /* The default is to display the character by a proper font. */
6571 return Qnil;
6572 /* The default for the no-font case is to display an empty box. */
6573 glyphless_method = Qempty_box;
6575 if (EQ (glyphless_method, Qzero_width))
6577 if (c >= 0)
6578 return glyphless_method;
6579 /* This method can't be used for the no-font case. */
6580 glyphless_method = Qempty_box;
6582 if (EQ (glyphless_method, Qthin_space))
6583 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6584 else if (EQ (glyphless_method, Qempty_box))
6585 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6586 else if (EQ (glyphless_method, Qhex_code))
6587 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6588 else if (STRINGP (glyphless_method))
6589 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6590 else
6592 /* Invalid value. We use the default method. */
6593 glyphless_method = Qnil;
6594 goto retry;
6596 it->what = IT_GLYPHLESS;
6597 return glyphless_method;
6600 /* Merge escape glyph face and cache the result. */
6602 static struct frame *last_escape_glyph_frame = NULL;
6603 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6604 static int last_escape_glyph_merged_face_id = 0;
6606 static int
6607 merge_escape_glyph_face (struct it *it)
6609 int face_id;
6611 if (it->f == last_escape_glyph_frame
6612 && it->face_id == last_escape_glyph_face_id)
6613 face_id = last_escape_glyph_merged_face_id;
6614 else
6616 /* Merge the `escape-glyph' face into the current face. */
6617 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6618 last_escape_glyph_frame = it->f;
6619 last_escape_glyph_face_id = it->face_id;
6620 last_escape_glyph_merged_face_id = face_id;
6622 return face_id;
6625 /* Likewise for glyphless glyph face. */
6627 static struct frame *last_glyphless_glyph_frame = NULL;
6628 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6629 static int last_glyphless_glyph_merged_face_id = 0;
6632 merge_glyphless_glyph_face (struct it *it)
6634 int face_id;
6636 if (it->f == last_glyphless_glyph_frame
6637 && it->face_id == last_glyphless_glyph_face_id)
6638 face_id = last_glyphless_glyph_merged_face_id;
6639 else
6641 /* Merge the `glyphless-char' face into the current face. */
6642 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6643 last_glyphless_glyph_frame = it->f;
6644 last_glyphless_glyph_face_id = it->face_id;
6645 last_glyphless_glyph_merged_face_id = face_id;
6647 return face_id;
6650 /* Load IT's display element fields with information about the next
6651 display element from the current position of IT. Value is zero if
6652 end of buffer (or C string) is reached. */
6654 static int
6655 get_next_display_element (struct it *it)
6657 /* Non-zero means that we found a display element. Zero means that
6658 we hit the end of what we iterate over. Performance note: the
6659 function pointer `method' used here turns out to be faster than
6660 using a sequence of if-statements. */
6661 int success_p;
6663 get_next:
6664 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6666 if (it->what == IT_CHARACTER)
6668 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6669 and only if (a) the resolved directionality of that character
6670 is R..." */
6671 /* FIXME: Do we need an exception for characters from display
6672 tables? */
6673 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6674 it->c = bidi_mirror_char (it->c);
6675 /* Map via display table or translate control characters.
6676 IT->c, IT->len etc. have been set to the next character by
6677 the function call above. If we have a display table, and it
6678 contains an entry for IT->c, translate it. Don't do this if
6679 IT->c itself comes from a display table, otherwise we could
6680 end up in an infinite recursion. (An alternative could be to
6681 count the recursion depth of this function and signal an
6682 error when a certain maximum depth is reached.) Is it worth
6683 it? */
6684 if (success_p && it->dpvec == NULL)
6686 Lisp_Object dv;
6687 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6688 int nonascii_space_p = 0;
6689 int nonascii_hyphen_p = 0;
6690 int c = it->c; /* This is the character to display. */
6692 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6694 eassert (SINGLE_BYTE_CHAR_P (c));
6695 if (unibyte_display_via_language_environment)
6697 c = DECODE_CHAR (unibyte, c);
6698 if (c < 0)
6699 c = BYTE8_TO_CHAR (it->c);
6701 else
6702 c = BYTE8_TO_CHAR (it->c);
6705 if (it->dp
6706 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6707 VECTORP (dv)))
6709 struct Lisp_Vector *v = XVECTOR (dv);
6711 /* Return the first character from the display table
6712 entry, if not empty. If empty, don't display the
6713 current character. */
6714 if (v->header.size)
6716 it->dpvec_char_len = it->len;
6717 it->dpvec = v->u.contents;
6718 it->dpend = v->u.contents + v->header.size;
6719 it->current.dpvec_index = 0;
6720 it->dpvec_face_id = -1;
6721 it->saved_face_id = it->face_id;
6722 it->method = GET_FROM_DISPLAY_VECTOR;
6723 it->ellipsis_p = 0;
6725 else
6727 set_iterator_to_next (it, 0);
6729 goto get_next;
6732 if (! NILP (lookup_glyphless_char_display (c, it)))
6734 if (it->what == IT_GLYPHLESS)
6735 goto done;
6736 /* Don't display this character. */
6737 set_iterator_to_next (it, 0);
6738 goto get_next;
6741 /* If `nobreak-char-display' is non-nil, we display
6742 non-ASCII spaces and hyphens specially. */
6743 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6745 if (c == 0xA0)
6746 nonascii_space_p = 1;
6747 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6748 nonascii_hyphen_p = 1;
6751 /* Translate control characters into `\003' or `^C' form.
6752 Control characters coming from a display table entry are
6753 currently not translated because we use IT->dpvec to hold
6754 the translation. This could easily be changed but I
6755 don't believe that it is worth doing.
6757 The characters handled by `nobreak-char-display' must be
6758 translated too.
6760 Non-printable characters and raw-byte characters are also
6761 translated to octal form. */
6762 if (((c < ' ' || c == 127) /* ASCII control chars */
6763 ? (it->area != TEXT_AREA
6764 /* In mode line, treat \n, \t like other crl chars. */
6765 || (c != '\t'
6766 && it->glyph_row
6767 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6768 || (c != '\n' && c != '\t'))
6769 : (nonascii_space_p
6770 || nonascii_hyphen_p
6771 || CHAR_BYTE8_P (c)
6772 || ! CHAR_PRINTABLE_P (c))))
6774 /* C is a control character, non-ASCII space/hyphen,
6775 raw-byte, or a non-printable character which must be
6776 displayed either as '\003' or as `^C' where the '\\'
6777 and '^' can be defined in the display table. Fill
6778 IT->ctl_chars with glyphs for what we have to
6779 display. Then, set IT->dpvec to these glyphs. */
6780 Lisp_Object gc;
6781 int ctl_len;
6782 int face_id;
6783 int lface_id = 0;
6784 int escape_glyph;
6786 /* Handle control characters with ^. */
6788 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6790 int g;
6792 g = '^'; /* default glyph for Control */
6793 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6794 if (it->dp
6795 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6797 g = GLYPH_CODE_CHAR (gc);
6798 lface_id = GLYPH_CODE_FACE (gc);
6801 face_id = (lface_id
6802 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6803 : merge_escape_glyph_face (it));
6805 XSETINT (it->ctl_chars[0], g);
6806 XSETINT (it->ctl_chars[1], c ^ 0100);
6807 ctl_len = 2;
6808 goto display_control;
6811 /* Handle non-ascii space in the mode where it only gets
6812 highlighting. */
6814 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6816 /* Merge `nobreak-space' into the current face. */
6817 face_id = merge_faces (it->f, Qnobreak_space, 0,
6818 it->face_id);
6819 XSETINT (it->ctl_chars[0], ' ');
6820 ctl_len = 1;
6821 goto display_control;
6824 /* Handle sequences that start with the "escape glyph". */
6826 /* the default escape glyph is \. */
6827 escape_glyph = '\\';
6829 if (it->dp
6830 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6832 escape_glyph = GLYPH_CODE_CHAR (gc);
6833 lface_id = GLYPH_CODE_FACE (gc);
6836 face_id = (lface_id
6837 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6838 : merge_escape_glyph_face (it));
6840 /* Draw non-ASCII hyphen with just highlighting: */
6842 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6844 XSETINT (it->ctl_chars[0], '-');
6845 ctl_len = 1;
6846 goto display_control;
6849 /* Draw non-ASCII space/hyphen with escape glyph: */
6851 if (nonascii_space_p || nonascii_hyphen_p)
6853 XSETINT (it->ctl_chars[0], escape_glyph);
6854 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6855 ctl_len = 2;
6856 goto display_control;
6860 char str[10];
6861 int len, i;
6863 if (CHAR_BYTE8_P (c))
6864 /* Display \200 instead of \17777600. */
6865 c = CHAR_TO_BYTE8 (c);
6866 len = sprintf (str, "%03o", c);
6868 XSETINT (it->ctl_chars[0], escape_glyph);
6869 for (i = 0; i < len; i++)
6870 XSETINT (it->ctl_chars[i + 1], str[i]);
6871 ctl_len = len + 1;
6874 display_control:
6875 /* Set up IT->dpvec and return first character from it. */
6876 it->dpvec_char_len = it->len;
6877 it->dpvec = it->ctl_chars;
6878 it->dpend = it->dpvec + ctl_len;
6879 it->current.dpvec_index = 0;
6880 it->dpvec_face_id = face_id;
6881 it->saved_face_id = it->face_id;
6882 it->method = GET_FROM_DISPLAY_VECTOR;
6883 it->ellipsis_p = 0;
6884 goto get_next;
6886 it->char_to_display = c;
6888 else if (success_p)
6890 it->char_to_display = it->c;
6894 #ifdef HAVE_WINDOW_SYSTEM
6895 /* Adjust face id for a multibyte character. There are no multibyte
6896 character in unibyte text. */
6897 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6898 && it->multibyte_p
6899 && success_p
6900 && FRAME_WINDOW_P (it->f))
6902 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6904 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6906 /* Automatic composition with glyph-string. */
6907 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6909 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6911 else
6913 ptrdiff_t pos = (it->s ? -1
6914 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6915 : IT_CHARPOS (*it));
6916 int c;
6918 if (it->what == IT_CHARACTER)
6919 c = it->char_to_display;
6920 else
6922 struct composition *cmp = composition_table[it->cmp_it.id];
6923 int i;
6925 c = ' ';
6926 for (i = 0; i < cmp->glyph_len; i++)
6927 /* TAB in a composition means display glyphs with
6928 padding space on the left or right. */
6929 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6930 break;
6932 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6935 #endif /* HAVE_WINDOW_SYSTEM */
6937 done:
6938 /* Is this character the last one of a run of characters with
6939 box? If yes, set IT->end_of_box_run_p to 1. */
6940 if (it->face_box_p
6941 && it->s == NULL)
6943 if (it->method == GET_FROM_STRING && it->sp)
6945 int face_id = underlying_face_id (it);
6946 struct face *face = FACE_FROM_ID (it->f, face_id);
6948 if (face)
6950 if (face->box == FACE_NO_BOX)
6952 /* If the box comes from face properties in a
6953 display string, check faces in that string. */
6954 int string_face_id = face_after_it_pos (it);
6955 it->end_of_box_run_p
6956 = (FACE_FROM_ID (it->f, string_face_id)->box
6957 == FACE_NO_BOX);
6959 /* Otherwise, the box comes from the underlying face.
6960 If this is the last string character displayed, check
6961 the next buffer location. */
6962 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6963 && (it->current.overlay_string_index
6964 == it->n_overlay_strings - 1))
6966 ptrdiff_t ignore;
6967 int next_face_id;
6968 struct text_pos pos = it->current.pos;
6969 INC_TEXT_POS (pos, it->multibyte_p);
6971 next_face_id = face_at_buffer_position
6972 (it->w, CHARPOS (pos), &ignore,
6973 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6974 -1);
6975 it->end_of_box_run_p
6976 = (FACE_FROM_ID (it->f, next_face_id)->box
6977 == FACE_NO_BOX);
6981 /* next_element_from_display_vector sets this flag according to
6982 faces of the display vector glyphs, see there. */
6983 else if (it->method != GET_FROM_DISPLAY_VECTOR)
6985 int face_id = face_after_it_pos (it);
6986 it->end_of_box_run_p
6987 = (face_id != it->face_id
6988 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6991 /* If we reached the end of the object we've been iterating (e.g., a
6992 display string or an overlay string), and there's something on
6993 IT->stack, proceed with what's on the stack. It doesn't make
6994 sense to return zero if there's unprocessed stuff on the stack,
6995 because otherwise that stuff will never be displayed. */
6996 if (!success_p && it->sp > 0)
6998 set_iterator_to_next (it, 0);
6999 success_p = get_next_display_element (it);
7002 /* Value is 0 if end of buffer or string reached. */
7003 return success_p;
7007 /* Move IT to the next display element.
7009 RESEAT_P non-zero means if called on a newline in buffer text,
7010 skip to the next visible line start.
7012 Functions get_next_display_element and set_iterator_to_next are
7013 separate because I find this arrangement easier to handle than a
7014 get_next_display_element function that also increments IT's
7015 position. The way it is we can first look at an iterator's current
7016 display element, decide whether it fits on a line, and if it does,
7017 increment the iterator position. The other way around we probably
7018 would either need a flag indicating whether the iterator has to be
7019 incremented the next time, or we would have to implement a
7020 decrement position function which would not be easy to write. */
7022 void
7023 set_iterator_to_next (struct it *it, int reseat_p)
7025 /* Reset flags indicating start and end of a sequence of characters
7026 with box. Reset them at the start of this function because
7027 moving the iterator to a new position might set them. */
7028 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7030 switch (it->method)
7032 case GET_FROM_BUFFER:
7033 /* The current display element of IT is a character from
7034 current_buffer. Advance in the buffer, and maybe skip over
7035 invisible lines that are so because of selective display. */
7036 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7037 reseat_at_next_visible_line_start (it, 0);
7038 else if (it->cmp_it.id >= 0)
7040 /* We are currently getting glyphs from a composition. */
7041 int i;
7043 if (! it->bidi_p)
7045 IT_CHARPOS (*it) += it->cmp_it.nchars;
7046 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7047 if (it->cmp_it.to < it->cmp_it.nglyphs)
7049 it->cmp_it.from = it->cmp_it.to;
7051 else
7053 it->cmp_it.id = -1;
7054 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7055 IT_BYTEPOS (*it),
7056 it->end_charpos, Qnil);
7059 else if (! it->cmp_it.reversed_p)
7061 /* Composition created while scanning forward. */
7062 /* Update IT's char/byte positions to point to the first
7063 character of the next grapheme cluster, or to the
7064 character visually after the current composition. */
7065 for (i = 0; i < it->cmp_it.nchars; i++)
7066 bidi_move_to_visually_next (&it->bidi_it);
7067 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7068 IT_CHARPOS (*it) = it->bidi_it.charpos;
7070 if (it->cmp_it.to < it->cmp_it.nglyphs)
7072 /* Proceed to the next grapheme cluster. */
7073 it->cmp_it.from = it->cmp_it.to;
7075 else
7077 /* No more grapheme clusters in this composition.
7078 Find the next stop position. */
7079 ptrdiff_t stop = it->end_charpos;
7080 if (it->bidi_it.scan_dir < 0)
7081 /* Now we are scanning backward and don't know
7082 where to stop. */
7083 stop = -1;
7084 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7085 IT_BYTEPOS (*it), stop, Qnil);
7088 else
7090 /* Composition created while scanning backward. */
7091 /* Update IT's char/byte positions to point to the last
7092 character of the previous grapheme cluster, or the
7093 character visually after the current composition. */
7094 for (i = 0; i < it->cmp_it.nchars; i++)
7095 bidi_move_to_visually_next (&it->bidi_it);
7096 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7097 IT_CHARPOS (*it) = it->bidi_it.charpos;
7098 if (it->cmp_it.from > 0)
7100 /* Proceed to the previous grapheme cluster. */
7101 it->cmp_it.to = it->cmp_it.from;
7103 else
7105 /* No more grapheme clusters in this composition.
7106 Find the next stop position. */
7107 ptrdiff_t stop = it->end_charpos;
7108 if (it->bidi_it.scan_dir < 0)
7109 /* Now we are scanning backward and don't know
7110 where to stop. */
7111 stop = -1;
7112 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7113 IT_BYTEPOS (*it), stop, Qnil);
7117 else
7119 eassert (it->len != 0);
7121 if (!it->bidi_p)
7123 IT_BYTEPOS (*it) += it->len;
7124 IT_CHARPOS (*it) += 1;
7126 else
7128 int prev_scan_dir = it->bidi_it.scan_dir;
7129 /* If this is a new paragraph, determine its base
7130 direction (a.k.a. its base embedding level). */
7131 if (it->bidi_it.new_paragraph)
7132 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7133 bidi_move_to_visually_next (&it->bidi_it);
7134 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7135 IT_CHARPOS (*it) = it->bidi_it.charpos;
7136 if (prev_scan_dir != it->bidi_it.scan_dir)
7138 /* As the scan direction was changed, we must
7139 re-compute the stop position for composition. */
7140 ptrdiff_t stop = it->end_charpos;
7141 if (it->bidi_it.scan_dir < 0)
7142 stop = -1;
7143 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7144 IT_BYTEPOS (*it), stop, Qnil);
7147 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7149 break;
7151 case GET_FROM_C_STRING:
7152 /* Current display element of IT is from a C string. */
7153 if (!it->bidi_p
7154 /* If the string position is beyond string's end, it means
7155 next_element_from_c_string is padding the string with
7156 blanks, in which case we bypass the bidi iterator,
7157 because it cannot deal with such virtual characters. */
7158 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7160 IT_BYTEPOS (*it) += it->len;
7161 IT_CHARPOS (*it) += 1;
7163 else
7165 bidi_move_to_visually_next (&it->bidi_it);
7166 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7167 IT_CHARPOS (*it) = it->bidi_it.charpos;
7169 break;
7171 case GET_FROM_DISPLAY_VECTOR:
7172 /* Current display element of IT is from a display table entry.
7173 Advance in the display table definition. Reset it to null if
7174 end reached, and continue with characters from buffers/
7175 strings. */
7176 ++it->current.dpvec_index;
7178 /* Restore face of the iterator to what they were before the
7179 display vector entry (these entries may contain faces). */
7180 it->face_id = it->saved_face_id;
7182 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7184 int recheck_faces = it->ellipsis_p;
7186 if (it->s)
7187 it->method = GET_FROM_C_STRING;
7188 else if (STRINGP (it->string))
7189 it->method = GET_FROM_STRING;
7190 else
7192 it->method = GET_FROM_BUFFER;
7193 it->object = it->w->contents;
7196 it->dpvec = NULL;
7197 it->current.dpvec_index = -1;
7199 /* Skip over characters which were displayed via IT->dpvec. */
7200 if (it->dpvec_char_len < 0)
7201 reseat_at_next_visible_line_start (it, 1);
7202 else if (it->dpvec_char_len > 0)
7204 if (it->method == GET_FROM_STRING
7205 && it->current.overlay_string_index >= 0
7206 && it->n_overlay_strings > 0)
7207 it->ignore_overlay_strings_at_pos_p = 1;
7208 it->len = it->dpvec_char_len;
7209 set_iterator_to_next (it, reseat_p);
7212 /* Maybe recheck faces after display vector */
7213 if (recheck_faces)
7214 it->stop_charpos = IT_CHARPOS (*it);
7216 break;
7218 case GET_FROM_STRING:
7219 /* Current display element is a character from a Lisp string. */
7220 eassert (it->s == NULL && STRINGP (it->string));
7221 /* Don't advance past string end. These conditions are true
7222 when set_iterator_to_next is called at the end of
7223 get_next_display_element, in which case the Lisp string is
7224 already exhausted, and all we want is pop the iterator
7225 stack. */
7226 if (it->current.overlay_string_index >= 0)
7228 /* This is an overlay string, so there's no padding with
7229 spaces, and the number of characters in the string is
7230 where the string ends. */
7231 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7232 goto consider_string_end;
7234 else
7236 /* Not an overlay string. There could be padding, so test
7237 against it->end_charpos . */
7238 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7239 goto consider_string_end;
7241 if (it->cmp_it.id >= 0)
7243 int i;
7245 if (! it->bidi_p)
7247 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7248 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7249 if (it->cmp_it.to < it->cmp_it.nglyphs)
7250 it->cmp_it.from = it->cmp_it.to;
7251 else
7253 it->cmp_it.id = -1;
7254 composition_compute_stop_pos (&it->cmp_it,
7255 IT_STRING_CHARPOS (*it),
7256 IT_STRING_BYTEPOS (*it),
7257 it->end_charpos, it->string);
7260 else if (! it->cmp_it.reversed_p)
7262 for (i = 0; i < it->cmp_it.nchars; i++)
7263 bidi_move_to_visually_next (&it->bidi_it);
7264 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7265 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7267 if (it->cmp_it.to < it->cmp_it.nglyphs)
7268 it->cmp_it.from = it->cmp_it.to;
7269 else
7271 ptrdiff_t stop = it->end_charpos;
7272 if (it->bidi_it.scan_dir < 0)
7273 stop = -1;
7274 composition_compute_stop_pos (&it->cmp_it,
7275 IT_STRING_CHARPOS (*it),
7276 IT_STRING_BYTEPOS (*it), stop,
7277 it->string);
7280 else
7282 for (i = 0; i < it->cmp_it.nchars; i++)
7283 bidi_move_to_visually_next (&it->bidi_it);
7284 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7285 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7286 if (it->cmp_it.from > 0)
7287 it->cmp_it.to = it->cmp_it.from;
7288 else
7290 ptrdiff_t stop = it->end_charpos;
7291 if (it->bidi_it.scan_dir < 0)
7292 stop = -1;
7293 composition_compute_stop_pos (&it->cmp_it,
7294 IT_STRING_CHARPOS (*it),
7295 IT_STRING_BYTEPOS (*it), stop,
7296 it->string);
7300 else
7302 if (!it->bidi_p
7303 /* If the string position is beyond string's end, it
7304 means next_element_from_string is padding the string
7305 with blanks, in which case we bypass the bidi
7306 iterator, because it cannot deal with such virtual
7307 characters. */
7308 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7310 IT_STRING_BYTEPOS (*it) += it->len;
7311 IT_STRING_CHARPOS (*it) += 1;
7313 else
7315 int prev_scan_dir = it->bidi_it.scan_dir;
7317 bidi_move_to_visually_next (&it->bidi_it);
7318 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7319 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7320 if (prev_scan_dir != it->bidi_it.scan_dir)
7322 ptrdiff_t stop = it->end_charpos;
7324 if (it->bidi_it.scan_dir < 0)
7325 stop = -1;
7326 composition_compute_stop_pos (&it->cmp_it,
7327 IT_STRING_CHARPOS (*it),
7328 IT_STRING_BYTEPOS (*it), stop,
7329 it->string);
7334 consider_string_end:
7336 if (it->current.overlay_string_index >= 0)
7338 /* IT->string is an overlay string. Advance to the
7339 next, if there is one. */
7340 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7342 it->ellipsis_p = 0;
7343 next_overlay_string (it);
7344 if (it->ellipsis_p)
7345 setup_for_ellipsis (it, 0);
7348 else
7350 /* IT->string is not an overlay string. If we reached
7351 its end, and there is something on IT->stack, proceed
7352 with what is on the stack. This can be either another
7353 string, this time an overlay string, or a buffer. */
7354 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7355 && it->sp > 0)
7357 pop_it (it);
7358 if (it->method == GET_FROM_STRING)
7359 goto consider_string_end;
7362 break;
7364 case GET_FROM_IMAGE:
7365 case GET_FROM_STRETCH:
7366 /* The position etc with which we have to proceed are on
7367 the stack. The position may be at the end of a string,
7368 if the `display' property takes up the whole string. */
7369 eassert (it->sp > 0);
7370 pop_it (it);
7371 if (it->method == GET_FROM_STRING)
7372 goto consider_string_end;
7373 break;
7375 default:
7376 /* There are no other methods defined, so this should be a bug. */
7377 emacs_abort ();
7380 eassert (it->method != GET_FROM_STRING
7381 || (STRINGP (it->string)
7382 && IT_STRING_CHARPOS (*it) >= 0));
7385 /* Load IT's display element fields with information about the next
7386 display element which comes from a display table entry or from the
7387 result of translating a control character to one of the forms `^C'
7388 or `\003'.
7390 IT->dpvec holds the glyphs to return as characters.
7391 IT->saved_face_id holds the face id before the display vector--it
7392 is restored into IT->face_id in set_iterator_to_next. */
7394 static int
7395 next_element_from_display_vector (struct it *it)
7397 Lisp_Object gc;
7398 int prev_face_id = it->face_id;
7399 int next_face_id;
7401 /* Precondition. */
7402 eassert (it->dpvec && it->current.dpvec_index >= 0);
7404 it->face_id = it->saved_face_id;
7406 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7407 That seemed totally bogus - so I changed it... */
7408 gc = it->dpvec[it->current.dpvec_index];
7410 if (GLYPH_CODE_P (gc))
7412 struct face *this_face, *prev_face, *next_face;
7414 it->c = GLYPH_CODE_CHAR (gc);
7415 it->len = CHAR_BYTES (it->c);
7417 /* The entry may contain a face id to use. Such a face id is
7418 the id of a Lisp face, not a realized face. A face id of
7419 zero means no face is specified. */
7420 if (it->dpvec_face_id >= 0)
7421 it->face_id = it->dpvec_face_id;
7422 else
7424 int lface_id = GLYPH_CODE_FACE (gc);
7425 if (lface_id > 0)
7426 it->face_id = merge_faces (it->f, Qt, lface_id,
7427 it->saved_face_id);
7430 /* Glyphs in the display vector could have the box face, so we
7431 need to set the related flags in the iterator, as
7432 appropriate. */
7433 this_face = FACE_FROM_ID (it->f, it->face_id);
7434 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7436 /* Is this character the first character of a box-face run? */
7437 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7438 && (!prev_face
7439 || prev_face->box == FACE_NO_BOX));
7441 /* For the last character of the box-face run, we need to look
7442 either at the next glyph from the display vector, or at the
7443 face we saw before the display vector. */
7444 next_face_id = it->saved_face_id;
7445 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7447 if (it->dpvec_face_id >= 0)
7448 next_face_id = it->dpvec_face_id;
7449 else
7451 int lface_id =
7452 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7454 if (lface_id > 0)
7455 next_face_id = merge_faces (it->f, Qt, lface_id,
7456 it->saved_face_id);
7459 next_face = FACE_FROM_ID (it->f, next_face_id);
7460 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7461 && (!next_face
7462 || next_face->box == FACE_NO_BOX));
7463 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7465 else
7466 /* Display table entry is invalid. Return a space. */
7467 it->c = ' ', it->len = 1;
7469 /* Don't change position and object of the iterator here. They are
7470 still the values of the character that had this display table
7471 entry or was translated, and that's what we want. */
7472 it->what = IT_CHARACTER;
7473 return 1;
7476 /* Get the first element of string/buffer in the visual order, after
7477 being reseated to a new position in a string or a buffer. */
7478 static void
7479 get_visually_first_element (struct it *it)
7481 int string_p = STRINGP (it->string) || it->s;
7482 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7483 ptrdiff_t bob = (string_p ? 0 : BEGV);
7485 if (STRINGP (it->string))
7487 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7488 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7490 else
7492 it->bidi_it.charpos = IT_CHARPOS (*it);
7493 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7496 if (it->bidi_it.charpos == eob)
7498 /* Nothing to do, but reset the FIRST_ELT flag, like
7499 bidi_paragraph_init does, because we are not going to
7500 call it. */
7501 it->bidi_it.first_elt = 0;
7503 else if (it->bidi_it.charpos == bob
7504 || (!string_p
7505 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7506 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7508 /* If we are at the beginning of a line/string, we can produce
7509 the next element right away. */
7510 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7511 bidi_move_to_visually_next (&it->bidi_it);
7513 else
7515 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7517 /* We need to prime the bidi iterator starting at the line's or
7518 string's beginning, before we will be able to produce the
7519 next element. */
7520 if (string_p)
7521 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7522 else
7523 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7524 IT_BYTEPOS (*it), -1,
7525 &it->bidi_it.bytepos);
7526 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7529 /* Now return to buffer/string position where we were asked
7530 to get the next display element, and produce that. */
7531 bidi_move_to_visually_next (&it->bidi_it);
7533 while (it->bidi_it.bytepos != orig_bytepos
7534 && it->bidi_it.charpos < eob);
7537 /* Adjust IT's position information to where we ended up. */
7538 if (STRINGP (it->string))
7540 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7541 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7543 else
7545 IT_CHARPOS (*it) = it->bidi_it.charpos;
7546 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7549 if (STRINGP (it->string) || !it->s)
7551 ptrdiff_t stop, charpos, bytepos;
7553 if (STRINGP (it->string))
7555 eassert (!it->s);
7556 stop = SCHARS (it->string);
7557 if (stop > it->end_charpos)
7558 stop = it->end_charpos;
7559 charpos = IT_STRING_CHARPOS (*it);
7560 bytepos = IT_STRING_BYTEPOS (*it);
7562 else
7564 stop = it->end_charpos;
7565 charpos = IT_CHARPOS (*it);
7566 bytepos = IT_BYTEPOS (*it);
7568 if (it->bidi_it.scan_dir < 0)
7569 stop = -1;
7570 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7571 it->string);
7575 /* Load IT with the next display element from Lisp string IT->string.
7576 IT->current.string_pos is the current position within the string.
7577 If IT->current.overlay_string_index >= 0, the Lisp string is an
7578 overlay string. */
7580 static int
7581 next_element_from_string (struct it *it)
7583 struct text_pos position;
7585 eassert (STRINGP (it->string));
7586 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7587 eassert (IT_STRING_CHARPOS (*it) >= 0);
7588 position = it->current.string_pos;
7590 /* With bidi reordering, the character to display might not be the
7591 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7592 that we were reseat()ed to a new string, whose paragraph
7593 direction is not known. */
7594 if (it->bidi_p && it->bidi_it.first_elt)
7596 get_visually_first_element (it);
7597 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7600 /* Time to check for invisible text? */
7601 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7603 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7605 if (!(!it->bidi_p
7606 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7607 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7609 /* With bidi non-linear iteration, we could find
7610 ourselves far beyond the last computed stop_charpos,
7611 with several other stop positions in between that we
7612 missed. Scan them all now, in buffer's logical
7613 order, until we find and handle the last stop_charpos
7614 that precedes our current position. */
7615 handle_stop_backwards (it, it->stop_charpos);
7616 return GET_NEXT_DISPLAY_ELEMENT (it);
7618 else
7620 if (it->bidi_p)
7622 /* Take note of the stop position we just moved
7623 across, for when we will move back across it. */
7624 it->prev_stop = it->stop_charpos;
7625 /* If we are at base paragraph embedding level, take
7626 note of the last stop position seen at this
7627 level. */
7628 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7629 it->base_level_stop = it->stop_charpos;
7631 handle_stop (it);
7633 /* Since a handler may have changed IT->method, we must
7634 recurse here. */
7635 return GET_NEXT_DISPLAY_ELEMENT (it);
7638 else if (it->bidi_p
7639 /* If we are before prev_stop, we may have overstepped
7640 on our way backwards a stop_pos, and if so, we need
7641 to handle that stop_pos. */
7642 && IT_STRING_CHARPOS (*it) < it->prev_stop
7643 /* We can sometimes back up for reasons that have nothing
7644 to do with bidi reordering. E.g., compositions. The
7645 code below is only needed when we are above the base
7646 embedding level, so test for that explicitly. */
7647 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7649 /* If we lost track of base_level_stop, we have no better
7650 place for handle_stop_backwards to start from than string
7651 beginning. This happens, e.g., when we were reseated to
7652 the previous screenful of text by vertical-motion. */
7653 if (it->base_level_stop <= 0
7654 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7655 it->base_level_stop = 0;
7656 handle_stop_backwards (it, it->base_level_stop);
7657 return GET_NEXT_DISPLAY_ELEMENT (it);
7661 if (it->current.overlay_string_index >= 0)
7663 /* Get the next character from an overlay string. In overlay
7664 strings, there is no field width or padding with spaces to
7665 do. */
7666 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7668 it->what = IT_EOB;
7669 return 0;
7671 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7672 IT_STRING_BYTEPOS (*it),
7673 it->bidi_it.scan_dir < 0
7674 ? -1
7675 : SCHARS (it->string))
7676 && next_element_from_composition (it))
7678 return 1;
7680 else if (STRING_MULTIBYTE (it->string))
7682 const unsigned char *s = (SDATA (it->string)
7683 + IT_STRING_BYTEPOS (*it));
7684 it->c = string_char_and_length (s, &it->len);
7686 else
7688 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7689 it->len = 1;
7692 else
7694 /* Get the next character from a Lisp string that is not an
7695 overlay string. Such strings come from the mode line, for
7696 example. We may have to pad with spaces, or truncate the
7697 string. See also next_element_from_c_string. */
7698 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7700 it->what = IT_EOB;
7701 return 0;
7703 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7705 /* Pad with spaces. */
7706 it->c = ' ', it->len = 1;
7707 CHARPOS (position) = BYTEPOS (position) = -1;
7709 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7710 IT_STRING_BYTEPOS (*it),
7711 it->bidi_it.scan_dir < 0
7712 ? -1
7713 : it->string_nchars)
7714 && next_element_from_composition (it))
7716 return 1;
7718 else if (STRING_MULTIBYTE (it->string))
7720 const unsigned char *s = (SDATA (it->string)
7721 + IT_STRING_BYTEPOS (*it));
7722 it->c = string_char_and_length (s, &it->len);
7724 else
7726 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7727 it->len = 1;
7731 /* Record what we have and where it came from. */
7732 it->what = IT_CHARACTER;
7733 it->object = it->string;
7734 it->position = position;
7735 return 1;
7739 /* Load IT with next display element from C string IT->s.
7740 IT->string_nchars is the maximum number of characters to return
7741 from the string. IT->end_charpos may be greater than
7742 IT->string_nchars when this function is called, in which case we
7743 may have to return padding spaces. Value is zero if end of string
7744 reached, including padding spaces. */
7746 static int
7747 next_element_from_c_string (struct it *it)
7749 int success_p = 1;
7751 eassert (it->s);
7752 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7753 it->what = IT_CHARACTER;
7754 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7755 it->object = Qnil;
7757 /* With bidi reordering, the character to display might not be the
7758 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7759 we were reseated to a new string, whose paragraph direction is
7760 not known. */
7761 if (it->bidi_p && it->bidi_it.first_elt)
7762 get_visually_first_element (it);
7764 /* IT's position can be greater than IT->string_nchars in case a
7765 field width or precision has been specified when the iterator was
7766 initialized. */
7767 if (IT_CHARPOS (*it) >= it->end_charpos)
7769 /* End of the game. */
7770 it->what = IT_EOB;
7771 success_p = 0;
7773 else if (IT_CHARPOS (*it) >= it->string_nchars)
7775 /* Pad with spaces. */
7776 it->c = ' ', it->len = 1;
7777 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7779 else if (it->multibyte_p)
7780 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7781 else
7782 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7784 return success_p;
7788 /* Set up IT to return characters from an ellipsis, if appropriate.
7789 The definition of the ellipsis glyphs may come from a display table
7790 entry. This function fills IT with the first glyph from the
7791 ellipsis if an ellipsis is to be displayed. */
7793 static int
7794 next_element_from_ellipsis (struct it *it)
7796 if (it->selective_display_ellipsis_p)
7797 setup_for_ellipsis (it, it->len);
7798 else
7800 /* The face at the current position may be different from the
7801 face we find after the invisible text. Remember what it
7802 was in IT->saved_face_id, and signal that it's there by
7803 setting face_before_selective_p. */
7804 it->saved_face_id = it->face_id;
7805 it->method = GET_FROM_BUFFER;
7806 it->object = it->w->contents;
7807 reseat_at_next_visible_line_start (it, 1);
7808 it->face_before_selective_p = 1;
7811 return GET_NEXT_DISPLAY_ELEMENT (it);
7815 /* Deliver an image display element. The iterator IT is already
7816 filled with image information (done in handle_display_prop). Value
7817 is always 1. */
7820 static int
7821 next_element_from_image (struct it *it)
7823 it->what = IT_IMAGE;
7824 it->ignore_overlay_strings_at_pos_p = 0;
7825 return 1;
7829 /* Fill iterator IT with next display element from a stretch glyph
7830 property. IT->object is the value of the text property. Value is
7831 always 1. */
7833 static int
7834 next_element_from_stretch (struct it *it)
7836 it->what = IT_STRETCH;
7837 return 1;
7840 /* Scan backwards from IT's current position until we find a stop
7841 position, or until BEGV. This is called when we find ourself
7842 before both the last known prev_stop and base_level_stop while
7843 reordering bidirectional text. */
7845 static void
7846 compute_stop_pos_backwards (struct it *it)
7848 const int SCAN_BACK_LIMIT = 1000;
7849 struct text_pos pos;
7850 struct display_pos save_current = it->current;
7851 struct text_pos save_position = it->position;
7852 ptrdiff_t charpos = IT_CHARPOS (*it);
7853 ptrdiff_t where_we_are = charpos;
7854 ptrdiff_t save_stop_pos = it->stop_charpos;
7855 ptrdiff_t save_end_pos = it->end_charpos;
7857 eassert (NILP (it->string) && !it->s);
7858 eassert (it->bidi_p);
7859 it->bidi_p = 0;
7862 it->end_charpos = min (charpos + 1, ZV);
7863 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7864 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7865 reseat_1 (it, pos, 0);
7866 compute_stop_pos (it);
7867 /* We must advance forward, right? */
7868 if (it->stop_charpos <= charpos)
7869 emacs_abort ();
7871 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7873 if (it->stop_charpos <= where_we_are)
7874 it->prev_stop = it->stop_charpos;
7875 else
7876 it->prev_stop = BEGV;
7877 it->bidi_p = 1;
7878 it->current = save_current;
7879 it->position = save_position;
7880 it->stop_charpos = save_stop_pos;
7881 it->end_charpos = save_end_pos;
7884 /* Scan forward from CHARPOS in the current buffer/string, until we
7885 find a stop position > current IT's position. Then handle the stop
7886 position before that. This is called when we bump into a stop
7887 position while reordering bidirectional text. CHARPOS should be
7888 the last previously processed stop_pos (or BEGV/0, if none were
7889 processed yet) whose position is less that IT's current
7890 position. */
7892 static void
7893 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7895 int bufp = !STRINGP (it->string);
7896 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7897 struct display_pos save_current = it->current;
7898 struct text_pos save_position = it->position;
7899 struct text_pos pos1;
7900 ptrdiff_t next_stop;
7902 /* Scan in strict logical order. */
7903 eassert (it->bidi_p);
7904 it->bidi_p = 0;
7907 it->prev_stop = charpos;
7908 if (bufp)
7910 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7911 reseat_1 (it, pos1, 0);
7913 else
7914 it->current.string_pos = string_pos (charpos, it->string);
7915 compute_stop_pos (it);
7916 /* We must advance forward, right? */
7917 if (it->stop_charpos <= it->prev_stop)
7918 emacs_abort ();
7919 charpos = it->stop_charpos;
7921 while (charpos <= where_we_are);
7923 it->bidi_p = 1;
7924 it->current = save_current;
7925 it->position = save_position;
7926 next_stop = it->stop_charpos;
7927 it->stop_charpos = it->prev_stop;
7928 handle_stop (it);
7929 it->stop_charpos = next_stop;
7932 /* Load IT with the next display element from current_buffer. Value
7933 is zero if end of buffer reached. IT->stop_charpos is the next
7934 position at which to stop and check for text properties or buffer
7935 end. */
7937 static int
7938 next_element_from_buffer (struct it *it)
7940 int success_p = 1;
7942 eassert (IT_CHARPOS (*it) >= BEGV);
7943 eassert (NILP (it->string) && !it->s);
7944 eassert (!it->bidi_p
7945 || (EQ (it->bidi_it.string.lstring, Qnil)
7946 && it->bidi_it.string.s == NULL));
7948 /* With bidi reordering, the character to display might not be the
7949 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7950 we were reseat()ed to a new buffer position, which is potentially
7951 a different paragraph. */
7952 if (it->bidi_p && it->bidi_it.first_elt)
7954 get_visually_first_element (it);
7955 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7958 if (IT_CHARPOS (*it) >= it->stop_charpos)
7960 if (IT_CHARPOS (*it) >= it->end_charpos)
7962 int overlay_strings_follow_p;
7964 /* End of the game, except when overlay strings follow that
7965 haven't been returned yet. */
7966 if (it->overlay_strings_at_end_processed_p)
7967 overlay_strings_follow_p = 0;
7968 else
7970 it->overlay_strings_at_end_processed_p = 1;
7971 overlay_strings_follow_p = get_overlay_strings (it, 0);
7974 if (overlay_strings_follow_p)
7975 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7976 else
7978 it->what = IT_EOB;
7979 it->position = it->current.pos;
7980 success_p = 0;
7983 else if (!(!it->bidi_p
7984 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7985 || IT_CHARPOS (*it) == it->stop_charpos))
7987 /* With bidi non-linear iteration, we could find ourselves
7988 far beyond the last computed stop_charpos, with several
7989 other stop positions in between that we missed. Scan
7990 them all now, in buffer's logical order, until we find
7991 and handle the last stop_charpos that precedes our
7992 current position. */
7993 handle_stop_backwards (it, it->stop_charpos);
7994 return GET_NEXT_DISPLAY_ELEMENT (it);
7996 else
7998 if (it->bidi_p)
8000 /* Take note of the stop position we just moved across,
8001 for when we will move back across it. */
8002 it->prev_stop = it->stop_charpos;
8003 /* If we are at base paragraph embedding level, take
8004 note of the last stop position seen at this
8005 level. */
8006 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8007 it->base_level_stop = it->stop_charpos;
8009 handle_stop (it);
8010 return GET_NEXT_DISPLAY_ELEMENT (it);
8013 else if (it->bidi_p
8014 /* If we are before prev_stop, we may have overstepped on
8015 our way backwards a stop_pos, and if so, we need to
8016 handle that stop_pos. */
8017 && IT_CHARPOS (*it) < it->prev_stop
8018 /* We can sometimes back up for reasons that have nothing
8019 to do with bidi reordering. E.g., compositions. The
8020 code below is only needed when we are above the base
8021 embedding level, so test for that explicitly. */
8022 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8024 if (it->base_level_stop <= 0
8025 || IT_CHARPOS (*it) < it->base_level_stop)
8027 /* If we lost track of base_level_stop, we need to find
8028 prev_stop by looking backwards. This happens, e.g., when
8029 we were reseated to the previous screenful of text by
8030 vertical-motion. */
8031 it->base_level_stop = BEGV;
8032 compute_stop_pos_backwards (it);
8033 handle_stop_backwards (it, it->prev_stop);
8035 else
8036 handle_stop_backwards (it, it->base_level_stop);
8037 return GET_NEXT_DISPLAY_ELEMENT (it);
8039 else
8041 /* No face changes, overlays etc. in sight, so just return a
8042 character from current_buffer. */
8043 unsigned char *p;
8044 ptrdiff_t stop;
8046 /* Maybe run the redisplay end trigger hook. Performance note:
8047 This doesn't seem to cost measurable time. */
8048 if (it->redisplay_end_trigger_charpos
8049 && it->glyph_row
8050 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8051 run_redisplay_end_trigger_hook (it);
8053 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8054 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8055 stop)
8056 && next_element_from_composition (it))
8058 return 1;
8061 /* Get the next character, maybe multibyte. */
8062 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8063 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8064 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8065 else
8066 it->c = *p, it->len = 1;
8068 /* Record what we have and where it came from. */
8069 it->what = IT_CHARACTER;
8070 it->object = it->w->contents;
8071 it->position = it->current.pos;
8073 /* Normally we return the character found above, except when we
8074 really want to return an ellipsis for selective display. */
8075 if (it->selective)
8077 if (it->c == '\n')
8079 /* A value of selective > 0 means hide lines indented more
8080 than that number of columns. */
8081 if (it->selective > 0
8082 && IT_CHARPOS (*it) + 1 < ZV
8083 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8084 IT_BYTEPOS (*it) + 1,
8085 it->selective))
8087 success_p = next_element_from_ellipsis (it);
8088 it->dpvec_char_len = -1;
8091 else if (it->c == '\r' && it->selective == -1)
8093 /* A value of selective == -1 means that everything from the
8094 CR to the end of the line is invisible, with maybe an
8095 ellipsis displayed for it. */
8096 success_p = next_element_from_ellipsis (it);
8097 it->dpvec_char_len = -1;
8102 /* Value is zero if end of buffer reached. */
8103 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8104 return success_p;
8108 /* Run the redisplay end trigger hook for IT. */
8110 static void
8111 run_redisplay_end_trigger_hook (struct it *it)
8113 Lisp_Object args[3];
8115 /* IT->glyph_row should be non-null, i.e. we should be actually
8116 displaying something, or otherwise we should not run the hook. */
8117 eassert (it->glyph_row);
8119 /* Set up hook arguments. */
8120 args[0] = Qredisplay_end_trigger_functions;
8121 args[1] = it->window;
8122 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8123 it->redisplay_end_trigger_charpos = 0;
8125 /* Since we are *trying* to run these functions, don't try to run
8126 them again, even if they get an error. */
8127 wset_redisplay_end_trigger (it->w, Qnil);
8128 Frun_hook_with_args (3, args);
8130 /* Notice if it changed the face of the character we are on. */
8131 handle_face_prop (it);
8135 /* Deliver a composition display element. Unlike the other
8136 next_element_from_XXX, this function is not registered in the array
8137 get_next_element[]. It is called from next_element_from_buffer and
8138 next_element_from_string when necessary. */
8140 static int
8141 next_element_from_composition (struct it *it)
8143 it->what = IT_COMPOSITION;
8144 it->len = it->cmp_it.nbytes;
8145 if (STRINGP (it->string))
8147 if (it->c < 0)
8149 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8150 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8151 return 0;
8153 it->position = it->current.string_pos;
8154 it->object = it->string;
8155 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8156 IT_STRING_BYTEPOS (*it), it->string);
8158 else
8160 if (it->c < 0)
8162 IT_CHARPOS (*it) += it->cmp_it.nchars;
8163 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8164 if (it->bidi_p)
8166 if (it->bidi_it.new_paragraph)
8167 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8168 /* Resync the bidi iterator with IT's new position.
8169 FIXME: this doesn't support bidirectional text. */
8170 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8171 bidi_move_to_visually_next (&it->bidi_it);
8173 return 0;
8175 it->position = it->current.pos;
8176 it->object = it->w->contents;
8177 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8178 IT_BYTEPOS (*it), Qnil);
8180 return 1;
8185 /***********************************************************************
8186 Moving an iterator without producing glyphs
8187 ***********************************************************************/
8189 /* Check if iterator is at a position corresponding to a valid buffer
8190 position after some move_it_ call. */
8192 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8193 ((it)->method == GET_FROM_STRING \
8194 ? IT_STRING_CHARPOS (*it) == 0 \
8195 : 1)
8198 /* Move iterator IT to a specified buffer or X position within one
8199 line on the display without producing glyphs.
8201 OP should be a bit mask including some or all of these bits:
8202 MOVE_TO_X: Stop upon reaching x-position TO_X.
8203 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8204 Regardless of OP's value, stop upon reaching the end of the display line.
8206 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8207 This means, in particular, that TO_X includes window's horizontal
8208 scroll amount.
8210 The return value has several possible values that
8211 say what condition caused the scan to stop:
8213 MOVE_POS_MATCH_OR_ZV
8214 - when TO_POS or ZV was reached.
8216 MOVE_X_REACHED
8217 -when TO_X was reached before TO_POS or ZV were reached.
8219 MOVE_LINE_CONTINUED
8220 - when we reached the end of the display area and the line must
8221 be continued.
8223 MOVE_LINE_TRUNCATED
8224 - when we reached the end of the display area and the line is
8225 truncated.
8227 MOVE_NEWLINE_OR_CR
8228 - when we stopped at a line end, i.e. a newline or a CR and selective
8229 display is on. */
8231 static enum move_it_result
8232 move_it_in_display_line_to (struct it *it,
8233 ptrdiff_t to_charpos, int to_x,
8234 enum move_operation_enum op)
8236 enum move_it_result result = MOVE_UNDEFINED;
8237 struct glyph_row *saved_glyph_row;
8238 struct it wrap_it, atpos_it, atx_it, ppos_it;
8239 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8240 void *ppos_data = NULL;
8241 int may_wrap = 0;
8242 enum it_method prev_method = it->method;
8243 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8244 int saw_smaller_pos = prev_pos < to_charpos;
8246 /* Don't produce glyphs in produce_glyphs. */
8247 saved_glyph_row = it->glyph_row;
8248 it->glyph_row = NULL;
8250 /* Use wrap_it to save a copy of IT wherever a word wrap could
8251 occur. Use atpos_it to save a copy of IT at the desired buffer
8252 position, if found, so that we can scan ahead and check if the
8253 word later overshoots the window edge. Use atx_it similarly, for
8254 pixel positions. */
8255 wrap_it.sp = -1;
8256 atpos_it.sp = -1;
8257 atx_it.sp = -1;
8259 /* Use ppos_it under bidi reordering to save a copy of IT for the
8260 position > CHARPOS that is the closest to CHARPOS. We restore
8261 that position in IT when we have scanned the entire display line
8262 without finding a match for CHARPOS and all the character
8263 positions are greater than CHARPOS. */
8264 if (it->bidi_p)
8266 SAVE_IT (ppos_it, *it, ppos_data);
8267 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8268 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8269 SAVE_IT (ppos_it, *it, ppos_data);
8272 #define BUFFER_POS_REACHED_P() \
8273 ((op & MOVE_TO_POS) != 0 \
8274 && BUFFERP (it->object) \
8275 && (IT_CHARPOS (*it) == to_charpos \
8276 || ((!it->bidi_p \
8277 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8278 && IT_CHARPOS (*it) > to_charpos) \
8279 || (it->what == IT_COMPOSITION \
8280 && ((IT_CHARPOS (*it) > to_charpos \
8281 && to_charpos >= it->cmp_it.charpos) \
8282 || (IT_CHARPOS (*it) < to_charpos \
8283 && to_charpos <= it->cmp_it.charpos)))) \
8284 && (it->method == GET_FROM_BUFFER \
8285 || (it->method == GET_FROM_DISPLAY_VECTOR \
8286 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8288 /* If there's a line-/wrap-prefix, handle it. */
8289 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8290 && it->current_y < it->last_visible_y)
8291 handle_line_prefix (it);
8293 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8294 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8296 while (1)
8298 int x, i, ascent = 0, descent = 0;
8300 /* Utility macro to reset an iterator with x, ascent, and descent. */
8301 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8302 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8303 (IT)->max_descent = descent)
8305 /* Stop if we move beyond TO_CHARPOS (after an image or a
8306 display string or stretch glyph). */
8307 if ((op & MOVE_TO_POS) != 0
8308 && BUFFERP (it->object)
8309 && it->method == GET_FROM_BUFFER
8310 && (((!it->bidi_p
8311 /* When the iterator is at base embedding level, we
8312 are guaranteed that characters are delivered for
8313 display in strictly increasing order of their
8314 buffer positions. */
8315 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8316 && IT_CHARPOS (*it) > to_charpos)
8317 || (it->bidi_p
8318 && (prev_method == GET_FROM_IMAGE
8319 || prev_method == GET_FROM_STRETCH
8320 || prev_method == GET_FROM_STRING)
8321 /* Passed TO_CHARPOS from left to right. */
8322 && ((prev_pos < to_charpos
8323 && IT_CHARPOS (*it) > to_charpos)
8324 /* Passed TO_CHARPOS from right to left. */
8325 || (prev_pos > to_charpos
8326 && IT_CHARPOS (*it) < to_charpos)))))
8328 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8330 result = MOVE_POS_MATCH_OR_ZV;
8331 break;
8333 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8334 /* If wrap_it is valid, the current position might be in a
8335 word that is wrapped. So, save the iterator in
8336 atpos_it and continue to see if wrapping happens. */
8337 SAVE_IT (atpos_it, *it, atpos_data);
8340 /* Stop when ZV reached.
8341 We used to stop here when TO_CHARPOS reached as well, but that is
8342 too soon if this glyph does not fit on this line. So we handle it
8343 explicitly below. */
8344 if (!get_next_display_element (it))
8346 result = MOVE_POS_MATCH_OR_ZV;
8347 break;
8350 if (it->line_wrap == TRUNCATE)
8352 if (BUFFER_POS_REACHED_P ())
8354 result = MOVE_POS_MATCH_OR_ZV;
8355 break;
8358 else
8360 if (it->line_wrap == WORD_WRAP)
8362 if (IT_DISPLAYING_WHITESPACE (it))
8363 may_wrap = 1;
8364 else if (may_wrap)
8366 /* We have reached a glyph that follows one or more
8367 whitespace characters. If the position is
8368 already found, we are done. */
8369 if (atpos_it.sp >= 0)
8371 RESTORE_IT (it, &atpos_it, atpos_data);
8372 result = MOVE_POS_MATCH_OR_ZV;
8373 goto done;
8375 if (atx_it.sp >= 0)
8377 RESTORE_IT (it, &atx_it, atx_data);
8378 result = MOVE_X_REACHED;
8379 goto done;
8381 /* Otherwise, we can wrap here. */
8382 SAVE_IT (wrap_it, *it, wrap_data);
8383 may_wrap = 0;
8388 /* Remember the line height for the current line, in case
8389 the next element doesn't fit on the line. */
8390 ascent = it->max_ascent;
8391 descent = it->max_descent;
8393 /* The call to produce_glyphs will get the metrics of the
8394 display element IT is loaded with. Record the x-position
8395 before this display element, in case it doesn't fit on the
8396 line. */
8397 x = it->current_x;
8399 PRODUCE_GLYPHS (it);
8401 if (it->area != TEXT_AREA)
8403 prev_method = it->method;
8404 if (it->method == GET_FROM_BUFFER)
8405 prev_pos = IT_CHARPOS (*it);
8406 set_iterator_to_next (it, 1);
8407 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8408 SET_TEXT_POS (this_line_min_pos,
8409 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8410 if (it->bidi_p
8411 && (op & MOVE_TO_POS)
8412 && IT_CHARPOS (*it) > to_charpos
8413 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8414 SAVE_IT (ppos_it, *it, ppos_data);
8415 continue;
8418 /* The number of glyphs we get back in IT->nglyphs will normally
8419 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8420 character on a terminal frame, or (iii) a line end. For the
8421 second case, IT->nglyphs - 1 padding glyphs will be present.
8422 (On X frames, there is only one glyph produced for a
8423 composite character.)
8425 The behavior implemented below means, for continuation lines,
8426 that as many spaces of a TAB as fit on the current line are
8427 displayed there. For terminal frames, as many glyphs of a
8428 multi-glyph character are displayed in the current line, too.
8429 This is what the old redisplay code did, and we keep it that
8430 way. Under X, the whole shape of a complex character must
8431 fit on the line or it will be completely displayed in the
8432 next line.
8434 Note that both for tabs and padding glyphs, all glyphs have
8435 the same width. */
8436 if (it->nglyphs)
8438 /* More than one glyph or glyph doesn't fit on line. All
8439 glyphs have the same width. */
8440 int single_glyph_width = it->pixel_width / it->nglyphs;
8441 int new_x;
8442 int x_before_this_char = x;
8443 int hpos_before_this_char = it->hpos;
8445 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8447 new_x = x + single_glyph_width;
8449 /* We want to leave anything reaching TO_X to the caller. */
8450 if ((op & MOVE_TO_X) && new_x > to_x)
8452 if (BUFFER_POS_REACHED_P ())
8454 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8455 goto buffer_pos_reached;
8456 if (atpos_it.sp < 0)
8458 SAVE_IT (atpos_it, *it, atpos_data);
8459 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8462 else
8464 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8466 it->current_x = x;
8467 result = MOVE_X_REACHED;
8468 break;
8470 if (atx_it.sp < 0)
8472 SAVE_IT (atx_it, *it, atx_data);
8473 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8478 if (/* Lines are continued. */
8479 it->line_wrap != TRUNCATE
8480 && (/* And glyph doesn't fit on the line. */
8481 new_x > it->last_visible_x
8482 /* Or it fits exactly and we're on a window
8483 system frame. */
8484 || (new_x == it->last_visible_x
8485 && FRAME_WINDOW_P (it->f)
8486 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8487 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8488 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8490 if (/* IT->hpos == 0 means the very first glyph
8491 doesn't fit on the line, e.g. a wide image. */
8492 it->hpos == 0
8493 || (new_x == it->last_visible_x
8494 && FRAME_WINDOW_P (it->f)))
8496 ++it->hpos;
8497 it->current_x = new_x;
8499 /* The character's last glyph just barely fits
8500 in this row. */
8501 if (i == it->nglyphs - 1)
8503 /* If this is the destination position,
8504 return a position *before* it in this row,
8505 now that we know it fits in this row. */
8506 if (BUFFER_POS_REACHED_P ())
8508 if (it->line_wrap != WORD_WRAP
8509 || wrap_it.sp < 0)
8511 it->hpos = hpos_before_this_char;
8512 it->current_x = x_before_this_char;
8513 result = MOVE_POS_MATCH_OR_ZV;
8514 break;
8516 if (it->line_wrap == WORD_WRAP
8517 && atpos_it.sp < 0)
8519 SAVE_IT (atpos_it, *it, atpos_data);
8520 atpos_it.current_x = x_before_this_char;
8521 atpos_it.hpos = hpos_before_this_char;
8525 prev_method = it->method;
8526 if (it->method == GET_FROM_BUFFER)
8527 prev_pos = IT_CHARPOS (*it);
8528 set_iterator_to_next (it, 1);
8529 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8530 SET_TEXT_POS (this_line_min_pos,
8531 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8532 /* On graphical terminals, newlines may
8533 "overflow" into the fringe if
8534 overflow-newline-into-fringe is non-nil.
8535 On text terminals, and on graphical
8536 terminals with no right margin, newlines
8537 may overflow into the last glyph on the
8538 display line.*/
8539 if (!FRAME_WINDOW_P (it->f)
8540 || ((it->bidi_p
8541 && it->bidi_it.paragraph_dir == R2L)
8542 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8543 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8544 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8546 if (!get_next_display_element (it))
8548 result = MOVE_POS_MATCH_OR_ZV;
8549 break;
8551 if (BUFFER_POS_REACHED_P ())
8553 if (ITERATOR_AT_END_OF_LINE_P (it))
8554 result = MOVE_POS_MATCH_OR_ZV;
8555 else
8556 result = MOVE_LINE_CONTINUED;
8557 break;
8559 if (ITERATOR_AT_END_OF_LINE_P (it)
8560 && (it->line_wrap != WORD_WRAP
8561 || wrap_it.sp < 0))
8563 result = MOVE_NEWLINE_OR_CR;
8564 break;
8569 else
8570 IT_RESET_X_ASCENT_DESCENT (it);
8572 if (wrap_it.sp >= 0)
8574 RESTORE_IT (it, &wrap_it, wrap_data);
8575 atpos_it.sp = -1;
8576 atx_it.sp = -1;
8579 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8580 IT_CHARPOS (*it)));
8581 result = MOVE_LINE_CONTINUED;
8582 break;
8585 if (BUFFER_POS_REACHED_P ())
8587 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8588 goto buffer_pos_reached;
8589 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8591 SAVE_IT (atpos_it, *it, atpos_data);
8592 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8596 if (new_x > it->first_visible_x)
8598 /* Glyph is visible. Increment number of glyphs that
8599 would be displayed. */
8600 ++it->hpos;
8604 if (result != MOVE_UNDEFINED)
8605 break;
8607 else if (BUFFER_POS_REACHED_P ())
8609 buffer_pos_reached:
8610 IT_RESET_X_ASCENT_DESCENT (it);
8611 result = MOVE_POS_MATCH_OR_ZV;
8612 break;
8614 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8616 /* Stop when TO_X specified and reached. This check is
8617 necessary here because of lines consisting of a line end,
8618 only. The line end will not produce any glyphs and we
8619 would never get MOVE_X_REACHED. */
8620 eassert (it->nglyphs == 0);
8621 result = MOVE_X_REACHED;
8622 break;
8625 /* Is this a line end? If yes, we're done. */
8626 if (ITERATOR_AT_END_OF_LINE_P (it))
8628 /* If we are past TO_CHARPOS, but never saw any character
8629 positions smaller than TO_CHARPOS, return
8630 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8631 did. */
8632 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8634 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8636 if (IT_CHARPOS (ppos_it) < ZV)
8638 RESTORE_IT (it, &ppos_it, ppos_data);
8639 result = MOVE_POS_MATCH_OR_ZV;
8641 else
8642 goto buffer_pos_reached;
8644 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8645 && IT_CHARPOS (*it) > to_charpos)
8646 goto buffer_pos_reached;
8647 else
8648 result = MOVE_NEWLINE_OR_CR;
8650 else
8651 result = MOVE_NEWLINE_OR_CR;
8652 break;
8655 prev_method = it->method;
8656 if (it->method == GET_FROM_BUFFER)
8657 prev_pos = IT_CHARPOS (*it);
8658 /* The current display element has been consumed. Advance
8659 to the next. */
8660 set_iterator_to_next (it, 1);
8661 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8662 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8663 if (IT_CHARPOS (*it) < to_charpos)
8664 saw_smaller_pos = 1;
8665 if (it->bidi_p
8666 && (op & MOVE_TO_POS)
8667 && IT_CHARPOS (*it) >= to_charpos
8668 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8669 SAVE_IT (ppos_it, *it, ppos_data);
8671 /* Stop if lines are truncated and IT's current x-position is
8672 past the right edge of the window now. */
8673 if (it->line_wrap == TRUNCATE
8674 && it->current_x >= it->last_visible_x)
8676 if (!FRAME_WINDOW_P (it->f)
8677 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8678 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8679 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8680 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8682 int at_eob_p = 0;
8684 if ((at_eob_p = !get_next_display_element (it))
8685 || BUFFER_POS_REACHED_P ()
8686 /* If we are past TO_CHARPOS, but never saw any
8687 character positions smaller than TO_CHARPOS,
8688 return MOVE_POS_MATCH_OR_ZV, like the
8689 unidirectional display did. */
8690 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8691 && !saw_smaller_pos
8692 && IT_CHARPOS (*it) > to_charpos))
8694 if (it->bidi_p
8695 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8696 RESTORE_IT (it, &ppos_it, ppos_data);
8697 result = MOVE_POS_MATCH_OR_ZV;
8698 break;
8700 if (ITERATOR_AT_END_OF_LINE_P (it))
8702 result = MOVE_NEWLINE_OR_CR;
8703 break;
8706 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8707 && !saw_smaller_pos
8708 && IT_CHARPOS (*it) > to_charpos)
8710 if (IT_CHARPOS (ppos_it) < ZV)
8711 RESTORE_IT (it, &ppos_it, ppos_data);
8712 result = MOVE_POS_MATCH_OR_ZV;
8713 break;
8715 result = MOVE_LINE_TRUNCATED;
8716 break;
8718 #undef IT_RESET_X_ASCENT_DESCENT
8721 #undef BUFFER_POS_REACHED_P
8723 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8724 restore the saved iterator. */
8725 if (atpos_it.sp >= 0)
8726 RESTORE_IT (it, &atpos_it, atpos_data);
8727 else if (atx_it.sp >= 0)
8728 RESTORE_IT (it, &atx_it, atx_data);
8730 done:
8732 if (atpos_data)
8733 bidi_unshelve_cache (atpos_data, 1);
8734 if (atx_data)
8735 bidi_unshelve_cache (atx_data, 1);
8736 if (wrap_data)
8737 bidi_unshelve_cache (wrap_data, 1);
8738 if (ppos_data)
8739 bidi_unshelve_cache (ppos_data, 1);
8741 /* Restore the iterator settings altered at the beginning of this
8742 function. */
8743 it->glyph_row = saved_glyph_row;
8744 return result;
8747 /* For external use. */
8748 void
8749 move_it_in_display_line (struct it *it,
8750 ptrdiff_t to_charpos, int to_x,
8751 enum move_operation_enum op)
8753 if (it->line_wrap == WORD_WRAP
8754 && (op & MOVE_TO_X))
8756 struct it save_it;
8757 void *save_data = NULL;
8758 int skip;
8760 SAVE_IT (save_it, *it, save_data);
8761 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8762 /* When word-wrap is on, TO_X may lie past the end
8763 of a wrapped line. Then it->current is the
8764 character on the next line, so backtrack to the
8765 space before the wrap point. */
8766 if (skip == MOVE_LINE_CONTINUED)
8768 int prev_x = max (it->current_x - 1, 0);
8769 RESTORE_IT (it, &save_it, save_data);
8770 move_it_in_display_line_to
8771 (it, -1, prev_x, MOVE_TO_X);
8773 else
8774 bidi_unshelve_cache (save_data, 1);
8776 else
8777 move_it_in_display_line_to (it, to_charpos, to_x, op);
8781 /* Move IT forward until it satisfies one or more of the criteria in
8782 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8784 OP is a bit-mask that specifies where to stop, and in particular,
8785 which of those four position arguments makes a difference. See the
8786 description of enum move_operation_enum.
8788 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8789 screen line, this function will set IT to the next position that is
8790 displayed to the right of TO_CHARPOS on the screen. */
8792 void
8793 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8795 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8796 int line_height, line_start_x = 0, reached = 0;
8797 void *backup_data = NULL;
8799 for (;;)
8801 if (op & MOVE_TO_VPOS)
8803 /* If no TO_CHARPOS and no TO_X specified, stop at the
8804 start of the line TO_VPOS. */
8805 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8807 if (it->vpos == to_vpos)
8809 reached = 1;
8810 break;
8812 else
8813 skip = move_it_in_display_line_to (it, -1, -1, 0);
8815 else
8817 /* TO_VPOS >= 0 means stop at TO_X in the line at
8818 TO_VPOS, or at TO_POS, whichever comes first. */
8819 if (it->vpos == to_vpos)
8821 reached = 2;
8822 break;
8825 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8827 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8829 reached = 3;
8830 break;
8832 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8834 /* We have reached TO_X but not in the line we want. */
8835 skip = move_it_in_display_line_to (it, to_charpos,
8836 -1, MOVE_TO_POS);
8837 if (skip == MOVE_POS_MATCH_OR_ZV)
8839 reached = 4;
8840 break;
8845 else if (op & MOVE_TO_Y)
8847 struct it it_backup;
8849 if (it->line_wrap == WORD_WRAP)
8850 SAVE_IT (it_backup, *it, backup_data);
8852 /* TO_Y specified means stop at TO_X in the line containing
8853 TO_Y---or at TO_CHARPOS if this is reached first. The
8854 problem is that we can't really tell whether the line
8855 contains TO_Y before we have completely scanned it, and
8856 this may skip past TO_X. What we do is to first scan to
8857 TO_X.
8859 If TO_X is not specified, use a TO_X of zero. The reason
8860 is to make the outcome of this function more predictable.
8861 If we didn't use TO_X == 0, we would stop at the end of
8862 the line which is probably not what a caller would expect
8863 to happen. */
8864 skip = move_it_in_display_line_to
8865 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8866 (MOVE_TO_X | (op & MOVE_TO_POS)));
8868 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8869 if (skip == MOVE_POS_MATCH_OR_ZV)
8870 reached = 5;
8871 else if (skip == MOVE_X_REACHED)
8873 /* If TO_X was reached, we want to know whether TO_Y is
8874 in the line. We know this is the case if the already
8875 scanned glyphs make the line tall enough. Otherwise,
8876 we must check by scanning the rest of the line. */
8877 line_height = it->max_ascent + it->max_descent;
8878 if (to_y >= it->current_y
8879 && to_y < it->current_y + line_height)
8881 reached = 6;
8882 break;
8884 SAVE_IT (it_backup, *it, backup_data);
8885 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8886 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8887 op & MOVE_TO_POS);
8888 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8889 line_height = it->max_ascent + it->max_descent;
8890 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8892 if (to_y >= it->current_y
8893 && to_y < it->current_y + line_height)
8895 /* If TO_Y is in this line and TO_X was reached
8896 above, we scanned too far. We have to restore
8897 IT's settings to the ones before skipping. But
8898 keep the more accurate values of max_ascent and
8899 max_descent we've found while skipping the rest
8900 of the line, for the sake of callers, such as
8901 pos_visible_p, that need to know the line
8902 height. */
8903 int max_ascent = it->max_ascent;
8904 int max_descent = it->max_descent;
8906 RESTORE_IT (it, &it_backup, backup_data);
8907 it->max_ascent = max_ascent;
8908 it->max_descent = max_descent;
8909 reached = 6;
8911 else
8913 skip = skip2;
8914 if (skip == MOVE_POS_MATCH_OR_ZV)
8915 reached = 7;
8918 else
8920 /* Check whether TO_Y is in this line. */
8921 line_height = it->max_ascent + it->max_descent;
8922 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8924 if (to_y >= it->current_y
8925 && to_y < it->current_y + line_height)
8927 /* When word-wrap is on, TO_X may lie past the end
8928 of a wrapped line. Then it->current is the
8929 character on the next line, so backtrack to the
8930 space before the wrap point. */
8931 if (skip == MOVE_LINE_CONTINUED
8932 && it->line_wrap == WORD_WRAP)
8934 int prev_x = max (it->current_x - 1, 0);
8935 RESTORE_IT (it, &it_backup, backup_data);
8936 skip = move_it_in_display_line_to
8937 (it, -1, prev_x, MOVE_TO_X);
8939 reached = 6;
8943 if (reached)
8944 break;
8946 else if (BUFFERP (it->object)
8947 && (it->method == GET_FROM_BUFFER
8948 || it->method == GET_FROM_STRETCH)
8949 && IT_CHARPOS (*it) >= to_charpos
8950 /* Under bidi iteration, a call to set_iterator_to_next
8951 can scan far beyond to_charpos if the initial
8952 portion of the next line needs to be reordered. In
8953 that case, give move_it_in_display_line_to another
8954 chance below. */
8955 && !(it->bidi_p
8956 && it->bidi_it.scan_dir == -1))
8957 skip = MOVE_POS_MATCH_OR_ZV;
8958 else
8959 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8961 switch (skip)
8963 case MOVE_POS_MATCH_OR_ZV:
8964 reached = 8;
8965 goto out;
8967 case MOVE_NEWLINE_OR_CR:
8968 set_iterator_to_next (it, 1);
8969 it->continuation_lines_width = 0;
8970 break;
8972 case MOVE_LINE_TRUNCATED:
8973 it->continuation_lines_width = 0;
8974 reseat_at_next_visible_line_start (it, 0);
8975 if ((op & MOVE_TO_POS) != 0
8976 && IT_CHARPOS (*it) > to_charpos)
8978 reached = 9;
8979 goto out;
8981 break;
8983 case MOVE_LINE_CONTINUED:
8984 /* For continued lines ending in a tab, some of the glyphs
8985 associated with the tab are displayed on the current
8986 line. Since it->current_x does not include these glyphs,
8987 we use it->last_visible_x instead. */
8988 if (it->c == '\t')
8990 it->continuation_lines_width += it->last_visible_x;
8991 /* When moving by vpos, ensure that the iterator really
8992 advances to the next line (bug#847, bug#969). Fixme:
8993 do we need to do this in other circumstances? */
8994 if (it->current_x != it->last_visible_x
8995 && (op & MOVE_TO_VPOS)
8996 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8998 line_start_x = it->current_x + it->pixel_width
8999 - it->last_visible_x;
9000 set_iterator_to_next (it, 0);
9003 else
9004 it->continuation_lines_width += it->current_x;
9005 break;
9007 default:
9008 emacs_abort ();
9011 /* Reset/increment for the next run. */
9012 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9013 it->current_x = line_start_x;
9014 line_start_x = 0;
9015 it->hpos = 0;
9016 it->current_y += it->max_ascent + it->max_descent;
9017 ++it->vpos;
9018 last_height = it->max_ascent + it->max_descent;
9019 it->max_ascent = it->max_descent = 0;
9022 out:
9024 /* On text terminals, we may stop at the end of a line in the middle
9025 of a multi-character glyph. If the glyph itself is continued,
9026 i.e. it is actually displayed on the next line, don't treat this
9027 stopping point as valid; move to the next line instead (unless
9028 that brings us offscreen). */
9029 if (!FRAME_WINDOW_P (it->f)
9030 && op & MOVE_TO_POS
9031 && IT_CHARPOS (*it) == to_charpos
9032 && it->what == IT_CHARACTER
9033 && it->nglyphs > 1
9034 && it->line_wrap == WINDOW_WRAP
9035 && it->current_x == it->last_visible_x - 1
9036 && it->c != '\n'
9037 && it->c != '\t'
9038 && it->vpos < it->w->window_end_vpos)
9040 it->continuation_lines_width += it->current_x;
9041 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9042 it->current_y += it->max_ascent + it->max_descent;
9043 ++it->vpos;
9044 last_height = it->max_ascent + it->max_descent;
9047 if (backup_data)
9048 bidi_unshelve_cache (backup_data, 1);
9050 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9054 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9056 If DY > 0, move IT backward at least that many pixels. DY = 0
9057 means move IT backward to the preceding line start or BEGV. This
9058 function may move over more than DY pixels if IT->current_y - DY
9059 ends up in the middle of a line; in this case IT->current_y will be
9060 set to the top of the line moved to. */
9062 void
9063 move_it_vertically_backward (struct it *it, int dy)
9065 int nlines, h;
9066 struct it it2, it3;
9067 void *it2data = NULL, *it3data = NULL;
9068 ptrdiff_t start_pos;
9069 int nchars_per_row
9070 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9071 ptrdiff_t pos_limit;
9073 move_further_back:
9074 eassert (dy >= 0);
9076 start_pos = IT_CHARPOS (*it);
9078 /* Estimate how many newlines we must move back. */
9079 nlines = max (1, dy / default_line_pixel_height (it->w));
9080 if (it->line_wrap == TRUNCATE)
9081 pos_limit = BEGV;
9082 else
9083 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9085 /* Set the iterator's position that many lines back. But don't go
9086 back more than NLINES full screen lines -- this wins a day with
9087 buffers which have very long lines. */
9088 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9089 back_to_previous_visible_line_start (it);
9091 /* Reseat the iterator here. When moving backward, we don't want
9092 reseat to skip forward over invisible text, set up the iterator
9093 to deliver from overlay strings at the new position etc. So,
9094 use reseat_1 here. */
9095 reseat_1 (it, it->current.pos, 1);
9097 /* We are now surely at a line start. */
9098 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9099 reordering is in effect. */
9100 it->continuation_lines_width = 0;
9102 /* Move forward and see what y-distance we moved. First move to the
9103 start of the next line so that we get its height. We need this
9104 height to be able to tell whether we reached the specified
9105 y-distance. */
9106 SAVE_IT (it2, *it, it2data);
9107 it2.max_ascent = it2.max_descent = 0;
9110 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9111 MOVE_TO_POS | MOVE_TO_VPOS);
9113 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9114 /* If we are in a display string which starts at START_POS,
9115 and that display string includes a newline, and we are
9116 right after that newline (i.e. at the beginning of a
9117 display line), exit the loop, because otherwise we will
9118 infloop, since move_it_to will see that it is already at
9119 START_POS and will not move. */
9120 || (it2.method == GET_FROM_STRING
9121 && IT_CHARPOS (it2) == start_pos
9122 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9123 eassert (IT_CHARPOS (*it) >= BEGV);
9124 SAVE_IT (it3, it2, it3data);
9126 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9127 eassert (IT_CHARPOS (*it) >= BEGV);
9128 /* H is the actual vertical distance from the position in *IT
9129 and the starting position. */
9130 h = it2.current_y - it->current_y;
9131 /* NLINES is the distance in number of lines. */
9132 nlines = it2.vpos - it->vpos;
9134 /* Correct IT's y and vpos position
9135 so that they are relative to the starting point. */
9136 it->vpos -= nlines;
9137 it->current_y -= h;
9139 if (dy == 0)
9141 /* DY == 0 means move to the start of the screen line. The
9142 value of nlines is > 0 if continuation lines were involved,
9143 or if the original IT position was at start of a line. */
9144 RESTORE_IT (it, it, it2data);
9145 if (nlines > 0)
9146 move_it_by_lines (it, nlines);
9147 /* The above code moves us to some position NLINES down,
9148 usually to its first glyph (leftmost in an L2R line), but
9149 that's not necessarily the start of the line, under bidi
9150 reordering. We want to get to the character position
9151 that is immediately after the newline of the previous
9152 line. */
9153 if (it->bidi_p
9154 && !it->continuation_lines_width
9155 && !STRINGP (it->string)
9156 && IT_CHARPOS (*it) > BEGV
9157 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9159 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9161 DEC_BOTH (cp, bp);
9162 cp = find_newline_no_quit (cp, bp, -1, NULL);
9163 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9165 bidi_unshelve_cache (it3data, 1);
9167 else
9169 /* The y-position we try to reach, relative to *IT.
9170 Note that H has been subtracted in front of the if-statement. */
9171 int target_y = it->current_y + h - dy;
9172 int y0 = it3.current_y;
9173 int y1;
9174 int line_height;
9176 RESTORE_IT (&it3, &it3, it3data);
9177 y1 = line_bottom_y (&it3);
9178 line_height = y1 - y0;
9179 RESTORE_IT (it, it, it2data);
9180 /* If we did not reach target_y, try to move further backward if
9181 we can. If we moved too far backward, try to move forward. */
9182 if (target_y < it->current_y
9183 /* This is heuristic. In a window that's 3 lines high, with
9184 a line height of 13 pixels each, recentering with point
9185 on the bottom line will try to move -39/2 = 19 pixels
9186 backward. Try to avoid moving into the first line. */
9187 && (it->current_y - target_y
9188 > min (window_box_height (it->w), line_height * 2 / 3))
9189 && IT_CHARPOS (*it) > BEGV)
9191 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9192 target_y - it->current_y));
9193 dy = it->current_y - target_y;
9194 goto move_further_back;
9196 else if (target_y >= it->current_y + line_height
9197 && IT_CHARPOS (*it) < ZV)
9199 /* Should move forward by at least one line, maybe more.
9201 Note: Calling move_it_by_lines can be expensive on
9202 terminal frames, where compute_motion is used (via
9203 vmotion) to do the job, when there are very long lines
9204 and truncate-lines is nil. That's the reason for
9205 treating terminal frames specially here. */
9207 if (!FRAME_WINDOW_P (it->f))
9208 move_it_vertically (it, target_y - (it->current_y + line_height));
9209 else
9213 move_it_by_lines (it, 1);
9215 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9222 /* Move IT by a specified amount of pixel lines DY. DY negative means
9223 move backwards. DY = 0 means move to start of screen line. At the
9224 end, IT will be on the start of a screen line. */
9226 void
9227 move_it_vertically (struct it *it, int dy)
9229 if (dy <= 0)
9230 move_it_vertically_backward (it, -dy);
9231 else
9233 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9234 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9235 MOVE_TO_POS | MOVE_TO_Y);
9236 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9238 /* If buffer ends in ZV without a newline, move to the start of
9239 the line to satisfy the post-condition. */
9240 if (IT_CHARPOS (*it) == ZV
9241 && ZV > BEGV
9242 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9243 move_it_by_lines (it, 0);
9248 /* Move iterator IT past the end of the text line it is in. */
9250 void
9251 move_it_past_eol (struct it *it)
9253 enum move_it_result rc;
9255 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9256 if (rc == MOVE_NEWLINE_OR_CR)
9257 set_iterator_to_next (it, 0);
9261 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9262 negative means move up. DVPOS == 0 means move to the start of the
9263 screen line.
9265 Optimization idea: If we would know that IT->f doesn't use
9266 a face with proportional font, we could be faster for
9267 truncate-lines nil. */
9269 void
9270 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9273 /* The commented-out optimization uses vmotion on terminals. This
9274 gives bad results, because elements like it->what, on which
9275 callers such as pos_visible_p rely, aren't updated. */
9276 /* struct position pos;
9277 if (!FRAME_WINDOW_P (it->f))
9279 struct text_pos textpos;
9281 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9282 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9283 reseat (it, textpos, 1);
9284 it->vpos += pos.vpos;
9285 it->current_y += pos.vpos;
9287 else */
9289 if (dvpos == 0)
9291 /* DVPOS == 0 means move to the start of the screen line. */
9292 move_it_vertically_backward (it, 0);
9293 /* Let next call to line_bottom_y calculate real line height */
9294 last_height = 0;
9296 else if (dvpos > 0)
9298 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9299 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9301 /* Only move to the next buffer position if we ended up in a
9302 string from display property, not in an overlay string
9303 (before-string or after-string). That is because the
9304 latter don't conceal the underlying buffer position, so
9305 we can ask to move the iterator to the exact position we
9306 are interested in. Note that, even if we are already at
9307 IT_CHARPOS (*it), the call below is not a no-op, as it
9308 will detect that we are at the end of the string, pop the
9309 iterator, and compute it->current_x and it->hpos
9310 correctly. */
9311 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9312 -1, -1, -1, MOVE_TO_POS);
9315 else
9317 struct it it2;
9318 void *it2data = NULL;
9319 ptrdiff_t start_charpos, i;
9320 int nchars_per_row
9321 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9322 ptrdiff_t pos_limit;
9324 /* Start at the beginning of the screen line containing IT's
9325 position. This may actually move vertically backwards,
9326 in case of overlays, so adjust dvpos accordingly. */
9327 dvpos += it->vpos;
9328 move_it_vertically_backward (it, 0);
9329 dvpos -= it->vpos;
9331 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9332 screen lines, and reseat the iterator there. */
9333 start_charpos = IT_CHARPOS (*it);
9334 if (it->line_wrap == TRUNCATE)
9335 pos_limit = BEGV;
9336 else
9337 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9338 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9339 back_to_previous_visible_line_start (it);
9340 reseat (it, it->current.pos, 1);
9342 /* Move further back if we end up in a string or an image. */
9343 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9345 /* First try to move to start of display line. */
9346 dvpos += it->vpos;
9347 move_it_vertically_backward (it, 0);
9348 dvpos -= it->vpos;
9349 if (IT_POS_VALID_AFTER_MOVE_P (it))
9350 break;
9351 /* If start of line is still in string or image,
9352 move further back. */
9353 back_to_previous_visible_line_start (it);
9354 reseat (it, it->current.pos, 1);
9355 dvpos--;
9358 it->current_x = it->hpos = 0;
9360 /* Above call may have moved too far if continuation lines
9361 are involved. Scan forward and see if it did. */
9362 SAVE_IT (it2, *it, it2data);
9363 it2.vpos = it2.current_y = 0;
9364 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9365 it->vpos -= it2.vpos;
9366 it->current_y -= it2.current_y;
9367 it->current_x = it->hpos = 0;
9369 /* If we moved too far back, move IT some lines forward. */
9370 if (it2.vpos > -dvpos)
9372 int delta = it2.vpos + dvpos;
9374 RESTORE_IT (&it2, &it2, it2data);
9375 SAVE_IT (it2, *it, it2data);
9376 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9377 /* Move back again if we got too far ahead. */
9378 if (IT_CHARPOS (*it) >= start_charpos)
9379 RESTORE_IT (it, &it2, it2data);
9380 else
9381 bidi_unshelve_cache (it2data, 1);
9383 else
9384 RESTORE_IT (it, it, it2data);
9388 /* Return 1 if IT points into the middle of a display vector. */
9391 in_display_vector_p (struct it *it)
9393 return (it->method == GET_FROM_DISPLAY_VECTOR
9394 && it->current.dpvec_index > 0
9395 && it->dpvec + it->current.dpvec_index != it->dpend);
9399 /***********************************************************************
9400 Messages
9401 ***********************************************************************/
9404 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9405 to *Messages*. */
9407 void
9408 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9410 Lisp_Object args[3];
9411 Lisp_Object msg, fmt;
9412 char *buffer;
9413 ptrdiff_t len;
9414 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9415 USE_SAFE_ALLOCA;
9417 fmt = msg = Qnil;
9418 GCPRO4 (fmt, msg, arg1, arg2);
9420 args[0] = fmt = build_string (format);
9421 args[1] = arg1;
9422 args[2] = arg2;
9423 msg = Fformat (3, args);
9425 len = SBYTES (msg) + 1;
9426 buffer = SAFE_ALLOCA (len);
9427 memcpy (buffer, SDATA (msg), len);
9429 message_dolog (buffer, len - 1, 1, 0);
9430 SAFE_FREE ();
9432 UNGCPRO;
9436 /* Output a newline in the *Messages* buffer if "needs" one. */
9438 void
9439 message_log_maybe_newline (void)
9441 if (message_log_need_newline)
9442 message_dolog ("", 0, 1, 0);
9446 /* Add a string M of length NBYTES to the message log, optionally
9447 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9448 true, means interpret the contents of M as multibyte. This
9449 function calls low-level routines in order to bypass text property
9450 hooks, etc. which might not be safe to run.
9452 This may GC (insert may run before/after change hooks),
9453 so the buffer M must NOT point to a Lisp string. */
9455 void
9456 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9458 const unsigned char *msg = (const unsigned char *) m;
9460 if (!NILP (Vmemory_full))
9461 return;
9463 if (!NILP (Vmessage_log_max))
9465 struct buffer *oldbuf;
9466 Lisp_Object oldpoint, oldbegv, oldzv;
9467 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9468 ptrdiff_t point_at_end = 0;
9469 ptrdiff_t zv_at_end = 0;
9470 Lisp_Object old_deactivate_mark;
9471 bool shown;
9472 struct gcpro gcpro1;
9474 old_deactivate_mark = Vdeactivate_mark;
9475 oldbuf = current_buffer;
9477 /* Ensure the Messages buffer exists, and switch to it.
9478 If we created it, set the major-mode. */
9480 int newbuffer = 0;
9481 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9483 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9485 if (newbuffer &&
9486 !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9487 call0 (intern ("messages-buffer-mode"));
9490 bset_undo_list (current_buffer, Qt);
9492 oldpoint = message_dolog_marker1;
9493 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9494 oldbegv = message_dolog_marker2;
9495 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9496 oldzv = message_dolog_marker3;
9497 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9498 GCPRO1 (old_deactivate_mark);
9500 if (PT == Z)
9501 point_at_end = 1;
9502 if (ZV == Z)
9503 zv_at_end = 1;
9505 BEGV = BEG;
9506 BEGV_BYTE = BEG_BYTE;
9507 ZV = Z;
9508 ZV_BYTE = Z_BYTE;
9509 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9511 /* Insert the string--maybe converting multibyte to single byte
9512 or vice versa, so that all the text fits the buffer. */
9513 if (multibyte
9514 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9516 ptrdiff_t i;
9517 int c, char_bytes;
9518 char work[1];
9520 /* Convert a multibyte string to single-byte
9521 for the *Message* buffer. */
9522 for (i = 0; i < nbytes; i += char_bytes)
9524 c = string_char_and_length (msg + i, &char_bytes);
9525 work[0] = (ASCII_CHAR_P (c)
9527 : multibyte_char_to_unibyte (c));
9528 insert_1_both (work, 1, 1, 1, 0, 0);
9531 else if (! multibyte
9532 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9534 ptrdiff_t i;
9535 int c, char_bytes;
9536 unsigned char str[MAX_MULTIBYTE_LENGTH];
9537 /* Convert a single-byte string to multibyte
9538 for the *Message* buffer. */
9539 for (i = 0; i < nbytes; i++)
9541 c = msg[i];
9542 MAKE_CHAR_MULTIBYTE (c);
9543 char_bytes = CHAR_STRING (c, str);
9544 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9547 else if (nbytes)
9548 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9550 if (nlflag)
9552 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9553 printmax_t dups;
9555 insert_1_both ("\n", 1, 1, 1, 0, 0);
9557 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9558 this_bol = PT;
9559 this_bol_byte = PT_BYTE;
9561 /* See if this line duplicates the previous one.
9562 If so, combine duplicates. */
9563 if (this_bol > BEG)
9565 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9566 prev_bol = PT;
9567 prev_bol_byte = PT_BYTE;
9569 dups = message_log_check_duplicate (prev_bol_byte,
9570 this_bol_byte);
9571 if (dups)
9573 del_range_both (prev_bol, prev_bol_byte,
9574 this_bol, this_bol_byte, 0);
9575 if (dups > 1)
9577 char dupstr[sizeof " [ times]"
9578 + INT_STRLEN_BOUND (printmax_t)];
9580 /* If you change this format, don't forget to also
9581 change message_log_check_duplicate. */
9582 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9583 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9584 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9589 /* If we have more than the desired maximum number of lines
9590 in the *Messages* buffer now, delete the oldest ones.
9591 This is safe because we don't have undo in this buffer. */
9593 if (NATNUMP (Vmessage_log_max))
9595 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9596 -XFASTINT (Vmessage_log_max) - 1, 0);
9597 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9600 BEGV = marker_position (oldbegv);
9601 BEGV_BYTE = marker_byte_position (oldbegv);
9603 if (zv_at_end)
9605 ZV = Z;
9606 ZV_BYTE = Z_BYTE;
9608 else
9610 ZV = marker_position (oldzv);
9611 ZV_BYTE = marker_byte_position (oldzv);
9614 if (point_at_end)
9615 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9616 else
9617 /* We can't do Fgoto_char (oldpoint) because it will run some
9618 Lisp code. */
9619 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9620 marker_byte_position (oldpoint));
9622 UNGCPRO;
9623 unchain_marker (XMARKER (oldpoint));
9624 unchain_marker (XMARKER (oldbegv));
9625 unchain_marker (XMARKER (oldzv));
9627 shown = buffer_window_count (current_buffer) > 0;
9628 set_buffer_internal (oldbuf);
9629 /* We called insert_1_both above with its 5th argument (PREPARE)
9630 zero, which prevents insert_1_both from calling
9631 prepare_to_modify_buffer, which in turns prevents us from
9632 incrementing windows_or_buffers_changed even if *Messages* is
9633 shown in some window. So we must manually incrementing
9634 windows_or_buffers_changed here to make up for that. */
9635 if (shown)
9636 windows_or_buffers_changed++;
9637 else
9638 windows_or_buffers_changed = old_windows_or_buffers_changed;
9639 message_log_need_newline = !nlflag;
9640 Vdeactivate_mark = old_deactivate_mark;
9645 /* We are at the end of the buffer after just having inserted a newline.
9646 (Note: We depend on the fact we won't be crossing the gap.)
9647 Check to see if the most recent message looks a lot like the previous one.
9648 Return 0 if different, 1 if the new one should just replace it, or a
9649 value N > 1 if we should also append " [N times]". */
9651 static intmax_t
9652 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9654 ptrdiff_t i;
9655 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9656 int seen_dots = 0;
9657 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9658 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9660 for (i = 0; i < len; i++)
9662 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9663 seen_dots = 1;
9664 if (p1[i] != p2[i])
9665 return seen_dots;
9667 p1 += len;
9668 if (*p1 == '\n')
9669 return 2;
9670 if (*p1++ == ' ' && *p1++ == '[')
9672 char *pend;
9673 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9674 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9675 return n + 1;
9677 return 0;
9681 /* Display an echo area message M with a specified length of NBYTES
9682 bytes. The string may include null characters. If M is not a
9683 string, clear out any existing message, and let the mini-buffer
9684 text show through.
9686 This function cancels echoing. */
9688 void
9689 message3 (Lisp_Object m)
9691 struct gcpro gcpro1;
9693 GCPRO1 (m);
9694 clear_message (1,1);
9695 cancel_echoing ();
9697 /* First flush out any partial line written with print. */
9698 message_log_maybe_newline ();
9699 if (STRINGP (m))
9701 ptrdiff_t nbytes = SBYTES (m);
9702 bool multibyte = STRING_MULTIBYTE (m);
9703 USE_SAFE_ALLOCA;
9704 char *buffer = SAFE_ALLOCA (nbytes);
9705 memcpy (buffer, SDATA (m), nbytes);
9706 message_dolog (buffer, nbytes, 1, multibyte);
9707 SAFE_FREE ();
9709 message3_nolog (m);
9711 UNGCPRO;
9715 /* The non-logging version of message3.
9716 This does not cancel echoing, because it is used for echoing.
9717 Perhaps we need to make a separate function for echoing
9718 and make this cancel echoing. */
9720 void
9721 message3_nolog (Lisp_Object m)
9723 struct frame *sf = SELECTED_FRAME ();
9725 if (FRAME_INITIAL_P (sf))
9727 if (noninteractive_need_newline)
9728 putc ('\n', stderr);
9729 noninteractive_need_newline = 0;
9730 if (STRINGP (m))
9731 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9732 if (cursor_in_echo_area == 0)
9733 fprintf (stderr, "\n");
9734 fflush (stderr);
9736 /* Error messages get reported properly by cmd_error, so this must be just an
9737 informative message; if the frame hasn't really been initialized yet, just
9738 toss it. */
9739 else if (INTERACTIVE && sf->glyphs_initialized_p)
9741 /* Get the frame containing the mini-buffer
9742 that the selected frame is using. */
9743 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9744 Lisp_Object frame = XWINDOW (mini_window)->frame;
9745 struct frame *f = XFRAME (frame);
9747 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9748 Fmake_frame_visible (frame);
9750 if (STRINGP (m) && SCHARS (m) > 0)
9752 set_message (m);
9753 if (minibuffer_auto_raise)
9754 Fraise_frame (frame);
9755 /* Assume we are not echoing.
9756 (If we are, echo_now will override this.) */
9757 echo_message_buffer = Qnil;
9759 else
9760 clear_message (1, 1);
9762 do_pending_window_change (0);
9763 echo_area_display (1);
9764 do_pending_window_change (0);
9765 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9766 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9771 /* Display a null-terminated echo area message M. If M is 0, clear
9772 out any existing message, and let the mini-buffer text show through.
9774 The buffer M must continue to exist until after the echo area gets
9775 cleared or some other message gets displayed there. Do not pass
9776 text that is stored in a Lisp string. Do not pass text in a buffer
9777 that was alloca'd. */
9779 void
9780 message1 (const char *m)
9782 message3 (m ? build_unibyte_string (m) : Qnil);
9786 /* The non-logging counterpart of message1. */
9788 void
9789 message1_nolog (const char *m)
9791 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9794 /* Display a message M which contains a single %s
9795 which gets replaced with STRING. */
9797 void
9798 message_with_string (const char *m, Lisp_Object string, int log)
9800 CHECK_STRING (string);
9802 if (noninteractive)
9804 if (m)
9806 if (noninteractive_need_newline)
9807 putc ('\n', stderr);
9808 noninteractive_need_newline = 0;
9809 fprintf (stderr, m, SDATA (string));
9810 if (!cursor_in_echo_area)
9811 fprintf (stderr, "\n");
9812 fflush (stderr);
9815 else if (INTERACTIVE)
9817 /* The frame whose minibuffer we're going to display the message on.
9818 It may be larger than the selected frame, so we need
9819 to use its buffer, not the selected frame's buffer. */
9820 Lisp_Object mini_window;
9821 struct frame *f, *sf = SELECTED_FRAME ();
9823 /* Get the frame containing the minibuffer
9824 that the selected frame is using. */
9825 mini_window = FRAME_MINIBUF_WINDOW (sf);
9826 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9828 /* Error messages get reported properly by cmd_error, so this must be
9829 just an informative message; if the frame hasn't really been
9830 initialized yet, just toss it. */
9831 if (f->glyphs_initialized_p)
9833 Lisp_Object args[2], msg;
9834 struct gcpro gcpro1, gcpro2;
9836 args[0] = build_string (m);
9837 args[1] = msg = string;
9838 GCPRO2 (args[0], msg);
9839 gcpro1.nvars = 2;
9841 msg = Fformat (2, args);
9843 if (log)
9844 message3 (msg);
9845 else
9846 message3_nolog (msg);
9848 UNGCPRO;
9850 /* Print should start at the beginning of the message
9851 buffer next time. */
9852 message_buf_print = 0;
9858 /* Dump an informative message to the minibuf. If M is 0, clear out
9859 any existing message, and let the mini-buffer text show through. */
9861 static void
9862 vmessage (const char *m, va_list ap)
9864 if (noninteractive)
9866 if (m)
9868 if (noninteractive_need_newline)
9869 putc ('\n', stderr);
9870 noninteractive_need_newline = 0;
9871 vfprintf (stderr, m, ap);
9872 if (cursor_in_echo_area == 0)
9873 fprintf (stderr, "\n");
9874 fflush (stderr);
9877 else if (INTERACTIVE)
9879 /* The frame whose mini-buffer we're going to display the message
9880 on. It may be larger than the selected frame, so we need to
9881 use its buffer, not the selected frame's buffer. */
9882 Lisp_Object mini_window;
9883 struct frame *f, *sf = SELECTED_FRAME ();
9885 /* Get the frame containing the mini-buffer
9886 that the selected frame is using. */
9887 mini_window = FRAME_MINIBUF_WINDOW (sf);
9888 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9890 /* Error messages get reported properly by cmd_error, so this must be
9891 just an informative message; if the frame hasn't really been
9892 initialized yet, just toss it. */
9893 if (f->glyphs_initialized_p)
9895 if (m)
9897 ptrdiff_t len;
9898 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9899 char *message_buf = alloca (maxsize + 1);
9901 len = doprnt (message_buf, maxsize, m, 0, ap);
9903 message3 (make_string (message_buf, len));
9905 else
9906 message1 (0);
9908 /* Print should start at the beginning of the message
9909 buffer next time. */
9910 message_buf_print = 0;
9915 void
9916 message (const char *m, ...)
9918 va_list ap;
9919 va_start (ap, m);
9920 vmessage (m, ap);
9921 va_end (ap);
9925 #if 0
9926 /* The non-logging version of message. */
9928 void
9929 message_nolog (const char *m, ...)
9931 Lisp_Object old_log_max;
9932 va_list ap;
9933 va_start (ap, m);
9934 old_log_max = Vmessage_log_max;
9935 Vmessage_log_max = Qnil;
9936 vmessage (m, ap);
9937 Vmessage_log_max = old_log_max;
9938 va_end (ap);
9940 #endif
9943 /* Display the current message in the current mini-buffer. This is
9944 only called from error handlers in process.c, and is not time
9945 critical. */
9947 void
9948 update_echo_area (void)
9950 if (!NILP (echo_area_buffer[0]))
9952 Lisp_Object string;
9953 string = Fcurrent_message ();
9954 message3 (string);
9959 /* Make sure echo area buffers in `echo_buffers' are live.
9960 If they aren't, make new ones. */
9962 static void
9963 ensure_echo_area_buffers (void)
9965 int i;
9967 for (i = 0; i < 2; ++i)
9968 if (!BUFFERP (echo_buffer[i])
9969 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9971 char name[30];
9972 Lisp_Object old_buffer;
9973 int j;
9975 old_buffer = echo_buffer[i];
9976 echo_buffer[i] = Fget_buffer_create
9977 (make_formatted_string (name, " *Echo Area %d*", i));
9978 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9979 /* to force word wrap in echo area -
9980 it was decided to postpone this*/
9981 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9983 for (j = 0; j < 2; ++j)
9984 if (EQ (old_buffer, echo_area_buffer[j]))
9985 echo_area_buffer[j] = echo_buffer[i];
9990 /* Call FN with args A1..A2 with either the current or last displayed
9991 echo_area_buffer as current buffer.
9993 WHICH zero means use the current message buffer
9994 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9995 from echo_buffer[] and clear it.
9997 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9998 suitable buffer from echo_buffer[] and clear it.
10000 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10001 that the current message becomes the last displayed one, make
10002 choose a suitable buffer for echo_area_buffer[0], and clear it.
10004 Value is what FN returns. */
10006 static int
10007 with_echo_area_buffer (struct window *w, int which,
10008 int (*fn) (ptrdiff_t, Lisp_Object),
10009 ptrdiff_t a1, Lisp_Object a2)
10011 Lisp_Object buffer;
10012 int this_one, the_other, clear_buffer_p, rc;
10013 ptrdiff_t count = SPECPDL_INDEX ();
10015 /* If buffers aren't live, make new ones. */
10016 ensure_echo_area_buffers ();
10018 clear_buffer_p = 0;
10020 if (which == 0)
10021 this_one = 0, the_other = 1;
10022 else if (which > 0)
10023 this_one = 1, the_other = 0;
10024 else
10026 this_one = 0, the_other = 1;
10027 clear_buffer_p = 1;
10029 /* We need a fresh one in case the current echo buffer equals
10030 the one containing the last displayed echo area message. */
10031 if (!NILP (echo_area_buffer[this_one])
10032 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10033 echo_area_buffer[this_one] = Qnil;
10036 /* Choose a suitable buffer from echo_buffer[] is we don't
10037 have one. */
10038 if (NILP (echo_area_buffer[this_one]))
10040 echo_area_buffer[this_one]
10041 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10042 ? echo_buffer[the_other]
10043 : echo_buffer[this_one]);
10044 clear_buffer_p = 1;
10047 buffer = echo_area_buffer[this_one];
10049 /* Don't get confused by reusing the buffer used for echoing
10050 for a different purpose. */
10051 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10052 cancel_echoing ();
10054 record_unwind_protect (unwind_with_echo_area_buffer,
10055 with_echo_area_buffer_unwind_data (w));
10057 /* Make the echo area buffer current. Note that for display
10058 purposes, it is not necessary that the displayed window's buffer
10059 == current_buffer, except for text property lookup. So, let's
10060 only set that buffer temporarily here without doing a full
10061 Fset_window_buffer. We must also change w->pointm, though,
10062 because otherwise an assertions in unshow_buffer fails, and Emacs
10063 aborts. */
10064 set_buffer_internal_1 (XBUFFER (buffer));
10065 if (w)
10067 wset_buffer (w, buffer);
10068 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10071 bset_undo_list (current_buffer, Qt);
10072 bset_read_only (current_buffer, Qnil);
10073 specbind (Qinhibit_read_only, Qt);
10074 specbind (Qinhibit_modification_hooks, Qt);
10076 if (clear_buffer_p && Z > BEG)
10077 del_range (BEG, Z);
10079 eassert (BEGV >= BEG);
10080 eassert (ZV <= Z && ZV >= BEGV);
10082 rc = fn (a1, a2);
10084 eassert (BEGV >= BEG);
10085 eassert (ZV <= Z && ZV >= BEGV);
10087 unbind_to (count, Qnil);
10088 return rc;
10092 /* Save state that should be preserved around the call to the function
10093 FN called in with_echo_area_buffer. */
10095 static Lisp_Object
10096 with_echo_area_buffer_unwind_data (struct window *w)
10098 int i = 0;
10099 Lisp_Object vector, tmp;
10101 /* Reduce consing by keeping one vector in
10102 Vwith_echo_area_save_vector. */
10103 vector = Vwith_echo_area_save_vector;
10104 Vwith_echo_area_save_vector = Qnil;
10106 if (NILP (vector))
10107 vector = Fmake_vector (make_number (9), Qnil);
10109 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10110 ASET (vector, i, Vdeactivate_mark); ++i;
10111 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10113 if (w)
10115 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10116 ASET (vector, i, w->contents); ++i;
10117 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10118 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10119 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10120 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10122 else
10124 int end = i + 6;
10125 for (; i < end; ++i)
10126 ASET (vector, i, Qnil);
10129 eassert (i == ASIZE (vector));
10130 return vector;
10134 /* Restore global state from VECTOR which was created by
10135 with_echo_area_buffer_unwind_data. */
10137 static void
10138 unwind_with_echo_area_buffer (Lisp_Object vector)
10140 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10141 Vdeactivate_mark = AREF (vector, 1);
10142 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10144 if (WINDOWP (AREF (vector, 3)))
10146 struct window *w;
10147 Lisp_Object buffer;
10149 w = XWINDOW (AREF (vector, 3));
10150 buffer = AREF (vector, 4);
10152 wset_buffer (w, buffer);
10153 set_marker_both (w->pointm, buffer,
10154 XFASTINT (AREF (vector, 5)),
10155 XFASTINT (AREF (vector, 6)));
10156 set_marker_both (w->start, buffer,
10157 XFASTINT (AREF (vector, 7)),
10158 XFASTINT (AREF (vector, 8)));
10161 Vwith_echo_area_save_vector = vector;
10165 /* Set up the echo area for use by print functions. MULTIBYTE_P
10166 non-zero means we will print multibyte. */
10168 void
10169 setup_echo_area_for_printing (int multibyte_p)
10171 /* If we can't find an echo area any more, exit. */
10172 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10173 Fkill_emacs (Qnil);
10175 ensure_echo_area_buffers ();
10177 if (!message_buf_print)
10179 /* A message has been output since the last time we printed.
10180 Choose a fresh echo area buffer. */
10181 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10182 echo_area_buffer[0] = echo_buffer[1];
10183 else
10184 echo_area_buffer[0] = echo_buffer[0];
10186 /* Switch to that buffer and clear it. */
10187 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10188 bset_truncate_lines (current_buffer, Qnil);
10190 if (Z > BEG)
10192 ptrdiff_t count = SPECPDL_INDEX ();
10193 specbind (Qinhibit_read_only, Qt);
10194 /* Note that undo recording is always disabled. */
10195 del_range (BEG, Z);
10196 unbind_to (count, Qnil);
10198 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10200 /* Set up the buffer for the multibyteness we need. */
10201 if (multibyte_p
10202 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10203 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10205 /* Raise the frame containing the echo area. */
10206 if (minibuffer_auto_raise)
10208 struct frame *sf = SELECTED_FRAME ();
10209 Lisp_Object mini_window;
10210 mini_window = FRAME_MINIBUF_WINDOW (sf);
10211 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10214 message_log_maybe_newline ();
10215 message_buf_print = 1;
10217 else
10219 if (NILP (echo_area_buffer[0]))
10221 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10222 echo_area_buffer[0] = echo_buffer[1];
10223 else
10224 echo_area_buffer[0] = echo_buffer[0];
10227 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10229 /* Someone switched buffers between print requests. */
10230 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10231 bset_truncate_lines (current_buffer, Qnil);
10237 /* Display an echo area message in window W. Value is non-zero if W's
10238 height is changed. If display_last_displayed_message_p is
10239 non-zero, display the message that was last displayed, otherwise
10240 display the current message. */
10242 static int
10243 display_echo_area (struct window *w)
10245 int i, no_message_p, window_height_changed_p;
10247 /* Temporarily disable garbage collections while displaying the echo
10248 area. This is done because a GC can print a message itself.
10249 That message would modify the echo area buffer's contents while a
10250 redisplay of the buffer is going on, and seriously confuse
10251 redisplay. */
10252 ptrdiff_t count = inhibit_garbage_collection ();
10254 /* If there is no message, we must call display_echo_area_1
10255 nevertheless because it resizes the window. But we will have to
10256 reset the echo_area_buffer in question to nil at the end because
10257 with_echo_area_buffer will sets it to an empty buffer. */
10258 i = display_last_displayed_message_p ? 1 : 0;
10259 no_message_p = NILP (echo_area_buffer[i]);
10261 window_height_changed_p
10262 = with_echo_area_buffer (w, display_last_displayed_message_p,
10263 display_echo_area_1,
10264 (intptr_t) w, Qnil);
10266 if (no_message_p)
10267 echo_area_buffer[i] = Qnil;
10269 unbind_to (count, Qnil);
10270 return window_height_changed_p;
10274 /* Helper for display_echo_area. Display the current buffer which
10275 contains the current echo area message in window W, a mini-window,
10276 a pointer to which is passed in A1. A2..A4 are currently not used.
10277 Change the height of W so that all of the message is displayed.
10278 Value is non-zero if height of W was changed. */
10280 static int
10281 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10283 intptr_t i1 = a1;
10284 struct window *w = (struct window *) i1;
10285 Lisp_Object window;
10286 struct text_pos start;
10287 int window_height_changed_p = 0;
10289 /* Do this before displaying, so that we have a large enough glyph
10290 matrix for the display. If we can't get enough space for the
10291 whole text, display the last N lines. That works by setting w->start. */
10292 window_height_changed_p = resize_mini_window (w, 0);
10294 /* Use the starting position chosen by resize_mini_window. */
10295 SET_TEXT_POS_FROM_MARKER (start, w->start);
10297 /* Display. */
10298 clear_glyph_matrix (w->desired_matrix);
10299 XSETWINDOW (window, w);
10300 try_window (window, start, 0);
10302 return window_height_changed_p;
10306 /* Resize the echo area window to exactly the size needed for the
10307 currently displayed message, if there is one. If a mini-buffer
10308 is active, don't shrink it. */
10310 void
10311 resize_echo_area_exactly (void)
10313 if (BUFFERP (echo_area_buffer[0])
10314 && WINDOWP (echo_area_window))
10316 struct window *w = XWINDOW (echo_area_window);
10317 int resized_p;
10318 Lisp_Object resize_exactly;
10320 if (minibuf_level == 0)
10321 resize_exactly = Qt;
10322 else
10323 resize_exactly = Qnil;
10325 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10326 (intptr_t) w, resize_exactly);
10327 if (resized_p)
10329 ++windows_or_buffers_changed;
10330 ++update_mode_lines;
10331 redisplay_internal ();
10337 /* Callback function for with_echo_area_buffer, when used from
10338 resize_echo_area_exactly. A1 contains a pointer to the window to
10339 resize, EXACTLY non-nil means resize the mini-window exactly to the
10340 size of the text displayed. A3 and A4 are not used. Value is what
10341 resize_mini_window returns. */
10343 static int
10344 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10346 intptr_t i1 = a1;
10347 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10351 /* Resize mini-window W to fit the size of its contents. EXACT_P
10352 means size the window exactly to the size needed. Otherwise, it's
10353 only enlarged until W's buffer is empty.
10355 Set W->start to the right place to begin display. If the whole
10356 contents fit, start at the beginning. Otherwise, start so as
10357 to make the end of the contents appear. This is particularly
10358 important for y-or-n-p, but seems desirable generally.
10360 Value is non-zero if the window height has been changed. */
10363 resize_mini_window (struct window *w, int exact_p)
10365 struct frame *f = XFRAME (w->frame);
10366 int window_height_changed_p = 0;
10368 eassert (MINI_WINDOW_P (w));
10370 /* By default, start display at the beginning. */
10371 set_marker_both (w->start, w->contents,
10372 BUF_BEGV (XBUFFER (w->contents)),
10373 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10375 /* Don't resize windows while redisplaying a window; it would
10376 confuse redisplay functions when the size of the window they are
10377 displaying changes from under them. Such a resizing can happen,
10378 for instance, when which-func prints a long message while
10379 we are running fontification-functions. We're running these
10380 functions with safe_call which binds inhibit-redisplay to t. */
10381 if (!NILP (Vinhibit_redisplay))
10382 return 0;
10384 /* Nil means don't try to resize. */
10385 if (NILP (Vresize_mini_windows)
10386 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10387 return 0;
10389 if (!FRAME_MINIBUF_ONLY_P (f))
10391 struct it it;
10392 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10393 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10394 int height;
10395 EMACS_INT max_height;
10396 int unit = FRAME_LINE_HEIGHT (f);
10397 struct text_pos start;
10398 struct buffer *old_current_buffer = NULL;
10400 if (current_buffer != XBUFFER (w->contents))
10402 old_current_buffer = current_buffer;
10403 set_buffer_internal (XBUFFER (w->contents));
10406 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10408 /* Compute the max. number of lines specified by the user. */
10409 if (FLOATP (Vmax_mini_window_height))
10410 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10411 else if (INTEGERP (Vmax_mini_window_height))
10412 max_height = XINT (Vmax_mini_window_height);
10413 else
10414 max_height = total_height / 4;
10416 /* Correct that max. height if it's bogus. */
10417 max_height = clip_to_bounds (1, max_height, total_height);
10419 /* Find out the height of the text in the window. */
10420 if (it.line_wrap == TRUNCATE)
10421 height = 1;
10422 else
10424 last_height = 0;
10425 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10426 if (it.max_ascent == 0 && it.max_descent == 0)
10427 height = it.current_y + last_height;
10428 else
10429 height = it.current_y + it.max_ascent + it.max_descent;
10430 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10431 height = (height + unit - 1) / unit;
10434 /* Compute a suitable window start. */
10435 if (height > max_height)
10437 height = max_height;
10438 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10439 move_it_vertically_backward (&it, (height - 1) * unit);
10440 start = it.current.pos;
10442 else
10443 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10444 SET_MARKER_FROM_TEXT_POS (w->start, start);
10446 if (EQ (Vresize_mini_windows, Qgrow_only))
10448 /* Let it grow only, until we display an empty message, in which
10449 case the window shrinks again. */
10450 if (height > WINDOW_TOTAL_LINES (w))
10452 int old_height = WINDOW_TOTAL_LINES (w);
10454 FRAME_WINDOWS_FROZEN (f) = 1;
10455 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10456 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10458 else if (height < WINDOW_TOTAL_LINES (w)
10459 && (exact_p || BEGV == ZV))
10461 int old_height = WINDOW_TOTAL_LINES (w);
10463 FRAME_WINDOWS_FROZEN (f) = 0;
10464 shrink_mini_window (w);
10465 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10468 else
10470 /* Always resize to exact size needed. */
10471 if (height > WINDOW_TOTAL_LINES (w))
10473 int old_height = WINDOW_TOTAL_LINES (w);
10475 FRAME_WINDOWS_FROZEN (f) = 1;
10476 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10477 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10479 else if (height < WINDOW_TOTAL_LINES (w))
10481 int old_height = WINDOW_TOTAL_LINES (w);
10483 FRAME_WINDOWS_FROZEN (f) = 0;
10484 shrink_mini_window (w);
10486 if (height)
10488 FRAME_WINDOWS_FROZEN (f) = 1;
10489 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10492 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10496 if (old_current_buffer)
10497 set_buffer_internal (old_current_buffer);
10500 return window_height_changed_p;
10504 /* Value is the current message, a string, or nil if there is no
10505 current message. */
10507 Lisp_Object
10508 current_message (void)
10510 Lisp_Object msg;
10512 if (!BUFFERP (echo_area_buffer[0]))
10513 msg = Qnil;
10514 else
10516 with_echo_area_buffer (0, 0, current_message_1,
10517 (intptr_t) &msg, Qnil);
10518 if (NILP (msg))
10519 echo_area_buffer[0] = Qnil;
10522 return msg;
10526 static int
10527 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10529 intptr_t i1 = a1;
10530 Lisp_Object *msg = (Lisp_Object *) i1;
10532 if (Z > BEG)
10533 *msg = make_buffer_string (BEG, Z, 1);
10534 else
10535 *msg = Qnil;
10536 return 0;
10540 /* Push the current message on Vmessage_stack for later restoration
10541 by restore_message. Value is non-zero if the current message isn't
10542 empty. This is a relatively infrequent operation, so it's not
10543 worth optimizing. */
10545 bool
10546 push_message (void)
10548 Lisp_Object msg = current_message ();
10549 Vmessage_stack = Fcons (msg, Vmessage_stack);
10550 return STRINGP (msg);
10554 /* Restore message display from the top of Vmessage_stack. */
10556 void
10557 restore_message (void)
10559 eassert (CONSP (Vmessage_stack));
10560 message3_nolog (XCAR (Vmessage_stack));
10564 /* Handler for unwind-protect calling pop_message. */
10566 void
10567 pop_message_unwind (void)
10569 /* Pop the top-most entry off Vmessage_stack. */
10570 eassert (CONSP (Vmessage_stack));
10571 Vmessage_stack = XCDR (Vmessage_stack);
10575 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10576 exits. If the stack is not empty, we have a missing pop_message
10577 somewhere. */
10579 void
10580 check_message_stack (void)
10582 if (!NILP (Vmessage_stack))
10583 emacs_abort ();
10587 /* Truncate to NCHARS what will be displayed in the echo area the next
10588 time we display it---but don't redisplay it now. */
10590 void
10591 truncate_echo_area (ptrdiff_t nchars)
10593 if (nchars == 0)
10594 echo_area_buffer[0] = Qnil;
10595 else if (!noninteractive
10596 && INTERACTIVE
10597 && !NILP (echo_area_buffer[0]))
10599 struct frame *sf = SELECTED_FRAME ();
10600 /* Error messages get reported properly by cmd_error, so this must be
10601 just an informative message; if the frame hasn't really been
10602 initialized yet, just toss it. */
10603 if (sf->glyphs_initialized_p)
10604 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10609 /* Helper function for truncate_echo_area. Truncate the current
10610 message to at most NCHARS characters. */
10612 static int
10613 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10615 if (BEG + nchars < Z)
10616 del_range (BEG + nchars, Z);
10617 if (Z == BEG)
10618 echo_area_buffer[0] = Qnil;
10619 return 0;
10622 /* Set the current message to STRING. */
10624 static void
10625 set_message (Lisp_Object string)
10627 eassert (STRINGP (string));
10629 message_enable_multibyte = STRING_MULTIBYTE (string);
10631 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10632 message_buf_print = 0;
10633 help_echo_showing_p = 0;
10635 if (STRINGP (Vdebug_on_message)
10636 && STRINGP (string)
10637 && fast_string_match (Vdebug_on_message, string) >= 0)
10638 call_debugger (list2 (Qerror, string));
10642 /* Helper function for set_message. First argument is ignored and second
10643 argument has the same meaning as for set_message.
10644 This function is called with the echo area buffer being current. */
10646 static int
10647 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10649 eassert (STRINGP (string));
10651 /* Change multibyteness of the echo buffer appropriately. */
10652 if (message_enable_multibyte
10653 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10654 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10656 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10657 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10658 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10660 /* Insert new message at BEG. */
10661 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10663 /* This function takes care of single/multibyte conversion.
10664 We just have to ensure that the echo area buffer has the right
10665 setting of enable_multibyte_characters. */
10666 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10668 return 0;
10672 /* Clear messages. CURRENT_P non-zero means clear the current
10673 message. LAST_DISPLAYED_P non-zero means clear the message
10674 last displayed. */
10676 void
10677 clear_message (int current_p, int last_displayed_p)
10679 if (current_p)
10681 echo_area_buffer[0] = Qnil;
10682 message_cleared_p = 1;
10685 if (last_displayed_p)
10686 echo_area_buffer[1] = Qnil;
10688 message_buf_print = 0;
10691 /* Clear garbaged frames.
10693 This function is used where the old redisplay called
10694 redraw_garbaged_frames which in turn called redraw_frame which in
10695 turn called clear_frame. The call to clear_frame was a source of
10696 flickering. I believe a clear_frame is not necessary. It should
10697 suffice in the new redisplay to invalidate all current matrices,
10698 and ensure a complete redisplay of all windows. */
10700 static void
10701 clear_garbaged_frames (void)
10703 if (frame_garbaged)
10705 Lisp_Object tail, frame;
10706 int changed_count = 0;
10708 FOR_EACH_FRAME (tail, frame)
10710 struct frame *f = XFRAME (frame);
10712 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10714 if (f->resized_p)
10715 redraw_frame (f);
10716 else
10717 clear_current_matrices (f);
10718 changed_count++;
10719 f->garbaged = 0;
10720 f->resized_p = 0;
10724 frame_garbaged = 0;
10725 if (changed_count)
10726 ++windows_or_buffers_changed;
10731 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10732 is non-zero update selected_frame. Value is non-zero if the
10733 mini-windows height has been changed. */
10735 static int
10736 echo_area_display (int update_frame_p)
10738 Lisp_Object mini_window;
10739 struct window *w;
10740 struct frame *f;
10741 int window_height_changed_p = 0;
10742 struct frame *sf = SELECTED_FRAME ();
10744 mini_window = FRAME_MINIBUF_WINDOW (sf);
10745 w = XWINDOW (mini_window);
10746 f = XFRAME (WINDOW_FRAME (w));
10748 /* Don't display if frame is invisible or not yet initialized. */
10749 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10750 return 0;
10752 #ifdef HAVE_WINDOW_SYSTEM
10753 /* When Emacs starts, selected_frame may be the initial terminal
10754 frame. If we let this through, a message would be displayed on
10755 the terminal. */
10756 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10757 return 0;
10758 #endif /* HAVE_WINDOW_SYSTEM */
10760 /* Redraw garbaged frames. */
10761 clear_garbaged_frames ();
10763 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10765 echo_area_window = mini_window;
10766 window_height_changed_p = display_echo_area (w);
10767 w->must_be_updated_p = 1;
10769 /* Update the display, unless called from redisplay_internal.
10770 Also don't update the screen during redisplay itself. The
10771 update will happen at the end of redisplay, and an update
10772 here could cause confusion. */
10773 if (update_frame_p && !redisplaying_p)
10775 int n = 0;
10777 /* If the display update has been interrupted by pending
10778 input, update mode lines in the frame. Due to the
10779 pending input, it might have been that redisplay hasn't
10780 been called, so that mode lines above the echo area are
10781 garbaged. This looks odd, so we prevent it here. */
10782 if (!display_completed)
10783 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10785 if (window_height_changed_p
10786 /* Don't do this if Emacs is shutting down. Redisplay
10787 needs to run hooks. */
10788 && !NILP (Vrun_hooks))
10790 /* Must update other windows. Likewise as in other
10791 cases, don't let this update be interrupted by
10792 pending input. */
10793 ptrdiff_t count = SPECPDL_INDEX ();
10794 specbind (Qredisplay_dont_pause, Qt);
10795 windows_or_buffers_changed = 1;
10796 redisplay_internal ();
10797 unbind_to (count, Qnil);
10799 else if (FRAME_WINDOW_P (f) && n == 0)
10801 /* Window configuration is the same as before.
10802 Can do with a display update of the echo area,
10803 unless we displayed some mode lines. */
10804 update_single_window (w, 1);
10805 flush_frame (f);
10807 else
10808 update_frame (f, 1, 1);
10810 /* If cursor is in the echo area, make sure that the next
10811 redisplay displays the minibuffer, so that the cursor will
10812 be replaced with what the minibuffer wants. */
10813 if (cursor_in_echo_area)
10814 ++windows_or_buffers_changed;
10817 else if (!EQ (mini_window, selected_window))
10818 windows_or_buffers_changed++;
10820 /* Last displayed message is now the current message. */
10821 echo_area_buffer[1] = echo_area_buffer[0];
10822 /* Inform read_char that we're not echoing. */
10823 echo_message_buffer = Qnil;
10825 /* Prevent redisplay optimization in redisplay_internal by resetting
10826 this_line_start_pos. This is done because the mini-buffer now
10827 displays the message instead of its buffer text. */
10828 if (EQ (mini_window, selected_window))
10829 CHARPOS (this_line_start_pos) = 0;
10831 return window_height_changed_p;
10834 /* Nonzero if the current window's buffer is shown in more than one
10835 window and was modified since last redisplay. */
10837 static int
10838 buffer_shared_and_changed (void)
10840 return (buffer_window_count (current_buffer) > 1
10841 && UNCHANGED_MODIFIED < MODIFF);
10844 /* Nonzero if W's buffer was changed but not saved. */
10846 static int
10847 window_buffer_changed (struct window *w)
10849 struct buffer *b = XBUFFER (w->contents);
10851 eassert (BUFFER_LIVE_P (b));
10853 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
10856 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10858 static int
10859 mode_line_update_needed (struct window *w)
10861 return (w->column_number_displayed != -1
10862 && !(PT == w->last_point && !window_outdated (w))
10863 && (w->column_number_displayed != current_column ()));
10866 /* Nonzero if window start of W is frozen and may not be changed during
10867 redisplay. */
10869 static bool
10870 window_frozen_p (struct window *w)
10872 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10874 Lisp_Object window;
10876 XSETWINDOW (window, w);
10877 if (MINI_WINDOW_P (w))
10878 return 0;
10879 else if (EQ (window, selected_window))
10880 return 0;
10881 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10882 && EQ (window, Vminibuf_scroll_window))
10883 /* This special window can't be frozen too. */
10884 return 0;
10885 else
10886 return 1;
10888 return 0;
10891 /***********************************************************************
10892 Mode Lines and Frame Titles
10893 ***********************************************************************/
10895 /* A buffer for constructing non-propertized mode-line strings and
10896 frame titles in it; allocated from the heap in init_xdisp and
10897 resized as needed in store_mode_line_noprop_char. */
10899 static char *mode_line_noprop_buf;
10901 /* The buffer's end, and a current output position in it. */
10903 static char *mode_line_noprop_buf_end;
10904 static char *mode_line_noprop_ptr;
10906 #define MODE_LINE_NOPROP_LEN(start) \
10907 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10909 static enum {
10910 MODE_LINE_DISPLAY = 0,
10911 MODE_LINE_TITLE,
10912 MODE_LINE_NOPROP,
10913 MODE_LINE_STRING
10914 } mode_line_target;
10916 /* Alist that caches the results of :propertize.
10917 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10918 static Lisp_Object mode_line_proptrans_alist;
10920 /* List of strings making up the mode-line. */
10921 static Lisp_Object mode_line_string_list;
10923 /* Base face property when building propertized mode line string. */
10924 static Lisp_Object mode_line_string_face;
10925 static Lisp_Object mode_line_string_face_prop;
10928 /* Unwind data for mode line strings */
10930 static Lisp_Object Vmode_line_unwind_vector;
10932 static Lisp_Object
10933 format_mode_line_unwind_data (struct frame *target_frame,
10934 struct buffer *obuf,
10935 Lisp_Object owin,
10936 int save_proptrans)
10938 Lisp_Object vector, tmp;
10940 /* Reduce consing by keeping one vector in
10941 Vwith_echo_area_save_vector. */
10942 vector = Vmode_line_unwind_vector;
10943 Vmode_line_unwind_vector = Qnil;
10945 if (NILP (vector))
10946 vector = Fmake_vector (make_number (10), Qnil);
10948 ASET (vector, 0, make_number (mode_line_target));
10949 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10950 ASET (vector, 2, mode_line_string_list);
10951 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10952 ASET (vector, 4, mode_line_string_face);
10953 ASET (vector, 5, mode_line_string_face_prop);
10955 if (obuf)
10956 XSETBUFFER (tmp, obuf);
10957 else
10958 tmp = Qnil;
10959 ASET (vector, 6, tmp);
10960 ASET (vector, 7, owin);
10961 if (target_frame)
10963 /* Similarly to `with-selected-window', if the operation selects
10964 a window on another frame, we must restore that frame's
10965 selected window, and (for a tty) the top-frame. */
10966 ASET (vector, 8, target_frame->selected_window);
10967 if (FRAME_TERMCAP_P (target_frame))
10968 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10971 return vector;
10974 static void
10975 unwind_format_mode_line (Lisp_Object vector)
10977 Lisp_Object old_window = AREF (vector, 7);
10978 Lisp_Object target_frame_window = AREF (vector, 8);
10979 Lisp_Object old_top_frame = AREF (vector, 9);
10981 mode_line_target = XINT (AREF (vector, 0));
10982 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10983 mode_line_string_list = AREF (vector, 2);
10984 if (! EQ (AREF (vector, 3), Qt))
10985 mode_line_proptrans_alist = AREF (vector, 3);
10986 mode_line_string_face = AREF (vector, 4);
10987 mode_line_string_face_prop = AREF (vector, 5);
10989 /* Select window before buffer, since it may change the buffer. */
10990 if (!NILP (old_window))
10992 /* If the operation that we are unwinding had selected a window
10993 on a different frame, reset its frame-selected-window. For a
10994 text terminal, reset its top-frame if necessary. */
10995 if (!NILP (target_frame_window))
10997 Lisp_Object frame
10998 = WINDOW_FRAME (XWINDOW (target_frame_window));
11000 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11001 Fselect_window (target_frame_window, Qt);
11003 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11004 Fselect_frame (old_top_frame, Qt);
11007 Fselect_window (old_window, Qt);
11010 if (!NILP (AREF (vector, 6)))
11012 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11013 ASET (vector, 6, Qnil);
11016 Vmode_line_unwind_vector = vector;
11020 /* Store a single character C for the frame title in mode_line_noprop_buf.
11021 Re-allocate mode_line_noprop_buf if necessary. */
11023 static void
11024 store_mode_line_noprop_char (char c)
11026 /* If output position has reached the end of the allocated buffer,
11027 increase the buffer's size. */
11028 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11030 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11031 ptrdiff_t size = len;
11032 mode_line_noprop_buf =
11033 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11034 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11035 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11038 *mode_line_noprop_ptr++ = c;
11042 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11043 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11044 characters that yield more columns than PRECISION; PRECISION <= 0
11045 means copy the whole string. Pad with spaces until FIELD_WIDTH
11046 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11047 pad. Called from display_mode_element when it is used to build a
11048 frame title. */
11050 static int
11051 store_mode_line_noprop (const char *string, int field_width, int precision)
11053 const unsigned char *str = (const unsigned char *) string;
11054 int n = 0;
11055 ptrdiff_t dummy, nbytes;
11057 /* Copy at most PRECISION chars from STR. */
11058 nbytes = strlen (string);
11059 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11060 while (nbytes--)
11061 store_mode_line_noprop_char (*str++);
11063 /* Fill up with spaces until FIELD_WIDTH reached. */
11064 while (field_width > 0
11065 && n < field_width)
11067 store_mode_line_noprop_char (' ');
11068 ++n;
11071 return n;
11074 /***********************************************************************
11075 Frame Titles
11076 ***********************************************************************/
11078 #ifdef HAVE_WINDOW_SYSTEM
11080 /* Set the title of FRAME, if it has changed. The title format is
11081 Vicon_title_format if FRAME is iconified, otherwise it is
11082 frame_title_format. */
11084 static void
11085 x_consider_frame_title (Lisp_Object frame)
11087 struct frame *f = XFRAME (frame);
11089 if (FRAME_WINDOW_P (f)
11090 || FRAME_MINIBUF_ONLY_P (f)
11091 || f->explicit_name)
11093 /* Do we have more than one visible frame on this X display? */
11094 Lisp_Object tail, other_frame, fmt;
11095 ptrdiff_t title_start;
11096 char *title;
11097 ptrdiff_t len;
11098 struct it it;
11099 ptrdiff_t count = SPECPDL_INDEX ();
11101 FOR_EACH_FRAME (tail, other_frame)
11103 struct frame *tf = XFRAME (other_frame);
11105 if (tf != f
11106 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11107 && !FRAME_MINIBUF_ONLY_P (tf)
11108 && !EQ (other_frame, tip_frame)
11109 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11110 break;
11113 /* Set global variable indicating that multiple frames exist. */
11114 multiple_frames = CONSP (tail);
11116 /* Switch to the buffer of selected window of the frame. Set up
11117 mode_line_target so that display_mode_element will output into
11118 mode_line_noprop_buf; then display the title. */
11119 record_unwind_protect (unwind_format_mode_line,
11120 format_mode_line_unwind_data
11121 (f, current_buffer, selected_window, 0));
11123 Fselect_window (f->selected_window, Qt);
11124 set_buffer_internal_1
11125 (XBUFFER (XWINDOW (f->selected_window)->contents));
11126 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11128 mode_line_target = MODE_LINE_TITLE;
11129 title_start = MODE_LINE_NOPROP_LEN (0);
11130 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11131 NULL, DEFAULT_FACE_ID);
11132 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11133 len = MODE_LINE_NOPROP_LEN (title_start);
11134 title = mode_line_noprop_buf + title_start;
11135 unbind_to (count, Qnil);
11137 /* Set the title only if it's changed. This avoids consing in
11138 the common case where it hasn't. (If it turns out that we've
11139 already wasted too much time by walking through the list with
11140 display_mode_element, then we might need to optimize at a
11141 higher level than this.) */
11142 if (! STRINGP (f->name)
11143 || SBYTES (f->name) != len
11144 || memcmp (title, SDATA (f->name), len) != 0)
11145 x_implicitly_set_name (f, make_string (title, len), Qnil);
11149 #endif /* not HAVE_WINDOW_SYSTEM */
11152 /***********************************************************************
11153 Menu Bars
11154 ***********************************************************************/
11157 /* Prepare for redisplay by updating menu-bar item lists when
11158 appropriate. This can call eval. */
11160 void
11161 prepare_menu_bars (void)
11163 int all_windows;
11164 struct gcpro gcpro1, gcpro2;
11165 struct frame *f;
11166 Lisp_Object tooltip_frame;
11168 #ifdef HAVE_WINDOW_SYSTEM
11169 tooltip_frame = tip_frame;
11170 #else
11171 tooltip_frame = Qnil;
11172 #endif
11174 /* Update all frame titles based on their buffer names, etc. We do
11175 this before the menu bars so that the buffer-menu will show the
11176 up-to-date frame titles. */
11177 #ifdef HAVE_WINDOW_SYSTEM
11178 if (windows_or_buffers_changed || update_mode_lines)
11180 Lisp_Object tail, frame;
11182 FOR_EACH_FRAME (tail, frame)
11184 f = XFRAME (frame);
11185 if (!EQ (frame, tooltip_frame)
11186 && (FRAME_ICONIFIED_P (f)
11187 || FRAME_VISIBLE_P (f) == 1
11188 /* Exclude TTY frames that are obscured because they
11189 are not the top frame on their console. This is
11190 because x_consider_frame_title actually switches
11191 to the frame, which for TTY frames means it is
11192 marked as garbaged, and will be completely
11193 redrawn on the next redisplay cycle. This causes
11194 TTY frames to be completely redrawn, when there
11195 are more than one of them, even though nothing
11196 should be changed on display. */
11197 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11198 x_consider_frame_title (frame);
11201 #endif /* HAVE_WINDOW_SYSTEM */
11203 /* Update the menu bar item lists, if appropriate. This has to be
11204 done before any actual redisplay or generation of display lines. */
11205 all_windows = (update_mode_lines
11206 || buffer_shared_and_changed ()
11207 || windows_or_buffers_changed);
11209 if (FUNCTIONP (Vpre_redisplay_function))
11210 safe_call1 (Vpre_redisplay_function, all_windows ? Qt : Qnil);
11212 if (all_windows)
11214 Lisp_Object tail, frame;
11215 ptrdiff_t count = SPECPDL_INDEX ();
11216 /* 1 means that update_menu_bar has run its hooks
11217 so any further calls to update_menu_bar shouldn't do so again. */
11218 int menu_bar_hooks_run = 0;
11220 record_unwind_save_match_data ();
11222 FOR_EACH_FRAME (tail, frame)
11224 f = XFRAME (frame);
11226 /* Ignore tooltip frame. */
11227 if (EQ (frame, tooltip_frame))
11228 continue;
11230 /* If a window on this frame changed size, report that to
11231 the user and clear the size-change flag. */
11232 if (FRAME_WINDOW_SIZES_CHANGED (f))
11234 Lisp_Object functions;
11236 /* Clear flag first in case we get an error below. */
11237 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11238 functions = Vwindow_size_change_functions;
11239 GCPRO2 (tail, functions);
11241 while (CONSP (functions))
11243 if (!EQ (XCAR (functions), Qt))
11244 call1 (XCAR (functions), frame);
11245 functions = XCDR (functions);
11247 UNGCPRO;
11250 GCPRO1 (tail);
11251 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11252 #ifdef HAVE_WINDOW_SYSTEM
11253 update_tool_bar (f, 0);
11254 #endif
11255 #ifdef HAVE_NS
11256 if (windows_or_buffers_changed
11257 && FRAME_NS_P (f))
11258 ns_set_doc_edited
11259 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11260 #endif
11261 UNGCPRO;
11264 unbind_to (count, Qnil);
11266 else
11268 struct frame *sf = SELECTED_FRAME ();
11269 update_menu_bar (sf, 1, 0);
11270 #ifdef HAVE_WINDOW_SYSTEM
11271 update_tool_bar (sf, 1);
11272 #endif
11277 /* Update the menu bar item list for frame F. This has to be done
11278 before we start to fill in any display lines, because it can call
11279 eval.
11281 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11283 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11284 already ran the menu bar hooks for this redisplay, so there
11285 is no need to run them again. The return value is the
11286 updated value of this flag, to pass to the next call. */
11288 static int
11289 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11291 Lisp_Object window;
11292 register struct window *w;
11294 /* If called recursively during a menu update, do nothing. This can
11295 happen when, for instance, an activate-menubar-hook causes a
11296 redisplay. */
11297 if (inhibit_menubar_update)
11298 return hooks_run;
11300 window = FRAME_SELECTED_WINDOW (f);
11301 w = XWINDOW (window);
11303 if (FRAME_WINDOW_P (f)
11305 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11306 || defined (HAVE_NS) || defined (USE_GTK)
11307 FRAME_EXTERNAL_MENU_BAR (f)
11308 #else
11309 FRAME_MENU_BAR_LINES (f) > 0
11310 #endif
11311 : FRAME_MENU_BAR_LINES (f) > 0)
11313 /* If the user has switched buffers or windows, we need to
11314 recompute to reflect the new bindings. But we'll
11315 recompute when update_mode_lines is set too; that means
11316 that people can use force-mode-line-update to request
11317 that the menu bar be recomputed. The adverse effect on
11318 the rest of the redisplay algorithm is about the same as
11319 windows_or_buffers_changed anyway. */
11320 if (windows_or_buffers_changed
11321 /* This used to test w->update_mode_line, but we believe
11322 there is no need to recompute the menu in that case. */
11323 || update_mode_lines
11324 || window_buffer_changed (w))
11326 struct buffer *prev = current_buffer;
11327 ptrdiff_t count = SPECPDL_INDEX ();
11329 specbind (Qinhibit_menubar_update, Qt);
11331 set_buffer_internal_1 (XBUFFER (w->contents));
11332 if (save_match_data)
11333 record_unwind_save_match_data ();
11334 if (NILP (Voverriding_local_map_menu_flag))
11336 specbind (Qoverriding_terminal_local_map, Qnil);
11337 specbind (Qoverriding_local_map, Qnil);
11340 if (!hooks_run)
11342 /* Run the Lucid hook. */
11343 safe_run_hooks (Qactivate_menubar_hook);
11345 /* If it has changed current-menubar from previous value,
11346 really recompute the menu-bar from the value. */
11347 if (! NILP (Vlucid_menu_bar_dirty_flag))
11348 call0 (Qrecompute_lucid_menubar);
11350 safe_run_hooks (Qmenu_bar_update_hook);
11352 hooks_run = 1;
11355 XSETFRAME (Vmenu_updating_frame, f);
11356 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11358 /* Redisplay the menu bar in case we changed it. */
11359 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11360 || defined (HAVE_NS) || defined (USE_GTK)
11361 if (FRAME_WINDOW_P (f))
11363 #if defined (HAVE_NS)
11364 /* All frames on Mac OS share the same menubar. So only
11365 the selected frame should be allowed to set it. */
11366 if (f == SELECTED_FRAME ())
11367 #endif
11368 set_frame_menubar (f, 0, 0);
11370 else
11371 /* On a terminal screen, the menu bar is an ordinary screen
11372 line, and this makes it get updated. */
11373 w->update_mode_line = 1;
11374 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11375 /* In the non-toolkit version, the menu bar is an ordinary screen
11376 line, and this makes it get updated. */
11377 w->update_mode_line = 1;
11378 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11380 unbind_to (count, Qnil);
11381 set_buffer_internal_1 (prev);
11385 return hooks_run;
11388 /***********************************************************************
11389 Tool-bars
11390 ***********************************************************************/
11392 #ifdef HAVE_WINDOW_SYSTEM
11394 /* Tool-bar item index of the item on which a mouse button was pressed
11395 or -1. */
11397 int last_tool_bar_item;
11399 /* Select `frame' temporarily without running all the code in
11400 do_switch_frame.
11401 FIXME: Maybe do_switch_frame should be trimmed down similarly
11402 when `norecord' is set. */
11403 static void
11404 fast_set_selected_frame (Lisp_Object frame)
11406 if (!EQ (selected_frame, frame))
11408 selected_frame = frame;
11409 selected_window = XFRAME (frame)->selected_window;
11413 /* Update the tool-bar item list for frame F. This has to be done
11414 before we start to fill in any display lines. Called from
11415 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11416 and restore it here. */
11418 static void
11419 update_tool_bar (struct frame *f, int save_match_data)
11421 #if defined (USE_GTK) || defined (HAVE_NS)
11422 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11423 #else
11424 int do_update = WINDOWP (f->tool_bar_window)
11425 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11426 #endif
11428 if (do_update)
11430 Lisp_Object window;
11431 struct window *w;
11433 window = FRAME_SELECTED_WINDOW (f);
11434 w = XWINDOW (window);
11436 /* If the user has switched buffers or windows, we need to
11437 recompute to reflect the new bindings. But we'll
11438 recompute when update_mode_lines is set too; that means
11439 that people can use force-mode-line-update to request
11440 that the menu bar be recomputed. The adverse effect on
11441 the rest of the redisplay algorithm is about the same as
11442 windows_or_buffers_changed anyway. */
11443 if (windows_or_buffers_changed
11444 || w->update_mode_line
11445 || update_mode_lines
11446 || window_buffer_changed (w))
11448 struct buffer *prev = current_buffer;
11449 ptrdiff_t count = SPECPDL_INDEX ();
11450 Lisp_Object frame, new_tool_bar;
11451 int new_n_tool_bar;
11452 struct gcpro gcpro1;
11454 /* Set current_buffer to the buffer of the selected
11455 window of the frame, so that we get the right local
11456 keymaps. */
11457 set_buffer_internal_1 (XBUFFER (w->contents));
11459 /* Save match data, if we must. */
11460 if (save_match_data)
11461 record_unwind_save_match_data ();
11463 /* Make sure that we don't accidentally use bogus keymaps. */
11464 if (NILP (Voverriding_local_map_menu_flag))
11466 specbind (Qoverriding_terminal_local_map, Qnil);
11467 specbind (Qoverriding_local_map, Qnil);
11470 GCPRO1 (new_tool_bar);
11472 /* We must temporarily set the selected frame to this frame
11473 before calling tool_bar_items, because the calculation of
11474 the tool-bar keymap uses the selected frame (see
11475 `tool-bar-make-keymap' in tool-bar.el). */
11476 eassert (EQ (selected_window,
11477 /* Since we only explicitly preserve selected_frame,
11478 check that selected_window would be redundant. */
11479 XFRAME (selected_frame)->selected_window));
11480 record_unwind_protect (fast_set_selected_frame, selected_frame);
11481 XSETFRAME (frame, f);
11482 fast_set_selected_frame (frame);
11484 /* Build desired tool-bar items from keymaps. */
11485 new_tool_bar
11486 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11487 &new_n_tool_bar);
11489 /* Redisplay the tool-bar if we changed it. */
11490 if (new_n_tool_bar != f->n_tool_bar_items
11491 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11493 /* Redisplay that happens asynchronously due to an expose event
11494 may access f->tool_bar_items. Make sure we update both
11495 variables within BLOCK_INPUT so no such event interrupts. */
11496 block_input ();
11497 fset_tool_bar_items (f, new_tool_bar);
11498 f->n_tool_bar_items = new_n_tool_bar;
11499 w->update_mode_line = 1;
11500 unblock_input ();
11503 UNGCPRO;
11505 unbind_to (count, Qnil);
11506 set_buffer_internal_1 (prev);
11511 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11513 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11514 F's desired tool-bar contents. F->tool_bar_items must have
11515 been set up previously by calling prepare_menu_bars. */
11517 static void
11518 build_desired_tool_bar_string (struct frame *f)
11520 int i, size, size_needed;
11521 struct gcpro gcpro1, gcpro2, gcpro3;
11522 Lisp_Object image, plist, props;
11524 image = plist = props = Qnil;
11525 GCPRO3 (image, plist, props);
11527 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11528 Otherwise, make a new string. */
11530 /* The size of the string we might be able to reuse. */
11531 size = (STRINGP (f->desired_tool_bar_string)
11532 ? SCHARS (f->desired_tool_bar_string)
11533 : 0);
11535 /* We need one space in the string for each image. */
11536 size_needed = f->n_tool_bar_items;
11538 /* Reuse f->desired_tool_bar_string, if possible. */
11539 if (size < size_needed || NILP (f->desired_tool_bar_string))
11540 fset_desired_tool_bar_string
11541 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11542 else
11544 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11545 Fremove_text_properties (make_number (0), make_number (size),
11546 props, f->desired_tool_bar_string);
11549 /* Put a `display' property on the string for the images to display,
11550 put a `menu_item' property on tool-bar items with a value that
11551 is the index of the item in F's tool-bar item vector. */
11552 for (i = 0; i < f->n_tool_bar_items; ++i)
11554 #define PROP(IDX) \
11555 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11557 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11558 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11559 int hmargin, vmargin, relief, idx, end;
11561 /* If image is a vector, choose the image according to the
11562 button state. */
11563 image = PROP (TOOL_BAR_ITEM_IMAGES);
11564 if (VECTORP (image))
11566 if (enabled_p)
11567 idx = (selected_p
11568 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11569 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11570 else
11571 idx = (selected_p
11572 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11573 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11575 eassert (ASIZE (image) >= idx);
11576 image = AREF (image, idx);
11578 else
11579 idx = -1;
11581 /* Ignore invalid image specifications. */
11582 if (!valid_image_p (image))
11583 continue;
11585 /* Display the tool-bar button pressed, or depressed. */
11586 plist = Fcopy_sequence (XCDR (image));
11588 /* Compute margin and relief to draw. */
11589 relief = (tool_bar_button_relief >= 0
11590 ? tool_bar_button_relief
11591 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11592 hmargin = vmargin = relief;
11594 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11595 INT_MAX - max (hmargin, vmargin)))
11597 hmargin += XFASTINT (Vtool_bar_button_margin);
11598 vmargin += XFASTINT (Vtool_bar_button_margin);
11600 else if (CONSP (Vtool_bar_button_margin))
11602 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11603 INT_MAX - hmargin))
11604 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11606 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11607 INT_MAX - vmargin))
11608 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11611 if (auto_raise_tool_bar_buttons_p)
11613 /* Add a `:relief' property to the image spec if the item is
11614 selected. */
11615 if (selected_p)
11617 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11618 hmargin -= relief;
11619 vmargin -= relief;
11622 else
11624 /* If image is selected, display it pressed, i.e. with a
11625 negative relief. If it's not selected, display it with a
11626 raised relief. */
11627 plist = Fplist_put (plist, QCrelief,
11628 (selected_p
11629 ? make_number (-relief)
11630 : make_number (relief)));
11631 hmargin -= relief;
11632 vmargin -= relief;
11635 /* Put a margin around the image. */
11636 if (hmargin || vmargin)
11638 if (hmargin == vmargin)
11639 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11640 else
11641 plist = Fplist_put (plist, QCmargin,
11642 Fcons (make_number (hmargin),
11643 make_number (vmargin)));
11646 /* If button is not enabled, and we don't have special images
11647 for the disabled state, make the image appear disabled by
11648 applying an appropriate algorithm to it. */
11649 if (!enabled_p && idx < 0)
11650 plist = Fplist_put (plist, QCconversion, Qdisabled);
11652 /* Put a `display' text property on the string for the image to
11653 display. Put a `menu-item' property on the string that gives
11654 the start of this item's properties in the tool-bar items
11655 vector. */
11656 image = Fcons (Qimage, plist);
11657 props = list4 (Qdisplay, image,
11658 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11660 /* Let the last image hide all remaining spaces in the tool bar
11661 string. The string can be longer than needed when we reuse a
11662 previous string. */
11663 if (i + 1 == f->n_tool_bar_items)
11664 end = SCHARS (f->desired_tool_bar_string);
11665 else
11666 end = i + 1;
11667 Fadd_text_properties (make_number (i), make_number (end),
11668 props, f->desired_tool_bar_string);
11669 #undef PROP
11672 UNGCPRO;
11676 /* Display one line of the tool-bar of frame IT->f.
11678 HEIGHT specifies the desired height of the tool-bar line.
11679 If the actual height of the glyph row is less than HEIGHT, the
11680 row's height is increased to HEIGHT, and the icons are centered
11681 vertically in the new height.
11683 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11684 count a final empty row in case the tool-bar width exactly matches
11685 the window width.
11688 static void
11689 display_tool_bar_line (struct it *it, int height)
11691 struct glyph_row *row = it->glyph_row;
11692 int max_x = it->last_visible_x;
11693 struct glyph *last;
11695 prepare_desired_row (row);
11696 row->y = it->current_y;
11698 /* Note that this isn't made use of if the face hasn't a box,
11699 so there's no need to check the face here. */
11700 it->start_of_box_run_p = 1;
11702 while (it->current_x < max_x)
11704 int x, n_glyphs_before, i, nglyphs;
11705 struct it it_before;
11707 /* Get the next display element. */
11708 if (!get_next_display_element (it))
11710 /* Don't count empty row if we are counting needed tool-bar lines. */
11711 if (height < 0 && !it->hpos)
11712 return;
11713 break;
11716 /* Produce glyphs. */
11717 n_glyphs_before = row->used[TEXT_AREA];
11718 it_before = *it;
11720 PRODUCE_GLYPHS (it);
11722 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11723 i = 0;
11724 x = it_before.current_x;
11725 while (i < nglyphs)
11727 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11729 if (x + glyph->pixel_width > max_x)
11731 /* Glyph doesn't fit on line. Backtrack. */
11732 row->used[TEXT_AREA] = n_glyphs_before;
11733 *it = it_before;
11734 /* If this is the only glyph on this line, it will never fit on the
11735 tool-bar, so skip it. But ensure there is at least one glyph,
11736 so we don't accidentally disable the tool-bar. */
11737 if (n_glyphs_before == 0
11738 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11739 break;
11740 goto out;
11743 ++it->hpos;
11744 x += glyph->pixel_width;
11745 ++i;
11748 /* Stop at line end. */
11749 if (ITERATOR_AT_END_OF_LINE_P (it))
11750 break;
11752 set_iterator_to_next (it, 1);
11755 out:;
11757 row->displays_text_p = row->used[TEXT_AREA] != 0;
11759 /* Use default face for the border below the tool bar.
11761 FIXME: When auto-resize-tool-bars is grow-only, there is
11762 no additional border below the possibly empty tool-bar lines.
11763 So to make the extra empty lines look "normal", we have to
11764 use the tool-bar face for the border too. */
11765 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11766 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11767 it->face_id = DEFAULT_FACE_ID;
11769 extend_face_to_end_of_line (it);
11770 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11771 last->right_box_line_p = 1;
11772 if (last == row->glyphs[TEXT_AREA])
11773 last->left_box_line_p = 1;
11775 /* Make line the desired height and center it vertically. */
11776 if ((height -= it->max_ascent + it->max_descent) > 0)
11778 /* Don't add more than one line height. */
11779 height %= FRAME_LINE_HEIGHT (it->f);
11780 it->max_ascent += height / 2;
11781 it->max_descent += (height + 1) / 2;
11784 compute_line_metrics (it);
11786 /* If line is empty, make it occupy the rest of the tool-bar. */
11787 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11789 row->height = row->phys_height = it->last_visible_y - row->y;
11790 row->visible_height = row->height;
11791 row->ascent = row->phys_ascent = 0;
11792 row->extra_line_spacing = 0;
11795 row->full_width_p = 1;
11796 row->continued_p = 0;
11797 row->truncated_on_left_p = 0;
11798 row->truncated_on_right_p = 0;
11800 it->current_x = it->hpos = 0;
11801 it->current_y += row->height;
11802 ++it->vpos;
11803 ++it->glyph_row;
11807 /* Max tool-bar height. */
11809 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11810 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11812 /* Value is the number of screen lines needed to make all tool-bar
11813 items of frame F visible. The number of actual rows needed is
11814 returned in *N_ROWS if non-NULL. */
11816 static int
11817 tool_bar_lines_needed (struct frame *f, int *n_rows)
11819 struct window *w = XWINDOW (f->tool_bar_window);
11820 struct it it;
11821 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11822 the desired matrix, so use (unused) mode-line row as temporary row to
11823 avoid destroying the first tool-bar row. */
11824 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11826 /* Initialize an iterator for iteration over
11827 F->desired_tool_bar_string in the tool-bar window of frame F. */
11828 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11829 it.first_visible_x = 0;
11830 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11831 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11832 it.paragraph_embedding = L2R;
11834 while (!ITERATOR_AT_END_P (&it))
11836 clear_glyph_row (temp_row);
11837 it.glyph_row = temp_row;
11838 display_tool_bar_line (&it, -1);
11840 clear_glyph_row (temp_row);
11842 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11843 if (n_rows)
11844 *n_rows = it.vpos > 0 ? it.vpos : -1;
11846 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11849 #endif /* !USE_GTK && !HAVE_NS */
11851 #if defined USE_GTK || defined HAVE_NS
11852 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
11853 #endif
11855 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11856 0, 1, 0,
11857 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11858 If FRAME is nil or omitted, use the selected frame. */)
11859 (Lisp_Object frame)
11861 int nlines = 0;
11862 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11863 struct frame *f = decode_any_frame (frame);
11864 struct window *w;
11866 if (WINDOWP (f->tool_bar_window)
11867 && (w = XWINDOW (f->tool_bar_window),
11868 WINDOW_TOTAL_LINES (w) > 0))
11870 update_tool_bar (f, 1);
11871 if (f->n_tool_bar_items)
11873 build_desired_tool_bar_string (f);
11874 nlines = tool_bar_lines_needed (f, NULL);
11877 #endif
11878 return make_number (nlines);
11882 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11883 height should be changed. */
11885 static int
11886 redisplay_tool_bar (struct frame *f)
11888 #if defined (USE_GTK) || defined (HAVE_NS)
11890 if (FRAME_EXTERNAL_TOOL_BAR (f))
11891 update_frame_tool_bar (f);
11892 return 0;
11894 #else /* !USE_GTK && !HAVE_NS */
11896 struct window *w;
11897 struct it it;
11898 struct glyph_row *row;
11900 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11901 do anything. This means you must start with tool-bar-lines
11902 non-zero to get the auto-sizing effect. Or in other words, you
11903 can turn off tool-bars by specifying tool-bar-lines zero. */
11904 if (!WINDOWP (f->tool_bar_window)
11905 || (w = XWINDOW (f->tool_bar_window),
11906 WINDOW_TOTAL_LINES (w) == 0))
11907 return 0;
11909 /* Set up an iterator for the tool-bar window. */
11910 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11911 it.first_visible_x = 0;
11912 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11913 row = it.glyph_row;
11915 /* Build a string that represents the contents of the tool-bar. */
11916 build_desired_tool_bar_string (f);
11917 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11918 /* FIXME: This should be controlled by a user option. But it
11919 doesn't make sense to have an R2L tool bar if the menu bar cannot
11920 be drawn also R2L, and making the menu bar R2L is tricky due
11921 toolkit-specific code that implements it. If an R2L tool bar is
11922 ever supported, display_tool_bar_line should also be augmented to
11923 call unproduce_glyphs like display_line and display_string
11924 do. */
11925 it.paragraph_embedding = L2R;
11927 if (f->n_tool_bar_rows == 0)
11929 int nlines;
11931 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11932 nlines != WINDOW_TOTAL_LINES (w)))
11934 Lisp_Object frame;
11935 int old_height = WINDOW_TOTAL_LINES (w);
11937 XSETFRAME (frame, f);
11938 Fmodify_frame_parameters (frame,
11939 list1 (Fcons (Qtool_bar_lines,
11940 make_number (nlines))));
11941 if (WINDOW_TOTAL_LINES (w) != old_height)
11943 clear_glyph_matrix (w->desired_matrix);
11944 f->fonts_changed = 1;
11945 return 1;
11950 /* Display as many lines as needed to display all tool-bar items. */
11952 if (f->n_tool_bar_rows > 0)
11954 int border, rows, height, extra;
11956 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11957 border = XINT (Vtool_bar_border);
11958 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11959 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11960 else if (EQ (Vtool_bar_border, Qborder_width))
11961 border = f->border_width;
11962 else
11963 border = 0;
11964 if (border < 0)
11965 border = 0;
11967 rows = f->n_tool_bar_rows;
11968 height = max (1, (it.last_visible_y - border) / rows);
11969 extra = it.last_visible_y - border - height * rows;
11971 while (it.current_y < it.last_visible_y)
11973 int h = 0;
11974 if (extra > 0 && rows-- > 0)
11976 h = (extra + rows - 1) / rows;
11977 extra -= h;
11979 display_tool_bar_line (&it, height + h);
11982 else
11984 while (it.current_y < it.last_visible_y)
11985 display_tool_bar_line (&it, 0);
11988 /* It doesn't make much sense to try scrolling in the tool-bar
11989 window, so don't do it. */
11990 w->desired_matrix->no_scrolling_p = 1;
11991 w->must_be_updated_p = 1;
11993 if (!NILP (Vauto_resize_tool_bars))
11995 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11996 int change_height_p = 0;
11998 /* If we couldn't display everything, change the tool-bar's
11999 height if there is room for more. */
12000 if (IT_STRING_CHARPOS (it) < it.end_charpos
12001 && it.current_y < max_tool_bar_height)
12002 change_height_p = 1;
12004 row = it.glyph_row - 1;
12006 /* If there are blank lines at the end, except for a partially
12007 visible blank line at the end that is smaller than
12008 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12009 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12010 && row->height >= FRAME_LINE_HEIGHT (f))
12011 change_height_p = 1;
12013 /* If row displays tool-bar items, but is partially visible,
12014 change the tool-bar's height. */
12015 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12016 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12017 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12018 change_height_p = 1;
12020 /* Resize windows as needed by changing the `tool-bar-lines'
12021 frame parameter. */
12022 if (change_height_p)
12024 Lisp_Object frame;
12025 int old_height = WINDOW_TOTAL_LINES (w);
12026 int nrows;
12027 int nlines = tool_bar_lines_needed (f, &nrows);
12029 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12030 && !f->minimize_tool_bar_window_p)
12031 ? (nlines > old_height)
12032 : (nlines != old_height));
12033 f->minimize_tool_bar_window_p = 0;
12035 if (change_height_p)
12037 XSETFRAME (frame, f);
12038 Fmodify_frame_parameters (frame,
12039 list1 (Fcons (Qtool_bar_lines,
12040 make_number (nlines))));
12041 if (WINDOW_TOTAL_LINES (w) != old_height)
12043 clear_glyph_matrix (w->desired_matrix);
12044 f->n_tool_bar_rows = nrows;
12045 f->fonts_changed = 1;
12046 return 1;
12052 f->minimize_tool_bar_window_p = 0;
12053 return 0;
12055 #endif /* USE_GTK || HAVE_NS */
12058 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12060 /* Get information about the tool-bar item which is displayed in GLYPH
12061 on frame F. Return in *PROP_IDX the index where tool-bar item
12062 properties start in F->tool_bar_items. Value is zero if
12063 GLYPH doesn't display a tool-bar item. */
12065 static int
12066 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12068 Lisp_Object prop;
12069 int success_p;
12070 int charpos;
12072 /* This function can be called asynchronously, which means we must
12073 exclude any possibility that Fget_text_property signals an
12074 error. */
12075 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12076 charpos = max (0, charpos);
12078 /* Get the text property `menu-item' at pos. The value of that
12079 property is the start index of this item's properties in
12080 F->tool_bar_items. */
12081 prop = Fget_text_property (make_number (charpos),
12082 Qmenu_item, f->current_tool_bar_string);
12083 if (INTEGERP (prop))
12085 *prop_idx = XINT (prop);
12086 success_p = 1;
12088 else
12089 success_p = 0;
12091 return success_p;
12095 /* Get information about the tool-bar item at position X/Y on frame F.
12096 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12097 the current matrix of the tool-bar window of F, or NULL if not
12098 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12099 item in F->tool_bar_items. Value is
12101 -1 if X/Y is not on a tool-bar item
12102 0 if X/Y is on the same item that was highlighted before.
12103 1 otherwise. */
12105 static int
12106 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12107 int *hpos, int *vpos, int *prop_idx)
12109 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12110 struct window *w = XWINDOW (f->tool_bar_window);
12111 int area;
12113 /* Find the glyph under X/Y. */
12114 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12115 if (*glyph == NULL)
12116 return -1;
12118 /* Get the start of this tool-bar item's properties in
12119 f->tool_bar_items. */
12120 if (!tool_bar_item_info (f, *glyph, prop_idx))
12121 return -1;
12123 /* Is mouse on the highlighted item? */
12124 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12125 && *vpos >= hlinfo->mouse_face_beg_row
12126 && *vpos <= hlinfo->mouse_face_end_row
12127 && (*vpos > hlinfo->mouse_face_beg_row
12128 || *hpos >= hlinfo->mouse_face_beg_col)
12129 && (*vpos < hlinfo->mouse_face_end_row
12130 || *hpos < hlinfo->mouse_face_end_col
12131 || hlinfo->mouse_face_past_end))
12132 return 0;
12134 return 1;
12138 /* EXPORT:
12139 Handle mouse button event on the tool-bar of frame F, at
12140 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12141 0 for button release. MODIFIERS is event modifiers for button
12142 release. */
12144 void
12145 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12146 int modifiers)
12148 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12149 struct window *w = XWINDOW (f->tool_bar_window);
12150 int hpos, vpos, prop_idx;
12151 struct glyph *glyph;
12152 Lisp_Object enabled_p;
12153 int ts;
12155 /* If not on the highlighted tool-bar item, and mouse-highlight is
12156 non-nil, return. This is so we generate the tool-bar button
12157 click only when the mouse button is released on the same item as
12158 where it was pressed. However, when mouse-highlight is disabled,
12159 generate the click when the button is released regardless of the
12160 highlight, since tool-bar items are not highlighted in that
12161 case. */
12162 frame_to_window_pixel_xy (w, &x, &y);
12163 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12164 if (ts == -1
12165 || (ts != 0 && !NILP (Vmouse_highlight)))
12166 return;
12168 /* When mouse-highlight is off, generate the click for the item
12169 where the button was pressed, disregarding where it was
12170 released. */
12171 if (NILP (Vmouse_highlight) && !down_p)
12172 prop_idx = last_tool_bar_item;
12174 /* If item is disabled, do nothing. */
12175 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12176 if (NILP (enabled_p))
12177 return;
12179 if (down_p)
12181 /* Show item in pressed state. */
12182 if (!NILP (Vmouse_highlight))
12183 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12184 last_tool_bar_item = prop_idx;
12186 else
12188 Lisp_Object key, frame;
12189 struct input_event event;
12190 EVENT_INIT (event);
12192 /* Show item in released state. */
12193 if (!NILP (Vmouse_highlight))
12194 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12196 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12198 XSETFRAME (frame, f);
12199 event.kind = TOOL_BAR_EVENT;
12200 event.frame_or_window = frame;
12201 event.arg = frame;
12202 kbd_buffer_store_event (&event);
12204 event.kind = TOOL_BAR_EVENT;
12205 event.frame_or_window = frame;
12206 event.arg = key;
12207 event.modifiers = modifiers;
12208 kbd_buffer_store_event (&event);
12209 last_tool_bar_item = -1;
12214 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12215 tool-bar window-relative coordinates X/Y. Called from
12216 note_mouse_highlight. */
12218 static void
12219 note_tool_bar_highlight (struct frame *f, int x, int y)
12221 Lisp_Object window = f->tool_bar_window;
12222 struct window *w = XWINDOW (window);
12223 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12224 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12225 int hpos, vpos;
12226 struct glyph *glyph;
12227 struct glyph_row *row;
12228 int i;
12229 Lisp_Object enabled_p;
12230 int prop_idx;
12231 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12232 int mouse_down_p, rc;
12234 /* Function note_mouse_highlight is called with negative X/Y
12235 values when mouse moves outside of the frame. */
12236 if (x <= 0 || y <= 0)
12238 clear_mouse_face (hlinfo);
12239 return;
12242 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12243 if (rc < 0)
12245 /* Not on tool-bar item. */
12246 clear_mouse_face (hlinfo);
12247 return;
12249 else if (rc == 0)
12250 /* On same tool-bar item as before. */
12251 goto set_help_echo;
12253 clear_mouse_face (hlinfo);
12255 /* Mouse is down, but on different tool-bar item? */
12256 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12257 && f == dpyinfo->last_mouse_frame);
12259 if (mouse_down_p
12260 && last_tool_bar_item != prop_idx)
12261 return;
12263 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12265 /* If tool-bar item is not enabled, don't highlight it. */
12266 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12267 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12269 /* Compute the x-position of the glyph. In front and past the
12270 image is a space. We include this in the highlighted area. */
12271 row = MATRIX_ROW (w->current_matrix, vpos);
12272 for (i = x = 0; i < hpos; ++i)
12273 x += row->glyphs[TEXT_AREA][i].pixel_width;
12275 /* Record this as the current active region. */
12276 hlinfo->mouse_face_beg_col = hpos;
12277 hlinfo->mouse_face_beg_row = vpos;
12278 hlinfo->mouse_face_beg_x = x;
12279 hlinfo->mouse_face_past_end = 0;
12281 hlinfo->mouse_face_end_col = hpos + 1;
12282 hlinfo->mouse_face_end_row = vpos;
12283 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12284 hlinfo->mouse_face_window = window;
12285 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12287 /* Display it as active. */
12288 show_mouse_face (hlinfo, draw);
12291 set_help_echo:
12293 /* Set help_echo_string to a help string to display for this tool-bar item.
12294 XTread_socket does the rest. */
12295 help_echo_object = help_echo_window = Qnil;
12296 help_echo_pos = -1;
12297 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12298 if (NILP (help_echo_string))
12299 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12302 #endif /* !USE_GTK && !HAVE_NS */
12304 #endif /* HAVE_WINDOW_SYSTEM */
12308 /************************************************************************
12309 Horizontal scrolling
12310 ************************************************************************/
12312 static int hscroll_window_tree (Lisp_Object);
12313 static int hscroll_windows (Lisp_Object);
12315 /* For all leaf windows in the window tree rooted at WINDOW, set their
12316 hscroll value so that PT is (i) visible in the window, and (ii) so
12317 that it is not within a certain margin at the window's left and
12318 right border. Value is non-zero if any window's hscroll has been
12319 changed. */
12321 static int
12322 hscroll_window_tree (Lisp_Object window)
12324 int hscrolled_p = 0;
12325 int hscroll_relative_p = FLOATP (Vhscroll_step);
12326 int hscroll_step_abs = 0;
12327 double hscroll_step_rel = 0;
12329 if (hscroll_relative_p)
12331 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12332 if (hscroll_step_rel < 0)
12334 hscroll_relative_p = 0;
12335 hscroll_step_abs = 0;
12338 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12340 hscroll_step_abs = XINT (Vhscroll_step);
12341 if (hscroll_step_abs < 0)
12342 hscroll_step_abs = 0;
12344 else
12345 hscroll_step_abs = 0;
12347 while (WINDOWP (window))
12349 struct window *w = XWINDOW (window);
12351 if (WINDOWP (w->contents))
12352 hscrolled_p |= hscroll_window_tree (w->contents);
12353 else if (w->cursor.vpos >= 0)
12355 int h_margin;
12356 int text_area_width;
12357 struct glyph_row *current_cursor_row
12358 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12359 struct glyph_row *desired_cursor_row
12360 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12361 struct glyph_row *cursor_row
12362 = (desired_cursor_row->enabled_p
12363 ? desired_cursor_row
12364 : current_cursor_row);
12365 int row_r2l_p = cursor_row->reversed_p;
12367 text_area_width = window_box_width (w, TEXT_AREA);
12369 /* Scroll when cursor is inside this scroll margin. */
12370 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12372 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12373 /* For left-to-right rows, hscroll when cursor is either
12374 (i) inside the right hscroll margin, or (ii) if it is
12375 inside the left margin and the window is already
12376 hscrolled. */
12377 && ((!row_r2l_p
12378 && ((w->hscroll
12379 && w->cursor.x <= h_margin)
12380 || (cursor_row->enabled_p
12381 && cursor_row->truncated_on_right_p
12382 && (w->cursor.x >= text_area_width - h_margin))))
12383 /* For right-to-left rows, the logic is similar,
12384 except that rules for scrolling to left and right
12385 are reversed. E.g., if cursor.x <= h_margin, we
12386 need to hscroll "to the right" unconditionally,
12387 and that will scroll the screen to the left so as
12388 to reveal the next portion of the row. */
12389 || (row_r2l_p
12390 && ((cursor_row->enabled_p
12391 /* FIXME: It is confusing to set the
12392 truncated_on_right_p flag when R2L rows
12393 are actually truncated on the left. */
12394 && cursor_row->truncated_on_right_p
12395 && w->cursor.x <= h_margin)
12396 || (w->hscroll
12397 && (w->cursor.x >= text_area_width - h_margin))))))
12399 struct it it;
12400 ptrdiff_t hscroll;
12401 struct buffer *saved_current_buffer;
12402 ptrdiff_t pt;
12403 int wanted_x;
12405 /* Find point in a display of infinite width. */
12406 saved_current_buffer = current_buffer;
12407 current_buffer = XBUFFER (w->contents);
12409 if (w == XWINDOW (selected_window))
12410 pt = PT;
12411 else
12412 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12414 /* Move iterator to pt starting at cursor_row->start in
12415 a line with infinite width. */
12416 init_to_row_start (&it, w, cursor_row);
12417 it.last_visible_x = INFINITY;
12418 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12419 current_buffer = saved_current_buffer;
12421 /* Position cursor in window. */
12422 if (!hscroll_relative_p && hscroll_step_abs == 0)
12423 hscroll = max (0, (it.current_x
12424 - (ITERATOR_AT_END_OF_LINE_P (&it)
12425 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12426 : (text_area_width / 2))))
12427 / FRAME_COLUMN_WIDTH (it.f);
12428 else if ((!row_r2l_p
12429 && w->cursor.x >= text_area_width - h_margin)
12430 || (row_r2l_p && w->cursor.x <= h_margin))
12432 if (hscroll_relative_p)
12433 wanted_x = text_area_width * (1 - hscroll_step_rel)
12434 - h_margin;
12435 else
12436 wanted_x = text_area_width
12437 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12438 - h_margin;
12439 hscroll
12440 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12442 else
12444 if (hscroll_relative_p)
12445 wanted_x = text_area_width * hscroll_step_rel
12446 + h_margin;
12447 else
12448 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12449 + h_margin;
12450 hscroll
12451 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12453 hscroll = max (hscroll, w->min_hscroll);
12455 /* Don't prevent redisplay optimizations if hscroll
12456 hasn't changed, as it will unnecessarily slow down
12457 redisplay. */
12458 if (w->hscroll != hscroll)
12460 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12461 w->hscroll = hscroll;
12462 hscrolled_p = 1;
12467 window = w->next;
12470 /* Value is non-zero if hscroll of any leaf window has been changed. */
12471 return hscrolled_p;
12475 /* Set hscroll so that cursor is visible and not inside horizontal
12476 scroll margins for all windows in the tree rooted at WINDOW. See
12477 also hscroll_window_tree above. Value is non-zero if any window's
12478 hscroll has been changed. If it has, desired matrices on the frame
12479 of WINDOW are cleared. */
12481 static int
12482 hscroll_windows (Lisp_Object window)
12484 int hscrolled_p = hscroll_window_tree (window);
12485 if (hscrolled_p)
12486 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12487 return hscrolled_p;
12492 /************************************************************************
12493 Redisplay
12494 ************************************************************************/
12496 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12497 to a non-zero value. This is sometimes handy to have in a debugger
12498 session. */
12500 #ifdef GLYPH_DEBUG
12502 /* First and last unchanged row for try_window_id. */
12504 static int debug_first_unchanged_at_end_vpos;
12505 static int debug_last_unchanged_at_beg_vpos;
12507 /* Delta vpos and y. */
12509 static int debug_dvpos, debug_dy;
12511 /* Delta in characters and bytes for try_window_id. */
12513 static ptrdiff_t debug_delta, debug_delta_bytes;
12515 /* Values of window_end_pos and window_end_vpos at the end of
12516 try_window_id. */
12518 static ptrdiff_t debug_end_vpos;
12520 /* Append a string to W->desired_matrix->method. FMT is a printf
12521 format string. If trace_redisplay_p is non-zero also printf the
12522 resulting string to stderr. */
12524 static void debug_method_add (struct window *, char const *, ...)
12525 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12527 static void
12528 debug_method_add (struct window *w, char const *fmt, ...)
12530 void *ptr = w;
12531 char *method = w->desired_matrix->method;
12532 int len = strlen (method);
12533 int size = sizeof w->desired_matrix->method;
12534 int remaining = size - len - 1;
12535 va_list ap;
12537 if (len && remaining)
12539 method[len] = '|';
12540 --remaining, ++len;
12543 va_start (ap, fmt);
12544 vsnprintf (method + len, remaining + 1, fmt, ap);
12545 va_end (ap);
12547 if (trace_redisplay_p)
12548 fprintf (stderr, "%p (%s): %s\n",
12549 ptr,
12550 ((BUFFERP (w->contents)
12551 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12552 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12553 : "no buffer"),
12554 method + len);
12557 #endif /* GLYPH_DEBUG */
12560 /* Value is non-zero if all changes in window W, which displays
12561 current_buffer, are in the text between START and END. START is a
12562 buffer position, END is given as a distance from Z. Used in
12563 redisplay_internal for display optimization. */
12565 static int
12566 text_outside_line_unchanged_p (struct window *w,
12567 ptrdiff_t start, ptrdiff_t end)
12569 int unchanged_p = 1;
12571 /* If text or overlays have changed, see where. */
12572 if (window_outdated (w))
12574 /* Gap in the line? */
12575 if (GPT < start || Z - GPT < end)
12576 unchanged_p = 0;
12578 /* Changes start in front of the line, or end after it? */
12579 if (unchanged_p
12580 && (BEG_UNCHANGED < start - 1
12581 || END_UNCHANGED < end))
12582 unchanged_p = 0;
12584 /* If selective display, can't optimize if changes start at the
12585 beginning of the line. */
12586 if (unchanged_p
12587 && INTEGERP (BVAR (current_buffer, selective_display))
12588 && XINT (BVAR (current_buffer, selective_display)) > 0
12589 && (BEG_UNCHANGED < start || GPT <= start))
12590 unchanged_p = 0;
12592 /* If there are overlays at the start or end of the line, these
12593 may have overlay strings with newlines in them. A change at
12594 START, for instance, may actually concern the display of such
12595 overlay strings as well, and they are displayed on different
12596 lines. So, quickly rule out this case. (For the future, it
12597 might be desirable to implement something more telling than
12598 just BEG/END_UNCHANGED.) */
12599 if (unchanged_p)
12601 if (BEG + BEG_UNCHANGED == start
12602 && overlay_touches_p (start))
12603 unchanged_p = 0;
12604 if (END_UNCHANGED == end
12605 && overlay_touches_p (Z - end))
12606 unchanged_p = 0;
12609 /* Under bidi reordering, adding or deleting a character in the
12610 beginning of a paragraph, before the first strong directional
12611 character, can change the base direction of the paragraph (unless
12612 the buffer specifies a fixed paragraph direction), which will
12613 require to redisplay the whole paragraph. It might be worthwhile
12614 to find the paragraph limits and widen the range of redisplayed
12615 lines to that, but for now just give up this optimization. */
12616 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12617 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12618 unchanged_p = 0;
12621 return unchanged_p;
12625 /* Do a frame update, taking possible shortcuts into account. This is
12626 the main external entry point for redisplay.
12628 If the last redisplay displayed an echo area message and that message
12629 is no longer requested, we clear the echo area or bring back the
12630 mini-buffer if that is in use. */
12632 void
12633 redisplay (void)
12635 redisplay_internal ();
12639 static Lisp_Object
12640 overlay_arrow_string_or_property (Lisp_Object var)
12642 Lisp_Object val;
12644 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12645 return val;
12647 return Voverlay_arrow_string;
12650 /* Return 1 if there are any overlay-arrows in current_buffer. */
12651 static int
12652 overlay_arrow_in_current_buffer_p (void)
12654 Lisp_Object vlist;
12656 for (vlist = Voverlay_arrow_variable_list;
12657 CONSP (vlist);
12658 vlist = XCDR (vlist))
12660 Lisp_Object var = XCAR (vlist);
12661 Lisp_Object val;
12663 if (!SYMBOLP (var))
12664 continue;
12665 val = find_symbol_value (var);
12666 if (MARKERP (val)
12667 && current_buffer == XMARKER (val)->buffer)
12668 return 1;
12670 return 0;
12674 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12675 has changed. */
12677 static int
12678 overlay_arrows_changed_p (void)
12680 Lisp_Object vlist;
12682 for (vlist = Voverlay_arrow_variable_list;
12683 CONSP (vlist);
12684 vlist = XCDR (vlist))
12686 Lisp_Object var = XCAR (vlist);
12687 Lisp_Object val, pstr;
12689 if (!SYMBOLP (var))
12690 continue;
12691 val = find_symbol_value (var);
12692 if (!MARKERP (val))
12693 continue;
12694 if (! EQ (COERCE_MARKER (val),
12695 Fget (var, Qlast_arrow_position))
12696 || ! (pstr = overlay_arrow_string_or_property (var),
12697 EQ (pstr, Fget (var, Qlast_arrow_string))))
12698 return 1;
12700 return 0;
12703 /* Mark overlay arrows to be updated on next redisplay. */
12705 static void
12706 update_overlay_arrows (int up_to_date)
12708 Lisp_Object vlist;
12710 for (vlist = Voverlay_arrow_variable_list;
12711 CONSP (vlist);
12712 vlist = XCDR (vlist))
12714 Lisp_Object var = XCAR (vlist);
12716 if (!SYMBOLP (var))
12717 continue;
12719 if (up_to_date > 0)
12721 Lisp_Object val = find_symbol_value (var);
12722 Fput (var, Qlast_arrow_position,
12723 COERCE_MARKER (val));
12724 Fput (var, Qlast_arrow_string,
12725 overlay_arrow_string_or_property (var));
12727 else if (up_to_date < 0
12728 || !NILP (Fget (var, Qlast_arrow_position)))
12730 Fput (var, Qlast_arrow_position, Qt);
12731 Fput (var, Qlast_arrow_string, Qt);
12737 /* Return overlay arrow string to display at row.
12738 Return integer (bitmap number) for arrow bitmap in left fringe.
12739 Return nil if no overlay arrow. */
12741 static Lisp_Object
12742 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12744 Lisp_Object vlist;
12746 for (vlist = Voverlay_arrow_variable_list;
12747 CONSP (vlist);
12748 vlist = XCDR (vlist))
12750 Lisp_Object var = XCAR (vlist);
12751 Lisp_Object val;
12753 if (!SYMBOLP (var))
12754 continue;
12756 val = find_symbol_value (var);
12758 if (MARKERP (val)
12759 && current_buffer == XMARKER (val)->buffer
12760 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12762 if (FRAME_WINDOW_P (it->f)
12763 /* FIXME: if ROW->reversed_p is set, this should test
12764 the right fringe, not the left one. */
12765 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12767 #ifdef HAVE_WINDOW_SYSTEM
12768 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12770 int fringe_bitmap;
12771 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12772 return make_number (fringe_bitmap);
12774 #endif
12775 return make_number (-1); /* Use default arrow bitmap. */
12777 return overlay_arrow_string_or_property (var);
12781 return Qnil;
12784 /* Return 1 if point moved out of or into a composition. Otherwise
12785 return 0. PREV_BUF and PREV_PT are the last point buffer and
12786 position. BUF and PT are the current point buffer and position. */
12788 static int
12789 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12790 struct buffer *buf, ptrdiff_t pt)
12792 ptrdiff_t start, end;
12793 Lisp_Object prop;
12794 Lisp_Object buffer;
12796 XSETBUFFER (buffer, buf);
12797 /* Check a composition at the last point if point moved within the
12798 same buffer. */
12799 if (prev_buf == buf)
12801 if (prev_pt == pt)
12802 /* Point didn't move. */
12803 return 0;
12805 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12806 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12807 && composition_valid_p (start, end, prop)
12808 && start < prev_pt && end > prev_pt)
12809 /* The last point was within the composition. Return 1 iff
12810 point moved out of the composition. */
12811 return (pt <= start || pt >= end);
12814 /* Check a composition at the current point. */
12815 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12816 && find_composition (pt, -1, &start, &end, &prop, buffer)
12817 && composition_valid_p (start, end, prop)
12818 && start < pt && end > pt);
12821 /* Reconsider the clip changes of buffer which is displayed in W. */
12823 static void
12824 reconsider_clip_changes (struct window *w)
12826 struct buffer *b = XBUFFER (w->contents);
12828 if (b->clip_changed
12829 && w->window_end_valid
12830 && w->current_matrix->buffer == b
12831 && w->current_matrix->zv == BUF_ZV (b)
12832 && w->current_matrix->begv == BUF_BEGV (b))
12833 b->clip_changed = 0;
12835 /* If display wasn't paused, and W is not a tool bar window, see if
12836 point has been moved into or out of a composition. In that case,
12837 we set b->clip_changed to 1 to force updating the screen. If
12838 b->clip_changed has already been set to 1, we can skip this
12839 check. */
12840 if (!b->clip_changed && w->window_end_valid)
12842 ptrdiff_t pt = (w == XWINDOW (selected_window)
12843 ? PT : marker_position (w->pointm));
12845 if ((w->current_matrix->buffer != b || pt != w->last_point)
12846 && check_point_in_composition (w->current_matrix->buffer,
12847 w->last_point, b, pt))
12848 b->clip_changed = 1;
12852 #define STOP_POLLING \
12853 do { if (! polling_stopped_here) stop_polling (); \
12854 polling_stopped_here = 1; } while (0)
12856 #define RESUME_POLLING \
12857 do { if (polling_stopped_here) start_polling (); \
12858 polling_stopped_here = 0; } while (0)
12861 /* Perhaps in the future avoid recentering windows if it
12862 is not necessary; currently that causes some problems. */
12864 static void
12865 redisplay_internal (void)
12867 struct window *w = XWINDOW (selected_window);
12868 struct window *sw;
12869 struct frame *fr;
12870 int pending;
12871 bool must_finish = 0, match_p;
12872 struct text_pos tlbufpos, tlendpos;
12873 int number_of_visible_frames;
12874 ptrdiff_t count;
12875 struct frame *sf;
12876 int polling_stopped_here = 0;
12877 Lisp_Object tail, frame;
12879 /* Non-zero means redisplay has to consider all windows on all
12880 frames. Zero means, only selected_window is considered. */
12881 int consider_all_windows_p;
12883 /* Non-zero means redisplay has to redisplay the miniwindow. */
12884 int update_miniwindow_p = 0;
12886 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12888 /* No redisplay if running in batch mode or frame is not yet fully
12889 initialized, or redisplay is explicitly turned off by setting
12890 Vinhibit_redisplay. */
12891 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12892 || !NILP (Vinhibit_redisplay))
12893 return;
12895 /* Don't examine these until after testing Vinhibit_redisplay.
12896 When Emacs is shutting down, perhaps because its connection to
12897 X has dropped, we should not look at them at all. */
12898 fr = XFRAME (w->frame);
12899 sf = SELECTED_FRAME ();
12901 if (!fr->glyphs_initialized_p)
12902 return;
12904 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12905 if (popup_activated ())
12906 return;
12907 #endif
12909 /* I don't think this happens but let's be paranoid. */
12910 if (redisplaying_p)
12911 return;
12913 /* Record a function that clears redisplaying_p
12914 when we leave this function. */
12915 count = SPECPDL_INDEX ();
12916 record_unwind_protect_void (unwind_redisplay);
12917 redisplaying_p = 1;
12918 specbind (Qinhibit_free_realized_faces, Qnil);
12920 /* Record this function, so it appears on the profiler's backtraces. */
12921 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12923 FOR_EACH_FRAME (tail, frame)
12924 XFRAME (frame)->already_hscrolled_p = 0;
12926 retry:
12927 /* Remember the currently selected window. */
12928 sw = w;
12930 pending = 0;
12931 last_escape_glyph_frame = NULL;
12932 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12933 last_glyphless_glyph_frame = NULL;
12934 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12936 /* If face_change_count is non-zero, init_iterator will free all
12937 realized faces, which includes the faces referenced from current
12938 matrices. So, we can't reuse current matrices in this case. */
12939 if (face_change_count)
12940 ++windows_or_buffers_changed;
12942 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12943 && FRAME_TTY (sf)->previous_frame != sf)
12945 /* Since frames on a single ASCII terminal share the same
12946 display area, displaying a different frame means redisplay
12947 the whole thing. */
12948 windows_or_buffers_changed++;
12949 SET_FRAME_GARBAGED (sf);
12950 #ifndef DOS_NT
12951 set_tty_color_mode (FRAME_TTY (sf), sf);
12952 #endif
12953 FRAME_TTY (sf)->previous_frame = sf;
12956 /* Set the visible flags for all frames. Do this before checking for
12957 resized or garbaged frames; they want to know if their frames are
12958 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12959 number_of_visible_frames = 0;
12961 FOR_EACH_FRAME (tail, frame)
12963 struct frame *f = XFRAME (frame);
12965 if (FRAME_VISIBLE_P (f))
12967 ++number_of_visible_frames;
12968 /* Adjust matrices for visible frames only. */
12969 if (f->fonts_changed)
12971 adjust_frame_glyphs (f);
12972 f->fonts_changed = 0;
12974 /* If cursor type has been changed on the frame
12975 other than selected, consider all frames. */
12976 if (f != sf && f->cursor_type_changed)
12977 update_mode_lines++;
12979 clear_desired_matrices (f);
12982 /* Notice any pending interrupt request to change frame size. */
12983 do_pending_window_change (1);
12985 /* do_pending_window_change could change the selected_window due to
12986 frame resizing which makes the selected window too small. */
12987 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12988 sw = w;
12990 /* Clear frames marked as garbaged. */
12991 clear_garbaged_frames ();
12993 /* Build menubar and tool-bar items. */
12994 if (NILP (Vmemory_full))
12995 prepare_menu_bars ();
12997 if (windows_or_buffers_changed)
12998 update_mode_lines++;
13000 reconsider_clip_changes (w);
13002 /* In most cases selected window displays current buffer. */
13003 match_p = XBUFFER (w->contents) == current_buffer;
13004 if (match_p)
13006 /* Detect case that we need to write or remove a star in the mode line. */
13007 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13009 w->update_mode_line = 1;
13010 if (buffer_shared_and_changed ())
13011 update_mode_lines++;
13014 if (mode_line_update_needed (w))
13015 w->update_mode_line = 1;
13018 consider_all_windows_p = (update_mode_lines
13019 || buffer_shared_and_changed ());
13021 /* If specs for an arrow have changed, do thorough redisplay
13022 to ensure we remove any arrow that should no longer exist. */
13023 if (overlay_arrows_changed_p ())
13024 consider_all_windows_p = windows_or_buffers_changed = 1;
13026 /* Normally the message* functions will have already displayed and
13027 updated the echo area, but the frame may have been trashed, or
13028 the update may have been preempted, so display the echo area
13029 again here. Checking message_cleared_p captures the case that
13030 the echo area should be cleared. */
13031 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13032 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13033 || (message_cleared_p
13034 && minibuf_level == 0
13035 /* If the mini-window is currently selected, this means the
13036 echo-area doesn't show through. */
13037 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13039 int window_height_changed_p = echo_area_display (0);
13041 if (message_cleared_p)
13042 update_miniwindow_p = 1;
13044 must_finish = 1;
13046 /* If we don't display the current message, don't clear the
13047 message_cleared_p flag, because, if we did, we wouldn't clear
13048 the echo area in the next redisplay which doesn't preserve
13049 the echo area. */
13050 if (!display_last_displayed_message_p)
13051 message_cleared_p = 0;
13053 if (window_height_changed_p)
13055 consider_all_windows_p = 1;
13056 ++update_mode_lines;
13057 ++windows_or_buffers_changed;
13059 /* If window configuration was changed, frames may have been
13060 marked garbaged. Clear them or we will experience
13061 surprises wrt scrolling. */
13062 clear_garbaged_frames ();
13065 else if (EQ (selected_window, minibuf_window)
13066 && (current_buffer->clip_changed || window_outdated (w))
13067 && resize_mini_window (w, 0))
13069 /* Resized active mini-window to fit the size of what it is
13070 showing if its contents might have changed. */
13071 must_finish = 1;
13072 /* FIXME: this causes all frames to be updated, which seems unnecessary
13073 since only the current frame needs to be considered. This function
13074 needs to be rewritten with two variables, consider_all_windows and
13075 consider_all_frames. */
13076 consider_all_windows_p = 1;
13077 ++windows_or_buffers_changed;
13078 ++update_mode_lines;
13080 /* If window configuration was changed, frames may have been
13081 marked garbaged. Clear them or we will experience
13082 surprises wrt scrolling. */
13083 clear_garbaged_frames ();
13086 /* Optimize the case that only the line containing the cursor in the
13087 selected window has changed. Variables starting with this_ are
13088 set in display_line and record information about the line
13089 containing the cursor. */
13090 tlbufpos = this_line_start_pos;
13091 tlendpos = this_line_end_pos;
13092 if (!consider_all_windows_p
13093 && CHARPOS (tlbufpos) > 0
13094 && !w->update_mode_line
13095 && !current_buffer->clip_changed
13096 && !current_buffer->prevent_redisplay_optimizations_p
13097 && FRAME_VISIBLE_P (XFRAME (w->frame))
13098 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13099 && !XFRAME (w->frame)->cursor_type_changed
13100 /* Make sure recorded data applies to current buffer, etc. */
13101 && this_line_buffer == current_buffer
13102 && match_p
13103 && !w->force_start
13104 && !w->optional_new_start
13105 /* Point must be on the line that we have info recorded about. */
13106 && PT >= CHARPOS (tlbufpos)
13107 && PT <= Z - CHARPOS (tlendpos)
13108 /* All text outside that line, including its final newline,
13109 must be unchanged. */
13110 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13111 CHARPOS (tlendpos)))
13113 if (CHARPOS (tlbufpos) > BEGV
13114 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13115 && (CHARPOS (tlbufpos) == ZV
13116 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13117 /* Former continuation line has disappeared by becoming empty. */
13118 goto cancel;
13119 else if (window_outdated (w) || MINI_WINDOW_P (w))
13121 /* We have to handle the case of continuation around a
13122 wide-column character (see the comment in indent.c around
13123 line 1340).
13125 For instance, in the following case:
13127 -------- Insert --------
13128 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13129 J_I_ ==> J_I_ `^^' are cursors.
13130 ^^ ^^
13131 -------- --------
13133 As we have to redraw the line above, we cannot use this
13134 optimization. */
13136 struct it it;
13137 int line_height_before = this_line_pixel_height;
13139 /* Note that start_display will handle the case that the
13140 line starting at tlbufpos is a continuation line. */
13141 start_display (&it, w, tlbufpos);
13143 /* Implementation note: It this still necessary? */
13144 if (it.current_x != this_line_start_x)
13145 goto cancel;
13147 TRACE ((stderr, "trying display optimization 1\n"));
13148 w->cursor.vpos = -1;
13149 overlay_arrow_seen = 0;
13150 it.vpos = this_line_vpos;
13151 it.current_y = this_line_y;
13152 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13153 display_line (&it);
13155 /* If line contains point, is not continued,
13156 and ends at same distance from eob as before, we win. */
13157 if (w->cursor.vpos >= 0
13158 /* Line is not continued, otherwise this_line_start_pos
13159 would have been set to 0 in display_line. */
13160 && CHARPOS (this_line_start_pos)
13161 /* Line ends as before. */
13162 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13163 /* Line has same height as before. Otherwise other lines
13164 would have to be shifted up or down. */
13165 && this_line_pixel_height == line_height_before)
13167 /* If this is not the window's last line, we must adjust
13168 the charstarts of the lines below. */
13169 if (it.current_y < it.last_visible_y)
13171 struct glyph_row *row
13172 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13173 ptrdiff_t delta, delta_bytes;
13175 /* We used to distinguish between two cases here,
13176 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13177 when the line ends in a newline or the end of the
13178 buffer's accessible portion. But both cases did
13179 the same, so they were collapsed. */
13180 delta = (Z
13181 - CHARPOS (tlendpos)
13182 - MATRIX_ROW_START_CHARPOS (row));
13183 delta_bytes = (Z_BYTE
13184 - BYTEPOS (tlendpos)
13185 - MATRIX_ROW_START_BYTEPOS (row));
13187 increment_matrix_positions (w->current_matrix,
13188 this_line_vpos + 1,
13189 w->current_matrix->nrows,
13190 delta, delta_bytes);
13193 /* If this row displays text now but previously didn't,
13194 or vice versa, w->window_end_vpos may have to be
13195 adjusted. */
13196 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13198 if (w->window_end_vpos < this_line_vpos)
13199 w->window_end_vpos = this_line_vpos;
13201 else if (w->window_end_vpos == this_line_vpos
13202 && this_line_vpos > 0)
13203 w->window_end_vpos = this_line_vpos - 1;
13204 w->window_end_valid = 0;
13206 /* Update hint: No need to try to scroll in update_window. */
13207 w->desired_matrix->no_scrolling_p = 1;
13209 #ifdef GLYPH_DEBUG
13210 *w->desired_matrix->method = 0;
13211 debug_method_add (w, "optimization 1");
13212 #endif
13213 #ifdef HAVE_WINDOW_SYSTEM
13214 update_window_fringes (w, 0);
13215 #endif
13216 goto update;
13218 else
13219 goto cancel;
13221 else if (/* Cursor position hasn't changed. */
13222 PT == w->last_point
13223 /* Make sure the cursor was last displayed
13224 in this window. Otherwise we have to reposition it. */
13225 && 0 <= w->cursor.vpos
13226 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13228 if (!must_finish)
13230 do_pending_window_change (1);
13231 /* If selected_window changed, redisplay again. */
13232 if (WINDOWP (selected_window)
13233 && (w = XWINDOW (selected_window)) != sw)
13234 goto retry;
13236 /* We used to always goto end_of_redisplay here, but this
13237 isn't enough if we have a blinking cursor. */
13238 if (w->cursor_off_p == w->last_cursor_off_p)
13239 goto end_of_redisplay;
13241 goto update;
13243 /* If highlighting the region, or if the cursor is in the echo area,
13244 then we can't just move the cursor. */
13245 else if (NILP (Vshow_trailing_whitespace)
13246 && !cursor_in_echo_area)
13248 struct it it;
13249 struct glyph_row *row;
13251 /* Skip from tlbufpos to PT and see where it is. Note that
13252 PT may be in invisible text. If so, we will end at the
13253 next visible position. */
13254 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13255 NULL, DEFAULT_FACE_ID);
13256 it.current_x = this_line_start_x;
13257 it.current_y = this_line_y;
13258 it.vpos = this_line_vpos;
13260 /* The call to move_it_to stops in front of PT, but
13261 moves over before-strings. */
13262 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13264 if (it.vpos == this_line_vpos
13265 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13266 row->enabled_p))
13268 eassert (this_line_vpos == it.vpos);
13269 eassert (this_line_y == it.current_y);
13270 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13271 #ifdef GLYPH_DEBUG
13272 *w->desired_matrix->method = 0;
13273 debug_method_add (w, "optimization 3");
13274 #endif
13275 goto update;
13277 else
13278 goto cancel;
13281 cancel:
13282 /* Text changed drastically or point moved off of line. */
13283 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13286 CHARPOS (this_line_start_pos) = 0;
13287 consider_all_windows_p |= buffer_shared_and_changed ();
13288 ++clear_face_cache_count;
13289 #ifdef HAVE_WINDOW_SYSTEM
13290 ++clear_image_cache_count;
13291 #endif
13293 /* Build desired matrices, and update the display. If
13294 consider_all_windows_p is non-zero, do it for all windows on all
13295 frames. Otherwise do it for selected_window, only. */
13297 if (consider_all_windows_p)
13299 FOR_EACH_FRAME (tail, frame)
13300 XFRAME (frame)->updated_p = 0;
13302 FOR_EACH_FRAME (tail, frame)
13304 struct frame *f = XFRAME (frame);
13306 /* We don't have to do anything for unselected terminal
13307 frames. */
13308 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13309 && !EQ (FRAME_TTY (f)->top_frame, frame))
13310 continue;
13312 retry_frame:
13314 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13316 /* Mark all the scroll bars to be removed; we'll redeem
13317 the ones we want when we redisplay their windows. */
13318 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13319 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13321 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13322 redisplay_windows (FRAME_ROOT_WINDOW (f));
13324 /* The X error handler may have deleted that frame. */
13325 if (!FRAME_LIVE_P (f))
13326 continue;
13328 /* Any scroll bars which redisplay_windows should have
13329 nuked should now go away. */
13330 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13331 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13333 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13335 /* If fonts changed on visible frame, display again. */
13336 if (f->fonts_changed)
13338 adjust_frame_glyphs (f);
13339 f->fonts_changed = 0;
13340 goto retry_frame;
13343 /* See if we have to hscroll. */
13344 if (!f->already_hscrolled_p)
13346 f->already_hscrolled_p = 1;
13347 if (hscroll_windows (f->root_window))
13348 goto retry_frame;
13351 /* Prevent various kinds of signals during display
13352 update. stdio is not robust about handling
13353 signals, which can cause an apparent I/O
13354 error. */
13355 if (interrupt_input)
13356 unrequest_sigio ();
13357 STOP_POLLING;
13359 /* Mark windows on frame F to update. If we decide to
13360 update all frames but windows_or_buffers_changed is
13361 zero, we assume that only the windows that shows
13362 current buffer should be really updated. */
13363 set_window_update_flags
13364 (XWINDOW (f->root_window),
13365 (windows_or_buffers_changed ? NULL : current_buffer), 1);
13366 pending |= update_frame (f, 0, 0);
13367 f->cursor_type_changed = 0;
13368 f->updated_p = 1;
13373 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13375 if (!pending)
13377 /* Do the mark_window_display_accurate after all windows have
13378 been redisplayed because this call resets flags in buffers
13379 which are needed for proper redisplay. */
13380 FOR_EACH_FRAME (tail, frame)
13382 struct frame *f = XFRAME (frame);
13383 if (f->updated_p)
13385 mark_window_display_accurate (f->root_window, 1);
13386 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13387 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13392 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13394 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13395 struct frame *mini_frame;
13397 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13398 /* Use list_of_error, not Qerror, so that
13399 we catch only errors and don't run the debugger. */
13400 internal_condition_case_1 (redisplay_window_1, selected_window,
13401 list_of_error,
13402 redisplay_window_error);
13403 if (update_miniwindow_p)
13404 internal_condition_case_1 (redisplay_window_1, mini_window,
13405 list_of_error,
13406 redisplay_window_error);
13408 /* Compare desired and current matrices, perform output. */
13410 update:
13411 /* If fonts changed, display again. */
13412 if (sf->fonts_changed)
13413 goto retry;
13415 /* Prevent various kinds of signals during display update.
13416 stdio is not robust about handling signals,
13417 which can cause an apparent I/O error. */
13418 if (interrupt_input)
13419 unrequest_sigio ();
13420 STOP_POLLING;
13422 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13424 if (hscroll_windows (selected_window))
13425 goto retry;
13427 XWINDOW (selected_window)->must_be_updated_p = 1;
13428 pending = update_frame (sf, 0, 0);
13429 sf->cursor_type_changed = 0;
13432 /* We may have called echo_area_display at the top of this
13433 function. If the echo area is on another frame, that may
13434 have put text on a frame other than the selected one, so the
13435 above call to update_frame would not have caught it. Catch
13436 it here. */
13437 mini_window = FRAME_MINIBUF_WINDOW (sf);
13438 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13440 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13442 XWINDOW (mini_window)->must_be_updated_p = 1;
13443 pending |= update_frame (mini_frame, 0, 0);
13444 mini_frame->cursor_type_changed = 0;
13445 if (!pending && hscroll_windows (mini_window))
13446 goto retry;
13450 /* If display was paused because of pending input, make sure we do a
13451 thorough update the next time. */
13452 if (pending)
13454 /* Prevent the optimization at the beginning of
13455 redisplay_internal that tries a single-line update of the
13456 line containing the cursor in the selected window. */
13457 CHARPOS (this_line_start_pos) = 0;
13459 /* Let the overlay arrow be updated the next time. */
13460 update_overlay_arrows (0);
13462 /* If we pause after scrolling, some rows in the current
13463 matrices of some windows are not valid. */
13464 if (!WINDOW_FULL_WIDTH_P (w)
13465 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13466 update_mode_lines = 1;
13468 else
13470 if (!consider_all_windows_p)
13472 /* This has already been done above if
13473 consider_all_windows_p is set. */
13474 mark_window_display_accurate_1 (w, 1);
13476 /* Say overlay arrows are up to date. */
13477 update_overlay_arrows (1);
13479 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13480 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13483 update_mode_lines = 0;
13484 windows_or_buffers_changed = 0;
13487 /* Start SIGIO interrupts coming again. Having them off during the
13488 code above makes it less likely one will discard output, but not
13489 impossible, since there might be stuff in the system buffer here.
13490 But it is much hairier to try to do anything about that. */
13491 if (interrupt_input)
13492 request_sigio ();
13493 RESUME_POLLING;
13495 /* If a frame has become visible which was not before, redisplay
13496 again, so that we display it. Expose events for such a frame
13497 (which it gets when becoming visible) don't call the parts of
13498 redisplay constructing glyphs, so simply exposing a frame won't
13499 display anything in this case. So, we have to display these
13500 frames here explicitly. */
13501 if (!pending)
13503 int new_count = 0;
13505 FOR_EACH_FRAME (tail, frame)
13507 int this_is_visible = 0;
13509 if (XFRAME (frame)->visible)
13510 this_is_visible = 1;
13512 if (this_is_visible)
13513 new_count++;
13516 if (new_count != number_of_visible_frames)
13517 windows_or_buffers_changed++;
13520 /* Change frame size now if a change is pending. */
13521 do_pending_window_change (1);
13523 /* If we just did a pending size change, or have additional
13524 visible frames, or selected_window changed, redisplay again. */
13525 if ((windows_or_buffers_changed && !pending)
13526 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13527 goto retry;
13529 /* Clear the face and image caches.
13531 We used to do this only if consider_all_windows_p. But the cache
13532 needs to be cleared if a timer creates images in the current
13533 buffer (e.g. the test case in Bug#6230). */
13535 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13537 clear_face_cache (0);
13538 clear_face_cache_count = 0;
13541 #ifdef HAVE_WINDOW_SYSTEM
13542 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13544 clear_image_caches (Qnil);
13545 clear_image_cache_count = 0;
13547 #endif /* HAVE_WINDOW_SYSTEM */
13549 end_of_redisplay:
13550 unbind_to (count, Qnil);
13551 RESUME_POLLING;
13555 /* Redisplay, but leave alone any recent echo area message unless
13556 another message has been requested in its place.
13558 This is useful in situations where you need to redisplay but no
13559 user action has occurred, making it inappropriate for the message
13560 area to be cleared. See tracking_off and
13561 wait_reading_process_output for examples of these situations.
13563 FROM_WHERE is an integer saying from where this function was
13564 called. This is useful for debugging. */
13566 void
13567 redisplay_preserve_echo_area (int from_where)
13569 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13571 if (!NILP (echo_area_buffer[1]))
13573 /* We have a previously displayed message, but no current
13574 message. Redisplay the previous message. */
13575 display_last_displayed_message_p = 1;
13576 redisplay_internal ();
13577 display_last_displayed_message_p = 0;
13579 else
13580 redisplay_internal ();
13582 flush_frame (SELECTED_FRAME ());
13586 /* Function registered with record_unwind_protect in redisplay_internal. */
13588 static void
13589 unwind_redisplay (void)
13591 redisplaying_p = 0;
13595 /* Mark the display of leaf window W as accurate or inaccurate.
13596 If ACCURATE_P is non-zero mark display of W as accurate. If
13597 ACCURATE_P is zero, arrange for W to be redisplayed the next
13598 time redisplay_internal is called. */
13600 static void
13601 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13603 struct buffer *b = XBUFFER (w->contents);
13605 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13606 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13607 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13609 if (accurate_p)
13611 b->clip_changed = 0;
13612 b->prevent_redisplay_optimizations_p = 0;
13614 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13615 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13616 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13617 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13619 w->current_matrix->buffer = b;
13620 w->current_matrix->begv = BUF_BEGV (b);
13621 w->current_matrix->zv = BUF_ZV (b);
13623 w->last_cursor_vpos = w->cursor.vpos;
13624 w->last_cursor_off_p = w->cursor_off_p;
13626 if (w == XWINDOW (selected_window))
13627 w->last_point = BUF_PT (b);
13628 else
13629 w->last_point = marker_position (w->pointm);
13631 w->window_end_valid = 1;
13632 w->update_mode_line = 0;
13637 /* Mark the display of windows in the window tree rooted at WINDOW as
13638 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13639 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13640 be redisplayed the next time redisplay_internal is called. */
13642 void
13643 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13645 struct window *w;
13647 for (; !NILP (window); window = w->next)
13649 w = XWINDOW (window);
13650 if (WINDOWP (w->contents))
13651 mark_window_display_accurate (w->contents, accurate_p);
13652 else
13653 mark_window_display_accurate_1 (w, accurate_p);
13656 if (accurate_p)
13657 update_overlay_arrows (1);
13658 else
13659 /* Force a thorough redisplay the next time by setting
13660 last_arrow_position and last_arrow_string to t, which is
13661 unequal to any useful value of Voverlay_arrow_... */
13662 update_overlay_arrows (-1);
13666 /* Return value in display table DP (Lisp_Char_Table *) for character
13667 C. Since a display table doesn't have any parent, we don't have to
13668 follow parent. Do not call this function directly but use the
13669 macro DISP_CHAR_VECTOR. */
13671 Lisp_Object
13672 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13674 Lisp_Object val;
13676 if (ASCII_CHAR_P (c))
13678 val = dp->ascii;
13679 if (SUB_CHAR_TABLE_P (val))
13680 val = XSUB_CHAR_TABLE (val)->contents[c];
13682 else
13684 Lisp_Object table;
13686 XSETCHAR_TABLE (table, dp);
13687 val = char_table_ref (table, c);
13689 if (NILP (val))
13690 val = dp->defalt;
13691 return val;
13696 /***********************************************************************
13697 Window Redisplay
13698 ***********************************************************************/
13700 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13702 static void
13703 redisplay_windows (Lisp_Object window)
13705 while (!NILP (window))
13707 struct window *w = XWINDOW (window);
13709 if (WINDOWP (w->contents))
13710 redisplay_windows (w->contents);
13711 else if (BUFFERP (w->contents))
13713 displayed_buffer = XBUFFER (w->contents);
13714 /* Use list_of_error, not Qerror, so that
13715 we catch only errors and don't run the debugger. */
13716 internal_condition_case_1 (redisplay_window_0, window,
13717 list_of_error,
13718 redisplay_window_error);
13721 window = w->next;
13725 static Lisp_Object
13726 redisplay_window_error (Lisp_Object ignore)
13728 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13729 return Qnil;
13732 static Lisp_Object
13733 redisplay_window_0 (Lisp_Object window)
13735 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13736 redisplay_window (window, 0);
13737 return Qnil;
13740 static Lisp_Object
13741 redisplay_window_1 (Lisp_Object window)
13743 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13744 redisplay_window (window, 1);
13745 return Qnil;
13749 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13750 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13751 which positions recorded in ROW differ from current buffer
13752 positions.
13754 Return 0 if cursor is not on this row, 1 otherwise. */
13756 static int
13757 set_cursor_from_row (struct window *w, struct glyph_row *row,
13758 struct glyph_matrix *matrix,
13759 ptrdiff_t delta, ptrdiff_t delta_bytes,
13760 int dy, int dvpos)
13762 struct glyph *glyph = row->glyphs[TEXT_AREA];
13763 struct glyph *end = glyph + row->used[TEXT_AREA];
13764 struct glyph *cursor = NULL;
13765 /* The last known character position in row. */
13766 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13767 int x = row->x;
13768 ptrdiff_t pt_old = PT - delta;
13769 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13770 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13771 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13772 /* A glyph beyond the edge of TEXT_AREA which we should never
13773 touch. */
13774 struct glyph *glyphs_end = end;
13775 /* Non-zero means we've found a match for cursor position, but that
13776 glyph has the avoid_cursor_p flag set. */
13777 int match_with_avoid_cursor = 0;
13778 /* Non-zero means we've seen at least one glyph that came from a
13779 display string. */
13780 int string_seen = 0;
13781 /* Largest and smallest buffer positions seen so far during scan of
13782 glyph row. */
13783 ptrdiff_t bpos_max = pos_before;
13784 ptrdiff_t bpos_min = pos_after;
13785 /* Last buffer position covered by an overlay string with an integer
13786 `cursor' property. */
13787 ptrdiff_t bpos_covered = 0;
13788 /* Non-zero means the display string on which to display the cursor
13789 comes from a text property, not from an overlay. */
13790 int string_from_text_prop = 0;
13792 /* Don't even try doing anything if called for a mode-line or
13793 header-line row, since the rest of the code isn't prepared to
13794 deal with such calamities. */
13795 eassert (!row->mode_line_p);
13796 if (row->mode_line_p)
13797 return 0;
13799 /* Skip over glyphs not having an object at the start and the end of
13800 the row. These are special glyphs like truncation marks on
13801 terminal frames. */
13802 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13804 if (!row->reversed_p)
13806 while (glyph < end
13807 && INTEGERP (glyph->object)
13808 && glyph->charpos < 0)
13810 x += glyph->pixel_width;
13811 ++glyph;
13813 while (end > glyph
13814 && INTEGERP ((end - 1)->object)
13815 /* CHARPOS is zero for blanks and stretch glyphs
13816 inserted by extend_face_to_end_of_line. */
13817 && (end - 1)->charpos <= 0)
13818 --end;
13819 glyph_before = glyph - 1;
13820 glyph_after = end;
13822 else
13824 struct glyph *g;
13826 /* If the glyph row is reversed, we need to process it from back
13827 to front, so swap the edge pointers. */
13828 glyphs_end = end = glyph - 1;
13829 glyph += row->used[TEXT_AREA] - 1;
13831 while (glyph > end + 1
13832 && INTEGERP (glyph->object)
13833 && glyph->charpos < 0)
13835 --glyph;
13836 x -= glyph->pixel_width;
13838 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13839 --glyph;
13840 /* By default, in reversed rows we put the cursor on the
13841 rightmost (first in the reading order) glyph. */
13842 for (g = end + 1; g < glyph; g++)
13843 x += g->pixel_width;
13844 while (end < glyph
13845 && INTEGERP ((end + 1)->object)
13846 && (end + 1)->charpos <= 0)
13847 ++end;
13848 glyph_before = glyph + 1;
13849 glyph_after = end;
13852 else if (row->reversed_p)
13854 /* In R2L rows that don't display text, put the cursor on the
13855 rightmost glyph. Case in point: an empty last line that is
13856 part of an R2L paragraph. */
13857 cursor = end - 1;
13858 /* Avoid placing the cursor on the last glyph of the row, where
13859 on terminal frames we hold the vertical border between
13860 adjacent windows. */
13861 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13862 && !WINDOW_RIGHTMOST_P (w)
13863 && cursor == row->glyphs[LAST_AREA] - 1)
13864 cursor--;
13865 x = -1; /* will be computed below, at label compute_x */
13868 /* Step 1: Try to find the glyph whose character position
13869 corresponds to point. If that's not possible, find 2 glyphs
13870 whose character positions are the closest to point, one before
13871 point, the other after it. */
13872 if (!row->reversed_p)
13873 while (/* not marched to end of glyph row */
13874 glyph < end
13875 /* glyph was not inserted by redisplay for internal purposes */
13876 && !INTEGERP (glyph->object))
13878 if (BUFFERP (glyph->object))
13880 ptrdiff_t dpos = glyph->charpos - pt_old;
13882 if (glyph->charpos > bpos_max)
13883 bpos_max = glyph->charpos;
13884 if (glyph->charpos < bpos_min)
13885 bpos_min = glyph->charpos;
13886 if (!glyph->avoid_cursor_p)
13888 /* If we hit point, we've found the glyph on which to
13889 display the cursor. */
13890 if (dpos == 0)
13892 match_with_avoid_cursor = 0;
13893 break;
13895 /* See if we've found a better approximation to
13896 POS_BEFORE or to POS_AFTER. */
13897 if (0 > dpos && dpos > pos_before - pt_old)
13899 pos_before = glyph->charpos;
13900 glyph_before = glyph;
13902 else if (0 < dpos && dpos < pos_after - pt_old)
13904 pos_after = glyph->charpos;
13905 glyph_after = glyph;
13908 else if (dpos == 0)
13909 match_with_avoid_cursor = 1;
13911 else if (STRINGP (glyph->object))
13913 Lisp_Object chprop;
13914 ptrdiff_t glyph_pos = glyph->charpos;
13916 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13917 glyph->object);
13918 if (!NILP (chprop))
13920 /* If the string came from a `display' text property,
13921 look up the buffer position of that property and
13922 use that position to update bpos_max, as if we
13923 actually saw such a position in one of the row's
13924 glyphs. This helps with supporting integer values
13925 of `cursor' property on the display string in
13926 situations where most or all of the row's buffer
13927 text is completely covered by display properties,
13928 so that no glyph with valid buffer positions is
13929 ever seen in the row. */
13930 ptrdiff_t prop_pos =
13931 string_buffer_position_lim (glyph->object, pos_before,
13932 pos_after, 0);
13934 if (prop_pos >= pos_before)
13935 bpos_max = prop_pos - 1;
13937 if (INTEGERP (chprop))
13939 bpos_covered = bpos_max + XINT (chprop);
13940 /* If the `cursor' property covers buffer positions up
13941 to and including point, we should display cursor on
13942 this glyph. Note that, if a `cursor' property on one
13943 of the string's characters has an integer value, we
13944 will break out of the loop below _before_ we get to
13945 the position match above. IOW, integer values of
13946 the `cursor' property override the "exact match for
13947 point" strategy of positioning the cursor. */
13948 /* Implementation note: bpos_max == pt_old when, e.g.,
13949 we are in an empty line, where bpos_max is set to
13950 MATRIX_ROW_START_CHARPOS, see above. */
13951 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13953 cursor = glyph;
13954 break;
13958 string_seen = 1;
13960 x += glyph->pixel_width;
13961 ++glyph;
13963 else if (glyph > end) /* row is reversed */
13964 while (!INTEGERP (glyph->object))
13966 if (BUFFERP (glyph->object))
13968 ptrdiff_t dpos = glyph->charpos - pt_old;
13970 if (glyph->charpos > bpos_max)
13971 bpos_max = glyph->charpos;
13972 if (glyph->charpos < bpos_min)
13973 bpos_min = glyph->charpos;
13974 if (!glyph->avoid_cursor_p)
13976 if (dpos == 0)
13978 match_with_avoid_cursor = 0;
13979 break;
13981 if (0 > dpos && dpos > pos_before - pt_old)
13983 pos_before = glyph->charpos;
13984 glyph_before = glyph;
13986 else if (0 < dpos && dpos < pos_after - pt_old)
13988 pos_after = glyph->charpos;
13989 glyph_after = glyph;
13992 else if (dpos == 0)
13993 match_with_avoid_cursor = 1;
13995 else if (STRINGP (glyph->object))
13997 Lisp_Object chprop;
13998 ptrdiff_t glyph_pos = glyph->charpos;
14000 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14001 glyph->object);
14002 if (!NILP (chprop))
14004 ptrdiff_t prop_pos =
14005 string_buffer_position_lim (glyph->object, pos_before,
14006 pos_after, 0);
14008 if (prop_pos >= pos_before)
14009 bpos_max = prop_pos - 1;
14011 if (INTEGERP (chprop))
14013 bpos_covered = bpos_max + XINT (chprop);
14014 /* If the `cursor' property covers buffer positions up
14015 to and including point, we should display cursor on
14016 this glyph. */
14017 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14019 cursor = glyph;
14020 break;
14023 string_seen = 1;
14025 --glyph;
14026 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14028 x--; /* can't use any pixel_width */
14029 break;
14031 x -= glyph->pixel_width;
14034 /* Step 2: If we didn't find an exact match for point, we need to
14035 look for a proper place to put the cursor among glyphs between
14036 GLYPH_BEFORE and GLYPH_AFTER. */
14037 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14038 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14039 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14041 /* An empty line has a single glyph whose OBJECT is zero and
14042 whose CHARPOS is the position of a newline on that line.
14043 Note that on a TTY, there are more glyphs after that, which
14044 were produced by extend_face_to_end_of_line, but their
14045 CHARPOS is zero or negative. */
14046 int empty_line_p =
14047 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14048 && INTEGERP (glyph->object) && glyph->charpos > 0
14049 /* On a TTY, continued and truncated rows also have a glyph at
14050 their end whose OBJECT is zero and whose CHARPOS is
14051 positive (the continuation and truncation glyphs), but such
14052 rows are obviously not "empty". */
14053 && !(row->continued_p || row->truncated_on_right_p);
14055 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14057 ptrdiff_t ellipsis_pos;
14059 /* Scan back over the ellipsis glyphs. */
14060 if (!row->reversed_p)
14062 ellipsis_pos = (glyph - 1)->charpos;
14063 while (glyph > row->glyphs[TEXT_AREA]
14064 && (glyph - 1)->charpos == ellipsis_pos)
14065 glyph--, x -= glyph->pixel_width;
14066 /* That loop always goes one position too far, including
14067 the glyph before the ellipsis. So scan forward over
14068 that one. */
14069 x += glyph->pixel_width;
14070 glyph++;
14072 else /* row is reversed */
14074 ellipsis_pos = (glyph + 1)->charpos;
14075 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14076 && (glyph + 1)->charpos == ellipsis_pos)
14077 glyph++, x += glyph->pixel_width;
14078 x -= glyph->pixel_width;
14079 glyph--;
14082 else if (match_with_avoid_cursor)
14084 cursor = glyph_after;
14085 x = -1;
14087 else if (string_seen)
14089 int incr = row->reversed_p ? -1 : +1;
14091 /* Need to find the glyph that came out of a string which is
14092 present at point. That glyph is somewhere between
14093 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14094 positioned between POS_BEFORE and POS_AFTER in the
14095 buffer. */
14096 struct glyph *start, *stop;
14097 ptrdiff_t pos = pos_before;
14099 x = -1;
14101 /* If the row ends in a newline from a display string,
14102 reordering could have moved the glyphs belonging to the
14103 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14104 in this case we extend the search to the last glyph in
14105 the row that was not inserted by redisplay. */
14106 if (row->ends_in_newline_from_string_p)
14108 glyph_after = end;
14109 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14112 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14113 correspond to POS_BEFORE and POS_AFTER, respectively. We
14114 need START and STOP in the order that corresponds to the
14115 row's direction as given by its reversed_p flag. If the
14116 directionality of characters between POS_BEFORE and
14117 POS_AFTER is the opposite of the row's base direction,
14118 these characters will have been reordered for display,
14119 and we need to reverse START and STOP. */
14120 if (!row->reversed_p)
14122 start = min (glyph_before, glyph_after);
14123 stop = max (glyph_before, glyph_after);
14125 else
14127 start = max (glyph_before, glyph_after);
14128 stop = min (glyph_before, glyph_after);
14130 for (glyph = start + incr;
14131 row->reversed_p ? glyph > stop : glyph < stop; )
14134 /* Any glyphs that come from the buffer are here because
14135 of bidi reordering. Skip them, and only pay
14136 attention to glyphs that came from some string. */
14137 if (STRINGP (glyph->object))
14139 Lisp_Object str;
14140 ptrdiff_t tem;
14141 /* If the display property covers the newline, we
14142 need to search for it one position farther. */
14143 ptrdiff_t lim = pos_after
14144 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14146 string_from_text_prop = 0;
14147 str = glyph->object;
14148 tem = string_buffer_position_lim (str, pos, lim, 0);
14149 if (tem == 0 /* from overlay */
14150 || pos <= tem)
14152 /* If the string from which this glyph came is
14153 found in the buffer at point, or at position
14154 that is closer to point than pos_after, then
14155 we've found the glyph we've been looking for.
14156 If it comes from an overlay (tem == 0), and
14157 it has the `cursor' property on one of its
14158 glyphs, record that glyph as a candidate for
14159 displaying the cursor. (As in the
14160 unidirectional version, we will display the
14161 cursor on the last candidate we find.) */
14162 if (tem == 0
14163 || tem == pt_old
14164 || (tem - pt_old > 0 && tem < pos_after))
14166 /* The glyphs from this string could have
14167 been reordered. Find the one with the
14168 smallest string position. Or there could
14169 be a character in the string with the
14170 `cursor' property, which means display
14171 cursor on that character's glyph. */
14172 ptrdiff_t strpos = glyph->charpos;
14174 if (tem)
14176 cursor = glyph;
14177 string_from_text_prop = 1;
14179 for ( ;
14180 (row->reversed_p ? glyph > stop : glyph < stop)
14181 && EQ (glyph->object, str);
14182 glyph += incr)
14184 Lisp_Object cprop;
14185 ptrdiff_t gpos = glyph->charpos;
14187 cprop = Fget_char_property (make_number (gpos),
14188 Qcursor,
14189 glyph->object);
14190 if (!NILP (cprop))
14192 cursor = glyph;
14193 break;
14195 if (tem && glyph->charpos < strpos)
14197 strpos = glyph->charpos;
14198 cursor = glyph;
14202 if (tem == pt_old
14203 || (tem - pt_old > 0 && tem < pos_after))
14204 goto compute_x;
14206 if (tem)
14207 pos = tem + 1; /* don't find previous instances */
14209 /* This string is not what we want; skip all of the
14210 glyphs that came from it. */
14211 while ((row->reversed_p ? glyph > stop : glyph < stop)
14212 && EQ (glyph->object, str))
14213 glyph += incr;
14215 else
14216 glyph += incr;
14219 /* If we reached the end of the line, and END was from a string,
14220 the cursor is not on this line. */
14221 if (cursor == NULL
14222 && (row->reversed_p ? glyph <= end : glyph >= end)
14223 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14224 && STRINGP (end->object)
14225 && row->continued_p)
14226 return 0;
14228 /* A truncated row may not include PT among its character positions.
14229 Setting the cursor inside the scroll margin will trigger
14230 recalculation of hscroll in hscroll_window_tree. But if a
14231 display string covers point, defer to the string-handling
14232 code below to figure this out. */
14233 else if (row->truncated_on_left_p && pt_old < bpos_min)
14235 cursor = glyph_before;
14236 x = -1;
14238 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14239 /* Zero-width characters produce no glyphs. */
14240 || (!empty_line_p
14241 && (row->reversed_p
14242 ? glyph_after > glyphs_end
14243 : glyph_after < glyphs_end)))
14245 cursor = glyph_after;
14246 x = -1;
14250 compute_x:
14251 if (cursor != NULL)
14252 glyph = cursor;
14253 else if (glyph == glyphs_end
14254 && pos_before == pos_after
14255 && STRINGP ((row->reversed_p
14256 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14257 : row->glyphs[TEXT_AREA])->object))
14259 /* If all the glyphs of this row came from strings, put the
14260 cursor on the first glyph of the row. This avoids having the
14261 cursor outside of the text area in this very rare and hard
14262 use case. */
14263 glyph =
14264 row->reversed_p
14265 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14266 : row->glyphs[TEXT_AREA];
14268 if (x < 0)
14270 struct glyph *g;
14272 /* Need to compute x that corresponds to GLYPH. */
14273 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14275 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14276 emacs_abort ();
14277 x += g->pixel_width;
14281 /* ROW could be part of a continued line, which, under bidi
14282 reordering, might have other rows whose start and end charpos
14283 occlude point. Only set w->cursor if we found a better
14284 approximation to the cursor position than we have from previously
14285 examined candidate rows belonging to the same continued line. */
14286 if (/* we already have a candidate row */
14287 w->cursor.vpos >= 0
14288 /* that candidate is not the row we are processing */
14289 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14290 /* Make sure cursor.vpos specifies a row whose start and end
14291 charpos occlude point, and it is valid candidate for being a
14292 cursor-row. This is because some callers of this function
14293 leave cursor.vpos at the row where the cursor was displayed
14294 during the last redisplay cycle. */
14295 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14296 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14297 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14299 struct glyph *g1 =
14300 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14302 /* Don't consider glyphs that are outside TEXT_AREA. */
14303 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14304 return 0;
14305 /* Keep the candidate whose buffer position is the closest to
14306 point or has the `cursor' property. */
14307 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14308 w->cursor.hpos >= 0
14309 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14310 && ((BUFFERP (g1->object)
14311 && (g1->charpos == pt_old /* an exact match always wins */
14312 || (BUFFERP (glyph->object)
14313 && eabs (g1->charpos - pt_old)
14314 < eabs (glyph->charpos - pt_old))))
14315 /* previous candidate is a glyph from a string that has
14316 a non-nil `cursor' property */
14317 || (STRINGP (g1->object)
14318 && (!NILP (Fget_char_property (make_number (g1->charpos),
14319 Qcursor, g1->object))
14320 /* previous candidate is from the same display
14321 string as this one, and the display string
14322 came from a text property */
14323 || (EQ (g1->object, glyph->object)
14324 && string_from_text_prop)
14325 /* this candidate is from newline and its
14326 position is not an exact match */
14327 || (INTEGERP (glyph->object)
14328 && glyph->charpos != pt_old)))))
14329 return 0;
14330 /* If this candidate gives an exact match, use that. */
14331 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14332 /* If this candidate is a glyph created for the
14333 terminating newline of a line, and point is on that
14334 newline, it wins because it's an exact match. */
14335 || (!row->continued_p
14336 && INTEGERP (glyph->object)
14337 && glyph->charpos == 0
14338 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14339 /* Otherwise, keep the candidate that comes from a row
14340 spanning less buffer positions. This may win when one or
14341 both candidate positions are on glyphs that came from
14342 display strings, for which we cannot compare buffer
14343 positions. */
14344 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14345 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14346 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14347 return 0;
14349 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14350 w->cursor.x = x;
14351 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14352 w->cursor.y = row->y + dy;
14354 if (w == XWINDOW (selected_window))
14356 if (!row->continued_p
14357 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14358 && row->x == 0)
14360 this_line_buffer = XBUFFER (w->contents);
14362 CHARPOS (this_line_start_pos)
14363 = MATRIX_ROW_START_CHARPOS (row) + delta;
14364 BYTEPOS (this_line_start_pos)
14365 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14367 CHARPOS (this_line_end_pos)
14368 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14369 BYTEPOS (this_line_end_pos)
14370 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14372 this_line_y = w->cursor.y;
14373 this_line_pixel_height = row->height;
14374 this_line_vpos = w->cursor.vpos;
14375 this_line_start_x = row->x;
14377 else
14378 CHARPOS (this_line_start_pos) = 0;
14381 return 1;
14385 /* Run window scroll functions, if any, for WINDOW with new window
14386 start STARTP. Sets the window start of WINDOW to that position.
14388 We assume that the window's buffer is really current. */
14390 static struct text_pos
14391 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14393 struct window *w = XWINDOW (window);
14394 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14396 eassert (current_buffer == XBUFFER (w->contents));
14398 if (!NILP (Vwindow_scroll_functions))
14400 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14401 make_number (CHARPOS (startp)));
14402 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14403 /* In case the hook functions switch buffers. */
14404 set_buffer_internal (XBUFFER (w->contents));
14407 return startp;
14411 /* Make sure the line containing the cursor is fully visible.
14412 A value of 1 means there is nothing to be done.
14413 (Either the line is fully visible, or it cannot be made so,
14414 or we cannot tell.)
14416 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14417 is higher than window.
14419 A value of 0 means the caller should do scrolling
14420 as if point had gone off the screen. */
14422 static int
14423 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14425 struct glyph_matrix *matrix;
14426 struct glyph_row *row;
14427 int window_height;
14429 if (!make_cursor_line_fully_visible_p)
14430 return 1;
14432 /* It's not always possible to find the cursor, e.g, when a window
14433 is full of overlay strings. Don't do anything in that case. */
14434 if (w->cursor.vpos < 0)
14435 return 1;
14437 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14438 row = MATRIX_ROW (matrix, w->cursor.vpos);
14440 /* If the cursor row is not partially visible, there's nothing to do. */
14441 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14442 return 1;
14444 /* If the row the cursor is in is taller than the window's height,
14445 it's not clear what to do, so do nothing. */
14446 window_height = window_box_height (w);
14447 if (row->height >= window_height)
14449 if (!force_p || MINI_WINDOW_P (w)
14450 || w->vscroll || w->cursor.vpos == 0)
14451 return 1;
14453 return 0;
14457 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14458 non-zero means only WINDOW is redisplayed in redisplay_internal.
14459 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14460 in redisplay_window to bring a partially visible line into view in
14461 the case that only the cursor has moved.
14463 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14464 last screen line's vertical height extends past the end of the screen.
14466 Value is
14468 1 if scrolling succeeded
14470 0 if scrolling didn't find point.
14472 -1 if new fonts have been loaded so that we must interrupt
14473 redisplay, adjust glyph matrices, and try again. */
14475 enum
14477 SCROLLING_SUCCESS,
14478 SCROLLING_FAILED,
14479 SCROLLING_NEED_LARGER_MATRICES
14482 /* If scroll-conservatively is more than this, never recenter.
14484 If you change this, don't forget to update the doc string of
14485 `scroll-conservatively' and the Emacs manual. */
14486 #define SCROLL_LIMIT 100
14488 static int
14489 try_scrolling (Lisp_Object window, int just_this_one_p,
14490 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14491 int temp_scroll_step, int last_line_misfit)
14493 struct window *w = XWINDOW (window);
14494 struct frame *f = XFRAME (w->frame);
14495 struct text_pos pos, startp;
14496 struct it it;
14497 int this_scroll_margin, scroll_max, rc, height;
14498 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14499 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14500 Lisp_Object aggressive;
14501 /* We will never try scrolling more than this number of lines. */
14502 int scroll_limit = SCROLL_LIMIT;
14503 int frame_line_height = default_line_pixel_height (w);
14504 int window_total_lines
14505 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14507 #ifdef GLYPH_DEBUG
14508 debug_method_add (w, "try_scrolling");
14509 #endif
14511 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14513 /* Compute scroll margin height in pixels. We scroll when point is
14514 within this distance from the top or bottom of the window. */
14515 if (scroll_margin > 0)
14516 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14517 * frame_line_height;
14518 else
14519 this_scroll_margin = 0;
14521 /* Force arg_scroll_conservatively to have a reasonable value, to
14522 avoid scrolling too far away with slow move_it_* functions. Note
14523 that the user can supply scroll-conservatively equal to
14524 `most-positive-fixnum', which can be larger than INT_MAX. */
14525 if (arg_scroll_conservatively > scroll_limit)
14527 arg_scroll_conservatively = scroll_limit + 1;
14528 scroll_max = scroll_limit * frame_line_height;
14530 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14531 /* Compute how much we should try to scroll maximally to bring
14532 point into view. */
14533 scroll_max = (max (scroll_step,
14534 max (arg_scroll_conservatively, temp_scroll_step))
14535 * frame_line_height);
14536 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14537 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14538 /* We're trying to scroll because of aggressive scrolling but no
14539 scroll_step is set. Choose an arbitrary one. */
14540 scroll_max = 10 * frame_line_height;
14541 else
14542 scroll_max = 0;
14544 too_near_end:
14546 /* Decide whether to scroll down. */
14547 if (PT > CHARPOS (startp))
14549 int scroll_margin_y;
14551 /* Compute the pixel ypos of the scroll margin, then move IT to
14552 either that ypos or PT, whichever comes first. */
14553 start_display (&it, w, startp);
14554 scroll_margin_y = it.last_visible_y - this_scroll_margin
14555 - frame_line_height * extra_scroll_margin_lines;
14556 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14557 (MOVE_TO_POS | MOVE_TO_Y));
14559 if (PT > CHARPOS (it.current.pos))
14561 int y0 = line_bottom_y (&it);
14562 /* Compute how many pixels below window bottom to stop searching
14563 for PT. This avoids costly search for PT that is far away if
14564 the user limited scrolling by a small number of lines, but
14565 always finds PT if scroll_conservatively is set to a large
14566 number, such as most-positive-fixnum. */
14567 int slack = max (scroll_max, 10 * frame_line_height);
14568 int y_to_move = it.last_visible_y + slack;
14570 /* Compute the distance from the scroll margin to PT or to
14571 the scroll limit, whichever comes first. This should
14572 include the height of the cursor line, to make that line
14573 fully visible. */
14574 move_it_to (&it, PT, -1, y_to_move,
14575 -1, MOVE_TO_POS | MOVE_TO_Y);
14576 dy = line_bottom_y (&it) - y0;
14578 if (dy > scroll_max)
14579 return SCROLLING_FAILED;
14581 if (dy > 0)
14582 scroll_down_p = 1;
14586 if (scroll_down_p)
14588 /* Point is in or below the bottom scroll margin, so move the
14589 window start down. If scrolling conservatively, move it just
14590 enough down to make point visible. If scroll_step is set,
14591 move it down by scroll_step. */
14592 if (arg_scroll_conservatively)
14593 amount_to_scroll
14594 = min (max (dy, frame_line_height),
14595 frame_line_height * arg_scroll_conservatively);
14596 else if (scroll_step || temp_scroll_step)
14597 amount_to_scroll = scroll_max;
14598 else
14600 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14601 height = WINDOW_BOX_TEXT_HEIGHT (w);
14602 if (NUMBERP (aggressive))
14604 double float_amount = XFLOATINT (aggressive) * height;
14605 int aggressive_scroll = float_amount;
14606 if (aggressive_scroll == 0 && float_amount > 0)
14607 aggressive_scroll = 1;
14608 /* Don't let point enter the scroll margin near top of
14609 the window. This could happen if the value of
14610 scroll_up_aggressively is too large and there are
14611 non-zero margins, because scroll_up_aggressively
14612 means put point that fraction of window height
14613 _from_the_bottom_margin_. */
14614 if (aggressive_scroll + 2*this_scroll_margin > height)
14615 aggressive_scroll = height - 2*this_scroll_margin;
14616 amount_to_scroll = dy + aggressive_scroll;
14620 if (amount_to_scroll <= 0)
14621 return SCROLLING_FAILED;
14623 start_display (&it, w, startp);
14624 if (arg_scroll_conservatively <= scroll_limit)
14625 move_it_vertically (&it, amount_to_scroll);
14626 else
14628 /* Extra precision for users who set scroll-conservatively
14629 to a large number: make sure the amount we scroll
14630 the window start is never less than amount_to_scroll,
14631 which was computed as distance from window bottom to
14632 point. This matters when lines at window top and lines
14633 below window bottom have different height. */
14634 struct it it1;
14635 void *it1data = NULL;
14636 /* We use a temporary it1 because line_bottom_y can modify
14637 its argument, if it moves one line down; see there. */
14638 int start_y;
14640 SAVE_IT (it1, it, it1data);
14641 start_y = line_bottom_y (&it1);
14642 do {
14643 RESTORE_IT (&it, &it, it1data);
14644 move_it_by_lines (&it, 1);
14645 SAVE_IT (it1, it, it1data);
14646 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14649 /* If STARTP is unchanged, move it down another screen line. */
14650 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14651 move_it_by_lines (&it, 1);
14652 startp = it.current.pos;
14654 else
14656 struct text_pos scroll_margin_pos = startp;
14657 int y_offset = 0;
14659 /* See if point is inside the scroll margin at the top of the
14660 window. */
14661 if (this_scroll_margin)
14663 int y_start;
14665 start_display (&it, w, startp);
14666 y_start = it.current_y;
14667 move_it_vertically (&it, this_scroll_margin);
14668 scroll_margin_pos = it.current.pos;
14669 /* If we didn't move enough before hitting ZV, request
14670 additional amount of scroll, to move point out of the
14671 scroll margin. */
14672 if (IT_CHARPOS (it) == ZV
14673 && it.current_y - y_start < this_scroll_margin)
14674 y_offset = this_scroll_margin - (it.current_y - y_start);
14677 if (PT < CHARPOS (scroll_margin_pos))
14679 /* Point is in the scroll margin at the top of the window or
14680 above what is displayed in the window. */
14681 int y0, y_to_move;
14683 /* Compute the vertical distance from PT to the scroll
14684 margin position. Move as far as scroll_max allows, or
14685 one screenful, or 10 screen lines, whichever is largest.
14686 Give up if distance is greater than scroll_max or if we
14687 didn't reach the scroll margin position. */
14688 SET_TEXT_POS (pos, PT, PT_BYTE);
14689 start_display (&it, w, pos);
14690 y0 = it.current_y;
14691 y_to_move = max (it.last_visible_y,
14692 max (scroll_max, 10 * frame_line_height));
14693 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14694 y_to_move, -1,
14695 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14696 dy = it.current_y - y0;
14697 if (dy > scroll_max
14698 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14699 return SCROLLING_FAILED;
14701 /* Additional scroll for when ZV was too close to point. */
14702 dy += y_offset;
14704 /* Compute new window start. */
14705 start_display (&it, w, startp);
14707 if (arg_scroll_conservatively)
14708 amount_to_scroll = max (dy, frame_line_height *
14709 max (scroll_step, temp_scroll_step));
14710 else if (scroll_step || temp_scroll_step)
14711 amount_to_scroll = scroll_max;
14712 else
14714 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14715 height = WINDOW_BOX_TEXT_HEIGHT (w);
14716 if (NUMBERP (aggressive))
14718 double float_amount = XFLOATINT (aggressive) * height;
14719 int aggressive_scroll = float_amount;
14720 if (aggressive_scroll == 0 && float_amount > 0)
14721 aggressive_scroll = 1;
14722 /* Don't let point enter the scroll margin near
14723 bottom of the window, if the value of
14724 scroll_down_aggressively happens to be too
14725 large. */
14726 if (aggressive_scroll + 2*this_scroll_margin > height)
14727 aggressive_scroll = height - 2*this_scroll_margin;
14728 amount_to_scroll = dy + aggressive_scroll;
14732 if (amount_to_scroll <= 0)
14733 return SCROLLING_FAILED;
14735 move_it_vertically_backward (&it, amount_to_scroll);
14736 startp = it.current.pos;
14740 /* Run window scroll functions. */
14741 startp = run_window_scroll_functions (window, startp);
14743 /* Display the window. Give up if new fonts are loaded, or if point
14744 doesn't appear. */
14745 if (!try_window (window, startp, 0))
14746 rc = SCROLLING_NEED_LARGER_MATRICES;
14747 else if (w->cursor.vpos < 0)
14749 clear_glyph_matrix (w->desired_matrix);
14750 rc = SCROLLING_FAILED;
14752 else
14754 /* Maybe forget recorded base line for line number display. */
14755 if (!just_this_one_p
14756 || current_buffer->clip_changed
14757 || BEG_UNCHANGED < CHARPOS (startp))
14758 w->base_line_number = 0;
14760 /* If cursor ends up on a partially visible line,
14761 treat that as being off the bottom of the screen. */
14762 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14763 /* It's possible that the cursor is on the first line of the
14764 buffer, which is partially obscured due to a vscroll
14765 (Bug#7537). In that case, avoid looping forever . */
14766 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14768 clear_glyph_matrix (w->desired_matrix);
14769 ++extra_scroll_margin_lines;
14770 goto too_near_end;
14772 rc = SCROLLING_SUCCESS;
14775 return rc;
14779 /* Compute a suitable window start for window W if display of W starts
14780 on a continuation line. Value is non-zero if a new window start
14781 was computed.
14783 The new window start will be computed, based on W's width, starting
14784 from the start of the continued line. It is the start of the
14785 screen line with the minimum distance from the old start W->start. */
14787 static int
14788 compute_window_start_on_continuation_line (struct window *w)
14790 struct text_pos pos, start_pos;
14791 int window_start_changed_p = 0;
14793 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14795 /* If window start is on a continuation line... Window start may be
14796 < BEGV in case there's invisible text at the start of the
14797 buffer (M-x rmail, for example). */
14798 if (CHARPOS (start_pos) > BEGV
14799 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14801 struct it it;
14802 struct glyph_row *row;
14804 /* Handle the case that the window start is out of range. */
14805 if (CHARPOS (start_pos) < BEGV)
14806 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14807 else if (CHARPOS (start_pos) > ZV)
14808 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14810 /* Find the start of the continued line. This should be fast
14811 because find_newline is fast (newline cache). */
14812 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14813 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14814 row, DEFAULT_FACE_ID);
14815 reseat_at_previous_visible_line_start (&it);
14817 /* If the line start is "too far" away from the window start,
14818 say it takes too much time to compute a new window start. */
14819 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14820 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14822 int min_distance, distance;
14824 /* Move forward by display lines to find the new window
14825 start. If window width was enlarged, the new start can
14826 be expected to be > the old start. If window width was
14827 decreased, the new window start will be < the old start.
14828 So, we're looking for the display line start with the
14829 minimum distance from the old window start. */
14830 pos = it.current.pos;
14831 min_distance = INFINITY;
14832 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14833 distance < min_distance)
14835 min_distance = distance;
14836 pos = it.current.pos;
14837 if (it.line_wrap == WORD_WRAP)
14839 /* Under WORD_WRAP, move_it_by_lines is likely to
14840 overshoot and stop not at the first, but the
14841 second character from the left margin. So in
14842 that case, we need a more tight control on the X
14843 coordinate of the iterator than move_it_by_lines
14844 promises in its contract. The method is to first
14845 go to the last (rightmost) visible character of a
14846 line, then move to the leftmost character on the
14847 next line in a separate call. */
14848 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14849 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14850 move_it_to (&it, ZV, 0,
14851 it.current_y + it.max_ascent + it.max_descent, -1,
14852 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14854 else
14855 move_it_by_lines (&it, 1);
14858 /* Set the window start there. */
14859 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14860 window_start_changed_p = 1;
14864 return window_start_changed_p;
14868 /* Try cursor movement in case text has not changed in window WINDOW,
14869 with window start STARTP. Value is
14871 CURSOR_MOVEMENT_SUCCESS if successful
14873 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14875 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14876 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14877 we want to scroll as if scroll-step were set to 1. See the code.
14879 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14880 which case we have to abort this redisplay, and adjust matrices
14881 first. */
14883 enum
14885 CURSOR_MOVEMENT_SUCCESS,
14886 CURSOR_MOVEMENT_CANNOT_BE_USED,
14887 CURSOR_MOVEMENT_MUST_SCROLL,
14888 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14891 static int
14892 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14894 struct window *w = XWINDOW (window);
14895 struct frame *f = XFRAME (w->frame);
14896 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14898 #ifdef GLYPH_DEBUG
14899 if (inhibit_try_cursor_movement)
14900 return rc;
14901 #endif
14903 /* Previously, there was a check for Lisp integer in the
14904 if-statement below. Now, this field is converted to
14905 ptrdiff_t, thus zero means invalid position in a buffer. */
14906 eassert (w->last_point > 0);
14907 /* Likewise there was a check whether window_end_vpos is nil or larger
14908 than the window. Now window_end_vpos is int and so never nil, but
14909 let's leave eassert to check whether it fits in the window. */
14910 eassert (w->window_end_vpos < w->current_matrix->nrows);
14912 /* Handle case where text has not changed, only point, and it has
14913 not moved off the frame. */
14914 if (/* Point may be in this window. */
14915 PT >= CHARPOS (startp)
14916 /* Selective display hasn't changed. */
14917 && !current_buffer->clip_changed
14918 /* Function force-mode-line-update is used to force a thorough
14919 redisplay. It sets either windows_or_buffers_changed or
14920 update_mode_lines. So don't take a shortcut here for these
14921 cases. */
14922 && !update_mode_lines
14923 && !windows_or_buffers_changed
14924 && !f->cursor_type_changed
14925 && NILP (Vshow_trailing_whitespace)
14926 /* This code is not used for mini-buffer for the sake of the case
14927 of redisplaying to replace an echo area message; since in
14928 that case the mini-buffer contents per se are usually
14929 unchanged. This code is of no real use in the mini-buffer
14930 since the handling of this_line_start_pos, etc., in redisplay
14931 handles the same cases. */
14932 && !EQ (window, minibuf_window)
14933 && (FRAME_WINDOW_P (f)
14934 || !overlay_arrow_in_current_buffer_p ()))
14936 int this_scroll_margin, top_scroll_margin;
14937 struct glyph_row *row = NULL;
14938 int frame_line_height = default_line_pixel_height (w);
14939 int window_total_lines
14940 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14942 #ifdef GLYPH_DEBUG
14943 debug_method_add (w, "cursor movement");
14944 #endif
14946 /* Scroll if point within this distance from the top or bottom
14947 of the window. This is a pixel value. */
14948 if (scroll_margin > 0)
14950 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
14951 this_scroll_margin *= frame_line_height;
14953 else
14954 this_scroll_margin = 0;
14956 top_scroll_margin = this_scroll_margin;
14957 if (WINDOW_WANTS_HEADER_LINE_P (w))
14958 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14960 /* Start with the row the cursor was displayed during the last
14961 not paused redisplay. Give up if that row is not valid. */
14962 if (w->last_cursor_vpos < 0
14963 || w->last_cursor_vpos >= w->current_matrix->nrows)
14964 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14965 else
14967 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
14968 if (row->mode_line_p)
14969 ++row;
14970 if (!row->enabled_p)
14971 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14974 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14976 int scroll_p = 0, must_scroll = 0;
14977 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14979 if (PT > w->last_point)
14981 /* Point has moved forward. */
14982 while (MATRIX_ROW_END_CHARPOS (row) < PT
14983 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14985 eassert (row->enabled_p);
14986 ++row;
14989 /* If the end position of a row equals the start
14990 position of the next row, and PT is at that position,
14991 we would rather display cursor in the next line. */
14992 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14993 && MATRIX_ROW_END_CHARPOS (row) == PT
14994 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
14995 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14996 && !cursor_row_p (row))
14997 ++row;
14999 /* If within the scroll margin, scroll. Note that
15000 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15001 the next line would be drawn, and that
15002 this_scroll_margin can be zero. */
15003 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15004 || PT > MATRIX_ROW_END_CHARPOS (row)
15005 /* Line is completely visible last line in window
15006 and PT is to be set in the next line. */
15007 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15008 && PT == MATRIX_ROW_END_CHARPOS (row)
15009 && !row->ends_at_zv_p
15010 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15011 scroll_p = 1;
15013 else if (PT < w->last_point)
15015 /* Cursor has to be moved backward. Note that PT >=
15016 CHARPOS (startp) because of the outer if-statement. */
15017 while (!row->mode_line_p
15018 && (MATRIX_ROW_START_CHARPOS (row) > PT
15019 || (MATRIX_ROW_START_CHARPOS (row) == PT
15020 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15021 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15022 row > w->current_matrix->rows
15023 && (row-1)->ends_in_newline_from_string_p))))
15024 && (row->y > top_scroll_margin
15025 || CHARPOS (startp) == BEGV))
15027 eassert (row->enabled_p);
15028 --row;
15031 /* Consider the following case: Window starts at BEGV,
15032 there is invisible, intangible text at BEGV, so that
15033 display starts at some point START > BEGV. It can
15034 happen that we are called with PT somewhere between
15035 BEGV and START. Try to handle that case. */
15036 if (row < w->current_matrix->rows
15037 || row->mode_line_p)
15039 row = w->current_matrix->rows;
15040 if (row->mode_line_p)
15041 ++row;
15044 /* Due to newlines in overlay strings, we may have to
15045 skip forward over overlay strings. */
15046 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15047 && MATRIX_ROW_END_CHARPOS (row) == PT
15048 && !cursor_row_p (row))
15049 ++row;
15051 /* If within the scroll margin, scroll. */
15052 if (row->y < top_scroll_margin
15053 && CHARPOS (startp) != BEGV)
15054 scroll_p = 1;
15056 else
15058 /* Cursor did not move. So don't scroll even if cursor line
15059 is partially visible, as it was so before. */
15060 rc = CURSOR_MOVEMENT_SUCCESS;
15063 if (PT < MATRIX_ROW_START_CHARPOS (row)
15064 || PT > MATRIX_ROW_END_CHARPOS (row))
15066 /* if PT is not in the glyph row, give up. */
15067 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15068 must_scroll = 1;
15070 else if (rc != CURSOR_MOVEMENT_SUCCESS
15071 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15073 struct glyph_row *row1;
15075 /* If rows are bidi-reordered and point moved, back up
15076 until we find a row that does not belong to a
15077 continuation line. This is because we must consider
15078 all rows of a continued line as candidates for the
15079 new cursor positioning, since row start and end
15080 positions change non-linearly with vertical position
15081 in such rows. */
15082 /* FIXME: Revisit this when glyph ``spilling'' in
15083 continuation lines' rows is implemented for
15084 bidi-reordered rows. */
15085 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15086 MATRIX_ROW_CONTINUATION_LINE_P (row);
15087 --row)
15089 /* If we hit the beginning of the displayed portion
15090 without finding the first row of a continued
15091 line, give up. */
15092 if (row <= row1)
15094 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15095 break;
15097 eassert (row->enabled_p);
15100 if (must_scroll)
15102 else if (rc != CURSOR_MOVEMENT_SUCCESS
15103 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15104 /* Make sure this isn't a header line by any chance, since
15105 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15106 && !row->mode_line_p
15107 && make_cursor_line_fully_visible_p)
15109 if (PT == MATRIX_ROW_END_CHARPOS (row)
15110 && !row->ends_at_zv_p
15111 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15112 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15113 else if (row->height > window_box_height (w))
15115 /* If we end up in a partially visible line, let's
15116 make it fully visible, except when it's taller
15117 than the window, in which case we can't do much
15118 about it. */
15119 *scroll_step = 1;
15120 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15122 else
15124 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15125 if (!cursor_row_fully_visible_p (w, 0, 1))
15126 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15127 else
15128 rc = CURSOR_MOVEMENT_SUCCESS;
15131 else if (scroll_p)
15132 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15133 else if (rc != CURSOR_MOVEMENT_SUCCESS
15134 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15136 /* With bidi-reordered rows, there could be more than
15137 one candidate row whose start and end positions
15138 occlude point. We need to let set_cursor_from_row
15139 find the best candidate. */
15140 /* FIXME: Revisit this when glyph ``spilling'' in
15141 continuation lines' rows is implemented for
15142 bidi-reordered rows. */
15143 int rv = 0;
15147 int at_zv_p = 0, exact_match_p = 0;
15149 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15150 && PT <= MATRIX_ROW_END_CHARPOS (row)
15151 && cursor_row_p (row))
15152 rv |= set_cursor_from_row (w, row, w->current_matrix,
15153 0, 0, 0, 0);
15154 /* As soon as we've found the exact match for point,
15155 or the first suitable row whose ends_at_zv_p flag
15156 is set, we are done. */
15157 at_zv_p =
15158 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15159 if (rv && !at_zv_p
15160 && w->cursor.hpos >= 0
15161 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15162 w->cursor.vpos))
15164 struct glyph_row *candidate =
15165 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15166 struct glyph *g =
15167 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15168 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15170 exact_match_p =
15171 (BUFFERP (g->object) && g->charpos == PT)
15172 || (INTEGERP (g->object)
15173 && (g->charpos == PT
15174 || (g->charpos == 0 && endpos - 1 == PT)));
15176 if (rv && (at_zv_p || exact_match_p))
15178 rc = CURSOR_MOVEMENT_SUCCESS;
15179 break;
15181 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15182 break;
15183 ++row;
15185 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15186 || row->continued_p)
15187 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15188 || (MATRIX_ROW_START_CHARPOS (row) == PT
15189 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15190 /* If we didn't find any candidate rows, or exited the
15191 loop before all the candidates were examined, signal
15192 to the caller that this method failed. */
15193 if (rc != CURSOR_MOVEMENT_SUCCESS
15194 && !(rv
15195 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15196 && !row->continued_p))
15197 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15198 else if (rv)
15199 rc = CURSOR_MOVEMENT_SUCCESS;
15201 else
15205 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15207 rc = CURSOR_MOVEMENT_SUCCESS;
15208 break;
15210 ++row;
15212 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15213 && MATRIX_ROW_START_CHARPOS (row) == PT
15214 && cursor_row_p (row));
15219 return rc;
15222 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15223 static
15224 #endif
15225 void
15226 set_vertical_scroll_bar (struct window *w)
15228 ptrdiff_t start, end, whole;
15230 /* Calculate the start and end positions for the current window.
15231 At some point, it would be nice to choose between scrollbars
15232 which reflect the whole buffer size, with special markers
15233 indicating narrowing, and scrollbars which reflect only the
15234 visible region.
15236 Note that mini-buffers sometimes aren't displaying any text. */
15237 if (!MINI_WINDOW_P (w)
15238 || (w == XWINDOW (minibuf_window)
15239 && NILP (echo_area_buffer[0])))
15241 struct buffer *buf = XBUFFER (w->contents);
15242 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15243 start = marker_position (w->start) - BUF_BEGV (buf);
15244 /* I don't think this is guaranteed to be right. For the
15245 moment, we'll pretend it is. */
15246 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15248 if (end < start)
15249 end = start;
15250 if (whole < (end - start))
15251 whole = end - start;
15253 else
15254 start = end = whole = 0;
15256 /* Indicate what this scroll bar ought to be displaying now. */
15257 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15258 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15259 (w, end - start, whole, start);
15263 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15264 selected_window is redisplayed.
15266 We can return without actually redisplaying the window if fonts has been
15267 changed on window's frame. In that case, redisplay_internal will retry. */
15269 static void
15270 redisplay_window (Lisp_Object window, int just_this_one_p)
15272 struct window *w = XWINDOW (window);
15273 struct frame *f = XFRAME (w->frame);
15274 struct buffer *buffer = XBUFFER (w->contents);
15275 struct buffer *old = current_buffer;
15276 struct text_pos lpoint, opoint, startp;
15277 int update_mode_line;
15278 int tem;
15279 struct it it;
15280 /* Record it now because it's overwritten. */
15281 int current_matrix_up_to_date_p = 0;
15282 int used_current_matrix_p = 0;
15283 /* This is less strict than current_matrix_up_to_date_p.
15284 It indicates that the buffer contents and narrowing are unchanged. */
15285 int buffer_unchanged_p = 0;
15286 int temp_scroll_step = 0;
15287 ptrdiff_t count = SPECPDL_INDEX ();
15288 int rc;
15289 int centering_position = -1;
15290 int last_line_misfit = 0;
15291 ptrdiff_t beg_unchanged, end_unchanged;
15292 int frame_line_height;
15294 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15295 opoint = lpoint;
15297 #ifdef GLYPH_DEBUG
15298 *w->desired_matrix->method = 0;
15299 #endif
15301 /* Make sure that both W's markers are valid. */
15302 eassert (XMARKER (w->start)->buffer == buffer);
15303 eassert (XMARKER (w->pointm)->buffer == buffer);
15305 restart:
15306 reconsider_clip_changes (w);
15307 frame_line_height = default_line_pixel_height (w);
15309 /* Has the mode line to be updated? */
15310 update_mode_line = (w->update_mode_line
15311 || update_mode_lines
15312 || buffer->clip_changed
15313 || buffer->prevent_redisplay_optimizations_p);
15315 if (MINI_WINDOW_P (w))
15317 if (w == XWINDOW (echo_area_window)
15318 && !NILP (echo_area_buffer[0]))
15320 if (update_mode_line)
15321 /* We may have to update a tty frame's menu bar or a
15322 tool-bar. Example `M-x C-h C-h C-g'. */
15323 goto finish_menu_bars;
15324 else
15325 /* We've already displayed the echo area glyphs in this window. */
15326 goto finish_scroll_bars;
15328 else if ((w != XWINDOW (minibuf_window)
15329 || minibuf_level == 0)
15330 /* When buffer is nonempty, redisplay window normally. */
15331 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15332 /* Quail displays non-mini buffers in minibuffer window.
15333 In that case, redisplay the window normally. */
15334 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15336 /* W is a mini-buffer window, but it's not active, so clear
15337 it. */
15338 int yb = window_text_bottom_y (w);
15339 struct glyph_row *row;
15340 int y;
15342 for (y = 0, row = w->desired_matrix->rows;
15343 y < yb;
15344 y += row->height, ++row)
15345 blank_row (w, row, y);
15346 goto finish_scroll_bars;
15349 clear_glyph_matrix (w->desired_matrix);
15352 /* Otherwise set up data on this window; select its buffer and point
15353 value. */
15354 /* Really select the buffer, for the sake of buffer-local
15355 variables. */
15356 set_buffer_internal_1 (XBUFFER (w->contents));
15358 current_matrix_up_to_date_p
15359 = (w->window_end_valid
15360 && !current_buffer->clip_changed
15361 && !current_buffer->prevent_redisplay_optimizations_p
15362 && !window_outdated (w));
15364 /* Run the window-bottom-change-functions
15365 if it is possible that the text on the screen has changed
15366 (either due to modification of the text, or any other reason). */
15367 if (!current_matrix_up_to_date_p
15368 && !NILP (Vwindow_text_change_functions))
15370 safe_run_hooks (Qwindow_text_change_functions);
15371 goto restart;
15374 beg_unchanged = BEG_UNCHANGED;
15375 end_unchanged = END_UNCHANGED;
15377 SET_TEXT_POS (opoint, PT, PT_BYTE);
15379 specbind (Qinhibit_point_motion_hooks, Qt);
15381 buffer_unchanged_p
15382 = (w->window_end_valid
15383 && !current_buffer->clip_changed
15384 && !window_outdated (w));
15386 /* When windows_or_buffers_changed is non-zero, we can't rely
15387 on the window end being valid, so set it to zero there. */
15388 if (windows_or_buffers_changed)
15390 /* If window starts on a continuation line, maybe adjust the
15391 window start in case the window's width changed. */
15392 if (XMARKER (w->start)->buffer == current_buffer)
15393 compute_window_start_on_continuation_line (w);
15395 w->window_end_valid = 0;
15396 /* If so, we also can't rely on current matrix
15397 and should not fool try_cursor_movement below. */
15398 current_matrix_up_to_date_p = 0;
15401 /* Some sanity checks. */
15402 CHECK_WINDOW_END (w);
15403 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15404 emacs_abort ();
15405 if (BYTEPOS (opoint) < CHARPOS (opoint))
15406 emacs_abort ();
15408 if (mode_line_update_needed (w))
15409 update_mode_line = 1;
15411 /* Point refers normally to the selected window. For any other
15412 window, set up appropriate value. */
15413 if (!EQ (window, selected_window))
15415 ptrdiff_t new_pt = marker_position (w->pointm);
15416 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15417 if (new_pt < BEGV)
15419 new_pt = BEGV;
15420 new_pt_byte = BEGV_BYTE;
15421 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15423 else if (new_pt > (ZV - 1))
15425 new_pt = ZV;
15426 new_pt_byte = ZV_BYTE;
15427 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15430 /* We don't use SET_PT so that the point-motion hooks don't run. */
15431 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15434 /* If any of the character widths specified in the display table
15435 have changed, invalidate the width run cache. It's true that
15436 this may be a bit late to catch such changes, but the rest of
15437 redisplay goes (non-fatally) haywire when the display table is
15438 changed, so why should we worry about doing any better? */
15439 if (current_buffer->width_run_cache)
15441 struct Lisp_Char_Table *disptab = buffer_display_table ();
15443 if (! disptab_matches_widthtab
15444 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15446 invalidate_region_cache (current_buffer,
15447 current_buffer->width_run_cache,
15448 BEG, Z);
15449 recompute_width_table (current_buffer, disptab);
15453 /* If window-start is screwed up, choose a new one. */
15454 if (XMARKER (w->start)->buffer != current_buffer)
15455 goto recenter;
15457 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15459 /* If someone specified a new starting point but did not insist,
15460 check whether it can be used. */
15461 if (w->optional_new_start
15462 && CHARPOS (startp) >= BEGV
15463 && CHARPOS (startp) <= ZV)
15465 w->optional_new_start = 0;
15466 start_display (&it, w, startp);
15467 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15468 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15469 if (IT_CHARPOS (it) == PT)
15470 w->force_start = 1;
15471 /* IT may overshoot PT if text at PT is invisible. */
15472 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15473 w->force_start = 1;
15476 force_start:
15478 /* Handle case where place to start displaying has been specified,
15479 unless the specified location is outside the accessible range. */
15480 if (w->force_start || window_frozen_p (w))
15482 /* We set this later on if we have to adjust point. */
15483 int new_vpos = -1;
15485 w->force_start = 0;
15486 w->vscroll = 0;
15487 w->window_end_valid = 0;
15489 /* Forget any recorded base line for line number display. */
15490 if (!buffer_unchanged_p)
15491 w->base_line_number = 0;
15493 /* Redisplay the mode line. Select the buffer properly for that.
15494 Also, run the hook window-scroll-functions
15495 because we have scrolled. */
15496 /* Note, we do this after clearing force_start because
15497 if there's an error, it is better to forget about force_start
15498 than to get into an infinite loop calling the hook functions
15499 and having them get more errors. */
15500 if (!update_mode_line
15501 || ! NILP (Vwindow_scroll_functions))
15503 update_mode_line = 1;
15504 w->update_mode_line = 1;
15505 startp = run_window_scroll_functions (window, startp);
15508 if (CHARPOS (startp) < BEGV)
15509 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15510 else if (CHARPOS (startp) > ZV)
15511 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15513 /* Redisplay, then check if cursor has been set during the
15514 redisplay. Give up if new fonts were loaded. */
15515 /* We used to issue a CHECK_MARGINS argument to try_window here,
15516 but this causes scrolling to fail when point begins inside
15517 the scroll margin (bug#148) -- cyd */
15518 if (!try_window (window, startp, 0))
15520 w->force_start = 1;
15521 clear_glyph_matrix (w->desired_matrix);
15522 goto need_larger_matrices;
15525 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15527 /* If point does not appear, try to move point so it does
15528 appear. The desired matrix has been built above, so we
15529 can use it here. */
15530 new_vpos = window_box_height (w) / 2;
15533 if (!cursor_row_fully_visible_p (w, 0, 0))
15535 /* Point does appear, but on a line partly visible at end of window.
15536 Move it back to a fully-visible line. */
15537 new_vpos = window_box_height (w);
15539 else if (w->cursor.vpos >= 0)
15541 /* Some people insist on not letting point enter the scroll
15542 margin, even though this part handles windows that didn't
15543 scroll at all. */
15544 int window_total_lines
15545 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15546 int margin = min (scroll_margin, window_total_lines / 4);
15547 int pixel_margin = margin * frame_line_height;
15548 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15550 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15551 below, which finds the row to move point to, advances by
15552 the Y coordinate of the _next_ row, see the definition of
15553 MATRIX_ROW_BOTTOM_Y. */
15554 if (w->cursor.vpos < margin + header_line)
15556 w->cursor.vpos = -1;
15557 clear_glyph_matrix (w->desired_matrix);
15558 goto try_to_scroll;
15560 else
15562 int window_height = window_box_height (w);
15564 if (header_line)
15565 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15566 if (w->cursor.y >= window_height - pixel_margin)
15568 w->cursor.vpos = -1;
15569 clear_glyph_matrix (w->desired_matrix);
15570 goto try_to_scroll;
15575 /* If we need to move point for either of the above reasons,
15576 now actually do it. */
15577 if (new_vpos >= 0)
15579 struct glyph_row *row;
15581 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15582 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15583 ++row;
15585 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15586 MATRIX_ROW_START_BYTEPOS (row));
15588 if (w != XWINDOW (selected_window))
15589 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15590 else if (current_buffer == old)
15591 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15593 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15595 /* If we are highlighting the region, then we just changed
15596 the region, so redisplay to show it. */
15597 /* FIXME: We need to (re)run pre-redisplay-function! */
15598 /* if (markpos_of_region () >= 0)
15600 clear_glyph_matrix (w->desired_matrix);
15601 if (!try_window (window, startp, 0))
15602 goto need_larger_matrices;
15607 #ifdef GLYPH_DEBUG
15608 debug_method_add (w, "forced window start");
15609 #endif
15610 goto done;
15613 /* Handle case where text has not changed, only point, and it has
15614 not moved off the frame, and we are not retrying after hscroll.
15615 (current_matrix_up_to_date_p is nonzero when retrying.) */
15616 if (current_matrix_up_to_date_p
15617 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15618 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15620 switch (rc)
15622 case CURSOR_MOVEMENT_SUCCESS:
15623 used_current_matrix_p = 1;
15624 goto done;
15626 case CURSOR_MOVEMENT_MUST_SCROLL:
15627 goto try_to_scroll;
15629 default:
15630 emacs_abort ();
15633 /* If current starting point was originally the beginning of a line
15634 but no longer is, find a new starting point. */
15635 else if (w->start_at_line_beg
15636 && !(CHARPOS (startp) <= BEGV
15637 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15639 #ifdef GLYPH_DEBUG
15640 debug_method_add (w, "recenter 1");
15641 #endif
15642 goto recenter;
15645 /* Try scrolling with try_window_id. Value is > 0 if update has
15646 been done, it is -1 if we know that the same window start will
15647 not work. It is 0 if unsuccessful for some other reason. */
15648 else if ((tem = try_window_id (w)) != 0)
15650 #ifdef GLYPH_DEBUG
15651 debug_method_add (w, "try_window_id %d", tem);
15652 #endif
15654 if (f->fonts_changed)
15655 goto need_larger_matrices;
15656 if (tem > 0)
15657 goto done;
15659 /* Otherwise try_window_id has returned -1 which means that we
15660 don't want the alternative below this comment to execute. */
15662 else if (CHARPOS (startp) >= BEGV
15663 && CHARPOS (startp) <= ZV
15664 && PT >= CHARPOS (startp)
15665 && (CHARPOS (startp) < ZV
15666 /* Avoid starting at end of buffer. */
15667 || CHARPOS (startp) == BEGV
15668 || !window_outdated (w)))
15670 int d1, d2, d3, d4, d5, d6;
15672 /* If first window line is a continuation line, and window start
15673 is inside the modified region, but the first change is before
15674 current window start, we must select a new window start.
15676 However, if this is the result of a down-mouse event (e.g. by
15677 extending the mouse-drag-overlay), we don't want to select a
15678 new window start, since that would change the position under
15679 the mouse, resulting in an unwanted mouse-movement rather
15680 than a simple mouse-click. */
15681 if (!w->start_at_line_beg
15682 && NILP (do_mouse_tracking)
15683 && CHARPOS (startp) > BEGV
15684 && CHARPOS (startp) > BEG + beg_unchanged
15685 && CHARPOS (startp) <= Z - end_unchanged
15686 /* Even if w->start_at_line_beg is nil, a new window may
15687 start at a line_beg, since that's how set_buffer_window
15688 sets it. So, we need to check the return value of
15689 compute_window_start_on_continuation_line. (See also
15690 bug#197). */
15691 && XMARKER (w->start)->buffer == current_buffer
15692 && compute_window_start_on_continuation_line (w)
15693 /* It doesn't make sense to force the window start like we
15694 do at label force_start if it is already known that point
15695 will not be visible in the resulting window, because
15696 doing so will move point from its correct position
15697 instead of scrolling the window to bring point into view.
15698 See bug#9324. */
15699 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15701 w->force_start = 1;
15702 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15703 goto force_start;
15706 #ifdef GLYPH_DEBUG
15707 debug_method_add (w, "same window start");
15708 #endif
15710 /* Try to redisplay starting at same place as before.
15711 If point has not moved off frame, accept the results. */
15712 if (!current_matrix_up_to_date_p
15713 /* Don't use try_window_reusing_current_matrix in this case
15714 because a window scroll function can have changed the
15715 buffer. */
15716 || !NILP (Vwindow_scroll_functions)
15717 || MINI_WINDOW_P (w)
15718 || !(used_current_matrix_p
15719 = try_window_reusing_current_matrix (w)))
15721 IF_DEBUG (debug_method_add (w, "1"));
15722 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15723 /* -1 means we need to scroll.
15724 0 means we need new matrices, but fonts_changed
15725 is set in that case, so we will detect it below. */
15726 goto try_to_scroll;
15729 if (f->fonts_changed)
15730 goto need_larger_matrices;
15732 if (w->cursor.vpos >= 0)
15734 if (!just_this_one_p
15735 || current_buffer->clip_changed
15736 || BEG_UNCHANGED < CHARPOS (startp))
15737 /* Forget any recorded base line for line number display. */
15738 w->base_line_number = 0;
15740 if (!cursor_row_fully_visible_p (w, 1, 0))
15742 clear_glyph_matrix (w->desired_matrix);
15743 last_line_misfit = 1;
15745 /* Drop through and scroll. */
15746 else
15747 goto done;
15749 else
15750 clear_glyph_matrix (w->desired_matrix);
15753 try_to_scroll:
15755 /* Redisplay the mode line. Select the buffer properly for that. */
15756 if (!update_mode_line)
15758 update_mode_line = 1;
15759 w->update_mode_line = 1;
15762 /* Try to scroll by specified few lines. */
15763 if ((scroll_conservatively
15764 || emacs_scroll_step
15765 || temp_scroll_step
15766 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15767 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15768 && CHARPOS (startp) >= BEGV
15769 && CHARPOS (startp) <= ZV)
15771 /* The function returns -1 if new fonts were loaded, 1 if
15772 successful, 0 if not successful. */
15773 int ss = try_scrolling (window, just_this_one_p,
15774 scroll_conservatively,
15775 emacs_scroll_step,
15776 temp_scroll_step, last_line_misfit);
15777 switch (ss)
15779 case SCROLLING_SUCCESS:
15780 goto done;
15782 case SCROLLING_NEED_LARGER_MATRICES:
15783 goto need_larger_matrices;
15785 case SCROLLING_FAILED:
15786 break;
15788 default:
15789 emacs_abort ();
15793 /* Finally, just choose a place to start which positions point
15794 according to user preferences. */
15796 recenter:
15798 #ifdef GLYPH_DEBUG
15799 debug_method_add (w, "recenter");
15800 #endif
15802 /* Forget any previously recorded base line for line number display. */
15803 if (!buffer_unchanged_p)
15804 w->base_line_number = 0;
15806 /* Determine the window start relative to point. */
15807 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15808 it.current_y = it.last_visible_y;
15809 if (centering_position < 0)
15811 int window_total_lines
15812 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15813 int margin =
15814 scroll_margin > 0
15815 ? min (scroll_margin, window_total_lines / 4)
15816 : 0;
15817 ptrdiff_t margin_pos = CHARPOS (startp);
15818 Lisp_Object aggressive;
15819 int scrolling_up;
15821 /* If there is a scroll margin at the top of the window, find
15822 its character position. */
15823 if (margin
15824 /* Cannot call start_display if startp is not in the
15825 accessible region of the buffer. This can happen when we
15826 have just switched to a different buffer and/or changed
15827 its restriction. In that case, startp is initialized to
15828 the character position 1 (BEGV) because we did not yet
15829 have chance to display the buffer even once. */
15830 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15832 struct it it1;
15833 void *it1data = NULL;
15835 SAVE_IT (it1, it, it1data);
15836 start_display (&it1, w, startp);
15837 move_it_vertically (&it1, margin * frame_line_height);
15838 margin_pos = IT_CHARPOS (it1);
15839 RESTORE_IT (&it, &it, it1data);
15841 scrolling_up = PT > margin_pos;
15842 aggressive =
15843 scrolling_up
15844 ? BVAR (current_buffer, scroll_up_aggressively)
15845 : BVAR (current_buffer, scroll_down_aggressively);
15847 if (!MINI_WINDOW_P (w)
15848 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15850 int pt_offset = 0;
15852 /* Setting scroll-conservatively overrides
15853 scroll-*-aggressively. */
15854 if (!scroll_conservatively && NUMBERP (aggressive))
15856 double float_amount = XFLOATINT (aggressive);
15858 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15859 if (pt_offset == 0 && float_amount > 0)
15860 pt_offset = 1;
15861 if (pt_offset && margin > 0)
15862 margin -= 1;
15864 /* Compute how much to move the window start backward from
15865 point so that point will be displayed where the user
15866 wants it. */
15867 if (scrolling_up)
15869 centering_position = it.last_visible_y;
15870 if (pt_offset)
15871 centering_position -= pt_offset;
15872 centering_position -=
15873 frame_line_height * (1 + margin + (last_line_misfit != 0))
15874 + WINDOW_HEADER_LINE_HEIGHT (w);
15875 /* Don't let point enter the scroll margin near top of
15876 the window. */
15877 if (centering_position < margin * frame_line_height)
15878 centering_position = margin * frame_line_height;
15880 else
15881 centering_position = margin * frame_line_height + pt_offset;
15883 else
15884 /* Set the window start half the height of the window backward
15885 from point. */
15886 centering_position = window_box_height (w) / 2;
15888 move_it_vertically_backward (&it, centering_position);
15890 eassert (IT_CHARPOS (it) >= BEGV);
15892 /* The function move_it_vertically_backward may move over more
15893 than the specified y-distance. If it->w is small, e.g. a
15894 mini-buffer window, we may end up in front of the window's
15895 display area. Start displaying at the start of the line
15896 containing PT in this case. */
15897 if (it.current_y <= 0)
15899 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15900 move_it_vertically_backward (&it, 0);
15901 it.current_y = 0;
15904 it.current_x = it.hpos = 0;
15906 /* Set the window start position here explicitly, to avoid an
15907 infinite loop in case the functions in window-scroll-functions
15908 get errors. */
15909 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15911 /* Run scroll hooks. */
15912 startp = run_window_scroll_functions (window, it.current.pos);
15914 /* Redisplay the window. */
15915 if (!current_matrix_up_to_date_p
15916 || windows_or_buffers_changed
15917 || f->cursor_type_changed
15918 /* Don't use try_window_reusing_current_matrix in this case
15919 because it can have changed the buffer. */
15920 || !NILP (Vwindow_scroll_functions)
15921 || !just_this_one_p
15922 || MINI_WINDOW_P (w)
15923 || !(used_current_matrix_p
15924 = try_window_reusing_current_matrix (w)))
15925 try_window (window, startp, 0);
15927 /* If new fonts have been loaded (due to fontsets), give up. We
15928 have to start a new redisplay since we need to re-adjust glyph
15929 matrices. */
15930 if (f->fonts_changed)
15931 goto need_larger_matrices;
15933 /* If cursor did not appear assume that the middle of the window is
15934 in the first line of the window. Do it again with the next line.
15935 (Imagine a window of height 100, displaying two lines of height
15936 60. Moving back 50 from it->last_visible_y will end in the first
15937 line.) */
15938 if (w->cursor.vpos < 0)
15940 if (w->window_end_valid && PT >= Z - w->window_end_pos)
15942 clear_glyph_matrix (w->desired_matrix);
15943 move_it_by_lines (&it, 1);
15944 try_window (window, it.current.pos, 0);
15946 else if (PT < IT_CHARPOS (it))
15948 clear_glyph_matrix (w->desired_matrix);
15949 move_it_by_lines (&it, -1);
15950 try_window (window, it.current.pos, 0);
15952 else
15954 /* Not much we can do about it. */
15958 /* Consider the following case: Window starts at BEGV, there is
15959 invisible, intangible text at BEGV, so that display starts at
15960 some point START > BEGV. It can happen that we are called with
15961 PT somewhere between BEGV and START. Try to handle that case. */
15962 if (w->cursor.vpos < 0)
15964 struct glyph_row *row = w->current_matrix->rows;
15965 if (row->mode_line_p)
15966 ++row;
15967 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15970 if (!cursor_row_fully_visible_p (w, 0, 0))
15972 /* If vscroll is enabled, disable it and try again. */
15973 if (w->vscroll)
15975 w->vscroll = 0;
15976 clear_glyph_matrix (w->desired_matrix);
15977 goto recenter;
15980 /* Users who set scroll-conservatively to a large number want
15981 point just above/below the scroll margin. If we ended up
15982 with point's row partially visible, move the window start to
15983 make that row fully visible and out of the margin. */
15984 if (scroll_conservatively > SCROLL_LIMIT)
15986 int window_total_lines
15987 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
15988 int margin =
15989 scroll_margin > 0
15990 ? min (scroll_margin, window_total_lines / 4)
15991 : 0;
15992 int move_down = w->cursor.vpos >= window_total_lines / 2;
15994 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15995 clear_glyph_matrix (w->desired_matrix);
15996 if (1 == try_window (window, it.current.pos,
15997 TRY_WINDOW_CHECK_MARGINS))
15998 goto done;
16001 /* If centering point failed to make the whole line visible,
16002 put point at the top instead. That has to make the whole line
16003 visible, if it can be done. */
16004 if (centering_position == 0)
16005 goto done;
16007 clear_glyph_matrix (w->desired_matrix);
16008 centering_position = 0;
16009 goto recenter;
16012 done:
16014 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16015 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16016 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16018 /* Display the mode line, if we must. */
16019 if ((update_mode_line
16020 /* If window not full width, must redo its mode line
16021 if (a) the window to its side is being redone and
16022 (b) we do a frame-based redisplay. This is a consequence
16023 of how inverted lines are drawn in frame-based redisplay. */
16024 || (!just_this_one_p
16025 && !FRAME_WINDOW_P (f)
16026 && !WINDOW_FULL_WIDTH_P (w))
16027 /* Line number to display. */
16028 || w->base_line_pos > 0
16029 /* Column number is displayed and different from the one displayed. */
16030 || (w->column_number_displayed != -1
16031 && (w->column_number_displayed != current_column ())))
16032 /* This means that the window has a mode line. */
16033 && (WINDOW_WANTS_MODELINE_P (w)
16034 || WINDOW_WANTS_HEADER_LINE_P (w)))
16036 display_mode_lines (w);
16038 /* If mode line height has changed, arrange for a thorough
16039 immediate redisplay using the correct mode line height. */
16040 if (WINDOW_WANTS_MODELINE_P (w)
16041 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16043 f->fonts_changed = 1;
16044 w->mode_line_height = -1;
16045 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16046 = DESIRED_MODE_LINE_HEIGHT (w);
16049 /* If header line height has changed, arrange for a thorough
16050 immediate redisplay using the correct header line height. */
16051 if (WINDOW_WANTS_HEADER_LINE_P (w)
16052 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16054 f->fonts_changed = 1;
16055 w->header_line_height = -1;
16056 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16057 = DESIRED_HEADER_LINE_HEIGHT (w);
16060 if (f->fonts_changed)
16061 goto need_larger_matrices;
16064 if (!line_number_displayed && w->base_line_pos != -1)
16066 w->base_line_pos = 0;
16067 w->base_line_number = 0;
16070 finish_menu_bars:
16072 /* When we reach a frame's selected window, redo the frame's menu bar. */
16073 if (update_mode_line
16074 && EQ (FRAME_SELECTED_WINDOW (f), window))
16076 int redisplay_menu_p = 0;
16078 if (FRAME_WINDOW_P (f))
16080 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16081 || defined (HAVE_NS) || defined (USE_GTK)
16082 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16083 #else
16084 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16085 #endif
16087 else
16088 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16090 if (redisplay_menu_p)
16091 display_menu_bar (w);
16093 #ifdef HAVE_WINDOW_SYSTEM
16094 if (FRAME_WINDOW_P (f))
16096 #if defined (USE_GTK) || defined (HAVE_NS)
16097 if (FRAME_EXTERNAL_TOOL_BAR (f))
16098 redisplay_tool_bar (f);
16099 #else
16100 if (WINDOWP (f->tool_bar_window)
16101 && (FRAME_TOOL_BAR_LINES (f) > 0
16102 || !NILP (Vauto_resize_tool_bars))
16103 && redisplay_tool_bar (f))
16104 ignore_mouse_drag_p = 1;
16105 #endif
16107 #endif
16110 #ifdef HAVE_WINDOW_SYSTEM
16111 if (FRAME_WINDOW_P (f)
16112 && update_window_fringes (w, (just_this_one_p
16113 || (!used_current_matrix_p && !overlay_arrow_seen)
16114 || w->pseudo_window_p)))
16116 update_begin (f);
16117 block_input ();
16118 if (draw_window_fringes (w, 1))
16119 x_draw_vertical_border (w);
16120 unblock_input ();
16121 update_end (f);
16123 #endif /* HAVE_WINDOW_SYSTEM */
16125 /* We go to this label, with fonts_changed set, if it is
16126 necessary to try again using larger glyph matrices.
16127 We have to redeem the scroll bar even in this case,
16128 because the loop in redisplay_internal expects that. */
16129 need_larger_matrices:
16131 finish_scroll_bars:
16133 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16135 /* Set the thumb's position and size. */
16136 set_vertical_scroll_bar (w);
16138 /* Note that we actually used the scroll bar attached to this
16139 window, so it shouldn't be deleted at the end of redisplay. */
16140 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16141 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16144 /* Restore current_buffer and value of point in it. The window
16145 update may have changed the buffer, so first make sure `opoint'
16146 is still valid (Bug#6177). */
16147 if (CHARPOS (opoint) < BEGV)
16148 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16149 else if (CHARPOS (opoint) > ZV)
16150 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16151 else
16152 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16154 set_buffer_internal_1 (old);
16155 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16156 shorter. This can be caused by log truncation in *Messages*. */
16157 if (CHARPOS (lpoint) <= ZV)
16158 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16160 unbind_to (count, Qnil);
16164 /* Build the complete desired matrix of WINDOW with a window start
16165 buffer position POS.
16167 Value is 1 if successful. It is zero if fonts were loaded during
16168 redisplay which makes re-adjusting glyph matrices necessary, and -1
16169 if point would appear in the scroll margins.
16170 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16171 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16172 set in FLAGS.) */
16175 try_window (Lisp_Object window, struct text_pos pos, int flags)
16177 struct window *w = XWINDOW (window);
16178 struct it it;
16179 struct glyph_row *last_text_row = NULL;
16180 struct frame *f = XFRAME (w->frame);
16181 int frame_line_height = default_line_pixel_height (w);
16183 /* Make POS the new window start. */
16184 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16186 /* Mark cursor position as unknown. No overlay arrow seen. */
16187 w->cursor.vpos = -1;
16188 overlay_arrow_seen = 0;
16190 /* Initialize iterator and info to start at POS. */
16191 start_display (&it, w, pos);
16193 /* Display all lines of W. */
16194 while (it.current_y < it.last_visible_y)
16196 if (display_line (&it))
16197 last_text_row = it.glyph_row - 1;
16198 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16199 return 0;
16202 /* Don't let the cursor end in the scroll margins. */
16203 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16204 && !MINI_WINDOW_P (w))
16206 int this_scroll_margin;
16207 int window_total_lines
16208 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16210 if (scroll_margin > 0)
16212 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16213 this_scroll_margin *= frame_line_height;
16215 else
16216 this_scroll_margin = 0;
16218 if ((w->cursor.y >= 0 /* not vscrolled */
16219 && w->cursor.y < this_scroll_margin
16220 && CHARPOS (pos) > BEGV
16221 && IT_CHARPOS (it) < ZV)
16222 /* rms: considering make_cursor_line_fully_visible_p here
16223 seems to give wrong results. We don't want to recenter
16224 when the last line is partly visible, we want to allow
16225 that case to be handled in the usual way. */
16226 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16228 w->cursor.vpos = -1;
16229 clear_glyph_matrix (w->desired_matrix);
16230 return -1;
16234 /* If bottom moved off end of frame, change mode line percentage. */
16235 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16236 w->update_mode_line = 1;
16238 /* Set window_end_pos to the offset of the last character displayed
16239 on the window from the end of current_buffer. Set
16240 window_end_vpos to its row number. */
16241 if (last_text_row)
16243 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16244 adjust_window_ends (w, last_text_row, 0);
16245 eassert
16246 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16247 w->window_end_vpos)));
16249 else
16251 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16252 w->window_end_pos = Z - ZV;
16253 w->window_end_vpos = 0;
16256 /* But that is not valid info until redisplay finishes. */
16257 w->window_end_valid = 0;
16258 return 1;
16263 /************************************************************************
16264 Window redisplay reusing current matrix when buffer has not changed
16265 ************************************************************************/
16267 /* Try redisplay of window W showing an unchanged buffer with a
16268 different window start than the last time it was displayed by
16269 reusing its current matrix. Value is non-zero if successful.
16270 W->start is the new window start. */
16272 static int
16273 try_window_reusing_current_matrix (struct window *w)
16275 struct frame *f = XFRAME (w->frame);
16276 struct glyph_row *bottom_row;
16277 struct it it;
16278 struct run run;
16279 struct text_pos start, new_start;
16280 int nrows_scrolled, i;
16281 struct glyph_row *last_text_row;
16282 struct glyph_row *last_reused_text_row;
16283 struct glyph_row *start_row;
16284 int start_vpos, min_y, max_y;
16286 #ifdef GLYPH_DEBUG
16287 if (inhibit_try_window_reusing)
16288 return 0;
16289 #endif
16291 if (/* This function doesn't handle terminal frames. */
16292 !FRAME_WINDOW_P (f)
16293 /* Don't try to reuse the display if windows have been split
16294 or such. */
16295 || windows_or_buffers_changed
16296 || f->cursor_type_changed)
16297 return 0;
16299 /* Can't do this if showing trailing whitespace. */
16300 if (!NILP (Vshow_trailing_whitespace))
16301 return 0;
16303 /* If top-line visibility has changed, give up. */
16304 if (WINDOW_WANTS_HEADER_LINE_P (w)
16305 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16306 return 0;
16308 /* Give up if old or new display is scrolled vertically. We could
16309 make this function handle this, but right now it doesn't. */
16310 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16311 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16312 return 0;
16314 /* The variable new_start now holds the new window start. The old
16315 start `start' can be determined from the current matrix. */
16316 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16317 start = start_row->minpos;
16318 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16320 /* Clear the desired matrix for the display below. */
16321 clear_glyph_matrix (w->desired_matrix);
16323 if (CHARPOS (new_start) <= CHARPOS (start))
16325 /* Don't use this method if the display starts with an ellipsis
16326 displayed for invisible text. It's not easy to handle that case
16327 below, and it's certainly not worth the effort since this is
16328 not a frequent case. */
16329 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16330 return 0;
16332 IF_DEBUG (debug_method_add (w, "twu1"));
16334 /* Display up to a row that can be reused. The variable
16335 last_text_row is set to the last row displayed that displays
16336 text. Note that it.vpos == 0 if or if not there is a
16337 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16338 start_display (&it, w, new_start);
16339 w->cursor.vpos = -1;
16340 last_text_row = last_reused_text_row = NULL;
16342 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16344 /* If we have reached into the characters in the START row,
16345 that means the line boundaries have changed. So we
16346 can't start copying with the row START. Maybe it will
16347 work to start copying with the following row. */
16348 while (IT_CHARPOS (it) > CHARPOS (start))
16350 /* Advance to the next row as the "start". */
16351 start_row++;
16352 start = start_row->minpos;
16353 /* If there are no more rows to try, or just one, give up. */
16354 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16355 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16356 || CHARPOS (start) == ZV)
16358 clear_glyph_matrix (w->desired_matrix);
16359 return 0;
16362 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16364 /* If we have reached alignment, we can copy the rest of the
16365 rows. */
16366 if (IT_CHARPOS (it) == CHARPOS (start)
16367 /* Don't accept "alignment" inside a display vector,
16368 since start_row could have started in the middle of
16369 that same display vector (thus their character
16370 positions match), and we have no way of telling if
16371 that is the case. */
16372 && it.current.dpvec_index < 0)
16373 break;
16375 if (display_line (&it))
16376 last_text_row = it.glyph_row - 1;
16380 /* A value of current_y < last_visible_y means that we stopped
16381 at the previous window start, which in turn means that we
16382 have at least one reusable row. */
16383 if (it.current_y < it.last_visible_y)
16385 struct glyph_row *row;
16387 /* IT.vpos always starts from 0; it counts text lines. */
16388 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16390 /* Find PT if not already found in the lines displayed. */
16391 if (w->cursor.vpos < 0)
16393 int dy = it.current_y - start_row->y;
16395 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16396 row = row_containing_pos (w, PT, row, NULL, dy);
16397 if (row)
16398 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16399 dy, nrows_scrolled);
16400 else
16402 clear_glyph_matrix (w->desired_matrix);
16403 return 0;
16407 /* Scroll the display. Do it before the current matrix is
16408 changed. The problem here is that update has not yet
16409 run, i.e. part of the current matrix is not up to date.
16410 scroll_run_hook will clear the cursor, and use the
16411 current matrix to get the height of the row the cursor is
16412 in. */
16413 run.current_y = start_row->y;
16414 run.desired_y = it.current_y;
16415 run.height = it.last_visible_y - it.current_y;
16417 if (run.height > 0 && run.current_y != run.desired_y)
16419 update_begin (f);
16420 FRAME_RIF (f)->update_window_begin_hook (w);
16421 FRAME_RIF (f)->clear_window_mouse_face (w);
16422 FRAME_RIF (f)->scroll_run_hook (w, &run);
16423 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16424 update_end (f);
16427 /* Shift current matrix down by nrows_scrolled lines. */
16428 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16429 rotate_matrix (w->current_matrix,
16430 start_vpos,
16431 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16432 nrows_scrolled);
16434 /* Disable lines that must be updated. */
16435 for (i = 0; i < nrows_scrolled; ++i)
16436 (start_row + i)->enabled_p = 0;
16438 /* Re-compute Y positions. */
16439 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16440 max_y = it.last_visible_y;
16441 for (row = start_row + nrows_scrolled;
16442 row < bottom_row;
16443 ++row)
16445 row->y = it.current_y;
16446 row->visible_height = row->height;
16448 if (row->y < min_y)
16449 row->visible_height -= min_y - row->y;
16450 if (row->y + row->height > max_y)
16451 row->visible_height -= row->y + row->height - max_y;
16452 if (row->fringe_bitmap_periodic_p)
16453 row->redraw_fringe_bitmaps_p = 1;
16455 it.current_y += row->height;
16457 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16458 last_reused_text_row = row;
16459 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16460 break;
16463 /* Disable lines in the current matrix which are now
16464 below the window. */
16465 for (++row; row < bottom_row; ++row)
16466 row->enabled_p = row->mode_line_p = 0;
16469 /* Update window_end_pos etc.; last_reused_text_row is the last
16470 reused row from the current matrix containing text, if any.
16471 The value of last_text_row is the last displayed line
16472 containing text. */
16473 if (last_reused_text_row)
16474 adjust_window_ends (w, last_reused_text_row, 1);
16475 else if (last_text_row)
16476 adjust_window_ends (w, last_text_row, 0);
16477 else
16479 /* This window must be completely empty. */
16480 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16481 w->window_end_pos = Z - ZV;
16482 w->window_end_vpos = 0;
16484 w->window_end_valid = 0;
16486 /* Update hint: don't try scrolling again in update_window. */
16487 w->desired_matrix->no_scrolling_p = 1;
16489 #ifdef GLYPH_DEBUG
16490 debug_method_add (w, "try_window_reusing_current_matrix 1");
16491 #endif
16492 return 1;
16494 else if (CHARPOS (new_start) > CHARPOS (start))
16496 struct glyph_row *pt_row, *row;
16497 struct glyph_row *first_reusable_row;
16498 struct glyph_row *first_row_to_display;
16499 int dy;
16500 int yb = window_text_bottom_y (w);
16502 /* Find the row starting at new_start, if there is one. Don't
16503 reuse a partially visible line at the end. */
16504 first_reusable_row = start_row;
16505 while (first_reusable_row->enabled_p
16506 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16507 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16508 < CHARPOS (new_start)))
16509 ++first_reusable_row;
16511 /* Give up if there is no row to reuse. */
16512 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16513 || !first_reusable_row->enabled_p
16514 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16515 != CHARPOS (new_start)))
16516 return 0;
16518 /* We can reuse fully visible rows beginning with
16519 first_reusable_row to the end of the window. Set
16520 first_row_to_display to the first row that cannot be reused.
16521 Set pt_row to the row containing point, if there is any. */
16522 pt_row = NULL;
16523 for (first_row_to_display = first_reusable_row;
16524 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16525 ++first_row_to_display)
16527 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16528 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16529 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16530 && first_row_to_display->ends_at_zv_p
16531 && pt_row == NULL)))
16532 pt_row = first_row_to_display;
16535 /* Start displaying at the start of first_row_to_display. */
16536 eassert (first_row_to_display->y < yb);
16537 init_to_row_start (&it, w, first_row_to_display);
16539 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16540 - start_vpos);
16541 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16542 - nrows_scrolled);
16543 it.current_y = (first_row_to_display->y - first_reusable_row->y
16544 + WINDOW_HEADER_LINE_HEIGHT (w));
16546 /* Display lines beginning with first_row_to_display in the
16547 desired matrix. Set last_text_row to the last row displayed
16548 that displays text. */
16549 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16550 if (pt_row == NULL)
16551 w->cursor.vpos = -1;
16552 last_text_row = NULL;
16553 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16554 if (display_line (&it))
16555 last_text_row = it.glyph_row - 1;
16557 /* If point is in a reused row, adjust y and vpos of the cursor
16558 position. */
16559 if (pt_row)
16561 w->cursor.vpos -= nrows_scrolled;
16562 w->cursor.y -= first_reusable_row->y - start_row->y;
16565 /* Give up if point isn't in a row displayed or reused. (This
16566 also handles the case where w->cursor.vpos < nrows_scrolled
16567 after the calls to display_line, which can happen with scroll
16568 margins. See bug#1295.) */
16569 if (w->cursor.vpos < 0)
16571 clear_glyph_matrix (w->desired_matrix);
16572 return 0;
16575 /* Scroll the display. */
16576 run.current_y = first_reusable_row->y;
16577 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16578 run.height = it.last_visible_y - run.current_y;
16579 dy = run.current_y - run.desired_y;
16581 if (run.height)
16583 update_begin (f);
16584 FRAME_RIF (f)->update_window_begin_hook (w);
16585 FRAME_RIF (f)->clear_window_mouse_face (w);
16586 FRAME_RIF (f)->scroll_run_hook (w, &run);
16587 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16588 update_end (f);
16591 /* Adjust Y positions of reused rows. */
16592 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16593 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16594 max_y = it.last_visible_y;
16595 for (row = first_reusable_row; row < first_row_to_display; ++row)
16597 row->y -= dy;
16598 row->visible_height = row->height;
16599 if (row->y < min_y)
16600 row->visible_height -= min_y - row->y;
16601 if (row->y + row->height > max_y)
16602 row->visible_height -= row->y + row->height - max_y;
16603 if (row->fringe_bitmap_periodic_p)
16604 row->redraw_fringe_bitmaps_p = 1;
16607 /* Scroll the current matrix. */
16608 eassert (nrows_scrolled > 0);
16609 rotate_matrix (w->current_matrix,
16610 start_vpos,
16611 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16612 -nrows_scrolled);
16614 /* Disable rows not reused. */
16615 for (row -= nrows_scrolled; row < bottom_row; ++row)
16616 row->enabled_p = 0;
16618 /* Point may have moved to a different line, so we cannot assume that
16619 the previous cursor position is valid; locate the correct row. */
16620 if (pt_row)
16622 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16623 row < bottom_row
16624 && PT >= MATRIX_ROW_END_CHARPOS (row)
16625 && !row->ends_at_zv_p;
16626 row++)
16628 w->cursor.vpos++;
16629 w->cursor.y = row->y;
16631 if (row < bottom_row)
16633 /* Can't simply scan the row for point with
16634 bidi-reordered glyph rows. Let set_cursor_from_row
16635 figure out where to put the cursor, and if it fails,
16636 give up. */
16637 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16639 if (!set_cursor_from_row (w, row, w->current_matrix,
16640 0, 0, 0, 0))
16642 clear_glyph_matrix (w->desired_matrix);
16643 return 0;
16646 else
16648 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16649 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16651 for (; glyph < end
16652 && (!BUFFERP (glyph->object)
16653 || glyph->charpos < PT);
16654 glyph++)
16656 w->cursor.hpos++;
16657 w->cursor.x += glyph->pixel_width;
16663 /* Adjust window end. A null value of last_text_row means that
16664 the window end is in reused rows which in turn means that
16665 only its vpos can have changed. */
16666 if (last_text_row)
16667 adjust_window_ends (w, last_text_row, 0);
16668 else
16669 w->window_end_vpos -= nrows_scrolled;
16671 w->window_end_valid = 0;
16672 w->desired_matrix->no_scrolling_p = 1;
16674 #ifdef GLYPH_DEBUG
16675 debug_method_add (w, "try_window_reusing_current_matrix 2");
16676 #endif
16677 return 1;
16680 return 0;
16685 /************************************************************************
16686 Window redisplay reusing current matrix when buffer has changed
16687 ************************************************************************/
16689 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16690 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16691 ptrdiff_t *, ptrdiff_t *);
16692 static struct glyph_row *
16693 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16694 struct glyph_row *);
16697 /* Return the last row in MATRIX displaying text. If row START is
16698 non-null, start searching with that row. IT gives the dimensions
16699 of the display. Value is null if matrix is empty; otherwise it is
16700 a pointer to the row found. */
16702 static struct glyph_row *
16703 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16704 struct glyph_row *start)
16706 struct glyph_row *row, *row_found;
16708 /* Set row_found to the last row in IT->w's current matrix
16709 displaying text. The loop looks funny but think of partially
16710 visible lines. */
16711 row_found = NULL;
16712 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16713 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16715 eassert (row->enabled_p);
16716 row_found = row;
16717 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16718 break;
16719 ++row;
16722 return row_found;
16726 /* Return the last row in the current matrix of W that is not affected
16727 by changes at the start of current_buffer that occurred since W's
16728 current matrix was built. Value is null if no such row exists.
16730 BEG_UNCHANGED us the number of characters unchanged at the start of
16731 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16732 first changed character in current_buffer. Characters at positions <
16733 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16734 when the current matrix was built. */
16736 static struct glyph_row *
16737 find_last_unchanged_at_beg_row (struct window *w)
16739 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16740 struct glyph_row *row;
16741 struct glyph_row *row_found = NULL;
16742 int yb = window_text_bottom_y (w);
16744 /* Find the last row displaying unchanged text. */
16745 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16746 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16747 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16748 ++row)
16750 if (/* If row ends before first_changed_pos, it is unchanged,
16751 except in some case. */
16752 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16753 /* When row ends in ZV and we write at ZV it is not
16754 unchanged. */
16755 && !row->ends_at_zv_p
16756 /* When first_changed_pos is the end of a continued line,
16757 row is not unchanged because it may be no longer
16758 continued. */
16759 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16760 && (row->continued_p
16761 || row->exact_window_width_line_p))
16762 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16763 needs to be recomputed, so don't consider this row as
16764 unchanged. This happens when the last line was
16765 bidi-reordered and was killed immediately before this
16766 redisplay cycle. In that case, ROW->end stores the
16767 buffer position of the first visual-order character of
16768 the killed text, which is now beyond ZV. */
16769 && CHARPOS (row->end.pos) <= ZV)
16770 row_found = row;
16772 /* Stop if last visible row. */
16773 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16774 break;
16777 return row_found;
16781 /* Find the first glyph row in the current matrix of W that is not
16782 affected by changes at the end of current_buffer since the
16783 time W's current matrix was built.
16785 Return in *DELTA the number of chars by which buffer positions in
16786 unchanged text at the end of current_buffer must be adjusted.
16788 Return in *DELTA_BYTES the corresponding number of bytes.
16790 Value is null if no such row exists, i.e. all rows are affected by
16791 changes. */
16793 static struct glyph_row *
16794 find_first_unchanged_at_end_row (struct window *w,
16795 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16797 struct glyph_row *row;
16798 struct glyph_row *row_found = NULL;
16800 *delta = *delta_bytes = 0;
16802 /* Display must not have been paused, otherwise the current matrix
16803 is not up to date. */
16804 eassert (w->window_end_valid);
16806 /* A value of window_end_pos >= END_UNCHANGED means that the window
16807 end is in the range of changed text. If so, there is no
16808 unchanged row at the end of W's current matrix. */
16809 if (w->window_end_pos >= END_UNCHANGED)
16810 return NULL;
16812 /* Set row to the last row in W's current matrix displaying text. */
16813 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16815 /* If matrix is entirely empty, no unchanged row exists. */
16816 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16818 /* The value of row is the last glyph row in the matrix having a
16819 meaningful buffer position in it. The end position of row
16820 corresponds to window_end_pos. This allows us to translate
16821 buffer positions in the current matrix to current buffer
16822 positions for characters not in changed text. */
16823 ptrdiff_t Z_old =
16824 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16825 ptrdiff_t Z_BYTE_old =
16826 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16827 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16828 struct glyph_row *first_text_row
16829 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16831 *delta = Z - Z_old;
16832 *delta_bytes = Z_BYTE - Z_BYTE_old;
16834 /* Set last_unchanged_pos to the buffer position of the last
16835 character in the buffer that has not been changed. Z is the
16836 index + 1 of the last character in current_buffer, i.e. by
16837 subtracting END_UNCHANGED we get the index of the last
16838 unchanged character, and we have to add BEG to get its buffer
16839 position. */
16840 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16841 last_unchanged_pos_old = last_unchanged_pos - *delta;
16843 /* Search backward from ROW for a row displaying a line that
16844 starts at a minimum position >= last_unchanged_pos_old. */
16845 for (; row > first_text_row; --row)
16847 /* This used to abort, but it can happen.
16848 It is ok to just stop the search instead here. KFS. */
16849 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16850 break;
16852 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16853 row_found = row;
16857 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16859 return row_found;
16863 /* Make sure that glyph rows in the current matrix of window W
16864 reference the same glyph memory as corresponding rows in the
16865 frame's frame matrix. This function is called after scrolling W's
16866 current matrix on a terminal frame in try_window_id and
16867 try_window_reusing_current_matrix. */
16869 static void
16870 sync_frame_with_window_matrix_rows (struct window *w)
16872 struct frame *f = XFRAME (w->frame);
16873 struct glyph_row *window_row, *window_row_end, *frame_row;
16875 /* Preconditions: W must be a leaf window and full-width. Its frame
16876 must have a frame matrix. */
16877 eassert (BUFFERP (w->contents));
16878 eassert (WINDOW_FULL_WIDTH_P (w));
16879 eassert (!FRAME_WINDOW_P (f));
16881 /* If W is a full-width window, glyph pointers in W's current matrix
16882 have, by definition, to be the same as glyph pointers in the
16883 corresponding frame matrix. Note that frame matrices have no
16884 marginal areas (see build_frame_matrix). */
16885 window_row = w->current_matrix->rows;
16886 window_row_end = window_row + w->current_matrix->nrows;
16887 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16888 while (window_row < window_row_end)
16890 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16891 struct glyph *end = window_row->glyphs[LAST_AREA];
16893 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16894 frame_row->glyphs[TEXT_AREA] = start;
16895 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16896 frame_row->glyphs[LAST_AREA] = end;
16898 /* Disable frame rows whose corresponding window rows have
16899 been disabled in try_window_id. */
16900 if (!window_row->enabled_p)
16901 frame_row->enabled_p = 0;
16903 ++window_row, ++frame_row;
16908 /* Find the glyph row in window W containing CHARPOS. Consider all
16909 rows between START and END (not inclusive). END null means search
16910 all rows to the end of the display area of W. Value is the row
16911 containing CHARPOS or null. */
16913 struct glyph_row *
16914 row_containing_pos (struct window *w, ptrdiff_t charpos,
16915 struct glyph_row *start, struct glyph_row *end, int dy)
16917 struct glyph_row *row = start;
16918 struct glyph_row *best_row = NULL;
16919 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16920 int last_y;
16922 /* If we happen to start on a header-line, skip that. */
16923 if (row->mode_line_p)
16924 ++row;
16926 if ((end && row >= end) || !row->enabled_p)
16927 return NULL;
16929 last_y = window_text_bottom_y (w) - dy;
16931 while (1)
16933 /* Give up if we have gone too far. */
16934 if (end && row >= end)
16935 return NULL;
16936 /* This formerly returned if they were equal.
16937 I think that both quantities are of a "last plus one" type;
16938 if so, when they are equal, the row is within the screen. -- rms. */
16939 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16940 return NULL;
16942 /* If it is in this row, return this row. */
16943 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16944 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16945 /* The end position of a row equals the start
16946 position of the next row. If CHARPOS is there, we
16947 would rather consider it displayed in the next
16948 line, except when this line ends in ZV. */
16949 && !row_for_charpos_p (row, charpos)))
16950 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16952 struct glyph *g;
16954 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
16955 || (!best_row && !row->continued_p))
16956 return row;
16957 /* In bidi-reordered rows, there could be several rows whose
16958 edges surround CHARPOS, all of these rows belonging to
16959 the same continued line. We need to find the row which
16960 fits CHARPOS the best. */
16961 for (g = row->glyphs[TEXT_AREA];
16962 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16963 g++)
16965 if (!STRINGP (g->object))
16967 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16969 mindif = eabs (g->charpos - charpos);
16970 best_row = row;
16971 /* Exact match always wins. */
16972 if (mindif == 0)
16973 return best_row;
16978 else if (best_row && !row->continued_p)
16979 return best_row;
16980 ++row;
16985 /* Try to redisplay window W by reusing its existing display. W's
16986 current matrix must be up to date when this function is called,
16987 i.e. window_end_valid must be nonzero.
16989 Value is
16991 1 if display has been updated
16992 0 if otherwise unsuccessful
16993 -1 if redisplay with same window start is known not to succeed
16995 The following steps are performed:
16997 1. Find the last row in the current matrix of W that is not
16998 affected by changes at the start of current_buffer. If no such row
16999 is found, give up.
17001 2. Find the first row in W's current matrix that is not affected by
17002 changes at the end of current_buffer. Maybe there is no such row.
17004 3. Display lines beginning with the row + 1 found in step 1 to the
17005 row found in step 2 or, if step 2 didn't find a row, to the end of
17006 the window.
17008 4. If cursor is not known to appear on the window, give up.
17010 5. If display stopped at the row found in step 2, scroll the
17011 display and current matrix as needed.
17013 6. Maybe display some lines at the end of W, if we must. This can
17014 happen under various circumstances, like a partially visible line
17015 becoming fully visible, or because newly displayed lines are displayed
17016 in smaller font sizes.
17018 7. Update W's window end information. */
17020 static int
17021 try_window_id (struct window *w)
17023 struct frame *f = XFRAME (w->frame);
17024 struct glyph_matrix *current_matrix = w->current_matrix;
17025 struct glyph_matrix *desired_matrix = w->desired_matrix;
17026 struct glyph_row *last_unchanged_at_beg_row;
17027 struct glyph_row *first_unchanged_at_end_row;
17028 struct glyph_row *row;
17029 struct glyph_row *bottom_row;
17030 int bottom_vpos;
17031 struct it it;
17032 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17033 int dvpos, dy;
17034 struct text_pos start_pos;
17035 struct run run;
17036 int first_unchanged_at_end_vpos = 0;
17037 struct glyph_row *last_text_row, *last_text_row_at_end;
17038 struct text_pos start;
17039 ptrdiff_t first_changed_charpos, last_changed_charpos;
17041 #ifdef GLYPH_DEBUG
17042 if (inhibit_try_window_id)
17043 return 0;
17044 #endif
17046 /* This is handy for debugging. */
17047 #if 0
17048 #define GIVE_UP(X) \
17049 do { \
17050 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17051 return 0; \
17052 } while (0)
17053 #else
17054 #define GIVE_UP(X) return 0
17055 #endif
17057 SET_TEXT_POS_FROM_MARKER (start, w->start);
17059 /* Don't use this for mini-windows because these can show
17060 messages and mini-buffers, and we don't handle that here. */
17061 if (MINI_WINDOW_P (w))
17062 GIVE_UP (1);
17064 /* This flag is used to prevent redisplay optimizations. */
17065 if (windows_or_buffers_changed || f->cursor_type_changed)
17066 GIVE_UP (2);
17068 /* Verify that narrowing has not changed.
17069 Also verify that we were not told to prevent redisplay optimizations.
17070 It would be nice to further
17071 reduce the number of cases where this prevents try_window_id. */
17072 if (current_buffer->clip_changed
17073 || current_buffer->prevent_redisplay_optimizations_p)
17074 GIVE_UP (3);
17076 /* Window must either use window-based redisplay or be full width. */
17077 if (!FRAME_WINDOW_P (f)
17078 && (!FRAME_LINE_INS_DEL_OK (f)
17079 || !WINDOW_FULL_WIDTH_P (w)))
17080 GIVE_UP (4);
17082 /* Give up if point is known NOT to appear in W. */
17083 if (PT < CHARPOS (start))
17084 GIVE_UP (5);
17086 /* Another way to prevent redisplay optimizations. */
17087 if (w->last_modified == 0)
17088 GIVE_UP (6);
17090 /* Verify that window is not hscrolled. */
17091 if (w->hscroll != 0)
17092 GIVE_UP (7);
17094 /* Verify that display wasn't paused. */
17095 if (!w->window_end_valid)
17096 GIVE_UP (8);
17098 /* Likewise if highlighting trailing whitespace. */
17099 if (!NILP (Vshow_trailing_whitespace))
17100 GIVE_UP (11);
17102 /* Can't use this if overlay arrow position and/or string have
17103 changed. */
17104 if (overlay_arrows_changed_p ())
17105 GIVE_UP (12);
17107 /* When word-wrap is on, adding a space to the first word of a
17108 wrapped line can change the wrap position, altering the line
17109 above it. It might be worthwhile to handle this more
17110 intelligently, but for now just redisplay from scratch. */
17111 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17112 GIVE_UP (21);
17114 /* Under bidi reordering, adding or deleting a character in the
17115 beginning of a paragraph, before the first strong directional
17116 character, can change the base direction of the paragraph (unless
17117 the buffer specifies a fixed paragraph direction), which will
17118 require to redisplay the whole paragraph. It might be worthwhile
17119 to find the paragraph limits and widen the range of redisplayed
17120 lines to that, but for now just give up this optimization and
17121 redisplay from scratch. */
17122 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17123 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17124 GIVE_UP (22);
17126 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17127 only if buffer has really changed. The reason is that the gap is
17128 initially at Z for freshly visited files. The code below would
17129 set end_unchanged to 0 in that case. */
17130 if (MODIFF > SAVE_MODIFF
17131 /* This seems to happen sometimes after saving a buffer. */
17132 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17134 if (GPT - BEG < BEG_UNCHANGED)
17135 BEG_UNCHANGED = GPT - BEG;
17136 if (Z - GPT < END_UNCHANGED)
17137 END_UNCHANGED = Z - GPT;
17140 /* The position of the first and last character that has been changed. */
17141 first_changed_charpos = BEG + BEG_UNCHANGED;
17142 last_changed_charpos = Z - END_UNCHANGED;
17144 /* If window starts after a line end, and the last change is in
17145 front of that newline, then changes don't affect the display.
17146 This case happens with stealth-fontification. Note that although
17147 the display is unchanged, glyph positions in the matrix have to
17148 be adjusted, of course. */
17149 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17150 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17151 && ((last_changed_charpos < CHARPOS (start)
17152 && CHARPOS (start) == BEGV)
17153 || (last_changed_charpos < CHARPOS (start) - 1
17154 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17156 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17157 struct glyph_row *r0;
17159 /* Compute how many chars/bytes have been added to or removed
17160 from the buffer. */
17161 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17162 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17163 Z_delta = Z - Z_old;
17164 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17166 /* Give up if PT is not in the window. Note that it already has
17167 been checked at the start of try_window_id that PT is not in
17168 front of the window start. */
17169 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17170 GIVE_UP (13);
17172 /* If window start is unchanged, we can reuse the whole matrix
17173 as is, after adjusting glyph positions. No need to compute
17174 the window end again, since its offset from Z hasn't changed. */
17175 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17176 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17177 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17178 /* PT must not be in a partially visible line. */
17179 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17180 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17182 /* Adjust positions in the glyph matrix. */
17183 if (Z_delta || Z_delta_bytes)
17185 struct glyph_row *r1
17186 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17187 increment_matrix_positions (w->current_matrix,
17188 MATRIX_ROW_VPOS (r0, current_matrix),
17189 MATRIX_ROW_VPOS (r1, current_matrix),
17190 Z_delta, Z_delta_bytes);
17193 /* Set the cursor. */
17194 row = row_containing_pos (w, PT, r0, NULL, 0);
17195 if (row)
17196 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17197 return 1;
17201 /* Handle the case that changes are all below what is displayed in
17202 the window, and that PT is in the window. This shortcut cannot
17203 be taken if ZV is visible in the window, and text has been added
17204 there that is visible in the window. */
17205 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17206 /* ZV is not visible in the window, or there are no
17207 changes at ZV, actually. */
17208 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17209 || first_changed_charpos == last_changed_charpos))
17211 struct glyph_row *r0;
17213 /* Give up if PT is not in the window. Note that it already has
17214 been checked at the start of try_window_id that PT is not in
17215 front of the window start. */
17216 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17217 GIVE_UP (14);
17219 /* If window start is unchanged, we can reuse the whole matrix
17220 as is, without changing glyph positions since no text has
17221 been added/removed in front of the window end. */
17222 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17223 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17224 /* PT must not be in a partially visible line. */
17225 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17226 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17228 /* We have to compute the window end anew since text
17229 could have been added/removed after it. */
17230 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17231 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17233 /* Set the cursor. */
17234 row = row_containing_pos (w, PT, r0, NULL, 0);
17235 if (row)
17236 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17237 return 2;
17241 /* Give up if window start is in the changed area.
17243 The condition used to read
17245 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17247 but why that was tested escapes me at the moment. */
17248 if (CHARPOS (start) >= first_changed_charpos
17249 && CHARPOS (start) <= last_changed_charpos)
17250 GIVE_UP (15);
17252 /* Check that window start agrees with the start of the first glyph
17253 row in its current matrix. Check this after we know the window
17254 start is not in changed text, otherwise positions would not be
17255 comparable. */
17256 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17257 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17258 GIVE_UP (16);
17260 /* Give up if the window ends in strings. Overlay strings
17261 at the end are difficult to handle, so don't try. */
17262 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17263 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17264 GIVE_UP (20);
17266 /* Compute the position at which we have to start displaying new
17267 lines. Some of the lines at the top of the window might be
17268 reusable because they are not displaying changed text. Find the
17269 last row in W's current matrix not affected by changes at the
17270 start of current_buffer. Value is null if changes start in the
17271 first line of window. */
17272 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17273 if (last_unchanged_at_beg_row)
17275 /* Avoid starting to display in the middle of a character, a TAB
17276 for instance. This is easier than to set up the iterator
17277 exactly, and it's not a frequent case, so the additional
17278 effort wouldn't really pay off. */
17279 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17280 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17281 && last_unchanged_at_beg_row > w->current_matrix->rows)
17282 --last_unchanged_at_beg_row;
17284 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17285 GIVE_UP (17);
17287 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17288 GIVE_UP (18);
17289 start_pos = it.current.pos;
17291 /* Start displaying new lines in the desired matrix at the same
17292 vpos we would use in the current matrix, i.e. below
17293 last_unchanged_at_beg_row. */
17294 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17295 current_matrix);
17296 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17297 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17299 eassert (it.hpos == 0 && it.current_x == 0);
17301 else
17303 /* There are no reusable lines at the start of the window.
17304 Start displaying in the first text line. */
17305 start_display (&it, w, start);
17306 it.vpos = it.first_vpos;
17307 start_pos = it.current.pos;
17310 /* Find the first row that is not affected by changes at the end of
17311 the buffer. Value will be null if there is no unchanged row, in
17312 which case we must redisplay to the end of the window. delta
17313 will be set to the value by which buffer positions beginning with
17314 first_unchanged_at_end_row have to be adjusted due to text
17315 changes. */
17316 first_unchanged_at_end_row
17317 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17318 IF_DEBUG (debug_delta = delta);
17319 IF_DEBUG (debug_delta_bytes = delta_bytes);
17321 /* Set stop_pos to the buffer position up to which we will have to
17322 display new lines. If first_unchanged_at_end_row != NULL, this
17323 is the buffer position of the start of the line displayed in that
17324 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17325 that we don't stop at a buffer position. */
17326 stop_pos = 0;
17327 if (first_unchanged_at_end_row)
17329 eassert (last_unchanged_at_beg_row == NULL
17330 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17332 /* If this is a continuation line, move forward to the next one
17333 that isn't. Changes in lines above affect this line.
17334 Caution: this may move first_unchanged_at_end_row to a row
17335 not displaying text. */
17336 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17337 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17338 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17339 < it.last_visible_y))
17340 ++first_unchanged_at_end_row;
17342 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17343 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17344 >= it.last_visible_y))
17345 first_unchanged_at_end_row = NULL;
17346 else
17348 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17349 + delta);
17350 first_unchanged_at_end_vpos
17351 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17352 eassert (stop_pos >= Z - END_UNCHANGED);
17355 else if (last_unchanged_at_beg_row == NULL)
17356 GIVE_UP (19);
17359 #ifdef GLYPH_DEBUG
17361 /* Either there is no unchanged row at the end, or the one we have
17362 now displays text. This is a necessary condition for the window
17363 end pos calculation at the end of this function. */
17364 eassert (first_unchanged_at_end_row == NULL
17365 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17367 debug_last_unchanged_at_beg_vpos
17368 = (last_unchanged_at_beg_row
17369 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17370 : -1);
17371 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17373 #endif /* GLYPH_DEBUG */
17376 /* Display new lines. Set last_text_row to the last new line
17377 displayed which has text on it, i.e. might end up as being the
17378 line where the window_end_vpos is. */
17379 w->cursor.vpos = -1;
17380 last_text_row = NULL;
17381 overlay_arrow_seen = 0;
17382 while (it.current_y < it.last_visible_y
17383 && !f->fonts_changed
17384 && (first_unchanged_at_end_row == NULL
17385 || IT_CHARPOS (it) < stop_pos))
17387 if (display_line (&it))
17388 last_text_row = it.glyph_row - 1;
17391 if (f->fonts_changed)
17392 return -1;
17395 /* Compute differences in buffer positions, y-positions etc. for
17396 lines reused at the bottom of the window. Compute what we can
17397 scroll. */
17398 if (first_unchanged_at_end_row
17399 /* No lines reused because we displayed everything up to the
17400 bottom of the window. */
17401 && it.current_y < it.last_visible_y)
17403 dvpos = (it.vpos
17404 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17405 current_matrix));
17406 dy = it.current_y - first_unchanged_at_end_row->y;
17407 run.current_y = first_unchanged_at_end_row->y;
17408 run.desired_y = run.current_y + dy;
17409 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17411 else
17413 delta = delta_bytes = dvpos = dy
17414 = run.current_y = run.desired_y = run.height = 0;
17415 first_unchanged_at_end_row = NULL;
17417 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17420 /* Find the cursor if not already found. We have to decide whether
17421 PT will appear on this window (it sometimes doesn't, but this is
17422 not a very frequent case.) This decision has to be made before
17423 the current matrix is altered. A value of cursor.vpos < 0 means
17424 that PT is either in one of the lines beginning at
17425 first_unchanged_at_end_row or below the window. Don't care for
17426 lines that might be displayed later at the window end; as
17427 mentioned, this is not a frequent case. */
17428 if (w->cursor.vpos < 0)
17430 /* Cursor in unchanged rows at the top? */
17431 if (PT < CHARPOS (start_pos)
17432 && last_unchanged_at_beg_row)
17434 row = row_containing_pos (w, PT,
17435 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17436 last_unchanged_at_beg_row + 1, 0);
17437 if (row)
17438 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17441 /* Start from first_unchanged_at_end_row looking for PT. */
17442 else if (first_unchanged_at_end_row)
17444 row = row_containing_pos (w, PT - delta,
17445 first_unchanged_at_end_row, NULL, 0);
17446 if (row)
17447 set_cursor_from_row (w, row, w->current_matrix, delta,
17448 delta_bytes, dy, dvpos);
17451 /* Give up if cursor was not found. */
17452 if (w->cursor.vpos < 0)
17454 clear_glyph_matrix (w->desired_matrix);
17455 return -1;
17459 /* Don't let the cursor end in the scroll margins. */
17461 int this_scroll_margin, cursor_height;
17462 int frame_line_height = default_line_pixel_height (w);
17463 int window_total_lines
17464 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17466 this_scroll_margin =
17467 max (0, min (scroll_margin, window_total_lines / 4));
17468 this_scroll_margin *= frame_line_height;
17469 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17471 if ((w->cursor.y < this_scroll_margin
17472 && CHARPOS (start) > BEGV)
17473 /* Old redisplay didn't take scroll margin into account at the bottom,
17474 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17475 || (w->cursor.y + (make_cursor_line_fully_visible_p
17476 ? cursor_height + this_scroll_margin
17477 : 1)) > it.last_visible_y)
17479 w->cursor.vpos = -1;
17480 clear_glyph_matrix (w->desired_matrix);
17481 return -1;
17485 /* Scroll the display. Do it before changing the current matrix so
17486 that xterm.c doesn't get confused about where the cursor glyph is
17487 found. */
17488 if (dy && run.height)
17490 update_begin (f);
17492 if (FRAME_WINDOW_P (f))
17494 FRAME_RIF (f)->update_window_begin_hook (w);
17495 FRAME_RIF (f)->clear_window_mouse_face (w);
17496 FRAME_RIF (f)->scroll_run_hook (w, &run);
17497 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17499 else
17501 /* Terminal frame. In this case, dvpos gives the number of
17502 lines to scroll by; dvpos < 0 means scroll up. */
17503 int from_vpos
17504 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17505 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17506 int end = (WINDOW_TOP_EDGE_LINE (w)
17507 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17508 + window_internal_height (w));
17510 #if defined (HAVE_GPM) || defined (MSDOS)
17511 x_clear_window_mouse_face (w);
17512 #endif
17513 /* Perform the operation on the screen. */
17514 if (dvpos > 0)
17516 /* Scroll last_unchanged_at_beg_row to the end of the
17517 window down dvpos lines. */
17518 set_terminal_window (f, end);
17520 /* On dumb terminals delete dvpos lines at the end
17521 before inserting dvpos empty lines. */
17522 if (!FRAME_SCROLL_REGION_OK (f))
17523 ins_del_lines (f, end - dvpos, -dvpos);
17525 /* Insert dvpos empty lines in front of
17526 last_unchanged_at_beg_row. */
17527 ins_del_lines (f, from, dvpos);
17529 else if (dvpos < 0)
17531 /* Scroll up last_unchanged_at_beg_vpos to the end of
17532 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17533 set_terminal_window (f, end);
17535 /* Delete dvpos lines in front of
17536 last_unchanged_at_beg_vpos. ins_del_lines will set
17537 the cursor to the given vpos and emit |dvpos| delete
17538 line sequences. */
17539 ins_del_lines (f, from + dvpos, dvpos);
17541 /* On a dumb terminal insert dvpos empty lines at the
17542 end. */
17543 if (!FRAME_SCROLL_REGION_OK (f))
17544 ins_del_lines (f, end + dvpos, -dvpos);
17547 set_terminal_window (f, 0);
17550 update_end (f);
17553 /* Shift reused rows of the current matrix to the right position.
17554 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17555 text. */
17556 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17557 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17558 if (dvpos < 0)
17560 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17561 bottom_vpos, dvpos);
17562 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17563 bottom_vpos);
17565 else if (dvpos > 0)
17567 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17568 bottom_vpos, dvpos);
17569 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17570 first_unchanged_at_end_vpos + dvpos);
17573 /* For frame-based redisplay, make sure that current frame and window
17574 matrix are in sync with respect to glyph memory. */
17575 if (!FRAME_WINDOW_P (f))
17576 sync_frame_with_window_matrix_rows (w);
17578 /* Adjust buffer positions in reused rows. */
17579 if (delta || delta_bytes)
17580 increment_matrix_positions (current_matrix,
17581 first_unchanged_at_end_vpos + dvpos,
17582 bottom_vpos, delta, delta_bytes);
17584 /* Adjust Y positions. */
17585 if (dy)
17586 shift_glyph_matrix (w, current_matrix,
17587 first_unchanged_at_end_vpos + dvpos,
17588 bottom_vpos, dy);
17590 if (first_unchanged_at_end_row)
17592 first_unchanged_at_end_row += dvpos;
17593 if (first_unchanged_at_end_row->y >= it.last_visible_y
17594 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17595 first_unchanged_at_end_row = NULL;
17598 /* If scrolling up, there may be some lines to display at the end of
17599 the window. */
17600 last_text_row_at_end = NULL;
17601 if (dy < 0)
17603 /* Scrolling up can leave for example a partially visible line
17604 at the end of the window to be redisplayed. */
17605 /* Set last_row to the glyph row in the current matrix where the
17606 window end line is found. It has been moved up or down in
17607 the matrix by dvpos. */
17608 int last_vpos = w->window_end_vpos + dvpos;
17609 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17611 /* If last_row is the window end line, it should display text. */
17612 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17614 /* If window end line was partially visible before, begin
17615 displaying at that line. Otherwise begin displaying with the
17616 line following it. */
17617 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17619 init_to_row_start (&it, w, last_row);
17620 it.vpos = last_vpos;
17621 it.current_y = last_row->y;
17623 else
17625 init_to_row_end (&it, w, last_row);
17626 it.vpos = 1 + last_vpos;
17627 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17628 ++last_row;
17631 /* We may start in a continuation line. If so, we have to
17632 get the right continuation_lines_width and current_x. */
17633 it.continuation_lines_width = last_row->continuation_lines_width;
17634 it.hpos = it.current_x = 0;
17636 /* Display the rest of the lines at the window end. */
17637 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17638 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17640 /* Is it always sure that the display agrees with lines in
17641 the current matrix? I don't think so, so we mark rows
17642 displayed invalid in the current matrix by setting their
17643 enabled_p flag to zero. */
17644 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17645 if (display_line (&it))
17646 last_text_row_at_end = it.glyph_row - 1;
17650 /* Update window_end_pos and window_end_vpos. */
17651 if (first_unchanged_at_end_row && !last_text_row_at_end)
17653 /* Window end line if one of the preserved rows from the current
17654 matrix. Set row to the last row displaying text in current
17655 matrix starting at first_unchanged_at_end_row, after
17656 scrolling. */
17657 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17658 row = find_last_row_displaying_text (w->current_matrix, &it,
17659 first_unchanged_at_end_row);
17660 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17661 adjust_window_ends (w, row, 1);
17662 eassert (w->window_end_bytepos >= 0);
17663 IF_DEBUG (debug_method_add (w, "A"));
17665 else if (last_text_row_at_end)
17667 adjust_window_ends (w, last_text_row_at_end, 0);
17668 eassert (w->window_end_bytepos >= 0);
17669 IF_DEBUG (debug_method_add (w, "B"));
17671 else if (last_text_row)
17673 /* We have displayed either to the end of the window or at the
17674 end of the window, i.e. the last row with text is to be found
17675 in the desired matrix. */
17676 adjust_window_ends (w, last_text_row, 0);
17677 eassert (w->window_end_bytepos >= 0);
17679 else if (first_unchanged_at_end_row == NULL
17680 && last_text_row == NULL
17681 && last_text_row_at_end == NULL)
17683 /* Displayed to end of window, but no line containing text was
17684 displayed. Lines were deleted at the end of the window. */
17685 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17686 int vpos = w->window_end_vpos;
17687 struct glyph_row *current_row = current_matrix->rows + vpos;
17688 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17690 for (row = NULL;
17691 row == NULL && vpos >= first_vpos;
17692 --vpos, --current_row, --desired_row)
17694 if (desired_row->enabled_p)
17696 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17697 row = desired_row;
17699 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17700 row = current_row;
17703 eassert (row != NULL);
17704 w->window_end_vpos = vpos + 1;
17705 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17706 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17707 eassert (w->window_end_bytepos >= 0);
17708 IF_DEBUG (debug_method_add (w, "C"));
17710 else
17711 emacs_abort ();
17713 IF_DEBUG (debug_end_pos = w->window_end_pos;
17714 debug_end_vpos = w->window_end_vpos);
17716 /* Record that display has not been completed. */
17717 w->window_end_valid = 0;
17718 w->desired_matrix->no_scrolling_p = 1;
17719 return 3;
17721 #undef GIVE_UP
17726 /***********************************************************************
17727 More debugging support
17728 ***********************************************************************/
17730 #ifdef GLYPH_DEBUG
17732 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17733 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17734 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17737 /* Dump the contents of glyph matrix MATRIX on stderr.
17739 GLYPHS 0 means don't show glyph contents.
17740 GLYPHS 1 means show glyphs in short form
17741 GLYPHS > 1 means show glyphs in long form. */
17743 void
17744 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17746 int i;
17747 for (i = 0; i < matrix->nrows; ++i)
17748 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17752 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17753 the glyph row and area where the glyph comes from. */
17755 void
17756 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17758 if (glyph->type == CHAR_GLYPH
17759 || glyph->type == GLYPHLESS_GLYPH)
17761 fprintf (stderr,
17762 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17763 glyph - row->glyphs[TEXT_AREA],
17764 (glyph->type == CHAR_GLYPH
17765 ? 'C'
17766 : 'G'),
17767 glyph->charpos,
17768 (BUFFERP (glyph->object)
17769 ? 'B'
17770 : (STRINGP (glyph->object)
17771 ? 'S'
17772 : (INTEGERP (glyph->object)
17773 ? '0'
17774 : '-'))),
17775 glyph->pixel_width,
17776 glyph->u.ch,
17777 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17778 ? glyph->u.ch
17779 : '.'),
17780 glyph->face_id,
17781 glyph->left_box_line_p,
17782 glyph->right_box_line_p);
17784 else if (glyph->type == STRETCH_GLYPH)
17786 fprintf (stderr,
17787 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17788 glyph - row->glyphs[TEXT_AREA],
17789 'S',
17790 glyph->charpos,
17791 (BUFFERP (glyph->object)
17792 ? 'B'
17793 : (STRINGP (glyph->object)
17794 ? 'S'
17795 : (INTEGERP (glyph->object)
17796 ? '0'
17797 : '-'))),
17798 glyph->pixel_width,
17800 ' ',
17801 glyph->face_id,
17802 glyph->left_box_line_p,
17803 glyph->right_box_line_p);
17805 else if (glyph->type == IMAGE_GLYPH)
17807 fprintf (stderr,
17808 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17809 glyph - row->glyphs[TEXT_AREA],
17810 'I',
17811 glyph->charpos,
17812 (BUFFERP (glyph->object)
17813 ? 'B'
17814 : (STRINGP (glyph->object)
17815 ? 'S'
17816 : (INTEGERP (glyph->object)
17817 ? '0'
17818 : '-'))),
17819 glyph->pixel_width,
17820 glyph->u.img_id,
17821 '.',
17822 glyph->face_id,
17823 glyph->left_box_line_p,
17824 glyph->right_box_line_p);
17826 else if (glyph->type == COMPOSITE_GLYPH)
17828 fprintf (stderr,
17829 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17830 glyph - row->glyphs[TEXT_AREA],
17831 '+',
17832 glyph->charpos,
17833 (BUFFERP (glyph->object)
17834 ? 'B'
17835 : (STRINGP (glyph->object)
17836 ? 'S'
17837 : (INTEGERP (glyph->object)
17838 ? '0'
17839 : '-'))),
17840 glyph->pixel_width,
17841 glyph->u.cmp.id);
17842 if (glyph->u.cmp.automatic)
17843 fprintf (stderr,
17844 "[%d-%d]",
17845 glyph->slice.cmp.from, glyph->slice.cmp.to);
17846 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17847 glyph->face_id,
17848 glyph->left_box_line_p,
17849 glyph->right_box_line_p);
17854 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17855 GLYPHS 0 means don't show glyph contents.
17856 GLYPHS 1 means show glyphs in short form
17857 GLYPHS > 1 means show glyphs in long form. */
17859 void
17860 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17862 if (glyphs != 1)
17864 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17865 fprintf (stderr, "==============================================================================\n");
17867 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17868 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17869 vpos,
17870 MATRIX_ROW_START_CHARPOS (row),
17871 MATRIX_ROW_END_CHARPOS (row),
17872 row->used[TEXT_AREA],
17873 row->contains_overlapping_glyphs_p,
17874 row->enabled_p,
17875 row->truncated_on_left_p,
17876 row->truncated_on_right_p,
17877 row->continued_p,
17878 MATRIX_ROW_CONTINUATION_LINE_P (row),
17879 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17880 row->ends_at_zv_p,
17881 row->fill_line_p,
17882 row->ends_in_middle_of_char_p,
17883 row->starts_in_middle_of_char_p,
17884 row->mouse_face_p,
17885 row->x,
17886 row->y,
17887 row->pixel_width,
17888 row->height,
17889 row->visible_height,
17890 row->ascent,
17891 row->phys_ascent);
17892 /* The next 3 lines should align to "Start" in the header. */
17893 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17894 row->end.overlay_string_index,
17895 row->continuation_lines_width);
17896 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17897 CHARPOS (row->start.string_pos),
17898 CHARPOS (row->end.string_pos));
17899 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17900 row->end.dpvec_index);
17903 if (glyphs > 1)
17905 int area;
17907 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17909 struct glyph *glyph = row->glyphs[area];
17910 struct glyph *glyph_end = glyph + row->used[area];
17912 /* Glyph for a line end in text. */
17913 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17914 ++glyph_end;
17916 if (glyph < glyph_end)
17917 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17919 for (; glyph < glyph_end; ++glyph)
17920 dump_glyph (row, glyph, area);
17923 else if (glyphs == 1)
17925 int area;
17927 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17929 char *s = alloca (row->used[area] + 4);
17930 int i;
17932 for (i = 0; i < row->used[area]; ++i)
17934 struct glyph *glyph = row->glyphs[area] + i;
17935 if (i == row->used[area] - 1
17936 && area == TEXT_AREA
17937 && INTEGERP (glyph->object)
17938 && glyph->type == CHAR_GLYPH
17939 && glyph->u.ch == ' ')
17941 strcpy (&s[i], "[\\n]");
17942 i += 4;
17944 else if (glyph->type == CHAR_GLYPH
17945 && glyph->u.ch < 0x80
17946 && glyph->u.ch >= ' ')
17947 s[i] = glyph->u.ch;
17948 else
17949 s[i] = '.';
17952 s[i] = '\0';
17953 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17959 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17960 Sdump_glyph_matrix, 0, 1, "p",
17961 doc: /* Dump the current matrix of the selected window to stderr.
17962 Shows contents of glyph row structures. With non-nil
17963 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17964 glyphs in short form, otherwise show glyphs in long form. */)
17965 (Lisp_Object glyphs)
17967 struct window *w = XWINDOW (selected_window);
17968 struct buffer *buffer = XBUFFER (w->contents);
17970 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17971 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17972 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17973 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17974 fprintf (stderr, "=============================================\n");
17975 dump_glyph_matrix (w->current_matrix,
17976 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
17977 return Qnil;
17981 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17982 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17983 (void)
17985 struct frame *f = XFRAME (selected_frame);
17986 dump_glyph_matrix (f->current_matrix, 1);
17987 return Qnil;
17991 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17992 doc: /* Dump glyph row ROW to stderr.
17993 GLYPH 0 means don't dump glyphs.
17994 GLYPH 1 means dump glyphs in short form.
17995 GLYPH > 1 or omitted means dump glyphs in long form. */)
17996 (Lisp_Object row, Lisp_Object glyphs)
17998 struct glyph_matrix *matrix;
17999 EMACS_INT vpos;
18001 CHECK_NUMBER (row);
18002 matrix = XWINDOW (selected_window)->current_matrix;
18003 vpos = XINT (row);
18004 if (vpos >= 0 && vpos < matrix->nrows)
18005 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18006 vpos,
18007 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18008 return Qnil;
18012 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18013 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18014 GLYPH 0 means don't dump glyphs.
18015 GLYPH 1 means dump glyphs in short form.
18016 GLYPH > 1 or omitted means dump glyphs in long form.
18018 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18019 do nothing. */)
18020 (Lisp_Object row, Lisp_Object glyphs)
18022 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18023 struct frame *sf = SELECTED_FRAME ();
18024 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18025 EMACS_INT vpos;
18027 CHECK_NUMBER (row);
18028 vpos = XINT (row);
18029 if (vpos >= 0 && vpos < m->nrows)
18030 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18031 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18032 #endif
18033 return Qnil;
18037 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18038 doc: /* Toggle tracing of redisplay.
18039 With ARG, turn tracing on if and only if ARG is positive. */)
18040 (Lisp_Object arg)
18042 if (NILP (arg))
18043 trace_redisplay_p = !trace_redisplay_p;
18044 else
18046 arg = Fprefix_numeric_value (arg);
18047 trace_redisplay_p = XINT (arg) > 0;
18050 return Qnil;
18054 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18055 doc: /* Like `format', but print result to stderr.
18056 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18057 (ptrdiff_t nargs, Lisp_Object *args)
18059 Lisp_Object s = Fformat (nargs, args);
18060 fprintf (stderr, "%s", SDATA (s));
18061 return Qnil;
18064 #endif /* GLYPH_DEBUG */
18068 /***********************************************************************
18069 Building Desired Matrix Rows
18070 ***********************************************************************/
18072 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18073 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18075 static struct glyph_row *
18076 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18078 struct frame *f = XFRAME (WINDOW_FRAME (w));
18079 struct buffer *buffer = XBUFFER (w->contents);
18080 struct buffer *old = current_buffer;
18081 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18082 int arrow_len = SCHARS (overlay_arrow_string);
18083 const unsigned char *arrow_end = arrow_string + arrow_len;
18084 const unsigned char *p;
18085 struct it it;
18086 bool multibyte_p;
18087 int n_glyphs_before;
18089 set_buffer_temp (buffer);
18090 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18091 it.glyph_row->used[TEXT_AREA] = 0;
18092 SET_TEXT_POS (it.position, 0, 0);
18094 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18095 p = arrow_string;
18096 while (p < arrow_end)
18098 Lisp_Object face, ilisp;
18100 /* Get the next character. */
18101 if (multibyte_p)
18102 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18103 else
18105 it.c = it.char_to_display = *p, it.len = 1;
18106 if (! ASCII_CHAR_P (it.c))
18107 it.char_to_display = BYTE8_TO_CHAR (it.c);
18109 p += it.len;
18111 /* Get its face. */
18112 ilisp = make_number (p - arrow_string);
18113 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18114 it.face_id = compute_char_face (f, it.char_to_display, face);
18116 /* Compute its width, get its glyphs. */
18117 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18118 SET_TEXT_POS (it.position, -1, -1);
18119 PRODUCE_GLYPHS (&it);
18121 /* If this character doesn't fit any more in the line, we have
18122 to remove some glyphs. */
18123 if (it.current_x > it.last_visible_x)
18125 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18126 break;
18130 set_buffer_temp (old);
18131 return it.glyph_row;
18135 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18136 glyphs to insert is determined by produce_special_glyphs. */
18138 static void
18139 insert_left_trunc_glyphs (struct it *it)
18141 struct it truncate_it;
18142 struct glyph *from, *end, *to, *toend;
18144 eassert (!FRAME_WINDOW_P (it->f)
18145 || (!it->glyph_row->reversed_p
18146 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18147 || (it->glyph_row->reversed_p
18148 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18150 /* Get the truncation glyphs. */
18151 truncate_it = *it;
18152 truncate_it.current_x = 0;
18153 truncate_it.face_id = DEFAULT_FACE_ID;
18154 truncate_it.glyph_row = &scratch_glyph_row;
18155 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18156 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18157 truncate_it.object = make_number (0);
18158 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18160 /* Overwrite glyphs from IT with truncation glyphs. */
18161 if (!it->glyph_row->reversed_p)
18163 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18165 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18166 end = from + tused;
18167 to = it->glyph_row->glyphs[TEXT_AREA];
18168 toend = to + it->glyph_row->used[TEXT_AREA];
18169 if (FRAME_WINDOW_P (it->f))
18171 /* On GUI frames, when variable-size fonts are displayed,
18172 the truncation glyphs may need more pixels than the row's
18173 glyphs they overwrite. We overwrite more glyphs to free
18174 enough screen real estate, and enlarge the stretch glyph
18175 on the right (see display_line), if there is one, to
18176 preserve the screen position of the truncation glyphs on
18177 the right. */
18178 int w = 0;
18179 struct glyph *g = to;
18180 short used;
18182 /* The first glyph could be partially visible, in which case
18183 it->glyph_row->x will be negative. But we want the left
18184 truncation glyphs to be aligned at the left margin of the
18185 window, so we override the x coordinate at which the row
18186 will begin. */
18187 it->glyph_row->x = 0;
18188 while (g < toend && w < it->truncation_pixel_width)
18190 w += g->pixel_width;
18191 ++g;
18193 if (g - to - tused > 0)
18195 memmove (to + tused, g, (toend - g) * sizeof(*g));
18196 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18198 used = it->glyph_row->used[TEXT_AREA];
18199 if (it->glyph_row->truncated_on_right_p
18200 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18201 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18202 == STRETCH_GLYPH)
18204 int extra = w - it->truncation_pixel_width;
18206 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18210 while (from < end)
18211 *to++ = *from++;
18213 /* There may be padding glyphs left over. Overwrite them too. */
18214 if (!FRAME_WINDOW_P (it->f))
18216 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18218 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18219 while (from < end)
18220 *to++ = *from++;
18224 if (to > toend)
18225 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18227 else
18229 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18231 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18232 that back to front. */
18233 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18234 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18235 toend = it->glyph_row->glyphs[TEXT_AREA];
18236 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18237 if (FRAME_WINDOW_P (it->f))
18239 int w = 0;
18240 struct glyph *g = to;
18242 while (g >= toend && w < it->truncation_pixel_width)
18244 w += g->pixel_width;
18245 --g;
18247 if (to - g - tused > 0)
18248 to = g + tused;
18249 if (it->glyph_row->truncated_on_right_p
18250 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18251 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18253 int extra = w - it->truncation_pixel_width;
18255 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18259 while (from >= end && to >= toend)
18260 *to-- = *from--;
18261 if (!FRAME_WINDOW_P (it->f))
18263 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18265 from =
18266 truncate_it.glyph_row->glyphs[TEXT_AREA]
18267 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18268 while (from >= end && to >= toend)
18269 *to-- = *from--;
18272 if (from >= end)
18274 /* Need to free some room before prepending additional
18275 glyphs. */
18276 int move_by = from - end + 1;
18277 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18278 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18280 for ( ; g >= g0; g--)
18281 g[move_by] = *g;
18282 while (from >= end)
18283 *to-- = *from--;
18284 it->glyph_row->used[TEXT_AREA] += move_by;
18289 /* Compute the hash code for ROW. */
18290 unsigned
18291 row_hash (struct glyph_row *row)
18293 int area, k;
18294 unsigned hashval = 0;
18296 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18297 for (k = 0; k < row->used[area]; ++k)
18298 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18299 + row->glyphs[area][k].u.val
18300 + row->glyphs[area][k].face_id
18301 + row->glyphs[area][k].padding_p
18302 + (row->glyphs[area][k].type << 2));
18304 return hashval;
18307 /* Compute the pixel height and width of IT->glyph_row.
18309 Most of the time, ascent and height of a display line will be equal
18310 to the max_ascent and max_height values of the display iterator
18311 structure. This is not the case if
18313 1. We hit ZV without displaying anything. In this case, max_ascent
18314 and max_height will be zero.
18316 2. We have some glyphs that don't contribute to the line height.
18317 (The glyph row flag contributes_to_line_height_p is for future
18318 pixmap extensions).
18320 The first case is easily covered by using default values because in
18321 these cases, the line height does not really matter, except that it
18322 must not be zero. */
18324 static void
18325 compute_line_metrics (struct it *it)
18327 struct glyph_row *row = it->glyph_row;
18329 if (FRAME_WINDOW_P (it->f))
18331 int i, min_y, max_y;
18333 /* The line may consist of one space only, that was added to
18334 place the cursor on it. If so, the row's height hasn't been
18335 computed yet. */
18336 if (row->height == 0)
18338 if (it->max_ascent + it->max_descent == 0)
18339 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18340 row->ascent = it->max_ascent;
18341 row->height = it->max_ascent + it->max_descent;
18342 row->phys_ascent = it->max_phys_ascent;
18343 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18344 row->extra_line_spacing = it->max_extra_line_spacing;
18347 /* Compute the width of this line. */
18348 row->pixel_width = row->x;
18349 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18350 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18352 eassert (row->pixel_width >= 0);
18353 eassert (row->ascent >= 0 && row->height > 0);
18355 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18356 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18358 /* If first line's physical ascent is larger than its logical
18359 ascent, use the physical ascent, and make the row taller.
18360 This makes accented characters fully visible. */
18361 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18362 && row->phys_ascent > row->ascent)
18364 row->height += row->phys_ascent - row->ascent;
18365 row->ascent = row->phys_ascent;
18368 /* Compute how much of the line is visible. */
18369 row->visible_height = row->height;
18371 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18372 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18374 if (row->y < min_y)
18375 row->visible_height -= min_y - row->y;
18376 if (row->y + row->height > max_y)
18377 row->visible_height -= row->y + row->height - max_y;
18379 else
18381 row->pixel_width = row->used[TEXT_AREA];
18382 if (row->continued_p)
18383 row->pixel_width -= it->continuation_pixel_width;
18384 else if (row->truncated_on_right_p)
18385 row->pixel_width -= it->truncation_pixel_width;
18386 row->ascent = row->phys_ascent = 0;
18387 row->height = row->phys_height = row->visible_height = 1;
18388 row->extra_line_spacing = 0;
18391 /* Compute a hash code for this row. */
18392 row->hash = row_hash (row);
18394 it->max_ascent = it->max_descent = 0;
18395 it->max_phys_ascent = it->max_phys_descent = 0;
18399 /* Append one space to the glyph row of iterator IT if doing a
18400 window-based redisplay. The space has the same face as
18401 IT->face_id. Value is non-zero if a space was added.
18403 This function is called to make sure that there is always one glyph
18404 at the end of a glyph row that the cursor can be set on under
18405 window-systems. (If there weren't such a glyph we would not know
18406 how wide and tall a box cursor should be displayed).
18408 At the same time this space let's a nicely handle clearing to the
18409 end of the line if the row ends in italic text. */
18411 static int
18412 append_space_for_newline (struct it *it, int default_face_p)
18414 if (FRAME_WINDOW_P (it->f))
18416 int n = it->glyph_row->used[TEXT_AREA];
18418 if (it->glyph_row->glyphs[TEXT_AREA] + n
18419 < it->glyph_row->glyphs[1 + TEXT_AREA])
18421 /* Save some values that must not be changed.
18422 Must save IT->c and IT->len because otherwise
18423 ITERATOR_AT_END_P wouldn't work anymore after
18424 append_space_for_newline has been called. */
18425 enum display_element_type saved_what = it->what;
18426 int saved_c = it->c, saved_len = it->len;
18427 int saved_char_to_display = it->char_to_display;
18428 int saved_x = it->current_x;
18429 int saved_face_id = it->face_id;
18430 int saved_box_end = it->end_of_box_run_p;
18431 struct text_pos saved_pos;
18432 Lisp_Object saved_object;
18433 struct face *face;
18435 saved_object = it->object;
18436 saved_pos = it->position;
18438 it->what = IT_CHARACTER;
18439 memset (&it->position, 0, sizeof it->position);
18440 it->object = make_number (0);
18441 it->c = it->char_to_display = ' ';
18442 it->len = 1;
18444 /* If the default face was remapped, be sure to use the
18445 remapped face for the appended newline. */
18446 if (default_face_p)
18447 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18448 else if (it->face_before_selective_p)
18449 it->face_id = it->saved_face_id;
18450 face = FACE_FROM_ID (it->f, it->face_id);
18451 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18452 /* In R2L rows, we will prepend a stretch glyph that will
18453 have the end_of_box_run_p flag set for it, so there's no
18454 need for the appended newline glyph to have that flag
18455 set. */
18456 if (it->glyph_row->reversed_p
18457 /* But if the appended newline glyph goes all the way to
18458 the end of the row, there will be no stretch glyph,
18459 so leave the box flag set. */
18460 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18461 it->end_of_box_run_p = 0;
18463 PRODUCE_GLYPHS (it);
18465 it->override_ascent = -1;
18466 it->constrain_row_ascent_descent_p = 0;
18467 it->current_x = saved_x;
18468 it->object = saved_object;
18469 it->position = saved_pos;
18470 it->what = saved_what;
18471 it->face_id = saved_face_id;
18472 it->len = saved_len;
18473 it->c = saved_c;
18474 it->char_to_display = saved_char_to_display;
18475 it->end_of_box_run_p = saved_box_end;
18476 return 1;
18480 return 0;
18484 /* Extend the face of the last glyph in the text area of IT->glyph_row
18485 to the end of the display line. Called from display_line. If the
18486 glyph row is empty, add a space glyph to it so that we know the
18487 face to draw. Set the glyph row flag fill_line_p. If the glyph
18488 row is R2L, prepend a stretch glyph to cover the empty space to the
18489 left of the leftmost glyph. */
18491 static void
18492 extend_face_to_end_of_line (struct it *it)
18494 struct face *face, *default_face;
18495 struct frame *f = it->f;
18497 /* If line is already filled, do nothing. Non window-system frames
18498 get a grace of one more ``pixel'' because their characters are
18499 1-``pixel'' wide, so they hit the equality too early. This grace
18500 is needed only for R2L rows that are not continued, to produce
18501 one extra blank where we could display the cursor. */
18502 if (it->current_x >= it->last_visible_x
18503 + (!FRAME_WINDOW_P (f)
18504 && it->glyph_row->reversed_p
18505 && !it->glyph_row->continued_p))
18506 return;
18508 /* The default face, possibly remapped. */
18509 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18511 /* Face extension extends the background and box of IT->face_id
18512 to the end of the line. If the background equals the background
18513 of the frame, we don't have to do anything. */
18514 if (it->face_before_selective_p)
18515 face = FACE_FROM_ID (f, it->saved_face_id);
18516 else
18517 face = FACE_FROM_ID (f, it->face_id);
18519 if (FRAME_WINDOW_P (f)
18520 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18521 && face->box == FACE_NO_BOX
18522 && face->background == FRAME_BACKGROUND_PIXEL (f)
18523 #ifdef HAVE_WINDOW_SYSTEM
18524 && !face->stipple
18525 #endif
18526 && !it->glyph_row->reversed_p)
18527 return;
18529 /* Set the glyph row flag indicating that the face of the last glyph
18530 in the text area has to be drawn to the end of the text area. */
18531 it->glyph_row->fill_line_p = 1;
18533 /* If current character of IT is not ASCII, make sure we have the
18534 ASCII face. This will be automatically undone the next time
18535 get_next_display_element returns a multibyte character. Note
18536 that the character will always be single byte in unibyte
18537 text. */
18538 if (!ASCII_CHAR_P (it->c))
18540 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18543 if (FRAME_WINDOW_P (f))
18545 /* If the row is empty, add a space with the current face of IT,
18546 so that we know which face to draw. */
18547 if (it->glyph_row->used[TEXT_AREA] == 0)
18549 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18550 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18551 it->glyph_row->used[TEXT_AREA] = 1;
18553 #ifdef HAVE_WINDOW_SYSTEM
18554 if (it->glyph_row->reversed_p)
18556 /* Prepend a stretch glyph to the row, such that the
18557 rightmost glyph will be drawn flushed all the way to the
18558 right margin of the window. The stretch glyph that will
18559 occupy the empty space, if any, to the left of the
18560 glyphs. */
18561 struct font *font = face->font ? face->font : FRAME_FONT (f);
18562 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18563 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18564 struct glyph *g;
18565 int row_width, stretch_ascent, stretch_width;
18566 struct text_pos saved_pos;
18567 int saved_face_id, saved_avoid_cursor, saved_box_start;
18569 for (row_width = 0, g = row_start; g < row_end; g++)
18570 row_width += g->pixel_width;
18571 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18572 if (stretch_width > 0)
18574 stretch_ascent =
18575 (((it->ascent + it->descent)
18576 * FONT_BASE (font)) / FONT_HEIGHT (font));
18577 saved_pos = it->position;
18578 memset (&it->position, 0, sizeof it->position);
18579 saved_avoid_cursor = it->avoid_cursor_p;
18580 it->avoid_cursor_p = 1;
18581 saved_face_id = it->face_id;
18582 saved_box_start = it->start_of_box_run_p;
18583 /* The last row's stretch glyph should get the default
18584 face, to avoid painting the rest of the window with
18585 the region face, if the region ends at ZV. */
18586 if (it->glyph_row->ends_at_zv_p)
18587 it->face_id = default_face->id;
18588 else
18589 it->face_id = face->id;
18590 it->start_of_box_run_p = 0;
18591 append_stretch_glyph (it, make_number (0), stretch_width,
18592 it->ascent + it->descent, stretch_ascent);
18593 it->position = saved_pos;
18594 it->avoid_cursor_p = saved_avoid_cursor;
18595 it->face_id = saved_face_id;
18596 it->start_of_box_run_p = saved_box_start;
18599 #endif /* HAVE_WINDOW_SYSTEM */
18601 else
18603 /* Save some values that must not be changed. */
18604 int saved_x = it->current_x;
18605 struct text_pos saved_pos;
18606 Lisp_Object saved_object;
18607 enum display_element_type saved_what = it->what;
18608 int saved_face_id = it->face_id;
18610 saved_object = it->object;
18611 saved_pos = it->position;
18613 it->what = IT_CHARACTER;
18614 memset (&it->position, 0, sizeof it->position);
18615 it->object = make_number (0);
18616 it->c = it->char_to_display = ' ';
18617 it->len = 1;
18618 /* The last row's blank glyphs should get the default face, to
18619 avoid painting the rest of the window with the region face,
18620 if the region ends at ZV. */
18621 if (it->glyph_row->ends_at_zv_p)
18622 it->face_id = default_face->id;
18623 else
18624 it->face_id = face->id;
18626 PRODUCE_GLYPHS (it);
18628 while (it->current_x <= it->last_visible_x)
18629 PRODUCE_GLYPHS (it);
18631 /* Don't count these blanks really. It would let us insert a left
18632 truncation glyph below and make us set the cursor on them, maybe. */
18633 it->current_x = saved_x;
18634 it->object = saved_object;
18635 it->position = saved_pos;
18636 it->what = saved_what;
18637 it->face_id = saved_face_id;
18642 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18643 trailing whitespace. */
18645 static int
18646 trailing_whitespace_p (ptrdiff_t charpos)
18648 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18649 int c = 0;
18651 while (bytepos < ZV_BYTE
18652 && (c = FETCH_CHAR (bytepos),
18653 c == ' ' || c == '\t'))
18654 ++bytepos;
18656 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18658 if (bytepos != PT_BYTE)
18659 return 1;
18661 return 0;
18665 /* Highlight trailing whitespace, if any, in ROW. */
18667 static void
18668 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18670 int used = row->used[TEXT_AREA];
18672 if (used)
18674 struct glyph *start = row->glyphs[TEXT_AREA];
18675 struct glyph *glyph = start + used - 1;
18677 if (row->reversed_p)
18679 /* Right-to-left rows need to be processed in the opposite
18680 direction, so swap the edge pointers. */
18681 glyph = start;
18682 start = row->glyphs[TEXT_AREA] + used - 1;
18685 /* Skip over glyphs inserted to display the cursor at the
18686 end of a line, for extending the face of the last glyph
18687 to the end of the line on terminals, and for truncation
18688 and continuation glyphs. */
18689 if (!row->reversed_p)
18691 while (glyph >= start
18692 && glyph->type == CHAR_GLYPH
18693 && INTEGERP (glyph->object))
18694 --glyph;
18696 else
18698 while (glyph <= start
18699 && glyph->type == CHAR_GLYPH
18700 && INTEGERP (glyph->object))
18701 ++glyph;
18704 /* If last glyph is a space or stretch, and it's trailing
18705 whitespace, set the face of all trailing whitespace glyphs in
18706 IT->glyph_row to `trailing-whitespace'. */
18707 if ((row->reversed_p ? glyph <= start : glyph >= start)
18708 && BUFFERP (glyph->object)
18709 && (glyph->type == STRETCH_GLYPH
18710 || (glyph->type == CHAR_GLYPH
18711 && glyph->u.ch == ' '))
18712 && trailing_whitespace_p (glyph->charpos))
18714 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18715 if (face_id < 0)
18716 return;
18718 if (!row->reversed_p)
18720 while (glyph >= start
18721 && BUFFERP (glyph->object)
18722 && (glyph->type == STRETCH_GLYPH
18723 || (glyph->type == CHAR_GLYPH
18724 && glyph->u.ch == ' ')))
18725 (glyph--)->face_id = face_id;
18727 else
18729 while (glyph <= start
18730 && BUFFERP (glyph->object)
18731 && (glyph->type == STRETCH_GLYPH
18732 || (glyph->type == CHAR_GLYPH
18733 && glyph->u.ch == ' ')))
18734 (glyph++)->face_id = face_id;
18741 /* Value is non-zero if glyph row ROW should be
18742 considered to hold the buffer position CHARPOS. */
18744 static int
18745 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18747 int result = 1;
18749 if (charpos == CHARPOS (row->end.pos)
18750 || charpos == MATRIX_ROW_END_CHARPOS (row))
18752 /* Suppose the row ends on a string.
18753 Unless the row is continued, that means it ends on a newline
18754 in the string. If it's anything other than a display string
18755 (e.g., a before-string from an overlay), we don't want the
18756 cursor there. (This heuristic seems to give the optimal
18757 behavior for the various types of multi-line strings.)
18758 One exception: if the string has `cursor' property on one of
18759 its characters, we _do_ want the cursor there. */
18760 if (CHARPOS (row->end.string_pos) >= 0)
18762 if (row->continued_p)
18763 result = 1;
18764 else
18766 /* Check for `display' property. */
18767 struct glyph *beg = row->glyphs[TEXT_AREA];
18768 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18769 struct glyph *glyph;
18771 result = 0;
18772 for (glyph = end; glyph >= beg; --glyph)
18773 if (STRINGP (glyph->object))
18775 Lisp_Object prop
18776 = Fget_char_property (make_number (charpos),
18777 Qdisplay, Qnil);
18778 result =
18779 (!NILP (prop)
18780 && display_prop_string_p (prop, glyph->object));
18781 /* If there's a `cursor' property on one of the
18782 string's characters, this row is a cursor row,
18783 even though this is not a display string. */
18784 if (!result)
18786 Lisp_Object s = glyph->object;
18788 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18790 ptrdiff_t gpos = glyph->charpos;
18792 if (!NILP (Fget_char_property (make_number (gpos),
18793 Qcursor, s)))
18795 result = 1;
18796 break;
18800 break;
18804 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18806 /* If the row ends in middle of a real character,
18807 and the line is continued, we want the cursor here.
18808 That's because CHARPOS (ROW->end.pos) would equal
18809 PT if PT is before the character. */
18810 if (!row->ends_in_ellipsis_p)
18811 result = row->continued_p;
18812 else
18813 /* If the row ends in an ellipsis, then
18814 CHARPOS (ROW->end.pos) will equal point after the
18815 invisible text. We want that position to be displayed
18816 after the ellipsis. */
18817 result = 0;
18819 /* If the row ends at ZV, display the cursor at the end of that
18820 row instead of at the start of the row below. */
18821 else if (row->ends_at_zv_p)
18822 result = 1;
18823 else
18824 result = 0;
18827 return result;
18830 /* Value is non-zero if glyph row ROW should be
18831 used to hold the cursor. */
18833 static int
18834 cursor_row_p (struct glyph_row *row)
18836 return row_for_charpos_p (row, PT);
18841 /* Push the property PROP so that it will be rendered at the current
18842 position in IT. Return 1 if PROP was successfully pushed, 0
18843 otherwise. Called from handle_line_prefix to handle the
18844 `line-prefix' and `wrap-prefix' properties. */
18846 static int
18847 push_prefix_prop (struct it *it, Lisp_Object prop)
18849 struct text_pos pos =
18850 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18852 eassert (it->method == GET_FROM_BUFFER
18853 || it->method == GET_FROM_DISPLAY_VECTOR
18854 || it->method == GET_FROM_STRING);
18856 /* We need to save the current buffer/string position, so it will be
18857 restored by pop_it, because iterate_out_of_display_property
18858 depends on that being set correctly, but some situations leave
18859 it->position not yet set when this function is called. */
18860 push_it (it, &pos);
18862 if (STRINGP (prop))
18864 if (SCHARS (prop) == 0)
18866 pop_it (it);
18867 return 0;
18870 it->string = prop;
18871 it->string_from_prefix_prop_p = 1;
18872 it->multibyte_p = STRING_MULTIBYTE (it->string);
18873 it->current.overlay_string_index = -1;
18874 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18875 it->end_charpos = it->string_nchars = SCHARS (it->string);
18876 it->method = GET_FROM_STRING;
18877 it->stop_charpos = 0;
18878 it->prev_stop = 0;
18879 it->base_level_stop = 0;
18881 /* Force paragraph direction to be that of the parent
18882 buffer/string. */
18883 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18884 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18885 else
18886 it->paragraph_embedding = L2R;
18888 /* Set up the bidi iterator for this display string. */
18889 if (it->bidi_p)
18891 it->bidi_it.string.lstring = it->string;
18892 it->bidi_it.string.s = NULL;
18893 it->bidi_it.string.schars = it->end_charpos;
18894 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18895 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18896 it->bidi_it.string.unibyte = !it->multibyte_p;
18897 it->bidi_it.w = it->w;
18898 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18901 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18903 it->method = GET_FROM_STRETCH;
18904 it->object = prop;
18906 #ifdef HAVE_WINDOW_SYSTEM
18907 else if (IMAGEP (prop))
18909 it->what = IT_IMAGE;
18910 it->image_id = lookup_image (it->f, prop);
18911 it->method = GET_FROM_IMAGE;
18913 #endif /* HAVE_WINDOW_SYSTEM */
18914 else
18916 pop_it (it); /* bogus display property, give up */
18917 return 0;
18920 return 1;
18923 /* Return the character-property PROP at the current position in IT. */
18925 static Lisp_Object
18926 get_it_property (struct it *it, Lisp_Object prop)
18928 Lisp_Object position, object = it->object;
18930 if (STRINGP (object))
18931 position = make_number (IT_STRING_CHARPOS (*it));
18932 else if (BUFFERP (object))
18934 position = make_number (IT_CHARPOS (*it));
18935 object = it->window;
18937 else
18938 return Qnil;
18940 return Fget_char_property (position, prop, object);
18943 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18945 static void
18946 handle_line_prefix (struct it *it)
18948 Lisp_Object prefix;
18950 if (it->continuation_lines_width > 0)
18952 prefix = get_it_property (it, Qwrap_prefix);
18953 if (NILP (prefix))
18954 prefix = Vwrap_prefix;
18956 else
18958 prefix = get_it_property (it, Qline_prefix);
18959 if (NILP (prefix))
18960 prefix = Vline_prefix;
18962 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18964 /* If the prefix is wider than the window, and we try to wrap
18965 it, it would acquire its own wrap prefix, and so on till the
18966 iterator stack overflows. So, don't wrap the prefix. */
18967 it->line_wrap = TRUNCATE;
18968 it->avoid_cursor_p = 1;
18974 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18975 only for R2L lines from display_line and display_string, when they
18976 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18977 the line/string needs to be continued on the next glyph row. */
18978 static void
18979 unproduce_glyphs (struct it *it, int n)
18981 struct glyph *glyph, *end;
18983 eassert (it->glyph_row);
18984 eassert (it->glyph_row->reversed_p);
18985 eassert (it->area == TEXT_AREA);
18986 eassert (n <= it->glyph_row->used[TEXT_AREA]);
18988 if (n > it->glyph_row->used[TEXT_AREA])
18989 n = it->glyph_row->used[TEXT_AREA];
18990 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18991 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18992 for ( ; glyph < end; glyph++)
18993 glyph[-n] = *glyph;
18996 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18997 and ROW->maxpos. */
18998 static void
18999 find_row_edges (struct it *it, struct glyph_row *row,
19000 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19001 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19003 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19004 lines' rows is implemented for bidi-reordered rows. */
19006 /* ROW->minpos is the value of min_pos, the minimal buffer position
19007 we have in ROW, or ROW->start.pos if that is smaller. */
19008 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19009 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19010 else
19011 /* We didn't find buffer positions smaller than ROW->start, or
19012 didn't find _any_ valid buffer positions in any of the glyphs,
19013 so we must trust the iterator's computed positions. */
19014 row->minpos = row->start.pos;
19015 if (max_pos <= 0)
19017 max_pos = CHARPOS (it->current.pos);
19018 max_bpos = BYTEPOS (it->current.pos);
19021 /* Here are the various use-cases for ending the row, and the
19022 corresponding values for ROW->maxpos:
19024 Line ends in a newline from buffer eol_pos + 1
19025 Line is continued from buffer max_pos + 1
19026 Line is truncated on right it->current.pos
19027 Line ends in a newline from string max_pos + 1(*)
19028 (*) + 1 only when line ends in a forward scan
19029 Line is continued from string max_pos
19030 Line is continued from display vector max_pos
19031 Line is entirely from a string min_pos == max_pos
19032 Line is entirely from a display vector min_pos == max_pos
19033 Line that ends at ZV ZV
19035 If you discover other use-cases, please add them here as
19036 appropriate. */
19037 if (row->ends_at_zv_p)
19038 row->maxpos = it->current.pos;
19039 else if (row->used[TEXT_AREA])
19041 int seen_this_string = 0;
19042 struct glyph_row *r1 = row - 1;
19044 /* Did we see the same display string on the previous row? */
19045 if (STRINGP (it->object)
19046 /* this is not the first row */
19047 && row > it->w->desired_matrix->rows
19048 /* previous row is not the header line */
19049 && !r1->mode_line_p
19050 /* previous row also ends in a newline from a string */
19051 && r1->ends_in_newline_from_string_p)
19053 struct glyph *start, *end;
19055 /* Search for the last glyph of the previous row that came
19056 from buffer or string. Depending on whether the row is
19057 L2R or R2L, we need to process it front to back or the
19058 other way round. */
19059 if (!r1->reversed_p)
19061 start = r1->glyphs[TEXT_AREA];
19062 end = start + r1->used[TEXT_AREA];
19063 /* Glyphs inserted by redisplay have an integer (zero)
19064 as their object. */
19065 while (end > start
19066 && INTEGERP ((end - 1)->object)
19067 && (end - 1)->charpos <= 0)
19068 --end;
19069 if (end > start)
19071 if (EQ ((end - 1)->object, it->object))
19072 seen_this_string = 1;
19074 else
19075 /* If all the glyphs of the previous row were inserted
19076 by redisplay, it means the previous row was
19077 produced from a single newline, which is only
19078 possible if that newline came from the same string
19079 as the one which produced this ROW. */
19080 seen_this_string = 1;
19082 else
19084 end = r1->glyphs[TEXT_AREA] - 1;
19085 start = end + r1->used[TEXT_AREA];
19086 while (end < start
19087 && INTEGERP ((end + 1)->object)
19088 && (end + 1)->charpos <= 0)
19089 ++end;
19090 if (end < start)
19092 if (EQ ((end + 1)->object, it->object))
19093 seen_this_string = 1;
19095 else
19096 seen_this_string = 1;
19099 /* Take note of each display string that covers a newline only
19100 once, the first time we see it. This is for when a display
19101 string includes more than one newline in it. */
19102 if (row->ends_in_newline_from_string_p && !seen_this_string)
19104 /* If we were scanning the buffer forward when we displayed
19105 the string, we want to account for at least one buffer
19106 position that belongs to this row (position covered by
19107 the display string), so that cursor positioning will
19108 consider this row as a candidate when point is at the end
19109 of the visual line represented by this row. This is not
19110 required when scanning back, because max_pos will already
19111 have a much larger value. */
19112 if (CHARPOS (row->end.pos) > max_pos)
19113 INC_BOTH (max_pos, max_bpos);
19114 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19116 else if (CHARPOS (it->eol_pos) > 0)
19117 SET_TEXT_POS (row->maxpos,
19118 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19119 else if (row->continued_p)
19121 /* If max_pos is different from IT's current position, it
19122 means IT->method does not belong to the display element
19123 at max_pos. However, it also means that the display
19124 element at max_pos was displayed in its entirety on this
19125 line, which is equivalent to saying that the next line
19126 starts at the next buffer position. */
19127 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19128 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19129 else
19131 INC_BOTH (max_pos, max_bpos);
19132 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19135 else if (row->truncated_on_right_p)
19136 /* display_line already called reseat_at_next_visible_line_start,
19137 which puts the iterator at the beginning of the next line, in
19138 the logical order. */
19139 row->maxpos = it->current.pos;
19140 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19141 /* A line that is entirely from a string/image/stretch... */
19142 row->maxpos = row->minpos;
19143 else
19144 emacs_abort ();
19146 else
19147 row->maxpos = it->current.pos;
19150 /* Construct the glyph row IT->glyph_row in the desired matrix of
19151 IT->w from text at the current position of IT. See dispextern.h
19152 for an overview of struct it. Value is non-zero if
19153 IT->glyph_row displays text, as opposed to a line displaying ZV
19154 only. */
19156 static int
19157 display_line (struct it *it)
19159 struct glyph_row *row = it->glyph_row;
19160 Lisp_Object overlay_arrow_string;
19161 struct it wrap_it;
19162 void *wrap_data = NULL;
19163 int may_wrap = 0, wrap_x IF_LINT (= 0);
19164 int wrap_row_used = -1;
19165 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19166 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19167 int wrap_row_extra_line_spacing IF_LINT (= 0);
19168 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19169 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19170 int cvpos;
19171 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19172 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19174 /* We always start displaying at hpos zero even if hscrolled. */
19175 eassert (it->hpos == 0 && it->current_x == 0);
19177 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19178 >= it->w->desired_matrix->nrows)
19180 it->w->nrows_scale_factor++;
19181 it->f->fonts_changed = 1;
19182 return 0;
19185 /* Clear the result glyph row and enable it. */
19186 prepare_desired_row (row);
19188 row->y = it->current_y;
19189 row->start = it->start;
19190 row->continuation_lines_width = it->continuation_lines_width;
19191 row->displays_text_p = 1;
19192 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19193 it->starts_in_middle_of_char_p = 0;
19195 /* Arrange the overlays nicely for our purposes. Usually, we call
19196 display_line on only one line at a time, in which case this
19197 can't really hurt too much, or we call it on lines which appear
19198 one after another in the buffer, in which case all calls to
19199 recenter_overlay_lists but the first will be pretty cheap. */
19200 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19202 /* Move over display elements that are not visible because we are
19203 hscrolled. This may stop at an x-position < IT->first_visible_x
19204 if the first glyph is partially visible or if we hit a line end. */
19205 if (it->current_x < it->first_visible_x)
19207 enum move_it_result move_result;
19209 this_line_min_pos = row->start.pos;
19210 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19211 MOVE_TO_POS | MOVE_TO_X);
19212 /* If we are under a large hscroll, move_it_in_display_line_to
19213 could hit the end of the line without reaching
19214 it->first_visible_x. Pretend that we did reach it. This is
19215 especially important on a TTY, where we will call
19216 extend_face_to_end_of_line, which needs to know how many
19217 blank glyphs to produce. */
19218 if (it->current_x < it->first_visible_x
19219 && (move_result == MOVE_NEWLINE_OR_CR
19220 || move_result == MOVE_POS_MATCH_OR_ZV))
19221 it->current_x = it->first_visible_x;
19223 /* Record the smallest positions seen while we moved over
19224 display elements that are not visible. This is needed by
19225 redisplay_internal for optimizing the case where the cursor
19226 stays inside the same line. The rest of this function only
19227 considers positions that are actually displayed, so
19228 RECORD_MAX_MIN_POS will not otherwise record positions that
19229 are hscrolled to the left of the left edge of the window. */
19230 min_pos = CHARPOS (this_line_min_pos);
19231 min_bpos = BYTEPOS (this_line_min_pos);
19233 else
19235 /* We only do this when not calling `move_it_in_display_line_to'
19236 above, because move_it_in_display_line_to calls
19237 handle_line_prefix itself. */
19238 handle_line_prefix (it);
19241 /* Get the initial row height. This is either the height of the
19242 text hscrolled, if there is any, or zero. */
19243 row->ascent = it->max_ascent;
19244 row->height = it->max_ascent + it->max_descent;
19245 row->phys_ascent = it->max_phys_ascent;
19246 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19247 row->extra_line_spacing = it->max_extra_line_spacing;
19249 /* Utility macro to record max and min buffer positions seen until now. */
19250 #define RECORD_MAX_MIN_POS(IT) \
19251 do \
19253 int composition_p = !STRINGP ((IT)->string) \
19254 && ((IT)->what == IT_COMPOSITION); \
19255 ptrdiff_t current_pos = \
19256 composition_p ? (IT)->cmp_it.charpos \
19257 : IT_CHARPOS (*(IT)); \
19258 ptrdiff_t current_bpos = \
19259 composition_p ? CHAR_TO_BYTE (current_pos) \
19260 : IT_BYTEPOS (*(IT)); \
19261 if (current_pos < min_pos) \
19263 min_pos = current_pos; \
19264 min_bpos = current_bpos; \
19266 if (IT_CHARPOS (*it) > max_pos) \
19268 max_pos = IT_CHARPOS (*it); \
19269 max_bpos = IT_BYTEPOS (*it); \
19272 while (0)
19274 /* Loop generating characters. The loop is left with IT on the next
19275 character to display. */
19276 while (1)
19278 int n_glyphs_before, hpos_before, x_before;
19279 int x, nglyphs;
19280 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19282 /* Retrieve the next thing to display. Value is zero if end of
19283 buffer reached. */
19284 if (!get_next_display_element (it))
19286 /* Maybe add a space at the end of this line that is used to
19287 display the cursor there under X. Set the charpos of the
19288 first glyph of blank lines not corresponding to any text
19289 to -1. */
19290 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19291 row->exact_window_width_line_p = 1;
19292 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19293 || row->used[TEXT_AREA] == 0)
19295 row->glyphs[TEXT_AREA]->charpos = -1;
19296 row->displays_text_p = 0;
19298 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19299 && (!MINI_WINDOW_P (it->w)
19300 || (minibuf_level && EQ (it->window, minibuf_window))))
19301 row->indicate_empty_line_p = 1;
19304 it->continuation_lines_width = 0;
19305 row->ends_at_zv_p = 1;
19306 /* A row that displays right-to-left text must always have
19307 its last face extended all the way to the end of line,
19308 even if this row ends in ZV, because we still write to
19309 the screen left to right. We also need to extend the
19310 last face if the default face is remapped to some
19311 different face, otherwise the functions that clear
19312 portions of the screen will clear with the default face's
19313 background color. */
19314 if (row->reversed_p
19315 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19316 extend_face_to_end_of_line (it);
19317 break;
19320 /* Now, get the metrics of what we want to display. This also
19321 generates glyphs in `row' (which is IT->glyph_row). */
19322 n_glyphs_before = row->used[TEXT_AREA];
19323 x = it->current_x;
19325 /* Remember the line height so far in case the next element doesn't
19326 fit on the line. */
19327 if (it->line_wrap != TRUNCATE)
19329 ascent = it->max_ascent;
19330 descent = it->max_descent;
19331 phys_ascent = it->max_phys_ascent;
19332 phys_descent = it->max_phys_descent;
19334 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19336 if (IT_DISPLAYING_WHITESPACE (it))
19337 may_wrap = 1;
19338 else if (may_wrap)
19340 SAVE_IT (wrap_it, *it, wrap_data);
19341 wrap_x = x;
19342 wrap_row_used = row->used[TEXT_AREA];
19343 wrap_row_ascent = row->ascent;
19344 wrap_row_height = row->height;
19345 wrap_row_phys_ascent = row->phys_ascent;
19346 wrap_row_phys_height = row->phys_height;
19347 wrap_row_extra_line_spacing = row->extra_line_spacing;
19348 wrap_row_min_pos = min_pos;
19349 wrap_row_min_bpos = min_bpos;
19350 wrap_row_max_pos = max_pos;
19351 wrap_row_max_bpos = max_bpos;
19352 may_wrap = 0;
19357 PRODUCE_GLYPHS (it);
19359 /* If this display element was in marginal areas, continue with
19360 the next one. */
19361 if (it->area != TEXT_AREA)
19363 row->ascent = max (row->ascent, it->max_ascent);
19364 row->height = max (row->height, it->max_ascent + it->max_descent);
19365 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19366 row->phys_height = max (row->phys_height,
19367 it->max_phys_ascent + it->max_phys_descent);
19368 row->extra_line_spacing = max (row->extra_line_spacing,
19369 it->max_extra_line_spacing);
19370 set_iterator_to_next (it, 1);
19371 continue;
19374 /* Does the display element fit on the line? If we truncate
19375 lines, we should draw past the right edge of the window. If
19376 we don't truncate, we want to stop so that we can display the
19377 continuation glyph before the right margin. If lines are
19378 continued, there are two possible strategies for characters
19379 resulting in more than 1 glyph (e.g. tabs): Display as many
19380 glyphs as possible in this line and leave the rest for the
19381 continuation line, or display the whole element in the next
19382 line. Original redisplay did the former, so we do it also. */
19383 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19384 hpos_before = it->hpos;
19385 x_before = x;
19387 if (/* Not a newline. */
19388 nglyphs > 0
19389 /* Glyphs produced fit entirely in the line. */
19390 && it->current_x < it->last_visible_x)
19392 it->hpos += nglyphs;
19393 row->ascent = max (row->ascent, it->max_ascent);
19394 row->height = max (row->height, it->max_ascent + it->max_descent);
19395 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19396 row->phys_height = max (row->phys_height,
19397 it->max_phys_ascent + it->max_phys_descent);
19398 row->extra_line_spacing = max (row->extra_line_spacing,
19399 it->max_extra_line_spacing);
19400 if (it->current_x - it->pixel_width < it->first_visible_x)
19401 row->x = x - it->first_visible_x;
19402 /* Record the maximum and minimum buffer positions seen so
19403 far in glyphs that will be displayed by this row. */
19404 if (it->bidi_p)
19405 RECORD_MAX_MIN_POS (it);
19407 else
19409 int i, new_x;
19410 struct glyph *glyph;
19412 for (i = 0; i < nglyphs; ++i, x = new_x)
19414 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19415 new_x = x + glyph->pixel_width;
19417 if (/* Lines are continued. */
19418 it->line_wrap != TRUNCATE
19419 && (/* Glyph doesn't fit on the line. */
19420 new_x > it->last_visible_x
19421 /* Or it fits exactly on a window system frame. */
19422 || (new_x == it->last_visible_x
19423 && FRAME_WINDOW_P (it->f)
19424 && (row->reversed_p
19425 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19426 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19428 /* End of a continued line. */
19430 if (it->hpos == 0
19431 || (new_x == it->last_visible_x
19432 && FRAME_WINDOW_P (it->f)
19433 && (row->reversed_p
19434 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19435 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19437 /* Current glyph is the only one on the line or
19438 fits exactly on the line. We must continue
19439 the line because we can't draw the cursor
19440 after the glyph. */
19441 row->continued_p = 1;
19442 it->current_x = new_x;
19443 it->continuation_lines_width += new_x;
19444 ++it->hpos;
19445 if (i == nglyphs - 1)
19447 /* If line-wrap is on, check if a previous
19448 wrap point was found. */
19449 if (wrap_row_used > 0
19450 /* Even if there is a previous wrap
19451 point, continue the line here as
19452 usual, if (i) the previous character
19453 was a space or tab AND (ii) the
19454 current character is not. */
19455 && (!may_wrap
19456 || IT_DISPLAYING_WHITESPACE (it)))
19457 goto back_to_wrap;
19459 /* Record the maximum and minimum buffer
19460 positions seen so far in glyphs that will be
19461 displayed by this row. */
19462 if (it->bidi_p)
19463 RECORD_MAX_MIN_POS (it);
19464 set_iterator_to_next (it, 1);
19465 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19467 if (!get_next_display_element (it))
19469 row->exact_window_width_line_p = 1;
19470 it->continuation_lines_width = 0;
19471 row->continued_p = 0;
19472 row->ends_at_zv_p = 1;
19474 else if (ITERATOR_AT_END_OF_LINE_P (it))
19476 row->continued_p = 0;
19477 row->exact_window_width_line_p = 1;
19481 else if (it->bidi_p)
19482 RECORD_MAX_MIN_POS (it);
19484 else if (CHAR_GLYPH_PADDING_P (*glyph)
19485 && !FRAME_WINDOW_P (it->f))
19487 /* A padding glyph that doesn't fit on this line.
19488 This means the whole character doesn't fit
19489 on the line. */
19490 if (row->reversed_p)
19491 unproduce_glyphs (it, row->used[TEXT_AREA]
19492 - n_glyphs_before);
19493 row->used[TEXT_AREA] = n_glyphs_before;
19495 /* Fill the rest of the row with continuation
19496 glyphs like in 20.x. */
19497 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19498 < row->glyphs[1 + TEXT_AREA])
19499 produce_special_glyphs (it, IT_CONTINUATION);
19501 row->continued_p = 1;
19502 it->current_x = x_before;
19503 it->continuation_lines_width += x_before;
19505 /* Restore the height to what it was before the
19506 element not fitting on the line. */
19507 it->max_ascent = ascent;
19508 it->max_descent = descent;
19509 it->max_phys_ascent = phys_ascent;
19510 it->max_phys_descent = phys_descent;
19512 else if (wrap_row_used > 0)
19514 back_to_wrap:
19515 if (row->reversed_p)
19516 unproduce_glyphs (it,
19517 row->used[TEXT_AREA] - wrap_row_used);
19518 RESTORE_IT (it, &wrap_it, wrap_data);
19519 it->continuation_lines_width += wrap_x;
19520 row->used[TEXT_AREA] = wrap_row_used;
19521 row->ascent = wrap_row_ascent;
19522 row->height = wrap_row_height;
19523 row->phys_ascent = wrap_row_phys_ascent;
19524 row->phys_height = wrap_row_phys_height;
19525 row->extra_line_spacing = wrap_row_extra_line_spacing;
19526 min_pos = wrap_row_min_pos;
19527 min_bpos = wrap_row_min_bpos;
19528 max_pos = wrap_row_max_pos;
19529 max_bpos = wrap_row_max_bpos;
19530 row->continued_p = 1;
19531 row->ends_at_zv_p = 0;
19532 row->exact_window_width_line_p = 0;
19533 it->continuation_lines_width += x;
19535 /* Make sure that a non-default face is extended
19536 up to the right margin of the window. */
19537 extend_face_to_end_of_line (it);
19539 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19541 /* A TAB that extends past the right edge of the
19542 window. This produces a single glyph on
19543 window system frames. We leave the glyph in
19544 this row and let it fill the row, but don't
19545 consume the TAB. */
19546 if ((row->reversed_p
19547 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19548 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19549 produce_special_glyphs (it, IT_CONTINUATION);
19550 it->continuation_lines_width += it->last_visible_x;
19551 row->ends_in_middle_of_char_p = 1;
19552 row->continued_p = 1;
19553 glyph->pixel_width = it->last_visible_x - x;
19554 it->starts_in_middle_of_char_p = 1;
19556 else
19558 /* Something other than a TAB that draws past
19559 the right edge of the window. Restore
19560 positions to values before the element. */
19561 if (row->reversed_p)
19562 unproduce_glyphs (it, row->used[TEXT_AREA]
19563 - (n_glyphs_before + i));
19564 row->used[TEXT_AREA] = n_glyphs_before + i;
19566 /* Display continuation glyphs. */
19567 it->current_x = x_before;
19568 it->continuation_lines_width += x;
19569 if (!FRAME_WINDOW_P (it->f)
19570 || (row->reversed_p
19571 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19572 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19573 produce_special_glyphs (it, IT_CONTINUATION);
19574 row->continued_p = 1;
19576 extend_face_to_end_of_line (it);
19578 if (nglyphs > 1 && i > 0)
19580 row->ends_in_middle_of_char_p = 1;
19581 it->starts_in_middle_of_char_p = 1;
19584 /* Restore the height to what it was before the
19585 element not fitting on the line. */
19586 it->max_ascent = ascent;
19587 it->max_descent = descent;
19588 it->max_phys_ascent = phys_ascent;
19589 it->max_phys_descent = phys_descent;
19592 break;
19594 else if (new_x > it->first_visible_x)
19596 /* Increment number of glyphs actually displayed. */
19597 ++it->hpos;
19599 /* Record the maximum and minimum buffer positions
19600 seen so far in glyphs that will be displayed by
19601 this row. */
19602 if (it->bidi_p)
19603 RECORD_MAX_MIN_POS (it);
19605 if (x < it->first_visible_x)
19606 /* Glyph is partially visible, i.e. row starts at
19607 negative X position. */
19608 row->x = x - it->first_visible_x;
19610 else
19612 /* Glyph is completely off the left margin of the
19613 window. This should not happen because of the
19614 move_it_in_display_line at the start of this
19615 function, unless the text display area of the
19616 window is empty. */
19617 eassert (it->first_visible_x <= it->last_visible_x);
19620 /* Even if this display element produced no glyphs at all,
19621 we want to record its position. */
19622 if (it->bidi_p && nglyphs == 0)
19623 RECORD_MAX_MIN_POS (it);
19625 row->ascent = max (row->ascent, it->max_ascent);
19626 row->height = max (row->height, it->max_ascent + it->max_descent);
19627 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19628 row->phys_height = max (row->phys_height,
19629 it->max_phys_ascent + it->max_phys_descent);
19630 row->extra_line_spacing = max (row->extra_line_spacing,
19631 it->max_extra_line_spacing);
19633 /* End of this display line if row is continued. */
19634 if (row->continued_p || row->ends_at_zv_p)
19635 break;
19638 at_end_of_line:
19639 /* Is this a line end? If yes, we're also done, after making
19640 sure that a non-default face is extended up to the right
19641 margin of the window. */
19642 if (ITERATOR_AT_END_OF_LINE_P (it))
19644 int used_before = row->used[TEXT_AREA];
19646 row->ends_in_newline_from_string_p = STRINGP (it->object);
19648 /* Add a space at the end of the line that is used to
19649 display the cursor there. */
19650 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19651 append_space_for_newline (it, 0);
19653 /* Extend the face to the end of the line. */
19654 extend_face_to_end_of_line (it);
19656 /* Make sure we have the position. */
19657 if (used_before == 0)
19658 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19660 /* Record the position of the newline, for use in
19661 find_row_edges. */
19662 it->eol_pos = it->current.pos;
19664 /* Consume the line end. This skips over invisible lines. */
19665 set_iterator_to_next (it, 1);
19666 it->continuation_lines_width = 0;
19667 break;
19670 /* Proceed with next display element. Note that this skips
19671 over lines invisible because of selective display. */
19672 set_iterator_to_next (it, 1);
19674 /* If we truncate lines, we are done when the last displayed
19675 glyphs reach past the right margin of the window. */
19676 if (it->line_wrap == TRUNCATE
19677 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19678 ? (it->current_x >= it->last_visible_x)
19679 : (it->current_x > it->last_visible_x)))
19681 /* Maybe add truncation glyphs. */
19682 if (!FRAME_WINDOW_P (it->f)
19683 || (row->reversed_p
19684 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19685 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19687 int i, n;
19689 if (!row->reversed_p)
19691 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19692 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19693 break;
19695 else
19697 for (i = 0; i < row->used[TEXT_AREA]; i++)
19698 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19699 break;
19700 /* Remove any padding glyphs at the front of ROW, to
19701 make room for the truncation glyphs we will be
19702 adding below. The loop below always inserts at
19703 least one truncation glyph, so also remove the
19704 last glyph added to ROW. */
19705 unproduce_glyphs (it, i + 1);
19706 /* Adjust i for the loop below. */
19707 i = row->used[TEXT_AREA] - (i + 1);
19710 it->current_x = x_before;
19711 if (!FRAME_WINDOW_P (it->f))
19713 for (n = row->used[TEXT_AREA]; i < n; ++i)
19715 row->used[TEXT_AREA] = i;
19716 produce_special_glyphs (it, IT_TRUNCATION);
19719 else
19721 row->used[TEXT_AREA] = i;
19722 produce_special_glyphs (it, IT_TRUNCATION);
19725 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19727 /* Don't truncate if we can overflow newline into fringe. */
19728 if (!get_next_display_element (it))
19730 it->continuation_lines_width = 0;
19731 row->ends_at_zv_p = 1;
19732 row->exact_window_width_line_p = 1;
19733 break;
19735 if (ITERATOR_AT_END_OF_LINE_P (it))
19737 row->exact_window_width_line_p = 1;
19738 goto at_end_of_line;
19740 it->current_x = x_before;
19743 row->truncated_on_right_p = 1;
19744 it->continuation_lines_width = 0;
19745 reseat_at_next_visible_line_start (it, 0);
19746 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19747 it->hpos = hpos_before;
19748 break;
19752 if (wrap_data)
19753 bidi_unshelve_cache (wrap_data, 1);
19755 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19756 at the left window margin. */
19757 if (it->first_visible_x
19758 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19760 if (!FRAME_WINDOW_P (it->f)
19761 || (row->reversed_p
19762 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19763 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19764 insert_left_trunc_glyphs (it);
19765 row->truncated_on_left_p = 1;
19768 /* Remember the position at which this line ends.
19770 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19771 cannot be before the call to find_row_edges below, since that is
19772 where these positions are determined. */
19773 row->end = it->current;
19774 if (!it->bidi_p)
19776 row->minpos = row->start.pos;
19777 row->maxpos = row->end.pos;
19779 else
19781 /* ROW->minpos and ROW->maxpos must be the smallest and
19782 `1 + the largest' buffer positions in ROW. But if ROW was
19783 bidi-reordered, these two positions can be anywhere in the
19784 row, so we must determine them now. */
19785 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19788 /* If the start of this line is the overlay arrow-position, then
19789 mark this glyph row as the one containing the overlay arrow.
19790 This is clearly a mess with variable size fonts. It would be
19791 better to let it be displayed like cursors under X. */
19792 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19793 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19794 !NILP (overlay_arrow_string)))
19796 /* Overlay arrow in window redisplay is a fringe bitmap. */
19797 if (STRINGP (overlay_arrow_string))
19799 struct glyph_row *arrow_row
19800 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19801 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19802 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19803 struct glyph *p = row->glyphs[TEXT_AREA];
19804 struct glyph *p2, *end;
19806 /* Copy the arrow glyphs. */
19807 while (glyph < arrow_end)
19808 *p++ = *glyph++;
19810 /* Throw away padding glyphs. */
19811 p2 = p;
19812 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19813 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19814 ++p2;
19815 if (p2 > p)
19817 while (p2 < end)
19818 *p++ = *p2++;
19819 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19822 else
19824 eassert (INTEGERP (overlay_arrow_string));
19825 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19827 overlay_arrow_seen = 1;
19830 /* Highlight trailing whitespace. */
19831 if (!NILP (Vshow_trailing_whitespace))
19832 highlight_trailing_whitespace (it->f, it->glyph_row);
19834 /* Compute pixel dimensions of this line. */
19835 compute_line_metrics (it);
19837 /* Implementation note: No changes in the glyphs of ROW or in their
19838 faces can be done past this point, because compute_line_metrics
19839 computes ROW's hash value and stores it within the glyph_row
19840 structure. */
19842 /* Record whether this row ends inside an ellipsis. */
19843 row->ends_in_ellipsis_p
19844 = (it->method == GET_FROM_DISPLAY_VECTOR
19845 && it->ellipsis_p);
19847 /* Save fringe bitmaps in this row. */
19848 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19849 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19850 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19851 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19853 it->left_user_fringe_bitmap = 0;
19854 it->left_user_fringe_face_id = 0;
19855 it->right_user_fringe_bitmap = 0;
19856 it->right_user_fringe_face_id = 0;
19858 /* Maybe set the cursor. */
19859 cvpos = it->w->cursor.vpos;
19860 if ((cvpos < 0
19861 /* In bidi-reordered rows, keep checking for proper cursor
19862 position even if one has been found already, because buffer
19863 positions in such rows change non-linearly with ROW->VPOS,
19864 when a line is continued. One exception: when we are at ZV,
19865 display cursor on the first suitable glyph row, since all
19866 the empty rows after that also have their position set to ZV. */
19867 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19868 lines' rows is implemented for bidi-reordered rows. */
19869 || (it->bidi_p
19870 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19871 && PT >= MATRIX_ROW_START_CHARPOS (row)
19872 && PT <= MATRIX_ROW_END_CHARPOS (row)
19873 && cursor_row_p (row))
19874 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19876 /* Prepare for the next line. This line starts horizontally at (X
19877 HPOS) = (0 0). Vertical positions are incremented. As a
19878 convenience for the caller, IT->glyph_row is set to the next
19879 row to be used. */
19880 it->current_x = it->hpos = 0;
19881 it->current_y += row->height;
19882 SET_TEXT_POS (it->eol_pos, 0, 0);
19883 ++it->vpos;
19884 ++it->glyph_row;
19885 /* The next row should by default use the same value of the
19886 reversed_p flag as this one. set_iterator_to_next decides when
19887 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19888 the flag accordingly. */
19889 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19890 it->glyph_row->reversed_p = row->reversed_p;
19891 it->start = row->end;
19892 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19894 #undef RECORD_MAX_MIN_POS
19897 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19898 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19899 doc: /* Return paragraph direction at point in BUFFER.
19900 Value is either `left-to-right' or `right-to-left'.
19901 If BUFFER is omitted or nil, it defaults to the current buffer.
19903 Paragraph direction determines how the text in the paragraph is displayed.
19904 In left-to-right paragraphs, text begins at the left margin of the window
19905 and the reading direction is generally left to right. In right-to-left
19906 paragraphs, text begins at the right margin and is read from right to left.
19908 See also `bidi-paragraph-direction'. */)
19909 (Lisp_Object buffer)
19911 struct buffer *buf = current_buffer;
19912 struct buffer *old = buf;
19914 if (! NILP (buffer))
19916 CHECK_BUFFER (buffer);
19917 buf = XBUFFER (buffer);
19920 if (NILP (BVAR (buf, bidi_display_reordering))
19921 || NILP (BVAR (buf, enable_multibyte_characters))
19922 /* When we are loading loadup.el, the character property tables
19923 needed for bidi iteration are not yet available. */
19924 || !NILP (Vpurify_flag))
19925 return Qleft_to_right;
19926 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19927 return BVAR (buf, bidi_paragraph_direction);
19928 else
19930 /* Determine the direction from buffer text. We could try to
19931 use current_matrix if it is up to date, but this seems fast
19932 enough as it is. */
19933 struct bidi_it itb;
19934 ptrdiff_t pos = BUF_PT (buf);
19935 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19936 int c;
19937 void *itb_data = bidi_shelve_cache ();
19939 set_buffer_temp (buf);
19940 /* bidi_paragraph_init finds the base direction of the paragraph
19941 by searching forward from paragraph start. We need the base
19942 direction of the current or _previous_ paragraph, so we need
19943 to make sure we are within that paragraph. To that end, find
19944 the previous non-empty line. */
19945 if (pos >= ZV && pos > BEGV)
19946 DEC_BOTH (pos, bytepos);
19947 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19948 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19950 while ((c = FETCH_BYTE (bytepos)) == '\n'
19951 || c == ' ' || c == '\t' || c == '\f')
19953 if (bytepos <= BEGV_BYTE)
19954 break;
19955 bytepos--;
19956 pos--;
19958 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19959 bytepos--;
19961 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19962 itb.paragraph_dir = NEUTRAL_DIR;
19963 itb.string.s = NULL;
19964 itb.string.lstring = Qnil;
19965 itb.string.bufpos = 0;
19966 itb.string.unibyte = 0;
19967 /* We have no window to use here for ignoring window-specific
19968 overlays. Using NULL for window pointer will cause
19969 compute_display_string_pos to use the current buffer. */
19970 itb.w = NULL;
19971 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19972 bidi_unshelve_cache (itb_data, 0);
19973 set_buffer_temp (old);
19974 switch (itb.paragraph_dir)
19976 case L2R:
19977 return Qleft_to_right;
19978 break;
19979 case R2L:
19980 return Qright_to_left;
19981 break;
19982 default:
19983 emacs_abort ();
19988 DEFUN ("move-point-visually", Fmove_point_visually,
19989 Smove_point_visually, 1, 1, 0,
19990 doc: /* Move point in the visual order in the specified DIRECTION.
19991 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
19992 left.
19994 Value is the new character position of point. */)
19995 (Lisp_Object direction)
19997 struct window *w = XWINDOW (selected_window);
19998 struct buffer *b = XBUFFER (w->contents);
19999 struct glyph_row *row;
20000 int dir;
20001 Lisp_Object paragraph_dir;
20003 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20004 (!(ROW)->continued_p \
20005 && INTEGERP ((GLYPH)->object) \
20006 && (GLYPH)->type == CHAR_GLYPH \
20007 && (GLYPH)->u.ch == ' ' \
20008 && (GLYPH)->charpos >= 0 \
20009 && !(GLYPH)->avoid_cursor_p)
20011 CHECK_NUMBER (direction);
20012 dir = XINT (direction);
20013 if (dir > 0)
20014 dir = 1;
20015 else
20016 dir = -1;
20018 /* If current matrix is up-to-date, we can use the information
20019 recorded in the glyphs, at least as long as the goal is on the
20020 screen. */
20021 if (w->window_end_valid
20022 && !windows_or_buffers_changed
20023 && b
20024 && !b->clip_changed
20025 && !b->prevent_redisplay_optimizations_p
20026 && !window_outdated (w)
20027 && w->cursor.vpos >= 0
20028 && w->cursor.vpos < w->current_matrix->nrows
20029 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20031 struct glyph *g = row->glyphs[TEXT_AREA];
20032 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20033 struct glyph *gpt = g + w->cursor.hpos;
20035 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20037 if (BUFFERP (g->object) && g->charpos != PT)
20039 SET_PT (g->charpos);
20040 w->cursor.vpos = -1;
20041 return make_number (PT);
20043 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20045 ptrdiff_t new_pos;
20047 if (BUFFERP (gpt->object))
20049 new_pos = PT;
20050 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20051 new_pos += (row->reversed_p ? -dir : dir);
20052 else
20053 new_pos -= (row->reversed_p ? -dir : dir);;
20055 else if (BUFFERP (g->object))
20056 new_pos = g->charpos;
20057 else
20058 break;
20059 SET_PT (new_pos);
20060 w->cursor.vpos = -1;
20061 return make_number (PT);
20063 else if (ROW_GLYPH_NEWLINE_P (row, g))
20065 /* Glyphs inserted at the end of a non-empty line for
20066 positioning the cursor have zero charpos, so we must
20067 deduce the value of point by other means. */
20068 if (g->charpos > 0)
20069 SET_PT (g->charpos);
20070 else if (row->ends_at_zv_p && PT != ZV)
20071 SET_PT (ZV);
20072 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20073 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20074 else
20075 break;
20076 w->cursor.vpos = -1;
20077 return make_number (PT);
20080 if (g == e || INTEGERP (g->object))
20082 if (row->truncated_on_left_p || row->truncated_on_right_p)
20083 goto simulate_display;
20084 if (!row->reversed_p)
20085 row += dir;
20086 else
20087 row -= dir;
20088 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20089 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20090 goto simulate_display;
20092 if (dir > 0)
20094 if (row->reversed_p && !row->continued_p)
20096 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20097 w->cursor.vpos = -1;
20098 return make_number (PT);
20100 g = row->glyphs[TEXT_AREA];
20101 e = g + row->used[TEXT_AREA];
20102 for ( ; g < e; g++)
20104 if (BUFFERP (g->object)
20105 /* Empty lines have only one glyph, which stands
20106 for the newline, and whose charpos is the
20107 buffer position of the newline. */
20108 || ROW_GLYPH_NEWLINE_P (row, g)
20109 /* When the buffer ends in a newline, the line at
20110 EOB also has one glyph, but its charpos is -1. */
20111 || (row->ends_at_zv_p
20112 && !row->reversed_p
20113 && INTEGERP (g->object)
20114 && g->type == CHAR_GLYPH
20115 && g->u.ch == ' '))
20117 if (g->charpos > 0)
20118 SET_PT (g->charpos);
20119 else if (!row->reversed_p
20120 && row->ends_at_zv_p
20121 && PT != ZV)
20122 SET_PT (ZV);
20123 else
20124 continue;
20125 w->cursor.vpos = -1;
20126 return make_number (PT);
20130 else
20132 if (!row->reversed_p && !row->continued_p)
20134 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20135 w->cursor.vpos = -1;
20136 return make_number (PT);
20138 e = row->glyphs[TEXT_AREA];
20139 g = e + row->used[TEXT_AREA] - 1;
20140 for ( ; g >= e; g--)
20142 if (BUFFERP (g->object)
20143 || (ROW_GLYPH_NEWLINE_P (row, g)
20144 && g->charpos > 0)
20145 /* Empty R2L lines on GUI frames have the buffer
20146 position of the newline stored in the stretch
20147 glyph. */
20148 || g->type == STRETCH_GLYPH
20149 || (row->ends_at_zv_p
20150 && row->reversed_p
20151 && INTEGERP (g->object)
20152 && g->type == CHAR_GLYPH
20153 && g->u.ch == ' '))
20155 if (g->charpos > 0)
20156 SET_PT (g->charpos);
20157 else if (row->reversed_p
20158 && row->ends_at_zv_p
20159 && PT != ZV)
20160 SET_PT (ZV);
20161 else
20162 continue;
20163 w->cursor.vpos = -1;
20164 return make_number (PT);
20171 simulate_display:
20173 /* If we wind up here, we failed to move by using the glyphs, so we
20174 need to simulate display instead. */
20176 if (b)
20177 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20178 else
20179 paragraph_dir = Qleft_to_right;
20180 if (EQ (paragraph_dir, Qright_to_left))
20181 dir = -dir;
20182 if (PT <= BEGV && dir < 0)
20183 xsignal0 (Qbeginning_of_buffer);
20184 else if (PT >= ZV && dir > 0)
20185 xsignal0 (Qend_of_buffer);
20186 else
20188 struct text_pos pt;
20189 struct it it;
20190 int pt_x, target_x, pixel_width, pt_vpos;
20191 bool at_eol_p;
20192 bool overshoot_expected = false;
20193 bool target_is_eol_p = false;
20195 /* Setup the arena. */
20196 SET_TEXT_POS (pt, PT, PT_BYTE);
20197 start_display (&it, w, pt);
20199 if (it.cmp_it.id < 0
20200 && it.method == GET_FROM_STRING
20201 && it.area == TEXT_AREA
20202 && it.string_from_display_prop_p
20203 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20204 overshoot_expected = true;
20206 /* Find the X coordinate of point. We start from the beginning
20207 of this or previous line to make sure we are before point in
20208 the logical order (since the move_it_* functions can only
20209 move forward). */
20210 reseat_at_previous_visible_line_start (&it);
20211 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20212 if (IT_CHARPOS (it) != PT)
20213 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20214 -1, -1, -1, MOVE_TO_POS);
20215 pt_x = it.current_x;
20216 pt_vpos = it.vpos;
20217 if (dir > 0 || overshoot_expected)
20219 struct glyph_row *row = it.glyph_row;
20221 /* When point is at beginning of line, we don't have
20222 information about the glyph there loaded into struct
20223 it. Calling get_next_display_element fixes that. */
20224 if (pt_x == 0)
20225 get_next_display_element (&it);
20226 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20227 it.glyph_row = NULL;
20228 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20229 it.glyph_row = row;
20230 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20231 it, lest it will become out of sync with it's buffer
20232 position. */
20233 it.current_x = pt_x;
20235 else
20236 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20237 pixel_width = it.pixel_width;
20238 if (overshoot_expected && at_eol_p)
20239 pixel_width = 0;
20240 else if (pixel_width <= 0)
20241 pixel_width = 1;
20243 /* If there's a display string at point, we are actually at the
20244 glyph to the left of point, so we need to correct the X
20245 coordinate. */
20246 if (overshoot_expected)
20247 pt_x += pixel_width;
20249 /* Compute target X coordinate, either to the left or to the
20250 right of point. On TTY frames, all characters have the same
20251 pixel width of 1, so we can use that. On GUI frames we don't
20252 have an easy way of getting at the pixel width of the
20253 character to the left of point, so we use a different method
20254 of getting to that place. */
20255 if (dir > 0)
20256 target_x = pt_x + pixel_width;
20257 else
20258 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20260 /* Target X coordinate could be one line above or below the line
20261 of point, in which case we need to adjust the target X
20262 coordinate. Also, if moving to the left, we need to begin at
20263 the left edge of the point's screen line. */
20264 if (dir < 0)
20266 if (pt_x > 0)
20268 start_display (&it, w, pt);
20269 reseat_at_previous_visible_line_start (&it);
20270 it.current_x = it.current_y = it.hpos = 0;
20271 if (pt_vpos != 0)
20272 move_it_by_lines (&it, pt_vpos);
20274 else
20276 move_it_by_lines (&it, -1);
20277 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20278 target_is_eol_p = true;
20281 else
20283 if (at_eol_p
20284 || (target_x >= it.last_visible_x
20285 && it.line_wrap != TRUNCATE))
20287 if (pt_x > 0)
20288 move_it_by_lines (&it, 0);
20289 move_it_by_lines (&it, 1);
20290 target_x = 0;
20294 /* Move to the target X coordinate. */
20295 #ifdef HAVE_WINDOW_SYSTEM
20296 /* On GUI frames, as we don't know the X coordinate of the
20297 character to the left of point, moving point to the left
20298 requires walking, one grapheme cluster at a time, until we
20299 find ourself at a place immediately to the left of the
20300 character at point. */
20301 if (FRAME_WINDOW_P (it.f) && dir < 0)
20303 struct text_pos new_pos = it.current.pos;
20304 enum move_it_result rc = MOVE_X_REACHED;
20306 while (it.current_x + it.pixel_width <= target_x
20307 && rc == MOVE_X_REACHED)
20309 int new_x = it.current_x + it.pixel_width;
20311 new_pos = it.current.pos;
20312 if (new_x == it.current_x)
20313 new_x++;
20314 rc = move_it_in_display_line_to (&it, ZV, new_x,
20315 MOVE_TO_POS | MOVE_TO_X);
20316 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20317 break;
20319 /* If we ended up on a composed character inside
20320 bidi-reordered text (e.g., Hebrew text with diacritics),
20321 the iterator gives us the buffer position of the last (in
20322 logical order) character of the composed grapheme cluster,
20323 which is not what we want. So we cheat: we compute the
20324 character position of the character that follows (in the
20325 logical order) the one where the above loop stopped. That
20326 character will appear on display to the left of point. */
20327 if (it.bidi_p
20328 && it.bidi_it.scan_dir == -1
20329 && new_pos.charpos - IT_CHARPOS (it) > 1)
20331 new_pos.charpos = IT_CHARPOS (it) + 1;
20332 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20334 it.current.pos = new_pos;
20336 else
20337 #endif
20338 if (it.current_x != target_x)
20339 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20341 /* When lines are truncated, the above loop will stop at the
20342 window edge. But we want to get to the end of line, even if
20343 it is beyond the window edge; automatic hscroll will then
20344 scroll the window to show point as appropriate. */
20345 if (target_is_eol_p && it.line_wrap == TRUNCATE
20346 && get_next_display_element (&it))
20348 struct text_pos new_pos = it.current.pos;
20350 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20352 set_iterator_to_next (&it, 0);
20353 if (it.method == GET_FROM_BUFFER)
20354 new_pos = it.current.pos;
20355 if (!get_next_display_element (&it))
20356 break;
20359 it.current.pos = new_pos;
20362 /* If we ended up in a display string that covers point, move to
20363 buffer position to the right in the visual order. */
20364 if (dir > 0)
20366 while (IT_CHARPOS (it) == PT)
20368 set_iterator_to_next (&it, 0);
20369 if (!get_next_display_element (&it))
20370 break;
20374 /* Move point to that position. */
20375 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20378 return make_number (PT);
20380 #undef ROW_GLYPH_NEWLINE_P
20384 /***********************************************************************
20385 Menu Bar
20386 ***********************************************************************/
20388 /* Redisplay the menu bar in the frame for window W.
20390 The menu bar of X frames that don't have X toolkit support is
20391 displayed in a special window W->frame->menu_bar_window.
20393 The menu bar of terminal frames is treated specially as far as
20394 glyph matrices are concerned. Menu bar lines are not part of
20395 windows, so the update is done directly on the frame matrix rows
20396 for the menu bar. */
20398 static void
20399 display_menu_bar (struct window *w)
20401 struct frame *f = XFRAME (WINDOW_FRAME (w));
20402 struct it it;
20403 Lisp_Object items;
20404 int i;
20406 /* Don't do all this for graphical frames. */
20407 #ifdef HAVE_NTGUI
20408 if (FRAME_W32_P (f))
20409 return;
20410 #endif
20411 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20412 if (FRAME_X_P (f))
20413 return;
20414 #endif
20416 #ifdef HAVE_NS
20417 if (FRAME_NS_P (f))
20418 return;
20419 #endif /* HAVE_NS */
20421 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20422 eassert (!FRAME_WINDOW_P (f));
20423 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20424 it.first_visible_x = 0;
20425 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20426 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20427 if (FRAME_WINDOW_P (f))
20429 /* Menu bar lines are displayed in the desired matrix of the
20430 dummy window menu_bar_window. */
20431 struct window *menu_w;
20432 menu_w = XWINDOW (f->menu_bar_window);
20433 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20434 MENU_FACE_ID);
20435 it.first_visible_x = 0;
20436 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20438 else
20439 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20441 /* This is a TTY frame, i.e. character hpos/vpos are used as
20442 pixel x/y. */
20443 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20444 MENU_FACE_ID);
20445 it.first_visible_x = 0;
20446 it.last_visible_x = FRAME_COLS (f);
20449 /* FIXME: This should be controlled by a user option. See the
20450 comments in redisplay_tool_bar and display_mode_line about
20451 this. */
20452 it.paragraph_embedding = L2R;
20454 /* Clear all rows of the menu bar. */
20455 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20457 struct glyph_row *row = it.glyph_row + i;
20458 clear_glyph_row (row);
20459 row->enabled_p = 1;
20460 row->full_width_p = 1;
20463 /* Display all items of the menu bar. */
20464 items = FRAME_MENU_BAR_ITEMS (it.f);
20465 for (i = 0; i < ASIZE (items); i += 4)
20467 Lisp_Object string;
20469 /* Stop at nil string. */
20470 string = AREF (items, i + 1);
20471 if (NILP (string))
20472 break;
20474 /* Remember where item was displayed. */
20475 ASET (items, i + 3, make_number (it.hpos));
20477 /* Display the item, pad with one space. */
20478 if (it.current_x < it.last_visible_x)
20479 display_string (NULL, string, Qnil, 0, 0, &it,
20480 SCHARS (string) + 1, 0, 0, -1);
20483 /* Fill out the line with spaces. */
20484 if (it.current_x < it.last_visible_x)
20485 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20487 /* Compute the total height of the lines. */
20488 compute_line_metrics (&it);
20491 #ifdef HAVE_MENUS
20492 /* Deep copy of a glyph row, including the glyphs. */
20493 static void
20494 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20496 struct glyph *pointers[1 + LAST_AREA];
20497 int to_used = to->used[TEXT_AREA];
20499 /* Save glyph pointers of TO. */
20500 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20502 /* Do a structure assignment. */
20503 *to = *from;
20505 /* Restore original glyph pointers of TO. */
20506 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20508 /* Copy the glyphs. */
20509 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20510 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20512 /* If we filled only part of the TO row, fill the rest with
20513 space_glyph (which will display as empty space). */
20514 if (to_used > from->used[TEXT_AREA])
20515 fill_up_frame_row_with_spaces (to, to_used);
20518 /* Display one menu item on a TTY, by overwriting the glyphs in the
20519 frame F's desired glyph matrix with glyphs produced from the menu
20520 item text. Called from term.c to display TTY drop-down menus one
20521 item at a time.
20523 ITEM_TEXT is the menu item text as a C string.
20525 FACE_ID is the face ID to be used for this menu item. FACE_ID
20526 could specify one of 3 faces: a face for an enabled item, a face
20527 for a disabled item, or a face for a selected item.
20529 X and Y are coordinates of the first glyph in the frame's desired
20530 matrix to be overwritten by the menu item. Since this is a TTY, Y
20531 is the zero-based number of the glyph row and X is the zero-based
20532 glyph number in the row, starting from left, where to start
20533 displaying the item.
20535 SUBMENU non-zero means this menu item drops down a submenu, which
20536 should be indicated by displaying a proper visual cue after the
20537 item text. */
20539 void
20540 display_tty_menu_item (const char *item_text, int width, int face_id,
20541 int x, int y, int submenu)
20543 struct it it;
20544 struct frame *f = SELECTED_FRAME ();
20545 struct window *w = XWINDOW (f->selected_window);
20546 int saved_used, saved_truncated, saved_width, saved_reversed;
20547 struct glyph_row *row;
20548 size_t item_len = strlen (item_text);
20550 eassert (FRAME_TERMCAP_P (f));
20552 /* Don't write beyond the matrix's last row. This can happen for
20553 TTY screens that are not high enough to show the entire menu.
20554 (This is actually a bit of defensive programming, as
20555 tty_menu_display already limits the number of menu items to one
20556 less than the number of screen lines.) */
20557 if (y >= f->desired_matrix->nrows)
20558 return;
20560 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
20561 it.first_visible_x = 0;
20562 it.last_visible_x = FRAME_COLS (f) - 1;
20563 row = it.glyph_row;
20564 /* Start with the row contents from the current matrix. */
20565 deep_copy_glyph_row (row, f->current_matrix->rows + y);
20566 saved_width = row->full_width_p;
20567 row->full_width_p = 1;
20568 saved_reversed = row->reversed_p;
20569 row->reversed_p = 0;
20570 row->enabled_p = 1;
20572 /* Arrange for the menu item glyphs to start at (X,Y) and have the
20573 desired face. */
20574 eassert (x < f->desired_matrix->matrix_w);
20575 it.current_x = it.hpos = x;
20576 it.current_y = it.vpos = y;
20577 saved_used = row->used[TEXT_AREA];
20578 saved_truncated = row->truncated_on_right_p;
20579 row->used[TEXT_AREA] = x;
20580 it.face_id = face_id;
20581 it.line_wrap = TRUNCATE;
20583 /* FIXME: This should be controlled by a user option. See the
20584 comments in redisplay_tool_bar and display_mode_line about this.
20585 Also, if paragraph_embedding could ever be R2L, changes will be
20586 needed to avoid shifting to the right the row characters in
20587 term.c:append_glyph. */
20588 it.paragraph_embedding = L2R;
20590 /* Pad with a space on the left. */
20591 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
20592 width--;
20593 /* Display the menu item, pad with spaces to WIDTH. */
20594 if (submenu)
20596 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20597 item_len, 0, FRAME_COLS (f) - 1, -1);
20598 width -= item_len;
20599 /* Indicate with " >" that there's a submenu. */
20600 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
20601 FRAME_COLS (f) - 1, -1);
20603 else
20604 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20605 width, 0, FRAME_COLS (f) - 1, -1);
20607 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
20608 row->truncated_on_right_p = saved_truncated;
20609 row->hash = row_hash (row);
20610 row->full_width_p = saved_width;
20611 row->reversed_p = saved_reversed;
20613 #endif /* HAVE_MENUS */
20615 /***********************************************************************
20616 Mode Line
20617 ***********************************************************************/
20619 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20620 FORCE is non-zero, redisplay mode lines unconditionally.
20621 Otherwise, redisplay only mode lines that are garbaged. Value is
20622 the number of windows whose mode lines were redisplayed. */
20624 static int
20625 redisplay_mode_lines (Lisp_Object window, int force)
20627 int nwindows = 0;
20629 while (!NILP (window))
20631 struct window *w = XWINDOW (window);
20633 if (WINDOWP (w->contents))
20634 nwindows += redisplay_mode_lines (w->contents, force);
20635 else if (force
20636 || FRAME_GARBAGED_P (XFRAME (w->frame))
20637 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20639 struct text_pos lpoint;
20640 struct buffer *old = current_buffer;
20642 /* Set the window's buffer for the mode line display. */
20643 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20644 set_buffer_internal_1 (XBUFFER (w->contents));
20646 /* Point refers normally to the selected window. For any
20647 other window, set up appropriate value. */
20648 if (!EQ (window, selected_window))
20650 struct text_pos pt;
20652 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20653 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20656 /* Display mode lines. */
20657 clear_glyph_matrix (w->desired_matrix);
20658 if (display_mode_lines (w))
20660 ++nwindows;
20661 w->must_be_updated_p = 1;
20664 /* Restore old settings. */
20665 set_buffer_internal_1 (old);
20666 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20669 window = w->next;
20672 return nwindows;
20676 /* Display the mode and/or header line of window W. Value is the
20677 sum number of mode lines and header lines displayed. */
20679 static int
20680 display_mode_lines (struct window *w)
20682 Lisp_Object old_selected_window = selected_window;
20683 Lisp_Object old_selected_frame = selected_frame;
20684 Lisp_Object new_frame = w->frame;
20685 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20686 int n = 0;
20688 selected_frame = new_frame;
20689 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20690 or window's point, then we'd need select_window_1 here as well. */
20691 XSETWINDOW (selected_window, w);
20692 XFRAME (new_frame)->selected_window = selected_window;
20694 /* These will be set while the mode line specs are processed. */
20695 line_number_displayed = 0;
20696 w->column_number_displayed = -1;
20698 if (WINDOW_WANTS_MODELINE_P (w))
20700 struct window *sel_w = XWINDOW (old_selected_window);
20702 /* Select mode line face based on the real selected window. */
20703 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20704 BVAR (current_buffer, mode_line_format));
20705 ++n;
20708 if (WINDOW_WANTS_HEADER_LINE_P (w))
20710 display_mode_line (w, HEADER_LINE_FACE_ID,
20711 BVAR (current_buffer, header_line_format));
20712 ++n;
20715 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20716 selected_frame = old_selected_frame;
20717 selected_window = old_selected_window;
20718 return n;
20722 /* Display mode or header line of window W. FACE_ID specifies which
20723 line to display; it is either MODE_LINE_FACE_ID or
20724 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20725 display. Value is the pixel height of the mode/header line
20726 displayed. */
20728 static int
20729 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20731 struct it it;
20732 struct face *face;
20733 ptrdiff_t count = SPECPDL_INDEX ();
20735 init_iterator (&it, w, -1, -1, NULL, face_id);
20736 /* Don't extend on a previously drawn mode-line.
20737 This may happen if called from pos_visible_p. */
20738 it.glyph_row->enabled_p = 0;
20739 prepare_desired_row (it.glyph_row);
20741 it.glyph_row->mode_line_p = 1;
20743 /* FIXME: This should be controlled by a user option. But
20744 supporting such an option is not trivial, since the mode line is
20745 made up of many separate strings. */
20746 it.paragraph_embedding = L2R;
20748 record_unwind_protect (unwind_format_mode_line,
20749 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20751 mode_line_target = MODE_LINE_DISPLAY;
20753 /* Temporarily make frame's keyboard the current kboard so that
20754 kboard-local variables in the mode_line_format will get the right
20755 values. */
20756 push_kboard (FRAME_KBOARD (it.f));
20757 record_unwind_save_match_data ();
20758 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20759 pop_kboard ();
20761 unbind_to (count, Qnil);
20763 /* Fill up with spaces. */
20764 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20766 compute_line_metrics (&it);
20767 it.glyph_row->full_width_p = 1;
20768 it.glyph_row->continued_p = 0;
20769 it.glyph_row->truncated_on_left_p = 0;
20770 it.glyph_row->truncated_on_right_p = 0;
20772 /* Make a 3D mode-line have a shadow at its right end. */
20773 face = FACE_FROM_ID (it.f, face_id);
20774 extend_face_to_end_of_line (&it);
20775 if (face->box != FACE_NO_BOX)
20777 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20778 + it.glyph_row->used[TEXT_AREA] - 1);
20779 last->right_box_line_p = 1;
20782 return it.glyph_row->height;
20785 /* Move element ELT in LIST to the front of LIST.
20786 Return the updated list. */
20788 static Lisp_Object
20789 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20791 register Lisp_Object tail, prev;
20792 register Lisp_Object tem;
20794 tail = list;
20795 prev = Qnil;
20796 while (CONSP (tail))
20798 tem = XCAR (tail);
20800 if (EQ (elt, tem))
20802 /* Splice out the link TAIL. */
20803 if (NILP (prev))
20804 list = XCDR (tail);
20805 else
20806 Fsetcdr (prev, XCDR (tail));
20808 /* Now make it the first. */
20809 Fsetcdr (tail, list);
20810 return tail;
20812 else
20813 prev = tail;
20814 tail = XCDR (tail);
20815 QUIT;
20818 /* Not found--return unchanged LIST. */
20819 return list;
20822 /* Contribute ELT to the mode line for window IT->w. How it
20823 translates into text depends on its data type.
20825 IT describes the display environment in which we display, as usual.
20827 DEPTH is the depth in recursion. It is used to prevent
20828 infinite recursion here.
20830 FIELD_WIDTH is the number of characters the display of ELT should
20831 occupy in the mode line, and PRECISION is the maximum number of
20832 characters to display from ELT's representation. See
20833 display_string for details.
20835 Returns the hpos of the end of the text generated by ELT.
20837 PROPS is a property list to add to any string we encounter.
20839 If RISKY is nonzero, remove (disregard) any properties in any string
20840 we encounter, and ignore :eval and :propertize.
20842 The global variable `mode_line_target' determines whether the
20843 output is passed to `store_mode_line_noprop',
20844 `store_mode_line_string', or `display_string'. */
20846 static int
20847 display_mode_element (struct it *it, int depth, int field_width, int precision,
20848 Lisp_Object elt, Lisp_Object props, int risky)
20850 int n = 0, field, prec;
20851 int literal = 0;
20853 tail_recurse:
20854 if (depth > 100)
20855 elt = build_string ("*too-deep*");
20857 depth++;
20859 switch (XTYPE (elt))
20861 case Lisp_String:
20863 /* A string: output it and check for %-constructs within it. */
20864 unsigned char c;
20865 ptrdiff_t offset = 0;
20867 if (SCHARS (elt) > 0
20868 && (!NILP (props) || risky))
20870 Lisp_Object oprops, aelt;
20871 oprops = Ftext_properties_at (make_number (0), elt);
20873 /* If the starting string's properties are not what
20874 we want, translate the string. Also, if the string
20875 is risky, do that anyway. */
20877 if (NILP (Fequal (props, oprops)) || risky)
20879 /* If the starting string has properties,
20880 merge the specified ones onto the existing ones. */
20881 if (! NILP (oprops) && !risky)
20883 Lisp_Object tem;
20885 oprops = Fcopy_sequence (oprops);
20886 tem = props;
20887 while (CONSP (tem))
20889 oprops = Fplist_put (oprops, XCAR (tem),
20890 XCAR (XCDR (tem)));
20891 tem = XCDR (XCDR (tem));
20893 props = oprops;
20896 aelt = Fassoc (elt, mode_line_proptrans_alist);
20897 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20899 /* AELT is what we want. Move it to the front
20900 without consing. */
20901 elt = XCAR (aelt);
20902 mode_line_proptrans_alist
20903 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20905 else
20907 Lisp_Object tem;
20909 /* If AELT has the wrong props, it is useless.
20910 so get rid of it. */
20911 if (! NILP (aelt))
20912 mode_line_proptrans_alist
20913 = Fdelq (aelt, mode_line_proptrans_alist);
20915 elt = Fcopy_sequence (elt);
20916 Fset_text_properties (make_number (0), Flength (elt),
20917 props, elt);
20918 /* Add this item to mode_line_proptrans_alist. */
20919 mode_line_proptrans_alist
20920 = Fcons (Fcons (elt, props),
20921 mode_line_proptrans_alist);
20922 /* Truncate mode_line_proptrans_alist
20923 to at most 50 elements. */
20924 tem = Fnthcdr (make_number (50),
20925 mode_line_proptrans_alist);
20926 if (! NILP (tem))
20927 XSETCDR (tem, Qnil);
20932 offset = 0;
20934 if (literal)
20936 prec = precision - n;
20937 switch (mode_line_target)
20939 case MODE_LINE_NOPROP:
20940 case MODE_LINE_TITLE:
20941 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20942 break;
20943 case MODE_LINE_STRING:
20944 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20945 break;
20946 case MODE_LINE_DISPLAY:
20947 n += display_string (NULL, elt, Qnil, 0, 0, it,
20948 0, prec, 0, STRING_MULTIBYTE (elt));
20949 break;
20952 break;
20955 /* Handle the non-literal case. */
20957 while ((precision <= 0 || n < precision)
20958 && SREF (elt, offset) != 0
20959 && (mode_line_target != MODE_LINE_DISPLAY
20960 || it->current_x < it->last_visible_x))
20962 ptrdiff_t last_offset = offset;
20964 /* Advance to end of string or next format specifier. */
20965 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20968 if (offset - 1 != last_offset)
20970 ptrdiff_t nchars, nbytes;
20972 /* Output to end of string or up to '%'. Field width
20973 is length of string. Don't output more than
20974 PRECISION allows us. */
20975 offset--;
20977 prec = c_string_width (SDATA (elt) + last_offset,
20978 offset - last_offset, precision - n,
20979 &nchars, &nbytes);
20981 switch (mode_line_target)
20983 case MODE_LINE_NOPROP:
20984 case MODE_LINE_TITLE:
20985 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20986 break;
20987 case MODE_LINE_STRING:
20989 ptrdiff_t bytepos = last_offset;
20990 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20991 ptrdiff_t endpos = (precision <= 0
20992 ? string_byte_to_char (elt, offset)
20993 : charpos + nchars);
20995 n += store_mode_line_string (NULL,
20996 Fsubstring (elt, make_number (charpos),
20997 make_number (endpos)),
20998 0, 0, 0, Qnil);
21000 break;
21001 case MODE_LINE_DISPLAY:
21003 ptrdiff_t bytepos = last_offset;
21004 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21006 if (precision <= 0)
21007 nchars = string_byte_to_char (elt, offset) - charpos;
21008 n += display_string (NULL, elt, Qnil, 0, charpos,
21009 it, 0, nchars, 0,
21010 STRING_MULTIBYTE (elt));
21012 break;
21015 else /* c == '%' */
21017 ptrdiff_t percent_position = offset;
21019 /* Get the specified minimum width. Zero means
21020 don't pad. */
21021 field = 0;
21022 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21023 field = field * 10 + c - '0';
21025 /* Don't pad beyond the total padding allowed. */
21026 if (field_width - n > 0 && field > field_width - n)
21027 field = field_width - n;
21029 /* Note that either PRECISION <= 0 or N < PRECISION. */
21030 prec = precision - n;
21032 if (c == 'M')
21033 n += display_mode_element (it, depth, field, prec,
21034 Vglobal_mode_string, props,
21035 risky);
21036 else if (c != 0)
21038 bool multibyte;
21039 ptrdiff_t bytepos, charpos;
21040 const char *spec;
21041 Lisp_Object string;
21043 bytepos = percent_position;
21044 charpos = (STRING_MULTIBYTE (elt)
21045 ? string_byte_to_char (elt, bytepos)
21046 : bytepos);
21047 spec = decode_mode_spec (it->w, c, field, &string);
21048 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21050 switch (mode_line_target)
21052 case MODE_LINE_NOPROP:
21053 case MODE_LINE_TITLE:
21054 n += store_mode_line_noprop (spec, field, prec);
21055 break;
21056 case MODE_LINE_STRING:
21058 Lisp_Object tem = build_string (spec);
21059 props = Ftext_properties_at (make_number (charpos), elt);
21060 /* Should only keep face property in props */
21061 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21063 break;
21064 case MODE_LINE_DISPLAY:
21066 int nglyphs_before, nwritten;
21068 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21069 nwritten = display_string (spec, string, elt,
21070 charpos, 0, it,
21071 field, prec, 0,
21072 multibyte);
21074 /* Assign to the glyphs written above the
21075 string where the `%x' came from, position
21076 of the `%'. */
21077 if (nwritten > 0)
21079 struct glyph *glyph
21080 = (it->glyph_row->glyphs[TEXT_AREA]
21081 + nglyphs_before);
21082 int i;
21084 for (i = 0; i < nwritten; ++i)
21086 glyph[i].object = elt;
21087 glyph[i].charpos = charpos;
21090 n += nwritten;
21093 break;
21096 else /* c == 0 */
21097 break;
21101 break;
21103 case Lisp_Symbol:
21104 /* A symbol: process the value of the symbol recursively
21105 as if it appeared here directly. Avoid error if symbol void.
21106 Special case: if value of symbol is a string, output the string
21107 literally. */
21109 register Lisp_Object tem;
21111 /* If the variable is not marked as risky to set
21112 then its contents are risky to use. */
21113 if (NILP (Fget (elt, Qrisky_local_variable)))
21114 risky = 1;
21116 tem = Fboundp (elt);
21117 if (!NILP (tem))
21119 tem = Fsymbol_value (elt);
21120 /* If value is a string, output that string literally:
21121 don't check for % within it. */
21122 if (STRINGP (tem))
21123 literal = 1;
21125 if (!EQ (tem, elt))
21127 /* Give up right away for nil or t. */
21128 elt = tem;
21129 goto tail_recurse;
21133 break;
21135 case Lisp_Cons:
21137 register Lisp_Object car, tem;
21139 /* A cons cell: five distinct cases.
21140 If first element is :eval or :propertize, do something special.
21141 If first element is a string or a cons, process all the elements
21142 and effectively concatenate them.
21143 If first element is a negative number, truncate displaying cdr to
21144 at most that many characters. If positive, pad (with spaces)
21145 to at least that many characters.
21146 If first element is a symbol, process the cadr or caddr recursively
21147 according to whether the symbol's value is non-nil or nil. */
21148 car = XCAR (elt);
21149 if (EQ (car, QCeval))
21151 /* An element of the form (:eval FORM) means evaluate FORM
21152 and use the result as mode line elements. */
21154 if (risky)
21155 break;
21157 if (CONSP (XCDR (elt)))
21159 Lisp_Object spec;
21160 spec = safe_eval (XCAR (XCDR (elt)));
21161 n += display_mode_element (it, depth, field_width - n,
21162 precision - n, spec, props,
21163 risky);
21166 else if (EQ (car, QCpropertize))
21168 /* An element of the form (:propertize ELT PROPS...)
21169 means display ELT but applying properties PROPS. */
21171 if (risky)
21172 break;
21174 if (CONSP (XCDR (elt)))
21175 n += display_mode_element (it, depth, field_width - n,
21176 precision - n, XCAR (XCDR (elt)),
21177 XCDR (XCDR (elt)), risky);
21179 else if (SYMBOLP (car))
21181 tem = Fboundp (car);
21182 elt = XCDR (elt);
21183 if (!CONSP (elt))
21184 goto invalid;
21185 /* elt is now the cdr, and we know it is a cons cell.
21186 Use its car if CAR has a non-nil value. */
21187 if (!NILP (tem))
21189 tem = Fsymbol_value (car);
21190 if (!NILP (tem))
21192 elt = XCAR (elt);
21193 goto tail_recurse;
21196 /* Symbol's value is nil (or symbol is unbound)
21197 Get the cddr of the original list
21198 and if possible find the caddr and use that. */
21199 elt = XCDR (elt);
21200 if (NILP (elt))
21201 break;
21202 else if (!CONSP (elt))
21203 goto invalid;
21204 elt = XCAR (elt);
21205 goto tail_recurse;
21207 else if (INTEGERP (car))
21209 register int lim = XINT (car);
21210 elt = XCDR (elt);
21211 if (lim < 0)
21213 /* Negative int means reduce maximum width. */
21214 if (precision <= 0)
21215 precision = -lim;
21216 else
21217 precision = min (precision, -lim);
21219 else if (lim > 0)
21221 /* Padding specified. Don't let it be more than
21222 current maximum. */
21223 if (precision > 0)
21224 lim = min (precision, lim);
21226 /* If that's more padding than already wanted, queue it.
21227 But don't reduce padding already specified even if
21228 that is beyond the current truncation point. */
21229 field_width = max (lim, field_width);
21231 goto tail_recurse;
21233 else if (STRINGP (car) || CONSP (car))
21235 Lisp_Object halftail = elt;
21236 int len = 0;
21238 while (CONSP (elt)
21239 && (precision <= 0 || n < precision))
21241 n += display_mode_element (it, depth,
21242 /* Do padding only after the last
21243 element in the list. */
21244 (! CONSP (XCDR (elt))
21245 ? field_width - n
21246 : 0),
21247 precision - n, XCAR (elt),
21248 props, risky);
21249 elt = XCDR (elt);
21250 len++;
21251 if ((len & 1) == 0)
21252 halftail = XCDR (halftail);
21253 /* Check for cycle. */
21254 if (EQ (halftail, elt))
21255 break;
21259 break;
21261 default:
21262 invalid:
21263 elt = build_string ("*invalid*");
21264 goto tail_recurse;
21267 /* Pad to FIELD_WIDTH. */
21268 if (field_width > 0 && n < field_width)
21270 switch (mode_line_target)
21272 case MODE_LINE_NOPROP:
21273 case MODE_LINE_TITLE:
21274 n += store_mode_line_noprop ("", field_width - n, 0);
21275 break;
21276 case MODE_LINE_STRING:
21277 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21278 break;
21279 case MODE_LINE_DISPLAY:
21280 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21281 0, 0, 0);
21282 break;
21286 return n;
21289 /* Store a mode-line string element in mode_line_string_list.
21291 If STRING is non-null, display that C string. Otherwise, the Lisp
21292 string LISP_STRING is displayed.
21294 FIELD_WIDTH is the minimum number of output glyphs to produce.
21295 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21296 with spaces. FIELD_WIDTH <= 0 means don't pad.
21298 PRECISION is the maximum number of characters to output from
21299 STRING. PRECISION <= 0 means don't truncate the string.
21301 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21302 properties to the string.
21304 PROPS are the properties to add to the string.
21305 The mode_line_string_face face property is always added to the string.
21308 static int
21309 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21310 int field_width, int precision, Lisp_Object props)
21312 ptrdiff_t len;
21313 int n = 0;
21315 if (string != NULL)
21317 len = strlen (string);
21318 if (precision > 0 && len > precision)
21319 len = precision;
21320 lisp_string = make_string (string, len);
21321 if (NILP (props))
21322 props = mode_line_string_face_prop;
21323 else if (!NILP (mode_line_string_face))
21325 Lisp_Object face = Fplist_get (props, Qface);
21326 props = Fcopy_sequence (props);
21327 if (NILP (face))
21328 face = mode_line_string_face;
21329 else
21330 face = list2 (face, mode_line_string_face);
21331 props = Fplist_put (props, Qface, face);
21333 Fadd_text_properties (make_number (0), make_number (len),
21334 props, lisp_string);
21336 else
21338 len = XFASTINT (Flength (lisp_string));
21339 if (precision > 0 && len > precision)
21341 len = precision;
21342 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21343 precision = -1;
21345 if (!NILP (mode_line_string_face))
21347 Lisp_Object face;
21348 if (NILP (props))
21349 props = Ftext_properties_at (make_number (0), lisp_string);
21350 face = Fplist_get (props, Qface);
21351 if (NILP (face))
21352 face = mode_line_string_face;
21353 else
21354 face = list2 (face, mode_line_string_face);
21355 props = list2 (Qface, face);
21356 if (copy_string)
21357 lisp_string = Fcopy_sequence (lisp_string);
21359 if (!NILP (props))
21360 Fadd_text_properties (make_number (0), make_number (len),
21361 props, lisp_string);
21364 if (len > 0)
21366 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21367 n += len;
21370 if (field_width > len)
21372 field_width -= len;
21373 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21374 if (!NILP (props))
21375 Fadd_text_properties (make_number (0), make_number (field_width),
21376 props, lisp_string);
21377 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21378 n += field_width;
21381 return n;
21385 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21386 1, 4, 0,
21387 doc: /* Format a string out of a mode line format specification.
21388 First arg FORMAT specifies the mode line format (see `mode-line-format'
21389 for details) to use.
21391 By default, the format is evaluated for the currently selected window.
21393 Optional second arg FACE specifies the face property to put on all
21394 characters for which no face is specified. The value nil means the
21395 default face. The value t means whatever face the window's mode line
21396 currently uses (either `mode-line' or `mode-line-inactive',
21397 depending on whether the window is the selected window or not).
21398 An integer value means the value string has no text
21399 properties.
21401 Optional third and fourth args WINDOW and BUFFER specify the window
21402 and buffer to use as the context for the formatting (defaults
21403 are the selected window and the WINDOW's buffer). */)
21404 (Lisp_Object format, Lisp_Object face,
21405 Lisp_Object window, Lisp_Object buffer)
21407 struct it it;
21408 int len;
21409 struct window *w;
21410 struct buffer *old_buffer = NULL;
21411 int face_id;
21412 int no_props = INTEGERP (face);
21413 ptrdiff_t count = SPECPDL_INDEX ();
21414 Lisp_Object str;
21415 int string_start = 0;
21417 w = decode_any_window (window);
21418 XSETWINDOW (window, w);
21420 if (NILP (buffer))
21421 buffer = w->contents;
21422 CHECK_BUFFER (buffer);
21424 /* Make formatting the modeline a non-op when noninteractive, otherwise
21425 there will be problems later caused by a partially initialized frame. */
21426 if (NILP (format) || noninteractive)
21427 return empty_unibyte_string;
21429 if (no_props)
21430 face = Qnil;
21432 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21433 : EQ (face, Qt) ? (EQ (window, selected_window)
21434 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21435 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21436 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21437 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21438 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21439 : DEFAULT_FACE_ID;
21441 old_buffer = current_buffer;
21443 /* Save things including mode_line_proptrans_alist,
21444 and set that to nil so that we don't alter the outer value. */
21445 record_unwind_protect (unwind_format_mode_line,
21446 format_mode_line_unwind_data
21447 (XFRAME (WINDOW_FRAME (w)),
21448 old_buffer, selected_window, 1));
21449 mode_line_proptrans_alist = Qnil;
21451 Fselect_window (window, Qt);
21452 set_buffer_internal_1 (XBUFFER (buffer));
21454 init_iterator (&it, w, -1, -1, NULL, face_id);
21456 if (no_props)
21458 mode_line_target = MODE_LINE_NOPROP;
21459 mode_line_string_face_prop = Qnil;
21460 mode_line_string_list = Qnil;
21461 string_start = MODE_LINE_NOPROP_LEN (0);
21463 else
21465 mode_line_target = MODE_LINE_STRING;
21466 mode_line_string_list = Qnil;
21467 mode_line_string_face = face;
21468 mode_line_string_face_prop
21469 = NILP (face) ? Qnil : list2 (Qface, face);
21472 push_kboard (FRAME_KBOARD (it.f));
21473 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21474 pop_kboard ();
21476 if (no_props)
21478 len = MODE_LINE_NOPROP_LEN (string_start);
21479 str = make_string (mode_line_noprop_buf + string_start, len);
21481 else
21483 mode_line_string_list = Fnreverse (mode_line_string_list);
21484 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21485 empty_unibyte_string);
21488 unbind_to (count, Qnil);
21489 return str;
21492 /* Write a null-terminated, right justified decimal representation of
21493 the positive integer D to BUF using a minimal field width WIDTH. */
21495 static void
21496 pint2str (register char *buf, register int width, register ptrdiff_t d)
21498 register char *p = buf;
21500 if (d <= 0)
21501 *p++ = '0';
21502 else
21504 while (d > 0)
21506 *p++ = d % 10 + '0';
21507 d /= 10;
21511 for (width -= (int) (p - buf); width > 0; --width)
21512 *p++ = ' ';
21513 *p-- = '\0';
21514 while (p > buf)
21516 d = *buf;
21517 *buf++ = *p;
21518 *p-- = d;
21522 /* Write a null-terminated, right justified decimal and "human
21523 readable" representation of the nonnegative integer D to BUF using
21524 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21526 static const char power_letter[] =
21528 0, /* no letter */
21529 'k', /* kilo */
21530 'M', /* mega */
21531 'G', /* giga */
21532 'T', /* tera */
21533 'P', /* peta */
21534 'E', /* exa */
21535 'Z', /* zetta */
21536 'Y' /* yotta */
21539 static void
21540 pint2hrstr (char *buf, int width, ptrdiff_t d)
21542 /* We aim to represent the nonnegative integer D as
21543 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21544 ptrdiff_t quotient = d;
21545 int remainder = 0;
21546 /* -1 means: do not use TENTHS. */
21547 int tenths = -1;
21548 int exponent = 0;
21550 /* Length of QUOTIENT.TENTHS as a string. */
21551 int length;
21553 char * psuffix;
21554 char * p;
21556 if (quotient >= 1000)
21558 /* Scale to the appropriate EXPONENT. */
21561 remainder = quotient % 1000;
21562 quotient /= 1000;
21563 exponent++;
21565 while (quotient >= 1000);
21567 /* Round to nearest and decide whether to use TENTHS or not. */
21568 if (quotient <= 9)
21570 tenths = remainder / 100;
21571 if (remainder % 100 >= 50)
21573 if (tenths < 9)
21574 tenths++;
21575 else
21577 quotient++;
21578 if (quotient == 10)
21579 tenths = -1;
21580 else
21581 tenths = 0;
21585 else
21586 if (remainder >= 500)
21588 if (quotient < 999)
21589 quotient++;
21590 else
21592 quotient = 1;
21593 exponent++;
21594 tenths = 0;
21599 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21600 if (tenths == -1 && quotient <= 99)
21601 if (quotient <= 9)
21602 length = 1;
21603 else
21604 length = 2;
21605 else
21606 length = 3;
21607 p = psuffix = buf + max (width, length);
21609 /* Print EXPONENT. */
21610 *psuffix++ = power_letter[exponent];
21611 *psuffix = '\0';
21613 /* Print TENTHS. */
21614 if (tenths >= 0)
21616 *--p = '0' + tenths;
21617 *--p = '.';
21620 /* Print QUOTIENT. */
21623 int digit = quotient % 10;
21624 *--p = '0' + digit;
21626 while ((quotient /= 10) != 0);
21628 /* Print leading spaces. */
21629 while (buf < p)
21630 *--p = ' ';
21633 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21634 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21635 type of CODING_SYSTEM. Return updated pointer into BUF. */
21637 static unsigned char invalid_eol_type[] = "(*invalid*)";
21639 static char *
21640 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21642 Lisp_Object val;
21643 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21644 const unsigned char *eol_str;
21645 int eol_str_len;
21646 /* The EOL conversion we are using. */
21647 Lisp_Object eoltype;
21649 val = CODING_SYSTEM_SPEC (coding_system);
21650 eoltype = Qnil;
21652 if (!VECTORP (val)) /* Not yet decided. */
21654 *buf++ = multibyte ? '-' : ' ';
21655 if (eol_flag)
21656 eoltype = eol_mnemonic_undecided;
21657 /* Don't mention EOL conversion if it isn't decided. */
21659 else
21661 Lisp_Object attrs;
21662 Lisp_Object eolvalue;
21664 attrs = AREF (val, 0);
21665 eolvalue = AREF (val, 2);
21667 *buf++ = multibyte
21668 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21669 : ' ';
21671 if (eol_flag)
21673 /* The EOL conversion that is normal on this system. */
21675 if (NILP (eolvalue)) /* Not yet decided. */
21676 eoltype = eol_mnemonic_undecided;
21677 else if (VECTORP (eolvalue)) /* Not yet decided. */
21678 eoltype = eol_mnemonic_undecided;
21679 else /* eolvalue is Qunix, Qdos, or Qmac. */
21680 eoltype = (EQ (eolvalue, Qunix)
21681 ? eol_mnemonic_unix
21682 : (EQ (eolvalue, Qdos) == 1
21683 ? eol_mnemonic_dos : eol_mnemonic_mac));
21687 if (eol_flag)
21689 /* Mention the EOL conversion if it is not the usual one. */
21690 if (STRINGP (eoltype))
21692 eol_str = SDATA (eoltype);
21693 eol_str_len = SBYTES (eoltype);
21695 else if (CHARACTERP (eoltype))
21697 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21698 int c = XFASTINT (eoltype);
21699 eol_str_len = CHAR_STRING (c, tmp);
21700 eol_str = tmp;
21702 else
21704 eol_str = invalid_eol_type;
21705 eol_str_len = sizeof (invalid_eol_type) - 1;
21707 memcpy (buf, eol_str, eol_str_len);
21708 buf += eol_str_len;
21711 return buf;
21714 /* Return a string for the output of a mode line %-spec for window W,
21715 generated by character C. FIELD_WIDTH > 0 means pad the string
21716 returned with spaces to that value. Return a Lisp string in
21717 *STRING if the resulting string is taken from that Lisp string.
21719 Note we operate on the current buffer for most purposes. */
21721 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21723 static const char *
21724 decode_mode_spec (struct window *w, register int c, int field_width,
21725 Lisp_Object *string)
21727 Lisp_Object obj;
21728 struct frame *f = XFRAME (WINDOW_FRAME (w));
21729 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21730 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21731 produce strings from numerical values, so limit preposterously
21732 large values of FIELD_WIDTH to avoid overrunning the buffer's
21733 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21734 bytes plus the terminating null. */
21735 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21736 struct buffer *b = current_buffer;
21738 obj = Qnil;
21739 *string = Qnil;
21741 switch (c)
21743 case '*':
21744 if (!NILP (BVAR (b, read_only)))
21745 return "%";
21746 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21747 return "*";
21748 return "-";
21750 case '+':
21751 /* This differs from %* only for a modified read-only buffer. */
21752 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21753 return "*";
21754 if (!NILP (BVAR (b, read_only)))
21755 return "%";
21756 return "-";
21758 case '&':
21759 /* This differs from %* in ignoring read-only-ness. */
21760 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21761 return "*";
21762 return "-";
21764 case '%':
21765 return "%";
21767 case '[':
21769 int i;
21770 char *p;
21772 if (command_loop_level > 5)
21773 return "[[[... ";
21774 p = decode_mode_spec_buf;
21775 for (i = 0; i < command_loop_level; i++)
21776 *p++ = '[';
21777 *p = 0;
21778 return decode_mode_spec_buf;
21781 case ']':
21783 int i;
21784 char *p;
21786 if (command_loop_level > 5)
21787 return " ...]]]";
21788 p = decode_mode_spec_buf;
21789 for (i = 0; i < command_loop_level; i++)
21790 *p++ = ']';
21791 *p = 0;
21792 return decode_mode_spec_buf;
21795 case '-':
21797 register int i;
21799 /* Let lots_of_dashes be a string of infinite length. */
21800 if (mode_line_target == MODE_LINE_NOPROP
21801 || mode_line_target == MODE_LINE_STRING)
21802 return "--";
21803 if (field_width <= 0
21804 || field_width > sizeof (lots_of_dashes))
21806 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21807 decode_mode_spec_buf[i] = '-';
21808 decode_mode_spec_buf[i] = '\0';
21809 return decode_mode_spec_buf;
21811 else
21812 return lots_of_dashes;
21815 case 'b':
21816 obj = BVAR (b, name);
21817 break;
21819 case 'c':
21820 /* %c and %l are ignored in `frame-title-format'.
21821 (In redisplay_internal, the frame title is drawn _before_ the
21822 windows are updated, so the stuff which depends on actual
21823 window contents (such as %l) may fail to render properly, or
21824 even crash emacs.) */
21825 if (mode_line_target == MODE_LINE_TITLE)
21826 return "";
21827 else
21829 ptrdiff_t col = current_column ();
21830 w->column_number_displayed = col;
21831 pint2str (decode_mode_spec_buf, width, col);
21832 return decode_mode_spec_buf;
21835 case 'e':
21836 #ifndef SYSTEM_MALLOC
21838 if (NILP (Vmemory_full))
21839 return "";
21840 else
21841 return "!MEM FULL! ";
21843 #else
21844 return "";
21845 #endif
21847 case 'F':
21848 /* %F displays the frame name. */
21849 if (!NILP (f->title))
21850 return SSDATA (f->title);
21851 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21852 return SSDATA (f->name);
21853 return "Emacs";
21855 case 'f':
21856 obj = BVAR (b, filename);
21857 break;
21859 case 'i':
21861 ptrdiff_t size = ZV - BEGV;
21862 pint2str (decode_mode_spec_buf, width, size);
21863 return decode_mode_spec_buf;
21866 case 'I':
21868 ptrdiff_t size = ZV - BEGV;
21869 pint2hrstr (decode_mode_spec_buf, width, size);
21870 return decode_mode_spec_buf;
21873 case 'l':
21875 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21876 ptrdiff_t topline, nlines, height;
21877 ptrdiff_t junk;
21879 /* %c and %l are ignored in `frame-title-format'. */
21880 if (mode_line_target == MODE_LINE_TITLE)
21881 return "";
21883 startpos = marker_position (w->start);
21884 startpos_byte = marker_byte_position (w->start);
21885 height = WINDOW_TOTAL_LINES (w);
21887 /* If we decided that this buffer isn't suitable for line numbers,
21888 don't forget that too fast. */
21889 if (w->base_line_pos == -1)
21890 goto no_value;
21892 /* If the buffer is very big, don't waste time. */
21893 if (INTEGERP (Vline_number_display_limit)
21894 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21896 w->base_line_pos = 0;
21897 w->base_line_number = 0;
21898 goto no_value;
21901 if (w->base_line_number > 0
21902 && w->base_line_pos > 0
21903 && w->base_line_pos <= startpos)
21905 line = w->base_line_number;
21906 linepos = w->base_line_pos;
21907 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21909 else
21911 line = 1;
21912 linepos = BUF_BEGV (b);
21913 linepos_byte = BUF_BEGV_BYTE (b);
21916 /* Count lines from base line to window start position. */
21917 nlines = display_count_lines (linepos_byte,
21918 startpos_byte,
21919 startpos, &junk);
21921 topline = nlines + line;
21923 /* Determine a new base line, if the old one is too close
21924 or too far away, or if we did not have one.
21925 "Too close" means it's plausible a scroll-down would
21926 go back past it. */
21927 if (startpos == BUF_BEGV (b))
21929 w->base_line_number = topline;
21930 w->base_line_pos = BUF_BEGV (b);
21932 else if (nlines < height + 25 || nlines > height * 3 + 50
21933 || linepos == BUF_BEGV (b))
21935 ptrdiff_t limit = BUF_BEGV (b);
21936 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21937 ptrdiff_t position;
21938 ptrdiff_t distance =
21939 (height * 2 + 30) * line_number_display_limit_width;
21941 if (startpos - distance > limit)
21943 limit = startpos - distance;
21944 limit_byte = CHAR_TO_BYTE (limit);
21947 nlines = display_count_lines (startpos_byte,
21948 limit_byte,
21949 - (height * 2 + 30),
21950 &position);
21951 /* If we couldn't find the lines we wanted within
21952 line_number_display_limit_width chars per line,
21953 give up on line numbers for this window. */
21954 if (position == limit_byte && limit == startpos - distance)
21956 w->base_line_pos = -1;
21957 w->base_line_number = 0;
21958 goto no_value;
21961 w->base_line_number = topline - nlines;
21962 w->base_line_pos = BYTE_TO_CHAR (position);
21965 /* Now count lines from the start pos to point. */
21966 nlines = display_count_lines (startpos_byte,
21967 PT_BYTE, PT, &junk);
21969 /* Record that we did display the line number. */
21970 line_number_displayed = 1;
21972 /* Make the string to show. */
21973 pint2str (decode_mode_spec_buf, width, topline + nlines);
21974 return decode_mode_spec_buf;
21975 no_value:
21977 char* p = decode_mode_spec_buf;
21978 int pad = width - 2;
21979 while (pad-- > 0)
21980 *p++ = ' ';
21981 *p++ = '?';
21982 *p++ = '?';
21983 *p = '\0';
21984 return decode_mode_spec_buf;
21987 break;
21989 case 'm':
21990 obj = BVAR (b, mode_name);
21991 break;
21993 case 'n':
21994 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21995 return " Narrow";
21996 break;
21998 case 'p':
22000 ptrdiff_t pos = marker_position (w->start);
22001 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22003 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22005 if (pos <= BUF_BEGV (b))
22006 return "All";
22007 else
22008 return "Bottom";
22010 else if (pos <= BUF_BEGV (b))
22011 return "Top";
22012 else
22014 if (total > 1000000)
22015 /* Do it differently for a large value, to avoid overflow. */
22016 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22017 else
22018 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22019 /* We can't normally display a 3-digit number,
22020 so get us a 2-digit number that is close. */
22021 if (total == 100)
22022 total = 99;
22023 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22024 return decode_mode_spec_buf;
22028 /* Display percentage of size above the bottom of the screen. */
22029 case 'P':
22031 ptrdiff_t toppos = marker_position (w->start);
22032 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22033 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22035 if (botpos >= BUF_ZV (b))
22037 if (toppos <= BUF_BEGV (b))
22038 return "All";
22039 else
22040 return "Bottom";
22042 else
22044 if (total > 1000000)
22045 /* Do it differently for a large value, to avoid overflow. */
22046 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22047 else
22048 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22049 /* We can't normally display a 3-digit number,
22050 so get us a 2-digit number that is close. */
22051 if (total == 100)
22052 total = 99;
22053 if (toppos <= BUF_BEGV (b))
22054 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22055 else
22056 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22057 return decode_mode_spec_buf;
22061 case 's':
22062 /* status of process */
22063 obj = Fget_buffer_process (Fcurrent_buffer ());
22064 if (NILP (obj))
22065 return "no process";
22066 #ifndef MSDOS
22067 obj = Fsymbol_name (Fprocess_status (obj));
22068 #endif
22069 break;
22071 case '@':
22073 ptrdiff_t count = inhibit_garbage_collection ();
22074 Lisp_Object val = call1 (intern ("file-remote-p"),
22075 BVAR (current_buffer, directory));
22076 unbind_to (count, Qnil);
22078 if (NILP (val))
22079 return "-";
22080 else
22081 return "@";
22084 case 'z':
22085 /* coding-system (not including end-of-line format) */
22086 case 'Z':
22087 /* coding-system (including end-of-line type) */
22089 int eol_flag = (c == 'Z');
22090 char *p = decode_mode_spec_buf;
22092 if (! FRAME_WINDOW_P (f))
22094 /* No need to mention EOL here--the terminal never needs
22095 to do EOL conversion. */
22096 p = decode_mode_spec_coding (CODING_ID_NAME
22097 (FRAME_KEYBOARD_CODING (f)->id),
22098 p, 0);
22099 p = decode_mode_spec_coding (CODING_ID_NAME
22100 (FRAME_TERMINAL_CODING (f)->id),
22101 p, 0);
22103 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22104 p, eol_flag);
22106 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22107 #ifdef subprocesses
22108 obj = Fget_buffer_process (Fcurrent_buffer ());
22109 if (PROCESSP (obj))
22111 p = decode_mode_spec_coding
22112 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22113 p = decode_mode_spec_coding
22114 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22116 #endif /* subprocesses */
22117 #endif /* 0 */
22118 *p = 0;
22119 return decode_mode_spec_buf;
22123 if (STRINGP (obj))
22125 *string = obj;
22126 return SSDATA (obj);
22128 else
22129 return "";
22133 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22134 means count lines back from START_BYTE. But don't go beyond
22135 LIMIT_BYTE. Return the number of lines thus found (always
22136 nonnegative).
22138 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22139 either the position COUNT lines after/before START_BYTE, if we
22140 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22141 COUNT lines. */
22143 static ptrdiff_t
22144 display_count_lines (ptrdiff_t start_byte,
22145 ptrdiff_t limit_byte, ptrdiff_t count,
22146 ptrdiff_t *byte_pos_ptr)
22148 register unsigned char *cursor;
22149 unsigned char *base;
22151 register ptrdiff_t ceiling;
22152 register unsigned char *ceiling_addr;
22153 ptrdiff_t orig_count = count;
22155 /* If we are not in selective display mode,
22156 check only for newlines. */
22157 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22158 && !INTEGERP (BVAR (current_buffer, selective_display)));
22160 if (count > 0)
22162 while (start_byte < limit_byte)
22164 ceiling = BUFFER_CEILING_OF (start_byte);
22165 ceiling = min (limit_byte - 1, ceiling);
22166 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22167 base = (cursor = BYTE_POS_ADDR (start_byte));
22171 if (selective_display)
22173 while (*cursor != '\n' && *cursor != 015
22174 && ++cursor != ceiling_addr)
22175 continue;
22176 if (cursor == ceiling_addr)
22177 break;
22179 else
22181 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22182 if (! cursor)
22183 break;
22186 cursor++;
22188 if (--count == 0)
22190 start_byte += cursor - base;
22191 *byte_pos_ptr = start_byte;
22192 return orig_count;
22195 while (cursor < ceiling_addr);
22197 start_byte += ceiling_addr - base;
22200 else
22202 while (start_byte > limit_byte)
22204 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22205 ceiling = max (limit_byte, ceiling);
22206 ceiling_addr = BYTE_POS_ADDR (ceiling);
22207 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22208 while (1)
22210 if (selective_display)
22212 while (--cursor >= ceiling_addr
22213 && *cursor != '\n' && *cursor != 015)
22214 continue;
22215 if (cursor < ceiling_addr)
22216 break;
22218 else
22220 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22221 if (! cursor)
22222 break;
22225 if (++count == 0)
22227 start_byte += cursor - base + 1;
22228 *byte_pos_ptr = start_byte;
22229 /* When scanning backwards, we should
22230 not count the newline posterior to which we stop. */
22231 return - orig_count - 1;
22234 start_byte += ceiling_addr - base;
22238 *byte_pos_ptr = limit_byte;
22240 if (count < 0)
22241 return - orig_count + count;
22242 return orig_count - count;
22248 /***********************************************************************
22249 Displaying strings
22250 ***********************************************************************/
22252 /* Display a NUL-terminated string, starting with index START.
22254 If STRING is non-null, display that C string. Otherwise, the Lisp
22255 string LISP_STRING is displayed. There's a case that STRING is
22256 non-null and LISP_STRING is not nil. It means STRING is a string
22257 data of LISP_STRING. In that case, we display LISP_STRING while
22258 ignoring its text properties.
22260 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22261 FACE_STRING. Display STRING or LISP_STRING with the face at
22262 FACE_STRING_POS in FACE_STRING:
22264 Display the string in the environment given by IT, but use the
22265 standard display table, temporarily.
22267 FIELD_WIDTH is the minimum number of output glyphs to produce.
22268 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22269 with spaces. If STRING has more characters, more than FIELD_WIDTH
22270 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22272 PRECISION is the maximum number of characters to output from
22273 STRING. PRECISION < 0 means don't truncate the string.
22275 This is roughly equivalent to printf format specifiers:
22277 FIELD_WIDTH PRECISION PRINTF
22278 ----------------------------------------
22279 -1 -1 %s
22280 -1 10 %.10s
22281 10 -1 %10s
22282 20 10 %20.10s
22284 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22285 display them, and < 0 means obey the current buffer's value of
22286 enable_multibyte_characters.
22288 Value is the number of columns displayed. */
22290 static int
22291 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22292 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22293 int field_width, int precision, int max_x, int multibyte)
22295 int hpos_at_start = it->hpos;
22296 int saved_face_id = it->face_id;
22297 struct glyph_row *row = it->glyph_row;
22298 ptrdiff_t it_charpos;
22300 /* Initialize the iterator IT for iteration over STRING beginning
22301 with index START. */
22302 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22303 precision, field_width, multibyte);
22304 if (string && STRINGP (lisp_string))
22305 /* LISP_STRING is the one returned by decode_mode_spec. We should
22306 ignore its text properties. */
22307 it->stop_charpos = it->end_charpos;
22309 /* If displaying STRING, set up the face of the iterator from
22310 FACE_STRING, if that's given. */
22311 if (STRINGP (face_string))
22313 ptrdiff_t endptr;
22314 struct face *face;
22316 it->face_id
22317 = face_at_string_position (it->w, face_string, face_string_pos,
22318 0, &endptr, it->base_face_id, 0);
22319 face = FACE_FROM_ID (it->f, it->face_id);
22320 it->face_box_p = face->box != FACE_NO_BOX;
22323 /* Set max_x to the maximum allowed X position. Don't let it go
22324 beyond the right edge of the window. */
22325 if (max_x <= 0)
22326 max_x = it->last_visible_x;
22327 else
22328 max_x = min (max_x, it->last_visible_x);
22330 /* Skip over display elements that are not visible. because IT->w is
22331 hscrolled. */
22332 if (it->current_x < it->first_visible_x)
22333 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22334 MOVE_TO_POS | MOVE_TO_X);
22336 row->ascent = it->max_ascent;
22337 row->height = it->max_ascent + it->max_descent;
22338 row->phys_ascent = it->max_phys_ascent;
22339 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22340 row->extra_line_spacing = it->max_extra_line_spacing;
22342 if (STRINGP (it->string))
22343 it_charpos = IT_STRING_CHARPOS (*it);
22344 else
22345 it_charpos = IT_CHARPOS (*it);
22347 /* This condition is for the case that we are called with current_x
22348 past last_visible_x. */
22349 while (it->current_x < max_x)
22351 int x_before, x, n_glyphs_before, i, nglyphs;
22353 /* Get the next display element. */
22354 if (!get_next_display_element (it))
22355 break;
22357 /* Produce glyphs. */
22358 x_before = it->current_x;
22359 n_glyphs_before = row->used[TEXT_AREA];
22360 PRODUCE_GLYPHS (it);
22362 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22363 i = 0;
22364 x = x_before;
22365 while (i < nglyphs)
22367 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22369 if (it->line_wrap != TRUNCATE
22370 && x + glyph->pixel_width > max_x)
22372 /* End of continued line or max_x reached. */
22373 if (CHAR_GLYPH_PADDING_P (*glyph))
22375 /* A wide character is unbreakable. */
22376 if (row->reversed_p)
22377 unproduce_glyphs (it, row->used[TEXT_AREA]
22378 - n_glyphs_before);
22379 row->used[TEXT_AREA] = n_glyphs_before;
22380 it->current_x = x_before;
22382 else
22384 if (row->reversed_p)
22385 unproduce_glyphs (it, row->used[TEXT_AREA]
22386 - (n_glyphs_before + i));
22387 row->used[TEXT_AREA] = n_glyphs_before + i;
22388 it->current_x = x;
22390 break;
22392 else if (x + glyph->pixel_width >= it->first_visible_x)
22394 /* Glyph is at least partially visible. */
22395 ++it->hpos;
22396 if (x < it->first_visible_x)
22397 row->x = x - it->first_visible_x;
22399 else
22401 /* Glyph is off the left margin of the display area.
22402 Should not happen. */
22403 emacs_abort ();
22406 row->ascent = max (row->ascent, it->max_ascent);
22407 row->height = max (row->height, it->max_ascent + it->max_descent);
22408 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22409 row->phys_height = max (row->phys_height,
22410 it->max_phys_ascent + it->max_phys_descent);
22411 row->extra_line_spacing = max (row->extra_line_spacing,
22412 it->max_extra_line_spacing);
22413 x += glyph->pixel_width;
22414 ++i;
22417 /* Stop if max_x reached. */
22418 if (i < nglyphs)
22419 break;
22421 /* Stop at line ends. */
22422 if (ITERATOR_AT_END_OF_LINE_P (it))
22424 it->continuation_lines_width = 0;
22425 break;
22428 set_iterator_to_next (it, 1);
22429 if (STRINGP (it->string))
22430 it_charpos = IT_STRING_CHARPOS (*it);
22431 else
22432 it_charpos = IT_CHARPOS (*it);
22434 /* Stop if truncating at the right edge. */
22435 if (it->line_wrap == TRUNCATE
22436 && it->current_x >= it->last_visible_x)
22438 /* Add truncation mark, but don't do it if the line is
22439 truncated at a padding space. */
22440 if (it_charpos < it->string_nchars)
22442 if (!FRAME_WINDOW_P (it->f))
22444 int ii, n;
22446 if (it->current_x > it->last_visible_x)
22448 if (!row->reversed_p)
22450 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22451 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22452 break;
22454 else
22456 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22457 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22458 break;
22459 unproduce_glyphs (it, ii + 1);
22460 ii = row->used[TEXT_AREA] - (ii + 1);
22462 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22464 row->used[TEXT_AREA] = ii;
22465 produce_special_glyphs (it, IT_TRUNCATION);
22468 produce_special_glyphs (it, IT_TRUNCATION);
22470 row->truncated_on_right_p = 1;
22472 break;
22476 /* Maybe insert a truncation at the left. */
22477 if (it->first_visible_x
22478 && it_charpos > 0)
22480 if (!FRAME_WINDOW_P (it->f)
22481 || (row->reversed_p
22482 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22483 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22484 insert_left_trunc_glyphs (it);
22485 row->truncated_on_left_p = 1;
22488 it->face_id = saved_face_id;
22490 /* Value is number of columns displayed. */
22491 return it->hpos - hpos_at_start;
22496 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22497 appears as an element of LIST or as the car of an element of LIST.
22498 If PROPVAL is a list, compare each element against LIST in that
22499 way, and return 1/2 if any element of PROPVAL is found in LIST.
22500 Otherwise return 0. This function cannot quit.
22501 The return value is 2 if the text is invisible but with an ellipsis
22502 and 1 if it's invisible and without an ellipsis. */
22505 invisible_p (register Lisp_Object propval, Lisp_Object list)
22507 register Lisp_Object tail, proptail;
22509 for (tail = list; CONSP (tail); tail = XCDR (tail))
22511 register Lisp_Object tem;
22512 tem = XCAR (tail);
22513 if (EQ (propval, tem))
22514 return 1;
22515 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22516 return NILP (XCDR (tem)) ? 1 : 2;
22519 if (CONSP (propval))
22521 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22523 Lisp_Object propelt;
22524 propelt = XCAR (proptail);
22525 for (tail = list; CONSP (tail); tail = XCDR (tail))
22527 register Lisp_Object tem;
22528 tem = XCAR (tail);
22529 if (EQ (propelt, tem))
22530 return 1;
22531 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22532 return NILP (XCDR (tem)) ? 1 : 2;
22537 return 0;
22540 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22541 doc: /* Non-nil if the property makes the text invisible.
22542 POS-OR-PROP can be a marker or number, in which case it is taken to be
22543 a position in the current buffer and the value of the `invisible' property
22544 is checked; or it can be some other value, which is then presumed to be the
22545 value of the `invisible' property of the text of interest.
22546 The non-nil value returned can be t for truly invisible text or something
22547 else if the text is replaced by an ellipsis. */)
22548 (Lisp_Object pos_or_prop)
22550 Lisp_Object prop
22551 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22552 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22553 : pos_or_prop);
22554 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22555 return (invis == 0 ? Qnil
22556 : invis == 1 ? Qt
22557 : make_number (invis));
22560 /* Calculate a width or height in pixels from a specification using
22561 the following elements:
22563 SPEC ::=
22564 NUM - a (fractional) multiple of the default font width/height
22565 (NUM) - specifies exactly NUM pixels
22566 UNIT - a fixed number of pixels, see below.
22567 ELEMENT - size of a display element in pixels, see below.
22568 (NUM . SPEC) - equals NUM * SPEC
22569 (+ SPEC SPEC ...) - add pixel values
22570 (- SPEC SPEC ...) - subtract pixel values
22571 (- SPEC) - negate pixel value
22573 NUM ::=
22574 INT or FLOAT - a number constant
22575 SYMBOL - use symbol's (buffer local) variable binding.
22577 UNIT ::=
22578 in - pixels per inch *)
22579 mm - pixels per 1/1000 meter *)
22580 cm - pixels per 1/100 meter *)
22581 width - width of current font in pixels.
22582 height - height of current font in pixels.
22584 *) using the ratio(s) defined in display-pixels-per-inch.
22586 ELEMENT ::=
22588 left-fringe - left fringe width in pixels
22589 right-fringe - right fringe width in pixels
22591 left-margin - left margin width in pixels
22592 right-margin - right margin width in pixels
22594 scroll-bar - scroll-bar area width in pixels
22596 Examples:
22598 Pixels corresponding to 5 inches:
22599 (5 . in)
22601 Total width of non-text areas on left side of window (if scroll-bar is on left):
22602 '(space :width (+ left-fringe left-margin scroll-bar))
22604 Align to first text column (in header line):
22605 '(space :align-to 0)
22607 Align to middle of text area minus half the width of variable `my-image'
22608 containing a loaded image:
22609 '(space :align-to (0.5 . (- text my-image)))
22611 Width of left margin minus width of 1 character in the default font:
22612 '(space :width (- left-margin 1))
22614 Width of left margin minus width of 2 characters in the current font:
22615 '(space :width (- left-margin (2 . width)))
22617 Center 1 character over left-margin (in header line):
22618 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22620 Different ways to express width of left fringe plus left margin minus one pixel:
22621 '(space :width (- (+ left-fringe left-margin) (1)))
22622 '(space :width (+ left-fringe left-margin (- (1))))
22623 '(space :width (+ left-fringe left-margin (-1)))
22627 static int
22628 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22629 struct font *font, int width_p, int *align_to)
22631 double pixels;
22633 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22634 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22636 if (NILP (prop))
22637 return OK_PIXELS (0);
22639 eassert (FRAME_LIVE_P (it->f));
22641 if (SYMBOLP (prop))
22643 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22645 char *unit = SSDATA (SYMBOL_NAME (prop));
22647 if (unit[0] == 'i' && unit[1] == 'n')
22648 pixels = 1.0;
22649 else if (unit[0] == 'm' && unit[1] == 'm')
22650 pixels = 25.4;
22651 else if (unit[0] == 'c' && unit[1] == 'm')
22652 pixels = 2.54;
22653 else
22654 pixels = 0;
22655 if (pixels > 0)
22657 double ppi = (width_p ? FRAME_RES_X (it->f)
22658 : FRAME_RES_Y (it->f));
22660 if (ppi > 0)
22661 return OK_PIXELS (ppi / pixels);
22662 return 0;
22666 #ifdef HAVE_WINDOW_SYSTEM
22667 if (EQ (prop, Qheight))
22668 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22669 if (EQ (prop, Qwidth))
22670 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22671 #else
22672 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22673 return OK_PIXELS (1);
22674 #endif
22676 if (EQ (prop, Qtext))
22677 return OK_PIXELS (width_p
22678 ? window_box_width (it->w, TEXT_AREA)
22679 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22681 if (align_to && *align_to < 0)
22683 *res = 0;
22684 if (EQ (prop, Qleft))
22685 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22686 if (EQ (prop, Qright))
22687 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22688 if (EQ (prop, Qcenter))
22689 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22690 + window_box_width (it->w, TEXT_AREA) / 2);
22691 if (EQ (prop, Qleft_fringe))
22692 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22693 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22694 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22695 if (EQ (prop, Qright_fringe))
22696 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22697 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22698 : window_box_right_offset (it->w, TEXT_AREA));
22699 if (EQ (prop, Qleft_margin))
22700 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22701 if (EQ (prop, Qright_margin))
22702 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22703 if (EQ (prop, Qscroll_bar))
22704 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22706 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22707 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22708 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22709 : 0)));
22711 else
22713 if (EQ (prop, Qleft_fringe))
22714 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22715 if (EQ (prop, Qright_fringe))
22716 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22717 if (EQ (prop, Qleft_margin))
22718 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22719 if (EQ (prop, Qright_margin))
22720 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22721 if (EQ (prop, Qscroll_bar))
22722 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22725 prop = buffer_local_value_1 (prop, it->w->contents);
22726 if (EQ (prop, Qunbound))
22727 prop = Qnil;
22730 if (INTEGERP (prop) || FLOATP (prop))
22732 int base_unit = (width_p
22733 ? FRAME_COLUMN_WIDTH (it->f)
22734 : FRAME_LINE_HEIGHT (it->f));
22735 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22738 if (CONSP (prop))
22740 Lisp_Object car = XCAR (prop);
22741 Lisp_Object cdr = XCDR (prop);
22743 if (SYMBOLP (car))
22745 #ifdef HAVE_WINDOW_SYSTEM
22746 if (FRAME_WINDOW_P (it->f)
22747 && valid_image_p (prop))
22749 ptrdiff_t id = lookup_image (it->f, prop);
22750 struct image *img = IMAGE_FROM_ID (it->f, id);
22752 return OK_PIXELS (width_p ? img->width : img->height);
22754 #endif
22755 if (EQ (car, Qplus) || EQ (car, Qminus))
22757 int first = 1;
22758 double px;
22760 pixels = 0;
22761 while (CONSP (cdr))
22763 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22764 font, width_p, align_to))
22765 return 0;
22766 if (first)
22767 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22768 else
22769 pixels += px;
22770 cdr = XCDR (cdr);
22772 if (EQ (car, Qminus))
22773 pixels = -pixels;
22774 return OK_PIXELS (pixels);
22777 car = buffer_local_value_1 (car, it->w->contents);
22778 if (EQ (car, Qunbound))
22779 car = Qnil;
22782 if (INTEGERP (car) || FLOATP (car))
22784 double fact;
22785 pixels = XFLOATINT (car);
22786 if (NILP (cdr))
22787 return OK_PIXELS (pixels);
22788 if (calc_pixel_width_or_height (&fact, it, cdr,
22789 font, width_p, align_to))
22790 return OK_PIXELS (pixels * fact);
22791 return 0;
22794 return 0;
22797 return 0;
22801 /***********************************************************************
22802 Glyph Display
22803 ***********************************************************************/
22805 #ifdef HAVE_WINDOW_SYSTEM
22807 #ifdef GLYPH_DEBUG
22809 void
22810 dump_glyph_string (struct glyph_string *s)
22812 fprintf (stderr, "glyph string\n");
22813 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22814 s->x, s->y, s->width, s->height);
22815 fprintf (stderr, " ybase = %d\n", s->ybase);
22816 fprintf (stderr, " hl = %d\n", s->hl);
22817 fprintf (stderr, " left overhang = %d, right = %d\n",
22818 s->left_overhang, s->right_overhang);
22819 fprintf (stderr, " nchars = %d\n", s->nchars);
22820 fprintf (stderr, " extends to end of line = %d\n",
22821 s->extends_to_end_of_line_p);
22822 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22823 fprintf (stderr, " bg width = %d\n", s->background_width);
22826 #endif /* GLYPH_DEBUG */
22828 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22829 of XChar2b structures for S; it can't be allocated in
22830 init_glyph_string because it must be allocated via `alloca'. W
22831 is the window on which S is drawn. ROW and AREA are the glyph row
22832 and area within the row from which S is constructed. START is the
22833 index of the first glyph structure covered by S. HL is a
22834 face-override for drawing S. */
22836 #ifdef HAVE_NTGUI
22837 #define OPTIONAL_HDC(hdc) HDC hdc,
22838 #define DECLARE_HDC(hdc) HDC hdc;
22839 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22840 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22841 #endif
22843 #ifndef OPTIONAL_HDC
22844 #define OPTIONAL_HDC(hdc)
22845 #define DECLARE_HDC(hdc)
22846 #define ALLOCATE_HDC(hdc, f)
22847 #define RELEASE_HDC(hdc, f)
22848 #endif
22850 static void
22851 init_glyph_string (struct glyph_string *s,
22852 OPTIONAL_HDC (hdc)
22853 XChar2b *char2b, struct window *w, struct glyph_row *row,
22854 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22856 memset (s, 0, sizeof *s);
22857 s->w = w;
22858 s->f = XFRAME (w->frame);
22859 #ifdef HAVE_NTGUI
22860 s->hdc = hdc;
22861 #endif
22862 s->display = FRAME_X_DISPLAY (s->f);
22863 s->window = FRAME_X_WINDOW (s->f);
22864 s->char2b = char2b;
22865 s->hl = hl;
22866 s->row = row;
22867 s->area = area;
22868 s->first_glyph = row->glyphs[area] + start;
22869 s->height = row->height;
22870 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22871 s->ybase = s->y + row->ascent;
22875 /* Append the list of glyph strings with head H and tail T to the list
22876 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22878 static void
22879 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22880 struct glyph_string *h, struct glyph_string *t)
22882 if (h)
22884 if (*head)
22885 (*tail)->next = h;
22886 else
22887 *head = h;
22888 h->prev = *tail;
22889 *tail = t;
22894 /* Prepend the list of glyph strings with head H and tail T to the
22895 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22896 result. */
22898 static void
22899 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22900 struct glyph_string *h, struct glyph_string *t)
22902 if (h)
22904 if (*head)
22905 (*head)->prev = t;
22906 else
22907 *tail = t;
22908 t->next = *head;
22909 *head = h;
22914 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22915 Set *HEAD and *TAIL to the resulting list. */
22917 static void
22918 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22919 struct glyph_string *s)
22921 s->next = s->prev = NULL;
22922 append_glyph_string_lists (head, tail, s, s);
22926 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22927 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22928 make sure that X resources for the face returned are allocated.
22929 Value is a pointer to a realized face that is ready for display if
22930 DISPLAY_P is non-zero. */
22932 static struct face *
22933 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22934 XChar2b *char2b, int display_p)
22936 struct face *face = FACE_FROM_ID (f, face_id);
22937 unsigned code = 0;
22939 if (face->font)
22941 code = face->font->driver->encode_char (face->font, c);
22943 if (code == FONT_INVALID_CODE)
22944 code = 0;
22946 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22948 /* Make sure X resources of the face are allocated. */
22949 #ifdef HAVE_X_WINDOWS
22950 if (display_p)
22951 #endif
22953 eassert (face != NULL);
22954 PREPARE_FACE_FOR_DISPLAY (f, face);
22957 return face;
22961 /* Get face and two-byte form of character glyph GLYPH on frame F.
22962 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22963 a pointer to a realized face that is ready for display. */
22965 static struct face *
22966 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22967 XChar2b *char2b, int *two_byte_p)
22969 struct face *face;
22970 unsigned code = 0;
22972 eassert (glyph->type == CHAR_GLYPH);
22973 face = FACE_FROM_ID (f, glyph->face_id);
22975 /* Make sure X resources of the face are allocated. */
22976 eassert (face != NULL);
22977 PREPARE_FACE_FOR_DISPLAY (f, face);
22979 if (two_byte_p)
22980 *two_byte_p = 0;
22982 if (face->font)
22984 if (CHAR_BYTE8_P (glyph->u.ch))
22985 code = CHAR_TO_BYTE8 (glyph->u.ch);
22986 else
22987 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22989 if (code == FONT_INVALID_CODE)
22990 code = 0;
22993 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22994 return face;
22998 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22999 Return 1 if FONT has a glyph for C, otherwise return 0. */
23001 static int
23002 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23004 unsigned code;
23006 if (CHAR_BYTE8_P (c))
23007 code = CHAR_TO_BYTE8 (c);
23008 else
23009 code = font->driver->encode_char (font, c);
23011 if (code == FONT_INVALID_CODE)
23012 return 0;
23013 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23014 return 1;
23018 /* Fill glyph string S with composition components specified by S->cmp.
23020 BASE_FACE is the base face of the composition.
23021 S->cmp_from is the index of the first component for S.
23023 OVERLAPS non-zero means S should draw the foreground only, and use
23024 its physical height for clipping. See also draw_glyphs.
23026 Value is the index of a component not in S. */
23028 static int
23029 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23030 int overlaps)
23032 int i;
23033 /* For all glyphs of this composition, starting at the offset
23034 S->cmp_from, until we reach the end of the definition or encounter a
23035 glyph that requires the different face, add it to S. */
23036 struct face *face;
23038 eassert (s);
23040 s->for_overlaps = overlaps;
23041 s->face = NULL;
23042 s->font = NULL;
23043 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23045 int c = COMPOSITION_GLYPH (s->cmp, i);
23047 /* TAB in a composition means display glyphs with padding space
23048 on the left or right. */
23049 if (c != '\t')
23051 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23052 -1, Qnil);
23054 face = get_char_face_and_encoding (s->f, c, face_id,
23055 s->char2b + i, 1);
23056 if (face)
23058 if (! s->face)
23060 s->face = face;
23061 s->font = s->face->font;
23063 else if (s->face != face)
23064 break;
23067 ++s->nchars;
23069 s->cmp_to = i;
23071 if (s->face == NULL)
23073 s->face = base_face->ascii_face;
23074 s->font = s->face->font;
23077 /* All glyph strings for the same composition has the same width,
23078 i.e. the width set for the first component of the composition. */
23079 s->width = s->first_glyph->pixel_width;
23081 /* If the specified font could not be loaded, use the frame's
23082 default font, but record the fact that we couldn't load it in
23083 the glyph string so that we can draw rectangles for the
23084 characters of the glyph string. */
23085 if (s->font == NULL)
23087 s->font_not_found_p = 1;
23088 s->font = FRAME_FONT (s->f);
23091 /* Adjust base line for subscript/superscript text. */
23092 s->ybase += s->first_glyph->voffset;
23094 /* This glyph string must always be drawn with 16-bit functions. */
23095 s->two_byte_p = 1;
23097 return s->cmp_to;
23100 static int
23101 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23102 int start, int end, int overlaps)
23104 struct glyph *glyph, *last;
23105 Lisp_Object lgstring;
23106 int i;
23108 s->for_overlaps = overlaps;
23109 glyph = s->row->glyphs[s->area] + start;
23110 last = s->row->glyphs[s->area] + end;
23111 s->cmp_id = glyph->u.cmp.id;
23112 s->cmp_from = glyph->slice.cmp.from;
23113 s->cmp_to = glyph->slice.cmp.to + 1;
23114 s->face = FACE_FROM_ID (s->f, face_id);
23115 lgstring = composition_gstring_from_id (s->cmp_id);
23116 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23117 glyph++;
23118 while (glyph < last
23119 && glyph->u.cmp.automatic
23120 && glyph->u.cmp.id == s->cmp_id
23121 && s->cmp_to == glyph->slice.cmp.from)
23122 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23124 for (i = s->cmp_from; i < s->cmp_to; i++)
23126 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23127 unsigned code = LGLYPH_CODE (lglyph);
23129 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23131 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23132 return glyph - s->row->glyphs[s->area];
23136 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23137 See the comment of fill_glyph_string for arguments.
23138 Value is the index of the first glyph not in S. */
23141 static int
23142 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23143 int start, int end, int overlaps)
23145 struct glyph *glyph, *last;
23146 int voffset;
23148 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23149 s->for_overlaps = overlaps;
23150 glyph = s->row->glyphs[s->area] + start;
23151 last = s->row->glyphs[s->area] + end;
23152 voffset = glyph->voffset;
23153 s->face = FACE_FROM_ID (s->f, face_id);
23154 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23155 s->nchars = 1;
23156 s->width = glyph->pixel_width;
23157 glyph++;
23158 while (glyph < last
23159 && glyph->type == GLYPHLESS_GLYPH
23160 && glyph->voffset == voffset
23161 && glyph->face_id == face_id)
23163 s->nchars++;
23164 s->width += glyph->pixel_width;
23165 glyph++;
23167 s->ybase += voffset;
23168 return glyph - s->row->glyphs[s->area];
23172 /* Fill glyph string S from a sequence of character glyphs.
23174 FACE_ID is the face id of the string. START is the index of the
23175 first glyph to consider, END is the index of the last + 1.
23176 OVERLAPS non-zero means S should draw the foreground only, and use
23177 its physical height for clipping. See also draw_glyphs.
23179 Value is the index of the first glyph not in S. */
23181 static int
23182 fill_glyph_string (struct glyph_string *s, int face_id,
23183 int start, int end, int overlaps)
23185 struct glyph *glyph, *last;
23186 int voffset;
23187 int glyph_not_available_p;
23189 eassert (s->f == XFRAME (s->w->frame));
23190 eassert (s->nchars == 0);
23191 eassert (start >= 0 && end > start);
23193 s->for_overlaps = overlaps;
23194 glyph = s->row->glyphs[s->area] + start;
23195 last = s->row->glyphs[s->area] + end;
23196 voffset = glyph->voffset;
23197 s->padding_p = glyph->padding_p;
23198 glyph_not_available_p = glyph->glyph_not_available_p;
23200 while (glyph < last
23201 && glyph->type == CHAR_GLYPH
23202 && glyph->voffset == voffset
23203 /* Same face id implies same font, nowadays. */
23204 && glyph->face_id == face_id
23205 && glyph->glyph_not_available_p == glyph_not_available_p)
23207 int two_byte_p;
23209 s->face = get_glyph_face_and_encoding (s->f, glyph,
23210 s->char2b + s->nchars,
23211 &two_byte_p);
23212 s->two_byte_p = two_byte_p;
23213 ++s->nchars;
23214 eassert (s->nchars <= end - start);
23215 s->width += glyph->pixel_width;
23216 if (glyph++->padding_p != s->padding_p)
23217 break;
23220 s->font = s->face->font;
23222 /* If the specified font could not be loaded, use the frame's font,
23223 but record the fact that we couldn't load it in
23224 S->font_not_found_p so that we can draw rectangles for the
23225 characters of the glyph string. */
23226 if (s->font == NULL || glyph_not_available_p)
23228 s->font_not_found_p = 1;
23229 s->font = FRAME_FONT (s->f);
23232 /* Adjust base line for subscript/superscript text. */
23233 s->ybase += voffset;
23235 eassert (s->face && s->face->gc);
23236 return glyph - s->row->glyphs[s->area];
23240 /* Fill glyph string S from image glyph S->first_glyph. */
23242 static void
23243 fill_image_glyph_string (struct glyph_string *s)
23245 eassert (s->first_glyph->type == IMAGE_GLYPH);
23246 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23247 eassert (s->img);
23248 s->slice = s->first_glyph->slice.img;
23249 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23250 s->font = s->face->font;
23251 s->width = s->first_glyph->pixel_width;
23253 /* Adjust base line for subscript/superscript text. */
23254 s->ybase += s->first_glyph->voffset;
23258 /* Fill glyph string S from a sequence of stretch glyphs.
23260 START is the index of the first glyph to consider,
23261 END is the index of the last + 1.
23263 Value is the index of the first glyph not in S. */
23265 static int
23266 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23268 struct glyph *glyph, *last;
23269 int voffset, face_id;
23271 eassert (s->first_glyph->type == STRETCH_GLYPH);
23273 glyph = s->row->glyphs[s->area] + start;
23274 last = s->row->glyphs[s->area] + end;
23275 face_id = glyph->face_id;
23276 s->face = FACE_FROM_ID (s->f, face_id);
23277 s->font = s->face->font;
23278 s->width = glyph->pixel_width;
23279 s->nchars = 1;
23280 voffset = glyph->voffset;
23282 for (++glyph;
23283 (glyph < last
23284 && glyph->type == STRETCH_GLYPH
23285 && glyph->voffset == voffset
23286 && glyph->face_id == face_id);
23287 ++glyph)
23288 s->width += glyph->pixel_width;
23290 /* Adjust base line for subscript/superscript text. */
23291 s->ybase += voffset;
23293 /* The case that face->gc == 0 is handled when drawing the glyph
23294 string by calling PREPARE_FACE_FOR_DISPLAY. */
23295 eassert (s->face);
23296 return glyph - s->row->glyphs[s->area];
23299 static struct font_metrics *
23300 get_per_char_metric (struct font *font, XChar2b *char2b)
23302 static struct font_metrics metrics;
23303 unsigned code;
23305 if (! font)
23306 return NULL;
23307 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23308 if (code == FONT_INVALID_CODE)
23309 return NULL;
23310 font->driver->text_extents (font, &code, 1, &metrics);
23311 return &metrics;
23314 /* EXPORT for RIF:
23315 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23316 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23317 assumed to be zero. */
23319 void
23320 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23322 *left = *right = 0;
23324 if (glyph->type == CHAR_GLYPH)
23326 struct face *face;
23327 XChar2b char2b;
23328 struct font_metrics *pcm;
23330 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23331 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23333 if (pcm->rbearing > pcm->width)
23334 *right = pcm->rbearing - pcm->width;
23335 if (pcm->lbearing < 0)
23336 *left = -pcm->lbearing;
23339 else if (glyph->type == COMPOSITE_GLYPH)
23341 if (! glyph->u.cmp.automatic)
23343 struct composition *cmp = composition_table[glyph->u.cmp.id];
23345 if (cmp->rbearing > cmp->pixel_width)
23346 *right = cmp->rbearing - cmp->pixel_width;
23347 if (cmp->lbearing < 0)
23348 *left = - cmp->lbearing;
23350 else
23352 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23353 struct font_metrics metrics;
23355 composition_gstring_width (gstring, glyph->slice.cmp.from,
23356 glyph->slice.cmp.to + 1, &metrics);
23357 if (metrics.rbearing > metrics.width)
23358 *right = metrics.rbearing - metrics.width;
23359 if (metrics.lbearing < 0)
23360 *left = - metrics.lbearing;
23366 /* Return the index of the first glyph preceding glyph string S that
23367 is overwritten by S because of S's left overhang. Value is -1
23368 if no glyphs are overwritten. */
23370 static int
23371 left_overwritten (struct glyph_string *s)
23373 int k;
23375 if (s->left_overhang)
23377 int x = 0, i;
23378 struct glyph *glyphs = s->row->glyphs[s->area];
23379 int first = s->first_glyph - glyphs;
23381 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23382 x -= glyphs[i].pixel_width;
23384 k = i + 1;
23386 else
23387 k = -1;
23389 return k;
23393 /* Return the index of the first glyph preceding glyph string S that
23394 is overwriting S because of its right overhang. Value is -1 if no
23395 glyph in front of S overwrites S. */
23397 static int
23398 left_overwriting (struct glyph_string *s)
23400 int i, k, x;
23401 struct glyph *glyphs = s->row->glyphs[s->area];
23402 int first = s->first_glyph - glyphs;
23404 k = -1;
23405 x = 0;
23406 for (i = first - 1; i >= 0; --i)
23408 int left, right;
23409 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23410 if (x + right > 0)
23411 k = i;
23412 x -= glyphs[i].pixel_width;
23415 return k;
23419 /* Return the index of the last glyph following glyph string S that is
23420 overwritten by S because of S's right overhang. Value is -1 if
23421 no such glyph is found. */
23423 static int
23424 right_overwritten (struct glyph_string *s)
23426 int k = -1;
23428 if (s->right_overhang)
23430 int x = 0, i;
23431 struct glyph *glyphs = s->row->glyphs[s->area];
23432 int first = (s->first_glyph - glyphs
23433 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23434 int end = s->row->used[s->area];
23436 for (i = first; i < end && s->right_overhang > x; ++i)
23437 x += glyphs[i].pixel_width;
23439 k = i;
23442 return k;
23446 /* Return the index of the last glyph following glyph string S that
23447 overwrites S because of its left overhang. Value is negative
23448 if no such glyph is found. */
23450 static int
23451 right_overwriting (struct glyph_string *s)
23453 int i, k, x;
23454 int end = s->row->used[s->area];
23455 struct glyph *glyphs = s->row->glyphs[s->area];
23456 int first = (s->first_glyph - glyphs
23457 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23459 k = -1;
23460 x = 0;
23461 for (i = first; i < end; ++i)
23463 int left, right;
23464 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23465 if (x - left < 0)
23466 k = i;
23467 x += glyphs[i].pixel_width;
23470 return k;
23474 /* Set background width of glyph string S. START is the index of the
23475 first glyph following S. LAST_X is the right-most x-position + 1
23476 in the drawing area. */
23478 static void
23479 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23481 /* If the face of this glyph string has to be drawn to the end of
23482 the drawing area, set S->extends_to_end_of_line_p. */
23484 if (start == s->row->used[s->area]
23485 && s->area == TEXT_AREA
23486 && ((s->row->fill_line_p
23487 && (s->hl == DRAW_NORMAL_TEXT
23488 || s->hl == DRAW_IMAGE_RAISED
23489 || s->hl == DRAW_IMAGE_SUNKEN))
23490 || s->hl == DRAW_MOUSE_FACE))
23491 s->extends_to_end_of_line_p = 1;
23493 /* If S extends its face to the end of the line, set its
23494 background_width to the distance to the right edge of the drawing
23495 area. */
23496 if (s->extends_to_end_of_line_p)
23497 s->background_width = last_x - s->x + 1;
23498 else
23499 s->background_width = s->width;
23503 /* Compute overhangs and x-positions for glyph string S and its
23504 predecessors, or successors. X is the starting x-position for S.
23505 BACKWARD_P non-zero means process predecessors. */
23507 static void
23508 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23510 if (backward_p)
23512 while (s)
23514 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23515 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23516 x -= s->width;
23517 s->x = x;
23518 s = s->prev;
23521 else
23523 while (s)
23525 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23526 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23527 s->x = x;
23528 x += s->width;
23529 s = s->next;
23536 /* The following macros are only called from draw_glyphs below.
23537 They reference the following parameters of that function directly:
23538 `w', `row', `area', and `overlap_p'
23539 as well as the following local variables:
23540 `s', `f', and `hdc' (in W32) */
23542 #ifdef HAVE_NTGUI
23543 /* On W32, silently add local `hdc' variable to argument list of
23544 init_glyph_string. */
23545 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23546 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23547 #else
23548 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23549 init_glyph_string (s, char2b, w, row, area, start, hl)
23550 #endif
23552 /* Add a glyph string for a stretch glyph to the list of strings
23553 between HEAD and TAIL. START is the index of the stretch glyph in
23554 row area AREA of glyph row ROW. END is the index of the last glyph
23555 in that glyph row area. X is the current output position assigned
23556 to the new glyph string constructed. HL overrides that face of the
23557 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23558 is the right-most x-position of the drawing area. */
23560 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23561 and below -- keep them on one line. */
23562 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23563 do \
23565 s = alloca (sizeof *s); \
23566 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23567 START = fill_stretch_glyph_string (s, START, END); \
23568 append_glyph_string (&HEAD, &TAIL, s); \
23569 s->x = (X); \
23571 while (0)
23574 /* Add a glyph string for an image glyph to the list of strings
23575 between HEAD and TAIL. START is the index of the image glyph in
23576 row area AREA of glyph row ROW. END is the index of the last glyph
23577 in that glyph row area. X is the current output position assigned
23578 to the new glyph string constructed. HL overrides that face of the
23579 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23580 is the right-most x-position of the drawing area. */
23582 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23583 do \
23585 s = alloca (sizeof *s); \
23586 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23587 fill_image_glyph_string (s); \
23588 append_glyph_string (&HEAD, &TAIL, s); \
23589 ++START; \
23590 s->x = (X); \
23592 while (0)
23595 /* Add a glyph string for a sequence of character glyphs to the list
23596 of strings between HEAD and TAIL. START is the index of the first
23597 glyph in row area AREA of glyph row ROW that is part of the new
23598 glyph string. END is the index of the last glyph in that glyph row
23599 area. X is the current output position assigned to the new glyph
23600 string constructed. HL overrides that face of the glyph; e.g. it
23601 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23602 right-most x-position of the drawing area. */
23604 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23605 do \
23607 int face_id; \
23608 XChar2b *char2b; \
23610 face_id = (row)->glyphs[area][START].face_id; \
23612 s = alloca (sizeof *s); \
23613 char2b = alloca ((END - START) * sizeof *char2b); \
23614 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23615 append_glyph_string (&HEAD, &TAIL, s); \
23616 s->x = (X); \
23617 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23619 while (0)
23622 /* Add a glyph string for a composite sequence to the list of strings
23623 between HEAD and TAIL. START is the index of the first glyph in
23624 row area AREA of glyph row ROW that is part of the new glyph
23625 string. END is the index of the last glyph in that glyph row area.
23626 X is the current output position assigned to the new glyph string
23627 constructed. HL overrides that face of the glyph; e.g. it is
23628 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23629 x-position of the drawing area. */
23631 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23632 do { \
23633 int face_id = (row)->glyphs[area][START].face_id; \
23634 struct face *base_face = FACE_FROM_ID (f, face_id); \
23635 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23636 struct composition *cmp = composition_table[cmp_id]; \
23637 XChar2b *char2b; \
23638 struct glyph_string *first_s = NULL; \
23639 int n; \
23641 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23643 /* Make glyph_strings for each glyph sequence that is drawable by \
23644 the same face, and append them to HEAD/TAIL. */ \
23645 for (n = 0; n < cmp->glyph_len;) \
23647 s = alloca (sizeof *s); \
23648 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23649 append_glyph_string (&(HEAD), &(TAIL), s); \
23650 s->cmp = cmp; \
23651 s->cmp_from = n; \
23652 s->x = (X); \
23653 if (n == 0) \
23654 first_s = s; \
23655 n = fill_composite_glyph_string (s, base_face, overlaps); \
23658 ++START; \
23659 s = first_s; \
23660 } while (0)
23663 /* Add a glyph string for a glyph-string sequence to the list of strings
23664 between HEAD and TAIL. */
23666 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23667 do { \
23668 int face_id; \
23669 XChar2b *char2b; \
23670 Lisp_Object gstring; \
23672 face_id = (row)->glyphs[area][START].face_id; \
23673 gstring = (composition_gstring_from_id \
23674 ((row)->glyphs[area][START].u.cmp.id)); \
23675 s = alloca (sizeof *s); \
23676 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23677 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23678 append_glyph_string (&(HEAD), &(TAIL), s); \
23679 s->x = (X); \
23680 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23681 } while (0)
23684 /* Add a glyph string for a sequence of glyphless character's glyphs
23685 to the list of strings between HEAD and TAIL. The meanings of
23686 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23688 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23689 do \
23691 int face_id; \
23693 face_id = (row)->glyphs[area][START].face_id; \
23695 s = alloca (sizeof *s); \
23696 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23697 append_glyph_string (&HEAD, &TAIL, s); \
23698 s->x = (X); \
23699 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23700 overlaps); \
23702 while (0)
23705 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23706 of AREA of glyph row ROW on window W between indices START and END.
23707 HL overrides the face for drawing glyph strings, e.g. it is
23708 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23709 x-positions of the drawing area.
23711 This is an ugly monster macro construct because we must use alloca
23712 to allocate glyph strings (because draw_glyphs can be called
23713 asynchronously). */
23715 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23716 do \
23718 HEAD = TAIL = NULL; \
23719 while (START < END) \
23721 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23722 switch (first_glyph->type) \
23724 case CHAR_GLYPH: \
23725 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23726 HL, X, LAST_X); \
23727 break; \
23729 case COMPOSITE_GLYPH: \
23730 if (first_glyph->u.cmp.automatic) \
23731 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23732 HL, X, LAST_X); \
23733 else \
23734 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23735 HL, X, LAST_X); \
23736 break; \
23738 case STRETCH_GLYPH: \
23739 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23740 HL, X, LAST_X); \
23741 break; \
23743 case IMAGE_GLYPH: \
23744 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23745 HL, X, LAST_X); \
23746 break; \
23748 case GLYPHLESS_GLYPH: \
23749 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23750 HL, X, LAST_X); \
23751 break; \
23753 default: \
23754 emacs_abort (); \
23757 if (s) \
23759 set_glyph_string_background_width (s, START, LAST_X); \
23760 (X) += s->width; \
23763 } while (0)
23766 /* Draw glyphs between START and END in AREA of ROW on window W,
23767 starting at x-position X. X is relative to AREA in W. HL is a
23768 face-override with the following meaning:
23770 DRAW_NORMAL_TEXT draw normally
23771 DRAW_CURSOR draw in cursor face
23772 DRAW_MOUSE_FACE draw in mouse face.
23773 DRAW_INVERSE_VIDEO draw in mode line face
23774 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23775 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23777 If OVERLAPS is non-zero, draw only the foreground of characters and
23778 clip to the physical height of ROW. Non-zero value also defines
23779 the overlapping part to be drawn:
23781 OVERLAPS_PRED overlap with preceding rows
23782 OVERLAPS_SUCC overlap with succeeding rows
23783 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23784 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23786 Value is the x-position reached, relative to AREA of W. */
23788 static int
23789 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23790 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23791 enum draw_glyphs_face hl, int overlaps)
23793 struct glyph_string *head, *tail;
23794 struct glyph_string *s;
23795 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23796 int i, j, x_reached, last_x, area_left = 0;
23797 struct frame *f = XFRAME (WINDOW_FRAME (w));
23798 DECLARE_HDC (hdc);
23800 ALLOCATE_HDC (hdc, f);
23802 /* Let's rather be paranoid than getting a SEGV. */
23803 end = min (end, row->used[area]);
23804 start = clip_to_bounds (0, start, end);
23806 /* Translate X to frame coordinates. Set last_x to the right
23807 end of the drawing area. */
23808 if (row->full_width_p)
23810 /* X is relative to the left edge of W, without scroll bars
23811 or fringes. */
23812 area_left = WINDOW_LEFT_EDGE_X (w);
23813 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23815 else
23817 area_left = window_box_left (w, area);
23818 last_x = area_left + window_box_width (w, area);
23820 x += area_left;
23822 /* Build a doubly-linked list of glyph_string structures between
23823 head and tail from what we have to draw. Note that the macro
23824 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23825 the reason we use a separate variable `i'. */
23826 i = start;
23827 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23828 if (tail)
23829 x_reached = tail->x + tail->background_width;
23830 else
23831 x_reached = x;
23833 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23834 the row, redraw some glyphs in front or following the glyph
23835 strings built above. */
23836 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23838 struct glyph_string *h, *t;
23839 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23840 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23841 int check_mouse_face = 0;
23842 int dummy_x = 0;
23844 /* If mouse highlighting is on, we may need to draw adjacent
23845 glyphs using mouse-face highlighting. */
23846 if (area == TEXT_AREA && row->mouse_face_p
23847 && hlinfo->mouse_face_beg_row >= 0
23848 && hlinfo->mouse_face_end_row >= 0)
23850 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23852 if (row_vpos >= hlinfo->mouse_face_beg_row
23853 && row_vpos <= hlinfo->mouse_face_end_row)
23855 check_mouse_face = 1;
23856 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23857 ? hlinfo->mouse_face_beg_col : 0;
23858 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23859 ? hlinfo->mouse_face_end_col
23860 : row->used[TEXT_AREA];
23864 /* Compute overhangs for all glyph strings. */
23865 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23866 for (s = head; s; s = s->next)
23867 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23869 /* Prepend glyph strings for glyphs in front of the first glyph
23870 string that are overwritten because of the first glyph
23871 string's left overhang. The background of all strings
23872 prepended must be drawn because the first glyph string
23873 draws over it. */
23874 i = left_overwritten (head);
23875 if (i >= 0)
23877 enum draw_glyphs_face overlap_hl;
23879 /* If this row contains mouse highlighting, attempt to draw
23880 the overlapped glyphs with the correct highlight. This
23881 code fails if the overlap encompasses more than one glyph
23882 and mouse-highlight spans only some of these glyphs.
23883 However, making it work perfectly involves a lot more
23884 code, and I don't know if the pathological case occurs in
23885 practice, so we'll stick to this for now. --- cyd */
23886 if (check_mouse_face
23887 && mouse_beg_col < start && mouse_end_col > i)
23888 overlap_hl = DRAW_MOUSE_FACE;
23889 else
23890 overlap_hl = DRAW_NORMAL_TEXT;
23892 j = i;
23893 BUILD_GLYPH_STRINGS (j, start, h, t,
23894 overlap_hl, dummy_x, last_x);
23895 start = i;
23896 compute_overhangs_and_x (t, head->x, 1);
23897 prepend_glyph_string_lists (&head, &tail, h, t);
23898 clip_head = head;
23901 /* Prepend glyph strings for glyphs in front of the first glyph
23902 string that overwrite that glyph string because of their
23903 right overhang. For these strings, only the foreground must
23904 be drawn, because it draws over the glyph string at `head'.
23905 The background must not be drawn because this would overwrite
23906 right overhangs of preceding glyphs for which no glyph
23907 strings exist. */
23908 i = left_overwriting (head);
23909 if (i >= 0)
23911 enum draw_glyphs_face overlap_hl;
23913 if (check_mouse_face
23914 && mouse_beg_col < start && mouse_end_col > i)
23915 overlap_hl = DRAW_MOUSE_FACE;
23916 else
23917 overlap_hl = DRAW_NORMAL_TEXT;
23919 clip_head = head;
23920 BUILD_GLYPH_STRINGS (i, start, h, t,
23921 overlap_hl, dummy_x, last_x);
23922 for (s = h; s; s = s->next)
23923 s->background_filled_p = 1;
23924 compute_overhangs_and_x (t, head->x, 1);
23925 prepend_glyph_string_lists (&head, &tail, h, t);
23928 /* Append glyphs strings for glyphs following the last glyph
23929 string tail that are overwritten by tail. The background of
23930 these strings has to be drawn because tail's foreground draws
23931 over it. */
23932 i = right_overwritten (tail);
23933 if (i >= 0)
23935 enum draw_glyphs_face overlap_hl;
23937 if (check_mouse_face
23938 && mouse_beg_col < i && mouse_end_col > end)
23939 overlap_hl = DRAW_MOUSE_FACE;
23940 else
23941 overlap_hl = DRAW_NORMAL_TEXT;
23943 BUILD_GLYPH_STRINGS (end, i, h, t,
23944 overlap_hl, x, last_x);
23945 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23946 we don't have `end = i;' here. */
23947 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23948 append_glyph_string_lists (&head, &tail, h, t);
23949 clip_tail = tail;
23952 /* Append glyph strings for glyphs following the last glyph
23953 string tail that overwrite tail. The foreground of such
23954 glyphs has to be drawn because it writes into the background
23955 of tail. The background must not be drawn because it could
23956 paint over the foreground of following glyphs. */
23957 i = right_overwriting (tail);
23958 if (i >= 0)
23960 enum draw_glyphs_face overlap_hl;
23961 if (check_mouse_face
23962 && mouse_beg_col < i && mouse_end_col > end)
23963 overlap_hl = DRAW_MOUSE_FACE;
23964 else
23965 overlap_hl = DRAW_NORMAL_TEXT;
23967 clip_tail = tail;
23968 i++; /* We must include the Ith glyph. */
23969 BUILD_GLYPH_STRINGS (end, i, h, t,
23970 overlap_hl, x, last_x);
23971 for (s = h; s; s = s->next)
23972 s->background_filled_p = 1;
23973 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23974 append_glyph_string_lists (&head, &tail, h, t);
23976 if (clip_head || clip_tail)
23977 for (s = head; s; s = s->next)
23979 s->clip_head = clip_head;
23980 s->clip_tail = clip_tail;
23984 /* Draw all strings. */
23985 for (s = head; s; s = s->next)
23986 FRAME_RIF (f)->draw_glyph_string (s);
23988 #ifndef HAVE_NS
23989 /* When focus a sole frame and move horizontally, this sets on_p to 0
23990 causing a failure to erase prev cursor position. */
23991 if (area == TEXT_AREA
23992 && !row->full_width_p
23993 /* When drawing overlapping rows, only the glyph strings'
23994 foreground is drawn, which doesn't erase a cursor
23995 completely. */
23996 && !overlaps)
23998 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23999 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24000 : (tail ? tail->x + tail->background_width : x));
24001 x0 -= area_left;
24002 x1 -= area_left;
24004 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24005 row->y, MATRIX_ROW_BOTTOM_Y (row));
24007 #endif
24009 /* Value is the x-position up to which drawn, relative to AREA of W.
24010 This doesn't include parts drawn because of overhangs. */
24011 if (row->full_width_p)
24012 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24013 else
24014 x_reached -= area_left;
24016 RELEASE_HDC (hdc, f);
24018 return x_reached;
24021 /* Expand row matrix if too narrow. Don't expand if area
24022 is not present. */
24024 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24026 if (!it->f->fonts_changed \
24027 && (it->glyph_row->glyphs[area] \
24028 < it->glyph_row->glyphs[area + 1])) \
24030 it->w->ncols_scale_factor++; \
24031 it->f->fonts_changed = 1; \
24035 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24036 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24038 static void
24039 append_glyph (struct it *it)
24041 struct glyph *glyph;
24042 enum glyph_row_area area = it->area;
24044 eassert (it->glyph_row);
24045 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24047 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24048 if (glyph < it->glyph_row->glyphs[area + 1])
24050 /* If the glyph row is reversed, we need to prepend the glyph
24051 rather than append it. */
24052 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24054 struct glyph *g;
24056 /* Make room for the additional glyph. */
24057 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24058 g[1] = *g;
24059 glyph = it->glyph_row->glyphs[area];
24061 glyph->charpos = CHARPOS (it->position);
24062 glyph->object = it->object;
24063 if (it->pixel_width > 0)
24065 glyph->pixel_width = it->pixel_width;
24066 glyph->padding_p = 0;
24068 else
24070 /* Assure at least 1-pixel width. Otherwise, cursor can't
24071 be displayed correctly. */
24072 glyph->pixel_width = 1;
24073 glyph->padding_p = 1;
24075 glyph->ascent = it->ascent;
24076 glyph->descent = it->descent;
24077 glyph->voffset = it->voffset;
24078 glyph->type = CHAR_GLYPH;
24079 glyph->avoid_cursor_p = it->avoid_cursor_p;
24080 glyph->multibyte_p = it->multibyte_p;
24081 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24083 /* In R2L rows, the left and the right box edges need to be
24084 drawn in reverse direction. */
24085 glyph->right_box_line_p = it->start_of_box_run_p;
24086 glyph->left_box_line_p = it->end_of_box_run_p;
24088 else
24090 glyph->left_box_line_p = it->start_of_box_run_p;
24091 glyph->right_box_line_p = it->end_of_box_run_p;
24093 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24094 || it->phys_descent > it->descent);
24095 glyph->glyph_not_available_p = it->glyph_not_available_p;
24096 glyph->face_id = it->face_id;
24097 glyph->u.ch = it->char_to_display;
24098 glyph->slice.img = null_glyph_slice;
24099 glyph->font_type = FONT_TYPE_UNKNOWN;
24100 if (it->bidi_p)
24102 glyph->resolved_level = it->bidi_it.resolved_level;
24103 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24104 emacs_abort ();
24105 glyph->bidi_type = it->bidi_it.type;
24107 else
24109 glyph->resolved_level = 0;
24110 glyph->bidi_type = UNKNOWN_BT;
24112 ++it->glyph_row->used[area];
24114 else
24115 IT_EXPAND_MATRIX_WIDTH (it, area);
24118 /* Store one glyph for the composition IT->cmp_it.id in
24119 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24120 non-null. */
24122 static void
24123 append_composite_glyph (struct it *it)
24125 struct glyph *glyph;
24126 enum glyph_row_area area = it->area;
24128 eassert (it->glyph_row);
24130 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24131 if (glyph < it->glyph_row->glyphs[area + 1])
24133 /* If the glyph row is reversed, we need to prepend the glyph
24134 rather than append it. */
24135 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24137 struct glyph *g;
24139 /* Make room for the new glyph. */
24140 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24141 g[1] = *g;
24142 glyph = it->glyph_row->glyphs[it->area];
24144 glyph->charpos = it->cmp_it.charpos;
24145 glyph->object = it->object;
24146 glyph->pixel_width = it->pixel_width;
24147 glyph->ascent = it->ascent;
24148 glyph->descent = it->descent;
24149 glyph->voffset = it->voffset;
24150 glyph->type = COMPOSITE_GLYPH;
24151 if (it->cmp_it.ch < 0)
24153 glyph->u.cmp.automatic = 0;
24154 glyph->u.cmp.id = it->cmp_it.id;
24155 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24157 else
24159 glyph->u.cmp.automatic = 1;
24160 glyph->u.cmp.id = it->cmp_it.id;
24161 glyph->slice.cmp.from = it->cmp_it.from;
24162 glyph->slice.cmp.to = it->cmp_it.to - 1;
24164 glyph->avoid_cursor_p = it->avoid_cursor_p;
24165 glyph->multibyte_p = it->multibyte_p;
24166 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24168 /* In R2L rows, the left and the right box edges need to be
24169 drawn in reverse direction. */
24170 glyph->right_box_line_p = it->start_of_box_run_p;
24171 glyph->left_box_line_p = it->end_of_box_run_p;
24173 else
24175 glyph->left_box_line_p = it->start_of_box_run_p;
24176 glyph->right_box_line_p = it->end_of_box_run_p;
24178 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24179 || it->phys_descent > it->descent);
24180 glyph->padding_p = 0;
24181 glyph->glyph_not_available_p = 0;
24182 glyph->face_id = it->face_id;
24183 glyph->font_type = FONT_TYPE_UNKNOWN;
24184 if (it->bidi_p)
24186 glyph->resolved_level = it->bidi_it.resolved_level;
24187 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24188 emacs_abort ();
24189 glyph->bidi_type = it->bidi_it.type;
24191 ++it->glyph_row->used[area];
24193 else
24194 IT_EXPAND_MATRIX_WIDTH (it, area);
24198 /* Change IT->ascent and IT->height according to the setting of
24199 IT->voffset. */
24201 static void
24202 take_vertical_position_into_account (struct it *it)
24204 if (it->voffset)
24206 if (it->voffset < 0)
24207 /* Increase the ascent so that we can display the text higher
24208 in the line. */
24209 it->ascent -= it->voffset;
24210 else
24211 /* Increase the descent so that we can display the text lower
24212 in the line. */
24213 it->descent += it->voffset;
24218 /* Produce glyphs/get display metrics for the image IT is loaded with.
24219 See the description of struct display_iterator in dispextern.h for
24220 an overview of struct display_iterator. */
24222 static void
24223 produce_image_glyph (struct it *it)
24225 struct image *img;
24226 struct face *face;
24227 int glyph_ascent, crop;
24228 struct glyph_slice slice;
24230 eassert (it->what == IT_IMAGE);
24232 face = FACE_FROM_ID (it->f, it->face_id);
24233 eassert (face);
24234 /* Make sure X resources of the face is loaded. */
24235 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24237 if (it->image_id < 0)
24239 /* Fringe bitmap. */
24240 it->ascent = it->phys_ascent = 0;
24241 it->descent = it->phys_descent = 0;
24242 it->pixel_width = 0;
24243 it->nglyphs = 0;
24244 return;
24247 img = IMAGE_FROM_ID (it->f, it->image_id);
24248 eassert (img);
24249 /* Make sure X resources of the image is loaded. */
24250 prepare_image_for_display (it->f, img);
24252 slice.x = slice.y = 0;
24253 slice.width = img->width;
24254 slice.height = img->height;
24256 if (INTEGERP (it->slice.x))
24257 slice.x = XINT (it->slice.x);
24258 else if (FLOATP (it->slice.x))
24259 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24261 if (INTEGERP (it->slice.y))
24262 slice.y = XINT (it->slice.y);
24263 else if (FLOATP (it->slice.y))
24264 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24266 if (INTEGERP (it->slice.width))
24267 slice.width = XINT (it->slice.width);
24268 else if (FLOATP (it->slice.width))
24269 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24271 if (INTEGERP (it->slice.height))
24272 slice.height = XINT (it->slice.height);
24273 else if (FLOATP (it->slice.height))
24274 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24276 if (slice.x >= img->width)
24277 slice.x = img->width;
24278 if (slice.y >= img->height)
24279 slice.y = img->height;
24280 if (slice.x + slice.width >= img->width)
24281 slice.width = img->width - slice.x;
24282 if (slice.y + slice.height > img->height)
24283 slice.height = img->height - slice.y;
24285 if (slice.width == 0 || slice.height == 0)
24286 return;
24288 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24290 it->descent = slice.height - glyph_ascent;
24291 if (slice.y == 0)
24292 it->descent += img->vmargin;
24293 if (slice.y + slice.height == img->height)
24294 it->descent += img->vmargin;
24295 it->phys_descent = it->descent;
24297 it->pixel_width = slice.width;
24298 if (slice.x == 0)
24299 it->pixel_width += img->hmargin;
24300 if (slice.x + slice.width == img->width)
24301 it->pixel_width += img->hmargin;
24303 /* It's quite possible for images to have an ascent greater than
24304 their height, so don't get confused in that case. */
24305 if (it->descent < 0)
24306 it->descent = 0;
24308 it->nglyphs = 1;
24310 if (face->box != FACE_NO_BOX)
24312 if (face->box_line_width > 0)
24314 if (slice.y == 0)
24315 it->ascent += face->box_line_width;
24316 if (slice.y + slice.height == img->height)
24317 it->descent += face->box_line_width;
24320 if (it->start_of_box_run_p && slice.x == 0)
24321 it->pixel_width += eabs (face->box_line_width);
24322 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24323 it->pixel_width += eabs (face->box_line_width);
24326 take_vertical_position_into_account (it);
24328 /* Automatically crop wide image glyphs at right edge so we can
24329 draw the cursor on same display row. */
24330 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24331 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24333 it->pixel_width -= crop;
24334 slice.width -= crop;
24337 if (it->glyph_row)
24339 struct glyph *glyph;
24340 enum glyph_row_area area = it->area;
24342 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24343 if (glyph < it->glyph_row->glyphs[area + 1])
24345 glyph->charpos = CHARPOS (it->position);
24346 glyph->object = it->object;
24347 glyph->pixel_width = it->pixel_width;
24348 glyph->ascent = glyph_ascent;
24349 glyph->descent = it->descent;
24350 glyph->voffset = it->voffset;
24351 glyph->type = IMAGE_GLYPH;
24352 glyph->avoid_cursor_p = it->avoid_cursor_p;
24353 glyph->multibyte_p = it->multibyte_p;
24354 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24356 /* In R2L rows, the left and the right box edges need to be
24357 drawn in reverse direction. */
24358 glyph->right_box_line_p = it->start_of_box_run_p;
24359 glyph->left_box_line_p = it->end_of_box_run_p;
24361 else
24363 glyph->left_box_line_p = it->start_of_box_run_p;
24364 glyph->right_box_line_p = it->end_of_box_run_p;
24366 glyph->overlaps_vertically_p = 0;
24367 glyph->padding_p = 0;
24368 glyph->glyph_not_available_p = 0;
24369 glyph->face_id = it->face_id;
24370 glyph->u.img_id = img->id;
24371 glyph->slice.img = slice;
24372 glyph->font_type = FONT_TYPE_UNKNOWN;
24373 if (it->bidi_p)
24375 glyph->resolved_level = it->bidi_it.resolved_level;
24376 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24377 emacs_abort ();
24378 glyph->bidi_type = it->bidi_it.type;
24380 ++it->glyph_row->used[area];
24382 else
24383 IT_EXPAND_MATRIX_WIDTH (it, area);
24388 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24389 of the glyph, WIDTH and HEIGHT are the width and height of the
24390 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24392 static void
24393 append_stretch_glyph (struct it *it, Lisp_Object object,
24394 int width, int height, int ascent)
24396 struct glyph *glyph;
24397 enum glyph_row_area area = it->area;
24399 eassert (ascent >= 0 && ascent <= height);
24401 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24402 if (glyph < it->glyph_row->glyphs[area + 1])
24404 /* If the glyph row is reversed, we need to prepend the glyph
24405 rather than append it. */
24406 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24408 struct glyph *g;
24410 /* Make room for the additional glyph. */
24411 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24412 g[1] = *g;
24413 glyph = it->glyph_row->glyphs[area];
24415 glyph->charpos = CHARPOS (it->position);
24416 glyph->object = object;
24417 glyph->pixel_width = width;
24418 glyph->ascent = ascent;
24419 glyph->descent = height - ascent;
24420 glyph->voffset = it->voffset;
24421 glyph->type = STRETCH_GLYPH;
24422 glyph->avoid_cursor_p = it->avoid_cursor_p;
24423 glyph->multibyte_p = it->multibyte_p;
24424 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24426 /* In R2L rows, the left and the right box edges need to be
24427 drawn in reverse direction. */
24428 glyph->right_box_line_p = it->start_of_box_run_p;
24429 glyph->left_box_line_p = it->end_of_box_run_p;
24431 else
24433 glyph->left_box_line_p = it->start_of_box_run_p;
24434 glyph->right_box_line_p = it->end_of_box_run_p;
24436 glyph->overlaps_vertically_p = 0;
24437 glyph->padding_p = 0;
24438 glyph->glyph_not_available_p = 0;
24439 glyph->face_id = it->face_id;
24440 glyph->u.stretch.ascent = ascent;
24441 glyph->u.stretch.height = height;
24442 glyph->slice.img = null_glyph_slice;
24443 glyph->font_type = FONT_TYPE_UNKNOWN;
24444 if (it->bidi_p)
24446 glyph->resolved_level = it->bidi_it.resolved_level;
24447 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24448 emacs_abort ();
24449 glyph->bidi_type = it->bidi_it.type;
24451 else
24453 glyph->resolved_level = 0;
24454 glyph->bidi_type = UNKNOWN_BT;
24456 ++it->glyph_row->used[area];
24458 else
24459 IT_EXPAND_MATRIX_WIDTH (it, area);
24462 #endif /* HAVE_WINDOW_SYSTEM */
24464 /* Produce a stretch glyph for iterator IT. IT->object is the value
24465 of the glyph property displayed. The value must be a list
24466 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24467 being recognized:
24469 1. `:width WIDTH' specifies that the space should be WIDTH *
24470 canonical char width wide. WIDTH may be an integer or floating
24471 point number.
24473 2. `:relative-width FACTOR' specifies that the width of the stretch
24474 should be computed from the width of the first character having the
24475 `glyph' property, and should be FACTOR times that width.
24477 3. `:align-to HPOS' specifies that the space should be wide enough
24478 to reach HPOS, a value in canonical character units.
24480 Exactly one of the above pairs must be present.
24482 4. `:height HEIGHT' specifies that the height of the stretch produced
24483 should be HEIGHT, measured in canonical character units.
24485 5. `:relative-height FACTOR' specifies that the height of the
24486 stretch should be FACTOR times the height of the characters having
24487 the glyph property.
24489 Either none or exactly one of 4 or 5 must be present.
24491 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24492 of the stretch should be used for the ascent of the stretch.
24493 ASCENT must be in the range 0 <= ASCENT <= 100. */
24495 void
24496 produce_stretch_glyph (struct it *it)
24498 /* (space :width WIDTH :height HEIGHT ...) */
24499 Lisp_Object prop, plist;
24500 int width = 0, height = 0, align_to = -1;
24501 int zero_width_ok_p = 0;
24502 double tem;
24503 struct font *font = NULL;
24505 #ifdef HAVE_WINDOW_SYSTEM
24506 int ascent = 0;
24507 int zero_height_ok_p = 0;
24509 if (FRAME_WINDOW_P (it->f))
24511 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24512 font = face->font ? face->font : FRAME_FONT (it->f);
24513 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24515 #endif
24517 /* List should start with `space'. */
24518 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24519 plist = XCDR (it->object);
24521 /* Compute the width of the stretch. */
24522 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24523 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24525 /* Absolute width `:width WIDTH' specified and valid. */
24526 zero_width_ok_p = 1;
24527 width = (int)tem;
24529 #ifdef HAVE_WINDOW_SYSTEM
24530 else if (FRAME_WINDOW_P (it->f)
24531 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24533 /* Relative width `:relative-width FACTOR' specified and valid.
24534 Compute the width of the characters having the `glyph'
24535 property. */
24536 struct it it2;
24537 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24539 it2 = *it;
24540 if (it->multibyte_p)
24541 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24542 else
24544 it2.c = it2.char_to_display = *p, it2.len = 1;
24545 if (! ASCII_CHAR_P (it2.c))
24546 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24549 it2.glyph_row = NULL;
24550 it2.what = IT_CHARACTER;
24551 x_produce_glyphs (&it2);
24552 width = NUMVAL (prop) * it2.pixel_width;
24554 #endif /* HAVE_WINDOW_SYSTEM */
24555 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24556 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24558 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24559 align_to = (align_to < 0
24561 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24562 else if (align_to < 0)
24563 align_to = window_box_left_offset (it->w, TEXT_AREA);
24564 width = max (0, (int)tem + align_to - it->current_x);
24565 zero_width_ok_p = 1;
24567 else
24568 /* Nothing specified -> width defaults to canonical char width. */
24569 width = FRAME_COLUMN_WIDTH (it->f);
24571 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24572 width = 1;
24574 #ifdef HAVE_WINDOW_SYSTEM
24575 /* Compute height. */
24576 if (FRAME_WINDOW_P (it->f))
24578 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24579 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24581 height = (int)tem;
24582 zero_height_ok_p = 1;
24584 else if (prop = Fplist_get (plist, QCrelative_height),
24585 NUMVAL (prop) > 0)
24586 height = FONT_HEIGHT (font) * NUMVAL (prop);
24587 else
24588 height = FONT_HEIGHT (font);
24590 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24591 height = 1;
24593 /* Compute percentage of height used for ascent. If
24594 `:ascent ASCENT' is present and valid, use that. Otherwise,
24595 derive the ascent from the font in use. */
24596 if (prop = Fplist_get (plist, QCascent),
24597 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24598 ascent = height * NUMVAL (prop) / 100.0;
24599 else if (!NILP (prop)
24600 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24601 ascent = min (max (0, (int)tem), height);
24602 else
24603 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24605 else
24606 #endif /* HAVE_WINDOW_SYSTEM */
24607 height = 1;
24609 if (width > 0 && it->line_wrap != TRUNCATE
24610 && it->current_x + width > it->last_visible_x)
24612 width = it->last_visible_x - it->current_x;
24613 #ifdef HAVE_WINDOW_SYSTEM
24614 /* Subtract one more pixel from the stretch width, but only on
24615 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24616 width -= FRAME_WINDOW_P (it->f);
24617 #endif
24620 if (width > 0 && height > 0 && it->glyph_row)
24622 Lisp_Object o_object = it->object;
24623 Lisp_Object object = it->stack[it->sp - 1].string;
24624 int n = width;
24626 if (!STRINGP (object))
24627 object = it->w->contents;
24628 #ifdef HAVE_WINDOW_SYSTEM
24629 if (FRAME_WINDOW_P (it->f))
24630 append_stretch_glyph (it, object, width, height, ascent);
24631 else
24632 #endif
24634 it->object = object;
24635 it->char_to_display = ' ';
24636 it->pixel_width = it->len = 1;
24637 while (n--)
24638 tty_append_glyph (it);
24639 it->object = o_object;
24643 it->pixel_width = width;
24644 #ifdef HAVE_WINDOW_SYSTEM
24645 if (FRAME_WINDOW_P (it->f))
24647 it->ascent = it->phys_ascent = ascent;
24648 it->descent = it->phys_descent = height - it->ascent;
24649 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24650 take_vertical_position_into_account (it);
24652 else
24653 #endif
24654 it->nglyphs = width;
24657 /* Get information about special display element WHAT in an
24658 environment described by IT. WHAT is one of IT_TRUNCATION or
24659 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24660 non-null glyph_row member. This function ensures that fields like
24661 face_id, c, len of IT are left untouched. */
24663 static void
24664 produce_special_glyphs (struct it *it, enum display_element_type what)
24666 struct it temp_it;
24667 Lisp_Object gc;
24668 GLYPH glyph;
24670 temp_it = *it;
24671 temp_it.object = make_number (0);
24672 memset (&temp_it.current, 0, sizeof temp_it.current);
24674 if (what == IT_CONTINUATION)
24676 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24677 if (it->bidi_it.paragraph_dir == R2L)
24678 SET_GLYPH_FROM_CHAR (glyph, '/');
24679 else
24680 SET_GLYPH_FROM_CHAR (glyph, '\\');
24681 if (it->dp
24682 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24684 /* FIXME: Should we mirror GC for R2L lines? */
24685 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24686 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24689 else if (what == IT_TRUNCATION)
24691 /* Truncation glyph. */
24692 SET_GLYPH_FROM_CHAR (glyph, '$');
24693 if (it->dp
24694 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24696 /* FIXME: Should we mirror GC for R2L lines? */
24697 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24698 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24701 else
24702 emacs_abort ();
24704 #ifdef HAVE_WINDOW_SYSTEM
24705 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24706 is turned off, we precede the truncation/continuation glyphs by a
24707 stretch glyph whose width is computed such that these special
24708 glyphs are aligned at the window margin, even when very different
24709 fonts are used in different glyph rows. */
24710 if (FRAME_WINDOW_P (temp_it.f)
24711 /* init_iterator calls this with it->glyph_row == NULL, and it
24712 wants only the pixel width of the truncation/continuation
24713 glyphs. */
24714 && temp_it.glyph_row
24715 /* insert_left_trunc_glyphs calls us at the beginning of the
24716 row, and it has its own calculation of the stretch glyph
24717 width. */
24718 && temp_it.glyph_row->used[TEXT_AREA] > 0
24719 && (temp_it.glyph_row->reversed_p
24720 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24721 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24723 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24725 if (stretch_width > 0)
24727 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24728 struct font *font =
24729 face->font ? face->font : FRAME_FONT (temp_it.f);
24730 int stretch_ascent =
24731 (((temp_it.ascent + temp_it.descent)
24732 * FONT_BASE (font)) / FONT_HEIGHT (font));
24734 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24735 temp_it.ascent + temp_it.descent,
24736 stretch_ascent);
24739 #endif
24741 temp_it.dp = NULL;
24742 temp_it.what = IT_CHARACTER;
24743 temp_it.len = 1;
24744 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24745 temp_it.face_id = GLYPH_FACE (glyph);
24746 temp_it.len = CHAR_BYTES (temp_it.c);
24748 PRODUCE_GLYPHS (&temp_it);
24749 it->pixel_width = temp_it.pixel_width;
24750 it->nglyphs = temp_it.pixel_width;
24753 #ifdef HAVE_WINDOW_SYSTEM
24755 /* Calculate line-height and line-spacing properties.
24756 An integer value specifies explicit pixel value.
24757 A float value specifies relative value to current face height.
24758 A cons (float . face-name) specifies relative value to
24759 height of specified face font.
24761 Returns height in pixels, or nil. */
24764 static Lisp_Object
24765 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24766 int boff, int override)
24768 Lisp_Object face_name = Qnil;
24769 int ascent, descent, height;
24771 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24772 return val;
24774 if (CONSP (val))
24776 face_name = XCAR (val);
24777 val = XCDR (val);
24778 if (!NUMBERP (val))
24779 val = make_number (1);
24780 if (NILP (face_name))
24782 height = it->ascent + it->descent;
24783 goto scale;
24787 if (NILP (face_name))
24789 font = FRAME_FONT (it->f);
24790 boff = FRAME_BASELINE_OFFSET (it->f);
24792 else if (EQ (face_name, Qt))
24794 override = 0;
24796 else
24798 int face_id;
24799 struct face *face;
24801 face_id = lookup_named_face (it->f, face_name, 0);
24802 if (face_id < 0)
24803 return make_number (-1);
24805 face = FACE_FROM_ID (it->f, face_id);
24806 font = face->font;
24807 if (font == NULL)
24808 return make_number (-1);
24809 boff = font->baseline_offset;
24810 if (font->vertical_centering)
24811 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24814 ascent = FONT_BASE (font) + boff;
24815 descent = FONT_DESCENT (font) - boff;
24817 if (override)
24819 it->override_ascent = ascent;
24820 it->override_descent = descent;
24821 it->override_boff = boff;
24824 height = ascent + descent;
24826 scale:
24827 if (FLOATP (val))
24828 height = (int)(XFLOAT_DATA (val) * height);
24829 else if (INTEGERP (val))
24830 height *= XINT (val);
24832 return make_number (height);
24836 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24837 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24838 and only if this is for a character for which no font was found.
24840 If the display method (it->glyphless_method) is
24841 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24842 length of the acronym or the hexadecimal string, UPPER_XOFF and
24843 UPPER_YOFF are pixel offsets for the upper part of the string,
24844 LOWER_XOFF and LOWER_YOFF are for the lower part.
24846 For the other display methods, LEN through LOWER_YOFF are zero. */
24848 static void
24849 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24850 short upper_xoff, short upper_yoff,
24851 short lower_xoff, short lower_yoff)
24853 struct glyph *glyph;
24854 enum glyph_row_area area = it->area;
24856 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24857 if (glyph < it->glyph_row->glyphs[area + 1])
24859 /* If the glyph row is reversed, we need to prepend the glyph
24860 rather than append it. */
24861 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24863 struct glyph *g;
24865 /* Make room for the additional glyph. */
24866 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24867 g[1] = *g;
24868 glyph = it->glyph_row->glyphs[area];
24870 glyph->charpos = CHARPOS (it->position);
24871 glyph->object = it->object;
24872 glyph->pixel_width = it->pixel_width;
24873 glyph->ascent = it->ascent;
24874 glyph->descent = it->descent;
24875 glyph->voffset = it->voffset;
24876 glyph->type = GLYPHLESS_GLYPH;
24877 glyph->u.glyphless.method = it->glyphless_method;
24878 glyph->u.glyphless.for_no_font = for_no_font;
24879 glyph->u.glyphless.len = len;
24880 glyph->u.glyphless.ch = it->c;
24881 glyph->slice.glyphless.upper_xoff = upper_xoff;
24882 glyph->slice.glyphless.upper_yoff = upper_yoff;
24883 glyph->slice.glyphless.lower_xoff = lower_xoff;
24884 glyph->slice.glyphless.lower_yoff = lower_yoff;
24885 glyph->avoid_cursor_p = it->avoid_cursor_p;
24886 glyph->multibyte_p = it->multibyte_p;
24887 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24889 /* In R2L rows, the left and the right box edges need to be
24890 drawn in reverse direction. */
24891 glyph->right_box_line_p = it->start_of_box_run_p;
24892 glyph->left_box_line_p = it->end_of_box_run_p;
24894 else
24896 glyph->left_box_line_p = it->start_of_box_run_p;
24897 glyph->right_box_line_p = it->end_of_box_run_p;
24899 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24900 || it->phys_descent > it->descent);
24901 glyph->padding_p = 0;
24902 glyph->glyph_not_available_p = 0;
24903 glyph->face_id = face_id;
24904 glyph->font_type = FONT_TYPE_UNKNOWN;
24905 if (it->bidi_p)
24907 glyph->resolved_level = it->bidi_it.resolved_level;
24908 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24909 emacs_abort ();
24910 glyph->bidi_type = it->bidi_it.type;
24912 ++it->glyph_row->used[area];
24914 else
24915 IT_EXPAND_MATRIX_WIDTH (it, area);
24919 /* Produce a glyph for a glyphless character for iterator IT.
24920 IT->glyphless_method specifies which method to use for displaying
24921 the character. See the description of enum
24922 glyphless_display_method in dispextern.h for the detail.
24924 FOR_NO_FONT is nonzero if and only if this is for a character for
24925 which no font was found. ACRONYM, if non-nil, is an acronym string
24926 for the character. */
24928 static void
24929 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24931 int face_id;
24932 struct face *face;
24933 struct font *font;
24934 int base_width, base_height, width, height;
24935 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24936 int len;
24938 /* Get the metrics of the base font. We always refer to the current
24939 ASCII face. */
24940 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24941 font = face->font ? face->font : FRAME_FONT (it->f);
24942 it->ascent = FONT_BASE (font) + font->baseline_offset;
24943 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24944 base_height = it->ascent + it->descent;
24945 base_width = font->average_width;
24947 face_id = merge_glyphless_glyph_face (it);
24949 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24951 it->pixel_width = THIN_SPACE_WIDTH;
24952 len = 0;
24953 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24955 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24957 width = CHAR_WIDTH (it->c);
24958 if (width == 0)
24959 width = 1;
24960 else if (width > 4)
24961 width = 4;
24962 it->pixel_width = base_width * width;
24963 len = 0;
24964 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24966 else
24968 char buf[7];
24969 const char *str;
24970 unsigned int code[6];
24971 int upper_len;
24972 int ascent, descent;
24973 struct font_metrics metrics_upper, metrics_lower;
24975 face = FACE_FROM_ID (it->f, face_id);
24976 font = face->font ? face->font : FRAME_FONT (it->f);
24977 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24979 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24981 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24982 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24983 if (CONSP (acronym))
24984 acronym = XCAR (acronym);
24985 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24987 else
24989 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24990 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24991 str = buf;
24993 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24994 code[len] = font->driver->encode_char (font, str[len]);
24995 upper_len = (len + 1) / 2;
24996 font->driver->text_extents (font, code, upper_len,
24997 &metrics_upper);
24998 font->driver->text_extents (font, code + upper_len, len - upper_len,
24999 &metrics_lower);
25003 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25004 width = max (metrics_upper.width, metrics_lower.width) + 4;
25005 upper_xoff = upper_yoff = 2; /* the typical case */
25006 if (base_width >= width)
25008 /* Align the upper to the left, the lower to the right. */
25009 it->pixel_width = base_width;
25010 lower_xoff = base_width - 2 - metrics_lower.width;
25012 else
25014 /* Center the shorter one. */
25015 it->pixel_width = width;
25016 if (metrics_upper.width >= metrics_lower.width)
25017 lower_xoff = (width - metrics_lower.width) / 2;
25018 else
25020 /* FIXME: This code doesn't look right. It formerly was
25021 missing the "lower_xoff = 0;", which couldn't have
25022 been right since it left lower_xoff uninitialized. */
25023 lower_xoff = 0;
25024 upper_xoff = (width - metrics_upper.width) / 2;
25028 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25029 top, bottom, and between upper and lower strings. */
25030 height = (metrics_upper.ascent + metrics_upper.descent
25031 + metrics_lower.ascent + metrics_lower.descent) + 5;
25032 /* Center vertically.
25033 H:base_height, D:base_descent
25034 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25036 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25037 descent = D - H/2 + h/2;
25038 lower_yoff = descent - 2 - ld;
25039 upper_yoff = lower_yoff - la - 1 - ud; */
25040 ascent = - (it->descent - (base_height + height + 1) / 2);
25041 descent = it->descent - (base_height - height) / 2;
25042 lower_yoff = descent - 2 - metrics_lower.descent;
25043 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25044 - metrics_upper.descent);
25045 /* Don't make the height shorter than the base height. */
25046 if (height > base_height)
25048 it->ascent = ascent;
25049 it->descent = descent;
25053 it->phys_ascent = it->ascent;
25054 it->phys_descent = it->descent;
25055 if (it->glyph_row)
25056 append_glyphless_glyph (it, face_id, for_no_font, len,
25057 upper_xoff, upper_yoff,
25058 lower_xoff, lower_yoff);
25059 it->nglyphs = 1;
25060 take_vertical_position_into_account (it);
25064 /* RIF:
25065 Produce glyphs/get display metrics for the display element IT is
25066 loaded with. See the description of struct it in dispextern.h
25067 for an overview of struct it. */
25069 void
25070 x_produce_glyphs (struct it *it)
25072 int extra_line_spacing = it->extra_line_spacing;
25074 it->glyph_not_available_p = 0;
25076 if (it->what == IT_CHARACTER)
25078 XChar2b char2b;
25079 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25080 struct font *font = face->font;
25081 struct font_metrics *pcm = NULL;
25082 int boff; /* baseline offset */
25084 if (font == NULL)
25086 /* When no suitable font is found, display this character by
25087 the method specified in the first extra slot of
25088 Vglyphless_char_display. */
25089 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25091 eassert (it->what == IT_GLYPHLESS);
25092 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25093 goto done;
25096 boff = font->baseline_offset;
25097 if (font->vertical_centering)
25098 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25100 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25102 int stretched_p;
25104 it->nglyphs = 1;
25106 if (it->override_ascent >= 0)
25108 it->ascent = it->override_ascent;
25109 it->descent = it->override_descent;
25110 boff = it->override_boff;
25112 else
25114 it->ascent = FONT_BASE (font) + boff;
25115 it->descent = FONT_DESCENT (font) - boff;
25118 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25120 pcm = get_per_char_metric (font, &char2b);
25121 if (pcm->width == 0
25122 && pcm->rbearing == 0 && pcm->lbearing == 0)
25123 pcm = NULL;
25126 if (pcm)
25128 it->phys_ascent = pcm->ascent + boff;
25129 it->phys_descent = pcm->descent - boff;
25130 it->pixel_width = pcm->width;
25132 else
25134 it->glyph_not_available_p = 1;
25135 it->phys_ascent = it->ascent;
25136 it->phys_descent = it->descent;
25137 it->pixel_width = font->space_width;
25140 if (it->constrain_row_ascent_descent_p)
25142 if (it->descent > it->max_descent)
25144 it->ascent += it->descent - it->max_descent;
25145 it->descent = it->max_descent;
25147 if (it->ascent > it->max_ascent)
25149 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25150 it->ascent = it->max_ascent;
25152 it->phys_ascent = min (it->phys_ascent, it->ascent);
25153 it->phys_descent = min (it->phys_descent, it->descent);
25154 extra_line_spacing = 0;
25157 /* If this is a space inside a region of text with
25158 `space-width' property, change its width. */
25159 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25160 if (stretched_p)
25161 it->pixel_width *= XFLOATINT (it->space_width);
25163 /* If face has a box, add the box thickness to the character
25164 height. If character has a box line to the left and/or
25165 right, add the box line width to the character's width. */
25166 if (face->box != FACE_NO_BOX)
25168 int thick = face->box_line_width;
25170 if (thick > 0)
25172 it->ascent += thick;
25173 it->descent += thick;
25175 else
25176 thick = -thick;
25178 if (it->start_of_box_run_p)
25179 it->pixel_width += thick;
25180 if (it->end_of_box_run_p)
25181 it->pixel_width += thick;
25184 /* If face has an overline, add the height of the overline
25185 (1 pixel) and a 1 pixel margin to the character height. */
25186 if (face->overline_p)
25187 it->ascent += overline_margin;
25189 if (it->constrain_row_ascent_descent_p)
25191 if (it->ascent > it->max_ascent)
25192 it->ascent = it->max_ascent;
25193 if (it->descent > it->max_descent)
25194 it->descent = it->max_descent;
25197 take_vertical_position_into_account (it);
25199 /* If we have to actually produce glyphs, do it. */
25200 if (it->glyph_row)
25202 if (stretched_p)
25204 /* Translate a space with a `space-width' property
25205 into a stretch glyph. */
25206 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25207 / FONT_HEIGHT (font));
25208 append_stretch_glyph (it, it->object, it->pixel_width,
25209 it->ascent + it->descent, ascent);
25211 else
25212 append_glyph (it);
25214 /* If characters with lbearing or rbearing are displayed
25215 in this line, record that fact in a flag of the
25216 glyph row. This is used to optimize X output code. */
25217 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25218 it->glyph_row->contains_overlapping_glyphs_p = 1;
25220 if (! stretched_p && it->pixel_width == 0)
25221 /* We assure that all visible glyphs have at least 1-pixel
25222 width. */
25223 it->pixel_width = 1;
25225 else if (it->char_to_display == '\n')
25227 /* A newline has no width, but we need the height of the
25228 line. But if previous part of the line sets a height,
25229 don't increase that height */
25231 Lisp_Object height;
25232 Lisp_Object total_height = Qnil;
25234 it->override_ascent = -1;
25235 it->pixel_width = 0;
25236 it->nglyphs = 0;
25238 height = get_it_property (it, Qline_height);
25239 /* Split (line-height total-height) list */
25240 if (CONSP (height)
25241 && CONSP (XCDR (height))
25242 && NILP (XCDR (XCDR (height))))
25244 total_height = XCAR (XCDR (height));
25245 height = XCAR (height);
25247 height = calc_line_height_property (it, height, font, boff, 1);
25249 if (it->override_ascent >= 0)
25251 it->ascent = it->override_ascent;
25252 it->descent = it->override_descent;
25253 boff = it->override_boff;
25255 else
25257 it->ascent = FONT_BASE (font) + boff;
25258 it->descent = FONT_DESCENT (font) - boff;
25261 if (EQ (height, Qt))
25263 if (it->descent > it->max_descent)
25265 it->ascent += it->descent - it->max_descent;
25266 it->descent = it->max_descent;
25268 if (it->ascent > it->max_ascent)
25270 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25271 it->ascent = it->max_ascent;
25273 it->phys_ascent = min (it->phys_ascent, it->ascent);
25274 it->phys_descent = min (it->phys_descent, it->descent);
25275 it->constrain_row_ascent_descent_p = 1;
25276 extra_line_spacing = 0;
25278 else
25280 Lisp_Object spacing;
25282 it->phys_ascent = it->ascent;
25283 it->phys_descent = it->descent;
25285 if ((it->max_ascent > 0 || it->max_descent > 0)
25286 && face->box != FACE_NO_BOX
25287 && face->box_line_width > 0)
25289 it->ascent += face->box_line_width;
25290 it->descent += face->box_line_width;
25292 if (!NILP (height)
25293 && XINT (height) > it->ascent + it->descent)
25294 it->ascent = XINT (height) - it->descent;
25296 if (!NILP (total_height))
25297 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25298 else
25300 spacing = get_it_property (it, Qline_spacing);
25301 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25303 if (INTEGERP (spacing))
25305 extra_line_spacing = XINT (spacing);
25306 if (!NILP (total_height))
25307 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25311 else /* i.e. (it->char_to_display == '\t') */
25313 if (font->space_width > 0)
25315 int tab_width = it->tab_width * font->space_width;
25316 int x = it->current_x + it->continuation_lines_width;
25317 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25319 /* If the distance from the current position to the next tab
25320 stop is less than a space character width, use the
25321 tab stop after that. */
25322 if (next_tab_x - x < font->space_width)
25323 next_tab_x += tab_width;
25325 it->pixel_width = next_tab_x - x;
25326 it->nglyphs = 1;
25327 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25328 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25330 if (it->glyph_row)
25332 append_stretch_glyph (it, it->object, it->pixel_width,
25333 it->ascent + it->descent, it->ascent);
25336 else
25338 it->pixel_width = 0;
25339 it->nglyphs = 1;
25343 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25345 /* A static composition.
25347 Note: A composition is represented as one glyph in the
25348 glyph matrix. There are no padding glyphs.
25350 Important note: pixel_width, ascent, and descent are the
25351 values of what is drawn by draw_glyphs (i.e. the values of
25352 the overall glyphs composed). */
25353 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25354 int boff; /* baseline offset */
25355 struct composition *cmp = composition_table[it->cmp_it.id];
25356 int glyph_len = cmp->glyph_len;
25357 struct font *font = face->font;
25359 it->nglyphs = 1;
25361 /* If we have not yet calculated pixel size data of glyphs of
25362 the composition for the current face font, calculate them
25363 now. Theoretically, we have to check all fonts for the
25364 glyphs, but that requires much time and memory space. So,
25365 here we check only the font of the first glyph. This may
25366 lead to incorrect display, but it's very rare, and C-l
25367 (recenter-top-bottom) can correct the display anyway. */
25368 if (! cmp->font || cmp->font != font)
25370 /* Ascent and descent of the font of the first character
25371 of this composition (adjusted by baseline offset).
25372 Ascent and descent of overall glyphs should not be less
25373 than these, respectively. */
25374 int font_ascent, font_descent, font_height;
25375 /* Bounding box of the overall glyphs. */
25376 int leftmost, rightmost, lowest, highest;
25377 int lbearing, rbearing;
25378 int i, width, ascent, descent;
25379 int left_padded = 0, right_padded = 0;
25380 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25381 XChar2b char2b;
25382 struct font_metrics *pcm;
25383 int font_not_found_p;
25384 ptrdiff_t pos;
25386 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25387 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25388 break;
25389 if (glyph_len < cmp->glyph_len)
25390 right_padded = 1;
25391 for (i = 0; i < glyph_len; i++)
25393 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25394 break;
25395 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25397 if (i > 0)
25398 left_padded = 1;
25400 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25401 : IT_CHARPOS (*it));
25402 /* If no suitable font is found, use the default font. */
25403 font_not_found_p = font == NULL;
25404 if (font_not_found_p)
25406 face = face->ascii_face;
25407 font = face->font;
25409 boff = font->baseline_offset;
25410 if (font->vertical_centering)
25411 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25412 font_ascent = FONT_BASE (font) + boff;
25413 font_descent = FONT_DESCENT (font) - boff;
25414 font_height = FONT_HEIGHT (font);
25416 cmp->font = font;
25418 pcm = NULL;
25419 if (! font_not_found_p)
25421 get_char_face_and_encoding (it->f, c, it->face_id,
25422 &char2b, 0);
25423 pcm = get_per_char_metric (font, &char2b);
25426 /* Initialize the bounding box. */
25427 if (pcm)
25429 width = cmp->glyph_len > 0 ? pcm->width : 0;
25430 ascent = pcm->ascent;
25431 descent = pcm->descent;
25432 lbearing = pcm->lbearing;
25433 rbearing = pcm->rbearing;
25435 else
25437 width = cmp->glyph_len > 0 ? font->space_width : 0;
25438 ascent = FONT_BASE (font);
25439 descent = FONT_DESCENT (font);
25440 lbearing = 0;
25441 rbearing = width;
25444 rightmost = width;
25445 leftmost = 0;
25446 lowest = - descent + boff;
25447 highest = ascent + boff;
25449 if (! font_not_found_p
25450 && font->default_ascent
25451 && CHAR_TABLE_P (Vuse_default_ascent)
25452 && !NILP (Faref (Vuse_default_ascent,
25453 make_number (it->char_to_display))))
25454 highest = font->default_ascent + boff;
25456 /* Draw the first glyph at the normal position. It may be
25457 shifted to right later if some other glyphs are drawn
25458 at the left. */
25459 cmp->offsets[i * 2] = 0;
25460 cmp->offsets[i * 2 + 1] = boff;
25461 cmp->lbearing = lbearing;
25462 cmp->rbearing = rbearing;
25464 /* Set cmp->offsets for the remaining glyphs. */
25465 for (i++; i < glyph_len; i++)
25467 int left, right, btm, top;
25468 int ch = COMPOSITION_GLYPH (cmp, i);
25469 int face_id;
25470 struct face *this_face;
25472 if (ch == '\t')
25473 ch = ' ';
25474 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25475 this_face = FACE_FROM_ID (it->f, face_id);
25476 font = this_face->font;
25478 if (font == NULL)
25479 pcm = NULL;
25480 else
25482 get_char_face_and_encoding (it->f, ch, face_id,
25483 &char2b, 0);
25484 pcm = get_per_char_metric (font, &char2b);
25486 if (! pcm)
25487 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25488 else
25490 width = pcm->width;
25491 ascent = pcm->ascent;
25492 descent = pcm->descent;
25493 lbearing = pcm->lbearing;
25494 rbearing = pcm->rbearing;
25495 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25497 /* Relative composition with or without
25498 alternate chars. */
25499 left = (leftmost + rightmost - width) / 2;
25500 btm = - descent + boff;
25501 if (font->relative_compose
25502 && (! CHAR_TABLE_P (Vignore_relative_composition)
25503 || NILP (Faref (Vignore_relative_composition,
25504 make_number (ch)))))
25507 if (- descent >= font->relative_compose)
25508 /* One extra pixel between two glyphs. */
25509 btm = highest + 1;
25510 else if (ascent <= 0)
25511 /* One extra pixel between two glyphs. */
25512 btm = lowest - 1 - ascent - descent;
25515 else
25517 /* A composition rule is specified by an integer
25518 value that encodes global and new reference
25519 points (GREF and NREF). GREF and NREF are
25520 specified by numbers as below:
25522 0---1---2 -- ascent
25526 9--10--11 -- center
25528 ---3---4---5--- baseline
25530 6---7---8 -- descent
25532 int rule = COMPOSITION_RULE (cmp, i);
25533 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25535 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25536 grefx = gref % 3, nrefx = nref % 3;
25537 grefy = gref / 3, nrefy = nref / 3;
25538 if (xoff)
25539 xoff = font_height * (xoff - 128) / 256;
25540 if (yoff)
25541 yoff = font_height * (yoff - 128) / 256;
25543 left = (leftmost
25544 + grefx * (rightmost - leftmost) / 2
25545 - nrefx * width / 2
25546 + xoff);
25548 btm = ((grefy == 0 ? highest
25549 : grefy == 1 ? 0
25550 : grefy == 2 ? lowest
25551 : (highest + lowest) / 2)
25552 - (nrefy == 0 ? ascent + descent
25553 : nrefy == 1 ? descent - boff
25554 : nrefy == 2 ? 0
25555 : (ascent + descent) / 2)
25556 + yoff);
25559 cmp->offsets[i * 2] = left;
25560 cmp->offsets[i * 2 + 1] = btm + descent;
25562 /* Update the bounding box of the overall glyphs. */
25563 if (width > 0)
25565 right = left + width;
25566 if (left < leftmost)
25567 leftmost = left;
25568 if (right > rightmost)
25569 rightmost = right;
25571 top = btm + descent + ascent;
25572 if (top > highest)
25573 highest = top;
25574 if (btm < lowest)
25575 lowest = btm;
25577 if (cmp->lbearing > left + lbearing)
25578 cmp->lbearing = left + lbearing;
25579 if (cmp->rbearing < left + rbearing)
25580 cmp->rbearing = left + rbearing;
25584 /* If there are glyphs whose x-offsets are negative,
25585 shift all glyphs to the right and make all x-offsets
25586 non-negative. */
25587 if (leftmost < 0)
25589 for (i = 0; i < cmp->glyph_len; i++)
25590 cmp->offsets[i * 2] -= leftmost;
25591 rightmost -= leftmost;
25592 cmp->lbearing -= leftmost;
25593 cmp->rbearing -= leftmost;
25596 if (left_padded && cmp->lbearing < 0)
25598 for (i = 0; i < cmp->glyph_len; i++)
25599 cmp->offsets[i * 2] -= cmp->lbearing;
25600 rightmost -= cmp->lbearing;
25601 cmp->rbearing -= cmp->lbearing;
25602 cmp->lbearing = 0;
25604 if (right_padded && rightmost < cmp->rbearing)
25606 rightmost = cmp->rbearing;
25609 cmp->pixel_width = rightmost;
25610 cmp->ascent = highest;
25611 cmp->descent = - lowest;
25612 if (cmp->ascent < font_ascent)
25613 cmp->ascent = font_ascent;
25614 if (cmp->descent < font_descent)
25615 cmp->descent = font_descent;
25618 if (it->glyph_row
25619 && (cmp->lbearing < 0
25620 || cmp->rbearing > cmp->pixel_width))
25621 it->glyph_row->contains_overlapping_glyphs_p = 1;
25623 it->pixel_width = cmp->pixel_width;
25624 it->ascent = it->phys_ascent = cmp->ascent;
25625 it->descent = it->phys_descent = cmp->descent;
25626 if (face->box != FACE_NO_BOX)
25628 int thick = face->box_line_width;
25630 if (thick > 0)
25632 it->ascent += thick;
25633 it->descent += thick;
25635 else
25636 thick = - thick;
25638 if (it->start_of_box_run_p)
25639 it->pixel_width += thick;
25640 if (it->end_of_box_run_p)
25641 it->pixel_width += thick;
25644 /* If face has an overline, add the height of the overline
25645 (1 pixel) and a 1 pixel margin to the character height. */
25646 if (face->overline_p)
25647 it->ascent += overline_margin;
25649 take_vertical_position_into_account (it);
25650 if (it->ascent < 0)
25651 it->ascent = 0;
25652 if (it->descent < 0)
25653 it->descent = 0;
25655 if (it->glyph_row && cmp->glyph_len > 0)
25656 append_composite_glyph (it);
25658 else if (it->what == IT_COMPOSITION)
25660 /* A dynamic (automatic) composition. */
25661 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25662 Lisp_Object gstring;
25663 struct font_metrics metrics;
25665 it->nglyphs = 1;
25667 gstring = composition_gstring_from_id (it->cmp_it.id);
25668 it->pixel_width
25669 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25670 &metrics);
25671 if (it->glyph_row
25672 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25673 it->glyph_row->contains_overlapping_glyphs_p = 1;
25674 it->ascent = it->phys_ascent = metrics.ascent;
25675 it->descent = it->phys_descent = metrics.descent;
25676 if (face->box != FACE_NO_BOX)
25678 int thick = face->box_line_width;
25680 if (thick > 0)
25682 it->ascent += thick;
25683 it->descent += thick;
25685 else
25686 thick = - thick;
25688 if (it->start_of_box_run_p)
25689 it->pixel_width += thick;
25690 if (it->end_of_box_run_p)
25691 it->pixel_width += thick;
25693 /* If face has an overline, add the height of the overline
25694 (1 pixel) and a 1 pixel margin to the character height. */
25695 if (face->overline_p)
25696 it->ascent += overline_margin;
25697 take_vertical_position_into_account (it);
25698 if (it->ascent < 0)
25699 it->ascent = 0;
25700 if (it->descent < 0)
25701 it->descent = 0;
25703 if (it->glyph_row)
25704 append_composite_glyph (it);
25706 else if (it->what == IT_GLYPHLESS)
25707 produce_glyphless_glyph (it, 0, Qnil);
25708 else if (it->what == IT_IMAGE)
25709 produce_image_glyph (it);
25710 else if (it->what == IT_STRETCH)
25711 produce_stretch_glyph (it);
25713 done:
25714 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25715 because this isn't true for images with `:ascent 100'. */
25716 eassert (it->ascent >= 0 && it->descent >= 0);
25717 if (it->area == TEXT_AREA)
25718 it->current_x += it->pixel_width;
25720 if (extra_line_spacing > 0)
25722 it->descent += extra_line_spacing;
25723 if (extra_line_spacing > it->max_extra_line_spacing)
25724 it->max_extra_line_spacing = extra_line_spacing;
25727 it->max_ascent = max (it->max_ascent, it->ascent);
25728 it->max_descent = max (it->max_descent, it->descent);
25729 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25730 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25733 /* EXPORT for RIF:
25734 Output LEN glyphs starting at START at the nominal cursor position.
25735 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25736 being updated, and UPDATED_AREA is the area of that row being updated. */
25738 void
25739 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25740 struct glyph *start, enum glyph_row_area updated_area, int len)
25742 int x, hpos, chpos = w->phys_cursor.hpos;
25744 eassert (updated_row);
25745 /* When the window is hscrolled, cursor hpos can legitimately be out
25746 of bounds, but we draw the cursor at the corresponding window
25747 margin in that case. */
25748 if (!updated_row->reversed_p && chpos < 0)
25749 chpos = 0;
25750 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25751 chpos = updated_row->used[TEXT_AREA] - 1;
25753 block_input ();
25755 /* Write glyphs. */
25757 hpos = start - updated_row->glyphs[updated_area];
25758 x = draw_glyphs (w, w->output_cursor.x,
25759 updated_row, updated_area,
25760 hpos, hpos + len,
25761 DRAW_NORMAL_TEXT, 0);
25763 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25764 if (updated_area == TEXT_AREA
25765 && w->phys_cursor_on_p
25766 && w->phys_cursor.vpos == w->output_cursor.vpos
25767 && chpos >= hpos
25768 && chpos < hpos + len)
25769 w->phys_cursor_on_p = 0;
25771 unblock_input ();
25773 /* Advance the output cursor. */
25774 w->output_cursor.hpos += len;
25775 w->output_cursor.x = x;
25779 /* EXPORT for RIF:
25780 Insert LEN glyphs from START at the nominal cursor position. */
25782 void
25783 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25784 struct glyph *start, enum glyph_row_area updated_area, int len)
25786 struct frame *f;
25787 int line_height, shift_by_width, shifted_region_width;
25788 struct glyph_row *row;
25789 struct glyph *glyph;
25790 int frame_x, frame_y;
25791 ptrdiff_t hpos;
25793 eassert (updated_row);
25794 block_input ();
25795 f = XFRAME (WINDOW_FRAME (w));
25797 /* Get the height of the line we are in. */
25798 row = updated_row;
25799 line_height = row->height;
25801 /* Get the width of the glyphs to insert. */
25802 shift_by_width = 0;
25803 for (glyph = start; glyph < start + len; ++glyph)
25804 shift_by_width += glyph->pixel_width;
25806 /* Get the width of the region to shift right. */
25807 shifted_region_width = (window_box_width (w, updated_area)
25808 - w->output_cursor.x
25809 - shift_by_width);
25811 /* Shift right. */
25812 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25813 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25815 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25816 line_height, shift_by_width);
25818 /* Write the glyphs. */
25819 hpos = start - row->glyphs[updated_area];
25820 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25821 hpos, hpos + len,
25822 DRAW_NORMAL_TEXT, 0);
25824 /* Advance the output cursor. */
25825 w->output_cursor.hpos += len;
25826 w->output_cursor.x += shift_by_width;
25827 unblock_input ();
25831 /* EXPORT for RIF:
25832 Erase the current text line from the nominal cursor position
25833 (inclusive) to pixel column TO_X (exclusive). The idea is that
25834 everything from TO_X onward is already erased.
25836 TO_X is a pixel position relative to UPDATED_AREA of currently
25837 updated window W. TO_X == -1 means clear to the end of this area. */
25839 void
25840 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25841 enum glyph_row_area updated_area, int to_x)
25843 struct frame *f;
25844 int max_x, min_y, max_y;
25845 int from_x, from_y, to_y;
25847 eassert (updated_row);
25848 f = XFRAME (w->frame);
25850 if (updated_row->full_width_p)
25851 max_x = WINDOW_TOTAL_WIDTH (w);
25852 else
25853 max_x = window_box_width (w, updated_area);
25854 max_y = window_text_bottom_y (w);
25856 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25857 of window. For TO_X > 0, truncate to end of drawing area. */
25858 if (to_x == 0)
25859 return;
25860 else if (to_x < 0)
25861 to_x = max_x;
25862 else
25863 to_x = min (to_x, max_x);
25865 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25867 /* Notice if the cursor will be cleared by this operation. */
25868 if (!updated_row->full_width_p)
25869 notice_overwritten_cursor (w, updated_area,
25870 w->output_cursor.x, -1,
25871 updated_row->y,
25872 MATRIX_ROW_BOTTOM_Y (updated_row));
25874 from_x = w->output_cursor.x;
25876 /* Translate to frame coordinates. */
25877 if (updated_row->full_width_p)
25879 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25880 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25882 else
25884 int area_left = window_box_left (w, updated_area);
25885 from_x += area_left;
25886 to_x += area_left;
25889 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25890 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25891 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25893 /* Prevent inadvertently clearing to end of the X window. */
25894 if (to_x > from_x && to_y > from_y)
25896 block_input ();
25897 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25898 to_x - from_x, to_y - from_y);
25899 unblock_input ();
25903 #endif /* HAVE_WINDOW_SYSTEM */
25907 /***********************************************************************
25908 Cursor types
25909 ***********************************************************************/
25911 /* Value is the internal representation of the specified cursor type
25912 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25913 of the bar cursor. */
25915 static enum text_cursor_kinds
25916 get_specified_cursor_type (Lisp_Object arg, int *width)
25918 enum text_cursor_kinds type;
25920 if (NILP (arg))
25921 return NO_CURSOR;
25923 if (EQ (arg, Qbox))
25924 return FILLED_BOX_CURSOR;
25926 if (EQ (arg, Qhollow))
25927 return HOLLOW_BOX_CURSOR;
25929 if (EQ (arg, Qbar))
25931 *width = 2;
25932 return BAR_CURSOR;
25935 if (CONSP (arg)
25936 && EQ (XCAR (arg), Qbar)
25937 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25939 *width = XINT (XCDR (arg));
25940 return BAR_CURSOR;
25943 if (EQ (arg, Qhbar))
25945 *width = 2;
25946 return HBAR_CURSOR;
25949 if (CONSP (arg)
25950 && EQ (XCAR (arg), Qhbar)
25951 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25953 *width = XINT (XCDR (arg));
25954 return HBAR_CURSOR;
25957 /* Treat anything unknown as "hollow box cursor".
25958 It was bad to signal an error; people have trouble fixing
25959 .Xdefaults with Emacs, when it has something bad in it. */
25960 type = HOLLOW_BOX_CURSOR;
25962 return type;
25965 /* Set the default cursor types for specified frame. */
25966 void
25967 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25969 int width = 1;
25970 Lisp_Object tem;
25972 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25973 FRAME_CURSOR_WIDTH (f) = width;
25975 /* By default, set up the blink-off state depending on the on-state. */
25977 tem = Fassoc (arg, Vblink_cursor_alist);
25978 if (!NILP (tem))
25980 FRAME_BLINK_OFF_CURSOR (f)
25981 = get_specified_cursor_type (XCDR (tem), &width);
25982 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25984 else
25985 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25987 /* Make sure the cursor gets redrawn. */
25988 f->cursor_type_changed = 1;
25992 #ifdef HAVE_WINDOW_SYSTEM
25994 /* Return the cursor we want to be displayed in window W. Return
25995 width of bar/hbar cursor through WIDTH arg. Return with
25996 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25997 (i.e. if the `system caret' should track this cursor).
25999 In a mini-buffer window, we want the cursor only to appear if we
26000 are reading input from this window. For the selected window, we
26001 want the cursor type given by the frame parameter or buffer local
26002 setting of cursor-type. If explicitly marked off, draw no cursor.
26003 In all other cases, we want a hollow box cursor. */
26005 static enum text_cursor_kinds
26006 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26007 int *active_cursor)
26009 struct frame *f = XFRAME (w->frame);
26010 struct buffer *b = XBUFFER (w->contents);
26011 int cursor_type = DEFAULT_CURSOR;
26012 Lisp_Object alt_cursor;
26013 int non_selected = 0;
26015 *active_cursor = 1;
26017 /* Echo area */
26018 if (cursor_in_echo_area
26019 && FRAME_HAS_MINIBUF_P (f)
26020 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26022 if (w == XWINDOW (echo_area_window))
26024 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26026 *width = FRAME_CURSOR_WIDTH (f);
26027 return FRAME_DESIRED_CURSOR (f);
26029 else
26030 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26033 *active_cursor = 0;
26034 non_selected = 1;
26037 /* Detect a nonselected window or nonselected frame. */
26038 else if (w != XWINDOW (f->selected_window)
26039 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26041 *active_cursor = 0;
26043 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26044 return NO_CURSOR;
26046 non_selected = 1;
26049 /* Never display a cursor in a window in which cursor-type is nil. */
26050 if (NILP (BVAR (b, cursor_type)))
26051 return NO_CURSOR;
26053 /* Get the normal cursor type for this window. */
26054 if (EQ (BVAR (b, cursor_type), Qt))
26056 cursor_type = FRAME_DESIRED_CURSOR (f);
26057 *width = FRAME_CURSOR_WIDTH (f);
26059 else
26060 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26062 /* Use cursor-in-non-selected-windows instead
26063 for non-selected window or frame. */
26064 if (non_selected)
26066 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26067 if (!EQ (Qt, alt_cursor))
26068 return get_specified_cursor_type (alt_cursor, width);
26069 /* t means modify the normal cursor type. */
26070 if (cursor_type == FILLED_BOX_CURSOR)
26071 cursor_type = HOLLOW_BOX_CURSOR;
26072 else if (cursor_type == BAR_CURSOR && *width > 1)
26073 --*width;
26074 return cursor_type;
26077 /* Use normal cursor if not blinked off. */
26078 if (!w->cursor_off_p)
26080 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26082 if (cursor_type == FILLED_BOX_CURSOR)
26084 /* Using a block cursor on large images can be very annoying.
26085 So use a hollow cursor for "large" images.
26086 If image is not transparent (no mask), also use hollow cursor. */
26087 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26088 if (img != NULL && IMAGEP (img->spec))
26090 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26091 where N = size of default frame font size.
26092 This should cover most of the "tiny" icons people may use. */
26093 if (!img->mask
26094 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26095 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26096 cursor_type = HOLLOW_BOX_CURSOR;
26099 else if (cursor_type != NO_CURSOR)
26101 /* Display current only supports BOX and HOLLOW cursors for images.
26102 So for now, unconditionally use a HOLLOW cursor when cursor is
26103 not a solid box cursor. */
26104 cursor_type = HOLLOW_BOX_CURSOR;
26107 return cursor_type;
26110 /* Cursor is blinked off, so determine how to "toggle" it. */
26112 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26113 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26114 return get_specified_cursor_type (XCDR (alt_cursor), width);
26116 /* Then see if frame has specified a specific blink off cursor type. */
26117 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26119 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26120 return FRAME_BLINK_OFF_CURSOR (f);
26123 #if 0
26124 /* Some people liked having a permanently visible blinking cursor,
26125 while others had very strong opinions against it. So it was
26126 decided to remove it. KFS 2003-09-03 */
26128 /* Finally perform built-in cursor blinking:
26129 filled box <-> hollow box
26130 wide [h]bar <-> narrow [h]bar
26131 narrow [h]bar <-> no cursor
26132 other type <-> no cursor */
26134 if (cursor_type == FILLED_BOX_CURSOR)
26135 return HOLLOW_BOX_CURSOR;
26137 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26139 *width = 1;
26140 return cursor_type;
26142 #endif
26144 return NO_CURSOR;
26148 /* Notice when the text cursor of window W has been completely
26149 overwritten by a drawing operation that outputs glyphs in AREA
26150 starting at X0 and ending at X1 in the line starting at Y0 and
26151 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26152 the rest of the line after X0 has been written. Y coordinates
26153 are window-relative. */
26155 static void
26156 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26157 int x0, int x1, int y0, int y1)
26159 int cx0, cx1, cy0, cy1;
26160 struct glyph_row *row;
26162 if (!w->phys_cursor_on_p)
26163 return;
26164 if (area != TEXT_AREA)
26165 return;
26167 if (w->phys_cursor.vpos < 0
26168 || w->phys_cursor.vpos >= w->current_matrix->nrows
26169 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26170 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26171 return;
26173 if (row->cursor_in_fringe_p)
26175 row->cursor_in_fringe_p = 0;
26176 draw_fringe_bitmap (w, row, row->reversed_p);
26177 w->phys_cursor_on_p = 0;
26178 return;
26181 cx0 = w->phys_cursor.x;
26182 cx1 = cx0 + w->phys_cursor_width;
26183 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26184 return;
26186 /* The cursor image will be completely removed from the
26187 screen if the output area intersects the cursor area in
26188 y-direction. When we draw in [y0 y1[, and some part of
26189 the cursor is at y < y0, that part must have been drawn
26190 before. When scrolling, the cursor is erased before
26191 actually scrolling, so we don't come here. When not
26192 scrolling, the rows above the old cursor row must have
26193 changed, and in this case these rows must have written
26194 over the cursor image.
26196 Likewise if part of the cursor is below y1, with the
26197 exception of the cursor being in the first blank row at
26198 the buffer and window end because update_text_area
26199 doesn't draw that row. (Except when it does, but
26200 that's handled in update_text_area.) */
26202 cy0 = w->phys_cursor.y;
26203 cy1 = cy0 + w->phys_cursor_height;
26204 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26205 return;
26207 w->phys_cursor_on_p = 0;
26210 #endif /* HAVE_WINDOW_SYSTEM */
26213 /************************************************************************
26214 Mouse Face
26215 ************************************************************************/
26217 #ifdef HAVE_WINDOW_SYSTEM
26219 /* EXPORT for RIF:
26220 Fix the display of area AREA of overlapping row ROW in window W
26221 with respect to the overlapping part OVERLAPS. */
26223 void
26224 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26225 enum glyph_row_area area, int overlaps)
26227 int i, x;
26229 block_input ();
26231 x = 0;
26232 for (i = 0; i < row->used[area];)
26234 if (row->glyphs[area][i].overlaps_vertically_p)
26236 int start = i, start_x = x;
26240 x += row->glyphs[area][i].pixel_width;
26241 ++i;
26243 while (i < row->used[area]
26244 && row->glyphs[area][i].overlaps_vertically_p);
26246 draw_glyphs (w, start_x, row, area,
26247 start, i,
26248 DRAW_NORMAL_TEXT, overlaps);
26250 else
26252 x += row->glyphs[area][i].pixel_width;
26253 ++i;
26257 unblock_input ();
26261 /* EXPORT:
26262 Draw the cursor glyph of window W in glyph row ROW. See the
26263 comment of draw_glyphs for the meaning of HL. */
26265 void
26266 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26267 enum draw_glyphs_face hl)
26269 /* If cursor hpos is out of bounds, don't draw garbage. This can
26270 happen in mini-buffer windows when switching between echo area
26271 glyphs and mini-buffer. */
26272 if ((row->reversed_p
26273 ? (w->phys_cursor.hpos >= 0)
26274 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26276 int on_p = w->phys_cursor_on_p;
26277 int x1;
26278 int hpos = w->phys_cursor.hpos;
26280 /* When the window is hscrolled, cursor hpos can legitimately be
26281 out of bounds, but we draw the cursor at the corresponding
26282 window margin in that case. */
26283 if (!row->reversed_p && hpos < 0)
26284 hpos = 0;
26285 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26286 hpos = row->used[TEXT_AREA] - 1;
26288 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26289 hl, 0);
26290 w->phys_cursor_on_p = on_p;
26292 if (hl == DRAW_CURSOR)
26293 w->phys_cursor_width = x1 - w->phys_cursor.x;
26294 /* When we erase the cursor, and ROW is overlapped by other
26295 rows, make sure that these overlapping parts of other rows
26296 are redrawn. */
26297 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26299 w->phys_cursor_width = x1 - w->phys_cursor.x;
26301 if (row > w->current_matrix->rows
26302 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26303 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26304 OVERLAPS_ERASED_CURSOR);
26306 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26307 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26308 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26309 OVERLAPS_ERASED_CURSOR);
26315 /* Erase the image of a cursor of window W from the screen. */
26317 #ifndef WINDOWSNT
26318 static
26319 #endif
26320 void
26321 erase_phys_cursor (struct window *w)
26323 struct frame *f = XFRAME (w->frame);
26324 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26325 int hpos = w->phys_cursor.hpos;
26326 int vpos = w->phys_cursor.vpos;
26327 int mouse_face_here_p = 0;
26328 struct glyph_matrix *active_glyphs = w->current_matrix;
26329 struct glyph_row *cursor_row;
26330 struct glyph *cursor_glyph;
26331 enum draw_glyphs_face hl;
26333 /* No cursor displayed or row invalidated => nothing to do on the
26334 screen. */
26335 if (w->phys_cursor_type == NO_CURSOR)
26336 goto mark_cursor_off;
26338 /* VPOS >= active_glyphs->nrows means that window has been resized.
26339 Don't bother to erase the cursor. */
26340 if (vpos >= active_glyphs->nrows)
26341 goto mark_cursor_off;
26343 /* If row containing cursor is marked invalid, there is nothing we
26344 can do. */
26345 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26346 if (!cursor_row->enabled_p)
26347 goto mark_cursor_off;
26349 /* If line spacing is > 0, old cursor may only be partially visible in
26350 window after split-window. So adjust visible height. */
26351 cursor_row->visible_height = min (cursor_row->visible_height,
26352 window_text_bottom_y (w) - cursor_row->y);
26354 /* If row is completely invisible, don't attempt to delete a cursor which
26355 isn't there. This can happen if cursor is at top of a window, and
26356 we switch to a buffer with a header line in that window. */
26357 if (cursor_row->visible_height <= 0)
26358 goto mark_cursor_off;
26360 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26361 if (cursor_row->cursor_in_fringe_p)
26363 cursor_row->cursor_in_fringe_p = 0;
26364 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26365 goto mark_cursor_off;
26368 /* This can happen when the new row is shorter than the old one.
26369 In this case, either draw_glyphs or clear_end_of_line
26370 should have cleared the cursor. Note that we wouldn't be
26371 able to erase the cursor in this case because we don't have a
26372 cursor glyph at hand. */
26373 if ((cursor_row->reversed_p
26374 ? (w->phys_cursor.hpos < 0)
26375 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26376 goto mark_cursor_off;
26378 /* When the window is hscrolled, cursor hpos can legitimately be out
26379 of bounds, but we draw the cursor at the corresponding window
26380 margin in that case. */
26381 if (!cursor_row->reversed_p && hpos < 0)
26382 hpos = 0;
26383 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26384 hpos = cursor_row->used[TEXT_AREA] - 1;
26386 /* If the cursor is in the mouse face area, redisplay that when
26387 we clear the cursor. */
26388 if (! NILP (hlinfo->mouse_face_window)
26389 && coords_in_mouse_face_p (w, hpos, vpos)
26390 /* Don't redraw the cursor's spot in mouse face if it is at the
26391 end of a line (on a newline). The cursor appears there, but
26392 mouse highlighting does not. */
26393 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26394 mouse_face_here_p = 1;
26396 /* Maybe clear the display under the cursor. */
26397 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26399 int x, y, left_x;
26400 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26401 int width;
26403 cursor_glyph = get_phys_cursor_glyph (w);
26404 if (cursor_glyph == NULL)
26405 goto mark_cursor_off;
26407 width = cursor_glyph->pixel_width;
26408 left_x = window_box_left_offset (w, TEXT_AREA);
26409 x = w->phys_cursor.x;
26410 if (x < left_x)
26411 width -= left_x - x;
26412 width = min (width, window_box_width (w, TEXT_AREA) - x);
26413 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26414 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26416 if (width > 0)
26417 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26420 /* Erase the cursor by redrawing the character underneath it. */
26421 if (mouse_face_here_p)
26422 hl = DRAW_MOUSE_FACE;
26423 else
26424 hl = DRAW_NORMAL_TEXT;
26425 draw_phys_cursor_glyph (w, cursor_row, hl);
26427 mark_cursor_off:
26428 w->phys_cursor_on_p = 0;
26429 w->phys_cursor_type = NO_CURSOR;
26433 /* EXPORT:
26434 Display or clear cursor of window W. If ON is zero, clear the
26435 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26436 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26438 void
26439 display_and_set_cursor (struct window *w, bool on,
26440 int hpos, int vpos, int x, int y)
26442 struct frame *f = XFRAME (w->frame);
26443 int new_cursor_type;
26444 int new_cursor_width;
26445 int active_cursor;
26446 struct glyph_row *glyph_row;
26447 struct glyph *glyph;
26449 /* This is pointless on invisible frames, and dangerous on garbaged
26450 windows and frames; in the latter case, the frame or window may
26451 be in the midst of changing its size, and x and y may be off the
26452 window. */
26453 if (! FRAME_VISIBLE_P (f)
26454 || FRAME_GARBAGED_P (f)
26455 || vpos >= w->current_matrix->nrows
26456 || hpos >= w->current_matrix->matrix_w)
26457 return;
26459 /* If cursor is off and we want it off, return quickly. */
26460 if (!on && !w->phys_cursor_on_p)
26461 return;
26463 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26464 /* If cursor row is not enabled, we don't really know where to
26465 display the cursor. */
26466 if (!glyph_row->enabled_p)
26468 w->phys_cursor_on_p = 0;
26469 return;
26472 glyph = NULL;
26473 if (!glyph_row->exact_window_width_line_p
26474 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26475 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26477 eassert (input_blocked_p ());
26479 /* Set new_cursor_type to the cursor we want to be displayed. */
26480 new_cursor_type = get_window_cursor_type (w, glyph,
26481 &new_cursor_width, &active_cursor);
26483 /* If cursor is currently being shown and we don't want it to be or
26484 it is in the wrong place, or the cursor type is not what we want,
26485 erase it. */
26486 if (w->phys_cursor_on_p
26487 && (!on
26488 || w->phys_cursor.x != x
26489 || w->phys_cursor.y != y
26490 || new_cursor_type != w->phys_cursor_type
26491 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26492 && new_cursor_width != w->phys_cursor_width)))
26493 erase_phys_cursor (w);
26495 /* Don't check phys_cursor_on_p here because that flag is only set
26496 to zero in some cases where we know that the cursor has been
26497 completely erased, to avoid the extra work of erasing the cursor
26498 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26499 still not be visible, or it has only been partly erased. */
26500 if (on)
26502 w->phys_cursor_ascent = glyph_row->ascent;
26503 w->phys_cursor_height = glyph_row->height;
26505 /* Set phys_cursor_.* before x_draw_.* is called because some
26506 of them may need the information. */
26507 w->phys_cursor.x = x;
26508 w->phys_cursor.y = glyph_row->y;
26509 w->phys_cursor.hpos = hpos;
26510 w->phys_cursor.vpos = vpos;
26513 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26514 new_cursor_type, new_cursor_width,
26515 on, active_cursor);
26519 /* Switch the display of W's cursor on or off, according to the value
26520 of ON. */
26522 static void
26523 update_window_cursor (struct window *w, bool on)
26525 /* Don't update cursor in windows whose frame is in the process
26526 of being deleted. */
26527 if (w->current_matrix)
26529 int hpos = w->phys_cursor.hpos;
26530 int vpos = w->phys_cursor.vpos;
26531 struct glyph_row *row;
26533 if (vpos >= w->current_matrix->nrows
26534 || hpos >= w->current_matrix->matrix_w)
26535 return;
26537 row = MATRIX_ROW (w->current_matrix, vpos);
26539 /* When the window is hscrolled, cursor hpos can legitimately be
26540 out of bounds, but we draw the cursor at the corresponding
26541 window margin in that case. */
26542 if (!row->reversed_p && hpos < 0)
26543 hpos = 0;
26544 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26545 hpos = row->used[TEXT_AREA] - 1;
26547 block_input ();
26548 display_and_set_cursor (w, on, hpos, vpos,
26549 w->phys_cursor.x, w->phys_cursor.y);
26550 unblock_input ();
26555 /* Call update_window_cursor with parameter ON_P on all leaf windows
26556 in the window tree rooted at W. */
26558 static void
26559 update_cursor_in_window_tree (struct window *w, bool on_p)
26561 while (w)
26563 if (WINDOWP (w->contents))
26564 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26565 else
26566 update_window_cursor (w, on_p);
26568 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26573 /* EXPORT:
26574 Display the cursor on window W, or clear it, according to ON_P.
26575 Don't change the cursor's position. */
26577 void
26578 x_update_cursor (struct frame *f, bool on_p)
26580 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26584 /* EXPORT:
26585 Clear the cursor of window W to background color, and mark the
26586 cursor as not shown. This is used when the text where the cursor
26587 is about to be rewritten. */
26589 void
26590 x_clear_cursor (struct window *w)
26592 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26593 update_window_cursor (w, 0);
26596 #endif /* HAVE_WINDOW_SYSTEM */
26598 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26599 and MSDOS. */
26600 static void
26601 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26602 int start_hpos, int end_hpos,
26603 enum draw_glyphs_face draw)
26605 #ifdef HAVE_WINDOW_SYSTEM
26606 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26608 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26609 return;
26611 #endif
26612 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26613 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26614 #endif
26617 /* Display the active region described by mouse_face_* according to DRAW. */
26619 static void
26620 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26622 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26623 struct frame *f = XFRAME (WINDOW_FRAME (w));
26625 if (/* If window is in the process of being destroyed, don't bother
26626 to do anything. */
26627 w->current_matrix != NULL
26628 /* Don't update mouse highlight if hidden */
26629 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26630 /* Recognize when we are called to operate on rows that don't exist
26631 anymore. This can happen when a window is split. */
26632 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26634 int phys_cursor_on_p = w->phys_cursor_on_p;
26635 struct glyph_row *row, *first, *last;
26637 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26638 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26640 for (row = first; row <= last && row->enabled_p; ++row)
26642 int start_hpos, end_hpos, start_x;
26644 /* For all but the first row, the highlight starts at column 0. */
26645 if (row == first)
26647 /* R2L rows have BEG and END in reversed order, but the
26648 screen drawing geometry is always left to right. So
26649 we need to mirror the beginning and end of the
26650 highlighted area in R2L rows. */
26651 if (!row->reversed_p)
26653 start_hpos = hlinfo->mouse_face_beg_col;
26654 start_x = hlinfo->mouse_face_beg_x;
26656 else if (row == last)
26658 start_hpos = hlinfo->mouse_face_end_col;
26659 start_x = hlinfo->mouse_face_end_x;
26661 else
26663 start_hpos = 0;
26664 start_x = 0;
26667 else if (row->reversed_p && row == last)
26669 start_hpos = hlinfo->mouse_face_end_col;
26670 start_x = hlinfo->mouse_face_end_x;
26672 else
26674 start_hpos = 0;
26675 start_x = 0;
26678 if (row == last)
26680 if (!row->reversed_p)
26681 end_hpos = hlinfo->mouse_face_end_col;
26682 else if (row == first)
26683 end_hpos = hlinfo->mouse_face_beg_col;
26684 else
26686 end_hpos = row->used[TEXT_AREA];
26687 if (draw == DRAW_NORMAL_TEXT)
26688 row->fill_line_p = 1; /* Clear to end of line */
26691 else if (row->reversed_p && row == first)
26692 end_hpos = hlinfo->mouse_face_beg_col;
26693 else
26695 end_hpos = row->used[TEXT_AREA];
26696 if (draw == DRAW_NORMAL_TEXT)
26697 row->fill_line_p = 1; /* Clear to end of line */
26700 if (end_hpos > start_hpos)
26702 draw_row_with_mouse_face (w, start_x, row,
26703 start_hpos, end_hpos, draw);
26705 row->mouse_face_p
26706 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26710 #ifdef HAVE_WINDOW_SYSTEM
26711 /* When we've written over the cursor, arrange for it to
26712 be displayed again. */
26713 if (FRAME_WINDOW_P (f)
26714 && phys_cursor_on_p && !w->phys_cursor_on_p)
26716 int hpos = w->phys_cursor.hpos;
26718 /* When the window is hscrolled, cursor hpos can legitimately be
26719 out of bounds, but we draw the cursor at the corresponding
26720 window margin in that case. */
26721 if (!row->reversed_p && hpos < 0)
26722 hpos = 0;
26723 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26724 hpos = row->used[TEXT_AREA] - 1;
26726 block_input ();
26727 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26728 w->phys_cursor.x, w->phys_cursor.y);
26729 unblock_input ();
26731 #endif /* HAVE_WINDOW_SYSTEM */
26734 #ifdef HAVE_WINDOW_SYSTEM
26735 /* Change the mouse cursor. */
26736 if (FRAME_WINDOW_P (f))
26738 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
26739 if (draw == DRAW_NORMAL_TEXT
26740 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26741 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26742 else
26743 #endif
26744 if (draw == DRAW_MOUSE_FACE)
26745 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26746 else
26747 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26749 #endif /* HAVE_WINDOW_SYSTEM */
26752 /* EXPORT:
26753 Clear out the mouse-highlighted active region.
26754 Redraw it un-highlighted first. Value is non-zero if mouse
26755 face was actually drawn unhighlighted. */
26758 clear_mouse_face (Mouse_HLInfo *hlinfo)
26760 int cleared = 0;
26762 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26764 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26765 cleared = 1;
26768 reset_mouse_highlight (hlinfo);
26769 return cleared;
26772 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26773 within the mouse face on that window. */
26774 static int
26775 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26777 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26779 /* Quickly resolve the easy cases. */
26780 if (!(WINDOWP (hlinfo->mouse_face_window)
26781 && XWINDOW (hlinfo->mouse_face_window) == w))
26782 return 0;
26783 if (vpos < hlinfo->mouse_face_beg_row
26784 || vpos > hlinfo->mouse_face_end_row)
26785 return 0;
26786 if (vpos > hlinfo->mouse_face_beg_row
26787 && vpos < hlinfo->mouse_face_end_row)
26788 return 1;
26790 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26792 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26794 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26795 return 1;
26797 else if ((vpos == hlinfo->mouse_face_beg_row
26798 && hpos >= hlinfo->mouse_face_beg_col)
26799 || (vpos == hlinfo->mouse_face_end_row
26800 && hpos < hlinfo->mouse_face_end_col))
26801 return 1;
26803 else
26805 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26807 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26808 return 1;
26810 else if ((vpos == hlinfo->mouse_face_beg_row
26811 && hpos <= hlinfo->mouse_face_beg_col)
26812 || (vpos == hlinfo->mouse_face_end_row
26813 && hpos > hlinfo->mouse_face_end_col))
26814 return 1;
26816 return 0;
26820 /* EXPORT:
26821 Non-zero if physical cursor of window W is within mouse face. */
26824 cursor_in_mouse_face_p (struct window *w)
26826 int hpos = w->phys_cursor.hpos;
26827 int vpos = w->phys_cursor.vpos;
26828 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26830 /* When the window is hscrolled, cursor hpos can legitimately be out
26831 of bounds, but we draw the cursor at the corresponding window
26832 margin in that case. */
26833 if (!row->reversed_p && hpos < 0)
26834 hpos = 0;
26835 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26836 hpos = row->used[TEXT_AREA] - 1;
26838 return coords_in_mouse_face_p (w, hpos, vpos);
26843 /* Find the glyph rows START_ROW and END_ROW of window W that display
26844 characters between buffer positions START_CHARPOS and END_CHARPOS
26845 (excluding END_CHARPOS). DISP_STRING is a display string that
26846 covers these buffer positions. This is similar to
26847 row_containing_pos, but is more accurate when bidi reordering makes
26848 buffer positions change non-linearly with glyph rows. */
26849 static void
26850 rows_from_pos_range (struct window *w,
26851 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26852 Lisp_Object disp_string,
26853 struct glyph_row **start, struct glyph_row **end)
26855 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26856 int last_y = window_text_bottom_y (w);
26857 struct glyph_row *row;
26859 *start = NULL;
26860 *end = NULL;
26862 while (!first->enabled_p
26863 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26864 first++;
26866 /* Find the START row. */
26867 for (row = first;
26868 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26869 row++)
26871 /* A row can potentially be the START row if the range of the
26872 characters it displays intersects the range
26873 [START_CHARPOS..END_CHARPOS). */
26874 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26875 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26876 /* See the commentary in row_containing_pos, for the
26877 explanation of the complicated way to check whether
26878 some position is beyond the end of the characters
26879 displayed by a row. */
26880 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26881 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26882 && !row->ends_at_zv_p
26883 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26884 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26885 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26886 && !row->ends_at_zv_p
26887 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26889 /* Found a candidate row. Now make sure at least one of the
26890 glyphs it displays has a charpos from the range
26891 [START_CHARPOS..END_CHARPOS).
26893 This is not obvious because bidi reordering could make
26894 buffer positions of a row be 1,2,3,102,101,100, and if we
26895 want to highlight characters in [50..60), we don't want
26896 this row, even though [50..60) does intersect [1..103),
26897 the range of character positions given by the row's start
26898 and end positions. */
26899 struct glyph *g = row->glyphs[TEXT_AREA];
26900 struct glyph *e = g + row->used[TEXT_AREA];
26902 while (g < e)
26904 if (((BUFFERP (g->object) || INTEGERP (g->object))
26905 && start_charpos <= g->charpos && g->charpos < end_charpos)
26906 /* A glyph that comes from DISP_STRING is by
26907 definition to be highlighted. */
26908 || EQ (g->object, disp_string))
26909 *start = row;
26910 g++;
26912 if (*start)
26913 break;
26917 /* Find the END row. */
26918 if (!*start
26919 /* If the last row is partially visible, start looking for END
26920 from that row, instead of starting from FIRST. */
26921 && !(row->enabled_p
26922 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26923 row = first;
26924 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26926 struct glyph_row *next = row + 1;
26927 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26929 if (!next->enabled_p
26930 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26931 /* The first row >= START whose range of displayed characters
26932 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26933 is the row END + 1. */
26934 || (start_charpos < next_start
26935 && end_charpos < next_start)
26936 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26937 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26938 && !next->ends_at_zv_p
26939 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26940 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26941 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26942 && !next->ends_at_zv_p
26943 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26945 *end = row;
26946 break;
26948 else
26950 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26951 but none of the characters it displays are in the range, it is
26952 also END + 1. */
26953 struct glyph *g = next->glyphs[TEXT_AREA];
26954 struct glyph *s = g;
26955 struct glyph *e = g + next->used[TEXT_AREA];
26957 while (g < e)
26959 if (((BUFFERP (g->object) || INTEGERP (g->object))
26960 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26961 /* If the buffer position of the first glyph in
26962 the row is equal to END_CHARPOS, it means
26963 the last character to be highlighted is the
26964 newline of ROW, and we must consider NEXT as
26965 END, not END+1. */
26966 || (((!next->reversed_p && g == s)
26967 || (next->reversed_p && g == e - 1))
26968 && (g->charpos == end_charpos
26969 /* Special case for when NEXT is an
26970 empty line at ZV. */
26971 || (g->charpos == -1
26972 && !row->ends_at_zv_p
26973 && next_start == end_charpos)))))
26974 /* A glyph that comes from DISP_STRING is by
26975 definition to be highlighted. */
26976 || EQ (g->object, disp_string))
26977 break;
26978 g++;
26980 if (g == e)
26982 *end = row;
26983 break;
26985 /* The first row that ends at ZV must be the last to be
26986 highlighted. */
26987 else if (next->ends_at_zv_p)
26989 *end = next;
26990 break;
26996 /* This function sets the mouse_face_* elements of HLINFO, assuming
26997 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26998 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26999 for the overlay or run of text properties specifying the mouse
27000 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27001 before-string and after-string that must also be highlighted.
27002 DISP_STRING, if non-nil, is a display string that may cover some
27003 or all of the highlighted text. */
27005 static void
27006 mouse_face_from_buffer_pos (Lisp_Object window,
27007 Mouse_HLInfo *hlinfo,
27008 ptrdiff_t mouse_charpos,
27009 ptrdiff_t start_charpos,
27010 ptrdiff_t end_charpos,
27011 Lisp_Object before_string,
27012 Lisp_Object after_string,
27013 Lisp_Object disp_string)
27015 struct window *w = XWINDOW (window);
27016 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27017 struct glyph_row *r1, *r2;
27018 struct glyph *glyph, *end;
27019 ptrdiff_t ignore, pos;
27020 int x;
27022 eassert (NILP (disp_string) || STRINGP (disp_string));
27023 eassert (NILP (before_string) || STRINGP (before_string));
27024 eassert (NILP (after_string) || STRINGP (after_string));
27026 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27027 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27028 if (r1 == NULL)
27029 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27030 /* If the before-string or display-string contains newlines,
27031 rows_from_pos_range skips to its last row. Move back. */
27032 if (!NILP (before_string) || !NILP (disp_string))
27034 struct glyph_row *prev;
27035 while ((prev = r1 - 1, prev >= first)
27036 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27037 && prev->used[TEXT_AREA] > 0)
27039 struct glyph *beg = prev->glyphs[TEXT_AREA];
27040 glyph = beg + prev->used[TEXT_AREA];
27041 while (--glyph >= beg && INTEGERP (glyph->object));
27042 if (glyph < beg
27043 || !(EQ (glyph->object, before_string)
27044 || EQ (glyph->object, disp_string)))
27045 break;
27046 r1 = prev;
27049 if (r2 == NULL)
27051 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27052 hlinfo->mouse_face_past_end = 1;
27054 else if (!NILP (after_string))
27056 /* If the after-string has newlines, advance to its last row. */
27057 struct glyph_row *next;
27058 struct glyph_row *last
27059 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27061 for (next = r2 + 1;
27062 next <= last
27063 && next->used[TEXT_AREA] > 0
27064 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27065 ++next)
27066 r2 = next;
27068 /* The rest of the display engine assumes that mouse_face_beg_row is
27069 either above mouse_face_end_row or identical to it. But with
27070 bidi-reordered continued lines, the row for START_CHARPOS could
27071 be below the row for END_CHARPOS. If so, swap the rows and store
27072 them in correct order. */
27073 if (r1->y > r2->y)
27075 struct glyph_row *tem = r2;
27077 r2 = r1;
27078 r1 = tem;
27081 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27082 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27084 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27085 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27086 could be anywhere in the row and in any order. The strategy
27087 below is to find the leftmost and the rightmost glyph that
27088 belongs to either of these 3 strings, or whose position is
27089 between START_CHARPOS and END_CHARPOS, and highlight all the
27090 glyphs between those two. This may cover more than just the text
27091 between START_CHARPOS and END_CHARPOS if the range of characters
27092 strides the bidi level boundary, e.g. if the beginning is in R2L
27093 text while the end is in L2R text or vice versa. */
27094 if (!r1->reversed_p)
27096 /* This row is in a left to right paragraph. Scan it left to
27097 right. */
27098 glyph = r1->glyphs[TEXT_AREA];
27099 end = glyph + r1->used[TEXT_AREA];
27100 x = r1->x;
27102 /* Skip truncation glyphs at the start of the glyph row. */
27103 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27104 for (; glyph < end
27105 && INTEGERP (glyph->object)
27106 && glyph->charpos < 0;
27107 ++glyph)
27108 x += glyph->pixel_width;
27110 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27111 or DISP_STRING, and the first glyph from buffer whose
27112 position is between START_CHARPOS and END_CHARPOS. */
27113 for (; glyph < end
27114 && !INTEGERP (glyph->object)
27115 && !EQ (glyph->object, disp_string)
27116 && !(BUFFERP (glyph->object)
27117 && (glyph->charpos >= start_charpos
27118 && glyph->charpos < end_charpos));
27119 ++glyph)
27121 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27122 are present at buffer positions between START_CHARPOS and
27123 END_CHARPOS, or if they come from an overlay. */
27124 if (EQ (glyph->object, before_string))
27126 pos = string_buffer_position (before_string,
27127 start_charpos);
27128 /* If pos == 0, it means before_string came from an
27129 overlay, not from a buffer position. */
27130 if (!pos || (pos >= start_charpos && pos < end_charpos))
27131 break;
27133 else if (EQ (glyph->object, after_string))
27135 pos = string_buffer_position (after_string, end_charpos);
27136 if (!pos || (pos >= start_charpos && pos < end_charpos))
27137 break;
27139 x += glyph->pixel_width;
27141 hlinfo->mouse_face_beg_x = x;
27142 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27144 else
27146 /* This row is in a right to left paragraph. Scan it right to
27147 left. */
27148 struct glyph *g;
27150 end = r1->glyphs[TEXT_AREA] - 1;
27151 glyph = end + r1->used[TEXT_AREA];
27153 /* Skip truncation glyphs at the start of the glyph row. */
27154 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27155 for (; glyph > end
27156 && INTEGERP (glyph->object)
27157 && glyph->charpos < 0;
27158 --glyph)
27161 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27162 or DISP_STRING, and the first glyph from buffer whose
27163 position is between START_CHARPOS and END_CHARPOS. */
27164 for (; glyph > end
27165 && !INTEGERP (glyph->object)
27166 && !EQ (glyph->object, disp_string)
27167 && !(BUFFERP (glyph->object)
27168 && (glyph->charpos >= start_charpos
27169 && glyph->charpos < end_charpos));
27170 --glyph)
27172 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27173 are present at buffer positions between START_CHARPOS and
27174 END_CHARPOS, or if they come from an overlay. */
27175 if (EQ (glyph->object, before_string))
27177 pos = string_buffer_position (before_string, start_charpos);
27178 /* If pos == 0, it means before_string came from an
27179 overlay, not from a buffer position. */
27180 if (!pos || (pos >= start_charpos && pos < end_charpos))
27181 break;
27183 else if (EQ (glyph->object, after_string))
27185 pos = string_buffer_position (after_string, end_charpos);
27186 if (!pos || (pos >= start_charpos && pos < end_charpos))
27187 break;
27191 glyph++; /* first glyph to the right of the highlighted area */
27192 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27193 x += g->pixel_width;
27194 hlinfo->mouse_face_beg_x = x;
27195 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27198 /* If the highlight ends in a different row, compute GLYPH and END
27199 for the end row. Otherwise, reuse the values computed above for
27200 the row where the highlight begins. */
27201 if (r2 != r1)
27203 if (!r2->reversed_p)
27205 glyph = r2->glyphs[TEXT_AREA];
27206 end = glyph + r2->used[TEXT_AREA];
27207 x = r2->x;
27209 else
27211 end = r2->glyphs[TEXT_AREA] - 1;
27212 glyph = end + r2->used[TEXT_AREA];
27216 if (!r2->reversed_p)
27218 /* Skip truncation and continuation glyphs near the end of the
27219 row, and also blanks and stretch glyphs inserted by
27220 extend_face_to_end_of_line. */
27221 while (end > glyph
27222 && INTEGERP ((end - 1)->object))
27223 --end;
27224 /* Scan the rest of the glyph row from the end, looking for the
27225 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27226 DISP_STRING, or whose position is between START_CHARPOS
27227 and END_CHARPOS */
27228 for (--end;
27229 end > glyph
27230 && !INTEGERP (end->object)
27231 && !EQ (end->object, disp_string)
27232 && !(BUFFERP (end->object)
27233 && (end->charpos >= start_charpos
27234 && end->charpos < end_charpos));
27235 --end)
27237 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27238 are present at buffer positions between START_CHARPOS and
27239 END_CHARPOS, or if they come from an overlay. */
27240 if (EQ (end->object, before_string))
27242 pos = string_buffer_position (before_string, start_charpos);
27243 if (!pos || (pos >= start_charpos && pos < end_charpos))
27244 break;
27246 else if (EQ (end->object, after_string))
27248 pos = string_buffer_position (after_string, end_charpos);
27249 if (!pos || (pos >= start_charpos && pos < end_charpos))
27250 break;
27253 /* Find the X coordinate of the last glyph to be highlighted. */
27254 for (; glyph <= end; ++glyph)
27255 x += glyph->pixel_width;
27257 hlinfo->mouse_face_end_x = x;
27258 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27260 else
27262 /* Skip truncation and continuation glyphs near the end of the
27263 row, and also blanks and stretch glyphs inserted by
27264 extend_face_to_end_of_line. */
27265 x = r2->x;
27266 end++;
27267 while (end < glyph
27268 && INTEGERP (end->object))
27270 x += end->pixel_width;
27271 ++end;
27273 /* Scan the rest of the glyph row from the end, looking for the
27274 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27275 DISP_STRING, or whose position is between START_CHARPOS
27276 and END_CHARPOS */
27277 for ( ;
27278 end < glyph
27279 && !INTEGERP (end->object)
27280 && !EQ (end->object, disp_string)
27281 && !(BUFFERP (end->object)
27282 && (end->charpos >= start_charpos
27283 && end->charpos < end_charpos));
27284 ++end)
27286 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27287 are present at buffer positions between START_CHARPOS and
27288 END_CHARPOS, or if they come from an overlay. */
27289 if (EQ (end->object, before_string))
27291 pos = string_buffer_position (before_string, start_charpos);
27292 if (!pos || (pos >= start_charpos && pos < end_charpos))
27293 break;
27295 else if (EQ (end->object, after_string))
27297 pos = string_buffer_position (after_string, end_charpos);
27298 if (!pos || (pos >= start_charpos && pos < end_charpos))
27299 break;
27301 x += end->pixel_width;
27303 /* If we exited the above loop because we arrived at the last
27304 glyph of the row, and its buffer position is still not in
27305 range, it means the last character in range is the preceding
27306 newline. Bump the end column and x values to get past the
27307 last glyph. */
27308 if (end == glyph
27309 && BUFFERP (end->object)
27310 && (end->charpos < start_charpos
27311 || end->charpos >= end_charpos))
27313 x += end->pixel_width;
27314 ++end;
27316 hlinfo->mouse_face_end_x = x;
27317 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27320 hlinfo->mouse_face_window = window;
27321 hlinfo->mouse_face_face_id
27322 = face_at_buffer_position (w, mouse_charpos, &ignore,
27323 mouse_charpos + 1,
27324 !hlinfo->mouse_face_hidden, -1);
27325 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27328 /* The following function is not used anymore (replaced with
27329 mouse_face_from_string_pos), but I leave it here for the time
27330 being, in case someone would. */
27332 #if 0 /* not used */
27334 /* Find the position of the glyph for position POS in OBJECT in
27335 window W's current matrix, and return in *X, *Y the pixel
27336 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27338 RIGHT_P non-zero means return the position of the right edge of the
27339 glyph, RIGHT_P zero means return the left edge position.
27341 If no glyph for POS exists in the matrix, return the position of
27342 the glyph with the next smaller position that is in the matrix, if
27343 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27344 exists in the matrix, return the position of the glyph with the
27345 next larger position in OBJECT.
27347 Value is non-zero if a glyph was found. */
27349 static int
27350 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27351 int *hpos, int *vpos, int *x, int *y, int right_p)
27353 int yb = window_text_bottom_y (w);
27354 struct glyph_row *r;
27355 struct glyph *best_glyph = NULL;
27356 struct glyph_row *best_row = NULL;
27357 int best_x = 0;
27359 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27360 r->enabled_p && r->y < yb;
27361 ++r)
27363 struct glyph *g = r->glyphs[TEXT_AREA];
27364 struct glyph *e = g + r->used[TEXT_AREA];
27365 int gx;
27367 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27368 if (EQ (g->object, object))
27370 if (g->charpos == pos)
27372 best_glyph = g;
27373 best_x = gx;
27374 best_row = r;
27375 goto found;
27377 else if (best_glyph == NULL
27378 || ((eabs (g->charpos - pos)
27379 < eabs (best_glyph->charpos - pos))
27380 && (right_p
27381 ? g->charpos < pos
27382 : g->charpos > pos)))
27384 best_glyph = g;
27385 best_x = gx;
27386 best_row = r;
27391 found:
27393 if (best_glyph)
27395 *x = best_x;
27396 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27398 if (right_p)
27400 *x += best_glyph->pixel_width;
27401 ++*hpos;
27404 *y = best_row->y;
27405 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27408 return best_glyph != NULL;
27410 #endif /* not used */
27412 /* Find the positions of the first and the last glyphs in window W's
27413 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27414 (assumed to be a string), and return in HLINFO's mouse_face_*
27415 members the pixel and column/row coordinates of those glyphs. */
27417 static void
27418 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27419 Lisp_Object object,
27420 ptrdiff_t startpos, ptrdiff_t endpos)
27422 int yb = window_text_bottom_y (w);
27423 struct glyph_row *r;
27424 struct glyph *g, *e;
27425 int gx;
27426 int found = 0;
27428 /* Find the glyph row with at least one position in the range
27429 [STARTPOS..ENDPOS), and the first glyph in that row whose
27430 position belongs to that range. */
27431 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27432 r->enabled_p && r->y < yb;
27433 ++r)
27435 if (!r->reversed_p)
27437 g = r->glyphs[TEXT_AREA];
27438 e = g + r->used[TEXT_AREA];
27439 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27440 if (EQ (g->object, object)
27441 && startpos <= g->charpos && g->charpos < endpos)
27443 hlinfo->mouse_face_beg_row
27444 = MATRIX_ROW_VPOS (r, w->current_matrix);
27445 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27446 hlinfo->mouse_face_beg_x = gx;
27447 found = 1;
27448 break;
27451 else
27453 struct glyph *g1;
27455 e = r->glyphs[TEXT_AREA];
27456 g = e + r->used[TEXT_AREA];
27457 for ( ; g > e; --g)
27458 if (EQ ((g-1)->object, object)
27459 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27461 hlinfo->mouse_face_beg_row
27462 = MATRIX_ROW_VPOS (r, w->current_matrix);
27463 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27464 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27465 gx += g1->pixel_width;
27466 hlinfo->mouse_face_beg_x = gx;
27467 found = 1;
27468 break;
27471 if (found)
27472 break;
27475 if (!found)
27476 return;
27478 /* Starting with the next row, look for the first row which does NOT
27479 include any glyphs whose positions are in the range. */
27480 for (++r; r->enabled_p && r->y < yb; ++r)
27482 g = r->glyphs[TEXT_AREA];
27483 e = g + r->used[TEXT_AREA];
27484 found = 0;
27485 for ( ; g < e; ++g)
27486 if (EQ (g->object, object)
27487 && startpos <= g->charpos && g->charpos < endpos)
27489 found = 1;
27490 break;
27492 if (!found)
27493 break;
27496 /* The highlighted region ends on the previous row. */
27497 r--;
27499 /* Set the end row. */
27500 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27502 /* Compute and set the end column and the end column's horizontal
27503 pixel coordinate. */
27504 if (!r->reversed_p)
27506 g = r->glyphs[TEXT_AREA];
27507 e = g + r->used[TEXT_AREA];
27508 for ( ; e > g; --e)
27509 if (EQ ((e-1)->object, object)
27510 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27511 break;
27512 hlinfo->mouse_face_end_col = e - g;
27514 for (gx = r->x; g < e; ++g)
27515 gx += g->pixel_width;
27516 hlinfo->mouse_face_end_x = gx;
27518 else
27520 e = r->glyphs[TEXT_AREA];
27521 g = e + r->used[TEXT_AREA];
27522 for (gx = r->x ; e < g; ++e)
27524 if (EQ (e->object, object)
27525 && startpos <= e->charpos && e->charpos < endpos)
27526 break;
27527 gx += e->pixel_width;
27529 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27530 hlinfo->mouse_face_end_x = gx;
27534 #ifdef HAVE_WINDOW_SYSTEM
27536 /* See if position X, Y is within a hot-spot of an image. */
27538 static int
27539 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27541 if (!CONSP (hot_spot))
27542 return 0;
27544 if (EQ (XCAR (hot_spot), Qrect))
27546 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27547 Lisp_Object rect = XCDR (hot_spot);
27548 Lisp_Object tem;
27549 if (!CONSP (rect))
27550 return 0;
27551 if (!CONSP (XCAR (rect)))
27552 return 0;
27553 if (!CONSP (XCDR (rect)))
27554 return 0;
27555 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27556 return 0;
27557 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27558 return 0;
27559 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27560 return 0;
27561 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27562 return 0;
27563 return 1;
27565 else if (EQ (XCAR (hot_spot), Qcircle))
27567 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27568 Lisp_Object circ = XCDR (hot_spot);
27569 Lisp_Object lr, lx0, ly0;
27570 if (CONSP (circ)
27571 && CONSP (XCAR (circ))
27572 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27573 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27574 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27576 double r = XFLOATINT (lr);
27577 double dx = XINT (lx0) - x;
27578 double dy = XINT (ly0) - y;
27579 return (dx * dx + dy * dy <= r * r);
27582 else if (EQ (XCAR (hot_spot), Qpoly))
27584 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27585 if (VECTORP (XCDR (hot_spot)))
27587 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27588 Lisp_Object *poly = v->u.contents;
27589 ptrdiff_t n = v->header.size;
27590 ptrdiff_t i;
27591 int inside = 0;
27592 Lisp_Object lx, ly;
27593 int x0, y0;
27595 /* Need an even number of coordinates, and at least 3 edges. */
27596 if (n < 6 || n & 1)
27597 return 0;
27599 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27600 If count is odd, we are inside polygon. Pixels on edges
27601 may or may not be included depending on actual geometry of the
27602 polygon. */
27603 if ((lx = poly[n-2], !INTEGERP (lx))
27604 || (ly = poly[n-1], !INTEGERP (lx)))
27605 return 0;
27606 x0 = XINT (lx), y0 = XINT (ly);
27607 for (i = 0; i < n; i += 2)
27609 int x1 = x0, y1 = y0;
27610 if ((lx = poly[i], !INTEGERP (lx))
27611 || (ly = poly[i+1], !INTEGERP (ly)))
27612 return 0;
27613 x0 = XINT (lx), y0 = XINT (ly);
27615 /* Does this segment cross the X line? */
27616 if (x0 >= x)
27618 if (x1 >= x)
27619 continue;
27621 else if (x1 < x)
27622 continue;
27623 if (y > y0 && y > y1)
27624 continue;
27625 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27626 inside = !inside;
27628 return inside;
27631 return 0;
27634 Lisp_Object
27635 find_hot_spot (Lisp_Object map, int x, int y)
27637 while (CONSP (map))
27639 if (CONSP (XCAR (map))
27640 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27641 return XCAR (map);
27642 map = XCDR (map);
27645 return Qnil;
27648 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27649 3, 3, 0,
27650 doc: /* Lookup in image map MAP coordinates X and Y.
27651 An image map is an alist where each element has the format (AREA ID PLIST).
27652 An AREA is specified as either a rectangle, a circle, or a polygon:
27653 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27654 pixel coordinates of the upper left and bottom right corners.
27655 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27656 and the radius of the circle; r may be a float or integer.
27657 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27658 vector describes one corner in the polygon.
27659 Returns the alist element for the first matching AREA in MAP. */)
27660 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27662 if (NILP (map))
27663 return Qnil;
27665 CHECK_NUMBER (x);
27666 CHECK_NUMBER (y);
27668 return find_hot_spot (map,
27669 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27670 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27674 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27675 static void
27676 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27678 /* Do not change cursor shape while dragging mouse. */
27679 if (!NILP (do_mouse_tracking))
27680 return;
27682 if (!NILP (pointer))
27684 if (EQ (pointer, Qarrow))
27685 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27686 else if (EQ (pointer, Qhand))
27687 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27688 else if (EQ (pointer, Qtext))
27689 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27690 else if (EQ (pointer, intern ("hdrag")))
27691 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27692 #ifdef HAVE_X_WINDOWS
27693 else if (EQ (pointer, intern ("vdrag")))
27694 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27695 #endif
27696 else if (EQ (pointer, intern ("hourglass")))
27697 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27698 else if (EQ (pointer, Qmodeline))
27699 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27700 else
27701 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27704 if (cursor != No_Cursor)
27705 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27708 #endif /* HAVE_WINDOW_SYSTEM */
27710 /* Take proper action when mouse has moved to the mode or header line
27711 or marginal area AREA of window W, x-position X and y-position Y.
27712 X is relative to the start of the text display area of W, so the
27713 width of bitmap areas and scroll bars must be subtracted to get a
27714 position relative to the start of the mode line. */
27716 static void
27717 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27718 enum window_part area)
27720 struct window *w = XWINDOW (window);
27721 struct frame *f = XFRAME (w->frame);
27722 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27723 #ifdef HAVE_WINDOW_SYSTEM
27724 Display_Info *dpyinfo;
27725 #endif
27726 Cursor cursor = No_Cursor;
27727 Lisp_Object pointer = Qnil;
27728 int dx, dy, width, height;
27729 ptrdiff_t charpos;
27730 Lisp_Object string, object = Qnil;
27731 Lisp_Object pos IF_LINT (= Qnil), help;
27733 Lisp_Object mouse_face;
27734 int original_x_pixel = x;
27735 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27736 struct glyph_row *row IF_LINT (= 0);
27738 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27740 int x0;
27741 struct glyph *end;
27743 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27744 returns them in row/column units! */
27745 string = mode_line_string (w, area, &x, &y, &charpos,
27746 &object, &dx, &dy, &width, &height);
27748 row = (area == ON_MODE_LINE
27749 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27750 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27752 /* Find the glyph under the mouse pointer. */
27753 if (row->mode_line_p && row->enabled_p)
27755 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27756 end = glyph + row->used[TEXT_AREA];
27758 for (x0 = original_x_pixel;
27759 glyph < end && x0 >= glyph->pixel_width;
27760 ++glyph)
27761 x0 -= glyph->pixel_width;
27763 if (glyph >= end)
27764 glyph = NULL;
27767 else
27769 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27770 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27771 returns them in row/column units! */
27772 string = marginal_area_string (w, area, &x, &y, &charpos,
27773 &object, &dx, &dy, &width, &height);
27776 help = Qnil;
27778 #ifdef HAVE_WINDOW_SYSTEM
27779 if (IMAGEP (object))
27781 Lisp_Object image_map, hotspot;
27782 if ((image_map = Fplist_get (XCDR (object), QCmap),
27783 !NILP (image_map))
27784 && (hotspot = find_hot_spot (image_map, dx, dy),
27785 CONSP (hotspot))
27786 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27788 Lisp_Object plist;
27790 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27791 If so, we could look for mouse-enter, mouse-leave
27792 properties in PLIST (and do something...). */
27793 hotspot = XCDR (hotspot);
27794 if (CONSP (hotspot)
27795 && (plist = XCAR (hotspot), CONSP (plist)))
27797 pointer = Fplist_get (plist, Qpointer);
27798 if (NILP (pointer))
27799 pointer = Qhand;
27800 help = Fplist_get (plist, Qhelp_echo);
27801 if (!NILP (help))
27803 help_echo_string = help;
27804 XSETWINDOW (help_echo_window, w);
27805 help_echo_object = w->contents;
27806 help_echo_pos = charpos;
27810 if (NILP (pointer))
27811 pointer = Fplist_get (XCDR (object), QCpointer);
27813 #endif /* HAVE_WINDOW_SYSTEM */
27815 if (STRINGP (string))
27816 pos = make_number (charpos);
27818 /* Set the help text and mouse pointer. If the mouse is on a part
27819 of the mode line without any text (e.g. past the right edge of
27820 the mode line text), use the default help text and pointer. */
27821 if (STRINGP (string) || area == ON_MODE_LINE)
27823 /* Arrange to display the help by setting the global variables
27824 help_echo_string, help_echo_object, and help_echo_pos. */
27825 if (NILP (help))
27827 if (STRINGP (string))
27828 help = Fget_text_property (pos, Qhelp_echo, string);
27830 if (!NILP (help))
27832 help_echo_string = help;
27833 XSETWINDOW (help_echo_window, w);
27834 help_echo_object = string;
27835 help_echo_pos = charpos;
27837 else if (area == ON_MODE_LINE)
27839 Lisp_Object default_help
27840 = buffer_local_value_1 (Qmode_line_default_help_echo,
27841 w->contents);
27843 if (STRINGP (default_help))
27845 help_echo_string = default_help;
27846 XSETWINDOW (help_echo_window, w);
27847 help_echo_object = Qnil;
27848 help_echo_pos = -1;
27853 #ifdef HAVE_WINDOW_SYSTEM
27854 /* Change the mouse pointer according to what is under it. */
27855 if (FRAME_WINDOW_P (f))
27857 dpyinfo = FRAME_DISPLAY_INFO (f);
27858 if (STRINGP (string))
27860 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27862 if (NILP (pointer))
27863 pointer = Fget_text_property (pos, Qpointer, string);
27865 /* Change the mouse pointer according to what is under X/Y. */
27866 if (NILP (pointer)
27867 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27869 Lisp_Object map;
27870 map = Fget_text_property (pos, Qlocal_map, string);
27871 if (!KEYMAPP (map))
27872 map = Fget_text_property (pos, Qkeymap, string);
27873 if (!KEYMAPP (map))
27874 cursor = dpyinfo->vertical_scroll_bar_cursor;
27877 else
27878 /* Default mode-line pointer. */
27879 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27881 #endif
27884 /* Change the mouse face according to what is under X/Y. */
27885 if (STRINGP (string))
27887 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27888 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27889 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27890 && glyph)
27892 Lisp_Object b, e;
27894 struct glyph * tmp_glyph;
27896 int gpos;
27897 int gseq_length;
27898 int total_pixel_width;
27899 ptrdiff_t begpos, endpos, ignore;
27901 int vpos, hpos;
27903 b = Fprevious_single_property_change (make_number (charpos + 1),
27904 Qmouse_face, string, Qnil);
27905 if (NILP (b))
27906 begpos = 0;
27907 else
27908 begpos = XINT (b);
27910 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27911 if (NILP (e))
27912 endpos = SCHARS (string);
27913 else
27914 endpos = XINT (e);
27916 /* Calculate the glyph position GPOS of GLYPH in the
27917 displayed string, relative to the beginning of the
27918 highlighted part of the string.
27920 Note: GPOS is different from CHARPOS. CHARPOS is the
27921 position of GLYPH in the internal string object. A mode
27922 line string format has structures which are converted to
27923 a flattened string by the Emacs Lisp interpreter. The
27924 internal string is an element of those structures. The
27925 displayed string is the flattened string. */
27926 tmp_glyph = row_start_glyph;
27927 while (tmp_glyph < glyph
27928 && (!(EQ (tmp_glyph->object, glyph->object)
27929 && begpos <= tmp_glyph->charpos
27930 && tmp_glyph->charpos < endpos)))
27931 tmp_glyph++;
27932 gpos = glyph - tmp_glyph;
27934 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27935 the highlighted part of the displayed string to which
27936 GLYPH belongs. Note: GSEQ_LENGTH is different from
27937 SCHARS (STRING), because the latter returns the length of
27938 the internal string. */
27939 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27940 tmp_glyph > glyph
27941 && (!(EQ (tmp_glyph->object, glyph->object)
27942 && begpos <= tmp_glyph->charpos
27943 && tmp_glyph->charpos < endpos));
27944 tmp_glyph--)
27946 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27948 /* Calculate the total pixel width of all the glyphs between
27949 the beginning of the highlighted area and GLYPH. */
27950 total_pixel_width = 0;
27951 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27952 total_pixel_width += tmp_glyph->pixel_width;
27954 /* Pre calculation of re-rendering position. Note: X is in
27955 column units here, after the call to mode_line_string or
27956 marginal_area_string. */
27957 hpos = x - gpos;
27958 vpos = (area == ON_MODE_LINE
27959 ? (w->current_matrix)->nrows - 1
27960 : 0);
27962 /* If GLYPH's position is included in the region that is
27963 already drawn in mouse face, we have nothing to do. */
27964 if ( EQ (window, hlinfo->mouse_face_window)
27965 && (!row->reversed_p
27966 ? (hlinfo->mouse_face_beg_col <= hpos
27967 && hpos < hlinfo->mouse_face_end_col)
27968 /* In R2L rows we swap BEG and END, see below. */
27969 : (hlinfo->mouse_face_end_col <= hpos
27970 && hpos < hlinfo->mouse_face_beg_col))
27971 && hlinfo->mouse_face_beg_row == vpos )
27972 return;
27974 if (clear_mouse_face (hlinfo))
27975 cursor = No_Cursor;
27977 if (!row->reversed_p)
27979 hlinfo->mouse_face_beg_col = hpos;
27980 hlinfo->mouse_face_beg_x = original_x_pixel
27981 - (total_pixel_width + dx);
27982 hlinfo->mouse_face_end_col = hpos + gseq_length;
27983 hlinfo->mouse_face_end_x = 0;
27985 else
27987 /* In R2L rows, show_mouse_face expects BEG and END
27988 coordinates to be swapped. */
27989 hlinfo->mouse_face_end_col = hpos;
27990 hlinfo->mouse_face_end_x = original_x_pixel
27991 - (total_pixel_width + dx);
27992 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27993 hlinfo->mouse_face_beg_x = 0;
27996 hlinfo->mouse_face_beg_row = vpos;
27997 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27998 hlinfo->mouse_face_past_end = 0;
27999 hlinfo->mouse_face_window = window;
28001 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28002 charpos,
28003 0, &ignore,
28004 glyph->face_id,
28006 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28008 if (NILP (pointer))
28009 pointer = Qhand;
28011 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28012 clear_mouse_face (hlinfo);
28014 #ifdef HAVE_WINDOW_SYSTEM
28015 if (FRAME_WINDOW_P (f))
28016 define_frame_cursor1 (f, cursor, pointer);
28017 #endif
28021 /* EXPORT:
28022 Take proper action when the mouse has moved to position X, Y on
28023 frame F with regards to highlighting portions of display that have
28024 mouse-face properties. Also de-highlight portions of display where
28025 the mouse was before, set the mouse pointer shape as appropriate
28026 for the mouse coordinates, and activate help echo (tooltips).
28027 X and Y can be negative or out of range. */
28029 void
28030 note_mouse_highlight (struct frame *f, int x, int y)
28032 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28033 enum window_part part = ON_NOTHING;
28034 Lisp_Object window;
28035 struct window *w;
28036 Cursor cursor = No_Cursor;
28037 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28038 struct buffer *b;
28040 /* When a menu is active, don't highlight because this looks odd. */
28041 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28042 if (popup_activated ())
28043 return;
28044 #endif
28046 if (!f->glyphs_initialized_p
28047 || f->pointer_invisible)
28048 return;
28050 hlinfo->mouse_face_mouse_x = x;
28051 hlinfo->mouse_face_mouse_y = y;
28052 hlinfo->mouse_face_mouse_frame = f;
28054 if (hlinfo->mouse_face_defer)
28055 return;
28057 /* Which window is that in? */
28058 window = window_from_coordinates (f, x, y, &part, 1);
28060 /* If displaying active text in another window, clear that. */
28061 if (! EQ (window, hlinfo->mouse_face_window)
28062 /* Also clear if we move out of text area in same window. */
28063 || (!NILP (hlinfo->mouse_face_window)
28064 && !NILP (window)
28065 && part != ON_TEXT
28066 && part != ON_MODE_LINE
28067 && part != ON_HEADER_LINE))
28068 clear_mouse_face (hlinfo);
28070 /* Not on a window -> return. */
28071 if (!WINDOWP (window))
28072 return;
28074 /* Reset help_echo_string. It will get recomputed below. */
28075 help_echo_string = Qnil;
28077 /* Convert to window-relative pixel coordinates. */
28078 w = XWINDOW (window);
28079 frame_to_window_pixel_xy (w, &x, &y);
28081 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28082 /* Handle tool-bar window differently since it doesn't display a
28083 buffer. */
28084 if (EQ (window, f->tool_bar_window))
28086 note_tool_bar_highlight (f, x, y);
28087 return;
28089 #endif
28091 /* Mouse is on the mode, header line or margin? */
28092 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28093 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28095 note_mode_line_or_margin_highlight (window, x, y, part);
28096 return;
28099 #ifdef HAVE_WINDOW_SYSTEM
28100 if (part == ON_VERTICAL_BORDER)
28102 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28103 help_echo_string = build_string ("drag-mouse-1: resize");
28105 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28106 || part == ON_SCROLL_BAR)
28107 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28108 else
28109 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28110 #endif
28112 /* Are we in a window whose display is up to date?
28113 And verify the buffer's text has not changed. */
28114 b = XBUFFER (w->contents);
28115 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28117 int hpos, vpos, dx, dy, area = LAST_AREA;
28118 ptrdiff_t pos;
28119 struct glyph *glyph;
28120 Lisp_Object object;
28121 Lisp_Object mouse_face = Qnil, position;
28122 Lisp_Object *overlay_vec = NULL;
28123 ptrdiff_t i, noverlays;
28124 struct buffer *obuf;
28125 ptrdiff_t obegv, ozv;
28126 int same_region;
28128 /* Find the glyph under X/Y. */
28129 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28131 #ifdef HAVE_WINDOW_SYSTEM
28132 /* Look for :pointer property on image. */
28133 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28135 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28136 if (img != NULL && IMAGEP (img->spec))
28138 Lisp_Object image_map, hotspot;
28139 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28140 !NILP (image_map))
28141 && (hotspot = find_hot_spot (image_map,
28142 glyph->slice.img.x + dx,
28143 glyph->slice.img.y + dy),
28144 CONSP (hotspot))
28145 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28147 Lisp_Object plist;
28149 /* Could check XCAR (hotspot) to see if we enter/leave
28150 this hot-spot.
28151 If so, we could look for mouse-enter, mouse-leave
28152 properties in PLIST (and do something...). */
28153 hotspot = XCDR (hotspot);
28154 if (CONSP (hotspot)
28155 && (plist = XCAR (hotspot), CONSP (plist)))
28157 pointer = Fplist_get (plist, Qpointer);
28158 if (NILP (pointer))
28159 pointer = Qhand;
28160 help_echo_string = Fplist_get (plist, Qhelp_echo);
28161 if (!NILP (help_echo_string))
28163 help_echo_window = window;
28164 help_echo_object = glyph->object;
28165 help_echo_pos = glyph->charpos;
28169 if (NILP (pointer))
28170 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28173 #endif /* HAVE_WINDOW_SYSTEM */
28175 /* Clear mouse face if X/Y not over text. */
28176 if (glyph == NULL
28177 || area != TEXT_AREA
28178 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28179 /* Glyph's OBJECT is an integer for glyphs inserted by the
28180 display engine for its internal purposes, like truncation
28181 and continuation glyphs and blanks beyond the end of
28182 line's text on text terminals. If we are over such a
28183 glyph, we are not over any text. */
28184 || INTEGERP (glyph->object)
28185 /* R2L rows have a stretch glyph at their front, which
28186 stands for no text, whereas L2R rows have no glyphs at
28187 all beyond the end of text. Treat such stretch glyphs
28188 like we do with NULL glyphs in L2R rows. */
28189 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28190 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28191 && glyph->type == STRETCH_GLYPH
28192 && glyph->avoid_cursor_p))
28194 if (clear_mouse_face (hlinfo))
28195 cursor = No_Cursor;
28196 #ifdef HAVE_WINDOW_SYSTEM
28197 if (FRAME_WINDOW_P (f) && NILP (pointer))
28199 if (area != TEXT_AREA)
28200 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28201 else
28202 pointer = Vvoid_text_area_pointer;
28204 #endif
28205 goto set_cursor;
28208 pos = glyph->charpos;
28209 object = glyph->object;
28210 if (!STRINGP (object) && !BUFFERP (object))
28211 goto set_cursor;
28213 /* If we get an out-of-range value, return now; avoid an error. */
28214 if (BUFFERP (object) && pos > BUF_Z (b))
28215 goto set_cursor;
28217 /* Make the window's buffer temporarily current for
28218 overlays_at and compute_char_face. */
28219 obuf = current_buffer;
28220 current_buffer = b;
28221 obegv = BEGV;
28222 ozv = ZV;
28223 BEGV = BEG;
28224 ZV = Z;
28226 /* Is this char mouse-active or does it have help-echo? */
28227 position = make_number (pos);
28229 if (BUFFERP (object))
28231 /* Put all the overlays we want in a vector in overlay_vec. */
28232 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28233 /* Sort overlays into increasing priority order. */
28234 noverlays = sort_overlays (overlay_vec, noverlays, w);
28236 else
28237 noverlays = 0;
28239 if (NILP (Vmouse_highlight))
28241 clear_mouse_face (hlinfo);
28242 goto check_help_echo;
28245 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28247 if (same_region)
28248 cursor = No_Cursor;
28250 /* Check mouse-face highlighting. */
28251 if (! same_region
28252 /* If there exists an overlay with mouse-face overlapping
28253 the one we are currently highlighting, we have to
28254 check if we enter the overlapping overlay, and then
28255 highlight only that. */
28256 || (OVERLAYP (hlinfo->mouse_face_overlay)
28257 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28259 /* Find the highest priority overlay with a mouse-face. */
28260 Lisp_Object overlay = Qnil;
28261 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28263 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28264 if (!NILP (mouse_face))
28265 overlay = overlay_vec[i];
28268 /* If we're highlighting the same overlay as before, there's
28269 no need to do that again. */
28270 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28271 goto check_help_echo;
28272 hlinfo->mouse_face_overlay = overlay;
28274 /* Clear the display of the old active region, if any. */
28275 if (clear_mouse_face (hlinfo))
28276 cursor = No_Cursor;
28278 /* If no overlay applies, get a text property. */
28279 if (NILP (overlay))
28280 mouse_face = Fget_text_property (position, Qmouse_face, object);
28282 /* Next, compute the bounds of the mouse highlighting and
28283 display it. */
28284 if (!NILP (mouse_face) && STRINGP (object))
28286 /* The mouse-highlighting comes from a display string
28287 with a mouse-face. */
28288 Lisp_Object s, e;
28289 ptrdiff_t ignore;
28291 s = Fprevious_single_property_change
28292 (make_number (pos + 1), Qmouse_face, object, Qnil);
28293 e = Fnext_single_property_change
28294 (position, Qmouse_face, object, Qnil);
28295 if (NILP (s))
28296 s = make_number (0);
28297 if (NILP (e))
28298 e = make_number (SCHARS (object));
28299 mouse_face_from_string_pos (w, hlinfo, object,
28300 XINT (s), XINT (e));
28301 hlinfo->mouse_face_past_end = 0;
28302 hlinfo->mouse_face_window = window;
28303 hlinfo->mouse_face_face_id
28304 = face_at_string_position (w, object, pos, 0, &ignore,
28305 glyph->face_id, 1);
28306 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28307 cursor = No_Cursor;
28309 else
28311 /* The mouse-highlighting, if any, comes from an overlay
28312 or text property in the buffer. */
28313 Lisp_Object buffer IF_LINT (= Qnil);
28314 Lisp_Object disp_string IF_LINT (= Qnil);
28316 if (STRINGP (object))
28318 /* If we are on a display string with no mouse-face,
28319 check if the text under it has one. */
28320 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28321 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28322 pos = string_buffer_position (object, start);
28323 if (pos > 0)
28325 mouse_face = get_char_property_and_overlay
28326 (make_number (pos), Qmouse_face, w->contents, &overlay);
28327 buffer = w->contents;
28328 disp_string = object;
28331 else
28333 buffer = object;
28334 disp_string = Qnil;
28337 if (!NILP (mouse_face))
28339 Lisp_Object before, after;
28340 Lisp_Object before_string, after_string;
28341 /* To correctly find the limits of mouse highlight
28342 in a bidi-reordered buffer, we must not use the
28343 optimization of limiting the search in
28344 previous-single-property-change and
28345 next-single-property-change, because
28346 rows_from_pos_range needs the real start and end
28347 positions to DTRT in this case. That's because
28348 the first row visible in a window does not
28349 necessarily display the character whose position
28350 is the smallest. */
28351 Lisp_Object lim1
28352 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28353 ? Fmarker_position (w->start)
28354 : Qnil;
28355 Lisp_Object lim2
28356 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28357 ? make_number (BUF_Z (XBUFFER (buffer))
28358 - w->window_end_pos)
28359 : Qnil;
28361 if (NILP (overlay))
28363 /* Handle the text property case. */
28364 before = Fprevious_single_property_change
28365 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28366 after = Fnext_single_property_change
28367 (make_number (pos), Qmouse_face, buffer, lim2);
28368 before_string = after_string = Qnil;
28370 else
28372 /* Handle the overlay case. */
28373 before = Foverlay_start (overlay);
28374 after = Foverlay_end (overlay);
28375 before_string = Foverlay_get (overlay, Qbefore_string);
28376 after_string = Foverlay_get (overlay, Qafter_string);
28378 if (!STRINGP (before_string)) before_string = Qnil;
28379 if (!STRINGP (after_string)) after_string = Qnil;
28382 mouse_face_from_buffer_pos (window, hlinfo, pos,
28383 NILP (before)
28385 : XFASTINT (before),
28386 NILP (after)
28387 ? BUF_Z (XBUFFER (buffer))
28388 : XFASTINT (after),
28389 before_string, after_string,
28390 disp_string);
28391 cursor = No_Cursor;
28396 check_help_echo:
28398 /* Look for a `help-echo' property. */
28399 if (NILP (help_echo_string)) {
28400 Lisp_Object help, overlay;
28402 /* Check overlays first. */
28403 help = overlay = Qnil;
28404 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28406 overlay = overlay_vec[i];
28407 help = Foverlay_get (overlay, Qhelp_echo);
28410 if (!NILP (help))
28412 help_echo_string = help;
28413 help_echo_window = window;
28414 help_echo_object = overlay;
28415 help_echo_pos = pos;
28417 else
28419 Lisp_Object obj = glyph->object;
28420 ptrdiff_t charpos = glyph->charpos;
28422 /* Try text properties. */
28423 if (STRINGP (obj)
28424 && charpos >= 0
28425 && charpos < SCHARS (obj))
28427 help = Fget_text_property (make_number (charpos),
28428 Qhelp_echo, obj);
28429 if (NILP (help))
28431 /* If the string itself doesn't specify a help-echo,
28432 see if the buffer text ``under'' it does. */
28433 struct glyph_row *r
28434 = MATRIX_ROW (w->current_matrix, vpos);
28435 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28436 ptrdiff_t p = string_buffer_position (obj, start);
28437 if (p > 0)
28439 help = Fget_char_property (make_number (p),
28440 Qhelp_echo, w->contents);
28441 if (!NILP (help))
28443 charpos = p;
28444 obj = w->contents;
28449 else if (BUFFERP (obj)
28450 && charpos >= BEGV
28451 && charpos < ZV)
28452 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28453 obj);
28455 if (!NILP (help))
28457 help_echo_string = help;
28458 help_echo_window = window;
28459 help_echo_object = obj;
28460 help_echo_pos = charpos;
28465 #ifdef HAVE_WINDOW_SYSTEM
28466 /* Look for a `pointer' property. */
28467 if (FRAME_WINDOW_P (f) && NILP (pointer))
28469 /* Check overlays first. */
28470 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28471 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28473 if (NILP (pointer))
28475 Lisp_Object obj = glyph->object;
28476 ptrdiff_t charpos = glyph->charpos;
28478 /* Try text properties. */
28479 if (STRINGP (obj)
28480 && charpos >= 0
28481 && charpos < SCHARS (obj))
28483 pointer = Fget_text_property (make_number (charpos),
28484 Qpointer, obj);
28485 if (NILP (pointer))
28487 /* If the string itself doesn't specify a pointer,
28488 see if the buffer text ``under'' it does. */
28489 struct glyph_row *r
28490 = MATRIX_ROW (w->current_matrix, vpos);
28491 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28492 ptrdiff_t p = string_buffer_position (obj, start);
28493 if (p > 0)
28494 pointer = Fget_char_property (make_number (p),
28495 Qpointer, w->contents);
28498 else if (BUFFERP (obj)
28499 && charpos >= BEGV
28500 && charpos < ZV)
28501 pointer = Fget_text_property (make_number (charpos),
28502 Qpointer, obj);
28505 #endif /* HAVE_WINDOW_SYSTEM */
28507 BEGV = obegv;
28508 ZV = ozv;
28509 current_buffer = obuf;
28512 set_cursor:
28514 #ifdef HAVE_WINDOW_SYSTEM
28515 if (FRAME_WINDOW_P (f))
28516 define_frame_cursor1 (f, cursor, pointer);
28517 #else
28518 /* This is here to prevent a compiler error, about "label at end of
28519 compound statement". */
28520 return;
28521 #endif
28525 /* EXPORT for RIF:
28526 Clear any mouse-face on window W. This function is part of the
28527 redisplay interface, and is called from try_window_id and similar
28528 functions to ensure the mouse-highlight is off. */
28530 void
28531 x_clear_window_mouse_face (struct window *w)
28533 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28534 Lisp_Object window;
28536 block_input ();
28537 XSETWINDOW (window, w);
28538 if (EQ (window, hlinfo->mouse_face_window))
28539 clear_mouse_face (hlinfo);
28540 unblock_input ();
28544 /* EXPORT:
28545 Just discard the mouse face information for frame F, if any.
28546 This is used when the size of F is changed. */
28548 void
28549 cancel_mouse_face (struct frame *f)
28551 Lisp_Object window;
28552 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28554 window = hlinfo->mouse_face_window;
28555 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28556 reset_mouse_highlight (hlinfo);
28561 /***********************************************************************
28562 Exposure Events
28563 ***********************************************************************/
28565 #ifdef HAVE_WINDOW_SYSTEM
28567 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28568 which intersects rectangle R. R is in window-relative coordinates. */
28570 static void
28571 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28572 enum glyph_row_area area)
28574 struct glyph *first = row->glyphs[area];
28575 struct glyph *end = row->glyphs[area] + row->used[area];
28576 struct glyph *last;
28577 int first_x, start_x, x;
28579 if (area == TEXT_AREA && row->fill_line_p)
28580 /* If row extends face to end of line write the whole line. */
28581 draw_glyphs (w, 0, row, area,
28582 0, row->used[area],
28583 DRAW_NORMAL_TEXT, 0);
28584 else
28586 /* Set START_X to the window-relative start position for drawing glyphs of
28587 AREA. The first glyph of the text area can be partially visible.
28588 The first glyphs of other areas cannot. */
28589 start_x = window_box_left_offset (w, area);
28590 x = start_x;
28591 if (area == TEXT_AREA)
28592 x += row->x;
28594 /* Find the first glyph that must be redrawn. */
28595 while (first < end
28596 && x + first->pixel_width < r->x)
28598 x += first->pixel_width;
28599 ++first;
28602 /* Find the last one. */
28603 last = first;
28604 first_x = x;
28605 while (last < end
28606 && x < r->x + r->width)
28608 x += last->pixel_width;
28609 ++last;
28612 /* Repaint. */
28613 if (last > first)
28614 draw_glyphs (w, first_x - start_x, row, area,
28615 first - row->glyphs[area], last - row->glyphs[area],
28616 DRAW_NORMAL_TEXT, 0);
28621 /* Redraw the parts of the glyph row ROW on window W intersecting
28622 rectangle R. R is in window-relative coordinates. Value is
28623 non-zero if mouse-face was overwritten. */
28625 static int
28626 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28628 eassert (row->enabled_p);
28630 if (row->mode_line_p || w->pseudo_window_p)
28631 draw_glyphs (w, 0, row, TEXT_AREA,
28632 0, row->used[TEXT_AREA],
28633 DRAW_NORMAL_TEXT, 0);
28634 else
28636 if (row->used[LEFT_MARGIN_AREA])
28637 expose_area (w, row, r, LEFT_MARGIN_AREA);
28638 if (row->used[TEXT_AREA])
28639 expose_area (w, row, r, TEXT_AREA);
28640 if (row->used[RIGHT_MARGIN_AREA])
28641 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28642 draw_row_fringe_bitmaps (w, row);
28645 return row->mouse_face_p;
28649 /* Redraw those parts of glyphs rows during expose event handling that
28650 overlap other rows. Redrawing of an exposed line writes over parts
28651 of lines overlapping that exposed line; this function fixes that.
28653 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28654 row in W's current matrix that is exposed and overlaps other rows.
28655 LAST_OVERLAPPING_ROW is the last such row. */
28657 static void
28658 expose_overlaps (struct window *w,
28659 struct glyph_row *first_overlapping_row,
28660 struct glyph_row *last_overlapping_row,
28661 XRectangle *r)
28663 struct glyph_row *row;
28665 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28666 if (row->overlapping_p)
28668 eassert (row->enabled_p && !row->mode_line_p);
28670 row->clip = r;
28671 if (row->used[LEFT_MARGIN_AREA])
28672 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28674 if (row->used[TEXT_AREA])
28675 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28677 if (row->used[RIGHT_MARGIN_AREA])
28678 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28679 row->clip = NULL;
28684 /* Return non-zero if W's cursor intersects rectangle R. */
28686 static int
28687 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28689 XRectangle cr, result;
28690 struct glyph *cursor_glyph;
28691 struct glyph_row *row;
28693 if (w->phys_cursor.vpos >= 0
28694 && w->phys_cursor.vpos < w->current_matrix->nrows
28695 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28696 row->enabled_p)
28697 && row->cursor_in_fringe_p)
28699 /* Cursor is in the fringe. */
28700 cr.x = window_box_right_offset (w,
28701 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28702 ? RIGHT_MARGIN_AREA
28703 : TEXT_AREA));
28704 cr.y = row->y;
28705 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28706 cr.height = row->height;
28707 return x_intersect_rectangles (&cr, r, &result);
28710 cursor_glyph = get_phys_cursor_glyph (w);
28711 if (cursor_glyph)
28713 /* r is relative to W's box, but w->phys_cursor.x is relative
28714 to left edge of W's TEXT area. Adjust it. */
28715 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28716 cr.y = w->phys_cursor.y;
28717 cr.width = cursor_glyph->pixel_width;
28718 cr.height = w->phys_cursor_height;
28719 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28720 I assume the effect is the same -- and this is portable. */
28721 return x_intersect_rectangles (&cr, r, &result);
28723 /* If we don't understand the format, pretend we're not in the hot-spot. */
28724 return 0;
28728 /* EXPORT:
28729 Draw a vertical window border to the right of window W if W doesn't
28730 have vertical scroll bars. */
28732 void
28733 x_draw_vertical_border (struct window *w)
28735 struct frame *f = XFRAME (WINDOW_FRAME (w));
28737 /* We could do better, if we knew what type of scroll-bar the adjacent
28738 windows (on either side) have... But we don't :-(
28739 However, I think this works ok. ++KFS 2003-04-25 */
28741 /* Redraw borders between horizontally adjacent windows. Don't
28742 do it for frames with vertical scroll bars because either the
28743 right scroll bar of a window, or the left scroll bar of its
28744 neighbor will suffice as a border. */
28745 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28746 return;
28748 /* Note: It is necessary to redraw both the left and the right
28749 borders, for when only this single window W is being
28750 redisplayed. */
28751 if (!WINDOW_RIGHTMOST_P (w)
28752 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28754 int x0, x1, y0, y1;
28756 window_box_edges (w, &x0, &y0, &x1, &y1);
28757 y1 -= 1;
28759 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28760 x1 -= 1;
28762 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28764 if (!WINDOW_LEFTMOST_P (w)
28765 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28767 int x0, x1, y0, y1;
28769 window_box_edges (w, &x0, &y0, &x1, &y1);
28770 y1 -= 1;
28772 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28773 x0 -= 1;
28775 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28780 /* Redraw the part of window W intersection rectangle FR. Pixel
28781 coordinates in FR are frame-relative. Call this function with
28782 input blocked. Value is non-zero if the exposure overwrites
28783 mouse-face. */
28785 static int
28786 expose_window (struct window *w, XRectangle *fr)
28788 struct frame *f = XFRAME (w->frame);
28789 XRectangle wr, r;
28790 int mouse_face_overwritten_p = 0;
28792 /* If window is not yet fully initialized, do nothing. This can
28793 happen when toolkit scroll bars are used and a window is split.
28794 Reconfiguring the scroll bar will generate an expose for a newly
28795 created window. */
28796 if (w->current_matrix == NULL)
28797 return 0;
28799 /* When we're currently updating the window, display and current
28800 matrix usually don't agree. Arrange for a thorough display
28801 later. */
28802 if (w->must_be_updated_p)
28804 SET_FRAME_GARBAGED (f);
28805 return 0;
28808 /* Frame-relative pixel rectangle of W. */
28809 wr.x = WINDOW_LEFT_EDGE_X (w);
28810 wr.y = WINDOW_TOP_EDGE_Y (w);
28811 wr.width = WINDOW_TOTAL_WIDTH (w);
28812 wr.height = WINDOW_TOTAL_HEIGHT (w);
28814 if (x_intersect_rectangles (fr, &wr, &r))
28816 int yb = window_text_bottom_y (w);
28817 struct glyph_row *row;
28818 int cursor_cleared_p, phys_cursor_on_p;
28819 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28821 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28822 r.x, r.y, r.width, r.height));
28824 /* Convert to window coordinates. */
28825 r.x -= WINDOW_LEFT_EDGE_X (w);
28826 r.y -= WINDOW_TOP_EDGE_Y (w);
28828 /* Turn off the cursor. */
28829 if (!w->pseudo_window_p
28830 && phys_cursor_in_rect_p (w, &r))
28832 x_clear_cursor (w);
28833 cursor_cleared_p = 1;
28835 else
28836 cursor_cleared_p = 0;
28838 /* If the row containing the cursor extends face to end of line,
28839 then expose_area might overwrite the cursor outside the
28840 rectangle and thus notice_overwritten_cursor might clear
28841 w->phys_cursor_on_p. We remember the original value and
28842 check later if it is changed. */
28843 phys_cursor_on_p = w->phys_cursor_on_p;
28845 /* Update lines intersecting rectangle R. */
28846 first_overlapping_row = last_overlapping_row = NULL;
28847 for (row = w->current_matrix->rows;
28848 row->enabled_p;
28849 ++row)
28851 int y0 = row->y;
28852 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28854 if ((y0 >= r.y && y0 < r.y + r.height)
28855 || (y1 > r.y && y1 < r.y + r.height)
28856 || (r.y >= y0 && r.y < y1)
28857 || (r.y + r.height > y0 && r.y + r.height < y1))
28859 /* A header line may be overlapping, but there is no need
28860 to fix overlapping areas for them. KFS 2005-02-12 */
28861 if (row->overlapping_p && !row->mode_line_p)
28863 if (first_overlapping_row == NULL)
28864 first_overlapping_row = row;
28865 last_overlapping_row = row;
28868 row->clip = fr;
28869 if (expose_line (w, row, &r))
28870 mouse_face_overwritten_p = 1;
28871 row->clip = NULL;
28873 else if (row->overlapping_p)
28875 /* We must redraw a row overlapping the exposed area. */
28876 if (y0 < r.y
28877 ? y0 + row->phys_height > r.y
28878 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28880 if (first_overlapping_row == NULL)
28881 first_overlapping_row = row;
28882 last_overlapping_row = row;
28886 if (y1 >= yb)
28887 break;
28890 /* Display the mode line if there is one. */
28891 if (WINDOW_WANTS_MODELINE_P (w)
28892 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28893 row->enabled_p)
28894 && row->y < r.y + r.height)
28896 if (expose_line (w, row, &r))
28897 mouse_face_overwritten_p = 1;
28900 if (!w->pseudo_window_p)
28902 /* Fix the display of overlapping rows. */
28903 if (first_overlapping_row)
28904 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28905 fr);
28907 /* Draw border between windows. */
28908 x_draw_vertical_border (w);
28910 /* Turn the cursor on again. */
28911 if (cursor_cleared_p
28912 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28913 update_window_cursor (w, 1);
28917 return mouse_face_overwritten_p;
28922 /* Redraw (parts) of all windows in the window tree rooted at W that
28923 intersect R. R contains frame pixel coordinates. Value is
28924 non-zero if the exposure overwrites mouse-face. */
28926 static int
28927 expose_window_tree (struct window *w, XRectangle *r)
28929 struct frame *f = XFRAME (w->frame);
28930 int mouse_face_overwritten_p = 0;
28932 while (w && !FRAME_GARBAGED_P (f))
28934 if (WINDOWP (w->contents))
28935 mouse_face_overwritten_p
28936 |= expose_window_tree (XWINDOW (w->contents), r);
28937 else
28938 mouse_face_overwritten_p |= expose_window (w, r);
28940 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28943 return mouse_face_overwritten_p;
28947 /* EXPORT:
28948 Redisplay an exposed area of frame F. X and Y are the upper-left
28949 corner of the exposed rectangle. W and H are width and height of
28950 the exposed area. All are pixel values. W or H zero means redraw
28951 the entire frame. */
28953 void
28954 expose_frame (struct frame *f, int x, int y, int w, int h)
28956 XRectangle r;
28957 int mouse_face_overwritten_p = 0;
28959 TRACE ((stderr, "expose_frame "));
28961 /* No need to redraw if frame will be redrawn soon. */
28962 if (FRAME_GARBAGED_P (f))
28964 TRACE ((stderr, " garbaged\n"));
28965 return;
28968 /* If basic faces haven't been realized yet, there is no point in
28969 trying to redraw anything. This can happen when we get an expose
28970 event while Emacs is starting, e.g. by moving another window. */
28971 if (FRAME_FACE_CACHE (f) == NULL
28972 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28974 TRACE ((stderr, " no faces\n"));
28975 return;
28978 if (w == 0 || h == 0)
28980 r.x = r.y = 0;
28981 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28982 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28984 else
28986 r.x = x;
28987 r.y = y;
28988 r.width = w;
28989 r.height = h;
28992 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28993 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28995 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28996 if (WINDOWP (f->tool_bar_window))
28997 mouse_face_overwritten_p
28998 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28999 #endif
29001 #ifdef HAVE_X_WINDOWS
29002 #ifndef MSDOS
29003 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29004 if (WINDOWP (f->menu_bar_window))
29005 mouse_face_overwritten_p
29006 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29007 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29008 #endif
29009 #endif
29011 /* Some window managers support a focus-follows-mouse style with
29012 delayed raising of frames. Imagine a partially obscured frame,
29013 and moving the mouse into partially obscured mouse-face on that
29014 frame. The visible part of the mouse-face will be highlighted,
29015 then the WM raises the obscured frame. With at least one WM, KDE
29016 2.1, Emacs is not getting any event for the raising of the frame
29017 (even tried with SubstructureRedirectMask), only Expose events.
29018 These expose events will draw text normally, i.e. not
29019 highlighted. Which means we must redo the highlight here.
29020 Subsume it under ``we love X''. --gerd 2001-08-15 */
29021 /* Included in Windows version because Windows most likely does not
29022 do the right thing if any third party tool offers
29023 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29024 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29026 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29027 if (f == hlinfo->mouse_face_mouse_frame)
29029 int mouse_x = hlinfo->mouse_face_mouse_x;
29030 int mouse_y = hlinfo->mouse_face_mouse_y;
29031 clear_mouse_face (hlinfo);
29032 note_mouse_highlight (f, mouse_x, mouse_y);
29038 /* EXPORT:
29039 Determine the intersection of two rectangles R1 and R2. Return
29040 the intersection in *RESULT. Value is non-zero if RESULT is not
29041 empty. */
29044 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29046 XRectangle *left, *right;
29047 XRectangle *upper, *lower;
29048 int intersection_p = 0;
29050 /* Rearrange so that R1 is the left-most rectangle. */
29051 if (r1->x < r2->x)
29052 left = r1, right = r2;
29053 else
29054 left = r2, right = r1;
29056 /* X0 of the intersection is right.x0, if this is inside R1,
29057 otherwise there is no intersection. */
29058 if (right->x <= left->x + left->width)
29060 result->x = right->x;
29062 /* The right end of the intersection is the minimum of
29063 the right ends of left and right. */
29064 result->width = (min (left->x + left->width, right->x + right->width)
29065 - result->x);
29067 /* Same game for Y. */
29068 if (r1->y < r2->y)
29069 upper = r1, lower = r2;
29070 else
29071 upper = r2, lower = r1;
29073 /* The upper end of the intersection is lower.y0, if this is inside
29074 of upper. Otherwise, there is no intersection. */
29075 if (lower->y <= upper->y + upper->height)
29077 result->y = lower->y;
29079 /* The lower end of the intersection is the minimum of the lower
29080 ends of upper and lower. */
29081 result->height = (min (lower->y + lower->height,
29082 upper->y + upper->height)
29083 - result->y);
29084 intersection_p = 1;
29088 return intersection_p;
29091 #endif /* HAVE_WINDOW_SYSTEM */
29094 /***********************************************************************
29095 Initialization
29096 ***********************************************************************/
29098 void
29099 syms_of_xdisp (void)
29101 Vwith_echo_area_save_vector = Qnil;
29102 staticpro (&Vwith_echo_area_save_vector);
29104 Vmessage_stack = Qnil;
29105 staticpro (&Vmessage_stack);
29107 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29108 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29110 message_dolog_marker1 = Fmake_marker ();
29111 staticpro (&message_dolog_marker1);
29112 message_dolog_marker2 = Fmake_marker ();
29113 staticpro (&message_dolog_marker2);
29114 message_dolog_marker3 = Fmake_marker ();
29115 staticpro (&message_dolog_marker3);
29117 #ifdef GLYPH_DEBUG
29118 defsubr (&Sdump_frame_glyph_matrix);
29119 defsubr (&Sdump_glyph_matrix);
29120 defsubr (&Sdump_glyph_row);
29121 defsubr (&Sdump_tool_bar_row);
29122 defsubr (&Strace_redisplay);
29123 defsubr (&Strace_to_stderr);
29124 #endif
29125 #ifdef HAVE_WINDOW_SYSTEM
29126 defsubr (&Stool_bar_lines_needed);
29127 defsubr (&Slookup_image_map);
29128 #endif
29129 defsubr (&Sline_pixel_height);
29130 defsubr (&Sformat_mode_line);
29131 defsubr (&Sinvisible_p);
29132 defsubr (&Scurrent_bidi_paragraph_direction);
29133 defsubr (&Smove_point_visually);
29135 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29136 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29137 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29138 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29139 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29140 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29141 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29142 DEFSYM (Qeval, "eval");
29143 DEFSYM (QCdata, ":data");
29144 DEFSYM (Qdisplay, "display");
29145 DEFSYM (Qspace_width, "space-width");
29146 DEFSYM (Qraise, "raise");
29147 DEFSYM (Qslice, "slice");
29148 DEFSYM (Qspace, "space");
29149 DEFSYM (Qmargin, "margin");
29150 DEFSYM (Qpointer, "pointer");
29151 DEFSYM (Qleft_margin, "left-margin");
29152 DEFSYM (Qright_margin, "right-margin");
29153 DEFSYM (Qcenter, "center");
29154 DEFSYM (Qline_height, "line-height");
29155 DEFSYM (QCalign_to, ":align-to");
29156 DEFSYM (QCrelative_width, ":relative-width");
29157 DEFSYM (QCrelative_height, ":relative-height");
29158 DEFSYM (QCeval, ":eval");
29159 DEFSYM (QCpropertize, ":propertize");
29160 DEFSYM (QCfile, ":file");
29161 DEFSYM (Qfontified, "fontified");
29162 DEFSYM (Qfontification_functions, "fontification-functions");
29163 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29164 DEFSYM (Qescape_glyph, "escape-glyph");
29165 DEFSYM (Qnobreak_space, "nobreak-space");
29166 DEFSYM (Qimage, "image");
29167 DEFSYM (Qtext, "text");
29168 DEFSYM (Qboth, "both");
29169 DEFSYM (Qboth_horiz, "both-horiz");
29170 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29171 DEFSYM (QCmap, ":map");
29172 DEFSYM (QCpointer, ":pointer");
29173 DEFSYM (Qrect, "rect");
29174 DEFSYM (Qcircle, "circle");
29175 DEFSYM (Qpoly, "poly");
29176 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29177 DEFSYM (Qgrow_only, "grow-only");
29178 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29179 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29180 DEFSYM (Qposition, "position");
29181 DEFSYM (Qbuffer_position, "buffer-position");
29182 DEFSYM (Qobject, "object");
29183 DEFSYM (Qbar, "bar");
29184 DEFSYM (Qhbar, "hbar");
29185 DEFSYM (Qbox, "box");
29186 DEFSYM (Qhollow, "hollow");
29187 DEFSYM (Qhand, "hand");
29188 DEFSYM (Qarrow, "arrow");
29189 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29191 list_of_error = list1 (list2 (intern_c_string ("error"),
29192 intern_c_string ("void-variable")));
29193 staticpro (&list_of_error);
29195 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29196 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29197 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29198 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29200 echo_buffer[0] = echo_buffer[1] = Qnil;
29201 staticpro (&echo_buffer[0]);
29202 staticpro (&echo_buffer[1]);
29204 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29205 staticpro (&echo_area_buffer[0]);
29206 staticpro (&echo_area_buffer[1]);
29208 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29209 staticpro (&Vmessages_buffer_name);
29211 mode_line_proptrans_alist = Qnil;
29212 staticpro (&mode_line_proptrans_alist);
29213 mode_line_string_list = Qnil;
29214 staticpro (&mode_line_string_list);
29215 mode_line_string_face = Qnil;
29216 staticpro (&mode_line_string_face);
29217 mode_line_string_face_prop = Qnil;
29218 staticpro (&mode_line_string_face_prop);
29219 Vmode_line_unwind_vector = Qnil;
29220 staticpro (&Vmode_line_unwind_vector);
29222 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29224 help_echo_string = Qnil;
29225 staticpro (&help_echo_string);
29226 help_echo_object = Qnil;
29227 staticpro (&help_echo_object);
29228 help_echo_window = Qnil;
29229 staticpro (&help_echo_window);
29230 previous_help_echo_string = Qnil;
29231 staticpro (&previous_help_echo_string);
29232 help_echo_pos = -1;
29234 DEFSYM (Qright_to_left, "right-to-left");
29235 DEFSYM (Qleft_to_right, "left-to-right");
29237 #ifdef HAVE_WINDOW_SYSTEM
29238 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29239 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29240 For example, if a block cursor is over a tab, it will be drawn as
29241 wide as that tab on the display. */);
29242 x_stretch_cursor_p = 0;
29243 #endif
29245 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29246 doc: /* Non-nil means highlight trailing whitespace.
29247 The face used for trailing whitespace is `trailing-whitespace'. */);
29248 Vshow_trailing_whitespace = Qnil;
29250 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29251 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29252 If the value is t, Emacs highlights non-ASCII chars which have the
29253 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29254 or `escape-glyph' face respectively.
29256 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29257 U+2011 (non-breaking hyphen) are affected.
29259 Any other non-nil value means to display these characters as a escape
29260 glyph followed by an ordinary space or hyphen.
29262 A value of nil means no special handling of these characters. */);
29263 Vnobreak_char_display = Qt;
29265 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29266 doc: /* The pointer shape to show in void text areas.
29267 A value of nil means to show the text pointer. Other options are `arrow',
29268 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29269 Vvoid_text_area_pointer = Qarrow;
29271 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29272 doc: /* Non-nil means don't actually do any redisplay.
29273 This is used for internal purposes. */);
29274 Vinhibit_redisplay = Qnil;
29276 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29277 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29278 Vglobal_mode_string = Qnil;
29280 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29281 doc: /* Marker for where to display an arrow on top of the buffer text.
29282 This must be the beginning of a line in order to work.
29283 See also `overlay-arrow-string'. */);
29284 Voverlay_arrow_position = Qnil;
29286 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29287 doc: /* String to display as an arrow in non-window frames.
29288 See also `overlay-arrow-position'. */);
29289 Voverlay_arrow_string = build_pure_c_string ("=>");
29291 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29292 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29293 The symbols on this list are examined during redisplay to determine
29294 where to display overlay arrows. */);
29295 Voverlay_arrow_variable_list
29296 = list1 (intern_c_string ("overlay-arrow-position"));
29298 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29299 doc: /* The number of lines to try scrolling a window by when point moves out.
29300 If that fails to bring point back on frame, point is centered instead.
29301 If this is zero, point is always centered after it moves off frame.
29302 If you want scrolling to always be a line at a time, you should set
29303 `scroll-conservatively' to a large value rather than set this to 1. */);
29305 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29306 doc: /* Scroll up to this many lines, to bring point back on screen.
29307 If point moves off-screen, redisplay will scroll by up to
29308 `scroll-conservatively' lines in order to bring point just barely
29309 onto the screen again. If that cannot be done, then redisplay
29310 recenters point as usual.
29312 If the value is greater than 100, redisplay will never recenter point,
29313 but will always scroll just enough text to bring point into view, even
29314 if you move far away.
29316 A value of zero means always recenter point if it moves off screen. */);
29317 scroll_conservatively = 0;
29319 DEFVAR_INT ("scroll-margin", scroll_margin,
29320 doc: /* Number of lines of margin at the top and bottom of a window.
29321 Recenter the window whenever point gets within this many lines
29322 of the top or bottom of the window. */);
29323 scroll_margin = 0;
29325 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29326 doc: /* Pixels per inch value for non-window system displays.
29327 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29328 Vdisplay_pixels_per_inch = make_float (72.0);
29330 #ifdef GLYPH_DEBUG
29331 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29332 #endif
29334 DEFVAR_LISP ("truncate-partial-width-windows",
29335 Vtruncate_partial_width_windows,
29336 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29337 For an integer value, truncate lines in each window narrower than the
29338 full frame width, provided the window width is less than that integer;
29339 otherwise, respect the value of `truncate-lines'.
29341 For any other non-nil value, truncate lines in all windows that do
29342 not span the full frame width.
29344 A value of nil means to respect the value of `truncate-lines'.
29346 If `word-wrap' is enabled, you might want to reduce this. */);
29347 Vtruncate_partial_width_windows = make_number (50);
29349 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29350 doc: /* Maximum buffer size for which line number should be displayed.
29351 If the buffer is bigger than this, the line number does not appear
29352 in the mode line. A value of nil means no limit. */);
29353 Vline_number_display_limit = Qnil;
29355 DEFVAR_INT ("line-number-display-limit-width",
29356 line_number_display_limit_width,
29357 doc: /* Maximum line width (in characters) for line number display.
29358 If the average length of the lines near point is bigger than this, then the
29359 line number may be omitted from the mode line. */);
29360 line_number_display_limit_width = 200;
29362 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29363 doc: /* Non-nil means highlight region even in nonselected windows. */);
29364 highlight_nonselected_windows = 0;
29366 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29367 doc: /* Non-nil if more than one frame is visible on this display.
29368 Minibuffer-only frames don't count, but iconified frames do.
29369 This variable is not guaranteed to be accurate except while processing
29370 `frame-title-format' and `icon-title-format'. */);
29372 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29373 doc: /* Template for displaying the title bar of visible frames.
29374 \(Assuming the window manager supports this feature.)
29376 This variable has the same structure as `mode-line-format', except that
29377 the %c and %l constructs are ignored. It is used only on frames for
29378 which no explicit name has been set \(see `modify-frame-parameters'). */);
29380 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29381 doc: /* Template for displaying the title bar of an iconified frame.
29382 \(Assuming the window manager supports this feature.)
29383 This variable has the same structure as `mode-line-format' (which see),
29384 and is used only on frames for which no explicit name has been set
29385 \(see `modify-frame-parameters'). */);
29386 Vicon_title_format
29387 = Vframe_title_format
29388 = listn (CONSTYPE_PURE, 3,
29389 intern_c_string ("multiple-frames"),
29390 build_pure_c_string ("%b"),
29391 listn (CONSTYPE_PURE, 4,
29392 empty_unibyte_string,
29393 intern_c_string ("invocation-name"),
29394 build_pure_c_string ("@"),
29395 intern_c_string ("system-name")));
29397 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29398 doc: /* Maximum number of lines to keep in the message log buffer.
29399 If nil, disable message logging. If t, log messages but don't truncate
29400 the buffer when it becomes large. */);
29401 Vmessage_log_max = make_number (1000);
29403 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29404 doc: /* Functions called before redisplay, if window sizes have changed.
29405 The value should be a list of functions that take one argument.
29406 Just before redisplay, for each frame, if any of its windows have changed
29407 size since the last redisplay, or have been split or deleted,
29408 all the functions in the list are called, with the frame as argument. */);
29409 Vwindow_size_change_functions = Qnil;
29411 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29412 doc: /* List of functions to call before redisplaying a window with scrolling.
29413 Each function is called with two arguments, the window and its new
29414 display-start position. Note that these functions are also called by
29415 `set-window-buffer'. Also note that the value of `window-end' is not
29416 valid when these functions are called.
29418 Warning: Do not use this feature to alter the way the window
29419 is scrolled. It is not designed for that, and such use probably won't
29420 work. */);
29421 Vwindow_scroll_functions = Qnil;
29423 DEFVAR_LISP ("window-text-change-functions",
29424 Vwindow_text_change_functions,
29425 doc: /* Functions to call in redisplay when text in the window might change. */);
29426 Vwindow_text_change_functions = Qnil;
29428 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29429 doc: /* Functions called when redisplay of a window reaches the end trigger.
29430 Each function is called with two arguments, the window and the end trigger value.
29431 See `set-window-redisplay-end-trigger'. */);
29432 Vredisplay_end_trigger_functions = Qnil;
29434 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29435 doc: /* Non-nil means autoselect window with mouse pointer.
29436 If nil, do not autoselect windows.
29437 A positive number means delay autoselection by that many seconds: a
29438 window is autoselected only after the mouse has remained in that
29439 window for the duration of the delay.
29440 A negative number has a similar effect, but causes windows to be
29441 autoselected only after the mouse has stopped moving. \(Because of
29442 the way Emacs compares mouse events, you will occasionally wait twice
29443 that time before the window gets selected.\)
29444 Any other value means to autoselect window instantaneously when the
29445 mouse pointer enters it.
29447 Autoselection selects the minibuffer only if it is active, and never
29448 unselects the minibuffer if it is active.
29450 When customizing this variable make sure that the actual value of
29451 `focus-follows-mouse' matches the behavior of your window manager. */);
29452 Vmouse_autoselect_window = Qnil;
29454 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29455 doc: /* Non-nil means automatically resize tool-bars.
29456 This dynamically changes the tool-bar's height to the minimum height
29457 that is needed to make all tool-bar items visible.
29458 If value is `grow-only', the tool-bar's height is only increased
29459 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29460 Vauto_resize_tool_bars = Qt;
29462 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29463 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29464 auto_raise_tool_bar_buttons_p = 1;
29466 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29467 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29468 make_cursor_line_fully_visible_p = 1;
29470 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29471 doc: /* Border below tool-bar in pixels.
29472 If an integer, use it as the height of the border.
29473 If it is one of `internal-border-width' or `border-width', use the
29474 value of the corresponding frame parameter.
29475 Otherwise, no border is added below the tool-bar. */);
29476 Vtool_bar_border = Qinternal_border_width;
29478 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29479 doc: /* Margin around tool-bar buttons in pixels.
29480 If an integer, use that for both horizontal and vertical margins.
29481 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29482 HORZ specifying the horizontal margin, and VERT specifying the
29483 vertical margin. */);
29484 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29486 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29487 doc: /* Relief thickness of tool-bar buttons. */);
29488 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29490 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29491 doc: /* Tool bar style to use.
29492 It can be one of
29493 image - show images only
29494 text - show text only
29495 both - show both, text below image
29496 both-horiz - show text to the right of the image
29497 text-image-horiz - show text to the left of the image
29498 any other - use system default or image if no system default.
29500 This variable only affects the GTK+ toolkit version of Emacs. */);
29501 Vtool_bar_style = Qnil;
29503 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29504 doc: /* Maximum number of characters a label can have to be shown.
29505 The tool bar style must also show labels for this to have any effect, see
29506 `tool-bar-style'. */);
29507 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29509 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29510 doc: /* List of functions to call to fontify regions of text.
29511 Each function is called with one argument POS. Functions must
29512 fontify a region starting at POS in the current buffer, and give
29513 fontified regions the property `fontified'. */);
29514 Vfontification_functions = Qnil;
29515 Fmake_variable_buffer_local (Qfontification_functions);
29517 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29518 unibyte_display_via_language_environment,
29519 doc: /* Non-nil means display unibyte text according to language environment.
29520 Specifically, this means that raw bytes in the range 160-255 decimal
29521 are displayed by converting them to the equivalent multibyte characters
29522 according to the current language environment. As a result, they are
29523 displayed according to the current fontset.
29525 Note that this variable affects only how these bytes are displayed,
29526 but does not change the fact they are interpreted as raw bytes. */);
29527 unibyte_display_via_language_environment = 0;
29529 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29530 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29531 If a float, it specifies a fraction of the mini-window frame's height.
29532 If an integer, it specifies a number of lines. */);
29533 Vmax_mini_window_height = make_float (0.25);
29535 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29536 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29537 A value of nil means don't automatically resize mini-windows.
29538 A value of t means resize them to fit the text displayed in them.
29539 A value of `grow-only', the default, means let mini-windows grow only;
29540 they return to their normal size when the minibuffer is closed, or the
29541 echo area becomes empty. */);
29542 Vresize_mini_windows = Qgrow_only;
29544 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29545 doc: /* Alist specifying how to blink the cursor off.
29546 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29547 `cursor-type' frame-parameter or variable equals ON-STATE,
29548 comparing using `equal', Emacs uses OFF-STATE to specify
29549 how to blink it off. ON-STATE and OFF-STATE are values for
29550 the `cursor-type' frame parameter.
29552 If a frame's ON-STATE has no entry in this list,
29553 the frame's other specifications determine how to blink the cursor off. */);
29554 Vblink_cursor_alist = Qnil;
29556 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29557 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29558 If non-nil, windows are automatically scrolled horizontally to make
29559 point visible. */);
29560 automatic_hscrolling_p = 1;
29561 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29563 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29564 doc: /* How many columns away from the window edge point is allowed to get
29565 before automatic hscrolling will horizontally scroll the window. */);
29566 hscroll_margin = 5;
29568 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29569 doc: /* How many columns to scroll the window when point gets too close to the edge.
29570 When point is less than `hscroll-margin' columns from the window
29571 edge, automatic hscrolling will scroll the window by the amount of columns
29572 determined by this variable. If its value is a positive integer, scroll that
29573 many columns. If it's a positive floating-point number, it specifies the
29574 fraction of the window's width to scroll. If it's nil or zero, point will be
29575 centered horizontally after the scroll. Any other value, including negative
29576 numbers, are treated as if the value were zero.
29578 Automatic hscrolling always moves point outside the scroll margin, so if
29579 point was more than scroll step columns inside the margin, the window will
29580 scroll more than the value given by the scroll step.
29582 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29583 and `scroll-right' overrides this variable's effect. */);
29584 Vhscroll_step = make_number (0);
29586 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29587 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29588 Bind this around calls to `message' to let it take effect. */);
29589 message_truncate_lines = 0;
29591 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29592 doc: /* Normal hook run to update the menu bar definitions.
29593 Redisplay runs this hook before it redisplays the menu bar.
29594 This is used to update submenus such as Buffers,
29595 whose contents depend on various data. */);
29596 Vmenu_bar_update_hook = Qnil;
29598 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29599 doc: /* Frame for which we are updating a menu.
29600 The enable predicate for a menu binding should check this variable. */);
29601 Vmenu_updating_frame = Qnil;
29603 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29604 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29605 inhibit_menubar_update = 0;
29607 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29608 doc: /* Prefix prepended to all continuation lines at display time.
29609 The value may be a string, an image, or a stretch-glyph; it is
29610 interpreted in the same way as the value of a `display' text property.
29612 This variable is overridden by any `wrap-prefix' text or overlay
29613 property.
29615 To add a prefix to non-continuation lines, use `line-prefix'. */);
29616 Vwrap_prefix = Qnil;
29617 DEFSYM (Qwrap_prefix, "wrap-prefix");
29618 Fmake_variable_buffer_local (Qwrap_prefix);
29620 DEFVAR_LISP ("line-prefix", Vline_prefix,
29621 doc: /* Prefix prepended to all non-continuation lines at display time.
29622 The value may be a string, an image, or a stretch-glyph; it is
29623 interpreted in the same way as the value of a `display' text property.
29625 This variable is overridden by any `line-prefix' text or overlay
29626 property.
29628 To add a prefix to continuation lines, use `wrap-prefix'. */);
29629 Vline_prefix = Qnil;
29630 DEFSYM (Qline_prefix, "line-prefix");
29631 Fmake_variable_buffer_local (Qline_prefix);
29633 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29634 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29635 inhibit_eval_during_redisplay = 0;
29637 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29638 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29639 inhibit_free_realized_faces = 0;
29641 #ifdef GLYPH_DEBUG
29642 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29643 doc: /* Inhibit try_window_id display optimization. */);
29644 inhibit_try_window_id = 0;
29646 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29647 doc: /* Inhibit try_window_reusing display optimization. */);
29648 inhibit_try_window_reusing = 0;
29650 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29651 doc: /* Inhibit try_cursor_movement display optimization. */);
29652 inhibit_try_cursor_movement = 0;
29653 #endif /* GLYPH_DEBUG */
29655 DEFVAR_INT ("overline-margin", overline_margin,
29656 doc: /* Space between overline and text, in pixels.
29657 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29658 margin to the character height. */);
29659 overline_margin = 2;
29661 DEFVAR_INT ("underline-minimum-offset",
29662 underline_minimum_offset,
29663 doc: /* Minimum distance between baseline and underline.
29664 This can improve legibility of underlined text at small font sizes,
29665 particularly when using variable `x-use-underline-position-properties'
29666 with fonts that specify an UNDERLINE_POSITION relatively close to the
29667 baseline. The default value is 1. */);
29668 underline_minimum_offset = 1;
29670 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29671 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29672 This feature only works when on a window system that can change
29673 cursor shapes. */);
29674 display_hourglass_p = 1;
29676 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29677 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29678 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29680 #ifdef HAVE_WINDOW_SYSTEM
29681 hourglass_atimer = NULL;
29682 hourglass_shown_p = 0;
29683 #endif /* HAVE_WINDOW_SYSTEM */
29685 DEFSYM (Qglyphless_char, "glyphless-char");
29686 DEFSYM (Qhex_code, "hex-code");
29687 DEFSYM (Qempty_box, "empty-box");
29688 DEFSYM (Qthin_space, "thin-space");
29689 DEFSYM (Qzero_width, "zero-width");
29691 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
29692 doc: /* Function run just before redisplay.
29693 It is called with one argument, which is the set of windows that are to
29694 be redisplayed. This set can be nil (meaning, only the selected window),
29695 or t (meaning all windows). */);
29696 Vpre_redisplay_function = intern ("ignore");
29698 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29699 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29701 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29702 doc: /* Char-table defining glyphless characters.
29703 Each element, if non-nil, should be one of the following:
29704 an ASCII acronym string: display this string in a box
29705 `hex-code': display the hexadecimal code of a character in a box
29706 `empty-box': display as an empty box
29707 `thin-space': display as 1-pixel width space
29708 `zero-width': don't display
29709 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29710 display method for graphical terminals and text terminals respectively.
29711 GRAPHICAL and TEXT should each have one of the values listed above.
29713 The char-table has one extra slot to control the display of a character for
29714 which no font is found. This slot only takes effect on graphical terminals.
29715 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29716 `thin-space'. The default is `empty-box'. */);
29717 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29718 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29719 Qempty_box);
29721 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29722 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29723 Vdebug_on_message = Qnil;
29727 /* Initialize this module when Emacs starts. */
29729 void
29730 init_xdisp (void)
29732 CHARPOS (this_line_start_pos) = 0;
29734 if (!noninteractive)
29736 struct window *m = XWINDOW (minibuf_window);
29737 Lisp_Object frame = m->frame;
29738 struct frame *f = XFRAME (frame);
29739 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29740 struct window *r = XWINDOW (root);
29741 int i;
29743 echo_area_window = minibuf_window;
29745 r->top_line = FRAME_TOP_MARGIN (f);
29746 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29747 r->total_cols = FRAME_COLS (f);
29749 m->top_line = FRAME_LINES (f) - 1;
29750 m->total_lines = 1;
29751 m->total_cols = FRAME_COLS (f);
29753 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29754 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29755 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29757 /* The default ellipsis glyphs `...'. */
29758 for (i = 0; i < 3; ++i)
29759 default_invis_vector[i] = make_number ('.');
29763 /* Allocate the buffer for frame titles.
29764 Also used for `format-mode-line'. */
29765 int size = 100;
29766 mode_line_noprop_buf = xmalloc (size);
29767 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29768 mode_line_noprop_ptr = mode_line_noprop_buf;
29769 mode_line_target = MODE_LINE_DISPLAY;
29772 help_echo_showing_p = 0;
29775 #ifdef HAVE_WINDOW_SYSTEM
29777 /* Platform-independent portion of hourglass implementation. */
29779 /* Cancel a currently active hourglass timer, and start a new one. */
29780 void
29781 start_hourglass (void)
29783 struct timespec delay;
29785 cancel_hourglass ();
29787 if (INTEGERP (Vhourglass_delay)
29788 && XINT (Vhourglass_delay) > 0)
29789 delay = make_timespec (min (XINT (Vhourglass_delay),
29790 TYPE_MAXIMUM (time_t)),
29792 else if (FLOATP (Vhourglass_delay)
29793 && XFLOAT_DATA (Vhourglass_delay) > 0)
29794 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29795 else
29796 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29798 #ifdef HAVE_NTGUI
29800 extern void w32_note_current_window (void);
29801 w32_note_current_window ();
29803 #endif /* HAVE_NTGUI */
29805 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29806 show_hourglass, NULL);
29810 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29811 shown. */
29812 void
29813 cancel_hourglass (void)
29815 if (hourglass_atimer)
29817 cancel_atimer (hourglass_atimer);
29818 hourglass_atimer = NULL;
29821 if (hourglass_shown_p)
29822 hide_hourglass ();
29825 #endif /* HAVE_WINDOW_SYSTEM */