Add multi-monitor support on X11.
[emacs.git] / src / xdisp.c
blobc7a258732720dadc508c409a4b50331f15cc7ecf
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
103 . try_window
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
116 Desired matrices.
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
161 Frame matrices.
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
182 Bidirectional display.
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
225 Bidirectional display and character compositions
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
252 Moving an iterator in bidirectional text
253 without producing glyphs
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
316 #include "font.h"
318 #ifndef FRAME_X_OUTPUT
319 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
320 #endif
322 #define INFINITY 10000000
324 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
325 Lisp_Object Qwindow_scroll_functions;
326 static Lisp_Object Qwindow_text_change_functions;
327 static Lisp_Object Qredisplay_end_trigger_functions;
328 Lisp_Object Qinhibit_point_motion_hooks;
329 static Lisp_Object QCeval, QCpropertize;
330 Lisp_Object QCfile, QCdata;
331 static Lisp_Object Qfontified;
332 static Lisp_Object Qgrow_only;
333 static Lisp_Object Qinhibit_eval_during_redisplay;
334 static Lisp_Object Qbuffer_position, Qposition, Qobject;
335 static Lisp_Object Qright_to_left, Qleft_to_right;
337 /* Cursor shapes. */
338 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
340 /* Pointer shapes. */
341 static Lisp_Object Qarrow, Qhand;
342 Lisp_Object Qtext;
344 /* Holds the list (error). */
345 static Lisp_Object list_of_error;
347 static Lisp_Object Qfontification_functions;
349 static Lisp_Object Qwrap_prefix;
350 static Lisp_Object Qline_prefix;
351 static Lisp_Object Qredisplay_internal;
353 /* Non-nil means don't actually do any redisplay. */
355 Lisp_Object Qinhibit_redisplay;
357 /* Names of text properties relevant for redisplay. */
359 Lisp_Object Qdisplay;
361 Lisp_Object Qspace, QCalign_to;
362 static Lisp_Object QCrelative_width, QCrelative_height;
363 Lisp_Object Qleft_margin, Qright_margin;
364 static Lisp_Object Qspace_width, Qraise;
365 static Lisp_Object Qslice;
366 Lisp_Object Qcenter;
367 static Lisp_Object Qmargin, Qpointer;
368 static Lisp_Object Qline_height;
370 #ifdef HAVE_WINDOW_SYSTEM
372 /* Test if overflow newline into fringe. Called with iterator IT
373 at or past right window margin, and with IT->current_x set. */
375 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
376 (!NILP (Voverflow_newline_into_fringe) \
377 && FRAME_WINDOW_P ((IT)->f) \
378 && ((IT)->bidi_it.paragraph_dir == R2L \
379 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
380 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
381 && (IT)->current_x == (IT)->last_visible_x \
382 && (IT)->line_wrap != WORD_WRAP)
384 #else /* !HAVE_WINDOW_SYSTEM */
385 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
386 #endif /* HAVE_WINDOW_SYSTEM */
388 /* Test if the display element loaded in IT, or the underlying buffer
389 or string character, is a space or a TAB character. This is used
390 to determine where word wrapping can occur. */
392 #define IT_DISPLAYING_WHITESPACE(it) \
393 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
394 || ((STRINGP (it->string) \
395 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
396 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
397 || (it->s \
398 && (it->s[IT_BYTEPOS (*it)] == ' ' \
399 || it->s[IT_BYTEPOS (*it)] == '\t')) \
400 || (IT_BYTEPOS (*it) < ZV_BYTE \
401 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
402 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
404 /* Name of the face used to highlight trailing whitespace. */
406 static Lisp_Object Qtrailing_whitespace;
408 /* Name and number of the face used to highlight escape glyphs. */
410 static Lisp_Object Qescape_glyph;
412 /* Name and number of the face used to highlight non-breaking spaces. */
414 static Lisp_Object Qnobreak_space;
416 /* The symbol `image' which is the car of the lists used to represent
417 images in Lisp. Also a tool bar style. */
419 Lisp_Object Qimage;
421 /* The image map types. */
422 Lisp_Object QCmap;
423 static Lisp_Object QCpointer;
424 static Lisp_Object Qrect, Qcircle, Qpoly;
426 /* Tool bar styles */
427 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
429 /* Non-zero means print newline to stdout before next mini-buffer
430 message. */
432 int noninteractive_need_newline;
434 /* Non-zero means print newline to message log before next message. */
436 static int message_log_need_newline;
438 /* Three markers that message_dolog uses.
439 It could allocate them itself, but that causes trouble
440 in handling memory-full errors. */
441 static Lisp_Object message_dolog_marker1;
442 static Lisp_Object message_dolog_marker2;
443 static Lisp_Object message_dolog_marker3;
445 /* The buffer position of the first character appearing entirely or
446 partially on the line of the selected window which contains the
447 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
448 redisplay optimization in redisplay_internal. */
450 static struct text_pos this_line_start_pos;
452 /* Number of characters past the end of the line above, including the
453 terminating newline. */
455 static struct text_pos this_line_end_pos;
457 /* The vertical positions and the height of this line. */
459 static int this_line_vpos;
460 static int this_line_y;
461 static int this_line_pixel_height;
463 /* X position at which this display line starts. Usually zero;
464 negative if first character is partially visible. */
466 static int this_line_start_x;
468 /* The smallest character position seen by move_it_* functions as they
469 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
470 hscrolled lines, see display_line. */
472 static struct text_pos this_line_min_pos;
474 /* Buffer that this_line_.* variables are referring to. */
476 static struct buffer *this_line_buffer;
479 /* Values of those variables at last redisplay are stored as
480 properties on `overlay-arrow-position' symbol. However, if
481 Voverlay_arrow_position is a marker, last-arrow-position is its
482 numerical position. */
484 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
486 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
487 properties on a symbol in overlay-arrow-variable-list. */
489 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
491 Lisp_Object Qmenu_bar_update_hook;
493 /* Nonzero if an overlay arrow has been displayed in this window. */
495 static int overlay_arrow_seen;
497 /* Vector containing glyphs for an ellipsis `...'. */
499 static Lisp_Object default_invis_vector[3];
501 /* This is the window where the echo area message was displayed. It
502 is always a mini-buffer window, but it may not be the same window
503 currently active as a mini-buffer. */
505 Lisp_Object echo_area_window;
507 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
508 pushes the current message and the value of
509 message_enable_multibyte on the stack, the function restore_message
510 pops the stack and displays MESSAGE again. */
512 static Lisp_Object Vmessage_stack;
514 /* Nonzero means multibyte characters were enabled when the echo area
515 message was specified. */
517 static int message_enable_multibyte;
519 /* Nonzero if we should redraw the mode lines on the next redisplay. */
521 int update_mode_lines;
523 /* Nonzero if window sizes or contents have changed since last
524 redisplay that finished. */
526 int windows_or_buffers_changed;
528 /* Nonzero means a frame's cursor type has been changed. */
530 int cursor_type_changed;
532 /* Nonzero after display_mode_line if %l was used and it displayed a
533 line number. */
535 static int line_number_displayed;
537 /* The name of the *Messages* buffer, a string. */
539 static Lisp_Object Vmessages_buffer_name;
541 /* Current, index 0, and last displayed echo area message. Either
542 buffers from echo_buffers, or nil to indicate no message. */
544 Lisp_Object echo_area_buffer[2];
546 /* The buffers referenced from echo_area_buffer. */
548 static Lisp_Object echo_buffer[2];
550 /* A vector saved used in with_area_buffer to reduce consing. */
552 static Lisp_Object Vwith_echo_area_save_vector;
554 /* Non-zero means display_echo_area should display the last echo area
555 message again. Set by redisplay_preserve_echo_area. */
557 static int display_last_displayed_message_p;
559 /* Nonzero if echo area is being used by print; zero if being used by
560 message. */
562 static int message_buf_print;
564 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
566 static Lisp_Object Qinhibit_menubar_update;
567 static Lisp_Object Qmessage_truncate_lines;
569 /* Set to 1 in clear_message to make redisplay_internal aware
570 of an emptied echo area. */
572 static int message_cleared_p;
574 /* A scratch glyph row with contents used for generating truncation
575 glyphs. Also used in direct_output_for_insert. */
577 #define MAX_SCRATCH_GLYPHS 100
578 static struct glyph_row scratch_glyph_row;
579 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
581 /* Ascent and height of the last line processed by move_it_to. */
583 static int last_height;
585 /* Non-zero if there's a help-echo in the echo area. */
587 int help_echo_showing_p;
589 /* If >= 0, computed, exact values of mode-line and header-line height
590 to use in the macros CURRENT_MODE_LINE_HEIGHT and
591 CURRENT_HEADER_LINE_HEIGHT. */
593 int current_mode_line_height, current_header_line_height;
595 /* The maximum distance to look ahead for text properties. Values
596 that are too small let us call compute_char_face and similar
597 functions too often which is expensive. Values that are too large
598 let us call compute_char_face and alike too often because we
599 might not be interested in text properties that far away. */
601 #define TEXT_PROP_DISTANCE_LIMIT 100
603 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
604 iterator state and later restore it. This is needed because the
605 bidi iterator on bidi.c keeps a stacked cache of its states, which
606 is really a singleton. When we use scratch iterator objects to
607 move around the buffer, we can cause the bidi cache to be pushed or
608 popped, and therefore we need to restore the cache state when we
609 return to the original iterator. */
610 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
611 do { \
612 if (CACHE) \
613 bidi_unshelve_cache (CACHE, 1); \
614 ITCOPY = ITORIG; \
615 CACHE = bidi_shelve_cache (); \
616 } while (0)
618 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
619 do { \
620 if (pITORIG != pITCOPY) \
621 *(pITORIG) = *(pITCOPY); \
622 bidi_unshelve_cache (CACHE, 0); \
623 CACHE = NULL; \
624 } while (0)
626 #ifdef GLYPH_DEBUG
628 /* Non-zero means print traces of redisplay if compiled with
629 GLYPH_DEBUG defined. */
631 int trace_redisplay_p;
633 #endif /* GLYPH_DEBUG */
635 #ifdef DEBUG_TRACE_MOVE
636 /* Non-zero means trace with TRACE_MOVE to stderr. */
637 int trace_move;
639 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
640 #else
641 #define TRACE_MOVE(x) (void) 0
642 #endif
644 static Lisp_Object Qauto_hscroll_mode;
646 /* Buffer being redisplayed -- for redisplay_window_error. */
648 static struct buffer *displayed_buffer;
650 /* Value returned from text property handlers (see below). */
652 enum prop_handled
654 HANDLED_NORMALLY,
655 HANDLED_RECOMPUTE_PROPS,
656 HANDLED_OVERLAY_STRING_CONSUMED,
657 HANDLED_RETURN
660 /* A description of text properties that redisplay is interested
661 in. */
663 struct props
665 /* The name of the property. */
666 Lisp_Object *name;
668 /* A unique index for the property. */
669 enum prop_idx idx;
671 /* A handler function called to set up iterator IT from the property
672 at IT's current position. Value is used to steer handle_stop. */
673 enum prop_handled (*handler) (struct it *it);
676 static enum prop_handled handle_face_prop (struct it *);
677 static enum prop_handled handle_invisible_prop (struct it *);
678 static enum prop_handled handle_display_prop (struct it *);
679 static enum prop_handled handle_composition_prop (struct it *);
680 static enum prop_handled handle_overlay_change (struct it *);
681 static enum prop_handled handle_fontified_prop (struct it *);
683 /* Properties handled by iterators. */
685 static struct props it_props[] =
687 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
688 /* Handle `face' before `display' because some sub-properties of
689 `display' need to know the face. */
690 {&Qface, FACE_PROP_IDX, handle_face_prop},
691 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
692 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
693 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
694 {NULL, 0, NULL}
697 /* Value is the position described by X. If X is a marker, value is
698 the marker_position of X. Otherwise, value is X. */
700 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
702 /* Enumeration returned by some move_it_.* functions internally. */
704 enum move_it_result
706 /* Not used. Undefined value. */
707 MOVE_UNDEFINED,
709 /* Move ended at the requested buffer position or ZV. */
710 MOVE_POS_MATCH_OR_ZV,
712 /* Move ended at the requested X pixel position. */
713 MOVE_X_REACHED,
715 /* Move within a line ended at the end of a line that must be
716 continued. */
717 MOVE_LINE_CONTINUED,
719 /* Move within a line ended at the end of a line that would
720 be displayed truncated. */
721 MOVE_LINE_TRUNCATED,
723 /* Move within a line ended at a line end. */
724 MOVE_NEWLINE_OR_CR
727 /* This counter is used to clear the face cache every once in a while
728 in redisplay_internal. It is incremented for each redisplay.
729 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
730 cleared. */
732 #define CLEAR_FACE_CACHE_COUNT 500
733 static int clear_face_cache_count;
735 /* Similarly for the image cache. */
737 #ifdef HAVE_WINDOW_SYSTEM
738 #define CLEAR_IMAGE_CACHE_COUNT 101
739 static int clear_image_cache_count;
741 /* Null glyph slice */
742 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
743 #endif
745 /* True while redisplay_internal is in progress. */
747 bool redisplaying_p;
749 static Lisp_Object Qinhibit_free_realized_faces;
750 static Lisp_Object Qmode_line_default_help_echo;
752 /* If a string, XTread_socket generates an event to display that string.
753 (The display is done in read_char.) */
755 Lisp_Object help_echo_string;
756 Lisp_Object help_echo_window;
757 Lisp_Object help_echo_object;
758 ptrdiff_t help_echo_pos;
760 /* Temporary variable for XTread_socket. */
762 Lisp_Object previous_help_echo_string;
764 /* Platform-independent portion of hourglass implementation. */
766 /* Non-zero means an hourglass cursor is currently shown. */
767 int hourglass_shown_p;
769 /* If non-null, an asynchronous timer that, when it expires, displays
770 an hourglass cursor on all frames. */
771 struct atimer *hourglass_atimer;
773 /* Name of the face used to display glyphless characters. */
774 Lisp_Object Qglyphless_char;
776 /* Symbol for the purpose of Vglyphless_char_display. */
777 static Lisp_Object Qglyphless_char_display;
779 /* Method symbols for Vglyphless_char_display. */
780 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
782 /* Default pixel width of `thin-space' display method. */
783 #define THIN_SPACE_WIDTH 1
785 /* Default number of seconds to wait before displaying an hourglass
786 cursor. */
787 #define DEFAULT_HOURGLASS_DELAY 1
790 /* Function prototypes. */
792 static void setup_for_ellipsis (struct it *, int);
793 static void set_iterator_to_next (struct it *, int);
794 static void mark_window_display_accurate_1 (struct window *, int);
795 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
796 static int display_prop_string_p (Lisp_Object, Lisp_Object);
797 static int cursor_row_p (struct glyph_row *);
798 static int redisplay_mode_lines (Lisp_Object, int);
799 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
801 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
803 static void handle_line_prefix (struct it *);
805 static void pint2str (char *, int, ptrdiff_t);
806 static void pint2hrstr (char *, int, ptrdiff_t);
807 static struct text_pos run_window_scroll_functions (Lisp_Object,
808 struct text_pos);
809 static void reconsider_clip_changes (struct window *, struct buffer *);
810 static int text_outside_line_unchanged_p (struct window *,
811 ptrdiff_t, ptrdiff_t);
812 static void store_mode_line_noprop_char (char);
813 static int store_mode_line_noprop (const char *, int, int);
814 static void handle_stop (struct it *);
815 static void handle_stop_backwards (struct it *, ptrdiff_t);
816 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
817 static void ensure_echo_area_buffers (void);
818 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
819 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
820 static int with_echo_area_buffer (struct window *, int,
821 int (*) (ptrdiff_t, Lisp_Object),
822 ptrdiff_t, Lisp_Object);
823 static void clear_garbaged_frames (void);
824 static int current_message_1 (ptrdiff_t, Lisp_Object);
825 static void pop_message (void);
826 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
827 static void set_message (Lisp_Object);
828 static int set_message_1 (ptrdiff_t, Lisp_Object);
829 static int display_echo_area (struct window *);
830 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
831 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
832 static Lisp_Object unwind_redisplay (Lisp_Object);
833 static int string_char_and_length (const unsigned char *, int *);
834 static struct text_pos display_prop_end (struct it *, Lisp_Object,
835 struct text_pos);
836 static int compute_window_start_on_continuation_line (struct window *);
837 static void insert_left_trunc_glyphs (struct it *);
838 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
839 Lisp_Object);
840 static void extend_face_to_end_of_line (struct it *);
841 static int append_space_for_newline (struct it *, int);
842 static int cursor_row_fully_visible_p (struct window *, int, int);
843 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
844 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
845 static int trailing_whitespace_p (ptrdiff_t);
846 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
847 static void push_it (struct it *, struct text_pos *);
848 static void iterate_out_of_display_property (struct it *);
849 static void pop_it (struct it *);
850 static void sync_frame_with_window_matrix_rows (struct window *);
851 static void redisplay_internal (void);
852 static int echo_area_display (int);
853 static void redisplay_windows (Lisp_Object);
854 static void redisplay_window (Lisp_Object, int);
855 static Lisp_Object redisplay_window_error (Lisp_Object);
856 static Lisp_Object redisplay_window_0 (Lisp_Object);
857 static Lisp_Object redisplay_window_1 (Lisp_Object);
858 static int set_cursor_from_row (struct window *, struct glyph_row *,
859 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
860 int, int);
861 static int update_menu_bar (struct frame *, int, int);
862 static int try_window_reusing_current_matrix (struct window *);
863 static int try_window_id (struct window *);
864 static int display_line (struct it *);
865 static int display_mode_lines (struct window *);
866 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
867 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
868 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
869 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
870 static void display_menu_bar (struct window *);
871 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
872 ptrdiff_t *);
873 static int display_string (const char *, Lisp_Object, Lisp_Object,
874 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
875 static void compute_line_metrics (struct it *);
876 static void run_redisplay_end_trigger_hook (struct it *);
877 static int get_overlay_strings (struct it *, ptrdiff_t);
878 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
879 static void next_overlay_string (struct it *);
880 static void reseat (struct it *, struct text_pos, int);
881 static void reseat_1 (struct it *, struct text_pos, int);
882 static void back_to_previous_visible_line_start (struct it *);
883 void reseat_at_previous_visible_line_start (struct it *);
884 static void reseat_at_next_visible_line_start (struct it *, int);
885 static int next_element_from_ellipsis (struct it *);
886 static int next_element_from_display_vector (struct it *);
887 static int next_element_from_string (struct it *);
888 static int next_element_from_c_string (struct it *);
889 static int next_element_from_buffer (struct it *);
890 static int next_element_from_composition (struct it *);
891 static int next_element_from_image (struct it *);
892 static int next_element_from_stretch (struct it *);
893 static void load_overlay_strings (struct it *, ptrdiff_t);
894 static int init_from_display_pos (struct it *, struct window *,
895 struct display_pos *);
896 static void reseat_to_string (struct it *, const char *,
897 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
898 static int get_next_display_element (struct it *);
899 static enum move_it_result
900 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
901 enum move_operation_enum);
902 void move_it_vertically_backward (struct it *, int);
903 static void get_visually_first_element (struct it *);
904 static void init_to_row_start (struct it *, struct window *,
905 struct glyph_row *);
906 static int init_to_row_end (struct it *, struct window *,
907 struct glyph_row *);
908 static void back_to_previous_line_start (struct it *);
909 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
910 static struct text_pos string_pos_nchars_ahead (struct text_pos,
911 Lisp_Object, ptrdiff_t);
912 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
913 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
914 static ptrdiff_t number_of_chars (const char *, bool);
915 static void compute_stop_pos (struct it *);
916 static void compute_string_pos (struct text_pos *, struct text_pos,
917 Lisp_Object);
918 static int face_before_or_after_it_pos (struct it *, int);
919 static ptrdiff_t next_overlay_change (ptrdiff_t);
920 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
921 Lisp_Object, struct text_pos *, ptrdiff_t, int);
922 static int handle_single_display_spec (struct it *, Lisp_Object,
923 Lisp_Object, Lisp_Object,
924 struct text_pos *, ptrdiff_t, int, int);
925 static int underlying_face_id (struct it *);
926 static int in_ellipses_for_invisible_text_p (struct display_pos *,
927 struct window *);
929 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
930 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
932 #ifdef HAVE_WINDOW_SYSTEM
934 static void x_consider_frame_title (Lisp_Object);
935 static int tool_bar_lines_needed (struct frame *, int *);
936 static void update_tool_bar (struct frame *, int);
937 static void build_desired_tool_bar_string (struct frame *f);
938 static int redisplay_tool_bar (struct frame *);
939 static void display_tool_bar_line (struct it *, int);
940 static void notice_overwritten_cursor (struct window *,
941 enum glyph_row_area,
942 int, int, int, int);
943 static void append_stretch_glyph (struct it *, Lisp_Object,
944 int, int, int);
947 #endif /* HAVE_WINDOW_SYSTEM */
949 static void produce_special_glyphs (struct it *, enum display_element_type);
950 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
951 static int coords_in_mouse_face_p (struct window *, int, int);
955 /***********************************************************************
956 Window display dimensions
957 ***********************************************************************/
959 /* Return the bottom boundary y-position for text lines in window W.
960 This is the first y position at which a line cannot start.
961 It is relative to the top of the window.
963 This is the height of W minus the height of a mode line, if any. */
966 window_text_bottom_y (struct window *w)
968 int height = WINDOW_TOTAL_HEIGHT (w);
970 if (WINDOW_WANTS_MODELINE_P (w))
971 height -= CURRENT_MODE_LINE_HEIGHT (w);
972 return height;
975 /* Return the pixel width of display area AREA of window W. AREA < 0
976 means return the total width of W, not including fringes to
977 the left and right of the window. */
980 window_box_width (struct window *w, int area)
982 int cols = w->total_cols;
983 int pixels = 0;
985 if (!w->pseudo_window_p)
987 cols -= WINDOW_SCROLL_BAR_COLS (w);
989 if (area == TEXT_AREA)
991 if (INTEGERP (w->left_margin_cols))
992 cols -= XFASTINT (w->left_margin_cols);
993 if (INTEGERP (w->right_margin_cols))
994 cols -= XFASTINT (w->right_margin_cols);
995 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
997 else if (area == LEFT_MARGIN_AREA)
999 cols = (INTEGERP (w->left_margin_cols)
1000 ? XFASTINT (w->left_margin_cols) : 0);
1001 pixels = 0;
1003 else if (area == RIGHT_MARGIN_AREA)
1005 cols = (INTEGERP (w->right_margin_cols)
1006 ? XFASTINT (w->right_margin_cols) : 0);
1007 pixels = 0;
1011 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1015 /* Return the pixel height of the display area of window W, not
1016 including mode lines of W, if any. */
1019 window_box_height (struct window *w)
1021 struct frame *f = XFRAME (w->frame);
1022 int height = WINDOW_TOTAL_HEIGHT (w);
1024 eassert (height >= 0);
1026 /* Note: the code below that determines the mode-line/header-line
1027 height is essentially the same as that contained in the macro
1028 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1029 the appropriate glyph row has its `mode_line_p' flag set,
1030 and if it doesn't, uses estimate_mode_line_height instead. */
1032 if (WINDOW_WANTS_MODELINE_P (w))
1034 struct glyph_row *ml_row
1035 = (w->current_matrix && w->current_matrix->rows
1036 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1037 : 0);
1038 if (ml_row && ml_row->mode_line_p)
1039 height -= ml_row->height;
1040 else
1041 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1044 if (WINDOW_WANTS_HEADER_LINE_P (w))
1046 struct glyph_row *hl_row
1047 = (w->current_matrix && w->current_matrix->rows
1048 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1049 : 0);
1050 if (hl_row && hl_row->mode_line_p)
1051 height -= hl_row->height;
1052 else
1053 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1056 /* With a very small font and a mode-line that's taller than
1057 default, we might end up with a negative height. */
1058 return max (0, height);
1061 /* Return the window-relative coordinate of the left edge of display
1062 area AREA of window W. AREA < 0 means return the left edge of the
1063 whole window, to the right of the left fringe of W. */
1066 window_box_left_offset (struct window *w, int area)
1068 int x;
1070 if (w->pseudo_window_p)
1071 return 0;
1073 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1075 if (area == TEXT_AREA)
1076 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1077 + window_box_width (w, LEFT_MARGIN_AREA));
1078 else if (area == RIGHT_MARGIN_AREA)
1079 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1080 + window_box_width (w, LEFT_MARGIN_AREA)
1081 + window_box_width (w, TEXT_AREA)
1082 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1084 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1085 else if (area == LEFT_MARGIN_AREA
1086 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1087 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1089 return x;
1093 /* Return the window-relative coordinate of the right edge of display
1094 area AREA of window W. AREA < 0 means return the right edge of the
1095 whole window, to the left of the right fringe of W. */
1098 window_box_right_offset (struct window *w, int area)
1100 return window_box_left_offset (w, area) + window_box_width (w, area);
1103 /* Return the frame-relative coordinate of the left edge of display
1104 area AREA of window W. AREA < 0 means return the left edge of the
1105 whole window, to the right of the left fringe of W. */
1108 window_box_left (struct window *w, int area)
1110 struct frame *f = XFRAME (w->frame);
1111 int x;
1113 if (w->pseudo_window_p)
1114 return FRAME_INTERNAL_BORDER_WIDTH (f);
1116 x = (WINDOW_LEFT_EDGE_X (w)
1117 + window_box_left_offset (w, area));
1119 return x;
1123 /* Return the frame-relative coordinate of the right edge of display
1124 area AREA of window W. AREA < 0 means return the right edge of the
1125 whole window, to the left of the right fringe of W. */
1128 window_box_right (struct window *w, int area)
1130 return window_box_left (w, area) + window_box_width (w, area);
1133 /* Get the bounding box of the display area AREA of window W, without
1134 mode lines, in frame-relative coordinates. AREA < 0 means the
1135 whole window, not including the left and right fringes of
1136 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1137 coordinates of the upper-left corner of the box. Return in
1138 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1140 void
1141 window_box (struct window *w, int area, int *box_x, int *box_y,
1142 int *box_width, int *box_height)
1144 if (box_width)
1145 *box_width = window_box_width (w, area);
1146 if (box_height)
1147 *box_height = window_box_height (w);
1148 if (box_x)
1149 *box_x = window_box_left (w, area);
1150 if (box_y)
1152 *box_y = WINDOW_TOP_EDGE_Y (w);
1153 if (WINDOW_WANTS_HEADER_LINE_P (w))
1154 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1159 /* Get the bounding box of the display area AREA of window W, without
1160 mode lines. AREA < 0 means the whole window, not including the
1161 left and right fringe of the window. Return in *TOP_LEFT_X
1162 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1163 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1164 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1165 box. */
1167 static void
1168 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1169 int *bottom_right_x, int *bottom_right_y)
1171 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1172 bottom_right_y);
1173 *bottom_right_x += *top_left_x;
1174 *bottom_right_y += *top_left_y;
1179 /***********************************************************************
1180 Utilities
1181 ***********************************************************************/
1183 /* Return the bottom y-position of the line the iterator IT is in.
1184 This can modify IT's settings. */
1187 line_bottom_y (struct it *it)
1189 int line_height = it->max_ascent + it->max_descent;
1190 int line_top_y = it->current_y;
1192 if (line_height == 0)
1194 if (last_height)
1195 line_height = last_height;
1196 else if (IT_CHARPOS (*it) < ZV)
1198 move_it_by_lines (it, 1);
1199 line_height = (it->max_ascent || it->max_descent
1200 ? it->max_ascent + it->max_descent
1201 : last_height);
1203 else
1205 struct glyph_row *row = it->glyph_row;
1207 /* Use the default character height. */
1208 it->glyph_row = NULL;
1209 it->what = IT_CHARACTER;
1210 it->c = ' ';
1211 it->len = 1;
1212 PRODUCE_GLYPHS (it);
1213 line_height = it->ascent + it->descent;
1214 it->glyph_row = row;
1218 return line_top_y + line_height;
1221 /* Subroutine of pos_visible_p below. Extracts a display string, if
1222 any, from the display spec given as its argument. */
1223 static Lisp_Object
1224 string_from_display_spec (Lisp_Object spec)
1226 if (CONSP (spec))
1228 while (CONSP (spec))
1230 if (STRINGP (XCAR (spec)))
1231 return XCAR (spec);
1232 spec = XCDR (spec);
1235 else if (VECTORP (spec))
1237 ptrdiff_t i;
1239 for (i = 0; i < ASIZE (spec); i++)
1241 if (STRINGP (AREF (spec, i)))
1242 return AREF (spec, i);
1244 return Qnil;
1247 return spec;
1251 /* Limit insanely large values of W->hscroll on frame F to the largest
1252 value that will still prevent first_visible_x and last_visible_x of
1253 'struct it' from overflowing an int. */
1254 static int
1255 window_hscroll_limited (struct window *w, struct frame *f)
1257 ptrdiff_t window_hscroll = w->hscroll;
1258 int window_text_width = window_box_width (w, TEXT_AREA);
1259 int colwidth = FRAME_COLUMN_WIDTH (f);
1261 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1262 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1264 return window_hscroll;
1267 /* Return 1 if position CHARPOS is visible in window W.
1268 CHARPOS < 0 means return info about WINDOW_END position.
1269 If visible, set *X and *Y to pixel coordinates of top left corner.
1270 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1271 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1274 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1275 int *rtop, int *rbot, int *rowh, int *vpos)
1277 struct it it;
1278 void *itdata = bidi_shelve_cache ();
1279 struct text_pos top;
1280 int visible_p = 0;
1281 struct buffer *old_buffer = NULL;
1283 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1284 return visible_p;
1286 if (XBUFFER (w->contents) != current_buffer)
1288 old_buffer = current_buffer;
1289 set_buffer_internal_1 (XBUFFER (w->contents));
1292 SET_TEXT_POS_FROM_MARKER (top, w->start);
1293 /* Scrolling a minibuffer window via scroll bar when the echo area
1294 shows long text sometimes resets the minibuffer contents behind
1295 our backs. */
1296 if (CHARPOS (top) > ZV)
1297 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1299 /* Compute exact mode line heights. */
1300 if (WINDOW_WANTS_MODELINE_P (w))
1301 current_mode_line_height
1302 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1303 BVAR (current_buffer, mode_line_format));
1305 if (WINDOW_WANTS_HEADER_LINE_P (w))
1306 current_header_line_height
1307 = display_mode_line (w, HEADER_LINE_FACE_ID,
1308 BVAR (current_buffer, header_line_format));
1310 start_display (&it, w, top);
1311 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1312 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1314 if (charpos >= 0
1315 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1316 && IT_CHARPOS (it) >= charpos)
1317 /* When scanning backwards under bidi iteration, move_it_to
1318 stops at or _before_ CHARPOS, because it stops at or to
1319 the _right_ of the character at CHARPOS. */
1320 || (it.bidi_p && it.bidi_it.scan_dir == -1
1321 && IT_CHARPOS (it) <= charpos)))
1323 /* We have reached CHARPOS, or passed it. How the call to
1324 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1325 or covered by a display property, move_it_to stops at the end
1326 of the invisible text, to the right of CHARPOS. (ii) If
1327 CHARPOS is in a display vector, move_it_to stops on its last
1328 glyph. */
1329 int top_x = it.current_x;
1330 int top_y = it.current_y;
1331 /* Calling line_bottom_y may change it.method, it.position, etc. */
1332 enum it_method it_method = it.method;
1333 int bottom_y = (last_height = 0, line_bottom_y (&it));
1334 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1336 if (top_y < window_top_y)
1337 visible_p = bottom_y > window_top_y;
1338 else if (top_y < it.last_visible_y)
1339 visible_p = 1;
1340 if (bottom_y >= it.last_visible_y
1341 && it.bidi_p && it.bidi_it.scan_dir == -1
1342 && IT_CHARPOS (it) < charpos)
1344 /* When the last line of the window is scanned backwards
1345 under bidi iteration, we could be duped into thinking
1346 that we have passed CHARPOS, when in fact move_it_to
1347 simply stopped short of CHARPOS because it reached
1348 last_visible_y. To see if that's what happened, we call
1349 move_it_to again with a slightly larger vertical limit,
1350 and see if it actually moved vertically; if it did, we
1351 didn't really reach CHARPOS, which is beyond window end. */
1352 struct it save_it = it;
1353 /* Why 10? because we don't know how many canonical lines
1354 will the height of the next line(s) be. So we guess. */
1355 int ten_more_lines =
1356 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1358 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1359 MOVE_TO_POS | MOVE_TO_Y);
1360 if (it.current_y > top_y)
1361 visible_p = 0;
1363 it = save_it;
1365 if (visible_p)
1367 if (it_method == GET_FROM_DISPLAY_VECTOR)
1369 /* We stopped on the last glyph of a display vector.
1370 Try and recompute. Hack alert! */
1371 if (charpos < 2 || top.charpos >= charpos)
1372 top_x = it.glyph_row->x;
1373 else
1375 struct it it2;
1376 start_display (&it2, w, top);
1377 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1378 get_next_display_element (&it2);
1379 PRODUCE_GLYPHS (&it2);
1380 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1381 || it2.current_x > it2.last_visible_x)
1382 top_x = it.glyph_row->x;
1383 else
1385 top_x = it2.current_x;
1386 top_y = it2.current_y;
1390 else if (IT_CHARPOS (it) != charpos)
1392 Lisp_Object cpos = make_number (charpos);
1393 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1394 Lisp_Object string = string_from_display_spec (spec);
1395 struct text_pos tpos;
1396 int replacing_spec_p;
1397 bool newline_in_string
1398 = (STRINGP (string)
1399 && memchr (SDATA (string), '\n', SBYTES (string)));
1401 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1402 replacing_spec_p
1403 = (!NILP (spec)
1404 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1405 charpos, FRAME_WINDOW_P (it.f)));
1406 /* The tricky code below is needed because there's a
1407 discrepancy between move_it_to and how we set cursor
1408 when PT is at the beginning of a portion of text
1409 covered by a display property or an overlay with a
1410 display property, or the display line ends in a
1411 newline from a display string. move_it_to will stop
1412 _after_ such display strings, whereas
1413 set_cursor_from_row conspires with cursor_row_p to
1414 place the cursor on the first glyph produced from the
1415 display string. */
1417 /* We have overshoot PT because it is covered by a
1418 display property that replaces the text it covers.
1419 If the string includes embedded newlines, we are also
1420 in the wrong display line. Backtrack to the correct
1421 line, where the display property begins. */
1422 if (replacing_spec_p)
1424 Lisp_Object startpos, endpos;
1425 EMACS_INT start, end;
1426 struct it it3;
1427 int it3_moved;
1429 /* Find the first and the last buffer positions
1430 covered by the display string. */
1431 endpos =
1432 Fnext_single_char_property_change (cpos, Qdisplay,
1433 Qnil, Qnil);
1434 startpos =
1435 Fprevious_single_char_property_change (endpos, Qdisplay,
1436 Qnil, Qnil);
1437 start = XFASTINT (startpos);
1438 end = XFASTINT (endpos);
1439 /* Move to the last buffer position before the
1440 display property. */
1441 start_display (&it3, w, top);
1442 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1443 /* Move forward one more line if the position before
1444 the display string is a newline or if it is the
1445 rightmost character on a line that is
1446 continued or word-wrapped. */
1447 if (it3.method == GET_FROM_BUFFER
1448 && (it3.c == '\n'
1449 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1450 move_it_by_lines (&it3, 1);
1451 else if (move_it_in_display_line_to (&it3, -1,
1452 it3.current_x
1453 + it3.pixel_width,
1454 MOVE_TO_X)
1455 == MOVE_LINE_CONTINUED)
1457 move_it_by_lines (&it3, 1);
1458 /* When we are under word-wrap, the #$@%!
1459 move_it_by_lines moves 2 lines, so we need to
1460 fix that up. */
1461 if (it3.line_wrap == WORD_WRAP)
1462 move_it_by_lines (&it3, -1);
1465 /* Record the vertical coordinate of the display
1466 line where we wound up. */
1467 top_y = it3.current_y;
1468 if (it3.bidi_p)
1470 /* When characters are reordered for display,
1471 the character displayed to the left of the
1472 display string could be _after_ the display
1473 property in the logical order. Use the
1474 smallest vertical position of these two. */
1475 start_display (&it3, w, top);
1476 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1477 if (it3.current_y < top_y)
1478 top_y = it3.current_y;
1480 /* Move from the top of the window to the beginning
1481 of the display line where the display string
1482 begins. */
1483 start_display (&it3, w, top);
1484 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1485 /* If it3_moved stays zero after the 'while' loop
1486 below, that means we already were at a newline
1487 before the loop (e.g., the display string begins
1488 with a newline), so we don't need to (and cannot)
1489 inspect the glyphs of it3.glyph_row, because
1490 PRODUCE_GLYPHS will not produce anything for a
1491 newline, and thus it3.glyph_row stays at its
1492 stale content it got at top of the window. */
1493 it3_moved = 0;
1494 /* Finally, advance the iterator until we hit the
1495 first display element whose character position is
1496 CHARPOS, or until the first newline from the
1497 display string, which signals the end of the
1498 display line. */
1499 while (get_next_display_element (&it3))
1501 PRODUCE_GLYPHS (&it3);
1502 if (IT_CHARPOS (it3) == charpos
1503 || ITERATOR_AT_END_OF_LINE_P (&it3))
1504 break;
1505 it3_moved = 1;
1506 set_iterator_to_next (&it3, 0);
1508 top_x = it3.current_x - it3.pixel_width;
1509 /* Normally, we would exit the above loop because we
1510 found the display element whose character
1511 position is CHARPOS. For the contingency that we
1512 didn't, and stopped at the first newline from the
1513 display string, move back over the glyphs
1514 produced from the string, until we find the
1515 rightmost glyph not from the string. */
1516 if (it3_moved
1517 && newline_in_string
1518 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1520 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1521 + it3.glyph_row->used[TEXT_AREA];
1523 while (EQ ((g - 1)->object, string))
1525 --g;
1526 top_x -= g->pixel_width;
1528 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1529 + it3.glyph_row->used[TEXT_AREA]);
1534 *x = top_x;
1535 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1536 *rtop = max (0, window_top_y - top_y);
1537 *rbot = max (0, bottom_y - it.last_visible_y);
1538 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1539 - max (top_y, window_top_y)));
1540 *vpos = it.vpos;
1543 else
1545 /* We were asked to provide info about WINDOW_END. */
1546 struct it it2;
1547 void *it2data = NULL;
1549 SAVE_IT (it2, it, it2data);
1550 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1551 move_it_by_lines (&it, 1);
1552 if (charpos < IT_CHARPOS (it)
1553 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1555 visible_p = 1;
1556 RESTORE_IT (&it2, &it2, it2data);
1557 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1558 *x = it2.current_x;
1559 *y = it2.current_y + it2.max_ascent - it2.ascent;
1560 *rtop = max (0, -it2.current_y);
1561 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1562 - it.last_visible_y));
1563 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1564 it.last_visible_y)
1565 - max (it2.current_y,
1566 WINDOW_HEADER_LINE_HEIGHT (w))));
1567 *vpos = it2.vpos;
1569 else
1570 bidi_unshelve_cache (it2data, 1);
1572 bidi_unshelve_cache (itdata, 0);
1574 if (old_buffer)
1575 set_buffer_internal_1 (old_buffer);
1577 current_header_line_height = current_mode_line_height = -1;
1579 if (visible_p && w->hscroll > 0)
1580 *x -=
1581 window_hscroll_limited (w, WINDOW_XFRAME (w))
1582 * WINDOW_FRAME_COLUMN_WIDTH (w);
1584 #if 0
1585 /* Debugging code. */
1586 if (visible_p)
1587 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1588 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1589 else
1590 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1591 #endif
1593 return visible_p;
1597 /* Return the next character from STR. Return in *LEN the length of
1598 the character. This is like STRING_CHAR_AND_LENGTH but never
1599 returns an invalid character. If we find one, we return a `?', but
1600 with the length of the invalid character. */
1602 static int
1603 string_char_and_length (const unsigned char *str, int *len)
1605 int c;
1607 c = STRING_CHAR_AND_LENGTH (str, *len);
1608 if (!CHAR_VALID_P (c))
1609 /* We may not change the length here because other places in Emacs
1610 don't use this function, i.e. they silently accept invalid
1611 characters. */
1612 c = '?';
1614 return c;
1619 /* Given a position POS containing a valid character and byte position
1620 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1622 static struct text_pos
1623 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1625 eassert (STRINGP (string) && nchars >= 0);
1627 if (STRING_MULTIBYTE (string))
1629 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1630 int len;
1632 while (nchars--)
1634 string_char_and_length (p, &len);
1635 p += len;
1636 CHARPOS (pos) += 1;
1637 BYTEPOS (pos) += len;
1640 else
1641 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1643 return pos;
1647 /* Value is the text position, i.e. character and byte position,
1648 for character position CHARPOS in STRING. */
1650 static struct text_pos
1651 string_pos (ptrdiff_t charpos, Lisp_Object string)
1653 struct text_pos pos;
1654 eassert (STRINGP (string));
1655 eassert (charpos >= 0);
1656 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1657 return pos;
1661 /* Value is a text position, i.e. character and byte position, for
1662 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1663 means recognize multibyte characters. */
1665 static struct text_pos
1666 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1668 struct text_pos pos;
1670 eassert (s != NULL);
1671 eassert (charpos >= 0);
1673 if (multibyte_p)
1675 int len;
1677 SET_TEXT_POS (pos, 0, 0);
1678 while (charpos--)
1680 string_char_and_length ((const unsigned char *) s, &len);
1681 s += len;
1682 CHARPOS (pos) += 1;
1683 BYTEPOS (pos) += len;
1686 else
1687 SET_TEXT_POS (pos, charpos, charpos);
1689 return pos;
1693 /* Value is the number of characters in C string S. MULTIBYTE_P
1694 non-zero means recognize multibyte characters. */
1696 static ptrdiff_t
1697 number_of_chars (const char *s, bool multibyte_p)
1699 ptrdiff_t nchars;
1701 if (multibyte_p)
1703 ptrdiff_t rest = strlen (s);
1704 int len;
1705 const unsigned char *p = (const unsigned char *) s;
1707 for (nchars = 0; rest > 0; ++nchars)
1709 string_char_and_length (p, &len);
1710 rest -= len, p += len;
1713 else
1714 nchars = strlen (s);
1716 return nchars;
1720 /* Compute byte position NEWPOS->bytepos corresponding to
1721 NEWPOS->charpos. POS is a known position in string STRING.
1722 NEWPOS->charpos must be >= POS.charpos. */
1724 static void
1725 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1727 eassert (STRINGP (string));
1728 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1730 if (STRING_MULTIBYTE (string))
1731 *newpos = string_pos_nchars_ahead (pos, string,
1732 CHARPOS (*newpos) - CHARPOS (pos));
1733 else
1734 BYTEPOS (*newpos) = CHARPOS (*newpos);
1737 /* EXPORT:
1738 Return an estimation of the pixel height of mode or header lines on
1739 frame F. FACE_ID specifies what line's height to estimate. */
1742 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1744 #ifdef HAVE_WINDOW_SYSTEM
1745 if (FRAME_WINDOW_P (f))
1747 int height = FONT_HEIGHT (FRAME_FONT (f));
1749 /* This function is called so early when Emacs starts that the face
1750 cache and mode line face are not yet initialized. */
1751 if (FRAME_FACE_CACHE (f))
1753 struct face *face = FACE_FROM_ID (f, face_id);
1754 if (face)
1756 if (face->font)
1757 height = FONT_HEIGHT (face->font);
1758 if (face->box_line_width > 0)
1759 height += 2 * face->box_line_width;
1763 return height;
1765 #endif
1767 return 1;
1770 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1771 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1772 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1773 not force the value into range. */
1775 void
1776 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1777 int *x, int *y, NativeRectangle *bounds, int noclip)
1780 #ifdef HAVE_WINDOW_SYSTEM
1781 if (FRAME_WINDOW_P (f))
1783 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1784 even for negative values. */
1785 if (pix_x < 0)
1786 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1787 if (pix_y < 0)
1788 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1790 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1791 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1793 if (bounds)
1794 STORE_NATIVE_RECT (*bounds,
1795 FRAME_COL_TO_PIXEL_X (f, pix_x),
1796 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1797 FRAME_COLUMN_WIDTH (f) - 1,
1798 FRAME_LINE_HEIGHT (f) - 1);
1800 if (!noclip)
1802 if (pix_x < 0)
1803 pix_x = 0;
1804 else if (pix_x > FRAME_TOTAL_COLS (f))
1805 pix_x = FRAME_TOTAL_COLS (f);
1807 if (pix_y < 0)
1808 pix_y = 0;
1809 else if (pix_y > FRAME_LINES (f))
1810 pix_y = FRAME_LINES (f);
1813 #endif
1815 *x = pix_x;
1816 *y = pix_y;
1820 /* Find the glyph under window-relative coordinates X/Y in window W.
1821 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1822 strings. Return in *HPOS and *VPOS the row and column number of
1823 the glyph found. Return in *AREA the glyph area containing X.
1824 Value is a pointer to the glyph found or null if X/Y is not on
1825 text, or we can't tell because W's current matrix is not up to
1826 date. */
1828 static
1829 struct glyph *
1830 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1831 int *dx, int *dy, int *area)
1833 struct glyph *glyph, *end;
1834 struct glyph_row *row = NULL;
1835 int x0, i;
1837 /* Find row containing Y. Give up if some row is not enabled. */
1838 for (i = 0; i < w->current_matrix->nrows; ++i)
1840 row = MATRIX_ROW (w->current_matrix, i);
1841 if (!row->enabled_p)
1842 return NULL;
1843 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1844 break;
1847 *vpos = i;
1848 *hpos = 0;
1850 /* Give up if Y is not in the window. */
1851 if (i == w->current_matrix->nrows)
1852 return NULL;
1854 /* Get the glyph area containing X. */
1855 if (w->pseudo_window_p)
1857 *area = TEXT_AREA;
1858 x0 = 0;
1860 else
1862 if (x < window_box_left_offset (w, TEXT_AREA))
1864 *area = LEFT_MARGIN_AREA;
1865 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1867 else if (x < window_box_right_offset (w, TEXT_AREA))
1869 *area = TEXT_AREA;
1870 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1872 else
1874 *area = RIGHT_MARGIN_AREA;
1875 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1879 /* Find glyph containing X. */
1880 glyph = row->glyphs[*area];
1881 end = glyph + row->used[*area];
1882 x -= x0;
1883 while (glyph < end && x >= glyph->pixel_width)
1885 x -= glyph->pixel_width;
1886 ++glyph;
1889 if (glyph == end)
1890 return NULL;
1892 if (dx)
1894 *dx = x;
1895 *dy = y - (row->y + row->ascent - glyph->ascent);
1898 *hpos = glyph - row->glyphs[*area];
1899 return glyph;
1902 /* Convert frame-relative x/y to coordinates relative to window W.
1903 Takes pseudo-windows into account. */
1905 static void
1906 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1908 if (w->pseudo_window_p)
1910 /* A pseudo-window is always full-width, and starts at the
1911 left edge of the frame, plus a frame border. */
1912 struct frame *f = XFRAME (w->frame);
1913 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1914 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1916 else
1918 *x -= WINDOW_LEFT_EDGE_X (w);
1919 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1923 #ifdef HAVE_WINDOW_SYSTEM
1925 /* EXPORT:
1926 Return in RECTS[] at most N clipping rectangles for glyph string S.
1927 Return the number of stored rectangles. */
1930 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1932 XRectangle r;
1934 if (n <= 0)
1935 return 0;
1937 if (s->row->full_width_p)
1939 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1940 r.x = WINDOW_LEFT_EDGE_X (s->w);
1941 r.width = WINDOW_TOTAL_WIDTH (s->w);
1943 /* Unless displaying a mode or menu bar line, which are always
1944 fully visible, clip to the visible part of the row. */
1945 if (s->w->pseudo_window_p)
1946 r.height = s->row->visible_height;
1947 else
1948 r.height = s->height;
1950 else
1952 /* This is a text line that may be partially visible. */
1953 r.x = window_box_left (s->w, s->area);
1954 r.width = window_box_width (s->w, s->area);
1955 r.height = s->row->visible_height;
1958 if (s->clip_head)
1959 if (r.x < s->clip_head->x)
1961 if (r.width >= s->clip_head->x - r.x)
1962 r.width -= s->clip_head->x - r.x;
1963 else
1964 r.width = 0;
1965 r.x = s->clip_head->x;
1967 if (s->clip_tail)
1968 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1970 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1971 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1972 else
1973 r.width = 0;
1976 /* If S draws overlapping rows, it's sufficient to use the top and
1977 bottom of the window for clipping because this glyph string
1978 intentionally draws over other lines. */
1979 if (s->for_overlaps)
1981 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1982 r.height = window_text_bottom_y (s->w) - r.y;
1984 /* Alas, the above simple strategy does not work for the
1985 environments with anti-aliased text: if the same text is
1986 drawn onto the same place multiple times, it gets thicker.
1987 If the overlap we are processing is for the erased cursor, we
1988 take the intersection with the rectangle of the cursor. */
1989 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1991 XRectangle rc, r_save = r;
1993 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1994 rc.y = s->w->phys_cursor.y;
1995 rc.width = s->w->phys_cursor_width;
1996 rc.height = s->w->phys_cursor_height;
1998 x_intersect_rectangles (&r_save, &rc, &r);
2001 else
2003 /* Don't use S->y for clipping because it doesn't take partially
2004 visible lines into account. For example, it can be negative for
2005 partially visible lines at the top of a window. */
2006 if (!s->row->full_width_p
2007 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2008 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2009 else
2010 r.y = max (0, s->row->y);
2013 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2015 /* If drawing the cursor, don't let glyph draw outside its
2016 advertised boundaries. Cleartype does this under some circumstances. */
2017 if (s->hl == DRAW_CURSOR)
2019 struct glyph *glyph = s->first_glyph;
2020 int height, max_y;
2022 if (s->x > r.x)
2024 r.width -= s->x - r.x;
2025 r.x = s->x;
2027 r.width = min (r.width, glyph->pixel_width);
2029 /* If r.y is below window bottom, ensure that we still see a cursor. */
2030 height = min (glyph->ascent + glyph->descent,
2031 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2032 max_y = window_text_bottom_y (s->w) - height;
2033 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2034 if (s->ybase - glyph->ascent > max_y)
2036 r.y = max_y;
2037 r.height = height;
2039 else
2041 /* Don't draw cursor glyph taller than our actual glyph. */
2042 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2043 if (height < r.height)
2045 max_y = r.y + r.height;
2046 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2047 r.height = min (max_y - r.y, height);
2052 if (s->row->clip)
2054 XRectangle r_save = r;
2056 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2057 r.width = 0;
2060 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2061 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2063 #ifdef CONVERT_FROM_XRECT
2064 CONVERT_FROM_XRECT (r, *rects);
2065 #else
2066 *rects = r;
2067 #endif
2068 return 1;
2070 else
2072 /* If we are processing overlapping and allowed to return
2073 multiple clipping rectangles, we exclude the row of the glyph
2074 string from the clipping rectangle. This is to avoid drawing
2075 the same text on the environment with anti-aliasing. */
2076 #ifdef CONVERT_FROM_XRECT
2077 XRectangle rs[2];
2078 #else
2079 XRectangle *rs = rects;
2080 #endif
2081 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2083 if (s->for_overlaps & OVERLAPS_PRED)
2085 rs[i] = r;
2086 if (r.y + r.height > row_y)
2088 if (r.y < row_y)
2089 rs[i].height = row_y - r.y;
2090 else
2091 rs[i].height = 0;
2093 i++;
2095 if (s->for_overlaps & OVERLAPS_SUCC)
2097 rs[i] = r;
2098 if (r.y < row_y + s->row->visible_height)
2100 if (r.y + r.height > row_y + s->row->visible_height)
2102 rs[i].y = row_y + s->row->visible_height;
2103 rs[i].height = r.y + r.height - rs[i].y;
2105 else
2106 rs[i].height = 0;
2108 i++;
2111 n = i;
2112 #ifdef CONVERT_FROM_XRECT
2113 for (i = 0; i < n; i++)
2114 CONVERT_FROM_XRECT (rs[i], rects[i]);
2115 #endif
2116 return n;
2120 /* EXPORT:
2121 Return in *NR the clipping rectangle for glyph string S. */
2123 void
2124 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2126 get_glyph_string_clip_rects (s, nr, 1);
2130 /* EXPORT:
2131 Return the position and height of the phys cursor in window W.
2132 Set w->phys_cursor_width to width of phys cursor.
2135 void
2136 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2137 struct glyph *glyph, int *xp, int *yp, int *heightp)
2139 struct frame *f = XFRAME (WINDOW_FRAME (w));
2140 int x, y, wd, h, h0, y0;
2142 /* Compute the width of the rectangle to draw. If on a stretch
2143 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2144 rectangle as wide as the glyph, but use a canonical character
2145 width instead. */
2146 wd = glyph->pixel_width - 1;
2147 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2148 wd++; /* Why? */
2149 #endif
2151 x = w->phys_cursor.x;
2152 if (x < 0)
2154 wd += x;
2155 x = 0;
2158 if (glyph->type == STRETCH_GLYPH
2159 && !x_stretch_cursor_p)
2160 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2161 w->phys_cursor_width = wd;
2163 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2165 /* If y is below window bottom, ensure that we still see a cursor. */
2166 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2168 h = max (h0, glyph->ascent + glyph->descent);
2169 h0 = min (h0, glyph->ascent + glyph->descent);
2171 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2172 if (y < y0)
2174 h = max (h - (y0 - y) + 1, h0);
2175 y = y0 - 1;
2177 else
2179 y0 = window_text_bottom_y (w) - h0;
2180 if (y > y0)
2182 h += y - y0;
2183 y = y0;
2187 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2188 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2189 *heightp = h;
2193 * Remember which glyph the mouse is over.
2196 void
2197 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2199 Lisp_Object window;
2200 struct window *w;
2201 struct glyph_row *r, *gr, *end_row;
2202 enum window_part part;
2203 enum glyph_row_area area;
2204 int x, y, width, height;
2206 /* Try to determine frame pixel position and size of the glyph under
2207 frame pixel coordinates X/Y on frame F. */
2209 if (!f->glyphs_initialized_p
2210 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2211 NILP (window)))
2213 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2214 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2215 goto virtual_glyph;
2218 w = XWINDOW (window);
2219 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2220 height = WINDOW_FRAME_LINE_HEIGHT (w);
2222 x = window_relative_x_coord (w, part, gx);
2223 y = gy - WINDOW_TOP_EDGE_Y (w);
2225 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2226 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2228 if (w->pseudo_window_p)
2230 area = TEXT_AREA;
2231 part = ON_MODE_LINE; /* Don't adjust margin. */
2232 goto text_glyph;
2235 switch (part)
2237 case ON_LEFT_MARGIN:
2238 area = LEFT_MARGIN_AREA;
2239 goto text_glyph;
2241 case ON_RIGHT_MARGIN:
2242 area = RIGHT_MARGIN_AREA;
2243 goto text_glyph;
2245 case ON_HEADER_LINE:
2246 case ON_MODE_LINE:
2247 gr = (part == ON_HEADER_LINE
2248 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2249 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2250 gy = gr->y;
2251 area = TEXT_AREA;
2252 goto text_glyph_row_found;
2254 case ON_TEXT:
2255 area = TEXT_AREA;
2257 text_glyph:
2258 gr = 0; gy = 0;
2259 for (; r <= end_row && r->enabled_p; ++r)
2260 if (r->y + r->height > y)
2262 gr = r; gy = r->y;
2263 break;
2266 text_glyph_row_found:
2267 if (gr && gy <= y)
2269 struct glyph *g = gr->glyphs[area];
2270 struct glyph *end = g + gr->used[area];
2272 height = gr->height;
2273 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2274 if (gx + g->pixel_width > x)
2275 break;
2277 if (g < end)
2279 if (g->type == IMAGE_GLYPH)
2281 /* Don't remember when mouse is over image, as
2282 image may have hot-spots. */
2283 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2284 return;
2286 width = g->pixel_width;
2288 else
2290 /* Use nominal char spacing at end of line. */
2291 x -= gx;
2292 gx += (x / width) * width;
2295 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2296 gx += window_box_left_offset (w, area);
2298 else
2300 /* Use nominal line height at end of window. */
2301 gx = (x / width) * width;
2302 y -= gy;
2303 gy += (y / height) * height;
2305 break;
2307 case ON_LEFT_FRINGE:
2308 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2309 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2310 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2311 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2312 goto row_glyph;
2314 case ON_RIGHT_FRINGE:
2315 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2316 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2317 : window_box_right_offset (w, TEXT_AREA));
2318 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2319 goto row_glyph;
2321 case ON_SCROLL_BAR:
2322 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2324 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2325 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2326 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2327 : 0)));
2328 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2330 row_glyph:
2331 gr = 0, gy = 0;
2332 for (; r <= end_row && r->enabled_p; ++r)
2333 if (r->y + r->height > y)
2335 gr = r; gy = r->y;
2336 break;
2339 if (gr && gy <= y)
2340 height = gr->height;
2341 else
2343 /* Use nominal line height at end of window. */
2344 y -= gy;
2345 gy += (y / height) * height;
2347 break;
2349 default:
2351 virtual_glyph:
2352 /* If there is no glyph under the mouse, then we divide the screen
2353 into a grid of the smallest glyph in the frame, and use that
2354 as our "glyph". */
2356 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2357 round down even for negative values. */
2358 if (gx < 0)
2359 gx -= width - 1;
2360 if (gy < 0)
2361 gy -= height - 1;
2363 gx = (gx / width) * width;
2364 gy = (gy / height) * height;
2366 goto store_rect;
2369 gx += WINDOW_LEFT_EDGE_X (w);
2370 gy += WINDOW_TOP_EDGE_Y (w);
2372 store_rect:
2373 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2375 /* Visible feedback for debugging. */
2376 #if 0
2377 #if HAVE_X_WINDOWS
2378 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2379 f->output_data.x->normal_gc,
2380 gx, gy, width, height);
2381 #endif
2382 #endif
2386 #endif /* HAVE_WINDOW_SYSTEM */
2389 /***********************************************************************
2390 Lisp form evaluation
2391 ***********************************************************************/
2393 /* Error handler for safe_eval and safe_call. */
2395 static Lisp_Object
2396 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2398 add_to_log ("Error during redisplay: %S signaled %S",
2399 Flist (nargs, args), arg);
2400 return Qnil;
2403 /* Call function FUNC with the rest of NARGS - 1 arguments
2404 following. Return the result, or nil if something went
2405 wrong. Prevent redisplay during the evaluation. */
2407 Lisp_Object
2408 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2410 Lisp_Object val;
2412 if (inhibit_eval_during_redisplay)
2413 val = Qnil;
2414 else
2416 va_list ap;
2417 ptrdiff_t i;
2418 ptrdiff_t count = SPECPDL_INDEX ();
2419 struct gcpro gcpro1;
2420 Lisp_Object *args = alloca (nargs * word_size);
2422 args[0] = func;
2423 va_start (ap, func);
2424 for (i = 1; i < nargs; i++)
2425 args[i] = va_arg (ap, Lisp_Object);
2426 va_end (ap);
2428 GCPRO1 (args[0]);
2429 gcpro1.nvars = nargs;
2430 specbind (Qinhibit_redisplay, Qt);
2431 /* Use Qt to ensure debugger does not run,
2432 so there is no possibility of wanting to redisplay. */
2433 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2434 safe_eval_handler);
2435 UNGCPRO;
2436 val = unbind_to (count, val);
2439 return val;
2443 /* Call function FN with one argument ARG.
2444 Return the result, or nil if something went wrong. */
2446 Lisp_Object
2447 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2449 return safe_call (2, fn, arg);
2452 static Lisp_Object Qeval;
2454 Lisp_Object
2455 safe_eval (Lisp_Object sexpr)
2457 return safe_call1 (Qeval, sexpr);
2460 /* Call function FN with two arguments ARG1 and ARG2.
2461 Return the result, or nil if something went wrong. */
2463 Lisp_Object
2464 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2466 return safe_call (3, fn, arg1, arg2);
2471 /***********************************************************************
2472 Debugging
2473 ***********************************************************************/
2475 #if 0
2477 /* Define CHECK_IT to perform sanity checks on iterators.
2478 This is for debugging. It is too slow to do unconditionally. */
2480 static void
2481 check_it (struct it *it)
2483 if (it->method == GET_FROM_STRING)
2485 eassert (STRINGP (it->string));
2486 eassert (IT_STRING_CHARPOS (*it) >= 0);
2488 else
2490 eassert (IT_STRING_CHARPOS (*it) < 0);
2491 if (it->method == GET_FROM_BUFFER)
2493 /* Check that character and byte positions agree. */
2494 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2498 if (it->dpvec)
2499 eassert (it->current.dpvec_index >= 0);
2500 else
2501 eassert (it->current.dpvec_index < 0);
2504 #define CHECK_IT(IT) check_it ((IT))
2506 #else /* not 0 */
2508 #define CHECK_IT(IT) (void) 0
2510 #endif /* not 0 */
2513 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2515 /* Check that the window end of window W is what we expect it
2516 to be---the last row in the current matrix displaying text. */
2518 static void
2519 check_window_end (struct window *w)
2521 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2523 struct glyph_row *row;
2524 eassert ((row = MATRIX_ROW (w->current_matrix,
2525 XFASTINT (w->window_end_vpos)),
2526 !row->enabled_p
2527 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2528 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2532 #define CHECK_WINDOW_END(W) check_window_end ((W))
2534 #else
2536 #define CHECK_WINDOW_END(W) (void) 0
2538 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2540 /* Return mark position if current buffer has the region of non-zero length,
2541 or -1 otherwise. */
2543 static ptrdiff_t
2544 markpos_of_region (void)
2546 if (!NILP (Vtransient_mark_mode)
2547 && !NILP (BVAR (current_buffer, mark_active))
2548 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2550 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2552 if (markpos != PT)
2553 return markpos;
2555 return -1;
2558 /***********************************************************************
2559 Iterator initialization
2560 ***********************************************************************/
2562 /* Initialize IT for displaying current_buffer in window W, starting
2563 at character position CHARPOS. CHARPOS < 0 means that no buffer
2564 position is specified which is useful when the iterator is assigned
2565 a position later. BYTEPOS is the byte position corresponding to
2566 CHARPOS.
2568 If ROW is not null, calls to produce_glyphs with IT as parameter
2569 will produce glyphs in that row.
2571 BASE_FACE_ID is the id of a base face to use. It must be one of
2572 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2573 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2574 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2576 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2577 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2578 will be initialized to use the corresponding mode line glyph row of
2579 the desired matrix of W. */
2581 void
2582 init_iterator (struct it *it, struct window *w,
2583 ptrdiff_t charpos, ptrdiff_t bytepos,
2584 struct glyph_row *row, enum face_id base_face_id)
2586 ptrdiff_t markpos;
2587 enum face_id remapped_base_face_id = base_face_id;
2589 /* Some precondition checks. */
2590 eassert (w != NULL && it != NULL);
2591 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2592 && charpos <= ZV));
2594 /* If face attributes have been changed since the last redisplay,
2595 free realized faces now because they depend on face definitions
2596 that might have changed. Don't free faces while there might be
2597 desired matrices pending which reference these faces. */
2598 if (face_change_count && !inhibit_free_realized_faces)
2600 face_change_count = 0;
2601 free_all_realized_faces (Qnil);
2604 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2605 if (! NILP (Vface_remapping_alist))
2606 remapped_base_face_id
2607 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2609 /* Use one of the mode line rows of W's desired matrix if
2610 appropriate. */
2611 if (row == NULL)
2613 if (base_face_id == MODE_LINE_FACE_ID
2614 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2615 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2616 else if (base_face_id == HEADER_LINE_FACE_ID)
2617 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2620 /* Clear IT. */
2621 memset (it, 0, sizeof *it);
2622 it->current.overlay_string_index = -1;
2623 it->current.dpvec_index = -1;
2624 it->base_face_id = remapped_base_face_id;
2625 it->string = Qnil;
2626 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2627 it->paragraph_embedding = L2R;
2628 it->bidi_it.string.lstring = Qnil;
2629 it->bidi_it.string.s = NULL;
2630 it->bidi_it.string.bufpos = 0;
2632 /* The window in which we iterate over current_buffer: */
2633 XSETWINDOW (it->window, w);
2634 it->w = w;
2635 it->f = XFRAME (w->frame);
2637 it->cmp_it.id = -1;
2639 /* Extra space between lines (on window systems only). */
2640 if (base_face_id == DEFAULT_FACE_ID
2641 && FRAME_WINDOW_P (it->f))
2643 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2644 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2645 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2646 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2647 * FRAME_LINE_HEIGHT (it->f));
2648 else if (it->f->extra_line_spacing > 0)
2649 it->extra_line_spacing = it->f->extra_line_spacing;
2650 it->max_extra_line_spacing = 0;
2653 /* If realized faces have been removed, e.g. because of face
2654 attribute changes of named faces, recompute them. When running
2655 in batch mode, the face cache of the initial frame is null. If
2656 we happen to get called, make a dummy face cache. */
2657 if (FRAME_FACE_CACHE (it->f) == NULL)
2658 init_frame_faces (it->f);
2659 if (FRAME_FACE_CACHE (it->f)->used == 0)
2660 recompute_basic_faces (it->f);
2662 /* Current value of the `slice', `space-width', and 'height' properties. */
2663 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2664 it->space_width = Qnil;
2665 it->font_height = Qnil;
2666 it->override_ascent = -1;
2668 /* Are control characters displayed as `^C'? */
2669 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2671 /* -1 means everything between a CR and the following line end
2672 is invisible. >0 means lines indented more than this value are
2673 invisible. */
2674 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2675 ? (clip_to_bounds
2676 (-1, XINT (BVAR (current_buffer, selective_display)),
2677 PTRDIFF_MAX))
2678 : (!NILP (BVAR (current_buffer, selective_display))
2679 ? -1 : 0));
2680 it->selective_display_ellipsis_p
2681 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2683 /* Display table to use. */
2684 it->dp = window_display_table (w);
2686 /* Are multibyte characters enabled in current_buffer? */
2687 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2689 /* If visible region is of non-zero length, set IT->region_beg_charpos
2690 and IT->region_end_charpos to the start and end of a visible region
2691 in window IT->w. Set both to -1 to indicate no region. */
2692 markpos = markpos_of_region ();
2693 if (markpos >= 0
2694 /* Maybe highlight only in selected window. */
2695 && (/* Either show region everywhere. */
2696 highlight_nonselected_windows
2697 /* Or show region in the selected window. */
2698 || w == XWINDOW (selected_window)
2699 /* Or show the region if we are in the mini-buffer and W is
2700 the window the mini-buffer refers to. */
2701 || (MINI_WINDOW_P (XWINDOW (selected_window))
2702 && WINDOWP (minibuf_selected_window)
2703 && w == XWINDOW (minibuf_selected_window))))
2705 it->region_beg_charpos = min (PT, markpos);
2706 it->region_end_charpos = max (PT, markpos);
2708 else
2709 it->region_beg_charpos = it->region_end_charpos = -1;
2711 /* Get the position at which the redisplay_end_trigger hook should
2712 be run, if it is to be run at all. */
2713 if (MARKERP (w->redisplay_end_trigger)
2714 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2715 it->redisplay_end_trigger_charpos
2716 = marker_position (w->redisplay_end_trigger);
2717 else if (INTEGERP (w->redisplay_end_trigger))
2718 it->redisplay_end_trigger_charpos =
2719 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2721 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2723 /* Are lines in the display truncated? */
2724 if (base_face_id != DEFAULT_FACE_ID
2725 || it->w->hscroll
2726 || (! WINDOW_FULL_WIDTH_P (it->w)
2727 && ((!NILP (Vtruncate_partial_width_windows)
2728 && !INTEGERP (Vtruncate_partial_width_windows))
2729 || (INTEGERP (Vtruncate_partial_width_windows)
2730 && (WINDOW_TOTAL_COLS (it->w)
2731 < XINT (Vtruncate_partial_width_windows))))))
2732 it->line_wrap = TRUNCATE;
2733 else if (NILP (BVAR (current_buffer, truncate_lines)))
2734 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2735 ? WINDOW_WRAP : WORD_WRAP;
2736 else
2737 it->line_wrap = TRUNCATE;
2739 /* Get dimensions of truncation and continuation glyphs. These are
2740 displayed as fringe bitmaps under X, but we need them for such
2741 frames when the fringes are turned off. But leave the dimensions
2742 zero for tooltip frames, as these glyphs look ugly there and also
2743 sabotage calculations of tooltip dimensions in x-show-tip. */
2744 #ifdef HAVE_WINDOW_SYSTEM
2745 if (!(FRAME_WINDOW_P (it->f)
2746 && FRAMEP (tip_frame)
2747 && it->f == XFRAME (tip_frame)))
2748 #endif
2750 if (it->line_wrap == TRUNCATE)
2752 /* We will need the truncation glyph. */
2753 eassert (it->glyph_row == NULL);
2754 produce_special_glyphs (it, IT_TRUNCATION);
2755 it->truncation_pixel_width = it->pixel_width;
2757 else
2759 /* We will need the continuation glyph. */
2760 eassert (it->glyph_row == NULL);
2761 produce_special_glyphs (it, IT_CONTINUATION);
2762 it->continuation_pixel_width = it->pixel_width;
2766 /* Reset these values to zero because the produce_special_glyphs
2767 above has changed them. */
2768 it->pixel_width = it->ascent = it->descent = 0;
2769 it->phys_ascent = it->phys_descent = 0;
2771 /* Set this after getting the dimensions of truncation and
2772 continuation glyphs, so that we don't produce glyphs when calling
2773 produce_special_glyphs, above. */
2774 it->glyph_row = row;
2775 it->area = TEXT_AREA;
2777 /* Forget any previous info about this row being reversed. */
2778 if (it->glyph_row)
2779 it->glyph_row->reversed_p = 0;
2781 /* Get the dimensions of the display area. The display area
2782 consists of the visible window area plus a horizontally scrolled
2783 part to the left of the window. All x-values are relative to the
2784 start of this total display area. */
2785 if (base_face_id != DEFAULT_FACE_ID)
2787 /* Mode lines, menu bar in terminal frames. */
2788 it->first_visible_x = 0;
2789 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2791 else
2793 it->first_visible_x =
2794 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2795 it->last_visible_x = (it->first_visible_x
2796 + window_box_width (w, TEXT_AREA));
2798 /* If we truncate lines, leave room for the truncation glyph(s) at
2799 the right margin. Otherwise, leave room for the continuation
2800 glyph(s). Done only if the window has no fringes. Since we
2801 don't know at this point whether there will be any R2L lines in
2802 the window, we reserve space for truncation/continuation glyphs
2803 even if only one of the fringes is absent. */
2804 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2805 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2807 if (it->line_wrap == TRUNCATE)
2808 it->last_visible_x -= it->truncation_pixel_width;
2809 else
2810 it->last_visible_x -= it->continuation_pixel_width;
2813 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2814 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2817 /* Leave room for a border glyph. */
2818 if (!FRAME_WINDOW_P (it->f)
2819 && !WINDOW_RIGHTMOST_P (it->w))
2820 it->last_visible_x -= 1;
2822 it->last_visible_y = window_text_bottom_y (w);
2824 /* For mode lines and alike, arrange for the first glyph having a
2825 left box line if the face specifies a box. */
2826 if (base_face_id != DEFAULT_FACE_ID)
2828 struct face *face;
2830 it->face_id = remapped_base_face_id;
2832 /* If we have a boxed mode line, make the first character appear
2833 with a left box line. */
2834 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2835 if (face->box != FACE_NO_BOX)
2836 it->start_of_box_run_p = 1;
2839 /* If a buffer position was specified, set the iterator there,
2840 getting overlays and face properties from that position. */
2841 if (charpos >= BUF_BEG (current_buffer))
2843 it->end_charpos = ZV;
2844 eassert (charpos == BYTE_TO_CHAR (bytepos));
2845 IT_CHARPOS (*it) = charpos;
2846 IT_BYTEPOS (*it) = bytepos;
2848 /* We will rely on `reseat' to set this up properly, via
2849 handle_face_prop. */
2850 it->face_id = it->base_face_id;
2852 it->start = it->current;
2853 /* Do we need to reorder bidirectional text? Not if this is a
2854 unibyte buffer: by definition, none of the single-byte
2855 characters are strong R2L, so no reordering is needed. And
2856 bidi.c doesn't support unibyte buffers anyway. Also, don't
2857 reorder while we are loading loadup.el, since the tables of
2858 character properties needed for reordering are not yet
2859 available. */
2860 it->bidi_p =
2861 NILP (Vpurify_flag)
2862 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2863 && it->multibyte_p;
2865 /* If we are to reorder bidirectional text, init the bidi
2866 iterator. */
2867 if (it->bidi_p)
2869 /* Note the paragraph direction that this buffer wants to
2870 use. */
2871 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2872 Qleft_to_right))
2873 it->paragraph_embedding = L2R;
2874 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2875 Qright_to_left))
2876 it->paragraph_embedding = R2L;
2877 else
2878 it->paragraph_embedding = NEUTRAL_DIR;
2879 bidi_unshelve_cache (NULL, 0);
2880 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2881 &it->bidi_it);
2884 /* Compute faces etc. */
2885 reseat (it, it->current.pos, 1);
2888 CHECK_IT (it);
2892 /* Initialize IT for the display of window W with window start POS. */
2894 void
2895 start_display (struct it *it, struct window *w, struct text_pos pos)
2897 struct glyph_row *row;
2898 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2900 row = w->desired_matrix->rows + first_vpos;
2901 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2902 it->first_vpos = first_vpos;
2904 /* Don't reseat to previous visible line start if current start
2905 position is in a string or image. */
2906 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2908 int start_at_line_beg_p;
2909 int first_y = it->current_y;
2911 /* If window start is not at a line start, skip forward to POS to
2912 get the correct continuation lines width. */
2913 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2914 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2915 if (!start_at_line_beg_p)
2917 int new_x;
2919 reseat_at_previous_visible_line_start (it);
2920 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2922 new_x = it->current_x + it->pixel_width;
2924 /* If lines are continued, this line may end in the middle
2925 of a multi-glyph character (e.g. a control character
2926 displayed as \003, or in the middle of an overlay
2927 string). In this case move_it_to above will not have
2928 taken us to the start of the continuation line but to the
2929 end of the continued line. */
2930 if (it->current_x > 0
2931 && it->line_wrap != TRUNCATE /* Lines are continued. */
2932 && (/* And glyph doesn't fit on the line. */
2933 new_x > it->last_visible_x
2934 /* Or it fits exactly and we're on a window
2935 system frame. */
2936 || (new_x == it->last_visible_x
2937 && FRAME_WINDOW_P (it->f)
2938 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2939 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2940 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2942 if ((it->current.dpvec_index >= 0
2943 || it->current.overlay_string_index >= 0)
2944 /* If we are on a newline from a display vector or
2945 overlay string, then we are already at the end of
2946 a screen line; no need to go to the next line in
2947 that case, as this line is not really continued.
2948 (If we do go to the next line, C-e will not DTRT.) */
2949 && it->c != '\n')
2951 set_iterator_to_next (it, 1);
2952 move_it_in_display_line_to (it, -1, -1, 0);
2955 it->continuation_lines_width += it->current_x;
2957 /* If the character at POS is displayed via a display
2958 vector, move_it_to above stops at the final glyph of
2959 IT->dpvec. To make the caller redisplay that character
2960 again (a.k.a. start at POS), we need to reset the
2961 dpvec_index to the beginning of IT->dpvec. */
2962 else if (it->current.dpvec_index >= 0)
2963 it->current.dpvec_index = 0;
2965 /* We're starting a new display line, not affected by the
2966 height of the continued line, so clear the appropriate
2967 fields in the iterator structure. */
2968 it->max_ascent = it->max_descent = 0;
2969 it->max_phys_ascent = it->max_phys_descent = 0;
2971 it->current_y = first_y;
2972 it->vpos = 0;
2973 it->current_x = it->hpos = 0;
2979 /* Return 1 if POS is a position in ellipses displayed for invisible
2980 text. W is the window we display, for text property lookup. */
2982 static int
2983 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2985 Lisp_Object prop, window;
2986 int ellipses_p = 0;
2987 ptrdiff_t charpos = CHARPOS (pos->pos);
2989 /* If POS specifies a position in a display vector, this might
2990 be for an ellipsis displayed for invisible text. We won't
2991 get the iterator set up for delivering that ellipsis unless
2992 we make sure that it gets aware of the invisible text. */
2993 if (pos->dpvec_index >= 0
2994 && pos->overlay_string_index < 0
2995 && CHARPOS (pos->string_pos) < 0
2996 && charpos > BEGV
2997 && (XSETWINDOW (window, w),
2998 prop = Fget_char_property (make_number (charpos),
2999 Qinvisible, window),
3000 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3002 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3003 window);
3004 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3007 return ellipses_p;
3011 /* Initialize IT for stepping through current_buffer in window W,
3012 starting at position POS that includes overlay string and display
3013 vector/ control character translation position information. Value
3014 is zero if there are overlay strings with newlines at POS. */
3016 static int
3017 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3019 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3020 int i, overlay_strings_with_newlines = 0;
3022 /* If POS specifies a position in a display vector, this might
3023 be for an ellipsis displayed for invisible text. We won't
3024 get the iterator set up for delivering that ellipsis unless
3025 we make sure that it gets aware of the invisible text. */
3026 if (in_ellipses_for_invisible_text_p (pos, w))
3028 --charpos;
3029 bytepos = 0;
3032 /* Keep in mind: the call to reseat in init_iterator skips invisible
3033 text, so we might end up at a position different from POS. This
3034 is only a problem when POS is a row start after a newline and an
3035 overlay starts there with an after-string, and the overlay has an
3036 invisible property. Since we don't skip invisible text in
3037 display_line and elsewhere immediately after consuming the
3038 newline before the row start, such a POS will not be in a string,
3039 but the call to init_iterator below will move us to the
3040 after-string. */
3041 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3043 /* This only scans the current chunk -- it should scan all chunks.
3044 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3045 to 16 in 22.1 to make this a lesser problem. */
3046 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3048 const char *s = SSDATA (it->overlay_strings[i]);
3049 const char *e = s + SBYTES (it->overlay_strings[i]);
3051 while (s < e && *s != '\n')
3052 ++s;
3054 if (s < e)
3056 overlay_strings_with_newlines = 1;
3057 break;
3061 /* If position is within an overlay string, set up IT to the right
3062 overlay string. */
3063 if (pos->overlay_string_index >= 0)
3065 int relative_index;
3067 /* If the first overlay string happens to have a `display'
3068 property for an image, the iterator will be set up for that
3069 image, and we have to undo that setup first before we can
3070 correct the overlay string index. */
3071 if (it->method == GET_FROM_IMAGE)
3072 pop_it (it);
3074 /* We already have the first chunk of overlay strings in
3075 IT->overlay_strings. Load more until the one for
3076 pos->overlay_string_index is in IT->overlay_strings. */
3077 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3079 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3080 it->current.overlay_string_index = 0;
3081 while (n--)
3083 load_overlay_strings (it, 0);
3084 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3088 it->current.overlay_string_index = pos->overlay_string_index;
3089 relative_index = (it->current.overlay_string_index
3090 % OVERLAY_STRING_CHUNK_SIZE);
3091 it->string = it->overlay_strings[relative_index];
3092 eassert (STRINGP (it->string));
3093 it->current.string_pos = pos->string_pos;
3094 it->method = GET_FROM_STRING;
3095 it->end_charpos = SCHARS (it->string);
3096 /* Set up the bidi iterator for this overlay string. */
3097 if (it->bidi_p)
3099 it->bidi_it.string.lstring = it->string;
3100 it->bidi_it.string.s = NULL;
3101 it->bidi_it.string.schars = SCHARS (it->string);
3102 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3103 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3104 it->bidi_it.string.unibyte = !it->multibyte_p;
3105 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3106 FRAME_WINDOW_P (it->f), &it->bidi_it);
3108 /* Synchronize the state of the bidi iterator with
3109 pos->string_pos. For any string position other than
3110 zero, this will be done automagically when we resume
3111 iteration over the string and get_visually_first_element
3112 is called. But if string_pos is zero, and the string is
3113 to be reordered for display, we need to resync manually,
3114 since it could be that the iteration state recorded in
3115 pos ended at string_pos of 0 moving backwards in string. */
3116 if (CHARPOS (pos->string_pos) == 0)
3118 get_visually_first_element (it);
3119 if (IT_STRING_CHARPOS (*it) != 0)
3120 do {
3121 /* Paranoia. */
3122 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3123 bidi_move_to_visually_next (&it->bidi_it);
3124 } while (it->bidi_it.charpos != 0);
3126 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3127 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3131 if (CHARPOS (pos->string_pos) >= 0)
3133 /* Recorded position is not in an overlay string, but in another
3134 string. This can only be a string from a `display' property.
3135 IT should already be filled with that string. */
3136 it->current.string_pos = pos->string_pos;
3137 eassert (STRINGP (it->string));
3138 if (it->bidi_p)
3139 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3140 FRAME_WINDOW_P (it->f), &it->bidi_it);
3143 /* Restore position in display vector translations, control
3144 character translations or ellipses. */
3145 if (pos->dpvec_index >= 0)
3147 if (it->dpvec == NULL)
3148 get_next_display_element (it);
3149 eassert (it->dpvec && it->current.dpvec_index == 0);
3150 it->current.dpvec_index = pos->dpvec_index;
3153 CHECK_IT (it);
3154 return !overlay_strings_with_newlines;
3158 /* Initialize IT for stepping through current_buffer in window W
3159 starting at ROW->start. */
3161 static void
3162 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3164 init_from_display_pos (it, w, &row->start);
3165 it->start = row->start;
3166 it->continuation_lines_width = row->continuation_lines_width;
3167 CHECK_IT (it);
3171 /* Initialize IT for stepping through current_buffer in window W
3172 starting in the line following ROW, i.e. starting at ROW->end.
3173 Value is zero if there are overlay strings with newlines at ROW's
3174 end position. */
3176 static int
3177 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3179 int success = 0;
3181 if (init_from_display_pos (it, w, &row->end))
3183 if (row->continued_p)
3184 it->continuation_lines_width
3185 = row->continuation_lines_width + row->pixel_width;
3186 CHECK_IT (it);
3187 success = 1;
3190 return success;
3196 /***********************************************************************
3197 Text properties
3198 ***********************************************************************/
3200 /* Called when IT reaches IT->stop_charpos. Handle text property and
3201 overlay changes. Set IT->stop_charpos to the next position where
3202 to stop. */
3204 static void
3205 handle_stop (struct it *it)
3207 enum prop_handled handled;
3208 int handle_overlay_change_p;
3209 struct props *p;
3211 it->dpvec = NULL;
3212 it->current.dpvec_index = -1;
3213 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3214 it->ignore_overlay_strings_at_pos_p = 0;
3215 it->ellipsis_p = 0;
3217 /* Use face of preceding text for ellipsis (if invisible) */
3218 if (it->selective_display_ellipsis_p)
3219 it->saved_face_id = it->face_id;
3223 handled = HANDLED_NORMALLY;
3225 /* Call text property handlers. */
3226 for (p = it_props; p->handler; ++p)
3228 handled = p->handler (it);
3230 if (handled == HANDLED_RECOMPUTE_PROPS)
3231 break;
3232 else if (handled == HANDLED_RETURN)
3234 /* We still want to show before and after strings from
3235 overlays even if the actual buffer text is replaced. */
3236 if (!handle_overlay_change_p
3237 || it->sp > 1
3238 /* Don't call get_overlay_strings_1 if we already
3239 have overlay strings loaded, because doing so
3240 will load them again and push the iterator state
3241 onto the stack one more time, which is not
3242 expected by the rest of the code that processes
3243 overlay strings. */
3244 || (it->current.overlay_string_index < 0
3245 ? !get_overlay_strings_1 (it, 0, 0)
3246 : 0))
3248 if (it->ellipsis_p)
3249 setup_for_ellipsis (it, 0);
3250 /* When handling a display spec, we might load an
3251 empty string. In that case, discard it here. We
3252 used to discard it in handle_single_display_spec,
3253 but that causes get_overlay_strings_1, above, to
3254 ignore overlay strings that we must check. */
3255 if (STRINGP (it->string) && !SCHARS (it->string))
3256 pop_it (it);
3257 return;
3259 else if (STRINGP (it->string) && !SCHARS (it->string))
3260 pop_it (it);
3261 else
3263 it->ignore_overlay_strings_at_pos_p = 1;
3264 it->string_from_display_prop_p = 0;
3265 it->from_disp_prop_p = 0;
3266 handle_overlay_change_p = 0;
3268 handled = HANDLED_RECOMPUTE_PROPS;
3269 break;
3271 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3272 handle_overlay_change_p = 0;
3275 if (handled != HANDLED_RECOMPUTE_PROPS)
3277 /* Don't check for overlay strings below when set to deliver
3278 characters from a display vector. */
3279 if (it->method == GET_FROM_DISPLAY_VECTOR)
3280 handle_overlay_change_p = 0;
3282 /* Handle overlay changes.
3283 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3284 if it finds overlays. */
3285 if (handle_overlay_change_p)
3286 handled = handle_overlay_change (it);
3289 if (it->ellipsis_p)
3291 setup_for_ellipsis (it, 0);
3292 break;
3295 while (handled == HANDLED_RECOMPUTE_PROPS);
3297 /* Determine where to stop next. */
3298 if (handled == HANDLED_NORMALLY)
3299 compute_stop_pos (it);
3303 /* Compute IT->stop_charpos from text property and overlay change
3304 information for IT's current position. */
3306 static void
3307 compute_stop_pos (struct it *it)
3309 register INTERVAL iv, next_iv;
3310 Lisp_Object object, limit, position;
3311 ptrdiff_t charpos, bytepos;
3313 if (STRINGP (it->string))
3315 /* Strings are usually short, so don't limit the search for
3316 properties. */
3317 it->stop_charpos = it->end_charpos;
3318 object = it->string;
3319 limit = Qnil;
3320 charpos = IT_STRING_CHARPOS (*it);
3321 bytepos = IT_STRING_BYTEPOS (*it);
3323 else
3325 ptrdiff_t pos;
3327 /* If end_charpos is out of range for some reason, such as a
3328 misbehaving display function, rationalize it (Bug#5984). */
3329 if (it->end_charpos > ZV)
3330 it->end_charpos = ZV;
3331 it->stop_charpos = it->end_charpos;
3333 /* If next overlay change is in front of the current stop pos
3334 (which is IT->end_charpos), stop there. Note: value of
3335 next_overlay_change is point-max if no overlay change
3336 follows. */
3337 charpos = IT_CHARPOS (*it);
3338 bytepos = IT_BYTEPOS (*it);
3339 pos = next_overlay_change (charpos);
3340 if (pos < it->stop_charpos)
3341 it->stop_charpos = pos;
3343 /* If showing the region, we have to stop at the region
3344 start or end because the face might change there. */
3345 if (it->region_beg_charpos > 0)
3347 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3348 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3349 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3350 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3353 /* Set up variables for computing the stop position from text
3354 property changes. */
3355 XSETBUFFER (object, current_buffer);
3356 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3359 /* Get the interval containing IT's position. Value is a null
3360 interval if there isn't such an interval. */
3361 position = make_number (charpos);
3362 iv = validate_interval_range (object, &position, &position, 0);
3363 if (iv)
3365 Lisp_Object values_here[LAST_PROP_IDX];
3366 struct props *p;
3368 /* Get properties here. */
3369 for (p = it_props; p->handler; ++p)
3370 values_here[p->idx] = textget (iv->plist, *p->name);
3372 /* Look for an interval following iv that has different
3373 properties. */
3374 for (next_iv = next_interval (iv);
3375 (next_iv
3376 && (NILP (limit)
3377 || XFASTINT (limit) > next_iv->position));
3378 next_iv = next_interval (next_iv))
3380 for (p = it_props; p->handler; ++p)
3382 Lisp_Object new_value;
3384 new_value = textget (next_iv->plist, *p->name);
3385 if (!EQ (values_here[p->idx], new_value))
3386 break;
3389 if (p->handler)
3390 break;
3393 if (next_iv)
3395 if (INTEGERP (limit)
3396 && next_iv->position >= XFASTINT (limit))
3397 /* No text property change up to limit. */
3398 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3399 else
3400 /* Text properties change in next_iv. */
3401 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3405 if (it->cmp_it.id < 0)
3407 ptrdiff_t stoppos = it->end_charpos;
3409 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3410 stoppos = -1;
3411 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3412 stoppos, it->string);
3415 eassert (STRINGP (it->string)
3416 || (it->stop_charpos >= BEGV
3417 && it->stop_charpos >= IT_CHARPOS (*it)));
3421 /* Return the position of the next overlay change after POS in
3422 current_buffer. Value is point-max if no overlay change
3423 follows. This is like `next-overlay-change' but doesn't use
3424 xmalloc. */
3426 static ptrdiff_t
3427 next_overlay_change (ptrdiff_t pos)
3429 ptrdiff_t i, noverlays;
3430 ptrdiff_t endpos;
3431 Lisp_Object *overlays;
3433 /* Get all overlays at the given position. */
3434 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3436 /* If any of these overlays ends before endpos,
3437 use its ending point instead. */
3438 for (i = 0; i < noverlays; ++i)
3440 Lisp_Object oend;
3441 ptrdiff_t oendpos;
3443 oend = OVERLAY_END (overlays[i]);
3444 oendpos = OVERLAY_POSITION (oend);
3445 endpos = min (endpos, oendpos);
3448 return endpos;
3451 /* How many characters forward to search for a display property or
3452 display string. Searching too far forward makes the bidi display
3453 sluggish, especially in small windows. */
3454 #define MAX_DISP_SCAN 250
3456 /* Return the character position of a display string at or after
3457 position specified by POSITION. If no display string exists at or
3458 after POSITION, return ZV. A display string is either an overlay
3459 with `display' property whose value is a string, or a `display'
3460 text property whose value is a string. STRING is data about the
3461 string to iterate; if STRING->lstring is nil, we are iterating a
3462 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3463 on a GUI frame. DISP_PROP is set to zero if we searched
3464 MAX_DISP_SCAN characters forward without finding any display
3465 strings, non-zero otherwise. It is set to 2 if the display string
3466 uses any kind of `(space ...)' spec that will produce a stretch of
3467 white space in the text area. */
3468 ptrdiff_t
3469 compute_display_string_pos (struct text_pos *position,
3470 struct bidi_string_data *string,
3471 int frame_window_p, int *disp_prop)
3473 /* OBJECT = nil means current buffer. */
3474 Lisp_Object object =
3475 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3476 Lisp_Object pos, spec, limpos;
3477 int string_p = (string && (STRINGP (string->lstring) || string->s));
3478 ptrdiff_t eob = string_p ? string->schars : ZV;
3479 ptrdiff_t begb = string_p ? 0 : BEGV;
3480 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3481 ptrdiff_t lim =
3482 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3483 struct text_pos tpos;
3484 int rv = 0;
3486 *disp_prop = 1;
3488 if (charpos >= eob
3489 /* We don't support display properties whose values are strings
3490 that have display string properties. */
3491 || string->from_disp_str
3492 /* C strings cannot have display properties. */
3493 || (string->s && !STRINGP (object)))
3495 *disp_prop = 0;
3496 return eob;
3499 /* If the character at CHARPOS is where the display string begins,
3500 return CHARPOS. */
3501 pos = make_number (charpos);
3502 if (STRINGP (object))
3503 bufpos = string->bufpos;
3504 else
3505 bufpos = charpos;
3506 tpos = *position;
3507 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3508 && (charpos <= begb
3509 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3510 object),
3511 spec))
3512 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3513 frame_window_p)))
3515 if (rv == 2)
3516 *disp_prop = 2;
3517 return charpos;
3520 /* Look forward for the first character with a `display' property
3521 that will replace the underlying text when displayed. */
3522 limpos = make_number (lim);
3523 do {
3524 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3525 CHARPOS (tpos) = XFASTINT (pos);
3526 if (CHARPOS (tpos) >= lim)
3528 *disp_prop = 0;
3529 break;
3531 if (STRINGP (object))
3532 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3533 else
3534 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3535 spec = Fget_char_property (pos, Qdisplay, object);
3536 if (!STRINGP (object))
3537 bufpos = CHARPOS (tpos);
3538 } while (NILP (spec)
3539 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3540 bufpos, frame_window_p)));
3541 if (rv == 2)
3542 *disp_prop = 2;
3544 return CHARPOS (tpos);
3547 /* Return the character position of the end of the display string that
3548 started at CHARPOS. If there's no display string at CHARPOS,
3549 return -1. A display string is either an overlay with `display'
3550 property whose value is a string or a `display' text property whose
3551 value is a string. */
3552 ptrdiff_t
3553 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3555 /* OBJECT = nil means current buffer. */
3556 Lisp_Object object =
3557 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3558 Lisp_Object pos = make_number (charpos);
3559 ptrdiff_t eob =
3560 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3562 if (charpos >= eob || (string->s && !STRINGP (object)))
3563 return eob;
3565 /* It could happen that the display property or overlay was removed
3566 since we found it in compute_display_string_pos above. One way
3567 this can happen is if JIT font-lock was called (through
3568 handle_fontified_prop), and jit-lock-functions remove text
3569 properties or overlays from the portion of buffer that includes
3570 CHARPOS. Muse mode is known to do that, for example. In this
3571 case, we return -1 to the caller, to signal that no display
3572 string is actually present at CHARPOS. See bidi_fetch_char for
3573 how this is handled.
3575 An alternative would be to never look for display properties past
3576 it->stop_charpos. But neither compute_display_string_pos nor
3577 bidi_fetch_char that calls it know or care where the next
3578 stop_charpos is. */
3579 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3580 return -1;
3582 /* Look forward for the first character where the `display' property
3583 changes. */
3584 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3586 return XFASTINT (pos);
3591 /***********************************************************************
3592 Fontification
3593 ***********************************************************************/
3595 /* Handle changes in the `fontified' property of the current buffer by
3596 calling hook functions from Qfontification_functions to fontify
3597 regions of text. */
3599 static enum prop_handled
3600 handle_fontified_prop (struct it *it)
3602 Lisp_Object prop, pos;
3603 enum prop_handled handled = HANDLED_NORMALLY;
3605 if (!NILP (Vmemory_full))
3606 return handled;
3608 /* Get the value of the `fontified' property at IT's current buffer
3609 position. (The `fontified' property doesn't have a special
3610 meaning in strings.) If the value is nil, call functions from
3611 Qfontification_functions. */
3612 if (!STRINGP (it->string)
3613 && it->s == NULL
3614 && !NILP (Vfontification_functions)
3615 && !NILP (Vrun_hooks)
3616 && (pos = make_number (IT_CHARPOS (*it)),
3617 prop = Fget_char_property (pos, Qfontified, Qnil),
3618 /* Ignore the special cased nil value always present at EOB since
3619 no amount of fontifying will be able to change it. */
3620 NILP (prop) && IT_CHARPOS (*it) < Z))
3622 ptrdiff_t count = SPECPDL_INDEX ();
3623 Lisp_Object val;
3624 struct buffer *obuf = current_buffer;
3625 int begv = BEGV, zv = ZV;
3626 int old_clip_changed = current_buffer->clip_changed;
3628 val = Vfontification_functions;
3629 specbind (Qfontification_functions, Qnil);
3631 eassert (it->end_charpos == ZV);
3633 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3634 safe_call1 (val, pos);
3635 else
3637 Lisp_Object fns, fn;
3638 struct gcpro gcpro1, gcpro2;
3640 fns = Qnil;
3641 GCPRO2 (val, fns);
3643 for (; CONSP (val); val = XCDR (val))
3645 fn = XCAR (val);
3647 if (EQ (fn, Qt))
3649 /* A value of t indicates this hook has a local
3650 binding; it means to run the global binding too.
3651 In a global value, t should not occur. If it
3652 does, we must ignore it to avoid an endless
3653 loop. */
3654 for (fns = Fdefault_value (Qfontification_functions);
3655 CONSP (fns);
3656 fns = XCDR (fns))
3658 fn = XCAR (fns);
3659 if (!EQ (fn, Qt))
3660 safe_call1 (fn, pos);
3663 else
3664 safe_call1 (fn, pos);
3667 UNGCPRO;
3670 unbind_to (count, Qnil);
3672 /* Fontification functions routinely call `save-restriction'.
3673 Normally, this tags clip_changed, which can confuse redisplay
3674 (see discussion in Bug#6671). Since we don't perform any
3675 special handling of fontification changes in the case where
3676 `save-restriction' isn't called, there's no point doing so in
3677 this case either. So, if the buffer's restrictions are
3678 actually left unchanged, reset clip_changed. */
3679 if (obuf == current_buffer)
3681 if (begv == BEGV && zv == ZV)
3682 current_buffer->clip_changed = old_clip_changed;
3684 /* There isn't much we can reasonably do to protect against
3685 misbehaving fontification, but here's a fig leaf. */
3686 else if (BUFFER_LIVE_P (obuf))
3687 set_buffer_internal_1 (obuf);
3689 /* The fontification code may have added/removed text.
3690 It could do even a lot worse, but let's at least protect against
3691 the most obvious case where only the text past `pos' gets changed',
3692 as is/was done in grep.el where some escapes sequences are turned
3693 into face properties (bug#7876). */
3694 it->end_charpos = ZV;
3696 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3697 something. This avoids an endless loop if they failed to
3698 fontify the text for which reason ever. */
3699 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3700 handled = HANDLED_RECOMPUTE_PROPS;
3703 return handled;
3708 /***********************************************************************
3709 Faces
3710 ***********************************************************************/
3712 /* Set up iterator IT from face properties at its current position.
3713 Called from handle_stop. */
3715 static enum prop_handled
3716 handle_face_prop (struct it *it)
3718 int new_face_id;
3719 ptrdiff_t next_stop;
3721 if (!STRINGP (it->string))
3723 new_face_id
3724 = face_at_buffer_position (it->w,
3725 IT_CHARPOS (*it),
3726 it->region_beg_charpos,
3727 it->region_end_charpos,
3728 &next_stop,
3729 (IT_CHARPOS (*it)
3730 + TEXT_PROP_DISTANCE_LIMIT),
3731 0, it->base_face_id);
3733 /* Is this a start of a run of characters with box face?
3734 Caveat: this can be called for a freshly initialized
3735 iterator; face_id is -1 in this case. We know that the new
3736 face will not change until limit, i.e. if the new face has a
3737 box, all characters up to limit will have one. But, as
3738 usual, we don't know whether limit is really the end. */
3739 if (new_face_id != it->face_id)
3741 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3742 /* If it->face_id is -1, old_face below will be NULL, see
3743 the definition of FACE_FROM_ID. This will happen if this
3744 is the initial call that gets the face. */
3745 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3747 /* If the value of face_id of the iterator is -1, we have to
3748 look in front of IT's position and see whether there is a
3749 face there that's different from new_face_id. */
3750 if (!old_face && IT_CHARPOS (*it) > BEG)
3752 int prev_face_id = face_before_it_pos (it);
3754 old_face = FACE_FROM_ID (it->f, prev_face_id);
3757 /* If the new face has a box, but the old face does not,
3758 this is the start of a run of characters with box face,
3759 i.e. this character has a shadow on the left side. */
3760 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3761 && (old_face == NULL || !old_face->box));
3762 it->face_box_p = new_face->box != FACE_NO_BOX;
3765 else
3767 int base_face_id;
3768 ptrdiff_t bufpos;
3769 int i;
3770 Lisp_Object from_overlay
3771 = (it->current.overlay_string_index >= 0
3772 ? it->string_overlays[it->current.overlay_string_index
3773 % OVERLAY_STRING_CHUNK_SIZE]
3774 : Qnil);
3776 /* See if we got to this string directly or indirectly from
3777 an overlay property. That includes the before-string or
3778 after-string of an overlay, strings in display properties
3779 provided by an overlay, their text properties, etc.
3781 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3782 if (! NILP (from_overlay))
3783 for (i = it->sp - 1; i >= 0; i--)
3785 if (it->stack[i].current.overlay_string_index >= 0)
3786 from_overlay
3787 = it->string_overlays[it->stack[i].current.overlay_string_index
3788 % OVERLAY_STRING_CHUNK_SIZE];
3789 else if (! NILP (it->stack[i].from_overlay))
3790 from_overlay = it->stack[i].from_overlay;
3792 if (!NILP (from_overlay))
3793 break;
3796 if (! NILP (from_overlay))
3798 bufpos = IT_CHARPOS (*it);
3799 /* For a string from an overlay, the base face depends
3800 only on text properties and ignores overlays. */
3801 base_face_id
3802 = face_for_overlay_string (it->w,
3803 IT_CHARPOS (*it),
3804 it->region_beg_charpos,
3805 it->region_end_charpos,
3806 &next_stop,
3807 (IT_CHARPOS (*it)
3808 + TEXT_PROP_DISTANCE_LIMIT),
3810 from_overlay);
3812 else
3814 bufpos = 0;
3816 /* For strings from a `display' property, use the face at
3817 IT's current buffer position as the base face to merge
3818 with, so that overlay strings appear in the same face as
3819 surrounding text, unless they specify their own
3820 faces. */
3821 base_face_id = it->string_from_prefix_prop_p
3822 ? DEFAULT_FACE_ID
3823 : underlying_face_id (it);
3826 new_face_id = face_at_string_position (it->w,
3827 it->string,
3828 IT_STRING_CHARPOS (*it),
3829 bufpos,
3830 it->region_beg_charpos,
3831 it->region_end_charpos,
3832 &next_stop,
3833 base_face_id, 0);
3835 /* Is this a start of a run of characters with box? Caveat:
3836 this can be called for a freshly allocated iterator; face_id
3837 is -1 is this case. We know that the new face will not
3838 change until the next check pos, i.e. if the new face has a
3839 box, all characters up to that position will have a
3840 box. But, as usual, we don't know whether that position
3841 is really the end. */
3842 if (new_face_id != it->face_id)
3844 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3845 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3847 /* If new face has a box but old face hasn't, this is the
3848 start of a run of characters with box, i.e. it has a
3849 shadow on the left side. */
3850 it->start_of_box_run_p
3851 = new_face->box && (old_face == NULL || !old_face->box);
3852 it->face_box_p = new_face->box != FACE_NO_BOX;
3856 it->face_id = new_face_id;
3857 return HANDLED_NORMALLY;
3861 /* Return the ID of the face ``underlying'' IT's current position,
3862 which is in a string. If the iterator is associated with a
3863 buffer, return the face at IT's current buffer position.
3864 Otherwise, use the iterator's base_face_id. */
3866 static int
3867 underlying_face_id (struct it *it)
3869 int face_id = it->base_face_id, i;
3871 eassert (STRINGP (it->string));
3873 for (i = it->sp - 1; i >= 0; --i)
3874 if (NILP (it->stack[i].string))
3875 face_id = it->stack[i].face_id;
3877 return face_id;
3881 /* Compute the face one character before or after the current position
3882 of IT, in the visual order. BEFORE_P non-zero means get the face
3883 in front (to the left in L2R paragraphs, to the right in R2L
3884 paragraphs) of IT's screen position. Value is the ID of the face. */
3886 static int
3887 face_before_or_after_it_pos (struct it *it, int before_p)
3889 int face_id, limit;
3890 ptrdiff_t next_check_charpos;
3891 struct it it_copy;
3892 void *it_copy_data = NULL;
3894 eassert (it->s == NULL);
3896 if (STRINGP (it->string))
3898 ptrdiff_t bufpos, charpos;
3899 int base_face_id;
3901 /* No face change past the end of the string (for the case
3902 we are padding with spaces). No face change before the
3903 string start. */
3904 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3905 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3906 return it->face_id;
3908 if (!it->bidi_p)
3910 /* Set charpos to the position before or after IT's current
3911 position, in the logical order, which in the non-bidi
3912 case is the same as the visual order. */
3913 if (before_p)
3914 charpos = IT_STRING_CHARPOS (*it) - 1;
3915 else if (it->what == IT_COMPOSITION)
3916 /* For composition, we must check the character after the
3917 composition. */
3918 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3919 else
3920 charpos = IT_STRING_CHARPOS (*it) + 1;
3922 else
3924 if (before_p)
3926 /* With bidi iteration, the character before the current
3927 in the visual order cannot be found by simple
3928 iteration, because "reverse" reordering is not
3929 supported. Instead, we need to use the move_it_*
3930 family of functions. */
3931 /* Ignore face changes before the first visible
3932 character on this display line. */
3933 if (it->current_x <= it->first_visible_x)
3934 return it->face_id;
3935 SAVE_IT (it_copy, *it, it_copy_data);
3936 /* Implementation note: Since move_it_in_display_line
3937 works in the iterator geometry, and thinks the first
3938 character is always the leftmost, even in R2L lines,
3939 we don't need to distinguish between the R2L and L2R
3940 cases here. */
3941 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3942 it_copy.current_x - 1, MOVE_TO_X);
3943 charpos = IT_STRING_CHARPOS (it_copy);
3944 RESTORE_IT (it, it, it_copy_data);
3946 else
3948 /* Set charpos to the string position of the character
3949 that comes after IT's current position in the visual
3950 order. */
3951 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3953 it_copy = *it;
3954 while (n--)
3955 bidi_move_to_visually_next (&it_copy.bidi_it);
3957 charpos = it_copy.bidi_it.charpos;
3960 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3962 if (it->current.overlay_string_index >= 0)
3963 bufpos = IT_CHARPOS (*it);
3964 else
3965 bufpos = 0;
3967 base_face_id = underlying_face_id (it);
3969 /* Get the face for ASCII, or unibyte. */
3970 face_id = face_at_string_position (it->w,
3971 it->string,
3972 charpos,
3973 bufpos,
3974 it->region_beg_charpos,
3975 it->region_end_charpos,
3976 &next_check_charpos,
3977 base_face_id, 0);
3979 /* Correct the face for charsets different from ASCII. Do it
3980 for the multibyte case only. The face returned above is
3981 suitable for unibyte text if IT->string is unibyte. */
3982 if (STRING_MULTIBYTE (it->string))
3984 struct text_pos pos1 = string_pos (charpos, it->string);
3985 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3986 int c, len;
3987 struct face *face = FACE_FROM_ID (it->f, face_id);
3989 c = string_char_and_length (p, &len);
3990 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3993 else
3995 struct text_pos pos;
3997 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3998 || (IT_CHARPOS (*it) <= BEGV && before_p))
3999 return it->face_id;
4001 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4002 pos = it->current.pos;
4004 if (!it->bidi_p)
4006 if (before_p)
4007 DEC_TEXT_POS (pos, it->multibyte_p);
4008 else
4010 if (it->what == IT_COMPOSITION)
4012 /* For composition, we must check the position after
4013 the composition. */
4014 pos.charpos += it->cmp_it.nchars;
4015 pos.bytepos += it->len;
4017 else
4018 INC_TEXT_POS (pos, it->multibyte_p);
4021 else
4023 if (before_p)
4025 /* With bidi iteration, the character before the current
4026 in the visual order cannot be found by simple
4027 iteration, because "reverse" reordering is not
4028 supported. Instead, we need to use the move_it_*
4029 family of functions. */
4030 /* Ignore face changes before the first visible
4031 character on this display line. */
4032 if (it->current_x <= it->first_visible_x)
4033 return it->face_id;
4034 SAVE_IT (it_copy, *it, it_copy_data);
4035 /* Implementation note: Since move_it_in_display_line
4036 works in the iterator geometry, and thinks the first
4037 character is always the leftmost, even in R2L lines,
4038 we don't need to distinguish between the R2L and L2R
4039 cases here. */
4040 move_it_in_display_line (&it_copy, ZV,
4041 it_copy.current_x - 1, MOVE_TO_X);
4042 pos = it_copy.current.pos;
4043 RESTORE_IT (it, it, it_copy_data);
4045 else
4047 /* Set charpos to the buffer position of the character
4048 that comes after IT's current position in the visual
4049 order. */
4050 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4052 it_copy = *it;
4053 while (n--)
4054 bidi_move_to_visually_next (&it_copy.bidi_it);
4056 SET_TEXT_POS (pos,
4057 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4060 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4062 /* Determine face for CHARSET_ASCII, or unibyte. */
4063 face_id = face_at_buffer_position (it->w,
4064 CHARPOS (pos),
4065 it->region_beg_charpos,
4066 it->region_end_charpos,
4067 &next_check_charpos,
4068 limit, 0, -1);
4070 /* Correct the face for charsets different from ASCII. Do it
4071 for the multibyte case only. The face returned above is
4072 suitable for unibyte text if current_buffer is unibyte. */
4073 if (it->multibyte_p)
4075 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4076 struct face *face = FACE_FROM_ID (it->f, face_id);
4077 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4081 return face_id;
4086 /***********************************************************************
4087 Invisible text
4088 ***********************************************************************/
4090 /* Set up iterator IT from invisible properties at its current
4091 position. Called from handle_stop. */
4093 static enum prop_handled
4094 handle_invisible_prop (struct it *it)
4096 enum prop_handled handled = HANDLED_NORMALLY;
4097 int invis_p;
4098 Lisp_Object prop;
4100 if (STRINGP (it->string))
4102 Lisp_Object end_charpos, limit, charpos;
4104 /* Get the value of the invisible text property at the
4105 current position. Value will be nil if there is no such
4106 property. */
4107 charpos = make_number (IT_STRING_CHARPOS (*it));
4108 prop = Fget_text_property (charpos, Qinvisible, it->string);
4109 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4111 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4113 /* Record whether we have to display an ellipsis for the
4114 invisible text. */
4115 int display_ellipsis_p = (invis_p == 2);
4116 ptrdiff_t len, endpos;
4118 handled = HANDLED_RECOMPUTE_PROPS;
4120 /* Get the position at which the next visible text can be
4121 found in IT->string, if any. */
4122 endpos = len = SCHARS (it->string);
4123 XSETINT (limit, len);
4126 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4127 it->string, limit);
4128 if (INTEGERP (end_charpos))
4130 endpos = XFASTINT (end_charpos);
4131 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4132 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4133 if (invis_p == 2)
4134 display_ellipsis_p = 1;
4137 while (invis_p && endpos < len);
4139 if (display_ellipsis_p)
4140 it->ellipsis_p = 1;
4142 if (endpos < len)
4144 /* Text at END_CHARPOS is visible. Move IT there. */
4145 struct text_pos old;
4146 ptrdiff_t oldpos;
4148 old = it->current.string_pos;
4149 oldpos = CHARPOS (old);
4150 if (it->bidi_p)
4152 if (it->bidi_it.first_elt
4153 && it->bidi_it.charpos < SCHARS (it->string))
4154 bidi_paragraph_init (it->paragraph_embedding,
4155 &it->bidi_it, 1);
4156 /* Bidi-iterate out of the invisible text. */
4159 bidi_move_to_visually_next (&it->bidi_it);
4161 while (oldpos <= it->bidi_it.charpos
4162 && it->bidi_it.charpos < endpos);
4164 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4165 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4166 if (IT_CHARPOS (*it) >= endpos)
4167 it->prev_stop = endpos;
4169 else
4171 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4172 compute_string_pos (&it->current.string_pos, old, it->string);
4175 else
4177 /* The rest of the string is invisible. If this is an
4178 overlay string, proceed with the next overlay string
4179 or whatever comes and return a character from there. */
4180 if (it->current.overlay_string_index >= 0
4181 && !display_ellipsis_p)
4183 next_overlay_string (it);
4184 /* Don't check for overlay strings when we just
4185 finished processing them. */
4186 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4188 else
4190 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4191 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4196 else
4198 ptrdiff_t newpos, next_stop, start_charpos, tem;
4199 Lisp_Object pos, overlay;
4201 /* First of all, is there invisible text at this position? */
4202 tem = start_charpos = IT_CHARPOS (*it);
4203 pos = make_number (tem);
4204 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4205 &overlay);
4206 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4208 /* If we are on invisible text, skip over it. */
4209 if (invis_p && start_charpos < it->end_charpos)
4211 /* Record whether we have to display an ellipsis for the
4212 invisible text. */
4213 int display_ellipsis_p = invis_p == 2;
4215 handled = HANDLED_RECOMPUTE_PROPS;
4217 /* Loop skipping over invisible text. The loop is left at
4218 ZV or with IT on the first char being visible again. */
4221 /* Try to skip some invisible text. Return value is the
4222 position reached which can be equal to where we start
4223 if there is nothing invisible there. This skips both
4224 over invisible text properties and overlays with
4225 invisible property. */
4226 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4228 /* If we skipped nothing at all we weren't at invisible
4229 text in the first place. If everything to the end of
4230 the buffer was skipped, end the loop. */
4231 if (newpos == tem || newpos >= ZV)
4232 invis_p = 0;
4233 else
4235 /* We skipped some characters but not necessarily
4236 all there are. Check if we ended up on visible
4237 text. Fget_char_property returns the property of
4238 the char before the given position, i.e. if we
4239 get invis_p = 0, this means that the char at
4240 newpos is visible. */
4241 pos = make_number (newpos);
4242 prop = Fget_char_property (pos, Qinvisible, it->window);
4243 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4246 /* If we ended up on invisible text, proceed to
4247 skip starting with next_stop. */
4248 if (invis_p)
4249 tem = next_stop;
4251 /* If there are adjacent invisible texts, don't lose the
4252 second one's ellipsis. */
4253 if (invis_p == 2)
4254 display_ellipsis_p = 1;
4256 while (invis_p);
4258 /* The position newpos is now either ZV or on visible text. */
4259 if (it->bidi_p)
4261 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4262 int on_newline =
4263 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4264 int after_newline =
4265 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4267 /* If the invisible text ends on a newline or on a
4268 character after a newline, we can avoid the costly,
4269 character by character, bidi iteration to NEWPOS, and
4270 instead simply reseat the iterator there. That's
4271 because all bidi reordering information is tossed at
4272 the newline. This is a big win for modes that hide
4273 complete lines, like Outline, Org, etc. */
4274 if (on_newline || after_newline)
4276 struct text_pos tpos;
4277 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4279 SET_TEXT_POS (tpos, newpos, bpos);
4280 reseat_1 (it, tpos, 0);
4281 /* If we reseat on a newline/ZV, we need to prep the
4282 bidi iterator for advancing to the next character
4283 after the newline/EOB, keeping the current paragraph
4284 direction (so that PRODUCE_GLYPHS does TRT wrt
4285 prepending/appending glyphs to a glyph row). */
4286 if (on_newline)
4288 it->bidi_it.first_elt = 0;
4289 it->bidi_it.paragraph_dir = pdir;
4290 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4291 it->bidi_it.nchars = 1;
4292 it->bidi_it.ch_len = 1;
4295 else /* Must use the slow method. */
4297 /* With bidi iteration, the region of invisible text
4298 could start and/or end in the middle of a
4299 non-base embedding level. Therefore, we need to
4300 skip invisible text using the bidi iterator,
4301 starting at IT's current position, until we find
4302 ourselves outside of the invisible text.
4303 Skipping invisible text _after_ bidi iteration
4304 avoids affecting the visual order of the
4305 displayed text when invisible properties are
4306 added or removed. */
4307 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4309 /* If we were `reseat'ed to a new paragraph,
4310 determine the paragraph base direction. We
4311 need to do it now because
4312 next_element_from_buffer may not have a
4313 chance to do it, if we are going to skip any
4314 text at the beginning, which resets the
4315 FIRST_ELT flag. */
4316 bidi_paragraph_init (it->paragraph_embedding,
4317 &it->bidi_it, 1);
4321 bidi_move_to_visually_next (&it->bidi_it);
4323 while (it->stop_charpos <= it->bidi_it.charpos
4324 && it->bidi_it.charpos < newpos);
4325 IT_CHARPOS (*it) = it->bidi_it.charpos;
4326 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4327 /* If we overstepped NEWPOS, record its position in
4328 the iterator, so that we skip invisible text if
4329 later the bidi iteration lands us in the
4330 invisible region again. */
4331 if (IT_CHARPOS (*it) >= newpos)
4332 it->prev_stop = newpos;
4335 else
4337 IT_CHARPOS (*it) = newpos;
4338 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4341 /* If there are before-strings at the start of invisible
4342 text, and the text is invisible because of a text
4343 property, arrange to show before-strings because 20.x did
4344 it that way. (If the text is invisible because of an
4345 overlay property instead of a text property, this is
4346 already handled in the overlay code.) */
4347 if (NILP (overlay)
4348 && get_overlay_strings (it, it->stop_charpos))
4350 handled = HANDLED_RECOMPUTE_PROPS;
4351 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4353 else if (display_ellipsis_p)
4355 /* Make sure that the glyphs of the ellipsis will get
4356 correct `charpos' values. If we would not update
4357 it->position here, the glyphs would belong to the
4358 last visible character _before_ the invisible
4359 text, which confuses `set_cursor_from_row'.
4361 We use the last invisible position instead of the
4362 first because this way the cursor is always drawn on
4363 the first "." of the ellipsis, whenever PT is inside
4364 the invisible text. Otherwise the cursor would be
4365 placed _after_ the ellipsis when the point is after the
4366 first invisible character. */
4367 if (!STRINGP (it->object))
4369 it->position.charpos = newpos - 1;
4370 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4372 it->ellipsis_p = 1;
4373 /* Let the ellipsis display before
4374 considering any properties of the following char.
4375 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4376 handled = HANDLED_RETURN;
4381 return handled;
4385 /* Make iterator IT return `...' next.
4386 Replaces LEN characters from buffer. */
4388 static void
4389 setup_for_ellipsis (struct it *it, int len)
4391 /* Use the display table definition for `...'. Invalid glyphs
4392 will be handled by the method returning elements from dpvec. */
4393 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4395 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4396 it->dpvec = v->contents;
4397 it->dpend = v->contents + v->header.size;
4399 else
4401 /* Default `...'. */
4402 it->dpvec = default_invis_vector;
4403 it->dpend = default_invis_vector + 3;
4406 it->dpvec_char_len = len;
4407 it->current.dpvec_index = 0;
4408 it->dpvec_face_id = -1;
4410 /* Remember the current face id in case glyphs specify faces.
4411 IT's face is restored in set_iterator_to_next.
4412 saved_face_id was set to preceding char's face in handle_stop. */
4413 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4414 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4416 it->method = GET_FROM_DISPLAY_VECTOR;
4417 it->ellipsis_p = 1;
4422 /***********************************************************************
4423 'display' property
4424 ***********************************************************************/
4426 /* Set up iterator IT from `display' property at its current position.
4427 Called from handle_stop.
4428 We return HANDLED_RETURN if some part of the display property
4429 overrides the display of the buffer text itself.
4430 Otherwise we return HANDLED_NORMALLY. */
4432 static enum prop_handled
4433 handle_display_prop (struct it *it)
4435 Lisp_Object propval, object, overlay;
4436 struct text_pos *position;
4437 ptrdiff_t bufpos;
4438 /* Nonzero if some property replaces the display of the text itself. */
4439 int display_replaced_p = 0;
4441 if (STRINGP (it->string))
4443 object = it->string;
4444 position = &it->current.string_pos;
4445 bufpos = CHARPOS (it->current.pos);
4447 else
4449 XSETWINDOW (object, it->w);
4450 position = &it->current.pos;
4451 bufpos = CHARPOS (*position);
4454 /* Reset those iterator values set from display property values. */
4455 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4456 it->space_width = Qnil;
4457 it->font_height = Qnil;
4458 it->voffset = 0;
4460 /* We don't support recursive `display' properties, i.e. string
4461 values that have a string `display' property, that have a string
4462 `display' property etc. */
4463 if (!it->string_from_display_prop_p)
4464 it->area = TEXT_AREA;
4466 propval = get_char_property_and_overlay (make_number (position->charpos),
4467 Qdisplay, object, &overlay);
4468 if (NILP (propval))
4469 return HANDLED_NORMALLY;
4470 /* Now OVERLAY is the overlay that gave us this property, or nil
4471 if it was a text property. */
4473 if (!STRINGP (it->string))
4474 object = it->w->contents;
4476 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4477 position, bufpos,
4478 FRAME_WINDOW_P (it->f));
4480 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4483 /* Subroutine of handle_display_prop. Returns non-zero if the display
4484 specification in SPEC is a replacing specification, i.e. it would
4485 replace the text covered by `display' property with something else,
4486 such as an image or a display string. If SPEC includes any kind or
4487 `(space ...) specification, the value is 2; this is used by
4488 compute_display_string_pos, which see.
4490 See handle_single_display_spec for documentation of arguments.
4491 frame_window_p is non-zero if the window being redisplayed is on a
4492 GUI frame; this argument is used only if IT is NULL, see below.
4494 IT can be NULL, if this is called by the bidi reordering code
4495 through compute_display_string_pos, which see. In that case, this
4496 function only examines SPEC, but does not otherwise "handle" it, in
4497 the sense that it doesn't set up members of IT from the display
4498 spec. */
4499 static int
4500 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4501 Lisp_Object overlay, struct text_pos *position,
4502 ptrdiff_t bufpos, int frame_window_p)
4504 int replacing_p = 0;
4505 int rv;
4507 if (CONSP (spec)
4508 /* Simple specifications. */
4509 && !EQ (XCAR (spec), Qimage)
4510 && !EQ (XCAR (spec), Qspace)
4511 && !EQ (XCAR (spec), Qwhen)
4512 && !EQ (XCAR (spec), Qslice)
4513 && !EQ (XCAR (spec), Qspace_width)
4514 && !EQ (XCAR (spec), Qheight)
4515 && !EQ (XCAR (spec), Qraise)
4516 /* Marginal area specifications. */
4517 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4518 && !EQ (XCAR (spec), Qleft_fringe)
4519 && !EQ (XCAR (spec), Qright_fringe)
4520 && !NILP (XCAR (spec)))
4522 for (; CONSP (spec); spec = XCDR (spec))
4524 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4525 overlay, position, bufpos,
4526 replacing_p, frame_window_p)))
4528 replacing_p = rv;
4529 /* If some text in a string is replaced, `position' no
4530 longer points to the position of `object'. */
4531 if (!it || STRINGP (object))
4532 break;
4536 else if (VECTORP (spec))
4538 ptrdiff_t i;
4539 for (i = 0; i < ASIZE (spec); ++i)
4540 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4541 overlay, position, bufpos,
4542 replacing_p, frame_window_p)))
4544 replacing_p = rv;
4545 /* If some text in a string is replaced, `position' no
4546 longer points to the position of `object'. */
4547 if (!it || STRINGP (object))
4548 break;
4551 else
4553 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4554 position, bufpos, 0,
4555 frame_window_p)))
4556 replacing_p = rv;
4559 return replacing_p;
4562 /* Value is the position of the end of the `display' property starting
4563 at START_POS in OBJECT. */
4565 static struct text_pos
4566 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4568 Lisp_Object end;
4569 struct text_pos end_pos;
4571 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4572 Qdisplay, object, Qnil);
4573 CHARPOS (end_pos) = XFASTINT (end);
4574 if (STRINGP (object))
4575 compute_string_pos (&end_pos, start_pos, it->string);
4576 else
4577 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4579 return end_pos;
4583 /* Set up IT from a single `display' property specification SPEC. OBJECT
4584 is the object in which the `display' property was found. *POSITION
4585 is the position in OBJECT at which the `display' property was found.
4586 BUFPOS is the buffer position of OBJECT (different from POSITION if
4587 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4588 previously saw a display specification which already replaced text
4589 display with something else, for example an image; we ignore such
4590 properties after the first one has been processed.
4592 OVERLAY is the overlay this `display' property came from,
4593 or nil if it was a text property.
4595 If SPEC is a `space' or `image' specification, and in some other
4596 cases too, set *POSITION to the position where the `display'
4597 property ends.
4599 If IT is NULL, only examine the property specification in SPEC, but
4600 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4601 is intended to be displayed in a window on a GUI frame.
4603 Value is non-zero if something was found which replaces the display
4604 of buffer or string text. */
4606 static int
4607 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4608 Lisp_Object overlay, struct text_pos *position,
4609 ptrdiff_t bufpos, int display_replaced_p,
4610 int frame_window_p)
4612 Lisp_Object form;
4613 Lisp_Object location, value;
4614 struct text_pos start_pos = *position;
4615 int valid_p;
4617 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4618 If the result is non-nil, use VALUE instead of SPEC. */
4619 form = Qt;
4620 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4622 spec = XCDR (spec);
4623 if (!CONSP (spec))
4624 return 0;
4625 form = XCAR (spec);
4626 spec = XCDR (spec);
4629 if (!NILP (form) && !EQ (form, Qt))
4631 ptrdiff_t count = SPECPDL_INDEX ();
4632 struct gcpro gcpro1;
4634 /* Bind `object' to the object having the `display' property, a
4635 buffer or string. Bind `position' to the position in the
4636 object where the property was found, and `buffer-position'
4637 to the current position in the buffer. */
4639 if (NILP (object))
4640 XSETBUFFER (object, current_buffer);
4641 specbind (Qobject, object);
4642 specbind (Qposition, make_number (CHARPOS (*position)));
4643 specbind (Qbuffer_position, make_number (bufpos));
4644 GCPRO1 (form);
4645 form = safe_eval (form);
4646 UNGCPRO;
4647 unbind_to (count, Qnil);
4650 if (NILP (form))
4651 return 0;
4653 /* Handle `(height HEIGHT)' specifications. */
4654 if (CONSP (spec)
4655 && EQ (XCAR (spec), Qheight)
4656 && CONSP (XCDR (spec)))
4658 if (it)
4660 if (!FRAME_WINDOW_P (it->f))
4661 return 0;
4663 it->font_height = XCAR (XCDR (spec));
4664 if (!NILP (it->font_height))
4666 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4667 int new_height = -1;
4669 if (CONSP (it->font_height)
4670 && (EQ (XCAR (it->font_height), Qplus)
4671 || EQ (XCAR (it->font_height), Qminus))
4672 && CONSP (XCDR (it->font_height))
4673 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4675 /* `(+ N)' or `(- N)' where N is an integer. */
4676 int steps = XINT (XCAR (XCDR (it->font_height)));
4677 if (EQ (XCAR (it->font_height), Qplus))
4678 steps = - steps;
4679 it->face_id = smaller_face (it->f, it->face_id, steps);
4681 else if (FUNCTIONP (it->font_height))
4683 /* Call function with current height as argument.
4684 Value is the new height. */
4685 Lisp_Object height;
4686 height = safe_call1 (it->font_height,
4687 face->lface[LFACE_HEIGHT_INDEX]);
4688 if (NUMBERP (height))
4689 new_height = XFLOATINT (height);
4691 else if (NUMBERP (it->font_height))
4693 /* Value is a multiple of the canonical char height. */
4694 struct face *f;
4696 f = FACE_FROM_ID (it->f,
4697 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4698 new_height = (XFLOATINT (it->font_height)
4699 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4701 else
4703 /* Evaluate IT->font_height with `height' bound to the
4704 current specified height to get the new height. */
4705 ptrdiff_t count = SPECPDL_INDEX ();
4707 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4708 value = safe_eval (it->font_height);
4709 unbind_to (count, Qnil);
4711 if (NUMBERP (value))
4712 new_height = XFLOATINT (value);
4715 if (new_height > 0)
4716 it->face_id = face_with_height (it->f, it->face_id, new_height);
4720 return 0;
4723 /* Handle `(space-width WIDTH)'. */
4724 if (CONSP (spec)
4725 && EQ (XCAR (spec), Qspace_width)
4726 && CONSP (XCDR (spec)))
4728 if (it)
4730 if (!FRAME_WINDOW_P (it->f))
4731 return 0;
4733 value = XCAR (XCDR (spec));
4734 if (NUMBERP (value) && XFLOATINT (value) > 0)
4735 it->space_width = value;
4738 return 0;
4741 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4742 if (CONSP (spec)
4743 && EQ (XCAR (spec), Qslice))
4745 Lisp_Object tem;
4747 if (it)
4749 if (!FRAME_WINDOW_P (it->f))
4750 return 0;
4752 if (tem = XCDR (spec), CONSP (tem))
4754 it->slice.x = XCAR (tem);
4755 if (tem = XCDR (tem), CONSP (tem))
4757 it->slice.y = XCAR (tem);
4758 if (tem = XCDR (tem), CONSP (tem))
4760 it->slice.width = XCAR (tem);
4761 if (tem = XCDR (tem), CONSP (tem))
4762 it->slice.height = XCAR (tem);
4768 return 0;
4771 /* Handle `(raise FACTOR)'. */
4772 if (CONSP (spec)
4773 && EQ (XCAR (spec), Qraise)
4774 && CONSP (XCDR (spec)))
4776 if (it)
4778 if (!FRAME_WINDOW_P (it->f))
4779 return 0;
4781 #ifdef HAVE_WINDOW_SYSTEM
4782 value = XCAR (XCDR (spec));
4783 if (NUMBERP (value))
4785 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4786 it->voffset = - (XFLOATINT (value)
4787 * (FONT_HEIGHT (face->font)));
4789 #endif /* HAVE_WINDOW_SYSTEM */
4792 return 0;
4795 /* Don't handle the other kinds of display specifications
4796 inside a string that we got from a `display' property. */
4797 if (it && it->string_from_display_prop_p)
4798 return 0;
4800 /* Characters having this form of property are not displayed, so
4801 we have to find the end of the property. */
4802 if (it)
4804 start_pos = *position;
4805 *position = display_prop_end (it, object, start_pos);
4807 value = Qnil;
4809 /* Stop the scan at that end position--we assume that all
4810 text properties change there. */
4811 if (it)
4812 it->stop_charpos = position->charpos;
4814 /* Handle `(left-fringe BITMAP [FACE])'
4815 and `(right-fringe BITMAP [FACE])'. */
4816 if (CONSP (spec)
4817 && (EQ (XCAR (spec), Qleft_fringe)
4818 || EQ (XCAR (spec), Qright_fringe))
4819 && CONSP (XCDR (spec)))
4821 int fringe_bitmap;
4823 if (it)
4825 if (!FRAME_WINDOW_P (it->f))
4826 /* If we return here, POSITION has been advanced
4827 across the text with this property. */
4829 /* Synchronize the bidi iterator with POSITION. This is
4830 needed because we are not going to push the iterator
4831 on behalf of this display property, so there will be
4832 no pop_it call to do this synchronization for us. */
4833 if (it->bidi_p)
4835 it->position = *position;
4836 iterate_out_of_display_property (it);
4837 *position = it->position;
4839 return 1;
4842 else if (!frame_window_p)
4843 return 1;
4845 #ifdef HAVE_WINDOW_SYSTEM
4846 value = XCAR (XCDR (spec));
4847 if (!SYMBOLP (value)
4848 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4849 /* If we return here, POSITION has been advanced
4850 across the text with this property. */
4852 if (it && it->bidi_p)
4854 it->position = *position;
4855 iterate_out_of_display_property (it);
4856 *position = it->position;
4858 return 1;
4861 if (it)
4863 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4865 if (CONSP (XCDR (XCDR (spec))))
4867 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4868 int face_id2 = lookup_derived_face (it->f, face_name,
4869 FRINGE_FACE_ID, 0);
4870 if (face_id2 >= 0)
4871 face_id = face_id2;
4874 /* Save current settings of IT so that we can restore them
4875 when we are finished with the glyph property value. */
4876 push_it (it, position);
4878 it->area = TEXT_AREA;
4879 it->what = IT_IMAGE;
4880 it->image_id = -1; /* no image */
4881 it->position = start_pos;
4882 it->object = NILP (object) ? it->w->contents : object;
4883 it->method = GET_FROM_IMAGE;
4884 it->from_overlay = Qnil;
4885 it->face_id = face_id;
4886 it->from_disp_prop_p = 1;
4888 /* Say that we haven't consumed the characters with
4889 `display' property yet. The call to pop_it in
4890 set_iterator_to_next will clean this up. */
4891 *position = start_pos;
4893 if (EQ (XCAR (spec), Qleft_fringe))
4895 it->left_user_fringe_bitmap = fringe_bitmap;
4896 it->left_user_fringe_face_id = face_id;
4898 else
4900 it->right_user_fringe_bitmap = fringe_bitmap;
4901 it->right_user_fringe_face_id = face_id;
4904 #endif /* HAVE_WINDOW_SYSTEM */
4905 return 1;
4908 /* Prepare to handle `((margin left-margin) ...)',
4909 `((margin right-margin) ...)' and `((margin nil) ...)'
4910 prefixes for display specifications. */
4911 location = Qunbound;
4912 if (CONSP (spec) && CONSP (XCAR (spec)))
4914 Lisp_Object tem;
4916 value = XCDR (spec);
4917 if (CONSP (value))
4918 value = XCAR (value);
4920 tem = XCAR (spec);
4921 if (EQ (XCAR (tem), Qmargin)
4922 && (tem = XCDR (tem),
4923 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4924 (NILP (tem)
4925 || EQ (tem, Qleft_margin)
4926 || EQ (tem, Qright_margin))))
4927 location = tem;
4930 if (EQ (location, Qunbound))
4932 location = Qnil;
4933 value = spec;
4936 /* After this point, VALUE is the property after any
4937 margin prefix has been stripped. It must be a string,
4938 an image specification, or `(space ...)'.
4940 LOCATION specifies where to display: `left-margin',
4941 `right-margin' or nil. */
4943 valid_p = (STRINGP (value)
4944 #ifdef HAVE_WINDOW_SYSTEM
4945 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4946 && valid_image_p (value))
4947 #endif /* not HAVE_WINDOW_SYSTEM */
4948 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4950 if (valid_p && !display_replaced_p)
4952 int retval = 1;
4954 if (!it)
4956 /* Callers need to know whether the display spec is any kind
4957 of `(space ...)' spec that is about to affect text-area
4958 display. */
4959 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4960 retval = 2;
4961 return retval;
4964 /* Save current settings of IT so that we can restore them
4965 when we are finished with the glyph property value. */
4966 push_it (it, position);
4967 it->from_overlay = overlay;
4968 it->from_disp_prop_p = 1;
4970 if (NILP (location))
4971 it->area = TEXT_AREA;
4972 else if (EQ (location, Qleft_margin))
4973 it->area = LEFT_MARGIN_AREA;
4974 else
4975 it->area = RIGHT_MARGIN_AREA;
4977 if (STRINGP (value))
4979 it->string = value;
4980 it->multibyte_p = STRING_MULTIBYTE (it->string);
4981 it->current.overlay_string_index = -1;
4982 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4983 it->end_charpos = it->string_nchars = SCHARS (it->string);
4984 it->method = GET_FROM_STRING;
4985 it->stop_charpos = 0;
4986 it->prev_stop = 0;
4987 it->base_level_stop = 0;
4988 it->string_from_display_prop_p = 1;
4989 /* Say that we haven't consumed the characters with
4990 `display' property yet. The call to pop_it in
4991 set_iterator_to_next will clean this up. */
4992 if (BUFFERP (object))
4993 *position = start_pos;
4995 /* Force paragraph direction to be that of the parent
4996 object. If the parent object's paragraph direction is
4997 not yet determined, default to L2R. */
4998 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4999 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5000 else
5001 it->paragraph_embedding = L2R;
5003 /* Set up the bidi iterator for this display string. */
5004 if (it->bidi_p)
5006 it->bidi_it.string.lstring = it->string;
5007 it->bidi_it.string.s = NULL;
5008 it->bidi_it.string.schars = it->end_charpos;
5009 it->bidi_it.string.bufpos = bufpos;
5010 it->bidi_it.string.from_disp_str = 1;
5011 it->bidi_it.string.unibyte = !it->multibyte_p;
5012 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5015 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5017 it->method = GET_FROM_STRETCH;
5018 it->object = value;
5019 *position = it->position = start_pos;
5020 retval = 1 + (it->area == TEXT_AREA);
5022 #ifdef HAVE_WINDOW_SYSTEM
5023 else
5025 it->what = IT_IMAGE;
5026 it->image_id = lookup_image (it->f, value);
5027 it->position = start_pos;
5028 it->object = NILP (object) ? it->w->contents : object;
5029 it->method = GET_FROM_IMAGE;
5031 /* Say that we haven't consumed the characters with
5032 `display' property yet. The call to pop_it in
5033 set_iterator_to_next will clean this up. */
5034 *position = start_pos;
5036 #endif /* HAVE_WINDOW_SYSTEM */
5038 return retval;
5041 /* Invalid property or property not supported. Restore
5042 POSITION to what it was before. */
5043 *position = start_pos;
5044 return 0;
5047 /* Check if PROP is a display property value whose text should be
5048 treated as intangible. OVERLAY is the overlay from which PROP
5049 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5050 specify the buffer position covered by PROP. */
5053 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5054 ptrdiff_t charpos, ptrdiff_t bytepos)
5056 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5057 struct text_pos position;
5059 SET_TEXT_POS (position, charpos, bytepos);
5060 return handle_display_spec (NULL, prop, Qnil, overlay,
5061 &position, charpos, frame_window_p);
5065 /* Return 1 if PROP is a display sub-property value containing STRING.
5067 Implementation note: this and the following function are really
5068 special cases of handle_display_spec and
5069 handle_single_display_spec, and should ideally use the same code.
5070 Until they do, these two pairs must be consistent and must be
5071 modified in sync. */
5073 static int
5074 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5076 if (EQ (string, prop))
5077 return 1;
5079 /* Skip over `when FORM'. */
5080 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5082 prop = XCDR (prop);
5083 if (!CONSP (prop))
5084 return 0;
5085 /* Actually, the condition following `when' should be eval'ed,
5086 like handle_single_display_spec does, and we should return
5087 zero if it evaluates to nil. However, this function is
5088 called only when the buffer was already displayed and some
5089 glyph in the glyph matrix was found to come from a display
5090 string. Therefore, the condition was already evaluated, and
5091 the result was non-nil, otherwise the display string wouldn't
5092 have been displayed and we would have never been called for
5093 this property. Thus, we can skip the evaluation and assume
5094 its result is non-nil. */
5095 prop = XCDR (prop);
5098 if (CONSP (prop))
5099 /* Skip over `margin LOCATION'. */
5100 if (EQ (XCAR (prop), Qmargin))
5102 prop = XCDR (prop);
5103 if (!CONSP (prop))
5104 return 0;
5106 prop = XCDR (prop);
5107 if (!CONSP (prop))
5108 return 0;
5111 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5115 /* Return 1 if STRING appears in the `display' property PROP. */
5117 static int
5118 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5120 if (CONSP (prop)
5121 && !EQ (XCAR (prop), Qwhen)
5122 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5124 /* A list of sub-properties. */
5125 while (CONSP (prop))
5127 if (single_display_spec_string_p (XCAR (prop), string))
5128 return 1;
5129 prop = XCDR (prop);
5132 else if (VECTORP (prop))
5134 /* A vector of sub-properties. */
5135 ptrdiff_t i;
5136 for (i = 0; i < ASIZE (prop); ++i)
5137 if (single_display_spec_string_p (AREF (prop, i), string))
5138 return 1;
5140 else
5141 return single_display_spec_string_p (prop, string);
5143 return 0;
5146 /* Look for STRING in overlays and text properties in the current
5147 buffer, between character positions FROM and TO (excluding TO).
5148 BACK_P non-zero means look back (in this case, TO is supposed to be
5149 less than FROM).
5150 Value is the first character position where STRING was found, or
5151 zero if it wasn't found before hitting TO.
5153 This function may only use code that doesn't eval because it is
5154 called asynchronously from note_mouse_highlight. */
5156 static ptrdiff_t
5157 string_buffer_position_lim (Lisp_Object string,
5158 ptrdiff_t from, ptrdiff_t to, int back_p)
5160 Lisp_Object limit, prop, pos;
5161 int found = 0;
5163 pos = make_number (max (from, BEGV));
5165 if (!back_p) /* looking forward */
5167 limit = make_number (min (to, ZV));
5168 while (!found && !EQ (pos, limit))
5170 prop = Fget_char_property (pos, Qdisplay, Qnil);
5171 if (!NILP (prop) && display_prop_string_p (prop, string))
5172 found = 1;
5173 else
5174 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5175 limit);
5178 else /* looking back */
5180 limit = make_number (max (to, BEGV));
5181 while (!found && !EQ (pos, limit))
5183 prop = Fget_char_property (pos, Qdisplay, Qnil);
5184 if (!NILP (prop) && display_prop_string_p (prop, string))
5185 found = 1;
5186 else
5187 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5188 limit);
5192 return found ? XINT (pos) : 0;
5195 /* Determine which buffer position in current buffer STRING comes from.
5196 AROUND_CHARPOS is an approximate position where it could come from.
5197 Value is the buffer position or 0 if it couldn't be determined.
5199 This function is necessary because we don't record buffer positions
5200 in glyphs generated from strings (to keep struct glyph small).
5201 This function may only use code that doesn't eval because it is
5202 called asynchronously from note_mouse_highlight. */
5204 static ptrdiff_t
5205 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5207 const int MAX_DISTANCE = 1000;
5208 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5209 around_charpos + MAX_DISTANCE,
5212 if (!found)
5213 found = string_buffer_position_lim (string, around_charpos,
5214 around_charpos - MAX_DISTANCE, 1);
5215 return found;
5220 /***********************************************************************
5221 `composition' property
5222 ***********************************************************************/
5224 /* Set up iterator IT from `composition' property at its current
5225 position. Called from handle_stop. */
5227 static enum prop_handled
5228 handle_composition_prop (struct it *it)
5230 Lisp_Object prop, string;
5231 ptrdiff_t pos, pos_byte, start, end;
5233 if (STRINGP (it->string))
5235 unsigned char *s;
5237 pos = IT_STRING_CHARPOS (*it);
5238 pos_byte = IT_STRING_BYTEPOS (*it);
5239 string = it->string;
5240 s = SDATA (string) + pos_byte;
5241 it->c = STRING_CHAR (s);
5243 else
5245 pos = IT_CHARPOS (*it);
5246 pos_byte = IT_BYTEPOS (*it);
5247 string = Qnil;
5248 it->c = FETCH_CHAR (pos_byte);
5251 /* If there's a valid composition and point is not inside of the
5252 composition (in the case that the composition is from the current
5253 buffer), draw a glyph composed from the composition components. */
5254 if (find_composition (pos, -1, &start, &end, &prop, string)
5255 && COMPOSITION_VALID_P (start, end, prop)
5256 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5258 if (start < pos)
5259 /* As we can't handle this situation (perhaps font-lock added
5260 a new composition), we just return here hoping that next
5261 redisplay will detect this composition much earlier. */
5262 return HANDLED_NORMALLY;
5263 if (start != pos)
5265 if (STRINGP (it->string))
5266 pos_byte = string_char_to_byte (it->string, start);
5267 else
5268 pos_byte = CHAR_TO_BYTE (start);
5270 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5271 prop, string);
5273 if (it->cmp_it.id >= 0)
5275 it->cmp_it.ch = -1;
5276 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5277 it->cmp_it.nglyphs = -1;
5281 return HANDLED_NORMALLY;
5286 /***********************************************************************
5287 Overlay strings
5288 ***********************************************************************/
5290 /* The following structure is used to record overlay strings for
5291 later sorting in load_overlay_strings. */
5293 struct overlay_entry
5295 Lisp_Object overlay;
5296 Lisp_Object string;
5297 EMACS_INT priority;
5298 int after_string_p;
5302 /* Set up iterator IT from overlay strings at its current position.
5303 Called from handle_stop. */
5305 static enum prop_handled
5306 handle_overlay_change (struct it *it)
5308 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5309 return HANDLED_RECOMPUTE_PROPS;
5310 else
5311 return HANDLED_NORMALLY;
5315 /* Set up the next overlay string for delivery by IT, if there is an
5316 overlay string to deliver. Called by set_iterator_to_next when the
5317 end of the current overlay string is reached. If there are more
5318 overlay strings to display, IT->string and
5319 IT->current.overlay_string_index are set appropriately here.
5320 Otherwise IT->string is set to nil. */
5322 static void
5323 next_overlay_string (struct it *it)
5325 ++it->current.overlay_string_index;
5326 if (it->current.overlay_string_index == it->n_overlay_strings)
5328 /* No more overlay strings. Restore IT's settings to what
5329 they were before overlay strings were processed, and
5330 continue to deliver from current_buffer. */
5332 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5333 pop_it (it);
5334 eassert (it->sp > 0
5335 || (NILP (it->string)
5336 && it->method == GET_FROM_BUFFER
5337 && it->stop_charpos >= BEGV
5338 && it->stop_charpos <= it->end_charpos));
5339 it->current.overlay_string_index = -1;
5340 it->n_overlay_strings = 0;
5341 it->overlay_strings_charpos = -1;
5342 /* If there's an empty display string on the stack, pop the
5343 stack, to resync the bidi iterator with IT's position. Such
5344 empty strings are pushed onto the stack in
5345 get_overlay_strings_1. */
5346 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5347 pop_it (it);
5349 /* If we're at the end of the buffer, record that we have
5350 processed the overlay strings there already, so that
5351 next_element_from_buffer doesn't try it again. */
5352 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5353 it->overlay_strings_at_end_processed_p = 1;
5355 else
5357 /* There are more overlay strings to process. If
5358 IT->current.overlay_string_index has advanced to a position
5359 where we must load IT->overlay_strings with more strings, do
5360 it. We must load at the IT->overlay_strings_charpos where
5361 IT->n_overlay_strings was originally computed; when invisible
5362 text is present, this might not be IT_CHARPOS (Bug#7016). */
5363 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5365 if (it->current.overlay_string_index && i == 0)
5366 load_overlay_strings (it, it->overlay_strings_charpos);
5368 /* Initialize IT to deliver display elements from the overlay
5369 string. */
5370 it->string = it->overlay_strings[i];
5371 it->multibyte_p = STRING_MULTIBYTE (it->string);
5372 SET_TEXT_POS (it->current.string_pos, 0, 0);
5373 it->method = GET_FROM_STRING;
5374 it->stop_charpos = 0;
5375 it->end_charpos = SCHARS (it->string);
5376 if (it->cmp_it.stop_pos >= 0)
5377 it->cmp_it.stop_pos = 0;
5378 it->prev_stop = 0;
5379 it->base_level_stop = 0;
5381 /* Set up the bidi iterator for this overlay string. */
5382 if (it->bidi_p)
5384 it->bidi_it.string.lstring = it->string;
5385 it->bidi_it.string.s = NULL;
5386 it->bidi_it.string.schars = SCHARS (it->string);
5387 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5388 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5389 it->bidi_it.string.unibyte = !it->multibyte_p;
5390 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5394 CHECK_IT (it);
5398 /* Compare two overlay_entry structures E1 and E2. Used as a
5399 comparison function for qsort in load_overlay_strings. Overlay
5400 strings for the same position are sorted so that
5402 1. All after-strings come in front of before-strings, except
5403 when they come from the same overlay.
5405 2. Within after-strings, strings are sorted so that overlay strings
5406 from overlays with higher priorities come first.
5408 2. Within before-strings, strings are sorted so that overlay
5409 strings from overlays with higher priorities come last.
5411 Value is analogous to strcmp. */
5414 static int
5415 compare_overlay_entries (const void *e1, const void *e2)
5417 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5418 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5419 int result;
5421 if (entry1->after_string_p != entry2->after_string_p)
5423 /* Let after-strings appear in front of before-strings if
5424 they come from different overlays. */
5425 if (EQ (entry1->overlay, entry2->overlay))
5426 result = entry1->after_string_p ? 1 : -1;
5427 else
5428 result = entry1->after_string_p ? -1 : 1;
5430 else if (entry1->priority != entry2->priority)
5432 if (entry1->after_string_p)
5433 /* After-strings sorted in order of decreasing priority. */
5434 result = entry2->priority < entry1->priority ? -1 : 1;
5435 else
5436 /* Before-strings sorted in order of increasing priority. */
5437 result = entry1->priority < entry2->priority ? -1 : 1;
5439 else
5440 result = 0;
5442 return result;
5446 /* Load the vector IT->overlay_strings with overlay strings from IT's
5447 current buffer position, or from CHARPOS if that is > 0. Set
5448 IT->n_overlays to the total number of overlay strings found.
5450 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5451 a time. On entry into load_overlay_strings,
5452 IT->current.overlay_string_index gives the number of overlay
5453 strings that have already been loaded by previous calls to this
5454 function.
5456 IT->add_overlay_start contains an additional overlay start
5457 position to consider for taking overlay strings from, if non-zero.
5458 This position comes into play when the overlay has an `invisible'
5459 property, and both before and after-strings. When we've skipped to
5460 the end of the overlay, because of its `invisible' property, we
5461 nevertheless want its before-string to appear.
5462 IT->add_overlay_start will contain the overlay start position
5463 in this case.
5465 Overlay strings are sorted so that after-string strings come in
5466 front of before-string strings. Within before and after-strings,
5467 strings are sorted by overlay priority. See also function
5468 compare_overlay_entries. */
5470 static void
5471 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5473 Lisp_Object overlay, window, str, invisible;
5474 struct Lisp_Overlay *ov;
5475 ptrdiff_t start, end;
5476 ptrdiff_t size = 20;
5477 ptrdiff_t n = 0, i, j;
5478 int invis_p;
5479 struct overlay_entry *entries = alloca (size * sizeof *entries);
5480 USE_SAFE_ALLOCA;
5482 if (charpos <= 0)
5483 charpos = IT_CHARPOS (*it);
5485 /* Append the overlay string STRING of overlay OVERLAY to vector
5486 `entries' which has size `size' and currently contains `n'
5487 elements. AFTER_P non-zero means STRING is an after-string of
5488 OVERLAY. */
5489 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5490 do \
5492 Lisp_Object priority; \
5494 if (n == size) \
5496 struct overlay_entry *old = entries; \
5497 SAFE_NALLOCA (entries, 2, size); \
5498 memcpy (entries, old, size * sizeof *entries); \
5499 size *= 2; \
5502 entries[n].string = (STRING); \
5503 entries[n].overlay = (OVERLAY); \
5504 priority = Foverlay_get ((OVERLAY), Qpriority); \
5505 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5506 entries[n].after_string_p = (AFTER_P); \
5507 ++n; \
5509 while (0)
5511 /* Process overlay before the overlay center. */
5512 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5514 XSETMISC (overlay, ov);
5515 eassert (OVERLAYP (overlay));
5516 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5517 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5519 if (end < charpos)
5520 break;
5522 /* Skip this overlay if it doesn't start or end at IT's current
5523 position. */
5524 if (end != charpos && start != charpos)
5525 continue;
5527 /* Skip this overlay if it doesn't apply to IT->w. */
5528 window = Foverlay_get (overlay, Qwindow);
5529 if (WINDOWP (window) && XWINDOW (window) != it->w)
5530 continue;
5532 /* If the text ``under'' the overlay is invisible, both before-
5533 and after-strings from this overlay are visible; start and
5534 end position are indistinguishable. */
5535 invisible = Foverlay_get (overlay, Qinvisible);
5536 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5538 /* If overlay has a non-empty before-string, record it. */
5539 if ((start == charpos || (end == charpos && invis_p))
5540 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5541 && SCHARS (str))
5542 RECORD_OVERLAY_STRING (overlay, str, 0);
5544 /* If overlay has a non-empty after-string, record it. */
5545 if ((end == charpos || (start == charpos && invis_p))
5546 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5547 && SCHARS (str))
5548 RECORD_OVERLAY_STRING (overlay, str, 1);
5551 /* Process overlays after the overlay center. */
5552 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5554 XSETMISC (overlay, ov);
5555 eassert (OVERLAYP (overlay));
5556 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5557 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5559 if (start > charpos)
5560 break;
5562 /* Skip this overlay if it doesn't start or end at IT's current
5563 position. */
5564 if (end != charpos && start != charpos)
5565 continue;
5567 /* Skip this overlay if it doesn't apply to IT->w. */
5568 window = Foverlay_get (overlay, Qwindow);
5569 if (WINDOWP (window) && XWINDOW (window) != it->w)
5570 continue;
5572 /* If the text ``under'' the overlay is invisible, it has a zero
5573 dimension, and both before- and after-strings apply. */
5574 invisible = Foverlay_get (overlay, Qinvisible);
5575 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5577 /* If overlay has a non-empty before-string, record it. */
5578 if ((start == charpos || (end == charpos && invis_p))
5579 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5580 && SCHARS (str))
5581 RECORD_OVERLAY_STRING (overlay, str, 0);
5583 /* If overlay has a non-empty after-string, record it. */
5584 if ((end == charpos || (start == charpos && invis_p))
5585 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5586 && SCHARS (str))
5587 RECORD_OVERLAY_STRING (overlay, str, 1);
5590 #undef RECORD_OVERLAY_STRING
5592 /* Sort entries. */
5593 if (n > 1)
5594 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5596 /* Record number of overlay strings, and where we computed it. */
5597 it->n_overlay_strings = n;
5598 it->overlay_strings_charpos = charpos;
5600 /* IT->current.overlay_string_index is the number of overlay strings
5601 that have already been consumed by IT. Copy some of the
5602 remaining overlay strings to IT->overlay_strings. */
5603 i = 0;
5604 j = it->current.overlay_string_index;
5605 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5607 it->overlay_strings[i] = entries[j].string;
5608 it->string_overlays[i++] = entries[j++].overlay;
5611 CHECK_IT (it);
5612 SAFE_FREE ();
5616 /* Get the first chunk of overlay strings at IT's current buffer
5617 position, or at CHARPOS if that is > 0. Value is non-zero if at
5618 least one overlay string was found. */
5620 static int
5621 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5623 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5624 process. This fills IT->overlay_strings with strings, and sets
5625 IT->n_overlay_strings to the total number of strings to process.
5626 IT->pos.overlay_string_index has to be set temporarily to zero
5627 because load_overlay_strings needs this; it must be set to -1
5628 when no overlay strings are found because a zero value would
5629 indicate a position in the first overlay string. */
5630 it->current.overlay_string_index = 0;
5631 load_overlay_strings (it, charpos);
5633 /* If we found overlay strings, set up IT to deliver display
5634 elements from the first one. Otherwise set up IT to deliver
5635 from current_buffer. */
5636 if (it->n_overlay_strings)
5638 /* Make sure we know settings in current_buffer, so that we can
5639 restore meaningful values when we're done with the overlay
5640 strings. */
5641 if (compute_stop_p)
5642 compute_stop_pos (it);
5643 eassert (it->face_id >= 0);
5645 /* Save IT's settings. They are restored after all overlay
5646 strings have been processed. */
5647 eassert (!compute_stop_p || it->sp == 0);
5649 /* When called from handle_stop, there might be an empty display
5650 string loaded. In that case, don't bother saving it. But
5651 don't use this optimization with the bidi iterator, since we
5652 need the corresponding pop_it call to resync the bidi
5653 iterator's position with IT's position, after we are done
5654 with the overlay strings. (The corresponding call to pop_it
5655 in case of an empty display string is in
5656 next_overlay_string.) */
5657 if (!(!it->bidi_p
5658 && STRINGP (it->string) && !SCHARS (it->string)))
5659 push_it (it, NULL);
5661 /* Set up IT to deliver display elements from the first overlay
5662 string. */
5663 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5664 it->string = it->overlay_strings[0];
5665 it->from_overlay = Qnil;
5666 it->stop_charpos = 0;
5667 eassert (STRINGP (it->string));
5668 it->end_charpos = SCHARS (it->string);
5669 it->prev_stop = 0;
5670 it->base_level_stop = 0;
5671 it->multibyte_p = STRING_MULTIBYTE (it->string);
5672 it->method = GET_FROM_STRING;
5673 it->from_disp_prop_p = 0;
5675 /* Force paragraph direction to be that of the parent
5676 buffer. */
5677 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5678 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5679 else
5680 it->paragraph_embedding = L2R;
5682 /* Set up the bidi iterator for this overlay string. */
5683 if (it->bidi_p)
5685 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5687 it->bidi_it.string.lstring = it->string;
5688 it->bidi_it.string.s = NULL;
5689 it->bidi_it.string.schars = SCHARS (it->string);
5690 it->bidi_it.string.bufpos = pos;
5691 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5692 it->bidi_it.string.unibyte = !it->multibyte_p;
5693 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5695 return 1;
5698 it->current.overlay_string_index = -1;
5699 return 0;
5702 static int
5703 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5705 it->string = Qnil;
5706 it->method = GET_FROM_BUFFER;
5708 (void) get_overlay_strings_1 (it, charpos, 1);
5710 CHECK_IT (it);
5712 /* Value is non-zero if we found at least one overlay string. */
5713 return STRINGP (it->string);
5718 /***********************************************************************
5719 Saving and restoring state
5720 ***********************************************************************/
5722 /* Save current settings of IT on IT->stack. Called, for example,
5723 before setting up IT for an overlay string, to be able to restore
5724 IT's settings to what they were after the overlay string has been
5725 processed. If POSITION is non-NULL, it is the position to save on
5726 the stack instead of IT->position. */
5728 static void
5729 push_it (struct it *it, struct text_pos *position)
5731 struct iterator_stack_entry *p;
5733 eassert (it->sp < IT_STACK_SIZE);
5734 p = it->stack + it->sp;
5736 p->stop_charpos = it->stop_charpos;
5737 p->prev_stop = it->prev_stop;
5738 p->base_level_stop = it->base_level_stop;
5739 p->cmp_it = it->cmp_it;
5740 eassert (it->face_id >= 0);
5741 p->face_id = it->face_id;
5742 p->string = it->string;
5743 p->method = it->method;
5744 p->from_overlay = it->from_overlay;
5745 switch (p->method)
5747 case GET_FROM_IMAGE:
5748 p->u.image.object = it->object;
5749 p->u.image.image_id = it->image_id;
5750 p->u.image.slice = it->slice;
5751 break;
5752 case GET_FROM_STRETCH:
5753 p->u.stretch.object = it->object;
5754 break;
5756 p->position = position ? *position : it->position;
5757 p->current = it->current;
5758 p->end_charpos = it->end_charpos;
5759 p->string_nchars = it->string_nchars;
5760 p->area = it->area;
5761 p->multibyte_p = it->multibyte_p;
5762 p->avoid_cursor_p = it->avoid_cursor_p;
5763 p->space_width = it->space_width;
5764 p->font_height = it->font_height;
5765 p->voffset = it->voffset;
5766 p->string_from_display_prop_p = it->string_from_display_prop_p;
5767 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5768 p->display_ellipsis_p = 0;
5769 p->line_wrap = it->line_wrap;
5770 p->bidi_p = it->bidi_p;
5771 p->paragraph_embedding = it->paragraph_embedding;
5772 p->from_disp_prop_p = it->from_disp_prop_p;
5773 ++it->sp;
5775 /* Save the state of the bidi iterator as well. */
5776 if (it->bidi_p)
5777 bidi_push_it (&it->bidi_it);
5780 static void
5781 iterate_out_of_display_property (struct it *it)
5783 int buffer_p = !STRINGP (it->string);
5784 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5785 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5787 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5789 /* Maybe initialize paragraph direction. If we are at the beginning
5790 of a new paragraph, next_element_from_buffer may not have a
5791 chance to do that. */
5792 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5793 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5794 /* prev_stop can be zero, so check against BEGV as well. */
5795 while (it->bidi_it.charpos >= bob
5796 && it->prev_stop <= it->bidi_it.charpos
5797 && it->bidi_it.charpos < CHARPOS (it->position)
5798 && it->bidi_it.charpos < eob)
5799 bidi_move_to_visually_next (&it->bidi_it);
5800 /* Record the stop_pos we just crossed, for when we cross it
5801 back, maybe. */
5802 if (it->bidi_it.charpos > CHARPOS (it->position))
5803 it->prev_stop = CHARPOS (it->position);
5804 /* If we ended up not where pop_it put us, resync IT's
5805 positional members with the bidi iterator. */
5806 if (it->bidi_it.charpos != CHARPOS (it->position))
5807 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5808 if (buffer_p)
5809 it->current.pos = it->position;
5810 else
5811 it->current.string_pos = it->position;
5814 /* Restore IT's settings from IT->stack. Called, for example, when no
5815 more overlay strings must be processed, and we return to delivering
5816 display elements from a buffer, or when the end of a string from a
5817 `display' property is reached and we return to delivering display
5818 elements from an overlay string, or from a buffer. */
5820 static void
5821 pop_it (struct it *it)
5823 struct iterator_stack_entry *p;
5824 int from_display_prop = it->from_disp_prop_p;
5826 eassert (it->sp > 0);
5827 --it->sp;
5828 p = it->stack + it->sp;
5829 it->stop_charpos = p->stop_charpos;
5830 it->prev_stop = p->prev_stop;
5831 it->base_level_stop = p->base_level_stop;
5832 it->cmp_it = p->cmp_it;
5833 it->face_id = p->face_id;
5834 it->current = p->current;
5835 it->position = p->position;
5836 it->string = p->string;
5837 it->from_overlay = p->from_overlay;
5838 if (NILP (it->string))
5839 SET_TEXT_POS (it->current.string_pos, -1, -1);
5840 it->method = p->method;
5841 switch (it->method)
5843 case GET_FROM_IMAGE:
5844 it->image_id = p->u.image.image_id;
5845 it->object = p->u.image.object;
5846 it->slice = p->u.image.slice;
5847 break;
5848 case GET_FROM_STRETCH:
5849 it->object = p->u.stretch.object;
5850 break;
5851 case GET_FROM_BUFFER:
5852 it->object = it->w->contents;
5853 break;
5854 case GET_FROM_STRING:
5855 it->object = it->string;
5856 break;
5857 case GET_FROM_DISPLAY_VECTOR:
5858 if (it->s)
5859 it->method = GET_FROM_C_STRING;
5860 else if (STRINGP (it->string))
5861 it->method = GET_FROM_STRING;
5862 else
5864 it->method = GET_FROM_BUFFER;
5865 it->object = it->w->contents;
5868 it->end_charpos = p->end_charpos;
5869 it->string_nchars = p->string_nchars;
5870 it->area = p->area;
5871 it->multibyte_p = p->multibyte_p;
5872 it->avoid_cursor_p = p->avoid_cursor_p;
5873 it->space_width = p->space_width;
5874 it->font_height = p->font_height;
5875 it->voffset = p->voffset;
5876 it->string_from_display_prop_p = p->string_from_display_prop_p;
5877 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5878 it->line_wrap = p->line_wrap;
5879 it->bidi_p = p->bidi_p;
5880 it->paragraph_embedding = p->paragraph_embedding;
5881 it->from_disp_prop_p = p->from_disp_prop_p;
5882 if (it->bidi_p)
5884 bidi_pop_it (&it->bidi_it);
5885 /* Bidi-iterate until we get out of the portion of text, if any,
5886 covered by a `display' text property or by an overlay with
5887 `display' property. (We cannot just jump there, because the
5888 internal coherency of the bidi iterator state can not be
5889 preserved across such jumps.) We also must determine the
5890 paragraph base direction if the overlay we just processed is
5891 at the beginning of a new paragraph. */
5892 if (from_display_prop
5893 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5894 iterate_out_of_display_property (it);
5896 eassert ((BUFFERP (it->object)
5897 && IT_CHARPOS (*it) == it->bidi_it.charpos
5898 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5899 || (STRINGP (it->object)
5900 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5901 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5902 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5908 /***********************************************************************
5909 Moving over lines
5910 ***********************************************************************/
5912 /* Set IT's current position to the previous line start. */
5914 static void
5915 back_to_previous_line_start (struct it *it)
5917 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
5919 DEC_BOTH (cp, bp);
5920 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
5924 /* Move IT to the next line start.
5926 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5927 we skipped over part of the text (as opposed to moving the iterator
5928 continuously over the text). Otherwise, don't change the value
5929 of *SKIPPED_P.
5931 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5932 iterator on the newline, if it was found.
5934 Newlines may come from buffer text, overlay strings, or strings
5935 displayed via the `display' property. That's the reason we can't
5936 simply use find_newline_no_quit.
5938 Note that this function may not skip over invisible text that is so
5939 because of text properties and immediately follows a newline. If
5940 it would, function reseat_at_next_visible_line_start, when called
5941 from set_iterator_to_next, would effectively make invisible
5942 characters following a newline part of the wrong glyph row, which
5943 leads to wrong cursor motion. */
5945 static int
5946 forward_to_next_line_start (struct it *it, int *skipped_p,
5947 struct bidi_it *bidi_it_prev)
5949 ptrdiff_t old_selective;
5950 int newline_found_p, n;
5951 const int MAX_NEWLINE_DISTANCE = 500;
5953 /* If already on a newline, just consume it to avoid unintended
5954 skipping over invisible text below. */
5955 if (it->what == IT_CHARACTER
5956 && it->c == '\n'
5957 && CHARPOS (it->position) == IT_CHARPOS (*it))
5959 if (it->bidi_p && bidi_it_prev)
5960 *bidi_it_prev = it->bidi_it;
5961 set_iterator_to_next (it, 0);
5962 it->c = 0;
5963 return 1;
5966 /* Don't handle selective display in the following. It's (a)
5967 unnecessary because it's done by the caller, and (b) leads to an
5968 infinite recursion because next_element_from_ellipsis indirectly
5969 calls this function. */
5970 old_selective = it->selective;
5971 it->selective = 0;
5973 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5974 from buffer text. */
5975 for (n = newline_found_p = 0;
5976 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5977 n += STRINGP (it->string) ? 0 : 1)
5979 if (!get_next_display_element (it))
5980 return 0;
5981 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5982 if (newline_found_p && it->bidi_p && bidi_it_prev)
5983 *bidi_it_prev = it->bidi_it;
5984 set_iterator_to_next (it, 0);
5987 /* If we didn't find a newline near enough, see if we can use a
5988 short-cut. */
5989 if (!newline_found_p)
5991 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
5992 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
5993 1, &bytepos);
5994 Lisp_Object pos;
5996 eassert (!STRINGP (it->string));
5998 /* If there isn't any `display' property in sight, and no
5999 overlays, we can just use the position of the newline in
6000 buffer text. */
6001 if (it->stop_charpos >= limit
6002 || ((pos = Fnext_single_property_change (make_number (start),
6003 Qdisplay, Qnil,
6004 make_number (limit)),
6005 NILP (pos))
6006 && next_overlay_change (start) == ZV))
6008 if (!it->bidi_p)
6010 IT_CHARPOS (*it) = limit;
6011 IT_BYTEPOS (*it) = bytepos;
6013 else
6015 struct bidi_it bprev;
6017 /* Help bidi.c avoid expensive searches for display
6018 properties and overlays, by telling it that there are
6019 none up to `limit'. */
6020 if (it->bidi_it.disp_pos < limit)
6022 it->bidi_it.disp_pos = limit;
6023 it->bidi_it.disp_prop = 0;
6025 do {
6026 bprev = it->bidi_it;
6027 bidi_move_to_visually_next (&it->bidi_it);
6028 } while (it->bidi_it.charpos != limit);
6029 IT_CHARPOS (*it) = limit;
6030 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6031 if (bidi_it_prev)
6032 *bidi_it_prev = bprev;
6034 *skipped_p = newline_found_p = 1;
6036 else
6038 while (get_next_display_element (it)
6039 && !newline_found_p)
6041 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6042 if (newline_found_p && it->bidi_p && bidi_it_prev)
6043 *bidi_it_prev = it->bidi_it;
6044 set_iterator_to_next (it, 0);
6049 it->selective = old_selective;
6050 return newline_found_p;
6054 /* Set IT's current position to the previous visible line start. Skip
6055 invisible text that is so either due to text properties or due to
6056 selective display. Caution: this does not change IT->current_x and
6057 IT->hpos. */
6059 static void
6060 back_to_previous_visible_line_start (struct it *it)
6062 while (IT_CHARPOS (*it) > BEGV)
6064 back_to_previous_line_start (it);
6066 if (IT_CHARPOS (*it) <= BEGV)
6067 break;
6069 /* If selective > 0, then lines indented more than its value are
6070 invisible. */
6071 if (it->selective > 0
6072 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6073 it->selective))
6074 continue;
6076 /* Check the newline before point for invisibility. */
6078 Lisp_Object prop;
6079 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6080 Qinvisible, it->window);
6081 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6082 continue;
6085 if (IT_CHARPOS (*it) <= BEGV)
6086 break;
6089 struct it it2;
6090 void *it2data = NULL;
6091 ptrdiff_t pos;
6092 ptrdiff_t beg, end;
6093 Lisp_Object val, overlay;
6095 SAVE_IT (it2, *it, it2data);
6097 /* If newline is part of a composition, continue from start of composition */
6098 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6099 && beg < IT_CHARPOS (*it))
6100 goto replaced;
6102 /* If newline is replaced by a display property, find start of overlay
6103 or interval and continue search from that point. */
6104 pos = --IT_CHARPOS (it2);
6105 --IT_BYTEPOS (it2);
6106 it2.sp = 0;
6107 bidi_unshelve_cache (NULL, 0);
6108 it2.string_from_display_prop_p = 0;
6109 it2.from_disp_prop_p = 0;
6110 if (handle_display_prop (&it2) == HANDLED_RETURN
6111 && !NILP (val = get_char_property_and_overlay
6112 (make_number (pos), Qdisplay, Qnil, &overlay))
6113 && (OVERLAYP (overlay)
6114 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6115 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6117 RESTORE_IT (it, it, it2data);
6118 goto replaced;
6121 /* Newline is not replaced by anything -- so we are done. */
6122 RESTORE_IT (it, it, it2data);
6123 break;
6125 replaced:
6126 if (beg < BEGV)
6127 beg = BEGV;
6128 IT_CHARPOS (*it) = beg;
6129 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6133 it->continuation_lines_width = 0;
6135 eassert (IT_CHARPOS (*it) >= BEGV);
6136 eassert (IT_CHARPOS (*it) == BEGV
6137 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6138 CHECK_IT (it);
6142 /* Reseat iterator IT at the previous visible line start. Skip
6143 invisible text that is so either due to text properties or due to
6144 selective display. At the end, update IT's overlay information,
6145 face information etc. */
6147 void
6148 reseat_at_previous_visible_line_start (struct it *it)
6150 back_to_previous_visible_line_start (it);
6151 reseat (it, it->current.pos, 1);
6152 CHECK_IT (it);
6156 /* Reseat iterator IT on the next visible line start in the current
6157 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6158 preceding the line start. Skip over invisible text that is so
6159 because of selective display. Compute faces, overlays etc at the
6160 new position. Note that this function does not skip over text that
6161 is invisible because of text properties. */
6163 static void
6164 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6166 int newline_found_p, skipped_p = 0;
6167 struct bidi_it bidi_it_prev;
6169 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6171 /* Skip over lines that are invisible because they are indented
6172 more than the value of IT->selective. */
6173 if (it->selective > 0)
6174 while (IT_CHARPOS (*it) < ZV
6175 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6176 it->selective))
6178 eassert (IT_BYTEPOS (*it) == BEGV
6179 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6180 newline_found_p =
6181 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6184 /* Position on the newline if that's what's requested. */
6185 if (on_newline_p && newline_found_p)
6187 if (STRINGP (it->string))
6189 if (IT_STRING_CHARPOS (*it) > 0)
6191 if (!it->bidi_p)
6193 --IT_STRING_CHARPOS (*it);
6194 --IT_STRING_BYTEPOS (*it);
6196 else
6198 /* We need to restore the bidi iterator to the state
6199 it had on the newline, and resync the IT's
6200 position with that. */
6201 it->bidi_it = bidi_it_prev;
6202 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6203 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6207 else if (IT_CHARPOS (*it) > BEGV)
6209 if (!it->bidi_p)
6211 --IT_CHARPOS (*it);
6212 --IT_BYTEPOS (*it);
6214 else
6216 /* We need to restore the bidi iterator to the state it
6217 had on the newline and resync IT with that. */
6218 it->bidi_it = bidi_it_prev;
6219 IT_CHARPOS (*it) = it->bidi_it.charpos;
6220 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6222 reseat (it, it->current.pos, 0);
6225 else if (skipped_p)
6226 reseat (it, it->current.pos, 0);
6228 CHECK_IT (it);
6233 /***********************************************************************
6234 Changing an iterator's position
6235 ***********************************************************************/
6237 /* Change IT's current position to POS in current_buffer. If FORCE_P
6238 is non-zero, always check for text properties at the new position.
6239 Otherwise, text properties are only looked up if POS >=
6240 IT->check_charpos of a property. */
6242 static void
6243 reseat (struct it *it, struct text_pos pos, int force_p)
6245 ptrdiff_t original_pos = IT_CHARPOS (*it);
6247 reseat_1 (it, pos, 0);
6249 /* Determine where to check text properties. Avoid doing it
6250 where possible because text property lookup is very expensive. */
6251 if (force_p
6252 || CHARPOS (pos) > it->stop_charpos
6253 || CHARPOS (pos) < original_pos)
6255 if (it->bidi_p)
6257 /* For bidi iteration, we need to prime prev_stop and
6258 base_level_stop with our best estimations. */
6259 /* Implementation note: Of course, POS is not necessarily a
6260 stop position, so assigning prev_pos to it is a lie; we
6261 should have called compute_stop_backwards. However, if
6262 the current buffer does not include any R2L characters,
6263 that call would be a waste of cycles, because the
6264 iterator will never move back, and thus never cross this
6265 "fake" stop position. So we delay that backward search
6266 until the time we really need it, in next_element_from_buffer. */
6267 if (CHARPOS (pos) != it->prev_stop)
6268 it->prev_stop = CHARPOS (pos);
6269 if (CHARPOS (pos) < it->base_level_stop)
6270 it->base_level_stop = 0; /* meaning it's unknown */
6271 handle_stop (it);
6273 else
6275 handle_stop (it);
6276 it->prev_stop = it->base_level_stop = 0;
6281 CHECK_IT (it);
6285 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6286 IT->stop_pos to POS, also. */
6288 static void
6289 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6291 /* Don't call this function when scanning a C string. */
6292 eassert (it->s == NULL);
6294 /* POS must be a reasonable value. */
6295 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6297 it->current.pos = it->position = pos;
6298 it->end_charpos = ZV;
6299 it->dpvec = NULL;
6300 it->current.dpvec_index = -1;
6301 it->current.overlay_string_index = -1;
6302 IT_STRING_CHARPOS (*it) = -1;
6303 IT_STRING_BYTEPOS (*it) = -1;
6304 it->string = Qnil;
6305 it->method = GET_FROM_BUFFER;
6306 it->object = it->w->contents;
6307 it->area = TEXT_AREA;
6308 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6309 it->sp = 0;
6310 it->string_from_display_prop_p = 0;
6311 it->string_from_prefix_prop_p = 0;
6313 it->from_disp_prop_p = 0;
6314 it->face_before_selective_p = 0;
6315 if (it->bidi_p)
6317 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6318 &it->bidi_it);
6319 bidi_unshelve_cache (NULL, 0);
6320 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6321 it->bidi_it.string.s = NULL;
6322 it->bidi_it.string.lstring = Qnil;
6323 it->bidi_it.string.bufpos = 0;
6324 it->bidi_it.string.unibyte = 0;
6327 if (set_stop_p)
6329 it->stop_charpos = CHARPOS (pos);
6330 it->base_level_stop = CHARPOS (pos);
6332 /* This make the information stored in it->cmp_it invalidate. */
6333 it->cmp_it.id = -1;
6337 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6338 If S is non-null, it is a C string to iterate over. Otherwise,
6339 STRING gives a Lisp string to iterate over.
6341 If PRECISION > 0, don't return more then PRECISION number of
6342 characters from the string.
6344 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6345 characters have been returned. FIELD_WIDTH < 0 means an infinite
6346 field width.
6348 MULTIBYTE = 0 means disable processing of multibyte characters,
6349 MULTIBYTE > 0 means enable it,
6350 MULTIBYTE < 0 means use IT->multibyte_p.
6352 IT must be initialized via a prior call to init_iterator before
6353 calling this function. */
6355 static void
6356 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6357 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6358 int multibyte)
6360 /* No region in strings. */
6361 it->region_beg_charpos = it->region_end_charpos = -1;
6363 /* No text property checks performed by default, but see below. */
6364 it->stop_charpos = -1;
6366 /* Set iterator position and end position. */
6367 memset (&it->current, 0, sizeof it->current);
6368 it->current.overlay_string_index = -1;
6369 it->current.dpvec_index = -1;
6370 eassert (charpos >= 0);
6372 /* If STRING is specified, use its multibyteness, otherwise use the
6373 setting of MULTIBYTE, if specified. */
6374 if (multibyte >= 0)
6375 it->multibyte_p = multibyte > 0;
6377 /* Bidirectional reordering of strings is controlled by the default
6378 value of bidi-display-reordering. Don't try to reorder while
6379 loading loadup.el, as the necessary character property tables are
6380 not yet available. */
6381 it->bidi_p =
6382 NILP (Vpurify_flag)
6383 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6385 if (s == NULL)
6387 eassert (STRINGP (string));
6388 it->string = string;
6389 it->s = NULL;
6390 it->end_charpos = it->string_nchars = SCHARS (string);
6391 it->method = GET_FROM_STRING;
6392 it->current.string_pos = string_pos (charpos, string);
6394 if (it->bidi_p)
6396 it->bidi_it.string.lstring = string;
6397 it->bidi_it.string.s = NULL;
6398 it->bidi_it.string.schars = it->end_charpos;
6399 it->bidi_it.string.bufpos = 0;
6400 it->bidi_it.string.from_disp_str = 0;
6401 it->bidi_it.string.unibyte = !it->multibyte_p;
6402 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6403 FRAME_WINDOW_P (it->f), &it->bidi_it);
6406 else
6408 it->s = (const unsigned char *) s;
6409 it->string = Qnil;
6411 /* Note that we use IT->current.pos, not it->current.string_pos,
6412 for displaying C strings. */
6413 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6414 if (it->multibyte_p)
6416 it->current.pos = c_string_pos (charpos, s, 1);
6417 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6419 else
6421 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6422 it->end_charpos = it->string_nchars = strlen (s);
6425 if (it->bidi_p)
6427 it->bidi_it.string.lstring = Qnil;
6428 it->bidi_it.string.s = (const unsigned char *) s;
6429 it->bidi_it.string.schars = it->end_charpos;
6430 it->bidi_it.string.bufpos = 0;
6431 it->bidi_it.string.from_disp_str = 0;
6432 it->bidi_it.string.unibyte = !it->multibyte_p;
6433 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6434 &it->bidi_it);
6436 it->method = GET_FROM_C_STRING;
6439 /* PRECISION > 0 means don't return more than PRECISION characters
6440 from the string. */
6441 if (precision > 0 && it->end_charpos - charpos > precision)
6443 it->end_charpos = it->string_nchars = charpos + precision;
6444 if (it->bidi_p)
6445 it->bidi_it.string.schars = it->end_charpos;
6448 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6449 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6450 FIELD_WIDTH < 0 means infinite field width. This is useful for
6451 padding with `-' at the end of a mode line. */
6452 if (field_width < 0)
6453 field_width = INFINITY;
6454 /* Implementation note: We deliberately don't enlarge
6455 it->bidi_it.string.schars here to fit it->end_charpos, because
6456 the bidi iterator cannot produce characters out of thin air. */
6457 if (field_width > it->end_charpos - charpos)
6458 it->end_charpos = charpos + field_width;
6460 /* Use the standard display table for displaying strings. */
6461 if (DISP_TABLE_P (Vstandard_display_table))
6462 it->dp = XCHAR_TABLE (Vstandard_display_table);
6464 it->stop_charpos = charpos;
6465 it->prev_stop = charpos;
6466 it->base_level_stop = 0;
6467 if (it->bidi_p)
6469 it->bidi_it.first_elt = 1;
6470 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6471 it->bidi_it.disp_pos = -1;
6473 if (s == NULL && it->multibyte_p)
6475 ptrdiff_t endpos = SCHARS (it->string);
6476 if (endpos > it->end_charpos)
6477 endpos = it->end_charpos;
6478 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6479 it->string);
6481 CHECK_IT (it);
6486 /***********************************************************************
6487 Iteration
6488 ***********************************************************************/
6490 /* Map enum it_method value to corresponding next_element_from_* function. */
6492 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6494 next_element_from_buffer,
6495 next_element_from_display_vector,
6496 next_element_from_string,
6497 next_element_from_c_string,
6498 next_element_from_image,
6499 next_element_from_stretch
6502 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6505 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6506 (possibly with the following characters). */
6508 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6509 ((IT)->cmp_it.id >= 0 \
6510 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6511 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6512 END_CHARPOS, (IT)->w, \
6513 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6514 (IT)->string)))
6517 /* Lookup the char-table Vglyphless_char_display for character C (-1
6518 if we want information for no-font case), and return the display
6519 method symbol. By side-effect, update it->what and
6520 it->glyphless_method. This function is called from
6521 get_next_display_element for each character element, and from
6522 x_produce_glyphs when no suitable font was found. */
6524 Lisp_Object
6525 lookup_glyphless_char_display (int c, struct it *it)
6527 Lisp_Object glyphless_method = Qnil;
6529 if (CHAR_TABLE_P (Vglyphless_char_display)
6530 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6532 if (c >= 0)
6534 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6535 if (CONSP (glyphless_method))
6536 glyphless_method = FRAME_WINDOW_P (it->f)
6537 ? XCAR (glyphless_method)
6538 : XCDR (glyphless_method);
6540 else
6541 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6544 retry:
6545 if (NILP (glyphless_method))
6547 if (c >= 0)
6548 /* The default is to display the character by a proper font. */
6549 return Qnil;
6550 /* The default for the no-font case is to display an empty box. */
6551 glyphless_method = Qempty_box;
6553 if (EQ (glyphless_method, Qzero_width))
6555 if (c >= 0)
6556 return glyphless_method;
6557 /* This method can't be used for the no-font case. */
6558 glyphless_method = Qempty_box;
6560 if (EQ (glyphless_method, Qthin_space))
6561 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6562 else if (EQ (glyphless_method, Qempty_box))
6563 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6564 else if (EQ (glyphless_method, Qhex_code))
6565 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6566 else if (STRINGP (glyphless_method))
6567 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6568 else
6570 /* Invalid value. We use the default method. */
6571 glyphless_method = Qnil;
6572 goto retry;
6574 it->what = IT_GLYPHLESS;
6575 return glyphless_method;
6578 /* Load IT's display element fields with information about the next
6579 display element from the current position of IT. Value is zero if
6580 end of buffer (or C string) is reached. */
6582 static struct frame *last_escape_glyph_frame = NULL;
6583 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6584 static int last_escape_glyph_merged_face_id = 0;
6586 struct frame *last_glyphless_glyph_frame = NULL;
6587 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6588 int last_glyphless_glyph_merged_face_id = 0;
6590 static int
6591 get_next_display_element (struct it *it)
6593 /* Non-zero means that we found a display element. Zero means that
6594 we hit the end of what we iterate over. Performance note: the
6595 function pointer `method' used here turns out to be faster than
6596 using a sequence of if-statements. */
6597 int success_p;
6599 get_next:
6600 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6602 if (it->what == IT_CHARACTER)
6604 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6605 and only if (a) the resolved directionality of that character
6606 is R..." */
6607 /* FIXME: Do we need an exception for characters from display
6608 tables? */
6609 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6610 it->c = bidi_mirror_char (it->c);
6611 /* Map via display table or translate control characters.
6612 IT->c, IT->len etc. have been set to the next character by
6613 the function call above. If we have a display table, and it
6614 contains an entry for IT->c, translate it. Don't do this if
6615 IT->c itself comes from a display table, otherwise we could
6616 end up in an infinite recursion. (An alternative could be to
6617 count the recursion depth of this function and signal an
6618 error when a certain maximum depth is reached.) Is it worth
6619 it? */
6620 if (success_p && it->dpvec == NULL)
6622 Lisp_Object dv;
6623 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6624 int nonascii_space_p = 0;
6625 int nonascii_hyphen_p = 0;
6626 int c = it->c; /* This is the character to display. */
6628 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6630 eassert (SINGLE_BYTE_CHAR_P (c));
6631 if (unibyte_display_via_language_environment)
6633 c = DECODE_CHAR (unibyte, c);
6634 if (c < 0)
6635 c = BYTE8_TO_CHAR (it->c);
6637 else
6638 c = BYTE8_TO_CHAR (it->c);
6641 if (it->dp
6642 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6643 VECTORP (dv)))
6645 struct Lisp_Vector *v = XVECTOR (dv);
6647 /* Return the first character from the display table
6648 entry, if not empty. If empty, don't display the
6649 current character. */
6650 if (v->header.size)
6652 it->dpvec_char_len = it->len;
6653 it->dpvec = v->contents;
6654 it->dpend = v->contents + v->header.size;
6655 it->current.dpvec_index = 0;
6656 it->dpvec_face_id = -1;
6657 it->saved_face_id = it->face_id;
6658 it->method = GET_FROM_DISPLAY_VECTOR;
6659 it->ellipsis_p = 0;
6661 else
6663 set_iterator_to_next (it, 0);
6665 goto get_next;
6668 if (! NILP (lookup_glyphless_char_display (c, it)))
6670 if (it->what == IT_GLYPHLESS)
6671 goto done;
6672 /* Don't display this character. */
6673 set_iterator_to_next (it, 0);
6674 goto get_next;
6677 /* If `nobreak-char-display' is non-nil, we display
6678 non-ASCII spaces and hyphens specially. */
6679 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6681 if (c == 0xA0)
6682 nonascii_space_p = 1;
6683 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6684 nonascii_hyphen_p = 1;
6687 /* Translate control characters into `\003' or `^C' form.
6688 Control characters coming from a display table entry are
6689 currently not translated because we use IT->dpvec to hold
6690 the translation. This could easily be changed but I
6691 don't believe that it is worth doing.
6693 The characters handled by `nobreak-char-display' must be
6694 translated too.
6696 Non-printable characters and raw-byte characters are also
6697 translated to octal form. */
6698 if (((c < ' ' || c == 127) /* ASCII control chars */
6699 ? (it->area != TEXT_AREA
6700 /* In mode line, treat \n, \t like other crl chars. */
6701 || (c != '\t'
6702 && it->glyph_row
6703 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6704 || (c != '\n' && c != '\t'))
6705 : (nonascii_space_p
6706 || nonascii_hyphen_p
6707 || CHAR_BYTE8_P (c)
6708 || ! CHAR_PRINTABLE_P (c))))
6710 /* C is a control character, non-ASCII space/hyphen,
6711 raw-byte, or a non-printable character which must be
6712 displayed either as '\003' or as `^C' where the '\\'
6713 and '^' can be defined in the display table. Fill
6714 IT->ctl_chars with glyphs for what we have to
6715 display. Then, set IT->dpvec to these glyphs. */
6716 Lisp_Object gc;
6717 int ctl_len;
6718 int face_id;
6719 int lface_id = 0;
6720 int escape_glyph;
6722 /* Handle control characters with ^. */
6724 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6726 int g;
6728 g = '^'; /* default glyph for Control */
6729 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6730 if (it->dp
6731 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6733 g = GLYPH_CODE_CHAR (gc);
6734 lface_id = GLYPH_CODE_FACE (gc);
6736 if (lface_id)
6738 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6740 else if (it->f == last_escape_glyph_frame
6741 && it->face_id == last_escape_glyph_face_id)
6743 face_id = last_escape_glyph_merged_face_id;
6745 else
6747 /* Merge the escape-glyph face into the current face. */
6748 face_id = merge_faces (it->f, Qescape_glyph, 0,
6749 it->face_id);
6750 last_escape_glyph_frame = it->f;
6751 last_escape_glyph_face_id = it->face_id;
6752 last_escape_glyph_merged_face_id = face_id;
6755 XSETINT (it->ctl_chars[0], g);
6756 XSETINT (it->ctl_chars[1], c ^ 0100);
6757 ctl_len = 2;
6758 goto display_control;
6761 /* Handle non-ascii space in the mode where it only gets
6762 highlighting. */
6764 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6766 /* Merge `nobreak-space' into the current face. */
6767 face_id = merge_faces (it->f, Qnobreak_space, 0,
6768 it->face_id);
6769 XSETINT (it->ctl_chars[0], ' ');
6770 ctl_len = 1;
6771 goto display_control;
6774 /* Handle sequences that start with the "escape glyph". */
6776 /* the default escape glyph is \. */
6777 escape_glyph = '\\';
6779 if (it->dp
6780 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6782 escape_glyph = GLYPH_CODE_CHAR (gc);
6783 lface_id = GLYPH_CODE_FACE (gc);
6785 if (lface_id)
6787 /* The display table specified a face.
6788 Merge it into face_id and also into escape_glyph. */
6789 face_id = merge_faces (it->f, Qt, lface_id,
6790 it->face_id);
6792 else if (it->f == last_escape_glyph_frame
6793 && it->face_id == last_escape_glyph_face_id)
6795 face_id = last_escape_glyph_merged_face_id;
6797 else
6799 /* Merge the escape-glyph face into the current face. */
6800 face_id = merge_faces (it->f, Qescape_glyph, 0,
6801 it->face_id);
6802 last_escape_glyph_frame = it->f;
6803 last_escape_glyph_face_id = it->face_id;
6804 last_escape_glyph_merged_face_id = face_id;
6807 /* Draw non-ASCII hyphen with just highlighting: */
6809 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6811 XSETINT (it->ctl_chars[0], '-');
6812 ctl_len = 1;
6813 goto display_control;
6816 /* Draw non-ASCII space/hyphen with escape glyph: */
6818 if (nonascii_space_p || nonascii_hyphen_p)
6820 XSETINT (it->ctl_chars[0], escape_glyph);
6821 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6822 ctl_len = 2;
6823 goto display_control;
6827 char str[10];
6828 int len, i;
6830 if (CHAR_BYTE8_P (c))
6831 /* Display \200 instead of \17777600. */
6832 c = CHAR_TO_BYTE8 (c);
6833 len = sprintf (str, "%03o", c);
6835 XSETINT (it->ctl_chars[0], escape_glyph);
6836 for (i = 0; i < len; i++)
6837 XSETINT (it->ctl_chars[i + 1], str[i]);
6838 ctl_len = len + 1;
6841 display_control:
6842 /* Set up IT->dpvec and return first character from it. */
6843 it->dpvec_char_len = it->len;
6844 it->dpvec = it->ctl_chars;
6845 it->dpend = it->dpvec + ctl_len;
6846 it->current.dpvec_index = 0;
6847 it->dpvec_face_id = face_id;
6848 it->saved_face_id = it->face_id;
6849 it->method = GET_FROM_DISPLAY_VECTOR;
6850 it->ellipsis_p = 0;
6851 goto get_next;
6853 it->char_to_display = c;
6855 else if (success_p)
6857 it->char_to_display = it->c;
6861 /* Adjust face id for a multibyte character. There are no multibyte
6862 character in unibyte text. */
6863 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6864 && it->multibyte_p
6865 && success_p
6866 && FRAME_WINDOW_P (it->f))
6868 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6870 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6872 /* Automatic composition with glyph-string. */
6873 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6875 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6877 else
6879 ptrdiff_t pos = (it->s ? -1
6880 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6881 : IT_CHARPOS (*it));
6882 int c;
6884 if (it->what == IT_CHARACTER)
6885 c = it->char_to_display;
6886 else
6888 struct composition *cmp = composition_table[it->cmp_it.id];
6889 int i;
6891 c = ' ';
6892 for (i = 0; i < cmp->glyph_len; i++)
6893 /* TAB in a composition means display glyphs with
6894 padding space on the left or right. */
6895 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6896 break;
6898 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6902 done:
6903 /* Is this character the last one of a run of characters with
6904 box? If yes, set IT->end_of_box_run_p to 1. */
6905 if (it->face_box_p
6906 && it->s == NULL)
6908 if (it->method == GET_FROM_STRING && it->sp)
6910 int face_id = underlying_face_id (it);
6911 struct face *face = FACE_FROM_ID (it->f, face_id);
6913 if (face)
6915 if (face->box == FACE_NO_BOX)
6917 /* If the box comes from face properties in a
6918 display string, check faces in that string. */
6919 int string_face_id = face_after_it_pos (it);
6920 it->end_of_box_run_p
6921 = (FACE_FROM_ID (it->f, string_face_id)->box
6922 == FACE_NO_BOX);
6924 /* Otherwise, the box comes from the underlying face.
6925 If this is the last string character displayed, check
6926 the next buffer location. */
6927 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6928 && (it->current.overlay_string_index
6929 == it->n_overlay_strings - 1))
6931 ptrdiff_t ignore;
6932 int next_face_id;
6933 struct text_pos pos = it->current.pos;
6934 INC_TEXT_POS (pos, it->multibyte_p);
6936 next_face_id = face_at_buffer_position
6937 (it->w, CHARPOS (pos), it->region_beg_charpos,
6938 it->region_end_charpos, &ignore,
6939 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6940 -1);
6941 it->end_of_box_run_p
6942 = (FACE_FROM_ID (it->f, next_face_id)->box
6943 == FACE_NO_BOX);
6947 else
6949 int face_id = face_after_it_pos (it);
6950 it->end_of_box_run_p
6951 = (face_id != it->face_id
6952 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6955 /* If we reached the end of the object we've been iterating (e.g., a
6956 display string or an overlay string), and there's something on
6957 IT->stack, proceed with what's on the stack. It doesn't make
6958 sense to return zero if there's unprocessed stuff on the stack,
6959 because otherwise that stuff will never be displayed. */
6960 if (!success_p && it->sp > 0)
6962 set_iterator_to_next (it, 0);
6963 success_p = get_next_display_element (it);
6966 /* Value is 0 if end of buffer or string reached. */
6967 return success_p;
6971 /* Move IT to the next display element.
6973 RESEAT_P non-zero means if called on a newline in buffer text,
6974 skip to the next visible line start.
6976 Functions get_next_display_element and set_iterator_to_next are
6977 separate because I find this arrangement easier to handle than a
6978 get_next_display_element function that also increments IT's
6979 position. The way it is we can first look at an iterator's current
6980 display element, decide whether it fits on a line, and if it does,
6981 increment the iterator position. The other way around we probably
6982 would either need a flag indicating whether the iterator has to be
6983 incremented the next time, or we would have to implement a
6984 decrement position function which would not be easy to write. */
6986 void
6987 set_iterator_to_next (struct it *it, int reseat_p)
6989 /* Reset flags indicating start and end of a sequence of characters
6990 with box. Reset them at the start of this function because
6991 moving the iterator to a new position might set them. */
6992 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6994 switch (it->method)
6996 case GET_FROM_BUFFER:
6997 /* The current display element of IT is a character from
6998 current_buffer. Advance in the buffer, and maybe skip over
6999 invisible lines that are so because of selective display. */
7000 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7001 reseat_at_next_visible_line_start (it, 0);
7002 else if (it->cmp_it.id >= 0)
7004 /* We are currently getting glyphs from a composition. */
7005 int i;
7007 if (! it->bidi_p)
7009 IT_CHARPOS (*it) += it->cmp_it.nchars;
7010 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7011 if (it->cmp_it.to < it->cmp_it.nglyphs)
7013 it->cmp_it.from = it->cmp_it.to;
7015 else
7017 it->cmp_it.id = -1;
7018 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7019 IT_BYTEPOS (*it),
7020 it->end_charpos, Qnil);
7023 else if (! it->cmp_it.reversed_p)
7025 /* Composition created while scanning forward. */
7026 /* Update IT's char/byte positions to point to the first
7027 character of the next grapheme cluster, or to the
7028 character visually after the current composition. */
7029 for (i = 0; i < it->cmp_it.nchars; i++)
7030 bidi_move_to_visually_next (&it->bidi_it);
7031 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7032 IT_CHARPOS (*it) = it->bidi_it.charpos;
7034 if (it->cmp_it.to < it->cmp_it.nglyphs)
7036 /* Proceed to the next grapheme cluster. */
7037 it->cmp_it.from = it->cmp_it.to;
7039 else
7041 /* No more grapheme clusters in this composition.
7042 Find the next stop position. */
7043 ptrdiff_t stop = it->end_charpos;
7044 if (it->bidi_it.scan_dir < 0)
7045 /* Now we are scanning backward and don't know
7046 where to stop. */
7047 stop = -1;
7048 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7049 IT_BYTEPOS (*it), stop, Qnil);
7052 else
7054 /* Composition created while scanning backward. */
7055 /* Update IT's char/byte positions to point to the last
7056 character of the previous grapheme cluster, or the
7057 character visually after the current composition. */
7058 for (i = 0; i < it->cmp_it.nchars; i++)
7059 bidi_move_to_visually_next (&it->bidi_it);
7060 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7061 IT_CHARPOS (*it) = it->bidi_it.charpos;
7062 if (it->cmp_it.from > 0)
7064 /* Proceed to the previous grapheme cluster. */
7065 it->cmp_it.to = it->cmp_it.from;
7067 else
7069 /* No more grapheme clusters in this composition.
7070 Find the next stop position. */
7071 ptrdiff_t stop = it->end_charpos;
7072 if (it->bidi_it.scan_dir < 0)
7073 /* Now we are scanning backward and don't know
7074 where to stop. */
7075 stop = -1;
7076 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7077 IT_BYTEPOS (*it), stop, Qnil);
7081 else
7083 eassert (it->len != 0);
7085 if (!it->bidi_p)
7087 IT_BYTEPOS (*it) += it->len;
7088 IT_CHARPOS (*it) += 1;
7090 else
7092 int prev_scan_dir = it->bidi_it.scan_dir;
7093 /* If this is a new paragraph, determine its base
7094 direction (a.k.a. its base embedding level). */
7095 if (it->bidi_it.new_paragraph)
7096 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
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;
7100 if (prev_scan_dir != it->bidi_it.scan_dir)
7102 /* As the scan direction was changed, we must
7103 re-compute the stop position for composition. */
7104 ptrdiff_t stop = it->end_charpos;
7105 if (it->bidi_it.scan_dir < 0)
7106 stop = -1;
7107 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7108 IT_BYTEPOS (*it), stop, Qnil);
7111 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7113 break;
7115 case GET_FROM_C_STRING:
7116 /* Current display element of IT is from a C string. */
7117 if (!it->bidi_p
7118 /* If the string position is beyond string's end, it means
7119 next_element_from_c_string is padding the string with
7120 blanks, in which case we bypass the bidi iterator,
7121 because it cannot deal with such virtual characters. */
7122 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7124 IT_BYTEPOS (*it) += it->len;
7125 IT_CHARPOS (*it) += 1;
7127 else
7129 bidi_move_to_visually_next (&it->bidi_it);
7130 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7131 IT_CHARPOS (*it) = it->bidi_it.charpos;
7133 break;
7135 case GET_FROM_DISPLAY_VECTOR:
7136 /* Current display element of IT is from a display table entry.
7137 Advance in the display table definition. Reset it to null if
7138 end reached, and continue with characters from buffers/
7139 strings. */
7140 ++it->current.dpvec_index;
7142 /* Restore face of the iterator to what they were before the
7143 display vector entry (these entries may contain faces). */
7144 it->face_id = it->saved_face_id;
7146 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7148 int recheck_faces = it->ellipsis_p;
7150 if (it->s)
7151 it->method = GET_FROM_C_STRING;
7152 else if (STRINGP (it->string))
7153 it->method = GET_FROM_STRING;
7154 else
7156 it->method = GET_FROM_BUFFER;
7157 it->object = it->w->contents;
7160 it->dpvec = NULL;
7161 it->current.dpvec_index = -1;
7163 /* Skip over characters which were displayed via IT->dpvec. */
7164 if (it->dpvec_char_len < 0)
7165 reseat_at_next_visible_line_start (it, 1);
7166 else if (it->dpvec_char_len > 0)
7168 if (it->method == GET_FROM_STRING
7169 && it->current.overlay_string_index >= 0
7170 && it->n_overlay_strings > 0)
7171 it->ignore_overlay_strings_at_pos_p = 1;
7172 it->len = it->dpvec_char_len;
7173 set_iterator_to_next (it, reseat_p);
7176 /* Maybe recheck faces after display vector */
7177 if (recheck_faces)
7178 it->stop_charpos = IT_CHARPOS (*it);
7180 break;
7182 case GET_FROM_STRING:
7183 /* Current display element is a character from a Lisp string. */
7184 eassert (it->s == NULL && STRINGP (it->string));
7185 /* Don't advance past string end. These conditions are true
7186 when set_iterator_to_next is called at the end of
7187 get_next_display_element, in which case the Lisp string is
7188 already exhausted, and all we want is pop the iterator
7189 stack. */
7190 if (it->current.overlay_string_index >= 0)
7192 /* This is an overlay string, so there's no padding with
7193 spaces, and the number of characters in the string is
7194 where the string ends. */
7195 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7196 goto consider_string_end;
7198 else
7200 /* Not an overlay string. There could be padding, so test
7201 against it->end_charpos . */
7202 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7203 goto consider_string_end;
7205 if (it->cmp_it.id >= 0)
7207 int i;
7209 if (! it->bidi_p)
7211 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7212 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7213 if (it->cmp_it.to < it->cmp_it.nglyphs)
7214 it->cmp_it.from = it->cmp_it.to;
7215 else
7217 it->cmp_it.id = -1;
7218 composition_compute_stop_pos (&it->cmp_it,
7219 IT_STRING_CHARPOS (*it),
7220 IT_STRING_BYTEPOS (*it),
7221 it->end_charpos, it->string);
7224 else if (! it->cmp_it.reversed_p)
7226 for (i = 0; i < it->cmp_it.nchars; i++)
7227 bidi_move_to_visually_next (&it->bidi_it);
7228 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7229 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7231 if (it->cmp_it.to < it->cmp_it.nglyphs)
7232 it->cmp_it.from = it->cmp_it.to;
7233 else
7235 ptrdiff_t stop = it->end_charpos;
7236 if (it->bidi_it.scan_dir < 0)
7237 stop = -1;
7238 composition_compute_stop_pos (&it->cmp_it,
7239 IT_STRING_CHARPOS (*it),
7240 IT_STRING_BYTEPOS (*it), stop,
7241 it->string);
7244 else
7246 for (i = 0; i < it->cmp_it.nchars; i++)
7247 bidi_move_to_visually_next (&it->bidi_it);
7248 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7249 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7250 if (it->cmp_it.from > 0)
7251 it->cmp_it.to = it->cmp_it.from;
7252 else
7254 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);
7264 else
7266 if (!it->bidi_p
7267 /* If the string position is beyond string's end, it
7268 means next_element_from_string is padding the string
7269 with blanks, in which case we bypass the bidi
7270 iterator, because it cannot deal with such virtual
7271 characters. */
7272 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7274 IT_STRING_BYTEPOS (*it) += it->len;
7275 IT_STRING_CHARPOS (*it) += 1;
7277 else
7279 int prev_scan_dir = it->bidi_it.scan_dir;
7281 bidi_move_to_visually_next (&it->bidi_it);
7282 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7283 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7284 if (prev_scan_dir != it->bidi_it.scan_dir)
7286 ptrdiff_t stop = it->end_charpos;
7288 if (it->bidi_it.scan_dir < 0)
7289 stop = -1;
7290 composition_compute_stop_pos (&it->cmp_it,
7291 IT_STRING_CHARPOS (*it),
7292 IT_STRING_BYTEPOS (*it), stop,
7293 it->string);
7298 consider_string_end:
7300 if (it->current.overlay_string_index >= 0)
7302 /* IT->string is an overlay string. Advance to the
7303 next, if there is one. */
7304 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7306 it->ellipsis_p = 0;
7307 next_overlay_string (it);
7308 if (it->ellipsis_p)
7309 setup_for_ellipsis (it, 0);
7312 else
7314 /* IT->string is not an overlay string. If we reached
7315 its end, and there is something on IT->stack, proceed
7316 with what is on the stack. This can be either another
7317 string, this time an overlay string, or a buffer. */
7318 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7319 && it->sp > 0)
7321 pop_it (it);
7322 if (it->method == GET_FROM_STRING)
7323 goto consider_string_end;
7326 break;
7328 case GET_FROM_IMAGE:
7329 case GET_FROM_STRETCH:
7330 /* The position etc with which we have to proceed are on
7331 the stack. The position may be at the end of a string,
7332 if the `display' property takes up the whole string. */
7333 eassert (it->sp > 0);
7334 pop_it (it);
7335 if (it->method == GET_FROM_STRING)
7336 goto consider_string_end;
7337 break;
7339 default:
7340 /* There are no other methods defined, so this should be a bug. */
7341 emacs_abort ();
7344 eassert (it->method != GET_FROM_STRING
7345 || (STRINGP (it->string)
7346 && IT_STRING_CHARPOS (*it) >= 0));
7349 /* Load IT's display element fields with information about the next
7350 display element which comes from a display table entry or from the
7351 result of translating a control character to one of the forms `^C'
7352 or `\003'.
7354 IT->dpvec holds the glyphs to return as characters.
7355 IT->saved_face_id holds the face id before the display vector--it
7356 is restored into IT->face_id in set_iterator_to_next. */
7358 static int
7359 next_element_from_display_vector (struct it *it)
7361 Lisp_Object gc;
7363 /* Precondition. */
7364 eassert (it->dpvec && it->current.dpvec_index >= 0);
7366 it->face_id = it->saved_face_id;
7368 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7369 That seemed totally bogus - so I changed it... */
7370 gc = it->dpvec[it->current.dpvec_index];
7372 if (GLYPH_CODE_P (gc))
7374 it->c = GLYPH_CODE_CHAR (gc);
7375 it->len = CHAR_BYTES (it->c);
7377 /* The entry may contain a face id to use. Such a face id is
7378 the id of a Lisp face, not a realized face. A face id of
7379 zero means no face is specified. */
7380 if (it->dpvec_face_id >= 0)
7381 it->face_id = it->dpvec_face_id;
7382 else
7384 int lface_id = GLYPH_CODE_FACE (gc);
7385 if (lface_id > 0)
7386 it->face_id = merge_faces (it->f, Qt, lface_id,
7387 it->saved_face_id);
7390 else
7391 /* Display table entry is invalid. Return a space. */
7392 it->c = ' ', it->len = 1;
7394 /* Don't change position and object of the iterator here. They are
7395 still the values of the character that had this display table
7396 entry or was translated, and that's what we want. */
7397 it->what = IT_CHARACTER;
7398 return 1;
7401 /* Get the first element of string/buffer in the visual order, after
7402 being reseated to a new position in a string or a buffer. */
7403 static void
7404 get_visually_first_element (struct it *it)
7406 int string_p = STRINGP (it->string) || it->s;
7407 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7408 ptrdiff_t bob = (string_p ? 0 : BEGV);
7410 if (STRINGP (it->string))
7412 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7413 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7415 else
7417 it->bidi_it.charpos = IT_CHARPOS (*it);
7418 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7421 if (it->bidi_it.charpos == eob)
7423 /* Nothing to do, but reset the FIRST_ELT flag, like
7424 bidi_paragraph_init does, because we are not going to
7425 call it. */
7426 it->bidi_it.first_elt = 0;
7428 else if (it->bidi_it.charpos == bob
7429 || (!string_p
7430 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7431 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7433 /* If we are at the beginning of a line/string, we can produce
7434 the next element right away. */
7435 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7436 bidi_move_to_visually_next (&it->bidi_it);
7438 else
7440 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7442 /* We need to prime the bidi iterator starting at the line's or
7443 string's beginning, before we will be able to produce the
7444 next element. */
7445 if (string_p)
7446 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7447 else
7448 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7449 IT_BYTEPOS (*it), -1,
7450 &it->bidi_it.bytepos);
7451 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7454 /* Now return to buffer/string position where we were asked
7455 to get the next display element, and produce that. */
7456 bidi_move_to_visually_next (&it->bidi_it);
7458 while (it->bidi_it.bytepos != orig_bytepos
7459 && it->bidi_it.charpos < eob);
7462 /* Adjust IT's position information to where we ended up. */
7463 if (STRINGP (it->string))
7465 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7466 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7468 else
7470 IT_CHARPOS (*it) = it->bidi_it.charpos;
7471 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7474 if (STRINGP (it->string) || !it->s)
7476 ptrdiff_t stop, charpos, bytepos;
7478 if (STRINGP (it->string))
7480 eassert (!it->s);
7481 stop = SCHARS (it->string);
7482 if (stop > it->end_charpos)
7483 stop = it->end_charpos;
7484 charpos = IT_STRING_CHARPOS (*it);
7485 bytepos = IT_STRING_BYTEPOS (*it);
7487 else
7489 stop = it->end_charpos;
7490 charpos = IT_CHARPOS (*it);
7491 bytepos = IT_BYTEPOS (*it);
7493 if (it->bidi_it.scan_dir < 0)
7494 stop = -1;
7495 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7496 it->string);
7500 /* Load IT with the next display element from Lisp string IT->string.
7501 IT->current.string_pos is the current position within the string.
7502 If IT->current.overlay_string_index >= 0, the Lisp string is an
7503 overlay string. */
7505 static int
7506 next_element_from_string (struct it *it)
7508 struct text_pos position;
7510 eassert (STRINGP (it->string));
7511 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7512 eassert (IT_STRING_CHARPOS (*it) >= 0);
7513 position = it->current.string_pos;
7515 /* With bidi reordering, the character to display might not be the
7516 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7517 that we were reseat()ed to a new string, whose paragraph
7518 direction is not known. */
7519 if (it->bidi_p && it->bidi_it.first_elt)
7521 get_visually_first_element (it);
7522 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7525 /* Time to check for invisible text? */
7526 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7528 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7530 if (!(!it->bidi_p
7531 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7532 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7534 /* With bidi non-linear iteration, we could find
7535 ourselves far beyond the last computed stop_charpos,
7536 with several other stop positions in between that we
7537 missed. Scan them all now, in buffer's logical
7538 order, until we find and handle the last stop_charpos
7539 that precedes our current position. */
7540 handle_stop_backwards (it, it->stop_charpos);
7541 return GET_NEXT_DISPLAY_ELEMENT (it);
7543 else
7545 if (it->bidi_p)
7547 /* Take note of the stop position we just moved
7548 across, for when we will move back across it. */
7549 it->prev_stop = it->stop_charpos;
7550 /* If we are at base paragraph embedding level, take
7551 note of the last stop position seen at this
7552 level. */
7553 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7554 it->base_level_stop = it->stop_charpos;
7556 handle_stop (it);
7558 /* Since a handler may have changed IT->method, we must
7559 recurse here. */
7560 return GET_NEXT_DISPLAY_ELEMENT (it);
7563 else if (it->bidi_p
7564 /* If we are before prev_stop, we may have overstepped
7565 on our way backwards a stop_pos, and if so, we need
7566 to handle that stop_pos. */
7567 && IT_STRING_CHARPOS (*it) < it->prev_stop
7568 /* We can sometimes back up for reasons that have nothing
7569 to do with bidi reordering. E.g., compositions. The
7570 code below is only needed when we are above the base
7571 embedding level, so test for that explicitly. */
7572 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7574 /* If we lost track of base_level_stop, we have no better
7575 place for handle_stop_backwards to start from than string
7576 beginning. This happens, e.g., when we were reseated to
7577 the previous screenful of text by vertical-motion. */
7578 if (it->base_level_stop <= 0
7579 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7580 it->base_level_stop = 0;
7581 handle_stop_backwards (it, it->base_level_stop);
7582 return GET_NEXT_DISPLAY_ELEMENT (it);
7586 if (it->current.overlay_string_index >= 0)
7588 /* Get the next character from an overlay string. In overlay
7589 strings, there is no field width or padding with spaces to
7590 do. */
7591 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7593 it->what = IT_EOB;
7594 return 0;
7596 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7597 IT_STRING_BYTEPOS (*it),
7598 it->bidi_it.scan_dir < 0
7599 ? -1
7600 : SCHARS (it->string))
7601 && next_element_from_composition (it))
7603 return 1;
7605 else if (STRING_MULTIBYTE (it->string))
7607 const unsigned char *s = (SDATA (it->string)
7608 + IT_STRING_BYTEPOS (*it));
7609 it->c = string_char_and_length (s, &it->len);
7611 else
7613 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7614 it->len = 1;
7617 else
7619 /* Get the next character from a Lisp string that is not an
7620 overlay string. Such strings come from the mode line, for
7621 example. We may have to pad with spaces, or truncate the
7622 string. See also next_element_from_c_string. */
7623 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7625 it->what = IT_EOB;
7626 return 0;
7628 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7630 /* Pad with spaces. */
7631 it->c = ' ', it->len = 1;
7632 CHARPOS (position) = BYTEPOS (position) = -1;
7634 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7635 IT_STRING_BYTEPOS (*it),
7636 it->bidi_it.scan_dir < 0
7637 ? -1
7638 : it->string_nchars)
7639 && next_element_from_composition (it))
7641 return 1;
7643 else if (STRING_MULTIBYTE (it->string))
7645 const unsigned char *s = (SDATA (it->string)
7646 + IT_STRING_BYTEPOS (*it));
7647 it->c = string_char_and_length (s, &it->len);
7649 else
7651 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7652 it->len = 1;
7656 /* Record what we have and where it came from. */
7657 it->what = IT_CHARACTER;
7658 it->object = it->string;
7659 it->position = position;
7660 return 1;
7664 /* Load IT with next display element from C string IT->s.
7665 IT->string_nchars is the maximum number of characters to return
7666 from the string. IT->end_charpos may be greater than
7667 IT->string_nchars when this function is called, in which case we
7668 may have to return padding spaces. Value is zero if end of string
7669 reached, including padding spaces. */
7671 static int
7672 next_element_from_c_string (struct it *it)
7674 int success_p = 1;
7676 eassert (it->s);
7677 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7678 it->what = IT_CHARACTER;
7679 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7680 it->object = Qnil;
7682 /* With bidi reordering, the character to display might not be the
7683 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7684 we were reseated to a new string, whose paragraph direction is
7685 not known. */
7686 if (it->bidi_p && it->bidi_it.first_elt)
7687 get_visually_first_element (it);
7689 /* IT's position can be greater than IT->string_nchars in case a
7690 field width or precision has been specified when the iterator was
7691 initialized. */
7692 if (IT_CHARPOS (*it) >= it->end_charpos)
7694 /* End of the game. */
7695 it->what = IT_EOB;
7696 success_p = 0;
7698 else if (IT_CHARPOS (*it) >= it->string_nchars)
7700 /* Pad with spaces. */
7701 it->c = ' ', it->len = 1;
7702 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7704 else if (it->multibyte_p)
7705 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7706 else
7707 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7709 return success_p;
7713 /* Set up IT to return characters from an ellipsis, if appropriate.
7714 The definition of the ellipsis glyphs may come from a display table
7715 entry. This function fills IT with the first glyph from the
7716 ellipsis if an ellipsis is to be displayed. */
7718 static int
7719 next_element_from_ellipsis (struct it *it)
7721 if (it->selective_display_ellipsis_p)
7722 setup_for_ellipsis (it, it->len);
7723 else
7725 /* The face at the current position may be different from the
7726 face we find after the invisible text. Remember what it
7727 was in IT->saved_face_id, and signal that it's there by
7728 setting face_before_selective_p. */
7729 it->saved_face_id = it->face_id;
7730 it->method = GET_FROM_BUFFER;
7731 it->object = it->w->contents;
7732 reseat_at_next_visible_line_start (it, 1);
7733 it->face_before_selective_p = 1;
7736 return GET_NEXT_DISPLAY_ELEMENT (it);
7740 /* Deliver an image display element. The iterator IT is already
7741 filled with image information (done in handle_display_prop). Value
7742 is always 1. */
7745 static int
7746 next_element_from_image (struct it *it)
7748 it->what = IT_IMAGE;
7749 it->ignore_overlay_strings_at_pos_p = 0;
7750 return 1;
7754 /* Fill iterator IT with next display element from a stretch glyph
7755 property. IT->object is the value of the text property. Value is
7756 always 1. */
7758 static int
7759 next_element_from_stretch (struct it *it)
7761 it->what = IT_STRETCH;
7762 return 1;
7765 /* Scan backwards from IT's current position until we find a stop
7766 position, or until BEGV. This is called when we find ourself
7767 before both the last known prev_stop and base_level_stop while
7768 reordering bidirectional text. */
7770 static void
7771 compute_stop_pos_backwards (struct it *it)
7773 const int SCAN_BACK_LIMIT = 1000;
7774 struct text_pos pos;
7775 struct display_pos save_current = it->current;
7776 struct text_pos save_position = it->position;
7777 ptrdiff_t charpos = IT_CHARPOS (*it);
7778 ptrdiff_t where_we_are = charpos;
7779 ptrdiff_t save_stop_pos = it->stop_charpos;
7780 ptrdiff_t save_end_pos = it->end_charpos;
7782 eassert (NILP (it->string) && !it->s);
7783 eassert (it->bidi_p);
7784 it->bidi_p = 0;
7787 it->end_charpos = min (charpos + 1, ZV);
7788 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7789 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7790 reseat_1 (it, pos, 0);
7791 compute_stop_pos (it);
7792 /* We must advance forward, right? */
7793 if (it->stop_charpos <= charpos)
7794 emacs_abort ();
7796 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7798 if (it->stop_charpos <= where_we_are)
7799 it->prev_stop = it->stop_charpos;
7800 else
7801 it->prev_stop = BEGV;
7802 it->bidi_p = 1;
7803 it->current = save_current;
7804 it->position = save_position;
7805 it->stop_charpos = save_stop_pos;
7806 it->end_charpos = save_end_pos;
7809 /* Scan forward from CHARPOS in the current buffer/string, until we
7810 find a stop position > current IT's position. Then handle the stop
7811 position before that. This is called when we bump into a stop
7812 position while reordering bidirectional text. CHARPOS should be
7813 the last previously processed stop_pos (or BEGV/0, if none were
7814 processed yet) whose position is less that IT's current
7815 position. */
7817 static void
7818 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7820 int bufp = !STRINGP (it->string);
7821 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7822 struct display_pos save_current = it->current;
7823 struct text_pos save_position = it->position;
7824 struct text_pos pos1;
7825 ptrdiff_t next_stop;
7827 /* Scan in strict logical order. */
7828 eassert (it->bidi_p);
7829 it->bidi_p = 0;
7832 it->prev_stop = charpos;
7833 if (bufp)
7835 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7836 reseat_1 (it, pos1, 0);
7838 else
7839 it->current.string_pos = string_pos (charpos, it->string);
7840 compute_stop_pos (it);
7841 /* We must advance forward, right? */
7842 if (it->stop_charpos <= it->prev_stop)
7843 emacs_abort ();
7844 charpos = it->stop_charpos;
7846 while (charpos <= where_we_are);
7848 it->bidi_p = 1;
7849 it->current = save_current;
7850 it->position = save_position;
7851 next_stop = it->stop_charpos;
7852 it->stop_charpos = it->prev_stop;
7853 handle_stop (it);
7854 it->stop_charpos = next_stop;
7857 /* Load IT with the next display element from current_buffer. Value
7858 is zero if end of buffer reached. IT->stop_charpos is the next
7859 position at which to stop and check for text properties or buffer
7860 end. */
7862 static int
7863 next_element_from_buffer (struct it *it)
7865 int success_p = 1;
7867 eassert (IT_CHARPOS (*it) >= BEGV);
7868 eassert (NILP (it->string) && !it->s);
7869 eassert (!it->bidi_p
7870 || (EQ (it->bidi_it.string.lstring, Qnil)
7871 && it->bidi_it.string.s == NULL));
7873 /* With bidi reordering, the character to display might not be the
7874 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7875 we were reseat()ed to a new buffer position, which is potentially
7876 a different paragraph. */
7877 if (it->bidi_p && it->bidi_it.first_elt)
7879 get_visually_first_element (it);
7880 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7883 if (IT_CHARPOS (*it) >= it->stop_charpos)
7885 if (IT_CHARPOS (*it) >= it->end_charpos)
7887 int overlay_strings_follow_p;
7889 /* End of the game, except when overlay strings follow that
7890 haven't been returned yet. */
7891 if (it->overlay_strings_at_end_processed_p)
7892 overlay_strings_follow_p = 0;
7893 else
7895 it->overlay_strings_at_end_processed_p = 1;
7896 overlay_strings_follow_p = get_overlay_strings (it, 0);
7899 if (overlay_strings_follow_p)
7900 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7901 else
7903 it->what = IT_EOB;
7904 it->position = it->current.pos;
7905 success_p = 0;
7908 else if (!(!it->bidi_p
7909 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7910 || IT_CHARPOS (*it) == it->stop_charpos))
7912 /* With bidi non-linear iteration, we could find ourselves
7913 far beyond the last computed stop_charpos, with several
7914 other stop positions in between that we missed. Scan
7915 them all now, in buffer's logical order, until we find
7916 and handle the last stop_charpos that precedes our
7917 current position. */
7918 handle_stop_backwards (it, it->stop_charpos);
7919 return GET_NEXT_DISPLAY_ELEMENT (it);
7921 else
7923 if (it->bidi_p)
7925 /* Take note of the stop position we just moved across,
7926 for when we will move back across it. */
7927 it->prev_stop = it->stop_charpos;
7928 /* If we are at base paragraph embedding level, take
7929 note of the last stop position seen at this
7930 level. */
7931 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7932 it->base_level_stop = it->stop_charpos;
7934 handle_stop (it);
7935 return GET_NEXT_DISPLAY_ELEMENT (it);
7938 else if (it->bidi_p
7939 /* If we are before prev_stop, we may have overstepped on
7940 our way backwards a stop_pos, and if so, we need to
7941 handle that stop_pos. */
7942 && IT_CHARPOS (*it) < it->prev_stop
7943 /* We can sometimes back up for reasons that have nothing
7944 to do with bidi reordering. E.g., compositions. The
7945 code below is only needed when we are above the base
7946 embedding level, so test for that explicitly. */
7947 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7949 if (it->base_level_stop <= 0
7950 || IT_CHARPOS (*it) < it->base_level_stop)
7952 /* If we lost track of base_level_stop, we need to find
7953 prev_stop by looking backwards. This happens, e.g., when
7954 we were reseated to the previous screenful of text by
7955 vertical-motion. */
7956 it->base_level_stop = BEGV;
7957 compute_stop_pos_backwards (it);
7958 handle_stop_backwards (it, it->prev_stop);
7960 else
7961 handle_stop_backwards (it, it->base_level_stop);
7962 return GET_NEXT_DISPLAY_ELEMENT (it);
7964 else
7966 /* No face changes, overlays etc. in sight, so just return a
7967 character from current_buffer. */
7968 unsigned char *p;
7969 ptrdiff_t stop;
7971 /* Maybe run the redisplay end trigger hook. Performance note:
7972 This doesn't seem to cost measurable time. */
7973 if (it->redisplay_end_trigger_charpos
7974 && it->glyph_row
7975 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7976 run_redisplay_end_trigger_hook (it);
7978 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7979 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7980 stop)
7981 && next_element_from_composition (it))
7983 return 1;
7986 /* Get the next character, maybe multibyte. */
7987 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7988 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7989 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7990 else
7991 it->c = *p, it->len = 1;
7993 /* Record what we have and where it came from. */
7994 it->what = IT_CHARACTER;
7995 it->object = it->w->contents;
7996 it->position = it->current.pos;
7998 /* Normally we return the character found above, except when we
7999 really want to return an ellipsis for selective display. */
8000 if (it->selective)
8002 if (it->c == '\n')
8004 /* A value of selective > 0 means hide lines indented more
8005 than that number of columns. */
8006 if (it->selective > 0
8007 && IT_CHARPOS (*it) + 1 < ZV
8008 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8009 IT_BYTEPOS (*it) + 1,
8010 it->selective))
8012 success_p = next_element_from_ellipsis (it);
8013 it->dpvec_char_len = -1;
8016 else if (it->c == '\r' && it->selective == -1)
8018 /* A value of selective == -1 means that everything from the
8019 CR to the end of the line is invisible, with maybe an
8020 ellipsis displayed for it. */
8021 success_p = next_element_from_ellipsis (it);
8022 it->dpvec_char_len = -1;
8027 /* Value is zero if end of buffer reached. */
8028 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8029 return success_p;
8033 /* Run the redisplay end trigger hook for IT. */
8035 static void
8036 run_redisplay_end_trigger_hook (struct it *it)
8038 Lisp_Object args[3];
8040 /* IT->glyph_row should be non-null, i.e. we should be actually
8041 displaying something, or otherwise we should not run the hook. */
8042 eassert (it->glyph_row);
8044 /* Set up hook arguments. */
8045 args[0] = Qredisplay_end_trigger_functions;
8046 args[1] = it->window;
8047 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8048 it->redisplay_end_trigger_charpos = 0;
8050 /* Since we are *trying* to run these functions, don't try to run
8051 them again, even if they get an error. */
8052 wset_redisplay_end_trigger (it->w, Qnil);
8053 Frun_hook_with_args (3, args);
8055 /* Notice if it changed the face of the character we are on. */
8056 handle_face_prop (it);
8060 /* Deliver a composition display element. Unlike the other
8061 next_element_from_XXX, this function is not registered in the array
8062 get_next_element[]. It is called from next_element_from_buffer and
8063 next_element_from_string when necessary. */
8065 static int
8066 next_element_from_composition (struct it *it)
8068 it->what = IT_COMPOSITION;
8069 it->len = it->cmp_it.nbytes;
8070 if (STRINGP (it->string))
8072 if (it->c < 0)
8074 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8075 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8076 return 0;
8078 it->position = it->current.string_pos;
8079 it->object = it->string;
8080 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8081 IT_STRING_BYTEPOS (*it), it->string);
8083 else
8085 if (it->c < 0)
8087 IT_CHARPOS (*it) += it->cmp_it.nchars;
8088 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8089 if (it->bidi_p)
8091 if (it->bidi_it.new_paragraph)
8092 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8093 /* Resync the bidi iterator with IT's new position.
8094 FIXME: this doesn't support bidirectional text. */
8095 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8096 bidi_move_to_visually_next (&it->bidi_it);
8098 return 0;
8100 it->position = it->current.pos;
8101 it->object = it->w->contents;
8102 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8103 IT_BYTEPOS (*it), Qnil);
8105 return 1;
8110 /***********************************************************************
8111 Moving an iterator without producing glyphs
8112 ***********************************************************************/
8114 /* Check if iterator is at a position corresponding to a valid buffer
8115 position after some move_it_ call. */
8117 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8118 ((it)->method == GET_FROM_STRING \
8119 ? IT_STRING_CHARPOS (*it) == 0 \
8120 : 1)
8123 /* Move iterator IT to a specified buffer or X position within one
8124 line on the display without producing glyphs.
8126 OP should be a bit mask including some or all of these bits:
8127 MOVE_TO_X: Stop upon reaching x-position TO_X.
8128 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8129 Regardless of OP's value, stop upon reaching the end of the display line.
8131 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8132 This means, in particular, that TO_X includes window's horizontal
8133 scroll amount.
8135 The return value has several possible values that
8136 say what condition caused the scan to stop:
8138 MOVE_POS_MATCH_OR_ZV
8139 - when TO_POS or ZV was reached.
8141 MOVE_X_REACHED
8142 -when TO_X was reached before TO_POS or ZV were reached.
8144 MOVE_LINE_CONTINUED
8145 - when we reached the end of the display area and the line must
8146 be continued.
8148 MOVE_LINE_TRUNCATED
8149 - when we reached the end of the display area and the line is
8150 truncated.
8152 MOVE_NEWLINE_OR_CR
8153 - when we stopped at a line end, i.e. a newline or a CR and selective
8154 display is on. */
8156 static enum move_it_result
8157 move_it_in_display_line_to (struct it *it,
8158 ptrdiff_t to_charpos, int to_x,
8159 enum move_operation_enum op)
8161 enum move_it_result result = MOVE_UNDEFINED;
8162 struct glyph_row *saved_glyph_row;
8163 struct it wrap_it, atpos_it, atx_it, ppos_it;
8164 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8165 void *ppos_data = NULL;
8166 int may_wrap = 0;
8167 enum it_method prev_method = it->method;
8168 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8169 int saw_smaller_pos = prev_pos < to_charpos;
8171 /* Don't produce glyphs in produce_glyphs. */
8172 saved_glyph_row = it->glyph_row;
8173 it->glyph_row = NULL;
8175 /* Use wrap_it to save a copy of IT wherever a word wrap could
8176 occur. Use atpos_it to save a copy of IT at the desired buffer
8177 position, if found, so that we can scan ahead and check if the
8178 word later overshoots the window edge. Use atx_it similarly, for
8179 pixel positions. */
8180 wrap_it.sp = -1;
8181 atpos_it.sp = -1;
8182 atx_it.sp = -1;
8184 /* Use ppos_it under bidi reordering to save a copy of IT for the
8185 position > CHARPOS that is the closest to CHARPOS. We restore
8186 that position in IT when we have scanned the entire display line
8187 without finding a match for CHARPOS and all the character
8188 positions are greater than CHARPOS. */
8189 if (it->bidi_p)
8191 SAVE_IT (ppos_it, *it, ppos_data);
8192 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8193 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8194 SAVE_IT (ppos_it, *it, ppos_data);
8197 #define BUFFER_POS_REACHED_P() \
8198 ((op & MOVE_TO_POS) != 0 \
8199 && BUFFERP (it->object) \
8200 && (IT_CHARPOS (*it) == to_charpos \
8201 || ((!it->bidi_p \
8202 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8203 && IT_CHARPOS (*it) > to_charpos) \
8204 || (it->what == IT_COMPOSITION \
8205 && ((IT_CHARPOS (*it) > to_charpos \
8206 && to_charpos >= it->cmp_it.charpos) \
8207 || (IT_CHARPOS (*it) < to_charpos \
8208 && to_charpos <= it->cmp_it.charpos)))) \
8209 && (it->method == GET_FROM_BUFFER \
8210 || (it->method == GET_FROM_DISPLAY_VECTOR \
8211 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8213 /* If there's a line-/wrap-prefix, handle it. */
8214 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8215 && it->current_y < it->last_visible_y)
8216 handle_line_prefix (it);
8218 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8219 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8221 while (1)
8223 int x, i, ascent = 0, descent = 0;
8225 /* Utility macro to reset an iterator with x, ascent, and descent. */
8226 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8227 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8228 (IT)->max_descent = descent)
8230 /* Stop if we move beyond TO_CHARPOS (after an image or a
8231 display string or stretch glyph). */
8232 if ((op & MOVE_TO_POS) != 0
8233 && BUFFERP (it->object)
8234 && it->method == GET_FROM_BUFFER
8235 && (((!it->bidi_p
8236 /* When the iterator is at base embedding level, we
8237 are guaranteed that characters are delivered for
8238 display in strictly increasing order of their
8239 buffer positions. */
8240 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8241 && IT_CHARPOS (*it) > to_charpos)
8242 || (it->bidi_p
8243 && (prev_method == GET_FROM_IMAGE
8244 || prev_method == GET_FROM_STRETCH
8245 || prev_method == GET_FROM_STRING)
8246 /* Passed TO_CHARPOS from left to right. */
8247 && ((prev_pos < to_charpos
8248 && IT_CHARPOS (*it) > to_charpos)
8249 /* Passed TO_CHARPOS from right to left. */
8250 || (prev_pos > to_charpos
8251 && IT_CHARPOS (*it) < to_charpos)))))
8253 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8255 result = MOVE_POS_MATCH_OR_ZV;
8256 break;
8258 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8259 /* If wrap_it is valid, the current position might be in a
8260 word that is wrapped. So, save the iterator in
8261 atpos_it and continue to see if wrapping happens. */
8262 SAVE_IT (atpos_it, *it, atpos_data);
8265 /* Stop when ZV reached.
8266 We used to stop here when TO_CHARPOS reached as well, but that is
8267 too soon if this glyph does not fit on this line. So we handle it
8268 explicitly below. */
8269 if (!get_next_display_element (it))
8271 result = MOVE_POS_MATCH_OR_ZV;
8272 break;
8275 if (it->line_wrap == TRUNCATE)
8277 if (BUFFER_POS_REACHED_P ())
8279 result = MOVE_POS_MATCH_OR_ZV;
8280 break;
8283 else
8285 if (it->line_wrap == WORD_WRAP)
8287 if (IT_DISPLAYING_WHITESPACE (it))
8288 may_wrap = 1;
8289 else if (may_wrap)
8291 /* We have reached a glyph that follows one or more
8292 whitespace characters. If the position is
8293 already found, we are done. */
8294 if (atpos_it.sp >= 0)
8296 RESTORE_IT (it, &atpos_it, atpos_data);
8297 result = MOVE_POS_MATCH_OR_ZV;
8298 goto done;
8300 if (atx_it.sp >= 0)
8302 RESTORE_IT (it, &atx_it, atx_data);
8303 result = MOVE_X_REACHED;
8304 goto done;
8306 /* Otherwise, we can wrap here. */
8307 SAVE_IT (wrap_it, *it, wrap_data);
8308 may_wrap = 0;
8313 /* Remember the line height for the current line, in case
8314 the next element doesn't fit on the line. */
8315 ascent = it->max_ascent;
8316 descent = it->max_descent;
8318 /* The call to produce_glyphs will get the metrics of the
8319 display element IT is loaded with. Record the x-position
8320 before this display element, in case it doesn't fit on the
8321 line. */
8322 x = it->current_x;
8324 PRODUCE_GLYPHS (it);
8326 if (it->area != TEXT_AREA)
8328 prev_method = it->method;
8329 if (it->method == GET_FROM_BUFFER)
8330 prev_pos = IT_CHARPOS (*it);
8331 set_iterator_to_next (it, 1);
8332 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8333 SET_TEXT_POS (this_line_min_pos,
8334 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8335 if (it->bidi_p
8336 && (op & MOVE_TO_POS)
8337 && IT_CHARPOS (*it) > to_charpos
8338 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8339 SAVE_IT (ppos_it, *it, ppos_data);
8340 continue;
8343 /* The number of glyphs we get back in IT->nglyphs will normally
8344 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8345 character on a terminal frame, or (iii) a line end. For the
8346 second case, IT->nglyphs - 1 padding glyphs will be present.
8347 (On X frames, there is only one glyph produced for a
8348 composite character.)
8350 The behavior implemented below means, for continuation lines,
8351 that as many spaces of a TAB as fit on the current line are
8352 displayed there. For terminal frames, as many glyphs of a
8353 multi-glyph character are displayed in the current line, too.
8354 This is what the old redisplay code did, and we keep it that
8355 way. Under X, the whole shape of a complex character must
8356 fit on the line or it will be completely displayed in the
8357 next line.
8359 Note that both for tabs and padding glyphs, all glyphs have
8360 the same width. */
8361 if (it->nglyphs)
8363 /* More than one glyph or glyph doesn't fit on line. All
8364 glyphs have the same width. */
8365 int single_glyph_width = it->pixel_width / it->nglyphs;
8366 int new_x;
8367 int x_before_this_char = x;
8368 int hpos_before_this_char = it->hpos;
8370 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8372 new_x = x + single_glyph_width;
8374 /* We want to leave anything reaching TO_X to the caller. */
8375 if ((op & MOVE_TO_X) && new_x > to_x)
8377 if (BUFFER_POS_REACHED_P ())
8379 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8380 goto buffer_pos_reached;
8381 if (atpos_it.sp < 0)
8383 SAVE_IT (atpos_it, *it, atpos_data);
8384 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8387 else
8389 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8391 it->current_x = x;
8392 result = MOVE_X_REACHED;
8393 break;
8395 if (atx_it.sp < 0)
8397 SAVE_IT (atx_it, *it, atx_data);
8398 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8403 if (/* Lines are continued. */
8404 it->line_wrap != TRUNCATE
8405 && (/* And glyph doesn't fit on the line. */
8406 new_x > it->last_visible_x
8407 /* Or it fits exactly and we're on a window
8408 system frame. */
8409 || (new_x == it->last_visible_x
8410 && FRAME_WINDOW_P (it->f)
8411 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8412 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8413 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8415 if (/* IT->hpos == 0 means the very first glyph
8416 doesn't fit on the line, e.g. a wide image. */
8417 it->hpos == 0
8418 || (new_x == it->last_visible_x
8419 && FRAME_WINDOW_P (it->f)))
8421 ++it->hpos;
8422 it->current_x = new_x;
8424 /* The character's last glyph just barely fits
8425 in this row. */
8426 if (i == it->nglyphs - 1)
8428 /* If this is the destination position,
8429 return a position *before* it in this row,
8430 now that we know it fits in this row. */
8431 if (BUFFER_POS_REACHED_P ())
8433 if (it->line_wrap != WORD_WRAP
8434 || wrap_it.sp < 0)
8436 it->hpos = hpos_before_this_char;
8437 it->current_x = x_before_this_char;
8438 result = MOVE_POS_MATCH_OR_ZV;
8439 break;
8441 if (it->line_wrap == WORD_WRAP
8442 && atpos_it.sp < 0)
8444 SAVE_IT (atpos_it, *it, atpos_data);
8445 atpos_it.current_x = x_before_this_char;
8446 atpos_it.hpos = hpos_before_this_char;
8450 prev_method = it->method;
8451 if (it->method == GET_FROM_BUFFER)
8452 prev_pos = IT_CHARPOS (*it);
8453 set_iterator_to_next (it, 1);
8454 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8455 SET_TEXT_POS (this_line_min_pos,
8456 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8457 /* On graphical terminals, newlines may
8458 "overflow" into the fringe if
8459 overflow-newline-into-fringe is non-nil.
8460 On text terminals, and on graphical
8461 terminals with no right margin, newlines
8462 may overflow into the last glyph on the
8463 display line.*/
8464 if (!FRAME_WINDOW_P (it->f)
8465 || ((it->bidi_p
8466 && it->bidi_it.paragraph_dir == R2L)
8467 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8468 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8469 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8471 if (!get_next_display_element (it))
8473 result = MOVE_POS_MATCH_OR_ZV;
8474 break;
8476 if (BUFFER_POS_REACHED_P ())
8478 if (ITERATOR_AT_END_OF_LINE_P (it))
8479 result = MOVE_POS_MATCH_OR_ZV;
8480 else
8481 result = MOVE_LINE_CONTINUED;
8482 break;
8484 if (ITERATOR_AT_END_OF_LINE_P (it))
8486 result = MOVE_NEWLINE_OR_CR;
8487 break;
8492 else
8493 IT_RESET_X_ASCENT_DESCENT (it);
8495 if (wrap_it.sp >= 0)
8497 RESTORE_IT (it, &wrap_it, wrap_data);
8498 atpos_it.sp = -1;
8499 atx_it.sp = -1;
8502 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8503 IT_CHARPOS (*it)));
8504 result = MOVE_LINE_CONTINUED;
8505 break;
8508 if (BUFFER_POS_REACHED_P ())
8510 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8511 goto buffer_pos_reached;
8512 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8514 SAVE_IT (atpos_it, *it, atpos_data);
8515 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8519 if (new_x > it->first_visible_x)
8521 /* Glyph is visible. Increment number of glyphs that
8522 would be displayed. */
8523 ++it->hpos;
8527 if (result != MOVE_UNDEFINED)
8528 break;
8530 else if (BUFFER_POS_REACHED_P ())
8532 buffer_pos_reached:
8533 IT_RESET_X_ASCENT_DESCENT (it);
8534 result = MOVE_POS_MATCH_OR_ZV;
8535 break;
8537 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8539 /* Stop when TO_X specified and reached. This check is
8540 necessary here because of lines consisting of a line end,
8541 only. The line end will not produce any glyphs and we
8542 would never get MOVE_X_REACHED. */
8543 eassert (it->nglyphs == 0);
8544 result = MOVE_X_REACHED;
8545 break;
8548 /* Is this a line end? If yes, we're done. */
8549 if (ITERATOR_AT_END_OF_LINE_P (it))
8551 /* If we are past TO_CHARPOS, but never saw any character
8552 positions smaller than TO_CHARPOS, return
8553 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8554 did. */
8555 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8557 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8559 if (IT_CHARPOS (ppos_it) < ZV)
8561 RESTORE_IT (it, &ppos_it, ppos_data);
8562 result = MOVE_POS_MATCH_OR_ZV;
8564 else
8565 goto buffer_pos_reached;
8567 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8568 && IT_CHARPOS (*it) > to_charpos)
8569 goto buffer_pos_reached;
8570 else
8571 result = MOVE_NEWLINE_OR_CR;
8573 else
8574 result = MOVE_NEWLINE_OR_CR;
8575 break;
8578 prev_method = it->method;
8579 if (it->method == GET_FROM_BUFFER)
8580 prev_pos = IT_CHARPOS (*it);
8581 /* The current display element has been consumed. Advance
8582 to the next. */
8583 set_iterator_to_next (it, 1);
8584 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8585 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8586 if (IT_CHARPOS (*it) < to_charpos)
8587 saw_smaller_pos = 1;
8588 if (it->bidi_p
8589 && (op & MOVE_TO_POS)
8590 && IT_CHARPOS (*it) >= to_charpos
8591 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8592 SAVE_IT (ppos_it, *it, ppos_data);
8594 /* Stop if lines are truncated and IT's current x-position is
8595 past the right edge of the window now. */
8596 if (it->line_wrap == TRUNCATE
8597 && it->current_x >= it->last_visible_x)
8599 if (!FRAME_WINDOW_P (it->f)
8600 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8601 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8602 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8603 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8605 int at_eob_p = 0;
8607 if ((at_eob_p = !get_next_display_element (it))
8608 || BUFFER_POS_REACHED_P ()
8609 /* If we are past TO_CHARPOS, but never saw any
8610 character positions smaller than TO_CHARPOS,
8611 return MOVE_POS_MATCH_OR_ZV, like the
8612 unidirectional display did. */
8613 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8614 && !saw_smaller_pos
8615 && IT_CHARPOS (*it) > to_charpos))
8617 if (it->bidi_p
8618 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8619 RESTORE_IT (it, &ppos_it, ppos_data);
8620 result = MOVE_POS_MATCH_OR_ZV;
8621 break;
8623 if (ITERATOR_AT_END_OF_LINE_P (it))
8625 result = MOVE_NEWLINE_OR_CR;
8626 break;
8629 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8630 && !saw_smaller_pos
8631 && IT_CHARPOS (*it) > to_charpos)
8633 if (IT_CHARPOS (ppos_it) < ZV)
8634 RESTORE_IT (it, &ppos_it, ppos_data);
8635 result = MOVE_POS_MATCH_OR_ZV;
8636 break;
8638 result = MOVE_LINE_TRUNCATED;
8639 break;
8641 #undef IT_RESET_X_ASCENT_DESCENT
8644 #undef BUFFER_POS_REACHED_P
8646 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8647 restore the saved iterator. */
8648 if (atpos_it.sp >= 0)
8649 RESTORE_IT (it, &atpos_it, atpos_data);
8650 else if (atx_it.sp >= 0)
8651 RESTORE_IT (it, &atx_it, atx_data);
8653 done:
8655 if (atpos_data)
8656 bidi_unshelve_cache (atpos_data, 1);
8657 if (atx_data)
8658 bidi_unshelve_cache (atx_data, 1);
8659 if (wrap_data)
8660 bidi_unshelve_cache (wrap_data, 1);
8661 if (ppos_data)
8662 bidi_unshelve_cache (ppos_data, 1);
8664 /* Restore the iterator settings altered at the beginning of this
8665 function. */
8666 it->glyph_row = saved_glyph_row;
8667 return result;
8670 /* For external use. */
8671 void
8672 move_it_in_display_line (struct it *it,
8673 ptrdiff_t to_charpos, int to_x,
8674 enum move_operation_enum op)
8676 if (it->line_wrap == WORD_WRAP
8677 && (op & MOVE_TO_X))
8679 struct it save_it;
8680 void *save_data = NULL;
8681 int skip;
8683 SAVE_IT (save_it, *it, save_data);
8684 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8685 /* When word-wrap is on, TO_X may lie past the end
8686 of a wrapped line. Then it->current is the
8687 character on the next line, so backtrack to the
8688 space before the wrap point. */
8689 if (skip == MOVE_LINE_CONTINUED)
8691 int prev_x = max (it->current_x - 1, 0);
8692 RESTORE_IT (it, &save_it, save_data);
8693 move_it_in_display_line_to
8694 (it, -1, prev_x, MOVE_TO_X);
8696 else
8697 bidi_unshelve_cache (save_data, 1);
8699 else
8700 move_it_in_display_line_to (it, to_charpos, to_x, op);
8704 /* Move IT forward until it satisfies one or more of the criteria in
8705 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8707 OP is a bit-mask that specifies where to stop, and in particular,
8708 which of those four position arguments makes a difference. See the
8709 description of enum move_operation_enum.
8711 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8712 screen line, this function will set IT to the next position that is
8713 displayed to the right of TO_CHARPOS on the screen. */
8715 void
8716 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8718 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8719 int line_height, line_start_x = 0, reached = 0;
8720 void *backup_data = NULL;
8722 for (;;)
8724 if (op & MOVE_TO_VPOS)
8726 /* If no TO_CHARPOS and no TO_X specified, stop at the
8727 start of the line TO_VPOS. */
8728 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8730 if (it->vpos == to_vpos)
8732 reached = 1;
8733 break;
8735 else
8736 skip = move_it_in_display_line_to (it, -1, -1, 0);
8738 else
8740 /* TO_VPOS >= 0 means stop at TO_X in the line at
8741 TO_VPOS, or at TO_POS, whichever comes first. */
8742 if (it->vpos == to_vpos)
8744 reached = 2;
8745 break;
8748 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8750 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8752 reached = 3;
8753 break;
8755 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8757 /* We have reached TO_X but not in the line we want. */
8758 skip = move_it_in_display_line_to (it, to_charpos,
8759 -1, MOVE_TO_POS);
8760 if (skip == MOVE_POS_MATCH_OR_ZV)
8762 reached = 4;
8763 break;
8768 else if (op & MOVE_TO_Y)
8770 struct it it_backup;
8772 if (it->line_wrap == WORD_WRAP)
8773 SAVE_IT (it_backup, *it, backup_data);
8775 /* TO_Y specified means stop at TO_X in the line containing
8776 TO_Y---or at TO_CHARPOS if this is reached first. The
8777 problem is that we can't really tell whether the line
8778 contains TO_Y before we have completely scanned it, and
8779 this may skip past TO_X. What we do is to first scan to
8780 TO_X.
8782 If TO_X is not specified, use a TO_X of zero. The reason
8783 is to make the outcome of this function more predictable.
8784 If we didn't use TO_X == 0, we would stop at the end of
8785 the line which is probably not what a caller would expect
8786 to happen. */
8787 skip = move_it_in_display_line_to
8788 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8789 (MOVE_TO_X | (op & MOVE_TO_POS)));
8791 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8792 if (skip == MOVE_POS_MATCH_OR_ZV)
8793 reached = 5;
8794 else if (skip == MOVE_X_REACHED)
8796 /* If TO_X was reached, we want to know whether TO_Y is
8797 in the line. We know this is the case if the already
8798 scanned glyphs make the line tall enough. Otherwise,
8799 we must check by scanning the rest of the line. */
8800 line_height = it->max_ascent + it->max_descent;
8801 if (to_y >= it->current_y
8802 && to_y < it->current_y + line_height)
8804 reached = 6;
8805 break;
8807 SAVE_IT (it_backup, *it, backup_data);
8808 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8809 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8810 op & MOVE_TO_POS);
8811 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8812 line_height = it->max_ascent + it->max_descent;
8813 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8815 if (to_y >= it->current_y
8816 && to_y < it->current_y + line_height)
8818 /* If TO_Y is in this line and TO_X was reached
8819 above, we scanned too far. We have to restore
8820 IT's settings to the ones before skipping. But
8821 keep the more accurate values of max_ascent and
8822 max_descent we've found while skipping the rest
8823 of the line, for the sake of callers, such as
8824 pos_visible_p, that need to know the line
8825 height. */
8826 int max_ascent = it->max_ascent;
8827 int max_descent = it->max_descent;
8829 RESTORE_IT (it, &it_backup, backup_data);
8830 it->max_ascent = max_ascent;
8831 it->max_descent = max_descent;
8832 reached = 6;
8834 else
8836 skip = skip2;
8837 if (skip == MOVE_POS_MATCH_OR_ZV)
8838 reached = 7;
8841 else
8843 /* Check whether TO_Y is in this line. */
8844 line_height = it->max_ascent + it->max_descent;
8845 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8847 if (to_y >= it->current_y
8848 && to_y < it->current_y + line_height)
8850 /* When word-wrap is on, TO_X may lie past the end
8851 of a wrapped line. Then it->current is the
8852 character on the next line, so backtrack to the
8853 space before the wrap point. */
8854 if (skip == MOVE_LINE_CONTINUED
8855 && it->line_wrap == WORD_WRAP)
8857 int prev_x = max (it->current_x - 1, 0);
8858 RESTORE_IT (it, &it_backup, backup_data);
8859 skip = move_it_in_display_line_to
8860 (it, -1, prev_x, MOVE_TO_X);
8862 reached = 6;
8866 if (reached)
8867 break;
8869 else if (BUFFERP (it->object)
8870 && (it->method == GET_FROM_BUFFER
8871 || it->method == GET_FROM_STRETCH)
8872 && IT_CHARPOS (*it) >= to_charpos
8873 /* Under bidi iteration, a call to set_iterator_to_next
8874 can scan far beyond to_charpos if the initial
8875 portion of the next line needs to be reordered. In
8876 that case, give move_it_in_display_line_to another
8877 chance below. */
8878 && !(it->bidi_p
8879 && it->bidi_it.scan_dir == -1))
8880 skip = MOVE_POS_MATCH_OR_ZV;
8881 else
8882 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8884 switch (skip)
8886 case MOVE_POS_MATCH_OR_ZV:
8887 reached = 8;
8888 goto out;
8890 case MOVE_NEWLINE_OR_CR:
8891 set_iterator_to_next (it, 1);
8892 it->continuation_lines_width = 0;
8893 break;
8895 case MOVE_LINE_TRUNCATED:
8896 it->continuation_lines_width = 0;
8897 reseat_at_next_visible_line_start (it, 0);
8898 if ((op & MOVE_TO_POS) != 0
8899 && IT_CHARPOS (*it) > to_charpos)
8901 reached = 9;
8902 goto out;
8904 break;
8906 case MOVE_LINE_CONTINUED:
8907 /* For continued lines ending in a tab, some of the glyphs
8908 associated with the tab are displayed on the current
8909 line. Since it->current_x does not include these glyphs,
8910 we use it->last_visible_x instead. */
8911 if (it->c == '\t')
8913 it->continuation_lines_width += it->last_visible_x;
8914 /* When moving by vpos, ensure that the iterator really
8915 advances to the next line (bug#847, bug#969). Fixme:
8916 do we need to do this in other circumstances? */
8917 if (it->current_x != it->last_visible_x
8918 && (op & MOVE_TO_VPOS)
8919 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8921 line_start_x = it->current_x + it->pixel_width
8922 - it->last_visible_x;
8923 set_iterator_to_next (it, 0);
8926 else
8927 it->continuation_lines_width += it->current_x;
8928 break;
8930 default:
8931 emacs_abort ();
8934 /* Reset/increment for the next run. */
8935 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8936 it->current_x = line_start_x;
8937 line_start_x = 0;
8938 it->hpos = 0;
8939 it->current_y += it->max_ascent + it->max_descent;
8940 ++it->vpos;
8941 last_height = it->max_ascent + it->max_descent;
8942 it->max_ascent = it->max_descent = 0;
8945 out:
8947 /* On text terminals, we may stop at the end of a line in the middle
8948 of a multi-character glyph. If the glyph itself is continued,
8949 i.e. it is actually displayed on the next line, don't treat this
8950 stopping point as valid; move to the next line instead (unless
8951 that brings us offscreen). */
8952 if (!FRAME_WINDOW_P (it->f)
8953 && op & MOVE_TO_POS
8954 && IT_CHARPOS (*it) == to_charpos
8955 && it->what == IT_CHARACTER
8956 && it->nglyphs > 1
8957 && it->line_wrap == WINDOW_WRAP
8958 && it->current_x == it->last_visible_x - 1
8959 && it->c != '\n'
8960 && it->c != '\t'
8961 && it->vpos < XFASTINT (it->w->window_end_vpos))
8963 it->continuation_lines_width += it->current_x;
8964 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8965 it->current_y += it->max_ascent + it->max_descent;
8966 ++it->vpos;
8967 last_height = it->max_ascent + it->max_descent;
8970 if (backup_data)
8971 bidi_unshelve_cache (backup_data, 1);
8973 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8977 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8979 If DY > 0, move IT backward at least that many pixels. DY = 0
8980 means move IT backward to the preceding line start or BEGV. This
8981 function may move over more than DY pixels if IT->current_y - DY
8982 ends up in the middle of a line; in this case IT->current_y will be
8983 set to the top of the line moved to. */
8985 void
8986 move_it_vertically_backward (struct it *it, int dy)
8988 int nlines, h;
8989 struct it it2, it3;
8990 void *it2data = NULL, *it3data = NULL;
8991 ptrdiff_t start_pos;
8992 int nchars_per_row
8993 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
8994 ptrdiff_t pos_limit;
8996 move_further_back:
8997 eassert (dy >= 0);
8999 start_pos = IT_CHARPOS (*it);
9001 /* Estimate how many newlines we must move back. */
9002 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9003 if (it->line_wrap == TRUNCATE)
9004 pos_limit = BEGV;
9005 else
9006 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9008 /* Set the iterator's position that many lines back. But don't go
9009 back more than NLINES full screen lines -- this wins a day with
9010 buffers which have very long lines. */
9011 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9012 back_to_previous_visible_line_start (it);
9014 /* Reseat the iterator here. When moving backward, we don't want
9015 reseat to skip forward over invisible text, set up the iterator
9016 to deliver from overlay strings at the new position etc. So,
9017 use reseat_1 here. */
9018 reseat_1 (it, it->current.pos, 1);
9020 /* We are now surely at a line start. */
9021 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9022 reordering is in effect. */
9023 it->continuation_lines_width = 0;
9025 /* Move forward and see what y-distance we moved. First move to the
9026 start of the next line so that we get its height. We need this
9027 height to be able to tell whether we reached the specified
9028 y-distance. */
9029 SAVE_IT (it2, *it, it2data);
9030 it2.max_ascent = it2.max_descent = 0;
9033 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9034 MOVE_TO_POS | MOVE_TO_VPOS);
9036 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9037 /* If we are in a display string which starts at START_POS,
9038 and that display string includes a newline, and we are
9039 right after that newline (i.e. at the beginning of a
9040 display line), exit the loop, because otherwise we will
9041 infloop, since move_it_to will see that it is already at
9042 START_POS and will not move. */
9043 || (it2.method == GET_FROM_STRING
9044 && IT_CHARPOS (it2) == start_pos
9045 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9046 eassert (IT_CHARPOS (*it) >= BEGV);
9047 SAVE_IT (it3, it2, it3data);
9049 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9050 eassert (IT_CHARPOS (*it) >= BEGV);
9051 /* H is the actual vertical distance from the position in *IT
9052 and the starting position. */
9053 h = it2.current_y - it->current_y;
9054 /* NLINES is the distance in number of lines. */
9055 nlines = it2.vpos - it->vpos;
9057 /* Correct IT's y and vpos position
9058 so that they are relative to the starting point. */
9059 it->vpos -= nlines;
9060 it->current_y -= h;
9062 if (dy == 0)
9064 /* DY == 0 means move to the start of the screen line. The
9065 value of nlines is > 0 if continuation lines were involved,
9066 or if the original IT position was at start of a line. */
9067 RESTORE_IT (it, it, it2data);
9068 if (nlines > 0)
9069 move_it_by_lines (it, nlines);
9070 /* The above code moves us to some position NLINES down,
9071 usually to its first glyph (leftmost in an L2R line), but
9072 that's not necessarily the start of the line, under bidi
9073 reordering. We want to get to the character position
9074 that is immediately after the newline of the previous
9075 line. */
9076 if (it->bidi_p
9077 && !it->continuation_lines_width
9078 && !STRINGP (it->string)
9079 && IT_CHARPOS (*it) > BEGV
9080 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9082 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9084 DEC_BOTH (cp, bp);
9085 cp = find_newline_no_quit (cp, bp, -1, NULL);
9086 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9088 bidi_unshelve_cache (it3data, 1);
9090 else
9092 /* The y-position we try to reach, relative to *IT.
9093 Note that H has been subtracted in front of the if-statement. */
9094 int target_y = it->current_y + h - dy;
9095 int y0 = it3.current_y;
9096 int y1;
9097 int line_height;
9099 RESTORE_IT (&it3, &it3, it3data);
9100 y1 = line_bottom_y (&it3);
9101 line_height = y1 - y0;
9102 RESTORE_IT (it, it, it2data);
9103 /* If we did not reach target_y, try to move further backward if
9104 we can. If we moved too far backward, try to move forward. */
9105 if (target_y < it->current_y
9106 /* This is heuristic. In a window that's 3 lines high, with
9107 a line height of 13 pixels each, recentering with point
9108 on the bottom line will try to move -39/2 = 19 pixels
9109 backward. Try to avoid moving into the first line. */
9110 && (it->current_y - target_y
9111 > min (window_box_height (it->w), line_height * 2 / 3))
9112 && IT_CHARPOS (*it) > BEGV)
9114 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9115 target_y - it->current_y));
9116 dy = it->current_y - target_y;
9117 goto move_further_back;
9119 else if (target_y >= it->current_y + line_height
9120 && IT_CHARPOS (*it) < ZV)
9122 /* Should move forward by at least one line, maybe more.
9124 Note: Calling move_it_by_lines can be expensive on
9125 terminal frames, where compute_motion is used (via
9126 vmotion) to do the job, when there are very long lines
9127 and truncate-lines is nil. That's the reason for
9128 treating terminal frames specially here. */
9130 if (!FRAME_WINDOW_P (it->f))
9131 move_it_vertically (it, target_y - (it->current_y + line_height));
9132 else
9136 move_it_by_lines (it, 1);
9138 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9145 /* Move IT by a specified amount of pixel lines DY. DY negative means
9146 move backwards. DY = 0 means move to start of screen line. At the
9147 end, IT will be on the start of a screen line. */
9149 void
9150 move_it_vertically (struct it *it, int dy)
9152 if (dy <= 0)
9153 move_it_vertically_backward (it, -dy);
9154 else
9156 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9157 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9158 MOVE_TO_POS | MOVE_TO_Y);
9159 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9161 /* If buffer ends in ZV without a newline, move to the start of
9162 the line to satisfy the post-condition. */
9163 if (IT_CHARPOS (*it) == ZV
9164 && ZV > BEGV
9165 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9166 move_it_by_lines (it, 0);
9171 /* Move iterator IT past the end of the text line it is in. */
9173 void
9174 move_it_past_eol (struct it *it)
9176 enum move_it_result rc;
9178 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9179 if (rc == MOVE_NEWLINE_OR_CR)
9180 set_iterator_to_next (it, 0);
9184 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9185 negative means move up. DVPOS == 0 means move to the start of the
9186 screen line.
9188 Optimization idea: If we would know that IT->f doesn't use
9189 a face with proportional font, we could be faster for
9190 truncate-lines nil. */
9192 void
9193 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9196 /* The commented-out optimization uses vmotion on terminals. This
9197 gives bad results, because elements like it->what, on which
9198 callers such as pos_visible_p rely, aren't updated. */
9199 /* struct position pos;
9200 if (!FRAME_WINDOW_P (it->f))
9202 struct text_pos textpos;
9204 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9205 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9206 reseat (it, textpos, 1);
9207 it->vpos += pos.vpos;
9208 it->current_y += pos.vpos;
9210 else */
9212 if (dvpos == 0)
9214 /* DVPOS == 0 means move to the start of the screen line. */
9215 move_it_vertically_backward (it, 0);
9216 /* Let next call to line_bottom_y calculate real line height */
9217 last_height = 0;
9219 else if (dvpos > 0)
9221 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9222 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9224 /* Only move to the next buffer position if we ended up in a
9225 string from display property, not in an overlay string
9226 (before-string or after-string). That is because the
9227 latter don't conceal the underlying buffer position, so
9228 we can ask to move the iterator to the exact position we
9229 are interested in. Note that, even if we are already at
9230 IT_CHARPOS (*it), the call below is not a no-op, as it
9231 will detect that we are at the end of the string, pop the
9232 iterator, and compute it->current_x and it->hpos
9233 correctly. */
9234 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9235 -1, -1, -1, MOVE_TO_POS);
9238 else
9240 struct it it2;
9241 void *it2data = NULL;
9242 ptrdiff_t start_charpos, i;
9243 int nchars_per_row
9244 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9245 ptrdiff_t pos_limit;
9247 /* Start at the beginning of the screen line containing IT's
9248 position. This may actually move vertically backwards,
9249 in case of overlays, so adjust dvpos accordingly. */
9250 dvpos += it->vpos;
9251 move_it_vertically_backward (it, 0);
9252 dvpos -= it->vpos;
9254 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9255 screen lines, and reseat the iterator there. */
9256 start_charpos = IT_CHARPOS (*it);
9257 if (it->line_wrap == TRUNCATE)
9258 pos_limit = BEGV;
9259 else
9260 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9261 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9262 back_to_previous_visible_line_start (it);
9263 reseat (it, it->current.pos, 1);
9265 /* Move further back if we end up in a string or an image. */
9266 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9268 /* First try to move to start of display line. */
9269 dvpos += it->vpos;
9270 move_it_vertically_backward (it, 0);
9271 dvpos -= it->vpos;
9272 if (IT_POS_VALID_AFTER_MOVE_P (it))
9273 break;
9274 /* If start of line is still in string or image,
9275 move further back. */
9276 back_to_previous_visible_line_start (it);
9277 reseat (it, it->current.pos, 1);
9278 dvpos--;
9281 it->current_x = it->hpos = 0;
9283 /* Above call may have moved too far if continuation lines
9284 are involved. Scan forward and see if it did. */
9285 SAVE_IT (it2, *it, it2data);
9286 it2.vpos = it2.current_y = 0;
9287 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9288 it->vpos -= it2.vpos;
9289 it->current_y -= it2.current_y;
9290 it->current_x = it->hpos = 0;
9292 /* If we moved too far back, move IT some lines forward. */
9293 if (it2.vpos > -dvpos)
9295 int delta = it2.vpos + dvpos;
9297 RESTORE_IT (&it2, &it2, it2data);
9298 SAVE_IT (it2, *it, it2data);
9299 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9300 /* Move back again if we got too far ahead. */
9301 if (IT_CHARPOS (*it) >= start_charpos)
9302 RESTORE_IT (it, &it2, it2data);
9303 else
9304 bidi_unshelve_cache (it2data, 1);
9306 else
9307 RESTORE_IT (it, it, it2data);
9311 /* Return 1 if IT points into the middle of a display vector. */
9314 in_display_vector_p (struct it *it)
9316 return (it->method == GET_FROM_DISPLAY_VECTOR
9317 && it->current.dpvec_index > 0
9318 && it->dpvec + it->current.dpvec_index != it->dpend);
9322 /***********************************************************************
9323 Messages
9324 ***********************************************************************/
9327 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9328 to *Messages*. */
9330 void
9331 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9333 Lisp_Object args[3];
9334 Lisp_Object msg, fmt;
9335 char *buffer;
9336 ptrdiff_t len;
9337 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9338 USE_SAFE_ALLOCA;
9340 fmt = msg = Qnil;
9341 GCPRO4 (fmt, msg, arg1, arg2);
9343 args[0] = fmt = build_string (format);
9344 args[1] = arg1;
9345 args[2] = arg2;
9346 msg = Fformat (3, args);
9348 len = SBYTES (msg) + 1;
9349 buffer = SAFE_ALLOCA (len);
9350 memcpy (buffer, SDATA (msg), len);
9352 message_dolog (buffer, len - 1, 1, 0);
9353 SAFE_FREE ();
9355 UNGCPRO;
9359 /* Output a newline in the *Messages* buffer if "needs" one. */
9361 void
9362 message_log_maybe_newline (void)
9364 if (message_log_need_newline)
9365 message_dolog ("", 0, 1, 0);
9369 /* Add a string M of length NBYTES to the message log, optionally
9370 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9371 true, means interpret the contents of M as multibyte. This
9372 function calls low-level routines in order to bypass text property
9373 hooks, etc. which might not be safe to run.
9375 This may GC (insert may run before/after change hooks),
9376 so the buffer M must NOT point to a Lisp string. */
9378 void
9379 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9381 const unsigned char *msg = (const unsigned char *) m;
9383 if (!NILP (Vmemory_full))
9384 return;
9386 if (!NILP (Vmessage_log_max))
9388 struct buffer *oldbuf;
9389 Lisp_Object oldpoint, oldbegv, oldzv;
9390 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9391 ptrdiff_t point_at_end = 0;
9392 ptrdiff_t zv_at_end = 0;
9393 Lisp_Object old_deactivate_mark;
9394 bool shown;
9395 struct gcpro gcpro1;
9397 old_deactivate_mark = Vdeactivate_mark;
9398 oldbuf = current_buffer;
9399 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9400 bset_undo_list (current_buffer, Qt);
9402 oldpoint = message_dolog_marker1;
9403 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9404 oldbegv = message_dolog_marker2;
9405 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9406 oldzv = message_dolog_marker3;
9407 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9408 GCPRO1 (old_deactivate_mark);
9410 if (PT == Z)
9411 point_at_end = 1;
9412 if (ZV == Z)
9413 zv_at_end = 1;
9415 BEGV = BEG;
9416 BEGV_BYTE = BEG_BYTE;
9417 ZV = Z;
9418 ZV_BYTE = Z_BYTE;
9419 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9421 /* Insert the string--maybe converting multibyte to single byte
9422 or vice versa, so that all the text fits the buffer. */
9423 if (multibyte
9424 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9426 ptrdiff_t i;
9427 int c, char_bytes;
9428 char work[1];
9430 /* Convert a multibyte string to single-byte
9431 for the *Message* buffer. */
9432 for (i = 0; i < nbytes; i += char_bytes)
9434 c = string_char_and_length (msg + i, &char_bytes);
9435 work[0] = (ASCII_CHAR_P (c)
9437 : multibyte_char_to_unibyte (c));
9438 insert_1_both (work, 1, 1, 1, 0, 0);
9441 else if (! multibyte
9442 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9444 ptrdiff_t i;
9445 int c, char_bytes;
9446 unsigned char str[MAX_MULTIBYTE_LENGTH];
9447 /* Convert a single-byte string to multibyte
9448 for the *Message* buffer. */
9449 for (i = 0; i < nbytes; i++)
9451 c = msg[i];
9452 MAKE_CHAR_MULTIBYTE (c);
9453 char_bytes = CHAR_STRING (c, str);
9454 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9457 else if (nbytes)
9458 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9460 if (nlflag)
9462 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9463 printmax_t dups;
9465 insert_1_both ("\n", 1, 1, 1, 0, 0);
9467 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9468 this_bol = PT;
9469 this_bol_byte = PT_BYTE;
9471 /* See if this line duplicates the previous one.
9472 If so, combine duplicates. */
9473 if (this_bol > BEG)
9475 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9476 prev_bol = PT;
9477 prev_bol_byte = PT_BYTE;
9479 dups = message_log_check_duplicate (prev_bol_byte,
9480 this_bol_byte);
9481 if (dups)
9483 del_range_both (prev_bol, prev_bol_byte,
9484 this_bol, this_bol_byte, 0);
9485 if (dups > 1)
9487 char dupstr[sizeof " [ times]"
9488 + INT_STRLEN_BOUND (printmax_t)];
9490 /* If you change this format, don't forget to also
9491 change message_log_check_duplicate. */
9492 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9493 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9494 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9499 /* If we have more than the desired maximum number of lines
9500 in the *Messages* buffer now, delete the oldest ones.
9501 This is safe because we don't have undo in this buffer. */
9503 if (NATNUMP (Vmessage_log_max))
9505 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9506 -XFASTINT (Vmessage_log_max) - 1, 0);
9507 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9510 BEGV = marker_position (oldbegv);
9511 BEGV_BYTE = marker_byte_position (oldbegv);
9513 if (zv_at_end)
9515 ZV = Z;
9516 ZV_BYTE = Z_BYTE;
9518 else
9520 ZV = marker_position (oldzv);
9521 ZV_BYTE = marker_byte_position (oldzv);
9524 if (point_at_end)
9525 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9526 else
9527 /* We can't do Fgoto_char (oldpoint) because it will run some
9528 Lisp code. */
9529 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9530 marker_byte_position (oldpoint));
9532 UNGCPRO;
9533 unchain_marker (XMARKER (oldpoint));
9534 unchain_marker (XMARKER (oldbegv));
9535 unchain_marker (XMARKER (oldzv));
9537 shown = buffer_window_count (current_buffer) > 0;
9538 set_buffer_internal (oldbuf);
9539 if (!shown)
9540 windows_or_buffers_changed = old_windows_or_buffers_changed;
9541 message_log_need_newline = !nlflag;
9542 Vdeactivate_mark = old_deactivate_mark;
9547 /* We are at the end of the buffer after just having inserted a newline.
9548 (Note: We depend on the fact we won't be crossing the gap.)
9549 Check to see if the most recent message looks a lot like the previous one.
9550 Return 0 if different, 1 if the new one should just replace it, or a
9551 value N > 1 if we should also append " [N times]". */
9553 static intmax_t
9554 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9556 ptrdiff_t i;
9557 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9558 int seen_dots = 0;
9559 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9560 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9562 for (i = 0; i < len; i++)
9564 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9565 seen_dots = 1;
9566 if (p1[i] != p2[i])
9567 return seen_dots;
9569 p1 += len;
9570 if (*p1 == '\n')
9571 return 2;
9572 if (*p1++ == ' ' && *p1++ == '[')
9574 char *pend;
9575 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9576 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9577 return n + 1;
9579 return 0;
9583 /* Display an echo area message M with a specified length of NBYTES
9584 bytes. The string may include null characters. If M is not a
9585 string, clear out any existing message, and let the mini-buffer
9586 text show through.
9588 This function cancels echoing. */
9590 void
9591 message3 (Lisp_Object m)
9593 struct gcpro gcpro1;
9595 GCPRO1 (m);
9596 clear_message (1,1);
9597 cancel_echoing ();
9599 /* First flush out any partial line written with print. */
9600 message_log_maybe_newline ();
9601 if (STRINGP (m))
9603 ptrdiff_t nbytes = SBYTES (m);
9604 bool multibyte = STRING_MULTIBYTE (m);
9605 USE_SAFE_ALLOCA;
9606 char *buffer = SAFE_ALLOCA (nbytes);
9607 memcpy (buffer, SDATA (m), nbytes);
9608 message_dolog (buffer, nbytes, 1, multibyte);
9609 SAFE_FREE ();
9611 message3_nolog (m);
9613 UNGCPRO;
9617 /* The non-logging version of message3.
9618 This does not cancel echoing, because it is used for echoing.
9619 Perhaps we need to make a separate function for echoing
9620 and make this cancel echoing. */
9622 void
9623 message3_nolog (Lisp_Object m)
9625 struct frame *sf = SELECTED_FRAME ();
9627 if (FRAME_INITIAL_P (sf))
9629 if (noninteractive_need_newline)
9630 putc ('\n', stderr);
9631 noninteractive_need_newline = 0;
9632 if (STRINGP (m))
9633 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9634 if (cursor_in_echo_area == 0)
9635 fprintf (stderr, "\n");
9636 fflush (stderr);
9638 /* Error messages get reported properly by cmd_error, so this must be just an
9639 informative message; if the frame hasn't really been initialized yet, just
9640 toss it. */
9641 else if (INTERACTIVE && sf->glyphs_initialized_p)
9643 /* Get the frame containing the mini-buffer
9644 that the selected frame is using. */
9645 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9646 Lisp_Object frame = XWINDOW (mini_window)->frame;
9647 struct frame *f = XFRAME (frame);
9649 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9650 Fmake_frame_visible (frame);
9652 if (STRINGP (m) && SCHARS (m) > 0)
9654 set_message (m);
9655 if (minibuffer_auto_raise)
9656 Fraise_frame (frame);
9657 /* Assume we are not echoing.
9658 (If we are, echo_now will override this.) */
9659 echo_message_buffer = Qnil;
9661 else
9662 clear_message (1, 1);
9664 do_pending_window_change (0);
9665 echo_area_display (1);
9666 do_pending_window_change (0);
9667 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9668 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9673 /* Display a null-terminated echo area message M. If M is 0, clear
9674 out any existing message, and let the mini-buffer text show through.
9676 The buffer M must continue to exist until after the echo area gets
9677 cleared or some other message gets displayed there. Do not pass
9678 text that is stored in a Lisp string. Do not pass text in a buffer
9679 that was alloca'd. */
9681 void
9682 message1 (const char *m)
9684 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9688 /* The non-logging counterpart of message1. */
9690 void
9691 message1_nolog (const char *m)
9693 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9696 /* Display a message M which contains a single %s
9697 which gets replaced with STRING. */
9699 void
9700 message_with_string (const char *m, Lisp_Object string, int log)
9702 CHECK_STRING (string);
9704 if (noninteractive)
9706 if (m)
9708 if (noninteractive_need_newline)
9709 putc ('\n', stderr);
9710 noninteractive_need_newline = 0;
9711 fprintf (stderr, m, SDATA (string));
9712 if (!cursor_in_echo_area)
9713 fprintf (stderr, "\n");
9714 fflush (stderr);
9717 else if (INTERACTIVE)
9719 /* The frame whose minibuffer we're going to display the message on.
9720 It may be larger than the selected frame, so we need
9721 to use its buffer, not the selected frame's buffer. */
9722 Lisp_Object mini_window;
9723 struct frame *f, *sf = SELECTED_FRAME ();
9725 /* Get the frame containing the minibuffer
9726 that the selected frame is using. */
9727 mini_window = FRAME_MINIBUF_WINDOW (sf);
9728 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9730 /* Error messages get reported properly by cmd_error, so this must be
9731 just an informative message; if the frame hasn't really been
9732 initialized yet, just toss it. */
9733 if (f->glyphs_initialized_p)
9735 Lisp_Object args[2], msg;
9736 struct gcpro gcpro1, gcpro2;
9738 args[0] = build_string (m);
9739 args[1] = msg = string;
9740 GCPRO2 (args[0], msg);
9741 gcpro1.nvars = 2;
9743 msg = Fformat (2, args);
9745 if (log)
9746 message3 (msg);
9747 else
9748 message3_nolog (msg);
9750 UNGCPRO;
9752 /* Print should start at the beginning of the message
9753 buffer next time. */
9754 message_buf_print = 0;
9760 /* Dump an informative message to the minibuf. If M is 0, clear out
9761 any existing message, and let the mini-buffer text show through. */
9763 static void
9764 vmessage (const char *m, va_list ap)
9766 if (noninteractive)
9768 if (m)
9770 if (noninteractive_need_newline)
9771 putc ('\n', stderr);
9772 noninteractive_need_newline = 0;
9773 vfprintf (stderr, m, ap);
9774 if (cursor_in_echo_area == 0)
9775 fprintf (stderr, "\n");
9776 fflush (stderr);
9779 else if (INTERACTIVE)
9781 /* The frame whose mini-buffer we're going to display the message
9782 on. It may be larger than the selected frame, so we need to
9783 use its buffer, not the selected frame's buffer. */
9784 Lisp_Object mini_window;
9785 struct frame *f, *sf = SELECTED_FRAME ();
9787 /* Get the frame containing the mini-buffer
9788 that the selected frame is using. */
9789 mini_window = FRAME_MINIBUF_WINDOW (sf);
9790 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9792 /* Error messages get reported properly by cmd_error, so this must be
9793 just an informative message; if the frame hasn't really been
9794 initialized yet, just toss it. */
9795 if (f->glyphs_initialized_p)
9797 if (m)
9799 ptrdiff_t len;
9800 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9801 char *message_buf = alloca (maxsize + 1);
9803 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9805 message3 (make_string (message_buf, len));
9807 else
9808 message1 (0);
9810 /* Print should start at the beginning of the message
9811 buffer next time. */
9812 message_buf_print = 0;
9817 void
9818 message (const char *m, ...)
9820 va_list ap;
9821 va_start (ap, m);
9822 vmessage (m, ap);
9823 va_end (ap);
9827 #if 0
9828 /* The non-logging version of message. */
9830 void
9831 message_nolog (const char *m, ...)
9833 Lisp_Object old_log_max;
9834 va_list ap;
9835 va_start (ap, m);
9836 old_log_max = Vmessage_log_max;
9837 Vmessage_log_max = Qnil;
9838 vmessage (m, ap);
9839 Vmessage_log_max = old_log_max;
9840 va_end (ap);
9842 #endif
9845 /* Display the current message in the current mini-buffer. This is
9846 only called from error handlers in process.c, and is not time
9847 critical. */
9849 void
9850 update_echo_area (void)
9852 if (!NILP (echo_area_buffer[0]))
9854 Lisp_Object string;
9855 string = Fcurrent_message ();
9856 message3 (string);
9861 /* Make sure echo area buffers in `echo_buffers' are live.
9862 If they aren't, make new ones. */
9864 static void
9865 ensure_echo_area_buffers (void)
9867 int i;
9869 for (i = 0; i < 2; ++i)
9870 if (!BUFFERP (echo_buffer[i])
9871 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9873 char name[30];
9874 Lisp_Object old_buffer;
9875 int j;
9877 old_buffer = echo_buffer[i];
9878 echo_buffer[i] = Fget_buffer_create
9879 (make_formatted_string (name, " *Echo Area %d*", i));
9880 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9881 /* to force word wrap in echo area -
9882 it was decided to postpone this*/
9883 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9885 for (j = 0; j < 2; ++j)
9886 if (EQ (old_buffer, echo_area_buffer[j]))
9887 echo_area_buffer[j] = echo_buffer[i];
9892 /* Call FN with args A1..A2 with either the current or last displayed
9893 echo_area_buffer as current buffer.
9895 WHICH zero means use the current message buffer
9896 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9897 from echo_buffer[] and clear it.
9899 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9900 suitable buffer from echo_buffer[] and clear it.
9902 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9903 that the current message becomes the last displayed one, make
9904 choose a suitable buffer for echo_area_buffer[0], and clear it.
9906 Value is what FN returns. */
9908 static int
9909 with_echo_area_buffer (struct window *w, int which,
9910 int (*fn) (ptrdiff_t, Lisp_Object),
9911 ptrdiff_t a1, Lisp_Object a2)
9913 Lisp_Object buffer;
9914 int this_one, the_other, clear_buffer_p, rc;
9915 ptrdiff_t count = SPECPDL_INDEX ();
9917 /* If buffers aren't live, make new ones. */
9918 ensure_echo_area_buffers ();
9920 clear_buffer_p = 0;
9922 if (which == 0)
9923 this_one = 0, the_other = 1;
9924 else if (which > 0)
9925 this_one = 1, the_other = 0;
9926 else
9928 this_one = 0, the_other = 1;
9929 clear_buffer_p = 1;
9931 /* We need a fresh one in case the current echo buffer equals
9932 the one containing the last displayed echo area message. */
9933 if (!NILP (echo_area_buffer[this_one])
9934 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9935 echo_area_buffer[this_one] = Qnil;
9938 /* Choose a suitable buffer from echo_buffer[] is we don't
9939 have one. */
9940 if (NILP (echo_area_buffer[this_one]))
9942 echo_area_buffer[this_one]
9943 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9944 ? echo_buffer[the_other]
9945 : echo_buffer[this_one]);
9946 clear_buffer_p = 1;
9949 buffer = echo_area_buffer[this_one];
9951 /* Don't get confused by reusing the buffer used for echoing
9952 for a different purpose. */
9953 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9954 cancel_echoing ();
9956 record_unwind_protect (unwind_with_echo_area_buffer,
9957 with_echo_area_buffer_unwind_data (w));
9959 /* Make the echo area buffer current. Note that for display
9960 purposes, it is not necessary that the displayed window's buffer
9961 == current_buffer, except for text property lookup. So, let's
9962 only set that buffer temporarily here without doing a full
9963 Fset_window_buffer. We must also change w->pointm, though,
9964 because otherwise an assertions in unshow_buffer fails, and Emacs
9965 aborts. */
9966 set_buffer_internal_1 (XBUFFER (buffer));
9967 if (w)
9969 wset_buffer (w, buffer);
9970 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9973 bset_undo_list (current_buffer, Qt);
9974 bset_read_only (current_buffer, Qnil);
9975 specbind (Qinhibit_read_only, Qt);
9976 specbind (Qinhibit_modification_hooks, Qt);
9978 if (clear_buffer_p && Z > BEG)
9979 del_range (BEG, Z);
9981 eassert (BEGV >= BEG);
9982 eassert (ZV <= Z && ZV >= BEGV);
9984 rc = fn (a1, a2);
9986 eassert (BEGV >= BEG);
9987 eassert (ZV <= Z && ZV >= BEGV);
9989 unbind_to (count, Qnil);
9990 return rc;
9994 /* Save state that should be preserved around the call to the function
9995 FN called in with_echo_area_buffer. */
9997 static Lisp_Object
9998 with_echo_area_buffer_unwind_data (struct window *w)
10000 int i = 0;
10001 Lisp_Object vector, tmp;
10003 /* Reduce consing by keeping one vector in
10004 Vwith_echo_area_save_vector. */
10005 vector = Vwith_echo_area_save_vector;
10006 Vwith_echo_area_save_vector = Qnil;
10008 if (NILP (vector))
10009 vector = Fmake_vector (make_number (9), Qnil);
10011 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10012 ASET (vector, i, Vdeactivate_mark); ++i;
10013 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10015 if (w)
10017 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10018 ASET (vector, i, w->contents); ++i;
10019 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10020 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10021 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10022 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10024 else
10026 int end = i + 6;
10027 for (; i < end; ++i)
10028 ASET (vector, i, Qnil);
10031 eassert (i == ASIZE (vector));
10032 return vector;
10036 /* Restore global state from VECTOR which was created by
10037 with_echo_area_buffer_unwind_data. */
10039 static Lisp_Object
10040 unwind_with_echo_area_buffer (Lisp_Object vector)
10042 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10043 Vdeactivate_mark = AREF (vector, 1);
10044 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10046 if (WINDOWP (AREF (vector, 3)))
10048 struct window *w;
10049 Lisp_Object buffer;
10051 w = XWINDOW (AREF (vector, 3));
10052 buffer = AREF (vector, 4);
10054 wset_buffer (w, buffer);
10055 set_marker_both (w->pointm, buffer,
10056 XFASTINT (AREF (vector, 5)),
10057 XFASTINT (AREF (vector, 6)));
10058 set_marker_both (w->start, buffer,
10059 XFASTINT (AREF (vector, 7)),
10060 XFASTINT (AREF (vector, 8)));
10063 Vwith_echo_area_save_vector = vector;
10064 return Qnil;
10068 /* Set up the echo area for use by print functions. MULTIBYTE_P
10069 non-zero means we will print multibyte. */
10071 void
10072 setup_echo_area_for_printing (int multibyte_p)
10074 /* If we can't find an echo area any more, exit. */
10075 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10076 Fkill_emacs (Qnil);
10078 ensure_echo_area_buffers ();
10080 if (!message_buf_print)
10082 /* A message has been output since the last time we printed.
10083 Choose a fresh echo area buffer. */
10084 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10085 echo_area_buffer[0] = echo_buffer[1];
10086 else
10087 echo_area_buffer[0] = echo_buffer[0];
10089 /* Switch to that buffer and clear it. */
10090 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10091 bset_truncate_lines (current_buffer, Qnil);
10093 if (Z > BEG)
10095 ptrdiff_t count = SPECPDL_INDEX ();
10096 specbind (Qinhibit_read_only, Qt);
10097 /* Note that undo recording is always disabled. */
10098 del_range (BEG, Z);
10099 unbind_to (count, Qnil);
10101 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10103 /* Set up the buffer for the multibyteness we need. */
10104 if (multibyte_p
10105 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10106 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10108 /* Raise the frame containing the echo area. */
10109 if (minibuffer_auto_raise)
10111 struct frame *sf = SELECTED_FRAME ();
10112 Lisp_Object mini_window;
10113 mini_window = FRAME_MINIBUF_WINDOW (sf);
10114 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10117 message_log_maybe_newline ();
10118 message_buf_print = 1;
10120 else
10122 if (NILP (echo_area_buffer[0]))
10124 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10125 echo_area_buffer[0] = echo_buffer[1];
10126 else
10127 echo_area_buffer[0] = echo_buffer[0];
10130 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10132 /* Someone switched buffers between print requests. */
10133 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10134 bset_truncate_lines (current_buffer, Qnil);
10140 /* Display an echo area message in window W. Value is non-zero if W's
10141 height is changed. If display_last_displayed_message_p is
10142 non-zero, display the message that was last displayed, otherwise
10143 display the current message. */
10145 static int
10146 display_echo_area (struct window *w)
10148 int i, no_message_p, window_height_changed_p;
10150 /* Temporarily disable garbage collections while displaying the echo
10151 area. This is done because a GC can print a message itself.
10152 That message would modify the echo area buffer's contents while a
10153 redisplay of the buffer is going on, and seriously confuse
10154 redisplay. */
10155 ptrdiff_t count = inhibit_garbage_collection ();
10157 /* If there is no message, we must call display_echo_area_1
10158 nevertheless because it resizes the window. But we will have to
10159 reset the echo_area_buffer in question to nil at the end because
10160 with_echo_area_buffer will sets it to an empty buffer. */
10161 i = display_last_displayed_message_p ? 1 : 0;
10162 no_message_p = NILP (echo_area_buffer[i]);
10164 window_height_changed_p
10165 = with_echo_area_buffer (w, display_last_displayed_message_p,
10166 display_echo_area_1,
10167 (intptr_t) w, Qnil);
10169 if (no_message_p)
10170 echo_area_buffer[i] = Qnil;
10172 unbind_to (count, Qnil);
10173 return window_height_changed_p;
10177 /* Helper for display_echo_area. Display the current buffer which
10178 contains the current echo area message in window W, a mini-window,
10179 a pointer to which is passed in A1. A2..A4 are currently not used.
10180 Change the height of W so that all of the message is displayed.
10181 Value is non-zero if height of W was changed. */
10183 static int
10184 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10186 intptr_t i1 = a1;
10187 struct window *w = (struct window *) i1;
10188 Lisp_Object window;
10189 struct text_pos start;
10190 int window_height_changed_p = 0;
10192 /* Do this before displaying, so that we have a large enough glyph
10193 matrix for the display. If we can't get enough space for the
10194 whole text, display the last N lines. That works by setting w->start. */
10195 window_height_changed_p = resize_mini_window (w, 0);
10197 /* Use the starting position chosen by resize_mini_window. */
10198 SET_TEXT_POS_FROM_MARKER (start, w->start);
10200 /* Display. */
10201 clear_glyph_matrix (w->desired_matrix);
10202 XSETWINDOW (window, w);
10203 try_window (window, start, 0);
10205 return window_height_changed_p;
10209 /* Resize the echo area window to exactly the size needed for the
10210 currently displayed message, if there is one. If a mini-buffer
10211 is active, don't shrink it. */
10213 void
10214 resize_echo_area_exactly (void)
10216 if (BUFFERP (echo_area_buffer[0])
10217 && WINDOWP (echo_area_window))
10219 struct window *w = XWINDOW (echo_area_window);
10220 int resized_p;
10221 Lisp_Object resize_exactly;
10223 if (minibuf_level == 0)
10224 resize_exactly = Qt;
10225 else
10226 resize_exactly = Qnil;
10228 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10229 (intptr_t) w, resize_exactly);
10230 if (resized_p)
10232 ++windows_or_buffers_changed;
10233 ++update_mode_lines;
10234 redisplay_internal ();
10240 /* Callback function for with_echo_area_buffer, when used from
10241 resize_echo_area_exactly. A1 contains a pointer to the window to
10242 resize, EXACTLY non-nil means resize the mini-window exactly to the
10243 size of the text displayed. A3 and A4 are not used. Value is what
10244 resize_mini_window returns. */
10246 static int
10247 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10249 intptr_t i1 = a1;
10250 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10254 /* Resize mini-window W to fit the size of its contents. EXACT_P
10255 means size the window exactly to the size needed. Otherwise, it's
10256 only enlarged until W's buffer is empty.
10258 Set W->start to the right place to begin display. If the whole
10259 contents fit, start at the beginning. Otherwise, start so as
10260 to make the end of the contents appear. This is particularly
10261 important for y-or-n-p, but seems desirable generally.
10263 Value is non-zero if the window height has been changed. */
10266 resize_mini_window (struct window *w, int exact_p)
10268 struct frame *f = XFRAME (w->frame);
10269 int window_height_changed_p = 0;
10271 eassert (MINI_WINDOW_P (w));
10273 /* By default, start display at the beginning. */
10274 set_marker_both (w->start, w->contents,
10275 BUF_BEGV (XBUFFER (w->contents)),
10276 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10278 /* Don't resize windows while redisplaying a window; it would
10279 confuse redisplay functions when the size of the window they are
10280 displaying changes from under them. Such a resizing can happen,
10281 for instance, when which-func prints a long message while
10282 we are running fontification-functions. We're running these
10283 functions with safe_call which binds inhibit-redisplay to t. */
10284 if (!NILP (Vinhibit_redisplay))
10285 return 0;
10287 /* Nil means don't try to resize. */
10288 if (NILP (Vresize_mini_windows)
10289 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10290 return 0;
10292 if (!FRAME_MINIBUF_ONLY_P (f))
10294 struct it it;
10295 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10296 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10297 int height;
10298 EMACS_INT max_height;
10299 int unit = FRAME_LINE_HEIGHT (f);
10300 struct text_pos start;
10301 struct buffer *old_current_buffer = NULL;
10303 if (current_buffer != XBUFFER (w->contents))
10305 old_current_buffer = current_buffer;
10306 set_buffer_internal (XBUFFER (w->contents));
10309 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10311 /* Compute the max. number of lines specified by the user. */
10312 if (FLOATP (Vmax_mini_window_height))
10313 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10314 else if (INTEGERP (Vmax_mini_window_height))
10315 max_height = XINT (Vmax_mini_window_height);
10316 else
10317 max_height = total_height / 4;
10319 /* Correct that max. height if it's bogus. */
10320 max_height = clip_to_bounds (1, max_height, total_height);
10322 /* Find out the height of the text in the window. */
10323 if (it.line_wrap == TRUNCATE)
10324 height = 1;
10325 else
10327 last_height = 0;
10328 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10329 if (it.max_ascent == 0 && it.max_descent == 0)
10330 height = it.current_y + last_height;
10331 else
10332 height = it.current_y + it.max_ascent + it.max_descent;
10333 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10334 height = (height + unit - 1) / unit;
10337 /* Compute a suitable window start. */
10338 if (height > max_height)
10340 height = max_height;
10341 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10342 move_it_vertically_backward (&it, (height - 1) * unit);
10343 start = it.current.pos;
10345 else
10346 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10347 SET_MARKER_FROM_TEXT_POS (w->start, start);
10349 if (EQ (Vresize_mini_windows, Qgrow_only))
10351 /* Let it grow only, until we display an empty message, in which
10352 case the window shrinks again. */
10353 if (height > WINDOW_TOTAL_LINES (w))
10355 int old_height = WINDOW_TOTAL_LINES (w);
10356 freeze_window_starts (f, 1);
10357 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10358 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10360 else if (height < WINDOW_TOTAL_LINES (w)
10361 && (exact_p || BEGV == ZV))
10363 int old_height = WINDOW_TOTAL_LINES (w);
10364 freeze_window_starts (f, 0);
10365 shrink_mini_window (w);
10366 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10369 else
10371 /* Always resize to exact size needed. */
10372 if (height > WINDOW_TOTAL_LINES (w))
10374 int old_height = WINDOW_TOTAL_LINES (w);
10375 freeze_window_starts (f, 1);
10376 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10377 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10379 else if (height < WINDOW_TOTAL_LINES (w))
10381 int old_height = WINDOW_TOTAL_LINES (w);
10382 freeze_window_starts (f, 0);
10383 shrink_mini_window (w);
10385 if (height)
10387 freeze_window_starts (f, 1);
10388 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10391 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10395 if (old_current_buffer)
10396 set_buffer_internal (old_current_buffer);
10399 return window_height_changed_p;
10403 /* Value is the current message, a string, or nil if there is no
10404 current message. */
10406 Lisp_Object
10407 current_message (void)
10409 Lisp_Object msg;
10411 if (!BUFFERP (echo_area_buffer[0]))
10412 msg = Qnil;
10413 else
10415 with_echo_area_buffer (0, 0, current_message_1,
10416 (intptr_t) &msg, Qnil);
10417 if (NILP (msg))
10418 echo_area_buffer[0] = Qnil;
10421 return msg;
10425 static int
10426 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10428 intptr_t i1 = a1;
10429 Lisp_Object *msg = (Lisp_Object *) i1;
10431 if (Z > BEG)
10432 *msg = make_buffer_string (BEG, Z, 1);
10433 else
10434 *msg = Qnil;
10435 return 0;
10439 /* Push the current message on Vmessage_stack for later restoration
10440 by restore_message. Value is non-zero if the current message isn't
10441 empty. This is a relatively infrequent operation, so it's not
10442 worth optimizing. */
10444 bool
10445 push_message (void)
10447 Lisp_Object msg = current_message ();
10448 Vmessage_stack = Fcons (msg, Vmessage_stack);
10449 return STRINGP (msg);
10453 /* Restore message display from the top of Vmessage_stack. */
10455 void
10456 restore_message (void)
10458 eassert (CONSP (Vmessage_stack));
10459 message3_nolog (XCAR (Vmessage_stack));
10463 /* Handler for record_unwind_protect calling pop_message. */
10465 Lisp_Object
10466 pop_message_unwind (Lisp_Object dummy)
10468 pop_message ();
10469 return Qnil;
10472 /* Pop the top-most entry off Vmessage_stack. */
10474 static void
10475 pop_message (void)
10477 eassert (CONSP (Vmessage_stack));
10478 Vmessage_stack = XCDR (Vmessage_stack);
10482 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10483 exits. If the stack is not empty, we have a missing pop_message
10484 somewhere. */
10486 void
10487 check_message_stack (void)
10489 if (!NILP (Vmessage_stack))
10490 emacs_abort ();
10494 /* Truncate to NCHARS what will be displayed in the echo area the next
10495 time we display it---but don't redisplay it now. */
10497 void
10498 truncate_echo_area (ptrdiff_t nchars)
10500 if (nchars == 0)
10501 echo_area_buffer[0] = Qnil;
10502 else if (!noninteractive
10503 && INTERACTIVE
10504 && !NILP (echo_area_buffer[0]))
10506 struct frame *sf = SELECTED_FRAME ();
10507 /* Error messages get reported properly by cmd_error, so this must be
10508 just an informative message; if the frame hasn't really been
10509 initialized yet, just toss it. */
10510 if (sf->glyphs_initialized_p)
10511 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10516 /* Helper function for truncate_echo_area. Truncate the current
10517 message to at most NCHARS characters. */
10519 static int
10520 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10522 if (BEG + nchars < Z)
10523 del_range (BEG + nchars, Z);
10524 if (Z == BEG)
10525 echo_area_buffer[0] = Qnil;
10526 return 0;
10529 /* Set the current message to STRING. */
10531 static void
10532 set_message (Lisp_Object string)
10534 eassert (STRINGP (string));
10536 message_enable_multibyte = STRING_MULTIBYTE (string);
10538 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10539 message_buf_print = 0;
10540 help_echo_showing_p = 0;
10542 if (STRINGP (Vdebug_on_message)
10543 && STRINGP (string)
10544 && fast_string_match (Vdebug_on_message, string) >= 0)
10545 call_debugger (list2 (Qerror, string));
10549 /* Helper function for set_message. First argument is ignored and second
10550 argument has the same meaning as for set_message.
10551 This function is called with the echo area buffer being current. */
10553 static int
10554 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10556 eassert (STRINGP (string));
10558 /* Change multibyteness of the echo buffer appropriately. */
10559 if (message_enable_multibyte
10560 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10561 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10563 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10564 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10565 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10567 /* Insert new message at BEG. */
10568 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10570 /* This function takes care of single/multibyte conversion.
10571 We just have to ensure that the echo area buffer has the right
10572 setting of enable_multibyte_characters. */
10573 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10575 return 0;
10579 /* Clear messages. CURRENT_P non-zero means clear the current
10580 message. LAST_DISPLAYED_P non-zero means clear the message
10581 last displayed. */
10583 void
10584 clear_message (int current_p, int last_displayed_p)
10586 if (current_p)
10588 echo_area_buffer[0] = Qnil;
10589 message_cleared_p = 1;
10592 if (last_displayed_p)
10593 echo_area_buffer[1] = Qnil;
10595 message_buf_print = 0;
10598 /* Clear garbaged frames.
10600 This function is used where the old redisplay called
10601 redraw_garbaged_frames which in turn called redraw_frame which in
10602 turn called clear_frame. The call to clear_frame was a source of
10603 flickering. I believe a clear_frame is not necessary. It should
10604 suffice in the new redisplay to invalidate all current matrices,
10605 and ensure a complete redisplay of all windows. */
10607 static void
10608 clear_garbaged_frames (void)
10610 if (frame_garbaged)
10612 Lisp_Object tail, frame;
10613 int changed_count = 0;
10615 FOR_EACH_FRAME (tail, frame)
10617 struct frame *f = XFRAME (frame);
10619 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10621 if (f->resized_p)
10623 redraw_frame (f);
10624 f->force_flush_display_p = 1;
10626 clear_current_matrices (f);
10627 changed_count++;
10628 f->garbaged = 0;
10629 f->resized_p = 0;
10633 frame_garbaged = 0;
10634 if (changed_count)
10635 ++windows_or_buffers_changed;
10640 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10641 is non-zero update selected_frame. Value is non-zero if the
10642 mini-windows height has been changed. */
10644 static int
10645 echo_area_display (int update_frame_p)
10647 Lisp_Object mini_window;
10648 struct window *w;
10649 struct frame *f;
10650 int window_height_changed_p = 0;
10651 struct frame *sf = SELECTED_FRAME ();
10653 mini_window = FRAME_MINIBUF_WINDOW (sf);
10654 w = XWINDOW (mini_window);
10655 f = XFRAME (WINDOW_FRAME (w));
10657 /* Don't display if frame is invisible or not yet initialized. */
10658 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10659 return 0;
10661 #ifdef HAVE_WINDOW_SYSTEM
10662 /* When Emacs starts, selected_frame may be the initial terminal
10663 frame. If we let this through, a message would be displayed on
10664 the terminal. */
10665 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10666 return 0;
10667 #endif /* HAVE_WINDOW_SYSTEM */
10669 /* Redraw garbaged frames. */
10670 clear_garbaged_frames ();
10672 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10674 echo_area_window = mini_window;
10675 window_height_changed_p = display_echo_area (w);
10676 w->must_be_updated_p = 1;
10678 /* Update the display, unless called from redisplay_internal.
10679 Also don't update the screen during redisplay itself. The
10680 update will happen at the end of redisplay, and an update
10681 here could cause confusion. */
10682 if (update_frame_p && !redisplaying_p)
10684 int n = 0;
10686 /* If the display update has been interrupted by pending
10687 input, update mode lines in the frame. Due to the
10688 pending input, it might have been that redisplay hasn't
10689 been called, so that mode lines above the echo area are
10690 garbaged. This looks odd, so we prevent it here. */
10691 if (!display_completed)
10692 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10694 if (window_height_changed_p
10695 /* Don't do this if Emacs is shutting down. Redisplay
10696 needs to run hooks. */
10697 && !NILP (Vrun_hooks))
10699 /* Must update other windows. Likewise as in other
10700 cases, don't let this update be interrupted by
10701 pending input. */
10702 ptrdiff_t count = SPECPDL_INDEX ();
10703 specbind (Qredisplay_dont_pause, Qt);
10704 windows_or_buffers_changed = 1;
10705 redisplay_internal ();
10706 unbind_to (count, Qnil);
10708 else if (FRAME_WINDOW_P (f) && n == 0)
10710 /* Window configuration is the same as before.
10711 Can do with a display update of the echo area,
10712 unless we displayed some mode lines. */
10713 update_single_window (w, 1);
10714 FRAME_RIF (f)->flush_display (f);
10716 else
10717 update_frame (f, 1, 1);
10719 /* If cursor is in the echo area, make sure that the next
10720 redisplay displays the minibuffer, so that the cursor will
10721 be replaced with what the minibuffer wants. */
10722 if (cursor_in_echo_area)
10723 ++windows_or_buffers_changed;
10726 else if (!EQ (mini_window, selected_window))
10727 windows_or_buffers_changed++;
10729 /* Last displayed message is now the current message. */
10730 echo_area_buffer[1] = echo_area_buffer[0];
10731 /* Inform read_char that we're not echoing. */
10732 echo_message_buffer = Qnil;
10734 /* Prevent redisplay optimization in redisplay_internal by resetting
10735 this_line_start_pos. This is done because the mini-buffer now
10736 displays the message instead of its buffer text. */
10737 if (EQ (mini_window, selected_window))
10738 CHARPOS (this_line_start_pos) = 0;
10740 return window_height_changed_p;
10743 /* Nonzero if the current window's buffer is shown in more than one
10744 window and was modified since last redisplay. */
10746 static int
10747 buffer_shared_and_changed (void)
10749 return (buffer_window_count (current_buffer) > 1
10750 && UNCHANGED_MODIFIED < MODIFF);
10753 /* Nonzero if W doesn't reflect the actual state of current buffer due
10754 to its text or overlays change. FIXME: this may be called when
10755 XBUFFER (w->contents) != current_buffer, which looks suspicious. */
10757 static int
10758 window_outdated (struct window *w)
10760 return (w->last_modified < MODIFF
10761 || w->last_overlay_modified < OVERLAY_MODIFF);
10764 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10765 is enabled and mark of W's buffer was changed since last W's update. */
10767 static int
10768 window_buffer_changed (struct window *w)
10770 struct buffer *b = XBUFFER (w->contents);
10772 eassert (BUFFER_LIVE_P (b));
10774 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10775 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10776 != (w->region_showing != 0)));
10779 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10781 static int
10782 mode_line_update_needed (struct window *w)
10784 return (w->column_number_displayed != -1
10785 && !(PT == w->last_point && !window_outdated (w))
10786 && (w->column_number_displayed != current_column ()));
10789 /***********************************************************************
10790 Mode Lines and Frame Titles
10791 ***********************************************************************/
10793 /* A buffer for constructing non-propertized mode-line strings and
10794 frame titles in it; allocated from the heap in init_xdisp and
10795 resized as needed in store_mode_line_noprop_char. */
10797 static char *mode_line_noprop_buf;
10799 /* The buffer's end, and a current output position in it. */
10801 static char *mode_line_noprop_buf_end;
10802 static char *mode_line_noprop_ptr;
10804 #define MODE_LINE_NOPROP_LEN(start) \
10805 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10807 static enum {
10808 MODE_LINE_DISPLAY = 0,
10809 MODE_LINE_TITLE,
10810 MODE_LINE_NOPROP,
10811 MODE_LINE_STRING
10812 } mode_line_target;
10814 /* Alist that caches the results of :propertize.
10815 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10816 static Lisp_Object mode_line_proptrans_alist;
10818 /* List of strings making up the mode-line. */
10819 static Lisp_Object mode_line_string_list;
10821 /* Base face property when building propertized mode line string. */
10822 static Lisp_Object mode_line_string_face;
10823 static Lisp_Object mode_line_string_face_prop;
10826 /* Unwind data for mode line strings */
10828 static Lisp_Object Vmode_line_unwind_vector;
10830 static Lisp_Object
10831 format_mode_line_unwind_data (struct frame *target_frame,
10832 struct buffer *obuf,
10833 Lisp_Object owin,
10834 int save_proptrans)
10836 Lisp_Object vector, tmp;
10838 /* Reduce consing by keeping one vector in
10839 Vwith_echo_area_save_vector. */
10840 vector = Vmode_line_unwind_vector;
10841 Vmode_line_unwind_vector = Qnil;
10843 if (NILP (vector))
10844 vector = Fmake_vector (make_number (10), Qnil);
10846 ASET (vector, 0, make_number (mode_line_target));
10847 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10848 ASET (vector, 2, mode_line_string_list);
10849 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10850 ASET (vector, 4, mode_line_string_face);
10851 ASET (vector, 5, mode_line_string_face_prop);
10853 if (obuf)
10854 XSETBUFFER (tmp, obuf);
10855 else
10856 tmp = Qnil;
10857 ASET (vector, 6, tmp);
10858 ASET (vector, 7, owin);
10859 if (target_frame)
10861 /* Similarly to `with-selected-window', if the operation selects
10862 a window on another frame, we must restore that frame's
10863 selected window, and (for a tty) the top-frame. */
10864 ASET (vector, 8, target_frame->selected_window);
10865 if (FRAME_TERMCAP_P (target_frame))
10866 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10869 return vector;
10872 static Lisp_Object
10873 unwind_format_mode_line (Lisp_Object vector)
10875 Lisp_Object old_window = AREF (vector, 7);
10876 Lisp_Object target_frame_window = AREF (vector, 8);
10877 Lisp_Object old_top_frame = AREF (vector, 9);
10879 mode_line_target = XINT (AREF (vector, 0));
10880 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10881 mode_line_string_list = AREF (vector, 2);
10882 if (! EQ (AREF (vector, 3), Qt))
10883 mode_line_proptrans_alist = AREF (vector, 3);
10884 mode_line_string_face = AREF (vector, 4);
10885 mode_line_string_face_prop = AREF (vector, 5);
10887 /* Select window before buffer, since it may change the buffer. */
10888 if (!NILP (old_window))
10890 /* If the operation that we are unwinding had selected a window
10891 on a different frame, reset its frame-selected-window. For a
10892 text terminal, reset its top-frame if necessary. */
10893 if (!NILP (target_frame_window))
10895 Lisp_Object frame
10896 = WINDOW_FRAME (XWINDOW (target_frame_window));
10898 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10899 Fselect_window (target_frame_window, Qt);
10901 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10902 Fselect_frame (old_top_frame, Qt);
10905 Fselect_window (old_window, Qt);
10908 if (!NILP (AREF (vector, 6)))
10910 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10911 ASET (vector, 6, Qnil);
10914 Vmode_line_unwind_vector = vector;
10915 return Qnil;
10919 /* Store a single character C for the frame title in mode_line_noprop_buf.
10920 Re-allocate mode_line_noprop_buf if necessary. */
10922 static void
10923 store_mode_line_noprop_char (char c)
10925 /* If output position has reached the end of the allocated buffer,
10926 increase the buffer's size. */
10927 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10929 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10930 ptrdiff_t size = len;
10931 mode_line_noprop_buf =
10932 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10933 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10934 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10937 *mode_line_noprop_ptr++ = c;
10941 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10942 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10943 characters that yield more columns than PRECISION; PRECISION <= 0
10944 means copy the whole string. Pad with spaces until FIELD_WIDTH
10945 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10946 pad. Called from display_mode_element when it is used to build a
10947 frame title. */
10949 static int
10950 store_mode_line_noprop (const char *string, int field_width, int precision)
10952 const unsigned char *str = (const unsigned char *) string;
10953 int n = 0;
10954 ptrdiff_t dummy, nbytes;
10956 /* Copy at most PRECISION chars from STR. */
10957 nbytes = strlen (string);
10958 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10959 while (nbytes--)
10960 store_mode_line_noprop_char (*str++);
10962 /* Fill up with spaces until FIELD_WIDTH reached. */
10963 while (field_width > 0
10964 && n < field_width)
10966 store_mode_line_noprop_char (' ');
10967 ++n;
10970 return n;
10973 /***********************************************************************
10974 Frame Titles
10975 ***********************************************************************/
10977 #ifdef HAVE_WINDOW_SYSTEM
10979 /* Set the title of FRAME, if it has changed. The title format is
10980 Vicon_title_format if FRAME is iconified, otherwise it is
10981 frame_title_format. */
10983 static void
10984 x_consider_frame_title (Lisp_Object frame)
10986 struct frame *f = XFRAME (frame);
10988 if (FRAME_WINDOW_P (f)
10989 || FRAME_MINIBUF_ONLY_P (f)
10990 || f->explicit_name)
10992 /* Do we have more than one visible frame on this X display? */
10993 Lisp_Object tail, other_frame, fmt;
10994 ptrdiff_t title_start;
10995 char *title;
10996 ptrdiff_t len;
10997 struct it it;
10998 ptrdiff_t count = SPECPDL_INDEX ();
11000 FOR_EACH_FRAME (tail, other_frame)
11002 struct frame *tf = XFRAME (other_frame);
11004 if (tf != f
11005 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11006 && !FRAME_MINIBUF_ONLY_P (tf)
11007 && !EQ (other_frame, tip_frame)
11008 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11009 break;
11012 /* Set global variable indicating that multiple frames exist. */
11013 multiple_frames = CONSP (tail);
11015 /* Switch to the buffer of selected window of the frame. Set up
11016 mode_line_target so that display_mode_element will output into
11017 mode_line_noprop_buf; then display the title. */
11018 record_unwind_protect (unwind_format_mode_line,
11019 format_mode_line_unwind_data
11020 (f, current_buffer, selected_window, 0));
11022 Fselect_window (f->selected_window, Qt);
11023 set_buffer_internal_1
11024 (XBUFFER (XWINDOW (f->selected_window)->contents));
11025 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11027 mode_line_target = MODE_LINE_TITLE;
11028 title_start = MODE_LINE_NOPROP_LEN (0);
11029 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11030 NULL, DEFAULT_FACE_ID);
11031 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11032 len = MODE_LINE_NOPROP_LEN (title_start);
11033 title = mode_line_noprop_buf + title_start;
11034 unbind_to (count, Qnil);
11036 /* Set the title only if it's changed. This avoids consing in
11037 the common case where it hasn't. (If it turns out that we've
11038 already wasted too much time by walking through the list with
11039 display_mode_element, then we might need to optimize at a
11040 higher level than this.) */
11041 if (! STRINGP (f->name)
11042 || SBYTES (f->name) != len
11043 || memcmp (title, SDATA (f->name), len) != 0)
11044 x_implicitly_set_name (f, make_string (title, len), Qnil);
11048 #endif /* not HAVE_WINDOW_SYSTEM */
11051 /***********************************************************************
11052 Menu Bars
11053 ***********************************************************************/
11056 /* Prepare for redisplay by updating menu-bar item lists when
11057 appropriate. This can call eval. */
11059 void
11060 prepare_menu_bars (void)
11062 int all_windows;
11063 struct gcpro gcpro1, gcpro2;
11064 struct frame *f;
11065 Lisp_Object tooltip_frame;
11067 #ifdef HAVE_WINDOW_SYSTEM
11068 tooltip_frame = tip_frame;
11069 #else
11070 tooltip_frame = Qnil;
11071 #endif
11073 /* Update all frame titles based on their buffer names, etc. We do
11074 this before the menu bars so that the buffer-menu will show the
11075 up-to-date frame titles. */
11076 #ifdef HAVE_WINDOW_SYSTEM
11077 if (windows_or_buffers_changed || update_mode_lines)
11079 Lisp_Object tail, frame;
11081 FOR_EACH_FRAME (tail, frame)
11083 f = XFRAME (frame);
11084 if (!EQ (frame, tooltip_frame)
11085 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11086 x_consider_frame_title (frame);
11089 #endif /* HAVE_WINDOW_SYSTEM */
11091 /* Update the menu bar item lists, if appropriate. This has to be
11092 done before any actual redisplay or generation of display lines. */
11093 all_windows = (update_mode_lines
11094 || buffer_shared_and_changed ()
11095 || windows_or_buffers_changed);
11096 if (all_windows)
11098 Lisp_Object tail, frame;
11099 ptrdiff_t count = SPECPDL_INDEX ();
11100 /* 1 means that update_menu_bar has run its hooks
11101 so any further calls to update_menu_bar shouldn't do so again. */
11102 int menu_bar_hooks_run = 0;
11104 record_unwind_save_match_data ();
11106 FOR_EACH_FRAME (tail, frame)
11108 f = XFRAME (frame);
11110 /* Ignore tooltip frame. */
11111 if (EQ (frame, tooltip_frame))
11112 continue;
11114 /* If a window on this frame changed size, report that to
11115 the user and clear the size-change flag. */
11116 if (FRAME_WINDOW_SIZES_CHANGED (f))
11118 Lisp_Object functions;
11120 /* Clear flag first in case we get an error below. */
11121 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11122 functions = Vwindow_size_change_functions;
11123 GCPRO2 (tail, functions);
11125 while (CONSP (functions))
11127 if (!EQ (XCAR (functions), Qt))
11128 call1 (XCAR (functions), frame);
11129 functions = XCDR (functions);
11131 UNGCPRO;
11134 GCPRO1 (tail);
11135 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11136 #ifdef HAVE_WINDOW_SYSTEM
11137 update_tool_bar (f, 0);
11138 #endif
11139 #ifdef HAVE_NS
11140 if (windows_or_buffers_changed
11141 && FRAME_NS_P (f))
11142 ns_set_doc_edited
11143 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11144 #endif
11145 UNGCPRO;
11148 unbind_to (count, Qnil);
11150 else
11152 struct frame *sf = SELECTED_FRAME ();
11153 update_menu_bar (sf, 1, 0);
11154 #ifdef HAVE_WINDOW_SYSTEM
11155 update_tool_bar (sf, 1);
11156 #endif
11161 /* Update the menu bar item list for frame F. This has to be done
11162 before we start to fill in any display lines, because it can call
11163 eval.
11165 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11167 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11168 already ran the menu bar hooks for this redisplay, so there
11169 is no need to run them again. The return value is the
11170 updated value of this flag, to pass to the next call. */
11172 static int
11173 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11175 Lisp_Object window;
11176 register struct window *w;
11178 /* If called recursively during a menu update, do nothing. This can
11179 happen when, for instance, an activate-menubar-hook causes a
11180 redisplay. */
11181 if (inhibit_menubar_update)
11182 return hooks_run;
11184 window = FRAME_SELECTED_WINDOW (f);
11185 w = XWINDOW (window);
11187 if (FRAME_WINDOW_P (f)
11189 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11190 || defined (HAVE_NS) || defined (USE_GTK)
11191 FRAME_EXTERNAL_MENU_BAR (f)
11192 #else
11193 FRAME_MENU_BAR_LINES (f) > 0
11194 #endif
11195 : FRAME_MENU_BAR_LINES (f) > 0)
11197 /* If the user has switched buffers or windows, we need to
11198 recompute to reflect the new bindings. But we'll
11199 recompute when update_mode_lines is set too; that means
11200 that people can use force-mode-line-update to request
11201 that the menu bar be recomputed. The adverse effect on
11202 the rest of the redisplay algorithm is about the same as
11203 windows_or_buffers_changed anyway. */
11204 if (windows_or_buffers_changed
11205 /* This used to test w->update_mode_line, but we believe
11206 there is no need to recompute the menu in that case. */
11207 || update_mode_lines
11208 || window_buffer_changed (w))
11210 struct buffer *prev = current_buffer;
11211 ptrdiff_t count = SPECPDL_INDEX ();
11213 specbind (Qinhibit_menubar_update, Qt);
11215 set_buffer_internal_1 (XBUFFER (w->contents));
11216 if (save_match_data)
11217 record_unwind_save_match_data ();
11218 if (NILP (Voverriding_local_map_menu_flag))
11220 specbind (Qoverriding_terminal_local_map, Qnil);
11221 specbind (Qoverriding_local_map, Qnil);
11224 if (!hooks_run)
11226 /* Run the Lucid hook. */
11227 safe_run_hooks (Qactivate_menubar_hook);
11229 /* If it has changed current-menubar from previous value,
11230 really recompute the menu-bar from the value. */
11231 if (! NILP (Vlucid_menu_bar_dirty_flag))
11232 call0 (Qrecompute_lucid_menubar);
11234 safe_run_hooks (Qmenu_bar_update_hook);
11236 hooks_run = 1;
11239 XSETFRAME (Vmenu_updating_frame, f);
11240 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11242 /* Redisplay the menu bar in case we changed it. */
11243 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11244 || defined (HAVE_NS) || defined (USE_GTK)
11245 if (FRAME_WINDOW_P (f))
11247 #if defined (HAVE_NS)
11248 /* All frames on Mac OS share the same menubar. So only
11249 the selected frame should be allowed to set it. */
11250 if (f == SELECTED_FRAME ())
11251 #endif
11252 set_frame_menubar (f, 0, 0);
11254 else
11255 /* On a terminal screen, the menu bar is an ordinary screen
11256 line, and this makes it get updated. */
11257 w->update_mode_line = 1;
11258 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11259 /* In the non-toolkit version, the menu bar is an ordinary screen
11260 line, and this makes it get updated. */
11261 w->update_mode_line = 1;
11262 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11264 unbind_to (count, Qnil);
11265 set_buffer_internal_1 (prev);
11269 return hooks_run;
11274 /***********************************************************************
11275 Output Cursor
11276 ***********************************************************************/
11278 #ifdef HAVE_WINDOW_SYSTEM
11280 /* EXPORT:
11281 Nominal cursor position -- where to draw output.
11282 HPOS and VPOS are window relative glyph matrix coordinates.
11283 X and Y are window relative pixel coordinates. */
11285 struct cursor_pos output_cursor;
11288 /* EXPORT:
11289 Set the global variable output_cursor to CURSOR. All cursor
11290 positions are relative to updated_window. */
11292 void
11293 set_output_cursor (struct cursor_pos *cursor)
11295 output_cursor.hpos = cursor->hpos;
11296 output_cursor.vpos = cursor->vpos;
11297 output_cursor.x = cursor->x;
11298 output_cursor.y = cursor->y;
11302 /* EXPORT for RIF:
11303 Set a nominal cursor position.
11305 HPOS and VPOS are column/row positions in a window glyph matrix. X
11306 and Y are window text area relative pixel positions.
11308 If this is done during an update, updated_window will contain the
11309 window that is being updated and the position is the future output
11310 cursor position for that window. If updated_window is null, use
11311 selected_window and display the cursor at the given position. */
11313 void
11314 x_cursor_to (int vpos, int hpos, int y, int x)
11316 struct window *w;
11318 /* If updated_window is not set, work on selected_window. */
11319 if (updated_window)
11320 w = updated_window;
11321 else
11322 w = XWINDOW (selected_window);
11324 /* Set the output cursor. */
11325 output_cursor.hpos = hpos;
11326 output_cursor.vpos = vpos;
11327 output_cursor.x = x;
11328 output_cursor.y = y;
11330 /* If not called as part of an update, really display the cursor.
11331 This will also set the cursor position of W. */
11332 if (updated_window == NULL)
11334 block_input ();
11335 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11336 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11337 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11338 unblock_input ();
11342 #endif /* HAVE_WINDOW_SYSTEM */
11345 /***********************************************************************
11346 Tool-bars
11347 ***********************************************************************/
11349 #ifdef HAVE_WINDOW_SYSTEM
11351 /* Where the mouse was last time we reported a mouse event. */
11353 FRAME_PTR last_mouse_frame;
11355 /* Tool-bar item index of the item on which a mouse button was pressed
11356 or -1. */
11358 int last_tool_bar_item;
11360 /* Select `frame' temporarily without running all the code in
11361 do_switch_frame.
11362 FIXME: Maybe do_switch_frame should be trimmed down similarly
11363 when `norecord' is set. */
11364 static Lisp_Object
11365 fast_set_selected_frame (Lisp_Object frame)
11367 if (!EQ (selected_frame, frame))
11369 selected_frame = frame;
11370 selected_window = XFRAME (frame)->selected_window;
11372 return Qnil;
11375 /* Update the tool-bar item list for frame F. This has to be done
11376 before we start to fill in any display lines. Called from
11377 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11378 and restore it here. */
11380 static void
11381 update_tool_bar (struct frame *f, int save_match_data)
11383 #if defined (USE_GTK) || defined (HAVE_NS)
11384 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11385 #else
11386 int do_update = WINDOWP (f->tool_bar_window)
11387 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11388 #endif
11390 if (do_update)
11392 Lisp_Object window;
11393 struct window *w;
11395 window = FRAME_SELECTED_WINDOW (f);
11396 w = XWINDOW (window);
11398 /* If the user has switched buffers or windows, we need to
11399 recompute to reflect the new bindings. But we'll
11400 recompute when update_mode_lines is set too; that means
11401 that people can use force-mode-line-update to request
11402 that the menu bar be recomputed. The adverse effect on
11403 the rest of the redisplay algorithm is about the same as
11404 windows_or_buffers_changed anyway. */
11405 if (windows_or_buffers_changed
11406 || w->update_mode_line
11407 || update_mode_lines
11408 || window_buffer_changed (w))
11410 struct buffer *prev = current_buffer;
11411 ptrdiff_t count = SPECPDL_INDEX ();
11412 Lisp_Object frame, new_tool_bar;
11413 int new_n_tool_bar;
11414 struct gcpro gcpro1;
11416 /* Set current_buffer to the buffer of the selected
11417 window of the frame, so that we get the right local
11418 keymaps. */
11419 set_buffer_internal_1 (XBUFFER (w->contents));
11421 /* Save match data, if we must. */
11422 if (save_match_data)
11423 record_unwind_save_match_data ();
11425 /* Make sure that we don't accidentally use bogus keymaps. */
11426 if (NILP (Voverriding_local_map_menu_flag))
11428 specbind (Qoverriding_terminal_local_map, Qnil);
11429 specbind (Qoverriding_local_map, Qnil);
11432 GCPRO1 (new_tool_bar);
11434 /* We must temporarily set the selected frame to this frame
11435 before calling tool_bar_items, because the calculation of
11436 the tool-bar keymap uses the selected frame (see
11437 `tool-bar-make-keymap' in tool-bar.el). */
11438 eassert (EQ (selected_window,
11439 /* Since we only explicitly preserve selected_frame,
11440 check that selected_window would be redundant. */
11441 XFRAME (selected_frame)->selected_window));
11442 record_unwind_protect (fast_set_selected_frame, selected_frame);
11443 XSETFRAME (frame, f);
11444 fast_set_selected_frame (frame);
11446 /* Build desired tool-bar items from keymaps. */
11447 new_tool_bar
11448 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11449 &new_n_tool_bar);
11451 /* Redisplay the tool-bar if we changed it. */
11452 if (new_n_tool_bar != f->n_tool_bar_items
11453 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11455 /* Redisplay that happens asynchronously due to an expose event
11456 may access f->tool_bar_items. Make sure we update both
11457 variables within BLOCK_INPUT so no such event interrupts. */
11458 block_input ();
11459 fset_tool_bar_items (f, new_tool_bar);
11460 f->n_tool_bar_items = new_n_tool_bar;
11461 w->update_mode_line = 1;
11462 unblock_input ();
11465 UNGCPRO;
11467 unbind_to (count, Qnil);
11468 set_buffer_internal_1 (prev);
11474 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11475 F's desired tool-bar contents. F->tool_bar_items must have
11476 been set up previously by calling prepare_menu_bars. */
11478 static void
11479 build_desired_tool_bar_string (struct frame *f)
11481 int i, size, size_needed;
11482 struct gcpro gcpro1, gcpro2, gcpro3;
11483 Lisp_Object image, plist, props;
11485 image = plist = props = Qnil;
11486 GCPRO3 (image, plist, props);
11488 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11489 Otherwise, make a new string. */
11491 /* The size of the string we might be able to reuse. */
11492 size = (STRINGP (f->desired_tool_bar_string)
11493 ? SCHARS (f->desired_tool_bar_string)
11494 : 0);
11496 /* We need one space in the string for each image. */
11497 size_needed = f->n_tool_bar_items;
11499 /* Reuse f->desired_tool_bar_string, if possible. */
11500 if (size < size_needed || NILP (f->desired_tool_bar_string))
11501 fset_desired_tool_bar_string
11502 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11503 else
11505 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11506 Fremove_text_properties (make_number (0), make_number (size),
11507 props, f->desired_tool_bar_string);
11510 /* Put a `display' property on the string for the images to display,
11511 put a `menu_item' property on tool-bar items with a value that
11512 is the index of the item in F's tool-bar item vector. */
11513 for (i = 0; i < f->n_tool_bar_items; ++i)
11515 #define PROP(IDX) \
11516 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11518 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11519 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11520 int hmargin, vmargin, relief, idx, end;
11522 /* If image is a vector, choose the image according to the
11523 button state. */
11524 image = PROP (TOOL_BAR_ITEM_IMAGES);
11525 if (VECTORP (image))
11527 if (enabled_p)
11528 idx = (selected_p
11529 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11530 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11531 else
11532 idx = (selected_p
11533 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11534 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11536 eassert (ASIZE (image) >= idx);
11537 image = AREF (image, idx);
11539 else
11540 idx = -1;
11542 /* Ignore invalid image specifications. */
11543 if (!valid_image_p (image))
11544 continue;
11546 /* Display the tool-bar button pressed, or depressed. */
11547 plist = Fcopy_sequence (XCDR (image));
11549 /* Compute margin and relief to draw. */
11550 relief = (tool_bar_button_relief >= 0
11551 ? tool_bar_button_relief
11552 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11553 hmargin = vmargin = relief;
11555 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11556 INT_MAX - max (hmargin, vmargin)))
11558 hmargin += XFASTINT (Vtool_bar_button_margin);
11559 vmargin += XFASTINT (Vtool_bar_button_margin);
11561 else if (CONSP (Vtool_bar_button_margin))
11563 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11564 INT_MAX - hmargin))
11565 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11567 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11568 INT_MAX - vmargin))
11569 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11572 if (auto_raise_tool_bar_buttons_p)
11574 /* Add a `:relief' property to the image spec if the item is
11575 selected. */
11576 if (selected_p)
11578 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11579 hmargin -= relief;
11580 vmargin -= relief;
11583 else
11585 /* If image is selected, display it pressed, i.e. with a
11586 negative relief. If it's not selected, display it with a
11587 raised relief. */
11588 plist = Fplist_put (plist, QCrelief,
11589 (selected_p
11590 ? make_number (-relief)
11591 : make_number (relief)));
11592 hmargin -= relief;
11593 vmargin -= relief;
11596 /* Put a margin around the image. */
11597 if (hmargin || vmargin)
11599 if (hmargin == vmargin)
11600 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11601 else
11602 plist = Fplist_put (plist, QCmargin,
11603 Fcons (make_number (hmargin),
11604 make_number (vmargin)));
11607 /* If button is not enabled, and we don't have special images
11608 for the disabled state, make the image appear disabled by
11609 applying an appropriate algorithm to it. */
11610 if (!enabled_p && idx < 0)
11611 plist = Fplist_put (plist, QCconversion, Qdisabled);
11613 /* Put a `display' text property on the string for the image to
11614 display. Put a `menu-item' property on the string that gives
11615 the start of this item's properties in the tool-bar items
11616 vector. */
11617 image = Fcons (Qimage, plist);
11618 props = list4 (Qdisplay, image,
11619 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11621 /* Let the last image hide all remaining spaces in the tool bar
11622 string. The string can be longer than needed when we reuse a
11623 previous string. */
11624 if (i + 1 == f->n_tool_bar_items)
11625 end = SCHARS (f->desired_tool_bar_string);
11626 else
11627 end = i + 1;
11628 Fadd_text_properties (make_number (i), make_number (end),
11629 props, f->desired_tool_bar_string);
11630 #undef PROP
11633 UNGCPRO;
11637 /* Display one line of the tool-bar of frame IT->f.
11639 HEIGHT specifies the desired height of the tool-bar line.
11640 If the actual height of the glyph row is less than HEIGHT, the
11641 row's height is increased to HEIGHT, and the icons are centered
11642 vertically in the new height.
11644 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11645 count a final empty row in case the tool-bar width exactly matches
11646 the window width.
11649 static void
11650 display_tool_bar_line (struct it *it, int height)
11652 struct glyph_row *row = it->glyph_row;
11653 int max_x = it->last_visible_x;
11654 struct glyph *last;
11656 prepare_desired_row (row);
11657 row->y = it->current_y;
11659 /* Note that this isn't made use of if the face hasn't a box,
11660 so there's no need to check the face here. */
11661 it->start_of_box_run_p = 1;
11663 while (it->current_x < max_x)
11665 int x, n_glyphs_before, i, nglyphs;
11666 struct it it_before;
11668 /* Get the next display element. */
11669 if (!get_next_display_element (it))
11671 /* Don't count empty row if we are counting needed tool-bar lines. */
11672 if (height < 0 && !it->hpos)
11673 return;
11674 break;
11677 /* Produce glyphs. */
11678 n_glyphs_before = row->used[TEXT_AREA];
11679 it_before = *it;
11681 PRODUCE_GLYPHS (it);
11683 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11684 i = 0;
11685 x = it_before.current_x;
11686 while (i < nglyphs)
11688 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11690 if (x + glyph->pixel_width > max_x)
11692 /* Glyph doesn't fit on line. Backtrack. */
11693 row->used[TEXT_AREA] = n_glyphs_before;
11694 *it = it_before;
11695 /* If this is the only glyph on this line, it will never fit on the
11696 tool-bar, so skip it. But ensure there is at least one glyph,
11697 so we don't accidentally disable the tool-bar. */
11698 if (n_glyphs_before == 0
11699 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11700 break;
11701 goto out;
11704 ++it->hpos;
11705 x += glyph->pixel_width;
11706 ++i;
11709 /* Stop at line end. */
11710 if (ITERATOR_AT_END_OF_LINE_P (it))
11711 break;
11713 set_iterator_to_next (it, 1);
11716 out:;
11718 row->displays_text_p = row->used[TEXT_AREA] != 0;
11720 /* Use default face for the border below the tool bar.
11722 FIXME: When auto-resize-tool-bars is grow-only, there is
11723 no additional border below the possibly empty tool-bar lines.
11724 So to make the extra empty lines look "normal", we have to
11725 use the tool-bar face for the border too. */
11726 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11727 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11728 it->face_id = DEFAULT_FACE_ID;
11730 extend_face_to_end_of_line (it);
11731 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11732 last->right_box_line_p = 1;
11733 if (last == row->glyphs[TEXT_AREA])
11734 last->left_box_line_p = 1;
11736 /* Make line the desired height and center it vertically. */
11737 if ((height -= it->max_ascent + it->max_descent) > 0)
11739 /* Don't add more than one line height. */
11740 height %= FRAME_LINE_HEIGHT (it->f);
11741 it->max_ascent += height / 2;
11742 it->max_descent += (height + 1) / 2;
11745 compute_line_metrics (it);
11747 /* If line is empty, make it occupy the rest of the tool-bar. */
11748 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11750 row->height = row->phys_height = it->last_visible_y - row->y;
11751 row->visible_height = row->height;
11752 row->ascent = row->phys_ascent = 0;
11753 row->extra_line_spacing = 0;
11756 row->full_width_p = 1;
11757 row->continued_p = 0;
11758 row->truncated_on_left_p = 0;
11759 row->truncated_on_right_p = 0;
11761 it->current_x = it->hpos = 0;
11762 it->current_y += row->height;
11763 ++it->vpos;
11764 ++it->glyph_row;
11768 /* Max tool-bar height. */
11770 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11771 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11773 /* Value is the number of screen lines needed to make all tool-bar
11774 items of frame F visible. The number of actual rows needed is
11775 returned in *N_ROWS if non-NULL. */
11777 static int
11778 tool_bar_lines_needed (struct frame *f, int *n_rows)
11780 struct window *w = XWINDOW (f->tool_bar_window);
11781 struct it it;
11782 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11783 the desired matrix, so use (unused) mode-line row as temporary row to
11784 avoid destroying the first tool-bar row. */
11785 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11787 /* Initialize an iterator for iteration over
11788 F->desired_tool_bar_string in the tool-bar window of frame F. */
11789 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11790 it.first_visible_x = 0;
11791 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11792 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11793 it.paragraph_embedding = L2R;
11795 while (!ITERATOR_AT_END_P (&it))
11797 clear_glyph_row (temp_row);
11798 it.glyph_row = temp_row;
11799 display_tool_bar_line (&it, -1);
11801 clear_glyph_row (temp_row);
11803 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11804 if (n_rows)
11805 *n_rows = it.vpos > 0 ? it.vpos : -1;
11807 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11811 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11812 0, 1, 0,
11813 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11814 If FRAME is nil or omitted, use the selected frame. */)
11815 (Lisp_Object frame)
11817 struct frame *f = decode_any_frame (frame);
11818 struct window *w;
11819 int nlines = 0;
11821 if (WINDOWP (f->tool_bar_window)
11822 && (w = XWINDOW (f->tool_bar_window),
11823 WINDOW_TOTAL_LINES (w) > 0))
11825 update_tool_bar (f, 1);
11826 if (f->n_tool_bar_items)
11828 build_desired_tool_bar_string (f);
11829 nlines = tool_bar_lines_needed (f, NULL);
11833 return make_number (nlines);
11837 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11838 height should be changed. */
11840 static int
11841 redisplay_tool_bar (struct frame *f)
11843 struct window *w;
11844 struct it it;
11845 struct glyph_row *row;
11847 #if defined (USE_GTK) || defined (HAVE_NS)
11848 if (FRAME_EXTERNAL_TOOL_BAR (f))
11849 update_frame_tool_bar (f);
11850 return 0;
11851 #endif
11853 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11854 do anything. This means you must start with tool-bar-lines
11855 non-zero to get the auto-sizing effect. Or in other words, you
11856 can turn off tool-bars by specifying tool-bar-lines zero. */
11857 if (!WINDOWP (f->tool_bar_window)
11858 || (w = XWINDOW (f->tool_bar_window),
11859 WINDOW_TOTAL_LINES (w) == 0))
11860 return 0;
11862 /* Set up an iterator for the tool-bar window. */
11863 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11864 it.first_visible_x = 0;
11865 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11866 row = it.glyph_row;
11868 /* Build a string that represents the contents of the tool-bar. */
11869 build_desired_tool_bar_string (f);
11870 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11871 /* FIXME: This should be controlled by a user option. But it
11872 doesn't make sense to have an R2L tool bar if the menu bar cannot
11873 be drawn also R2L, and making the menu bar R2L is tricky due
11874 toolkit-specific code that implements it. If an R2L tool bar is
11875 ever supported, display_tool_bar_line should also be augmented to
11876 call unproduce_glyphs like display_line and display_string
11877 do. */
11878 it.paragraph_embedding = L2R;
11880 if (f->n_tool_bar_rows == 0)
11882 int nlines;
11884 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11885 nlines != WINDOW_TOTAL_LINES (w)))
11887 Lisp_Object frame;
11888 int old_height = WINDOW_TOTAL_LINES (w);
11890 XSETFRAME (frame, f);
11891 Fmodify_frame_parameters (frame,
11892 Fcons (Fcons (Qtool_bar_lines,
11893 make_number (nlines)),
11894 Qnil));
11895 if (WINDOW_TOTAL_LINES (w) != old_height)
11897 clear_glyph_matrix (w->desired_matrix);
11898 fonts_changed_p = 1;
11899 return 1;
11904 /* Display as many lines as needed to display all tool-bar items. */
11906 if (f->n_tool_bar_rows > 0)
11908 int border, rows, height, extra;
11910 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11911 border = XINT (Vtool_bar_border);
11912 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11913 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11914 else if (EQ (Vtool_bar_border, Qborder_width))
11915 border = f->border_width;
11916 else
11917 border = 0;
11918 if (border < 0)
11919 border = 0;
11921 rows = f->n_tool_bar_rows;
11922 height = max (1, (it.last_visible_y - border) / rows);
11923 extra = it.last_visible_y - border - height * rows;
11925 while (it.current_y < it.last_visible_y)
11927 int h = 0;
11928 if (extra > 0 && rows-- > 0)
11930 h = (extra + rows - 1) / rows;
11931 extra -= h;
11933 display_tool_bar_line (&it, height + h);
11936 else
11938 while (it.current_y < it.last_visible_y)
11939 display_tool_bar_line (&it, 0);
11942 /* It doesn't make much sense to try scrolling in the tool-bar
11943 window, so don't do it. */
11944 w->desired_matrix->no_scrolling_p = 1;
11945 w->must_be_updated_p = 1;
11947 if (!NILP (Vauto_resize_tool_bars))
11949 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11950 int change_height_p = 0;
11952 /* If we couldn't display everything, change the tool-bar's
11953 height if there is room for more. */
11954 if (IT_STRING_CHARPOS (it) < it.end_charpos
11955 && it.current_y < max_tool_bar_height)
11956 change_height_p = 1;
11958 row = it.glyph_row - 1;
11960 /* If there are blank lines at the end, except for a partially
11961 visible blank line at the end that is smaller than
11962 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11963 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11964 && row->height >= FRAME_LINE_HEIGHT (f))
11965 change_height_p = 1;
11967 /* If row displays tool-bar items, but is partially visible,
11968 change the tool-bar's height. */
11969 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11970 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11971 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11972 change_height_p = 1;
11974 /* Resize windows as needed by changing the `tool-bar-lines'
11975 frame parameter. */
11976 if (change_height_p)
11978 Lisp_Object frame;
11979 int old_height = WINDOW_TOTAL_LINES (w);
11980 int nrows;
11981 int nlines = tool_bar_lines_needed (f, &nrows);
11983 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11984 && !f->minimize_tool_bar_window_p)
11985 ? (nlines > old_height)
11986 : (nlines != old_height));
11987 f->minimize_tool_bar_window_p = 0;
11989 if (change_height_p)
11991 XSETFRAME (frame, f);
11992 Fmodify_frame_parameters (frame,
11993 Fcons (Fcons (Qtool_bar_lines,
11994 make_number (nlines)),
11995 Qnil));
11996 if (WINDOW_TOTAL_LINES (w) != old_height)
11998 clear_glyph_matrix (w->desired_matrix);
11999 f->n_tool_bar_rows = nrows;
12000 fonts_changed_p = 1;
12001 return 1;
12007 f->minimize_tool_bar_window_p = 0;
12008 return 0;
12012 /* Get information about the tool-bar item which is displayed in GLYPH
12013 on frame F. Return in *PROP_IDX the index where tool-bar item
12014 properties start in F->tool_bar_items. Value is zero if
12015 GLYPH doesn't display a tool-bar item. */
12017 static int
12018 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12020 Lisp_Object prop;
12021 int success_p;
12022 int charpos;
12024 /* This function can be called asynchronously, which means we must
12025 exclude any possibility that Fget_text_property signals an
12026 error. */
12027 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12028 charpos = max (0, charpos);
12030 /* Get the text property `menu-item' at pos. The value of that
12031 property is the start index of this item's properties in
12032 F->tool_bar_items. */
12033 prop = Fget_text_property (make_number (charpos),
12034 Qmenu_item, f->current_tool_bar_string);
12035 if (INTEGERP (prop))
12037 *prop_idx = XINT (prop);
12038 success_p = 1;
12040 else
12041 success_p = 0;
12043 return success_p;
12047 /* Get information about the tool-bar item at position X/Y on frame F.
12048 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12049 the current matrix of the tool-bar window of F, or NULL if not
12050 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12051 item in F->tool_bar_items. Value is
12053 -1 if X/Y is not on a tool-bar item
12054 0 if X/Y is on the same item that was highlighted before.
12055 1 otherwise. */
12057 static int
12058 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12059 int *hpos, int *vpos, int *prop_idx)
12061 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12062 struct window *w = XWINDOW (f->tool_bar_window);
12063 int area;
12065 /* Find the glyph under X/Y. */
12066 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12067 if (*glyph == NULL)
12068 return -1;
12070 /* Get the start of this tool-bar item's properties in
12071 f->tool_bar_items. */
12072 if (!tool_bar_item_info (f, *glyph, prop_idx))
12073 return -1;
12075 /* Is mouse on the highlighted item? */
12076 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12077 && *vpos >= hlinfo->mouse_face_beg_row
12078 && *vpos <= hlinfo->mouse_face_end_row
12079 && (*vpos > hlinfo->mouse_face_beg_row
12080 || *hpos >= hlinfo->mouse_face_beg_col)
12081 && (*vpos < hlinfo->mouse_face_end_row
12082 || *hpos < hlinfo->mouse_face_end_col
12083 || hlinfo->mouse_face_past_end))
12084 return 0;
12086 return 1;
12090 /* EXPORT:
12091 Handle mouse button event on the tool-bar of frame F, at
12092 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12093 0 for button release. MODIFIERS is event modifiers for button
12094 release. */
12096 void
12097 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12098 int modifiers)
12100 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12101 struct window *w = XWINDOW (f->tool_bar_window);
12102 int hpos, vpos, prop_idx;
12103 struct glyph *glyph;
12104 Lisp_Object enabled_p;
12106 /* If not on the highlighted tool-bar item, return. */
12107 frame_to_window_pixel_xy (w, &x, &y);
12108 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12109 return;
12111 /* If item is disabled, do nothing. */
12112 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12113 if (NILP (enabled_p))
12114 return;
12116 if (down_p)
12118 /* Show item in pressed state. */
12119 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12120 last_tool_bar_item = prop_idx;
12122 else
12124 Lisp_Object key, frame;
12125 struct input_event event;
12126 EVENT_INIT (event);
12128 /* Show item in released state. */
12129 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12131 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12133 XSETFRAME (frame, f);
12134 event.kind = TOOL_BAR_EVENT;
12135 event.frame_or_window = frame;
12136 event.arg = frame;
12137 kbd_buffer_store_event (&event);
12139 event.kind = TOOL_BAR_EVENT;
12140 event.frame_or_window = frame;
12141 event.arg = key;
12142 event.modifiers = modifiers;
12143 kbd_buffer_store_event (&event);
12144 last_tool_bar_item = -1;
12149 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12150 tool-bar window-relative coordinates X/Y. Called from
12151 note_mouse_highlight. */
12153 static void
12154 note_tool_bar_highlight (struct frame *f, int x, int y)
12156 Lisp_Object window = f->tool_bar_window;
12157 struct window *w = XWINDOW (window);
12158 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12159 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12160 int hpos, vpos;
12161 struct glyph *glyph;
12162 struct glyph_row *row;
12163 int i;
12164 Lisp_Object enabled_p;
12165 int prop_idx;
12166 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12167 int mouse_down_p, rc;
12169 /* Function note_mouse_highlight is called with negative X/Y
12170 values when mouse moves outside of the frame. */
12171 if (x <= 0 || y <= 0)
12173 clear_mouse_face (hlinfo);
12174 return;
12177 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12178 if (rc < 0)
12180 /* Not on tool-bar item. */
12181 clear_mouse_face (hlinfo);
12182 return;
12184 else if (rc == 0)
12185 /* On same tool-bar item as before. */
12186 goto set_help_echo;
12188 clear_mouse_face (hlinfo);
12190 /* Mouse is down, but on different tool-bar item? */
12191 mouse_down_p = (dpyinfo->grabbed
12192 && f == last_mouse_frame
12193 && FRAME_LIVE_P (f));
12194 if (mouse_down_p
12195 && last_tool_bar_item != prop_idx)
12196 return;
12198 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12200 /* If tool-bar item is not enabled, don't highlight it. */
12201 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12202 if (!NILP (enabled_p))
12204 /* Compute the x-position of the glyph. In front and past the
12205 image is a space. We include this in the highlighted area. */
12206 row = MATRIX_ROW (w->current_matrix, vpos);
12207 for (i = x = 0; i < hpos; ++i)
12208 x += row->glyphs[TEXT_AREA][i].pixel_width;
12210 /* Record this as the current active region. */
12211 hlinfo->mouse_face_beg_col = hpos;
12212 hlinfo->mouse_face_beg_row = vpos;
12213 hlinfo->mouse_face_beg_x = x;
12214 hlinfo->mouse_face_beg_y = row->y;
12215 hlinfo->mouse_face_past_end = 0;
12217 hlinfo->mouse_face_end_col = hpos + 1;
12218 hlinfo->mouse_face_end_row = vpos;
12219 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12220 hlinfo->mouse_face_end_y = row->y;
12221 hlinfo->mouse_face_window = window;
12222 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12224 /* Display it as active. */
12225 show_mouse_face (hlinfo, draw);
12228 set_help_echo:
12230 /* Set help_echo_string to a help string to display for this tool-bar item.
12231 XTread_socket does the rest. */
12232 help_echo_object = help_echo_window = Qnil;
12233 help_echo_pos = -1;
12234 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12235 if (NILP (help_echo_string))
12236 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12239 #endif /* HAVE_WINDOW_SYSTEM */
12243 /************************************************************************
12244 Horizontal scrolling
12245 ************************************************************************/
12247 static int hscroll_window_tree (Lisp_Object);
12248 static int hscroll_windows (Lisp_Object);
12250 /* For all leaf windows in the window tree rooted at WINDOW, set their
12251 hscroll value so that PT is (i) visible in the window, and (ii) so
12252 that it is not within a certain margin at the window's left and
12253 right border. Value is non-zero if any window's hscroll has been
12254 changed. */
12256 static int
12257 hscroll_window_tree (Lisp_Object window)
12259 int hscrolled_p = 0;
12260 int hscroll_relative_p = FLOATP (Vhscroll_step);
12261 int hscroll_step_abs = 0;
12262 double hscroll_step_rel = 0;
12264 if (hscroll_relative_p)
12266 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12267 if (hscroll_step_rel < 0)
12269 hscroll_relative_p = 0;
12270 hscroll_step_abs = 0;
12273 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12275 hscroll_step_abs = XINT (Vhscroll_step);
12276 if (hscroll_step_abs < 0)
12277 hscroll_step_abs = 0;
12279 else
12280 hscroll_step_abs = 0;
12282 while (WINDOWP (window))
12284 struct window *w = XWINDOW (window);
12286 if (WINDOWP (w->contents))
12287 hscrolled_p |= hscroll_window_tree (w->contents);
12288 else if (w->cursor.vpos >= 0)
12290 int h_margin;
12291 int text_area_width;
12292 struct glyph_row *current_cursor_row
12293 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12294 struct glyph_row *desired_cursor_row
12295 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12296 struct glyph_row *cursor_row
12297 = (desired_cursor_row->enabled_p
12298 ? desired_cursor_row
12299 : current_cursor_row);
12300 int row_r2l_p = cursor_row->reversed_p;
12302 text_area_width = window_box_width (w, TEXT_AREA);
12304 /* Scroll when cursor is inside this scroll margin. */
12305 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12307 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12308 /* For left-to-right rows, hscroll when cursor is either
12309 (i) inside the right hscroll margin, or (ii) if it is
12310 inside the left margin and the window is already
12311 hscrolled. */
12312 && ((!row_r2l_p
12313 && ((w->hscroll
12314 && w->cursor.x <= h_margin)
12315 || (cursor_row->enabled_p
12316 && cursor_row->truncated_on_right_p
12317 && (w->cursor.x >= text_area_width - h_margin))))
12318 /* For right-to-left rows, the logic is similar,
12319 except that rules for scrolling to left and right
12320 are reversed. E.g., if cursor.x <= h_margin, we
12321 need to hscroll "to the right" unconditionally,
12322 and that will scroll the screen to the left so as
12323 to reveal the next portion of the row. */
12324 || (row_r2l_p
12325 && ((cursor_row->enabled_p
12326 /* FIXME: It is confusing to set the
12327 truncated_on_right_p flag when R2L rows
12328 are actually truncated on the left. */
12329 && cursor_row->truncated_on_right_p
12330 && w->cursor.x <= h_margin)
12331 || (w->hscroll
12332 && (w->cursor.x >= text_area_width - h_margin))))))
12334 struct it it;
12335 ptrdiff_t hscroll;
12336 struct buffer *saved_current_buffer;
12337 ptrdiff_t pt;
12338 int wanted_x;
12340 /* Find point in a display of infinite width. */
12341 saved_current_buffer = current_buffer;
12342 current_buffer = XBUFFER (w->contents);
12344 if (w == XWINDOW (selected_window))
12345 pt = PT;
12346 else
12347 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12349 /* Move iterator to pt starting at cursor_row->start in
12350 a line with infinite width. */
12351 init_to_row_start (&it, w, cursor_row);
12352 it.last_visible_x = INFINITY;
12353 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12354 current_buffer = saved_current_buffer;
12356 /* Position cursor in window. */
12357 if (!hscroll_relative_p && hscroll_step_abs == 0)
12358 hscroll = max (0, (it.current_x
12359 - (ITERATOR_AT_END_OF_LINE_P (&it)
12360 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12361 : (text_area_width / 2))))
12362 / FRAME_COLUMN_WIDTH (it.f);
12363 else if ((!row_r2l_p
12364 && w->cursor.x >= text_area_width - h_margin)
12365 || (row_r2l_p && w->cursor.x <= h_margin))
12367 if (hscroll_relative_p)
12368 wanted_x = text_area_width * (1 - hscroll_step_rel)
12369 - h_margin;
12370 else
12371 wanted_x = text_area_width
12372 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12373 - h_margin;
12374 hscroll
12375 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12377 else
12379 if (hscroll_relative_p)
12380 wanted_x = text_area_width * hscroll_step_rel
12381 + h_margin;
12382 else
12383 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12384 + h_margin;
12385 hscroll
12386 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12388 hscroll = max (hscroll, w->min_hscroll);
12390 /* Don't prevent redisplay optimizations if hscroll
12391 hasn't changed, as it will unnecessarily slow down
12392 redisplay. */
12393 if (w->hscroll != hscroll)
12395 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12396 w->hscroll = hscroll;
12397 hscrolled_p = 1;
12402 window = w->next;
12405 /* Value is non-zero if hscroll of any leaf window has been changed. */
12406 return hscrolled_p;
12410 /* Set hscroll so that cursor is visible and not inside horizontal
12411 scroll margins for all windows in the tree rooted at WINDOW. See
12412 also hscroll_window_tree above. Value is non-zero if any window's
12413 hscroll has been changed. If it has, desired matrices on the frame
12414 of WINDOW are cleared. */
12416 static int
12417 hscroll_windows (Lisp_Object window)
12419 int hscrolled_p = hscroll_window_tree (window);
12420 if (hscrolled_p)
12421 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12422 return hscrolled_p;
12427 /************************************************************************
12428 Redisplay
12429 ************************************************************************/
12431 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12432 to a non-zero value. This is sometimes handy to have in a debugger
12433 session. */
12435 #ifdef GLYPH_DEBUG
12437 /* First and last unchanged row for try_window_id. */
12439 static int debug_first_unchanged_at_end_vpos;
12440 static int debug_last_unchanged_at_beg_vpos;
12442 /* Delta vpos and y. */
12444 static int debug_dvpos, debug_dy;
12446 /* Delta in characters and bytes for try_window_id. */
12448 static ptrdiff_t debug_delta, debug_delta_bytes;
12450 /* Values of window_end_pos and window_end_vpos at the end of
12451 try_window_id. */
12453 static ptrdiff_t debug_end_vpos;
12455 /* Append a string to W->desired_matrix->method. FMT is a printf
12456 format string. If trace_redisplay_p is non-zero also printf the
12457 resulting string to stderr. */
12459 static void debug_method_add (struct window *, char const *, ...)
12460 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12462 static void
12463 debug_method_add (struct window *w, char const *fmt, ...)
12465 char *method = w->desired_matrix->method;
12466 int len = strlen (method);
12467 int size = sizeof w->desired_matrix->method;
12468 int remaining = size - len - 1;
12469 va_list ap;
12471 if (len && remaining)
12473 method[len] = '|';
12474 --remaining, ++len;
12477 va_start (ap, fmt);
12478 vsnprintf (method + len, remaining + 1, fmt, ap);
12479 va_end (ap);
12481 if (trace_redisplay_p)
12482 fprintf (stderr, "%p (%s): %s\n",
12484 ((BUFFERP (w->contents)
12485 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12486 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12487 : "no buffer"),
12488 method + len);
12491 #endif /* GLYPH_DEBUG */
12494 /* Value is non-zero if all changes in window W, which displays
12495 current_buffer, are in the text between START and END. START is a
12496 buffer position, END is given as a distance from Z. Used in
12497 redisplay_internal for display optimization. */
12499 static int
12500 text_outside_line_unchanged_p (struct window *w,
12501 ptrdiff_t start, ptrdiff_t end)
12503 int unchanged_p = 1;
12505 /* If text or overlays have changed, see where. */
12506 if (window_outdated (w))
12508 /* Gap in the line? */
12509 if (GPT < start || Z - GPT < end)
12510 unchanged_p = 0;
12512 /* Changes start in front of the line, or end after it? */
12513 if (unchanged_p
12514 && (BEG_UNCHANGED < start - 1
12515 || END_UNCHANGED < end))
12516 unchanged_p = 0;
12518 /* If selective display, can't optimize if changes start at the
12519 beginning of the line. */
12520 if (unchanged_p
12521 && INTEGERP (BVAR (current_buffer, selective_display))
12522 && XINT (BVAR (current_buffer, selective_display)) > 0
12523 && (BEG_UNCHANGED < start || GPT <= start))
12524 unchanged_p = 0;
12526 /* If there are overlays at the start or end of the line, these
12527 may have overlay strings with newlines in them. A change at
12528 START, for instance, may actually concern the display of such
12529 overlay strings as well, and they are displayed on different
12530 lines. So, quickly rule out this case. (For the future, it
12531 might be desirable to implement something more telling than
12532 just BEG/END_UNCHANGED.) */
12533 if (unchanged_p)
12535 if (BEG + BEG_UNCHANGED == start
12536 && overlay_touches_p (start))
12537 unchanged_p = 0;
12538 if (END_UNCHANGED == end
12539 && overlay_touches_p (Z - end))
12540 unchanged_p = 0;
12543 /* Under bidi reordering, adding or deleting a character in the
12544 beginning of a paragraph, before the first strong directional
12545 character, can change the base direction of the paragraph (unless
12546 the buffer specifies a fixed paragraph direction), which will
12547 require to redisplay the whole paragraph. It might be worthwhile
12548 to find the paragraph limits and widen the range of redisplayed
12549 lines to that, but for now just give up this optimization. */
12550 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12551 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12552 unchanged_p = 0;
12555 return unchanged_p;
12559 /* Do a frame update, taking possible shortcuts into account. This is
12560 the main external entry point for redisplay.
12562 If the last redisplay displayed an echo area message and that message
12563 is no longer requested, we clear the echo area or bring back the
12564 mini-buffer if that is in use. */
12566 void
12567 redisplay (void)
12569 redisplay_internal ();
12573 static Lisp_Object
12574 overlay_arrow_string_or_property (Lisp_Object var)
12576 Lisp_Object val;
12578 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12579 return val;
12581 return Voverlay_arrow_string;
12584 /* Return 1 if there are any overlay-arrows in current_buffer. */
12585 static int
12586 overlay_arrow_in_current_buffer_p (void)
12588 Lisp_Object vlist;
12590 for (vlist = Voverlay_arrow_variable_list;
12591 CONSP (vlist);
12592 vlist = XCDR (vlist))
12594 Lisp_Object var = XCAR (vlist);
12595 Lisp_Object val;
12597 if (!SYMBOLP (var))
12598 continue;
12599 val = find_symbol_value (var);
12600 if (MARKERP (val)
12601 && current_buffer == XMARKER (val)->buffer)
12602 return 1;
12604 return 0;
12608 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12609 has changed. */
12611 static int
12612 overlay_arrows_changed_p (void)
12614 Lisp_Object vlist;
12616 for (vlist = Voverlay_arrow_variable_list;
12617 CONSP (vlist);
12618 vlist = XCDR (vlist))
12620 Lisp_Object var = XCAR (vlist);
12621 Lisp_Object val, pstr;
12623 if (!SYMBOLP (var))
12624 continue;
12625 val = find_symbol_value (var);
12626 if (!MARKERP (val))
12627 continue;
12628 if (! EQ (COERCE_MARKER (val),
12629 Fget (var, Qlast_arrow_position))
12630 || ! (pstr = overlay_arrow_string_or_property (var),
12631 EQ (pstr, Fget (var, Qlast_arrow_string))))
12632 return 1;
12634 return 0;
12637 /* Mark overlay arrows to be updated on next redisplay. */
12639 static void
12640 update_overlay_arrows (int up_to_date)
12642 Lisp_Object vlist;
12644 for (vlist = Voverlay_arrow_variable_list;
12645 CONSP (vlist);
12646 vlist = XCDR (vlist))
12648 Lisp_Object var = XCAR (vlist);
12650 if (!SYMBOLP (var))
12651 continue;
12653 if (up_to_date > 0)
12655 Lisp_Object val = find_symbol_value (var);
12656 Fput (var, Qlast_arrow_position,
12657 COERCE_MARKER (val));
12658 Fput (var, Qlast_arrow_string,
12659 overlay_arrow_string_or_property (var));
12661 else if (up_to_date < 0
12662 || !NILP (Fget (var, Qlast_arrow_position)))
12664 Fput (var, Qlast_arrow_position, Qt);
12665 Fput (var, Qlast_arrow_string, Qt);
12671 /* Return overlay arrow string to display at row.
12672 Return integer (bitmap number) for arrow bitmap in left fringe.
12673 Return nil if no overlay arrow. */
12675 static Lisp_Object
12676 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12678 Lisp_Object vlist;
12680 for (vlist = Voverlay_arrow_variable_list;
12681 CONSP (vlist);
12682 vlist = XCDR (vlist))
12684 Lisp_Object var = XCAR (vlist);
12685 Lisp_Object val;
12687 if (!SYMBOLP (var))
12688 continue;
12690 val = find_symbol_value (var);
12692 if (MARKERP (val)
12693 && current_buffer == XMARKER (val)->buffer
12694 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12696 if (FRAME_WINDOW_P (it->f)
12697 /* FIXME: if ROW->reversed_p is set, this should test
12698 the right fringe, not the left one. */
12699 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12701 #ifdef HAVE_WINDOW_SYSTEM
12702 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12704 int fringe_bitmap;
12705 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12706 return make_number (fringe_bitmap);
12708 #endif
12709 return make_number (-1); /* Use default arrow bitmap. */
12711 return overlay_arrow_string_or_property (var);
12715 return Qnil;
12718 /* Return 1 if point moved out of or into a composition. Otherwise
12719 return 0. PREV_BUF and PREV_PT are the last point buffer and
12720 position. BUF and PT are the current point buffer and position. */
12722 static int
12723 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12724 struct buffer *buf, ptrdiff_t pt)
12726 ptrdiff_t start, end;
12727 Lisp_Object prop;
12728 Lisp_Object buffer;
12730 XSETBUFFER (buffer, buf);
12731 /* Check a composition at the last point if point moved within the
12732 same buffer. */
12733 if (prev_buf == buf)
12735 if (prev_pt == pt)
12736 /* Point didn't move. */
12737 return 0;
12739 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12740 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12741 && COMPOSITION_VALID_P (start, end, prop)
12742 && start < prev_pt && end > prev_pt)
12743 /* The last point was within the composition. Return 1 iff
12744 point moved out of the composition. */
12745 return (pt <= start || pt >= end);
12748 /* Check a composition at the current point. */
12749 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12750 && find_composition (pt, -1, &start, &end, &prop, buffer)
12751 && COMPOSITION_VALID_P (start, end, prop)
12752 && start < pt && end > pt);
12756 /* Reconsider the setting of B->clip_changed which is displayed
12757 in window W. */
12759 static void
12760 reconsider_clip_changes (struct window *w, struct buffer *b)
12762 if (b->clip_changed
12763 && w->window_end_valid
12764 && w->current_matrix->buffer == b
12765 && w->current_matrix->zv == BUF_ZV (b)
12766 && w->current_matrix->begv == BUF_BEGV (b))
12767 b->clip_changed = 0;
12769 /* If display wasn't paused, and W is not a tool bar window, see if
12770 point has been moved into or out of a composition. In that case,
12771 we set b->clip_changed to 1 to force updating the screen. If
12772 b->clip_changed has already been set to 1, we can skip this
12773 check. */
12774 if (!b->clip_changed && BUFFERP (w->contents) && w->window_end_valid)
12776 ptrdiff_t pt;
12778 if (w == XWINDOW (selected_window))
12779 pt = PT;
12780 else
12781 pt = marker_position (w->pointm);
12783 if ((w->current_matrix->buffer != XBUFFER (w->contents)
12784 || pt != w->last_point)
12785 && check_point_in_composition (w->current_matrix->buffer,
12786 w->last_point,
12787 XBUFFER (w->contents), pt))
12788 b->clip_changed = 1;
12793 #define STOP_POLLING \
12794 do { if (! polling_stopped_here) stop_polling (); \
12795 polling_stopped_here = 1; } while (0)
12797 #define RESUME_POLLING \
12798 do { if (polling_stopped_here) start_polling (); \
12799 polling_stopped_here = 0; } while (0)
12802 /* Perhaps in the future avoid recentering windows if it
12803 is not necessary; currently that causes some problems. */
12805 static void
12806 redisplay_internal (void)
12808 struct window *w = XWINDOW (selected_window);
12809 struct window *sw;
12810 struct frame *fr;
12811 int pending;
12812 int must_finish = 0;
12813 struct text_pos tlbufpos, tlendpos;
12814 int number_of_visible_frames;
12815 ptrdiff_t count, count1;
12816 struct frame *sf;
12817 int polling_stopped_here = 0;
12818 Lisp_Object tail, frame;
12819 struct backtrace backtrace;
12821 /* Non-zero means redisplay has to consider all windows on all
12822 frames. Zero means, only selected_window is considered. */
12823 int consider_all_windows_p;
12825 /* Non-zero means redisplay has to redisplay the miniwindow. */
12826 int update_miniwindow_p = 0;
12828 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12830 /* No redisplay if running in batch mode or frame is not yet fully
12831 initialized, or redisplay is explicitly turned off by setting
12832 Vinhibit_redisplay. */
12833 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12834 || !NILP (Vinhibit_redisplay))
12835 return;
12837 /* Don't examine these until after testing Vinhibit_redisplay.
12838 When Emacs is shutting down, perhaps because its connection to
12839 X has dropped, we should not look at them at all. */
12840 fr = XFRAME (w->frame);
12841 sf = SELECTED_FRAME ();
12843 if (!fr->glyphs_initialized_p)
12844 return;
12846 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12847 if (popup_activated ())
12848 return;
12849 #endif
12851 /* I don't think this happens but let's be paranoid. */
12852 if (redisplaying_p)
12853 return;
12855 /* Record a function that clears redisplaying_p
12856 when we leave this function. */
12857 count = SPECPDL_INDEX ();
12858 record_unwind_protect (unwind_redisplay, selected_frame);
12859 redisplaying_p = 1;
12860 specbind (Qinhibit_free_realized_faces, Qnil);
12862 /* Record this function, so it appears on the profiler's backtraces. */
12863 backtrace.next = backtrace_list;
12864 backtrace.function = Qredisplay_internal;
12865 backtrace.args = &Qnil;
12866 backtrace.nargs = 0;
12867 backtrace.debug_on_exit = 0;
12868 backtrace_list = &backtrace;
12870 FOR_EACH_FRAME (tail, frame)
12871 XFRAME (frame)->already_hscrolled_p = 0;
12873 retry:
12874 /* Remember the currently selected window. */
12875 sw = w;
12877 pending = 0;
12878 reconsider_clip_changes (w, current_buffer);
12879 last_escape_glyph_frame = NULL;
12880 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12881 last_glyphless_glyph_frame = NULL;
12882 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12884 /* If new fonts have been loaded that make a glyph matrix adjustment
12885 necessary, do it. */
12886 if (fonts_changed_p)
12888 adjust_glyphs (NULL);
12889 ++windows_or_buffers_changed;
12890 fonts_changed_p = 0;
12893 /* If face_change_count is non-zero, init_iterator will free all
12894 realized faces, which includes the faces referenced from current
12895 matrices. So, we can't reuse current matrices in this case. */
12896 if (face_change_count)
12897 ++windows_or_buffers_changed;
12899 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12900 && FRAME_TTY (sf)->previous_frame != sf)
12902 /* Since frames on a single ASCII terminal share the same
12903 display area, displaying a different frame means redisplay
12904 the whole thing. */
12905 windows_or_buffers_changed++;
12906 SET_FRAME_GARBAGED (sf);
12907 #ifndef DOS_NT
12908 set_tty_color_mode (FRAME_TTY (sf), sf);
12909 #endif
12910 FRAME_TTY (sf)->previous_frame = sf;
12913 /* Set the visible flags for all frames. Do this before checking for
12914 resized or garbaged frames; they want to know if their frames are
12915 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12916 number_of_visible_frames = 0;
12918 FOR_EACH_FRAME (tail, frame)
12920 struct frame *f = XFRAME (frame);
12922 if (FRAME_VISIBLE_P (f))
12923 ++number_of_visible_frames;
12924 clear_desired_matrices (f);
12927 /* Notice any pending interrupt request to change frame size. */
12928 do_pending_window_change (1);
12930 /* do_pending_window_change could change the selected_window due to
12931 frame resizing which makes the selected window too small. */
12932 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12934 sw = w;
12935 reconsider_clip_changes (w, current_buffer);
12938 /* Clear frames marked as garbaged. */
12939 clear_garbaged_frames ();
12941 /* Build menubar and tool-bar items. */
12942 if (NILP (Vmemory_full))
12943 prepare_menu_bars ();
12945 if (windows_or_buffers_changed)
12946 update_mode_lines++;
12948 /* Detect case that we need to write or remove a star in the mode line. */
12949 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
12951 w->update_mode_line = 1;
12952 if (buffer_shared_and_changed ())
12953 update_mode_lines++;
12956 /* Avoid invocation of point motion hooks by `current_column' below. */
12957 count1 = SPECPDL_INDEX ();
12958 specbind (Qinhibit_point_motion_hooks, Qt);
12960 if (mode_line_update_needed (w))
12961 w->update_mode_line = 1;
12963 unbind_to (count1, Qnil);
12965 consider_all_windows_p = (update_mode_lines
12966 || buffer_shared_and_changed ()
12967 || cursor_type_changed);
12969 /* If specs for an arrow have changed, do thorough redisplay
12970 to ensure we remove any arrow that should no longer exist. */
12971 if (overlay_arrows_changed_p ())
12972 consider_all_windows_p = windows_or_buffers_changed = 1;
12974 /* Normally the message* functions will have already displayed and
12975 updated the echo area, but the frame may have been trashed, or
12976 the update may have been preempted, so display the echo area
12977 again here. Checking message_cleared_p captures the case that
12978 the echo area should be cleared. */
12979 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12980 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12981 || (message_cleared_p
12982 && minibuf_level == 0
12983 /* If the mini-window is currently selected, this means the
12984 echo-area doesn't show through. */
12985 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12987 int window_height_changed_p = echo_area_display (0);
12989 if (message_cleared_p)
12990 update_miniwindow_p = 1;
12992 must_finish = 1;
12994 /* If we don't display the current message, don't clear the
12995 message_cleared_p flag, because, if we did, we wouldn't clear
12996 the echo area in the next redisplay which doesn't preserve
12997 the echo area. */
12998 if (!display_last_displayed_message_p)
12999 message_cleared_p = 0;
13001 if (fonts_changed_p)
13002 goto retry;
13003 else if (window_height_changed_p)
13005 consider_all_windows_p = 1;
13006 ++update_mode_lines;
13007 ++windows_or_buffers_changed;
13009 /* If window configuration was changed, frames may have been
13010 marked garbaged. Clear them or we will experience
13011 surprises wrt scrolling. */
13012 clear_garbaged_frames ();
13015 else if (EQ (selected_window, minibuf_window)
13016 && (current_buffer->clip_changed || window_outdated (w))
13017 && resize_mini_window (w, 0))
13019 /* Resized active mini-window to fit the size of what it is
13020 showing if its contents might have changed. */
13021 must_finish = 1;
13022 /* FIXME: this causes all frames to be updated, which seems unnecessary
13023 since only the current frame needs to be considered. This function
13024 needs to be rewritten with two variables, consider_all_windows and
13025 consider_all_frames. */
13026 consider_all_windows_p = 1;
13027 ++windows_or_buffers_changed;
13028 ++update_mode_lines;
13030 /* If window configuration was changed, frames may have been
13031 marked garbaged. Clear them or we will experience
13032 surprises wrt scrolling. */
13033 clear_garbaged_frames ();
13036 /* If showing the region, and mark has changed, we must redisplay
13037 the whole window. The assignment to this_line_start_pos prevents
13038 the optimization directly below this if-statement. */
13039 if (((!NILP (Vtransient_mark_mode)
13040 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13041 != (w->region_showing > 0))
13042 || (w->region_showing
13043 && w->region_showing
13044 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13045 CHARPOS (this_line_start_pos) = 0;
13047 /* Optimize the case that only the line containing the cursor in the
13048 selected window has changed. Variables starting with this_ are
13049 set in display_line and record information about the line
13050 containing the cursor. */
13051 tlbufpos = this_line_start_pos;
13052 tlendpos = this_line_end_pos;
13053 if (!consider_all_windows_p
13054 && CHARPOS (tlbufpos) > 0
13055 && !w->update_mode_line
13056 && !current_buffer->clip_changed
13057 && !current_buffer->prevent_redisplay_optimizations_p
13058 && FRAME_VISIBLE_P (XFRAME (w->frame))
13059 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13060 /* Make sure recorded data applies to current buffer, etc. */
13061 && this_line_buffer == current_buffer
13062 && current_buffer == XBUFFER (w->contents)
13063 && !w->force_start
13064 && !w->optional_new_start
13065 /* Point must be on the line that we have info recorded about. */
13066 && PT >= CHARPOS (tlbufpos)
13067 && PT <= Z - CHARPOS (tlendpos)
13068 /* All text outside that line, including its final newline,
13069 must be unchanged. */
13070 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13071 CHARPOS (tlendpos)))
13073 if (CHARPOS (tlbufpos) > BEGV
13074 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13075 && (CHARPOS (tlbufpos) == ZV
13076 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13077 /* Former continuation line has disappeared by becoming empty. */
13078 goto cancel;
13079 else if (window_outdated (w) || MINI_WINDOW_P (w))
13081 /* We have to handle the case of continuation around a
13082 wide-column character (see the comment in indent.c around
13083 line 1340).
13085 For instance, in the following case:
13087 -------- Insert --------
13088 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13089 J_I_ ==> J_I_ `^^' are cursors.
13090 ^^ ^^
13091 -------- --------
13093 As we have to redraw the line above, we cannot use this
13094 optimization. */
13096 struct it it;
13097 int line_height_before = this_line_pixel_height;
13099 /* Note that start_display will handle the case that the
13100 line starting at tlbufpos is a continuation line. */
13101 start_display (&it, w, tlbufpos);
13103 /* Implementation note: It this still necessary? */
13104 if (it.current_x != this_line_start_x)
13105 goto cancel;
13107 TRACE ((stderr, "trying display optimization 1\n"));
13108 w->cursor.vpos = -1;
13109 overlay_arrow_seen = 0;
13110 it.vpos = this_line_vpos;
13111 it.current_y = this_line_y;
13112 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13113 display_line (&it);
13115 /* If line contains point, is not continued,
13116 and ends at same distance from eob as before, we win. */
13117 if (w->cursor.vpos >= 0
13118 /* Line is not continued, otherwise this_line_start_pos
13119 would have been set to 0 in display_line. */
13120 && CHARPOS (this_line_start_pos)
13121 /* Line ends as before. */
13122 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13123 /* Line has same height as before. Otherwise other lines
13124 would have to be shifted up or down. */
13125 && this_line_pixel_height == line_height_before)
13127 /* If this is not the window's last line, we must adjust
13128 the charstarts of the lines below. */
13129 if (it.current_y < it.last_visible_y)
13131 struct glyph_row *row
13132 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13133 ptrdiff_t delta, delta_bytes;
13135 /* We used to distinguish between two cases here,
13136 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13137 when the line ends in a newline or the end of the
13138 buffer's accessible portion. But both cases did
13139 the same, so they were collapsed. */
13140 delta = (Z
13141 - CHARPOS (tlendpos)
13142 - MATRIX_ROW_START_CHARPOS (row));
13143 delta_bytes = (Z_BYTE
13144 - BYTEPOS (tlendpos)
13145 - MATRIX_ROW_START_BYTEPOS (row));
13147 increment_matrix_positions (w->current_matrix,
13148 this_line_vpos + 1,
13149 w->current_matrix->nrows,
13150 delta, delta_bytes);
13153 /* If this row displays text now but previously didn't,
13154 or vice versa, w->window_end_vpos may have to be
13155 adjusted. */
13156 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13158 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13159 wset_window_end_vpos (w, make_number (this_line_vpos));
13161 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13162 && this_line_vpos > 0)
13163 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13164 w->window_end_valid = 0;
13166 /* Update hint: No need to try to scroll in update_window. */
13167 w->desired_matrix->no_scrolling_p = 1;
13169 #ifdef GLYPH_DEBUG
13170 *w->desired_matrix->method = 0;
13171 debug_method_add (w, "optimization 1");
13172 #endif
13173 #ifdef HAVE_WINDOW_SYSTEM
13174 update_window_fringes (w, 0);
13175 #endif
13176 goto update;
13178 else
13179 goto cancel;
13181 else if (/* Cursor position hasn't changed. */
13182 PT == w->last_point
13183 /* Make sure the cursor was last displayed
13184 in this window. Otherwise we have to reposition it. */
13185 && 0 <= w->cursor.vpos
13186 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13188 if (!must_finish)
13190 do_pending_window_change (1);
13191 /* If selected_window changed, redisplay again. */
13192 if (WINDOWP (selected_window)
13193 && (w = XWINDOW (selected_window)) != sw)
13194 goto retry;
13196 /* We used to always goto end_of_redisplay here, but this
13197 isn't enough if we have a blinking cursor. */
13198 if (w->cursor_off_p == w->last_cursor_off_p)
13199 goto end_of_redisplay;
13201 goto update;
13203 /* If highlighting the region, or if the cursor is in the echo area,
13204 then we can't just move the cursor. */
13205 else if (! (!NILP (Vtransient_mark_mode)
13206 && !NILP (BVAR (current_buffer, mark_active)))
13207 && (EQ (selected_window,
13208 BVAR (current_buffer, last_selected_window))
13209 || highlight_nonselected_windows)
13210 && !w->region_showing
13211 && NILP (Vshow_trailing_whitespace)
13212 && !cursor_in_echo_area)
13214 struct it it;
13215 struct glyph_row *row;
13217 /* Skip from tlbufpos to PT and see where it is. Note that
13218 PT may be in invisible text. If so, we will end at the
13219 next visible position. */
13220 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13221 NULL, DEFAULT_FACE_ID);
13222 it.current_x = this_line_start_x;
13223 it.current_y = this_line_y;
13224 it.vpos = this_line_vpos;
13226 /* The call to move_it_to stops in front of PT, but
13227 moves over before-strings. */
13228 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13230 if (it.vpos == this_line_vpos
13231 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13232 row->enabled_p))
13234 eassert (this_line_vpos == it.vpos);
13235 eassert (this_line_y == it.current_y);
13236 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13237 #ifdef GLYPH_DEBUG
13238 *w->desired_matrix->method = 0;
13239 debug_method_add (w, "optimization 3");
13240 #endif
13241 goto update;
13243 else
13244 goto cancel;
13247 cancel:
13248 /* Text changed drastically or point moved off of line. */
13249 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13252 CHARPOS (this_line_start_pos) = 0;
13253 consider_all_windows_p |= buffer_shared_and_changed ();
13254 ++clear_face_cache_count;
13255 #ifdef HAVE_WINDOW_SYSTEM
13256 ++clear_image_cache_count;
13257 #endif
13259 /* Build desired matrices, and update the display. If
13260 consider_all_windows_p is non-zero, do it for all windows on all
13261 frames. Otherwise do it for selected_window, only. */
13263 if (consider_all_windows_p)
13265 FOR_EACH_FRAME (tail, frame)
13266 XFRAME (frame)->updated_p = 0;
13268 FOR_EACH_FRAME (tail, frame)
13270 struct frame *f = XFRAME (frame);
13272 /* We don't have to do anything for unselected terminal
13273 frames. */
13274 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13275 && !EQ (FRAME_TTY (f)->top_frame, frame))
13276 continue;
13278 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13280 /* Mark all the scroll bars to be removed; we'll redeem
13281 the ones we want when we redisplay their windows. */
13282 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13283 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13285 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13286 redisplay_windows (FRAME_ROOT_WINDOW (f));
13288 /* The X error handler may have deleted that frame. */
13289 if (!FRAME_LIVE_P (f))
13290 continue;
13292 /* Any scroll bars which redisplay_windows should have
13293 nuked should now go away. */
13294 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13295 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13297 /* If fonts changed, display again. */
13298 /* ??? rms: I suspect it is a mistake to jump all the way
13299 back to retry here. It should just retry this frame. */
13300 if (fonts_changed_p)
13301 goto retry;
13303 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13305 /* See if we have to hscroll. */
13306 if (!f->already_hscrolled_p)
13308 f->already_hscrolled_p = 1;
13309 if (hscroll_windows (f->root_window))
13310 goto retry;
13313 /* Prevent various kinds of signals during display
13314 update. stdio is not robust about handling
13315 signals, which can cause an apparent I/O
13316 error. */
13317 if (interrupt_input)
13318 unrequest_sigio ();
13319 STOP_POLLING;
13321 /* Update the display. */
13322 set_window_update_flags (XWINDOW (f->root_window), 1);
13323 pending |= update_frame (f, 0, 0);
13324 f->updated_p = 1;
13329 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13331 if (!pending)
13333 /* Do the mark_window_display_accurate after all windows have
13334 been redisplayed because this call resets flags in buffers
13335 which are needed for proper redisplay. */
13336 FOR_EACH_FRAME (tail, frame)
13338 struct frame *f = XFRAME (frame);
13339 if (f->updated_p)
13341 mark_window_display_accurate (f->root_window, 1);
13342 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13343 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13348 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13350 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13351 struct frame *mini_frame;
13353 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13354 /* Use list_of_error, not Qerror, so that
13355 we catch only errors and don't run the debugger. */
13356 internal_condition_case_1 (redisplay_window_1, selected_window,
13357 list_of_error,
13358 redisplay_window_error);
13359 if (update_miniwindow_p)
13360 internal_condition_case_1 (redisplay_window_1, mini_window,
13361 list_of_error,
13362 redisplay_window_error);
13364 /* Compare desired and current matrices, perform output. */
13366 update:
13367 /* If fonts changed, display again. */
13368 if (fonts_changed_p)
13369 goto retry;
13371 /* Prevent various kinds of signals during display update.
13372 stdio is not robust about handling signals,
13373 which can cause an apparent I/O error. */
13374 if (interrupt_input)
13375 unrequest_sigio ();
13376 STOP_POLLING;
13378 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13380 if (hscroll_windows (selected_window))
13381 goto retry;
13383 XWINDOW (selected_window)->must_be_updated_p = 1;
13384 pending = update_frame (sf, 0, 0);
13387 /* We may have called echo_area_display at the top of this
13388 function. If the echo area is on another frame, that may
13389 have put text on a frame other than the selected one, so the
13390 above call to update_frame would not have caught it. Catch
13391 it here. */
13392 mini_window = FRAME_MINIBUF_WINDOW (sf);
13393 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13395 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13397 XWINDOW (mini_window)->must_be_updated_p = 1;
13398 pending |= update_frame (mini_frame, 0, 0);
13399 if (!pending && hscroll_windows (mini_window))
13400 goto retry;
13404 /* If display was paused because of pending input, make sure we do a
13405 thorough update the next time. */
13406 if (pending)
13408 /* Prevent the optimization at the beginning of
13409 redisplay_internal that tries a single-line update of the
13410 line containing the cursor in the selected window. */
13411 CHARPOS (this_line_start_pos) = 0;
13413 /* Let the overlay arrow be updated the next time. */
13414 update_overlay_arrows (0);
13416 /* If we pause after scrolling, some rows in the current
13417 matrices of some windows are not valid. */
13418 if (!WINDOW_FULL_WIDTH_P (w)
13419 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13420 update_mode_lines = 1;
13422 else
13424 if (!consider_all_windows_p)
13426 /* This has already been done above if
13427 consider_all_windows_p is set. */
13428 mark_window_display_accurate_1 (w, 1);
13430 /* Say overlay arrows are up to date. */
13431 update_overlay_arrows (1);
13433 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13434 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13437 update_mode_lines = 0;
13438 windows_or_buffers_changed = 0;
13439 cursor_type_changed = 0;
13442 /* Start SIGIO interrupts coming again. Having them off during the
13443 code above makes it less likely one will discard output, but not
13444 impossible, since there might be stuff in the system buffer here.
13445 But it is much hairier to try to do anything about that. */
13446 if (interrupt_input)
13447 request_sigio ();
13448 RESUME_POLLING;
13450 /* If a frame has become visible which was not before, redisplay
13451 again, so that we display it. Expose events for such a frame
13452 (which it gets when becoming visible) don't call the parts of
13453 redisplay constructing glyphs, so simply exposing a frame won't
13454 display anything in this case. So, we have to display these
13455 frames here explicitly. */
13456 if (!pending)
13458 int new_count = 0;
13460 FOR_EACH_FRAME (tail, frame)
13462 int this_is_visible = 0;
13464 if (XFRAME (frame)->visible)
13465 this_is_visible = 1;
13467 if (this_is_visible)
13468 new_count++;
13471 if (new_count != number_of_visible_frames)
13472 windows_or_buffers_changed++;
13475 /* Change frame size now if a change is pending. */
13476 do_pending_window_change (1);
13478 /* If we just did a pending size change, or have additional
13479 visible frames, or selected_window changed, redisplay again. */
13480 if ((windows_or_buffers_changed && !pending)
13481 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13482 goto retry;
13484 /* Clear the face and image caches.
13486 We used to do this only if consider_all_windows_p. But the cache
13487 needs to be cleared if a timer creates images in the current
13488 buffer (e.g. the test case in Bug#6230). */
13490 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13492 clear_face_cache (0);
13493 clear_face_cache_count = 0;
13496 #ifdef HAVE_WINDOW_SYSTEM
13497 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13499 clear_image_caches (Qnil);
13500 clear_image_cache_count = 0;
13502 #endif /* HAVE_WINDOW_SYSTEM */
13504 end_of_redisplay:
13505 backtrace_list = backtrace.next;
13506 unbind_to (count, Qnil);
13507 RESUME_POLLING;
13511 /* Redisplay, but leave alone any recent echo area message unless
13512 another message has been requested in its place.
13514 This is useful in situations where you need to redisplay but no
13515 user action has occurred, making it inappropriate for the message
13516 area to be cleared. See tracking_off and
13517 wait_reading_process_output for examples of these situations.
13519 FROM_WHERE is an integer saying from where this function was
13520 called. This is useful for debugging. */
13522 void
13523 redisplay_preserve_echo_area (int from_where)
13525 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13527 if (!NILP (echo_area_buffer[1]))
13529 /* We have a previously displayed message, but no current
13530 message. Redisplay the previous message. */
13531 display_last_displayed_message_p = 1;
13532 redisplay_internal ();
13533 display_last_displayed_message_p = 0;
13535 else
13536 redisplay_internal ();
13538 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13539 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13540 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13544 /* Function registered with record_unwind_protect in redisplay_internal.
13545 Clear redisplaying_p. Also select the previously selected frame. */
13547 static Lisp_Object
13548 unwind_redisplay (Lisp_Object old_frame)
13550 redisplaying_p = 0;
13551 return Qnil;
13555 /* Mark the display of leaf window W as accurate or inaccurate.
13556 If ACCURATE_P is non-zero mark display of W as accurate. If
13557 ACCURATE_P is zero, arrange for W to be redisplayed the next
13558 time redisplay_internal is called. */
13560 static void
13561 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13563 struct buffer *b = XBUFFER (w->contents);
13565 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13566 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13567 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13569 if (accurate_p)
13571 b->clip_changed = 0;
13572 b->prevent_redisplay_optimizations_p = 0;
13574 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13575 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13576 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13577 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13579 w->current_matrix->buffer = b;
13580 w->current_matrix->begv = BUF_BEGV (b);
13581 w->current_matrix->zv = BUF_ZV (b);
13583 w->last_cursor = w->cursor;
13584 w->last_cursor_off_p = w->cursor_off_p;
13586 if (w == XWINDOW (selected_window))
13587 w->last_point = BUF_PT (b);
13588 else
13589 w->last_point = marker_position (w->pointm);
13591 w->window_end_valid = 1;
13592 w->update_mode_line = 0;
13597 /* Mark the display of windows in the window tree rooted at WINDOW as
13598 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13599 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13600 be redisplayed the next time redisplay_internal is called. */
13602 void
13603 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13605 struct window *w;
13607 for (; !NILP (window); window = w->next)
13609 w = XWINDOW (window);
13610 if (WINDOWP (w->contents))
13611 mark_window_display_accurate (w->contents, accurate_p);
13612 else
13613 mark_window_display_accurate_1 (w, accurate_p);
13616 if (accurate_p)
13617 update_overlay_arrows (1);
13618 else
13619 /* Force a thorough redisplay the next time by setting
13620 last_arrow_position and last_arrow_string to t, which is
13621 unequal to any useful value of Voverlay_arrow_... */
13622 update_overlay_arrows (-1);
13626 /* Return value in display table DP (Lisp_Char_Table *) for character
13627 C. Since a display table doesn't have any parent, we don't have to
13628 follow parent. Do not call this function directly but use the
13629 macro DISP_CHAR_VECTOR. */
13631 Lisp_Object
13632 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13634 Lisp_Object val;
13636 if (ASCII_CHAR_P (c))
13638 val = dp->ascii;
13639 if (SUB_CHAR_TABLE_P (val))
13640 val = XSUB_CHAR_TABLE (val)->contents[c];
13642 else
13644 Lisp_Object table;
13646 XSETCHAR_TABLE (table, dp);
13647 val = char_table_ref (table, c);
13649 if (NILP (val))
13650 val = dp->defalt;
13651 return val;
13656 /***********************************************************************
13657 Window Redisplay
13658 ***********************************************************************/
13660 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13662 static void
13663 redisplay_windows (Lisp_Object window)
13665 while (!NILP (window))
13667 struct window *w = XWINDOW (window);
13669 if (WINDOWP (w->contents))
13670 redisplay_windows (w->contents);
13671 else if (BUFFERP (w->contents))
13673 displayed_buffer = XBUFFER (w->contents);
13674 /* Use list_of_error, not Qerror, so that
13675 we catch only errors and don't run the debugger. */
13676 internal_condition_case_1 (redisplay_window_0, window,
13677 list_of_error,
13678 redisplay_window_error);
13681 window = w->next;
13685 static Lisp_Object
13686 redisplay_window_error (Lisp_Object ignore)
13688 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13689 return Qnil;
13692 static Lisp_Object
13693 redisplay_window_0 (Lisp_Object window)
13695 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13696 redisplay_window (window, 0);
13697 return Qnil;
13700 static Lisp_Object
13701 redisplay_window_1 (Lisp_Object window)
13703 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13704 redisplay_window (window, 1);
13705 return Qnil;
13709 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13710 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13711 which positions recorded in ROW differ from current buffer
13712 positions.
13714 Return 0 if cursor is not on this row, 1 otherwise. */
13716 static int
13717 set_cursor_from_row (struct window *w, struct glyph_row *row,
13718 struct glyph_matrix *matrix,
13719 ptrdiff_t delta, ptrdiff_t delta_bytes,
13720 int dy, int dvpos)
13722 struct glyph *glyph = row->glyphs[TEXT_AREA];
13723 struct glyph *end = glyph + row->used[TEXT_AREA];
13724 struct glyph *cursor = NULL;
13725 /* The last known character position in row. */
13726 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13727 int x = row->x;
13728 ptrdiff_t pt_old = PT - delta;
13729 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13730 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13731 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13732 /* A glyph beyond the edge of TEXT_AREA which we should never
13733 touch. */
13734 struct glyph *glyphs_end = end;
13735 /* Non-zero means we've found a match for cursor position, but that
13736 glyph has the avoid_cursor_p flag set. */
13737 int match_with_avoid_cursor = 0;
13738 /* Non-zero means we've seen at least one glyph that came from a
13739 display string. */
13740 int string_seen = 0;
13741 /* Largest and smallest buffer positions seen so far during scan of
13742 glyph row. */
13743 ptrdiff_t bpos_max = pos_before;
13744 ptrdiff_t bpos_min = pos_after;
13745 /* Last buffer position covered by an overlay string with an integer
13746 `cursor' property. */
13747 ptrdiff_t bpos_covered = 0;
13748 /* Non-zero means the display string on which to display the cursor
13749 comes from a text property, not from an overlay. */
13750 int string_from_text_prop = 0;
13752 /* Don't even try doing anything if called for a mode-line or
13753 header-line row, since the rest of the code isn't prepared to
13754 deal with such calamities. */
13755 eassert (!row->mode_line_p);
13756 if (row->mode_line_p)
13757 return 0;
13759 /* Skip over glyphs not having an object at the start and the end of
13760 the row. These are special glyphs like truncation marks on
13761 terminal frames. */
13762 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13764 if (!row->reversed_p)
13766 while (glyph < end
13767 && INTEGERP (glyph->object)
13768 && glyph->charpos < 0)
13770 x += glyph->pixel_width;
13771 ++glyph;
13773 while (end > glyph
13774 && INTEGERP ((end - 1)->object)
13775 /* CHARPOS is zero for blanks and stretch glyphs
13776 inserted by extend_face_to_end_of_line. */
13777 && (end - 1)->charpos <= 0)
13778 --end;
13779 glyph_before = glyph - 1;
13780 glyph_after = end;
13782 else
13784 struct glyph *g;
13786 /* If the glyph row is reversed, we need to process it from back
13787 to front, so swap the edge pointers. */
13788 glyphs_end = end = glyph - 1;
13789 glyph += row->used[TEXT_AREA] - 1;
13791 while (glyph > end + 1
13792 && INTEGERP (glyph->object)
13793 && glyph->charpos < 0)
13795 --glyph;
13796 x -= glyph->pixel_width;
13798 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13799 --glyph;
13800 /* By default, in reversed rows we put the cursor on the
13801 rightmost (first in the reading order) glyph. */
13802 for (g = end + 1; g < glyph; g++)
13803 x += g->pixel_width;
13804 while (end < glyph
13805 && INTEGERP ((end + 1)->object)
13806 && (end + 1)->charpos <= 0)
13807 ++end;
13808 glyph_before = glyph + 1;
13809 glyph_after = end;
13812 else if (row->reversed_p)
13814 /* In R2L rows that don't display text, put the cursor on the
13815 rightmost glyph. Case in point: an empty last line that is
13816 part of an R2L paragraph. */
13817 cursor = end - 1;
13818 /* Avoid placing the cursor on the last glyph of the row, where
13819 on terminal frames we hold the vertical border between
13820 adjacent windows. */
13821 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13822 && !WINDOW_RIGHTMOST_P (w)
13823 && cursor == row->glyphs[LAST_AREA] - 1)
13824 cursor--;
13825 x = -1; /* will be computed below, at label compute_x */
13828 /* Step 1: Try to find the glyph whose character position
13829 corresponds to point. If that's not possible, find 2 glyphs
13830 whose character positions are the closest to point, one before
13831 point, the other after it. */
13832 if (!row->reversed_p)
13833 while (/* not marched to end of glyph row */
13834 glyph < end
13835 /* glyph was not inserted by redisplay for internal purposes */
13836 && !INTEGERP (glyph->object))
13838 if (BUFFERP (glyph->object))
13840 ptrdiff_t dpos = glyph->charpos - pt_old;
13842 if (glyph->charpos > bpos_max)
13843 bpos_max = glyph->charpos;
13844 if (glyph->charpos < bpos_min)
13845 bpos_min = glyph->charpos;
13846 if (!glyph->avoid_cursor_p)
13848 /* If we hit point, we've found the glyph on which to
13849 display the cursor. */
13850 if (dpos == 0)
13852 match_with_avoid_cursor = 0;
13853 break;
13855 /* See if we've found a better approximation to
13856 POS_BEFORE or to POS_AFTER. */
13857 if (0 > dpos && dpos > pos_before - pt_old)
13859 pos_before = glyph->charpos;
13860 glyph_before = glyph;
13862 else if (0 < dpos && dpos < pos_after - pt_old)
13864 pos_after = glyph->charpos;
13865 glyph_after = glyph;
13868 else if (dpos == 0)
13869 match_with_avoid_cursor = 1;
13871 else if (STRINGP (glyph->object))
13873 Lisp_Object chprop;
13874 ptrdiff_t glyph_pos = glyph->charpos;
13876 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13877 glyph->object);
13878 if (!NILP (chprop))
13880 /* If the string came from a `display' text property,
13881 look up the buffer position of that property and
13882 use that position to update bpos_max, as if we
13883 actually saw such a position in one of the row's
13884 glyphs. This helps with supporting integer values
13885 of `cursor' property on the display string in
13886 situations where most or all of the row's buffer
13887 text is completely covered by display properties,
13888 so that no glyph with valid buffer positions is
13889 ever seen in the row. */
13890 ptrdiff_t prop_pos =
13891 string_buffer_position_lim (glyph->object, pos_before,
13892 pos_after, 0);
13894 if (prop_pos >= pos_before)
13895 bpos_max = prop_pos - 1;
13897 if (INTEGERP (chprop))
13899 bpos_covered = bpos_max + XINT (chprop);
13900 /* If the `cursor' property covers buffer positions up
13901 to and including point, we should display cursor on
13902 this glyph. Note that, if a `cursor' property on one
13903 of the string's characters has an integer value, we
13904 will break out of the loop below _before_ we get to
13905 the position match above. IOW, integer values of
13906 the `cursor' property override the "exact match for
13907 point" strategy of positioning the cursor. */
13908 /* Implementation note: bpos_max == pt_old when, e.g.,
13909 we are in an empty line, where bpos_max is set to
13910 MATRIX_ROW_START_CHARPOS, see above. */
13911 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13913 cursor = glyph;
13914 break;
13918 string_seen = 1;
13920 x += glyph->pixel_width;
13921 ++glyph;
13923 else if (glyph > end) /* row is reversed */
13924 while (!INTEGERP (glyph->object))
13926 if (BUFFERP (glyph->object))
13928 ptrdiff_t dpos = glyph->charpos - pt_old;
13930 if (glyph->charpos > bpos_max)
13931 bpos_max = glyph->charpos;
13932 if (glyph->charpos < bpos_min)
13933 bpos_min = glyph->charpos;
13934 if (!glyph->avoid_cursor_p)
13936 if (dpos == 0)
13938 match_with_avoid_cursor = 0;
13939 break;
13941 if (0 > dpos && dpos > pos_before - pt_old)
13943 pos_before = glyph->charpos;
13944 glyph_before = glyph;
13946 else if (0 < dpos && dpos < pos_after - pt_old)
13948 pos_after = glyph->charpos;
13949 glyph_after = glyph;
13952 else if (dpos == 0)
13953 match_with_avoid_cursor = 1;
13955 else if (STRINGP (glyph->object))
13957 Lisp_Object chprop;
13958 ptrdiff_t glyph_pos = glyph->charpos;
13960 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13961 glyph->object);
13962 if (!NILP (chprop))
13964 ptrdiff_t prop_pos =
13965 string_buffer_position_lim (glyph->object, pos_before,
13966 pos_after, 0);
13968 if (prop_pos >= pos_before)
13969 bpos_max = prop_pos - 1;
13971 if (INTEGERP (chprop))
13973 bpos_covered = bpos_max + XINT (chprop);
13974 /* If the `cursor' property covers buffer positions up
13975 to and including point, we should display cursor on
13976 this glyph. */
13977 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13979 cursor = glyph;
13980 break;
13983 string_seen = 1;
13985 --glyph;
13986 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13988 x--; /* can't use any pixel_width */
13989 break;
13991 x -= glyph->pixel_width;
13994 /* Step 2: If we didn't find an exact match for point, we need to
13995 look for a proper place to put the cursor among glyphs between
13996 GLYPH_BEFORE and GLYPH_AFTER. */
13997 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13998 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13999 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14001 /* An empty line has a single glyph whose OBJECT is zero and
14002 whose CHARPOS is the position of a newline on that line.
14003 Note that on a TTY, there are more glyphs after that, which
14004 were produced by extend_face_to_end_of_line, but their
14005 CHARPOS is zero or negative. */
14006 int empty_line_p =
14007 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14008 && INTEGERP (glyph->object) && glyph->charpos > 0
14009 /* On a TTY, continued and truncated rows also have a glyph at
14010 their end whose OBJECT is zero and whose CHARPOS is
14011 positive (the continuation and truncation glyphs), but such
14012 rows are obviously not "empty". */
14013 && !(row->continued_p || row->truncated_on_right_p);
14015 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14017 ptrdiff_t ellipsis_pos;
14019 /* Scan back over the ellipsis glyphs. */
14020 if (!row->reversed_p)
14022 ellipsis_pos = (glyph - 1)->charpos;
14023 while (glyph > row->glyphs[TEXT_AREA]
14024 && (glyph - 1)->charpos == ellipsis_pos)
14025 glyph--, x -= glyph->pixel_width;
14026 /* That loop always goes one position too far, including
14027 the glyph before the ellipsis. So scan forward over
14028 that one. */
14029 x += glyph->pixel_width;
14030 glyph++;
14032 else /* row is reversed */
14034 ellipsis_pos = (glyph + 1)->charpos;
14035 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14036 && (glyph + 1)->charpos == ellipsis_pos)
14037 glyph++, x += glyph->pixel_width;
14038 x -= glyph->pixel_width;
14039 glyph--;
14042 else if (match_with_avoid_cursor)
14044 cursor = glyph_after;
14045 x = -1;
14047 else if (string_seen)
14049 int incr = row->reversed_p ? -1 : +1;
14051 /* Need to find the glyph that came out of a string which is
14052 present at point. That glyph is somewhere between
14053 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14054 positioned between POS_BEFORE and POS_AFTER in the
14055 buffer. */
14056 struct glyph *start, *stop;
14057 ptrdiff_t pos = pos_before;
14059 x = -1;
14061 /* If the row ends in a newline from a display string,
14062 reordering could have moved the glyphs belonging to the
14063 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14064 in this case we extend the search to the last glyph in
14065 the row that was not inserted by redisplay. */
14066 if (row->ends_in_newline_from_string_p)
14068 glyph_after = end;
14069 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14072 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14073 correspond to POS_BEFORE and POS_AFTER, respectively. We
14074 need START and STOP in the order that corresponds to the
14075 row's direction as given by its reversed_p flag. If the
14076 directionality of characters between POS_BEFORE and
14077 POS_AFTER is the opposite of the row's base direction,
14078 these characters will have been reordered for display,
14079 and we need to reverse START and STOP. */
14080 if (!row->reversed_p)
14082 start = min (glyph_before, glyph_after);
14083 stop = max (glyph_before, glyph_after);
14085 else
14087 start = max (glyph_before, glyph_after);
14088 stop = min (glyph_before, glyph_after);
14090 for (glyph = start + incr;
14091 row->reversed_p ? glyph > stop : glyph < stop; )
14094 /* Any glyphs that come from the buffer are here because
14095 of bidi reordering. Skip them, and only pay
14096 attention to glyphs that came from some string. */
14097 if (STRINGP (glyph->object))
14099 Lisp_Object str;
14100 ptrdiff_t tem;
14101 /* If the display property covers the newline, we
14102 need to search for it one position farther. */
14103 ptrdiff_t lim = pos_after
14104 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14106 string_from_text_prop = 0;
14107 str = glyph->object;
14108 tem = string_buffer_position_lim (str, pos, lim, 0);
14109 if (tem == 0 /* from overlay */
14110 || pos <= tem)
14112 /* If the string from which this glyph came is
14113 found in the buffer at point, or at position
14114 that is closer to point than pos_after, then
14115 we've found the glyph we've been looking for.
14116 If it comes from an overlay (tem == 0), and
14117 it has the `cursor' property on one of its
14118 glyphs, record that glyph as a candidate for
14119 displaying the cursor. (As in the
14120 unidirectional version, we will display the
14121 cursor on the last candidate we find.) */
14122 if (tem == 0
14123 || tem == pt_old
14124 || (tem - pt_old > 0 && tem < pos_after))
14126 /* The glyphs from this string could have
14127 been reordered. Find the one with the
14128 smallest string position. Or there could
14129 be a character in the string with the
14130 `cursor' property, which means display
14131 cursor on that character's glyph. */
14132 ptrdiff_t strpos = glyph->charpos;
14134 if (tem)
14136 cursor = glyph;
14137 string_from_text_prop = 1;
14139 for ( ;
14140 (row->reversed_p ? glyph > stop : glyph < stop)
14141 && EQ (glyph->object, str);
14142 glyph += incr)
14144 Lisp_Object cprop;
14145 ptrdiff_t gpos = glyph->charpos;
14147 cprop = Fget_char_property (make_number (gpos),
14148 Qcursor,
14149 glyph->object);
14150 if (!NILP (cprop))
14152 cursor = glyph;
14153 break;
14155 if (tem && glyph->charpos < strpos)
14157 strpos = glyph->charpos;
14158 cursor = glyph;
14162 if (tem == pt_old
14163 || (tem - pt_old > 0 && tem < pos_after))
14164 goto compute_x;
14166 if (tem)
14167 pos = tem + 1; /* don't find previous instances */
14169 /* This string is not what we want; skip all of the
14170 glyphs that came from it. */
14171 while ((row->reversed_p ? glyph > stop : glyph < stop)
14172 && EQ (glyph->object, str))
14173 glyph += incr;
14175 else
14176 glyph += incr;
14179 /* If we reached the end of the line, and END was from a string,
14180 the cursor is not on this line. */
14181 if (cursor == NULL
14182 && (row->reversed_p ? glyph <= end : glyph >= end)
14183 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14184 && STRINGP (end->object)
14185 && row->continued_p)
14186 return 0;
14188 /* A truncated row may not include PT among its character positions.
14189 Setting the cursor inside the scroll margin will trigger
14190 recalculation of hscroll in hscroll_window_tree. But if a
14191 display string covers point, defer to the string-handling
14192 code below to figure this out. */
14193 else if (row->truncated_on_left_p && pt_old < bpos_min)
14195 cursor = glyph_before;
14196 x = -1;
14198 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14199 /* Zero-width characters produce no glyphs. */
14200 || (!empty_line_p
14201 && (row->reversed_p
14202 ? glyph_after > glyphs_end
14203 : glyph_after < glyphs_end)))
14205 cursor = glyph_after;
14206 x = -1;
14210 compute_x:
14211 if (cursor != NULL)
14212 glyph = cursor;
14213 else if (glyph == glyphs_end
14214 && pos_before == pos_after
14215 && STRINGP ((row->reversed_p
14216 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14217 : row->glyphs[TEXT_AREA])->object))
14219 /* If all the glyphs of this row came from strings, put the
14220 cursor on the first glyph of the row. This avoids having the
14221 cursor outside of the text area in this very rare and hard
14222 use case. */
14223 glyph =
14224 row->reversed_p
14225 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14226 : row->glyphs[TEXT_AREA];
14228 if (x < 0)
14230 struct glyph *g;
14232 /* Need to compute x that corresponds to GLYPH. */
14233 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14235 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14236 emacs_abort ();
14237 x += g->pixel_width;
14241 /* ROW could be part of a continued line, which, under bidi
14242 reordering, might have other rows whose start and end charpos
14243 occlude point. Only set w->cursor if we found a better
14244 approximation to the cursor position than we have from previously
14245 examined candidate rows belonging to the same continued line. */
14246 if (/* we already have a candidate row */
14247 w->cursor.vpos >= 0
14248 /* that candidate is not the row we are processing */
14249 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14250 /* Make sure cursor.vpos specifies a row whose start and end
14251 charpos occlude point, and it is valid candidate for being a
14252 cursor-row. This is because some callers of this function
14253 leave cursor.vpos at the row where the cursor was displayed
14254 during the last redisplay cycle. */
14255 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14256 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14257 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14259 struct glyph *g1 =
14260 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14262 /* Don't consider glyphs that are outside TEXT_AREA. */
14263 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14264 return 0;
14265 /* Keep the candidate whose buffer position is the closest to
14266 point or has the `cursor' property. */
14267 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14268 w->cursor.hpos >= 0
14269 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14270 && ((BUFFERP (g1->object)
14271 && (g1->charpos == pt_old /* an exact match always wins */
14272 || (BUFFERP (glyph->object)
14273 && eabs (g1->charpos - pt_old)
14274 < eabs (glyph->charpos - pt_old))))
14275 /* previous candidate is a glyph from a string that has
14276 a non-nil `cursor' property */
14277 || (STRINGP (g1->object)
14278 && (!NILP (Fget_char_property (make_number (g1->charpos),
14279 Qcursor, g1->object))
14280 /* previous candidate is from the same display
14281 string as this one, and the display string
14282 came from a text property */
14283 || (EQ (g1->object, glyph->object)
14284 && string_from_text_prop)
14285 /* this candidate is from newline and its
14286 position is not an exact match */
14287 || (INTEGERP (glyph->object)
14288 && glyph->charpos != pt_old)))))
14289 return 0;
14290 /* If this candidate gives an exact match, use that. */
14291 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14292 /* If this candidate is a glyph created for the
14293 terminating newline of a line, and point is on that
14294 newline, it wins because it's an exact match. */
14295 || (!row->continued_p
14296 && INTEGERP (glyph->object)
14297 && glyph->charpos == 0
14298 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14299 /* Otherwise, keep the candidate that comes from a row
14300 spanning less buffer positions. This may win when one or
14301 both candidate positions are on glyphs that came from
14302 display strings, for which we cannot compare buffer
14303 positions. */
14304 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14305 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14306 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14307 return 0;
14309 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14310 w->cursor.x = x;
14311 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14312 w->cursor.y = row->y + dy;
14314 if (w == XWINDOW (selected_window))
14316 if (!row->continued_p
14317 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14318 && row->x == 0)
14320 this_line_buffer = XBUFFER (w->contents);
14322 CHARPOS (this_line_start_pos)
14323 = MATRIX_ROW_START_CHARPOS (row) + delta;
14324 BYTEPOS (this_line_start_pos)
14325 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14327 CHARPOS (this_line_end_pos)
14328 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14329 BYTEPOS (this_line_end_pos)
14330 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14332 this_line_y = w->cursor.y;
14333 this_line_pixel_height = row->height;
14334 this_line_vpos = w->cursor.vpos;
14335 this_line_start_x = row->x;
14337 else
14338 CHARPOS (this_line_start_pos) = 0;
14341 return 1;
14345 /* Run window scroll functions, if any, for WINDOW with new window
14346 start STARTP. Sets the window start of WINDOW to that position.
14348 We assume that the window's buffer is really current. */
14350 static struct text_pos
14351 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14353 struct window *w = XWINDOW (window);
14354 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14356 if (current_buffer != XBUFFER (w->contents))
14357 emacs_abort ();
14359 if (!NILP (Vwindow_scroll_functions))
14361 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14362 make_number (CHARPOS (startp)));
14363 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14364 /* In case the hook functions switch buffers. */
14365 set_buffer_internal (XBUFFER (w->contents));
14368 return startp;
14372 /* Make sure the line containing the cursor is fully visible.
14373 A value of 1 means there is nothing to be done.
14374 (Either the line is fully visible, or it cannot be made so,
14375 or we cannot tell.)
14377 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14378 is higher than window.
14380 A value of 0 means the caller should do scrolling
14381 as if point had gone off the screen. */
14383 static int
14384 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14386 struct glyph_matrix *matrix;
14387 struct glyph_row *row;
14388 int window_height;
14390 if (!make_cursor_line_fully_visible_p)
14391 return 1;
14393 /* It's not always possible to find the cursor, e.g, when a window
14394 is full of overlay strings. Don't do anything in that case. */
14395 if (w->cursor.vpos < 0)
14396 return 1;
14398 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14399 row = MATRIX_ROW (matrix, w->cursor.vpos);
14401 /* If the cursor row is not partially visible, there's nothing to do. */
14402 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14403 return 1;
14405 /* If the row the cursor is in is taller than the window's height,
14406 it's not clear what to do, so do nothing. */
14407 window_height = window_box_height (w);
14408 if (row->height >= window_height)
14410 if (!force_p || MINI_WINDOW_P (w)
14411 || w->vscroll || w->cursor.vpos == 0)
14412 return 1;
14414 return 0;
14418 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14419 non-zero means only WINDOW is redisplayed in redisplay_internal.
14420 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14421 in redisplay_window to bring a partially visible line into view in
14422 the case that only the cursor has moved.
14424 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14425 last screen line's vertical height extends past the end of the screen.
14427 Value is
14429 1 if scrolling succeeded
14431 0 if scrolling didn't find point.
14433 -1 if new fonts have been loaded so that we must interrupt
14434 redisplay, adjust glyph matrices, and try again. */
14436 enum
14438 SCROLLING_SUCCESS,
14439 SCROLLING_FAILED,
14440 SCROLLING_NEED_LARGER_MATRICES
14443 /* If scroll-conservatively is more than this, never recenter.
14445 If you change this, don't forget to update the doc string of
14446 `scroll-conservatively' and the Emacs manual. */
14447 #define SCROLL_LIMIT 100
14449 static int
14450 try_scrolling (Lisp_Object window, int just_this_one_p,
14451 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14452 int temp_scroll_step, int last_line_misfit)
14454 struct window *w = XWINDOW (window);
14455 struct frame *f = XFRAME (w->frame);
14456 struct text_pos pos, startp;
14457 struct it it;
14458 int this_scroll_margin, scroll_max, rc, height;
14459 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14460 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14461 Lisp_Object aggressive;
14462 /* We will never try scrolling more than this number of lines. */
14463 int scroll_limit = SCROLL_LIMIT;
14465 #ifdef GLYPH_DEBUG
14466 debug_method_add (w, "try_scrolling");
14467 #endif
14469 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14471 /* Compute scroll margin height in pixels. We scroll when point is
14472 within this distance from the top or bottom of the window. */
14473 if (scroll_margin > 0)
14474 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14475 * FRAME_LINE_HEIGHT (f);
14476 else
14477 this_scroll_margin = 0;
14479 /* Force arg_scroll_conservatively to have a reasonable value, to
14480 avoid scrolling too far away with slow move_it_* functions. Note
14481 that the user can supply scroll-conservatively equal to
14482 `most-positive-fixnum', which can be larger than INT_MAX. */
14483 if (arg_scroll_conservatively > scroll_limit)
14485 arg_scroll_conservatively = scroll_limit + 1;
14486 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14488 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14489 /* Compute how much we should try to scroll maximally to bring
14490 point into view. */
14491 scroll_max = (max (scroll_step,
14492 max (arg_scroll_conservatively, temp_scroll_step))
14493 * FRAME_LINE_HEIGHT (f));
14494 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14495 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14496 /* We're trying to scroll because of aggressive scrolling but no
14497 scroll_step is set. Choose an arbitrary one. */
14498 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14499 else
14500 scroll_max = 0;
14502 too_near_end:
14504 /* Decide whether to scroll down. */
14505 if (PT > CHARPOS (startp))
14507 int scroll_margin_y;
14509 /* Compute the pixel ypos of the scroll margin, then move IT to
14510 either that ypos or PT, whichever comes first. */
14511 start_display (&it, w, startp);
14512 scroll_margin_y = it.last_visible_y - this_scroll_margin
14513 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14514 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14515 (MOVE_TO_POS | MOVE_TO_Y));
14517 if (PT > CHARPOS (it.current.pos))
14519 int y0 = line_bottom_y (&it);
14520 /* Compute how many pixels below window bottom to stop searching
14521 for PT. This avoids costly search for PT that is far away if
14522 the user limited scrolling by a small number of lines, but
14523 always finds PT if scroll_conservatively is set to a large
14524 number, such as most-positive-fixnum. */
14525 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14526 int y_to_move = it.last_visible_y + slack;
14528 /* Compute the distance from the scroll margin to PT or to
14529 the scroll limit, whichever comes first. This should
14530 include the height of the cursor line, to make that line
14531 fully visible. */
14532 move_it_to (&it, PT, -1, y_to_move,
14533 -1, MOVE_TO_POS | MOVE_TO_Y);
14534 dy = line_bottom_y (&it) - y0;
14536 if (dy > scroll_max)
14537 return SCROLLING_FAILED;
14539 if (dy > 0)
14540 scroll_down_p = 1;
14544 if (scroll_down_p)
14546 /* Point is in or below the bottom scroll margin, so move the
14547 window start down. If scrolling conservatively, move it just
14548 enough down to make point visible. If scroll_step is set,
14549 move it down by scroll_step. */
14550 if (arg_scroll_conservatively)
14551 amount_to_scroll
14552 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14553 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14554 else if (scroll_step || temp_scroll_step)
14555 amount_to_scroll = scroll_max;
14556 else
14558 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14559 height = WINDOW_BOX_TEXT_HEIGHT (w);
14560 if (NUMBERP (aggressive))
14562 double float_amount = XFLOATINT (aggressive) * height;
14563 int aggressive_scroll = float_amount;
14564 if (aggressive_scroll == 0 && float_amount > 0)
14565 aggressive_scroll = 1;
14566 /* Don't let point enter the scroll margin near top of
14567 the window. This could happen if the value of
14568 scroll_up_aggressively is too large and there are
14569 non-zero margins, because scroll_up_aggressively
14570 means put point that fraction of window height
14571 _from_the_bottom_margin_. */
14572 if (aggressive_scroll + 2*this_scroll_margin > height)
14573 aggressive_scroll = height - 2*this_scroll_margin;
14574 amount_to_scroll = dy + aggressive_scroll;
14578 if (amount_to_scroll <= 0)
14579 return SCROLLING_FAILED;
14581 start_display (&it, w, startp);
14582 if (arg_scroll_conservatively <= scroll_limit)
14583 move_it_vertically (&it, amount_to_scroll);
14584 else
14586 /* Extra precision for users who set scroll-conservatively
14587 to a large number: make sure the amount we scroll
14588 the window start is never less than amount_to_scroll,
14589 which was computed as distance from window bottom to
14590 point. This matters when lines at window top and lines
14591 below window bottom have different height. */
14592 struct it it1;
14593 void *it1data = NULL;
14594 /* We use a temporary it1 because line_bottom_y can modify
14595 its argument, if it moves one line down; see there. */
14596 int start_y;
14598 SAVE_IT (it1, it, it1data);
14599 start_y = line_bottom_y (&it1);
14600 do {
14601 RESTORE_IT (&it, &it, it1data);
14602 move_it_by_lines (&it, 1);
14603 SAVE_IT (it1, it, it1data);
14604 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14607 /* If STARTP is unchanged, move it down another screen line. */
14608 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14609 move_it_by_lines (&it, 1);
14610 startp = it.current.pos;
14612 else
14614 struct text_pos scroll_margin_pos = startp;
14615 int y_offset = 0;
14617 /* See if point is inside the scroll margin at the top of the
14618 window. */
14619 if (this_scroll_margin)
14621 int y_start;
14623 start_display (&it, w, startp);
14624 y_start = it.current_y;
14625 move_it_vertically (&it, this_scroll_margin);
14626 scroll_margin_pos = it.current.pos;
14627 /* If we didn't move enough before hitting ZV, request
14628 additional amount of scroll, to move point out of the
14629 scroll margin. */
14630 if (IT_CHARPOS (it) == ZV
14631 && it.current_y - y_start < this_scroll_margin)
14632 y_offset = this_scroll_margin - (it.current_y - y_start);
14635 if (PT < CHARPOS (scroll_margin_pos))
14637 /* Point is in the scroll margin at the top of the window or
14638 above what is displayed in the window. */
14639 int y0, y_to_move;
14641 /* Compute the vertical distance from PT to the scroll
14642 margin position. Move as far as scroll_max allows, or
14643 one screenful, or 10 screen lines, whichever is largest.
14644 Give up if distance is greater than scroll_max or if we
14645 didn't reach the scroll margin position. */
14646 SET_TEXT_POS (pos, PT, PT_BYTE);
14647 start_display (&it, w, pos);
14648 y0 = it.current_y;
14649 y_to_move = max (it.last_visible_y,
14650 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14651 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14652 y_to_move, -1,
14653 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14654 dy = it.current_y - y0;
14655 if (dy > scroll_max
14656 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14657 return SCROLLING_FAILED;
14659 /* Additional scroll for when ZV was too close to point. */
14660 dy += y_offset;
14662 /* Compute new window start. */
14663 start_display (&it, w, startp);
14665 if (arg_scroll_conservatively)
14666 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14667 max (scroll_step, temp_scroll_step));
14668 else if (scroll_step || temp_scroll_step)
14669 amount_to_scroll = scroll_max;
14670 else
14672 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14673 height = WINDOW_BOX_TEXT_HEIGHT (w);
14674 if (NUMBERP (aggressive))
14676 double float_amount = XFLOATINT (aggressive) * height;
14677 int aggressive_scroll = float_amount;
14678 if (aggressive_scroll == 0 && float_amount > 0)
14679 aggressive_scroll = 1;
14680 /* Don't let point enter the scroll margin near
14681 bottom of the window, if the value of
14682 scroll_down_aggressively happens to be too
14683 large. */
14684 if (aggressive_scroll + 2*this_scroll_margin > height)
14685 aggressive_scroll = height - 2*this_scroll_margin;
14686 amount_to_scroll = dy + aggressive_scroll;
14690 if (amount_to_scroll <= 0)
14691 return SCROLLING_FAILED;
14693 move_it_vertically_backward (&it, amount_to_scroll);
14694 startp = it.current.pos;
14698 /* Run window scroll functions. */
14699 startp = run_window_scroll_functions (window, startp);
14701 /* Display the window. Give up if new fonts are loaded, or if point
14702 doesn't appear. */
14703 if (!try_window (window, startp, 0))
14704 rc = SCROLLING_NEED_LARGER_MATRICES;
14705 else if (w->cursor.vpos < 0)
14707 clear_glyph_matrix (w->desired_matrix);
14708 rc = SCROLLING_FAILED;
14710 else
14712 /* Maybe forget recorded base line for line number display. */
14713 if (!just_this_one_p
14714 || current_buffer->clip_changed
14715 || BEG_UNCHANGED < CHARPOS (startp))
14716 w->base_line_number = 0;
14718 /* If cursor ends up on a partially visible line,
14719 treat that as being off the bottom of the screen. */
14720 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14721 /* It's possible that the cursor is on the first line of the
14722 buffer, which is partially obscured due to a vscroll
14723 (Bug#7537). In that case, avoid looping forever . */
14724 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14726 clear_glyph_matrix (w->desired_matrix);
14727 ++extra_scroll_margin_lines;
14728 goto too_near_end;
14730 rc = SCROLLING_SUCCESS;
14733 return rc;
14737 /* Compute a suitable window start for window W if display of W starts
14738 on a continuation line. Value is non-zero if a new window start
14739 was computed.
14741 The new window start will be computed, based on W's width, starting
14742 from the start of the continued line. It is the start of the
14743 screen line with the minimum distance from the old start W->start. */
14745 static int
14746 compute_window_start_on_continuation_line (struct window *w)
14748 struct text_pos pos, start_pos;
14749 int window_start_changed_p = 0;
14751 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14753 /* If window start is on a continuation line... Window start may be
14754 < BEGV in case there's invisible text at the start of the
14755 buffer (M-x rmail, for example). */
14756 if (CHARPOS (start_pos) > BEGV
14757 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14759 struct it it;
14760 struct glyph_row *row;
14762 /* Handle the case that the window start is out of range. */
14763 if (CHARPOS (start_pos) < BEGV)
14764 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14765 else if (CHARPOS (start_pos) > ZV)
14766 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14768 /* Find the start of the continued line. This should be fast
14769 because find_newline is fast (newline cache). */
14770 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14771 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14772 row, DEFAULT_FACE_ID);
14773 reseat_at_previous_visible_line_start (&it);
14775 /* If the line start is "too far" away from the window start,
14776 say it takes too much time to compute a new window start. */
14777 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14778 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14780 int min_distance, distance;
14782 /* Move forward by display lines to find the new window
14783 start. If window width was enlarged, the new start can
14784 be expected to be > the old start. If window width was
14785 decreased, the new window start will be < the old start.
14786 So, we're looking for the display line start with the
14787 minimum distance from the old window start. */
14788 pos = it.current.pos;
14789 min_distance = INFINITY;
14790 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14791 distance < min_distance)
14793 min_distance = distance;
14794 pos = it.current.pos;
14795 move_it_by_lines (&it, 1);
14798 /* Set the window start there. */
14799 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14800 window_start_changed_p = 1;
14804 return window_start_changed_p;
14808 /* Try cursor movement in case text has not changed in window WINDOW,
14809 with window start STARTP. Value is
14811 CURSOR_MOVEMENT_SUCCESS if successful
14813 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14815 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14816 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14817 we want to scroll as if scroll-step were set to 1. See the code.
14819 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14820 which case we have to abort this redisplay, and adjust matrices
14821 first. */
14823 enum
14825 CURSOR_MOVEMENT_SUCCESS,
14826 CURSOR_MOVEMENT_CANNOT_BE_USED,
14827 CURSOR_MOVEMENT_MUST_SCROLL,
14828 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14831 static int
14832 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14834 struct window *w = XWINDOW (window);
14835 struct frame *f = XFRAME (w->frame);
14836 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14838 #ifdef GLYPH_DEBUG
14839 if (inhibit_try_cursor_movement)
14840 return rc;
14841 #endif
14843 /* Previously, there was a check for Lisp integer in the
14844 if-statement below. Now, this field is converted to
14845 ptrdiff_t, thus zero means invalid position in a buffer. */
14846 eassert (w->last_point > 0);
14848 /* Handle case where text has not changed, only point, and it has
14849 not moved off the frame. */
14850 if (/* Point may be in this window. */
14851 PT >= CHARPOS (startp)
14852 /* Selective display hasn't changed. */
14853 && !current_buffer->clip_changed
14854 /* Function force-mode-line-update is used to force a thorough
14855 redisplay. It sets either windows_or_buffers_changed or
14856 update_mode_lines. So don't take a shortcut here for these
14857 cases. */
14858 && !update_mode_lines
14859 && !windows_or_buffers_changed
14860 && !cursor_type_changed
14861 /* Can't use this case if highlighting a region. When a
14862 region exists, cursor movement has to do more than just
14863 set the cursor. */
14864 && markpos_of_region () < 0
14865 && !w->region_showing
14866 && NILP (Vshow_trailing_whitespace)
14867 /* This code is not used for mini-buffer for the sake of the case
14868 of redisplaying to replace an echo area message; since in
14869 that case the mini-buffer contents per se are usually
14870 unchanged. This code is of no real use in the mini-buffer
14871 since the handling of this_line_start_pos, etc., in redisplay
14872 handles the same cases. */
14873 && !EQ (window, minibuf_window)
14874 /* When splitting windows or for new windows, it happens that
14875 redisplay is called with a nil window_end_vpos or one being
14876 larger than the window. This should really be fixed in
14877 window.c. I don't have this on my list, now, so we do
14878 approximately the same as the old redisplay code. --gerd. */
14879 && INTEGERP (w->window_end_vpos)
14880 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14881 && (FRAME_WINDOW_P (f)
14882 || !overlay_arrow_in_current_buffer_p ()))
14884 int this_scroll_margin, top_scroll_margin;
14885 struct glyph_row *row = NULL;
14887 #ifdef GLYPH_DEBUG
14888 debug_method_add (w, "cursor movement");
14889 #endif
14891 /* Scroll if point within this distance from the top or bottom
14892 of the window. This is a pixel value. */
14893 if (scroll_margin > 0)
14895 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14896 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14898 else
14899 this_scroll_margin = 0;
14901 top_scroll_margin = this_scroll_margin;
14902 if (WINDOW_WANTS_HEADER_LINE_P (w))
14903 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14905 /* Start with the row the cursor was displayed during the last
14906 not paused redisplay. Give up if that row is not valid. */
14907 if (w->last_cursor.vpos < 0
14908 || w->last_cursor.vpos >= w->current_matrix->nrows)
14909 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14910 else
14912 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14913 if (row->mode_line_p)
14914 ++row;
14915 if (!row->enabled_p)
14916 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14919 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14921 int scroll_p = 0, must_scroll = 0;
14922 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14924 if (PT > w->last_point)
14926 /* Point has moved forward. */
14927 while (MATRIX_ROW_END_CHARPOS (row) < PT
14928 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14930 eassert (row->enabled_p);
14931 ++row;
14934 /* If the end position of a row equals the start
14935 position of the next row, and PT is at that position,
14936 we would rather display cursor in the next line. */
14937 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14938 && MATRIX_ROW_END_CHARPOS (row) == PT
14939 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
14940 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14941 && !cursor_row_p (row))
14942 ++row;
14944 /* If within the scroll margin, scroll. Note that
14945 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14946 the next line would be drawn, and that
14947 this_scroll_margin can be zero. */
14948 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14949 || PT > MATRIX_ROW_END_CHARPOS (row)
14950 /* Line is completely visible last line in window
14951 and PT is to be set in the next line. */
14952 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14953 && PT == MATRIX_ROW_END_CHARPOS (row)
14954 && !row->ends_at_zv_p
14955 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14956 scroll_p = 1;
14958 else if (PT < w->last_point)
14960 /* Cursor has to be moved backward. Note that PT >=
14961 CHARPOS (startp) because of the outer if-statement. */
14962 while (!row->mode_line_p
14963 && (MATRIX_ROW_START_CHARPOS (row) > PT
14964 || (MATRIX_ROW_START_CHARPOS (row) == PT
14965 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14966 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14967 row > w->current_matrix->rows
14968 && (row-1)->ends_in_newline_from_string_p))))
14969 && (row->y > top_scroll_margin
14970 || CHARPOS (startp) == BEGV))
14972 eassert (row->enabled_p);
14973 --row;
14976 /* Consider the following case: Window starts at BEGV,
14977 there is invisible, intangible text at BEGV, so that
14978 display starts at some point START > BEGV. It can
14979 happen that we are called with PT somewhere between
14980 BEGV and START. Try to handle that case. */
14981 if (row < w->current_matrix->rows
14982 || row->mode_line_p)
14984 row = w->current_matrix->rows;
14985 if (row->mode_line_p)
14986 ++row;
14989 /* Due to newlines in overlay strings, we may have to
14990 skip forward over overlay strings. */
14991 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14992 && MATRIX_ROW_END_CHARPOS (row) == PT
14993 && !cursor_row_p (row))
14994 ++row;
14996 /* If within the scroll margin, scroll. */
14997 if (row->y < top_scroll_margin
14998 && CHARPOS (startp) != BEGV)
14999 scroll_p = 1;
15001 else
15003 /* Cursor did not move. So don't scroll even if cursor line
15004 is partially visible, as it was so before. */
15005 rc = CURSOR_MOVEMENT_SUCCESS;
15008 if (PT < MATRIX_ROW_START_CHARPOS (row)
15009 || PT > MATRIX_ROW_END_CHARPOS (row))
15011 /* if PT is not in the glyph row, give up. */
15012 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15013 must_scroll = 1;
15015 else if (rc != CURSOR_MOVEMENT_SUCCESS
15016 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15018 struct glyph_row *row1;
15020 /* If rows are bidi-reordered and point moved, back up
15021 until we find a row that does not belong to a
15022 continuation line. This is because we must consider
15023 all rows of a continued line as candidates for the
15024 new cursor positioning, since row start and end
15025 positions change non-linearly with vertical position
15026 in such rows. */
15027 /* FIXME: Revisit this when glyph ``spilling'' in
15028 continuation lines' rows is implemented for
15029 bidi-reordered rows. */
15030 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15031 MATRIX_ROW_CONTINUATION_LINE_P (row);
15032 --row)
15034 /* If we hit the beginning of the displayed portion
15035 without finding the first row of a continued
15036 line, give up. */
15037 if (row <= row1)
15039 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15040 break;
15042 eassert (row->enabled_p);
15045 if (must_scroll)
15047 else if (rc != CURSOR_MOVEMENT_SUCCESS
15048 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15049 /* Make sure this isn't a header line by any chance, since
15050 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15051 && !row->mode_line_p
15052 && make_cursor_line_fully_visible_p)
15054 if (PT == MATRIX_ROW_END_CHARPOS (row)
15055 && !row->ends_at_zv_p
15056 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15057 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15058 else if (row->height > window_box_height (w))
15060 /* If we end up in a partially visible line, let's
15061 make it fully visible, except when it's taller
15062 than the window, in which case we can't do much
15063 about it. */
15064 *scroll_step = 1;
15065 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15067 else
15069 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15070 if (!cursor_row_fully_visible_p (w, 0, 1))
15071 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15072 else
15073 rc = CURSOR_MOVEMENT_SUCCESS;
15076 else if (scroll_p)
15077 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15078 else if (rc != CURSOR_MOVEMENT_SUCCESS
15079 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15081 /* With bidi-reordered rows, there could be more than
15082 one candidate row whose start and end positions
15083 occlude point. We need to let set_cursor_from_row
15084 find the best candidate. */
15085 /* FIXME: Revisit this when glyph ``spilling'' in
15086 continuation lines' rows is implemented for
15087 bidi-reordered rows. */
15088 int rv = 0;
15092 int at_zv_p = 0, exact_match_p = 0;
15094 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15095 && PT <= MATRIX_ROW_END_CHARPOS (row)
15096 && cursor_row_p (row))
15097 rv |= set_cursor_from_row (w, row, w->current_matrix,
15098 0, 0, 0, 0);
15099 /* As soon as we've found the exact match for point,
15100 or the first suitable row whose ends_at_zv_p flag
15101 is set, we are done. */
15102 at_zv_p =
15103 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15104 if (rv && !at_zv_p
15105 && w->cursor.hpos >= 0
15106 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15107 w->cursor.vpos))
15109 struct glyph_row *candidate =
15110 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15111 struct glyph *g =
15112 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15113 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15115 exact_match_p =
15116 (BUFFERP (g->object) && g->charpos == PT)
15117 || (INTEGERP (g->object)
15118 && (g->charpos == PT
15119 || (g->charpos == 0 && endpos - 1 == PT)));
15121 if (rv && (at_zv_p || exact_match_p))
15123 rc = CURSOR_MOVEMENT_SUCCESS;
15124 break;
15126 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15127 break;
15128 ++row;
15130 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15131 || row->continued_p)
15132 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15133 || (MATRIX_ROW_START_CHARPOS (row) == PT
15134 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15135 /* If we didn't find any candidate rows, or exited the
15136 loop before all the candidates were examined, signal
15137 to the caller that this method failed. */
15138 if (rc != CURSOR_MOVEMENT_SUCCESS
15139 && !(rv
15140 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15141 && !row->continued_p))
15142 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15143 else if (rv)
15144 rc = CURSOR_MOVEMENT_SUCCESS;
15146 else
15150 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15152 rc = CURSOR_MOVEMENT_SUCCESS;
15153 break;
15155 ++row;
15157 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15158 && MATRIX_ROW_START_CHARPOS (row) == PT
15159 && cursor_row_p (row));
15164 return rc;
15167 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15168 static
15169 #endif
15170 void
15171 set_vertical_scroll_bar (struct window *w)
15173 ptrdiff_t start, end, whole;
15175 /* Calculate the start and end positions for the current window.
15176 At some point, it would be nice to choose between scrollbars
15177 which reflect the whole buffer size, with special markers
15178 indicating narrowing, and scrollbars which reflect only the
15179 visible region.
15181 Note that mini-buffers sometimes aren't displaying any text. */
15182 if (!MINI_WINDOW_P (w)
15183 || (w == XWINDOW (minibuf_window)
15184 && NILP (echo_area_buffer[0])))
15186 struct buffer *buf = XBUFFER (w->contents);
15187 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15188 start = marker_position (w->start) - BUF_BEGV (buf);
15189 /* I don't think this is guaranteed to be right. For the
15190 moment, we'll pretend it is. */
15191 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15193 if (end < start)
15194 end = start;
15195 if (whole < (end - start))
15196 whole = end - start;
15198 else
15199 start = end = whole = 0;
15201 /* Indicate what this scroll bar ought to be displaying now. */
15202 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15203 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15204 (w, end - start, whole, start);
15208 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15209 selected_window is redisplayed.
15211 We can return without actually redisplaying the window if
15212 fonts_changed_p. In that case, redisplay_internal will
15213 retry. */
15215 static void
15216 redisplay_window (Lisp_Object window, int just_this_one_p)
15218 struct window *w = XWINDOW (window);
15219 struct frame *f = XFRAME (w->frame);
15220 struct buffer *buffer = XBUFFER (w->contents);
15221 struct buffer *old = current_buffer;
15222 struct text_pos lpoint, opoint, startp;
15223 int update_mode_line;
15224 int tem;
15225 struct it it;
15226 /* Record it now because it's overwritten. */
15227 int current_matrix_up_to_date_p = 0;
15228 int used_current_matrix_p = 0;
15229 /* This is less strict than current_matrix_up_to_date_p.
15230 It indicates that the buffer contents and narrowing are unchanged. */
15231 int buffer_unchanged_p = 0;
15232 int temp_scroll_step = 0;
15233 ptrdiff_t count = SPECPDL_INDEX ();
15234 int rc;
15235 int centering_position = -1;
15236 int last_line_misfit = 0;
15237 ptrdiff_t beg_unchanged, end_unchanged;
15239 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15240 opoint = lpoint;
15242 #ifdef GLYPH_DEBUG
15243 *w->desired_matrix->method = 0;
15244 #endif
15246 /* Make sure that both W's markers are valid. */
15247 eassert (XMARKER (w->start)->buffer == buffer);
15248 eassert (XMARKER (w->pointm)->buffer == buffer);
15250 restart:
15251 reconsider_clip_changes (w, buffer);
15253 /* Has the mode line to be updated? */
15254 update_mode_line = (w->update_mode_line
15255 || update_mode_lines
15256 || buffer->clip_changed
15257 || buffer->prevent_redisplay_optimizations_p);
15259 if (MINI_WINDOW_P (w))
15261 if (w == XWINDOW (echo_area_window)
15262 && !NILP (echo_area_buffer[0]))
15264 if (update_mode_line)
15265 /* We may have to update a tty frame's menu bar or a
15266 tool-bar. Example `M-x C-h C-h C-g'. */
15267 goto finish_menu_bars;
15268 else
15269 /* We've already displayed the echo area glyphs in this window. */
15270 goto finish_scroll_bars;
15272 else if ((w != XWINDOW (minibuf_window)
15273 || minibuf_level == 0)
15274 /* When buffer is nonempty, redisplay window normally. */
15275 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15276 /* Quail displays non-mini buffers in minibuffer window.
15277 In that case, redisplay the window normally. */
15278 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15280 /* W is a mini-buffer window, but it's not active, so clear
15281 it. */
15282 int yb = window_text_bottom_y (w);
15283 struct glyph_row *row;
15284 int y;
15286 for (y = 0, row = w->desired_matrix->rows;
15287 y < yb;
15288 y += row->height, ++row)
15289 blank_row (w, row, y);
15290 goto finish_scroll_bars;
15293 clear_glyph_matrix (w->desired_matrix);
15296 /* Otherwise set up data on this window; select its buffer and point
15297 value. */
15298 /* Really select the buffer, for the sake of buffer-local
15299 variables. */
15300 set_buffer_internal_1 (XBUFFER (w->contents));
15302 current_matrix_up_to_date_p
15303 = (w->window_end_valid
15304 && !current_buffer->clip_changed
15305 && !current_buffer->prevent_redisplay_optimizations_p
15306 && !window_outdated (w));
15308 /* Run the window-bottom-change-functions
15309 if it is possible that the text on the screen has changed
15310 (either due to modification of the text, or any other reason). */
15311 if (!current_matrix_up_to_date_p
15312 && !NILP (Vwindow_text_change_functions))
15314 safe_run_hooks (Qwindow_text_change_functions);
15315 goto restart;
15318 beg_unchanged = BEG_UNCHANGED;
15319 end_unchanged = END_UNCHANGED;
15321 SET_TEXT_POS (opoint, PT, PT_BYTE);
15323 specbind (Qinhibit_point_motion_hooks, Qt);
15325 buffer_unchanged_p
15326 = (w->window_end_valid
15327 && !current_buffer->clip_changed
15328 && !window_outdated (w));
15330 /* When windows_or_buffers_changed is non-zero, we can't rely on
15331 the window end being valid, so set it to nil there. */
15332 if (windows_or_buffers_changed)
15334 /* If window starts on a continuation line, maybe adjust the
15335 window start in case the window's width changed. */
15336 if (XMARKER (w->start)->buffer == current_buffer)
15337 compute_window_start_on_continuation_line (w);
15339 w->window_end_valid = 0;
15342 /* Some sanity checks. */
15343 CHECK_WINDOW_END (w);
15344 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15345 emacs_abort ();
15346 if (BYTEPOS (opoint) < CHARPOS (opoint))
15347 emacs_abort ();
15349 if (mode_line_update_needed (w))
15350 update_mode_line = 1;
15352 /* Point refers normally to the selected window. For any other
15353 window, set up appropriate value. */
15354 if (!EQ (window, selected_window))
15356 ptrdiff_t new_pt = marker_position (w->pointm);
15357 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15358 if (new_pt < BEGV)
15360 new_pt = BEGV;
15361 new_pt_byte = BEGV_BYTE;
15362 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15364 else if (new_pt > (ZV - 1))
15366 new_pt = ZV;
15367 new_pt_byte = ZV_BYTE;
15368 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15371 /* We don't use SET_PT so that the point-motion hooks don't run. */
15372 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15375 /* If any of the character widths specified in the display table
15376 have changed, invalidate the width run cache. It's true that
15377 this may be a bit late to catch such changes, but the rest of
15378 redisplay goes (non-fatally) haywire when the display table is
15379 changed, so why should we worry about doing any better? */
15380 if (current_buffer->width_run_cache)
15382 struct Lisp_Char_Table *disptab = buffer_display_table ();
15384 if (! disptab_matches_widthtab
15385 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15387 invalidate_region_cache (current_buffer,
15388 current_buffer->width_run_cache,
15389 BEG, Z);
15390 recompute_width_table (current_buffer, disptab);
15394 /* If window-start is screwed up, choose a new one. */
15395 if (XMARKER (w->start)->buffer != current_buffer)
15396 goto recenter;
15398 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15400 /* If someone specified a new starting point but did not insist,
15401 check whether it can be used. */
15402 if (w->optional_new_start
15403 && CHARPOS (startp) >= BEGV
15404 && CHARPOS (startp) <= ZV)
15406 w->optional_new_start = 0;
15407 start_display (&it, w, startp);
15408 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15409 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15410 if (IT_CHARPOS (it) == PT)
15411 w->force_start = 1;
15412 /* IT may overshoot PT if text at PT is invisible. */
15413 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15414 w->force_start = 1;
15417 force_start:
15419 /* Handle case where place to start displaying has been specified,
15420 unless the specified location is outside the accessible range. */
15421 if (w->force_start || w->frozen_window_start_p)
15423 /* We set this later on if we have to adjust point. */
15424 int new_vpos = -1;
15426 w->force_start = 0;
15427 w->vscroll = 0;
15428 w->window_end_valid = 0;
15430 /* Forget any recorded base line for line number display. */
15431 if (!buffer_unchanged_p)
15432 w->base_line_number = 0;
15434 /* Redisplay the mode line. Select the buffer properly for that.
15435 Also, run the hook window-scroll-functions
15436 because we have scrolled. */
15437 /* Note, we do this after clearing force_start because
15438 if there's an error, it is better to forget about force_start
15439 than to get into an infinite loop calling the hook functions
15440 and having them get more errors. */
15441 if (!update_mode_line
15442 || ! NILP (Vwindow_scroll_functions))
15444 update_mode_line = 1;
15445 w->update_mode_line = 1;
15446 startp = run_window_scroll_functions (window, startp);
15449 w->last_modified = 0;
15450 w->last_overlay_modified = 0;
15451 if (CHARPOS (startp) < BEGV)
15452 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15453 else if (CHARPOS (startp) > ZV)
15454 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15456 /* Redisplay, then check if cursor has been set during the
15457 redisplay. Give up if new fonts were loaded. */
15458 /* We used to issue a CHECK_MARGINS argument to try_window here,
15459 but this causes scrolling to fail when point begins inside
15460 the scroll margin (bug#148) -- cyd */
15461 if (!try_window (window, startp, 0))
15463 w->force_start = 1;
15464 clear_glyph_matrix (w->desired_matrix);
15465 goto need_larger_matrices;
15468 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15470 /* If point does not appear, try to move point so it does
15471 appear. The desired matrix has been built above, so we
15472 can use it here. */
15473 new_vpos = window_box_height (w) / 2;
15476 if (!cursor_row_fully_visible_p (w, 0, 0))
15478 /* Point does appear, but on a line partly visible at end of window.
15479 Move it back to a fully-visible line. */
15480 new_vpos = window_box_height (w);
15482 else if (w->cursor.vpos >=0)
15484 /* Some people insist on not letting point enter the scroll
15485 margin, even though this part handles windows that didn't
15486 scroll at all. */
15487 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15488 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15489 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15491 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15492 below, which finds the row to move point to, advances by
15493 the Y coordinate of the _next_ row, see the definition of
15494 MATRIX_ROW_BOTTOM_Y. */
15495 if (w->cursor.vpos < margin + header_line)
15496 new_vpos
15497 = pixel_margin + (header_line
15498 ? CURRENT_HEADER_LINE_HEIGHT (w)
15499 : 0) + FRAME_LINE_HEIGHT (f);
15500 else
15502 int window_height = window_box_height (w);
15504 if (header_line)
15505 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15506 if (w->cursor.y >= window_height - pixel_margin)
15507 new_vpos = window_height - pixel_margin;
15511 /* If we need to move point for either of the above reasons,
15512 now actually do it. */
15513 if (new_vpos >= 0)
15515 struct glyph_row *row;
15517 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15518 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15519 ++row;
15521 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15522 MATRIX_ROW_START_BYTEPOS (row));
15524 if (w != XWINDOW (selected_window))
15525 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15526 else if (current_buffer == old)
15527 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15529 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15531 /* If we are highlighting the region, then we just changed
15532 the region, so redisplay to show it. */
15533 if (markpos_of_region () >= 0)
15535 clear_glyph_matrix (w->desired_matrix);
15536 if (!try_window (window, startp, 0))
15537 goto need_larger_matrices;
15541 #ifdef GLYPH_DEBUG
15542 debug_method_add (w, "forced window start");
15543 #endif
15544 goto done;
15547 /* Handle case where text has not changed, only point, and it has
15548 not moved off the frame, and we are not retrying after hscroll.
15549 (current_matrix_up_to_date_p is nonzero when retrying.) */
15550 if (current_matrix_up_to_date_p
15551 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15552 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15554 switch (rc)
15556 case CURSOR_MOVEMENT_SUCCESS:
15557 used_current_matrix_p = 1;
15558 goto done;
15560 case CURSOR_MOVEMENT_MUST_SCROLL:
15561 goto try_to_scroll;
15563 default:
15564 emacs_abort ();
15567 /* If current starting point was originally the beginning of a line
15568 but no longer is, find a new starting point. */
15569 else if (w->start_at_line_beg
15570 && !(CHARPOS (startp) <= BEGV
15571 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15573 #ifdef GLYPH_DEBUG
15574 debug_method_add (w, "recenter 1");
15575 #endif
15576 goto recenter;
15579 /* Try scrolling with try_window_id. Value is > 0 if update has
15580 been done, it is -1 if we know that the same window start will
15581 not work. It is 0 if unsuccessful for some other reason. */
15582 else if ((tem = try_window_id (w)) != 0)
15584 #ifdef GLYPH_DEBUG
15585 debug_method_add (w, "try_window_id %d", tem);
15586 #endif
15588 if (fonts_changed_p)
15589 goto need_larger_matrices;
15590 if (tem > 0)
15591 goto done;
15593 /* Otherwise try_window_id has returned -1 which means that we
15594 don't want the alternative below this comment to execute. */
15596 else if (CHARPOS (startp) >= BEGV
15597 && CHARPOS (startp) <= ZV
15598 && PT >= CHARPOS (startp)
15599 && (CHARPOS (startp) < ZV
15600 /* Avoid starting at end of buffer. */
15601 || CHARPOS (startp) == BEGV
15602 || !window_outdated (w)))
15604 int d1, d2, d3, d4, d5, d6;
15606 /* If first window line is a continuation line, and window start
15607 is inside the modified region, but the first change is before
15608 current window start, we must select a new window start.
15610 However, if this is the result of a down-mouse event (e.g. by
15611 extending the mouse-drag-overlay), we don't want to select a
15612 new window start, since that would change the position under
15613 the mouse, resulting in an unwanted mouse-movement rather
15614 than a simple mouse-click. */
15615 if (!w->start_at_line_beg
15616 && NILP (do_mouse_tracking)
15617 && CHARPOS (startp) > BEGV
15618 && CHARPOS (startp) > BEG + beg_unchanged
15619 && CHARPOS (startp) <= Z - end_unchanged
15620 /* Even if w->start_at_line_beg is nil, a new window may
15621 start at a line_beg, since that's how set_buffer_window
15622 sets it. So, we need to check the return value of
15623 compute_window_start_on_continuation_line. (See also
15624 bug#197). */
15625 && XMARKER (w->start)->buffer == current_buffer
15626 && compute_window_start_on_continuation_line (w)
15627 /* It doesn't make sense to force the window start like we
15628 do at label force_start if it is already known that point
15629 will not be visible in the resulting window, because
15630 doing so will move point from its correct position
15631 instead of scrolling the window to bring point into view.
15632 See bug#9324. */
15633 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15635 w->force_start = 1;
15636 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15637 goto force_start;
15640 #ifdef GLYPH_DEBUG
15641 debug_method_add (w, "same window start");
15642 #endif
15644 /* Try to redisplay starting at same place as before.
15645 If point has not moved off frame, accept the results. */
15646 if (!current_matrix_up_to_date_p
15647 /* Don't use try_window_reusing_current_matrix in this case
15648 because a window scroll function can have changed the
15649 buffer. */
15650 || !NILP (Vwindow_scroll_functions)
15651 || MINI_WINDOW_P (w)
15652 || !(used_current_matrix_p
15653 = try_window_reusing_current_matrix (w)))
15655 IF_DEBUG (debug_method_add (w, "1"));
15656 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15657 /* -1 means we need to scroll.
15658 0 means we need new matrices, but fonts_changed_p
15659 is set in that case, so we will detect it below. */
15660 goto try_to_scroll;
15663 if (fonts_changed_p)
15664 goto need_larger_matrices;
15666 if (w->cursor.vpos >= 0)
15668 if (!just_this_one_p
15669 || current_buffer->clip_changed
15670 || BEG_UNCHANGED < CHARPOS (startp))
15671 /* Forget any recorded base line for line number display. */
15672 w->base_line_number = 0;
15674 if (!cursor_row_fully_visible_p (w, 1, 0))
15676 clear_glyph_matrix (w->desired_matrix);
15677 last_line_misfit = 1;
15679 /* Drop through and scroll. */
15680 else
15681 goto done;
15683 else
15684 clear_glyph_matrix (w->desired_matrix);
15687 try_to_scroll:
15689 w->last_modified = 0;
15690 w->last_overlay_modified = 0;
15692 /* Redisplay the mode line. Select the buffer properly for that. */
15693 if (!update_mode_line)
15695 update_mode_line = 1;
15696 w->update_mode_line = 1;
15699 /* Try to scroll by specified few lines. */
15700 if ((scroll_conservatively
15701 || emacs_scroll_step
15702 || temp_scroll_step
15703 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15704 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15705 && CHARPOS (startp) >= BEGV
15706 && CHARPOS (startp) <= ZV)
15708 /* The function returns -1 if new fonts were loaded, 1 if
15709 successful, 0 if not successful. */
15710 int ss = try_scrolling (window, just_this_one_p,
15711 scroll_conservatively,
15712 emacs_scroll_step,
15713 temp_scroll_step, last_line_misfit);
15714 switch (ss)
15716 case SCROLLING_SUCCESS:
15717 goto done;
15719 case SCROLLING_NEED_LARGER_MATRICES:
15720 goto need_larger_matrices;
15722 case SCROLLING_FAILED:
15723 break;
15725 default:
15726 emacs_abort ();
15730 /* Finally, just choose a place to start which positions point
15731 according to user preferences. */
15733 recenter:
15735 #ifdef GLYPH_DEBUG
15736 debug_method_add (w, "recenter");
15737 #endif
15739 /* Forget any previously recorded base line for line number display. */
15740 if (!buffer_unchanged_p)
15741 w->base_line_number = 0;
15743 /* Determine the window start relative to point. */
15744 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15745 it.current_y = it.last_visible_y;
15746 if (centering_position < 0)
15748 int margin =
15749 scroll_margin > 0
15750 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15751 : 0;
15752 ptrdiff_t margin_pos = CHARPOS (startp);
15753 Lisp_Object aggressive;
15754 int scrolling_up;
15756 /* If there is a scroll margin at the top of the window, find
15757 its character position. */
15758 if (margin
15759 /* Cannot call start_display if startp is not in the
15760 accessible region of the buffer. This can happen when we
15761 have just switched to a different buffer and/or changed
15762 its restriction. In that case, startp is initialized to
15763 the character position 1 (BEGV) because we did not yet
15764 have chance to display the buffer even once. */
15765 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15767 struct it it1;
15768 void *it1data = NULL;
15770 SAVE_IT (it1, it, it1data);
15771 start_display (&it1, w, startp);
15772 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15773 margin_pos = IT_CHARPOS (it1);
15774 RESTORE_IT (&it, &it, it1data);
15776 scrolling_up = PT > margin_pos;
15777 aggressive =
15778 scrolling_up
15779 ? BVAR (current_buffer, scroll_up_aggressively)
15780 : BVAR (current_buffer, scroll_down_aggressively);
15782 if (!MINI_WINDOW_P (w)
15783 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15785 int pt_offset = 0;
15787 /* Setting scroll-conservatively overrides
15788 scroll-*-aggressively. */
15789 if (!scroll_conservatively && NUMBERP (aggressive))
15791 double float_amount = XFLOATINT (aggressive);
15793 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15794 if (pt_offset == 0 && float_amount > 0)
15795 pt_offset = 1;
15796 if (pt_offset && margin > 0)
15797 margin -= 1;
15799 /* Compute how much to move the window start backward from
15800 point so that point will be displayed where the user
15801 wants it. */
15802 if (scrolling_up)
15804 centering_position = it.last_visible_y;
15805 if (pt_offset)
15806 centering_position -= pt_offset;
15807 centering_position -=
15808 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15809 + WINDOW_HEADER_LINE_HEIGHT (w);
15810 /* Don't let point enter the scroll margin near top of
15811 the window. */
15812 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15813 centering_position = margin * FRAME_LINE_HEIGHT (f);
15815 else
15816 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15818 else
15819 /* Set the window start half the height of the window backward
15820 from point. */
15821 centering_position = window_box_height (w) / 2;
15823 move_it_vertically_backward (&it, centering_position);
15825 eassert (IT_CHARPOS (it) >= BEGV);
15827 /* The function move_it_vertically_backward may move over more
15828 than the specified y-distance. If it->w is small, e.g. a
15829 mini-buffer window, we may end up in front of the window's
15830 display area. Start displaying at the start of the line
15831 containing PT in this case. */
15832 if (it.current_y <= 0)
15834 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15835 move_it_vertically_backward (&it, 0);
15836 it.current_y = 0;
15839 it.current_x = it.hpos = 0;
15841 /* Set the window start position here explicitly, to avoid an
15842 infinite loop in case the functions in window-scroll-functions
15843 get errors. */
15844 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15846 /* Run scroll hooks. */
15847 startp = run_window_scroll_functions (window, it.current.pos);
15849 /* Redisplay the window. */
15850 if (!current_matrix_up_to_date_p
15851 || windows_or_buffers_changed
15852 || cursor_type_changed
15853 /* Don't use try_window_reusing_current_matrix in this case
15854 because it can have changed the buffer. */
15855 || !NILP (Vwindow_scroll_functions)
15856 || !just_this_one_p
15857 || MINI_WINDOW_P (w)
15858 || !(used_current_matrix_p
15859 = try_window_reusing_current_matrix (w)))
15860 try_window (window, startp, 0);
15862 /* If new fonts have been loaded (due to fontsets), give up. We
15863 have to start a new redisplay since we need to re-adjust glyph
15864 matrices. */
15865 if (fonts_changed_p)
15866 goto need_larger_matrices;
15868 /* If cursor did not appear assume that the middle of the window is
15869 in the first line of the window. Do it again with the next line.
15870 (Imagine a window of height 100, displaying two lines of height
15871 60. Moving back 50 from it->last_visible_y will end in the first
15872 line.) */
15873 if (w->cursor.vpos < 0)
15875 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15877 clear_glyph_matrix (w->desired_matrix);
15878 move_it_by_lines (&it, 1);
15879 try_window (window, it.current.pos, 0);
15881 else if (PT < IT_CHARPOS (it))
15883 clear_glyph_matrix (w->desired_matrix);
15884 move_it_by_lines (&it, -1);
15885 try_window (window, it.current.pos, 0);
15887 else
15889 /* Not much we can do about it. */
15893 /* Consider the following case: Window starts at BEGV, there is
15894 invisible, intangible text at BEGV, so that display starts at
15895 some point START > BEGV. It can happen that we are called with
15896 PT somewhere between BEGV and START. Try to handle that case. */
15897 if (w->cursor.vpos < 0)
15899 struct glyph_row *row = w->current_matrix->rows;
15900 if (row->mode_line_p)
15901 ++row;
15902 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15905 if (!cursor_row_fully_visible_p (w, 0, 0))
15907 /* If vscroll is enabled, disable it and try again. */
15908 if (w->vscroll)
15910 w->vscroll = 0;
15911 clear_glyph_matrix (w->desired_matrix);
15912 goto recenter;
15915 /* Users who set scroll-conservatively to a large number want
15916 point just above/below the scroll margin. If we ended up
15917 with point's row partially visible, move the window start to
15918 make that row fully visible and out of the margin. */
15919 if (scroll_conservatively > SCROLL_LIMIT)
15921 int margin =
15922 scroll_margin > 0
15923 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15924 : 0;
15925 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15927 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15928 clear_glyph_matrix (w->desired_matrix);
15929 if (1 == try_window (window, it.current.pos,
15930 TRY_WINDOW_CHECK_MARGINS))
15931 goto done;
15934 /* If centering point failed to make the whole line visible,
15935 put point at the top instead. That has to make the whole line
15936 visible, if it can be done. */
15937 if (centering_position == 0)
15938 goto done;
15940 clear_glyph_matrix (w->desired_matrix);
15941 centering_position = 0;
15942 goto recenter;
15945 done:
15947 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15948 w->start_at_line_beg = (CHARPOS (startp) == BEGV
15949 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
15951 /* Display the mode line, if we must. */
15952 if ((update_mode_line
15953 /* If window not full width, must redo its mode line
15954 if (a) the window to its side is being redone and
15955 (b) we do a frame-based redisplay. This is a consequence
15956 of how inverted lines are drawn in frame-based redisplay. */
15957 || (!just_this_one_p
15958 && !FRAME_WINDOW_P (f)
15959 && !WINDOW_FULL_WIDTH_P (w))
15960 /* Line number to display. */
15961 || w->base_line_pos > 0
15962 /* Column number is displayed and different from the one displayed. */
15963 || (w->column_number_displayed != -1
15964 && (w->column_number_displayed != current_column ())))
15965 /* This means that the window has a mode line. */
15966 && (WINDOW_WANTS_MODELINE_P (w)
15967 || WINDOW_WANTS_HEADER_LINE_P (w)))
15969 display_mode_lines (w);
15971 /* If mode line height has changed, arrange for a thorough
15972 immediate redisplay using the correct mode line height. */
15973 if (WINDOW_WANTS_MODELINE_P (w)
15974 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15976 fonts_changed_p = 1;
15977 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15978 = DESIRED_MODE_LINE_HEIGHT (w);
15981 /* If header line height has changed, arrange for a thorough
15982 immediate redisplay using the correct header line height. */
15983 if (WINDOW_WANTS_HEADER_LINE_P (w)
15984 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15986 fonts_changed_p = 1;
15987 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15988 = DESIRED_HEADER_LINE_HEIGHT (w);
15991 if (fonts_changed_p)
15992 goto need_larger_matrices;
15995 if (!line_number_displayed && w->base_line_pos != -1)
15997 w->base_line_pos = 0;
15998 w->base_line_number = 0;
16001 finish_menu_bars:
16003 /* When we reach a frame's selected window, redo the frame's menu bar. */
16004 if (update_mode_line
16005 && EQ (FRAME_SELECTED_WINDOW (f), window))
16007 int redisplay_menu_p = 0;
16009 if (FRAME_WINDOW_P (f))
16011 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16012 || defined (HAVE_NS) || defined (USE_GTK)
16013 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16014 #else
16015 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16016 #endif
16018 else
16019 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16021 if (redisplay_menu_p)
16022 display_menu_bar (w);
16024 #ifdef HAVE_WINDOW_SYSTEM
16025 if (FRAME_WINDOW_P (f))
16027 #if defined (USE_GTK) || defined (HAVE_NS)
16028 if (FRAME_EXTERNAL_TOOL_BAR (f))
16029 redisplay_tool_bar (f);
16030 #else
16031 if (WINDOWP (f->tool_bar_window)
16032 && (FRAME_TOOL_BAR_LINES (f) > 0
16033 || !NILP (Vauto_resize_tool_bars))
16034 && redisplay_tool_bar (f))
16035 ignore_mouse_drag_p = 1;
16036 #endif
16038 #endif
16041 #ifdef HAVE_WINDOW_SYSTEM
16042 if (FRAME_WINDOW_P (f)
16043 && update_window_fringes (w, (just_this_one_p
16044 || (!used_current_matrix_p && !overlay_arrow_seen)
16045 || w->pseudo_window_p)))
16047 update_begin (f);
16048 block_input ();
16049 if (draw_window_fringes (w, 1))
16050 x_draw_vertical_border (w);
16051 unblock_input ();
16052 update_end (f);
16054 #endif /* HAVE_WINDOW_SYSTEM */
16056 /* We go to this label, with fonts_changed_p set,
16057 if it is necessary to try again using larger glyph matrices.
16058 We have to redeem the scroll bar even in this case,
16059 because the loop in redisplay_internal expects that. */
16060 need_larger_matrices:
16062 finish_scroll_bars:
16064 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16066 /* Set the thumb's position and size. */
16067 set_vertical_scroll_bar (w);
16069 /* Note that we actually used the scroll bar attached to this
16070 window, so it shouldn't be deleted at the end of redisplay. */
16071 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16072 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16075 /* Restore current_buffer and value of point in it. The window
16076 update may have changed the buffer, so first make sure `opoint'
16077 is still valid (Bug#6177). */
16078 if (CHARPOS (opoint) < BEGV)
16079 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16080 else if (CHARPOS (opoint) > ZV)
16081 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16082 else
16083 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16085 set_buffer_internal_1 (old);
16086 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16087 shorter. This can be caused by log truncation in *Messages*. */
16088 if (CHARPOS (lpoint) <= ZV)
16089 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16091 unbind_to (count, Qnil);
16095 /* Build the complete desired matrix of WINDOW with a window start
16096 buffer position POS.
16098 Value is 1 if successful. It is zero if fonts were loaded during
16099 redisplay which makes re-adjusting glyph matrices necessary, and -1
16100 if point would appear in the scroll margins.
16101 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16102 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16103 set in FLAGS.) */
16106 try_window (Lisp_Object window, struct text_pos pos, int flags)
16108 struct window *w = XWINDOW (window);
16109 struct it it;
16110 struct glyph_row *last_text_row = NULL;
16111 struct frame *f = XFRAME (w->frame);
16113 /* Make POS the new window start. */
16114 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16116 /* Mark cursor position as unknown. No overlay arrow seen. */
16117 w->cursor.vpos = -1;
16118 overlay_arrow_seen = 0;
16120 /* Initialize iterator and info to start at POS. */
16121 start_display (&it, w, pos);
16123 /* Display all lines of W. */
16124 while (it.current_y < it.last_visible_y)
16126 if (display_line (&it))
16127 last_text_row = it.glyph_row - 1;
16128 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16129 return 0;
16132 /* Don't let the cursor end in the scroll margins. */
16133 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16134 && !MINI_WINDOW_P (w))
16136 int this_scroll_margin;
16138 if (scroll_margin > 0)
16140 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16141 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16143 else
16144 this_scroll_margin = 0;
16146 if ((w->cursor.y >= 0 /* not vscrolled */
16147 && w->cursor.y < this_scroll_margin
16148 && CHARPOS (pos) > BEGV
16149 && IT_CHARPOS (it) < ZV)
16150 /* rms: considering make_cursor_line_fully_visible_p here
16151 seems to give wrong results. We don't want to recenter
16152 when the last line is partly visible, we want to allow
16153 that case to be handled in the usual way. */
16154 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16156 w->cursor.vpos = -1;
16157 clear_glyph_matrix (w->desired_matrix);
16158 return -1;
16162 /* If bottom moved off end of frame, change mode line percentage. */
16163 if (XFASTINT (w->window_end_pos) <= 0
16164 && Z != IT_CHARPOS (it))
16165 w->update_mode_line = 1;
16167 /* Set window_end_pos to the offset of the last character displayed
16168 on the window from the end of current_buffer. Set
16169 window_end_vpos to its row number. */
16170 if (last_text_row)
16172 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16173 w->window_end_bytepos
16174 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16175 wset_window_end_pos
16176 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16177 wset_window_end_vpos
16178 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16179 eassert
16180 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16181 XFASTINT (w->window_end_vpos))));
16183 else
16185 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16186 wset_window_end_pos (w, make_number (Z - ZV));
16187 wset_window_end_vpos (w, make_number (0));
16190 /* But that is not valid info until redisplay finishes. */
16191 w->window_end_valid = 0;
16192 return 1;
16197 /************************************************************************
16198 Window redisplay reusing current matrix when buffer has not changed
16199 ************************************************************************/
16201 /* Try redisplay of window W showing an unchanged buffer with a
16202 different window start than the last time it was displayed by
16203 reusing its current matrix. Value is non-zero if successful.
16204 W->start is the new window start. */
16206 static int
16207 try_window_reusing_current_matrix (struct window *w)
16209 struct frame *f = XFRAME (w->frame);
16210 struct glyph_row *bottom_row;
16211 struct it it;
16212 struct run run;
16213 struct text_pos start, new_start;
16214 int nrows_scrolled, i;
16215 struct glyph_row *last_text_row;
16216 struct glyph_row *last_reused_text_row;
16217 struct glyph_row *start_row;
16218 int start_vpos, min_y, max_y;
16220 #ifdef GLYPH_DEBUG
16221 if (inhibit_try_window_reusing)
16222 return 0;
16223 #endif
16225 if (/* This function doesn't handle terminal frames. */
16226 !FRAME_WINDOW_P (f)
16227 /* Don't try to reuse the display if windows have been split
16228 or such. */
16229 || windows_or_buffers_changed
16230 || cursor_type_changed)
16231 return 0;
16233 /* Can't do this if region may have changed. */
16234 if (markpos_of_region () >= 0
16235 || w->region_showing
16236 || !NILP (Vshow_trailing_whitespace))
16237 return 0;
16239 /* If top-line visibility has changed, give up. */
16240 if (WINDOW_WANTS_HEADER_LINE_P (w)
16241 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16242 return 0;
16244 /* Give up if old or new display is scrolled vertically. We could
16245 make this function handle this, but right now it doesn't. */
16246 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16247 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16248 return 0;
16250 /* The variable new_start now holds the new window start. The old
16251 start `start' can be determined from the current matrix. */
16252 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16253 start = start_row->minpos;
16254 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16256 /* Clear the desired matrix for the display below. */
16257 clear_glyph_matrix (w->desired_matrix);
16259 if (CHARPOS (new_start) <= CHARPOS (start))
16261 /* Don't use this method if the display starts with an ellipsis
16262 displayed for invisible text. It's not easy to handle that case
16263 below, and it's certainly not worth the effort since this is
16264 not a frequent case. */
16265 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16266 return 0;
16268 IF_DEBUG (debug_method_add (w, "twu1"));
16270 /* Display up to a row that can be reused. The variable
16271 last_text_row is set to the last row displayed that displays
16272 text. Note that it.vpos == 0 if or if not there is a
16273 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16274 start_display (&it, w, new_start);
16275 w->cursor.vpos = -1;
16276 last_text_row = last_reused_text_row = NULL;
16278 while (it.current_y < it.last_visible_y
16279 && !fonts_changed_p)
16281 /* If we have reached into the characters in the START row,
16282 that means the line boundaries have changed. So we
16283 can't start copying with the row START. Maybe it will
16284 work to start copying with the following row. */
16285 while (IT_CHARPOS (it) > CHARPOS (start))
16287 /* Advance to the next row as the "start". */
16288 start_row++;
16289 start = start_row->minpos;
16290 /* If there are no more rows to try, or just one, give up. */
16291 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16292 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16293 || CHARPOS (start) == ZV)
16295 clear_glyph_matrix (w->desired_matrix);
16296 return 0;
16299 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16301 /* If we have reached alignment, we can copy the rest of the
16302 rows. */
16303 if (IT_CHARPOS (it) == CHARPOS (start)
16304 /* Don't accept "alignment" inside a display vector,
16305 since start_row could have started in the middle of
16306 that same display vector (thus their character
16307 positions match), and we have no way of telling if
16308 that is the case. */
16309 && it.current.dpvec_index < 0)
16310 break;
16312 if (display_line (&it))
16313 last_text_row = it.glyph_row - 1;
16317 /* A value of current_y < last_visible_y means that we stopped
16318 at the previous window start, which in turn means that we
16319 have at least one reusable row. */
16320 if (it.current_y < it.last_visible_y)
16322 struct glyph_row *row;
16324 /* IT.vpos always starts from 0; it counts text lines. */
16325 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16327 /* Find PT if not already found in the lines displayed. */
16328 if (w->cursor.vpos < 0)
16330 int dy = it.current_y - start_row->y;
16332 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16333 row = row_containing_pos (w, PT, row, NULL, dy);
16334 if (row)
16335 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16336 dy, nrows_scrolled);
16337 else
16339 clear_glyph_matrix (w->desired_matrix);
16340 return 0;
16344 /* Scroll the display. Do it before the current matrix is
16345 changed. The problem here is that update has not yet
16346 run, i.e. part of the current matrix is not up to date.
16347 scroll_run_hook will clear the cursor, and use the
16348 current matrix to get the height of the row the cursor is
16349 in. */
16350 run.current_y = start_row->y;
16351 run.desired_y = it.current_y;
16352 run.height = it.last_visible_y - it.current_y;
16354 if (run.height > 0 && run.current_y != run.desired_y)
16356 update_begin (f);
16357 FRAME_RIF (f)->update_window_begin_hook (w);
16358 FRAME_RIF (f)->clear_window_mouse_face (w);
16359 FRAME_RIF (f)->scroll_run_hook (w, &run);
16360 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16361 update_end (f);
16364 /* Shift current matrix down by nrows_scrolled lines. */
16365 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16366 rotate_matrix (w->current_matrix,
16367 start_vpos,
16368 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16369 nrows_scrolled);
16371 /* Disable lines that must be updated. */
16372 for (i = 0; i < nrows_scrolled; ++i)
16373 (start_row + i)->enabled_p = 0;
16375 /* Re-compute Y positions. */
16376 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16377 max_y = it.last_visible_y;
16378 for (row = start_row + nrows_scrolled;
16379 row < bottom_row;
16380 ++row)
16382 row->y = it.current_y;
16383 row->visible_height = row->height;
16385 if (row->y < min_y)
16386 row->visible_height -= min_y - row->y;
16387 if (row->y + row->height > max_y)
16388 row->visible_height -= row->y + row->height - max_y;
16389 if (row->fringe_bitmap_periodic_p)
16390 row->redraw_fringe_bitmaps_p = 1;
16392 it.current_y += row->height;
16394 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16395 last_reused_text_row = row;
16396 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16397 break;
16400 /* Disable lines in the current matrix which are now
16401 below the window. */
16402 for (++row; row < bottom_row; ++row)
16403 row->enabled_p = row->mode_line_p = 0;
16406 /* Update window_end_pos etc.; last_reused_text_row is the last
16407 reused row from the current matrix containing text, if any.
16408 The value of last_text_row is the last displayed line
16409 containing text. */
16410 if (last_reused_text_row)
16412 w->window_end_bytepos
16413 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16414 wset_window_end_pos
16415 (w, make_number (Z
16416 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16417 wset_window_end_vpos
16418 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16419 w->current_matrix)));
16421 else if (last_text_row)
16423 w->window_end_bytepos
16424 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16425 wset_window_end_pos
16426 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16427 wset_window_end_vpos
16428 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16429 w->desired_matrix)));
16431 else
16433 /* This window must be completely empty. */
16434 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16435 wset_window_end_pos (w, make_number (Z - ZV));
16436 wset_window_end_vpos (w, make_number (0));
16438 w->window_end_valid = 0;
16440 /* Update hint: don't try scrolling again in update_window. */
16441 w->desired_matrix->no_scrolling_p = 1;
16443 #ifdef GLYPH_DEBUG
16444 debug_method_add (w, "try_window_reusing_current_matrix 1");
16445 #endif
16446 return 1;
16448 else if (CHARPOS (new_start) > CHARPOS (start))
16450 struct glyph_row *pt_row, *row;
16451 struct glyph_row *first_reusable_row;
16452 struct glyph_row *first_row_to_display;
16453 int dy;
16454 int yb = window_text_bottom_y (w);
16456 /* Find the row starting at new_start, if there is one. Don't
16457 reuse a partially visible line at the end. */
16458 first_reusable_row = start_row;
16459 while (first_reusable_row->enabled_p
16460 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16461 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16462 < CHARPOS (new_start)))
16463 ++first_reusable_row;
16465 /* Give up if there is no row to reuse. */
16466 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16467 || !first_reusable_row->enabled_p
16468 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16469 != CHARPOS (new_start)))
16470 return 0;
16472 /* We can reuse fully visible rows beginning with
16473 first_reusable_row to the end of the window. Set
16474 first_row_to_display to the first row that cannot be reused.
16475 Set pt_row to the row containing point, if there is any. */
16476 pt_row = NULL;
16477 for (first_row_to_display = first_reusable_row;
16478 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16479 ++first_row_to_display)
16481 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16482 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16483 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16484 && first_row_to_display->ends_at_zv_p
16485 && pt_row == NULL)))
16486 pt_row = first_row_to_display;
16489 /* Start displaying at the start of first_row_to_display. */
16490 eassert (first_row_to_display->y < yb);
16491 init_to_row_start (&it, w, first_row_to_display);
16493 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16494 - start_vpos);
16495 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16496 - nrows_scrolled);
16497 it.current_y = (first_row_to_display->y - first_reusable_row->y
16498 + WINDOW_HEADER_LINE_HEIGHT (w));
16500 /* Display lines beginning with first_row_to_display in the
16501 desired matrix. Set last_text_row to the last row displayed
16502 that displays text. */
16503 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16504 if (pt_row == NULL)
16505 w->cursor.vpos = -1;
16506 last_text_row = NULL;
16507 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16508 if (display_line (&it))
16509 last_text_row = it.glyph_row - 1;
16511 /* If point is in a reused row, adjust y and vpos of the cursor
16512 position. */
16513 if (pt_row)
16515 w->cursor.vpos -= nrows_scrolled;
16516 w->cursor.y -= first_reusable_row->y - start_row->y;
16519 /* Give up if point isn't in a row displayed or reused. (This
16520 also handles the case where w->cursor.vpos < nrows_scrolled
16521 after the calls to display_line, which can happen with scroll
16522 margins. See bug#1295.) */
16523 if (w->cursor.vpos < 0)
16525 clear_glyph_matrix (w->desired_matrix);
16526 return 0;
16529 /* Scroll the display. */
16530 run.current_y = first_reusable_row->y;
16531 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16532 run.height = it.last_visible_y - run.current_y;
16533 dy = run.current_y - run.desired_y;
16535 if (run.height)
16537 update_begin (f);
16538 FRAME_RIF (f)->update_window_begin_hook (w);
16539 FRAME_RIF (f)->clear_window_mouse_face (w);
16540 FRAME_RIF (f)->scroll_run_hook (w, &run);
16541 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16542 update_end (f);
16545 /* Adjust Y positions of reused rows. */
16546 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16547 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16548 max_y = it.last_visible_y;
16549 for (row = first_reusable_row; row < first_row_to_display; ++row)
16551 row->y -= dy;
16552 row->visible_height = row->height;
16553 if (row->y < min_y)
16554 row->visible_height -= min_y - row->y;
16555 if (row->y + row->height > max_y)
16556 row->visible_height -= row->y + row->height - max_y;
16557 if (row->fringe_bitmap_periodic_p)
16558 row->redraw_fringe_bitmaps_p = 1;
16561 /* Scroll the current matrix. */
16562 eassert (nrows_scrolled > 0);
16563 rotate_matrix (w->current_matrix,
16564 start_vpos,
16565 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16566 -nrows_scrolled);
16568 /* Disable rows not reused. */
16569 for (row -= nrows_scrolled; row < bottom_row; ++row)
16570 row->enabled_p = 0;
16572 /* Point may have moved to a different line, so we cannot assume that
16573 the previous cursor position is valid; locate the correct row. */
16574 if (pt_row)
16576 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16577 row < bottom_row
16578 && PT >= MATRIX_ROW_END_CHARPOS (row)
16579 && !row->ends_at_zv_p;
16580 row++)
16582 w->cursor.vpos++;
16583 w->cursor.y = row->y;
16585 if (row < bottom_row)
16587 /* Can't simply scan the row for point with
16588 bidi-reordered glyph rows. Let set_cursor_from_row
16589 figure out where to put the cursor, and if it fails,
16590 give up. */
16591 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16593 if (!set_cursor_from_row (w, row, w->current_matrix,
16594 0, 0, 0, 0))
16596 clear_glyph_matrix (w->desired_matrix);
16597 return 0;
16600 else
16602 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16603 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16605 for (; glyph < end
16606 && (!BUFFERP (glyph->object)
16607 || glyph->charpos < PT);
16608 glyph++)
16610 w->cursor.hpos++;
16611 w->cursor.x += glyph->pixel_width;
16617 /* Adjust window end. A null value of last_text_row means that
16618 the window end is in reused rows which in turn means that
16619 only its vpos can have changed. */
16620 if (last_text_row)
16622 w->window_end_bytepos
16623 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16624 wset_window_end_pos
16625 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16626 wset_window_end_vpos
16627 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16628 w->desired_matrix)));
16630 else
16632 wset_window_end_vpos
16633 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16636 w->window_end_valid = 0;
16637 w->desired_matrix->no_scrolling_p = 1;
16639 #ifdef GLYPH_DEBUG
16640 debug_method_add (w, "try_window_reusing_current_matrix 2");
16641 #endif
16642 return 1;
16645 return 0;
16650 /************************************************************************
16651 Window redisplay reusing current matrix when buffer has changed
16652 ************************************************************************/
16654 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16655 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16656 ptrdiff_t *, ptrdiff_t *);
16657 static struct glyph_row *
16658 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16659 struct glyph_row *);
16662 /* Return the last row in MATRIX displaying text. If row START is
16663 non-null, start searching with that row. IT gives the dimensions
16664 of the display. Value is null if matrix is empty; otherwise it is
16665 a pointer to the row found. */
16667 static struct glyph_row *
16668 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16669 struct glyph_row *start)
16671 struct glyph_row *row, *row_found;
16673 /* Set row_found to the last row in IT->w's current matrix
16674 displaying text. The loop looks funny but think of partially
16675 visible lines. */
16676 row_found = NULL;
16677 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16678 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16680 eassert (row->enabled_p);
16681 row_found = row;
16682 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16683 break;
16684 ++row;
16687 return row_found;
16691 /* Return the last row in the current matrix of W that is not affected
16692 by changes at the start of current_buffer that occurred since W's
16693 current matrix was built. Value is null if no such row exists.
16695 BEG_UNCHANGED us the number of characters unchanged at the start of
16696 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16697 first changed character in current_buffer. Characters at positions <
16698 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16699 when the current matrix was built. */
16701 static struct glyph_row *
16702 find_last_unchanged_at_beg_row (struct window *w)
16704 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16705 struct glyph_row *row;
16706 struct glyph_row *row_found = NULL;
16707 int yb = window_text_bottom_y (w);
16709 /* Find the last row displaying unchanged text. */
16710 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16711 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16712 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16713 ++row)
16715 if (/* If row ends before first_changed_pos, it is unchanged,
16716 except in some case. */
16717 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16718 /* When row ends in ZV and we write at ZV it is not
16719 unchanged. */
16720 && !row->ends_at_zv_p
16721 /* When first_changed_pos is the end of a continued line,
16722 row is not unchanged because it may be no longer
16723 continued. */
16724 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16725 && (row->continued_p
16726 || row->exact_window_width_line_p))
16727 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16728 needs to be recomputed, so don't consider this row as
16729 unchanged. This happens when the last line was
16730 bidi-reordered and was killed immediately before this
16731 redisplay cycle. In that case, ROW->end stores the
16732 buffer position of the first visual-order character of
16733 the killed text, which is now beyond ZV. */
16734 && CHARPOS (row->end.pos) <= ZV)
16735 row_found = row;
16737 /* Stop if last visible row. */
16738 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16739 break;
16742 return row_found;
16746 /* Find the first glyph row in the current matrix of W that is not
16747 affected by changes at the end of current_buffer since the
16748 time W's current matrix was built.
16750 Return in *DELTA the number of chars by which buffer positions in
16751 unchanged text at the end of current_buffer must be adjusted.
16753 Return in *DELTA_BYTES the corresponding number of bytes.
16755 Value is null if no such row exists, i.e. all rows are affected by
16756 changes. */
16758 static struct glyph_row *
16759 find_first_unchanged_at_end_row (struct window *w,
16760 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16762 struct glyph_row *row;
16763 struct glyph_row *row_found = NULL;
16765 *delta = *delta_bytes = 0;
16767 /* Display must not have been paused, otherwise the current matrix
16768 is not up to date. */
16769 eassert (w->window_end_valid);
16771 /* A value of window_end_pos >= END_UNCHANGED means that the window
16772 end is in the range of changed text. If so, there is no
16773 unchanged row at the end of W's current matrix. */
16774 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16775 return NULL;
16777 /* Set row to the last row in W's current matrix displaying text. */
16778 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16780 /* If matrix is entirely empty, no unchanged row exists. */
16781 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16783 /* The value of row is the last glyph row in the matrix having a
16784 meaningful buffer position in it. The end position of row
16785 corresponds to window_end_pos. This allows us to translate
16786 buffer positions in the current matrix to current buffer
16787 positions for characters not in changed text. */
16788 ptrdiff_t Z_old =
16789 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16790 ptrdiff_t Z_BYTE_old =
16791 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16792 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16793 struct glyph_row *first_text_row
16794 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16796 *delta = Z - Z_old;
16797 *delta_bytes = Z_BYTE - Z_BYTE_old;
16799 /* Set last_unchanged_pos to the buffer position of the last
16800 character in the buffer that has not been changed. Z is the
16801 index + 1 of the last character in current_buffer, i.e. by
16802 subtracting END_UNCHANGED we get the index of the last
16803 unchanged character, and we have to add BEG to get its buffer
16804 position. */
16805 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16806 last_unchanged_pos_old = last_unchanged_pos - *delta;
16808 /* Search backward from ROW for a row displaying a line that
16809 starts at a minimum position >= last_unchanged_pos_old. */
16810 for (; row > first_text_row; --row)
16812 /* This used to abort, but it can happen.
16813 It is ok to just stop the search instead here. KFS. */
16814 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16815 break;
16817 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16818 row_found = row;
16822 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16824 return row_found;
16828 /* Make sure that glyph rows in the current matrix of window W
16829 reference the same glyph memory as corresponding rows in the
16830 frame's frame matrix. This function is called after scrolling W's
16831 current matrix on a terminal frame in try_window_id and
16832 try_window_reusing_current_matrix. */
16834 static void
16835 sync_frame_with_window_matrix_rows (struct window *w)
16837 struct frame *f = XFRAME (w->frame);
16838 struct glyph_row *window_row, *window_row_end, *frame_row;
16840 /* Preconditions: W must be a leaf window and full-width. Its frame
16841 must have a frame matrix. */
16842 eassert (BUFFERP (w->contents));
16843 eassert (WINDOW_FULL_WIDTH_P (w));
16844 eassert (!FRAME_WINDOW_P (f));
16846 /* If W is a full-width window, glyph pointers in W's current matrix
16847 have, by definition, to be the same as glyph pointers in the
16848 corresponding frame matrix. Note that frame matrices have no
16849 marginal areas (see build_frame_matrix). */
16850 window_row = w->current_matrix->rows;
16851 window_row_end = window_row + w->current_matrix->nrows;
16852 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16853 while (window_row < window_row_end)
16855 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16856 struct glyph *end = window_row->glyphs[LAST_AREA];
16858 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16859 frame_row->glyphs[TEXT_AREA] = start;
16860 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16861 frame_row->glyphs[LAST_AREA] = end;
16863 /* Disable frame rows whose corresponding window rows have
16864 been disabled in try_window_id. */
16865 if (!window_row->enabled_p)
16866 frame_row->enabled_p = 0;
16868 ++window_row, ++frame_row;
16873 /* Find the glyph row in window W containing CHARPOS. Consider all
16874 rows between START and END (not inclusive). END null means search
16875 all rows to the end of the display area of W. Value is the row
16876 containing CHARPOS or null. */
16878 struct glyph_row *
16879 row_containing_pos (struct window *w, ptrdiff_t charpos,
16880 struct glyph_row *start, struct glyph_row *end, int dy)
16882 struct glyph_row *row = start;
16883 struct glyph_row *best_row = NULL;
16884 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16885 int last_y;
16887 /* If we happen to start on a header-line, skip that. */
16888 if (row->mode_line_p)
16889 ++row;
16891 if ((end && row >= end) || !row->enabled_p)
16892 return NULL;
16894 last_y = window_text_bottom_y (w) - dy;
16896 while (1)
16898 /* Give up if we have gone too far. */
16899 if (end && row >= end)
16900 return NULL;
16901 /* This formerly returned if they were equal.
16902 I think that both quantities are of a "last plus one" type;
16903 if so, when they are equal, the row is within the screen. -- rms. */
16904 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16905 return NULL;
16907 /* If it is in this row, return this row. */
16908 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16909 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16910 /* The end position of a row equals the start
16911 position of the next row. If CHARPOS is there, we
16912 would rather display it in the next line, except
16913 when this line ends in ZV. */
16914 && !row->ends_at_zv_p
16915 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16916 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16918 struct glyph *g;
16920 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
16921 || (!best_row && !row->continued_p))
16922 return row;
16923 /* In bidi-reordered rows, there could be several rows
16924 occluding point, all of them belonging to the same
16925 continued line. We need to find the row which fits
16926 CHARPOS the best. */
16927 for (g = row->glyphs[TEXT_AREA];
16928 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16929 g++)
16931 if (!STRINGP (g->object))
16933 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16935 mindif = eabs (g->charpos - charpos);
16936 best_row = row;
16937 /* Exact match always wins. */
16938 if (mindif == 0)
16939 return best_row;
16944 else if (best_row && !row->continued_p)
16945 return best_row;
16946 ++row;
16951 /* Try to redisplay window W by reusing its existing display. W's
16952 current matrix must be up to date when this function is called,
16953 i.e. window_end_valid must be nonzero.
16955 Value is
16957 1 if display has been updated
16958 0 if otherwise unsuccessful
16959 -1 if redisplay with same window start is known not to succeed
16961 The following steps are performed:
16963 1. Find the last row in the current matrix of W that is not
16964 affected by changes at the start of current_buffer. If no such row
16965 is found, give up.
16967 2. Find the first row in W's current matrix that is not affected by
16968 changes at the end of current_buffer. Maybe there is no such row.
16970 3. Display lines beginning with the row + 1 found in step 1 to the
16971 row found in step 2 or, if step 2 didn't find a row, to the end of
16972 the window.
16974 4. If cursor is not known to appear on the window, give up.
16976 5. If display stopped at the row found in step 2, scroll the
16977 display and current matrix as needed.
16979 6. Maybe display some lines at the end of W, if we must. This can
16980 happen under various circumstances, like a partially visible line
16981 becoming fully visible, or because newly displayed lines are displayed
16982 in smaller font sizes.
16984 7. Update W's window end information. */
16986 static int
16987 try_window_id (struct window *w)
16989 struct frame *f = XFRAME (w->frame);
16990 struct glyph_matrix *current_matrix = w->current_matrix;
16991 struct glyph_matrix *desired_matrix = w->desired_matrix;
16992 struct glyph_row *last_unchanged_at_beg_row;
16993 struct glyph_row *first_unchanged_at_end_row;
16994 struct glyph_row *row;
16995 struct glyph_row *bottom_row;
16996 int bottom_vpos;
16997 struct it it;
16998 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
16999 int dvpos, dy;
17000 struct text_pos start_pos;
17001 struct run run;
17002 int first_unchanged_at_end_vpos = 0;
17003 struct glyph_row *last_text_row, *last_text_row_at_end;
17004 struct text_pos start;
17005 ptrdiff_t first_changed_charpos, last_changed_charpos;
17007 #ifdef GLYPH_DEBUG
17008 if (inhibit_try_window_id)
17009 return 0;
17010 #endif
17012 /* This is handy for debugging. */
17013 #if 0
17014 #define GIVE_UP(X) \
17015 do { \
17016 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17017 return 0; \
17018 } while (0)
17019 #else
17020 #define GIVE_UP(X) return 0
17021 #endif
17023 SET_TEXT_POS_FROM_MARKER (start, w->start);
17025 /* Don't use this for mini-windows because these can show
17026 messages and mini-buffers, and we don't handle that here. */
17027 if (MINI_WINDOW_P (w))
17028 GIVE_UP (1);
17030 /* This flag is used to prevent redisplay optimizations. */
17031 if (windows_or_buffers_changed || cursor_type_changed)
17032 GIVE_UP (2);
17034 /* Verify that narrowing has not changed.
17035 Also verify that we were not told to prevent redisplay optimizations.
17036 It would be nice to further
17037 reduce the number of cases where this prevents try_window_id. */
17038 if (current_buffer->clip_changed
17039 || current_buffer->prevent_redisplay_optimizations_p)
17040 GIVE_UP (3);
17042 /* Window must either use window-based redisplay or be full width. */
17043 if (!FRAME_WINDOW_P (f)
17044 && (!FRAME_LINE_INS_DEL_OK (f)
17045 || !WINDOW_FULL_WIDTH_P (w)))
17046 GIVE_UP (4);
17048 /* Give up if point is known NOT to appear in W. */
17049 if (PT < CHARPOS (start))
17050 GIVE_UP (5);
17052 /* Another way to prevent redisplay optimizations. */
17053 if (w->last_modified == 0)
17054 GIVE_UP (6);
17056 /* Verify that window is not hscrolled. */
17057 if (w->hscroll != 0)
17058 GIVE_UP (7);
17060 /* Verify that display wasn't paused. */
17061 if (!w->window_end_valid)
17062 GIVE_UP (8);
17064 /* Can't use this if highlighting a region because a cursor movement
17065 will do more than just set the cursor. */
17066 if (markpos_of_region () >= 0)
17067 GIVE_UP (9);
17069 /* Likewise if highlighting trailing whitespace. */
17070 if (!NILP (Vshow_trailing_whitespace))
17071 GIVE_UP (11);
17073 /* Likewise if showing a region. */
17074 if (w->region_showing)
17075 GIVE_UP (10);
17077 /* Can't use this if overlay arrow position and/or string have
17078 changed. */
17079 if (overlay_arrows_changed_p ())
17080 GIVE_UP (12);
17082 /* When word-wrap is on, adding a space to the first word of a
17083 wrapped line can change the wrap position, altering the line
17084 above it. It might be worthwhile to handle this more
17085 intelligently, but for now just redisplay from scratch. */
17086 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17087 GIVE_UP (21);
17089 /* Under bidi reordering, adding or deleting a character in the
17090 beginning of a paragraph, before the first strong directional
17091 character, can change the base direction of the paragraph (unless
17092 the buffer specifies a fixed paragraph direction), which will
17093 require to redisplay the whole paragraph. It might be worthwhile
17094 to find the paragraph limits and widen the range of redisplayed
17095 lines to that, but for now just give up this optimization and
17096 redisplay from scratch. */
17097 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17098 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17099 GIVE_UP (22);
17101 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17102 only if buffer has really changed. The reason is that the gap is
17103 initially at Z for freshly visited files. The code below would
17104 set end_unchanged to 0 in that case. */
17105 if (MODIFF > SAVE_MODIFF
17106 /* This seems to happen sometimes after saving a buffer. */
17107 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17109 if (GPT - BEG < BEG_UNCHANGED)
17110 BEG_UNCHANGED = GPT - BEG;
17111 if (Z - GPT < END_UNCHANGED)
17112 END_UNCHANGED = Z - GPT;
17115 /* The position of the first and last character that has been changed. */
17116 first_changed_charpos = BEG + BEG_UNCHANGED;
17117 last_changed_charpos = Z - END_UNCHANGED;
17119 /* If window starts after a line end, and the last change is in
17120 front of that newline, then changes don't affect the display.
17121 This case happens with stealth-fontification. Note that although
17122 the display is unchanged, glyph positions in the matrix have to
17123 be adjusted, of course. */
17124 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17125 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17126 && ((last_changed_charpos < CHARPOS (start)
17127 && CHARPOS (start) == BEGV)
17128 || (last_changed_charpos < CHARPOS (start) - 1
17129 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17131 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17132 struct glyph_row *r0;
17134 /* Compute how many chars/bytes have been added to or removed
17135 from the buffer. */
17136 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17137 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17138 Z_delta = Z - Z_old;
17139 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17141 /* Give up if PT is not in the window. Note that it already has
17142 been checked at the start of try_window_id that PT is not in
17143 front of the window start. */
17144 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17145 GIVE_UP (13);
17147 /* If window start is unchanged, we can reuse the whole matrix
17148 as is, after adjusting glyph positions. No need to compute
17149 the window end again, since its offset from Z hasn't changed. */
17150 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17151 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17152 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17153 /* PT must not be in a partially visible line. */
17154 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17155 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17157 /* Adjust positions in the glyph matrix. */
17158 if (Z_delta || Z_delta_bytes)
17160 struct glyph_row *r1
17161 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17162 increment_matrix_positions (w->current_matrix,
17163 MATRIX_ROW_VPOS (r0, current_matrix),
17164 MATRIX_ROW_VPOS (r1, current_matrix),
17165 Z_delta, Z_delta_bytes);
17168 /* Set the cursor. */
17169 row = row_containing_pos (w, PT, r0, NULL, 0);
17170 if (row)
17171 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17172 else
17173 emacs_abort ();
17174 return 1;
17178 /* Handle the case that changes are all below what is displayed in
17179 the window, and that PT is in the window. This shortcut cannot
17180 be taken if ZV is visible in the window, and text has been added
17181 there that is visible in the window. */
17182 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17183 /* ZV is not visible in the window, or there are no
17184 changes at ZV, actually. */
17185 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17186 || first_changed_charpos == last_changed_charpos))
17188 struct glyph_row *r0;
17190 /* Give up if PT is not in the window. Note that it already has
17191 been checked at the start of try_window_id that PT is not in
17192 front of the window start. */
17193 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17194 GIVE_UP (14);
17196 /* If window start is unchanged, we can reuse the whole matrix
17197 as is, without changing glyph positions since no text has
17198 been added/removed in front of the window end. */
17199 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17200 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17201 /* PT must not be in a partially visible line. */
17202 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17203 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17205 /* We have to compute the window end anew since text
17206 could have been added/removed after it. */
17207 wset_window_end_pos
17208 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17209 w->window_end_bytepos
17210 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17212 /* Set the cursor. */
17213 row = row_containing_pos (w, PT, r0, NULL, 0);
17214 if (row)
17215 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17216 else
17217 emacs_abort ();
17218 return 2;
17222 /* Give up if window start is in the changed area.
17224 The condition used to read
17226 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17228 but why that was tested escapes me at the moment. */
17229 if (CHARPOS (start) >= first_changed_charpos
17230 && CHARPOS (start) <= last_changed_charpos)
17231 GIVE_UP (15);
17233 /* Check that window start agrees with the start of the first glyph
17234 row in its current matrix. Check this after we know the window
17235 start is not in changed text, otherwise positions would not be
17236 comparable. */
17237 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17238 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17239 GIVE_UP (16);
17241 /* Give up if the window ends in strings. Overlay strings
17242 at the end are difficult to handle, so don't try. */
17243 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17244 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17245 GIVE_UP (20);
17247 /* Compute the position at which we have to start displaying new
17248 lines. Some of the lines at the top of the window might be
17249 reusable because they are not displaying changed text. Find the
17250 last row in W's current matrix not affected by changes at the
17251 start of current_buffer. Value is null if changes start in the
17252 first line of window. */
17253 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17254 if (last_unchanged_at_beg_row)
17256 /* Avoid starting to display in the middle of a character, a TAB
17257 for instance. This is easier than to set up the iterator
17258 exactly, and it's not a frequent case, so the additional
17259 effort wouldn't really pay off. */
17260 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17261 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17262 && last_unchanged_at_beg_row > w->current_matrix->rows)
17263 --last_unchanged_at_beg_row;
17265 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17266 GIVE_UP (17);
17268 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17269 GIVE_UP (18);
17270 start_pos = it.current.pos;
17272 /* Start displaying new lines in the desired matrix at the same
17273 vpos we would use in the current matrix, i.e. below
17274 last_unchanged_at_beg_row. */
17275 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17276 current_matrix);
17277 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17278 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17280 eassert (it.hpos == 0 && it.current_x == 0);
17282 else
17284 /* There are no reusable lines at the start of the window.
17285 Start displaying in the first text line. */
17286 start_display (&it, w, start);
17287 it.vpos = it.first_vpos;
17288 start_pos = it.current.pos;
17291 /* Find the first row that is not affected by changes at the end of
17292 the buffer. Value will be null if there is no unchanged row, in
17293 which case we must redisplay to the end of the window. delta
17294 will be set to the value by which buffer positions beginning with
17295 first_unchanged_at_end_row have to be adjusted due to text
17296 changes. */
17297 first_unchanged_at_end_row
17298 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17299 IF_DEBUG (debug_delta = delta);
17300 IF_DEBUG (debug_delta_bytes = delta_bytes);
17302 /* Set stop_pos to the buffer position up to which we will have to
17303 display new lines. If first_unchanged_at_end_row != NULL, this
17304 is the buffer position of the start of the line displayed in that
17305 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17306 that we don't stop at a buffer position. */
17307 stop_pos = 0;
17308 if (first_unchanged_at_end_row)
17310 eassert (last_unchanged_at_beg_row == NULL
17311 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17313 /* If this is a continuation line, move forward to the next one
17314 that isn't. Changes in lines above affect this line.
17315 Caution: this may move first_unchanged_at_end_row to a row
17316 not displaying text. */
17317 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17318 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17319 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17320 < it.last_visible_y))
17321 ++first_unchanged_at_end_row;
17323 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17324 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17325 >= it.last_visible_y))
17326 first_unchanged_at_end_row = NULL;
17327 else
17329 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17330 + delta);
17331 first_unchanged_at_end_vpos
17332 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17333 eassert (stop_pos >= Z - END_UNCHANGED);
17336 else if (last_unchanged_at_beg_row == NULL)
17337 GIVE_UP (19);
17340 #ifdef GLYPH_DEBUG
17342 /* Either there is no unchanged row at the end, or the one we have
17343 now displays text. This is a necessary condition for the window
17344 end pos calculation at the end of this function. */
17345 eassert (first_unchanged_at_end_row == NULL
17346 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17348 debug_last_unchanged_at_beg_vpos
17349 = (last_unchanged_at_beg_row
17350 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17351 : -1);
17352 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17354 #endif /* GLYPH_DEBUG */
17357 /* Display new lines. Set last_text_row to the last new line
17358 displayed which has text on it, i.e. might end up as being the
17359 line where the window_end_vpos is. */
17360 w->cursor.vpos = -1;
17361 last_text_row = NULL;
17362 overlay_arrow_seen = 0;
17363 while (it.current_y < it.last_visible_y
17364 && !fonts_changed_p
17365 && (first_unchanged_at_end_row == NULL
17366 || IT_CHARPOS (it) < stop_pos))
17368 if (display_line (&it))
17369 last_text_row = it.glyph_row - 1;
17372 if (fonts_changed_p)
17373 return -1;
17376 /* Compute differences in buffer positions, y-positions etc. for
17377 lines reused at the bottom of the window. Compute what we can
17378 scroll. */
17379 if (first_unchanged_at_end_row
17380 /* No lines reused because we displayed everything up to the
17381 bottom of the window. */
17382 && it.current_y < it.last_visible_y)
17384 dvpos = (it.vpos
17385 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17386 current_matrix));
17387 dy = it.current_y - first_unchanged_at_end_row->y;
17388 run.current_y = first_unchanged_at_end_row->y;
17389 run.desired_y = run.current_y + dy;
17390 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17392 else
17394 delta = delta_bytes = dvpos = dy
17395 = run.current_y = run.desired_y = run.height = 0;
17396 first_unchanged_at_end_row = NULL;
17398 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17401 /* Find the cursor if not already found. We have to decide whether
17402 PT will appear on this window (it sometimes doesn't, but this is
17403 not a very frequent case.) This decision has to be made before
17404 the current matrix is altered. A value of cursor.vpos < 0 means
17405 that PT is either in one of the lines beginning at
17406 first_unchanged_at_end_row or below the window. Don't care for
17407 lines that might be displayed later at the window end; as
17408 mentioned, this is not a frequent case. */
17409 if (w->cursor.vpos < 0)
17411 /* Cursor in unchanged rows at the top? */
17412 if (PT < CHARPOS (start_pos)
17413 && last_unchanged_at_beg_row)
17415 row = row_containing_pos (w, PT,
17416 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17417 last_unchanged_at_beg_row + 1, 0);
17418 if (row)
17419 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17422 /* Start from first_unchanged_at_end_row looking for PT. */
17423 else if (first_unchanged_at_end_row)
17425 row = row_containing_pos (w, PT - delta,
17426 first_unchanged_at_end_row, NULL, 0);
17427 if (row)
17428 set_cursor_from_row (w, row, w->current_matrix, delta,
17429 delta_bytes, dy, dvpos);
17432 /* Give up if cursor was not found. */
17433 if (w->cursor.vpos < 0)
17435 clear_glyph_matrix (w->desired_matrix);
17436 return -1;
17440 /* Don't let the cursor end in the scroll margins. */
17442 int this_scroll_margin, cursor_height;
17444 this_scroll_margin =
17445 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17446 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17447 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17449 if ((w->cursor.y < this_scroll_margin
17450 && CHARPOS (start) > BEGV)
17451 /* Old redisplay didn't take scroll margin into account at the bottom,
17452 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17453 || (w->cursor.y + (make_cursor_line_fully_visible_p
17454 ? cursor_height + this_scroll_margin
17455 : 1)) > it.last_visible_y)
17457 w->cursor.vpos = -1;
17458 clear_glyph_matrix (w->desired_matrix);
17459 return -1;
17463 /* Scroll the display. Do it before changing the current matrix so
17464 that xterm.c doesn't get confused about where the cursor glyph is
17465 found. */
17466 if (dy && run.height)
17468 update_begin (f);
17470 if (FRAME_WINDOW_P (f))
17472 FRAME_RIF (f)->update_window_begin_hook (w);
17473 FRAME_RIF (f)->clear_window_mouse_face (w);
17474 FRAME_RIF (f)->scroll_run_hook (w, &run);
17475 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17477 else
17479 /* Terminal frame. In this case, dvpos gives the number of
17480 lines to scroll by; dvpos < 0 means scroll up. */
17481 int from_vpos
17482 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17483 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17484 int end = (WINDOW_TOP_EDGE_LINE (w)
17485 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17486 + window_internal_height (w));
17488 #if defined (HAVE_GPM) || defined (MSDOS)
17489 x_clear_window_mouse_face (w);
17490 #endif
17491 /* Perform the operation on the screen. */
17492 if (dvpos > 0)
17494 /* Scroll last_unchanged_at_beg_row to the end of the
17495 window down dvpos lines. */
17496 set_terminal_window (f, end);
17498 /* On dumb terminals delete dvpos lines at the end
17499 before inserting dvpos empty lines. */
17500 if (!FRAME_SCROLL_REGION_OK (f))
17501 ins_del_lines (f, end - dvpos, -dvpos);
17503 /* Insert dvpos empty lines in front of
17504 last_unchanged_at_beg_row. */
17505 ins_del_lines (f, from, dvpos);
17507 else if (dvpos < 0)
17509 /* Scroll up last_unchanged_at_beg_vpos to the end of
17510 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17511 set_terminal_window (f, end);
17513 /* Delete dvpos lines in front of
17514 last_unchanged_at_beg_vpos. ins_del_lines will set
17515 the cursor to the given vpos and emit |dvpos| delete
17516 line sequences. */
17517 ins_del_lines (f, from + dvpos, dvpos);
17519 /* On a dumb terminal insert dvpos empty lines at the
17520 end. */
17521 if (!FRAME_SCROLL_REGION_OK (f))
17522 ins_del_lines (f, end + dvpos, -dvpos);
17525 set_terminal_window (f, 0);
17528 update_end (f);
17531 /* Shift reused rows of the current matrix to the right position.
17532 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17533 text. */
17534 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17535 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17536 if (dvpos < 0)
17538 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17539 bottom_vpos, dvpos);
17540 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17541 bottom_vpos);
17543 else if (dvpos > 0)
17545 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17546 bottom_vpos, dvpos);
17547 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17548 first_unchanged_at_end_vpos + dvpos);
17551 /* For frame-based redisplay, make sure that current frame and window
17552 matrix are in sync with respect to glyph memory. */
17553 if (!FRAME_WINDOW_P (f))
17554 sync_frame_with_window_matrix_rows (w);
17556 /* Adjust buffer positions in reused rows. */
17557 if (delta || delta_bytes)
17558 increment_matrix_positions (current_matrix,
17559 first_unchanged_at_end_vpos + dvpos,
17560 bottom_vpos, delta, delta_bytes);
17562 /* Adjust Y positions. */
17563 if (dy)
17564 shift_glyph_matrix (w, current_matrix,
17565 first_unchanged_at_end_vpos + dvpos,
17566 bottom_vpos, dy);
17568 if (first_unchanged_at_end_row)
17570 first_unchanged_at_end_row += dvpos;
17571 if (first_unchanged_at_end_row->y >= it.last_visible_y
17572 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17573 first_unchanged_at_end_row = NULL;
17576 /* If scrolling up, there may be some lines to display at the end of
17577 the window. */
17578 last_text_row_at_end = NULL;
17579 if (dy < 0)
17581 /* Scrolling up can leave for example a partially visible line
17582 at the end of the window to be redisplayed. */
17583 /* Set last_row to the glyph row in the current matrix where the
17584 window end line is found. It has been moved up or down in
17585 the matrix by dvpos. */
17586 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17587 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17589 /* If last_row is the window end line, it should display text. */
17590 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17592 /* If window end line was partially visible before, begin
17593 displaying at that line. Otherwise begin displaying with the
17594 line following it. */
17595 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17597 init_to_row_start (&it, w, last_row);
17598 it.vpos = last_vpos;
17599 it.current_y = last_row->y;
17601 else
17603 init_to_row_end (&it, w, last_row);
17604 it.vpos = 1 + last_vpos;
17605 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17606 ++last_row;
17609 /* We may start in a continuation line. If so, we have to
17610 get the right continuation_lines_width and current_x. */
17611 it.continuation_lines_width = last_row->continuation_lines_width;
17612 it.hpos = it.current_x = 0;
17614 /* Display the rest of the lines at the window end. */
17615 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17616 while (it.current_y < it.last_visible_y
17617 && !fonts_changed_p)
17619 /* Is it always sure that the display agrees with lines in
17620 the current matrix? I don't think so, so we mark rows
17621 displayed invalid in the current matrix by setting their
17622 enabled_p flag to zero. */
17623 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17624 if (display_line (&it))
17625 last_text_row_at_end = it.glyph_row - 1;
17629 /* Update window_end_pos and window_end_vpos. */
17630 if (first_unchanged_at_end_row
17631 && !last_text_row_at_end)
17633 /* Window end line if one of the preserved rows from the current
17634 matrix. Set row to the last row displaying text in current
17635 matrix starting at first_unchanged_at_end_row, after
17636 scrolling. */
17637 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17638 row = find_last_row_displaying_text (w->current_matrix, &it,
17639 first_unchanged_at_end_row);
17640 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17642 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17643 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17644 wset_window_end_vpos
17645 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17646 eassert (w->window_end_bytepos >= 0);
17647 IF_DEBUG (debug_method_add (w, "A"));
17649 else if (last_text_row_at_end)
17651 wset_window_end_pos
17652 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17653 w->window_end_bytepos
17654 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17655 wset_window_end_vpos
17656 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17657 desired_matrix)));
17658 eassert (w->window_end_bytepos >= 0);
17659 IF_DEBUG (debug_method_add (w, "B"));
17661 else if (last_text_row)
17663 /* We have displayed either to the end of the window or at the
17664 end of the window, i.e. the last row with text is to be found
17665 in the desired matrix. */
17666 wset_window_end_pos
17667 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17668 w->window_end_bytepos
17669 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17670 wset_window_end_vpos
17671 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17672 eassert (w->window_end_bytepos >= 0);
17674 else if (first_unchanged_at_end_row == NULL
17675 && last_text_row == NULL
17676 && last_text_row_at_end == NULL)
17678 /* Displayed to end of window, but no line containing text was
17679 displayed. Lines were deleted at the end of the window. */
17680 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17681 int vpos = XFASTINT (w->window_end_vpos);
17682 struct glyph_row *current_row = current_matrix->rows + vpos;
17683 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17685 for (row = NULL;
17686 row == NULL && vpos >= first_vpos;
17687 --vpos, --current_row, --desired_row)
17689 if (desired_row->enabled_p)
17691 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17692 row = desired_row;
17694 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17695 row = current_row;
17698 eassert (row != NULL);
17699 wset_window_end_vpos (w, make_number (vpos + 1));
17700 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17701 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17702 eassert (w->window_end_bytepos >= 0);
17703 IF_DEBUG (debug_method_add (w, "C"));
17705 else
17706 emacs_abort ();
17708 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17709 debug_end_vpos = XFASTINT (w->window_end_vpos));
17711 /* Record that display has not been completed. */
17712 w->window_end_valid = 0;
17713 w->desired_matrix->no_scrolling_p = 1;
17714 return 3;
17716 #undef GIVE_UP
17721 /***********************************************************************
17722 More debugging support
17723 ***********************************************************************/
17725 #ifdef GLYPH_DEBUG
17727 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17728 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17729 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17732 /* Dump the contents of glyph matrix MATRIX on stderr.
17734 GLYPHS 0 means don't show glyph contents.
17735 GLYPHS 1 means show glyphs in short form
17736 GLYPHS > 1 means show glyphs in long form. */
17738 void
17739 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17741 int i;
17742 for (i = 0; i < matrix->nrows; ++i)
17743 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17747 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17748 the glyph row and area where the glyph comes from. */
17750 void
17751 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17753 if (glyph->type == CHAR_GLYPH
17754 || glyph->type == GLYPHLESS_GLYPH)
17756 fprintf (stderr,
17757 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17758 glyph - row->glyphs[TEXT_AREA],
17759 (glyph->type == CHAR_GLYPH
17760 ? 'C'
17761 : 'G'),
17762 glyph->charpos,
17763 (BUFFERP (glyph->object)
17764 ? 'B'
17765 : (STRINGP (glyph->object)
17766 ? 'S'
17767 : (INTEGERP (glyph->object)
17768 ? '0'
17769 : '-'))),
17770 glyph->pixel_width,
17771 glyph->u.ch,
17772 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17773 ? glyph->u.ch
17774 : '.'),
17775 glyph->face_id,
17776 glyph->left_box_line_p,
17777 glyph->right_box_line_p);
17779 else if (glyph->type == STRETCH_GLYPH)
17781 fprintf (stderr,
17782 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17783 glyph - row->glyphs[TEXT_AREA],
17784 'S',
17785 glyph->charpos,
17786 (BUFFERP (glyph->object)
17787 ? 'B'
17788 : (STRINGP (glyph->object)
17789 ? 'S'
17790 : (INTEGERP (glyph->object)
17791 ? '0'
17792 : '-'))),
17793 glyph->pixel_width,
17795 ' ',
17796 glyph->face_id,
17797 glyph->left_box_line_p,
17798 glyph->right_box_line_p);
17800 else if (glyph->type == IMAGE_GLYPH)
17802 fprintf (stderr,
17803 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17804 glyph - row->glyphs[TEXT_AREA],
17805 'I',
17806 glyph->charpos,
17807 (BUFFERP (glyph->object)
17808 ? 'B'
17809 : (STRINGP (glyph->object)
17810 ? 'S'
17811 : (INTEGERP (glyph->object)
17812 ? '0'
17813 : '-'))),
17814 glyph->pixel_width,
17815 glyph->u.img_id,
17816 '.',
17817 glyph->face_id,
17818 glyph->left_box_line_p,
17819 glyph->right_box_line_p);
17821 else if (glyph->type == COMPOSITE_GLYPH)
17823 fprintf (stderr,
17824 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17825 glyph - row->glyphs[TEXT_AREA],
17826 '+',
17827 glyph->charpos,
17828 (BUFFERP (glyph->object)
17829 ? 'B'
17830 : (STRINGP (glyph->object)
17831 ? 'S'
17832 : (INTEGERP (glyph->object)
17833 ? '0'
17834 : '-'))),
17835 glyph->pixel_width,
17836 glyph->u.cmp.id);
17837 if (glyph->u.cmp.automatic)
17838 fprintf (stderr,
17839 "[%d-%d]",
17840 glyph->slice.cmp.from, glyph->slice.cmp.to);
17841 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17842 glyph->face_id,
17843 glyph->left_box_line_p,
17844 glyph->right_box_line_p);
17849 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17850 GLYPHS 0 means don't show glyph contents.
17851 GLYPHS 1 means show glyphs in short form
17852 GLYPHS > 1 means show glyphs in long form. */
17854 void
17855 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17857 if (glyphs != 1)
17859 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17860 fprintf (stderr, "==============================================================================\n");
17862 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17863 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17864 vpos,
17865 MATRIX_ROW_START_CHARPOS (row),
17866 MATRIX_ROW_END_CHARPOS (row),
17867 row->used[TEXT_AREA],
17868 row->contains_overlapping_glyphs_p,
17869 row->enabled_p,
17870 row->truncated_on_left_p,
17871 row->truncated_on_right_p,
17872 row->continued_p,
17873 MATRIX_ROW_CONTINUATION_LINE_P (row),
17874 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17875 row->ends_at_zv_p,
17876 row->fill_line_p,
17877 row->ends_in_middle_of_char_p,
17878 row->starts_in_middle_of_char_p,
17879 row->mouse_face_p,
17880 row->x,
17881 row->y,
17882 row->pixel_width,
17883 row->height,
17884 row->visible_height,
17885 row->ascent,
17886 row->phys_ascent);
17887 /* The next 3 lines should align to "Start" in the header. */
17888 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17889 row->end.overlay_string_index,
17890 row->continuation_lines_width);
17891 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17892 CHARPOS (row->start.string_pos),
17893 CHARPOS (row->end.string_pos));
17894 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17895 row->end.dpvec_index);
17898 if (glyphs > 1)
17900 int area;
17902 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17904 struct glyph *glyph = row->glyphs[area];
17905 struct glyph *glyph_end = glyph + row->used[area];
17907 /* Glyph for a line end in text. */
17908 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17909 ++glyph_end;
17911 if (glyph < glyph_end)
17912 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17914 for (; glyph < glyph_end; ++glyph)
17915 dump_glyph (row, glyph, area);
17918 else if (glyphs == 1)
17920 int area;
17922 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17924 char *s = alloca (row->used[area] + 4);
17925 int i;
17927 for (i = 0; i < row->used[area]; ++i)
17929 struct glyph *glyph = row->glyphs[area] + i;
17930 if (i == row->used[area] - 1
17931 && area == TEXT_AREA
17932 && INTEGERP (glyph->object)
17933 && glyph->type == CHAR_GLYPH
17934 && glyph->u.ch == ' ')
17936 strcpy (&s[i], "[\\n]");
17937 i += 4;
17939 else if (glyph->type == CHAR_GLYPH
17940 && glyph->u.ch < 0x80
17941 && glyph->u.ch >= ' ')
17942 s[i] = glyph->u.ch;
17943 else
17944 s[i] = '.';
17947 s[i] = '\0';
17948 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17954 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17955 Sdump_glyph_matrix, 0, 1, "p",
17956 doc: /* Dump the current matrix of the selected window to stderr.
17957 Shows contents of glyph row structures. With non-nil
17958 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17959 glyphs in short form, otherwise show glyphs in long form. */)
17960 (Lisp_Object glyphs)
17962 struct window *w = XWINDOW (selected_window);
17963 struct buffer *buffer = XBUFFER (w->contents);
17965 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17966 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17967 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17968 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17969 fprintf (stderr, "=============================================\n");
17970 dump_glyph_matrix (w->current_matrix,
17971 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
17972 return Qnil;
17976 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17977 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17978 (void)
17980 struct frame *f = XFRAME (selected_frame);
17981 dump_glyph_matrix (f->current_matrix, 1);
17982 return Qnil;
17986 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17987 doc: /* Dump glyph row ROW to stderr.
17988 GLYPH 0 means don't dump glyphs.
17989 GLYPH 1 means dump glyphs in short form.
17990 GLYPH > 1 or omitted means dump glyphs in long form. */)
17991 (Lisp_Object row, Lisp_Object glyphs)
17993 struct glyph_matrix *matrix;
17994 EMACS_INT vpos;
17996 CHECK_NUMBER (row);
17997 matrix = XWINDOW (selected_window)->current_matrix;
17998 vpos = XINT (row);
17999 if (vpos >= 0 && vpos < matrix->nrows)
18000 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18001 vpos,
18002 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18003 return Qnil;
18007 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18008 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18009 GLYPH 0 means don't dump glyphs.
18010 GLYPH 1 means dump glyphs in short form.
18011 GLYPH > 1 or omitted means dump glyphs in long form. */)
18012 (Lisp_Object row, Lisp_Object glyphs)
18014 struct frame *sf = SELECTED_FRAME ();
18015 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18016 EMACS_INT vpos;
18018 CHECK_NUMBER (row);
18019 vpos = XINT (row);
18020 if (vpos >= 0 && vpos < m->nrows)
18021 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18022 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18023 return Qnil;
18027 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18028 doc: /* Toggle tracing of redisplay.
18029 With ARG, turn tracing on if and only if ARG is positive. */)
18030 (Lisp_Object arg)
18032 if (NILP (arg))
18033 trace_redisplay_p = !trace_redisplay_p;
18034 else
18036 arg = Fprefix_numeric_value (arg);
18037 trace_redisplay_p = XINT (arg) > 0;
18040 return Qnil;
18044 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18045 doc: /* Like `format', but print result to stderr.
18046 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18047 (ptrdiff_t nargs, Lisp_Object *args)
18049 Lisp_Object s = Fformat (nargs, args);
18050 fprintf (stderr, "%s", SDATA (s));
18051 return Qnil;
18054 #endif /* GLYPH_DEBUG */
18058 /***********************************************************************
18059 Building Desired Matrix Rows
18060 ***********************************************************************/
18062 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18063 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18065 static struct glyph_row *
18066 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18068 struct frame *f = XFRAME (WINDOW_FRAME (w));
18069 struct buffer *buffer = XBUFFER (w->contents);
18070 struct buffer *old = current_buffer;
18071 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18072 int arrow_len = SCHARS (overlay_arrow_string);
18073 const unsigned char *arrow_end = arrow_string + arrow_len;
18074 const unsigned char *p;
18075 struct it it;
18076 bool multibyte_p;
18077 int n_glyphs_before;
18079 set_buffer_temp (buffer);
18080 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18081 it.glyph_row->used[TEXT_AREA] = 0;
18082 SET_TEXT_POS (it.position, 0, 0);
18084 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18085 p = arrow_string;
18086 while (p < arrow_end)
18088 Lisp_Object face, ilisp;
18090 /* Get the next character. */
18091 if (multibyte_p)
18092 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18093 else
18095 it.c = it.char_to_display = *p, it.len = 1;
18096 if (! ASCII_CHAR_P (it.c))
18097 it.char_to_display = BYTE8_TO_CHAR (it.c);
18099 p += it.len;
18101 /* Get its face. */
18102 ilisp = make_number (p - arrow_string);
18103 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18104 it.face_id = compute_char_face (f, it.char_to_display, face);
18106 /* Compute its width, get its glyphs. */
18107 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18108 SET_TEXT_POS (it.position, -1, -1);
18109 PRODUCE_GLYPHS (&it);
18111 /* If this character doesn't fit any more in the line, we have
18112 to remove some glyphs. */
18113 if (it.current_x > it.last_visible_x)
18115 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18116 break;
18120 set_buffer_temp (old);
18121 return it.glyph_row;
18125 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18126 glyphs to insert is determined by produce_special_glyphs. */
18128 static void
18129 insert_left_trunc_glyphs (struct it *it)
18131 struct it truncate_it;
18132 struct glyph *from, *end, *to, *toend;
18134 eassert (!FRAME_WINDOW_P (it->f)
18135 || (!it->glyph_row->reversed_p
18136 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18137 || (it->glyph_row->reversed_p
18138 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18140 /* Get the truncation glyphs. */
18141 truncate_it = *it;
18142 truncate_it.current_x = 0;
18143 truncate_it.face_id = DEFAULT_FACE_ID;
18144 truncate_it.glyph_row = &scratch_glyph_row;
18145 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18146 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18147 truncate_it.object = make_number (0);
18148 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18150 /* Overwrite glyphs from IT with truncation glyphs. */
18151 if (!it->glyph_row->reversed_p)
18153 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18155 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18156 end = from + tused;
18157 to = it->glyph_row->glyphs[TEXT_AREA];
18158 toend = to + it->glyph_row->used[TEXT_AREA];
18159 if (FRAME_WINDOW_P (it->f))
18161 /* On GUI frames, when variable-size fonts are displayed,
18162 the truncation glyphs may need more pixels than the row's
18163 glyphs they overwrite. We overwrite more glyphs to free
18164 enough screen real estate, and enlarge the stretch glyph
18165 on the right (see display_line), if there is one, to
18166 preserve the screen position of the truncation glyphs on
18167 the right. */
18168 int w = 0;
18169 struct glyph *g = to;
18170 short used;
18172 /* The first glyph could be partially visible, in which case
18173 it->glyph_row->x will be negative. But we want the left
18174 truncation glyphs to be aligned at the left margin of the
18175 window, so we override the x coordinate at which the row
18176 will begin. */
18177 it->glyph_row->x = 0;
18178 while (g < toend && w < it->truncation_pixel_width)
18180 w += g->pixel_width;
18181 ++g;
18183 if (g - to - tused > 0)
18185 memmove (to + tused, g, (toend - g) * sizeof(*g));
18186 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18188 used = it->glyph_row->used[TEXT_AREA];
18189 if (it->glyph_row->truncated_on_right_p
18190 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18191 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18192 == STRETCH_GLYPH)
18194 int extra = w - it->truncation_pixel_width;
18196 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18200 while (from < end)
18201 *to++ = *from++;
18203 /* There may be padding glyphs left over. Overwrite them too. */
18204 if (!FRAME_WINDOW_P (it->f))
18206 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18208 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18209 while (from < end)
18210 *to++ = *from++;
18214 if (to > toend)
18215 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18217 else
18219 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18221 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18222 that back to front. */
18223 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18224 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18225 toend = it->glyph_row->glyphs[TEXT_AREA];
18226 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18227 if (FRAME_WINDOW_P (it->f))
18229 int w = 0;
18230 struct glyph *g = to;
18232 while (g >= toend && w < it->truncation_pixel_width)
18234 w += g->pixel_width;
18235 --g;
18237 if (to - g - tused > 0)
18238 to = g + tused;
18239 if (it->glyph_row->truncated_on_right_p
18240 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18241 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18243 int extra = w - it->truncation_pixel_width;
18245 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18249 while (from >= end && to >= toend)
18250 *to-- = *from--;
18251 if (!FRAME_WINDOW_P (it->f))
18253 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18255 from =
18256 truncate_it.glyph_row->glyphs[TEXT_AREA]
18257 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18258 while (from >= end && to >= toend)
18259 *to-- = *from--;
18262 if (from >= end)
18264 /* Need to free some room before prepending additional
18265 glyphs. */
18266 int move_by = from - end + 1;
18267 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18268 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18270 for ( ; g >= g0; g--)
18271 g[move_by] = *g;
18272 while (from >= end)
18273 *to-- = *from--;
18274 it->glyph_row->used[TEXT_AREA] += move_by;
18279 /* Compute the hash code for ROW. */
18280 unsigned
18281 row_hash (struct glyph_row *row)
18283 int area, k;
18284 unsigned hashval = 0;
18286 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18287 for (k = 0; k < row->used[area]; ++k)
18288 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18289 + row->glyphs[area][k].u.val
18290 + row->glyphs[area][k].face_id
18291 + row->glyphs[area][k].padding_p
18292 + (row->glyphs[area][k].type << 2));
18294 return hashval;
18297 /* Compute the pixel height and width of IT->glyph_row.
18299 Most of the time, ascent and height of a display line will be equal
18300 to the max_ascent and max_height values of the display iterator
18301 structure. This is not the case if
18303 1. We hit ZV without displaying anything. In this case, max_ascent
18304 and max_height will be zero.
18306 2. We have some glyphs that don't contribute to the line height.
18307 (The glyph row flag contributes_to_line_height_p is for future
18308 pixmap extensions).
18310 The first case is easily covered by using default values because in
18311 these cases, the line height does not really matter, except that it
18312 must not be zero. */
18314 static void
18315 compute_line_metrics (struct it *it)
18317 struct glyph_row *row = it->glyph_row;
18319 if (FRAME_WINDOW_P (it->f))
18321 int i, min_y, max_y;
18323 /* The line may consist of one space only, that was added to
18324 place the cursor on it. If so, the row's height hasn't been
18325 computed yet. */
18326 if (row->height == 0)
18328 if (it->max_ascent + it->max_descent == 0)
18329 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18330 row->ascent = it->max_ascent;
18331 row->height = it->max_ascent + it->max_descent;
18332 row->phys_ascent = it->max_phys_ascent;
18333 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18334 row->extra_line_spacing = it->max_extra_line_spacing;
18337 /* Compute the width of this line. */
18338 row->pixel_width = row->x;
18339 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18340 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18342 eassert (row->pixel_width >= 0);
18343 eassert (row->ascent >= 0 && row->height > 0);
18345 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18346 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18348 /* If first line's physical ascent is larger than its logical
18349 ascent, use the physical ascent, and make the row taller.
18350 This makes accented characters fully visible. */
18351 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18352 && row->phys_ascent > row->ascent)
18354 row->height += row->phys_ascent - row->ascent;
18355 row->ascent = row->phys_ascent;
18358 /* Compute how much of the line is visible. */
18359 row->visible_height = row->height;
18361 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18362 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18364 if (row->y < min_y)
18365 row->visible_height -= min_y - row->y;
18366 if (row->y + row->height > max_y)
18367 row->visible_height -= row->y + row->height - max_y;
18369 else
18371 row->pixel_width = row->used[TEXT_AREA];
18372 if (row->continued_p)
18373 row->pixel_width -= it->continuation_pixel_width;
18374 else if (row->truncated_on_right_p)
18375 row->pixel_width -= it->truncation_pixel_width;
18376 row->ascent = row->phys_ascent = 0;
18377 row->height = row->phys_height = row->visible_height = 1;
18378 row->extra_line_spacing = 0;
18381 /* Compute a hash code for this row. */
18382 row->hash = row_hash (row);
18384 it->max_ascent = it->max_descent = 0;
18385 it->max_phys_ascent = it->max_phys_descent = 0;
18389 /* Append one space to the glyph row of iterator IT if doing a
18390 window-based redisplay. The space has the same face as
18391 IT->face_id. Value is non-zero if a space was added.
18393 This function is called to make sure that there is always one glyph
18394 at the end of a glyph row that the cursor can be set on under
18395 window-systems. (If there weren't such a glyph we would not know
18396 how wide and tall a box cursor should be displayed).
18398 At the same time this space let's a nicely handle clearing to the
18399 end of the line if the row ends in italic text. */
18401 static int
18402 append_space_for_newline (struct it *it, int default_face_p)
18404 if (FRAME_WINDOW_P (it->f))
18406 int n = it->glyph_row->used[TEXT_AREA];
18408 if (it->glyph_row->glyphs[TEXT_AREA] + n
18409 < it->glyph_row->glyphs[1 + TEXT_AREA])
18411 /* Save some values that must not be changed.
18412 Must save IT->c and IT->len because otherwise
18413 ITERATOR_AT_END_P wouldn't work anymore after
18414 append_space_for_newline has been called. */
18415 enum display_element_type saved_what = it->what;
18416 int saved_c = it->c, saved_len = it->len;
18417 int saved_char_to_display = it->char_to_display;
18418 int saved_x = it->current_x;
18419 int saved_face_id = it->face_id;
18420 int saved_box_end = it->end_of_box_run_p;
18421 struct text_pos saved_pos;
18422 Lisp_Object saved_object;
18423 struct face *face;
18425 saved_object = it->object;
18426 saved_pos = it->position;
18428 it->what = IT_CHARACTER;
18429 memset (&it->position, 0, sizeof it->position);
18430 it->object = make_number (0);
18431 it->c = it->char_to_display = ' ';
18432 it->len = 1;
18434 /* If the default face was remapped, be sure to use the
18435 remapped face for the appended newline. */
18436 if (default_face_p)
18437 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18438 else if (it->face_before_selective_p)
18439 it->face_id = it->saved_face_id;
18440 face = FACE_FROM_ID (it->f, it->face_id);
18441 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18442 /* In R2L rows, we will prepend a stretch glyph that will
18443 have the end_of_box_run_p flag set for it, so there's no
18444 need for the appended newline glyph to have that flag
18445 set. */
18446 if (it->glyph_row->reversed_p
18447 /* But if the appended newline glyph goes all the way to
18448 the end of the row, there will be no stretch glyph,
18449 so leave the box flag set. */
18450 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18451 it->end_of_box_run_p = 0;
18453 PRODUCE_GLYPHS (it);
18455 it->override_ascent = -1;
18456 it->constrain_row_ascent_descent_p = 0;
18457 it->current_x = saved_x;
18458 it->object = saved_object;
18459 it->position = saved_pos;
18460 it->what = saved_what;
18461 it->face_id = saved_face_id;
18462 it->len = saved_len;
18463 it->c = saved_c;
18464 it->char_to_display = saved_char_to_display;
18465 it->end_of_box_run_p = saved_box_end;
18466 return 1;
18470 return 0;
18474 /* Extend the face of the last glyph in the text area of IT->glyph_row
18475 to the end of the display line. Called from display_line. If the
18476 glyph row is empty, add a space glyph to it so that we know the
18477 face to draw. Set the glyph row flag fill_line_p. If the glyph
18478 row is R2L, prepend a stretch glyph to cover the empty space to the
18479 left of the leftmost glyph. */
18481 static void
18482 extend_face_to_end_of_line (struct it *it)
18484 struct face *face, *default_face;
18485 struct frame *f = it->f;
18487 /* If line is already filled, do nothing. Non window-system frames
18488 get a grace of one more ``pixel'' because their characters are
18489 1-``pixel'' wide, so they hit the equality too early. This grace
18490 is needed only for R2L rows that are not continued, to produce
18491 one extra blank where we could display the cursor. */
18492 if (it->current_x >= it->last_visible_x
18493 + (!FRAME_WINDOW_P (f)
18494 && it->glyph_row->reversed_p
18495 && !it->glyph_row->continued_p))
18496 return;
18498 /* The default face, possibly remapped. */
18499 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18501 /* Face extension extends the background and box of IT->face_id
18502 to the end of the line. If the background equals the background
18503 of the frame, we don't have to do anything. */
18504 if (it->face_before_selective_p)
18505 face = FACE_FROM_ID (f, it->saved_face_id);
18506 else
18507 face = FACE_FROM_ID (f, it->face_id);
18509 if (FRAME_WINDOW_P (f)
18510 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18511 && face->box == FACE_NO_BOX
18512 && face->background == FRAME_BACKGROUND_PIXEL (f)
18513 && !face->stipple
18514 && !it->glyph_row->reversed_p)
18515 return;
18517 /* Set the glyph row flag indicating that the face of the last glyph
18518 in the text area has to be drawn to the end of the text area. */
18519 it->glyph_row->fill_line_p = 1;
18521 /* If current character of IT is not ASCII, make sure we have the
18522 ASCII face. This will be automatically undone the next time
18523 get_next_display_element returns a multibyte character. Note
18524 that the character will always be single byte in unibyte
18525 text. */
18526 if (!ASCII_CHAR_P (it->c))
18528 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18531 if (FRAME_WINDOW_P (f))
18533 /* If the row is empty, add a space with the current face of IT,
18534 so that we know which face to draw. */
18535 if (it->glyph_row->used[TEXT_AREA] == 0)
18537 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18538 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18539 it->glyph_row->used[TEXT_AREA] = 1;
18541 #ifdef HAVE_WINDOW_SYSTEM
18542 if (it->glyph_row->reversed_p)
18544 /* Prepend a stretch glyph to the row, such that the
18545 rightmost glyph will be drawn flushed all the way to the
18546 right margin of the window. The stretch glyph that will
18547 occupy the empty space, if any, to the left of the
18548 glyphs. */
18549 struct font *font = face->font ? face->font : FRAME_FONT (f);
18550 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18551 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18552 struct glyph *g;
18553 int row_width, stretch_ascent, stretch_width;
18554 struct text_pos saved_pos;
18555 int saved_face_id, saved_avoid_cursor, saved_box_start;
18557 for (row_width = 0, g = row_start; g < row_end; g++)
18558 row_width += g->pixel_width;
18559 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18560 if (stretch_width > 0)
18562 stretch_ascent =
18563 (((it->ascent + it->descent)
18564 * FONT_BASE (font)) / FONT_HEIGHT (font));
18565 saved_pos = it->position;
18566 memset (&it->position, 0, sizeof it->position);
18567 saved_avoid_cursor = it->avoid_cursor_p;
18568 it->avoid_cursor_p = 1;
18569 saved_face_id = it->face_id;
18570 saved_box_start = it->start_of_box_run_p;
18571 /* The last row's stretch glyph should get the default
18572 face, to avoid painting the rest of the window with
18573 the region face, if the region ends at ZV. */
18574 if (it->glyph_row->ends_at_zv_p)
18575 it->face_id = default_face->id;
18576 else
18577 it->face_id = face->id;
18578 it->start_of_box_run_p = 0;
18579 append_stretch_glyph (it, make_number (0), stretch_width,
18580 it->ascent + it->descent, stretch_ascent);
18581 it->position = saved_pos;
18582 it->avoid_cursor_p = saved_avoid_cursor;
18583 it->face_id = saved_face_id;
18584 it->start_of_box_run_p = saved_box_start;
18587 #endif /* HAVE_WINDOW_SYSTEM */
18589 else
18591 /* Save some values that must not be changed. */
18592 int saved_x = it->current_x;
18593 struct text_pos saved_pos;
18594 Lisp_Object saved_object;
18595 enum display_element_type saved_what = it->what;
18596 int saved_face_id = it->face_id;
18598 saved_object = it->object;
18599 saved_pos = it->position;
18601 it->what = IT_CHARACTER;
18602 memset (&it->position, 0, sizeof it->position);
18603 it->object = make_number (0);
18604 it->c = it->char_to_display = ' ';
18605 it->len = 1;
18606 /* The last row's blank glyphs should get the default face, to
18607 avoid painting the rest of the window with the region face,
18608 if the region ends at ZV. */
18609 if (it->glyph_row->ends_at_zv_p)
18610 it->face_id = default_face->id;
18611 else
18612 it->face_id = face->id;
18614 PRODUCE_GLYPHS (it);
18616 while (it->current_x <= it->last_visible_x)
18617 PRODUCE_GLYPHS (it);
18619 /* Don't count these blanks really. It would let us insert a left
18620 truncation glyph below and make us set the cursor on them, maybe. */
18621 it->current_x = saved_x;
18622 it->object = saved_object;
18623 it->position = saved_pos;
18624 it->what = saved_what;
18625 it->face_id = saved_face_id;
18630 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18631 trailing whitespace. */
18633 static int
18634 trailing_whitespace_p (ptrdiff_t charpos)
18636 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18637 int c = 0;
18639 while (bytepos < ZV_BYTE
18640 && (c = FETCH_CHAR (bytepos),
18641 c == ' ' || c == '\t'))
18642 ++bytepos;
18644 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18646 if (bytepos != PT_BYTE)
18647 return 1;
18649 return 0;
18653 /* Highlight trailing whitespace, if any, in ROW. */
18655 static void
18656 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18658 int used = row->used[TEXT_AREA];
18660 if (used)
18662 struct glyph *start = row->glyphs[TEXT_AREA];
18663 struct glyph *glyph = start + used - 1;
18665 if (row->reversed_p)
18667 /* Right-to-left rows need to be processed in the opposite
18668 direction, so swap the edge pointers. */
18669 glyph = start;
18670 start = row->glyphs[TEXT_AREA] + used - 1;
18673 /* Skip over glyphs inserted to display the cursor at the
18674 end of a line, for extending the face of the last glyph
18675 to the end of the line on terminals, and for truncation
18676 and continuation glyphs. */
18677 if (!row->reversed_p)
18679 while (glyph >= start
18680 && glyph->type == CHAR_GLYPH
18681 && INTEGERP (glyph->object))
18682 --glyph;
18684 else
18686 while (glyph <= start
18687 && glyph->type == CHAR_GLYPH
18688 && INTEGERP (glyph->object))
18689 ++glyph;
18692 /* If last glyph is a space or stretch, and it's trailing
18693 whitespace, set the face of all trailing whitespace glyphs in
18694 IT->glyph_row to `trailing-whitespace'. */
18695 if ((row->reversed_p ? glyph <= start : glyph >= start)
18696 && BUFFERP (glyph->object)
18697 && (glyph->type == STRETCH_GLYPH
18698 || (glyph->type == CHAR_GLYPH
18699 && glyph->u.ch == ' '))
18700 && trailing_whitespace_p (glyph->charpos))
18702 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18703 if (face_id < 0)
18704 return;
18706 if (!row->reversed_p)
18708 while (glyph >= start
18709 && BUFFERP (glyph->object)
18710 && (glyph->type == STRETCH_GLYPH
18711 || (glyph->type == CHAR_GLYPH
18712 && glyph->u.ch == ' ')))
18713 (glyph--)->face_id = face_id;
18715 else
18717 while (glyph <= start
18718 && BUFFERP (glyph->object)
18719 && (glyph->type == STRETCH_GLYPH
18720 || (glyph->type == CHAR_GLYPH
18721 && glyph->u.ch == ' ')))
18722 (glyph++)->face_id = face_id;
18729 /* Value is non-zero if glyph row ROW should be
18730 used to hold the cursor. */
18732 static int
18733 cursor_row_p (struct glyph_row *row)
18735 int result = 1;
18737 if (PT == CHARPOS (row->end.pos)
18738 || PT == MATRIX_ROW_END_CHARPOS (row))
18740 /* Suppose the row ends on a string.
18741 Unless the row is continued, that means it ends on a newline
18742 in the string. If it's anything other than a display string
18743 (e.g., a before-string from an overlay), we don't want the
18744 cursor there. (This heuristic seems to give the optimal
18745 behavior for the various types of multi-line strings.)
18746 One exception: if the string has `cursor' property on one of
18747 its characters, we _do_ want the cursor there. */
18748 if (CHARPOS (row->end.string_pos) >= 0)
18750 if (row->continued_p)
18751 result = 1;
18752 else
18754 /* Check for `display' property. */
18755 struct glyph *beg = row->glyphs[TEXT_AREA];
18756 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18757 struct glyph *glyph;
18759 result = 0;
18760 for (glyph = end; glyph >= beg; --glyph)
18761 if (STRINGP (glyph->object))
18763 Lisp_Object prop
18764 = Fget_char_property (make_number (PT),
18765 Qdisplay, Qnil);
18766 result =
18767 (!NILP (prop)
18768 && display_prop_string_p (prop, glyph->object));
18769 /* If there's a `cursor' property on one of the
18770 string's characters, this row is a cursor row,
18771 even though this is not a display string. */
18772 if (!result)
18774 Lisp_Object s = glyph->object;
18776 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18778 ptrdiff_t gpos = glyph->charpos;
18780 if (!NILP (Fget_char_property (make_number (gpos),
18781 Qcursor, s)))
18783 result = 1;
18784 break;
18788 break;
18792 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18794 /* If the row ends in middle of a real character,
18795 and the line is continued, we want the cursor here.
18796 That's because CHARPOS (ROW->end.pos) would equal
18797 PT if PT is before the character. */
18798 if (!row->ends_in_ellipsis_p)
18799 result = row->continued_p;
18800 else
18801 /* If the row ends in an ellipsis, then
18802 CHARPOS (ROW->end.pos) will equal point after the
18803 invisible text. We want that position to be displayed
18804 after the ellipsis. */
18805 result = 0;
18807 /* If the row ends at ZV, display the cursor at the end of that
18808 row instead of at the start of the row below. */
18809 else if (row->ends_at_zv_p)
18810 result = 1;
18811 else
18812 result = 0;
18815 return result;
18820 /* Push the property PROP so that it will be rendered at the current
18821 position in IT. Return 1 if PROP was successfully pushed, 0
18822 otherwise. Called from handle_line_prefix to handle the
18823 `line-prefix' and `wrap-prefix' properties. */
18825 static int
18826 push_prefix_prop (struct it *it, Lisp_Object prop)
18828 struct text_pos pos =
18829 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18831 eassert (it->method == GET_FROM_BUFFER
18832 || it->method == GET_FROM_DISPLAY_VECTOR
18833 || it->method == GET_FROM_STRING);
18835 /* We need to save the current buffer/string position, so it will be
18836 restored by pop_it, because iterate_out_of_display_property
18837 depends on that being set correctly, but some situations leave
18838 it->position not yet set when this function is called. */
18839 push_it (it, &pos);
18841 if (STRINGP (prop))
18843 if (SCHARS (prop) == 0)
18845 pop_it (it);
18846 return 0;
18849 it->string = prop;
18850 it->string_from_prefix_prop_p = 1;
18851 it->multibyte_p = STRING_MULTIBYTE (it->string);
18852 it->current.overlay_string_index = -1;
18853 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18854 it->end_charpos = it->string_nchars = SCHARS (it->string);
18855 it->method = GET_FROM_STRING;
18856 it->stop_charpos = 0;
18857 it->prev_stop = 0;
18858 it->base_level_stop = 0;
18860 /* Force paragraph direction to be that of the parent
18861 buffer/string. */
18862 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18863 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18864 else
18865 it->paragraph_embedding = L2R;
18867 /* Set up the bidi iterator for this display string. */
18868 if (it->bidi_p)
18870 it->bidi_it.string.lstring = it->string;
18871 it->bidi_it.string.s = NULL;
18872 it->bidi_it.string.schars = it->end_charpos;
18873 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18874 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18875 it->bidi_it.string.unibyte = !it->multibyte_p;
18876 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18879 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18881 it->method = GET_FROM_STRETCH;
18882 it->object = prop;
18884 #ifdef HAVE_WINDOW_SYSTEM
18885 else if (IMAGEP (prop))
18887 it->what = IT_IMAGE;
18888 it->image_id = lookup_image (it->f, prop);
18889 it->method = GET_FROM_IMAGE;
18891 #endif /* HAVE_WINDOW_SYSTEM */
18892 else
18894 pop_it (it); /* bogus display property, give up */
18895 return 0;
18898 return 1;
18901 /* Return the character-property PROP at the current position in IT. */
18903 static Lisp_Object
18904 get_it_property (struct it *it, Lisp_Object prop)
18906 Lisp_Object position;
18908 if (STRINGP (it->object))
18909 position = make_number (IT_STRING_CHARPOS (*it));
18910 else if (BUFFERP (it->object))
18911 position = make_number (IT_CHARPOS (*it));
18912 else
18913 return Qnil;
18915 return Fget_char_property (position, prop, it->object);
18918 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18920 static void
18921 handle_line_prefix (struct it *it)
18923 Lisp_Object prefix;
18925 if (it->continuation_lines_width > 0)
18927 prefix = get_it_property (it, Qwrap_prefix);
18928 if (NILP (prefix))
18929 prefix = Vwrap_prefix;
18931 else
18933 prefix = get_it_property (it, Qline_prefix);
18934 if (NILP (prefix))
18935 prefix = Vline_prefix;
18937 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18939 /* If the prefix is wider than the window, and we try to wrap
18940 it, it would acquire its own wrap prefix, and so on till the
18941 iterator stack overflows. So, don't wrap the prefix. */
18942 it->line_wrap = TRUNCATE;
18943 it->avoid_cursor_p = 1;
18949 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18950 only for R2L lines from display_line and display_string, when they
18951 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18952 the line/string needs to be continued on the next glyph row. */
18953 static void
18954 unproduce_glyphs (struct it *it, int n)
18956 struct glyph *glyph, *end;
18958 eassert (it->glyph_row);
18959 eassert (it->glyph_row->reversed_p);
18960 eassert (it->area == TEXT_AREA);
18961 eassert (n <= it->glyph_row->used[TEXT_AREA]);
18963 if (n > it->glyph_row->used[TEXT_AREA])
18964 n = it->glyph_row->used[TEXT_AREA];
18965 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18966 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18967 for ( ; glyph < end; glyph++)
18968 glyph[-n] = *glyph;
18971 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18972 and ROW->maxpos. */
18973 static void
18974 find_row_edges (struct it *it, struct glyph_row *row,
18975 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18976 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18978 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18979 lines' rows is implemented for bidi-reordered rows. */
18981 /* ROW->minpos is the value of min_pos, the minimal buffer position
18982 we have in ROW, or ROW->start.pos if that is smaller. */
18983 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18984 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18985 else
18986 /* We didn't find buffer positions smaller than ROW->start, or
18987 didn't find _any_ valid buffer positions in any of the glyphs,
18988 so we must trust the iterator's computed positions. */
18989 row->minpos = row->start.pos;
18990 if (max_pos <= 0)
18992 max_pos = CHARPOS (it->current.pos);
18993 max_bpos = BYTEPOS (it->current.pos);
18996 /* Here are the various use-cases for ending the row, and the
18997 corresponding values for ROW->maxpos:
18999 Line ends in a newline from buffer eol_pos + 1
19000 Line is continued from buffer max_pos + 1
19001 Line is truncated on right it->current.pos
19002 Line ends in a newline from string max_pos + 1(*)
19003 (*) + 1 only when line ends in a forward scan
19004 Line is continued from string max_pos
19005 Line is continued from display vector max_pos
19006 Line is entirely from a string min_pos == max_pos
19007 Line is entirely from a display vector min_pos == max_pos
19008 Line that ends at ZV ZV
19010 If you discover other use-cases, please add them here as
19011 appropriate. */
19012 if (row->ends_at_zv_p)
19013 row->maxpos = it->current.pos;
19014 else if (row->used[TEXT_AREA])
19016 int seen_this_string = 0;
19017 struct glyph_row *r1 = row - 1;
19019 /* Did we see the same display string on the previous row? */
19020 if (STRINGP (it->object)
19021 /* this is not the first row */
19022 && row > it->w->desired_matrix->rows
19023 /* previous row is not the header line */
19024 && !r1->mode_line_p
19025 /* previous row also ends in a newline from a string */
19026 && r1->ends_in_newline_from_string_p)
19028 struct glyph *start, *end;
19030 /* Search for the last glyph of the previous row that came
19031 from buffer or string. Depending on whether the row is
19032 L2R or R2L, we need to process it front to back or the
19033 other way round. */
19034 if (!r1->reversed_p)
19036 start = r1->glyphs[TEXT_AREA];
19037 end = start + r1->used[TEXT_AREA];
19038 /* Glyphs inserted by redisplay have an integer (zero)
19039 as their object. */
19040 while (end > start
19041 && INTEGERP ((end - 1)->object)
19042 && (end - 1)->charpos <= 0)
19043 --end;
19044 if (end > start)
19046 if (EQ ((end - 1)->object, it->object))
19047 seen_this_string = 1;
19049 else
19050 /* If all the glyphs of the previous row were inserted
19051 by redisplay, it means the previous row was
19052 produced from a single newline, which is only
19053 possible if that newline came from the same string
19054 as the one which produced this ROW. */
19055 seen_this_string = 1;
19057 else
19059 end = r1->glyphs[TEXT_AREA] - 1;
19060 start = end + r1->used[TEXT_AREA];
19061 while (end < start
19062 && INTEGERP ((end + 1)->object)
19063 && (end + 1)->charpos <= 0)
19064 ++end;
19065 if (end < start)
19067 if (EQ ((end + 1)->object, it->object))
19068 seen_this_string = 1;
19070 else
19071 seen_this_string = 1;
19074 /* Take note of each display string that covers a newline only
19075 once, the first time we see it. This is for when a display
19076 string includes more than one newline in it. */
19077 if (row->ends_in_newline_from_string_p && !seen_this_string)
19079 /* If we were scanning the buffer forward when we displayed
19080 the string, we want to account for at least one buffer
19081 position that belongs to this row (position covered by
19082 the display string), so that cursor positioning will
19083 consider this row as a candidate when point is at the end
19084 of the visual line represented by this row. This is not
19085 required when scanning back, because max_pos will already
19086 have a much larger value. */
19087 if (CHARPOS (row->end.pos) > max_pos)
19088 INC_BOTH (max_pos, max_bpos);
19089 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19091 else if (CHARPOS (it->eol_pos) > 0)
19092 SET_TEXT_POS (row->maxpos,
19093 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19094 else if (row->continued_p)
19096 /* If max_pos is different from IT's current position, it
19097 means IT->method does not belong to the display element
19098 at max_pos. However, it also means that the display
19099 element at max_pos was displayed in its entirety on this
19100 line, which is equivalent to saying that the next line
19101 starts at the next buffer position. */
19102 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19103 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19104 else
19106 INC_BOTH (max_pos, max_bpos);
19107 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19110 else if (row->truncated_on_right_p)
19111 /* display_line already called reseat_at_next_visible_line_start,
19112 which puts the iterator at the beginning of the next line, in
19113 the logical order. */
19114 row->maxpos = it->current.pos;
19115 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19116 /* A line that is entirely from a string/image/stretch... */
19117 row->maxpos = row->minpos;
19118 else
19119 emacs_abort ();
19121 else
19122 row->maxpos = it->current.pos;
19125 /* Construct the glyph row IT->glyph_row in the desired matrix of
19126 IT->w from text at the current position of IT. See dispextern.h
19127 for an overview of struct it. Value is non-zero if
19128 IT->glyph_row displays text, as opposed to a line displaying ZV
19129 only. */
19131 static int
19132 display_line (struct it *it)
19134 struct glyph_row *row = it->glyph_row;
19135 Lisp_Object overlay_arrow_string;
19136 struct it wrap_it;
19137 void *wrap_data = NULL;
19138 int may_wrap = 0, wrap_x IF_LINT (= 0);
19139 int wrap_row_used = -1;
19140 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19141 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19142 int wrap_row_extra_line_spacing IF_LINT (= 0);
19143 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19144 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19145 int cvpos;
19146 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19147 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19149 /* We always start displaying at hpos zero even if hscrolled. */
19150 eassert (it->hpos == 0 && it->current_x == 0);
19152 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19153 >= it->w->desired_matrix->nrows)
19155 it->w->nrows_scale_factor++;
19156 fonts_changed_p = 1;
19157 return 0;
19160 /* Is IT->w showing the region? */
19161 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19163 /* Clear the result glyph row and enable it. */
19164 prepare_desired_row (row);
19166 row->y = it->current_y;
19167 row->start = it->start;
19168 row->continuation_lines_width = it->continuation_lines_width;
19169 row->displays_text_p = 1;
19170 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19171 it->starts_in_middle_of_char_p = 0;
19173 /* Arrange the overlays nicely for our purposes. Usually, we call
19174 display_line on only one line at a time, in which case this
19175 can't really hurt too much, or we call it on lines which appear
19176 one after another in the buffer, in which case all calls to
19177 recenter_overlay_lists but the first will be pretty cheap. */
19178 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19180 /* Move over display elements that are not visible because we are
19181 hscrolled. This may stop at an x-position < IT->first_visible_x
19182 if the first glyph is partially visible or if we hit a line end. */
19183 if (it->current_x < it->first_visible_x)
19185 enum move_it_result move_result;
19187 this_line_min_pos = row->start.pos;
19188 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19189 MOVE_TO_POS | MOVE_TO_X);
19190 /* If we are under a large hscroll, move_it_in_display_line_to
19191 could hit the end of the line without reaching
19192 it->first_visible_x. Pretend that we did reach it. This is
19193 especially important on a TTY, where we will call
19194 extend_face_to_end_of_line, which needs to know how many
19195 blank glyphs to produce. */
19196 if (it->current_x < it->first_visible_x
19197 && (move_result == MOVE_NEWLINE_OR_CR
19198 || move_result == MOVE_POS_MATCH_OR_ZV))
19199 it->current_x = it->first_visible_x;
19201 /* Record the smallest positions seen while we moved over
19202 display elements that are not visible. This is needed by
19203 redisplay_internal for optimizing the case where the cursor
19204 stays inside the same line. The rest of this function only
19205 considers positions that are actually displayed, so
19206 RECORD_MAX_MIN_POS will not otherwise record positions that
19207 are hscrolled to the left of the left edge of the window. */
19208 min_pos = CHARPOS (this_line_min_pos);
19209 min_bpos = BYTEPOS (this_line_min_pos);
19211 else
19213 /* We only do this when not calling `move_it_in_display_line_to'
19214 above, because move_it_in_display_line_to calls
19215 handle_line_prefix itself. */
19216 handle_line_prefix (it);
19219 /* Get the initial row height. This is either the height of the
19220 text hscrolled, if there is any, or zero. */
19221 row->ascent = it->max_ascent;
19222 row->height = it->max_ascent + it->max_descent;
19223 row->phys_ascent = it->max_phys_ascent;
19224 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19225 row->extra_line_spacing = it->max_extra_line_spacing;
19227 /* Utility macro to record max and min buffer positions seen until now. */
19228 #define RECORD_MAX_MIN_POS(IT) \
19229 do \
19231 int composition_p = !STRINGP ((IT)->string) \
19232 && ((IT)->what == IT_COMPOSITION); \
19233 ptrdiff_t current_pos = \
19234 composition_p ? (IT)->cmp_it.charpos \
19235 : IT_CHARPOS (*(IT)); \
19236 ptrdiff_t current_bpos = \
19237 composition_p ? CHAR_TO_BYTE (current_pos) \
19238 : IT_BYTEPOS (*(IT)); \
19239 if (current_pos < min_pos) \
19241 min_pos = current_pos; \
19242 min_bpos = current_bpos; \
19244 if (IT_CHARPOS (*it) > max_pos) \
19246 max_pos = IT_CHARPOS (*it); \
19247 max_bpos = IT_BYTEPOS (*it); \
19250 while (0)
19252 /* Loop generating characters. The loop is left with IT on the next
19253 character to display. */
19254 while (1)
19256 int n_glyphs_before, hpos_before, x_before;
19257 int x, nglyphs;
19258 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19260 /* Retrieve the next thing to display. Value is zero if end of
19261 buffer reached. */
19262 if (!get_next_display_element (it))
19264 /* Maybe add a space at the end of this line that is used to
19265 display the cursor there under X. Set the charpos of the
19266 first glyph of blank lines not corresponding to any text
19267 to -1. */
19268 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19269 row->exact_window_width_line_p = 1;
19270 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19271 || row->used[TEXT_AREA] == 0)
19273 row->glyphs[TEXT_AREA]->charpos = -1;
19274 row->displays_text_p = 0;
19276 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19277 && (!MINI_WINDOW_P (it->w)
19278 || (minibuf_level && EQ (it->window, minibuf_window))))
19279 row->indicate_empty_line_p = 1;
19282 it->continuation_lines_width = 0;
19283 row->ends_at_zv_p = 1;
19284 /* A row that displays right-to-left text must always have
19285 its last face extended all the way to the end of line,
19286 even if this row ends in ZV, because we still write to
19287 the screen left to right. We also need to extend the
19288 last face if the default face is remapped to some
19289 different face, otherwise the functions that clear
19290 portions of the screen will clear with the default face's
19291 background color. */
19292 if (row->reversed_p
19293 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19294 extend_face_to_end_of_line (it);
19295 break;
19298 /* Now, get the metrics of what we want to display. This also
19299 generates glyphs in `row' (which is IT->glyph_row). */
19300 n_glyphs_before = row->used[TEXT_AREA];
19301 x = it->current_x;
19303 /* Remember the line height so far in case the next element doesn't
19304 fit on the line. */
19305 if (it->line_wrap != TRUNCATE)
19307 ascent = it->max_ascent;
19308 descent = it->max_descent;
19309 phys_ascent = it->max_phys_ascent;
19310 phys_descent = it->max_phys_descent;
19312 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19314 if (IT_DISPLAYING_WHITESPACE (it))
19315 may_wrap = 1;
19316 else if (may_wrap)
19318 SAVE_IT (wrap_it, *it, wrap_data);
19319 wrap_x = x;
19320 wrap_row_used = row->used[TEXT_AREA];
19321 wrap_row_ascent = row->ascent;
19322 wrap_row_height = row->height;
19323 wrap_row_phys_ascent = row->phys_ascent;
19324 wrap_row_phys_height = row->phys_height;
19325 wrap_row_extra_line_spacing = row->extra_line_spacing;
19326 wrap_row_min_pos = min_pos;
19327 wrap_row_min_bpos = min_bpos;
19328 wrap_row_max_pos = max_pos;
19329 wrap_row_max_bpos = max_bpos;
19330 may_wrap = 0;
19335 PRODUCE_GLYPHS (it);
19337 /* If this display element was in marginal areas, continue with
19338 the next one. */
19339 if (it->area != TEXT_AREA)
19341 row->ascent = max (row->ascent, it->max_ascent);
19342 row->height = max (row->height, it->max_ascent + it->max_descent);
19343 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19344 row->phys_height = max (row->phys_height,
19345 it->max_phys_ascent + it->max_phys_descent);
19346 row->extra_line_spacing = max (row->extra_line_spacing,
19347 it->max_extra_line_spacing);
19348 set_iterator_to_next (it, 1);
19349 continue;
19352 /* Does the display element fit on the line? If we truncate
19353 lines, we should draw past the right edge of the window. If
19354 we don't truncate, we want to stop so that we can display the
19355 continuation glyph before the right margin. If lines are
19356 continued, there are two possible strategies for characters
19357 resulting in more than 1 glyph (e.g. tabs): Display as many
19358 glyphs as possible in this line and leave the rest for the
19359 continuation line, or display the whole element in the next
19360 line. Original redisplay did the former, so we do it also. */
19361 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19362 hpos_before = it->hpos;
19363 x_before = x;
19365 if (/* Not a newline. */
19366 nglyphs > 0
19367 /* Glyphs produced fit entirely in the line. */
19368 && it->current_x < it->last_visible_x)
19370 it->hpos += nglyphs;
19371 row->ascent = max (row->ascent, it->max_ascent);
19372 row->height = max (row->height, it->max_ascent + it->max_descent);
19373 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19374 row->phys_height = max (row->phys_height,
19375 it->max_phys_ascent + it->max_phys_descent);
19376 row->extra_line_spacing = max (row->extra_line_spacing,
19377 it->max_extra_line_spacing);
19378 if (it->current_x - it->pixel_width < it->first_visible_x)
19379 row->x = x - it->first_visible_x;
19380 /* Record the maximum and minimum buffer positions seen so
19381 far in glyphs that will be displayed by this row. */
19382 if (it->bidi_p)
19383 RECORD_MAX_MIN_POS (it);
19385 else
19387 int i, new_x;
19388 struct glyph *glyph;
19390 for (i = 0; i < nglyphs; ++i, x = new_x)
19392 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19393 new_x = x + glyph->pixel_width;
19395 if (/* Lines are continued. */
19396 it->line_wrap != TRUNCATE
19397 && (/* Glyph doesn't fit on the line. */
19398 new_x > it->last_visible_x
19399 /* Or it fits exactly on a window system frame. */
19400 || (new_x == it->last_visible_x
19401 && FRAME_WINDOW_P (it->f)
19402 && (row->reversed_p
19403 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19404 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19406 /* End of a continued line. */
19408 if (it->hpos == 0
19409 || (new_x == it->last_visible_x
19410 && FRAME_WINDOW_P (it->f)
19411 && (row->reversed_p
19412 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19413 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19415 /* Current glyph is the only one on the line or
19416 fits exactly on the line. We must continue
19417 the line because we can't draw the cursor
19418 after the glyph. */
19419 row->continued_p = 1;
19420 it->current_x = new_x;
19421 it->continuation_lines_width += new_x;
19422 ++it->hpos;
19423 if (i == nglyphs - 1)
19425 /* If line-wrap is on, check if a previous
19426 wrap point was found. */
19427 if (wrap_row_used > 0
19428 /* Even if there is a previous wrap
19429 point, continue the line here as
19430 usual, if (i) the previous character
19431 was a space or tab AND (ii) the
19432 current character is not. */
19433 && (!may_wrap
19434 || IT_DISPLAYING_WHITESPACE (it)))
19435 goto back_to_wrap;
19437 /* Record the maximum and minimum buffer
19438 positions seen so far in glyphs that will be
19439 displayed by this row. */
19440 if (it->bidi_p)
19441 RECORD_MAX_MIN_POS (it);
19442 set_iterator_to_next (it, 1);
19443 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19445 if (!get_next_display_element (it))
19447 row->exact_window_width_line_p = 1;
19448 it->continuation_lines_width = 0;
19449 row->continued_p = 0;
19450 row->ends_at_zv_p = 1;
19452 else if (ITERATOR_AT_END_OF_LINE_P (it))
19454 row->continued_p = 0;
19455 row->exact_window_width_line_p = 1;
19459 else if (it->bidi_p)
19460 RECORD_MAX_MIN_POS (it);
19462 else if (CHAR_GLYPH_PADDING_P (*glyph)
19463 && !FRAME_WINDOW_P (it->f))
19465 /* A padding glyph that doesn't fit on this line.
19466 This means the whole character doesn't fit
19467 on the line. */
19468 if (row->reversed_p)
19469 unproduce_glyphs (it, row->used[TEXT_AREA]
19470 - n_glyphs_before);
19471 row->used[TEXT_AREA] = n_glyphs_before;
19473 /* Fill the rest of the row with continuation
19474 glyphs like in 20.x. */
19475 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19476 < row->glyphs[1 + TEXT_AREA])
19477 produce_special_glyphs (it, IT_CONTINUATION);
19479 row->continued_p = 1;
19480 it->current_x = x_before;
19481 it->continuation_lines_width += x_before;
19483 /* Restore the height to what it was before the
19484 element not fitting on the line. */
19485 it->max_ascent = ascent;
19486 it->max_descent = descent;
19487 it->max_phys_ascent = phys_ascent;
19488 it->max_phys_descent = phys_descent;
19490 else if (wrap_row_used > 0)
19492 back_to_wrap:
19493 if (row->reversed_p)
19494 unproduce_glyphs (it,
19495 row->used[TEXT_AREA] - wrap_row_used);
19496 RESTORE_IT (it, &wrap_it, wrap_data);
19497 it->continuation_lines_width += wrap_x;
19498 row->used[TEXT_AREA] = wrap_row_used;
19499 row->ascent = wrap_row_ascent;
19500 row->height = wrap_row_height;
19501 row->phys_ascent = wrap_row_phys_ascent;
19502 row->phys_height = wrap_row_phys_height;
19503 row->extra_line_spacing = wrap_row_extra_line_spacing;
19504 min_pos = wrap_row_min_pos;
19505 min_bpos = wrap_row_min_bpos;
19506 max_pos = wrap_row_max_pos;
19507 max_bpos = wrap_row_max_bpos;
19508 row->continued_p = 1;
19509 row->ends_at_zv_p = 0;
19510 row->exact_window_width_line_p = 0;
19511 it->continuation_lines_width += x;
19513 /* Make sure that a non-default face is extended
19514 up to the right margin of the window. */
19515 extend_face_to_end_of_line (it);
19517 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19519 /* A TAB that extends past the right edge of the
19520 window. This produces a single glyph on
19521 window system frames. We leave the glyph in
19522 this row and let it fill the row, but don't
19523 consume the TAB. */
19524 if ((row->reversed_p
19525 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19526 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19527 produce_special_glyphs (it, IT_CONTINUATION);
19528 it->continuation_lines_width += it->last_visible_x;
19529 row->ends_in_middle_of_char_p = 1;
19530 row->continued_p = 1;
19531 glyph->pixel_width = it->last_visible_x - x;
19532 it->starts_in_middle_of_char_p = 1;
19534 else
19536 /* Something other than a TAB that draws past
19537 the right edge of the window. Restore
19538 positions to values before the element. */
19539 if (row->reversed_p)
19540 unproduce_glyphs (it, row->used[TEXT_AREA]
19541 - (n_glyphs_before + i));
19542 row->used[TEXT_AREA] = n_glyphs_before + i;
19544 /* Display continuation glyphs. */
19545 it->current_x = x_before;
19546 it->continuation_lines_width += x;
19547 if (!FRAME_WINDOW_P (it->f)
19548 || (row->reversed_p
19549 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19550 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19551 produce_special_glyphs (it, IT_CONTINUATION);
19552 row->continued_p = 1;
19554 extend_face_to_end_of_line (it);
19556 if (nglyphs > 1 && i > 0)
19558 row->ends_in_middle_of_char_p = 1;
19559 it->starts_in_middle_of_char_p = 1;
19562 /* Restore the height to what it was before the
19563 element not fitting on the line. */
19564 it->max_ascent = ascent;
19565 it->max_descent = descent;
19566 it->max_phys_ascent = phys_ascent;
19567 it->max_phys_descent = phys_descent;
19570 break;
19572 else if (new_x > it->first_visible_x)
19574 /* Increment number of glyphs actually displayed. */
19575 ++it->hpos;
19577 /* Record the maximum and minimum buffer positions
19578 seen so far in glyphs that will be displayed by
19579 this row. */
19580 if (it->bidi_p)
19581 RECORD_MAX_MIN_POS (it);
19583 if (x < it->first_visible_x)
19584 /* Glyph is partially visible, i.e. row starts at
19585 negative X position. */
19586 row->x = x - it->first_visible_x;
19588 else
19590 /* Glyph is completely off the left margin of the
19591 window. This should not happen because of the
19592 move_it_in_display_line at the start of this
19593 function, unless the text display area of the
19594 window is empty. */
19595 eassert (it->first_visible_x <= it->last_visible_x);
19598 /* Even if this display element produced no glyphs at all,
19599 we want to record its position. */
19600 if (it->bidi_p && nglyphs == 0)
19601 RECORD_MAX_MIN_POS (it);
19603 row->ascent = max (row->ascent, it->max_ascent);
19604 row->height = max (row->height, it->max_ascent + it->max_descent);
19605 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19606 row->phys_height = max (row->phys_height,
19607 it->max_phys_ascent + it->max_phys_descent);
19608 row->extra_line_spacing = max (row->extra_line_spacing,
19609 it->max_extra_line_spacing);
19611 /* End of this display line if row is continued. */
19612 if (row->continued_p || row->ends_at_zv_p)
19613 break;
19616 at_end_of_line:
19617 /* Is this a line end? If yes, we're also done, after making
19618 sure that a non-default face is extended up to the right
19619 margin of the window. */
19620 if (ITERATOR_AT_END_OF_LINE_P (it))
19622 int used_before = row->used[TEXT_AREA];
19624 row->ends_in_newline_from_string_p = STRINGP (it->object);
19626 /* Add a space at the end of the line that is used to
19627 display the cursor there. */
19628 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19629 append_space_for_newline (it, 0);
19631 /* Extend the face to the end of the line. */
19632 extend_face_to_end_of_line (it);
19634 /* Make sure we have the position. */
19635 if (used_before == 0)
19636 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19638 /* Record the position of the newline, for use in
19639 find_row_edges. */
19640 it->eol_pos = it->current.pos;
19642 /* Consume the line end. This skips over invisible lines. */
19643 set_iterator_to_next (it, 1);
19644 it->continuation_lines_width = 0;
19645 break;
19648 /* Proceed with next display element. Note that this skips
19649 over lines invisible because of selective display. */
19650 set_iterator_to_next (it, 1);
19652 /* If we truncate lines, we are done when the last displayed
19653 glyphs reach past the right margin of the window. */
19654 if (it->line_wrap == TRUNCATE
19655 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19656 ? (it->current_x >= it->last_visible_x)
19657 : (it->current_x > it->last_visible_x)))
19659 /* Maybe add truncation glyphs. */
19660 if (!FRAME_WINDOW_P (it->f)
19661 || (row->reversed_p
19662 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19663 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19665 int i, n;
19667 if (!row->reversed_p)
19669 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19670 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19671 break;
19673 else
19675 for (i = 0; i < row->used[TEXT_AREA]; i++)
19676 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19677 break;
19678 /* Remove any padding glyphs at the front of ROW, to
19679 make room for the truncation glyphs we will be
19680 adding below. The loop below always inserts at
19681 least one truncation glyph, so also remove the
19682 last glyph added to ROW. */
19683 unproduce_glyphs (it, i + 1);
19684 /* Adjust i for the loop below. */
19685 i = row->used[TEXT_AREA] - (i + 1);
19688 it->current_x = x_before;
19689 if (!FRAME_WINDOW_P (it->f))
19691 for (n = row->used[TEXT_AREA]; i < n; ++i)
19693 row->used[TEXT_AREA] = i;
19694 produce_special_glyphs (it, IT_TRUNCATION);
19697 else
19699 row->used[TEXT_AREA] = i;
19700 produce_special_glyphs (it, IT_TRUNCATION);
19703 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19705 /* Don't truncate if we can overflow newline into fringe. */
19706 if (!get_next_display_element (it))
19708 it->continuation_lines_width = 0;
19709 row->ends_at_zv_p = 1;
19710 row->exact_window_width_line_p = 1;
19711 break;
19713 if (ITERATOR_AT_END_OF_LINE_P (it))
19715 row->exact_window_width_line_p = 1;
19716 goto at_end_of_line;
19718 it->current_x = x_before;
19721 row->truncated_on_right_p = 1;
19722 it->continuation_lines_width = 0;
19723 reseat_at_next_visible_line_start (it, 0);
19724 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19725 it->hpos = hpos_before;
19726 break;
19730 if (wrap_data)
19731 bidi_unshelve_cache (wrap_data, 1);
19733 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19734 at the left window margin. */
19735 if (it->first_visible_x
19736 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19738 if (!FRAME_WINDOW_P (it->f)
19739 || (row->reversed_p
19740 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19741 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19742 insert_left_trunc_glyphs (it);
19743 row->truncated_on_left_p = 1;
19746 /* Remember the position at which this line ends.
19748 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19749 cannot be before the call to find_row_edges below, since that is
19750 where these positions are determined. */
19751 row->end = it->current;
19752 if (!it->bidi_p)
19754 row->minpos = row->start.pos;
19755 row->maxpos = row->end.pos;
19757 else
19759 /* ROW->minpos and ROW->maxpos must be the smallest and
19760 `1 + the largest' buffer positions in ROW. But if ROW was
19761 bidi-reordered, these two positions can be anywhere in the
19762 row, so we must determine them now. */
19763 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19766 /* If the start of this line is the overlay arrow-position, then
19767 mark this glyph row as the one containing the overlay arrow.
19768 This is clearly a mess with variable size fonts. It would be
19769 better to let it be displayed like cursors under X. */
19770 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19771 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19772 !NILP (overlay_arrow_string)))
19774 /* Overlay arrow in window redisplay is a fringe bitmap. */
19775 if (STRINGP (overlay_arrow_string))
19777 struct glyph_row *arrow_row
19778 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19779 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19780 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19781 struct glyph *p = row->glyphs[TEXT_AREA];
19782 struct glyph *p2, *end;
19784 /* Copy the arrow glyphs. */
19785 while (glyph < arrow_end)
19786 *p++ = *glyph++;
19788 /* Throw away padding glyphs. */
19789 p2 = p;
19790 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19791 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19792 ++p2;
19793 if (p2 > p)
19795 while (p2 < end)
19796 *p++ = *p2++;
19797 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19800 else
19802 eassert (INTEGERP (overlay_arrow_string));
19803 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19805 overlay_arrow_seen = 1;
19808 /* Highlight trailing whitespace. */
19809 if (!NILP (Vshow_trailing_whitespace))
19810 highlight_trailing_whitespace (it->f, it->glyph_row);
19812 /* Compute pixel dimensions of this line. */
19813 compute_line_metrics (it);
19815 /* Implementation note: No changes in the glyphs of ROW or in their
19816 faces can be done past this point, because compute_line_metrics
19817 computes ROW's hash value and stores it within the glyph_row
19818 structure. */
19820 /* Record whether this row ends inside an ellipsis. */
19821 row->ends_in_ellipsis_p
19822 = (it->method == GET_FROM_DISPLAY_VECTOR
19823 && it->ellipsis_p);
19825 /* Save fringe bitmaps in this row. */
19826 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19827 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19828 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19829 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19831 it->left_user_fringe_bitmap = 0;
19832 it->left_user_fringe_face_id = 0;
19833 it->right_user_fringe_bitmap = 0;
19834 it->right_user_fringe_face_id = 0;
19836 /* Maybe set the cursor. */
19837 cvpos = it->w->cursor.vpos;
19838 if ((cvpos < 0
19839 /* In bidi-reordered rows, keep checking for proper cursor
19840 position even if one has been found already, because buffer
19841 positions in such rows change non-linearly with ROW->VPOS,
19842 when a line is continued. One exception: when we are at ZV,
19843 display cursor on the first suitable glyph row, since all
19844 the empty rows after that also have their position set to ZV. */
19845 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19846 lines' rows is implemented for bidi-reordered rows. */
19847 || (it->bidi_p
19848 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19849 && PT >= MATRIX_ROW_START_CHARPOS (row)
19850 && PT <= MATRIX_ROW_END_CHARPOS (row)
19851 && cursor_row_p (row))
19852 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19854 /* Prepare for the next line. This line starts horizontally at (X
19855 HPOS) = (0 0). Vertical positions are incremented. As a
19856 convenience for the caller, IT->glyph_row is set to the next
19857 row to be used. */
19858 it->current_x = it->hpos = 0;
19859 it->current_y += row->height;
19860 SET_TEXT_POS (it->eol_pos, 0, 0);
19861 ++it->vpos;
19862 ++it->glyph_row;
19863 /* The next row should by default use the same value of the
19864 reversed_p flag as this one. set_iterator_to_next decides when
19865 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19866 the flag accordingly. */
19867 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19868 it->glyph_row->reversed_p = row->reversed_p;
19869 it->start = row->end;
19870 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19872 #undef RECORD_MAX_MIN_POS
19875 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19876 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19877 doc: /* Return paragraph direction at point in BUFFER.
19878 Value is either `left-to-right' or `right-to-left'.
19879 If BUFFER is omitted or nil, it defaults to the current buffer.
19881 Paragraph direction determines how the text in the paragraph is displayed.
19882 In left-to-right paragraphs, text begins at the left margin of the window
19883 and the reading direction is generally left to right. In right-to-left
19884 paragraphs, text begins at the right margin and is read from right to left.
19886 See also `bidi-paragraph-direction'. */)
19887 (Lisp_Object buffer)
19889 struct buffer *buf = current_buffer;
19890 struct buffer *old = buf;
19892 if (! NILP (buffer))
19894 CHECK_BUFFER (buffer);
19895 buf = XBUFFER (buffer);
19898 if (NILP (BVAR (buf, bidi_display_reordering))
19899 || NILP (BVAR (buf, enable_multibyte_characters))
19900 /* When we are loading loadup.el, the character property tables
19901 needed for bidi iteration are not yet available. */
19902 || !NILP (Vpurify_flag))
19903 return Qleft_to_right;
19904 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19905 return BVAR (buf, bidi_paragraph_direction);
19906 else
19908 /* Determine the direction from buffer text. We could try to
19909 use current_matrix if it is up to date, but this seems fast
19910 enough as it is. */
19911 struct bidi_it itb;
19912 ptrdiff_t pos = BUF_PT (buf);
19913 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19914 int c;
19915 void *itb_data = bidi_shelve_cache ();
19917 set_buffer_temp (buf);
19918 /* bidi_paragraph_init finds the base direction of the paragraph
19919 by searching forward from paragraph start. We need the base
19920 direction of the current or _previous_ paragraph, so we need
19921 to make sure we are within that paragraph. To that end, find
19922 the previous non-empty line. */
19923 if (pos >= ZV && pos > BEGV)
19924 DEC_BOTH (pos, bytepos);
19925 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19926 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19928 while ((c = FETCH_BYTE (bytepos)) == '\n'
19929 || c == ' ' || c == '\t' || c == '\f')
19931 if (bytepos <= BEGV_BYTE)
19932 break;
19933 bytepos--;
19934 pos--;
19936 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19937 bytepos--;
19939 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19940 itb.paragraph_dir = NEUTRAL_DIR;
19941 itb.string.s = NULL;
19942 itb.string.lstring = Qnil;
19943 itb.string.bufpos = 0;
19944 itb.string.unibyte = 0;
19945 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19946 bidi_unshelve_cache (itb_data, 0);
19947 set_buffer_temp (old);
19948 switch (itb.paragraph_dir)
19950 case L2R:
19951 return Qleft_to_right;
19952 break;
19953 case R2L:
19954 return Qright_to_left;
19955 break;
19956 default:
19957 emacs_abort ();
19964 /***********************************************************************
19965 Menu Bar
19966 ***********************************************************************/
19968 /* Redisplay the menu bar in the frame for window W.
19970 The menu bar of X frames that don't have X toolkit support is
19971 displayed in a special window W->frame->menu_bar_window.
19973 The menu bar of terminal frames is treated specially as far as
19974 glyph matrices are concerned. Menu bar lines are not part of
19975 windows, so the update is done directly on the frame matrix rows
19976 for the menu bar. */
19978 static void
19979 display_menu_bar (struct window *w)
19981 struct frame *f = XFRAME (WINDOW_FRAME (w));
19982 struct it it;
19983 Lisp_Object items;
19984 int i;
19986 /* Don't do all this for graphical frames. */
19987 #ifdef HAVE_NTGUI
19988 if (FRAME_W32_P (f))
19989 return;
19990 #endif
19991 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19992 if (FRAME_X_P (f))
19993 return;
19994 #endif
19996 #ifdef HAVE_NS
19997 if (FRAME_NS_P (f))
19998 return;
19999 #endif /* HAVE_NS */
20001 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20002 eassert (!FRAME_WINDOW_P (f));
20003 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20004 it.first_visible_x = 0;
20005 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20006 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20007 if (FRAME_WINDOW_P (f))
20009 /* Menu bar lines are displayed in the desired matrix of the
20010 dummy window menu_bar_window. */
20011 struct window *menu_w;
20012 menu_w = XWINDOW (f->menu_bar_window);
20013 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20014 MENU_FACE_ID);
20015 it.first_visible_x = 0;
20016 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20018 else
20019 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20021 /* This is a TTY frame, i.e. character hpos/vpos are used as
20022 pixel x/y. */
20023 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20024 MENU_FACE_ID);
20025 it.first_visible_x = 0;
20026 it.last_visible_x = FRAME_COLS (f);
20029 /* FIXME: This should be controlled by a user option. See the
20030 comments in redisplay_tool_bar and display_mode_line about
20031 this. */
20032 it.paragraph_embedding = L2R;
20034 /* Clear all rows of the menu bar. */
20035 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20037 struct glyph_row *row = it.glyph_row + i;
20038 clear_glyph_row (row);
20039 row->enabled_p = 1;
20040 row->full_width_p = 1;
20043 /* Display all items of the menu bar. */
20044 items = FRAME_MENU_BAR_ITEMS (it.f);
20045 for (i = 0; i < ASIZE (items); i += 4)
20047 Lisp_Object string;
20049 /* Stop at nil string. */
20050 string = AREF (items, i + 1);
20051 if (NILP (string))
20052 break;
20054 /* Remember where item was displayed. */
20055 ASET (items, i + 3, make_number (it.hpos));
20057 /* Display the item, pad with one space. */
20058 if (it.current_x < it.last_visible_x)
20059 display_string (NULL, string, Qnil, 0, 0, &it,
20060 SCHARS (string) + 1, 0, 0, -1);
20063 /* Fill out the line with spaces. */
20064 if (it.current_x < it.last_visible_x)
20065 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20067 /* Compute the total height of the lines. */
20068 compute_line_metrics (&it);
20073 /***********************************************************************
20074 Mode Line
20075 ***********************************************************************/
20077 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20078 FORCE is non-zero, redisplay mode lines unconditionally.
20079 Otherwise, redisplay only mode lines that are garbaged. Value is
20080 the number of windows whose mode lines were redisplayed. */
20082 static int
20083 redisplay_mode_lines (Lisp_Object window, int force)
20085 int nwindows = 0;
20087 while (!NILP (window))
20089 struct window *w = XWINDOW (window);
20091 if (WINDOWP (w->contents))
20092 nwindows += redisplay_mode_lines (w->contents, force);
20093 else if (force
20094 || FRAME_GARBAGED_P (XFRAME (w->frame))
20095 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20097 struct text_pos lpoint;
20098 struct buffer *old = current_buffer;
20100 /* Set the window's buffer for the mode line display. */
20101 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20102 set_buffer_internal_1 (XBUFFER (w->contents));
20104 /* Point refers normally to the selected window. For any
20105 other window, set up appropriate value. */
20106 if (!EQ (window, selected_window))
20108 struct text_pos pt;
20110 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20111 if (CHARPOS (pt) < BEGV)
20112 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20113 else if (CHARPOS (pt) > (ZV - 1))
20114 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20115 else
20116 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20119 /* Display mode lines. */
20120 clear_glyph_matrix (w->desired_matrix);
20121 if (display_mode_lines (w))
20123 ++nwindows;
20124 w->must_be_updated_p = 1;
20127 /* Restore old settings. */
20128 set_buffer_internal_1 (old);
20129 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20132 window = w->next;
20135 return nwindows;
20139 /* Display the mode and/or header line of window W. Value is the
20140 sum number of mode lines and header lines displayed. */
20142 static int
20143 display_mode_lines (struct window *w)
20145 Lisp_Object old_selected_window = selected_window;
20146 Lisp_Object old_selected_frame = selected_frame;
20147 Lisp_Object new_frame = w->frame;
20148 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20149 int n = 0;
20151 selected_frame = new_frame;
20152 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20153 or window's point, then we'd need select_window_1 here as well. */
20154 XSETWINDOW (selected_window, w);
20155 XFRAME (new_frame)->selected_window = selected_window;
20157 /* These will be set while the mode line specs are processed. */
20158 line_number_displayed = 0;
20159 w->column_number_displayed = -1;
20161 if (WINDOW_WANTS_MODELINE_P (w))
20163 struct window *sel_w = XWINDOW (old_selected_window);
20165 /* Select mode line face based on the real selected window. */
20166 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20167 BVAR (current_buffer, mode_line_format));
20168 ++n;
20171 if (WINDOW_WANTS_HEADER_LINE_P (w))
20173 display_mode_line (w, HEADER_LINE_FACE_ID,
20174 BVAR (current_buffer, header_line_format));
20175 ++n;
20178 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20179 selected_frame = old_selected_frame;
20180 selected_window = old_selected_window;
20181 return n;
20185 /* Display mode or header line of window W. FACE_ID specifies which
20186 line to display; it is either MODE_LINE_FACE_ID or
20187 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20188 display. Value is the pixel height of the mode/header line
20189 displayed. */
20191 static int
20192 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20194 struct it it;
20195 struct face *face;
20196 ptrdiff_t count = SPECPDL_INDEX ();
20198 init_iterator (&it, w, -1, -1, NULL, face_id);
20199 /* Don't extend on a previously drawn mode-line.
20200 This may happen if called from pos_visible_p. */
20201 it.glyph_row->enabled_p = 0;
20202 prepare_desired_row (it.glyph_row);
20204 it.glyph_row->mode_line_p = 1;
20206 /* FIXME: This should be controlled by a user option. But
20207 supporting such an option is not trivial, since the mode line is
20208 made up of many separate strings. */
20209 it.paragraph_embedding = L2R;
20211 record_unwind_protect (unwind_format_mode_line,
20212 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20214 mode_line_target = MODE_LINE_DISPLAY;
20216 /* Temporarily make frame's keyboard the current kboard so that
20217 kboard-local variables in the mode_line_format will get the right
20218 values. */
20219 push_kboard (FRAME_KBOARD (it.f));
20220 record_unwind_save_match_data ();
20221 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20222 pop_kboard ();
20224 unbind_to (count, Qnil);
20226 /* Fill up with spaces. */
20227 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20229 compute_line_metrics (&it);
20230 it.glyph_row->full_width_p = 1;
20231 it.glyph_row->continued_p = 0;
20232 it.glyph_row->truncated_on_left_p = 0;
20233 it.glyph_row->truncated_on_right_p = 0;
20235 /* Make a 3D mode-line have a shadow at its right end. */
20236 face = FACE_FROM_ID (it.f, face_id);
20237 extend_face_to_end_of_line (&it);
20238 if (face->box != FACE_NO_BOX)
20240 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20241 + it.glyph_row->used[TEXT_AREA] - 1);
20242 last->right_box_line_p = 1;
20245 return it.glyph_row->height;
20248 /* Move element ELT in LIST to the front of LIST.
20249 Return the updated list. */
20251 static Lisp_Object
20252 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20254 register Lisp_Object tail, prev;
20255 register Lisp_Object tem;
20257 tail = list;
20258 prev = Qnil;
20259 while (CONSP (tail))
20261 tem = XCAR (tail);
20263 if (EQ (elt, tem))
20265 /* Splice out the link TAIL. */
20266 if (NILP (prev))
20267 list = XCDR (tail);
20268 else
20269 Fsetcdr (prev, XCDR (tail));
20271 /* Now make it the first. */
20272 Fsetcdr (tail, list);
20273 return tail;
20275 else
20276 prev = tail;
20277 tail = XCDR (tail);
20278 QUIT;
20281 /* Not found--return unchanged LIST. */
20282 return list;
20285 /* Contribute ELT to the mode line for window IT->w. How it
20286 translates into text depends on its data type.
20288 IT describes the display environment in which we display, as usual.
20290 DEPTH is the depth in recursion. It is used to prevent
20291 infinite recursion here.
20293 FIELD_WIDTH is the number of characters the display of ELT should
20294 occupy in the mode line, and PRECISION is the maximum number of
20295 characters to display from ELT's representation. See
20296 display_string for details.
20298 Returns the hpos of the end of the text generated by ELT.
20300 PROPS is a property list to add to any string we encounter.
20302 If RISKY is nonzero, remove (disregard) any properties in any string
20303 we encounter, and ignore :eval and :propertize.
20305 The global variable `mode_line_target' determines whether the
20306 output is passed to `store_mode_line_noprop',
20307 `store_mode_line_string', or `display_string'. */
20309 static int
20310 display_mode_element (struct it *it, int depth, int field_width, int precision,
20311 Lisp_Object elt, Lisp_Object props, int risky)
20313 int n = 0, field, prec;
20314 int literal = 0;
20316 tail_recurse:
20317 if (depth > 100)
20318 elt = build_string ("*too-deep*");
20320 depth++;
20322 switch (XTYPE (elt))
20324 case Lisp_String:
20326 /* A string: output it and check for %-constructs within it. */
20327 unsigned char c;
20328 ptrdiff_t offset = 0;
20330 if (SCHARS (elt) > 0
20331 && (!NILP (props) || risky))
20333 Lisp_Object oprops, aelt;
20334 oprops = Ftext_properties_at (make_number (0), elt);
20336 /* If the starting string's properties are not what
20337 we want, translate the string. Also, if the string
20338 is risky, do that anyway. */
20340 if (NILP (Fequal (props, oprops)) || risky)
20342 /* If the starting string has properties,
20343 merge the specified ones onto the existing ones. */
20344 if (! NILP (oprops) && !risky)
20346 Lisp_Object tem;
20348 oprops = Fcopy_sequence (oprops);
20349 tem = props;
20350 while (CONSP (tem))
20352 oprops = Fplist_put (oprops, XCAR (tem),
20353 XCAR (XCDR (tem)));
20354 tem = XCDR (XCDR (tem));
20356 props = oprops;
20359 aelt = Fassoc (elt, mode_line_proptrans_alist);
20360 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20362 /* AELT is what we want. Move it to the front
20363 without consing. */
20364 elt = XCAR (aelt);
20365 mode_line_proptrans_alist
20366 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20368 else
20370 Lisp_Object tem;
20372 /* If AELT has the wrong props, it is useless.
20373 so get rid of it. */
20374 if (! NILP (aelt))
20375 mode_line_proptrans_alist
20376 = Fdelq (aelt, mode_line_proptrans_alist);
20378 elt = Fcopy_sequence (elt);
20379 Fset_text_properties (make_number (0), Flength (elt),
20380 props, elt);
20381 /* Add this item to mode_line_proptrans_alist. */
20382 mode_line_proptrans_alist
20383 = Fcons (Fcons (elt, props),
20384 mode_line_proptrans_alist);
20385 /* Truncate mode_line_proptrans_alist
20386 to at most 50 elements. */
20387 tem = Fnthcdr (make_number (50),
20388 mode_line_proptrans_alist);
20389 if (! NILP (tem))
20390 XSETCDR (tem, Qnil);
20395 offset = 0;
20397 if (literal)
20399 prec = precision - n;
20400 switch (mode_line_target)
20402 case MODE_LINE_NOPROP:
20403 case MODE_LINE_TITLE:
20404 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20405 break;
20406 case MODE_LINE_STRING:
20407 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20408 break;
20409 case MODE_LINE_DISPLAY:
20410 n += display_string (NULL, elt, Qnil, 0, 0, it,
20411 0, prec, 0, STRING_MULTIBYTE (elt));
20412 break;
20415 break;
20418 /* Handle the non-literal case. */
20420 while ((precision <= 0 || n < precision)
20421 && SREF (elt, offset) != 0
20422 && (mode_line_target != MODE_LINE_DISPLAY
20423 || it->current_x < it->last_visible_x))
20425 ptrdiff_t last_offset = offset;
20427 /* Advance to end of string or next format specifier. */
20428 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20431 if (offset - 1 != last_offset)
20433 ptrdiff_t nchars, nbytes;
20435 /* Output to end of string or up to '%'. Field width
20436 is length of string. Don't output more than
20437 PRECISION allows us. */
20438 offset--;
20440 prec = c_string_width (SDATA (elt) + last_offset,
20441 offset - last_offset, precision - n,
20442 &nchars, &nbytes);
20444 switch (mode_line_target)
20446 case MODE_LINE_NOPROP:
20447 case MODE_LINE_TITLE:
20448 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20449 break;
20450 case MODE_LINE_STRING:
20452 ptrdiff_t bytepos = last_offset;
20453 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20454 ptrdiff_t endpos = (precision <= 0
20455 ? string_byte_to_char (elt, offset)
20456 : charpos + nchars);
20458 n += store_mode_line_string (NULL,
20459 Fsubstring (elt, make_number (charpos),
20460 make_number (endpos)),
20461 0, 0, 0, Qnil);
20463 break;
20464 case MODE_LINE_DISPLAY:
20466 ptrdiff_t bytepos = last_offset;
20467 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20469 if (precision <= 0)
20470 nchars = string_byte_to_char (elt, offset) - charpos;
20471 n += display_string (NULL, elt, Qnil, 0, charpos,
20472 it, 0, nchars, 0,
20473 STRING_MULTIBYTE (elt));
20475 break;
20478 else /* c == '%' */
20480 ptrdiff_t percent_position = offset;
20482 /* Get the specified minimum width. Zero means
20483 don't pad. */
20484 field = 0;
20485 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20486 field = field * 10 + c - '0';
20488 /* Don't pad beyond the total padding allowed. */
20489 if (field_width - n > 0 && field > field_width - n)
20490 field = field_width - n;
20492 /* Note that either PRECISION <= 0 or N < PRECISION. */
20493 prec = precision - n;
20495 if (c == 'M')
20496 n += display_mode_element (it, depth, field, prec,
20497 Vglobal_mode_string, props,
20498 risky);
20499 else if (c != 0)
20501 bool multibyte;
20502 ptrdiff_t bytepos, charpos;
20503 const char *spec;
20504 Lisp_Object string;
20506 bytepos = percent_position;
20507 charpos = (STRING_MULTIBYTE (elt)
20508 ? string_byte_to_char (elt, bytepos)
20509 : bytepos);
20510 spec = decode_mode_spec (it->w, c, field, &string);
20511 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20513 switch (mode_line_target)
20515 case MODE_LINE_NOPROP:
20516 case MODE_LINE_TITLE:
20517 n += store_mode_line_noprop (spec, field, prec);
20518 break;
20519 case MODE_LINE_STRING:
20521 Lisp_Object tem = build_string (spec);
20522 props = Ftext_properties_at (make_number (charpos), elt);
20523 /* Should only keep face property in props */
20524 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20526 break;
20527 case MODE_LINE_DISPLAY:
20529 int nglyphs_before, nwritten;
20531 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20532 nwritten = display_string (spec, string, elt,
20533 charpos, 0, it,
20534 field, prec, 0,
20535 multibyte);
20537 /* Assign to the glyphs written above the
20538 string where the `%x' came from, position
20539 of the `%'. */
20540 if (nwritten > 0)
20542 struct glyph *glyph
20543 = (it->glyph_row->glyphs[TEXT_AREA]
20544 + nglyphs_before);
20545 int i;
20547 for (i = 0; i < nwritten; ++i)
20549 glyph[i].object = elt;
20550 glyph[i].charpos = charpos;
20553 n += nwritten;
20556 break;
20559 else /* c == 0 */
20560 break;
20564 break;
20566 case Lisp_Symbol:
20567 /* A symbol: process the value of the symbol recursively
20568 as if it appeared here directly. Avoid error if symbol void.
20569 Special case: if value of symbol is a string, output the string
20570 literally. */
20572 register Lisp_Object tem;
20574 /* If the variable is not marked as risky to set
20575 then its contents are risky to use. */
20576 if (NILP (Fget (elt, Qrisky_local_variable)))
20577 risky = 1;
20579 tem = Fboundp (elt);
20580 if (!NILP (tem))
20582 tem = Fsymbol_value (elt);
20583 /* If value is a string, output that string literally:
20584 don't check for % within it. */
20585 if (STRINGP (tem))
20586 literal = 1;
20588 if (!EQ (tem, elt))
20590 /* Give up right away for nil or t. */
20591 elt = tem;
20592 goto tail_recurse;
20596 break;
20598 case Lisp_Cons:
20600 register Lisp_Object car, tem;
20602 /* A cons cell: five distinct cases.
20603 If first element is :eval or :propertize, do something special.
20604 If first element is a string or a cons, process all the elements
20605 and effectively concatenate them.
20606 If first element is a negative number, truncate displaying cdr to
20607 at most that many characters. If positive, pad (with spaces)
20608 to at least that many characters.
20609 If first element is a symbol, process the cadr or caddr recursively
20610 according to whether the symbol's value is non-nil or nil. */
20611 car = XCAR (elt);
20612 if (EQ (car, QCeval))
20614 /* An element of the form (:eval FORM) means evaluate FORM
20615 and use the result as mode line elements. */
20617 if (risky)
20618 break;
20620 if (CONSP (XCDR (elt)))
20622 Lisp_Object spec;
20623 spec = safe_eval (XCAR (XCDR (elt)));
20624 n += display_mode_element (it, depth, field_width - n,
20625 precision - n, spec, props,
20626 risky);
20629 else if (EQ (car, QCpropertize))
20631 /* An element of the form (:propertize ELT PROPS...)
20632 means display ELT but applying properties PROPS. */
20634 if (risky)
20635 break;
20637 if (CONSP (XCDR (elt)))
20638 n += display_mode_element (it, depth, field_width - n,
20639 precision - n, XCAR (XCDR (elt)),
20640 XCDR (XCDR (elt)), risky);
20642 else if (SYMBOLP (car))
20644 tem = Fboundp (car);
20645 elt = XCDR (elt);
20646 if (!CONSP (elt))
20647 goto invalid;
20648 /* elt is now the cdr, and we know it is a cons cell.
20649 Use its car if CAR has a non-nil value. */
20650 if (!NILP (tem))
20652 tem = Fsymbol_value (car);
20653 if (!NILP (tem))
20655 elt = XCAR (elt);
20656 goto tail_recurse;
20659 /* Symbol's value is nil (or symbol is unbound)
20660 Get the cddr of the original list
20661 and if possible find the caddr and use that. */
20662 elt = XCDR (elt);
20663 if (NILP (elt))
20664 break;
20665 else if (!CONSP (elt))
20666 goto invalid;
20667 elt = XCAR (elt);
20668 goto tail_recurse;
20670 else if (INTEGERP (car))
20672 register int lim = XINT (car);
20673 elt = XCDR (elt);
20674 if (lim < 0)
20676 /* Negative int means reduce maximum width. */
20677 if (precision <= 0)
20678 precision = -lim;
20679 else
20680 precision = min (precision, -lim);
20682 else if (lim > 0)
20684 /* Padding specified. Don't let it be more than
20685 current maximum. */
20686 if (precision > 0)
20687 lim = min (precision, lim);
20689 /* If that's more padding than already wanted, queue it.
20690 But don't reduce padding already specified even if
20691 that is beyond the current truncation point. */
20692 field_width = max (lim, field_width);
20694 goto tail_recurse;
20696 else if (STRINGP (car) || CONSP (car))
20698 Lisp_Object halftail = elt;
20699 int len = 0;
20701 while (CONSP (elt)
20702 && (precision <= 0 || n < precision))
20704 n += display_mode_element (it, depth,
20705 /* Do padding only after the last
20706 element in the list. */
20707 (! CONSP (XCDR (elt))
20708 ? field_width - n
20709 : 0),
20710 precision - n, XCAR (elt),
20711 props, risky);
20712 elt = XCDR (elt);
20713 len++;
20714 if ((len & 1) == 0)
20715 halftail = XCDR (halftail);
20716 /* Check for cycle. */
20717 if (EQ (halftail, elt))
20718 break;
20722 break;
20724 default:
20725 invalid:
20726 elt = build_string ("*invalid*");
20727 goto tail_recurse;
20730 /* Pad to FIELD_WIDTH. */
20731 if (field_width > 0 && n < field_width)
20733 switch (mode_line_target)
20735 case MODE_LINE_NOPROP:
20736 case MODE_LINE_TITLE:
20737 n += store_mode_line_noprop ("", field_width - n, 0);
20738 break;
20739 case MODE_LINE_STRING:
20740 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20741 break;
20742 case MODE_LINE_DISPLAY:
20743 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20744 0, 0, 0);
20745 break;
20749 return n;
20752 /* Store a mode-line string element in mode_line_string_list.
20754 If STRING is non-null, display that C string. Otherwise, the Lisp
20755 string LISP_STRING is displayed.
20757 FIELD_WIDTH is the minimum number of output glyphs to produce.
20758 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20759 with spaces. FIELD_WIDTH <= 0 means don't pad.
20761 PRECISION is the maximum number of characters to output from
20762 STRING. PRECISION <= 0 means don't truncate the string.
20764 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20765 properties to the string.
20767 PROPS are the properties to add to the string.
20768 The mode_line_string_face face property is always added to the string.
20771 static int
20772 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20773 int field_width, int precision, Lisp_Object props)
20775 ptrdiff_t len;
20776 int n = 0;
20778 if (string != NULL)
20780 len = strlen (string);
20781 if (precision > 0 && len > precision)
20782 len = precision;
20783 lisp_string = make_string (string, len);
20784 if (NILP (props))
20785 props = mode_line_string_face_prop;
20786 else if (!NILP (mode_line_string_face))
20788 Lisp_Object face = Fplist_get (props, Qface);
20789 props = Fcopy_sequence (props);
20790 if (NILP (face))
20791 face = mode_line_string_face;
20792 else
20793 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20794 props = Fplist_put (props, Qface, face);
20796 Fadd_text_properties (make_number (0), make_number (len),
20797 props, lisp_string);
20799 else
20801 len = XFASTINT (Flength (lisp_string));
20802 if (precision > 0 && len > precision)
20804 len = precision;
20805 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20806 precision = -1;
20808 if (!NILP (mode_line_string_face))
20810 Lisp_Object face;
20811 if (NILP (props))
20812 props = Ftext_properties_at (make_number (0), lisp_string);
20813 face = Fplist_get (props, Qface);
20814 if (NILP (face))
20815 face = mode_line_string_face;
20816 else
20817 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20818 props = Fcons (Qface, Fcons (face, Qnil));
20819 if (copy_string)
20820 lisp_string = Fcopy_sequence (lisp_string);
20822 if (!NILP (props))
20823 Fadd_text_properties (make_number (0), make_number (len),
20824 props, lisp_string);
20827 if (len > 0)
20829 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20830 n += len;
20833 if (field_width > len)
20835 field_width -= len;
20836 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20837 if (!NILP (props))
20838 Fadd_text_properties (make_number (0), make_number (field_width),
20839 props, lisp_string);
20840 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20841 n += field_width;
20844 return n;
20848 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20849 1, 4, 0,
20850 doc: /* Format a string out of a mode line format specification.
20851 First arg FORMAT specifies the mode line format (see `mode-line-format'
20852 for details) to use.
20854 By default, the format is evaluated for the currently selected window.
20856 Optional second arg FACE specifies the face property to put on all
20857 characters for which no face is specified. The value nil means the
20858 default face. The value t means whatever face the window's mode line
20859 currently uses (either `mode-line' or `mode-line-inactive',
20860 depending on whether the window is the selected window or not).
20861 An integer value means the value string has no text
20862 properties.
20864 Optional third and fourth args WINDOW and BUFFER specify the window
20865 and buffer to use as the context for the formatting (defaults
20866 are the selected window and the WINDOW's buffer). */)
20867 (Lisp_Object format, Lisp_Object face,
20868 Lisp_Object window, Lisp_Object buffer)
20870 struct it it;
20871 int len;
20872 struct window *w;
20873 struct buffer *old_buffer = NULL;
20874 int face_id;
20875 int no_props = INTEGERP (face);
20876 ptrdiff_t count = SPECPDL_INDEX ();
20877 Lisp_Object str;
20878 int string_start = 0;
20880 w = decode_any_window (window);
20881 XSETWINDOW (window, w);
20883 if (NILP (buffer))
20884 buffer = w->contents;
20885 CHECK_BUFFER (buffer);
20887 /* Make formatting the modeline a non-op when noninteractive, otherwise
20888 there will be problems later caused by a partially initialized frame. */
20889 if (NILP (format) || noninteractive)
20890 return empty_unibyte_string;
20892 if (no_props)
20893 face = Qnil;
20895 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20896 : EQ (face, Qt) ? (EQ (window, selected_window)
20897 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20898 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20899 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20900 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20901 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20902 : DEFAULT_FACE_ID;
20904 old_buffer = current_buffer;
20906 /* Save things including mode_line_proptrans_alist,
20907 and set that to nil so that we don't alter the outer value. */
20908 record_unwind_protect (unwind_format_mode_line,
20909 format_mode_line_unwind_data
20910 (XFRAME (WINDOW_FRAME (w)),
20911 old_buffer, selected_window, 1));
20912 mode_line_proptrans_alist = Qnil;
20914 Fselect_window (window, Qt);
20915 set_buffer_internal_1 (XBUFFER (buffer));
20917 init_iterator (&it, w, -1, -1, NULL, face_id);
20919 if (no_props)
20921 mode_line_target = MODE_LINE_NOPROP;
20922 mode_line_string_face_prop = Qnil;
20923 mode_line_string_list = Qnil;
20924 string_start = MODE_LINE_NOPROP_LEN (0);
20926 else
20928 mode_line_target = MODE_LINE_STRING;
20929 mode_line_string_list = Qnil;
20930 mode_line_string_face = face;
20931 mode_line_string_face_prop
20932 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20935 push_kboard (FRAME_KBOARD (it.f));
20936 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20937 pop_kboard ();
20939 if (no_props)
20941 len = MODE_LINE_NOPROP_LEN (string_start);
20942 str = make_string (mode_line_noprop_buf + string_start, len);
20944 else
20946 mode_line_string_list = Fnreverse (mode_line_string_list);
20947 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20948 empty_unibyte_string);
20951 unbind_to (count, Qnil);
20952 return str;
20955 /* Write a null-terminated, right justified decimal representation of
20956 the positive integer D to BUF using a minimal field width WIDTH. */
20958 static void
20959 pint2str (register char *buf, register int width, register ptrdiff_t d)
20961 register char *p = buf;
20963 if (d <= 0)
20964 *p++ = '0';
20965 else
20967 while (d > 0)
20969 *p++ = d % 10 + '0';
20970 d /= 10;
20974 for (width -= (int) (p - buf); width > 0; --width)
20975 *p++ = ' ';
20976 *p-- = '\0';
20977 while (p > buf)
20979 d = *buf;
20980 *buf++ = *p;
20981 *p-- = d;
20985 /* Write a null-terminated, right justified decimal and "human
20986 readable" representation of the nonnegative integer D to BUF using
20987 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20989 static const char power_letter[] =
20991 0, /* no letter */
20992 'k', /* kilo */
20993 'M', /* mega */
20994 'G', /* giga */
20995 'T', /* tera */
20996 'P', /* peta */
20997 'E', /* exa */
20998 'Z', /* zetta */
20999 'Y' /* yotta */
21002 static void
21003 pint2hrstr (char *buf, int width, ptrdiff_t d)
21005 /* We aim to represent the nonnegative integer D as
21006 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21007 ptrdiff_t quotient = d;
21008 int remainder = 0;
21009 /* -1 means: do not use TENTHS. */
21010 int tenths = -1;
21011 int exponent = 0;
21013 /* Length of QUOTIENT.TENTHS as a string. */
21014 int length;
21016 char * psuffix;
21017 char * p;
21019 if (quotient >= 1000)
21021 /* Scale to the appropriate EXPONENT. */
21024 remainder = quotient % 1000;
21025 quotient /= 1000;
21026 exponent++;
21028 while (quotient >= 1000);
21030 /* Round to nearest and decide whether to use TENTHS or not. */
21031 if (quotient <= 9)
21033 tenths = remainder / 100;
21034 if (remainder % 100 >= 50)
21036 if (tenths < 9)
21037 tenths++;
21038 else
21040 quotient++;
21041 if (quotient == 10)
21042 tenths = -1;
21043 else
21044 tenths = 0;
21048 else
21049 if (remainder >= 500)
21051 if (quotient < 999)
21052 quotient++;
21053 else
21055 quotient = 1;
21056 exponent++;
21057 tenths = 0;
21062 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21063 if (tenths == -1 && quotient <= 99)
21064 if (quotient <= 9)
21065 length = 1;
21066 else
21067 length = 2;
21068 else
21069 length = 3;
21070 p = psuffix = buf + max (width, length);
21072 /* Print EXPONENT. */
21073 *psuffix++ = power_letter[exponent];
21074 *psuffix = '\0';
21076 /* Print TENTHS. */
21077 if (tenths >= 0)
21079 *--p = '0' + tenths;
21080 *--p = '.';
21083 /* Print QUOTIENT. */
21086 int digit = quotient % 10;
21087 *--p = '0' + digit;
21089 while ((quotient /= 10) != 0);
21091 /* Print leading spaces. */
21092 while (buf < p)
21093 *--p = ' ';
21096 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21097 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21098 type of CODING_SYSTEM. Return updated pointer into BUF. */
21100 static unsigned char invalid_eol_type[] = "(*invalid*)";
21102 static char *
21103 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21105 Lisp_Object val;
21106 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21107 const unsigned char *eol_str;
21108 int eol_str_len;
21109 /* The EOL conversion we are using. */
21110 Lisp_Object eoltype;
21112 val = CODING_SYSTEM_SPEC (coding_system);
21113 eoltype = Qnil;
21115 if (!VECTORP (val)) /* Not yet decided. */
21117 *buf++ = multibyte ? '-' : ' ';
21118 if (eol_flag)
21119 eoltype = eol_mnemonic_undecided;
21120 /* Don't mention EOL conversion if it isn't decided. */
21122 else
21124 Lisp_Object attrs;
21125 Lisp_Object eolvalue;
21127 attrs = AREF (val, 0);
21128 eolvalue = AREF (val, 2);
21130 *buf++ = multibyte
21131 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21132 : ' ';
21134 if (eol_flag)
21136 /* The EOL conversion that is normal on this system. */
21138 if (NILP (eolvalue)) /* Not yet decided. */
21139 eoltype = eol_mnemonic_undecided;
21140 else if (VECTORP (eolvalue)) /* Not yet decided. */
21141 eoltype = eol_mnemonic_undecided;
21142 else /* eolvalue is Qunix, Qdos, or Qmac. */
21143 eoltype = (EQ (eolvalue, Qunix)
21144 ? eol_mnemonic_unix
21145 : (EQ (eolvalue, Qdos) == 1
21146 ? eol_mnemonic_dos : eol_mnemonic_mac));
21150 if (eol_flag)
21152 /* Mention the EOL conversion if it is not the usual one. */
21153 if (STRINGP (eoltype))
21155 eol_str = SDATA (eoltype);
21156 eol_str_len = SBYTES (eoltype);
21158 else if (CHARACTERP (eoltype))
21160 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21161 int c = XFASTINT (eoltype);
21162 eol_str_len = CHAR_STRING (c, tmp);
21163 eol_str = tmp;
21165 else
21167 eol_str = invalid_eol_type;
21168 eol_str_len = sizeof (invalid_eol_type) - 1;
21170 memcpy (buf, eol_str, eol_str_len);
21171 buf += eol_str_len;
21174 return buf;
21177 /* Return a string for the output of a mode line %-spec for window W,
21178 generated by character C. FIELD_WIDTH > 0 means pad the string
21179 returned with spaces to that value. Return a Lisp string in
21180 *STRING if the resulting string is taken from that Lisp string.
21182 Note we operate on the current buffer for most purposes. */
21184 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21186 static const char *
21187 decode_mode_spec (struct window *w, register int c, int field_width,
21188 Lisp_Object *string)
21190 Lisp_Object obj;
21191 struct frame *f = XFRAME (WINDOW_FRAME (w));
21192 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21193 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21194 produce strings from numerical values, so limit preposterously
21195 large values of FIELD_WIDTH to avoid overrunning the buffer's
21196 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21197 bytes plus the terminating null. */
21198 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21199 struct buffer *b = current_buffer;
21201 obj = Qnil;
21202 *string = Qnil;
21204 switch (c)
21206 case '*':
21207 if (!NILP (BVAR (b, read_only)))
21208 return "%";
21209 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21210 return "*";
21211 return "-";
21213 case '+':
21214 /* This differs from %* only for a modified read-only buffer. */
21215 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21216 return "*";
21217 if (!NILP (BVAR (b, read_only)))
21218 return "%";
21219 return "-";
21221 case '&':
21222 /* This differs from %* in ignoring read-only-ness. */
21223 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21224 return "*";
21225 return "-";
21227 case '%':
21228 return "%";
21230 case '[':
21232 int i;
21233 char *p;
21235 if (command_loop_level > 5)
21236 return "[[[... ";
21237 p = decode_mode_spec_buf;
21238 for (i = 0; i < command_loop_level; i++)
21239 *p++ = '[';
21240 *p = 0;
21241 return decode_mode_spec_buf;
21244 case ']':
21246 int i;
21247 char *p;
21249 if (command_loop_level > 5)
21250 return " ...]]]";
21251 p = decode_mode_spec_buf;
21252 for (i = 0; i < command_loop_level; i++)
21253 *p++ = ']';
21254 *p = 0;
21255 return decode_mode_spec_buf;
21258 case '-':
21260 register int i;
21262 /* Let lots_of_dashes be a string of infinite length. */
21263 if (mode_line_target == MODE_LINE_NOPROP
21264 || mode_line_target == MODE_LINE_STRING)
21265 return "--";
21266 if (field_width <= 0
21267 || field_width > sizeof (lots_of_dashes))
21269 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21270 decode_mode_spec_buf[i] = '-';
21271 decode_mode_spec_buf[i] = '\0';
21272 return decode_mode_spec_buf;
21274 else
21275 return lots_of_dashes;
21278 case 'b':
21279 obj = BVAR (b, name);
21280 break;
21282 case 'c':
21283 /* %c and %l are ignored in `frame-title-format'.
21284 (In redisplay_internal, the frame title is drawn _before_ the
21285 windows are updated, so the stuff which depends on actual
21286 window contents (such as %l) may fail to render properly, or
21287 even crash emacs.) */
21288 if (mode_line_target == MODE_LINE_TITLE)
21289 return "";
21290 else
21292 ptrdiff_t col = current_column ();
21293 w->column_number_displayed = col;
21294 pint2str (decode_mode_spec_buf, width, col);
21295 return decode_mode_spec_buf;
21298 case 'e':
21299 #ifndef SYSTEM_MALLOC
21301 if (NILP (Vmemory_full))
21302 return "";
21303 else
21304 return "!MEM FULL! ";
21306 #else
21307 return "";
21308 #endif
21310 case 'F':
21311 /* %F displays the frame name. */
21312 if (!NILP (f->title))
21313 return SSDATA (f->title);
21314 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21315 return SSDATA (f->name);
21316 return "Emacs";
21318 case 'f':
21319 obj = BVAR (b, filename);
21320 break;
21322 case 'i':
21324 ptrdiff_t size = ZV - BEGV;
21325 pint2str (decode_mode_spec_buf, width, size);
21326 return decode_mode_spec_buf;
21329 case 'I':
21331 ptrdiff_t size = ZV - BEGV;
21332 pint2hrstr (decode_mode_spec_buf, width, size);
21333 return decode_mode_spec_buf;
21336 case 'l':
21338 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21339 ptrdiff_t topline, nlines, height;
21340 ptrdiff_t junk;
21342 /* %c and %l are ignored in `frame-title-format'. */
21343 if (mode_line_target == MODE_LINE_TITLE)
21344 return "";
21346 startpos = marker_position (w->start);
21347 startpos_byte = marker_byte_position (w->start);
21348 height = WINDOW_TOTAL_LINES (w);
21350 /* If we decided that this buffer isn't suitable for line numbers,
21351 don't forget that too fast. */
21352 if (w->base_line_pos == -1)
21353 goto no_value;
21355 /* If the buffer is very big, don't waste time. */
21356 if (INTEGERP (Vline_number_display_limit)
21357 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21359 w->base_line_pos = 0;
21360 w->base_line_number = 0;
21361 goto no_value;
21364 if (w->base_line_number > 0
21365 && w->base_line_pos > 0
21366 && w->base_line_pos <= startpos)
21368 line = w->base_line_number;
21369 linepos = w->base_line_pos;
21370 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21372 else
21374 line = 1;
21375 linepos = BUF_BEGV (b);
21376 linepos_byte = BUF_BEGV_BYTE (b);
21379 /* Count lines from base line to window start position. */
21380 nlines = display_count_lines (linepos_byte,
21381 startpos_byte,
21382 startpos, &junk);
21384 topline = nlines + line;
21386 /* Determine a new base line, if the old one is too close
21387 or too far away, or if we did not have one.
21388 "Too close" means it's plausible a scroll-down would
21389 go back past it. */
21390 if (startpos == BUF_BEGV (b))
21392 w->base_line_number = topline;
21393 w->base_line_pos = BUF_BEGV (b);
21395 else if (nlines < height + 25 || nlines > height * 3 + 50
21396 || linepos == BUF_BEGV (b))
21398 ptrdiff_t limit = BUF_BEGV (b);
21399 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21400 ptrdiff_t position;
21401 ptrdiff_t distance =
21402 (height * 2 + 30) * line_number_display_limit_width;
21404 if (startpos - distance > limit)
21406 limit = startpos - distance;
21407 limit_byte = CHAR_TO_BYTE (limit);
21410 nlines = display_count_lines (startpos_byte,
21411 limit_byte,
21412 - (height * 2 + 30),
21413 &position);
21414 /* If we couldn't find the lines we wanted within
21415 line_number_display_limit_width chars per line,
21416 give up on line numbers for this window. */
21417 if (position == limit_byte && limit == startpos - distance)
21419 w->base_line_pos = -1;
21420 w->base_line_number = 0;
21421 goto no_value;
21424 w->base_line_number = topline - nlines;
21425 w->base_line_pos = BYTE_TO_CHAR (position);
21428 /* Now count lines from the start pos to point. */
21429 nlines = display_count_lines (startpos_byte,
21430 PT_BYTE, PT, &junk);
21432 /* Record that we did display the line number. */
21433 line_number_displayed = 1;
21435 /* Make the string to show. */
21436 pint2str (decode_mode_spec_buf, width, topline + nlines);
21437 return decode_mode_spec_buf;
21438 no_value:
21440 char* p = decode_mode_spec_buf;
21441 int pad = width - 2;
21442 while (pad-- > 0)
21443 *p++ = ' ';
21444 *p++ = '?';
21445 *p++ = '?';
21446 *p = '\0';
21447 return decode_mode_spec_buf;
21450 break;
21452 case 'm':
21453 obj = BVAR (b, mode_name);
21454 break;
21456 case 'n':
21457 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21458 return " Narrow";
21459 break;
21461 case 'p':
21463 ptrdiff_t pos = marker_position (w->start);
21464 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21466 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21468 if (pos <= BUF_BEGV (b))
21469 return "All";
21470 else
21471 return "Bottom";
21473 else if (pos <= BUF_BEGV (b))
21474 return "Top";
21475 else
21477 if (total > 1000000)
21478 /* Do it differently for a large value, to avoid overflow. */
21479 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21480 else
21481 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21482 /* We can't normally display a 3-digit number,
21483 so get us a 2-digit number that is close. */
21484 if (total == 100)
21485 total = 99;
21486 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21487 return decode_mode_spec_buf;
21491 /* Display percentage of size above the bottom of the screen. */
21492 case 'P':
21494 ptrdiff_t toppos = marker_position (w->start);
21495 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21496 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21498 if (botpos >= BUF_ZV (b))
21500 if (toppos <= BUF_BEGV (b))
21501 return "All";
21502 else
21503 return "Bottom";
21505 else
21507 if (total > 1000000)
21508 /* Do it differently for a large value, to avoid overflow. */
21509 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21510 else
21511 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21512 /* We can't normally display a 3-digit number,
21513 so get us a 2-digit number that is close. */
21514 if (total == 100)
21515 total = 99;
21516 if (toppos <= BUF_BEGV (b))
21517 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21518 else
21519 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21520 return decode_mode_spec_buf;
21524 case 's':
21525 /* status of process */
21526 obj = Fget_buffer_process (Fcurrent_buffer ());
21527 if (NILP (obj))
21528 return "no process";
21529 #ifndef MSDOS
21530 obj = Fsymbol_name (Fprocess_status (obj));
21531 #endif
21532 break;
21534 case '@':
21536 ptrdiff_t count = inhibit_garbage_collection ();
21537 Lisp_Object val = call1 (intern ("file-remote-p"),
21538 BVAR (current_buffer, directory));
21539 unbind_to (count, Qnil);
21541 if (NILP (val))
21542 return "-";
21543 else
21544 return "@";
21547 case 'z':
21548 /* coding-system (not including end-of-line format) */
21549 case 'Z':
21550 /* coding-system (including end-of-line type) */
21552 int eol_flag = (c == 'Z');
21553 char *p = decode_mode_spec_buf;
21555 if (! FRAME_WINDOW_P (f))
21557 /* No need to mention EOL here--the terminal never needs
21558 to do EOL conversion. */
21559 p = decode_mode_spec_coding (CODING_ID_NAME
21560 (FRAME_KEYBOARD_CODING (f)->id),
21561 p, 0);
21562 p = decode_mode_spec_coding (CODING_ID_NAME
21563 (FRAME_TERMINAL_CODING (f)->id),
21564 p, 0);
21566 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21567 p, eol_flag);
21569 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21570 #ifdef subprocesses
21571 obj = Fget_buffer_process (Fcurrent_buffer ());
21572 if (PROCESSP (obj))
21574 p = decode_mode_spec_coding
21575 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21576 p = decode_mode_spec_coding
21577 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21579 #endif /* subprocesses */
21580 #endif /* 0 */
21581 *p = 0;
21582 return decode_mode_spec_buf;
21586 if (STRINGP (obj))
21588 *string = obj;
21589 return SSDATA (obj);
21591 else
21592 return "";
21596 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
21597 means count lines back from START_BYTE. But don't go beyond
21598 LIMIT_BYTE. Return the number of lines thus found (always
21599 nonnegative).
21601 Set *BYTE_POS_PTR to the byte position where we stopped. This is
21602 either the position COUNT lines after/before START_BYTE, if we
21603 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
21604 COUNT lines. */
21606 static ptrdiff_t
21607 display_count_lines (ptrdiff_t start_byte,
21608 ptrdiff_t limit_byte, ptrdiff_t count,
21609 ptrdiff_t *byte_pos_ptr)
21611 register unsigned char *cursor;
21612 unsigned char *base;
21614 register ptrdiff_t ceiling;
21615 register unsigned char *ceiling_addr;
21616 ptrdiff_t orig_count = count;
21618 /* If we are not in selective display mode,
21619 check only for newlines. */
21620 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21621 && !INTEGERP (BVAR (current_buffer, selective_display)));
21623 if (count > 0)
21625 while (start_byte < limit_byte)
21627 ceiling = BUFFER_CEILING_OF (start_byte);
21628 ceiling = min (limit_byte - 1, ceiling);
21629 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21630 base = (cursor = BYTE_POS_ADDR (start_byte));
21634 if (selective_display)
21636 while (*cursor != '\n' && *cursor != 015
21637 && ++cursor != ceiling_addr)
21638 continue;
21639 if (cursor == ceiling_addr)
21640 break;
21642 else
21644 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
21645 if (! cursor)
21646 break;
21649 cursor++;
21651 if (--count == 0)
21653 start_byte += cursor - base;
21654 *byte_pos_ptr = start_byte;
21655 return orig_count;
21658 while (cursor < ceiling_addr);
21660 start_byte += ceiling_addr - base;
21663 else
21665 while (start_byte > limit_byte)
21667 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21668 ceiling = max (limit_byte, ceiling);
21669 ceiling_addr = BYTE_POS_ADDR (ceiling);
21670 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21671 while (1)
21673 if (selective_display)
21675 while (--cursor >= ceiling_addr
21676 && *cursor != '\n' && *cursor != 015)
21677 continue;
21678 if (cursor < ceiling_addr)
21679 break;
21681 else
21683 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
21684 if (! cursor)
21685 break;
21688 if (++count == 0)
21690 start_byte += cursor - base + 1;
21691 *byte_pos_ptr = start_byte;
21692 /* When scanning backwards, we should
21693 not count the newline posterior to which we stop. */
21694 return - orig_count - 1;
21697 start_byte += ceiling_addr - base;
21701 *byte_pos_ptr = limit_byte;
21703 if (count < 0)
21704 return - orig_count + count;
21705 return orig_count - count;
21711 /***********************************************************************
21712 Displaying strings
21713 ***********************************************************************/
21715 /* Display a NUL-terminated string, starting with index START.
21717 If STRING is non-null, display that C string. Otherwise, the Lisp
21718 string LISP_STRING is displayed. There's a case that STRING is
21719 non-null and LISP_STRING is not nil. It means STRING is a string
21720 data of LISP_STRING. In that case, we display LISP_STRING while
21721 ignoring its text properties.
21723 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21724 FACE_STRING. Display STRING or LISP_STRING with the face at
21725 FACE_STRING_POS in FACE_STRING:
21727 Display the string in the environment given by IT, but use the
21728 standard display table, temporarily.
21730 FIELD_WIDTH is the minimum number of output glyphs to produce.
21731 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21732 with spaces. If STRING has more characters, more than FIELD_WIDTH
21733 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21735 PRECISION is the maximum number of characters to output from
21736 STRING. PRECISION < 0 means don't truncate the string.
21738 This is roughly equivalent to printf format specifiers:
21740 FIELD_WIDTH PRECISION PRINTF
21741 ----------------------------------------
21742 -1 -1 %s
21743 -1 10 %.10s
21744 10 -1 %10s
21745 20 10 %20.10s
21747 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21748 display them, and < 0 means obey the current buffer's value of
21749 enable_multibyte_characters.
21751 Value is the number of columns displayed. */
21753 static int
21754 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21755 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21756 int field_width, int precision, int max_x, int multibyte)
21758 int hpos_at_start = it->hpos;
21759 int saved_face_id = it->face_id;
21760 struct glyph_row *row = it->glyph_row;
21761 ptrdiff_t it_charpos;
21763 /* Initialize the iterator IT for iteration over STRING beginning
21764 with index START. */
21765 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21766 precision, field_width, multibyte);
21767 if (string && STRINGP (lisp_string))
21768 /* LISP_STRING is the one returned by decode_mode_spec. We should
21769 ignore its text properties. */
21770 it->stop_charpos = it->end_charpos;
21772 /* If displaying STRING, set up the face of the iterator from
21773 FACE_STRING, if that's given. */
21774 if (STRINGP (face_string))
21776 ptrdiff_t endptr;
21777 struct face *face;
21779 it->face_id
21780 = face_at_string_position (it->w, face_string, face_string_pos,
21781 0, it->region_beg_charpos,
21782 it->region_end_charpos,
21783 &endptr, it->base_face_id, 0);
21784 face = FACE_FROM_ID (it->f, it->face_id);
21785 it->face_box_p = face->box != FACE_NO_BOX;
21788 /* Set max_x to the maximum allowed X position. Don't let it go
21789 beyond the right edge of the window. */
21790 if (max_x <= 0)
21791 max_x = it->last_visible_x;
21792 else
21793 max_x = min (max_x, it->last_visible_x);
21795 /* Skip over display elements that are not visible. because IT->w is
21796 hscrolled. */
21797 if (it->current_x < it->first_visible_x)
21798 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21799 MOVE_TO_POS | MOVE_TO_X);
21801 row->ascent = it->max_ascent;
21802 row->height = it->max_ascent + it->max_descent;
21803 row->phys_ascent = it->max_phys_ascent;
21804 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21805 row->extra_line_spacing = it->max_extra_line_spacing;
21807 if (STRINGP (it->string))
21808 it_charpos = IT_STRING_CHARPOS (*it);
21809 else
21810 it_charpos = IT_CHARPOS (*it);
21812 /* This condition is for the case that we are called with current_x
21813 past last_visible_x. */
21814 while (it->current_x < max_x)
21816 int x_before, x, n_glyphs_before, i, nglyphs;
21818 /* Get the next display element. */
21819 if (!get_next_display_element (it))
21820 break;
21822 /* Produce glyphs. */
21823 x_before = it->current_x;
21824 n_glyphs_before = row->used[TEXT_AREA];
21825 PRODUCE_GLYPHS (it);
21827 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21828 i = 0;
21829 x = x_before;
21830 while (i < nglyphs)
21832 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21834 if (it->line_wrap != TRUNCATE
21835 && x + glyph->pixel_width > max_x)
21837 /* End of continued line or max_x reached. */
21838 if (CHAR_GLYPH_PADDING_P (*glyph))
21840 /* A wide character is unbreakable. */
21841 if (row->reversed_p)
21842 unproduce_glyphs (it, row->used[TEXT_AREA]
21843 - n_glyphs_before);
21844 row->used[TEXT_AREA] = n_glyphs_before;
21845 it->current_x = x_before;
21847 else
21849 if (row->reversed_p)
21850 unproduce_glyphs (it, row->used[TEXT_AREA]
21851 - (n_glyphs_before + i));
21852 row->used[TEXT_AREA] = n_glyphs_before + i;
21853 it->current_x = x;
21855 break;
21857 else if (x + glyph->pixel_width >= it->first_visible_x)
21859 /* Glyph is at least partially visible. */
21860 ++it->hpos;
21861 if (x < it->first_visible_x)
21862 row->x = x - it->first_visible_x;
21864 else
21866 /* Glyph is off the left margin of the display area.
21867 Should not happen. */
21868 emacs_abort ();
21871 row->ascent = max (row->ascent, it->max_ascent);
21872 row->height = max (row->height, it->max_ascent + it->max_descent);
21873 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21874 row->phys_height = max (row->phys_height,
21875 it->max_phys_ascent + it->max_phys_descent);
21876 row->extra_line_spacing = max (row->extra_line_spacing,
21877 it->max_extra_line_spacing);
21878 x += glyph->pixel_width;
21879 ++i;
21882 /* Stop if max_x reached. */
21883 if (i < nglyphs)
21884 break;
21886 /* Stop at line ends. */
21887 if (ITERATOR_AT_END_OF_LINE_P (it))
21889 it->continuation_lines_width = 0;
21890 break;
21893 set_iterator_to_next (it, 1);
21894 if (STRINGP (it->string))
21895 it_charpos = IT_STRING_CHARPOS (*it);
21896 else
21897 it_charpos = IT_CHARPOS (*it);
21899 /* Stop if truncating at the right edge. */
21900 if (it->line_wrap == TRUNCATE
21901 && it->current_x >= it->last_visible_x)
21903 /* Add truncation mark, but don't do it if the line is
21904 truncated at a padding space. */
21905 if (it_charpos < it->string_nchars)
21907 if (!FRAME_WINDOW_P (it->f))
21909 int ii, n;
21911 if (it->current_x > it->last_visible_x)
21913 if (!row->reversed_p)
21915 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21916 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21917 break;
21919 else
21921 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21922 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21923 break;
21924 unproduce_glyphs (it, ii + 1);
21925 ii = row->used[TEXT_AREA] - (ii + 1);
21927 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21929 row->used[TEXT_AREA] = ii;
21930 produce_special_glyphs (it, IT_TRUNCATION);
21933 produce_special_glyphs (it, IT_TRUNCATION);
21935 row->truncated_on_right_p = 1;
21937 break;
21941 /* Maybe insert a truncation at the left. */
21942 if (it->first_visible_x
21943 && it_charpos > 0)
21945 if (!FRAME_WINDOW_P (it->f)
21946 || (row->reversed_p
21947 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21948 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21949 insert_left_trunc_glyphs (it);
21950 row->truncated_on_left_p = 1;
21953 it->face_id = saved_face_id;
21955 /* Value is number of columns displayed. */
21956 return it->hpos - hpos_at_start;
21961 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21962 appears as an element of LIST or as the car of an element of LIST.
21963 If PROPVAL is a list, compare each element against LIST in that
21964 way, and return 1/2 if any element of PROPVAL is found in LIST.
21965 Otherwise return 0. This function cannot quit.
21966 The return value is 2 if the text is invisible but with an ellipsis
21967 and 1 if it's invisible and without an ellipsis. */
21970 invisible_p (register Lisp_Object propval, Lisp_Object list)
21972 register Lisp_Object tail, proptail;
21974 for (tail = list; CONSP (tail); tail = XCDR (tail))
21976 register Lisp_Object tem;
21977 tem = XCAR (tail);
21978 if (EQ (propval, tem))
21979 return 1;
21980 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21981 return NILP (XCDR (tem)) ? 1 : 2;
21984 if (CONSP (propval))
21986 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21988 Lisp_Object propelt;
21989 propelt = XCAR (proptail);
21990 for (tail = list; CONSP (tail); tail = XCDR (tail))
21992 register Lisp_Object tem;
21993 tem = XCAR (tail);
21994 if (EQ (propelt, tem))
21995 return 1;
21996 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21997 return NILP (XCDR (tem)) ? 1 : 2;
22002 return 0;
22005 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22006 doc: /* Non-nil if the property makes the text invisible.
22007 POS-OR-PROP can be a marker or number, in which case it is taken to be
22008 a position in the current buffer and the value of the `invisible' property
22009 is checked; or it can be some other value, which is then presumed to be the
22010 value of the `invisible' property of the text of interest.
22011 The non-nil value returned can be t for truly invisible text or something
22012 else if the text is replaced by an ellipsis. */)
22013 (Lisp_Object pos_or_prop)
22015 Lisp_Object prop
22016 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22017 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22018 : pos_or_prop);
22019 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22020 return (invis == 0 ? Qnil
22021 : invis == 1 ? Qt
22022 : make_number (invis));
22025 /* Calculate a width or height in pixels from a specification using
22026 the following elements:
22028 SPEC ::=
22029 NUM - a (fractional) multiple of the default font width/height
22030 (NUM) - specifies exactly NUM pixels
22031 UNIT - a fixed number of pixels, see below.
22032 ELEMENT - size of a display element in pixels, see below.
22033 (NUM . SPEC) - equals NUM * SPEC
22034 (+ SPEC SPEC ...) - add pixel values
22035 (- SPEC SPEC ...) - subtract pixel values
22036 (- SPEC) - negate pixel value
22038 NUM ::=
22039 INT or FLOAT - a number constant
22040 SYMBOL - use symbol's (buffer local) variable binding.
22042 UNIT ::=
22043 in - pixels per inch *)
22044 mm - pixels per 1/1000 meter *)
22045 cm - pixels per 1/100 meter *)
22046 width - width of current font in pixels.
22047 height - height of current font in pixels.
22049 *) using the ratio(s) defined in display-pixels-per-inch.
22051 ELEMENT ::=
22053 left-fringe - left fringe width in pixels
22054 right-fringe - right fringe width in pixels
22056 left-margin - left margin width in pixels
22057 right-margin - right margin width in pixels
22059 scroll-bar - scroll-bar area width in pixels
22061 Examples:
22063 Pixels corresponding to 5 inches:
22064 (5 . in)
22066 Total width of non-text areas on left side of window (if scroll-bar is on left):
22067 '(space :width (+ left-fringe left-margin scroll-bar))
22069 Align to first text column (in header line):
22070 '(space :align-to 0)
22072 Align to middle of text area minus half the width of variable `my-image'
22073 containing a loaded image:
22074 '(space :align-to (0.5 . (- text my-image)))
22076 Width of left margin minus width of 1 character in the default font:
22077 '(space :width (- left-margin 1))
22079 Width of left margin minus width of 2 characters in the current font:
22080 '(space :width (- left-margin (2 . width)))
22082 Center 1 character over left-margin (in header line):
22083 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22085 Different ways to express width of left fringe plus left margin minus one pixel:
22086 '(space :width (- (+ left-fringe left-margin) (1)))
22087 '(space :width (+ left-fringe left-margin (- (1))))
22088 '(space :width (+ left-fringe left-margin (-1)))
22092 static int
22093 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22094 struct font *font, int width_p, int *align_to)
22096 double pixels;
22098 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22099 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22101 if (NILP (prop))
22102 return OK_PIXELS (0);
22104 eassert (FRAME_LIVE_P (it->f));
22106 if (SYMBOLP (prop))
22108 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22110 char *unit = SSDATA (SYMBOL_NAME (prop));
22112 if (unit[0] == 'i' && unit[1] == 'n')
22113 pixels = 1.0;
22114 else if (unit[0] == 'm' && unit[1] == 'm')
22115 pixels = 25.4;
22116 else if (unit[0] == 'c' && unit[1] == 'm')
22117 pixels = 2.54;
22118 else
22119 pixels = 0;
22120 if (pixels > 0)
22122 double ppi = (width_p ? FRAME_RES_X (it->f)
22123 : FRAME_RES_Y (it->f));
22125 if (ppi > 0)
22126 return OK_PIXELS (ppi / pixels);
22127 return 0;
22131 #ifdef HAVE_WINDOW_SYSTEM
22132 if (EQ (prop, Qheight))
22133 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22134 if (EQ (prop, Qwidth))
22135 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22136 #else
22137 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22138 return OK_PIXELS (1);
22139 #endif
22141 if (EQ (prop, Qtext))
22142 return OK_PIXELS (width_p
22143 ? window_box_width (it->w, TEXT_AREA)
22144 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22146 if (align_to && *align_to < 0)
22148 *res = 0;
22149 if (EQ (prop, Qleft))
22150 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22151 if (EQ (prop, Qright))
22152 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22153 if (EQ (prop, Qcenter))
22154 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22155 + window_box_width (it->w, TEXT_AREA) / 2);
22156 if (EQ (prop, Qleft_fringe))
22157 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22158 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22159 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22160 if (EQ (prop, Qright_fringe))
22161 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22162 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22163 : window_box_right_offset (it->w, TEXT_AREA));
22164 if (EQ (prop, Qleft_margin))
22165 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22166 if (EQ (prop, Qright_margin))
22167 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22168 if (EQ (prop, Qscroll_bar))
22169 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22171 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22172 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22173 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22174 : 0)));
22176 else
22178 if (EQ (prop, Qleft_fringe))
22179 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22180 if (EQ (prop, Qright_fringe))
22181 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22182 if (EQ (prop, Qleft_margin))
22183 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22184 if (EQ (prop, Qright_margin))
22185 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22186 if (EQ (prop, Qscroll_bar))
22187 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22190 prop = buffer_local_value_1 (prop, it->w->contents);
22191 if (EQ (prop, Qunbound))
22192 prop = Qnil;
22195 if (INTEGERP (prop) || FLOATP (prop))
22197 int base_unit = (width_p
22198 ? FRAME_COLUMN_WIDTH (it->f)
22199 : FRAME_LINE_HEIGHT (it->f));
22200 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22203 if (CONSP (prop))
22205 Lisp_Object car = XCAR (prop);
22206 Lisp_Object cdr = XCDR (prop);
22208 if (SYMBOLP (car))
22210 #ifdef HAVE_WINDOW_SYSTEM
22211 if (FRAME_WINDOW_P (it->f)
22212 && valid_image_p (prop))
22214 ptrdiff_t id = lookup_image (it->f, prop);
22215 struct image *img = IMAGE_FROM_ID (it->f, id);
22217 return OK_PIXELS (width_p ? img->width : img->height);
22219 #endif
22220 if (EQ (car, Qplus) || EQ (car, Qminus))
22222 int first = 1;
22223 double px;
22225 pixels = 0;
22226 while (CONSP (cdr))
22228 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22229 font, width_p, align_to))
22230 return 0;
22231 if (first)
22232 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22233 else
22234 pixels += px;
22235 cdr = XCDR (cdr);
22237 if (EQ (car, Qminus))
22238 pixels = -pixels;
22239 return OK_PIXELS (pixels);
22242 car = buffer_local_value_1 (car, it->w->contents);
22243 if (EQ (car, Qunbound))
22244 car = Qnil;
22247 if (INTEGERP (car) || FLOATP (car))
22249 double fact;
22250 pixels = XFLOATINT (car);
22251 if (NILP (cdr))
22252 return OK_PIXELS (pixels);
22253 if (calc_pixel_width_or_height (&fact, it, cdr,
22254 font, width_p, align_to))
22255 return OK_PIXELS (pixels * fact);
22256 return 0;
22259 return 0;
22262 return 0;
22266 /***********************************************************************
22267 Glyph Display
22268 ***********************************************************************/
22270 #ifdef HAVE_WINDOW_SYSTEM
22272 #ifdef GLYPH_DEBUG
22274 void
22275 dump_glyph_string (struct glyph_string *s)
22277 fprintf (stderr, "glyph string\n");
22278 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22279 s->x, s->y, s->width, s->height);
22280 fprintf (stderr, " ybase = %d\n", s->ybase);
22281 fprintf (stderr, " hl = %d\n", s->hl);
22282 fprintf (stderr, " left overhang = %d, right = %d\n",
22283 s->left_overhang, s->right_overhang);
22284 fprintf (stderr, " nchars = %d\n", s->nchars);
22285 fprintf (stderr, " extends to end of line = %d\n",
22286 s->extends_to_end_of_line_p);
22287 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22288 fprintf (stderr, " bg width = %d\n", s->background_width);
22291 #endif /* GLYPH_DEBUG */
22293 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22294 of XChar2b structures for S; it can't be allocated in
22295 init_glyph_string because it must be allocated via `alloca'. W
22296 is the window on which S is drawn. ROW and AREA are the glyph row
22297 and area within the row from which S is constructed. START is the
22298 index of the first glyph structure covered by S. HL is a
22299 face-override for drawing S. */
22301 #ifdef HAVE_NTGUI
22302 #define OPTIONAL_HDC(hdc) HDC hdc,
22303 #define DECLARE_HDC(hdc) HDC hdc;
22304 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22305 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22306 #endif
22308 #ifndef OPTIONAL_HDC
22309 #define OPTIONAL_HDC(hdc)
22310 #define DECLARE_HDC(hdc)
22311 #define ALLOCATE_HDC(hdc, f)
22312 #define RELEASE_HDC(hdc, f)
22313 #endif
22315 static void
22316 init_glyph_string (struct glyph_string *s,
22317 OPTIONAL_HDC (hdc)
22318 XChar2b *char2b, struct window *w, struct glyph_row *row,
22319 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22321 memset (s, 0, sizeof *s);
22322 s->w = w;
22323 s->f = XFRAME (w->frame);
22324 #ifdef HAVE_NTGUI
22325 s->hdc = hdc;
22326 #endif
22327 s->display = FRAME_X_DISPLAY (s->f);
22328 s->window = FRAME_X_WINDOW (s->f);
22329 s->char2b = char2b;
22330 s->hl = hl;
22331 s->row = row;
22332 s->area = area;
22333 s->first_glyph = row->glyphs[area] + start;
22334 s->height = row->height;
22335 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22336 s->ybase = s->y + row->ascent;
22340 /* Append the list of glyph strings with head H and tail T to the list
22341 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22343 static void
22344 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22345 struct glyph_string *h, struct glyph_string *t)
22347 if (h)
22349 if (*head)
22350 (*tail)->next = h;
22351 else
22352 *head = h;
22353 h->prev = *tail;
22354 *tail = t;
22359 /* Prepend the list of glyph strings with head H and tail T to the
22360 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22361 result. */
22363 static void
22364 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22365 struct glyph_string *h, struct glyph_string *t)
22367 if (h)
22369 if (*head)
22370 (*head)->prev = t;
22371 else
22372 *tail = t;
22373 t->next = *head;
22374 *head = h;
22379 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22380 Set *HEAD and *TAIL to the resulting list. */
22382 static void
22383 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22384 struct glyph_string *s)
22386 s->next = s->prev = NULL;
22387 append_glyph_string_lists (head, tail, s, s);
22391 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22392 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22393 make sure that X resources for the face returned are allocated.
22394 Value is a pointer to a realized face that is ready for display if
22395 DISPLAY_P is non-zero. */
22397 static struct face *
22398 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22399 XChar2b *char2b, int display_p)
22401 struct face *face = FACE_FROM_ID (f, face_id);
22402 unsigned code = 0;
22404 if (face->font)
22406 code = face->font->driver->encode_char (face->font, c);
22408 if (code == FONT_INVALID_CODE)
22409 code = 0;
22411 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22413 /* Make sure X resources of the face are allocated. */
22414 #ifdef HAVE_X_WINDOWS
22415 if (display_p)
22416 #endif
22418 eassert (face != NULL);
22419 PREPARE_FACE_FOR_DISPLAY (f, face);
22422 return face;
22426 /* Get face and two-byte form of character glyph GLYPH on frame F.
22427 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22428 a pointer to a realized face that is ready for display. */
22430 static struct face *
22431 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22432 XChar2b *char2b, int *two_byte_p)
22434 struct face *face;
22435 unsigned code = 0;
22437 eassert (glyph->type == CHAR_GLYPH);
22438 face = FACE_FROM_ID (f, glyph->face_id);
22440 /* Make sure X resources of the face are allocated. */
22441 eassert (face != NULL);
22442 PREPARE_FACE_FOR_DISPLAY (f, face);
22444 if (two_byte_p)
22445 *two_byte_p = 0;
22447 if (face->font)
22449 if (CHAR_BYTE8_P (glyph->u.ch))
22450 code = CHAR_TO_BYTE8 (glyph->u.ch);
22451 else
22452 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22454 if (code == FONT_INVALID_CODE)
22455 code = 0;
22458 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22459 return face;
22463 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22464 Return 1 if FONT has a glyph for C, otherwise return 0. */
22466 static int
22467 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22469 unsigned code;
22471 if (CHAR_BYTE8_P (c))
22472 code = CHAR_TO_BYTE8 (c);
22473 else
22474 code = font->driver->encode_char (font, c);
22476 if (code == FONT_INVALID_CODE)
22477 return 0;
22478 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22479 return 1;
22483 /* Fill glyph string S with composition components specified by S->cmp.
22485 BASE_FACE is the base face of the composition.
22486 S->cmp_from is the index of the first component for S.
22488 OVERLAPS non-zero means S should draw the foreground only, and use
22489 its physical height for clipping. See also draw_glyphs.
22491 Value is the index of a component not in S. */
22493 static int
22494 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22495 int overlaps)
22497 int i;
22498 /* For all glyphs of this composition, starting at the offset
22499 S->cmp_from, until we reach the end of the definition or encounter a
22500 glyph that requires the different face, add it to S. */
22501 struct face *face;
22503 eassert (s);
22505 s->for_overlaps = overlaps;
22506 s->face = NULL;
22507 s->font = NULL;
22508 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22510 int c = COMPOSITION_GLYPH (s->cmp, i);
22512 /* TAB in a composition means display glyphs with padding space
22513 on the left or right. */
22514 if (c != '\t')
22516 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22517 -1, Qnil);
22519 face = get_char_face_and_encoding (s->f, c, face_id,
22520 s->char2b + i, 1);
22521 if (face)
22523 if (! s->face)
22525 s->face = face;
22526 s->font = s->face->font;
22528 else if (s->face != face)
22529 break;
22532 ++s->nchars;
22534 s->cmp_to = i;
22536 if (s->face == NULL)
22538 s->face = base_face->ascii_face;
22539 s->font = s->face->font;
22542 /* All glyph strings for the same composition has the same width,
22543 i.e. the width set for the first component of the composition. */
22544 s->width = s->first_glyph->pixel_width;
22546 /* If the specified font could not be loaded, use the frame's
22547 default font, but record the fact that we couldn't load it in
22548 the glyph string so that we can draw rectangles for the
22549 characters of the glyph string. */
22550 if (s->font == NULL)
22552 s->font_not_found_p = 1;
22553 s->font = FRAME_FONT (s->f);
22556 /* Adjust base line for subscript/superscript text. */
22557 s->ybase += s->first_glyph->voffset;
22559 /* This glyph string must always be drawn with 16-bit functions. */
22560 s->two_byte_p = 1;
22562 return s->cmp_to;
22565 static int
22566 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22567 int start, int end, int overlaps)
22569 struct glyph *glyph, *last;
22570 Lisp_Object lgstring;
22571 int i;
22573 s->for_overlaps = overlaps;
22574 glyph = s->row->glyphs[s->area] + start;
22575 last = s->row->glyphs[s->area] + end;
22576 s->cmp_id = glyph->u.cmp.id;
22577 s->cmp_from = glyph->slice.cmp.from;
22578 s->cmp_to = glyph->slice.cmp.to + 1;
22579 s->face = FACE_FROM_ID (s->f, face_id);
22580 lgstring = composition_gstring_from_id (s->cmp_id);
22581 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22582 glyph++;
22583 while (glyph < last
22584 && glyph->u.cmp.automatic
22585 && glyph->u.cmp.id == s->cmp_id
22586 && s->cmp_to == glyph->slice.cmp.from)
22587 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22589 for (i = s->cmp_from; i < s->cmp_to; i++)
22591 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22592 unsigned code = LGLYPH_CODE (lglyph);
22594 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22596 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22597 return glyph - s->row->glyphs[s->area];
22601 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22602 See the comment of fill_glyph_string for arguments.
22603 Value is the index of the first glyph not in S. */
22606 static int
22607 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22608 int start, int end, int overlaps)
22610 struct glyph *glyph, *last;
22611 int voffset;
22613 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22614 s->for_overlaps = overlaps;
22615 glyph = s->row->glyphs[s->area] + start;
22616 last = s->row->glyphs[s->area] + end;
22617 voffset = glyph->voffset;
22618 s->face = FACE_FROM_ID (s->f, face_id);
22619 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22620 s->nchars = 1;
22621 s->width = glyph->pixel_width;
22622 glyph++;
22623 while (glyph < last
22624 && glyph->type == GLYPHLESS_GLYPH
22625 && glyph->voffset == voffset
22626 && glyph->face_id == face_id)
22628 s->nchars++;
22629 s->width += glyph->pixel_width;
22630 glyph++;
22632 s->ybase += voffset;
22633 return glyph - s->row->glyphs[s->area];
22637 /* Fill glyph string S from a sequence of character glyphs.
22639 FACE_ID is the face id of the string. START is the index of the
22640 first glyph to consider, END is the index of the last + 1.
22641 OVERLAPS non-zero means S should draw the foreground only, and use
22642 its physical height for clipping. See also draw_glyphs.
22644 Value is the index of the first glyph not in S. */
22646 static int
22647 fill_glyph_string (struct glyph_string *s, int face_id,
22648 int start, int end, int overlaps)
22650 struct glyph *glyph, *last;
22651 int voffset;
22652 int glyph_not_available_p;
22654 eassert (s->f == XFRAME (s->w->frame));
22655 eassert (s->nchars == 0);
22656 eassert (start >= 0 && end > start);
22658 s->for_overlaps = overlaps;
22659 glyph = s->row->glyphs[s->area] + start;
22660 last = s->row->glyphs[s->area] + end;
22661 voffset = glyph->voffset;
22662 s->padding_p = glyph->padding_p;
22663 glyph_not_available_p = glyph->glyph_not_available_p;
22665 while (glyph < last
22666 && glyph->type == CHAR_GLYPH
22667 && glyph->voffset == voffset
22668 /* Same face id implies same font, nowadays. */
22669 && glyph->face_id == face_id
22670 && glyph->glyph_not_available_p == glyph_not_available_p)
22672 int two_byte_p;
22674 s->face = get_glyph_face_and_encoding (s->f, glyph,
22675 s->char2b + s->nchars,
22676 &two_byte_p);
22677 s->two_byte_p = two_byte_p;
22678 ++s->nchars;
22679 eassert (s->nchars <= end - start);
22680 s->width += glyph->pixel_width;
22681 if (glyph++->padding_p != s->padding_p)
22682 break;
22685 s->font = s->face->font;
22687 /* If the specified font could not be loaded, use the frame's font,
22688 but record the fact that we couldn't load it in
22689 S->font_not_found_p so that we can draw rectangles for the
22690 characters of the glyph string. */
22691 if (s->font == NULL || glyph_not_available_p)
22693 s->font_not_found_p = 1;
22694 s->font = FRAME_FONT (s->f);
22697 /* Adjust base line for subscript/superscript text. */
22698 s->ybase += voffset;
22700 eassert (s->face && s->face->gc);
22701 return glyph - s->row->glyphs[s->area];
22705 /* Fill glyph string S from image glyph S->first_glyph. */
22707 static void
22708 fill_image_glyph_string (struct glyph_string *s)
22710 eassert (s->first_glyph->type == IMAGE_GLYPH);
22711 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22712 eassert (s->img);
22713 s->slice = s->first_glyph->slice.img;
22714 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22715 s->font = s->face->font;
22716 s->width = s->first_glyph->pixel_width;
22718 /* Adjust base line for subscript/superscript text. */
22719 s->ybase += s->first_glyph->voffset;
22723 /* Fill glyph string S from a sequence of stretch glyphs.
22725 START is the index of the first glyph to consider,
22726 END is the index of the last + 1.
22728 Value is the index of the first glyph not in S. */
22730 static int
22731 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22733 struct glyph *glyph, *last;
22734 int voffset, face_id;
22736 eassert (s->first_glyph->type == STRETCH_GLYPH);
22738 glyph = s->row->glyphs[s->area] + start;
22739 last = s->row->glyphs[s->area] + end;
22740 face_id = glyph->face_id;
22741 s->face = FACE_FROM_ID (s->f, face_id);
22742 s->font = s->face->font;
22743 s->width = glyph->pixel_width;
22744 s->nchars = 1;
22745 voffset = glyph->voffset;
22747 for (++glyph;
22748 (glyph < last
22749 && glyph->type == STRETCH_GLYPH
22750 && glyph->voffset == voffset
22751 && glyph->face_id == face_id);
22752 ++glyph)
22753 s->width += glyph->pixel_width;
22755 /* Adjust base line for subscript/superscript text. */
22756 s->ybase += voffset;
22758 /* The case that face->gc == 0 is handled when drawing the glyph
22759 string by calling PREPARE_FACE_FOR_DISPLAY. */
22760 eassert (s->face);
22761 return glyph - s->row->glyphs[s->area];
22764 static struct font_metrics *
22765 get_per_char_metric (struct font *font, XChar2b *char2b)
22767 static struct font_metrics metrics;
22768 unsigned code;
22770 if (! font)
22771 return NULL;
22772 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22773 if (code == FONT_INVALID_CODE)
22774 return NULL;
22775 font->driver->text_extents (font, &code, 1, &metrics);
22776 return &metrics;
22779 /* EXPORT for RIF:
22780 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22781 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22782 assumed to be zero. */
22784 void
22785 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22787 *left = *right = 0;
22789 if (glyph->type == CHAR_GLYPH)
22791 struct face *face;
22792 XChar2b char2b;
22793 struct font_metrics *pcm;
22795 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22796 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22798 if (pcm->rbearing > pcm->width)
22799 *right = pcm->rbearing - pcm->width;
22800 if (pcm->lbearing < 0)
22801 *left = -pcm->lbearing;
22804 else if (glyph->type == COMPOSITE_GLYPH)
22806 if (! glyph->u.cmp.automatic)
22808 struct composition *cmp = composition_table[glyph->u.cmp.id];
22810 if (cmp->rbearing > cmp->pixel_width)
22811 *right = cmp->rbearing - cmp->pixel_width;
22812 if (cmp->lbearing < 0)
22813 *left = - cmp->lbearing;
22815 else
22817 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22818 struct font_metrics metrics;
22820 composition_gstring_width (gstring, glyph->slice.cmp.from,
22821 glyph->slice.cmp.to + 1, &metrics);
22822 if (metrics.rbearing > metrics.width)
22823 *right = metrics.rbearing - metrics.width;
22824 if (metrics.lbearing < 0)
22825 *left = - metrics.lbearing;
22831 /* Return the index of the first glyph preceding glyph string S that
22832 is overwritten by S because of S's left overhang. Value is -1
22833 if no glyphs are overwritten. */
22835 static int
22836 left_overwritten (struct glyph_string *s)
22838 int k;
22840 if (s->left_overhang)
22842 int x = 0, i;
22843 struct glyph *glyphs = s->row->glyphs[s->area];
22844 int first = s->first_glyph - glyphs;
22846 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22847 x -= glyphs[i].pixel_width;
22849 k = i + 1;
22851 else
22852 k = -1;
22854 return k;
22858 /* Return the index of the first glyph preceding glyph string S that
22859 is overwriting S because of its right overhang. Value is -1 if no
22860 glyph in front of S overwrites S. */
22862 static int
22863 left_overwriting (struct glyph_string *s)
22865 int i, k, x;
22866 struct glyph *glyphs = s->row->glyphs[s->area];
22867 int first = s->first_glyph - glyphs;
22869 k = -1;
22870 x = 0;
22871 for (i = first - 1; i >= 0; --i)
22873 int left, right;
22874 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22875 if (x + right > 0)
22876 k = i;
22877 x -= glyphs[i].pixel_width;
22880 return k;
22884 /* Return the index of the last glyph following glyph string S that is
22885 overwritten by S because of S's right overhang. Value is -1 if
22886 no such glyph is found. */
22888 static int
22889 right_overwritten (struct glyph_string *s)
22891 int k = -1;
22893 if (s->right_overhang)
22895 int x = 0, i;
22896 struct glyph *glyphs = s->row->glyphs[s->area];
22897 int first = (s->first_glyph - glyphs
22898 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22899 int end = s->row->used[s->area];
22901 for (i = first; i < end && s->right_overhang > x; ++i)
22902 x += glyphs[i].pixel_width;
22904 k = i;
22907 return k;
22911 /* Return the index of the last glyph following glyph string S that
22912 overwrites S because of its left overhang. Value is negative
22913 if no such glyph is found. */
22915 static int
22916 right_overwriting (struct glyph_string *s)
22918 int i, k, x;
22919 int end = s->row->used[s->area];
22920 struct glyph *glyphs = s->row->glyphs[s->area];
22921 int first = (s->first_glyph - glyphs
22922 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22924 k = -1;
22925 x = 0;
22926 for (i = first; i < end; ++i)
22928 int left, right;
22929 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22930 if (x - left < 0)
22931 k = i;
22932 x += glyphs[i].pixel_width;
22935 return k;
22939 /* Set background width of glyph string S. START is the index of the
22940 first glyph following S. LAST_X is the right-most x-position + 1
22941 in the drawing area. */
22943 static void
22944 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22946 /* If the face of this glyph string has to be drawn to the end of
22947 the drawing area, set S->extends_to_end_of_line_p. */
22949 if (start == s->row->used[s->area]
22950 && s->area == TEXT_AREA
22951 && ((s->row->fill_line_p
22952 && (s->hl == DRAW_NORMAL_TEXT
22953 || s->hl == DRAW_IMAGE_RAISED
22954 || s->hl == DRAW_IMAGE_SUNKEN))
22955 || s->hl == DRAW_MOUSE_FACE))
22956 s->extends_to_end_of_line_p = 1;
22958 /* If S extends its face to the end of the line, set its
22959 background_width to the distance to the right edge of the drawing
22960 area. */
22961 if (s->extends_to_end_of_line_p)
22962 s->background_width = last_x - s->x + 1;
22963 else
22964 s->background_width = s->width;
22968 /* Compute overhangs and x-positions for glyph string S and its
22969 predecessors, or successors. X is the starting x-position for S.
22970 BACKWARD_P non-zero means process predecessors. */
22972 static void
22973 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22975 if (backward_p)
22977 while (s)
22979 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22980 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22981 x -= s->width;
22982 s->x = x;
22983 s = s->prev;
22986 else
22988 while (s)
22990 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22991 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22992 s->x = x;
22993 x += s->width;
22994 s = s->next;
23001 /* The following macros are only called from draw_glyphs below.
23002 They reference the following parameters of that function directly:
23003 `w', `row', `area', and `overlap_p'
23004 as well as the following local variables:
23005 `s', `f', and `hdc' (in W32) */
23007 #ifdef HAVE_NTGUI
23008 /* On W32, silently add local `hdc' variable to argument list of
23009 init_glyph_string. */
23010 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23011 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23012 #else
23013 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23014 init_glyph_string (s, char2b, w, row, area, start, hl)
23015 #endif
23017 /* Add a glyph string for a stretch glyph to the list of strings
23018 between HEAD and TAIL. START is the index of the stretch glyph in
23019 row area AREA of glyph row ROW. END is the index of the last glyph
23020 in that glyph row area. X is the current output position assigned
23021 to the new glyph string constructed. HL overrides that face of the
23022 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23023 is the right-most x-position of the drawing area. */
23025 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23026 and below -- keep them on one line. */
23027 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23028 do \
23030 s = alloca (sizeof *s); \
23031 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23032 START = fill_stretch_glyph_string (s, START, END); \
23033 append_glyph_string (&HEAD, &TAIL, s); \
23034 s->x = (X); \
23036 while (0)
23039 /* Add a glyph string for an image glyph to the list of strings
23040 between HEAD and TAIL. START is the index of the image glyph in
23041 row area AREA of glyph row ROW. END is the index of the last glyph
23042 in that glyph row area. X is the current output position assigned
23043 to the new glyph string constructed. HL overrides that face of the
23044 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23045 is the right-most x-position of the drawing area. */
23047 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23048 do \
23050 s = alloca (sizeof *s); \
23051 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23052 fill_image_glyph_string (s); \
23053 append_glyph_string (&HEAD, &TAIL, s); \
23054 ++START; \
23055 s->x = (X); \
23057 while (0)
23060 /* Add a glyph string for a sequence of character glyphs to the list
23061 of strings between HEAD and TAIL. START is the index of the first
23062 glyph in row area AREA of glyph row ROW that is part of the new
23063 glyph string. END is the index of the last glyph in that glyph row
23064 area. X is the current output position assigned to the new glyph
23065 string constructed. HL overrides that face of the glyph; e.g. it
23066 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23067 right-most x-position of the drawing area. */
23069 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23070 do \
23072 int face_id; \
23073 XChar2b *char2b; \
23075 face_id = (row)->glyphs[area][START].face_id; \
23077 s = alloca (sizeof *s); \
23078 char2b = alloca ((END - START) * sizeof *char2b); \
23079 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23080 append_glyph_string (&HEAD, &TAIL, s); \
23081 s->x = (X); \
23082 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23084 while (0)
23087 /* Add a glyph string for a composite sequence to the list of strings
23088 between HEAD and TAIL. START is the index of the first glyph in
23089 row area AREA of glyph row ROW that is part of the new glyph
23090 string. END is the index of the last glyph in that glyph row area.
23091 X is the current output position assigned to the new glyph string
23092 constructed. HL overrides that face of the glyph; e.g. it is
23093 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23094 x-position of the drawing area. */
23096 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23097 do { \
23098 int face_id = (row)->glyphs[area][START].face_id; \
23099 struct face *base_face = FACE_FROM_ID (f, face_id); \
23100 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23101 struct composition *cmp = composition_table[cmp_id]; \
23102 XChar2b *char2b; \
23103 struct glyph_string *first_s = NULL; \
23104 int n; \
23106 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23108 /* Make glyph_strings for each glyph sequence that is drawable by \
23109 the same face, and append them to HEAD/TAIL. */ \
23110 for (n = 0; n < cmp->glyph_len;) \
23112 s = alloca (sizeof *s); \
23113 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23114 append_glyph_string (&(HEAD), &(TAIL), s); \
23115 s->cmp = cmp; \
23116 s->cmp_from = n; \
23117 s->x = (X); \
23118 if (n == 0) \
23119 first_s = s; \
23120 n = fill_composite_glyph_string (s, base_face, overlaps); \
23123 ++START; \
23124 s = first_s; \
23125 } while (0)
23128 /* Add a glyph string for a glyph-string sequence to the list of strings
23129 between HEAD and TAIL. */
23131 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23132 do { \
23133 int face_id; \
23134 XChar2b *char2b; \
23135 Lisp_Object gstring; \
23137 face_id = (row)->glyphs[area][START].face_id; \
23138 gstring = (composition_gstring_from_id \
23139 ((row)->glyphs[area][START].u.cmp.id)); \
23140 s = alloca (sizeof *s); \
23141 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23142 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23143 append_glyph_string (&(HEAD), &(TAIL), s); \
23144 s->x = (X); \
23145 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23146 } while (0)
23149 /* Add a glyph string for a sequence of glyphless character's glyphs
23150 to the list of strings between HEAD and TAIL. The meanings of
23151 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23153 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23154 do \
23156 int face_id; \
23158 face_id = (row)->glyphs[area][START].face_id; \
23160 s = alloca (sizeof *s); \
23161 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23162 append_glyph_string (&HEAD, &TAIL, s); \
23163 s->x = (X); \
23164 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23165 overlaps); \
23167 while (0)
23170 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23171 of AREA of glyph row ROW on window W between indices START and END.
23172 HL overrides the face for drawing glyph strings, e.g. it is
23173 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23174 x-positions of the drawing area.
23176 This is an ugly monster macro construct because we must use alloca
23177 to allocate glyph strings (because draw_glyphs can be called
23178 asynchronously). */
23180 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23181 do \
23183 HEAD = TAIL = NULL; \
23184 while (START < END) \
23186 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23187 switch (first_glyph->type) \
23189 case CHAR_GLYPH: \
23190 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23191 HL, X, LAST_X); \
23192 break; \
23194 case COMPOSITE_GLYPH: \
23195 if (first_glyph->u.cmp.automatic) \
23196 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23197 HL, X, LAST_X); \
23198 else \
23199 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23200 HL, X, LAST_X); \
23201 break; \
23203 case STRETCH_GLYPH: \
23204 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23205 HL, X, LAST_X); \
23206 break; \
23208 case IMAGE_GLYPH: \
23209 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23210 HL, X, LAST_X); \
23211 break; \
23213 case GLYPHLESS_GLYPH: \
23214 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23215 HL, X, LAST_X); \
23216 break; \
23218 default: \
23219 emacs_abort (); \
23222 if (s) \
23224 set_glyph_string_background_width (s, START, LAST_X); \
23225 (X) += s->width; \
23228 } while (0)
23231 /* Draw glyphs between START and END in AREA of ROW on window W,
23232 starting at x-position X. X is relative to AREA in W. HL is a
23233 face-override with the following meaning:
23235 DRAW_NORMAL_TEXT draw normally
23236 DRAW_CURSOR draw in cursor face
23237 DRAW_MOUSE_FACE draw in mouse face.
23238 DRAW_INVERSE_VIDEO draw in mode line face
23239 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23240 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23242 If OVERLAPS is non-zero, draw only the foreground of characters and
23243 clip to the physical height of ROW. Non-zero value also defines
23244 the overlapping part to be drawn:
23246 OVERLAPS_PRED overlap with preceding rows
23247 OVERLAPS_SUCC overlap with succeeding rows
23248 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23249 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23251 Value is the x-position reached, relative to AREA of W. */
23253 static int
23254 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23255 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23256 enum draw_glyphs_face hl, int overlaps)
23258 struct glyph_string *head, *tail;
23259 struct glyph_string *s;
23260 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23261 int i, j, x_reached, last_x, area_left = 0;
23262 struct frame *f = XFRAME (WINDOW_FRAME (w));
23263 DECLARE_HDC (hdc);
23265 ALLOCATE_HDC (hdc, f);
23267 /* Let's rather be paranoid than getting a SEGV. */
23268 end = min (end, row->used[area]);
23269 start = clip_to_bounds (0, start, end);
23271 /* Translate X to frame coordinates. Set last_x to the right
23272 end of the drawing area. */
23273 if (row->full_width_p)
23275 /* X is relative to the left edge of W, without scroll bars
23276 or fringes. */
23277 area_left = WINDOW_LEFT_EDGE_X (w);
23278 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23280 else
23282 area_left = window_box_left (w, area);
23283 last_x = area_left + window_box_width (w, area);
23285 x += area_left;
23287 /* Build a doubly-linked list of glyph_string structures between
23288 head and tail from what we have to draw. Note that the macro
23289 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23290 the reason we use a separate variable `i'. */
23291 i = start;
23292 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23293 if (tail)
23294 x_reached = tail->x + tail->background_width;
23295 else
23296 x_reached = x;
23298 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23299 the row, redraw some glyphs in front or following the glyph
23300 strings built above. */
23301 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23303 struct glyph_string *h, *t;
23304 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23305 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23306 int check_mouse_face = 0;
23307 int dummy_x = 0;
23309 /* If mouse highlighting is on, we may need to draw adjacent
23310 glyphs using mouse-face highlighting. */
23311 if (area == TEXT_AREA && row->mouse_face_p
23312 && hlinfo->mouse_face_beg_row >= 0
23313 && hlinfo->mouse_face_end_row >= 0)
23315 struct glyph_row *mouse_beg_row, *mouse_end_row;
23317 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23318 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23320 if (row >= mouse_beg_row && row <= mouse_end_row)
23322 check_mouse_face = 1;
23323 mouse_beg_col = (row == mouse_beg_row)
23324 ? hlinfo->mouse_face_beg_col : 0;
23325 mouse_end_col = (row == mouse_end_row)
23326 ? hlinfo->mouse_face_end_col
23327 : row->used[TEXT_AREA];
23331 /* Compute overhangs for all glyph strings. */
23332 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23333 for (s = head; s; s = s->next)
23334 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23336 /* Prepend glyph strings for glyphs in front of the first glyph
23337 string that are overwritten because of the first glyph
23338 string's left overhang. The background of all strings
23339 prepended must be drawn because the first glyph string
23340 draws over it. */
23341 i = left_overwritten (head);
23342 if (i >= 0)
23344 enum draw_glyphs_face overlap_hl;
23346 /* If this row contains mouse highlighting, attempt to draw
23347 the overlapped glyphs with the correct highlight. This
23348 code fails if the overlap encompasses more than one glyph
23349 and mouse-highlight spans only some of these glyphs.
23350 However, making it work perfectly involves a lot more
23351 code, and I don't know if the pathological case occurs in
23352 practice, so we'll stick to this for now. --- cyd */
23353 if (check_mouse_face
23354 && mouse_beg_col < start && mouse_end_col > i)
23355 overlap_hl = DRAW_MOUSE_FACE;
23356 else
23357 overlap_hl = DRAW_NORMAL_TEXT;
23359 j = i;
23360 BUILD_GLYPH_STRINGS (j, start, h, t,
23361 overlap_hl, dummy_x, last_x);
23362 start = i;
23363 compute_overhangs_and_x (t, head->x, 1);
23364 prepend_glyph_string_lists (&head, &tail, h, t);
23365 clip_head = head;
23368 /* Prepend glyph strings for glyphs in front of the first glyph
23369 string that overwrite that glyph string because of their
23370 right overhang. For these strings, only the foreground must
23371 be drawn, because it draws over the glyph string at `head'.
23372 The background must not be drawn because this would overwrite
23373 right overhangs of preceding glyphs for which no glyph
23374 strings exist. */
23375 i = left_overwriting (head);
23376 if (i >= 0)
23378 enum draw_glyphs_face overlap_hl;
23380 if (check_mouse_face
23381 && mouse_beg_col < start && mouse_end_col > i)
23382 overlap_hl = DRAW_MOUSE_FACE;
23383 else
23384 overlap_hl = DRAW_NORMAL_TEXT;
23386 clip_head = head;
23387 BUILD_GLYPH_STRINGS (i, start, h, t,
23388 overlap_hl, dummy_x, last_x);
23389 for (s = h; s; s = s->next)
23390 s->background_filled_p = 1;
23391 compute_overhangs_and_x (t, head->x, 1);
23392 prepend_glyph_string_lists (&head, &tail, h, t);
23395 /* Append glyphs strings for glyphs following the last glyph
23396 string tail that are overwritten by tail. The background of
23397 these strings has to be drawn because tail's foreground draws
23398 over it. */
23399 i = right_overwritten (tail);
23400 if (i >= 0)
23402 enum draw_glyphs_face overlap_hl;
23404 if (check_mouse_face
23405 && mouse_beg_col < i && mouse_end_col > end)
23406 overlap_hl = DRAW_MOUSE_FACE;
23407 else
23408 overlap_hl = DRAW_NORMAL_TEXT;
23410 BUILD_GLYPH_STRINGS (end, i, h, t,
23411 overlap_hl, x, last_x);
23412 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23413 we don't have `end = i;' here. */
23414 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23415 append_glyph_string_lists (&head, &tail, h, t);
23416 clip_tail = tail;
23419 /* Append glyph strings for glyphs following the last glyph
23420 string tail that overwrite tail. The foreground of such
23421 glyphs has to be drawn because it writes into the background
23422 of tail. The background must not be drawn because it could
23423 paint over the foreground of following glyphs. */
23424 i = right_overwriting (tail);
23425 if (i >= 0)
23427 enum draw_glyphs_face overlap_hl;
23428 if (check_mouse_face
23429 && mouse_beg_col < i && mouse_end_col > end)
23430 overlap_hl = DRAW_MOUSE_FACE;
23431 else
23432 overlap_hl = DRAW_NORMAL_TEXT;
23434 clip_tail = tail;
23435 i++; /* We must include the Ith glyph. */
23436 BUILD_GLYPH_STRINGS (end, i, h, t,
23437 overlap_hl, x, last_x);
23438 for (s = h; s; s = s->next)
23439 s->background_filled_p = 1;
23440 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23441 append_glyph_string_lists (&head, &tail, h, t);
23443 if (clip_head || clip_tail)
23444 for (s = head; s; s = s->next)
23446 s->clip_head = clip_head;
23447 s->clip_tail = clip_tail;
23451 /* Draw all strings. */
23452 for (s = head; s; s = s->next)
23453 FRAME_RIF (f)->draw_glyph_string (s);
23455 #ifndef HAVE_NS
23456 /* When focus a sole frame and move horizontally, this sets on_p to 0
23457 causing a failure to erase prev cursor position. */
23458 if (area == TEXT_AREA
23459 && !row->full_width_p
23460 /* When drawing overlapping rows, only the glyph strings'
23461 foreground is drawn, which doesn't erase a cursor
23462 completely. */
23463 && !overlaps)
23465 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23466 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23467 : (tail ? tail->x + tail->background_width : x));
23468 x0 -= area_left;
23469 x1 -= area_left;
23471 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23472 row->y, MATRIX_ROW_BOTTOM_Y (row));
23474 #endif
23476 /* Value is the x-position up to which drawn, relative to AREA of W.
23477 This doesn't include parts drawn because of overhangs. */
23478 if (row->full_width_p)
23479 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23480 else
23481 x_reached -= area_left;
23483 RELEASE_HDC (hdc, f);
23485 return x_reached;
23488 /* Expand row matrix if too narrow. Don't expand if area
23489 is not present. */
23491 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23493 if (!fonts_changed_p \
23494 && (it->glyph_row->glyphs[area] \
23495 < it->glyph_row->glyphs[area + 1])) \
23497 it->w->ncols_scale_factor++; \
23498 fonts_changed_p = 1; \
23502 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23503 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23505 static void
23506 append_glyph (struct it *it)
23508 struct glyph *glyph;
23509 enum glyph_row_area area = it->area;
23511 eassert (it->glyph_row);
23512 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23514 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23515 if (glyph < it->glyph_row->glyphs[area + 1])
23517 /* If the glyph row is reversed, we need to prepend the glyph
23518 rather than append it. */
23519 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23521 struct glyph *g;
23523 /* Make room for the additional glyph. */
23524 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23525 g[1] = *g;
23526 glyph = it->glyph_row->glyphs[area];
23528 glyph->charpos = CHARPOS (it->position);
23529 glyph->object = it->object;
23530 if (it->pixel_width > 0)
23532 glyph->pixel_width = it->pixel_width;
23533 glyph->padding_p = 0;
23535 else
23537 /* Assure at least 1-pixel width. Otherwise, cursor can't
23538 be displayed correctly. */
23539 glyph->pixel_width = 1;
23540 glyph->padding_p = 1;
23542 glyph->ascent = it->ascent;
23543 glyph->descent = it->descent;
23544 glyph->voffset = it->voffset;
23545 glyph->type = CHAR_GLYPH;
23546 glyph->avoid_cursor_p = it->avoid_cursor_p;
23547 glyph->multibyte_p = it->multibyte_p;
23548 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23550 /* In R2L rows, the left and the right box edges need to be
23551 drawn in reverse direction. */
23552 glyph->right_box_line_p = it->start_of_box_run_p;
23553 glyph->left_box_line_p = it->end_of_box_run_p;
23555 else
23557 glyph->left_box_line_p = it->start_of_box_run_p;
23558 glyph->right_box_line_p = it->end_of_box_run_p;
23560 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23561 || it->phys_descent > it->descent);
23562 glyph->glyph_not_available_p = it->glyph_not_available_p;
23563 glyph->face_id = it->face_id;
23564 glyph->u.ch = it->char_to_display;
23565 glyph->slice.img = null_glyph_slice;
23566 glyph->font_type = FONT_TYPE_UNKNOWN;
23567 if (it->bidi_p)
23569 glyph->resolved_level = it->bidi_it.resolved_level;
23570 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23571 emacs_abort ();
23572 glyph->bidi_type = it->bidi_it.type;
23574 else
23576 glyph->resolved_level = 0;
23577 glyph->bidi_type = UNKNOWN_BT;
23579 ++it->glyph_row->used[area];
23581 else
23582 IT_EXPAND_MATRIX_WIDTH (it, area);
23585 /* Store one glyph for the composition IT->cmp_it.id in
23586 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23587 non-null. */
23589 static void
23590 append_composite_glyph (struct it *it)
23592 struct glyph *glyph;
23593 enum glyph_row_area area = it->area;
23595 eassert (it->glyph_row);
23597 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23598 if (glyph < it->glyph_row->glyphs[area + 1])
23600 /* If the glyph row is reversed, we need to prepend the glyph
23601 rather than append it. */
23602 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23604 struct glyph *g;
23606 /* Make room for the new glyph. */
23607 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23608 g[1] = *g;
23609 glyph = it->glyph_row->glyphs[it->area];
23611 glyph->charpos = it->cmp_it.charpos;
23612 glyph->object = it->object;
23613 glyph->pixel_width = it->pixel_width;
23614 glyph->ascent = it->ascent;
23615 glyph->descent = it->descent;
23616 glyph->voffset = it->voffset;
23617 glyph->type = COMPOSITE_GLYPH;
23618 if (it->cmp_it.ch < 0)
23620 glyph->u.cmp.automatic = 0;
23621 glyph->u.cmp.id = it->cmp_it.id;
23622 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23624 else
23626 glyph->u.cmp.automatic = 1;
23627 glyph->u.cmp.id = it->cmp_it.id;
23628 glyph->slice.cmp.from = it->cmp_it.from;
23629 glyph->slice.cmp.to = it->cmp_it.to - 1;
23631 glyph->avoid_cursor_p = it->avoid_cursor_p;
23632 glyph->multibyte_p = it->multibyte_p;
23633 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23635 /* In R2L rows, the left and the right box edges need to be
23636 drawn in reverse direction. */
23637 glyph->right_box_line_p = it->start_of_box_run_p;
23638 glyph->left_box_line_p = it->end_of_box_run_p;
23640 else
23642 glyph->left_box_line_p = it->start_of_box_run_p;
23643 glyph->right_box_line_p = it->end_of_box_run_p;
23645 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23646 || it->phys_descent > it->descent);
23647 glyph->padding_p = 0;
23648 glyph->glyph_not_available_p = 0;
23649 glyph->face_id = it->face_id;
23650 glyph->font_type = FONT_TYPE_UNKNOWN;
23651 if (it->bidi_p)
23653 glyph->resolved_level = it->bidi_it.resolved_level;
23654 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23655 emacs_abort ();
23656 glyph->bidi_type = it->bidi_it.type;
23658 ++it->glyph_row->used[area];
23660 else
23661 IT_EXPAND_MATRIX_WIDTH (it, area);
23665 /* Change IT->ascent and IT->height according to the setting of
23666 IT->voffset. */
23668 static void
23669 take_vertical_position_into_account (struct it *it)
23671 if (it->voffset)
23673 if (it->voffset < 0)
23674 /* Increase the ascent so that we can display the text higher
23675 in the line. */
23676 it->ascent -= it->voffset;
23677 else
23678 /* Increase the descent so that we can display the text lower
23679 in the line. */
23680 it->descent += it->voffset;
23685 /* Produce glyphs/get display metrics for the image IT is loaded with.
23686 See the description of struct display_iterator in dispextern.h for
23687 an overview of struct display_iterator. */
23689 static void
23690 produce_image_glyph (struct it *it)
23692 struct image *img;
23693 struct face *face;
23694 int glyph_ascent, crop;
23695 struct glyph_slice slice;
23697 eassert (it->what == IT_IMAGE);
23699 face = FACE_FROM_ID (it->f, it->face_id);
23700 eassert (face);
23701 /* Make sure X resources of the face is loaded. */
23702 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23704 if (it->image_id < 0)
23706 /* Fringe bitmap. */
23707 it->ascent = it->phys_ascent = 0;
23708 it->descent = it->phys_descent = 0;
23709 it->pixel_width = 0;
23710 it->nglyphs = 0;
23711 return;
23714 img = IMAGE_FROM_ID (it->f, it->image_id);
23715 eassert (img);
23716 /* Make sure X resources of the image is loaded. */
23717 prepare_image_for_display (it->f, img);
23719 slice.x = slice.y = 0;
23720 slice.width = img->width;
23721 slice.height = img->height;
23723 if (INTEGERP (it->slice.x))
23724 slice.x = XINT (it->slice.x);
23725 else if (FLOATP (it->slice.x))
23726 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23728 if (INTEGERP (it->slice.y))
23729 slice.y = XINT (it->slice.y);
23730 else if (FLOATP (it->slice.y))
23731 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23733 if (INTEGERP (it->slice.width))
23734 slice.width = XINT (it->slice.width);
23735 else if (FLOATP (it->slice.width))
23736 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23738 if (INTEGERP (it->slice.height))
23739 slice.height = XINT (it->slice.height);
23740 else if (FLOATP (it->slice.height))
23741 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23743 if (slice.x >= img->width)
23744 slice.x = img->width;
23745 if (slice.y >= img->height)
23746 slice.y = img->height;
23747 if (slice.x + slice.width >= img->width)
23748 slice.width = img->width - slice.x;
23749 if (slice.y + slice.height > img->height)
23750 slice.height = img->height - slice.y;
23752 if (slice.width == 0 || slice.height == 0)
23753 return;
23755 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23757 it->descent = slice.height - glyph_ascent;
23758 if (slice.y == 0)
23759 it->descent += img->vmargin;
23760 if (slice.y + slice.height == img->height)
23761 it->descent += img->vmargin;
23762 it->phys_descent = it->descent;
23764 it->pixel_width = slice.width;
23765 if (slice.x == 0)
23766 it->pixel_width += img->hmargin;
23767 if (slice.x + slice.width == img->width)
23768 it->pixel_width += img->hmargin;
23770 /* It's quite possible for images to have an ascent greater than
23771 their height, so don't get confused in that case. */
23772 if (it->descent < 0)
23773 it->descent = 0;
23775 it->nglyphs = 1;
23777 if (face->box != FACE_NO_BOX)
23779 if (face->box_line_width > 0)
23781 if (slice.y == 0)
23782 it->ascent += face->box_line_width;
23783 if (slice.y + slice.height == img->height)
23784 it->descent += face->box_line_width;
23787 if (it->start_of_box_run_p && slice.x == 0)
23788 it->pixel_width += eabs (face->box_line_width);
23789 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23790 it->pixel_width += eabs (face->box_line_width);
23793 take_vertical_position_into_account (it);
23795 /* Automatically crop wide image glyphs at right edge so we can
23796 draw the cursor on same display row. */
23797 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23798 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23800 it->pixel_width -= crop;
23801 slice.width -= crop;
23804 if (it->glyph_row)
23806 struct glyph *glyph;
23807 enum glyph_row_area area = it->area;
23809 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23810 if (glyph < it->glyph_row->glyphs[area + 1])
23812 glyph->charpos = CHARPOS (it->position);
23813 glyph->object = it->object;
23814 glyph->pixel_width = it->pixel_width;
23815 glyph->ascent = glyph_ascent;
23816 glyph->descent = it->descent;
23817 glyph->voffset = it->voffset;
23818 glyph->type = IMAGE_GLYPH;
23819 glyph->avoid_cursor_p = it->avoid_cursor_p;
23820 glyph->multibyte_p = it->multibyte_p;
23821 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23823 /* In R2L rows, the left and the right box edges need to be
23824 drawn in reverse direction. */
23825 glyph->right_box_line_p = it->start_of_box_run_p;
23826 glyph->left_box_line_p = it->end_of_box_run_p;
23828 else
23830 glyph->left_box_line_p = it->start_of_box_run_p;
23831 glyph->right_box_line_p = it->end_of_box_run_p;
23833 glyph->overlaps_vertically_p = 0;
23834 glyph->padding_p = 0;
23835 glyph->glyph_not_available_p = 0;
23836 glyph->face_id = it->face_id;
23837 glyph->u.img_id = img->id;
23838 glyph->slice.img = slice;
23839 glyph->font_type = FONT_TYPE_UNKNOWN;
23840 if (it->bidi_p)
23842 glyph->resolved_level = it->bidi_it.resolved_level;
23843 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23844 emacs_abort ();
23845 glyph->bidi_type = it->bidi_it.type;
23847 ++it->glyph_row->used[area];
23849 else
23850 IT_EXPAND_MATRIX_WIDTH (it, area);
23855 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23856 of the glyph, WIDTH and HEIGHT are the width and height of the
23857 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23859 static void
23860 append_stretch_glyph (struct it *it, Lisp_Object object,
23861 int width, int height, int ascent)
23863 struct glyph *glyph;
23864 enum glyph_row_area area = it->area;
23866 eassert (ascent >= 0 && ascent <= height);
23868 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23869 if (glyph < it->glyph_row->glyphs[area + 1])
23871 /* If the glyph row is reversed, we need to prepend the glyph
23872 rather than append it. */
23873 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23875 struct glyph *g;
23877 /* Make room for the additional glyph. */
23878 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23879 g[1] = *g;
23880 glyph = it->glyph_row->glyphs[area];
23882 glyph->charpos = CHARPOS (it->position);
23883 glyph->object = object;
23884 glyph->pixel_width = width;
23885 glyph->ascent = ascent;
23886 glyph->descent = height - ascent;
23887 glyph->voffset = it->voffset;
23888 glyph->type = STRETCH_GLYPH;
23889 glyph->avoid_cursor_p = it->avoid_cursor_p;
23890 glyph->multibyte_p = it->multibyte_p;
23891 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23893 /* In R2L rows, the left and the right box edges need to be
23894 drawn in reverse direction. */
23895 glyph->right_box_line_p = it->start_of_box_run_p;
23896 glyph->left_box_line_p = it->end_of_box_run_p;
23898 else
23900 glyph->left_box_line_p = it->start_of_box_run_p;
23901 glyph->right_box_line_p = it->end_of_box_run_p;
23903 glyph->overlaps_vertically_p = 0;
23904 glyph->padding_p = 0;
23905 glyph->glyph_not_available_p = 0;
23906 glyph->face_id = it->face_id;
23907 glyph->u.stretch.ascent = ascent;
23908 glyph->u.stretch.height = height;
23909 glyph->slice.img = null_glyph_slice;
23910 glyph->font_type = FONT_TYPE_UNKNOWN;
23911 if (it->bidi_p)
23913 glyph->resolved_level = it->bidi_it.resolved_level;
23914 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23915 emacs_abort ();
23916 glyph->bidi_type = it->bidi_it.type;
23918 else
23920 glyph->resolved_level = 0;
23921 glyph->bidi_type = UNKNOWN_BT;
23923 ++it->glyph_row->used[area];
23925 else
23926 IT_EXPAND_MATRIX_WIDTH (it, area);
23929 #endif /* HAVE_WINDOW_SYSTEM */
23931 /* Produce a stretch glyph for iterator IT. IT->object is the value
23932 of the glyph property displayed. The value must be a list
23933 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23934 being recognized:
23936 1. `:width WIDTH' specifies that the space should be WIDTH *
23937 canonical char width wide. WIDTH may be an integer or floating
23938 point number.
23940 2. `:relative-width FACTOR' specifies that the width of the stretch
23941 should be computed from the width of the first character having the
23942 `glyph' property, and should be FACTOR times that width.
23944 3. `:align-to HPOS' specifies that the space should be wide enough
23945 to reach HPOS, a value in canonical character units.
23947 Exactly one of the above pairs must be present.
23949 4. `:height HEIGHT' specifies that the height of the stretch produced
23950 should be HEIGHT, measured in canonical character units.
23952 5. `:relative-height FACTOR' specifies that the height of the
23953 stretch should be FACTOR times the height of the characters having
23954 the glyph property.
23956 Either none or exactly one of 4 or 5 must be present.
23958 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23959 of the stretch should be used for the ascent of the stretch.
23960 ASCENT must be in the range 0 <= ASCENT <= 100. */
23962 void
23963 produce_stretch_glyph (struct it *it)
23965 /* (space :width WIDTH :height HEIGHT ...) */
23966 Lisp_Object prop, plist;
23967 int width = 0, height = 0, align_to = -1;
23968 int zero_width_ok_p = 0;
23969 double tem;
23970 struct font *font = NULL;
23972 #ifdef HAVE_WINDOW_SYSTEM
23973 int ascent = 0;
23974 int zero_height_ok_p = 0;
23976 if (FRAME_WINDOW_P (it->f))
23978 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23979 font = face->font ? face->font : FRAME_FONT (it->f);
23980 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23982 #endif
23984 /* List should start with `space'. */
23985 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23986 plist = XCDR (it->object);
23988 /* Compute the width of the stretch. */
23989 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23990 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23992 /* Absolute width `:width WIDTH' specified and valid. */
23993 zero_width_ok_p = 1;
23994 width = (int)tem;
23996 #ifdef HAVE_WINDOW_SYSTEM
23997 else if (FRAME_WINDOW_P (it->f)
23998 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24000 /* Relative width `:relative-width FACTOR' specified and valid.
24001 Compute the width of the characters having the `glyph'
24002 property. */
24003 struct it it2;
24004 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24006 it2 = *it;
24007 if (it->multibyte_p)
24008 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24009 else
24011 it2.c = it2.char_to_display = *p, it2.len = 1;
24012 if (! ASCII_CHAR_P (it2.c))
24013 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24016 it2.glyph_row = NULL;
24017 it2.what = IT_CHARACTER;
24018 x_produce_glyphs (&it2);
24019 width = NUMVAL (prop) * it2.pixel_width;
24021 #endif /* HAVE_WINDOW_SYSTEM */
24022 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24023 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24025 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24026 align_to = (align_to < 0
24028 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24029 else if (align_to < 0)
24030 align_to = window_box_left_offset (it->w, TEXT_AREA);
24031 width = max (0, (int)tem + align_to - it->current_x);
24032 zero_width_ok_p = 1;
24034 else
24035 /* Nothing specified -> width defaults to canonical char width. */
24036 width = FRAME_COLUMN_WIDTH (it->f);
24038 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24039 width = 1;
24041 #ifdef HAVE_WINDOW_SYSTEM
24042 /* Compute height. */
24043 if (FRAME_WINDOW_P (it->f))
24045 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24046 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24048 height = (int)tem;
24049 zero_height_ok_p = 1;
24051 else if (prop = Fplist_get (plist, QCrelative_height),
24052 NUMVAL (prop) > 0)
24053 height = FONT_HEIGHT (font) * NUMVAL (prop);
24054 else
24055 height = FONT_HEIGHT (font);
24057 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24058 height = 1;
24060 /* Compute percentage of height used for ascent. If
24061 `:ascent ASCENT' is present and valid, use that. Otherwise,
24062 derive the ascent from the font in use. */
24063 if (prop = Fplist_get (plist, QCascent),
24064 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24065 ascent = height * NUMVAL (prop) / 100.0;
24066 else if (!NILP (prop)
24067 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24068 ascent = min (max (0, (int)tem), height);
24069 else
24070 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24072 else
24073 #endif /* HAVE_WINDOW_SYSTEM */
24074 height = 1;
24076 if (width > 0 && it->line_wrap != TRUNCATE
24077 && it->current_x + width > it->last_visible_x)
24079 width = it->last_visible_x - it->current_x;
24080 #ifdef HAVE_WINDOW_SYSTEM
24081 /* Subtract one more pixel from the stretch width, but only on
24082 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24083 width -= FRAME_WINDOW_P (it->f);
24084 #endif
24087 if (width > 0 && height > 0 && it->glyph_row)
24089 Lisp_Object o_object = it->object;
24090 Lisp_Object object = it->stack[it->sp - 1].string;
24091 int n = width;
24093 if (!STRINGP (object))
24094 object = it->w->contents;
24095 #ifdef HAVE_WINDOW_SYSTEM
24096 if (FRAME_WINDOW_P (it->f))
24097 append_stretch_glyph (it, object, width, height, ascent);
24098 else
24099 #endif
24101 it->object = object;
24102 it->char_to_display = ' ';
24103 it->pixel_width = it->len = 1;
24104 while (n--)
24105 tty_append_glyph (it);
24106 it->object = o_object;
24110 it->pixel_width = width;
24111 #ifdef HAVE_WINDOW_SYSTEM
24112 if (FRAME_WINDOW_P (it->f))
24114 it->ascent = it->phys_ascent = ascent;
24115 it->descent = it->phys_descent = height - it->ascent;
24116 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24117 take_vertical_position_into_account (it);
24119 else
24120 #endif
24121 it->nglyphs = width;
24124 /* Get information about special display element WHAT in an
24125 environment described by IT. WHAT is one of IT_TRUNCATION or
24126 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24127 non-null glyph_row member. This function ensures that fields like
24128 face_id, c, len of IT are left untouched. */
24130 static void
24131 produce_special_glyphs (struct it *it, enum display_element_type what)
24133 struct it temp_it;
24134 Lisp_Object gc;
24135 GLYPH glyph;
24137 temp_it = *it;
24138 temp_it.object = make_number (0);
24139 memset (&temp_it.current, 0, sizeof temp_it.current);
24141 if (what == IT_CONTINUATION)
24143 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24144 if (it->bidi_it.paragraph_dir == R2L)
24145 SET_GLYPH_FROM_CHAR (glyph, '/');
24146 else
24147 SET_GLYPH_FROM_CHAR (glyph, '\\');
24148 if (it->dp
24149 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24151 /* FIXME: Should we mirror GC for R2L lines? */
24152 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24153 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24156 else if (what == IT_TRUNCATION)
24158 /* Truncation glyph. */
24159 SET_GLYPH_FROM_CHAR (glyph, '$');
24160 if (it->dp
24161 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24163 /* FIXME: Should we mirror GC for R2L lines? */
24164 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24165 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24168 else
24169 emacs_abort ();
24171 #ifdef HAVE_WINDOW_SYSTEM
24172 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24173 is turned off, we precede the truncation/continuation glyphs by a
24174 stretch glyph whose width is computed such that these special
24175 glyphs are aligned at the window margin, even when very different
24176 fonts are used in different glyph rows. */
24177 if (FRAME_WINDOW_P (temp_it.f)
24178 /* init_iterator calls this with it->glyph_row == NULL, and it
24179 wants only the pixel width of the truncation/continuation
24180 glyphs. */
24181 && temp_it.glyph_row
24182 /* insert_left_trunc_glyphs calls us at the beginning of the
24183 row, and it has its own calculation of the stretch glyph
24184 width. */
24185 && temp_it.glyph_row->used[TEXT_AREA] > 0
24186 && (temp_it.glyph_row->reversed_p
24187 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24188 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24190 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24192 if (stretch_width > 0)
24194 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24195 struct font *font =
24196 face->font ? face->font : FRAME_FONT (temp_it.f);
24197 int stretch_ascent =
24198 (((temp_it.ascent + temp_it.descent)
24199 * FONT_BASE (font)) / FONT_HEIGHT (font));
24201 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24202 temp_it.ascent + temp_it.descent,
24203 stretch_ascent);
24206 #endif
24208 temp_it.dp = NULL;
24209 temp_it.what = IT_CHARACTER;
24210 temp_it.len = 1;
24211 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24212 temp_it.face_id = GLYPH_FACE (glyph);
24213 temp_it.len = CHAR_BYTES (temp_it.c);
24215 PRODUCE_GLYPHS (&temp_it);
24216 it->pixel_width = temp_it.pixel_width;
24217 it->nglyphs = temp_it.pixel_width;
24220 #ifdef HAVE_WINDOW_SYSTEM
24222 /* Calculate line-height and line-spacing properties.
24223 An integer value specifies explicit pixel value.
24224 A float value specifies relative value to current face height.
24225 A cons (float . face-name) specifies relative value to
24226 height of specified face font.
24228 Returns height in pixels, or nil. */
24231 static Lisp_Object
24232 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24233 int boff, int override)
24235 Lisp_Object face_name = Qnil;
24236 int ascent, descent, height;
24238 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24239 return val;
24241 if (CONSP (val))
24243 face_name = XCAR (val);
24244 val = XCDR (val);
24245 if (!NUMBERP (val))
24246 val = make_number (1);
24247 if (NILP (face_name))
24249 height = it->ascent + it->descent;
24250 goto scale;
24254 if (NILP (face_name))
24256 font = FRAME_FONT (it->f);
24257 boff = FRAME_BASELINE_OFFSET (it->f);
24259 else if (EQ (face_name, Qt))
24261 override = 0;
24263 else
24265 int face_id;
24266 struct face *face;
24268 face_id = lookup_named_face (it->f, face_name, 0);
24269 if (face_id < 0)
24270 return make_number (-1);
24272 face = FACE_FROM_ID (it->f, face_id);
24273 font = face->font;
24274 if (font == NULL)
24275 return make_number (-1);
24276 boff = font->baseline_offset;
24277 if (font->vertical_centering)
24278 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24281 ascent = FONT_BASE (font) + boff;
24282 descent = FONT_DESCENT (font) - boff;
24284 if (override)
24286 it->override_ascent = ascent;
24287 it->override_descent = descent;
24288 it->override_boff = boff;
24291 height = ascent + descent;
24293 scale:
24294 if (FLOATP (val))
24295 height = (int)(XFLOAT_DATA (val) * height);
24296 else if (INTEGERP (val))
24297 height *= XINT (val);
24299 return make_number (height);
24303 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24304 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24305 and only if this is for a character for which no font was found.
24307 If the display method (it->glyphless_method) is
24308 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24309 length of the acronym or the hexadecimal string, UPPER_XOFF and
24310 UPPER_YOFF are pixel offsets for the upper part of the string,
24311 LOWER_XOFF and LOWER_YOFF are for the lower part.
24313 For the other display methods, LEN through LOWER_YOFF are zero. */
24315 static void
24316 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24317 short upper_xoff, short upper_yoff,
24318 short lower_xoff, short lower_yoff)
24320 struct glyph *glyph;
24321 enum glyph_row_area area = it->area;
24323 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24324 if (glyph < it->glyph_row->glyphs[area + 1])
24326 /* If the glyph row is reversed, we need to prepend the glyph
24327 rather than append it. */
24328 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24330 struct glyph *g;
24332 /* Make room for the additional glyph. */
24333 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24334 g[1] = *g;
24335 glyph = it->glyph_row->glyphs[area];
24337 glyph->charpos = CHARPOS (it->position);
24338 glyph->object = it->object;
24339 glyph->pixel_width = it->pixel_width;
24340 glyph->ascent = it->ascent;
24341 glyph->descent = it->descent;
24342 glyph->voffset = it->voffset;
24343 glyph->type = GLYPHLESS_GLYPH;
24344 glyph->u.glyphless.method = it->glyphless_method;
24345 glyph->u.glyphless.for_no_font = for_no_font;
24346 glyph->u.glyphless.len = len;
24347 glyph->u.glyphless.ch = it->c;
24348 glyph->slice.glyphless.upper_xoff = upper_xoff;
24349 glyph->slice.glyphless.upper_yoff = upper_yoff;
24350 glyph->slice.glyphless.lower_xoff = lower_xoff;
24351 glyph->slice.glyphless.lower_yoff = lower_yoff;
24352 glyph->avoid_cursor_p = it->avoid_cursor_p;
24353 glyph->multibyte_p = it->multibyte_p;
24354 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24356 /* In R2L rows, the left and the right box edges need to be
24357 drawn in reverse direction. */
24358 glyph->right_box_line_p = it->start_of_box_run_p;
24359 glyph->left_box_line_p = it->end_of_box_run_p;
24361 else
24363 glyph->left_box_line_p = it->start_of_box_run_p;
24364 glyph->right_box_line_p = it->end_of_box_run_p;
24366 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24367 || it->phys_descent > it->descent);
24368 glyph->padding_p = 0;
24369 glyph->glyph_not_available_p = 0;
24370 glyph->face_id = face_id;
24371 glyph->font_type = FONT_TYPE_UNKNOWN;
24372 if (it->bidi_p)
24374 glyph->resolved_level = it->bidi_it.resolved_level;
24375 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24376 emacs_abort ();
24377 glyph->bidi_type = it->bidi_it.type;
24379 ++it->glyph_row->used[area];
24381 else
24382 IT_EXPAND_MATRIX_WIDTH (it, area);
24386 /* Produce a glyph for a glyphless character for iterator IT.
24387 IT->glyphless_method specifies which method to use for displaying
24388 the character. See the description of enum
24389 glyphless_display_method in dispextern.h for the detail.
24391 FOR_NO_FONT is nonzero if and only if this is for a character for
24392 which no font was found. ACRONYM, if non-nil, is an acronym string
24393 for the character. */
24395 static void
24396 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24398 int face_id;
24399 struct face *face;
24400 struct font *font;
24401 int base_width, base_height, width, height;
24402 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24403 int len;
24405 /* Get the metrics of the base font. We always refer to the current
24406 ASCII face. */
24407 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24408 font = face->font ? face->font : FRAME_FONT (it->f);
24409 it->ascent = FONT_BASE (font) + font->baseline_offset;
24410 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24411 base_height = it->ascent + it->descent;
24412 base_width = font->average_width;
24414 /* Get a face ID for the glyph by utilizing a cache (the same way as
24415 done for `escape-glyph' in get_next_display_element). */
24416 if (it->f == last_glyphless_glyph_frame
24417 && it->face_id == last_glyphless_glyph_face_id)
24419 face_id = last_glyphless_glyph_merged_face_id;
24421 else
24423 /* Merge the `glyphless-char' face into the current face. */
24424 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24425 last_glyphless_glyph_frame = it->f;
24426 last_glyphless_glyph_face_id = it->face_id;
24427 last_glyphless_glyph_merged_face_id = face_id;
24430 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24432 it->pixel_width = THIN_SPACE_WIDTH;
24433 len = 0;
24434 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24436 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24438 width = CHAR_WIDTH (it->c);
24439 if (width == 0)
24440 width = 1;
24441 else if (width > 4)
24442 width = 4;
24443 it->pixel_width = base_width * width;
24444 len = 0;
24445 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24447 else
24449 char buf[7];
24450 const char *str;
24451 unsigned int code[6];
24452 int upper_len;
24453 int ascent, descent;
24454 struct font_metrics metrics_upper, metrics_lower;
24456 face = FACE_FROM_ID (it->f, face_id);
24457 font = face->font ? face->font : FRAME_FONT (it->f);
24458 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24460 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24462 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24463 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24464 if (CONSP (acronym))
24465 acronym = XCAR (acronym);
24466 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24468 else
24470 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24471 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24472 str = buf;
24474 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24475 code[len] = font->driver->encode_char (font, str[len]);
24476 upper_len = (len + 1) / 2;
24477 font->driver->text_extents (font, code, upper_len,
24478 &metrics_upper);
24479 font->driver->text_extents (font, code + upper_len, len - upper_len,
24480 &metrics_lower);
24484 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24485 width = max (metrics_upper.width, metrics_lower.width) + 4;
24486 upper_xoff = upper_yoff = 2; /* the typical case */
24487 if (base_width >= width)
24489 /* Align the upper to the left, the lower to the right. */
24490 it->pixel_width = base_width;
24491 lower_xoff = base_width - 2 - metrics_lower.width;
24493 else
24495 /* Center the shorter one. */
24496 it->pixel_width = width;
24497 if (metrics_upper.width >= metrics_lower.width)
24498 lower_xoff = (width - metrics_lower.width) / 2;
24499 else
24501 /* FIXME: This code doesn't look right. It formerly was
24502 missing the "lower_xoff = 0;", which couldn't have
24503 been right since it left lower_xoff uninitialized. */
24504 lower_xoff = 0;
24505 upper_xoff = (width - metrics_upper.width) / 2;
24509 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24510 top, bottom, and between upper and lower strings. */
24511 height = (metrics_upper.ascent + metrics_upper.descent
24512 + metrics_lower.ascent + metrics_lower.descent) + 5;
24513 /* Center vertically.
24514 H:base_height, D:base_descent
24515 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24517 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24518 descent = D - H/2 + h/2;
24519 lower_yoff = descent - 2 - ld;
24520 upper_yoff = lower_yoff - la - 1 - ud; */
24521 ascent = - (it->descent - (base_height + height + 1) / 2);
24522 descent = it->descent - (base_height - height) / 2;
24523 lower_yoff = descent - 2 - metrics_lower.descent;
24524 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24525 - metrics_upper.descent);
24526 /* Don't make the height shorter than the base height. */
24527 if (height > base_height)
24529 it->ascent = ascent;
24530 it->descent = descent;
24534 it->phys_ascent = it->ascent;
24535 it->phys_descent = it->descent;
24536 if (it->glyph_row)
24537 append_glyphless_glyph (it, face_id, for_no_font, len,
24538 upper_xoff, upper_yoff,
24539 lower_xoff, lower_yoff);
24540 it->nglyphs = 1;
24541 take_vertical_position_into_account (it);
24545 /* RIF:
24546 Produce glyphs/get display metrics for the display element IT is
24547 loaded with. See the description of struct it in dispextern.h
24548 for an overview of struct it. */
24550 void
24551 x_produce_glyphs (struct it *it)
24553 int extra_line_spacing = it->extra_line_spacing;
24555 it->glyph_not_available_p = 0;
24557 if (it->what == IT_CHARACTER)
24559 XChar2b char2b;
24560 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24561 struct font *font = face->font;
24562 struct font_metrics *pcm = NULL;
24563 int boff; /* baseline offset */
24565 if (font == NULL)
24567 /* When no suitable font is found, display this character by
24568 the method specified in the first extra slot of
24569 Vglyphless_char_display. */
24570 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24572 eassert (it->what == IT_GLYPHLESS);
24573 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24574 goto done;
24577 boff = font->baseline_offset;
24578 if (font->vertical_centering)
24579 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24581 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24583 int stretched_p;
24585 it->nglyphs = 1;
24587 if (it->override_ascent >= 0)
24589 it->ascent = it->override_ascent;
24590 it->descent = it->override_descent;
24591 boff = it->override_boff;
24593 else
24595 it->ascent = FONT_BASE (font) + boff;
24596 it->descent = FONT_DESCENT (font) - boff;
24599 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24601 pcm = get_per_char_metric (font, &char2b);
24602 if (pcm->width == 0
24603 && pcm->rbearing == 0 && pcm->lbearing == 0)
24604 pcm = NULL;
24607 if (pcm)
24609 it->phys_ascent = pcm->ascent + boff;
24610 it->phys_descent = pcm->descent - boff;
24611 it->pixel_width = pcm->width;
24613 else
24615 it->glyph_not_available_p = 1;
24616 it->phys_ascent = it->ascent;
24617 it->phys_descent = it->descent;
24618 it->pixel_width = font->space_width;
24621 if (it->constrain_row_ascent_descent_p)
24623 if (it->descent > it->max_descent)
24625 it->ascent += it->descent - it->max_descent;
24626 it->descent = it->max_descent;
24628 if (it->ascent > it->max_ascent)
24630 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24631 it->ascent = it->max_ascent;
24633 it->phys_ascent = min (it->phys_ascent, it->ascent);
24634 it->phys_descent = min (it->phys_descent, it->descent);
24635 extra_line_spacing = 0;
24638 /* If this is a space inside a region of text with
24639 `space-width' property, change its width. */
24640 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24641 if (stretched_p)
24642 it->pixel_width *= XFLOATINT (it->space_width);
24644 /* If face has a box, add the box thickness to the character
24645 height. If character has a box line to the left and/or
24646 right, add the box line width to the character's width. */
24647 if (face->box != FACE_NO_BOX)
24649 int thick = face->box_line_width;
24651 if (thick > 0)
24653 it->ascent += thick;
24654 it->descent += thick;
24656 else
24657 thick = -thick;
24659 if (it->start_of_box_run_p)
24660 it->pixel_width += thick;
24661 if (it->end_of_box_run_p)
24662 it->pixel_width += thick;
24665 /* If face has an overline, add the height of the overline
24666 (1 pixel) and a 1 pixel margin to the character height. */
24667 if (face->overline_p)
24668 it->ascent += overline_margin;
24670 if (it->constrain_row_ascent_descent_p)
24672 if (it->ascent > it->max_ascent)
24673 it->ascent = it->max_ascent;
24674 if (it->descent > it->max_descent)
24675 it->descent = it->max_descent;
24678 take_vertical_position_into_account (it);
24680 /* If we have to actually produce glyphs, do it. */
24681 if (it->glyph_row)
24683 if (stretched_p)
24685 /* Translate a space with a `space-width' property
24686 into a stretch glyph. */
24687 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24688 / FONT_HEIGHT (font));
24689 append_stretch_glyph (it, it->object, it->pixel_width,
24690 it->ascent + it->descent, ascent);
24692 else
24693 append_glyph (it);
24695 /* If characters with lbearing or rbearing are displayed
24696 in this line, record that fact in a flag of the
24697 glyph row. This is used to optimize X output code. */
24698 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24699 it->glyph_row->contains_overlapping_glyphs_p = 1;
24701 if (! stretched_p && it->pixel_width == 0)
24702 /* We assure that all visible glyphs have at least 1-pixel
24703 width. */
24704 it->pixel_width = 1;
24706 else if (it->char_to_display == '\n')
24708 /* A newline has no width, but we need the height of the
24709 line. But if previous part of the line sets a height,
24710 don't increase that height */
24712 Lisp_Object height;
24713 Lisp_Object total_height = Qnil;
24715 it->override_ascent = -1;
24716 it->pixel_width = 0;
24717 it->nglyphs = 0;
24719 height = get_it_property (it, Qline_height);
24720 /* Split (line-height total-height) list */
24721 if (CONSP (height)
24722 && CONSP (XCDR (height))
24723 && NILP (XCDR (XCDR (height))))
24725 total_height = XCAR (XCDR (height));
24726 height = XCAR (height);
24728 height = calc_line_height_property (it, height, font, boff, 1);
24730 if (it->override_ascent >= 0)
24732 it->ascent = it->override_ascent;
24733 it->descent = it->override_descent;
24734 boff = it->override_boff;
24736 else
24738 it->ascent = FONT_BASE (font) + boff;
24739 it->descent = FONT_DESCENT (font) - boff;
24742 if (EQ (height, Qt))
24744 if (it->descent > it->max_descent)
24746 it->ascent += it->descent - it->max_descent;
24747 it->descent = it->max_descent;
24749 if (it->ascent > it->max_ascent)
24751 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24752 it->ascent = it->max_ascent;
24754 it->phys_ascent = min (it->phys_ascent, it->ascent);
24755 it->phys_descent = min (it->phys_descent, it->descent);
24756 it->constrain_row_ascent_descent_p = 1;
24757 extra_line_spacing = 0;
24759 else
24761 Lisp_Object spacing;
24763 it->phys_ascent = it->ascent;
24764 it->phys_descent = it->descent;
24766 if ((it->max_ascent > 0 || it->max_descent > 0)
24767 && face->box != FACE_NO_BOX
24768 && face->box_line_width > 0)
24770 it->ascent += face->box_line_width;
24771 it->descent += face->box_line_width;
24773 if (!NILP (height)
24774 && XINT (height) > it->ascent + it->descent)
24775 it->ascent = XINT (height) - it->descent;
24777 if (!NILP (total_height))
24778 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24779 else
24781 spacing = get_it_property (it, Qline_spacing);
24782 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24784 if (INTEGERP (spacing))
24786 extra_line_spacing = XINT (spacing);
24787 if (!NILP (total_height))
24788 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24792 else /* i.e. (it->char_to_display == '\t') */
24794 if (font->space_width > 0)
24796 int tab_width = it->tab_width * font->space_width;
24797 int x = it->current_x + it->continuation_lines_width;
24798 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24800 /* If the distance from the current position to the next tab
24801 stop is less than a space character width, use the
24802 tab stop after that. */
24803 if (next_tab_x - x < font->space_width)
24804 next_tab_x += tab_width;
24806 it->pixel_width = next_tab_x - x;
24807 it->nglyphs = 1;
24808 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24809 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24811 if (it->glyph_row)
24813 append_stretch_glyph (it, it->object, it->pixel_width,
24814 it->ascent + it->descent, it->ascent);
24817 else
24819 it->pixel_width = 0;
24820 it->nglyphs = 1;
24824 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24826 /* A static composition.
24828 Note: A composition is represented as one glyph in the
24829 glyph matrix. There are no padding glyphs.
24831 Important note: pixel_width, ascent, and descent are the
24832 values of what is drawn by draw_glyphs (i.e. the values of
24833 the overall glyphs composed). */
24834 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24835 int boff; /* baseline offset */
24836 struct composition *cmp = composition_table[it->cmp_it.id];
24837 int glyph_len = cmp->glyph_len;
24838 struct font *font = face->font;
24840 it->nglyphs = 1;
24842 /* If we have not yet calculated pixel size data of glyphs of
24843 the composition for the current face font, calculate them
24844 now. Theoretically, we have to check all fonts for the
24845 glyphs, but that requires much time and memory space. So,
24846 here we check only the font of the first glyph. This may
24847 lead to incorrect display, but it's very rare, and C-l
24848 (recenter-top-bottom) can correct the display anyway. */
24849 if (! cmp->font || cmp->font != font)
24851 /* Ascent and descent of the font of the first character
24852 of this composition (adjusted by baseline offset).
24853 Ascent and descent of overall glyphs should not be less
24854 than these, respectively. */
24855 int font_ascent, font_descent, font_height;
24856 /* Bounding box of the overall glyphs. */
24857 int leftmost, rightmost, lowest, highest;
24858 int lbearing, rbearing;
24859 int i, width, ascent, descent;
24860 int left_padded = 0, right_padded = 0;
24861 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24862 XChar2b char2b;
24863 struct font_metrics *pcm;
24864 int font_not_found_p;
24865 ptrdiff_t pos;
24867 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24868 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24869 break;
24870 if (glyph_len < cmp->glyph_len)
24871 right_padded = 1;
24872 for (i = 0; i < glyph_len; i++)
24874 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24875 break;
24876 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24878 if (i > 0)
24879 left_padded = 1;
24881 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24882 : IT_CHARPOS (*it));
24883 /* If no suitable font is found, use the default font. */
24884 font_not_found_p = font == NULL;
24885 if (font_not_found_p)
24887 face = face->ascii_face;
24888 font = face->font;
24890 boff = font->baseline_offset;
24891 if (font->vertical_centering)
24892 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24893 font_ascent = FONT_BASE (font) + boff;
24894 font_descent = FONT_DESCENT (font) - boff;
24895 font_height = FONT_HEIGHT (font);
24897 cmp->font = font;
24899 pcm = NULL;
24900 if (! font_not_found_p)
24902 get_char_face_and_encoding (it->f, c, it->face_id,
24903 &char2b, 0);
24904 pcm = get_per_char_metric (font, &char2b);
24907 /* Initialize the bounding box. */
24908 if (pcm)
24910 width = cmp->glyph_len > 0 ? pcm->width : 0;
24911 ascent = pcm->ascent;
24912 descent = pcm->descent;
24913 lbearing = pcm->lbearing;
24914 rbearing = pcm->rbearing;
24916 else
24918 width = cmp->glyph_len > 0 ? font->space_width : 0;
24919 ascent = FONT_BASE (font);
24920 descent = FONT_DESCENT (font);
24921 lbearing = 0;
24922 rbearing = width;
24925 rightmost = width;
24926 leftmost = 0;
24927 lowest = - descent + boff;
24928 highest = ascent + boff;
24930 if (! font_not_found_p
24931 && font->default_ascent
24932 && CHAR_TABLE_P (Vuse_default_ascent)
24933 && !NILP (Faref (Vuse_default_ascent,
24934 make_number (it->char_to_display))))
24935 highest = font->default_ascent + boff;
24937 /* Draw the first glyph at the normal position. It may be
24938 shifted to right later if some other glyphs are drawn
24939 at the left. */
24940 cmp->offsets[i * 2] = 0;
24941 cmp->offsets[i * 2 + 1] = boff;
24942 cmp->lbearing = lbearing;
24943 cmp->rbearing = rbearing;
24945 /* Set cmp->offsets for the remaining glyphs. */
24946 for (i++; i < glyph_len; i++)
24948 int left, right, btm, top;
24949 int ch = COMPOSITION_GLYPH (cmp, i);
24950 int face_id;
24951 struct face *this_face;
24953 if (ch == '\t')
24954 ch = ' ';
24955 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24956 this_face = FACE_FROM_ID (it->f, face_id);
24957 font = this_face->font;
24959 if (font == NULL)
24960 pcm = NULL;
24961 else
24963 get_char_face_and_encoding (it->f, ch, face_id,
24964 &char2b, 0);
24965 pcm = get_per_char_metric (font, &char2b);
24967 if (! pcm)
24968 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24969 else
24971 width = pcm->width;
24972 ascent = pcm->ascent;
24973 descent = pcm->descent;
24974 lbearing = pcm->lbearing;
24975 rbearing = pcm->rbearing;
24976 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24978 /* Relative composition with or without
24979 alternate chars. */
24980 left = (leftmost + rightmost - width) / 2;
24981 btm = - descent + boff;
24982 if (font->relative_compose
24983 && (! CHAR_TABLE_P (Vignore_relative_composition)
24984 || NILP (Faref (Vignore_relative_composition,
24985 make_number (ch)))))
24988 if (- descent >= font->relative_compose)
24989 /* One extra pixel between two glyphs. */
24990 btm = highest + 1;
24991 else if (ascent <= 0)
24992 /* One extra pixel between two glyphs. */
24993 btm = lowest - 1 - ascent - descent;
24996 else
24998 /* A composition rule is specified by an integer
24999 value that encodes global and new reference
25000 points (GREF and NREF). GREF and NREF are
25001 specified by numbers as below:
25003 0---1---2 -- ascent
25007 9--10--11 -- center
25009 ---3---4---5--- baseline
25011 6---7---8 -- descent
25013 int rule = COMPOSITION_RULE (cmp, i);
25014 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25016 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25017 grefx = gref % 3, nrefx = nref % 3;
25018 grefy = gref / 3, nrefy = nref / 3;
25019 if (xoff)
25020 xoff = font_height * (xoff - 128) / 256;
25021 if (yoff)
25022 yoff = font_height * (yoff - 128) / 256;
25024 left = (leftmost
25025 + grefx * (rightmost - leftmost) / 2
25026 - nrefx * width / 2
25027 + xoff);
25029 btm = ((grefy == 0 ? highest
25030 : grefy == 1 ? 0
25031 : grefy == 2 ? lowest
25032 : (highest + lowest) / 2)
25033 - (nrefy == 0 ? ascent + descent
25034 : nrefy == 1 ? descent - boff
25035 : nrefy == 2 ? 0
25036 : (ascent + descent) / 2)
25037 + yoff);
25040 cmp->offsets[i * 2] = left;
25041 cmp->offsets[i * 2 + 1] = btm + descent;
25043 /* Update the bounding box of the overall glyphs. */
25044 if (width > 0)
25046 right = left + width;
25047 if (left < leftmost)
25048 leftmost = left;
25049 if (right > rightmost)
25050 rightmost = right;
25052 top = btm + descent + ascent;
25053 if (top > highest)
25054 highest = top;
25055 if (btm < lowest)
25056 lowest = btm;
25058 if (cmp->lbearing > left + lbearing)
25059 cmp->lbearing = left + lbearing;
25060 if (cmp->rbearing < left + rbearing)
25061 cmp->rbearing = left + rbearing;
25065 /* If there are glyphs whose x-offsets are negative,
25066 shift all glyphs to the right and make all x-offsets
25067 non-negative. */
25068 if (leftmost < 0)
25070 for (i = 0; i < cmp->glyph_len; i++)
25071 cmp->offsets[i * 2] -= leftmost;
25072 rightmost -= leftmost;
25073 cmp->lbearing -= leftmost;
25074 cmp->rbearing -= leftmost;
25077 if (left_padded && cmp->lbearing < 0)
25079 for (i = 0; i < cmp->glyph_len; i++)
25080 cmp->offsets[i * 2] -= cmp->lbearing;
25081 rightmost -= cmp->lbearing;
25082 cmp->rbearing -= cmp->lbearing;
25083 cmp->lbearing = 0;
25085 if (right_padded && rightmost < cmp->rbearing)
25087 rightmost = cmp->rbearing;
25090 cmp->pixel_width = rightmost;
25091 cmp->ascent = highest;
25092 cmp->descent = - lowest;
25093 if (cmp->ascent < font_ascent)
25094 cmp->ascent = font_ascent;
25095 if (cmp->descent < font_descent)
25096 cmp->descent = font_descent;
25099 if (it->glyph_row
25100 && (cmp->lbearing < 0
25101 || cmp->rbearing > cmp->pixel_width))
25102 it->glyph_row->contains_overlapping_glyphs_p = 1;
25104 it->pixel_width = cmp->pixel_width;
25105 it->ascent = it->phys_ascent = cmp->ascent;
25106 it->descent = it->phys_descent = cmp->descent;
25107 if (face->box != FACE_NO_BOX)
25109 int thick = face->box_line_width;
25111 if (thick > 0)
25113 it->ascent += thick;
25114 it->descent += thick;
25116 else
25117 thick = - thick;
25119 if (it->start_of_box_run_p)
25120 it->pixel_width += thick;
25121 if (it->end_of_box_run_p)
25122 it->pixel_width += thick;
25125 /* If face has an overline, add the height of the overline
25126 (1 pixel) and a 1 pixel margin to the character height. */
25127 if (face->overline_p)
25128 it->ascent += overline_margin;
25130 take_vertical_position_into_account (it);
25131 if (it->ascent < 0)
25132 it->ascent = 0;
25133 if (it->descent < 0)
25134 it->descent = 0;
25136 if (it->glyph_row && cmp->glyph_len > 0)
25137 append_composite_glyph (it);
25139 else if (it->what == IT_COMPOSITION)
25141 /* A dynamic (automatic) composition. */
25142 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25143 Lisp_Object gstring;
25144 struct font_metrics metrics;
25146 it->nglyphs = 1;
25148 gstring = composition_gstring_from_id (it->cmp_it.id);
25149 it->pixel_width
25150 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25151 &metrics);
25152 if (it->glyph_row
25153 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25154 it->glyph_row->contains_overlapping_glyphs_p = 1;
25155 it->ascent = it->phys_ascent = metrics.ascent;
25156 it->descent = it->phys_descent = metrics.descent;
25157 if (face->box != FACE_NO_BOX)
25159 int thick = face->box_line_width;
25161 if (thick > 0)
25163 it->ascent += thick;
25164 it->descent += thick;
25166 else
25167 thick = - thick;
25169 if (it->start_of_box_run_p)
25170 it->pixel_width += thick;
25171 if (it->end_of_box_run_p)
25172 it->pixel_width += thick;
25174 /* If face has an overline, add the height of the overline
25175 (1 pixel) and a 1 pixel margin to the character height. */
25176 if (face->overline_p)
25177 it->ascent += overline_margin;
25178 take_vertical_position_into_account (it);
25179 if (it->ascent < 0)
25180 it->ascent = 0;
25181 if (it->descent < 0)
25182 it->descent = 0;
25184 if (it->glyph_row)
25185 append_composite_glyph (it);
25187 else if (it->what == IT_GLYPHLESS)
25188 produce_glyphless_glyph (it, 0, Qnil);
25189 else if (it->what == IT_IMAGE)
25190 produce_image_glyph (it);
25191 else if (it->what == IT_STRETCH)
25192 produce_stretch_glyph (it);
25194 done:
25195 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25196 because this isn't true for images with `:ascent 100'. */
25197 eassert (it->ascent >= 0 && it->descent >= 0);
25198 if (it->area == TEXT_AREA)
25199 it->current_x += it->pixel_width;
25201 if (extra_line_spacing > 0)
25203 it->descent += extra_line_spacing;
25204 if (extra_line_spacing > it->max_extra_line_spacing)
25205 it->max_extra_line_spacing = extra_line_spacing;
25208 it->max_ascent = max (it->max_ascent, it->ascent);
25209 it->max_descent = max (it->max_descent, it->descent);
25210 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25211 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25214 /* EXPORT for RIF:
25215 Output LEN glyphs starting at START at the nominal cursor position.
25216 Advance the nominal cursor over the text. The global variable
25217 updated_window contains the window being updated, updated_row is
25218 the glyph row being updated, and updated_area is the area of that
25219 row being updated. */
25221 void
25222 x_write_glyphs (struct glyph *start, int len)
25224 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25226 eassert (updated_window && updated_row);
25227 /* When the window is hscrolled, cursor hpos can legitimately be out
25228 of bounds, but we draw the cursor at the corresponding window
25229 margin in that case. */
25230 if (!updated_row->reversed_p && chpos < 0)
25231 chpos = 0;
25232 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25233 chpos = updated_row->used[TEXT_AREA] - 1;
25235 block_input ();
25237 /* Write glyphs. */
25239 hpos = start - updated_row->glyphs[updated_area];
25240 x = draw_glyphs (updated_window, output_cursor.x,
25241 updated_row, updated_area,
25242 hpos, hpos + len,
25243 DRAW_NORMAL_TEXT, 0);
25245 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25246 if (updated_area == TEXT_AREA
25247 && updated_window->phys_cursor_on_p
25248 && updated_window->phys_cursor.vpos == output_cursor.vpos
25249 && chpos >= hpos
25250 && chpos < hpos + len)
25251 updated_window->phys_cursor_on_p = 0;
25253 unblock_input ();
25255 /* Advance the output cursor. */
25256 output_cursor.hpos += len;
25257 output_cursor.x = x;
25261 /* EXPORT for RIF:
25262 Insert LEN glyphs from START at the nominal cursor position. */
25264 void
25265 x_insert_glyphs (struct glyph *start, int len)
25267 struct frame *f;
25268 struct window *w;
25269 int line_height, shift_by_width, shifted_region_width;
25270 struct glyph_row *row;
25271 struct glyph *glyph;
25272 int frame_x, frame_y;
25273 ptrdiff_t hpos;
25275 eassert (updated_window && updated_row);
25276 block_input ();
25277 w = updated_window;
25278 f = XFRAME (WINDOW_FRAME (w));
25280 /* Get the height of the line we are in. */
25281 row = updated_row;
25282 line_height = row->height;
25284 /* Get the width of the glyphs to insert. */
25285 shift_by_width = 0;
25286 for (glyph = start; glyph < start + len; ++glyph)
25287 shift_by_width += glyph->pixel_width;
25289 /* Get the width of the region to shift right. */
25290 shifted_region_width = (window_box_width (w, updated_area)
25291 - output_cursor.x
25292 - shift_by_width);
25294 /* Shift right. */
25295 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25296 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25298 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25299 line_height, shift_by_width);
25301 /* Write the glyphs. */
25302 hpos = start - row->glyphs[updated_area];
25303 draw_glyphs (w, output_cursor.x, row, updated_area,
25304 hpos, hpos + len,
25305 DRAW_NORMAL_TEXT, 0);
25307 /* Advance the output cursor. */
25308 output_cursor.hpos += len;
25309 output_cursor.x += shift_by_width;
25310 unblock_input ();
25314 /* EXPORT for RIF:
25315 Erase the current text line from the nominal cursor position
25316 (inclusive) to pixel column TO_X (exclusive). The idea is that
25317 everything from TO_X onward is already erased.
25319 TO_X is a pixel position relative to updated_area of
25320 updated_window. TO_X == -1 means clear to the end of this area. */
25322 void
25323 x_clear_end_of_line (int to_x)
25325 struct frame *f;
25326 struct window *w = updated_window;
25327 int max_x, min_y, max_y;
25328 int from_x, from_y, to_y;
25330 eassert (updated_window && updated_row);
25331 f = XFRAME (w->frame);
25333 if (updated_row->full_width_p)
25334 max_x = WINDOW_TOTAL_WIDTH (w);
25335 else
25336 max_x = window_box_width (w, updated_area);
25337 max_y = window_text_bottom_y (w);
25339 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25340 of window. For TO_X > 0, truncate to end of drawing area. */
25341 if (to_x == 0)
25342 return;
25343 else if (to_x < 0)
25344 to_x = max_x;
25345 else
25346 to_x = min (to_x, max_x);
25348 to_y = min (max_y, output_cursor.y + updated_row->height);
25350 /* Notice if the cursor will be cleared by this operation. */
25351 if (!updated_row->full_width_p)
25352 notice_overwritten_cursor (w, updated_area,
25353 output_cursor.x, -1,
25354 updated_row->y,
25355 MATRIX_ROW_BOTTOM_Y (updated_row));
25357 from_x = output_cursor.x;
25359 /* Translate to frame coordinates. */
25360 if (updated_row->full_width_p)
25362 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25363 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25365 else
25367 int area_left = window_box_left (w, updated_area);
25368 from_x += area_left;
25369 to_x += area_left;
25372 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25373 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25374 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25376 /* Prevent inadvertently clearing to end of the X window. */
25377 if (to_x > from_x && to_y > from_y)
25379 block_input ();
25380 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25381 to_x - from_x, to_y - from_y);
25382 unblock_input ();
25386 #endif /* HAVE_WINDOW_SYSTEM */
25390 /***********************************************************************
25391 Cursor types
25392 ***********************************************************************/
25394 /* Value is the internal representation of the specified cursor type
25395 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25396 of the bar cursor. */
25398 static enum text_cursor_kinds
25399 get_specified_cursor_type (Lisp_Object arg, int *width)
25401 enum text_cursor_kinds type;
25403 if (NILP (arg))
25404 return NO_CURSOR;
25406 if (EQ (arg, Qbox))
25407 return FILLED_BOX_CURSOR;
25409 if (EQ (arg, Qhollow))
25410 return HOLLOW_BOX_CURSOR;
25412 if (EQ (arg, Qbar))
25414 *width = 2;
25415 return BAR_CURSOR;
25418 if (CONSP (arg)
25419 && EQ (XCAR (arg), Qbar)
25420 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25422 *width = XINT (XCDR (arg));
25423 return BAR_CURSOR;
25426 if (EQ (arg, Qhbar))
25428 *width = 2;
25429 return HBAR_CURSOR;
25432 if (CONSP (arg)
25433 && EQ (XCAR (arg), Qhbar)
25434 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25436 *width = XINT (XCDR (arg));
25437 return HBAR_CURSOR;
25440 /* Treat anything unknown as "hollow box cursor".
25441 It was bad to signal an error; people have trouble fixing
25442 .Xdefaults with Emacs, when it has something bad in it. */
25443 type = HOLLOW_BOX_CURSOR;
25445 return type;
25448 /* Set the default cursor types for specified frame. */
25449 void
25450 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25452 int width = 1;
25453 Lisp_Object tem;
25455 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25456 FRAME_CURSOR_WIDTH (f) = width;
25458 /* By default, set up the blink-off state depending on the on-state. */
25460 tem = Fassoc (arg, Vblink_cursor_alist);
25461 if (!NILP (tem))
25463 FRAME_BLINK_OFF_CURSOR (f)
25464 = get_specified_cursor_type (XCDR (tem), &width);
25465 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25467 else
25468 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25472 #ifdef HAVE_WINDOW_SYSTEM
25474 /* Return the cursor we want to be displayed in window W. Return
25475 width of bar/hbar cursor through WIDTH arg. Return with
25476 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25477 (i.e. if the `system caret' should track this cursor).
25479 In a mini-buffer window, we want the cursor only to appear if we
25480 are reading input from this window. For the selected window, we
25481 want the cursor type given by the frame parameter or buffer local
25482 setting of cursor-type. If explicitly marked off, draw no cursor.
25483 In all other cases, we want a hollow box cursor. */
25485 static enum text_cursor_kinds
25486 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25487 int *active_cursor)
25489 struct frame *f = XFRAME (w->frame);
25490 struct buffer *b = XBUFFER (w->contents);
25491 int cursor_type = DEFAULT_CURSOR;
25492 Lisp_Object alt_cursor;
25493 int non_selected = 0;
25495 *active_cursor = 1;
25497 /* Echo area */
25498 if (cursor_in_echo_area
25499 && FRAME_HAS_MINIBUF_P (f)
25500 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25502 if (w == XWINDOW (echo_area_window))
25504 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25506 *width = FRAME_CURSOR_WIDTH (f);
25507 return FRAME_DESIRED_CURSOR (f);
25509 else
25510 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25513 *active_cursor = 0;
25514 non_selected = 1;
25517 /* Detect a nonselected window or nonselected frame. */
25518 else if (w != XWINDOW (f->selected_window)
25519 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25521 *active_cursor = 0;
25523 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25524 return NO_CURSOR;
25526 non_selected = 1;
25529 /* Never display a cursor in a window in which cursor-type is nil. */
25530 if (NILP (BVAR (b, cursor_type)))
25531 return NO_CURSOR;
25533 /* Get the normal cursor type for this window. */
25534 if (EQ (BVAR (b, cursor_type), Qt))
25536 cursor_type = FRAME_DESIRED_CURSOR (f);
25537 *width = FRAME_CURSOR_WIDTH (f);
25539 else
25540 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25542 /* Use cursor-in-non-selected-windows instead
25543 for non-selected window or frame. */
25544 if (non_selected)
25546 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25547 if (!EQ (Qt, alt_cursor))
25548 return get_specified_cursor_type (alt_cursor, width);
25549 /* t means modify the normal cursor type. */
25550 if (cursor_type == FILLED_BOX_CURSOR)
25551 cursor_type = HOLLOW_BOX_CURSOR;
25552 else if (cursor_type == BAR_CURSOR && *width > 1)
25553 --*width;
25554 return cursor_type;
25557 /* Use normal cursor if not blinked off. */
25558 if (!w->cursor_off_p)
25560 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25562 if (cursor_type == FILLED_BOX_CURSOR)
25564 /* Using a block cursor on large images can be very annoying.
25565 So use a hollow cursor for "large" images.
25566 If image is not transparent (no mask), also use hollow cursor. */
25567 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25568 if (img != NULL && IMAGEP (img->spec))
25570 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25571 where N = size of default frame font size.
25572 This should cover most of the "tiny" icons people may use. */
25573 if (!img->mask
25574 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25575 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25576 cursor_type = HOLLOW_BOX_CURSOR;
25579 else if (cursor_type != NO_CURSOR)
25581 /* Display current only supports BOX and HOLLOW cursors for images.
25582 So for now, unconditionally use a HOLLOW cursor when cursor is
25583 not a solid box cursor. */
25584 cursor_type = HOLLOW_BOX_CURSOR;
25587 return cursor_type;
25590 /* Cursor is blinked off, so determine how to "toggle" it. */
25592 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25593 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25594 return get_specified_cursor_type (XCDR (alt_cursor), width);
25596 /* Then see if frame has specified a specific blink off cursor type. */
25597 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25599 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25600 return FRAME_BLINK_OFF_CURSOR (f);
25603 #if 0
25604 /* Some people liked having a permanently visible blinking cursor,
25605 while others had very strong opinions against it. So it was
25606 decided to remove it. KFS 2003-09-03 */
25608 /* Finally perform built-in cursor blinking:
25609 filled box <-> hollow box
25610 wide [h]bar <-> narrow [h]bar
25611 narrow [h]bar <-> no cursor
25612 other type <-> no cursor */
25614 if (cursor_type == FILLED_BOX_CURSOR)
25615 return HOLLOW_BOX_CURSOR;
25617 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25619 *width = 1;
25620 return cursor_type;
25622 #endif
25624 return NO_CURSOR;
25628 /* Notice when the text cursor of window W has been completely
25629 overwritten by a drawing operation that outputs glyphs in AREA
25630 starting at X0 and ending at X1 in the line starting at Y0 and
25631 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25632 the rest of the line after X0 has been written. Y coordinates
25633 are window-relative. */
25635 static void
25636 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25637 int x0, int x1, int y0, int y1)
25639 int cx0, cx1, cy0, cy1;
25640 struct glyph_row *row;
25642 if (!w->phys_cursor_on_p)
25643 return;
25644 if (area != TEXT_AREA)
25645 return;
25647 if (w->phys_cursor.vpos < 0
25648 || w->phys_cursor.vpos >= w->current_matrix->nrows
25649 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25650 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
25651 return;
25653 if (row->cursor_in_fringe_p)
25655 row->cursor_in_fringe_p = 0;
25656 draw_fringe_bitmap (w, row, row->reversed_p);
25657 w->phys_cursor_on_p = 0;
25658 return;
25661 cx0 = w->phys_cursor.x;
25662 cx1 = cx0 + w->phys_cursor_width;
25663 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25664 return;
25666 /* The cursor image will be completely removed from the
25667 screen if the output area intersects the cursor area in
25668 y-direction. When we draw in [y0 y1[, and some part of
25669 the cursor is at y < y0, that part must have been drawn
25670 before. When scrolling, the cursor is erased before
25671 actually scrolling, so we don't come here. When not
25672 scrolling, the rows above the old cursor row must have
25673 changed, and in this case these rows must have written
25674 over the cursor image.
25676 Likewise if part of the cursor is below y1, with the
25677 exception of the cursor being in the first blank row at
25678 the buffer and window end because update_text_area
25679 doesn't draw that row. (Except when it does, but
25680 that's handled in update_text_area.) */
25682 cy0 = w->phys_cursor.y;
25683 cy1 = cy0 + w->phys_cursor_height;
25684 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25685 return;
25687 w->phys_cursor_on_p = 0;
25690 #endif /* HAVE_WINDOW_SYSTEM */
25693 /************************************************************************
25694 Mouse Face
25695 ************************************************************************/
25697 #ifdef HAVE_WINDOW_SYSTEM
25699 /* EXPORT for RIF:
25700 Fix the display of area AREA of overlapping row ROW in window W
25701 with respect to the overlapping part OVERLAPS. */
25703 void
25704 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25705 enum glyph_row_area area, int overlaps)
25707 int i, x;
25709 block_input ();
25711 x = 0;
25712 for (i = 0; i < row->used[area];)
25714 if (row->glyphs[area][i].overlaps_vertically_p)
25716 int start = i, start_x = x;
25720 x += row->glyphs[area][i].pixel_width;
25721 ++i;
25723 while (i < row->used[area]
25724 && row->glyphs[area][i].overlaps_vertically_p);
25726 draw_glyphs (w, start_x, row, area,
25727 start, i,
25728 DRAW_NORMAL_TEXT, overlaps);
25730 else
25732 x += row->glyphs[area][i].pixel_width;
25733 ++i;
25737 unblock_input ();
25741 /* EXPORT:
25742 Draw the cursor glyph of window W in glyph row ROW. See the
25743 comment of draw_glyphs for the meaning of HL. */
25745 void
25746 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25747 enum draw_glyphs_face hl)
25749 /* If cursor hpos is out of bounds, don't draw garbage. This can
25750 happen in mini-buffer windows when switching between echo area
25751 glyphs and mini-buffer. */
25752 if ((row->reversed_p
25753 ? (w->phys_cursor.hpos >= 0)
25754 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25756 int on_p = w->phys_cursor_on_p;
25757 int x1;
25758 int hpos = w->phys_cursor.hpos;
25760 /* When the window is hscrolled, cursor hpos can legitimately be
25761 out of bounds, but we draw the cursor at the corresponding
25762 window margin in that case. */
25763 if (!row->reversed_p && hpos < 0)
25764 hpos = 0;
25765 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25766 hpos = row->used[TEXT_AREA] - 1;
25768 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25769 hl, 0);
25770 w->phys_cursor_on_p = on_p;
25772 if (hl == DRAW_CURSOR)
25773 w->phys_cursor_width = x1 - w->phys_cursor.x;
25774 /* When we erase the cursor, and ROW is overlapped by other
25775 rows, make sure that these overlapping parts of other rows
25776 are redrawn. */
25777 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25779 w->phys_cursor_width = x1 - w->phys_cursor.x;
25781 if (row > w->current_matrix->rows
25782 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25783 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25784 OVERLAPS_ERASED_CURSOR);
25786 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25787 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25788 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25789 OVERLAPS_ERASED_CURSOR);
25795 /* EXPORT:
25796 Erase the image of a cursor of window W from the screen. */
25798 void
25799 erase_phys_cursor (struct window *w)
25801 struct frame *f = XFRAME (w->frame);
25802 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25803 int hpos = w->phys_cursor.hpos;
25804 int vpos = w->phys_cursor.vpos;
25805 int mouse_face_here_p = 0;
25806 struct glyph_matrix *active_glyphs = w->current_matrix;
25807 struct glyph_row *cursor_row;
25808 struct glyph *cursor_glyph;
25809 enum draw_glyphs_face hl;
25811 /* No cursor displayed or row invalidated => nothing to do on the
25812 screen. */
25813 if (w->phys_cursor_type == NO_CURSOR)
25814 goto mark_cursor_off;
25816 /* VPOS >= active_glyphs->nrows means that window has been resized.
25817 Don't bother to erase the cursor. */
25818 if (vpos >= active_glyphs->nrows)
25819 goto mark_cursor_off;
25821 /* If row containing cursor is marked invalid, there is nothing we
25822 can do. */
25823 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25824 if (!cursor_row->enabled_p)
25825 goto mark_cursor_off;
25827 /* If line spacing is > 0, old cursor may only be partially visible in
25828 window after split-window. So adjust visible height. */
25829 cursor_row->visible_height = min (cursor_row->visible_height,
25830 window_text_bottom_y (w) - cursor_row->y);
25832 /* If row is completely invisible, don't attempt to delete a cursor which
25833 isn't there. This can happen if cursor is at top of a window, and
25834 we switch to a buffer with a header line in that window. */
25835 if (cursor_row->visible_height <= 0)
25836 goto mark_cursor_off;
25838 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25839 if (cursor_row->cursor_in_fringe_p)
25841 cursor_row->cursor_in_fringe_p = 0;
25842 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25843 goto mark_cursor_off;
25846 /* This can happen when the new row is shorter than the old one.
25847 In this case, either draw_glyphs or clear_end_of_line
25848 should have cleared the cursor. Note that we wouldn't be
25849 able to erase the cursor in this case because we don't have a
25850 cursor glyph at hand. */
25851 if ((cursor_row->reversed_p
25852 ? (w->phys_cursor.hpos < 0)
25853 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25854 goto mark_cursor_off;
25856 /* When the window is hscrolled, cursor hpos can legitimately be out
25857 of bounds, but we draw the cursor at the corresponding window
25858 margin in that case. */
25859 if (!cursor_row->reversed_p && hpos < 0)
25860 hpos = 0;
25861 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25862 hpos = cursor_row->used[TEXT_AREA] - 1;
25864 /* If the cursor is in the mouse face area, redisplay that when
25865 we clear the cursor. */
25866 if (! NILP (hlinfo->mouse_face_window)
25867 && coords_in_mouse_face_p (w, hpos, vpos)
25868 /* Don't redraw the cursor's spot in mouse face if it is at the
25869 end of a line (on a newline). The cursor appears there, but
25870 mouse highlighting does not. */
25871 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25872 mouse_face_here_p = 1;
25874 /* Maybe clear the display under the cursor. */
25875 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25877 int x, y, left_x;
25878 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25879 int width;
25881 cursor_glyph = get_phys_cursor_glyph (w);
25882 if (cursor_glyph == NULL)
25883 goto mark_cursor_off;
25885 width = cursor_glyph->pixel_width;
25886 left_x = window_box_left_offset (w, TEXT_AREA);
25887 x = w->phys_cursor.x;
25888 if (x < left_x)
25889 width -= left_x - x;
25890 width = min (width, window_box_width (w, TEXT_AREA) - x);
25891 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25892 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25894 if (width > 0)
25895 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25898 /* Erase the cursor by redrawing the character underneath it. */
25899 if (mouse_face_here_p)
25900 hl = DRAW_MOUSE_FACE;
25901 else
25902 hl = DRAW_NORMAL_TEXT;
25903 draw_phys_cursor_glyph (w, cursor_row, hl);
25905 mark_cursor_off:
25906 w->phys_cursor_on_p = 0;
25907 w->phys_cursor_type = NO_CURSOR;
25911 /* EXPORT:
25912 Display or clear cursor of window W. If ON is zero, clear the
25913 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25914 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25916 void
25917 display_and_set_cursor (struct window *w, int on,
25918 int hpos, int vpos, int x, int y)
25920 struct frame *f = XFRAME (w->frame);
25921 int new_cursor_type;
25922 int new_cursor_width;
25923 int active_cursor;
25924 struct glyph_row *glyph_row;
25925 struct glyph *glyph;
25927 /* This is pointless on invisible frames, and dangerous on garbaged
25928 windows and frames; in the latter case, the frame or window may
25929 be in the midst of changing its size, and x and y may be off the
25930 window. */
25931 if (! FRAME_VISIBLE_P (f)
25932 || FRAME_GARBAGED_P (f)
25933 || vpos >= w->current_matrix->nrows
25934 || hpos >= w->current_matrix->matrix_w)
25935 return;
25937 /* If cursor is off and we want it off, return quickly. */
25938 if (!on && !w->phys_cursor_on_p)
25939 return;
25941 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25942 /* If cursor row is not enabled, we don't really know where to
25943 display the cursor. */
25944 if (!glyph_row->enabled_p)
25946 w->phys_cursor_on_p = 0;
25947 return;
25950 glyph = NULL;
25951 if (!glyph_row->exact_window_width_line_p
25952 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25953 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25955 eassert (input_blocked_p ());
25957 /* Set new_cursor_type to the cursor we want to be displayed. */
25958 new_cursor_type = get_window_cursor_type (w, glyph,
25959 &new_cursor_width, &active_cursor);
25961 /* If cursor is currently being shown and we don't want it to be or
25962 it is in the wrong place, or the cursor type is not what we want,
25963 erase it. */
25964 if (w->phys_cursor_on_p
25965 && (!on
25966 || w->phys_cursor.x != x
25967 || w->phys_cursor.y != y
25968 || new_cursor_type != w->phys_cursor_type
25969 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25970 && new_cursor_width != w->phys_cursor_width)))
25971 erase_phys_cursor (w);
25973 /* Don't check phys_cursor_on_p here because that flag is only set
25974 to zero in some cases where we know that the cursor has been
25975 completely erased, to avoid the extra work of erasing the cursor
25976 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25977 still not be visible, or it has only been partly erased. */
25978 if (on)
25980 w->phys_cursor_ascent = glyph_row->ascent;
25981 w->phys_cursor_height = glyph_row->height;
25983 /* Set phys_cursor_.* before x_draw_.* is called because some
25984 of them may need the information. */
25985 w->phys_cursor.x = x;
25986 w->phys_cursor.y = glyph_row->y;
25987 w->phys_cursor.hpos = hpos;
25988 w->phys_cursor.vpos = vpos;
25991 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25992 new_cursor_type, new_cursor_width,
25993 on, active_cursor);
25997 /* Switch the display of W's cursor on or off, according to the value
25998 of ON. */
26000 static void
26001 update_window_cursor (struct window *w, int on)
26003 /* Don't update cursor in windows whose frame is in the process
26004 of being deleted. */
26005 if (w->current_matrix)
26007 int hpos = w->phys_cursor.hpos;
26008 int vpos = w->phys_cursor.vpos;
26009 struct glyph_row *row;
26011 if (vpos >= w->current_matrix->nrows
26012 || hpos >= w->current_matrix->matrix_w)
26013 return;
26015 row = MATRIX_ROW (w->current_matrix, vpos);
26017 /* When the window is hscrolled, cursor hpos can legitimately be
26018 out of bounds, but we draw the cursor at the corresponding
26019 window margin in that case. */
26020 if (!row->reversed_p && hpos < 0)
26021 hpos = 0;
26022 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26023 hpos = row->used[TEXT_AREA] - 1;
26025 block_input ();
26026 display_and_set_cursor (w, on, hpos, vpos,
26027 w->phys_cursor.x, w->phys_cursor.y);
26028 unblock_input ();
26033 /* Call update_window_cursor with parameter ON_P on all leaf windows
26034 in the window tree rooted at W. */
26036 static void
26037 update_cursor_in_window_tree (struct window *w, int on_p)
26039 while (w)
26041 if (WINDOWP (w->contents))
26042 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26043 else
26044 update_window_cursor (w, on_p);
26046 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26051 /* EXPORT:
26052 Display the cursor on window W, or clear it, according to ON_P.
26053 Don't change the cursor's position. */
26055 void
26056 x_update_cursor (struct frame *f, int on_p)
26058 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26062 /* EXPORT:
26063 Clear the cursor of window W to background color, and mark the
26064 cursor as not shown. This is used when the text where the cursor
26065 is about to be rewritten. */
26067 void
26068 x_clear_cursor (struct window *w)
26070 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26071 update_window_cursor (w, 0);
26074 #endif /* HAVE_WINDOW_SYSTEM */
26076 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26077 and MSDOS. */
26078 static void
26079 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26080 int start_hpos, int end_hpos,
26081 enum draw_glyphs_face draw)
26083 #ifdef HAVE_WINDOW_SYSTEM
26084 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26086 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26087 return;
26089 #endif
26090 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26091 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26092 #endif
26095 /* Display the active region described by mouse_face_* according to DRAW. */
26097 static void
26098 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26100 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26101 struct frame *f = XFRAME (WINDOW_FRAME (w));
26103 if (/* If window is in the process of being destroyed, don't bother
26104 to do anything. */
26105 w->current_matrix != NULL
26106 /* Don't update mouse highlight if hidden */
26107 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26108 /* Recognize when we are called to operate on rows that don't exist
26109 anymore. This can happen when a window is split. */
26110 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26112 int phys_cursor_on_p = w->phys_cursor_on_p;
26113 struct glyph_row *row, *first, *last;
26115 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26116 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26118 for (row = first; row <= last && row->enabled_p; ++row)
26120 int start_hpos, end_hpos, start_x;
26122 /* For all but the first row, the highlight starts at column 0. */
26123 if (row == first)
26125 /* R2L rows have BEG and END in reversed order, but the
26126 screen drawing geometry is always left to right. So
26127 we need to mirror the beginning and end of the
26128 highlighted area in R2L rows. */
26129 if (!row->reversed_p)
26131 start_hpos = hlinfo->mouse_face_beg_col;
26132 start_x = hlinfo->mouse_face_beg_x;
26134 else if (row == last)
26136 start_hpos = hlinfo->mouse_face_end_col;
26137 start_x = hlinfo->mouse_face_end_x;
26139 else
26141 start_hpos = 0;
26142 start_x = 0;
26145 else if (row->reversed_p && row == last)
26147 start_hpos = hlinfo->mouse_face_end_col;
26148 start_x = hlinfo->mouse_face_end_x;
26150 else
26152 start_hpos = 0;
26153 start_x = 0;
26156 if (row == last)
26158 if (!row->reversed_p)
26159 end_hpos = hlinfo->mouse_face_end_col;
26160 else if (row == first)
26161 end_hpos = hlinfo->mouse_face_beg_col;
26162 else
26164 end_hpos = row->used[TEXT_AREA];
26165 if (draw == DRAW_NORMAL_TEXT)
26166 row->fill_line_p = 1; /* Clear to end of line */
26169 else if (row->reversed_p && row == first)
26170 end_hpos = hlinfo->mouse_face_beg_col;
26171 else
26173 end_hpos = row->used[TEXT_AREA];
26174 if (draw == DRAW_NORMAL_TEXT)
26175 row->fill_line_p = 1; /* Clear to end of line */
26178 if (end_hpos > start_hpos)
26180 draw_row_with_mouse_face (w, start_x, row,
26181 start_hpos, end_hpos, draw);
26183 row->mouse_face_p
26184 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26188 #ifdef HAVE_WINDOW_SYSTEM
26189 /* When we've written over the cursor, arrange for it to
26190 be displayed again. */
26191 if (FRAME_WINDOW_P (f)
26192 && phys_cursor_on_p && !w->phys_cursor_on_p)
26194 int hpos = w->phys_cursor.hpos;
26196 /* When the window is hscrolled, cursor hpos can legitimately be
26197 out of bounds, but we draw the cursor at the corresponding
26198 window margin in that case. */
26199 if (!row->reversed_p && hpos < 0)
26200 hpos = 0;
26201 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26202 hpos = row->used[TEXT_AREA] - 1;
26204 block_input ();
26205 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26206 w->phys_cursor.x, w->phys_cursor.y);
26207 unblock_input ();
26209 #endif /* HAVE_WINDOW_SYSTEM */
26212 #ifdef HAVE_WINDOW_SYSTEM
26213 /* Change the mouse cursor. */
26214 if (FRAME_WINDOW_P (f))
26216 if (draw == DRAW_NORMAL_TEXT
26217 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26218 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26219 else if (draw == DRAW_MOUSE_FACE)
26220 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26221 else
26222 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26224 #endif /* HAVE_WINDOW_SYSTEM */
26227 /* EXPORT:
26228 Clear out the mouse-highlighted active region.
26229 Redraw it un-highlighted first. Value is non-zero if mouse
26230 face was actually drawn unhighlighted. */
26233 clear_mouse_face (Mouse_HLInfo *hlinfo)
26235 int cleared = 0;
26237 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26239 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26240 cleared = 1;
26243 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26244 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26245 hlinfo->mouse_face_window = Qnil;
26246 hlinfo->mouse_face_overlay = Qnil;
26247 return cleared;
26250 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26251 within the mouse face on that window. */
26252 static int
26253 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26255 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26257 /* Quickly resolve the easy cases. */
26258 if (!(WINDOWP (hlinfo->mouse_face_window)
26259 && XWINDOW (hlinfo->mouse_face_window) == w))
26260 return 0;
26261 if (vpos < hlinfo->mouse_face_beg_row
26262 || vpos > hlinfo->mouse_face_end_row)
26263 return 0;
26264 if (vpos > hlinfo->mouse_face_beg_row
26265 && vpos < hlinfo->mouse_face_end_row)
26266 return 1;
26268 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26270 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26272 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26273 return 1;
26275 else if ((vpos == hlinfo->mouse_face_beg_row
26276 && hpos >= hlinfo->mouse_face_beg_col)
26277 || (vpos == hlinfo->mouse_face_end_row
26278 && hpos < hlinfo->mouse_face_end_col))
26279 return 1;
26281 else
26283 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26285 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26286 return 1;
26288 else if ((vpos == hlinfo->mouse_face_beg_row
26289 && hpos <= hlinfo->mouse_face_beg_col)
26290 || (vpos == hlinfo->mouse_face_end_row
26291 && hpos > hlinfo->mouse_face_end_col))
26292 return 1;
26294 return 0;
26298 /* EXPORT:
26299 Non-zero if physical cursor of window W is within mouse face. */
26302 cursor_in_mouse_face_p (struct window *w)
26304 int hpos = w->phys_cursor.hpos;
26305 int vpos = w->phys_cursor.vpos;
26306 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26308 /* When the window is hscrolled, cursor hpos can legitimately be out
26309 of bounds, but we draw the cursor at the corresponding window
26310 margin in that case. */
26311 if (!row->reversed_p && hpos < 0)
26312 hpos = 0;
26313 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26314 hpos = row->used[TEXT_AREA] - 1;
26316 return coords_in_mouse_face_p (w, hpos, vpos);
26321 /* Find the glyph rows START_ROW and END_ROW of window W that display
26322 characters between buffer positions START_CHARPOS and END_CHARPOS
26323 (excluding END_CHARPOS). DISP_STRING is a display string that
26324 covers these buffer positions. This is similar to
26325 row_containing_pos, but is more accurate when bidi reordering makes
26326 buffer positions change non-linearly with glyph rows. */
26327 static void
26328 rows_from_pos_range (struct window *w,
26329 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26330 Lisp_Object disp_string,
26331 struct glyph_row **start, struct glyph_row **end)
26333 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26334 int last_y = window_text_bottom_y (w);
26335 struct glyph_row *row;
26337 *start = NULL;
26338 *end = NULL;
26340 while (!first->enabled_p
26341 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26342 first++;
26344 /* Find the START row. */
26345 for (row = first;
26346 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26347 row++)
26349 /* A row can potentially be the START row if the range of the
26350 characters it displays intersects the range
26351 [START_CHARPOS..END_CHARPOS). */
26352 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26353 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26354 /* See the commentary in row_containing_pos, for the
26355 explanation of the complicated way to check whether
26356 some position is beyond the end of the characters
26357 displayed by a row. */
26358 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26359 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26360 && !row->ends_at_zv_p
26361 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26362 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26363 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26364 && !row->ends_at_zv_p
26365 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26367 /* Found a candidate row. Now make sure at least one of the
26368 glyphs it displays has a charpos from the range
26369 [START_CHARPOS..END_CHARPOS).
26371 This is not obvious because bidi reordering could make
26372 buffer positions of a row be 1,2,3,102,101,100, and if we
26373 want to highlight characters in [50..60), we don't want
26374 this row, even though [50..60) does intersect [1..103),
26375 the range of character positions given by the row's start
26376 and end positions. */
26377 struct glyph *g = row->glyphs[TEXT_AREA];
26378 struct glyph *e = g + row->used[TEXT_AREA];
26380 while (g < e)
26382 if (((BUFFERP (g->object) || INTEGERP (g->object))
26383 && start_charpos <= g->charpos && g->charpos < end_charpos)
26384 /* A glyph that comes from DISP_STRING is by
26385 definition to be highlighted. */
26386 || EQ (g->object, disp_string))
26387 *start = row;
26388 g++;
26390 if (*start)
26391 break;
26395 /* Find the END row. */
26396 if (!*start
26397 /* If the last row is partially visible, start looking for END
26398 from that row, instead of starting from FIRST. */
26399 && !(row->enabled_p
26400 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26401 row = first;
26402 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26404 struct glyph_row *next = row + 1;
26405 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26407 if (!next->enabled_p
26408 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26409 /* The first row >= START whose range of displayed characters
26410 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26411 is the row END + 1. */
26412 || (start_charpos < next_start
26413 && end_charpos < next_start)
26414 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26415 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26416 && !next->ends_at_zv_p
26417 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26418 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26419 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26420 && !next->ends_at_zv_p
26421 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26423 *end = row;
26424 break;
26426 else
26428 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26429 but none of the characters it displays are in the range, it is
26430 also END + 1. */
26431 struct glyph *g = next->glyphs[TEXT_AREA];
26432 struct glyph *s = g;
26433 struct glyph *e = g + next->used[TEXT_AREA];
26435 while (g < e)
26437 if (((BUFFERP (g->object) || INTEGERP (g->object))
26438 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26439 /* If the buffer position of the first glyph in
26440 the row is equal to END_CHARPOS, it means
26441 the last character to be highlighted is the
26442 newline of ROW, and we must consider NEXT as
26443 END, not END+1. */
26444 || (((!next->reversed_p && g == s)
26445 || (next->reversed_p && g == e - 1))
26446 && (g->charpos == end_charpos
26447 /* Special case for when NEXT is an
26448 empty line at ZV. */
26449 || (g->charpos == -1
26450 && !row->ends_at_zv_p
26451 && next_start == end_charpos)))))
26452 /* A glyph that comes from DISP_STRING is by
26453 definition to be highlighted. */
26454 || EQ (g->object, disp_string))
26455 break;
26456 g++;
26458 if (g == e)
26460 *end = row;
26461 break;
26463 /* The first row that ends at ZV must be the last to be
26464 highlighted. */
26465 else if (next->ends_at_zv_p)
26467 *end = next;
26468 break;
26474 /* This function sets the mouse_face_* elements of HLINFO, assuming
26475 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26476 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26477 for the overlay or run of text properties specifying the mouse
26478 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26479 before-string and after-string that must also be highlighted.
26480 DISP_STRING, if non-nil, is a display string that may cover some
26481 or all of the highlighted text. */
26483 static void
26484 mouse_face_from_buffer_pos (Lisp_Object window,
26485 Mouse_HLInfo *hlinfo,
26486 ptrdiff_t mouse_charpos,
26487 ptrdiff_t start_charpos,
26488 ptrdiff_t end_charpos,
26489 Lisp_Object before_string,
26490 Lisp_Object after_string,
26491 Lisp_Object disp_string)
26493 struct window *w = XWINDOW (window);
26494 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26495 struct glyph_row *r1, *r2;
26496 struct glyph *glyph, *end;
26497 ptrdiff_t ignore, pos;
26498 int x;
26500 eassert (NILP (disp_string) || STRINGP (disp_string));
26501 eassert (NILP (before_string) || STRINGP (before_string));
26502 eassert (NILP (after_string) || STRINGP (after_string));
26504 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26505 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26506 if (r1 == NULL)
26507 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26508 /* If the before-string or display-string contains newlines,
26509 rows_from_pos_range skips to its last row. Move back. */
26510 if (!NILP (before_string) || !NILP (disp_string))
26512 struct glyph_row *prev;
26513 while ((prev = r1 - 1, prev >= first)
26514 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26515 && prev->used[TEXT_AREA] > 0)
26517 struct glyph *beg = prev->glyphs[TEXT_AREA];
26518 glyph = beg + prev->used[TEXT_AREA];
26519 while (--glyph >= beg && INTEGERP (glyph->object));
26520 if (glyph < beg
26521 || !(EQ (glyph->object, before_string)
26522 || EQ (glyph->object, disp_string)))
26523 break;
26524 r1 = prev;
26527 if (r2 == NULL)
26529 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26530 hlinfo->mouse_face_past_end = 1;
26532 else if (!NILP (after_string))
26534 /* If the after-string has newlines, advance to its last row. */
26535 struct glyph_row *next;
26536 struct glyph_row *last
26537 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26539 for (next = r2 + 1;
26540 next <= last
26541 && next->used[TEXT_AREA] > 0
26542 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26543 ++next)
26544 r2 = next;
26546 /* The rest of the display engine assumes that mouse_face_beg_row is
26547 either above mouse_face_end_row or identical to it. But with
26548 bidi-reordered continued lines, the row for START_CHARPOS could
26549 be below the row for END_CHARPOS. If so, swap the rows and store
26550 them in correct order. */
26551 if (r1->y > r2->y)
26553 struct glyph_row *tem = r2;
26555 r2 = r1;
26556 r1 = tem;
26559 hlinfo->mouse_face_beg_y = r1->y;
26560 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26561 hlinfo->mouse_face_end_y = r2->y;
26562 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26564 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26565 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26566 could be anywhere in the row and in any order. The strategy
26567 below is to find the leftmost and the rightmost glyph that
26568 belongs to either of these 3 strings, or whose position is
26569 between START_CHARPOS and END_CHARPOS, and highlight all the
26570 glyphs between those two. This may cover more than just the text
26571 between START_CHARPOS and END_CHARPOS if the range of characters
26572 strides the bidi level boundary, e.g. if the beginning is in R2L
26573 text while the end is in L2R text or vice versa. */
26574 if (!r1->reversed_p)
26576 /* This row is in a left to right paragraph. Scan it left to
26577 right. */
26578 glyph = r1->glyphs[TEXT_AREA];
26579 end = glyph + r1->used[TEXT_AREA];
26580 x = r1->x;
26582 /* Skip truncation glyphs at the start of the glyph row. */
26583 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
26584 for (; glyph < end
26585 && INTEGERP (glyph->object)
26586 && glyph->charpos < 0;
26587 ++glyph)
26588 x += glyph->pixel_width;
26590 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26591 or DISP_STRING, and the first glyph from buffer whose
26592 position is between START_CHARPOS and END_CHARPOS. */
26593 for (; glyph < end
26594 && !INTEGERP (glyph->object)
26595 && !EQ (glyph->object, disp_string)
26596 && !(BUFFERP (glyph->object)
26597 && (glyph->charpos >= start_charpos
26598 && glyph->charpos < end_charpos));
26599 ++glyph)
26601 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26602 are present at buffer positions between START_CHARPOS and
26603 END_CHARPOS, or if they come from an overlay. */
26604 if (EQ (glyph->object, before_string))
26606 pos = string_buffer_position (before_string,
26607 start_charpos);
26608 /* If pos == 0, it means before_string came from an
26609 overlay, not from a buffer position. */
26610 if (!pos || (pos >= start_charpos && pos < end_charpos))
26611 break;
26613 else if (EQ (glyph->object, after_string))
26615 pos = string_buffer_position (after_string, end_charpos);
26616 if (!pos || (pos >= start_charpos && pos < end_charpos))
26617 break;
26619 x += glyph->pixel_width;
26621 hlinfo->mouse_face_beg_x = x;
26622 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26624 else
26626 /* This row is in a right to left paragraph. Scan it right to
26627 left. */
26628 struct glyph *g;
26630 end = r1->glyphs[TEXT_AREA] - 1;
26631 glyph = end + r1->used[TEXT_AREA];
26633 /* Skip truncation glyphs at the start of the glyph row. */
26634 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
26635 for (; glyph > end
26636 && INTEGERP (glyph->object)
26637 && glyph->charpos < 0;
26638 --glyph)
26641 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26642 or DISP_STRING, and the first glyph from buffer whose
26643 position is between START_CHARPOS and END_CHARPOS. */
26644 for (; glyph > end
26645 && !INTEGERP (glyph->object)
26646 && !EQ (glyph->object, disp_string)
26647 && !(BUFFERP (glyph->object)
26648 && (glyph->charpos >= start_charpos
26649 && glyph->charpos < end_charpos));
26650 --glyph)
26652 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26653 are present at buffer positions between START_CHARPOS and
26654 END_CHARPOS, or if they come from an overlay. */
26655 if (EQ (glyph->object, before_string))
26657 pos = string_buffer_position (before_string, start_charpos);
26658 /* If pos == 0, it means before_string came from an
26659 overlay, not from a buffer position. */
26660 if (!pos || (pos >= start_charpos && pos < end_charpos))
26661 break;
26663 else if (EQ (glyph->object, after_string))
26665 pos = string_buffer_position (after_string, end_charpos);
26666 if (!pos || (pos >= start_charpos && pos < end_charpos))
26667 break;
26671 glyph++; /* first glyph to the right of the highlighted area */
26672 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26673 x += g->pixel_width;
26674 hlinfo->mouse_face_beg_x = x;
26675 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26678 /* If the highlight ends in a different row, compute GLYPH and END
26679 for the end row. Otherwise, reuse the values computed above for
26680 the row where the highlight begins. */
26681 if (r2 != r1)
26683 if (!r2->reversed_p)
26685 glyph = r2->glyphs[TEXT_AREA];
26686 end = glyph + r2->used[TEXT_AREA];
26687 x = r2->x;
26689 else
26691 end = r2->glyphs[TEXT_AREA] - 1;
26692 glyph = end + r2->used[TEXT_AREA];
26696 if (!r2->reversed_p)
26698 /* Skip truncation and continuation glyphs near the end of the
26699 row, and also blanks and stretch glyphs inserted by
26700 extend_face_to_end_of_line. */
26701 while (end > glyph
26702 && INTEGERP ((end - 1)->object))
26703 --end;
26704 /* Scan the rest of the glyph row from the end, looking for the
26705 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26706 DISP_STRING, or whose position is between START_CHARPOS
26707 and END_CHARPOS */
26708 for (--end;
26709 end > glyph
26710 && !INTEGERP (end->object)
26711 && !EQ (end->object, disp_string)
26712 && !(BUFFERP (end->object)
26713 && (end->charpos >= start_charpos
26714 && end->charpos < end_charpos));
26715 --end)
26717 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26718 are present at buffer positions between START_CHARPOS and
26719 END_CHARPOS, or if they come from an overlay. */
26720 if (EQ (end->object, before_string))
26722 pos = string_buffer_position (before_string, start_charpos);
26723 if (!pos || (pos >= start_charpos && pos < end_charpos))
26724 break;
26726 else if (EQ (end->object, after_string))
26728 pos = string_buffer_position (after_string, end_charpos);
26729 if (!pos || (pos >= start_charpos && pos < end_charpos))
26730 break;
26733 /* Find the X coordinate of the last glyph to be highlighted. */
26734 for (; glyph <= end; ++glyph)
26735 x += glyph->pixel_width;
26737 hlinfo->mouse_face_end_x = x;
26738 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26740 else
26742 /* Skip truncation and continuation glyphs near the end of the
26743 row, and also blanks and stretch glyphs inserted by
26744 extend_face_to_end_of_line. */
26745 x = r2->x;
26746 end++;
26747 while (end < glyph
26748 && INTEGERP (end->object))
26750 x += end->pixel_width;
26751 ++end;
26753 /* Scan the rest of the glyph row from the end, looking for the
26754 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26755 DISP_STRING, or whose position is between START_CHARPOS
26756 and END_CHARPOS */
26757 for ( ;
26758 end < glyph
26759 && !INTEGERP (end->object)
26760 && !EQ (end->object, disp_string)
26761 && !(BUFFERP (end->object)
26762 && (end->charpos >= start_charpos
26763 && end->charpos < end_charpos));
26764 ++end)
26766 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26767 are present at buffer positions between START_CHARPOS and
26768 END_CHARPOS, or if they come from an overlay. */
26769 if (EQ (end->object, before_string))
26771 pos = string_buffer_position (before_string, start_charpos);
26772 if (!pos || (pos >= start_charpos && pos < end_charpos))
26773 break;
26775 else if (EQ (end->object, after_string))
26777 pos = string_buffer_position (after_string, end_charpos);
26778 if (!pos || (pos >= start_charpos && pos < end_charpos))
26779 break;
26781 x += end->pixel_width;
26783 /* If we exited the above loop because we arrived at the last
26784 glyph of the row, and its buffer position is still not in
26785 range, it means the last character in range is the preceding
26786 newline. Bump the end column and x values to get past the
26787 last glyph. */
26788 if (end == glyph
26789 && BUFFERP (end->object)
26790 && (end->charpos < start_charpos
26791 || end->charpos >= end_charpos))
26793 x += end->pixel_width;
26794 ++end;
26796 hlinfo->mouse_face_end_x = x;
26797 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26800 hlinfo->mouse_face_window = window;
26801 hlinfo->mouse_face_face_id
26802 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26803 mouse_charpos + 1,
26804 !hlinfo->mouse_face_hidden, -1);
26805 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26808 /* The following function is not used anymore (replaced with
26809 mouse_face_from_string_pos), but I leave it here for the time
26810 being, in case someone would. */
26812 #if 0 /* not used */
26814 /* Find the position of the glyph for position POS in OBJECT in
26815 window W's current matrix, and return in *X, *Y the pixel
26816 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26818 RIGHT_P non-zero means return the position of the right edge of the
26819 glyph, RIGHT_P zero means return the left edge position.
26821 If no glyph for POS exists in the matrix, return the position of
26822 the glyph with the next smaller position that is in the matrix, if
26823 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26824 exists in the matrix, return the position of the glyph with the
26825 next larger position in OBJECT.
26827 Value is non-zero if a glyph was found. */
26829 static int
26830 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26831 int *hpos, int *vpos, int *x, int *y, int right_p)
26833 int yb = window_text_bottom_y (w);
26834 struct glyph_row *r;
26835 struct glyph *best_glyph = NULL;
26836 struct glyph_row *best_row = NULL;
26837 int best_x = 0;
26839 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26840 r->enabled_p && r->y < yb;
26841 ++r)
26843 struct glyph *g = r->glyphs[TEXT_AREA];
26844 struct glyph *e = g + r->used[TEXT_AREA];
26845 int gx;
26847 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26848 if (EQ (g->object, object))
26850 if (g->charpos == pos)
26852 best_glyph = g;
26853 best_x = gx;
26854 best_row = r;
26855 goto found;
26857 else if (best_glyph == NULL
26858 || ((eabs (g->charpos - pos)
26859 < eabs (best_glyph->charpos - pos))
26860 && (right_p
26861 ? g->charpos < pos
26862 : g->charpos > pos)))
26864 best_glyph = g;
26865 best_x = gx;
26866 best_row = r;
26871 found:
26873 if (best_glyph)
26875 *x = best_x;
26876 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26878 if (right_p)
26880 *x += best_glyph->pixel_width;
26881 ++*hpos;
26884 *y = best_row->y;
26885 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
26888 return best_glyph != NULL;
26890 #endif /* not used */
26892 /* Find the positions of the first and the last glyphs in window W's
26893 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26894 (assumed to be a string), and return in HLINFO's mouse_face_*
26895 members the pixel and column/row coordinates of those glyphs. */
26897 static void
26898 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26899 Lisp_Object object,
26900 ptrdiff_t startpos, ptrdiff_t endpos)
26902 int yb = window_text_bottom_y (w);
26903 struct glyph_row *r;
26904 struct glyph *g, *e;
26905 int gx;
26906 int found = 0;
26908 /* Find the glyph row with at least one position in the range
26909 [STARTPOS..ENDPOS], and the first glyph in that row whose
26910 position belongs to that range. */
26911 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26912 r->enabled_p && r->y < yb;
26913 ++r)
26915 if (!r->reversed_p)
26917 g = r->glyphs[TEXT_AREA];
26918 e = g + r->used[TEXT_AREA];
26919 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26920 if (EQ (g->object, object)
26921 && startpos <= g->charpos && g->charpos <= endpos)
26923 hlinfo->mouse_face_beg_row
26924 = MATRIX_ROW_VPOS (r, w->current_matrix);
26925 hlinfo->mouse_face_beg_y = r->y;
26926 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26927 hlinfo->mouse_face_beg_x = gx;
26928 found = 1;
26929 break;
26932 else
26934 struct glyph *g1;
26936 e = r->glyphs[TEXT_AREA];
26937 g = e + r->used[TEXT_AREA];
26938 for ( ; g > e; --g)
26939 if (EQ ((g-1)->object, object)
26940 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26942 hlinfo->mouse_face_beg_row
26943 = MATRIX_ROW_VPOS (r, w->current_matrix);
26944 hlinfo->mouse_face_beg_y = r->y;
26945 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26946 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26947 gx += g1->pixel_width;
26948 hlinfo->mouse_face_beg_x = gx;
26949 found = 1;
26950 break;
26953 if (found)
26954 break;
26957 if (!found)
26958 return;
26960 /* Starting with the next row, look for the first row which does NOT
26961 include any glyphs whose positions are in the range. */
26962 for (++r; r->enabled_p && r->y < yb; ++r)
26964 g = r->glyphs[TEXT_AREA];
26965 e = g + r->used[TEXT_AREA];
26966 found = 0;
26967 for ( ; g < e; ++g)
26968 if (EQ (g->object, object)
26969 && startpos <= g->charpos && g->charpos <= endpos)
26971 found = 1;
26972 break;
26974 if (!found)
26975 break;
26978 /* The highlighted region ends on the previous row. */
26979 r--;
26981 /* Set the end row and its vertical pixel coordinate. */
26982 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
26983 hlinfo->mouse_face_end_y = r->y;
26985 /* Compute and set the end column and the end column's horizontal
26986 pixel coordinate. */
26987 if (!r->reversed_p)
26989 g = r->glyphs[TEXT_AREA];
26990 e = g + r->used[TEXT_AREA];
26991 for ( ; e > g; --e)
26992 if (EQ ((e-1)->object, object)
26993 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26994 break;
26995 hlinfo->mouse_face_end_col = e - g;
26997 for (gx = r->x; g < e; ++g)
26998 gx += g->pixel_width;
26999 hlinfo->mouse_face_end_x = gx;
27001 else
27003 e = r->glyphs[TEXT_AREA];
27004 g = e + r->used[TEXT_AREA];
27005 for (gx = r->x ; e < g; ++e)
27007 if (EQ (e->object, object)
27008 && startpos <= e->charpos && e->charpos <= endpos)
27009 break;
27010 gx += e->pixel_width;
27012 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27013 hlinfo->mouse_face_end_x = gx;
27017 #ifdef HAVE_WINDOW_SYSTEM
27019 /* See if position X, Y is within a hot-spot of an image. */
27021 static int
27022 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27024 if (!CONSP (hot_spot))
27025 return 0;
27027 if (EQ (XCAR (hot_spot), Qrect))
27029 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27030 Lisp_Object rect = XCDR (hot_spot);
27031 Lisp_Object tem;
27032 if (!CONSP (rect))
27033 return 0;
27034 if (!CONSP (XCAR (rect)))
27035 return 0;
27036 if (!CONSP (XCDR (rect)))
27037 return 0;
27038 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27039 return 0;
27040 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27041 return 0;
27042 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27043 return 0;
27044 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27045 return 0;
27046 return 1;
27048 else if (EQ (XCAR (hot_spot), Qcircle))
27050 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27051 Lisp_Object circ = XCDR (hot_spot);
27052 Lisp_Object lr, lx0, ly0;
27053 if (CONSP (circ)
27054 && CONSP (XCAR (circ))
27055 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27056 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27057 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27059 double r = XFLOATINT (lr);
27060 double dx = XINT (lx0) - x;
27061 double dy = XINT (ly0) - y;
27062 return (dx * dx + dy * dy <= r * r);
27065 else if (EQ (XCAR (hot_spot), Qpoly))
27067 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27068 if (VECTORP (XCDR (hot_spot)))
27070 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27071 Lisp_Object *poly = v->contents;
27072 ptrdiff_t n = v->header.size;
27073 ptrdiff_t i;
27074 int inside = 0;
27075 Lisp_Object lx, ly;
27076 int x0, y0;
27078 /* Need an even number of coordinates, and at least 3 edges. */
27079 if (n < 6 || n & 1)
27080 return 0;
27082 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27083 If count is odd, we are inside polygon. Pixels on edges
27084 may or may not be included depending on actual geometry of the
27085 polygon. */
27086 if ((lx = poly[n-2], !INTEGERP (lx))
27087 || (ly = poly[n-1], !INTEGERP (lx)))
27088 return 0;
27089 x0 = XINT (lx), y0 = XINT (ly);
27090 for (i = 0; i < n; i += 2)
27092 int x1 = x0, y1 = y0;
27093 if ((lx = poly[i], !INTEGERP (lx))
27094 || (ly = poly[i+1], !INTEGERP (ly)))
27095 return 0;
27096 x0 = XINT (lx), y0 = XINT (ly);
27098 /* Does this segment cross the X line? */
27099 if (x0 >= x)
27101 if (x1 >= x)
27102 continue;
27104 else if (x1 < x)
27105 continue;
27106 if (y > y0 && y > y1)
27107 continue;
27108 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27109 inside = !inside;
27111 return inside;
27114 return 0;
27117 Lisp_Object
27118 find_hot_spot (Lisp_Object map, int x, int y)
27120 while (CONSP (map))
27122 if (CONSP (XCAR (map))
27123 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27124 return XCAR (map);
27125 map = XCDR (map);
27128 return Qnil;
27131 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27132 3, 3, 0,
27133 doc: /* Lookup in image map MAP coordinates X and Y.
27134 An image map is an alist where each element has the format (AREA ID PLIST).
27135 An AREA is specified as either a rectangle, a circle, or a polygon:
27136 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27137 pixel coordinates of the upper left and bottom right corners.
27138 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27139 and the radius of the circle; r may be a float or integer.
27140 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27141 vector describes one corner in the polygon.
27142 Returns the alist element for the first matching AREA in MAP. */)
27143 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27145 if (NILP (map))
27146 return Qnil;
27148 CHECK_NUMBER (x);
27149 CHECK_NUMBER (y);
27151 return find_hot_spot (map,
27152 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27153 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27157 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27158 static void
27159 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27161 /* Do not change cursor shape while dragging mouse. */
27162 if (!NILP (do_mouse_tracking))
27163 return;
27165 if (!NILP (pointer))
27167 if (EQ (pointer, Qarrow))
27168 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27169 else if (EQ (pointer, Qhand))
27170 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27171 else if (EQ (pointer, Qtext))
27172 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27173 else if (EQ (pointer, intern ("hdrag")))
27174 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27175 #ifdef HAVE_X_WINDOWS
27176 else if (EQ (pointer, intern ("vdrag")))
27177 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27178 #endif
27179 else if (EQ (pointer, intern ("hourglass")))
27180 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27181 else if (EQ (pointer, Qmodeline))
27182 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27183 else
27184 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27187 if (cursor != No_Cursor)
27188 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27191 #endif /* HAVE_WINDOW_SYSTEM */
27193 /* Take proper action when mouse has moved to the mode or header line
27194 or marginal area AREA of window W, x-position X and y-position Y.
27195 X is relative to the start of the text display area of W, so the
27196 width of bitmap areas and scroll bars must be subtracted to get a
27197 position relative to the start of the mode line. */
27199 static void
27200 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27201 enum window_part area)
27203 struct window *w = XWINDOW (window);
27204 struct frame *f = XFRAME (w->frame);
27205 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27206 #ifdef HAVE_WINDOW_SYSTEM
27207 Display_Info *dpyinfo;
27208 #endif
27209 Cursor cursor = No_Cursor;
27210 Lisp_Object pointer = Qnil;
27211 int dx, dy, width, height;
27212 ptrdiff_t charpos;
27213 Lisp_Object string, object = Qnil;
27214 Lisp_Object pos IF_LINT (= Qnil), help;
27216 Lisp_Object mouse_face;
27217 int original_x_pixel = x;
27218 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27219 struct glyph_row *row IF_LINT (= 0);
27221 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27223 int x0;
27224 struct glyph *end;
27226 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27227 returns them in row/column units! */
27228 string = mode_line_string (w, area, &x, &y, &charpos,
27229 &object, &dx, &dy, &width, &height);
27231 row = (area == ON_MODE_LINE
27232 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27233 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27235 /* Find the glyph under the mouse pointer. */
27236 if (row->mode_line_p && row->enabled_p)
27238 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27239 end = glyph + row->used[TEXT_AREA];
27241 for (x0 = original_x_pixel;
27242 glyph < end && x0 >= glyph->pixel_width;
27243 ++glyph)
27244 x0 -= glyph->pixel_width;
27246 if (glyph >= end)
27247 glyph = NULL;
27250 else
27252 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27253 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27254 returns them in row/column units! */
27255 string = marginal_area_string (w, area, &x, &y, &charpos,
27256 &object, &dx, &dy, &width, &height);
27259 help = Qnil;
27261 #ifdef HAVE_WINDOW_SYSTEM
27262 if (IMAGEP (object))
27264 Lisp_Object image_map, hotspot;
27265 if ((image_map = Fplist_get (XCDR (object), QCmap),
27266 !NILP (image_map))
27267 && (hotspot = find_hot_spot (image_map, dx, dy),
27268 CONSP (hotspot))
27269 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27271 Lisp_Object plist;
27273 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27274 If so, we could look for mouse-enter, mouse-leave
27275 properties in PLIST (and do something...). */
27276 hotspot = XCDR (hotspot);
27277 if (CONSP (hotspot)
27278 && (plist = XCAR (hotspot), CONSP (plist)))
27280 pointer = Fplist_get (plist, Qpointer);
27281 if (NILP (pointer))
27282 pointer = Qhand;
27283 help = Fplist_get (plist, Qhelp_echo);
27284 if (!NILP (help))
27286 help_echo_string = help;
27287 XSETWINDOW (help_echo_window, w);
27288 help_echo_object = w->contents;
27289 help_echo_pos = charpos;
27293 if (NILP (pointer))
27294 pointer = Fplist_get (XCDR (object), QCpointer);
27296 #endif /* HAVE_WINDOW_SYSTEM */
27298 if (STRINGP (string))
27299 pos = make_number (charpos);
27301 /* Set the help text and mouse pointer. If the mouse is on a part
27302 of the mode line without any text (e.g. past the right edge of
27303 the mode line text), use the default help text and pointer. */
27304 if (STRINGP (string) || area == ON_MODE_LINE)
27306 /* Arrange to display the help by setting the global variables
27307 help_echo_string, help_echo_object, and help_echo_pos. */
27308 if (NILP (help))
27310 if (STRINGP (string))
27311 help = Fget_text_property (pos, Qhelp_echo, string);
27313 if (!NILP (help))
27315 help_echo_string = help;
27316 XSETWINDOW (help_echo_window, w);
27317 help_echo_object = string;
27318 help_echo_pos = charpos;
27320 else if (area == ON_MODE_LINE)
27322 Lisp_Object default_help
27323 = buffer_local_value_1 (Qmode_line_default_help_echo,
27324 w->contents);
27326 if (STRINGP (default_help))
27328 help_echo_string = default_help;
27329 XSETWINDOW (help_echo_window, w);
27330 help_echo_object = Qnil;
27331 help_echo_pos = -1;
27336 #ifdef HAVE_WINDOW_SYSTEM
27337 /* Change the mouse pointer according to what is under it. */
27338 if (FRAME_WINDOW_P (f))
27340 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27341 if (STRINGP (string))
27343 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27345 if (NILP (pointer))
27346 pointer = Fget_text_property (pos, Qpointer, string);
27348 /* Change the mouse pointer according to what is under X/Y. */
27349 if (NILP (pointer)
27350 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27352 Lisp_Object map;
27353 map = Fget_text_property (pos, Qlocal_map, string);
27354 if (!KEYMAPP (map))
27355 map = Fget_text_property (pos, Qkeymap, string);
27356 if (!KEYMAPP (map))
27357 cursor = dpyinfo->vertical_scroll_bar_cursor;
27360 else
27361 /* Default mode-line pointer. */
27362 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27364 #endif
27367 /* Change the mouse face according to what is under X/Y. */
27368 if (STRINGP (string))
27370 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27371 if (!NILP (mouse_face)
27372 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27373 && glyph)
27375 Lisp_Object b, e;
27377 struct glyph * tmp_glyph;
27379 int gpos;
27380 int gseq_length;
27381 int total_pixel_width;
27382 ptrdiff_t begpos, endpos, ignore;
27384 int vpos, hpos;
27386 b = Fprevious_single_property_change (make_number (charpos + 1),
27387 Qmouse_face, string, Qnil);
27388 if (NILP (b))
27389 begpos = 0;
27390 else
27391 begpos = XINT (b);
27393 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27394 if (NILP (e))
27395 endpos = SCHARS (string);
27396 else
27397 endpos = XINT (e);
27399 /* Calculate the glyph position GPOS of GLYPH in the
27400 displayed string, relative to the beginning of the
27401 highlighted part of the string.
27403 Note: GPOS is different from CHARPOS. CHARPOS is the
27404 position of GLYPH in the internal string object. A mode
27405 line string format has structures which are converted to
27406 a flattened string by the Emacs Lisp interpreter. The
27407 internal string is an element of those structures. The
27408 displayed string is the flattened string. */
27409 tmp_glyph = row_start_glyph;
27410 while (tmp_glyph < glyph
27411 && (!(EQ (tmp_glyph->object, glyph->object)
27412 && begpos <= tmp_glyph->charpos
27413 && tmp_glyph->charpos < endpos)))
27414 tmp_glyph++;
27415 gpos = glyph - tmp_glyph;
27417 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27418 the highlighted part of the displayed string to which
27419 GLYPH belongs. Note: GSEQ_LENGTH is different from
27420 SCHARS (STRING), because the latter returns the length of
27421 the internal string. */
27422 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27423 tmp_glyph > glyph
27424 && (!(EQ (tmp_glyph->object, glyph->object)
27425 && begpos <= tmp_glyph->charpos
27426 && tmp_glyph->charpos < endpos));
27427 tmp_glyph--)
27429 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27431 /* Calculate the total pixel width of all the glyphs between
27432 the beginning of the highlighted area and GLYPH. */
27433 total_pixel_width = 0;
27434 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27435 total_pixel_width += tmp_glyph->pixel_width;
27437 /* Pre calculation of re-rendering position. Note: X is in
27438 column units here, after the call to mode_line_string or
27439 marginal_area_string. */
27440 hpos = x - gpos;
27441 vpos = (area == ON_MODE_LINE
27442 ? (w->current_matrix)->nrows - 1
27443 : 0);
27445 /* If GLYPH's position is included in the region that is
27446 already drawn in mouse face, we have nothing to do. */
27447 if ( EQ (window, hlinfo->mouse_face_window)
27448 && (!row->reversed_p
27449 ? (hlinfo->mouse_face_beg_col <= hpos
27450 && hpos < hlinfo->mouse_face_end_col)
27451 /* In R2L rows we swap BEG and END, see below. */
27452 : (hlinfo->mouse_face_end_col <= hpos
27453 && hpos < hlinfo->mouse_face_beg_col))
27454 && hlinfo->mouse_face_beg_row == vpos )
27455 return;
27457 if (clear_mouse_face (hlinfo))
27458 cursor = No_Cursor;
27460 if (!row->reversed_p)
27462 hlinfo->mouse_face_beg_col = hpos;
27463 hlinfo->mouse_face_beg_x = original_x_pixel
27464 - (total_pixel_width + dx);
27465 hlinfo->mouse_face_end_col = hpos + gseq_length;
27466 hlinfo->mouse_face_end_x = 0;
27468 else
27470 /* In R2L rows, show_mouse_face expects BEG and END
27471 coordinates to be swapped. */
27472 hlinfo->mouse_face_end_col = hpos;
27473 hlinfo->mouse_face_end_x = original_x_pixel
27474 - (total_pixel_width + dx);
27475 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27476 hlinfo->mouse_face_beg_x = 0;
27479 hlinfo->mouse_face_beg_row = vpos;
27480 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27481 hlinfo->mouse_face_beg_y = 0;
27482 hlinfo->mouse_face_end_y = 0;
27483 hlinfo->mouse_face_past_end = 0;
27484 hlinfo->mouse_face_window = window;
27486 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27487 charpos,
27488 0, 0, 0,
27489 &ignore,
27490 glyph->face_id,
27492 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27494 if (NILP (pointer))
27495 pointer = Qhand;
27497 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27498 clear_mouse_face (hlinfo);
27500 #ifdef HAVE_WINDOW_SYSTEM
27501 if (FRAME_WINDOW_P (f))
27502 define_frame_cursor1 (f, cursor, pointer);
27503 #endif
27507 /* EXPORT:
27508 Take proper action when the mouse has moved to position X, Y on
27509 frame F as regards highlighting characters that have mouse-face
27510 properties. Also de-highlighting chars where the mouse was before.
27511 X and Y can be negative or out of range. */
27513 void
27514 note_mouse_highlight (struct frame *f, int x, int y)
27516 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27517 enum window_part part = ON_NOTHING;
27518 Lisp_Object window;
27519 struct window *w;
27520 Cursor cursor = No_Cursor;
27521 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27522 struct buffer *b;
27524 /* When a menu is active, don't highlight because this looks odd. */
27525 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27526 if (popup_activated ())
27527 return;
27528 #endif
27530 if (NILP (Vmouse_highlight)
27531 || !f->glyphs_initialized_p
27532 || f->pointer_invisible)
27533 return;
27535 hlinfo->mouse_face_mouse_x = x;
27536 hlinfo->mouse_face_mouse_y = y;
27537 hlinfo->mouse_face_mouse_frame = f;
27539 if (hlinfo->mouse_face_defer)
27540 return;
27542 /* Which window is that in? */
27543 window = window_from_coordinates (f, x, y, &part, 1);
27545 /* If displaying active text in another window, clear that. */
27546 if (! EQ (window, hlinfo->mouse_face_window)
27547 /* Also clear if we move out of text area in same window. */
27548 || (!NILP (hlinfo->mouse_face_window)
27549 && !NILP (window)
27550 && part != ON_TEXT
27551 && part != ON_MODE_LINE
27552 && part != ON_HEADER_LINE))
27553 clear_mouse_face (hlinfo);
27555 /* Not on a window -> return. */
27556 if (!WINDOWP (window))
27557 return;
27559 /* Reset help_echo_string. It will get recomputed below. */
27560 help_echo_string = Qnil;
27562 /* Convert to window-relative pixel coordinates. */
27563 w = XWINDOW (window);
27564 frame_to_window_pixel_xy (w, &x, &y);
27566 #ifdef HAVE_WINDOW_SYSTEM
27567 /* Handle tool-bar window differently since it doesn't display a
27568 buffer. */
27569 if (EQ (window, f->tool_bar_window))
27571 note_tool_bar_highlight (f, x, y);
27572 return;
27574 #endif
27576 /* Mouse is on the mode, header line or margin? */
27577 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27578 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27580 note_mode_line_or_margin_highlight (window, x, y, part);
27581 return;
27584 #ifdef HAVE_WINDOW_SYSTEM
27585 if (part == ON_VERTICAL_BORDER)
27587 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27588 help_echo_string = build_string ("drag-mouse-1: resize");
27590 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27591 || part == ON_SCROLL_BAR)
27592 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27593 else
27594 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27595 #endif
27597 /* Are we in a window whose display is up to date?
27598 And verify the buffer's text has not changed. */
27599 b = XBUFFER (w->contents);
27600 if (part == ON_TEXT
27601 && w->window_end_valid
27602 && w->last_modified == BUF_MODIFF (b)
27603 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27605 int hpos, vpos, dx, dy, area = LAST_AREA;
27606 ptrdiff_t pos;
27607 struct glyph *glyph;
27608 Lisp_Object object;
27609 Lisp_Object mouse_face = Qnil, position;
27610 Lisp_Object *overlay_vec = NULL;
27611 ptrdiff_t i, noverlays;
27612 struct buffer *obuf;
27613 ptrdiff_t obegv, ozv;
27614 int same_region;
27616 /* Find the glyph under X/Y. */
27617 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27619 #ifdef HAVE_WINDOW_SYSTEM
27620 /* Look for :pointer property on image. */
27621 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27623 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27624 if (img != NULL && IMAGEP (img->spec))
27626 Lisp_Object image_map, hotspot;
27627 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27628 !NILP (image_map))
27629 && (hotspot = find_hot_spot (image_map,
27630 glyph->slice.img.x + dx,
27631 glyph->slice.img.y + dy),
27632 CONSP (hotspot))
27633 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27635 Lisp_Object plist;
27637 /* Could check XCAR (hotspot) to see if we enter/leave
27638 this hot-spot.
27639 If so, we could look for mouse-enter, mouse-leave
27640 properties in PLIST (and do something...). */
27641 hotspot = XCDR (hotspot);
27642 if (CONSP (hotspot)
27643 && (plist = XCAR (hotspot), CONSP (plist)))
27645 pointer = Fplist_get (plist, Qpointer);
27646 if (NILP (pointer))
27647 pointer = Qhand;
27648 help_echo_string = Fplist_get (plist, Qhelp_echo);
27649 if (!NILP (help_echo_string))
27651 help_echo_window = window;
27652 help_echo_object = glyph->object;
27653 help_echo_pos = glyph->charpos;
27657 if (NILP (pointer))
27658 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27661 #endif /* HAVE_WINDOW_SYSTEM */
27663 /* Clear mouse face if X/Y not over text. */
27664 if (glyph == NULL
27665 || area != TEXT_AREA
27666 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
27667 /* Glyph's OBJECT is an integer for glyphs inserted by the
27668 display engine for its internal purposes, like truncation
27669 and continuation glyphs and blanks beyond the end of
27670 line's text on text terminals. If we are over such a
27671 glyph, we are not over any text. */
27672 || INTEGERP (glyph->object)
27673 /* R2L rows have a stretch glyph at their front, which
27674 stands for no text, whereas L2R rows have no glyphs at
27675 all beyond the end of text. Treat such stretch glyphs
27676 like we do with NULL glyphs in L2R rows. */
27677 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27678 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
27679 && glyph->type == STRETCH_GLYPH
27680 && glyph->avoid_cursor_p))
27682 if (clear_mouse_face (hlinfo))
27683 cursor = No_Cursor;
27684 #ifdef HAVE_WINDOW_SYSTEM
27685 if (FRAME_WINDOW_P (f) && NILP (pointer))
27687 if (area != TEXT_AREA)
27688 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27689 else
27690 pointer = Vvoid_text_area_pointer;
27692 #endif
27693 goto set_cursor;
27696 pos = glyph->charpos;
27697 object = glyph->object;
27698 if (!STRINGP (object) && !BUFFERP (object))
27699 goto set_cursor;
27701 /* If we get an out-of-range value, return now; avoid an error. */
27702 if (BUFFERP (object) && pos > BUF_Z (b))
27703 goto set_cursor;
27705 /* Make the window's buffer temporarily current for
27706 overlays_at and compute_char_face. */
27707 obuf = current_buffer;
27708 current_buffer = b;
27709 obegv = BEGV;
27710 ozv = ZV;
27711 BEGV = BEG;
27712 ZV = Z;
27714 /* Is this char mouse-active or does it have help-echo? */
27715 position = make_number (pos);
27717 if (BUFFERP (object))
27719 /* Put all the overlays we want in a vector in overlay_vec. */
27720 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27721 /* Sort overlays into increasing priority order. */
27722 noverlays = sort_overlays (overlay_vec, noverlays, w);
27724 else
27725 noverlays = 0;
27727 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27729 if (same_region)
27730 cursor = No_Cursor;
27732 /* Check mouse-face highlighting. */
27733 if (! same_region
27734 /* If there exists an overlay with mouse-face overlapping
27735 the one we are currently highlighting, we have to
27736 check if we enter the overlapping overlay, and then
27737 highlight only that. */
27738 || (OVERLAYP (hlinfo->mouse_face_overlay)
27739 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27741 /* Find the highest priority overlay with a mouse-face. */
27742 Lisp_Object overlay = Qnil;
27743 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27745 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27746 if (!NILP (mouse_face))
27747 overlay = overlay_vec[i];
27750 /* If we're highlighting the same overlay as before, there's
27751 no need to do that again. */
27752 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27753 goto check_help_echo;
27754 hlinfo->mouse_face_overlay = overlay;
27756 /* Clear the display of the old active region, if any. */
27757 if (clear_mouse_face (hlinfo))
27758 cursor = No_Cursor;
27760 /* If no overlay applies, get a text property. */
27761 if (NILP (overlay))
27762 mouse_face = Fget_text_property (position, Qmouse_face, object);
27764 /* Next, compute the bounds of the mouse highlighting and
27765 display it. */
27766 if (!NILP (mouse_face) && STRINGP (object))
27768 /* The mouse-highlighting comes from a display string
27769 with a mouse-face. */
27770 Lisp_Object s, e;
27771 ptrdiff_t ignore;
27773 s = Fprevious_single_property_change
27774 (make_number (pos + 1), Qmouse_face, object, Qnil);
27775 e = Fnext_single_property_change
27776 (position, Qmouse_face, object, Qnil);
27777 if (NILP (s))
27778 s = make_number (0);
27779 if (NILP (e))
27780 e = make_number (SCHARS (object) - 1);
27781 mouse_face_from_string_pos (w, hlinfo, object,
27782 XINT (s), XINT (e));
27783 hlinfo->mouse_face_past_end = 0;
27784 hlinfo->mouse_face_window = window;
27785 hlinfo->mouse_face_face_id
27786 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27787 glyph->face_id, 1);
27788 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27789 cursor = No_Cursor;
27791 else
27793 /* The mouse-highlighting, if any, comes from an overlay
27794 or text property in the buffer. */
27795 Lisp_Object buffer IF_LINT (= Qnil);
27796 Lisp_Object disp_string IF_LINT (= Qnil);
27798 if (STRINGP (object))
27800 /* If we are on a display string with no mouse-face,
27801 check if the text under it has one. */
27802 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27803 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27804 pos = string_buffer_position (object, start);
27805 if (pos > 0)
27807 mouse_face = get_char_property_and_overlay
27808 (make_number (pos), Qmouse_face, w->contents, &overlay);
27809 buffer = w->contents;
27810 disp_string = object;
27813 else
27815 buffer = object;
27816 disp_string = Qnil;
27819 if (!NILP (mouse_face))
27821 Lisp_Object before, after;
27822 Lisp_Object before_string, after_string;
27823 /* To correctly find the limits of mouse highlight
27824 in a bidi-reordered buffer, we must not use the
27825 optimization of limiting the search in
27826 previous-single-property-change and
27827 next-single-property-change, because
27828 rows_from_pos_range needs the real start and end
27829 positions to DTRT in this case. That's because
27830 the first row visible in a window does not
27831 necessarily display the character whose position
27832 is the smallest. */
27833 Lisp_Object lim1 =
27834 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27835 ? Fmarker_position (w->start)
27836 : Qnil;
27837 Lisp_Object lim2 =
27838 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27839 ? make_number (BUF_Z (XBUFFER (buffer))
27840 - XFASTINT (w->window_end_pos))
27841 : Qnil;
27843 if (NILP (overlay))
27845 /* Handle the text property case. */
27846 before = Fprevious_single_property_change
27847 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27848 after = Fnext_single_property_change
27849 (make_number (pos), Qmouse_face, buffer, lim2);
27850 before_string = after_string = Qnil;
27852 else
27854 /* Handle the overlay case. */
27855 before = Foverlay_start (overlay);
27856 after = Foverlay_end (overlay);
27857 before_string = Foverlay_get (overlay, Qbefore_string);
27858 after_string = Foverlay_get (overlay, Qafter_string);
27860 if (!STRINGP (before_string)) before_string = Qnil;
27861 if (!STRINGP (after_string)) after_string = Qnil;
27864 mouse_face_from_buffer_pos (window, hlinfo, pos,
27865 NILP (before)
27867 : XFASTINT (before),
27868 NILP (after)
27869 ? BUF_Z (XBUFFER (buffer))
27870 : XFASTINT (after),
27871 before_string, after_string,
27872 disp_string);
27873 cursor = No_Cursor;
27878 check_help_echo:
27880 /* Look for a `help-echo' property. */
27881 if (NILP (help_echo_string)) {
27882 Lisp_Object help, overlay;
27884 /* Check overlays first. */
27885 help = overlay = Qnil;
27886 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27888 overlay = overlay_vec[i];
27889 help = Foverlay_get (overlay, Qhelp_echo);
27892 if (!NILP (help))
27894 help_echo_string = help;
27895 help_echo_window = window;
27896 help_echo_object = overlay;
27897 help_echo_pos = pos;
27899 else
27901 Lisp_Object obj = glyph->object;
27902 ptrdiff_t charpos = glyph->charpos;
27904 /* Try text properties. */
27905 if (STRINGP (obj)
27906 && charpos >= 0
27907 && charpos < SCHARS (obj))
27909 help = Fget_text_property (make_number (charpos),
27910 Qhelp_echo, obj);
27911 if (NILP (help))
27913 /* If the string itself doesn't specify a help-echo,
27914 see if the buffer text ``under'' it does. */
27915 struct glyph_row *r
27916 = MATRIX_ROW (w->current_matrix, vpos);
27917 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27918 ptrdiff_t p = string_buffer_position (obj, start);
27919 if (p > 0)
27921 help = Fget_char_property (make_number (p),
27922 Qhelp_echo, w->contents);
27923 if (!NILP (help))
27925 charpos = p;
27926 obj = w->contents;
27931 else if (BUFFERP (obj)
27932 && charpos >= BEGV
27933 && charpos < ZV)
27934 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27935 obj);
27937 if (!NILP (help))
27939 help_echo_string = help;
27940 help_echo_window = window;
27941 help_echo_object = obj;
27942 help_echo_pos = charpos;
27947 #ifdef HAVE_WINDOW_SYSTEM
27948 /* Look for a `pointer' property. */
27949 if (FRAME_WINDOW_P (f) && NILP (pointer))
27951 /* Check overlays first. */
27952 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27953 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27955 if (NILP (pointer))
27957 Lisp_Object obj = glyph->object;
27958 ptrdiff_t charpos = glyph->charpos;
27960 /* Try text properties. */
27961 if (STRINGP (obj)
27962 && charpos >= 0
27963 && charpos < SCHARS (obj))
27965 pointer = Fget_text_property (make_number (charpos),
27966 Qpointer, obj);
27967 if (NILP (pointer))
27969 /* If the string itself doesn't specify a pointer,
27970 see if the buffer text ``under'' it does. */
27971 struct glyph_row *r
27972 = MATRIX_ROW (w->current_matrix, vpos);
27973 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27974 ptrdiff_t p = string_buffer_position (obj, start);
27975 if (p > 0)
27976 pointer = Fget_char_property (make_number (p),
27977 Qpointer, w->contents);
27980 else if (BUFFERP (obj)
27981 && charpos >= BEGV
27982 && charpos < ZV)
27983 pointer = Fget_text_property (make_number (charpos),
27984 Qpointer, obj);
27987 #endif /* HAVE_WINDOW_SYSTEM */
27989 BEGV = obegv;
27990 ZV = ozv;
27991 current_buffer = obuf;
27994 set_cursor:
27996 #ifdef HAVE_WINDOW_SYSTEM
27997 if (FRAME_WINDOW_P (f))
27998 define_frame_cursor1 (f, cursor, pointer);
27999 #else
28000 /* This is here to prevent a compiler error, about "label at end of
28001 compound statement". */
28002 return;
28003 #endif
28007 /* EXPORT for RIF:
28008 Clear any mouse-face on window W. This function is part of the
28009 redisplay interface, and is called from try_window_id and similar
28010 functions to ensure the mouse-highlight is off. */
28012 void
28013 x_clear_window_mouse_face (struct window *w)
28015 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28016 Lisp_Object window;
28018 block_input ();
28019 XSETWINDOW (window, w);
28020 if (EQ (window, hlinfo->mouse_face_window))
28021 clear_mouse_face (hlinfo);
28022 unblock_input ();
28026 /* EXPORT:
28027 Just discard the mouse face information for frame F, if any.
28028 This is used when the size of F is changed. */
28030 void
28031 cancel_mouse_face (struct frame *f)
28033 Lisp_Object window;
28034 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28036 window = hlinfo->mouse_face_window;
28037 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28039 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28040 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28041 hlinfo->mouse_face_window = Qnil;
28047 /***********************************************************************
28048 Exposure Events
28049 ***********************************************************************/
28051 #ifdef HAVE_WINDOW_SYSTEM
28053 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28054 which intersects rectangle R. R is in window-relative coordinates. */
28056 static void
28057 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28058 enum glyph_row_area area)
28060 struct glyph *first = row->glyphs[area];
28061 struct glyph *end = row->glyphs[area] + row->used[area];
28062 struct glyph *last;
28063 int first_x, start_x, x;
28065 if (area == TEXT_AREA && row->fill_line_p)
28066 /* If row extends face to end of line write the whole line. */
28067 draw_glyphs (w, 0, row, area,
28068 0, row->used[area],
28069 DRAW_NORMAL_TEXT, 0);
28070 else
28072 /* Set START_X to the window-relative start position for drawing glyphs of
28073 AREA. The first glyph of the text area can be partially visible.
28074 The first glyphs of other areas cannot. */
28075 start_x = window_box_left_offset (w, area);
28076 x = start_x;
28077 if (area == TEXT_AREA)
28078 x += row->x;
28080 /* Find the first glyph that must be redrawn. */
28081 while (first < end
28082 && x + first->pixel_width < r->x)
28084 x += first->pixel_width;
28085 ++first;
28088 /* Find the last one. */
28089 last = first;
28090 first_x = x;
28091 while (last < end
28092 && x < r->x + r->width)
28094 x += last->pixel_width;
28095 ++last;
28098 /* Repaint. */
28099 if (last > first)
28100 draw_glyphs (w, first_x - start_x, row, area,
28101 first - row->glyphs[area], last - row->glyphs[area],
28102 DRAW_NORMAL_TEXT, 0);
28107 /* Redraw the parts of the glyph row ROW on window W intersecting
28108 rectangle R. R is in window-relative coordinates. Value is
28109 non-zero if mouse-face was overwritten. */
28111 static int
28112 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28114 eassert (row->enabled_p);
28116 if (row->mode_line_p || w->pseudo_window_p)
28117 draw_glyphs (w, 0, row, TEXT_AREA,
28118 0, row->used[TEXT_AREA],
28119 DRAW_NORMAL_TEXT, 0);
28120 else
28122 if (row->used[LEFT_MARGIN_AREA])
28123 expose_area (w, row, r, LEFT_MARGIN_AREA);
28124 if (row->used[TEXT_AREA])
28125 expose_area (w, row, r, TEXT_AREA);
28126 if (row->used[RIGHT_MARGIN_AREA])
28127 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28128 draw_row_fringe_bitmaps (w, row);
28131 return row->mouse_face_p;
28135 /* Redraw those parts of glyphs rows during expose event handling that
28136 overlap other rows. Redrawing of an exposed line writes over parts
28137 of lines overlapping that exposed line; this function fixes that.
28139 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28140 row in W's current matrix that is exposed and overlaps other rows.
28141 LAST_OVERLAPPING_ROW is the last such row. */
28143 static void
28144 expose_overlaps (struct window *w,
28145 struct glyph_row *first_overlapping_row,
28146 struct glyph_row *last_overlapping_row,
28147 XRectangle *r)
28149 struct glyph_row *row;
28151 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28152 if (row->overlapping_p)
28154 eassert (row->enabled_p && !row->mode_line_p);
28156 row->clip = r;
28157 if (row->used[LEFT_MARGIN_AREA])
28158 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28160 if (row->used[TEXT_AREA])
28161 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28163 if (row->used[RIGHT_MARGIN_AREA])
28164 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28165 row->clip = NULL;
28170 /* Return non-zero if W's cursor intersects rectangle R. */
28172 static int
28173 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28175 XRectangle cr, result;
28176 struct glyph *cursor_glyph;
28177 struct glyph_row *row;
28179 if (w->phys_cursor.vpos >= 0
28180 && w->phys_cursor.vpos < w->current_matrix->nrows
28181 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28182 row->enabled_p)
28183 && row->cursor_in_fringe_p)
28185 /* Cursor is in the fringe. */
28186 cr.x = window_box_right_offset (w,
28187 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28188 ? RIGHT_MARGIN_AREA
28189 : TEXT_AREA));
28190 cr.y = row->y;
28191 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28192 cr.height = row->height;
28193 return x_intersect_rectangles (&cr, r, &result);
28196 cursor_glyph = get_phys_cursor_glyph (w);
28197 if (cursor_glyph)
28199 /* r is relative to W's box, but w->phys_cursor.x is relative
28200 to left edge of W's TEXT area. Adjust it. */
28201 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28202 cr.y = w->phys_cursor.y;
28203 cr.width = cursor_glyph->pixel_width;
28204 cr.height = w->phys_cursor_height;
28205 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28206 I assume the effect is the same -- and this is portable. */
28207 return x_intersect_rectangles (&cr, r, &result);
28209 /* If we don't understand the format, pretend we're not in the hot-spot. */
28210 return 0;
28214 /* EXPORT:
28215 Draw a vertical window border to the right of window W if W doesn't
28216 have vertical scroll bars. */
28218 void
28219 x_draw_vertical_border (struct window *w)
28221 struct frame *f = XFRAME (WINDOW_FRAME (w));
28223 /* We could do better, if we knew what type of scroll-bar the adjacent
28224 windows (on either side) have... But we don't :-(
28225 However, I think this works ok. ++KFS 2003-04-25 */
28227 /* Redraw borders between horizontally adjacent windows. Don't
28228 do it for frames with vertical scroll bars because either the
28229 right scroll bar of a window, or the left scroll bar of its
28230 neighbor will suffice as a border. */
28231 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28232 return;
28234 /* Note: It is necessary to redraw both the left and the right
28235 borders, for when only this single window W is being
28236 redisplayed. */
28237 if (!WINDOW_RIGHTMOST_P (w)
28238 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28240 int x0, x1, y0, y1;
28242 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28243 y1 -= 1;
28245 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28246 x1 -= 1;
28248 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28250 if (!WINDOW_LEFTMOST_P (w)
28251 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28253 int x0, x1, y0, y1;
28255 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28256 y1 -= 1;
28258 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28259 x0 -= 1;
28261 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28266 /* Redraw the part of window W intersection rectangle FR. Pixel
28267 coordinates in FR are frame-relative. Call this function with
28268 input blocked. Value is non-zero if the exposure overwrites
28269 mouse-face. */
28271 static int
28272 expose_window (struct window *w, XRectangle *fr)
28274 struct frame *f = XFRAME (w->frame);
28275 XRectangle wr, r;
28276 int mouse_face_overwritten_p = 0;
28278 /* If window is not yet fully initialized, do nothing. This can
28279 happen when toolkit scroll bars are used and a window is split.
28280 Reconfiguring the scroll bar will generate an expose for a newly
28281 created window. */
28282 if (w->current_matrix == NULL)
28283 return 0;
28285 /* When we're currently updating the window, display and current
28286 matrix usually don't agree. Arrange for a thorough display
28287 later. */
28288 if (w == updated_window)
28290 SET_FRAME_GARBAGED (f);
28291 return 0;
28294 /* Frame-relative pixel rectangle of W. */
28295 wr.x = WINDOW_LEFT_EDGE_X (w);
28296 wr.y = WINDOW_TOP_EDGE_Y (w);
28297 wr.width = WINDOW_TOTAL_WIDTH (w);
28298 wr.height = WINDOW_TOTAL_HEIGHT (w);
28300 if (x_intersect_rectangles (fr, &wr, &r))
28302 int yb = window_text_bottom_y (w);
28303 struct glyph_row *row;
28304 int cursor_cleared_p, phys_cursor_on_p;
28305 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28307 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28308 r.x, r.y, r.width, r.height));
28310 /* Convert to window coordinates. */
28311 r.x -= WINDOW_LEFT_EDGE_X (w);
28312 r.y -= WINDOW_TOP_EDGE_Y (w);
28314 /* Turn off the cursor. */
28315 if (!w->pseudo_window_p
28316 && phys_cursor_in_rect_p (w, &r))
28318 x_clear_cursor (w);
28319 cursor_cleared_p = 1;
28321 else
28322 cursor_cleared_p = 0;
28324 /* If the row containing the cursor extends face to end of line,
28325 then expose_area might overwrite the cursor outside the
28326 rectangle and thus notice_overwritten_cursor might clear
28327 w->phys_cursor_on_p. We remember the original value and
28328 check later if it is changed. */
28329 phys_cursor_on_p = w->phys_cursor_on_p;
28331 /* Update lines intersecting rectangle R. */
28332 first_overlapping_row = last_overlapping_row = NULL;
28333 for (row = w->current_matrix->rows;
28334 row->enabled_p;
28335 ++row)
28337 int y0 = row->y;
28338 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28340 if ((y0 >= r.y && y0 < r.y + r.height)
28341 || (y1 > r.y && y1 < r.y + r.height)
28342 || (r.y >= y0 && r.y < y1)
28343 || (r.y + r.height > y0 && r.y + r.height < y1))
28345 /* A header line may be overlapping, but there is no need
28346 to fix overlapping areas for them. KFS 2005-02-12 */
28347 if (row->overlapping_p && !row->mode_line_p)
28349 if (first_overlapping_row == NULL)
28350 first_overlapping_row = row;
28351 last_overlapping_row = row;
28354 row->clip = fr;
28355 if (expose_line (w, row, &r))
28356 mouse_face_overwritten_p = 1;
28357 row->clip = NULL;
28359 else if (row->overlapping_p)
28361 /* We must redraw a row overlapping the exposed area. */
28362 if (y0 < r.y
28363 ? y0 + row->phys_height > r.y
28364 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28366 if (first_overlapping_row == NULL)
28367 first_overlapping_row = row;
28368 last_overlapping_row = row;
28372 if (y1 >= yb)
28373 break;
28376 /* Display the mode line if there is one. */
28377 if (WINDOW_WANTS_MODELINE_P (w)
28378 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28379 row->enabled_p)
28380 && row->y < r.y + r.height)
28382 if (expose_line (w, row, &r))
28383 mouse_face_overwritten_p = 1;
28386 if (!w->pseudo_window_p)
28388 /* Fix the display of overlapping rows. */
28389 if (first_overlapping_row)
28390 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28391 fr);
28393 /* Draw border between windows. */
28394 x_draw_vertical_border (w);
28396 /* Turn the cursor on again. */
28397 if (cursor_cleared_p
28398 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28399 update_window_cursor (w, 1);
28403 return mouse_face_overwritten_p;
28408 /* Redraw (parts) of all windows in the window tree rooted at W that
28409 intersect R. R contains frame pixel coordinates. Value is
28410 non-zero if the exposure overwrites mouse-face. */
28412 static int
28413 expose_window_tree (struct window *w, XRectangle *r)
28415 struct frame *f = XFRAME (w->frame);
28416 int mouse_face_overwritten_p = 0;
28418 while (w && !FRAME_GARBAGED_P (f))
28420 if (WINDOWP (w->contents))
28421 mouse_face_overwritten_p
28422 |= expose_window_tree (XWINDOW (w->contents), r);
28423 else
28424 mouse_face_overwritten_p |= expose_window (w, r);
28426 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28429 return mouse_face_overwritten_p;
28433 /* EXPORT:
28434 Redisplay an exposed area of frame F. X and Y are the upper-left
28435 corner of the exposed rectangle. W and H are width and height of
28436 the exposed area. All are pixel values. W or H zero means redraw
28437 the entire frame. */
28439 void
28440 expose_frame (struct frame *f, int x, int y, int w, int h)
28442 XRectangle r;
28443 int mouse_face_overwritten_p = 0;
28445 TRACE ((stderr, "expose_frame "));
28447 /* No need to redraw if frame will be redrawn soon. */
28448 if (FRAME_GARBAGED_P (f))
28450 TRACE ((stderr, " garbaged\n"));
28451 return;
28454 /* If basic faces haven't been realized yet, there is no point in
28455 trying to redraw anything. This can happen when we get an expose
28456 event while Emacs is starting, e.g. by moving another window. */
28457 if (FRAME_FACE_CACHE (f) == NULL
28458 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28460 TRACE ((stderr, " no faces\n"));
28461 return;
28464 if (w == 0 || h == 0)
28466 r.x = r.y = 0;
28467 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28468 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28470 else
28472 r.x = x;
28473 r.y = y;
28474 r.width = w;
28475 r.height = h;
28478 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28479 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28481 if (WINDOWP (f->tool_bar_window))
28482 mouse_face_overwritten_p
28483 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28485 #ifdef HAVE_X_WINDOWS
28486 #ifndef MSDOS
28487 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28488 if (WINDOWP (f->menu_bar_window))
28489 mouse_face_overwritten_p
28490 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28491 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28492 #endif
28493 #endif
28495 /* Some window managers support a focus-follows-mouse style with
28496 delayed raising of frames. Imagine a partially obscured frame,
28497 and moving the mouse into partially obscured mouse-face on that
28498 frame. The visible part of the mouse-face will be highlighted,
28499 then the WM raises the obscured frame. With at least one WM, KDE
28500 2.1, Emacs is not getting any event for the raising of the frame
28501 (even tried with SubstructureRedirectMask), only Expose events.
28502 These expose events will draw text normally, i.e. not
28503 highlighted. Which means we must redo the highlight here.
28504 Subsume it under ``we love X''. --gerd 2001-08-15 */
28505 /* Included in Windows version because Windows most likely does not
28506 do the right thing if any third party tool offers
28507 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28508 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28510 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28511 if (f == hlinfo->mouse_face_mouse_frame)
28513 int mouse_x = hlinfo->mouse_face_mouse_x;
28514 int mouse_y = hlinfo->mouse_face_mouse_y;
28515 clear_mouse_face (hlinfo);
28516 note_mouse_highlight (f, mouse_x, mouse_y);
28522 /* EXPORT:
28523 Determine the intersection of two rectangles R1 and R2. Return
28524 the intersection in *RESULT. Value is non-zero if RESULT is not
28525 empty. */
28528 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28530 XRectangle *left, *right;
28531 XRectangle *upper, *lower;
28532 int intersection_p = 0;
28534 /* Rearrange so that R1 is the left-most rectangle. */
28535 if (r1->x < r2->x)
28536 left = r1, right = r2;
28537 else
28538 left = r2, right = r1;
28540 /* X0 of the intersection is right.x0, if this is inside R1,
28541 otherwise there is no intersection. */
28542 if (right->x <= left->x + left->width)
28544 result->x = right->x;
28546 /* The right end of the intersection is the minimum of
28547 the right ends of left and right. */
28548 result->width = (min (left->x + left->width, right->x + right->width)
28549 - result->x);
28551 /* Same game for Y. */
28552 if (r1->y < r2->y)
28553 upper = r1, lower = r2;
28554 else
28555 upper = r2, lower = r1;
28557 /* The upper end of the intersection is lower.y0, if this is inside
28558 of upper. Otherwise, there is no intersection. */
28559 if (lower->y <= upper->y + upper->height)
28561 result->y = lower->y;
28563 /* The lower end of the intersection is the minimum of the lower
28564 ends of upper and lower. */
28565 result->height = (min (lower->y + lower->height,
28566 upper->y + upper->height)
28567 - result->y);
28568 intersection_p = 1;
28572 return intersection_p;
28575 #endif /* HAVE_WINDOW_SYSTEM */
28578 /***********************************************************************
28579 Initialization
28580 ***********************************************************************/
28582 void
28583 syms_of_xdisp (void)
28585 Vwith_echo_area_save_vector = Qnil;
28586 staticpro (&Vwith_echo_area_save_vector);
28588 Vmessage_stack = Qnil;
28589 staticpro (&Vmessage_stack);
28591 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28592 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28594 message_dolog_marker1 = Fmake_marker ();
28595 staticpro (&message_dolog_marker1);
28596 message_dolog_marker2 = Fmake_marker ();
28597 staticpro (&message_dolog_marker2);
28598 message_dolog_marker3 = Fmake_marker ();
28599 staticpro (&message_dolog_marker3);
28601 #ifdef GLYPH_DEBUG
28602 defsubr (&Sdump_frame_glyph_matrix);
28603 defsubr (&Sdump_glyph_matrix);
28604 defsubr (&Sdump_glyph_row);
28605 defsubr (&Sdump_tool_bar_row);
28606 defsubr (&Strace_redisplay);
28607 defsubr (&Strace_to_stderr);
28608 #endif
28609 #ifdef HAVE_WINDOW_SYSTEM
28610 defsubr (&Stool_bar_lines_needed);
28611 defsubr (&Slookup_image_map);
28612 #endif
28613 defsubr (&Sformat_mode_line);
28614 defsubr (&Sinvisible_p);
28615 defsubr (&Scurrent_bidi_paragraph_direction);
28617 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28618 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28619 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28620 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28621 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28622 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28623 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28624 DEFSYM (Qeval, "eval");
28625 DEFSYM (QCdata, ":data");
28626 DEFSYM (Qdisplay, "display");
28627 DEFSYM (Qspace_width, "space-width");
28628 DEFSYM (Qraise, "raise");
28629 DEFSYM (Qslice, "slice");
28630 DEFSYM (Qspace, "space");
28631 DEFSYM (Qmargin, "margin");
28632 DEFSYM (Qpointer, "pointer");
28633 DEFSYM (Qleft_margin, "left-margin");
28634 DEFSYM (Qright_margin, "right-margin");
28635 DEFSYM (Qcenter, "center");
28636 DEFSYM (Qline_height, "line-height");
28637 DEFSYM (QCalign_to, ":align-to");
28638 DEFSYM (QCrelative_width, ":relative-width");
28639 DEFSYM (QCrelative_height, ":relative-height");
28640 DEFSYM (QCeval, ":eval");
28641 DEFSYM (QCpropertize, ":propertize");
28642 DEFSYM (QCfile, ":file");
28643 DEFSYM (Qfontified, "fontified");
28644 DEFSYM (Qfontification_functions, "fontification-functions");
28645 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28646 DEFSYM (Qescape_glyph, "escape-glyph");
28647 DEFSYM (Qnobreak_space, "nobreak-space");
28648 DEFSYM (Qimage, "image");
28649 DEFSYM (Qtext, "text");
28650 DEFSYM (Qboth, "both");
28651 DEFSYM (Qboth_horiz, "both-horiz");
28652 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28653 DEFSYM (QCmap, ":map");
28654 DEFSYM (QCpointer, ":pointer");
28655 DEFSYM (Qrect, "rect");
28656 DEFSYM (Qcircle, "circle");
28657 DEFSYM (Qpoly, "poly");
28658 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28659 DEFSYM (Qgrow_only, "grow-only");
28660 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28661 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28662 DEFSYM (Qposition, "position");
28663 DEFSYM (Qbuffer_position, "buffer-position");
28664 DEFSYM (Qobject, "object");
28665 DEFSYM (Qbar, "bar");
28666 DEFSYM (Qhbar, "hbar");
28667 DEFSYM (Qbox, "box");
28668 DEFSYM (Qhollow, "hollow");
28669 DEFSYM (Qhand, "hand");
28670 DEFSYM (Qarrow, "arrow");
28671 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28673 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28674 Fcons (intern_c_string ("void-variable"), Qnil)),
28675 Qnil);
28676 staticpro (&list_of_error);
28678 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28679 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28680 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28681 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28683 echo_buffer[0] = echo_buffer[1] = Qnil;
28684 staticpro (&echo_buffer[0]);
28685 staticpro (&echo_buffer[1]);
28687 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28688 staticpro (&echo_area_buffer[0]);
28689 staticpro (&echo_area_buffer[1]);
28691 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28692 staticpro (&Vmessages_buffer_name);
28694 mode_line_proptrans_alist = Qnil;
28695 staticpro (&mode_line_proptrans_alist);
28696 mode_line_string_list = Qnil;
28697 staticpro (&mode_line_string_list);
28698 mode_line_string_face = Qnil;
28699 staticpro (&mode_line_string_face);
28700 mode_line_string_face_prop = Qnil;
28701 staticpro (&mode_line_string_face_prop);
28702 Vmode_line_unwind_vector = Qnil;
28703 staticpro (&Vmode_line_unwind_vector);
28705 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28707 help_echo_string = Qnil;
28708 staticpro (&help_echo_string);
28709 help_echo_object = Qnil;
28710 staticpro (&help_echo_object);
28711 help_echo_window = Qnil;
28712 staticpro (&help_echo_window);
28713 previous_help_echo_string = Qnil;
28714 staticpro (&previous_help_echo_string);
28715 help_echo_pos = -1;
28717 DEFSYM (Qright_to_left, "right-to-left");
28718 DEFSYM (Qleft_to_right, "left-to-right");
28720 #ifdef HAVE_WINDOW_SYSTEM
28721 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28722 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28723 For example, if a block cursor is over a tab, it will be drawn as
28724 wide as that tab on the display. */);
28725 x_stretch_cursor_p = 0;
28726 #endif
28728 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28729 doc: /* Non-nil means highlight trailing whitespace.
28730 The face used for trailing whitespace is `trailing-whitespace'. */);
28731 Vshow_trailing_whitespace = Qnil;
28733 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28734 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28735 If the value is t, Emacs highlights non-ASCII chars which have the
28736 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28737 or `escape-glyph' face respectively.
28739 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28740 U+2011 (non-breaking hyphen) are affected.
28742 Any other non-nil value means to display these characters as a escape
28743 glyph followed by an ordinary space or hyphen.
28745 A value of nil means no special handling of these characters. */);
28746 Vnobreak_char_display = Qt;
28748 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28749 doc: /* The pointer shape to show in void text areas.
28750 A value of nil means to show the text pointer. Other options are `arrow',
28751 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28752 Vvoid_text_area_pointer = Qarrow;
28754 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28755 doc: /* Non-nil means don't actually do any redisplay.
28756 This is used for internal purposes. */);
28757 Vinhibit_redisplay = Qnil;
28759 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28760 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28761 Vglobal_mode_string = Qnil;
28763 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28764 doc: /* Marker for where to display an arrow on top of the buffer text.
28765 This must be the beginning of a line in order to work.
28766 See also `overlay-arrow-string'. */);
28767 Voverlay_arrow_position = Qnil;
28769 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28770 doc: /* String to display as an arrow in non-window frames.
28771 See also `overlay-arrow-position'. */);
28772 Voverlay_arrow_string = build_pure_c_string ("=>");
28774 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28775 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28776 The symbols on this list are examined during redisplay to determine
28777 where to display overlay arrows. */);
28778 Voverlay_arrow_variable_list
28779 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28781 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28782 doc: /* The number of lines to try scrolling a window by when point moves out.
28783 If that fails to bring point back on frame, point is centered instead.
28784 If this is zero, point is always centered after it moves off frame.
28785 If you want scrolling to always be a line at a time, you should set
28786 `scroll-conservatively' to a large value rather than set this to 1. */);
28788 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28789 doc: /* Scroll up to this many lines, to bring point back on screen.
28790 If point moves off-screen, redisplay will scroll by up to
28791 `scroll-conservatively' lines in order to bring point just barely
28792 onto the screen again. If that cannot be done, then redisplay
28793 recenters point as usual.
28795 If the value is greater than 100, redisplay will never recenter point,
28796 but will always scroll just enough text to bring point into view, even
28797 if you move far away.
28799 A value of zero means always recenter point if it moves off screen. */);
28800 scroll_conservatively = 0;
28802 DEFVAR_INT ("scroll-margin", scroll_margin,
28803 doc: /* Number of lines of margin at the top and bottom of a window.
28804 Recenter the window whenever point gets within this many lines
28805 of the top or bottom of the window. */);
28806 scroll_margin = 0;
28808 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28809 doc: /* Pixels per inch value for non-window system displays.
28810 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28811 Vdisplay_pixels_per_inch = make_float (72.0);
28813 #ifdef GLYPH_DEBUG
28814 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28815 #endif
28817 DEFVAR_LISP ("truncate-partial-width-windows",
28818 Vtruncate_partial_width_windows,
28819 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28820 For an integer value, truncate lines in each window narrower than the
28821 full frame width, provided the window width is less than that integer;
28822 otherwise, respect the value of `truncate-lines'.
28824 For any other non-nil value, truncate lines in all windows that do
28825 not span the full frame width.
28827 A value of nil means to respect the value of `truncate-lines'.
28829 If `word-wrap' is enabled, you might want to reduce this. */);
28830 Vtruncate_partial_width_windows = make_number (50);
28832 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28833 doc: /* Maximum buffer size for which line number should be displayed.
28834 If the buffer is bigger than this, the line number does not appear
28835 in the mode line. A value of nil means no limit. */);
28836 Vline_number_display_limit = Qnil;
28838 DEFVAR_INT ("line-number-display-limit-width",
28839 line_number_display_limit_width,
28840 doc: /* Maximum line width (in characters) for line number display.
28841 If the average length of the lines near point is bigger than this, then the
28842 line number may be omitted from the mode line. */);
28843 line_number_display_limit_width = 200;
28845 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28846 doc: /* Non-nil means highlight region even in nonselected windows. */);
28847 highlight_nonselected_windows = 0;
28849 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28850 doc: /* Non-nil if more than one frame is visible on this display.
28851 Minibuffer-only frames don't count, but iconified frames do.
28852 This variable is not guaranteed to be accurate except while processing
28853 `frame-title-format' and `icon-title-format'. */);
28855 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28856 doc: /* Template for displaying the title bar of visible frames.
28857 \(Assuming the window manager supports this feature.)
28859 This variable has the same structure as `mode-line-format', except that
28860 the %c and %l constructs are ignored. It is used only on frames for
28861 which no explicit name has been set \(see `modify-frame-parameters'). */);
28863 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28864 doc: /* Template for displaying the title bar of an iconified frame.
28865 \(Assuming the window manager supports this feature.)
28866 This variable has the same structure as `mode-line-format' (which see),
28867 and is used only on frames for which no explicit name has been set
28868 \(see `modify-frame-parameters'). */);
28869 Vicon_title_format
28870 = Vframe_title_format
28871 = listn (CONSTYPE_PURE, 3,
28872 intern_c_string ("multiple-frames"),
28873 build_pure_c_string ("%b"),
28874 listn (CONSTYPE_PURE, 4,
28875 empty_unibyte_string,
28876 intern_c_string ("invocation-name"),
28877 build_pure_c_string ("@"),
28878 intern_c_string ("system-name")));
28880 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28881 doc: /* Maximum number of lines to keep in the message log buffer.
28882 If nil, disable message logging. If t, log messages but don't truncate
28883 the buffer when it becomes large. */);
28884 Vmessage_log_max = make_number (1000);
28886 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28887 doc: /* Functions called before redisplay, if window sizes have changed.
28888 The value should be a list of functions that take one argument.
28889 Just before redisplay, for each frame, if any of its windows have changed
28890 size since the last redisplay, or have been split or deleted,
28891 all the functions in the list are called, with the frame as argument. */);
28892 Vwindow_size_change_functions = Qnil;
28894 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28895 doc: /* List of functions to call before redisplaying a window with scrolling.
28896 Each function is called with two arguments, the window and its new
28897 display-start position. Note that these functions are also called by
28898 `set-window-buffer'. Also note that the value of `window-end' is not
28899 valid when these functions are called.
28901 Warning: Do not use this feature to alter the way the window
28902 is scrolled. It is not designed for that, and such use probably won't
28903 work. */);
28904 Vwindow_scroll_functions = Qnil;
28906 DEFVAR_LISP ("window-text-change-functions",
28907 Vwindow_text_change_functions,
28908 doc: /* Functions to call in redisplay when text in the window might change. */);
28909 Vwindow_text_change_functions = Qnil;
28911 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28912 doc: /* Functions called when redisplay of a window reaches the end trigger.
28913 Each function is called with two arguments, the window and the end trigger value.
28914 See `set-window-redisplay-end-trigger'. */);
28915 Vredisplay_end_trigger_functions = Qnil;
28917 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28918 doc: /* Non-nil means autoselect window with mouse pointer.
28919 If nil, do not autoselect windows.
28920 A positive number means delay autoselection by that many seconds: a
28921 window is autoselected only after the mouse has remained in that
28922 window for the duration of the delay.
28923 A negative number has a similar effect, but causes windows to be
28924 autoselected only after the mouse has stopped moving. \(Because of
28925 the way Emacs compares mouse events, you will occasionally wait twice
28926 that time before the window gets selected.\)
28927 Any other value means to autoselect window instantaneously when the
28928 mouse pointer enters it.
28930 Autoselection selects the minibuffer only if it is active, and never
28931 unselects the minibuffer if it is active.
28933 When customizing this variable make sure that the actual value of
28934 `focus-follows-mouse' matches the behavior of your window manager. */);
28935 Vmouse_autoselect_window = Qnil;
28937 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28938 doc: /* Non-nil means automatically resize tool-bars.
28939 This dynamically changes the tool-bar's height to the minimum height
28940 that is needed to make all tool-bar items visible.
28941 If value is `grow-only', the tool-bar's height is only increased
28942 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28943 Vauto_resize_tool_bars = Qt;
28945 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28946 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28947 auto_raise_tool_bar_buttons_p = 1;
28949 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28950 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28951 make_cursor_line_fully_visible_p = 1;
28953 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28954 doc: /* Border below tool-bar in pixels.
28955 If an integer, use it as the height of the border.
28956 If it is one of `internal-border-width' or `border-width', use the
28957 value of the corresponding frame parameter.
28958 Otherwise, no border is added below the tool-bar. */);
28959 Vtool_bar_border = Qinternal_border_width;
28961 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28962 doc: /* Margin around tool-bar buttons in pixels.
28963 If an integer, use that for both horizontal and vertical margins.
28964 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28965 HORZ specifying the horizontal margin, and VERT specifying the
28966 vertical margin. */);
28967 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28969 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28970 doc: /* Relief thickness of tool-bar buttons. */);
28971 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28973 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28974 doc: /* Tool bar style to use.
28975 It can be one of
28976 image - show images only
28977 text - show text only
28978 both - show both, text below image
28979 both-horiz - show text to the right of the image
28980 text-image-horiz - show text to the left of the image
28981 any other - use system default or image if no system default.
28983 This variable only affects the GTK+ toolkit version of Emacs. */);
28984 Vtool_bar_style = Qnil;
28986 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28987 doc: /* Maximum number of characters a label can have to be shown.
28988 The tool bar style must also show labels for this to have any effect, see
28989 `tool-bar-style'. */);
28990 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28992 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28993 doc: /* List of functions to call to fontify regions of text.
28994 Each function is called with one argument POS. Functions must
28995 fontify a region starting at POS in the current buffer, and give
28996 fontified regions the property `fontified'. */);
28997 Vfontification_functions = Qnil;
28998 Fmake_variable_buffer_local (Qfontification_functions);
29000 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29001 unibyte_display_via_language_environment,
29002 doc: /* Non-nil means display unibyte text according to language environment.
29003 Specifically, this means that raw bytes in the range 160-255 decimal
29004 are displayed by converting them to the equivalent multibyte characters
29005 according to the current language environment. As a result, they are
29006 displayed according to the current fontset.
29008 Note that this variable affects only how these bytes are displayed,
29009 but does not change the fact they are interpreted as raw bytes. */);
29010 unibyte_display_via_language_environment = 0;
29012 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29013 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29014 If a float, it specifies a fraction of the mini-window frame's height.
29015 If an integer, it specifies a number of lines. */);
29016 Vmax_mini_window_height = make_float (0.25);
29018 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29019 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29020 A value of nil means don't automatically resize mini-windows.
29021 A value of t means resize them to fit the text displayed in them.
29022 A value of `grow-only', the default, means let mini-windows grow only;
29023 they return to their normal size when the minibuffer is closed, or the
29024 echo area becomes empty. */);
29025 Vresize_mini_windows = Qgrow_only;
29027 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29028 doc: /* Alist specifying how to blink the cursor off.
29029 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29030 `cursor-type' frame-parameter or variable equals ON-STATE,
29031 comparing using `equal', Emacs uses OFF-STATE to specify
29032 how to blink it off. ON-STATE and OFF-STATE are values for
29033 the `cursor-type' frame parameter.
29035 If a frame's ON-STATE has no entry in this list,
29036 the frame's other specifications determine how to blink the cursor off. */);
29037 Vblink_cursor_alist = Qnil;
29039 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29040 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29041 If non-nil, windows are automatically scrolled horizontally to make
29042 point visible. */);
29043 automatic_hscrolling_p = 1;
29044 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29046 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29047 doc: /* How many columns away from the window edge point is allowed to get
29048 before automatic hscrolling will horizontally scroll the window. */);
29049 hscroll_margin = 5;
29051 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29052 doc: /* How many columns to scroll the window when point gets too close to the edge.
29053 When point is less than `hscroll-margin' columns from the window
29054 edge, automatic hscrolling will scroll the window by the amount of columns
29055 determined by this variable. If its value is a positive integer, scroll that
29056 many columns. If it's a positive floating-point number, it specifies the
29057 fraction of the window's width to scroll. If it's nil or zero, point will be
29058 centered horizontally after the scroll. Any other value, including negative
29059 numbers, are treated as if the value were zero.
29061 Automatic hscrolling always moves point outside the scroll margin, so if
29062 point was more than scroll step columns inside the margin, the window will
29063 scroll more than the value given by the scroll step.
29065 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29066 and `scroll-right' overrides this variable's effect. */);
29067 Vhscroll_step = make_number (0);
29069 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29070 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29071 Bind this around calls to `message' to let it take effect. */);
29072 message_truncate_lines = 0;
29074 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29075 doc: /* Normal hook run to update the menu bar definitions.
29076 Redisplay runs this hook before it redisplays the menu bar.
29077 This is used to update submenus such as Buffers,
29078 whose contents depend on various data. */);
29079 Vmenu_bar_update_hook = Qnil;
29081 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29082 doc: /* Frame for which we are updating a menu.
29083 The enable predicate for a menu binding should check this variable. */);
29084 Vmenu_updating_frame = Qnil;
29086 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29087 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29088 inhibit_menubar_update = 0;
29090 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29091 doc: /* Prefix prepended to all continuation lines at display time.
29092 The value may be a string, an image, or a stretch-glyph; it is
29093 interpreted in the same way as the value of a `display' text property.
29095 This variable is overridden by any `wrap-prefix' text or overlay
29096 property.
29098 To add a prefix to non-continuation lines, use `line-prefix'. */);
29099 Vwrap_prefix = Qnil;
29100 DEFSYM (Qwrap_prefix, "wrap-prefix");
29101 Fmake_variable_buffer_local (Qwrap_prefix);
29103 DEFVAR_LISP ("line-prefix", Vline_prefix,
29104 doc: /* Prefix prepended to all non-continuation lines at display time.
29105 The value may be a string, an image, or a stretch-glyph; it is
29106 interpreted in the same way as the value of a `display' text property.
29108 This variable is overridden by any `line-prefix' text or overlay
29109 property.
29111 To add a prefix to continuation lines, use `wrap-prefix'. */);
29112 Vline_prefix = Qnil;
29113 DEFSYM (Qline_prefix, "line-prefix");
29114 Fmake_variable_buffer_local (Qline_prefix);
29116 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29117 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29118 inhibit_eval_during_redisplay = 0;
29120 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29121 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29122 inhibit_free_realized_faces = 0;
29124 #ifdef GLYPH_DEBUG
29125 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29126 doc: /* Inhibit try_window_id display optimization. */);
29127 inhibit_try_window_id = 0;
29129 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29130 doc: /* Inhibit try_window_reusing display optimization. */);
29131 inhibit_try_window_reusing = 0;
29133 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29134 doc: /* Inhibit try_cursor_movement display optimization. */);
29135 inhibit_try_cursor_movement = 0;
29136 #endif /* GLYPH_DEBUG */
29138 DEFVAR_INT ("overline-margin", overline_margin,
29139 doc: /* Space between overline and text, in pixels.
29140 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29141 margin to the character height. */);
29142 overline_margin = 2;
29144 DEFVAR_INT ("underline-minimum-offset",
29145 underline_minimum_offset,
29146 doc: /* Minimum distance between baseline and underline.
29147 This can improve legibility of underlined text at small font sizes,
29148 particularly when using variable `x-use-underline-position-properties'
29149 with fonts that specify an UNDERLINE_POSITION relatively close to the
29150 baseline. The default value is 1. */);
29151 underline_minimum_offset = 1;
29153 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29154 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29155 This feature only works when on a window system that can change
29156 cursor shapes. */);
29157 display_hourglass_p = 1;
29159 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29160 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29161 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29163 hourglass_atimer = NULL;
29164 hourglass_shown_p = 0;
29166 DEFSYM (Qglyphless_char, "glyphless-char");
29167 DEFSYM (Qhex_code, "hex-code");
29168 DEFSYM (Qempty_box, "empty-box");
29169 DEFSYM (Qthin_space, "thin-space");
29170 DEFSYM (Qzero_width, "zero-width");
29172 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29173 /* Intern this now in case it isn't already done.
29174 Setting this variable twice is harmless.
29175 But don't staticpro it here--that is done in alloc.c. */
29176 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29177 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29179 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29180 doc: /* Char-table defining glyphless characters.
29181 Each element, if non-nil, should be one of the following:
29182 an ASCII acronym string: display this string in a box
29183 `hex-code': display the hexadecimal code of a character in a box
29184 `empty-box': display as an empty box
29185 `thin-space': display as 1-pixel width space
29186 `zero-width': don't display
29187 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29188 display method for graphical terminals and text terminals respectively.
29189 GRAPHICAL and TEXT should each have one of the values listed above.
29191 The char-table has one extra slot to control the display of a character for
29192 which no font is found. This slot only takes effect on graphical terminals.
29193 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29194 `thin-space'. The default is `empty-box'. */);
29195 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29196 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29197 Qempty_box);
29199 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29200 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29201 Vdebug_on_message = Qnil;
29205 /* Initialize this module when Emacs starts. */
29207 void
29208 init_xdisp (void)
29210 current_header_line_height = current_mode_line_height = -1;
29212 CHARPOS (this_line_start_pos) = 0;
29214 if (!noninteractive)
29216 struct window *m = XWINDOW (minibuf_window);
29217 Lisp_Object frame = m->frame;
29218 struct frame *f = XFRAME (frame);
29219 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29220 struct window *r = XWINDOW (root);
29221 int i;
29223 echo_area_window = minibuf_window;
29225 r->top_line = FRAME_TOP_MARGIN (f);
29226 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29227 r->total_cols = FRAME_COLS (f);
29229 m->top_line = FRAME_LINES (f) - 1;
29230 m->total_lines = 1;
29231 m->total_cols = FRAME_COLS (f);
29233 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29234 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29235 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29237 /* The default ellipsis glyphs `...'. */
29238 for (i = 0; i < 3; ++i)
29239 default_invis_vector[i] = make_number ('.');
29243 /* Allocate the buffer for frame titles.
29244 Also used for `format-mode-line'. */
29245 int size = 100;
29246 mode_line_noprop_buf = xmalloc (size);
29247 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29248 mode_line_noprop_ptr = mode_line_noprop_buf;
29249 mode_line_target = MODE_LINE_DISPLAY;
29252 help_echo_showing_p = 0;
29255 /* Platform-independent portion of hourglass implementation. */
29257 /* Cancel a currently active hourglass timer, and start a new one. */
29258 void
29259 start_hourglass (void)
29261 #if defined (HAVE_WINDOW_SYSTEM)
29262 EMACS_TIME delay;
29264 cancel_hourglass ();
29266 if (INTEGERP (Vhourglass_delay)
29267 && XINT (Vhourglass_delay) > 0)
29268 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29269 TYPE_MAXIMUM (time_t)),
29271 else if (FLOATP (Vhourglass_delay)
29272 && XFLOAT_DATA (Vhourglass_delay) > 0)
29273 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29274 else
29275 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29277 #ifdef HAVE_NTGUI
29279 extern void w32_note_current_window (void);
29280 w32_note_current_window ();
29282 #endif /* HAVE_NTGUI */
29284 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29285 show_hourglass, NULL);
29286 #endif
29290 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29291 shown. */
29292 void
29293 cancel_hourglass (void)
29295 #if defined (HAVE_WINDOW_SYSTEM)
29296 if (hourglass_atimer)
29298 cancel_atimer (hourglass_atimer);
29299 hourglass_atimer = NULL;
29302 if (hourglass_shown_p)
29303 hide_hourglass ();
29304 #endif