Make pcomplete less eager to add an extra space.
[emacs.git] / src / xdisp.c
blob5ee5a46601cfac32eca0a3df946705d26825c974
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 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 "keyboard.h"
279 #include "frame.h"
280 #include "window.h"
281 #include "termchar.h"
282 #include "dispextern.h"
283 #include "character.h"
284 #include "buffer.h"
285 #include "charset.h"
286 #include "indent.h"
287 #include "commands.h"
288 #include "keymap.h"
289 #include "macros.h"
290 #include "disptab.h"
291 #include "termhooks.h"
292 #include "termopts.h"
293 #include "intervals.h"
294 #include "coding.h"
295 #include "process.h"
296 #include "region-cache.h"
297 #include "font.h"
298 #include "fontset.h"
299 #include "blockinput.h"
301 #ifdef HAVE_X_WINDOWS
302 #include "xterm.h"
303 #endif
304 #ifdef WINDOWSNT
305 #include "w32term.h"
306 #endif
307 #ifdef HAVE_NS
308 #include "nsterm.h"
309 #endif
310 #ifdef USE_GTK
311 #include "gtkutil.h"
312 #endif
314 #include "font.h"
316 #ifndef FRAME_X_OUTPUT
317 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
318 #endif
320 #define INFINITY 10000000
322 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
323 Lisp_Object Qwindow_scroll_functions;
324 static Lisp_Object Qwindow_text_change_functions;
325 static Lisp_Object Qredisplay_end_trigger_functions;
326 Lisp_Object Qinhibit_point_motion_hooks;
327 static Lisp_Object QCeval, QCpropertize;
328 Lisp_Object QCfile, QCdata;
329 static Lisp_Object Qfontified;
330 static Lisp_Object Qgrow_only;
331 static Lisp_Object Qinhibit_eval_during_redisplay;
332 static Lisp_Object Qbuffer_position, Qposition, Qobject;
333 static Lisp_Object Qright_to_left, Qleft_to_right;
335 /* Cursor shapes */
336 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338 /* Pointer shapes */
339 static Lisp_Object Qarrow, Qhand;
340 Lisp_Object Qtext;
342 /* Holds the list (error). */
343 static Lisp_Object list_of_error;
345 static Lisp_Object Qfontification_functions;
347 static Lisp_Object Qwrap_prefix;
348 static Lisp_Object Qline_prefix;
350 /* Non-nil means don't actually do any redisplay. */
352 Lisp_Object Qinhibit_redisplay;
354 /* Names of text properties relevant for redisplay. */
356 Lisp_Object Qdisplay;
358 Lisp_Object Qspace, QCalign_to;
359 static Lisp_Object QCrelative_width, QCrelative_height;
360 Lisp_Object Qleft_margin, Qright_margin;
361 static Lisp_Object Qspace_width, Qraise;
362 static Lisp_Object Qslice;
363 Lisp_Object Qcenter;
364 static Lisp_Object Qmargin, Qpointer;
365 static Lisp_Object Qline_height;
367 /* These setters are used only in this file, so they can be private. */
368 static inline void
369 wset_base_line_number (struct window *w, Lisp_Object val)
371 w->base_line_number = val;
373 static inline void
374 wset_base_line_pos (struct window *w, Lisp_Object val)
376 w->base_line_pos = val;
378 static inline void
379 wset_column_number_displayed (struct window *w, Lisp_Object val)
381 w->column_number_displayed = val;
383 static inline void
384 wset_region_showing (struct window *w, Lisp_Object val)
386 w->region_showing = val;
389 #ifdef HAVE_WINDOW_SYSTEM
391 /* Test if overflow newline into fringe. Called with iterator IT
392 at or past right window margin, and with IT->current_x set. */
394 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
395 (!NILP (Voverflow_newline_into_fringe) \
396 && FRAME_WINDOW_P ((IT)->f) \
397 && ((IT)->bidi_it.paragraph_dir == R2L \
398 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
399 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
400 && (IT)->current_x == (IT)->last_visible_x \
401 && (IT)->line_wrap != WORD_WRAP)
403 #else /* !HAVE_WINDOW_SYSTEM */
404 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
405 #endif /* HAVE_WINDOW_SYSTEM */
407 /* Test if the display element loaded in IT, or the underlying buffer
408 or string character, is a space or a TAB character. This is used
409 to determine where word wrapping can occur. */
411 #define IT_DISPLAYING_WHITESPACE(it) \
412 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
413 || ((STRINGP (it->string) \
414 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
415 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
416 || (it->s \
417 && (it->s[IT_BYTEPOS (*it)] == ' ' \
418 || it->s[IT_BYTEPOS (*it)] == '\t')) \
419 || (IT_BYTEPOS (*it) < ZV_BYTE \
420 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
421 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
423 /* Name of the face used to highlight trailing whitespace. */
425 static Lisp_Object Qtrailing_whitespace;
427 /* Name and number of the face used to highlight escape glyphs. */
429 static Lisp_Object Qescape_glyph;
431 /* Name and number of the face used to highlight non-breaking spaces. */
433 static Lisp_Object Qnobreak_space;
435 /* The symbol `image' which is the car of the lists used to represent
436 images in Lisp. Also a tool bar style. */
438 Lisp_Object Qimage;
440 /* The image map types. */
441 Lisp_Object QCmap;
442 static Lisp_Object QCpointer;
443 static Lisp_Object Qrect, Qcircle, Qpoly;
445 /* Tool bar styles */
446 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
448 /* Non-zero means print newline to stdout before next mini-buffer
449 message. */
451 int noninteractive_need_newline;
453 /* Non-zero means print newline to message log before next message. */
455 static int message_log_need_newline;
457 /* Three markers that message_dolog uses.
458 It could allocate them itself, but that causes trouble
459 in handling memory-full errors. */
460 static Lisp_Object message_dolog_marker1;
461 static Lisp_Object message_dolog_marker2;
462 static Lisp_Object message_dolog_marker3;
464 /* The buffer position of the first character appearing entirely or
465 partially on the line of the selected window which contains the
466 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
467 redisplay optimization in redisplay_internal. */
469 static struct text_pos this_line_start_pos;
471 /* Number of characters past the end of the line above, including the
472 terminating newline. */
474 static struct text_pos this_line_end_pos;
476 /* The vertical positions and the height of this line. */
478 static int this_line_vpos;
479 static int this_line_y;
480 static int this_line_pixel_height;
482 /* X position at which this display line starts. Usually zero;
483 negative if first character is partially visible. */
485 static int this_line_start_x;
487 /* The smallest character position seen by move_it_* functions as they
488 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
489 hscrolled lines, see display_line. */
491 static struct text_pos this_line_min_pos;
493 /* Buffer that this_line_.* variables are referring to. */
495 static struct buffer *this_line_buffer;
498 /* Values of those variables at last redisplay are stored as
499 properties on `overlay-arrow-position' symbol. However, if
500 Voverlay_arrow_position is a marker, last-arrow-position is its
501 numerical position. */
503 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
505 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
506 properties on a symbol in overlay-arrow-variable-list. */
508 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
510 Lisp_Object Qmenu_bar_update_hook;
512 /* Nonzero if an overlay arrow has been displayed in this window. */
514 static int overlay_arrow_seen;
516 /* Number of windows showing the buffer of the selected window (or
517 another buffer with the same base buffer). keyboard.c refers to
518 this. */
520 int buffer_shared;
522 /* Vector containing glyphs for an ellipsis `...'. */
524 static Lisp_Object default_invis_vector[3];
526 /* This is the window where the echo area message was displayed. It
527 is always a mini-buffer window, but it may not be the same window
528 currently active as a mini-buffer. */
530 Lisp_Object echo_area_window;
532 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
533 pushes the current message and the value of
534 message_enable_multibyte on the stack, the function restore_message
535 pops the stack and displays MESSAGE again. */
537 static Lisp_Object Vmessage_stack;
539 /* Nonzero means multibyte characters were enabled when the echo area
540 message was specified. */
542 static int message_enable_multibyte;
544 /* Nonzero if we should redraw the mode lines on the next redisplay. */
546 int update_mode_lines;
548 /* Nonzero if window sizes or contents have changed since last
549 redisplay that finished. */
551 int windows_or_buffers_changed;
553 /* Nonzero means a frame's cursor type has been changed. */
555 int cursor_type_changed;
557 /* Nonzero after display_mode_line if %l was used and it displayed a
558 line number. */
560 static int line_number_displayed;
562 /* The name of the *Messages* buffer, a string. */
564 static Lisp_Object Vmessages_buffer_name;
566 /* Current, index 0, and last displayed echo area message. Either
567 buffers from echo_buffers, or nil to indicate no message. */
569 Lisp_Object echo_area_buffer[2];
571 /* The buffers referenced from echo_area_buffer. */
573 static Lisp_Object echo_buffer[2];
575 /* A vector saved used in with_area_buffer to reduce consing. */
577 static Lisp_Object Vwith_echo_area_save_vector;
579 /* Non-zero means display_echo_area should display the last echo area
580 message again. Set by redisplay_preserve_echo_area. */
582 static int display_last_displayed_message_p;
584 /* Nonzero if echo area is being used by print; zero if being used by
585 message. */
587 static int message_buf_print;
589 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
591 static Lisp_Object Qinhibit_menubar_update;
592 static Lisp_Object Qmessage_truncate_lines;
594 /* Set to 1 in clear_message to make redisplay_internal aware
595 of an emptied echo area. */
597 static int message_cleared_p;
599 /* A scratch glyph row with contents used for generating truncation
600 glyphs. Also used in direct_output_for_insert. */
602 #define MAX_SCRATCH_GLYPHS 100
603 static struct glyph_row scratch_glyph_row;
604 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
606 /* Ascent and height of the last line processed by move_it_to. */
608 static int last_max_ascent, last_height;
610 /* Non-zero if there's a help-echo in the echo area. */
612 int help_echo_showing_p;
614 /* If >= 0, computed, exact values of mode-line and header-line height
615 to use in the macros CURRENT_MODE_LINE_HEIGHT and
616 CURRENT_HEADER_LINE_HEIGHT. */
618 int current_mode_line_height, current_header_line_height;
620 /* The maximum distance to look ahead for text properties. Values
621 that are too small let us call compute_char_face and similar
622 functions too often which is expensive. Values that are too large
623 let us call compute_char_face and alike too often because we
624 might not be interested in text properties that far away. */
626 #define TEXT_PROP_DISTANCE_LIMIT 100
628 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
629 iterator state and later restore it. This is needed because the
630 bidi iterator on bidi.c keeps a stacked cache of its states, which
631 is really a singleton. When we use scratch iterator objects to
632 move around the buffer, we can cause the bidi cache to be pushed or
633 popped, and therefore we need to restore the cache state when we
634 return to the original iterator. */
635 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
636 do { \
637 if (CACHE) \
638 bidi_unshelve_cache (CACHE, 1); \
639 ITCOPY = ITORIG; \
640 CACHE = bidi_shelve_cache (); \
641 } while (0)
643 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
644 do { \
645 if (pITORIG != pITCOPY) \
646 *(pITORIG) = *(pITCOPY); \
647 bidi_unshelve_cache (CACHE, 0); \
648 CACHE = NULL; \
649 } while (0)
651 #ifdef GLYPH_DEBUG
653 /* Non-zero means print traces of redisplay if compiled with
654 GLYPH_DEBUG defined. */
656 int trace_redisplay_p;
658 #endif /* GLYPH_DEBUG */
660 #ifdef DEBUG_TRACE_MOVE
661 /* Non-zero means trace with TRACE_MOVE to stderr. */
662 int trace_move;
664 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
665 #else
666 #define TRACE_MOVE(x) (void) 0
667 #endif
669 static Lisp_Object Qauto_hscroll_mode;
671 /* Buffer being redisplayed -- for redisplay_window_error. */
673 static struct buffer *displayed_buffer;
675 /* Value returned from text property handlers (see below). */
677 enum prop_handled
679 HANDLED_NORMALLY,
680 HANDLED_RECOMPUTE_PROPS,
681 HANDLED_OVERLAY_STRING_CONSUMED,
682 HANDLED_RETURN
685 /* A description of text properties that redisplay is interested
686 in. */
688 struct props
690 /* The name of the property. */
691 Lisp_Object *name;
693 /* A unique index for the property. */
694 enum prop_idx idx;
696 /* A handler function called to set up iterator IT from the property
697 at IT's current position. Value is used to steer handle_stop. */
698 enum prop_handled (*handler) (struct it *it);
701 static enum prop_handled handle_face_prop (struct it *);
702 static enum prop_handled handle_invisible_prop (struct it *);
703 static enum prop_handled handle_display_prop (struct it *);
704 static enum prop_handled handle_composition_prop (struct it *);
705 static enum prop_handled handle_overlay_change (struct it *);
706 static enum prop_handled handle_fontified_prop (struct it *);
708 /* Properties handled by iterators. */
710 static struct props it_props[] =
712 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
713 /* Handle `face' before `display' because some sub-properties of
714 `display' need to know the face. */
715 {&Qface, FACE_PROP_IDX, handle_face_prop},
716 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
717 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
718 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
719 {NULL, 0, NULL}
722 /* Value is the position described by X. If X is a marker, value is
723 the marker_position of X. Otherwise, value is X. */
725 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
727 /* Enumeration returned by some move_it_.* functions internally. */
729 enum move_it_result
731 /* Not used. Undefined value. */
732 MOVE_UNDEFINED,
734 /* Move ended at the requested buffer position or ZV. */
735 MOVE_POS_MATCH_OR_ZV,
737 /* Move ended at the requested X pixel position. */
738 MOVE_X_REACHED,
740 /* Move within a line ended at the end of a line that must be
741 continued. */
742 MOVE_LINE_CONTINUED,
744 /* Move within a line ended at the end of a line that would
745 be displayed truncated. */
746 MOVE_LINE_TRUNCATED,
748 /* Move within a line ended at a line end. */
749 MOVE_NEWLINE_OR_CR
752 /* This counter is used to clear the face cache every once in a while
753 in redisplay_internal. It is incremented for each redisplay.
754 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
755 cleared. */
757 #define CLEAR_FACE_CACHE_COUNT 500
758 static int clear_face_cache_count;
760 /* Similarly for the image cache. */
762 #ifdef HAVE_WINDOW_SYSTEM
763 #define CLEAR_IMAGE_CACHE_COUNT 101
764 static int clear_image_cache_count;
766 /* Null glyph slice */
767 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
768 #endif
770 /* True while redisplay_internal is in progress. */
772 bool redisplaying_p;
774 static Lisp_Object Qinhibit_free_realized_faces;
775 static Lisp_Object Qmode_line_default_help_echo;
777 /* If a string, XTread_socket generates an event to display that string.
778 (The display is done in read_char.) */
780 Lisp_Object help_echo_string;
781 Lisp_Object help_echo_window;
782 Lisp_Object help_echo_object;
783 ptrdiff_t help_echo_pos;
785 /* Temporary variable for XTread_socket. */
787 Lisp_Object previous_help_echo_string;
789 /* Platform-independent portion of hourglass implementation. */
791 /* Non-zero means an hourglass cursor is currently shown. */
792 int hourglass_shown_p;
794 /* If non-null, an asynchronous timer that, when it expires, displays
795 an hourglass cursor on all frames. */
796 struct atimer *hourglass_atimer;
798 /* Name of the face used to display glyphless characters. */
799 Lisp_Object Qglyphless_char;
801 /* Symbol for the purpose of Vglyphless_char_display. */
802 static Lisp_Object Qglyphless_char_display;
804 /* Method symbols for Vglyphless_char_display. */
805 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
807 /* Default pixel width of `thin-space' display method. */
808 #define THIN_SPACE_WIDTH 1
810 /* Default number of seconds to wait before displaying an hourglass
811 cursor. */
812 #define DEFAULT_HOURGLASS_DELAY 1
815 /* Function prototypes. */
817 static void setup_for_ellipsis (struct it *, int);
818 static void set_iterator_to_next (struct it *, int);
819 static void mark_window_display_accurate_1 (struct window *, int);
820 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
821 static int display_prop_string_p (Lisp_Object, Lisp_Object);
822 static int cursor_row_p (struct glyph_row *);
823 static int redisplay_mode_lines (Lisp_Object, int);
824 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
826 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
828 static void handle_line_prefix (struct it *);
830 static void pint2str (char *, int, ptrdiff_t);
831 static void pint2hrstr (char *, int, ptrdiff_t);
832 static struct text_pos run_window_scroll_functions (Lisp_Object,
833 struct text_pos);
834 static void reconsider_clip_changes (struct window *, struct buffer *);
835 static int text_outside_line_unchanged_p (struct window *,
836 ptrdiff_t, ptrdiff_t);
837 static void store_mode_line_noprop_char (char);
838 static int store_mode_line_noprop (const char *, int, int);
839 static void handle_stop (struct it *);
840 static void handle_stop_backwards (struct it *, ptrdiff_t);
841 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
842 static void ensure_echo_area_buffers (void);
843 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
844 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
845 static int with_echo_area_buffer (struct window *, int,
846 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
847 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
848 static void clear_garbaged_frames (void);
849 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
850 static void pop_message (void);
851 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
852 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
853 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
854 static int display_echo_area (struct window *);
855 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
856 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
857 static Lisp_Object unwind_redisplay (Lisp_Object);
858 static int string_char_and_length (const unsigned char *, int *);
859 static struct text_pos display_prop_end (struct it *, Lisp_Object,
860 struct text_pos);
861 static int compute_window_start_on_continuation_line (struct window *);
862 static void insert_left_trunc_glyphs (struct it *);
863 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
864 Lisp_Object);
865 static void extend_face_to_end_of_line (struct it *);
866 static int append_space_for_newline (struct it *, int);
867 static int cursor_row_fully_visible_p (struct window *, int, int);
868 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
869 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
870 static int trailing_whitespace_p (ptrdiff_t);
871 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
872 static void push_it (struct it *, struct text_pos *);
873 static void iterate_out_of_display_property (struct it *);
874 static void pop_it (struct it *);
875 static void sync_frame_with_window_matrix_rows (struct window *);
876 static void select_frame_for_redisplay (Lisp_Object);
877 static void redisplay_internal (void);
878 static int echo_area_display (int);
879 static void redisplay_windows (Lisp_Object);
880 static void redisplay_window (Lisp_Object, int);
881 static Lisp_Object redisplay_window_error (Lisp_Object);
882 static Lisp_Object redisplay_window_0 (Lisp_Object);
883 static Lisp_Object redisplay_window_1 (Lisp_Object);
884 static int set_cursor_from_row (struct window *, struct glyph_row *,
885 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
886 int, int);
887 static int update_menu_bar (struct frame *, int, int);
888 static int try_window_reusing_current_matrix (struct window *);
889 static int try_window_id (struct window *);
890 static int display_line (struct it *);
891 static int display_mode_lines (struct window *);
892 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
893 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
894 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
895 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
896 static void display_menu_bar (struct window *);
897 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
898 ptrdiff_t *);
899 static int display_string (const char *, Lisp_Object, Lisp_Object,
900 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
901 static void compute_line_metrics (struct it *);
902 static void run_redisplay_end_trigger_hook (struct it *);
903 static int get_overlay_strings (struct it *, ptrdiff_t);
904 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
905 static void next_overlay_string (struct it *);
906 static void reseat (struct it *, struct text_pos, int);
907 static void reseat_1 (struct it *, struct text_pos, int);
908 static void back_to_previous_visible_line_start (struct it *);
909 void reseat_at_previous_visible_line_start (struct it *);
910 static void reseat_at_next_visible_line_start (struct it *, int);
911 static int next_element_from_ellipsis (struct it *);
912 static int next_element_from_display_vector (struct it *);
913 static int next_element_from_string (struct it *);
914 static int next_element_from_c_string (struct it *);
915 static int next_element_from_buffer (struct it *);
916 static int next_element_from_composition (struct it *);
917 static int next_element_from_image (struct it *);
918 static int next_element_from_stretch (struct it *);
919 static void load_overlay_strings (struct it *, ptrdiff_t);
920 static int init_from_display_pos (struct it *, struct window *,
921 struct display_pos *);
922 static void reseat_to_string (struct it *, const char *,
923 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
924 static int get_next_display_element (struct it *);
925 static enum move_it_result
926 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
927 enum move_operation_enum);
928 void move_it_vertically_backward (struct it *, int);
929 static void init_to_row_start (struct it *, struct window *,
930 struct glyph_row *);
931 static int init_to_row_end (struct it *, struct window *,
932 struct glyph_row *);
933 static void back_to_previous_line_start (struct it *);
934 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
935 static struct text_pos string_pos_nchars_ahead (struct text_pos,
936 Lisp_Object, ptrdiff_t);
937 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
938 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
939 static ptrdiff_t number_of_chars (const char *, int);
940 static void compute_stop_pos (struct it *);
941 static void compute_string_pos (struct text_pos *, struct text_pos,
942 Lisp_Object);
943 static int face_before_or_after_it_pos (struct it *, int);
944 static ptrdiff_t next_overlay_change (ptrdiff_t);
945 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
946 Lisp_Object, struct text_pos *, ptrdiff_t, int);
947 static int handle_single_display_spec (struct it *, Lisp_Object,
948 Lisp_Object, Lisp_Object,
949 struct text_pos *, ptrdiff_t, int, int);
950 static int underlying_face_id (struct it *);
951 static int in_ellipses_for_invisible_text_p (struct display_pos *,
952 struct window *);
954 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
955 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
957 #ifdef HAVE_WINDOW_SYSTEM
959 static void x_consider_frame_title (Lisp_Object);
960 static int tool_bar_lines_needed (struct frame *, int *);
961 static void update_tool_bar (struct frame *, int);
962 static void build_desired_tool_bar_string (struct frame *f);
963 static int redisplay_tool_bar (struct frame *);
964 static void display_tool_bar_line (struct it *, int);
965 static void notice_overwritten_cursor (struct window *,
966 enum glyph_row_area,
967 int, int, int, int);
968 static void append_stretch_glyph (struct it *, Lisp_Object,
969 int, int, int);
972 #endif /* HAVE_WINDOW_SYSTEM */
974 static void produce_special_glyphs (struct it *, enum display_element_type);
975 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
976 static int coords_in_mouse_face_p (struct window *, int, int);
980 /***********************************************************************
981 Window display dimensions
982 ***********************************************************************/
984 /* Return the bottom boundary y-position for text lines in window W.
985 This is the first y position at which a line cannot start.
986 It is relative to the top of the window.
988 This is the height of W minus the height of a mode line, if any. */
991 window_text_bottom_y (struct window *w)
993 int height = WINDOW_TOTAL_HEIGHT (w);
995 if (WINDOW_WANTS_MODELINE_P (w))
996 height -= CURRENT_MODE_LINE_HEIGHT (w);
997 return height;
1000 /* Return the pixel width of display area AREA of window W. AREA < 0
1001 means return the total width of W, not including fringes to
1002 the left and right of the window. */
1005 window_box_width (struct window *w, int area)
1007 int cols = XFASTINT (w->total_cols);
1008 int pixels = 0;
1010 if (!w->pseudo_window_p)
1012 cols -= WINDOW_SCROLL_BAR_COLS (w);
1014 if (area == TEXT_AREA)
1016 if (INTEGERP (w->left_margin_cols))
1017 cols -= XFASTINT (w->left_margin_cols);
1018 if (INTEGERP (w->right_margin_cols))
1019 cols -= XFASTINT (w->right_margin_cols);
1020 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1022 else if (area == LEFT_MARGIN_AREA)
1024 cols = (INTEGERP (w->left_margin_cols)
1025 ? XFASTINT (w->left_margin_cols) : 0);
1026 pixels = 0;
1028 else if (area == RIGHT_MARGIN_AREA)
1030 cols = (INTEGERP (w->right_margin_cols)
1031 ? XFASTINT (w->right_margin_cols) : 0);
1032 pixels = 0;
1036 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1040 /* Return the pixel height of the display area of window W, not
1041 including mode lines of W, if any. */
1044 window_box_height (struct window *w)
1046 struct frame *f = XFRAME (w->frame);
1047 int height = WINDOW_TOTAL_HEIGHT (w);
1049 eassert (height >= 0);
1051 /* Note: the code below that determines the mode-line/header-line
1052 height is essentially the same as that contained in the macro
1053 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1054 the appropriate glyph row has its `mode_line_p' flag set,
1055 and if it doesn't, uses estimate_mode_line_height instead. */
1057 if (WINDOW_WANTS_MODELINE_P (w))
1059 struct glyph_row *ml_row
1060 = (w->current_matrix && w->current_matrix->rows
1061 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1062 : 0);
1063 if (ml_row && ml_row->mode_line_p)
1064 height -= ml_row->height;
1065 else
1066 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1069 if (WINDOW_WANTS_HEADER_LINE_P (w))
1071 struct glyph_row *hl_row
1072 = (w->current_matrix && w->current_matrix->rows
1073 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1074 : 0);
1075 if (hl_row && hl_row->mode_line_p)
1076 height -= hl_row->height;
1077 else
1078 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1081 /* With a very small font and a mode-line that's taller than
1082 default, we might end up with a negative height. */
1083 return max (0, height);
1086 /* Return the window-relative coordinate of the left edge of display
1087 area AREA of window W. AREA < 0 means return the left edge of the
1088 whole window, to the right of the left fringe of W. */
1091 window_box_left_offset (struct window *w, int area)
1093 int x;
1095 if (w->pseudo_window_p)
1096 return 0;
1098 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1100 if (area == TEXT_AREA)
1101 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1102 + window_box_width (w, LEFT_MARGIN_AREA));
1103 else if (area == RIGHT_MARGIN_AREA)
1104 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1105 + window_box_width (w, LEFT_MARGIN_AREA)
1106 + window_box_width (w, TEXT_AREA)
1107 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1109 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1110 else if (area == LEFT_MARGIN_AREA
1111 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1112 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1114 return x;
1118 /* Return the window-relative coordinate of the right edge of display
1119 area AREA of window W. AREA < 0 means return the right edge of the
1120 whole window, to the left of the right fringe of W. */
1123 window_box_right_offset (struct window *w, int area)
1125 return window_box_left_offset (w, area) + window_box_width (w, area);
1128 /* Return the frame-relative coordinate of the left edge of display
1129 area AREA of window W. AREA < 0 means return the left edge of the
1130 whole window, to the right of the left fringe of W. */
1133 window_box_left (struct window *w, int area)
1135 struct frame *f = XFRAME (w->frame);
1136 int x;
1138 if (w->pseudo_window_p)
1139 return FRAME_INTERNAL_BORDER_WIDTH (f);
1141 x = (WINDOW_LEFT_EDGE_X (w)
1142 + window_box_left_offset (w, area));
1144 return x;
1148 /* Return the frame-relative coordinate of the right edge of display
1149 area AREA of window W. AREA < 0 means return the right edge of the
1150 whole window, to the left of the right fringe of W. */
1153 window_box_right (struct window *w, int area)
1155 return window_box_left (w, area) + window_box_width (w, area);
1158 /* Get the bounding box of the display area AREA of window W, without
1159 mode lines, in frame-relative coordinates. AREA < 0 means the
1160 whole window, not including the left and right fringes of
1161 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1162 coordinates of the upper-left corner of the box. Return in
1163 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1165 void
1166 window_box (struct window *w, int area, int *box_x, int *box_y,
1167 int *box_width, int *box_height)
1169 if (box_width)
1170 *box_width = window_box_width (w, area);
1171 if (box_height)
1172 *box_height = window_box_height (w);
1173 if (box_x)
1174 *box_x = window_box_left (w, area);
1175 if (box_y)
1177 *box_y = WINDOW_TOP_EDGE_Y (w);
1178 if (WINDOW_WANTS_HEADER_LINE_P (w))
1179 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1184 /* Get the bounding box of the display area AREA of window W, without
1185 mode lines. AREA < 0 means the whole window, not including the
1186 left and right fringe of the window. Return in *TOP_LEFT_X
1187 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1188 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1189 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1190 box. */
1192 static inline void
1193 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1194 int *bottom_right_x, int *bottom_right_y)
1196 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1197 bottom_right_y);
1198 *bottom_right_x += *top_left_x;
1199 *bottom_right_y += *top_left_y;
1204 /***********************************************************************
1205 Utilities
1206 ***********************************************************************/
1208 /* Return the bottom y-position of the line the iterator IT is in.
1209 This can modify IT's settings. */
1212 line_bottom_y (struct it *it)
1214 int line_height = it->max_ascent + it->max_descent;
1215 int line_top_y = it->current_y;
1217 if (line_height == 0)
1219 if (last_height)
1220 line_height = last_height;
1221 else if (IT_CHARPOS (*it) < ZV)
1223 move_it_by_lines (it, 1);
1224 line_height = (it->max_ascent || it->max_descent
1225 ? it->max_ascent + it->max_descent
1226 : last_height);
1228 else
1230 struct glyph_row *row = it->glyph_row;
1232 /* Use the default character height. */
1233 it->glyph_row = NULL;
1234 it->what = IT_CHARACTER;
1235 it->c = ' ';
1236 it->len = 1;
1237 PRODUCE_GLYPHS (it);
1238 line_height = it->ascent + it->descent;
1239 it->glyph_row = row;
1243 return line_top_y + line_height;
1246 /* Subroutine of pos_visible_p below. Extracts a display string, if
1247 any, from the display spec given as its argument. */
1248 static Lisp_Object
1249 string_from_display_spec (Lisp_Object spec)
1251 if (CONSP (spec))
1253 while (CONSP (spec))
1255 if (STRINGP (XCAR (spec)))
1256 return XCAR (spec);
1257 spec = XCDR (spec);
1260 else if (VECTORP (spec))
1262 ptrdiff_t i;
1264 for (i = 0; i < ASIZE (spec); i++)
1266 if (STRINGP (AREF (spec, i)))
1267 return AREF (spec, i);
1269 return Qnil;
1272 return spec;
1276 /* Limit insanely large values of W->hscroll on frame F to the largest
1277 value that will still prevent first_visible_x and last_visible_x of
1278 'struct it' from overflowing an int. */
1279 static inline int
1280 window_hscroll_limited (struct window *w, struct frame *f)
1282 ptrdiff_t window_hscroll = w->hscroll;
1283 int window_text_width = window_box_width (w, TEXT_AREA);
1284 int colwidth = FRAME_COLUMN_WIDTH (f);
1286 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1287 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1289 return window_hscroll;
1292 /* Return 1 if position CHARPOS is visible in window W.
1293 CHARPOS < 0 means return info about WINDOW_END position.
1294 If visible, set *X and *Y to pixel coordinates of top left corner.
1295 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1296 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1299 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1300 int *rtop, int *rbot, int *rowh, int *vpos)
1302 struct it it;
1303 void *itdata = bidi_shelve_cache ();
1304 struct text_pos top;
1305 int visible_p = 0;
1306 struct buffer *old_buffer = NULL;
1308 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1309 return visible_p;
1311 if (XBUFFER (w->buffer) != current_buffer)
1313 old_buffer = current_buffer;
1314 set_buffer_internal_1 (XBUFFER (w->buffer));
1317 SET_TEXT_POS_FROM_MARKER (top, w->start);
1318 /* Scrolling a minibuffer window via scroll bar when the echo area
1319 shows long text sometimes resets the minibuffer contents behind
1320 our backs. */
1321 if (CHARPOS (top) > ZV)
1322 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1324 /* Compute exact mode line heights. */
1325 if (WINDOW_WANTS_MODELINE_P (w))
1326 current_mode_line_height
1327 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1328 BVAR (current_buffer, mode_line_format));
1330 if (WINDOW_WANTS_HEADER_LINE_P (w))
1331 current_header_line_height
1332 = display_mode_line (w, HEADER_LINE_FACE_ID,
1333 BVAR (current_buffer, header_line_format));
1335 start_display (&it, w, top);
1336 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1337 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1339 if (charpos >= 0
1340 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1341 && IT_CHARPOS (it) >= charpos)
1342 /* When scanning backwards under bidi iteration, move_it_to
1343 stops at or _before_ CHARPOS, because it stops at or to
1344 the _right_ of the character at CHARPOS. */
1345 || (it.bidi_p && it.bidi_it.scan_dir == -1
1346 && IT_CHARPOS (it) <= charpos)))
1348 /* We have reached CHARPOS, or passed it. How the call to
1349 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1350 or covered by a display property, move_it_to stops at the end
1351 of the invisible text, to the right of CHARPOS. (ii) If
1352 CHARPOS is in a display vector, move_it_to stops on its last
1353 glyph. */
1354 int top_x = it.current_x;
1355 int top_y = it.current_y;
1356 /* Calling line_bottom_y may change it.method, it.position, etc. */
1357 enum it_method it_method = it.method;
1358 int bottom_y = (last_height = 0, line_bottom_y (&it));
1359 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1361 if (top_y < window_top_y)
1362 visible_p = bottom_y > window_top_y;
1363 else if (top_y < it.last_visible_y)
1364 visible_p = 1;
1365 if (bottom_y >= it.last_visible_y
1366 && it.bidi_p && it.bidi_it.scan_dir == -1
1367 && IT_CHARPOS (it) < charpos)
1369 /* When the last line of the window is scanned backwards
1370 under bidi iteration, we could be duped into thinking
1371 that we have passed CHARPOS, when in fact move_it_to
1372 simply stopped short of CHARPOS because it reached
1373 last_visible_y. To see if that's what happened, we call
1374 move_it_to again with a slightly larger vertical limit,
1375 and see if it actually moved vertically; if it did, we
1376 didn't really reach CHARPOS, which is beyond window end. */
1377 struct it save_it = it;
1378 /* Why 10? because we don't know how many canonical lines
1379 will the height of the next line(s) be. So we guess. */
1380 int ten_more_lines =
1381 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1383 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1384 MOVE_TO_POS | MOVE_TO_Y);
1385 if (it.current_y > top_y)
1386 visible_p = 0;
1388 it = save_it;
1390 if (visible_p)
1392 if (it_method == GET_FROM_DISPLAY_VECTOR)
1394 /* We stopped on the last glyph of a display vector.
1395 Try and recompute. Hack alert! */
1396 if (charpos < 2 || top.charpos >= charpos)
1397 top_x = it.glyph_row->x;
1398 else
1400 struct it it2;
1401 start_display (&it2, w, top);
1402 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1403 get_next_display_element (&it2);
1404 PRODUCE_GLYPHS (&it2);
1405 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1406 || it2.current_x > it2.last_visible_x)
1407 top_x = it.glyph_row->x;
1408 else
1410 top_x = it2.current_x;
1411 top_y = it2.current_y;
1415 else if (IT_CHARPOS (it) != charpos)
1417 Lisp_Object cpos = make_number (charpos);
1418 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1419 Lisp_Object string = string_from_display_spec (spec);
1420 int newline_in_string = 0;
1422 if (STRINGP (string))
1424 const char *s = SSDATA (string);
1425 const char *e = s + SBYTES (string);
1426 while (s < e)
1428 if (*s++ == '\n')
1430 newline_in_string = 1;
1431 break;
1435 /* The tricky code below is needed because there's a
1436 discrepancy between move_it_to and how we set cursor
1437 when the display line ends in a newline from a
1438 display string. move_it_to will stop _after_ such
1439 display strings, whereas set_cursor_from_row
1440 conspires with cursor_row_p to place the cursor on
1441 the first glyph produced from the display string. */
1443 /* We have overshoot PT because it is covered by a
1444 display property whose value is a string. If the
1445 string includes embedded newlines, we are also in the
1446 wrong display line. Backtrack to the correct line,
1447 where the display string begins. */
1448 if (newline_in_string)
1450 Lisp_Object startpos, endpos;
1451 EMACS_INT start, end;
1452 struct it it3;
1453 int it3_moved;
1455 /* Find the first and the last buffer positions
1456 covered by the display string. */
1457 endpos =
1458 Fnext_single_char_property_change (cpos, Qdisplay,
1459 Qnil, Qnil);
1460 startpos =
1461 Fprevious_single_char_property_change (endpos, Qdisplay,
1462 Qnil, Qnil);
1463 start = XFASTINT (startpos);
1464 end = XFASTINT (endpos);
1465 /* Move to the last buffer position before the
1466 display property. */
1467 start_display (&it3, w, top);
1468 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1469 /* Move forward one more line if the position before
1470 the display string is a newline or if it is the
1471 rightmost character on a line that is
1472 continued or word-wrapped. */
1473 if (it3.method == GET_FROM_BUFFER
1474 && it3.c == '\n')
1475 move_it_by_lines (&it3, 1);
1476 else if (move_it_in_display_line_to (&it3, -1,
1477 it3.current_x
1478 + it3.pixel_width,
1479 MOVE_TO_X)
1480 == MOVE_LINE_CONTINUED)
1482 move_it_by_lines (&it3, 1);
1483 /* When we are under word-wrap, the #$@%!
1484 move_it_by_lines moves 2 lines, so we need to
1485 fix that up. */
1486 if (it3.line_wrap == WORD_WRAP)
1487 move_it_by_lines (&it3, -1);
1490 /* Record the vertical coordinate of the display
1491 line where we wound up. */
1492 top_y = it3.current_y;
1493 if (it3.bidi_p)
1495 /* When characters are reordered for display,
1496 the character displayed to the left of the
1497 display string could be _after_ the display
1498 property in the logical order. Use the
1499 smallest vertical position of these two. */
1500 start_display (&it3, w, top);
1501 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1502 if (it3.current_y < top_y)
1503 top_y = it3.current_y;
1505 /* Move from the top of the window to the beginning
1506 of the display line where the display string
1507 begins. */
1508 start_display (&it3, w, top);
1509 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1510 /* If it3_moved stays zero after the 'while' loop
1511 below, that means we already were at a newline
1512 before the loop (e.g., the display string begins
1513 with a newline), so we don't need to (and cannot)
1514 inspect the glyphs of it3.glyph_row, because
1515 PRODUCE_GLYPHS will not produce anything for a
1516 newline, and thus it3.glyph_row stays at its
1517 stale content it got at top of the window. */
1518 it3_moved = 0;
1519 /* Finally, advance the iterator until we hit the
1520 first display element whose character position is
1521 CHARPOS, or until the first newline from the
1522 display string, which signals the end of the
1523 display line. */
1524 while (get_next_display_element (&it3))
1526 PRODUCE_GLYPHS (&it3);
1527 if (IT_CHARPOS (it3) == charpos
1528 || ITERATOR_AT_END_OF_LINE_P (&it3))
1529 break;
1530 it3_moved = 1;
1531 set_iterator_to_next (&it3, 0);
1533 top_x = it3.current_x - it3.pixel_width;
1534 /* Normally, we would exit the above loop because we
1535 found the display element whose character
1536 position is CHARPOS. For the contingency that we
1537 didn't, and stopped at the first newline from the
1538 display string, move back over the glyphs
1539 produced from the string, until we find the
1540 rightmost glyph not from the string. */
1541 if (it3_moved
1542 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1544 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1545 + it3.glyph_row->used[TEXT_AREA];
1547 while (EQ ((g - 1)->object, string))
1549 --g;
1550 top_x -= g->pixel_width;
1552 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1553 + it3.glyph_row->used[TEXT_AREA]);
1558 *x = top_x;
1559 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1560 *rtop = max (0, window_top_y - top_y);
1561 *rbot = max (0, bottom_y - it.last_visible_y);
1562 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1563 - max (top_y, window_top_y)));
1564 *vpos = it.vpos;
1567 else
1569 /* We were asked to provide info about WINDOW_END. */
1570 struct it it2;
1571 void *it2data = NULL;
1573 SAVE_IT (it2, it, it2data);
1574 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1575 move_it_by_lines (&it, 1);
1576 if (charpos < IT_CHARPOS (it)
1577 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1579 visible_p = 1;
1580 RESTORE_IT (&it2, &it2, it2data);
1581 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1582 *x = it2.current_x;
1583 *y = it2.current_y + it2.max_ascent - it2.ascent;
1584 *rtop = max (0, -it2.current_y);
1585 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1586 - it.last_visible_y));
1587 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1588 it.last_visible_y)
1589 - max (it2.current_y,
1590 WINDOW_HEADER_LINE_HEIGHT (w))));
1591 *vpos = it2.vpos;
1593 else
1594 bidi_unshelve_cache (it2data, 1);
1596 bidi_unshelve_cache (itdata, 0);
1598 if (old_buffer)
1599 set_buffer_internal_1 (old_buffer);
1601 current_header_line_height = current_mode_line_height = -1;
1603 if (visible_p && w->hscroll > 0)
1604 *x -=
1605 window_hscroll_limited (w, WINDOW_XFRAME (w))
1606 * WINDOW_FRAME_COLUMN_WIDTH (w);
1608 #if 0
1609 /* Debugging code. */
1610 if (visible_p)
1611 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1612 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1613 else
1614 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1615 #endif
1617 return visible_p;
1621 /* Return the next character from STR. Return in *LEN the length of
1622 the character. This is like STRING_CHAR_AND_LENGTH but never
1623 returns an invalid character. If we find one, we return a `?', but
1624 with the length of the invalid character. */
1626 static inline int
1627 string_char_and_length (const unsigned char *str, int *len)
1629 int c;
1631 c = STRING_CHAR_AND_LENGTH (str, *len);
1632 if (!CHAR_VALID_P (c))
1633 /* We may not change the length here because other places in Emacs
1634 don't use this function, i.e. they silently accept invalid
1635 characters. */
1636 c = '?';
1638 return c;
1643 /* Given a position POS containing a valid character and byte position
1644 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1646 static struct text_pos
1647 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1649 eassert (STRINGP (string) && nchars >= 0);
1651 if (STRING_MULTIBYTE (string))
1653 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1654 int len;
1656 while (nchars--)
1658 string_char_and_length (p, &len);
1659 p += len;
1660 CHARPOS (pos) += 1;
1661 BYTEPOS (pos) += len;
1664 else
1665 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1667 return pos;
1671 /* Value is the text position, i.e. character and byte position,
1672 for character position CHARPOS in STRING. */
1674 static inline struct text_pos
1675 string_pos (ptrdiff_t charpos, Lisp_Object string)
1677 struct text_pos pos;
1678 eassert (STRINGP (string));
1679 eassert (charpos >= 0);
1680 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1681 return pos;
1685 /* Value is a text position, i.e. character and byte position, for
1686 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1687 means recognize multibyte characters. */
1689 static struct text_pos
1690 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1692 struct text_pos pos;
1694 eassert (s != NULL);
1695 eassert (charpos >= 0);
1697 if (multibyte_p)
1699 int len;
1701 SET_TEXT_POS (pos, 0, 0);
1702 while (charpos--)
1704 string_char_and_length ((const unsigned char *) s, &len);
1705 s += len;
1706 CHARPOS (pos) += 1;
1707 BYTEPOS (pos) += len;
1710 else
1711 SET_TEXT_POS (pos, charpos, charpos);
1713 return pos;
1717 /* Value is the number of characters in C string S. MULTIBYTE_P
1718 non-zero means recognize multibyte characters. */
1720 static ptrdiff_t
1721 number_of_chars (const char *s, int multibyte_p)
1723 ptrdiff_t nchars;
1725 if (multibyte_p)
1727 ptrdiff_t rest = strlen (s);
1728 int len;
1729 const unsigned char *p = (const unsigned char *) s;
1731 for (nchars = 0; rest > 0; ++nchars)
1733 string_char_and_length (p, &len);
1734 rest -= len, p += len;
1737 else
1738 nchars = strlen (s);
1740 return nchars;
1744 /* Compute byte position NEWPOS->bytepos corresponding to
1745 NEWPOS->charpos. POS is a known position in string STRING.
1746 NEWPOS->charpos must be >= POS.charpos. */
1748 static void
1749 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1751 eassert (STRINGP (string));
1752 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1754 if (STRING_MULTIBYTE (string))
1755 *newpos = string_pos_nchars_ahead (pos, string,
1756 CHARPOS (*newpos) - CHARPOS (pos));
1757 else
1758 BYTEPOS (*newpos) = CHARPOS (*newpos);
1761 /* EXPORT:
1762 Return an estimation of the pixel height of mode or header lines on
1763 frame F. FACE_ID specifies what line's height to estimate. */
1766 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1768 #ifdef HAVE_WINDOW_SYSTEM
1769 if (FRAME_WINDOW_P (f))
1771 int height = FONT_HEIGHT (FRAME_FONT (f));
1773 /* This function is called so early when Emacs starts that the face
1774 cache and mode line face are not yet initialized. */
1775 if (FRAME_FACE_CACHE (f))
1777 struct face *face = FACE_FROM_ID (f, face_id);
1778 if (face)
1780 if (face->font)
1781 height = FONT_HEIGHT (face->font);
1782 if (face->box_line_width > 0)
1783 height += 2 * face->box_line_width;
1787 return height;
1789 #endif
1791 return 1;
1794 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1795 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1796 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1797 not force the value into range. */
1799 void
1800 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1801 int *x, int *y, NativeRectangle *bounds, int noclip)
1804 #ifdef HAVE_WINDOW_SYSTEM
1805 if (FRAME_WINDOW_P (f))
1807 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1808 even for negative values. */
1809 if (pix_x < 0)
1810 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1811 if (pix_y < 0)
1812 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1814 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1815 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1817 if (bounds)
1818 STORE_NATIVE_RECT (*bounds,
1819 FRAME_COL_TO_PIXEL_X (f, pix_x),
1820 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1821 FRAME_COLUMN_WIDTH (f) - 1,
1822 FRAME_LINE_HEIGHT (f) - 1);
1824 if (!noclip)
1826 if (pix_x < 0)
1827 pix_x = 0;
1828 else if (pix_x > FRAME_TOTAL_COLS (f))
1829 pix_x = FRAME_TOTAL_COLS (f);
1831 if (pix_y < 0)
1832 pix_y = 0;
1833 else if (pix_y > FRAME_LINES (f))
1834 pix_y = FRAME_LINES (f);
1837 #endif
1839 *x = pix_x;
1840 *y = pix_y;
1844 /* Find the glyph under window-relative coordinates X/Y in window W.
1845 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1846 strings. Return in *HPOS and *VPOS the row and column number of
1847 the glyph found. Return in *AREA the glyph area containing X.
1848 Value is a pointer to the glyph found or null if X/Y is not on
1849 text, or we can't tell because W's current matrix is not up to
1850 date. */
1852 static
1853 struct glyph *
1854 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1855 int *dx, int *dy, int *area)
1857 struct glyph *glyph, *end;
1858 struct glyph_row *row = NULL;
1859 int x0, i;
1861 /* Find row containing Y. Give up if some row is not enabled. */
1862 for (i = 0; i < w->current_matrix->nrows; ++i)
1864 row = MATRIX_ROW (w->current_matrix, i);
1865 if (!row->enabled_p)
1866 return NULL;
1867 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1868 break;
1871 *vpos = i;
1872 *hpos = 0;
1874 /* Give up if Y is not in the window. */
1875 if (i == w->current_matrix->nrows)
1876 return NULL;
1878 /* Get the glyph area containing X. */
1879 if (w->pseudo_window_p)
1881 *area = TEXT_AREA;
1882 x0 = 0;
1884 else
1886 if (x < window_box_left_offset (w, TEXT_AREA))
1888 *area = LEFT_MARGIN_AREA;
1889 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1891 else if (x < window_box_right_offset (w, TEXT_AREA))
1893 *area = TEXT_AREA;
1894 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1896 else
1898 *area = RIGHT_MARGIN_AREA;
1899 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1903 /* Find glyph containing X. */
1904 glyph = row->glyphs[*area];
1905 end = glyph + row->used[*area];
1906 x -= x0;
1907 while (glyph < end && x >= glyph->pixel_width)
1909 x -= glyph->pixel_width;
1910 ++glyph;
1913 if (glyph == end)
1914 return NULL;
1916 if (dx)
1918 *dx = x;
1919 *dy = y - (row->y + row->ascent - glyph->ascent);
1922 *hpos = glyph - row->glyphs[*area];
1923 return glyph;
1926 /* Convert frame-relative x/y to coordinates relative to window W.
1927 Takes pseudo-windows into account. */
1929 static void
1930 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1932 if (w->pseudo_window_p)
1934 /* A pseudo-window is always full-width, and starts at the
1935 left edge of the frame, plus a frame border. */
1936 struct frame *f = XFRAME (w->frame);
1937 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1938 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1940 else
1942 *x -= WINDOW_LEFT_EDGE_X (w);
1943 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1947 #ifdef HAVE_WINDOW_SYSTEM
1949 /* EXPORT:
1950 Return in RECTS[] at most N clipping rectangles for glyph string S.
1951 Return the number of stored rectangles. */
1954 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1956 XRectangle r;
1958 if (n <= 0)
1959 return 0;
1961 if (s->row->full_width_p)
1963 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1964 r.x = WINDOW_LEFT_EDGE_X (s->w);
1965 r.width = WINDOW_TOTAL_WIDTH (s->w);
1967 /* Unless displaying a mode or menu bar line, which are always
1968 fully visible, clip to the visible part of the row. */
1969 if (s->w->pseudo_window_p)
1970 r.height = s->row->visible_height;
1971 else
1972 r.height = s->height;
1974 else
1976 /* This is a text line that may be partially visible. */
1977 r.x = window_box_left (s->w, s->area);
1978 r.width = window_box_width (s->w, s->area);
1979 r.height = s->row->visible_height;
1982 if (s->clip_head)
1983 if (r.x < s->clip_head->x)
1985 if (r.width >= s->clip_head->x - r.x)
1986 r.width -= s->clip_head->x - r.x;
1987 else
1988 r.width = 0;
1989 r.x = s->clip_head->x;
1991 if (s->clip_tail)
1992 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1994 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1995 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1996 else
1997 r.width = 0;
2000 /* If S draws overlapping rows, it's sufficient to use the top and
2001 bottom of the window for clipping because this glyph string
2002 intentionally draws over other lines. */
2003 if (s->for_overlaps)
2005 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2006 r.height = window_text_bottom_y (s->w) - r.y;
2008 /* Alas, the above simple strategy does not work for the
2009 environments with anti-aliased text: if the same text is
2010 drawn onto the same place multiple times, it gets thicker.
2011 If the overlap we are processing is for the erased cursor, we
2012 take the intersection with the rectangle of the cursor. */
2013 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2015 XRectangle rc, r_save = r;
2017 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2018 rc.y = s->w->phys_cursor.y;
2019 rc.width = s->w->phys_cursor_width;
2020 rc.height = s->w->phys_cursor_height;
2022 x_intersect_rectangles (&r_save, &rc, &r);
2025 else
2027 /* Don't use S->y for clipping because it doesn't take partially
2028 visible lines into account. For example, it can be negative for
2029 partially visible lines at the top of a window. */
2030 if (!s->row->full_width_p
2031 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2032 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2033 else
2034 r.y = max (0, s->row->y);
2037 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2039 /* If drawing the cursor, don't let glyph draw outside its
2040 advertised boundaries. Cleartype does this under some circumstances. */
2041 if (s->hl == DRAW_CURSOR)
2043 struct glyph *glyph = s->first_glyph;
2044 int height, max_y;
2046 if (s->x > r.x)
2048 r.width -= s->x - r.x;
2049 r.x = s->x;
2051 r.width = min (r.width, glyph->pixel_width);
2053 /* If r.y is below window bottom, ensure that we still see a cursor. */
2054 height = min (glyph->ascent + glyph->descent,
2055 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2056 max_y = window_text_bottom_y (s->w) - height;
2057 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2058 if (s->ybase - glyph->ascent > max_y)
2060 r.y = max_y;
2061 r.height = height;
2063 else
2065 /* Don't draw cursor glyph taller than our actual glyph. */
2066 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2067 if (height < r.height)
2069 max_y = r.y + r.height;
2070 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2071 r.height = min (max_y - r.y, height);
2076 if (s->row->clip)
2078 XRectangle r_save = r;
2080 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2081 r.width = 0;
2084 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2085 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2087 #ifdef CONVERT_FROM_XRECT
2088 CONVERT_FROM_XRECT (r, *rects);
2089 #else
2090 *rects = r;
2091 #endif
2092 return 1;
2094 else
2096 /* If we are processing overlapping and allowed to return
2097 multiple clipping rectangles, we exclude the row of the glyph
2098 string from the clipping rectangle. This is to avoid drawing
2099 the same text on the environment with anti-aliasing. */
2100 #ifdef CONVERT_FROM_XRECT
2101 XRectangle rs[2];
2102 #else
2103 XRectangle *rs = rects;
2104 #endif
2105 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2107 if (s->for_overlaps & OVERLAPS_PRED)
2109 rs[i] = r;
2110 if (r.y + r.height > row_y)
2112 if (r.y < row_y)
2113 rs[i].height = row_y - r.y;
2114 else
2115 rs[i].height = 0;
2117 i++;
2119 if (s->for_overlaps & OVERLAPS_SUCC)
2121 rs[i] = r;
2122 if (r.y < row_y + s->row->visible_height)
2124 if (r.y + r.height > row_y + s->row->visible_height)
2126 rs[i].y = row_y + s->row->visible_height;
2127 rs[i].height = r.y + r.height - rs[i].y;
2129 else
2130 rs[i].height = 0;
2132 i++;
2135 n = i;
2136 #ifdef CONVERT_FROM_XRECT
2137 for (i = 0; i < n; i++)
2138 CONVERT_FROM_XRECT (rs[i], rects[i]);
2139 #endif
2140 return n;
2144 /* EXPORT:
2145 Return in *NR the clipping rectangle for glyph string S. */
2147 void
2148 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2150 get_glyph_string_clip_rects (s, nr, 1);
2154 /* EXPORT:
2155 Return the position and height of the phys cursor in window W.
2156 Set w->phys_cursor_width to width of phys cursor.
2159 void
2160 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2161 struct glyph *glyph, int *xp, int *yp, int *heightp)
2163 struct frame *f = XFRAME (WINDOW_FRAME (w));
2164 int x, y, wd, h, h0, y0;
2166 /* Compute the width of the rectangle to draw. If on a stretch
2167 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2168 rectangle as wide as the glyph, but use a canonical character
2169 width instead. */
2170 wd = glyph->pixel_width - 1;
2171 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2172 wd++; /* Why? */
2173 #endif
2175 x = w->phys_cursor.x;
2176 if (x < 0)
2178 wd += x;
2179 x = 0;
2182 if (glyph->type == STRETCH_GLYPH
2183 && !x_stretch_cursor_p)
2184 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2185 w->phys_cursor_width = wd;
2187 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2189 /* If y is below window bottom, ensure that we still see a cursor. */
2190 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2192 h = max (h0, glyph->ascent + glyph->descent);
2193 h0 = min (h0, glyph->ascent + glyph->descent);
2195 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2196 if (y < y0)
2198 h = max (h - (y0 - y) + 1, h0);
2199 y = y0 - 1;
2201 else
2203 y0 = window_text_bottom_y (w) - h0;
2204 if (y > y0)
2206 h += y - y0;
2207 y = y0;
2211 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2212 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2213 *heightp = h;
2217 * Remember which glyph the mouse is over.
2220 void
2221 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2223 Lisp_Object window;
2224 struct window *w;
2225 struct glyph_row *r, *gr, *end_row;
2226 enum window_part part;
2227 enum glyph_row_area area;
2228 int x, y, width, height;
2230 /* Try to determine frame pixel position and size of the glyph under
2231 frame pixel coordinates X/Y on frame F. */
2233 if (!f->glyphs_initialized_p
2234 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2235 NILP (window)))
2237 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2238 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2239 goto virtual_glyph;
2242 w = XWINDOW (window);
2243 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2244 height = WINDOW_FRAME_LINE_HEIGHT (w);
2246 x = window_relative_x_coord (w, part, gx);
2247 y = gy - WINDOW_TOP_EDGE_Y (w);
2249 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2250 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2252 if (w->pseudo_window_p)
2254 area = TEXT_AREA;
2255 part = ON_MODE_LINE; /* Don't adjust margin. */
2256 goto text_glyph;
2259 switch (part)
2261 case ON_LEFT_MARGIN:
2262 area = LEFT_MARGIN_AREA;
2263 goto text_glyph;
2265 case ON_RIGHT_MARGIN:
2266 area = RIGHT_MARGIN_AREA;
2267 goto text_glyph;
2269 case ON_HEADER_LINE:
2270 case ON_MODE_LINE:
2271 gr = (part == ON_HEADER_LINE
2272 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2273 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2274 gy = gr->y;
2275 area = TEXT_AREA;
2276 goto text_glyph_row_found;
2278 case ON_TEXT:
2279 area = TEXT_AREA;
2281 text_glyph:
2282 gr = 0; gy = 0;
2283 for (; r <= end_row && r->enabled_p; ++r)
2284 if (r->y + r->height > y)
2286 gr = r; gy = r->y;
2287 break;
2290 text_glyph_row_found:
2291 if (gr && gy <= y)
2293 struct glyph *g = gr->glyphs[area];
2294 struct glyph *end = g + gr->used[area];
2296 height = gr->height;
2297 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2298 if (gx + g->pixel_width > x)
2299 break;
2301 if (g < end)
2303 if (g->type == IMAGE_GLYPH)
2305 /* Don't remember when mouse is over image, as
2306 image may have hot-spots. */
2307 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2308 return;
2310 width = g->pixel_width;
2312 else
2314 /* Use nominal char spacing at end of line. */
2315 x -= gx;
2316 gx += (x / width) * width;
2319 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2320 gx += window_box_left_offset (w, area);
2322 else
2324 /* Use nominal line height at end of window. */
2325 gx = (x / width) * width;
2326 y -= gy;
2327 gy += (y / height) * height;
2329 break;
2331 case ON_LEFT_FRINGE:
2332 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2333 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2334 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2335 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2336 goto row_glyph;
2338 case ON_RIGHT_FRINGE:
2339 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2340 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2341 : window_box_right_offset (w, TEXT_AREA));
2342 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2343 goto row_glyph;
2345 case ON_SCROLL_BAR:
2346 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2348 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2349 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2350 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2351 : 0)));
2352 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2354 row_glyph:
2355 gr = 0, gy = 0;
2356 for (; r <= end_row && r->enabled_p; ++r)
2357 if (r->y + r->height > y)
2359 gr = r; gy = r->y;
2360 break;
2363 if (gr && gy <= y)
2364 height = gr->height;
2365 else
2367 /* Use nominal line height at end of window. */
2368 y -= gy;
2369 gy += (y / height) * height;
2371 break;
2373 default:
2375 virtual_glyph:
2376 /* If there is no glyph under the mouse, then we divide the screen
2377 into a grid of the smallest glyph in the frame, and use that
2378 as our "glyph". */
2380 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2381 round down even for negative values. */
2382 if (gx < 0)
2383 gx -= width - 1;
2384 if (gy < 0)
2385 gy -= height - 1;
2387 gx = (gx / width) * width;
2388 gy = (gy / height) * height;
2390 goto store_rect;
2393 gx += WINDOW_LEFT_EDGE_X (w);
2394 gy += WINDOW_TOP_EDGE_Y (w);
2396 store_rect:
2397 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2399 /* Visible feedback for debugging. */
2400 #if 0
2401 #if HAVE_X_WINDOWS
2402 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2403 f->output_data.x->normal_gc,
2404 gx, gy, width, height);
2405 #endif
2406 #endif
2410 #endif /* HAVE_WINDOW_SYSTEM */
2413 /***********************************************************************
2414 Lisp form evaluation
2415 ***********************************************************************/
2417 /* Error handler for safe_eval and safe_call. */
2419 static Lisp_Object
2420 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2422 add_to_log ("Error during redisplay: %S signaled %S",
2423 Flist (nargs, args), arg);
2424 return Qnil;
2427 /* Call function FUNC with the rest of NARGS - 1 arguments
2428 following. Return the result, or nil if something went
2429 wrong. Prevent redisplay during the evaluation. */
2431 Lisp_Object
2432 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2434 Lisp_Object val;
2436 if (inhibit_eval_during_redisplay)
2437 val = Qnil;
2438 else
2440 va_list ap;
2441 ptrdiff_t i;
2442 ptrdiff_t count = SPECPDL_INDEX ();
2443 struct gcpro gcpro1;
2444 Lisp_Object *args = alloca (nargs * word_size);
2446 args[0] = func;
2447 va_start (ap, func);
2448 for (i = 1; i < nargs; i++)
2449 args[i] = va_arg (ap, Lisp_Object);
2450 va_end (ap);
2452 GCPRO1 (args[0]);
2453 gcpro1.nvars = nargs;
2454 specbind (Qinhibit_redisplay, Qt);
2455 /* Use Qt to ensure debugger does not run,
2456 so there is no possibility of wanting to redisplay. */
2457 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2458 safe_eval_handler);
2459 UNGCPRO;
2460 val = unbind_to (count, val);
2463 return val;
2467 /* Call function FN with one argument ARG.
2468 Return the result, or nil if something went wrong. */
2470 Lisp_Object
2471 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2473 return safe_call (2, fn, arg);
2476 static Lisp_Object Qeval;
2478 Lisp_Object
2479 safe_eval (Lisp_Object sexpr)
2481 return safe_call1 (Qeval, sexpr);
2484 /* Call function FN with two arguments ARG1 and ARG2.
2485 Return the result, or nil if something went wrong. */
2487 Lisp_Object
2488 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2490 return safe_call (3, fn, arg1, arg2);
2495 /***********************************************************************
2496 Debugging
2497 ***********************************************************************/
2499 #if 0
2501 /* Define CHECK_IT to perform sanity checks on iterators.
2502 This is for debugging. It is too slow to do unconditionally. */
2504 static void
2505 check_it (struct it *it)
2507 if (it->method == GET_FROM_STRING)
2509 eassert (STRINGP (it->string));
2510 eassert (IT_STRING_CHARPOS (*it) >= 0);
2512 else
2514 eassert (IT_STRING_CHARPOS (*it) < 0);
2515 if (it->method == GET_FROM_BUFFER)
2517 /* Check that character and byte positions agree. */
2518 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2522 if (it->dpvec)
2523 eassert (it->current.dpvec_index >= 0);
2524 else
2525 eassert (it->current.dpvec_index < 0);
2528 #define CHECK_IT(IT) check_it ((IT))
2530 #else /* not 0 */
2532 #define CHECK_IT(IT) (void) 0
2534 #endif /* not 0 */
2537 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2539 /* Check that the window end of window W is what we expect it
2540 to be---the last row in the current matrix displaying text. */
2542 static void
2543 check_window_end (struct window *w)
2545 if (!MINI_WINDOW_P (w)
2546 && !NILP (w->window_end_valid))
2548 struct glyph_row *row;
2549 eassert ((row = MATRIX_ROW (w->current_matrix,
2550 XFASTINT (w->window_end_vpos)),
2551 !row->enabled_p
2552 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2553 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2557 #define CHECK_WINDOW_END(W) check_window_end ((W))
2559 #else
2561 #define CHECK_WINDOW_END(W) (void) 0
2563 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2567 /***********************************************************************
2568 Iterator initialization
2569 ***********************************************************************/
2571 /* Initialize IT for displaying current_buffer in window W, starting
2572 at character position CHARPOS. CHARPOS < 0 means that no buffer
2573 position is specified which is useful when the iterator is assigned
2574 a position later. BYTEPOS is the byte position corresponding to
2575 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2577 If ROW is not null, calls to produce_glyphs with IT as parameter
2578 will produce glyphs in that row.
2580 BASE_FACE_ID is the id of a base face to use. It must be one of
2581 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2582 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2583 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2585 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2586 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2587 will be initialized to use the corresponding mode line glyph row of
2588 the desired matrix of W. */
2590 void
2591 init_iterator (struct it *it, struct window *w,
2592 ptrdiff_t charpos, ptrdiff_t bytepos,
2593 struct glyph_row *row, enum face_id base_face_id)
2595 int highlight_region_p;
2596 enum face_id remapped_base_face_id = base_face_id;
2598 /* Some precondition checks. */
2599 eassert (w != NULL && it != NULL);
2600 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2601 && charpos <= ZV));
2603 /* If face attributes have been changed since the last redisplay,
2604 free realized faces now because they depend on face definitions
2605 that might have changed. Don't free faces while there might be
2606 desired matrices pending which reference these faces. */
2607 if (face_change_count && !inhibit_free_realized_faces)
2609 face_change_count = 0;
2610 free_all_realized_faces (Qnil);
2613 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2614 if (! NILP (Vface_remapping_alist))
2615 remapped_base_face_id
2616 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2618 /* Use one of the mode line rows of W's desired matrix if
2619 appropriate. */
2620 if (row == NULL)
2622 if (base_face_id == MODE_LINE_FACE_ID
2623 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2624 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2625 else if (base_face_id == HEADER_LINE_FACE_ID)
2626 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2629 /* Clear IT. */
2630 memset (it, 0, sizeof *it);
2631 it->current.overlay_string_index = -1;
2632 it->current.dpvec_index = -1;
2633 it->base_face_id = remapped_base_face_id;
2634 it->string = Qnil;
2635 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2636 it->paragraph_embedding = L2R;
2637 it->bidi_it.string.lstring = Qnil;
2638 it->bidi_it.string.s = NULL;
2639 it->bidi_it.string.bufpos = 0;
2641 /* The window in which we iterate over current_buffer: */
2642 XSETWINDOW (it->window, w);
2643 it->w = w;
2644 it->f = XFRAME (w->frame);
2646 it->cmp_it.id = -1;
2648 /* Extra space between lines (on window systems only). */
2649 if (base_face_id == DEFAULT_FACE_ID
2650 && FRAME_WINDOW_P (it->f))
2652 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2653 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2654 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2655 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2656 * FRAME_LINE_HEIGHT (it->f));
2657 else if (it->f->extra_line_spacing > 0)
2658 it->extra_line_spacing = it->f->extra_line_spacing;
2659 it->max_extra_line_spacing = 0;
2662 /* If realized faces have been removed, e.g. because of face
2663 attribute changes of named faces, recompute them. When running
2664 in batch mode, the face cache of the initial frame is null. If
2665 we happen to get called, make a dummy face cache. */
2666 if (FRAME_FACE_CACHE (it->f) == NULL)
2667 init_frame_faces (it->f);
2668 if (FRAME_FACE_CACHE (it->f)->used == 0)
2669 recompute_basic_faces (it->f);
2671 /* Current value of the `slice', `space-width', and 'height' properties. */
2672 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2673 it->space_width = Qnil;
2674 it->font_height = Qnil;
2675 it->override_ascent = -1;
2677 /* Are control characters displayed as `^C'? */
2678 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2680 /* -1 means everything between a CR and the following line end
2681 is invisible. >0 means lines indented more than this value are
2682 invisible. */
2683 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2684 ? (clip_to_bounds
2685 (-1, XINT (BVAR (current_buffer, selective_display)),
2686 PTRDIFF_MAX))
2687 : (!NILP (BVAR (current_buffer, selective_display))
2688 ? -1 : 0));
2689 it->selective_display_ellipsis_p
2690 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2692 /* Display table to use. */
2693 it->dp = window_display_table (w);
2695 /* Are multibyte characters enabled in current_buffer? */
2696 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2698 /* Non-zero if we should highlight the region. */
2699 highlight_region_p
2700 = (!NILP (Vtransient_mark_mode)
2701 && !NILP (BVAR (current_buffer, mark_active))
2702 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2704 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2705 start and end of a visible region in window IT->w. Set both to
2706 -1 to indicate no region. */
2707 if (highlight_region_p
2708 /* Maybe highlight only in selected window. */
2709 && (/* Either show region everywhere. */
2710 highlight_nonselected_windows
2711 /* Or show region in the selected window. */
2712 || w == XWINDOW (selected_window)
2713 /* Or show the region if we are in the mini-buffer and W is
2714 the window the mini-buffer refers to. */
2715 || (MINI_WINDOW_P (XWINDOW (selected_window))
2716 && WINDOWP (minibuf_selected_window)
2717 && w == XWINDOW (minibuf_selected_window))))
2719 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2720 it->region_beg_charpos = min (PT, markpos);
2721 it->region_end_charpos = max (PT, markpos);
2723 else
2724 it->region_beg_charpos = it->region_end_charpos = -1;
2726 /* Get the position at which the redisplay_end_trigger hook should
2727 be run, if it is to be run at all. */
2728 if (MARKERP (w->redisplay_end_trigger)
2729 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2730 it->redisplay_end_trigger_charpos
2731 = marker_position (w->redisplay_end_trigger);
2732 else if (INTEGERP (w->redisplay_end_trigger))
2733 it->redisplay_end_trigger_charpos =
2734 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2736 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2738 /* Are lines in the display truncated? */
2739 if (base_face_id != DEFAULT_FACE_ID
2740 || it->w->hscroll
2741 || (! WINDOW_FULL_WIDTH_P (it->w)
2742 && ((!NILP (Vtruncate_partial_width_windows)
2743 && !INTEGERP (Vtruncate_partial_width_windows))
2744 || (INTEGERP (Vtruncate_partial_width_windows)
2745 && (WINDOW_TOTAL_COLS (it->w)
2746 < XINT (Vtruncate_partial_width_windows))))))
2747 it->line_wrap = TRUNCATE;
2748 else if (NILP (BVAR (current_buffer, truncate_lines)))
2749 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2750 ? WINDOW_WRAP : WORD_WRAP;
2751 else
2752 it->line_wrap = TRUNCATE;
2754 /* Get dimensions of truncation and continuation glyphs. These are
2755 displayed as fringe bitmaps under X, but we need them for such
2756 frames when the fringes are turned off. But leave the dimensions
2757 zero for tooltip frames, as these glyphs look ugly there and also
2758 sabotage calculations of tooltip dimensions in x-show-tip. */
2759 #ifdef HAVE_WINDOW_SYSTEM
2760 if (!(FRAME_WINDOW_P (it->f)
2761 && FRAMEP (tip_frame)
2762 && it->f == XFRAME (tip_frame)))
2763 #endif
2765 if (it->line_wrap == TRUNCATE)
2767 /* We will need the truncation glyph. */
2768 eassert (it->glyph_row == NULL);
2769 produce_special_glyphs (it, IT_TRUNCATION);
2770 it->truncation_pixel_width = it->pixel_width;
2772 else
2774 /* We will need the continuation glyph. */
2775 eassert (it->glyph_row == NULL);
2776 produce_special_glyphs (it, IT_CONTINUATION);
2777 it->continuation_pixel_width = it->pixel_width;
2781 /* Reset these values to zero because the produce_special_glyphs
2782 above has changed them. */
2783 it->pixel_width = it->ascent = it->descent = 0;
2784 it->phys_ascent = it->phys_descent = 0;
2786 /* Set this after getting the dimensions of truncation and
2787 continuation glyphs, so that we don't produce glyphs when calling
2788 produce_special_glyphs, above. */
2789 it->glyph_row = row;
2790 it->area = TEXT_AREA;
2792 /* Forget any previous info about this row being reversed. */
2793 if (it->glyph_row)
2794 it->glyph_row->reversed_p = 0;
2796 /* Get the dimensions of the display area. The display area
2797 consists of the visible window area plus a horizontally scrolled
2798 part to the left of the window. All x-values are relative to the
2799 start of this total display area. */
2800 if (base_face_id != DEFAULT_FACE_ID)
2802 /* Mode lines, menu bar in terminal frames. */
2803 it->first_visible_x = 0;
2804 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2806 else
2808 it->first_visible_x =
2809 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2810 it->last_visible_x = (it->first_visible_x
2811 + window_box_width (w, TEXT_AREA));
2813 /* If we truncate lines, leave room for the truncation glyph(s) at
2814 the right margin. Otherwise, leave room for the continuation
2815 glyph(s). Done only if the window has no fringes. Since we
2816 don't know at this point whether there will be any R2L lines in
2817 the window, we reserve space for truncation/continuation glyphs
2818 even if only one of the fringes is absent. */
2819 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2820 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2822 if (it->line_wrap == TRUNCATE)
2823 it->last_visible_x -= it->truncation_pixel_width;
2824 else
2825 it->last_visible_x -= it->continuation_pixel_width;
2828 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2829 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2832 /* Leave room for a border glyph. */
2833 if (!FRAME_WINDOW_P (it->f)
2834 && !WINDOW_RIGHTMOST_P (it->w))
2835 it->last_visible_x -= 1;
2837 it->last_visible_y = window_text_bottom_y (w);
2839 /* For mode lines and alike, arrange for the first glyph having a
2840 left box line if the face specifies a box. */
2841 if (base_face_id != DEFAULT_FACE_ID)
2843 struct face *face;
2845 it->face_id = remapped_base_face_id;
2847 /* If we have a boxed mode line, make the first character appear
2848 with a left box line. */
2849 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2850 if (face->box != FACE_NO_BOX)
2851 it->start_of_box_run_p = 1;
2854 /* If a buffer position was specified, set the iterator there,
2855 getting overlays and face properties from that position. */
2856 if (charpos >= BUF_BEG (current_buffer))
2858 it->end_charpos = ZV;
2859 IT_CHARPOS (*it) = charpos;
2861 /* We will rely on `reseat' to set this up properly, via
2862 handle_face_prop. */
2863 it->face_id = it->base_face_id;
2865 /* Compute byte position if not specified. */
2866 if (bytepos < charpos)
2867 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2868 else
2869 IT_BYTEPOS (*it) = bytepos;
2871 it->start = it->current;
2872 /* Do we need to reorder bidirectional text? Not if this is a
2873 unibyte buffer: by definition, none of the single-byte
2874 characters are strong R2L, so no reordering is needed. And
2875 bidi.c doesn't support unibyte buffers anyway. Also, don't
2876 reorder while we are loading loadup.el, since the tables of
2877 character properties needed for reordering are not yet
2878 available. */
2879 it->bidi_p =
2880 NILP (Vpurify_flag)
2881 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2882 && it->multibyte_p;
2884 /* If we are to reorder bidirectional text, init the bidi
2885 iterator. */
2886 if (it->bidi_p)
2888 /* Note the paragraph direction that this buffer wants to
2889 use. */
2890 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2891 Qleft_to_right))
2892 it->paragraph_embedding = L2R;
2893 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2894 Qright_to_left))
2895 it->paragraph_embedding = R2L;
2896 else
2897 it->paragraph_embedding = NEUTRAL_DIR;
2898 bidi_unshelve_cache (NULL, 0);
2899 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2900 &it->bidi_it);
2903 /* Compute faces etc. */
2904 reseat (it, it->current.pos, 1);
2907 CHECK_IT (it);
2911 /* Initialize IT for the display of window W with window start POS. */
2913 void
2914 start_display (struct it *it, struct window *w, struct text_pos pos)
2916 struct glyph_row *row;
2917 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2919 row = w->desired_matrix->rows + first_vpos;
2920 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2921 it->first_vpos = first_vpos;
2923 /* Don't reseat to previous visible line start if current start
2924 position is in a string or image. */
2925 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2927 int start_at_line_beg_p;
2928 int first_y = it->current_y;
2930 /* If window start is not at a line start, skip forward to POS to
2931 get the correct continuation lines width. */
2932 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2933 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2934 if (!start_at_line_beg_p)
2936 int new_x;
2938 reseat_at_previous_visible_line_start (it);
2939 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2941 new_x = it->current_x + it->pixel_width;
2943 /* If lines are continued, this line may end in the middle
2944 of a multi-glyph character (e.g. a control character
2945 displayed as \003, or in the middle of an overlay
2946 string). In this case move_it_to above will not have
2947 taken us to the start of the continuation line but to the
2948 end of the continued line. */
2949 if (it->current_x > 0
2950 && it->line_wrap != TRUNCATE /* Lines are continued. */
2951 && (/* And glyph doesn't fit on the line. */
2952 new_x > it->last_visible_x
2953 /* Or it fits exactly and we're on a window
2954 system frame. */
2955 || (new_x == it->last_visible_x
2956 && FRAME_WINDOW_P (it->f)
2957 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2958 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2959 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2961 if ((it->current.dpvec_index >= 0
2962 || it->current.overlay_string_index >= 0)
2963 /* If we are on a newline from a display vector or
2964 overlay string, then we are already at the end of
2965 a screen line; no need to go to the next line in
2966 that case, as this line is not really continued.
2967 (If we do go to the next line, C-e will not DTRT.) */
2968 && it->c != '\n')
2970 set_iterator_to_next (it, 1);
2971 move_it_in_display_line_to (it, -1, -1, 0);
2974 it->continuation_lines_width += it->current_x;
2976 /* If the character at POS is displayed via a display
2977 vector, move_it_to above stops at the final glyph of
2978 IT->dpvec. To make the caller redisplay that character
2979 again (a.k.a. start at POS), we need to reset the
2980 dpvec_index to the beginning of IT->dpvec. */
2981 else if (it->current.dpvec_index >= 0)
2982 it->current.dpvec_index = 0;
2984 /* We're starting a new display line, not affected by the
2985 height of the continued line, so clear the appropriate
2986 fields in the iterator structure. */
2987 it->max_ascent = it->max_descent = 0;
2988 it->max_phys_ascent = it->max_phys_descent = 0;
2990 it->current_y = first_y;
2991 it->vpos = 0;
2992 it->current_x = it->hpos = 0;
2998 /* Return 1 if POS is a position in ellipses displayed for invisible
2999 text. W is the window we display, for text property lookup. */
3001 static int
3002 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3004 Lisp_Object prop, window;
3005 int ellipses_p = 0;
3006 ptrdiff_t charpos = CHARPOS (pos->pos);
3008 /* If POS specifies a position in a display vector, this might
3009 be for an ellipsis displayed for invisible text. We won't
3010 get the iterator set up for delivering that ellipsis unless
3011 we make sure that it gets aware of the invisible text. */
3012 if (pos->dpvec_index >= 0
3013 && pos->overlay_string_index < 0
3014 && CHARPOS (pos->string_pos) < 0
3015 && charpos > BEGV
3016 && (XSETWINDOW (window, w),
3017 prop = Fget_char_property (make_number (charpos),
3018 Qinvisible, window),
3019 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3021 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3022 window);
3023 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3026 return ellipses_p;
3030 /* Initialize IT for stepping through current_buffer in window W,
3031 starting at position POS that includes overlay string and display
3032 vector/ control character translation position information. Value
3033 is zero if there are overlay strings with newlines at POS. */
3035 static int
3036 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3038 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3039 int i, overlay_strings_with_newlines = 0;
3041 /* If POS specifies a position in a display vector, this might
3042 be for an ellipsis displayed for invisible text. We won't
3043 get the iterator set up for delivering that ellipsis unless
3044 we make sure that it gets aware of the invisible text. */
3045 if (in_ellipses_for_invisible_text_p (pos, w))
3047 --charpos;
3048 bytepos = 0;
3051 /* Keep in mind: the call to reseat in init_iterator skips invisible
3052 text, so we might end up at a position different from POS. This
3053 is only a problem when POS is a row start after a newline and an
3054 overlay starts there with an after-string, and the overlay has an
3055 invisible property. Since we don't skip invisible text in
3056 display_line and elsewhere immediately after consuming the
3057 newline before the row start, such a POS will not be in a string,
3058 but the call to init_iterator below will move us to the
3059 after-string. */
3060 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3062 /* This only scans the current chunk -- it should scan all chunks.
3063 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3064 to 16 in 22.1 to make this a lesser problem. */
3065 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3067 const char *s = SSDATA (it->overlay_strings[i]);
3068 const char *e = s + SBYTES (it->overlay_strings[i]);
3070 while (s < e && *s != '\n')
3071 ++s;
3073 if (s < e)
3075 overlay_strings_with_newlines = 1;
3076 break;
3080 /* If position is within an overlay string, set up IT to the right
3081 overlay string. */
3082 if (pos->overlay_string_index >= 0)
3084 int relative_index;
3086 /* If the first overlay string happens to have a `display'
3087 property for an image, the iterator will be set up for that
3088 image, and we have to undo that setup first before we can
3089 correct the overlay string index. */
3090 if (it->method == GET_FROM_IMAGE)
3091 pop_it (it);
3093 /* We already have the first chunk of overlay strings in
3094 IT->overlay_strings. Load more until the one for
3095 pos->overlay_string_index is in IT->overlay_strings. */
3096 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3098 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3099 it->current.overlay_string_index = 0;
3100 while (n--)
3102 load_overlay_strings (it, 0);
3103 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3107 it->current.overlay_string_index = pos->overlay_string_index;
3108 relative_index = (it->current.overlay_string_index
3109 % OVERLAY_STRING_CHUNK_SIZE);
3110 it->string = it->overlay_strings[relative_index];
3111 eassert (STRINGP (it->string));
3112 it->current.string_pos = pos->string_pos;
3113 it->method = GET_FROM_STRING;
3116 if (CHARPOS (pos->string_pos) >= 0)
3118 /* Recorded position is not in an overlay string, but in another
3119 string. This can only be a string from a `display' property.
3120 IT should already be filled with that string. */
3121 it->current.string_pos = pos->string_pos;
3122 eassert (STRINGP (it->string));
3125 /* Restore position in display vector translations, control
3126 character translations or ellipses. */
3127 if (pos->dpvec_index >= 0)
3129 if (it->dpvec == NULL)
3130 get_next_display_element (it);
3131 eassert (it->dpvec && it->current.dpvec_index == 0);
3132 it->current.dpvec_index = pos->dpvec_index;
3135 CHECK_IT (it);
3136 return !overlay_strings_with_newlines;
3140 /* Initialize IT for stepping through current_buffer in window W
3141 starting at ROW->start. */
3143 static void
3144 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3146 init_from_display_pos (it, w, &row->start);
3147 it->start = row->start;
3148 it->continuation_lines_width = row->continuation_lines_width;
3149 CHECK_IT (it);
3153 /* Initialize IT for stepping through current_buffer in window W
3154 starting in the line following ROW, i.e. starting at ROW->end.
3155 Value is zero if there are overlay strings with newlines at ROW's
3156 end position. */
3158 static int
3159 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3161 int success = 0;
3163 if (init_from_display_pos (it, w, &row->end))
3165 if (row->continued_p)
3166 it->continuation_lines_width
3167 = row->continuation_lines_width + row->pixel_width;
3168 CHECK_IT (it);
3169 success = 1;
3172 return success;
3178 /***********************************************************************
3179 Text properties
3180 ***********************************************************************/
3182 /* Called when IT reaches IT->stop_charpos. Handle text property and
3183 overlay changes. Set IT->stop_charpos to the next position where
3184 to stop. */
3186 static void
3187 handle_stop (struct it *it)
3189 enum prop_handled handled;
3190 int handle_overlay_change_p;
3191 struct props *p;
3193 it->dpvec = NULL;
3194 it->current.dpvec_index = -1;
3195 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3196 it->ignore_overlay_strings_at_pos_p = 0;
3197 it->ellipsis_p = 0;
3199 /* Use face of preceding text for ellipsis (if invisible) */
3200 if (it->selective_display_ellipsis_p)
3201 it->saved_face_id = it->face_id;
3205 handled = HANDLED_NORMALLY;
3207 /* Call text property handlers. */
3208 for (p = it_props; p->handler; ++p)
3210 handled = p->handler (it);
3212 if (handled == HANDLED_RECOMPUTE_PROPS)
3213 break;
3214 else if (handled == HANDLED_RETURN)
3216 /* We still want to show before and after strings from
3217 overlays even if the actual buffer text is replaced. */
3218 if (!handle_overlay_change_p
3219 || it->sp > 1
3220 /* Don't call get_overlay_strings_1 if we already
3221 have overlay strings loaded, because doing so
3222 will load them again and push the iterator state
3223 onto the stack one more time, which is not
3224 expected by the rest of the code that processes
3225 overlay strings. */
3226 || (it->current.overlay_string_index < 0
3227 ? !get_overlay_strings_1 (it, 0, 0)
3228 : 0))
3230 if (it->ellipsis_p)
3231 setup_for_ellipsis (it, 0);
3232 /* When handling a display spec, we might load an
3233 empty string. In that case, discard it here. We
3234 used to discard it in handle_single_display_spec,
3235 but that causes get_overlay_strings_1, above, to
3236 ignore overlay strings that we must check. */
3237 if (STRINGP (it->string) && !SCHARS (it->string))
3238 pop_it (it);
3239 return;
3241 else if (STRINGP (it->string) && !SCHARS (it->string))
3242 pop_it (it);
3243 else
3245 it->ignore_overlay_strings_at_pos_p = 1;
3246 it->string_from_display_prop_p = 0;
3247 it->from_disp_prop_p = 0;
3248 handle_overlay_change_p = 0;
3250 handled = HANDLED_RECOMPUTE_PROPS;
3251 break;
3253 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3254 handle_overlay_change_p = 0;
3257 if (handled != HANDLED_RECOMPUTE_PROPS)
3259 /* Don't check for overlay strings below when set to deliver
3260 characters from a display vector. */
3261 if (it->method == GET_FROM_DISPLAY_VECTOR)
3262 handle_overlay_change_p = 0;
3264 /* Handle overlay changes.
3265 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3266 if it finds overlays. */
3267 if (handle_overlay_change_p)
3268 handled = handle_overlay_change (it);
3271 if (it->ellipsis_p)
3273 setup_for_ellipsis (it, 0);
3274 break;
3277 while (handled == HANDLED_RECOMPUTE_PROPS);
3279 /* Determine where to stop next. */
3280 if (handled == HANDLED_NORMALLY)
3281 compute_stop_pos (it);
3285 /* Compute IT->stop_charpos from text property and overlay change
3286 information for IT's current position. */
3288 static void
3289 compute_stop_pos (struct it *it)
3291 register INTERVAL iv, next_iv;
3292 Lisp_Object object, limit, position;
3293 ptrdiff_t charpos, bytepos;
3295 if (STRINGP (it->string))
3297 /* Strings are usually short, so don't limit the search for
3298 properties. */
3299 it->stop_charpos = it->end_charpos;
3300 object = it->string;
3301 limit = Qnil;
3302 charpos = IT_STRING_CHARPOS (*it);
3303 bytepos = IT_STRING_BYTEPOS (*it);
3305 else
3307 ptrdiff_t pos;
3309 /* If end_charpos is out of range for some reason, such as a
3310 misbehaving display function, rationalize it (Bug#5984). */
3311 if (it->end_charpos > ZV)
3312 it->end_charpos = ZV;
3313 it->stop_charpos = it->end_charpos;
3315 /* If next overlay change is in front of the current stop pos
3316 (which is IT->end_charpos), stop there. Note: value of
3317 next_overlay_change is point-max if no overlay change
3318 follows. */
3319 charpos = IT_CHARPOS (*it);
3320 bytepos = IT_BYTEPOS (*it);
3321 pos = next_overlay_change (charpos);
3322 if (pos < it->stop_charpos)
3323 it->stop_charpos = pos;
3325 /* If showing the region, we have to stop at the region
3326 start or end because the face might change there. */
3327 if (it->region_beg_charpos > 0)
3329 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3330 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3331 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3332 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3335 /* Set up variables for computing the stop position from text
3336 property changes. */
3337 XSETBUFFER (object, current_buffer);
3338 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3341 /* Get the interval containing IT's position. Value is a null
3342 interval if there isn't such an interval. */
3343 position = make_number (charpos);
3344 iv = validate_interval_range (object, &position, &position, 0);
3345 if (iv)
3347 Lisp_Object values_here[LAST_PROP_IDX];
3348 struct props *p;
3350 /* Get properties here. */
3351 for (p = it_props; p->handler; ++p)
3352 values_here[p->idx] = textget (iv->plist, *p->name);
3354 /* Look for an interval following iv that has different
3355 properties. */
3356 for (next_iv = next_interval (iv);
3357 (next_iv
3358 && (NILP (limit)
3359 || XFASTINT (limit) > next_iv->position));
3360 next_iv = next_interval (next_iv))
3362 for (p = it_props; p->handler; ++p)
3364 Lisp_Object new_value;
3366 new_value = textget (next_iv->plist, *p->name);
3367 if (!EQ (values_here[p->idx], new_value))
3368 break;
3371 if (p->handler)
3372 break;
3375 if (next_iv)
3377 if (INTEGERP (limit)
3378 && next_iv->position >= XFASTINT (limit))
3379 /* No text property change up to limit. */
3380 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3381 else
3382 /* Text properties change in next_iv. */
3383 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3387 if (it->cmp_it.id < 0)
3389 ptrdiff_t stoppos = it->end_charpos;
3391 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3392 stoppos = -1;
3393 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3394 stoppos, it->string);
3397 eassert (STRINGP (it->string)
3398 || (it->stop_charpos >= BEGV
3399 && it->stop_charpos >= IT_CHARPOS (*it)));
3403 /* Return the position of the next overlay change after POS in
3404 current_buffer. Value is point-max if no overlay change
3405 follows. This is like `next-overlay-change' but doesn't use
3406 xmalloc. */
3408 static ptrdiff_t
3409 next_overlay_change (ptrdiff_t pos)
3411 ptrdiff_t i, noverlays;
3412 ptrdiff_t endpos;
3413 Lisp_Object *overlays;
3415 /* Get all overlays at the given position. */
3416 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3418 /* If any of these overlays ends before endpos,
3419 use its ending point instead. */
3420 for (i = 0; i < noverlays; ++i)
3422 Lisp_Object oend;
3423 ptrdiff_t oendpos;
3425 oend = OVERLAY_END (overlays[i]);
3426 oendpos = OVERLAY_POSITION (oend);
3427 endpos = min (endpos, oendpos);
3430 return endpos;
3433 /* How many characters forward to search for a display property or
3434 display string. Searching too far forward makes the bidi display
3435 sluggish, especially in small windows. */
3436 #define MAX_DISP_SCAN 250
3438 /* Return the character position of a display string at or after
3439 position specified by POSITION. If no display string exists at or
3440 after POSITION, return ZV. A display string is either an overlay
3441 with `display' property whose value is a string, or a `display'
3442 text property whose value is a string. STRING is data about the
3443 string to iterate; if STRING->lstring is nil, we are iterating a
3444 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3445 on a GUI frame. DISP_PROP is set to zero if we searched
3446 MAX_DISP_SCAN characters forward without finding any display
3447 strings, non-zero otherwise. It is set to 2 if the display string
3448 uses any kind of `(space ...)' spec that will produce a stretch of
3449 white space in the text area. */
3450 ptrdiff_t
3451 compute_display_string_pos (struct text_pos *position,
3452 struct bidi_string_data *string,
3453 int frame_window_p, int *disp_prop)
3455 /* OBJECT = nil means current buffer. */
3456 Lisp_Object object =
3457 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3458 Lisp_Object pos, spec, limpos;
3459 int string_p = (string && (STRINGP (string->lstring) || string->s));
3460 ptrdiff_t eob = string_p ? string->schars : ZV;
3461 ptrdiff_t begb = string_p ? 0 : BEGV;
3462 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3463 ptrdiff_t lim =
3464 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3465 struct text_pos tpos;
3466 int rv = 0;
3468 *disp_prop = 1;
3470 if (charpos >= eob
3471 /* We don't support display properties whose values are strings
3472 that have display string properties. */
3473 || string->from_disp_str
3474 /* C strings cannot have display properties. */
3475 || (string->s && !STRINGP (object)))
3477 *disp_prop = 0;
3478 return eob;
3481 /* If the character at CHARPOS is where the display string begins,
3482 return CHARPOS. */
3483 pos = make_number (charpos);
3484 if (STRINGP (object))
3485 bufpos = string->bufpos;
3486 else
3487 bufpos = charpos;
3488 tpos = *position;
3489 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3490 && (charpos <= begb
3491 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3492 object),
3493 spec))
3494 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3495 frame_window_p)))
3497 if (rv == 2)
3498 *disp_prop = 2;
3499 return charpos;
3502 /* Look forward for the first character with a `display' property
3503 that will replace the underlying text when displayed. */
3504 limpos = make_number (lim);
3505 do {
3506 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3507 CHARPOS (tpos) = XFASTINT (pos);
3508 if (CHARPOS (tpos) >= lim)
3510 *disp_prop = 0;
3511 break;
3513 if (STRINGP (object))
3514 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3515 else
3516 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3517 spec = Fget_char_property (pos, Qdisplay, object);
3518 if (!STRINGP (object))
3519 bufpos = CHARPOS (tpos);
3520 } while (NILP (spec)
3521 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3522 bufpos, frame_window_p)));
3523 if (rv == 2)
3524 *disp_prop = 2;
3526 return CHARPOS (tpos);
3529 /* Return the character position of the end of the display string that
3530 started at CHARPOS. If there's no display string at CHARPOS,
3531 return -1. A display string is either an overlay with `display'
3532 property whose value is a string or a `display' text property whose
3533 value is a string. */
3534 ptrdiff_t
3535 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3537 /* OBJECT = nil means current buffer. */
3538 Lisp_Object object =
3539 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3540 Lisp_Object pos = make_number (charpos);
3541 ptrdiff_t eob =
3542 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3544 if (charpos >= eob || (string->s && !STRINGP (object)))
3545 return eob;
3547 /* It could happen that the display property or overlay was removed
3548 since we found it in compute_display_string_pos above. One way
3549 this can happen is if JIT font-lock was called (through
3550 handle_fontified_prop), and jit-lock-functions remove text
3551 properties or overlays from the portion of buffer that includes
3552 CHARPOS. Muse mode is known to do that, for example. In this
3553 case, we return -1 to the caller, to signal that no display
3554 string is actually present at CHARPOS. See bidi_fetch_char for
3555 how this is handled.
3557 An alternative would be to never look for display properties past
3558 it->stop_charpos. But neither compute_display_string_pos nor
3559 bidi_fetch_char that calls it know or care where the next
3560 stop_charpos is. */
3561 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3562 return -1;
3564 /* Look forward for the first character where the `display' property
3565 changes. */
3566 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3568 return XFASTINT (pos);
3573 /***********************************************************************
3574 Fontification
3575 ***********************************************************************/
3577 /* Handle changes in the `fontified' property of the current buffer by
3578 calling hook functions from Qfontification_functions to fontify
3579 regions of text. */
3581 static enum prop_handled
3582 handle_fontified_prop (struct it *it)
3584 Lisp_Object prop, pos;
3585 enum prop_handled handled = HANDLED_NORMALLY;
3587 if (!NILP (Vmemory_full))
3588 return handled;
3590 /* Get the value of the `fontified' property at IT's current buffer
3591 position. (The `fontified' property doesn't have a special
3592 meaning in strings.) If the value is nil, call functions from
3593 Qfontification_functions. */
3594 if (!STRINGP (it->string)
3595 && it->s == NULL
3596 && !NILP (Vfontification_functions)
3597 && !NILP (Vrun_hooks)
3598 && (pos = make_number (IT_CHARPOS (*it)),
3599 prop = Fget_char_property (pos, Qfontified, Qnil),
3600 /* Ignore the special cased nil value always present at EOB since
3601 no amount of fontifying will be able to change it. */
3602 NILP (prop) && IT_CHARPOS (*it) < Z))
3604 ptrdiff_t count = SPECPDL_INDEX ();
3605 Lisp_Object val;
3606 struct buffer *obuf = current_buffer;
3607 int begv = BEGV, zv = ZV;
3608 int old_clip_changed = current_buffer->clip_changed;
3610 val = Vfontification_functions;
3611 specbind (Qfontification_functions, Qnil);
3613 eassert (it->end_charpos == ZV);
3615 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3616 safe_call1 (val, pos);
3617 else
3619 Lisp_Object fns, fn;
3620 struct gcpro gcpro1, gcpro2;
3622 fns = Qnil;
3623 GCPRO2 (val, fns);
3625 for (; CONSP (val); val = XCDR (val))
3627 fn = XCAR (val);
3629 if (EQ (fn, Qt))
3631 /* A value of t indicates this hook has a local
3632 binding; it means to run the global binding too.
3633 In a global value, t should not occur. If it
3634 does, we must ignore it to avoid an endless
3635 loop. */
3636 for (fns = Fdefault_value (Qfontification_functions);
3637 CONSP (fns);
3638 fns = XCDR (fns))
3640 fn = XCAR (fns);
3641 if (!EQ (fn, Qt))
3642 safe_call1 (fn, pos);
3645 else
3646 safe_call1 (fn, pos);
3649 UNGCPRO;
3652 unbind_to (count, Qnil);
3654 /* Fontification functions routinely call `save-restriction'.
3655 Normally, this tags clip_changed, which can confuse redisplay
3656 (see discussion in Bug#6671). Since we don't perform any
3657 special handling of fontification changes in the case where
3658 `save-restriction' isn't called, there's no point doing so in
3659 this case either. So, if the buffer's restrictions are
3660 actually left unchanged, reset clip_changed. */
3661 if (obuf == current_buffer)
3663 if (begv == BEGV && zv == ZV)
3664 current_buffer->clip_changed = old_clip_changed;
3666 /* There isn't much we can reasonably do to protect against
3667 misbehaving fontification, but here's a fig leaf. */
3668 else if (BUFFER_LIVE_P (obuf))
3669 set_buffer_internal_1 (obuf);
3671 /* The fontification code may have added/removed text.
3672 It could do even a lot worse, but let's at least protect against
3673 the most obvious case where only the text past `pos' gets changed',
3674 as is/was done in grep.el where some escapes sequences are turned
3675 into face properties (bug#7876). */
3676 it->end_charpos = ZV;
3678 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3679 something. This avoids an endless loop if they failed to
3680 fontify the text for which reason ever. */
3681 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3682 handled = HANDLED_RECOMPUTE_PROPS;
3685 return handled;
3690 /***********************************************************************
3691 Faces
3692 ***********************************************************************/
3694 /* Set up iterator IT from face properties at its current position.
3695 Called from handle_stop. */
3697 static enum prop_handled
3698 handle_face_prop (struct it *it)
3700 int new_face_id;
3701 ptrdiff_t next_stop;
3703 if (!STRINGP (it->string))
3705 new_face_id
3706 = face_at_buffer_position (it->w,
3707 IT_CHARPOS (*it),
3708 it->region_beg_charpos,
3709 it->region_end_charpos,
3710 &next_stop,
3711 (IT_CHARPOS (*it)
3712 + TEXT_PROP_DISTANCE_LIMIT),
3713 0, it->base_face_id);
3715 /* Is this a start of a run of characters with box face?
3716 Caveat: this can be called for a freshly initialized
3717 iterator; face_id is -1 in this case. We know that the new
3718 face will not change until limit, i.e. if the new face has a
3719 box, all characters up to limit will have one. But, as
3720 usual, we don't know whether limit is really the end. */
3721 if (new_face_id != it->face_id)
3723 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3725 /* If new face has a box but old face has not, this is
3726 the start of a run of characters with box, i.e. it has
3727 a shadow on the left side. The value of face_id of the
3728 iterator will be -1 if this is the initial call that gets
3729 the face. In this case, we have to look in front of IT's
3730 position and see whether there is a face != new_face_id. */
3731 it->start_of_box_run_p
3732 = (new_face->box != FACE_NO_BOX
3733 && (it->face_id >= 0
3734 || IT_CHARPOS (*it) == BEG
3735 || new_face_id != face_before_it_pos (it)));
3736 it->face_box_p = new_face->box != FACE_NO_BOX;
3739 else
3741 int base_face_id;
3742 ptrdiff_t bufpos;
3743 int i;
3744 Lisp_Object from_overlay
3745 = (it->current.overlay_string_index >= 0
3746 ? it->string_overlays[it->current.overlay_string_index
3747 % OVERLAY_STRING_CHUNK_SIZE]
3748 : Qnil);
3750 /* See if we got to this string directly or indirectly from
3751 an overlay property. That includes the before-string or
3752 after-string of an overlay, strings in display properties
3753 provided by an overlay, their text properties, etc.
3755 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3756 if (! NILP (from_overlay))
3757 for (i = it->sp - 1; i >= 0; i--)
3759 if (it->stack[i].current.overlay_string_index >= 0)
3760 from_overlay
3761 = it->string_overlays[it->stack[i].current.overlay_string_index
3762 % OVERLAY_STRING_CHUNK_SIZE];
3763 else if (! NILP (it->stack[i].from_overlay))
3764 from_overlay = it->stack[i].from_overlay;
3766 if (!NILP (from_overlay))
3767 break;
3770 if (! NILP (from_overlay))
3772 bufpos = IT_CHARPOS (*it);
3773 /* For a string from an overlay, the base face depends
3774 only on text properties and ignores overlays. */
3775 base_face_id
3776 = face_for_overlay_string (it->w,
3777 IT_CHARPOS (*it),
3778 it->region_beg_charpos,
3779 it->region_end_charpos,
3780 &next_stop,
3781 (IT_CHARPOS (*it)
3782 + TEXT_PROP_DISTANCE_LIMIT),
3784 from_overlay);
3786 else
3788 bufpos = 0;
3790 /* For strings from a `display' property, use the face at
3791 IT's current buffer position as the base face to merge
3792 with, so that overlay strings appear in the same face as
3793 surrounding text, unless they specify their own
3794 faces. */
3795 base_face_id = it->string_from_prefix_prop_p
3796 ? DEFAULT_FACE_ID
3797 : underlying_face_id (it);
3800 new_face_id = face_at_string_position (it->w,
3801 it->string,
3802 IT_STRING_CHARPOS (*it),
3803 bufpos,
3804 it->region_beg_charpos,
3805 it->region_end_charpos,
3806 &next_stop,
3807 base_face_id, 0);
3809 /* Is this a start of a run of characters with box? Caveat:
3810 this can be called for a freshly allocated iterator; face_id
3811 is -1 is this case. We know that the new face will not
3812 change until the next check pos, i.e. if the new face has a
3813 box, all characters up to that position will have a
3814 box. But, as usual, we don't know whether that position
3815 is really the end. */
3816 if (new_face_id != it->face_id)
3818 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3819 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3821 /* If new face has a box but old face hasn't, this is the
3822 start of a run of characters with box, i.e. it has a
3823 shadow on the left side. */
3824 it->start_of_box_run_p
3825 = new_face->box && (old_face == NULL || !old_face->box);
3826 it->face_box_p = new_face->box != FACE_NO_BOX;
3830 it->face_id = new_face_id;
3831 return HANDLED_NORMALLY;
3835 /* Return the ID of the face ``underlying'' IT's current position,
3836 which is in a string. If the iterator is associated with a
3837 buffer, return the face at IT's current buffer position.
3838 Otherwise, use the iterator's base_face_id. */
3840 static int
3841 underlying_face_id (struct it *it)
3843 int face_id = it->base_face_id, i;
3845 eassert (STRINGP (it->string));
3847 for (i = it->sp - 1; i >= 0; --i)
3848 if (NILP (it->stack[i].string))
3849 face_id = it->stack[i].face_id;
3851 return face_id;
3855 /* Compute the face one character before or after the current position
3856 of IT, in the visual order. BEFORE_P non-zero means get the face
3857 in front (to the left in L2R paragraphs, to the right in R2L
3858 paragraphs) of IT's screen position. Value is the ID of the face. */
3860 static int
3861 face_before_or_after_it_pos (struct it *it, int before_p)
3863 int face_id, limit;
3864 ptrdiff_t next_check_charpos;
3865 struct it it_copy;
3866 void *it_copy_data = NULL;
3868 eassert (it->s == NULL);
3870 if (STRINGP (it->string))
3872 ptrdiff_t bufpos, charpos;
3873 int base_face_id;
3875 /* No face change past the end of the string (for the case
3876 we are padding with spaces). No face change before the
3877 string start. */
3878 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3879 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3880 return it->face_id;
3882 if (!it->bidi_p)
3884 /* Set charpos to the position before or after IT's current
3885 position, in the logical order, which in the non-bidi
3886 case is the same as the visual order. */
3887 if (before_p)
3888 charpos = IT_STRING_CHARPOS (*it) - 1;
3889 else if (it->what == IT_COMPOSITION)
3890 /* For composition, we must check the character after the
3891 composition. */
3892 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3893 else
3894 charpos = IT_STRING_CHARPOS (*it) + 1;
3896 else
3898 if (before_p)
3900 /* With bidi iteration, the character before the current
3901 in the visual order cannot be found by simple
3902 iteration, because "reverse" reordering is not
3903 supported. Instead, we need to use the move_it_*
3904 family of functions. */
3905 /* Ignore face changes before the first visible
3906 character on this display line. */
3907 if (it->current_x <= it->first_visible_x)
3908 return it->face_id;
3909 SAVE_IT (it_copy, *it, it_copy_data);
3910 /* Implementation note: Since move_it_in_display_line
3911 works in the iterator geometry, and thinks the first
3912 character is always the leftmost, even in R2L lines,
3913 we don't need to distinguish between the R2L and L2R
3914 cases here. */
3915 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3916 it_copy.current_x - 1, MOVE_TO_X);
3917 charpos = IT_STRING_CHARPOS (it_copy);
3918 RESTORE_IT (it, it, it_copy_data);
3920 else
3922 /* Set charpos to the string position of the character
3923 that comes after IT's current position in the visual
3924 order. */
3925 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3927 it_copy = *it;
3928 while (n--)
3929 bidi_move_to_visually_next (&it_copy.bidi_it);
3931 charpos = it_copy.bidi_it.charpos;
3934 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3936 if (it->current.overlay_string_index >= 0)
3937 bufpos = IT_CHARPOS (*it);
3938 else
3939 bufpos = 0;
3941 base_face_id = underlying_face_id (it);
3943 /* Get the face for ASCII, or unibyte. */
3944 face_id = face_at_string_position (it->w,
3945 it->string,
3946 charpos,
3947 bufpos,
3948 it->region_beg_charpos,
3949 it->region_end_charpos,
3950 &next_check_charpos,
3951 base_face_id, 0);
3953 /* Correct the face for charsets different from ASCII. Do it
3954 for the multibyte case only. The face returned above is
3955 suitable for unibyte text if IT->string is unibyte. */
3956 if (STRING_MULTIBYTE (it->string))
3958 struct text_pos pos1 = string_pos (charpos, it->string);
3959 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3960 int c, len;
3961 struct face *face = FACE_FROM_ID (it->f, face_id);
3963 c = string_char_and_length (p, &len);
3964 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3967 else
3969 struct text_pos pos;
3971 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3972 || (IT_CHARPOS (*it) <= BEGV && before_p))
3973 return it->face_id;
3975 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3976 pos = it->current.pos;
3978 if (!it->bidi_p)
3980 if (before_p)
3981 DEC_TEXT_POS (pos, it->multibyte_p);
3982 else
3984 if (it->what == IT_COMPOSITION)
3986 /* For composition, we must check the position after
3987 the composition. */
3988 pos.charpos += it->cmp_it.nchars;
3989 pos.bytepos += it->len;
3991 else
3992 INC_TEXT_POS (pos, it->multibyte_p);
3995 else
3997 if (before_p)
3999 /* With bidi iteration, the character before the current
4000 in the visual order cannot be found by simple
4001 iteration, because "reverse" reordering is not
4002 supported. Instead, we need to use the move_it_*
4003 family of functions. */
4004 /* Ignore face changes before the first visible
4005 character on this display line. */
4006 if (it->current_x <= it->first_visible_x)
4007 return it->face_id;
4008 SAVE_IT (it_copy, *it, it_copy_data);
4009 /* Implementation note: Since move_it_in_display_line
4010 works in the iterator geometry, and thinks the first
4011 character is always the leftmost, even in R2L lines,
4012 we don't need to distinguish between the R2L and L2R
4013 cases here. */
4014 move_it_in_display_line (&it_copy, ZV,
4015 it_copy.current_x - 1, MOVE_TO_X);
4016 pos = it_copy.current.pos;
4017 RESTORE_IT (it, it, it_copy_data);
4019 else
4021 /* Set charpos to the buffer position of the character
4022 that comes after IT's current position in the visual
4023 order. */
4024 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4026 it_copy = *it;
4027 while (n--)
4028 bidi_move_to_visually_next (&it_copy.bidi_it);
4030 SET_TEXT_POS (pos,
4031 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4034 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4036 /* Determine face for CHARSET_ASCII, or unibyte. */
4037 face_id = face_at_buffer_position (it->w,
4038 CHARPOS (pos),
4039 it->region_beg_charpos,
4040 it->region_end_charpos,
4041 &next_check_charpos,
4042 limit, 0, -1);
4044 /* Correct the face for charsets different from ASCII. Do it
4045 for the multibyte case only. The face returned above is
4046 suitable for unibyte text if current_buffer is unibyte. */
4047 if (it->multibyte_p)
4049 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4050 struct face *face = FACE_FROM_ID (it->f, face_id);
4051 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4055 return face_id;
4060 /***********************************************************************
4061 Invisible text
4062 ***********************************************************************/
4064 /* Set up iterator IT from invisible properties at its current
4065 position. Called from handle_stop. */
4067 static enum prop_handled
4068 handle_invisible_prop (struct it *it)
4070 enum prop_handled handled = HANDLED_NORMALLY;
4071 int invis_p;
4072 Lisp_Object prop;
4074 if (STRINGP (it->string))
4076 Lisp_Object end_charpos, limit, charpos;
4078 /* Get the value of the invisible text property at the
4079 current position. Value will be nil if there is no such
4080 property. */
4081 charpos = make_number (IT_STRING_CHARPOS (*it));
4082 prop = Fget_text_property (charpos, Qinvisible, it->string);
4083 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4085 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4087 /* Record whether we have to display an ellipsis for the
4088 invisible text. */
4089 int display_ellipsis_p = (invis_p == 2);
4090 ptrdiff_t len, endpos;
4092 handled = HANDLED_RECOMPUTE_PROPS;
4094 /* Get the position at which the next visible text can be
4095 found in IT->string, if any. */
4096 endpos = len = SCHARS (it->string);
4097 XSETINT (limit, len);
4100 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4101 it->string, limit);
4102 if (INTEGERP (end_charpos))
4104 endpos = XFASTINT (end_charpos);
4105 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4106 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4107 if (invis_p == 2)
4108 display_ellipsis_p = 1;
4111 while (invis_p && endpos < len);
4113 if (display_ellipsis_p)
4114 it->ellipsis_p = 1;
4116 if (endpos < len)
4118 /* Text at END_CHARPOS is visible. Move IT there. */
4119 struct text_pos old;
4120 ptrdiff_t oldpos;
4122 old = it->current.string_pos;
4123 oldpos = CHARPOS (old);
4124 if (it->bidi_p)
4126 if (it->bidi_it.first_elt
4127 && it->bidi_it.charpos < SCHARS (it->string))
4128 bidi_paragraph_init (it->paragraph_embedding,
4129 &it->bidi_it, 1);
4130 /* Bidi-iterate out of the invisible text. */
4133 bidi_move_to_visually_next (&it->bidi_it);
4135 while (oldpos <= it->bidi_it.charpos
4136 && it->bidi_it.charpos < endpos);
4138 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4139 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4140 if (IT_CHARPOS (*it) >= endpos)
4141 it->prev_stop = endpos;
4143 else
4145 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4146 compute_string_pos (&it->current.string_pos, old, it->string);
4149 else
4151 /* The rest of the string is invisible. If this is an
4152 overlay string, proceed with the next overlay string
4153 or whatever comes and return a character from there. */
4154 if (it->current.overlay_string_index >= 0
4155 && !display_ellipsis_p)
4157 next_overlay_string (it);
4158 /* Don't check for overlay strings when we just
4159 finished processing them. */
4160 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4162 else
4164 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4165 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4170 else
4172 ptrdiff_t newpos, next_stop, start_charpos, tem;
4173 Lisp_Object pos, overlay;
4175 /* First of all, is there invisible text at this position? */
4176 tem = start_charpos = IT_CHARPOS (*it);
4177 pos = make_number (tem);
4178 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4179 &overlay);
4180 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4182 /* If we are on invisible text, skip over it. */
4183 if (invis_p && start_charpos < it->end_charpos)
4185 /* Record whether we have to display an ellipsis for the
4186 invisible text. */
4187 int display_ellipsis_p = invis_p == 2;
4189 handled = HANDLED_RECOMPUTE_PROPS;
4191 /* Loop skipping over invisible text. The loop is left at
4192 ZV or with IT on the first char being visible again. */
4195 /* Try to skip some invisible text. Return value is the
4196 position reached which can be equal to where we start
4197 if there is nothing invisible there. This skips both
4198 over invisible text properties and overlays with
4199 invisible property. */
4200 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4202 /* If we skipped nothing at all we weren't at invisible
4203 text in the first place. If everything to the end of
4204 the buffer was skipped, end the loop. */
4205 if (newpos == tem || newpos >= ZV)
4206 invis_p = 0;
4207 else
4209 /* We skipped some characters but not necessarily
4210 all there are. Check if we ended up on visible
4211 text. Fget_char_property returns the property of
4212 the char before the given position, i.e. if we
4213 get invis_p = 0, this means that the char at
4214 newpos is visible. */
4215 pos = make_number (newpos);
4216 prop = Fget_char_property (pos, Qinvisible, it->window);
4217 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4220 /* If we ended up on invisible text, proceed to
4221 skip starting with next_stop. */
4222 if (invis_p)
4223 tem = next_stop;
4225 /* If there are adjacent invisible texts, don't lose the
4226 second one's ellipsis. */
4227 if (invis_p == 2)
4228 display_ellipsis_p = 1;
4230 while (invis_p);
4232 /* The position newpos is now either ZV or on visible text. */
4233 if (it->bidi_p)
4235 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4236 int on_newline =
4237 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4238 int after_newline =
4239 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4241 /* If the invisible text ends on a newline or on a
4242 character after a newline, we can avoid the costly,
4243 character by character, bidi iteration to NEWPOS, and
4244 instead simply reseat the iterator there. That's
4245 because all bidi reordering information is tossed at
4246 the newline. This is a big win for modes that hide
4247 complete lines, like Outline, Org, etc. */
4248 if (on_newline || after_newline)
4250 struct text_pos tpos;
4251 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4253 SET_TEXT_POS (tpos, newpos, bpos);
4254 reseat_1 (it, tpos, 0);
4255 /* If we reseat on a newline/ZV, we need to prep the
4256 bidi iterator for advancing to the next character
4257 after the newline/EOB, keeping the current paragraph
4258 direction (so that PRODUCE_GLYPHS does TRT wrt
4259 prepending/appending glyphs to a glyph row). */
4260 if (on_newline)
4262 it->bidi_it.first_elt = 0;
4263 it->bidi_it.paragraph_dir = pdir;
4264 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4265 it->bidi_it.nchars = 1;
4266 it->bidi_it.ch_len = 1;
4269 else /* Must use the slow method. */
4271 /* With bidi iteration, the region of invisible text
4272 could start and/or end in the middle of a
4273 non-base embedding level. Therefore, we need to
4274 skip invisible text using the bidi iterator,
4275 starting at IT's current position, until we find
4276 ourselves outside of the invisible text.
4277 Skipping invisible text _after_ bidi iteration
4278 avoids affecting the visual order of the
4279 displayed text when invisible properties are
4280 added or removed. */
4281 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4283 /* If we were `reseat'ed to a new paragraph,
4284 determine the paragraph base direction. We
4285 need to do it now because
4286 next_element_from_buffer may not have a
4287 chance to do it, if we are going to skip any
4288 text at the beginning, which resets the
4289 FIRST_ELT flag. */
4290 bidi_paragraph_init (it->paragraph_embedding,
4291 &it->bidi_it, 1);
4295 bidi_move_to_visually_next (&it->bidi_it);
4297 while (it->stop_charpos <= it->bidi_it.charpos
4298 && it->bidi_it.charpos < newpos);
4299 IT_CHARPOS (*it) = it->bidi_it.charpos;
4300 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4301 /* If we overstepped NEWPOS, record its position in
4302 the iterator, so that we skip invisible text if
4303 later the bidi iteration lands us in the
4304 invisible region again. */
4305 if (IT_CHARPOS (*it) >= newpos)
4306 it->prev_stop = newpos;
4309 else
4311 IT_CHARPOS (*it) = newpos;
4312 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4315 /* If there are before-strings at the start of invisible
4316 text, and the text is invisible because of a text
4317 property, arrange to show before-strings because 20.x did
4318 it that way. (If the text is invisible because of an
4319 overlay property instead of a text property, this is
4320 already handled in the overlay code.) */
4321 if (NILP (overlay)
4322 && get_overlay_strings (it, it->stop_charpos))
4324 handled = HANDLED_RECOMPUTE_PROPS;
4325 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4327 else if (display_ellipsis_p)
4329 /* Make sure that the glyphs of the ellipsis will get
4330 correct `charpos' values. If we would not update
4331 it->position here, the glyphs would belong to the
4332 last visible character _before_ the invisible
4333 text, which confuses `set_cursor_from_row'.
4335 We use the last invisible position instead of the
4336 first because this way the cursor is always drawn on
4337 the first "." of the ellipsis, whenever PT is inside
4338 the invisible text. Otherwise the cursor would be
4339 placed _after_ the ellipsis when the point is after the
4340 first invisible character. */
4341 if (!STRINGP (it->object))
4343 it->position.charpos = newpos - 1;
4344 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4346 it->ellipsis_p = 1;
4347 /* Let the ellipsis display before
4348 considering any properties of the following char.
4349 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4350 handled = HANDLED_RETURN;
4355 return handled;
4359 /* Make iterator IT return `...' next.
4360 Replaces LEN characters from buffer. */
4362 static void
4363 setup_for_ellipsis (struct it *it, int len)
4365 /* Use the display table definition for `...'. Invalid glyphs
4366 will be handled by the method returning elements from dpvec. */
4367 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4369 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4370 it->dpvec = v->contents;
4371 it->dpend = v->contents + v->header.size;
4373 else
4375 /* Default `...'. */
4376 it->dpvec = default_invis_vector;
4377 it->dpend = default_invis_vector + 3;
4380 it->dpvec_char_len = len;
4381 it->current.dpvec_index = 0;
4382 it->dpvec_face_id = -1;
4384 /* Remember the current face id in case glyphs specify faces.
4385 IT's face is restored in set_iterator_to_next.
4386 saved_face_id was set to preceding char's face in handle_stop. */
4387 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4388 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4390 it->method = GET_FROM_DISPLAY_VECTOR;
4391 it->ellipsis_p = 1;
4396 /***********************************************************************
4397 'display' property
4398 ***********************************************************************/
4400 /* Set up iterator IT from `display' property at its current position.
4401 Called from handle_stop.
4402 We return HANDLED_RETURN if some part of the display property
4403 overrides the display of the buffer text itself.
4404 Otherwise we return HANDLED_NORMALLY. */
4406 static enum prop_handled
4407 handle_display_prop (struct it *it)
4409 Lisp_Object propval, object, overlay;
4410 struct text_pos *position;
4411 ptrdiff_t bufpos;
4412 /* Nonzero if some property replaces the display of the text itself. */
4413 int display_replaced_p = 0;
4415 if (STRINGP (it->string))
4417 object = it->string;
4418 position = &it->current.string_pos;
4419 bufpos = CHARPOS (it->current.pos);
4421 else
4423 XSETWINDOW (object, it->w);
4424 position = &it->current.pos;
4425 bufpos = CHARPOS (*position);
4428 /* Reset those iterator values set from display property values. */
4429 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4430 it->space_width = Qnil;
4431 it->font_height = Qnil;
4432 it->voffset = 0;
4434 /* We don't support recursive `display' properties, i.e. string
4435 values that have a string `display' property, that have a string
4436 `display' property etc. */
4437 if (!it->string_from_display_prop_p)
4438 it->area = TEXT_AREA;
4440 propval = get_char_property_and_overlay (make_number (position->charpos),
4441 Qdisplay, object, &overlay);
4442 if (NILP (propval))
4443 return HANDLED_NORMALLY;
4444 /* Now OVERLAY is the overlay that gave us this property, or nil
4445 if it was a text property. */
4447 if (!STRINGP (it->string))
4448 object = it->w->buffer;
4450 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4451 position, bufpos,
4452 FRAME_WINDOW_P (it->f));
4454 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4457 /* Subroutine of handle_display_prop. Returns non-zero if the display
4458 specification in SPEC is a replacing specification, i.e. it would
4459 replace the text covered by `display' property with something else,
4460 such as an image or a display string. If SPEC includes any kind or
4461 `(space ...) specification, the value is 2; this is used by
4462 compute_display_string_pos, which see.
4464 See handle_single_display_spec for documentation of arguments.
4465 frame_window_p is non-zero if the window being redisplayed is on a
4466 GUI frame; this argument is used only if IT is NULL, see below.
4468 IT can be NULL, if this is called by the bidi reordering code
4469 through compute_display_string_pos, which see. In that case, this
4470 function only examines SPEC, but does not otherwise "handle" it, in
4471 the sense that it doesn't set up members of IT from the display
4472 spec. */
4473 static int
4474 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4475 Lisp_Object overlay, struct text_pos *position,
4476 ptrdiff_t bufpos, int frame_window_p)
4478 int replacing_p = 0;
4479 int rv;
4481 if (CONSP (spec)
4482 /* Simple specifications. */
4483 && !EQ (XCAR (spec), Qimage)
4484 && !EQ (XCAR (spec), Qspace)
4485 && !EQ (XCAR (spec), Qwhen)
4486 && !EQ (XCAR (spec), Qslice)
4487 && !EQ (XCAR (spec), Qspace_width)
4488 && !EQ (XCAR (spec), Qheight)
4489 && !EQ (XCAR (spec), Qraise)
4490 /* Marginal area specifications. */
4491 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4492 && !EQ (XCAR (spec), Qleft_fringe)
4493 && !EQ (XCAR (spec), Qright_fringe)
4494 && !NILP (XCAR (spec)))
4496 for (; CONSP (spec); spec = XCDR (spec))
4498 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4499 overlay, position, bufpos,
4500 replacing_p, frame_window_p)))
4502 replacing_p = rv;
4503 /* If some text in a string is replaced, `position' no
4504 longer points to the position of `object'. */
4505 if (!it || STRINGP (object))
4506 break;
4510 else if (VECTORP (spec))
4512 ptrdiff_t i;
4513 for (i = 0; i < ASIZE (spec); ++i)
4514 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4515 overlay, position, bufpos,
4516 replacing_p, frame_window_p)))
4518 replacing_p = rv;
4519 /* If some text in a string is replaced, `position' no
4520 longer points to the position of `object'. */
4521 if (!it || STRINGP (object))
4522 break;
4525 else
4527 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4528 position, bufpos, 0,
4529 frame_window_p)))
4530 replacing_p = rv;
4533 return replacing_p;
4536 /* Value is the position of the end of the `display' property starting
4537 at START_POS in OBJECT. */
4539 static struct text_pos
4540 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4542 Lisp_Object end;
4543 struct text_pos end_pos;
4545 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4546 Qdisplay, object, Qnil);
4547 CHARPOS (end_pos) = XFASTINT (end);
4548 if (STRINGP (object))
4549 compute_string_pos (&end_pos, start_pos, it->string);
4550 else
4551 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4553 return end_pos;
4557 /* Set up IT from a single `display' property specification SPEC. OBJECT
4558 is the object in which the `display' property was found. *POSITION
4559 is the position in OBJECT at which the `display' property was found.
4560 BUFPOS is the buffer position of OBJECT (different from POSITION if
4561 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4562 previously saw a display specification which already replaced text
4563 display with something else, for example an image; we ignore such
4564 properties after the first one has been processed.
4566 OVERLAY is the overlay this `display' property came from,
4567 or nil if it was a text property.
4569 If SPEC is a `space' or `image' specification, and in some other
4570 cases too, set *POSITION to the position where the `display'
4571 property ends.
4573 If IT is NULL, only examine the property specification in SPEC, but
4574 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4575 is intended to be displayed in a window on a GUI frame.
4577 Value is non-zero if something was found which replaces the display
4578 of buffer or string text. */
4580 static int
4581 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4582 Lisp_Object overlay, struct text_pos *position,
4583 ptrdiff_t bufpos, int display_replaced_p,
4584 int frame_window_p)
4586 Lisp_Object form;
4587 Lisp_Object location, value;
4588 struct text_pos start_pos = *position;
4589 int valid_p;
4591 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4592 If the result is non-nil, use VALUE instead of SPEC. */
4593 form = Qt;
4594 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4596 spec = XCDR (spec);
4597 if (!CONSP (spec))
4598 return 0;
4599 form = XCAR (spec);
4600 spec = XCDR (spec);
4603 if (!NILP (form) && !EQ (form, Qt))
4605 ptrdiff_t count = SPECPDL_INDEX ();
4606 struct gcpro gcpro1;
4608 /* Bind `object' to the object having the `display' property, a
4609 buffer or string. Bind `position' to the position in the
4610 object where the property was found, and `buffer-position'
4611 to the current position in the buffer. */
4613 if (NILP (object))
4614 XSETBUFFER (object, current_buffer);
4615 specbind (Qobject, object);
4616 specbind (Qposition, make_number (CHARPOS (*position)));
4617 specbind (Qbuffer_position, make_number (bufpos));
4618 GCPRO1 (form);
4619 form = safe_eval (form);
4620 UNGCPRO;
4621 unbind_to (count, Qnil);
4624 if (NILP (form))
4625 return 0;
4627 /* Handle `(height HEIGHT)' specifications. */
4628 if (CONSP (spec)
4629 && EQ (XCAR (spec), Qheight)
4630 && CONSP (XCDR (spec)))
4632 if (it)
4634 if (!FRAME_WINDOW_P (it->f))
4635 return 0;
4637 it->font_height = XCAR (XCDR (spec));
4638 if (!NILP (it->font_height))
4640 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4641 int new_height = -1;
4643 if (CONSP (it->font_height)
4644 && (EQ (XCAR (it->font_height), Qplus)
4645 || EQ (XCAR (it->font_height), Qminus))
4646 && CONSP (XCDR (it->font_height))
4647 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4649 /* `(+ N)' or `(- N)' where N is an integer. */
4650 int steps = XINT (XCAR (XCDR (it->font_height)));
4651 if (EQ (XCAR (it->font_height), Qplus))
4652 steps = - steps;
4653 it->face_id = smaller_face (it->f, it->face_id, steps);
4655 else if (FUNCTIONP (it->font_height))
4657 /* Call function with current height as argument.
4658 Value is the new height. */
4659 Lisp_Object height;
4660 height = safe_call1 (it->font_height,
4661 face->lface[LFACE_HEIGHT_INDEX]);
4662 if (NUMBERP (height))
4663 new_height = XFLOATINT (height);
4665 else if (NUMBERP (it->font_height))
4667 /* Value is a multiple of the canonical char height. */
4668 struct face *f;
4670 f = FACE_FROM_ID (it->f,
4671 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4672 new_height = (XFLOATINT (it->font_height)
4673 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4675 else
4677 /* Evaluate IT->font_height with `height' bound to the
4678 current specified height to get the new height. */
4679 ptrdiff_t count = SPECPDL_INDEX ();
4681 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4682 value = safe_eval (it->font_height);
4683 unbind_to (count, Qnil);
4685 if (NUMBERP (value))
4686 new_height = XFLOATINT (value);
4689 if (new_height > 0)
4690 it->face_id = face_with_height (it->f, it->face_id, new_height);
4694 return 0;
4697 /* Handle `(space-width WIDTH)'. */
4698 if (CONSP (spec)
4699 && EQ (XCAR (spec), Qspace_width)
4700 && CONSP (XCDR (spec)))
4702 if (it)
4704 if (!FRAME_WINDOW_P (it->f))
4705 return 0;
4707 value = XCAR (XCDR (spec));
4708 if (NUMBERP (value) && XFLOATINT (value) > 0)
4709 it->space_width = value;
4712 return 0;
4715 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4716 if (CONSP (spec)
4717 && EQ (XCAR (spec), Qslice))
4719 Lisp_Object tem;
4721 if (it)
4723 if (!FRAME_WINDOW_P (it->f))
4724 return 0;
4726 if (tem = XCDR (spec), CONSP (tem))
4728 it->slice.x = XCAR (tem);
4729 if (tem = XCDR (tem), CONSP (tem))
4731 it->slice.y = XCAR (tem);
4732 if (tem = XCDR (tem), CONSP (tem))
4734 it->slice.width = XCAR (tem);
4735 if (tem = XCDR (tem), CONSP (tem))
4736 it->slice.height = XCAR (tem);
4742 return 0;
4745 /* Handle `(raise FACTOR)'. */
4746 if (CONSP (spec)
4747 && EQ (XCAR (spec), Qraise)
4748 && CONSP (XCDR (spec)))
4750 if (it)
4752 if (!FRAME_WINDOW_P (it->f))
4753 return 0;
4755 #ifdef HAVE_WINDOW_SYSTEM
4756 value = XCAR (XCDR (spec));
4757 if (NUMBERP (value))
4759 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4760 it->voffset = - (XFLOATINT (value)
4761 * (FONT_HEIGHT (face->font)));
4763 #endif /* HAVE_WINDOW_SYSTEM */
4766 return 0;
4769 /* Don't handle the other kinds of display specifications
4770 inside a string that we got from a `display' property. */
4771 if (it && it->string_from_display_prop_p)
4772 return 0;
4774 /* Characters having this form of property are not displayed, so
4775 we have to find the end of the property. */
4776 if (it)
4778 start_pos = *position;
4779 *position = display_prop_end (it, object, start_pos);
4781 value = Qnil;
4783 /* Stop the scan at that end position--we assume that all
4784 text properties change there. */
4785 if (it)
4786 it->stop_charpos = position->charpos;
4788 /* Handle `(left-fringe BITMAP [FACE])'
4789 and `(right-fringe BITMAP [FACE])'. */
4790 if (CONSP (spec)
4791 && (EQ (XCAR (spec), Qleft_fringe)
4792 || EQ (XCAR (spec), Qright_fringe))
4793 && CONSP (XCDR (spec)))
4795 int fringe_bitmap;
4797 if (it)
4799 if (!FRAME_WINDOW_P (it->f))
4800 /* If we return here, POSITION has been advanced
4801 across the text with this property. */
4803 /* Synchronize the bidi iterator with POSITION. This is
4804 needed because we are not going to push the iterator
4805 on behalf of this display property, so there will be
4806 no pop_it call to do this synchronization for us. */
4807 if (it->bidi_p)
4809 it->position = *position;
4810 iterate_out_of_display_property (it);
4811 *position = it->position;
4813 return 1;
4816 else if (!frame_window_p)
4817 return 1;
4819 #ifdef HAVE_WINDOW_SYSTEM
4820 value = XCAR (XCDR (spec));
4821 if (!SYMBOLP (value)
4822 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4823 /* If we return here, POSITION has been advanced
4824 across the text with this property. */
4826 if (it && it->bidi_p)
4828 it->position = *position;
4829 iterate_out_of_display_property (it);
4830 *position = it->position;
4832 return 1;
4835 if (it)
4837 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4839 if (CONSP (XCDR (XCDR (spec))))
4841 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4842 int face_id2 = lookup_derived_face (it->f, face_name,
4843 FRINGE_FACE_ID, 0);
4844 if (face_id2 >= 0)
4845 face_id = face_id2;
4848 /* Save current settings of IT so that we can restore them
4849 when we are finished with the glyph property value. */
4850 push_it (it, position);
4852 it->area = TEXT_AREA;
4853 it->what = IT_IMAGE;
4854 it->image_id = -1; /* no image */
4855 it->position = start_pos;
4856 it->object = NILP (object) ? it->w->buffer : object;
4857 it->method = GET_FROM_IMAGE;
4858 it->from_overlay = Qnil;
4859 it->face_id = face_id;
4860 it->from_disp_prop_p = 1;
4862 /* Say that we haven't consumed the characters with
4863 `display' property yet. The call to pop_it in
4864 set_iterator_to_next will clean this up. */
4865 *position = start_pos;
4867 if (EQ (XCAR (spec), Qleft_fringe))
4869 it->left_user_fringe_bitmap = fringe_bitmap;
4870 it->left_user_fringe_face_id = face_id;
4872 else
4874 it->right_user_fringe_bitmap = fringe_bitmap;
4875 it->right_user_fringe_face_id = face_id;
4878 #endif /* HAVE_WINDOW_SYSTEM */
4879 return 1;
4882 /* Prepare to handle `((margin left-margin) ...)',
4883 `((margin right-margin) ...)' and `((margin nil) ...)'
4884 prefixes for display specifications. */
4885 location = Qunbound;
4886 if (CONSP (spec) && CONSP (XCAR (spec)))
4888 Lisp_Object tem;
4890 value = XCDR (spec);
4891 if (CONSP (value))
4892 value = XCAR (value);
4894 tem = XCAR (spec);
4895 if (EQ (XCAR (tem), Qmargin)
4896 && (tem = XCDR (tem),
4897 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4898 (NILP (tem)
4899 || EQ (tem, Qleft_margin)
4900 || EQ (tem, Qright_margin))))
4901 location = tem;
4904 if (EQ (location, Qunbound))
4906 location = Qnil;
4907 value = spec;
4910 /* After this point, VALUE is the property after any
4911 margin prefix has been stripped. It must be a string,
4912 an image specification, or `(space ...)'.
4914 LOCATION specifies where to display: `left-margin',
4915 `right-margin' or nil. */
4917 valid_p = (STRINGP (value)
4918 #ifdef HAVE_WINDOW_SYSTEM
4919 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4920 && valid_image_p (value))
4921 #endif /* not HAVE_WINDOW_SYSTEM */
4922 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4924 if (valid_p && !display_replaced_p)
4926 int retval = 1;
4928 if (!it)
4930 /* Callers need to know whether the display spec is any kind
4931 of `(space ...)' spec that is about to affect text-area
4932 display. */
4933 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4934 retval = 2;
4935 return retval;
4938 /* Save current settings of IT so that we can restore them
4939 when we are finished with the glyph property value. */
4940 push_it (it, position);
4941 it->from_overlay = overlay;
4942 it->from_disp_prop_p = 1;
4944 if (NILP (location))
4945 it->area = TEXT_AREA;
4946 else if (EQ (location, Qleft_margin))
4947 it->area = LEFT_MARGIN_AREA;
4948 else
4949 it->area = RIGHT_MARGIN_AREA;
4951 if (STRINGP (value))
4953 it->string = value;
4954 it->multibyte_p = STRING_MULTIBYTE (it->string);
4955 it->current.overlay_string_index = -1;
4956 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4957 it->end_charpos = it->string_nchars = SCHARS (it->string);
4958 it->method = GET_FROM_STRING;
4959 it->stop_charpos = 0;
4960 it->prev_stop = 0;
4961 it->base_level_stop = 0;
4962 it->string_from_display_prop_p = 1;
4963 /* Say that we haven't consumed the characters with
4964 `display' property yet. The call to pop_it in
4965 set_iterator_to_next will clean this up. */
4966 if (BUFFERP (object))
4967 *position = start_pos;
4969 /* Force paragraph direction to be that of the parent
4970 object. If the parent object's paragraph direction is
4971 not yet determined, default to L2R. */
4972 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4973 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4974 else
4975 it->paragraph_embedding = L2R;
4977 /* Set up the bidi iterator for this display string. */
4978 if (it->bidi_p)
4980 it->bidi_it.string.lstring = it->string;
4981 it->bidi_it.string.s = NULL;
4982 it->bidi_it.string.schars = it->end_charpos;
4983 it->bidi_it.string.bufpos = bufpos;
4984 it->bidi_it.string.from_disp_str = 1;
4985 it->bidi_it.string.unibyte = !it->multibyte_p;
4986 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4989 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4991 it->method = GET_FROM_STRETCH;
4992 it->object = value;
4993 *position = it->position = start_pos;
4994 retval = 1 + (it->area == TEXT_AREA);
4996 #ifdef HAVE_WINDOW_SYSTEM
4997 else
4999 it->what = IT_IMAGE;
5000 it->image_id = lookup_image (it->f, value);
5001 it->position = start_pos;
5002 it->object = NILP (object) ? it->w->buffer : object;
5003 it->method = GET_FROM_IMAGE;
5005 /* Say that we haven't consumed the characters with
5006 `display' property yet. The call to pop_it in
5007 set_iterator_to_next will clean this up. */
5008 *position = start_pos;
5010 #endif /* HAVE_WINDOW_SYSTEM */
5012 return retval;
5015 /* Invalid property or property not supported. Restore
5016 POSITION to what it was before. */
5017 *position = start_pos;
5018 return 0;
5021 /* Check if PROP is a display property value whose text should be
5022 treated as intangible. OVERLAY is the overlay from which PROP
5023 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5024 specify the buffer position covered by PROP. */
5027 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5028 ptrdiff_t charpos, ptrdiff_t bytepos)
5030 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5031 struct text_pos position;
5033 SET_TEXT_POS (position, charpos, bytepos);
5034 return handle_display_spec (NULL, prop, Qnil, overlay,
5035 &position, charpos, frame_window_p);
5039 /* Return 1 if PROP is a display sub-property value containing STRING.
5041 Implementation note: this and the following function are really
5042 special cases of handle_display_spec and
5043 handle_single_display_spec, and should ideally use the same code.
5044 Until they do, these two pairs must be consistent and must be
5045 modified in sync. */
5047 static int
5048 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5050 if (EQ (string, prop))
5051 return 1;
5053 /* Skip over `when FORM'. */
5054 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5056 prop = XCDR (prop);
5057 if (!CONSP (prop))
5058 return 0;
5059 /* Actually, the condition following `when' should be eval'ed,
5060 like handle_single_display_spec does, and we should return
5061 zero if it evaluates to nil. However, this function is
5062 called only when the buffer was already displayed and some
5063 glyph in the glyph matrix was found to come from a display
5064 string. Therefore, the condition was already evaluated, and
5065 the result was non-nil, otherwise the display string wouldn't
5066 have been displayed and we would have never been called for
5067 this property. Thus, we can skip the evaluation and assume
5068 its result is non-nil. */
5069 prop = XCDR (prop);
5072 if (CONSP (prop))
5073 /* Skip over `margin LOCATION'. */
5074 if (EQ (XCAR (prop), Qmargin))
5076 prop = XCDR (prop);
5077 if (!CONSP (prop))
5078 return 0;
5080 prop = XCDR (prop);
5081 if (!CONSP (prop))
5082 return 0;
5085 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5089 /* Return 1 if STRING appears in the `display' property PROP. */
5091 static int
5092 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5094 if (CONSP (prop)
5095 && !EQ (XCAR (prop), Qwhen)
5096 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5098 /* A list of sub-properties. */
5099 while (CONSP (prop))
5101 if (single_display_spec_string_p (XCAR (prop), string))
5102 return 1;
5103 prop = XCDR (prop);
5106 else if (VECTORP (prop))
5108 /* A vector of sub-properties. */
5109 ptrdiff_t i;
5110 for (i = 0; i < ASIZE (prop); ++i)
5111 if (single_display_spec_string_p (AREF (prop, i), string))
5112 return 1;
5114 else
5115 return single_display_spec_string_p (prop, string);
5117 return 0;
5120 /* Look for STRING in overlays and text properties in the current
5121 buffer, between character positions FROM and TO (excluding TO).
5122 BACK_P non-zero means look back (in this case, TO is supposed to be
5123 less than FROM).
5124 Value is the first character position where STRING was found, or
5125 zero if it wasn't found before hitting TO.
5127 This function may only use code that doesn't eval because it is
5128 called asynchronously from note_mouse_highlight. */
5130 static ptrdiff_t
5131 string_buffer_position_lim (Lisp_Object string,
5132 ptrdiff_t from, ptrdiff_t to, int back_p)
5134 Lisp_Object limit, prop, pos;
5135 int found = 0;
5137 pos = make_number (max (from, BEGV));
5139 if (!back_p) /* looking forward */
5141 limit = make_number (min (to, ZV));
5142 while (!found && !EQ (pos, limit))
5144 prop = Fget_char_property (pos, Qdisplay, Qnil);
5145 if (!NILP (prop) && display_prop_string_p (prop, string))
5146 found = 1;
5147 else
5148 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5149 limit);
5152 else /* looking back */
5154 limit = make_number (max (to, BEGV));
5155 while (!found && !EQ (pos, limit))
5157 prop = Fget_char_property (pos, Qdisplay, Qnil);
5158 if (!NILP (prop) && display_prop_string_p (prop, string))
5159 found = 1;
5160 else
5161 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5162 limit);
5166 return found ? XINT (pos) : 0;
5169 /* Determine which buffer position in current buffer STRING comes from.
5170 AROUND_CHARPOS is an approximate position where it could come from.
5171 Value is the buffer position or 0 if it couldn't be determined.
5173 This function is necessary because we don't record buffer positions
5174 in glyphs generated from strings (to keep struct glyph small).
5175 This function may only use code that doesn't eval because it is
5176 called asynchronously from note_mouse_highlight. */
5178 static ptrdiff_t
5179 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5181 const int MAX_DISTANCE = 1000;
5182 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5183 around_charpos + MAX_DISTANCE,
5186 if (!found)
5187 found = string_buffer_position_lim (string, around_charpos,
5188 around_charpos - MAX_DISTANCE, 1);
5189 return found;
5194 /***********************************************************************
5195 `composition' property
5196 ***********************************************************************/
5198 /* Set up iterator IT from `composition' property at its current
5199 position. Called from handle_stop. */
5201 static enum prop_handled
5202 handle_composition_prop (struct it *it)
5204 Lisp_Object prop, string;
5205 ptrdiff_t pos, pos_byte, start, end;
5207 if (STRINGP (it->string))
5209 unsigned char *s;
5211 pos = IT_STRING_CHARPOS (*it);
5212 pos_byte = IT_STRING_BYTEPOS (*it);
5213 string = it->string;
5214 s = SDATA (string) + pos_byte;
5215 it->c = STRING_CHAR (s);
5217 else
5219 pos = IT_CHARPOS (*it);
5220 pos_byte = IT_BYTEPOS (*it);
5221 string = Qnil;
5222 it->c = FETCH_CHAR (pos_byte);
5225 /* If there's a valid composition and point is not inside of the
5226 composition (in the case that the composition is from the current
5227 buffer), draw a glyph composed from the composition components. */
5228 if (find_composition (pos, -1, &start, &end, &prop, string)
5229 && COMPOSITION_VALID_P (start, end, prop)
5230 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5232 if (start < pos)
5233 /* As we can't handle this situation (perhaps font-lock added
5234 a new composition), we just return here hoping that next
5235 redisplay will detect this composition much earlier. */
5236 return HANDLED_NORMALLY;
5237 if (start != pos)
5239 if (STRINGP (it->string))
5240 pos_byte = string_char_to_byte (it->string, start);
5241 else
5242 pos_byte = CHAR_TO_BYTE (start);
5244 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5245 prop, string);
5247 if (it->cmp_it.id >= 0)
5249 it->cmp_it.ch = -1;
5250 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5251 it->cmp_it.nglyphs = -1;
5255 return HANDLED_NORMALLY;
5260 /***********************************************************************
5261 Overlay strings
5262 ***********************************************************************/
5264 /* The following structure is used to record overlay strings for
5265 later sorting in load_overlay_strings. */
5267 struct overlay_entry
5269 Lisp_Object overlay;
5270 Lisp_Object string;
5271 EMACS_INT priority;
5272 int after_string_p;
5276 /* Set up iterator IT from overlay strings at its current position.
5277 Called from handle_stop. */
5279 static enum prop_handled
5280 handle_overlay_change (struct it *it)
5282 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5283 return HANDLED_RECOMPUTE_PROPS;
5284 else
5285 return HANDLED_NORMALLY;
5289 /* Set up the next overlay string for delivery by IT, if there is an
5290 overlay string to deliver. Called by set_iterator_to_next when the
5291 end of the current overlay string is reached. If there are more
5292 overlay strings to display, IT->string and
5293 IT->current.overlay_string_index are set appropriately here.
5294 Otherwise IT->string is set to nil. */
5296 static void
5297 next_overlay_string (struct it *it)
5299 ++it->current.overlay_string_index;
5300 if (it->current.overlay_string_index == it->n_overlay_strings)
5302 /* No more overlay strings. Restore IT's settings to what
5303 they were before overlay strings were processed, and
5304 continue to deliver from current_buffer. */
5306 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5307 pop_it (it);
5308 eassert (it->sp > 0
5309 || (NILP (it->string)
5310 && it->method == GET_FROM_BUFFER
5311 && it->stop_charpos >= BEGV
5312 && it->stop_charpos <= it->end_charpos));
5313 it->current.overlay_string_index = -1;
5314 it->n_overlay_strings = 0;
5315 it->overlay_strings_charpos = -1;
5316 /* If there's an empty display string on the stack, pop the
5317 stack, to resync the bidi iterator with IT's position. Such
5318 empty strings are pushed onto the stack in
5319 get_overlay_strings_1. */
5320 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5321 pop_it (it);
5323 /* If we're at the end of the buffer, record that we have
5324 processed the overlay strings there already, so that
5325 next_element_from_buffer doesn't try it again. */
5326 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5327 it->overlay_strings_at_end_processed_p = 1;
5329 else
5331 /* There are more overlay strings to process. If
5332 IT->current.overlay_string_index has advanced to a position
5333 where we must load IT->overlay_strings with more strings, do
5334 it. We must load at the IT->overlay_strings_charpos where
5335 IT->n_overlay_strings was originally computed; when invisible
5336 text is present, this might not be IT_CHARPOS (Bug#7016). */
5337 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5339 if (it->current.overlay_string_index && i == 0)
5340 load_overlay_strings (it, it->overlay_strings_charpos);
5342 /* Initialize IT to deliver display elements from the overlay
5343 string. */
5344 it->string = it->overlay_strings[i];
5345 it->multibyte_p = STRING_MULTIBYTE (it->string);
5346 SET_TEXT_POS (it->current.string_pos, 0, 0);
5347 it->method = GET_FROM_STRING;
5348 it->stop_charpos = 0;
5349 if (it->cmp_it.stop_pos >= 0)
5350 it->cmp_it.stop_pos = 0;
5351 it->prev_stop = 0;
5352 it->base_level_stop = 0;
5354 /* Set up the bidi iterator for this overlay string. */
5355 if (it->bidi_p)
5357 it->bidi_it.string.lstring = it->string;
5358 it->bidi_it.string.s = NULL;
5359 it->bidi_it.string.schars = SCHARS (it->string);
5360 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5361 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5362 it->bidi_it.string.unibyte = !it->multibyte_p;
5363 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5367 CHECK_IT (it);
5371 /* Compare two overlay_entry structures E1 and E2. Used as a
5372 comparison function for qsort in load_overlay_strings. Overlay
5373 strings for the same position are sorted so that
5375 1. All after-strings come in front of before-strings, except
5376 when they come from the same overlay.
5378 2. Within after-strings, strings are sorted so that overlay strings
5379 from overlays with higher priorities come first.
5381 2. Within before-strings, strings are sorted so that overlay
5382 strings from overlays with higher priorities come last.
5384 Value is analogous to strcmp. */
5387 static int
5388 compare_overlay_entries (const void *e1, const void *e2)
5390 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5391 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5392 int result;
5394 if (entry1->after_string_p != entry2->after_string_p)
5396 /* Let after-strings appear in front of before-strings if
5397 they come from different overlays. */
5398 if (EQ (entry1->overlay, entry2->overlay))
5399 result = entry1->after_string_p ? 1 : -1;
5400 else
5401 result = entry1->after_string_p ? -1 : 1;
5403 else if (entry1->priority != entry2->priority)
5405 if (entry1->after_string_p)
5406 /* After-strings sorted in order of decreasing priority. */
5407 result = entry2->priority < entry1->priority ? -1 : 1;
5408 else
5409 /* Before-strings sorted in order of increasing priority. */
5410 result = entry1->priority < entry2->priority ? -1 : 1;
5412 else
5413 result = 0;
5415 return result;
5419 /* Load the vector IT->overlay_strings with overlay strings from IT's
5420 current buffer position, or from CHARPOS if that is > 0. Set
5421 IT->n_overlays to the total number of overlay strings found.
5423 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5424 a time. On entry into load_overlay_strings,
5425 IT->current.overlay_string_index gives the number of overlay
5426 strings that have already been loaded by previous calls to this
5427 function.
5429 IT->add_overlay_start contains an additional overlay start
5430 position to consider for taking overlay strings from, if non-zero.
5431 This position comes into play when the overlay has an `invisible'
5432 property, and both before and after-strings. When we've skipped to
5433 the end of the overlay, because of its `invisible' property, we
5434 nevertheless want its before-string to appear.
5435 IT->add_overlay_start will contain the overlay start position
5436 in this case.
5438 Overlay strings are sorted so that after-string strings come in
5439 front of before-string strings. Within before and after-strings,
5440 strings are sorted by overlay priority. See also function
5441 compare_overlay_entries. */
5443 static void
5444 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5446 Lisp_Object overlay, window, str, invisible;
5447 struct Lisp_Overlay *ov;
5448 ptrdiff_t start, end;
5449 ptrdiff_t size = 20;
5450 ptrdiff_t n = 0, i, j;
5451 int invis_p;
5452 struct overlay_entry *entries = alloca (size * sizeof *entries);
5453 USE_SAFE_ALLOCA;
5455 if (charpos <= 0)
5456 charpos = IT_CHARPOS (*it);
5458 /* Append the overlay string STRING of overlay OVERLAY to vector
5459 `entries' which has size `size' and currently contains `n'
5460 elements. AFTER_P non-zero means STRING is an after-string of
5461 OVERLAY. */
5462 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5463 do \
5465 Lisp_Object priority; \
5467 if (n == size) \
5469 struct overlay_entry *old = entries; \
5470 SAFE_NALLOCA (entries, 2, size); \
5471 memcpy (entries, old, size * sizeof *entries); \
5472 size *= 2; \
5475 entries[n].string = (STRING); \
5476 entries[n].overlay = (OVERLAY); \
5477 priority = Foverlay_get ((OVERLAY), Qpriority); \
5478 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5479 entries[n].after_string_p = (AFTER_P); \
5480 ++n; \
5482 while (0)
5484 /* Process overlay before the overlay center. */
5485 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5487 XSETMISC (overlay, ov);
5488 eassert (OVERLAYP (overlay));
5489 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5490 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5492 if (end < charpos)
5493 break;
5495 /* Skip this overlay if it doesn't start or end at IT's current
5496 position. */
5497 if (end != charpos && start != charpos)
5498 continue;
5500 /* Skip this overlay if it doesn't apply to IT->w. */
5501 window = Foverlay_get (overlay, Qwindow);
5502 if (WINDOWP (window) && XWINDOW (window) != it->w)
5503 continue;
5505 /* If the text ``under'' the overlay is invisible, both before-
5506 and after-strings from this overlay are visible; start and
5507 end position are indistinguishable. */
5508 invisible = Foverlay_get (overlay, Qinvisible);
5509 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5511 /* If overlay has a non-empty before-string, record it. */
5512 if ((start == charpos || (end == charpos && invis_p))
5513 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5514 && SCHARS (str))
5515 RECORD_OVERLAY_STRING (overlay, str, 0);
5517 /* If overlay has a non-empty after-string, record it. */
5518 if ((end == charpos || (start == charpos && invis_p))
5519 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5520 && SCHARS (str))
5521 RECORD_OVERLAY_STRING (overlay, str, 1);
5524 /* Process overlays after the overlay center. */
5525 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5527 XSETMISC (overlay, ov);
5528 eassert (OVERLAYP (overlay));
5529 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5530 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5532 if (start > charpos)
5533 break;
5535 /* Skip this overlay if it doesn't start or end at IT's current
5536 position. */
5537 if (end != charpos && start != charpos)
5538 continue;
5540 /* Skip this overlay if it doesn't apply to IT->w. */
5541 window = Foverlay_get (overlay, Qwindow);
5542 if (WINDOWP (window) && XWINDOW (window) != it->w)
5543 continue;
5545 /* If the text ``under'' the overlay is invisible, it has a zero
5546 dimension, and both before- and after-strings apply. */
5547 invisible = Foverlay_get (overlay, Qinvisible);
5548 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5550 /* If overlay has a non-empty before-string, record it. */
5551 if ((start == charpos || (end == charpos && invis_p))
5552 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5553 && SCHARS (str))
5554 RECORD_OVERLAY_STRING (overlay, str, 0);
5556 /* If overlay has a non-empty after-string, record it. */
5557 if ((end == charpos || (start == charpos && invis_p))
5558 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5559 && SCHARS (str))
5560 RECORD_OVERLAY_STRING (overlay, str, 1);
5563 #undef RECORD_OVERLAY_STRING
5565 /* Sort entries. */
5566 if (n > 1)
5567 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5569 /* Record number of overlay strings, and where we computed it. */
5570 it->n_overlay_strings = n;
5571 it->overlay_strings_charpos = charpos;
5573 /* IT->current.overlay_string_index is the number of overlay strings
5574 that have already been consumed by IT. Copy some of the
5575 remaining overlay strings to IT->overlay_strings. */
5576 i = 0;
5577 j = it->current.overlay_string_index;
5578 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5580 it->overlay_strings[i] = entries[j].string;
5581 it->string_overlays[i++] = entries[j++].overlay;
5584 CHECK_IT (it);
5585 SAFE_FREE ();
5589 /* Get the first chunk of overlay strings at IT's current buffer
5590 position, or at CHARPOS if that is > 0. Value is non-zero if at
5591 least one overlay string was found. */
5593 static int
5594 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5596 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5597 process. This fills IT->overlay_strings with strings, and sets
5598 IT->n_overlay_strings to the total number of strings to process.
5599 IT->pos.overlay_string_index has to be set temporarily to zero
5600 because load_overlay_strings needs this; it must be set to -1
5601 when no overlay strings are found because a zero value would
5602 indicate a position in the first overlay string. */
5603 it->current.overlay_string_index = 0;
5604 load_overlay_strings (it, charpos);
5606 /* If we found overlay strings, set up IT to deliver display
5607 elements from the first one. Otherwise set up IT to deliver
5608 from current_buffer. */
5609 if (it->n_overlay_strings)
5611 /* Make sure we know settings in current_buffer, so that we can
5612 restore meaningful values when we're done with the overlay
5613 strings. */
5614 if (compute_stop_p)
5615 compute_stop_pos (it);
5616 eassert (it->face_id >= 0);
5618 /* Save IT's settings. They are restored after all overlay
5619 strings have been processed. */
5620 eassert (!compute_stop_p || it->sp == 0);
5622 /* When called from handle_stop, there might be an empty display
5623 string loaded. In that case, don't bother saving it. But
5624 don't use this optimization with the bidi iterator, since we
5625 need the corresponding pop_it call to resync the bidi
5626 iterator's position with IT's position, after we are done
5627 with the overlay strings. (The corresponding call to pop_it
5628 in case of an empty display string is in
5629 next_overlay_string.) */
5630 if (!(!it->bidi_p
5631 && STRINGP (it->string) && !SCHARS (it->string)))
5632 push_it (it, NULL);
5634 /* Set up IT to deliver display elements from the first overlay
5635 string. */
5636 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5637 it->string = it->overlay_strings[0];
5638 it->from_overlay = Qnil;
5639 it->stop_charpos = 0;
5640 eassert (STRINGP (it->string));
5641 it->end_charpos = SCHARS (it->string);
5642 it->prev_stop = 0;
5643 it->base_level_stop = 0;
5644 it->multibyte_p = STRING_MULTIBYTE (it->string);
5645 it->method = GET_FROM_STRING;
5646 it->from_disp_prop_p = 0;
5648 /* Force paragraph direction to be that of the parent
5649 buffer. */
5650 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5651 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5652 else
5653 it->paragraph_embedding = L2R;
5655 /* Set up the bidi iterator for this overlay string. */
5656 if (it->bidi_p)
5658 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5660 it->bidi_it.string.lstring = it->string;
5661 it->bidi_it.string.s = NULL;
5662 it->bidi_it.string.schars = SCHARS (it->string);
5663 it->bidi_it.string.bufpos = pos;
5664 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5665 it->bidi_it.string.unibyte = !it->multibyte_p;
5666 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5668 return 1;
5671 it->current.overlay_string_index = -1;
5672 return 0;
5675 static int
5676 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5678 it->string = Qnil;
5679 it->method = GET_FROM_BUFFER;
5681 (void) get_overlay_strings_1 (it, charpos, 1);
5683 CHECK_IT (it);
5685 /* Value is non-zero if we found at least one overlay string. */
5686 return STRINGP (it->string);
5691 /***********************************************************************
5692 Saving and restoring state
5693 ***********************************************************************/
5695 /* Save current settings of IT on IT->stack. Called, for example,
5696 before setting up IT for an overlay string, to be able to restore
5697 IT's settings to what they were after the overlay string has been
5698 processed. If POSITION is non-NULL, it is the position to save on
5699 the stack instead of IT->position. */
5701 static void
5702 push_it (struct it *it, struct text_pos *position)
5704 struct iterator_stack_entry *p;
5706 eassert (it->sp < IT_STACK_SIZE);
5707 p = it->stack + it->sp;
5709 p->stop_charpos = it->stop_charpos;
5710 p->prev_stop = it->prev_stop;
5711 p->base_level_stop = it->base_level_stop;
5712 p->cmp_it = it->cmp_it;
5713 eassert (it->face_id >= 0);
5714 p->face_id = it->face_id;
5715 p->string = it->string;
5716 p->method = it->method;
5717 p->from_overlay = it->from_overlay;
5718 switch (p->method)
5720 case GET_FROM_IMAGE:
5721 p->u.image.object = it->object;
5722 p->u.image.image_id = it->image_id;
5723 p->u.image.slice = it->slice;
5724 break;
5725 case GET_FROM_STRETCH:
5726 p->u.stretch.object = it->object;
5727 break;
5729 p->position = position ? *position : it->position;
5730 p->current = it->current;
5731 p->end_charpos = it->end_charpos;
5732 p->string_nchars = it->string_nchars;
5733 p->area = it->area;
5734 p->multibyte_p = it->multibyte_p;
5735 p->avoid_cursor_p = it->avoid_cursor_p;
5736 p->space_width = it->space_width;
5737 p->font_height = it->font_height;
5738 p->voffset = it->voffset;
5739 p->string_from_display_prop_p = it->string_from_display_prop_p;
5740 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5741 p->display_ellipsis_p = 0;
5742 p->line_wrap = it->line_wrap;
5743 p->bidi_p = it->bidi_p;
5744 p->paragraph_embedding = it->paragraph_embedding;
5745 p->from_disp_prop_p = it->from_disp_prop_p;
5746 ++it->sp;
5748 /* Save the state of the bidi iterator as well. */
5749 if (it->bidi_p)
5750 bidi_push_it (&it->bidi_it);
5753 static void
5754 iterate_out_of_display_property (struct it *it)
5756 int buffer_p = !STRINGP (it->string);
5757 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5758 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5760 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5762 /* Maybe initialize paragraph direction. If we are at the beginning
5763 of a new paragraph, next_element_from_buffer may not have a
5764 chance to do that. */
5765 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5766 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5767 /* prev_stop can be zero, so check against BEGV as well. */
5768 while (it->bidi_it.charpos >= bob
5769 && it->prev_stop <= it->bidi_it.charpos
5770 && it->bidi_it.charpos < CHARPOS (it->position)
5771 && it->bidi_it.charpos < eob)
5772 bidi_move_to_visually_next (&it->bidi_it);
5773 /* Record the stop_pos we just crossed, for when we cross it
5774 back, maybe. */
5775 if (it->bidi_it.charpos > CHARPOS (it->position))
5776 it->prev_stop = CHARPOS (it->position);
5777 /* If we ended up not where pop_it put us, resync IT's
5778 positional members with the bidi iterator. */
5779 if (it->bidi_it.charpos != CHARPOS (it->position))
5780 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5781 if (buffer_p)
5782 it->current.pos = it->position;
5783 else
5784 it->current.string_pos = it->position;
5787 /* Restore IT's settings from IT->stack. Called, for example, when no
5788 more overlay strings must be processed, and we return to delivering
5789 display elements from a buffer, or when the end of a string from a
5790 `display' property is reached and we return to delivering display
5791 elements from an overlay string, or from a buffer. */
5793 static void
5794 pop_it (struct it *it)
5796 struct iterator_stack_entry *p;
5797 int from_display_prop = it->from_disp_prop_p;
5799 eassert (it->sp > 0);
5800 --it->sp;
5801 p = it->stack + it->sp;
5802 it->stop_charpos = p->stop_charpos;
5803 it->prev_stop = p->prev_stop;
5804 it->base_level_stop = p->base_level_stop;
5805 it->cmp_it = p->cmp_it;
5806 it->face_id = p->face_id;
5807 it->current = p->current;
5808 it->position = p->position;
5809 it->string = p->string;
5810 it->from_overlay = p->from_overlay;
5811 if (NILP (it->string))
5812 SET_TEXT_POS (it->current.string_pos, -1, -1);
5813 it->method = p->method;
5814 switch (it->method)
5816 case GET_FROM_IMAGE:
5817 it->image_id = p->u.image.image_id;
5818 it->object = p->u.image.object;
5819 it->slice = p->u.image.slice;
5820 break;
5821 case GET_FROM_STRETCH:
5822 it->object = p->u.stretch.object;
5823 break;
5824 case GET_FROM_BUFFER:
5825 it->object = it->w->buffer;
5826 break;
5827 case GET_FROM_STRING:
5828 it->object = it->string;
5829 break;
5830 case GET_FROM_DISPLAY_VECTOR:
5831 if (it->s)
5832 it->method = GET_FROM_C_STRING;
5833 else if (STRINGP (it->string))
5834 it->method = GET_FROM_STRING;
5835 else
5837 it->method = GET_FROM_BUFFER;
5838 it->object = it->w->buffer;
5841 it->end_charpos = p->end_charpos;
5842 it->string_nchars = p->string_nchars;
5843 it->area = p->area;
5844 it->multibyte_p = p->multibyte_p;
5845 it->avoid_cursor_p = p->avoid_cursor_p;
5846 it->space_width = p->space_width;
5847 it->font_height = p->font_height;
5848 it->voffset = p->voffset;
5849 it->string_from_display_prop_p = p->string_from_display_prop_p;
5850 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5851 it->line_wrap = p->line_wrap;
5852 it->bidi_p = p->bidi_p;
5853 it->paragraph_embedding = p->paragraph_embedding;
5854 it->from_disp_prop_p = p->from_disp_prop_p;
5855 if (it->bidi_p)
5857 bidi_pop_it (&it->bidi_it);
5858 /* Bidi-iterate until we get out of the portion of text, if any,
5859 covered by a `display' text property or by an overlay with
5860 `display' property. (We cannot just jump there, because the
5861 internal coherency of the bidi iterator state can not be
5862 preserved across such jumps.) We also must determine the
5863 paragraph base direction if the overlay we just processed is
5864 at the beginning of a new paragraph. */
5865 if (from_display_prop
5866 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5867 iterate_out_of_display_property (it);
5869 eassert ((BUFFERP (it->object)
5870 && IT_CHARPOS (*it) == it->bidi_it.charpos
5871 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5872 || (STRINGP (it->object)
5873 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5874 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5875 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5881 /***********************************************************************
5882 Moving over lines
5883 ***********************************************************************/
5885 /* Set IT's current position to the previous line start. */
5887 static void
5888 back_to_previous_line_start (struct it *it)
5890 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5891 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5895 /* Move IT to the next line start.
5897 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5898 we skipped over part of the text (as opposed to moving the iterator
5899 continuously over the text). Otherwise, don't change the value
5900 of *SKIPPED_P.
5902 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5903 iterator on the newline, if it was found.
5905 Newlines may come from buffer text, overlay strings, or strings
5906 displayed via the `display' property. That's the reason we can't
5907 simply use find_next_newline_no_quit.
5909 Note that this function may not skip over invisible text that is so
5910 because of text properties and immediately follows a newline. If
5911 it would, function reseat_at_next_visible_line_start, when called
5912 from set_iterator_to_next, would effectively make invisible
5913 characters following a newline part of the wrong glyph row, which
5914 leads to wrong cursor motion. */
5916 static int
5917 forward_to_next_line_start (struct it *it, int *skipped_p,
5918 struct bidi_it *bidi_it_prev)
5920 ptrdiff_t old_selective;
5921 int newline_found_p, n;
5922 const int MAX_NEWLINE_DISTANCE = 500;
5924 /* If already on a newline, just consume it to avoid unintended
5925 skipping over invisible text below. */
5926 if (it->what == IT_CHARACTER
5927 && it->c == '\n'
5928 && CHARPOS (it->position) == IT_CHARPOS (*it))
5930 if (it->bidi_p && bidi_it_prev)
5931 *bidi_it_prev = it->bidi_it;
5932 set_iterator_to_next (it, 0);
5933 it->c = 0;
5934 return 1;
5937 /* Don't handle selective display in the following. It's (a)
5938 unnecessary because it's done by the caller, and (b) leads to an
5939 infinite recursion because next_element_from_ellipsis indirectly
5940 calls this function. */
5941 old_selective = it->selective;
5942 it->selective = 0;
5944 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5945 from buffer text. */
5946 for (n = newline_found_p = 0;
5947 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5948 n += STRINGP (it->string) ? 0 : 1)
5950 if (!get_next_display_element (it))
5951 return 0;
5952 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5953 if (newline_found_p && it->bidi_p && bidi_it_prev)
5954 *bidi_it_prev = it->bidi_it;
5955 set_iterator_to_next (it, 0);
5958 /* If we didn't find a newline near enough, see if we can use a
5959 short-cut. */
5960 if (!newline_found_p)
5962 ptrdiff_t start = IT_CHARPOS (*it);
5963 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5964 Lisp_Object pos;
5966 eassert (!STRINGP (it->string));
5968 /* If there isn't any `display' property in sight, and no
5969 overlays, we can just use the position of the newline in
5970 buffer text. */
5971 if (it->stop_charpos >= limit
5972 || ((pos = Fnext_single_property_change (make_number (start),
5973 Qdisplay, Qnil,
5974 make_number (limit)),
5975 NILP (pos))
5976 && next_overlay_change (start) == ZV))
5978 if (!it->bidi_p)
5980 IT_CHARPOS (*it) = limit;
5981 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5983 else
5985 struct bidi_it bprev;
5987 /* Help bidi.c avoid expensive searches for display
5988 properties and overlays, by telling it that there are
5989 none up to `limit'. */
5990 if (it->bidi_it.disp_pos < limit)
5992 it->bidi_it.disp_pos = limit;
5993 it->bidi_it.disp_prop = 0;
5995 do {
5996 bprev = it->bidi_it;
5997 bidi_move_to_visually_next (&it->bidi_it);
5998 } while (it->bidi_it.charpos != limit);
5999 IT_CHARPOS (*it) = limit;
6000 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6001 if (bidi_it_prev)
6002 *bidi_it_prev = bprev;
6004 *skipped_p = newline_found_p = 1;
6006 else
6008 while (get_next_display_element (it)
6009 && !newline_found_p)
6011 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6012 if (newline_found_p && it->bidi_p && bidi_it_prev)
6013 *bidi_it_prev = it->bidi_it;
6014 set_iterator_to_next (it, 0);
6019 it->selective = old_selective;
6020 return newline_found_p;
6024 /* Set IT's current position to the previous visible line start. Skip
6025 invisible text that is so either due to text properties or due to
6026 selective display. Caution: this does not change IT->current_x and
6027 IT->hpos. */
6029 static void
6030 back_to_previous_visible_line_start (struct it *it)
6032 while (IT_CHARPOS (*it) > BEGV)
6034 back_to_previous_line_start (it);
6036 if (IT_CHARPOS (*it) <= BEGV)
6037 break;
6039 /* If selective > 0, then lines indented more than its value are
6040 invisible. */
6041 if (it->selective > 0
6042 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6043 it->selective))
6044 continue;
6046 /* Check the newline before point for invisibility. */
6048 Lisp_Object prop;
6049 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6050 Qinvisible, it->window);
6051 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6052 continue;
6055 if (IT_CHARPOS (*it) <= BEGV)
6056 break;
6059 struct it it2;
6060 void *it2data = NULL;
6061 ptrdiff_t pos;
6062 ptrdiff_t beg, end;
6063 Lisp_Object val, overlay;
6065 SAVE_IT (it2, *it, it2data);
6067 /* If newline is part of a composition, continue from start of composition */
6068 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6069 && beg < IT_CHARPOS (*it))
6070 goto replaced;
6072 /* If newline is replaced by a display property, find start of overlay
6073 or interval and continue search from that point. */
6074 pos = --IT_CHARPOS (it2);
6075 --IT_BYTEPOS (it2);
6076 it2.sp = 0;
6077 bidi_unshelve_cache (NULL, 0);
6078 it2.string_from_display_prop_p = 0;
6079 it2.from_disp_prop_p = 0;
6080 if (handle_display_prop (&it2) == HANDLED_RETURN
6081 && !NILP (val = get_char_property_and_overlay
6082 (make_number (pos), Qdisplay, Qnil, &overlay))
6083 && (OVERLAYP (overlay)
6084 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6085 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6087 RESTORE_IT (it, it, it2data);
6088 goto replaced;
6091 /* Newline is not replaced by anything -- so we are done. */
6092 RESTORE_IT (it, it, it2data);
6093 break;
6095 replaced:
6096 if (beg < BEGV)
6097 beg = BEGV;
6098 IT_CHARPOS (*it) = beg;
6099 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6103 it->continuation_lines_width = 0;
6105 eassert (IT_CHARPOS (*it) >= BEGV);
6106 eassert (IT_CHARPOS (*it) == BEGV
6107 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6108 CHECK_IT (it);
6112 /* Reseat iterator IT at the previous visible line start. Skip
6113 invisible text that is so either due to text properties or due to
6114 selective display. At the end, update IT's overlay information,
6115 face information etc. */
6117 void
6118 reseat_at_previous_visible_line_start (struct it *it)
6120 back_to_previous_visible_line_start (it);
6121 reseat (it, it->current.pos, 1);
6122 CHECK_IT (it);
6126 /* Reseat iterator IT on the next visible line start in the current
6127 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6128 preceding the line start. Skip over invisible text that is so
6129 because of selective display. Compute faces, overlays etc at the
6130 new position. Note that this function does not skip over text that
6131 is invisible because of text properties. */
6133 static void
6134 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6136 int newline_found_p, skipped_p = 0;
6137 struct bidi_it bidi_it_prev;
6139 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6141 /* Skip over lines that are invisible because they are indented
6142 more than the value of IT->selective. */
6143 if (it->selective > 0)
6144 while (IT_CHARPOS (*it) < ZV
6145 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6146 it->selective))
6148 eassert (IT_BYTEPOS (*it) == BEGV
6149 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6150 newline_found_p =
6151 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6154 /* Position on the newline if that's what's requested. */
6155 if (on_newline_p && newline_found_p)
6157 if (STRINGP (it->string))
6159 if (IT_STRING_CHARPOS (*it) > 0)
6161 if (!it->bidi_p)
6163 --IT_STRING_CHARPOS (*it);
6164 --IT_STRING_BYTEPOS (*it);
6166 else
6168 /* We need to restore the bidi iterator to the state
6169 it had on the newline, and resync the IT's
6170 position with that. */
6171 it->bidi_it = bidi_it_prev;
6172 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6173 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6177 else if (IT_CHARPOS (*it) > BEGV)
6179 if (!it->bidi_p)
6181 --IT_CHARPOS (*it);
6182 --IT_BYTEPOS (*it);
6184 else
6186 /* We need to restore the bidi iterator to the state it
6187 had on the newline and resync IT with that. */
6188 it->bidi_it = bidi_it_prev;
6189 IT_CHARPOS (*it) = it->bidi_it.charpos;
6190 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6192 reseat (it, it->current.pos, 0);
6195 else if (skipped_p)
6196 reseat (it, it->current.pos, 0);
6198 CHECK_IT (it);
6203 /***********************************************************************
6204 Changing an iterator's position
6205 ***********************************************************************/
6207 /* Change IT's current position to POS in current_buffer. If FORCE_P
6208 is non-zero, always check for text properties at the new position.
6209 Otherwise, text properties are only looked up if POS >=
6210 IT->check_charpos of a property. */
6212 static void
6213 reseat (struct it *it, struct text_pos pos, int force_p)
6215 ptrdiff_t original_pos = IT_CHARPOS (*it);
6217 reseat_1 (it, pos, 0);
6219 /* Determine where to check text properties. Avoid doing it
6220 where possible because text property lookup is very expensive. */
6221 if (force_p
6222 || CHARPOS (pos) > it->stop_charpos
6223 || CHARPOS (pos) < original_pos)
6225 if (it->bidi_p)
6227 /* For bidi iteration, we need to prime prev_stop and
6228 base_level_stop with our best estimations. */
6229 /* Implementation note: Of course, POS is not necessarily a
6230 stop position, so assigning prev_pos to it is a lie; we
6231 should have called compute_stop_backwards. However, if
6232 the current buffer does not include any R2L characters,
6233 that call would be a waste of cycles, because the
6234 iterator will never move back, and thus never cross this
6235 "fake" stop position. So we delay that backward search
6236 until the time we really need it, in next_element_from_buffer. */
6237 if (CHARPOS (pos) != it->prev_stop)
6238 it->prev_stop = CHARPOS (pos);
6239 if (CHARPOS (pos) < it->base_level_stop)
6240 it->base_level_stop = 0; /* meaning it's unknown */
6241 handle_stop (it);
6243 else
6245 handle_stop (it);
6246 it->prev_stop = it->base_level_stop = 0;
6251 CHECK_IT (it);
6255 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6256 IT->stop_pos to POS, also. */
6258 static void
6259 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6261 /* Don't call this function when scanning a C string. */
6262 eassert (it->s == NULL);
6264 /* POS must be a reasonable value. */
6265 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6267 it->current.pos = it->position = pos;
6268 it->end_charpos = ZV;
6269 it->dpvec = NULL;
6270 it->current.dpvec_index = -1;
6271 it->current.overlay_string_index = -1;
6272 IT_STRING_CHARPOS (*it) = -1;
6273 IT_STRING_BYTEPOS (*it) = -1;
6274 it->string = Qnil;
6275 it->method = GET_FROM_BUFFER;
6276 it->object = it->w->buffer;
6277 it->area = TEXT_AREA;
6278 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6279 it->sp = 0;
6280 it->string_from_display_prop_p = 0;
6281 it->string_from_prefix_prop_p = 0;
6283 it->from_disp_prop_p = 0;
6284 it->face_before_selective_p = 0;
6285 if (it->bidi_p)
6287 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6288 &it->bidi_it);
6289 bidi_unshelve_cache (NULL, 0);
6290 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6291 it->bidi_it.string.s = NULL;
6292 it->bidi_it.string.lstring = Qnil;
6293 it->bidi_it.string.bufpos = 0;
6294 it->bidi_it.string.unibyte = 0;
6297 if (set_stop_p)
6299 it->stop_charpos = CHARPOS (pos);
6300 it->base_level_stop = CHARPOS (pos);
6305 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6306 If S is non-null, it is a C string to iterate over. Otherwise,
6307 STRING gives a Lisp string to iterate over.
6309 If PRECISION > 0, don't return more then PRECISION number of
6310 characters from the string.
6312 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6313 characters have been returned. FIELD_WIDTH < 0 means an infinite
6314 field width.
6316 MULTIBYTE = 0 means disable processing of multibyte characters,
6317 MULTIBYTE > 0 means enable it,
6318 MULTIBYTE < 0 means use IT->multibyte_p.
6320 IT must be initialized via a prior call to init_iterator before
6321 calling this function. */
6323 static void
6324 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6325 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6326 int multibyte)
6328 /* No region in strings. */
6329 it->region_beg_charpos = it->region_end_charpos = -1;
6331 /* No text property checks performed by default, but see below. */
6332 it->stop_charpos = -1;
6334 /* Set iterator position and end position. */
6335 memset (&it->current, 0, sizeof it->current);
6336 it->current.overlay_string_index = -1;
6337 it->current.dpvec_index = -1;
6338 eassert (charpos >= 0);
6340 /* If STRING is specified, use its multibyteness, otherwise use the
6341 setting of MULTIBYTE, if specified. */
6342 if (multibyte >= 0)
6343 it->multibyte_p = multibyte > 0;
6345 /* Bidirectional reordering of strings is controlled by the default
6346 value of bidi-display-reordering. Don't try to reorder while
6347 loading loadup.el, as the necessary character property tables are
6348 not yet available. */
6349 it->bidi_p =
6350 NILP (Vpurify_flag)
6351 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6353 if (s == NULL)
6355 eassert (STRINGP (string));
6356 it->string = string;
6357 it->s = NULL;
6358 it->end_charpos = it->string_nchars = SCHARS (string);
6359 it->method = GET_FROM_STRING;
6360 it->current.string_pos = string_pos (charpos, string);
6362 if (it->bidi_p)
6364 it->bidi_it.string.lstring = string;
6365 it->bidi_it.string.s = NULL;
6366 it->bidi_it.string.schars = it->end_charpos;
6367 it->bidi_it.string.bufpos = 0;
6368 it->bidi_it.string.from_disp_str = 0;
6369 it->bidi_it.string.unibyte = !it->multibyte_p;
6370 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6371 FRAME_WINDOW_P (it->f), &it->bidi_it);
6374 else
6376 it->s = (const unsigned char *) s;
6377 it->string = Qnil;
6379 /* Note that we use IT->current.pos, not it->current.string_pos,
6380 for displaying C strings. */
6381 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6382 if (it->multibyte_p)
6384 it->current.pos = c_string_pos (charpos, s, 1);
6385 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6387 else
6389 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6390 it->end_charpos = it->string_nchars = strlen (s);
6393 if (it->bidi_p)
6395 it->bidi_it.string.lstring = Qnil;
6396 it->bidi_it.string.s = (const unsigned char *) s;
6397 it->bidi_it.string.schars = it->end_charpos;
6398 it->bidi_it.string.bufpos = 0;
6399 it->bidi_it.string.from_disp_str = 0;
6400 it->bidi_it.string.unibyte = !it->multibyte_p;
6401 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6402 &it->bidi_it);
6404 it->method = GET_FROM_C_STRING;
6407 /* PRECISION > 0 means don't return more than PRECISION characters
6408 from the string. */
6409 if (precision > 0 && it->end_charpos - charpos > precision)
6411 it->end_charpos = it->string_nchars = charpos + precision;
6412 if (it->bidi_p)
6413 it->bidi_it.string.schars = it->end_charpos;
6416 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6417 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6418 FIELD_WIDTH < 0 means infinite field width. This is useful for
6419 padding with `-' at the end of a mode line. */
6420 if (field_width < 0)
6421 field_width = INFINITY;
6422 /* Implementation note: We deliberately don't enlarge
6423 it->bidi_it.string.schars here to fit it->end_charpos, because
6424 the bidi iterator cannot produce characters out of thin air. */
6425 if (field_width > it->end_charpos - charpos)
6426 it->end_charpos = charpos + field_width;
6428 /* Use the standard display table for displaying strings. */
6429 if (DISP_TABLE_P (Vstandard_display_table))
6430 it->dp = XCHAR_TABLE (Vstandard_display_table);
6432 it->stop_charpos = charpos;
6433 it->prev_stop = charpos;
6434 it->base_level_stop = 0;
6435 if (it->bidi_p)
6437 it->bidi_it.first_elt = 1;
6438 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6439 it->bidi_it.disp_pos = -1;
6441 if (s == NULL && it->multibyte_p)
6443 ptrdiff_t endpos = SCHARS (it->string);
6444 if (endpos > it->end_charpos)
6445 endpos = it->end_charpos;
6446 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6447 it->string);
6449 CHECK_IT (it);
6454 /***********************************************************************
6455 Iteration
6456 ***********************************************************************/
6458 /* Map enum it_method value to corresponding next_element_from_* function. */
6460 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6462 next_element_from_buffer,
6463 next_element_from_display_vector,
6464 next_element_from_string,
6465 next_element_from_c_string,
6466 next_element_from_image,
6467 next_element_from_stretch
6470 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6473 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6474 (possibly with the following characters). */
6476 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6477 ((IT)->cmp_it.id >= 0 \
6478 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6479 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6480 END_CHARPOS, (IT)->w, \
6481 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6482 (IT)->string)))
6485 /* Lookup the char-table Vglyphless_char_display for character C (-1
6486 if we want information for no-font case), and return the display
6487 method symbol. By side-effect, update it->what and
6488 it->glyphless_method. This function is called from
6489 get_next_display_element for each character element, and from
6490 x_produce_glyphs when no suitable font was found. */
6492 Lisp_Object
6493 lookup_glyphless_char_display (int c, struct it *it)
6495 Lisp_Object glyphless_method = Qnil;
6497 if (CHAR_TABLE_P (Vglyphless_char_display)
6498 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6500 if (c >= 0)
6502 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6503 if (CONSP (glyphless_method))
6504 glyphless_method = FRAME_WINDOW_P (it->f)
6505 ? XCAR (glyphless_method)
6506 : XCDR (glyphless_method);
6508 else
6509 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6512 retry:
6513 if (NILP (glyphless_method))
6515 if (c >= 0)
6516 /* The default is to display the character by a proper font. */
6517 return Qnil;
6518 /* The default for the no-font case is to display an empty box. */
6519 glyphless_method = Qempty_box;
6521 if (EQ (glyphless_method, Qzero_width))
6523 if (c >= 0)
6524 return glyphless_method;
6525 /* This method can't be used for the no-font case. */
6526 glyphless_method = Qempty_box;
6528 if (EQ (glyphless_method, Qthin_space))
6529 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6530 else if (EQ (glyphless_method, Qempty_box))
6531 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6532 else if (EQ (glyphless_method, Qhex_code))
6533 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6534 else if (STRINGP (glyphless_method))
6535 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6536 else
6538 /* Invalid value. We use the default method. */
6539 glyphless_method = Qnil;
6540 goto retry;
6542 it->what = IT_GLYPHLESS;
6543 return glyphless_method;
6546 /* Load IT's display element fields with information about the next
6547 display element from the current position of IT. Value is zero if
6548 end of buffer (or C string) is reached. */
6550 static struct frame *last_escape_glyph_frame = NULL;
6551 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6552 static int last_escape_glyph_merged_face_id = 0;
6554 struct frame *last_glyphless_glyph_frame = NULL;
6555 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6556 int last_glyphless_glyph_merged_face_id = 0;
6558 static int
6559 get_next_display_element (struct it *it)
6561 /* Non-zero means that we found a display element. Zero means that
6562 we hit the end of what we iterate over. Performance note: the
6563 function pointer `method' used here turns out to be faster than
6564 using a sequence of if-statements. */
6565 int success_p;
6567 get_next:
6568 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6570 if (it->what == IT_CHARACTER)
6572 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6573 and only if (a) the resolved directionality of that character
6574 is R..." */
6575 /* FIXME: Do we need an exception for characters from display
6576 tables? */
6577 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6578 it->c = bidi_mirror_char (it->c);
6579 /* Map via display table or translate control characters.
6580 IT->c, IT->len etc. have been set to the next character by
6581 the function call above. If we have a display table, and it
6582 contains an entry for IT->c, translate it. Don't do this if
6583 IT->c itself comes from a display table, otherwise we could
6584 end up in an infinite recursion. (An alternative could be to
6585 count the recursion depth of this function and signal an
6586 error when a certain maximum depth is reached.) Is it worth
6587 it? */
6588 if (success_p && it->dpvec == NULL)
6590 Lisp_Object dv;
6591 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6592 int nonascii_space_p = 0;
6593 int nonascii_hyphen_p = 0;
6594 int c = it->c; /* This is the character to display. */
6596 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6598 eassert (SINGLE_BYTE_CHAR_P (c));
6599 if (unibyte_display_via_language_environment)
6601 c = DECODE_CHAR (unibyte, c);
6602 if (c < 0)
6603 c = BYTE8_TO_CHAR (it->c);
6605 else
6606 c = BYTE8_TO_CHAR (it->c);
6609 if (it->dp
6610 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6611 VECTORP (dv)))
6613 struct Lisp_Vector *v = XVECTOR (dv);
6615 /* Return the first character from the display table
6616 entry, if not empty. If empty, don't display the
6617 current character. */
6618 if (v->header.size)
6620 it->dpvec_char_len = it->len;
6621 it->dpvec = v->contents;
6622 it->dpend = v->contents + v->header.size;
6623 it->current.dpvec_index = 0;
6624 it->dpvec_face_id = -1;
6625 it->saved_face_id = it->face_id;
6626 it->method = GET_FROM_DISPLAY_VECTOR;
6627 it->ellipsis_p = 0;
6629 else
6631 set_iterator_to_next (it, 0);
6633 goto get_next;
6636 if (! NILP (lookup_glyphless_char_display (c, it)))
6638 if (it->what == IT_GLYPHLESS)
6639 goto done;
6640 /* Don't display this character. */
6641 set_iterator_to_next (it, 0);
6642 goto get_next;
6645 /* If `nobreak-char-display' is non-nil, we display
6646 non-ASCII spaces and hyphens specially. */
6647 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6649 if (c == 0xA0)
6650 nonascii_space_p = 1;
6651 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6652 nonascii_hyphen_p = 1;
6655 /* Translate control characters into `\003' or `^C' form.
6656 Control characters coming from a display table entry are
6657 currently not translated because we use IT->dpvec to hold
6658 the translation. This could easily be changed but I
6659 don't believe that it is worth doing.
6661 The characters handled by `nobreak-char-display' must be
6662 translated too.
6664 Non-printable characters and raw-byte characters are also
6665 translated to octal form. */
6666 if (((c < ' ' || c == 127) /* ASCII control chars */
6667 ? (it->area != TEXT_AREA
6668 /* In mode line, treat \n, \t like other crl chars. */
6669 || (c != '\t'
6670 && it->glyph_row
6671 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6672 || (c != '\n' && c != '\t'))
6673 : (nonascii_space_p
6674 || nonascii_hyphen_p
6675 || CHAR_BYTE8_P (c)
6676 || ! CHAR_PRINTABLE_P (c))))
6678 /* C is a control character, non-ASCII space/hyphen,
6679 raw-byte, or a non-printable character which must be
6680 displayed either as '\003' or as `^C' where the '\\'
6681 and '^' can be defined in the display table. Fill
6682 IT->ctl_chars with glyphs for what we have to
6683 display. Then, set IT->dpvec to these glyphs. */
6684 Lisp_Object gc;
6685 int ctl_len;
6686 int face_id;
6687 int lface_id = 0;
6688 int escape_glyph;
6690 /* Handle control characters with ^. */
6692 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6694 int g;
6696 g = '^'; /* default glyph for Control */
6697 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6698 if (it->dp
6699 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6701 g = GLYPH_CODE_CHAR (gc);
6702 lface_id = GLYPH_CODE_FACE (gc);
6704 if (lface_id)
6706 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6708 else if (it->f == last_escape_glyph_frame
6709 && it->face_id == last_escape_glyph_face_id)
6711 face_id = last_escape_glyph_merged_face_id;
6713 else
6715 /* Merge the escape-glyph face into the current face. */
6716 face_id = merge_faces (it->f, Qescape_glyph, 0,
6717 it->face_id);
6718 last_escape_glyph_frame = it->f;
6719 last_escape_glyph_face_id = it->face_id;
6720 last_escape_glyph_merged_face_id = face_id;
6723 XSETINT (it->ctl_chars[0], g);
6724 XSETINT (it->ctl_chars[1], c ^ 0100);
6725 ctl_len = 2;
6726 goto display_control;
6729 /* Handle non-ascii space in the mode where it only gets
6730 highlighting. */
6732 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6734 /* Merge `nobreak-space' into the current face. */
6735 face_id = merge_faces (it->f, Qnobreak_space, 0,
6736 it->face_id);
6737 XSETINT (it->ctl_chars[0], ' ');
6738 ctl_len = 1;
6739 goto display_control;
6742 /* Handle sequences that start with the "escape glyph". */
6744 /* the default escape glyph is \. */
6745 escape_glyph = '\\';
6747 if (it->dp
6748 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6750 escape_glyph = GLYPH_CODE_CHAR (gc);
6751 lface_id = GLYPH_CODE_FACE (gc);
6753 if (lface_id)
6755 /* The display table specified a face.
6756 Merge it into face_id and also into escape_glyph. */
6757 face_id = merge_faces (it->f, Qt, lface_id,
6758 it->face_id);
6760 else if (it->f == last_escape_glyph_frame
6761 && it->face_id == last_escape_glyph_face_id)
6763 face_id = last_escape_glyph_merged_face_id;
6765 else
6767 /* Merge the escape-glyph face into the current face. */
6768 face_id = merge_faces (it->f, Qescape_glyph, 0,
6769 it->face_id);
6770 last_escape_glyph_frame = it->f;
6771 last_escape_glyph_face_id = it->face_id;
6772 last_escape_glyph_merged_face_id = face_id;
6775 /* Draw non-ASCII hyphen with just highlighting: */
6777 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6779 XSETINT (it->ctl_chars[0], '-');
6780 ctl_len = 1;
6781 goto display_control;
6784 /* Draw non-ASCII space/hyphen with escape glyph: */
6786 if (nonascii_space_p || nonascii_hyphen_p)
6788 XSETINT (it->ctl_chars[0], escape_glyph);
6789 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6790 ctl_len = 2;
6791 goto display_control;
6795 char str[10];
6796 int len, i;
6798 if (CHAR_BYTE8_P (c))
6799 /* Display \200 instead of \17777600. */
6800 c = CHAR_TO_BYTE8 (c);
6801 len = sprintf (str, "%03o", c);
6803 XSETINT (it->ctl_chars[0], escape_glyph);
6804 for (i = 0; i < len; i++)
6805 XSETINT (it->ctl_chars[i + 1], str[i]);
6806 ctl_len = len + 1;
6809 display_control:
6810 /* Set up IT->dpvec and return first character from it. */
6811 it->dpvec_char_len = it->len;
6812 it->dpvec = it->ctl_chars;
6813 it->dpend = it->dpvec + ctl_len;
6814 it->current.dpvec_index = 0;
6815 it->dpvec_face_id = face_id;
6816 it->saved_face_id = it->face_id;
6817 it->method = GET_FROM_DISPLAY_VECTOR;
6818 it->ellipsis_p = 0;
6819 goto get_next;
6821 it->char_to_display = c;
6823 else if (success_p)
6825 it->char_to_display = it->c;
6829 /* Adjust face id for a multibyte character. There are no multibyte
6830 character in unibyte text. */
6831 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6832 && it->multibyte_p
6833 && success_p
6834 && FRAME_WINDOW_P (it->f))
6836 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6838 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6840 /* Automatic composition with glyph-string. */
6841 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6843 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6845 else
6847 ptrdiff_t pos = (it->s ? -1
6848 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6849 : IT_CHARPOS (*it));
6850 int c;
6852 if (it->what == IT_CHARACTER)
6853 c = it->char_to_display;
6854 else
6856 struct composition *cmp = composition_table[it->cmp_it.id];
6857 int i;
6859 c = ' ';
6860 for (i = 0; i < cmp->glyph_len; i++)
6861 /* TAB in a composition means display glyphs with
6862 padding space on the left or right. */
6863 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6864 break;
6866 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6870 done:
6871 /* Is this character the last one of a run of characters with
6872 box? If yes, set IT->end_of_box_run_p to 1. */
6873 if (it->face_box_p
6874 && it->s == NULL)
6876 if (it->method == GET_FROM_STRING && it->sp)
6878 int face_id = underlying_face_id (it);
6879 struct face *face = FACE_FROM_ID (it->f, face_id);
6881 if (face)
6883 if (face->box == FACE_NO_BOX)
6885 /* If the box comes from face properties in a
6886 display string, check faces in that string. */
6887 int string_face_id = face_after_it_pos (it);
6888 it->end_of_box_run_p
6889 = (FACE_FROM_ID (it->f, string_face_id)->box
6890 == FACE_NO_BOX);
6892 /* Otherwise, the box comes from the underlying face.
6893 If this is the last string character displayed, check
6894 the next buffer location. */
6895 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6896 && (it->current.overlay_string_index
6897 == it->n_overlay_strings - 1))
6899 ptrdiff_t ignore;
6900 int next_face_id;
6901 struct text_pos pos = it->current.pos;
6902 INC_TEXT_POS (pos, it->multibyte_p);
6904 next_face_id = face_at_buffer_position
6905 (it->w, CHARPOS (pos), it->region_beg_charpos,
6906 it->region_end_charpos, &ignore,
6907 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6908 -1);
6909 it->end_of_box_run_p
6910 = (FACE_FROM_ID (it->f, next_face_id)->box
6911 == FACE_NO_BOX);
6915 else
6917 int face_id = face_after_it_pos (it);
6918 it->end_of_box_run_p
6919 = (face_id != it->face_id
6920 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6923 /* If we reached the end of the object we've been iterating (e.g., a
6924 display string or an overlay string), and there's something on
6925 IT->stack, proceed with what's on the stack. It doesn't make
6926 sense to return zero if there's unprocessed stuff on the stack,
6927 because otherwise that stuff will never be displayed. */
6928 if (!success_p && it->sp > 0)
6930 set_iterator_to_next (it, 0);
6931 success_p = get_next_display_element (it);
6934 /* Value is 0 if end of buffer or string reached. */
6935 return success_p;
6939 /* Move IT to the next display element.
6941 RESEAT_P non-zero means if called on a newline in buffer text,
6942 skip to the next visible line start.
6944 Functions get_next_display_element and set_iterator_to_next are
6945 separate because I find this arrangement easier to handle than a
6946 get_next_display_element function that also increments IT's
6947 position. The way it is we can first look at an iterator's current
6948 display element, decide whether it fits on a line, and if it does,
6949 increment the iterator position. The other way around we probably
6950 would either need a flag indicating whether the iterator has to be
6951 incremented the next time, or we would have to implement a
6952 decrement position function which would not be easy to write. */
6954 void
6955 set_iterator_to_next (struct it *it, int reseat_p)
6957 /* Reset flags indicating start and end of a sequence of characters
6958 with box. Reset them at the start of this function because
6959 moving the iterator to a new position might set them. */
6960 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6962 switch (it->method)
6964 case GET_FROM_BUFFER:
6965 /* The current display element of IT is a character from
6966 current_buffer. Advance in the buffer, and maybe skip over
6967 invisible lines that are so because of selective display. */
6968 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6969 reseat_at_next_visible_line_start (it, 0);
6970 else if (it->cmp_it.id >= 0)
6972 /* We are currently getting glyphs from a composition. */
6973 int i;
6975 if (! it->bidi_p)
6977 IT_CHARPOS (*it) += it->cmp_it.nchars;
6978 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6979 if (it->cmp_it.to < it->cmp_it.nglyphs)
6981 it->cmp_it.from = it->cmp_it.to;
6983 else
6985 it->cmp_it.id = -1;
6986 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6987 IT_BYTEPOS (*it),
6988 it->end_charpos, Qnil);
6991 else if (! it->cmp_it.reversed_p)
6993 /* Composition created while scanning forward. */
6994 /* Update IT's char/byte positions to point to the first
6995 character of the next grapheme cluster, or to the
6996 character visually after the current composition. */
6997 for (i = 0; i < it->cmp_it.nchars; i++)
6998 bidi_move_to_visually_next (&it->bidi_it);
6999 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7000 IT_CHARPOS (*it) = it->bidi_it.charpos;
7002 if (it->cmp_it.to < it->cmp_it.nglyphs)
7004 /* Proceed to the next grapheme cluster. */
7005 it->cmp_it.from = it->cmp_it.to;
7007 else
7009 /* No more grapheme clusters in this composition.
7010 Find the next stop position. */
7011 ptrdiff_t stop = it->end_charpos;
7012 if (it->bidi_it.scan_dir < 0)
7013 /* Now we are scanning backward and don't know
7014 where to stop. */
7015 stop = -1;
7016 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7017 IT_BYTEPOS (*it), stop, Qnil);
7020 else
7022 /* Composition created while scanning backward. */
7023 /* Update IT's char/byte positions to point to the last
7024 character of the previous grapheme cluster, or the
7025 character visually after the current composition. */
7026 for (i = 0; i < it->cmp_it.nchars; i++)
7027 bidi_move_to_visually_next (&it->bidi_it);
7028 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7029 IT_CHARPOS (*it) = it->bidi_it.charpos;
7030 if (it->cmp_it.from > 0)
7032 /* Proceed to the previous grapheme cluster. */
7033 it->cmp_it.to = it->cmp_it.from;
7035 else
7037 /* No more grapheme clusters in this composition.
7038 Find the next stop position. */
7039 ptrdiff_t stop = it->end_charpos;
7040 if (it->bidi_it.scan_dir < 0)
7041 /* Now we are scanning backward and don't know
7042 where to stop. */
7043 stop = -1;
7044 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7045 IT_BYTEPOS (*it), stop, Qnil);
7049 else
7051 eassert (it->len != 0);
7053 if (!it->bidi_p)
7055 IT_BYTEPOS (*it) += it->len;
7056 IT_CHARPOS (*it) += 1;
7058 else
7060 int prev_scan_dir = it->bidi_it.scan_dir;
7061 /* If this is a new paragraph, determine its base
7062 direction (a.k.a. its base embedding level). */
7063 if (it->bidi_it.new_paragraph)
7064 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7065 bidi_move_to_visually_next (&it->bidi_it);
7066 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7067 IT_CHARPOS (*it) = it->bidi_it.charpos;
7068 if (prev_scan_dir != it->bidi_it.scan_dir)
7070 /* As the scan direction was changed, we must
7071 re-compute the stop position for composition. */
7072 ptrdiff_t stop = it->end_charpos;
7073 if (it->bidi_it.scan_dir < 0)
7074 stop = -1;
7075 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7076 IT_BYTEPOS (*it), stop, Qnil);
7079 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7081 break;
7083 case GET_FROM_C_STRING:
7084 /* Current display element of IT is from a C string. */
7085 if (!it->bidi_p
7086 /* If the string position is beyond string's end, it means
7087 next_element_from_c_string is padding the string with
7088 blanks, in which case we bypass the bidi iterator,
7089 because it cannot deal with such virtual characters. */
7090 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7092 IT_BYTEPOS (*it) += it->len;
7093 IT_CHARPOS (*it) += 1;
7095 else
7097 bidi_move_to_visually_next (&it->bidi_it);
7098 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7099 IT_CHARPOS (*it) = it->bidi_it.charpos;
7101 break;
7103 case GET_FROM_DISPLAY_VECTOR:
7104 /* Current display element of IT is from a display table entry.
7105 Advance in the display table definition. Reset it to null if
7106 end reached, and continue with characters from buffers/
7107 strings. */
7108 ++it->current.dpvec_index;
7110 /* Restore face of the iterator to what they were before the
7111 display vector entry (these entries may contain faces). */
7112 it->face_id = it->saved_face_id;
7114 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7116 int recheck_faces = it->ellipsis_p;
7118 if (it->s)
7119 it->method = GET_FROM_C_STRING;
7120 else if (STRINGP (it->string))
7121 it->method = GET_FROM_STRING;
7122 else
7124 it->method = GET_FROM_BUFFER;
7125 it->object = it->w->buffer;
7128 it->dpvec = NULL;
7129 it->current.dpvec_index = -1;
7131 /* Skip over characters which were displayed via IT->dpvec. */
7132 if (it->dpvec_char_len < 0)
7133 reseat_at_next_visible_line_start (it, 1);
7134 else if (it->dpvec_char_len > 0)
7136 if (it->method == GET_FROM_STRING
7137 && it->n_overlay_strings > 0)
7138 it->ignore_overlay_strings_at_pos_p = 1;
7139 it->len = it->dpvec_char_len;
7140 set_iterator_to_next (it, reseat_p);
7143 /* Maybe recheck faces after display vector */
7144 if (recheck_faces)
7145 it->stop_charpos = IT_CHARPOS (*it);
7147 break;
7149 case GET_FROM_STRING:
7150 /* Current display element is a character from a Lisp string. */
7151 eassert (it->s == NULL && STRINGP (it->string));
7152 /* Don't advance past string end. These conditions are true
7153 when set_iterator_to_next is called at the end of
7154 get_next_display_element, in which case the Lisp string is
7155 already exhausted, and all we want is pop the iterator
7156 stack. */
7157 if (it->current.overlay_string_index >= 0)
7159 /* This is an overlay string, so there's no padding with
7160 spaces, and the number of characters in the string is
7161 where the string ends. */
7162 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7163 goto consider_string_end;
7165 else
7167 /* Not an overlay string. There could be padding, so test
7168 against it->end_charpos . */
7169 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7170 goto consider_string_end;
7172 if (it->cmp_it.id >= 0)
7174 int i;
7176 if (! it->bidi_p)
7178 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7179 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7180 if (it->cmp_it.to < it->cmp_it.nglyphs)
7181 it->cmp_it.from = it->cmp_it.to;
7182 else
7184 it->cmp_it.id = -1;
7185 composition_compute_stop_pos (&it->cmp_it,
7186 IT_STRING_CHARPOS (*it),
7187 IT_STRING_BYTEPOS (*it),
7188 it->end_charpos, it->string);
7191 else if (! it->cmp_it.reversed_p)
7193 for (i = 0; i < it->cmp_it.nchars; i++)
7194 bidi_move_to_visually_next (&it->bidi_it);
7195 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7196 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7198 if (it->cmp_it.to < it->cmp_it.nglyphs)
7199 it->cmp_it.from = it->cmp_it.to;
7200 else
7202 ptrdiff_t stop = it->end_charpos;
7203 if (it->bidi_it.scan_dir < 0)
7204 stop = -1;
7205 composition_compute_stop_pos (&it->cmp_it,
7206 IT_STRING_CHARPOS (*it),
7207 IT_STRING_BYTEPOS (*it), stop,
7208 it->string);
7211 else
7213 for (i = 0; i < it->cmp_it.nchars; i++)
7214 bidi_move_to_visually_next (&it->bidi_it);
7215 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7216 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7217 if (it->cmp_it.from > 0)
7218 it->cmp_it.to = it->cmp_it.from;
7219 else
7221 ptrdiff_t stop = it->end_charpos;
7222 if (it->bidi_it.scan_dir < 0)
7223 stop = -1;
7224 composition_compute_stop_pos (&it->cmp_it,
7225 IT_STRING_CHARPOS (*it),
7226 IT_STRING_BYTEPOS (*it), stop,
7227 it->string);
7231 else
7233 if (!it->bidi_p
7234 /* If the string position is beyond string's end, it
7235 means next_element_from_string is padding the string
7236 with blanks, in which case we bypass the bidi
7237 iterator, because it cannot deal with such virtual
7238 characters. */
7239 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7241 IT_STRING_BYTEPOS (*it) += it->len;
7242 IT_STRING_CHARPOS (*it) += 1;
7244 else
7246 int prev_scan_dir = it->bidi_it.scan_dir;
7248 bidi_move_to_visually_next (&it->bidi_it);
7249 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7250 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7251 if (prev_scan_dir != it->bidi_it.scan_dir)
7253 ptrdiff_t stop = it->end_charpos;
7255 if (it->bidi_it.scan_dir < 0)
7256 stop = -1;
7257 composition_compute_stop_pos (&it->cmp_it,
7258 IT_STRING_CHARPOS (*it),
7259 IT_STRING_BYTEPOS (*it), stop,
7260 it->string);
7265 consider_string_end:
7267 if (it->current.overlay_string_index >= 0)
7269 /* IT->string is an overlay string. Advance to the
7270 next, if there is one. */
7271 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7273 it->ellipsis_p = 0;
7274 next_overlay_string (it);
7275 if (it->ellipsis_p)
7276 setup_for_ellipsis (it, 0);
7279 else
7281 /* IT->string is not an overlay string. If we reached
7282 its end, and there is something on IT->stack, proceed
7283 with what is on the stack. This can be either another
7284 string, this time an overlay string, or a buffer. */
7285 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7286 && it->sp > 0)
7288 pop_it (it);
7289 if (it->method == GET_FROM_STRING)
7290 goto consider_string_end;
7293 break;
7295 case GET_FROM_IMAGE:
7296 case GET_FROM_STRETCH:
7297 /* The position etc with which we have to proceed are on
7298 the stack. The position may be at the end of a string,
7299 if the `display' property takes up the whole string. */
7300 eassert (it->sp > 0);
7301 pop_it (it);
7302 if (it->method == GET_FROM_STRING)
7303 goto consider_string_end;
7304 break;
7306 default:
7307 /* There are no other methods defined, so this should be a bug. */
7308 emacs_abort ();
7311 eassert (it->method != GET_FROM_STRING
7312 || (STRINGP (it->string)
7313 && IT_STRING_CHARPOS (*it) >= 0));
7316 /* Load IT's display element fields with information about the next
7317 display element which comes from a display table entry or from the
7318 result of translating a control character to one of the forms `^C'
7319 or `\003'.
7321 IT->dpvec holds the glyphs to return as characters.
7322 IT->saved_face_id holds the face id before the display vector--it
7323 is restored into IT->face_id in set_iterator_to_next. */
7325 static int
7326 next_element_from_display_vector (struct it *it)
7328 Lisp_Object gc;
7330 /* Precondition. */
7331 eassert (it->dpvec && it->current.dpvec_index >= 0);
7333 it->face_id = it->saved_face_id;
7335 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7336 That seemed totally bogus - so I changed it... */
7337 gc = it->dpvec[it->current.dpvec_index];
7339 if (GLYPH_CODE_P (gc))
7341 it->c = GLYPH_CODE_CHAR (gc);
7342 it->len = CHAR_BYTES (it->c);
7344 /* The entry may contain a face id to use. Such a face id is
7345 the id of a Lisp face, not a realized face. A face id of
7346 zero means no face is specified. */
7347 if (it->dpvec_face_id >= 0)
7348 it->face_id = it->dpvec_face_id;
7349 else
7351 int lface_id = GLYPH_CODE_FACE (gc);
7352 if (lface_id > 0)
7353 it->face_id = merge_faces (it->f, Qt, lface_id,
7354 it->saved_face_id);
7357 else
7358 /* Display table entry is invalid. Return a space. */
7359 it->c = ' ', it->len = 1;
7361 /* Don't change position and object of the iterator here. They are
7362 still the values of the character that had this display table
7363 entry or was translated, and that's what we want. */
7364 it->what = IT_CHARACTER;
7365 return 1;
7368 /* Get the first element of string/buffer in the visual order, after
7369 being reseated to a new position in a string or a buffer. */
7370 static void
7371 get_visually_first_element (struct it *it)
7373 int string_p = STRINGP (it->string) || it->s;
7374 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7375 ptrdiff_t bob = (string_p ? 0 : BEGV);
7377 if (STRINGP (it->string))
7379 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7380 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7382 else
7384 it->bidi_it.charpos = IT_CHARPOS (*it);
7385 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7388 if (it->bidi_it.charpos == eob)
7390 /* Nothing to do, but reset the FIRST_ELT flag, like
7391 bidi_paragraph_init does, because we are not going to
7392 call it. */
7393 it->bidi_it.first_elt = 0;
7395 else if (it->bidi_it.charpos == bob
7396 || (!string_p
7397 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7398 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7400 /* If we are at the beginning of a line/string, we can produce
7401 the next element right away. */
7402 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7403 bidi_move_to_visually_next (&it->bidi_it);
7405 else
7407 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7409 /* We need to prime the bidi iterator starting at the line's or
7410 string's beginning, before we will be able to produce the
7411 next element. */
7412 if (string_p)
7413 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7414 else
7416 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7417 -1);
7418 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7420 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7423 /* Now return to buffer/string position where we were asked
7424 to get the next display element, and produce that. */
7425 bidi_move_to_visually_next (&it->bidi_it);
7427 while (it->bidi_it.bytepos != orig_bytepos
7428 && it->bidi_it.charpos < eob);
7431 /* Adjust IT's position information to where we ended up. */
7432 if (STRINGP (it->string))
7434 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7435 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7437 else
7439 IT_CHARPOS (*it) = it->bidi_it.charpos;
7440 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7443 if (STRINGP (it->string) || !it->s)
7445 ptrdiff_t stop, charpos, bytepos;
7447 if (STRINGP (it->string))
7449 eassert (!it->s);
7450 stop = SCHARS (it->string);
7451 if (stop > it->end_charpos)
7452 stop = it->end_charpos;
7453 charpos = IT_STRING_CHARPOS (*it);
7454 bytepos = IT_STRING_BYTEPOS (*it);
7456 else
7458 stop = it->end_charpos;
7459 charpos = IT_CHARPOS (*it);
7460 bytepos = IT_BYTEPOS (*it);
7462 if (it->bidi_it.scan_dir < 0)
7463 stop = -1;
7464 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7465 it->string);
7469 /* Load IT with the next display element from Lisp string IT->string.
7470 IT->current.string_pos is the current position within the string.
7471 If IT->current.overlay_string_index >= 0, the Lisp string is an
7472 overlay string. */
7474 static int
7475 next_element_from_string (struct it *it)
7477 struct text_pos position;
7479 eassert (STRINGP (it->string));
7480 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7481 eassert (IT_STRING_CHARPOS (*it) >= 0);
7482 position = it->current.string_pos;
7484 /* With bidi reordering, the character to display might not be the
7485 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7486 that we were reseat()ed to a new string, whose paragraph
7487 direction is not known. */
7488 if (it->bidi_p && it->bidi_it.first_elt)
7490 get_visually_first_element (it);
7491 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7494 /* Time to check for invisible text? */
7495 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7497 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7499 if (!(!it->bidi_p
7500 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7501 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7503 /* With bidi non-linear iteration, we could find
7504 ourselves far beyond the last computed stop_charpos,
7505 with several other stop positions in between that we
7506 missed. Scan them all now, in buffer's logical
7507 order, until we find and handle the last stop_charpos
7508 that precedes our current position. */
7509 handle_stop_backwards (it, it->stop_charpos);
7510 return GET_NEXT_DISPLAY_ELEMENT (it);
7512 else
7514 if (it->bidi_p)
7516 /* Take note of the stop position we just moved
7517 across, for when we will move back across it. */
7518 it->prev_stop = it->stop_charpos;
7519 /* If we are at base paragraph embedding level, take
7520 note of the last stop position seen at this
7521 level. */
7522 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7523 it->base_level_stop = it->stop_charpos;
7525 handle_stop (it);
7527 /* Since a handler may have changed IT->method, we must
7528 recurse here. */
7529 return GET_NEXT_DISPLAY_ELEMENT (it);
7532 else if (it->bidi_p
7533 /* If we are before prev_stop, we may have overstepped
7534 on our way backwards a stop_pos, and if so, we need
7535 to handle that stop_pos. */
7536 && IT_STRING_CHARPOS (*it) < it->prev_stop
7537 /* We can sometimes back up for reasons that have nothing
7538 to do with bidi reordering. E.g., compositions. The
7539 code below is only needed when we are above the base
7540 embedding level, so test for that explicitly. */
7541 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7543 /* If we lost track of base_level_stop, we have no better
7544 place for handle_stop_backwards to start from than string
7545 beginning. This happens, e.g., when we were reseated to
7546 the previous screenful of text by vertical-motion. */
7547 if (it->base_level_stop <= 0
7548 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7549 it->base_level_stop = 0;
7550 handle_stop_backwards (it, it->base_level_stop);
7551 return GET_NEXT_DISPLAY_ELEMENT (it);
7555 if (it->current.overlay_string_index >= 0)
7557 /* Get the next character from an overlay string. In overlay
7558 strings, there is no field width or padding with spaces to
7559 do. */
7560 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7562 it->what = IT_EOB;
7563 return 0;
7565 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7566 IT_STRING_BYTEPOS (*it),
7567 it->bidi_it.scan_dir < 0
7568 ? -1
7569 : SCHARS (it->string))
7570 && next_element_from_composition (it))
7572 return 1;
7574 else if (STRING_MULTIBYTE (it->string))
7576 const unsigned char *s = (SDATA (it->string)
7577 + IT_STRING_BYTEPOS (*it));
7578 it->c = string_char_and_length (s, &it->len);
7580 else
7582 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7583 it->len = 1;
7586 else
7588 /* Get the next character from a Lisp string that is not an
7589 overlay string. Such strings come from the mode line, for
7590 example. We may have to pad with spaces, or truncate the
7591 string. See also next_element_from_c_string. */
7592 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7594 it->what = IT_EOB;
7595 return 0;
7597 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7599 /* Pad with spaces. */
7600 it->c = ' ', it->len = 1;
7601 CHARPOS (position) = BYTEPOS (position) = -1;
7603 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7604 IT_STRING_BYTEPOS (*it),
7605 it->bidi_it.scan_dir < 0
7606 ? -1
7607 : it->string_nchars)
7608 && next_element_from_composition (it))
7610 return 1;
7612 else if (STRING_MULTIBYTE (it->string))
7614 const unsigned char *s = (SDATA (it->string)
7615 + IT_STRING_BYTEPOS (*it));
7616 it->c = string_char_and_length (s, &it->len);
7618 else
7620 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7621 it->len = 1;
7625 /* Record what we have and where it came from. */
7626 it->what = IT_CHARACTER;
7627 it->object = it->string;
7628 it->position = position;
7629 return 1;
7633 /* Load IT with next display element from C string IT->s.
7634 IT->string_nchars is the maximum number of characters to return
7635 from the string. IT->end_charpos may be greater than
7636 IT->string_nchars when this function is called, in which case we
7637 may have to return padding spaces. Value is zero if end of string
7638 reached, including padding spaces. */
7640 static int
7641 next_element_from_c_string (struct it *it)
7643 int success_p = 1;
7645 eassert (it->s);
7646 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7647 it->what = IT_CHARACTER;
7648 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7649 it->object = Qnil;
7651 /* With bidi reordering, the character to display might not be the
7652 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7653 we were reseated to a new string, whose paragraph direction is
7654 not known. */
7655 if (it->bidi_p && it->bidi_it.first_elt)
7656 get_visually_first_element (it);
7658 /* IT's position can be greater than IT->string_nchars in case a
7659 field width or precision has been specified when the iterator was
7660 initialized. */
7661 if (IT_CHARPOS (*it) >= it->end_charpos)
7663 /* End of the game. */
7664 it->what = IT_EOB;
7665 success_p = 0;
7667 else if (IT_CHARPOS (*it) >= it->string_nchars)
7669 /* Pad with spaces. */
7670 it->c = ' ', it->len = 1;
7671 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7673 else if (it->multibyte_p)
7674 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7675 else
7676 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7678 return success_p;
7682 /* Set up IT to return characters from an ellipsis, if appropriate.
7683 The definition of the ellipsis glyphs may come from a display table
7684 entry. This function fills IT with the first glyph from the
7685 ellipsis if an ellipsis is to be displayed. */
7687 static int
7688 next_element_from_ellipsis (struct it *it)
7690 if (it->selective_display_ellipsis_p)
7691 setup_for_ellipsis (it, it->len);
7692 else
7694 /* The face at the current position may be different from the
7695 face we find after the invisible text. Remember what it
7696 was in IT->saved_face_id, and signal that it's there by
7697 setting face_before_selective_p. */
7698 it->saved_face_id = it->face_id;
7699 it->method = GET_FROM_BUFFER;
7700 it->object = it->w->buffer;
7701 reseat_at_next_visible_line_start (it, 1);
7702 it->face_before_selective_p = 1;
7705 return GET_NEXT_DISPLAY_ELEMENT (it);
7709 /* Deliver an image display element. The iterator IT is already
7710 filled with image information (done in handle_display_prop). Value
7711 is always 1. */
7714 static int
7715 next_element_from_image (struct it *it)
7717 it->what = IT_IMAGE;
7718 it->ignore_overlay_strings_at_pos_p = 0;
7719 return 1;
7723 /* Fill iterator IT with next display element from a stretch glyph
7724 property. IT->object is the value of the text property. Value is
7725 always 1. */
7727 static int
7728 next_element_from_stretch (struct it *it)
7730 it->what = IT_STRETCH;
7731 return 1;
7734 /* Scan backwards from IT's current position until we find a stop
7735 position, or until BEGV. This is called when we find ourself
7736 before both the last known prev_stop and base_level_stop while
7737 reordering bidirectional text. */
7739 static void
7740 compute_stop_pos_backwards (struct it *it)
7742 const int SCAN_BACK_LIMIT = 1000;
7743 struct text_pos pos;
7744 struct display_pos save_current = it->current;
7745 struct text_pos save_position = it->position;
7746 ptrdiff_t charpos = IT_CHARPOS (*it);
7747 ptrdiff_t where_we_are = charpos;
7748 ptrdiff_t save_stop_pos = it->stop_charpos;
7749 ptrdiff_t save_end_pos = it->end_charpos;
7751 eassert (NILP (it->string) && !it->s);
7752 eassert (it->bidi_p);
7753 it->bidi_p = 0;
7756 it->end_charpos = min (charpos + 1, ZV);
7757 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7758 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7759 reseat_1 (it, pos, 0);
7760 compute_stop_pos (it);
7761 /* We must advance forward, right? */
7762 if (it->stop_charpos <= charpos)
7763 emacs_abort ();
7765 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7767 if (it->stop_charpos <= where_we_are)
7768 it->prev_stop = it->stop_charpos;
7769 else
7770 it->prev_stop = BEGV;
7771 it->bidi_p = 1;
7772 it->current = save_current;
7773 it->position = save_position;
7774 it->stop_charpos = save_stop_pos;
7775 it->end_charpos = save_end_pos;
7778 /* Scan forward from CHARPOS in the current buffer/string, until we
7779 find a stop position > current IT's position. Then handle the stop
7780 position before that. This is called when we bump into a stop
7781 position while reordering bidirectional text. CHARPOS should be
7782 the last previously processed stop_pos (or BEGV/0, if none were
7783 processed yet) whose position is less that IT's current
7784 position. */
7786 static void
7787 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7789 int bufp = !STRINGP (it->string);
7790 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7791 struct display_pos save_current = it->current;
7792 struct text_pos save_position = it->position;
7793 struct text_pos pos1;
7794 ptrdiff_t next_stop;
7796 /* Scan in strict logical order. */
7797 eassert (it->bidi_p);
7798 it->bidi_p = 0;
7801 it->prev_stop = charpos;
7802 if (bufp)
7804 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7805 reseat_1 (it, pos1, 0);
7807 else
7808 it->current.string_pos = string_pos (charpos, it->string);
7809 compute_stop_pos (it);
7810 /* We must advance forward, right? */
7811 if (it->stop_charpos <= it->prev_stop)
7812 emacs_abort ();
7813 charpos = it->stop_charpos;
7815 while (charpos <= where_we_are);
7817 it->bidi_p = 1;
7818 it->current = save_current;
7819 it->position = save_position;
7820 next_stop = it->stop_charpos;
7821 it->stop_charpos = it->prev_stop;
7822 handle_stop (it);
7823 it->stop_charpos = next_stop;
7826 /* Load IT with the next display element from current_buffer. Value
7827 is zero if end of buffer reached. IT->stop_charpos is the next
7828 position at which to stop and check for text properties or buffer
7829 end. */
7831 static int
7832 next_element_from_buffer (struct it *it)
7834 int success_p = 1;
7836 eassert (IT_CHARPOS (*it) >= BEGV);
7837 eassert (NILP (it->string) && !it->s);
7838 eassert (!it->bidi_p
7839 || (EQ (it->bidi_it.string.lstring, Qnil)
7840 && it->bidi_it.string.s == NULL));
7842 /* With bidi reordering, the character to display might not be the
7843 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7844 we were reseat()ed to a new buffer position, which is potentially
7845 a different paragraph. */
7846 if (it->bidi_p && it->bidi_it.first_elt)
7848 get_visually_first_element (it);
7849 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7852 if (IT_CHARPOS (*it) >= it->stop_charpos)
7854 if (IT_CHARPOS (*it) >= it->end_charpos)
7856 int overlay_strings_follow_p;
7858 /* End of the game, except when overlay strings follow that
7859 haven't been returned yet. */
7860 if (it->overlay_strings_at_end_processed_p)
7861 overlay_strings_follow_p = 0;
7862 else
7864 it->overlay_strings_at_end_processed_p = 1;
7865 overlay_strings_follow_p = get_overlay_strings (it, 0);
7868 if (overlay_strings_follow_p)
7869 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7870 else
7872 it->what = IT_EOB;
7873 it->position = it->current.pos;
7874 success_p = 0;
7877 else if (!(!it->bidi_p
7878 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7879 || IT_CHARPOS (*it) == it->stop_charpos))
7881 /* With bidi non-linear iteration, we could find ourselves
7882 far beyond the last computed stop_charpos, with several
7883 other stop positions in between that we missed. Scan
7884 them all now, in buffer's logical order, until we find
7885 and handle the last stop_charpos that precedes our
7886 current position. */
7887 handle_stop_backwards (it, it->stop_charpos);
7888 return GET_NEXT_DISPLAY_ELEMENT (it);
7890 else
7892 if (it->bidi_p)
7894 /* Take note of the stop position we just moved across,
7895 for when we will move back across it. */
7896 it->prev_stop = it->stop_charpos;
7897 /* If we are at base paragraph embedding level, take
7898 note of the last stop position seen at this
7899 level. */
7900 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7901 it->base_level_stop = it->stop_charpos;
7903 handle_stop (it);
7904 return GET_NEXT_DISPLAY_ELEMENT (it);
7907 else if (it->bidi_p
7908 /* If we are before prev_stop, we may have overstepped on
7909 our way backwards a stop_pos, and if so, we need to
7910 handle that stop_pos. */
7911 && IT_CHARPOS (*it) < it->prev_stop
7912 /* We can sometimes back up for reasons that have nothing
7913 to do with bidi reordering. E.g., compositions. The
7914 code below is only needed when we are above the base
7915 embedding level, so test for that explicitly. */
7916 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7918 if (it->base_level_stop <= 0
7919 || IT_CHARPOS (*it) < it->base_level_stop)
7921 /* If we lost track of base_level_stop, we need to find
7922 prev_stop by looking backwards. This happens, e.g., when
7923 we were reseated to the previous screenful of text by
7924 vertical-motion. */
7925 it->base_level_stop = BEGV;
7926 compute_stop_pos_backwards (it);
7927 handle_stop_backwards (it, it->prev_stop);
7929 else
7930 handle_stop_backwards (it, it->base_level_stop);
7931 return GET_NEXT_DISPLAY_ELEMENT (it);
7933 else
7935 /* No face changes, overlays etc. in sight, so just return a
7936 character from current_buffer. */
7937 unsigned char *p;
7938 ptrdiff_t stop;
7940 /* Maybe run the redisplay end trigger hook. Performance note:
7941 This doesn't seem to cost measurable time. */
7942 if (it->redisplay_end_trigger_charpos
7943 && it->glyph_row
7944 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7945 run_redisplay_end_trigger_hook (it);
7947 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7948 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7949 stop)
7950 && next_element_from_composition (it))
7952 return 1;
7955 /* Get the next character, maybe multibyte. */
7956 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7957 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7958 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7959 else
7960 it->c = *p, it->len = 1;
7962 /* Record what we have and where it came from. */
7963 it->what = IT_CHARACTER;
7964 it->object = it->w->buffer;
7965 it->position = it->current.pos;
7967 /* Normally we return the character found above, except when we
7968 really want to return an ellipsis for selective display. */
7969 if (it->selective)
7971 if (it->c == '\n')
7973 /* A value of selective > 0 means hide lines indented more
7974 than that number of columns. */
7975 if (it->selective > 0
7976 && IT_CHARPOS (*it) + 1 < ZV
7977 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7978 IT_BYTEPOS (*it) + 1,
7979 it->selective))
7981 success_p = next_element_from_ellipsis (it);
7982 it->dpvec_char_len = -1;
7985 else if (it->c == '\r' && it->selective == -1)
7987 /* A value of selective == -1 means that everything from the
7988 CR to the end of the line is invisible, with maybe an
7989 ellipsis displayed for it. */
7990 success_p = next_element_from_ellipsis (it);
7991 it->dpvec_char_len = -1;
7996 /* Value is zero if end of buffer reached. */
7997 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7998 return success_p;
8002 /* Run the redisplay end trigger hook for IT. */
8004 static void
8005 run_redisplay_end_trigger_hook (struct it *it)
8007 Lisp_Object args[3];
8009 /* IT->glyph_row should be non-null, i.e. we should be actually
8010 displaying something, or otherwise we should not run the hook. */
8011 eassert (it->glyph_row);
8013 /* Set up hook arguments. */
8014 args[0] = Qredisplay_end_trigger_functions;
8015 args[1] = it->window;
8016 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8017 it->redisplay_end_trigger_charpos = 0;
8019 /* Since we are *trying* to run these functions, don't try to run
8020 them again, even if they get an error. */
8021 wset_redisplay_end_trigger (it->w, Qnil);
8022 Frun_hook_with_args (3, args);
8024 /* Notice if it changed the face of the character we are on. */
8025 handle_face_prop (it);
8029 /* Deliver a composition display element. Unlike the other
8030 next_element_from_XXX, this function is not registered in the array
8031 get_next_element[]. It is called from next_element_from_buffer and
8032 next_element_from_string when necessary. */
8034 static int
8035 next_element_from_composition (struct it *it)
8037 it->what = IT_COMPOSITION;
8038 it->len = it->cmp_it.nbytes;
8039 if (STRINGP (it->string))
8041 if (it->c < 0)
8043 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8044 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8045 return 0;
8047 it->position = it->current.string_pos;
8048 it->object = it->string;
8049 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8050 IT_STRING_BYTEPOS (*it), it->string);
8052 else
8054 if (it->c < 0)
8056 IT_CHARPOS (*it) += it->cmp_it.nchars;
8057 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8058 if (it->bidi_p)
8060 if (it->bidi_it.new_paragraph)
8061 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8062 /* Resync the bidi iterator with IT's new position.
8063 FIXME: this doesn't support bidirectional text. */
8064 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8065 bidi_move_to_visually_next (&it->bidi_it);
8067 return 0;
8069 it->position = it->current.pos;
8070 it->object = it->w->buffer;
8071 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8072 IT_BYTEPOS (*it), Qnil);
8074 return 1;
8079 /***********************************************************************
8080 Moving an iterator without producing glyphs
8081 ***********************************************************************/
8083 /* Check if iterator is at a position corresponding to a valid buffer
8084 position after some move_it_ call. */
8086 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8087 ((it)->method == GET_FROM_STRING \
8088 ? IT_STRING_CHARPOS (*it) == 0 \
8089 : 1)
8092 /* Move iterator IT to a specified buffer or X position within one
8093 line on the display without producing glyphs.
8095 OP should be a bit mask including some or all of these bits:
8096 MOVE_TO_X: Stop upon reaching x-position TO_X.
8097 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8098 Regardless of OP's value, stop upon reaching the end of the display line.
8100 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8101 This means, in particular, that TO_X includes window's horizontal
8102 scroll amount.
8104 The return value has several possible values that
8105 say what condition caused the scan to stop:
8107 MOVE_POS_MATCH_OR_ZV
8108 - when TO_POS or ZV was reached.
8110 MOVE_X_REACHED
8111 -when TO_X was reached before TO_POS or ZV were reached.
8113 MOVE_LINE_CONTINUED
8114 - when we reached the end of the display area and the line must
8115 be continued.
8117 MOVE_LINE_TRUNCATED
8118 - when we reached the end of the display area and the line is
8119 truncated.
8121 MOVE_NEWLINE_OR_CR
8122 - when we stopped at a line end, i.e. a newline or a CR and selective
8123 display is on. */
8125 static enum move_it_result
8126 move_it_in_display_line_to (struct it *it,
8127 ptrdiff_t to_charpos, int to_x,
8128 enum move_operation_enum op)
8130 enum move_it_result result = MOVE_UNDEFINED;
8131 struct glyph_row *saved_glyph_row;
8132 struct it wrap_it, atpos_it, atx_it, ppos_it;
8133 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8134 void *ppos_data = NULL;
8135 int may_wrap = 0;
8136 enum it_method prev_method = it->method;
8137 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8138 int saw_smaller_pos = prev_pos < to_charpos;
8140 /* Don't produce glyphs in produce_glyphs. */
8141 saved_glyph_row = it->glyph_row;
8142 it->glyph_row = NULL;
8144 /* Use wrap_it to save a copy of IT wherever a word wrap could
8145 occur. Use atpos_it to save a copy of IT at the desired buffer
8146 position, if found, so that we can scan ahead and check if the
8147 word later overshoots the window edge. Use atx_it similarly, for
8148 pixel positions. */
8149 wrap_it.sp = -1;
8150 atpos_it.sp = -1;
8151 atx_it.sp = -1;
8153 /* Use ppos_it under bidi reordering to save a copy of IT for the
8154 position > CHARPOS that is the closest to CHARPOS. We restore
8155 that position in IT when we have scanned the entire display line
8156 without finding a match for CHARPOS and all the character
8157 positions are greater than CHARPOS. */
8158 if (it->bidi_p)
8160 SAVE_IT (ppos_it, *it, ppos_data);
8161 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8162 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8163 SAVE_IT (ppos_it, *it, ppos_data);
8166 #define BUFFER_POS_REACHED_P() \
8167 ((op & MOVE_TO_POS) != 0 \
8168 && BUFFERP (it->object) \
8169 && (IT_CHARPOS (*it) == to_charpos \
8170 || ((!it->bidi_p \
8171 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8172 && IT_CHARPOS (*it) > to_charpos) \
8173 || (it->what == IT_COMPOSITION \
8174 && ((IT_CHARPOS (*it) > to_charpos \
8175 && to_charpos >= it->cmp_it.charpos) \
8176 || (IT_CHARPOS (*it) < to_charpos \
8177 && to_charpos <= it->cmp_it.charpos)))) \
8178 && (it->method == GET_FROM_BUFFER \
8179 || (it->method == GET_FROM_DISPLAY_VECTOR \
8180 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8182 /* If there's a line-/wrap-prefix, handle it. */
8183 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8184 && it->current_y < it->last_visible_y)
8185 handle_line_prefix (it);
8187 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8188 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8190 while (1)
8192 int x, i, ascent = 0, descent = 0;
8194 /* Utility macro to reset an iterator with x, ascent, and descent. */
8195 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8196 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8197 (IT)->max_descent = descent)
8199 /* Stop if we move beyond TO_CHARPOS (after an image or a
8200 display string or stretch glyph). */
8201 if ((op & MOVE_TO_POS) != 0
8202 && BUFFERP (it->object)
8203 && it->method == GET_FROM_BUFFER
8204 && (((!it->bidi_p
8205 /* When the iterator is at base embedding level, we
8206 are guaranteed that characters are delivered for
8207 display in strictly increasing order of their
8208 buffer positions. */
8209 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8210 && IT_CHARPOS (*it) > to_charpos)
8211 || (it->bidi_p
8212 && (prev_method == GET_FROM_IMAGE
8213 || prev_method == GET_FROM_STRETCH
8214 || prev_method == GET_FROM_STRING)
8215 /* Passed TO_CHARPOS from left to right. */
8216 && ((prev_pos < to_charpos
8217 && IT_CHARPOS (*it) > to_charpos)
8218 /* Passed TO_CHARPOS from right to left. */
8219 || (prev_pos > to_charpos
8220 && IT_CHARPOS (*it) < to_charpos)))))
8222 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8224 result = MOVE_POS_MATCH_OR_ZV;
8225 break;
8227 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8228 /* If wrap_it is valid, the current position might be in a
8229 word that is wrapped. So, save the iterator in
8230 atpos_it and continue to see if wrapping happens. */
8231 SAVE_IT (atpos_it, *it, atpos_data);
8234 /* Stop when ZV reached.
8235 We used to stop here when TO_CHARPOS reached as well, but that is
8236 too soon if this glyph does not fit on this line. So we handle it
8237 explicitly below. */
8238 if (!get_next_display_element (it))
8240 result = MOVE_POS_MATCH_OR_ZV;
8241 break;
8244 if (it->line_wrap == TRUNCATE)
8246 if (BUFFER_POS_REACHED_P ())
8248 result = MOVE_POS_MATCH_OR_ZV;
8249 break;
8252 else
8254 if (it->line_wrap == WORD_WRAP)
8256 if (IT_DISPLAYING_WHITESPACE (it))
8257 may_wrap = 1;
8258 else if (may_wrap)
8260 /* We have reached a glyph that follows one or more
8261 whitespace characters. If the position is
8262 already found, we are done. */
8263 if (atpos_it.sp >= 0)
8265 RESTORE_IT (it, &atpos_it, atpos_data);
8266 result = MOVE_POS_MATCH_OR_ZV;
8267 goto done;
8269 if (atx_it.sp >= 0)
8271 RESTORE_IT (it, &atx_it, atx_data);
8272 result = MOVE_X_REACHED;
8273 goto done;
8275 /* Otherwise, we can wrap here. */
8276 SAVE_IT (wrap_it, *it, wrap_data);
8277 may_wrap = 0;
8282 /* Remember the line height for the current line, in case
8283 the next element doesn't fit on the line. */
8284 ascent = it->max_ascent;
8285 descent = it->max_descent;
8287 /* The call to produce_glyphs will get the metrics of the
8288 display element IT is loaded with. Record the x-position
8289 before this display element, in case it doesn't fit on the
8290 line. */
8291 x = it->current_x;
8293 PRODUCE_GLYPHS (it);
8295 if (it->area != TEXT_AREA)
8297 prev_method = it->method;
8298 if (it->method == GET_FROM_BUFFER)
8299 prev_pos = IT_CHARPOS (*it);
8300 set_iterator_to_next (it, 1);
8301 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8302 SET_TEXT_POS (this_line_min_pos,
8303 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8304 if (it->bidi_p
8305 && (op & MOVE_TO_POS)
8306 && IT_CHARPOS (*it) > to_charpos
8307 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8308 SAVE_IT (ppos_it, *it, ppos_data);
8309 continue;
8312 /* The number of glyphs we get back in IT->nglyphs will normally
8313 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8314 character on a terminal frame, or (iii) a line end. For the
8315 second case, IT->nglyphs - 1 padding glyphs will be present.
8316 (On X frames, there is only one glyph produced for a
8317 composite character.)
8319 The behavior implemented below means, for continuation lines,
8320 that as many spaces of a TAB as fit on the current line are
8321 displayed there. For terminal frames, as many glyphs of a
8322 multi-glyph character are displayed in the current line, too.
8323 This is what the old redisplay code did, and we keep it that
8324 way. Under X, the whole shape of a complex character must
8325 fit on the line or it will be completely displayed in the
8326 next line.
8328 Note that both for tabs and padding glyphs, all glyphs have
8329 the same width. */
8330 if (it->nglyphs)
8332 /* More than one glyph or glyph doesn't fit on line. All
8333 glyphs have the same width. */
8334 int single_glyph_width = it->pixel_width / it->nglyphs;
8335 int new_x;
8336 int x_before_this_char = x;
8337 int hpos_before_this_char = it->hpos;
8339 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8341 new_x = x + single_glyph_width;
8343 /* We want to leave anything reaching TO_X to the caller. */
8344 if ((op & MOVE_TO_X) && new_x > to_x)
8346 if (BUFFER_POS_REACHED_P ())
8348 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8349 goto buffer_pos_reached;
8350 if (atpos_it.sp < 0)
8352 SAVE_IT (atpos_it, *it, atpos_data);
8353 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8356 else
8358 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8360 it->current_x = x;
8361 result = MOVE_X_REACHED;
8362 break;
8364 if (atx_it.sp < 0)
8366 SAVE_IT (atx_it, *it, atx_data);
8367 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8372 if (/* Lines are continued. */
8373 it->line_wrap != TRUNCATE
8374 && (/* And glyph doesn't fit on the line. */
8375 new_x > it->last_visible_x
8376 /* Or it fits exactly and we're on a window
8377 system frame. */
8378 || (new_x == it->last_visible_x
8379 && FRAME_WINDOW_P (it->f)
8380 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8381 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8382 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8384 if (/* IT->hpos == 0 means the very first glyph
8385 doesn't fit on the line, e.g. a wide image. */
8386 it->hpos == 0
8387 || (new_x == it->last_visible_x
8388 && FRAME_WINDOW_P (it->f)))
8390 ++it->hpos;
8391 it->current_x = new_x;
8393 /* The character's last glyph just barely fits
8394 in this row. */
8395 if (i == it->nglyphs - 1)
8397 /* If this is the destination position,
8398 return a position *before* it in this row,
8399 now that we know it fits in this row. */
8400 if (BUFFER_POS_REACHED_P ())
8402 if (it->line_wrap != WORD_WRAP
8403 || wrap_it.sp < 0)
8405 it->hpos = hpos_before_this_char;
8406 it->current_x = x_before_this_char;
8407 result = MOVE_POS_MATCH_OR_ZV;
8408 break;
8410 if (it->line_wrap == WORD_WRAP
8411 && atpos_it.sp < 0)
8413 SAVE_IT (atpos_it, *it, atpos_data);
8414 atpos_it.current_x = x_before_this_char;
8415 atpos_it.hpos = hpos_before_this_char;
8419 prev_method = it->method;
8420 if (it->method == GET_FROM_BUFFER)
8421 prev_pos = IT_CHARPOS (*it);
8422 set_iterator_to_next (it, 1);
8423 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8424 SET_TEXT_POS (this_line_min_pos,
8425 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8426 /* On graphical terminals, newlines may
8427 "overflow" into the fringe if
8428 overflow-newline-into-fringe is non-nil.
8429 On text terminals, and on graphical
8430 terminals with no right margin, newlines
8431 may overflow into the last glyph on the
8432 display line.*/
8433 if (!FRAME_WINDOW_P (it->f)
8434 || ((it->bidi_p
8435 && it->bidi_it.paragraph_dir == R2L)
8436 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8437 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8438 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8440 if (!get_next_display_element (it))
8442 result = MOVE_POS_MATCH_OR_ZV;
8443 break;
8445 if (BUFFER_POS_REACHED_P ())
8447 if (ITERATOR_AT_END_OF_LINE_P (it))
8448 result = MOVE_POS_MATCH_OR_ZV;
8449 else
8450 result = MOVE_LINE_CONTINUED;
8451 break;
8453 if (ITERATOR_AT_END_OF_LINE_P (it))
8455 result = MOVE_NEWLINE_OR_CR;
8456 break;
8461 else
8462 IT_RESET_X_ASCENT_DESCENT (it);
8464 if (wrap_it.sp >= 0)
8466 RESTORE_IT (it, &wrap_it, wrap_data);
8467 atpos_it.sp = -1;
8468 atx_it.sp = -1;
8471 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8472 IT_CHARPOS (*it)));
8473 result = MOVE_LINE_CONTINUED;
8474 break;
8477 if (BUFFER_POS_REACHED_P ())
8479 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8480 goto buffer_pos_reached;
8481 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8483 SAVE_IT (atpos_it, *it, atpos_data);
8484 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8488 if (new_x > it->first_visible_x)
8490 /* Glyph is visible. Increment number of glyphs that
8491 would be displayed. */
8492 ++it->hpos;
8496 if (result != MOVE_UNDEFINED)
8497 break;
8499 else if (BUFFER_POS_REACHED_P ())
8501 buffer_pos_reached:
8502 IT_RESET_X_ASCENT_DESCENT (it);
8503 result = MOVE_POS_MATCH_OR_ZV;
8504 break;
8506 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8508 /* Stop when TO_X specified and reached. This check is
8509 necessary here because of lines consisting of a line end,
8510 only. The line end will not produce any glyphs and we
8511 would never get MOVE_X_REACHED. */
8512 eassert (it->nglyphs == 0);
8513 result = MOVE_X_REACHED;
8514 break;
8517 /* Is this a line end? If yes, we're done. */
8518 if (ITERATOR_AT_END_OF_LINE_P (it))
8520 /* If we are past TO_CHARPOS, but never saw any character
8521 positions smaller than TO_CHARPOS, return
8522 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8523 did. */
8524 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8526 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8528 if (IT_CHARPOS (ppos_it) < ZV)
8530 RESTORE_IT (it, &ppos_it, ppos_data);
8531 result = MOVE_POS_MATCH_OR_ZV;
8533 else
8534 goto buffer_pos_reached;
8536 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8537 && IT_CHARPOS (*it) > to_charpos)
8538 goto buffer_pos_reached;
8539 else
8540 result = MOVE_NEWLINE_OR_CR;
8542 else
8543 result = MOVE_NEWLINE_OR_CR;
8544 break;
8547 prev_method = it->method;
8548 if (it->method == GET_FROM_BUFFER)
8549 prev_pos = IT_CHARPOS (*it);
8550 /* The current display element has been consumed. Advance
8551 to the next. */
8552 set_iterator_to_next (it, 1);
8553 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8554 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8555 if (IT_CHARPOS (*it) < to_charpos)
8556 saw_smaller_pos = 1;
8557 if (it->bidi_p
8558 && (op & MOVE_TO_POS)
8559 && IT_CHARPOS (*it) >= to_charpos
8560 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8561 SAVE_IT (ppos_it, *it, ppos_data);
8563 /* Stop if lines are truncated and IT's current x-position is
8564 past the right edge of the window now. */
8565 if (it->line_wrap == TRUNCATE
8566 && it->current_x >= it->last_visible_x)
8568 if (!FRAME_WINDOW_P (it->f)
8569 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8570 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8571 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8572 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8574 int at_eob_p = 0;
8576 if ((at_eob_p = !get_next_display_element (it))
8577 || BUFFER_POS_REACHED_P ()
8578 /* If we are past TO_CHARPOS, but never saw any
8579 character positions smaller than TO_CHARPOS,
8580 return MOVE_POS_MATCH_OR_ZV, like the
8581 unidirectional display did. */
8582 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8583 && !saw_smaller_pos
8584 && IT_CHARPOS (*it) > to_charpos))
8586 if (it->bidi_p
8587 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8588 RESTORE_IT (it, &ppos_it, ppos_data);
8589 result = MOVE_POS_MATCH_OR_ZV;
8590 break;
8592 if (ITERATOR_AT_END_OF_LINE_P (it))
8594 result = MOVE_NEWLINE_OR_CR;
8595 break;
8598 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8599 && !saw_smaller_pos
8600 && IT_CHARPOS (*it) > to_charpos)
8602 if (IT_CHARPOS (ppos_it) < ZV)
8603 RESTORE_IT (it, &ppos_it, ppos_data);
8604 result = MOVE_POS_MATCH_OR_ZV;
8605 break;
8607 result = MOVE_LINE_TRUNCATED;
8608 break;
8610 #undef IT_RESET_X_ASCENT_DESCENT
8613 #undef BUFFER_POS_REACHED_P
8615 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8616 restore the saved iterator. */
8617 if (atpos_it.sp >= 0)
8618 RESTORE_IT (it, &atpos_it, atpos_data);
8619 else if (atx_it.sp >= 0)
8620 RESTORE_IT (it, &atx_it, atx_data);
8622 done:
8624 if (atpos_data)
8625 bidi_unshelve_cache (atpos_data, 1);
8626 if (atx_data)
8627 bidi_unshelve_cache (atx_data, 1);
8628 if (wrap_data)
8629 bidi_unshelve_cache (wrap_data, 1);
8630 if (ppos_data)
8631 bidi_unshelve_cache (ppos_data, 1);
8633 /* Restore the iterator settings altered at the beginning of this
8634 function. */
8635 it->glyph_row = saved_glyph_row;
8636 return result;
8639 /* For external use. */
8640 void
8641 move_it_in_display_line (struct it *it,
8642 ptrdiff_t to_charpos, int to_x,
8643 enum move_operation_enum op)
8645 if (it->line_wrap == WORD_WRAP
8646 && (op & MOVE_TO_X))
8648 struct it save_it;
8649 void *save_data = NULL;
8650 int skip;
8652 SAVE_IT (save_it, *it, save_data);
8653 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8654 /* When word-wrap is on, TO_X may lie past the end
8655 of a wrapped line. Then it->current is the
8656 character on the next line, so backtrack to the
8657 space before the wrap point. */
8658 if (skip == MOVE_LINE_CONTINUED)
8660 int prev_x = max (it->current_x - 1, 0);
8661 RESTORE_IT (it, &save_it, save_data);
8662 move_it_in_display_line_to
8663 (it, -1, prev_x, MOVE_TO_X);
8665 else
8666 bidi_unshelve_cache (save_data, 1);
8668 else
8669 move_it_in_display_line_to (it, to_charpos, to_x, op);
8673 /* Move IT forward until it satisfies one or more of the criteria in
8674 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8676 OP is a bit-mask that specifies where to stop, and in particular,
8677 which of those four position arguments makes a difference. See the
8678 description of enum move_operation_enum.
8680 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8681 screen line, this function will set IT to the next position that is
8682 displayed to the right of TO_CHARPOS on the screen. */
8684 void
8685 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8687 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8688 int line_height, line_start_x = 0, reached = 0;
8689 void *backup_data = NULL;
8691 for (;;)
8693 if (op & MOVE_TO_VPOS)
8695 /* If no TO_CHARPOS and no TO_X specified, stop at the
8696 start of the line TO_VPOS. */
8697 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8699 if (it->vpos == to_vpos)
8701 reached = 1;
8702 break;
8704 else
8705 skip = move_it_in_display_line_to (it, -1, -1, 0);
8707 else
8709 /* TO_VPOS >= 0 means stop at TO_X in the line at
8710 TO_VPOS, or at TO_POS, whichever comes first. */
8711 if (it->vpos == to_vpos)
8713 reached = 2;
8714 break;
8717 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8719 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8721 reached = 3;
8722 break;
8724 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8726 /* We have reached TO_X but not in the line we want. */
8727 skip = move_it_in_display_line_to (it, to_charpos,
8728 -1, MOVE_TO_POS);
8729 if (skip == MOVE_POS_MATCH_OR_ZV)
8731 reached = 4;
8732 break;
8737 else if (op & MOVE_TO_Y)
8739 struct it it_backup;
8741 if (it->line_wrap == WORD_WRAP)
8742 SAVE_IT (it_backup, *it, backup_data);
8744 /* TO_Y specified means stop at TO_X in the line containing
8745 TO_Y---or at TO_CHARPOS if this is reached first. The
8746 problem is that we can't really tell whether the line
8747 contains TO_Y before we have completely scanned it, and
8748 this may skip past TO_X. What we do is to first scan to
8749 TO_X.
8751 If TO_X is not specified, use a TO_X of zero. The reason
8752 is to make the outcome of this function more predictable.
8753 If we didn't use TO_X == 0, we would stop at the end of
8754 the line which is probably not what a caller would expect
8755 to happen. */
8756 skip = move_it_in_display_line_to
8757 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8758 (MOVE_TO_X | (op & MOVE_TO_POS)));
8760 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8761 if (skip == MOVE_POS_MATCH_OR_ZV)
8762 reached = 5;
8763 else if (skip == MOVE_X_REACHED)
8765 /* If TO_X was reached, we want to know whether TO_Y is
8766 in the line. We know this is the case if the already
8767 scanned glyphs make the line tall enough. Otherwise,
8768 we must check by scanning the rest of the line. */
8769 line_height = it->max_ascent + it->max_descent;
8770 if (to_y >= it->current_y
8771 && to_y < it->current_y + line_height)
8773 reached = 6;
8774 break;
8776 SAVE_IT (it_backup, *it, backup_data);
8777 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8778 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8779 op & MOVE_TO_POS);
8780 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8781 line_height = it->max_ascent + it->max_descent;
8782 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8784 if (to_y >= it->current_y
8785 && to_y < it->current_y + line_height)
8787 /* If TO_Y is in this line and TO_X was reached
8788 above, we scanned too far. We have to restore
8789 IT's settings to the ones before skipping. But
8790 keep the more accurate values of max_ascent and
8791 max_descent we've found while skipping the rest
8792 of the line, for the sake of callers, such as
8793 pos_visible_p, that need to know the line
8794 height. */
8795 int max_ascent = it->max_ascent;
8796 int max_descent = it->max_descent;
8798 RESTORE_IT (it, &it_backup, backup_data);
8799 it->max_ascent = max_ascent;
8800 it->max_descent = max_descent;
8801 reached = 6;
8803 else
8805 skip = skip2;
8806 if (skip == MOVE_POS_MATCH_OR_ZV)
8807 reached = 7;
8810 else
8812 /* Check whether TO_Y is in this line. */
8813 line_height = it->max_ascent + it->max_descent;
8814 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8816 if (to_y >= it->current_y
8817 && to_y < it->current_y + line_height)
8819 /* When word-wrap is on, TO_X may lie past the end
8820 of a wrapped line. Then it->current is the
8821 character on the next line, so backtrack to the
8822 space before the wrap point. */
8823 if (skip == MOVE_LINE_CONTINUED
8824 && it->line_wrap == WORD_WRAP)
8826 int prev_x = max (it->current_x - 1, 0);
8827 RESTORE_IT (it, &it_backup, backup_data);
8828 skip = move_it_in_display_line_to
8829 (it, -1, prev_x, MOVE_TO_X);
8831 reached = 6;
8835 if (reached)
8836 break;
8838 else if (BUFFERP (it->object)
8839 && (it->method == GET_FROM_BUFFER
8840 || it->method == GET_FROM_STRETCH)
8841 && IT_CHARPOS (*it) >= to_charpos
8842 /* Under bidi iteration, a call to set_iterator_to_next
8843 can scan far beyond to_charpos if the initial
8844 portion of the next line needs to be reordered. In
8845 that case, give move_it_in_display_line_to another
8846 chance below. */
8847 && !(it->bidi_p
8848 && it->bidi_it.scan_dir == -1))
8849 skip = MOVE_POS_MATCH_OR_ZV;
8850 else
8851 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8853 switch (skip)
8855 case MOVE_POS_MATCH_OR_ZV:
8856 reached = 8;
8857 goto out;
8859 case MOVE_NEWLINE_OR_CR:
8860 set_iterator_to_next (it, 1);
8861 it->continuation_lines_width = 0;
8862 break;
8864 case MOVE_LINE_TRUNCATED:
8865 it->continuation_lines_width = 0;
8866 reseat_at_next_visible_line_start (it, 0);
8867 if ((op & MOVE_TO_POS) != 0
8868 && IT_CHARPOS (*it) > to_charpos)
8870 reached = 9;
8871 goto out;
8873 break;
8875 case MOVE_LINE_CONTINUED:
8876 /* For continued lines ending in a tab, some of the glyphs
8877 associated with the tab are displayed on the current
8878 line. Since it->current_x does not include these glyphs,
8879 we use it->last_visible_x instead. */
8880 if (it->c == '\t')
8882 it->continuation_lines_width += it->last_visible_x;
8883 /* When moving by vpos, ensure that the iterator really
8884 advances to the next line (bug#847, bug#969). Fixme:
8885 do we need to do this in other circumstances? */
8886 if (it->current_x != it->last_visible_x
8887 && (op & MOVE_TO_VPOS)
8888 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8890 line_start_x = it->current_x + it->pixel_width
8891 - it->last_visible_x;
8892 set_iterator_to_next (it, 0);
8895 else
8896 it->continuation_lines_width += it->current_x;
8897 break;
8899 default:
8900 emacs_abort ();
8903 /* Reset/increment for the next run. */
8904 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8905 it->current_x = line_start_x;
8906 line_start_x = 0;
8907 it->hpos = 0;
8908 it->current_y += it->max_ascent + it->max_descent;
8909 ++it->vpos;
8910 last_height = it->max_ascent + it->max_descent;
8911 last_max_ascent = it->max_ascent;
8912 it->max_ascent = it->max_descent = 0;
8915 out:
8917 /* On text terminals, we may stop at the end of a line in the middle
8918 of a multi-character glyph. If the glyph itself is continued,
8919 i.e. it is actually displayed on the next line, don't treat this
8920 stopping point as valid; move to the next line instead (unless
8921 that brings us offscreen). */
8922 if (!FRAME_WINDOW_P (it->f)
8923 && op & MOVE_TO_POS
8924 && IT_CHARPOS (*it) == to_charpos
8925 && it->what == IT_CHARACTER
8926 && it->nglyphs > 1
8927 && it->line_wrap == WINDOW_WRAP
8928 && it->current_x == it->last_visible_x - 1
8929 && it->c != '\n'
8930 && it->c != '\t'
8931 && it->vpos < XFASTINT (it->w->window_end_vpos))
8933 it->continuation_lines_width += it->current_x;
8934 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8935 it->current_y += it->max_ascent + it->max_descent;
8936 ++it->vpos;
8937 last_height = it->max_ascent + it->max_descent;
8938 last_max_ascent = it->max_ascent;
8941 if (backup_data)
8942 bidi_unshelve_cache (backup_data, 1);
8944 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8948 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8950 If DY > 0, move IT backward at least that many pixels. DY = 0
8951 means move IT backward to the preceding line start or BEGV. This
8952 function may move over more than DY pixels if IT->current_y - DY
8953 ends up in the middle of a line; in this case IT->current_y will be
8954 set to the top of the line moved to. */
8956 void
8957 move_it_vertically_backward (struct it *it, int dy)
8959 int nlines, h;
8960 struct it it2, it3;
8961 void *it2data = NULL, *it3data = NULL;
8962 ptrdiff_t start_pos;
8964 move_further_back:
8965 eassert (dy >= 0);
8967 start_pos = IT_CHARPOS (*it);
8969 /* Estimate how many newlines we must move back. */
8970 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8972 /* Set the iterator's position that many lines back. */
8973 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8974 back_to_previous_visible_line_start (it);
8976 /* Reseat the iterator here. When moving backward, we don't want
8977 reseat to skip forward over invisible text, set up the iterator
8978 to deliver from overlay strings at the new position etc. So,
8979 use reseat_1 here. */
8980 reseat_1 (it, it->current.pos, 1);
8982 /* We are now surely at a line start. */
8983 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8984 reordering is in effect. */
8985 it->continuation_lines_width = 0;
8987 /* Move forward and see what y-distance we moved. First move to the
8988 start of the next line so that we get its height. We need this
8989 height to be able to tell whether we reached the specified
8990 y-distance. */
8991 SAVE_IT (it2, *it, it2data);
8992 it2.max_ascent = it2.max_descent = 0;
8995 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8996 MOVE_TO_POS | MOVE_TO_VPOS);
8998 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8999 /* If we are in a display string which starts at START_POS,
9000 and that display string includes a newline, and we are
9001 right after that newline (i.e. at the beginning of a
9002 display line), exit the loop, because otherwise we will
9003 infloop, since move_it_to will see that it is already at
9004 START_POS and will not move. */
9005 || (it2.method == GET_FROM_STRING
9006 && IT_CHARPOS (it2) == start_pos
9007 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9008 eassert (IT_CHARPOS (*it) >= BEGV);
9009 SAVE_IT (it3, it2, it3data);
9011 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9012 eassert (IT_CHARPOS (*it) >= BEGV);
9013 /* H is the actual vertical distance from the position in *IT
9014 and the starting position. */
9015 h = it2.current_y - it->current_y;
9016 /* NLINES is the distance in number of lines. */
9017 nlines = it2.vpos - it->vpos;
9019 /* Correct IT's y and vpos position
9020 so that they are relative to the starting point. */
9021 it->vpos -= nlines;
9022 it->current_y -= h;
9024 if (dy == 0)
9026 /* DY == 0 means move to the start of the screen line. The
9027 value of nlines is > 0 if continuation lines were involved,
9028 or if the original IT position was at start of a line. */
9029 RESTORE_IT (it, it, it2data);
9030 if (nlines > 0)
9031 move_it_by_lines (it, nlines);
9032 /* The above code moves us to some position NLINES down,
9033 usually to its first glyph (leftmost in an L2R line), but
9034 that's not necessarily the start of the line, under bidi
9035 reordering. We want to get to the character position
9036 that is immediately after the newline of the previous
9037 line. */
9038 if (it->bidi_p
9039 && !it->continuation_lines_width
9040 && !STRINGP (it->string)
9041 && IT_CHARPOS (*it) > BEGV
9042 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9044 ptrdiff_t nl_pos =
9045 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9047 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9049 bidi_unshelve_cache (it3data, 1);
9051 else
9053 /* The y-position we try to reach, relative to *IT.
9054 Note that H has been subtracted in front of the if-statement. */
9055 int target_y = it->current_y + h - dy;
9056 int y0 = it3.current_y;
9057 int y1;
9058 int line_height;
9060 RESTORE_IT (&it3, &it3, it3data);
9061 y1 = line_bottom_y (&it3);
9062 line_height = y1 - y0;
9063 RESTORE_IT (it, it, it2data);
9064 /* If we did not reach target_y, try to move further backward if
9065 we can. If we moved too far backward, try to move forward. */
9066 if (target_y < it->current_y
9067 /* This is heuristic. In a window that's 3 lines high, with
9068 a line height of 13 pixels each, recentering with point
9069 on the bottom line will try to move -39/2 = 19 pixels
9070 backward. Try to avoid moving into the first line. */
9071 && (it->current_y - target_y
9072 > min (window_box_height (it->w), line_height * 2 / 3))
9073 && IT_CHARPOS (*it) > BEGV)
9075 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9076 target_y - it->current_y));
9077 dy = it->current_y - target_y;
9078 goto move_further_back;
9080 else if (target_y >= it->current_y + line_height
9081 && IT_CHARPOS (*it) < ZV)
9083 /* Should move forward by at least one line, maybe more.
9085 Note: Calling move_it_by_lines can be expensive on
9086 terminal frames, where compute_motion is used (via
9087 vmotion) to do the job, when there are very long lines
9088 and truncate-lines is nil. That's the reason for
9089 treating terminal frames specially here. */
9091 if (!FRAME_WINDOW_P (it->f))
9092 move_it_vertically (it, target_y - (it->current_y + line_height));
9093 else
9097 move_it_by_lines (it, 1);
9099 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9106 /* Move IT by a specified amount of pixel lines DY. DY negative means
9107 move backwards. DY = 0 means move to start of screen line. At the
9108 end, IT will be on the start of a screen line. */
9110 void
9111 move_it_vertically (struct it *it, int dy)
9113 if (dy <= 0)
9114 move_it_vertically_backward (it, -dy);
9115 else
9117 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9118 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9119 MOVE_TO_POS | MOVE_TO_Y);
9120 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9122 /* If buffer ends in ZV without a newline, move to the start of
9123 the line to satisfy the post-condition. */
9124 if (IT_CHARPOS (*it) == ZV
9125 && ZV > BEGV
9126 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9127 move_it_by_lines (it, 0);
9132 /* Move iterator IT past the end of the text line it is in. */
9134 void
9135 move_it_past_eol (struct it *it)
9137 enum move_it_result rc;
9139 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9140 if (rc == MOVE_NEWLINE_OR_CR)
9141 set_iterator_to_next (it, 0);
9145 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9146 negative means move up. DVPOS == 0 means move to the start of the
9147 screen line.
9149 Optimization idea: If we would know that IT->f doesn't use
9150 a face with proportional font, we could be faster for
9151 truncate-lines nil. */
9153 void
9154 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9157 /* The commented-out optimization uses vmotion on terminals. This
9158 gives bad results, because elements like it->what, on which
9159 callers such as pos_visible_p rely, aren't updated. */
9160 /* struct position pos;
9161 if (!FRAME_WINDOW_P (it->f))
9163 struct text_pos textpos;
9165 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9166 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9167 reseat (it, textpos, 1);
9168 it->vpos += pos.vpos;
9169 it->current_y += pos.vpos;
9171 else */
9173 if (dvpos == 0)
9175 /* DVPOS == 0 means move to the start of the screen line. */
9176 move_it_vertically_backward (it, 0);
9177 /* Let next call to line_bottom_y calculate real line height */
9178 last_height = 0;
9180 else if (dvpos > 0)
9182 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9183 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9185 /* Only move to the next buffer position if we ended up in a
9186 string from display property, not in an overlay string
9187 (before-string or after-string). That is because the
9188 latter don't conceal the underlying buffer position, so
9189 we can ask to move the iterator to the exact position we
9190 are interested in. Note that, even if we are already at
9191 IT_CHARPOS (*it), the call below is not a no-op, as it
9192 will detect that we are at the end of the string, pop the
9193 iterator, and compute it->current_x and it->hpos
9194 correctly. */
9195 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9196 -1, -1, -1, MOVE_TO_POS);
9199 else
9201 struct it it2;
9202 void *it2data = NULL;
9203 ptrdiff_t start_charpos, i;
9205 /* Start at the beginning of the screen line containing IT's
9206 position. This may actually move vertically backwards,
9207 in case of overlays, so adjust dvpos accordingly. */
9208 dvpos += it->vpos;
9209 move_it_vertically_backward (it, 0);
9210 dvpos -= it->vpos;
9212 /* Go back -DVPOS visible lines and reseat the iterator there. */
9213 start_charpos = IT_CHARPOS (*it);
9214 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9215 back_to_previous_visible_line_start (it);
9216 reseat (it, it->current.pos, 1);
9218 /* Move further back if we end up in a string or an image. */
9219 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9221 /* First try to move to start of display line. */
9222 dvpos += it->vpos;
9223 move_it_vertically_backward (it, 0);
9224 dvpos -= it->vpos;
9225 if (IT_POS_VALID_AFTER_MOVE_P (it))
9226 break;
9227 /* If start of line is still in string or image,
9228 move further back. */
9229 back_to_previous_visible_line_start (it);
9230 reseat (it, it->current.pos, 1);
9231 dvpos--;
9234 it->current_x = it->hpos = 0;
9236 /* Above call may have moved too far if continuation lines
9237 are involved. Scan forward and see if it did. */
9238 SAVE_IT (it2, *it, it2data);
9239 it2.vpos = it2.current_y = 0;
9240 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9241 it->vpos -= it2.vpos;
9242 it->current_y -= it2.current_y;
9243 it->current_x = it->hpos = 0;
9245 /* If we moved too far back, move IT some lines forward. */
9246 if (it2.vpos > -dvpos)
9248 int delta = it2.vpos + dvpos;
9250 RESTORE_IT (&it2, &it2, it2data);
9251 SAVE_IT (it2, *it, it2data);
9252 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9253 /* Move back again if we got too far ahead. */
9254 if (IT_CHARPOS (*it) >= start_charpos)
9255 RESTORE_IT (it, &it2, it2data);
9256 else
9257 bidi_unshelve_cache (it2data, 1);
9259 else
9260 RESTORE_IT (it, it, it2data);
9264 /* Return 1 if IT points into the middle of a display vector. */
9267 in_display_vector_p (struct it *it)
9269 return (it->method == GET_FROM_DISPLAY_VECTOR
9270 && it->current.dpvec_index > 0
9271 && it->dpvec + it->current.dpvec_index != it->dpend);
9275 /***********************************************************************
9276 Messages
9277 ***********************************************************************/
9280 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9281 to *Messages*. */
9283 void
9284 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9286 Lisp_Object args[3];
9287 Lisp_Object msg, fmt;
9288 char *buffer;
9289 ptrdiff_t len;
9290 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9291 USE_SAFE_ALLOCA;
9293 fmt = msg = Qnil;
9294 GCPRO4 (fmt, msg, arg1, arg2);
9296 args[0] = fmt = build_string (format);
9297 args[1] = arg1;
9298 args[2] = arg2;
9299 msg = Fformat (3, args);
9301 len = SBYTES (msg) + 1;
9302 buffer = SAFE_ALLOCA (len);
9303 memcpy (buffer, SDATA (msg), len);
9305 message_dolog (buffer, len - 1, 1, 0);
9306 SAFE_FREE ();
9308 UNGCPRO;
9312 /* Output a newline in the *Messages* buffer if "needs" one. */
9314 void
9315 message_log_maybe_newline (void)
9317 if (message_log_need_newline)
9318 message_dolog ("", 0, 1, 0);
9322 /* Add a string M of length NBYTES to the message log, optionally
9323 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9324 nonzero, means interpret the contents of M as multibyte. This
9325 function calls low-level routines in order to bypass text property
9326 hooks, etc. which might not be safe to run.
9328 This may GC (insert may run before/after change hooks),
9329 so the buffer M must NOT point to a Lisp string. */
9331 void
9332 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9334 const unsigned char *msg = (const unsigned char *) m;
9336 if (!NILP (Vmemory_full))
9337 return;
9339 if (!NILP (Vmessage_log_max))
9341 struct buffer *oldbuf;
9342 Lisp_Object oldpoint, oldbegv, oldzv;
9343 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9344 ptrdiff_t point_at_end = 0;
9345 ptrdiff_t zv_at_end = 0;
9346 Lisp_Object old_deactivate_mark, tem;
9347 struct gcpro gcpro1;
9349 old_deactivate_mark = Vdeactivate_mark;
9350 oldbuf = current_buffer;
9351 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9352 bset_undo_list (current_buffer, Qt);
9354 oldpoint = message_dolog_marker1;
9355 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9356 oldbegv = message_dolog_marker2;
9357 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9358 oldzv = message_dolog_marker3;
9359 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9360 GCPRO1 (old_deactivate_mark);
9362 if (PT == Z)
9363 point_at_end = 1;
9364 if (ZV == Z)
9365 zv_at_end = 1;
9367 BEGV = BEG;
9368 BEGV_BYTE = BEG_BYTE;
9369 ZV = Z;
9370 ZV_BYTE = Z_BYTE;
9371 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9373 /* Insert the string--maybe converting multibyte to single byte
9374 or vice versa, so that all the text fits the buffer. */
9375 if (multibyte
9376 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9378 ptrdiff_t i;
9379 int c, char_bytes;
9380 char work[1];
9382 /* Convert a multibyte string to single-byte
9383 for the *Message* buffer. */
9384 for (i = 0; i < nbytes; i += char_bytes)
9386 c = string_char_and_length (msg + i, &char_bytes);
9387 work[0] = (ASCII_CHAR_P (c)
9389 : multibyte_char_to_unibyte (c));
9390 insert_1_both (work, 1, 1, 1, 0, 0);
9393 else if (! multibyte
9394 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9396 ptrdiff_t i;
9397 int c, char_bytes;
9398 unsigned char str[MAX_MULTIBYTE_LENGTH];
9399 /* Convert a single-byte string to multibyte
9400 for the *Message* buffer. */
9401 for (i = 0; i < nbytes; i++)
9403 c = msg[i];
9404 MAKE_CHAR_MULTIBYTE (c);
9405 char_bytes = CHAR_STRING (c, str);
9406 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9409 else if (nbytes)
9410 insert_1 (m, nbytes, 1, 0, 0);
9412 if (nlflag)
9414 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9415 printmax_t dups;
9416 insert_1 ("\n", 1, 1, 0, 0);
9418 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9419 this_bol = PT;
9420 this_bol_byte = PT_BYTE;
9422 /* See if this line duplicates the previous one.
9423 If so, combine duplicates. */
9424 if (this_bol > BEG)
9426 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9427 prev_bol = PT;
9428 prev_bol_byte = PT_BYTE;
9430 dups = message_log_check_duplicate (prev_bol_byte,
9431 this_bol_byte);
9432 if (dups)
9434 del_range_both (prev_bol, prev_bol_byte,
9435 this_bol, this_bol_byte, 0);
9436 if (dups > 1)
9438 char dupstr[sizeof " [ times]"
9439 + INT_STRLEN_BOUND (printmax_t)];
9441 /* If you change this format, don't forget to also
9442 change message_log_check_duplicate. */
9443 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9444 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9445 insert_1 (dupstr, duplen, 1, 0, 1);
9450 /* If we have more than the desired maximum number of lines
9451 in the *Messages* buffer now, delete the oldest ones.
9452 This is safe because we don't have undo in this buffer. */
9454 if (NATNUMP (Vmessage_log_max))
9456 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9457 -XFASTINT (Vmessage_log_max) - 1, 0);
9458 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9461 BEGV = XMARKER (oldbegv)->charpos;
9462 BEGV_BYTE = marker_byte_position (oldbegv);
9464 if (zv_at_end)
9466 ZV = Z;
9467 ZV_BYTE = Z_BYTE;
9469 else
9471 ZV = XMARKER (oldzv)->charpos;
9472 ZV_BYTE = marker_byte_position (oldzv);
9475 if (point_at_end)
9476 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9477 else
9478 /* We can't do Fgoto_char (oldpoint) because it will run some
9479 Lisp code. */
9480 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9481 XMARKER (oldpoint)->bytepos);
9483 UNGCPRO;
9484 unchain_marker (XMARKER (oldpoint));
9485 unchain_marker (XMARKER (oldbegv));
9486 unchain_marker (XMARKER (oldzv));
9488 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9489 set_buffer_internal (oldbuf);
9490 if (NILP (tem))
9491 windows_or_buffers_changed = old_windows_or_buffers_changed;
9492 message_log_need_newline = !nlflag;
9493 Vdeactivate_mark = old_deactivate_mark;
9498 /* We are at the end of the buffer after just having inserted a newline.
9499 (Note: We depend on the fact we won't be crossing the gap.)
9500 Check to see if the most recent message looks a lot like the previous one.
9501 Return 0 if different, 1 if the new one should just replace it, or a
9502 value N > 1 if we should also append " [N times]". */
9504 static intmax_t
9505 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9507 ptrdiff_t i;
9508 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9509 int seen_dots = 0;
9510 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9511 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9513 for (i = 0; i < len; i++)
9515 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9516 seen_dots = 1;
9517 if (p1[i] != p2[i])
9518 return seen_dots;
9520 p1 += len;
9521 if (*p1 == '\n')
9522 return 2;
9523 if (*p1++ == ' ' && *p1++ == '[')
9525 char *pend;
9526 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9527 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9528 return n+1;
9530 return 0;
9534 /* Display an echo area message M with a specified length of NBYTES
9535 bytes. The string may include null characters. If M is 0, clear
9536 out any existing message, and let the mini-buffer text show
9537 through.
9539 This may GC, so the buffer M must NOT point to a Lisp string. */
9541 void
9542 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9544 /* First flush out any partial line written with print. */
9545 message_log_maybe_newline ();
9546 if (m)
9547 message_dolog (m, nbytes, 1, multibyte);
9548 message2_nolog (m, nbytes, multibyte);
9552 /* The non-logging counterpart of message2. */
9554 void
9555 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9557 struct frame *sf = SELECTED_FRAME ();
9558 message_enable_multibyte = multibyte;
9560 if (FRAME_INITIAL_P (sf))
9562 if (noninteractive_need_newline)
9563 putc ('\n', stderr);
9564 noninteractive_need_newline = 0;
9565 if (m)
9566 fwrite (m, nbytes, 1, stderr);
9567 if (cursor_in_echo_area == 0)
9568 fprintf (stderr, "\n");
9569 fflush (stderr);
9571 /* A null message buffer means that the frame hasn't really been
9572 initialized yet. Error messages get reported properly by
9573 cmd_error, so this must be just an informative message; toss it. */
9574 else if (INTERACTIVE
9575 && sf->glyphs_initialized_p
9576 && FRAME_MESSAGE_BUF (sf))
9578 Lisp_Object mini_window;
9579 struct frame *f;
9581 /* Get the frame containing the mini-buffer
9582 that the selected frame is using. */
9583 mini_window = FRAME_MINIBUF_WINDOW (sf);
9584 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9586 FRAME_SAMPLE_VISIBILITY (f);
9587 if (FRAME_VISIBLE_P (sf)
9588 && ! FRAME_VISIBLE_P (f))
9589 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9591 if (m)
9593 set_message (m, Qnil, nbytes, multibyte);
9594 if (minibuffer_auto_raise)
9595 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9597 else
9598 clear_message (1, 1);
9600 do_pending_window_change (0);
9601 echo_area_display (1);
9602 do_pending_window_change (0);
9603 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9604 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9609 /* Display an echo area message M with a specified length of NBYTES
9610 bytes. The string may include null characters. If M is not a
9611 string, clear out any existing message, and let the mini-buffer
9612 text show through.
9614 This function cancels echoing. */
9616 void
9617 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9619 struct gcpro gcpro1;
9621 GCPRO1 (m);
9622 clear_message (1,1);
9623 cancel_echoing ();
9625 /* First flush out any partial line written with print. */
9626 message_log_maybe_newline ();
9627 if (STRINGP (m))
9629 USE_SAFE_ALLOCA;
9630 char *buffer = SAFE_ALLOCA (nbytes);
9631 memcpy (buffer, SDATA (m), nbytes);
9632 message_dolog (buffer, nbytes, 1, multibyte);
9633 SAFE_FREE ();
9635 message3_nolog (m, nbytes, multibyte);
9637 UNGCPRO;
9641 /* The non-logging version of message3.
9642 This does not cancel echoing, because it is used for echoing.
9643 Perhaps we need to make a separate function for echoing
9644 and make this cancel echoing. */
9646 void
9647 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9649 struct frame *sf = SELECTED_FRAME ();
9650 message_enable_multibyte = multibyte;
9652 if (FRAME_INITIAL_P (sf))
9654 if (noninteractive_need_newline)
9655 putc ('\n', stderr);
9656 noninteractive_need_newline = 0;
9657 if (STRINGP (m))
9658 fwrite (SDATA (m), nbytes, 1, stderr);
9659 if (cursor_in_echo_area == 0)
9660 fprintf (stderr, "\n");
9661 fflush (stderr);
9663 /* A null message buffer means that the frame hasn't really been
9664 initialized yet. Error messages get reported properly by
9665 cmd_error, so this must be just an informative message; toss it. */
9666 else if (INTERACTIVE
9667 && sf->glyphs_initialized_p
9668 && FRAME_MESSAGE_BUF (sf))
9670 Lisp_Object mini_window;
9671 Lisp_Object frame;
9672 struct frame *f;
9674 /* Get the frame containing the mini-buffer
9675 that the selected frame is using. */
9676 mini_window = FRAME_MINIBUF_WINDOW (sf);
9677 frame = XWINDOW (mini_window)->frame;
9678 f = XFRAME (frame);
9680 FRAME_SAMPLE_VISIBILITY (f);
9681 if (FRAME_VISIBLE_P (sf)
9682 && !FRAME_VISIBLE_P (f))
9683 Fmake_frame_visible (frame);
9685 if (STRINGP (m) && SCHARS (m) > 0)
9687 set_message (NULL, m, nbytes, multibyte);
9688 if (minibuffer_auto_raise)
9689 Fraise_frame (frame);
9690 /* Assume we are not echoing.
9691 (If we are, echo_now will override this.) */
9692 echo_message_buffer = Qnil;
9694 else
9695 clear_message (1, 1);
9697 do_pending_window_change (0);
9698 echo_area_display (1);
9699 do_pending_window_change (0);
9700 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9701 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9706 /* Display a null-terminated echo area message M. If M is 0, clear
9707 out any existing message, and let the mini-buffer text show through.
9709 The buffer M must continue to exist until after the echo area gets
9710 cleared or some other message gets displayed there. Do not pass
9711 text that is stored in a Lisp string. Do not pass text in a buffer
9712 that was alloca'd. */
9714 void
9715 message1 (const char *m)
9717 message2 (m, (m ? strlen (m) : 0), 0);
9721 /* The non-logging counterpart of message1. */
9723 void
9724 message1_nolog (const char *m)
9726 message2_nolog (m, (m ? strlen (m) : 0), 0);
9729 /* Display a message M which contains a single %s
9730 which gets replaced with STRING. */
9732 void
9733 message_with_string (const char *m, Lisp_Object string, int log)
9735 CHECK_STRING (string);
9737 if (noninteractive)
9739 if (m)
9741 if (noninteractive_need_newline)
9742 putc ('\n', stderr);
9743 noninteractive_need_newline = 0;
9744 fprintf (stderr, m, SDATA (string));
9745 if (!cursor_in_echo_area)
9746 fprintf (stderr, "\n");
9747 fflush (stderr);
9750 else if (INTERACTIVE)
9752 /* The frame whose minibuffer we're going to display the message on.
9753 It may be larger than the selected frame, so we need
9754 to use its buffer, not the selected frame's buffer. */
9755 Lisp_Object mini_window;
9756 struct frame *f, *sf = SELECTED_FRAME ();
9758 /* Get the frame containing the minibuffer
9759 that the selected frame is using. */
9760 mini_window = FRAME_MINIBUF_WINDOW (sf);
9761 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9763 /* A null message buffer means that the frame hasn't really been
9764 initialized yet. Error messages get reported properly by
9765 cmd_error, so this must be just an informative message; toss it. */
9766 if (FRAME_MESSAGE_BUF (f))
9768 Lisp_Object args[2], msg;
9769 struct gcpro gcpro1, gcpro2;
9771 args[0] = build_string (m);
9772 args[1] = msg = string;
9773 GCPRO2 (args[0], msg);
9774 gcpro1.nvars = 2;
9776 msg = Fformat (2, args);
9778 if (log)
9779 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9780 else
9781 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9783 UNGCPRO;
9785 /* Print should start at the beginning of the message
9786 buffer next time. */
9787 message_buf_print = 0;
9793 /* Dump an informative message to the minibuf. If M is 0, clear out
9794 any existing message, and let the mini-buffer text show through. */
9796 static void
9797 vmessage (const char *m, va_list ap)
9799 if (noninteractive)
9801 if (m)
9803 if (noninteractive_need_newline)
9804 putc ('\n', stderr);
9805 noninteractive_need_newline = 0;
9806 vfprintf (stderr, m, ap);
9807 if (cursor_in_echo_area == 0)
9808 fprintf (stderr, "\n");
9809 fflush (stderr);
9812 else if (INTERACTIVE)
9814 /* The frame whose mini-buffer we're going to display the message
9815 on. It may be larger than the selected frame, so we need to
9816 use its buffer, not the selected frame's buffer. */
9817 Lisp_Object mini_window;
9818 struct frame *f, *sf = SELECTED_FRAME ();
9820 /* Get the frame containing the mini-buffer
9821 that the selected frame is using. */
9822 mini_window = FRAME_MINIBUF_WINDOW (sf);
9823 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9825 /* A null message buffer means that the frame hasn't really been
9826 initialized yet. Error messages get reported properly by
9827 cmd_error, so this must be just an informative message; toss
9828 it. */
9829 if (FRAME_MESSAGE_BUF (f))
9831 if (m)
9833 ptrdiff_t len;
9835 len = doprnt (FRAME_MESSAGE_BUF (f),
9836 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9838 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9840 else
9841 message1 (0);
9843 /* Print should start at the beginning of the message
9844 buffer next time. */
9845 message_buf_print = 0;
9850 void
9851 message (const char *m, ...)
9853 va_list ap;
9854 va_start (ap, m);
9855 vmessage (m, ap);
9856 va_end (ap);
9860 #if 0
9861 /* The non-logging version of message. */
9863 void
9864 message_nolog (const char *m, ...)
9866 Lisp_Object old_log_max;
9867 va_list ap;
9868 va_start (ap, m);
9869 old_log_max = Vmessage_log_max;
9870 Vmessage_log_max = Qnil;
9871 vmessage (m, ap);
9872 Vmessage_log_max = old_log_max;
9873 va_end (ap);
9875 #endif
9878 /* Display the current message in the current mini-buffer. This is
9879 only called from error handlers in process.c, and is not time
9880 critical. */
9882 void
9883 update_echo_area (void)
9885 if (!NILP (echo_area_buffer[0]))
9887 Lisp_Object string;
9888 string = Fcurrent_message ();
9889 message3 (string, SBYTES (string),
9890 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9895 /* Make sure echo area buffers in `echo_buffers' are live.
9896 If they aren't, make new ones. */
9898 static void
9899 ensure_echo_area_buffers (void)
9901 int i;
9903 for (i = 0; i < 2; ++i)
9904 if (!BUFFERP (echo_buffer[i])
9905 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9907 char name[30];
9908 Lisp_Object old_buffer;
9909 int j;
9911 old_buffer = echo_buffer[i];
9912 echo_buffer[i] = Fget_buffer_create
9913 (make_formatted_string (name, " *Echo Area %d*", i));
9914 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9915 /* to force word wrap in echo area -
9916 it was decided to postpone this*/
9917 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9919 for (j = 0; j < 2; ++j)
9920 if (EQ (old_buffer, echo_area_buffer[j]))
9921 echo_area_buffer[j] = echo_buffer[i];
9926 /* Call FN with args A1..A4 with either the current or last displayed
9927 echo_area_buffer as current buffer.
9929 WHICH zero means use the current message buffer
9930 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9931 from echo_buffer[] and clear it.
9933 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9934 suitable buffer from echo_buffer[] and clear it.
9936 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9937 that the current message becomes the last displayed one, make
9938 choose a suitable buffer for echo_area_buffer[0], and clear it.
9940 Value is what FN returns. */
9942 static int
9943 with_echo_area_buffer (struct window *w, int which,
9944 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9945 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9947 Lisp_Object buffer;
9948 int this_one, the_other, clear_buffer_p, rc;
9949 ptrdiff_t count = SPECPDL_INDEX ();
9951 /* If buffers aren't live, make new ones. */
9952 ensure_echo_area_buffers ();
9954 clear_buffer_p = 0;
9956 if (which == 0)
9957 this_one = 0, the_other = 1;
9958 else if (which > 0)
9959 this_one = 1, the_other = 0;
9960 else
9962 this_one = 0, the_other = 1;
9963 clear_buffer_p = 1;
9965 /* We need a fresh one in case the current echo buffer equals
9966 the one containing the last displayed echo area message. */
9967 if (!NILP (echo_area_buffer[this_one])
9968 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9969 echo_area_buffer[this_one] = Qnil;
9972 /* Choose a suitable buffer from echo_buffer[] is we don't
9973 have one. */
9974 if (NILP (echo_area_buffer[this_one]))
9976 echo_area_buffer[this_one]
9977 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9978 ? echo_buffer[the_other]
9979 : echo_buffer[this_one]);
9980 clear_buffer_p = 1;
9983 buffer = echo_area_buffer[this_one];
9985 /* Don't get confused by reusing the buffer used for echoing
9986 for a different purpose. */
9987 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9988 cancel_echoing ();
9990 record_unwind_protect (unwind_with_echo_area_buffer,
9991 with_echo_area_buffer_unwind_data (w));
9993 /* Make the echo area buffer current. Note that for display
9994 purposes, it is not necessary that the displayed window's buffer
9995 == current_buffer, except for text property lookup. So, let's
9996 only set that buffer temporarily here without doing a full
9997 Fset_window_buffer. We must also change w->pointm, though,
9998 because otherwise an assertions in unshow_buffer fails, and Emacs
9999 aborts. */
10000 set_buffer_internal_1 (XBUFFER (buffer));
10001 if (w)
10003 wset_buffer (w, buffer);
10004 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10007 bset_undo_list (current_buffer, Qt);
10008 bset_read_only (current_buffer, Qnil);
10009 specbind (Qinhibit_read_only, Qt);
10010 specbind (Qinhibit_modification_hooks, Qt);
10012 if (clear_buffer_p && Z > BEG)
10013 del_range (BEG, Z);
10015 eassert (BEGV >= BEG);
10016 eassert (ZV <= Z && ZV >= BEGV);
10018 rc = fn (a1, a2, a3, a4);
10020 eassert (BEGV >= BEG);
10021 eassert (ZV <= Z && ZV >= BEGV);
10023 unbind_to (count, Qnil);
10024 return rc;
10028 /* Save state that should be preserved around the call to the function
10029 FN called in with_echo_area_buffer. */
10031 static Lisp_Object
10032 with_echo_area_buffer_unwind_data (struct window *w)
10034 int i = 0;
10035 Lisp_Object vector, tmp;
10037 /* Reduce consing by keeping one vector in
10038 Vwith_echo_area_save_vector. */
10039 vector = Vwith_echo_area_save_vector;
10040 Vwith_echo_area_save_vector = Qnil;
10042 if (NILP (vector))
10043 vector = Fmake_vector (make_number (7), Qnil);
10045 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10046 ASET (vector, i, Vdeactivate_mark); ++i;
10047 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10049 if (w)
10051 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10052 ASET (vector, i, w->buffer); ++i;
10053 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10054 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10056 else
10058 int end = i + 4;
10059 for (; i < end; ++i)
10060 ASET (vector, i, Qnil);
10063 eassert (i == ASIZE (vector));
10064 return vector;
10068 /* Restore global state from VECTOR which was created by
10069 with_echo_area_buffer_unwind_data. */
10071 static Lisp_Object
10072 unwind_with_echo_area_buffer (Lisp_Object vector)
10074 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10075 Vdeactivate_mark = AREF (vector, 1);
10076 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10078 if (WINDOWP (AREF (vector, 3)))
10080 struct window *w;
10081 Lisp_Object buffer, charpos, bytepos;
10083 w = XWINDOW (AREF (vector, 3));
10084 buffer = AREF (vector, 4);
10085 charpos = AREF (vector, 5);
10086 bytepos = AREF (vector, 6);
10088 wset_buffer (w, buffer);
10089 set_marker_both (w->pointm, buffer,
10090 XFASTINT (charpos), XFASTINT (bytepos));
10093 Vwith_echo_area_save_vector = vector;
10094 return Qnil;
10098 /* Set up the echo area for use by print functions. MULTIBYTE_P
10099 non-zero means we will print multibyte. */
10101 void
10102 setup_echo_area_for_printing (int multibyte_p)
10104 /* If we can't find an echo area any more, exit. */
10105 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10106 Fkill_emacs (Qnil);
10108 ensure_echo_area_buffers ();
10110 if (!message_buf_print)
10112 /* A message has been output since the last time we printed.
10113 Choose a fresh echo area buffer. */
10114 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10115 echo_area_buffer[0] = echo_buffer[1];
10116 else
10117 echo_area_buffer[0] = echo_buffer[0];
10119 /* Switch to that buffer and clear it. */
10120 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10121 bset_truncate_lines (current_buffer, Qnil);
10123 if (Z > BEG)
10125 ptrdiff_t count = SPECPDL_INDEX ();
10126 specbind (Qinhibit_read_only, Qt);
10127 /* Note that undo recording is always disabled. */
10128 del_range (BEG, Z);
10129 unbind_to (count, Qnil);
10131 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10133 /* Set up the buffer for the multibyteness we need. */
10134 if (multibyte_p
10135 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10136 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10138 /* Raise the frame containing the echo area. */
10139 if (minibuffer_auto_raise)
10141 struct frame *sf = SELECTED_FRAME ();
10142 Lisp_Object mini_window;
10143 mini_window = FRAME_MINIBUF_WINDOW (sf);
10144 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10147 message_log_maybe_newline ();
10148 message_buf_print = 1;
10150 else
10152 if (NILP (echo_area_buffer[0]))
10154 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10155 echo_area_buffer[0] = echo_buffer[1];
10156 else
10157 echo_area_buffer[0] = echo_buffer[0];
10160 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10162 /* Someone switched buffers between print requests. */
10163 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10164 bset_truncate_lines (current_buffer, Qnil);
10170 /* Display an echo area message in window W. Value is non-zero if W's
10171 height is changed. If display_last_displayed_message_p is
10172 non-zero, display the message that was last displayed, otherwise
10173 display the current message. */
10175 static int
10176 display_echo_area (struct window *w)
10178 int i, no_message_p, window_height_changed_p;
10180 /* Temporarily disable garbage collections while displaying the echo
10181 area. This is done because a GC can print a message itself.
10182 That message would modify the echo area buffer's contents while a
10183 redisplay of the buffer is going on, and seriously confuse
10184 redisplay. */
10185 ptrdiff_t count = inhibit_garbage_collection ();
10187 /* If there is no message, we must call display_echo_area_1
10188 nevertheless because it resizes the window. But we will have to
10189 reset the echo_area_buffer in question to nil at the end because
10190 with_echo_area_buffer will sets it to an empty buffer. */
10191 i = display_last_displayed_message_p ? 1 : 0;
10192 no_message_p = NILP (echo_area_buffer[i]);
10194 window_height_changed_p
10195 = with_echo_area_buffer (w, display_last_displayed_message_p,
10196 display_echo_area_1,
10197 (intptr_t) w, Qnil, 0, 0);
10199 if (no_message_p)
10200 echo_area_buffer[i] = Qnil;
10202 unbind_to (count, Qnil);
10203 return window_height_changed_p;
10207 /* Helper for display_echo_area. Display the current buffer which
10208 contains the current echo area message in window W, a mini-window,
10209 a pointer to which is passed in A1. A2..A4 are currently not used.
10210 Change the height of W so that all of the message is displayed.
10211 Value is non-zero if height of W was changed. */
10213 static int
10214 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10216 intptr_t i1 = a1;
10217 struct window *w = (struct window *) i1;
10218 Lisp_Object window;
10219 struct text_pos start;
10220 int window_height_changed_p = 0;
10222 /* Do this before displaying, so that we have a large enough glyph
10223 matrix for the display. If we can't get enough space for the
10224 whole text, display the last N lines. That works by setting w->start. */
10225 window_height_changed_p = resize_mini_window (w, 0);
10227 /* Use the starting position chosen by resize_mini_window. */
10228 SET_TEXT_POS_FROM_MARKER (start, w->start);
10230 /* Display. */
10231 clear_glyph_matrix (w->desired_matrix);
10232 XSETWINDOW (window, w);
10233 try_window (window, start, 0);
10235 return window_height_changed_p;
10239 /* Resize the echo area window to exactly the size needed for the
10240 currently displayed message, if there is one. If a mini-buffer
10241 is active, don't shrink it. */
10243 void
10244 resize_echo_area_exactly (void)
10246 if (BUFFERP (echo_area_buffer[0])
10247 && WINDOWP (echo_area_window))
10249 struct window *w = XWINDOW (echo_area_window);
10250 int resized_p;
10251 Lisp_Object resize_exactly;
10253 if (minibuf_level == 0)
10254 resize_exactly = Qt;
10255 else
10256 resize_exactly = Qnil;
10258 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10259 (intptr_t) w, resize_exactly,
10260 0, 0);
10261 if (resized_p)
10263 ++windows_or_buffers_changed;
10264 ++update_mode_lines;
10265 redisplay_internal ();
10271 /* Callback function for with_echo_area_buffer, when used from
10272 resize_echo_area_exactly. A1 contains a pointer to the window to
10273 resize, EXACTLY non-nil means resize the mini-window exactly to the
10274 size of the text displayed. A3 and A4 are not used. Value is what
10275 resize_mini_window returns. */
10277 static int
10278 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10280 intptr_t i1 = a1;
10281 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10285 /* Resize mini-window W to fit the size of its contents. EXACT_P
10286 means size the window exactly to the size needed. Otherwise, it's
10287 only enlarged until W's buffer is empty.
10289 Set W->start to the right place to begin display. If the whole
10290 contents fit, start at the beginning. Otherwise, start so as
10291 to make the end of the contents appear. This is particularly
10292 important for y-or-n-p, but seems desirable generally.
10294 Value is non-zero if the window height has been changed. */
10297 resize_mini_window (struct window *w, int exact_p)
10299 struct frame *f = XFRAME (w->frame);
10300 int window_height_changed_p = 0;
10302 eassert (MINI_WINDOW_P (w));
10304 /* By default, start display at the beginning. */
10305 set_marker_both (w->start, w->buffer,
10306 BUF_BEGV (XBUFFER (w->buffer)),
10307 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10309 /* Don't resize windows while redisplaying a window; it would
10310 confuse redisplay functions when the size of the window they are
10311 displaying changes from under them. Such a resizing can happen,
10312 for instance, when which-func prints a long message while
10313 we are running fontification-functions. We're running these
10314 functions with safe_call which binds inhibit-redisplay to t. */
10315 if (!NILP (Vinhibit_redisplay))
10316 return 0;
10318 /* Nil means don't try to resize. */
10319 if (NILP (Vresize_mini_windows)
10320 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10321 return 0;
10323 if (!FRAME_MINIBUF_ONLY_P (f))
10325 struct it it;
10326 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10327 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10328 int height;
10329 EMACS_INT max_height;
10330 int unit = FRAME_LINE_HEIGHT (f);
10331 struct text_pos start;
10332 struct buffer *old_current_buffer = NULL;
10334 if (current_buffer != XBUFFER (w->buffer))
10336 old_current_buffer = current_buffer;
10337 set_buffer_internal (XBUFFER (w->buffer));
10340 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10342 /* Compute the max. number of lines specified by the user. */
10343 if (FLOATP (Vmax_mini_window_height))
10344 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10345 else if (INTEGERP (Vmax_mini_window_height))
10346 max_height = XINT (Vmax_mini_window_height);
10347 else
10348 max_height = total_height / 4;
10350 /* Correct that max. height if it's bogus. */
10351 max_height = max (1, max_height);
10352 max_height = min (total_height, max_height);
10354 /* Find out the height of the text in the window. */
10355 if (it.line_wrap == TRUNCATE)
10356 height = 1;
10357 else
10359 last_height = 0;
10360 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10361 if (it.max_ascent == 0 && it.max_descent == 0)
10362 height = it.current_y + last_height;
10363 else
10364 height = it.current_y + it.max_ascent + it.max_descent;
10365 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10366 height = (height + unit - 1) / unit;
10369 /* Compute a suitable window start. */
10370 if (height > max_height)
10372 height = max_height;
10373 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10374 move_it_vertically_backward (&it, (height - 1) * unit);
10375 start = it.current.pos;
10377 else
10378 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10379 SET_MARKER_FROM_TEXT_POS (w->start, start);
10381 if (EQ (Vresize_mini_windows, Qgrow_only))
10383 /* Let it grow only, until we display an empty message, in which
10384 case the window shrinks again. */
10385 if (height > WINDOW_TOTAL_LINES (w))
10387 int old_height = WINDOW_TOTAL_LINES (w);
10388 freeze_window_starts (f, 1);
10389 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10390 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10392 else if (height < WINDOW_TOTAL_LINES (w)
10393 && (exact_p || BEGV == ZV))
10395 int old_height = WINDOW_TOTAL_LINES (w);
10396 freeze_window_starts (f, 0);
10397 shrink_mini_window (w);
10398 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10401 else
10403 /* Always resize to exact size needed. */
10404 if (height > WINDOW_TOTAL_LINES (w))
10406 int old_height = WINDOW_TOTAL_LINES (w);
10407 freeze_window_starts (f, 1);
10408 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10409 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10411 else if (height < WINDOW_TOTAL_LINES (w))
10413 int old_height = WINDOW_TOTAL_LINES (w);
10414 freeze_window_starts (f, 0);
10415 shrink_mini_window (w);
10417 if (height)
10419 freeze_window_starts (f, 1);
10420 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10423 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10427 if (old_current_buffer)
10428 set_buffer_internal (old_current_buffer);
10431 return window_height_changed_p;
10435 /* Value is the current message, a string, or nil if there is no
10436 current message. */
10438 Lisp_Object
10439 current_message (void)
10441 Lisp_Object msg;
10443 if (!BUFFERP (echo_area_buffer[0]))
10444 msg = Qnil;
10445 else
10447 with_echo_area_buffer (0, 0, current_message_1,
10448 (intptr_t) &msg, Qnil, 0, 0);
10449 if (NILP (msg))
10450 echo_area_buffer[0] = Qnil;
10453 return msg;
10457 static int
10458 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10460 intptr_t i1 = a1;
10461 Lisp_Object *msg = (Lisp_Object *) i1;
10463 if (Z > BEG)
10464 *msg = make_buffer_string (BEG, Z, 1);
10465 else
10466 *msg = Qnil;
10467 return 0;
10471 /* Push the current message on Vmessage_stack for later restoration
10472 by restore_message. Value is non-zero if the current message isn't
10473 empty. This is a relatively infrequent operation, so it's not
10474 worth optimizing. */
10476 bool
10477 push_message (void)
10479 Lisp_Object msg = current_message ();
10480 Vmessage_stack = Fcons (msg, Vmessage_stack);
10481 return STRINGP (msg);
10485 /* Restore message display from the top of Vmessage_stack. */
10487 void
10488 restore_message (void)
10490 Lisp_Object msg;
10492 eassert (CONSP (Vmessage_stack));
10493 msg = XCAR (Vmessage_stack);
10494 if (STRINGP (msg))
10495 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10496 else
10497 message3_nolog (msg, 0, 0);
10501 /* Handler for record_unwind_protect calling pop_message. */
10503 Lisp_Object
10504 pop_message_unwind (Lisp_Object dummy)
10506 pop_message ();
10507 return Qnil;
10510 /* Pop the top-most entry off Vmessage_stack. */
10512 static void
10513 pop_message (void)
10515 eassert (CONSP (Vmessage_stack));
10516 Vmessage_stack = XCDR (Vmessage_stack);
10520 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10521 exits. If the stack is not empty, we have a missing pop_message
10522 somewhere. */
10524 void
10525 check_message_stack (void)
10527 if (!NILP (Vmessage_stack))
10528 emacs_abort ();
10532 /* Truncate to NCHARS what will be displayed in the echo area the next
10533 time we display it---but don't redisplay it now. */
10535 void
10536 truncate_echo_area (ptrdiff_t nchars)
10538 if (nchars == 0)
10539 echo_area_buffer[0] = Qnil;
10540 /* A null message buffer means that the frame hasn't really been
10541 initialized yet. Error messages get reported properly by
10542 cmd_error, so this must be just an informative message; toss it. */
10543 else if (!noninteractive
10544 && INTERACTIVE
10545 && !NILP (echo_area_buffer[0]))
10547 struct frame *sf = SELECTED_FRAME ();
10548 if (FRAME_MESSAGE_BUF (sf))
10549 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10554 /* Helper function for truncate_echo_area. Truncate the current
10555 message to at most NCHARS characters. */
10557 static int
10558 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10560 if (BEG + nchars < Z)
10561 del_range (BEG + nchars, Z);
10562 if (Z == BEG)
10563 echo_area_buffer[0] = Qnil;
10564 return 0;
10567 /* Set the current message to a substring of S or STRING.
10569 If STRING is a Lisp string, set the message to the first NBYTES
10570 bytes from STRING. NBYTES zero means use the whole string. If
10571 STRING is multibyte, the message will be displayed multibyte.
10573 If S is not null, set the message to the first LEN bytes of S. LEN
10574 zero means use the whole string. MULTIBYTE_P non-zero means S is
10575 multibyte. Display the message multibyte in that case.
10577 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10578 to t before calling set_message_1 (which calls insert).
10581 static void
10582 set_message (const char *s, Lisp_Object string,
10583 ptrdiff_t nbytes, int multibyte_p)
10585 message_enable_multibyte
10586 = ((s && multibyte_p)
10587 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10589 with_echo_area_buffer (0, -1, set_message_1,
10590 (intptr_t) s, string, nbytes, multibyte_p);
10591 message_buf_print = 0;
10592 help_echo_showing_p = 0;
10594 if (STRINGP (Vdebug_on_message)
10595 && fast_string_match (Vdebug_on_message, string) >= 0)
10596 call_debugger (list2 (Qerror, string));
10600 /* Helper function for set_message. Arguments have the same meaning
10601 as there, with A1 corresponding to S and A2 corresponding to STRING
10602 This function is called with the echo area buffer being
10603 current. */
10605 static int
10606 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10608 intptr_t i1 = a1;
10609 const char *s = (const char *) i1;
10610 const unsigned char *msg = (const unsigned char *) s;
10611 Lisp_Object string = a2;
10613 /* Change multibyteness of the echo buffer appropriately. */
10614 if (message_enable_multibyte
10615 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10616 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10618 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10619 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10620 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10622 /* Insert new message at BEG. */
10623 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10625 if (STRINGP (string))
10627 ptrdiff_t nchars;
10629 if (nbytes == 0)
10630 nbytes = SBYTES (string);
10631 nchars = string_byte_to_char (string, nbytes);
10633 /* This function takes care of single/multibyte conversion. We
10634 just have to ensure that the echo area buffer has the right
10635 setting of enable_multibyte_characters. */
10636 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10638 else if (s)
10640 if (nbytes == 0)
10641 nbytes = strlen (s);
10643 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10645 /* Convert from multi-byte to single-byte. */
10646 ptrdiff_t i;
10647 int c, n;
10648 char work[1];
10650 /* Convert a multibyte string to single-byte. */
10651 for (i = 0; i < nbytes; i += n)
10653 c = string_char_and_length (msg + i, &n);
10654 work[0] = (ASCII_CHAR_P (c)
10656 : multibyte_char_to_unibyte (c));
10657 insert_1_both (work, 1, 1, 1, 0, 0);
10660 else if (!multibyte_p
10661 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10663 /* Convert from single-byte to multi-byte. */
10664 ptrdiff_t i;
10665 int c, n;
10666 unsigned char str[MAX_MULTIBYTE_LENGTH];
10668 /* Convert a single-byte string to multibyte. */
10669 for (i = 0; i < nbytes; i++)
10671 c = msg[i];
10672 MAKE_CHAR_MULTIBYTE (c);
10673 n = CHAR_STRING (c, str);
10674 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10677 else
10678 insert_1 (s, nbytes, 1, 0, 0);
10681 return 0;
10685 /* Clear messages. CURRENT_P non-zero means clear the current
10686 message. LAST_DISPLAYED_P non-zero means clear the message
10687 last displayed. */
10689 void
10690 clear_message (int current_p, int last_displayed_p)
10692 if (current_p)
10694 echo_area_buffer[0] = Qnil;
10695 message_cleared_p = 1;
10698 if (last_displayed_p)
10699 echo_area_buffer[1] = Qnil;
10701 message_buf_print = 0;
10704 /* Clear garbaged frames.
10706 This function is used where the old redisplay called
10707 redraw_garbaged_frames which in turn called redraw_frame which in
10708 turn called clear_frame. The call to clear_frame was a source of
10709 flickering. I believe a clear_frame is not necessary. It should
10710 suffice in the new redisplay to invalidate all current matrices,
10711 and ensure a complete redisplay of all windows. */
10713 static void
10714 clear_garbaged_frames (void)
10716 if (frame_garbaged)
10718 Lisp_Object tail, frame;
10719 int changed_count = 0;
10721 FOR_EACH_FRAME (tail, frame)
10723 struct frame *f = XFRAME (frame);
10725 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10727 if (f->resized_p)
10729 Fredraw_frame (frame);
10730 f->force_flush_display_p = 1;
10732 clear_current_matrices (f);
10733 changed_count++;
10734 f->garbaged = 0;
10735 f->resized_p = 0;
10739 frame_garbaged = 0;
10740 if (changed_count)
10741 ++windows_or_buffers_changed;
10746 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10747 is non-zero update selected_frame. Value is non-zero if the
10748 mini-windows height has been changed. */
10750 static int
10751 echo_area_display (int update_frame_p)
10753 Lisp_Object mini_window;
10754 struct window *w;
10755 struct frame *f;
10756 int window_height_changed_p = 0;
10757 struct frame *sf = SELECTED_FRAME ();
10759 mini_window = FRAME_MINIBUF_WINDOW (sf);
10760 w = XWINDOW (mini_window);
10761 f = XFRAME (WINDOW_FRAME (w));
10763 /* Don't display if frame is invisible or not yet initialized. */
10764 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10765 return 0;
10767 #ifdef HAVE_WINDOW_SYSTEM
10768 /* When Emacs starts, selected_frame may be the initial terminal
10769 frame. If we let this through, a message would be displayed on
10770 the terminal. */
10771 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10772 return 0;
10773 #endif /* HAVE_WINDOW_SYSTEM */
10775 /* Redraw garbaged frames. */
10776 if (frame_garbaged)
10777 clear_garbaged_frames ();
10779 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10781 echo_area_window = mini_window;
10782 window_height_changed_p = display_echo_area (w);
10783 w->must_be_updated_p = 1;
10785 /* Update the display, unless called from redisplay_internal.
10786 Also don't update the screen during redisplay itself. The
10787 update will happen at the end of redisplay, and an update
10788 here could cause confusion. */
10789 if (update_frame_p && !redisplaying_p)
10791 int n = 0;
10793 /* If the display update has been interrupted by pending
10794 input, update mode lines in the frame. Due to the
10795 pending input, it might have been that redisplay hasn't
10796 been called, so that mode lines above the echo area are
10797 garbaged. This looks odd, so we prevent it here. */
10798 if (!display_completed)
10799 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10801 if (window_height_changed_p
10802 /* Don't do this if Emacs is shutting down. Redisplay
10803 needs to run hooks. */
10804 && !NILP (Vrun_hooks))
10806 /* Must update other windows. Likewise as in other
10807 cases, don't let this update be interrupted by
10808 pending input. */
10809 ptrdiff_t count = SPECPDL_INDEX ();
10810 specbind (Qredisplay_dont_pause, Qt);
10811 windows_or_buffers_changed = 1;
10812 redisplay_internal ();
10813 unbind_to (count, Qnil);
10815 else if (FRAME_WINDOW_P (f) && n == 0)
10817 /* Window configuration is the same as before.
10818 Can do with a display update of the echo area,
10819 unless we displayed some mode lines. */
10820 update_single_window (w, 1);
10821 FRAME_RIF (f)->flush_display (f);
10823 else
10824 update_frame (f, 1, 1);
10826 /* If cursor is in the echo area, make sure that the next
10827 redisplay displays the minibuffer, so that the cursor will
10828 be replaced with what the minibuffer wants. */
10829 if (cursor_in_echo_area)
10830 ++windows_or_buffers_changed;
10833 else if (!EQ (mini_window, selected_window))
10834 windows_or_buffers_changed++;
10836 /* Last displayed message is now the current message. */
10837 echo_area_buffer[1] = echo_area_buffer[0];
10838 /* Inform read_char that we're not echoing. */
10839 echo_message_buffer = Qnil;
10841 /* Prevent redisplay optimization in redisplay_internal by resetting
10842 this_line_start_pos. This is done because the mini-buffer now
10843 displays the message instead of its buffer text. */
10844 if (EQ (mini_window, selected_window))
10845 CHARPOS (this_line_start_pos) = 0;
10847 return window_height_changed_p;
10852 /***********************************************************************
10853 Mode Lines and Frame Titles
10854 ***********************************************************************/
10856 /* A buffer for constructing non-propertized mode-line strings and
10857 frame titles in it; allocated from the heap in init_xdisp and
10858 resized as needed in store_mode_line_noprop_char. */
10860 static char *mode_line_noprop_buf;
10862 /* The buffer's end, and a current output position in it. */
10864 static char *mode_line_noprop_buf_end;
10865 static char *mode_line_noprop_ptr;
10867 #define MODE_LINE_NOPROP_LEN(start) \
10868 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10870 static enum {
10871 MODE_LINE_DISPLAY = 0,
10872 MODE_LINE_TITLE,
10873 MODE_LINE_NOPROP,
10874 MODE_LINE_STRING
10875 } mode_line_target;
10877 /* Alist that caches the results of :propertize.
10878 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10879 static Lisp_Object mode_line_proptrans_alist;
10881 /* List of strings making up the mode-line. */
10882 static Lisp_Object mode_line_string_list;
10884 /* Base face property when building propertized mode line string. */
10885 static Lisp_Object mode_line_string_face;
10886 static Lisp_Object mode_line_string_face_prop;
10889 /* Unwind data for mode line strings */
10891 static Lisp_Object Vmode_line_unwind_vector;
10893 static Lisp_Object
10894 format_mode_line_unwind_data (struct frame *target_frame,
10895 struct buffer *obuf,
10896 Lisp_Object owin,
10897 int save_proptrans)
10899 Lisp_Object vector, tmp;
10901 /* Reduce consing by keeping one vector in
10902 Vwith_echo_area_save_vector. */
10903 vector = Vmode_line_unwind_vector;
10904 Vmode_line_unwind_vector = Qnil;
10906 if (NILP (vector))
10907 vector = Fmake_vector (make_number (10), Qnil);
10909 ASET (vector, 0, make_number (mode_line_target));
10910 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10911 ASET (vector, 2, mode_line_string_list);
10912 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10913 ASET (vector, 4, mode_line_string_face);
10914 ASET (vector, 5, mode_line_string_face_prop);
10916 if (obuf)
10917 XSETBUFFER (tmp, obuf);
10918 else
10919 tmp = Qnil;
10920 ASET (vector, 6, tmp);
10921 ASET (vector, 7, owin);
10922 if (target_frame)
10924 /* Similarly to `with-selected-window', if the operation selects
10925 a window on another frame, we must restore that frame's
10926 selected window, and (for a tty) the top-frame. */
10927 ASET (vector, 8, target_frame->selected_window);
10928 if (FRAME_TERMCAP_P (target_frame))
10929 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10932 return vector;
10935 static Lisp_Object
10936 unwind_format_mode_line (Lisp_Object vector)
10938 Lisp_Object old_window = AREF (vector, 7);
10939 Lisp_Object target_frame_window = AREF (vector, 8);
10940 Lisp_Object old_top_frame = AREF (vector, 9);
10942 mode_line_target = XINT (AREF (vector, 0));
10943 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10944 mode_line_string_list = AREF (vector, 2);
10945 if (! EQ (AREF (vector, 3), Qt))
10946 mode_line_proptrans_alist = AREF (vector, 3);
10947 mode_line_string_face = AREF (vector, 4);
10948 mode_line_string_face_prop = AREF (vector, 5);
10950 /* Select window before buffer, since it may change the buffer. */
10951 if (!NILP (old_window))
10953 /* If the operation that we are unwinding had selected a window
10954 on a different frame, reset its frame-selected-window. For a
10955 text terminal, reset its top-frame if necessary. */
10956 if (!NILP (target_frame_window))
10958 Lisp_Object frame
10959 = WINDOW_FRAME (XWINDOW (target_frame_window));
10961 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10962 Fselect_window (target_frame_window, Qt);
10964 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10965 Fselect_frame (old_top_frame, Qt);
10968 Fselect_window (old_window, Qt);
10971 if (!NILP (AREF (vector, 6)))
10973 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10974 ASET (vector, 6, Qnil);
10977 Vmode_line_unwind_vector = vector;
10978 return Qnil;
10982 /* Store a single character C for the frame title in mode_line_noprop_buf.
10983 Re-allocate mode_line_noprop_buf if necessary. */
10985 static void
10986 store_mode_line_noprop_char (char c)
10988 /* If output position has reached the end of the allocated buffer,
10989 increase the buffer's size. */
10990 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10992 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10993 ptrdiff_t size = len;
10994 mode_line_noprop_buf =
10995 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10996 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10997 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11000 *mode_line_noprop_ptr++ = c;
11004 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11005 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11006 characters that yield more columns than PRECISION; PRECISION <= 0
11007 means copy the whole string. Pad with spaces until FIELD_WIDTH
11008 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11009 pad. Called from display_mode_element when it is used to build a
11010 frame title. */
11012 static int
11013 store_mode_line_noprop (const char *string, int field_width, int precision)
11015 const unsigned char *str = (const unsigned char *) string;
11016 int n = 0;
11017 ptrdiff_t dummy, nbytes;
11019 /* Copy at most PRECISION chars from STR. */
11020 nbytes = strlen (string);
11021 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11022 while (nbytes--)
11023 store_mode_line_noprop_char (*str++);
11025 /* Fill up with spaces until FIELD_WIDTH reached. */
11026 while (field_width > 0
11027 && n < field_width)
11029 store_mode_line_noprop_char (' ');
11030 ++n;
11033 return n;
11036 /***********************************************************************
11037 Frame Titles
11038 ***********************************************************************/
11040 #ifdef HAVE_WINDOW_SYSTEM
11042 /* Set the title of FRAME, if it has changed. The title format is
11043 Vicon_title_format if FRAME is iconified, otherwise it is
11044 frame_title_format. */
11046 static void
11047 x_consider_frame_title (Lisp_Object frame)
11049 struct frame *f = XFRAME (frame);
11051 if (FRAME_WINDOW_P (f)
11052 || FRAME_MINIBUF_ONLY_P (f)
11053 || f->explicit_name)
11055 /* Do we have more than one visible frame on this X display? */
11056 Lisp_Object tail;
11057 Lisp_Object fmt;
11058 ptrdiff_t title_start;
11059 char *title;
11060 ptrdiff_t len;
11061 struct it it;
11062 ptrdiff_t count = SPECPDL_INDEX ();
11064 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11066 Lisp_Object other_frame = XCAR (tail);
11067 struct frame *tf = XFRAME (other_frame);
11069 if (tf != f
11070 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11071 && !FRAME_MINIBUF_ONLY_P (tf)
11072 && !EQ (other_frame, tip_frame)
11073 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11074 break;
11077 /* Set global variable indicating that multiple frames exist. */
11078 multiple_frames = CONSP (tail);
11080 /* Switch to the buffer of selected window of the frame. Set up
11081 mode_line_target so that display_mode_element will output into
11082 mode_line_noprop_buf; then display the title. */
11083 record_unwind_protect (unwind_format_mode_line,
11084 format_mode_line_unwind_data
11085 (f, current_buffer, selected_window, 0));
11087 Fselect_window (f->selected_window, Qt);
11088 set_buffer_internal_1
11089 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11090 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11092 mode_line_target = MODE_LINE_TITLE;
11093 title_start = MODE_LINE_NOPROP_LEN (0);
11094 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11095 NULL, DEFAULT_FACE_ID);
11096 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11097 len = MODE_LINE_NOPROP_LEN (title_start);
11098 title = mode_line_noprop_buf + title_start;
11099 unbind_to (count, Qnil);
11101 /* Set the title only if it's changed. This avoids consing in
11102 the common case where it hasn't. (If it turns out that we've
11103 already wasted too much time by walking through the list with
11104 display_mode_element, then we might need to optimize at a
11105 higher level than this.) */
11106 if (! STRINGP (f->name)
11107 || SBYTES (f->name) != len
11108 || memcmp (title, SDATA (f->name), len) != 0)
11109 x_implicitly_set_name (f, make_string (title, len), Qnil);
11113 #endif /* not HAVE_WINDOW_SYSTEM */
11116 /***********************************************************************
11117 Menu Bars
11118 ***********************************************************************/
11121 /* Prepare for redisplay by updating menu-bar item lists when
11122 appropriate. This can call eval. */
11124 void
11125 prepare_menu_bars (void)
11127 int all_windows;
11128 struct gcpro gcpro1, gcpro2;
11129 struct frame *f;
11130 Lisp_Object tooltip_frame;
11132 #ifdef HAVE_WINDOW_SYSTEM
11133 tooltip_frame = tip_frame;
11134 #else
11135 tooltip_frame = Qnil;
11136 #endif
11138 /* Update all frame titles based on their buffer names, etc. We do
11139 this before the menu bars so that the buffer-menu will show the
11140 up-to-date frame titles. */
11141 #ifdef HAVE_WINDOW_SYSTEM
11142 if (windows_or_buffers_changed || update_mode_lines)
11144 Lisp_Object tail, frame;
11146 FOR_EACH_FRAME (tail, frame)
11148 f = XFRAME (frame);
11149 if (!EQ (frame, tooltip_frame)
11150 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11151 x_consider_frame_title (frame);
11154 #endif /* HAVE_WINDOW_SYSTEM */
11156 /* Update the menu bar item lists, if appropriate. This has to be
11157 done before any actual redisplay or generation of display lines. */
11158 all_windows = (update_mode_lines
11159 || buffer_shared > 1
11160 || windows_or_buffers_changed);
11161 if (all_windows)
11163 Lisp_Object tail, frame;
11164 ptrdiff_t count = SPECPDL_INDEX ();
11165 /* 1 means that update_menu_bar has run its hooks
11166 so any further calls to update_menu_bar shouldn't do so again. */
11167 int menu_bar_hooks_run = 0;
11169 record_unwind_save_match_data ();
11171 FOR_EACH_FRAME (tail, frame)
11173 f = XFRAME (frame);
11175 /* Ignore tooltip frame. */
11176 if (EQ (frame, tooltip_frame))
11177 continue;
11179 /* If a window on this frame changed size, report that to
11180 the user and clear the size-change flag. */
11181 if (FRAME_WINDOW_SIZES_CHANGED (f))
11183 Lisp_Object functions;
11185 /* Clear flag first in case we get an error below. */
11186 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11187 functions = Vwindow_size_change_functions;
11188 GCPRO2 (tail, functions);
11190 while (CONSP (functions))
11192 if (!EQ (XCAR (functions), Qt))
11193 call1 (XCAR (functions), frame);
11194 functions = XCDR (functions);
11196 UNGCPRO;
11199 GCPRO1 (tail);
11200 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11201 #ifdef HAVE_WINDOW_SYSTEM
11202 update_tool_bar (f, 0);
11203 #endif
11204 #ifdef HAVE_NS
11205 if (windows_or_buffers_changed
11206 && FRAME_NS_P (f))
11207 ns_set_doc_edited
11208 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11209 #endif
11210 UNGCPRO;
11213 unbind_to (count, Qnil);
11215 else
11217 struct frame *sf = SELECTED_FRAME ();
11218 update_menu_bar (sf, 1, 0);
11219 #ifdef HAVE_WINDOW_SYSTEM
11220 update_tool_bar (sf, 1);
11221 #endif
11226 /* Update the menu bar item list for frame F. This has to be done
11227 before we start to fill in any display lines, because it can call
11228 eval.
11230 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11232 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11233 already ran the menu bar hooks for this redisplay, so there
11234 is no need to run them again. The return value is the
11235 updated value of this flag, to pass to the next call. */
11237 static int
11238 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11240 Lisp_Object window;
11241 register struct window *w;
11243 /* If called recursively during a menu update, do nothing. This can
11244 happen when, for instance, an activate-menubar-hook causes a
11245 redisplay. */
11246 if (inhibit_menubar_update)
11247 return hooks_run;
11249 window = FRAME_SELECTED_WINDOW (f);
11250 w = XWINDOW (window);
11252 if (FRAME_WINDOW_P (f)
11254 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11255 || defined (HAVE_NS) || defined (USE_GTK)
11256 FRAME_EXTERNAL_MENU_BAR (f)
11257 #else
11258 FRAME_MENU_BAR_LINES (f) > 0
11259 #endif
11260 : FRAME_MENU_BAR_LINES (f) > 0)
11262 /* If the user has switched buffers or windows, we need to
11263 recompute to reflect the new bindings. But we'll
11264 recompute when update_mode_lines is set too; that means
11265 that people can use force-mode-line-update to request
11266 that the menu bar be recomputed. The adverse effect on
11267 the rest of the redisplay algorithm is about the same as
11268 windows_or_buffers_changed anyway. */
11269 if (windows_or_buffers_changed
11270 /* This used to test w->update_mode_line, but we believe
11271 there is no need to recompute the menu in that case. */
11272 || update_mode_lines
11273 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11274 < BUF_MODIFF (XBUFFER (w->buffer)))
11275 != w->last_had_star)
11276 || ((!NILP (Vtransient_mark_mode)
11277 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11278 != !NILP (w->region_showing)))
11280 struct buffer *prev = current_buffer;
11281 ptrdiff_t count = SPECPDL_INDEX ();
11283 specbind (Qinhibit_menubar_update, Qt);
11285 set_buffer_internal_1 (XBUFFER (w->buffer));
11286 if (save_match_data)
11287 record_unwind_save_match_data ();
11288 if (NILP (Voverriding_local_map_menu_flag))
11290 specbind (Qoverriding_terminal_local_map, Qnil);
11291 specbind (Qoverriding_local_map, Qnil);
11294 if (!hooks_run)
11296 /* Run the Lucid hook. */
11297 safe_run_hooks (Qactivate_menubar_hook);
11299 /* If it has changed current-menubar from previous value,
11300 really recompute the menu-bar from the value. */
11301 if (! NILP (Vlucid_menu_bar_dirty_flag))
11302 call0 (Qrecompute_lucid_menubar);
11304 safe_run_hooks (Qmenu_bar_update_hook);
11306 hooks_run = 1;
11309 XSETFRAME (Vmenu_updating_frame, f);
11310 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11312 /* Redisplay the menu bar in case we changed it. */
11313 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11314 || defined (HAVE_NS) || defined (USE_GTK)
11315 if (FRAME_WINDOW_P (f))
11317 #if defined (HAVE_NS)
11318 /* All frames on Mac OS share the same menubar. So only
11319 the selected frame should be allowed to set it. */
11320 if (f == SELECTED_FRAME ())
11321 #endif
11322 set_frame_menubar (f, 0, 0);
11324 else
11325 /* On a terminal screen, the menu bar is an ordinary screen
11326 line, and this makes it get updated. */
11327 w->update_mode_line = 1;
11328 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11329 /* In the non-toolkit version, the menu bar is an ordinary screen
11330 line, and this makes it get updated. */
11331 w->update_mode_line = 1;
11332 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11334 unbind_to (count, Qnil);
11335 set_buffer_internal_1 (prev);
11339 return hooks_run;
11344 /***********************************************************************
11345 Output Cursor
11346 ***********************************************************************/
11348 #ifdef HAVE_WINDOW_SYSTEM
11350 /* EXPORT:
11351 Nominal cursor position -- where to draw output.
11352 HPOS and VPOS are window relative glyph matrix coordinates.
11353 X and Y are window relative pixel coordinates. */
11355 struct cursor_pos output_cursor;
11358 /* EXPORT:
11359 Set the global variable output_cursor to CURSOR. All cursor
11360 positions are relative to updated_window. */
11362 void
11363 set_output_cursor (struct cursor_pos *cursor)
11365 output_cursor.hpos = cursor->hpos;
11366 output_cursor.vpos = cursor->vpos;
11367 output_cursor.x = cursor->x;
11368 output_cursor.y = cursor->y;
11372 /* EXPORT for RIF:
11373 Set a nominal cursor position.
11375 HPOS and VPOS are column/row positions in a window glyph matrix. X
11376 and Y are window text area relative pixel positions.
11378 If this is done during an update, updated_window will contain the
11379 window that is being updated and the position is the future output
11380 cursor position for that window. If updated_window is null, use
11381 selected_window and display the cursor at the given position. */
11383 void
11384 x_cursor_to (int vpos, int hpos, int y, int x)
11386 struct window *w;
11388 /* If updated_window is not set, work on selected_window. */
11389 if (updated_window)
11390 w = updated_window;
11391 else
11392 w = XWINDOW (selected_window);
11394 /* Set the output cursor. */
11395 output_cursor.hpos = hpos;
11396 output_cursor.vpos = vpos;
11397 output_cursor.x = x;
11398 output_cursor.y = y;
11400 /* If not called as part of an update, really display the cursor.
11401 This will also set the cursor position of W. */
11402 if (updated_window == NULL)
11404 BLOCK_INPUT;
11405 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11406 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11407 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11408 UNBLOCK_INPUT;
11412 #endif /* HAVE_WINDOW_SYSTEM */
11415 /***********************************************************************
11416 Tool-bars
11417 ***********************************************************************/
11419 #ifdef HAVE_WINDOW_SYSTEM
11421 /* Where the mouse was last time we reported a mouse event. */
11423 FRAME_PTR last_mouse_frame;
11425 /* Tool-bar item index of the item on which a mouse button was pressed
11426 or -1. */
11428 int last_tool_bar_item;
11431 static Lisp_Object
11432 update_tool_bar_unwind (Lisp_Object frame)
11434 selected_frame = frame;
11435 return Qnil;
11438 /* Update the tool-bar item list for frame F. This has to be done
11439 before we start to fill in any display lines. Called from
11440 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11441 and restore it here. */
11443 static void
11444 update_tool_bar (struct frame *f, int save_match_data)
11446 #if defined (USE_GTK) || defined (HAVE_NS)
11447 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11448 #else
11449 int do_update = WINDOWP (f->tool_bar_window)
11450 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11451 #endif
11453 if (do_update)
11455 Lisp_Object window;
11456 struct window *w;
11458 window = FRAME_SELECTED_WINDOW (f);
11459 w = XWINDOW (window);
11461 /* If the user has switched buffers or windows, we need to
11462 recompute to reflect the new bindings. But we'll
11463 recompute when update_mode_lines is set too; that means
11464 that people can use force-mode-line-update to request
11465 that the menu bar be recomputed. The adverse effect on
11466 the rest of the redisplay algorithm is about the same as
11467 windows_or_buffers_changed anyway. */
11468 if (windows_or_buffers_changed
11469 || w->update_mode_line
11470 || update_mode_lines
11471 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11472 < BUF_MODIFF (XBUFFER (w->buffer)))
11473 != w->last_had_star)
11474 || ((!NILP (Vtransient_mark_mode)
11475 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11476 != !NILP (w->region_showing)))
11478 struct buffer *prev = current_buffer;
11479 ptrdiff_t count = SPECPDL_INDEX ();
11480 Lisp_Object frame, new_tool_bar;
11481 int new_n_tool_bar;
11482 struct gcpro gcpro1;
11484 /* Set current_buffer to the buffer of the selected
11485 window of the frame, so that we get the right local
11486 keymaps. */
11487 set_buffer_internal_1 (XBUFFER (w->buffer));
11489 /* Save match data, if we must. */
11490 if (save_match_data)
11491 record_unwind_save_match_data ();
11493 /* Make sure that we don't accidentally use bogus keymaps. */
11494 if (NILP (Voverriding_local_map_menu_flag))
11496 specbind (Qoverriding_terminal_local_map, Qnil);
11497 specbind (Qoverriding_local_map, Qnil);
11500 GCPRO1 (new_tool_bar);
11502 /* We must temporarily set the selected frame to this frame
11503 before calling tool_bar_items, because the calculation of
11504 the tool-bar keymap uses the selected frame (see
11505 `tool-bar-make-keymap' in tool-bar.el). */
11506 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11507 XSETFRAME (frame, f);
11508 selected_frame = frame;
11510 /* Build desired tool-bar items from keymaps. */
11511 new_tool_bar
11512 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11513 &new_n_tool_bar);
11515 /* Redisplay the tool-bar if we changed it. */
11516 if (new_n_tool_bar != f->n_tool_bar_items
11517 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11519 /* Redisplay that happens asynchronously due to an expose event
11520 may access f->tool_bar_items. Make sure we update both
11521 variables within BLOCK_INPUT so no such event interrupts. */
11522 BLOCK_INPUT;
11523 fset_tool_bar_items (f, new_tool_bar);
11524 f->n_tool_bar_items = new_n_tool_bar;
11525 w->update_mode_line = 1;
11526 UNBLOCK_INPUT;
11529 UNGCPRO;
11531 unbind_to (count, Qnil);
11532 set_buffer_internal_1 (prev);
11538 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11539 F's desired tool-bar contents. F->tool_bar_items must have
11540 been set up previously by calling prepare_menu_bars. */
11542 static void
11543 build_desired_tool_bar_string (struct frame *f)
11545 int i, size, size_needed;
11546 struct gcpro gcpro1, gcpro2, gcpro3;
11547 Lisp_Object image, plist, props;
11549 image = plist = props = Qnil;
11550 GCPRO3 (image, plist, props);
11552 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11553 Otherwise, make a new string. */
11555 /* The size of the string we might be able to reuse. */
11556 size = (STRINGP (f->desired_tool_bar_string)
11557 ? SCHARS (f->desired_tool_bar_string)
11558 : 0);
11560 /* We need one space in the string for each image. */
11561 size_needed = f->n_tool_bar_items;
11563 /* Reuse f->desired_tool_bar_string, if possible. */
11564 if (size < size_needed || NILP (f->desired_tool_bar_string))
11565 fset_desired_tool_bar_string
11566 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11567 else
11569 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11570 Fremove_text_properties (make_number (0), make_number (size),
11571 props, f->desired_tool_bar_string);
11574 /* Put a `display' property on the string for the images to display,
11575 put a `menu_item' property on tool-bar items with a value that
11576 is the index of the item in F's tool-bar item vector. */
11577 for (i = 0; i < f->n_tool_bar_items; ++i)
11579 #define PROP(IDX) \
11580 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11582 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11583 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11584 int hmargin, vmargin, relief, idx, end;
11586 /* If image is a vector, choose the image according to the
11587 button state. */
11588 image = PROP (TOOL_BAR_ITEM_IMAGES);
11589 if (VECTORP (image))
11591 if (enabled_p)
11592 idx = (selected_p
11593 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11594 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11595 else
11596 idx = (selected_p
11597 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11598 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11600 eassert (ASIZE (image) >= idx);
11601 image = AREF (image, idx);
11603 else
11604 idx = -1;
11606 /* Ignore invalid image specifications. */
11607 if (!valid_image_p (image))
11608 continue;
11610 /* Display the tool-bar button pressed, or depressed. */
11611 plist = Fcopy_sequence (XCDR (image));
11613 /* Compute margin and relief to draw. */
11614 relief = (tool_bar_button_relief >= 0
11615 ? tool_bar_button_relief
11616 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11617 hmargin = vmargin = relief;
11619 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11620 INT_MAX - max (hmargin, vmargin)))
11622 hmargin += XFASTINT (Vtool_bar_button_margin);
11623 vmargin += XFASTINT (Vtool_bar_button_margin);
11625 else if (CONSP (Vtool_bar_button_margin))
11627 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11628 INT_MAX - hmargin))
11629 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11631 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11632 INT_MAX - vmargin))
11633 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11636 if (auto_raise_tool_bar_buttons_p)
11638 /* Add a `:relief' property to the image spec if the item is
11639 selected. */
11640 if (selected_p)
11642 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11643 hmargin -= relief;
11644 vmargin -= relief;
11647 else
11649 /* If image is selected, display it pressed, i.e. with a
11650 negative relief. If it's not selected, display it with a
11651 raised relief. */
11652 plist = Fplist_put (plist, QCrelief,
11653 (selected_p
11654 ? make_number (-relief)
11655 : make_number (relief)));
11656 hmargin -= relief;
11657 vmargin -= relief;
11660 /* Put a margin around the image. */
11661 if (hmargin || vmargin)
11663 if (hmargin == vmargin)
11664 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11665 else
11666 plist = Fplist_put (plist, QCmargin,
11667 Fcons (make_number (hmargin),
11668 make_number (vmargin)));
11671 /* If button is not enabled, and we don't have special images
11672 for the disabled state, make the image appear disabled by
11673 applying an appropriate algorithm to it. */
11674 if (!enabled_p && idx < 0)
11675 plist = Fplist_put (plist, QCconversion, Qdisabled);
11677 /* Put a `display' text property on the string for the image to
11678 display. Put a `menu-item' property on the string that gives
11679 the start of this item's properties in the tool-bar items
11680 vector. */
11681 image = Fcons (Qimage, plist);
11682 props = list4 (Qdisplay, image,
11683 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11685 /* Let the last image hide all remaining spaces in the tool bar
11686 string. The string can be longer than needed when we reuse a
11687 previous string. */
11688 if (i + 1 == f->n_tool_bar_items)
11689 end = SCHARS (f->desired_tool_bar_string);
11690 else
11691 end = i + 1;
11692 Fadd_text_properties (make_number (i), make_number (end),
11693 props, f->desired_tool_bar_string);
11694 #undef PROP
11697 UNGCPRO;
11701 /* Display one line of the tool-bar of frame IT->f.
11703 HEIGHT specifies the desired height of the tool-bar line.
11704 If the actual height of the glyph row is less than HEIGHT, the
11705 row's height is increased to HEIGHT, and the icons are centered
11706 vertically in the new height.
11708 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11709 count a final empty row in case the tool-bar width exactly matches
11710 the window width.
11713 static void
11714 display_tool_bar_line (struct it *it, int height)
11716 struct glyph_row *row = it->glyph_row;
11717 int max_x = it->last_visible_x;
11718 struct glyph *last;
11720 prepare_desired_row (row);
11721 row->y = it->current_y;
11723 /* Note that this isn't made use of if the face hasn't a box,
11724 so there's no need to check the face here. */
11725 it->start_of_box_run_p = 1;
11727 while (it->current_x < max_x)
11729 int x, n_glyphs_before, i, nglyphs;
11730 struct it it_before;
11732 /* Get the next display element. */
11733 if (!get_next_display_element (it))
11735 /* Don't count empty row if we are counting needed tool-bar lines. */
11736 if (height < 0 && !it->hpos)
11737 return;
11738 break;
11741 /* Produce glyphs. */
11742 n_glyphs_before = row->used[TEXT_AREA];
11743 it_before = *it;
11745 PRODUCE_GLYPHS (it);
11747 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11748 i = 0;
11749 x = it_before.current_x;
11750 while (i < nglyphs)
11752 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11754 if (x + glyph->pixel_width > max_x)
11756 /* Glyph doesn't fit on line. Backtrack. */
11757 row->used[TEXT_AREA] = n_glyphs_before;
11758 *it = it_before;
11759 /* If this is the only glyph on this line, it will never fit on the
11760 tool-bar, so skip it. But ensure there is at least one glyph,
11761 so we don't accidentally disable the tool-bar. */
11762 if (n_glyphs_before == 0
11763 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11764 break;
11765 goto out;
11768 ++it->hpos;
11769 x += glyph->pixel_width;
11770 ++i;
11773 /* Stop at line end. */
11774 if (ITERATOR_AT_END_OF_LINE_P (it))
11775 break;
11777 set_iterator_to_next (it, 1);
11780 out:;
11782 row->displays_text_p = row->used[TEXT_AREA] != 0;
11784 /* Use default face for the border below the tool bar.
11786 FIXME: When auto-resize-tool-bars is grow-only, there is
11787 no additional border below the possibly empty tool-bar lines.
11788 So to make the extra empty lines look "normal", we have to
11789 use the tool-bar face for the border too. */
11790 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11791 it->face_id = DEFAULT_FACE_ID;
11793 extend_face_to_end_of_line (it);
11794 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11795 last->right_box_line_p = 1;
11796 if (last == row->glyphs[TEXT_AREA])
11797 last->left_box_line_p = 1;
11799 /* Make line the desired height and center it vertically. */
11800 if ((height -= it->max_ascent + it->max_descent) > 0)
11802 /* Don't add more than one line height. */
11803 height %= FRAME_LINE_HEIGHT (it->f);
11804 it->max_ascent += height / 2;
11805 it->max_descent += (height + 1) / 2;
11808 compute_line_metrics (it);
11810 /* If line is empty, make it occupy the rest of the tool-bar. */
11811 if (!row->displays_text_p)
11813 row->height = row->phys_height = it->last_visible_y - row->y;
11814 row->visible_height = row->height;
11815 row->ascent = row->phys_ascent = 0;
11816 row->extra_line_spacing = 0;
11819 row->full_width_p = 1;
11820 row->continued_p = 0;
11821 row->truncated_on_left_p = 0;
11822 row->truncated_on_right_p = 0;
11824 it->current_x = it->hpos = 0;
11825 it->current_y += row->height;
11826 ++it->vpos;
11827 ++it->glyph_row;
11831 /* Max tool-bar height. */
11833 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11834 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11836 /* Value is the number of screen lines needed to make all tool-bar
11837 items of frame F visible. The number of actual rows needed is
11838 returned in *N_ROWS if non-NULL. */
11840 static int
11841 tool_bar_lines_needed (struct frame *f, int *n_rows)
11843 struct window *w = XWINDOW (f->tool_bar_window);
11844 struct it it;
11845 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11846 the desired matrix, so use (unused) mode-line row as temporary row to
11847 avoid destroying the first tool-bar row. */
11848 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11850 /* Initialize an iterator for iteration over
11851 F->desired_tool_bar_string in the tool-bar window of frame F. */
11852 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11853 it.first_visible_x = 0;
11854 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11855 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11856 it.paragraph_embedding = L2R;
11858 while (!ITERATOR_AT_END_P (&it))
11860 clear_glyph_row (temp_row);
11861 it.glyph_row = temp_row;
11862 display_tool_bar_line (&it, -1);
11864 clear_glyph_row (temp_row);
11866 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11867 if (n_rows)
11868 *n_rows = it.vpos > 0 ? it.vpos : -1;
11870 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11874 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11875 0, 1, 0,
11876 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11877 (Lisp_Object frame)
11879 struct frame *f;
11880 struct window *w;
11881 int nlines = 0;
11883 if (NILP (frame))
11884 frame = selected_frame;
11885 else
11886 CHECK_FRAME (frame);
11887 f = XFRAME (frame);
11889 if (WINDOWP (f->tool_bar_window)
11890 && (w = XWINDOW (f->tool_bar_window),
11891 WINDOW_TOTAL_LINES (w) > 0))
11893 update_tool_bar (f, 1);
11894 if (f->n_tool_bar_items)
11896 build_desired_tool_bar_string (f);
11897 nlines = tool_bar_lines_needed (f, NULL);
11901 return make_number (nlines);
11905 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11906 height should be changed. */
11908 static int
11909 redisplay_tool_bar (struct frame *f)
11911 struct window *w;
11912 struct it it;
11913 struct glyph_row *row;
11915 #if defined (USE_GTK) || defined (HAVE_NS)
11916 if (FRAME_EXTERNAL_TOOL_BAR (f))
11917 update_frame_tool_bar (f);
11918 return 0;
11919 #endif
11921 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11922 do anything. This means you must start with tool-bar-lines
11923 non-zero to get the auto-sizing effect. Or in other words, you
11924 can turn off tool-bars by specifying tool-bar-lines zero. */
11925 if (!WINDOWP (f->tool_bar_window)
11926 || (w = XWINDOW (f->tool_bar_window),
11927 WINDOW_TOTAL_LINES (w) == 0))
11928 return 0;
11930 /* Set up an iterator for the tool-bar window. */
11931 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11932 it.first_visible_x = 0;
11933 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11934 row = it.glyph_row;
11936 /* Build a string that represents the contents of the tool-bar. */
11937 build_desired_tool_bar_string (f);
11938 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11939 /* FIXME: This should be controlled by a user option. But it
11940 doesn't make sense to have an R2L tool bar if the menu bar cannot
11941 be drawn also R2L, and making the menu bar R2L is tricky due
11942 toolkit-specific code that implements it. If an R2L tool bar is
11943 ever supported, display_tool_bar_line should also be augmented to
11944 call unproduce_glyphs like display_line and display_string
11945 do. */
11946 it.paragraph_embedding = L2R;
11948 if (f->n_tool_bar_rows == 0)
11950 int nlines;
11952 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11953 nlines != WINDOW_TOTAL_LINES (w)))
11955 Lisp_Object frame;
11956 int old_height = WINDOW_TOTAL_LINES (w);
11958 XSETFRAME (frame, f);
11959 Fmodify_frame_parameters (frame,
11960 Fcons (Fcons (Qtool_bar_lines,
11961 make_number (nlines)),
11962 Qnil));
11963 if (WINDOW_TOTAL_LINES (w) != old_height)
11965 clear_glyph_matrix (w->desired_matrix);
11966 fonts_changed_p = 1;
11967 return 1;
11972 /* Display as many lines as needed to display all tool-bar items. */
11974 if (f->n_tool_bar_rows > 0)
11976 int border, rows, height, extra;
11978 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11979 border = XINT (Vtool_bar_border);
11980 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11981 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11982 else if (EQ (Vtool_bar_border, Qborder_width))
11983 border = f->border_width;
11984 else
11985 border = 0;
11986 if (border < 0)
11987 border = 0;
11989 rows = f->n_tool_bar_rows;
11990 height = max (1, (it.last_visible_y - border) / rows);
11991 extra = it.last_visible_y - border - height * rows;
11993 while (it.current_y < it.last_visible_y)
11995 int h = 0;
11996 if (extra > 0 && rows-- > 0)
11998 h = (extra + rows - 1) / rows;
11999 extra -= h;
12001 display_tool_bar_line (&it, height + h);
12004 else
12006 while (it.current_y < it.last_visible_y)
12007 display_tool_bar_line (&it, 0);
12010 /* It doesn't make much sense to try scrolling in the tool-bar
12011 window, so don't do it. */
12012 w->desired_matrix->no_scrolling_p = 1;
12013 w->must_be_updated_p = 1;
12015 if (!NILP (Vauto_resize_tool_bars))
12017 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12018 int change_height_p = 0;
12020 /* If we couldn't display everything, change the tool-bar's
12021 height if there is room for more. */
12022 if (IT_STRING_CHARPOS (it) < it.end_charpos
12023 && it.current_y < max_tool_bar_height)
12024 change_height_p = 1;
12026 row = it.glyph_row - 1;
12028 /* If there are blank lines at the end, except for a partially
12029 visible blank line at the end that is smaller than
12030 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12031 if (!row->displays_text_p
12032 && row->height >= FRAME_LINE_HEIGHT (f))
12033 change_height_p = 1;
12035 /* If row displays tool-bar items, but is partially visible,
12036 change the tool-bar's height. */
12037 if (row->displays_text_p
12038 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12039 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12040 change_height_p = 1;
12042 /* Resize windows as needed by changing the `tool-bar-lines'
12043 frame parameter. */
12044 if (change_height_p)
12046 Lisp_Object frame;
12047 int old_height = WINDOW_TOTAL_LINES (w);
12048 int nrows;
12049 int nlines = tool_bar_lines_needed (f, &nrows);
12051 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12052 && !f->minimize_tool_bar_window_p)
12053 ? (nlines > old_height)
12054 : (nlines != old_height));
12055 f->minimize_tool_bar_window_p = 0;
12057 if (change_height_p)
12059 XSETFRAME (frame, f);
12060 Fmodify_frame_parameters (frame,
12061 Fcons (Fcons (Qtool_bar_lines,
12062 make_number (nlines)),
12063 Qnil));
12064 if (WINDOW_TOTAL_LINES (w) != old_height)
12066 clear_glyph_matrix (w->desired_matrix);
12067 f->n_tool_bar_rows = nrows;
12068 fonts_changed_p = 1;
12069 return 1;
12075 f->minimize_tool_bar_window_p = 0;
12076 return 0;
12080 /* Get information about the tool-bar item which is displayed in GLYPH
12081 on frame F. Return in *PROP_IDX the index where tool-bar item
12082 properties start in F->tool_bar_items. Value is zero if
12083 GLYPH doesn't display a tool-bar item. */
12085 static int
12086 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12088 Lisp_Object prop;
12089 int success_p;
12090 int charpos;
12092 /* This function can be called asynchronously, which means we must
12093 exclude any possibility that Fget_text_property signals an
12094 error. */
12095 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12096 charpos = max (0, charpos);
12098 /* Get the text property `menu-item' at pos. The value of that
12099 property is the start index of this item's properties in
12100 F->tool_bar_items. */
12101 prop = Fget_text_property (make_number (charpos),
12102 Qmenu_item, f->current_tool_bar_string);
12103 if (INTEGERP (prop))
12105 *prop_idx = XINT (prop);
12106 success_p = 1;
12108 else
12109 success_p = 0;
12111 return success_p;
12115 /* Get information about the tool-bar item at position X/Y on frame F.
12116 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12117 the current matrix of the tool-bar window of F, or NULL if not
12118 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12119 item in F->tool_bar_items. Value is
12121 -1 if X/Y is not on a tool-bar item
12122 0 if X/Y is on the same item that was highlighted before.
12123 1 otherwise. */
12125 static int
12126 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12127 int *hpos, int *vpos, int *prop_idx)
12129 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12130 struct window *w = XWINDOW (f->tool_bar_window);
12131 int area;
12133 /* Find the glyph under X/Y. */
12134 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12135 if (*glyph == NULL)
12136 return -1;
12138 /* Get the start of this tool-bar item's properties in
12139 f->tool_bar_items. */
12140 if (!tool_bar_item_info (f, *glyph, prop_idx))
12141 return -1;
12143 /* Is mouse on the highlighted item? */
12144 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12145 && *vpos >= hlinfo->mouse_face_beg_row
12146 && *vpos <= hlinfo->mouse_face_end_row
12147 && (*vpos > hlinfo->mouse_face_beg_row
12148 || *hpos >= hlinfo->mouse_face_beg_col)
12149 && (*vpos < hlinfo->mouse_face_end_row
12150 || *hpos < hlinfo->mouse_face_end_col
12151 || hlinfo->mouse_face_past_end))
12152 return 0;
12154 return 1;
12158 /* EXPORT:
12159 Handle mouse button event on the tool-bar of frame F, at
12160 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12161 0 for button release. MODIFIERS is event modifiers for button
12162 release. */
12164 void
12165 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12166 int modifiers)
12168 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12169 struct window *w = XWINDOW (f->tool_bar_window);
12170 int hpos, vpos, prop_idx;
12171 struct glyph *glyph;
12172 Lisp_Object enabled_p;
12174 /* If not on the highlighted tool-bar item, return. */
12175 frame_to_window_pixel_xy (w, &x, &y);
12176 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12177 return;
12179 /* If item is disabled, do nothing. */
12180 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12181 if (NILP (enabled_p))
12182 return;
12184 if (down_p)
12186 /* Show item in pressed state. */
12187 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12188 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12189 last_tool_bar_item = prop_idx;
12191 else
12193 Lisp_Object key, frame;
12194 struct input_event event;
12195 EVENT_INIT (event);
12197 /* Show item in released state. */
12198 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12199 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12201 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12203 XSETFRAME (frame, f);
12204 event.kind = TOOL_BAR_EVENT;
12205 event.frame_or_window = frame;
12206 event.arg = frame;
12207 kbd_buffer_store_event (&event);
12209 event.kind = TOOL_BAR_EVENT;
12210 event.frame_or_window = frame;
12211 event.arg = key;
12212 event.modifiers = modifiers;
12213 kbd_buffer_store_event (&event);
12214 last_tool_bar_item = -1;
12219 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12220 tool-bar window-relative coordinates X/Y. Called from
12221 note_mouse_highlight. */
12223 static void
12224 note_tool_bar_highlight (struct frame *f, int x, int y)
12226 Lisp_Object window = f->tool_bar_window;
12227 struct window *w = XWINDOW (window);
12228 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12229 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12230 int hpos, vpos;
12231 struct glyph *glyph;
12232 struct glyph_row *row;
12233 int i;
12234 Lisp_Object enabled_p;
12235 int prop_idx;
12236 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12237 int mouse_down_p, rc;
12239 /* Function note_mouse_highlight is called with negative X/Y
12240 values when mouse moves outside of the frame. */
12241 if (x <= 0 || y <= 0)
12243 clear_mouse_face (hlinfo);
12244 return;
12247 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12248 if (rc < 0)
12250 /* Not on tool-bar item. */
12251 clear_mouse_face (hlinfo);
12252 return;
12254 else if (rc == 0)
12255 /* On same tool-bar item as before. */
12256 goto set_help_echo;
12258 clear_mouse_face (hlinfo);
12260 /* Mouse is down, but on different tool-bar item? */
12261 mouse_down_p = (dpyinfo->grabbed
12262 && f == last_mouse_frame
12263 && FRAME_LIVE_P (f));
12264 if (mouse_down_p
12265 && last_tool_bar_item != prop_idx)
12266 return;
12268 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12269 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12271 /* If tool-bar item is not enabled, don't highlight it. */
12272 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12273 if (!NILP (enabled_p))
12275 /* Compute the x-position of the glyph. In front and past the
12276 image is a space. We include this in the highlighted area. */
12277 row = MATRIX_ROW (w->current_matrix, vpos);
12278 for (i = x = 0; i < hpos; ++i)
12279 x += row->glyphs[TEXT_AREA][i].pixel_width;
12281 /* Record this as the current active region. */
12282 hlinfo->mouse_face_beg_col = hpos;
12283 hlinfo->mouse_face_beg_row = vpos;
12284 hlinfo->mouse_face_beg_x = x;
12285 hlinfo->mouse_face_beg_y = row->y;
12286 hlinfo->mouse_face_past_end = 0;
12288 hlinfo->mouse_face_end_col = hpos + 1;
12289 hlinfo->mouse_face_end_row = vpos;
12290 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12291 hlinfo->mouse_face_end_y = row->y;
12292 hlinfo->mouse_face_window = window;
12293 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12295 /* Display it as active. */
12296 show_mouse_face (hlinfo, draw);
12297 hlinfo->mouse_face_image_state = draw;
12300 set_help_echo:
12302 /* Set help_echo_string to a help string to display for this tool-bar item.
12303 XTread_socket does the rest. */
12304 help_echo_object = help_echo_window = Qnil;
12305 help_echo_pos = -1;
12306 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12307 if (NILP (help_echo_string))
12308 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12311 #endif /* HAVE_WINDOW_SYSTEM */
12315 /************************************************************************
12316 Horizontal scrolling
12317 ************************************************************************/
12319 static int hscroll_window_tree (Lisp_Object);
12320 static int hscroll_windows (Lisp_Object);
12322 /* For all leaf windows in the window tree rooted at WINDOW, set their
12323 hscroll value so that PT is (i) visible in the window, and (ii) so
12324 that it is not within a certain margin at the window's left and
12325 right border. Value is non-zero if any window's hscroll has been
12326 changed. */
12328 static int
12329 hscroll_window_tree (Lisp_Object window)
12331 int hscrolled_p = 0;
12332 int hscroll_relative_p = FLOATP (Vhscroll_step);
12333 int hscroll_step_abs = 0;
12334 double hscroll_step_rel = 0;
12336 if (hscroll_relative_p)
12338 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12339 if (hscroll_step_rel < 0)
12341 hscroll_relative_p = 0;
12342 hscroll_step_abs = 0;
12345 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12347 hscroll_step_abs = XINT (Vhscroll_step);
12348 if (hscroll_step_abs < 0)
12349 hscroll_step_abs = 0;
12351 else
12352 hscroll_step_abs = 0;
12354 while (WINDOWP (window))
12356 struct window *w = XWINDOW (window);
12358 if (WINDOWP (w->hchild))
12359 hscrolled_p |= hscroll_window_tree (w->hchild);
12360 else if (WINDOWP (w->vchild))
12361 hscrolled_p |= hscroll_window_tree (w->vchild);
12362 else if (w->cursor.vpos >= 0)
12364 int h_margin;
12365 int text_area_width;
12366 struct glyph_row *current_cursor_row
12367 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12368 struct glyph_row *desired_cursor_row
12369 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12370 struct glyph_row *cursor_row
12371 = (desired_cursor_row->enabled_p
12372 ? desired_cursor_row
12373 : current_cursor_row);
12374 int row_r2l_p = cursor_row->reversed_p;
12376 text_area_width = window_box_width (w, TEXT_AREA);
12378 /* Scroll when cursor is inside this scroll margin. */
12379 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12381 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12382 /* For left-to-right rows, hscroll when cursor is either
12383 (i) inside the right hscroll margin, or (ii) if it is
12384 inside the left margin and the window is already
12385 hscrolled. */
12386 && ((!row_r2l_p
12387 && ((w->hscroll
12388 && w->cursor.x <= h_margin)
12389 || (cursor_row->enabled_p
12390 && cursor_row->truncated_on_right_p
12391 && (w->cursor.x >= text_area_width - h_margin))))
12392 /* For right-to-left rows, the logic is similar,
12393 except that rules for scrolling to left and right
12394 are reversed. E.g., if cursor.x <= h_margin, we
12395 need to hscroll "to the right" unconditionally,
12396 and that will scroll the screen to the left so as
12397 to reveal the next portion of the row. */
12398 || (row_r2l_p
12399 && ((cursor_row->enabled_p
12400 /* FIXME: It is confusing to set the
12401 truncated_on_right_p flag when R2L rows
12402 are actually truncated on the left. */
12403 && cursor_row->truncated_on_right_p
12404 && w->cursor.x <= h_margin)
12405 || (w->hscroll
12406 && (w->cursor.x >= text_area_width - h_margin))))))
12408 struct it it;
12409 ptrdiff_t hscroll;
12410 struct buffer *saved_current_buffer;
12411 ptrdiff_t pt;
12412 int wanted_x;
12414 /* Find point in a display of infinite width. */
12415 saved_current_buffer = current_buffer;
12416 current_buffer = XBUFFER (w->buffer);
12418 if (w == XWINDOW (selected_window))
12419 pt = PT;
12420 else
12422 pt = marker_position (w->pointm);
12423 pt = max (BEGV, pt);
12424 pt = min (ZV, pt);
12427 /* Move iterator to pt starting at cursor_row->start in
12428 a line with infinite width. */
12429 init_to_row_start (&it, w, cursor_row);
12430 it.last_visible_x = INFINITY;
12431 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12432 current_buffer = saved_current_buffer;
12434 /* Position cursor in window. */
12435 if (!hscroll_relative_p && hscroll_step_abs == 0)
12436 hscroll = max (0, (it.current_x
12437 - (ITERATOR_AT_END_OF_LINE_P (&it)
12438 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12439 : (text_area_width / 2))))
12440 / FRAME_COLUMN_WIDTH (it.f);
12441 else if ((!row_r2l_p
12442 && w->cursor.x >= text_area_width - h_margin)
12443 || (row_r2l_p && w->cursor.x <= h_margin))
12445 if (hscroll_relative_p)
12446 wanted_x = text_area_width * (1 - hscroll_step_rel)
12447 - h_margin;
12448 else
12449 wanted_x = text_area_width
12450 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12451 - h_margin;
12452 hscroll
12453 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12455 else
12457 if (hscroll_relative_p)
12458 wanted_x = text_area_width * hscroll_step_rel
12459 + h_margin;
12460 else
12461 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12462 + h_margin;
12463 hscroll
12464 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12466 hscroll = max (hscroll, w->min_hscroll);
12468 /* Don't prevent redisplay optimizations if hscroll
12469 hasn't changed, as it will unnecessarily slow down
12470 redisplay. */
12471 if (w->hscroll != hscroll)
12473 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12474 w->hscroll = hscroll;
12475 hscrolled_p = 1;
12480 window = w->next;
12483 /* Value is non-zero if hscroll of any leaf window has been changed. */
12484 return hscrolled_p;
12488 /* Set hscroll so that cursor is visible and not inside horizontal
12489 scroll margins for all windows in the tree rooted at WINDOW. See
12490 also hscroll_window_tree above. Value is non-zero if any window's
12491 hscroll has been changed. If it has, desired matrices on the frame
12492 of WINDOW are cleared. */
12494 static int
12495 hscroll_windows (Lisp_Object window)
12497 int hscrolled_p = hscroll_window_tree (window);
12498 if (hscrolled_p)
12499 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12500 return hscrolled_p;
12505 /************************************************************************
12506 Redisplay
12507 ************************************************************************/
12509 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12510 to a non-zero value. This is sometimes handy to have in a debugger
12511 session. */
12513 #ifdef GLYPH_DEBUG
12515 /* First and last unchanged row for try_window_id. */
12517 static int debug_first_unchanged_at_end_vpos;
12518 static int debug_last_unchanged_at_beg_vpos;
12520 /* Delta vpos and y. */
12522 static int debug_dvpos, debug_dy;
12524 /* Delta in characters and bytes for try_window_id. */
12526 static ptrdiff_t debug_delta, debug_delta_bytes;
12528 /* Values of window_end_pos and window_end_vpos at the end of
12529 try_window_id. */
12531 static ptrdiff_t debug_end_vpos;
12533 /* Append a string to W->desired_matrix->method. FMT is a printf
12534 format string. If trace_redisplay_p is non-zero also printf the
12535 resulting string to stderr. */
12537 static void debug_method_add (struct window *, char const *, ...)
12538 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12540 static void
12541 debug_method_add (struct window *w, char const *fmt, ...)
12543 char *method = w->desired_matrix->method;
12544 int len = strlen (method);
12545 int size = sizeof w->desired_matrix->method;
12546 int remaining = size - len - 1;
12547 va_list ap;
12549 if (len && remaining)
12551 method[len] = '|';
12552 --remaining, ++len;
12555 va_start (ap, fmt);
12556 vsnprintf (method + len, remaining + 1, fmt, ap);
12557 va_end (ap);
12559 if (trace_redisplay_p)
12560 fprintf (stderr, "%p (%s): %s\n",
12562 ((BUFFERP (w->buffer)
12563 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12564 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12565 : "no buffer"),
12566 method + len);
12569 #endif /* GLYPH_DEBUG */
12572 /* Value is non-zero if all changes in window W, which displays
12573 current_buffer, are in the text between START and END. START is a
12574 buffer position, END is given as a distance from Z. Used in
12575 redisplay_internal for display optimization. */
12577 static inline int
12578 text_outside_line_unchanged_p (struct window *w,
12579 ptrdiff_t start, ptrdiff_t end)
12581 int unchanged_p = 1;
12583 /* If text or overlays have changed, see where. */
12584 if (w->last_modified < MODIFF
12585 || w->last_overlay_modified < OVERLAY_MODIFF)
12587 /* Gap in the line? */
12588 if (GPT < start || Z - GPT < end)
12589 unchanged_p = 0;
12591 /* Changes start in front of the line, or end after it? */
12592 if (unchanged_p
12593 && (BEG_UNCHANGED < start - 1
12594 || END_UNCHANGED < end))
12595 unchanged_p = 0;
12597 /* If selective display, can't optimize if changes start at the
12598 beginning of the line. */
12599 if (unchanged_p
12600 && INTEGERP (BVAR (current_buffer, selective_display))
12601 && XINT (BVAR (current_buffer, selective_display)) > 0
12602 && (BEG_UNCHANGED < start || GPT <= start))
12603 unchanged_p = 0;
12605 /* If there are overlays at the start or end of the line, these
12606 may have overlay strings with newlines in them. A change at
12607 START, for instance, may actually concern the display of such
12608 overlay strings as well, and they are displayed on different
12609 lines. So, quickly rule out this case. (For the future, it
12610 might be desirable to implement something more telling than
12611 just BEG/END_UNCHANGED.) */
12612 if (unchanged_p)
12614 if (BEG + BEG_UNCHANGED == start
12615 && overlay_touches_p (start))
12616 unchanged_p = 0;
12617 if (END_UNCHANGED == end
12618 && overlay_touches_p (Z - end))
12619 unchanged_p = 0;
12622 /* Under bidi reordering, adding or deleting a character in the
12623 beginning of a paragraph, before the first strong directional
12624 character, can change the base direction of the paragraph (unless
12625 the buffer specifies a fixed paragraph direction), which will
12626 require to redisplay the whole paragraph. It might be worthwhile
12627 to find the paragraph limits and widen the range of redisplayed
12628 lines to that, but for now just give up this optimization. */
12629 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12630 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12631 unchanged_p = 0;
12634 return unchanged_p;
12638 /* Do a frame update, taking possible shortcuts into account. This is
12639 the main external entry point for redisplay.
12641 If the last redisplay displayed an echo area message and that message
12642 is no longer requested, we clear the echo area or bring back the
12643 mini-buffer if that is in use. */
12645 void
12646 redisplay (void)
12648 redisplay_internal ();
12652 static Lisp_Object
12653 overlay_arrow_string_or_property (Lisp_Object var)
12655 Lisp_Object val;
12657 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12658 return val;
12660 return Voverlay_arrow_string;
12663 /* Return 1 if there are any overlay-arrows in current_buffer. */
12664 static int
12665 overlay_arrow_in_current_buffer_p (void)
12667 Lisp_Object vlist;
12669 for (vlist = Voverlay_arrow_variable_list;
12670 CONSP (vlist);
12671 vlist = XCDR (vlist))
12673 Lisp_Object var = XCAR (vlist);
12674 Lisp_Object val;
12676 if (!SYMBOLP (var))
12677 continue;
12678 val = find_symbol_value (var);
12679 if (MARKERP (val)
12680 && current_buffer == XMARKER (val)->buffer)
12681 return 1;
12683 return 0;
12687 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12688 has changed. */
12690 static int
12691 overlay_arrows_changed_p (void)
12693 Lisp_Object vlist;
12695 for (vlist = Voverlay_arrow_variable_list;
12696 CONSP (vlist);
12697 vlist = XCDR (vlist))
12699 Lisp_Object var = XCAR (vlist);
12700 Lisp_Object val, pstr;
12702 if (!SYMBOLP (var))
12703 continue;
12704 val = find_symbol_value (var);
12705 if (!MARKERP (val))
12706 continue;
12707 if (! EQ (COERCE_MARKER (val),
12708 Fget (var, Qlast_arrow_position))
12709 || ! (pstr = overlay_arrow_string_or_property (var),
12710 EQ (pstr, Fget (var, Qlast_arrow_string))))
12711 return 1;
12713 return 0;
12716 /* Mark overlay arrows to be updated on next redisplay. */
12718 static void
12719 update_overlay_arrows (int up_to_date)
12721 Lisp_Object vlist;
12723 for (vlist = Voverlay_arrow_variable_list;
12724 CONSP (vlist);
12725 vlist = XCDR (vlist))
12727 Lisp_Object var = XCAR (vlist);
12729 if (!SYMBOLP (var))
12730 continue;
12732 if (up_to_date > 0)
12734 Lisp_Object val = find_symbol_value (var);
12735 Fput (var, Qlast_arrow_position,
12736 COERCE_MARKER (val));
12737 Fput (var, Qlast_arrow_string,
12738 overlay_arrow_string_or_property (var));
12740 else if (up_to_date < 0
12741 || !NILP (Fget (var, Qlast_arrow_position)))
12743 Fput (var, Qlast_arrow_position, Qt);
12744 Fput (var, Qlast_arrow_string, Qt);
12750 /* Return overlay arrow string to display at row.
12751 Return integer (bitmap number) for arrow bitmap in left fringe.
12752 Return nil if no overlay arrow. */
12754 static Lisp_Object
12755 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12757 Lisp_Object vlist;
12759 for (vlist = Voverlay_arrow_variable_list;
12760 CONSP (vlist);
12761 vlist = XCDR (vlist))
12763 Lisp_Object var = XCAR (vlist);
12764 Lisp_Object val;
12766 if (!SYMBOLP (var))
12767 continue;
12769 val = find_symbol_value (var);
12771 if (MARKERP (val)
12772 && current_buffer == XMARKER (val)->buffer
12773 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12775 if (FRAME_WINDOW_P (it->f)
12776 /* FIXME: if ROW->reversed_p is set, this should test
12777 the right fringe, not the left one. */
12778 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12780 #ifdef HAVE_WINDOW_SYSTEM
12781 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12783 int fringe_bitmap;
12784 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12785 return make_number (fringe_bitmap);
12787 #endif
12788 return make_number (-1); /* Use default arrow bitmap. */
12790 return overlay_arrow_string_or_property (var);
12794 return Qnil;
12797 /* Return 1 if point moved out of or into a composition. Otherwise
12798 return 0. PREV_BUF and PREV_PT are the last point buffer and
12799 position. BUF and PT are the current point buffer and position. */
12801 static int
12802 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12803 struct buffer *buf, ptrdiff_t pt)
12805 ptrdiff_t start, end;
12806 Lisp_Object prop;
12807 Lisp_Object buffer;
12809 XSETBUFFER (buffer, buf);
12810 /* Check a composition at the last point if point moved within the
12811 same buffer. */
12812 if (prev_buf == buf)
12814 if (prev_pt == pt)
12815 /* Point didn't move. */
12816 return 0;
12818 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12819 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12820 && COMPOSITION_VALID_P (start, end, prop)
12821 && start < prev_pt && end > prev_pt)
12822 /* The last point was within the composition. Return 1 iff
12823 point moved out of the composition. */
12824 return (pt <= start || pt >= end);
12827 /* Check a composition at the current point. */
12828 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12829 && find_composition (pt, -1, &start, &end, &prop, buffer)
12830 && COMPOSITION_VALID_P (start, end, prop)
12831 && start < pt && end > pt);
12835 /* Reconsider the setting of B->clip_changed which is displayed
12836 in window W. */
12838 static inline void
12839 reconsider_clip_changes (struct window *w, struct buffer *b)
12841 if (b->clip_changed
12842 && !NILP (w->window_end_valid)
12843 && w->current_matrix->buffer == b
12844 && w->current_matrix->zv == BUF_ZV (b)
12845 && w->current_matrix->begv == BUF_BEGV (b))
12846 b->clip_changed = 0;
12848 /* If display wasn't paused, and W is not a tool bar window, see if
12849 point has been moved into or out of a composition. In that case,
12850 we set b->clip_changed to 1 to force updating the screen. If
12851 b->clip_changed has already been set to 1, we can skip this
12852 check. */
12853 if (!b->clip_changed
12854 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12856 ptrdiff_t pt;
12858 if (w == XWINDOW (selected_window))
12859 pt = PT;
12860 else
12861 pt = marker_position (w->pointm);
12863 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12864 || pt != w->last_point)
12865 && check_point_in_composition (w->current_matrix->buffer,
12866 w->last_point,
12867 XBUFFER (w->buffer), pt))
12868 b->clip_changed = 1;
12873 /* Select FRAME to forward the values of frame-local variables into C
12874 variables so that the redisplay routines can access those values
12875 directly. */
12877 static void
12878 select_frame_for_redisplay (Lisp_Object frame)
12880 Lisp_Object tail, tem;
12881 Lisp_Object old = selected_frame;
12882 struct Lisp_Symbol *sym;
12884 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12886 selected_frame = frame;
12888 do {
12889 for (tail = XFRAME (frame)->param_alist;
12890 CONSP (tail); tail = XCDR (tail))
12891 if (CONSP (XCAR (tail))
12892 && (tem = XCAR (XCAR (tail)),
12893 SYMBOLP (tem))
12894 && (sym = indirect_variable (XSYMBOL (tem)),
12895 sym->redirect == SYMBOL_LOCALIZED)
12896 && sym->val.blv->frame_local)
12897 /* Use find_symbol_value rather than Fsymbol_value
12898 to avoid an error if it is void. */
12899 find_symbol_value (tem);
12900 } while (!EQ (frame, old) && (frame = old, 1));
12904 #define STOP_POLLING \
12905 do { if (! polling_stopped_here) stop_polling (); \
12906 polling_stopped_here = 1; } while (0)
12908 #define RESUME_POLLING \
12909 do { if (polling_stopped_here) start_polling (); \
12910 polling_stopped_here = 0; } while (0)
12913 /* Perhaps in the future avoid recentering windows if it
12914 is not necessary; currently that causes some problems. */
12916 static void
12917 redisplay_internal (void)
12919 struct window *w = XWINDOW (selected_window);
12920 struct window *sw;
12921 struct frame *fr;
12922 int pending;
12923 int must_finish = 0;
12924 struct text_pos tlbufpos, tlendpos;
12925 int number_of_visible_frames;
12926 ptrdiff_t count, count1;
12927 struct frame *sf;
12928 int polling_stopped_here = 0;
12929 Lisp_Object old_frame = selected_frame;
12931 /* Non-zero means redisplay has to consider all windows on all
12932 frames. Zero means, only selected_window is considered. */
12933 int consider_all_windows_p;
12935 /* Non-zero means redisplay has to redisplay the miniwindow */
12936 int update_miniwindow_p = 0;
12938 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12940 /* No redisplay if running in batch mode or frame is not yet fully
12941 initialized, or redisplay is explicitly turned off by setting
12942 Vinhibit_redisplay. */
12943 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12944 || !NILP (Vinhibit_redisplay))
12945 return;
12947 /* Don't examine these until after testing Vinhibit_redisplay.
12948 When Emacs is shutting down, perhaps because its connection to
12949 X has dropped, we should not look at them at all. */
12950 fr = XFRAME (w->frame);
12951 sf = SELECTED_FRAME ();
12953 if (!fr->glyphs_initialized_p)
12954 return;
12956 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12957 if (popup_activated ())
12958 return;
12959 #endif
12961 /* I don't think this happens but let's be paranoid. */
12962 if (redisplaying_p)
12963 return;
12965 /* Record a function that clears redisplaying_p
12966 when we leave this function. */
12967 count = SPECPDL_INDEX ();
12968 record_unwind_protect (unwind_redisplay, selected_frame);
12969 redisplaying_p = 1;
12970 specbind (Qinhibit_free_realized_faces, Qnil);
12973 Lisp_Object tail, frame;
12975 FOR_EACH_FRAME (tail, frame)
12977 struct frame *f = XFRAME (frame);
12978 f->already_hscrolled_p = 0;
12982 retry:
12983 /* Remember the currently selected window. */
12984 sw = w;
12986 if (!EQ (old_frame, selected_frame)
12987 && FRAME_LIVE_P (XFRAME (old_frame)))
12988 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12989 selected_frame and selected_window to be temporarily out-of-sync so
12990 when we come back here via `goto retry', we need to resync because we
12991 may need to run Elisp code (via prepare_menu_bars). */
12992 select_frame_for_redisplay (old_frame);
12994 pending = 0;
12995 reconsider_clip_changes (w, current_buffer);
12996 last_escape_glyph_frame = NULL;
12997 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12998 last_glyphless_glyph_frame = NULL;
12999 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13001 /* If new fonts have been loaded that make a glyph matrix adjustment
13002 necessary, do it. */
13003 if (fonts_changed_p)
13005 adjust_glyphs (NULL);
13006 ++windows_or_buffers_changed;
13007 fonts_changed_p = 0;
13010 /* If face_change_count is non-zero, init_iterator will free all
13011 realized faces, which includes the faces referenced from current
13012 matrices. So, we can't reuse current matrices in this case. */
13013 if (face_change_count)
13014 ++windows_or_buffers_changed;
13016 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13017 && FRAME_TTY (sf)->previous_frame != sf)
13019 /* Since frames on a single ASCII terminal share the same
13020 display area, displaying a different frame means redisplay
13021 the whole thing. */
13022 windows_or_buffers_changed++;
13023 SET_FRAME_GARBAGED (sf);
13024 #ifndef DOS_NT
13025 set_tty_color_mode (FRAME_TTY (sf), sf);
13026 #endif
13027 FRAME_TTY (sf)->previous_frame = sf;
13030 /* Set the visible flags for all frames. Do this before checking
13031 for resized or garbaged frames; they want to know if their frames
13032 are visible. See the comment in frame.h for
13033 FRAME_SAMPLE_VISIBILITY. */
13035 Lisp_Object tail, frame;
13037 number_of_visible_frames = 0;
13039 FOR_EACH_FRAME (tail, frame)
13041 struct frame *f = XFRAME (frame);
13043 FRAME_SAMPLE_VISIBILITY (f);
13044 if (FRAME_VISIBLE_P (f))
13045 ++number_of_visible_frames;
13046 clear_desired_matrices (f);
13050 /* Notice any pending interrupt request to change frame size. */
13051 do_pending_window_change (1);
13053 /* do_pending_window_change could change the selected_window due to
13054 frame resizing which makes the selected window too small. */
13055 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13057 sw = w;
13058 reconsider_clip_changes (w, current_buffer);
13061 /* Clear frames marked as garbaged. */
13062 if (frame_garbaged)
13063 clear_garbaged_frames ();
13065 /* Build menubar and tool-bar items. */
13066 if (NILP (Vmemory_full))
13067 prepare_menu_bars ();
13069 if (windows_or_buffers_changed)
13070 update_mode_lines++;
13072 /* Detect case that we need to write or remove a star in the mode line. */
13073 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13075 w->update_mode_line = 1;
13076 if (buffer_shared > 1)
13077 update_mode_lines++;
13080 /* Avoid invocation of point motion hooks by `current_column' below. */
13081 count1 = SPECPDL_INDEX ();
13082 specbind (Qinhibit_point_motion_hooks, Qt);
13084 /* If %c is in the mode line, update it if needed. */
13085 if (!NILP (w->column_number_displayed)
13086 /* This alternative quickly identifies a common case
13087 where no change is needed. */
13088 && !(PT == w->last_point
13089 && w->last_modified >= MODIFF
13090 && w->last_overlay_modified >= OVERLAY_MODIFF)
13091 && (XFASTINT (w->column_number_displayed) != current_column ()))
13092 w->update_mode_line = 1;
13094 unbind_to (count1, Qnil);
13096 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13098 /* The variable buffer_shared is set in redisplay_window and
13099 indicates that we redisplay a buffer in different windows. See
13100 there. */
13101 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13102 || cursor_type_changed);
13104 /* If specs for an arrow have changed, do thorough redisplay
13105 to ensure we remove any arrow that should no longer exist. */
13106 if (overlay_arrows_changed_p ())
13107 consider_all_windows_p = windows_or_buffers_changed = 1;
13109 /* Normally the message* functions will have already displayed and
13110 updated the echo area, but the frame may have been trashed, or
13111 the update may have been preempted, so display the echo area
13112 again here. Checking message_cleared_p captures the case that
13113 the echo area should be cleared. */
13114 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13115 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13116 || (message_cleared_p
13117 && minibuf_level == 0
13118 /* If the mini-window is currently selected, this means the
13119 echo-area doesn't show through. */
13120 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13122 int window_height_changed_p = echo_area_display (0);
13124 if (message_cleared_p)
13125 update_miniwindow_p = 1;
13127 must_finish = 1;
13129 /* If we don't display the current message, don't clear the
13130 message_cleared_p flag, because, if we did, we wouldn't clear
13131 the echo area in the next redisplay which doesn't preserve
13132 the echo area. */
13133 if (!display_last_displayed_message_p)
13134 message_cleared_p = 0;
13136 if (fonts_changed_p)
13137 goto retry;
13138 else if (window_height_changed_p)
13140 consider_all_windows_p = 1;
13141 ++update_mode_lines;
13142 ++windows_or_buffers_changed;
13144 /* If window configuration was changed, frames may have been
13145 marked garbaged. Clear them or we will experience
13146 surprises wrt scrolling. */
13147 if (frame_garbaged)
13148 clear_garbaged_frames ();
13151 else if (EQ (selected_window, minibuf_window)
13152 && (current_buffer->clip_changed
13153 || w->last_modified < MODIFF
13154 || w->last_overlay_modified < OVERLAY_MODIFF)
13155 && resize_mini_window (w, 0))
13157 /* Resized active mini-window to fit the size of what it is
13158 showing if its contents might have changed. */
13159 must_finish = 1;
13160 /* FIXME: this causes all frames to be updated, which seems unnecessary
13161 since only the current frame needs to be considered. This function needs
13162 to be rewritten with two variables, consider_all_windows and
13163 consider_all_frames. */
13164 consider_all_windows_p = 1;
13165 ++windows_or_buffers_changed;
13166 ++update_mode_lines;
13168 /* If window configuration was changed, frames may have been
13169 marked garbaged. Clear them or we will experience
13170 surprises wrt scrolling. */
13171 if (frame_garbaged)
13172 clear_garbaged_frames ();
13176 /* If showing the region, and mark has changed, we must redisplay
13177 the whole window. The assignment to this_line_start_pos prevents
13178 the optimization directly below this if-statement. */
13179 if (((!NILP (Vtransient_mark_mode)
13180 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13181 != !NILP (w->region_showing))
13182 || (!NILP (w->region_showing)
13183 && !EQ (w->region_showing,
13184 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13185 CHARPOS (this_line_start_pos) = 0;
13187 /* Optimize the case that only the line containing the cursor in the
13188 selected window has changed. Variables starting with this_ are
13189 set in display_line and record information about the line
13190 containing the cursor. */
13191 tlbufpos = this_line_start_pos;
13192 tlendpos = this_line_end_pos;
13193 if (!consider_all_windows_p
13194 && CHARPOS (tlbufpos) > 0
13195 && !w->update_mode_line
13196 && !current_buffer->clip_changed
13197 && !current_buffer->prevent_redisplay_optimizations_p
13198 && FRAME_VISIBLE_P (XFRAME (w->frame))
13199 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13200 /* Make sure recorded data applies to current buffer, etc. */
13201 && this_line_buffer == current_buffer
13202 && current_buffer == XBUFFER (w->buffer)
13203 && !w->force_start
13204 && !w->optional_new_start
13205 /* Point must be on the line that we have info recorded about. */
13206 && PT >= CHARPOS (tlbufpos)
13207 && PT <= Z - CHARPOS (tlendpos)
13208 /* All text outside that line, including its final newline,
13209 must be unchanged. */
13210 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13211 CHARPOS (tlendpos)))
13213 if (CHARPOS (tlbufpos) > BEGV
13214 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13215 && (CHARPOS (tlbufpos) == ZV
13216 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13217 /* Former continuation line has disappeared by becoming empty. */
13218 goto cancel;
13219 else if (w->last_modified < MODIFF
13220 || w->last_overlay_modified < OVERLAY_MODIFF
13221 || MINI_WINDOW_P (w))
13223 /* We have to handle the case of continuation around a
13224 wide-column character (see the comment in indent.c around
13225 line 1340).
13227 For instance, in the following case:
13229 -------- Insert --------
13230 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13231 J_I_ ==> J_I_ `^^' are cursors.
13232 ^^ ^^
13233 -------- --------
13235 As we have to redraw the line above, we cannot use this
13236 optimization. */
13238 struct it it;
13239 int line_height_before = this_line_pixel_height;
13241 /* Note that start_display will handle the case that the
13242 line starting at tlbufpos is a continuation line. */
13243 start_display (&it, w, tlbufpos);
13245 /* Implementation note: It this still necessary? */
13246 if (it.current_x != this_line_start_x)
13247 goto cancel;
13249 TRACE ((stderr, "trying display optimization 1\n"));
13250 w->cursor.vpos = -1;
13251 overlay_arrow_seen = 0;
13252 it.vpos = this_line_vpos;
13253 it.current_y = this_line_y;
13254 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13255 display_line (&it);
13257 /* If line contains point, is not continued,
13258 and ends at same distance from eob as before, we win. */
13259 if (w->cursor.vpos >= 0
13260 /* Line is not continued, otherwise this_line_start_pos
13261 would have been set to 0 in display_line. */
13262 && CHARPOS (this_line_start_pos)
13263 /* Line ends as before. */
13264 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13265 /* Line has same height as before. Otherwise other lines
13266 would have to be shifted up or down. */
13267 && this_line_pixel_height == line_height_before)
13269 /* If this is not the window's last line, we must adjust
13270 the charstarts of the lines below. */
13271 if (it.current_y < it.last_visible_y)
13273 struct glyph_row *row
13274 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13275 ptrdiff_t delta, delta_bytes;
13277 /* We used to distinguish between two cases here,
13278 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13279 when the line ends in a newline or the end of the
13280 buffer's accessible portion. But both cases did
13281 the same, so they were collapsed. */
13282 delta = (Z
13283 - CHARPOS (tlendpos)
13284 - MATRIX_ROW_START_CHARPOS (row));
13285 delta_bytes = (Z_BYTE
13286 - BYTEPOS (tlendpos)
13287 - MATRIX_ROW_START_BYTEPOS (row));
13289 increment_matrix_positions (w->current_matrix,
13290 this_line_vpos + 1,
13291 w->current_matrix->nrows,
13292 delta, delta_bytes);
13295 /* If this row displays text now but previously didn't,
13296 or vice versa, w->window_end_vpos may have to be
13297 adjusted. */
13298 if ((it.glyph_row - 1)->displays_text_p)
13300 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13301 wset_window_end_vpos (w, make_number (this_line_vpos));
13303 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13304 && this_line_vpos > 0)
13305 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13306 wset_window_end_valid (w, Qnil);
13308 /* Update hint: No need to try to scroll in update_window. */
13309 w->desired_matrix->no_scrolling_p = 1;
13311 #ifdef GLYPH_DEBUG
13312 *w->desired_matrix->method = 0;
13313 debug_method_add (w, "optimization 1");
13314 #endif
13315 #ifdef HAVE_WINDOW_SYSTEM
13316 update_window_fringes (w, 0);
13317 #endif
13318 goto update;
13320 else
13321 goto cancel;
13323 else if (/* Cursor position hasn't changed. */
13324 PT == w->last_point
13325 /* Make sure the cursor was last displayed
13326 in this window. Otherwise we have to reposition it. */
13327 && 0 <= w->cursor.vpos
13328 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13330 if (!must_finish)
13332 do_pending_window_change (1);
13333 /* If selected_window changed, redisplay again. */
13334 if (WINDOWP (selected_window)
13335 && (w = XWINDOW (selected_window)) != sw)
13336 goto retry;
13338 /* We used to always goto end_of_redisplay here, but this
13339 isn't enough if we have a blinking cursor. */
13340 if (w->cursor_off_p == w->last_cursor_off_p)
13341 goto end_of_redisplay;
13343 goto update;
13345 /* If highlighting the region, or if the cursor is in the echo area,
13346 then we can't just move the cursor. */
13347 else if (! (!NILP (Vtransient_mark_mode)
13348 && !NILP (BVAR (current_buffer, mark_active)))
13349 && (EQ (selected_window,
13350 BVAR (current_buffer, last_selected_window))
13351 || highlight_nonselected_windows)
13352 && NILP (w->region_showing)
13353 && NILP (Vshow_trailing_whitespace)
13354 && !cursor_in_echo_area)
13356 struct it it;
13357 struct glyph_row *row;
13359 /* Skip from tlbufpos to PT and see where it is. Note that
13360 PT may be in invisible text. If so, we will end at the
13361 next visible position. */
13362 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13363 NULL, DEFAULT_FACE_ID);
13364 it.current_x = this_line_start_x;
13365 it.current_y = this_line_y;
13366 it.vpos = this_line_vpos;
13368 /* The call to move_it_to stops in front of PT, but
13369 moves over before-strings. */
13370 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13372 if (it.vpos == this_line_vpos
13373 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13374 row->enabled_p))
13376 eassert (this_line_vpos == it.vpos);
13377 eassert (this_line_y == it.current_y);
13378 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13379 #ifdef GLYPH_DEBUG
13380 *w->desired_matrix->method = 0;
13381 debug_method_add (w, "optimization 3");
13382 #endif
13383 goto update;
13385 else
13386 goto cancel;
13389 cancel:
13390 /* Text changed drastically or point moved off of line. */
13391 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13394 CHARPOS (this_line_start_pos) = 0;
13395 consider_all_windows_p |= buffer_shared > 1;
13396 ++clear_face_cache_count;
13397 #ifdef HAVE_WINDOW_SYSTEM
13398 ++clear_image_cache_count;
13399 #endif
13401 /* Build desired matrices, and update the display. If
13402 consider_all_windows_p is non-zero, do it for all windows on all
13403 frames. Otherwise do it for selected_window, only. */
13405 if (consider_all_windows_p)
13407 Lisp_Object tail, frame;
13409 FOR_EACH_FRAME (tail, frame)
13410 XFRAME (frame)->updated_p = 0;
13412 /* Recompute # windows showing selected buffer. This will be
13413 incremented each time such a window is displayed. */
13414 buffer_shared = 0;
13416 FOR_EACH_FRAME (tail, frame)
13418 struct frame *f = XFRAME (frame);
13420 /* We don't have to do anything for unselected terminal
13421 frames. */
13422 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13423 && !EQ (FRAME_TTY (f)->top_frame, frame))
13424 continue;
13426 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13428 if (! EQ (frame, selected_frame))
13429 /* Select the frame, for the sake of frame-local
13430 variables. */
13431 select_frame_for_redisplay (frame);
13433 /* Mark all the scroll bars to be removed; we'll redeem
13434 the ones we want when we redisplay their windows. */
13435 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13436 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13438 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13439 redisplay_windows (FRAME_ROOT_WINDOW (f));
13441 /* The X error handler may have deleted that frame. */
13442 if (!FRAME_LIVE_P (f))
13443 continue;
13445 /* Any scroll bars which redisplay_windows should have
13446 nuked should now go away. */
13447 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13448 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13450 /* If fonts changed, display again. */
13451 /* ??? rms: I suspect it is a mistake to jump all the way
13452 back to retry here. It should just retry this frame. */
13453 if (fonts_changed_p)
13454 goto retry;
13456 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13458 /* See if we have to hscroll. */
13459 if (!f->already_hscrolled_p)
13461 f->already_hscrolled_p = 1;
13462 if (hscroll_windows (f->root_window))
13463 goto retry;
13466 /* Prevent various kinds of signals during display
13467 update. stdio is not robust about handling
13468 signals, which can cause an apparent I/O
13469 error. */
13470 if (interrupt_input)
13471 unrequest_sigio ();
13472 STOP_POLLING;
13474 /* Update the display. */
13475 set_window_update_flags (XWINDOW (f->root_window), 1);
13476 pending |= update_frame (f, 0, 0);
13477 f->updated_p = 1;
13482 if (!EQ (old_frame, selected_frame)
13483 && FRAME_LIVE_P (XFRAME (old_frame)))
13484 /* We played a bit fast-and-loose above and allowed selected_frame
13485 and selected_window to be temporarily out-of-sync but let's make
13486 sure this stays contained. */
13487 select_frame_for_redisplay (old_frame);
13488 eassert (EQ (XFRAME (selected_frame)->selected_window,
13489 selected_window));
13491 if (!pending)
13493 /* Do the mark_window_display_accurate after all windows have
13494 been redisplayed because this call resets flags in buffers
13495 which are needed for proper redisplay. */
13496 FOR_EACH_FRAME (tail, frame)
13498 struct frame *f = XFRAME (frame);
13499 if (f->updated_p)
13501 mark_window_display_accurate (f->root_window, 1);
13502 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13503 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13508 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13510 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13511 struct frame *mini_frame;
13513 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13514 /* Use list_of_error, not Qerror, so that
13515 we catch only errors and don't run the debugger. */
13516 internal_condition_case_1 (redisplay_window_1, selected_window,
13517 list_of_error,
13518 redisplay_window_error);
13519 if (update_miniwindow_p)
13520 internal_condition_case_1 (redisplay_window_1, mini_window,
13521 list_of_error,
13522 redisplay_window_error);
13524 /* Compare desired and current matrices, perform output. */
13526 update:
13527 /* If fonts changed, display again. */
13528 if (fonts_changed_p)
13529 goto retry;
13531 /* Prevent various kinds of signals during display update.
13532 stdio is not robust about handling signals,
13533 which can cause an apparent I/O error. */
13534 if (interrupt_input)
13535 unrequest_sigio ();
13536 STOP_POLLING;
13538 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13540 if (hscroll_windows (selected_window))
13541 goto retry;
13543 XWINDOW (selected_window)->must_be_updated_p = 1;
13544 pending = update_frame (sf, 0, 0);
13547 /* We may have called echo_area_display at the top of this
13548 function. If the echo area is on another frame, that may
13549 have put text on a frame other than the selected one, so the
13550 above call to update_frame would not have caught it. Catch
13551 it here. */
13552 mini_window = FRAME_MINIBUF_WINDOW (sf);
13553 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13555 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13557 XWINDOW (mini_window)->must_be_updated_p = 1;
13558 pending |= update_frame (mini_frame, 0, 0);
13559 if (!pending && hscroll_windows (mini_window))
13560 goto retry;
13564 /* If display was paused because of pending input, make sure we do a
13565 thorough update the next time. */
13566 if (pending)
13568 /* Prevent the optimization at the beginning of
13569 redisplay_internal that tries a single-line update of the
13570 line containing the cursor in the selected window. */
13571 CHARPOS (this_line_start_pos) = 0;
13573 /* Let the overlay arrow be updated the next time. */
13574 update_overlay_arrows (0);
13576 /* If we pause after scrolling, some rows in the current
13577 matrices of some windows are not valid. */
13578 if (!WINDOW_FULL_WIDTH_P (w)
13579 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13580 update_mode_lines = 1;
13582 else
13584 if (!consider_all_windows_p)
13586 /* This has already been done above if
13587 consider_all_windows_p is set. */
13588 mark_window_display_accurate_1 (w, 1);
13590 /* Say overlay arrows are up to date. */
13591 update_overlay_arrows (1);
13593 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13594 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13597 update_mode_lines = 0;
13598 windows_or_buffers_changed = 0;
13599 cursor_type_changed = 0;
13602 /* Start SIGIO interrupts coming again. Having them off during the
13603 code above makes it less likely one will discard output, but not
13604 impossible, since there might be stuff in the system buffer here.
13605 But it is much hairier to try to do anything about that. */
13606 if (interrupt_input)
13607 request_sigio ();
13608 RESUME_POLLING;
13610 /* If a frame has become visible which was not before, redisplay
13611 again, so that we display it. Expose events for such a frame
13612 (which it gets when becoming visible) don't call the parts of
13613 redisplay constructing glyphs, so simply exposing a frame won't
13614 display anything in this case. So, we have to display these
13615 frames here explicitly. */
13616 if (!pending)
13618 Lisp_Object tail, frame;
13619 int new_count = 0;
13621 FOR_EACH_FRAME (tail, frame)
13623 int this_is_visible = 0;
13625 if (XFRAME (frame)->visible)
13626 this_is_visible = 1;
13627 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13628 if (XFRAME (frame)->visible)
13629 this_is_visible = 1;
13631 if (this_is_visible)
13632 new_count++;
13635 if (new_count != number_of_visible_frames)
13636 windows_or_buffers_changed++;
13639 /* Change frame size now if a change is pending. */
13640 do_pending_window_change (1);
13642 /* If we just did a pending size change, or have additional
13643 visible frames, or selected_window changed, redisplay again. */
13644 if ((windows_or_buffers_changed && !pending)
13645 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13646 goto retry;
13648 /* Clear the face and image caches.
13650 We used to do this only if consider_all_windows_p. But the cache
13651 needs to be cleared if a timer creates images in the current
13652 buffer (e.g. the test case in Bug#6230). */
13654 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13656 clear_face_cache (0);
13657 clear_face_cache_count = 0;
13660 #ifdef HAVE_WINDOW_SYSTEM
13661 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13663 clear_image_caches (Qnil);
13664 clear_image_cache_count = 0;
13666 #endif /* HAVE_WINDOW_SYSTEM */
13668 end_of_redisplay:
13669 unbind_to (count, Qnil);
13670 RESUME_POLLING;
13674 /* Redisplay, but leave alone any recent echo area message unless
13675 another message has been requested in its place.
13677 This is useful in situations where you need to redisplay but no
13678 user action has occurred, making it inappropriate for the message
13679 area to be cleared. See tracking_off and
13680 wait_reading_process_output for examples of these situations.
13682 FROM_WHERE is an integer saying from where this function was
13683 called. This is useful for debugging. */
13685 void
13686 redisplay_preserve_echo_area (int from_where)
13688 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13690 if (!NILP (echo_area_buffer[1]))
13692 /* We have a previously displayed message, but no current
13693 message. Redisplay the previous message. */
13694 display_last_displayed_message_p = 1;
13695 redisplay_internal ();
13696 display_last_displayed_message_p = 0;
13698 else
13699 redisplay_internal ();
13701 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13702 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13703 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13707 /* Function registered with record_unwind_protect in redisplay_internal.
13708 Clear redisplaying_p. Also, select the previously
13709 selected frame, unless it has been deleted (by an X connection
13710 failure during redisplay, for example). */
13712 static Lisp_Object
13713 unwind_redisplay (Lisp_Object old_frame)
13715 redisplaying_p = 0;
13716 if (! EQ (old_frame, selected_frame)
13717 && FRAME_LIVE_P (XFRAME (old_frame)))
13718 select_frame_for_redisplay (old_frame);
13719 return Qnil;
13723 /* Mark the display of window W as accurate or inaccurate. If
13724 ACCURATE_P is non-zero mark display of W as accurate. If
13725 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13726 redisplay_internal is called. */
13728 static void
13729 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13731 if (BUFFERP (w->buffer))
13733 struct buffer *b = XBUFFER (w->buffer);
13735 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13736 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13737 w->last_had_star
13738 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13740 if (accurate_p)
13742 b->clip_changed = 0;
13743 b->prevent_redisplay_optimizations_p = 0;
13745 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13746 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13747 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13748 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13750 w->current_matrix->buffer = b;
13751 w->current_matrix->begv = BUF_BEGV (b);
13752 w->current_matrix->zv = BUF_ZV (b);
13754 w->last_cursor = w->cursor;
13755 w->last_cursor_off_p = w->cursor_off_p;
13757 if (w == XWINDOW (selected_window))
13758 w->last_point = BUF_PT (b);
13759 else
13760 w->last_point = XMARKER (w->pointm)->charpos;
13764 if (accurate_p)
13766 wset_window_end_valid (w, w->buffer);
13767 w->update_mode_line = 0;
13772 /* Mark the display of windows in the window tree rooted at WINDOW as
13773 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13774 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13775 be redisplayed the next time redisplay_internal is called. */
13777 void
13778 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13780 struct window *w;
13782 for (; !NILP (window); window = w->next)
13784 w = XWINDOW (window);
13785 mark_window_display_accurate_1 (w, accurate_p);
13787 if (!NILP (w->vchild))
13788 mark_window_display_accurate (w->vchild, accurate_p);
13789 if (!NILP (w->hchild))
13790 mark_window_display_accurate (w->hchild, accurate_p);
13793 if (accurate_p)
13795 update_overlay_arrows (1);
13797 else
13799 /* Force a thorough redisplay the next time by setting
13800 last_arrow_position and last_arrow_string to t, which is
13801 unequal to any useful value of Voverlay_arrow_... */
13802 update_overlay_arrows (-1);
13807 /* Return value in display table DP (Lisp_Char_Table *) for character
13808 C. Since a display table doesn't have any parent, we don't have to
13809 follow parent. Do not call this function directly but use the
13810 macro DISP_CHAR_VECTOR. */
13812 Lisp_Object
13813 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13815 Lisp_Object val;
13817 if (ASCII_CHAR_P (c))
13819 val = dp->ascii;
13820 if (SUB_CHAR_TABLE_P (val))
13821 val = XSUB_CHAR_TABLE (val)->contents[c];
13823 else
13825 Lisp_Object table;
13827 XSETCHAR_TABLE (table, dp);
13828 val = char_table_ref (table, c);
13830 if (NILP (val))
13831 val = dp->defalt;
13832 return val;
13837 /***********************************************************************
13838 Window Redisplay
13839 ***********************************************************************/
13841 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13843 static void
13844 redisplay_windows (Lisp_Object window)
13846 while (!NILP (window))
13848 struct window *w = XWINDOW (window);
13850 if (!NILP (w->hchild))
13851 redisplay_windows (w->hchild);
13852 else if (!NILP (w->vchild))
13853 redisplay_windows (w->vchild);
13854 else if (!NILP (w->buffer))
13856 displayed_buffer = XBUFFER (w->buffer);
13857 /* Use list_of_error, not Qerror, so that
13858 we catch only errors and don't run the debugger. */
13859 internal_condition_case_1 (redisplay_window_0, window,
13860 list_of_error,
13861 redisplay_window_error);
13864 window = w->next;
13868 static Lisp_Object
13869 redisplay_window_error (Lisp_Object ignore)
13871 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13872 return Qnil;
13875 static Lisp_Object
13876 redisplay_window_0 (Lisp_Object window)
13878 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13879 redisplay_window (window, 0);
13880 return Qnil;
13883 static Lisp_Object
13884 redisplay_window_1 (Lisp_Object window)
13886 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13887 redisplay_window (window, 1);
13888 return Qnil;
13892 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13893 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13894 which positions recorded in ROW differ from current buffer
13895 positions.
13897 Return 0 if cursor is not on this row, 1 otherwise. */
13899 static int
13900 set_cursor_from_row (struct window *w, struct glyph_row *row,
13901 struct glyph_matrix *matrix,
13902 ptrdiff_t delta, ptrdiff_t delta_bytes,
13903 int dy, int dvpos)
13905 struct glyph *glyph = row->glyphs[TEXT_AREA];
13906 struct glyph *end = glyph + row->used[TEXT_AREA];
13907 struct glyph *cursor = NULL;
13908 /* The last known character position in row. */
13909 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13910 int x = row->x;
13911 ptrdiff_t pt_old = PT - delta;
13912 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13913 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13914 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13915 /* A glyph beyond the edge of TEXT_AREA which we should never
13916 touch. */
13917 struct glyph *glyphs_end = end;
13918 /* Non-zero means we've found a match for cursor position, but that
13919 glyph has the avoid_cursor_p flag set. */
13920 int match_with_avoid_cursor = 0;
13921 /* Non-zero means we've seen at least one glyph that came from a
13922 display string. */
13923 int string_seen = 0;
13924 /* Largest and smallest buffer positions seen so far during scan of
13925 glyph row. */
13926 ptrdiff_t bpos_max = pos_before;
13927 ptrdiff_t bpos_min = pos_after;
13928 /* Last buffer position covered by an overlay string with an integer
13929 `cursor' property. */
13930 ptrdiff_t bpos_covered = 0;
13931 /* Non-zero means the display string on which to display the cursor
13932 comes from a text property, not from an overlay. */
13933 int string_from_text_prop = 0;
13935 /* Don't even try doing anything if called for a mode-line or
13936 header-line row, since the rest of the code isn't prepared to
13937 deal with such calamities. */
13938 eassert (!row->mode_line_p);
13939 if (row->mode_line_p)
13940 return 0;
13942 /* Skip over glyphs not having an object at the start and the end of
13943 the row. These are special glyphs like truncation marks on
13944 terminal frames. */
13945 if (row->displays_text_p)
13947 if (!row->reversed_p)
13949 while (glyph < end
13950 && INTEGERP (glyph->object)
13951 && glyph->charpos < 0)
13953 x += glyph->pixel_width;
13954 ++glyph;
13956 while (end > glyph
13957 && INTEGERP ((end - 1)->object)
13958 /* CHARPOS is zero for blanks and stretch glyphs
13959 inserted by extend_face_to_end_of_line. */
13960 && (end - 1)->charpos <= 0)
13961 --end;
13962 glyph_before = glyph - 1;
13963 glyph_after = end;
13965 else
13967 struct glyph *g;
13969 /* If the glyph row is reversed, we need to process it from back
13970 to front, so swap the edge pointers. */
13971 glyphs_end = end = glyph - 1;
13972 glyph += row->used[TEXT_AREA] - 1;
13974 while (glyph > end + 1
13975 && INTEGERP (glyph->object)
13976 && glyph->charpos < 0)
13978 --glyph;
13979 x -= glyph->pixel_width;
13981 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13982 --glyph;
13983 /* By default, in reversed rows we put the cursor on the
13984 rightmost (first in the reading order) glyph. */
13985 for (g = end + 1; g < glyph; g++)
13986 x += g->pixel_width;
13987 while (end < glyph
13988 && INTEGERP ((end + 1)->object)
13989 && (end + 1)->charpos <= 0)
13990 ++end;
13991 glyph_before = glyph + 1;
13992 glyph_after = end;
13995 else if (row->reversed_p)
13997 /* In R2L rows that don't display text, put the cursor on the
13998 rightmost glyph. Case in point: an empty last line that is
13999 part of an R2L paragraph. */
14000 cursor = end - 1;
14001 /* Avoid placing the cursor on the last glyph of the row, where
14002 on terminal frames we hold the vertical border between
14003 adjacent windows. */
14004 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14005 && !WINDOW_RIGHTMOST_P (w)
14006 && cursor == row->glyphs[LAST_AREA] - 1)
14007 cursor--;
14008 x = -1; /* will be computed below, at label compute_x */
14011 /* Step 1: Try to find the glyph whose character position
14012 corresponds to point. If that's not possible, find 2 glyphs
14013 whose character positions are the closest to point, one before
14014 point, the other after it. */
14015 if (!row->reversed_p)
14016 while (/* not marched to end of glyph row */
14017 glyph < end
14018 /* glyph was not inserted by redisplay for internal purposes */
14019 && !INTEGERP (glyph->object))
14021 if (BUFFERP (glyph->object))
14023 ptrdiff_t dpos = glyph->charpos - pt_old;
14025 if (glyph->charpos > bpos_max)
14026 bpos_max = glyph->charpos;
14027 if (glyph->charpos < bpos_min)
14028 bpos_min = glyph->charpos;
14029 if (!glyph->avoid_cursor_p)
14031 /* If we hit point, we've found the glyph on which to
14032 display the cursor. */
14033 if (dpos == 0)
14035 match_with_avoid_cursor = 0;
14036 break;
14038 /* See if we've found a better approximation to
14039 POS_BEFORE or to POS_AFTER. */
14040 if (0 > dpos && dpos > pos_before - pt_old)
14042 pos_before = glyph->charpos;
14043 glyph_before = glyph;
14045 else if (0 < dpos && dpos < pos_after - pt_old)
14047 pos_after = glyph->charpos;
14048 glyph_after = glyph;
14051 else if (dpos == 0)
14052 match_with_avoid_cursor = 1;
14054 else if (STRINGP (glyph->object))
14056 Lisp_Object chprop;
14057 ptrdiff_t glyph_pos = glyph->charpos;
14059 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14060 glyph->object);
14061 if (!NILP (chprop))
14063 /* If the string came from a `display' text property,
14064 look up the buffer position of that property and
14065 use that position to update bpos_max, as if we
14066 actually saw such a position in one of the row's
14067 glyphs. This helps with supporting integer values
14068 of `cursor' property on the display string in
14069 situations where most or all of the row's buffer
14070 text is completely covered by display properties,
14071 so that no glyph with valid buffer positions is
14072 ever seen in the row. */
14073 ptrdiff_t prop_pos =
14074 string_buffer_position_lim (glyph->object, pos_before,
14075 pos_after, 0);
14077 if (prop_pos >= pos_before)
14078 bpos_max = prop_pos - 1;
14080 if (INTEGERP (chprop))
14082 bpos_covered = bpos_max + XINT (chprop);
14083 /* If the `cursor' property covers buffer positions up
14084 to and including point, we should display cursor on
14085 this glyph. Note that, if a `cursor' property on one
14086 of the string's characters has an integer value, we
14087 will break out of the loop below _before_ we get to
14088 the position match above. IOW, integer values of
14089 the `cursor' property override the "exact match for
14090 point" strategy of positioning the cursor. */
14091 /* Implementation note: bpos_max == pt_old when, e.g.,
14092 we are in an empty line, where bpos_max is set to
14093 MATRIX_ROW_START_CHARPOS, see above. */
14094 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14096 cursor = glyph;
14097 break;
14101 string_seen = 1;
14103 x += glyph->pixel_width;
14104 ++glyph;
14106 else if (glyph > end) /* row is reversed */
14107 while (!INTEGERP (glyph->object))
14109 if (BUFFERP (glyph->object))
14111 ptrdiff_t dpos = glyph->charpos - pt_old;
14113 if (glyph->charpos > bpos_max)
14114 bpos_max = glyph->charpos;
14115 if (glyph->charpos < bpos_min)
14116 bpos_min = glyph->charpos;
14117 if (!glyph->avoid_cursor_p)
14119 if (dpos == 0)
14121 match_with_avoid_cursor = 0;
14122 break;
14124 if (0 > dpos && dpos > pos_before - pt_old)
14126 pos_before = glyph->charpos;
14127 glyph_before = glyph;
14129 else if (0 < dpos && dpos < pos_after - pt_old)
14131 pos_after = glyph->charpos;
14132 glyph_after = glyph;
14135 else if (dpos == 0)
14136 match_with_avoid_cursor = 1;
14138 else if (STRINGP (glyph->object))
14140 Lisp_Object chprop;
14141 ptrdiff_t glyph_pos = glyph->charpos;
14143 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14144 glyph->object);
14145 if (!NILP (chprop))
14147 ptrdiff_t prop_pos =
14148 string_buffer_position_lim (glyph->object, pos_before,
14149 pos_after, 0);
14151 if (prop_pos >= pos_before)
14152 bpos_max = prop_pos - 1;
14154 if (INTEGERP (chprop))
14156 bpos_covered = bpos_max + XINT (chprop);
14157 /* If the `cursor' property covers buffer positions up
14158 to and including point, we should display cursor on
14159 this glyph. */
14160 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14162 cursor = glyph;
14163 break;
14166 string_seen = 1;
14168 --glyph;
14169 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14171 x--; /* can't use any pixel_width */
14172 break;
14174 x -= glyph->pixel_width;
14177 /* Step 2: If we didn't find an exact match for point, we need to
14178 look for a proper place to put the cursor among glyphs between
14179 GLYPH_BEFORE and GLYPH_AFTER. */
14180 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14181 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14182 && bpos_covered < pt_old)
14184 /* An empty line has a single glyph whose OBJECT is zero and
14185 whose CHARPOS is the position of a newline on that line.
14186 Note that on a TTY, there are more glyphs after that, which
14187 were produced by extend_face_to_end_of_line, but their
14188 CHARPOS is zero or negative. */
14189 int empty_line_p =
14190 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14191 && INTEGERP (glyph->object) && glyph->charpos > 0;
14193 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14195 ptrdiff_t ellipsis_pos;
14197 /* Scan back over the ellipsis glyphs. */
14198 if (!row->reversed_p)
14200 ellipsis_pos = (glyph - 1)->charpos;
14201 while (glyph > row->glyphs[TEXT_AREA]
14202 && (glyph - 1)->charpos == ellipsis_pos)
14203 glyph--, x -= glyph->pixel_width;
14204 /* That loop always goes one position too far, including
14205 the glyph before the ellipsis. So scan forward over
14206 that one. */
14207 x += glyph->pixel_width;
14208 glyph++;
14210 else /* row is reversed */
14212 ellipsis_pos = (glyph + 1)->charpos;
14213 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14214 && (glyph + 1)->charpos == ellipsis_pos)
14215 glyph++, x += glyph->pixel_width;
14216 x -= glyph->pixel_width;
14217 glyph--;
14220 else if (match_with_avoid_cursor)
14222 cursor = glyph_after;
14223 x = -1;
14225 else if (string_seen)
14227 int incr = row->reversed_p ? -1 : +1;
14229 /* Need to find the glyph that came out of a string which is
14230 present at point. That glyph is somewhere between
14231 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14232 positioned between POS_BEFORE and POS_AFTER in the
14233 buffer. */
14234 struct glyph *start, *stop;
14235 ptrdiff_t pos = pos_before;
14237 x = -1;
14239 /* If the row ends in a newline from a display string,
14240 reordering could have moved the glyphs belonging to the
14241 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14242 in this case we extend the search to the last glyph in
14243 the row that was not inserted by redisplay. */
14244 if (row->ends_in_newline_from_string_p)
14246 glyph_after = end;
14247 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14250 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14251 correspond to POS_BEFORE and POS_AFTER, respectively. We
14252 need START and STOP in the order that corresponds to the
14253 row's direction as given by its reversed_p flag. If the
14254 directionality of characters between POS_BEFORE and
14255 POS_AFTER is the opposite of the row's base direction,
14256 these characters will have been reordered for display,
14257 and we need to reverse START and STOP. */
14258 if (!row->reversed_p)
14260 start = min (glyph_before, glyph_after);
14261 stop = max (glyph_before, glyph_after);
14263 else
14265 start = max (glyph_before, glyph_after);
14266 stop = min (glyph_before, glyph_after);
14268 for (glyph = start + incr;
14269 row->reversed_p ? glyph > stop : glyph < stop; )
14272 /* Any glyphs that come from the buffer are here because
14273 of bidi reordering. Skip them, and only pay
14274 attention to glyphs that came from some string. */
14275 if (STRINGP (glyph->object))
14277 Lisp_Object str;
14278 ptrdiff_t tem;
14279 /* If the display property covers the newline, we
14280 need to search for it one position farther. */
14281 ptrdiff_t lim = pos_after
14282 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14284 string_from_text_prop = 0;
14285 str = glyph->object;
14286 tem = string_buffer_position_lim (str, pos, lim, 0);
14287 if (tem == 0 /* from overlay */
14288 || pos <= tem)
14290 /* If the string from which this glyph came is
14291 found in the buffer at point, or at position
14292 that is closer to point than pos_after, then
14293 we've found the glyph we've been looking for.
14294 If it comes from an overlay (tem == 0), and
14295 it has the `cursor' property on one of its
14296 glyphs, record that glyph as a candidate for
14297 displaying the cursor. (As in the
14298 unidirectional version, we will display the
14299 cursor on the last candidate we find.) */
14300 if (tem == 0
14301 || tem == pt_old
14302 || (tem - pt_old > 0 && tem < pos_after))
14304 /* The glyphs from this string could have
14305 been reordered. Find the one with the
14306 smallest string position. Or there could
14307 be a character in the string with the
14308 `cursor' property, which means display
14309 cursor on that character's glyph. */
14310 ptrdiff_t strpos = glyph->charpos;
14312 if (tem)
14314 cursor = glyph;
14315 string_from_text_prop = 1;
14317 for ( ;
14318 (row->reversed_p ? glyph > stop : glyph < stop)
14319 && EQ (glyph->object, str);
14320 glyph += incr)
14322 Lisp_Object cprop;
14323 ptrdiff_t gpos = glyph->charpos;
14325 cprop = Fget_char_property (make_number (gpos),
14326 Qcursor,
14327 glyph->object);
14328 if (!NILP (cprop))
14330 cursor = glyph;
14331 break;
14333 if (tem && glyph->charpos < strpos)
14335 strpos = glyph->charpos;
14336 cursor = glyph;
14340 if (tem == pt_old
14341 || (tem - pt_old > 0 && tem < pos_after))
14342 goto compute_x;
14344 if (tem)
14345 pos = tem + 1; /* don't find previous instances */
14347 /* This string is not what we want; skip all of the
14348 glyphs that came from it. */
14349 while ((row->reversed_p ? glyph > stop : glyph < stop)
14350 && EQ (glyph->object, str))
14351 glyph += incr;
14353 else
14354 glyph += incr;
14357 /* If we reached the end of the line, and END was from a string,
14358 the cursor is not on this line. */
14359 if (cursor == NULL
14360 && (row->reversed_p ? glyph <= end : glyph >= end)
14361 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14362 && STRINGP (end->object)
14363 && row->continued_p)
14364 return 0;
14366 /* A truncated row may not include PT among its character positions.
14367 Setting the cursor inside the scroll margin will trigger
14368 recalculation of hscroll in hscroll_window_tree. But if a
14369 display string covers point, defer to the string-handling
14370 code below to figure this out. */
14371 else if (row->truncated_on_left_p && pt_old < bpos_min)
14373 cursor = glyph_before;
14374 x = -1;
14376 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14377 /* Zero-width characters produce no glyphs. */
14378 || (!empty_line_p
14379 && (row->reversed_p
14380 ? glyph_after > glyphs_end
14381 : glyph_after < glyphs_end)))
14383 cursor = glyph_after;
14384 x = -1;
14388 compute_x:
14389 if (cursor != NULL)
14390 glyph = cursor;
14391 else if (glyph == glyphs_end
14392 && pos_before == pos_after
14393 && STRINGP ((row->reversed_p
14394 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14395 : row->glyphs[TEXT_AREA])->object))
14397 /* If all the glyphs of this row came from strings, put the
14398 cursor on the first glyph of the row. This avoids having the
14399 cursor outside of the text area in this very rare and hard
14400 use case. */
14401 glyph =
14402 row->reversed_p
14403 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14404 : row->glyphs[TEXT_AREA];
14406 if (x < 0)
14408 struct glyph *g;
14410 /* Need to compute x that corresponds to GLYPH. */
14411 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14413 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14414 emacs_abort ();
14415 x += g->pixel_width;
14419 /* ROW could be part of a continued line, which, under bidi
14420 reordering, might have other rows whose start and end charpos
14421 occlude point. Only set w->cursor if we found a better
14422 approximation to the cursor position than we have from previously
14423 examined candidate rows belonging to the same continued line. */
14424 if (/* we already have a candidate row */
14425 w->cursor.vpos >= 0
14426 /* that candidate is not the row we are processing */
14427 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14428 /* Make sure cursor.vpos specifies a row whose start and end
14429 charpos occlude point, and it is valid candidate for being a
14430 cursor-row. This is because some callers of this function
14431 leave cursor.vpos at the row where the cursor was displayed
14432 during the last redisplay cycle. */
14433 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14434 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14435 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14437 struct glyph *g1 =
14438 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14440 /* Don't consider glyphs that are outside TEXT_AREA. */
14441 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14442 return 0;
14443 /* Keep the candidate whose buffer position is the closest to
14444 point or has the `cursor' property. */
14445 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14446 w->cursor.hpos >= 0
14447 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14448 && ((BUFFERP (g1->object)
14449 && (g1->charpos == pt_old /* an exact match always wins */
14450 || (BUFFERP (glyph->object)
14451 && eabs (g1->charpos - pt_old)
14452 < eabs (glyph->charpos - pt_old))))
14453 /* previous candidate is a glyph from a string that has
14454 a non-nil `cursor' property */
14455 || (STRINGP (g1->object)
14456 && (!NILP (Fget_char_property (make_number (g1->charpos),
14457 Qcursor, g1->object))
14458 /* previous candidate is from the same display
14459 string as this one, and the display string
14460 came from a text property */
14461 || (EQ (g1->object, glyph->object)
14462 && string_from_text_prop)
14463 /* this candidate is from newline and its
14464 position is not an exact match */
14465 || (INTEGERP (glyph->object)
14466 && glyph->charpos != pt_old)))))
14467 return 0;
14468 /* If this candidate gives an exact match, use that. */
14469 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14470 /* If this candidate is a glyph created for the
14471 terminating newline of a line, and point is on that
14472 newline, it wins because it's an exact match. */
14473 || (!row->continued_p
14474 && INTEGERP (glyph->object)
14475 && glyph->charpos == 0
14476 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14477 /* Otherwise, keep the candidate that comes from a row
14478 spanning less buffer positions. This may win when one or
14479 both candidate positions are on glyphs that came from
14480 display strings, for which we cannot compare buffer
14481 positions. */
14482 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14483 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14484 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14485 return 0;
14487 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14488 w->cursor.x = x;
14489 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14490 w->cursor.y = row->y + dy;
14492 if (w == XWINDOW (selected_window))
14494 if (!row->continued_p
14495 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14496 && row->x == 0)
14498 this_line_buffer = XBUFFER (w->buffer);
14500 CHARPOS (this_line_start_pos)
14501 = MATRIX_ROW_START_CHARPOS (row) + delta;
14502 BYTEPOS (this_line_start_pos)
14503 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14505 CHARPOS (this_line_end_pos)
14506 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14507 BYTEPOS (this_line_end_pos)
14508 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14510 this_line_y = w->cursor.y;
14511 this_line_pixel_height = row->height;
14512 this_line_vpos = w->cursor.vpos;
14513 this_line_start_x = row->x;
14515 else
14516 CHARPOS (this_line_start_pos) = 0;
14519 return 1;
14523 /* Run window scroll functions, if any, for WINDOW with new window
14524 start STARTP. Sets the window start of WINDOW to that position.
14526 We assume that the window's buffer is really current. */
14528 static inline struct text_pos
14529 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14531 struct window *w = XWINDOW (window);
14532 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14534 if (current_buffer != XBUFFER (w->buffer))
14535 emacs_abort ();
14537 if (!NILP (Vwindow_scroll_functions))
14539 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14540 make_number (CHARPOS (startp)));
14541 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14542 /* In case the hook functions switch buffers. */
14543 set_buffer_internal (XBUFFER (w->buffer));
14546 return startp;
14550 /* Make sure the line containing the cursor is fully visible.
14551 A value of 1 means there is nothing to be done.
14552 (Either the line is fully visible, or it cannot be made so,
14553 or we cannot tell.)
14555 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14556 is higher than window.
14558 A value of 0 means the caller should do scrolling
14559 as if point had gone off the screen. */
14561 static int
14562 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14564 struct glyph_matrix *matrix;
14565 struct glyph_row *row;
14566 int window_height;
14568 if (!make_cursor_line_fully_visible_p)
14569 return 1;
14571 /* It's not always possible to find the cursor, e.g, when a window
14572 is full of overlay strings. Don't do anything in that case. */
14573 if (w->cursor.vpos < 0)
14574 return 1;
14576 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14577 row = MATRIX_ROW (matrix, w->cursor.vpos);
14579 /* If the cursor row is not partially visible, there's nothing to do. */
14580 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14581 return 1;
14583 /* If the row the cursor is in is taller than the window's height,
14584 it's not clear what to do, so do nothing. */
14585 window_height = window_box_height (w);
14586 if (row->height >= window_height)
14588 if (!force_p || MINI_WINDOW_P (w)
14589 || w->vscroll || w->cursor.vpos == 0)
14590 return 1;
14592 return 0;
14596 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14597 non-zero means only WINDOW is redisplayed in redisplay_internal.
14598 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14599 in redisplay_window to bring a partially visible line into view in
14600 the case that only the cursor has moved.
14602 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14603 last screen line's vertical height extends past the end of the screen.
14605 Value is
14607 1 if scrolling succeeded
14609 0 if scrolling didn't find point.
14611 -1 if new fonts have been loaded so that we must interrupt
14612 redisplay, adjust glyph matrices, and try again. */
14614 enum
14616 SCROLLING_SUCCESS,
14617 SCROLLING_FAILED,
14618 SCROLLING_NEED_LARGER_MATRICES
14621 /* If scroll-conservatively is more than this, never recenter.
14623 If you change this, don't forget to update the doc string of
14624 `scroll-conservatively' and the Emacs manual. */
14625 #define SCROLL_LIMIT 100
14627 static int
14628 try_scrolling (Lisp_Object window, int just_this_one_p,
14629 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14630 int temp_scroll_step, int last_line_misfit)
14632 struct window *w = XWINDOW (window);
14633 struct frame *f = XFRAME (w->frame);
14634 struct text_pos pos, startp;
14635 struct it it;
14636 int this_scroll_margin, scroll_max, rc, height;
14637 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14638 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14639 Lisp_Object aggressive;
14640 /* We will never try scrolling more than this number of lines. */
14641 int scroll_limit = SCROLL_LIMIT;
14643 #ifdef GLYPH_DEBUG
14644 debug_method_add (w, "try_scrolling");
14645 #endif
14647 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14649 /* Compute scroll margin height in pixels. We scroll when point is
14650 within this distance from the top or bottom of the window. */
14651 if (scroll_margin > 0)
14652 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14653 * FRAME_LINE_HEIGHT (f);
14654 else
14655 this_scroll_margin = 0;
14657 /* Force arg_scroll_conservatively to have a reasonable value, to
14658 avoid scrolling too far away with slow move_it_* functions. Note
14659 that the user can supply scroll-conservatively equal to
14660 `most-positive-fixnum', which can be larger than INT_MAX. */
14661 if (arg_scroll_conservatively > scroll_limit)
14663 arg_scroll_conservatively = scroll_limit + 1;
14664 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14666 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14667 /* Compute how much we should try to scroll maximally to bring
14668 point into view. */
14669 scroll_max = (max (scroll_step,
14670 max (arg_scroll_conservatively, temp_scroll_step))
14671 * FRAME_LINE_HEIGHT (f));
14672 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14673 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14674 /* We're trying to scroll because of aggressive scrolling but no
14675 scroll_step is set. Choose an arbitrary one. */
14676 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14677 else
14678 scroll_max = 0;
14680 too_near_end:
14682 /* Decide whether to scroll down. */
14683 if (PT > CHARPOS (startp))
14685 int scroll_margin_y;
14687 /* Compute the pixel ypos of the scroll margin, then move IT to
14688 either that ypos or PT, whichever comes first. */
14689 start_display (&it, w, startp);
14690 scroll_margin_y = it.last_visible_y - this_scroll_margin
14691 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14692 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14693 (MOVE_TO_POS | MOVE_TO_Y));
14695 if (PT > CHARPOS (it.current.pos))
14697 int y0 = line_bottom_y (&it);
14698 /* Compute how many pixels below window bottom to stop searching
14699 for PT. This avoids costly search for PT that is far away if
14700 the user limited scrolling by a small number of lines, but
14701 always finds PT if scroll_conservatively is set to a large
14702 number, such as most-positive-fixnum. */
14703 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14704 int y_to_move = it.last_visible_y + slack;
14706 /* Compute the distance from the scroll margin to PT or to
14707 the scroll limit, whichever comes first. This should
14708 include the height of the cursor line, to make that line
14709 fully visible. */
14710 move_it_to (&it, PT, -1, y_to_move,
14711 -1, MOVE_TO_POS | MOVE_TO_Y);
14712 dy = line_bottom_y (&it) - y0;
14714 if (dy > scroll_max)
14715 return SCROLLING_FAILED;
14717 if (dy > 0)
14718 scroll_down_p = 1;
14722 if (scroll_down_p)
14724 /* Point is in or below the bottom scroll margin, so move the
14725 window start down. If scrolling conservatively, move it just
14726 enough down to make point visible. If scroll_step is set,
14727 move it down by scroll_step. */
14728 if (arg_scroll_conservatively)
14729 amount_to_scroll
14730 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14731 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14732 else if (scroll_step || temp_scroll_step)
14733 amount_to_scroll = scroll_max;
14734 else
14736 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14737 height = WINDOW_BOX_TEXT_HEIGHT (w);
14738 if (NUMBERP (aggressive))
14740 double float_amount = XFLOATINT (aggressive) * height;
14741 amount_to_scroll = float_amount;
14742 if (amount_to_scroll == 0 && float_amount > 0)
14743 amount_to_scroll = 1;
14744 /* Don't let point enter the scroll margin near top of
14745 the window. */
14746 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14747 amount_to_scroll = height - 2*this_scroll_margin + dy;
14751 if (amount_to_scroll <= 0)
14752 return SCROLLING_FAILED;
14754 start_display (&it, w, startp);
14755 if (arg_scroll_conservatively <= scroll_limit)
14756 move_it_vertically (&it, amount_to_scroll);
14757 else
14759 /* Extra precision for users who set scroll-conservatively
14760 to a large number: make sure the amount we scroll
14761 the window start is never less than amount_to_scroll,
14762 which was computed as distance from window bottom to
14763 point. This matters when lines at window top and lines
14764 below window bottom have different height. */
14765 struct it it1;
14766 void *it1data = NULL;
14767 /* We use a temporary it1 because line_bottom_y can modify
14768 its argument, if it moves one line down; see there. */
14769 int start_y;
14771 SAVE_IT (it1, it, it1data);
14772 start_y = line_bottom_y (&it1);
14773 do {
14774 RESTORE_IT (&it, &it, it1data);
14775 move_it_by_lines (&it, 1);
14776 SAVE_IT (it1, it, it1data);
14777 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14780 /* If STARTP is unchanged, move it down another screen line. */
14781 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14782 move_it_by_lines (&it, 1);
14783 startp = it.current.pos;
14785 else
14787 struct text_pos scroll_margin_pos = startp;
14789 /* See if point is inside the scroll margin at the top of the
14790 window. */
14791 if (this_scroll_margin)
14793 start_display (&it, w, startp);
14794 move_it_vertically (&it, this_scroll_margin);
14795 scroll_margin_pos = it.current.pos;
14798 if (PT < CHARPOS (scroll_margin_pos))
14800 /* Point is in the scroll margin at the top of the window or
14801 above what is displayed in the window. */
14802 int y0, y_to_move;
14804 /* Compute the vertical distance from PT to the scroll
14805 margin position. Move as far as scroll_max allows, or
14806 one screenful, or 10 screen lines, whichever is largest.
14807 Give up if distance is greater than scroll_max. */
14808 SET_TEXT_POS (pos, PT, PT_BYTE);
14809 start_display (&it, w, pos);
14810 y0 = it.current_y;
14811 y_to_move = max (it.last_visible_y,
14812 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14813 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14814 y_to_move, -1,
14815 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14816 dy = it.current_y - y0;
14817 if (dy > scroll_max)
14818 return SCROLLING_FAILED;
14820 /* Compute new window start. */
14821 start_display (&it, w, startp);
14823 if (arg_scroll_conservatively)
14824 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14825 max (scroll_step, temp_scroll_step));
14826 else if (scroll_step || temp_scroll_step)
14827 amount_to_scroll = scroll_max;
14828 else
14830 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14831 height = WINDOW_BOX_TEXT_HEIGHT (w);
14832 if (NUMBERP (aggressive))
14834 double float_amount = XFLOATINT (aggressive) * height;
14835 amount_to_scroll = float_amount;
14836 if (amount_to_scroll == 0 && float_amount > 0)
14837 amount_to_scroll = 1;
14838 amount_to_scroll -=
14839 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14840 /* Don't let point enter the scroll margin near
14841 bottom of the window. */
14842 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14843 amount_to_scroll = height - 2*this_scroll_margin + dy;
14847 if (amount_to_scroll <= 0)
14848 return SCROLLING_FAILED;
14850 move_it_vertically_backward (&it, amount_to_scroll);
14851 startp = it.current.pos;
14855 /* Run window scroll functions. */
14856 startp = run_window_scroll_functions (window, startp);
14858 /* Display the window. Give up if new fonts are loaded, or if point
14859 doesn't appear. */
14860 if (!try_window (window, startp, 0))
14861 rc = SCROLLING_NEED_LARGER_MATRICES;
14862 else if (w->cursor.vpos < 0)
14864 clear_glyph_matrix (w->desired_matrix);
14865 rc = SCROLLING_FAILED;
14867 else
14869 /* Maybe forget recorded base line for line number display. */
14870 if (!just_this_one_p
14871 || current_buffer->clip_changed
14872 || BEG_UNCHANGED < CHARPOS (startp))
14873 wset_base_line_number (w, Qnil);
14875 /* If cursor ends up on a partially visible line,
14876 treat that as being off the bottom of the screen. */
14877 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14878 /* It's possible that the cursor is on the first line of the
14879 buffer, which is partially obscured due to a vscroll
14880 (Bug#7537). In that case, avoid looping forever . */
14881 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14883 clear_glyph_matrix (w->desired_matrix);
14884 ++extra_scroll_margin_lines;
14885 goto too_near_end;
14887 rc = SCROLLING_SUCCESS;
14890 return rc;
14894 /* Compute a suitable window start for window W if display of W starts
14895 on a continuation line. Value is non-zero if a new window start
14896 was computed.
14898 The new window start will be computed, based on W's width, starting
14899 from the start of the continued line. It is the start of the
14900 screen line with the minimum distance from the old start W->start. */
14902 static int
14903 compute_window_start_on_continuation_line (struct window *w)
14905 struct text_pos pos, start_pos;
14906 int window_start_changed_p = 0;
14908 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14910 /* If window start is on a continuation line... Window start may be
14911 < BEGV in case there's invisible text at the start of the
14912 buffer (M-x rmail, for example). */
14913 if (CHARPOS (start_pos) > BEGV
14914 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14916 struct it it;
14917 struct glyph_row *row;
14919 /* Handle the case that the window start is out of range. */
14920 if (CHARPOS (start_pos) < BEGV)
14921 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14922 else if (CHARPOS (start_pos) > ZV)
14923 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14925 /* Find the start of the continued line. This should be fast
14926 because scan_buffer is fast (newline cache). */
14927 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14928 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14929 row, DEFAULT_FACE_ID);
14930 reseat_at_previous_visible_line_start (&it);
14932 /* If the line start is "too far" away from the window start,
14933 say it takes too much time to compute a new window start. */
14934 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14935 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14937 int min_distance, distance;
14939 /* Move forward by display lines to find the new window
14940 start. If window width was enlarged, the new start can
14941 be expected to be > the old start. If window width was
14942 decreased, the new window start will be < the old start.
14943 So, we're looking for the display line start with the
14944 minimum distance from the old window start. */
14945 pos = it.current.pos;
14946 min_distance = INFINITY;
14947 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14948 distance < min_distance)
14950 min_distance = distance;
14951 pos = it.current.pos;
14952 move_it_by_lines (&it, 1);
14955 /* Set the window start there. */
14956 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14957 window_start_changed_p = 1;
14961 return window_start_changed_p;
14965 /* Try cursor movement in case text has not changed in window WINDOW,
14966 with window start STARTP. Value is
14968 CURSOR_MOVEMENT_SUCCESS if successful
14970 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14972 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14973 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14974 we want to scroll as if scroll-step were set to 1. See the code.
14976 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14977 which case we have to abort this redisplay, and adjust matrices
14978 first. */
14980 enum
14982 CURSOR_MOVEMENT_SUCCESS,
14983 CURSOR_MOVEMENT_CANNOT_BE_USED,
14984 CURSOR_MOVEMENT_MUST_SCROLL,
14985 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14988 static int
14989 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14991 struct window *w = XWINDOW (window);
14992 struct frame *f = XFRAME (w->frame);
14993 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14995 #ifdef GLYPH_DEBUG
14996 if (inhibit_try_cursor_movement)
14997 return rc;
14998 #endif
15000 /* Previously, there was a check for Lisp integer in the
15001 if-statement below. Now, this field is converted to
15002 ptrdiff_t, thus zero means invalid position in a buffer. */
15003 eassert (w->last_point > 0);
15005 /* Handle case where text has not changed, only point, and it has
15006 not moved off the frame. */
15007 if (/* Point may be in this window. */
15008 PT >= CHARPOS (startp)
15009 /* Selective display hasn't changed. */
15010 && !current_buffer->clip_changed
15011 /* Function force-mode-line-update is used to force a thorough
15012 redisplay. It sets either windows_or_buffers_changed or
15013 update_mode_lines. So don't take a shortcut here for these
15014 cases. */
15015 && !update_mode_lines
15016 && !windows_or_buffers_changed
15017 && !cursor_type_changed
15018 /* Can't use this case if highlighting a region. When a
15019 region exists, cursor movement has to do more than just
15020 set the cursor. */
15021 && !(!NILP (Vtransient_mark_mode)
15022 && !NILP (BVAR (current_buffer, mark_active)))
15023 && NILP (w->region_showing)
15024 && NILP (Vshow_trailing_whitespace)
15025 /* This code is not used for mini-buffer for the sake of the case
15026 of redisplaying to replace an echo area message; since in
15027 that case the mini-buffer contents per se are usually
15028 unchanged. This code is of no real use in the mini-buffer
15029 since the handling of this_line_start_pos, etc., in redisplay
15030 handles the same cases. */
15031 && !EQ (window, minibuf_window)
15032 /* When splitting windows or for new windows, it happens that
15033 redisplay is called with a nil window_end_vpos or one being
15034 larger than the window. This should really be fixed in
15035 window.c. I don't have this on my list, now, so we do
15036 approximately the same as the old redisplay code. --gerd. */
15037 && INTEGERP (w->window_end_vpos)
15038 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15039 && (FRAME_WINDOW_P (f)
15040 || !overlay_arrow_in_current_buffer_p ()))
15042 int this_scroll_margin, top_scroll_margin;
15043 struct glyph_row *row = NULL;
15045 #ifdef GLYPH_DEBUG
15046 debug_method_add (w, "cursor movement");
15047 #endif
15049 /* Scroll if point within this distance from the top or bottom
15050 of the window. This is a pixel value. */
15051 if (scroll_margin > 0)
15053 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15054 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15056 else
15057 this_scroll_margin = 0;
15059 top_scroll_margin = this_scroll_margin;
15060 if (WINDOW_WANTS_HEADER_LINE_P (w))
15061 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15063 /* Start with the row the cursor was displayed during the last
15064 not paused redisplay. Give up if that row is not valid. */
15065 if (w->last_cursor.vpos < 0
15066 || w->last_cursor.vpos >= w->current_matrix->nrows)
15067 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15068 else
15070 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15071 if (row->mode_line_p)
15072 ++row;
15073 if (!row->enabled_p)
15074 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15077 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15079 int scroll_p = 0, must_scroll = 0;
15080 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15082 if (PT > w->last_point)
15084 /* Point has moved forward. */
15085 while (MATRIX_ROW_END_CHARPOS (row) < PT
15086 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15088 eassert (row->enabled_p);
15089 ++row;
15092 /* If the end position of a row equals the start
15093 position of the next row, and PT is at that position,
15094 we would rather display cursor in the next line. */
15095 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15096 && MATRIX_ROW_END_CHARPOS (row) == PT
15097 && row < w->current_matrix->rows
15098 + w->current_matrix->nrows - 1
15099 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15100 && !cursor_row_p (row))
15101 ++row;
15103 /* If within the scroll margin, scroll. Note that
15104 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15105 the next line would be drawn, and that
15106 this_scroll_margin can be zero. */
15107 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15108 || PT > MATRIX_ROW_END_CHARPOS (row)
15109 /* Line is completely visible last line in window
15110 and PT is to be set in the next line. */
15111 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15112 && PT == MATRIX_ROW_END_CHARPOS (row)
15113 && !row->ends_at_zv_p
15114 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15115 scroll_p = 1;
15117 else if (PT < w->last_point)
15119 /* Cursor has to be moved backward. Note that PT >=
15120 CHARPOS (startp) because of the outer if-statement. */
15121 while (!row->mode_line_p
15122 && (MATRIX_ROW_START_CHARPOS (row) > PT
15123 || (MATRIX_ROW_START_CHARPOS (row) == PT
15124 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15125 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15126 row > w->current_matrix->rows
15127 && (row-1)->ends_in_newline_from_string_p))))
15128 && (row->y > top_scroll_margin
15129 || CHARPOS (startp) == BEGV))
15131 eassert (row->enabled_p);
15132 --row;
15135 /* Consider the following case: Window starts at BEGV,
15136 there is invisible, intangible text at BEGV, so that
15137 display starts at some point START > BEGV. It can
15138 happen that we are called with PT somewhere between
15139 BEGV and START. Try to handle that case. */
15140 if (row < w->current_matrix->rows
15141 || row->mode_line_p)
15143 row = w->current_matrix->rows;
15144 if (row->mode_line_p)
15145 ++row;
15148 /* Due to newlines in overlay strings, we may have to
15149 skip forward over overlay strings. */
15150 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15151 && MATRIX_ROW_END_CHARPOS (row) == PT
15152 && !cursor_row_p (row))
15153 ++row;
15155 /* If within the scroll margin, scroll. */
15156 if (row->y < top_scroll_margin
15157 && CHARPOS (startp) != BEGV)
15158 scroll_p = 1;
15160 else
15162 /* Cursor did not move. So don't scroll even if cursor line
15163 is partially visible, as it was so before. */
15164 rc = CURSOR_MOVEMENT_SUCCESS;
15167 if (PT < MATRIX_ROW_START_CHARPOS (row)
15168 || PT > MATRIX_ROW_END_CHARPOS (row))
15170 /* if PT is not in the glyph row, give up. */
15171 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15172 must_scroll = 1;
15174 else if (rc != CURSOR_MOVEMENT_SUCCESS
15175 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15177 struct glyph_row *row1;
15179 /* If rows are bidi-reordered and point moved, back up
15180 until we find a row that does not belong to a
15181 continuation line. This is because we must consider
15182 all rows of a continued line as candidates for the
15183 new cursor positioning, since row start and end
15184 positions change non-linearly with vertical position
15185 in such rows. */
15186 /* FIXME: Revisit this when glyph ``spilling'' in
15187 continuation lines' rows is implemented for
15188 bidi-reordered rows. */
15189 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15190 MATRIX_ROW_CONTINUATION_LINE_P (row);
15191 --row)
15193 /* If we hit the beginning of the displayed portion
15194 without finding the first row of a continued
15195 line, give up. */
15196 if (row <= row1)
15198 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15199 break;
15201 eassert (row->enabled_p);
15204 if (must_scroll)
15206 else if (rc != CURSOR_MOVEMENT_SUCCESS
15207 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15208 /* Make sure this isn't a header line by any chance, since
15209 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15210 && !row->mode_line_p
15211 && make_cursor_line_fully_visible_p)
15213 if (PT == MATRIX_ROW_END_CHARPOS (row)
15214 && !row->ends_at_zv_p
15215 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15216 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15217 else if (row->height > window_box_height (w))
15219 /* If we end up in a partially visible line, let's
15220 make it fully visible, except when it's taller
15221 than the window, in which case we can't do much
15222 about it. */
15223 *scroll_step = 1;
15224 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15226 else
15228 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15229 if (!cursor_row_fully_visible_p (w, 0, 1))
15230 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15231 else
15232 rc = CURSOR_MOVEMENT_SUCCESS;
15235 else if (scroll_p)
15236 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15237 else if (rc != CURSOR_MOVEMENT_SUCCESS
15238 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15240 /* With bidi-reordered rows, there could be more than
15241 one candidate row whose start and end positions
15242 occlude point. We need to let set_cursor_from_row
15243 find the best candidate. */
15244 /* FIXME: Revisit this when glyph ``spilling'' in
15245 continuation lines' rows is implemented for
15246 bidi-reordered rows. */
15247 int rv = 0;
15251 int at_zv_p = 0, exact_match_p = 0;
15253 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15254 && PT <= MATRIX_ROW_END_CHARPOS (row)
15255 && cursor_row_p (row))
15256 rv |= set_cursor_from_row (w, row, w->current_matrix,
15257 0, 0, 0, 0);
15258 /* As soon as we've found the exact match for point,
15259 or the first suitable row whose ends_at_zv_p flag
15260 is set, we are done. */
15261 at_zv_p =
15262 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15263 if (rv && !at_zv_p
15264 && w->cursor.hpos >= 0
15265 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15266 w->cursor.vpos))
15268 struct glyph_row *candidate =
15269 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15270 struct glyph *g =
15271 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15272 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15274 exact_match_p =
15275 (BUFFERP (g->object) && g->charpos == PT)
15276 || (INTEGERP (g->object)
15277 && (g->charpos == PT
15278 || (g->charpos == 0 && endpos - 1 == PT)));
15280 if (rv && (at_zv_p || exact_match_p))
15282 rc = CURSOR_MOVEMENT_SUCCESS;
15283 break;
15285 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15286 break;
15287 ++row;
15289 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15290 || row->continued_p)
15291 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15292 || (MATRIX_ROW_START_CHARPOS (row) == PT
15293 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15294 /* If we didn't find any candidate rows, or exited the
15295 loop before all the candidates were examined, signal
15296 to the caller that this method failed. */
15297 if (rc != CURSOR_MOVEMENT_SUCCESS
15298 && !(rv
15299 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15300 && !row->continued_p))
15301 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15302 else if (rv)
15303 rc = CURSOR_MOVEMENT_SUCCESS;
15305 else
15309 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15311 rc = CURSOR_MOVEMENT_SUCCESS;
15312 break;
15314 ++row;
15316 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15317 && MATRIX_ROW_START_CHARPOS (row) == PT
15318 && cursor_row_p (row));
15323 return rc;
15326 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15327 static
15328 #endif
15329 void
15330 set_vertical_scroll_bar (struct window *w)
15332 ptrdiff_t start, end, whole;
15334 /* Calculate the start and end positions for the current window.
15335 At some point, it would be nice to choose between scrollbars
15336 which reflect the whole buffer size, with special markers
15337 indicating narrowing, and scrollbars which reflect only the
15338 visible region.
15340 Note that mini-buffers sometimes aren't displaying any text. */
15341 if (!MINI_WINDOW_P (w)
15342 || (w == XWINDOW (minibuf_window)
15343 && NILP (echo_area_buffer[0])))
15345 struct buffer *buf = XBUFFER (w->buffer);
15346 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15347 start = marker_position (w->start) - BUF_BEGV (buf);
15348 /* I don't think this is guaranteed to be right. For the
15349 moment, we'll pretend it is. */
15350 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15352 if (end < start)
15353 end = start;
15354 if (whole < (end - start))
15355 whole = end - start;
15357 else
15358 start = end = whole = 0;
15360 /* Indicate what this scroll bar ought to be displaying now. */
15361 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15362 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15363 (w, end - start, whole, start);
15367 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15368 selected_window is redisplayed.
15370 We can return without actually redisplaying the window if
15371 fonts_changed_p. In that case, redisplay_internal will
15372 retry. */
15374 static void
15375 redisplay_window (Lisp_Object window, int just_this_one_p)
15377 struct window *w = XWINDOW (window);
15378 struct frame *f = XFRAME (w->frame);
15379 struct buffer *buffer = XBUFFER (w->buffer);
15380 struct buffer *old = current_buffer;
15381 struct text_pos lpoint, opoint, startp;
15382 int update_mode_line;
15383 int tem;
15384 struct it it;
15385 /* Record it now because it's overwritten. */
15386 int current_matrix_up_to_date_p = 0;
15387 int used_current_matrix_p = 0;
15388 /* This is less strict than current_matrix_up_to_date_p.
15389 It indicates that the buffer contents and narrowing are unchanged. */
15390 int buffer_unchanged_p = 0;
15391 int temp_scroll_step = 0;
15392 ptrdiff_t count = SPECPDL_INDEX ();
15393 int rc;
15394 int centering_position = -1;
15395 int last_line_misfit = 0;
15396 ptrdiff_t beg_unchanged, end_unchanged;
15398 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15399 opoint = lpoint;
15401 /* W must be a leaf window here. */
15402 eassert (!NILP (w->buffer));
15403 #ifdef GLYPH_DEBUG
15404 *w->desired_matrix->method = 0;
15405 #endif
15407 restart:
15408 reconsider_clip_changes (w, buffer);
15410 /* Has the mode line to be updated? */
15411 update_mode_line = (w->update_mode_line
15412 || update_mode_lines
15413 || buffer->clip_changed
15414 || buffer->prevent_redisplay_optimizations_p);
15416 if (MINI_WINDOW_P (w))
15418 if (w == XWINDOW (echo_area_window)
15419 && !NILP (echo_area_buffer[0]))
15421 if (update_mode_line)
15422 /* We may have to update a tty frame's menu bar or a
15423 tool-bar. Example `M-x C-h C-h C-g'. */
15424 goto finish_menu_bars;
15425 else
15426 /* We've already displayed the echo area glyphs in this window. */
15427 goto finish_scroll_bars;
15429 else if ((w != XWINDOW (minibuf_window)
15430 || minibuf_level == 0)
15431 /* When buffer is nonempty, redisplay window normally. */
15432 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15433 /* Quail displays non-mini buffers in minibuffer window.
15434 In that case, redisplay the window normally. */
15435 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15437 /* W is a mini-buffer window, but it's not active, so clear
15438 it. */
15439 int yb = window_text_bottom_y (w);
15440 struct glyph_row *row;
15441 int y;
15443 for (y = 0, row = w->desired_matrix->rows;
15444 y < yb;
15445 y += row->height, ++row)
15446 blank_row (w, row, y);
15447 goto finish_scroll_bars;
15450 clear_glyph_matrix (w->desired_matrix);
15453 /* Otherwise set up data on this window; select its buffer and point
15454 value. */
15455 /* Really select the buffer, for the sake of buffer-local
15456 variables. */
15457 set_buffer_internal_1 (XBUFFER (w->buffer));
15459 current_matrix_up_to_date_p
15460 = (!NILP (w->window_end_valid)
15461 && !current_buffer->clip_changed
15462 && !current_buffer->prevent_redisplay_optimizations_p
15463 && w->last_modified >= MODIFF
15464 && w->last_overlay_modified >= OVERLAY_MODIFF);
15466 /* Run the window-bottom-change-functions
15467 if it is possible that the text on the screen has changed
15468 (either due to modification of the text, or any other reason). */
15469 if (!current_matrix_up_to_date_p
15470 && !NILP (Vwindow_text_change_functions))
15472 safe_run_hooks (Qwindow_text_change_functions);
15473 goto restart;
15476 beg_unchanged = BEG_UNCHANGED;
15477 end_unchanged = END_UNCHANGED;
15479 SET_TEXT_POS (opoint, PT, PT_BYTE);
15481 specbind (Qinhibit_point_motion_hooks, Qt);
15483 buffer_unchanged_p
15484 = (!NILP (w->window_end_valid)
15485 && !current_buffer->clip_changed
15486 && w->last_modified >= MODIFF
15487 && w->last_overlay_modified >= OVERLAY_MODIFF);
15489 /* When windows_or_buffers_changed is non-zero, we can't rely on
15490 the window end being valid, so set it to nil there. */
15491 if (windows_or_buffers_changed)
15493 /* If window starts on a continuation line, maybe adjust the
15494 window start in case the window's width changed. */
15495 if (XMARKER (w->start)->buffer == current_buffer)
15496 compute_window_start_on_continuation_line (w);
15498 wset_window_end_valid (w, Qnil);
15501 /* Some sanity checks. */
15502 CHECK_WINDOW_END (w);
15503 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15504 emacs_abort ();
15505 if (BYTEPOS (opoint) < CHARPOS (opoint))
15506 emacs_abort ();
15508 /* If %c is in mode line, update it if needed. */
15509 if (!NILP (w->column_number_displayed)
15510 /* This alternative quickly identifies a common case
15511 where no change is needed. */
15512 && !(PT == w->last_point
15513 && w->last_modified >= MODIFF
15514 && w->last_overlay_modified >= OVERLAY_MODIFF)
15515 && (XFASTINT (w->column_number_displayed) != current_column ()))
15516 update_mode_line = 1;
15518 /* Count number of windows showing the selected buffer. An indirect
15519 buffer counts as its base buffer. */
15520 if (!just_this_one_p)
15522 struct buffer *current_base, *window_base;
15523 current_base = current_buffer;
15524 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15525 if (current_base->base_buffer)
15526 current_base = current_base->base_buffer;
15527 if (window_base->base_buffer)
15528 window_base = window_base->base_buffer;
15529 if (current_base == window_base)
15530 buffer_shared++;
15533 /* Point refers normally to the selected window. For any other
15534 window, set up appropriate value. */
15535 if (!EQ (window, selected_window))
15537 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15538 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15539 if (new_pt < BEGV)
15541 new_pt = BEGV;
15542 new_pt_byte = BEGV_BYTE;
15543 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15545 else if (new_pt > (ZV - 1))
15547 new_pt = ZV;
15548 new_pt_byte = ZV_BYTE;
15549 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15552 /* We don't use SET_PT so that the point-motion hooks don't run. */
15553 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15556 /* If any of the character widths specified in the display table
15557 have changed, invalidate the width run cache. It's true that
15558 this may be a bit late to catch such changes, but the rest of
15559 redisplay goes (non-fatally) haywire when the display table is
15560 changed, so why should we worry about doing any better? */
15561 if (current_buffer->width_run_cache)
15563 struct Lisp_Char_Table *disptab = buffer_display_table ();
15565 if (! disptab_matches_widthtab
15566 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15568 invalidate_region_cache (current_buffer,
15569 current_buffer->width_run_cache,
15570 BEG, Z);
15571 recompute_width_table (current_buffer, disptab);
15575 /* If window-start is screwed up, choose a new one. */
15576 if (XMARKER (w->start)->buffer != current_buffer)
15577 goto recenter;
15579 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15581 /* If someone specified a new starting point but did not insist,
15582 check whether it can be used. */
15583 if (w->optional_new_start
15584 && CHARPOS (startp) >= BEGV
15585 && CHARPOS (startp) <= ZV)
15587 w->optional_new_start = 0;
15588 start_display (&it, w, startp);
15589 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15590 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15591 if (IT_CHARPOS (it) == PT)
15592 w->force_start = 1;
15593 /* IT may overshoot PT if text at PT is invisible. */
15594 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15595 w->force_start = 1;
15598 force_start:
15600 /* Handle case where place to start displaying has been specified,
15601 unless the specified location is outside the accessible range. */
15602 if (w->force_start || w->frozen_window_start_p)
15604 /* We set this later on if we have to adjust point. */
15605 int new_vpos = -1;
15607 w->force_start = 0;
15608 w->vscroll = 0;
15609 wset_window_end_valid (w, Qnil);
15611 /* Forget any recorded base line for line number display. */
15612 if (!buffer_unchanged_p)
15613 wset_base_line_number (w, Qnil);
15615 /* Redisplay the mode line. Select the buffer properly for that.
15616 Also, run the hook window-scroll-functions
15617 because we have scrolled. */
15618 /* Note, we do this after clearing force_start because
15619 if there's an error, it is better to forget about force_start
15620 than to get into an infinite loop calling the hook functions
15621 and having them get more errors. */
15622 if (!update_mode_line
15623 || ! NILP (Vwindow_scroll_functions))
15625 update_mode_line = 1;
15626 w->update_mode_line = 1;
15627 startp = run_window_scroll_functions (window, startp);
15630 w->last_modified = 0;
15631 w->last_overlay_modified = 0;
15632 if (CHARPOS (startp) < BEGV)
15633 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15634 else if (CHARPOS (startp) > ZV)
15635 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15637 /* Redisplay, then check if cursor has been set during the
15638 redisplay. Give up if new fonts were loaded. */
15639 /* We used to issue a CHECK_MARGINS argument to try_window here,
15640 but this causes scrolling to fail when point begins inside
15641 the scroll margin (bug#148) -- cyd */
15642 if (!try_window (window, startp, 0))
15644 w->force_start = 1;
15645 clear_glyph_matrix (w->desired_matrix);
15646 goto need_larger_matrices;
15649 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15651 /* If point does not appear, try to move point so it does
15652 appear. The desired matrix has been built above, so we
15653 can use it here. */
15654 new_vpos = window_box_height (w) / 2;
15657 if (!cursor_row_fully_visible_p (w, 0, 0))
15659 /* Point does appear, but on a line partly visible at end of window.
15660 Move it back to a fully-visible line. */
15661 new_vpos = window_box_height (w);
15664 /* If we need to move point for either of the above reasons,
15665 now actually do it. */
15666 if (new_vpos >= 0)
15668 struct glyph_row *row;
15670 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15671 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15672 ++row;
15674 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15675 MATRIX_ROW_START_BYTEPOS (row));
15677 if (w != XWINDOW (selected_window))
15678 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15679 else if (current_buffer == old)
15680 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15682 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15684 /* If we are highlighting the region, then we just changed
15685 the region, so redisplay to show it. */
15686 if (!NILP (Vtransient_mark_mode)
15687 && !NILP (BVAR (current_buffer, mark_active)))
15689 clear_glyph_matrix (w->desired_matrix);
15690 if (!try_window (window, startp, 0))
15691 goto need_larger_matrices;
15695 #ifdef GLYPH_DEBUG
15696 debug_method_add (w, "forced window start");
15697 #endif
15698 goto done;
15701 /* Handle case where text has not changed, only point, and it has
15702 not moved off the frame, and we are not retrying after hscroll.
15703 (current_matrix_up_to_date_p is nonzero when retrying.) */
15704 if (current_matrix_up_to_date_p
15705 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15706 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15708 switch (rc)
15710 case CURSOR_MOVEMENT_SUCCESS:
15711 used_current_matrix_p = 1;
15712 goto done;
15714 case CURSOR_MOVEMENT_MUST_SCROLL:
15715 goto try_to_scroll;
15717 default:
15718 emacs_abort ();
15721 /* If current starting point was originally the beginning of a line
15722 but no longer is, find a new starting point. */
15723 else if (w->start_at_line_beg
15724 && !(CHARPOS (startp) <= BEGV
15725 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15727 #ifdef GLYPH_DEBUG
15728 debug_method_add (w, "recenter 1");
15729 #endif
15730 goto recenter;
15733 /* Try scrolling with try_window_id. Value is > 0 if update has
15734 been done, it is -1 if we know that the same window start will
15735 not work. It is 0 if unsuccessful for some other reason. */
15736 else if ((tem = try_window_id (w)) != 0)
15738 #ifdef GLYPH_DEBUG
15739 debug_method_add (w, "try_window_id %d", tem);
15740 #endif
15742 if (fonts_changed_p)
15743 goto need_larger_matrices;
15744 if (tem > 0)
15745 goto done;
15747 /* Otherwise try_window_id has returned -1 which means that we
15748 don't want the alternative below this comment to execute. */
15750 else if (CHARPOS (startp) >= BEGV
15751 && CHARPOS (startp) <= ZV
15752 && PT >= CHARPOS (startp)
15753 && (CHARPOS (startp) < ZV
15754 /* Avoid starting at end of buffer. */
15755 || CHARPOS (startp) == BEGV
15756 || (w->last_modified >= MODIFF
15757 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15759 int d1, d2, d3, d4, d5, d6;
15761 /* If first window line is a continuation line, and window start
15762 is inside the modified region, but the first change is before
15763 current window start, we must select a new window start.
15765 However, if this is the result of a down-mouse event (e.g. by
15766 extending the mouse-drag-overlay), we don't want to select a
15767 new window start, since that would change the position under
15768 the mouse, resulting in an unwanted mouse-movement rather
15769 than a simple mouse-click. */
15770 if (!w->start_at_line_beg
15771 && NILP (do_mouse_tracking)
15772 && CHARPOS (startp) > BEGV
15773 && CHARPOS (startp) > BEG + beg_unchanged
15774 && CHARPOS (startp) <= Z - end_unchanged
15775 /* Even if w->start_at_line_beg is nil, a new window may
15776 start at a line_beg, since that's how set_buffer_window
15777 sets it. So, we need to check the return value of
15778 compute_window_start_on_continuation_line. (See also
15779 bug#197). */
15780 && XMARKER (w->start)->buffer == current_buffer
15781 && compute_window_start_on_continuation_line (w)
15782 /* It doesn't make sense to force the window start like we
15783 do at label force_start if it is already known that point
15784 will not be visible in the resulting window, because
15785 doing so will move point from its correct position
15786 instead of scrolling the window to bring point into view.
15787 See bug#9324. */
15788 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15790 w->force_start = 1;
15791 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15792 goto force_start;
15795 #ifdef GLYPH_DEBUG
15796 debug_method_add (w, "same window start");
15797 #endif
15799 /* Try to redisplay starting at same place as before.
15800 If point has not moved off frame, accept the results. */
15801 if (!current_matrix_up_to_date_p
15802 /* Don't use try_window_reusing_current_matrix in this case
15803 because a window scroll function can have changed the
15804 buffer. */
15805 || !NILP (Vwindow_scroll_functions)
15806 || MINI_WINDOW_P (w)
15807 || !(used_current_matrix_p
15808 = try_window_reusing_current_matrix (w)))
15810 IF_DEBUG (debug_method_add (w, "1"));
15811 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15812 /* -1 means we need to scroll.
15813 0 means we need new matrices, but fonts_changed_p
15814 is set in that case, so we will detect it below. */
15815 goto try_to_scroll;
15818 if (fonts_changed_p)
15819 goto need_larger_matrices;
15821 if (w->cursor.vpos >= 0)
15823 if (!just_this_one_p
15824 || current_buffer->clip_changed
15825 || BEG_UNCHANGED < CHARPOS (startp))
15826 /* Forget any recorded base line for line number display. */
15827 wset_base_line_number (w, Qnil);
15829 if (!cursor_row_fully_visible_p (w, 1, 0))
15831 clear_glyph_matrix (w->desired_matrix);
15832 last_line_misfit = 1;
15834 /* Drop through and scroll. */
15835 else
15836 goto done;
15838 else
15839 clear_glyph_matrix (w->desired_matrix);
15842 try_to_scroll:
15844 w->last_modified = 0;
15845 w->last_overlay_modified = 0;
15847 /* Redisplay the mode line. Select the buffer properly for that. */
15848 if (!update_mode_line)
15850 update_mode_line = 1;
15851 w->update_mode_line = 1;
15854 /* Try to scroll by specified few lines. */
15855 if ((scroll_conservatively
15856 || emacs_scroll_step
15857 || temp_scroll_step
15858 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15859 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15860 && CHARPOS (startp) >= BEGV
15861 && CHARPOS (startp) <= ZV)
15863 /* The function returns -1 if new fonts were loaded, 1 if
15864 successful, 0 if not successful. */
15865 int ss = try_scrolling (window, just_this_one_p,
15866 scroll_conservatively,
15867 emacs_scroll_step,
15868 temp_scroll_step, last_line_misfit);
15869 switch (ss)
15871 case SCROLLING_SUCCESS:
15872 goto done;
15874 case SCROLLING_NEED_LARGER_MATRICES:
15875 goto need_larger_matrices;
15877 case SCROLLING_FAILED:
15878 break;
15880 default:
15881 emacs_abort ();
15885 /* Finally, just choose a place to start which positions point
15886 according to user preferences. */
15888 recenter:
15890 #ifdef GLYPH_DEBUG
15891 debug_method_add (w, "recenter");
15892 #endif
15894 /* w->vscroll = 0; */
15896 /* Forget any previously recorded base line for line number display. */
15897 if (!buffer_unchanged_p)
15898 wset_base_line_number (w, Qnil);
15900 /* Determine the window start relative to point. */
15901 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15902 it.current_y = it.last_visible_y;
15903 if (centering_position < 0)
15905 int margin =
15906 scroll_margin > 0
15907 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15908 : 0;
15909 ptrdiff_t margin_pos = CHARPOS (startp);
15910 Lisp_Object aggressive;
15911 int scrolling_up;
15913 /* If there is a scroll margin at the top of the window, find
15914 its character position. */
15915 if (margin
15916 /* Cannot call start_display if startp is not in the
15917 accessible region of the buffer. This can happen when we
15918 have just switched to a different buffer and/or changed
15919 its restriction. In that case, startp is initialized to
15920 the character position 1 (BEGV) because we did not yet
15921 have chance to display the buffer even once. */
15922 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15924 struct it it1;
15925 void *it1data = NULL;
15927 SAVE_IT (it1, it, it1data);
15928 start_display (&it1, w, startp);
15929 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15930 margin_pos = IT_CHARPOS (it1);
15931 RESTORE_IT (&it, &it, it1data);
15933 scrolling_up = PT > margin_pos;
15934 aggressive =
15935 scrolling_up
15936 ? BVAR (current_buffer, scroll_up_aggressively)
15937 : BVAR (current_buffer, scroll_down_aggressively);
15939 if (!MINI_WINDOW_P (w)
15940 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15942 int pt_offset = 0;
15944 /* Setting scroll-conservatively overrides
15945 scroll-*-aggressively. */
15946 if (!scroll_conservatively && NUMBERP (aggressive))
15948 double float_amount = XFLOATINT (aggressive);
15950 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15951 if (pt_offset == 0 && float_amount > 0)
15952 pt_offset = 1;
15953 if (pt_offset && margin > 0)
15954 margin -= 1;
15956 /* Compute how much to move the window start backward from
15957 point so that point will be displayed where the user
15958 wants it. */
15959 if (scrolling_up)
15961 centering_position = it.last_visible_y;
15962 if (pt_offset)
15963 centering_position -= pt_offset;
15964 centering_position -=
15965 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15966 + WINDOW_HEADER_LINE_HEIGHT (w);
15967 /* Don't let point enter the scroll margin near top of
15968 the window. */
15969 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15970 centering_position = margin * FRAME_LINE_HEIGHT (f);
15972 else
15973 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15975 else
15976 /* Set the window start half the height of the window backward
15977 from point. */
15978 centering_position = window_box_height (w) / 2;
15980 move_it_vertically_backward (&it, centering_position);
15982 eassert (IT_CHARPOS (it) >= BEGV);
15984 /* The function move_it_vertically_backward may move over more
15985 than the specified y-distance. If it->w is small, e.g. a
15986 mini-buffer window, we may end up in front of the window's
15987 display area. Start displaying at the start of the line
15988 containing PT in this case. */
15989 if (it.current_y <= 0)
15991 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15992 move_it_vertically_backward (&it, 0);
15993 it.current_y = 0;
15996 it.current_x = it.hpos = 0;
15998 /* Set the window start position here explicitly, to avoid an
15999 infinite loop in case the functions in window-scroll-functions
16000 get errors. */
16001 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16003 /* Run scroll hooks. */
16004 startp = run_window_scroll_functions (window, it.current.pos);
16006 /* Redisplay the window. */
16007 if (!current_matrix_up_to_date_p
16008 || windows_or_buffers_changed
16009 || cursor_type_changed
16010 /* Don't use try_window_reusing_current_matrix in this case
16011 because it can have changed the buffer. */
16012 || !NILP (Vwindow_scroll_functions)
16013 || !just_this_one_p
16014 || MINI_WINDOW_P (w)
16015 || !(used_current_matrix_p
16016 = try_window_reusing_current_matrix (w)))
16017 try_window (window, startp, 0);
16019 /* If new fonts have been loaded (due to fontsets), give up. We
16020 have to start a new redisplay since we need to re-adjust glyph
16021 matrices. */
16022 if (fonts_changed_p)
16023 goto need_larger_matrices;
16025 /* If cursor did not appear assume that the middle of the window is
16026 in the first line of the window. Do it again with the next line.
16027 (Imagine a window of height 100, displaying two lines of height
16028 60. Moving back 50 from it->last_visible_y will end in the first
16029 line.) */
16030 if (w->cursor.vpos < 0)
16032 if (!NILP (w->window_end_valid)
16033 && PT >= Z - XFASTINT (w->window_end_pos))
16035 clear_glyph_matrix (w->desired_matrix);
16036 move_it_by_lines (&it, 1);
16037 try_window (window, it.current.pos, 0);
16039 else if (PT < IT_CHARPOS (it))
16041 clear_glyph_matrix (w->desired_matrix);
16042 move_it_by_lines (&it, -1);
16043 try_window (window, it.current.pos, 0);
16045 else
16047 /* Not much we can do about it. */
16051 /* Consider the following case: Window starts at BEGV, there is
16052 invisible, intangible text at BEGV, so that display starts at
16053 some point START > BEGV. It can happen that we are called with
16054 PT somewhere between BEGV and START. Try to handle that case. */
16055 if (w->cursor.vpos < 0)
16057 struct glyph_row *row = w->current_matrix->rows;
16058 if (row->mode_line_p)
16059 ++row;
16060 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16063 if (!cursor_row_fully_visible_p (w, 0, 0))
16065 /* If vscroll is enabled, disable it and try again. */
16066 if (w->vscroll)
16068 w->vscroll = 0;
16069 clear_glyph_matrix (w->desired_matrix);
16070 goto recenter;
16073 /* Users who set scroll-conservatively to a large number want
16074 point just above/below the scroll margin. If we ended up
16075 with point's row partially visible, move the window start to
16076 make that row fully visible and out of the margin. */
16077 if (scroll_conservatively > SCROLL_LIMIT)
16079 int margin =
16080 scroll_margin > 0
16081 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16082 : 0;
16083 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16085 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16086 clear_glyph_matrix (w->desired_matrix);
16087 if (1 == try_window (window, it.current.pos,
16088 TRY_WINDOW_CHECK_MARGINS))
16089 goto done;
16092 /* If centering point failed to make the whole line visible,
16093 put point at the top instead. That has to make the whole line
16094 visible, if it can be done. */
16095 if (centering_position == 0)
16096 goto done;
16098 clear_glyph_matrix (w->desired_matrix);
16099 centering_position = 0;
16100 goto recenter;
16103 done:
16105 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16106 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16107 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16109 /* Display the mode line, if we must. */
16110 if ((update_mode_line
16111 /* If window not full width, must redo its mode line
16112 if (a) the window to its side is being redone and
16113 (b) we do a frame-based redisplay. This is a consequence
16114 of how inverted lines are drawn in frame-based redisplay. */
16115 || (!just_this_one_p
16116 && !FRAME_WINDOW_P (f)
16117 && !WINDOW_FULL_WIDTH_P (w))
16118 /* Line number to display. */
16119 || INTEGERP (w->base_line_pos)
16120 /* Column number is displayed and different from the one displayed. */
16121 || (!NILP (w->column_number_displayed)
16122 && (XFASTINT (w->column_number_displayed) != current_column ())))
16123 /* This means that the window has a mode line. */
16124 && (WINDOW_WANTS_MODELINE_P (w)
16125 || WINDOW_WANTS_HEADER_LINE_P (w)))
16127 display_mode_lines (w);
16129 /* If mode line height has changed, arrange for a thorough
16130 immediate redisplay using the correct mode line height. */
16131 if (WINDOW_WANTS_MODELINE_P (w)
16132 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16134 fonts_changed_p = 1;
16135 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16136 = DESIRED_MODE_LINE_HEIGHT (w);
16139 /* If header line height has changed, arrange for a thorough
16140 immediate redisplay using the correct header line height. */
16141 if (WINDOW_WANTS_HEADER_LINE_P (w)
16142 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16144 fonts_changed_p = 1;
16145 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16146 = DESIRED_HEADER_LINE_HEIGHT (w);
16149 if (fonts_changed_p)
16150 goto need_larger_matrices;
16153 if (!line_number_displayed
16154 && !BUFFERP (w->base_line_pos))
16156 wset_base_line_pos (w, Qnil);
16157 wset_base_line_number (w, Qnil);
16160 finish_menu_bars:
16162 /* When we reach a frame's selected window, redo the frame's menu bar. */
16163 if (update_mode_line
16164 && EQ (FRAME_SELECTED_WINDOW (f), window))
16166 int redisplay_menu_p = 0;
16168 if (FRAME_WINDOW_P (f))
16170 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16171 || defined (HAVE_NS) || defined (USE_GTK)
16172 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16173 #else
16174 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16175 #endif
16177 else
16178 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16180 if (redisplay_menu_p)
16181 display_menu_bar (w);
16183 #ifdef HAVE_WINDOW_SYSTEM
16184 if (FRAME_WINDOW_P (f))
16186 #if defined (USE_GTK) || defined (HAVE_NS)
16187 if (FRAME_EXTERNAL_TOOL_BAR (f))
16188 redisplay_tool_bar (f);
16189 #else
16190 if (WINDOWP (f->tool_bar_window)
16191 && (FRAME_TOOL_BAR_LINES (f) > 0
16192 || !NILP (Vauto_resize_tool_bars))
16193 && redisplay_tool_bar (f))
16194 ignore_mouse_drag_p = 1;
16195 #endif
16197 #endif
16200 #ifdef HAVE_WINDOW_SYSTEM
16201 if (FRAME_WINDOW_P (f)
16202 && update_window_fringes (w, (just_this_one_p
16203 || (!used_current_matrix_p && !overlay_arrow_seen)
16204 || w->pseudo_window_p)))
16206 update_begin (f);
16207 BLOCK_INPUT;
16208 if (draw_window_fringes (w, 1))
16209 x_draw_vertical_border (w);
16210 UNBLOCK_INPUT;
16211 update_end (f);
16213 #endif /* HAVE_WINDOW_SYSTEM */
16215 /* We go to this label, with fonts_changed_p set,
16216 if it is necessary to try again using larger glyph matrices.
16217 We have to redeem the scroll bar even in this case,
16218 because the loop in redisplay_internal expects that. */
16219 need_larger_matrices:
16221 finish_scroll_bars:
16223 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16225 /* Set the thumb's position and size. */
16226 set_vertical_scroll_bar (w);
16228 /* Note that we actually used the scroll bar attached to this
16229 window, so it shouldn't be deleted at the end of redisplay. */
16230 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16231 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16234 /* Restore current_buffer and value of point in it. The window
16235 update may have changed the buffer, so first make sure `opoint'
16236 is still valid (Bug#6177). */
16237 if (CHARPOS (opoint) < BEGV)
16238 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16239 else if (CHARPOS (opoint) > ZV)
16240 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16241 else
16242 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16244 set_buffer_internal_1 (old);
16245 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16246 shorter. This can be caused by log truncation in *Messages*. */
16247 if (CHARPOS (lpoint) <= ZV)
16248 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16250 unbind_to (count, Qnil);
16254 /* Build the complete desired matrix of WINDOW with a window start
16255 buffer position POS.
16257 Value is 1 if successful. It is zero if fonts were loaded during
16258 redisplay which makes re-adjusting glyph matrices necessary, and -1
16259 if point would appear in the scroll margins.
16260 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16261 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16262 set in FLAGS.) */
16265 try_window (Lisp_Object window, struct text_pos pos, int flags)
16267 struct window *w = XWINDOW (window);
16268 struct it it;
16269 struct glyph_row *last_text_row = NULL;
16270 struct frame *f = XFRAME (w->frame);
16272 /* Make POS the new window start. */
16273 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16275 /* Mark cursor position as unknown. No overlay arrow seen. */
16276 w->cursor.vpos = -1;
16277 overlay_arrow_seen = 0;
16279 /* Initialize iterator and info to start at POS. */
16280 start_display (&it, w, pos);
16282 /* Display all lines of W. */
16283 while (it.current_y < it.last_visible_y)
16285 if (display_line (&it))
16286 last_text_row = it.glyph_row - 1;
16287 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16288 return 0;
16291 /* Don't let the cursor end in the scroll margins. */
16292 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16293 && !MINI_WINDOW_P (w))
16295 int this_scroll_margin;
16297 if (scroll_margin > 0)
16299 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16300 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16302 else
16303 this_scroll_margin = 0;
16305 if ((w->cursor.y >= 0 /* not vscrolled */
16306 && w->cursor.y < this_scroll_margin
16307 && CHARPOS (pos) > BEGV
16308 && IT_CHARPOS (it) < ZV)
16309 /* rms: considering make_cursor_line_fully_visible_p here
16310 seems to give wrong results. We don't want to recenter
16311 when the last line is partly visible, we want to allow
16312 that case to be handled in the usual way. */
16313 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16315 w->cursor.vpos = -1;
16316 clear_glyph_matrix (w->desired_matrix);
16317 return -1;
16321 /* If bottom moved off end of frame, change mode line percentage. */
16322 if (XFASTINT (w->window_end_pos) <= 0
16323 && Z != IT_CHARPOS (it))
16324 w->update_mode_line = 1;
16326 /* Set window_end_pos to the offset of the last character displayed
16327 on the window from the end of current_buffer. Set
16328 window_end_vpos to its row number. */
16329 if (last_text_row)
16331 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16332 w->window_end_bytepos
16333 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16334 wset_window_end_pos
16335 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16336 wset_window_end_vpos
16337 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16338 eassert
16339 (MATRIX_ROW (w->desired_matrix,
16340 XFASTINT (w->window_end_vpos))->displays_text_p);
16342 else
16344 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16345 wset_window_end_pos (w, make_number (Z - ZV));
16346 wset_window_end_vpos (w, make_number (0));
16349 /* But that is not valid info until redisplay finishes. */
16350 wset_window_end_valid (w, Qnil);
16351 return 1;
16356 /************************************************************************
16357 Window redisplay reusing current matrix when buffer has not changed
16358 ************************************************************************/
16360 /* Try redisplay of window W showing an unchanged buffer with a
16361 different window start than the last time it was displayed by
16362 reusing its current matrix. Value is non-zero if successful.
16363 W->start is the new window start. */
16365 static int
16366 try_window_reusing_current_matrix (struct window *w)
16368 struct frame *f = XFRAME (w->frame);
16369 struct glyph_row *bottom_row;
16370 struct it it;
16371 struct run run;
16372 struct text_pos start, new_start;
16373 int nrows_scrolled, i;
16374 struct glyph_row *last_text_row;
16375 struct glyph_row *last_reused_text_row;
16376 struct glyph_row *start_row;
16377 int start_vpos, min_y, max_y;
16379 #ifdef GLYPH_DEBUG
16380 if (inhibit_try_window_reusing)
16381 return 0;
16382 #endif
16384 if (/* This function doesn't handle terminal frames. */
16385 !FRAME_WINDOW_P (f)
16386 /* Don't try to reuse the display if windows have been split
16387 or such. */
16388 || windows_or_buffers_changed
16389 || cursor_type_changed)
16390 return 0;
16392 /* Can't do this if region may have changed. */
16393 if ((!NILP (Vtransient_mark_mode)
16394 && !NILP (BVAR (current_buffer, mark_active)))
16395 || !NILP (w->region_showing)
16396 || !NILP (Vshow_trailing_whitespace))
16397 return 0;
16399 /* If top-line visibility has changed, give up. */
16400 if (WINDOW_WANTS_HEADER_LINE_P (w)
16401 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16402 return 0;
16404 /* Give up if old or new display is scrolled vertically. We could
16405 make this function handle this, but right now it doesn't. */
16406 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16407 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16408 return 0;
16410 /* The variable new_start now holds the new window start. The old
16411 start `start' can be determined from the current matrix. */
16412 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16413 start = start_row->minpos;
16414 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16416 /* Clear the desired matrix for the display below. */
16417 clear_glyph_matrix (w->desired_matrix);
16419 if (CHARPOS (new_start) <= CHARPOS (start))
16421 /* Don't use this method if the display starts with an ellipsis
16422 displayed for invisible text. It's not easy to handle that case
16423 below, and it's certainly not worth the effort since this is
16424 not a frequent case. */
16425 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16426 return 0;
16428 IF_DEBUG (debug_method_add (w, "twu1"));
16430 /* Display up to a row that can be reused. The variable
16431 last_text_row is set to the last row displayed that displays
16432 text. Note that it.vpos == 0 if or if not there is a
16433 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16434 start_display (&it, w, new_start);
16435 w->cursor.vpos = -1;
16436 last_text_row = last_reused_text_row = NULL;
16438 while (it.current_y < it.last_visible_y
16439 && !fonts_changed_p)
16441 /* If we have reached into the characters in the START row,
16442 that means the line boundaries have changed. So we
16443 can't start copying with the row START. Maybe it will
16444 work to start copying with the following row. */
16445 while (IT_CHARPOS (it) > CHARPOS (start))
16447 /* Advance to the next row as the "start". */
16448 start_row++;
16449 start = start_row->minpos;
16450 /* If there are no more rows to try, or just one, give up. */
16451 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16452 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16453 || CHARPOS (start) == ZV)
16455 clear_glyph_matrix (w->desired_matrix);
16456 return 0;
16459 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16461 /* If we have reached alignment, we can copy the rest of the
16462 rows. */
16463 if (IT_CHARPOS (it) == CHARPOS (start)
16464 /* Don't accept "alignment" inside a display vector,
16465 since start_row could have started in the middle of
16466 that same display vector (thus their character
16467 positions match), and we have no way of telling if
16468 that is the case. */
16469 && it.current.dpvec_index < 0)
16470 break;
16472 if (display_line (&it))
16473 last_text_row = it.glyph_row - 1;
16477 /* A value of current_y < last_visible_y means that we stopped
16478 at the previous window start, which in turn means that we
16479 have at least one reusable row. */
16480 if (it.current_y < it.last_visible_y)
16482 struct glyph_row *row;
16484 /* IT.vpos always starts from 0; it counts text lines. */
16485 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16487 /* Find PT if not already found in the lines displayed. */
16488 if (w->cursor.vpos < 0)
16490 int dy = it.current_y - start_row->y;
16492 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16493 row = row_containing_pos (w, PT, row, NULL, dy);
16494 if (row)
16495 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16496 dy, nrows_scrolled);
16497 else
16499 clear_glyph_matrix (w->desired_matrix);
16500 return 0;
16504 /* Scroll the display. Do it before the current matrix is
16505 changed. The problem here is that update has not yet
16506 run, i.e. part of the current matrix is not up to date.
16507 scroll_run_hook will clear the cursor, and use the
16508 current matrix to get the height of the row the cursor is
16509 in. */
16510 run.current_y = start_row->y;
16511 run.desired_y = it.current_y;
16512 run.height = it.last_visible_y - it.current_y;
16514 if (run.height > 0 && run.current_y != run.desired_y)
16516 update_begin (f);
16517 FRAME_RIF (f)->update_window_begin_hook (w);
16518 FRAME_RIF (f)->clear_window_mouse_face (w);
16519 FRAME_RIF (f)->scroll_run_hook (w, &run);
16520 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16521 update_end (f);
16524 /* Shift current matrix down by nrows_scrolled lines. */
16525 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16526 rotate_matrix (w->current_matrix,
16527 start_vpos,
16528 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16529 nrows_scrolled);
16531 /* Disable lines that must be updated. */
16532 for (i = 0; i < nrows_scrolled; ++i)
16533 (start_row + i)->enabled_p = 0;
16535 /* Re-compute Y positions. */
16536 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16537 max_y = it.last_visible_y;
16538 for (row = start_row + nrows_scrolled;
16539 row < bottom_row;
16540 ++row)
16542 row->y = it.current_y;
16543 row->visible_height = row->height;
16545 if (row->y < min_y)
16546 row->visible_height -= min_y - row->y;
16547 if (row->y + row->height > max_y)
16548 row->visible_height -= row->y + row->height - max_y;
16549 if (row->fringe_bitmap_periodic_p)
16550 row->redraw_fringe_bitmaps_p = 1;
16552 it.current_y += row->height;
16554 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16555 last_reused_text_row = row;
16556 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16557 break;
16560 /* Disable lines in the current matrix which are now
16561 below the window. */
16562 for (++row; row < bottom_row; ++row)
16563 row->enabled_p = row->mode_line_p = 0;
16566 /* Update window_end_pos etc.; last_reused_text_row is the last
16567 reused row from the current matrix containing text, if any.
16568 The value of last_text_row is the last displayed line
16569 containing text. */
16570 if (last_reused_text_row)
16572 w->window_end_bytepos
16573 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16574 wset_window_end_pos
16575 (w, make_number (Z
16576 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16577 wset_window_end_vpos
16578 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16579 w->current_matrix)));
16581 else if (last_text_row)
16583 w->window_end_bytepos
16584 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16585 wset_window_end_pos
16586 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16587 wset_window_end_vpos
16588 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16589 w->desired_matrix)));
16591 else
16593 /* This window must be completely empty. */
16594 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16595 wset_window_end_pos (w, make_number (Z - ZV));
16596 wset_window_end_vpos (w, make_number (0));
16598 wset_window_end_valid (w, Qnil);
16600 /* Update hint: don't try scrolling again in update_window. */
16601 w->desired_matrix->no_scrolling_p = 1;
16603 #ifdef GLYPH_DEBUG
16604 debug_method_add (w, "try_window_reusing_current_matrix 1");
16605 #endif
16606 return 1;
16608 else if (CHARPOS (new_start) > CHARPOS (start))
16610 struct glyph_row *pt_row, *row;
16611 struct glyph_row *first_reusable_row;
16612 struct glyph_row *first_row_to_display;
16613 int dy;
16614 int yb = window_text_bottom_y (w);
16616 /* Find the row starting at new_start, if there is one. Don't
16617 reuse a partially visible line at the end. */
16618 first_reusable_row = start_row;
16619 while (first_reusable_row->enabled_p
16620 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16621 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16622 < CHARPOS (new_start)))
16623 ++first_reusable_row;
16625 /* Give up if there is no row to reuse. */
16626 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16627 || !first_reusable_row->enabled_p
16628 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16629 != CHARPOS (new_start)))
16630 return 0;
16632 /* We can reuse fully visible rows beginning with
16633 first_reusable_row to the end of the window. Set
16634 first_row_to_display to the first row that cannot be reused.
16635 Set pt_row to the row containing point, if there is any. */
16636 pt_row = NULL;
16637 for (first_row_to_display = first_reusable_row;
16638 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16639 ++first_row_to_display)
16641 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16642 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16643 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16644 && first_row_to_display->ends_at_zv_p
16645 && pt_row == NULL)))
16646 pt_row = first_row_to_display;
16649 /* Start displaying at the start of first_row_to_display. */
16650 eassert (first_row_to_display->y < yb);
16651 init_to_row_start (&it, w, first_row_to_display);
16653 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16654 - start_vpos);
16655 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16656 - nrows_scrolled);
16657 it.current_y = (first_row_to_display->y - first_reusable_row->y
16658 + WINDOW_HEADER_LINE_HEIGHT (w));
16660 /* Display lines beginning with first_row_to_display in the
16661 desired matrix. Set last_text_row to the last row displayed
16662 that displays text. */
16663 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16664 if (pt_row == NULL)
16665 w->cursor.vpos = -1;
16666 last_text_row = NULL;
16667 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16668 if (display_line (&it))
16669 last_text_row = it.glyph_row - 1;
16671 /* If point is in a reused row, adjust y and vpos of the cursor
16672 position. */
16673 if (pt_row)
16675 w->cursor.vpos -= nrows_scrolled;
16676 w->cursor.y -= first_reusable_row->y - start_row->y;
16679 /* Give up if point isn't in a row displayed or reused. (This
16680 also handles the case where w->cursor.vpos < nrows_scrolled
16681 after the calls to display_line, which can happen with scroll
16682 margins. See bug#1295.) */
16683 if (w->cursor.vpos < 0)
16685 clear_glyph_matrix (w->desired_matrix);
16686 return 0;
16689 /* Scroll the display. */
16690 run.current_y = first_reusable_row->y;
16691 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16692 run.height = it.last_visible_y - run.current_y;
16693 dy = run.current_y - run.desired_y;
16695 if (run.height)
16697 update_begin (f);
16698 FRAME_RIF (f)->update_window_begin_hook (w);
16699 FRAME_RIF (f)->clear_window_mouse_face (w);
16700 FRAME_RIF (f)->scroll_run_hook (w, &run);
16701 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16702 update_end (f);
16705 /* Adjust Y positions of reused rows. */
16706 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16707 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16708 max_y = it.last_visible_y;
16709 for (row = first_reusable_row; row < first_row_to_display; ++row)
16711 row->y -= dy;
16712 row->visible_height = row->height;
16713 if (row->y < min_y)
16714 row->visible_height -= min_y - row->y;
16715 if (row->y + row->height > max_y)
16716 row->visible_height -= row->y + row->height - max_y;
16717 if (row->fringe_bitmap_periodic_p)
16718 row->redraw_fringe_bitmaps_p = 1;
16721 /* Scroll the current matrix. */
16722 eassert (nrows_scrolled > 0);
16723 rotate_matrix (w->current_matrix,
16724 start_vpos,
16725 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16726 -nrows_scrolled);
16728 /* Disable rows not reused. */
16729 for (row -= nrows_scrolled; row < bottom_row; ++row)
16730 row->enabled_p = 0;
16732 /* Point may have moved to a different line, so we cannot assume that
16733 the previous cursor position is valid; locate the correct row. */
16734 if (pt_row)
16736 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16737 row < bottom_row
16738 && PT >= MATRIX_ROW_END_CHARPOS (row)
16739 && !row->ends_at_zv_p;
16740 row++)
16742 w->cursor.vpos++;
16743 w->cursor.y = row->y;
16745 if (row < bottom_row)
16747 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16748 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16750 /* Can't use this optimization with bidi-reordered glyph
16751 rows, unless cursor is already at point. */
16752 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16754 if (!(w->cursor.hpos >= 0
16755 && w->cursor.hpos < row->used[TEXT_AREA]
16756 && BUFFERP (glyph->object)
16757 && glyph->charpos == PT))
16758 return 0;
16760 else
16761 for (; glyph < end
16762 && (!BUFFERP (glyph->object)
16763 || glyph->charpos < PT);
16764 glyph++)
16766 w->cursor.hpos++;
16767 w->cursor.x += glyph->pixel_width;
16772 /* Adjust window end. A null value of last_text_row means that
16773 the window end is in reused rows which in turn means that
16774 only its vpos can have changed. */
16775 if (last_text_row)
16777 w->window_end_bytepos
16778 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16779 wset_window_end_pos
16780 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16781 wset_window_end_vpos
16782 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16783 w->desired_matrix)));
16785 else
16787 wset_window_end_vpos
16788 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16791 wset_window_end_valid (w, Qnil);
16792 w->desired_matrix->no_scrolling_p = 1;
16794 #ifdef GLYPH_DEBUG
16795 debug_method_add (w, "try_window_reusing_current_matrix 2");
16796 #endif
16797 return 1;
16800 return 0;
16805 /************************************************************************
16806 Window redisplay reusing current matrix when buffer has changed
16807 ************************************************************************/
16809 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16810 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16811 ptrdiff_t *, ptrdiff_t *);
16812 static struct glyph_row *
16813 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16814 struct glyph_row *);
16817 /* Return the last row in MATRIX displaying text. If row START is
16818 non-null, start searching with that row. IT gives the dimensions
16819 of the display. Value is null if matrix is empty; otherwise it is
16820 a pointer to the row found. */
16822 static struct glyph_row *
16823 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16824 struct glyph_row *start)
16826 struct glyph_row *row, *row_found;
16828 /* Set row_found to the last row in IT->w's current matrix
16829 displaying text. The loop looks funny but think of partially
16830 visible lines. */
16831 row_found = NULL;
16832 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16833 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16835 eassert (row->enabled_p);
16836 row_found = row;
16837 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16838 break;
16839 ++row;
16842 return row_found;
16846 /* Return the last row in the current matrix of W that is not affected
16847 by changes at the start of current_buffer that occurred since W's
16848 current matrix was built. Value is null if no such row exists.
16850 BEG_UNCHANGED us the number of characters unchanged at the start of
16851 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16852 first changed character in current_buffer. Characters at positions <
16853 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16854 when the current matrix was built. */
16856 static struct glyph_row *
16857 find_last_unchanged_at_beg_row (struct window *w)
16859 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16860 struct glyph_row *row;
16861 struct glyph_row *row_found = NULL;
16862 int yb = window_text_bottom_y (w);
16864 /* Find the last row displaying unchanged text. */
16865 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16866 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16867 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16868 ++row)
16870 if (/* If row ends before first_changed_pos, it is unchanged,
16871 except in some case. */
16872 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16873 /* When row ends in ZV and we write at ZV it is not
16874 unchanged. */
16875 && !row->ends_at_zv_p
16876 /* When first_changed_pos is the end of a continued line,
16877 row is not unchanged because it may be no longer
16878 continued. */
16879 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16880 && (row->continued_p
16881 || row->exact_window_width_line_p))
16882 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16883 needs to be recomputed, so don't consider this row as
16884 unchanged. This happens when the last line was
16885 bidi-reordered and was killed immediately before this
16886 redisplay cycle. In that case, ROW->end stores the
16887 buffer position of the first visual-order character of
16888 the killed text, which is now beyond ZV. */
16889 && CHARPOS (row->end.pos) <= ZV)
16890 row_found = row;
16892 /* Stop if last visible row. */
16893 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16894 break;
16897 return row_found;
16901 /* Find the first glyph row in the current matrix of W that is not
16902 affected by changes at the end of current_buffer since the
16903 time W's current matrix was built.
16905 Return in *DELTA the number of chars by which buffer positions in
16906 unchanged text at the end of current_buffer must be adjusted.
16908 Return in *DELTA_BYTES the corresponding number of bytes.
16910 Value is null if no such row exists, i.e. all rows are affected by
16911 changes. */
16913 static struct glyph_row *
16914 find_first_unchanged_at_end_row (struct window *w,
16915 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16917 struct glyph_row *row;
16918 struct glyph_row *row_found = NULL;
16920 *delta = *delta_bytes = 0;
16922 /* Display must not have been paused, otherwise the current matrix
16923 is not up to date. */
16924 eassert (!NILP (w->window_end_valid));
16926 /* A value of window_end_pos >= END_UNCHANGED means that the window
16927 end is in the range of changed text. If so, there is no
16928 unchanged row at the end of W's current matrix. */
16929 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16930 return NULL;
16932 /* Set row to the last row in W's current matrix displaying text. */
16933 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16935 /* If matrix is entirely empty, no unchanged row exists. */
16936 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16938 /* The value of row is the last glyph row in the matrix having a
16939 meaningful buffer position in it. The end position of row
16940 corresponds to window_end_pos. This allows us to translate
16941 buffer positions in the current matrix to current buffer
16942 positions for characters not in changed text. */
16943 ptrdiff_t Z_old =
16944 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16945 ptrdiff_t Z_BYTE_old =
16946 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16947 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16948 struct glyph_row *first_text_row
16949 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16951 *delta = Z - Z_old;
16952 *delta_bytes = Z_BYTE - Z_BYTE_old;
16954 /* Set last_unchanged_pos to the buffer position of the last
16955 character in the buffer that has not been changed. Z is the
16956 index + 1 of the last character in current_buffer, i.e. by
16957 subtracting END_UNCHANGED we get the index of the last
16958 unchanged character, and we have to add BEG to get its buffer
16959 position. */
16960 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16961 last_unchanged_pos_old = last_unchanged_pos - *delta;
16963 /* Search backward from ROW for a row displaying a line that
16964 starts at a minimum position >= last_unchanged_pos_old. */
16965 for (; row > first_text_row; --row)
16967 /* This used to abort, but it can happen.
16968 It is ok to just stop the search instead here. KFS. */
16969 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16970 break;
16972 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16973 row_found = row;
16977 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16979 return row_found;
16983 /* Make sure that glyph rows in the current matrix of window W
16984 reference the same glyph memory as corresponding rows in the
16985 frame's frame matrix. This function is called after scrolling W's
16986 current matrix on a terminal frame in try_window_id and
16987 try_window_reusing_current_matrix. */
16989 static void
16990 sync_frame_with_window_matrix_rows (struct window *w)
16992 struct frame *f = XFRAME (w->frame);
16993 struct glyph_row *window_row, *window_row_end, *frame_row;
16995 /* Preconditions: W must be a leaf window and full-width. Its frame
16996 must have a frame matrix. */
16997 eassert (NILP (w->hchild) && NILP (w->vchild));
16998 eassert (WINDOW_FULL_WIDTH_P (w));
16999 eassert (!FRAME_WINDOW_P (f));
17001 /* If W is a full-width window, glyph pointers in W's current matrix
17002 have, by definition, to be the same as glyph pointers in the
17003 corresponding frame matrix. Note that frame matrices have no
17004 marginal areas (see build_frame_matrix). */
17005 window_row = w->current_matrix->rows;
17006 window_row_end = window_row + w->current_matrix->nrows;
17007 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17008 while (window_row < window_row_end)
17010 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17011 struct glyph *end = window_row->glyphs[LAST_AREA];
17013 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17014 frame_row->glyphs[TEXT_AREA] = start;
17015 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17016 frame_row->glyphs[LAST_AREA] = end;
17018 /* Disable frame rows whose corresponding window rows have
17019 been disabled in try_window_id. */
17020 if (!window_row->enabled_p)
17021 frame_row->enabled_p = 0;
17023 ++window_row, ++frame_row;
17028 /* Find the glyph row in window W containing CHARPOS. Consider all
17029 rows between START and END (not inclusive). END null means search
17030 all rows to the end of the display area of W. Value is the row
17031 containing CHARPOS or null. */
17033 struct glyph_row *
17034 row_containing_pos (struct window *w, ptrdiff_t charpos,
17035 struct glyph_row *start, struct glyph_row *end, int dy)
17037 struct glyph_row *row = start;
17038 struct glyph_row *best_row = NULL;
17039 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17040 int last_y;
17042 /* If we happen to start on a header-line, skip that. */
17043 if (row->mode_line_p)
17044 ++row;
17046 if ((end && row >= end) || !row->enabled_p)
17047 return NULL;
17049 last_y = window_text_bottom_y (w) - dy;
17051 while (1)
17053 /* Give up if we have gone too far. */
17054 if (end && row >= end)
17055 return NULL;
17056 /* This formerly returned if they were equal.
17057 I think that both quantities are of a "last plus one" type;
17058 if so, when they are equal, the row is within the screen. -- rms. */
17059 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17060 return NULL;
17062 /* If it is in this row, return this row. */
17063 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17064 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17065 /* The end position of a row equals the start
17066 position of the next row. If CHARPOS is there, we
17067 would rather display it in the next line, except
17068 when this line ends in ZV. */
17069 && !row->ends_at_zv_p
17070 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17071 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17073 struct glyph *g;
17075 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17076 || (!best_row && !row->continued_p))
17077 return row;
17078 /* In bidi-reordered rows, there could be several rows
17079 occluding point, all of them belonging to the same
17080 continued line. We need to find the row which fits
17081 CHARPOS the best. */
17082 for (g = row->glyphs[TEXT_AREA];
17083 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17084 g++)
17086 if (!STRINGP (g->object))
17088 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17090 mindif = eabs (g->charpos - charpos);
17091 best_row = row;
17092 /* Exact match always wins. */
17093 if (mindif == 0)
17094 return best_row;
17099 else if (best_row && !row->continued_p)
17100 return best_row;
17101 ++row;
17106 /* Try to redisplay window W by reusing its existing display. W's
17107 current matrix must be up to date when this function is called,
17108 i.e. window_end_valid must not be nil.
17110 Value is
17112 1 if display has been updated
17113 0 if otherwise unsuccessful
17114 -1 if redisplay with same window start is known not to succeed
17116 The following steps are performed:
17118 1. Find the last row in the current matrix of W that is not
17119 affected by changes at the start of current_buffer. If no such row
17120 is found, give up.
17122 2. Find the first row in W's current matrix that is not affected by
17123 changes at the end of current_buffer. Maybe there is no such row.
17125 3. Display lines beginning with the row + 1 found in step 1 to the
17126 row found in step 2 or, if step 2 didn't find a row, to the end of
17127 the window.
17129 4. If cursor is not known to appear on the window, give up.
17131 5. If display stopped at the row found in step 2, scroll the
17132 display and current matrix as needed.
17134 6. Maybe display some lines at the end of W, if we must. This can
17135 happen under various circumstances, like a partially visible line
17136 becoming fully visible, or because newly displayed lines are displayed
17137 in smaller font sizes.
17139 7. Update W's window end information. */
17141 static int
17142 try_window_id (struct window *w)
17144 struct frame *f = XFRAME (w->frame);
17145 struct glyph_matrix *current_matrix = w->current_matrix;
17146 struct glyph_matrix *desired_matrix = w->desired_matrix;
17147 struct glyph_row *last_unchanged_at_beg_row;
17148 struct glyph_row *first_unchanged_at_end_row;
17149 struct glyph_row *row;
17150 struct glyph_row *bottom_row;
17151 int bottom_vpos;
17152 struct it it;
17153 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17154 int dvpos, dy;
17155 struct text_pos start_pos;
17156 struct run run;
17157 int first_unchanged_at_end_vpos = 0;
17158 struct glyph_row *last_text_row, *last_text_row_at_end;
17159 struct text_pos start;
17160 ptrdiff_t first_changed_charpos, last_changed_charpos;
17162 #ifdef GLYPH_DEBUG
17163 if (inhibit_try_window_id)
17164 return 0;
17165 #endif
17167 /* This is handy for debugging. */
17168 #if 0
17169 #define GIVE_UP(X) \
17170 do { \
17171 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17172 return 0; \
17173 } while (0)
17174 #else
17175 #define GIVE_UP(X) return 0
17176 #endif
17178 SET_TEXT_POS_FROM_MARKER (start, w->start);
17180 /* Don't use this for mini-windows because these can show
17181 messages and mini-buffers, and we don't handle that here. */
17182 if (MINI_WINDOW_P (w))
17183 GIVE_UP (1);
17185 /* This flag is used to prevent redisplay optimizations. */
17186 if (windows_or_buffers_changed || cursor_type_changed)
17187 GIVE_UP (2);
17189 /* Verify that narrowing has not changed.
17190 Also verify that we were not told to prevent redisplay optimizations.
17191 It would be nice to further
17192 reduce the number of cases where this prevents try_window_id. */
17193 if (current_buffer->clip_changed
17194 || current_buffer->prevent_redisplay_optimizations_p)
17195 GIVE_UP (3);
17197 /* Window must either use window-based redisplay or be full width. */
17198 if (!FRAME_WINDOW_P (f)
17199 && (!FRAME_LINE_INS_DEL_OK (f)
17200 || !WINDOW_FULL_WIDTH_P (w)))
17201 GIVE_UP (4);
17203 /* Give up if point is known NOT to appear in W. */
17204 if (PT < CHARPOS (start))
17205 GIVE_UP (5);
17207 /* Another way to prevent redisplay optimizations. */
17208 if (w->last_modified == 0)
17209 GIVE_UP (6);
17211 /* Verify that window is not hscrolled. */
17212 if (w->hscroll != 0)
17213 GIVE_UP (7);
17215 /* Verify that display wasn't paused. */
17216 if (NILP (w->window_end_valid))
17217 GIVE_UP (8);
17219 /* Can't use this if highlighting a region because a cursor movement
17220 will do more than just set the cursor. */
17221 if (!NILP (Vtransient_mark_mode)
17222 && !NILP (BVAR (current_buffer, mark_active)))
17223 GIVE_UP (9);
17225 /* Likewise if highlighting trailing whitespace. */
17226 if (!NILP (Vshow_trailing_whitespace))
17227 GIVE_UP (11);
17229 /* Likewise if showing a region. */
17230 if (!NILP (w->region_showing))
17231 GIVE_UP (10);
17233 /* Can't use this if overlay arrow position and/or string have
17234 changed. */
17235 if (overlay_arrows_changed_p ())
17236 GIVE_UP (12);
17238 /* When word-wrap is on, adding a space to the first word of a
17239 wrapped line can change the wrap position, altering the line
17240 above it. It might be worthwhile to handle this more
17241 intelligently, but for now just redisplay from scratch. */
17242 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17243 GIVE_UP (21);
17245 /* Under bidi reordering, adding or deleting a character in the
17246 beginning of a paragraph, before the first strong directional
17247 character, can change the base direction of the paragraph (unless
17248 the buffer specifies a fixed paragraph direction), which will
17249 require to redisplay the whole paragraph. It might be worthwhile
17250 to find the paragraph limits and widen the range of redisplayed
17251 lines to that, but for now just give up this optimization and
17252 redisplay from scratch. */
17253 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17254 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17255 GIVE_UP (22);
17257 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17258 only if buffer has really changed. The reason is that the gap is
17259 initially at Z for freshly visited files. The code below would
17260 set end_unchanged to 0 in that case. */
17261 if (MODIFF > SAVE_MODIFF
17262 /* This seems to happen sometimes after saving a buffer. */
17263 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17265 if (GPT - BEG < BEG_UNCHANGED)
17266 BEG_UNCHANGED = GPT - BEG;
17267 if (Z - GPT < END_UNCHANGED)
17268 END_UNCHANGED = Z - GPT;
17271 /* The position of the first and last character that has been changed. */
17272 first_changed_charpos = BEG + BEG_UNCHANGED;
17273 last_changed_charpos = Z - END_UNCHANGED;
17275 /* If window starts after a line end, and the last change is in
17276 front of that newline, then changes don't affect the display.
17277 This case happens with stealth-fontification. Note that although
17278 the display is unchanged, glyph positions in the matrix have to
17279 be adjusted, of course. */
17280 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17281 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17282 && ((last_changed_charpos < CHARPOS (start)
17283 && CHARPOS (start) == BEGV)
17284 || (last_changed_charpos < CHARPOS (start) - 1
17285 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17287 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17288 struct glyph_row *r0;
17290 /* Compute how many chars/bytes have been added to or removed
17291 from the buffer. */
17292 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17293 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17294 Z_delta = Z - Z_old;
17295 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17297 /* Give up if PT is not in the window. Note that it already has
17298 been checked at the start of try_window_id that PT is not in
17299 front of the window start. */
17300 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17301 GIVE_UP (13);
17303 /* If window start is unchanged, we can reuse the whole matrix
17304 as is, after adjusting glyph positions. No need to compute
17305 the window end again, since its offset from Z hasn't changed. */
17306 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17307 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17308 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17309 /* PT must not be in a partially visible line. */
17310 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17311 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17313 /* Adjust positions in the glyph matrix. */
17314 if (Z_delta || Z_delta_bytes)
17316 struct glyph_row *r1
17317 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17318 increment_matrix_positions (w->current_matrix,
17319 MATRIX_ROW_VPOS (r0, current_matrix),
17320 MATRIX_ROW_VPOS (r1, current_matrix),
17321 Z_delta, Z_delta_bytes);
17324 /* Set the cursor. */
17325 row = row_containing_pos (w, PT, r0, NULL, 0);
17326 if (row)
17327 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17328 else
17329 emacs_abort ();
17330 return 1;
17334 /* Handle the case that changes are all below what is displayed in
17335 the window, and that PT is in the window. This shortcut cannot
17336 be taken if ZV is visible in the window, and text has been added
17337 there that is visible in the window. */
17338 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17339 /* ZV is not visible in the window, or there are no
17340 changes at ZV, actually. */
17341 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17342 || first_changed_charpos == last_changed_charpos))
17344 struct glyph_row *r0;
17346 /* Give up if PT is not in the window. Note that it already has
17347 been checked at the start of try_window_id that PT is not in
17348 front of the window start. */
17349 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17350 GIVE_UP (14);
17352 /* If window start is unchanged, we can reuse the whole matrix
17353 as is, without changing glyph positions since no text has
17354 been added/removed in front of the window end. */
17355 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17356 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17357 /* PT must not be in a partially visible line. */
17358 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17359 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17361 /* We have to compute the window end anew since text
17362 could have been added/removed after it. */
17363 wset_window_end_pos
17364 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17365 w->window_end_bytepos
17366 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17368 /* Set the cursor. */
17369 row = row_containing_pos (w, PT, r0, NULL, 0);
17370 if (row)
17371 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17372 else
17373 emacs_abort ();
17374 return 2;
17378 /* Give up if window start is in the changed area.
17380 The condition used to read
17382 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17384 but why that was tested escapes me at the moment. */
17385 if (CHARPOS (start) >= first_changed_charpos
17386 && CHARPOS (start) <= last_changed_charpos)
17387 GIVE_UP (15);
17389 /* Check that window start agrees with the start of the first glyph
17390 row in its current matrix. Check this after we know the window
17391 start is not in changed text, otherwise positions would not be
17392 comparable. */
17393 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17394 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17395 GIVE_UP (16);
17397 /* Give up if the window ends in strings. Overlay strings
17398 at the end are difficult to handle, so don't try. */
17399 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17400 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17401 GIVE_UP (20);
17403 /* Compute the position at which we have to start displaying new
17404 lines. Some of the lines at the top of the window might be
17405 reusable because they are not displaying changed text. Find the
17406 last row in W's current matrix not affected by changes at the
17407 start of current_buffer. Value is null if changes start in the
17408 first line of window. */
17409 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17410 if (last_unchanged_at_beg_row)
17412 /* Avoid starting to display in the middle of a character, a TAB
17413 for instance. This is easier than to set up the iterator
17414 exactly, and it's not a frequent case, so the additional
17415 effort wouldn't really pay off. */
17416 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17417 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17418 && last_unchanged_at_beg_row > w->current_matrix->rows)
17419 --last_unchanged_at_beg_row;
17421 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17422 GIVE_UP (17);
17424 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17425 GIVE_UP (18);
17426 start_pos = it.current.pos;
17428 /* Start displaying new lines in the desired matrix at the same
17429 vpos we would use in the current matrix, i.e. below
17430 last_unchanged_at_beg_row. */
17431 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17432 current_matrix);
17433 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17434 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17436 eassert (it.hpos == 0 && it.current_x == 0);
17438 else
17440 /* There are no reusable lines at the start of the window.
17441 Start displaying in the first text line. */
17442 start_display (&it, w, start);
17443 it.vpos = it.first_vpos;
17444 start_pos = it.current.pos;
17447 /* Find the first row that is not affected by changes at the end of
17448 the buffer. Value will be null if there is no unchanged row, in
17449 which case we must redisplay to the end of the window. delta
17450 will be set to the value by which buffer positions beginning with
17451 first_unchanged_at_end_row have to be adjusted due to text
17452 changes. */
17453 first_unchanged_at_end_row
17454 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17455 IF_DEBUG (debug_delta = delta);
17456 IF_DEBUG (debug_delta_bytes = delta_bytes);
17458 /* Set stop_pos to the buffer position up to which we will have to
17459 display new lines. If first_unchanged_at_end_row != NULL, this
17460 is the buffer position of the start of the line displayed in that
17461 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17462 that we don't stop at a buffer position. */
17463 stop_pos = 0;
17464 if (first_unchanged_at_end_row)
17466 eassert (last_unchanged_at_beg_row == NULL
17467 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17469 /* If this is a continuation line, move forward to the next one
17470 that isn't. Changes in lines above affect this line.
17471 Caution: this may move first_unchanged_at_end_row to a row
17472 not displaying text. */
17473 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17474 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17475 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17476 < it.last_visible_y))
17477 ++first_unchanged_at_end_row;
17479 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17480 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17481 >= it.last_visible_y))
17482 first_unchanged_at_end_row = NULL;
17483 else
17485 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17486 + delta);
17487 first_unchanged_at_end_vpos
17488 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17489 eassert (stop_pos >= Z - END_UNCHANGED);
17492 else if (last_unchanged_at_beg_row == NULL)
17493 GIVE_UP (19);
17496 #ifdef GLYPH_DEBUG
17498 /* Either there is no unchanged row at the end, or the one we have
17499 now displays text. This is a necessary condition for the window
17500 end pos calculation at the end of this function. */
17501 eassert (first_unchanged_at_end_row == NULL
17502 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17504 debug_last_unchanged_at_beg_vpos
17505 = (last_unchanged_at_beg_row
17506 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17507 : -1);
17508 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17510 #endif /* GLYPH_DEBUG */
17513 /* Display new lines. Set last_text_row to the last new line
17514 displayed which has text on it, i.e. might end up as being the
17515 line where the window_end_vpos is. */
17516 w->cursor.vpos = -1;
17517 last_text_row = NULL;
17518 overlay_arrow_seen = 0;
17519 while (it.current_y < it.last_visible_y
17520 && !fonts_changed_p
17521 && (first_unchanged_at_end_row == NULL
17522 || IT_CHARPOS (it) < stop_pos))
17524 if (display_line (&it))
17525 last_text_row = it.glyph_row - 1;
17528 if (fonts_changed_p)
17529 return -1;
17532 /* Compute differences in buffer positions, y-positions etc. for
17533 lines reused at the bottom of the window. Compute what we can
17534 scroll. */
17535 if (first_unchanged_at_end_row
17536 /* No lines reused because we displayed everything up to the
17537 bottom of the window. */
17538 && it.current_y < it.last_visible_y)
17540 dvpos = (it.vpos
17541 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17542 current_matrix));
17543 dy = it.current_y - first_unchanged_at_end_row->y;
17544 run.current_y = first_unchanged_at_end_row->y;
17545 run.desired_y = run.current_y + dy;
17546 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17548 else
17550 delta = delta_bytes = dvpos = dy
17551 = run.current_y = run.desired_y = run.height = 0;
17552 first_unchanged_at_end_row = NULL;
17554 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17557 /* Find the cursor if not already found. We have to decide whether
17558 PT will appear on this window (it sometimes doesn't, but this is
17559 not a very frequent case.) This decision has to be made before
17560 the current matrix is altered. A value of cursor.vpos < 0 means
17561 that PT is either in one of the lines beginning at
17562 first_unchanged_at_end_row or below the window. Don't care for
17563 lines that might be displayed later at the window end; as
17564 mentioned, this is not a frequent case. */
17565 if (w->cursor.vpos < 0)
17567 /* Cursor in unchanged rows at the top? */
17568 if (PT < CHARPOS (start_pos)
17569 && last_unchanged_at_beg_row)
17571 row = row_containing_pos (w, PT,
17572 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17573 last_unchanged_at_beg_row + 1, 0);
17574 if (row)
17575 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17578 /* Start from first_unchanged_at_end_row looking for PT. */
17579 else if (first_unchanged_at_end_row)
17581 row = row_containing_pos (w, PT - delta,
17582 first_unchanged_at_end_row, NULL, 0);
17583 if (row)
17584 set_cursor_from_row (w, row, w->current_matrix, delta,
17585 delta_bytes, dy, dvpos);
17588 /* Give up if cursor was not found. */
17589 if (w->cursor.vpos < 0)
17591 clear_glyph_matrix (w->desired_matrix);
17592 return -1;
17596 /* Don't let the cursor end in the scroll margins. */
17598 int this_scroll_margin, cursor_height;
17600 this_scroll_margin =
17601 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17602 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17603 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17605 if ((w->cursor.y < this_scroll_margin
17606 && CHARPOS (start) > BEGV)
17607 /* Old redisplay didn't take scroll margin into account at the bottom,
17608 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17609 || (w->cursor.y + (make_cursor_line_fully_visible_p
17610 ? cursor_height + this_scroll_margin
17611 : 1)) > it.last_visible_y)
17613 w->cursor.vpos = -1;
17614 clear_glyph_matrix (w->desired_matrix);
17615 return -1;
17619 /* Scroll the display. Do it before changing the current matrix so
17620 that xterm.c doesn't get confused about where the cursor glyph is
17621 found. */
17622 if (dy && run.height)
17624 update_begin (f);
17626 if (FRAME_WINDOW_P (f))
17628 FRAME_RIF (f)->update_window_begin_hook (w);
17629 FRAME_RIF (f)->clear_window_mouse_face (w);
17630 FRAME_RIF (f)->scroll_run_hook (w, &run);
17631 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17633 else
17635 /* Terminal frame. In this case, dvpos gives the number of
17636 lines to scroll by; dvpos < 0 means scroll up. */
17637 int from_vpos
17638 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17639 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17640 int end = (WINDOW_TOP_EDGE_LINE (w)
17641 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17642 + window_internal_height (w));
17644 #if defined (HAVE_GPM) || defined (MSDOS)
17645 x_clear_window_mouse_face (w);
17646 #endif
17647 /* Perform the operation on the screen. */
17648 if (dvpos > 0)
17650 /* Scroll last_unchanged_at_beg_row to the end of the
17651 window down dvpos lines. */
17652 set_terminal_window (f, end);
17654 /* On dumb terminals delete dvpos lines at the end
17655 before inserting dvpos empty lines. */
17656 if (!FRAME_SCROLL_REGION_OK (f))
17657 ins_del_lines (f, end - dvpos, -dvpos);
17659 /* Insert dvpos empty lines in front of
17660 last_unchanged_at_beg_row. */
17661 ins_del_lines (f, from, dvpos);
17663 else if (dvpos < 0)
17665 /* Scroll up last_unchanged_at_beg_vpos to the end of
17666 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17667 set_terminal_window (f, end);
17669 /* Delete dvpos lines in front of
17670 last_unchanged_at_beg_vpos. ins_del_lines will set
17671 the cursor to the given vpos and emit |dvpos| delete
17672 line sequences. */
17673 ins_del_lines (f, from + dvpos, dvpos);
17675 /* On a dumb terminal insert dvpos empty lines at the
17676 end. */
17677 if (!FRAME_SCROLL_REGION_OK (f))
17678 ins_del_lines (f, end + dvpos, -dvpos);
17681 set_terminal_window (f, 0);
17684 update_end (f);
17687 /* Shift reused rows of the current matrix to the right position.
17688 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17689 text. */
17690 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17691 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17692 if (dvpos < 0)
17694 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17695 bottom_vpos, dvpos);
17696 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17697 bottom_vpos);
17699 else if (dvpos > 0)
17701 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17702 bottom_vpos, dvpos);
17703 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17704 first_unchanged_at_end_vpos + dvpos);
17707 /* For frame-based redisplay, make sure that current frame and window
17708 matrix are in sync with respect to glyph memory. */
17709 if (!FRAME_WINDOW_P (f))
17710 sync_frame_with_window_matrix_rows (w);
17712 /* Adjust buffer positions in reused rows. */
17713 if (delta || delta_bytes)
17714 increment_matrix_positions (current_matrix,
17715 first_unchanged_at_end_vpos + dvpos,
17716 bottom_vpos, delta, delta_bytes);
17718 /* Adjust Y positions. */
17719 if (dy)
17720 shift_glyph_matrix (w, current_matrix,
17721 first_unchanged_at_end_vpos + dvpos,
17722 bottom_vpos, dy);
17724 if (first_unchanged_at_end_row)
17726 first_unchanged_at_end_row += dvpos;
17727 if (first_unchanged_at_end_row->y >= it.last_visible_y
17728 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17729 first_unchanged_at_end_row = NULL;
17732 /* If scrolling up, there may be some lines to display at the end of
17733 the window. */
17734 last_text_row_at_end = NULL;
17735 if (dy < 0)
17737 /* Scrolling up can leave for example a partially visible line
17738 at the end of the window to be redisplayed. */
17739 /* Set last_row to the glyph row in the current matrix where the
17740 window end line is found. It has been moved up or down in
17741 the matrix by dvpos. */
17742 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17743 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17745 /* If last_row is the window end line, it should display text. */
17746 eassert (last_row->displays_text_p);
17748 /* If window end line was partially visible before, begin
17749 displaying at that line. Otherwise begin displaying with the
17750 line following it. */
17751 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17753 init_to_row_start (&it, w, last_row);
17754 it.vpos = last_vpos;
17755 it.current_y = last_row->y;
17757 else
17759 init_to_row_end (&it, w, last_row);
17760 it.vpos = 1 + last_vpos;
17761 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17762 ++last_row;
17765 /* We may start in a continuation line. If so, we have to
17766 get the right continuation_lines_width and current_x. */
17767 it.continuation_lines_width = last_row->continuation_lines_width;
17768 it.hpos = it.current_x = 0;
17770 /* Display the rest of the lines at the window end. */
17771 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17772 while (it.current_y < it.last_visible_y
17773 && !fonts_changed_p)
17775 /* Is it always sure that the display agrees with lines in
17776 the current matrix? I don't think so, so we mark rows
17777 displayed invalid in the current matrix by setting their
17778 enabled_p flag to zero. */
17779 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17780 if (display_line (&it))
17781 last_text_row_at_end = it.glyph_row - 1;
17785 /* Update window_end_pos and window_end_vpos. */
17786 if (first_unchanged_at_end_row
17787 && !last_text_row_at_end)
17789 /* Window end line if one of the preserved rows from the current
17790 matrix. Set row to the last row displaying text in current
17791 matrix starting at first_unchanged_at_end_row, after
17792 scrolling. */
17793 eassert (first_unchanged_at_end_row->displays_text_p);
17794 row = find_last_row_displaying_text (w->current_matrix, &it,
17795 first_unchanged_at_end_row);
17796 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17798 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17799 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17800 wset_window_end_vpos
17801 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17802 eassert (w->window_end_bytepos >= 0);
17803 IF_DEBUG (debug_method_add (w, "A"));
17805 else if (last_text_row_at_end)
17807 wset_window_end_pos
17808 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17809 w->window_end_bytepos
17810 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17811 wset_window_end_vpos
17812 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17813 desired_matrix)));
17814 eassert (w->window_end_bytepos >= 0);
17815 IF_DEBUG (debug_method_add (w, "B"));
17817 else if (last_text_row)
17819 /* We have displayed either to the end of the window or at the
17820 end of the window, i.e. the last row with text is to be found
17821 in the desired matrix. */
17822 wset_window_end_pos
17823 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17824 w->window_end_bytepos
17825 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17826 wset_window_end_vpos
17827 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17828 eassert (w->window_end_bytepos >= 0);
17830 else if (first_unchanged_at_end_row == NULL
17831 && last_text_row == NULL
17832 && last_text_row_at_end == NULL)
17834 /* Displayed to end of window, but no line containing text was
17835 displayed. Lines were deleted at the end of the window. */
17836 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17837 int vpos = XFASTINT (w->window_end_vpos);
17838 struct glyph_row *current_row = current_matrix->rows + vpos;
17839 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17841 for (row = NULL;
17842 row == NULL && vpos >= first_vpos;
17843 --vpos, --current_row, --desired_row)
17845 if (desired_row->enabled_p)
17847 if (desired_row->displays_text_p)
17848 row = desired_row;
17850 else if (current_row->displays_text_p)
17851 row = current_row;
17854 eassert (row != NULL);
17855 wset_window_end_vpos (w, make_number (vpos + 1));
17856 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17857 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17858 eassert (w->window_end_bytepos >= 0);
17859 IF_DEBUG (debug_method_add (w, "C"));
17861 else
17862 emacs_abort ();
17864 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17865 debug_end_vpos = XFASTINT (w->window_end_vpos));
17867 /* Record that display has not been completed. */
17868 wset_window_end_valid (w, Qnil);
17869 w->desired_matrix->no_scrolling_p = 1;
17870 return 3;
17872 #undef GIVE_UP
17877 /***********************************************************************
17878 More debugging support
17879 ***********************************************************************/
17881 #ifdef GLYPH_DEBUG
17883 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17884 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17885 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17888 /* Dump the contents of glyph matrix MATRIX on stderr.
17890 GLYPHS 0 means don't show glyph contents.
17891 GLYPHS 1 means show glyphs in short form
17892 GLYPHS > 1 means show glyphs in long form. */
17894 void
17895 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17897 int i;
17898 for (i = 0; i < matrix->nrows; ++i)
17899 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17903 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17904 the glyph row and area where the glyph comes from. */
17906 void
17907 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17909 if (glyph->type == CHAR_GLYPH)
17911 fprintf (stderr,
17912 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17913 glyph - row->glyphs[TEXT_AREA],
17914 'C',
17915 glyph->charpos,
17916 (BUFFERP (glyph->object)
17917 ? 'B'
17918 : (STRINGP (glyph->object)
17919 ? 'S'
17920 : '-')),
17921 glyph->pixel_width,
17922 glyph->u.ch,
17923 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17924 ? glyph->u.ch
17925 : '.'),
17926 glyph->face_id,
17927 glyph->left_box_line_p,
17928 glyph->right_box_line_p);
17930 else if (glyph->type == STRETCH_GLYPH)
17932 fprintf (stderr,
17933 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17934 glyph - row->glyphs[TEXT_AREA],
17935 'S',
17936 glyph->charpos,
17937 (BUFFERP (glyph->object)
17938 ? 'B'
17939 : (STRINGP (glyph->object)
17940 ? 'S'
17941 : '-')),
17942 glyph->pixel_width,
17944 '.',
17945 glyph->face_id,
17946 glyph->left_box_line_p,
17947 glyph->right_box_line_p);
17949 else if (glyph->type == IMAGE_GLYPH)
17951 fprintf (stderr,
17952 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17953 glyph - row->glyphs[TEXT_AREA],
17954 'I',
17955 glyph->charpos,
17956 (BUFFERP (glyph->object)
17957 ? 'B'
17958 : (STRINGP (glyph->object)
17959 ? 'S'
17960 : '-')),
17961 glyph->pixel_width,
17962 glyph->u.img_id,
17963 '.',
17964 glyph->face_id,
17965 glyph->left_box_line_p,
17966 glyph->right_box_line_p);
17968 else if (glyph->type == COMPOSITE_GLYPH)
17970 fprintf (stderr,
17971 " %5td %4c %6"pI"d %c %3d 0x%05x",
17972 glyph - row->glyphs[TEXT_AREA],
17973 '+',
17974 glyph->charpos,
17975 (BUFFERP (glyph->object)
17976 ? 'B'
17977 : (STRINGP (glyph->object)
17978 ? 'S'
17979 : '-')),
17980 glyph->pixel_width,
17981 glyph->u.cmp.id);
17982 if (glyph->u.cmp.automatic)
17983 fprintf (stderr,
17984 "[%d-%d]",
17985 glyph->slice.cmp.from, glyph->slice.cmp.to);
17986 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17987 glyph->face_id,
17988 glyph->left_box_line_p,
17989 glyph->right_box_line_p);
17994 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17995 GLYPHS 0 means don't show glyph contents.
17996 GLYPHS 1 means show glyphs in short form
17997 GLYPHS > 1 means show glyphs in long form. */
17999 void
18000 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18002 if (glyphs != 1)
18004 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18005 fprintf (stderr, "======================================================================\n");
18007 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18008 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18009 vpos,
18010 MATRIX_ROW_START_CHARPOS (row),
18011 MATRIX_ROW_END_CHARPOS (row),
18012 row->used[TEXT_AREA],
18013 row->contains_overlapping_glyphs_p,
18014 row->enabled_p,
18015 row->truncated_on_left_p,
18016 row->truncated_on_right_p,
18017 row->continued_p,
18018 MATRIX_ROW_CONTINUATION_LINE_P (row),
18019 row->displays_text_p,
18020 row->ends_at_zv_p,
18021 row->fill_line_p,
18022 row->ends_in_middle_of_char_p,
18023 row->starts_in_middle_of_char_p,
18024 row->mouse_face_p,
18025 row->x,
18026 row->y,
18027 row->pixel_width,
18028 row->height,
18029 row->visible_height,
18030 row->ascent,
18031 row->phys_ascent);
18032 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
18033 row->end.overlay_string_index,
18034 row->continuation_lines_width);
18035 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18036 CHARPOS (row->start.string_pos),
18037 CHARPOS (row->end.string_pos));
18038 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18039 row->end.dpvec_index);
18042 if (glyphs > 1)
18044 int area;
18046 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18048 struct glyph *glyph = row->glyphs[area];
18049 struct glyph *glyph_end = glyph + row->used[area];
18051 /* Glyph for a line end in text. */
18052 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18053 ++glyph_end;
18055 if (glyph < glyph_end)
18056 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18058 for (; glyph < glyph_end; ++glyph)
18059 dump_glyph (row, glyph, area);
18062 else if (glyphs == 1)
18064 int area;
18066 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18068 char *s = alloca (row->used[area] + 1);
18069 int i;
18071 for (i = 0; i < row->used[area]; ++i)
18073 struct glyph *glyph = row->glyphs[area] + i;
18074 if (glyph->type == CHAR_GLYPH
18075 && glyph->u.ch < 0x80
18076 && glyph->u.ch >= ' ')
18077 s[i] = glyph->u.ch;
18078 else
18079 s[i] = '.';
18082 s[i] = '\0';
18083 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18089 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18090 Sdump_glyph_matrix, 0, 1, "p",
18091 doc: /* Dump the current matrix of the selected window to stderr.
18092 Shows contents of glyph row structures. With non-nil
18093 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18094 glyphs in short form, otherwise show glyphs in long form. */)
18095 (Lisp_Object glyphs)
18097 struct window *w = XWINDOW (selected_window);
18098 struct buffer *buffer = XBUFFER (w->buffer);
18100 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18101 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18102 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18103 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18104 fprintf (stderr, "=============================================\n");
18105 dump_glyph_matrix (w->current_matrix,
18106 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18107 return Qnil;
18111 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18112 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18113 (void)
18115 struct frame *f = XFRAME (selected_frame);
18116 dump_glyph_matrix (f->current_matrix, 1);
18117 return Qnil;
18121 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18122 doc: /* Dump glyph row ROW to stderr.
18123 GLYPH 0 means don't dump glyphs.
18124 GLYPH 1 means dump glyphs in short form.
18125 GLYPH > 1 or omitted means dump glyphs in long form. */)
18126 (Lisp_Object row, Lisp_Object glyphs)
18128 struct glyph_matrix *matrix;
18129 EMACS_INT vpos;
18131 CHECK_NUMBER (row);
18132 matrix = XWINDOW (selected_window)->current_matrix;
18133 vpos = XINT (row);
18134 if (vpos >= 0 && vpos < matrix->nrows)
18135 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18136 vpos,
18137 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18138 return Qnil;
18142 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18143 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18144 GLYPH 0 means don't dump glyphs.
18145 GLYPH 1 means dump glyphs in short form.
18146 GLYPH > 1 or omitted means dump glyphs in long form. */)
18147 (Lisp_Object row, Lisp_Object glyphs)
18149 struct frame *sf = SELECTED_FRAME ();
18150 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18151 EMACS_INT vpos;
18153 CHECK_NUMBER (row);
18154 vpos = XINT (row);
18155 if (vpos >= 0 && vpos < m->nrows)
18156 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18157 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18158 return Qnil;
18162 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18163 doc: /* Toggle tracing of redisplay.
18164 With ARG, turn tracing on if and only if ARG is positive. */)
18165 (Lisp_Object arg)
18167 if (NILP (arg))
18168 trace_redisplay_p = !trace_redisplay_p;
18169 else
18171 arg = Fprefix_numeric_value (arg);
18172 trace_redisplay_p = XINT (arg) > 0;
18175 return Qnil;
18179 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18180 doc: /* Like `format', but print result to stderr.
18181 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18182 (ptrdiff_t nargs, Lisp_Object *args)
18184 Lisp_Object s = Fformat (nargs, args);
18185 fprintf (stderr, "%s", SDATA (s));
18186 return Qnil;
18189 #endif /* GLYPH_DEBUG */
18193 /***********************************************************************
18194 Building Desired Matrix Rows
18195 ***********************************************************************/
18197 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18198 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18200 static struct glyph_row *
18201 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18203 struct frame *f = XFRAME (WINDOW_FRAME (w));
18204 struct buffer *buffer = XBUFFER (w->buffer);
18205 struct buffer *old = current_buffer;
18206 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18207 int arrow_len = SCHARS (overlay_arrow_string);
18208 const unsigned char *arrow_end = arrow_string + arrow_len;
18209 const unsigned char *p;
18210 struct it it;
18211 int multibyte_p;
18212 int n_glyphs_before;
18214 set_buffer_temp (buffer);
18215 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18216 it.glyph_row->used[TEXT_AREA] = 0;
18217 SET_TEXT_POS (it.position, 0, 0);
18219 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18220 p = arrow_string;
18221 while (p < arrow_end)
18223 Lisp_Object face, ilisp;
18225 /* Get the next character. */
18226 if (multibyte_p)
18227 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18228 else
18230 it.c = it.char_to_display = *p, it.len = 1;
18231 if (! ASCII_CHAR_P (it.c))
18232 it.char_to_display = BYTE8_TO_CHAR (it.c);
18234 p += it.len;
18236 /* Get its face. */
18237 ilisp = make_number (p - arrow_string);
18238 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18239 it.face_id = compute_char_face (f, it.char_to_display, face);
18241 /* Compute its width, get its glyphs. */
18242 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18243 SET_TEXT_POS (it.position, -1, -1);
18244 PRODUCE_GLYPHS (&it);
18246 /* If this character doesn't fit any more in the line, we have
18247 to remove some glyphs. */
18248 if (it.current_x > it.last_visible_x)
18250 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18251 break;
18255 set_buffer_temp (old);
18256 return it.glyph_row;
18260 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18261 glyphs to insert is determined by produce_special_glyphs. */
18263 static void
18264 insert_left_trunc_glyphs (struct it *it)
18266 struct it truncate_it;
18267 struct glyph *from, *end, *to, *toend;
18269 eassert (!FRAME_WINDOW_P (it->f)
18270 || (!it->glyph_row->reversed_p
18271 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18272 || (it->glyph_row->reversed_p
18273 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18275 /* Get the truncation glyphs. */
18276 truncate_it = *it;
18277 truncate_it.current_x = 0;
18278 truncate_it.face_id = DEFAULT_FACE_ID;
18279 truncate_it.glyph_row = &scratch_glyph_row;
18280 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18281 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18282 truncate_it.object = make_number (0);
18283 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18285 /* Overwrite glyphs from IT with truncation glyphs. */
18286 if (!it->glyph_row->reversed_p)
18288 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18290 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18291 end = from + tused;
18292 to = it->glyph_row->glyphs[TEXT_AREA];
18293 toend = to + it->glyph_row->used[TEXT_AREA];
18294 if (FRAME_WINDOW_P (it->f))
18296 /* On GUI frames, when variable-size fonts are displayed,
18297 the truncation glyphs may need more pixels than the row's
18298 glyphs they overwrite. We overwrite more glyphs to free
18299 enough screen real estate, and enlarge the stretch glyph
18300 on the right (see display_line), if there is one, to
18301 preserve the screen position of the truncation glyphs on
18302 the right. */
18303 int w = 0;
18304 struct glyph *g = to;
18305 short used;
18307 /* The first glyph could be partially visible, in which case
18308 it->glyph_row->x will be negative. But we want the left
18309 truncation glyphs to be aligned at the left margin of the
18310 window, so we override the x coordinate at which the row
18311 will begin. */
18312 it->glyph_row->x = 0;
18313 while (g < toend && w < it->truncation_pixel_width)
18315 w += g->pixel_width;
18316 ++g;
18318 if (g - to - tused > 0)
18320 memmove (to + tused, g, (toend - g) * sizeof(*g));
18321 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18323 used = it->glyph_row->used[TEXT_AREA];
18324 if (it->glyph_row->truncated_on_right_p
18325 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18326 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18327 == STRETCH_GLYPH)
18329 int extra = w - it->truncation_pixel_width;
18331 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18335 while (from < end)
18336 *to++ = *from++;
18338 /* There may be padding glyphs left over. Overwrite them too. */
18339 if (!FRAME_WINDOW_P (it->f))
18341 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18343 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18344 while (from < end)
18345 *to++ = *from++;
18349 if (to > toend)
18350 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18352 else
18354 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18356 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18357 that back to front. */
18358 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18359 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18360 toend = it->glyph_row->glyphs[TEXT_AREA];
18361 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18362 if (FRAME_WINDOW_P (it->f))
18364 int w = 0;
18365 struct glyph *g = to;
18367 while (g >= toend && w < it->truncation_pixel_width)
18369 w += g->pixel_width;
18370 --g;
18372 if (to - g - tused > 0)
18373 to = g + tused;
18374 if (it->glyph_row->truncated_on_right_p
18375 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18376 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18378 int extra = w - it->truncation_pixel_width;
18380 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18384 while (from >= end && to >= toend)
18385 *to-- = *from--;
18386 if (!FRAME_WINDOW_P (it->f))
18388 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18390 from =
18391 truncate_it.glyph_row->glyphs[TEXT_AREA]
18392 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18393 while (from >= end && to >= toend)
18394 *to-- = *from--;
18397 if (from >= end)
18399 /* Need to free some room before prepending additional
18400 glyphs. */
18401 int move_by = from - end + 1;
18402 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18403 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18405 for ( ; g >= g0; g--)
18406 g[move_by] = *g;
18407 while (from >= end)
18408 *to-- = *from--;
18409 it->glyph_row->used[TEXT_AREA] += move_by;
18414 /* Compute the hash code for ROW. */
18415 unsigned
18416 row_hash (struct glyph_row *row)
18418 int area, k;
18419 unsigned hashval = 0;
18421 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18422 for (k = 0; k < row->used[area]; ++k)
18423 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18424 + row->glyphs[area][k].u.val
18425 + row->glyphs[area][k].face_id
18426 + row->glyphs[area][k].padding_p
18427 + (row->glyphs[area][k].type << 2));
18429 return hashval;
18432 /* Compute the pixel height and width of IT->glyph_row.
18434 Most of the time, ascent and height of a display line will be equal
18435 to the max_ascent and max_height values of the display iterator
18436 structure. This is not the case if
18438 1. We hit ZV without displaying anything. In this case, max_ascent
18439 and max_height will be zero.
18441 2. We have some glyphs that don't contribute to the line height.
18442 (The glyph row flag contributes_to_line_height_p is for future
18443 pixmap extensions).
18445 The first case is easily covered by using default values because in
18446 these cases, the line height does not really matter, except that it
18447 must not be zero. */
18449 static void
18450 compute_line_metrics (struct it *it)
18452 struct glyph_row *row = it->glyph_row;
18454 if (FRAME_WINDOW_P (it->f))
18456 int i, min_y, max_y;
18458 /* The line may consist of one space only, that was added to
18459 place the cursor on it. If so, the row's height hasn't been
18460 computed yet. */
18461 if (row->height == 0)
18463 if (it->max_ascent + it->max_descent == 0)
18464 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18465 row->ascent = it->max_ascent;
18466 row->height = it->max_ascent + it->max_descent;
18467 row->phys_ascent = it->max_phys_ascent;
18468 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18469 row->extra_line_spacing = it->max_extra_line_spacing;
18472 /* Compute the width of this line. */
18473 row->pixel_width = row->x;
18474 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18475 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18477 eassert (row->pixel_width >= 0);
18478 eassert (row->ascent >= 0 && row->height > 0);
18480 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18481 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18483 /* If first line's physical ascent is larger than its logical
18484 ascent, use the physical ascent, and make the row taller.
18485 This makes accented characters fully visible. */
18486 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18487 && row->phys_ascent > row->ascent)
18489 row->height += row->phys_ascent - row->ascent;
18490 row->ascent = row->phys_ascent;
18493 /* Compute how much of the line is visible. */
18494 row->visible_height = row->height;
18496 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18497 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18499 if (row->y < min_y)
18500 row->visible_height -= min_y - row->y;
18501 if (row->y + row->height > max_y)
18502 row->visible_height -= row->y + row->height - max_y;
18504 else
18506 row->pixel_width = row->used[TEXT_AREA];
18507 if (row->continued_p)
18508 row->pixel_width -= it->continuation_pixel_width;
18509 else if (row->truncated_on_right_p)
18510 row->pixel_width -= it->truncation_pixel_width;
18511 row->ascent = row->phys_ascent = 0;
18512 row->height = row->phys_height = row->visible_height = 1;
18513 row->extra_line_spacing = 0;
18516 /* Compute a hash code for this row. */
18517 row->hash = row_hash (row);
18519 it->max_ascent = it->max_descent = 0;
18520 it->max_phys_ascent = it->max_phys_descent = 0;
18524 /* Append one space to the glyph row of iterator IT if doing a
18525 window-based redisplay. The space has the same face as
18526 IT->face_id. Value is non-zero if a space was added.
18528 This function is called to make sure that there is always one glyph
18529 at the end of a glyph row that the cursor can be set on under
18530 window-systems. (If there weren't such a glyph we would not know
18531 how wide and tall a box cursor should be displayed).
18533 At the same time this space let's a nicely handle clearing to the
18534 end of the line if the row ends in italic text. */
18536 static int
18537 append_space_for_newline (struct it *it, int default_face_p)
18539 if (FRAME_WINDOW_P (it->f))
18541 int n = it->glyph_row->used[TEXT_AREA];
18543 if (it->glyph_row->glyphs[TEXT_AREA] + n
18544 < it->glyph_row->glyphs[1 + TEXT_AREA])
18546 /* Save some values that must not be changed.
18547 Must save IT->c and IT->len because otherwise
18548 ITERATOR_AT_END_P wouldn't work anymore after
18549 append_space_for_newline has been called. */
18550 enum display_element_type saved_what = it->what;
18551 int saved_c = it->c, saved_len = it->len;
18552 int saved_char_to_display = it->char_to_display;
18553 int saved_x = it->current_x;
18554 int saved_face_id = it->face_id;
18555 struct text_pos saved_pos;
18556 Lisp_Object saved_object;
18557 struct face *face;
18559 saved_object = it->object;
18560 saved_pos = it->position;
18562 it->what = IT_CHARACTER;
18563 memset (&it->position, 0, sizeof it->position);
18564 it->object = make_number (0);
18565 it->c = it->char_to_display = ' ';
18566 it->len = 1;
18568 /* If the default face was remapped, be sure to use the
18569 remapped face for the appended newline. */
18570 if (default_face_p)
18571 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18572 else if (it->face_before_selective_p)
18573 it->face_id = it->saved_face_id;
18574 face = FACE_FROM_ID (it->f, it->face_id);
18575 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18577 PRODUCE_GLYPHS (it);
18579 it->override_ascent = -1;
18580 it->constrain_row_ascent_descent_p = 0;
18581 it->current_x = saved_x;
18582 it->object = saved_object;
18583 it->position = saved_pos;
18584 it->what = saved_what;
18585 it->face_id = saved_face_id;
18586 it->len = saved_len;
18587 it->c = saved_c;
18588 it->char_to_display = saved_char_to_display;
18589 return 1;
18593 return 0;
18597 /* Extend the face of the last glyph in the text area of IT->glyph_row
18598 to the end of the display line. Called from display_line. If the
18599 glyph row is empty, add a space glyph to it so that we know the
18600 face to draw. Set the glyph row flag fill_line_p. If the glyph
18601 row is R2L, prepend a stretch glyph to cover the empty space to the
18602 left of the leftmost glyph. */
18604 static void
18605 extend_face_to_end_of_line (struct it *it)
18607 struct face *face, *default_face;
18608 struct frame *f = it->f;
18610 /* If line is already filled, do nothing. Non window-system frames
18611 get a grace of one more ``pixel'' because their characters are
18612 1-``pixel'' wide, so they hit the equality too early. This grace
18613 is needed only for R2L rows that are not continued, to produce
18614 one extra blank where we could display the cursor. */
18615 if (it->current_x >= it->last_visible_x
18616 + (!FRAME_WINDOW_P (f)
18617 && it->glyph_row->reversed_p
18618 && !it->glyph_row->continued_p))
18619 return;
18621 /* The default face, possibly remapped. */
18622 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18624 /* Face extension extends the background and box of IT->face_id
18625 to the end of the line. If the background equals the background
18626 of the frame, we don't have to do anything. */
18627 if (it->face_before_selective_p)
18628 face = FACE_FROM_ID (f, it->saved_face_id);
18629 else
18630 face = FACE_FROM_ID (f, it->face_id);
18632 if (FRAME_WINDOW_P (f)
18633 && it->glyph_row->displays_text_p
18634 && face->box == FACE_NO_BOX
18635 && face->background == FRAME_BACKGROUND_PIXEL (f)
18636 && !face->stipple
18637 && !it->glyph_row->reversed_p)
18638 return;
18640 /* Set the glyph row flag indicating that the face of the last glyph
18641 in the text area has to be drawn to the end of the text area. */
18642 it->glyph_row->fill_line_p = 1;
18644 /* If current character of IT is not ASCII, make sure we have the
18645 ASCII face. This will be automatically undone the next time
18646 get_next_display_element returns a multibyte character. Note
18647 that the character will always be single byte in unibyte
18648 text. */
18649 if (!ASCII_CHAR_P (it->c))
18651 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18654 if (FRAME_WINDOW_P (f))
18656 /* If the row is empty, add a space with the current face of IT,
18657 so that we know which face to draw. */
18658 if (it->glyph_row->used[TEXT_AREA] == 0)
18660 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18661 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18662 it->glyph_row->used[TEXT_AREA] = 1;
18664 #ifdef HAVE_WINDOW_SYSTEM
18665 if (it->glyph_row->reversed_p)
18667 /* Prepend a stretch glyph to the row, such that the
18668 rightmost glyph will be drawn flushed all the way to the
18669 right margin of the window. The stretch glyph that will
18670 occupy the empty space, if any, to the left of the
18671 glyphs. */
18672 struct font *font = face->font ? face->font : FRAME_FONT (f);
18673 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18674 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18675 struct glyph *g;
18676 int row_width, stretch_ascent, stretch_width;
18677 struct text_pos saved_pos;
18678 int saved_face_id, saved_avoid_cursor;
18680 for (row_width = 0, g = row_start; g < row_end; g++)
18681 row_width += g->pixel_width;
18682 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18683 if (stretch_width > 0)
18685 stretch_ascent =
18686 (((it->ascent + it->descent)
18687 * FONT_BASE (font)) / FONT_HEIGHT (font));
18688 saved_pos = it->position;
18689 memset (&it->position, 0, sizeof it->position);
18690 saved_avoid_cursor = it->avoid_cursor_p;
18691 it->avoid_cursor_p = 1;
18692 saved_face_id = it->face_id;
18693 /* The last row's stretch glyph should get the default
18694 face, to avoid painting the rest of the window with
18695 the region face, if the region ends at ZV. */
18696 if (it->glyph_row->ends_at_zv_p)
18697 it->face_id = default_face->id;
18698 else
18699 it->face_id = face->id;
18700 append_stretch_glyph (it, make_number (0), stretch_width,
18701 it->ascent + it->descent, stretch_ascent);
18702 it->position = saved_pos;
18703 it->avoid_cursor_p = saved_avoid_cursor;
18704 it->face_id = saved_face_id;
18707 #endif /* HAVE_WINDOW_SYSTEM */
18709 else
18711 /* Save some values that must not be changed. */
18712 int saved_x = it->current_x;
18713 struct text_pos saved_pos;
18714 Lisp_Object saved_object;
18715 enum display_element_type saved_what = it->what;
18716 int saved_face_id = it->face_id;
18718 saved_object = it->object;
18719 saved_pos = it->position;
18721 it->what = IT_CHARACTER;
18722 memset (&it->position, 0, sizeof it->position);
18723 it->object = make_number (0);
18724 it->c = it->char_to_display = ' ';
18725 it->len = 1;
18726 /* The last row's blank glyphs should get the default face, to
18727 avoid painting the rest of the window with the region face,
18728 if the region ends at ZV. */
18729 if (it->glyph_row->ends_at_zv_p)
18730 it->face_id = default_face->id;
18731 else
18732 it->face_id = face->id;
18734 PRODUCE_GLYPHS (it);
18736 while (it->current_x <= it->last_visible_x)
18737 PRODUCE_GLYPHS (it);
18739 /* Don't count these blanks really. It would let us insert a left
18740 truncation glyph below and make us set the cursor on them, maybe. */
18741 it->current_x = saved_x;
18742 it->object = saved_object;
18743 it->position = saved_pos;
18744 it->what = saved_what;
18745 it->face_id = saved_face_id;
18750 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18751 trailing whitespace. */
18753 static int
18754 trailing_whitespace_p (ptrdiff_t charpos)
18756 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18757 int c = 0;
18759 while (bytepos < ZV_BYTE
18760 && (c = FETCH_CHAR (bytepos),
18761 c == ' ' || c == '\t'))
18762 ++bytepos;
18764 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18766 if (bytepos != PT_BYTE)
18767 return 1;
18769 return 0;
18773 /* Highlight trailing whitespace, if any, in ROW. */
18775 static void
18776 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18778 int used = row->used[TEXT_AREA];
18780 if (used)
18782 struct glyph *start = row->glyphs[TEXT_AREA];
18783 struct glyph *glyph = start + used - 1;
18785 if (row->reversed_p)
18787 /* Right-to-left rows need to be processed in the opposite
18788 direction, so swap the edge pointers. */
18789 glyph = start;
18790 start = row->glyphs[TEXT_AREA] + used - 1;
18793 /* Skip over glyphs inserted to display the cursor at the
18794 end of a line, for extending the face of the last glyph
18795 to the end of the line on terminals, and for truncation
18796 and continuation glyphs. */
18797 if (!row->reversed_p)
18799 while (glyph >= start
18800 && glyph->type == CHAR_GLYPH
18801 && INTEGERP (glyph->object))
18802 --glyph;
18804 else
18806 while (glyph <= start
18807 && glyph->type == CHAR_GLYPH
18808 && INTEGERP (glyph->object))
18809 ++glyph;
18812 /* If last glyph is a space or stretch, and it's trailing
18813 whitespace, set the face of all trailing whitespace glyphs in
18814 IT->glyph_row to `trailing-whitespace'. */
18815 if ((row->reversed_p ? glyph <= start : glyph >= start)
18816 && BUFFERP (glyph->object)
18817 && (glyph->type == STRETCH_GLYPH
18818 || (glyph->type == CHAR_GLYPH
18819 && glyph->u.ch == ' '))
18820 && trailing_whitespace_p (glyph->charpos))
18822 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18823 if (face_id < 0)
18824 return;
18826 if (!row->reversed_p)
18828 while (glyph >= start
18829 && BUFFERP (glyph->object)
18830 && (glyph->type == STRETCH_GLYPH
18831 || (glyph->type == CHAR_GLYPH
18832 && glyph->u.ch == ' ')))
18833 (glyph--)->face_id = face_id;
18835 else
18837 while (glyph <= start
18838 && BUFFERP (glyph->object)
18839 && (glyph->type == STRETCH_GLYPH
18840 || (glyph->type == CHAR_GLYPH
18841 && glyph->u.ch == ' ')))
18842 (glyph++)->face_id = face_id;
18849 /* Value is non-zero if glyph row ROW should be
18850 used to hold the cursor. */
18852 static int
18853 cursor_row_p (struct glyph_row *row)
18855 int result = 1;
18857 if (PT == CHARPOS (row->end.pos)
18858 || PT == MATRIX_ROW_END_CHARPOS (row))
18860 /* Suppose the row ends on a string.
18861 Unless the row is continued, that means it ends on a newline
18862 in the string. If it's anything other than a display string
18863 (e.g., a before-string from an overlay), we don't want the
18864 cursor there. (This heuristic seems to give the optimal
18865 behavior for the various types of multi-line strings.)
18866 One exception: if the string has `cursor' property on one of
18867 its characters, we _do_ want the cursor there. */
18868 if (CHARPOS (row->end.string_pos) >= 0)
18870 if (row->continued_p)
18871 result = 1;
18872 else
18874 /* Check for `display' property. */
18875 struct glyph *beg = row->glyphs[TEXT_AREA];
18876 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18877 struct glyph *glyph;
18879 result = 0;
18880 for (glyph = end; glyph >= beg; --glyph)
18881 if (STRINGP (glyph->object))
18883 Lisp_Object prop
18884 = Fget_char_property (make_number (PT),
18885 Qdisplay, Qnil);
18886 result =
18887 (!NILP (prop)
18888 && display_prop_string_p (prop, glyph->object));
18889 /* If there's a `cursor' property on one of the
18890 string's characters, this row is a cursor row,
18891 even though this is not a display string. */
18892 if (!result)
18894 Lisp_Object s = glyph->object;
18896 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18898 ptrdiff_t gpos = glyph->charpos;
18900 if (!NILP (Fget_char_property (make_number (gpos),
18901 Qcursor, s)))
18903 result = 1;
18904 break;
18908 break;
18912 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18914 /* If the row ends in middle of a real character,
18915 and the line is continued, we want the cursor here.
18916 That's because CHARPOS (ROW->end.pos) would equal
18917 PT if PT is before the character. */
18918 if (!row->ends_in_ellipsis_p)
18919 result = row->continued_p;
18920 else
18921 /* If the row ends in an ellipsis, then
18922 CHARPOS (ROW->end.pos) will equal point after the
18923 invisible text. We want that position to be displayed
18924 after the ellipsis. */
18925 result = 0;
18927 /* If the row ends at ZV, display the cursor at the end of that
18928 row instead of at the start of the row below. */
18929 else if (row->ends_at_zv_p)
18930 result = 1;
18931 else
18932 result = 0;
18935 return result;
18940 /* Push the property PROP so that it will be rendered at the current
18941 position in IT. Return 1 if PROP was successfully pushed, 0
18942 otherwise. Called from handle_line_prefix to handle the
18943 `line-prefix' and `wrap-prefix' properties. */
18945 static int
18946 push_prefix_prop (struct it *it, Lisp_Object prop)
18948 struct text_pos pos =
18949 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18951 eassert (it->method == GET_FROM_BUFFER
18952 || it->method == GET_FROM_DISPLAY_VECTOR
18953 || it->method == GET_FROM_STRING);
18955 /* We need to save the current buffer/string position, so it will be
18956 restored by pop_it, because iterate_out_of_display_property
18957 depends on that being set correctly, but some situations leave
18958 it->position not yet set when this function is called. */
18959 push_it (it, &pos);
18961 if (STRINGP (prop))
18963 if (SCHARS (prop) == 0)
18965 pop_it (it);
18966 return 0;
18969 it->string = prop;
18970 it->string_from_prefix_prop_p = 1;
18971 it->multibyte_p = STRING_MULTIBYTE (it->string);
18972 it->current.overlay_string_index = -1;
18973 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18974 it->end_charpos = it->string_nchars = SCHARS (it->string);
18975 it->method = GET_FROM_STRING;
18976 it->stop_charpos = 0;
18977 it->prev_stop = 0;
18978 it->base_level_stop = 0;
18980 /* Force paragraph direction to be that of the parent
18981 buffer/string. */
18982 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18983 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18984 else
18985 it->paragraph_embedding = L2R;
18987 /* Set up the bidi iterator for this display string. */
18988 if (it->bidi_p)
18990 it->bidi_it.string.lstring = it->string;
18991 it->bidi_it.string.s = NULL;
18992 it->bidi_it.string.schars = it->end_charpos;
18993 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18994 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18995 it->bidi_it.string.unibyte = !it->multibyte_p;
18996 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18999 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19001 it->method = GET_FROM_STRETCH;
19002 it->object = prop;
19004 #ifdef HAVE_WINDOW_SYSTEM
19005 else if (IMAGEP (prop))
19007 it->what = IT_IMAGE;
19008 it->image_id = lookup_image (it->f, prop);
19009 it->method = GET_FROM_IMAGE;
19011 #endif /* HAVE_WINDOW_SYSTEM */
19012 else
19014 pop_it (it); /* bogus display property, give up */
19015 return 0;
19018 return 1;
19021 /* Return the character-property PROP at the current position in IT. */
19023 static Lisp_Object
19024 get_it_property (struct it *it, Lisp_Object prop)
19026 Lisp_Object position;
19028 if (STRINGP (it->object))
19029 position = make_number (IT_STRING_CHARPOS (*it));
19030 else if (BUFFERP (it->object))
19031 position = make_number (IT_CHARPOS (*it));
19032 else
19033 return Qnil;
19035 return Fget_char_property (position, prop, it->object);
19038 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19040 static void
19041 handle_line_prefix (struct it *it)
19043 Lisp_Object prefix;
19045 if (it->continuation_lines_width > 0)
19047 prefix = get_it_property (it, Qwrap_prefix);
19048 if (NILP (prefix))
19049 prefix = Vwrap_prefix;
19051 else
19053 prefix = get_it_property (it, Qline_prefix);
19054 if (NILP (prefix))
19055 prefix = Vline_prefix;
19057 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19059 /* If the prefix is wider than the window, and we try to wrap
19060 it, it would acquire its own wrap prefix, and so on till the
19061 iterator stack overflows. So, don't wrap the prefix. */
19062 it->line_wrap = TRUNCATE;
19063 it->avoid_cursor_p = 1;
19069 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19070 only for R2L lines from display_line and display_string, when they
19071 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19072 the line/string needs to be continued on the next glyph row. */
19073 static void
19074 unproduce_glyphs (struct it *it, int n)
19076 struct glyph *glyph, *end;
19078 eassert (it->glyph_row);
19079 eassert (it->glyph_row->reversed_p);
19080 eassert (it->area == TEXT_AREA);
19081 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19083 if (n > it->glyph_row->used[TEXT_AREA])
19084 n = it->glyph_row->used[TEXT_AREA];
19085 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19086 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19087 for ( ; glyph < end; glyph++)
19088 glyph[-n] = *glyph;
19091 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19092 and ROW->maxpos. */
19093 static void
19094 find_row_edges (struct it *it, struct glyph_row *row,
19095 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19096 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19098 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19099 lines' rows is implemented for bidi-reordered rows. */
19101 /* ROW->minpos is the value of min_pos, the minimal buffer position
19102 we have in ROW, or ROW->start.pos if that is smaller. */
19103 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19104 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19105 else
19106 /* We didn't find buffer positions smaller than ROW->start, or
19107 didn't find _any_ valid buffer positions in any of the glyphs,
19108 so we must trust the iterator's computed positions. */
19109 row->minpos = row->start.pos;
19110 if (max_pos <= 0)
19112 max_pos = CHARPOS (it->current.pos);
19113 max_bpos = BYTEPOS (it->current.pos);
19116 /* Here are the various use-cases for ending the row, and the
19117 corresponding values for ROW->maxpos:
19119 Line ends in a newline from buffer eol_pos + 1
19120 Line is continued from buffer max_pos + 1
19121 Line is truncated on right it->current.pos
19122 Line ends in a newline from string max_pos + 1(*)
19123 (*) + 1 only when line ends in a forward scan
19124 Line is continued from string max_pos
19125 Line is continued from display vector max_pos
19126 Line is entirely from a string min_pos == max_pos
19127 Line is entirely from a display vector min_pos == max_pos
19128 Line that ends at ZV ZV
19130 If you discover other use-cases, please add them here as
19131 appropriate. */
19132 if (row->ends_at_zv_p)
19133 row->maxpos = it->current.pos;
19134 else if (row->used[TEXT_AREA])
19136 int seen_this_string = 0;
19137 struct glyph_row *r1 = row - 1;
19139 /* Did we see the same display string on the previous row? */
19140 if (STRINGP (it->object)
19141 /* this is not the first row */
19142 && row > it->w->desired_matrix->rows
19143 /* previous row is not the header line */
19144 && !r1->mode_line_p
19145 /* previous row also ends in a newline from a string */
19146 && r1->ends_in_newline_from_string_p)
19148 struct glyph *start, *end;
19150 /* Search for the last glyph of the previous row that came
19151 from buffer or string. Depending on whether the row is
19152 L2R or R2L, we need to process it front to back or the
19153 other way round. */
19154 if (!r1->reversed_p)
19156 start = r1->glyphs[TEXT_AREA];
19157 end = start + r1->used[TEXT_AREA];
19158 /* Glyphs inserted by redisplay have an integer (zero)
19159 as their object. */
19160 while (end > start
19161 && INTEGERP ((end - 1)->object)
19162 && (end - 1)->charpos <= 0)
19163 --end;
19164 if (end > start)
19166 if (EQ ((end - 1)->object, it->object))
19167 seen_this_string = 1;
19169 else
19170 /* If all the glyphs of the previous row were inserted
19171 by redisplay, it means the previous row was
19172 produced from a single newline, which is only
19173 possible if that newline came from the same string
19174 as the one which produced this ROW. */
19175 seen_this_string = 1;
19177 else
19179 end = r1->glyphs[TEXT_AREA] - 1;
19180 start = end + r1->used[TEXT_AREA];
19181 while (end < start
19182 && INTEGERP ((end + 1)->object)
19183 && (end + 1)->charpos <= 0)
19184 ++end;
19185 if (end < start)
19187 if (EQ ((end + 1)->object, it->object))
19188 seen_this_string = 1;
19190 else
19191 seen_this_string = 1;
19194 /* Take note of each display string that covers a newline only
19195 once, the first time we see it. This is for when a display
19196 string includes more than one newline in it. */
19197 if (row->ends_in_newline_from_string_p && !seen_this_string)
19199 /* If we were scanning the buffer forward when we displayed
19200 the string, we want to account for at least one buffer
19201 position that belongs to this row (position covered by
19202 the display string), so that cursor positioning will
19203 consider this row as a candidate when point is at the end
19204 of the visual line represented by this row. This is not
19205 required when scanning back, because max_pos will already
19206 have a much larger value. */
19207 if (CHARPOS (row->end.pos) > max_pos)
19208 INC_BOTH (max_pos, max_bpos);
19209 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19211 else if (CHARPOS (it->eol_pos) > 0)
19212 SET_TEXT_POS (row->maxpos,
19213 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19214 else if (row->continued_p)
19216 /* If max_pos is different from IT's current position, it
19217 means IT->method does not belong to the display element
19218 at max_pos. However, it also means that the display
19219 element at max_pos was displayed in its entirety on this
19220 line, which is equivalent to saying that the next line
19221 starts at the next buffer position. */
19222 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19223 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19224 else
19226 INC_BOTH (max_pos, max_bpos);
19227 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19230 else if (row->truncated_on_right_p)
19231 /* display_line already called reseat_at_next_visible_line_start,
19232 which puts the iterator at the beginning of the next line, in
19233 the logical order. */
19234 row->maxpos = it->current.pos;
19235 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19236 /* A line that is entirely from a string/image/stretch... */
19237 row->maxpos = row->minpos;
19238 else
19239 emacs_abort ();
19241 else
19242 row->maxpos = it->current.pos;
19245 /* Construct the glyph row IT->glyph_row in the desired matrix of
19246 IT->w from text at the current position of IT. See dispextern.h
19247 for an overview of struct it. Value is non-zero if
19248 IT->glyph_row displays text, as opposed to a line displaying ZV
19249 only. */
19251 static int
19252 display_line (struct it *it)
19254 struct glyph_row *row = it->glyph_row;
19255 Lisp_Object overlay_arrow_string;
19256 struct it wrap_it;
19257 void *wrap_data = NULL;
19258 int may_wrap = 0, wrap_x IF_LINT (= 0);
19259 int wrap_row_used = -1;
19260 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19261 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19262 int wrap_row_extra_line_spacing IF_LINT (= 0);
19263 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19264 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19265 int cvpos;
19266 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19267 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19269 /* We always start displaying at hpos zero even if hscrolled. */
19270 eassert (it->hpos == 0 && it->current_x == 0);
19272 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19273 >= it->w->desired_matrix->nrows)
19275 it->w->nrows_scale_factor++;
19276 fonts_changed_p = 1;
19277 return 0;
19280 /* Is IT->w showing the region? */
19281 wset_region_showing (it->w, it->region_beg_charpos > 0 ? Qt : Qnil);
19283 /* Clear the result glyph row and enable it. */
19284 prepare_desired_row (row);
19286 row->y = it->current_y;
19287 row->start = it->start;
19288 row->continuation_lines_width = it->continuation_lines_width;
19289 row->displays_text_p = 1;
19290 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19291 it->starts_in_middle_of_char_p = 0;
19293 /* Arrange the overlays nicely for our purposes. Usually, we call
19294 display_line on only one line at a time, in which case this
19295 can't really hurt too much, or we call it on lines which appear
19296 one after another in the buffer, in which case all calls to
19297 recenter_overlay_lists but the first will be pretty cheap. */
19298 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19300 /* Move over display elements that are not visible because we are
19301 hscrolled. This may stop at an x-position < IT->first_visible_x
19302 if the first glyph is partially visible or if we hit a line end. */
19303 if (it->current_x < it->first_visible_x)
19305 enum move_it_result move_result;
19307 this_line_min_pos = row->start.pos;
19308 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19309 MOVE_TO_POS | MOVE_TO_X);
19310 /* If we are under a large hscroll, move_it_in_display_line_to
19311 could hit the end of the line without reaching
19312 it->first_visible_x. Pretend that we did reach it. This is
19313 especially important on a TTY, where we will call
19314 extend_face_to_end_of_line, which needs to know how many
19315 blank glyphs to produce. */
19316 if (it->current_x < it->first_visible_x
19317 && (move_result == MOVE_NEWLINE_OR_CR
19318 || move_result == MOVE_POS_MATCH_OR_ZV))
19319 it->current_x = it->first_visible_x;
19321 /* Record the smallest positions seen while we moved over
19322 display elements that are not visible. This is needed by
19323 redisplay_internal for optimizing the case where the cursor
19324 stays inside the same line. The rest of this function only
19325 considers positions that are actually displayed, so
19326 RECORD_MAX_MIN_POS will not otherwise record positions that
19327 are hscrolled to the left of the left edge of the window. */
19328 min_pos = CHARPOS (this_line_min_pos);
19329 min_bpos = BYTEPOS (this_line_min_pos);
19331 else
19333 /* We only do this when not calling `move_it_in_display_line_to'
19334 above, because move_it_in_display_line_to calls
19335 handle_line_prefix itself. */
19336 handle_line_prefix (it);
19339 /* Get the initial row height. This is either the height of the
19340 text hscrolled, if there is any, or zero. */
19341 row->ascent = it->max_ascent;
19342 row->height = it->max_ascent + it->max_descent;
19343 row->phys_ascent = it->max_phys_ascent;
19344 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19345 row->extra_line_spacing = it->max_extra_line_spacing;
19347 /* Utility macro to record max and min buffer positions seen until now. */
19348 #define RECORD_MAX_MIN_POS(IT) \
19349 do \
19351 int composition_p = !STRINGP ((IT)->string) \
19352 && ((IT)->what == IT_COMPOSITION); \
19353 ptrdiff_t current_pos = \
19354 composition_p ? (IT)->cmp_it.charpos \
19355 : IT_CHARPOS (*(IT)); \
19356 ptrdiff_t current_bpos = \
19357 composition_p ? CHAR_TO_BYTE (current_pos) \
19358 : IT_BYTEPOS (*(IT)); \
19359 if (current_pos < min_pos) \
19361 min_pos = current_pos; \
19362 min_bpos = current_bpos; \
19364 if (IT_CHARPOS (*it) > max_pos) \
19366 max_pos = IT_CHARPOS (*it); \
19367 max_bpos = IT_BYTEPOS (*it); \
19370 while (0)
19372 /* Loop generating characters. The loop is left with IT on the next
19373 character to display. */
19374 while (1)
19376 int n_glyphs_before, hpos_before, x_before;
19377 int x, nglyphs;
19378 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19380 /* Retrieve the next thing to display. Value is zero if end of
19381 buffer reached. */
19382 if (!get_next_display_element (it))
19384 /* Maybe add a space at the end of this line that is used to
19385 display the cursor there under X. Set the charpos of the
19386 first glyph of blank lines not corresponding to any text
19387 to -1. */
19388 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19389 row->exact_window_width_line_p = 1;
19390 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19391 || row->used[TEXT_AREA] == 0)
19393 row->glyphs[TEXT_AREA]->charpos = -1;
19394 row->displays_text_p = 0;
19396 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19397 && (!MINI_WINDOW_P (it->w)
19398 || (minibuf_level && EQ (it->window, minibuf_window))))
19399 row->indicate_empty_line_p = 1;
19402 it->continuation_lines_width = 0;
19403 row->ends_at_zv_p = 1;
19404 /* A row that displays right-to-left text must always have
19405 its last face extended all the way to the end of line,
19406 even if this row ends in ZV, because we still write to
19407 the screen left to right. We also need to extend the
19408 last face if the default face is remapped to some
19409 different face, otherwise the functions that clear
19410 portions of the screen will clear with the default face's
19411 background color. */
19412 if (row->reversed_p
19413 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19414 extend_face_to_end_of_line (it);
19415 break;
19418 /* Now, get the metrics of what we want to display. This also
19419 generates glyphs in `row' (which is IT->glyph_row). */
19420 n_glyphs_before = row->used[TEXT_AREA];
19421 x = it->current_x;
19423 /* Remember the line height so far in case the next element doesn't
19424 fit on the line. */
19425 if (it->line_wrap != TRUNCATE)
19427 ascent = it->max_ascent;
19428 descent = it->max_descent;
19429 phys_ascent = it->max_phys_ascent;
19430 phys_descent = it->max_phys_descent;
19432 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19434 if (IT_DISPLAYING_WHITESPACE (it))
19435 may_wrap = 1;
19436 else if (may_wrap)
19438 SAVE_IT (wrap_it, *it, wrap_data);
19439 wrap_x = x;
19440 wrap_row_used = row->used[TEXT_AREA];
19441 wrap_row_ascent = row->ascent;
19442 wrap_row_height = row->height;
19443 wrap_row_phys_ascent = row->phys_ascent;
19444 wrap_row_phys_height = row->phys_height;
19445 wrap_row_extra_line_spacing = row->extra_line_spacing;
19446 wrap_row_min_pos = min_pos;
19447 wrap_row_min_bpos = min_bpos;
19448 wrap_row_max_pos = max_pos;
19449 wrap_row_max_bpos = max_bpos;
19450 may_wrap = 0;
19455 PRODUCE_GLYPHS (it);
19457 /* If this display element was in marginal areas, continue with
19458 the next one. */
19459 if (it->area != TEXT_AREA)
19461 row->ascent = max (row->ascent, it->max_ascent);
19462 row->height = max (row->height, it->max_ascent + it->max_descent);
19463 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19464 row->phys_height = max (row->phys_height,
19465 it->max_phys_ascent + it->max_phys_descent);
19466 row->extra_line_spacing = max (row->extra_line_spacing,
19467 it->max_extra_line_spacing);
19468 set_iterator_to_next (it, 1);
19469 continue;
19472 /* Does the display element fit on the line? If we truncate
19473 lines, we should draw past the right edge of the window. If
19474 we don't truncate, we want to stop so that we can display the
19475 continuation glyph before the right margin. If lines are
19476 continued, there are two possible strategies for characters
19477 resulting in more than 1 glyph (e.g. tabs): Display as many
19478 glyphs as possible in this line and leave the rest for the
19479 continuation line, or display the whole element in the next
19480 line. Original redisplay did the former, so we do it also. */
19481 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19482 hpos_before = it->hpos;
19483 x_before = x;
19485 if (/* Not a newline. */
19486 nglyphs > 0
19487 /* Glyphs produced fit entirely in the line. */
19488 && it->current_x < it->last_visible_x)
19490 it->hpos += nglyphs;
19491 row->ascent = max (row->ascent, it->max_ascent);
19492 row->height = max (row->height, it->max_ascent + it->max_descent);
19493 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19494 row->phys_height = max (row->phys_height,
19495 it->max_phys_ascent + it->max_phys_descent);
19496 row->extra_line_spacing = max (row->extra_line_spacing,
19497 it->max_extra_line_spacing);
19498 if (it->current_x - it->pixel_width < it->first_visible_x)
19499 row->x = x - it->first_visible_x;
19500 /* Record the maximum and minimum buffer positions seen so
19501 far in glyphs that will be displayed by this row. */
19502 if (it->bidi_p)
19503 RECORD_MAX_MIN_POS (it);
19505 else
19507 int i, new_x;
19508 struct glyph *glyph;
19510 for (i = 0; i < nglyphs; ++i, x = new_x)
19512 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19513 new_x = x + glyph->pixel_width;
19515 if (/* Lines are continued. */
19516 it->line_wrap != TRUNCATE
19517 && (/* Glyph doesn't fit on the line. */
19518 new_x > it->last_visible_x
19519 /* Or it fits exactly on a window system frame. */
19520 || (new_x == it->last_visible_x
19521 && FRAME_WINDOW_P (it->f)
19522 && (row->reversed_p
19523 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19524 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19526 /* End of a continued line. */
19528 if (it->hpos == 0
19529 || (new_x == it->last_visible_x
19530 && FRAME_WINDOW_P (it->f)
19531 && (row->reversed_p
19532 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19533 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19535 /* Current glyph is the only one on the line or
19536 fits exactly on the line. We must continue
19537 the line because we can't draw the cursor
19538 after the glyph. */
19539 row->continued_p = 1;
19540 it->current_x = new_x;
19541 it->continuation_lines_width += new_x;
19542 ++it->hpos;
19543 if (i == nglyphs - 1)
19545 /* If line-wrap is on, check if a previous
19546 wrap point was found. */
19547 if (wrap_row_used > 0
19548 /* Even if there is a previous wrap
19549 point, continue the line here as
19550 usual, if (i) the previous character
19551 was a space or tab AND (ii) the
19552 current character is not. */
19553 && (!may_wrap
19554 || IT_DISPLAYING_WHITESPACE (it)))
19555 goto back_to_wrap;
19557 /* Record the maximum and minimum buffer
19558 positions seen so far in glyphs that will be
19559 displayed by this row. */
19560 if (it->bidi_p)
19561 RECORD_MAX_MIN_POS (it);
19562 set_iterator_to_next (it, 1);
19563 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19565 if (!get_next_display_element (it))
19567 row->exact_window_width_line_p = 1;
19568 it->continuation_lines_width = 0;
19569 row->continued_p = 0;
19570 row->ends_at_zv_p = 1;
19572 else if (ITERATOR_AT_END_OF_LINE_P (it))
19574 row->continued_p = 0;
19575 row->exact_window_width_line_p = 1;
19579 else if (it->bidi_p)
19580 RECORD_MAX_MIN_POS (it);
19582 else if (CHAR_GLYPH_PADDING_P (*glyph)
19583 && !FRAME_WINDOW_P (it->f))
19585 /* A padding glyph that doesn't fit on this line.
19586 This means the whole character doesn't fit
19587 on the line. */
19588 if (row->reversed_p)
19589 unproduce_glyphs (it, row->used[TEXT_AREA]
19590 - n_glyphs_before);
19591 row->used[TEXT_AREA] = n_glyphs_before;
19593 /* Fill the rest of the row with continuation
19594 glyphs like in 20.x. */
19595 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19596 < row->glyphs[1 + TEXT_AREA])
19597 produce_special_glyphs (it, IT_CONTINUATION);
19599 row->continued_p = 1;
19600 it->current_x = x_before;
19601 it->continuation_lines_width += x_before;
19603 /* Restore the height to what it was before the
19604 element not fitting on the line. */
19605 it->max_ascent = ascent;
19606 it->max_descent = descent;
19607 it->max_phys_ascent = phys_ascent;
19608 it->max_phys_descent = phys_descent;
19610 else if (wrap_row_used > 0)
19612 back_to_wrap:
19613 if (row->reversed_p)
19614 unproduce_glyphs (it,
19615 row->used[TEXT_AREA] - wrap_row_used);
19616 RESTORE_IT (it, &wrap_it, wrap_data);
19617 it->continuation_lines_width += wrap_x;
19618 row->used[TEXT_AREA] = wrap_row_used;
19619 row->ascent = wrap_row_ascent;
19620 row->height = wrap_row_height;
19621 row->phys_ascent = wrap_row_phys_ascent;
19622 row->phys_height = wrap_row_phys_height;
19623 row->extra_line_spacing = wrap_row_extra_line_spacing;
19624 min_pos = wrap_row_min_pos;
19625 min_bpos = wrap_row_min_bpos;
19626 max_pos = wrap_row_max_pos;
19627 max_bpos = wrap_row_max_bpos;
19628 row->continued_p = 1;
19629 row->ends_at_zv_p = 0;
19630 row->exact_window_width_line_p = 0;
19631 it->continuation_lines_width += x;
19633 /* Make sure that a non-default face is extended
19634 up to the right margin of the window. */
19635 extend_face_to_end_of_line (it);
19637 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19639 /* A TAB that extends past the right edge of the
19640 window. This produces a single glyph on
19641 window system frames. We leave the glyph in
19642 this row and let it fill the row, but don't
19643 consume the TAB. */
19644 if ((row->reversed_p
19645 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19646 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19647 produce_special_glyphs (it, IT_CONTINUATION);
19648 it->continuation_lines_width += it->last_visible_x;
19649 row->ends_in_middle_of_char_p = 1;
19650 row->continued_p = 1;
19651 glyph->pixel_width = it->last_visible_x - x;
19652 it->starts_in_middle_of_char_p = 1;
19654 else
19656 /* Something other than a TAB that draws past
19657 the right edge of the window. Restore
19658 positions to values before the element. */
19659 if (row->reversed_p)
19660 unproduce_glyphs (it, row->used[TEXT_AREA]
19661 - (n_glyphs_before + i));
19662 row->used[TEXT_AREA] = n_glyphs_before + i;
19664 /* Display continuation glyphs. */
19665 it->current_x = x_before;
19666 it->continuation_lines_width += x;
19667 if (!FRAME_WINDOW_P (it->f)
19668 || (row->reversed_p
19669 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19670 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19671 produce_special_glyphs (it, IT_CONTINUATION);
19672 row->continued_p = 1;
19674 extend_face_to_end_of_line (it);
19676 if (nglyphs > 1 && i > 0)
19678 row->ends_in_middle_of_char_p = 1;
19679 it->starts_in_middle_of_char_p = 1;
19682 /* Restore the height to what it was before the
19683 element not fitting on the line. */
19684 it->max_ascent = ascent;
19685 it->max_descent = descent;
19686 it->max_phys_ascent = phys_ascent;
19687 it->max_phys_descent = phys_descent;
19690 break;
19692 else if (new_x > it->first_visible_x)
19694 /* Increment number of glyphs actually displayed. */
19695 ++it->hpos;
19697 /* Record the maximum and minimum buffer positions
19698 seen so far in glyphs that will be displayed by
19699 this row. */
19700 if (it->bidi_p)
19701 RECORD_MAX_MIN_POS (it);
19703 if (x < it->first_visible_x)
19704 /* Glyph is partially visible, i.e. row starts at
19705 negative X position. */
19706 row->x = x - it->first_visible_x;
19708 else
19710 /* Glyph is completely off the left margin of the
19711 window. This should not happen because of the
19712 move_it_in_display_line at the start of this
19713 function, unless the text display area of the
19714 window is empty. */
19715 eassert (it->first_visible_x <= it->last_visible_x);
19718 /* Even if this display element produced no glyphs at all,
19719 we want to record its position. */
19720 if (it->bidi_p && nglyphs == 0)
19721 RECORD_MAX_MIN_POS (it);
19723 row->ascent = max (row->ascent, it->max_ascent);
19724 row->height = max (row->height, it->max_ascent + it->max_descent);
19725 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19726 row->phys_height = max (row->phys_height,
19727 it->max_phys_ascent + it->max_phys_descent);
19728 row->extra_line_spacing = max (row->extra_line_spacing,
19729 it->max_extra_line_spacing);
19731 /* End of this display line if row is continued. */
19732 if (row->continued_p || row->ends_at_zv_p)
19733 break;
19736 at_end_of_line:
19737 /* Is this a line end? If yes, we're also done, after making
19738 sure that a non-default face is extended up to the right
19739 margin of the window. */
19740 if (ITERATOR_AT_END_OF_LINE_P (it))
19742 int used_before = row->used[TEXT_AREA];
19744 row->ends_in_newline_from_string_p = STRINGP (it->object);
19746 /* Add a space at the end of the line that is used to
19747 display the cursor there. */
19748 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19749 append_space_for_newline (it, 0);
19751 /* Extend the face to the end of the line. */
19752 extend_face_to_end_of_line (it);
19754 /* Make sure we have the position. */
19755 if (used_before == 0)
19756 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19758 /* Record the position of the newline, for use in
19759 find_row_edges. */
19760 it->eol_pos = it->current.pos;
19762 /* Consume the line end. This skips over invisible lines. */
19763 set_iterator_to_next (it, 1);
19764 it->continuation_lines_width = 0;
19765 break;
19768 /* Proceed with next display element. Note that this skips
19769 over lines invisible because of selective display. */
19770 set_iterator_to_next (it, 1);
19772 /* If we truncate lines, we are done when the last displayed
19773 glyphs reach past the right margin of the window. */
19774 if (it->line_wrap == TRUNCATE
19775 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19776 ? (it->current_x >= it->last_visible_x)
19777 : (it->current_x > it->last_visible_x)))
19779 /* Maybe add truncation glyphs. */
19780 if (!FRAME_WINDOW_P (it->f)
19781 || (row->reversed_p
19782 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19783 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19785 int i, n;
19787 if (!row->reversed_p)
19789 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19790 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19791 break;
19793 else
19795 for (i = 0; i < row->used[TEXT_AREA]; i++)
19796 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19797 break;
19798 /* Remove any padding glyphs at the front of ROW, to
19799 make room for the truncation glyphs we will be
19800 adding below. The loop below always inserts at
19801 least one truncation glyph, so also remove the
19802 last glyph added to ROW. */
19803 unproduce_glyphs (it, i + 1);
19804 /* Adjust i for the loop below. */
19805 i = row->used[TEXT_AREA] - (i + 1);
19808 it->current_x = x_before;
19809 if (!FRAME_WINDOW_P (it->f))
19811 for (n = row->used[TEXT_AREA]; i < n; ++i)
19813 row->used[TEXT_AREA] = i;
19814 produce_special_glyphs (it, IT_TRUNCATION);
19817 else
19819 row->used[TEXT_AREA] = i;
19820 produce_special_glyphs (it, IT_TRUNCATION);
19823 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19825 /* Don't truncate if we can overflow newline into fringe. */
19826 if (!get_next_display_element (it))
19828 it->continuation_lines_width = 0;
19829 row->ends_at_zv_p = 1;
19830 row->exact_window_width_line_p = 1;
19831 break;
19833 if (ITERATOR_AT_END_OF_LINE_P (it))
19835 row->exact_window_width_line_p = 1;
19836 goto at_end_of_line;
19838 it->current_x = x_before;
19841 row->truncated_on_right_p = 1;
19842 it->continuation_lines_width = 0;
19843 reseat_at_next_visible_line_start (it, 0);
19844 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19845 it->hpos = hpos_before;
19846 break;
19850 if (wrap_data)
19851 bidi_unshelve_cache (wrap_data, 1);
19853 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19854 at the left window margin. */
19855 if (it->first_visible_x
19856 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19858 if (!FRAME_WINDOW_P (it->f)
19859 || (row->reversed_p
19860 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19861 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19862 insert_left_trunc_glyphs (it);
19863 row->truncated_on_left_p = 1;
19866 /* Remember the position at which this line ends.
19868 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19869 cannot be before the call to find_row_edges below, since that is
19870 where these positions are determined. */
19871 row->end = it->current;
19872 if (!it->bidi_p)
19874 row->minpos = row->start.pos;
19875 row->maxpos = row->end.pos;
19877 else
19879 /* ROW->minpos and ROW->maxpos must be the smallest and
19880 `1 + the largest' buffer positions in ROW. But if ROW was
19881 bidi-reordered, these two positions can be anywhere in the
19882 row, so we must determine them now. */
19883 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19886 /* If the start of this line is the overlay arrow-position, then
19887 mark this glyph row as the one containing the overlay arrow.
19888 This is clearly a mess with variable size fonts. It would be
19889 better to let it be displayed like cursors under X. */
19890 if ((row->displays_text_p || !overlay_arrow_seen)
19891 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19892 !NILP (overlay_arrow_string)))
19894 /* Overlay arrow in window redisplay is a fringe bitmap. */
19895 if (STRINGP (overlay_arrow_string))
19897 struct glyph_row *arrow_row
19898 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19899 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19900 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19901 struct glyph *p = row->glyphs[TEXT_AREA];
19902 struct glyph *p2, *end;
19904 /* Copy the arrow glyphs. */
19905 while (glyph < arrow_end)
19906 *p++ = *glyph++;
19908 /* Throw away padding glyphs. */
19909 p2 = p;
19910 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19911 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19912 ++p2;
19913 if (p2 > p)
19915 while (p2 < end)
19916 *p++ = *p2++;
19917 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19920 else
19922 eassert (INTEGERP (overlay_arrow_string));
19923 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19925 overlay_arrow_seen = 1;
19928 /* Highlight trailing whitespace. */
19929 if (!NILP (Vshow_trailing_whitespace))
19930 highlight_trailing_whitespace (it->f, it->glyph_row);
19932 /* Compute pixel dimensions of this line. */
19933 compute_line_metrics (it);
19935 /* Implementation note: No changes in the glyphs of ROW or in their
19936 faces can be done past this point, because compute_line_metrics
19937 computes ROW's hash value and stores it within the glyph_row
19938 structure. */
19940 /* Record whether this row ends inside an ellipsis. */
19941 row->ends_in_ellipsis_p
19942 = (it->method == GET_FROM_DISPLAY_VECTOR
19943 && it->ellipsis_p);
19945 /* Save fringe bitmaps in this row. */
19946 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19947 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19948 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19949 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19951 it->left_user_fringe_bitmap = 0;
19952 it->left_user_fringe_face_id = 0;
19953 it->right_user_fringe_bitmap = 0;
19954 it->right_user_fringe_face_id = 0;
19956 /* Maybe set the cursor. */
19957 cvpos = it->w->cursor.vpos;
19958 if ((cvpos < 0
19959 /* In bidi-reordered rows, keep checking for proper cursor
19960 position even if one has been found already, because buffer
19961 positions in such rows change non-linearly with ROW->VPOS,
19962 when a line is continued. One exception: when we are at ZV,
19963 display cursor on the first suitable glyph row, since all
19964 the empty rows after that also have their position set to ZV. */
19965 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19966 lines' rows is implemented for bidi-reordered rows. */
19967 || (it->bidi_p
19968 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19969 && PT >= MATRIX_ROW_START_CHARPOS (row)
19970 && PT <= MATRIX_ROW_END_CHARPOS (row)
19971 && cursor_row_p (row))
19972 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19974 /* Prepare for the next line. This line starts horizontally at (X
19975 HPOS) = (0 0). Vertical positions are incremented. As a
19976 convenience for the caller, IT->glyph_row is set to the next
19977 row to be used. */
19978 it->current_x = it->hpos = 0;
19979 it->current_y += row->height;
19980 SET_TEXT_POS (it->eol_pos, 0, 0);
19981 ++it->vpos;
19982 ++it->glyph_row;
19983 /* The next row should by default use the same value of the
19984 reversed_p flag as this one. set_iterator_to_next decides when
19985 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19986 the flag accordingly. */
19987 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19988 it->glyph_row->reversed_p = row->reversed_p;
19989 it->start = row->end;
19990 return row->displays_text_p;
19992 #undef RECORD_MAX_MIN_POS
19995 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19996 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19997 doc: /* Return paragraph direction at point in BUFFER.
19998 Value is either `left-to-right' or `right-to-left'.
19999 If BUFFER is omitted or nil, it defaults to the current buffer.
20001 Paragraph direction determines how the text in the paragraph is displayed.
20002 In left-to-right paragraphs, text begins at the left margin of the window
20003 and the reading direction is generally left to right. In right-to-left
20004 paragraphs, text begins at the right margin and is read from right to left.
20006 See also `bidi-paragraph-direction'. */)
20007 (Lisp_Object buffer)
20009 struct buffer *buf = current_buffer;
20010 struct buffer *old = buf;
20012 if (! NILP (buffer))
20014 CHECK_BUFFER (buffer);
20015 buf = XBUFFER (buffer);
20018 if (NILP (BVAR (buf, bidi_display_reordering))
20019 || NILP (BVAR (buf, enable_multibyte_characters))
20020 /* When we are loading loadup.el, the character property tables
20021 needed for bidi iteration are not yet available. */
20022 || !NILP (Vpurify_flag))
20023 return Qleft_to_right;
20024 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20025 return BVAR (buf, bidi_paragraph_direction);
20026 else
20028 /* Determine the direction from buffer text. We could try to
20029 use current_matrix if it is up to date, but this seems fast
20030 enough as it is. */
20031 struct bidi_it itb;
20032 ptrdiff_t pos = BUF_PT (buf);
20033 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20034 int c;
20035 void *itb_data = bidi_shelve_cache ();
20037 set_buffer_temp (buf);
20038 /* bidi_paragraph_init finds the base direction of the paragraph
20039 by searching forward from paragraph start. We need the base
20040 direction of the current or _previous_ paragraph, so we need
20041 to make sure we are within that paragraph. To that end, find
20042 the previous non-empty line. */
20043 if (pos >= ZV && pos > BEGV)
20045 pos--;
20046 bytepos = CHAR_TO_BYTE (pos);
20048 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20049 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20051 while ((c = FETCH_BYTE (bytepos)) == '\n'
20052 || c == ' ' || c == '\t' || c == '\f')
20054 if (bytepos <= BEGV_BYTE)
20055 break;
20056 bytepos--;
20057 pos--;
20059 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20060 bytepos--;
20062 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20063 itb.paragraph_dir = NEUTRAL_DIR;
20064 itb.string.s = NULL;
20065 itb.string.lstring = Qnil;
20066 itb.string.bufpos = 0;
20067 itb.string.unibyte = 0;
20068 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20069 bidi_unshelve_cache (itb_data, 0);
20070 set_buffer_temp (old);
20071 switch (itb.paragraph_dir)
20073 case L2R:
20074 return Qleft_to_right;
20075 break;
20076 case R2L:
20077 return Qright_to_left;
20078 break;
20079 default:
20080 emacs_abort ();
20087 /***********************************************************************
20088 Menu Bar
20089 ***********************************************************************/
20091 /* Redisplay the menu bar in the frame for window W.
20093 The menu bar of X frames that don't have X toolkit support is
20094 displayed in a special window W->frame->menu_bar_window.
20096 The menu bar of terminal frames is treated specially as far as
20097 glyph matrices are concerned. Menu bar lines are not part of
20098 windows, so the update is done directly on the frame matrix rows
20099 for the menu bar. */
20101 static void
20102 display_menu_bar (struct window *w)
20104 struct frame *f = XFRAME (WINDOW_FRAME (w));
20105 struct it it;
20106 Lisp_Object items;
20107 int i;
20109 /* Don't do all this for graphical frames. */
20110 #ifdef HAVE_NTGUI
20111 if (FRAME_W32_P (f))
20112 return;
20113 #endif
20114 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20115 if (FRAME_X_P (f))
20116 return;
20117 #endif
20119 #ifdef HAVE_NS
20120 if (FRAME_NS_P (f))
20121 return;
20122 #endif /* HAVE_NS */
20124 #ifdef USE_X_TOOLKIT
20125 eassert (!FRAME_WINDOW_P (f));
20126 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20127 it.first_visible_x = 0;
20128 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20129 #else /* not USE_X_TOOLKIT */
20130 if (FRAME_WINDOW_P (f))
20132 /* Menu bar lines are displayed in the desired matrix of the
20133 dummy window menu_bar_window. */
20134 struct window *menu_w;
20135 eassert (WINDOWP (f->menu_bar_window));
20136 menu_w = XWINDOW (f->menu_bar_window);
20137 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20138 MENU_FACE_ID);
20139 it.first_visible_x = 0;
20140 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20142 else
20144 /* This is a TTY frame, i.e. character hpos/vpos are used as
20145 pixel x/y. */
20146 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20147 MENU_FACE_ID);
20148 it.first_visible_x = 0;
20149 it.last_visible_x = FRAME_COLS (f);
20151 #endif /* not USE_X_TOOLKIT */
20153 /* FIXME: This should be controlled by a user option. See the
20154 comments in redisplay_tool_bar and display_mode_line about
20155 this. */
20156 it.paragraph_embedding = L2R;
20158 if (! mode_line_inverse_video)
20159 /* Force the menu-bar to be displayed in the default face. */
20160 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20162 /* Clear all rows of the menu bar. */
20163 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20165 struct glyph_row *row = it.glyph_row + i;
20166 clear_glyph_row (row);
20167 row->enabled_p = 1;
20168 row->full_width_p = 1;
20171 /* Display all items of the menu bar. */
20172 items = FRAME_MENU_BAR_ITEMS (it.f);
20173 for (i = 0; i < ASIZE (items); i += 4)
20175 Lisp_Object string;
20177 /* Stop at nil string. */
20178 string = AREF (items, i + 1);
20179 if (NILP (string))
20180 break;
20182 /* Remember where item was displayed. */
20183 ASET (items, i + 3, make_number (it.hpos));
20185 /* Display the item, pad with one space. */
20186 if (it.current_x < it.last_visible_x)
20187 display_string (NULL, string, Qnil, 0, 0, &it,
20188 SCHARS (string) + 1, 0, 0, -1);
20191 /* Fill out the line with spaces. */
20192 if (it.current_x < it.last_visible_x)
20193 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20195 /* Compute the total height of the lines. */
20196 compute_line_metrics (&it);
20201 /***********************************************************************
20202 Mode Line
20203 ***********************************************************************/
20205 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20206 FORCE is non-zero, redisplay mode lines unconditionally.
20207 Otherwise, redisplay only mode lines that are garbaged. Value is
20208 the number of windows whose mode lines were redisplayed. */
20210 static int
20211 redisplay_mode_lines (Lisp_Object window, int force)
20213 int nwindows = 0;
20215 while (!NILP (window))
20217 struct window *w = XWINDOW (window);
20219 if (WINDOWP (w->hchild))
20220 nwindows += redisplay_mode_lines (w->hchild, force);
20221 else if (WINDOWP (w->vchild))
20222 nwindows += redisplay_mode_lines (w->vchild, force);
20223 else if (force
20224 || FRAME_GARBAGED_P (XFRAME (w->frame))
20225 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20227 struct text_pos lpoint;
20228 struct buffer *old = current_buffer;
20230 /* Set the window's buffer for the mode line display. */
20231 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20232 set_buffer_internal_1 (XBUFFER (w->buffer));
20234 /* Point refers normally to the selected window. For any
20235 other window, set up appropriate value. */
20236 if (!EQ (window, selected_window))
20238 struct text_pos pt;
20240 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20241 if (CHARPOS (pt) < BEGV)
20242 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20243 else if (CHARPOS (pt) > (ZV - 1))
20244 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20245 else
20246 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20249 /* Display mode lines. */
20250 clear_glyph_matrix (w->desired_matrix);
20251 if (display_mode_lines (w))
20253 ++nwindows;
20254 w->must_be_updated_p = 1;
20257 /* Restore old settings. */
20258 set_buffer_internal_1 (old);
20259 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20262 window = w->next;
20265 return nwindows;
20269 /* Display the mode and/or header line of window W. Value is the
20270 sum number of mode lines and header lines displayed. */
20272 static int
20273 display_mode_lines (struct window *w)
20275 Lisp_Object old_selected_window, old_selected_frame;
20276 int n = 0;
20278 old_selected_frame = selected_frame;
20279 selected_frame = w->frame;
20280 old_selected_window = selected_window;
20281 XSETWINDOW (selected_window, w);
20283 /* These will be set while the mode line specs are processed. */
20284 line_number_displayed = 0;
20285 wset_column_number_displayed (w, Qnil);
20287 if (WINDOW_WANTS_MODELINE_P (w))
20289 struct window *sel_w = XWINDOW (old_selected_window);
20291 /* Select mode line face based on the real selected window. */
20292 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20293 BVAR (current_buffer, mode_line_format));
20294 ++n;
20297 if (WINDOW_WANTS_HEADER_LINE_P (w))
20299 display_mode_line (w, HEADER_LINE_FACE_ID,
20300 BVAR (current_buffer, header_line_format));
20301 ++n;
20304 selected_frame = old_selected_frame;
20305 selected_window = old_selected_window;
20306 return n;
20310 /* Display mode or header line of window W. FACE_ID specifies which
20311 line to display; it is either MODE_LINE_FACE_ID or
20312 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20313 display. Value is the pixel height of the mode/header line
20314 displayed. */
20316 static int
20317 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20319 struct it it;
20320 struct face *face;
20321 ptrdiff_t count = SPECPDL_INDEX ();
20323 init_iterator (&it, w, -1, -1, NULL, face_id);
20324 /* Don't extend on a previously drawn mode-line.
20325 This may happen if called from pos_visible_p. */
20326 it.glyph_row->enabled_p = 0;
20327 prepare_desired_row (it.glyph_row);
20329 it.glyph_row->mode_line_p = 1;
20331 if (! mode_line_inverse_video)
20332 /* Force the mode-line to be displayed in the default face. */
20333 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20335 /* FIXME: This should be controlled by a user option. But
20336 supporting such an option is not trivial, since the mode line is
20337 made up of many separate strings. */
20338 it.paragraph_embedding = L2R;
20340 record_unwind_protect (unwind_format_mode_line,
20341 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20343 mode_line_target = MODE_LINE_DISPLAY;
20345 /* Temporarily make frame's keyboard the current kboard so that
20346 kboard-local variables in the mode_line_format will get the right
20347 values. */
20348 push_kboard (FRAME_KBOARD (it.f));
20349 record_unwind_save_match_data ();
20350 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20351 pop_kboard ();
20353 unbind_to (count, Qnil);
20355 /* Fill up with spaces. */
20356 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20358 compute_line_metrics (&it);
20359 it.glyph_row->full_width_p = 1;
20360 it.glyph_row->continued_p = 0;
20361 it.glyph_row->truncated_on_left_p = 0;
20362 it.glyph_row->truncated_on_right_p = 0;
20364 /* Make a 3D mode-line have a shadow at its right end. */
20365 face = FACE_FROM_ID (it.f, face_id);
20366 extend_face_to_end_of_line (&it);
20367 if (face->box != FACE_NO_BOX)
20369 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20370 + it.glyph_row->used[TEXT_AREA] - 1);
20371 last->right_box_line_p = 1;
20374 return it.glyph_row->height;
20377 /* Move element ELT in LIST to the front of LIST.
20378 Return the updated list. */
20380 static Lisp_Object
20381 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20383 register Lisp_Object tail, prev;
20384 register Lisp_Object tem;
20386 tail = list;
20387 prev = Qnil;
20388 while (CONSP (tail))
20390 tem = XCAR (tail);
20392 if (EQ (elt, tem))
20394 /* Splice out the link TAIL. */
20395 if (NILP (prev))
20396 list = XCDR (tail);
20397 else
20398 Fsetcdr (prev, XCDR (tail));
20400 /* Now make it the first. */
20401 Fsetcdr (tail, list);
20402 return tail;
20404 else
20405 prev = tail;
20406 tail = XCDR (tail);
20407 QUIT;
20410 /* Not found--return unchanged LIST. */
20411 return list;
20414 /* Contribute ELT to the mode line for window IT->w. How it
20415 translates into text depends on its data type.
20417 IT describes the display environment in which we display, as usual.
20419 DEPTH is the depth in recursion. It is used to prevent
20420 infinite recursion here.
20422 FIELD_WIDTH is the number of characters the display of ELT should
20423 occupy in the mode line, and PRECISION is the maximum number of
20424 characters to display from ELT's representation. See
20425 display_string for details.
20427 Returns the hpos of the end of the text generated by ELT.
20429 PROPS is a property list to add to any string we encounter.
20431 If RISKY is nonzero, remove (disregard) any properties in any string
20432 we encounter, and ignore :eval and :propertize.
20434 The global variable `mode_line_target' determines whether the
20435 output is passed to `store_mode_line_noprop',
20436 `store_mode_line_string', or `display_string'. */
20438 static int
20439 display_mode_element (struct it *it, int depth, int field_width, int precision,
20440 Lisp_Object elt, Lisp_Object props, int risky)
20442 int n = 0, field, prec;
20443 int literal = 0;
20445 tail_recurse:
20446 if (depth > 100)
20447 elt = build_string ("*too-deep*");
20449 depth++;
20451 switch (XTYPE (elt))
20453 case Lisp_String:
20455 /* A string: output it and check for %-constructs within it. */
20456 unsigned char c;
20457 ptrdiff_t offset = 0;
20459 if (SCHARS (elt) > 0
20460 && (!NILP (props) || risky))
20462 Lisp_Object oprops, aelt;
20463 oprops = Ftext_properties_at (make_number (0), elt);
20465 /* If the starting string's properties are not what
20466 we want, translate the string. Also, if the string
20467 is risky, do that anyway. */
20469 if (NILP (Fequal (props, oprops)) || risky)
20471 /* If the starting string has properties,
20472 merge the specified ones onto the existing ones. */
20473 if (! NILP (oprops) && !risky)
20475 Lisp_Object tem;
20477 oprops = Fcopy_sequence (oprops);
20478 tem = props;
20479 while (CONSP (tem))
20481 oprops = Fplist_put (oprops, XCAR (tem),
20482 XCAR (XCDR (tem)));
20483 tem = XCDR (XCDR (tem));
20485 props = oprops;
20488 aelt = Fassoc (elt, mode_line_proptrans_alist);
20489 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20491 /* AELT is what we want. Move it to the front
20492 without consing. */
20493 elt = XCAR (aelt);
20494 mode_line_proptrans_alist
20495 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20497 else
20499 Lisp_Object tem;
20501 /* If AELT has the wrong props, it is useless.
20502 so get rid of it. */
20503 if (! NILP (aelt))
20504 mode_line_proptrans_alist
20505 = Fdelq (aelt, mode_line_proptrans_alist);
20507 elt = Fcopy_sequence (elt);
20508 Fset_text_properties (make_number (0), Flength (elt),
20509 props, elt);
20510 /* Add this item to mode_line_proptrans_alist. */
20511 mode_line_proptrans_alist
20512 = Fcons (Fcons (elt, props),
20513 mode_line_proptrans_alist);
20514 /* Truncate mode_line_proptrans_alist
20515 to at most 50 elements. */
20516 tem = Fnthcdr (make_number (50),
20517 mode_line_proptrans_alist);
20518 if (! NILP (tem))
20519 XSETCDR (tem, Qnil);
20524 offset = 0;
20526 if (literal)
20528 prec = precision - n;
20529 switch (mode_line_target)
20531 case MODE_LINE_NOPROP:
20532 case MODE_LINE_TITLE:
20533 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20534 break;
20535 case MODE_LINE_STRING:
20536 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20537 break;
20538 case MODE_LINE_DISPLAY:
20539 n += display_string (NULL, elt, Qnil, 0, 0, it,
20540 0, prec, 0, STRING_MULTIBYTE (elt));
20541 break;
20544 break;
20547 /* Handle the non-literal case. */
20549 while ((precision <= 0 || n < precision)
20550 && SREF (elt, offset) != 0
20551 && (mode_line_target != MODE_LINE_DISPLAY
20552 || it->current_x < it->last_visible_x))
20554 ptrdiff_t last_offset = offset;
20556 /* Advance to end of string or next format specifier. */
20557 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20560 if (offset - 1 != last_offset)
20562 ptrdiff_t nchars, nbytes;
20564 /* Output to end of string or up to '%'. Field width
20565 is length of string. Don't output more than
20566 PRECISION allows us. */
20567 offset--;
20569 prec = c_string_width (SDATA (elt) + last_offset,
20570 offset - last_offset, precision - n,
20571 &nchars, &nbytes);
20573 switch (mode_line_target)
20575 case MODE_LINE_NOPROP:
20576 case MODE_LINE_TITLE:
20577 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20578 break;
20579 case MODE_LINE_STRING:
20581 ptrdiff_t bytepos = last_offset;
20582 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20583 ptrdiff_t endpos = (precision <= 0
20584 ? string_byte_to_char (elt, offset)
20585 : charpos + nchars);
20587 n += store_mode_line_string (NULL,
20588 Fsubstring (elt, make_number (charpos),
20589 make_number (endpos)),
20590 0, 0, 0, Qnil);
20592 break;
20593 case MODE_LINE_DISPLAY:
20595 ptrdiff_t bytepos = last_offset;
20596 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20598 if (precision <= 0)
20599 nchars = string_byte_to_char (elt, offset) - charpos;
20600 n += display_string (NULL, elt, Qnil, 0, charpos,
20601 it, 0, nchars, 0,
20602 STRING_MULTIBYTE (elt));
20604 break;
20607 else /* c == '%' */
20609 ptrdiff_t percent_position = offset;
20611 /* Get the specified minimum width. Zero means
20612 don't pad. */
20613 field = 0;
20614 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20615 field = field * 10 + c - '0';
20617 /* Don't pad beyond the total padding allowed. */
20618 if (field_width - n > 0 && field > field_width - n)
20619 field = field_width - n;
20621 /* Note that either PRECISION <= 0 or N < PRECISION. */
20622 prec = precision - n;
20624 if (c == 'M')
20625 n += display_mode_element (it, depth, field, prec,
20626 Vglobal_mode_string, props,
20627 risky);
20628 else if (c != 0)
20630 int multibyte;
20631 ptrdiff_t bytepos, charpos;
20632 const char *spec;
20633 Lisp_Object string;
20635 bytepos = percent_position;
20636 charpos = (STRING_MULTIBYTE (elt)
20637 ? string_byte_to_char (elt, bytepos)
20638 : bytepos);
20639 spec = decode_mode_spec (it->w, c, field, &string);
20640 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20642 switch (mode_line_target)
20644 case MODE_LINE_NOPROP:
20645 case MODE_LINE_TITLE:
20646 n += store_mode_line_noprop (spec, field, prec);
20647 break;
20648 case MODE_LINE_STRING:
20650 Lisp_Object tem = build_string (spec);
20651 props = Ftext_properties_at (make_number (charpos), elt);
20652 /* Should only keep face property in props */
20653 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20655 break;
20656 case MODE_LINE_DISPLAY:
20658 int nglyphs_before, nwritten;
20660 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20661 nwritten = display_string (spec, string, elt,
20662 charpos, 0, it,
20663 field, prec, 0,
20664 multibyte);
20666 /* Assign to the glyphs written above the
20667 string where the `%x' came from, position
20668 of the `%'. */
20669 if (nwritten > 0)
20671 struct glyph *glyph
20672 = (it->glyph_row->glyphs[TEXT_AREA]
20673 + nglyphs_before);
20674 int i;
20676 for (i = 0; i < nwritten; ++i)
20678 glyph[i].object = elt;
20679 glyph[i].charpos = charpos;
20682 n += nwritten;
20685 break;
20688 else /* c == 0 */
20689 break;
20693 break;
20695 case Lisp_Symbol:
20696 /* A symbol: process the value of the symbol recursively
20697 as if it appeared here directly. Avoid error if symbol void.
20698 Special case: if value of symbol is a string, output the string
20699 literally. */
20701 register Lisp_Object tem;
20703 /* If the variable is not marked as risky to set
20704 then its contents are risky to use. */
20705 if (NILP (Fget (elt, Qrisky_local_variable)))
20706 risky = 1;
20708 tem = Fboundp (elt);
20709 if (!NILP (tem))
20711 tem = Fsymbol_value (elt);
20712 /* If value is a string, output that string literally:
20713 don't check for % within it. */
20714 if (STRINGP (tem))
20715 literal = 1;
20717 if (!EQ (tem, elt))
20719 /* Give up right away for nil or t. */
20720 elt = tem;
20721 goto tail_recurse;
20725 break;
20727 case Lisp_Cons:
20729 register Lisp_Object car, tem;
20731 /* A cons cell: five distinct cases.
20732 If first element is :eval or :propertize, do something special.
20733 If first element is a string or a cons, process all the elements
20734 and effectively concatenate them.
20735 If first element is a negative number, truncate displaying cdr to
20736 at most that many characters. If positive, pad (with spaces)
20737 to at least that many characters.
20738 If first element is a symbol, process the cadr or caddr recursively
20739 according to whether the symbol's value is non-nil or nil. */
20740 car = XCAR (elt);
20741 if (EQ (car, QCeval))
20743 /* An element of the form (:eval FORM) means evaluate FORM
20744 and use the result as mode line elements. */
20746 if (risky)
20747 break;
20749 if (CONSP (XCDR (elt)))
20751 Lisp_Object spec;
20752 spec = safe_eval (XCAR (XCDR (elt)));
20753 n += display_mode_element (it, depth, field_width - n,
20754 precision - n, spec, props,
20755 risky);
20758 else if (EQ (car, QCpropertize))
20760 /* An element of the form (:propertize ELT PROPS...)
20761 means display ELT but applying properties PROPS. */
20763 if (risky)
20764 break;
20766 if (CONSP (XCDR (elt)))
20767 n += display_mode_element (it, depth, field_width - n,
20768 precision - n, XCAR (XCDR (elt)),
20769 XCDR (XCDR (elt)), risky);
20771 else if (SYMBOLP (car))
20773 tem = Fboundp (car);
20774 elt = XCDR (elt);
20775 if (!CONSP (elt))
20776 goto invalid;
20777 /* elt is now the cdr, and we know it is a cons cell.
20778 Use its car if CAR has a non-nil value. */
20779 if (!NILP (tem))
20781 tem = Fsymbol_value (car);
20782 if (!NILP (tem))
20784 elt = XCAR (elt);
20785 goto tail_recurse;
20788 /* Symbol's value is nil (or symbol is unbound)
20789 Get the cddr of the original list
20790 and if possible find the caddr and use that. */
20791 elt = XCDR (elt);
20792 if (NILP (elt))
20793 break;
20794 else if (!CONSP (elt))
20795 goto invalid;
20796 elt = XCAR (elt);
20797 goto tail_recurse;
20799 else if (INTEGERP (car))
20801 register int lim = XINT (car);
20802 elt = XCDR (elt);
20803 if (lim < 0)
20805 /* Negative int means reduce maximum width. */
20806 if (precision <= 0)
20807 precision = -lim;
20808 else
20809 precision = min (precision, -lim);
20811 else if (lim > 0)
20813 /* Padding specified. Don't let it be more than
20814 current maximum. */
20815 if (precision > 0)
20816 lim = min (precision, lim);
20818 /* If that's more padding than already wanted, queue it.
20819 But don't reduce padding already specified even if
20820 that is beyond the current truncation point. */
20821 field_width = max (lim, field_width);
20823 goto tail_recurse;
20825 else if (STRINGP (car) || CONSP (car))
20827 Lisp_Object halftail = elt;
20828 int len = 0;
20830 while (CONSP (elt)
20831 && (precision <= 0 || n < precision))
20833 n += display_mode_element (it, depth,
20834 /* Do padding only after the last
20835 element in the list. */
20836 (! CONSP (XCDR (elt))
20837 ? field_width - n
20838 : 0),
20839 precision - n, XCAR (elt),
20840 props, risky);
20841 elt = XCDR (elt);
20842 len++;
20843 if ((len & 1) == 0)
20844 halftail = XCDR (halftail);
20845 /* Check for cycle. */
20846 if (EQ (halftail, elt))
20847 break;
20851 break;
20853 default:
20854 invalid:
20855 elt = build_string ("*invalid*");
20856 goto tail_recurse;
20859 /* Pad to FIELD_WIDTH. */
20860 if (field_width > 0 && n < field_width)
20862 switch (mode_line_target)
20864 case MODE_LINE_NOPROP:
20865 case MODE_LINE_TITLE:
20866 n += store_mode_line_noprop ("", field_width - n, 0);
20867 break;
20868 case MODE_LINE_STRING:
20869 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20870 break;
20871 case MODE_LINE_DISPLAY:
20872 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20873 0, 0, 0);
20874 break;
20878 return n;
20881 /* Store a mode-line string element in mode_line_string_list.
20883 If STRING is non-null, display that C string. Otherwise, the Lisp
20884 string LISP_STRING is displayed.
20886 FIELD_WIDTH is the minimum number of output glyphs to produce.
20887 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20888 with spaces. FIELD_WIDTH <= 0 means don't pad.
20890 PRECISION is the maximum number of characters to output from
20891 STRING. PRECISION <= 0 means don't truncate the string.
20893 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20894 properties to the string.
20896 PROPS are the properties to add to the string.
20897 The mode_line_string_face face property is always added to the string.
20900 static int
20901 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20902 int field_width, int precision, Lisp_Object props)
20904 ptrdiff_t len;
20905 int n = 0;
20907 if (string != NULL)
20909 len = strlen (string);
20910 if (precision > 0 && len > precision)
20911 len = precision;
20912 lisp_string = make_string (string, len);
20913 if (NILP (props))
20914 props = mode_line_string_face_prop;
20915 else if (!NILP (mode_line_string_face))
20917 Lisp_Object face = Fplist_get (props, Qface);
20918 props = Fcopy_sequence (props);
20919 if (NILP (face))
20920 face = mode_line_string_face;
20921 else
20922 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20923 props = Fplist_put (props, Qface, face);
20925 Fadd_text_properties (make_number (0), make_number (len),
20926 props, lisp_string);
20928 else
20930 len = XFASTINT (Flength (lisp_string));
20931 if (precision > 0 && len > precision)
20933 len = precision;
20934 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20935 precision = -1;
20937 if (!NILP (mode_line_string_face))
20939 Lisp_Object face;
20940 if (NILP (props))
20941 props = Ftext_properties_at (make_number (0), lisp_string);
20942 face = Fplist_get (props, Qface);
20943 if (NILP (face))
20944 face = mode_line_string_face;
20945 else
20946 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20947 props = Fcons (Qface, Fcons (face, Qnil));
20948 if (copy_string)
20949 lisp_string = Fcopy_sequence (lisp_string);
20951 if (!NILP (props))
20952 Fadd_text_properties (make_number (0), make_number (len),
20953 props, lisp_string);
20956 if (len > 0)
20958 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20959 n += len;
20962 if (field_width > len)
20964 field_width -= len;
20965 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20966 if (!NILP (props))
20967 Fadd_text_properties (make_number (0), make_number (field_width),
20968 props, lisp_string);
20969 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20970 n += field_width;
20973 return n;
20977 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20978 1, 4, 0,
20979 doc: /* Format a string out of a mode line format specification.
20980 First arg FORMAT specifies the mode line format (see `mode-line-format'
20981 for details) to use.
20983 By default, the format is evaluated for the currently selected window.
20985 Optional second arg FACE specifies the face property to put on all
20986 characters for which no face is specified. The value nil means the
20987 default face. The value t means whatever face the window's mode line
20988 currently uses (either `mode-line' or `mode-line-inactive',
20989 depending on whether the window is the selected window or not).
20990 An integer value means the value string has no text
20991 properties.
20993 Optional third and fourth args WINDOW and BUFFER specify the window
20994 and buffer to use as the context for the formatting (defaults
20995 are the selected window and the WINDOW's buffer). */)
20996 (Lisp_Object format, Lisp_Object face,
20997 Lisp_Object window, Lisp_Object buffer)
20999 struct it it;
21000 int len;
21001 struct window *w;
21002 struct buffer *old_buffer = NULL;
21003 int face_id;
21004 int no_props = INTEGERP (face);
21005 ptrdiff_t count = SPECPDL_INDEX ();
21006 Lisp_Object str;
21007 int string_start = 0;
21009 if (NILP (window))
21010 window = selected_window;
21011 CHECK_WINDOW (window);
21012 w = XWINDOW (window);
21014 if (NILP (buffer))
21015 buffer = w->buffer;
21016 CHECK_BUFFER (buffer);
21018 /* Make formatting the modeline a non-op when noninteractive, otherwise
21019 there will be problems later caused by a partially initialized frame. */
21020 if (NILP (format) || noninteractive)
21021 return empty_unibyte_string;
21023 if (no_props)
21024 face = Qnil;
21026 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21027 : EQ (face, Qt) ? (EQ (window, selected_window)
21028 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21029 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21030 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21031 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21032 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21033 : DEFAULT_FACE_ID;
21035 old_buffer = current_buffer;
21037 /* Save things including mode_line_proptrans_alist,
21038 and set that to nil so that we don't alter the outer value. */
21039 record_unwind_protect (unwind_format_mode_line,
21040 format_mode_line_unwind_data
21041 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21042 old_buffer, selected_window, 1));
21043 mode_line_proptrans_alist = Qnil;
21045 Fselect_window (window, Qt);
21046 set_buffer_internal_1 (XBUFFER (buffer));
21048 init_iterator (&it, w, -1, -1, NULL, face_id);
21050 if (no_props)
21052 mode_line_target = MODE_LINE_NOPROP;
21053 mode_line_string_face_prop = Qnil;
21054 mode_line_string_list = Qnil;
21055 string_start = MODE_LINE_NOPROP_LEN (0);
21057 else
21059 mode_line_target = MODE_LINE_STRING;
21060 mode_line_string_list = Qnil;
21061 mode_line_string_face = face;
21062 mode_line_string_face_prop
21063 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21066 push_kboard (FRAME_KBOARD (it.f));
21067 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21068 pop_kboard ();
21070 if (no_props)
21072 len = MODE_LINE_NOPROP_LEN (string_start);
21073 str = make_string (mode_line_noprop_buf + string_start, len);
21075 else
21077 mode_line_string_list = Fnreverse (mode_line_string_list);
21078 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21079 empty_unibyte_string);
21082 unbind_to (count, Qnil);
21083 return str;
21086 /* Write a null-terminated, right justified decimal representation of
21087 the positive integer D to BUF using a minimal field width WIDTH. */
21089 static void
21090 pint2str (register char *buf, register int width, register ptrdiff_t d)
21092 register char *p = buf;
21094 if (d <= 0)
21095 *p++ = '0';
21096 else
21098 while (d > 0)
21100 *p++ = d % 10 + '0';
21101 d /= 10;
21105 for (width -= (int) (p - buf); width > 0; --width)
21106 *p++ = ' ';
21107 *p-- = '\0';
21108 while (p > buf)
21110 d = *buf;
21111 *buf++ = *p;
21112 *p-- = d;
21116 /* Write a null-terminated, right justified decimal and "human
21117 readable" representation of the nonnegative integer D to BUF using
21118 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21120 static const char power_letter[] =
21122 0, /* no letter */
21123 'k', /* kilo */
21124 'M', /* mega */
21125 'G', /* giga */
21126 'T', /* tera */
21127 'P', /* peta */
21128 'E', /* exa */
21129 'Z', /* zetta */
21130 'Y' /* yotta */
21133 static void
21134 pint2hrstr (char *buf, int width, ptrdiff_t d)
21136 /* We aim to represent the nonnegative integer D as
21137 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21138 ptrdiff_t quotient = d;
21139 int remainder = 0;
21140 /* -1 means: do not use TENTHS. */
21141 int tenths = -1;
21142 int exponent = 0;
21144 /* Length of QUOTIENT.TENTHS as a string. */
21145 int length;
21147 char * psuffix;
21148 char * p;
21150 if (1000 <= quotient)
21152 /* Scale to the appropriate EXPONENT. */
21155 remainder = quotient % 1000;
21156 quotient /= 1000;
21157 exponent++;
21159 while (1000 <= quotient);
21161 /* Round to nearest and decide whether to use TENTHS or not. */
21162 if (quotient <= 9)
21164 tenths = remainder / 100;
21165 if (50 <= remainder % 100)
21167 if (tenths < 9)
21168 tenths++;
21169 else
21171 quotient++;
21172 if (quotient == 10)
21173 tenths = -1;
21174 else
21175 tenths = 0;
21179 else
21180 if (500 <= remainder)
21182 if (quotient < 999)
21183 quotient++;
21184 else
21186 quotient = 1;
21187 exponent++;
21188 tenths = 0;
21193 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21194 if (tenths == -1 && quotient <= 99)
21195 if (quotient <= 9)
21196 length = 1;
21197 else
21198 length = 2;
21199 else
21200 length = 3;
21201 p = psuffix = buf + max (width, length);
21203 /* Print EXPONENT. */
21204 *psuffix++ = power_letter[exponent];
21205 *psuffix = '\0';
21207 /* Print TENTHS. */
21208 if (tenths >= 0)
21210 *--p = '0' + tenths;
21211 *--p = '.';
21214 /* Print QUOTIENT. */
21217 int digit = quotient % 10;
21218 *--p = '0' + digit;
21220 while ((quotient /= 10) != 0);
21222 /* Print leading spaces. */
21223 while (buf < p)
21224 *--p = ' ';
21227 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21228 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21229 type of CODING_SYSTEM. Return updated pointer into BUF. */
21231 static unsigned char invalid_eol_type[] = "(*invalid*)";
21233 static char *
21234 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21236 Lisp_Object val;
21237 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21238 const unsigned char *eol_str;
21239 int eol_str_len;
21240 /* The EOL conversion we are using. */
21241 Lisp_Object eoltype;
21243 val = CODING_SYSTEM_SPEC (coding_system);
21244 eoltype = Qnil;
21246 if (!VECTORP (val)) /* Not yet decided. */
21248 *buf++ = multibyte ? '-' : ' ';
21249 if (eol_flag)
21250 eoltype = eol_mnemonic_undecided;
21251 /* Don't mention EOL conversion if it isn't decided. */
21253 else
21255 Lisp_Object attrs;
21256 Lisp_Object eolvalue;
21258 attrs = AREF (val, 0);
21259 eolvalue = AREF (val, 2);
21261 *buf++ = multibyte
21262 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21263 : ' ';
21265 if (eol_flag)
21267 /* The EOL conversion that is normal on this system. */
21269 if (NILP (eolvalue)) /* Not yet decided. */
21270 eoltype = eol_mnemonic_undecided;
21271 else if (VECTORP (eolvalue)) /* Not yet decided. */
21272 eoltype = eol_mnemonic_undecided;
21273 else /* eolvalue is Qunix, Qdos, or Qmac. */
21274 eoltype = (EQ (eolvalue, Qunix)
21275 ? eol_mnemonic_unix
21276 : (EQ (eolvalue, Qdos) == 1
21277 ? eol_mnemonic_dos : eol_mnemonic_mac));
21281 if (eol_flag)
21283 /* Mention the EOL conversion if it is not the usual one. */
21284 if (STRINGP (eoltype))
21286 eol_str = SDATA (eoltype);
21287 eol_str_len = SBYTES (eoltype);
21289 else if (CHARACTERP (eoltype))
21291 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21292 int c = XFASTINT (eoltype);
21293 eol_str_len = CHAR_STRING (c, tmp);
21294 eol_str = tmp;
21296 else
21298 eol_str = invalid_eol_type;
21299 eol_str_len = sizeof (invalid_eol_type) - 1;
21301 memcpy (buf, eol_str, eol_str_len);
21302 buf += eol_str_len;
21305 return buf;
21308 /* Return a string for the output of a mode line %-spec for window W,
21309 generated by character C. FIELD_WIDTH > 0 means pad the string
21310 returned with spaces to that value. Return a Lisp string in
21311 *STRING if the resulting string is taken from that Lisp string.
21313 Note we operate on the current buffer for most purposes,
21314 the exception being w->base_line_pos. */
21316 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21318 static const char *
21319 decode_mode_spec (struct window *w, register int c, int field_width,
21320 Lisp_Object *string)
21322 Lisp_Object obj;
21323 struct frame *f = XFRAME (WINDOW_FRAME (w));
21324 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21325 struct buffer *b = current_buffer;
21327 obj = Qnil;
21328 *string = Qnil;
21330 switch (c)
21332 case '*':
21333 if (!NILP (BVAR (b, read_only)))
21334 return "%";
21335 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21336 return "*";
21337 return "-";
21339 case '+':
21340 /* This differs from %* only for a modified read-only buffer. */
21341 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21342 return "*";
21343 if (!NILP (BVAR (b, read_only)))
21344 return "%";
21345 return "-";
21347 case '&':
21348 /* This differs from %* in ignoring read-only-ness. */
21349 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21350 return "*";
21351 return "-";
21353 case '%':
21354 return "%";
21356 case '[':
21358 int i;
21359 char *p;
21361 if (command_loop_level > 5)
21362 return "[[[... ";
21363 p = decode_mode_spec_buf;
21364 for (i = 0; i < command_loop_level; i++)
21365 *p++ = '[';
21366 *p = 0;
21367 return decode_mode_spec_buf;
21370 case ']':
21372 int i;
21373 char *p;
21375 if (command_loop_level > 5)
21376 return " ...]]]";
21377 p = decode_mode_spec_buf;
21378 for (i = 0; i < command_loop_level; i++)
21379 *p++ = ']';
21380 *p = 0;
21381 return decode_mode_spec_buf;
21384 case '-':
21386 register int i;
21388 /* Let lots_of_dashes be a string of infinite length. */
21389 if (mode_line_target == MODE_LINE_NOPROP ||
21390 mode_line_target == MODE_LINE_STRING)
21391 return "--";
21392 if (field_width <= 0
21393 || field_width > sizeof (lots_of_dashes))
21395 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21396 decode_mode_spec_buf[i] = '-';
21397 decode_mode_spec_buf[i] = '\0';
21398 return decode_mode_spec_buf;
21400 else
21401 return lots_of_dashes;
21404 case 'b':
21405 obj = BVAR (b, name);
21406 break;
21408 case 'c':
21409 /* %c and %l are ignored in `frame-title-format'.
21410 (In redisplay_internal, the frame title is drawn _before_ the
21411 windows are updated, so the stuff which depends on actual
21412 window contents (such as %l) may fail to render properly, or
21413 even crash emacs.) */
21414 if (mode_line_target == MODE_LINE_TITLE)
21415 return "";
21416 else
21418 ptrdiff_t col = current_column ();
21419 wset_column_number_displayed (w, make_number (col));
21420 pint2str (decode_mode_spec_buf, field_width, col);
21421 return decode_mode_spec_buf;
21424 case 'e':
21425 #ifndef SYSTEM_MALLOC
21427 if (NILP (Vmemory_full))
21428 return "";
21429 else
21430 return "!MEM FULL! ";
21432 #else
21433 return "";
21434 #endif
21436 case 'F':
21437 /* %F displays the frame name. */
21438 if (!NILP (f->title))
21439 return SSDATA (f->title);
21440 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21441 return SSDATA (f->name);
21442 return "Emacs";
21444 case 'f':
21445 obj = BVAR (b, filename);
21446 break;
21448 case 'i':
21450 ptrdiff_t size = ZV - BEGV;
21451 pint2str (decode_mode_spec_buf, field_width, size);
21452 return decode_mode_spec_buf;
21455 case 'I':
21457 ptrdiff_t size = ZV - BEGV;
21458 pint2hrstr (decode_mode_spec_buf, field_width, size);
21459 return decode_mode_spec_buf;
21462 case 'l':
21464 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21465 ptrdiff_t topline, nlines, height;
21466 ptrdiff_t junk;
21468 /* %c and %l are ignored in `frame-title-format'. */
21469 if (mode_line_target == MODE_LINE_TITLE)
21470 return "";
21472 startpos = XMARKER (w->start)->charpos;
21473 startpos_byte = marker_byte_position (w->start);
21474 height = WINDOW_TOTAL_LINES (w);
21476 /* If we decided that this buffer isn't suitable for line numbers,
21477 don't forget that too fast. */
21478 if (EQ (w->base_line_pos, w->buffer))
21479 goto no_value;
21480 /* But do forget it, if the window shows a different buffer now. */
21481 else if (BUFFERP (w->base_line_pos))
21482 wset_base_line_pos (w, Qnil);
21484 /* If the buffer is very big, don't waste time. */
21485 if (INTEGERP (Vline_number_display_limit)
21486 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21488 wset_base_line_pos (w, Qnil);
21489 wset_base_line_number (w, Qnil);
21490 goto no_value;
21493 if (INTEGERP (w->base_line_number)
21494 && INTEGERP (w->base_line_pos)
21495 && XFASTINT (w->base_line_pos) <= startpos)
21497 line = XFASTINT (w->base_line_number);
21498 linepos = XFASTINT (w->base_line_pos);
21499 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21501 else
21503 line = 1;
21504 linepos = BUF_BEGV (b);
21505 linepos_byte = BUF_BEGV_BYTE (b);
21508 /* Count lines from base line to window start position. */
21509 nlines = display_count_lines (linepos_byte,
21510 startpos_byte,
21511 startpos, &junk);
21513 topline = nlines + line;
21515 /* Determine a new base line, if the old one is too close
21516 or too far away, or if we did not have one.
21517 "Too close" means it's plausible a scroll-down would
21518 go back past it. */
21519 if (startpos == BUF_BEGV (b))
21521 wset_base_line_number (w, make_number (topline));
21522 wset_base_line_pos (w, make_number (BUF_BEGV (b)));
21524 else if (nlines < height + 25 || nlines > height * 3 + 50
21525 || linepos == BUF_BEGV (b))
21527 ptrdiff_t limit = BUF_BEGV (b);
21528 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21529 ptrdiff_t position;
21530 ptrdiff_t distance =
21531 (height * 2 + 30) * line_number_display_limit_width;
21533 if (startpos - distance > limit)
21535 limit = startpos - distance;
21536 limit_byte = CHAR_TO_BYTE (limit);
21539 nlines = display_count_lines (startpos_byte,
21540 limit_byte,
21541 - (height * 2 + 30),
21542 &position);
21543 /* If we couldn't find the lines we wanted within
21544 line_number_display_limit_width chars per line,
21545 give up on line numbers for this window. */
21546 if (position == limit_byte && limit == startpos - distance)
21548 wset_base_line_pos (w, w->buffer);
21549 wset_base_line_number (w, Qnil);
21550 goto no_value;
21553 wset_base_line_number (w, make_number (topline - nlines));
21554 wset_base_line_pos (w, make_number (BYTE_TO_CHAR (position)));
21557 /* Now count lines from the start pos to point. */
21558 nlines = display_count_lines (startpos_byte,
21559 PT_BYTE, PT, &junk);
21561 /* Record that we did display the line number. */
21562 line_number_displayed = 1;
21564 /* Make the string to show. */
21565 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21566 return decode_mode_spec_buf;
21567 no_value:
21569 char* p = decode_mode_spec_buf;
21570 int pad = field_width - 2;
21571 while (pad-- > 0)
21572 *p++ = ' ';
21573 *p++ = '?';
21574 *p++ = '?';
21575 *p = '\0';
21576 return decode_mode_spec_buf;
21579 break;
21581 case 'm':
21582 obj = BVAR (b, mode_name);
21583 break;
21585 case 'n':
21586 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21587 return " Narrow";
21588 break;
21590 case 'p':
21592 ptrdiff_t pos = marker_position (w->start);
21593 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21595 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21597 if (pos <= BUF_BEGV (b))
21598 return "All";
21599 else
21600 return "Bottom";
21602 else if (pos <= BUF_BEGV (b))
21603 return "Top";
21604 else
21606 if (total > 1000000)
21607 /* Do it differently for a large value, to avoid overflow. */
21608 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21609 else
21610 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21611 /* We can't normally display a 3-digit number,
21612 so get us a 2-digit number that is close. */
21613 if (total == 100)
21614 total = 99;
21615 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21616 return decode_mode_spec_buf;
21620 /* Display percentage of size above the bottom of the screen. */
21621 case 'P':
21623 ptrdiff_t toppos = marker_position (w->start);
21624 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21625 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21627 if (botpos >= BUF_ZV (b))
21629 if (toppos <= BUF_BEGV (b))
21630 return "All";
21631 else
21632 return "Bottom";
21634 else
21636 if (total > 1000000)
21637 /* Do it differently for a large value, to avoid overflow. */
21638 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21639 else
21640 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21641 /* We can't normally display a 3-digit number,
21642 so get us a 2-digit number that is close. */
21643 if (total == 100)
21644 total = 99;
21645 if (toppos <= BUF_BEGV (b))
21646 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21647 else
21648 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21649 return decode_mode_spec_buf;
21653 case 's':
21654 /* status of process */
21655 obj = Fget_buffer_process (Fcurrent_buffer ());
21656 if (NILP (obj))
21657 return "no process";
21658 #ifndef MSDOS
21659 obj = Fsymbol_name (Fprocess_status (obj));
21660 #endif
21661 break;
21663 case '@':
21665 ptrdiff_t count = inhibit_garbage_collection ();
21666 Lisp_Object val = call1 (intern ("file-remote-p"),
21667 BVAR (current_buffer, directory));
21668 unbind_to (count, Qnil);
21670 if (NILP (val))
21671 return "-";
21672 else
21673 return "@";
21676 case 't': /* indicate TEXT or BINARY */
21677 return "T";
21679 case 'z':
21680 /* coding-system (not including end-of-line format) */
21681 case 'Z':
21682 /* coding-system (including end-of-line type) */
21684 int eol_flag = (c == 'Z');
21685 char *p = decode_mode_spec_buf;
21687 if (! FRAME_WINDOW_P (f))
21689 /* No need to mention EOL here--the terminal never needs
21690 to do EOL conversion. */
21691 p = decode_mode_spec_coding (CODING_ID_NAME
21692 (FRAME_KEYBOARD_CODING (f)->id),
21693 p, 0);
21694 p = decode_mode_spec_coding (CODING_ID_NAME
21695 (FRAME_TERMINAL_CODING (f)->id),
21696 p, 0);
21698 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21699 p, eol_flag);
21701 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21702 #ifdef subprocesses
21703 obj = Fget_buffer_process (Fcurrent_buffer ());
21704 if (PROCESSP (obj))
21706 p = decode_mode_spec_coding
21707 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21708 p = decode_mode_spec_coding
21709 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21711 #endif /* subprocesses */
21712 #endif /* 0 */
21713 *p = 0;
21714 return decode_mode_spec_buf;
21718 if (STRINGP (obj))
21720 *string = obj;
21721 return SSDATA (obj);
21723 else
21724 return "";
21728 /* Count up to COUNT lines starting from START_BYTE.
21729 But don't go beyond LIMIT_BYTE.
21730 Return the number of lines thus found (always nonnegative).
21732 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21734 static ptrdiff_t
21735 display_count_lines (ptrdiff_t start_byte,
21736 ptrdiff_t limit_byte, ptrdiff_t count,
21737 ptrdiff_t *byte_pos_ptr)
21739 register unsigned char *cursor;
21740 unsigned char *base;
21742 register ptrdiff_t ceiling;
21743 register unsigned char *ceiling_addr;
21744 ptrdiff_t orig_count = count;
21746 /* If we are not in selective display mode,
21747 check only for newlines. */
21748 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21749 && !INTEGERP (BVAR (current_buffer, selective_display)));
21751 if (count > 0)
21753 while (start_byte < limit_byte)
21755 ceiling = BUFFER_CEILING_OF (start_byte);
21756 ceiling = min (limit_byte - 1, ceiling);
21757 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21758 base = (cursor = BYTE_POS_ADDR (start_byte));
21759 while (1)
21761 if (selective_display)
21762 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21764 else
21765 while (*cursor != '\n' && ++cursor != ceiling_addr)
21768 if (cursor != ceiling_addr)
21770 if (--count == 0)
21772 start_byte += cursor - base + 1;
21773 *byte_pos_ptr = start_byte;
21774 return orig_count;
21776 else
21777 if (++cursor == ceiling_addr)
21778 break;
21780 else
21781 break;
21783 start_byte += cursor - base;
21786 else
21788 while (start_byte > limit_byte)
21790 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21791 ceiling = max (limit_byte, ceiling);
21792 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21793 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21794 while (1)
21796 if (selective_display)
21797 while (--cursor != ceiling_addr
21798 && *cursor != '\n' && *cursor != 015)
21800 else
21801 while (--cursor != ceiling_addr && *cursor != '\n')
21804 if (cursor != ceiling_addr)
21806 if (++count == 0)
21808 start_byte += cursor - base + 1;
21809 *byte_pos_ptr = start_byte;
21810 /* When scanning backwards, we should
21811 not count the newline posterior to which we stop. */
21812 return - orig_count - 1;
21815 else
21816 break;
21818 /* Here we add 1 to compensate for the last decrement
21819 of CURSOR, which took it past the valid range. */
21820 start_byte += cursor - base + 1;
21824 *byte_pos_ptr = limit_byte;
21826 if (count < 0)
21827 return - orig_count + count;
21828 return orig_count - count;
21834 /***********************************************************************
21835 Displaying strings
21836 ***********************************************************************/
21838 /* Display a NUL-terminated string, starting with index START.
21840 If STRING is non-null, display that C string. Otherwise, the Lisp
21841 string LISP_STRING is displayed. There's a case that STRING is
21842 non-null and LISP_STRING is not nil. It means STRING is a string
21843 data of LISP_STRING. In that case, we display LISP_STRING while
21844 ignoring its text properties.
21846 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21847 FACE_STRING. Display STRING or LISP_STRING with the face at
21848 FACE_STRING_POS in FACE_STRING:
21850 Display the string in the environment given by IT, but use the
21851 standard display table, temporarily.
21853 FIELD_WIDTH is the minimum number of output glyphs to produce.
21854 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21855 with spaces. If STRING has more characters, more than FIELD_WIDTH
21856 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21858 PRECISION is the maximum number of characters to output from
21859 STRING. PRECISION < 0 means don't truncate the string.
21861 This is roughly equivalent to printf format specifiers:
21863 FIELD_WIDTH PRECISION PRINTF
21864 ----------------------------------------
21865 -1 -1 %s
21866 -1 10 %.10s
21867 10 -1 %10s
21868 20 10 %20.10s
21870 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21871 display them, and < 0 means obey the current buffer's value of
21872 enable_multibyte_characters.
21874 Value is the number of columns displayed. */
21876 static int
21877 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21878 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21879 int field_width, int precision, int max_x, int multibyte)
21881 int hpos_at_start = it->hpos;
21882 int saved_face_id = it->face_id;
21883 struct glyph_row *row = it->glyph_row;
21884 ptrdiff_t it_charpos;
21886 /* Initialize the iterator IT for iteration over STRING beginning
21887 with index START. */
21888 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21889 precision, field_width, multibyte);
21890 if (string && STRINGP (lisp_string))
21891 /* LISP_STRING is the one returned by decode_mode_spec. We should
21892 ignore its text properties. */
21893 it->stop_charpos = it->end_charpos;
21895 /* If displaying STRING, set up the face of the iterator from
21896 FACE_STRING, if that's given. */
21897 if (STRINGP (face_string))
21899 ptrdiff_t endptr;
21900 struct face *face;
21902 it->face_id
21903 = face_at_string_position (it->w, face_string, face_string_pos,
21904 0, it->region_beg_charpos,
21905 it->region_end_charpos,
21906 &endptr, it->base_face_id, 0);
21907 face = FACE_FROM_ID (it->f, it->face_id);
21908 it->face_box_p = face->box != FACE_NO_BOX;
21911 /* Set max_x to the maximum allowed X position. Don't let it go
21912 beyond the right edge of the window. */
21913 if (max_x <= 0)
21914 max_x = it->last_visible_x;
21915 else
21916 max_x = min (max_x, it->last_visible_x);
21918 /* Skip over display elements that are not visible. because IT->w is
21919 hscrolled. */
21920 if (it->current_x < it->first_visible_x)
21921 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21922 MOVE_TO_POS | MOVE_TO_X);
21924 row->ascent = it->max_ascent;
21925 row->height = it->max_ascent + it->max_descent;
21926 row->phys_ascent = it->max_phys_ascent;
21927 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21928 row->extra_line_spacing = it->max_extra_line_spacing;
21930 if (STRINGP (it->string))
21931 it_charpos = IT_STRING_CHARPOS (*it);
21932 else
21933 it_charpos = IT_CHARPOS (*it);
21935 /* This condition is for the case that we are called with current_x
21936 past last_visible_x. */
21937 while (it->current_x < max_x)
21939 int x_before, x, n_glyphs_before, i, nglyphs;
21941 /* Get the next display element. */
21942 if (!get_next_display_element (it))
21943 break;
21945 /* Produce glyphs. */
21946 x_before = it->current_x;
21947 n_glyphs_before = row->used[TEXT_AREA];
21948 PRODUCE_GLYPHS (it);
21950 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21951 i = 0;
21952 x = x_before;
21953 while (i < nglyphs)
21955 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21957 if (it->line_wrap != TRUNCATE
21958 && x + glyph->pixel_width > max_x)
21960 /* End of continued line or max_x reached. */
21961 if (CHAR_GLYPH_PADDING_P (*glyph))
21963 /* A wide character is unbreakable. */
21964 if (row->reversed_p)
21965 unproduce_glyphs (it, row->used[TEXT_AREA]
21966 - n_glyphs_before);
21967 row->used[TEXT_AREA] = n_glyphs_before;
21968 it->current_x = x_before;
21970 else
21972 if (row->reversed_p)
21973 unproduce_glyphs (it, row->used[TEXT_AREA]
21974 - (n_glyphs_before + i));
21975 row->used[TEXT_AREA] = n_glyphs_before + i;
21976 it->current_x = x;
21978 break;
21980 else if (x + glyph->pixel_width >= it->first_visible_x)
21982 /* Glyph is at least partially visible. */
21983 ++it->hpos;
21984 if (x < it->first_visible_x)
21985 row->x = x - it->first_visible_x;
21987 else
21989 /* Glyph is off the left margin of the display area.
21990 Should not happen. */
21991 emacs_abort ();
21994 row->ascent = max (row->ascent, it->max_ascent);
21995 row->height = max (row->height, it->max_ascent + it->max_descent);
21996 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21997 row->phys_height = max (row->phys_height,
21998 it->max_phys_ascent + it->max_phys_descent);
21999 row->extra_line_spacing = max (row->extra_line_spacing,
22000 it->max_extra_line_spacing);
22001 x += glyph->pixel_width;
22002 ++i;
22005 /* Stop if max_x reached. */
22006 if (i < nglyphs)
22007 break;
22009 /* Stop at line ends. */
22010 if (ITERATOR_AT_END_OF_LINE_P (it))
22012 it->continuation_lines_width = 0;
22013 break;
22016 set_iterator_to_next (it, 1);
22017 if (STRINGP (it->string))
22018 it_charpos = IT_STRING_CHARPOS (*it);
22019 else
22020 it_charpos = IT_CHARPOS (*it);
22022 /* Stop if truncating at the right edge. */
22023 if (it->line_wrap == TRUNCATE
22024 && it->current_x >= it->last_visible_x)
22026 /* Add truncation mark, but don't do it if the line is
22027 truncated at a padding space. */
22028 if (it_charpos < it->string_nchars)
22030 if (!FRAME_WINDOW_P (it->f))
22032 int ii, n;
22034 if (it->current_x > it->last_visible_x)
22036 if (!row->reversed_p)
22038 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22039 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22040 break;
22042 else
22044 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22045 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22046 break;
22047 unproduce_glyphs (it, ii + 1);
22048 ii = row->used[TEXT_AREA] - (ii + 1);
22050 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22052 row->used[TEXT_AREA] = ii;
22053 produce_special_glyphs (it, IT_TRUNCATION);
22056 produce_special_glyphs (it, IT_TRUNCATION);
22058 row->truncated_on_right_p = 1;
22060 break;
22064 /* Maybe insert a truncation at the left. */
22065 if (it->first_visible_x
22066 && it_charpos > 0)
22068 if (!FRAME_WINDOW_P (it->f)
22069 || (row->reversed_p
22070 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22071 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22072 insert_left_trunc_glyphs (it);
22073 row->truncated_on_left_p = 1;
22076 it->face_id = saved_face_id;
22078 /* Value is number of columns displayed. */
22079 return it->hpos - hpos_at_start;
22084 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22085 appears as an element of LIST or as the car of an element of LIST.
22086 If PROPVAL is a list, compare each element against LIST in that
22087 way, and return 1/2 if any element of PROPVAL is found in LIST.
22088 Otherwise return 0. This function cannot quit.
22089 The return value is 2 if the text is invisible but with an ellipsis
22090 and 1 if it's invisible and without an ellipsis. */
22093 invisible_p (register Lisp_Object propval, Lisp_Object list)
22095 register Lisp_Object tail, proptail;
22097 for (tail = list; CONSP (tail); tail = XCDR (tail))
22099 register Lisp_Object tem;
22100 tem = XCAR (tail);
22101 if (EQ (propval, tem))
22102 return 1;
22103 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22104 return NILP (XCDR (tem)) ? 1 : 2;
22107 if (CONSP (propval))
22109 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22111 Lisp_Object propelt;
22112 propelt = XCAR (proptail);
22113 for (tail = list; CONSP (tail); tail = XCDR (tail))
22115 register Lisp_Object tem;
22116 tem = XCAR (tail);
22117 if (EQ (propelt, tem))
22118 return 1;
22119 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22120 return NILP (XCDR (tem)) ? 1 : 2;
22125 return 0;
22128 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22129 doc: /* Non-nil if the property makes the text invisible.
22130 POS-OR-PROP can be a marker or number, in which case it is taken to be
22131 a position in the current buffer and the value of the `invisible' property
22132 is checked; or it can be some other value, which is then presumed to be the
22133 value of the `invisible' property of the text of interest.
22134 The non-nil value returned can be t for truly invisible text or something
22135 else if the text is replaced by an ellipsis. */)
22136 (Lisp_Object pos_or_prop)
22138 Lisp_Object prop
22139 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22140 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22141 : pos_or_prop);
22142 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22143 return (invis == 0 ? Qnil
22144 : invis == 1 ? Qt
22145 : make_number (invis));
22148 /* Calculate a width or height in pixels from a specification using
22149 the following elements:
22151 SPEC ::=
22152 NUM - a (fractional) multiple of the default font width/height
22153 (NUM) - specifies exactly NUM pixels
22154 UNIT - a fixed number of pixels, see below.
22155 ELEMENT - size of a display element in pixels, see below.
22156 (NUM . SPEC) - equals NUM * SPEC
22157 (+ SPEC SPEC ...) - add pixel values
22158 (- SPEC SPEC ...) - subtract pixel values
22159 (- SPEC) - negate pixel value
22161 NUM ::=
22162 INT or FLOAT - a number constant
22163 SYMBOL - use symbol's (buffer local) variable binding.
22165 UNIT ::=
22166 in - pixels per inch *)
22167 mm - pixels per 1/1000 meter *)
22168 cm - pixels per 1/100 meter *)
22169 width - width of current font in pixels.
22170 height - height of current font in pixels.
22172 *) using the ratio(s) defined in display-pixels-per-inch.
22174 ELEMENT ::=
22176 left-fringe - left fringe width in pixels
22177 right-fringe - right fringe width in pixels
22179 left-margin - left margin width in pixels
22180 right-margin - right margin width in pixels
22182 scroll-bar - scroll-bar area width in pixels
22184 Examples:
22186 Pixels corresponding to 5 inches:
22187 (5 . in)
22189 Total width of non-text areas on left side of window (if scroll-bar is on left):
22190 '(space :width (+ left-fringe left-margin scroll-bar))
22192 Align to first text column (in header line):
22193 '(space :align-to 0)
22195 Align to middle of text area minus half the width of variable `my-image'
22196 containing a loaded image:
22197 '(space :align-to (0.5 . (- text my-image)))
22199 Width of left margin minus width of 1 character in the default font:
22200 '(space :width (- left-margin 1))
22202 Width of left margin minus width of 2 characters in the current font:
22203 '(space :width (- left-margin (2 . width)))
22205 Center 1 character over left-margin (in header line):
22206 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22208 Different ways to express width of left fringe plus left margin minus one pixel:
22209 '(space :width (- (+ left-fringe left-margin) (1)))
22210 '(space :width (+ left-fringe left-margin (- (1))))
22211 '(space :width (+ left-fringe left-margin (-1)))
22215 #define NUMVAL(X) \
22216 ((INTEGERP (X) || FLOATP (X)) \
22217 ? XFLOATINT (X) \
22218 : - 1)
22220 static int
22221 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22222 struct font *font, int width_p, int *align_to)
22224 double pixels;
22226 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22227 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22229 if (NILP (prop))
22230 return OK_PIXELS (0);
22232 eassert (FRAME_LIVE_P (it->f));
22234 if (SYMBOLP (prop))
22236 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22238 char *unit = SSDATA (SYMBOL_NAME (prop));
22240 if (unit[0] == 'i' && unit[1] == 'n')
22241 pixels = 1.0;
22242 else if (unit[0] == 'm' && unit[1] == 'm')
22243 pixels = 25.4;
22244 else if (unit[0] == 'c' && unit[1] == 'm')
22245 pixels = 2.54;
22246 else
22247 pixels = 0;
22248 if (pixels > 0)
22250 double ppi;
22251 #ifdef HAVE_WINDOW_SYSTEM
22252 if (FRAME_WINDOW_P (it->f)
22253 && (ppi = (width_p
22254 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22255 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22256 ppi > 0))
22257 return OK_PIXELS (ppi / pixels);
22258 #endif
22260 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22261 || (CONSP (Vdisplay_pixels_per_inch)
22262 && (ppi = (width_p
22263 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22264 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22265 ppi > 0)))
22266 return OK_PIXELS (ppi / pixels);
22268 return 0;
22272 #ifdef HAVE_WINDOW_SYSTEM
22273 if (EQ (prop, Qheight))
22274 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22275 if (EQ (prop, Qwidth))
22276 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22277 #else
22278 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22279 return OK_PIXELS (1);
22280 #endif
22282 if (EQ (prop, Qtext))
22283 return OK_PIXELS (width_p
22284 ? window_box_width (it->w, TEXT_AREA)
22285 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22287 if (align_to && *align_to < 0)
22289 *res = 0;
22290 if (EQ (prop, Qleft))
22291 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22292 if (EQ (prop, Qright))
22293 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22294 if (EQ (prop, Qcenter))
22295 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22296 + window_box_width (it->w, TEXT_AREA) / 2);
22297 if (EQ (prop, Qleft_fringe))
22298 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22299 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22300 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22301 if (EQ (prop, Qright_fringe))
22302 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22303 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22304 : window_box_right_offset (it->w, TEXT_AREA));
22305 if (EQ (prop, Qleft_margin))
22306 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22307 if (EQ (prop, Qright_margin))
22308 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22309 if (EQ (prop, Qscroll_bar))
22310 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22312 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22313 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22314 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22315 : 0)));
22317 else
22319 if (EQ (prop, Qleft_fringe))
22320 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22321 if (EQ (prop, Qright_fringe))
22322 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22323 if (EQ (prop, Qleft_margin))
22324 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22325 if (EQ (prop, Qright_margin))
22326 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22327 if (EQ (prop, Qscroll_bar))
22328 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22331 prop = buffer_local_value_1 (prop, it->w->buffer);
22332 if (EQ (prop, Qunbound))
22333 prop = Qnil;
22336 if (INTEGERP (prop) || FLOATP (prop))
22338 int base_unit = (width_p
22339 ? FRAME_COLUMN_WIDTH (it->f)
22340 : FRAME_LINE_HEIGHT (it->f));
22341 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22344 if (CONSP (prop))
22346 Lisp_Object car = XCAR (prop);
22347 Lisp_Object cdr = XCDR (prop);
22349 if (SYMBOLP (car))
22351 #ifdef HAVE_WINDOW_SYSTEM
22352 if (FRAME_WINDOW_P (it->f)
22353 && valid_image_p (prop))
22355 ptrdiff_t id = lookup_image (it->f, prop);
22356 struct image *img = IMAGE_FROM_ID (it->f, id);
22358 return OK_PIXELS (width_p ? img->width : img->height);
22360 #endif
22361 if (EQ (car, Qplus) || EQ (car, Qminus))
22363 int first = 1;
22364 double px;
22366 pixels = 0;
22367 while (CONSP (cdr))
22369 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22370 font, width_p, align_to))
22371 return 0;
22372 if (first)
22373 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22374 else
22375 pixels += px;
22376 cdr = XCDR (cdr);
22378 if (EQ (car, Qminus))
22379 pixels = -pixels;
22380 return OK_PIXELS (pixels);
22383 car = buffer_local_value_1 (car, it->w->buffer);
22384 if (EQ (car, Qunbound))
22385 car = Qnil;
22388 if (INTEGERP (car) || FLOATP (car))
22390 double fact;
22391 pixels = XFLOATINT (car);
22392 if (NILP (cdr))
22393 return OK_PIXELS (pixels);
22394 if (calc_pixel_width_or_height (&fact, it, cdr,
22395 font, width_p, align_to))
22396 return OK_PIXELS (pixels * fact);
22397 return 0;
22400 return 0;
22403 return 0;
22407 /***********************************************************************
22408 Glyph Display
22409 ***********************************************************************/
22411 #ifdef HAVE_WINDOW_SYSTEM
22413 #ifdef GLYPH_DEBUG
22415 void
22416 dump_glyph_string (struct glyph_string *s)
22418 fprintf (stderr, "glyph string\n");
22419 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22420 s->x, s->y, s->width, s->height);
22421 fprintf (stderr, " ybase = %d\n", s->ybase);
22422 fprintf (stderr, " hl = %d\n", s->hl);
22423 fprintf (stderr, " left overhang = %d, right = %d\n",
22424 s->left_overhang, s->right_overhang);
22425 fprintf (stderr, " nchars = %d\n", s->nchars);
22426 fprintf (stderr, " extends to end of line = %d\n",
22427 s->extends_to_end_of_line_p);
22428 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22429 fprintf (stderr, " bg width = %d\n", s->background_width);
22432 #endif /* GLYPH_DEBUG */
22434 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22435 of XChar2b structures for S; it can't be allocated in
22436 init_glyph_string because it must be allocated via `alloca'. W
22437 is the window on which S is drawn. ROW and AREA are the glyph row
22438 and area within the row from which S is constructed. START is the
22439 index of the first glyph structure covered by S. HL is a
22440 face-override for drawing S. */
22442 #ifdef HAVE_NTGUI
22443 #define OPTIONAL_HDC(hdc) HDC hdc,
22444 #define DECLARE_HDC(hdc) HDC hdc;
22445 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22446 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22447 #endif
22449 #ifndef OPTIONAL_HDC
22450 #define OPTIONAL_HDC(hdc)
22451 #define DECLARE_HDC(hdc)
22452 #define ALLOCATE_HDC(hdc, f)
22453 #define RELEASE_HDC(hdc, f)
22454 #endif
22456 static void
22457 init_glyph_string (struct glyph_string *s,
22458 OPTIONAL_HDC (hdc)
22459 XChar2b *char2b, struct window *w, struct glyph_row *row,
22460 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22462 memset (s, 0, sizeof *s);
22463 s->w = w;
22464 s->f = XFRAME (w->frame);
22465 #ifdef HAVE_NTGUI
22466 s->hdc = hdc;
22467 #endif
22468 s->display = FRAME_X_DISPLAY (s->f);
22469 s->window = FRAME_X_WINDOW (s->f);
22470 s->char2b = char2b;
22471 s->hl = hl;
22472 s->row = row;
22473 s->area = area;
22474 s->first_glyph = row->glyphs[area] + start;
22475 s->height = row->height;
22476 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22477 s->ybase = s->y + row->ascent;
22481 /* Append the list of glyph strings with head H and tail T to the list
22482 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22484 static inline void
22485 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22486 struct glyph_string *h, struct glyph_string *t)
22488 if (h)
22490 if (*head)
22491 (*tail)->next = h;
22492 else
22493 *head = h;
22494 h->prev = *tail;
22495 *tail = t;
22500 /* Prepend the list of glyph strings with head H and tail T to the
22501 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22502 result. */
22504 static inline void
22505 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22506 struct glyph_string *h, struct glyph_string *t)
22508 if (h)
22510 if (*head)
22511 (*head)->prev = t;
22512 else
22513 *tail = t;
22514 t->next = *head;
22515 *head = h;
22520 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22521 Set *HEAD and *TAIL to the resulting list. */
22523 static inline void
22524 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22525 struct glyph_string *s)
22527 s->next = s->prev = NULL;
22528 append_glyph_string_lists (head, tail, s, s);
22532 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22533 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22534 make sure that X resources for the face returned are allocated.
22535 Value is a pointer to a realized face that is ready for display if
22536 DISPLAY_P is non-zero. */
22538 static inline struct face *
22539 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22540 XChar2b *char2b, int display_p)
22542 struct face *face = FACE_FROM_ID (f, face_id);
22544 if (face->font)
22546 unsigned code = face->font->driver->encode_char (face->font, c);
22548 if (code != FONT_INVALID_CODE)
22549 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22550 else
22551 STORE_XCHAR2B (char2b, 0, 0);
22554 /* Make sure X resources of the face are allocated. */
22555 #ifdef HAVE_X_WINDOWS
22556 if (display_p)
22557 #endif
22559 eassert (face != NULL);
22560 PREPARE_FACE_FOR_DISPLAY (f, face);
22563 return face;
22567 /* Get face and two-byte form of character glyph GLYPH on frame F.
22568 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22569 a pointer to a realized face that is ready for display. */
22571 static inline struct face *
22572 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22573 XChar2b *char2b, int *two_byte_p)
22575 struct face *face;
22577 eassert (glyph->type == CHAR_GLYPH);
22578 face = FACE_FROM_ID (f, glyph->face_id);
22580 if (two_byte_p)
22581 *two_byte_p = 0;
22583 if (face->font)
22585 unsigned code;
22587 if (CHAR_BYTE8_P (glyph->u.ch))
22588 code = CHAR_TO_BYTE8 (glyph->u.ch);
22589 else
22590 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22592 if (code != FONT_INVALID_CODE)
22593 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22594 else
22595 STORE_XCHAR2B (char2b, 0, 0);
22598 /* Make sure X resources of the face are allocated. */
22599 eassert (face != NULL);
22600 PREPARE_FACE_FOR_DISPLAY (f, face);
22601 return face;
22605 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22606 Return 1 if FONT has a glyph for C, otherwise return 0. */
22608 static inline int
22609 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22611 unsigned code;
22613 if (CHAR_BYTE8_P (c))
22614 code = CHAR_TO_BYTE8 (c);
22615 else
22616 code = font->driver->encode_char (font, c);
22618 if (code == FONT_INVALID_CODE)
22619 return 0;
22620 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22621 return 1;
22625 /* Fill glyph string S with composition components specified by S->cmp.
22627 BASE_FACE is the base face of the composition.
22628 S->cmp_from is the index of the first component for S.
22630 OVERLAPS non-zero means S should draw the foreground only, and use
22631 its physical height for clipping. See also draw_glyphs.
22633 Value is the index of a component not in S. */
22635 static int
22636 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22637 int overlaps)
22639 int i;
22640 /* For all glyphs of this composition, starting at the offset
22641 S->cmp_from, until we reach the end of the definition or encounter a
22642 glyph that requires the different face, add it to S. */
22643 struct face *face;
22645 eassert (s);
22647 s->for_overlaps = overlaps;
22648 s->face = NULL;
22649 s->font = NULL;
22650 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22652 int c = COMPOSITION_GLYPH (s->cmp, i);
22654 /* TAB in a composition means display glyphs with padding space
22655 on the left or right. */
22656 if (c != '\t')
22658 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22659 -1, Qnil);
22661 face = get_char_face_and_encoding (s->f, c, face_id,
22662 s->char2b + i, 1);
22663 if (face)
22665 if (! s->face)
22667 s->face = face;
22668 s->font = s->face->font;
22670 else if (s->face != face)
22671 break;
22674 ++s->nchars;
22676 s->cmp_to = i;
22678 if (s->face == NULL)
22680 s->face = base_face->ascii_face;
22681 s->font = s->face->font;
22684 /* All glyph strings for the same composition has the same width,
22685 i.e. the width set for the first component of the composition. */
22686 s->width = s->first_glyph->pixel_width;
22688 /* If the specified font could not be loaded, use the frame's
22689 default font, but record the fact that we couldn't load it in
22690 the glyph string so that we can draw rectangles for the
22691 characters of the glyph string. */
22692 if (s->font == NULL)
22694 s->font_not_found_p = 1;
22695 s->font = FRAME_FONT (s->f);
22698 /* Adjust base line for subscript/superscript text. */
22699 s->ybase += s->first_glyph->voffset;
22701 /* This glyph string must always be drawn with 16-bit functions. */
22702 s->two_byte_p = 1;
22704 return s->cmp_to;
22707 static int
22708 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22709 int start, int end, int overlaps)
22711 struct glyph *glyph, *last;
22712 Lisp_Object lgstring;
22713 int i;
22715 s->for_overlaps = overlaps;
22716 glyph = s->row->glyphs[s->area] + start;
22717 last = s->row->glyphs[s->area] + end;
22718 s->cmp_id = glyph->u.cmp.id;
22719 s->cmp_from = glyph->slice.cmp.from;
22720 s->cmp_to = glyph->slice.cmp.to + 1;
22721 s->face = FACE_FROM_ID (s->f, face_id);
22722 lgstring = composition_gstring_from_id (s->cmp_id);
22723 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22724 glyph++;
22725 while (glyph < last
22726 && glyph->u.cmp.automatic
22727 && glyph->u.cmp.id == s->cmp_id
22728 && s->cmp_to == glyph->slice.cmp.from)
22729 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22731 for (i = s->cmp_from; i < s->cmp_to; i++)
22733 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22734 unsigned code = LGLYPH_CODE (lglyph);
22736 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22738 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22739 return glyph - s->row->glyphs[s->area];
22743 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22744 See the comment of fill_glyph_string for arguments.
22745 Value is the index of the first glyph not in S. */
22748 static int
22749 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22750 int start, int end, int overlaps)
22752 struct glyph *glyph, *last;
22753 int voffset;
22755 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22756 s->for_overlaps = overlaps;
22757 glyph = s->row->glyphs[s->area] + start;
22758 last = s->row->glyphs[s->area] + end;
22759 voffset = glyph->voffset;
22760 s->face = FACE_FROM_ID (s->f, face_id);
22761 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22762 s->nchars = 1;
22763 s->width = glyph->pixel_width;
22764 glyph++;
22765 while (glyph < last
22766 && glyph->type == GLYPHLESS_GLYPH
22767 && glyph->voffset == voffset
22768 && glyph->face_id == face_id)
22770 s->nchars++;
22771 s->width += glyph->pixel_width;
22772 glyph++;
22774 s->ybase += voffset;
22775 return glyph - s->row->glyphs[s->area];
22779 /* Fill glyph string S from a sequence of character glyphs.
22781 FACE_ID is the face id of the string. START is the index of the
22782 first glyph to consider, END is the index of the last + 1.
22783 OVERLAPS non-zero means S should draw the foreground only, and use
22784 its physical height for clipping. See also draw_glyphs.
22786 Value is the index of the first glyph not in S. */
22788 static int
22789 fill_glyph_string (struct glyph_string *s, int face_id,
22790 int start, int end, int overlaps)
22792 struct glyph *glyph, *last;
22793 int voffset;
22794 int glyph_not_available_p;
22796 eassert (s->f == XFRAME (s->w->frame));
22797 eassert (s->nchars == 0);
22798 eassert (start >= 0 && end > start);
22800 s->for_overlaps = overlaps;
22801 glyph = s->row->glyphs[s->area] + start;
22802 last = s->row->glyphs[s->area] + end;
22803 voffset = glyph->voffset;
22804 s->padding_p = glyph->padding_p;
22805 glyph_not_available_p = glyph->glyph_not_available_p;
22807 while (glyph < last
22808 && glyph->type == CHAR_GLYPH
22809 && glyph->voffset == voffset
22810 /* Same face id implies same font, nowadays. */
22811 && glyph->face_id == face_id
22812 && glyph->glyph_not_available_p == glyph_not_available_p)
22814 int two_byte_p;
22816 s->face = get_glyph_face_and_encoding (s->f, glyph,
22817 s->char2b + s->nchars,
22818 &two_byte_p);
22819 s->two_byte_p = two_byte_p;
22820 ++s->nchars;
22821 eassert (s->nchars <= end - start);
22822 s->width += glyph->pixel_width;
22823 if (glyph++->padding_p != s->padding_p)
22824 break;
22827 s->font = s->face->font;
22829 /* If the specified font could not be loaded, use the frame's font,
22830 but record the fact that we couldn't load it in
22831 S->font_not_found_p so that we can draw rectangles for the
22832 characters of the glyph string. */
22833 if (s->font == NULL || glyph_not_available_p)
22835 s->font_not_found_p = 1;
22836 s->font = FRAME_FONT (s->f);
22839 /* Adjust base line for subscript/superscript text. */
22840 s->ybase += voffset;
22842 eassert (s->face && s->face->gc);
22843 return glyph - s->row->glyphs[s->area];
22847 /* Fill glyph string S from image glyph S->first_glyph. */
22849 static void
22850 fill_image_glyph_string (struct glyph_string *s)
22852 eassert (s->first_glyph->type == IMAGE_GLYPH);
22853 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22854 eassert (s->img);
22855 s->slice = s->first_glyph->slice.img;
22856 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22857 s->font = s->face->font;
22858 s->width = s->first_glyph->pixel_width;
22860 /* Adjust base line for subscript/superscript text. */
22861 s->ybase += s->first_glyph->voffset;
22865 /* Fill glyph string S from a sequence of stretch glyphs.
22867 START is the index of the first glyph to consider,
22868 END is the index of the last + 1.
22870 Value is the index of the first glyph not in S. */
22872 static int
22873 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22875 struct glyph *glyph, *last;
22876 int voffset, face_id;
22878 eassert (s->first_glyph->type == STRETCH_GLYPH);
22880 glyph = s->row->glyphs[s->area] + start;
22881 last = s->row->glyphs[s->area] + end;
22882 face_id = glyph->face_id;
22883 s->face = FACE_FROM_ID (s->f, face_id);
22884 s->font = s->face->font;
22885 s->width = glyph->pixel_width;
22886 s->nchars = 1;
22887 voffset = glyph->voffset;
22889 for (++glyph;
22890 (glyph < last
22891 && glyph->type == STRETCH_GLYPH
22892 && glyph->voffset == voffset
22893 && glyph->face_id == face_id);
22894 ++glyph)
22895 s->width += glyph->pixel_width;
22897 /* Adjust base line for subscript/superscript text. */
22898 s->ybase += voffset;
22900 /* The case that face->gc == 0 is handled when drawing the glyph
22901 string by calling PREPARE_FACE_FOR_DISPLAY. */
22902 eassert (s->face);
22903 return glyph - s->row->glyphs[s->area];
22906 static struct font_metrics *
22907 get_per_char_metric (struct font *font, XChar2b *char2b)
22909 static struct font_metrics metrics;
22910 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22912 if (! font || code == FONT_INVALID_CODE)
22913 return NULL;
22914 font->driver->text_extents (font, &code, 1, &metrics);
22915 return &metrics;
22918 /* EXPORT for RIF:
22919 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22920 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22921 assumed to be zero. */
22923 void
22924 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22926 *left = *right = 0;
22928 if (glyph->type == CHAR_GLYPH)
22930 struct face *face;
22931 XChar2b char2b;
22932 struct font_metrics *pcm;
22934 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22935 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22937 if (pcm->rbearing > pcm->width)
22938 *right = pcm->rbearing - pcm->width;
22939 if (pcm->lbearing < 0)
22940 *left = -pcm->lbearing;
22943 else if (glyph->type == COMPOSITE_GLYPH)
22945 if (! glyph->u.cmp.automatic)
22947 struct composition *cmp = composition_table[glyph->u.cmp.id];
22949 if (cmp->rbearing > cmp->pixel_width)
22950 *right = cmp->rbearing - cmp->pixel_width;
22951 if (cmp->lbearing < 0)
22952 *left = - cmp->lbearing;
22954 else
22956 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22957 struct font_metrics metrics;
22959 composition_gstring_width (gstring, glyph->slice.cmp.from,
22960 glyph->slice.cmp.to + 1, &metrics);
22961 if (metrics.rbearing > metrics.width)
22962 *right = metrics.rbearing - metrics.width;
22963 if (metrics.lbearing < 0)
22964 *left = - metrics.lbearing;
22970 /* Return the index of the first glyph preceding glyph string S that
22971 is overwritten by S because of S's left overhang. Value is -1
22972 if no glyphs are overwritten. */
22974 static int
22975 left_overwritten (struct glyph_string *s)
22977 int k;
22979 if (s->left_overhang)
22981 int x = 0, i;
22982 struct glyph *glyphs = s->row->glyphs[s->area];
22983 int first = s->first_glyph - glyphs;
22985 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22986 x -= glyphs[i].pixel_width;
22988 k = i + 1;
22990 else
22991 k = -1;
22993 return k;
22997 /* Return the index of the first glyph preceding glyph string S that
22998 is overwriting S because of its right overhang. Value is -1 if no
22999 glyph in front of S overwrites S. */
23001 static int
23002 left_overwriting (struct glyph_string *s)
23004 int i, k, x;
23005 struct glyph *glyphs = s->row->glyphs[s->area];
23006 int first = s->first_glyph - glyphs;
23008 k = -1;
23009 x = 0;
23010 for (i = first - 1; i >= 0; --i)
23012 int left, right;
23013 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23014 if (x + right > 0)
23015 k = i;
23016 x -= glyphs[i].pixel_width;
23019 return k;
23023 /* Return the index of the last glyph following glyph string S that is
23024 overwritten by S because of S's right overhang. Value is -1 if
23025 no such glyph is found. */
23027 static int
23028 right_overwritten (struct glyph_string *s)
23030 int k = -1;
23032 if (s->right_overhang)
23034 int x = 0, i;
23035 struct glyph *glyphs = s->row->glyphs[s->area];
23036 int first = (s->first_glyph - glyphs
23037 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23038 int end = s->row->used[s->area];
23040 for (i = first; i < end && s->right_overhang > x; ++i)
23041 x += glyphs[i].pixel_width;
23043 k = i;
23046 return k;
23050 /* Return the index of the last glyph following glyph string S that
23051 overwrites S because of its left overhang. Value is negative
23052 if no such glyph is found. */
23054 static int
23055 right_overwriting (struct glyph_string *s)
23057 int i, k, x;
23058 int end = s->row->used[s->area];
23059 struct glyph *glyphs = s->row->glyphs[s->area];
23060 int first = (s->first_glyph - glyphs
23061 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23063 k = -1;
23064 x = 0;
23065 for (i = first; i < end; ++i)
23067 int left, right;
23068 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23069 if (x - left < 0)
23070 k = i;
23071 x += glyphs[i].pixel_width;
23074 return k;
23078 /* Set background width of glyph string S. START is the index of the
23079 first glyph following S. LAST_X is the right-most x-position + 1
23080 in the drawing area. */
23082 static inline void
23083 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23085 /* If the face of this glyph string has to be drawn to the end of
23086 the drawing area, set S->extends_to_end_of_line_p. */
23088 if (start == s->row->used[s->area]
23089 && s->area == TEXT_AREA
23090 && ((s->row->fill_line_p
23091 && (s->hl == DRAW_NORMAL_TEXT
23092 || s->hl == DRAW_IMAGE_RAISED
23093 || s->hl == DRAW_IMAGE_SUNKEN))
23094 || s->hl == DRAW_MOUSE_FACE))
23095 s->extends_to_end_of_line_p = 1;
23097 /* If S extends its face to the end of the line, set its
23098 background_width to the distance to the right edge of the drawing
23099 area. */
23100 if (s->extends_to_end_of_line_p)
23101 s->background_width = last_x - s->x + 1;
23102 else
23103 s->background_width = s->width;
23107 /* Compute overhangs and x-positions for glyph string S and its
23108 predecessors, or successors. X is the starting x-position for S.
23109 BACKWARD_P non-zero means process predecessors. */
23111 static void
23112 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23114 if (backward_p)
23116 while (s)
23118 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23119 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23120 x -= s->width;
23121 s->x = x;
23122 s = s->prev;
23125 else
23127 while (s)
23129 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23130 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23131 s->x = x;
23132 x += s->width;
23133 s = s->next;
23140 /* The following macros are only called from draw_glyphs below.
23141 They reference the following parameters of that function directly:
23142 `w', `row', `area', and `overlap_p'
23143 as well as the following local variables:
23144 `s', `f', and `hdc' (in W32) */
23146 #ifdef HAVE_NTGUI
23147 /* On W32, silently add local `hdc' variable to argument list of
23148 init_glyph_string. */
23149 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23150 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23151 #else
23152 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23153 init_glyph_string (s, char2b, w, row, area, start, hl)
23154 #endif
23156 /* Add a glyph string for a stretch glyph to the list of strings
23157 between HEAD and TAIL. START is the index of the stretch glyph in
23158 row area AREA of glyph row ROW. END is the index of the last glyph
23159 in that glyph row area. X is the current output position assigned
23160 to the new glyph string constructed. HL overrides that face of the
23161 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23162 is the right-most x-position of the drawing area. */
23164 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23165 and below -- keep them on one line. */
23166 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23167 do \
23169 s = alloca (sizeof *s); \
23170 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23171 START = fill_stretch_glyph_string (s, START, END); \
23172 append_glyph_string (&HEAD, &TAIL, s); \
23173 s->x = (X); \
23175 while (0)
23178 /* Add a glyph string for an image glyph to the list of strings
23179 between HEAD and TAIL. START is the index of the image glyph in
23180 row area AREA of glyph row ROW. END is the index of the last glyph
23181 in that glyph row area. X is the current output position assigned
23182 to the new glyph string constructed. HL overrides that face of the
23183 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23184 is the right-most x-position of the drawing area. */
23186 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23187 do \
23189 s = alloca (sizeof *s); \
23190 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23191 fill_image_glyph_string (s); \
23192 append_glyph_string (&HEAD, &TAIL, s); \
23193 ++START; \
23194 s->x = (X); \
23196 while (0)
23199 /* Add a glyph string for a sequence of character glyphs to the list
23200 of strings between HEAD and TAIL. START is the index of the first
23201 glyph in row area AREA of glyph row ROW that is part of the new
23202 glyph string. END is the index of the last glyph in that glyph row
23203 area. X is the current output position assigned to the new glyph
23204 string constructed. HL overrides that face of the glyph; e.g. it
23205 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23206 right-most x-position of the drawing area. */
23208 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23209 do \
23211 int face_id; \
23212 XChar2b *char2b; \
23214 face_id = (row)->glyphs[area][START].face_id; \
23216 s = alloca (sizeof *s); \
23217 char2b = alloca ((END - START) * sizeof *char2b); \
23218 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23219 append_glyph_string (&HEAD, &TAIL, s); \
23220 s->x = (X); \
23221 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23223 while (0)
23226 /* Add a glyph string for a composite sequence to the list of strings
23227 between HEAD and TAIL. START is the index of the first glyph in
23228 row area AREA of glyph row ROW that is part of the new glyph
23229 string. END is the index of the last glyph in that glyph row area.
23230 X is the current output position assigned to the new glyph string
23231 constructed. HL overrides that face of the glyph; e.g. it is
23232 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23233 x-position of the drawing area. */
23235 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23236 do { \
23237 int face_id = (row)->glyphs[area][START].face_id; \
23238 struct face *base_face = FACE_FROM_ID (f, face_id); \
23239 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23240 struct composition *cmp = composition_table[cmp_id]; \
23241 XChar2b *char2b; \
23242 struct glyph_string *first_s = NULL; \
23243 int n; \
23245 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23247 /* Make glyph_strings for each glyph sequence that is drawable by \
23248 the same face, and append them to HEAD/TAIL. */ \
23249 for (n = 0; n < cmp->glyph_len;) \
23251 s = alloca (sizeof *s); \
23252 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23253 append_glyph_string (&(HEAD), &(TAIL), s); \
23254 s->cmp = cmp; \
23255 s->cmp_from = n; \
23256 s->x = (X); \
23257 if (n == 0) \
23258 first_s = s; \
23259 n = fill_composite_glyph_string (s, base_face, overlaps); \
23262 ++START; \
23263 s = first_s; \
23264 } while (0)
23267 /* Add a glyph string for a glyph-string sequence to the list of strings
23268 between HEAD and TAIL. */
23270 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23271 do { \
23272 int face_id; \
23273 XChar2b *char2b; \
23274 Lisp_Object gstring; \
23276 face_id = (row)->glyphs[area][START].face_id; \
23277 gstring = (composition_gstring_from_id \
23278 ((row)->glyphs[area][START].u.cmp.id)); \
23279 s = alloca (sizeof *s); \
23280 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23281 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23282 append_glyph_string (&(HEAD), &(TAIL), s); \
23283 s->x = (X); \
23284 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23285 } while (0)
23288 /* Add a glyph string for a sequence of glyphless character's glyphs
23289 to the list of strings between HEAD and TAIL. The meanings of
23290 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23292 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23293 do \
23295 int face_id; \
23297 face_id = (row)->glyphs[area][START].face_id; \
23299 s = alloca (sizeof *s); \
23300 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23301 append_glyph_string (&HEAD, &TAIL, s); \
23302 s->x = (X); \
23303 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23304 overlaps); \
23306 while (0)
23309 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23310 of AREA of glyph row ROW on window W between indices START and END.
23311 HL overrides the face for drawing glyph strings, e.g. it is
23312 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23313 x-positions of the drawing area.
23315 This is an ugly monster macro construct because we must use alloca
23316 to allocate glyph strings (because draw_glyphs can be called
23317 asynchronously). */
23319 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23320 do \
23322 HEAD = TAIL = NULL; \
23323 while (START < END) \
23325 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23326 switch (first_glyph->type) \
23328 case CHAR_GLYPH: \
23329 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23330 HL, X, LAST_X); \
23331 break; \
23333 case COMPOSITE_GLYPH: \
23334 if (first_glyph->u.cmp.automatic) \
23335 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23336 HL, X, LAST_X); \
23337 else \
23338 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23339 HL, X, LAST_X); \
23340 break; \
23342 case STRETCH_GLYPH: \
23343 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23344 HL, X, LAST_X); \
23345 break; \
23347 case IMAGE_GLYPH: \
23348 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23349 HL, X, LAST_X); \
23350 break; \
23352 case GLYPHLESS_GLYPH: \
23353 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23354 HL, X, LAST_X); \
23355 break; \
23357 default: \
23358 emacs_abort (); \
23361 if (s) \
23363 set_glyph_string_background_width (s, START, LAST_X); \
23364 (X) += s->width; \
23367 } while (0)
23370 /* Draw glyphs between START and END in AREA of ROW on window W,
23371 starting at x-position X. X is relative to AREA in W. HL is a
23372 face-override with the following meaning:
23374 DRAW_NORMAL_TEXT draw normally
23375 DRAW_CURSOR draw in cursor face
23376 DRAW_MOUSE_FACE draw in mouse face.
23377 DRAW_INVERSE_VIDEO draw in mode line face
23378 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23379 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23381 If OVERLAPS is non-zero, draw only the foreground of characters and
23382 clip to the physical height of ROW. Non-zero value also defines
23383 the overlapping part to be drawn:
23385 OVERLAPS_PRED overlap with preceding rows
23386 OVERLAPS_SUCC overlap with succeeding rows
23387 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23388 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23390 Value is the x-position reached, relative to AREA of W. */
23392 static int
23393 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23394 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23395 enum draw_glyphs_face hl, int overlaps)
23397 struct glyph_string *head, *tail;
23398 struct glyph_string *s;
23399 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23400 int i, j, x_reached, last_x, area_left = 0;
23401 struct frame *f = XFRAME (WINDOW_FRAME (w));
23402 DECLARE_HDC (hdc);
23404 ALLOCATE_HDC (hdc, f);
23406 /* Let's rather be paranoid than getting a SEGV. */
23407 end = min (end, row->used[area]);
23408 start = max (0, start);
23409 start = min (end, start);
23411 /* Translate X to frame coordinates. Set last_x to the right
23412 end of the drawing area. */
23413 if (row->full_width_p)
23415 /* X is relative to the left edge of W, without scroll bars
23416 or fringes. */
23417 area_left = WINDOW_LEFT_EDGE_X (w);
23418 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23420 else
23422 area_left = window_box_left (w, area);
23423 last_x = area_left + window_box_width (w, area);
23425 x += area_left;
23427 /* Build a doubly-linked list of glyph_string structures between
23428 head and tail from what we have to draw. Note that the macro
23429 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23430 the reason we use a separate variable `i'. */
23431 i = start;
23432 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23433 if (tail)
23434 x_reached = tail->x + tail->background_width;
23435 else
23436 x_reached = x;
23438 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23439 the row, redraw some glyphs in front or following the glyph
23440 strings built above. */
23441 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23443 struct glyph_string *h, *t;
23444 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23445 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23446 int check_mouse_face = 0;
23447 int dummy_x = 0;
23449 /* If mouse highlighting is on, we may need to draw adjacent
23450 glyphs using mouse-face highlighting. */
23451 if (area == TEXT_AREA && row->mouse_face_p)
23453 struct glyph_row *mouse_beg_row, *mouse_end_row;
23455 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23456 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23458 if (row >= mouse_beg_row && row <= mouse_end_row)
23460 check_mouse_face = 1;
23461 mouse_beg_col = (row == mouse_beg_row)
23462 ? hlinfo->mouse_face_beg_col : 0;
23463 mouse_end_col = (row == mouse_end_row)
23464 ? hlinfo->mouse_face_end_col
23465 : row->used[TEXT_AREA];
23469 /* Compute overhangs for all glyph strings. */
23470 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23471 for (s = head; s; s = s->next)
23472 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23474 /* Prepend glyph strings for glyphs in front of the first glyph
23475 string that are overwritten because of the first glyph
23476 string's left overhang. The background of all strings
23477 prepended must be drawn because the first glyph string
23478 draws over it. */
23479 i = left_overwritten (head);
23480 if (i >= 0)
23482 enum draw_glyphs_face overlap_hl;
23484 /* If this row contains mouse highlighting, attempt to draw
23485 the overlapped glyphs with the correct highlight. This
23486 code fails if the overlap encompasses more than one glyph
23487 and mouse-highlight spans only some of these glyphs.
23488 However, making it work perfectly involves a lot more
23489 code, and I don't know if the pathological case occurs in
23490 practice, so we'll stick to this for now. --- cyd */
23491 if (check_mouse_face
23492 && mouse_beg_col < start && mouse_end_col > i)
23493 overlap_hl = DRAW_MOUSE_FACE;
23494 else
23495 overlap_hl = DRAW_NORMAL_TEXT;
23497 j = i;
23498 BUILD_GLYPH_STRINGS (j, start, h, t,
23499 overlap_hl, dummy_x, last_x);
23500 start = i;
23501 compute_overhangs_and_x (t, head->x, 1);
23502 prepend_glyph_string_lists (&head, &tail, h, t);
23503 clip_head = head;
23506 /* Prepend glyph strings for glyphs in front of the first glyph
23507 string that overwrite that glyph string because of their
23508 right overhang. For these strings, only the foreground must
23509 be drawn, because it draws over the glyph string at `head'.
23510 The background must not be drawn because this would overwrite
23511 right overhangs of preceding glyphs for which no glyph
23512 strings exist. */
23513 i = left_overwriting (head);
23514 if (i >= 0)
23516 enum draw_glyphs_face overlap_hl;
23518 if (check_mouse_face
23519 && mouse_beg_col < start && mouse_end_col > i)
23520 overlap_hl = DRAW_MOUSE_FACE;
23521 else
23522 overlap_hl = DRAW_NORMAL_TEXT;
23524 clip_head = head;
23525 BUILD_GLYPH_STRINGS (i, start, h, t,
23526 overlap_hl, dummy_x, last_x);
23527 for (s = h; s; s = s->next)
23528 s->background_filled_p = 1;
23529 compute_overhangs_and_x (t, head->x, 1);
23530 prepend_glyph_string_lists (&head, &tail, h, t);
23533 /* Append glyphs strings for glyphs following the last glyph
23534 string tail that are overwritten by tail. The background of
23535 these strings has to be drawn because tail's foreground draws
23536 over it. */
23537 i = right_overwritten (tail);
23538 if (i >= 0)
23540 enum draw_glyphs_face overlap_hl;
23542 if (check_mouse_face
23543 && mouse_beg_col < i && mouse_end_col > end)
23544 overlap_hl = DRAW_MOUSE_FACE;
23545 else
23546 overlap_hl = DRAW_NORMAL_TEXT;
23548 BUILD_GLYPH_STRINGS (end, i, h, t,
23549 overlap_hl, x, last_x);
23550 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23551 we don't have `end = i;' here. */
23552 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23553 append_glyph_string_lists (&head, &tail, h, t);
23554 clip_tail = tail;
23557 /* Append glyph strings for glyphs following the last glyph
23558 string tail that overwrite tail. The foreground of such
23559 glyphs has to be drawn because it writes into the background
23560 of tail. The background must not be drawn because it could
23561 paint over the foreground of following glyphs. */
23562 i = right_overwriting (tail);
23563 if (i >= 0)
23565 enum draw_glyphs_face overlap_hl;
23566 if (check_mouse_face
23567 && mouse_beg_col < i && mouse_end_col > end)
23568 overlap_hl = DRAW_MOUSE_FACE;
23569 else
23570 overlap_hl = DRAW_NORMAL_TEXT;
23572 clip_tail = tail;
23573 i++; /* We must include the Ith glyph. */
23574 BUILD_GLYPH_STRINGS (end, i, h, t,
23575 overlap_hl, x, last_x);
23576 for (s = h; s; s = s->next)
23577 s->background_filled_p = 1;
23578 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23579 append_glyph_string_lists (&head, &tail, h, t);
23581 if (clip_head || clip_tail)
23582 for (s = head; s; s = s->next)
23584 s->clip_head = clip_head;
23585 s->clip_tail = clip_tail;
23589 /* Draw all strings. */
23590 for (s = head; s; s = s->next)
23591 FRAME_RIF (f)->draw_glyph_string (s);
23593 #ifndef HAVE_NS
23594 /* When focus a sole frame and move horizontally, this sets on_p to 0
23595 causing a failure to erase prev cursor position. */
23596 if (area == TEXT_AREA
23597 && !row->full_width_p
23598 /* When drawing overlapping rows, only the glyph strings'
23599 foreground is drawn, which doesn't erase a cursor
23600 completely. */
23601 && !overlaps)
23603 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23604 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23605 : (tail ? tail->x + tail->background_width : x));
23606 x0 -= area_left;
23607 x1 -= area_left;
23609 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23610 row->y, MATRIX_ROW_BOTTOM_Y (row));
23612 #endif
23614 /* Value is the x-position up to which drawn, relative to AREA of W.
23615 This doesn't include parts drawn because of overhangs. */
23616 if (row->full_width_p)
23617 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23618 else
23619 x_reached -= area_left;
23621 RELEASE_HDC (hdc, f);
23623 return x_reached;
23626 /* Expand row matrix if too narrow. Don't expand if area
23627 is not present. */
23629 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23631 if (!fonts_changed_p \
23632 && (it->glyph_row->glyphs[area] \
23633 < it->glyph_row->glyphs[area + 1])) \
23635 it->w->ncols_scale_factor++; \
23636 fonts_changed_p = 1; \
23640 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23641 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23643 static inline void
23644 append_glyph (struct it *it)
23646 struct glyph *glyph;
23647 enum glyph_row_area area = it->area;
23649 eassert (it->glyph_row);
23650 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23652 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23653 if (glyph < it->glyph_row->glyphs[area + 1])
23655 /* If the glyph row is reversed, we need to prepend the glyph
23656 rather than append it. */
23657 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23659 struct glyph *g;
23661 /* Make room for the additional glyph. */
23662 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23663 g[1] = *g;
23664 glyph = it->glyph_row->glyphs[area];
23666 glyph->charpos = CHARPOS (it->position);
23667 glyph->object = it->object;
23668 if (it->pixel_width > 0)
23670 glyph->pixel_width = it->pixel_width;
23671 glyph->padding_p = 0;
23673 else
23675 /* Assure at least 1-pixel width. Otherwise, cursor can't
23676 be displayed correctly. */
23677 glyph->pixel_width = 1;
23678 glyph->padding_p = 1;
23680 glyph->ascent = it->ascent;
23681 glyph->descent = it->descent;
23682 glyph->voffset = it->voffset;
23683 glyph->type = CHAR_GLYPH;
23684 glyph->avoid_cursor_p = it->avoid_cursor_p;
23685 glyph->multibyte_p = it->multibyte_p;
23686 glyph->left_box_line_p = it->start_of_box_run_p;
23687 glyph->right_box_line_p = it->end_of_box_run_p;
23688 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23689 || it->phys_descent > it->descent);
23690 glyph->glyph_not_available_p = it->glyph_not_available_p;
23691 glyph->face_id = it->face_id;
23692 glyph->u.ch = it->char_to_display;
23693 glyph->slice.img = null_glyph_slice;
23694 glyph->font_type = FONT_TYPE_UNKNOWN;
23695 if (it->bidi_p)
23697 glyph->resolved_level = it->bidi_it.resolved_level;
23698 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23699 emacs_abort ();
23700 glyph->bidi_type = it->bidi_it.type;
23702 else
23704 glyph->resolved_level = 0;
23705 glyph->bidi_type = UNKNOWN_BT;
23707 ++it->glyph_row->used[area];
23709 else
23710 IT_EXPAND_MATRIX_WIDTH (it, area);
23713 /* Store one glyph for the composition IT->cmp_it.id in
23714 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23715 non-null. */
23717 static inline void
23718 append_composite_glyph (struct it *it)
23720 struct glyph *glyph;
23721 enum glyph_row_area area = it->area;
23723 eassert (it->glyph_row);
23725 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23726 if (glyph < it->glyph_row->glyphs[area + 1])
23728 /* If the glyph row is reversed, we need to prepend the glyph
23729 rather than append it. */
23730 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23732 struct glyph *g;
23734 /* Make room for the new glyph. */
23735 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23736 g[1] = *g;
23737 glyph = it->glyph_row->glyphs[it->area];
23739 glyph->charpos = it->cmp_it.charpos;
23740 glyph->object = it->object;
23741 glyph->pixel_width = it->pixel_width;
23742 glyph->ascent = it->ascent;
23743 glyph->descent = it->descent;
23744 glyph->voffset = it->voffset;
23745 glyph->type = COMPOSITE_GLYPH;
23746 if (it->cmp_it.ch < 0)
23748 glyph->u.cmp.automatic = 0;
23749 glyph->u.cmp.id = it->cmp_it.id;
23750 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23752 else
23754 glyph->u.cmp.automatic = 1;
23755 glyph->u.cmp.id = it->cmp_it.id;
23756 glyph->slice.cmp.from = it->cmp_it.from;
23757 glyph->slice.cmp.to = it->cmp_it.to - 1;
23759 glyph->avoid_cursor_p = it->avoid_cursor_p;
23760 glyph->multibyte_p = it->multibyte_p;
23761 glyph->left_box_line_p = it->start_of_box_run_p;
23762 glyph->right_box_line_p = it->end_of_box_run_p;
23763 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23764 || it->phys_descent > it->descent);
23765 glyph->padding_p = 0;
23766 glyph->glyph_not_available_p = 0;
23767 glyph->face_id = it->face_id;
23768 glyph->font_type = FONT_TYPE_UNKNOWN;
23769 if (it->bidi_p)
23771 glyph->resolved_level = it->bidi_it.resolved_level;
23772 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23773 emacs_abort ();
23774 glyph->bidi_type = it->bidi_it.type;
23776 ++it->glyph_row->used[area];
23778 else
23779 IT_EXPAND_MATRIX_WIDTH (it, area);
23783 /* Change IT->ascent and IT->height according to the setting of
23784 IT->voffset. */
23786 static inline void
23787 take_vertical_position_into_account (struct it *it)
23789 if (it->voffset)
23791 if (it->voffset < 0)
23792 /* Increase the ascent so that we can display the text higher
23793 in the line. */
23794 it->ascent -= it->voffset;
23795 else
23796 /* Increase the descent so that we can display the text lower
23797 in the line. */
23798 it->descent += it->voffset;
23803 /* Produce glyphs/get display metrics for the image IT is loaded with.
23804 See the description of struct display_iterator in dispextern.h for
23805 an overview of struct display_iterator. */
23807 static void
23808 produce_image_glyph (struct it *it)
23810 struct image *img;
23811 struct face *face;
23812 int glyph_ascent, crop;
23813 struct glyph_slice slice;
23815 eassert (it->what == IT_IMAGE);
23817 face = FACE_FROM_ID (it->f, it->face_id);
23818 eassert (face);
23819 /* Make sure X resources of the face is loaded. */
23820 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23822 if (it->image_id < 0)
23824 /* Fringe bitmap. */
23825 it->ascent = it->phys_ascent = 0;
23826 it->descent = it->phys_descent = 0;
23827 it->pixel_width = 0;
23828 it->nglyphs = 0;
23829 return;
23832 img = IMAGE_FROM_ID (it->f, it->image_id);
23833 eassert (img);
23834 /* Make sure X resources of the image is loaded. */
23835 prepare_image_for_display (it->f, img);
23837 slice.x = slice.y = 0;
23838 slice.width = img->width;
23839 slice.height = img->height;
23841 if (INTEGERP (it->slice.x))
23842 slice.x = XINT (it->slice.x);
23843 else if (FLOATP (it->slice.x))
23844 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23846 if (INTEGERP (it->slice.y))
23847 slice.y = XINT (it->slice.y);
23848 else if (FLOATP (it->slice.y))
23849 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23851 if (INTEGERP (it->slice.width))
23852 slice.width = XINT (it->slice.width);
23853 else if (FLOATP (it->slice.width))
23854 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23856 if (INTEGERP (it->slice.height))
23857 slice.height = XINT (it->slice.height);
23858 else if (FLOATP (it->slice.height))
23859 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23861 if (slice.x >= img->width)
23862 slice.x = img->width;
23863 if (slice.y >= img->height)
23864 slice.y = img->height;
23865 if (slice.x + slice.width >= img->width)
23866 slice.width = img->width - slice.x;
23867 if (slice.y + slice.height > img->height)
23868 slice.height = img->height - slice.y;
23870 if (slice.width == 0 || slice.height == 0)
23871 return;
23873 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23875 it->descent = slice.height - glyph_ascent;
23876 if (slice.y == 0)
23877 it->descent += img->vmargin;
23878 if (slice.y + slice.height == img->height)
23879 it->descent += img->vmargin;
23880 it->phys_descent = it->descent;
23882 it->pixel_width = slice.width;
23883 if (slice.x == 0)
23884 it->pixel_width += img->hmargin;
23885 if (slice.x + slice.width == img->width)
23886 it->pixel_width += img->hmargin;
23888 /* It's quite possible for images to have an ascent greater than
23889 their height, so don't get confused in that case. */
23890 if (it->descent < 0)
23891 it->descent = 0;
23893 it->nglyphs = 1;
23895 if (face->box != FACE_NO_BOX)
23897 if (face->box_line_width > 0)
23899 if (slice.y == 0)
23900 it->ascent += face->box_line_width;
23901 if (slice.y + slice.height == img->height)
23902 it->descent += face->box_line_width;
23905 if (it->start_of_box_run_p && slice.x == 0)
23906 it->pixel_width += eabs (face->box_line_width);
23907 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23908 it->pixel_width += eabs (face->box_line_width);
23911 take_vertical_position_into_account (it);
23913 /* Automatically crop wide image glyphs at right edge so we can
23914 draw the cursor on same display row. */
23915 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23916 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23918 it->pixel_width -= crop;
23919 slice.width -= crop;
23922 if (it->glyph_row)
23924 struct glyph *glyph;
23925 enum glyph_row_area area = it->area;
23927 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23928 if (glyph < it->glyph_row->glyphs[area + 1])
23930 glyph->charpos = CHARPOS (it->position);
23931 glyph->object = it->object;
23932 glyph->pixel_width = it->pixel_width;
23933 glyph->ascent = glyph_ascent;
23934 glyph->descent = it->descent;
23935 glyph->voffset = it->voffset;
23936 glyph->type = IMAGE_GLYPH;
23937 glyph->avoid_cursor_p = it->avoid_cursor_p;
23938 glyph->multibyte_p = it->multibyte_p;
23939 glyph->left_box_line_p = it->start_of_box_run_p;
23940 glyph->right_box_line_p = it->end_of_box_run_p;
23941 glyph->overlaps_vertically_p = 0;
23942 glyph->padding_p = 0;
23943 glyph->glyph_not_available_p = 0;
23944 glyph->face_id = it->face_id;
23945 glyph->u.img_id = img->id;
23946 glyph->slice.img = slice;
23947 glyph->font_type = FONT_TYPE_UNKNOWN;
23948 if (it->bidi_p)
23950 glyph->resolved_level = it->bidi_it.resolved_level;
23951 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23952 emacs_abort ();
23953 glyph->bidi_type = it->bidi_it.type;
23955 ++it->glyph_row->used[area];
23957 else
23958 IT_EXPAND_MATRIX_WIDTH (it, area);
23963 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23964 of the glyph, WIDTH and HEIGHT are the width and height of the
23965 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23967 static void
23968 append_stretch_glyph (struct it *it, Lisp_Object object,
23969 int width, int height, int ascent)
23971 struct glyph *glyph;
23972 enum glyph_row_area area = it->area;
23974 eassert (ascent >= 0 && ascent <= height);
23976 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23977 if (glyph < it->glyph_row->glyphs[area + 1])
23979 /* If the glyph row is reversed, we need to prepend the glyph
23980 rather than append it. */
23981 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23983 struct glyph *g;
23985 /* Make room for the additional glyph. */
23986 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23987 g[1] = *g;
23988 glyph = it->glyph_row->glyphs[area];
23990 glyph->charpos = CHARPOS (it->position);
23991 glyph->object = object;
23992 glyph->pixel_width = width;
23993 glyph->ascent = ascent;
23994 glyph->descent = height - ascent;
23995 glyph->voffset = it->voffset;
23996 glyph->type = STRETCH_GLYPH;
23997 glyph->avoid_cursor_p = it->avoid_cursor_p;
23998 glyph->multibyte_p = it->multibyte_p;
23999 glyph->left_box_line_p = it->start_of_box_run_p;
24000 glyph->right_box_line_p = it->end_of_box_run_p;
24001 glyph->overlaps_vertically_p = 0;
24002 glyph->padding_p = 0;
24003 glyph->glyph_not_available_p = 0;
24004 glyph->face_id = it->face_id;
24005 glyph->u.stretch.ascent = ascent;
24006 glyph->u.stretch.height = height;
24007 glyph->slice.img = null_glyph_slice;
24008 glyph->font_type = FONT_TYPE_UNKNOWN;
24009 if (it->bidi_p)
24011 glyph->resolved_level = it->bidi_it.resolved_level;
24012 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24013 emacs_abort ();
24014 glyph->bidi_type = it->bidi_it.type;
24016 else
24018 glyph->resolved_level = 0;
24019 glyph->bidi_type = UNKNOWN_BT;
24021 ++it->glyph_row->used[area];
24023 else
24024 IT_EXPAND_MATRIX_WIDTH (it, area);
24027 #endif /* HAVE_WINDOW_SYSTEM */
24029 /* Produce a stretch glyph for iterator IT. IT->object is the value
24030 of the glyph property displayed. The value must be a list
24031 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24032 being recognized:
24034 1. `:width WIDTH' specifies that the space should be WIDTH *
24035 canonical char width wide. WIDTH may be an integer or floating
24036 point number.
24038 2. `:relative-width FACTOR' specifies that the width of the stretch
24039 should be computed from the width of the first character having the
24040 `glyph' property, and should be FACTOR times that width.
24042 3. `:align-to HPOS' specifies that the space should be wide enough
24043 to reach HPOS, a value in canonical character units.
24045 Exactly one of the above pairs must be present.
24047 4. `:height HEIGHT' specifies that the height of the stretch produced
24048 should be HEIGHT, measured in canonical character units.
24050 5. `:relative-height FACTOR' specifies that the height of the
24051 stretch should be FACTOR times the height of the characters having
24052 the glyph property.
24054 Either none or exactly one of 4 or 5 must be present.
24056 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24057 of the stretch should be used for the ascent of the stretch.
24058 ASCENT must be in the range 0 <= ASCENT <= 100. */
24060 void
24061 produce_stretch_glyph (struct it *it)
24063 /* (space :width WIDTH :height HEIGHT ...) */
24064 Lisp_Object prop, plist;
24065 int width = 0, height = 0, align_to = -1;
24066 int zero_width_ok_p = 0;
24067 int ascent = 0;
24068 double tem;
24069 struct face *face = NULL;
24070 struct font *font = NULL;
24072 #ifdef HAVE_WINDOW_SYSTEM
24073 int zero_height_ok_p = 0;
24075 if (FRAME_WINDOW_P (it->f))
24077 face = FACE_FROM_ID (it->f, it->face_id);
24078 font = face->font ? face->font : FRAME_FONT (it->f);
24079 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24081 #endif
24083 /* List should start with `space'. */
24084 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24085 plist = XCDR (it->object);
24087 /* Compute the width of the stretch. */
24088 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24089 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24091 /* Absolute width `:width WIDTH' specified and valid. */
24092 zero_width_ok_p = 1;
24093 width = (int)tem;
24095 #ifdef HAVE_WINDOW_SYSTEM
24096 else if (FRAME_WINDOW_P (it->f)
24097 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24099 /* Relative width `:relative-width FACTOR' specified and valid.
24100 Compute the width of the characters having the `glyph'
24101 property. */
24102 struct it it2;
24103 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24105 it2 = *it;
24106 if (it->multibyte_p)
24107 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24108 else
24110 it2.c = it2.char_to_display = *p, it2.len = 1;
24111 if (! ASCII_CHAR_P (it2.c))
24112 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24115 it2.glyph_row = NULL;
24116 it2.what = IT_CHARACTER;
24117 x_produce_glyphs (&it2);
24118 width = NUMVAL (prop) * it2.pixel_width;
24120 #endif /* HAVE_WINDOW_SYSTEM */
24121 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24122 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24124 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24125 align_to = (align_to < 0
24127 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24128 else if (align_to < 0)
24129 align_to = window_box_left_offset (it->w, TEXT_AREA);
24130 width = max (0, (int)tem + align_to - it->current_x);
24131 zero_width_ok_p = 1;
24133 else
24134 /* Nothing specified -> width defaults to canonical char width. */
24135 width = FRAME_COLUMN_WIDTH (it->f);
24137 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24138 width = 1;
24140 #ifdef HAVE_WINDOW_SYSTEM
24141 /* Compute height. */
24142 if (FRAME_WINDOW_P (it->f))
24144 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24145 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24147 height = (int)tem;
24148 zero_height_ok_p = 1;
24150 else if (prop = Fplist_get (plist, QCrelative_height),
24151 NUMVAL (prop) > 0)
24152 height = FONT_HEIGHT (font) * NUMVAL (prop);
24153 else
24154 height = FONT_HEIGHT (font);
24156 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24157 height = 1;
24159 /* Compute percentage of height used for ascent. If
24160 `:ascent ASCENT' is present and valid, use that. Otherwise,
24161 derive the ascent from the font in use. */
24162 if (prop = Fplist_get (plist, QCascent),
24163 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24164 ascent = height * NUMVAL (prop) / 100.0;
24165 else if (!NILP (prop)
24166 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24167 ascent = min (max (0, (int)tem), height);
24168 else
24169 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24171 else
24172 #endif /* HAVE_WINDOW_SYSTEM */
24173 height = 1;
24175 if (width > 0 && it->line_wrap != TRUNCATE
24176 && it->current_x + width > it->last_visible_x)
24178 width = it->last_visible_x - it->current_x;
24179 #ifdef HAVE_WINDOW_SYSTEM
24180 /* Subtract one more pixel from the stretch width, but only on
24181 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24182 width -= FRAME_WINDOW_P (it->f);
24183 #endif
24186 if (width > 0 && height > 0 && it->glyph_row)
24188 Lisp_Object o_object = it->object;
24189 Lisp_Object object = it->stack[it->sp - 1].string;
24190 int n = width;
24192 if (!STRINGP (object))
24193 object = it->w->buffer;
24194 #ifdef HAVE_WINDOW_SYSTEM
24195 if (FRAME_WINDOW_P (it->f))
24196 append_stretch_glyph (it, object, width, height, ascent);
24197 else
24198 #endif
24200 it->object = object;
24201 it->char_to_display = ' ';
24202 it->pixel_width = it->len = 1;
24203 while (n--)
24204 tty_append_glyph (it);
24205 it->object = o_object;
24209 it->pixel_width = width;
24210 #ifdef HAVE_WINDOW_SYSTEM
24211 if (FRAME_WINDOW_P (it->f))
24213 it->ascent = it->phys_ascent = ascent;
24214 it->descent = it->phys_descent = height - it->ascent;
24215 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24216 take_vertical_position_into_account (it);
24218 else
24219 #endif
24220 it->nglyphs = width;
24223 /* Get information about special display element WHAT in an
24224 environment described by IT. WHAT is one of IT_TRUNCATION or
24225 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24226 non-null glyph_row member. This function ensures that fields like
24227 face_id, c, len of IT are left untouched. */
24229 static void
24230 produce_special_glyphs (struct it *it, enum display_element_type what)
24232 struct it temp_it;
24233 Lisp_Object gc;
24234 GLYPH glyph;
24236 temp_it = *it;
24237 temp_it.object = make_number (0);
24238 memset (&temp_it.current, 0, sizeof temp_it.current);
24240 if (what == IT_CONTINUATION)
24242 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24243 if (it->bidi_it.paragraph_dir == R2L)
24244 SET_GLYPH_FROM_CHAR (glyph, '/');
24245 else
24246 SET_GLYPH_FROM_CHAR (glyph, '\\');
24247 if (it->dp
24248 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24250 /* FIXME: Should we mirror GC for R2L lines? */
24251 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24252 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24255 else if (what == IT_TRUNCATION)
24257 /* Truncation glyph. */
24258 SET_GLYPH_FROM_CHAR (glyph, '$');
24259 if (it->dp
24260 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24262 /* FIXME: Should we mirror GC for R2L lines? */
24263 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24264 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24267 else
24268 emacs_abort ();
24270 #ifdef HAVE_WINDOW_SYSTEM
24271 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24272 is turned off, we precede the truncation/continuation glyphs by a
24273 stretch glyph whose width is computed such that these special
24274 glyphs are aligned at the window margin, even when very different
24275 fonts are used in different glyph rows. */
24276 if (FRAME_WINDOW_P (temp_it.f)
24277 /* init_iterator calls this with it->glyph_row == NULL, and it
24278 wants only the pixel width of the truncation/continuation
24279 glyphs. */
24280 && temp_it.glyph_row
24281 /* insert_left_trunc_glyphs calls us at the beginning of the
24282 row, and it has its own calculation of the stretch glyph
24283 width. */
24284 && temp_it.glyph_row->used[TEXT_AREA] > 0
24285 && (temp_it.glyph_row->reversed_p
24286 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24287 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24289 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24291 if (stretch_width > 0)
24293 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24294 struct font *font =
24295 face->font ? face->font : FRAME_FONT (temp_it.f);
24296 int stretch_ascent =
24297 (((temp_it.ascent + temp_it.descent)
24298 * FONT_BASE (font)) / FONT_HEIGHT (font));
24300 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24301 temp_it.ascent + temp_it.descent,
24302 stretch_ascent);
24305 #endif
24307 temp_it.dp = NULL;
24308 temp_it.what = IT_CHARACTER;
24309 temp_it.len = 1;
24310 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24311 temp_it.face_id = GLYPH_FACE (glyph);
24312 temp_it.len = CHAR_BYTES (temp_it.c);
24314 PRODUCE_GLYPHS (&temp_it);
24315 it->pixel_width = temp_it.pixel_width;
24316 it->nglyphs = temp_it.pixel_width;
24319 #ifdef HAVE_WINDOW_SYSTEM
24321 /* Calculate line-height and line-spacing properties.
24322 An integer value specifies explicit pixel value.
24323 A float value specifies relative value to current face height.
24324 A cons (float . face-name) specifies relative value to
24325 height of specified face font.
24327 Returns height in pixels, or nil. */
24330 static Lisp_Object
24331 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24332 int boff, int override)
24334 Lisp_Object face_name = Qnil;
24335 int ascent, descent, height;
24337 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24338 return val;
24340 if (CONSP (val))
24342 face_name = XCAR (val);
24343 val = XCDR (val);
24344 if (!NUMBERP (val))
24345 val = make_number (1);
24346 if (NILP (face_name))
24348 height = it->ascent + it->descent;
24349 goto scale;
24353 if (NILP (face_name))
24355 font = FRAME_FONT (it->f);
24356 boff = FRAME_BASELINE_OFFSET (it->f);
24358 else if (EQ (face_name, Qt))
24360 override = 0;
24362 else
24364 int face_id;
24365 struct face *face;
24367 face_id = lookup_named_face (it->f, face_name, 0);
24368 if (face_id < 0)
24369 return make_number (-1);
24371 face = FACE_FROM_ID (it->f, face_id);
24372 font = face->font;
24373 if (font == NULL)
24374 return make_number (-1);
24375 boff = font->baseline_offset;
24376 if (font->vertical_centering)
24377 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24380 ascent = FONT_BASE (font) + boff;
24381 descent = FONT_DESCENT (font) - boff;
24383 if (override)
24385 it->override_ascent = ascent;
24386 it->override_descent = descent;
24387 it->override_boff = boff;
24390 height = ascent + descent;
24392 scale:
24393 if (FLOATP (val))
24394 height = (int)(XFLOAT_DATA (val) * height);
24395 else if (INTEGERP (val))
24396 height *= XINT (val);
24398 return make_number (height);
24402 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24403 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24404 and only if this is for a character for which no font was found.
24406 If the display method (it->glyphless_method) is
24407 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24408 length of the acronym or the hexadecimal string, UPPER_XOFF and
24409 UPPER_YOFF are pixel offsets for the upper part of the string,
24410 LOWER_XOFF and LOWER_YOFF are for the lower part.
24412 For the other display methods, LEN through LOWER_YOFF are zero. */
24414 static void
24415 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24416 short upper_xoff, short upper_yoff,
24417 short lower_xoff, short lower_yoff)
24419 struct glyph *glyph;
24420 enum glyph_row_area area = it->area;
24422 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24423 if (glyph < it->glyph_row->glyphs[area + 1])
24425 /* If the glyph row is reversed, we need to prepend the glyph
24426 rather than append it. */
24427 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24429 struct glyph *g;
24431 /* Make room for the additional glyph. */
24432 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24433 g[1] = *g;
24434 glyph = it->glyph_row->glyphs[area];
24436 glyph->charpos = CHARPOS (it->position);
24437 glyph->object = it->object;
24438 glyph->pixel_width = it->pixel_width;
24439 glyph->ascent = it->ascent;
24440 glyph->descent = it->descent;
24441 glyph->voffset = it->voffset;
24442 glyph->type = GLYPHLESS_GLYPH;
24443 glyph->u.glyphless.method = it->glyphless_method;
24444 glyph->u.glyphless.for_no_font = for_no_font;
24445 glyph->u.glyphless.len = len;
24446 glyph->u.glyphless.ch = it->c;
24447 glyph->slice.glyphless.upper_xoff = upper_xoff;
24448 glyph->slice.glyphless.upper_yoff = upper_yoff;
24449 glyph->slice.glyphless.lower_xoff = lower_xoff;
24450 glyph->slice.glyphless.lower_yoff = lower_yoff;
24451 glyph->avoid_cursor_p = it->avoid_cursor_p;
24452 glyph->multibyte_p = it->multibyte_p;
24453 glyph->left_box_line_p = it->start_of_box_run_p;
24454 glyph->right_box_line_p = it->end_of_box_run_p;
24455 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24456 || it->phys_descent > it->descent);
24457 glyph->padding_p = 0;
24458 glyph->glyph_not_available_p = 0;
24459 glyph->face_id = face_id;
24460 glyph->font_type = FONT_TYPE_UNKNOWN;
24461 if (it->bidi_p)
24463 glyph->resolved_level = it->bidi_it.resolved_level;
24464 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24465 emacs_abort ();
24466 glyph->bidi_type = it->bidi_it.type;
24468 ++it->glyph_row->used[area];
24470 else
24471 IT_EXPAND_MATRIX_WIDTH (it, area);
24475 /* Produce a glyph for a glyphless character for iterator IT.
24476 IT->glyphless_method specifies which method to use for displaying
24477 the character. See the description of enum
24478 glyphless_display_method in dispextern.h for the detail.
24480 FOR_NO_FONT is nonzero if and only if this is for a character for
24481 which no font was found. ACRONYM, if non-nil, is an acronym string
24482 for the character. */
24484 static void
24485 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24487 int face_id;
24488 struct face *face;
24489 struct font *font;
24490 int base_width, base_height, width, height;
24491 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24492 int len;
24494 /* Get the metrics of the base font. We always refer to the current
24495 ASCII face. */
24496 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24497 font = face->font ? face->font : FRAME_FONT (it->f);
24498 it->ascent = FONT_BASE (font) + font->baseline_offset;
24499 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24500 base_height = it->ascent + it->descent;
24501 base_width = font->average_width;
24503 /* Get a face ID for the glyph by utilizing a cache (the same way as
24504 done for `escape-glyph' in get_next_display_element). */
24505 if (it->f == last_glyphless_glyph_frame
24506 && it->face_id == last_glyphless_glyph_face_id)
24508 face_id = last_glyphless_glyph_merged_face_id;
24510 else
24512 /* Merge the `glyphless-char' face into the current face. */
24513 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24514 last_glyphless_glyph_frame = it->f;
24515 last_glyphless_glyph_face_id = it->face_id;
24516 last_glyphless_glyph_merged_face_id = face_id;
24519 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24521 it->pixel_width = THIN_SPACE_WIDTH;
24522 len = 0;
24523 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24525 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24527 width = CHAR_WIDTH (it->c);
24528 if (width == 0)
24529 width = 1;
24530 else if (width > 4)
24531 width = 4;
24532 it->pixel_width = base_width * width;
24533 len = 0;
24534 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24536 else
24538 char buf[7];
24539 const char *str;
24540 unsigned int code[6];
24541 int upper_len;
24542 int ascent, descent;
24543 struct font_metrics metrics_upper, metrics_lower;
24545 face = FACE_FROM_ID (it->f, face_id);
24546 font = face->font ? face->font : FRAME_FONT (it->f);
24547 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24549 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24551 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24552 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24553 if (CONSP (acronym))
24554 acronym = XCAR (acronym);
24555 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24557 else
24559 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24560 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24561 str = buf;
24563 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24564 code[len] = font->driver->encode_char (font, str[len]);
24565 upper_len = (len + 1) / 2;
24566 font->driver->text_extents (font, code, upper_len,
24567 &metrics_upper);
24568 font->driver->text_extents (font, code + upper_len, len - upper_len,
24569 &metrics_lower);
24573 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24574 width = max (metrics_upper.width, metrics_lower.width) + 4;
24575 upper_xoff = upper_yoff = 2; /* the typical case */
24576 if (base_width >= width)
24578 /* Align the upper to the left, the lower to the right. */
24579 it->pixel_width = base_width;
24580 lower_xoff = base_width - 2 - metrics_lower.width;
24582 else
24584 /* Center the shorter one. */
24585 it->pixel_width = width;
24586 if (metrics_upper.width >= metrics_lower.width)
24587 lower_xoff = (width - metrics_lower.width) / 2;
24588 else
24590 /* FIXME: This code doesn't look right. It formerly was
24591 missing the "lower_xoff = 0;", which couldn't have
24592 been right since it left lower_xoff uninitialized. */
24593 lower_xoff = 0;
24594 upper_xoff = (width - metrics_upper.width) / 2;
24598 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24599 top, bottom, and between upper and lower strings. */
24600 height = (metrics_upper.ascent + metrics_upper.descent
24601 + metrics_lower.ascent + metrics_lower.descent) + 5;
24602 /* Center vertically.
24603 H:base_height, D:base_descent
24604 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24606 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24607 descent = D - H/2 + h/2;
24608 lower_yoff = descent - 2 - ld;
24609 upper_yoff = lower_yoff - la - 1 - ud; */
24610 ascent = - (it->descent - (base_height + height + 1) / 2);
24611 descent = it->descent - (base_height - height) / 2;
24612 lower_yoff = descent - 2 - metrics_lower.descent;
24613 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24614 - metrics_upper.descent);
24615 /* Don't make the height shorter than the base height. */
24616 if (height > base_height)
24618 it->ascent = ascent;
24619 it->descent = descent;
24623 it->phys_ascent = it->ascent;
24624 it->phys_descent = it->descent;
24625 if (it->glyph_row)
24626 append_glyphless_glyph (it, face_id, for_no_font, len,
24627 upper_xoff, upper_yoff,
24628 lower_xoff, lower_yoff);
24629 it->nglyphs = 1;
24630 take_vertical_position_into_account (it);
24634 /* RIF:
24635 Produce glyphs/get display metrics for the display element IT is
24636 loaded with. See the description of struct it in dispextern.h
24637 for an overview of struct it. */
24639 void
24640 x_produce_glyphs (struct it *it)
24642 int extra_line_spacing = it->extra_line_spacing;
24644 it->glyph_not_available_p = 0;
24646 if (it->what == IT_CHARACTER)
24648 XChar2b char2b;
24649 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24650 struct font *font = face->font;
24651 struct font_metrics *pcm = NULL;
24652 int boff; /* baseline offset */
24654 if (font == NULL)
24656 /* When no suitable font is found, display this character by
24657 the method specified in the first extra slot of
24658 Vglyphless_char_display. */
24659 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24661 eassert (it->what == IT_GLYPHLESS);
24662 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24663 goto done;
24666 boff = font->baseline_offset;
24667 if (font->vertical_centering)
24668 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24670 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24672 int stretched_p;
24674 it->nglyphs = 1;
24676 if (it->override_ascent >= 0)
24678 it->ascent = it->override_ascent;
24679 it->descent = it->override_descent;
24680 boff = it->override_boff;
24682 else
24684 it->ascent = FONT_BASE (font) + boff;
24685 it->descent = FONT_DESCENT (font) - boff;
24688 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24690 pcm = get_per_char_metric (font, &char2b);
24691 if (pcm->width == 0
24692 && pcm->rbearing == 0 && pcm->lbearing == 0)
24693 pcm = NULL;
24696 if (pcm)
24698 it->phys_ascent = pcm->ascent + boff;
24699 it->phys_descent = pcm->descent - boff;
24700 it->pixel_width = pcm->width;
24702 else
24704 it->glyph_not_available_p = 1;
24705 it->phys_ascent = it->ascent;
24706 it->phys_descent = it->descent;
24707 it->pixel_width = font->space_width;
24710 if (it->constrain_row_ascent_descent_p)
24712 if (it->descent > it->max_descent)
24714 it->ascent += it->descent - it->max_descent;
24715 it->descent = it->max_descent;
24717 if (it->ascent > it->max_ascent)
24719 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24720 it->ascent = it->max_ascent;
24722 it->phys_ascent = min (it->phys_ascent, it->ascent);
24723 it->phys_descent = min (it->phys_descent, it->descent);
24724 extra_line_spacing = 0;
24727 /* If this is a space inside a region of text with
24728 `space-width' property, change its width. */
24729 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24730 if (stretched_p)
24731 it->pixel_width *= XFLOATINT (it->space_width);
24733 /* If face has a box, add the box thickness to the character
24734 height. If character has a box line to the left and/or
24735 right, add the box line width to the character's width. */
24736 if (face->box != FACE_NO_BOX)
24738 int thick = face->box_line_width;
24740 if (thick > 0)
24742 it->ascent += thick;
24743 it->descent += thick;
24745 else
24746 thick = -thick;
24748 if (it->start_of_box_run_p)
24749 it->pixel_width += thick;
24750 if (it->end_of_box_run_p)
24751 it->pixel_width += thick;
24754 /* If face has an overline, add the height of the overline
24755 (1 pixel) and a 1 pixel margin to the character height. */
24756 if (face->overline_p)
24757 it->ascent += overline_margin;
24759 if (it->constrain_row_ascent_descent_p)
24761 if (it->ascent > it->max_ascent)
24762 it->ascent = it->max_ascent;
24763 if (it->descent > it->max_descent)
24764 it->descent = it->max_descent;
24767 take_vertical_position_into_account (it);
24769 /* If we have to actually produce glyphs, do it. */
24770 if (it->glyph_row)
24772 if (stretched_p)
24774 /* Translate a space with a `space-width' property
24775 into a stretch glyph. */
24776 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24777 / FONT_HEIGHT (font));
24778 append_stretch_glyph (it, it->object, it->pixel_width,
24779 it->ascent + it->descent, ascent);
24781 else
24782 append_glyph (it);
24784 /* If characters with lbearing or rbearing are displayed
24785 in this line, record that fact in a flag of the
24786 glyph row. This is used to optimize X output code. */
24787 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24788 it->glyph_row->contains_overlapping_glyphs_p = 1;
24790 if (! stretched_p && it->pixel_width == 0)
24791 /* We assure that all visible glyphs have at least 1-pixel
24792 width. */
24793 it->pixel_width = 1;
24795 else if (it->char_to_display == '\n')
24797 /* A newline has no width, but we need the height of the
24798 line. But if previous part of the line sets a height,
24799 don't increase that height */
24801 Lisp_Object height;
24802 Lisp_Object total_height = Qnil;
24804 it->override_ascent = -1;
24805 it->pixel_width = 0;
24806 it->nglyphs = 0;
24808 height = get_it_property (it, Qline_height);
24809 /* Split (line-height total-height) list */
24810 if (CONSP (height)
24811 && CONSP (XCDR (height))
24812 && NILP (XCDR (XCDR (height))))
24814 total_height = XCAR (XCDR (height));
24815 height = XCAR (height);
24817 height = calc_line_height_property (it, height, font, boff, 1);
24819 if (it->override_ascent >= 0)
24821 it->ascent = it->override_ascent;
24822 it->descent = it->override_descent;
24823 boff = it->override_boff;
24825 else
24827 it->ascent = FONT_BASE (font) + boff;
24828 it->descent = FONT_DESCENT (font) - boff;
24831 if (EQ (height, Qt))
24833 if (it->descent > it->max_descent)
24835 it->ascent += it->descent - it->max_descent;
24836 it->descent = it->max_descent;
24838 if (it->ascent > it->max_ascent)
24840 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24841 it->ascent = it->max_ascent;
24843 it->phys_ascent = min (it->phys_ascent, it->ascent);
24844 it->phys_descent = min (it->phys_descent, it->descent);
24845 it->constrain_row_ascent_descent_p = 1;
24846 extra_line_spacing = 0;
24848 else
24850 Lisp_Object spacing;
24852 it->phys_ascent = it->ascent;
24853 it->phys_descent = it->descent;
24855 if ((it->max_ascent > 0 || it->max_descent > 0)
24856 && face->box != FACE_NO_BOX
24857 && face->box_line_width > 0)
24859 it->ascent += face->box_line_width;
24860 it->descent += face->box_line_width;
24862 if (!NILP (height)
24863 && XINT (height) > it->ascent + it->descent)
24864 it->ascent = XINT (height) - it->descent;
24866 if (!NILP (total_height))
24867 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24868 else
24870 spacing = get_it_property (it, Qline_spacing);
24871 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24873 if (INTEGERP (spacing))
24875 extra_line_spacing = XINT (spacing);
24876 if (!NILP (total_height))
24877 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24881 else /* i.e. (it->char_to_display == '\t') */
24883 if (font->space_width > 0)
24885 int tab_width = it->tab_width * font->space_width;
24886 int x = it->current_x + it->continuation_lines_width;
24887 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24889 /* If the distance from the current position to the next tab
24890 stop is less than a space character width, use the
24891 tab stop after that. */
24892 if (next_tab_x - x < font->space_width)
24893 next_tab_x += tab_width;
24895 it->pixel_width = next_tab_x - x;
24896 it->nglyphs = 1;
24897 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24898 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24900 if (it->glyph_row)
24902 append_stretch_glyph (it, it->object, it->pixel_width,
24903 it->ascent + it->descent, it->ascent);
24906 else
24908 it->pixel_width = 0;
24909 it->nglyphs = 1;
24913 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24915 /* A static composition.
24917 Note: A composition is represented as one glyph in the
24918 glyph matrix. There are no padding glyphs.
24920 Important note: pixel_width, ascent, and descent are the
24921 values of what is drawn by draw_glyphs (i.e. the values of
24922 the overall glyphs composed). */
24923 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24924 int boff; /* baseline offset */
24925 struct composition *cmp = composition_table[it->cmp_it.id];
24926 int glyph_len = cmp->glyph_len;
24927 struct font *font = face->font;
24929 it->nglyphs = 1;
24931 /* If we have not yet calculated pixel size data of glyphs of
24932 the composition for the current face font, calculate them
24933 now. Theoretically, we have to check all fonts for the
24934 glyphs, but that requires much time and memory space. So,
24935 here we check only the font of the first glyph. This may
24936 lead to incorrect display, but it's very rare, and C-l
24937 (recenter-top-bottom) can correct the display anyway. */
24938 if (! cmp->font || cmp->font != font)
24940 /* Ascent and descent of the font of the first character
24941 of this composition (adjusted by baseline offset).
24942 Ascent and descent of overall glyphs should not be less
24943 than these, respectively. */
24944 int font_ascent, font_descent, font_height;
24945 /* Bounding box of the overall glyphs. */
24946 int leftmost, rightmost, lowest, highest;
24947 int lbearing, rbearing;
24948 int i, width, ascent, descent;
24949 int left_padded = 0, right_padded = 0;
24950 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24951 XChar2b char2b;
24952 struct font_metrics *pcm;
24953 int font_not_found_p;
24954 ptrdiff_t pos;
24956 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24957 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24958 break;
24959 if (glyph_len < cmp->glyph_len)
24960 right_padded = 1;
24961 for (i = 0; i < glyph_len; i++)
24963 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24964 break;
24965 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24967 if (i > 0)
24968 left_padded = 1;
24970 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24971 : IT_CHARPOS (*it));
24972 /* If no suitable font is found, use the default font. */
24973 font_not_found_p = font == NULL;
24974 if (font_not_found_p)
24976 face = face->ascii_face;
24977 font = face->font;
24979 boff = font->baseline_offset;
24980 if (font->vertical_centering)
24981 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24982 font_ascent = FONT_BASE (font) + boff;
24983 font_descent = FONT_DESCENT (font) - boff;
24984 font_height = FONT_HEIGHT (font);
24986 cmp->font = font;
24988 pcm = NULL;
24989 if (! font_not_found_p)
24991 get_char_face_and_encoding (it->f, c, it->face_id,
24992 &char2b, 0);
24993 pcm = get_per_char_metric (font, &char2b);
24996 /* Initialize the bounding box. */
24997 if (pcm)
24999 width = cmp->glyph_len > 0 ? pcm->width : 0;
25000 ascent = pcm->ascent;
25001 descent = pcm->descent;
25002 lbearing = pcm->lbearing;
25003 rbearing = pcm->rbearing;
25005 else
25007 width = cmp->glyph_len > 0 ? font->space_width : 0;
25008 ascent = FONT_BASE (font);
25009 descent = FONT_DESCENT (font);
25010 lbearing = 0;
25011 rbearing = width;
25014 rightmost = width;
25015 leftmost = 0;
25016 lowest = - descent + boff;
25017 highest = ascent + boff;
25019 if (! font_not_found_p
25020 && font->default_ascent
25021 && CHAR_TABLE_P (Vuse_default_ascent)
25022 && !NILP (Faref (Vuse_default_ascent,
25023 make_number (it->char_to_display))))
25024 highest = font->default_ascent + boff;
25026 /* Draw the first glyph at the normal position. It may be
25027 shifted to right later if some other glyphs are drawn
25028 at the left. */
25029 cmp->offsets[i * 2] = 0;
25030 cmp->offsets[i * 2 + 1] = boff;
25031 cmp->lbearing = lbearing;
25032 cmp->rbearing = rbearing;
25034 /* Set cmp->offsets for the remaining glyphs. */
25035 for (i++; i < glyph_len; i++)
25037 int left, right, btm, top;
25038 int ch = COMPOSITION_GLYPH (cmp, i);
25039 int face_id;
25040 struct face *this_face;
25042 if (ch == '\t')
25043 ch = ' ';
25044 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25045 this_face = FACE_FROM_ID (it->f, face_id);
25046 font = this_face->font;
25048 if (font == NULL)
25049 pcm = NULL;
25050 else
25052 get_char_face_and_encoding (it->f, ch, face_id,
25053 &char2b, 0);
25054 pcm = get_per_char_metric (font, &char2b);
25056 if (! pcm)
25057 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25058 else
25060 width = pcm->width;
25061 ascent = pcm->ascent;
25062 descent = pcm->descent;
25063 lbearing = pcm->lbearing;
25064 rbearing = pcm->rbearing;
25065 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25067 /* Relative composition with or without
25068 alternate chars. */
25069 left = (leftmost + rightmost - width) / 2;
25070 btm = - descent + boff;
25071 if (font->relative_compose
25072 && (! CHAR_TABLE_P (Vignore_relative_composition)
25073 || NILP (Faref (Vignore_relative_composition,
25074 make_number (ch)))))
25077 if (- descent >= font->relative_compose)
25078 /* One extra pixel between two glyphs. */
25079 btm = highest + 1;
25080 else if (ascent <= 0)
25081 /* One extra pixel between two glyphs. */
25082 btm = lowest - 1 - ascent - descent;
25085 else
25087 /* A composition rule is specified by an integer
25088 value that encodes global and new reference
25089 points (GREF and NREF). GREF and NREF are
25090 specified by numbers as below:
25092 0---1---2 -- ascent
25096 9--10--11 -- center
25098 ---3---4---5--- baseline
25100 6---7---8 -- descent
25102 int rule = COMPOSITION_RULE (cmp, i);
25103 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25105 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25106 grefx = gref % 3, nrefx = nref % 3;
25107 grefy = gref / 3, nrefy = nref / 3;
25108 if (xoff)
25109 xoff = font_height * (xoff - 128) / 256;
25110 if (yoff)
25111 yoff = font_height * (yoff - 128) / 256;
25113 left = (leftmost
25114 + grefx * (rightmost - leftmost) / 2
25115 - nrefx * width / 2
25116 + xoff);
25118 btm = ((grefy == 0 ? highest
25119 : grefy == 1 ? 0
25120 : grefy == 2 ? lowest
25121 : (highest + lowest) / 2)
25122 - (nrefy == 0 ? ascent + descent
25123 : nrefy == 1 ? descent - boff
25124 : nrefy == 2 ? 0
25125 : (ascent + descent) / 2)
25126 + yoff);
25129 cmp->offsets[i * 2] = left;
25130 cmp->offsets[i * 2 + 1] = btm + descent;
25132 /* Update the bounding box of the overall glyphs. */
25133 if (width > 0)
25135 right = left + width;
25136 if (left < leftmost)
25137 leftmost = left;
25138 if (right > rightmost)
25139 rightmost = right;
25141 top = btm + descent + ascent;
25142 if (top > highest)
25143 highest = top;
25144 if (btm < lowest)
25145 lowest = btm;
25147 if (cmp->lbearing > left + lbearing)
25148 cmp->lbearing = left + lbearing;
25149 if (cmp->rbearing < left + rbearing)
25150 cmp->rbearing = left + rbearing;
25154 /* If there are glyphs whose x-offsets are negative,
25155 shift all glyphs to the right and make all x-offsets
25156 non-negative. */
25157 if (leftmost < 0)
25159 for (i = 0; i < cmp->glyph_len; i++)
25160 cmp->offsets[i * 2] -= leftmost;
25161 rightmost -= leftmost;
25162 cmp->lbearing -= leftmost;
25163 cmp->rbearing -= leftmost;
25166 if (left_padded && cmp->lbearing < 0)
25168 for (i = 0; i < cmp->glyph_len; i++)
25169 cmp->offsets[i * 2] -= cmp->lbearing;
25170 rightmost -= cmp->lbearing;
25171 cmp->rbearing -= cmp->lbearing;
25172 cmp->lbearing = 0;
25174 if (right_padded && rightmost < cmp->rbearing)
25176 rightmost = cmp->rbearing;
25179 cmp->pixel_width = rightmost;
25180 cmp->ascent = highest;
25181 cmp->descent = - lowest;
25182 if (cmp->ascent < font_ascent)
25183 cmp->ascent = font_ascent;
25184 if (cmp->descent < font_descent)
25185 cmp->descent = font_descent;
25188 if (it->glyph_row
25189 && (cmp->lbearing < 0
25190 || cmp->rbearing > cmp->pixel_width))
25191 it->glyph_row->contains_overlapping_glyphs_p = 1;
25193 it->pixel_width = cmp->pixel_width;
25194 it->ascent = it->phys_ascent = cmp->ascent;
25195 it->descent = it->phys_descent = cmp->descent;
25196 if (face->box != FACE_NO_BOX)
25198 int thick = face->box_line_width;
25200 if (thick > 0)
25202 it->ascent += thick;
25203 it->descent += thick;
25205 else
25206 thick = - thick;
25208 if (it->start_of_box_run_p)
25209 it->pixel_width += thick;
25210 if (it->end_of_box_run_p)
25211 it->pixel_width += thick;
25214 /* If face has an overline, add the height of the overline
25215 (1 pixel) and a 1 pixel margin to the character height. */
25216 if (face->overline_p)
25217 it->ascent += overline_margin;
25219 take_vertical_position_into_account (it);
25220 if (it->ascent < 0)
25221 it->ascent = 0;
25222 if (it->descent < 0)
25223 it->descent = 0;
25225 if (it->glyph_row && cmp->glyph_len > 0)
25226 append_composite_glyph (it);
25228 else if (it->what == IT_COMPOSITION)
25230 /* A dynamic (automatic) composition. */
25231 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25232 Lisp_Object gstring;
25233 struct font_metrics metrics;
25235 it->nglyphs = 1;
25237 gstring = composition_gstring_from_id (it->cmp_it.id);
25238 it->pixel_width
25239 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25240 &metrics);
25241 if (it->glyph_row
25242 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25243 it->glyph_row->contains_overlapping_glyphs_p = 1;
25244 it->ascent = it->phys_ascent = metrics.ascent;
25245 it->descent = it->phys_descent = metrics.descent;
25246 if (face->box != FACE_NO_BOX)
25248 int thick = face->box_line_width;
25250 if (thick > 0)
25252 it->ascent += thick;
25253 it->descent += thick;
25255 else
25256 thick = - thick;
25258 if (it->start_of_box_run_p)
25259 it->pixel_width += thick;
25260 if (it->end_of_box_run_p)
25261 it->pixel_width += thick;
25263 /* If face has an overline, add the height of the overline
25264 (1 pixel) and a 1 pixel margin to the character height. */
25265 if (face->overline_p)
25266 it->ascent += overline_margin;
25267 take_vertical_position_into_account (it);
25268 if (it->ascent < 0)
25269 it->ascent = 0;
25270 if (it->descent < 0)
25271 it->descent = 0;
25273 if (it->glyph_row)
25274 append_composite_glyph (it);
25276 else if (it->what == IT_GLYPHLESS)
25277 produce_glyphless_glyph (it, 0, Qnil);
25278 else if (it->what == IT_IMAGE)
25279 produce_image_glyph (it);
25280 else if (it->what == IT_STRETCH)
25281 produce_stretch_glyph (it);
25283 done:
25284 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25285 because this isn't true for images with `:ascent 100'. */
25286 eassert (it->ascent >= 0 && it->descent >= 0);
25287 if (it->area == TEXT_AREA)
25288 it->current_x += it->pixel_width;
25290 if (extra_line_spacing > 0)
25292 it->descent += extra_line_spacing;
25293 if (extra_line_spacing > it->max_extra_line_spacing)
25294 it->max_extra_line_spacing = extra_line_spacing;
25297 it->max_ascent = max (it->max_ascent, it->ascent);
25298 it->max_descent = max (it->max_descent, it->descent);
25299 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25300 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25303 /* EXPORT for RIF:
25304 Output LEN glyphs starting at START at the nominal cursor position.
25305 Advance the nominal cursor over the text. The global variable
25306 updated_window contains the window being updated, updated_row is
25307 the glyph row being updated, and updated_area is the area of that
25308 row being updated. */
25310 void
25311 x_write_glyphs (struct glyph *start, int len)
25313 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25315 eassert (updated_window && updated_row);
25316 /* When the window is hscrolled, cursor hpos can legitimately be out
25317 of bounds, but we draw the cursor at the corresponding window
25318 margin in that case. */
25319 if (!updated_row->reversed_p && chpos < 0)
25320 chpos = 0;
25321 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25322 chpos = updated_row->used[TEXT_AREA] - 1;
25324 BLOCK_INPUT;
25326 /* Write glyphs. */
25328 hpos = start - updated_row->glyphs[updated_area];
25329 x = draw_glyphs (updated_window, output_cursor.x,
25330 updated_row, updated_area,
25331 hpos, hpos + len,
25332 DRAW_NORMAL_TEXT, 0);
25334 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25335 if (updated_area == TEXT_AREA
25336 && updated_window->phys_cursor_on_p
25337 && updated_window->phys_cursor.vpos == output_cursor.vpos
25338 && chpos >= hpos
25339 && chpos < hpos + len)
25340 updated_window->phys_cursor_on_p = 0;
25342 UNBLOCK_INPUT;
25344 /* Advance the output cursor. */
25345 output_cursor.hpos += len;
25346 output_cursor.x = x;
25350 /* EXPORT for RIF:
25351 Insert LEN glyphs from START at the nominal cursor position. */
25353 void
25354 x_insert_glyphs (struct glyph *start, int len)
25356 struct frame *f;
25357 struct window *w;
25358 int line_height, shift_by_width, shifted_region_width;
25359 struct glyph_row *row;
25360 struct glyph *glyph;
25361 int frame_x, frame_y;
25362 ptrdiff_t hpos;
25364 eassert (updated_window && updated_row);
25365 BLOCK_INPUT;
25366 w = updated_window;
25367 f = XFRAME (WINDOW_FRAME (w));
25369 /* Get the height of the line we are in. */
25370 row = updated_row;
25371 line_height = row->height;
25373 /* Get the width of the glyphs to insert. */
25374 shift_by_width = 0;
25375 for (glyph = start; glyph < start + len; ++glyph)
25376 shift_by_width += glyph->pixel_width;
25378 /* Get the width of the region to shift right. */
25379 shifted_region_width = (window_box_width (w, updated_area)
25380 - output_cursor.x
25381 - shift_by_width);
25383 /* Shift right. */
25384 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25385 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25387 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25388 line_height, shift_by_width);
25390 /* Write the glyphs. */
25391 hpos = start - row->glyphs[updated_area];
25392 draw_glyphs (w, output_cursor.x, row, updated_area,
25393 hpos, hpos + len,
25394 DRAW_NORMAL_TEXT, 0);
25396 /* Advance the output cursor. */
25397 output_cursor.hpos += len;
25398 output_cursor.x += shift_by_width;
25399 UNBLOCK_INPUT;
25403 /* EXPORT for RIF:
25404 Erase the current text line from the nominal cursor position
25405 (inclusive) to pixel column TO_X (exclusive). The idea is that
25406 everything from TO_X onward is already erased.
25408 TO_X is a pixel position relative to updated_area of
25409 updated_window. TO_X == -1 means clear to the end of this area. */
25411 void
25412 x_clear_end_of_line (int to_x)
25414 struct frame *f;
25415 struct window *w = updated_window;
25416 int max_x, min_y, max_y;
25417 int from_x, from_y, to_y;
25419 eassert (updated_window && updated_row);
25420 f = XFRAME (w->frame);
25422 if (updated_row->full_width_p)
25423 max_x = WINDOW_TOTAL_WIDTH (w);
25424 else
25425 max_x = window_box_width (w, updated_area);
25426 max_y = window_text_bottom_y (w);
25428 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25429 of window. For TO_X > 0, truncate to end of drawing area. */
25430 if (to_x == 0)
25431 return;
25432 else if (to_x < 0)
25433 to_x = max_x;
25434 else
25435 to_x = min (to_x, max_x);
25437 to_y = min (max_y, output_cursor.y + updated_row->height);
25439 /* Notice if the cursor will be cleared by this operation. */
25440 if (!updated_row->full_width_p)
25441 notice_overwritten_cursor (w, updated_area,
25442 output_cursor.x, -1,
25443 updated_row->y,
25444 MATRIX_ROW_BOTTOM_Y (updated_row));
25446 from_x = output_cursor.x;
25448 /* Translate to frame coordinates. */
25449 if (updated_row->full_width_p)
25451 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25452 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25454 else
25456 int area_left = window_box_left (w, updated_area);
25457 from_x += area_left;
25458 to_x += area_left;
25461 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25462 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25463 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25465 /* Prevent inadvertently clearing to end of the X window. */
25466 if (to_x > from_x && to_y > from_y)
25468 BLOCK_INPUT;
25469 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25470 to_x - from_x, to_y - from_y);
25471 UNBLOCK_INPUT;
25475 #endif /* HAVE_WINDOW_SYSTEM */
25479 /***********************************************************************
25480 Cursor types
25481 ***********************************************************************/
25483 /* Value is the internal representation of the specified cursor type
25484 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25485 of the bar cursor. */
25487 static enum text_cursor_kinds
25488 get_specified_cursor_type (Lisp_Object arg, int *width)
25490 enum text_cursor_kinds type;
25492 if (NILP (arg))
25493 return NO_CURSOR;
25495 if (EQ (arg, Qbox))
25496 return FILLED_BOX_CURSOR;
25498 if (EQ (arg, Qhollow))
25499 return HOLLOW_BOX_CURSOR;
25501 if (EQ (arg, Qbar))
25503 *width = 2;
25504 return BAR_CURSOR;
25507 if (CONSP (arg)
25508 && EQ (XCAR (arg), Qbar)
25509 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25511 *width = XINT (XCDR (arg));
25512 return BAR_CURSOR;
25515 if (EQ (arg, Qhbar))
25517 *width = 2;
25518 return HBAR_CURSOR;
25521 if (CONSP (arg)
25522 && EQ (XCAR (arg), Qhbar)
25523 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25525 *width = XINT (XCDR (arg));
25526 return HBAR_CURSOR;
25529 /* Treat anything unknown as "hollow box cursor".
25530 It was bad to signal an error; people have trouble fixing
25531 .Xdefaults with Emacs, when it has something bad in it. */
25532 type = HOLLOW_BOX_CURSOR;
25534 return type;
25537 /* Set the default cursor types for specified frame. */
25538 void
25539 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25541 int width = 1;
25542 Lisp_Object tem;
25544 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25545 FRAME_CURSOR_WIDTH (f) = width;
25547 /* By default, set up the blink-off state depending on the on-state. */
25549 tem = Fassoc (arg, Vblink_cursor_alist);
25550 if (!NILP (tem))
25552 FRAME_BLINK_OFF_CURSOR (f)
25553 = get_specified_cursor_type (XCDR (tem), &width);
25554 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25556 else
25557 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25561 #ifdef HAVE_WINDOW_SYSTEM
25563 /* Return the cursor we want to be displayed in window W. Return
25564 width of bar/hbar cursor through WIDTH arg. Return with
25565 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25566 (i.e. if the `system caret' should track this cursor).
25568 In a mini-buffer window, we want the cursor only to appear if we
25569 are reading input from this window. For the selected window, we
25570 want the cursor type given by the frame parameter or buffer local
25571 setting of cursor-type. If explicitly marked off, draw no cursor.
25572 In all other cases, we want a hollow box cursor. */
25574 static enum text_cursor_kinds
25575 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25576 int *active_cursor)
25578 struct frame *f = XFRAME (w->frame);
25579 struct buffer *b = XBUFFER (w->buffer);
25580 int cursor_type = DEFAULT_CURSOR;
25581 Lisp_Object alt_cursor;
25582 int non_selected = 0;
25584 *active_cursor = 1;
25586 /* Echo area */
25587 if (cursor_in_echo_area
25588 && FRAME_HAS_MINIBUF_P (f)
25589 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25591 if (w == XWINDOW (echo_area_window))
25593 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25595 *width = FRAME_CURSOR_WIDTH (f);
25596 return FRAME_DESIRED_CURSOR (f);
25598 else
25599 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25602 *active_cursor = 0;
25603 non_selected = 1;
25606 /* Detect a nonselected window or nonselected frame. */
25607 else if (w != XWINDOW (f->selected_window)
25608 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25610 *active_cursor = 0;
25612 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25613 return NO_CURSOR;
25615 non_selected = 1;
25618 /* Never display a cursor in a window in which cursor-type is nil. */
25619 if (NILP (BVAR (b, cursor_type)))
25620 return NO_CURSOR;
25622 /* Get the normal cursor type for this window. */
25623 if (EQ (BVAR (b, cursor_type), Qt))
25625 cursor_type = FRAME_DESIRED_CURSOR (f);
25626 *width = FRAME_CURSOR_WIDTH (f);
25628 else
25629 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25631 /* Use cursor-in-non-selected-windows instead
25632 for non-selected window or frame. */
25633 if (non_selected)
25635 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25636 if (!EQ (Qt, alt_cursor))
25637 return get_specified_cursor_type (alt_cursor, width);
25638 /* t means modify the normal cursor type. */
25639 if (cursor_type == FILLED_BOX_CURSOR)
25640 cursor_type = HOLLOW_BOX_CURSOR;
25641 else if (cursor_type == BAR_CURSOR && *width > 1)
25642 --*width;
25643 return cursor_type;
25646 /* Use normal cursor if not blinked off. */
25647 if (!w->cursor_off_p)
25649 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25651 if (cursor_type == FILLED_BOX_CURSOR)
25653 /* Using a block cursor on large images can be very annoying.
25654 So use a hollow cursor for "large" images.
25655 If image is not transparent (no mask), also use hollow cursor. */
25656 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25657 if (img != NULL && IMAGEP (img->spec))
25659 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25660 where N = size of default frame font size.
25661 This should cover most of the "tiny" icons people may use. */
25662 if (!img->mask
25663 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25664 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25665 cursor_type = HOLLOW_BOX_CURSOR;
25668 else if (cursor_type != NO_CURSOR)
25670 /* Display current only supports BOX and HOLLOW cursors for images.
25671 So for now, unconditionally use a HOLLOW cursor when cursor is
25672 not a solid box cursor. */
25673 cursor_type = HOLLOW_BOX_CURSOR;
25676 return cursor_type;
25679 /* Cursor is blinked off, so determine how to "toggle" it. */
25681 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25682 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25683 return get_specified_cursor_type (XCDR (alt_cursor), width);
25685 /* Then see if frame has specified a specific blink off cursor type. */
25686 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25688 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25689 return FRAME_BLINK_OFF_CURSOR (f);
25692 #if 0
25693 /* Some people liked having a permanently visible blinking cursor,
25694 while others had very strong opinions against it. So it was
25695 decided to remove it. KFS 2003-09-03 */
25697 /* Finally perform built-in cursor blinking:
25698 filled box <-> hollow box
25699 wide [h]bar <-> narrow [h]bar
25700 narrow [h]bar <-> no cursor
25701 other type <-> no cursor */
25703 if (cursor_type == FILLED_BOX_CURSOR)
25704 return HOLLOW_BOX_CURSOR;
25706 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25708 *width = 1;
25709 return cursor_type;
25711 #endif
25713 return NO_CURSOR;
25717 /* Notice when the text cursor of window W has been completely
25718 overwritten by a drawing operation that outputs glyphs in AREA
25719 starting at X0 and ending at X1 in the line starting at Y0 and
25720 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25721 the rest of the line after X0 has been written. Y coordinates
25722 are window-relative. */
25724 static void
25725 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25726 int x0, int x1, int y0, int y1)
25728 int cx0, cx1, cy0, cy1;
25729 struct glyph_row *row;
25731 if (!w->phys_cursor_on_p)
25732 return;
25733 if (area != TEXT_AREA)
25734 return;
25736 if (w->phys_cursor.vpos < 0
25737 || w->phys_cursor.vpos >= w->current_matrix->nrows
25738 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25739 !(row->enabled_p && row->displays_text_p)))
25740 return;
25742 if (row->cursor_in_fringe_p)
25744 row->cursor_in_fringe_p = 0;
25745 draw_fringe_bitmap (w, row, row->reversed_p);
25746 w->phys_cursor_on_p = 0;
25747 return;
25750 cx0 = w->phys_cursor.x;
25751 cx1 = cx0 + w->phys_cursor_width;
25752 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25753 return;
25755 /* The cursor image will be completely removed from the
25756 screen if the output area intersects the cursor area in
25757 y-direction. When we draw in [y0 y1[, and some part of
25758 the cursor is at y < y0, that part must have been drawn
25759 before. When scrolling, the cursor is erased before
25760 actually scrolling, so we don't come here. When not
25761 scrolling, the rows above the old cursor row must have
25762 changed, and in this case these rows must have written
25763 over the cursor image.
25765 Likewise if part of the cursor is below y1, with the
25766 exception of the cursor being in the first blank row at
25767 the buffer and window end because update_text_area
25768 doesn't draw that row. (Except when it does, but
25769 that's handled in update_text_area.) */
25771 cy0 = w->phys_cursor.y;
25772 cy1 = cy0 + w->phys_cursor_height;
25773 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25774 return;
25776 w->phys_cursor_on_p = 0;
25779 #endif /* HAVE_WINDOW_SYSTEM */
25782 /************************************************************************
25783 Mouse Face
25784 ************************************************************************/
25786 #ifdef HAVE_WINDOW_SYSTEM
25788 /* EXPORT for RIF:
25789 Fix the display of area AREA of overlapping row ROW in window W
25790 with respect to the overlapping part OVERLAPS. */
25792 void
25793 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25794 enum glyph_row_area area, int overlaps)
25796 int i, x;
25798 BLOCK_INPUT;
25800 x = 0;
25801 for (i = 0; i < row->used[area];)
25803 if (row->glyphs[area][i].overlaps_vertically_p)
25805 int start = i, start_x = x;
25809 x += row->glyphs[area][i].pixel_width;
25810 ++i;
25812 while (i < row->used[area]
25813 && row->glyphs[area][i].overlaps_vertically_p);
25815 draw_glyphs (w, start_x, row, area,
25816 start, i,
25817 DRAW_NORMAL_TEXT, overlaps);
25819 else
25821 x += row->glyphs[area][i].pixel_width;
25822 ++i;
25826 UNBLOCK_INPUT;
25830 /* EXPORT:
25831 Draw the cursor glyph of window W in glyph row ROW. See the
25832 comment of draw_glyphs for the meaning of HL. */
25834 void
25835 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25836 enum draw_glyphs_face hl)
25838 /* If cursor hpos is out of bounds, don't draw garbage. This can
25839 happen in mini-buffer windows when switching between echo area
25840 glyphs and mini-buffer. */
25841 if ((row->reversed_p
25842 ? (w->phys_cursor.hpos >= 0)
25843 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25845 int on_p = w->phys_cursor_on_p;
25846 int x1;
25847 int hpos = w->phys_cursor.hpos;
25849 /* When the window is hscrolled, cursor hpos can legitimately be
25850 out of bounds, but we draw the cursor at the corresponding
25851 window margin in that case. */
25852 if (!row->reversed_p && hpos < 0)
25853 hpos = 0;
25854 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25855 hpos = row->used[TEXT_AREA] - 1;
25857 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25858 hl, 0);
25859 w->phys_cursor_on_p = on_p;
25861 if (hl == DRAW_CURSOR)
25862 w->phys_cursor_width = x1 - w->phys_cursor.x;
25863 /* When we erase the cursor, and ROW is overlapped by other
25864 rows, make sure that these overlapping parts of other rows
25865 are redrawn. */
25866 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25868 w->phys_cursor_width = x1 - w->phys_cursor.x;
25870 if (row > w->current_matrix->rows
25871 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25872 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25873 OVERLAPS_ERASED_CURSOR);
25875 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25876 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25877 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25878 OVERLAPS_ERASED_CURSOR);
25884 /* EXPORT:
25885 Erase the image of a cursor of window W from the screen. */
25887 void
25888 erase_phys_cursor (struct window *w)
25890 struct frame *f = XFRAME (w->frame);
25891 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25892 int hpos = w->phys_cursor.hpos;
25893 int vpos = w->phys_cursor.vpos;
25894 int mouse_face_here_p = 0;
25895 struct glyph_matrix *active_glyphs = w->current_matrix;
25896 struct glyph_row *cursor_row;
25897 struct glyph *cursor_glyph;
25898 enum draw_glyphs_face hl;
25900 /* No cursor displayed or row invalidated => nothing to do on the
25901 screen. */
25902 if (w->phys_cursor_type == NO_CURSOR)
25903 goto mark_cursor_off;
25905 /* VPOS >= active_glyphs->nrows means that window has been resized.
25906 Don't bother to erase the cursor. */
25907 if (vpos >= active_glyphs->nrows)
25908 goto mark_cursor_off;
25910 /* If row containing cursor is marked invalid, there is nothing we
25911 can do. */
25912 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25913 if (!cursor_row->enabled_p)
25914 goto mark_cursor_off;
25916 /* If line spacing is > 0, old cursor may only be partially visible in
25917 window after split-window. So adjust visible height. */
25918 cursor_row->visible_height = min (cursor_row->visible_height,
25919 window_text_bottom_y (w) - cursor_row->y);
25921 /* If row is completely invisible, don't attempt to delete a cursor which
25922 isn't there. This can happen if cursor is at top of a window, and
25923 we switch to a buffer with a header line in that window. */
25924 if (cursor_row->visible_height <= 0)
25925 goto mark_cursor_off;
25927 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25928 if (cursor_row->cursor_in_fringe_p)
25930 cursor_row->cursor_in_fringe_p = 0;
25931 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25932 goto mark_cursor_off;
25935 /* This can happen when the new row is shorter than the old one.
25936 In this case, either draw_glyphs or clear_end_of_line
25937 should have cleared the cursor. Note that we wouldn't be
25938 able to erase the cursor in this case because we don't have a
25939 cursor glyph at hand. */
25940 if ((cursor_row->reversed_p
25941 ? (w->phys_cursor.hpos < 0)
25942 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25943 goto mark_cursor_off;
25945 /* When the window is hscrolled, cursor hpos can legitimately be out
25946 of bounds, but we draw the cursor at the corresponding window
25947 margin in that case. */
25948 if (!cursor_row->reversed_p && hpos < 0)
25949 hpos = 0;
25950 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25951 hpos = cursor_row->used[TEXT_AREA] - 1;
25953 /* If the cursor is in the mouse face area, redisplay that when
25954 we clear the cursor. */
25955 if (! NILP (hlinfo->mouse_face_window)
25956 && coords_in_mouse_face_p (w, hpos, vpos)
25957 /* Don't redraw the cursor's spot in mouse face if it is at the
25958 end of a line (on a newline). The cursor appears there, but
25959 mouse highlighting does not. */
25960 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25961 mouse_face_here_p = 1;
25963 /* Maybe clear the display under the cursor. */
25964 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25966 int x, y, left_x;
25967 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25968 int width;
25970 cursor_glyph = get_phys_cursor_glyph (w);
25971 if (cursor_glyph == NULL)
25972 goto mark_cursor_off;
25974 width = cursor_glyph->pixel_width;
25975 left_x = window_box_left_offset (w, TEXT_AREA);
25976 x = w->phys_cursor.x;
25977 if (x < left_x)
25978 width -= left_x - x;
25979 width = min (width, window_box_width (w, TEXT_AREA) - x);
25980 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25981 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25983 if (width > 0)
25984 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25987 /* Erase the cursor by redrawing the character underneath it. */
25988 if (mouse_face_here_p)
25989 hl = DRAW_MOUSE_FACE;
25990 else
25991 hl = DRAW_NORMAL_TEXT;
25992 draw_phys_cursor_glyph (w, cursor_row, hl);
25994 mark_cursor_off:
25995 w->phys_cursor_on_p = 0;
25996 w->phys_cursor_type = NO_CURSOR;
26000 /* EXPORT:
26001 Display or clear cursor of window W. If ON is zero, clear the
26002 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26003 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26005 void
26006 display_and_set_cursor (struct window *w, int on,
26007 int hpos, int vpos, int x, int y)
26009 struct frame *f = XFRAME (w->frame);
26010 int new_cursor_type;
26011 int new_cursor_width;
26012 int active_cursor;
26013 struct glyph_row *glyph_row;
26014 struct glyph *glyph;
26016 /* This is pointless on invisible frames, and dangerous on garbaged
26017 windows and frames; in the latter case, the frame or window may
26018 be in the midst of changing its size, and x and y may be off the
26019 window. */
26020 if (! FRAME_VISIBLE_P (f)
26021 || FRAME_GARBAGED_P (f)
26022 || vpos >= w->current_matrix->nrows
26023 || hpos >= w->current_matrix->matrix_w)
26024 return;
26026 /* If cursor is off and we want it off, return quickly. */
26027 if (!on && !w->phys_cursor_on_p)
26028 return;
26030 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26031 /* If cursor row is not enabled, we don't really know where to
26032 display the cursor. */
26033 if (!glyph_row->enabled_p)
26035 w->phys_cursor_on_p = 0;
26036 return;
26039 glyph = NULL;
26040 if (!glyph_row->exact_window_width_line_p
26041 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26042 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26044 eassert (interrupt_input_blocked);
26046 /* Set new_cursor_type to the cursor we want to be displayed. */
26047 new_cursor_type = get_window_cursor_type (w, glyph,
26048 &new_cursor_width, &active_cursor);
26050 /* If cursor is currently being shown and we don't want it to be or
26051 it is in the wrong place, or the cursor type is not what we want,
26052 erase it. */
26053 if (w->phys_cursor_on_p
26054 && (!on
26055 || w->phys_cursor.x != x
26056 || w->phys_cursor.y != y
26057 || new_cursor_type != w->phys_cursor_type
26058 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26059 && new_cursor_width != w->phys_cursor_width)))
26060 erase_phys_cursor (w);
26062 /* Don't check phys_cursor_on_p here because that flag is only set
26063 to zero in some cases where we know that the cursor has been
26064 completely erased, to avoid the extra work of erasing the cursor
26065 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26066 still not be visible, or it has only been partly erased. */
26067 if (on)
26069 w->phys_cursor_ascent = glyph_row->ascent;
26070 w->phys_cursor_height = glyph_row->height;
26072 /* Set phys_cursor_.* before x_draw_.* is called because some
26073 of them may need the information. */
26074 w->phys_cursor.x = x;
26075 w->phys_cursor.y = glyph_row->y;
26076 w->phys_cursor.hpos = hpos;
26077 w->phys_cursor.vpos = vpos;
26080 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26081 new_cursor_type, new_cursor_width,
26082 on, active_cursor);
26086 /* Switch the display of W's cursor on or off, according to the value
26087 of ON. */
26089 static void
26090 update_window_cursor (struct window *w, int on)
26092 /* Don't update cursor in windows whose frame is in the process
26093 of being deleted. */
26094 if (w->current_matrix)
26096 int hpos = w->phys_cursor.hpos;
26097 int vpos = w->phys_cursor.vpos;
26098 struct glyph_row *row;
26100 if (vpos >= w->current_matrix->nrows
26101 || hpos >= w->current_matrix->matrix_w)
26102 return;
26104 row = MATRIX_ROW (w->current_matrix, vpos);
26106 /* When the window is hscrolled, cursor hpos can legitimately be
26107 out of bounds, but we draw the cursor at the corresponding
26108 window margin in that case. */
26109 if (!row->reversed_p && hpos < 0)
26110 hpos = 0;
26111 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26112 hpos = row->used[TEXT_AREA] - 1;
26114 BLOCK_INPUT;
26115 display_and_set_cursor (w, on, hpos, vpos,
26116 w->phys_cursor.x, w->phys_cursor.y);
26117 UNBLOCK_INPUT;
26122 /* Call update_window_cursor with parameter ON_P on all leaf windows
26123 in the window tree rooted at W. */
26125 static void
26126 update_cursor_in_window_tree (struct window *w, int on_p)
26128 while (w)
26130 if (!NILP (w->hchild))
26131 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26132 else if (!NILP (w->vchild))
26133 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26134 else
26135 update_window_cursor (w, on_p);
26137 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26142 /* EXPORT:
26143 Display the cursor on window W, or clear it, according to ON_P.
26144 Don't change the cursor's position. */
26146 void
26147 x_update_cursor (struct frame *f, int on_p)
26149 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26153 /* EXPORT:
26154 Clear the cursor of window W to background color, and mark the
26155 cursor as not shown. This is used when the text where the cursor
26156 is about to be rewritten. */
26158 void
26159 x_clear_cursor (struct window *w)
26161 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26162 update_window_cursor (w, 0);
26165 #endif /* HAVE_WINDOW_SYSTEM */
26167 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26168 and MSDOS. */
26169 static void
26170 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26171 int start_hpos, int end_hpos,
26172 enum draw_glyphs_face draw)
26174 #ifdef HAVE_WINDOW_SYSTEM
26175 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26177 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26178 return;
26180 #endif
26181 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26182 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26183 #endif
26186 /* Display the active region described by mouse_face_* according to DRAW. */
26188 static void
26189 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26191 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26192 struct frame *f = XFRAME (WINDOW_FRAME (w));
26194 if (/* If window is in the process of being destroyed, don't bother
26195 to do anything. */
26196 w->current_matrix != NULL
26197 /* Don't update mouse highlight if hidden */
26198 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26199 /* Recognize when we are called to operate on rows that don't exist
26200 anymore. This can happen when a window is split. */
26201 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26203 int phys_cursor_on_p = w->phys_cursor_on_p;
26204 struct glyph_row *row, *first, *last;
26206 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26207 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26209 for (row = first; row <= last && row->enabled_p; ++row)
26211 int start_hpos, end_hpos, start_x;
26213 /* For all but the first row, the highlight starts at column 0. */
26214 if (row == first)
26216 /* R2L rows have BEG and END in reversed order, but the
26217 screen drawing geometry is always left to right. So
26218 we need to mirror the beginning and end of the
26219 highlighted area in R2L rows. */
26220 if (!row->reversed_p)
26222 start_hpos = hlinfo->mouse_face_beg_col;
26223 start_x = hlinfo->mouse_face_beg_x;
26225 else if (row == last)
26227 start_hpos = hlinfo->mouse_face_end_col;
26228 start_x = hlinfo->mouse_face_end_x;
26230 else
26232 start_hpos = 0;
26233 start_x = 0;
26236 else if (row->reversed_p && row == last)
26238 start_hpos = hlinfo->mouse_face_end_col;
26239 start_x = hlinfo->mouse_face_end_x;
26241 else
26243 start_hpos = 0;
26244 start_x = 0;
26247 if (row == last)
26249 if (!row->reversed_p)
26250 end_hpos = hlinfo->mouse_face_end_col;
26251 else if (row == first)
26252 end_hpos = hlinfo->mouse_face_beg_col;
26253 else
26255 end_hpos = row->used[TEXT_AREA];
26256 if (draw == DRAW_NORMAL_TEXT)
26257 row->fill_line_p = 1; /* Clear to end of line */
26260 else if (row->reversed_p && row == first)
26261 end_hpos = hlinfo->mouse_face_beg_col;
26262 else
26264 end_hpos = row->used[TEXT_AREA];
26265 if (draw == DRAW_NORMAL_TEXT)
26266 row->fill_line_p = 1; /* Clear to end of line */
26269 if (end_hpos > start_hpos)
26271 draw_row_with_mouse_face (w, start_x, row,
26272 start_hpos, end_hpos, draw);
26274 row->mouse_face_p
26275 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26279 #ifdef HAVE_WINDOW_SYSTEM
26280 /* When we've written over the cursor, arrange for it to
26281 be displayed again. */
26282 if (FRAME_WINDOW_P (f)
26283 && phys_cursor_on_p && !w->phys_cursor_on_p)
26285 int hpos = w->phys_cursor.hpos;
26287 /* When the window is hscrolled, cursor hpos can legitimately be
26288 out of bounds, but we draw the cursor at the corresponding
26289 window margin in that case. */
26290 if (!row->reversed_p && hpos < 0)
26291 hpos = 0;
26292 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26293 hpos = row->used[TEXT_AREA] - 1;
26295 BLOCK_INPUT;
26296 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26297 w->phys_cursor.x, w->phys_cursor.y);
26298 UNBLOCK_INPUT;
26300 #endif /* HAVE_WINDOW_SYSTEM */
26303 #ifdef HAVE_WINDOW_SYSTEM
26304 /* Change the mouse cursor. */
26305 if (FRAME_WINDOW_P (f))
26307 if (draw == DRAW_NORMAL_TEXT
26308 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26309 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26310 else if (draw == DRAW_MOUSE_FACE)
26311 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26312 else
26313 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26315 #endif /* HAVE_WINDOW_SYSTEM */
26318 /* EXPORT:
26319 Clear out the mouse-highlighted active region.
26320 Redraw it un-highlighted first. Value is non-zero if mouse
26321 face was actually drawn unhighlighted. */
26324 clear_mouse_face (Mouse_HLInfo *hlinfo)
26326 int cleared = 0;
26328 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26330 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26331 cleared = 1;
26334 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26335 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26336 hlinfo->mouse_face_window = Qnil;
26337 hlinfo->mouse_face_overlay = Qnil;
26338 return cleared;
26341 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26342 within the mouse face on that window. */
26343 static int
26344 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26346 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26348 /* Quickly resolve the easy cases. */
26349 if (!(WINDOWP (hlinfo->mouse_face_window)
26350 && XWINDOW (hlinfo->mouse_face_window) == w))
26351 return 0;
26352 if (vpos < hlinfo->mouse_face_beg_row
26353 || vpos > hlinfo->mouse_face_end_row)
26354 return 0;
26355 if (vpos > hlinfo->mouse_face_beg_row
26356 && vpos < hlinfo->mouse_face_end_row)
26357 return 1;
26359 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26361 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26363 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26364 return 1;
26366 else if ((vpos == hlinfo->mouse_face_beg_row
26367 && hpos >= hlinfo->mouse_face_beg_col)
26368 || (vpos == hlinfo->mouse_face_end_row
26369 && hpos < hlinfo->mouse_face_end_col))
26370 return 1;
26372 else
26374 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26376 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26377 return 1;
26379 else if ((vpos == hlinfo->mouse_face_beg_row
26380 && hpos <= hlinfo->mouse_face_beg_col)
26381 || (vpos == hlinfo->mouse_face_end_row
26382 && hpos > hlinfo->mouse_face_end_col))
26383 return 1;
26385 return 0;
26389 /* EXPORT:
26390 Non-zero if physical cursor of window W is within mouse face. */
26393 cursor_in_mouse_face_p (struct window *w)
26395 int hpos = w->phys_cursor.hpos;
26396 int vpos = w->phys_cursor.vpos;
26397 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26399 /* When the window is hscrolled, cursor hpos can legitimately be out
26400 of bounds, but we draw the cursor at the corresponding window
26401 margin in that case. */
26402 if (!row->reversed_p && hpos < 0)
26403 hpos = 0;
26404 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26405 hpos = row->used[TEXT_AREA] - 1;
26407 return coords_in_mouse_face_p (w, hpos, vpos);
26412 /* Find the glyph rows START_ROW and END_ROW of window W that display
26413 characters between buffer positions START_CHARPOS and END_CHARPOS
26414 (excluding END_CHARPOS). DISP_STRING is a display string that
26415 covers these buffer positions. This is similar to
26416 row_containing_pos, but is more accurate when bidi reordering makes
26417 buffer positions change non-linearly with glyph rows. */
26418 static void
26419 rows_from_pos_range (struct window *w,
26420 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26421 Lisp_Object disp_string,
26422 struct glyph_row **start, struct glyph_row **end)
26424 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26425 int last_y = window_text_bottom_y (w);
26426 struct glyph_row *row;
26428 *start = NULL;
26429 *end = NULL;
26431 while (!first->enabled_p
26432 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26433 first++;
26435 /* Find the START row. */
26436 for (row = first;
26437 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26438 row++)
26440 /* A row can potentially be the START row if the range of the
26441 characters it displays intersects the range
26442 [START_CHARPOS..END_CHARPOS). */
26443 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26444 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26445 /* See the commentary in row_containing_pos, for the
26446 explanation of the complicated way to check whether
26447 some position is beyond the end of the characters
26448 displayed by a row. */
26449 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26450 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26451 && !row->ends_at_zv_p
26452 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26453 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26454 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26455 && !row->ends_at_zv_p
26456 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26458 /* Found a candidate row. Now make sure at least one of the
26459 glyphs it displays has a charpos from the range
26460 [START_CHARPOS..END_CHARPOS).
26462 This is not obvious because bidi reordering could make
26463 buffer positions of a row be 1,2,3,102,101,100, and if we
26464 want to highlight characters in [50..60), we don't want
26465 this row, even though [50..60) does intersect [1..103),
26466 the range of character positions given by the row's start
26467 and end positions. */
26468 struct glyph *g = row->glyphs[TEXT_AREA];
26469 struct glyph *e = g + row->used[TEXT_AREA];
26471 while (g < e)
26473 if (((BUFFERP (g->object) || INTEGERP (g->object))
26474 && start_charpos <= g->charpos && g->charpos < end_charpos)
26475 /* A glyph that comes from DISP_STRING is by
26476 definition to be highlighted. */
26477 || EQ (g->object, disp_string))
26478 *start = row;
26479 g++;
26481 if (*start)
26482 break;
26486 /* Find the END row. */
26487 if (!*start
26488 /* If the last row is partially visible, start looking for END
26489 from that row, instead of starting from FIRST. */
26490 && !(row->enabled_p
26491 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26492 row = first;
26493 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26495 struct glyph_row *next = row + 1;
26496 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26498 if (!next->enabled_p
26499 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26500 /* The first row >= START whose range of displayed characters
26501 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26502 is the row END + 1. */
26503 || (start_charpos < next_start
26504 && end_charpos < next_start)
26505 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26506 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26507 && !next->ends_at_zv_p
26508 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26509 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26510 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26511 && !next->ends_at_zv_p
26512 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26514 *end = row;
26515 break;
26517 else
26519 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26520 but none of the characters it displays are in the range, it is
26521 also END + 1. */
26522 struct glyph *g = next->glyphs[TEXT_AREA];
26523 struct glyph *s = g;
26524 struct glyph *e = g + next->used[TEXT_AREA];
26526 while (g < e)
26528 if (((BUFFERP (g->object) || INTEGERP (g->object))
26529 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26530 /* If the buffer position of the first glyph in
26531 the row is equal to END_CHARPOS, it means
26532 the last character to be highlighted is the
26533 newline of ROW, and we must consider NEXT as
26534 END, not END+1. */
26535 || (((!next->reversed_p && g == s)
26536 || (next->reversed_p && g == e - 1))
26537 && (g->charpos == end_charpos
26538 /* Special case for when NEXT is an
26539 empty line at ZV. */
26540 || (g->charpos == -1
26541 && !row->ends_at_zv_p
26542 && next_start == end_charpos)))))
26543 /* A glyph that comes from DISP_STRING is by
26544 definition to be highlighted. */
26545 || EQ (g->object, disp_string))
26546 break;
26547 g++;
26549 if (g == e)
26551 *end = row;
26552 break;
26554 /* The first row that ends at ZV must be the last to be
26555 highlighted. */
26556 else if (next->ends_at_zv_p)
26558 *end = next;
26559 break;
26565 /* This function sets the mouse_face_* elements of HLINFO, assuming
26566 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26567 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26568 for the overlay or run of text properties specifying the mouse
26569 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26570 before-string and after-string that must also be highlighted.
26571 DISP_STRING, if non-nil, is a display string that may cover some
26572 or all of the highlighted text. */
26574 static void
26575 mouse_face_from_buffer_pos (Lisp_Object window,
26576 Mouse_HLInfo *hlinfo,
26577 ptrdiff_t mouse_charpos,
26578 ptrdiff_t start_charpos,
26579 ptrdiff_t end_charpos,
26580 Lisp_Object before_string,
26581 Lisp_Object after_string,
26582 Lisp_Object disp_string)
26584 struct window *w = XWINDOW (window);
26585 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26586 struct glyph_row *r1, *r2;
26587 struct glyph *glyph, *end;
26588 ptrdiff_t ignore, pos;
26589 int x;
26591 eassert (NILP (disp_string) || STRINGP (disp_string));
26592 eassert (NILP (before_string) || STRINGP (before_string));
26593 eassert (NILP (after_string) || STRINGP (after_string));
26595 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26596 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26597 if (r1 == NULL)
26598 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26599 /* If the before-string or display-string contains newlines,
26600 rows_from_pos_range skips to its last row. Move back. */
26601 if (!NILP (before_string) || !NILP (disp_string))
26603 struct glyph_row *prev;
26604 while ((prev = r1 - 1, prev >= first)
26605 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26606 && prev->used[TEXT_AREA] > 0)
26608 struct glyph *beg = prev->glyphs[TEXT_AREA];
26609 glyph = beg + prev->used[TEXT_AREA];
26610 while (--glyph >= beg && INTEGERP (glyph->object));
26611 if (glyph < beg
26612 || !(EQ (glyph->object, before_string)
26613 || EQ (glyph->object, disp_string)))
26614 break;
26615 r1 = prev;
26618 if (r2 == NULL)
26620 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26621 hlinfo->mouse_face_past_end = 1;
26623 else if (!NILP (after_string))
26625 /* If the after-string has newlines, advance to its last row. */
26626 struct glyph_row *next;
26627 struct glyph_row *last
26628 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26630 for (next = r2 + 1;
26631 next <= last
26632 && next->used[TEXT_AREA] > 0
26633 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26634 ++next)
26635 r2 = next;
26637 /* The rest of the display engine assumes that mouse_face_beg_row is
26638 either above mouse_face_end_row or identical to it. But with
26639 bidi-reordered continued lines, the row for START_CHARPOS could
26640 be below the row for END_CHARPOS. If so, swap the rows and store
26641 them in correct order. */
26642 if (r1->y > r2->y)
26644 struct glyph_row *tem = r2;
26646 r2 = r1;
26647 r1 = tem;
26650 hlinfo->mouse_face_beg_y = r1->y;
26651 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26652 hlinfo->mouse_face_end_y = r2->y;
26653 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26655 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26656 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26657 could be anywhere in the row and in any order. The strategy
26658 below is to find the leftmost and the rightmost glyph that
26659 belongs to either of these 3 strings, or whose position is
26660 between START_CHARPOS and END_CHARPOS, and highlight all the
26661 glyphs between those two. This may cover more than just the text
26662 between START_CHARPOS and END_CHARPOS if the range of characters
26663 strides the bidi level boundary, e.g. if the beginning is in R2L
26664 text while the end is in L2R text or vice versa. */
26665 if (!r1->reversed_p)
26667 /* This row is in a left to right paragraph. Scan it left to
26668 right. */
26669 glyph = r1->glyphs[TEXT_AREA];
26670 end = glyph + r1->used[TEXT_AREA];
26671 x = r1->x;
26673 /* Skip truncation glyphs at the start of the glyph row. */
26674 if (r1->displays_text_p)
26675 for (; glyph < end
26676 && INTEGERP (glyph->object)
26677 && glyph->charpos < 0;
26678 ++glyph)
26679 x += glyph->pixel_width;
26681 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26682 or DISP_STRING, and the first glyph from buffer whose
26683 position is between START_CHARPOS and END_CHARPOS. */
26684 for (; glyph < end
26685 && !INTEGERP (glyph->object)
26686 && !EQ (glyph->object, disp_string)
26687 && !(BUFFERP (glyph->object)
26688 && (glyph->charpos >= start_charpos
26689 && glyph->charpos < end_charpos));
26690 ++glyph)
26692 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26693 are present at buffer positions between START_CHARPOS and
26694 END_CHARPOS, or if they come from an overlay. */
26695 if (EQ (glyph->object, before_string))
26697 pos = string_buffer_position (before_string,
26698 start_charpos);
26699 /* If pos == 0, it means before_string came from an
26700 overlay, not from a buffer position. */
26701 if (!pos || (pos >= start_charpos && pos < end_charpos))
26702 break;
26704 else if (EQ (glyph->object, after_string))
26706 pos = string_buffer_position (after_string, end_charpos);
26707 if (!pos || (pos >= start_charpos && pos < end_charpos))
26708 break;
26710 x += glyph->pixel_width;
26712 hlinfo->mouse_face_beg_x = x;
26713 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26715 else
26717 /* This row is in a right to left paragraph. Scan it right to
26718 left. */
26719 struct glyph *g;
26721 end = r1->glyphs[TEXT_AREA] - 1;
26722 glyph = end + r1->used[TEXT_AREA];
26724 /* Skip truncation glyphs at the start of the glyph row. */
26725 if (r1->displays_text_p)
26726 for (; glyph > end
26727 && INTEGERP (glyph->object)
26728 && glyph->charpos < 0;
26729 --glyph)
26732 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26733 or DISP_STRING, and the first glyph from buffer whose
26734 position is between START_CHARPOS and END_CHARPOS. */
26735 for (; glyph > end
26736 && !INTEGERP (glyph->object)
26737 && !EQ (glyph->object, disp_string)
26738 && !(BUFFERP (glyph->object)
26739 && (glyph->charpos >= start_charpos
26740 && glyph->charpos < end_charpos));
26741 --glyph)
26743 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26744 are present at buffer positions between START_CHARPOS and
26745 END_CHARPOS, or if they come from an overlay. */
26746 if (EQ (glyph->object, before_string))
26748 pos = string_buffer_position (before_string, start_charpos);
26749 /* If pos == 0, it means before_string came from an
26750 overlay, not from a buffer position. */
26751 if (!pos || (pos >= start_charpos && pos < end_charpos))
26752 break;
26754 else if (EQ (glyph->object, after_string))
26756 pos = string_buffer_position (after_string, end_charpos);
26757 if (!pos || (pos >= start_charpos && pos < end_charpos))
26758 break;
26762 glyph++; /* first glyph to the right of the highlighted area */
26763 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26764 x += g->pixel_width;
26765 hlinfo->mouse_face_beg_x = x;
26766 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26769 /* If the highlight ends in a different row, compute GLYPH and END
26770 for the end row. Otherwise, reuse the values computed above for
26771 the row where the highlight begins. */
26772 if (r2 != r1)
26774 if (!r2->reversed_p)
26776 glyph = r2->glyphs[TEXT_AREA];
26777 end = glyph + r2->used[TEXT_AREA];
26778 x = r2->x;
26780 else
26782 end = r2->glyphs[TEXT_AREA] - 1;
26783 glyph = end + r2->used[TEXT_AREA];
26787 if (!r2->reversed_p)
26789 /* Skip truncation and continuation glyphs near the end of the
26790 row, and also blanks and stretch glyphs inserted by
26791 extend_face_to_end_of_line. */
26792 while (end > glyph
26793 && INTEGERP ((end - 1)->object))
26794 --end;
26795 /* Scan the rest of the glyph row from the end, looking for the
26796 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26797 DISP_STRING, or whose position is between START_CHARPOS
26798 and END_CHARPOS */
26799 for (--end;
26800 end > glyph
26801 && !INTEGERP (end->object)
26802 && !EQ (end->object, disp_string)
26803 && !(BUFFERP (end->object)
26804 && (end->charpos >= start_charpos
26805 && end->charpos < end_charpos));
26806 --end)
26808 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26809 are present at buffer positions between START_CHARPOS and
26810 END_CHARPOS, or if they come from an overlay. */
26811 if (EQ (end->object, before_string))
26813 pos = string_buffer_position (before_string, start_charpos);
26814 if (!pos || (pos >= start_charpos && pos < end_charpos))
26815 break;
26817 else if (EQ (end->object, after_string))
26819 pos = string_buffer_position (after_string, end_charpos);
26820 if (!pos || (pos >= start_charpos && pos < end_charpos))
26821 break;
26824 /* Find the X coordinate of the last glyph to be highlighted. */
26825 for (; glyph <= end; ++glyph)
26826 x += glyph->pixel_width;
26828 hlinfo->mouse_face_end_x = x;
26829 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26831 else
26833 /* Skip truncation and continuation glyphs near the end of the
26834 row, and also blanks and stretch glyphs inserted by
26835 extend_face_to_end_of_line. */
26836 x = r2->x;
26837 end++;
26838 while (end < glyph
26839 && INTEGERP (end->object))
26841 x += end->pixel_width;
26842 ++end;
26844 /* Scan the rest of the glyph row from the end, looking for the
26845 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26846 DISP_STRING, or whose position is between START_CHARPOS
26847 and END_CHARPOS */
26848 for ( ;
26849 end < glyph
26850 && !INTEGERP (end->object)
26851 && !EQ (end->object, disp_string)
26852 && !(BUFFERP (end->object)
26853 && (end->charpos >= start_charpos
26854 && end->charpos < end_charpos));
26855 ++end)
26857 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26858 are present at buffer positions between START_CHARPOS and
26859 END_CHARPOS, or if they come from an overlay. */
26860 if (EQ (end->object, before_string))
26862 pos = string_buffer_position (before_string, start_charpos);
26863 if (!pos || (pos >= start_charpos && pos < end_charpos))
26864 break;
26866 else if (EQ (end->object, after_string))
26868 pos = string_buffer_position (after_string, end_charpos);
26869 if (!pos || (pos >= start_charpos && pos < end_charpos))
26870 break;
26872 x += end->pixel_width;
26874 /* If we exited the above loop because we arrived at the last
26875 glyph of the row, and its buffer position is still not in
26876 range, it means the last character in range is the preceding
26877 newline. Bump the end column and x values to get past the
26878 last glyph. */
26879 if (end == glyph
26880 && BUFFERP (end->object)
26881 && (end->charpos < start_charpos
26882 || end->charpos >= end_charpos))
26884 x += end->pixel_width;
26885 ++end;
26887 hlinfo->mouse_face_end_x = x;
26888 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26891 hlinfo->mouse_face_window = window;
26892 hlinfo->mouse_face_face_id
26893 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26894 mouse_charpos + 1,
26895 !hlinfo->mouse_face_hidden, -1);
26896 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26899 /* The following function is not used anymore (replaced with
26900 mouse_face_from_string_pos), but I leave it here for the time
26901 being, in case someone would. */
26903 #if 0 /* not used */
26905 /* Find the position of the glyph for position POS in OBJECT in
26906 window W's current matrix, and return in *X, *Y the pixel
26907 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26909 RIGHT_P non-zero means return the position of the right edge of the
26910 glyph, RIGHT_P zero means return the left edge position.
26912 If no glyph for POS exists in the matrix, return the position of
26913 the glyph with the next smaller position that is in the matrix, if
26914 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26915 exists in the matrix, return the position of the glyph with the
26916 next larger position in OBJECT.
26918 Value is non-zero if a glyph was found. */
26920 static int
26921 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26922 int *hpos, int *vpos, int *x, int *y, int right_p)
26924 int yb = window_text_bottom_y (w);
26925 struct glyph_row *r;
26926 struct glyph *best_glyph = NULL;
26927 struct glyph_row *best_row = NULL;
26928 int best_x = 0;
26930 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26931 r->enabled_p && r->y < yb;
26932 ++r)
26934 struct glyph *g = r->glyphs[TEXT_AREA];
26935 struct glyph *e = g + r->used[TEXT_AREA];
26936 int gx;
26938 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26939 if (EQ (g->object, object))
26941 if (g->charpos == pos)
26943 best_glyph = g;
26944 best_x = gx;
26945 best_row = r;
26946 goto found;
26948 else if (best_glyph == NULL
26949 || ((eabs (g->charpos - pos)
26950 < eabs (best_glyph->charpos - pos))
26951 && (right_p
26952 ? g->charpos < pos
26953 : g->charpos > pos)))
26955 best_glyph = g;
26956 best_x = gx;
26957 best_row = r;
26962 found:
26964 if (best_glyph)
26966 *x = best_x;
26967 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26969 if (right_p)
26971 *x += best_glyph->pixel_width;
26972 ++*hpos;
26975 *y = best_row->y;
26976 *vpos = best_row - w->current_matrix->rows;
26979 return best_glyph != NULL;
26981 #endif /* not used */
26983 /* Find the positions of the first and the last glyphs in window W's
26984 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26985 (assumed to be a string), and return in HLINFO's mouse_face_*
26986 members the pixel and column/row coordinates of those glyphs. */
26988 static void
26989 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26990 Lisp_Object object,
26991 ptrdiff_t startpos, ptrdiff_t endpos)
26993 int yb = window_text_bottom_y (w);
26994 struct glyph_row *r;
26995 struct glyph *g, *e;
26996 int gx;
26997 int found = 0;
26999 /* Find the glyph row with at least one position in the range
27000 [STARTPOS..ENDPOS], and the first glyph in that row whose
27001 position belongs to that range. */
27002 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27003 r->enabled_p && r->y < yb;
27004 ++r)
27006 if (!r->reversed_p)
27008 g = r->glyphs[TEXT_AREA];
27009 e = g + r->used[TEXT_AREA];
27010 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27011 if (EQ (g->object, object)
27012 && startpos <= g->charpos && g->charpos <= endpos)
27014 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27015 hlinfo->mouse_face_beg_y = r->y;
27016 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27017 hlinfo->mouse_face_beg_x = gx;
27018 found = 1;
27019 break;
27022 else
27024 struct glyph *g1;
27026 e = r->glyphs[TEXT_AREA];
27027 g = e + r->used[TEXT_AREA];
27028 for ( ; g > e; --g)
27029 if (EQ ((g-1)->object, object)
27030 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27032 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27033 hlinfo->mouse_face_beg_y = r->y;
27034 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27035 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27036 gx += g1->pixel_width;
27037 hlinfo->mouse_face_beg_x = gx;
27038 found = 1;
27039 break;
27042 if (found)
27043 break;
27046 if (!found)
27047 return;
27049 /* Starting with the next row, look for the first row which does NOT
27050 include any glyphs whose positions are in the range. */
27051 for (++r; r->enabled_p && r->y < yb; ++r)
27053 g = r->glyphs[TEXT_AREA];
27054 e = g + r->used[TEXT_AREA];
27055 found = 0;
27056 for ( ; g < e; ++g)
27057 if (EQ (g->object, object)
27058 && startpos <= g->charpos && g->charpos <= endpos)
27060 found = 1;
27061 break;
27063 if (!found)
27064 break;
27067 /* The highlighted region ends on the previous row. */
27068 r--;
27070 /* Set the end row and its vertical pixel coordinate. */
27071 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27072 hlinfo->mouse_face_end_y = r->y;
27074 /* Compute and set the end column and the end column's horizontal
27075 pixel coordinate. */
27076 if (!r->reversed_p)
27078 g = r->glyphs[TEXT_AREA];
27079 e = g + r->used[TEXT_AREA];
27080 for ( ; e > g; --e)
27081 if (EQ ((e-1)->object, object)
27082 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27083 break;
27084 hlinfo->mouse_face_end_col = e - g;
27086 for (gx = r->x; g < e; ++g)
27087 gx += g->pixel_width;
27088 hlinfo->mouse_face_end_x = gx;
27090 else
27092 e = r->glyphs[TEXT_AREA];
27093 g = e + r->used[TEXT_AREA];
27094 for (gx = r->x ; e < g; ++e)
27096 if (EQ (e->object, object)
27097 && startpos <= e->charpos && e->charpos <= endpos)
27098 break;
27099 gx += e->pixel_width;
27101 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27102 hlinfo->mouse_face_end_x = gx;
27106 #ifdef HAVE_WINDOW_SYSTEM
27108 /* See if position X, Y is within a hot-spot of an image. */
27110 static int
27111 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27113 if (!CONSP (hot_spot))
27114 return 0;
27116 if (EQ (XCAR (hot_spot), Qrect))
27118 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27119 Lisp_Object rect = XCDR (hot_spot);
27120 Lisp_Object tem;
27121 if (!CONSP (rect))
27122 return 0;
27123 if (!CONSP (XCAR (rect)))
27124 return 0;
27125 if (!CONSP (XCDR (rect)))
27126 return 0;
27127 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27128 return 0;
27129 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27130 return 0;
27131 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27132 return 0;
27133 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27134 return 0;
27135 return 1;
27137 else if (EQ (XCAR (hot_spot), Qcircle))
27139 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27140 Lisp_Object circ = XCDR (hot_spot);
27141 Lisp_Object lr, lx0, ly0;
27142 if (CONSP (circ)
27143 && CONSP (XCAR (circ))
27144 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27145 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27146 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27148 double r = XFLOATINT (lr);
27149 double dx = XINT (lx0) - x;
27150 double dy = XINT (ly0) - y;
27151 return (dx * dx + dy * dy <= r * r);
27154 else if (EQ (XCAR (hot_spot), Qpoly))
27156 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27157 if (VECTORP (XCDR (hot_spot)))
27159 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27160 Lisp_Object *poly = v->contents;
27161 ptrdiff_t n = v->header.size;
27162 ptrdiff_t i;
27163 int inside = 0;
27164 Lisp_Object lx, ly;
27165 int x0, y0;
27167 /* Need an even number of coordinates, and at least 3 edges. */
27168 if (n < 6 || n & 1)
27169 return 0;
27171 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27172 If count is odd, we are inside polygon. Pixels on edges
27173 may or may not be included depending on actual geometry of the
27174 polygon. */
27175 if ((lx = poly[n-2], !INTEGERP (lx))
27176 || (ly = poly[n-1], !INTEGERP (lx)))
27177 return 0;
27178 x0 = XINT (lx), y0 = XINT (ly);
27179 for (i = 0; i < n; i += 2)
27181 int x1 = x0, y1 = y0;
27182 if ((lx = poly[i], !INTEGERP (lx))
27183 || (ly = poly[i+1], !INTEGERP (ly)))
27184 return 0;
27185 x0 = XINT (lx), y0 = XINT (ly);
27187 /* Does this segment cross the X line? */
27188 if (x0 >= x)
27190 if (x1 >= x)
27191 continue;
27193 else if (x1 < x)
27194 continue;
27195 if (y > y0 && y > y1)
27196 continue;
27197 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27198 inside = !inside;
27200 return inside;
27203 return 0;
27206 Lisp_Object
27207 find_hot_spot (Lisp_Object map, int x, int y)
27209 while (CONSP (map))
27211 if (CONSP (XCAR (map))
27212 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27213 return XCAR (map);
27214 map = XCDR (map);
27217 return Qnil;
27220 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27221 3, 3, 0,
27222 doc: /* Lookup in image map MAP coordinates X and Y.
27223 An image map is an alist where each element has the format (AREA ID PLIST).
27224 An AREA is specified as either a rectangle, a circle, or a polygon:
27225 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27226 pixel coordinates of the upper left and bottom right corners.
27227 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27228 and the radius of the circle; r may be a float or integer.
27229 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27230 vector describes one corner in the polygon.
27231 Returns the alist element for the first matching AREA in MAP. */)
27232 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27234 if (NILP (map))
27235 return Qnil;
27237 CHECK_NUMBER (x);
27238 CHECK_NUMBER (y);
27240 return find_hot_spot (map,
27241 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27242 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27246 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27247 static void
27248 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27250 /* Do not change cursor shape while dragging mouse. */
27251 if (!NILP (do_mouse_tracking))
27252 return;
27254 if (!NILP (pointer))
27256 if (EQ (pointer, Qarrow))
27257 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27258 else if (EQ (pointer, Qhand))
27259 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27260 else if (EQ (pointer, Qtext))
27261 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27262 else if (EQ (pointer, intern ("hdrag")))
27263 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27264 #ifdef HAVE_X_WINDOWS
27265 else if (EQ (pointer, intern ("vdrag")))
27266 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27267 #endif
27268 else if (EQ (pointer, intern ("hourglass")))
27269 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27270 else if (EQ (pointer, Qmodeline))
27271 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27272 else
27273 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27276 if (cursor != No_Cursor)
27277 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27280 #endif /* HAVE_WINDOW_SYSTEM */
27282 /* Take proper action when mouse has moved to the mode or header line
27283 or marginal area AREA of window W, x-position X and y-position Y.
27284 X is relative to the start of the text display area of W, so the
27285 width of bitmap areas and scroll bars must be subtracted to get a
27286 position relative to the start of the mode line. */
27288 static void
27289 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27290 enum window_part area)
27292 struct window *w = XWINDOW (window);
27293 struct frame *f = XFRAME (w->frame);
27294 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27295 #ifdef HAVE_WINDOW_SYSTEM
27296 Display_Info *dpyinfo;
27297 #endif
27298 Cursor cursor = No_Cursor;
27299 Lisp_Object pointer = Qnil;
27300 int dx, dy, width, height;
27301 ptrdiff_t charpos;
27302 Lisp_Object string, object = Qnil;
27303 Lisp_Object pos IF_LINT (= Qnil), help;
27305 Lisp_Object mouse_face;
27306 int original_x_pixel = x;
27307 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27308 struct glyph_row *row IF_LINT (= 0);
27310 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27312 int x0;
27313 struct glyph *end;
27315 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27316 returns them in row/column units! */
27317 string = mode_line_string (w, area, &x, &y, &charpos,
27318 &object, &dx, &dy, &width, &height);
27320 row = (area == ON_MODE_LINE
27321 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27322 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27324 /* Find the glyph under the mouse pointer. */
27325 if (row->mode_line_p && row->enabled_p)
27327 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27328 end = glyph + row->used[TEXT_AREA];
27330 for (x0 = original_x_pixel;
27331 glyph < end && x0 >= glyph->pixel_width;
27332 ++glyph)
27333 x0 -= glyph->pixel_width;
27335 if (glyph >= end)
27336 glyph = NULL;
27339 else
27341 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27342 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27343 returns them in row/column units! */
27344 string = marginal_area_string (w, area, &x, &y, &charpos,
27345 &object, &dx, &dy, &width, &height);
27348 help = Qnil;
27350 #ifdef HAVE_WINDOW_SYSTEM
27351 if (IMAGEP (object))
27353 Lisp_Object image_map, hotspot;
27354 if ((image_map = Fplist_get (XCDR (object), QCmap),
27355 !NILP (image_map))
27356 && (hotspot = find_hot_spot (image_map, dx, dy),
27357 CONSP (hotspot))
27358 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27360 Lisp_Object plist;
27362 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27363 If so, we could look for mouse-enter, mouse-leave
27364 properties in PLIST (and do something...). */
27365 hotspot = XCDR (hotspot);
27366 if (CONSP (hotspot)
27367 && (plist = XCAR (hotspot), CONSP (plist)))
27369 pointer = Fplist_get (plist, Qpointer);
27370 if (NILP (pointer))
27371 pointer = Qhand;
27372 help = Fplist_get (plist, Qhelp_echo);
27373 if (!NILP (help))
27375 help_echo_string = help;
27376 XSETWINDOW (help_echo_window, w);
27377 help_echo_object = w->buffer;
27378 help_echo_pos = charpos;
27382 if (NILP (pointer))
27383 pointer = Fplist_get (XCDR (object), QCpointer);
27385 #endif /* HAVE_WINDOW_SYSTEM */
27387 if (STRINGP (string))
27388 pos = make_number (charpos);
27390 /* Set the help text and mouse pointer. If the mouse is on a part
27391 of the mode line without any text (e.g. past the right edge of
27392 the mode line text), use the default help text and pointer. */
27393 if (STRINGP (string) || area == ON_MODE_LINE)
27395 /* Arrange to display the help by setting the global variables
27396 help_echo_string, help_echo_object, and help_echo_pos. */
27397 if (NILP (help))
27399 if (STRINGP (string))
27400 help = Fget_text_property (pos, Qhelp_echo, string);
27402 if (!NILP (help))
27404 help_echo_string = help;
27405 XSETWINDOW (help_echo_window, w);
27406 help_echo_object = string;
27407 help_echo_pos = charpos;
27409 else if (area == ON_MODE_LINE)
27411 Lisp_Object default_help
27412 = buffer_local_value_1 (Qmode_line_default_help_echo,
27413 w->buffer);
27415 if (STRINGP (default_help))
27417 help_echo_string = default_help;
27418 XSETWINDOW (help_echo_window, w);
27419 help_echo_object = Qnil;
27420 help_echo_pos = -1;
27425 #ifdef HAVE_WINDOW_SYSTEM
27426 /* Change the mouse pointer according to what is under it. */
27427 if (FRAME_WINDOW_P (f))
27429 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27430 if (STRINGP (string))
27432 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27434 if (NILP (pointer))
27435 pointer = Fget_text_property (pos, Qpointer, string);
27437 /* Change the mouse pointer according to what is under X/Y. */
27438 if (NILP (pointer)
27439 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27441 Lisp_Object map;
27442 map = Fget_text_property (pos, Qlocal_map, string);
27443 if (!KEYMAPP (map))
27444 map = Fget_text_property (pos, Qkeymap, string);
27445 if (!KEYMAPP (map))
27446 cursor = dpyinfo->vertical_scroll_bar_cursor;
27449 else
27450 /* Default mode-line pointer. */
27451 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27453 #endif
27456 /* Change the mouse face according to what is under X/Y. */
27457 if (STRINGP (string))
27459 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27460 if (!NILP (mouse_face)
27461 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27462 && glyph)
27464 Lisp_Object b, e;
27466 struct glyph * tmp_glyph;
27468 int gpos;
27469 int gseq_length;
27470 int total_pixel_width;
27471 ptrdiff_t begpos, endpos, ignore;
27473 int vpos, hpos;
27475 b = Fprevious_single_property_change (make_number (charpos + 1),
27476 Qmouse_face, string, Qnil);
27477 if (NILP (b))
27478 begpos = 0;
27479 else
27480 begpos = XINT (b);
27482 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27483 if (NILP (e))
27484 endpos = SCHARS (string);
27485 else
27486 endpos = XINT (e);
27488 /* Calculate the glyph position GPOS of GLYPH in the
27489 displayed string, relative to the beginning of the
27490 highlighted part of the string.
27492 Note: GPOS is different from CHARPOS. CHARPOS is the
27493 position of GLYPH in the internal string object. A mode
27494 line string format has structures which are converted to
27495 a flattened string by the Emacs Lisp interpreter. The
27496 internal string is an element of those structures. The
27497 displayed string is the flattened string. */
27498 tmp_glyph = row_start_glyph;
27499 while (tmp_glyph < glyph
27500 && (!(EQ (tmp_glyph->object, glyph->object)
27501 && begpos <= tmp_glyph->charpos
27502 && tmp_glyph->charpos < endpos)))
27503 tmp_glyph++;
27504 gpos = glyph - tmp_glyph;
27506 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27507 the highlighted part of the displayed string to which
27508 GLYPH belongs. Note: GSEQ_LENGTH is different from
27509 SCHARS (STRING), because the latter returns the length of
27510 the internal string. */
27511 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27512 tmp_glyph > glyph
27513 && (!(EQ (tmp_glyph->object, glyph->object)
27514 && begpos <= tmp_glyph->charpos
27515 && tmp_glyph->charpos < endpos));
27516 tmp_glyph--)
27518 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27520 /* Calculate the total pixel width of all the glyphs between
27521 the beginning of the highlighted area and GLYPH. */
27522 total_pixel_width = 0;
27523 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27524 total_pixel_width += tmp_glyph->pixel_width;
27526 /* Pre calculation of re-rendering position. Note: X is in
27527 column units here, after the call to mode_line_string or
27528 marginal_area_string. */
27529 hpos = x - gpos;
27530 vpos = (area == ON_MODE_LINE
27531 ? (w->current_matrix)->nrows - 1
27532 : 0);
27534 /* If GLYPH's position is included in the region that is
27535 already drawn in mouse face, we have nothing to do. */
27536 if ( EQ (window, hlinfo->mouse_face_window)
27537 && (!row->reversed_p
27538 ? (hlinfo->mouse_face_beg_col <= hpos
27539 && hpos < hlinfo->mouse_face_end_col)
27540 /* In R2L rows we swap BEG and END, see below. */
27541 : (hlinfo->mouse_face_end_col <= hpos
27542 && hpos < hlinfo->mouse_face_beg_col))
27543 && hlinfo->mouse_face_beg_row == vpos )
27544 return;
27546 if (clear_mouse_face (hlinfo))
27547 cursor = No_Cursor;
27549 if (!row->reversed_p)
27551 hlinfo->mouse_face_beg_col = hpos;
27552 hlinfo->mouse_face_beg_x = original_x_pixel
27553 - (total_pixel_width + dx);
27554 hlinfo->mouse_face_end_col = hpos + gseq_length;
27555 hlinfo->mouse_face_end_x = 0;
27557 else
27559 /* In R2L rows, show_mouse_face expects BEG and END
27560 coordinates to be swapped. */
27561 hlinfo->mouse_face_end_col = hpos;
27562 hlinfo->mouse_face_end_x = original_x_pixel
27563 - (total_pixel_width + dx);
27564 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27565 hlinfo->mouse_face_beg_x = 0;
27568 hlinfo->mouse_face_beg_row = vpos;
27569 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27570 hlinfo->mouse_face_beg_y = 0;
27571 hlinfo->mouse_face_end_y = 0;
27572 hlinfo->mouse_face_past_end = 0;
27573 hlinfo->mouse_face_window = window;
27575 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27576 charpos,
27577 0, 0, 0,
27578 &ignore,
27579 glyph->face_id,
27581 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27583 if (NILP (pointer))
27584 pointer = Qhand;
27586 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27587 clear_mouse_face (hlinfo);
27589 #ifdef HAVE_WINDOW_SYSTEM
27590 if (FRAME_WINDOW_P (f))
27591 define_frame_cursor1 (f, cursor, pointer);
27592 #endif
27596 /* EXPORT:
27597 Take proper action when the mouse has moved to position X, Y on
27598 frame F as regards highlighting characters that have mouse-face
27599 properties. Also de-highlighting chars where the mouse was before.
27600 X and Y can be negative or out of range. */
27602 void
27603 note_mouse_highlight (struct frame *f, int x, int y)
27605 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27606 enum window_part part = ON_NOTHING;
27607 Lisp_Object window;
27608 struct window *w;
27609 Cursor cursor = No_Cursor;
27610 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27611 struct buffer *b;
27613 /* When a menu is active, don't highlight because this looks odd. */
27614 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27615 if (popup_activated ())
27616 return;
27617 #endif
27619 if (NILP (Vmouse_highlight)
27620 || !f->glyphs_initialized_p
27621 || f->pointer_invisible)
27622 return;
27624 hlinfo->mouse_face_mouse_x = x;
27625 hlinfo->mouse_face_mouse_y = y;
27626 hlinfo->mouse_face_mouse_frame = f;
27628 if (hlinfo->mouse_face_defer)
27629 return;
27631 if (gc_in_progress)
27633 hlinfo->mouse_face_deferred_gc = 1;
27634 return;
27637 /* Which window is that in? */
27638 window = window_from_coordinates (f, x, y, &part, 1);
27640 /* If displaying active text in another window, clear that. */
27641 if (! EQ (window, hlinfo->mouse_face_window)
27642 /* Also clear if we move out of text area in same window. */
27643 || (!NILP (hlinfo->mouse_face_window)
27644 && !NILP (window)
27645 && part != ON_TEXT
27646 && part != ON_MODE_LINE
27647 && part != ON_HEADER_LINE))
27648 clear_mouse_face (hlinfo);
27650 /* Not on a window -> return. */
27651 if (!WINDOWP (window))
27652 return;
27654 /* Reset help_echo_string. It will get recomputed below. */
27655 help_echo_string = Qnil;
27657 /* Convert to window-relative pixel coordinates. */
27658 w = XWINDOW (window);
27659 frame_to_window_pixel_xy (w, &x, &y);
27661 #ifdef HAVE_WINDOW_SYSTEM
27662 /* Handle tool-bar window differently since it doesn't display a
27663 buffer. */
27664 if (EQ (window, f->tool_bar_window))
27666 note_tool_bar_highlight (f, x, y);
27667 return;
27669 #endif
27671 /* Mouse is on the mode, header line or margin? */
27672 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27673 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27675 note_mode_line_or_margin_highlight (window, x, y, part);
27676 return;
27679 #ifdef HAVE_WINDOW_SYSTEM
27680 if (part == ON_VERTICAL_BORDER)
27682 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27683 help_echo_string = build_string ("drag-mouse-1: resize");
27685 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27686 || part == ON_SCROLL_BAR)
27687 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27688 else
27689 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27690 #endif
27692 /* Are we in a window whose display is up to date?
27693 And verify the buffer's text has not changed. */
27694 b = XBUFFER (w->buffer);
27695 if (part == ON_TEXT
27696 && EQ (w->window_end_valid, w->buffer)
27697 && w->last_modified == BUF_MODIFF (b)
27698 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27700 int hpos, vpos, dx, dy, area = LAST_AREA;
27701 ptrdiff_t pos;
27702 struct glyph *glyph;
27703 Lisp_Object object;
27704 Lisp_Object mouse_face = Qnil, position;
27705 Lisp_Object *overlay_vec = NULL;
27706 ptrdiff_t i, noverlays;
27707 struct buffer *obuf;
27708 ptrdiff_t obegv, ozv;
27709 int same_region;
27711 /* Find the glyph under X/Y. */
27712 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27714 #ifdef HAVE_WINDOW_SYSTEM
27715 /* Look for :pointer property on image. */
27716 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27718 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27719 if (img != NULL && IMAGEP (img->spec))
27721 Lisp_Object image_map, hotspot;
27722 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27723 !NILP (image_map))
27724 && (hotspot = find_hot_spot (image_map,
27725 glyph->slice.img.x + dx,
27726 glyph->slice.img.y + dy),
27727 CONSP (hotspot))
27728 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27730 Lisp_Object plist;
27732 /* Could check XCAR (hotspot) to see if we enter/leave
27733 this hot-spot.
27734 If so, we could look for mouse-enter, mouse-leave
27735 properties in PLIST (and do something...). */
27736 hotspot = XCDR (hotspot);
27737 if (CONSP (hotspot)
27738 && (plist = XCAR (hotspot), CONSP (plist)))
27740 pointer = Fplist_get (plist, Qpointer);
27741 if (NILP (pointer))
27742 pointer = Qhand;
27743 help_echo_string = Fplist_get (plist, Qhelp_echo);
27744 if (!NILP (help_echo_string))
27746 help_echo_window = window;
27747 help_echo_object = glyph->object;
27748 help_echo_pos = glyph->charpos;
27752 if (NILP (pointer))
27753 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27756 #endif /* HAVE_WINDOW_SYSTEM */
27758 /* Clear mouse face if X/Y not over text. */
27759 if (glyph == NULL
27760 || area != TEXT_AREA
27761 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27762 /* Glyph's OBJECT is an integer for glyphs inserted by the
27763 display engine for its internal purposes, like truncation
27764 and continuation glyphs and blanks beyond the end of
27765 line's text on text terminals. If we are over such a
27766 glyph, we are not over any text. */
27767 || INTEGERP (glyph->object)
27768 /* R2L rows have a stretch glyph at their front, which
27769 stands for no text, whereas L2R rows have no glyphs at
27770 all beyond the end of text. Treat such stretch glyphs
27771 like we do with NULL glyphs in L2R rows. */
27772 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27773 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27774 && glyph->type == STRETCH_GLYPH
27775 && glyph->avoid_cursor_p))
27777 if (clear_mouse_face (hlinfo))
27778 cursor = No_Cursor;
27779 #ifdef HAVE_WINDOW_SYSTEM
27780 if (FRAME_WINDOW_P (f) && NILP (pointer))
27782 if (area != TEXT_AREA)
27783 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27784 else
27785 pointer = Vvoid_text_area_pointer;
27787 #endif
27788 goto set_cursor;
27791 pos = glyph->charpos;
27792 object = glyph->object;
27793 if (!STRINGP (object) && !BUFFERP (object))
27794 goto set_cursor;
27796 /* If we get an out-of-range value, return now; avoid an error. */
27797 if (BUFFERP (object) && pos > BUF_Z (b))
27798 goto set_cursor;
27800 /* Make the window's buffer temporarily current for
27801 overlays_at and compute_char_face. */
27802 obuf = current_buffer;
27803 current_buffer = b;
27804 obegv = BEGV;
27805 ozv = ZV;
27806 BEGV = BEG;
27807 ZV = Z;
27809 /* Is this char mouse-active or does it have help-echo? */
27810 position = make_number (pos);
27812 if (BUFFERP (object))
27814 /* Put all the overlays we want in a vector in overlay_vec. */
27815 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27816 /* Sort overlays into increasing priority order. */
27817 noverlays = sort_overlays (overlay_vec, noverlays, w);
27819 else
27820 noverlays = 0;
27822 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27824 if (same_region)
27825 cursor = No_Cursor;
27827 /* Check mouse-face highlighting. */
27828 if (! same_region
27829 /* If there exists an overlay with mouse-face overlapping
27830 the one we are currently highlighting, we have to
27831 check if we enter the overlapping overlay, and then
27832 highlight only that. */
27833 || (OVERLAYP (hlinfo->mouse_face_overlay)
27834 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27836 /* Find the highest priority overlay with a mouse-face. */
27837 Lisp_Object overlay = Qnil;
27838 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27840 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27841 if (!NILP (mouse_face))
27842 overlay = overlay_vec[i];
27845 /* If we're highlighting the same overlay as before, there's
27846 no need to do that again. */
27847 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27848 goto check_help_echo;
27849 hlinfo->mouse_face_overlay = overlay;
27851 /* Clear the display of the old active region, if any. */
27852 if (clear_mouse_face (hlinfo))
27853 cursor = No_Cursor;
27855 /* If no overlay applies, get a text property. */
27856 if (NILP (overlay))
27857 mouse_face = Fget_text_property (position, Qmouse_face, object);
27859 /* Next, compute the bounds of the mouse highlighting and
27860 display it. */
27861 if (!NILP (mouse_face) && STRINGP (object))
27863 /* The mouse-highlighting comes from a display string
27864 with a mouse-face. */
27865 Lisp_Object s, e;
27866 ptrdiff_t ignore;
27868 s = Fprevious_single_property_change
27869 (make_number (pos + 1), Qmouse_face, object, Qnil);
27870 e = Fnext_single_property_change
27871 (position, Qmouse_face, object, Qnil);
27872 if (NILP (s))
27873 s = make_number (0);
27874 if (NILP (e))
27875 e = make_number (SCHARS (object) - 1);
27876 mouse_face_from_string_pos (w, hlinfo, object,
27877 XINT (s), XINT (e));
27878 hlinfo->mouse_face_past_end = 0;
27879 hlinfo->mouse_face_window = window;
27880 hlinfo->mouse_face_face_id
27881 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27882 glyph->face_id, 1);
27883 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27884 cursor = No_Cursor;
27886 else
27888 /* The mouse-highlighting, if any, comes from an overlay
27889 or text property in the buffer. */
27890 Lisp_Object buffer IF_LINT (= Qnil);
27891 Lisp_Object disp_string IF_LINT (= Qnil);
27893 if (STRINGP (object))
27895 /* If we are on a display string with no mouse-face,
27896 check if the text under it has one. */
27897 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27898 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27899 pos = string_buffer_position (object, start);
27900 if (pos > 0)
27902 mouse_face = get_char_property_and_overlay
27903 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27904 buffer = w->buffer;
27905 disp_string = object;
27908 else
27910 buffer = object;
27911 disp_string = Qnil;
27914 if (!NILP (mouse_face))
27916 Lisp_Object before, after;
27917 Lisp_Object before_string, after_string;
27918 /* To correctly find the limits of mouse highlight
27919 in a bidi-reordered buffer, we must not use the
27920 optimization of limiting the search in
27921 previous-single-property-change and
27922 next-single-property-change, because
27923 rows_from_pos_range needs the real start and end
27924 positions to DTRT in this case. That's because
27925 the first row visible in a window does not
27926 necessarily display the character whose position
27927 is the smallest. */
27928 Lisp_Object lim1 =
27929 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27930 ? Fmarker_position (w->start)
27931 : Qnil;
27932 Lisp_Object lim2 =
27933 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27934 ? make_number (BUF_Z (XBUFFER (buffer))
27935 - XFASTINT (w->window_end_pos))
27936 : Qnil;
27938 if (NILP (overlay))
27940 /* Handle the text property case. */
27941 before = Fprevious_single_property_change
27942 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27943 after = Fnext_single_property_change
27944 (make_number (pos), Qmouse_face, buffer, lim2);
27945 before_string = after_string = Qnil;
27947 else
27949 /* Handle the overlay case. */
27950 before = Foverlay_start (overlay);
27951 after = Foverlay_end (overlay);
27952 before_string = Foverlay_get (overlay, Qbefore_string);
27953 after_string = Foverlay_get (overlay, Qafter_string);
27955 if (!STRINGP (before_string)) before_string = Qnil;
27956 if (!STRINGP (after_string)) after_string = Qnil;
27959 mouse_face_from_buffer_pos (window, hlinfo, pos,
27960 NILP (before)
27962 : XFASTINT (before),
27963 NILP (after)
27964 ? BUF_Z (XBUFFER (buffer))
27965 : XFASTINT (after),
27966 before_string, after_string,
27967 disp_string);
27968 cursor = No_Cursor;
27973 check_help_echo:
27975 /* Look for a `help-echo' property. */
27976 if (NILP (help_echo_string)) {
27977 Lisp_Object help, overlay;
27979 /* Check overlays first. */
27980 help = overlay = Qnil;
27981 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27983 overlay = overlay_vec[i];
27984 help = Foverlay_get (overlay, Qhelp_echo);
27987 if (!NILP (help))
27989 help_echo_string = help;
27990 help_echo_window = window;
27991 help_echo_object = overlay;
27992 help_echo_pos = pos;
27994 else
27996 Lisp_Object obj = glyph->object;
27997 ptrdiff_t charpos = glyph->charpos;
27999 /* Try text properties. */
28000 if (STRINGP (obj)
28001 && charpos >= 0
28002 && charpos < SCHARS (obj))
28004 help = Fget_text_property (make_number (charpos),
28005 Qhelp_echo, obj);
28006 if (NILP (help))
28008 /* If the string itself doesn't specify a help-echo,
28009 see if the buffer text ``under'' it does. */
28010 struct glyph_row *r
28011 = MATRIX_ROW (w->current_matrix, vpos);
28012 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28013 ptrdiff_t p = string_buffer_position (obj, start);
28014 if (p > 0)
28016 help = Fget_char_property (make_number (p),
28017 Qhelp_echo, w->buffer);
28018 if (!NILP (help))
28020 charpos = p;
28021 obj = w->buffer;
28026 else if (BUFFERP (obj)
28027 && charpos >= BEGV
28028 && charpos < ZV)
28029 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28030 obj);
28032 if (!NILP (help))
28034 help_echo_string = help;
28035 help_echo_window = window;
28036 help_echo_object = obj;
28037 help_echo_pos = charpos;
28042 #ifdef HAVE_WINDOW_SYSTEM
28043 /* Look for a `pointer' property. */
28044 if (FRAME_WINDOW_P (f) && NILP (pointer))
28046 /* Check overlays first. */
28047 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28048 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28050 if (NILP (pointer))
28052 Lisp_Object obj = glyph->object;
28053 ptrdiff_t charpos = glyph->charpos;
28055 /* Try text properties. */
28056 if (STRINGP (obj)
28057 && charpos >= 0
28058 && charpos < SCHARS (obj))
28060 pointer = Fget_text_property (make_number (charpos),
28061 Qpointer, obj);
28062 if (NILP (pointer))
28064 /* If the string itself doesn't specify a pointer,
28065 see if the buffer text ``under'' it does. */
28066 struct glyph_row *r
28067 = MATRIX_ROW (w->current_matrix, vpos);
28068 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28069 ptrdiff_t p = string_buffer_position (obj, start);
28070 if (p > 0)
28071 pointer = Fget_char_property (make_number (p),
28072 Qpointer, w->buffer);
28075 else if (BUFFERP (obj)
28076 && charpos >= BEGV
28077 && charpos < ZV)
28078 pointer = Fget_text_property (make_number (charpos),
28079 Qpointer, obj);
28082 #endif /* HAVE_WINDOW_SYSTEM */
28084 BEGV = obegv;
28085 ZV = ozv;
28086 current_buffer = obuf;
28089 set_cursor:
28091 #ifdef HAVE_WINDOW_SYSTEM
28092 if (FRAME_WINDOW_P (f))
28093 define_frame_cursor1 (f, cursor, pointer);
28094 #else
28095 /* This is here to prevent a compiler error, about "label at end of
28096 compound statement". */
28097 return;
28098 #endif
28102 /* EXPORT for RIF:
28103 Clear any mouse-face on window W. This function is part of the
28104 redisplay interface, and is called from try_window_id and similar
28105 functions to ensure the mouse-highlight is off. */
28107 void
28108 x_clear_window_mouse_face (struct window *w)
28110 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28111 Lisp_Object window;
28113 BLOCK_INPUT;
28114 XSETWINDOW (window, w);
28115 if (EQ (window, hlinfo->mouse_face_window))
28116 clear_mouse_face (hlinfo);
28117 UNBLOCK_INPUT;
28121 /* EXPORT:
28122 Just discard the mouse face information for frame F, if any.
28123 This is used when the size of F is changed. */
28125 void
28126 cancel_mouse_face (struct frame *f)
28128 Lisp_Object window;
28129 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28131 window = hlinfo->mouse_face_window;
28132 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28134 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28135 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28136 hlinfo->mouse_face_window = Qnil;
28142 /***********************************************************************
28143 Exposure Events
28144 ***********************************************************************/
28146 #ifdef HAVE_WINDOW_SYSTEM
28148 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28149 which intersects rectangle R. R is in window-relative coordinates. */
28151 static void
28152 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28153 enum glyph_row_area area)
28155 struct glyph *first = row->glyphs[area];
28156 struct glyph *end = row->glyphs[area] + row->used[area];
28157 struct glyph *last;
28158 int first_x, start_x, x;
28160 if (area == TEXT_AREA && row->fill_line_p)
28161 /* If row extends face to end of line write the whole line. */
28162 draw_glyphs (w, 0, row, area,
28163 0, row->used[area],
28164 DRAW_NORMAL_TEXT, 0);
28165 else
28167 /* Set START_X to the window-relative start position for drawing glyphs of
28168 AREA. The first glyph of the text area can be partially visible.
28169 The first glyphs of other areas cannot. */
28170 start_x = window_box_left_offset (w, area);
28171 x = start_x;
28172 if (area == TEXT_AREA)
28173 x += row->x;
28175 /* Find the first glyph that must be redrawn. */
28176 while (first < end
28177 && x + first->pixel_width < r->x)
28179 x += first->pixel_width;
28180 ++first;
28183 /* Find the last one. */
28184 last = first;
28185 first_x = x;
28186 while (last < end
28187 && x < r->x + r->width)
28189 x += last->pixel_width;
28190 ++last;
28193 /* Repaint. */
28194 if (last > first)
28195 draw_glyphs (w, first_x - start_x, row, area,
28196 first - row->glyphs[area], last - row->glyphs[area],
28197 DRAW_NORMAL_TEXT, 0);
28202 /* Redraw the parts of the glyph row ROW on window W intersecting
28203 rectangle R. R is in window-relative coordinates. Value is
28204 non-zero if mouse-face was overwritten. */
28206 static int
28207 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28209 eassert (row->enabled_p);
28211 if (row->mode_line_p || w->pseudo_window_p)
28212 draw_glyphs (w, 0, row, TEXT_AREA,
28213 0, row->used[TEXT_AREA],
28214 DRAW_NORMAL_TEXT, 0);
28215 else
28217 if (row->used[LEFT_MARGIN_AREA])
28218 expose_area (w, row, r, LEFT_MARGIN_AREA);
28219 if (row->used[TEXT_AREA])
28220 expose_area (w, row, r, TEXT_AREA);
28221 if (row->used[RIGHT_MARGIN_AREA])
28222 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28223 draw_row_fringe_bitmaps (w, row);
28226 return row->mouse_face_p;
28230 /* Redraw those parts of glyphs rows during expose event handling that
28231 overlap other rows. Redrawing of an exposed line writes over parts
28232 of lines overlapping that exposed line; this function fixes that.
28234 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28235 row in W's current matrix that is exposed and overlaps other rows.
28236 LAST_OVERLAPPING_ROW is the last such row. */
28238 static void
28239 expose_overlaps (struct window *w,
28240 struct glyph_row *first_overlapping_row,
28241 struct glyph_row *last_overlapping_row,
28242 XRectangle *r)
28244 struct glyph_row *row;
28246 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28247 if (row->overlapping_p)
28249 eassert (row->enabled_p && !row->mode_line_p);
28251 row->clip = r;
28252 if (row->used[LEFT_MARGIN_AREA])
28253 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28255 if (row->used[TEXT_AREA])
28256 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28258 if (row->used[RIGHT_MARGIN_AREA])
28259 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28260 row->clip = NULL;
28265 /* Return non-zero if W's cursor intersects rectangle R. */
28267 static int
28268 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28270 XRectangle cr, result;
28271 struct glyph *cursor_glyph;
28272 struct glyph_row *row;
28274 if (w->phys_cursor.vpos >= 0
28275 && w->phys_cursor.vpos < w->current_matrix->nrows
28276 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28277 row->enabled_p)
28278 && row->cursor_in_fringe_p)
28280 /* Cursor is in the fringe. */
28281 cr.x = window_box_right_offset (w,
28282 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28283 ? RIGHT_MARGIN_AREA
28284 : TEXT_AREA));
28285 cr.y = row->y;
28286 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28287 cr.height = row->height;
28288 return x_intersect_rectangles (&cr, r, &result);
28291 cursor_glyph = get_phys_cursor_glyph (w);
28292 if (cursor_glyph)
28294 /* r is relative to W's box, but w->phys_cursor.x is relative
28295 to left edge of W's TEXT area. Adjust it. */
28296 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28297 cr.y = w->phys_cursor.y;
28298 cr.width = cursor_glyph->pixel_width;
28299 cr.height = w->phys_cursor_height;
28300 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28301 I assume the effect is the same -- and this is portable. */
28302 return x_intersect_rectangles (&cr, r, &result);
28304 /* If we don't understand the format, pretend we're not in the hot-spot. */
28305 return 0;
28309 /* EXPORT:
28310 Draw a vertical window border to the right of window W if W doesn't
28311 have vertical scroll bars. */
28313 void
28314 x_draw_vertical_border (struct window *w)
28316 struct frame *f = XFRAME (WINDOW_FRAME (w));
28318 /* We could do better, if we knew what type of scroll-bar the adjacent
28319 windows (on either side) have... But we don't :-(
28320 However, I think this works ok. ++KFS 2003-04-25 */
28322 /* Redraw borders between horizontally adjacent windows. Don't
28323 do it for frames with vertical scroll bars because either the
28324 right scroll bar of a window, or the left scroll bar of its
28325 neighbor will suffice as a border. */
28326 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28327 return;
28329 if (!WINDOW_RIGHTMOST_P (w)
28330 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28332 int x0, x1, y0, y1;
28334 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28335 y1 -= 1;
28337 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28338 x1 -= 1;
28340 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28342 else if (!WINDOW_LEFTMOST_P (w)
28343 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28345 int x0, x1, y0, y1;
28347 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28348 y1 -= 1;
28350 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28351 x0 -= 1;
28353 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28358 /* Redraw the part of window W intersection rectangle FR. Pixel
28359 coordinates in FR are frame-relative. Call this function with
28360 input blocked. Value is non-zero if the exposure overwrites
28361 mouse-face. */
28363 static int
28364 expose_window (struct window *w, XRectangle *fr)
28366 struct frame *f = XFRAME (w->frame);
28367 XRectangle wr, r;
28368 int mouse_face_overwritten_p = 0;
28370 /* If window is not yet fully initialized, do nothing. This can
28371 happen when toolkit scroll bars are used and a window is split.
28372 Reconfiguring the scroll bar will generate an expose for a newly
28373 created window. */
28374 if (w->current_matrix == NULL)
28375 return 0;
28377 /* When we're currently updating the window, display and current
28378 matrix usually don't agree. Arrange for a thorough display
28379 later. */
28380 if (w == updated_window)
28382 SET_FRAME_GARBAGED (f);
28383 return 0;
28386 /* Frame-relative pixel rectangle of W. */
28387 wr.x = WINDOW_LEFT_EDGE_X (w);
28388 wr.y = WINDOW_TOP_EDGE_Y (w);
28389 wr.width = WINDOW_TOTAL_WIDTH (w);
28390 wr.height = WINDOW_TOTAL_HEIGHT (w);
28392 if (x_intersect_rectangles (fr, &wr, &r))
28394 int yb = window_text_bottom_y (w);
28395 struct glyph_row *row;
28396 int cursor_cleared_p, phys_cursor_on_p;
28397 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28399 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28400 r.x, r.y, r.width, r.height));
28402 /* Convert to window coordinates. */
28403 r.x -= WINDOW_LEFT_EDGE_X (w);
28404 r.y -= WINDOW_TOP_EDGE_Y (w);
28406 /* Turn off the cursor. */
28407 if (!w->pseudo_window_p
28408 && phys_cursor_in_rect_p (w, &r))
28410 x_clear_cursor (w);
28411 cursor_cleared_p = 1;
28413 else
28414 cursor_cleared_p = 0;
28416 /* If the row containing the cursor extends face to end of line,
28417 then expose_area might overwrite the cursor outside the
28418 rectangle and thus notice_overwritten_cursor might clear
28419 w->phys_cursor_on_p. We remember the original value and
28420 check later if it is changed. */
28421 phys_cursor_on_p = w->phys_cursor_on_p;
28423 /* Update lines intersecting rectangle R. */
28424 first_overlapping_row = last_overlapping_row = NULL;
28425 for (row = w->current_matrix->rows;
28426 row->enabled_p;
28427 ++row)
28429 int y0 = row->y;
28430 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28432 if ((y0 >= r.y && y0 < r.y + r.height)
28433 || (y1 > r.y && y1 < r.y + r.height)
28434 || (r.y >= y0 && r.y < y1)
28435 || (r.y + r.height > y0 && r.y + r.height < y1))
28437 /* A header line may be overlapping, but there is no need
28438 to fix overlapping areas for them. KFS 2005-02-12 */
28439 if (row->overlapping_p && !row->mode_line_p)
28441 if (first_overlapping_row == NULL)
28442 first_overlapping_row = row;
28443 last_overlapping_row = row;
28446 row->clip = fr;
28447 if (expose_line (w, row, &r))
28448 mouse_face_overwritten_p = 1;
28449 row->clip = NULL;
28451 else if (row->overlapping_p)
28453 /* We must redraw a row overlapping the exposed area. */
28454 if (y0 < r.y
28455 ? y0 + row->phys_height > r.y
28456 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28458 if (first_overlapping_row == NULL)
28459 first_overlapping_row = row;
28460 last_overlapping_row = row;
28464 if (y1 >= yb)
28465 break;
28468 /* Display the mode line if there is one. */
28469 if (WINDOW_WANTS_MODELINE_P (w)
28470 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28471 row->enabled_p)
28472 && row->y < r.y + r.height)
28474 if (expose_line (w, row, &r))
28475 mouse_face_overwritten_p = 1;
28478 if (!w->pseudo_window_p)
28480 /* Fix the display of overlapping rows. */
28481 if (first_overlapping_row)
28482 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28483 fr);
28485 /* Draw border between windows. */
28486 x_draw_vertical_border (w);
28488 /* Turn the cursor on again. */
28489 if (cursor_cleared_p
28490 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28491 update_window_cursor (w, 1);
28495 return mouse_face_overwritten_p;
28500 /* Redraw (parts) of all windows in the window tree rooted at W that
28501 intersect R. R contains frame pixel coordinates. Value is
28502 non-zero if the exposure overwrites mouse-face. */
28504 static int
28505 expose_window_tree (struct window *w, XRectangle *r)
28507 struct frame *f = XFRAME (w->frame);
28508 int mouse_face_overwritten_p = 0;
28510 while (w && !FRAME_GARBAGED_P (f))
28512 if (!NILP (w->hchild))
28513 mouse_face_overwritten_p
28514 |= expose_window_tree (XWINDOW (w->hchild), r);
28515 else if (!NILP (w->vchild))
28516 mouse_face_overwritten_p
28517 |= expose_window_tree (XWINDOW (w->vchild), r);
28518 else
28519 mouse_face_overwritten_p |= expose_window (w, r);
28521 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28524 return mouse_face_overwritten_p;
28528 /* EXPORT:
28529 Redisplay an exposed area of frame F. X and Y are the upper-left
28530 corner of the exposed rectangle. W and H are width and height of
28531 the exposed area. All are pixel values. W or H zero means redraw
28532 the entire frame. */
28534 void
28535 expose_frame (struct frame *f, int x, int y, int w, int h)
28537 XRectangle r;
28538 int mouse_face_overwritten_p = 0;
28540 TRACE ((stderr, "expose_frame "));
28542 /* No need to redraw if frame will be redrawn soon. */
28543 if (FRAME_GARBAGED_P (f))
28545 TRACE ((stderr, " garbaged\n"));
28546 return;
28549 /* If basic faces haven't been realized yet, there is no point in
28550 trying to redraw anything. This can happen when we get an expose
28551 event while Emacs is starting, e.g. by moving another window. */
28552 if (FRAME_FACE_CACHE (f) == NULL
28553 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28555 TRACE ((stderr, " no faces\n"));
28556 return;
28559 if (w == 0 || h == 0)
28561 r.x = r.y = 0;
28562 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28563 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28565 else
28567 r.x = x;
28568 r.y = y;
28569 r.width = w;
28570 r.height = h;
28573 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28574 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28576 if (WINDOWP (f->tool_bar_window))
28577 mouse_face_overwritten_p
28578 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28580 #ifdef HAVE_X_WINDOWS
28581 #ifndef MSDOS
28582 #ifndef USE_X_TOOLKIT
28583 if (WINDOWP (f->menu_bar_window))
28584 mouse_face_overwritten_p
28585 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28586 #endif /* not USE_X_TOOLKIT */
28587 #endif
28588 #endif
28590 /* Some window managers support a focus-follows-mouse style with
28591 delayed raising of frames. Imagine a partially obscured frame,
28592 and moving the mouse into partially obscured mouse-face on that
28593 frame. The visible part of the mouse-face will be highlighted,
28594 then the WM raises the obscured frame. With at least one WM, KDE
28595 2.1, Emacs is not getting any event for the raising of the frame
28596 (even tried with SubstructureRedirectMask), only Expose events.
28597 These expose events will draw text normally, i.e. not
28598 highlighted. Which means we must redo the highlight here.
28599 Subsume it under ``we love X''. --gerd 2001-08-15 */
28600 /* Included in Windows version because Windows most likely does not
28601 do the right thing if any third party tool offers
28602 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28603 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28605 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28606 if (f == hlinfo->mouse_face_mouse_frame)
28608 int mouse_x = hlinfo->mouse_face_mouse_x;
28609 int mouse_y = hlinfo->mouse_face_mouse_y;
28610 clear_mouse_face (hlinfo);
28611 note_mouse_highlight (f, mouse_x, mouse_y);
28617 /* EXPORT:
28618 Determine the intersection of two rectangles R1 and R2. Return
28619 the intersection in *RESULT. Value is non-zero if RESULT is not
28620 empty. */
28623 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28625 XRectangle *left, *right;
28626 XRectangle *upper, *lower;
28627 int intersection_p = 0;
28629 /* Rearrange so that R1 is the left-most rectangle. */
28630 if (r1->x < r2->x)
28631 left = r1, right = r2;
28632 else
28633 left = r2, right = r1;
28635 /* X0 of the intersection is right.x0, if this is inside R1,
28636 otherwise there is no intersection. */
28637 if (right->x <= left->x + left->width)
28639 result->x = right->x;
28641 /* The right end of the intersection is the minimum of
28642 the right ends of left and right. */
28643 result->width = (min (left->x + left->width, right->x + right->width)
28644 - result->x);
28646 /* Same game for Y. */
28647 if (r1->y < r2->y)
28648 upper = r1, lower = r2;
28649 else
28650 upper = r2, lower = r1;
28652 /* The upper end of the intersection is lower.y0, if this is inside
28653 of upper. Otherwise, there is no intersection. */
28654 if (lower->y <= upper->y + upper->height)
28656 result->y = lower->y;
28658 /* The lower end of the intersection is the minimum of the lower
28659 ends of upper and lower. */
28660 result->height = (min (lower->y + lower->height,
28661 upper->y + upper->height)
28662 - result->y);
28663 intersection_p = 1;
28667 return intersection_p;
28670 #endif /* HAVE_WINDOW_SYSTEM */
28673 /***********************************************************************
28674 Initialization
28675 ***********************************************************************/
28677 void
28678 syms_of_xdisp (void)
28680 Vwith_echo_area_save_vector = Qnil;
28681 staticpro (&Vwith_echo_area_save_vector);
28683 Vmessage_stack = Qnil;
28684 staticpro (&Vmessage_stack);
28686 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28688 message_dolog_marker1 = Fmake_marker ();
28689 staticpro (&message_dolog_marker1);
28690 message_dolog_marker2 = Fmake_marker ();
28691 staticpro (&message_dolog_marker2);
28692 message_dolog_marker3 = Fmake_marker ();
28693 staticpro (&message_dolog_marker3);
28695 #ifdef GLYPH_DEBUG
28696 defsubr (&Sdump_frame_glyph_matrix);
28697 defsubr (&Sdump_glyph_matrix);
28698 defsubr (&Sdump_glyph_row);
28699 defsubr (&Sdump_tool_bar_row);
28700 defsubr (&Strace_redisplay);
28701 defsubr (&Strace_to_stderr);
28702 #endif
28703 #ifdef HAVE_WINDOW_SYSTEM
28704 defsubr (&Stool_bar_lines_needed);
28705 defsubr (&Slookup_image_map);
28706 #endif
28707 defsubr (&Sformat_mode_line);
28708 defsubr (&Sinvisible_p);
28709 defsubr (&Scurrent_bidi_paragraph_direction);
28711 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28712 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28713 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28714 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28715 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28716 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28717 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28718 DEFSYM (Qeval, "eval");
28719 DEFSYM (QCdata, ":data");
28720 DEFSYM (Qdisplay, "display");
28721 DEFSYM (Qspace_width, "space-width");
28722 DEFSYM (Qraise, "raise");
28723 DEFSYM (Qslice, "slice");
28724 DEFSYM (Qspace, "space");
28725 DEFSYM (Qmargin, "margin");
28726 DEFSYM (Qpointer, "pointer");
28727 DEFSYM (Qleft_margin, "left-margin");
28728 DEFSYM (Qright_margin, "right-margin");
28729 DEFSYM (Qcenter, "center");
28730 DEFSYM (Qline_height, "line-height");
28731 DEFSYM (QCalign_to, ":align-to");
28732 DEFSYM (QCrelative_width, ":relative-width");
28733 DEFSYM (QCrelative_height, ":relative-height");
28734 DEFSYM (QCeval, ":eval");
28735 DEFSYM (QCpropertize, ":propertize");
28736 DEFSYM (QCfile, ":file");
28737 DEFSYM (Qfontified, "fontified");
28738 DEFSYM (Qfontification_functions, "fontification-functions");
28739 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28740 DEFSYM (Qescape_glyph, "escape-glyph");
28741 DEFSYM (Qnobreak_space, "nobreak-space");
28742 DEFSYM (Qimage, "image");
28743 DEFSYM (Qtext, "text");
28744 DEFSYM (Qboth, "both");
28745 DEFSYM (Qboth_horiz, "both-horiz");
28746 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28747 DEFSYM (QCmap, ":map");
28748 DEFSYM (QCpointer, ":pointer");
28749 DEFSYM (Qrect, "rect");
28750 DEFSYM (Qcircle, "circle");
28751 DEFSYM (Qpoly, "poly");
28752 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28753 DEFSYM (Qgrow_only, "grow-only");
28754 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28755 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28756 DEFSYM (Qposition, "position");
28757 DEFSYM (Qbuffer_position, "buffer-position");
28758 DEFSYM (Qobject, "object");
28759 DEFSYM (Qbar, "bar");
28760 DEFSYM (Qhbar, "hbar");
28761 DEFSYM (Qbox, "box");
28762 DEFSYM (Qhollow, "hollow");
28763 DEFSYM (Qhand, "hand");
28764 DEFSYM (Qarrow, "arrow");
28765 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28767 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28768 Fcons (intern_c_string ("void-variable"), Qnil)),
28769 Qnil);
28770 staticpro (&list_of_error);
28772 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28773 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28774 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28775 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28777 echo_buffer[0] = echo_buffer[1] = Qnil;
28778 staticpro (&echo_buffer[0]);
28779 staticpro (&echo_buffer[1]);
28781 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28782 staticpro (&echo_area_buffer[0]);
28783 staticpro (&echo_area_buffer[1]);
28785 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28786 staticpro (&Vmessages_buffer_name);
28788 mode_line_proptrans_alist = Qnil;
28789 staticpro (&mode_line_proptrans_alist);
28790 mode_line_string_list = Qnil;
28791 staticpro (&mode_line_string_list);
28792 mode_line_string_face = Qnil;
28793 staticpro (&mode_line_string_face);
28794 mode_line_string_face_prop = Qnil;
28795 staticpro (&mode_line_string_face_prop);
28796 Vmode_line_unwind_vector = Qnil;
28797 staticpro (&Vmode_line_unwind_vector);
28799 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28801 help_echo_string = Qnil;
28802 staticpro (&help_echo_string);
28803 help_echo_object = Qnil;
28804 staticpro (&help_echo_object);
28805 help_echo_window = Qnil;
28806 staticpro (&help_echo_window);
28807 previous_help_echo_string = Qnil;
28808 staticpro (&previous_help_echo_string);
28809 help_echo_pos = -1;
28811 DEFSYM (Qright_to_left, "right-to-left");
28812 DEFSYM (Qleft_to_right, "left-to-right");
28814 #ifdef HAVE_WINDOW_SYSTEM
28815 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28816 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28817 For example, if a block cursor is over a tab, it will be drawn as
28818 wide as that tab on the display. */);
28819 x_stretch_cursor_p = 0;
28820 #endif
28822 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28823 doc: /* Non-nil means highlight trailing whitespace.
28824 The face used for trailing whitespace is `trailing-whitespace'. */);
28825 Vshow_trailing_whitespace = Qnil;
28827 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28828 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28829 If the value is t, Emacs highlights non-ASCII chars which have the
28830 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28831 or `escape-glyph' face respectively.
28833 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28834 U+2011 (non-breaking hyphen) are affected.
28836 Any other non-nil value means to display these characters as a escape
28837 glyph followed by an ordinary space or hyphen.
28839 A value of nil means no special handling of these characters. */);
28840 Vnobreak_char_display = Qt;
28842 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28843 doc: /* The pointer shape to show in void text areas.
28844 A value of nil means to show the text pointer. Other options are `arrow',
28845 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28846 Vvoid_text_area_pointer = Qarrow;
28848 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28849 doc: /* Non-nil means don't actually do any redisplay.
28850 This is used for internal purposes. */);
28851 Vinhibit_redisplay = Qnil;
28853 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28854 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28855 Vglobal_mode_string = Qnil;
28857 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28858 doc: /* Marker for where to display an arrow on top of the buffer text.
28859 This must be the beginning of a line in order to work.
28860 See also `overlay-arrow-string'. */);
28861 Voverlay_arrow_position = Qnil;
28863 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28864 doc: /* String to display as an arrow in non-window frames.
28865 See also `overlay-arrow-position'. */);
28866 Voverlay_arrow_string = build_pure_c_string ("=>");
28868 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28869 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28870 The symbols on this list are examined during redisplay to determine
28871 where to display overlay arrows. */);
28872 Voverlay_arrow_variable_list
28873 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28875 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28876 doc: /* The number of lines to try scrolling a window by when point moves out.
28877 If that fails to bring point back on frame, point is centered instead.
28878 If this is zero, point is always centered after it moves off frame.
28879 If you want scrolling to always be a line at a time, you should set
28880 `scroll-conservatively' to a large value rather than set this to 1. */);
28882 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28883 doc: /* Scroll up to this many lines, to bring point back on screen.
28884 If point moves off-screen, redisplay will scroll by up to
28885 `scroll-conservatively' lines in order to bring point just barely
28886 onto the screen again. If that cannot be done, then redisplay
28887 recenters point as usual.
28889 If the value is greater than 100, redisplay will never recenter point,
28890 but will always scroll just enough text to bring point into view, even
28891 if you move far away.
28893 A value of zero means always recenter point if it moves off screen. */);
28894 scroll_conservatively = 0;
28896 DEFVAR_INT ("scroll-margin", scroll_margin,
28897 doc: /* Number of lines of margin at the top and bottom of a window.
28898 Recenter the window whenever point gets within this many lines
28899 of the top or bottom of the window. */);
28900 scroll_margin = 0;
28902 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28903 doc: /* Pixels per inch value for non-window system displays.
28904 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28905 Vdisplay_pixels_per_inch = make_float (72.0);
28907 #ifdef GLYPH_DEBUG
28908 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28909 #endif
28911 DEFVAR_LISP ("truncate-partial-width-windows",
28912 Vtruncate_partial_width_windows,
28913 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28914 For an integer value, truncate lines in each window narrower than the
28915 full frame width, provided the window width is less than that integer;
28916 otherwise, respect the value of `truncate-lines'.
28918 For any other non-nil value, truncate lines in all windows that do
28919 not span the full frame width.
28921 A value of nil means to respect the value of `truncate-lines'.
28923 If `word-wrap' is enabled, you might want to reduce this. */);
28924 Vtruncate_partial_width_windows = make_number (50);
28926 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28927 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28928 Any other value means to use the appropriate face, `mode-line',
28929 `header-line', or `menu' respectively. */);
28930 mode_line_inverse_video = 1;
28932 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28933 doc: /* Maximum buffer size for which line number should be displayed.
28934 If the buffer is bigger than this, the line number does not appear
28935 in the mode line. A value of nil means no limit. */);
28936 Vline_number_display_limit = Qnil;
28938 DEFVAR_INT ("line-number-display-limit-width",
28939 line_number_display_limit_width,
28940 doc: /* Maximum line width (in characters) for line number display.
28941 If the average length of the lines near point is bigger than this, then the
28942 line number may be omitted from the mode line. */);
28943 line_number_display_limit_width = 200;
28945 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28946 doc: /* Non-nil means highlight region even in nonselected windows. */);
28947 highlight_nonselected_windows = 0;
28949 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28950 doc: /* Non-nil if more than one frame is visible on this display.
28951 Minibuffer-only frames don't count, but iconified frames do.
28952 This variable is not guaranteed to be accurate except while processing
28953 `frame-title-format' and `icon-title-format'. */);
28955 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28956 doc: /* Template for displaying the title bar of visible frames.
28957 \(Assuming the window manager supports this feature.)
28959 This variable has the same structure as `mode-line-format', except that
28960 the %c and %l constructs are ignored. It is used only on frames for
28961 which no explicit name has been set \(see `modify-frame-parameters'). */);
28963 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28964 doc: /* Template for displaying the title bar of an iconified frame.
28965 \(Assuming the window manager supports this feature.)
28966 This variable has the same structure as `mode-line-format' (which see),
28967 and is used only on frames for which no explicit name has been set
28968 \(see `modify-frame-parameters'). */);
28969 Vicon_title_format
28970 = Vframe_title_format
28971 = listn (CONSTYPE_PURE, 3,
28972 intern_c_string ("multiple-frames"),
28973 build_pure_c_string ("%b"),
28974 listn (CONSTYPE_PURE, 4,
28975 empty_unibyte_string,
28976 intern_c_string ("invocation-name"),
28977 build_pure_c_string ("@"),
28978 intern_c_string ("system-name")));
28980 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28981 doc: /* Maximum number of lines to keep in the message log buffer.
28982 If nil, disable message logging. If t, log messages but don't truncate
28983 the buffer when it becomes large. */);
28984 Vmessage_log_max = make_number (100);
28986 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28987 doc: /* Functions called before redisplay, if window sizes have changed.
28988 The value should be a list of functions that take one argument.
28989 Just before redisplay, for each frame, if any of its windows have changed
28990 size since the last redisplay, or have been split or deleted,
28991 all the functions in the list are called, with the frame as argument. */);
28992 Vwindow_size_change_functions = Qnil;
28994 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28995 doc: /* List of functions to call before redisplaying a window with scrolling.
28996 Each function is called with two arguments, the window and its new
28997 display-start position. Note that these functions are also called by
28998 `set-window-buffer'. Also note that the value of `window-end' is not
28999 valid when these functions are called.
29001 Warning: Do not use this feature to alter the way the window
29002 is scrolled. It is not designed for that, and such use probably won't
29003 work. */);
29004 Vwindow_scroll_functions = Qnil;
29006 DEFVAR_LISP ("window-text-change-functions",
29007 Vwindow_text_change_functions,
29008 doc: /* Functions to call in redisplay when text in the window might change. */);
29009 Vwindow_text_change_functions = Qnil;
29011 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29012 doc: /* Functions called when redisplay of a window reaches the end trigger.
29013 Each function is called with two arguments, the window and the end trigger value.
29014 See `set-window-redisplay-end-trigger'. */);
29015 Vredisplay_end_trigger_functions = Qnil;
29017 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29018 doc: /* Non-nil means autoselect window with mouse pointer.
29019 If nil, do not autoselect windows.
29020 A positive number means delay autoselection by that many seconds: a
29021 window is autoselected only after the mouse has remained in that
29022 window for the duration of the delay.
29023 A negative number has a similar effect, but causes windows to be
29024 autoselected only after the mouse has stopped moving. \(Because of
29025 the way Emacs compares mouse events, you will occasionally wait twice
29026 that time before the window gets selected.\)
29027 Any other value means to autoselect window instantaneously when the
29028 mouse pointer enters it.
29030 Autoselection selects the minibuffer only if it is active, and never
29031 unselects the minibuffer if it is active.
29033 When customizing this variable make sure that the actual value of
29034 `focus-follows-mouse' matches the behavior of your window manager. */);
29035 Vmouse_autoselect_window = Qnil;
29037 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29038 doc: /* Non-nil means automatically resize tool-bars.
29039 This dynamically changes the tool-bar's height to the minimum height
29040 that is needed to make all tool-bar items visible.
29041 If value is `grow-only', the tool-bar's height is only increased
29042 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29043 Vauto_resize_tool_bars = Qt;
29045 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29046 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29047 auto_raise_tool_bar_buttons_p = 1;
29049 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29050 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29051 make_cursor_line_fully_visible_p = 1;
29053 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29054 doc: /* Border below tool-bar in pixels.
29055 If an integer, use it as the height of the border.
29056 If it is one of `internal-border-width' or `border-width', use the
29057 value of the corresponding frame parameter.
29058 Otherwise, no border is added below the tool-bar. */);
29059 Vtool_bar_border = Qinternal_border_width;
29061 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29062 doc: /* Margin around tool-bar buttons in pixels.
29063 If an integer, use that for both horizontal and vertical margins.
29064 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29065 HORZ specifying the horizontal margin, and VERT specifying the
29066 vertical margin. */);
29067 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29069 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29070 doc: /* Relief thickness of tool-bar buttons. */);
29071 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29073 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29074 doc: /* Tool bar style to use.
29075 It can be one of
29076 image - show images only
29077 text - show text only
29078 both - show both, text below image
29079 both-horiz - show text to the right of the image
29080 text-image-horiz - show text to the left of the image
29081 any other - use system default or image if no system default.
29083 This variable only affects the GTK+ toolkit version of Emacs. */);
29084 Vtool_bar_style = Qnil;
29086 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29087 doc: /* Maximum number of characters a label can have to be shown.
29088 The tool bar style must also show labels for this to have any effect, see
29089 `tool-bar-style'. */);
29090 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29092 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29093 doc: /* List of functions to call to fontify regions of text.
29094 Each function is called with one argument POS. Functions must
29095 fontify a region starting at POS in the current buffer, and give
29096 fontified regions the property `fontified'. */);
29097 Vfontification_functions = Qnil;
29098 Fmake_variable_buffer_local (Qfontification_functions);
29100 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29101 unibyte_display_via_language_environment,
29102 doc: /* Non-nil means display unibyte text according to language environment.
29103 Specifically, this means that raw bytes in the range 160-255 decimal
29104 are displayed by converting them to the equivalent multibyte characters
29105 according to the current language environment. As a result, they are
29106 displayed according to the current fontset.
29108 Note that this variable affects only how these bytes are displayed,
29109 but does not change the fact they are interpreted as raw bytes. */);
29110 unibyte_display_via_language_environment = 0;
29112 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29113 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29114 If a float, it specifies a fraction of the mini-window frame's height.
29115 If an integer, it specifies a number of lines. */);
29116 Vmax_mini_window_height = make_float (0.25);
29118 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29119 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29120 A value of nil means don't automatically resize mini-windows.
29121 A value of t means resize them to fit the text displayed in them.
29122 A value of `grow-only', the default, means let mini-windows grow only;
29123 they return to their normal size when the minibuffer is closed, or the
29124 echo area becomes empty. */);
29125 Vresize_mini_windows = Qgrow_only;
29127 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29128 doc: /* Alist specifying how to blink the cursor off.
29129 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29130 `cursor-type' frame-parameter or variable equals ON-STATE,
29131 comparing using `equal', Emacs uses OFF-STATE to specify
29132 how to blink it off. ON-STATE and OFF-STATE are values for
29133 the `cursor-type' frame parameter.
29135 If a frame's ON-STATE has no entry in this list,
29136 the frame's other specifications determine how to blink the cursor off. */);
29137 Vblink_cursor_alist = Qnil;
29139 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29140 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29141 If non-nil, windows are automatically scrolled horizontally to make
29142 point visible. */);
29143 automatic_hscrolling_p = 1;
29144 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29146 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29147 doc: /* How many columns away from the window edge point is allowed to get
29148 before automatic hscrolling will horizontally scroll the window. */);
29149 hscroll_margin = 5;
29151 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29152 doc: /* How many columns to scroll the window when point gets too close to the edge.
29153 When point is less than `hscroll-margin' columns from the window
29154 edge, automatic hscrolling will scroll the window by the amount of columns
29155 determined by this variable. If its value is a positive integer, scroll that
29156 many columns. If it's a positive floating-point number, it specifies the
29157 fraction of the window's width to scroll. If it's nil or zero, point will be
29158 centered horizontally after the scroll. Any other value, including negative
29159 numbers, are treated as if the value were zero.
29161 Automatic hscrolling always moves point outside the scroll margin, so if
29162 point was more than scroll step columns inside the margin, the window will
29163 scroll more than the value given by the scroll step.
29165 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29166 and `scroll-right' overrides this variable's effect. */);
29167 Vhscroll_step = make_number (0);
29169 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29170 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29171 Bind this around calls to `message' to let it take effect. */);
29172 message_truncate_lines = 0;
29174 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29175 doc: /* Normal hook run to update the menu bar definitions.
29176 Redisplay runs this hook before it redisplays the menu bar.
29177 This is used to update submenus such as Buffers,
29178 whose contents depend on various data. */);
29179 Vmenu_bar_update_hook = Qnil;
29181 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29182 doc: /* Frame for which we are updating a menu.
29183 The enable predicate for a menu binding should check this variable. */);
29184 Vmenu_updating_frame = Qnil;
29186 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29187 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29188 inhibit_menubar_update = 0;
29190 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29191 doc: /* Prefix prepended to all continuation lines at display time.
29192 The value may be a string, an image, or a stretch-glyph; it is
29193 interpreted in the same way as the value of a `display' text property.
29195 This variable is overridden by any `wrap-prefix' text or overlay
29196 property.
29198 To add a prefix to non-continuation lines, use `line-prefix'. */);
29199 Vwrap_prefix = Qnil;
29200 DEFSYM (Qwrap_prefix, "wrap-prefix");
29201 Fmake_variable_buffer_local (Qwrap_prefix);
29203 DEFVAR_LISP ("line-prefix", Vline_prefix,
29204 doc: /* Prefix prepended to all non-continuation lines at display time.
29205 The value may be a string, an image, or a stretch-glyph; it is
29206 interpreted in the same way as the value of a `display' text property.
29208 This variable is overridden by any `line-prefix' text or overlay
29209 property.
29211 To add a prefix to continuation lines, use `wrap-prefix'. */);
29212 Vline_prefix = Qnil;
29213 DEFSYM (Qline_prefix, "line-prefix");
29214 Fmake_variable_buffer_local (Qline_prefix);
29216 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29217 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29218 inhibit_eval_during_redisplay = 0;
29220 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29221 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29222 inhibit_free_realized_faces = 0;
29224 #ifdef GLYPH_DEBUG
29225 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29226 doc: /* Inhibit try_window_id display optimization. */);
29227 inhibit_try_window_id = 0;
29229 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29230 doc: /* Inhibit try_window_reusing display optimization. */);
29231 inhibit_try_window_reusing = 0;
29233 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29234 doc: /* Inhibit try_cursor_movement display optimization. */);
29235 inhibit_try_cursor_movement = 0;
29236 #endif /* GLYPH_DEBUG */
29238 DEFVAR_INT ("overline-margin", overline_margin,
29239 doc: /* Space between overline and text, in pixels.
29240 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29241 margin to the character height. */);
29242 overline_margin = 2;
29244 DEFVAR_INT ("underline-minimum-offset",
29245 underline_minimum_offset,
29246 doc: /* Minimum distance between baseline and underline.
29247 This can improve legibility of underlined text at small font sizes,
29248 particularly when using variable `x-use-underline-position-properties'
29249 with fonts that specify an UNDERLINE_POSITION relatively close to the
29250 baseline. The default value is 1. */);
29251 underline_minimum_offset = 1;
29253 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29254 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29255 This feature only works when on a window system that can change
29256 cursor shapes. */);
29257 display_hourglass_p = 1;
29259 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29260 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29261 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29263 hourglass_atimer = NULL;
29264 hourglass_shown_p = 0;
29266 DEFSYM (Qglyphless_char, "glyphless-char");
29267 DEFSYM (Qhex_code, "hex-code");
29268 DEFSYM (Qempty_box, "empty-box");
29269 DEFSYM (Qthin_space, "thin-space");
29270 DEFSYM (Qzero_width, "zero-width");
29272 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29273 /* Intern this now in case it isn't already done.
29274 Setting this variable twice is harmless.
29275 But don't staticpro it here--that is done in alloc.c. */
29276 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29277 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29279 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29280 doc: /* Char-table defining glyphless characters.
29281 Each element, if non-nil, should be one of the following:
29282 an ASCII acronym string: display this string in a box
29283 `hex-code': display the hexadecimal code of a character in a box
29284 `empty-box': display as an empty box
29285 `thin-space': display as 1-pixel width space
29286 `zero-width': don't display
29287 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29288 display method for graphical terminals and text terminals respectively.
29289 GRAPHICAL and TEXT should each have one of the values listed above.
29291 The char-table has one extra slot to control the display of a character for
29292 which no font is found. This slot only takes effect on graphical terminals.
29293 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29294 `thin-space'. The default is `empty-box'. */);
29295 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29296 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29297 Qempty_box);
29299 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29300 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29301 Vdebug_on_message = Qnil;
29305 /* Initialize this module when Emacs starts. */
29307 void
29308 init_xdisp (void)
29310 current_header_line_height = current_mode_line_height = -1;
29312 CHARPOS (this_line_start_pos) = 0;
29314 if (!noninteractive)
29316 struct window *m = XWINDOW (minibuf_window);
29317 Lisp_Object frame = m->frame;
29318 struct frame *f = XFRAME (frame);
29319 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29320 struct window *r = XWINDOW (root);
29321 int i;
29323 echo_area_window = minibuf_window;
29325 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29326 wset_total_lines
29327 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29328 wset_total_cols (r, make_number (FRAME_COLS (f)));
29329 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29330 wset_total_lines (m, make_number (1));
29331 wset_total_cols (m, make_number (FRAME_COLS (f)));
29333 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29334 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29335 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29337 /* The default ellipsis glyphs `...'. */
29338 for (i = 0; i < 3; ++i)
29339 default_invis_vector[i] = make_number ('.');
29343 /* Allocate the buffer for frame titles.
29344 Also used for `format-mode-line'. */
29345 int size = 100;
29346 mode_line_noprop_buf = xmalloc (size);
29347 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29348 mode_line_noprop_ptr = mode_line_noprop_buf;
29349 mode_line_target = MODE_LINE_DISPLAY;
29352 help_echo_showing_p = 0;
29355 /* Since w32 does not support atimers, it defines its own implementation of
29356 the following three functions in w32fns.c. */
29357 #ifndef WINDOWSNT
29359 /* Platform-independent portion of hourglass implementation. */
29361 /* Cancel a currently active hourglass timer, and start a new one. */
29362 void
29363 start_hourglass (void)
29365 #if defined (HAVE_WINDOW_SYSTEM)
29366 EMACS_TIME delay;
29368 cancel_hourglass ();
29370 if (INTEGERP (Vhourglass_delay)
29371 && XINT (Vhourglass_delay) > 0)
29372 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29373 TYPE_MAXIMUM (time_t)),
29375 else if (FLOATP (Vhourglass_delay)
29376 && XFLOAT_DATA (Vhourglass_delay) > 0)
29377 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29378 else
29379 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29381 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29382 show_hourglass, NULL);
29383 #endif
29387 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29388 shown. */
29389 void
29390 cancel_hourglass (void)
29392 #if defined (HAVE_WINDOW_SYSTEM)
29393 if (hourglass_atimer)
29395 cancel_atimer (hourglass_atimer);
29396 hourglass_atimer = NULL;
29399 if (hourglass_shown_p)
29400 hide_hourglass ();
29401 #endif
29403 #endif /* ! WINDOWSNT */