* lisp/progmodes/flymake.el (flymake-xml-program): New option.
[emacs.git] / src / xdisp.c
blob0a79e6fd891959b22d22a986db2aa2d5ce01b63a
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 row_for_charpos_p (struct glyph_row *, ptrdiff_t);
798 static int cursor_row_p (struct glyph_row *);
799 static int redisplay_mode_lines (Lisp_Object, int);
800 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
802 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
804 static void handle_line_prefix (struct it *);
806 static void pint2str (char *, int, ptrdiff_t);
807 static void pint2hrstr (char *, int, ptrdiff_t);
808 static struct text_pos run_window_scroll_functions (Lisp_Object,
809 struct text_pos);
810 static void reconsider_clip_changes (struct window *, struct buffer *);
811 static int text_outside_line_unchanged_p (struct window *,
812 ptrdiff_t, ptrdiff_t);
813 static void store_mode_line_noprop_char (char);
814 static int store_mode_line_noprop (const char *, int, int);
815 static void handle_stop (struct it *);
816 static void handle_stop_backwards (struct it *, ptrdiff_t);
817 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
818 static void ensure_echo_area_buffers (void);
819 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
820 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
821 static int with_echo_area_buffer (struct window *, int,
822 int (*) (ptrdiff_t, Lisp_Object),
823 ptrdiff_t, Lisp_Object);
824 static void clear_garbaged_frames (void);
825 static int current_message_1 (ptrdiff_t, Lisp_Object);
826 static void pop_message (void);
827 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
828 static void set_message (Lisp_Object);
829 static int set_message_1 (ptrdiff_t, Lisp_Object);
830 static int display_echo_area (struct window *);
831 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
832 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
833 static Lisp_Object unwind_redisplay (Lisp_Object);
834 static int string_char_and_length (const unsigned char *, int *);
835 static struct text_pos display_prop_end (struct it *, Lisp_Object,
836 struct text_pos);
837 static int compute_window_start_on_continuation_line (struct window *);
838 static void insert_left_trunc_glyphs (struct it *);
839 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
840 Lisp_Object);
841 static void extend_face_to_end_of_line (struct it *);
842 static int append_space_for_newline (struct it *, int);
843 static int cursor_row_fully_visible_p (struct window *, int, int);
844 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
845 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
846 static int trailing_whitespace_p (ptrdiff_t);
847 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
848 static void push_it (struct it *, struct text_pos *);
849 static void iterate_out_of_display_property (struct it *);
850 static void pop_it (struct it *);
851 static void sync_frame_with_window_matrix_rows (struct window *);
852 static void redisplay_internal (void);
853 static int echo_area_display (int);
854 static void redisplay_windows (Lisp_Object);
855 static void redisplay_window (Lisp_Object, int);
856 static Lisp_Object redisplay_window_error (Lisp_Object);
857 static Lisp_Object redisplay_window_0 (Lisp_Object);
858 static Lisp_Object redisplay_window_1 (Lisp_Object);
859 static int set_cursor_from_row (struct window *, struct glyph_row *,
860 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
861 int, int);
862 static int update_menu_bar (struct frame *, int, int);
863 static int try_window_reusing_current_matrix (struct window *);
864 static int try_window_id (struct window *);
865 static int display_line (struct it *);
866 static int display_mode_lines (struct window *);
867 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
868 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
869 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
870 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
871 static void display_menu_bar (struct window *);
872 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
873 ptrdiff_t *);
874 static int display_string (const char *, Lisp_Object, Lisp_Object,
875 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
876 static void compute_line_metrics (struct it *);
877 static void run_redisplay_end_trigger_hook (struct it *);
878 static int get_overlay_strings (struct it *, ptrdiff_t);
879 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
880 static void next_overlay_string (struct it *);
881 static void reseat (struct it *, struct text_pos, int);
882 static void reseat_1 (struct it *, struct text_pos, int);
883 static void back_to_previous_visible_line_start (struct it *);
884 void reseat_at_previous_visible_line_start (struct it *);
885 static void reseat_at_next_visible_line_start (struct it *, int);
886 static int next_element_from_ellipsis (struct it *);
887 static int next_element_from_display_vector (struct it *);
888 static int next_element_from_string (struct it *);
889 static int next_element_from_c_string (struct it *);
890 static int next_element_from_buffer (struct it *);
891 static int next_element_from_composition (struct it *);
892 static int next_element_from_image (struct it *);
893 static int next_element_from_stretch (struct it *);
894 static void load_overlay_strings (struct it *, ptrdiff_t);
895 static int init_from_display_pos (struct it *, struct window *,
896 struct display_pos *);
897 static void reseat_to_string (struct it *, const char *,
898 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
899 static int get_next_display_element (struct it *);
900 static enum move_it_result
901 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
902 enum move_operation_enum);
903 void move_it_vertically_backward (struct it *, int);
904 static void get_visually_first_element (struct it *);
905 static void init_to_row_start (struct it *, struct window *,
906 struct glyph_row *);
907 static int init_to_row_end (struct it *, struct window *,
908 struct glyph_row *);
909 static void back_to_previous_line_start (struct it *);
910 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
911 static struct text_pos string_pos_nchars_ahead (struct text_pos,
912 Lisp_Object, ptrdiff_t);
913 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
914 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
915 static ptrdiff_t number_of_chars (const char *, bool);
916 static void compute_stop_pos (struct it *);
917 static void compute_string_pos (struct text_pos *, struct text_pos,
918 Lisp_Object);
919 static int face_before_or_after_it_pos (struct it *, int);
920 static ptrdiff_t next_overlay_change (ptrdiff_t);
921 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
922 Lisp_Object, struct text_pos *, ptrdiff_t, int);
923 static int handle_single_display_spec (struct it *, Lisp_Object,
924 Lisp_Object, Lisp_Object,
925 struct text_pos *, ptrdiff_t, int, int);
926 static int underlying_face_id (struct it *);
927 static int in_ellipses_for_invisible_text_p (struct display_pos *,
928 struct window *);
930 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
931 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
933 #ifdef HAVE_WINDOW_SYSTEM
935 static void x_consider_frame_title (Lisp_Object);
936 static int tool_bar_lines_needed (struct frame *, int *);
937 static void update_tool_bar (struct frame *, int);
938 static void build_desired_tool_bar_string (struct frame *f);
939 static int redisplay_tool_bar (struct frame *);
940 static void display_tool_bar_line (struct it *, int);
941 static void notice_overwritten_cursor (struct window *,
942 enum glyph_row_area,
943 int, int, int, int);
944 static void append_stretch_glyph (struct it *, Lisp_Object,
945 int, int, int);
948 #endif /* HAVE_WINDOW_SYSTEM */
950 static void produce_special_glyphs (struct it *, enum display_element_type);
951 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
952 static int coords_in_mouse_face_p (struct window *, int, int);
956 /***********************************************************************
957 Window display dimensions
958 ***********************************************************************/
960 /* Return the bottom boundary y-position for text lines in window W.
961 This is the first y position at which a line cannot start.
962 It is relative to the top of the window.
964 This is the height of W minus the height of a mode line, if any. */
967 window_text_bottom_y (struct window *w)
969 int height = WINDOW_TOTAL_HEIGHT (w);
971 if (WINDOW_WANTS_MODELINE_P (w))
972 height -= CURRENT_MODE_LINE_HEIGHT (w);
973 return height;
976 /* Return the pixel width of display area AREA of window W. AREA < 0
977 means return the total width of W, not including fringes to
978 the left and right of the window. */
981 window_box_width (struct window *w, int area)
983 int cols = w->total_cols;
984 int pixels = 0;
986 if (!w->pseudo_window_p)
988 cols -= WINDOW_SCROLL_BAR_COLS (w);
990 if (area == TEXT_AREA)
992 if (INTEGERP (w->left_margin_cols))
993 cols -= XFASTINT (w->left_margin_cols);
994 if (INTEGERP (w->right_margin_cols))
995 cols -= XFASTINT (w->right_margin_cols);
996 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
998 else if (area == LEFT_MARGIN_AREA)
1000 cols = (INTEGERP (w->left_margin_cols)
1001 ? XFASTINT (w->left_margin_cols) : 0);
1002 pixels = 0;
1004 else if (area == RIGHT_MARGIN_AREA)
1006 cols = (INTEGERP (w->right_margin_cols)
1007 ? XFASTINT (w->right_margin_cols) : 0);
1008 pixels = 0;
1012 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1016 /* Return the pixel height of the display area of window W, not
1017 including mode lines of W, if any. */
1020 window_box_height (struct window *w)
1022 struct frame *f = XFRAME (w->frame);
1023 int height = WINDOW_TOTAL_HEIGHT (w);
1025 eassert (height >= 0);
1027 /* Note: the code below that determines the mode-line/header-line
1028 height is essentially the same as that contained in the macro
1029 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1030 the appropriate glyph row has its `mode_line_p' flag set,
1031 and if it doesn't, uses estimate_mode_line_height instead. */
1033 if (WINDOW_WANTS_MODELINE_P (w))
1035 struct glyph_row *ml_row
1036 = (w->current_matrix && w->current_matrix->rows
1037 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1038 : 0);
1039 if (ml_row && ml_row->mode_line_p)
1040 height -= ml_row->height;
1041 else
1042 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1045 if (WINDOW_WANTS_HEADER_LINE_P (w))
1047 struct glyph_row *hl_row
1048 = (w->current_matrix && w->current_matrix->rows
1049 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1050 : 0);
1051 if (hl_row && hl_row->mode_line_p)
1052 height -= hl_row->height;
1053 else
1054 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1057 /* With a very small font and a mode-line that's taller than
1058 default, we might end up with a negative height. */
1059 return max (0, height);
1062 /* Return the window-relative coordinate of the left edge of display
1063 area AREA of window W. AREA < 0 means return the left edge of the
1064 whole window, to the right of the left fringe of W. */
1067 window_box_left_offset (struct window *w, int area)
1069 int x;
1071 if (w->pseudo_window_p)
1072 return 0;
1074 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1076 if (area == TEXT_AREA)
1077 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1078 + window_box_width (w, LEFT_MARGIN_AREA));
1079 else if (area == RIGHT_MARGIN_AREA)
1080 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1081 + window_box_width (w, LEFT_MARGIN_AREA)
1082 + window_box_width (w, TEXT_AREA)
1083 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1085 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1086 else if (area == LEFT_MARGIN_AREA
1087 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1088 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1090 return x;
1094 /* Return the window-relative coordinate of the right edge of display
1095 area AREA of window W. AREA < 0 means return the right edge of the
1096 whole window, to the left of the right fringe of W. */
1099 window_box_right_offset (struct window *w, int area)
1101 return window_box_left_offset (w, area) + window_box_width (w, area);
1104 /* Return the frame-relative coordinate of the left edge of display
1105 area AREA of window W. AREA < 0 means return the left edge of the
1106 whole window, to the right of the left fringe of W. */
1109 window_box_left (struct window *w, int area)
1111 struct frame *f = XFRAME (w->frame);
1112 int x;
1114 if (w->pseudo_window_p)
1115 return FRAME_INTERNAL_BORDER_WIDTH (f);
1117 x = (WINDOW_LEFT_EDGE_X (w)
1118 + window_box_left_offset (w, area));
1120 return x;
1124 /* Return the frame-relative coordinate of the right edge of display
1125 area AREA of window W. AREA < 0 means return the right edge of the
1126 whole window, to the left of the right fringe of W. */
1129 window_box_right (struct window *w, int area)
1131 return window_box_left (w, area) + window_box_width (w, area);
1134 /* Get the bounding box of the display area AREA of window W, without
1135 mode lines, in frame-relative coordinates. AREA < 0 means the
1136 whole window, not including the left and right fringes of
1137 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1138 coordinates of the upper-left corner of the box. Return in
1139 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1141 void
1142 window_box (struct window *w, int area, int *box_x, int *box_y,
1143 int *box_width, int *box_height)
1145 if (box_width)
1146 *box_width = window_box_width (w, area);
1147 if (box_height)
1148 *box_height = window_box_height (w);
1149 if (box_x)
1150 *box_x = window_box_left (w, area);
1151 if (box_y)
1153 *box_y = WINDOW_TOP_EDGE_Y (w);
1154 if (WINDOW_WANTS_HEADER_LINE_P (w))
1155 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1160 /* Get the bounding box of the display area AREA of window W, without
1161 mode lines. AREA < 0 means the whole window, not including the
1162 left and right fringe of the window. Return in *TOP_LEFT_X
1163 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1164 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1165 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1166 box. */
1168 static void
1169 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1170 int *bottom_right_x, int *bottom_right_y)
1172 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1173 bottom_right_y);
1174 *bottom_right_x += *top_left_x;
1175 *bottom_right_y += *top_left_y;
1180 /***********************************************************************
1181 Utilities
1182 ***********************************************************************/
1184 /* Return the bottom y-position of the line the iterator IT is in.
1185 This can modify IT's settings. */
1188 line_bottom_y (struct it *it)
1190 int line_height = it->max_ascent + it->max_descent;
1191 int line_top_y = it->current_y;
1193 if (line_height == 0)
1195 if (last_height)
1196 line_height = last_height;
1197 else if (IT_CHARPOS (*it) < ZV)
1199 move_it_by_lines (it, 1);
1200 line_height = (it->max_ascent || it->max_descent
1201 ? it->max_ascent + it->max_descent
1202 : last_height);
1204 else
1206 struct glyph_row *row = it->glyph_row;
1208 /* Use the default character height. */
1209 it->glyph_row = NULL;
1210 it->what = IT_CHARACTER;
1211 it->c = ' ';
1212 it->len = 1;
1213 PRODUCE_GLYPHS (it);
1214 line_height = it->ascent + it->descent;
1215 it->glyph_row = row;
1219 return line_top_y + line_height;
1222 /* Subroutine of pos_visible_p below. Extracts a display string, if
1223 any, from the display spec given as its argument. */
1224 static Lisp_Object
1225 string_from_display_spec (Lisp_Object spec)
1227 if (CONSP (spec))
1229 while (CONSP (spec))
1231 if (STRINGP (XCAR (spec)))
1232 return XCAR (spec);
1233 spec = XCDR (spec);
1236 else if (VECTORP (spec))
1238 ptrdiff_t i;
1240 for (i = 0; i < ASIZE (spec); i++)
1242 if (STRINGP (AREF (spec, i)))
1243 return AREF (spec, i);
1245 return Qnil;
1248 return spec;
1252 /* Limit insanely large values of W->hscroll on frame F to the largest
1253 value that will still prevent first_visible_x and last_visible_x of
1254 'struct it' from overflowing an int. */
1255 static int
1256 window_hscroll_limited (struct window *w, struct frame *f)
1258 ptrdiff_t window_hscroll = w->hscroll;
1259 int window_text_width = window_box_width (w, TEXT_AREA);
1260 int colwidth = FRAME_COLUMN_WIDTH (f);
1262 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1263 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1265 return window_hscroll;
1268 /* Return 1 if position CHARPOS is visible in window W.
1269 CHARPOS < 0 means return info about WINDOW_END position.
1270 If visible, set *X and *Y to pixel coordinates of top left corner.
1271 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1272 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1275 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1276 int *rtop, int *rbot, int *rowh, int *vpos)
1278 struct it it;
1279 void *itdata = bidi_shelve_cache ();
1280 struct text_pos top;
1281 int visible_p = 0;
1282 struct buffer *old_buffer = NULL;
1284 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1285 return visible_p;
1287 if (XBUFFER (w->contents) != current_buffer)
1289 old_buffer = current_buffer;
1290 set_buffer_internal_1 (XBUFFER (w->contents));
1293 SET_TEXT_POS_FROM_MARKER (top, w->start);
1294 /* Scrolling a minibuffer window via scroll bar when the echo area
1295 shows long text sometimes resets the minibuffer contents behind
1296 our backs. */
1297 if (CHARPOS (top) > ZV)
1298 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1300 /* Compute exact mode line heights. */
1301 if (WINDOW_WANTS_MODELINE_P (w))
1302 current_mode_line_height
1303 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1304 BVAR (current_buffer, mode_line_format));
1306 if (WINDOW_WANTS_HEADER_LINE_P (w))
1307 current_header_line_height
1308 = display_mode_line (w, HEADER_LINE_FACE_ID,
1309 BVAR (current_buffer, header_line_format));
1311 start_display (&it, w, top);
1312 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1313 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1315 if (charpos >= 0
1316 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1317 && IT_CHARPOS (it) >= charpos)
1318 /* When scanning backwards under bidi iteration, move_it_to
1319 stops at or _before_ CHARPOS, because it stops at or to
1320 the _right_ of the character at CHARPOS. */
1321 || (it.bidi_p && it.bidi_it.scan_dir == -1
1322 && IT_CHARPOS (it) <= charpos)))
1324 /* We have reached CHARPOS, or passed it. How the call to
1325 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1326 or covered by a display property, move_it_to stops at the end
1327 of the invisible text, to the right of CHARPOS. (ii) If
1328 CHARPOS is in a display vector, move_it_to stops on its last
1329 glyph. */
1330 int top_x = it.current_x;
1331 int top_y = it.current_y;
1332 /* Calling line_bottom_y may change it.method, it.position, etc. */
1333 enum it_method it_method = it.method;
1334 int bottom_y = (last_height = 0, line_bottom_y (&it));
1335 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1337 if (top_y < window_top_y)
1338 visible_p = bottom_y > window_top_y;
1339 else if (top_y < it.last_visible_y)
1340 visible_p = 1;
1341 if (bottom_y >= it.last_visible_y
1342 && it.bidi_p && it.bidi_it.scan_dir == -1
1343 && IT_CHARPOS (it) < charpos)
1345 /* When the last line of the window is scanned backwards
1346 under bidi iteration, we could be duped into thinking
1347 that we have passed CHARPOS, when in fact move_it_to
1348 simply stopped short of CHARPOS because it reached
1349 last_visible_y. To see if that's what happened, we call
1350 move_it_to again with a slightly larger vertical limit,
1351 and see if it actually moved vertically; if it did, we
1352 didn't really reach CHARPOS, which is beyond window end. */
1353 struct it save_it = it;
1354 /* Why 10? because we don't know how many canonical lines
1355 will the height of the next line(s) be. So we guess. */
1356 int ten_more_lines =
1357 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1359 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1360 MOVE_TO_POS | MOVE_TO_Y);
1361 if (it.current_y > top_y)
1362 visible_p = 0;
1364 it = save_it;
1366 if (visible_p)
1368 if (it_method == GET_FROM_DISPLAY_VECTOR)
1370 /* We stopped on the last glyph of a display vector.
1371 Try and recompute. Hack alert! */
1372 if (charpos < 2 || top.charpos >= charpos)
1373 top_x = it.glyph_row->x;
1374 else
1376 struct it it2;
1377 start_display (&it2, w, top);
1378 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1379 get_next_display_element (&it2);
1380 PRODUCE_GLYPHS (&it2);
1381 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1382 || it2.current_x > it2.last_visible_x)
1383 top_x = it.glyph_row->x;
1384 else
1386 top_x = it2.current_x;
1387 top_y = it2.current_y;
1391 else if (IT_CHARPOS (it) != charpos)
1393 Lisp_Object cpos = make_number (charpos);
1394 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1395 Lisp_Object string = string_from_display_spec (spec);
1396 struct text_pos tpos;
1397 int replacing_spec_p;
1398 bool newline_in_string
1399 = (STRINGP (string)
1400 && memchr (SDATA (string), '\n', SBYTES (string)));
1402 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1403 replacing_spec_p
1404 = (!NILP (spec)
1405 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1406 charpos, FRAME_WINDOW_P (it.f)));
1407 /* The tricky code below is needed because there's a
1408 discrepancy between move_it_to and how we set cursor
1409 when PT is at the beginning of a portion of text
1410 covered by a display property or an overlay with a
1411 display property, or the display line ends in a
1412 newline from a display string. move_it_to will stop
1413 _after_ such display strings, whereas
1414 set_cursor_from_row conspires with cursor_row_p to
1415 place the cursor on the first glyph produced from the
1416 display string. */
1418 /* We have overshoot PT because it is covered by a
1419 display property that replaces the text it covers.
1420 If the string includes embedded newlines, we are also
1421 in the wrong display line. Backtrack to the correct
1422 line, where the display property begins. */
1423 if (replacing_spec_p)
1425 Lisp_Object startpos, endpos;
1426 EMACS_INT start, end;
1427 struct it it3;
1428 int it3_moved;
1430 /* Find the first and the last buffer positions
1431 covered by the display string. */
1432 endpos =
1433 Fnext_single_char_property_change (cpos, Qdisplay,
1434 Qnil, Qnil);
1435 startpos =
1436 Fprevious_single_char_property_change (endpos, Qdisplay,
1437 Qnil, Qnil);
1438 start = XFASTINT (startpos);
1439 end = XFASTINT (endpos);
1440 /* Move to the last buffer position before the
1441 display property. */
1442 start_display (&it3, w, top);
1443 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1444 /* Move forward one more line if the position before
1445 the display string is a newline or if it is the
1446 rightmost character on a line that is
1447 continued or word-wrapped. */
1448 if (it3.method == GET_FROM_BUFFER
1449 && (it3.c == '\n'
1450 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1451 move_it_by_lines (&it3, 1);
1452 else if (move_it_in_display_line_to (&it3, -1,
1453 it3.current_x
1454 + it3.pixel_width,
1455 MOVE_TO_X)
1456 == MOVE_LINE_CONTINUED)
1458 move_it_by_lines (&it3, 1);
1459 /* When we are under word-wrap, the #$@%!
1460 move_it_by_lines moves 2 lines, so we need to
1461 fix that up. */
1462 if (it3.line_wrap == WORD_WRAP)
1463 move_it_by_lines (&it3, -1);
1466 /* Record the vertical coordinate of the display
1467 line where we wound up. */
1468 top_y = it3.current_y;
1469 if (it3.bidi_p)
1471 /* When characters are reordered for display,
1472 the character displayed to the left of the
1473 display string could be _after_ the display
1474 property in the logical order. Use the
1475 smallest vertical position of these two. */
1476 start_display (&it3, w, top);
1477 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1478 if (it3.current_y < top_y)
1479 top_y = it3.current_y;
1481 /* Move from the top of the window to the beginning
1482 of the display line where the display string
1483 begins. */
1484 start_display (&it3, w, top);
1485 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1486 /* If it3_moved stays zero after the 'while' loop
1487 below, that means we already were at a newline
1488 before the loop (e.g., the display string begins
1489 with a newline), so we don't need to (and cannot)
1490 inspect the glyphs of it3.glyph_row, because
1491 PRODUCE_GLYPHS will not produce anything for a
1492 newline, and thus it3.glyph_row stays at its
1493 stale content it got at top of the window. */
1494 it3_moved = 0;
1495 /* Finally, advance the iterator until we hit the
1496 first display element whose character position is
1497 CHARPOS, or until the first newline from the
1498 display string, which signals the end of the
1499 display line. */
1500 while (get_next_display_element (&it3))
1502 PRODUCE_GLYPHS (&it3);
1503 if (IT_CHARPOS (it3) == charpos
1504 || ITERATOR_AT_END_OF_LINE_P (&it3))
1505 break;
1506 it3_moved = 1;
1507 set_iterator_to_next (&it3, 0);
1509 top_x = it3.current_x - it3.pixel_width;
1510 /* Normally, we would exit the above loop because we
1511 found the display element whose character
1512 position is CHARPOS. For the contingency that we
1513 didn't, and stopped at the first newline from the
1514 display string, move back over the glyphs
1515 produced from the string, until we find the
1516 rightmost glyph not from the string. */
1517 if (it3_moved
1518 && newline_in_string
1519 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1521 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1522 + it3.glyph_row->used[TEXT_AREA];
1524 while (EQ ((g - 1)->object, string))
1526 --g;
1527 top_x -= g->pixel_width;
1529 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1530 + it3.glyph_row->used[TEXT_AREA]);
1535 *x = top_x;
1536 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1537 *rtop = max (0, window_top_y - top_y);
1538 *rbot = max (0, bottom_y - it.last_visible_y);
1539 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1540 - max (top_y, window_top_y)));
1541 *vpos = it.vpos;
1544 else
1546 /* We were asked to provide info about WINDOW_END. */
1547 struct it it2;
1548 void *it2data = NULL;
1550 SAVE_IT (it2, it, it2data);
1551 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1552 move_it_by_lines (&it, 1);
1553 if (charpos < IT_CHARPOS (it)
1554 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1556 visible_p = 1;
1557 RESTORE_IT (&it2, &it2, it2data);
1558 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1559 *x = it2.current_x;
1560 *y = it2.current_y + it2.max_ascent - it2.ascent;
1561 *rtop = max (0, -it2.current_y);
1562 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1563 - it.last_visible_y));
1564 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1565 it.last_visible_y)
1566 - max (it2.current_y,
1567 WINDOW_HEADER_LINE_HEIGHT (w))));
1568 *vpos = it2.vpos;
1570 else
1571 bidi_unshelve_cache (it2data, 1);
1573 bidi_unshelve_cache (itdata, 0);
1575 if (old_buffer)
1576 set_buffer_internal_1 (old_buffer);
1578 current_header_line_height = current_mode_line_height = -1;
1580 if (visible_p && w->hscroll > 0)
1581 *x -=
1582 window_hscroll_limited (w, WINDOW_XFRAME (w))
1583 * WINDOW_FRAME_COLUMN_WIDTH (w);
1585 #if 0
1586 /* Debugging code. */
1587 if (visible_p)
1588 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1589 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1590 else
1591 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1592 #endif
1594 return visible_p;
1598 /* Return the next character from STR. Return in *LEN the length of
1599 the character. This is like STRING_CHAR_AND_LENGTH but never
1600 returns an invalid character. If we find one, we return a `?', but
1601 with the length of the invalid character. */
1603 static int
1604 string_char_and_length (const unsigned char *str, int *len)
1606 int c;
1608 c = STRING_CHAR_AND_LENGTH (str, *len);
1609 if (!CHAR_VALID_P (c))
1610 /* We may not change the length here because other places in Emacs
1611 don't use this function, i.e. they silently accept invalid
1612 characters. */
1613 c = '?';
1615 return c;
1620 /* Given a position POS containing a valid character and byte position
1621 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1623 static struct text_pos
1624 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1626 eassert (STRINGP (string) && nchars >= 0);
1628 if (STRING_MULTIBYTE (string))
1630 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1631 int len;
1633 while (nchars--)
1635 string_char_and_length (p, &len);
1636 p += len;
1637 CHARPOS (pos) += 1;
1638 BYTEPOS (pos) += len;
1641 else
1642 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1644 return pos;
1648 /* Value is the text position, i.e. character and byte position,
1649 for character position CHARPOS in STRING. */
1651 static struct text_pos
1652 string_pos (ptrdiff_t charpos, Lisp_Object string)
1654 struct text_pos pos;
1655 eassert (STRINGP (string));
1656 eassert (charpos >= 0);
1657 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1658 return pos;
1662 /* Value is a text position, i.e. character and byte position, for
1663 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1664 means recognize multibyte characters. */
1666 static struct text_pos
1667 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1669 struct text_pos pos;
1671 eassert (s != NULL);
1672 eassert (charpos >= 0);
1674 if (multibyte_p)
1676 int len;
1678 SET_TEXT_POS (pos, 0, 0);
1679 while (charpos--)
1681 string_char_and_length ((const unsigned char *) s, &len);
1682 s += len;
1683 CHARPOS (pos) += 1;
1684 BYTEPOS (pos) += len;
1687 else
1688 SET_TEXT_POS (pos, charpos, charpos);
1690 return pos;
1694 /* Value is the number of characters in C string S. MULTIBYTE_P
1695 non-zero means recognize multibyte characters. */
1697 static ptrdiff_t
1698 number_of_chars (const char *s, bool multibyte_p)
1700 ptrdiff_t nchars;
1702 if (multibyte_p)
1704 ptrdiff_t rest = strlen (s);
1705 int len;
1706 const unsigned char *p = (const unsigned char *) s;
1708 for (nchars = 0; rest > 0; ++nchars)
1710 string_char_and_length (p, &len);
1711 rest -= len, p += len;
1714 else
1715 nchars = strlen (s);
1717 return nchars;
1721 /* Compute byte position NEWPOS->bytepos corresponding to
1722 NEWPOS->charpos. POS is a known position in string STRING.
1723 NEWPOS->charpos must be >= POS.charpos. */
1725 static void
1726 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1728 eassert (STRINGP (string));
1729 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1731 if (STRING_MULTIBYTE (string))
1732 *newpos = string_pos_nchars_ahead (pos, string,
1733 CHARPOS (*newpos) - CHARPOS (pos));
1734 else
1735 BYTEPOS (*newpos) = CHARPOS (*newpos);
1738 /* EXPORT:
1739 Return an estimation of the pixel height of mode or header lines on
1740 frame F. FACE_ID specifies what line's height to estimate. */
1743 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1745 #ifdef HAVE_WINDOW_SYSTEM
1746 if (FRAME_WINDOW_P (f))
1748 int height = FONT_HEIGHT (FRAME_FONT (f));
1750 /* This function is called so early when Emacs starts that the face
1751 cache and mode line face are not yet initialized. */
1752 if (FRAME_FACE_CACHE (f))
1754 struct face *face = FACE_FROM_ID (f, face_id);
1755 if (face)
1757 if (face->font)
1758 height = FONT_HEIGHT (face->font);
1759 if (face->box_line_width > 0)
1760 height += 2 * face->box_line_width;
1764 return height;
1766 #endif
1768 return 1;
1771 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1772 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1773 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1774 not force the value into range. */
1776 void
1777 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1778 int *x, int *y, NativeRectangle *bounds, int noclip)
1781 #ifdef HAVE_WINDOW_SYSTEM
1782 if (FRAME_WINDOW_P (f))
1784 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1785 even for negative values. */
1786 if (pix_x < 0)
1787 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1788 if (pix_y < 0)
1789 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1791 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1792 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1794 if (bounds)
1795 STORE_NATIVE_RECT (*bounds,
1796 FRAME_COL_TO_PIXEL_X (f, pix_x),
1797 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1798 FRAME_COLUMN_WIDTH (f) - 1,
1799 FRAME_LINE_HEIGHT (f) - 1);
1801 if (!noclip)
1803 if (pix_x < 0)
1804 pix_x = 0;
1805 else if (pix_x > FRAME_TOTAL_COLS (f))
1806 pix_x = FRAME_TOTAL_COLS (f);
1808 if (pix_y < 0)
1809 pix_y = 0;
1810 else if (pix_y > FRAME_LINES (f))
1811 pix_y = FRAME_LINES (f);
1814 #endif
1816 *x = pix_x;
1817 *y = pix_y;
1821 /* Find the glyph under window-relative coordinates X/Y in window W.
1822 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1823 strings. Return in *HPOS and *VPOS the row and column number of
1824 the glyph found. Return in *AREA the glyph area containing X.
1825 Value is a pointer to the glyph found or null if X/Y is not on
1826 text, or we can't tell because W's current matrix is not up to
1827 date. */
1829 static
1830 struct glyph *
1831 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1832 int *dx, int *dy, int *area)
1834 struct glyph *glyph, *end;
1835 struct glyph_row *row = NULL;
1836 int x0, i;
1838 /* Find row containing Y. Give up if some row is not enabled. */
1839 for (i = 0; i < w->current_matrix->nrows; ++i)
1841 row = MATRIX_ROW (w->current_matrix, i);
1842 if (!row->enabled_p)
1843 return NULL;
1844 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1845 break;
1848 *vpos = i;
1849 *hpos = 0;
1851 /* Give up if Y is not in the window. */
1852 if (i == w->current_matrix->nrows)
1853 return NULL;
1855 /* Get the glyph area containing X. */
1856 if (w->pseudo_window_p)
1858 *area = TEXT_AREA;
1859 x0 = 0;
1861 else
1863 if (x < window_box_left_offset (w, TEXT_AREA))
1865 *area = LEFT_MARGIN_AREA;
1866 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1868 else if (x < window_box_right_offset (w, TEXT_AREA))
1870 *area = TEXT_AREA;
1871 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1873 else
1875 *area = RIGHT_MARGIN_AREA;
1876 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1880 /* Find glyph containing X. */
1881 glyph = row->glyphs[*area];
1882 end = glyph + row->used[*area];
1883 x -= x0;
1884 while (glyph < end && x >= glyph->pixel_width)
1886 x -= glyph->pixel_width;
1887 ++glyph;
1890 if (glyph == end)
1891 return NULL;
1893 if (dx)
1895 *dx = x;
1896 *dy = y - (row->y + row->ascent - glyph->ascent);
1899 *hpos = glyph - row->glyphs[*area];
1900 return glyph;
1903 /* Convert frame-relative x/y to coordinates relative to window W.
1904 Takes pseudo-windows into account. */
1906 static void
1907 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1909 if (w->pseudo_window_p)
1911 /* A pseudo-window is always full-width, and starts at the
1912 left edge of the frame, plus a frame border. */
1913 struct frame *f = XFRAME (w->frame);
1914 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1915 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1917 else
1919 *x -= WINDOW_LEFT_EDGE_X (w);
1920 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1924 #ifdef HAVE_WINDOW_SYSTEM
1926 /* EXPORT:
1927 Return in RECTS[] at most N clipping rectangles for glyph string S.
1928 Return the number of stored rectangles. */
1931 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1933 XRectangle r;
1935 if (n <= 0)
1936 return 0;
1938 if (s->row->full_width_p)
1940 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1941 r.x = WINDOW_LEFT_EDGE_X (s->w);
1942 r.width = WINDOW_TOTAL_WIDTH (s->w);
1944 /* Unless displaying a mode or menu bar line, which are always
1945 fully visible, clip to the visible part of the row. */
1946 if (s->w->pseudo_window_p)
1947 r.height = s->row->visible_height;
1948 else
1949 r.height = s->height;
1951 else
1953 /* This is a text line that may be partially visible. */
1954 r.x = window_box_left (s->w, s->area);
1955 r.width = window_box_width (s->w, s->area);
1956 r.height = s->row->visible_height;
1959 if (s->clip_head)
1960 if (r.x < s->clip_head->x)
1962 if (r.width >= s->clip_head->x - r.x)
1963 r.width -= s->clip_head->x - r.x;
1964 else
1965 r.width = 0;
1966 r.x = s->clip_head->x;
1968 if (s->clip_tail)
1969 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1971 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1972 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1973 else
1974 r.width = 0;
1977 /* If S draws overlapping rows, it's sufficient to use the top and
1978 bottom of the window for clipping because this glyph string
1979 intentionally draws over other lines. */
1980 if (s->for_overlaps)
1982 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1983 r.height = window_text_bottom_y (s->w) - r.y;
1985 /* Alas, the above simple strategy does not work for the
1986 environments with anti-aliased text: if the same text is
1987 drawn onto the same place multiple times, it gets thicker.
1988 If the overlap we are processing is for the erased cursor, we
1989 take the intersection with the rectangle of the cursor. */
1990 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1992 XRectangle rc, r_save = r;
1994 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1995 rc.y = s->w->phys_cursor.y;
1996 rc.width = s->w->phys_cursor_width;
1997 rc.height = s->w->phys_cursor_height;
1999 x_intersect_rectangles (&r_save, &rc, &r);
2002 else
2004 /* Don't use S->y for clipping because it doesn't take partially
2005 visible lines into account. For example, it can be negative for
2006 partially visible lines at the top of a window. */
2007 if (!s->row->full_width_p
2008 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2009 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2010 else
2011 r.y = max (0, s->row->y);
2014 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2016 /* If drawing the cursor, don't let glyph draw outside its
2017 advertised boundaries. Cleartype does this under some circumstances. */
2018 if (s->hl == DRAW_CURSOR)
2020 struct glyph *glyph = s->first_glyph;
2021 int height, max_y;
2023 if (s->x > r.x)
2025 r.width -= s->x - r.x;
2026 r.x = s->x;
2028 r.width = min (r.width, glyph->pixel_width);
2030 /* If r.y is below window bottom, ensure that we still see a cursor. */
2031 height = min (glyph->ascent + glyph->descent,
2032 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2033 max_y = window_text_bottom_y (s->w) - height;
2034 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2035 if (s->ybase - glyph->ascent > max_y)
2037 r.y = max_y;
2038 r.height = height;
2040 else
2042 /* Don't draw cursor glyph taller than our actual glyph. */
2043 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2044 if (height < r.height)
2046 max_y = r.y + r.height;
2047 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2048 r.height = min (max_y - r.y, height);
2053 if (s->row->clip)
2055 XRectangle r_save = r;
2057 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2058 r.width = 0;
2061 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2062 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2064 #ifdef CONVERT_FROM_XRECT
2065 CONVERT_FROM_XRECT (r, *rects);
2066 #else
2067 *rects = r;
2068 #endif
2069 return 1;
2071 else
2073 /* If we are processing overlapping and allowed to return
2074 multiple clipping rectangles, we exclude the row of the glyph
2075 string from the clipping rectangle. This is to avoid drawing
2076 the same text on the environment with anti-aliasing. */
2077 #ifdef CONVERT_FROM_XRECT
2078 XRectangle rs[2];
2079 #else
2080 XRectangle *rs = rects;
2081 #endif
2082 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2084 if (s->for_overlaps & OVERLAPS_PRED)
2086 rs[i] = r;
2087 if (r.y + r.height > row_y)
2089 if (r.y < row_y)
2090 rs[i].height = row_y - r.y;
2091 else
2092 rs[i].height = 0;
2094 i++;
2096 if (s->for_overlaps & OVERLAPS_SUCC)
2098 rs[i] = r;
2099 if (r.y < row_y + s->row->visible_height)
2101 if (r.y + r.height > row_y + s->row->visible_height)
2103 rs[i].y = row_y + s->row->visible_height;
2104 rs[i].height = r.y + r.height - rs[i].y;
2106 else
2107 rs[i].height = 0;
2109 i++;
2112 n = i;
2113 #ifdef CONVERT_FROM_XRECT
2114 for (i = 0; i < n; i++)
2115 CONVERT_FROM_XRECT (rs[i], rects[i]);
2116 #endif
2117 return n;
2121 /* EXPORT:
2122 Return in *NR the clipping rectangle for glyph string S. */
2124 void
2125 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2127 get_glyph_string_clip_rects (s, nr, 1);
2131 /* EXPORT:
2132 Return the position and height of the phys cursor in window W.
2133 Set w->phys_cursor_width to width of phys cursor.
2136 void
2137 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2138 struct glyph *glyph, int *xp, int *yp, int *heightp)
2140 struct frame *f = XFRAME (WINDOW_FRAME (w));
2141 int x, y, wd, h, h0, y0;
2143 /* Compute the width of the rectangle to draw. If on a stretch
2144 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2145 rectangle as wide as the glyph, but use a canonical character
2146 width instead. */
2147 wd = glyph->pixel_width - 1;
2148 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2149 wd++; /* Why? */
2150 #endif
2152 x = w->phys_cursor.x;
2153 if (x < 0)
2155 wd += x;
2156 x = 0;
2159 if (glyph->type == STRETCH_GLYPH
2160 && !x_stretch_cursor_p)
2161 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2162 w->phys_cursor_width = wd;
2164 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2166 /* If y is below window bottom, ensure that we still see a cursor. */
2167 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2169 h = max (h0, glyph->ascent + glyph->descent);
2170 h0 = min (h0, glyph->ascent + glyph->descent);
2172 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2173 if (y < y0)
2175 h = max (h - (y0 - y) + 1, h0);
2176 y = y0 - 1;
2178 else
2180 y0 = window_text_bottom_y (w) - h0;
2181 if (y > y0)
2183 h += y - y0;
2184 y = y0;
2188 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2189 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2190 *heightp = h;
2194 * Remember which glyph the mouse is over.
2197 void
2198 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2200 Lisp_Object window;
2201 struct window *w;
2202 struct glyph_row *r, *gr, *end_row;
2203 enum window_part part;
2204 enum glyph_row_area area;
2205 int x, y, width, height;
2207 /* Try to determine frame pixel position and size of the glyph under
2208 frame pixel coordinates X/Y on frame F. */
2210 if (!f->glyphs_initialized_p
2211 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2212 NILP (window)))
2214 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2215 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2216 goto virtual_glyph;
2219 w = XWINDOW (window);
2220 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2221 height = WINDOW_FRAME_LINE_HEIGHT (w);
2223 x = window_relative_x_coord (w, part, gx);
2224 y = gy - WINDOW_TOP_EDGE_Y (w);
2226 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2227 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2229 if (w->pseudo_window_p)
2231 area = TEXT_AREA;
2232 part = ON_MODE_LINE; /* Don't adjust margin. */
2233 goto text_glyph;
2236 switch (part)
2238 case ON_LEFT_MARGIN:
2239 area = LEFT_MARGIN_AREA;
2240 goto text_glyph;
2242 case ON_RIGHT_MARGIN:
2243 area = RIGHT_MARGIN_AREA;
2244 goto text_glyph;
2246 case ON_HEADER_LINE:
2247 case ON_MODE_LINE:
2248 gr = (part == ON_HEADER_LINE
2249 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2250 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2251 gy = gr->y;
2252 area = TEXT_AREA;
2253 goto text_glyph_row_found;
2255 case ON_TEXT:
2256 area = TEXT_AREA;
2258 text_glyph:
2259 gr = 0; gy = 0;
2260 for (; r <= end_row && r->enabled_p; ++r)
2261 if (r->y + r->height > y)
2263 gr = r; gy = r->y;
2264 break;
2267 text_glyph_row_found:
2268 if (gr && gy <= y)
2270 struct glyph *g = gr->glyphs[area];
2271 struct glyph *end = g + gr->used[area];
2273 height = gr->height;
2274 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2275 if (gx + g->pixel_width > x)
2276 break;
2278 if (g < end)
2280 if (g->type == IMAGE_GLYPH)
2282 /* Don't remember when mouse is over image, as
2283 image may have hot-spots. */
2284 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2285 return;
2287 width = g->pixel_width;
2289 else
2291 /* Use nominal char spacing at end of line. */
2292 x -= gx;
2293 gx += (x / width) * width;
2296 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2297 gx += window_box_left_offset (w, area);
2299 else
2301 /* Use nominal line height at end of window. */
2302 gx = (x / width) * width;
2303 y -= gy;
2304 gy += (y / height) * height;
2306 break;
2308 case ON_LEFT_FRINGE:
2309 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2310 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2311 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2312 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2313 goto row_glyph;
2315 case ON_RIGHT_FRINGE:
2316 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2317 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2318 : window_box_right_offset (w, TEXT_AREA));
2319 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2320 goto row_glyph;
2322 case ON_SCROLL_BAR:
2323 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2325 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2326 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2327 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2328 : 0)));
2329 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2331 row_glyph:
2332 gr = 0, gy = 0;
2333 for (; r <= end_row && r->enabled_p; ++r)
2334 if (r->y + r->height > y)
2336 gr = r; gy = r->y;
2337 break;
2340 if (gr && gy <= y)
2341 height = gr->height;
2342 else
2344 /* Use nominal line height at end of window. */
2345 y -= gy;
2346 gy += (y / height) * height;
2348 break;
2350 default:
2352 virtual_glyph:
2353 /* If there is no glyph under the mouse, then we divide the screen
2354 into a grid of the smallest glyph in the frame, and use that
2355 as our "glyph". */
2357 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2358 round down even for negative values. */
2359 if (gx < 0)
2360 gx -= width - 1;
2361 if (gy < 0)
2362 gy -= height - 1;
2364 gx = (gx / width) * width;
2365 gy = (gy / height) * height;
2367 goto store_rect;
2370 gx += WINDOW_LEFT_EDGE_X (w);
2371 gy += WINDOW_TOP_EDGE_Y (w);
2373 store_rect:
2374 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2376 /* Visible feedback for debugging. */
2377 #if 0
2378 #if HAVE_X_WINDOWS
2379 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2380 f->output_data.x->normal_gc,
2381 gx, gy, width, height);
2382 #endif
2383 #endif
2387 #endif /* HAVE_WINDOW_SYSTEM */
2390 /***********************************************************************
2391 Lisp form evaluation
2392 ***********************************************************************/
2394 /* Error handler for safe_eval and safe_call. */
2396 static Lisp_Object
2397 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2399 add_to_log ("Error during redisplay: %S signaled %S",
2400 Flist (nargs, args), arg);
2401 return Qnil;
2404 /* Call function FUNC with the rest of NARGS - 1 arguments
2405 following. Return the result, or nil if something went
2406 wrong. Prevent redisplay during the evaluation. */
2408 Lisp_Object
2409 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2411 Lisp_Object val;
2413 if (inhibit_eval_during_redisplay)
2414 val = Qnil;
2415 else
2417 va_list ap;
2418 ptrdiff_t i;
2419 ptrdiff_t count = SPECPDL_INDEX ();
2420 struct gcpro gcpro1;
2421 Lisp_Object *args = alloca (nargs * word_size);
2423 args[0] = func;
2424 va_start (ap, func);
2425 for (i = 1; i < nargs; i++)
2426 args[i] = va_arg (ap, Lisp_Object);
2427 va_end (ap);
2429 GCPRO1 (args[0]);
2430 gcpro1.nvars = nargs;
2431 specbind (Qinhibit_redisplay, Qt);
2432 /* Use Qt to ensure debugger does not run,
2433 so there is no possibility of wanting to redisplay. */
2434 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2435 safe_eval_handler);
2436 UNGCPRO;
2437 val = unbind_to (count, val);
2440 return val;
2444 /* Call function FN with one argument ARG.
2445 Return the result, or nil if something went wrong. */
2447 Lisp_Object
2448 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2450 return safe_call (2, fn, arg);
2453 static Lisp_Object Qeval;
2455 Lisp_Object
2456 safe_eval (Lisp_Object sexpr)
2458 return safe_call1 (Qeval, sexpr);
2461 /* Call function FN with two arguments ARG1 and ARG2.
2462 Return the result, or nil if something went wrong. */
2464 Lisp_Object
2465 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2467 return safe_call (3, fn, arg1, arg2);
2472 /***********************************************************************
2473 Debugging
2474 ***********************************************************************/
2476 #if 0
2478 /* Define CHECK_IT to perform sanity checks on iterators.
2479 This is for debugging. It is too slow to do unconditionally. */
2481 static void
2482 check_it (struct it *it)
2484 if (it->method == GET_FROM_STRING)
2486 eassert (STRINGP (it->string));
2487 eassert (IT_STRING_CHARPOS (*it) >= 0);
2489 else
2491 eassert (IT_STRING_CHARPOS (*it) < 0);
2492 if (it->method == GET_FROM_BUFFER)
2494 /* Check that character and byte positions agree. */
2495 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2499 if (it->dpvec)
2500 eassert (it->current.dpvec_index >= 0);
2501 else
2502 eassert (it->current.dpvec_index < 0);
2505 #define CHECK_IT(IT) check_it ((IT))
2507 #else /* not 0 */
2509 #define CHECK_IT(IT) (void) 0
2511 #endif /* not 0 */
2514 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2516 /* Check that the window end of window W is what we expect it
2517 to be---the last row in the current matrix displaying text. */
2519 static void
2520 check_window_end (struct window *w)
2522 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2524 struct glyph_row *row;
2525 eassert ((row = MATRIX_ROW (w->current_matrix,
2526 XFASTINT (w->window_end_vpos)),
2527 !row->enabled_p
2528 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2529 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2533 #define CHECK_WINDOW_END(W) check_window_end ((W))
2535 #else
2537 #define CHECK_WINDOW_END(W) (void) 0
2539 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2541 /* Return mark position if current buffer has the region of non-zero length,
2542 or -1 otherwise. */
2544 static ptrdiff_t
2545 markpos_of_region (void)
2547 if (!NILP (Vtransient_mark_mode)
2548 && !NILP (BVAR (current_buffer, mark_active))
2549 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2551 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2553 if (markpos != PT)
2554 return markpos;
2556 return -1;
2559 /***********************************************************************
2560 Iterator initialization
2561 ***********************************************************************/
2563 /* Initialize IT for displaying current_buffer in window W, starting
2564 at character position CHARPOS. CHARPOS < 0 means that no buffer
2565 position is specified which is useful when the iterator is assigned
2566 a position later. BYTEPOS is the byte position corresponding to
2567 CHARPOS.
2569 If ROW is not null, calls to produce_glyphs with IT as parameter
2570 will produce glyphs in that row.
2572 BASE_FACE_ID is the id of a base face to use. It must be one of
2573 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2574 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2575 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2577 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2578 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2579 will be initialized to use the corresponding mode line glyph row of
2580 the desired matrix of W. */
2582 void
2583 init_iterator (struct it *it, struct window *w,
2584 ptrdiff_t charpos, ptrdiff_t bytepos,
2585 struct glyph_row *row, enum face_id base_face_id)
2587 ptrdiff_t markpos;
2588 enum face_id remapped_base_face_id = base_face_id;
2590 /* Some precondition checks. */
2591 eassert (w != NULL && it != NULL);
2592 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2593 && charpos <= ZV));
2595 /* If face attributes have been changed since the last redisplay,
2596 free realized faces now because they depend on face definitions
2597 that might have changed. Don't free faces while there might be
2598 desired matrices pending which reference these faces. */
2599 if (face_change_count && !inhibit_free_realized_faces)
2601 face_change_count = 0;
2602 free_all_realized_faces (Qnil);
2605 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2606 if (! NILP (Vface_remapping_alist))
2607 remapped_base_face_id
2608 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2610 /* Use one of the mode line rows of W's desired matrix if
2611 appropriate. */
2612 if (row == NULL)
2614 if (base_face_id == MODE_LINE_FACE_ID
2615 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2616 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2617 else if (base_face_id == HEADER_LINE_FACE_ID)
2618 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2621 /* Clear IT. */
2622 memset (it, 0, sizeof *it);
2623 it->current.overlay_string_index = -1;
2624 it->current.dpvec_index = -1;
2625 it->base_face_id = remapped_base_face_id;
2626 it->string = Qnil;
2627 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2628 it->paragraph_embedding = L2R;
2629 it->bidi_it.string.lstring = Qnil;
2630 it->bidi_it.string.s = NULL;
2631 it->bidi_it.string.bufpos = 0;
2633 /* The window in which we iterate over current_buffer: */
2634 XSETWINDOW (it->window, w);
2635 it->w = w;
2636 it->f = XFRAME (w->frame);
2638 it->cmp_it.id = -1;
2640 /* Extra space between lines (on window systems only). */
2641 if (base_face_id == DEFAULT_FACE_ID
2642 && FRAME_WINDOW_P (it->f))
2644 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2645 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2646 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2647 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2648 * FRAME_LINE_HEIGHT (it->f));
2649 else if (it->f->extra_line_spacing > 0)
2650 it->extra_line_spacing = it->f->extra_line_spacing;
2651 it->max_extra_line_spacing = 0;
2654 /* If realized faces have been removed, e.g. because of face
2655 attribute changes of named faces, recompute them. When running
2656 in batch mode, the face cache of the initial frame is null. If
2657 we happen to get called, make a dummy face cache. */
2658 if (FRAME_FACE_CACHE (it->f) == NULL)
2659 init_frame_faces (it->f);
2660 if (FRAME_FACE_CACHE (it->f)->used == 0)
2661 recompute_basic_faces (it->f);
2663 /* Current value of the `slice', `space-width', and 'height' properties. */
2664 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2665 it->space_width = Qnil;
2666 it->font_height = Qnil;
2667 it->override_ascent = -1;
2669 /* Are control characters displayed as `^C'? */
2670 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2672 /* -1 means everything between a CR and the following line end
2673 is invisible. >0 means lines indented more than this value are
2674 invisible. */
2675 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2676 ? (clip_to_bounds
2677 (-1, XINT (BVAR (current_buffer, selective_display)),
2678 PTRDIFF_MAX))
2679 : (!NILP (BVAR (current_buffer, selective_display))
2680 ? -1 : 0));
2681 it->selective_display_ellipsis_p
2682 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2684 /* Display table to use. */
2685 it->dp = window_display_table (w);
2687 /* Are multibyte characters enabled in current_buffer? */
2688 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2690 /* If visible region is of non-zero length, set IT->region_beg_charpos
2691 and IT->region_end_charpos to the start and end of a visible region
2692 in window IT->w. Set both to -1 to indicate no region. */
2693 markpos = markpos_of_region ();
2694 if (markpos >= 0
2695 /* Maybe highlight only in selected window. */
2696 && (/* Either show region everywhere. */
2697 highlight_nonselected_windows
2698 /* Or show region in the selected window. */
2699 || w == XWINDOW (selected_window)
2700 /* Or show the region if we are in the mini-buffer and W is
2701 the window the mini-buffer refers to. */
2702 || (MINI_WINDOW_P (XWINDOW (selected_window))
2703 && WINDOWP (minibuf_selected_window)
2704 && w == XWINDOW (minibuf_selected_window))))
2706 it->region_beg_charpos = min (PT, markpos);
2707 it->region_end_charpos = max (PT, markpos);
2709 else
2710 it->region_beg_charpos = it->region_end_charpos = -1;
2712 /* Get the position at which the redisplay_end_trigger hook should
2713 be run, if it is to be run at all. */
2714 if (MARKERP (w->redisplay_end_trigger)
2715 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2716 it->redisplay_end_trigger_charpos
2717 = marker_position (w->redisplay_end_trigger);
2718 else if (INTEGERP (w->redisplay_end_trigger))
2719 it->redisplay_end_trigger_charpos =
2720 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2722 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2724 /* Are lines in the display truncated? */
2725 if (base_face_id != DEFAULT_FACE_ID
2726 || it->w->hscroll
2727 || (! WINDOW_FULL_WIDTH_P (it->w)
2728 && ((!NILP (Vtruncate_partial_width_windows)
2729 && !INTEGERP (Vtruncate_partial_width_windows))
2730 || (INTEGERP (Vtruncate_partial_width_windows)
2731 && (WINDOW_TOTAL_COLS (it->w)
2732 < XINT (Vtruncate_partial_width_windows))))))
2733 it->line_wrap = TRUNCATE;
2734 else if (NILP (BVAR (current_buffer, truncate_lines)))
2735 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2736 ? WINDOW_WRAP : WORD_WRAP;
2737 else
2738 it->line_wrap = TRUNCATE;
2740 /* Get dimensions of truncation and continuation glyphs. These are
2741 displayed as fringe bitmaps under X, but we need them for such
2742 frames when the fringes are turned off. But leave the dimensions
2743 zero for tooltip frames, as these glyphs look ugly there and also
2744 sabotage calculations of tooltip dimensions in x-show-tip. */
2745 #ifdef HAVE_WINDOW_SYSTEM
2746 if (!(FRAME_WINDOW_P (it->f)
2747 && FRAMEP (tip_frame)
2748 && it->f == XFRAME (tip_frame)))
2749 #endif
2751 if (it->line_wrap == TRUNCATE)
2753 /* We will need the truncation glyph. */
2754 eassert (it->glyph_row == NULL);
2755 produce_special_glyphs (it, IT_TRUNCATION);
2756 it->truncation_pixel_width = it->pixel_width;
2758 else
2760 /* We will need the continuation glyph. */
2761 eassert (it->glyph_row == NULL);
2762 produce_special_glyphs (it, IT_CONTINUATION);
2763 it->continuation_pixel_width = it->pixel_width;
2767 /* Reset these values to zero because the produce_special_glyphs
2768 above has changed them. */
2769 it->pixel_width = it->ascent = it->descent = 0;
2770 it->phys_ascent = it->phys_descent = 0;
2772 /* Set this after getting the dimensions of truncation and
2773 continuation glyphs, so that we don't produce glyphs when calling
2774 produce_special_glyphs, above. */
2775 it->glyph_row = row;
2776 it->area = TEXT_AREA;
2778 /* Forget any previous info about this row being reversed. */
2779 if (it->glyph_row)
2780 it->glyph_row->reversed_p = 0;
2782 /* Get the dimensions of the display area. The display area
2783 consists of the visible window area plus a horizontally scrolled
2784 part to the left of the window. All x-values are relative to the
2785 start of this total display area. */
2786 if (base_face_id != DEFAULT_FACE_ID)
2788 /* Mode lines, menu bar in terminal frames. */
2789 it->first_visible_x = 0;
2790 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2792 else
2794 it->first_visible_x =
2795 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2796 it->last_visible_x = (it->first_visible_x
2797 + window_box_width (w, TEXT_AREA));
2799 /* If we truncate lines, leave room for the truncation glyph(s) at
2800 the right margin. Otherwise, leave room for the continuation
2801 glyph(s). Done only if the window has no fringes. Since we
2802 don't know at this point whether there will be any R2L lines in
2803 the window, we reserve space for truncation/continuation glyphs
2804 even if only one of the fringes is absent. */
2805 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2806 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2808 if (it->line_wrap == TRUNCATE)
2809 it->last_visible_x -= it->truncation_pixel_width;
2810 else
2811 it->last_visible_x -= it->continuation_pixel_width;
2814 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2815 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2818 /* Leave room for a border glyph. */
2819 if (!FRAME_WINDOW_P (it->f)
2820 && !WINDOW_RIGHTMOST_P (it->w))
2821 it->last_visible_x -= 1;
2823 it->last_visible_y = window_text_bottom_y (w);
2825 /* For mode lines and alike, arrange for the first glyph having a
2826 left box line if the face specifies a box. */
2827 if (base_face_id != DEFAULT_FACE_ID)
2829 struct face *face;
2831 it->face_id = remapped_base_face_id;
2833 /* If we have a boxed mode line, make the first character appear
2834 with a left box line. */
2835 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2836 if (face->box != FACE_NO_BOX)
2837 it->start_of_box_run_p = 1;
2840 /* If a buffer position was specified, set the iterator there,
2841 getting overlays and face properties from that position. */
2842 if (charpos >= BUF_BEG (current_buffer))
2844 it->end_charpos = ZV;
2845 eassert (charpos == BYTE_TO_CHAR (bytepos));
2846 IT_CHARPOS (*it) = charpos;
2847 IT_BYTEPOS (*it) = bytepos;
2849 /* We will rely on `reseat' to set this up properly, via
2850 handle_face_prop. */
2851 it->face_id = it->base_face_id;
2853 it->start = it->current;
2854 /* Do we need to reorder bidirectional text? Not if this is a
2855 unibyte buffer: by definition, none of the single-byte
2856 characters are strong R2L, so no reordering is needed. And
2857 bidi.c doesn't support unibyte buffers anyway. Also, don't
2858 reorder while we are loading loadup.el, since the tables of
2859 character properties needed for reordering are not yet
2860 available. */
2861 it->bidi_p =
2862 NILP (Vpurify_flag)
2863 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2864 && it->multibyte_p;
2866 /* If we are to reorder bidirectional text, init the bidi
2867 iterator. */
2868 if (it->bidi_p)
2870 /* Note the paragraph direction that this buffer wants to
2871 use. */
2872 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2873 Qleft_to_right))
2874 it->paragraph_embedding = L2R;
2875 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2876 Qright_to_left))
2877 it->paragraph_embedding = R2L;
2878 else
2879 it->paragraph_embedding = NEUTRAL_DIR;
2880 bidi_unshelve_cache (NULL, 0);
2881 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2882 &it->bidi_it);
2885 /* Compute faces etc. */
2886 reseat (it, it->current.pos, 1);
2889 CHECK_IT (it);
2893 /* Initialize IT for the display of window W with window start POS. */
2895 void
2896 start_display (struct it *it, struct window *w, struct text_pos pos)
2898 struct glyph_row *row;
2899 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2901 row = w->desired_matrix->rows + first_vpos;
2902 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2903 it->first_vpos = first_vpos;
2905 /* Don't reseat to previous visible line start if current start
2906 position is in a string or image. */
2907 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2909 int start_at_line_beg_p;
2910 int first_y = it->current_y;
2912 /* If window start is not at a line start, skip forward to POS to
2913 get the correct continuation lines width. */
2914 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2915 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2916 if (!start_at_line_beg_p)
2918 int new_x;
2920 reseat_at_previous_visible_line_start (it);
2921 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2923 new_x = it->current_x + it->pixel_width;
2925 /* If lines are continued, this line may end in the middle
2926 of a multi-glyph character (e.g. a control character
2927 displayed as \003, or in the middle of an overlay
2928 string). In this case move_it_to above will not have
2929 taken us to the start of the continuation line but to the
2930 end of the continued line. */
2931 if (it->current_x > 0
2932 && it->line_wrap != TRUNCATE /* Lines are continued. */
2933 && (/* And glyph doesn't fit on the line. */
2934 new_x > it->last_visible_x
2935 /* Or it fits exactly and we're on a window
2936 system frame. */
2937 || (new_x == it->last_visible_x
2938 && FRAME_WINDOW_P (it->f)
2939 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2940 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2941 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2943 if ((it->current.dpvec_index >= 0
2944 || it->current.overlay_string_index >= 0)
2945 /* If we are on a newline from a display vector or
2946 overlay string, then we are already at the end of
2947 a screen line; no need to go to the next line in
2948 that case, as this line is not really continued.
2949 (If we do go to the next line, C-e will not DTRT.) */
2950 && it->c != '\n')
2952 set_iterator_to_next (it, 1);
2953 move_it_in_display_line_to (it, -1, -1, 0);
2956 it->continuation_lines_width += it->current_x;
2958 /* If the character at POS is displayed via a display
2959 vector, move_it_to above stops at the final glyph of
2960 IT->dpvec. To make the caller redisplay that character
2961 again (a.k.a. start at POS), we need to reset the
2962 dpvec_index to the beginning of IT->dpvec. */
2963 else if (it->current.dpvec_index >= 0)
2964 it->current.dpvec_index = 0;
2966 /* We're starting a new display line, not affected by the
2967 height of the continued line, so clear the appropriate
2968 fields in the iterator structure. */
2969 it->max_ascent = it->max_descent = 0;
2970 it->max_phys_ascent = it->max_phys_descent = 0;
2972 it->current_y = first_y;
2973 it->vpos = 0;
2974 it->current_x = it->hpos = 0;
2980 /* Return 1 if POS is a position in ellipses displayed for invisible
2981 text. W is the window we display, for text property lookup. */
2983 static int
2984 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2986 Lisp_Object prop, window;
2987 int ellipses_p = 0;
2988 ptrdiff_t charpos = CHARPOS (pos->pos);
2990 /* If POS specifies a position in a display vector, this might
2991 be for an ellipsis displayed for invisible text. We won't
2992 get the iterator set up for delivering that ellipsis unless
2993 we make sure that it gets aware of the invisible text. */
2994 if (pos->dpvec_index >= 0
2995 && pos->overlay_string_index < 0
2996 && CHARPOS (pos->string_pos) < 0
2997 && charpos > BEGV
2998 && (XSETWINDOW (window, w),
2999 prop = Fget_char_property (make_number (charpos),
3000 Qinvisible, window),
3001 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3003 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3004 window);
3005 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3008 return ellipses_p;
3012 /* Initialize IT for stepping through current_buffer in window W,
3013 starting at position POS that includes overlay string and display
3014 vector/ control character translation position information. Value
3015 is zero if there are overlay strings with newlines at POS. */
3017 static int
3018 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3020 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3021 int i, overlay_strings_with_newlines = 0;
3023 /* If POS specifies a position in a display vector, this might
3024 be for an ellipsis displayed for invisible text. We won't
3025 get the iterator set up for delivering that ellipsis unless
3026 we make sure that it gets aware of the invisible text. */
3027 if (in_ellipses_for_invisible_text_p (pos, w))
3029 --charpos;
3030 bytepos = 0;
3033 /* Keep in mind: the call to reseat in init_iterator skips invisible
3034 text, so we might end up at a position different from POS. This
3035 is only a problem when POS is a row start after a newline and an
3036 overlay starts there with an after-string, and the overlay has an
3037 invisible property. Since we don't skip invisible text in
3038 display_line and elsewhere immediately after consuming the
3039 newline before the row start, such a POS will not be in a string,
3040 but the call to init_iterator below will move us to the
3041 after-string. */
3042 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3044 /* This only scans the current chunk -- it should scan all chunks.
3045 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3046 to 16 in 22.1 to make this a lesser problem. */
3047 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3049 const char *s = SSDATA (it->overlay_strings[i]);
3050 const char *e = s + SBYTES (it->overlay_strings[i]);
3052 while (s < e && *s != '\n')
3053 ++s;
3055 if (s < e)
3057 overlay_strings_with_newlines = 1;
3058 break;
3062 /* If position is within an overlay string, set up IT to the right
3063 overlay string. */
3064 if (pos->overlay_string_index >= 0)
3066 int relative_index;
3068 /* If the first overlay string happens to have a `display'
3069 property for an image, the iterator will be set up for that
3070 image, and we have to undo that setup first before we can
3071 correct the overlay string index. */
3072 if (it->method == GET_FROM_IMAGE)
3073 pop_it (it);
3075 /* We already have the first chunk of overlay strings in
3076 IT->overlay_strings. Load more until the one for
3077 pos->overlay_string_index is in IT->overlay_strings. */
3078 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3080 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3081 it->current.overlay_string_index = 0;
3082 while (n--)
3084 load_overlay_strings (it, 0);
3085 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3089 it->current.overlay_string_index = pos->overlay_string_index;
3090 relative_index = (it->current.overlay_string_index
3091 % OVERLAY_STRING_CHUNK_SIZE);
3092 it->string = it->overlay_strings[relative_index];
3093 eassert (STRINGP (it->string));
3094 it->current.string_pos = pos->string_pos;
3095 it->method = GET_FROM_STRING;
3096 it->end_charpos = SCHARS (it->string);
3097 /* Set up the bidi iterator for this overlay string. */
3098 if (it->bidi_p)
3100 it->bidi_it.string.lstring = it->string;
3101 it->bidi_it.string.s = NULL;
3102 it->bidi_it.string.schars = SCHARS (it->string);
3103 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3104 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3105 it->bidi_it.string.unibyte = !it->multibyte_p;
3106 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3107 FRAME_WINDOW_P (it->f), &it->bidi_it);
3109 /* Synchronize the state of the bidi iterator with
3110 pos->string_pos. For any string position other than
3111 zero, this will be done automagically when we resume
3112 iteration over the string and get_visually_first_element
3113 is called. But if string_pos is zero, and the string is
3114 to be reordered for display, we need to resync manually,
3115 since it could be that the iteration state recorded in
3116 pos ended at string_pos of 0 moving backwards in string. */
3117 if (CHARPOS (pos->string_pos) == 0)
3119 get_visually_first_element (it);
3120 if (IT_STRING_CHARPOS (*it) != 0)
3121 do {
3122 /* Paranoia. */
3123 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3124 bidi_move_to_visually_next (&it->bidi_it);
3125 } while (it->bidi_it.charpos != 0);
3127 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3128 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3132 if (CHARPOS (pos->string_pos) >= 0)
3134 /* Recorded position is not in an overlay string, but in another
3135 string. This can only be a string from a `display' property.
3136 IT should already be filled with that string. */
3137 it->current.string_pos = pos->string_pos;
3138 eassert (STRINGP (it->string));
3139 if (it->bidi_p)
3140 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3141 FRAME_WINDOW_P (it->f), &it->bidi_it);
3144 /* Restore position in display vector translations, control
3145 character translations or ellipses. */
3146 if (pos->dpvec_index >= 0)
3148 if (it->dpvec == NULL)
3149 get_next_display_element (it);
3150 eassert (it->dpvec && it->current.dpvec_index == 0);
3151 it->current.dpvec_index = pos->dpvec_index;
3154 CHECK_IT (it);
3155 return !overlay_strings_with_newlines;
3159 /* Initialize IT for stepping through current_buffer in window W
3160 starting at ROW->start. */
3162 static void
3163 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3165 init_from_display_pos (it, w, &row->start);
3166 it->start = row->start;
3167 it->continuation_lines_width = row->continuation_lines_width;
3168 CHECK_IT (it);
3172 /* Initialize IT for stepping through current_buffer in window W
3173 starting in the line following ROW, i.e. starting at ROW->end.
3174 Value is zero if there are overlay strings with newlines at ROW's
3175 end position. */
3177 static int
3178 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3180 int success = 0;
3182 if (init_from_display_pos (it, w, &row->end))
3184 if (row->continued_p)
3185 it->continuation_lines_width
3186 = row->continuation_lines_width + row->pixel_width;
3187 CHECK_IT (it);
3188 success = 1;
3191 return success;
3197 /***********************************************************************
3198 Text properties
3199 ***********************************************************************/
3201 /* Called when IT reaches IT->stop_charpos. Handle text property and
3202 overlay changes. Set IT->stop_charpos to the next position where
3203 to stop. */
3205 static void
3206 handle_stop (struct it *it)
3208 enum prop_handled handled;
3209 int handle_overlay_change_p;
3210 struct props *p;
3212 it->dpvec = NULL;
3213 it->current.dpvec_index = -1;
3214 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3215 it->ignore_overlay_strings_at_pos_p = 0;
3216 it->ellipsis_p = 0;
3218 /* Use face of preceding text for ellipsis (if invisible) */
3219 if (it->selective_display_ellipsis_p)
3220 it->saved_face_id = it->face_id;
3224 handled = HANDLED_NORMALLY;
3226 /* Call text property handlers. */
3227 for (p = it_props; p->handler; ++p)
3229 handled = p->handler (it);
3231 if (handled == HANDLED_RECOMPUTE_PROPS)
3232 break;
3233 else if (handled == HANDLED_RETURN)
3235 /* We still want to show before and after strings from
3236 overlays even if the actual buffer text is replaced. */
3237 if (!handle_overlay_change_p
3238 || it->sp > 1
3239 /* Don't call get_overlay_strings_1 if we already
3240 have overlay strings loaded, because doing so
3241 will load them again and push the iterator state
3242 onto the stack one more time, which is not
3243 expected by the rest of the code that processes
3244 overlay strings. */
3245 || (it->current.overlay_string_index < 0
3246 ? !get_overlay_strings_1 (it, 0, 0)
3247 : 0))
3249 if (it->ellipsis_p)
3250 setup_for_ellipsis (it, 0);
3251 /* When handling a display spec, we might load an
3252 empty string. In that case, discard it here. We
3253 used to discard it in handle_single_display_spec,
3254 but that causes get_overlay_strings_1, above, to
3255 ignore overlay strings that we must check. */
3256 if (STRINGP (it->string) && !SCHARS (it->string))
3257 pop_it (it);
3258 return;
3260 else if (STRINGP (it->string) && !SCHARS (it->string))
3261 pop_it (it);
3262 else
3264 it->ignore_overlay_strings_at_pos_p = 1;
3265 it->string_from_display_prop_p = 0;
3266 it->from_disp_prop_p = 0;
3267 handle_overlay_change_p = 0;
3269 handled = HANDLED_RECOMPUTE_PROPS;
3270 break;
3272 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3273 handle_overlay_change_p = 0;
3276 if (handled != HANDLED_RECOMPUTE_PROPS)
3278 /* Don't check for overlay strings below when set to deliver
3279 characters from a display vector. */
3280 if (it->method == GET_FROM_DISPLAY_VECTOR)
3281 handle_overlay_change_p = 0;
3283 /* Handle overlay changes.
3284 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3285 if it finds overlays. */
3286 if (handle_overlay_change_p)
3287 handled = handle_overlay_change (it);
3290 if (it->ellipsis_p)
3292 setup_for_ellipsis (it, 0);
3293 break;
3296 while (handled == HANDLED_RECOMPUTE_PROPS);
3298 /* Determine where to stop next. */
3299 if (handled == HANDLED_NORMALLY)
3300 compute_stop_pos (it);
3304 /* Compute IT->stop_charpos from text property and overlay change
3305 information for IT's current position. */
3307 static void
3308 compute_stop_pos (struct it *it)
3310 register INTERVAL iv, next_iv;
3311 Lisp_Object object, limit, position;
3312 ptrdiff_t charpos, bytepos;
3314 if (STRINGP (it->string))
3316 /* Strings are usually short, so don't limit the search for
3317 properties. */
3318 it->stop_charpos = it->end_charpos;
3319 object = it->string;
3320 limit = Qnil;
3321 charpos = IT_STRING_CHARPOS (*it);
3322 bytepos = IT_STRING_BYTEPOS (*it);
3324 else
3326 ptrdiff_t pos;
3328 /* If end_charpos is out of range for some reason, such as a
3329 misbehaving display function, rationalize it (Bug#5984). */
3330 if (it->end_charpos > ZV)
3331 it->end_charpos = ZV;
3332 it->stop_charpos = it->end_charpos;
3334 /* If next overlay change is in front of the current stop pos
3335 (which is IT->end_charpos), stop there. Note: value of
3336 next_overlay_change is point-max if no overlay change
3337 follows. */
3338 charpos = IT_CHARPOS (*it);
3339 bytepos = IT_BYTEPOS (*it);
3340 pos = next_overlay_change (charpos);
3341 if (pos < it->stop_charpos)
3342 it->stop_charpos = pos;
3344 /* If showing the region, we have to stop at the region
3345 start or end because the face might change there. */
3346 if (it->region_beg_charpos > 0)
3348 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3349 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3350 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3351 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3354 /* Set up variables for computing the stop position from text
3355 property changes. */
3356 XSETBUFFER (object, current_buffer);
3357 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3360 /* Get the interval containing IT's position. Value is a null
3361 interval if there isn't such an interval. */
3362 position = make_number (charpos);
3363 iv = validate_interval_range (object, &position, &position, 0);
3364 if (iv)
3366 Lisp_Object values_here[LAST_PROP_IDX];
3367 struct props *p;
3369 /* Get properties here. */
3370 for (p = it_props; p->handler; ++p)
3371 values_here[p->idx] = textget (iv->plist, *p->name);
3373 /* Look for an interval following iv that has different
3374 properties. */
3375 for (next_iv = next_interval (iv);
3376 (next_iv
3377 && (NILP (limit)
3378 || XFASTINT (limit) > next_iv->position));
3379 next_iv = next_interval (next_iv))
3381 for (p = it_props; p->handler; ++p)
3383 Lisp_Object new_value;
3385 new_value = textget (next_iv->plist, *p->name);
3386 if (!EQ (values_here[p->idx], new_value))
3387 break;
3390 if (p->handler)
3391 break;
3394 if (next_iv)
3396 if (INTEGERP (limit)
3397 && next_iv->position >= XFASTINT (limit))
3398 /* No text property change up to limit. */
3399 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3400 else
3401 /* Text properties change in next_iv. */
3402 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3406 if (it->cmp_it.id < 0)
3408 ptrdiff_t stoppos = it->end_charpos;
3410 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3411 stoppos = -1;
3412 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3413 stoppos, it->string);
3416 eassert (STRINGP (it->string)
3417 || (it->stop_charpos >= BEGV
3418 && it->stop_charpos >= IT_CHARPOS (*it)));
3422 /* Return the position of the next overlay change after POS in
3423 current_buffer. Value is point-max if no overlay change
3424 follows. This is like `next-overlay-change' but doesn't use
3425 xmalloc. */
3427 static ptrdiff_t
3428 next_overlay_change (ptrdiff_t pos)
3430 ptrdiff_t i, noverlays;
3431 ptrdiff_t endpos;
3432 Lisp_Object *overlays;
3434 /* Get all overlays at the given position. */
3435 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3437 /* If any of these overlays ends before endpos,
3438 use its ending point instead. */
3439 for (i = 0; i < noverlays; ++i)
3441 Lisp_Object oend;
3442 ptrdiff_t oendpos;
3444 oend = OVERLAY_END (overlays[i]);
3445 oendpos = OVERLAY_POSITION (oend);
3446 endpos = min (endpos, oendpos);
3449 return endpos;
3452 /* How many characters forward to search for a display property or
3453 display string. Searching too far forward makes the bidi display
3454 sluggish, especially in small windows. */
3455 #define MAX_DISP_SCAN 250
3457 /* Return the character position of a display string at or after
3458 position specified by POSITION. If no display string exists at or
3459 after POSITION, return ZV. A display string is either an overlay
3460 with `display' property whose value is a string, or a `display'
3461 text property whose value is a string. STRING is data about the
3462 string to iterate; if STRING->lstring is nil, we are iterating a
3463 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3464 on a GUI frame. DISP_PROP is set to zero if we searched
3465 MAX_DISP_SCAN characters forward without finding any display
3466 strings, non-zero otherwise. It is set to 2 if the display string
3467 uses any kind of `(space ...)' spec that will produce a stretch of
3468 white space in the text area. */
3469 ptrdiff_t
3470 compute_display_string_pos (struct text_pos *position,
3471 struct bidi_string_data *string,
3472 int frame_window_p, int *disp_prop)
3474 /* OBJECT = nil means current buffer. */
3475 Lisp_Object object =
3476 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3477 Lisp_Object pos, spec, limpos;
3478 int string_p = (string && (STRINGP (string->lstring) || string->s));
3479 ptrdiff_t eob = string_p ? string->schars : ZV;
3480 ptrdiff_t begb = string_p ? 0 : BEGV;
3481 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3482 ptrdiff_t lim =
3483 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3484 struct text_pos tpos;
3485 int rv = 0;
3487 *disp_prop = 1;
3489 if (charpos >= eob
3490 /* We don't support display properties whose values are strings
3491 that have display string properties. */
3492 || string->from_disp_str
3493 /* C strings cannot have display properties. */
3494 || (string->s && !STRINGP (object)))
3496 *disp_prop = 0;
3497 return eob;
3500 /* If the character at CHARPOS is where the display string begins,
3501 return CHARPOS. */
3502 pos = make_number (charpos);
3503 if (STRINGP (object))
3504 bufpos = string->bufpos;
3505 else
3506 bufpos = charpos;
3507 tpos = *position;
3508 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3509 && (charpos <= begb
3510 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3511 object),
3512 spec))
3513 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3514 frame_window_p)))
3516 if (rv == 2)
3517 *disp_prop = 2;
3518 return charpos;
3521 /* Look forward for the first character with a `display' property
3522 that will replace the underlying text when displayed. */
3523 limpos = make_number (lim);
3524 do {
3525 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3526 CHARPOS (tpos) = XFASTINT (pos);
3527 if (CHARPOS (tpos) >= lim)
3529 *disp_prop = 0;
3530 break;
3532 if (STRINGP (object))
3533 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3534 else
3535 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3536 spec = Fget_char_property (pos, Qdisplay, object);
3537 if (!STRINGP (object))
3538 bufpos = CHARPOS (tpos);
3539 } while (NILP (spec)
3540 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3541 bufpos, frame_window_p)));
3542 if (rv == 2)
3543 *disp_prop = 2;
3545 return CHARPOS (tpos);
3548 /* Return the character position of the end of the display string that
3549 started at CHARPOS. If there's no display string at CHARPOS,
3550 return -1. A display string is either an overlay with `display'
3551 property whose value is a string or a `display' text property whose
3552 value is a string. */
3553 ptrdiff_t
3554 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3556 /* OBJECT = nil means current buffer. */
3557 Lisp_Object object =
3558 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3559 Lisp_Object pos = make_number (charpos);
3560 ptrdiff_t eob =
3561 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3563 if (charpos >= eob || (string->s && !STRINGP (object)))
3564 return eob;
3566 /* It could happen that the display property or overlay was removed
3567 since we found it in compute_display_string_pos above. One way
3568 this can happen is if JIT font-lock was called (through
3569 handle_fontified_prop), and jit-lock-functions remove text
3570 properties or overlays from the portion of buffer that includes
3571 CHARPOS. Muse mode is known to do that, for example. In this
3572 case, we return -1 to the caller, to signal that no display
3573 string is actually present at CHARPOS. See bidi_fetch_char for
3574 how this is handled.
3576 An alternative would be to never look for display properties past
3577 it->stop_charpos. But neither compute_display_string_pos nor
3578 bidi_fetch_char that calls it know or care where the next
3579 stop_charpos is. */
3580 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3581 return -1;
3583 /* Look forward for the first character where the `display' property
3584 changes. */
3585 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3587 return XFASTINT (pos);
3592 /***********************************************************************
3593 Fontification
3594 ***********************************************************************/
3596 /* Handle changes in the `fontified' property of the current buffer by
3597 calling hook functions from Qfontification_functions to fontify
3598 regions of text. */
3600 static enum prop_handled
3601 handle_fontified_prop (struct it *it)
3603 Lisp_Object prop, pos;
3604 enum prop_handled handled = HANDLED_NORMALLY;
3606 if (!NILP (Vmemory_full))
3607 return handled;
3609 /* Get the value of the `fontified' property at IT's current buffer
3610 position. (The `fontified' property doesn't have a special
3611 meaning in strings.) If the value is nil, call functions from
3612 Qfontification_functions. */
3613 if (!STRINGP (it->string)
3614 && it->s == NULL
3615 && !NILP (Vfontification_functions)
3616 && !NILP (Vrun_hooks)
3617 && (pos = make_number (IT_CHARPOS (*it)),
3618 prop = Fget_char_property (pos, Qfontified, Qnil),
3619 /* Ignore the special cased nil value always present at EOB since
3620 no amount of fontifying will be able to change it. */
3621 NILP (prop) && IT_CHARPOS (*it) < Z))
3623 ptrdiff_t count = SPECPDL_INDEX ();
3624 Lisp_Object val;
3625 struct buffer *obuf = current_buffer;
3626 int begv = BEGV, zv = ZV;
3627 int old_clip_changed = current_buffer->clip_changed;
3629 val = Vfontification_functions;
3630 specbind (Qfontification_functions, Qnil);
3632 eassert (it->end_charpos == ZV);
3634 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3635 safe_call1 (val, pos);
3636 else
3638 Lisp_Object fns, fn;
3639 struct gcpro gcpro1, gcpro2;
3641 fns = Qnil;
3642 GCPRO2 (val, fns);
3644 for (; CONSP (val); val = XCDR (val))
3646 fn = XCAR (val);
3648 if (EQ (fn, Qt))
3650 /* A value of t indicates this hook has a local
3651 binding; it means to run the global binding too.
3652 In a global value, t should not occur. If it
3653 does, we must ignore it to avoid an endless
3654 loop. */
3655 for (fns = Fdefault_value (Qfontification_functions);
3656 CONSP (fns);
3657 fns = XCDR (fns))
3659 fn = XCAR (fns);
3660 if (!EQ (fn, Qt))
3661 safe_call1 (fn, pos);
3664 else
3665 safe_call1 (fn, pos);
3668 UNGCPRO;
3671 unbind_to (count, Qnil);
3673 /* Fontification functions routinely call `save-restriction'.
3674 Normally, this tags clip_changed, which can confuse redisplay
3675 (see discussion in Bug#6671). Since we don't perform any
3676 special handling of fontification changes in the case where
3677 `save-restriction' isn't called, there's no point doing so in
3678 this case either. So, if the buffer's restrictions are
3679 actually left unchanged, reset clip_changed. */
3680 if (obuf == current_buffer)
3682 if (begv == BEGV && zv == ZV)
3683 current_buffer->clip_changed = old_clip_changed;
3685 /* There isn't much we can reasonably do to protect against
3686 misbehaving fontification, but here's a fig leaf. */
3687 else if (BUFFER_LIVE_P (obuf))
3688 set_buffer_internal_1 (obuf);
3690 /* The fontification code may have added/removed text.
3691 It could do even a lot worse, but let's at least protect against
3692 the most obvious case where only the text past `pos' gets changed',
3693 as is/was done in grep.el where some escapes sequences are turned
3694 into face properties (bug#7876). */
3695 it->end_charpos = ZV;
3697 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3698 something. This avoids an endless loop if they failed to
3699 fontify the text for which reason ever. */
3700 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3701 handled = HANDLED_RECOMPUTE_PROPS;
3704 return handled;
3709 /***********************************************************************
3710 Faces
3711 ***********************************************************************/
3713 /* Set up iterator IT from face properties at its current position.
3714 Called from handle_stop. */
3716 static enum prop_handled
3717 handle_face_prop (struct it *it)
3719 int new_face_id;
3720 ptrdiff_t next_stop;
3722 if (!STRINGP (it->string))
3724 new_face_id
3725 = face_at_buffer_position (it->w,
3726 IT_CHARPOS (*it),
3727 it->region_beg_charpos,
3728 it->region_end_charpos,
3729 &next_stop,
3730 (IT_CHARPOS (*it)
3731 + TEXT_PROP_DISTANCE_LIMIT),
3732 0, it->base_face_id);
3734 /* Is this a start of a run of characters with box face?
3735 Caveat: this can be called for a freshly initialized
3736 iterator; face_id is -1 in this case. We know that the new
3737 face will not change until limit, i.e. if the new face has a
3738 box, all characters up to limit will have one. But, as
3739 usual, we don't know whether limit is really the end. */
3740 if (new_face_id != it->face_id)
3742 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3743 /* If it->face_id is -1, old_face below will be NULL, see
3744 the definition of FACE_FROM_ID. This will happen if this
3745 is the initial call that gets the face. */
3746 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3748 /* If the value of face_id of the iterator is -1, we have to
3749 look in front of IT's position and see whether there is a
3750 face there that's different from new_face_id. */
3751 if (!old_face && IT_CHARPOS (*it) > BEG)
3753 int prev_face_id = face_before_it_pos (it);
3755 old_face = FACE_FROM_ID (it->f, prev_face_id);
3758 /* If the new face has a box, but the old face does not,
3759 this is the start of a run of characters with box face,
3760 i.e. this character has a shadow on the left side. */
3761 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3762 && (old_face == NULL || !old_face->box));
3763 it->face_box_p = new_face->box != FACE_NO_BOX;
3766 else
3768 int base_face_id;
3769 ptrdiff_t bufpos;
3770 int i;
3771 Lisp_Object from_overlay
3772 = (it->current.overlay_string_index >= 0
3773 ? it->string_overlays[it->current.overlay_string_index
3774 % OVERLAY_STRING_CHUNK_SIZE]
3775 : Qnil);
3777 /* See if we got to this string directly or indirectly from
3778 an overlay property. That includes the before-string or
3779 after-string of an overlay, strings in display properties
3780 provided by an overlay, their text properties, etc.
3782 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3783 if (! NILP (from_overlay))
3784 for (i = it->sp - 1; i >= 0; i--)
3786 if (it->stack[i].current.overlay_string_index >= 0)
3787 from_overlay
3788 = it->string_overlays[it->stack[i].current.overlay_string_index
3789 % OVERLAY_STRING_CHUNK_SIZE];
3790 else if (! NILP (it->stack[i].from_overlay))
3791 from_overlay = it->stack[i].from_overlay;
3793 if (!NILP (from_overlay))
3794 break;
3797 if (! NILP (from_overlay))
3799 bufpos = IT_CHARPOS (*it);
3800 /* For a string from an overlay, the base face depends
3801 only on text properties and ignores overlays. */
3802 base_face_id
3803 = face_for_overlay_string (it->w,
3804 IT_CHARPOS (*it),
3805 it->region_beg_charpos,
3806 it->region_end_charpos,
3807 &next_stop,
3808 (IT_CHARPOS (*it)
3809 + TEXT_PROP_DISTANCE_LIMIT),
3811 from_overlay);
3813 else
3815 bufpos = 0;
3817 /* For strings from a `display' property, use the face at
3818 IT's current buffer position as the base face to merge
3819 with, so that overlay strings appear in the same face as
3820 surrounding text, unless they specify their own
3821 faces. */
3822 base_face_id = it->string_from_prefix_prop_p
3823 ? DEFAULT_FACE_ID
3824 : underlying_face_id (it);
3827 new_face_id = face_at_string_position (it->w,
3828 it->string,
3829 IT_STRING_CHARPOS (*it),
3830 bufpos,
3831 it->region_beg_charpos,
3832 it->region_end_charpos,
3833 &next_stop,
3834 base_face_id, 0);
3836 /* Is this a start of a run of characters with box? Caveat:
3837 this can be called for a freshly allocated iterator; face_id
3838 is -1 is this case. We know that the new face will not
3839 change until the next check pos, i.e. if the new face has a
3840 box, all characters up to that position will have a
3841 box. But, as usual, we don't know whether that position
3842 is really the end. */
3843 if (new_face_id != it->face_id)
3845 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3846 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3848 /* If new face has a box but old face hasn't, this is the
3849 start of a run of characters with box, i.e. it has a
3850 shadow on the left side. */
3851 it->start_of_box_run_p
3852 = new_face->box && (old_face == NULL || !old_face->box);
3853 it->face_box_p = new_face->box != FACE_NO_BOX;
3857 it->face_id = new_face_id;
3858 return HANDLED_NORMALLY;
3862 /* Return the ID of the face ``underlying'' IT's current position,
3863 which is in a string. If the iterator is associated with a
3864 buffer, return the face at IT's current buffer position.
3865 Otherwise, use the iterator's base_face_id. */
3867 static int
3868 underlying_face_id (struct it *it)
3870 int face_id = it->base_face_id, i;
3872 eassert (STRINGP (it->string));
3874 for (i = it->sp - 1; i >= 0; --i)
3875 if (NILP (it->stack[i].string))
3876 face_id = it->stack[i].face_id;
3878 return face_id;
3882 /* Compute the face one character before or after the current position
3883 of IT, in the visual order. BEFORE_P non-zero means get the face
3884 in front (to the left in L2R paragraphs, to the right in R2L
3885 paragraphs) of IT's screen position. Value is the ID of the face. */
3887 static int
3888 face_before_or_after_it_pos (struct it *it, int before_p)
3890 int face_id, limit;
3891 ptrdiff_t next_check_charpos;
3892 struct it it_copy;
3893 void *it_copy_data = NULL;
3895 eassert (it->s == NULL);
3897 if (STRINGP (it->string))
3899 ptrdiff_t bufpos, charpos;
3900 int base_face_id;
3902 /* No face change past the end of the string (for the case
3903 we are padding with spaces). No face change before the
3904 string start. */
3905 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3906 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3907 return it->face_id;
3909 if (!it->bidi_p)
3911 /* Set charpos to the position before or after IT's current
3912 position, in the logical order, which in the non-bidi
3913 case is the same as the visual order. */
3914 if (before_p)
3915 charpos = IT_STRING_CHARPOS (*it) - 1;
3916 else if (it->what == IT_COMPOSITION)
3917 /* For composition, we must check the character after the
3918 composition. */
3919 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3920 else
3921 charpos = IT_STRING_CHARPOS (*it) + 1;
3923 else
3925 if (before_p)
3927 /* With bidi iteration, the character before the current
3928 in the visual order cannot be found by simple
3929 iteration, because "reverse" reordering is not
3930 supported. Instead, we need to use the move_it_*
3931 family of functions. */
3932 /* Ignore face changes before the first visible
3933 character on this display line. */
3934 if (it->current_x <= it->first_visible_x)
3935 return it->face_id;
3936 SAVE_IT (it_copy, *it, it_copy_data);
3937 /* Implementation note: Since move_it_in_display_line
3938 works in the iterator geometry, and thinks the first
3939 character is always the leftmost, even in R2L lines,
3940 we don't need to distinguish between the R2L and L2R
3941 cases here. */
3942 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3943 it_copy.current_x - 1, MOVE_TO_X);
3944 charpos = IT_STRING_CHARPOS (it_copy);
3945 RESTORE_IT (it, it, it_copy_data);
3947 else
3949 /* Set charpos to the string position of the character
3950 that comes after IT's current position in the visual
3951 order. */
3952 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3954 it_copy = *it;
3955 while (n--)
3956 bidi_move_to_visually_next (&it_copy.bidi_it);
3958 charpos = it_copy.bidi_it.charpos;
3961 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3963 if (it->current.overlay_string_index >= 0)
3964 bufpos = IT_CHARPOS (*it);
3965 else
3966 bufpos = 0;
3968 base_face_id = underlying_face_id (it);
3970 /* Get the face for ASCII, or unibyte. */
3971 face_id = face_at_string_position (it->w,
3972 it->string,
3973 charpos,
3974 bufpos,
3975 it->region_beg_charpos,
3976 it->region_end_charpos,
3977 &next_check_charpos,
3978 base_face_id, 0);
3980 /* Correct the face for charsets different from ASCII. Do it
3981 for the multibyte case only. The face returned above is
3982 suitable for unibyte text if IT->string is unibyte. */
3983 if (STRING_MULTIBYTE (it->string))
3985 struct text_pos pos1 = string_pos (charpos, it->string);
3986 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3987 int c, len;
3988 struct face *face = FACE_FROM_ID (it->f, face_id);
3990 c = string_char_and_length (p, &len);
3991 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3994 else
3996 struct text_pos pos;
3998 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3999 || (IT_CHARPOS (*it) <= BEGV && before_p))
4000 return it->face_id;
4002 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4003 pos = it->current.pos;
4005 if (!it->bidi_p)
4007 if (before_p)
4008 DEC_TEXT_POS (pos, it->multibyte_p);
4009 else
4011 if (it->what == IT_COMPOSITION)
4013 /* For composition, we must check the position after
4014 the composition. */
4015 pos.charpos += it->cmp_it.nchars;
4016 pos.bytepos += it->len;
4018 else
4019 INC_TEXT_POS (pos, it->multibyte_p);
4022 else
4024 if (before_p)
4026 /* With bidi iteration, the character before the current
4027 in the visual order cannot be found by simple
4028 iteration, because "reverse" reordering is not
4029 supported. Instead, we need to use the move_it_*
4030 family of functions. */
4031 /* Ignore face changes before the first visible
4032 character on this display line. */
4033 if (it->current_x <= it->first_visible_x)
4034 return it->face_id;
4035 SAVE_IT (it_copy, *it, it_copy_data);
4036 /* Implementation note: Since move_it_in_display_line
4037 works in the iterator geometry, and thinks the first
4038 character is always the leftmost, even in R2L lines,
4039 we don't need to distinguish between the R2L and L2R
4040 cases here. */
4041 move_it_in_display_line (&it_copy, ZV,
4042 it_copy.current_x - 1, MOVE_TO_X);
4043 pos = it_copy.current.pos;
4044 RESTORE_IT (it, it, it_copy_data);
4046 else
4048 /* Set charpos to the buffer position of the character
4049 that comes after IT's current position in the visual
4050 order. */
4051 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4053 it_copy = *it;
4054 while (n--)
4055 bidi_move_to_visually_next (&it_copy.bidi_it);
4057 SET_TEXT_POS (pos,
4058 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4061 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4063 /* Determine face for CHARSET_ASCII, or unibyte. */
4064 face_id = face_at_buffer_position (it->w,
4065 CHARPOS (pos),
4066 it->region_beg_charpos,
4067 it->region_end_charpos,
4068 &next_check_charpos,
4069 limit, 0, -1);
4071 /* Correct the face for charsets different from ASCII. Do it
4072 for the multibyte case only. The face returned above is
4073 suitable for unibyte text if current_buffer is unibyte. */
4074 if (it->multibyte_p)
4076 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4077 struct face *face = FACE_FROM_ID (it->f, face_id);
4078 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4082 return face_id;
4087 /***********************************************************************
4088 Invisible text
4089 ***********************************************************************/
4091 /* Set up iterator IT from invisible properties at its current
4092 position. Called from handle_stop. */
4094 static enum prop_handled
4095 handle_invisible_prop (struct it *it)
4097 enum prop_handled handled = HANDLED_NORMALLY;
4098 int invis_p;
4099 Lisp_Object prop;
4101 if (STRINGP (it->string))
4103 Lisp_Object end_charpos, limit, charpos;
4105 /* Get the value of the invisible text property at the
4106 current position. Value will be nil if there is no such
4107 property. */
4108 charpos = make_number (IT_STRING_CHARPOS (*it));
4109 prop = Fget_text_property (charpos, Qinvisible, it->string);
4110 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4112 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4114 /* Record whether we have to display an ellipsis for the
4115 invisible text. */
4116 int display_ellipsis_p = (invis_p == 2);
4117 ptrdiff_t len, endpos;
4119 handled = HANDLED_RECOMPUTE_PROPS;
4121 /* Get the position at which the next visible text can be
4122 found in IT->string, if any. */
4123 endpos = len = SCHARS (it->string);
4124 XSETINT (limit, len);
4127 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4128 it->string, limit);
4129 if (INTEGERP (end_charpos))
4131 endpos = XFASTINT (end_charpos);
4132 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4133 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4134 if (invis_p == 2)
4135 display_ellipsis_p = 1;
4138 while (invis_p && endpos < len);
4140 if (display_ellipsis_p)
4141 it->ellipsis_p = 1;
4143 if (endpos < len)
4145 /* Text at END_CHARPOS is visible. Move IT there. */
4146 struct text_pos old;
4147 ptrdiff_t oldpos;
4149 old = it->current.string_pos;
4150 oldpos = CHARPOS (old);
4151 if (it->bidi_p)
4153 if (it->bidi_it.first_elt
4154 && it->bidi_it.charpos < SCHARS (it->string))
4155 bidi_paragraph_init (it->paragraph_embedding,
4156 &it->bidi_it, 1);
4157 /* Bidi-iterate out of the invisible text. */
4160 bidi_move_to_visually_next (&it->bidi_it);
4162 while (oldpos <= it->bidi_it.charpos
4163 && it->bidi_it.charpos < endpos);
4165 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4166 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4167 if (IT_CHARPOS (*it) >= endpos)
4168 it->prev_stop = endpos;
4170 else
4172 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4173 compute_string_pos (&it->current.string_pos, old, it->string);
4176 else
4178 /* The rest of the string is invisible. If this is an
4179 overlay string, proceed with the next overlay string
4180 or whatever comes and return a character from there. */
4181 if (it->current.overlay_string_index >= 0
4182 && !display_ellipsis_p)
4184 next_overlay_string (it);
4185 /* Don't check for overlay strings when we just
4186 finished processing them. */
4187 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4189 else
4191 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4192 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4197 else
4199 ptrdiff_t newpos, next_stop, start_charpos, tem;
4200 Lisp_Object pos, overlay;
4202 /* First of all, is there invisible text at this position? */
4203 tem = start_charpos = IT_CHARPOS (*it);
4204 pos = make_number (tem);
4205 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4206 &overlay);
4207 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4209 /* If we are on invisible text, skip over it. */
4210 if (invis_p && start_charpos < it->end_charpos)
4212 /* Record whether we have to display an ellipsis for the
4213 invisible text. */
4214 int display_ellipsis_p = invis_p == 2;
4216 handled = HANDLED_RECOMPUTE_PROPS;
4218 /* Loop skipping over invisible text. The loop is left at
4219 ZV or with IT on the first char being visible again. */
4222 /* Try to skip some invisible text. Return value is the
4223 position reached which can be equal to where we start
4224 if there is nothing invisible there. This skips both
4225 over invisible text properties and overlays with
4226 invisible property. */
4227 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4229 /* If we skipped nothing at all we weren't at invisible
4230 text in the first place. If everything to the end of
4231 the buffer was skipped, end the loop. */
4232 if (newpos == tem || newpos >= ZV)
4233 invis_p = 0;
4234 else
4236 /* We skipped some characters but not necessarily
4237 all there are. Check if we ended up on visible
4238 text. Fget_char_property returns the property of
4239 the char before the given position, i.e. if we
4240 get invis_p = 0, this means that the char at
4241 newpos is visible. */
4242 pos = make_number (newpos);
4243 prop = Fget_char_property (pos, Qinvisible, it->window);
4244 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4247 /* If we ended up on invisible text, proceed to
4248 skip starting with next_stop. */
4249 if (invis_p)
4250 tem = next_stop;
4252 /* If there are adjacent invisible texts, don't lose the
4253 second one's ellipsis. */
4254 if (invis_p == 2)
4255 display_ellipsis_p = 1;
4257 while (invis_p);
4259 /* The position newpos is now either ZV or on visible text. */
4260 if (it->bidi_p)
4262 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4263 int on_newline =
4264 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4265 int after_newline =
4266 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4268 /* If the invisible text ends on a newline or on a
4269 character after a newline, we can avoid the costly,
4270 character by character, bidi iteration to NEWPOS, and
4271 instead simply reseat the iterator there. That's
4272 because all bidi reordering information is tossed at
4273 the newline. This is a big win for modes that hide
4274 complete lines, like Outline, Org, etc. */
4275 if (on_newline || after_newline)
4277 struct text_pos tpos;
4278 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4280 SET_TEXT_POS (tpos, newpos, bpos);
4281 reseat_1 (it, tpos, 0);
4282 /* If we reseat on a newline/ZV, we need to prep the
4283 bidi iterator for advancing to the next character
4284 after the newline/EOB, keeping the current paragraph
4285 direction (so that PRODUCE_GLYPHS does TRT wrt
4286 prepending/appending glyphs to a glyph row). */
4287 if (on_newline)
4289 it->bidi_it.first_elt = 0;
4290 it->bidi_it.paragraph_dir = pdir;
4291 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4292 it->bidi_it.nchars = 1;
4293 it->bidi_it.ch_len = 1;
4296 else /* Must use the slow method. */
4298 /* With bidi iteration, the region of invisible text
4299 could start and/or end in the middle of a
4300 non-base embedding level. Therefore, we need to
4301 skip invisible text using the bidi iterator,
4302 starting at IT's current position, until we find
4303 ourselves outside of the invisible text.
4304 Skipping invisible text _after_ bidi iteration
4305 avoids affecting the visual order of the
4306 displayed text when invisible properties are
4307 added or removed. */
4308 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4310 /* If we were `reseat'ed to a new paragraph,
4311 determine the paragraph base direction. We
4312 need to do it now because
4313 next_element_from_buffer may not have a
4314 chance to do it, if we are going to skip any
4315 text at the beginning, which resets the
4316 FIRST_ELT flag. */
4317 bidi_paragraph_init (it->paragraph_embedding,
4318 &it->bidi_it, 1);
4322 bidi_move_to_visually_next (&it->bidi_it);
4324 while (it->stop_charpos <= it->bidi_it.charpos
4325 && it->bidi_it.charpos < newpos);
4326 IT_CHARPOS (*it) = it->bidi_it.charpos;
4327 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4328 /* If we overstepped NEWPOS, record its position in
4329 the iterator, so that we skip invisible text if
4330 later the bidi iteration lands us in the
4331 invisible region again. */
4332 if (IT_CHARPOS (*it) >= newpos)
4333 it->prev_stop = newpos;
4336 else
4338 IT_CHARPOS (*it) = newpos;
4339 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4342 /* If there are before-strings at the start of invisible
4343 text, and the text is invisible because of a text
4344 property, arrange to show before-strings because 20.x did
4345 it that way. (If the text is invisible because of an
4346 overlay property instead of a text property, this is
4347 already handled in the overlay code.) */
4348 if (NILP (overlay)
4349 && get_overlay_strings (it, it->stop_charpos))
4351 handled = HANDLED_RECOMPUTE_PROPS;
4352 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4354 else if (display_ellipsis_p)
4356 /* Make sure that the glyphs of the ellipsis will get
4357 correct `charpos' values. If we would not update
4358 it->position here, the glyphs would belong to the
4359 last visible character _before_ the invisible
4360 text, which confuses `set_cursor_from_row'.
4362 We use the last invisible position instead of the
4363 first because this way the cursor is always drawn on
4364 the first "." of the ellipsis, whenever PT is inside
4365 the invisible text. Otherwise the cursor would be
4366 placed _after_ the ellipsis when the point is after the
4367 first invisible character. */
4368 if (!STRINGP (it->object))
4370 it->position.charpos = newpos - 1;
4371 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4373 it->ellipsis_p = 1;
4374 /* Let the ellipsis display before
4375 considering any properties of the following char.
4376 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4377 handled = HANDLED_RETURN;
4382 return handled;
4386 /* Make iterator IT return `...' next.
4387 Replaces LEN characters from buffer. */
4389 static void
4390 setup_for_ellipsis (struct it *it, int len)
4392 /* Use the display table definition for `...'. Invalid glyphs
4393 will be handled by the method returning elements from dpvec. */
4394 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4396 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4397 it->dpvec = v->contents;
4398 it->dpend = v->contents + v->header.size;
4400 else
4402 /* Default `...'. */
4403 it->dpvec = default_invis_vector;
4404 it->dpend = default_invis_vector + 3;
4407 it->dpvec_char_len = len;
4408 it->current.dpvec_index = 0;
4409 it->dpvec_face_id = -1;
4411 /* Remember the current face id in case glyphs specify faces.
4412 IT's face is restored in set_iterator_to_next.
4413 saved_face_id was set to preceding char's face in handle_stop. */
4414 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4415 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4417 it->method = GET_FROM_DISPLAY_VECTOR;
4418 it->ellipsis_p = 1;
4423 /***********************************************************************
4424 'display' property
4425 ***********************************************************************/
4427 /* Set up iterator IT from `display' property at its current position.
4428 Called from handle_stop.
4429 We return HANDLED_RETURN if some part of the display property
4430 overrides the display of the buffer text itself.
4431 Otherwise we return HANDLED_NORMALLY. */
4433 static enum prop_handled
4434 handle_display_prop (struct it *it)
4436 Lisp_Object propval, object, overlay;
4437 struct text_pos *position;
4438 ptrdiff_t bufpos;
4439 /* Nonzero if some property replaces the display of the text itself. */
4440 int display_replaced_p = 0;
4442 if (STRINGP (it->string))
4444 object = it->string;
4445 position = &it->current.string_pos;
4446 bufpos = CHARPOS (it->current.pos);
4448 else
4450 XSETWINDOW (object, it->w);
4451 position = &it->current.pos;
4452 bufpos = CHARPOS (*position);
4455 /* Reset those iterator values set from display property values. */
4456 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4457 it->space_width = Qnil;
4458 it->font_height = Qnil;
4459 it->voffset = 0;
4461 /* We don't support recursive `display' properties, i.e. string
4462 values that have a string `display' property, that have a string
4463 `display' property etc. */
4464 if (!it->string_from_display_prop_p)
4465 it->area = TEXT_AREA;
4467 propval = get_char_property_and_overlay (make_number (position->charpos),
4468 Qdisplay, object, &overlay);
4469 if (NILP (propval))
4470 return HANDLED_NORMALLY;
4471 /* Now OVERLAY is the overlay that gave us this property, or nil
4472 if it was a text property. */
4474 if (!STRINGP (it->string))
4475 object = it->w->contents;
4477 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4478 position, bufpos,
4479 FRAME_WINDOW_P (it->f));
4481 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4484 /* Subroutine of handle_display_prop. Returns non-zero if the display
4485 specification in SPEC is a replacing specification, i.e. it would
4486 replace the text covered by `display' property with something else,
4487 such as an image or a display string. If SPEC includes any kind or
4488 `(space ...) specification, the value is 2; this is used by
4489 compute_display_string_pos, which see.
4491 See handle_single_display_spec for documentation of arguments.
4492 frame_window_p is non-zero if the window being redisplayed is on a
4493 GUI frame; this argument is used only if IT is NULL, see below.
4495 IT can be NULL, if this is called by the bidi reordering code
4496 through compute_display_string_pos, which see. In that case, this
4497 function only examines SPEC, but does not otherwise "handle" it, in
4498 the sense that it doesn't set up members of IT from the display
4499 spec. */
4500 static int
4501 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4502 Lisp_Object overlay, struct text_pos *position,
4503 ptrdiff_t bufpos, int frame_window_p)
4505 int replacing_p = 0;
4506 int rv;
4508 if (CONSP (spec)
4509 /* Simple specifications. */
4510 && !EQ (XCAR (spec), Qimage)
4511 && !EQ (XCAR (spec), Qspace)
4512 && !EQ (XCAR (spec), Qwhen)
4513 && !EQ (XCAR (spec), Qslice)
4514 && !EQ (XCAR (spec), Qspace_width)
4515 && !EQ (XCAR (spec), Qheight)
4516 && !EQ (XCAR (spec), Qraise)
4517 /* Marginal area specifications. */
4518 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4519 && !EQ (XCAR (spec), Qleft_fringe)
4520 && !EQ (XCAR (spec), Qright_fringe)
4521 && !NILP (XCAR (spec)))
4523 for (; CONSP (spec); spec = XCDR (spec))
4525 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4526 overlay, position, bufpos,
4527 replacing_p, frame_window_p)))
4529 replacing_p = rv;
4530 /* If some text in a string is replaced, `position' no
4531 longer points to the position of `object'. */
4532 if (!it || STRINGP (object))
4533 break;
4537 else if (VECTORP (spec))
4539 ptrdiff_t i;
4540 for (i = 0; i < ASIZE (spec); ++i)
4541 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4542 overlay, position, bufpos,
4543 replacing_p, frame_window_p)))
4545 replacing_p = rv;
4546 /* If some text in a string is replaced, `position' no
4547 longer points to the position of `object'. */
4548 if (!it || STRINGP (object))
4549 break;
4552 else
4554 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4555 position, bufpos, 0,
4556 frame_window_p)))
4557 replacing_p = rv;
4560 return replacing_p;
4563 /* Value is the position of the end of the `display' property starting
4564 at START_POS in OBJECT. */
4566 static struct text_pos
4567 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4569 Lisp_Object end;
4570 struct text_pos end_pos;
4572 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4573 Qdisplay, object, Qnil);
4574 CHARPOS (end_pos) = XFASTINT (end);
4575 if (STRINGP (object))
4576 compute_string_pos (&end_pos, start_pos, it->string);
4577 else
4578 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4580 return end_pos;
4584 /* Set up IT from a single `display' property specification SPEC. OBJECT
4585 is the object in which the `display' property was found. *POSITION
4586 is the position in OBJECT at which the `display' property was found.
4587 BUFPOS is the buffer position of OBJECT (different from POSITION if
4588 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4589 previously saw a display specification which already replaced text
4590 display with something else, for example an image; we ignore such
4591 properties after the first one has been processed.
4593 OVERLAY is the overlay this `display' property came from,
4594 or nil if it was a text property.
4596 If SPEC is a `space' or `image' specification, and in some other
4597 cases too, set *POSITION to the position where the `display'
4598 property ends.
4600 If IT is NULL, only examine the property specification in SPEC, but
4601 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4602 is intended to be displayed in a window on a GUI frame.
4604 Value is non-zero if something was found which replaces the display
4605 of buffer or string text. */
4607 static int
4608 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4609 Lisp_Object overlay, struct text_pos *position,
4610 ptrdiff_t bufpos, int display_replaced_p,
4611 int frame_window_p)
4613 Lisp_Object form;
4614 Lisp_Object location, value;
4615 struct text_pos start_pos = *position;
4616 int valid_p;
4618 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4619 If the result is non-nil, use VALUE instead of SPEC. */
4620 form = Qt;
4621 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4623 spec = XCDR (spec);
4624 if (!CONSP (spec))
4625 return 0;
4626 form = XCAR (spec);
4627 spec = XCDR (spec);
4630 if (!NILP (form) && !EQ (form, Qt))
4632 ptrdiff_t count = SPECPDL_INDEX ();
4633 struct gcpro gcpro1;
4635 /* Bind `object' to the object having the `display' property, a
4636 buffer or string. Bind `position' to the position in the
4637 object where the property was found, and `buffer-position'
4638 to the current position in the buffer. */
4640 if (NILP (object))
4641 XSETBUFFER (object, current_buffer);
4642 specbind (Qobject, object);
4643 specbind (Qposition, make_number (CHARPOS (*position)));
4644 specbind (Qbuffer_position, make_number (bufpos));
4645 GCPRO1 (form);
4646 form = safe_eval (form);
4647 UNGCPRO;
4648 unbind_to (count, Qnil);
4651 if (NILP (form))
4652 return 0;
4654 /* Handle `(height HEIGHT)' specifications. */
4655 if (CONSP (spec)
4656 && EQ (XCAR (spec), Qheight)
4657 && CONSP (XCDR (spec)))
4659 if (it)
4661 if (!FRAME_WINDOW_P (it->f))
4662 return 0;
4664 it->font_height = XCAR (XCDR (spec));
4665 if (!NILP (it->font_height))
4667 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4668 int new_height = -1;
4670 if (CONSP (it->font_height)
4671 && (EQ (XCAR (it->font_height), Qplus)
4672 || EQ (XCAR (it->font_height), Qminus))
4673 && CONSP (XCDR (it->font_height))
4674 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4676 /* `(+ N)' or `(- N)' where N is an integer. */
4677 int steps = XINT (XCAR (XCDR (it->font_height)));
4678 if (EQ (XCAR (it->font_height), Qplus))
4679 steps = - steps;
4680 it->face_id = smaller_face (it->f, it->face_id, steps);
4682 else if (FUNCTIONP (it->font_height))
4684 /* Call function with current height as argument.
4685 Value is the new height. */
4686 Lisp_Object height;
4687 height = safe_call1 (it->font_height,
4688 face->lface[LFACE_HEIGHT_INDEX]);
4689 if (NUMBERP (height))
4690 new_height = XFLOATINT (height);
4692 else if (NUMBERP (it->font_height))
4694 /* Value is a multiple of the canonical char height. */
4695 struct face *f;
4697 f = FACE_FROM_ID (it->f,
4698 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4699 new_height = (XFLOATINT (it->font_height)
4700 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4702 else
4704 /* Evaluate IT->font_height with `height' bound to the
4705 current specified height to get the new height. */
4706 ptrdiff_t count = SPECPDL_INDEX ();
4708 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4709 value = safe_eval (it->font_height);
4710 unbind_to (count, Qnil);
4712 if (NUMBERP (value))
4713 new_height = XFLOATINT (value);
4716 if (new_height > 0)
4717 it->face_id = face_with_height (it->f, it->face_id, new_height);
4721 return 0;
4724 /* Handle `(space-width WIDTH)'. */
4725 if (CONSP (spec)
4726 && EQ (XCAR (spec), Qspace_width)
4727 && CONSP (XCDR (spec)))
4729 if (it)
4731 if (!FRAME_WINDOW_P (it->f))
4732 return 0;
4734 value = XCAR (XCDR (spec));
4735 if (NUMBERP (value) && XFLOATINT (value) > 0)
4736 it->space_width = value;
4739 return 0;
4742 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4743 if (CONSP (spec)
4744 && EQ (XCAR (spec), Qslice))
4746 Lisp_Object tem;
4748 if (it)
4750 if (!FRAME_WINDOW_P (it->f))
4751 return 0;
4753 if (tem = XCDR (spec), CONSP (tem))
4755 it->slice.x = XCAR (tem);
4756 if (tem = XCDR (tem), CONSP (tem))
4758 it->slice.y = XCAR (tem);
4759 if (tem = XCDR (tem), CONSP (tem))
4761 it->slice.width = XCAR (tem);
4762 if (tem = XCDR (tem), CONSP (tem))
4763 it->slice.height = XCAR (tem);
4769 return 0;
4772 /* Handle `(raise FACTOR)'. */
4773 if (CONSP (spec)
4774 && EQ (XCAR (spec), Qraise)
4775 && CONSP (XCDR (spec)))
4777 if (it)
4779 if (!FRAME_WINDOW_P (it->f))
4780 return 0;
4782 #ifdef HAVE_WINDOW_SYSTEM
4783 value = XCAR (XCDR (spec));
4784 if (NUMBERP (value))
4786 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4787 it->voffset = - (XFLOATINT (value)
4788 * (FONT_HEIGHT (face->font)));
4790 #endif /* HAVE_WINDOW_SYSTEM */
4793 return 0;
4796 /* Don't handle the other kinds of display specifications
4797 inside a string that we got from a `display' property. */
4798 if (it && it->string_from_display_prop_p)
4799 return 0;
4801 /* Characters having this form of property are not displayed, so
4802 we have to find the end of the property. */
4803 if (it)
4805 start_pos = *position;
4806 *position = display_prop_end (it, object, start_pos);
4808 value = Qnil;
4810 /* Stop the scan at that end position--we assume that all
4811 text properties change there. */
4812 if (it)
4813 it->stop_charpos = position->charpos;
4815 /* Handle `(left-fringe BITMAP [FACE])'
4816 and `(right-fringe BITMAP [FACE])'. */
4817 if (CONSP (spec)
4818 && (EQ (XCAR (spec), Qleft_fringe)
4819 || EQ (XCAR (spec), Qright_fringe))
4820 && CONSP (XCDR (spec)))
4822 int fringe_bitmap;
4824 if (it)
4826 if (!FRAME_WINDOW_P (it->f))
4827 /* If we return here, POSITION has been advanced
4828 across the text with this property. */
4830 /* Synchronize the bidi iterator with POSITION. This is
4831 needed because we are not going to push the iterator
4832 on behalf of this display property, so there will be
4833 no pop_it call to do this synchronization for us. */
4834 if (it->bidi_p)
4836 it->position = *position;
4837 iterate_out_of_display_property (it);
4838 *position = it->position;
4840 return 1;
4843 else if (!frame_window_p)
4844 return 1;
4846 #ifdef HAVE_WINDOW_SYSTEM
4847 value = XCAR (XCDR (spec));
4848 if (!SYMBOLP (value)
4849 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4850 /* If we return here, POSITION has been advanced
4851 across the text with this property. */
4853 if (it && it->bidi_p)
4855 it->position = *position;
4856 iterate_out_of_display_property (it);
4857 *position = it->position;
4859 return 1;
4862 if (it)
4864 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4866 if (CONSP (XCDR (XCDR (spec))))
4868 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4869 int face_id2 = lookup_derived_face (it->f, face_name,
4870 FRINGE_FACE_ID, 0);
4871 if (face_id2 >= 0)
4872 face_id = face_id2;
4875 /* Save current settings of IT so that we can restore them
4876 when we are finished with the glyph property value. */
4877 push_it (it, position);
4879 it->area = TEXT_AREA;
4880 it->what = IT_IMAGE;
4881 it->image_id = -1; /* no image */
4882 it->position = start_pos;
4883 it->object = NILP (object) ? it->w->contents : object;
4884 it->method = GET_FROM_IMAGE;
4885 it->from_overlay = Qnil;
4886 it->face_id = face_id;
4887 it->from_disp_prop_p = 1;
4889 /* Say that we haven't consumed the characters with
4890 `display' property yet. The call to pop_it in
4891 set_iterator_to_next will clean this up. */
4892 *position = start_pos;
4894 if (EQ (XCAR (spec), Qleft_fringe))
4896 it->left_user_fringe_bitmap = fringe_bitmap;
4897 it->left_user_fringe_face_id = face_id;
4899 else
4901 it->right_user_fringe_bitmap = fringe_bitmap;
4902 it->right_user_fringe_face_id = face_id;
4905 #endif /* HAVE_WINDOW_SYSTEM */
4906 return 1;
4909 /* Prepare to handle `((margin left-margin) ...)',
4910 `((margin right-margin) ...)' and `((margin nil) ...)'
4911 prefixes for display specifications. */
4912 location = Qunbound;
4913 if (CONSP (spec) && CONSP (XCAR (spec)))
4915 Lisp_Object tem;
4917 value = XCDR (spec);
4918 if (CONSP (value))
4919 value = XCAR (value);
4921 tem = XCAR (spec);
4922 if (EQ (XCAR (tem), Qmargin)
4923 && (tem = XCDR (tem),
4924 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4925 (NILP (tem)
4926 || EQ (tem, Qleft_margin)
4927 || EQ (tem, Qright_margin))))
4928 location = tem;
4931 if (EQ (location, Qunbound))
4933 location = Qnil;
4934 value = spec;
4937 /* After this point, VALUE is the property after any
4938 margin prefix has been stripped. It must be a string,
4939 an image specification, or `(space ...)'.
4941 LOCATION specifies where to display: `left-margin',
4942 `right-margin' or nil. */
4944 valid_p = (STRINGP (value)
4945 #ifdef HAVE_WINDOW_SYSTEM
4946 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4947 && valid_image_p (value))
4948 #endif /* not HAVE_WINDOW_SYSTEM */
4949 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4951 if (valid_p && !display_replaced_p)
4953 int retval = 1;
4955 if (!it)
4957 /* Callers need to know whether the display spec is any kind
4958 of `(space ...)' spec that is about to affect text-area
4959 display. */
4960 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4961 retval = 2;
4962 return retval;
4965 /* Save current settings of IT so that we can restore them
4966 when we are finished with the glyph property value. */
4967 push_it (it, position);
4968 it->from_overlay = overlay;
4969 it->from_disp_prop_p = 1;
4971 if (NILP (location))
4972 it->area = TEXT_AREA;
4973 else if (EQ (location, Qleft_margin))
4974 it->area = LEFT_MARGIN_AREA;
4975 else
4976 it->area = RIGHT_MARGIN_AREA;
4978 if (STRINGP (value))
4980 it->string = value;
4981 it->multibyte_p = STRING_MULTIBYTE (it->string);
4982 it->current.overlay_string_index = -1;
4983 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4984 it->end_charpos = it->string_nchars = SCHARS (it->string);
4985 it->method = GET_FROM_STRING;
4986 it->stop_charpos = 0;
4987 it->prev_stop = 0;
4988 it->base_level_stop = 0;
4989 it->string_from_display_prop_p = 1;
4990 /* Say that we haven't consumed the characters with
4991 `display' property yet. The call to pop_it in
4992 set_iterator_to_next will clean this up. */
4993 if (BUFFERP (object))
4994 *position = start_pos;
4996 /* Force paragraph direction to be that of the parent
4997 object. If the parent object's paragraph direction is
4998 not yet determined, default to L2R. */
4999 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5000 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5001 else
5002 it->paragraph_embedding = L2R;
5004 /* Set up the bidi iterator for this display string. */
5005 if (it->bidi_p)
5007 it->bidi_it.string.lstring = it->string;
5008 it->bidi_it.string.s = NULL;
5009 it->bidi_it.string.schars = it->end_charpos;
5010 it->bidi_it.string.bufpos = bufpos;
5011 it->bidi_it.string.from_disp_str = 1;
5012 it->bidi_it.string.unibyte = !it->multibyte_p;
5013 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5016 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5018 it->method = GET_FROM_STRETCH;
5019 it->object = value;
5020 *position = it->position = start_pos;
5021 retval = 1 + (it->area == TEXT_AREA);
5023 #ifdef HAVE_WINDOW_SYSTEM
5024 else
5026 it->what = IT_IMAGE;
5027 it->image_id = lookup_image (it->f, value);
5028 it->position = start_pos;
5029 it->object = NILP (object) ? it->w->contents : object;
5030 it->method = GET_FROM_IMAGE;
5032 /* Say that we haven't consumed the characters with
5033 `display' property yet. The call to pop_it in
5034 set_iterator_to_next will clean this up. */
5035 *position = start_pos;
5037 #endif /* HAVE_WINDOW_SYSTEM */
5039 return retval;
5042 /* Invalid property or property not supported. Restore
5043 POSITION to what it was before. */
5044 *position = start_pos;
5045 return 0;
5048 /* Check if PROP is a display property value whose text should be
5049 treated as intangible. OVERLAY is the overlay from which PROP
5050 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5051 specify the buffer position covered by PROP. */
5054 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5055 ptrdiff_t charpos, ptrdiff_t bytepos)
5057 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5058 struct text_pos position;
5060 SET_TEXT_POS (position, charpos, bytepos);
5061 return handle_display_spec (NULL, prop, Qnil, overlay,
5062 &position, charpos, frame_window_p);
5066 /* Return 1 if PROP is a display sub-property value containing STRING.
5068 Implementation note: this and the following function are really
5069 special cases of handle_display_spec and
5070 handle_single_display_spec, and should ideally use the same code.
5071 Until they do, these two pairs must be consistent and must be
5072 modified in sync. */
5074 static int
5075 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5077 if (EQ (string, prop))
5078 return 1;
5080 /* Skip over `when FORM'. */
5081 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5083 prop = XCDR (prop);
5084 if (!CONSP (prop))
5085 return 0;
5086 /* Actually, the condition following `when' should be eval'ed,
5087 like handle_single_display_spec does, and we should return
5088 zero if it evaluates to nil. However, this function is
5089 called only when the buffer was already displayed and some
5090 glyph in the glyph matrix was found to come from a display
5091 string. Therefore, the condition was already evaluated, and
5092 the result was non-nil, otherwise the display string wouldn't
5093 have been displayed and we would have never been called for
5094 this property. Thus, we can skip the evaluation and assume
5095 its result is non-nil. */
5096 prop = XCDR (prop);
5099 if (CONSP (prop))
5100 /* Skip over `margin LOCATION'. */
5101 if (EQ (XCAR (prop), Qmargin))
5103 prop = XCDR (prop);
5104 if (!CONSP (prop))
5105 return 0;
5107 prop = XCDR (prop);
5108 if (!CONSP (prop))
5109 return 0;
5112 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5116 /* Return 1 if STRING appears in the `display' property PROP. */
5118 static int
5119 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5121 if (CONSP (prop)
5122 && !EQ (XCAR (prop), Qwhen)
5123 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5125 /* A list of sub-properties. */
5126 while (CONSP (prop))
5128 if (single_display_spec_string_p (XCAR (prop), string))
5129 return 1;
5130 prop = XCDR (prop);
5133 else if (VECTORP (prop))
5135 /* A vector of sub-properties. */
5136 ptrdiff_t i;
5137 for (i = 0; i < ASIZE (prop); ++i)
5138 if (single_display_spec_string_p (AREF (prop, i), string))
5139 return 1;
5141 else
5142 return single_display_spec_string_p (prop, string);
5144 return 0;
5147 /* Look for STRING in overlays and text properties in the current
5148 buffer, between character positions FROM and TO (excluding TO).
5149 BACK_P non-zero means look back (in this case, TO is supposed to be
5150 less than FROM).
5151 Value is the first character position where STRING was found, or
5152 zero if it wasn't found before hitting TO.
5154 This function may only use code that doesn't eval because it is
5155 called asynchronously from note_mouse_highlight. */
5157 static ptrdiff_t
5158 string_buffer_position_lim (Lisp_Object string,
5159 ptrdiff_t from, ptrdiff_t to, int back_p)
5161 Lisp_Object limit, prop, pos;
5162 int found = 0;
5164 pos = make_number (max (from, BEGV));
5166 if (!back_p) /* looking forward */
5168 limit = make_number (min (to, ZV));
5169 while (!found && !EQ (pos, limit))
5171 prop = Fget_char_property (pos, Qdisplay, Qnil);
5172 if (!NILP (prop) && display_prop_string_p (prop, string))
5173 found = 1;
5174 else
5175 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5176 limit);
5179 else /* looking back */
5181 limit = make_number (max (to, BEGV));
5182 while (!found && !EQ (pos, limit))
5184 prop = Fget_char_property (pos, Qdisplay, Qnil);
5185 if (!NILP (prop) && display_prop_string_p (prop, string))
5186 found = 1;
5187 else
5188 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5189 limit);
5193 return found ? XINT (pos) : 0;
5196 /* Determine which buffer position in current buffer STRING comes from.
5197 AROUND_CHARPOS is an approximate position where it could come from.
5198 Value is the buffer position or 0 if it couldn't be determined.
5200 This function is necessary because we don't record buffer positions
5201 in glyphs generated from strings (to keep struct glyph small).
5202 This function may only use code that doesn't eval because it is
5203 called asynchronously from note_mouse_highlight. */
5205 static ptrdiff_t
5206 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5208 const int MAX_DISTANCE = 1000;
5209 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5210 around_charpos + MAX_DISTANCE,
5213 if (!found)
5214 found = string_buffer_position_lim (string, around_charpos,
5215 around_charpos - MAX_DISTANCE, 1);
5216 return found;
5221 /***********************************************************************
5222 `composition' property
5223 ***********************************************************************/
5225 /* Set up iterator IT from `composition' property at its current
5226 position. Called from handle_stop. */
5228 static enum prop_handled
5229 handle_composition_prop (struct it *it)
5231 Lisp_Object prop, string;
5232 ptrdiff_t pos, pos_byte, start, end;
5234 if (STRINGP (it->string))
5236 unsigned char *s;
5238 pos = IT_STRING_CHARPOS (*it);
5239 pos_byte = IT_STRING_BYTEPOS (*it);
5240 string = it->string;
5241 s = SDATA (string) + pos_byte;
5242 it->c = STRING_CHAR (s);
5244 else
5246 pos = IT_CHARPOS (*it);
5247 pos_byte = IT_BYTEPOS (*it);
5248 string = Qnil;
5249 it->c = FETCH_CHAR (pos_byte);
5252 /* If there's a valid composition and point is not inside of the
5253 composition (in the case that the composition is from the current
5254 buffer), draw a glyph composed from the composition components. */
5255 if (find_composition (pos, -1, &start, &end, &prop, string)
5256 && COMPOSITION_VALID_P (start, end, prop)
5257 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5259 if (start < pos)
5260 /* As we can't handle this situation (perhaps font-lock added
5261 a new composition), we just return here hoping that next
5262 redisplay will detect this composition much earlier. */
5263 return HANDLED_NORMALLY;
5264 if (start != pos)
5266 if (STRINGP (it->string))
5267 pos_byte = string_char_to_byte (it->string, start);
5268 else
5269 pos_byte = CHAR_TO_BYTE (start);
5271 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5272 prop, string);
5274 if (it->cmp_it.id >= 0)
5276 it->cmp_it.ch = -1;
5277 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5278 it->cmp_it.nglyphs = -1;
5282 return HANDLED_NORMALLY;
5287 /***********************************************************************
5288 Overlay strings
5289 ***********************************************************************/
5291 /* The following structure is used to record overlay strings for
5292 later sorting in load_overlay_strings. */
5294 struct overlay_entry
5296 Lisp_Object overlay;
5297 Lisp_Object string;
5298 EMACS_INT priority;
5299 int after_string_p;
5303 /* Set up iterator IT from overlay strings at its current position.
5304 Called from handle_stop. */
5306 static enum prop_handled
5307 handle_overlay_change (struct it *it)
5309 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5310 return HANDLED_RECOMPUTE_PROPS;
5311 else
5312 return HANDLED_NORMALLY;
5316 /* Set up the next overlay string for delivery by IT, if there is an
5317 overlay string to deliver. Called by set_iterator_to_next when the
5318 end of the current overlay string is reached. If there are more
5319 overlay strings to display, IT->string and
5320 IT->current.overlay_string_index are set appropriately here.
5321 Otherwise IT->string is set to nil. */
5323 static void
5324 next_overlay_string (struct it *it)
5326 ++it->current.overlay_string_index;
5327 if (it->current.overlay_string_index == it->n_overlay_strings)
5329 /* No more overlay strings. Restore IT's settings to what
5330 they were before overlay strings were processed, and
5331 continue to deliver from current_buffer. */
5333 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5334 pop_it (it);
5335 eassert (it->sp > 0
5336 || (NILP (it->string)
5337 && it->method == GET_FROM_BUFFER
5338 && it->stop_charpos >= BEGV
5339 && it->stop_charpos <= it->end_charpos));
5340 it->current.overlay_string_index = -1;
5341 it->n_overlay_strings = 0;
5342 it->overlay_strings_charpos = -1;
5343 /* If there's an empty display string on the stack, pop the
5344 stack, to resync the bidi iterator with IT's position. Such
5345 empty strings are pushed onto the stack in
5346 get_overlay_strings_1. */
5347 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5348 pop_it (it);
5350 /* If we're at the end of the buffer, record that we have
5351 processed the overlay strings there already, so that
5352 next_element_from_buffer doesn't try it again. */
5353 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5354 it->overlay_strings_at_end_processed_p = 1;
5356 else
5358 /* There are more overlay strings to process. If
5359 IT->current.overlay_string_index has advanced to a position
5360 where we must load IT->overlay_strings with more strings, do
5361 it. We must load at the IT->overlay_strings_charpos where
5362 IT->n_overlay_strings was originally computed; when invisible
5363 text is present, this might not be IT_CHARPOS (Bug#7016). */
5364 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5366 if (it->current.overlay_string_index && i == 0)
5367 load_overlay_strings (it, it->overlay_strings_charpos);
5369 /* Initialize IT to deliver display elements from the overlay
5370 string. */
5371 it->string = it->overlay_strings[i];
5372 it->multibyte_p = STRING_MULTIBYTE (it->string);
5373 SET_TEXT_POS (it->current.string_pos, 0, 0);
5374 it->method = GET_FROM_STRING;
5375 it->stop_charpos = 0;
5376 it->end_charpos = SCHARS (it->string);
5377 if (it->cmp_it.stop_pos >= 0)
5378 it->cmp_it.stop_pos = 0;
5379 it->prev_stop = 0;
5380 it->base_level_stop = 0;
5382 /* Set up the bidi iterator for this overlay string. */
5383 if (it->bidi_p)
5385 it->bidi_it.string.lstring = it->string;
5386 it->bidi_it.string.s = NULL;
5387 it->bidi_it.string.schars = SCHARS (it->string);
5388 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5389 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5390 it->bidi_it.string.unibyte = !it->multibyte_p;
5391 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5395 CHECK_IT (it);
5399 /* Compare two overlay_entry structures E1 and E2. Used as a
5400 comparison function for qsort in load_overlay_strings. Overlay
5401 strings for the same position are sorted so that
5403 1. All after-strings come in front of before-strings, except
5404 when they come from the same overlay.
5406 2. Within after-strings, strings are sorted so that overlay strings
5407 from overlays with higher priorities come first.
5409 2. Within before-strings, strings are sorted so that overlay
5410 strings from overlays with higher priorities come last.
5412 Value is analogous to strcmp. */
5415 static int
5416 compare_overlay_entries (const void *e1, const void *e2)
5418 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5419 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5420 int result;
5422 if (entry1->after_string_p != entry2->after_string_p)
5424 /* Let after-strings appear in front of before-strings if
5425 they come from different overlays. */
5426 if (EQ (entry1->overlay, entry2->overlay))
5427 result = entry1->after_string_p ? 1 : -1;
5428 else
5429 result = entry1->after_string_p ? -1 : 1;
5431 else if (entry1->priority != entry2->priority)
5433 if (entry1->after_string_p)
5434 /* After-strings sorted in order of decreasing priority. */
5435 result = entry2->priority < entry1->priority ? -1 : 1;
5436 else
5437 /* Before-strings sorted in order of increasing priority. */
5438 result = entry1->priority < entry2->priority ? -1 : 1;
5440 else
5441 result = 0;
5443 return result;
5447 /* Load the vector IT->overlay_strings with overlay strings from IT's
5448 current buffer position, or from CHARPOS if that is > 0. Set
5449 IT->n_overlays to the total number of overlay strings found.
5451 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5452 a time. On entry into load_overlay_strings,
5453 IT->current.overlay_string_index gives the number of overlay
5454 strings that have already been loaded by previous calls to this
5455 function.
5457 IT->add_overlay_start contains an additional overlay start
5458 position to consider for taking overlay strings from, if non-zero.
5459 This position comes into play when the overlay has an `invisible'
5460 property, and both before and after-strings. When we've skipped to
5461 the end of the overlay, because of its `invisible' property, we
5462 nevertheless want its before-string to appear.
5463 IT->add_overlay_start will contain the overlay start position
5464 in this case.
5466 Overlay strings are sorted so that after-string strings come in
5467 front of before-string strings. Within before and after-strings,
5468 strings are sorted by overlay priority. See also function
5469 compare_overlay_entries. */
5471 static void
5472 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5474 Lisp_Object overlay, window, str, invisible;
5475 struct Lisp_Overlay *ov;
5476 ptrdiff_t start, end;
5477 ptrdiff_t size = 20;
5478 ptrdiff_t n = 0, i, j;
5479 int invis_p;
5480 struct overlay_entry *entries = alloca (size * sizeof *entries);
5481 USE_SAFE_ALLOCA;
5483 if (charpos <= 0)
5484 charpos = IT_CHARPOS (*it);
5486 /* Append the overlay string STRING of overlay OVERLAY to vector
5487 `entries' which has size `size' and currently contains `n'
5488 elements. AFTER_P non-zero means STRING is an after-string of
5489 OVERLAY. */
5490 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5491 do \
5493 Lisp_Object priority; \
5495 if (n == size) \
5497 struct overlay_entry *old = entries; \
5498 SAFE_NALLOCA (entries, 2, size); \
5499 memcpy (entries, old, size * sizeof *entries); \
5500 size *= 2; \
5503 entries[n].string = (STRING); \
5504 entries[n].overlay = (OVERLAY); \
5505 priority = Foverlay_get ((OVERLAY), Qpriority); \
5506 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5507 entries[n].after_string_p = (AFTER_P); \
5508 ++n; \
5510 while (0)
5512 /* Process overlay before the overlay center. */
5513 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5515 XSETMISC (overlay, ov);
5516 eassert (OVERLAYP (overlay));
5517 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5518 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5520 if (end < charpos)
5521 break;
5523 /* Skip this overlay if it doesn't start or end at IT's current
5524 position. */
5525 if (end != charpos && start != charpos)
5526 continue;
5528 /* Skip this overlay if it doesn't apply to IT->w. */
5529 window = Foverlay_get (overlay, Qwindow);
5530 if (WINDOWP (window) && XWINDOW (window) != it->w)
5531 continue;
5533 /* If the text ``under'' the overlay is invisible, both before-
5534 and after-strings from this overlay are visible; start and
5535 end position are indistinguishable. */
5536 invisible = Foverlay_get (overlay, Qinvisible);
5537 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5539 /* If overlay has a non-empty before-string, record it. */
5540 if ((start == charpos || (end == charpos && invis_p))
5541 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5542 && SCHARS (str))
5543 RECORD_OVERLAY_STRING (overlay, str, 0);
5545 /* If overlay has a non-empty after-string, record it. */
5546 if ((end == charpos || (start == charpos && invis_p))
5547 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5548 && SCHARS (str))
5549 RECORD_OVERLAY_STRING (overlay, str, 1);
5552 /* Process overlays after the overlay center. */
5553 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5555 XSETMISC (overlay, ov);
5556 eassert (OVERLAYP (overlay));
5557 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5558 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5560 if (start > charpos)
5561 break;
5563 /* Skip this overlay if it doesn't start or end at IT's current
5564 position. */
5565 if (end != charpos && start != charpos)
5566 continue;
5568 /* Skip this overlay if it doesn't apply to IT->w. */
5569 window = Foverlay_get (overlay, Qwindow);
5570 if (WINDOWP (window) && XWINDOW (window) != it->w)
5571 continue;
5573 /* If the text ``under'' the overlay is invisible, it has a zero
5574 dimension, and both before- and after-strings apply. */
5575 invisible = Foverlay_get (overlay, Qinvisible);
5576 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5578 /* If overlay has a non-empty before-string, record it. */
5579 if ((start == charpos || (end == charpos && invis_p))
5580 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5581 && SCHARS (str))
5582 RECORD_OVERLAY_STRING (overlay, str, 0);
5584 /* If overlay has a non-empty after-string, record it. */
5585 if ((end == charpos || (start == charpos && invis_p))
5586 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5587 && SCHARS (str))
5588 RECORD_OVERLAY_STRING (overlay, str, 1);
5591 #undef RECORD_OVERLAY_STRING
5593 /* Sort entries. */
5594 if (n > 1)
5595 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5597 /* Record number of overlay strings, and where we computed it. */
5598 it->n_overlay_strings = n;
5599 it->overlay_strings_charpos = charpos;
5601 /* IT->current.overlay_string_index is the number of overlay strings
5602 that have already been consumed by IT. Copy some of the
5603 remaining overlay strings to IT->overlay_strings. */
5604 i = 0;
5605 j = it->current.overlay_string_index;
5606 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5608 it->overlay_strings[i] = entries[j].string;
5609 it->string_overlays[i++] = entries[j++].overlay;
5612 CHECK_IT (it);
5613 SAFE_FREE ();
5617 /* Get the first chunk of overlay strings at IT's current buffer
5618 position, or at CHARPOS if that is > 0. Value is non-zero if at
5619 least one overlay string was found. */
5621 static int
5622 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5624 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5625 process. This fills IT->overlay_strings with strings, and sets
5626 IT->n_overlay_strings to the total number of strings to process.
5627 IT->pos.overlay_string_index has to be set temporarily to zero
5628 because load_overlay_strings needs this; it must be set to -1
5629 when no overlay strings are found because a zero value would
5630 indicate a position in the first overlay string. */
5631 it->current.overlay_string_index = 0;
5632 load_overlay_strings (it, charpos);
5634 /* If we found overlay strings, set up IT to deliver display
5635 elements from the first one. Otherwise set up IT to deliver
5636 from current_buffer. */
5637 if (it->n_overlay_strings)
5639 /* Make sure we know settings in current_buffer, so that we can
5640 restore meaningful values when we're done with the overlay
5641 strings. */
5642 if (compute_stop_p)
5643 compute_stop_pos (it);
5644 eassert (it->face_id >= 0);
5646 /* Save IT's settings. They are restored after all overlay
5647 strings have been processed. */
5648 eassert (!compute_stop_p || it->sp == 0);
5650 /* When called from handle_stop, there might be an empty display
5651 string loaded. In that case, don't bother saving it. But
5652 don't use this optimization with the bidi iterator, since we
5653 need the corresponding pop_it call to resync the bidi
5654 iterator's position with IT's position, after we are done
5655 with the overlay strings. (The corresponding call to pop_it
5656 in case of an empty display string is in
5657 next_overlay_string.) */
5658 if (!(!it->bidi_p
5659 && STRINGP (it->string) && !SCHARS (it->string)))
5660 push_it (it, NULL);
5662 /* Set up IT to deliver display elements from the first overlay
5663 string. */
5664 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5665 it->string = it->overlay_strings[0];
5666 it->from_overlay = Qnil;
5667 it->stop_charpos = 0;
5668 eassert (STRINGP (it->string));
5669 it->end_charpos = SCHARS (it->string);
5670 it->prev_stop = 0;
5671 it->base_level_stop = 0;
5672 it->multibyte_p = STRING_MULTIBYTE (it->string);
5673 it->method = GET_FROM_STRING;
5674 it->from_disp_prop_p = 0;
5676 /* Force paragraph direction to be that of the parent
5677 buffer. */
5678 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5679 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5680 else
5681 it->paragraph_embedding = L2R;
5683 /* Set up the bidi iterator for this overlay string. */
5684 if (it->bidi_p)
5686 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5688 it->bidi_it.string.lstring = it->string;
5689 it->bidi_it.string.s = NULL;
5690 it->bidi_it.string.schars = SCHARS (it->string);
5691 it->bidi_it.string.bufpos = pos;
5692 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5693 it->bidi_it.string.unibyte = !it->multibyte_p;
5694 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5696 return 1;
5699 it->current.overlay_string_index = -1;
5700 return 0;
5703 static int
5704 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5706 it->string = Qnil;
5707 it->method = GET_FROM_BUFFER;
5709 (void) get_overlay_strings_1 (it, charpos, 1);
5711 CHECK_IT (it);
5713 /* Value is non-zero if we found at least one overlay string. */
5714 return STRINGP (it->string);
5719 /***********************************************************************
5720 Saving and restoring state
5721 ***********************************************************************/
5723 /* Save current settings of IT on IT->stack. Called, for example,
5724 before setting up IT for an overlay string, to be able to restore
5725 IT's settings to what they were after the overlay string has been
5726 processed. If POSITION is non-NULL, it is the position to save on
5727 the stack instead of IT->position. */
5729 static void
5730 push_it (struct it *it, struct text_pos *position)
5732 struct iterator_stack_entry *p;
5734 eassert (it->sp < IT_STACK_SIZE);
5735 p = it->stack + it->sp;
5737 p->stop_charpos = it->stop_charpos;
5738 p->prev_stop = it->prev_stop;
5739 p->base_level_stop = it->base_level_stop;
5740 p->cmp_it = it->cmp_it;
5741 eassert (it->face_id >= 0);
5742 p->face_id = it->face_id;
5743 p->string = it->string;
5744 p->method = it->method;
5745 p->from_overlay = it->from_overlay;
5746 switch (p->method)
5748 case GET_FROM_IMAGE:
5749 p->u.image.object = it->object;
5750 p->u.image.image_id = it->image_id;
5751 p->u.image.slice = it->slice;
5752 break;
5753 case GET_FROM_STRETCH:
5754 p->u.stretch.object = it->object;
5755 break;
5757 p->position = position ? *position : it->position;
5758 p->current = it->current;
5759 p->end_charpos = it->end_charpos;
5760 p->string_nchars = it->string_nchars;
5761 p->area = it->area;
5762 p->multibyte_p = it->multibyte_p;
5763 p->avoid_cursor_p = it->avoid_cursor_p;
5764 p->space_width = it->space_width;
5765 p->font_height = it->font_height;
5766 p->voffset = it->voffset;
5767 p->string_from_display_prop_p = it->string_from_display_prop_p;
5768 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5769 p->display_ellipsis_p = 0;
5770 p->line_wrap = it->line_wrap;
5771 p->bidi_p = it->bidi_p;
5772 p->paragraph_embedding = it->paragraph_embedding;
5773 p->from_disp_prop_p = it->from_disp_prop_p;
5774 ++it->sp;
5776 /* Save the state of the bidi iterator as well. */
5777 if (it->bidi_p)
5778 bidi_push_it (&it->bidi_it);
5781 static void
5782 iterate_out_of_display_property (struct it *it)
5784 int buffer_p = !STRINGP (it->string);
5785 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5786 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5788 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5790 /* Maybe initialize paragraph direction. If we are at the beginning
5791 of a new paragraph, next_element_from_buffer may not have a
5792 chance to do that. */
5793 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5794 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5795 /* prev_stop can be zero, so check against BEGV as well. */
5796 while (it->bidi_it.charpos >= bob
5797 && it->prev_stop <= it->bidi_it.charpos
5798 && it->bidi_it.charpos < CHARPOS (it->position)
5799 && it->bidi_it.charpos < eob)
5800 bidi_move_to_visually_next (&it->bidi_it);
5801 /* Record the stop_pos we just crossed, for when we cross it
5802 back, maybe. */
5803 if (it->bidi_it.charpos > CHARPOS (it->position))
5804 it->prev_stop = CHARPOS (it->position);
5805 /* If we ended up not where pop_it put us, resync IT's
5806 positional members with the bidi iterator. */
5807 if (it->bidi_it.charpos != CHARPOS (it->position))
5808 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5809 if (buffer_p)
5810 it->current.pos = it->position;
5811 else
5812 it->current.string_pos = it->position;
5815 /* Restore IT's settings from IT->stack. Called, for example, when no
5816 more overlay strings must be processed, and we return to delivering
5817 display elements from a buffer, or when the end of a string from a
5818 `display' property is reached and we return to delivering display
5819 elements from an overlay string, or from a buffer. */
5821 static void
5822 pop_it (struct it *it)
5824 struct iterator_stack_entry *p;
5825 int from_display_prop = it->from_disp_prop_p;
5827 eassert (it->sp > 0);
5828 --it->sp;
5829 p = it->stack + it->sp;
5830 it->stop_charpos = p->stop_charpos;
5831 it->prev_stop = p->prev_stop;
5832 it->base_level_stop = p->base_level_stop;
5833 it->cmp_it = p->cmp_it;
5834 it->face_id = p->face_id;
5835 it->current = p->current;
5836 it->position = p->position;
5837 it->string = p->string;
5838 it->from_overlay = p->from_overlay;
5839 if (NILP (it->string))
5840 SET_TEXT_POS (it->current.string_pos, -1, -1);
5841 it->method = p->method;
5842 switch (it->method)
5844 case GET_FROM_IMAGE:
5845 it->image_id = p->u.image.image_id;
5846 it->object = p->u.image.object;
5847 it->slice = p->u.image.slice;
5848 break;
5849 case GET_FROM_STRETCH:
5850 it->object = p->u.stretch.object;
5851 break;
5852 case GET_FROM_BUFFER:
5853 it->object = it->w->contents;
5854 break;
5855 case GET_FROM_STRING:
5856 it->object = it->string;
5857 break;
5858 case GET_FROM_DISPLAY_VECTOR:
5859 if (it->s)
5860 it->method = GET_FROM_C_STRING;
5861 else if (STRINGP (it->string))
5862 it->method = GET_FROM_STRING;
5863 else
5865 it->method = GET_FROM_BUFFER;
5866 it->object = it->w->contents;
5869 it->end_charpos = p->end_charpos;
5870 it->string_nchars = p->string_nchars;
5871 it->area = p->area;
5872 it->multibyte_p = p->multibyte_p;
5873 it->avoid_cursor_p = p->avoid_cursor_p;
5874 it->space_width = p->space_width;
5875 it->font_height = p->font_height;
5876 it->voffset = p->voffset;
5877 it->string_from_display_prop_p = p->string_from_display_prop_p;
5878 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5879 it->line_wrap = p->line_wrap;
5880 it->bidi_p = p->bidi_p;
5881 it->paragraph_embedding = p->paragraph_embedding;
5882 it->from_disp_prop_p = p->from_disp_prop_p;
5883 if (it->bidi_p)
5885 bidi_pop_it (&it->bidi_it);
5886 /* Bidi-iterate until we get out of the portion of text, if any,
5887 covered by a `display' text property or by an overlay with
5888 `display' property. (We cannot just jump there, because the
5889 internal coherency of the bidi iterator state can not be
5890 preserved across such jumps.) We also must determine the
5891 paragraph base direction if the overlay we just processed is
5892 at the beginning of a new paragraph. */
5893 if (from_display_prop
5894 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5895 iterate_out_of_display_property (it);
5897 eassert ((BUFFERP (it->object)
5898 && IT_CHARPOS (*it) == it->bidi_it.charpos
5899 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5900 || (STRINGP (it->object)
5901 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5902 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5903 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5909 /***********************************************************************
5910 Moving over lines
5911 ***********************************************************************/
5913 /* Set IT's current position to the previous line start. */
5915 static void
5916 back_to_previous_line_start (struct it *it)
5918 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
5920 DEC_BOTH (cp, bp);
5921 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
5925 /* Move IT to the next line start.
5927 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5928 we skipped over part of the text (as opposed to moving the iterator
5929 continuously over the text). Otherwise, don't change the value
5930 of *SKIPPED_P.
5932 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5933 iterator on the newline, if it was found.
5935 Newlines may come from buffer text, overlay strings, or strings
5936 displayed via the `display' property. That's the reason we can't
5937 simply use find_newline_no_quit.
5939 Note that this function may not skip over invisible text that is so
5940 because of text properties and immediately follows a newline. If
5941 it would, function reseat_at_next_visible_line_start, when called
5942 from set_iterator_to_next, would effectively make invisible
5943 characters following a newline part of the wrong glyph row, which
5944 leads to wrong cursor motion. */
5946 static int
5947 forward_to_next_line_start (struct it *it, int *skipped_p,
5948 struct bidi_it *bidi_it_prev)
5950 ptrdiff_t old_selective;
5951 int newline_found_p, n;
5952 const int MAX_NEWLINE_DISTANCE = 500;
5954 /* If already on a newline, just consume it to avoid unintended
5955 skipping over invisible text below. */
5956 if (it->what == IT_CHARACTER
5957 && it->c == '\n'
5958 && CHARPOS (it->position) == IT_CHARPOS (*it))
5960 if (it->bidi_p && bidi_it_prev)
5961 *bidi_it_prev = it->bidi_it;
5962 set_iterator_to_next (it, 0);
5963 it->c = 0;
5964 return 1;
5967 /* Don't handle selective display in the following. It's (a)
5968 unnecessary because it's done by the caller, and (b) leads to an
5969 infinite recursion because next_element_from_ellipsis indirectly
5970 calls this function. */
5971 old_selective = it->selective;
5972 it->selective = 0;
5974 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5975 from buffer text. */
5976 for (n = newline_found_p = 0;
5977 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5978 n += STRINGP (it->string) ? 0 : 1)
5980 if (!get_next_display_element (it))
5981 return 0;
5982 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5983 if (newline_found_p && it->bidi_p && bidi_it_prev)
5984 *bidi_it_prev = it->bidi_it;
5985 set_iterator_to_next (it, 0);
5988 /* If we didn't find a newline near enough, see if we can use a
5989 short-cut. */
5990 if (!newline_found_p)
5992 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
5993 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
5994 1, &bytepos);
5995 Lisp_Object pos;
5997 eassert (!STRINGP (it->string));
5999 /* If there isn't any `display' property in sight, and no
6000 overlays, we can just use the position of the newline in
6001 buffer text. */
6002 if (it->stop_charpos >= limit
6003 || ((pos = Fnext_single_property_change (make_number (start),
6004 Qdisplay, Qnil,
6005 make_number (limit)),
6006 NILP (pos))
6007 && next_overlay_change (start) == ZV))
6009 if (!it->bidi_p)
6011 IT_CHARPOS (*it) = limit;
6012 IT_BYTEPOS (*it) = bytepos;
6014 else
6016 struct bidi_it bprev;
6018 /* Help bidi.c avoid expensive searches for display
6019 properties and overlays, by telling it that there are
6020 none up to `limit'. */
6021 if (it->bidi_it.disp_pos < limit)
6023 it->bidi_it.disp_pos = limit;
6024 it->bidi_it.disp_prop = 0;
6026 do {
6027 bprev = it->bidi_it;
6028 bidi_move_to_visually_next (&it->bidi_it);
6029 } while (it->bidi_it.charpos != limit);
6030 IT_CHARPOS (*it) = limit;
6031 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6032 if (bidi_it_prev)
6033 *bidi_it_prev = bprev;
6035 *skipped_p = newline_found_p = 1;
6037 else
6039 while (get_next_display_element (it)
6040 && !newline_found_p)
6042 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6043 if (newline_found_p && it->bidi_p && bidi_it_prev)
6044 *bidi_it_prev = it->bidi_it;
6045 set_iterator_to_next (it, 0);
6050 it->selective = old_selective;
6051 return newline_found_p;
6055 /* Set IT's current position to the previous visible line start. Skip
6056 invisible text that is so either due to text properties or due to
6057 selective display. Caution: this does not change IT->current_x and
6058 IT->hpos. */
6060 static void
6061 back_to_previous_visible_line_start (struct it *it)
6063 while (IT_CHARPOS (*it) > BEGV)
6065 back_to_previous_line_start (it);
6067 if (IT_CHARPOS (*it) <= BEGV)
6068 break;
6070 /* If selective > 0, then lines indented more than its value are
6071 invisible. */
6072 if (it->selective > 0
6073 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6074 it->selective))
6075 continue;
6077 /* Check the newline before point for invisibility. */
6079 Lisp_Object prop;
6080 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6081 Qinvisible, it->window);
6082 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6083 continue;
6086 if (IT_CHARPOS (*it) <= BEGV)
6087 break;
6090 struct it it2;
6091 void *it2data = NULL;
6092 ptrdiff_t pos;
6093 ptrdiff_t beg, end;
6094 Lisp_Object val, overlay;
6096 SAVE_IT (it2, *it, it2data);
6098 /* If newline is part of a composition, continue from start of composition */
6099 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6100 && beg < IT_CHARPOS (*it))
6101 goto replaced;
6103 /* If newline is replaced by a display property, find start of overlay
6104 or interval and continue search from that point. */
6105 pos = --IT_CHARPOS (it2);
6106 --IT_BYTEPOS (it2);
6107 it2.sp = 0;
6108 bidi_unshelve_cache (NULL, 0);
6109 it2.string_from_display_prop_p = 0;
6110 it2.from_disp_prop_p = 0;
6111 if (handle_display_prop (&it2) == HANDLED_RETURN
6112 && !NILP (val = get_char_property_and_overlay
6113 (make_number (pos), Qdisplay, Qnil, &overlay))
6114 && (OVERLAYP (overlay)
6115 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6116 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6118 RESTORE_IT (it, it, it2data);
6119 goto replaced;
6122 /* Newline is not replaced by anything -- so we are done. */
6123 RESTORE_IT (it, it, it2data);
6124 break;
6126 replaced:
6127 if (beg < BEGV)
6128 beg = BEGV;
6129 IT_CHARPOS (*it) = beg;
6130 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6134 it->continuation_lines_width = 0;
6136 eassert (IT_CHARPOS (*it) >= BEGV);
6137 eassert (IT_CHARPOS (*it) == BEGV
6138 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6139 CHECK_IT (it);
6143 /* Reseat iterator IT at the previous visible line start. Skip
6144 invisible text that is so either due to text properties or due to
6145 selective display. At the end, update IT's overlay information,
6146 face information etc. */
6148 void
6149 reseat_at_previous_visible_line_start (struct it *it)
6151 back_to_previous_visible_line_start (it);
6152 reseat (it, it->current.pos, 1);
6153 CHECK_IT (it);
6157 /* Reseat iterator IT on the next visible line start in the current
6158 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6159 preceding the line start. Skip over invisible text that is so
6160 because of selective display. Compute faces, overlays etc at the
6161 new position. Note that this function does not skip over text that
6162 is invisible because of text properties. */
6164 static void
6165 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6167 int newline_found_p, skipped_p = 0;
6168 struct bidi_it bidi_it_prev;
6170 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6172 /* Skip over lines that are invisible because they are indented
6173 more than the value of IT->selective. */
6174 if (it->selective > 0)
6175 while (IT_CHARPOS (*it) < ZV
6176 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6177 it->selective))
6179 eassert (IT_BYTEPOS (*it) == BEGV
6180 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6181 newline_found_p =
6182 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6185 /* Position on the newline if that's what's requested. */
6186 if (on_newline_p && newline_found_p)
6188 if (STRINGP (it->string))
6190 if (IT_STRING_CHARPOS (*it) > 0)
6192 if (!it->bidi_p)
6194 --IT_STRING_CHARPOS (*it);
6195 --IT_STRING_BYTEPOS (*it);
6197 else
6199 /* We need to restore the bidi iterator to the state
6200 it had on the newline, and resync the IT's
6201 position with that. */
6202 it->bidi_it = bidi_it_prev;
6203 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6204 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6208 else if (IT_CHARPOS (*it) > BEGV)
6210 if (!it->bidi_p)
6212 --IT_CHARPOS (*it);
6213 --IT_BYTEPOS (*it);
6215 else
6217 /* We need to restore the bidi iterator to the state it
6218 had on the newline and resync IT with that. */
6219 it->bidi_it = bidi_it_prev;
6220 IT_CHARPOS (*it) = it->bidi_it.charpos;
6221 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6223 reseat (it, it->current.pos, 0);
6226 else if (skipped_p)
6227 reseat (it, it->current.pos, 0);
6229 CHECK_IT (it);
6234 /***********************************************************************
6235 Changing an iterator's position
6236 ***********************************************************************/
6238 /* Change IT's current position to POS in current_buffer. If FORCE_P
6239 is non-zero, always check for text properties at the new position.
6240 Otherwise, text properties are only looked up if POS >=
6241 IT->check_charpos of a property. */
6243 static void
6244 reseat (struct it *it, struct text_pos pos, int force_p)
6246 ptrdiff_t original_pos = IT_CHARPOS (*it);
6248 reseat_1 (it, pos, 0);
6250 /* Determine where to check text properties. Avoid doing it
6251 where possible because text property lookup is very expensive. */
6252 if (force_p
6253 || CHARPOS (pos) > it->stop_charpos
6254 || CHARPOS (pos) < original_pos)
6256 if (it->bidi_p)
6258 /* For bidi iteration, we need to prime prev_stop and
6259 base_level_stop with our best estimations. */
6260 /* Implementation note: Of course, POS is not necessarily a
6261 stop position, so assigning prev_pos to it is a lie; we
6262 should have called compute_stop_backwards. However, if
6263 the current buffer does not include any R2L characters,
6264 that call would be a waste of cycles, because the
6265 iterator will never move back, and thus never cross this
6266 "fake" stop position. So we delay that backward search
6267 until the time we really need it, in next_element_from_buffer. */
6268 if (CHARPOS (pos) != it->prev_stop)
6269 it->prev_stop = CHARPOS (pos);
6270 if (CHARPOS (pos) < it->base_level_stop)
6271 it->base_level_stop = 0; /* meaning it's unknown */
6272 handle_stop (it);
6274 else
6276 handle_stop (it);
6277 it->prev_stop = it->base_level_stop = 0;
6282 CHECK_IT (it);
6286 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6287 IT->stop_pos to POS, also. */
6289 static void
6290 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6292 /* Don't call this function when scanning a C string. */
6293 eassert (it->s == NULL);
6295 /* POS must be a reasonable value. */
6296 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6298 it->current.pos = it->position = pos;
6299 it->end_charpos = ZV;
6300 it->dpvec = NULL;
6301 it->current.dpvec_index = -1;
6302 it->current.overlay_string_index = -1;
6303 IT_STRING_CHARPOS (*it) = -1;
6304 IT_STRING_BYTEPOS (*it) = -1;
6305 it->string = Qnil;
6306 it->method = GET_FROM_BUFFER;
6307 it->object = it->w->contents;
6308 it->area = TEXT_AREA;
6309 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6310 it->sp = 0;
6311 it->string_from_display_prop_p = 0;
6312 it->string_from_prefix_prop_p = 0;
6314 it->from_disp_prop_p = 0;
6315 it->face_before_selective_p = 0;
6316 if (it->bidi_p)
6318 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6319 &it->bidi_it);
6320 bidi_unshelve_cache (NULL, 0);
6321 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6322 it->bidi_it.string.s = NULL;
6323 it->bidi_it.string.lstring = Qnil;
6324 it->bidi_it.string.bufpos = 0;
6325 it->bidi_it.string.unibyte = 0;
6328 if (set_stop_p)
6330 it->stop_charpos = CHARPOS (pos);
6331 it->base_level_stop = CHARPOS (pos);
6333 /* This make the information stored in it->cmp_it invalidate. */
6334 it->cmp_it.id = -1;
6338 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6339 If S is non-null, it is a C string to iterate over. Otherwise,
6340 STRING gives a Lisp string to iterate over.
6342 If PRECISION > 0, don't return more then PRECISION number of
6343 characters from the string.
6345 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6346 characters have been returned. FIELD_WIDTH < 0 means an infinite
6347 field width.
6349 MULTIBYTE = 0 means disable processing of multibyte characters,
6350 MULTIBYTE > 0 means enable it,
6351 MULTIBYTE < 0 means use IT->multibyte_p.
6353 IT must be initialized via a prior call to init_iterator before
6354 calling this function. */
6356 static void
6357 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6358 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6359 int multibyte)
6361 /* No region in strings. */
6362 it->region_beg_charpos = it->region_end_charpos = -1;
6364 /* No text property checks performed by default, but see below. */
6365 it->stop_charpos = -1;
6367 /* Set iterator position and end position. */
6368 memset (&it->current, 0, sizeof it->current);
6369 it->current.overlay_string_index = -1;
6370 it->current.dpvec_index = -1;
6371 eassert (charpos >= 0);
6373 /* If STRING is specified, use its multibyteness, otherwise use the
6374 setting of MULTIBYTE, if specified. */
6375 if (multibyte >= 0)
6376 it->multibyte_p = multibyte > 0;
6378 /* Bidirectional reordering of strings is controlled by the default
6379 value of bidi-display-reordering. Don't try to reorder while
6380 loading loadup.el, as the necessary character property tables are
6381 not yet available. */
6382 it->bidi_p =
6383 NILP (Vpurify_flag)
6384 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6386 if (s == NULL)
6388 eassert (STRINGP (string));
6389 it->string = string;
6390 it->s = NULL;
6391 it->end_charpos = it->string_nchars = SCHARS (string);
6392 it->method = GET_FROM_STRING;
6393 it->current.string_pos = string_pos (charpos, string);
6395 if (it->bidi_p)
6397 it->bidi_it.string.lstring = string;
6398 it->bidi_it.string.s = NULL;
6399 it->bidi_it.string.schars = it->end_charpos;
6400 it->bidi_it.string.bufpos = 0;
6401 it->bidi_it.string.from_disp_str = 0;
6402 it->bidi_it.string.unibyte = !it->multibyte_p;
6403 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6404 FRAME_WINDOW_P (it->f), &it->bidi_it);
6407 else
6409 it->s = (const unsigned char *) s;
6410 it->string = Qnil;
6412 /* Note that we use IT->current.pos, not it->current.string_pos,
6413 for displaying C strings. */
6414 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6415 if (it->multibyte_p)
6417 it->current.pos = c_string_pos (charpos, s, 1);
6418 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6420 else
6422 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6423 it->end_charpos = it->string_nchars = strlen (s);
6426 if (it->bidi_p)
6428 it->bidi_it.string.lstring = Qnil;
6429 it->bidi_it.string.s = (const unsigned char *) s;
6430 it->bidi_it.string.schars = it->end_charpos;
6431 it->bidi_it.string.bufpos = 0;
6432 it->bidi_it.string.from_disp_str = 0;
6433 it->bidi_it.string.unibyte = !it->multibyte_p;
6434 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6435 &it->bidi_it);
6437 it->method = GET_FROM_C_STRING;
6440 /* PRECISION > 0 means don't return more than PRECISION characters
6441 from the string. */
6442 if (precision > 0 && it->end_charpos - charpos > precision)
6444 it->end_charpos = it->string_nchars = charpos + precision;
6445 if (it->bidi_p)
6446 it->bidi_it.string.schars = it->end_charpos;
6449 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6450 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6451 FIELD_WIDTH < 0 means infinite field width. This is useful for
6452 padding with `-' at the end of a mode line. */
6453 if (field_width < 0)
6454 field_width = INFINITY;
6455 /* Implementation note: We deliberately don't enlarge
6456 it->bidi_it.string.schars here to fit it->end_charpos, because
6457 the bidi iterator cannot produce characters out of thin air. */
6458 if (field_width > it->end_charpos - charpos)
6459 it->end_charpos = charpos + field_width;
6461 /* Use the standard display table for displaying strings. */
6462 if (DISP_TABLE_P (Vstandard_display_table))
6463 it->dp = XCHAR_TABLE (Vstandard_display_table);
6465 it->stop_charpos = charpos;
6466 it->prev_stop = charpos;
6467 it->base_level_stop = 0;
6468 if (it->bidi_p)
6470 it->bidi_it.first_elt = 1;
6471 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6472 it->bidi_it.disp_pos = -1;
6474 if (s == NULL && it->multibyte_p)
6476 ptrdiff_t endpos = SCHARS (it->string);
6477 if (endpos > it->end_charpos)
6478 endpos = it->end_charpos;
6479 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6480 it->string);
6482 CHECK_IT (it);
6487 /***********************************************************************
6488 Iteration
6489 ***********************************************************************/
6491 /* Map enum it_method value to corresponding next_element_from_* function. */
6493 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6495 next_element_from_buffer,
6496 next_element_from_display_vector,
6497 next_element_from_string,
6498 next_element_from_c_string,
6499 next_element_from_image,
6500 next_element_from_stretch
6503 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6506 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6507 (possibly with the following characters). */
6509 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6510 ((IT)->cmp_it.id >= 0 \
6511 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6512 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6513 END_CHARPOS, (IT)->w, \
6514 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6515 (IT)->string)))
6518 /* Lookup the char-table Vglyphless_char_display for character C (-1
6519 if we want information for no-font case), and return the display
6520 method symbol. By side-effect, update it->what and
6521 it->glyphless_method. This function is called from
6522 get_next_display_element for each character element, and from
6523 x_produce_glyphs when no suitable font was found. */
6525 Lisp_Object
6526 lookup_glyphless_char_display (int c, struct it *it)
6528 Lisp_Object glyphless_method = Qnil;
6530 if (CHAR_TABLE_P (Vglyphless_char_display)
6531 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6533 if (c >= 0)
6535 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6536 if (CONSP (glyphless_method))
6537 glyphless_method = FRAME_WINDOW_P (it->f)
6538 ? XCAR (glyphless_method)
6539 : XCDR (glyphless_method);
6541 else
6542 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6545 retry:
6546 if (NILP (glyphless_method))
6548 if (c >= 0)
6549 /* The default is to display the character by a proper font. */
6550 return Qnil;
6551 /* The default for the no-font case is to display an empty box. */
6552 glyphless_method = Qempty_box;
6554 if (EQ (glyphless_method, Qzero_width))
6556 if (c >= 0)
6557 return glyphless_method;
6558 /* This method can't be used for the no-font case. */
6559 glyphless_method = Qempty_box;
6561 if (EQ (glyphless_method, Qthin_space))
6562 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6563 else if (EQ (glyphless_method, Qempty_box))
6564 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6565 else if (EQ (glyphless_method, Qhex_code))
6566 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6567 else if (STRINGP (glyphless_method))
6568 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6569 else
6571 /* Invalid value. We use the default method. */
6572 glyphless_method = Qnil;
6573 goto retry;
6575 it->what = IT_GLYPHLESS;
6576 return glyphless_method;
6579 /* Load IT's display element fields with information about the next
6580 display element from the current position of IT. Value is zero if
6581 end of buffer (or C string) is reached. */
6583 static struct frame *last_escape_glyph_frame = NULL;
6584 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6585 static int last_escape_glyph_merged_face_id = 0;
6587 struct frame *last_glyphless_glyph_frame = NULL;
6588 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6589 int last_glyphless_glyph_merged_face_id = 0;
6591 static int
6592 get_next_display_element (struct it *it)
6594 /* Non-zero means that we found a display element. Zero means that
6595 we hit the end of what we iterate over. Performance note: the
6596 function pointer `method' used here turns out to be faster than
6597 using a sequence of if-statements. */
6598 int success_p;
6600 get_next:
6601 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6603 if (it->what == IT_CHARACTER)
6605 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6606 and only if (a) the resolved directionality of that character
6607 is R..." */
6608 /* FIXME: Do we need an exception for characters from display
6609 tables? */
6610 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6611 it->c = bidi_mirror_char (it->c);
6612 /* Map via display table or translate control characters.
6613 IT->c, IT->len etc. have been set to the next character by
6614 the function call above. If we have a display table, and it
6615 contains an entry for IT->c, translate it. Don't do this if
6616 IT->c itself comes from a display table, otherwise we could
6617 end up in an infinite recursion. (An alternative could be to
6618 count the recursion depth of this function and signal an
6619 error when a certain maximum depth is reached.) Is it worth
6620 it? */
6621 if (success_p && it->dpvec == NULL)
6623 Lisp_Object dv;
6624 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6625 int nonascii_space_p = 0;
6626 int nonascii_hyphen_p = 0;
6627 int c = it->c; /* This is the character to display. */
6629 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6631 eassert (SINGLE_BYTE_CHAR_P (c));
6632 if (unibyte_display_via_language_environment)
6634 c = DECODE_CHAR (unibyte, c);
6635 if (c < 0)
6636 c = BYTE8_TO_CHAR (it->c);
6638 else
6639 c = BYTE8_TO_CHAR (it->c);
6642 if (it->dp
6643 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6644 VECTORP (dv)))
6646 struct Lisp_Vector *v = XVECTOR (dv);
6648 /* Return the first character from the display table
6649 entry, if not empty. If empty, don't display the
6650 current character. */
6651 if (v->header.size)
6653 it->dpvec_char_len = it->len;
6654 it->dpvec = v->contents;
6655 it->dpend = v->contents + v->header.size;
6656 it->current.dpvec_index = 0;
6657 it->dpvec_face_id = -1;
6658 it->saved_face_id = it->face_id;
6659 it->method = GET_FROM_DISPLAY_VECTOR;
6660 it->ellipsis_p = 0;
6662 else
6664 set_iterator_to_next (it, 0);
6666 goto get_next;
6669 if (! NILP (lookup_glyphless_char_display (c, it)))
6671 if (it->what == IT_GLYPHLESS)
6672 goto done;
6673 /* Don't display this character. */
6674 set_iterator_to_next (it, 0);
6675 goto get_next;
6678 /* If `nobreak-char-display' is non-nil, we display
6679 non-ASCII spaces and hyphens specially. */
6680 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6682 if (c == 0xA0)
6683 nonascii_space_p = 1;
6684 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6685 nonascii_hyphen_p = 1;
6688 /* Translate control characters into `\003' or `^C' form.
6689 Control characters coming from a display table entry are
6690 currently not translated because we use IT->dpvec to hold
6691 the translation. This could easily be changed but I
6692 don't believe that it is worth doing.
6694 The characters handled by `nobreak-char-display' must be
6695 translated too.
6697 Non-printable characters and raw-byte characters are also
6698 translated to octal form. */
6699 if (((c < ' ' || c == 127) /* ASCII control chars */
6700 ? (it->area != TEXT_AREA
6701 /* In mode line, treat \n, \t like other crl chars. */
6702 || (c != '\t'
6703 && it->glyph_row
6704 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6705 || (c != '\n' && c != '\t'))
6706 : (nonascii_space_p
6707 || nonascii_hyphen_p
6708 || CHAR_BYTE8_P (c)
6709 || ! CHAR_PRINTABLE_P (c))))
6711 /* C is a control character, non-ASCII space/hyphen,
6712 raw-byte, or a non-printable character which must be
6713 displayed either as '\003' or as `^C' where the '\\'
6714 and '^' can be defined in the display table. Fill
6715 IT->ctl_chars with glyphs for what we have to
6716 display. Then, set IT->dpvec to these glyphs. */
6717 Lisp_Object gc;
6718 int ctl_len;
6719 int face_id;
6720 int lface_id = 0;
6721 int escape_glyph;
6723 /* Handle control characters with ^. */
6725 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6727 int g;
6729 g = '^'; /* default glyph for Control */
6730 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6731 if (it->dp
6732 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6734 g = GLYPH_CODE_CHAR (gc);
6735 lface_id = GLYPH_CODE_FACE (gc);
6737 if (lface_id)
6739 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6741 else if (it->f == last_escape_glyph_frame
6742 && it->face_id == last_escape_glyph_face_id)
6744 face_id = last_escape_glyph_merged_face_id;
6746 else
6748 /* Merge the escape-glyph face into the current face. */
6749 face_id = merge_faces (it->f, Qescape_glyph, 0,
6750 it->face_id);
6751 last_escape_glyph_frame = it->f;
6752 last_escape_glyph_face_id = it->face_id;
6753 last_escape_glyph_merged_face_id = face_id;
6756 XSETINT (it->ctl_chars[0], g);
6757 XSETINT (it->ctl_chars[1], c ^ 0100);
6758 ctl_len = 2;
6759 goto display_control;
6762 /* Handle non-ascii space in the mode where it only gets
6763 highlighting. */
6765 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6767 /* Merge `nobreak-space' into the current face. */
6768 face_id = merge_faces (it->f, Qnobreak_space, 0,
6769 it->face_id);
6770 XSETINT (it->ctl_chars[0], ' ');
6771 ctl_len = 1;
6772 goto display_control;
6775 /* Handle sequences that start with the "escape glyph". */
6777 /* the default escape glyph is \. */
6778 escape_glyph = '\\';
6780 if (it->dp
6781 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6783 escape_glyph = GLYPH_CODE_CHAR (gc);
6784 lface_id = GLYPH_CODE_FACE (gc);
6786 if (lface_id)
6788 /* The display table specified a face.
6789 Merge it into face_id and also into escape_glyph. */
6790 face_id = merge_faces (it->f, Qt, lface_id,
6791 it->face_id);
6793 else if (it->f == last_escape_glyph_frame
6794 && it->face_id == last_escape_glyph_face_id)
6796 face_id = last_escape_glyph_merged_face_id;
6798 else
6800 /* Merge the escape-glyph face into the current face. */
6801 face_id = merge_faces (it->f, Qescape_glyph, 0,
6802 it->face_id);
6803 last_escape_glyph_frame = it->f;
6804 last_escape_glyph_face_id = it->face_id;
6805 last_escape_glyph_merged_face_id = face_id;
6808 /* Draw non-ASCII hyphen with just highlighting: */
6810 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6812 XSETINT (it->ctl_chars[0], '-');
6813 ctl_len = 1;
6814 goto display_control;
6817 /* Draw non-ASCII space/hyphen with escape glyph: */
6819 if (nonascii_space_p || nonascii_hyphen_p)
6821 XSETINT (it->ctl_chars[0], escape_glyph);
6822 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6823 ctl_len = 2;
6824 goto display_control;
6828 char str[10];
6829 int len, i;
6831 if (CHAR_BYTE8_P (c))
6832 /* Display \200 instead of \17777600. */
6833 c = CHAR_TO_BYTE8 (c);
6834 len = sprintf (str, "%03o", c);
6836 XSETINT (it->ctl_chars[0], escape_glyph);
6837 for (i = 0; i < len; i++)
6838 XSETINT (it->ctl_chars[i + 1], str[i]);
6839 ctl_len = len + 1;
6842 display_control:
6843 /* Set up IT->dpvec and return first character from it. */
6844 it->dpvec_char_len = it->len;
6845 it->dpvec = it->ctl_chars;
6846 it->dpend = it->dpvec + ctl_len;
6847 it->current.dpvec_index = 0;
6848 it->dpvec_face_id = face_id;
6849 it->saved_face_id = it->face_id;
6850 it->method = GET_FROM_DISPLAY_VECTOR;
6851 it->ellipsis_p = 0;
6852 goto get_next;
6854 it->char_to_display = c;
6856 else if (success_p)
6858 it->char_to_display = it->c;
6862 /* Adjust face id for a multibyte character. There are no multibyte
6863 character in unibyte text. */
6864 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6865 && it->multibyte_p
6866 && success_p
6867 && FRAME_WINDOW_P (it->f))
6869 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6871 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6873 /* Automatic composition with glyph-string. */
6874 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6876 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6878 else
6880 ptrdiff_t pos = (it->s ? -1
6881 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6882 : IT_CHARPOS (*it));
6883 int c;
6885 if (it->what == IT_CHARACTER)
6886 c = it->char_to_display;
6887 else
6889 struct composition *cmp = composition_table[it->cmp_it.id];
6890 int i;
6892 c = ' ';
6893 for (i = 0; i < cmp->glyph_len; i++)
6894 /* TAB in a composition means display glyphs with
6895 padding space on the left or right. */
6896 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6897 break;
6899 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6903 done:
6904 /* Is this character the last one of a run of characters with
6905 box? If yes, set IT->end_of_box_run_p to 1. */
6906 if (it->face_box_p
6907 && it->s == NULL)
6909 if (it->method == GET_FROM_STRING && it->sp)
6911 int face_id = underlying_face_id (it);
6912 struct face *face = FACE_FROM_ID (it->f, face_id);
6914 if (face)
6916 if (face->box == FACE_NO_BOX)
6918 /* If the box comes from face properties in a
6919 display string, check faces in that string. */
6920 int string_face_id = face_after_it_pos (it);
6921 it->end_of_box_run_p
6922 = (FACE_FROM_ID (it->f, string_face_id)->box
6923 == FACE_NO_BOX);
6925 /* Otherwise, the box comes from the underlying face.
6926 If this is the last string character displayed, check
6927 the next buffer location. */
6928 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6929 && (it->current.overlay_string_index
6930 == it->n_overlay_strings - 1))
6932 ptrdiff_t ignore;
6933 int next_face_id;
6934 struct text_pos pos = it->current.pos;
6935 INC_TEXT_POS (pos, it->multibyte_p);
6937 next_face_id = face_at_buffer_position
6938 (it->w, CHARPOS (pos), it->region_beg_charpos,
6939 it->region_end_charpos, &ignore,
6940 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6941 -1);
6942 it->end_of_box_run_p
6943 = (FACE_FROM_ID (it->f, next_face_id)->box
6944 == FACE_NO_BOX);
6948 else
6950 int face_id = face_after_it_pos (it);
6951 it->end_of_box_run_p
6952 = (face_id != it->face_id
6953 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6956 /* If we reached the end of the object we've been iterating (e.g., a
6957 display string or an overlay string), and there's something on
6958 IT->stack, proceed with what's on the stack. It doesn't make
6959 sense to return zero if there's unprocessed stuff on the stack,
6960 because otherwise that stuff will never be displayed. */
6961 if (!success_p && it->sp > 0)
6963 set_iterator_to_next (it, 0);
6964 success_p = get_next_display_element (it);
6967 /* Value is 0 if end of buffer or string reached. */
6968 return success_p;
6972 /* Move IT to the next display element.
6974 RESEAT_P non-zero means if called on a newline in buffer text,
6975 skip to the next visible line start.
6977 Functions get_next_display_element and set_iterator_to_next are
6978 separate because I find this arrangement easier to handle than a
6979 get_next_display_element function that also increments IT's
6980 position. The way it is we can first look at an iterator's current
6981 display element, decide whether it fits on a line, and if it does,
6982 increment the iterator position. The other way around we probably
6983 would either need a flag indicating whether the iterator has to be
6984 incremented the next time, or we would have to implement a
6985 decrement position function which would not be easy to write. */
6987 void
6988 set_iterator_to_next (struct it *it, int reseat_p)
6990 /* Reset flags indicating start and end of a sequence of characters
6991 with box. Reset them at the start of this function because
6992 moving the iterator to a new position might set them. */
6993 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6995 switch (it->method)
6997 case GET_FROM_BUFFER:
6998 /* The current display element of IT is a character from
6999 current_buffer. Advance in the buffer, and maybe skip over
7000 invisible lines that are so because of selective display. */
7001 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7002 reseat_at_next_visible_line_start (it, 0);
7003 else if (it->cmp_it.id >= 0)
7005 /* We are currently getting glyphs from a composition. */
7006 int i;
7008 if (! it->bidi_p)
7010 IT_CHARPOS (*it) += it->cmp_it.nchars;
7011 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7012 if (it->cmp_it.to < it->cmp_it.nglyphs)
7014 it->cmp_it.from = it->cmp_it.to;
7016 else
7018 it->cmp_it.id = -1;
7019 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7020 IT_BYTEPOS (*it),
7021 it->end_charpos, Qnil);
7024 else if (! it->cmp_it.reversed_p)
7026 /* Composition created while scanning forward. */
7027 /* Update IT's char/byte positions to point to the first
7028 character of the next grapheme cluster, or to the
7029 character visually after the current composition. */
7030 for (i = 0; i < it->cmp_it.nchars; i++)
7031 bidi_move_to_visually_next (&it->bidi_it);
7032 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7033 IT_CHARPOS (*it) = it->bidi_it.charpos;
7035 if (it->cmp_it.to < it->cmp_it.nglyphs)
7037 /* Proceed to the next grapheme cluster. */
7038 it->cmp_it.from = it->cmp_it.to;
7040 else
7042 /* No more grapheme clusters in this composition.
7043 Find the next stop position. */
7044 ptrdiff_t stop = it->end_charpos;
7045 if (it->bidi_it.scan_dir < 0)
7046 /* Now we are scanning backward and don't know
7047 where to stop. */
7048 stop = -1;
7049 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7050 IT_BYTEPOS (*it), stop, Qnil);
7053 else
7055 /* Composition created while scanning backward. */
7056 /* Update IT's char/byte positions to point to the last
7057 character of the previous grapheme cluster, or the
7058 character visually after the current composition. */
7059 for (i = 0; i < it->cmp_it.nchars; i++)
7060 bidi_move_to_visually_next (&it->bidi_it);
7061 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7062 IT_CHARPOS (*it) = it->bidi_it.charpos;
7063 if (it->cmp_it.from > 0)
7065 /* Proceed to the previous grapheme cluster. */
7066 it->cmp_it.to = it->cmp_it.from;
7068 else
7070 /* No more grapheme clusters in this composition.
7071 Find the next stop position. */
7072 ptrdiff_t stop = it->end_charpos;
7073 if (it->bidi_it.scan_dir < 0)
7074 /* Now we are scanning backward and don't know
7075 where to stop. */
7076 stop = -1;
7077 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7078 IT_BYTEPOS (*it), stop, Qnil);
7082 else
7084 eassert (it->len != 0);
7086 if (!it->bidi_p)
7088 IT_BYTEPOS (*it) += it->len;
7089 IT_CHARPOS (*it) += 1;
7091 else
7093 int prev_scan_dir = it->bidi_it.scan_dir;
7094 /* If this is a new paragraph, determine its base
7095 direction (a.k.a. its base embedding level). */
7096 if (it->bidi_it.new_paragraph)
7097 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7098 bidi_move_to_visually_next (&it->bidi_it);
7099 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7100 IT_CHARPOS (*it) = it->bidi_it.charpos;
7101 if (prev_scan_dir != it->bidi_it.scan_dir)
7103 /* As the scan direction was changed, we must
7104 re-compute the stop position for composition. */
7105 ptrdiff_t stop = it->end_charpos;
7106 if (it->bidi_it.scan_dir < 0)
7107 stop = -1;
7108 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7109 IT_BYTEPOS (*it), stop, Qnil);
7112 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7114 break;
7116 case GET_FROM_C_STRING:
7117 /* Current display element of IT is from a C string. */
7118 if (!it->bidi_p
7119 /* If the string position is beyond string's end, it means
7120 next_element_from_c_string is padding the string with
7121 blanks, in which case we bypass the bidi iterator,
7122 because it cannot deal with such virtual characters. */
7123 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7125 IT_BYTEPOS (*it) += it->len;
7126 IT_CHARPOS (*it) += 1;
7128 else
7130 bidi_move_to_visually_next (&it->bidi_it);
7131 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7132 IT_CHARPOS (*it) = it->bidi_it.charpos;
7134 break;
7136 case GET_FROM_DISPLAY_VECTOR:
7137 /* Current display element of IT is from a display table entry.
7138 Advance in the display table definition. Reset it to null if
7139 end reached, and continue with characters from buffers/
7140 strings. */
7141 ++it->current.dpvec_index;
7143 /* Restore face of the iterator to what they were before the
7144 display vector entry (these entries may contain faces). */
7145 it->face_id = it->saved_face_id;
7147 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7149 int recheck_faces = it->ellipsis_p;
7151 if (it->s)
7152 it->method = GET_FROM_C_STRING;
7153 else if (STRINGP (it->string))
7154 it->method = GET_FROM_STRING;
7155 else
7157 it->method = GET_FROM_BUFFER;
7158 it->object = it->w->contents;
7161 it->dpvec = NULL;
7162 it->current.dpvec_index = -1;
7164 /* Skip over characters which were displayed via IT->dpvec. */
7165 if (it->dpvec_char_len < 0)
7166 reseat_at_next_visible_line_start (it, 1);
7167 else if (it->dpvec_char_len > 0)
7169 if (it->method == GET_FROM_STRING
7170 && it->current.overlay_string_index >= 0
7171 && it->n_overlay_strings > 0)
7172 it->ignore_overlay_strings_at_pos_p = 1;
7173 it->len = it->dpvec_char_len;
7174 set_iterator_to_next (it, reseat_p);
7177 /* Maybe recheck faces after display vector */
7178 if (recheck_faces)
7179 it->stop_charpos = IT_CHARPOS (*it);
7181 break;
7183 case GET_FROM_STRING:
7184 /* Current display element is a character from a Lisp string. */
7185 eassert (it->s == NULL && STRINGP (it->string));
7186 /* Don't advance past string end. These conditions are true
7187 when set_iterator_to_next is called at the end of
7188 get_next_display_element, in which case the Lisp string is
7189 already exhausted, and all we want is pop the iterator
7190 stack. */
7191 if (it->current.overlay_string_index >= 0)
7193 /* This is an overlay string, so there's no padding with
7194 spaces, and the number of characters in the string is
7195 where the string ends. */
7196 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7197 goto consider_string_end;
7199 else
7201 /* Not an overlay string. There could be padding, so test
7202 against it->end_charpos . */
7203 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7204 goto consider_string_end;
7206 if (it->cmp_it.id >= 0)
7208 int i;
7210 if (! it->bidi_p)
7212 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7213 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7214 if (it->cmp_it.to < it->cmp_it.nglyphs)
7215 it->cmp_it.from = it->cmp_it.to;
7216 else
7218 it->cmp_it.id = -1;
7219 composition_compute_stop_pos (&it->cmp_it,
7220 IT_STRING_CHARPOS (*it),
7221 IT_STRING_BYTEPOS (*it),
7222 it->end_charpos, it->string);
7225 else if (! it->cmp_it.reversed_p)
7227 for (i = 0; i < it->cmp_it.nchars; i++)
7228 bidi_move_to_visually_next (&it->bidi_it);
7229 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7230 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7232 if (it->cmp_it.to < it->cmp_it.nglyphs)
7233 it->cmp_it.from = it->cmp_it.to;
7234 else
7236 ptrdiff_t stop = it->end_charpos;
7237 if (it->bidi_it.scan_dir < 0)
7238 stop = -1;
7239 composition_compute_stop_pos (&it->cmp_it,
7240 IT_STRING_CHARPOS (*it),
7241 IT_STRING_BYTEPOS (*it), stop,
7242 it->string);
7245 else
7247 for (i = 0; i < it->cmp_it.nchars; i++)
7248 bidi_move_to_visually_next (&it->bidi_it);
7249 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7250 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7251 if (it->cmp_it.from > 0)
7252 it->cmp_it.to = it->cmp_it.from;
7253 else
7255 ptrdiff_t stop = it->end_charpos;
7256 if (it->bidi_it.scan_dir < 0)
7257 stop = -1;
7258 composition_compute_stop_pos (&it->cmp_it,
7259 IT_STRING_CHARPOS (*it),
7260 IT_STRING_BYTEPOS (*it), stop,
7261 it->string);
7265 else
7267 if (!it->bidi_p
7268 /* If the string position is beyond string's end, it
7269 means next_element_from_string is padding the string
7270 with blanks, in which case we bypass the bidi
7271 iterator, because it cannot deal with such virtual
7272 characters. */
7273 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7275 IT_STRING_BYTEPOS (*it) += it->len;
7276 IT_STRING_CHARPOS (*it) += 1;
7278 else
7280 int prev_scan_dir = it->bidi_it.scan_dir;
7282 bidi_move_to_visually_next (&it->bidi_it);
7283 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7284 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7285 if (prev_scan_dir != it->bidi_it.scan_dir)
7287 ptrdiff_t stop = it->end_charpos;
7289 if (it->bidi_it.scan_dir < 0)
7290 stop = -1;
7291 composition_compute_stop_pos (&it->cmp_it,
7292 IT_STRING_CHARPOS (*it),
7293 IT_STRING_BYTEPOS (*it), stop,
7294 it->string);
7299 consider_string_end:
7301 if (it->current.overlay_string_index >= 0)
7303 /* IT->string is an overlay string. Advance to the
7304 next, if there is one. */
7305 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7307 it->ellipsis_p = 0;
7308 next_overlay_string (it);
7309 if (it->ellipsis_p)
7310 setup_for_ellipsis (it, 0);
7313 else
7315 /* IT->string is not an overlay string. If we reached
7316 its end, and there is something on IT->stack, proceed
7317 with what is on the stack. This can be either another
7318 string, this time an overlay string, or a buffer. */
7319 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7320 && it->sp > 0)
7322 pop_it (it);
7323 if (it->method == GET_FROM_STRING)
7324 goto consider_string_end;
7327 break;
7329 case GET_FROM_IMAGE:
7330 case GET_FROM_STRETCH:
7331 /* The position etc with which we have to proceed are on
7332 the stack. The position may be at the end of a string,
7333 if the `display' property takes up the whole string. */
7334 eassert (it->sp > 0);
7335 pop_it (it);
7336 if (it->method == GET_FROM_STRING)
7337 goto consider_string_end;
7338 break;
7340 default:
7341 /* There are no other methods defined, so this should be a bug. */
7342 emacs_abort ();
7345 eassert (it->method != GET_FROM_STRING
7346 || (STRINGP (it->string)
7347 && IT_STRING_CHARPOS (*it) >= 0));
7350 /* Load IT's display element fields with information about the next
7351 display element which comes from a display table entry or from the
7352 result of translating a control character to one of the forms `^C'
7353 or `\003'.
7355 IT->dpvec holds the glyphs to return as characters.
7356 IT->saved_face_id holds the face id before the display vector--it
7357 is restored into IT->face_id in set_iterator_to_next. */
7359 static int
7360 next_element_from_display_vector (struct it *it)
7362 Lisp_Object gc;
7364 /* Precondition. */
7365 eassert (it->dpvec && it->current.dpvec_index >= 0);
7367 it->face_id = it->saved_face_id;
7369 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7370 That seemed totally bogus - so I changed it... */
7371 gc = it->dpvec[it->current.dpvec_index];
7373 if (GLYPH_CODE_P (gc))
7375 it->c = GLYPH_CODE_CHAR (gc);
7376 it->len = CHAR_BYTES (it->c);
7378 /* The entry may contain a face id to use. Such a face id is
7379 the id of a Lisp face, not a realized face. A face id of
7380 zero means no face is specified. */
7381 if (it->dpvec_face_id >= 0)
7382 it->face_id = it->dpvec_face_id;
7383 else
7385 int lface_id = GLYPH_CODE_FACE (gc);
7386 if (lface_id > 0)
7387 it->face_id = merge_faces (it->f, Qt, lface_id,
7388 it->saved_face_id);
7391 else
7392 /* Display table entry is invalid. Return a space. */
7393 it->c = ' ', it->len = 1;
7395 /* Don't change position and object of the iterator here. They are
7396 still the values of the character that had this display table
7397 entry or was translated, and that's what we want. */
7398 it->what = IT_CHARACTER;
7399 return 1;
7402 /* Get the first element of string/buffer in the visual order, after
7403 being reseated to a new position in a string or a buffer. */
7404 static void
7405 get_visually_first_element (struct it *it)
7407 int string_p = STRINGP (it->string) || it->s;
7408 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7409 ptrdiff_t bob = (string_p ? 0 : BEGV);
7411 if (STRINGP (it->string))
7413 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7414 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7416 else
7418 it->bidi_it.charpos = IT_CHARPOS (*it);
7419 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7422 if (it->bidi_it.charpos == eob)
7424 /* Nothing to do, but reset the FIRST_ELT flag, like
7425 bidi_paragraph_init does, because we are not going to
7426 call it. */
7427 it->bidi_it.first_elt = 0;
7429 else if (it->bidi_it.charpos == bob
7430 || (!string_p
7431 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7432 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7434 /* If we are at the beginning of a line/string, we can produce
7435 the next element right away. */
7436 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7437 bidi_move_to_visually_next (&it->bidi_it);
7439 else
7441 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7443 /* We need to prime the bidi iterator starting at the line's or
7444 string's beginning, before we will be able to produce the
7445 next element. */
7446 if (string_p)
7447 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7448 else
7449 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7450 IT_BYTEPOS (*it), -1,
7451 &it->bidi_it.bytepos);
7452 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7455 /* Now return to buffer/string position where we were asked
7456 to get the next display element, and produce that. */
7457 bidi_move_to_visually_next (&it->bidi_it);
7459 while (it->bidi_it.bytepos != orig_bytepos
7460 && it->bidi_it.charpos < eob);
7463 /* Adjust IT's position information to where we ended up. */
7464 if (STRINGP (it->string))
7466 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7467 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7469 else
7471 IT_CHARPOS (*it) = it->bidi_it.charpos;
7472 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7475 if (STRINGP (it->string) || !it->s)
7477 ptrdiff_t stop, charpos, bytepos;
7479 if (STRINGP (it->string))
7481 eassert (!it->s);
7482 stop = SCHARS (it->string);
7483 if (stop > it->end_charpos)
7484 stop = it->end_charpos;
7485 charpos = IT_STRING_CHARPOS (*it);
7486 bytepos = IT_STRING_BYTEPOS (*it);
7488 else
7490 stop = it->end_charpos;
7491 charpos = IT_CHARPOS (*it);
7492 bytepos = IT_BYTEPOS (*it);
7494 if (it->bidi_it.scan_dir < 0)
7495 stop = -1;
7496 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7497 it->string);
7501 /* Load IT with the next display element from Lisp string IT->string.
7502 IT->current.string_pos is the current position within the string.
7503 If IT->current.overlay_string_index >= 0, the Lisp string is an
7504 overlay string. */
7506 static int
7507 next_element_from_string (struct it *it)
7509 struct text_pos position;
7511 eassert (STRINGP (it->string));
7512 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7513 eassert (IT_STRING_CHARPOS (*it) >= 0);
7514 position = it->current.string_pos;
7516 /* With bidi reordering, the character to display might not be the
7517 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7518 that we were reseat()ed to a new string, whose paragraph
7519 direction is not known. */
7520 if (it->bidi_p && it->bidi_it.first_elt)
7522 get_visually_first_element (it);
7523 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7526 /* Time to check for invisible text? */
7527 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7529 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7531 if (!(!it->bidi_p
7532 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7533 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7535 /* With bidi non-linear iteration, we could find
7536 ourselves far beyond the last computed stop_charpos,
7537 with several other stop positions in between that we
7538 missed. Scan them all now, in buffer's logical
7539 order, until we find and handle the last stop_charpos
7540 that precedes our current position. */
7541 handle_stop_backwards (it, it->stop_charpos);
7542 return GET_NEXT_DISPLAY_ELEMENT (it);
7544 else
7546 if (it->bidi_p)
7548 /* Take note of the stop position we just moved
7549 across, for when we will move back across it. */
7550 it->prev_stop = it->stop_charpos;
7551 /* If we are at base paragraph embedding level, take
7552 note of the last stop position seen at this
7553 level. */
7554 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7555 it->base_level_stop = it->stop_charpos;
7557 handle_stop (it);
7559 /* Since a handler may have changed IT->method, we must
7560 recurse here. */
7561 return GET_NEXT_DISPLAY_ELEMENT (it);
7564 else if (it->bidi_p
7565 /* If we are before prev_stop, we may have overstepped
7566 on our way backwards a stop_pos, and if so, we need
7567 to handle that stop_pos. */
7568 && IT_STRING_CHARPOS (*it) < it->prev_stop
7569 /* We can sometimes back up for reasons that have nothing
7570 to do with bidi reordering. E.g., compositions. The
7571 code below is only needed when we are above the base
7572 embedding level, so test for that explicitly. */
7573 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7575 /* If we lost track of base_level_stop, we have no better
7576 place for handle_stop_backwards to start from than string
7577 beginning. This happens, e.g., when we were reseated to
7578 the previous screenful of text by vertical-motion. */
7579 if (it->base_level_stop <= 0
7580 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7581 it->base_level_stop = 0;
7582 handle_stop_backwards (it, it->base_level_stop);
7583 return GET_NEXT_DISPLAY_ELEMENT (it);
7587 if (it->current.overlay_string_index >= 0)
7589 /* Get the next character from an overlay string. In overlay
7590 strings, there is no field width or padding with spaces to
7591 do. */
7592 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7594 it->what = IT_EOB;
7595 return 0;
7597 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7598 IT_STRING_BYTEPOS (*it),
7599 it->bidi_it.scan_dir < 0
7600 ? -1
7601 : SCHARS (it->string))
7602 && next_element_from_composition (it))
7604 return 1;
7606 else if (STRING_MULTIBYTE (it->string))
7608 const unsigned char *s = (SDATA (it->string)
7609 + IT_STRING_BYTEPOS (*it));
7610 it->c = string_char_and_length (s, &it->len);
7612 else
7614 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7615 it->len = 1;
7618 else
7620 /* Get the next character from a Lisp string that is not an
7621 overlay string. Such strings come from the mode line, for
7622 example. We may have to pad with spaces, or truncate the
7623 string. See also next_element_from_c_string. */
7624 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7626 it->what = IT_EOB;
7627 return 0;
7629 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7631 /* Pad with spaces. */
7632 it->c = ' ', it->len = 1;
7633 CHARPOS (position) = BYTEPOS (position) = -1;
7635 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7636 IT_STRING_BYTEPOS (*it),
7637 it->bidi_it.scan_dir < 0
7638 ? -1
7639 : it->string_nchars)
7640 && next_element_from_composition (it))
7642 return 1;
7644 else if (STRING_MULTIBYTE (it->string))
7646 const unsigned char *s = (SDATA (it->string)
7647 + IT_STRING_BYTEPOS (*it));
7648 it->c = string_char_and_length (s, &it->len);
7650 else
7652 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7653 it->len = 1;
7657 /* Record what we have and where it came from. */
7658 it->what = IT_CHARACTER;
7659 it->object = it->string;
7660 it->position = position;
7661 return 1;
7665 /* Load IT with next display element from C string IT->s.
7666 IT->string_nchars is the maximum number of characters to return
7667 from the string. IT->end_charpos may be greater than
7668 IT->string_nchars when this function is called, in which case we
7669 may have to return padding spaces. Value is zero if end of string
7670 reached, including padding spaces. */
7672 static int
7673 next_element_from_c_string (struct it *it)
7675 int success_p = 1;
7677 eassert (it->s);
7678 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7679 it->what = IT_CHARACTER;
7680 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7681 it->object = Qnil;
7683 /* With bidi reordering, the character to display might not be the
7684 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7685 we were reseated to a new string, whose paragraph direction is
7686 not known. */
7687 if (it->bidi_p && it->bidi_it.first_elt)
7688 get_visually_first_element (it);
7690 /* IT's position can be greater than IT->string_nchars in case a
7691 field width or precision has been specified when the iterator was
7692 initialized. */
7693 if (IT_CHARPOS (*it) >= it->end_charpos)
7695 /* End of the game. */
7696 it->what = IT_EOB;
7697 success_p = 0;
7699 else if (IT_CHARPOS (*it) >= it->string_nchars)
7701 /* Pad with spaces. */
7702 it->c = ' ', it->len = 1;
7703 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7705 else if (it->multibyte_p)
7706 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7707 else
7708 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7710 return success_p;
7714 /* Set up IT to return characters from an ellipsis, if appropriate.
7715 The definition of the ellipsis glyphs may come from a display table
7716 entry. This function fills IT with the first glyph from the
7717 ellipsis if an ellipsis is to be displayed. */
7719 static int
7720 next_element_from_ellipsis (struct it *it)
7722 if (it->selective_display_ellipsis_p)
7723 setup_for_ellipsis (it, it->len);
7724 else
7726 /* The face at the current position may be different from the
7727 face we find after the invisible text. Remember what it
7728 was in IT->saved_face_id, and signal that it's there by
7729 setting face_before_selective_p. */
7730 it->saved_face_id = it->face_id;
7731 it->method = GET_FROM_BUFFER;
7732 it->object = it->w->contents;
7733 reseat_at_next_visible_line_start (it, 1);
7734 it->face_before_selective_p = 1;
7737 return GET_NEXT_DISPLAY_ELEMENT (it);
7741 /* Deliver an image display element. The iterator IT is already
7742 filled with image information (done in handle_display_prop). Value
7743 is always 1. */
7746 static int
7747 next_element_from_image (struct it *it)
7749 it->what = IT_IMAGE;
7750 it->ignore_overlay_strings_at_pos_p = 0;
7751 return 1;
7755 /* Fill iterator IT with next display element from a stretch glyph
7756 property. IT->object is the value of the text property. Value is
7757 always 1. */
7759 static int
7760 next_element_from_stretch (struct it *it)
7762 it->what = IT_STRETCH;
7763 return 1;
7766 /* Scan backwards from IT's current position until we find a stop
7767 position, or until BEGV. This is called when we find ourself
7768 before both the last known prev_stop and base_level_stop while
7769 reordering bidirectional text. */
7771 static void
7772 compute_stop_pos_backwards (struct it *it)
7774 const int SCAN_BACK_LIMIT = 1000;
7775 struct text_pos pos;
7776 struct display_pos save_current = it->current;
7777 struct text_pos save_position = it->position;
7778 ptrdiff_t charpos = IT_CHARPOS (*it);
7779 ptrdiff_t where_we_are = charpos;
7780 ptrdiff_t save_stop_pos = it->stop_charpos;
7781 ptrdiff_t save_end_pos = it->end_charpos;
7783 eassert (NILP (it->string) && !it->s);
7784 eassert (it->bidi_p);
7785 it->bidi_p = 0;
7788 it->end_charpos = min (charpos + 1, ZV);
7789 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7790 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7791 reseat_1 (it, pos, 0);
7792 compute_stop_pos (it);
7793 /* We must advance forward, right? */
7794 if (it->stop_charpos <= charpos)
7795 emacs_abort ();
7797 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7799 if (it->stop_charpos <= where_we_are)
7800 it->prev_stop = it->stop_charpos;
7801 else
7802 it->prev_stop = BEGV;
7803 it->bidi_p = 1;
7804 it->current = save_current;
7805 it->position = save_position;
7806 it->stop_charpos = save_stop_pos;
7807 it->end_charpos = save_end_pos;
7810 /* Scan forward from CHARPOS in the current buffer/string, until we
7811 find a stop position > current IT's position. Then handle the stop
7812 position before that. This is called when we bump into a stop
7813 position while reordering bidirectional text. CHARPOS should be
7814 the last previously processed stop_pos (or BEGV/0, if none were
7815 processed yet) whose position is less that IT's current
7816 position. */
7818 static void
7819 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7821 int bufp = !STRINGP (it->string);
7822 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7823 struct display_pos save_current = it->current;
7824 struct text_pos save_position = it->position;
7825 struct text_pos pos1;
7826 ptrdiff_t next_stop;
7828 /* Scan in strict logical order. */
7829 eassert (it->bidi_p);
7830 it->bidi_p = 0;
7833 it->prev_stop = charpos;
7834 if (bufp)
7836 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7837 reseat_1 (it, pos1, 0);
7839 else
7840 it->current.string_pos = string_pos (charpos, it->string);
7841 compute_stop_pos (it);
7842 /* We must advance forward, right? */
7843 if (it->stop_charpos <= it->prev_stop)
7844 emacs_abort ();
7845 charpos = it->stop_charpos;
7847 while (charpos <= where_we_are);
7849 it->bidi_p = 1;
7850 it->current = save_current;
7851 it->position = save_position;
7852 next_stop = it->stop_charpos;
7853 it->stop_charpos = it->prev_stop;
7854 handle_stop (it);
7855 it->stop_charpos = next_stop;
7858 /* Load IT with the next display element from current_buffer. Value
7859 is zero if end of buffer reached. IT->stop_charpos is the next
7860 position at which to stop and check for text properties or buffer
7861 end. */
7863 static int
7864 next_element_from_buffer (struct it *it)
7866 int success_p = 1;
7868 eassert (IT_CHARPOS (*it) >= BEGV);
7869 eassert (NILP (it->string) && !it->s);
7870 eassert (!it->bidi_p
7871 || (EQ (it->bidi_it.string.lstring, Qnil)
7872 && it->bidi_it.string.s == NULL));
7874 /* With bidi reordering, the character to display might not be the
7875 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7876 we were reseat()ed to a new buffer position, which is potentially
7877 a different paragraph. */
7878 if (it->bidi_p && it->bidi_it.first_elt)
7880 get_visually_first_element (it);
7881 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7884 if (IT_CHARPOS (*it) >= it->stop_charpos)
7886 if (IT_CHARPOS (*it) >= it->end_charpos)
7888 int overlay_strings_follow_p;
7890 /* End of the game, except when overlay strings follow that
7891 haven't been returned yet. */
7892 if (it->overlay_strings_at_end_processed_p)
7893 overlay_strings_follow_p = 0;
7894 else
7896 it->overlay_strings_at_end_processed_p = 1;
7897 overlay_strings_follow_p = get_overlay_strings (it, 0);
7900 if (overlay_strings_follow_p)
7901 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7902 else
7904 it->what = IT_EOB;
7905 it->position = it->current.pos;
7906 success_p = 0;
7909 else if (!(!it->bidi_p
7910 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7911 || IT_CHARPOS (*it) == it->stop_charpos))
7913 /* With bidi non-linear iteration, we could find ourselves
7914 far beyond the last computed stop_charpos, with several
7915 other stop positions in between that we missed. Scan
7916 them all now, in buffer's logical order, until we find
7917 and handle the last stop_charpos that precedes our
7918 current position. */
7919 handle_stop_backwards (it, it->stop_charpos);
7920 return GET_NEXT_DISPLAY_ELEMENT (it);
7922 else
7924 if (it->bidi_p)
7926 /* Take note of the stop position we just moved across,
7927 for when we will move back across it. */
7928 it->prev_stop = it->stop_charpos;
7929 /* If we are at base paragraph embedding level, take
7930 note of the last stop position seen at this
7931 level. */
7932 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7933 it->base_level_stop = it->stop_charpos;
7935 handle_stop (it);
7936 return GET_NEXT_DISPLAY_ELEMENT (it);
7939 else if (it->bidi_p
7940 /* If we are before prev_stop, we may have overstepped on
7941 our way backwards a stop_pos, and if so, we need to
7942 handle that stop_pos. */
7943 && IT_CHARPOS (*it) < it->prev_stop
7944 /* We can sometimes back up for reasons that have nothing
7945 to do with bidi reordering. E.g., compositions. The
7946 code below is only needed when we are above the base
7947 embedding level, so test for that explicitly. */
7948 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7950 if (it->base_level_stop <= 0
7951 || IT_CHARPOS (*it) < it->base_level_stop)
7953 /* If we lost track of base_level_stop, we need to find
7954 prev_stop by looking backwards. This happens, e.g., when
7955 we were reseated to the previous screenful of text by
7956 vertical-motion. */
7957 it->base_level_stop = BEGV;
7958 compute_stop_pos_backwards (it);
7959 handle_stop_backwards (it, it->prev_stop);
7961 else
7962 handle_stop_backwards (it, it->base_level_stop);
7963 return GET_NEXT_DISPLAY_ELEMENT (it);
7965 else
7967 /* No face changes, overlays etc. in sight, so just return a
7968 character from current_buffer. */
7969 unsigned char *p;
7970 ptrdiff_t stop;
7972 /* Maybe run the redisplay end trigger hook. Performance note:
7973 This doesn't seem to cost measurable time. */
7974 if (it->redisplay_end_trigger_charpos
7975 && it->glyph_row
7976 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7977 run_redisplay_end_trigger_hook (it);
7979 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7980 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7981 stop)
7982 && next_element_from_composition (it))
7984 return 1;
7987 /* Get the next character, maybe multibyte. */
7988 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7989 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7990 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7991 else
7992 it->c = *p, it->len = 1;
7994 /* Record what we have and where it came from. */
7995 it->what = IT_CHARACTER;
7996 it->object = it->w->contents;
7997 it->position = it->current.pos;
7999 /* Normally we return the character found above, except when we
8000 really want to return an ellipsis for selective display. */
8001 if (it->selective)
8003 if (it->c == '\n')
8005 /* A value of selective > 0 means hide lines indented more
8006 than that number of columns. */
8007 if (it->selective > 0
8008 && IT_CHARPOS (*it) + 1 < ZV
8009 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8010 IT_BYTEPOS (*it) + 1,
8011 it->selective))
8013 success_p = next_element_from_ellipsis (it);
8014 it->dpvec_char_len = -1;
8017 else if (it->c == '\r' && it->selective == -1)
8019 /* A value of selective == -1 means that everything from the
8020 CR to the end of the line is invisible, with maybe an
8021 ellipsis displayed for it. */
8022 success_p = next_element_from_ellipsis (it);
8023 it->dpvec_char_len = -1;
8028 /* Value is zero if end of buffer reached. */
8029 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8030 return success_p;
8034 /* Run the redisplay end trigger hook for IT. */
8036 static void
8037 run_redisplay_end_trigger_hook (struct it *it)
8039 Lisp_Object args[3];
8041 /* IT->glyph_row should be non-null, i.e. we should be actually
8042 displaying something, or otherwise we should not run the hook. */
8043 eassert (it->glyph_row);
8045 /* Set up hook arguments. */
8046 args[0] = Qredisplay_end_trigger_functions;
8047 args[1] = it->window;
8048 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8049 it->redisplay_end_trigger_charpos = 0;
8051 /* Since we are *trying* to run these functions, don't try to run
8052 them again, even if they get an error. */
8053 wset_redisplay_end_trigger (it->w, Qnil);
8054 Frun_hook_with_args (3, args);
8056 /* Notice if it changed the face of the character we are on. */
8057 handle_face_prop (it);
8061 /* Deliver a composition display element. Unlike the other
8062 next_element_from_XXX, this function is not registered in the array
8063 get_next_element[]. It is called from next_element_from_buffer and
8064 next_element_from_string when necessary. */
8066 static int
8067 next_element_from_composition (struct it *it)
8069 it->what = IT_COMPOSITION;
8070 it->len = it->cmp_it.nbytes;
8071 if (STRINGP (it->string))
8073 if (it->c < 0)
8075 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8076 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8077 return 0;
8079 it->position = it->current.string_pos;
8080 it->object = it->string;
8081 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8082 IT_STRING_BYTEPOS (*it), it->string);
8084 else
8086 if (it->c < 0)
8088 IT_CHARPOS (*it) += it->cmp_it.nchars;
8089 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8090 if (it->bidi_p)
8092 if (it->bidi_it.new_paragraph)
8093 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8094 /* Resync the bidi iterator with IT's new position.
8095 FIXME: this doesn't support bidirectional text. */
8096 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8097 bidi_move_to_visually_next (&it->bidi_it);
8099 return 0;
8101 it->position = it->current.pos;
8102 it->object = it->w->contents;
8103 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8104 IT_BYTEPOS (*it), Qnil);
8106 return 1;
8111 /***********************************************************************
8112 Moving an iterator without producing glyphs
8113 ***********************************************************************/
8115 /* Check if iterator is at a position corresponding to a valid buffer
8116 position after some move_it_ call. */
8118 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8119 ((it)->method == GET_FROM_STRING \
8120 ? IT_STRING_CHARPOS (*it) == 0 \
8121 : 1)
8124 /* Move iterator IT to a specified buffer or X position within one
8125 line on the display without producing glyphs.
8127 OP should be a bit mask including some or all of these bits:
8128 MOVE_TO_X: Stop upon reaching x-position TO_X.
8129 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8130 Regardless of OP's value, stop upon reaching the end of the display line.
8132 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8133 This means, in particular, that TO_X includes window's horizontal
8134 scroll amount.
8136 The return value has several possible values that
8137 say what condition caused the scan to stop:
8139 MOVE_POS_MATCH_OR_ZV
8140 - when TO_POS or ZV was reached.
8142 MOVE_X_REACHED
8143 -when TO_X was reached before TO_POS or ZV were reached.
8145 MOVE_LINE_CONTINUED
8146 - when we reached the end of the display area and the line must
8147 be continued.
8149 MOVE_LINE_TRUNCATED
8150 - when we reached the end of the display area and the line is
8151 truncated.
8153 MOVE_NEWLINE_OR_CR
8154 - when we stopped at a line end, i.e. a newline or a CR and selective
8155 display is on. */
8157 static enum move_it_result
8158 move_it_in_display_line_to (struct it *it,
8159 ptrdiff_t to_charpos, int to_x,
8160 enum move_operation_enum op)
8162 enum move_it_result result = MOVE_UNDEFINED;
8163 struct glyph_row *saved_glyph_row;
8164 struct it wrap_it, atpos_it, atx_it, ppos_it;
8165 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8166 void *ppos_data = NULL;
8167 int may_wrap = 0;
8168 enum it_method prev_method = it->method;
8169 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8170 int saw_smaller_pos = prev_pos < to_charpos;
8172 /* Don't produce glyphs in produce_glyphs. */
8173 saved_glyph_row = it->glyph_row;
8174 it->glyph_row = NULL;
8176 /* Use wrap_it to save a copy of IT wherever a word wrap could
8177 occur. Use atpos_it to save a copy of IT at the desired buffer
8178 position, if found, so that we can scan ahead and check if the
8179 word later overshoots the window edge. Use atx_it similarly, for
8180 pixel positions. */
8181 wrap_it.sp = -1;
8182 atpos_it.sp = -1;
8183 atx_it.sp = -1;
8185 /* Use ppos_it under bidi reordering to save a copy of IT for the
8186 position > CHARPOS that is the closest to CHARPOS. We restore
8187 that position in IT when we have scanned the entire display line
8188 without finding a match for CHARPOS and all the character
8189 positions are greater than CHARPOS. */
8190 if (it->bidi_p)
8192 SAVE_IT (ppos_it, *it, ppos_data);
8193 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8194 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8195 SAVE_IT (ppos_it, *it, ppos_data);
8198 #define BUFFER_POS_REACHED_P() \
8199 ((op & MOVE_TO_POS) != 0 \
8200 && BUFFERP (it->object) \
8201 && (IT_CHARPOS (*it) == to_charpos \
8202 || ((!it->bidi_p \
8203 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8204 && IT_CHARPOS (*it) > to_charpos) \
8205 || (it->what == IT_COMPOSITION \
8206 && ((IT_CHARPOS (*it) > to_charpos \
8207 && to_charpos >= it->cmp_it.charpos) \
8208 || (IT_CHARPOS (*it) < to_charpos \
8209 && to_charpos <= it->cmp_it.charpos)))) \
8210 && (it->method == GET_FROM_BUFFER \
8211 || (it->method == GET_FROM_DISPLAY_VECTOR \
8212 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8214 /* If there's a line-/wrap-prefix, handle it. */
8215 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8216 && it->current_y < it->last_visible_y)
8217 handle_line_prefix (it);
8219 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8220 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8222 while (1)
8224 int x, i, ascent = 0, descent = 0;
8226 /* Utility macro to reset an iterator with x, ascent, and descent. */
8227 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8228 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8229 (IT)->max_descent = descent)
8231 /* Stop if we move beyond TO_CHARPOS (after an image or a
8232 display string or stretch glyph). */
8233 if ((op & MOVE_TO_POS) != 0
8234 && BUFFERP (it->object)
8235 && it->method == GET_FROM_BUFFER
8236 && (((!it->bidi_p
8237 /* When the iterator is at base embedding level, we
8238 are guaranteed that characters are delivered for
8239 display in strictly increasing order of their
8240 buffer positions. */
8241 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8242 && IT_CHARPOS (*it) > to_charpos)
8243 || (it->bidi_p
8244 && (prev_method == GET_FROM_IMAGE
8245 || prev_method == GET_FROM_STRETCH
8246 || prev_method == GET_FROM_STRING)
8247 /* Passed TO_CHARPOS from left to right. */
8248 && ((prev_pos < to_charpos
8249 && IT_CHARPOS (*it) > to_charpos)
8250 /* Passed TO_CHARPOS from right to left. */
8251 || (prev_pos > to_charpos
8252 && IT_CHARPOS (*it) < to_charpos)))))
8254 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8256 result = MOVE_POS_MATCH_OR_ZV;
8257 break;
8259 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8260 /* If wrap_it is valid, the current position might be in a
8261 word that is wrapped. So, save the iterator in
8262 atpos_it and continue to see if wrapping happens. */
8263 SAVE_IT (atpos_it, *it, atpos_data);
8266 /* Stop when ZV reached.
8267 We used to stop here when TO_CHARPOS reached as well, but that is
8268 too soon if this glyph does not fit on this line. So we handle it
8269 explicitly below. */
8270 if (!get_next_display_element (it))
8272 result = MOVE_POS_MATCH_OR_ZV;
8273 break;
8276 if (it->line_wrap == TRUNCATE)
8278 if (BUFFER_POS_REACHED_P ())
8280 result = MOVE_POS_MATCH_OR_ZV;
8281 break;
8284 else
8286 if (it->line_wrap == WORD_WRAP)
8288 if (IT_DISPLAYING_WHITESPACE (it))
8289 may_wrap = 1;
8290 else if (may_wrap)
8292 /* We have reached a glyph that follows one or more
8293 whitespace characters. If the position is
8294 already found, we are done. */
8295 if (atpos_it.sp >= 0)
8297 RESTORE_IT (it, &atpos_it, atpos_data);
8298 result = MOVE_POS_MATCH_OR_ZV;
8299 goto done;
8301 if (atx_it.sp >= 0)
8303 RESTORE_IT (it, &atx_it, atx_data);
8304 result = MOVE_X_REACHED;
8305 goto done;
8307 /* Otherwise, we can wrap here. */
8308 SAVE_IT (wrap_it, *it, wrap_data);
8309 may_wrap = 0;
8314 /* Remember the line height for the current line, in case
8315 the next element doesn't fit on the line. */
8316 ascent = it->max_ascent;
8317 descent = it->max_descent;
8319 /* The call to produce_glyphs will get the metrics of the
8320 display element IT is loaded with. Record the x-position
8321 before this display element, in case it doesn't fit on the
8322 line. */
8323 x = it->current_x;
8325 PRODUCE_GLYPHS (it);
8327 if (it->area != TEXT_AREA)
8329 prev_method = it->method;
8330 if (it->method == GET_FROM_BUFFER)
8331 prev_pos = IT_CHARPOS (*it);
8332 set_iterator_to_next (it, 1);
8333 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8334 SET_TEXT_POS (this_line_min_pos,
8335 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8336 if (it->bidi_p
8337 && (op & MOVE_TO_POS)
8338 && IT_CHARPOS (*it) > to_charpos
8339 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8340 SAVE_IT (ppos_it, *it, ppos_data);
8341 continue;
8344 /* The number of glyphs we get back in IT->nglyphs will normally
8345 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8346 character on a terminal frame, or (iii) a line end. For the
8347 second case, IT->nglyphs - 1 padding glyphs will be present.
8348 (On X frames, there is only one glyph produced for a
8349 composite character.)
8351 The behavior implemented below means, for continuation lines,
8352 that as many spaces of a TAB as fit on the current line are
8353 displayed there. For terminal frames, as many glyphs of a
8354 multi-glyph character are displayed in the current line, too.
8355 This is what the old redisplay code did, and we keep it that
8356 way. Under X, the whole shape of a complex character must
8357 fit on the line or it will be completely displayed in the
8358 next line.
8360 Note that both for tabs and padding glyphs, all glyphs have
8361 the same width. */
8362 if (it->nglyphs)
8364 /* More than one glyph or glyph doesn't fit on line. All
8365 glyphs have the same width. */
8366 int single_glyph_width = it->pixel_width / it->nglyphs;
8367 int new_x;
8368 int x_before_this_char = x;
8369 int hpos_before_this_char = it->hpos;
8371 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8373 new_x = x + single_glyph_width;
8375 /* We want to leave anything reaching TO_X to the caller. */
8376 if ((op & MOVE_TO_X) && new_x > to_x)
8378 if (BUFFER_POS_REACHED_P ())
8380 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8381 goto buffer_pos_reached;
8382 if (atpos_it.sp < 0)
8384 SAVE_IT (atpos_it, *it, atpos_data);
8385 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8388 else
8390 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8392 it->current_x = x;
8393 result = MOVE_X_REACHED;
8394 break;
8396 if (atx_it.sp < 0)
8398 SAVE_IT (atx_it, *it, atx_data);
8399 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8404 if (/* Lines are continued. */
8405 it->line_wrap != TRUNCATE
8406 && (/* And glyph doesn't fit on the line. */
8407 new_x > it->last_visible_x
8408 /* Or it fits exactly and we're on a window
8409 system frame. */
8410 || (new_x == it->last_visible_x
8411 && FRAME_WINDOW_P (it->f)
8412 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8413 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8414 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8416 if (/* IT->hpos == 0 means the very first glyph
8417 doesn't fit on the line, e.g. a wide image. */
8418 it->hpos == 0
8419 || (new_x == it->last_visible_x
8420 && FRAME_WINDOW_P (it->f)))
8422 ++it->hpos;
8423 it->current_x = new_x;
8425 /* The character's last glyph just barely fits
8426 in this row. */
8427 if (i == it->nglyphs - 1)
8429 /* If this is the destination position,
8430 return a position *before* it in this row,
8431 now that we know it fits in this row. */
8432 if (BUFFER_POS_REACHED_P ())
8434 if (it->line_wrap != WORD_WRAP
8435 || wrap_it.sp < 0)
8437 it->hpos = hpos_before_this_char;
8438 it->current_x = x_before_this_char;
8439 result = MOVE_POS_MATCH_OR_ZV;
8440 break;
8442 if (it->line_wrap == WORD_WRAP
8443 && atpos_it.sp < 0)
8445 SAVE_IT (atpos_it, *it, atpos_data);
8446 atpos_it.current_x = x_before_this_char;
8447 atpos_it.hpos = hpos_before_this_char;
8451 prev_method = it->method;
8452 if (it->method == GET_FROM_BUFFER)
8453 prev_pos = IT_CHARPOS (*it);
8454 set_iterator_to_next (it, 1);
8455 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8456 SET_TEXT_POS (this_line_min_pos,
8457 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8458 /* On graphical terminals, newlines may
8459 "overflow" into the fringe if
8460 overflow-newline-into-fringe is non-nil.
8461 On text terminals, and on graphical
8462 terminals with no right margin, newlines
8463 may overflow into the last glyph on the
8464 display line.*/
8465 if (!FRAME_WINDOW_P (it->f)
8466 || ((it->bidi_p
8467 && it->bidi_it.paragraph_dir == R2L)
8468 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8469 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8470 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8472 if (!get_next_display_element (it))
8474 result = MOVE_POS_MATCH_OR_ZV;
8475 break;
8477 if (BUFFER_POS_REACHED_P ())
8479 if (ITERATOR_AT_END_OF_LINE_P (it))
8480 result = MOVE_POS_MATCH_OR_ZV;
8481 else
8482 result = MOVE_LINE_CONTINUED;
8483 break;
8485 if (ITERATOR_AT_END_OF_LINE_P (it))
8487 result = MOVE_NEWLINE_OR_CR;
8488 break;
8493 else
8494 IT_RESET_X_ASCENT_DESCENT (it);
8496 if (wrap_it.sp >= 0)
8498 RESTORE_IT (it, &wrap_it, wrap_data);
8499 atpos_it.sp = -1;
8500 atx_it.sp = -1;
8503 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8504 IT_CHARPOS (*it)));
8505 result = MOVE_LINE_CONTINUED;
8506 break;
8509 if (BUFFER_POS_REACHED_P ())
8511 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8512 goto buffer_pos_reached;
8513 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8515 SAVE_IT (atpos_it, *it, atpos_data);
8516 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8520 if (new_x > it->first_visible_x)
8522 /* Glyph is visible. Increment number of glyphs that
8523 would be displayed. */
8524 ++it->hpos;
8528 if (result != MOVE_UNDEFINED)
8529 break;
8531 else if (BUFFER_POS_REACHED_P ())
8533 buffer_pos_reached:
8534 IT_RESET_X_ASCENT_DESCENT (it);
8535 result = MOVE_POS_MATCH_OR_ZV;
8536 break;
8538 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8540 /* Stop when TO_X specified and reached. This check is
8541 necessary here because of lines consisting of a line end,
8542 only. The line end will not produce any glyphs and we
8543 would never get MOVE_X_REACHED. */
8544 eassert (it->nglyphs == 0);
8545 result = MOVE_X_REACHED;
8546 break;
8549 /* Is this a line end? If yes, we're done. */
8550 if (ITERATOR_AT_END_OF_LINE_P (it))
8552 /* If we are past TO_CHARPOS, but never saw any character
8553 positions smaller than TO_CHARPOS, return
8554 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8555 did. */
8556 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8558 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8560 if (IT_CHARPOS (ppos_it) < ZV)
8562 RESTORE_IT (it, &ppos_it, ppos_data);
8563 result = MOVE_POS_MATCH_OR_ZV;
8565 else
8566 goto buffer_pos_reached;
8568 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8569 && IT_CHARPOS (*it) > to_charpos)
8570 goto buffer_pos_reached;
8571 else
8572 result = MOVE_NEWLINE_OR_CR;
8574 else
8575 result = MOVE_NEWLINE_OR_CR;
8576 break;
8579 prev_method = it->method;
8580 if (it->method == GET_FROM_BUFFER)
8581 prev_pos = IT_CHARPOS (*it);
8582 /* The current display element has been consumed. Advance
8583 to the next. */
8584 set_iterator_to_next (it, 1);
8585 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8586 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8587 if (IT_CHARPOS (*it) < to_charpos)
8588 saw_smaller_pos = 1;
8589 if (it->bidi_p
8590 && (op & MOVE_TO_POS)
8591 && IT_CHARPOS (*it) >= to_charpos
8592 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8593 SAVE_IT (ppos_it, *it, ppos_data);
8595 /* Stop if lines are truncated and IT's current x-position is
8596 past the right edge of the window now. */
8597 if (it->line_wrap == TRUNCATE
8598 && it->current_x >= it->last_visible_x)
8600 if (!FRAME_WINDOW_P (it->f)
8601 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8602 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8603 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8604 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8606 int at_eob_p = 0;
8608 if ((at_eob_p = !get_next_display_element (it))
8609 || BUFFER_POS_REACHED_P ()
8610 /* If we are past TO_CHARPOS, but never saw any
8611 character positions smaller than TO_CHARPOS,
8612 return MOVE_POS_MATCH_OR_ZV, like the
8613 unidirectional display did. */
8614 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8615 && !saw_smaller_pos
8616 && IT_CHARPOS (*it) > to_charpos))
8618 if (it->bidi_p
8619 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8620 RESTORE_IT (it, &ppos_it, ppos_data);
8621 result = MOVE_POS_MATCH_OR_ZV;
8622 break;
8624 if (ITERATOR_AT_END_OF_LINE_P (it))
8626 result = MOVE_NEWLINE_OR_CR;
8627 break;
8630 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8631 && !saw_smaller_pos
8632 && IT_CHARPOS (*it) > to_charpos)
8634 if (IT_CHARPOS (ppos_it) < ZV)
8635 RESTORE_IT (it, &ppos_it, ppos_data);
8636 result = MOVE_POS_MATCH_OR_ZV;
8637 break;
8639 result = MOVE_LINE_TRUNCATED;
8640 break;
8642 #undef IT_RESET_X_ASCENT_DESCENT
8645 #undef BUFFER_POS_REACHED_P
8647 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8648 restore the saved iterator. */
8649 if (atpos_it.sp >= 0)
8650 RESTORE_IT (it, &atpos_it, atpos_data);
8651 else if (atx_it.sp >= 0)
8652 RESTORE_IT (it, &atx_it, atx_data);
8654 done:
8656 if (atpos_data)
8657 bidi_unshelve_cache (atpos_data, 1);
8658 if (atx_data)
8659 bidi_unshelve_cache (atx_data, 1);
8660 if (wrap_data)
8661 bidi_unshelve_cache (wrap_data, 1);
8662 if (ppos_data)
8663 bidi_unshelve_cache (ppos_data, 1);
8665 /* Restore the iterator settings altered at the beginning of this
8666 function. */
8667 it->glyph_row = saved_glyph_row;
8668 return result;
8671 /* For external use. */
8672 void
8673 move_it_in_display_line (struct it *it,
8674 ptrdiff_t to_charpos, int to_x,
8675 enum move_operation_enum op)
8677 if (it->line_wrap == WORD_WRAP
8678 && (op & MOVE_TO_X))
8680 struct it save_it;
8681 void *save_data = NULL;
8682 int skip;
8684 SAVE_IT (save_it, *it, save_data);
8685 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8686 /* When word-wrap is on, TO_X may lie past the end
8687 of a wrapped line. Then it->current is the
8688 character on the next line, so backtrack to the
8689 space before the wrap point. */
8690 if (skip == MOVE_LINE_CONTINUED)
8692 int prev_x = max (it->current_x - 1, 0);
8693 RESTORE_IT (it, &save_it, save_data);
8694 move_it_in_display_line_to
8695 (it, -1, prev_x, MOVE_TO_X);
8697 else
8698 bidi_unshelve_cache (save_data, 1);
8700 else
8701 move_it_in_display_line_to (it, to_charpos, to_x, op);
8705 /* Move IT forward until it satisfies one or more of the criteria in
8706 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8708 OP is a bit-mask that specifies where to stop, and in particular,
8709 which of those four position arguments makes a difference. See the
8710 description of enum move_operation_enum.
8712 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8713 screen line, this function will set IT to the next position that is
8714 displayed to the right of TO_CHARPOS on the screen. */
8716 void
8717 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8719 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8720 int line_height, line_start_x = 0, reached = 0;
8721 void *backup_data = NULL;
8723 for (;;)
8725 if (op & MOVE_TO_VPOS)
8727 /* If no TO_CHARPOS and no TO_X specified, stop at the
8728 start of the line TO_VPOS. */
8729 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8731 if (it->vpos == to_vpos)
8733 reached = 1;
8734 break;
8736 else
8737 skip = move_it_in_display_line_to (it, -1, -1, 0);
8739 else
8741 /* TO_VPOS >= 0 means stop at TO_X in the line at
8742 TO_VPOS, or at TO_POS, whichever comes first. */
8743 if (it->vpos == to_vpos)
8745 reached = 2;
8746 break;
8749 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8751 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8753 reached = 3;
8754 break;
8756 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8758 /* We have reached TO_X but not in the line we want. */
8759 skip = move_it_in_display_line_to (it, to_charpos,
8760 -1, MOVE_TO_POS);
8761 if (skip == MOVE_POS_MATCH_OR_ZV)
8763 reached = 4;
8764 break;
8769 else if (op & MOVE_TO_Y)
8771 struct it it_backup;
8773 if (it->line_wrap == WORD_WRAP)
8774 SAVE_IT (it_backup, *it, backup_data);
8776 /* TO_Y specified means stop at TO_X in the line containing
8777 TO_Y---or at TO_CHARPOS if this is reached first. The
8778 problem is that we can't really tell whether the line
8779 contains TO_Y before we have completely scanned it, and
8780 this may skip past TO_X. What we do is to first scan to
8781 TO_X.
8783 If TO_X is not specified, use a TO_X of zero. The reason
8784 is to make the outcome of this function more predictable.
8785 If we didn't use TO_X == 0, we would stop at the end of
8786 the line which is probably not what a caller would expect
8787 to happen. */
8788 skip = move_it_in_display_line_to
8789 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8790 (MOVE_TO_X | (op & MOVE_TO_POS)));
8792 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8793 if (skip == MOVE_POS_MATCH_OR_ZV)
8794 reached = 5;
8795 else if (skip == MOVE_X_REACHED)
8797 /* If TO_X was reached, we want to know whether TO_Y is
8798 in the line. We know this is the case if the already
8799 scanned glyphs make the line tall enough. Otherwise,
8800 we must check by scanning the rest of the line. */
8801 line_height = it->max_ascent + it->max_descent;
8802 if (to_y >= it->current_y
8803 && to_y < it->current_y + line_height)
8805 reached = 6;
8806 break;
8808 SAVE_IT (it_backup, *it, backup_data);
8809 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8810 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8811 op & MOVE_TO_POS);
8812 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8813 line_height = it->max_ascent + it->max_descent;
8814 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8816 if (to_y >= it->current_y
8817 && to_y < it->current_y + line_height)
8819 /* If TO_Y is in this line and TO_X was reached
8820 above, we scanned too far. We have to restore
8821 IT's settings to the ones before skipping. But
8822 keep the more accurate values of max_ascent and
8823 max_descent we've found while skipping the rest
8824 of the line, for the sake of callers, such as
8825 pos_visible_p, that need to know the line
8826 height. */
8827 int max_ascent = it->max_ascent;
8828 int max_descent = it->max_descent;
8830 RESTORE_IT (it, &it_backup, backup_data);
8831 it->max_ascent = max_ascent;
8832 it->max_descent = max_descent;
8833 reached = 6;
8835 else
8837 skip = skip2;
8838 if (skip == MOVE_POS_MATCH_OR_ZV)
8839 reached = 7;
8842 else
8844 /* Check whether TO_Y is in this line. */
8845 line_height = it->max_ascent + it->max_descent;
8846 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8848 if (to_y >= it->current_y
8849 && to_y < it->current_y + line_height)
8851 /* When word-wrap is on, TO_X may lie past the end
8852 of a wrapped line. Then it->current is the
8853 character on the next line, so backtrack to the
8854 space before the wrap point. */
8855 if (skip == MOVE_LINE_CONTINUED
8856 && it->line_wrap == WORD_WRAP)
8858 int prev_x = max (it->current_x - 1, 0);
8859 RESTORE_IT (it, &it_backup, backup_data);
8860 skip = move_it_in_display_line_to
8861 (it, -1, prev_x, MOVE_TO_X);
8863 reached = 6;
8867 if (reached)
8868 break;
8870 else if (BUFFERP (it->object)
8871 && (it->method == GET_FROM_BUFFER
8872 || it->method == GET_FROM_STRETCH)
8873 && IT_CHARPOS (*it) >= to_charpos
8874 /* Under bidi iteration, a call to set_iterator_to_next
8875 can scan far beyond to_charpos if the initial
8876 portion of the next line needs to be reordered. In
8877 that case, give move_it_in_display_line_to another
8878 chance below. */
8879 && !(it->bidi_p
8880 && it->bidi_it.scan_dir == -1))
8881 skip = MOVE_POS_MATCH_OR_ZV;
8882 else
8883 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8885 switch (skip)
8887 case MOVE_POS_MATCH_OR_ZV:
8888 reached = 8;
8889 goto out;
8891 case MOVE_NEWLINE_OR_CR:
8892 set_iterator_to_next (it, 1);
8893 it->continuation_lines_width = 0;
8894 break;
8896 case MOVE_LINE_TRUNCATED:
8897 it->continuation_lines_width = 0;
8898 reseat_at_next_visible_line_start (it, 0);
8899 if ((op & MOVE_TO_POS) != 0
8900 && IT_CHARPOS (*it) > to_charpos)
8902 reached = 9;
8903 goto out;
8905 break;
8907 case MOVE_LINE_CONTINUED:
8908 /* For continued lines ending in a tab, some of the glyphs
8909 associated with the tab are displayed on the current
8910 line. Since it->current_x does not include these glyphs,
8911 we use it->last_visible_x instead. */
8912 if (it->c == '\t')
8914 it->continuation_lines_width += it->last_visible_x;
8915 /* When moving by vpos, ensure that the iterator really
8916 advances to the next line (bug#847, bug#969). Fixme:
8917 do we need to do this in other circumstances? */
8918 if (it->current_x != it->last_visible_x
8919 && (op & MOVE_TO_VPOS)
8920 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8922 line_start_x = it->current_x + it->pixel_width
8923 - it->last_visible_x;
8924 set_iterator_to_next (it, 0);
8927 else
8928 it->continuation_lines_width += it->current_x;
8929 break;
8931 default:
8932 emacs_abort ();
8935 /* Reset/increment for the next run. */
8936 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8937 it->current_x = line_start_x;
8938 line_start_x = 0;
8939 it->hpos = 0;
8940 it->current_y += it->max_ascent + it->max_descent;
8941 ++it->vpos;
8942 last_height = it->max_ascent + it->max_descent;
8943 it->max_ascent = it->max_descent = 0;
8946 out:
8948 /* On text terminals, we may stop at the end of a line in the middle
8949 of a multi-character glyph. If the glyph itself is continued,
8950 i.e. it is actually displayed on the next line, don't treat this
8951 stopping point as valid; move to the next line instead (unless
8952 that brings us offscreen). */
8953 if (!FRAME_WINDOW_P (it->f)
8954 && op & MOVE_TO_POS
8955 && IT_CHARPOS (*it) == to_charpos
8956 && it->what == IT_CHARACTER
8957 && it->nglyphs > 1
8958 && it->line_wrap == WINDOW_WRAP
8959 && it->current_x == it->last_visible_x - 1
8960 && it->c != '\n'
8961 && it->c != '\t'
8962 && it->vpos < XFASTINT (it->w->window_end_vpos))
8964 it->continuation_lines_width += it->current_x;
8965 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8966 it->current_y += it->max_ascent + it->max_descent;
8967 ++it->vpos;
8968 last_height = it->max_ascent + it->max_descent;
8971 if (backup_data)
8972 bidi_unshelve_cache (backup_data, 1);
8974 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8978 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8980 If DY > 0, move IT backward at least that many pixels. DY = 0
8981 means move IT backward to the preceding line start or BEGV. This
8982 function may move over more than DY pixels if IT->current_y - DY
8983 ends up in the middle of a line; in this case IT->current_y will be
8984 set to the top of the line moved to. */
8986 void
8987 move_it_vertically_backward (struct it *it, int dy)
8989 int nlines, h;
8990 struct it it2, it3;
8991 void *it2data = NULL, *it3data = NULL;
8992 ptrdiff_t start_pos;
8993 int nchars_per_row
8994 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
8995 ptrdiff_t pos_limit;
8997 move_further_back:
8998 eassert (dy >= 0);
9000 start_pos = IT_CHARPOS (*it);
9002 /* Estimate how many newlines we must move back. */
9003 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9004 if (it->line_wrap == TRUNCATE)
9005 pos_limit = BEGV;
9006 else
9007 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9009 /* Set the iterator's position that many lines back. But don't go
9010 back more than NLINES full screen lines -- this wins a day with
9011 buffers which have very long lines. */
9012 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9013 back_to_previous_visible_line_start (it);
9015 /* Reseat the iterator here. When moving backward, we don't want
9016 reseat to skip forward over invisible text, set up the iterator
9017 to deliver from overlay strings at the new position etc. So,
9018 use reseat_1 here. */
9019 reseat_1 (it, it->current.pos, 1);
9021 /* We are now surely at a line start. */
9022 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9023 reordering is in effect. */
9024 it->continuation_lines_width = 0;
9026 /* Move forward and see what y-distance we moved. First move to the
9027 start of the next line so that we get its height. We need this
9028 height to be able to tell whether we reached the specified
9029 y-distance. */
9030 SAVE_IT (it2, *it, it2data);
9031 it2.max_ascent = it2.max_descent = 0;
9034 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9035 MOVE_TO_POS | MOVE_TO_VPOS);
9037 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9038 /* If we are in a display string which starts at START_POS,
9039 and that display string includes a newline, and we are
9040 right after that newline (i.e. at the beginning of a
9041 display line), exit the loop, because otherwise we will
9042 infloop, since move_it_to will see that it is already at
9043 START_POS and will not move. */
9044 || (it2.method == GET_FROM_STRING
9045 && IT_CHARPOS (it2) == start_pos
9046 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9047 eassert (IT_CHARPOS (*it) >= BEGV);
9048 SAVE_IT (it3, it2, it3data);
9050 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9051 eassert (IT_CHARPOS (*it) >= BEGV);
9052 /* H is the actual vertical distance from the position in *IT
9053 and the starting position. */
9054 h = it2.current_y - it->current_y;
9055 /* NLINES is the distance in number of lines. */
9056 nlines = it2.vpos - it->vpos;
9058 /* Correct IT's y and vpos position
9059 so that they are relative to the starting point. */
9060 it->vpos -= nlines;
9061 it->current_y -= h;
9063 if (dy == 0)
9065 /* DY == 0 means move to the start of the screen line. The
9066 value of nlines is > 0 if continuation lines were involved,
9067 or if the original IT position was at start of a line. */
9068 RESTORE_IT (it, it, it2data);
9069 if (nlines > 0)
9070 move_it_by_lines (it, nlines);
9071 /* The above code moves us to some position NLINES down,
9072 usually to its first glyph (leftmost in an L2R line), but
9073 that's not necessarily the start of the line, under bidi
9074 reordering. We want to get to the character position
9075 that is immediately after the newline of the previous
9076 line. */
9077 if (it->bidi_p
9078 && !it->continuation_lines_width
9079 && !STRINGP (it->string)
9080 && IT_CHARPOS (*it) > BEGV
9081 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9083 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9085 DEC_BOTH (cp, bp);
9086 cp = find_newline_no_quit (cp, bp, -1, NULL);
9087 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9089 bidi_unshelve_cache (it3data, 1);
9091 else
9093 /* The y-position we try to reach, relative to *IT.
9094 Note that H has been subtracted in front of the if-statement. */
9095 int target_y = it->current_y + h - dy;
9096 int y0 = it3.current_y;
9097 int y1;
9098 int line_height;
9100 RESTORE_IT (&it3, &it3, it3data);
9101 y1 = line_bottom_y (&it3);
9102 line_height = y1 - y0;
9103 RESTORE_IT (it, it, it2data);
9104 /* If we did not reach target_y, try to move further backward if
9105 we can. If we moved too far backward, try to move forward. */
9106 if (target_y < it->current_y
9107 /* This is heuristic. In a window that's 3 lines high, with
9108 a line height of 13 pixels each, recentering with point
9109 on the bottom line will try to move -39/2 = 19 pixels
9110 backward. Try to avoid moving into the first line. */
9111 && (it->current_y - target_y
9112 > min (window_box_height (it->w), line_height * 2 / 3))
9113 && IT_CHARPOS (*it) > BEGV)
9115 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9116 target_y - it->current_y));
9117 dy = it->current_y - target_y;
9118 goto move_further_back;
9120 else if (target_y >= it->current_y + line_height
9121 && IT_CHARPOS (*it) < ZV)
9123 /* Should move forward by at least one line, maybe more.
9125 Note: Calling move_it_by_lines can be expensive on
9126 terminal frames, where compute_motion is used (via
9127 vmotion) to do the job, when there are very long lines
9128 and truncate-lines is nil. That's the reason for
9129 treating terminal frames specially here. */
9131 if (!FRAME_WINDOW_P (it->f))
9132 move_it_vertically (it, target_y - (it->current_y + line_height));
9133 else
9137 move_it_by_lines (it, 1);
9139 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9146 /* Move IT by a specified amount of pixel lines DY. DY negative means
9147 move backwards. DY = 0 means move to start of screen line. At the
9148 end, IT will be on the start of a screen line. */
9150 void
9151 move_it_vertically (struct it *it, int dy)
9153 if (dy <= 0)
9154 move_it_vertically_backward (it, -dy);
9155 else
9157 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9158 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9159 MOVE_TO_POS | MOVE_TO_Y);
9160 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9162 /* If buffer ends in ZV without a newline, move to the start of
9163 the line to satisfy the post-condition. */
9164 if (IT_CHARPOS (*it) == ZV
9165 && ZV > BEGV
9166 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9167 move_it_by_lines (it, 0);
9172 /* Move iterator IT past the end of the text line it is in. */
9174 void
9175 move_it_past_eol (struct it *it)
9177 enum move_it_result rc;
9179 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9180 if (rc == MOVE_NEWLINE_OR_CR)
9181 set_iterator_to_next (it, 0);
9185 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9186 negative means move up. DVPOS == 0 means move to the start of the
9187 screen line.
9189 Optimization idea: If we would know that IT->f doesn't use
9190 a face with proportional font, we could be faster for
9191 truncate-lines nil. */
9193 void
9194 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9197 /* The commented-out optimization uses vmotion on terminals. This
9198 gives bad results, because elements like it->what, on which
9199 callers such as pos_visible_p rely, aren't updated. */
9200 /* struct position pos;
9201 if (!FRAME_WINDOW_P (it->f))
9203 struct text_pos textpos;
9205 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9206 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9207 reseat (it, textpos, 1);
9208 it->vpos += pos.vpos;
9209 it->current_y += pos.vpos;
9211 else */
9213 if (dvpos == 0)
9215 /* DVPOS == 0 means move to the start of the screen line. */
9216 move_it_vertically_backward (it, 0);
9217 /* Let next call to line_bottom_y calculate real line height */
9218 last_height = 0;
9220 else if (dvpos > 0)
9222 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9223 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9225 /* Only move to the next buffer position if we ended up in a
9226 string from display property, not in an overlay string
9227 (before-string or after-string). That is because the
9228 latter don't conceal the underlying buffer position, so
9229 we can ask to move the iterator to the exact position we
9230 are interested in. Note that, even if we are already at
9231 IT_CHARPOS (*it), the call below is not a no-op, as it
9232 will detect that we are at the end of the string, pop the
9233 iterator, and compute it->current_x and it->hpos
9234 correctly. */
9235 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9236 -1, -1, -1, MOVE_TO_POS);
9239 else
9241 struct it it2;
9242 void *it2data = NULL;
9243 ptrdiff_t start_charpos, i;
9244 int nchars_per_row
9245 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9246 ptrdiff_t pos_limit;
9248 /* Start at the beginning of the screen line containing IT's
9249 position. This may actually move vertically backwards,
9250 in case of overlays, so adjust dvpos accordingly. */
9251 dvpos += it->vpos;
9252 move_it_vertically_backward (it, 0);
9253 dvpos -= it->vpos;
9255 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9256 screen lines, and reseat the iterator there. */
9257 start_charpos = IT_CHARPOS (*it);
9258 if (it->line_wrap == TRUNCATE)
9259 pos_limit = BEGV;
9260 else
9261 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9262 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9263 back_to_previous_visible_line_start (it);
9264 reseat (it, it->current.pos, 1);
9266 /* Move further back if we end up in a string or an image. */
9267 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9269 /* First try to move to start of display line. */
9270 dvpos += it->vpos;
9271 move_it_vertically_backward (it, 0);
9272 dvpos -= it->vpos;
9273 if (IT_POS_VALID_AFTER_MOVE_P (it))
9274 break;
9275 /* If start of line is still in string or image,
9276 move further back. */
9277 back_to_previous_visible_line_start (it);
9278 reseat (it, it->current.pos, 1);
9279 dvpos--;
9282 it->current_x = it->hpos = 0;
9284 /* Above call may have moved too far if continuation lines
9285 are involved. Scan forward and see if it did. */
9286 SAVE_IT (it2, *it, it2data);
9287 it2.vpos = it2.current_y = 0;
9288 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9289 it->vpos -= it2.vpos;
9290 it->current_y -= it2.current_y;
9291 it->current_x = it->hpos = 0;
9293 /* If we moved too far back, move IT some lines forward. */
9294 if (it2.vpos > -dvpos)
9296 int delta = it2.vpos + dvpos;
9298 RESTORE_IT (&it2, &it2, it2data);
9299 SAVE_IT (it2, *it, it2data);
9300 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9301 /* Move back again if we got too far ahead. */
9302 if (IT_CHARPOS (*it) >= start_charpos)
9303 RESTORE_IT (it, &it2, it2data);
9304 else
9305 bidi_unshelve_cache (it2data, 1);
9307 else
9308 RESTORE_IT (it, it, it2data);
9312 /* Return 1 if IT points into the middle of a display vector. */
9315 in_display_vector_p (struct it *it)
9317 return (it->method == GET_FROM_DISPLAY_VECTOR
9318 && it->current.dpvec_index > 0
9319 && it->dpvec + it->current.dpvec_index != it->dpend);
9323 /***********************************************************************
9324 Messages
9325 ***********************************************************************/
9328 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9329 to *Messages*. */
9331 void
9332 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9334 Lisp_Object args[3];
9335 Lisp_Object msg, fmt;
9336 char *buffer;
9337 ptrdiff_t len;
9338 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9339 USE_SAFE_ALLOCA;
9341 fmt = msg = Qnil;
9342 GCPRO4 (fmt, msg, arg1, arg2);
9344 args[0] = fmt = build_string (format);
9345 args[1] = arg1;
9346 args[2] = arg2;
9347 msg = Fformat (3, args);
9349 len = SBYTES (msg) + 1;
9350 buffer = SAFE_ALLOCA (len);
9351 memcpy (buffer, SDATA (msg), len);
9353 message_dolog (buffer, len - 1, 1, 0);
9354 SAFE_FREE ();
9356 UNGCPRO;
9360 /* Output a newline in the *Messages* buffer if "needs" one. */
9362 void
9363 message_log_maybe_newline (void)
9365 if (message_log_need_newline)
9366 message_dolog ("", 0, 1, 0);
9370 /* Add a string M of length NBYTES to the message log, optionally
9371 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9372 true, means interpret the contents of M as multibyte. This
9373 function calls low-level routines in order to bypass text property
9374 hooks, etc. which might not be safe to run.
9376 This may GC (insert may run before/after change hooks),
9377 so the buffer M must NOT point to a Lisp string. */
9379 void
9380 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9382 const unsigned char *msg = (const unsigned char *) m;
9384 if (!NILP (Vmemory_full))
9385 return;
9387 if (!NILP (Vmessage_log_max))
9389 struct buffer *oldbuf;
9390 Lisp_Object oldpoint, oldbegv, oldzv;
9391 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9392 ptrdiff_t point_at_end = 0;
9393 ptrdiff_t zv_at_end = 0;
9394 Lisp_Object old_deactivate_mark;
9395 bool shown;
9396 struct gcpro gcpro1;
9398 old_deactivate_mark = Vdeactivate_mark;
9399 oldbuf = current_buffer;
9400 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9401 bset_undo_list (current_buffer, Qt);
9403 oldpoint = message_dolog_marker1;
9404 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9405 oldbegv = message_dolog_marker2;
9406 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9407 oldzv = message_dolog_marker3;
9408 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9409 GCPRO1 (old_deactivate_mark);
9411 if (PT == Z)
9412 point_at_end = 1;
9413 if (ZV == Z)
9414 zv_at_end = 1;
9416 BEGV = BEG;
9417 BEGV_BYTE = BEG_BYTE;
9418 ZV = Z;
9419 ZV_BYTE = Z_BYTE;
9420 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9422 /* Insert the string--maybe converting multibyte to single byte
9423 or vice versa, so that all the text fits the buffer. */
9424 if (multibyte
9425 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9427 ptrdiff_t i;
9428 int c, char_bytes;
9429 char work[1];
9431 /* Convert a multibyte string to single-byte
9432 for the *Message* buffer. */
9433 for (i = 0; i < nbytes; i += char_bytes)
9435 c = string_char_and_length (msg + i, &char_bytes);
9436 work[0] = (ASCII_CHAR_P (c)
9438 : multibyte_char_to_unibyte (c));
9439 insert_1_both (work, 1, 1, 1, 0, 0);
9442 else if (! multibyte
9443 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9445 ptrdiff_t i;
9446 int c, char_bytes;
9447 unsigned char str[MAX_MULTIBYTE_LENGTH];
9448 /* Convert a single-byte string to multibyte
9449 for the *Message* buffer. */
9450 for (i = 0; i < nbytes; i++)
9452 c = msg[i];
9453 MAKE_CHAR_MULTIBYTE (c);
9454 char_bytes = CHAR_STRING (c, str);
9455 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9458 else if (nbytes)
9459 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9461 if (nlflag)
9463 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9464 printmax_t dups;
9466 insert_1_both ("\n", 1, 1, 1, 0, 0);
9468 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9469 this_bol = PT;
9470 this_bol_byte = PT_BYTE;
9472 /* See if this line duplicates the previous one.
9473 If so, combine duplicates. */
9474 if (this_bol > BEG)
9476 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9477 prev_bol = PT;
9478 prev_bol_byte = PT_BYTE;
9480 dups = message_log_check_duplicate (prev_bol_byte,
9481 this_bol_byte);
9482 if (dups)
9484 del_range_both (prev_bol, prev_bol_byte,
9485 this_bol, this_bol_byte, 0);
9486 if (dups > 1)
9488 char dupstr[sizeof " [ times]"
9489 + INT_STRLEN_BOUND (printmax_t)];
9491 /* If you change this format, don't forget to also
9492 change message_log_check_duplicate. */
9493 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9494 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9495 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9500 /* If we have more than the desired maximum number of lines
9501 in the *Messages* buffer now, delete the oldest ones.
9502 This is safe because we don't have undo in this buffer. */
9504 if (NATNUMP (Vmessage_log_max))
9506 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9507 -XFASTINT (Vmessage_log_max) - 1, 0);
9508 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9511 BEGV = marker_position (oldbegv);
9512 BEGV_BYTE = marker_byte_position (oldbegv);
9514 if (zv_at_end)
9516 ZV = Z;
9517 ZV_BYTE = Z_BYTE;
9519 else
9521 ZV = marker_position (oldzv);
9522 ZV_BYTE = marker_byte_position (oldzv);
9525 if (point_at_end)
9526 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9527 else
9528 /* We can't do Fgoto_char (oldpoint) because it will run some
9529 Lisp code. */
9530 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9531 marker_byte_position (oldpoint));
9533 UNGCPRO;
9534 unchain_marker (XMARKER (oldpoint));
9535 unchain_marker (XMARKER (oldbegv));
9536 unchain_marker (XMARKER (oldzv));
9538 shown = buffer_window_count (current_buffer) > 0;
9539 set_buffer_internal (oldbuf);
9540 if (!shown)
9541 windows_or_buffers_changed = old_windows_or_buffers_changed;
9542 message_log_need_newline = !nlflag;
9543 Vdeactivate_mark = old_deactivate_mark;
9548 /* We are at the end of the buffer after just having inserted a newline.
9549 (Note: We depend on the fact we won't be crossing the gap.)
9550 Check to see if the most recent message looks a lot like the previous one.
9551 Return 0 if different, 1 if the new one should just replace it, or a
9552 value N > 1 if we should also append " [N times]". */
9554 static intmax_t
9555 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9557 ptrdiff_t i;
9558 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9559 int seen_dots = 0;
9560 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9561 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9563 for (i = 0; i < len; i++)
9565 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9566 seen_dots = 1;
9567 if (p1[i] != p2[i])
9568 return seen_dots;
9570 p1 += len;
9571 if (*p1 == '\n')
9572 return 2;
9573 if (*p1++ == ' ' && *p1++ == '[')
9575 char *pend;
9576 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9577 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9578 return n + 1;
9580 return 0;
9584 /* Display an echo area message M with a specified length of NBYTES
9585 bytes. The string may include null characters. If M is not a
9586 string, clear out any existing message, and let the mini-buffer
9587 text show through.
9589 This function cancels echoing. */
9591 void
9592 message3 (Lisp_Object m)
9594 struct gcpro gcpro1;
9596 GCPRO1 (m);
9597 clear_message (1,1);
9598 cancel_echoing ();
9600 /* First flush out any partial line written with print. */
9601 message_log_maybe_newline ();
9602 if (STRINGP (m))
9604 ptrdiff_t nbytes = SBYTES (m);
9605 bool multibyte = STRING_MULTIBYTE (m);
9606 USE_SAFE_ALLOCA;
9607 char *buffer = SAFE_ALLOCA (nbytes);
9608 memcpy (buffer, SDATA (m), nbytes);
9609 message_dolog (buffer, nbytes, 1, multibyte);
9610 SAFE_FREE ();
9612 message3_nolog (m);
9614 UNGCPRO;
9618 /* The non-logging version of message3.
9619 This does not cancel echoing, because it is used for echoing.
9620 Perhaps we need to make a separate function for echoing
9621 and make this cancel echoing. */
9623 void
9624 message3_nolog (Lisp_Object m)
9626 struct frame *sf = SELECTED_FRAME ();
9628 if (FRAME_INITIAL_P (sf))
9630 if (noninteractive_need_newline)
9631 putc ('\n', stderr);
9632 noninteractive_need_newline = 0;
9633 if (STRINGP (m))
9634 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9635 if (cursor_in_echo_area == 0)
9636 fprintf (stderr, "\n");
9637 fflush (stderr);
9639 /* Error messages get reported properly by cmd_error, so this must be just an
9640 informative message; if the frame hasn't really been initialized yet, just
9641 toss it. */
9642 else if (INTERACTIVE && sf->glyphs_initialized_p)
9644 /* Get the frame containing the mini-buffer
9645 that the selected frame is using. */
9646 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9647 Lisp_Object frame = XWINDOW (mini_window)->frame;
9648 struct frame *f = XFRAME (frame);
9650 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9651 Fmake_frame_visible (frame);
9653 if (STRINGP (m) && SCHARS (m) > 0)
9655 set_message (m);
9656 if (minibuffer_auto_raise)
9657 Fraise_frame (frame);
9658 /* Assume we are not echoing.
9659 (If we are, echo_now will override this.) */
9660 echo_message_buffer = Qnil;
9662 else
9663 clear_message (1, 1);
9665 do_pending_window_change (0);
9666 echo_area_display (1);
9667 do_pending_window_change (0);
9668 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9669 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9674 /* Display a null-terminated echo area message M. If M is 0, clear
9675 out any existing message, and let the mini-buffer text show through.
9677 The buffer M must continue to exist until after the echo area gets
9678 cleared or some other message gets displayed there. Do not pass
9679 text that is stored in a Lisp string. Do not pass text in a buffer
9680 that was alloca'd. */
9682 void
9683 message1 (const char *m)
9685 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9689 /* The non-logging counterpart of message1. */
9691 void
9692 message1_nolog (const char *m)
9694 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9697 /* Display a message M which contains a single %s
9698 which gets replaced with STRING. */
9700 void
9701 message_with_string (const char *m, Lisp_Object string, int log)
9703 CHECK_STRING (string);
9705 if (noninteractive)
9707 if (m)
9709 if (noninteractive_need_newline)
9710 putc ('\n', stderr);
9711 noninteractive_need_newline = 0;
9712 fprintf (stderr, m, SDATA (string));
9713 if (!cursor_in_echo_area)
9714 fprintf (stderr, "\n");
9715 fflush (stderr);
9718 else if (INTERACTIVE)
9720 /* The frame whose minibuffer we're going to display the message on.
9721 It may be larger than the selected frame, so we need
9722 to use its buffer, not the selected frame's buffer. */
9723 Lisp_Object mini_window;
9724 struct frame *f, *sf = SELECTED_FRAME ();
9726 /* Get the frame containing the minibuffer
9727 that the selected frame is using. */
9728 mini_window = FRAME_MINIBUF_WINDOW (sf);
9729 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9731 /* Error messages get reported properly by cmd_error, so this must be
9732 just an informative message; if the frame hasn't really been
9733 initialized yet, just toss it. */
9734 if (f->glyphs_initialized_p)
9736 Lisp_Object args[2], msg;
9737 struct gcpro gcpro1, gcpro2;
9739 args[0] = build_string (m);
9740 args[1] = msg = string;
9741 GCPRO2 (args[0], msg);
9742 gcpro1.nvars = 2;
9744 msg = Fformat (2, args);
9746 if (log)
9747 message3 (msg);
9748 else
9749 message3_nolog (msg);
9751 UNGCPRO;
9753 /* Print should start at the beginning of the message
9754 buffer next time. */
9755 message_buf_print = 0;
9761 /* Dump an informative message to the minibuf. If M is 0, clear out
9762 any existing message, and let the mini-buffer text show through. */
9764 static void
9765 vmessage (const char *m, va_list ap)
9767 if (noninteractive)
9769 if (m)
9771 if (noninteractive_need_newline)
9772 putc ('\n', stderr);
9773 noninteractive_need_newline = 0;
9774 vfprintf (stderr, m, ap);
9775 if (cursor_in_echo_area == 0)
9776 fprintf (stderr, "\n");
9777 fflush (stderr);
9780 else if (INTERACTIVE)
9782 /* The frame whose mini-buffer we're going to display the message
9783 on. It may be larger than the selected frame, so we need to
9784 use its buffer, not the selected frame's buffer. */
9785 Lisp_Object mini_window;
9786 struct frame *f, *sf = SELECTED_FRAME ();
9788 /* Get the frame containing the mini-buffer
9789 that the selected frame is using. */
9790 mini_window = FRAME_MINIBUF_WINDOW (sf);
9791 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9793 /* Error messages get reported properly by cmd_error, so this must be
9794 just an informative message; if the frame hasn't really been
9795 initialized yet, just toss it. */
9796 if (f->glyphs_initialized_p)
9798 if (m)
9800 ptrdiff_t len;
9801 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9802 char *message_buf = alloca (maxsize + 1);
9804 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9806 message3 (make_string (message_buf, len));
9808 else
9809 message1 (0);
9811 /* Print should start at the beginning of the message
9812 buffer next time. */
9813 message_buf_print = 0;
9818 void
9819 message (const char *m, ...)
9821 va_list ap;
9822 va_start (ap, m);
9823 vmessage (m, ap);
9824 va_end (ap);
9828 #if 0
9829 /* The non-logging version of message. */
9831 void
9832 message_nolog (const char *m, ...)
9834 Lisp_Object old_log_max;
9835 va_list ap;
9836 va_start (ap, m);
9837 old_log_max = Vmessage_log_max;
9838 Vmessage_log_max = Qnil;
9839 vmessage (m, ap);
9840 Vmessage_log_max = old_log_max;
9841 va_end (ap);
9843 #endif
9846 /* Display the current message in the current mini-buffer. This is
9847 only called from error handlers in process.c, and is not time
9848 critical. */
9850 void
9851 update_echo_area (void)
9853 if (!NILP (echo_area_buffer[0]))
9855 Lisp_Object string;
9856 string = Fcurrent_message ();
9857 message3 (string);
9862 /* Make sure echo area buffers in `echo_buffers' are live.
9863 If they aren't, make new ones. */
9865 static void
9866 ensure_echo_area_buffers (void)
9868 int i;
9870 for (i = 0; i < 2; ++i)
9871 if (!BUFFERP (echo_buffer[i])
9872 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9874 char name[30];
9875 Lisp_Object old_buffer;
9876 int j;
9878 old_buffer = echo_buffer[i];
9879 echo_buffer[i] = Fget_buffer_create
9880 (make_formatted_string (name, " *Echo Area %d*", i));
9881 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9882 /* to force word wrap in echo area -
9883 it was decided to postpone this*/
9884 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9886 for (j = 0; j < 2; ++j)
9887 if (EQ (old_buffer, echo_area_buffer[j]))
9888 echo_area_buffer[j] = echo_buffer[i];
9893 /* Call FN with args A1..A2 with either the current or last displayed
9894 echo_area_buffer as current buffer.
9896 WHICH zero means use the current message buffer
9897 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9898 from echo_buffer[] and clear it.
9900 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9901 suitable buffer from echo_buffer[] and clear it.
9903 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9904 that the current message becomes the last displayed one, make
9905 choose a suitable buffer for echo_area_buffer[0], and clear it.
9907 Value is what FN returns. */
9909 static int
9910 with_echo_area_buffer (struct window *w, int which,
9911 int (*fn) (ptrdiff_t, Lisp_Object),
9912 ptrdiff_t a1, Lisp_Object a2)
9914 Lisp_Object buffer;
9915 int this_one, the_other, clear_buffer_p, rc;
9916 ptrdiff_t count = SPECPDL_INDEX ();
9918 /* If buffers aren't live, make new ones. */
9919 ensure_echo_area_buffers ();
9921 clear_buffer_p = 0;
9923 if (which == 0)
9924 this_one = 0, the_other = 1;
9925 else if (which > 0)
9926 this_one = 1, the_other = 0;
9927 else
9929 this_one = 0, the_other = 1;
9930 clear_buffer_p = 1;
9932 /* We need a fresh one in case the current echo buffer equals
9933 the one containing the last displayed echo area message. */
9934 if (!NILP (echo_area_buffer[this_one])
9935 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9936 echo_area_buffer[this_one] = Qnil;
9939 /* Choose a suitable buffer from echo_buffer[] is we don't
9940 have one. */
9941 if (NILP (echo_area_buffer[this_one]))
9943 echo_area_buffer[this_one]
9944 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9945 ? echo_buffer[the_other]
9946 : echo_buffer[this_one]);
9947 clear_buffer_p = 1;
9950 buffer = echo_area_buffer[this_one];
9952 /* Don't get confused by reusing the buffer used for echoing
9953 for a different purpose. */
9954 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9955 cancel_echoing ();
9957 record_unwind_protect (unwind_with_echo_area_buffer,
9958 with_echo_area_buffer_unwind_data (w));
9960 /* Make the echo area buffer current. Note that for display
9961 purposes, it is not necessary that the displayed window's buffer
9962 == current_buffer, except for text property lookup. So, let's
9963 only set that buffer temporarily here without doing a full
9964 Fset_window_buffer. We must also change w->pointm, though,
9965 because otherwise an assertions in unshow_buffer fails, and Emacs
9966 aborts. */
9967 set_buffer_internal_1 (XBUFFER (buffer));
9968 if (w)
9970 wset_buffer (w, buffer);
9971 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9974 bset_undo_list (current_buffer, Qt);
9975 bset_read_only (current_buffer, Qnil);
9976 specbind (Qinhibit_read_only, Qt);
9977 specbind (Qinhibit_modification_hooks, Qt);
9979 if (clear_buffer_p && Z > BEG)
9980 del_range (BEG, Z);
9982 eassert (BEGV >= BEG);
9983 eassert (ZV <= Z && ZV >= BEGV);
9985 rc = fn (a1, a2);
9987 eassert (BEGV >= BEG);
9988 eassert (ZV <= Z && ZV >= BEGV);
9990 unbind_to (count, Qnil);
9991 return rc;
9995 /* Save state that should be preserved around the call to the function
9996 FN called in with_echo_area_buffer. */
9998 static Lisp_Object
9999 with_echo_area_buffer_unwind_data (struct window *w)
10001 int i = 0;
10002 Lisp_Object vector, tmp;
10004 /* Reduce consing by keeping one vector in
10005 Vwith_echo_area_save_vector. */
10006 vector = Vwith_echo_area_save_vector;
10007 Vwith_echo_area_save_vector = Qnil;
10009 if (NILP (vector))
10010 vector = Fmake_vector (make_number (9), Qnil);
10012 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10013 ASET (vector, i, Vdeactivate_mark); ++i;
10014 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10016 if (w)
10018 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10019 ASET (vector, i, w->contents); ++i;
10020 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10021 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10022 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10023 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10025 else
10027 int end = i + 6;
10028 for (; i < end; ++i)
10029 ASET (vector, i, Qnil);
10032 eassert (i == ASIZE (vector));
10033 return vector;
10037 /* Restore global state from VECTOR which was created by
10038 with_echo_area_buffer_unwind_data. */
10040 static Lisp_Object
10041 unwind_with_echo_area_buffer (Lisp_Object vector)
10043 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10044 Vdeactivate_mark = AREF (vector, 1);
10045 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10047 if (WINDOWP (AREF (vector, 3)))
10049 struct window *w;
10050 Lisp_Object buffer;
10052 w = XWINDOW (AREF (vector, 3));
10053 buffer = AREF (vector, 4);
10055 wset_buffer (w, buffer);
10056 set_marker_both (w->pointm, buffer,
10057 XFASTINT (AREF (vector, 5)),
10058 XFASTINT (AREF (vector, 6)));
10059 set_marker_both (w->start, buffer,
10060 XFASTINT (AREF (vector, 7)),
10061 XFASTINT (AREF (vector, 8)));
10064 Vwith_echo_area_save_vector = vector;
10065 return Qnil;
10069 /* Set up the echo area for use by print functions. MULTIBYTE_P
10070 non-zero means we will print multibyte. */
10072 void
10073 setup_echo_area_for_printing (int multibyte_p)
10075 /* If we can't find an echo area any more, exit. */
10076 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10077 Fkill_emacs (Qnil);
10079 ensure_echo_area_buffers ();
10081 if (!message_buf_print)
10083 /* A message has been output since the last time we printed.
10084 Choose a fresh echo area buffer. */
10085 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10086 echo_area_buffer[0] = echo_buffer[1];
10087 else
10088 echo_area_buffer[0] = echo_buffer[0];
10090 /* Switch to that buffer and clear it. */
10091 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10092 bset_truncate_lines (current_buffer, Qnil);
10094 if (Z > BEG)
10096 ptrdiff_t count = SPECPDL_INDEX ();
10097 specbind (Qinhibit_read_only, Qt);
10098 /* Note that undo recording is always disabled. */
10099 del_range (BEG, Z);
10100 unbind_to (count, Qnil);
10102 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10104 /* Set up the buffer for the multibyteness we need. */
10105 if (multibyte_p
10106 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10107 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10109 /* Raise the frame containing the echo area. */
10110 if (minibuffer_auto_raise)
10112 struct frame *sf = SELECTED_FRAME ();
10113 Lisp_Object mini_window;
10114 mini_window = FRAME_MINIBUF_WINDOW (sf);
10115 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10118 message_log_maybe_newline ();
10119 message_buf_print = 1;
10121 else
10123 if (NILP (echo_area_buffer[0]))
10125 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10126 echo_area_buffer[0] = echo_buffer[1];
10127 else
10128 echo_area_buffer[0] = echo_buffer[0];
10131 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10133 /* Someone switched buffers between print requests. */
10134 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10135 bset_truncate_lines (current_buffer, Qnil);
10141 /* Display an echo area message in window W. Value is non-zero if W's
10142 height is changed. If display_last_displayed_message_p is
10143 non-zero, display the message that was last displayed, otherwise
10144 display the current message. */
10146 static int
10147 display_echo_area (struct window *w)
10149 int i, no_message_p, window_height_changed_p;
10151 /* Temporarily disable garbage collections while displaying the echo
10152 area. This is done because a GC can print a message itself.
10153 That message would modify the echo area buffer's contents while a
10154 redisplay of the buffer is going on, and seriously confuse
10155 redisplay. */
10156 ptrdiff_t count = inhibit_garbage_collection ();
10158 /* If there is no message, we must call display_echo_area_1
10159 nevertheless because it resizes the window. But we will have to
10160 reset the echo_area_buffer in question to nil at the end because
10161 with_echo_area_buffer will sets it to an empty buffer. */
10162 i = display_last_displayed_message_p ? 1 : 0;
10163 no_message_p = NILP (echo_area_buffer[i]);
10165 window_height_changed_p
10166 = with_echo_area_buffer (w, display_last_displayed_message_p,
10167 display_echo_area_1,
10168 (intptr_t) w, Qnil);
10170 if (no_message_p)
10171 echo_area_buffer[i] = Qnil;
10173 unbind_to (count, Qnil);
10174 return window_height_changed_p;
10178 /* Helper for display_echo_area. Display the current buffer which
10179 contains the current echo area message in window W, a mini-window,
10180 a pointer to which is passed in A1. A2..A4 are currently not used.
10181 Change the height of W so that all of the message is displayed.
10182 Value is non-zero if height of W was changed. */
10184 static int
10185 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10187 intptr_t i1 = a1;
10188 struct window *w = (struct window *) i1;
10189 Lisp_Object window;
10190 struct text_pos start;
10191 int window_height_changed_p = 0;
10193 /* Do this before displaying, so that we have a large enough glyph
10194 matrix for the display. If we can't get enough space for the
10195 whole text, display the last N lines. That works by setting w->start. */
10196 window_height_changed_p = resize_mini_window (w, 0);
10198 /* Use the starting position chosen by resize_mini_window. */
10199 SET_TEXT_POS_FROM_MARKER (start, w->start);
10201 /* Display. */
10202 clear_glyph_matrix (w->desired_matrix);
10203 XSETWINDOW (window, w);
10204 try_window (window, start, 0);
10206 return window_height_changed_p;
10210 /* Resize the echo area window to exactly the size needed for the
10211 currently displayed message, if there is one. If a mini-buffer
10212 is active, don't shrink it. */
10214 void
10215 resize_echo_area_exactly (void)
10217 if (BUFFERP (echo_area_buffer[0])
10218 && WINDOWP (echo_area_window))
10220 struct window *w = XWINDOW (echo_area_window);
10221 int resized_p;
10222 Lisp_Object resize_exactly;
10224 if (minibuf_level == 0)
10225 resize_exactly = Qt;
10226 else
10227 resize_exactly = Qnil;
10229 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10230 (intptr_t) w, resize_exactly);
10231 if (resized_p)
10233 ++windows_or_buffers_changed;
10234 ++update_mode_lines;
10235 redisplay_internal ();
10241 /* Callback function for with_echo_area_buffer, when used from
10242 resize_echo_area_exactly. A1 contains a pointer to the window to
10243 resize, EXACTLY non-nil means resize the mini-window exactly to the
10244 size of the text displayed. A3 and A4 are not used. Value is what
10245 resize_mini_window returns. */
10247 static int
10248 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10250 intptr_t i1 = a1;
10251 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10255 /* Resize mini-window W to fit the size of its contents. EXACT_P
10256 means size the window exactly to the size needed. Otherwise, it's
10257 only enlarged until W's buffer is empty.
10259 Set W->start to the right place to begin display. If the whole
10260 contents fit, start at the beginning. Otherwise, start so as
10261 to make the end of the contents appear. This is particularly
10262 important for y-or-n-p, but seems desirable generally.
10264 Value is non-zero if the window height has been changed. */
10267 resize_mini_window (struct window *w, int exact_p)
10269 struct frame *f = XFRAME (w->frame);
10270 int window_height_changed_p = 0;
10272 eassert (MINI_WINDOW_P (w));
10274 /* By default, start display at the beginning. */
10275 set_marker_both (w->start, w->contents,
10276 BUF_BEGV (XBUFFER (w->contents)),
10277 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10279 /* Don't resize windows while redisplaying a window; it would
10280 confuse redisplay functions when the size of the window they are
10281 displaying changes from under them. Such a resizing can happen,
10282 for instance, when which-func prints a long message while
10283 we are running fontification-functions. We're running these
10284 functions with safe_call which binds inhibit-redisplay to t. */
10285 if (!NILP (Vinhibit_redisplay))
10286 return 0;
10288 /* Nil means don't try to resize. */
10289 if (NILP (Vresize_mini_windows)
10290 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10291 return 0;
10293 if (!FRAME_MINIBUF_ONLY_P (f))
10295 struct it it;
10296 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10297 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10298 int height;
10299 EMACS_INT max_height;
10300 int unit = FRAME_LINE_HEIGHT (f);
10301 struct text_pos start;
10302 struct buffer *old_current_buffer = NULL;
10304 if (current_buffer != XBUFFER (w->contents))
10306 old_current_buffer = current_buffer;
10307 set_buffer_internal (XBUFFER (w->contents));
10310 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10312 /* Compute the max. number of lines specified by the user. */
10313 if (FLOATP (Vmax_mini_window_height))
10314 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10315 else if (INTEGERP (Vmax_mini_window_height))
10316 max_height = XINT (Vmax_mini_window_height);
10317 else
10318 max_height = total_height / 4;
10320 /* Correct that max. height if it's bogus. */
10321 max_height = clip_to_bounds (1, max_height, total_height);
10323 /* Find out the height of the text in the window. */
10324 if (it.line_wrap == TRUNCATE)
10325 height = 1;
10326 else
10328 last_height = 0;
10329 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10330 if (it.max_ascent == 0 && it.max_descent == 0)
10331 height = it.current_y + last_height;
10332 else
10333 height = it.current_y + it.max_ascent + it.max_descent;
10334 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10335 height = (height + unit - 1) / unit;
10338 /* Compute a suitable window start. */
10339 if (height > max_height)
10341 height = max_height;
10342 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10343 move_it_vertically_backward (&it, (height - 1) * unit);
10344 start = it.current.pos;
10346 else
10347 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10348 SET_MARKER_FROM_TEXT_POS (w->start, start);
10350 if (EQ (Vresize_mini_windows, Qgrow_only))
10352 /* Let it grow only, until we display an empty message, in which
10353 case the window shrinks again. */
10354 if (height > WINDOW_TOTAL_LINES (w))
10356 int old_height = WINDOW_TOTAL_LINES (w);
10357 freeze_window_starts (f, 1);
10358 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10359 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10361 else if (height < WINDOW_TOTAL_LINES (w)
10362 && (exact_p || BEGV == ZV))
10364 int old_height = WINDOW_TOTAL_LINES (w);
10365 freeze_window_starts (f, 0);
10366 shrink_mini_window (w);
10367 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10370 else
10372 /* Always resize to exact size needed. */
10373 if (height > WINDOW_TOTAL_LINES (w))
10375 int old_height = WINDOW_TOTAL_LINES (w);
10376 freeze_window_starts (f, 1);
10377 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10378 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10380 else if (height < WINDOW_TOTAL_LINES (w))
10382 int old_height = WINDOW_TOTAL_LINES (w);
10383 freeze_window_starts (f, 0);
10384 shrink_mini_window (w);
10386 if (height)
10388 freeze_window_starts (f, 1);
10389 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10392 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10396 if (old_current_buffer)
10397 set_buffer_internal (old_current_buffer);
10400 return window_height_changed_p;
10404 /* Value is the current message, a string, or nil if there is no
10405 current message. */
10407 Lisp_Object
10408 current_message (void)
10410 Lisp_Object msg;
10412 if (!BUFFERP (echo_area_buffer[0]))
10413 msg = Qnil;
10414 else
10416 with_echo_area_buffer (0, 0, current_message_1,
10417 (intptr_t) &msg, Qnil);
10418 if (NILP (msg))
10419 echo_area_buffer[0] = Qnil;
10422 return msg;
10426 static int
10427 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10429 intptr_t i1 = a1;
10430 Lisp_Object *msg = (Lisp_Object *) i1;
10432 if (Z > BEG)
10433 *msg = make_buffer_string (BEG, Z, 1);
10434 else
10435 *msg = Qnil;
10436 return 0;
10440 /* Push the current message on Vmessage_stack for later restoration
10441 by restore_message. Value is non-zero if the current message isn't
10442 empty. This is a relatively infrequent operation, so it's not
10443 worth optimizing. */
10445 bool
10446 push_message (void)
10448 Lisp_Object msg = current_message ();
10449 Vmessage_stack = Fcons (msg, Vmessage_stack);
10450 return STRINGP (msg);
10454 /* Restore message display from the top of Vmessage_stack. */
10456 void
10457 restore_message (void)
10459 eassert (CONSP (Vmessage_stack));
10460 message3_nolog (XCAR (Vmessage_stack));
10464 /* Handler for record_unwind_protect calling pop_message. */
10466 Lisp_Object
10467 pop_message_unwind (Lisp_Object dummy)
10469 pop_message ();
10470 return Qnil;
10473 /* Pop the top-most entry off Vmessage_stack. */
10475 static void
10476 pop_message (void)
10478 eassert (CONSP (Vmessage_stack));
10479 Vmessage_stack = XCDR (Vmessage_stack);
10483 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10484 exits. If the stack is not empty, we have a missing pop_message
10485 somewhere. */
10487 void
10488 check_message_stack (void)
10490 if (!NILP (Vmessage_stack))
10491 emacs_abort ();
10495 /* Truncate to NCHARS what will be displayed in the echo area the next
10496 time we display it---but don't redisplay it now. */
10498 void
10499 truncate_echo_area (ptrdiff_t nchars)
10501 if (nchars == 0)
10502 echo_area_buffer[0] = Qnil;
10503 else if (!noninteractive
10504 && INTERACTIVE
10505 && !NILP (echo_area_buffer[0]))
10507 struct frame *sf = SELECTED_FRAME ();
10508 /* Error messages get reported properly by cmd_error, so this must be
10509 just an informative message; if the frame hasn't really been
10510 initialized yet, just toss it. */
10511 if (sf->glyphs_initialized_p)
10512 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10517 /* Helper function for truncate_echo_area. Truncate the current
10518 message to at most NCHARS characters. */
10520 static int
10521 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10523 if (BEG + nchars < Z)
10524 del_range (BEG + nchars, Z);
10525 if (Z == BEG)
10526 echo_area_buffer[0] = Qnil;
10527 return 0;
10530 /* Set the current message to STRING. */
10532 static void
10533 set_message (Lisp_Object string)
10535 eassert (STRINGP (string));
10537 message_enable_multibyte = STRING_MULTIBYTE (string);
10539 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10540 message_buf_print = 0;
10541 help_echo_showing_p = 0;
10543 if (STRINGP (Vdebug_on_message)
10544 && STRINGP (string)
10545 && fast_string_match (Vdebug_on_message, string) >= 0)
10546 call_debugger (list2 (Qerror, string));
10550 /* Helper function for set_message. First argument is ignored and second
10551 argument has the same meaning as for set_message.
10552 This function is called with the echo area buffer being current. */
10554 static int
10555 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10557 eassert (STRINGP (string));
10559 /* Change multibyteness of the echo buffer appropriately. */
10560 if (message_enable_multibyte
10561 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10562 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10564 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10565 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10566 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10568 /* Insert new message at BEG. */
10569 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10571 /* This function takes care of single/multibyte conversion.
10572 We just have to ensure that the echo area buffer has the right
10573 setting of enable_multibyte_characters. */
10574 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10576 return 0;
10580 /* Clear messages. CURRENT_P non-zero means clear the current
10581 message. LAST_DISPLAYED_P non-zero means clear the message
10582 last displayed. */
10584 void
10585 clear_message (int current_p, int last_displayed_p)
10587 if (current_p)
10589 echo_area_buffer[0] = Qnil;
10590 message_cleared_p = 1;
10593 if (last_displayed_p)
10594 echo_area_buffer[1] = Qnil;
10596 message_buf_print = 0;
10599 /* Clear garbaged frames.
10601 This function is used where the old redisplay called
10602 redraw_garbaged_frames which in turn called redraw_frame which in
10603 turn called clear_frame. The call to clear_frame was a source of
10604 flickering. I believe a clear_frame is not necessary. It should
10605 suffice in the new redisplay to invalidate all current matrices,
10606 and ensure a complete redisplay of all windows. */
10608 static void
10609 clear_garbaged_frames (void)
10611 if (frame_garbaged)
10613 Lisp_Object tail, frame;
10614 int changed_count = 0;
10616 FOR_EACH_FRAME (tail, frame)
10618 struct frame *f = XFRAME (frame);
10620 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10622 if (f->resized_p)
10624 redraw_frame (f);
10625 f->force_flush_display_p = 1;
10627 clear_current_matrices (f);
10628 changed_count++;
10629 f->garbaged = 0;
10630 f->resized_p = 0;
10634 frame_garbaged = 0;
10635 if (changed_count)
10636 ++windows_or_buffers_changed;
10641 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10642 is non-zero update selected_frame. Value is non-zero if the
10643 mini-windows height has been changed. */
10645 static int
10646 echo_area_display (int update_frame_p)
10648 Lisp_Object mini_window;
10649 struct window *w;
10650 struct frame *f;
10651 int window_height_changed_p = 0;
10652 struct frame *sf = SELECTED_FRAME ();
10654 mini_window = FRAME_MINIBUF_WINDOW (sf);
10655 w = XWINDOW (mini_window);
10656 f = XFRAME (WINDOW_FRAME (w));
10658 /* Don't display if frame is invisible or not yet initialized. */
10659 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10660 return 0;
10662 #ifdef HAVE_WINDOW_SYSTEM
10663 /* When Emacs starts, selected_frame may be the initial terminal
10664 frame. If we let this through, a message would be displayed on
10665 the terminal. */
10666 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10667 return 0;
10668 #endif /* HAVE_WINDOW_SYSTEM */
10670 /* Redraw garbaged frames. */
10671 clear_garbaged_frames ();
10673 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10675 echo_area_window = mini_window;
10676 window_height_changed_p = display_echo_area (w);
10677 w->must_be_updated_p = 1;
10679 /* Update the display, unless called from redisplay_internal.
10680 Also don't update the screen during redisplay itself. The
10681 update will happen at the end of redisplay, and an update
10682 here could cause confusion. */
10683 if (update_frame_p && !redisplaying_p)
10685 int n = 0;
10687 /* If the display update has been interrupted by pending
10688 input, update mode lines in the frame. Due to the
10689 pending input, it might have been that redisplay hasn't
10690 been called, so that mode lines above the echo area are
10691 garbaged. This looks odd, so we prevent it here. */
10692 if (!display_completed)
10693 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10695 if (window_height_changed_p
10696 /* Don't do this if Emacs is shutting down. Redisplay
10697 needs to run hooks. */
10698 && !NILP (Vrun_hooks))
10700 /* Must update other windows. Likewise as in other
10701 cases, don't let this update be interrupted by
10702 pending input. */
10703 ptrdiff_t count = SPECPDL_INDEX ();
10704 specbind (Qredisplay_dont_pause, Qt);
10705 windows_or_buffers_changed = 1;
10706 redisplay_internal ();
10707 unbind_to (count, Qnil);
10709 else if (FRAME_WINDOW_P (f) && n == 0)
10711 /* Window configuration is the same as before.
10712 Can do with a display update of the echo area,
10713 unless we displayed some mode lines. */
10714 update_single_window (w, 1);
10715 FRAME_RIF (f)->flush_display (f);
10717 else
10718 update_frame (f, 1, 1);
10720 /* If cursor is in the echo area, make sure that the next
10721 redisplay displays the minibuffer, so that the cursor will
10722 be replaced with what the minibuffer wants. */
10723 if (cursor_in_echo_area)
10724 ++windows_or_buffers_changed;
10727 else if (!EQ (mini_window, selected_window))
10728 windows_or_buffers_changed++;
10730 /* Last displayed message is now the current message. */
10731 echo_area_buffer[1] = echo_area_buffer[0];
10732 /* Inform read_char that we're not echoing. */
10733 echo_message_buffer = Qnil;
10735 /* Prevent redisplay optimization in redisplay_internal by resetting
10736 this_line_start_pos. This is done because the mini-buffer now
10737 displays the message instead of its buffer text. */
10738 if (EQ (mini_window, selected_window))
10739 CHARPOS (this_line_start_pos) = 0;
10741 return window_height_changed_p;
10744 /* Nonzero if the current window's buffer is shown in more than one
10745 window and was modified since last redisplay. */
10747 static int
10748 buffer_shared_and_changed (void)
10750 return (buffer_window_count (current_buffer) > 1
10751 && UNCHANGED_MODIFIED < MODIFF);
10754 /* Nonzero if W doesn't reflect the actual state of current buffer due
10755 to its text or overlays change. FIXME: this may be called when
10756 XBUFFER (w->contents) != current_buffer, which looks suspicious. */
10758 static int
10759 window_outdated (struct window *w)
10761 return (w->last_modified < MODIFF
10762 || w->last_overlay_modified < OVERLAY_MODIFF);
10765 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10766 is enabled and mark of W's buffer was changed since last W's update. */
10768 static int
10769 window_buffer_changed (struct window *w)
10771 struct buffer *b = XBUFFER (w->contents);
10773 eassert (BUFFER_LIVE_P (b));
10775 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10776 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10777 != (w->region_showing != 0)));
10780 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10782 static int
10783 mode_line_update_needed (struct window *w)
10785 return (w->column_number_displayed != -1
10786 && !(PT == w->last_point && !window_outdated (w))
10787 && (w->column_number_displayed != current_column ()));
10790 /***********************************************************************
10791 Mode Lines and Frame Titles
10792 ***********************************************************************/
10794 /* A buffer for constructing non-propertized mode-line strings and
10795 frame titles in it; allocated from the heap in init_xdisp and
10796 resized as needed in store_mode_line_noprop_char. */
10798 static char *mode_line_noprop_buf;
10800 /* The buffer's end, and a current output position in it. */
10802 static char *mode_line_noprop_buf_end;
10803 static char *mode_line_noprop_ptr;
10805 #define MODE_LINE_NOPROP_LEN(start) \
10806 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10808 static enum {
10809 MODE_LINE_DISPLAY = 0,
10810 MODE_LINE_TITLE,
10811 MODE_LINE_NOPROP,
10812 MODE_LINE_STRING
10813 } mode_line_target;
10815 /* Alist that caches the results of :propertize.
10816 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10817 static Lisp_Object mode_line_proptrans_alist;
10819 /* List of strings making up the mode-line. */
10820 static Lisp_Object mode_line_string_list;
10822 /* Base face property when building propertized mode line string. */
10823 static Lisp_Object mode_line_string_face;
10824 static Lisp_Object mode_line_string_face_prop;
10827 /* Unwind data for mode line strings */
10829 static Lisp_Object Vmode_line_unwind_vector;
10831 static Lisp_Object
10832 format_mode_line_unwind_data (struct frame *target_frame,
10833 struct buffer *obuf,
10834 Lisp_Object owin,
10835 int save_proptrans)
10837 Lisp_Object vector, tmp;
10839 /* Reduce consing by keeping one vector in
10840 Vwith_echo_area_save_vector. */
10841 vector = Vmode_line_unwind_vector;
10842 Vmode_line_unwind_vector = Qnil;
10844 if (NILP (vector))
10845 vector = Fmake_vector (make_number (10), Qnil);
10847 ASET (vector, 0, make_number (mode_line_target));
10848 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10849 ASET (vector, 2, mode_line_string_list);
10850 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10851 ASET (vector, 4, mode_line_string_face);
10852 ASET (vector, 5, mode_line_string_face_prop);
10854 if (obuf)
10855 XSETBUFFER (tmp, obuf);
10856 else
10857 tmp = Qnil;
10858 ASET (vector, 6, tmp);
10859 ASET (vector, 7, owin);
10860 if (target_frame)
10862 /* Similarly to `with-selected-window', if the operation selects
10863 a window on another frame, we must restore that frame's
10864 selected window, and (for a tty) the top-frame. */
10865 ASET (vector, 8, target_frame->selected_window);
10866 if (FRAME_TERMCAP_P (target_frame))
10867 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10870 return vector;
10873 static Lisp_Object
10874 unwind_format_mode_line (Lisp_Object vector)
10876 Lisp_Object old_window = AREF (vector, 7);
10877 Lisp_Object target_frame_window = AREF (vector, 8);
10878 Lisp_Object old_top_frame = AREF (vector, 9);
10880 mode_line_target = XINT (AREF (vector, 0));
10881 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10882 mode_line_string_list = AREF (vector, 2);
10883 if (! EQ (AREF (vector, 3), Qt))
10884 mode_line_proptrans_alist = AREF (vector, 3);
10885 mode_line_string_face = AREF (vector, 4);
10886 mode_line_string_face_prop = AREF (vector, 5);
10888 /* Select window before buffer, since it may change the buffer. */
10889 if (!NILP (old_window))
10891 /* If the operation that we are unwinding had selected a window
10892 on a different frame, reset its frame-selected-window. For a
10893 text terminal, reset its top-frame if necessary. */
10894 if (!NILP (target_frame_window))
10896 Lisp_Object frame
10897 = WINDOW_FRAME (XWINDOW (target_frame_window));
10899 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10900 Fselect_window (target_frame_window, Qt);
10902 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10903 Fselect_frame (old_top_frame, Qt);
10906 Fselect_window (old_window, Qt);
10909 if (!NILP (AREF (vector, 6)))
10911 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10912 ASET (vector, 6, Qnil);
10915 Vmode_line_unwind_vector = vector;
10916 return Qnil;
10920 /* Store a single character C for the frame title in mode_line_noprop_buf.
10921 Re-allocate mode_line_noprop_buf if necessary. */
10923 static void
10924 store_mode_line_noprop_char (char c)
10926 /* If output position has reached the end of the allocated buffer,
10927 increase the buffer's size. */
10928 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10930 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10931 ptrdiff_t size = len;
10932 mode_line_noprop_buf =
10933 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10934 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10935 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10938 *mode_line_noprop_ptr++ = c;
10942 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10943 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10944 characters that yield more columns than PRECISION; PRECISION <= 0
10945 means copy the whole string. Pad with spaces until FIELD_WIDTH
10946 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10947 pad. Called from display_mode_element when it is used to build a
10948 frame title. */
10950 static int
10951 store_mode_line_noprop (const char *string, int field_width, int precision)
10953 const unsigned char *str = (const unsigned char *) string;
10954 int n = 0;
10955 ptrdiff_t dummy, nbytes;
10957 /* Copy at most PRECISION chars from STR. */
10958 nbytes = strlen (string);
10959 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10960 while (nbytes--)
10961 store_mode_line_noprop_char (*str++);
10963 /* Fill up with spaces until FIELD_WIDTH reached. */
10964 while (field_width > 0
10965 && n < field_width)
10967 store_mode_line_noprop_char (' ');
10968 ++n;
10971 return n;
10974 /***********************************************************************
10975 Frame Titles
10976 ***********************************************************************/
10978 #ifdef HAVE_WINDOW_SYSTEM
10980 /* Set the title of FRAME, if it has changed. The title format is
10981 Vicon_title_format if FRAME is iconified, otherwise it is
10982 frame_title_format. */
10984 static void
10985 x_consider_frame_title (Lisp_Object frame)
10987 struct frame *f = XFRAME (frame);
10989 if (FRAME_WINDOW_P (f)
10990 || FRAME_MINIBUF_ONLY_P (f)
10991 || f->explicit_name)
10993 /* Do we have more than one visible frame on this X display? */
10994 Lisp_Object tail, other_frame, fmt;
10995 ptrdiff_t title_start;
10996 char *title;
10997 ptrdiff_t len;
10998 struct it it;
10999 ptrdiff_t count = SPECPDL_INDEX ();
11001 FOR_EACH_FRAME (tail, other_frame)
11003 struct frame *tf = XFRAME (other_frame);
11005 if (tf != f
11006 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11007 && !FRAME_MINIBUF_ONLY_P (tf)
11008 && !EQ (other_frame, tip_frame)
11009 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11010 break;
11013 /* Set global variable indicating that multiple frames exist. */
11014 multiple_frames = CONSP (tail);
11016 /* Switch to the buffer of selected window of the frame. Set up
11017 mode_line_target so that display_mode_element will output into
11018 mode_line_noprop_buf; then display the title. */
11019 record_unwind_protect (unwind_format_mode_line,
11020 format_mode_line_unwind_data
11021 (f, current_buffer, selected_window, 0));
11023 Fselect_window (f->selected_window, Qt);
11024 set_buffer_internal_1
11025 (XBUFFER (XWINDOW (f->selected_window)->contents));
11026 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11028 mode_line_target = MODE_LINE_TITLE;
11029 title_start = MODE_LINE_NOPROP_LEN (0);
11030 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11031 NULL, DEFAULT_FACE_ID);
11032 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11033 len = MODE_LINE_NOPROP_LEN (title_start);
11034 title = mode_line_noprop_buf + title_start;
11035 unbind_to (count, Qnil);
11037 /* Set the title only if it's changed. This avoids consing in
11038 the common case where it hasn't. (If it turns out that we've
11039 already wasted too much time by walking through the list with
11040 display_mode_element, then we might need to optimize at a
11041 higher level than this.) */
11042 if (! STRINGP (f->name)
11043 || SBYTES (f->name) != len
11044 || memcmp (title, SDATA (f->name), len) != 0)
11045 x_implicitly_set_name (f, make_string (title, len), Qnil);
11049 #endif /* not HAVE_WINDOW_SYSTEM */
11052 /***********************************************************************
11053 Menu Bars
11054 ***********************************************************************/
11057 /* Prepare for redisplay by updating menu-bar item lists when
11058 appropriate. This can call eval. */
11060 void
11061 prepare_menu_bars (void)
11063 int all_windows;
11064 struct gcpro gcpro1, gcpro2;
11065 struct frame *f;
11066 Lisp_Object tooltip_frame;
11068 #ifdef HAVE_WINDOW_SYSTEM
11069 tooltip_frame = tip_frame;
11070 #else
11071 tooltip_frame = Qnil;
11072 #endif
11074 /* Update all frame titles based on their buffer names, etc. We do
11075 this before the menu bars so that the buffer-menu will show the
11076 up-to-date frame titles. */
11077 #ifdef HAVE_WINDOW_SYSTEM
11078 if (windows_or_buffers_changed || update_mode_lines)
11080 Lisp_Object tail, frame;
11082 FOR_EACH_FRAME (tail, frame)
11084 f = XFRAME (frame);
11085 if (!EQ (frame, tooltip_frame)
11086 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11087 x_consider_frame_title (frame);
11090 #endif /* HAVE_WINDOW_SYSTEM */
11092 /* Update the menu bar item lists, if appropriate. This has to be
11093 done before any actual redisplay or generation of display lines. */
11094 all_windows = (update_mode_lines
11095 || buffer_shared_and_changed ()
11096 || windows_or_buffers_changed);
11097 if (all_windows)
11099 Lisp_Object tail, frame;
11100 ptrdiff_t count = SPECPDL_INDEX ();
11101 /* 1 means that update_menu_bar has run its hooks
11102 so any further calls to update_menu_bar shouldn't do so again. */
11103 int menu_bar_hooks_run = 0;
11105 record_unwind_save_match_data ();
11107 FOR_EACH_FRAME (tail, frame)
11109 f = XFRAME (frame);
11111 /* Ignore tooltip frame. */
11112 if (EQ (frame, tooltip_frame))
11113 continue;
11115 /* If a window on this frame changed size, report that to
11116 the user and clear the size-change flag. */
11117 if (FRAME_WINDOW_SIZES_CHANGED (f))
11119 Lisp_Object functions;
11121 /* Clear flag first in case we get an error below. */
11122 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11123 functions = Vwindow_size_change_functions;
11124 GCPRO2 (tail, functions);
11126 while (CONSP (functions))
11128 if (!EQ (XCAR (functions), Qt))
11129 call1 (XCAR (functions), frame);
11130 functions = XCDR (functions);
11132 UNGCPRO;
11135 GCPRO1 (tail);
11136 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11137 #ifdef HAVE_WINDOW_SYSTEM
11138 update_tool_bar (f, 0);
11139 #endif
11140 #ifdef HAVE_NS
11141 if (windows_or_buffers_changed
11142 && FRAME_NS_P (f))
11143 ns_set_doc_edited
11144 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11145 #endif
11146 UNGCPRO;
11149 unbind_to (count, Qnil);
11151 else
11153 struct frame *sf = SELECTED_FRAME ();
11154 update_menu_bar (sf, 1, 0);
11155 #ifdef HAVE_WINDOW_SYSTEM
11156 update_tool_bar (sf, 1);
11157 #endif
11162 /* Update the menu bar item list for frame F. This has to be done
11163 before we start to fill in any display lines, because it can call
11164 eval.
11166 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11168 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11169 already ran the menu bar hooks for this redisplay, so there
11170 is no need to run them again. The return value is the
11171 updated value of this flag, to pass to the next call. */
11173 static int
11174 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11176 Lisp_Object window;
11177 register struct window *w;
11179 /* If called recursively during a menu update, do nothing. This can
11180 happen when, for instance, an activate-menubar-hook causes a
11181 redisplay. */
11182 if (inhibit_menubar_update)
11183 return hooks_run;
11185 window = FRAME_SELECTED_WINDOW (f);
11186 w = XWINDOW (window);
11188 if (FRAME_WINDOW_P (f)
11190 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11191 || defined (HAVE_NS) || defined (USE_GTK)
11192 FRAME_EXTERNAL_MENU_BAR (f)
11193 #else
11194 FRAME_MENU_BAR_LINES (f) > 0
11195 #endif
11196 : FRAME_MENU_BAR_LINES (f) > 0)
11198 /* If the user has switched buffers or windows, we need to
11199 recompute to reflect the new bindings. But we'll
11200 recompute when update_mode_lines is set too; that means
11201 that people can use force-mode-line-update to request
11202 that the menu bar be recomputed. The adverse effect on
11203 the rest of the redisplay algorithm is about the same as
11204 windows_or_buffers_changed anyway. */
11205 if (windows_or_buffers_changed
11206 /* This used to test w->update_mode_line, but we believe
11207 there is no need to recompute the menu in that case. */
11208 || update_mode_lines
11209 || window_buffer_changed (w))
11211 struct buffer *prev = current_buffer;
11212 ptrdiff_t count = SPECPDL_INDEX ();
11214 specbind (Qinhibit_menubar_update, Qt);
11216 set_buffer_internal_1 (XBUFFER (w->contents));
11217 if (save_match_data)
11218 record_unwind_save_match_data ();
11219 if (NILP (Voverriding_local_map_menu_flag))
11221 specbind (Qoverriding_terminal_local_map, Qnil);
11222 specbind (Qoverriding_local_map, Qnil);
11225 if (!hooks_run)
11227 /* Run the Lucid hook. */
11228 safe_run_hooks (Qactivate_menubar_hook);
11230 /* If it has changed current-menubar from previous value,
11231 really recompute the menu-bar from the value. */
11232 if (! NILP (Vlucid_menu_bar_dirty_flag))
11233 call0 (Qrecompute_lucid_menubar);
11235 safe_run_hooks (Qmenu_bar_update_hook);
11237 hooks_run = 1;
11240 XSETFRAME (Vmenu_updating_frame, f);
11241 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11243 /* Redisplay the menu bar in case we changed it. */
11244 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11245 || defined (HAVE_NS) || defined (USE_GTK)
11246 if (FRAME_WINDOW_P (f))
11248 #if defined (HAVE_NS)
11249 /* All frames on Mac OS share the same menubar. So only
11250 the selected frame should be allowed to set it. */
11251 if (f == SELECTED_FRAME ())
11252 #endif
11253 set_frame_menubar (f, 0, 0);
11255 else
11256 /* On a terminal screen, the menu bar is an ordinary screen
11257 line, and this makes it get updated. */
11258 w->update_mode_line = 1;
11259 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11260 /* In the non-toolkit version, the menu bar is an ordinary screen
11261 line, and this makes it get updated. */
11262 w->update_mode_line = 1;
11263 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11265 unbind_to (count, Qnil);
11266 set_buffer_internal_1 (prev);
11270 return hooks_run;
11275 /***********************************************************************
11276 Output Cursor
11277 ***********************************************************************/
11279 #ifdef HAVE_WINDOW_SYSTEM
11281 /* EXPORT:
11282 Nominal cursor position -- where to draw output.
11283 HPOS and VPOS are window relative glyph matrix coordinates.
11284 X and Y are window relative pixel coordinates. */
11286 struct cursor_pos output_cursor;
11289 /* EXPORT:
11290 Set the global variable output_cursor to CURSOR. All cursor
11291 positions are relative to updated_window. */
11293 void
11294 set_output_cursor (struct cursor_pos *cursor)
11296 output_cursor.hpos = cursor->hpos;
11297 output_cursor.vpos = cursor->vpos;
11298 output_cursor.x = cursor->x;
11299 output_cursor.y = cursor->y;
11303 /* EXPORT for RIF:
11304 Set a nominal cursor position.
11306 HPOS and VPOS are column/row positions in a window glyph matrix. X
11307 and Y are window text area relative pixel positions.
11309 If this is done during an update, updated_window will contain the
11310 window that is being updated and the position is the future output
11311 cursor position for that window. If updated_window is null, use
11312 selected_window and display the cursor at the given position. */
11314 void
11315 x_cursor_to (int vpos, int hpos, int y, int x)
11317 struct window *w;
11319 /* If updated_window is not set, work on selected_window. */
11320 if (updated_window)
11321 w = updated_window;
11322 else
11323 w = XWINDOW (selected_window);
11325 /* Set the output cursor. */
11326 output_cursor.hpos = hpos;
11327 output_cursor.vpos = vpos;
11328 output_cursor.x = x;
11329 output_cursor.y = y;
11331 /* If not called as part of an update, really display the cursor.
11332 This will also set the cursor position of W. */
11333 if (updated_window == NULL)
11335 block_input ();
11336 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11337 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11338 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11339 unblock_input ();
11343 #endif /* HAVE_WINDOW_SYSTEM */
11346 /***********************************************************************
11347 Tool-bars
11348 ***********************************************************************/
11350 #ifdef HAVE_WINDOW_SYSTEM
11352 /* Where the mouse was last time we reported a mouse event. */
11354 FRAME_PTR last_mouse_frame;
11356 /* Tool-bar item index of the item on which a mouse button was pressed
11357 or -1. */
11359 int last_tool_bar_item;
11361 /* Select `frame' temporarily without running all the code in
11362 do_switch_frame.
11363 FIXME: Maybe do_switch_frame should be trimmed down similarly
11364 when `norecord' is set. */
11365 static Lisp_Object
11366 fast_set_selected_frame (Lisp_Object frame)
11368 if (!EQ (selected_frame, frame))
11370 selected_frame = frame;
11371 selected_window = XFRAME (frame)->selected_window;
11373 return Qnil;
11376 /* Update the tool-bar item list for frame F. This has to be done
11377 before we start to fill in any display lines. Called from
11378 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11379 and restore it here. */
11381 static void
11382 update_tool_bar (struct frame *f, int save_match_data)
11384 #if defined (USE_GTK) || defined (HAVE_NS)
11385 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11386 #else
11387 int do_update = WINDOWP (f->tool_bar_window)
11388 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11389 #endif
11391 if (do_update)
11393 Lisp_Object window;
11394 struct window *w;
11396 window = FRAME_SELECTED_WINDOW (f);
11397 w = XWINDOW (window);
11399 /* If the user has switched buffers or windows, we need to
11400 recompute to reflect the new bindings. But we'll
11401 recompute when update_mode_lines is set too; that means
11402 that people can use force-mode-line-update to request
11403 that the menu bar be recomputed. The adverse effect on
11404 the rest of the redisplay algorithm is about the same as
11405 windows_or_buffers_changed anyway. */
11406 if (windows_or_buffers_changed
11407 || w->update_mode_line
11408 || update_mode_lines
11409 || window_buffer_changed (w))
11411 struct buffer *prev = current_buffer;
11412 ptrdiff_t count = SPECPDL_INDEX ();
11413 Lisp_Object frame, new_tool_bar;
11414 int new_n_tool_bar;
11415 struct gcpro gcpro1;
11417 /* Set current_buffer to the buffer of the selected
11418 window of the frame, so that we get the right local
11419 keymaps. */
11420 set_buffer_internal_1 (XBUFFER (w->contents));
11422 /* Save match data, if we must. */
11423 if (save_match_data)
11424 record_unwind_save_match_data ();
11426 /* Make sure that we don't accidentally use bogus keymaps. */
11427 if (NILP (Voverriding_local_map_menu_flag))
11429 specbind (Qoverriding_terminal_local_map, Qnil);
11430 specbind (Qoverriding_local_map, Qnil);
11433 GCPRO1 (new_tool_bar);
11435 /* We must temporarily set the selected frame to this frame
11436 before calling tool_bar_items, because the calculation of
11437 the tool-bar keymap uses the selected frame (see
11438 `tool-bar-make-keymap' in tool-bar.el). */
11439 eassert (EQ (selected_window,
11440 /* Since we only explicitly preserve selected_frame,
11441 check that selected_window would be redundant. */
11442 XFRAME (selected_frame)->selected_window));
11443 record_unwind_protect (fast_set_selected_frame, selected_frame);
11444 XSETFRAME (frame, f);
11445 fast_set_selected_frame (frame);
11447 /* Build desired tool-bar items from keymaps. */
11448 new_tool_bar
11449 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11450 &new_n_tool_bar);
11452 /* Redisplay the tool-bar if we changed it. */
11453 if (new_n_tool_bar != f->n_tool_bar_items
11454 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11456 /* Redisplay that happens asynchronously due to an expose event
11457 may access f->tool_bar_items. Make sure we update both
11458 variables within BLOCK_INPUT so no such event interrupts. */
11459 block_input ();
11460 fset_tool_bar_items (f, new_tool_bar);
11461 f->n_tool_bar_items = new_n_tool_bar;
11462 w->update_mode_line = 1;
11463 unblock_input ();
11466 UNGCPRO;
11468 unbind_to (count, Qnil);
11469 set_buffer_internal_1 (prev);
11475 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11476 F's desired tool-bar contents. F->tool_bar_items must have
11477 been set up previously by calling prepare_menu_bars. */
11479 static void
11480 build_desired_tool_bar_string (struct frame *f)
11482 int i, size, size_needed;
11483 struct gcpro gcpro1, gcpro2, gcpro3;
11484 Lisp_Object image, plist, props;
11486 image = plist = props = Qnil;
11487 GCPRO3 (image, plist, props);
11489 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11490 Otherwise, make a new string. */
11492 /* The size of the string we might be able to reuse. */
11493 size = (STRINGP (f->desired_tool_bar_string)
11494 ? SCHARS (f->desired_tool_bar_string)
11495 : 0);
11497 /* We need one space in the string for each image. */
11498 size_needed = f->n_tool_bar_items;
11500 /* Reuse f->desired_tool_bar_string, if possible. */
11501 if (size < size_needed || NILP (f->desired_tool_bar_string))
11502 fset_desired_tool_bar_string
11503 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11504 else
11506 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11507 Fremove_text_properties (make_number (0), make_number (size),
11508 props, f->desired_tool_bar_string);
11511 /* Put a `display' property on the string for the images to display,
11512 put a `menu_item' property on tool-bar items with a value that
11513 is the index of the item in F's tool-bar item vector. */
11514 for (i = 0; i < f->n_tool_bar_items; ++i)
11516 #define PROP(IDX) \
11517 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11519 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11520 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11521 int hmargin, vmargin, relief, idx, end;
11523 /* If image is a vector, choose the image according to the
11524 button state. */
11525 image = PROP (TOOL_BAR_ITEM_IMAGES);
11526 if (VECTORP (image))
11528 if (enabled_p)
11529 idx = (selected_p
11530 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11531 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11532 else
11533 idx = (selected_p
11534 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11535 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11537 eassert (ASIZE (image) >= idx);
11538 image = AREF (image, idx);
11540 else
11541 idx = -1;
11543 /* Ignore invalid image specifications. */
11544 if (!valid_image_p (image))
11545 continue;
11547 /* Display the tool-bar button pressed, or depressed. */
11548 plist = Fcopy_sequence (XCDR (image));
11550 /* Compute margin and relief to draw. */
11551 relief = (tool_bar_button_relief >= 0
11552 ? tool_bar_button_relief
11553 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11554 hmargin = vmargin = relief;
11556 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11557 INT_MAX - max (hmargin, vmargin)))
11559 hmargin += XFASTINT (Vtool_bar_button_margin);
11560 vmargin += XFASTINT (Vtool_bar_button_margin);
11562 else if (CONSP (Vtool_bar_button_margin))
11564 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11565 INT_MAX - hmargin))
11566 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11568 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11569 INT_MAX - vmargin))
11570 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11573 if (auto_raise_tool_bar_buttons_p)
11575 /* Add a `:relief' property to the image spec if the item is
11576 selected. */
11577 if (selected_p)
11579 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11580 hmargin -= relief;
11581 vmargin -= relief;
11584 else
11586 /* If image is selected, display it pressed, i.e. with a
11587 negative relief. If it's not selected, display it with a
11588 raised relief. */
11589 plist = Fplist_put (plist, QCrelief,
11590 (selected_p
11591 ? make_number (-relief)
11592 : make_number (relief)));
11593 hmargin -= relief;
11594 vmargin -= relief;
11597 /* Put a margin around the image. */
11598 if (hmargin || vmargin)
11600 if (hmargin == vmargin)
11601 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11602 else
11603 plist = Fplist_put (plist, QCmargin,
11604 Fcons (make_number (hmargin),
11605 make_number (vmargin)));
11608 /* If button is not enabled, and we don't have special images
11609 for the disabled state, make the image appear disabled by
11610 applying an appropriate algorithm to it. */
11611 if (!enabled_p && idx < 0)
11612 plist = Fplist_put (plist, QCconversion, Qdisabled);
11614 /* Put a `display' text property on the string for the image to
11615 display. Put a `menu-item' property on the string that gives
11616 the start of this item's properties in the tool-bar items
11617 vector. */
11618 image = Fcons (Qimage, plist);
11619 props = list4 (Qdisplay, image,
11620 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11622 /* Let the last image hide all remaining spaces in the tool bar
11623 string. The string can be longer than needed when we reuse a
11624 previous string. */
11625 if (i + 1 == f->n_tool_bar_items)
11626 end = SCHARS (f->desired_tool_bar_string);
11627 else
11628 end = i + 1;
11629 Fadd_text_properties (make_number (i), make_number (end),
11630 props, f->desired_tool_bar_string);
11631 #undef PROP
11634 UNGCPRO;
11638 /* Display one line of the tool-bar of frame IT->f.
11640 HEIGHT specifies the desired height of the tool-bar line.
11641 If the actual height of the glyph row is less than HEIGHT, the
11642 row's height is increased to HEIGHT, and the icons are centered
11643 vertically in the new height.
11645 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11646 count a final empty row in case the tool-bar width exactly matches
11647 the window width.
11650 static void
11651 display_tool_bar_line (struct it *it, int height)
11653 struct glyph_row *row = it->glyph_row;
11654 int max_x = it->last_visible_x;
11655 struct glyph *last;
11657 prepare_desired_row (row);
11658 row->y = it->current_y;
11660 /* Note that this isn't made use of if the face hasn't a box,
11661 so there's no need to check the face here. */
11662 it->start_of_box_run_p = 1;
11664 while (it->current_x < max_x)
11666 int x, n_glyphs_before, i, nglyphs;
11667 struct it it_before;
11669 /* Get the next display element. */
11670 if (!get_next_display_element (it))
11672 /* Don't count empty row if we are counting needed tool-bar lines. */
11673 if (height < 0 && !it->hpos)
11674 return;
11675 break;
11678 /* Produce glyphs. */
11679 n_glyphs_before = row->used[TEXT_AREA];
11680 it_before = *it;
11682 PRODUCE_GLYPHS (it);
11684 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11685 i = 0;
11686 x = it_before.current_x;
11687 while (i < nglyphs)
11689 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11691 if (x + glyph->pixel_width > max_x)
11693 /* Glyph doesn't fit on line. Backtrack. */
11694 row->used[TEXT_AREA] = n_glyphs_before;
11695 *it = it_before;
11696 /* If this is the only glyph on this line, it will never fit on the
11697 tool-bar, so skip it. But ensure there is at least one glyph,
11698 so we don't accidentally disable the tool-bar. */
11699 if (n_glyphs_before == 0
11700 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11701 break;
11702 goto out;
11705 ++it->hpos;
11706 x += glyph->pixel_width;
11707 ++i;
11710 /* Stop at line end. */
11711 if (ITERATOR_AT_END_OF_LINE_P (it))
11712 break;
11714 set_iterator_to_next (it, 1);
11717 out:;
11719 row->displays_text_p = row->used[TEXT_AREA] != 0;
11721 /* Use default face for the border below the tool bar.
11723 FIXME: When auto-resize-tool-bars is grow-only, there is
11724 no additional border below the possibly empty tool-bar lines.
11725 So to make the extra empty lines look "normal", we have to
11726 use the tool-bar face for the border too. */
11727 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11728 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11729 it->face_id = DEFAULT_FACE_ID;
11731 extend_face_to_end_of_line (it);
11732 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11733 last->right_box_line_p = 1;
11734 if (last == row->glyphs[TEXT_AREA])
11735 last->left_box_line_p = 1;
11737 /* Make line the desired height and center it vertically. */
11738 if ((height -= it->max_ascent + it->max_descent) > 0)
11740 /* Don't add more than one line height. */
11741 height %= FRAME_LINE_HEIGHT (it->f);
11742 it->max_ascent += height / 2;
11743 it->max_descent += (height + 1) / 2;
11746 compute_line_metrics (it);
11748 /* If line is empty, make it occupy the rest of the tool-bar. */
11749 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11751 row->height = row->phys_height = it->last_visible_y - row->y;
11752 row->visible_height = row->height;
11753 row->ascent = row->phys_ascent = 0;
11754 row->extra_line_spacing = 0;
11757 row->full_width_p = 1;
11758 row->continued_p = 0;
11759 row->truncated_on_left_p = 0;
11760 row->truncated_on_right_p = 0;
11762 it->current_x = it->hpos = 0;
11763 it->current_y += row->height;
11764 ++it->vpos;
11765 ++it->glyph_row;
11769 /* Max tool-bar height. */
11771 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11772 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11774 /* Value is the number of screen lines needed to make all tool-bar
11775 items of frame F visible. The number of actual rows needed is
11776 returned in *N_ROWS if non-NULL. */
11778 static int
11779 tool_bar_lines_needed (struct frame *f, int *n_rows)
11781 struct window *w = XWINDOW (f->tool_bar_window);
11782 struct it it;
11783 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11784 the desired matrix, so use (unused) mode-line row as temporary row to
11785 avoid destroying the first tool-bar row. */
11786 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11788 /* Initialize an iterator for iteration over
11789 F->desired_tool_bar_string in the tool-bar window of frame F. */
11790 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11791 it.first_visible_x = 0;
11792 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11793 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11794 it.paragraph_embedding = L2R;
11796 while (!ITERATOR_AT_END_P (&it))
11798 clear_glyph_row (temp_row);
11799 it.glyph_row = temp_row;
11800 display_tool_bar_line (&it, -1);
11802 clear_glyph_row (temp_row);
11804 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11805 if (n_rows)
11806 *n_rows = it.vpos > 0 ? it.vpos : -1;
11808 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11812 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11813 0, 1, 0,
11814 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11815 If FRAME is nil or omitted, use the selected frame. */)
11816 (Lisp_Object frame)
11818 struct frame *f = decode_any_frame (frame);
11819 struct window *w;
11820 int nlines = 0;
11822 if (WINDOWP (f->tool_bar_window)
11823 && (w = XWINDOW (f->tool_bar_window),
11824 WINDOW_TOTAL_LINES (w) > 0))
11826 update_tool_bar (f, 1);
11827 if (f->n_tool_bar_items)
11829 build_desired_tool_bar_string (f);
11830 nlines = tool_bar_lines_needed (f, NULL);
11834 return make_number (nlines);
11838 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11839 height should be changed. */
11841 static int
11842 redisplay_tool_bar (struct frame *f)
11844 struct window *w;
11845 struct it it;
11846 struct glyph_row *row;
11848 #if defined (USE_GTK) || defined (HAVE_NS)
11849 if (FRAME_EXTERNAL_TOOL_BAR (f))
11850 update_frame_tool_bar (f);
11851 return 0;
11852 #endif
11854 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11855 do anything. This means you must start with tool-bar-lines
11856 non-zero to get the auto-sizing effect. Or in other words, you
11857 can turn off tool-bars by specifying tool-bar-lines zero. */
11858 if (!WINDOWP (f->tool_bar_window)
11859 || (w = XWINDOW (f->tool_bar_window),
11860 WINDOW_TOTAL_LINES (w) == 0))
11861 return 0;
11863 /* Set up an iterator for the tool-bar window. */
11864 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11865 it.first_visible_x = 0;
11866 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11867 row = it.glyph_row;
11869 /* Build a string that represents the contents of the tool-bar. */
11870 build_desired_tool_bar_string (f);
11871 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11872 /* FIXME: This should be controlled by a user option. But it
11873 doesn't make sense to have an R2L tool bar if the menu bar cannot
11874 be drawn also R2L, and making the menu bar R2L is tricky due
11875 toolkit-specific code that implements it. If an R2L tool bar is
11876 ever supported, display_tool_bar_line should also be augmented to
11877 call unproduce_glyphs like display_line and display_string
11878 do. */
11879 it.paragraph_embedding = L2R;
11881 if (f->n_tool_bar_rows == 0)
11883 int nlines;
11885 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11886 nlines != WINDOW_TOTAL_LINES (w)))
11888 Lisp_Object frame;
11889 int old_height = WINDOW_TOTAL_LINES (w);
11891 XSETFRAME (frame, f);
11892 Fmodify_frame_parameters (frame,
11893 Fcons (Fcons (Qtool_bar_lines,
11894 make_number (nlines)),
11895 Qnil));
11896 if (WINDOW_TOTAL_LINES (w) != old_height)
11898 clear_glyph_matrix (w->desired_matrix);
11899 fonts_changed_p = 1;
11900 return 1;
11905 /* Display as many lines as needed to display all tool-bar items. */
11907 if (f->n_tool_bar_rows > 0)
11909 int border, rows, height, extra;
11911 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11912 border = XINT (Vtool_bar_border);
11913 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11914 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11915 else if (EQ (Vtool_bar_border, Qborder_width))
11916 border = f->border_width;
11917 else
11918 border = 0;
11919 if (border < 0)
11920 border = 0;
11922 rows = f->n_tool_bar_rows;
11923 height = max (1, (it.last_visible_y - border) / rows);
11924 extra = it.last_visible_y - border - height * rows;
11926 while (it.current_y < it.last_visible_y)
11928 int h = 0;
11929 if (extra > 0 && rows-- > 0)
11931 h = (extra + rows - 1) / rows;
11932 extra -= h;
11934 display_tool_bar_line (&it, height + h);
11937 else
11939 while (it.current_y < it.last_visible_y)
11940 display_tool_bar_line (&it, 0);
11943 /* It doesn't make much sense to try scrolling in the tool-bar
11944 window, so don't do it. */
11945 w->desired_matrix->no_scrolling_p = 1;
11946 w->must_be_updated_p = 1;
11948 if (!NILP (Vauto_resize_tool_bars))
11950 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11951 int change_height_p = 0;
11953 /* If we couldn't display everything, change the tool-bar's
11954 height if there is room for more. */
11955 if (IT_STRING_CHARPOS (it) < it.end_charpos
11956 && it.current_y < max_tool_bar_height)
11957 change_height_p = 1;
11959 row = it.glyph_row - 1;
11961 /* If there are blank lines at the end, except for a partially
11962 visible blank line at the end that is smaller than
11963 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11964 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11965 && row->height >= FRAME_LINE_HEIGHT (f))
11966 change_height_p = 1;
11968 /* If row displays tool-bar items, but is partially visible,
11969 change the tool-bar's height. */
11970 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11971 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11972 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11973 change_height_p = 1;
11975 /* Resize windows as needed by changing the `tool-bar-lines'
11976 frame parameter. */
11977 if (change_height_p)
11979 Lisp_Object frame;
11980 int old_height = WINDOW_TOTAL_LINES (w);
11981 int nrows;
11982 int nlines = tool_bar_lines_needed (f, &nrows);
11984 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11985 && !f->minimize_tool_bar_window_p)
11986 ? (nlines > old_height)
11987 : (nlines != old_height));
11988 f->minimize_tool_bar_window_p = 0;
11990 if (change_height_p)
11992 XSETFRAME (frame, f);
11993 Fmodify_frame_parameters (frame,
11994 Fcons (Fcons (Qtool_bar_lines,
11995 make_number (nlines)),
11996 Qnil));
11997 if (WINDOW_TOTAL_LINES (w) != old_height)
11999 clear_glyph_matrix (w->desired_matrix);
12000 f->n_tool_bar_rows = nrows;
12001 fonts_changed_p = 1;
12002 return 1;
12008 f->minimize_tool_bar_window_p = 0;
12009 return 0;
12013 /* Get information about the tool-bar item which is displayed in GLYPH
12014 on frame F. Return in *PROP_IDX the index where tool-bar item
12015 properties start in F->tool_bar_items. Value is zero if
12016 GLYPH doesn't display a tool-bar item. */
12018 static int
12019 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12021 Lisp_Object prop;
12022 int success_p;
12023 int charpos;
12025 /* This function can be called asynchronously, which means we must
12026 exclude any possibility that Fget_text_property signals an
12027 error. */
12028 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12029 charpos = max (0, charpos);
12031 /* Get the text property `menu-item' at pos. The value of that
12032 property is the start index of this item's properties in
12033 F->tool_bar_items. */
12034 prop = Fget_text_property (make_number (charpos),
12035 Qmenu_item, f->current_tool_bar_string);
12036 if (INTEGERP (prop))
12038 *prop_idx = XINT (prop);
12039 success_p = 1;
12041 else
12042 success_p = 0;
12044 return success_p;
12048 /* Get information about the tool-bar item at position X/Y on frame F.
12049 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12050 the current matrix of the tool-bar window of F, or NULL if not
12051 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12052 item in F->tool_bar_items. Value is
12054 -1 if X/Y is not on a tool-bar item
12055 0 if X/Y is on the same item that was highlighted before.
12056 1 otherwise. */
12058 static int
12059 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12060 int *hpos, int *vpos, int *prop_idx)
12062 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12063 struct window *w = XWINDOW (f->tool_bar_window);
12064 int area;
12066 /* Find the glyph under X/Y. */
12067 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12068 if (*glyph == NULL)
12069 return -1;
12071 /* Get the start of this tool-bar item's properties in
12072 f->tool_bar_items. */
12073 if (!tool_bar_item_info (f, *glyph, prop_idx))
12074 return -1;
12076 /* Is mouse on the highlighted item? */
12077 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12078 && *vpos >= hlinfo->mouse_face_beg_row
12079 && *vpos <= hlinfo->mouse_face_end_row
12080 && (*vpos > hlinfo->mouse_face_beg_row
12081 || *hpos >= hlinfo->mouse_face_beg_col)
12082 && (*vpos < hlinfo->mouse_face_end_row
12083 || *hpos < hlinfo->mouse_face_end_col
12084 || hlinfo->mouse_face_past_end))
12085 return 0;
12087 return 1;
12091 /* EXPORT:
12092 Handle mouse button event on the tool-bar of frame F, at
12093 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12094 0 for button release. MODIFIERS is event modifiers for button
12095 release. */
12097 void
12098 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12099 int modifiers)
12101 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12102 struct window *w = XWINDOW (f->tool_bar_window);
12103 int hpos, vpos, prop_idx;
12104 struct glyph *glyph;
12105 Lisp_Object enabled_p;
12107 /* If not on the highlighted tool-bar item, return. */
12108 frame_to_window_pixel_xy (w, &x, &y);
12109 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12110 return;
12112 /* If item is disabled, do nothing. */
12113 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12114 if (NILP (enabled_p))
12115 return;
12117 if (down_p)
12119 /* Show item in pressed state. */
12120 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12121 last_tool_bar_item = prop_idx;
12123 else
12125 Lisp_Object key, frame;
12126 struct input_event event;
12127 EVENT_INIT (event);
12129 /* Show item in released state. */
12130 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12132 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12134 XSETFRAME (frame, f);
12135 event.kind = TOOL_BAR_EVENT;
12136 event.frame_or_window = frame;
12137 event.arg = frame;
12138 kbd_buffer_store_event (&event);
12140 event.kind = TOOL_BAR_EVENT;
12141 event.frame_or_window = frame;
12142 event.arg = key;
12143 event.modifiers = modifiers;
12144 kbd_buffer_store_event (&event);
12145 last_tool_bar_item = -1;
12150 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12151 tool-bar window-relative coordinates X/Y. Called from
12152 note_mouse_highlight. */
12154 static void
12155 note_tool_bar_highlight (struct frame *f, int x, int y)
12157 Lisp_Object window = f->tool_bar_window;
12158 struct window *w = XWINDOW (window);
12159 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12160 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12161 int hpos, vpos;
12162 struct glyph *glyph;
12163 struct glyph_row *row;
12164 int i;
12165 Lisp_Object enabled_p;
12166 int prop_idx;
12167 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12168 int mouse_down_p, rc;
12170 /* Function note_mouse_highlight is called with negative X/Y
12171 values when mouse moves outside of the frame. */
12172 if (x <= 0 || y <= 0)
12174 clear_mouse_face (hlinfo);
12175 return;
12178 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12179 if (rc < 0)
12181 /* Not on tool-bar item. */
12182 clear_mouse_face (hlinfo);
12183 return;
12185 else if (rc == 0)
12186 /* On same tool-bar item as before. */
12187 goto set_help_echo;
12189 clear_mouse_face (hlinfo);
12191 /* Mouse is down, but on different tool-bar item? */
12192 mouse_down_p = (dpyinfo->grabbed
12193 && f == last_mouse_frame
12194 && FRAME_LIVE_P (f));
12195 if (mouse_down_p
12196 && last_tool_bar_item != prop_idx)
12197 return;
12199 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12201 /* If tool-bar item is not enabled, don't highlight it. */
12202 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12203 if (!NILP (enabled_p))
12205 /* Compute the x-position of the glyph. In front and past the
12206 image is a space. We include this in the highlighted area. */
12207 row = MATRIX_ROW (w->current_matrix, vpos);
12208 for (i = x = 0; i < hpos; ++i)
12209 x += row->glyphs[TEXT_AREA][i].pixel_width;
12211 /* Record this as the current active region. */
12212 hlinfo->mouse_face_beg_col = hpos;
12213 hlinfo->mouse_face_beg_row = vpos;
12214 hlinfo->mouse_face_beg_x = x;
12215 hlinfo->mouse_face_beg_y = row->y;
12216 hlinfo->mouse_face_past_end = 0;
12218 hlinfo->mouse_face_end_col = hpos + 1;
12219 hlinfo->mouse_face_end_row = vpos;
12220 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12221 hlinfo->mouse_face_end_y = row->y;
12222 hlinfo->mouse_face_window = window;
12223 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12225 /* Display it as active. */
12226 show_mouse_face (hlinfo, draw);
12229 set_help_echo:
12231 /* Set help_echo_string to a help string to display for this tool-bar item.
12232 XTread_socket does the rest. */
12233 help_echo_object = help_echo_window = Qnil;
12234 help_echo_pos = -1;
12235 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12236 if (NILP (help_echo_string))
12237 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12240 #endif /* HAVE_WINDOW_SYSTEM */
12244 /************************************************************************
12245 Horizontal scrolling
12246 ************************************************************************/
12248 static int hscroll_window_tree (Lisp_Object);
12249 static int hscroll_windows (Lisp_Object);
12251 /* For all leaf windows in the window tree rooted at WINDOW, set their
12252 hscroll value so that PT is (i) visible in the window, and (ii) so
12253 that it is not within a certain margin at the window's left and
12254 right border. Value is non-zero if any window's hscroll has been
12255 changed. */
12257 static int
12258 hscroll_window_tree (Lisp_Object window)
12260 int hscrolled_p = 0;
12261 int hscroll_relative_p = FLOATP (Vhscroll_step);
12262 int hscroll_step_abs = 0;
12263 double hscroll_step_rel = 0;
12265 if (hscroll_relative_p)
12267 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12268 if (hscroll_step_rel < 0)
12270 hscroll_relative_p = 0;
12271 hscroll_step_abs = 0;
12274 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12276 hscroll_step_abs = XINT (Vhscroll_step);
12277 if (hscroll_step_abs < 0)
12278 hscroll_step_abs = 0;
12280 else
12281 hscroll_step_abs = 0;
12283 while (WINDOWP (window))
12285 struct window *w = XWINDOW (window);
12287 if (WINDOWP (w->contents))
12288 hscrolled_p |= hscroll_window_tree (w->contents);
12289 else if (w->cursor.vpos >= 0)
12291 int h_margin;
12292 int text_area_width;
12293 struct glyph_row *current_cursor_row
12294 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12295 struct glyph_row *desired_cursor_row
12296 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12297 struct glyph_row *cursor_row
12298 = (desired_cursor_row->enabled_p
12299 ? desired_cursor_row
12300 : current_cursor_row);
12301 int row_r2l_p = cursor_row->reversed_p;
12303 text_area_width = window_box_width (w, TEXT_AREA);
12305 /* Scroll when cursor is inside this scroll margin. */
12306 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12308 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12309 /* For left-to-right rows, hscroll when cursor is either
12310 (i) inside the right hscroll margin, or (ii) if it is
12311 inside the left margin and the window is already
12312 hscrolled. */
12313 && ((!row_r2l_p
12314 && ((w->hscroll
12315 && w->cursor.x <= h_margin)
12316 || (cursor_row->enabled_p
12317 && cursor_row->truncated_on_right_p
12318 && (w->cursor.x >= text_area_width - h_margin))))
12319 /* For right-to-left rows, the logic is similar,
12320 except that rules for scrolling to left and right
12321 are reversed. E.g., if cursor.x <= h_margin, we
12322 need to hscroll "to the right" unconditionally,
12323 and that will scroll the screen to the left so as
12324 to reveal the next portion of the row. */
12325 || (row_r2l_p
12326 && ((cursor_row->enabled_p
12327 /* FIXME: It is confusing to set the
12328 truncated_on_right_p flag when R2L rows
12329 are actually truncated on the left. */
12330 && cursor_row->truncated_on_right_p
12331 && w->cursor.x <= h_margin)
12332 || (w->hscroll
12333 && (w->cursor.x >= text_area_width - h_margin))))))
12335 struct it it;
12336 ptrdiff_t hscroll;
12337 struct buffer *saved_current_buffer;
12338 ptrdiff_t pt;
12339 int wanted_x;
12341 /* Find point in a display of infinite width. */
12342 saved_current_buffer = current_buffer;
12343 current_buffer = XBUFFER (w->contents);
12345 if (w == XWINDOW (selected_window))
12346 pt = PT;
12347 else
12348 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12350 /* Move iterator to pt starting at cursor_row->start in
12351 a line with infinite width. */
12352 init_to_row_start (&it, w, cursor_row);
12353 it.last_visible_x = INFINITY;
12354 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12355 current_buffer = saved_current_buffer;
12357 /* Position cursor in window. */
12358 if (!hscroll_relative_p && hscroll_step_abs == 0)
12359 hscroll = max (0, (it.current_x
12360 - (ITERATOR_AT_END_OF_LINE_P (&it)
12361 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12362 : (text_area_width / 2))))
12363 / FRAME_COLUMN_WIDTH (it.f);
12364 else if ((!row_r2l_p
12365 && w->cursor.x >= text_area_width - h_margin)
12366 || (row_r2l_p && w->cursor.x <= h_margin))
12368 if (hscroll_relative_p)
12369 wanted_x = text_area_width * (1 - hscroll_step_rel)
12370 - h_margin;
12371 else
12372 wanted_x = text_area_width
12373 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12374 - h_margin;
12375 hscroll
12376 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12378 else
12380 if (hscroll_relative_p)
12381 wanted_x = text_area_width * hscroll_step_rel
12382 + h_margin;
12383 else
12384 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12385 + h_margin;
12386 hscroll
12387 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12389 hscroll = max (hscroll, w->min_hscroll);
12391 /* Don't prevent redisplay optimizations if hscroll
12392 hasn't changed, as it will unnecessarily slow down
12393 redisplay. */
12394 if (w->hscroll != hscroll)
12396 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12397 w->hscroll = hscroll;
12398 hscrolled_p = 1;
12403 window = w->next;
12406 /* Value is non-zero if hscroll of any leaf window has been changed. */
12407 return hscrolled_p;
12411 /* Set hscroll so that cursor is visible and not inside horizontal
12412 scroll margins for all windows in the tree rooted at WINDOW. See
12413 also hscroll_window_tree above. Value is non-zero if any window's
12414 hscroll has been changed. If it has, desired matrices on the frame
12415 of WINDOW are cleared. */
12417 static int
12418 hscroll_windows (Lisp_Object window)
12420 int hscrolled_p = hscroll_window_tree (window);
12421 if (hscrolled_p)
12422 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12423 return hscrolled_p;
12428 /************************************************************************
12429 Redisplay
12430 ************************************************************************/
12432 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12433 to a non-zero value. This is sometimes handy to have in a debugger
12434 session. */
12436 #ifdef GLYPH_DEBUG
12438 /* First and last unchanged row for try_window_id. */
12440 static int debug_first_unchanged_at_end_vpos;
12441 static int debug_last_unchanged_at_beg_vpos;
12443 /* Delta vpos and y. */
12445 static int debug_dvpos, debug_dy;
12447 /* Delta in characters and bytes for try_window_id. */
12449 static ptrdiff_t debug_delta, debug_delta_bytes;
12451 /* Values of window_end_pos and window_end_vpos at the end of
12452 try_window_id. */
12454 static ptrdiff_t debug_end_vpos;
12456 /* Append a string to W->desired_matrix->method. FMT is a printf
12457 format string. If trace_redisplay_p is non-zero also printf the
12458 resulting string to stderr. */
12460 static void debug_method_add (struct window *, char const *, ...)
12461 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12463 static void
12464 debug_method_add (struct window *w, char const *fmt, ...)
12466 char *method = w->desired_matrix->method;
12467 int len = strlen (method);
12468 int size = sizeof w->desired_matrix->method;
12469 int remaining = size - len - 1;
12470 va_list ap;
12472 if (len && remaining)
12474 method[len] = '|';
12475 --remaining, ++len;
12478 va_start (ap, fmt);
12479 vsnprintf (method + len, remaining + 1, fmt, ap);
12480 va_end (ap);
12482 if (trace_redisplay_p)
12483 fprintf (stderr, "%p (%s): %s\n",
12485 ((BUFFERP (w->contents)
12486 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12487 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12488 : "no buffer"),
12489 method + len);
12492 #endif /* GLYPH_DEBUG */
12495 /* Value is non-zero if all changes in window W, which displays
12496 current_buffer, are in the text between START and END. START is a
12497 buffer position, END is given as a distance from Z. Used in
12498 redisplay_internal for display optimization. */
12500 static int
12501 text_outside_line_unchanged_p (struct window *w,
12502 ptrdiff_t start, ptrdiff_t end)
12504 int unchanged_p = 1;
12506 /* If text or overlays have changed, see where. */
12507 if (window_outdated (w))
12509 /* Gap in the line? */
12510 if (GPT < start || Z - GPT < end)
12511 unchanged_p = 0;
12513 /* Changes start in front of the line, or end after it? */
12514 if (unchanged_p
12515 && (BEG_UNCHANGED < start - 1
12516 || END_UNCHANGED < end))
12517 unchanged_p = 0;
12519 /* If selective display, can't optimize if changes start at the
12520 beginning of the line. */
12521 if (unchanged_p
12522 && INTEGERP (BVAR (current_buffer, selective_display))
12523 && XINT (BVAR (current_buffer, selective_display)) > 0
12524 && (BEG_UNCHANGED < start || GPT <= start))
12525 unchanged_p = 0;
12527 /* If there are overlays at the start or end of the line, these
12528 may have overlay strings with newlines in them. A change at
12529 START, for instance, may actually concern the display of such
12530 overlay strings as well, and they are displayed on different
12531 lines. So, quickly rule out this case. (For the future, it
12532 might be desirable to implement something more telling than
12533 just BEG/END_UNCHANGED.) */
12534 if (unchanged_p)
12536 if (BEG + BEG_UNCHANGED == start
12537 && overlay_touches_p (start))
12538 unchanged_p = 0;
12539 if (END_UNCHANGED == end
12540 && overlay_touches_p (Z - end))
12541 unchanged_p = 0;
12544 /* Under bidi reordering, adding or deleting a character in the
12545 beginning of a paragraph, before the first strong directional
12546 character, can change the base direction of the paragraph (unless
12547 the buffer specifies a fixed paragraph direction), which will
12548 require to redisplay the whole paragraph. It might be worthwhile
12549 to find the paragraph limits and widen the range of redisplayed
12550 lines to that, but for now just give up this optimization. */
12551 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12552 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12553 unchanged_p = 0;
12556 return unchanged_p;
12560 /* Do a frame update, taking possible shortcuts into account. This is
12561 the main external entry point for redisplay.
12563 If the last redisplay displayed an echo area message and that message
12564 is no longer requested, we clear the echo area or bring back the
12565 mini-buffer if that is in use. */
12567 void
12568 redisplay (void)
12570 redisplay_internal ();
12574 static Lisp_Object
12575 overlay_arrow_string_or_property (Lisp_Object var)
12577 Lisp_Object val;
12579 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12580 return val;
12582 return Voverlay_arrow_string;
12585 /* Return 1 if there are any overlay-arrows in current_buffer. */
12586 static int
12587 overlay_arrow_in_current_buffer_p (void)
12589 Lisp_Object vlist;
12591 for (vlist = Voverlay_arrow_variable_list;
12592 CONSP (vlist);
12593 vlist = XCDR (vlist))
12595 Lisp_Object var = XCAR (vlist);
12596 Lisp_Object val;
12598 if (!SYMBOLP (var))
12599 continue;
12600 val = find_symbol_value (var);
12601 if (MARKERP (val)
12602 && current_buffer == XMARKER (val)->buffer)
12603 return 1;
12605 return 0;
12609 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12610 has changed. */
12612 static int
12613 overlay_arrows_changed_p (void)
12615 Lisp_Object vlist;
12617 for (vlist = Voverlay_arrow_variable_list;
12618 CONSP (vlist);
12619 vlist = XCDR (vlist))
12621 Lisp_Object var = XCAR (vlist);
12622 Lisp_Object val, pstr;
12624 if (!SYMBOLP (var))
12625 continue;
12626 val = find_symbol_value (var);
12627 if (!MARKERP (val))
12628 continue;
12629 if (! EQ (COERCE_MARKER (val),
12630 Fget (var, Qlast_arrow_position))
12631 || ! (pstr = overlay_arrow_string_or_property (var),
12632 EQ (pstr, Fget (var, Qlast_arrow_string))))
12633 return 1;
12635 return 0;
12638 /* Mark overlay arrows to be updated on next redisplay. */
12640 static void
12641 update_overlay_arrows (int up_to_date)
12643 Lisp_Object vlist;
12645 for (vlist = Voverlay_arrow_variable_list;
12646 CONSP (vlist);
12647 vlist = XCDR (vlist))
12649 Lisp_Object var = XCAR (vlist);
12651 if (!SYMBOLP (var))
12652 continue;
12654 if (up_to_date > 0)
12656 Lisp_Object val = find_symbol_value (var);
12657 Fput (var, Qlast_arrow_position,
12658 COERCE_MARKER (val));
12659 Fput (var, Qlast_arrow_string,
12660 overlay_arrow_string_or_property (var));
12662 else if (up_to_date < 0
12663 || !NILP (Fget (var, Qlast_arrow_position)))
12665 Fput (var, Qlast_arrow_position, Qt);
12666 Fput (var, Qlast_arrow_string, Qt);
12672 /* Return overlay arrow string to display at row.
12673 Return integer (bitmap number) for arrow bitmap in left fringe.
12674 Return nil if no overlay arrow. */
12676 static Lisp_Object
12677 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12679 Lisp_Object vlist;
12681 for (vlist = Voverlay_arrow_variable_list;
12682 CONSP (vlist);
12683 vlist = XCDR (vlist))
12685 Lisp_Object var = XCAR (vlist);
12686 Lisp_Object val;
12688 if (!SYMBOLP (var))
12689 continue;
12691 val = find_symbol_value (var);
12693 if (MARKERP (val)
12694 && current_buffer == XMARKER (val)->buffer
12695 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12697 if (FRAME_WINDOW_P (it->f)
12698 /* FIXME: if ROW->reversed_p is set, this should test
12699 the right fringe, not the left one. */
12700 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12702 #ifdef HAVE_WINDOW_SYSTEM
12703 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12705 int fringe_bitmap;
12706 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12707 return make_number (fringe_bitmap);
12709 #endif
12710 return make_number (-1); /* Use default arrow bitmap. */
12712 return overlay_arrow_string_or_property (var);
12716 return Qnil;
12719 /* Return 1 if point moved out of or into a composition. Otherwise
12720 return 0. PREV_BUF and PREV_PT are the last point buffer and
12721 position. BUF and PT are the current point buffer and position. */
12723 static int
12724 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12725 struct buffer *buf, ptrdiff_t pt)
12727 ptrdiff_t start, end;
12728 Lisp_Object prop;
12729 Lisp_Object buffer;
12731 XSETBUFFER (buffer, buf);
12732 /* Check a composition at the last point if point moved within the
12733 same buffer. */
12734 if (prev_buf == buf)
12736 if (prev_pt == pt)
12737 /* Point didn't move. */
12738 return 0;
12740 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12741 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12742 && COMPOSITION_VALID_P (start, end, prop)
12743 && start < prev_pt && end > prev_pt)
12744 /* The last point was within the composition. Return 1 iff
12745 point moved out of the composition. */
12746 return (pt <= start || pt >= end);
12749 /* Check a composition at the current point. */
12750 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12751 && find_composition (pt, -1, &start, &end, &prop, buffer)
12752 && COMPOSITION_VALID_P (start, end, prop)
12753 && start < pt && end > pt);
12757 /* Reconsider the setting of B->clip_changed which is displayed
12758 in window W. */
12760 static void
12761 reconsider_clip_changes (struct window *w, struct buffer *b)
12763 if (b->clip_changed
12764 && w->window_end_valid
12765 && w->current_matrix->buffer == b
12766 && w->current_matrix->zv == BUF_ZV (b)
12767 && w->current_matrix->begv == BUF_BEGV (b))
12768 b->clip_changed = 0;
12770 /* If display wasn't paused, and W is not a tool bar window, see if
12771 point has been moved into or out of a composition. In that case,
12772 we set b->clip_changed to 1 to force updating the screen. If
12773 b->clip_changed has already been set to 1, we can skip this
12774 check. */
12775 if (!b->clip_changed && BUFFERP (w->contents) && w->window_end_valid)
12777 ptrdiff_t pt;
12779 if (w == XWINDOW (selected_window))
12780 pt = PT;
12781 else
12782 pt = marker_position (w->pointm);
12784 if ((w->current_matrix->buffer != XBUFFER (w->contents)
12785 || pt != w->last_point)
12786 && check_point_in_composition (w->current_matrix->buffer,
12787 w->last_point,
12788 XBUFFER (w->contents), pt))
12789 b->clip_changed = 1;
12794 #define STOP_POLLING \
12795 do { if (! polling_stopped_here) stop_polling (); \
12796 polling_stopped_here = 1; } while (0)
12798 #define RESUME_POLLING \
12799 do { if (polling_stopped_here) start_polling (); \
12800 polling_stopped_here = 0; } while (0)
12803 /* Perhaps in the future avoid recentering windows if it
12804 is not necessary; currently that causes some problems. */
12806 static void
12807 redisplay_internal (void)
12809 struct window *w = XWINDOW (selected_window);
12810 struct window *sw;
12811 struct frame *fr;
12812 int pending;
12813 int must_finish = 0;
12814 struct text_pos tlbufpos, tlendpos;
12815 int number_of_visible_frames;
12816 ptrdiff_t count, count1;
12817 struct frame *sf;
12818 int polling_stopped_here = 0;
12819 Lisp_Object tail, frame;
12820 struct backtrace backtrace;
12822 /* Non-zero means redisplay has to consider all windows on all
12823 frames. Zero means, only selected_window is considered. */
12824 int consider_all_windows_p;
12826 /* Non-zero means redisplay has to redisplay the miniwindow. */
12827 int update_miniwindow_p = 0;
12829 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12831 /* No redisplay if running in batch mode or frame is not yet fully
12832 initialized, or redisplay is explicitly turned off by setting
12833 Vinhibit_redisplay. */
12834 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12835 || !NILP (Vinhibit_redisplay))
12836 return;
12838 /* Don't examine these until after testing Vinhibit_redisplay.
12839 When Emacs is shutting down, perhaps because its connection to
12840 X has dropped, we should not look at them at all. */
12841 fr = XFRAME (w->frame);
12842 sf = SELECTED_FRAME ();
12844 if (!fr->glyphs_initialized_p)
12845 return;
12847 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12848 if (popup_activated ())
12849 return;
12850 #endif
12852 /* I don't think this happens but let's be paranoid. */
12853 if (redisplaying_p)
12854 return;
12856 /* Record a function that clears redisplaying_p
12857 when we leave this function. */
12858 count = SPECPDL_INDEX ();
12859 record_unwind_protect (unwind_redisplay, selected_frame);
12860 redisplaying_p = 1;
12861 specbind (Qinhibit_free_realized_faces, Qnil);
12863 /* Record this function, so it appears on the profiler's backtraces. */
12864 backtrace.next = backtrace_list;
12865 backtrace.function = Qredisplay_internal;
12866 backtrace.args = &Qnil;
12867 backtrace.nargs = 0;
12868 backtrace.debug_on_exit = 0;
12869 backtrace_list = &backtrace;
12871 FOR_EACH_FRAME (tail, frame)
12872 XFRAME (frame)->already_hscrolled_p = 0;
12874 retry:
12875 /* Remember the currently selected window. */
12876 sw = w;
12878 pending = 0;
12879 reconsider_clip_changes (w, current_buffer);
12880 last_escape_glyph_frame = NULL;
12881 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12882 last_glyphless_glyph_frame = NULL;
12883 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12885 /* If new fonts have been loaded that make a glyph matrix adjustment
12886 necessary, do it. */
12887 if (fonts_changed_p)
12889 adjust_glyphs (NULL);
12890 ++windows_or_buffers_changed;
12891 fonts_changed_p = 0;
12894 /* If face_change_count is non-zero, init_iterator will free all
12895 realized faces, which includes the faces referenced from current
12896 matrices. So, we can't reuse current matrices in this case. */
12897 if (face_change_count)
12898 ++windows_or_buffers_changed;
12900 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12901 && FRAME_TTY (sf)->previous_frame != sf)
12903 /* Since frames on a single ASCII terminal share the same
12904 display area, displaying a different frame means redisplay
12905 the whole thing. */
12906 windows_or_buffers_changed++;
12907 SET_FRAME_GARBAGED (sf);
12908 #ifndef DOS_NT
12909 set_tty_color_mode (FRAME_TTY (sf), sf);
12910 #endif
12911 FRAME_TTY (sf)->previous_frame = sf;
12914 /* Set the visible flags for all frames. Do this before checking for
12915 resized or garbaged frames; they want to know if their frames are
12916 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12917 number_of_visible_frames = 0;
12919 FOR_EACH_FRAME (tail, frame)
12921 struct frame *f = XFRAME (frame);
12923 if (FRAME_VISIBLE_P (f))
12924 ++number_of_visible_frames;
12925 clear_desired_matrices (f);
12928 /* Notice any pending interrupt request to change frame size. */
12929 do_pending_window_change (1);
12931 /* do_pending_window_change could change the selected_window due to
12932 frame resizing which makes the selected window too small. */
12933 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12935 sw = w;
12936 reconsider_clip_changes (w, current_buffer);
12939 /* Clear frames marked as garbaged. */
12940 clear_garbaged_frames ();
12942 /* Build menubar and tool-bar items. */
12943 if (NILP (Vmemory_full))
12944 prepare_menu_bars ();
12946 if (windows_or_buffers_changed)
12947 update_mode_lines++;
12949 /* Detect case that we need to write or remove a star in the mode line. */
12950 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
12952 w->update_mode_line = 1;
12953 if (buffer_shared_and_changed ())
12954 update_mode_lines++;
12957 /* Avoid invocation of point motion hooks by `current_column' below. */
12958 count1 = SPECPDL_INDEX ();
12959 specbind (Qinhibit_point_motion_hooks, Qt);
12961 if (mode_line_update_needed (w))
12962 w->update_mode_line = 1;
12964 unbind_to (count1, Qnil);
12966 consider_all_windows_p = (update_mode_lines
12967 || buffer_shared_and_changed ()
12968 || cursor_type_changed);
12970 /* If specs for an arrow have changed, do thorough redisplay
12971 to ensure we remove any arrow that should no longer exist. */
12972 if (overlay_arrows_changed_p ())
12973 consider_all_windows_p = windows_or_buffers_changed = 1;
12975 /* Normally the message* functions will have already displayed and
12976 updated the echo area, but the frame may have been trashed, or
12977 the update may have been preempted, so display the echo area
12978 again here. Checking message_cleared_p captures the case that
12979 the echo area should be cleared. */
12980 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12981 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12982 || (message_cleared_p
12983 && minibuf_level == 0
12984 /* If the mini-window is currently selected, this means the
12985 echo-area doesn't show through. */
12986 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12988 int window_height_changed_p = echo_area_display (0);
12990 if (message_cleared_p)
12991 update_miniwindow_p = 1;
12993 must_finish = 1;
12995 /* If we don't display the current message, don't clear the
12996 message_cleared_p flag, because, if we did, we wouldn't clear
12997 the echo area in the next redisplay which doesn't preserve
12998 the echo area. */
12999 if (!display_last_displayed_message_p)
13000 message_cleared_p = 0;
13002 if (fonts_changed_p)
13003 goto retry;
13004 else if (window_height_changed_p)
13006 consider_all_windows_p = 1;
13007 ++update_mode_lines;
13008 ++windows_or_buffers_changed;
13010 /* If window configuration was changed, frames may have been
13011 marked garbaged. Clear them or we will experience
13012 surprises wrt scrolling. */
13013 clear_garbaged_frames ();
13016 else if (EQ (selected_window, minibuf_window)
13017 && (current_buffer->clip_changed || window_outdated (w))
13018 && resize_mini_window (w, 0))
13020 /* Resized active mini-window to fit the size of what it is
13021 showing if its contents might have changed. */
13022 must_finish = 1;
13023 /* FIXME: this causes all frames to be updated, which seems unnecessary
13024 since only the current frame needs to be considered. This function
13025 needs to be rewritten with two variables, consider_all_windows and
13026 consider_all_frames. */
13027 consider_all_windows_p = 1;
13028 ++windows_or_buffers_changed;
13029 ++update_mode_lines;
13031 /* If window configuration was changed, frames may have been
13032 marked garbaged. Clear them or we will experience
13033 surprises wrt scrolling. */
13034 clear_garbaged_frames ();
13037 /* If showing the region, and mark has changed, we must redisplay
13038 the whole window. The assignment to this_line_start_pos prevents
13039 the optimization directly below this if-statement. */
13040 if (((!NILP (Vtransient_mark_mode)
13041 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13042 != (w->region_showing > 0))
13043 || (w->region_showing
13044 && w->region_showing
13045 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13046 CHARPOS (this_line_start_pos) = 0;
13048 /* Optimize the case that only the line containing the cursor in the
13049 selected window has changed. Variables starting with this_ are
13050 set in display_line and record information about the line
13051 containing the cursor. */
13052 tlbufpos = this_line_start_pos;
13053 tlendpos = this_line_end_pos;
13054 if (!consider_all_windows_p
13055 && CHARPOS (tlbufpos) > 0
13056 && !w->update_mode_line
13057 && !current_buffer->clip_changed
13058 && !current_buffer->prevent_redisplay_optimizations_p
13059 && FRAME_VISIBLE_P (XFRAME (w->frame))
13060 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13061 /* Make sure recorded data applies to current buffer, etc. */
13062 && this_line_buffer == current_buffer
13063 && current_buffer == XBUFFER (w->contents)
13064 && !w->force_start
13065 && !w->optional_new_start
13066 /* Point must be on the line that we have info recorded about. */
13067 && PT >= CHARPOS (tlbufpos)
13068 && PT <= Z - CHARPOS (tlendpos)
13069 /* All text outside that line, including its final newline,
13070 must be unchanged. */
13071 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13072 CHARPOS (tlendpos)))
13074 if (CHARPOS (tlbufpos) > BEGV
13075 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13076 && (CHARPOS (tlbufpos) == ZV
13077 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13078 /* Former continuation line has disappeared by becoming empty. */
13079 goto cancel;
13080 else if (window_outdated (w) || MINI_WINDOW_P (w))
13082 /* We have to handle the case of continuation around a
13083 wide-column character (see the comment in indent.c around
13084 line 1340).
13086 For instance, in the following case:
13088 -------- Insert --------
13089 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13090 J_I_ ==> J_I_ `^^' are cursors.
13091 ^^ ^^
13092 -------- --------
13094 As we have to redraw the line above, we cannot use this
13095 optimization. */
13097 struct it it;
13098 int line_height_before = this_line_pixel_height;
13100 /* Note that start_display will handle the case that the
13101 line starting at tlbufpos is a continuation line. */
13102 start_display (&it, w, tlbufpos);
13104 /* Implementation note: It this still necessary? */
13105 if (it.current_x != this_line_start_x)
13106 goto cancel;
13108 TRACE ((stderr, "trying display optimization 1\n"));
13109 w->cursor.vpos = -1;
13110 overlay_arrow_seen = 0;
13111 it.vpos = this_line_vpos;
13112 it.current_y = this_line_y;
13113 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13114 display_line (&it);
13116 /* If line contains point, is not continued,
13117 and ends at same distance from eob as before, we win. */
13118 if (w->cursor.vpos >= 0
13119 /* Line is not continued, otherwise this_line_start_pos
13120 would have been set to 0 in display_line. */
13121 && CHARPOS (this_line_start_pos)
13122 /* Line ends as before. */
13123 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13124 /* Line has same height as before. Otherwise other lines
13125 would have to be shifted up or down. */
13126 && this_line_pixel_height == line_height_before)
13128 /* If this is not the window's last line, we must adjust
13129 the charstarts of the lines below. */
13130 if (it.current_y < it.last_visible_y)
13132 struct glyph_row *row
13133 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13134 ptrdiff_t delta, delta_bytes;
13136 /* We used to distinguish between two cases here,
13137 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13138 when the line ends in a newline or the end of the
13139 buffer's accessible portion. But both cases did
13140 the same, so they were collapsed. */
13141 delta = (Z
13142 - CHARPOS (tlendpos)
13143 - MATRIX_ROW_START_CHARPOS (row));
13144 delta_bytes = (Z_BYTE
13145 - BYTEPOS (tlendpos)
13146 - MATRIX_ROW_START_BYTEPOS (row));
13148 increment_matrix_positions (w->current_matrix,
13149 this_line_vpos + 1,
13150 w->current_matrix->nrows,
13151 delta, delta_bytes);
13154 /* If this row displays text now but previously didn't,
13155 or vice versa, w->window_end_vpos may have to be
13156 adjusted. */
13157 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13159 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13160 wset_window_end_vpos (w, make_number (this_line_vpos));
13162 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13163 && this_line_vpos > 0)
13164 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13165 w->window_end_valid = 0;
13167 /* Update hint: No need to try to scroll in update_window. */
13168 w->desired_matrix->no_scrolling_p = 1;
13170 #ifdef GLYPH_DEBUG
13171 *w->desired_matrix->method = 0;
13172 debug_method_add (w, "optimization 1");
13173 #endif
13174 #ifdef HAVE_WINDOW_SYSTEM
13175 update_window_fringes (w, 0);
13176 #endif
13177 goto update;
13179 else
13180 goto cancel;
13182 else if (/* Cursor position hasn't changed. */
13183 PT == w->last_point
13184 /* Make sure the cursor was last displayed
13185 in this window. Otherwise we have to reposition it. */
13186 && 0 <= w->cursor.vpos
13187 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13189 if (!must_finish)
13191 do_pending_window_change (1);
13192 /* If selected_window changed, redisplay again. */
13193 if (WINDOWP (selected_window)
13194 && (w = XWINDOW (selected_window)) != sw)
13195 goto retry;
13197 /* We used to always goto end_of_redisplay here, but this
13198 isn't enough if we have a blinking cursor. */
13199 if (w->cursor_off_p == w->last_cursor_off_p)
13200 goto end_of_redisplay;
13202 goto update;
13204 /* If highlighting the region, or if the cursor is in the echo area,
13205 then we can't just move the cursor. */
13206 else if (! (!NILP (Vtransient_mark_mode)
13207 && !NILP (BVAR (current_buffer, mark_active)))
13208 && (EQ (selected_window,
13209 BVAR (current_buffer, last_selected_window))
13210 || highlight_nonselected_windows)
13211 && !w->region_showing
13212 && NILP (Vshow_trailing_whitespace)
13213 && !cursor_in_echo_area)
13215 struct it it;
13216 struct glyph_row *row;
13218 /* Skip from tlbufpos to PT and see where it is. Note that
13219 PT may be in invisible text. If so, we will end at the
13220 next visible position. */
13221 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13222 NULL, DEFAULT_FACE_ID);
13223 it.current_x = this_line_start_x;
13224 it.current_y = this_line_y;
13225 it.vpos = this_line_vpos;
13227 /* The call to move_it_to stops in front of PT, but
13228 moves over before-strings. */
13229 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13231 if (it.vpos == this_line_vpos
13232 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13233 row->enabled_p))
13235 eassert (this_line_vpos == it.vpos);
13236 eassert (this_line_y == it.current_y);
13237 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13238 #ifdef GLYPH_DEBUG
13239 *w->desired_matrix->method = 0;
13240 debug_method_add (w, "optimization 3");
13241 #endif
13242 goto update;
13244 else
13245 goto cancel;
13248 cancel:
13249 /* Text changed drastically or point moved off of line. */
13250 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13253 CHARPOS (this_line_start_pos) = 0;
13254 consider_all_windows_p |= buffer_shared_and_changed ();
13255 ++clear_face_cache_count;
13256 #ifdef HAVE_WINDOW_SYSTEM
13257 ++clear_image_cache_count;
13258 #endif
13260 /* Build desired matrices, and update the display. If
13261 consider_all_windows_p is non-zero, do it for all windows on all
13262 frames. Otherwise do it for selected_window, only. */
13264 if (consider_all_windows_p)
13266 FOR_EACH_FRAME (tail, frame)
13267 XFRAME (frame)->updated_p = 0;
13269 FOR_EACH_FRAME (tail, frame)
13271 struct frame *f = XFRAME (frame);
13273 /* We don't have to do anything for unselected terminal
13274 frames. */
13275 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13276 && !EQ (FRAME_TTY (f)->top_frame, frame))
13277 continue;
13279 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13281 /* Mark all the scroll bars to be removed; we'll redeem
13282 the ones we want when we redisplay their windows. */
13283 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13284 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13286 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13287 redisplay_windows (FRAME_ROOT_WINDOW (f));
13289 /* The X error handler may have deleted that frame. */
13290 if (!FRAME_LIVE_P (f))
13291 continue;
13293 /* Any scroll bars which redisplay_windows should have
13294 nuked should now go away. */
13295 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13296 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13298 /* If fonts changed, display again. */
13299 /* ??? rms: I suspect it is a mistake to jump all the way
13300 back to retry here. It should just retry this frame. */
13301 if (fonts_changed_p)
13302 goto retry;
13304 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13306 /* See if we have to hscroll. */
13307 if (!f->already_hscrolled_p)
13309 f->already_hscrolled_p = 1;
13310 if (hscroll_windows (f->root_window))
13311 goto retry;
13314 /* Prevent various kinds of signals during display
13315 update. stdio is not robust about handling
13316 signals, which can cause an apparent I/O
13317 error. */
13318 if (interrupt_input)
13319 unrequest_sigio ();
13320 STOP_POLLING;
13322 /* Update the display. */
13323 set_window_update_flags (XWINDOW (f->root_window), 1);
13324 pending |= update_frame (f, 0, 0);
13325 f->updated_p = 1;
13330 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13332 if (!pending)
13334 /* Do the mark_window_display_accurate after all windows have
13335 been redisplayed because this call resets flags in buffers
13336 which are needed for proper redisplay. */
13337 FOR_EACH_FRAME (tail, frame)
13339 struct frame *f = XFRAME (frame);
13340 if (f->updated_p)
13342 mark_window_display_accurate (f->root_window, 1);
13343 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13344 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13349 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13351 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13352 struct frame *mini_frame;
13354 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13355 /* Use list_of_error, not Qerror, so that
13356 we catch only errors and don't run the debugger. */
13357 internal_condition_case_1 (redisplay_window_1, selected_window,
13358 list_of_error,
13359 redisplay_window_error);
13360 if (update_miniwindow_p)
13361 internal_condition_case_1 (redisplay_window_1, mini_window,
13362 list_of_error,
13363 redisplay_window_error);
13365 /* Compare desired and current matrices, perform output. */
13367 update:
13368 /* If fonts changed, display again. */
13369 if (fonts_changed_p)
13370 goto retry;
13372 /* Prevent various kinds of signals during display update.
13373 stdio is not robust about handling signals,
13374 which can cause an apparent I/O error. */
13375 if (interrupt_input)
13376 unrequest_sigio ();
13377 STOP_POLLING;
13379 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13381 if (hscroll_windows (selected_window))
13382 goto retry;
13384 XWINDOW (selected_window)->must_be_updated_p = 1;
13385 pending = update_frame (sf, 0, 0);
13388 /* We may have called echo_area_display at the top of this
13389 function. If the echo area is on another frame, that may
13390 have put text on a frame other than the selected one, so the
13391 above call to update_frame would not have caught it. Catch
13392 it here. */
13393 mini_window = FRAME_MINIBUF_WINDOW (sf);
13394 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13396 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13398 XWINDOW (mini_window)->must_be_updated_p = 1;
13399 pending |= update_frame (mini_frame, 0, 0);
13400 if (!pending && hscroll_windows (mini_window))
13401 goto retry;
13405 /* If display was paused because of pending input, make sure we do a
13406 thorough update the next time. */
13407 if (pending)
13409 /* Prevent the optimization at the beginning of
13410 redisplay_internal that tries a single-line update of the
13411 line containing the cursor in the selected window. */
13412 CHARPOS (this_line_start_pos) = 0;
13414 /* Let the overlay arrow be updated the next time. */
13415 update_overlay_arrows (0);
13417 /* If we pause after scrolling, some rows in the current
13418 matrices of some windows are not valid. */
13419 if (!WINDOW_FULL_WIDTH_P (w)
13420 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13421 update_mode_lines = 1;
13423 else
13425 if (!consider_all_windows_p)
13427 /* This has already been done above if
13428 consider_all_windows_p is set. */
13429 mark_window_display_accurate_1 (w, 1);
13431 /* Say overlay arrows are up to date. */
13432 update_overlay_arrows (1);
13434 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13435 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13438 update_mode_lines = 0;
13439 windows_or_buffers_changed = 0;
13440 cursor_type_changed = 0;
13443 /* Start SIGIO interrupts coming again. Having them off during the
13444 code above makes it less likely one will discard output, but not
13445 impossible, since there might be stuff in the system buffer here.
13446 But it is much hairier to try to do anything about that. */
13447 if (interrupt_input)
13448 request_sigio ();
13449 RESUME_POLLING;
13451 /* If a frame has become visible which was not before, redisplay
13452 again, so that we display it. Expose events for such a frame
13453 (which it gets when becoming visible) don't call the parts of
13454 redisplay constructing glyphs, so simply exposing a frame won't
13455 display anything in this case. So, we have to display these
13456 frames here explicitly. */
13457 if (!pending)
13459 int new_count = 0;
13461 FOR_EACH_FRAME (tail, frame)
13463 int this_is_visible = 0;
13465 if (XFRAME (frame)->visible)
13466 this_is_visible = 1;
13468 if (this_is_visible)
13469 new_count++;
13472 if (new_count != number_of_visible_frames)
13473 windows_or_buffers_changed++;
13476 /* Change frame size now if a change is pending. */
13477 do_pending_window_change (1);
13479 /* If we just did a pending size change, or have additional
13480 visible frames, or selected_window changed, redisplay again. */
13481 if ((windows_or_buffers_changed && !pending)
13482 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13483 goto retry;
13485 /* Clear the face and image caches.
13487 We used to do this only if consider_all_windows_p. But the cache
13488 needs to be cleared if a timer creates images in the current
13489 buffer (e.g. the test case in Bug#6230). */
13491 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13493 clear_face_cache (0);
13494 clear_face_cache_count = 0;
13497 #ifdef HAVE_WINDOW_SYSTEM
13498 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13500 clear_image_caches (Qnil);
13501 clear_image_cache_count = 0;
13503 #endif /* HAVE_WINDOW_SYSTEM */
13505 end_of_redisplay:
13506 backtrace_list = backtrace.next;
13507 unbind_to (count, Qnil);
13508 RESUME_POLLING;
13512 /* Redisplay, but leave alone any recent echo area message unless
13513 another message has been requested in its place.
13515 This is useful in situations where you need to redisplay but no
13516 user action has occurred, making it inappropriate for the message
13517 area to be cleared. See tracking_off and
13518 wait_reading_process_output for examples of these situations.
13520 FROM_WHERE is an integer saying from where this function was
13521 called. This is useful for debugging. */
13523 void
13524 redisplay_preserve_echo_area (int from_where)
13526 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13528 if (!NILP (echo_area_buffer[1]))
13530 /* We have a previously displayed message, but no current
13531 message. Redisplay the previous message. */
13532 display_last_displayed_message_p = 1;
13533 redisplay_internal ();
13534 display_last_displayed_message_p = 0;
13536 else
13537 redisplay_internal ();
13539 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13540 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13541 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13545 /* Function registered with record_unwind_protect in redisplay_internal.
13546 Clear redisplaying_p. Also select the previously selected frame. */
13548 static Lisp_Object
13549 unwind_redisplay (Lisp_Object old_frame)
13551 redisplaying_p = 0;
13552 return Qnil;
13556 /* Mark the display of leaf window W as accurate or inaccurate.
13557 If ACCURATE_P is non-zero mark display of W as accurate. If
13558 ACCURATE_P is zero, arrange for W to be redisplayed the next
13559 time redisplay_internal is called. */
13561 static void
13562 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13564 struct buffer *b = XBUFFER (w->contents);
13566 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13567 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13568 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13570 if (accurate_p)
13572 b->clip_changed = 0;
13573 b->prevent_redisplay_optimizations_p = 0;
13575 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13576 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13577 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13578 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13580 w->current_matrix->buffer = b;
13581 w->current_matrix->begv = BUF_BEGV (b);
13582 w->current_matrix->zv = BUF_ZV (b);
13584 w->last_cursor = w->cursor;
13585 w->last_cursor_off_p = w->cursor_off_p;
13587 if (w == XWINDOW (selected_window))
13588 w->last_point = BUF_PT (b);
13589 else
13590 w->last_point = marker_position (w->pointm);
13592 w->window_end_valid = 1;
13593 w->update_mode_line = 0;
13598 /* Mark the display of windows in the window tree rooted at WINDOW as
13599 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13600 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13601 be redisplayed the next time redisplay_internal is called. */
13603 void
13604 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13606 struct window *w;
13608 for (; !NILP (window); window = w->next)
13610 w = XWINDOW (window);
13611 if (WINDOWP (w->contents))
13612 mark_window_display_accurate (w->contents, accurate_p);
13613 else
13614 mark_window_display_accurate_1 (w, accurate_p);
13617 if (accurate_p)
13618 update_overlay_arrows (1);
13619 else
13620 /* Force a thorough redisplay the next time by setting
13621 last_arrow_position and last_arrow_string to t, which is
13622 unequal to any useful value of Voverlay_arrow_... */
13623 update_overlay_arrows (-1);
13627 /* Return value in display table DP (Lisp_Char_Table *) for character
13628 C. Since a display table doesn't have any parent, we don't have to
13629 follow parent. Do not call this function directly but use the
13630 macro DISP_CHAR_VECTOR. */
13632 Lisp_Object
13633 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13635 Lisp_Object val;
13637 if (ASCII_CHAR_P (c))
13639 val = dp->ascii;
13640 if (SUB_CHAR_TABLE_P (val))
13641 val = XSUB_CHAR_TABLE (val)->contents[c];
13643 else
13645 Lisp_Object table;
13647 XSETCHAR_TABLE (table, dp);
13648 val = char_table_ref (table, c);
13650 if (NILP (val))
13651 val = dp->defalt;
13652 return val;
13657 /***********************************************************************
13658 Window Redisplay
13659 ***********************************************************************/
13661 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13663 static void
13664 redisplay_windows (Lisp_Object window)
13666 while (!NILP (window))
13668 struct window *w = XWINDOW (window);
13670 if (WINDOWP (w->contents))
13671 redisplay_windows (w->contents);
13672 else if (BUFFERP (w->contents))
13674 displayed_buffer = XBUFFER (w->contents);
13675 /* Use list_of_error, not Qerror, so that
13676 we catch only errors and don't run the debugger. */
13677 internal_condition_case_1 (redisplay_window_0, window,
13678 list_of_error,
13679 redisplay_window_error);
13682 window = w->next;
13686 static Lisp_Object
13687 redisplay_window_error (Lisp_Object ignore)
13689 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13690 return Qnil;
13693 static Lisp_Object
13694 redisplay_window_0 (Lisp_Object window)
13696 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13697 redisplay_window (window, 0);
13698 return Qnil;
13701 static Lisp_Object
13702 redisplay_window_1 (Lisp_Object window)
13704 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13705 redisplay_window (window, 1);
13706 return Qnil;
13710 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13711 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13712 which positions recorded in ROW differ from current buffer
13713 positions.
13715 Return 0 if cursor is not on this row, 1 otherwise. */
13717 static int
13718 set_cursor_from_row (struct window *w, struct glyph_row *row,
13719 struct glyph_matrix *matrix,
13720 ptrdiff_t delta, ptrdiff_t delta_bytes,
13721 int dy, int dvpos)
13723 struct glyph *glyph = row->glyphs[TEXT_AREA];
13724 struct glyph *end = glyph + row->used[TEXT_AREA];
13725 struct glyph *cursor = NULL;
13726 /* The last known character position in row. */
13727 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13728 int x = row->x;
13729 ptrdiff_t pt_old = PT - delta;
13730 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13731 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13732 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13733 /* A glyph beyond the edge of TEXT_AREA which we should never
13734 touch. */
13735 struct glyph *glyphs_end = end;
13736 /* Non-zero means we've found a match for cursor position, but that
13737 glyph has the avoid_cursor_p flag set. */
13738 int match_with_avoid_cursor = 0;
13739 /* Non-zero means we've seen at least one glyph that came from a
13740 display string. */
13741 int string_seen = 0;
13742 /* Largest and smallest buffer positions seen so far during scan of
13743 glyph row. */
13744 ptrdiff_t bpos_max = pos_before;
13745 ptrdiff_t bpos_min = pos_after;
13746 /* Last buffer position covered by an overlay string with an integer
13747 `cursor' property. */
13748 ptrdiff_t bpos_covered = 0;
13749 /* Non-zero means the display string on which to display the cursor
13750 comes from a text property, not from an overlay. */
13751 int string_from_text_prop = 0;
13753 /* Don't even try doing anything if called for a mode-line or
13754 header-line row, since the rest of the code isn't prepared to
13755 deal with such calamities. */
13756 eassert (!row->mode_line_p);
13757 if (row->mode_line_p)
13758 return 0;
13760 /* Skip over glyphs not having an object at the start and the end of
13761 the row. These are special glyphs like truncation marks on
13762 terminal frames. */
13763 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13765 if (!row->reversed_p)
13767 while (glyph < end
13768 && INTEGERP (glyph->object)
13769 && glyph->charpos < 0)
13771 x += glyph->pixel_width;
13772 ++glyph;
13774 while (end > glyph
13775 && INTEGERP ((end - 1)->object)
13776 /* CHARPOS is zero for blanks and stretch glyphs
13777 inserted by extend_face_to_end_of_line. */
13778 && (end - 1)->charpos <= 0)
13779 --end;
13780 glyph_before = glyph - 1;
13781 glyph_after = end;
13783 else
13785 struct glyph *g;
13787 /* If the glyph row is reversed, we need to process it from back
13788 to front, so swap the edge pointers. */
13789 glyphs_end = end = glyph - 1;
13790 glyph += row->used[TEXT_AREA] - 1;
13792 while (glyph > end + 1
13793 && INTEGERP (glyph->object)
13794 && glyph->charpos < 0)
13796 --glyph;
13797 x -= glyph->pixel_width;
13799 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13800 --glyph;
13801 /* By default, in reversed rows we put the cursor on the
13802 rightmost (first in the reading order) glyph. */
13803 for (g = end + 1; g < glyph; g++)
13804 x += g->pixel_width;
13805 while (end < glyph
13806 && INTEGERP ((end + 1)->object)
13807 && (end + 1)->charpos <= 0)
13808 ++end;
13809 glyph_before = glyph + 1;
13810 glyph_after = end;
13813 else if (row->reversed_p)
13815 /* In R2L rows that don't display text, put the cursor on the
13816 rightmost glyph. Case in point: an empty last line that is
13817 part of an R2L paragraph. */
13818 cursor = end - 1;
13819 /* Avoid placing the cursor on the last glyph of the row, where
13820 on terminal frames we hold the vertical border between
13821 adjacent windows. */
13822 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13823 && !WINDOW_RIGHTMOST_P (w)
13824 && cursor == row->glyphs[LAST_AREA] - 1)
13825 cursor--;
13826 x = -1; /* will be computed below, at label compute_x */
13829 /* Step 1: Try to find the glyph whose character position
13830 corresponds to point. If that's not possible, find 2 glyphs
13831 whose character positions are the closest to point, one before
13832 point, the other after it. */
13833 if (!row->reversed_p)
13834 while (/* not marched to end of glyph row */
13835 glyph < end
13836 /* glyph was not inserted by redisplay for internal purposes */
13837 && !INTEGERP (glyph->object))
13839 if (BUFFERP (glyph->object))
13841 ptrdiff_t dpos = glyph->charpos - pt_old;
13843 if (glyph->charpos > bpos_max)
13844 bpos_max = glyph->charpos;
13845 if (glyph->charpos < bpos_min)
13846 bpos_min = glyph->charpos;
13847 if (!glyph->avoid_cursor_p)
13849 /* If we hit point, we've found the glyph on which to
13850 display the cursor. */
13851 if (dpos == 0)
13853 match_with_avoid_cursor = 0;
13854 break;
13856 /* See if we've found a better approximation to
13857 POS_BEFORE or to POS_AFTER. */
13858 if (0 > dpos && dpos > pos_before - pt_old)
13860 pos_before = glyph->charpos;
13861 glyph_before = glyph;
13863 else if (0 < dpos && dpos < pos_after - pt_old)
13865 pos_after = glyph->charpos;
13866 glyph_after = glyph;
13869 else if (dpos == 0)
13870 match_with_avoid_cursor = 1;
13872 else if (STRINGP (glyph->object))
13874 Lisp_Object chprop;
13875 ptrdiff_t glyph_pos = glyph->charpos;
13877 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13878 glyph->object);
13879 if (!NILP (chprop))
13881 /* If the string came from a `display' text property,
13882 look up the buffer position of that property and
13883 use that position to update bpos_max, as if we
13884 actually saw such a position in one of the row's
13885 glyphs. This helps with supporting integer values
13886 of `cursor' property on the display string in
13887 situations where most or all of the row's buffer
13888 text is completely covered by display properties,
13889 so that no glyph with valid buffer positions is
13890 ever seen in the row. */
13891 ptrdiff_t prop_pos =
13892 string_buffer_position_lim (glyph->object, pos_before,
13893 pos_after, 0);
13895 if (prop_pos >= pos_before)
13896 bpos_max = prop_pos - 1;
13898 if (INTEGERP (chprop))
13900 bpos_covered = bpos_max + XINT (chprop);
13901 /* If the `cursor' property covers buffer positions up
13902 to and including point, we should display cursor on
13903 this glyph. Note that, if a `cursor' property on one
13904 of the string's characters has an integer value, we
13905 will break out of the loop below _before_ we get to
13906 the position match above. IOW, integer values of
13907 the `cursor' property override the "exact match for
13908 point" strategy of positioning the cursor. */
13909 /* Implementation note: bpos_max == pt_old when, e.g.,
13910 we are in an empty line, where bpos_max is set to
13911 MATRIX_ROW_START_CHARPOS, see above. */
13912 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13914 cursor = glyph;
13915 break;
13919 string_seen = 1;
13921 x += glyph->pixel_width;
13922 ++glyph;
13924 else if (glyph > end) /* row is reversed */
13925 while (!INTEGERP (glyph->object))
13927 if (BUFFERP (glyph->object))
13929 ptrdiff_t dpos = glyph->charpos - pt_old;
13931 if (glyph->charpos > bpos_max)
13932 bpos_max = glyph->charpos;
13933 if (glyph->charpos < bpos_min)
13934 bpos_min = glyph->charpos;
13935 if (!glyph->avoid_cursor_p)
13937 if (dpos == 0)
13939 match_with_avoid_cursor = 0;
13940 break;
13942 if (0 > dpos && dpos > pos_before - pt_old)
13944 pos_before = glyph->charpos;
13945 glyph_before = glyph;
13947 else if (0 < dpos && dpos < pos_after - pt_old)
13949 pos_after = glyph->charpos;
13950 glyph_after = glyph;
13953 else if (dpos == 0)
13954 match_with_avoid_cursor = 1;
13956 else if (STRINGP (glyph->object))
13958 Lisp_Object chprop;
13959 ptrdiff_t glyph_pos = glyph->charpos;
13961 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13962 glyph->object);
13963 if (!NILP (chprop))
13965 ptrdiff_t prop_pos =
13966 string_buffer_position_lim (glyph->object, pos_before,
13967 pos_after, 0);
13969 if (prop_pos >= pos_before)
13970 bpos_max = prop_pos - 1;
13972 if (INTEGERP (chprop))
13974 bpos_covered = bpos_max + XINT (chprop);
13975 /* If the `cursor' property covers buffer positions up
13976 to and including point, we should display cursor on
13977 this glyph. */
13978 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13980 cursor = glyph;
13981 break;
13984 string_seen = 1;
13986 --glyph;
13987 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13989 x--; /* can't use any pixel_width */
13990 break;
13992 x -= glyph->pixel_width;
13995 /* Step 2: If we didn't find an exact match for point, we need to
13996 look for a proper place to put the cursor among glyphs between
13997 GLYPH_BEFORE and GLYPH_AFTER. */
13998 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13999 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14000 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14002 /* An empty line has a single glyph whose OBJECT is zero and
14003 whose CHARPOS is the position of a newline on that line.
14004 Note that on a TTY, there are more glyphs after that, which
14005 were produced by extend_face_to_end_of_line, but their
14006 CHARPOS is zero or negative. */
14007 int empty_line_p =
14008 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14009 && INTEGERP (glyph->object) && glyph->charpos > 0
14010 /* On a TTY, continued and truncated rows also have a glyph at
14011 their end whose OBJECT is zero and whose CHARPOS is
14012 positive (the continuation and truncation glyphs), but such
14013 rows are obviously not "empty". */
14014 && !(row->continued_p || row->truncated_on_right_p);
14016 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14018 ptrdiff_t ellipsis_pos;
14020 /* Scan back over the ellipsis glyphs. */
14021 if (!row->reversed_p)
14023 ellipsis_pos = (glyph - 1)->charpos;
14024 while (glyph > row->glyphs[TEXT_AREA]
14025 && (glyph - 1)->charpos == ellipsis_pos)
14026 glyph--, x -= glyph->pixel_width;
14027 /* That loop always goes one position too far, including
14028 the glyph before the ellipsis. So scan forward over
14029 that one. */
14030 x += glyph->pixel_width;
14031 glyph++;
14033 else /* row is reversed */
14035 ellipsis_pos = (glyph + 1)->charpos;
14036 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14037 && (glyph + 1)->charpos == ellipsis_pos)
14038 glyph++, x += glyph->pixel_width;
14039 x -= glyph->pixel_width;
14040 glyph--;
14043 else if (match_with_avoid_cursor)
14045 cursor = glyph_after;
14046 x = -1;
14048 else if (string_seen)
14050 int incr = row->reversed_p ? -1 : +1;
14052 /* Need to find the glyph that came out of a string which is
14053 present at point. That glyph is somewhere between
14054 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14055 positioned between POS_BEFORE and POS_AFTER in the
14056 buffer. */
14057 struct glyph *start, *stop;
14058 ptrdiff_t pos = pos_before;
14060 x = -1;
14062 /* If the row ends in a newline from a display string,
14063 reordering could have moved the glyphs belonging to the
14064 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14065 in this case we extend the search to the last glyph in
14066 the row that was not inserted by redisplay. */
14067 if (row->ends_in_newline_from_string_p)
14069 glyph_after = end;
14070 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14073 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14074 correspond to POS_BEFORE and POS_AFTER, respectively. We
14075 need START and STOP in the order that corresponds to the
14076 row's direction as given by its reversed_p flag. If the
14077 directionality of characters between POS_BEFORE and
14078 POS_AFTER is the opposite of the row's base direction,
14079 these characters will have been reordered for display,
14080 and we need to reverse START and STOP. */
14081 if (!row->reversed_p)
14083 start = min (glyph_before, glyph_after);
14084 stop = max (glyph_before, glyph_after);
14086 else
14088 start = max (glyph_before, glyph_after);
14089 stop = min (glyph_before, glyph_after);
14091 for (glyph = start + incr;
14092 row->reversed_p ? glyph > stop : glyph < stop; )
14095 /* Any glyphs that come from the buffer are here because
14096 of bidi reordering. Skip them, and only pay
14097 attention to glyphs that came from some string. */
14098 if (STRINGP (glyph->object))
14100 Lisp_Object str;
14101 ptrdiff_t tem;
14102 /* If the display property covers the newline, we
14103 need to search for it one position farther. */
14104 ptrdiff_t lim = pos_after
14105 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14107 string_from_text_prop = 0;
14108 str = glyph->object;
14109 tem = string_buffer_position_lim (str, pos, lim, 0);
14110 if (tem == 0 /* from overlay */
14111 || pos <= tem)
14113 /* If the string from which this glyph came is
14114 found in the buffer at point, or at position
14115 that is closer to point than pos_after, then
14116 we've found the glyph we've been looking for.
14117 If it comes from an overlay (tem == 0), and
14118 it has the `cursor' property on one of its
14119 glyphs, record that glyph as a candidate for
14120 displaying the cursor. (As in the
14121 unidirectional version, we will display the
14122 cursor on the last candidate we find.) */
14123 if (tem == 0
14124 || tem == pt_old
14125 || (tem - pt_old > 0 && tem < pos_after))
14127 /* The glyphs from this string could have
14128 been reordered. Find the one with the
14129 smallest string position. Or there could
14130 be a character in the string with the
14131 `cursor' property, which means display
14132 cursor on that character's glyph. */
14133 ptrdiff_t strpos = glyph->charpos;
14135 if (tem)
14137 cursor = glyph;
14138 string_from_text_prop = 1;
14140 for ( ;
14141 (row->reversed_p ? glyph > stop : glyph < stop)
14142 && EQ (glyph->object, str);
14143 glyph += incr)
14145 Lisp_Object cprop;
14146 ptrdiff_t gpos = glyph->charpos;
14148 cprop = Fget_char_property (make_number (gpos),
14149 Qcursor,
14150 glyph->object);
14151 if (!NILP (cprop))
14153 cursor = glyph;
14154 break;
14156 if (tem && glyph->charpos < strpos)
14158 strpos = glyph->charpos;
14159 cursor = glyph;
14163 if (tem == pt_old
14164 || (tem - pt_old > 0 && tem < pos_after))
14165 goto compute_x;
14167 if (tem)
14168 pos = tem + 1; /* don't find previous instances */
14170 /* This string is not what we want; skip all of the
14171 glyphs that came from it. */
14172 while ((row->reversed_p ? glyph > stop : glyph < stop)
14173 && EQ (glyph->object, str))
14174 glyph += incr;
14176 else
14177 glyph += incr;
14180 /* If we reached the end of the line, and END was from a string,
14181 the cursor is not on this line. */
14182 if (cursor == NULL
14183 && (row->reversed_p ? glyph <= end : glyph >= end)
14184 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14185 && STRINGP (end->object)
14186 && row->continued_p)
14187 return 0;
14189 /* A truncated row may not include PT among its character positions.
14190 Setting the cursor inside the scroll margin will trigger
14191 recalculation of hscroll in hscroll_window_tree. But if a
14192 display string covers point, defer to the string-handling
14193 code below to figure this out. */
14194 else if (row->truncated_on_left_p && pt_old < bpos_min)
14196 cursor = glyph_before;
14197 x = -1;
14199 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14200 /* Zero-width characters produce no glyphs. */
14201 || (!empty_line_p
14202 && (row->reversed_p
14203 ? glyph_after > glyphs_end
14204 : glyph_after < glyphs_end)))
14206 cursor = glyph_after;
14207 x = -1;
14211 compute_x:
14212 if (cursor != NULL)
14213 glyph = cursor;
14214 else if (glyph == glyphs_end
14215 && pos_before == pos_after
14216 && STRINGP ((row->reversed_p
14217 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14218 : row->glyphs[TEXT_AREA])->object))
14220 /* If all the glyphs of this row came from strings, put the
14221 cursor on the first glyph of the row. This avoids having the
14222 cursor outside of the text area in this very rare and hard
14223 use case. */
14224 glyph =
14225 row->reversed_p
14226 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14227 : row->glyphs[TEXT_AREA];
14229 if (x < 0)
14231 struct glyph *g;
14233 /* Need to compute x that corresponds to GLYPH. */
14234 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14236 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14237 emacs_abort ();
14238 x += g->pixel_width;
14242 /* ROW could be part of a continued line, which, under bidi
14243 reordering, might have other rows whose start and end charpos
14244 occlude point. Only set w->cursor if we found a better
14245 approximation to the cursor position than we have from previously
14246 examined candidate rows belonging to the same continued line. */
14247 if (/* we already have a candidate row */
14248 w->cursor.vpos >= 0
14249 /* that candidate is not the row we are processing */
14250 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14251 /* Make sure cursor.vpos specifies a row whose start and end
14252 charpos occlude point, and it is valid candidate for being a
14253 cursor-row. This is because some callers of this function
14254 leave cursor.vpos at the row where the cursor was displayed
14255 during the last redisplay cycle. */
14256 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14257 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14258 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14260 struct glyph *g1 =
14261 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14263 /* Don't consider glyphs that are outside TEXT_AREA. */
14264 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14265 return 0;
14266 /* Keep the candidate whose buffer position is the closest to
14267 point or has the `cursor' property. */
14268 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14269 w->cursor.hpos >= 0
14270 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14271 && ((BUFFERP (g1->object)
14272 && (g1->charpos == pt_old /* an exact match always wins */
14273 || (BUFFERP (glyph->object)
14274 && eabs (g1->charpos - pt_old)
14275 < eabs (glyph->charpos - pt_old))))
14276 /* previous candidate is a glyph from a string that has
14277 a non-nil `cursor' property */
14278 || (STRINGP (g1->object)
14279 && (!NILP (Fget_char_property (make_number (g1->charpos),
14280 Qcursor, g1->object))
14281 /* previous candidate is from the same display
14282 string as this one, and the display string
14283 came from a text property */
14284 || (EQ (g1->object, glyph->object)
14285 && string_from_text_prop)
14286 /* this candidate is from newline and its
14287 position is not an exact match */
14288 || (INTEGERP (glyph->object)
14289 && glyph->charpos != pt_old)))))
14290 return 0;
14291 /* If this candidate gives an exact match, use that. */
14292 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14293 /* If this candidate is a glyph created for the
14294 terminating newline of a line, and point is on that
14295 newline, it wins because it's an exact match. */
14296 || (!row->continued_p
14297 && INTEGERP (glyph->object)
14298 && glyph->charpos == 0
14299 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14300 /* Otherwise, keep the candidate that comes from a row
14301 spanning less buffer positions. This may win when one or
14302 both candidate positions are on glyphs that came from
14303 display strings, for which we cannot compare buffer
14304 positions. */
14305 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14306 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14307 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14308 return 0;
14310 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14311 w->cursor.x = x;
14312 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14313 w->cursor.y = row->y + dy;
14315 if (w == XWINDOW (selected_window))
14317 if (!row->continued_p
14318 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14319 && row->x == 0)
14321 this_line_buffer = XBUFFER (w->contents);
14323 CHARPOS (this_line_start_pos)
14324 = MATRIX_ROW_START_CHARPOS (row) + delta;
14325 BYTEPOS (this_line_start_pos)
14326 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14328 CHARPOS (this_line_end_pos)
14329 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14330 BYTEPOS (this_line_end_pos)
14331 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14333 this_line_y = w->cursor.y;
14334 this_line_pixel_height = row->height;
14335 this_line_vpos = w->cursor.vpos;
14336 this_line_start_x = row->x;
14338 else
14339 CHARPOS (this_line_start_pos) = 0;
14342 return 1;
14346 /* Run window scroll functions, if any, for WINDOW with new window
14347 start STARTP. Sets the window start of WINDOW to that position.
14349 We assume that the window's buffer is really current. */
14351 static struct text_pos
14352 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14354 struct window *w = XWINDOW (window);
14355 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14357 if (current_buffer != XBUFFER (w->contents))
14358 emacs_abort ();
14360 if (!NILP (Vwindow_scroll_functions))
14362 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14363 make_number (CHARPOS (startp)));
14364 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14365 /* In case the hook functions switch buffers. */
14366 set_buffer_internal (XBUFFER (w->contents));
14369 return startp;
14373 /* Make sure the line containing the cursor is fully visible.
14374 A value of 1 means there is nothing to be done.
14375 (Either the line is fully visible, or it cannot be made so,
14376 or we cannot tell.)
14378 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14379 is higher than window.
14381 A value of 0 means the caller should do scrolling
14382 as if point had gone off the screen. */
14384 static int
14385 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14387 struct glyph_matrix *matrix;
14388 struct glyph_row *row;
14389 int window_height;
14391 if (!make_cursor_line_fully_visible_p)
14392 return 1;
14394 /* It's not always possible to find the cursor, e.g, when a window
14395 is full of overlay strings. Don't do anything in that case. */
14396 if (w->cursor.vpos < 0)
14397 return 1;
14399 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14400 row = MATRIX_ROW (matrix, w->cursor.vpos);
14402 /* If the cursor row is not partially visible, there's nothing to do. */
14403 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14404 return 1;
14406 /* If the row the cursor is in is taller than the window's height,
14407 it's not clear what to do, so do nothing. */
14408 window_height = window_box_height (w);
14409 if (row->height >= window_height)
14411 if (!force_p || MINI_WINDOW_P (w)
14412 || w->vscroll || w->cursor.vpos == 0)
14413 return 1;
14415 return 0;
14419 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14420 non-zero means only WINDOW is redisplayed in redisplay_internal.
14421 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14422 in redisplay_window to bring a partially visible line into view in
14423 the case that only the cursor has moved.
14425 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14426 last screen line's vertical height extends past the end of the screen.
14428 Value is
14430 1 if scrolling succeeded
14432 0 if scrolling didn't find point.
14434 -1 if new fonts have been loaded so that we must interrupt
14435 redisplay, adjust glyph matrices, and try again. */
14437 enum
14439 SCROLLING_SUCCESS,
14440 SCROLLING_FAILED,
14441 SCROLLING_NEED_LARGER_MATRICES
14444 /* If scroll-conservatively is more than this, never recenter.
14446 If you change this, don't forget to update the doc string of
14447 `scroll-conservatively' and the Emacs manual. */
14448 #define SCROLL_LIMIT 100
14450 static int
14451 try_scrolling (Lisp_Object window, int just_this_one_p,
14452 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14453 int temp_scroll_step, int last_line_misfit)
14455 struct window *w = XWINDOW (window);
14456 struct frame *f = XFRAME (w->frame);
14457 struct text_pos pos, startp;
14458 struct it it;
14459 int this_scroll_margin, scroll_max, rc, height;
14460 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14461 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14462 Lisp_Object aggressive;
14463 /* We will never try scrolling more than this number of lines. */
14464 int scroll_limit = SCROLL_LIMIT;
14466 #ifdef GLYPH_DEBUG
14467 debug_method_add (w, "try_scrolling");
14468 #endif
14470 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14472 /* Compute scroll margin height in pixels. We scroll when point is
14473 within this distance from the top or bottom of the window. */
14474 if (scroll_margin > 0)
14475 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14476 * FRAME_LINE_HEIGHT (f);
14477 else
14478 this_scroll_margin = 0;
14480 /* Force arg_scroll_conservatively to have a reasonable value, to
14481 avoid scrolling too far away with slow move_it_* functions. Note
14482 that the user can supply scroll-conservatively equal to
14483 `most-positive-fixnum', which can be larger than INT_MAX. */
14484 if (arg_scroll_conservatively > scroll_limit)
14486 arg_scroll_conservatively = scroll_limit + 1;
14487 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14489 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14490 /* Compute how much we should try to scroll maximally to bring
14491 point into view. */
14492 scroll_max = (max (scroll_step,
14493 max (arg_scroll_conservatively, temp_scroll_step))
14494 * FRAME_LINE_HEIGHT (f));
14495 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14496 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14497 /* We're trying to scroll because of aggressive scrolling but no
14498 scroll_step is set. Choose an arbitrary one. */
14499 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14500 else
14501 scroll_max = 0;
14503 too_near_end:
14505 /* Decide whether to scroll down. */
14506 if (PT > CHARPOS (startp))
14508 int scroll_margin_y;
14510 /* Compute the pixel ypos of the scroll margin, then move IT to
14511 either that ypos or PT, whichever comes first. */
14512 start_display (&it, w, startp);
14513 scroll_margin_y = it.last_visible_y - this_scroll_margin
14514 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14515 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14516 (MOVE_TO_POS | MOVE_TO_Y));
14518 if (PT > CHARPOS (it.current.pos))
14520 int y0 = line_bottom_y (&it);
14521 /* Compute how many pixels below window bottom to stop searching
14522 for PT. This avoids costly search for PT that is far away if
14523 the user limited scrolling by a small number of lines, but
14524 always finds PT if scroll_conservatively is set to a large
14525 number, such as most-positive-fixnum. */
14526 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14527 int y_to_move = it.last_visible_y + slack;
14529 /* Compute the distance from the scroll margin to PT or to
14530 the scroll limit, whichever comes first. This should
14531 include the height of the cursor line, to make that line
14532 fully visible. */
14533 move_it_to (&it, PT, -1, y_to_move,
14534 -1, MOVE_TO_POS | MOVE_TO_Y);
14535 dy = line_bottom_y (&it) - y0;
14537 if (dy > scroll_max)
14538 return SCROLLING_FAILED;
14540 if (dy > 0)
14541 scroll_down_p = 1;
14545 if (scroll_down_p)
14547 /* Point is in or below the bottom scroll margin, so move the
14548 window start down. If scrolling conservatively, move it just
14549 enough down to make point visible. If scroll_step is set,
14550 move it down by scroll_step. */
14551 if (arg_scroll_conservatively)
14552 amount_to_scroll
14553 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14554 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14555 else if (scroll_step || temp_scroll_step)
14556 amount_to_scroll = scroll_max;
14557 else
14559 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14560 height = WINDOW_BOX_TEXT_HEIGHT (w);
14561 if (NUMBERP (aggressive))
14563 double float_amount = XFLOATINT (aggressive) * height;
14564 int aggressive_scroll = float_amount;
14565 if (aggressive_scroll == 0 && float_amount > 0)
14566 aggressive_scroll = 1;
14567 /* Don't let point enter the scroll margin near top of
14568 the window. This could happen if the value of
14569 scroll_up_aggressively is too large and there are
14570 non-zero margins, because scroll_up_aggressively
14571 means put point that fraction of window height
14572 _from_the_bottom_margin_. */
14573 if (aggressive_scroll + 2*this_scroll_margin > height)
14574 aggressive_scroll = height - 2*this_scroll_margin;
14575 amount_to_scroll = dy + aggressive_scroll;
14579 if (amount_to_scroll <= 0)
14580 return SCROLLING_FAILED;
14582 start_display (&it, w, startp);
14583 if (arg_scroll_conservatively <= scroll_limit)
14584 move_it_vertically (&it, amount_to_scroll);
14585 else
14587 /* Extra precision for users who set scroll-conservatively
14588 to a large number: make sure the amount we scroll
14589 the window start is never less than amount_to_scroll,
14590 which was computed as distance from window bottom to
14591 point. This matters when lines at window top and lines
14592 below window bottom have different height. */
14593 struct it it1;
14594 void *it1data = NULL;
14595 /* We use a temporary it1 because line_bottom_y can modify
14596 its argument, if it moves one line down; see there. */
14597 int start_y;
14599 SAVE_IT (it1, it, it1data);
14600 start_y = line_bottom_y (&it1);
14601 do {
14602 RESTORE_IT (&it, &it, it1data);
14603 move_it_by_lines (&it, 1);
14604 SAVE_IT (it1, it, it1data);
14605 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14608 /* If STARTP is unchanged, move it down another screen line. */
14609 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14610 move_it_by_lines (&it, 1);
14611 startp = it.current.pos;
14613 else
14615 struct text_pos scroll_margin_pos = startp;
14616 int y_offset = 0;
14618 /* See if point is inside the scroll margin at the top of the
14619 window. */
14620 if (this_scroll_margin)
14622 int y_start;
14624 start_display (&it, w, startp);
14625 y_start = it.current_y;
14626 move_it_vertically (&it, this_scroll_margin);
14627 scroll_margin_pos = it.current.pos;
14628 /* If we didn't move enough before hitting ZV, request
14629 additional amount of scroll, to move point out of the
14630 scroll margin. */
14631 if (IT_CHARPOS (it) == ZV
14632 && it.current_y - y_start < this_scroll_margin)
14633 y_offset = this_scroll_margin - (it.current_y - y_start);
14636 if (PT < CHARPOS (scroll_margin_pos))
14638 /* Point is in the scroll margin at the top of the window or
14639 above what is displayed in the window. */
14640 int y0, y_to_move;
14642 /* Compute the vertical distance from PT to the scroll
14643 margin position. Move as far as scroll_max allows, or
14644 one screenful, or 10 screen lines, whichever is largest.
14645 Give up if distance is greater than scroll_max or if we
14646 didn't reach the scroll margin position. */
14647 SET_TEXT_POS (pos, PT, PT_BYTE);
14648 start_display (&it, w, pos);
14649 y0 = it.current_y;
14650 y_to_move = max (it.last_visible_y,
14651 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14652 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14653 y_to_move, -1,
14654 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14655 dy = it.current_y - y0;
14656 if (dy > scroll_max
14657 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14658 return SCROLLING_FAILED;
14660 /* Additional scroll for when ZV was too close to point. */
14661 dy += y_offset;
14663 /* Compute new window start. */
14664 start_display (&it, w, startp);
14666 if (arg_scroll_conservatively)
14667 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14668 max (scroll_step, temp_scroll_step));
14669 else if (scroll_step || temp_scroll_step)
14670 amount_to_scroll = scroll_max;
14671 else
14673 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14674 height = WINDOW_BOX_TEXT_HEIGHT (w);
14675 if (NUMBERP (aggressive))
14677 double float_amount = XFLOATINT (aggressive) * height;
14678 int aggressive_scroll = float_amount;
14679 if (aggressive_scroll == 0 && float_amount > 0)
14680 aggressive_scroll = 1;
14681 /* Don't let point enter the scroll margin near
14682 bottom of the window, if the value of
14683 scroll_down_aggressively happens to be too
14684 large. */
14685 if (aggressive_scroll + 2*this_scroll_margin > height)
14686 aggressive_scroll = height - 2*this_scroll_margin;
14687 amount_to_scroll = dy + aggressive_scroll;
14691 if (amount_to_scroll <= 0)
14692 return SCROLLING_FAILED;
14694 move_it_vertically_backward (&it, amount_to_scroll);
14695 startp = it.current.pos;
14699 /* Run window scroll functions. */
14700 startp = run_window_scroll_functions (window, startp);
14702 /* Display the window. Give up if new fonts are loaded, or if point
14703 doesn't appear. */
14704 if (!try_window (window, startp, 0))
14705 rc = SCROLLING_NEED_LARGER_MATRICES;
14706 else if (w->cursor.vpos < 0)
14708 clear_glyph_matrix (w->desired_matrix);
14709 rc = SCROLLING_FAILED;
14711 else
14713 /* Maybe forget recorded base line for line number display. */
14714 if (!just_this_one_p
14715 || current_buffer->clip_changed
14716 || BEG_UNCHANGED < CHARPOS (startp))
14717 w->base_line_number = 0;
14719 /* If cursor ends up on a partially visible line,
14720 treat that as being off the bottom of the screen. */
14721 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14722 /* It's possible that the cursor is on the first line of the
14723 buffer, which is partially obscured due to a vscroll
14724 (Bug#7537). In that case, avoid looping forever . */
14725 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14727 clear_glyph_matrix (w->desired_matrix);
14728 ++extra_scroll_margin_lines;
14729 goto too_near_end;
14731 rc = SCROLLING_SUCCESS;
14734 return rc;
14738 /* Compute a suitable window start for window W if display of W starts
14739 on a continuation line. Value is non-zero if a new window start
14740 was computed.
14742 The new window start will be computed, based on W's width, starting
14743 from the start of the continued line. It is the start of the
14744 screen line with the minimum distance from the old start W->start. */
14746 static int
14747 compute_window_start_on_continuation_line (struct window *w)
14749 struct text_pos pos, start_pos;
14750 int window_start_changed_p = 0;
14752 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14754 /* If window start is on a continuation line... Window start may be
14755 < BEGV in case there's invisible text at the start of the
14756 buffer (M-x rmail, for example). */
14757 if (CHARPOS (start_pos) > BEGV
14758 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14760 struct it it;
14761 struct glyph_row *row;
14763 /* Handle the case that the window start is out of range. */
14764 if (CHARPOS (start_pos) < BEGV)
14765 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14766 else if (CHARPOS (start_pos) > ZV)
14767 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14769 /* Find the start of the continued line. This should be fast
14770 because find_newline is fast (newline cache). */
14771 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14772 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14773 row, DEFAULT_FACE_ID);
14774 reseat_at_previous_visible_line_start (&it);
14776 /* If the line start is "too far" away from the window start,
14777 say it takes too much time to compute a new window start. */
14778 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14779 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14781 int min_distance, distance;
14783 /* Move forward by display lines to find the new window
14784 start. If window width was enlarged, the new start can
14785 be expected to be > the old start. If window width was
14786 decreased, the new window start will be < the old start.
14787 So, we're looking for the display line start with the
14788 minimum distance from the old window start. */
14789 pos = it.current.pos;
14790 min_distance = INFINITY;
14791 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14792 distance < min_distance)
14794 min_distance = distance;
14795 pos = it.current.pos;
14796 move_it_by_lines (&it, 1);
14799 /* Set the window start there. */
14800 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14801 window_start_changed_p = 1;
14805 return window_start_changed_p;
14809 /* Try cursor movement in case text has not changed in window WINDOW,
14810 with window start STARTP. Value is
14812 CURSOR_MOVEMENT_SUCCESS if successful
14814 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14816 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14817 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14818 we want to scroll as if scroll-step were set to 1. See the code.
14820 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14821 which case we have to abort this redisplay, and adjust matrices
14822 first. */
14824 enum
14826 CURSOR_MOVEMENT_SUCCESS,
14827 CURSOR_MOVEMENT_CANNOT_BE_USED,
14828 CURSOR_MOVEMENT_MUST_SCROLL,
14829 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14832 static int
14833 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14835 struct window *w = XWINDOW (window);
14836 struct frame *f = XFRAME (w->frame);
14837 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14839 #ifdef GLYPH_DEBUG
14840 if (inhibit_try_cursor_movement)
14841 return rc;
14842 #endif
14844 /* Previously, there was a check for Lisp integer in the
14845 if-statement below. Now, this field is converted to
14846 ptrdiff_t, thus zero means invalid position in a buffer. */
14847 eassert (w->last_point > 0);
14849 /* Handle case where text has not changed, only point, and it has
14850 not moved off the frame. */
14851 if (/* Point may be in this window. */
14852 PT >= CHARPOS (startp)
14853 /* Selective display hasn't changed. */
14854 && !current_buffer->clip_changed
14855 /* Function force-mode-line-update is used to force a thorough
14856 redisplay. It sets either windows_or_buffers_changed or
14857 update_mode_lines. So don't take a shortcut here for these
14858 cases. */
14859 && !update_mode_lines
14860 && !windows_or_buffers_changed
14861 && !cursor_type_changed
14862 /* Can't use this case if highlighting a region. When a
14863 region exists, cursor movement has to do more than just
14864 set the cursor. */
14865 && markpos_of_region () < 0
14866 && !w->region_showing
14867 && NILP (Vshow_trailing_whitespace)
14868 /* This code is not used for mini-buffer for the sake of the case
14869 of redisplaying to replace an echo area message; since in
14870 that case the mini-buffer contents per se are usually
14871 unchanged. This code is of no real use in the mini-buffer
14872 since the handling of this_line_start_pos, etc., in redisplay
14873 handles the same cases. */
14874 && !EQ (window, minibuf_window)
14875 /* When splitting windows or for new windows, it happens that
14876 redisplay is called with a nil window_end_vpos or one being
14877 larger than the window. This should really be fixed in
14878 window.c. I don't have this on my list, now, so we do
14879 approximately the same as the old redisplay code. --gerd. */
14880 && INTEGERP (w->window_end_vpos)
14881 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14882 && (FRAME_WINDOW_P (f)
14883 || !overlay_arrow_in_current_buffer_p ()))
14885 int this_scroll_margin, top_scroll_margin;
14886 struct glyph_row *row = NULL;
14888 #ifdef GLYPH_DEBUG
14889 debug_method_add (w, "cursor movement");
14890 #endif
14892 /* Scroll if point within this distance from the top or bottom
14893 of the window. This is a pixel value. */
14894 if (scroll_margin > 0)
14896 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14897 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14899 else
14900 this_scroll_margin = 0;
14902 top_scroll_margin = this_scroll_margin;
14903 if (WINDOW_WANTS_HEADER_LINE_P (w))
14904 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14906 /* Start with the row the cursor was displayed during the last
14907 not paused redisplay. Give up if that row is not valid. */
14908 if (w->last_cursor.vpos < 0
14909 || w->last_cursor.vpos >= w->current_matrix->nrows)
14910 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14911 else
14913 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14914 if (row->mode_line_p)
14915 ++row;
14916 if (!row->enabled_p)
14917 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14920 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14922 int scroll_p = 0, must_scroll = 0;
14923 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14925 if (PT > w->last_point)
14927 /* Point has moved forward. */
14928 while (MATRIX_ROW_END_CHARPOS (row) < PT
14929 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14931 eassert (row->enabled_p);
14932 ++row;
14935 /* If the end position of a row equals the start
14936 position of the next row, and PT is at that position,
14937 we would rather display cursor in the next line. */
14938 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14939 && MATRIX_ROW_END_CHARPOS (row) == PT
14940 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
14941 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14942 && !cursor_row_p (row))
14943 ++row;
14945 /* If within the scroll margin, scroll. Note that
14946 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14947 the next line would be drawn, and that
14948 this_scroll_margin can be zero. */
14949 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14950 || PT > MATRIX_ROW_END_CHARPOS (row)
14951 /* Line is completely visible last line in window
14952 and PT is to be set in the next line. */
14953 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14954 && PT == MATRIX_ROW_END_CHARPOS (row)
14955 && !row->ends_at_zv_p
14956 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14957 scroll_p = 1;
14959 else if (PT < w->last_point)
14961 /* Cursor has to be moved backward. Note that PT >=
14962 CHARPOS (startp) because of the outer if-statement. */
14963 while (!row->mode_line_p
14964 && (MATRIX_ROW_START_CHARPOS (row) > PT
14965 || (MATRIX_ROW_START_CHARPOS (row) == PT
14966 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14967 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14968 row > w->current_matrix->rows
14969 && (row-1)->ends_in_newline_from_string_p))))
14970 && (row->y > top_scroll_margin
14971 || CHARPOS (startp) == BEGV))
14973 eassert (row->enabled_p);
14974 --row;
14977 /* Consider the following case: Window starts at BEGV,
14978 there is invisible, intangible text at BEGV, so that
14979 display starts at some point START > BEGV. It can
14980 happen that we are called with PT somewhere between
14981 BEGV and START. Try to handle that case. */
14982 if (row < w->current_matrix->rows
14983 || row->mode_line_p)
14985 row = w->current_matrix->rows;
14986 if (row->mode_line_p)
14987 ++row;
14990 /* Due to newlines in overlay strings, we may have to
14991 skip forward over overlay strings. */
14992 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14993 && MATRIX_ROW_END_CHARPOS (row) == PT
14994 && !cursor_row_p (row))
14995 ++row;
14997 /* If within the scroll margin, scroll. */
14998 if (row->y < top_scroll_margin
14999 && CHARPOS (startp) != BEGV)
15000 scroll_p = 1;
15002 else
15004 /* Cursor did not move. So don't scroll even if cursor line
15005 is partially visible, as it was so before. */
15006 rc = CURSOR_MOVEMENT_SUCCESS;
15009 if (PT < MATRIX_ROW_START_CHARPOS (row)
15010 || PT > MATRIX_ROW_END_CHARPOS (row))
15012 /* if PT is not in the glyph row, give up. */
15013 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15014 must_scroll = 1;
15016 else if (rc != CURSOR_MOVEMENT_SUCCESS
15017 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15019 struct glyph_row *row1;
15021 /* If rows are bidi-reordered and point moved, back up
15022 until we find a row that does not belong to a
15023 continuation line. This is because we must consider
15024 all rows of a continued line as candidates for the
15025 new cursor positioning, since row start and end
15026 positions change non-linearly with vertical position
15027 in such rows. */
15028 /* FIXME: Revisit this when glyph ``spilling'' in
15029 continuation lines' rows is implemented for
15030 bidi-reordered rows. */
15031 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15032 MATRIX_ROW_CONTINUATION_LINE_P (row);
15033 --row)
15035 /* If we hit the beginning of the displayed portion
15036 without finding the first row of a continued
15037 line, give up. */
15038 if (row <= row1)
15040 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15041 break;
15043 eassert (row->enabled_p);
15046 if (must_scroll)
15048 else if (rc != CURSOR_MOVEMENT_SUCCESS
15049 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15050 /* Make sure this isn't a header line by any chance, since
15051 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15052 && !row->mode_line_p
15053 && make_cursor_line_fully_visible_p)
15055 if (PT == MATRIX_ROW_END_CHARPOS (row)
15056 && !row->ends_at_zv_p
15057 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15058 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15059 else if (row->height > window_box_height (w))
15061 /* If we end up in a partially visible line, let's
15062 make it fully visible, except when it's taller
15063 than the window, in which case we can't do much
15064 about it. */
15065 *scroll_step = 1;
15066 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15068 else
15070 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15071 if (!cursor_row_fully_visible_p (w, 0, 1))
15072 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15073 else
15074 rc = CURSOR_MOVEMENT_SUCCESS;
15077 else if (scroll_p)
15078 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15079 else if (rc != CURSOR_MOVEMENT_SUCCESS
15080 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15082 /* With bidi-reordered rows, there could be more than
15083 one candidate row whose start and end positions
15084 occlude point. We need to let set_cursor_from_row
15085 find the best candidate. */
15086 /* FIXME: Revisit this when glyph ``spilling'' in
15087 continuation lines' rows is implemented for
15088 bidi-reordered rows. */
15089 int rv = 0;
15093 int at_zv_p = 0, exact_match_p = 0;
15095 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15096 && PT <= MATRIX_ROW_END_CHARPOS (row)
15097 && cursor_row_p (row))
15098 rv |= set_cursor_from_row (w, row, w->current_matrix,
15099 0, 0, 0, 0);
15100 /* As soon as we've found the exact match for point,
15101 or the first suitable row whose ends_at_zv_p flag
15102 is set, we are done. */
15103 at_zv_p =
15104 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15105 if (rv && !at_zv_p
15106 && w->cursor.hpos >= 0
15107 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15108 w->cursor.vpos))
15110 struct glyph_row *candidate =
15111 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15112 struct glyph *g =
15113 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15114 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15116 exact_match_p =
15117 (BUFFERP (g->object) && g->charpos == PT)
15118 || (INTEGERP (g->object)
15119 && (g->charpos == PT
15120 || (g->charpos == 0 && endpos - 1 == PT)));
15122 if (rv && (at_zv_p || exact_match_p))
15124 rc = CURSOR_MOVEMENT_SUCCESS;
15125 break;
15127 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15128 break;
15129 ++row;
15131 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15132 || row->continued_p)
15133 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15134 || (MATRIX_ROW_START_CHARPOS (row) == PT
15135 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15136 /* If we didn't find any candidate rows, or exited the
15137 loop before all the candidates were examined, signal
15138 to the caller that this method failed. */
15139 if (rc != CURSOR_MOVEMENT_SUCCESS
15140 && !(rv
15141 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15142 && !row->continued_p))
15143 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15144 else if (rv)
15145 rc = CURSOR_MOVEMENT_SUCCESS;
15147 else
15151 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15153 rc = CURSOR_MOVEMENT_SUCCESS;
15154 break;
15156 ++row;
15158 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15159 && MATRIX_ROW_START_CHARPOS (row) == PT
15160 && cursor_row_p (row));
15165 return rc;
15168 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15169 static
15170 #endif
15171 void
15172 set_vertical_scroll_bar (struct window *w)
15174 ptrdiff_t start, end, whole;
15176 /* Calculate the start and end positions for the current window.
15177 At some point, it would be nice to choose between scrollbars
15178 which reflect the whole buffer size, with special markers
15179 indicating narrowing, and scrollbars which reflect only the
15180 visible region.
15182 Note that mini-buffers sometimes aren't displaying any text. */
15183 if (!MINI_WINDOW_P (w)
15184 || (w == XWINDOW (minibuf_window)
15185 && NILP (echo_area_buffer[0])))
15187 struct buffer *buf = XBUFFER (w->contents);
15188 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15189 start = marker_position (w->start) - BUF_BEGV (buf);
15190 /* I don't think this is guaranteed to be right. For the
15191 moment, we'll pretend it is. */
15192 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15194 if (end < start)
15195 end = start;
15196 if (whole < (end - start))
15197 whole = end - start;
15199 else
15200 start = end = whole = 0;
15202 /* Indicate what this scroll bar ought to be displaying now. */
15203 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15204 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15205 (w, end - start, whole, start);
15209 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15210 selected_window is redisplayed.
15212 We can return without actually redisplaying the window if
15213 fonts_changed_p. In that case, redisplay_internal will
15214 retry. */
15216 static void
15217 redisplay_window (Lisp_Object window, int just_this_one_p)
15219 struct window *w = XWINDOW (window);
15220 struct frame *f = XFRAME (w->frame);
15221 struct buffer *buffer = XBUFFER (w->contents);
15222 struct buffer *old = current_buffer;
15223 struct text_pos lpoint, opoint, startp;
15224 int update_mode_line;
15225 int tem;
15226 struct it it;
15227 /* Record it now because it's overwritten. */
15228 int current_matrix_up_to_date_p = 0;
15229 int used_current_matrix_p = 0;
15230 /* This is less strict than current_matrix_up_to_date_p.
15231 It indicates that the buffer contents and narrowing are unchanged. */
15232 int buffer_unchanged_p = 0;
15233 int temp_scroll_step = 0;
15234 ptrdiff_t count = SPECPDL_INDEX ();
15235 int rc;
15236 int centering_position = -1;
15237 int last_line_misfit = 0;
15238 ptrdiff_t beg_unchanged, end_unchanged;
15240 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15241 opoint = lpoint;
15243 #ifdef GLYPH_DEBUG
15244 *w->desired_matrix->method = 0;
15245 #endif
15247 /* Make sure that both W's markers are valid. */
15248 eassert (XMARKER (w->start)->buffer == buffer);
15249 eassert (XMARKER (w->pointm)->buffer == buffer);
15251 restart:
15252 reconsider_clip_changes (w, buffer);
15254 /* Has the mode line to be updated? */
15255 update_mode_line = (w->update_mode_line
15256 || update_mode_lines
15257 || buffer->clip_changed
15258 || buffer->prevent_redisplay_optimizations_p);
15260 if (MINI_WINDOW_P (w))
15262 if (w == XWINDOW (echo_area_window)
15263 && !NILP (echo_area_buffer[0]))
15265 if (update_mode_line)
15266 /* We may have to update a tty frame's menu bar or a
15267 tool-bar. Example `M-x C-h C-h C-g'. */
15268 goto finish_menu_bars;
15269 else
15270 /* We've already displayed the echo area glyphs in this window. */
15271 goto finish_scroll_bars;
15273 else if ((w != XWINDOW (minibuf_window)
15274 || minibuf_level == 0)
15275 /* When buffer is nonempty, redisplay window normally. */
15276 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15277 /* Quail displays non-mini buffers in minibuffer window.
15278 In that case, redisplay the window normally. */
15279 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15281 /* W is a mini-buffer window, but it's not active, so clear
15282 it. */
15283 int yb = window_text_bottom_y (w);
15284 struct glyph_row *row;
15285 int y;
15287 for (y = 0, row = w->desired_matrix->rows;
15288 y < yb;
15289 y += row->height, ++row)
15290 blank_row (w, row, y);
15291 goto finish_scroll_bars;
15294 clear_glyph_matrix (w->desired_matrix);
15297 /* Otherwise set up data on this window; select its buffer and point
15298 value. */
15299 /* Really select the buffer, for the sake of buffer-local
15300 variables. */
15301 set_buffer_internal_1 (XBUFFER (w->contents));
15303 current_matrix_up_to_date_p
15304 = (w->window_end_valid
15305 && !current_buffer->clip_changed
15306 && !current_buffer->prevent_redisplay_optimizations_p
15307 && !window_outdated (w));
15309 /* Run the window-bottom-change-functions
15310 if it is possible that the text on the screen has changed
15311 (either due to modification of the text, or any other reason). */
15312 if (!current_matrix_up_to_date_p
15313 && !NILP (Vwindow_text_change_functions))
15315 safe_run_hooks (Qwindow_text_change_functions);
15316 goto restart;
15319 beg_unchanged = BEG_UNCHANGED;
15320 end_unchanged = END_UNCHANGED;
15322 SET_TEXT_POS (opoint, PT, PT_BYTE);
15324 specbind (Qinhibit_point_motion_hooks, Qt);
15326 buffer_unchanged_p
15327 = (w->window_end_valid
15328 && !current_buffer->clip_changed
15329 && !window_outdated (w));
15331 /* When windows_or_buffers_changed is non-zero, we can't rely on
15332 the window end being valid, so set it to nil there. */
15333 if (windows_or_buffers_changed)
15335 /* If window starts on a continuation line, maybe adjust the
15336 window start in case the window's width changed. */
15337 if (XMARKER (w->start)->buffer == current_buffer)
15338 compute_window_start_on_continuation_line (w);
15340 w->window_end_valid = 0;
15343 /* Some sanity checks. */
15344 CHECK_WINDOW_END (w);
15345 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15346 emacs_abort ();
15347 if (BYTEPOS (opoint) < CHARPOS (opoint))
15348 emacs_abort ();
15350 if (mode_line_update_needed (w))
15351 update_mode_line = 1;
15353 /* Point refers normally to the selected window. For any other
15354 window, set up appropriate value. */
15355 if (!EQ (window, selected_window))
15357 ptrdiff_t new_pt = marker_position (w->pointm);
15358 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15359 if (new_pt < BEGV)
15361 new_pt = BEGV;
15362 new_pt_byte = BEGV_BYTE;
15363 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15365 else if (new_pt > (ZV - 1))
15367 new_pt = ZV;
15368 new_pt_byte = ZV_BYTE;
15369 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15372 /* We don't use SET_PT so that the point-motion hooks don't run. */
15373 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15376 /* If any of the character widths specified in the display table
15377 have changed, invalidate the width run cache. It's true that
15378 this may be a bit late to catch such changes, but the rest of
15379 redisplay goes (non-fatally) haywire when the display table is
15380 changed, so why should we worry about doing any better? */
15381 if (current_buffer->width_run_cache)
15383 struct Lisp_Char_Table *disptab = buffer_display_table ();
15385 if (! disptab_matches_widthtab
15386 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15388 invalidate_region_cache (current_buffer,
15389 current_buffer->width_run_cache,
15390 BEG, Z);
15391 recompute_width_table (current_buffer, disptab);
15395 /* If window-start is screwed up, choose a new one. */
15396 if (XMARKER (w->start)->buffer != current_buffer)
15397 goto recenter;
15399 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15401 /* If someone specified a new starting point but did not insist,
15402 check whether it can be used. */
15403 if (w->optional_new_start
15404 && CHARPOS (startp) >= BEGV
15405 && CHARPOS (startp) <= ZV)
15407 w->optional_new_start = 0;
15408 start_display (&it, w, startp);
15409 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15410 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15411 if (IT_CHARPOS (it) == PT)
15412 w->force_start = 1;
15413 /* IT may overshoot PT if text at PT is invisible. */
15414 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15415 w->force_start = 1;
15418 force_start:
15420 /* Handle case where place to start displaying has been specified,
15421 unless the specified location is outside the accessible range. */
15422 if (w->force_start || w->frozen_window_start_p)
15424 /* We set this later on if we have to adjust point. */
15425 int new_vpos = -1;
15427 w->force_start = 0;
15428 w->vscroll = 0;
15429 w->window_end_valid = 0;
15431 /* Forget any recorded base line for line number display. */
15432 if (!buffer_unchanged_p)
15433 w->base_line_number = 0;
15435 /* Redisplay the mode line. Select the buffer properly for that.
15436 Also, run the hook window-scroll-functions
15437 because we have scrolled. */
15438 /* Note, we do this after clearing force_start because
15439 if there's an error, it is better to forget about force_start
15440 than to get into an infinite loop calling the hook functions
15441 and having them get more errors. */
15442 if (!update_mode_line
15443 || ! NILP (Vwindow_scroll_functions))
15445 update_mode_line = 1;
15446 w->update_mode_line = 1;
15447 startp = run_window_scroll_functions (window, startp);
15450 w->last_modified = 0;
15451 w->last_overlay_modified = 0;
15452 if (CHARPOS (startp) < BEGV)
15453 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15454 else if (CHARPOS (startp) > ZV)
15455 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15457 /* Redisplay, then check if cursor has been set during the
15458 redisplay. Give up if new fonts were loaded. */
15459 /* We used to issue a CHECK_MARGINS argument to try_window here,
15460 but this causes scrolling to fail when point begins inside
15461 the scroll margin (bug#148) -- cyd */
15462 if (!try_window (window, startp, 0))
15464 w->force_start = 1;
15465 clear_glyph_matrix (w->desired_matrix);
15466 goto need_larger_matrices;
15469 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15471 /* If point does not appear, try to move point so it does
15472 appear. The desired matrix has been built above, so we
15473 can use it here. */
15474 new_vpos = window_box_height (w) / 2;
15477 if (!cursor_row_fully_visible_p (w, 0, 0))
15479 /* Point does appear, but on a line partly visible at end of window.
15480 Move it back to a fully-visible line. */
15481 new_vpos = window_box_height (w);
15483 else if (w->cursor.vpos >=0)
15485 /* Some people insist on not letting point enter the scroll
15486 margin, even though this part handles windows that didn't
15487 scroll at all. */
15488 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15489 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15490 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15492 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15493 below, which finds the row to move point to, advances by
15494 the Y coordinate of the _next_ row, see the definition of
15495 MATRIX_ROW_BOTTOM_Y. */
15496 if (w->cursor.vpos < margin + header_line)
15497 new_vpos
15498 = pixel_margin + (header_line
15499 ? CURRENT_HEADER_LINE_HEIGHT (w)
15500 : 0) + FRAME_LINE_HEIGHT (f);
15501 else
15503 int window_height = window_box_height (w);
15505 if (header_line)
15506 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15507 if (w->cursor.y >= window_height - pixel_margin)
15508 new_vpos = window_height - pixel_margin;
15512 /* If we need to move point for either of the above reasons,
15513 now actually do it. */
15514 if (new_vpos >= 0)
15516 struct glyph_row *row;
15518 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15519 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15520 ++row;
15522 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15523 MATRIX_ROW_START_BYTEPOS (row));
15525 if (w != XWINDOW (selected_window))
15526 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15527 else if (current_buffer == old)
15528 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15530 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15532 /* If we are highlighting the region, then we just changed
15533 the region, so redisplay to show it. */
15534 if (markpos_of_region () >= 0)
15536 clear_glyph_matrix (w->desired_matrix);
15537 if (!try_window (window, startp, 0))
15538 goto need_larger_matrices;
15542 #ifdef GLYPH_DEBUG
15543 debug_method_add (w, "forced window start");
15544 #endif
15545 goto done;
15548 /* Handle case where text has not changed, only point, and it has
15549 not moved off the frame, and we are not retrying after hscroll.
15550 (current_matrix_up_to_date_p is nonzero when retrying.) */
15551 if (current_matrix_up_to_date_p
15552 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15553 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15555 switch (rc)
15557 case CURSOR_MOVEMENT_SUCCESS:
15558 used_current_matrix_p = 1;
15559 goto done;
15561 case CURSOR_MOVEMENT_MUST_SCROLL:
15562 goto try_to_scroll;
15564 default:
15565 emacs_abort ();
15568 /* If current starting point was originally the beginning of a line
15569 but no longer is, find a new starting point. */
15570 else if (w->start_at_line_beg
15571 && !(CHARPOS (startp) <= BEGV
15572 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15574 #ifdef GLYPH_DEBUG
15575 debug_method_add (w, "recenter 1");
15576 #endif
15577 goto recenter;
15580 /* Try scrolling with try_window_id. Value is > 0 if update has
15581 been done, it is -1 if we know that the same window start will
15582 not work. It is 0 if unsuccessful for some other reason. */
15583 else if ((tem = try_window_id (w)) != 0)
15585 #ifdef GLYPH_DEBUG
15586 debug_method_add (w, "try_window_id %d", tem);
15587 #endif
15589 if (fonts_changed_p)
15590 goto need_larger_matrices;
15591 if (tem > 0)
15592 goto done;
15594 /* Otherwise try_window_id has returned -1 which means that we
15595 don't want the alternative below this comment to execute. */
15597 else if (CHARPOS (startp) >= BEGV
15598 && CHARPOS (startp) <= ZV
15599 && PT >= CHARPOS (startp)
15600 && (CHARPOS (startp) < ZV
15601 /* Avoid starting at end of buffer. */
15602 || CHARPOS (startp) == BEGV
15603 || !window_outdated (w)))
15605 int d1, d2, d3, d4, d5, d6;
15607 /* If first window line is a continuation line, and window start
15608 is inside the modified region, but the first change is before
15609 current window start, we must select a new window start.
15611 However, if this is the result of a down-mouse event (e.g. by
15612 extending the mouse-drag-overlay), we don't want to select a
15613 new window start, since that would change the position under
15614 the mouse, resulting in an unwanted mouse-movement rather
15615 than a simple mouse-click. */
15616 if (!w->start_at_line_beg
15617 && NILP (do_mouse_tracking)
15618 && CHARPOS (startp) > BEGV
15619 && CHARPOS (startp) > BEG + beg_unchanged
15620 && CHARPOS (startp) <= Z - end_unchanged
15621 /* Even if w->start_at_line_beg is nil, a new window may
15622 start at a line_beg, since that's how set_buffer_window
15623 sets it. So, we need to check the return value of
15624 compute_window_start_on_continuation_line. (See also
15625 bug#197). */
15626 && XMARKER (w->start)->buffer == current_buffer
15627 && compute_window_start_on_continuation_line (w)
15628 /* It doesn't make sense to force the window start like we
15629 do at label force_start if it is already known that point
15630 will not be visible in the resulting window, because
15631 doing so will move point from its correct position
15632 instead of scrolling the window to bring point into view.
15633 See bug#9324. */
15634 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15636 w->force_start = 1;
15637 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15638 goto force_start;
15641 #ifdef GLYPH_DEBUG
15642 debug_method_add (w, "same window start");
15643 #endif
15645 /* Try to redisplay starting at same place as before.
15646 If point has not moved off frame, accept the results. */
15647 if (!current_matrix_up_to_date_p
15648 /* Don't use try_window_reusing_current_matrix in this case
15649 because a window scroll function can have changed the
15650 buffer. */
15651 || !NILP (Vwindow_scroll_functions)
15652 || MINI_WINDOW_P (w)
15653 || !(used_current_matrix_p
15654 = try_window_reusing_current_matrix (w)))
15656 IF_DEBUG (debug_method_add (w, "1"));
15657 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15658 /* -1 means we need to scroll.
15659 0 means we need new matrices, but fonts_changed_p
15660 is set in that case, so we will detect it below. */
15661 goto try_to_scroll;
15664 if (fonts_changed_p)
15665 goto need_larger_matrices;
15667 if (w->cursor.vpos >= 0)
15669 if (!just_this_one_p
15670 || current_buffer->clip_changed
15671 || BEG_UNCHANGED < CHARPOS (startp))
15672 /* Forget any recorded base line for line number display. */
15673 w->base_line_number = 0;
15675 if (!cursor_row_fully_visible_p (w, 1, 0))
15677 clear_glyph_matrix (w->desired_matrix);
15678 last_line_misfit = 1;
15680 /* Drop through and scroll. */
15681 else
15682 goto done;
15684 else
15685 clear_glyph_matrix (w->desired_matrix);
15688 try_to_scroll:
15690 w->last_modified = 0;
15691 w->last_overlay_modified = 0;
15693 /* Redisplay the mode line. Select the buffer properly for that. */
15694 if (!update_mode_line)
15696 update_mode_line = 1;
15697 w->update_mode_line = 1;
15700 /* Try to scroll by specified few lines. */
15701 if ((scroll_conservatively
15702 || emacs_scroll_step
15703 || temp_scroll_step
15704 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15705 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15706 && CHARPOS (startp) >= BEGV
15707 && CHARPOS (startp) <= ZV)
15709 /* The function returns -1 if new fonts were loaded, 1 if
15710 successful, 0 if not successful. */
15711 int ss = try_scrolling (window, just_this_one_p,
15712 scroll_conservatively,
15713 emacs_scroll_step,
15714 temp_scroll_step, last_line_misfit);
15715 switch (ss)
15717 case SCROLLING_SUCCESS:
15718 goto done;
15720 case SCROLLING_NEED_LARGER_MATRICES:
15721 goto need_larger_matrices;
15723 case SCROLLING_FAILED:
15724 break;
15726 default:
15727 emacs_abort ();
15731 /* Finally, just choose a place to start which positions point
15732 according to user preferences. */
15734 recenter:
15736 #ifdef GLYPH_DEBUG
15737 debug_method_add (w, "recenter");
15738 #endif
15740 /* Forget any previously recorded base line for line number display. */
15741 if (!buffer_unchanged_p)
15742 w->base_line_number = 0;
15744 /* Determine the window start relative to point. */
15745 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15746 it.current_y = it.last_visible_y;
15747 if (centering_position < 0)
15749 int margin =
15750 scroll_margin > 0
15751 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15752 : 0;
15753 ptrdiff_t margin_pos = CHARPOS (startp);
15754 Lisp_Object aggressive;
15755 int scrolling_up;
15757 /* If there is a scroll margin at the top of the window, find
15758 its character position. */
15759 if (margin
15760 /* Cannot call start_display if startp is not in the
15761 accessible region of the buffer. This can happen when we
15762 have just switched to a different buffer and/or changed
15763 its restriction. In that case, startp is initialized to
15764 the character position 1 (BEGV) because we did not yet
15765 have chance to display the buffer even once. */
15766 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15768 struct it it1;
15769 void *it1data = NULL;
15771 SAVE_IT (it1, it, it1data);
15772 start_display (&it1, w, startp);
15773 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15774 margin_pos = IT_CHARPOS (it1);
15775 RESTORE_IT (&it, &it, it1data);
15777 scrolling_up = PT > margin_pos;
15778 aggressive =
15779 scrolling_up
15780 ? BVAR (current_buffer, scroll_up_aggressively)
15781 : BVAR (current_buffer, scroll_down_aggressively);
15783 if (!MINI_WINDOW_P (w)
15784 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15786 int pt_offset = 0;
15788 /* Setting scroll-conservatively overrides
15789 scroll-*-aggressively. */
15790 if (!scroll_conservatively && NUMBERP (aggressive))
15792 double float_amount = XFLOATINT (aggressive);
15794 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15795 if (pt_offset == 0 && float_amount > 0)
15796 pt_offset = 1;
15797 if (pt_offset && margin > 0)
15798 margin -= 1;
15800 /* Compute how much to move the window start backward from
15801 point so that point will be displayed where the user
15802 wants it. */
15803 if (scrolling_up)
15805 centering_position = it.last_visible_y;
15806 if (pt_offset)
15807 centering_position -= pt_offset;
15808 centering_position -=
15809 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15810 + WINDOW_HEADER_LINE_HEIGHT (w);
15811 /* Don't let point enter the scroll margin near top of
15812 the window. */
15813 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15814 centering_position = margin * FRAME_LINE_HEIGHT (f);
15816 else
15817 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15819 else
15820 /* Set the window start half the height of the window backward
15821 from point. */
15822 centering_position = window_box_height (w) / 2;
15824 move_it_vertically_backward (&it, centering_position);
15826 eassert (IT_CHARPOS (it) >= BEGV);
15828 /* The function move_it_vertically_backward may move over more
15829 than the specified y-distance. If it->w is small, e.g. a
15830 mini-buffer window, we may end up in front of the window's
15831 display area. Start displaying at the start of the line
15832 containing PT in this case. */
15833 if (it.current_y <= 0)
15835 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15836 move_it_vertically_backward (&it, 0);
15837 it.current_y = 0;
15840 it.current_x = it.hpos = 0;
15842 /* Set the window start position here explicitly, to avoid an
15843 infinite loop in case the functions in window-scroll-functions
15844 get errors. */
15845 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15847 /* Run scroll hooks. */
15848 startp = run_window_scroll_functions (window, it.current.pos);
15850 /* Redisplay the window. */
15851 if (!current_matrix_up_to_date_p
15852 || windows_or_buffers_changed
15853 || cursor_type_changed
15854 /* Don't use try_window_reusing_current_matrix in this case
15855 because it can have changed the buffer. */
15856 || !NILP (Vwindow_scroll_functions)
15857 || !just_this_one_p
15858 || MINI_WINDOW_P (w)
15859 || !(used_current_matrix_p
15860 = try_window_reusing_current_matrix (w)))
15861 try_window (window, startp, 0);
15863 /* If new fonts have been loaded (due to fontsets), give up. We
15864 have to start a new redisplay since we need to re-adjust glyph
15865 matrices. */
15866 if (fonts_changed_p)
15867 goto need_larger_matrices;
15869 /* If cursor did not appear assume that the middle of the window is
15870 in the first line of the window. Do it again with the next line.
15871 (Imagine a window of height 100, displaying two lines of height
15872 60. Moving back 50 from it->last_visible_y will end in the first
15873 line.) */
15874 if (w->cursor.vpos < 0)
15876 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15878 clear_glyph_matrix (w->desired_matrix);
15879 move_it_by_lines (&it, 1);
15880 try_window (window, it.current.pos, 0);
15882 else if (PT < IT_CHARPOS (it))
15884 clear_glyph_matrix (w->desired_matrix);
15885 move_it_by_lines (&it, -1);
15886 try_window (window, it.current.pos, 0);
15888 else
15890 /* Not much we can do about it. */
15894 /* Consider the following case: Window starts at BEGV, there is
15895 invisible, intangible text at BEGV, so that display starts at
15896 some point START > BEGV. It can happen that we are called with
15897 PT somewhere between BEGV and START. Try to handle that case. */
15898 if (w->cursor.vpos < 0)
15900 struct glyph_row *row = w->current_matrix->rows;
15901 if (row->mode_line_p)
15902 ++row;
15903 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15906 if (!cursor_row_fully_visible_p (w, 0, 0))
15908 /* If vscroll is enabled, disable it and try again. */
15909 if (w->vscroll)
15911 w->vscroll = 0;
15912 clear_glyph_matrix (w->desired_matrix);
15913 goto recenter;
15916 /* Users who set scroll-conservatively to a large number want
15917 point just above/below the scroll margin. If we ended up
15918 with point's row partially visible, move the window start to
15919 make that row fully visible and out of the margin. */
15920 if (scroll_conservatively > SCROLL_LIMIT)
15922 int margin =
15923 scroll_margin > 0
15924 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15925 : 0;
15926 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15928 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15929 clear_glyph_matrix (w->desired_matrix);
15930 if (1 == try_window (window, it.current.pos,
15931 TRY_WINDOW_CHECK_MARGINS))
15932 goto done;
15935 /* If centering point failed to make the whole line visible,
15936 put point at the top instead. That has to make the whole line
15937 visible, if it can be done. */
15938 if (centering_position == 0)
15939 goto done;
15941 clear_glyph_matrix (w->desired_matrix);
15942 centering_position = 0;
15943 goto recenter;
15946 done:
15948 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15949 w->start_at_line_beg = (CHARPOS (startp) == BEGV
15950 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
15952 /* Display the mode line, if we must. */
15953 if ((update_mode_line
15954 /* If window not full width, must redo its mode line
15955 if (a) the window to its side is being redone and
15956 (b) we do a frame-based redisplay. This is a consequence
15957 of how inverted lines are drawn in frame-based redisplay. */
15958 || (!just_this_one_p
15959 && !FRAME_WINDOW_P (f)
15960 && !WINDOW_FULL_WIDTH_P (w))
15961 /* Line number to display. */
15962 || w->base_line_pos > 0
15963 /* Column number is displayed and different from the one displayed. */
15964 || (w->column_number_displayed != -1
15965 && (w->column_number_displayed != current_column ())))
15966 /* This means that the window has a mode line. */
15967 && (WINDOW_WANTS_MODELINE_P (w)
15968 || WINDOW_WANTS_HEADER_LINE_P (w)))
15970 display_mode_lines (w);
15972 /* If mode line height has changed, arrange for a thorough
15973 immediate redisplay using the correct mode line height. */
15974 if (WINDOW_WANTS_MODELINE_P (w)
15975 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15977 fonts_changed_p = 1;
15978 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15979 = DESIRED_MODE_LINE_HEIGHT (w);
15982 /* If header line height has changed, arrange for a thorough
15983 immediate redisplay using the correct header line height. */
15984 if (WINDOW_WANTS_HEADER_LINE_P (w)
15985 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15987 fonts_changed_p = 1;
15988 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15989 = DESIRED_HEADER_LINE_HEIGHT (w);
15992 if (fonts_changed_p)
15993 goto need_larger_matrices;
15996 if (!line_number_displayed && w->base_line_pos != -1)
15998 w->base_line_pos = 0;
15999 w->base_line_number = 0;
16002 finish_menu_bars:
16004 /* When we reach a frame's selected window, redo the frame's menu bar. */
16005 if (update_mode_line
16006 && EQ (FRAME_SELECTED_WINDOW (f), window))
16008 int redisplay_menu_p = 0;
16010 if (FRAME_WINDOW_P (f))
16012 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16013 || defined (HAVE_NS) || defined (USE_GTK)
16014 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16015 #else
16016 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16017 #endif
16019 else
16020 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16022 if (redisplay_menu_p)
16023 display_menu_bar (w);
16025 #ifdef HAVE_WINDOW_SYSTEM
16026 if (FRAME_WINDOW_P (f))
16028 #if defined (USE_GTK) || defined (HAVE_NS)
16029 if (FRAME_EXTERNAL_TOOL_BAR (f))
16030 redisplay_tool_bar (f);
16031 #else
16032 if (WINDOWP (f->tool_bar_window)
16033 && (FRAME_TOOL_BAR_LINES (f) > 0
16034 || !NILP (Vauto_resize_tool_bars))
16035 && redisplay_tool_bar (f))
16036 ignore_mouse_drag_p = 1;
16037 #endif
16039 #endif
16042 #ifdef HAVE_WINDOW_SYSTEM
16043 if (FRAME_WINDOW_P (f)
16044 && update_window_fringes (w, (just_this_one_p
16045 || (!used_current_matrix_p && !overlay_arrow_seen)
16046 || w->pseudo_window_p)))
16048 update_begin (f);
16049 block_input ();
16050 if (draw_window_fringes (w, 1))
16051 x_draw_vertical_border (w);
16052 unblock_input ();
16053 update_end (f);
16055 #endif /* HAVE_WINDOW_SYSTEM */
16057 /* We go to this label, with fonts_changed_p set,
16058 if it is necessary to try again using larger glyph matrices.
16059 We have to redeem the scroll bar even in this case,
16060 because the loop in redisplay_internal expects that. */
16061 need_larger_matrices:
16063 finish_scroll_bars:
16065 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16067 /* Set the thumb's position and size. */
16068 set_vertical_scroll_bar (w);
16070 /* Note that we actually used the scroll bar attached to this
16071 window, so it shouldn't be deleted at the end of redisplay. */
16072 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16073 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16076 /* Restore current_buffer and value of point in it. The window
16077 update may have changed the buffer, so first make sure `opoint'
16078 is still valid (Bug#6177). */
16079 if (CHARPOS (opoint) < BEGV)
16080 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16081 else if (CHARPOS (opoint) > ZV)
16082 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16083 else
16084 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16086 set_buffer_internal_1 (old);
16087 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16088 shorter. This can be caused by log truncation in *Messages*. */
16089 if (CHARPOS (lpoint) <= ZV)
16090 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16092 unbind_to (count, Qnil);
16096 /* Build the complete desired matrix of WINDOW with a window start
16097 buffer position POS.
16099 Value is 1 if successful. It is zero if fonts were loaded during
16100 redisplay which makes re-adjusting glyph matrices necessary, and -1
16101 if point would appear in the scroll margins.
16102 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16103 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16104 set in FLAGS.) */
16107 try_window (Lisp_Object window, struct text_pos pos, int flags)
16109 struct window *w = XWINDOW (window);
16110 struct it it;
16111 struct glyph_row *last_text_row = NULL;
16112 struct frame *f = XFRAME (w->frame);
16114 /* Make POS the new window start. */
16115 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16117 /* Mark cursor position as unknown. No overlay arrow seen. */
16118 w->cursor.vpos = -1;
16119 overlay_arrow_seen = 0;
16121 /* Initialize iterator and info to start at POS. */
16122 start_display (&it, w, pos);
16124 /* Display all lines of W. */
16125 while (it.current_y < it.last_visible_y)
16127 if (display_line (&it))
16128 last_text_row = it.glyph_row - 1;
16129 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16130 return 0;
16133 /* Don't let the cursor end in the scroll margins. */
16134 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16135 && !MINI_WINDOW_P (w))
16137 int this_scroll_margin;
16139 if (scroll_margin > 0)
16141 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16142 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16144 else
16145 this_scroll_margin = 0;
16147 if ((w->cursor.y >= 0 /* not vscrolled */
16148 && w->cursor.y < this_scroll_margin
16149 && CHARPOS (pos) > BEGV
16150 && IT_CHARPOS (it) < ZV)
16151 /* rms: considering make_cursor_line_fully_visible_p here
16152 seems to give wrong results. We don't want to recenter
16153 when the last line is partly visible, we want to allow
16154 that case to be handled in the usual way. */
16155 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16157 w->cursor.vpos = -1;
16158 clear_glyph_matrix (w->desired_matrix);
16159 return -1;
16163 /* If bottom moved off end of frame, change mode line percentage. */
16164 if (XFASTINT (w->window_end_pos) <= 0
16165 && Z != IT_CHARPOS (it))
16166 w->update_mode_line = 1;
16168 /* Set window_end_pos to the offset of the last character displayed
16169 on the window from the end of current_buffer. Set
16170 window_end_vpos to its row number. */
16171 if (last_text_row)
16173 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16174 w->window_end_bytepos
16175 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16176 wset_window_end_pos
16177 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16178 wset_window_end_vpos
16179 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16180 eassert
16181 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16182 XFASTINT (w->window_end_vpos))));
16184 else
16186 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16187 wset_window_end_pos (w, make_number (Z - ZV));
16188 wset_window_end_vpos (w, make_number (0));
16191 /* But that is not valid info until redisplay finishes. */
16192 w->window_end_valid = 0;
16193 return 1;
16198 /************************************************************************
16199 Window redisplay reusing current matrix when buffer has not changed
16200 ************************************************************************/
16202 /* Try redisplay of window W showing an unchanged buffer with a
16203 different window start than the last time it was displayed by
16204 reusing its current matrix. Value is non-zero if successful.
16205 W->start is the new window start. */
16207 static int
16208 try_window_reusing_current_matrix (struct window *w)
16210 struct frame *f = XFRAME (w->frame);
16211 struct glyph_row *bottom_row;
16212 struct it it;
16213 struct run run;
16214 struct text_pos start, new_start;
16215 int nrows_scrolled, i;
16216 struct glyph_row *last_text_row;
16217 struct glyph_row *last_reused_text_row;
16218 struct glyph_row *start_row;
16219 int start_vpos, min_y, max_y;
16221 #ifdef GLYPH_DEBUG
16222 if (inhibit_try_window_reusing)
16223 return 0;
16224 #endif
16226 if (/* This function doesn't handle terminal frames. */
16227 !FRAME_WINDOW_P (f)
16228 /* Don't try to reuse the display if windows have been split
16229 or such. */
16230 || windows_or_buffers_changed
16231 || cursor_type_changed)
16232 return 0;
16234 /* Can't do this if region may have changed. */
16235 if (markpos_of_region () >= 0
16236 || w->region_showing
16237 || !NILP (Vshow_trailing_whitespace))
16238 return 0;
16240 /* If top-line visibility has changed, give up. */
16241 if (WINDOW_WANTS_HEADER_LINE_P (w)
16242 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16243 return 0;
16245 /* Give up if old or new display is scrolled vertically. We could
16246 make this function handle this, but right now it doesn't. */
16247 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16248 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16249 return 0;
16251 /* The variable new_start now holds the new window start. The old
16252 start `start' can be determined from the current matrix. */
16253 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16254 start = start_row->minpos;
16255 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16257 /* Clear the desired matrix for the display below. */
16258 clear_glyph_matrix (w->desired_matrix);
16260 if (CHARPOS (new_start) <= CHARPOS (start))
16262 /* Don't use this method if the display starts with an ellipsis
16263 displayed for invisible text. It's not easy to handle that case
16264 below, and it's certainly not worth the effort since this is
16265 not a frequent case. */
16266 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16267 return 0;
16269 IF_DEBUG (debug_method_add (w, "twu1"));
16271 /* Display up to a row that can be reused. The variable
16272 last_text_row is set to the last row displayed that displays
16273 text. Note that it.vpos == 0 if or if not there is a
16274 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16275 start_display (&it, w, new_start);
16276 w->cursor.vpos = -1;
16277 last_text_row = last_reused_text_row = NULL;
16279 while (it.current_y < it.last_visible_y
16280 && !fonts_changed_p)
16282 /* If we have reached into the characters in the START row,
16283 that means the line boundaries have changed. So we
16284 can't start copying with the row START. Maybe it will
16285 work to start copying with the following row. */
16286 while (IT_CHARPOS (it) > CHARPOS (start))
16288 /* Advance to the next row as the "start". */
16289 start_row++;
16290 start = start_row->minpos;
16291 /* If there are no more rows to try, or just one, give up. */
16292 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16293 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16294 || CHARPOS (start) == ZV)
16296 clear_glyph_matrix (w->desired_matrix);
16297 return 0;
16300 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16302 /* If we have reached alignment, we can copy the rest of the
16303 rows. */
16304 if (IT_CHARPOS (it) == CHARPOS (start)
16305 /* Don't accept "alignment" inside a display vector,
16306 since start_row could have started in the middle of
16307 that same display vector (thus their character
16308 positions match), and we have no way of telling if
16309 that is the case. */
16310 && it.current.dpvec_index < 0)
16311 break;
16313 if (display_line (&it))
16314 last_text_row = it.glyph_row - 1;
16318 /* A value of current_y < last_visible_y means that we stopped
16319 at the previous window start, which in turn means that we
16320 have at least one reusable row. */
16321 if (it.current_y < it.last_visible_y)
16323 struct glyph_row *row;
16325 /* IT.vpos always starts from 0; it counts text lines. */
16326 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16328 /* Find PT if not already found in the lines displayed. */
16329 if (w->cursor.vpos < 0)
16331 int dy = it.current_y - start_row->y;
16333 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16334 row = row_containing_pos (w, PT, row, NULL, dy);
16335 if (row)
16336 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16337 dy, nrows_scrolled);
16338 else
16340 clear_glyph_matrix (w->desired_matrix);
16341 return 0;
16345 /* Scroll the display. Do it before the current matrix is
16346 changed. The problem here is that update has not yet
16347 run, i.e. part of the current matrix is not up to date.
16348 scroll_run_hook will clear the cursor, and use the
16349 current matrix to get the height of the row the cursor is
16350 in. */
16351 run.current_y = start_row->y;
16352 run.desired_y = it.current_y;
16353 run.height = it.last_visible_y - it.current_y;
16355 if (run.height > 0 && run.current_y != run.desired_y)
16357 update_begin (f);
16358 FRAME_RIF (f)->update_window_begin_hook (w);
16359 FRAME_RIF (f)->clear_window_mouse_face (w);
16360 FRAME_RIF (f)->scroll_run_hook (w, &run);
16361 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16362 update_end (f);
16365 /* Shift current matrix down by nrows_scrolled lines. */
16366 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16367 rotate_matrix (w->current_matrix,
16368 start_vpos,
16369 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16370 nrows_scrolled);
16372 /* Disable lines that must be updated. */
16373 for (i = 0; i < nrows_scrolled; ++i)
16374 (start_row + i)->enabled_p = 0;
16376 /* Re-compute Y positions. */
16377 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16378 max_y = it.last_visible_y;
16379 for (row = start_row + nrows_scrolled;
16380 row < bottom_row;
16381 ++row)
16383 row->y = it.current_y;
16384 row->visible_height = row->height;
16386 if (row->y < min_y)
16387 row->visible_height -= min_y - row->y;
16388 if (row->y + row->height > max_y)
16389 row->visible_height -= row->y + row->height - max_y;
16390 if (row->fringe_bitmap_periodic_p)
16391 row->redraw_fringe_bitmaps_p = 1;
16393 it.current_y += row->height;
16395 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16396 last_reused_text_row = row;
16397 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16398 break;
16401 /* Disable lines in the current matrix which are now
16402 below the window. */
16403 for (++row; row < bottom_row; ++row)
16404 row->enabled_p = row->mode_line_p = 0;
16407 /* Update window_end_pos etc.; last_reused_text_row is the last
16408 reused row from the current matrix containing text, if any.
16409 The value of last_text_row is the last displayed line
16410 containing text. */
16411 if (last_reused_text_row)
16413 w->window_end_bytepos
16414 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16415 wset_window_end_pos
16416 (w, make_number (Z
16417 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16418 wset_window_end_vpos
16419 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16420 w->current_matrix)));
16422 else if (last_text_row)
16424 w->window_end_bytepos
16425 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16426 wset_window_end_pos
16427 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16428 wset_window_end_vpos
16429 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16430 w->desired_matrix)));
16432 else
16434 /* This window must be completely empty. */
16435 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16436 wset_window_end_pos (w, make_number (Z - ZV));
16437 wset_window_end_vpos (w, make_number (0));
16439 w->window_end_valid = 0;
16441 /* Update hint: don't try scrolling again in update_window. */
16442 w->desired_matrix->no_scrolling_p = 1;
16444 #ifdef GLYPH_DEBUG
16445 debug_method_add (w, "try_window_reusing_current_matrix 1");
16446 #endif
16447 return 1;
16449 else if (CHARPOS (new_start) > CHARPOS (start))
16451 struct glyph_row *pt_row, *row;
16452 struct glyph_row *first_reusable_row;
16453 struct glyph_row *first_row_to_display;
16454 int dy;
16455 int yb = window_text_bottom_y (w);
16457 /* Find the row starting at new_start, if there is one. Don't
16458 reuse a partially visible line at the end. */
16459 first_reusable_row = start_row;
16460 while (first_reusable_row->enabled_p
16461 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16462 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16463 < CHARPOS (new_start)))
16464 ++first_reusable_row;
16466 /* Give up if there is no row to reuse. */
16467 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16468 || !first_reusable_row->enabled_p
16469 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16470 != CHARPOS (new_start)))
16471 return 0;
16473 /* We can reuse fully visible rows beginning with
16474 first_reusable_row to the end of the window. Set
16475 first_row_to_display to the first row that cannot be reused.
16476 Set pt_row to the row containing point, if there is any. */
16477 pt_row = NULL;
16478 for (first_row_to_display = first_reusable_row;
16479 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16480 ++first_row_to_display)
16482 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16483 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16484 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16485 && first_row_to_display->ends_at_zv_p
16486 && pt_row == NULL)))
16487 pt_row = first_row_to_display;
16490 /* Start displaying at the start of first_row_to_display. */
16491 eassert (first_row_to_display->y < yb);
16492 init_to_row_start (&it, w, first_row_to_display);
16494 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16495 - start_vpos);
16496 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16497 - nrows_scrolled);
16498 it.current_y = (first_row_to_display->y - first_reusable_row->y
16499 + WINDOW_HEADER_LINE_HEIGHT (w));
16501 /* Display lines beginning with first_row_to_display in the
16502 desired matrix. Set last_text_row to the last row displayed
16503 that displays text. */
16504 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16505 if (pt_row == NULL)
16506 w->cursor.vpos = -1;
16507 last_text_row = NULL;
16508 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16509 if (display_line (&it))
16510 last_text_row = it.glyph_row - 1;
16512 /* If point is in a reused row, adjust y and vpos of the cursor
16513 position. */
16514 if (pt_row)
16516 w->cursor.vpos -= nrows_scrolled;
16517 w->cursor.y -= first_reusable_row->y - start_row->y;
16520 /* Give up if point isn't in a row displayed or reused. (This
16521 also handles the case where w->cursor.vpos < nrows_scrolled
16522 after the calls to display_line, which can happen with scroll
16523 margins. See bug#1295.) */
16524 if (w->cursor.vpos < 0)
16526 clear_glyph_matrix (w->desired_matrix);
16527 return 0;
16530 /* Scroll the display. */
16531 run.current_y = first_reusable_row->y;
16532 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16533 run.height = it.last_visible_y - run.current_y;
16534 dy = run.current_y - run.desired_y;
16536 if (run.height)
16538 update_begin (f);
16539 FRAME_RIF (f)->update_window_begin_hook (w);
16540 FRAME_RIF (f)->clear_window_mouse_face (w);
16541 FRAME_RIF (f)->scroll_run_hook (w, &run);
16542 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16543 update_end (f);
16546 /* Adjust Y positions of reused rows. */
16547 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16548 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16549 max_y = it.last_visible_y;
16550 for (row = first_reusable_row; row < first_row_to_display; ++row)
16552 row->y -= dy;
16553 row->visible_height = row->height;
16554 if (row->y < min_y)
16555 row->visible_height -= min_y - row->y;
16556 if (row->y + row->height > max_y)
16557 row->visible_height -= row->y + row->height - max_y;
16558 if (row->fringe_bitmap_periodic_p)
16559 row->redraw_fringe_bitmaps_p = 1;
16562 /* Scroll the current matrix. */
16563 eassert (nrows_scrolled > 0);
16564 rotate_matrix (w->current_matrix,
16565 start_vpos,
16566 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16567 -nrows_scrolled);
16569 /* Disable rows not reused. */
16570 for (row -= nrows_scrolled; row < bottom_row; ++row)
16571 row->enabled_p = 0;
16573 /* Point may have moved to a different line, so we cannot assume that
16574 the previous cursor position is valid; locate the correct row. */
16575 if (pt_row)
16577 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16578 row < bottom_row
16579 && PT >= MATRIX_ROW_END_CHARPOS (row)
16580 && !row->ends_at_zv_p;
16581 row++)
16583 w->cursor.vpos++;
16584 w->cursor.y = row->y;
16586 if (row < bottom_row)
16588 /* Can't simply scan the row for point with
16589 bidi-reordered glyph rows. Let set_cursor_from_row
16590 figure out where to put the cursor, and if it fails,
16591 give up. */
16592 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16594 if (!set_cursor_from_row (w, row, w->current_matrix,
16595 0, 0, 0, 0))
16597 clear_glyph_matrix (w->desired_matrix);
16598 return 0;
16601 else
16603 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16604 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16606 for (; glyph < end
16607 && (!BUFFERP (glyph->object)
16608 || glyph->charpos < PT);
16609 glyph++)
16611 w->cursor.hpos++;
16612 w->cursor.x += glyph->pixel_width;
16618 /* Adjust window end. A null value of last_text_row means that
16619 the window end is in reused rows which in turn means that
16620 only its vpos can have changed. */
16621 if (last_text_row)
16623 w->window_end_bytepos
16624 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16625 wset_window_end_pos
16626 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16627 wset_window_end_vpos
16628 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16629 w->desired_matrix)));
16631 else
16633 wset_window_end_vpos
16634 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16637 w->window_end_valid = 0;
16638 w->desired_matrix->no_scrolling_p = 1;
16640 #ifdef GLYPH_DEBUG
16641 debug_method_add (w, "try_window_reusing_current_matrix 2");
16642 #endif
16643 return 1;
16646 return 0;
16651 /************************************************************************
16652 Window redisplay reusing current matrix when buffer has changed
16653 ************************************************************************/
16655 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16656 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16657 ptrdiff_t *, ptrdiff_t *);
16658 static struct glyph_row *
16659 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16660 struct glyph_row *);
16663 /* Return the last row in MATRIX displaying text. If row START is
16664 non-null, start searching with that row. IT gives the dimensions
16665 of the display. Value is null if matrix is empty; otherwise it is
16666 a pointer to the row found. */
16668 static struct glyph_row *
16669 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16670 struct glyph_row *start)
16672 struct glyph_row *row, *row_found;
16674 /* Set row_found to the last row in IT->w's current matrix
16675 displaying text. The loop looks funny but think of partially
16676 visible lines. */
16677 row_found = NULL;
16678 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16679 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16681 eassert (row->enabled_p);
16682 row_found = row;
16683 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16684 break;
16685 ++row;
16688 return row_found;
16692 /* Return the last row in the current matrix of W that is not affected
16693 by changes at the start of current_buffer that occurred since W's
16694 current matrix was built. Value is null if no such row exists.
16696 BEG_UNCHANGED us the number of characters unchanged at the start of
16697 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16698 first changed character in current_buffer. Characters at positions <
16699 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16700 when the current matrix was built. */
16702 static struct glyph_row *
16703 find_last_unchanged_at_beg_row (struct window *w)
16705 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16706 struct glyph_row *row;
16707 struct glyph_row *row_found = NULL;
16708 int yb = window_text_bottom_y (w);
16710 /* Find the last row displaying unchanged text. */
16711 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16712 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16713 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16714 ++row)
16716 if (/* If row ends before first_changed_pos, it is unchanged,
16717 except in some case. */
16718 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16719 /* When row ends in ZV and we write at ZV it is not
16720 unchanged. */
16721 && !row->ends_at_zv_p
16722 /* When first_changed_pos is the end of a continued line,
16723 row is not unchanged because it may be no longer
16724 continued. */
16725 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16726 && (row->continued_p
16727 || row->exact_window_width_line_p))
16728 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16729 needs to be recomputed, so don't consider this row as
16730 unchanged. This happens when the last line was
16731 bidi-reordered and was killed immediately before this
16732 redisplay cycle. In that case, ROW->end stores the
16733 buffer position of the first visual-order character of
16734 the killed text, which is now beyond ZV. */
16735 && CHARPOS (row->end.pos) <= ZV)
16736 row_found = row;
16738 /* Stop if last visible row. */
16739 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16740 break;
16743 return row_found;
16747 /* Find the first glyph row in the current matrix of W that is not
16748 affected by changes at the end of current_buffer since the
16749 time W's current matrix was built.
16751 Return in *DELTA the number of chars by which buffer positions in
16752 unchanged text at the end of current_buffer must be adjusted.
16754 Return in *DELTA_BYTES the corresponding number of bytes.
16756 Value is null if no such row exists, i.e. all rows are affected by
16757 changes. */
16759 static struct glyph_row *
16760 find_first_unchanged_at_end_row (struct window *w,
16761 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16763 struct glyph_row *row;
16764 struct glyph_row *row_found = NULL;
16766 *delta = *delta_bytes = 0;
16768 /* Display must not have been paused, otherwise the current matrix
16769 is not up to date. */
16770 eassert (w->window_end_valid);
16772 /* A value of window_end_pos >= END_UNCHANGED means that the window
16773 end is in the range of changed text. If so, there is no
16774 unchanged row at the end of W's current matrix. */
16775 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16776 return NULL;
16778 /* Set row to the last row in W's current matrix displaying text. */
16779 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16781 /* If matrix is entirely empty, no unchanged row exists. */
16782 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16784 /* The value of row is the last glyph row in the matrix having a
16785 meaningful buffer position in it. The end position of row
16786 corresponds to window_end_pos. This allows us to translate
16787 buffer positions in the current matrix to current buffer
16788 positions for characters not in changed text. */
16789 ptrdiff_t Z_old =
16790 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16791 ptrdiff_t Z_BYTE_old =
16792 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16793 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16794 struct glyph_row *first_text_row
16795 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16797 *delta = Z - Z_old;
16798 *delta_bytes = Z_BYTE - Z_BYTE_old;
16800 /* Set last_unchanged_pos to the buffer position of the last
16801 character in the buffer that has not been changed. Z is the
16802 index + 1 of the last character in current_buffer, i.e. by
16803 subtracting END_UNCHANGED we get the index of the last
16804 unchanged character, and we have to add BEG to get its buffer
16805 position. */
16806 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16807 last_unchanged_pos_old = last_unchanged_pos - *delta;
16809 /* Search backward from ROW for a row displaying a line that
16810 starts at a minimum position >= last_unchanged_pos_old. */
16811 for (; row > first_text_row; --row)
16813 /* This used to abort, but it can happen.
16814 It is ok to just stop the search instead here. KFS. */
16815 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16816 break;
16818 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16819 row_found = row;
16823 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16825 return row_found;
16829 /* Make sure that glyph rows in the current matrix of window W
16830 reference the same glyph memory as corresponding rows in the
16831 frame's frame matrix. This function is called after scrolling W's
16832 current matrix on a terminal frame in try_window_id and
16833 try_window_reusing_current_matrix. */
16835 static void
16836 sync_frame_with_window_matrix_rows (struct window *w)
16838 struct frame *f = XFRAME (w->frame);
16839 struct glyph_row *window_row, *window_row_end, *frame_row;
16841 /* Preconditions: W must be a leaf window and full-width. Its frame
16842 must have a frame matrix. */
16843 eassert (BUFFERP (w->contents));
16844 eassert (WINDOW_FULL_WIDTH_P (w));
16845 eassert (!FRAME_WINDOW_P (f));
16847 /* If W is a full-width window, glyph pointers in W's current matrix
16848 have, by definition, to be the same as glyph pointers in the
16849 corresponding frame matrix. Note that frame matrices have no
16850 marginal areas (see build_frame_matrix). */
16851 window_row = w->current_matrix->rows;
16852 window_row_end = window_row + w->current_matrix->nrows;
16853 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16854 while (window_row < window_row_end)
16856 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16857 struct glyph *end = window_row->glyphs[LAST_AREA];
16859 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16860 frame_row->glyphs[TEXT_AREA] = start;
16861 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16862 frame_row->glyphs[LAST_AREA] = end;
16864 /* Disable frame rows whose corresponding window rows have
16865 been disabled in try_window_id. */
16866 if (!window_row->enabled_p)
16867 frame_row->enabled_p = 0;
16869 ++window_row, ++frame_row;
16874 /* Find the glyph row in window W containing CHARPOS. Consider all
16875 rows between START and END (not inclusive). END null means search
16876 all rows to the end of the display area of W. Value is the row
16877 containing CHARPOS or null. */
16879 struct glyph_row *
16880 row_containing_pos (struct window *w, ptrdiff_t charpos,
16881 struct glyph_row *start, struct glyph_row *end, int dy)
16883 struct glyph_row *row = start;
16884 struct glyph_row *best_row = NULL;
16885 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16886 int last_y;
16888 /* If we happen to start on a header-line, skip that. */
16889 if (row->mode_line_p)
16890 ++row;
16892 if ((end && row >= end) || !row->enabled_p)
16893 return NULL;
16895 last_y = window_text_bottom_y (w) - dy;
16897 while (1)
16899 /* Give up if we have gone too far. */
16900 if (end && row >= end)
16901 return NULL;
16902 /* This formerly returned if they were equal.
16903 I think that both quantities are of a "last plus one" type;
16904 if so, when they are equal, the row is within the screen. -- rms. */
16905 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16906 return NULL;
16908 /* If it is in this row, return this row. */
16909 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16910 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16911 /* The end position of a row equals the start
16912 position of the next row. If CHARPOS is there, we
16913 would rather consider it displayed in the next
16914 line, except when this line ends in ZV. */
16915 && !row_for_charpos_p (row, charpos)))
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 whose
16924 edges surround CHARPOS, all of these rows belonging to
16925 the same continued line. We need to find the row which
16926 fits 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 considered to hold the buffer position CHARPOS. */
18732 static int
18733 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18735 int result = 1;
18737 if (charpos == CHARPOS (row->end.pos)
18738 || charpos == 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 (charpos),
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;
18818 /* Value is non-zero if glyph row ROW should be
18819 used to hold the cursor. */
18821 static int
18822 cursor_row_p (struct glyph_row *row)
18824 return row_for_charpos_p (row, PT);
18829 /* Push the property PROP so that it will be rendered at the current
18830 position in IT. Return 1 if PROP was successfully pushed, 0
18831 otherwise. Called from handle_line_prefix to handle the
18832 `line-prefix' and `wrap-prefix' properties. */
18834 static int
18835 push_prefix_prop (struct it *it, Lisp_Object prop)
18837 struct text_pos pos =
18838 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18840 eassert (it->method == GET_FROM_BUFFER
18841 || it->method == GET_FROM_DISPLAY_VECTOR
18842 || it->method == GET_FROM_STRING);
18844 /* We need to save the current buffer/string position, so it will be
18845 restored by pop_it, because iterate_out_of_display_property
18846 depends on that being set correctly, but some situations leave
18847 it->position not yet set when this function is called. */
18848 push_it (it, &pos);
18850 if (STRINGP (prop))
18852 if (SCHARS (prop) == 0)
18854 pop_it (it);
18855 return 0;
18858 it->string = prop;
18859 it->string_from_prefix_prop_p = 1;
18860 it->multibyte_p = STRING_MULTIBYTE (it->string);
18861 it->current.overlay_string_index = -1;
18862 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18863 it->end_charpos = it->string_nchars = SCHARS (it->string);
18864 it->method = GET_FROM_STRING;
18865 it->stop_charpos = 0;
18866 it->prev_stop = 0;
18867 it->base_level_stop = 0;
18869 /* Force paragraph direction to be that of the parent
18870 buffer/string. */
18871 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18872 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18873 else
18874 it->paragraph_embedding = L2R;
18876 /* Set up the bidi iterator for this display string. */
18877 if (it->bidi_p)
18879 it->bidi_it.string.lstring = it->string;
18880 it->bidi_it.string.s = NULL;
18881 it->bidi_it.string.schars = it->end_charpos;
18882 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18883 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18884 it->bidi_it.string.unibyte = !it->multibyte_p;
18885 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18888 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18890 it->method = GET_FROM_STRETCH;
18891 it->object = prop;
18893 #ifdef HAVE_WINDOW_SYSTEM
18894 else if (IMAGEP (prop))
18896 it->what = IT_IMAGE;
18897 it->image_id = lookup_image (it->f, prop);
18898 it->method = GET_FROM_IMAGE;
18900 #endif /* HAVE_WINDOW_SYSTEM */
18901 else
18903 pop_it (it); /* bogus display property, give up */
18904 return 0;
18907 return 1;
18910 /* Return the character-property PROP at the current position in IT. */
18912 static Lisp_Object
18913 get_it_property (struct it *it, Lisp_Object prop)
18915 Lisp_Object position;
18917 if (STRINGP (it->object))
18918 position = make_number (IT_STRING_CHARPOS (*it));
18919 else if (BUFFERP (it->object))
18920 position = make_number (IT_CHARPOS (*it));
18921 else
18922 return Qnil;
18924 return Fget_char_property (position, prop, it->object);
18927 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18929 static void
18930 handle_line_prefix (struct it *it)
18932 Lisp_Object prefix;
18934 if (it->continuation_lines_width > 0)
18936 prefix = get_it_property (it, Qwrap_prefix);
18937 if (NILP (prefix))
18938 prefix = Vwrap_prefix;
18940 else
18942 prefix = get_it_property (it, Qline_prefix);
18943 if (NILP (prefix))
18944 prefix = Vline_prefix;
18946 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18948 /* If the prefix is wider than the window, and we try to wrap
18949 it, it would acquire its own wrap prefix, and so on till the
18950 iterator stack overflows. So, don't wrap the prefix. */
18951 it->line_wrap = TRUNCATE;
18952 it->avoid_cursor_p = 1;
18958 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18959 only for R2L lines from display_line and display_string, when they
18960 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18961 the line/string needs to be continued on the next glyph row. */
18962 static void
18963 unproduce_glyphs (struct it *it, int n)
18965 struct glyph *glyph, *end;
18967 eassert (it->glyph_row);
18968 eassert (it->glyph_row->reversed_p);
18969 eassert (it->area == TEXT_AREA);
18970 eassert (n <= it->glyph_row->used[TEXT_AREA]);
18972 if (n > it->glyph_row->used[TEXT_AREA])
18973 n = it->glyph_row->used[TEXT_AREA];
18974 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18975 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18976 for ( ; glyph < end; glyph++)
18977 glyph[-n] = *glyph;
18980 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18981 and ROW->maxpos. */
18982 static void
18983 find_row_edges (struct it *it, struct glyph_row *row,
18984 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18985 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18987 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18988 lines' rows is implemented for bidi-reordered rows. */
18990 /* ROW->minpos is the value of min_pos, the minimal buffer position
18991 we have in ROW, or ROW->start.pos if that is smaller. */
18992 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18993 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18994 else
18995 /* We didn't find buffer positions smaller than ROW->start, or
18996 didn't find _any_ valid buffer positions in any of the glyphs,
18997 so we must trust the iterator's computed positions. */
18998 row->minpos = row->start.pos;
18999 if (max_pos <= 0)
19001 max_pos = CHARPOS (it->current.pos);
19002 max_bpos = BYTEPOS (it->current.pos);
19005 /* Here are the various use-cases for ending the row, and the
19006 corresponding values for ROW->maxpos:
19008 Line ends in a newline from buffer eol_pos + 1
19009 Line is continued from buffer max_pos + 1
19010 Line is truncated on right it->current.pos
19011 Line ends in a newline from string max_pos + 1(*)
19012 (*) + 1 only when line ends in a forward scan
19013 Line is continued from string max_pos
19014 Line is continued from display vector max_pos
19015 Line is entirely from a string min_pos == max_pos
19016 Line is entirely from a display vector min_pos == max_pos
19017 Line that ends at ZV ZV
19019 If you discover other use-cases, please add them here as
19020 appropriate. */
19021 if (row->ends_at_zv_p)
19022 row->maxpos = it->current.pos;
19023 else if (row->used[TEXT_AREA])
19025 int seen_this_string = 0;
19026 struct glyph_row *r1 = row - 1;
19028 /* Did we see the same display string on the previous row? */
19029 if (STRINGP (it->object)
19030 /* this is not the first row */
19031 && row > it->w->desired_matrix->rows
19032 /* previous row is not the header line */
19033 && !r1->mode_line_p
19034 /* previous row also ends in a newline from a string */
19035 && r1->ends_in_newline_from_string_p)
19037 struct glyph *start, *end;
19039 /* Search for the last glyph of the previous row that came
19040 from buffer or string. Depending on whether the row is
19041 L2R or R2L, we need to process it front to back or the
19042 other way round. */
19043 if (!r1->reversed_p)
19045 start = r1->glyphs[TEXT_AREA];
19046 end = start + r1->used[TEXT_AREA];
19047 /* Glyphs inserted by redisplay have an integer (zero)
19048 as their object. */
19049 while (end > start
19050 && INTEGERP ((end - 1)->object)
19051 && (end - 1)->charpos <= 0)
19052 --end;
19053 if (end > start)
19055 if (EQ ((end - 1)->object, it->object))
19056 seen_this_string = 1;
19058 else
19059 /* If all the glyphs of the previous row were inserted
19060 by redisplay, it means the previous row was
19061 produced from a single newline, which is only
19062 possible if that newline came from the same string
19063 as the one which produced this ROW. */
19064 seen_this_string = 1;
19066 else
19068 end = r1->glyphs[TEXT_AREA] - 1;
19069 start = end + r1->used[TEXT_AREA];
19070 while (end < start
19071 && INTEGERP ((end + 1)->object)
19072 && (end + 1)->charpos <= 0)
19073 ++end;
19074 if (end < start)
19076 if (EQ ((end + 1)->object, it->object))
19077 seen_this_string = 1;
19079 else
19080 seen_this_string = 1;
19083 /* Take note of each display string that covers a newline only
19084 once, the first time we see it. This is for when a display
19085 string includes more than one newline in it. */
19086 if (row->ends_in_newline_from_string_p && !seen_this_string)
19088 /* If we were scanning the buffer forward when we displayed
19089 the string, we want to account for at least one buffer
19090 position that belongs to this row (position covered by
19091 the display string), so that cursor positioning will
19092 consider this row as a candidate when point is at the end
19093 of the visual line represented by this row. This is not
19094 required when scanning back, because max_pos will already
19095 have a much larger value. */
19096 if (CHARPOS (row->end.pos) > max_pos)
19097 INC_BOTH (max_pos, max_bpos);
19098 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19100 else if (CHARPOS (it->eol_pos) > 0)
19101 SET_TEXT_POS (row->maxpos,
19102 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19103 else if (row->continued_p)
19105 /* If max_pos is different from IT's current position, it
19106 means IT->method does not belong to the display element
19107 at max_pos. However, it also means that the display
19108 element at max_pos was displayed in its entirety on this
19109 line, which is equivalent to saying that the next line
19110 starts at the next buffer position. */
19111 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19112 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19113 else
19115 INC_BOTH (max_pos, max_bpos);
19116 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19119 else if (row->truncated_on_right_p)
19120 /* display_line already called reseat_at_next_visible_line_start,
19121 which puts the iterator at the beginning of the next line, in
19122 the logical order. */
19123 row->maxpos = it->current.pos;
19124 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19125 /* A line that is entirely from a string/image/stretch... */
19126 row->maxpos = row->minpos;
19127 else
19128 emacs_abort ();
19130 else
19131 row->maxpos = it->current.pos;
19134 /* Construct the glyph row IT->glyph_row in the desired matrix of
19135 IT->w from text at the current position of IT. See dispextern.h
19136 for an overview of struct it. Value is non-zero if
19137 IT->glyph_row displays text, as opposed to a line displaying ZV
19138 only. */
19140 static int
19141 display_line (struct it *it)
19143 struct glyph_row *row = it->glyph_row;
19144 Lisp_Object overlay_arrow_string;
19145 struct it wrap_it;
19146 void *wrap_data = NULL;
19147 int may_wrap = 0, wrap_x IF_LINT (= 0);
19148 int wrap_row_used = -1;
19149 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19150 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19151 int wrap_row_extra_line_spacing IF_LINT (= 0);
19152 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19153 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19154 int cvpos;
19155 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19156 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19158 /* We always start displaying at hpos zero even if hscrolled. */
19159 eassert (it->hpos == 0 && it->current_x == 0);
19161 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19162 >= it->w->desired_matrix->nrows)
19164 it->w->nrows_scale_factor++;
19165 fonts_changed_p = 1;
19166 return 0;
19169 /* Is IT->w showing the region? */
19170 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19172 /* Clear the result glyph row and enable it. */
19173 prepare_desired_row (row);
19175 row->y = it->current_y;
19176 row->start = it->start;
19177 row->continuation_lines_width = it->continuation_lines_width;
19178 row->displays_text_p = 1;
19179 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19180 it->starts_in_middle_of_char_p = 0;
19182 /* Arrange the overlays nicely for our purposes. Usually, we call
19183 display_line on only one line at a time, in which case this
19184 can't really hurt too much, or we call it on lines which appear
19185 one after another in the buffer, in which case all calls to
19186 recenter_overlay_lists but the first will be pretty cheap. */
19187 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19189 /* Move over display elements that are not visible because we are
19190 hscrolled. This may stop at an x-position < IT->first_visible_x
19191 if the first glyph is partially visible or if we hit a line end. */
19192 if (it->current_x < it->first_visible_x)
19194 enum move_it_result move_result;
19196 this_line_min_pos = row->start.pos;
19197 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19198 MOVE_TO_POS | MOVE_TO_X);
19199 /* If we are under a large hscroll, move_it_in_display_line_to
19200 could hit the end of the line without reaching
19201 it->first_visible_x. Pretend that we did reach it. This is
19202 especially important on a TTY, where we will call
19203 extend_face_to_end_of_line, which needs to know how many
19204 blank glyphs to produce. */
19205 if (it->current_x < it->first_visible_x
19206 && (move_result == MOVE_NEWLINE_OR_CR
19207 || move_result == MOVE_POS_MATCH_OR_ZV))
19208 it->current_x = it->first_visible_x;
19210 /* Record the smallest positions seen while we moved over
19211 display elements that are not visible. This is needed by
19212 redisplay_internal for optimizing the case where the cursor
19213 stays inside the same line. The rest of this function only
19214 considers positions that are actually displayed, so
19215 RECORD_MAX_MIN_POS will not otherwise record positions that
19216 are hscrolled to the left of the left edge of the window. */
19217 min_pos = CHARPOS (this_line_min_pos);
19218 min_bpos = BYTEPOS (this_line_min_pos);
19220 else
19222 /* We only do this when not calling `move_it_in_display_line_to'
19223 above, because move_it_in_display_line_to calls
19224 handle_line_prefix itself. */
19225 handle_line_prefix (it);
19228 /* Get the initial row height. This is either the height of the
19229 text hscrolled, if there is any, or zero. */
19230 row->ascent = it->max_ascent;
19231 row->height = it->max_ascent + it->max_descent;
19232 row->phys_ascent = it->max_phys_ascent;
19233 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19234 row->extra_line_spacing = it->max_extra_line_spacing;
19236 /* Utility macro to record max and min buffer positions seen until now. */
19237 #define RECORD_MAX_MIN_POS(IT) \
19238 do \
19240 int composition_p = !STRINGP ((IT)->string) \
19241 && ((IT)->what == IT_COMPOSITION); \
19242 ptrdiff_t current_pos = \
19243 composition_p ? (IT)->cmp_it.charpos \
19244 : IT_CHARPOS (*(IT)); \
19245 ptrdiff_t current_bpos = \
19246 composition_p ? CHAR_TO_BYTE (current_pos) \
19247 : IT_BYTEPOS (*(IT)); \
19248 if (current_pos < min_pos) \
19250 min_pos = current_pos; \
19251 min_bpos = current_bpos; \
19253 if (IT_CHARPOS (*it) > max_pos) \
19255 max_pos = IT_CHARPOS (*it); \
19256 max_bpos = IT_BYTEPOS (*it); \
19259 while (0)
19261 /* Loop generating characters. The loop is left with IT on the next
19262 character to display. */
19263 while (1)
19265 int n_glyphs_before, hpos_before, x_before;
19266 int x, nglyphs;
19267 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19269 /* Retrieve the next thing to display. Value is zero if end of
19270 buffer reached. */
19271 if (!get_next_display_element (it))
19273 /* Maybe add a space at the end of this line that is used to
19274 display the cursor there under X. Set the charpos of the
19275 first glyph of blank lines not corresponding to any text
19276 to -1. */
19277 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19278 row->exact_window_width_line_p = 1;
19279 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19280 || row->used[TEXT_AREA] == 0)
19282 row->glyphs[TEXT_AREA]->charpos = -1;
19283 row->displays_text_p = 0;
19285 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19286 && (!MINI_WINDOW_P (it->w)
19287 || (minibuf_level && EQ (it->window, minibuf_window))))
19288 row->indicate_empty_line_p = 1;
19291 it->continuation_lines_width = 0;
19292 row->ends_at_zv_p = 1;
19293 /* A row that displays right-to-left text must always have
19294 its last face extended all the way to the end of line,
19295 even if this row ends in ZV, because we still write to
19296 the screen left to right. We also need to extend the
19297 last face if the default face is remapped to some
19298 different face, otherwise the functions that clear
19299 portions of the screen will clear with the default face's
19300 background color. */
19301 if (row->reversed_p
19302 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19303 extend_face_to_end_of_line (it);
19304 break;
19307 /* Now, get the metrics of what we want to display. This also
19308 generates glyphs in `row' (which is IT->glyph_row). */
19309 n_glyphs_before = row->used[TEXT_AREA];
19310 x = it->current_x;
19312 /* Remember the line height so far in case the next element doesn't
19313 fit on the line. */
19314 if (it->line_wrap != TRUNCATE)
19316 ascent = it->max_ascent;
19317 descent = it->max_descent;
19318 phys_ascent = it->max_phys_ascent;
19319 phys_descent = it->max_phys_descent;
19321 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19323 if (IT_DISPLAYING_WHITESPACE (it))
19324 may_wrap = 1;
19325 else if (may_wrap)
19327 SAVE_IT (wrap_it, *it, wrap_data);
19328 wrap_x = x;
19329 wrap_row_used = row->used[TEXT_AREA];
19330 wrap_row_ascent = row->ascent;
19331 wrap_row_height = row->height;
19332 wrap_row_phys_ascent = row->phys_ascent;
19333 wrap_row_phys_height = row->phys_height;
19334 wrap_row_extra_line_spacing = row->extra_line_spacing;
19335 wrap_row_min_pos = min_pos;
19336 wrap_row_min_bpos = min_bpos;
19337 wrap_row_max_pos = max_pos;
19338 wrap_row_max_bpos = max_bpos;
19339 may_wrap = 0;
19344 PRODUCE_GLYPHS (it);
19346 /* If this display element was in marginal areas, continue with
19347 the next one. */
19348 if (it->area != TEXT_AREA)
19350 row->ascent = max (row->ascent, it->max_ascent);
19351 row->height = max (row->height, it->max_ascent + it->max_descent);
19352 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19353 row->phys_height = max (row->phys_height,
19354 it->max_phys_ascent + it->max_phys_descent);
19355 row->extra_line_spacing = max (row->extra_line_spacing,
19356 it->max_extra_line_spacing);
19357 set_iterator_to_next (it, 1);
19358 continue;
19361 /* Does the display element fit on the line? If we truncate
19362 lines, we should draw past the right edge of the window. If
19363 we don't truncate, we want to stop so that we can display the
19364 continuation glyph before the right margin. If lines are
19365 continued, there are two possible strategies for characters
19366 resulting in more than 1 glyph (e.g. tabs): Display as many
19367 glyphs as possible in this line and leave the rest for the
19368 continuation line, or display the whole element in the next
19369 line. Original redisplay did the former, so we do it also. */
19370 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19371 hpos_before = it->hpos;
19372 x_before = x;
19374 if (/* Not a newline. */
19375 nglyphs > 0
19376 /* Glyphs produced fit entirely in the line. */
19377 && it->current_x < it->last_visible_x)
19379 it->hpos += nglyphs;
19380 row->ascent = max (row->ascent, it->max_ascent);
19381 row->height = max (row->height, it->max_ascent + it->max_descent);
19382 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19383 row->phys_height = max (row->phys_height,
19384 it->max_phys_ascent + it->max_phys_descent);
19385 row->extra_line_spacing = max (row->extra_line_spacing,
19386 it->max_extra_line_spacing);
19387 if (it->current_x - it->pixel_width < it->first_visible_x)
19388 row->x = x - it->first_visible_x;
19389 /* Record the maximum and minimum buffer positions seen so
19390 far in glyphs that will be displayed by this row. */
19391 if (it->bidi_p)
19392 RECORD_MAX_MIN_POS (it);
19394 else
19396 int i, new_x;
19397 struct glyph *glyph;
19399 for (i = 0; i < nglyphs; ++i, x = new_x)
19401 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19402 new_x = x + glyph->pixel_width;
19404 if (/* Lines are continued. */
19405 it->line_wrap != TRUNCATE
19406 && (/* Glyph doesn't fit on the line. */
19407 new_x > it->last_visible_x
19408 /* Or it fits exactly on a window system frame. */
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 /* End of a continued line. */
19417 if (it->hpos == 0
19418 || (new_x == it->last_visible_x
19419 && FRAME_WINDOW_P (it->f)
19420 && (row->reversed_p
19421 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19422 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19424 /* Current glyph is the only one on the line or
19425 fits exactly on the line. We must continue
19426 the line because we can't draw the cursor
19427 after the glyph. */
19428 row->continued_p = 1;
19429 it->current_x = new_x;
19430 it->continuation_lines_width += new_x;
19431 ++it->hpos;
19432 if (i == nglyphs - 1)
19434 /* If line-wrap is on, check if a previous
19435 wrap point was found. */
19436 if (wrap_row_used > 0
19437 /* Even if there is a previous wrap
19438 point, continue the line here as
19439 usual, if (i) the previous character
19440 was a space or tab AND (ii) the
19441 current character is not. */
19442 && (!may_wrap
19443 || IT_DISPLAYING_WHITESPACE (it)))
19444 goto back_to_wrap;
19446 /* Record the maximum and minimum buffer
19447 positions seen so far in glyphs that will be
19448 displayed by this row. */
19449 if (it->bidi_p)
19450 RECORD_MAX_MIN_POS (it);
19451 set_iterator_to_next (it, 1);
19452 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19454 if (!get_next_display_element (it))
19456 row->exact_window_width_line_p = 1;
19457 it->continuation_lines_width = 0;
19458 row->continued_p = 0;
19459 row->ends_at_zv_p = 1;
19461 else if (ITERATOR_AT_END_OF_LINE_P (it))
19463 row->continued_p = 0;
19464 row->exact_window_width_line_p = 1;
19468 else if (it->bidi_p)
19469 RECORD_MAX_MIN_POS (it);
19471 else if (CHAR_GLYPH_PADDING_P (*glyph)
19472 && !FRAME_WINDOW_P (it->f))
19474 /* A padding glyph that doesn't fit on this line.
19475 This means the whole character doesn't fit
19476 on the line. */
19477 if (row->reversed_p)
19478 unproduce_glyphs (it, row->used[TEXT_AREA]
19479 - n_glyphs_before);
19480 row->used[TEXT_AREA] = n_glyphs_before;
19482 /* Fill the rest of the row with continuation
19483 glyphs like in 20.x. */
19484 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19485 < row->glyphs[1 + TEXT_AREA])
19486 produce_special_glyphs (it, IT_CONTINUATION);
19488 row->continued_p = 1;
19489 it->current_x = x_before;
19490 it->continuation_lines_width += x_before;
19492 /* Restore the height to what it was before the
19493 element not fitting on the line. */
19494 it->max_ascent = ascent;
19495 it->max_descent = descent;
19496 it->max_phys_ascent = phys_ascent;
19497 it->max_phys_descent = phys_descent;
19499 else if (wrap_row_used > 0)
19501 back_to_wrap:
19502 if (row->reversed_p)
19503 unproduce_glyphs (it,
19504 row->used[TEXT_AREA] - wrap_row_used);
19505 RESTORE_IT (it, &wrap_it, wrap_data);
19506 it->continuation_lines_width += wrap_x;
19507 row->used[TEXT_AREA] = wrap_row_used;
19508 row->ascent = wrap_row_ascent;
19509 row->height = wrap_row_height;
19510 row->phys_ascent = wrap_row_phys_ascent;
19511 row->phys_height = wrap_row_phys_height;
19512 row->extra_line_spacing = wrap_row_extra_line_spacing;
19513 min_pos = wrap_row_min_pos;
19514 min_bpos = wrap_row_min_bpos;
19515 max_pos = wrap_row_max_pos;
19516 max_bpos = wrap_row_max_bpos;
19517 row->continued_p = 1;
19518 row->ends_at_zv_p = 0;
19519 row->exact_window_width_line_p = 0;
19520 it->continuation_lines_width += x;
19522 /* Make sure that a non-default face is extended
19523 up to the right margin of the window. */
19524 extend_face_to_end_of_line (it);
19526 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19528 /* A TAB that extends past the right edge of the
19529 window. This produces a single glyph on
19530 window system frames. We leave the glyph in
19531 this row and let it fill the row, but don't
19532 consume the TAB. */
19533 if ((row->reversed_p
19534 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19535 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19536 produce_special_glyphs (it, IT_CONTINUATION);
19537 it->continuation_lines_width += it->last_visible_x;
19538 row->ends_in_middle_of_char_p = 1;
19539 row->continued_p = 1;
19540 glyph->pixel_width = it->last_visible_x - x;
19541 it->starts_in_middle_of_char_p = 1;
19543 else
19545 /* Something other than a TAB that draws past
19546 the right edge of the window. Restore
19547 positions to values before the element. */
19548 if (row->reversed_p)
19549 unproduce_glyphs (it, row->used[TEXT_AREA]
19550 - (n_glyphs_before + i));
19551 row->used[TEXT_AREA] = n_glyphs_before + i;
19553 /* Display continuation glyphs. */
19554 it->current_x = x_before;
19555 it->continuation_lines_width += x;
19556 if (!FRAME_WINDOW_P (it->f)
19557 || (row->reversed_p
19558 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19559 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19560 produce_special_glyphs (it, IT_CONTINUATION);
19561 row->continued_p = 1;
19563 extend_face_to_end_of_line (it);
19565 if (nglyphs > 1 && i > 0)
19567 row->ends_in_middle_of_char_p = 1;
19568 it->starts_in_middle_of_char_p = 1;
19571 /* Restore the height to what it was before the
19572 element not fitting on the line. */
19573 it->max_ascent = ascent;
19574 it->max_descent = descent;
19575 it->max_phys_ascent = phys_ascent;
19576 it->max_phys_descent = phys_descent;
19579 break;
19581 else if (new_x > it->first_visible_x)
19583 /* Increment number of glyphs actually displayed. */
19584 ++it->hpos;
19586 /* Record the maximum and minimum buffer positions
19587 seen so far in glyphs that will be displayed by
19588 this row. */
19589 if (it->bidi_p)
19590 RECORD_MAX_MIN_POS (it);
19592 if (x < it->first_visible_x)
19593 /* Glyph is partially visible, i.e. row starts at
19594 negative X position. */
19595 row->x = x - it->first_visible_x;
19597 else
19599 /* Glyph is completely off the left margin of the
19600 window. This should not happen because of the
19601 move_it_in_display_line at the start of this
19602 function, unless the text display area of the
19603 window is empty. */
19604 eassert (it->first_visible_x <= it->last_visible_x);
19607 /* Even if this display element produced no glyphs at all,
19608 we want to record its position. */
19609 if (it->bidi_p && nglyphs == 0)
19610 RECORD_MAX_MIN_POS (it);
19612 row->ascent = max (row->ascent, it->max_ascent);
19613 row->height = max (row->height, it->max_ascent + it->max_descent);
19614 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19615 row->phys_height = max (row->phys_height,
19616 it->max_phys_ascent + it->max_phys_descent);
19617 row->extra_line_spacing = max (row->extra_line_spacing,
19618 it->max_extra_line_spacing);
19620 /* End of this display line if row is continued. */
19621 if (row->continued_p || row->ends_at_zv_p)
19622 break;
19625 at_end_of_line:
19626 /* Is this a line end? If yes, we're also done, after making
19627 sure that a non-default face is extended up to the right
19628 margin of the window. */
19629 if (ITERATOR_AT_END_OF_LINE_P (it))
19631 int used_before = row->used[TEXT_AREA];
19633 row->ends_in_newline_from_string_p = STRINGP (it->object);
19635 /* Add a space at the end of the line that is used to
19636 display the cursor there. */
19637 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19638 append_space_for_newline (it, 0);
19640 /* Extend the face to the end of the line. */
19641 extend_face_to_end_of_line (it);
19643 /* Make sure we have the position. */
19644 if (used_before == 0)
19645 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19647 /* Record the position of the newline, for use in
19648 find_row_edges. */
19649 it->eol_pos = it->current.pos;
19651 /* Consume the line end. This skips over invisible lines. */
19652 set_iterator_to_next (it, 1);
19653 it->continuation_lines_width = 0;
19654 break;
19657 /* Proceed with next display element. Note that this skips
19658 over lines invisible because of selective display. */
19659 set_iterator_to_next (it, 1);
19661 /* If we truncate lines, we are done when the last displayed
19662 glyphs reach past the right margin of the window. */
19663 if (it->line_wrap == TRUNCATE
19664 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19665 ? (it->current_x >= it->last_visible_x)
19666 : (it->current_x > it->last_visible_x)))
19668 /* Maybe add truncation glyphs. */
19669 if (!FRAME_WINDOW_P (it->f)
19670 || (row->reversed_p
19671 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19672 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19674 int i, n;
19676 if (!row->reversed_p)
19678 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19679 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19680 break;
19682 else
19684 for (i = 0; i < row->used[TEXT_AREA]; i++)
19685 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19686 break;
19687 /* Remove any padding glyphs at the front of ROW, to
19688 make room for the truncation glyphs we will be
19689 adding below. The loop below always inserts at
19690 least one truncation glyph, so also remove the
19691 last glyph added to ROW. */
19692 unproduce_glyphs (it, i + 1);
19693 /* Adjust i for the loop below. */
19694 i = row->used[TEXT_AREA] - (i + 1);
19697 it->current_x = x_before;
19698 if (!FRAME_WINDOW_P (it->f))
19700 for (n = row->used[TEXT_AREA]; i < n; ++i)
19702 row->used[TEXT_AREA] = i;
19703 produce_special_glyphs (it, IT_TRUNCATION);
19706 else
19708 row->used[TEXT_AREA] = i;
19709 produce_special_glyphs (it, IT_TRUNCATION);
19712 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19714 /* Don't truncate if we can overflow newline into fringe. */
19715 if (!get_next_display_element (it))
19717 it->continuation_lines_width = 0;
19718 row->ends_at_zv_p = 1;
19719 row->exact_window_width_line_p = 1;
19720 break;
19722 if (ITERATOR_AT_END_OF_LINE_P (it))
19724 row->exact_window_width_line_p = 1;
19725 goto at_end_of_line;
19727 it->current_x = x_before;
19730 row->truncated_on_right_p = 1;
19731 it->continuation_lines_width = 0;
19732 reseat_at_next_visible_line_start (it, 0);
19733 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19734 it->hpos = hpos_before;
19735 break;
19739 if (wrap_data)
19740 bidi_unshelve_cache (wrap_data, 1);
19742 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19743 at the left window margin. */
19744 if (it->first_visible_x
19745 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19747 if (!FRAME_WINDOW_P (it->f)
19748 || (row->reversed_p
19749 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19750 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19751 insert_left_trunc_glyphs (it);
19752 row->truncated_on_left_p = 1;
19755 /* Remember the position at which this line ends.
19757 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19758 cannot be before the call to find_row_edges below, since that is
19759 where these positions are determined. */
19760 row->end = it->current;
19761 if (!it->bidi_p)
19763 row->minpos = row->start.pos;
19764 row->maxpos = row->end.pos;
19766 else
19768 /* ROW->minpos and ROW->maxpos must be the smallest and
19769 `1 + the largest' buffer positions in ROW. But if ROW was
19770 bidi-reordered, these two positions can be anywhere in the
19771 row, so we must determine them now. */
19772 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19775 /* If the start of this line is the overlay arrow-position, then
19776 mark this glyph row as the one containing the overlay arrow.
19777 This is clearly a mess with variable size fonts. It would be
19778 better to let it be displayed like cursors under X. */
19779 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19780 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19781 !NILP (overlay_arrow_string)))
19783 /* Overlay arrow in window redisplay is a fringe bitmap. */
19784 if (STRINGP (overlay_arrow_string))
19786 struct glyph_row *arrow_row
19787 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19788 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19789 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19790 struct glyph *p = row->glyphs[TEXT_AREA];
19791 struct glyph *p2, *end;
19793 /* Copy the arrow glyphs. */
19794 while (glyph < arrow_end)
19795 *p++ = *glyph++;
19797 /* Throw away padding glyphs. */
19798 p2 = p;
19799 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19800 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19801 ++p2;
19802 if (p2 > p)
19804 while (p2 < end)
19805 *p++ = *p2++;
19806 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19809 else
19811 eassert (INTEGERP (overlay_arrow_string));
19812 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19814 overlay_arrow_seen = 1;
19817 /* Highlight trailing whitespace. */
19818 if (!NILP (Vshow_trailing_whitespace))
19819 highlight_trailing_whitespace (it->f, it->glyph_row);
19821 /* Compute pixel dimensions of this line. */
19822 compute_line_metrics (it);
19824 /* Implementation note: No changes in the glyphs of ROW or in their
19825 faces can be done past this point, because compute_line_metrics
19826 computes ROW's hash value and stores it within the glyph_row
19827 structure. */
19829 /* Record whether this row ends inside an ellipsis. */
19830 row->ends_in_ellipsis_p
19831 = (it->method == GET_FROM_DISPLAY_VECTOR
19832 && it->ellipsis_p);
19834 /* Save fringe bitmaps in this row. */
19835 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19836 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19837 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19838 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19840 it->left_user_fringe_bitmap = 0;
19841 it->left_user_fringe_face_id = 0;
19842 it->right_user_fringe_bitmap = 0;
19843 it->right_user_fringe_face_id = 0;
19845 /* Maybe set the cursor. */
19846 cvpos = it->w->cursor.vpos;
19847 if ((cvpos < 0
19848 /* In bidi-reordered rows, keep checking for proper cursor
19849 position even if one has been found already, because buffer
19850 positions in such rows change non-linearly with ROW->VPOS,
19851 when a line is continued. One exception: when we are at ZV,
19852 display cursor on the first suitable glyph row, since all
19853 the empty rows after that also have their position set to ZV. */
19854 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19855 lines' rows is implemented for bidi-reordered rows. */
19856 || (it->bidi_p
19857 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19858 && PT >= MATRIX_ROW_START_CHARPOS (row)
19859 && PT <= MATRIX_ROW_END_CHARPOS (row)
19860 && cursor_row_p (row))
19861 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19863 /* Prepare for the next line. This line starts horizontally at (X
19864 HPOS) = (0 0). Vertical positions are incremented. As a
19865 convenience for the caller, IT->glyph_row is set to the next
19866 row to be used. */
19867 it->current_x = it->hpos = 0;
19868 it->current_y += row->height;
19869 SET_TEXT_POS (it->eol_pos, 0, 0);
19870 ++it->vpos;
19871 ++it->glyph_row;
19872 /* The next row should by default use the same value of the
19873 reversed_p flag as this one. set_iterator_to_next decides when
19874 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19875 the flag accordingly. */
19876 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19877 it->glyph_row->reversed_p = row->reversed_p;
19878 it->start = row->end;
19879 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19881 #undef RECORD_MAX_MIN_POS
19884 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19885 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19886 doc: /* Return paragraph direction at point in BUFFER.
19887 Value is either `left-to-right' or `right-to-left'.
19888 If BUFFER is omitted or nil, it defaults to the current buffer.
19890 Paragraph direction determines how the text in the paragraph is displayed.
19891 In left-to-right paragraphs, text begins at the left margin of the window
19892 and the reading direction is generally left to right. In right-to-left
19893 paragraphs, text begins at the right margin and is read from right to left.
19895 See also `bidi-paragraph-direction'. */)
19896 (Lisp_Object buffer)
19898 struct buffer *buf = current_buffer;
19899 struct buffer *old = buf;
19901 if (! NILP (buffer))
19903 CHECK_BUFFER (buffer);
19904 buf = XBUFFER (buffer);
19907 if (NILP (BVAR (buf, bidi_display_reordering))
19908 || NILP (BVAR (buf, enable_multibyte_characters))
19909 /* When we are loading loadup.el, the character property tables
19910 needed for bidi iteration are not yet available. */
19911 || !NILP (Vpurify_flag))
19912 return Qleft_to_right;
19913 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19914 return BVAR (buf, bidi_paragraph_direction);
19915 else
19917 /* Determine the direction from buffer text. We could try to
19918 use current_matrix if it is up to date, but this seems fast
19919 enough as it is. */
19920 struct bidi_it itb;
19921 ptrdiff_t pos = BUF_PT (buf);
19922 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19923 int c;
19924 void *itb_data = bidi_shelve_cache ();
19926 set_buffer_temp (buf);
19927 /* bidi_paragraph_init finds the base direction of the paragraph
19928 by searching forward from paragraph start. We need the base
19929 direction of the current or _previous_ paragraph, so we need
19930 to make sure we are within that paragraph. To that end, find
19931 the previous non-empty line. */
19932 if (pos >= ZV && pos > BEGV)
19933 DEC_BOTH (pos, bytepos);
19934 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19935 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19937 while ((c = FETCH_BYTE (bytepos)) == '\n'
19938 || c == ' ' || c == '\t' || c == '\f')
19940 if (bytepos <= BEGV_BYTE)
19941 break;
19942 bytepos--;
19943 pos--;
19945 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19946 bytepos--;
19948 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19949 itb.paragraph_dir = NEUTRAL_DIR;
19950 itb.string.s = NULL;
19951 itb.string.lstring = Qnil;
19952 itb.string.bufpos = 0;
19953 itb.string.unibyte = 0;
19954 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19955 bidi_unshelve_cache (itb_data, 0);
19956 set_buffer_temp (old);
19957 switch (itb.paragraph_dir)
19959 case L2R:
19960 return Qleft_to_right;
19961 break;
19962 case R2L:
19963 return Qright_to_left;
19964 break;
19965 default:
19966 emacs_abort ();
19973 /***********************************************************************
19974 Menu Bar
19975 ***********************************************************************/
19977 /* Redisplay the menu bar in the frame for window W.
19979 The menu bar of X frames that don't have X toolkit support is
19980 displayed in a special window W->frame->menu_bar_window.
19982 The menu bar of terminal frames is treated specially as far as
19983 glyph matrices are concerned. Menu bar lines are not part of
19984 windows, so the update is done directly on the frame matrix rows
19985 for the menu bar. */
19987 static void
19988 display_menu_bar (struct window *w)
19990 struct frame *f = XFRAME (WINDOW_FRAME (w));
19991 struct it it;
19992 Lisp_Object items;
19993 int i;
19995 /* Don't do all this for graphical frames. */
19996 #ifdef HAVE_NTGUI
19997 if (FRAME_W32_P (f))
19998 return;
19999 #endif
20000 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20001 if (FRAME_X_P (f))
20002 return;
20003 #endif
20005 #ifdef HAVE_NS
20006 if (FRAME_NS_P (f))
20007 return;
20008 #endif /* HAVE_NS */
20010 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20011 eassert (!FRAME_WINDOW_P (f));
20012 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20013 it.first_visible_x = 0;
20014 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20015 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20016 if (FRAME_WINDOW_P (f))
20018 /* Menu bar lines are displayed in the desired matrix of the
20019 dummy window menu_bar_window. */
20020 struct window *menu_w;
20021 menu_w = XWINDOW (f->menu_bar_window);
20022 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20023 MENU_FACE_ID);
20024 it.first_visible_x = 0;
20025 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20027 else
20028 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20030 /* This is a TTY frame, i.e. character hpos/vpos are used as
20031 pixel x/y. */
20032 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20033 MENU_FACE_ID);
20034 it.first_visible_x = 0;
20035 it.last_visible_x = FRAME_COLS (f);
20038 /* FIXME: This should be controlled by a user option. See the
20039 comments in redisplay_tool_bar and display_mode_line about
20040 this. */
20041 it.paragraph_embedding = L2R;
20043 /* Clear all rows of the menu bar. */
20044 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20046 struct glyph_row *row = it.glyph_row + i;
20047 clear_glyph_row (row);
20048 row->enabled_p = 1;
20049 row->full_width_p = 1;
20052 /* Display all items of the menu bar. */
20053 items = FRAME_MENU_BAR_ITEMS (it.f);
20054 for (i = 0; i < ASIZE (items); i += 4)
20056 Lisp_Object string;
20058 /* Stop at nil string. */
20059 string = AREF (items, i + 1);
20060 if (NILP (string))
20061 break;
20063 /* Remember where item was displayed. */
20064 ASET (items, i + 3, make_number (it.hpos));
20066 /* Display the item, pad with one space. */
20067 if (it.current_x < it.last_visible_x)
20068 display_string (NULL, string, Qnil, 0, 0, &it,
20069 SCHARS (string) + 1, 0, 0, -1);
20072 /* Fill out the line with spaces. */
20073 if (it.current_x < it.last_visible_x)
20074 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20076 /* Compute the total height of the lines. */
20077 compute_line_metrics (&it);
20082 /***********************************************************************
20083 Mode Line
20084 ***********************************************************************/
20086 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20087 FORCE is non-zero, redisplay mode lines unconditionally.
20088 Otherwise, redisplay only mode lines that are garbaged. Value is
20089 the number of windows whose mode lines were redisplayed. */
20091 static int
20092 redisplay_mode_lines (Lisp_Object window, int force)
20094 int nwindows = 0;
20096 while (!NILP (window))
20098 struct window *w = XWINDOW (window);
20100 if (WINDOWP (w->contents))
20101 nwindows += redisplay_mode_lines (w->contents, force);
20102 else if (force
20103 || FRAME_GARBAGED_P (XFRAME (w->frame))
20104 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20106 struct text_pos lpoint;
20107 struct buffer *old = current_buffer;
20109 /* Set the window's buffer for the mode line display. */
20110 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20111 set_buffer_internal_1 (XBUFFER (w->contents));
20113 /* Point refers normally to the selected window. For any
20114 other window, set up appropriate value. */
20115 if (!EQ (window, selected_window))
20117 struct text_pos pt;
20119 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20120 if (CHARPOS (pt) < BEGV)
20121 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20122 else if (CHARPOS (pt) > (ZV - 1))
20123 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20124 else
20125 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20128 /* Display mode lines. */
20129 clear_glyph_matrix (w->desired_matrix);
20130 if (display_mode_lines (w))
20132 ++nwindows;
20133 w->must_be_updated_p = 1;
20136 /* Restore old settings. */
20137 set_buffer_internal_1 (old);
20138 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20141 window = w->next;
20144 return nwindows;
20148 /* Display the mode and/or header line of window W. Value is the
20149 sum number of mode lines and header lines displayed. */
20151 static int
20152 display_mode_lines (struct window *w)
20154 Lisp_Object old_selected_window = selected_window;
20155 Lisp_Object old_selected_frame = selected_frame;
20156 Lisp_Object new_frame = w->frame;
20157 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20158 int n = 0;
20160 selected_frame = new_frame;
20161 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20162 or window's point, then we'd need select_window_1 here as well. */
20163 XSETWINDOW (selected_window, w);
20164 XFRAME (new_frame)->selected_window = selected_window;
20166 /* These will be set while the mode line specs are processed. */
20167 line_number_displayed = 0;
20168 w->column_number_displayed = -1;
20170 if (WINDOW_WANTS_MODELINE_P (w))
20172 struct window *sel_w = XWINDOW (old_selected_window);
20174 /* Select mode line face based on the real selected window. */
20175 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20176 BVAR (current_buffer, mode_line_format));
20177 ++n;
20180 if (WINDOW_WANTS_HEADER_LINE_P (w))
20182 display_mode_line (w, HEADER_LINE_FACE_ID,
20183 BVAR (current_buffer, header_line_format));
20184 ++n;
20187 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20188 selected_frame = old_selected_frame;
20189 selected_window = old_selected_window;
20190 return n;
20194 /* Display mode or header line of window W. FACE_ID specifies which
20195 line to display; it is either MODE_LINE_FACE_ID or
20196 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20197 display. Value is the pixel height of the mode/header line
20198 displayed. */
20200 static int
20201 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20203 struct it it;
20204 struct face *face;
20205 ptrdiff_t count = SPECPDL_INDEX ();
20207 init_iterator (&it, w, -1, -1, NULL, face_id);
20208 /* Don't extend on a previously drawn mode-line.
20209 This may happen if called from pos_visible_p. */
20210 it.glyph_row->enabled_p = 0;
20211 prepare_desired_row (it.glyph_row);
20213 it.glyph_row->mode_line_p = 1;
20215 /* FIXME: This should be controlled by a user option. But
20216 supporting such an option is not trivial, since the mode line is
20217 made up of many separate strings. */
20218 it.paragraph_embedding = L2R;
20220 record_unwind_protect (unwind_format_mode_line,
20221 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20223 mode_line_target = MODE_LINE_DISPLAY;
20225 /* Temporarily make frame's keyboard the current kboard so that
20226 kboard-local variables in the mode_line_format will get the right
20227 values. */
20228 push_kboard (FRAME_KBOARD (it.f));
20229 record_unwind_save_match_data ();
20230 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20231 pop_kboard ();
20233 unbind_to (count, Qnil);
20235 /* Fill up with spaces. */
20236 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20238 compute_line_metrics (&it);
20239 it.glyph_row->full_width_p = 1;
20240 it.glyph_row->continued_p = 0;
20241 it.glyph_row->truncated_on_left_p = 0;
20242 it.glyph_row->truncated_on_right_p = 0;
20244 /* Make a 3D mode-line have a shadow at its right end. */
20245 face = FACE_FROM_ID (it.f, face_id);
20246 extend_face_to_end_of_line (&it);
20247 if (face->box != FACE_NO_BOX)
20249 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20250 + it.glyph_row->used[TEXT_AREA] - 1);
20251 last->right_box_line_p = 1;
20254 return it.glyph_row->height;
20257 /* Move element ELT in LIST to the front of LIST.
20258 Return the updated list. */
20260 static Lisp_Object
20261 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20263 register Lisp_Object tail, prev;
20264 register Lisp_Object tem;
20266 tail = list;
20267 prev = Qnil;
20268 while (CONSP (tail))
20270 tem = XCAR (tail);
20272 if (EQ (elt, tem))
20274 /* Splice out the link TAIL. */
20275 if (NILP (prev))
20276 list = XCDR (tail);
20277 else
20278 Fsetcdr (prev, XCDR (tail));
20280 /* Now make it the first. */
20281 Fsetcdr (tail, list);
20282 return tail;
20284 else
20285 prev = tail;
20286 tail = XCDR (tail);
20287 QUIT;
20290 /* Not found--return unchanged LIST. */
20291 return list;
20294 /* Contribute ELT to the mode line for window IT->w. How it
20295 translates into text depends on its data type.
20297 IT describes the display environment in which we display, as usual.
20299 DEPTH is the depth in recursion. It is used to prevent
20300 infinite recursion here.
20302 FIELD_WIDTH is the number of characters the display of ELT should
20303 occupy in the mode line, and PRECISION is the maximum number of
20304 characters to display from ELT's representation. See
20305 display_string for details.
20307 Returns the hpos of the end of the text generated by ELT.
20309 PROPS is a property list to add to any string we encounter.
20311 If RISKY is nonzero, remove (disregard) any properties in any string
20312 we encounter, and ignore :eval and :propertize.
20314 The global variable `mode_line_target' determines whether the
20315 output is passed to `store_mode_line_noprop',
20316 `store_mode_line_string', or `display_string'. */
20318 static int
20319 display_mode_element (struct it *it, int depth, int field_width, int precision,
20320 Lisp_Object elt, Lisp_Object props, int risky)
20322 int n = 0, field, prec;
20323 int literal = 0;
20325 tail_recurse:
20326 if (depth > 100)
20327 elt = build_string ("*too-deep*");
20329 depth++;
20331 switch (XTYPE (elt))
20333 case Lisp_String:
20335 /* A string: output it and check for %-constructs within it. */
20336 unsigned char c;
20337 ptrdiff_t offset = 0;
20339 if (SCHARS (elt) > 0
20340 && (!NILP (props) || risky))
20342 Lisp_Object oprops, aelt;
20343 oprops = Ftext_properties_at (make_number (0), elt);
20345 /* If the starting string's properties are not what
20346 we want, translate the string. Also, if the string
20347 is risky, do that anyway. */
20349 if (NILP (Fequal (props, oprops)) || risky)
20351 /* If the starting string has properties,
20352 merge the specified ones onto the existing ones. */
20353 if (! NILP (oprops) && !risky)
20355 Lisp_Object tem;
20357 oprops = Fcopy_sequence (oprops);
20358 tem = props;
20359 while (CONSP (tem))
20361 oprops = Fplist_put (oprops, XCAR (tem),
20362 XCAR (XCDR (tem)));
20363 tem = XCDR (XCDR (tem));
20365 props = oprops;
20368 aelt = Fassoc (elt, mode_line_proptrans_alist);
20369 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20371 /* AELT is what we want. Move it to the front
20372 without consing. */
20373 elt = XCAR (aelt);
20374 mode_line_proptrans_alist
20375 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20377 else
20379 Lisp_Object tem;
20381 /* If AELT has the wrong props, it is useless.
20382 so get rid of it. */
20383 if (! NILP (aelt))
20384 mode_line_proptrans_alist
20385 = Fdelq (aelt, mode_line_proptrans_alist);
20387 elt = Fcopy_sequence (elt);
20388 Fset_text_properties (make_number (0), Flength (elt),
20389 props, elt);
20390 /* Add this item to mode_line_proptrans_alist. */
20391 mode_line_proptrans_alist
20392 = Fcons (Fcons (elt, props),
20393 mode_line_proptrans_alist);
20394 /* Truncate mode_line_proptrans_alist
20395 to at most 50 elements. */
20396 tem = Fnthcdr (make_number (50),
20397 mode_line_proptrans_alist);
20398 if (! NILP (tem))
20399 XSETCDR (tem, Qnil);
20404 offset = 0;
20406 if (literal)
20408 prec = precision - n;
20409 switch (mode_line_target)
20411 case MODE_LINE_NOPROP:
20412 case MODE_LINE_TITLE:
20413 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20414 break;
20415 case MODE_LINE_STRING:
20416 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20417 break;
20418 case MODE_LINE_DISPLAY:
20419 n += display_string (NULL, elt, Qnil, 0, 0, it,
20420 0, prec, 0, STRING_MULTIBYTE (elt));
20421 break;
20424 break;
20427 /* Handle the non-literal case. */
20429 while ((precision <= 0 || n < precision)
20430 && SREF (elt, offset) != 0
20431 && (mode_line_target != MODE_LINE_DISPLAY
20432 || it->current_x < it->last_visible_x))
20434 ptrdiff_t last_offset = offset;
20436 /* Advance to end of string or next format specifier. */
20437 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20440 if (offset - 1 != last_offset)
20442 ptrdiff_t nchars, nbytes;
20444 /* Output to end of string or up to '%'. Field width
20445 is length of string. Don't output more than
20446 PRECISION allows us. */
20447 offset--;
20449 prec = c_string_width (SDATA (elt) + last_offset,
20450 offset - last_offset, precision - n,
20451 &nchars, &nbytes);
20453 switch (mode_line_target)
20455 case MODE_LINE_NOPROP:
20456 case MODE_LINE_TITLE:
20457 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20458 break;
20459 case MODE_LINE_STRING:
20461 ptrdiff_t bytepos = last_offset;
20462 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20463 ptrdiff_t endpos = (precision <= 0
20464 ? string_byte_to_char (elt, offset)
20465 : charpos + nchars);
20467 n += store_mode_line_string (NULL,
20468 Fsubstring (elt, make_number (charpos),
20469 make_number (endpos)),
20470 0, 0, 0, Qnil);
20472 break;
20473 case MODE_LINE_DISPLAY:
20475 ptrdiff_t bytepos = last_offset;
20476 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20478 if (precision <= 0)
20479 nchars = string_byte_to_char (elt, offset) - charpos;
20480 n += display_string (NULL, elt, Qnil, 0, charpos,
20481 it, 0, nchars, 0,
20482 STRING_MULTIBYTE (elt));
20484 break;
20487 else /* c == '%' */
20489 ptrdiff_t percent_position = offset;
20491 /* Get the specified minimum width. Zero means
20492 don't pad. */
20493 field = 0;
20494 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20495 field = field * 10 + c - '0';
20497 /* Don't pad beyond the total padding allowed. */
20498 if (field_width - n > 0 && field > field_width - n)
20499 field = field_width - n;
20501 /* Note that either PRECISION <= 0 or N < PRECISION. */
20502 prec = precision - n;
20504 if (c == 'M')
20505 n += display_mode_element (it, depth, field, prec,
20506 Vglobal_mode_string, props,
20507 risky);
20508 else if (c != 0)
20510 bool multibyte;
20511 ptrdiff_t bytepos, charpos;
20512 const char *spec;
20513 Lisp_Object string;
20515 bytepos = percent_position;
20516 charpos = (STRING_MULTIBYTE (elt)
20517 ? string_byte_to_char (elt, bytepos)
20518 : bytepos);
20519 spec = decode_mode_spec (it->w, c, field, &string);
20520 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20522 switch (mode_line_target)
20524 case MODE_LINE_NOPROP:
20525 case MODE_LINE_TITLE:
20526 n += store_mode_line_noprop (spec, field, prec);
20527 break;
20528 case MODE_LINE_STRING:
20530 Lisp_Object tem = build_string (spec);
20531 props = Ftext_properties_at (make_number (charpos), elt);
20532 /* Should only keep face property in props */
20533 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20535 break;
20536 case MODE_LINE_DISPLAY:
20538 int nglyphs_before, nwritten;
20540 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20541 nwritten = display_string (spec, string, elt,
20542 charpos, 0, it,
20543 field, prec, 0,
20544 multibyte);
20546 /* Assign to the glyphs written above the
20547 string where the `%x' came from, position
20548 of the `%'. */
20549 if (nwritten > 0)
20551 struct glyph *glyph
20552 = (it->glyph_row->glyphs[TEXT_AREA]
20553 + nglyphs_before);
20554 int i;
20556 for (i = 0; i < nwritten; ++i)
20558 glyph[i].object = elt;
20559 glyph[i].charpos = charpos;
20562 n += nwritten;
20565 break;
20568 else /* c == 0 */
20569 break;
20573 break;
20575 case Lisp_Symbol:
20576 /* A symbol: process the value of the symbol recursively
20577 as if it appeared here directly. Avoid error if symbol void.
20578 Special case: if value of symbol is a string, output the string
20579 literally. */
20581 register Lisp_Object tem;
20583 /* If the variable is not marked as risky to set
20584 then its contents are risky to use. */
20585 if (NILP (Fget (elt, Qrisky_local_variable)))
20586 risky = 1;
20588 tem = Fboundp (elt);
20589 if (!NILP (tem))
20591 tem = Fsymbol_value (elt);
20592 /* If value is a string, output that string literally:
20593 don't check for % within it. */
20594 if (STRINGP (tem))
20595 literal = 1;
20597 if (!EQ (tem, elt))
20599 /* Give up right away for nil or t. */
20600 elt = tem;
20601 goto tail_recurse;
20605 break;
20607 case Lisp_Cons:
20609 register Lisp_Object car, tem;
20611 /* A cons cell: five distinct cases.
20612 If first element is :eval or :propertize, do something special.
20613 If first element is a string or a cons, process all the elements
20614 and effectively concatenate them.
20615 If first element is a negative number, truncate displaying cdr to
20616 at most that many characters. If positive, pad (with spaces)
20617 to at least that many characters.
20618 If first element is a symbol, process the cadr or caddr recursively
20619 according to whether the symbol's value is non-nil or nil. */
20620 car = XCAR (elt);
20621 if (EQ (car, QCeval))
20623 /* An element of the form (:eval FORM) means evaluate FORM
20624 and use the result as mode line elements. */
20626 if (risky)
20627 break;
20629 if (CONSP (XCDR (elt)))
20631 Lisp_Object spec;
20632 spec = safe_eval (XCAR (XCDR (elt)));
20633 n += display_mode_element (it, depth, field_width - n,
20634 precision - n, spec, props,
20635 risky);
20638 else if (EQ (car, QCpropertize))
20640 /* An element of the form (:propertize ELT PROPS...)
20641 means display ELT but applying properties PROPS. */
20643 if (risky)
20644 break;
20646 if (CONSP (XCDR (elt)))
20647 n += display_mode_element (it, depth, field_width - n,
20648 precision - n, XCAR (XCDR (elt)),
20649 XCDR (XCDR (elt)), risky);
20651 else if (SYMBOLP (car))
20653 tem = Fboundp (car);
20654 elt = XCDR (elt);
20655 if (!CONSP (elt))
20656 goto invalid;
20657 /* elt is now the cdr, and we know it is a cons cell.
20658 Use its car if CAR has a non-nil value. */
20659 if (!NILP (tem))
20661 tem = Fsymbol_value (car);
20662 if (!NILP (tem))
20664 elt = XCAR (elt);
20665 goto tail_recurse;
20668 /* Symbol's value is nil (or symbol is unbound)
20669 Get the cddr of the original list
20670 and if possible find the caddr and use that. */
20671 elt = XCDR (elt);
20672 if (NILP (elt))
20673 break;
20674 else if (!CONSP (elt))
20675 goto invalid;
20676 elt = XCAR (elt);
20677 goto tail_recurse;
20679 else if (INTEGERP (car))
20681 register int lim = XINT (car);
20682 elt = XCDR (elt);
20683 if (lim < 0)
20685 /* Negative int means reduce maximum width. */
20686 if (precision <= 0)
20687 precision = -lim;
20688 else
20689 precision = min (precision, -lim);
20691 else if (lim > 0)
20693 /* Padding specified. Don't let it be more than
20694 current maximum. */
20695 if (precision > 0)
20696 lim = min (precision, lim);
20698 /* If that's more padding than already wanted, queue it.
20699 But don't reduce padding already specified even if
20700 that is beyond the current truncation point. */
20701 field_width = max (lim, field_width);
20703 goto tail_recurse;
20705 else if (STRINGP (car) || CONSP (car))
20707 Lisp_Object halftail = elt;
20708 int len = 0;
20710 while (CONSP (elt)
20711 && (precision <= 0 || n < precision))
20713 n += display_mode_element (it, depth,
20714 /* Do padding only after the last
20715 element in the list. */
20716 (! CONSP (XCDR (elt))
20717 ? field_width - n
20718 : 0),
20719 precision - n, XCAR (elt),
20720 props, risky);
20721 elt = XCDR (elt);
20722 len++;
20723 if ((len & 1) == 0)
20724 halftail = XCDR (halftail);
20725 /* Check for cycle. */
20726 if (EQ (halftail, elt))
20727 break;
20731 break;
20733 default:
20734 invalid:
20735 elt = build_string ("*invalid*");
20736 goto tail_recurse;
20739 /* Pad to FIELD_WIDTH. */
20740 if (field_width > 0 && n < field_width)
20742 switch (mode_line_target)
20744 case MODE_LINE_NOPROP:
20745 case MODE_LINE_TITLE:
20746 n += store_mode_line_noprop ("", field_width - n, 0);
20747 break;
20748 case MODE_LINE_STRING:
20749 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20750 break;
20751 case MODE_LINE_DISPLAY:
20752 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20753 0, 0, 0);
20754 break;
20758 return n;
20761 /* Store a mode-line string element in mode_line_string_list.
20763 If STRING is non-null, display that C string. Otherwise, the Lisp
20764 string LISP_STRING is displayed.
20766 FIELD_WIDTH is the minimum number of output glyphs to produce.
20767 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20768 with spaces. FIELD_WIDTH <= 0 means don't pad.
20770 PRECISION is the maximum number of characters to output from
20771 STRING. PRECISION <= 0 means don't truncate the string.
20773 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20774 properties to the string.
20776 PROPS are the properties to add to the string.
20777 The mode_line_string_face face property is always added to the string.
20780 static int
20781 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20782 int field_width, int precision, Lisp_Object props)
20784 ptrdiff_t len;
20785 int n = 0;
20787 if (string != NULL)
20789 len = strlen (string);
20790 if (precision > 0 && len > precision)
20791 len = precision;
20792 lisp_string = make_string (string, len);
20793 if (NILP (props))
20794 props = mode_line_string_face_prop;
20795 else if (!NILP (mode_line_string_face))
20797 Lisp_Object face = Fplist_get (props, Qface);
20798 props = Fcopy_sequence (props);
20799 if (NILP (face))
20800 face = mode_line_string_face;
20801 else
20802 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20803 props = Fplist_put (props, Qface, face);
20805 Fadd_text_properties (make_number (0), make_number (len),
20806 props, lisp_string);
20808 else
20810 len = XFASTINT (Flength (lisp_string));
20811 if (precision > 0 && len > precision)
20813 len = precision;
20814 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20815 precision = -1;
20817 if (!NILP (mode_line_string_face))
20819 Lisp_Object face;
20820 if (NILP (props))
20821 props = Ftext_properties_at (make_number (0), lisp_string);
20822 face = Fplist_get (props, Qface);
20823 if (NILP (face))
20824 face = mode_line_string_face;
20825 else
20826 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20827 props = Fcons (Qface, Fcons (face, Qnil));
20828 if (copy_string)
20829 lisp_string = Fcopy_sequence (lisp_string);
20831 if (!NILP (props))
20832 Fadd_text_properties (make_number (0), make_number (len),
20833 props, lisp_string);
20836 if (len > 0)
20838 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20839 n += len;
20842 if (field_width > len)
20844 field_width -= len;
20845 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20846 if (!NILP (props))
20847 Fadd_text_properties (make_number (0), make_number (field_width),
20848 props, lisp_string);
20849 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20850 n += field_width;
20853 return n;
20857 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20858 1, 4, 0,
20859 doc: /* Format a string out of a mode line format specification.
20860 First arg FORMAT specifies the mode line format (see `mode-line-format'
20861 for details) to use.
20863 By default, the format is evaluated for the currently selected window.
20865 Optional second arg FACE specifies the face property to put on all
20866 characters for which no face is specified. The value nil means the
20867 default face. The value t means whatever face the window's mode line
20868 currently uses (either `mode-line' or `mode-line-inactive',
20869 depending on whether the window is the selected window or not).
20870 An integer value means the value string has no text
20871 properties.
20873 Optional third and fourth args WINDOW and BUFFER specify the window
20874 and buffer to use as the context for the formatting (defaults
20875 are the selected window and the WINDOW's buffer). */)
20876 (Lisp_Object format, Lisp_Object face,
20877 Lisp_Object window, Lisp_Object buffer)
20879 struct it it;
20880 int len;
20881 struct window *w;
20882 struct buffer *old_buffer = NULL;
20883 int face_id;
20884 int no_props = INTEGERP (face);
20885 ptrdiff_t count = SPECPDL_INDEX ();
20886 Lisp_Object str;
20887 int string_start = 0;
20889 w = decode_any_window (window);
20890 XSETWINDOW (window, w);
20892 if (NILP (buffer))
20893 buffer = w->contents;
20894 CHECK_BUFFER (buffer);
20896 /* Make formatting the modeline a non-op when noninteractive, otherwise
20897 there will be problems later caused by a partially initialized frame. */
20898 if (NILP (format) || noninteractive)
20899 return empty_unibyte_string;
20901 if (no_props)
20902 face = Qnil;
20904 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20905 : EQ (face, Qt) ? (EQ (window, selected_window)
20906 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20907 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20908 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20909 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20910 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20911 : DEFAULT_FACE_ID;
20913 old_buffer = current_buffer;
20915 /* Save things including mode_line_proptrans_alist,
20916 and set that to nil so that we don't alter the outer value. */
20917 record_unwind_protect (unwind_format_mode_line,
20918 format_mode_line_unwind_data
20919 (XFRAME (WINDOW_FRAME (w)),
20920 old_buffer, selected_window, 1));
20921 mode_line_proptrans_alist = Qnil;
20923 Fselect_window (window, Qt);
20924 set_buffer_internal_1 (XBUFFER (buffer));
20926 init_iterator (&it, w, -1, -1, NULL, face_id);
20928 if (no_props)
20930 mode_line_target = MODE_LINE_NOPROP;
20931 mode_line_string_face_prop = Qnil;
20932 mode_line_string_list = Qnil;
20933 string_start = MODE_LINE_NOPROP_LEN (0);
20935 else
20937 mode_line_target = MODE_LINE_STRING;
20938 mode_line_string_list = Qnil;
20939 mode_line_string_face = face;
20940 mode_line_string_face_prop
20941 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20944 push_kboard (FRAME_KBOARD (it.f));
20945 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20946 pop_kboard ();
20948 if (no_props)
20950 len = MODE_LINE_NOPROP_LEN (string_start);
20951 str = make_string (mode_line_noprop_buf + string_start, len);
20953 else
20955 mode_line_string_list = Fnreverse (mode_line_string_list);
20956 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20957 empty_unibyte_string);
20960 unbind_to (count, Qnil);
20961 return str;
20964 /* Write a null-terminated, right justified decimal representation of
20965 the positive integer D to BUF using a minimal field width WIDTH. */
20967 static void
20968 pint2str (register char *buf, register int width, register ptrdiff_t d)
20970 register char *p = buf;
20972 if (d <= 0)
20973 *p++ = '0';
20974 else
20976 while (d > 0)
20978 *p++ = d % 10 + '0';
20979 d /= 10;
20983 for (width -= (int) (p - buf); width > 0; --width)
20984 *p++ = ' ';
20985 *p-- = '\0';
20986 while (p > buf)
20988 d = *buf;
20989 *buf++ = *p;
20990 *p-- = d;
20994 /* Write a null-terminated, right justified decimal and "human
20995 readable" representation of the nonnegative integer D to BUF using
20996 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20998 static const char power_letter[] =
21000 0, /* no letter */
21001 'k', /* kilo */
21002 'M', /* mega */
21003 'G', /* giga */
21004 'T', /* tera */
21005 'P', /* peta */
21006 'E', /* exa */
21007 'Z', /* zetta */
21008 'Y' /* yotta */
21011 static void
21012 pint2hrstr (char *buf, int width, ptrdiff_t d)
21014 /* We aim to represent the nonnegative integer D as
21015 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21016 ptrdiff_t quotient = d;
21017 int remainder = 0;
21018 /* -1 means: do not use TENTHS. */
21019 int tenths = -1;
21020 int exponent = 0;
21022 /* Length of QUOTIENT.TENTHS as a string. */
21023 int length;
21025 char * psuffix;
21026 char * p;
21028 if (quotient >= 1000)
21030 /* Scale to the appropriate EXPONENT. */
21033 remainder = quotient % 1000;
21034 quotient /= 1000;
21035 exponent++;
21037 while (quotient >= 1000);
21039 /* Round to nearest and decide whether to use TENTHS or not. */
21040 if (quotient <= 9)
21042 tenths = remainder / 100;
21043 if (remainder % 100 >= 50)
21045 if (tenths < 9)
21046 tenths++;
21047 else
21049 quotient++;
21050 if (quotient == 10)
21051 tenths = -1;
21052 else
21053 tenths = 0;
21057 else
21058 if (remainder >= 500)
21060 if (quotient < 999)
21061 quotient++;
21062 else
21064 quotient = 1;
21065 exponent++;
21066 tenths = 0;
21071 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21072 if (tenths == -1 && quotient <= 99)
21073 if (quotient <= 9)
21074 length = 1;
21075 else
21076 length = 2;
21077 else
21078 length = 3;
21079 p = psuffix = buf + max (width, length);
21081 /* Print EXPONENT. */
21082 *psuffix++ = power_letter[exponent];
21083 *psuffix = '\0';
21085 /* Print TENTHS. */
21086 if (tenths >= 0)
21088 *--p = '0' + tenths;
21089 *--p = '.';
21092 /* Print QUOTIENT. */
21095 int digit = quotient % 10;
21096 *--p = '0' + digit;
21098 while ((quotient /= 10) != 0);
21100 /* Print leading spaces. */
21101 while (buf < p)
21102 *--p = ' ';
21105 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21106 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21107 type of CODING_SYSTEM. Return updated pointer into BUF. */
21109 static unsigned char invalid_eol_type[] = "(*invalid*)";
21111 static char *
21112 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21114 Lisp_Object val;
21115 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21116 const unsigned char *eol_str;
21117 int eol_str_len;
21118 /* The EOL conversion we are using. */
21119 Lisp_Object eoltype;
21121 val = CODING_SYSTEM_SPEC (coding_system);
21122 eoltype = Qnil;
21124 if (!VECTORP (val)) /* Not yet decided. */
21126 *buf++ = multibyte ? '-' : ' ';
21127 if (eol_flag)
21128 eoltype = eol_mnemonic_undecided;
21129 /* Don't mention EOL conversion if it isn't decided. */
21131 else
21133 Lisp_Object attrs;
21134 Lisp_Object eolvalue;
21136 attrs = AREF (val, 0);
21137 eolvalue = AREF (val, 2);
21139 *buf++ = multibyte
21140 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21141 : ' ';
21143 if (eol_flag)
21145 /* The EOL conversion that is normal on this system. */
21147 if (NILP (eolvalue)) /* Not yet decided. */
21148 eoltype = eol_mnemonic_undecided;
21149 else if (VECTORP (eolvalue)) /* Not yet decided. */
21150 eoltype = eol_mnemonic_undecided;
21151 else /* eolvalue is Qunix, Qdos, or Qmac. */
21152 eoltype = (EQ (eolvalue, Qunix)
21153 ? eol_mnemonic_unix
21154 : (EQ (eolvalue, Qdos) == 1
21155 ? eol_mnemonic_dos : eol_mnemonic_mac));
21159 if (eol_flag)
21161 /* Mention the EOL conversion if it is not the usual one. */
21162 if (STRINGP (eoltype))
21164 eol_str = SDATA (eoltype);
21165 eol_str_len = SBYTES (eoltype);
21167 else if (CHARACTERP (eoltype))
21169 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21170 int c = XFASTINT (eoltype);
21171 eol_str_len = CHAR_STRING (c, tmp);
21172 eol_str = tmp;
21174 else
21176 eol_str = invalid_eol_type;
21177 eol_str_len = sizeof (invalid_eol_type) - 1;
21179 memcpy (buf, eol_str, eol_str_len);
21180 buf += eol_str_len;
21183 return buf;
21186 /* Return a string for the output of a mode line %-spec for window W,
21187 generated by character C. FIELD_WIDTH > 0 means pad the string
21188 returned with spaces to that value. Return a Lisp string in
21189 *STRING if the resulting string is taken from that Lisp string.
21191 Note we operate on the current buffer for most purposes. */
21193 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21195 static const char *
21196 decode_mode_spec (struct window *w, register int c, int field_width,
21197 Lisp_Object *string)
21199 Lisp_Object obj;
21200 struct frame *f = XFRAME (WINDOW_FRAME (w));
21201 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21202 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21203 produce strings from numerical values, so limit preposterously
21204 large values of FIELD_WIDTH to avoid overrunning the buffer's
21205 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21206 bytes plus the terminating null. */
21207 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21208 struct buffer *b = current_buffer;
21210 obj = Qnil;
21211 *string = Qnil;
21213 switch (c)
21215 case '*':
21216 if (!NILP (BVAR (b, read_only)))
21217 return "%";
21218 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21219 return "*";
21220 return "-";
21222 case '+':
21223 /* This differs from %* only for a modified read-only buffer. */
21224 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21225 return "*";
21226 if (!NILP (BVAR (b, read_only)))
21227 return "%";
21228 return "-";
21230 case '&':
21231 /* This differs from %* in ignoring read-only-ness. */
21232 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21233 return "*";
21234 return "-";
21236 case '%':
21237 return "%";
21239 case '[':
21241 int i;
21242 char *p;
21244 if (command_loop_level > 5)
21245 return "[[[... ";
21246 p = decode_mode_spec_buf;
21247 for (i = 0; i < command_loop_level; i++)
21248 *p++ = '[';
21249 *p = 0;
21250 return decode_mode_spec_buf;
21253 case ']':
21255 int i;
21256 char *p;
21258 if (command_loop_level > 5)
21259 return " ...]]]";
21260 p = decode_mode_spec_buf;
21261 for (i = 0; i < command_loop_level; i++)
21262 *p++ = ']';
21263 *p = 0;
21264 return decode_mode_spec_buf;
21267 case '-':
21269 register int i;
21271 /* Let lots_of_dashes be a string of infinite length. */
21272 if (mode_line_target == MODE_LINE_NOPROP
21273 || mode_line_target == MODE_LINE_STRING)
21274 return "--";
21275 if (field_width <= 0
21276 || field_width > sizeof (lots_of_dashes))
21278 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21279 decode_mode_spec_buf[i] = '-';
21280 decode_mode_spec_buf[i] = '\0';
21281 return decode_mode_spec_buf;
21283 else
21284 return lots_of_dashes;
21287 case 'b':
21288 obj = BVAR (b, name);
21289 break;
21291 case 'c':
21292 /* %c and %l are ignored in `frame-title-format'.
21293 (In redisplay_internal, the frame title is drawn _before_ the
21294 windows are updated, so the stuff which depends on actual
21295 window contents (such as %l) may fail to render properly, or
21296 even crash emacs.) */
21297 if (mode_line_target == MODE_LINE_TITLE)
21298 return "";
21299 else
21301 ptrdiff_t col = current_column ();
21302 w->column_number_displayed = col;
21303 pint2str (decode_mode_spec_buf, width, col);
21304 return decode_mode_spec_buf;
21307 case 'e':
21308 #ifndef SYSTEM_MALLOC
21310 if (NILP (Vmemory_full))
21311 return "";
21312 else
21313 return "!MEM FULL! ";
21315 #else
21316 return "";
21317 #endif
21319 case 'F':
21320 /* %F displays the frame name. */
21321 if (!NILP (f->title))
21322 return SSDATA (f->title);
21323 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21324 return SSDATA (f->name);
21325 return "Emacs";
21327 case 'f':
21328 obj = BVAR (b, filename);
21329 break;
21331 case 'i':
21333 ptrdiff_t size = ZV - BEGV;
21334 pint2str (decode_mode_spec_buf, width, size);
21335 return decode_mode_spec_buf;
21338 case 'I':
21340 ptrdiff_t size = ZV - BEGV;
21341 pint2hrstr (decode_mode_spec_buf, width, size);
21342 return decode_mode_spec_buf;
21345 case 'l':
21347 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21348 ptrdiff_t topline, nlines, height;
21349 ptrdiff_t junk;
21351 /* %c and %l are ignored in `frame-title-format'. */
21352 if (mode_line_target == MODE_LINE_TITLE)
21353 return "";
21355 startpos = marker_position (w->start);
21356 startpos_byte = marker_byte_position (w->start);
21357 height = WINDOW_TOTAL_LINES (w);
21359 /* If we decided that this buffer isn't suitable for line numbers,
21360 don't forget that too fast. */
21361 if (w->base_line_pos == -1)
21362 goto no_value;
21364 /* If the buffer is very big, don't waste time. */
21365 if (INTEGERP (Vline_number_display_limit)
21366 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21368 w->base_line_pos = 0;
21369 w->base_line_number = 0;
21370 goto no_value;
21373 if (w->base_line_number > 0
21374 && w->base_line_pos > 0
21375 && w->base_line_pos <= startpos)
21377 line = w->base_line_number;
21378 linepos = w->base_line_pos;
21379 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21381 else
21383 line = 1;
21384 linepos = BUF_BEGV (b);
21385 linepos_byte = BUF_BEGV_BYTE (b);
21388 /* Count lines from base line to window start position. */
21389 nlines = display_count_lines (linepos_byte,
21390 startpos_byte,
21391 startpos, &junk);
21393 topline = nlines + line;
21395 /* Determine a new base line, if the old one is too close
21396 or too far away, or if we did not have one.
21397 "Too close" means it's plausible a scroll-down would
21398 go back past it. */
21399 if (startpos == BUF_BEGV (b))
21401 w->base_line_number = topline;
21402 w->base_line_pos = BUF_BEGV (b);
21404 else if (nlines < height + 25 || nlines > height * 3 + 50
21405 || linepos == BUF_BEGV (b))
21407 ptrdiff_t limit = BUF_BEGV (b);
21408 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21409 ptrdiff_t position;
21410 ptrdiff_t distance =
21411 (height * 2 + 30) * line_number_display_limit_width;
21413 if (startpos - distance > limit)
21415 limit = startpos - distance;
21416 limit_byte = CHAR_TO_BYTE (limit);
21419 nlines = display_count_lines (startpos_byte,
21420 limit_byte,
21421 - (height * 2 + 30),
21422 &position);
21423 /* If we couldn't find the lines we wanted within
21424 line_number_display_limit_width chars per line,
21425 give up on line numbers for this window. */
21426 if (position == limit_byte && limit == startpos - distance)
21428 w->base_line_pos = -1;
21429 w->base_line_number = 0;
21430 goto no_value;
21433 w->base_line_number = topline - nlines;
21434 w->base_line_pos = BYTE_TO_CHAR (position);
21437 /* Now count lines from the start pos to point. */
21438 nlines = display_count_lines (startpos_byte,
21439 PT_BYTE, PT, &junk);
21441 /* Record that we did display the line number. */
21442 line_number_displayed = 1;
21444 /* Make the string to show. */
21445 pint2str (decode_mode_spec_buf, width, topline + nlines);
21446 return decode_mode_spec_buf;
21447 no_value:
21449 char* p = decode_mode_spec_buf;
21450 int pad = width - 2;
21451 while (pad-- > 0)
21452 *p++ = ' ';
21453 *p++ = '?';
21454 *p++ = '?';
21455 *p = '\0';
21456 return decode_mode_spec_buf;
21459 break;
21461 case 'm':
21462 obj = BVAR (b, mode_name);
21463 break;
21465 case 'n':
21466 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21467 return " Narrow";
21468 break;
21470 case 'p':
21472 ptrdiff_t pos = marker_position (w->start);
21473 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21475 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21477 if (pos <= BUF_BEGV (b))
21478 return "All";
21479 else
21480 return "Bottom";
21482 else if (pos <= BUF_BEGV (b))
21483 return "Top";
21484 else
21486 if (total > 1000000)
21487 /* Do it differently for a large value, to avoid overflow. */
21488 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21489 else
21490 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21491 /* We can't normally display a 3-digit number,
21492 so get us a 2-digit number that is close. */
21493 if (total == 100)
21494 total = 99;
21495 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21496 return decode_mode_spec_buf;
21500 /* Display percentage of size above the bottom of the screen. */
21501 case 'P':
21503 ptrdiff_t toppos = marker_position (w->start);
21504 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21505 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21507 if (botpos >= BUF_ZV (b))
21509 if (toppos <= BUF_BEGV (b))
21510 return "All";
21511 else
21512 return "Bottom";
21514 else
21516 if (total > 1000000)
21517 /* Do it differently for a large value, to avoid overflow. */
21518 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21519 else
21520 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21521 /* We can't normally display a 3-digit number,
21522 so get us a 2-digit number that is close. */
21523 if (total == 100)
21524 total = 99;
21525 if (toppos <= BUF_BEGV (b))
21526 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21527 else
21528 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21529 return decode_mode_spec_buf;
21533 case 's':
21534 /* status of process */
21535 obj = Fget_buffer_process (Fcurrent_buffer ());
21536 if (NILP (obj))
21537 return "no process";
21538 #ifndef MSDOS
21539 obj = Fsymbol_name (Fprocess_status (obj));
21540 #endif
21541 break;
21543 case '@':
21545 ptrdiff_t count = inhibit_garbage_collection ();
21546 Lisp_Object val = call1 (intern ("file-remote-p"),
21547 BVAR (current_buffer, directory));
21548 unbind_to (count, Qnil);
21550 if (NILP (val))
21551 return "-";
21552 else
21553 return "@";
21556 case 'z':
21557 /* coding-system (not including end-of-line format) */
21558 case 'Z':
21559 /* coding-system (including end-of-line type) */
21561 int eol_flag = (c == 'Z');
21562 char *p = decode_mode_spec_buf;
21564 if (! FRAME_WINDOW_P (f))
21566 /* No need to mention EOL here--the terminal never needs
21567 to do EOL conversion. */
21568 p = decode_mode_spec_coding (CODING_ID_NAME
21569 (FRAME_KEYBOARD_CODING (f)->id),
21570 p, 0);
21571 p = decode_mode_spec_coding (CODING_ID_NAME
21572 (FRAME_TERMINAL_CODING (f)->id),
21573 p, 0);
21575 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21576 p, eol_flag);
21578 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21579 #ifdef subprocesses
21580 obj = Fget_buffer_process (Fcurrent_buffer ());
21581 if (PROCESSP (obj))
21583 p = decode_mode_spec_coding
21584 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21585 p = decode_mode_spec_coding
21586 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21588 #endif /* subprocesses */
21589 #endif /* 0 */
21590 *p = 0;
21591 return decode_mode_spec_buf;
21595 if (STRINGP (obj))
21597 *string = obj;
21598 return SSDATA (obj);
21600 else
21601 return "";
21605 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
21606 means count lines back from START_BYTE. But don't go beyond
21607 LIMIT_BYTE. Return the number of lines thus found (always
21608 nonnegative).
21610 Set *BYTE_POS_PTR to the byte position where we stopped. This is
21611 either the position COUNT lines after/before START_BYTE, if we
21612 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
21613 COUNT lines. */
21615 static ptrdiff_t
21616 display_count_lines (ptrdiff_t start_byte,
21617 ptrdiff_t limit_byte, ptrdiff_t count,
21618 ptrdiff_t *byte_pos_ptr)
21620 register unsigned char *cursor;
21621 unsigned char *base;
21623 register ptrdiff_t ceiling;
21624 register unsigned char *ceiling_addr;
21625 ptrdiff_t orig_count = count;
21627 /* If we are not in selective display mode,
21628 check only for newlines. */
21629 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21630 && !INTEGERP (BVAR (current_buffer, selective_display)));
21632 if (count > 0)
21634 while (start_byte < limit_byte)
21636 ceiling = BUFFER_CEILING_OF (start_byte);
21637 ceiling = min (limit_byte - 1, ceiling);
21638 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21639 base = (cursor = BYTE_POS_ADDR (start_byte));
21643 if (selective_display)
21645 while (*cursor != '\n' && *cursor != 015
21646 && ++cursor != ceiling_addr)
21647 continue;
21648 if (cursor == ceiling_addr)
21649 break;
21651 else
21653 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
21654 if (! cursor)
21655 break;
21658 cursor++;
21660 if (--count == 0)
21662 start_byte += cursor - base;
21663 *byte_pos_ptr = start_byte;
21664 return orig_count;
21667 while (cursor < ceiling_addr);
21669 start_byte += ceiling_addr - base;
21672 else
21674 while (start_byte > limit_byte)
21676 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21677 ceiling = max (limit_byte, ceiling);
21678 ceiling_addr = BYTE_POS_ADDR (ceiling);
21679 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21680 while (1)
21682 if (selective_display)
21684 while (--cursor >= ceiling_addr
21685 && *cursor != '\n' && *cursor != 015)
21686 continue;
21687 if (cursor < ceiling_addr)
21688 break;
21690 else
21692 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
21693 if (! cursor)
21694 break;
21697 if (++count == 0)
21699 start_byte += cursor - base + 1;
21700 *byte_pos_ptr = start_byte;
21701 /* When scanning backwards, we should
21702 not count the newline posterior to which we stop. */
21703 return - orig_count - 1;
21706 start_byte += ceiling_addr - base;
21710 *byte_pos_ptr = limit_byte;
21712 if (count < 0)
21713 return - orig_count + count;
21714 return orig_count - count;
21720 /***********************************************************************
21721 Displaying strings
21722 ***********************************************************************/
21724 /* Display a NUL-terminated string, starting with index START.
21726 If STRING is non-null, display that C string. Otherwise, the Lisp
21727 string LISP_STRING is displayed. There's a case that STRING is
21728 non-null and LISP_STRING is not nil. It means STRING is a string
21729 data of LISP_STRING. In that case, we display LISP_STRING while
21730 ignoring its text properties.
21732 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21733 FACE_STRING. Display STRING or LISP_STRING with the face at
21734 FACE_STRING_POS in FACE_STRING:
21736 Display the string in the environment given by IT, but use the
21737 standard display table, temporarily.
21739 FIELD_WIDTH is the minimum number of output glyphs to produce.
21740 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21741 with spaces. If STRING has more characters, more than FIELD_WIDTH
21742 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21744 PRECISION is the maximum number of characters to output from
21745 STRING. PRECISION < 0 means don't truncate the string.
21747 This is roughly equivalent to printf format specifiers:
21749 FIELD_WIDTH PRECISION PRINTF
21750 ----------------------------------------
21751 -1 -1 %s
21752 -1 10 %.10s
21753 10 -1 %10s
21754 20 10 %20.10s
21756 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21757 display them, and < 0 means obey the current buffer's value of
21758 enable_multibyte_characters.
21760 Value is the number of columns displayed. */
21762 static int
21763 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21764 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21765 int field_width, int precision, int max_x, int multibyte)
21767 int hpos_at_start = it->hpos;
21768 int saved_face_id = it->face_id;
21769 struct glyph_row *row = it->glyph_row;
21770 ptrdiff_t it_charpos;
21772 /* Initialize the iterator IT for iteration over STRING beginning
21773 with index START. */
21774 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21775 precision, field_width, multibyte);
21776 if (string && STRINGP (lisp_string))
21777 /* LISP_STRING is the one returned by decode_mode_spec. We should
21778 ignore its text properties. */
21779 it->stop_charpos = it->end_charpos;
21781 /* If displaying STRING, set up the face of the iterator from
21782 FACE_STRING, if that's given. */
21783 if (STRINGP (face_string))
21785 ptrdiff_t endptr;
21786 struct face *face;
21788 it->face_id
21789 = face_at_string_position (it->w, face_string, face_string_pos,
21790 0, it->region_beg_charpos,
21791 it->region_end_charpos,
21792 &endptr, it->base_face_id, 0);
21793 face = FACE_FROM_ID (it->f, it->face_id);
21794 it->face_box_p = face->box != FACE_NO_BOX;
21797 /* Set max_x to the maximum allowed X position. Don't let it go
21798 beyond the right edge of the window. */
21799 if (max_x <= 0)
21800 max_x = it->last_visible_x;
21801 else
21802 max_x = min (max_x, it->last_visible_x);
21804 /* Skip over display elements that are not visible. because IT->w is
21805 hscrolled. */
21806 if (it->current_x < it->first_visible_x)
21807 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21808 MOVE_TO_POS | MOVE_TO_X);
21810 row->ascent = it->max_ascent;
21811 row->height = it->max_ascent + it->max_descent;
21812 row->phys_ascent = it->max_phys_ascent;
21813 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21814 row->extra_line_spacing = it->max_extra_line_spacing;
21816 if (STRINGP (it->string))
21817 it_charpos = IT_STRING_CHARPOS (*it);
21818 else
21819 it_charpos = IT_CHARPOS (*it);
21821 /* This condition is for the case that we are called with current_x
21822 past last_visible_x. */
21823 while (it->current_x < max_x)
21825 int x_before, x, n_glyphs_before, i, nglyphs;
21827 /* Get the next display element. */
21828 if (!get_next_display_element (it))
21829 break;
21831 /* Produce glyphs. */
21832 x_before = it->current_x;
21833 n_glyphs_before = row->used[TEXT_AREA];
21834 PRODUCE_GLYPHS (it);
21836 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21837 i = 0;
21838 x = x_before;
21839 while (i < nglyphs)
21841 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21843 if (it->line_wrap != TRUNCATE
21844 && x + glyph->pixel_width > max_x)
21846 /* End of continued line or max_x reached. */
21847 if (CHAR_GLYPH_PADDING_P (*glyph))
21849 /* A wide character is unbreakable. */
21850 if (row->reversed_p)
21851 unproduce_glyphs (it, row->used[TEXT_AREA]
21852 - n_glyphs_before);
21853 row->used[TEXT_AREA] = n_glyphs_before;
21854 it->current_x = x_before;
21856 else
21858 if (row->reversed_p)
21859 unproduce_glyphs (it, row->used[TEXT_AREA]
21860 - (n_glyphs_before + i));
21861 row->used[TEXT_AREA] = n_glyphs_before + i;
21862 it->current_x = x;
21864 break;
21866 else if (x + glyph->pixel_width >= it->first_visible_x)
21868 /* Glyph is at least partially visible. */
21869 ++it->hpos;
21870 if (x < it->first_visible_x)
21871 row->x = x - it->first_visible_x;
21873 else
21875 /* Glyph is off the left margin of the display area.
21876 Should not happen. */
21877 emacs_abort ();
21880 row->ascent = max (row->ascent, it->max_ascent);
21881 row->height = max (row->height, it->max_ascent + it->max_descent);
21882 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21883 row->phys_height = max (row->phys_height,
21884 it->max_phys_ascent + it->max_phys_descent);
21885 row->extra_line_spacing = max (row->extra_line_spacing,
21886 it->max_extra_line_spacing);
21887 x += glyph->pixel_width;
21888 ++i;
21891 /* Stop if max_x reached. */
21892 if (i < nglyphs)
21893 break;
21895 /* Stop at line ends. */
21896 if (ITERATOR_AT_END_OF_LINE_P (it))
21898 it->continuation_lines_width = 0;
21899 break;
21902 set_iterator_to_next (it, 1);
21903 if (STRINGP (it->string))
21904 it_charpos = IT_STRING_CHARPOS (*it);
21905 else
21906 it_charpos = IT_CHARPOS (*it);
21908 /* Stop if truncating at the right edge. */
21909 if (it->line_wrap == TRUNCATE
21910 && it->current_x >= it->last_visible_x)
21912 /* Add truncation mark, but don't do it if the line is
21913 truncated at a padding space. */
21914 if (it_charpos < it->string_nchars)
21916 if (!FRAME_WINDOW_P (it->f))
21918 int ii, n;
21920 if (it->current_x > it->last_visible_x)
21922 if (!row->reversed_p)
21924 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21925 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21926 break;
21928 else
21930 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21931 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21932 break;
21933 unproduce_glyphs (it, ii + 1);
21934 ii = row->used[TEXT_AREA] - (ii + 1);
21936 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21938 row->used[TEXT_AREA] = ii;
21939 produce_special_glyphs (it, IT_TRUNCATION);
21942 produce_special_glyphs (it, IT_TRUNCATION);
21944 row->truncated_on_right_p = 1;
21946 break;
21950 /* Maybe insert a truncation at the left. */
21951 if (it->first_visible_x
21952 && it_charpos > 0)
21954 if (!FRAME_WINDOW_P (it->f)
21955 || (row->reversed_p
21956 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21957 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21958 insert_left_trunc_glyphs (it);
21959 row->truncated_on_left_p = 1;
21962 it->face_id = saved_face_id;
21964 /* Value is number of columns displayed. */
21965 return it->hpos - hpos_at_start;
21970 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21971 appears as an element of LIST or as the car of an element of LIST.
21972 If PROPVAL is a list, compare each element against LIST in that
21973 way, and return 1/2 if any element of PROPVAL is found in LIST.
21974 Otherwise return 0. This function cannot quit.
21975 The return value is 2 if the text is invisible but with an ellipsis
21976 and 1 if it's invisible and without an ellipsis. */
21979 invisible_p (register Lisp_Object propval, Lisp_Object list)
21981 register Lisp_Object tail, proptail;
21983 for (tail = list; CONSP (tail); tail = XCDR (tail))
21985 register Lisp_Object tem;
21986 tem = XCAR (tail);
21987 if (EQ (propval, tem))
21988 return 1;
21989 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21990 return NILP (XCDR (tem)) ? 1 : 2;
21993 if (CONSP (propval))
21995 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21997 Lisp_Object propelt;
21998 propelt = XCAR (proptail);
21999 for (tail = list; CONSP (tail); tail = XCDR (tail))
22001 register Lisp_Object tem;
22002 tem = XCAR (tail);
22003 if (EQ (propelt, tem))
22004 return 1;
22005 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22006 return NILP (XCDR (tem)) ? 1 : 2;
22011 return 0;
22014 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22015 doc: /* Non-nil if the property makes the text invisible.
22016 POS-OR-PROP can be a marker or number, in which case it is taken to be
22017 a position in the current buffer and the value of the `invisible' property
22018 is checked; or it can be some other value, which is then presumed to be the
22019 value of the `invisible' property of the text of interest.
22020 The non-nil value returned can be t for truly invisible text or something
22021 else if the text is replaced by an ellipsis. */)
22022 (Lisp_Object pos_or_prop)
22024 Lisp_Object prop
22025 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22026 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22027 : pos_or_prop);
22028 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22029 return (invis == 0 ? Qnil
22030 : invis == 1 ? Qt
22031 : make_number (invis));
22034 /* Calculate a width or height in pixels from a specification using
22035 the following elements:
22037 SPEC ::=
22038 NUM - a (fractional) multiple of the default font width/height
22039 (NUM) - specifies exactly NUM pixels
22040 UNIT - a fixed number of pixels, see below.
22041 ELEMENT - size of a display element in pixels, see below.
22042 (NUM . SPEC) - equals NUM * SPEC
22043 (+ SPEC SPEC ...) - add pixel values
22044 (- SPEC SPEC ...) - subtract pixel values
22045 (- SPEC) - negate pixel value
22047 NUM ::=
22048 INT or FLOAT - a number constant
22049 SYMBOL - use symbol's (buffer local) variable binding.
22051 UNIT ::=
22052 in - pixels per inch *)
22053 mm - pixels per 1/1000 meter *)
22054 cm - pixels per 1/100 meter *)
22055 width - width of current font in pixels.
22056 height - height of current font in pixels.
22058 *) using the ratio(s) defined in display-pixels-per-inch.
22060 ELEMENT ::=
22062 left-fringe - left fringe width in pixels
22063 right-fringe - right fringe width in pixels
22065 left-margin - left margin width in pixels
22066 right-margin - right margin width in pixels
22068 scroll-bar - scroll-bar area width in pixels
22070 Examples:
22072 Pixels corresponding to 5 inches:
22073 (5 . in)
22075 Total width of non-text areas on left side of window (if scroll-bar is on left):
22076 '(space :width (+ left-fringe left-margin scroll-bar))
22078 Align to first text column (in header line):
22079 '(space :align-to 0)
22081 Align to middle of text area minus half the width of variable `my-image'
22082 containing a loaded image:
22083 '(space :align-to (0.5 . (- text my-image)))
22085 Width of left margin minus width of 1 character in the default font:
22086 '(space :width (- left-margin 1))
22088 Width of left margin minus width of 2 characters in the current font:
22089 '(space :width (- left-margin (2 . width)))
22091 Center 1 character over left-margin (in header line):
22092 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22094 Different ways to express width of left fringe plus left margin minus one pixel:
22095 '(space :width (- (+ left-fringe left-margin) (1)))
22096 '(space :width (+ left-fringe left-margin (- (1))))
22097 '(space :width (+ left-fringe left-margin (-1)))
22101 static int
22102 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22103 struct font *font, int width_p, int *align_to)
22105 double pixels;
22107 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22108 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22110 if (NILP (prop))
22111 return OK_PIXELS (0);
22113 eassert (FRAME_LIVE_P (it->f));
22115 if (SYMBOLP (prop))
22117 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22119 char *unit = SSDATA (SYMBOL_NAME (prop));
22121 if (unit[0] == 'i' && unit[1] == 'n')
22122 pixels = 1.0;
22123 else if (unit[0] == 'm' && unit[1] == 'm')
22124 pixels = 25.4;
22125 else if (unit[0] == 'c' && unit[1] == 'm')
22126 pixels = 2.54;
22127 else
22128 pixels = 0;
22129 if (pixels > 0)
22131 double ppi = (width_p ? FRAME_RES_X (it->f)
22132 : FRAME_RES_Y (it->f));
22134 if (ppi > 0)
22135 return OK_PIXELS (ppi / pixels);
22136 return 0;
22140 #ifdef HAVE_WINDOW_SYSTEM
22141 if (EQ (prop, Qheight))
22142 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22143 if (EQ (prop, Qwidth))
22144 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22145 #else
22146 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22147 return OK_PIXELS (1);
22148 #endif
22150 if (EQ (prop, Qtext))
22151 return OK_PIXELS (width_p
22152 ? window_box_width (it->w, TEXT_AREA)
22153 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22155 if (align_to && *align_to < 0)
22157 *res = 0;
22158 if (EQ (prop, Qleft))
22159 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22160 if (EQ (prop, Qright))
22161 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22162 if (EQ (prop, Qcenter))
22163 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22164 + window_box_width (it->w, TEXT_AREA) / 2);
22165 if (EQ (prop, Qleft_fringe))
22166 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22167 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22168 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22169 if (EQ (prop, Qright_fringe))
22170 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22171 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22172 : window_box_right_offset (it->w, TEXT_AREA));
22173 if (EQ (prop, Qleft_margin))
22174 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22175 if (EQ (prop, Qright_margin))
22176 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22177 if (EQ (prop, Qscroll_bar))
22178 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22180 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22181 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22182 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22183 : 0)));
22185 else
22187 if (EQ (prop, Qleft_fringe))
22188 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22189 if (EQ (prop, Qright_fringe))
22190 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22191 if (EQ (prop, Qleft_margin))
22192 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22193 if (EQ (prop, Qright_margin))
22194 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22195 if (EQ (prop, Qscroll_bar))
22196 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22199 prop = buffer_local_value_1 (prop, it->w->contents);
22200 if (EQ (prop, Qunbound))
22201 prop = Qnil;
22204 if (INTEGERP (prop) || FLOATP (prop))
22206 int base_unit = (width_p
22207 ? FRAME_COLUMN_WIDTH (it->f)
22208 : FRAME_LINE_HEIGHT (it->f));
22209 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22212 if (CONSP (prop))
22214 Lisp_Object car = XCAR (prop);
22215 Lisp_Object cdr = XCDR (prop);
22217 if (SYMBOLP (car))
22219 #ifdef HAVE_WINDOW_SYSTEM
22220 if (FRAME_WINDOW_P (it->f)
22221 && valid_image_p (prop))
22223 ptrdiff_t id = lookup_image (it->f, prop);
22224 struct image *img = IMAGE_FROM_ID (it->f, id);
22226 return OK_PIXELS (width_p ? img->width : img->height);
22228 #endif
22229 if (EQ (car, Qplus) || EQ (car, Qminus))
22231 int first = 1;
22232 double px;
22234 pixels = 0;
22235 while (CONSP (cdr))
22237 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22238 font, width_p, align_to))
22239 return 0;
22240 if (first)
22241 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22242 else
22243 pixels += px;
22244 cdr = XCDR (cdr);
22246 if (EQ (car, Qminus))
22247 pixels = -pixels;
22248 return OK_PIXELS (pixels);
22251 car = buffer_local_value_1 (car, it->w->contents);
22252 if (EQ (car, Qunbound))
22253 car = Qnil;
22256 if (INTEGERP (car) || FLOATP (car))
22258 double fact;
22259 pixels = XFLOATINT (car);
22260 if (NILP (cdr))
22261 return OK_PIXELS (pixels);
22262 if (calc_pixel_width_or_height (&fact, it, cdr,
22263 font, width_p, align_to))
22264 return OK_PIXELS (pixels * fact);
22265 return 0;
22268 return 0;
22271 return 0;
22275 /***********************************************************************
22276 Glyph Display
22277 ***********************************************************************/
22279 #ifdef HAVE_WINDOW_SYSTEM
22281 #ifdef GLYPH_DEBUG
22283 void
22284 dump_glyph_string (struct glyph_string *s)
22286 fprintf (stderr, "glyph string\n");
22287 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22288 s->x, s->y, s->width, s->height);
22289 fprintf (stderr, " ybase = %d\n", s->ybase);
22290 fprintf (stderr, " hl = %d\n", s->hl);
22291 fprintf (stderr, " left overhang = %d, right = %d\n",
22292 s->left_overhang, s->right_overhang);
22293 fprintf (stderr, " nchars = %d\n", s->nchars);
22294 fprintf (stderr, " extends to end of line = %d\n",
22295 s->extends_to_end_of_line_p);
22296 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22297 fprintf (stderr, " bg width = %d\n", s->background_width);
22300 #endif /* GLYPH_DEBUG */
22302 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22303 of XChar2b structures for S; it can't be allocated in
22304 init_glyph_string because it must be allocated via `alloca'. W
22305 is the window on which S is drawn. ROW and AREA are the glyph row
22306 and area within the row from which S is constructed. START is the
22307 index of the first glyph structure covered by S. HL is a
22308 face-override for drawing S. */
22310 #ifdef HAVE_NTGUI
22311 #define OPTIONAL_HDC(hdc) HDC hdc,
22312 #define DECLARE_HDC(hdc) HDC hdc;
22313 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22314 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22315 #endif
22317 #ifndef OPTIONAL_HDC
22318 #define OPTIONAL_HDC(hdc)
22319 #define DECLARE_HDC(hdc)
22320 #define ALLOCATE_HDC(hdc, f)
22321 #define RELEASE_HDC(hdc, f)
22322 #endif
22324 static void
22325 init_glyph_string (struct glyph_string *s,
22326 OPTIONAL_HDC (hdc)
22327 XChar2b *char2b, struct window *w, struct glyph_row *row,
22328 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22330 memset (s, 0, sizeof *s);
22331 s->w = w;
22332 s->f = XFRAME (w->frame);
22333 #ifdef HAVE_NTGUI
22334 s->hdc = hdc;
22335 #endif
22336 s->display = FRAME_X_DISPLAY (s->f);
22337 s->window = FRAME_X_WINDOW (s->f);
22338 s->char2b = char2b;
22339 s->hl = hl;
22340 s->row = row;
22341 s->area = area;
22342 s->first_glyph = row->glyphs[area] + start;
22343 s->height = row->height;
22344 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22345 s->ybase = s->y + row->ascent;
22349 /* Append the list of glyph strings with head H and tail T to the list
22350 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22352 static void
22353 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22354 struct glyph_string *h, struct glyph_string *t)
22356 if (h)
22358 if (*head)
22359 (*tail)->next = h;
22360 else
22361 *head = h;
22362 h->prev = *tail;
22363 *tail = t;
22368 /* Prepend the list of glyph strings with head H and tail T to the
22369 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22370 result. */
22372 static void
22373 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22374 struct glyph_string *h, struct glyph_string *t)
22376 if (h)
22378 if (*head)
22379 (*head)->prev = t;
22380 else
22381 *tail = t;
22382 t->next = *head;
22383 *head = h;
22388 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22389 Set *HEAD and *TAIL to the resulting list. */
22391 static void
22392 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22393 struct glyph_string *s)
22395 s->next = s->prev = NULL;
22396 append_glyph_string_lists (head, tail, s, s);
22400 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22401 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22402 make sure that X resources for the face returned are allocated.
22403 Value is a pointer to a realized face that is ready for display if
22404 DISPLAY_P is non-zero. */
22406 static struct face *
22407 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22408 XChar2b *char2b, int display_p)
22410 struct face *face = FACE_FROM_ID (f, face_id);
22411 unsigned code = 0;
22413 if (face->font)
22415 code = face->font->driver->encode_char (face->font, c);
22417 if (code == FONT_INVALID_CODE)
22418 code = 0;
22420 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22422 /* Make sure X resources of the face are allocated. */
22423 #ifdef HAVE_X_WINDOWS
22424 if (display_p)
22425 #endif
22427 eassert (face != NULL);
22428 PREPARE_FACE_FOR_DISPLAY (f, face);
22431 return face;
22435 /* Get face and two-byte form of character glyph GLYPH on frame F.
22436 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22437 a pointer to a realized face that is ready for display. */
22439 static struct face *
22440 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22441 XChar2b *char2b, int *two_byte_p)
22443 struct face *face;
22444 unsigned code = 0;
22446 eassert (glyph->type == CHAR_GLYPH);
22447 face = FACE_FROM_ID (f, glyph->face_id);
22449 /* Make sure X resources of the face are allocated. */
22450 eassert (face != NULL);
22451 PREPARE_FACE_FOR_DISPLAY (f, face);
22453 if (two_byte_p)
22454 *two_byte_p = 0;
22456 if (face->font)
22458 if (CHAR_BYTE8_P (glyph->u.ch))
22459 code = CHAR_TO_BYTE8 (glyph->u.ch);
22460 else
22461 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22463 if (code == FONT_INVALID_CODE)
22464 code = 0;
22467 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22468 return face;
22472 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22473 Return 1 if FONT has a glyph for C, otherwise return 0. */
22475 static int
22476 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22478 unsigned code;
22480 if (CHAR_BYTE8_P (c))
22481 code = CHAR_TO_BYTE8 (c);
22482 else
22483 code = font->driver->encode_char (font, c);
22485 if (code == FONT_INVALID_CODE)
22486 return 0;
22487 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22488 return 1;
22492 /* Fill glyph string S with composition components specified by S->cmp.
22494 BASE_FACE is the base face of the composition.
22495 S->cmp_from is the index of the first component for S.
22497 OVERLAPS non-zero means S should draw the foreground only, and use
22498 its physical height for clipping. See also draw_glyphs.
22500 Value is the index of a component not in S. */
22502 static int
22503 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22504 int overlaps)
22506 int i;
22507 /* For all glyphs of this composition, starting at the offset
22508 S->cmp_from, until we reach the end of the definition or encounter a
22509 glyph that requires the different face, add it to S. */
22510 struct face *face;
22512 eassert (s);
22514 s->for_overlaps = overlaps;
22515 s->face = NULL;
22516 s->font = NULL;
22517 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22519 int c = COMPOSITION_GLYPH (s->cmp, i);
22521 /* TAB in a composition means display glyphs with padding space
22522 on the left or right. */
22523 if (c != '\t')
22525 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22526 -1, Qnil);
22528 face = get_char_face_and_encoding (s->f, c, face_id,
22529 s->char2b + i, 1);
22530 if (face)
22532 if (! s->face)
22534 s->face = face;
22535 s->font = s->face->font;
22537 else if (s->face != face)
22538 break;
22541 ++s->nchars;
22543 s->cmp_to = i;
22545 if (s->face == NULL)
22547 s->face = base_face->ascii_face;
22548 s->font = s->face->font;
22551 /* All glyph strings for the same composition has the same width,
22552 i.e. the width set for the first component of the composition. */
22553 s->width = s->first_glyph->pixel_width;
22555 /* If the specified font could not be loaded, use the frame's
22556 default font, but record the fact that we couldn't load it in
22557 the glyph string so that we can draw rectangles for the
22558 characters of the glyph string. */
22559 if (s->font == NULL)
22561 s->font_not_found_p = 1;
22562 s->font = FRAME_FONT (s->f);
22565 /* Adjust base line for subscript/superscript text. */
22566 s->ybase += s->first_glyph->voffset;
22568 /* This glyph string must always be drawn with 16-bit functions. */
22569 s->two_byte_p = 1;
22571 return s->cmp_to;
22574 static int
22575 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22576 int start, int end, int overlaps)
22578 struct glyph *glyph, *last;
22579 Lisp_Object lgstring;
22580 int i;
22582 s->for_overlaps = overlaps;
22583 glyph = s->row->glyphs[s->area] + start;
22584 last = s->row->glyphs[s->area] + end;
22585 s->cmp_id = glyph->u.cmp.id;
22586 s->cmp_from = glyph->slice.cmp.from;
22587 s->cmp_to = glyph->slice.cmp.to + 1;
22588 s->face = FACE_FROM_ID (s->f, face_id);
22589 lgstring = composition_gstring_from_id (s->cmp_id);
22590 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22591 glyph++;
22592 while (glyph < last
22593 && glyph->u.cmp.automatic
22594 && glyph->u.cmp.id == s->cmp_id
22595 && s->cmp_to == glyph->slice.cmp.from)
22596 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22598 for (i = s->cmp_from; i < s->cmp_to; i++)
22600 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22601 unsigned code = LGLYPH_CODE (lglyph);
22603 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22605 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22606 return glyph - s->row->glyphs[s->area];
22610 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22611 See the comment of fill_glyph_string for arguments.
22612 Value is the index of the first glyph not in S. */
22615 static int
22616 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22617 int start, int end, int overlaps)
22619 struct glyph *glyph, *last;
22620 int voffset;
22622 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22623 s->for_overlaps = overlaps;
22624 glyph = s->row->glyphs[s->area] + start;
22625 last = s->row->glyphs[s->area] + end;
22626 voffset = glyph->voffset;
22627 s->face = FACE_FROM_ID (s->f, face_id);
22628 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22629 s->nchars = 1;
22630 s->width = glyph->pixel_width;
22631 glyph++;
22632 while (glyph < last
22633 && glyph->type == GLYPHLESS_GLYPH
22634 && glyph->voffset == voffset
22635 && glyph->face_id == face_id)
22637 s->nchars++;
22638 s->width += glyph->pixel_width;
22639 glyph++;
22641 s->ybase += voffset;
22642 return glyph - s->row->glyphs[s->area];
22646 /* Fill glyph string S from a sequence of character glyphs.
22648 FACE_ID is the face id of the string. START is the index of the
22649 first glyph to consider, END is the index of the last + 1.
22650 OVERLAPS non-zero means S should draw the foreground only, and use
22651 its physical height for clipping. See also draw_glyphs.
22653 Value is the index of the first glyph not in S. */
22655 static int
22656 fill_glyph_string (struct glyph_string *s, int face_id,
22657 int start, int end, int overlaps)
22659 struct glyph *glyph, *last;
22660 int voffset;
22661 int glyph_not_available_p;
22663 eassert (s->f == XFRAME (s->w->frame));
22664 eassert (s->nchars == 0);
22665 eassert (start >= 0 && end > start);
22667 s->for_overlaps = overlaps;
22668 glyph = s->row->glyphs[s->area] + start;
22669 last = s->row->glyphs[s->area] + end;
22670 voffset = glyph->voffset;
22671 s->padding_p = glyph->padding_p;
22672 glyph_not_available_p = glyph->glyph_not_available_p;
22674 while (glyph < last
22675 && glyph->type == CHAR_GLYPH
22676 && glyph->voffset == voffset
22677 /* Same face id implies same font, nowadays. */
22678 && glyph->face_id == face_id
22679 && glyph->glyph_not_available_p == glyph_not_available_p)
22681 int two_byte_p;
22683 s->face = get_glyph_face_and_encoding (s->f, glyph,
22684 s->char2b + s->nchars,
22685 &two_byte_p);
22686 s->two_byte_p = two_byte_p;
22687 ++s->nchars;
22688 eassert (s->nchars <= end - start);
22689 s->width += glyph->pixel_width;
22690 if (glyph++->padding_p != s->padding_p)
22691 break;
22694 s->font = s->face->font;
22696 /* If the specified font could not be loaded, use the frame's font,
22697 but record the fact that we couldn't load it in
22698 S->font_not_found_p so that we can draw rectangles for the
22699 characters of the glyph string. */
22700 if (s->font == NULL || glyph_not_available_p)
22702 s->font_not_found_p = 1;
22703 s->font = FRAME_FONT (s->f);
22706 /* Adjust base line for subscript/superscript text. */
22707 s->ybase += voffset;
22709 eassert (s->face && s->face->gc);
22710 return glyph - s->row->glyphs[s->area];
22714 /* Fill glyph string S from image glyph S->first_glyph. */
22716 static void
22717 fill_image_glyph_string (struct glyph_string *s)
22719 eassert (s->first_glyph->type == IMAGE_GLYPH);
22720 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22721 eassert (s->img);
22722 s->slice = s->first_glyph->slice.img;
22723 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22724 s->font = s->face->font;
22725 s->width = s->first_glyph->pixel_width;
22727 /* Adjust base line for subscript/superscript text. */
22728 s->ybase += s->first_glyph->voffset;
22732 /* Fill glyph string S from a sequence of stretch glyphs.
22734 START is the index of the first glyph to consider,
22735 END is the index of the last + 1.
22737 Value is the index of the first glyph not in S. */
22739 static int
22740 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22742 struct glyph *glyph, *last;
22743 int voffset, face_id;
22745 eassert (s->first_glyph->type == STRETCH_GLYPH);
22747 glyph = s->row->glyphs[s->area] + start;
22748 last = s->row->glyphs[s->area] + end;
22749 face_id = glyph->face_id;
22750 s->face = FACE_FROM_ID (s->f, face_id);
22751 s->font = s->face->font;
22752 s->width = glyph->pixel_width;
22753 s->nchars = 1;
22754 voffset = glyph->voffset;
22756 for (++glyph;
22757 (glyph < last
22758 && glyph->type == STRETCH_GLYPH
22759 && glyph->voffset == voffset
22760 && glyph->face_id == face_id);
22761 ++glyph)
22762 s->width += glyph->pixel_width;
22764 /* Adjust base line for subscript/superscript text. */
22765 s->ybase += voffset;
22767 /* The case that face->gc == 0 is handled when drawing the glyph
22768 string by calling PREPARE_FACE_FOR_DISPLAY. */
22769 eassert (s->face);
22770 return glyph - s->row->glyphs[s->area];
22773 static struct font_metrics *
22774 get_per_char_metric (struct font *font, XChar2b *char2b)
22776 static struct font_metrics metrics;
22777 unsigned code;
22779 if (! font)
22780 return NULL;
22781 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22782 if (code == FONT_INVALID_CODE)
22783 return NULL;
22784 font->driver->text_extents (font, &code, 1, &metrics);
22785 return &metrics;
22788 /* EXPORT for RIF:
22789 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22790 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22791 assumed to be zero. */
22793 void
22794 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22796 *left = *right = 0;
22798 if (glyph->type == CHAR_GLYPH)
22800 struct face *face;
22801 XChar2b char2b;
22802 struct font_metrics *pcm;
22804 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22805 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22807 if (pcm->rbearing > pcm->width)
22808 *right = pcm->rbearing - pcm->width;
22809 if (pcm->lbearing < 0)
22810 *left = -pcm->lbearing;
22813 else if (glyph->type == COMPOSITE_GLYPH)
22815 if (! glyph->u.cmp.automatic)
22817 struct composition *cmp = composition_table[glyph->u.cmp.id];
22819 if (cmp->rbearing > cmp->pixel_width)
22820 *right = cmp->rbearing - cmp->pixel_width;
22821 if (cmp->lbearing < 0)
22822 *left = - cmp->lbearing;
22824 else
22826 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22827 struct font_metrics metrics;
22829 composition_gstring_width (gstring, glyph->slice.cmp.from,
22830 glyph->slice.cmp.to + 1, &metrics);
22831 if (metrics.rbearing > metrics.width)
22832 *right = metrics.rbearing - metrics.width;
22833 if (metrics.lbearing < 0)
22834 *left = - metrics.lbearing;
22840 /* Return the index of the first glyph preceding glyph string S that
22841 is overwritten by S because of S's left overhang. Value is -1
22842 if no glyphs are overwritten. */
22844 static int
22845 left_overwritten (struct glyph_string *s)
22847 int k;
22849 if (s->left_overhang)
22851 int x = 0, i;
22852 struct glyph *glyphs = s->row->glyphs[s->area];
22853 int first = s->first_glyph - glyphs;
22855 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22856 x -= glyphs[i].pixel_width;
22858 k = i + 1;
22860 else
22861 k = -1;
22863 return k;
22867 /* Return the index of the first glyph preceding glyph string S that
22868 is overwriting S because of its right overhang. Value is -1 if no
22869 glyph in front of S overwrites S. */
22871 static int
22872 left_overwriting (struct glyph_string *s)
22874 int i, k, x;
22875 struct glyph *glyphs = s->row->glyphs[s->area];
22876 int first = s->first_glyph - glyphs;
22878 k = -1;
22879 x = 0;
22880 for (i = first - 1; i >= 0; --i)
22882 int left, right;
22883 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22884 if (x + right > 0)
22885 k = i;
22886 x -= glyphs[i].pixel_width;
22889 return k;
22893 /* Return the index of the last glyph following glyph string S that is
22894 overwritten by S because of S's right overhang. Value is -1 if
22895 no such glyph is found. */
22897 static int
22898 right_overwritten (struct glyph_string *s)
22900 int k = -1;
22902 if (s->right_overhang)
22904 int x = 0, i;
22905 struct glyph *glyphs = s->row->glyphs[s->area];
22906 int first = (s->first_glyph - glyphs
22907 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22908 int end = s->row->used[s->area];
22910 for (i = first; i < end && s->right_overhang > x; ++i)
22911 x += glyphs[i].pixel_width;
22913 k = i;
22916 return k;
22920 /* Return the index of the last glyph following glyph string S that
22921 overwrites S because of its left overhang. Value is negative
22922 if no such glyph is found. */
22924 static int
22925 right_overwriting (struct glyph_string *s)
22927 int i, k, x;
22928 int end = s->row->used[s->area];
22929 struct glyph *glyphs = s->row->glyphs[s->area];
22930 int first = (s->first_glyph - glyphs
22931 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22933 k = -1;
22934 x = 0;
22935 for (i = first; i < end; ++i)
22937 int left, right;
22938 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22939 if (x - left < 0)
22940 k = i;
22941 x += glyphs[i].pixel_width;
22944 return k;
22948 /* Set background width of glyph string S. START is the index of the
22949 first glyph following S. LAST_X is the right-most x-position + 1
22950 in the drawing area. */
22952 static void
22953 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22955 /* If the face of this glyph string has to be drawn to the end of
22956 the drawing area, set S->extends_to_end_of_line_p. */
22958 if (start == s->row->used[s->area]
22959 && s->area == TEXT_AREA
22960 && ((s->row->fill_line_p
22961 && (s->hl == DRAW_NORMAL_TEXT
22962 || s->hl == DRAW_IMAGE_RAISED
22963 || s->hl == DRAW_IMAGE_SUNKEN))
22964 || s->hl == DRAW_MOUSE_FACE))
22965 s->extends_to_end_of_line_p = 1;
22967 /* If S extends its face to the end of the line, set its
22968 background_width to the distance to the right edge of the drawing
22969 area. */
22970 if (s->extends_to_end_of_line_p)
22971 s->background_width = last_x - s->x + 1;
22972 else
22973 s->background_width = s->width;
22977 /* Compute overhangs and x-positions for glyph string S and its
22978 predecessors, or successors. X is the starting x-position for S.
22979 BACKWARD_P non-zero means process predecessors. */
22981 static void
22982 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22984 if (backward_p)
22986 while (s)
22988 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22989 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22990 x -= s->width;
22991 s->x = x;
22992 s = s->prev;
22995 else
22997 while (s)
22999 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23000 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23001 s->x = x;
23002 x += s->width;
23003 s = s->next;
23010 /* The following macros are only called from draw_glyphs below.
23011 They reference the following parameters of that function directly:
23012 `w', `row', `area', and `overlap_p'
23013 as well as the following local variables:
23014 `s', `f', and `hdc' (in W32) */
23016 #ifdef HAVE_NTGUI
23017 /* On W32, silently add local `hdc' variable to argument list of
23018 init_glyph_string. */
23019 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23020 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23021 #else
23022 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23023 init_glyph_string (s, char2b, w, row, area, start, hl)
23024 #endif
23026 /* Add a glyph string for a stretch glyph to the list of strings
23027 between HEAD and TAIL. START is the index of the stretch glyph in
23028 row area AREA of glyph row ROW. END is the index of the last glyph
23029 in that glyph row area. X is the current output position assigned
23030 to the new glyph string constructed. HL overrides that face of the
23031 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23032 is the right-most x-position of the drawing area. */
23034 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23035 and below -- keep them on one line. */
23036 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23037 do \
23039 s = alloca (sizeof *s); \
23040 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23041 START = fill_stretch_glyph_string (s, START, END); \
23042 append_glyph_string (&HEAD, &TAIL, s); \
23043 s->x = (X); \
23045 while (0)
23048 /* Add a glyph string for an image glyph to the list of strings
23049 between HEAD and TAIL. START is the index of the image glyph in
23050 row area AREA of glyph row ROW. END is the index of the last glyph
23051 in that glyph row area. X is the current output position assigned
23052 to the new glyph string constructed. HL overrides that face of the
23053 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23054 is the right-most x-position of the drawing area. */
23056 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23057 do \
23059 s = alloca (sizeof *s); \
23060 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23061 fill_image_glyph_string (s); \
23062 append_glyph_string (&HEAD, &TAIL, s); \
23063 ++START; \
23064 s->x = (X); \
23066 while (0)
23069 /* Add a glyph string for a sequence of character glyphs to the list
23070 of strings between HEAD and TAIL. START is the index of the first
23071 glyph in row area AREA of glyph row ROW that is part of the new
23072 glyph string. END is the index of the last glyph in that glyph row
23073 area. X is the current output position assigned to the new glyph
23074 string constructed. HL overrides that face of the glyph; e.g. it
23075 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23076 right-most x-position of the drawing area. */
23078 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23079 do \
23081 int face_id; \
23082 XChar2b *char2b; \
23084 face_id = (row)->glyphs[area][START].face_id; \
23086 s = alloca (sizeof *s); \
23087 char2b = alloca ((END - START) * sizeof *char2b); \
23088 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23089 append_glyph_string (&HEAD, &TAIL, s); \
23090 s->x = (X); \
23091 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23093 while (0)
23096 /* Add a glyph string for a composite sequence to the list of strings
23097 between HEAD and TAIL. START is the index of the first glyph in
23098 row area AREA of glyph row ROW that is part of the new glyph
23099 string. END is the index of the last glyph in that glyph row area.
23100 X is the current output position assigned to the new glyph string
23101 constructed. HL overrides that face of the glyph; e.g. it is
23102 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23103 x-position of the drawing area. */
23105 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23106 do { \
23107 int face_id = (row)->glyphs[area][START].face_id; \
23108 struct face *base_face = FACE_FROM_ID (f, face_id); \
23109 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23110 struct composition *cmp = composition_table[cmp_id]; \
23111 XChar2b *char2b; \
23112 struct glyph_string *first_s = NULL; \
23113 int n; \
23115 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23117 /* Make glyph_strings for each glyph sequence that is drawable by \
23118 the same face, and append them to HEAD/TAIL. */ \
23119 for (n = 0; n < cmp->glyph_len;) \
23121 s = alloca (sizeof *s); \
23122 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23123 append_glyph_string (&(HEAD), &(TAIL), s); \
23124 s->cmp = cmp; \
23125 s->cmp_from = n; \
23126 s->x = (X); \
23127 if (n == 0) \
23128 first_s = s; \
23129 n = fill_composite_glyph_string (s, base_face, overlaps); \
23132 ++START; \
23133 s = first_s; \
23134 } while (0)
23137 /* Add a glyph string for a glyph-string sequence to the list of strings
23138 between HEAD and TAIL. */
23140 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23141 do { \
23142 int face_id; \
23143 XChar2b *char2b; \
23144 Lisp_Object gstring; \
23146 face_id = (row)->glyphs[area][START].face_id; \
23147 gstring = (composition_gstring_from_id \
23148 ((row)->glyphs[area][START].u.cmp.id)); \
23149 s = alloca (sizeof *s); \
23150 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23151 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23152 append_glyph_string (&(HEAD), &(TAIL), s); \
23153 s->x = (X); \
23154 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23155 } while (0)
23158 /* Add a glyph string for a sequence of glyphless character's glyphs
23159 to the list of strings between HEAD and TAIL. The meanings of
23160 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23162 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23163 do \
23165 int face_id; \
23167 face_id = (row)->glyphs[area][START].face_id; \
23169 s = alloca (sizeof *s); \
23170 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23171 append_glyph_string (&HEAD, &TAIL, s); \
23172 s->x = (X); \
23173 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23174 overlaps); \
23176 while (0)
23179 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23180 of AREA of glyph row ROW on window W between indices START and END.
23181 HL overrides the face for drawing glyph strings, e.g. it is
23182 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23183 x-positions of the drawing area.
23185 This is an ugly monster macro construct because we must use alloca
23186 to allocate glyph strings (because draw_glyphs can be called
23187 asynchronously). */
23189 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23190 do \
23192 HEAD = TAIL = NULL; \
23193 while (START < END) \
23195 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23196 switch (first_glyph->type) \
23198 case CHAR_GLYPH: \
23199 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23200 HL, X, LAST_X); \
23201 break; \
23203 case COMPOSITE_GLYPH: \
23204 if (first_glyph->u.cmp.automatic) \
23205 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23206 HL, X, LAST_X); \
23207 else \
23208 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23209 HL, X, LAST_X); \
23210 break; \
23212 case STRETCH_GLYPH: \
23213 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23214 HL, X, LAST_X); \
23215 break; \
23217 case IMAGE_GLYPH: \
23218 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23219 HL, X, LAST_X); \
23220 break; \
23222 case GLYPHLESS_GLYPH: \
23223 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23224 HL, X, LAST_X); \
23225 break; \
23227 default: \
23228 emacs_abort (); \
23231 if (s) \
23233 set_glyph_string_background_width (s, START, LAST_X); \
23234 (X) += s->width; \
23237 } while (0)
23240 /* Draw glyphs between START and END in AREA of ROW on window W,
23241 starting at x-position X. X is relative to AREA in W. HL is a
23242 face-override with the following meaning:
23244 DRAW_NORMAL_TEXT draw normally
23245 DRAW_CURSOR draw in cursor face
23246 DRAW_MOUSE_FACE draw in mouse face.
23247 DRAW_INVERSE_VIDEO draw in mode line face
23248 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23249 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23251 If OVERLAPS is non-zero, draw only the foreground of characters and
23252 clip to the physical height of ROW. Non-zero value also defines
23253 the overlapping part to be drawn:
23255 OVERLAPS_PRED overlap with preceding rows
23256 OVERLAPS_SUCC overlap with succeeding rows
23257 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23258 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23260 Value is the x-position reached, relative to AREA of W. */
23262 static int
23263 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23264 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23265 enum draw_glyphs_face hl, int overlaps)
23267 struct glyph_string *head, *tail;
23268 struct glyph_string *s;
23269 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23270 int i, j, x_reached, last_x, area_left = 0;
23271 struct frame *f = XFRAME (WINDOW_FRAME (w));
23272 DECLARE_HDC (hdc);
23274 ALLOCATE_HDC (hdc, f);
23276 /* Let's rather be paranoid than getting a SEGV. */
23277 end = min (end, row->used[area]);
23278 start = clip_to_bounds (0, start, end);
23280 /* Translate X to frame coordinates. Set last_x to the right
23281 end of the drawing area. */
23282 if (row->full_width_p)
23284 /* X is relative to the left edge of W, without scroll bars
23285 or fringes. */
23286 area_left = WINDOW_LEFT_EDGE_X (w);
23287 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23289 else
23291 area_left = window_box_left (w, area);
23292 last_x = area_left + window_box_width (w, area);
23294 x += area_left;
23296 /* Build a doubly-linked list of glyph_string structures between
23297 head and tail from what we have to draw. Note that the macro
23298 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23299 the reason we use a separate variable `i'. */
23300 i = start;
23301 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23302 if (tail)
23303 x_reached = tail->x + tail->background_width;
23304 else
23305 x_reached = x;
23307 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23308 the row, redraw some glyphs in front or following the glyph
23309 strings built above. */
23310 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23312 struct glyph_string *h, *t;
23313 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23314 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23315 int check_mouse_face = 0;
23316 int dummy_x = 0;
23318 /* If mouse highlighting is on, we may need to draw adjacent
23319 glyphs using mouse-face highlighting. */
23320 if (area == TEXT_AREA && row->mouse_face_p
23321 && hlinfo->mouse_face_beg_row >= 0
23322 && hlinfo->mouse_face_end_row >= 0)
23324 struct glyph_row *mouse_beg_row, *mouse_end_row;
23326 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23327 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23329 if (row >= mouse_beg_row && row <= mouse_end_row)
23331 check_mouse_face = 1;
23332 mouse_beg_col = (row == mouse_beg_row)
23333 ? hlinfo->mouse_face_beg_col : 0;
23334 mouse_end_col = (row == mouse_end_row)
23335 ? hlinfo->mouse_face_end_col
23336 : row->used[TEXT_AREA];
23340 /* Compute overhangs for all glyph strings. */
23341 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23342 for (s = head; s; s = s->next)
23343 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23345 /* Prepend glyph strings for glyphs in front of the first glyph
23346 string that are overwritten because of the first glyph
23347 string's left overhang. The background of all strings
23348 prepended must be drawn because the first glyph string
23349 draws over it. */
23350 i = left_overwritten (head);
23351 if (i >= 0)
23353 enum draw_glyphs_face overlap_hl;
23355 /* If this row contains mouse highlighting, attempt to draw
23356 the overlapped glyphs with the correct highlight. This
23357 code fails if the overlap encompasses more than one glyph
23358 and mouse-highlight spans only some of these glyphs.
23359 However, making it work perfectly involves a lot more
23360 code, and I don't know if the pathological case occurs in
23361 practice, so we'll stick to this for now. --- cyd */
23362 if (check_mouse_face
23363 && mouse_beg_col < start && mouse_end_col > i)
23364 overlap_hl = DRAW_MOUSE_FACE;
23365 else
23366 overlap_hl = DRAW_NORMAL_TEXT;
23368 j = i;
23369 BUILD_GLYPH_STRINGS (j, start, h, t,
23370 overlap_hl, dummy_x, last_x);
23371 start = i;
23372 compute_overhangs_and_x (t, head->x, 1);
23373 prepend_glyph_string_lists (&head, &tail, h, t);
23374 clip_head = head;
23377 /* Prepend glyph strings for glyphs in front of the first glyph
23378 string that overwrite that glyph string because of their
23379 right overhang. For these strings, only the foreground must
23380 be drawn, because it draws over the glyph string at `head'.
23381 The background must not be drawn because this would overwrite
23382 right overhangs of preceding glyphs for which no glyph
23383 strings exist. */
23384 i = left_overwriting (head);
23385 if (i >= 0)
23387 enum draw_glyphs_face overlap_hl;
23389 if (check_mouse_face
23390 && mouse_beg_col < start && mouse_end_col > i)
23391 overlap_hl = DRAW_MOUSE_FACE;
23392 else
23393 overlap_hl = DRAW_NORMAL_TEXT;
23395 clip_head = head;
23396 BUILD_GLYPH_STRINGS (i, start, h, t,
23397 overlap_hl, dummy_x, last_x);
23398 for (s = h; s; s = s->next)
23399 s->background_filled_p = 1;
23400 compute_overhangs_and_x (t, head->x, 1);
23401 prepend_glyph_string_lists (&head, &tail, h, t);
23404 /* Append glyphs strings for glyphs following the last glyph
23405 string tail that are overwritten by tail. The background of
23406 these strings has to be drawn because tail's foreground draws
23407 over it. */
23408 i = right_overwritten (tail);
23409 if (i >= 0)
23411 enum draw_glyphs_face overlap_hl;
23413 if (check_mouse_face
23414 && mouse_beg_col < i && mouse_end_col > end)
23415 overlap_hl = DRAW_MOUSE_FACE;
23416 else
23417 overlap_hl = DRAW_NORMAL_TEXT;
23419 BUILD_GLYPH_STRINGS (end, i, h, t,
23420 overlap_hl, x, last_x);
23421 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23422 we don't have `end = i;' here. */
23423 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23424 append_glyph_string_lists (&head, &tail, h, t);
23425 clip_tail = tail;
23428 /* Append glyph strings for glyphs following the last glyph
23429 string tail that overwrite tail. The foreground of such
23430 glyphs has to be drawn because it writes into the background
23431 of tail. The background must not be drawn because it could
23432 paint over the foreground of following glyphs. */
23433 i = right_overwriting (tail);
23434 if (i >= 0)
23436 enum draw_glyphs_face overlap_hl;
23437 if (check_mouse_face
23438 && mouse_beg_col < i && mouse_end_col > end)
23439 overlap_hl = DRAW_MOUSE_FACE;
23440 else
23441 overlap_hl = DRAW_NORMAL_TEXT;
23443 clip_tail = tail;
23444 i++; /* We must include the Ith glyph. */
23445 BUILD_GLYPH_STRINGS (end, i, h, t,
23446 overlap_hl, x, last_x);
23447 for (s = h; s; s = s->next)
23448 s->background_filled_p = 1;
23449 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23450 append_glyph_string_lists (&head, &tail, h, t);
23452 if (clip_head || clip_tail)
23453 for (s = head; s; s = s->next)
23455 s->clip_head = clip_head;
23456 s->clip_tail = clip_tail;
23460 /* Draw all strings. */
23461 for (s = head; s; s = s->next)
23462 FRAME_RIF (f)->draw_glyph_string (s);
23464 #ifndef HAVE_NS
23465 /* When focus a sole frame and move horizontally, this sets on_p to 0
23466 causing a failure to erase prev cursor position. */
23467 if (area == TEXT_AREA
23468 && !row->full_width_p
23469 /* When drawing overlapping rows, only the glyph strings'
23470 foreground is drawn, which doesn't erase a cursor
23471 completely. */
23472 && !overlaps)
23474 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23475 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23476 : (tail ? tail->x + tail->background_width : x));
23477 x0 -= area_left;
23478 x1 -= area_left;
23480 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23481 row->y, MATRIX_ROW_BOTTOM_Y (row));
23483 #endif
23485 /* Value is the x-position up to which drawn, relative to AREA of W.
23486 This doesn't include parts drawn because of overhangs. */
23487 if (row->full_width_p)
23488 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23489 else
23490 x_reached -= area_left;
23492 RELEASE_HDC (hdc, f);
23494 return x_reached;
23497 /* Expand row matrix if too narrow. Don't expand if area
23498 is not present. */
23500 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23502 if (!fonts_changed_p \
23503 && (it->glyph_row->glyphs[area] \
23504 < it->glyph_row->glyphs[area + 1])) \
23506 it->w->ncols_scale_factor++; \
23507 fonts_changed_p = 1; \
23511 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23512 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23514 static void
23515 append_glyph (struct it *it)
23517 struct glyph *glyph;
23518 enum glyph_row_area area = it->area;
23520 eassert (it->glyph_row);
23521 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23523 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23524 if (glyph < it->glyph_row->glyphs[area + 1])
23526 /* If the glyph row is reversed, we need to prepend the glyph
23527 rather than append it. */
23528 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23530 struct glyph *g;
23532 /* Make room for the additional glyph. */
23533 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23534 g[1] = *g;
23535 glyph = it->glyph_row->glyphs[area];
23537 glyph->charpos = CHARPOS (it->position);
23538 glyph->object = it->object;
23539 if (it->pixel_width > 0)
23541 glyph->pixel_width = it->pixel_width;
23542 glyph->padding_p = 0;
23544 else
23546 /* Assure at least 1-pixel width. Otherwise, cursor can't
23547 be displayed correctly. */
23548 glyph->pixel_width = 1;
23549 glyph->padding_p = 1;
23551 glyph->ascent = it->ascent;
23552 glyph->descent = it->descent;
23553 glyph->voffset = it->voffset;
23554 glyph->type = CHAR_GLYPH;
23555 glyph->avoid_cursor_p = it->avoid_cursor_p;
23556 glyph->multibyte_p = it->multibyte_p;
23557 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23559 /* In R2L rows, the left and the right box edges need to be
23560 drawn in reverse direction. */
23561 glyph->right_box_line_p = it->start_of_box_run_p;
23562 glyph->left_box_line_p = it->end_of_box_run_p;
23564 else
23566 glyph->left_box_line_p = it->start_of_box_run_p;
23567 glyph->right_box_line_p = it->end_of_box_run_p;
23569 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23570 || it->phys_descent > it->descent);
23571 glyph->glyph_not_available_p = it->glyph_not_available_p;
23572 glyph->face_id = it->face_id;
23573 glyph->u.ch = it->char_to_display;
23574 glyph->slice.img = null_glyph_slice;
23575 glyph->font_type = FONT_TYPE_UNKNOWN;
23576 if (it->bidi_p)
23578 glyph->resolved_level = it->bidi_it.resolved_level;
23579 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23580 emacs_abort ();
23581 glyph->bidi_type = it->bidi_it.type;
23583 else
23585 glyph->resolved_level = 0;
23586 glyph->bidi_type = UNKNOWN_BT;
23588 ++it->glyph_row->used[area];
23590 else
23591 IT_EXPAND_MATRIX_WIDTH (it, area);
23594 /* Store one glyph for the composition IT->cmp_it.id in
23595 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23596 non-null. */
23598 static void
23599 append_composite_glyph (struct it *it)
23601 struct glyph *glyph;
23602 enum glyph_row_area area = it->area;
23604 eassert (it->glyph_row);
23606 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23607 if (glyph < it->glyph_row->glyphs[area + 1])
23609 /* If the glyph row is reversed, we need to prepend the glyph
23610 rather than append it. */
23611 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23613 struct glyph *g;
23615 /* Make room for the new glyph. */
23616 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23617 g[1] = *g;
23618 glyph = it->glyph_row->glyphs[it->area];
23620 glyph->charpos = it->cmp_it.charpos;
23621 glyph->object = it->object;
23622 glyph->pixel_width = it->pixel_width;
23623 glyph->ascent = it->ascent;
23624 glyph->descent = it->descent;
23625 glyph->voffset = it->voffset;
23626 glyph->type = COMPOSITE_GLYPH;
23627 if (it->cmp_it.ch < 0)
23629 glyph->u.cmp.automatic = 0;
23630 glyph->u.cmp.id = it->cmp_it.id;
23631 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23633 else
23635 glyph->u.cmp.automatic = 1;
23636 glyph->u.cmp.id = it->cmp_it.id;
23637 glyph->slice.cmp.from = it->cmp_it.from;
23638 glyph->slice.cmp.to = it->cmp_it.to - 1;
23640 glyph->avoid_cursor_p = it->avoid_cursor_p;
23641 glyph->multibyte_p = it->multibyte_p;
23642 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23644 /* In R2L rows, the left and the right box edges need to be
23645 drawn in reverse direction. */
23646 glyph->right_box_line_p = it->start_of_box_run_p;
23647 glyph->left_box_line_p = it->end_of_box_run_p;
23649 else
23651 glyph->left_box_line_p = it->start_of_box_run_p;
23652 glyph->right_box_line_p = it->end_of_box_run_p;
23654 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23655 || it->phys_descent > it->descent);
23656 glyph->padding_p = 0;
23657 glyph->glyph_not_available_p = 0;
23658 glyph->face_id = it->face_id;
23659 glyph->font_type = FONT_TYPE_UNKNOWN;
23660 if (it->bidi_p)
23662 glyph->resolved_level = it->bidi_it.resolved_level;
23663 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23664 emacs_abort ();
23665 glyph->bidi_type = it->bidi_it.type;
23667 ++it->glyph_row->used[area];
23669 else
23670 IT_EXPAND_MATRIX_WIDTH (it, area);
23674 /* Change IT->ascent and IT->height according to the setting of
23675 IT->voffset. */
23677 static void
23678 take_vertical_position_into_account (struct it *it)
23680 if (it->voffset)
23682 if (it->voffset < 0)
23683 /* Increase the ascent so that we can display the text higher
23684 in the line. */
23685 it->ascent -= it->voffset;
23686 else
23687 /* Increase the descent so that we can display the text lower
23688 in the line. */
23689 it->descent += it->voffset;
23694 /* Produce glyphs/get display metrics for the image IT is loaded with.
23695 See the description of struct display_iterator in dispextern.h for
23696 an overview of struct display_iterator. */
23698 static void
23699 produce_image_glyph (struct it *it)
23701 struct image *img;
23702 struct face *face;
23703 int glyph_ascent, crop;
23704 struct glyph_slice slice;
23706 eassert (it->what == IT_IMAGE);
23708 face = FACE_FROM_ID (it->f, it->face_id);
23709 eassert (face);
23710 /* Make sure X resources of the face is loaded. */
23711 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23713 if (it->image_id < 0)
23715 /* Fringe bitmap. */
23716 it->ascent = it->phys_ascent = 0;
23717 it->descent = it->phys_descent = 0;
23718 it->pixel_width = 0;
23719 it->nglyphs = 0;
23720 return;
23723 img = IMAGE_FROM_ID (it->f, it->image_id);
23724 eassert (img);
23725 /* Make sure X resources of the image is loaded. */
23726 prepare_image_for_display (it->f, img);
23728 slice.x = slice.y = 0;
23729 slice.width = img->width;
23730 slice.height = img->height;
23732 if (INTEGERP (it->slice.x))
23733 slice.x = XINT (it->slice.x);
23734 else if (FLOATP (it->slice.x))
23735 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23737 if (INTEGERP (it->slice.y))
23738 slice.y = XINT (it->slice.y);
23739 else if (FLOATP (it->slice.y))
23740 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23742 if (INTEGERP (it->slice.width))
23743 slice.width = XINT (it->slice.width);
23744 else if (FLOATP (it->slice.width))
23745 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23747 if (INTEGERP (it->slice.height))
23748 slice.height = XINT (it->slice.height);
23749 else if (FLOATP (it->slice.height))
23750 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23752 if (slice.x >= img->width)
23753 slice.x = img->width;
23754 if (slice.y >= img->height)
23755 slice.y = img->height;
23756 if (slice.x + slice.width >= img->width)
23757 slice.width = img->width - slice.x;
23758 if (slice.y + slice.height > img->height)
23759 slice.height = img->height - slice.y;
23761 if (slice.width == 0 || slice.height == 0)
23762 return;
23764 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23766 it->descent = slice.height - glyph_ascent;
23767 if (slice.y == 0)
23768 it->descent += img->vmargin;
23769 if (slice.y + slice.height == img->height)
23770 it->descent += img->vmargin;
23771 it->phys_descent = it->descent;
23773 it->pixel_width = slice.width;
23774 if (slice.x == 0)
23775 it->pixel_width += img->hmargin;
23776 if (slice.x + slice.width == img->width)
23777 it->pixel_width += img->hmargin;
23779 /* It's quite possible for images to have an ascent greater than
23780 their height, so don't get confused in that case. */
23781 if (it->descent < 0)
23782 it->descent = 0;
23784 it->nglyphs = 1;
23786 if (face->box != FACE_NO_BOX)
23788 if (face->box_line_width > 0)
23790 if (slice.y == 0)
23791 it->ascent += face->box_line_width;
23792 if (slice.y + slice.height == img->height)
23793 it->descent += face->box_line_width;
23796 if (it->start_of_box_run_p && slice.x == 0)
23797 it->pixel_width += eabs (face->box_line_width);
23798 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23799 it->pixel_width += eabs (face->box_line_width);
23802 take_vertical_position_into_account (it);
23804 /* Automatically crop wide image glyphs at right edge so we can
23805 draw the cursor on same display row. */
23806 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23807 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23809 it->pixel_width -= crop;
23810 slice.width -= crop;
23813 if (it->glyph_row)
23815 struct glyph *glyph;
23816 enum glyph_row_area area = it->area;
23818 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23819 if (glyph < it->glyph_row->glyphs[area + 1])
23821 glyph->charpos = CHARPOS (it->position);
23822 glyph->object = it->object;
23823 glyph->pixel_width = it->pixel_width;
23824 glyph->ascent = glyph_ascent;
23825 glyph->descent = it->descent;
23826 glyph->voffset = it->voffset;
23827 glyph->type = IMAGE_GLYPH;
23828 glyph->avoid_cursor_p = it->avoid_cursor_p;
23829 glyph->multibyte_p = it->multibyte_p;
23830 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23832 /* In R2L rows, the left and the right box edges need to be
23833 drawn in reverse direction. */
23834 glyph->right_box_line_p = it->start_of_box_run_p;
23835 glyph->left_box_line_p = it->end_of_box_run_p;
23837 else
23839 glyph->left_box_line_p = it->start_of_box_run_p;
23840 glyph->right_box_line_p = it->end_of_box_run_p;
23842 glyph->overlaps_vertically_p = 0;
23843 glyph->padding_p = 0;
23844 glyph->glyph_not_available_p = 0;
23845 glyph->face_id = it->face_id;
23846 glyph->u.img_id = img->id;
23847 glyph->slice.img = slice;
23848 glyph->font_type = FONT_TYPE_UNKNOWN;
23849 if (it->bidi_p)
23851 glyph->resolved_level = it->bidi_it.resolved_level;
23852 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23853 emacs_abort ();
23854 glyph->bidi_type = it->bidi_it.type;
23856 ++it->glyph_row->used[area];
23858 else
23859 IT_EXPAND_MATRIX_WIDTH (it, area);
23864 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23865 of the glyph, WIDTH and HEIGHT are the width and height of the
23866 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23868 static void
23869 append_stretch_glyph (struct it *it, Lisp_Object object,
23870 int width, int height, int ascent)
23872 struct glyph *glyph;
23873 enum glyph_row_area area = it->area;
23875 eassert (ascent >= 0 && ascent <= height);
23877 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23878 if (glyph < it->glyph_row->glyphs[area + 1])
23880 /* If the glyph row is reversed, we need to prepend the glyph
23881 rather than append it. */
23882 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23884 struct glyph *g;
23886 /* Make room for the additional glyph. */
23887 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23888 g[1] = *g;
23889 glyph = it->glyph_row->glyphs[area];
23891 glyph->charpos = CHARPOS (it->position);
23892 glyph->object = object;
23893 glyph->pixel_width = width;
23894 glyph->ascent = ascent;
23895 glyph->descent = height - ascent;
23896 glyph->voffset = it->voffset;
23897 glyph->type = STRETCH_GLYPH;
23898 glyph->avoid_cursor_p = it->avoid_cursor_p;
23899 glyph->multibyte_p = it->multibyte_p;
23900 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23902 /* In R2L rows, the left and the right box edges need to be
23903 drawn in reverse direction. */
23904 glyph->right_box_line_p = it->start_of_box_run_p;
23905 glyph->left_box_line_p = it->end_of_box_run_p;
23907 else
23909 glyph->left_box_line_p = it->start_of_box_run_p;
23910 glyph->right_box_line_p = it->end_of_box_run_p;
23912 glyph->overlaps_vertically_p = 0;
23913 glyph->padding_p = 0;
23914 glyph->glyph_not_available_p = 0;
23915 glyph->face_id = it->face_id;
23916 glyph->u.stretch.ascent = ascent;
23917 glyph->u.stretch.height = height;
23918 glyph->slice.img = null_glyph_slice;
23919 glyph->font_type = FONT_TYPE_UNKNOWN;
23920 if (it->bidi_p)
23922 glyph->resolved_level = it->bidi_it.resolved_level;
23923 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23924 emacs_abort ();
23925 glyph->bidi_type = it->bidi_it.type;
23927 else
23929 glyph->resolved_level = 0;
23930 glyph->bidi_type = UNKNOWN_BT;
23932 ++it->glyph_row->used[area];
23934 else
23935 IT_EXPAND_MATRIX_WIDTH (it, area);
23938 #endif /* HAVE_WINDOW_SYSTEM */
23940 /* Produce a stretch glyph for iterator IT. IT->object is the value
23941 of the glyph property displayed. The value must be a list
23942 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23943 being recognized:
23945 1. `:width WIDTH' specifies that the space should be WIDTH *
23946 canonical char width wide. WIDTH may be an integer or floating
23947 point number.
23949 2. `:relative-width FACTOR' specifies that the width of the stretch
23950 should be computed from the width of the first character having the
23951 `glyph' property, and should be FACTOR times that width.
23953 3. `:align-to HPOS' specifies that the space should be wide enough
23954 to reach HPOS, a value in canonical character units.
23956 Exactly one of the above pairs must be present.
23958 4. `:height HEIGHT' specifies that the height of the stretch produced
23959 should be HEIGHT, measured in canonical character units.
23961 5. `:relative-height FACTOR' specifies that the height of the
23962 stretch should be FACTOR times the height of the characters having
23963 the glyph property.
23965 Either none or exactly one of 4 or 5 must be present.
23967 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23968 of the stretch should be used for the ascent of the stretch.
23969 ASCENT must be in the range 0 <= ASCENT <= 100. */
23971 void
23972 produce_stretch_glyph (struct it *it)
23974 /* (space :width WIDTH :height HEIGHT ...) */
23975 Lisp_Object prop, plist;
23976 int width = 0, height = 0, align_to = -1;
23977 int zero_width_ok_p = 0;
23978 double tem;
23979 struct font *font = NULL;
23981 #ifdef HAVE_WINDOW_SYSTEM
23982 int ascent = 0;
23983 int zero_height_ok_p = 0;
23985 if (FRAME_WINDOW_P (it->f))
23987 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23988 font = face->font ? face->font : FRAME_FONT (it->f);
23989 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23991 #endif
23993 /* List should start with `space'. */
23994 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23995 plist = XCDR (it->object);
23997 /* Compute the width of the stretch. */
23998 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23999 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24001 /* Absolute width `:width WIDTH' specified and valid. */
24002 zero_width_ok_p = 1;
24003 width = (int)tem;
24005 #ifdef HAVE_WINDOW_SYSTEM
24006 else if (FRAME_WINDOW_P (it->f)
24007 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24009 /* Relative width `:relative-width FACTOR' specified and valid.
24010 Compute the width of the characters having the `glyph'
24011 property. */
24012 struct it it2;
24013 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24015 it2 = *it;
24016 if (it->multibyte_p)
24017 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24018 else
24020 it2.c = it2.char_to_display = *p, it2.len = 1;
24021 if (! ASCII_CHAR_P (it2.c))
24022 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24025 it2.glyph_row = NULL;
24026 it2.what = IT_CHARACTER;
24027 x_produce_glyphs (&it2);
24028 width = NUMVAL (prop) * it2.pixel_width;
24030 #endif /* HAVE_WINDOW_SYSTEM */
24031 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24032 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24034 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24035 align_to = (align_to < 0
24037 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24038 else if (align_to < 0)
24039 align_to = window_box_left_offset (it->w, TEXT_AREA);
24040 width = max (0, (int)tem + align_to - it->current_x);
24041 zero_width_ok_p = 1;
24043 else
24044 /* Nothing specified -> width defaults to canonical char width. */
24045 width = FRAME_COLUMN_WIDTH (it->f);
24047 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24048 width = 1;
24050 #ifdef HAVE_WINDOW_SYSTEM
24051 /* Compute height. */
24052 if (FRAME_WINDOW_P (it->f))
24054 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24055 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24057 height = (int)tem;
24058 zero_height_ok_p = 1;
24060 else if (prop = Fplist_get (plist, QCrelative_height),
24061 NUMVAL (prop) > 0)
24062 height = FONT_HEIGHT (font) * NUMVAL (prop);
24063 else
24064 height = FONT_HEIGHT (font);
24066 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24067 height = 1;
24069 /* Compute percentage of height used for ascent. If
24070 `:ascent ASCENT' is present and valid, use that. Otherwise,
24071 derive the ascent from the font in use. */
24072 if (prop = Fplist_get (plist, QCascent),
24073 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24074 ascent = height * NUMVAL (prop) / 100.0;
24075 else if (!NILP (prop)
24076 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24077 ascent = min (max (0, (int)tem), height);
24078 else
24079 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24081 else
24082 #endif /* HAVE_WINDOW_SYSTEM */
24083 height = 1;
24085 if (width > 0 && it->line_wrap != TRUNCATE
24086 && it->current_x + width > it->last_visible_x)
24088 width = it->last_visible_x - it->current_x;
24089 #ifdef HAVE_WINDOW_SYSTEM
24090 /* Subtract one more pixel from the stretch width, but only on
24091 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24092 width -= FRAME_WINDOW_P (it->f);
24093 #endif
24096 if (width > 0 && height > 0 && it->glyph_row)
24098 Lisp_Object o_object = it->object;
24099 Lisp_Object object = it->stack[it->sp - 1].string;
24100 int n = width;
24102 if (!STRINGP (object))
24103 object = it->w->contents;
24104 #ifdef HAVE_WINDOW_SYSTEM
24105 if (FRAME_WINDOW_P (it->f))
24106 append_stretch_glyph (it, object, width, height, ascent);
24107 else
24108 #endif
24110 it->object = object;
24111 it->char_to_display = ' ';
24112 it->pixel_width = it->len = 1;
24113 while (n--)
24114 tty_append_glyph (it);
24115 it->object = o_object;
24119 it->pixel_width = width;
24120 #ifdef HAVE_WINDOW_SYSTEM
24121 if (FRAME_WINDOW_P (it->f))
24123 it->ascent = it->phys_ascent = ascent;
24124 it->descent = it->phys_descent = height - it->ascent;
24125 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24126 take_vertical_position_into_account (it);
24128 else
24129 #endif
24130 it->nglyphs = width;
24133 /* Get information about special display element WHAT in an
24134 environment described by IT. WHAT is one of IT_TRUNCATION or
24135 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24136 non-null glyph_row member. This function ensures that fields like
24137 face_id, c, len of IT are left untouched. */
24139 static void
24140 produce_special_glyphs (struct it *it, enum display_element_type what)
24142 struct it temp_it;
24143 Lisp_Object gc;
24144 GLYPH glyph;
24146 temp_it = *it;
24147 temp_it.object = make_number (0);
24148 memset (&temp_it.current, 0, sizeof temp_it.current);
24150 if (what == IT_CONTINUATION)
24152 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24153 if (it->bidi_it.paragraph_dir == R2L)
24154 SET_GLYPH_FROM_CHAR (glyph, '/');
24155 else
24156 SET_GLYPH_FROM_CHAR (glyph, '\\');
24157 if (it->dp
24158 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24160 /* FIXME: Should we mirror GC for R2L lines? */
24161 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24162 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24165 else if (what == IT_TRUNCATION)
24167 /* Truncation glyph. */
24168 SET_GLYPH_FROM_CHAR (glyph, '$');
24169 if (it->dp
24170 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24172 /* FIXME: Should we mirror GC for R2L lines? */
24173 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24174 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24177 else
24178 emacs_abort ();
24180 #ifdef HAVE_WINDOW_SYSTEM
24181 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24182 is turned off, we precede the truncation/continuation glyphs by a
24183 stretch glyph whose width is computed such that these special
24184 glyphs are aligned at the window margin, even when very different
24185 fonts are used in different glyph rows. */
24186 if (FRAME_WINDOW_P (temp_it.f)
24187 /* init_iterator calls this with it->glyph_row == NULL, and it
24188 wants only the pixel width of the truncation/continuation
24189 glyphs. */
24190 && temp_it.glyph_row
24191 /* insert_left_trunc_glyphs calls us at the beginning of the
24192 row, and it has its own calculation of the stretch glyph
24193 width. */
24194 && temp_it.glyph_row->used[TEXT_AREA] > 0
24195 && (temp_it.glyph_row->reversed_p
24196 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24197 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24199 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24201 if (stretch_width > 0)
24203 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24204 struct font *font =
24205 face->font ? face->font : FRAME_FONT (temp_it.f);
24206 int stretch_ascent =
24207 (((temp_it.ascent + temp_it.descent)
24208 * FONT_BASE (font)) / FONT_HEIGHT (font));
24210 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24211 temp_it.ascent + temp_it.descent,
24212 stretch_ascent);
24215 #endif
24217 temp_it.dp = NULL;
24218 temp_it.what = IT_CHARACTER;
24219 temp_it.len = 1;
24220 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24221 temp_it.face_id = GLYPH_FACE (glyph);
24222 temp_it.len = CHAR_BYTES (temp_it.c);
24224 PRODUCE_GLYPHS (&temp_it);
24225 it->pixel_width = temp_it.pixel_width;
24226 it->nglyphs = temp_it.pixel_width;
24229 #ifdef HAVE_WINDOW_SYSTEM
24231 /* Calculate line-height and line-spacing properties.
24232 An integer value specifies explicit pixel value.
24233 A float value specifies relative value to current face height.
24234 A cons (float . face-name) specifies relative value to
24235 height of specified face font.
24237 Returns height in pixels, or nil. */
24240 static Lisp_Object
24241 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24242 int boff, int override)
24244 Lisp_Object face_name = Qnil;
24245 int ascent, descent, height;
24247 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24248 return val;
24250 if (CONSP (val))
24252 face_name = XCAR (val);
24253 val = XCDR (val);
24254 if (!NUMBERP (val))
24255 val = make_number (1);
24256 if (NILP (face_name))
24258 height = it->ascent + it->descent;
24259 goto scale;
24263 if (NILP (face_name))
24265 font = FRAME_FONT (it->f);
24266 boff = FRAME_BASELINE_OFFSET (it->f);
24268 else if (EQ (face_name, Qt))
24270 override = 0;
24272 else
24274 int face_id;
24275 struct face *face;
24277 face_id = lookup_named_face (it->f, face_name, 0);
24278 if (face_id < 0)
24279 return make_number (-1);
24281 face = FACE_FROM_ID (it->f, face_id);
24282 font = face->font;
24283 if (font == NULL)
24284 return make_number (-1);
24285 boff = font->baseline_offset;
24286 if (font->vertical_centering)
24287 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24290 ascent = FONT_BASE (font) + boff;
24291 descent = FONT_DESCENT (font) - boff;
24293 if (override)
24295 it->override_ascent = ascent;
24296 it->override_descent = descent;
24297 it->override_boff = boff;
24300 height = ascent + descent;
24302 scale:
24303 if (FLOATP (val))
24304 height = (int)(XFLOAT_DATA (val) * height);
24305 else if (INTEGERP (val))
24306 height *= XINT (val);
24308 return make_number (height);
24312 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24313 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24314 and only if this is for a character for which no font was found.
24316 If the display method (it->glyphless_method) is
24317 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24318 length of the acronym or the hexadecimal string, UPPER_XOFF and
24319 UPPER_YOFF are pixel offsets for the upper part of the string,
24320 LOWER_XOFF and LOWER_YOFF are for the lower part.
24322 For the other display methods, LEN through LOWER_YOFF are zero. */
24324 static void
24325 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24326 short upper_xoff, short upper_yoff,
24327 short lower_xoff, short lower_yoff)
24329 struct glyph *glyph;
24330 enum glyph_row_area area = it->area;
24332 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24333 if (glyph < it->glyph_row->glyphs[area + 1])
24335 /* If the glyph row is reversed, we need to prepend the glyph
24336 rather than append it. */
24337 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24339 struct glyph *g;
24341 /* Make room for the additional glyph. */
24342 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24343 g[1] = *g;
24344 glyph = it->glyph_row->glyphs[area];
24346 glyph->charpos = CHARPOS (it->position);
24347 glyph->object = it->object;
24348 glyph->pixel_width = it->pixel_width;
24349 glyph->ascent = it->ascent;
24350 glyph->descent = it->descent;
24351 glyph->voffset = it->voffset;
24352 glyph->type = GLYPHLESS_GLYPH;
24353 glyph->u.glyphless.method = it->glyphless_method;
24354 glyph->u.glyphless.for_no_font = for_no_font;
24355 glyph->u.glyphless.len = len;
24356 glyph->u.glyphless.ch = it->c;
24357 glyph->slice.glyphless.upper_xoff = upper_xoff;
24358 glyph->slice.glyphless.upper_yoff = upper_yoff;
24359 glyph->slice.glyphless.lower_xoff = lower_xoff;
24360 glyph->slice.glyphless.lower_yoff = lower_yoff;
24361 glyph->avoid_cursor_p = it->avoid_cursor_p;
24362 glyph->multibyte_p = it->multibyte_p;
24363 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24365 /* In R2L rows, the left and the right box edges need to be
24366 drawn in reverse direction. */
24367 glyph->right_box_line_p = it->start_of_box_run_p;
24368 glyph->left_box_line_p = it->end_of_box_run_p;
24370 else
24372 glyph->left_box_line_p = it->start_of_box_run_p;
24373 glyph->right_box_line_p = it->end_of_box_run_p;
24375 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24376 || it->phys_descent > it->descent);
24377 glyph->padding_p = 0;
24378 glyph->glyph_not_available_p = 0;
24379 glyph->face_id = face_id;
24380 glyph->font_type = FONT_TYPE_UNKNOWN;
24381 if (it->bidi_p)
24383 glyph->resolved_level = it->bidi_it.resolved_level;
24384 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24385 emacs_abort ();
24386 glyph->bidi_type = it->bidi_it.type;
24388 ++it->glyph_row->used[area];
24390 else
24391 IT_EXPAND_MATRIX_WIDTH (it, area);
24395 /* Produce a glyph for a glyphless character for iterator IT.
24396 IT->glyphless_method specifies which method to use for displaying
24397 the character. See the description of enum
24398 glyphless_display_method in dispextern.h for the detail.
24400 FOR_NO_FONT is nonzero if and only if this is for a character for
24401 which no font was found. ACRONYM, if non-nil, is an acronym string
24402 for the character. */
24404 static void
24405 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24407 int face_id;
24408 struct face *face;
24409 struct font *font;
24410 int base_width, base_height, width, height;
24411 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24412 int len;
24414 /* Get the metrics of the base font. We always refer to the current
24415 ASCII face. */
24416 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24417 font = face->font ? face->font : FRAME_FONT (it->f);
24418 it->ascent = FONT_BASE (font) + font->baseline_offset;
24419 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24420 base_height = it->ascent + it->descent;
24421 base_width = font->average_width;
24423 /* Get a face ID for the glyph by utilizing a cache (the same way as
24424 done for `escape-glyph' in get_next_display_element). */
24425 if (it->f == last_glyphless_glyph_frame
24426 && it->face_id == last_glyphless_glyph_face_id)
24428 face_id = last_glyphless_glyph_merged_face_id;
24430 else
24432 /* Merge the `glyphless-char' face into the current face. */
24433 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24434 last_glyphless_glyph_frame = it->f;
24435 last_glyphless_glyph_face_id = it->face_id;
24436 last_glyphless_glyph_merged_face_id = face_id;
24439 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24441 it->pixel_width = THIN_SPACE_WIDTH;
24442 len = 0;
24443 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24445 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24447 width = CHAR_WIDTH (it->c);
24448 if (width == 0)
24449 width = 1;
24450 else if (width > 4)
24451 width = 4;
24452 it->pixel_width = base_width * width;
24453 len = 0;
24454 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24456 else
24458 char buf[7];
24459 const char *str;
24460 unsigned int code[6];
24461 int upper_len;
24462 int ascent, descent;
24463 struct font_metrics metrics_upper, metrics_lower;
24465 face = FACE_FROM_ID (it->f, face_id);
24466 font = face->font ? face->font : FRAME_FONT (it->f);
24467 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24469 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24471 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24472 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24473 if (CONSP (acronym))
24474 acronym = XCAR (acronym);
24475 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24477 else
24479 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24480 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24481 str = buf;
24483 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24484 code[len] = font->driver->encode_char (font, str[len]);
24485 upper_len = (len + 1) / 2;
24486 font->driver->text_extents (font, code, upper_len,
24487 &metrics_upper);
24488 font->driver->text_extents (font, code + upper_len, len - upper_len,
24489 &metrics_lower);
24493 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24494 width = max (metrics_upper.width, metrics_lower.width) + 4;
24495 upper_xoff = upper_yoff = 2; /* the typical case */
24496 if (base_width >= width)
24498 /* Align the upper to the left, the lower to the right. */
24499 it->pixel_width = base_width;
24500 lower_xoff = base_width - 2 - metrics_lower.width;
24502 else
24504 /* Center the shorter one. */
24505 it->pixel_width = width;
24506 if (metrics_upper.width >= metrics_lower.width)
24507 lower_xoff = (width - metrics_lower.width) / 2;
24508 else
24510 /* FIXME: This code doesn't look right. It formerly was
24511 missing the "lower_xoff = 0;", which couldn't have
24512 been right since it left lower_xoff uninitialized. */
24513 lower_xoff = 0;
24514 upper_xoff = (width - metrics_upper.width) / 2;
24518 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24519 top, bottom, and between upper and lower strings. */
24520 height = (metrics_upper.ascent + metrics_upper.descent
24521 + metrics_lower.ascent + metrics_lower.descent) + 5;
24522 /* Center vertically.
24523 H:base_height, D:base_descent
24524 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24526 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24527 descent = D - H/2 + h/2;
24528 lower_yoff = descent - 2 - ld;
24529 upper_yoff = lower_yoff - la - 1 - ud; */
24530 ascent = - (it->descent - (base_height + height + 1) / 2);
24531 descent = it->descent - (base_height - height) / 2;
24532 lower_yoff = descent - 2 - metrics_lower.descent;
24533 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24534 - metrics_upper.descent);
24535 /* Don't make the height shorter than the base height. */
24536 if (height > base_height)
24538 it->ascent = ascent;
24539 it->descent = descent;
24543 it->phys_ascent = it->ascent;
24544 it->phys_descent = it->descent;
24545 if (it->glyph_row)
24546 append_glyphless_glyph (it, face_id, for_no_font, len,
24547 upper_xoff, upper_yoff,
24548 lower_xoff, lower_yoff);
24549 it->nglyphs = 1;
24550 take_vertical_position_into_account (it);
24554 /* RIF:
24555 Produce glyphs/get display metrics for the display element IT is
24556 loaded with. See the description of struct it in dispextern.h
24557 for an overview of struct it. */
24559 void
24560 x_produce_glyphs (struct it *it)
24562 int extra_line_spacing = it->extra_line_spacing;
24564 it->glyph_not_available_p = 0;
24566 if (it->what == IT_CHARACTER)
24568 XChar2b char2b;
24569 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24570 struct font *font = face->font;
24571 struct font_metrics *pcm = NULL;
24572 int boff; /* baseline offset */
24574 if (font == NULL)
24576 /* When no suitable font is found, display this character by
24577 the method specified in the first extra slot of
24578 Vglyphless_char_display. */
24579 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24581 eassert (it->what == IT_GLYPHLESS);
24582 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24583 goto done;
24586 boff = font->baseline_offset;
24587 if (font->vertical_centering)
24588 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24590 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24592 int stretched_p;
24594 it->nglyphs = 1;
24596 if (it->override_ascent >= 0)
24598 it->ascent = it->override_ascent;
24599 it->descent = it->override_descent;
24600 boff = it->override_boff;
24602 else
24604 it->ascent = FONT_BASE (font) + boff;
24605 it->descent = FONT_DESCENT (font) - boff;
24608 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24610 pcm = get_per_char_metric (font, &char2b);
24611 if (pcm->width == 0
24612 && pcm->rbearing == 0 && pcm->lbearing == 0)
24613 pcm = NULL;
24616 if (pcm)
24618 it->phys_ascent = pcm->ascent + boff;
24619 it->phys_descent = pcm->descent - boff;
24620 it->pixel_width = pcm->width;
24622 else
24624 it->glyph_not_available_p = 1;
24625 it->phys_ascent = it->ascent;
24626 it->phys_descent = it->descent;
24627 it->pixel_width = font->space_width;
24630 if (it->constrain_row_ascent_descent_p)
24632 if (it->descent > it->max_descent)
24634 it->ascent += it->descent - it->max_descent;
24635 it->descent = it->max_descent;
24637 if (it->ascent > it->max_ascent)
24639 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24640 it->ascent = it->max_ascent;
24642 it->phys_ascent = min (it->phys_ascent, it->ascent);
24643 it->phys_descent = min (it->phys_descent, it->descent);
24644 extra_line_spacing = 0;
24647 /* If this is a space inside a region of text with
24648 `space-width' property, change its width. */
24649 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24650 if (stretched_p)
24651 it->pixel_width *= XFLOATINT (it->space_width);
24653 /* If face has a box, add the box thickness to the character
24654 height. If character has a box line to the left and/or
24655 right, add the box line width to the character's width. */
24656 if (face->box != FACE_NO_BOX)
24658 int thick = face->box_line_width;
24660 if (thick > 0)
24662 it->ascent += thick;
24663 it->descent += thick;
24665 else
24666 thick = -thick;
24668 if (it->start_of_box_run_p)
24669 it->pixel_width += thick;
24670 if (it->end_of_box_run_p)
24671 it->pixel_width += thick;
24674 /* If face has an overline, add the height of the overline
24675 (1 pixel) and a 1 pixel margin to the character height. */
24676 if (face->overline_p)
24677 it->ascent += overline_margin;
24679 if (it->constrain_row_ascent_descent_p)
24681 if (it->ascent > it->max_ascent)
24682 it->ascent = it->max_ascent;
24683 if (it->descent > it->max_descent)
24684 it->descent = it->max_descent;
24687 take_vertical_position_into_account (it);
24689 /* If we have to actually produce glyphs, do it. */
24690 if (it->glyph_row)
24692 if (stretched_p)
24694 /* Translate a space with a `space-width' property
24695 into a stretch glyph. */
24696 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24697 / FONT_HEIGHT (font));
24698 append_stretch_glyph (it, it->object, it->pixel_width,
24699 it->ascent + it->descent, ascent);
24701 else
24702 append_glyph (it);
24704 /* If characters with lbearing or rbearing are displayed
24705 in this line, record that fact in a flag of the
24706 glyph row. This is used to optimize X output code. */
24707 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24708 it->glyph_row->contains_overlapping_glyphs_p = 1;
24710 if (! stretched_p && it->pixel_width == 0)
24711 /* We assure that all visible glyphs have at least 1-pixel
24712 width. */
24713 it->pixel_width = 1;
24715 else if (it->char_to_display == '\n')
24717 /* A newline has no width, but we need the height of the
24718 line. But if previous part of the line sets a height,
24719 don't increase that height */
24721 Lisp_Object height;
24722 Lisp_Object total_height = Qnil;
24724 it->override_ascent = -1;
24725 it->pixel_width = 0;
24726 it->nglyphs = 0;
24728 height = get_it_property (it, Qline_height);
24729 /* Split (line-height total-height) list */
24730 if (CONSP (height)
24731 && CONSP (XCDR (height))
24732 && NILP (XCDR (XCDR (height))))
24734 total_height = XCAR (XCDR (height));
24735 height = XCAR (height);
24737 height = calc_line_height_property (it, height, font, boff, 1);
24739 if (it->override_ascent >= 0)
24741 it->ascent = it->override_ascent;
24742 it->descent = it->override_descent;
24743 boff = it->override_boff;
24745 else
24747 it->ascent = FONT_BASE (font) + boff;
24748 it->descent = FONT_DESCENT (font) - boff;
24751 if (EQ (height, Qt))
24753 if (it->descent > it->max_descent)
24755 it->ascent += it->descent - it->max_descent;
24756 it->descent = it->max_descent;
24758 if (it->ascent > it->max_ascent)
24760 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24761 it->ascent = it->max_ascent;
24763 it->phys_ascent = min (it->phys_ascent, it->ascent);
24764 it->phys_descent = min (it->phys_descent, it->descent);
24765 it->constrain_row_ascent_descent_p = 1;
24766 extra_line_spacing = 0;
24768 else
24770 Lisp_Object spacing;
24772 it->phys_ascent = it->ascent;
24773 it->phys_descent = it->descent;
24775 if ((it->max_ascent > 0 || it->max_descent > 0)
24776 && face->box != FACE_NO_BOX
24777 && face->box_line_width > 0)
24779 it->ascent += face->box_line_width;
24780 it->descent += face->box_line_width;
24782 if (!NILP (height)
24783 && XINT (height) > it->ascent + it->descent)
24784 it->ascent = XINT (height) - it->descent;
24786 if (!NILP (total_height))
24787 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24788 else
24790 spacing = get_it_property (it, Qline_spacing);
24791 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24793 if (INTEGERP (spacing))
24795 extra_line_spacing = XINT (spacing);
24796 if (!NILP (total_height))
24797 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24801 else /* i.e. (it->char_to_display == '\t') */
24803 if (font->space_width > 0)
24805 int tab_width = it->tab_width * font->space_width;
24806 int x = it->current_x + it->continuation_lines_width;
24807 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24809 /* If the distance from the current position to the next tab
24810 stop is less than a space character width, use the
24811 tab stop after that. */
24812 if (next_tab_x - x < font->space_width)
24813 next_tab_x += tab_width;
24815 it->pixel_width = next_tab_x - x;
24816 it->nglyphs = 1;
24817 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24818 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24820 if (it->glyph_row)
24822 append_stretch_glyph (it, it->object, it->pixel_width,
24823 it->ascent + it->descent, it->ascent);
24826 else
24828 it->pixel_width = 0;
24829 it->nglyphs = 1;
24833 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24835 /* A static composition.
24837 Note: A composition is represented as one glyph in the
24838 glyph matrix. There are no padding glyphs.
24840 Important note: pixel_width, ascent, and descent are the
24841 values of what is drawn by draw_glyphs (i.e. the values of
24842 the overall glyphs composed). */
24843 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24844 int boff; /* baseline offset */
24845 struct composition *cmp = composition_table[it->cmp_it.id];
24846 int glyph_len = cmp->glyph_len;
24847 struct font *font = face->font;
24849 it->nglyphs = 1;
24851 /* If we have not yet calculated pixel size data of glyphs of
24852 the composition for the current face font, calculate them
24853 now. Theoretically, we have to check all fonts for the
24854 glyphs, but that requires much time and memory space. So,
24855 here we check only the font of the first glyph. This may
24856 lead to incorrect display, but it's very rare, and C-l
24857 (recenter-top-bottom) can correct the display anyway. */
24858 if (! cmp->font || cmp->font != font)
24860 /* Ascent and descent of the font of the first character
24861 of this composition (adjusted by baseline offset).
24862 Ascent and descent of overall glyphs should not be less
24863 than these, respectively. */
24864 int font_ascent, font_descent, font_height;
24865 /* Bounding box of the overall glyphs. */
24866 int leftmost, rightmost, lowest, highest;
24867 int lbearing, rbearing;
24868 int i, width, ascent, descent;
24869 int left_padded = 0, right_padded = 0;
24870 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24871 XChar2b char2b;
24872 struct font_metrics *pcm;
24873 int font_not_found_p;
24874 ptrdiff_t pos;
24876 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24877 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24878 break;
24879 if (glyph_len < cmp->glyph_len)
24880 right_padded = 1;
24881 for (i = 0; i < glyph_len; i++)
24883 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24884 break;
24885 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24887 if (i > 0)
24888 left_padded = 1;
24890 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24891 : IT_CHARPOS (*it));
24892 /* If no suitable font is found, use the default font. */
24893 font_not_found_p = font == NULL;
24894 if (font_not_found_p)
24896 face = face->ascii_face;
24897 font = face->font;
24899 boff = font->baseline_offset;
24900 if (font->vertical_centering)
24901 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24902 font_ascent = FONT_BASE (font) + boff;
24903 font_descent = FONT_DESCENT (font) - boff;
24904 font_height = FONT_HEIGHT (font);
24906 cmp->font = font;
24908 pcm = NULL;
24909 if (! font_not_found_p)
24911 get_char_face_and_encoding (it->f, c, it->face_id,
24912 &char2b, 0);
24913 pcm = get_per_char_metric (font, &char2b);
24916 /* Initialize the bounding box. */
24917 if (pcm)
24919 width = cmp->glyph_len > 0 ? pcm->width : 0;
24920 ascent = pcm->ascent;
24921 descent = pcm->descent;
24922 lbearing = pcm->lbearing;
24923 rbearing = pcm->rbearing;
24925 else
24927 width = cmp->glyph_len > 0 ? font->space_width : 0;
24928 ascent = FONT_BASE (font);
24929 descent = FONT_DESCENT (font);
24930 lbearing = 0;
24931 rbearing = width;
24934 rightmost = width;
24935 leftmost = 0;
24936 lowest = - descent + boff;
24937 highest = ascent + boff;
24939 if (! font_not_found_p
24940 && font->default_ascent
24941 && CHAR_TABLE_P (Vuse_default_ascent)
24942 && !NILP (Faref (Vuse_default_ascent,
24943 make_number (it->char_to_display))))
24944 highest = font->default_ascent + boff;
24946 /* Draw the first glyph at the normal position. It may be
24947 shifted to right later if some other glyphs are drawn
24948 at the left. */
24949 cmp->offsets[i * 2] = 0;
24950 cmp->offsets[i * 2 + 1] = boff;
24951 cmp->lbearing = lbearing;
24952 cmp->rbearing = rbearing;
24954 /* Set cmp->offsets for the remaining glyphs. */
24955 for (i++; i < glyph_len; i++)
24957 int left, right, btm, top;
24958 int ch = COMPOSITION_GLYPH (cmp, i);
24959 int face_id;
24960 struct face *this_face;
24962 if (ch == '\t')
24963 ch = ' ';
24964 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24965 this_face = FACE_FROM_ID (it->f, face_id);
24966 font = this_face->font;
24968 if (font == NULL)
24969 pcm = NULL;
24970 else
24972 get_char_face_and_encoding (it->f, ch, face_id,
24973 &char2b, 0);
24974 pcm = get_per_char_metric (font, &char2b);
24976 if (! pcm)
24977 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24978 else
24980 width = pcm->width;
24981 ascent = pcm->ascent;
24982 descent = pcm->descent;
24983 lbearing = pcm->lbearing;
24984 rbearing = pcm->rbearing;
24985 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24987 /* Relative composition with or without
24988 alternate chars. */
24989 left = (leftmost + rightmost - width) / 2;
24990 btm = - descent + boff;
24991 if (font->relative_compose
24992 && (! CHAR_TABLE_P (Vignore_relative_composition)
24993 || NILP (Faref (Vignore_relative_composition,
24994 make_number (ch)))))
24997 if (- descent >= font->relative_compose)
24998 /* One extra pixel between two glyphs. */
24999 btm = highest + 1;
25000 else if (ascent <= 0)
25001 /* One extra pixel between two glyphs. */
25002 btm = lowest - 1 - ascent - descent;
25005 else
25007 /* A composition rule is specified by an integer
25008 value that encodes global and new reference
25009 points (GREF and NREF). GREF and NREF are
25010 specified by numbers as below:
25012 0---1---2 -- ascent
25016 9--10--11 -- center
25018 ---3---4---5--- baseline
25020 6---7---8 -- descent
25022 int rule = COMPOSITION_RULE (cmp, i);
25023 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25025 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25026 grefx = gref % 3, nrefx = nref % 3;
25027 grefy = gref / 3, nrefy = nref / 3;
25028 if (xoff)
25029 xoff = font_height * (xoff - 128) / 256;
25030 if (yoff)
25031 yoff = font_height * (yoff - 128) / 256;
25033 left = (leftmost
25034 + grefx * (rightmost - leftmost) / 2
25035 - nrefx * width / 2
25036 + xoff);
25038 btm = ((grefy == 0 ? highest
25039 : grefy == 1 ? 0
25040 : grefy == 2 ? lowest
25041 : (highest + lowest) / 2)
25042 - (nrefy == 0 ? ascent + descent
25043 : nrefy == 1 ? descent - boff
25044 : nrefy == 2 ? 0
25045 : (ascent + descent) / 2)
25046 + yoff);
25049 cmp->offsets[i * 2] = left;
25050 cmp->offsets[i * 2 + 1] = btm + descent;
25052 /* Update the bounding box of the overall glyphs. */
25053 if (width > 0)
25055 right = left + width;
25056 if (left < leftmost)
25057 leftmost = left;
25058 if (right > rightmost)
25059 rightmost = right;
25061 top = btm + descent + ascent;
25062 if (top > highest)
25063 highest = top;
25064 if (btm < lowest)
25065 lowest = btm;
25067 if (cmp->lbearing > left + lbearing)
25068 cmp->lbearing = left + lbearing;
25069 if (cmp->rbearing < left + rbearing)
25070 cmp->rbearing = left + rbearing;
25074 /* If there are glyphs whose x-offsets are negative,
25075 shift all glyphs to the right and make all x-offsets
25076 non-negative. */
25077 if (leftmost < 0)
25079 for (i = 0; i < cmp->glyph_len; i++)
25080 cmp->offsets[i * 2] -= leftmost;
25081 rightmost -= leftmost;
25082 cmp->lbearing -= leftmost;
25083 cmp->rbearing -= leftmost;
25086 if (left_padded && cmp->lbearing < 0)
25088 for (i = 0; i < cmp->glyph_len; i++)
25089 cmp->offsets[i * 2] -= cmp->lbearing;
25090 rightmost -= cmp->lbearing;
25091 cmp->rbearing -= cmp->lbearing;
25092 cmp->lbearing = 0;
25094 if (right_padded && rightmost < cmp->rbearing)
25096 rightmost = cmp->rbearing;
25099 cmp->pixel_width = rightmost;
25100 cmp->ascent = highest;
25101 cmp->descent = - lowest;
25102 if (cmp->ascent < font_ascent)
25103 cmp->ascent = font_ascent;
25104 if (cmp->descent < font_descent)
25105 cmp->descent = font_descent;
25108 if (it->glyph_row
25109 && (cmp->lbearing < 0
25110 || cmp->rbearing > cmp->pixel_width))
25111 it->glyph_row->contains_overlapping_glyphs_p = 1;
25113 it->pixel_width = cmp->pixel_width;
25114 it->ascent = it->phys_ascent = cmp->ascent;
25115 it->descent = it->phys_descent = cmp->descent;
25116 if (face->box != FACE_NO_BOX)
25118 int thick = face->box_line_width;
25120 if (thick > 0)
25122 it->ascent += thick;
25123 it->descent += thick;
25125 else
25126 thick = - thick;
25128 if (it->start_of_box_run_p)
25129 it->pixel_width += thick;
25130 if (it->end_of_box_run_p)
25131 it->pixel_width += thick;
25134 /* If face has an overline, add the height of the overline
25135 (1 pixel) and a 1 pixel margin to the character height. */
25136 if (face->overline_p)
25137 it->ascent += overline_margin;
25139 take_vertical_position_into_account (it);
25140 if (it->ascent < 0)
25141 it->ascent = 0;
25142 if (it->descent < 0)
25143 it->descent = 0;
25145 if (it->glyph_row && cmp->glyph_len > 0)
25146 append_composite_glyph (it);
25148 else if (it->what == IT_COMPOSITION)
25150 /* A dynamic (automatic) composition. */
25151 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25152 Lisp_Object gstring;
25153 struct font_metrics metrics;
25155 it->nglyphs = 1;
25157 gstring = composition_gstring_from_id (it->cmp_it.id);
25158 it->pixel_width
25159 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25160 &metrics);
25161 if (it->glyph_row
25162 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25163 it->glyph_row->contains_overlapping_glyphs_p = 1;
25164 it->ascent = it->phys_ascent = metrics.ascent;
25165 it->descent = it->phys_descent = metrics.descent;
25166 if (face->box != FACE_NO_BOX)
25168 int thick = face->box_line_width;
25170 if (thick > 0)
25172 it->ascent += thick;
25173 it->descent += thick;
25175 else
25176 thick = - thick;
25178 if (it->start_of_box_run_p)
25179 it->pixel_width += thick;
25180 if (it->end_of_box_run_p)
25181 it->pixel_width += thick;
25183 /* If face has an overline, add the height of the overline
25184 (1 pixel) and a 1 pixel margin to the character height. */
25185 if (face->overline_p)
25186 it->ascent += overline_margin;
25187 take_vertical_position_into_account (it);
25188 if (it->ascent < 0)
25189 it->ascent = 0;
25190 if (it->descent < 0)
25191 it->descent = 0;
25193 if (it->glyph_row)
25194 append_composite_glyph (it);
25196 else if (it->what == IT_GLYPHLESS)
25197 produce_glyphless_glyph (it, 0, Qnil);
25198 else if (it->what == IT_IMAGE)
25199 produce_image_glyph (it);
25200 else if (it->what == IT_STRETCH)
25201 produce_stretch_glyph (it);
25203 done:
25204 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25205 because this isn't true for images with `:ascent 100'. */
25206 eassert (it->ascent >= 0 && it->descent >= 0);
25207 if (it->area == TEXT_AREA)
25208 it->current_x += it->pixel_width;
25210 if (extra_line_spacing > 0)
25212 it->descent += extra_line_spacing;
25213 if (extra_line_spacing > it->max_extra_line_spacing)
25214 it->max_extra_line_spacing = extra_line_spacing;
25217 it->max_ascent = max (it->max_ascent, it->ascent);
25218 it->max_descent = max (it->max_descent, it->descent);
25219 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25220 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25223 /* EXPORT for RIF:
25224 Output LEN glyphs starting at START at the nominal cursor position.
25225 Advance the nominal cursor over the text. The global variable
25226 updated_window contains the window being updated, updated_row is
25227 the glyph row being updated, and updated_area is the area of that
25228 row being updated. */
25230 void
25231 x_write_glyphs (struct glyph *start, int len)
25233 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25235 eassert (updated_window && updated_row);
25236 /* When the window is hscrolled, cursor hpos can legitimately be out
25237 of bounds, but we draw the cursor at the corresponding window
25238 margin in that case. */
25239 if (!updated_row->reversed_p && chpos < 0)
25240 chpos = 0;
25241 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25242 chpos = updated_row->used[TEXT_AREA] - 1;
25244 block_input ();
25246 /* Write glyphs. */
25248 hpos = start - updated_row->glyphs[updated_area];
25249 x = draw_glyphs (updated_window, output_cursor.x,
25250 updated_row, updated_area,
25251 hpos, hpos + len,
25252 DRAW_NORMAL_TEXT, 0);
25254 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25255 if (updated_area == TEXT_AREA
25256 && updated_window->phys_cursor_on_p
25257 && updated_window->phys_cursor.vpos == output_cursor.vpos
25258 && chpos >= hpos
25259 && chpos < hpos + len)
25260 updated_window->phys_cursor_on_p = 0;
25262 unblock_input ();
25264 /* Advance the output cursor. */
25265 output_cursor.hpos += len;
25266 output_cursor.x = x;
25270 /* EXPORT for RIF:
25271 Insert LEN glyphs from START at the nominal cursor position. */
25273 void
25274 x_insert_glyphs (struct glyph *start, int len)
25276 struct frame *f;
25277 struct window *w;
25278 int line_height, shift_by_width, shifted_region_width;
25279 struct glyph_row *row;
25280 struct glyph *glyph;
25281 int frame_x, frame_y;
25282 ptrdiff_t hpos;
25284 eassert (updated_window && updated_row);
25285 block_input ();
25286 w = updated_window;
25287 f = XFRAME (WINDOW_FRAME (w));
25289 /* Get the height of the line we are in. */
25290 row = updated_row;
25291 line_height = row->height;
25293 /* Get the width of the glyphs to insert. */
25294 shift_by_width = 0;
25295 for (glyph = start; glyph < start + len; ++glyph)
25296 shift_by_width += glyph->pixel_width;
25298 /* Get the width of the region to shift right. */
25299 shifted_region_width = (window_box_width (w, updated_area)
25300 - output_cursor.x
25301 - shift_by_width);
25303 /* Shift right. */
25304 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25305 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25307 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25308 line_height, shift_by_width);
25310 /* Write the glyphs. */
25311 hpos = start - row->glyphs[updated_area];
25312 draw_glyphs (w, output_cursor.x, row, updated_area,
25313 hpos, hpos + len,
25314 DRAW_NORMAL_TEXT, 0);
25316 /* Advance the output cursor. */
25317 output_cursor.hpos += len;
25318 output_cursor.x += shift_by_width;
25319 unblock_input ();
25323 /* EXPORT for RIF:
25324 Erase the current text line from the nominal cursor position
25325 (inclusive) to pixel column TO_X (exclusive). The idea is that
25326 everything from TO_X onward is already erased.
25328 TO_X is a pixel position relative to updated_area of
25329 updated_window. TO_X == -1 means clear to the end of this area. */
25331 void
25332 x_clear_end_of_line (int to_x)
25334 struct frame *f;
25335 struct window *w = updated_window;
25336 int max_x, min_y, max_y;
25337 int from_x, from_y, to_y;
25339 eassert (updated_window && updated_row);
25340 f = XFRAME (w->frame);
25342 if (updated_row->full_width_p)
25343 max_x = WINDOW_TOTAL_WIDTH (w);
25344 else
25345 max_x = window_box_width (w, updated_area);
25346 max_y = window_text_bottom_y (w);
25348 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25349 of window. For TO_X > 0, truncate to end of drawing area. */
25350 if (to_x == 0)
25351 return;
25352 else if (to_x < 0)
25353 to_x = max_x;
25354 else
25355 to_x = min (to_x, max_x);
25357 to_y = min (max_y, output_cursor.y + updated_row->height);
25359 /* Notice if the cursor will be cleared by this operation. */
25360 if (!updated_row->full_width_p)
25361 notice_overwritten_cursor (w, updated_area,
25362 output_cursor.x, -1,
25363 updated_row->y,
25364 MATRIX_ROW_BOTTOM_Y (updated_row));
25366 from_x = output_cursor.x;
25368 /* Translate to frame coordinates. */
25369 if (updated_row->full_width_p)
25371 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25372 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25374 else
25376 int area_left = window_box_left (w, updated_area);
25377 from_x += area_left;
25378 to_x += area_left;
25381 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25382 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25383 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25385 /* Prevent inadvertently clearing to end of the X window. */
25386 if (to_x > from_x && to_y > from_y)
25388 block_input ();
25389 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25390 to_x - from_x, to_y - from_y);
25391 unblock_input ();
25395 #endif /* HAVE_WINDOW_SYSTEM */
25399 /***********************************************************************
25400 Cursor types
25401 ***********************************************************************/
25403 /* Value is the internal representation of the specified cursor type
25404 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25405 of the bar cursor. */
25407 static enum text_cursor_kinds
25408 get_specified_cursor_type (Lisp_Object arg, int *width)
25410 enum text_cursor_kinds type;
25412 if (NILP (arg))
25413 return NO_CURSOR;
25415 if (EQ (arg, Qbox))
25416 return FILLED_BOX_CURSOR;
25418 if (EQ (arg, Qhollow))
25419 return HOLLOW_BOX_CURSOR;
25421 if (EQ (arg, Qbar))
25423 *width = 2;
25424 return BAR_CURSOR;
25427 if (CONSP (arg)
25428 && EQ (XCAR (arg), Qbar)
25429 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25431 *width = XINT (XCDR (arg));
25432 return BAR_CURSOR;
25435 if (EQ (arg, Qhbar))
25437 *width = 2;
25438 return HBAR_CURSOR;
25441 if (CONSP (arg)
25442 && EQ (XCAR (arg), Qhbar)
25443 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25445 *width = XINT (XCDR (arg));
25446 return HBAR_CURSOR;
25449 /* Treat anything unknown as "hollow box cursor".
25450 It was bad to signal an error; people have trouble fixing
25451 .Xdefaults with Emacs, when it has something bad in it. */
25452 type = HOLLOW_BOX_CURSOR;
25454 return type;
25457 /* Set the default cursor types for specified frame. */
25458 void
25459 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25461 int width = 1;
25462 Lisp_Object tem;
25464 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25465 FRAME_CURSOR_WIDTH (f) = width;
25467 /* By default, set up the blink-off state depending on the on-state. */
25469 tem = Fassoc (arg, Vblink_cursor_alist);
25470 if (!NILP (tem))
25472 FRAME_BLINK_OFF_CURSOR (f)
25473 = get_specified_cursor_type (XCDR (tem), &width);
25474 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25476 else
25477 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25481 #ifdef HAVE_WINDOW_SYSTEM
25483 /* Return the cursor we want to be displayed in window W. Return
25484 width of bar/hbar cursor through WIDTH arg. Return with
25485 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25486 (i.e. if the `system caret' should track this cursor).
25488 In a mini-buffer window, we want the cursor only to appear if we
25489 are reading input from this window. For the selected window, we
25490 want the cursor type given by the frame parameter or buffer local
25491 setting of cursor-type. If explicitly marked off, draw no cursor.
25492 In all other cases, we want a hollow box cursor. */
25494 static enum text_cursor_kinds
25495 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25496 int *active_cursor)
25498 struct frame *f = XFRAME (w->frame);
25499 struct buffer *b = XBUFFER (w->contents);
25500 int cursor_type = DEFAULT_CURSOR;
25501 Lisp_Object alt_cursor;
25502 int non_selected = 0;
25504 *active_cursor = 1;
25506 /* Echo area */
25507 if (cursor_in_echo_area
25508 && FRAME_HAS_MINIBUF_P (f)
25509 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25511 if (w == XWINDOW (echo_area_window))
25513 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25515 *width = FRAME_CURSOR_WIDTH (f);
25516 return FRAME_DESIRED_CURSOR (f);
25518 else
25519 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25522 *active_cursor = 0;
25523 non_selected = 1;
25526 /* Detect a nonselected window or nonselected frame. */
25527 else if (w != XWINDOW (f->selected_window)
25528 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25530 *active_cursor = 0;
25532 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25533 return NO_CURSOR;
25535 non_selected = 1;
25538 /* Never display a cursor in a window in which cursor-type is nil. */
25539 if (NILP (BVAR (b, cursor_type)))
25540 return NO_CURSOR;
25542 /* Get the normal cursor type for this window. */
25543 if (EQ (BVAR (b, cursor_type), Qt))
25545 cursor_type = FRAME_DESIRED_CURSOR (f);
25546 *width = FRAME_CURSOR_WIDTH (f);
25548 else
25549 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25551 /* Use cursor-in-non-selected-windows instead
25552 for non-selected window or frame. */
25553 if (non_selected)
25555 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25556 if (!EQ (Qt, alt_cursor))
25557 return get_specified_cursor_type (alt_cursor, width);
25558 /* t means modify the normal cursor type. */
25559 if (cursor_type == FILLED_BOX_CURSOR)
25560 cursor_type = HOLLOW_BOX_CURSOR;
25561 else if (cursor_type == BAR_CURSOR && *width > 1)
25562 --*width;
25563 return cursor_type;
25566 /* Use normal cursor if not blinked off. */
25567 if (!w->cursor_off_p)
25569 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25571 if (cursor_type == FILLED_BOX_CURSOR)
25573 /* Using a block cursor on large images can be very annoying.
25574 So use a hollow cursor for "large" images.
25575 If image is not transparent (no mask), also use hollow cursor. */
25576 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25577 if (img != NULL && IMAGEP (img->spec))
25579 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25580 where N = size of default frame font size.
25581 This should cover most of the "tiny" icons people may use. */
25582 if (!img->mask
25583 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25584 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25585 cursor_type = HOLLOW_BOX_CURSOR;
25588 else if (cursor_type != NO_CURSOR)
25590 /* Display current only supports BOX and HOLLOW cursors for images.
25591 So for now, unconditionally use a HOLLOW cursor when cursor is
25592 not a solid box cursor. */
25593 cursor_type = HOLLOW_BOX_CURSOR;
25596 return cursor_type;
25599 /* Cursor is blinked off, so determine how to "toggle" it. */
25601 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25602 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25603 return get_specified_cursor_type (XCDR (alt_cursor), width);
25605 /* Then see if frame has specified a specific blink off cursor type. */
25606 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25608 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25609 return FRAME_BLINK_OFF_CURSOR (f);
25612 #if 0
25613 /* Some people liked having a permanently visible blinking cursor,
25614 while others had very strong opinions against it. So it was
25615 decided to remove it. KFS 2003-09-03 */
25617 /* Finally perform built-in cursor blinking:
25618 filled box <-> hollow box
25619 wide [h]bar <-> narrow [h]bar
25620 narrow [h]bar <-> no cursor
25621 other type <-> no cursor */
25623 if (cursor_type == FILLED_BOX_CURSOR)
25624 return HOLLOW_BOX_CURSOR;
25626 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25628 *width = 1;
25629 return cursor_type;
25631 #endif
25633 return NO_CURSOR;
25637 /* Notice when the text cursor of window W has been completely
25638 overwritten by a drawing operation that outputs glyphs in AREA
25639 starting at X0 and ending at X1 in the line starting at Y0 and
25640 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25641 the rest of the line after X0 has been written. Y coordinates
25642 are window-relative. */
25644 static void
25645 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25646 int x0, int x1, int y0, int y1)
25648 int cx0, cx1, cy0, cy1;
25649 struct glyph_row *row;
25651 if (!w->phys_cursor_on_p)
25652 return;
25653 if (area != TEXT_AREA)
25654 return;
25656 if (w->phys_cursor.vpos < 0
25657 || w->phys_cursor.vpos >= w->current_matrix->nrows
25658 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25659 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
25660 return;
25662 if (row->cursor_in_fringe_p)
25664 row->cursor_in_fringe_p = 0;
25665 draw_fringe_bitmap (w, row, row->reversed_p);
25666 w->phys_cursor_on_p = 0;
25667 return;
25670 cx0 = w->phys_cursor.x;
25671 cx1 = cx0 + w->phys_cursor_width;
25672 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25673 return;
25675 /* The cursor image will be completely removed from the
25676 screen if the output area intersects the cursor area in
25677 y-direction. When we draw in [y0 y1[, and some part of
25678 the cursor is at y < y0, that part must have been drawn
25679 before. When scrolling, the cursor is erased before
25680 actually scrolling, so we don't come here. When not
25681 scrolling, the rows above the old cursor row must have
25682 changed, and in this case these rows must have written
25683 over the cursor image.
25685 Likewise if part of the cursor is below y1, with the
25686 exception of the cursor being in the first blank row at
25687 the buffer and window end because update_text_area
25688 doesn't draw that row. (Except when it does, but
25689 that's handled in update_text_area.) */
25691 cy0 = w->phys_cursor.y;
25692 cy1 = cy0 + w->phys_cursor_height;
25693 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25694 return;
25696 w->phys_cursor_on_p = 0;
25699 #endif /* HAVE_WINDOW_SYSTEM */
25702 /************************************************************************
25703 Mouse Face
25704 ************************************************************************/
25706 #ifdef HAVE_WINDOW_SYSTEM
25708 /* EXPORT for RIF:
25709 Fix the display of area AREA of overlapping row ROW in window W
25710 with respect to the overlapping part OVERLAPS. */
25712 void
25713 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25714 enum glyph_row_area area, int overlaps)
25716 int i, x;
25718 block_input ();
25720 x = 0;
25721 for (i = 0; i < row->used[area];)
25723 if (row->glyphs[area][i].overlaps_vertically_p)
25725 int start = i, start_x = x;
25729 x += row->glyphs[area][i].pixel_width;
25730 ++i;
25732 while (i < row->used[area]
25733 && row->glyphs[area][i].overlaps_vertically_p);
25735 draw_glyphs (w, start_x, row, area,
25736 start, i,
25737 DRAW_NORMAL_TEXT, overlaps);
25739 else
25741 x += row->glyphs[area][i].pixel_width;
25742 ++i;
25746 unblock_input ();
25750 /* EXPORT:
25751 Draw the cursor glyph of window W in glyph row ROW. See the
25752 comment of draw_glyphs for the meaning of HL. */
25754 void
25755 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25756 enum draw_glyphs_face hl)
25758 /* If cursor hpos is out of bounds, don't draw garbage. This can
25759 happen in mini-buffer windows when switching between echo area
25760 glyphs and mini-buffer. */
25761 if ((row->reversed_p
25762 ? (w->phys_cursor.hpos >= 0)
25763 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25765 int on_p = w->phys_cursor_on_p;
25766 int x1;
25767 int hpos = w->phys_cursor.hpos;
25769 /* When the window is hscrolled, cursor hpos can legitimately be
25770 out of bounds, but we draw the cursor at the corresponding
25771 window margin in that case. */
25772 if (!row->reversed_p && hpos < 0)
25773 hpos = 0;
25774 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25775 hpos = row->used[TEXT_AREA] - 1;
25777 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25778 hl, 0);
25779 w->phys_cursor_on_p = on_p;
25781 if (hl == DRAW_CURSOR)
25782 w->phys_cursor_width = x1 - w->phys_cursor.x;
25783 /* When we erase the cursor, and ROW is overlapped by other
25784 rows, make sure that these overlapping parts of other rows
25785 are redrawn. */
25786 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25788 w->phys_cursor_width = x1 - w->phys_cursor.x;
25790 if (row > w->current_matrix->rows
25791 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25792 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25793 OVERLAPS_ERASED_CURSOR);
25795 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25796 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25797 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25798 OVERLAPS_ERASED_CURSOR);
25804 /* EXPORT:
25805 Erase the image of a cursor of window W from the screen. */
25807 void
25808 erase_phys_cursor (struct window *w)
25810 struct frame *f = XFRAME (w->frame);
25811 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25812 int hpos = w->phys_cursor.hpos;
25813 int vpos = w->phys_cursor.vpos;
25814 int mouse_face_here_p = 0;
25815 struct glyph_matrix *active_glyphs = w->current_matrix;
25816 struct glyph_row *cursor_row;
25817 struct glyph *cursor_glyph;
25818 enum draw_glyphs_face hl;
25820 /* No cursor displayed or row invalidated => nothing to do on the
25821 screen. */
25822 if (w->phys_cursor_type == NO_CURSOR)
25823 goto mark_cursor_off;
25825 /* VPOS >= active_glyphs->nrows means that window has been resized.
25826 Don't bother to erase the cursor. */
25827 if (vpos >= active_glyphs->nrows)
25828 goto mark_cursor_off;
25830 /* If row containing cursor is marked invalid, there is nothing we
25831 can do. */
25832 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25833 if (!cursor_row->enabled_p)
25834 goto mark_cursor_off;
25836 /* If line spacing is > 0, old cursor may only be partially visible in
25837 window after split-window. So adjust visible height. */
25838 cursor_row->visible_height = min (cursor_row->visible_height,
25839 window_text_bottom_y (w) - cursor_row->y);
25841 /* If row is completely invisible, don't attempt to delete a cursor which
25842 isn't there. This can happen if cursor is at top of a window, and
25843 we switch to a buffer with a header line in that window. */
25844 if (cursor_row->visible_height <= 0)
25845 goto mark_cursor_off;
25847 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25848 if (cursor_row->cursor_in_fringe_p)
25850 cursor_row->cursor_in_fringe_p = 0;
25851 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25852 goto mark_cursor_off;
25855 /* This can happen when the new row is shorter than the old one.
25856 In this case, either draw_glyphs or clear_end_of_line
25857 should have cleared the cursor. Note that we wouldn't be
25858 able to erase the cursor in this case because we don't have a
25859 cursor glyph at hand. */
25860 if ((cursor_row->reversed_p
25861 ? (w->phys_cursor.hpos < 0)
25862 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25863 goto mark_cursor_off;
25865 /* When the window is hscrolled, cursor hpos can legitimately be out
25866 of bounds, but we draw the cursor at the corresponding window
25867 margin in that case. */
25868 if (!cursor_row->reversed_p && hpos < 0)
25869 hpos = 0;
25870 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25871 hpos = cursor_row->used[TEXT_AREA] - 1;
25873 /* If the cursor is in the mouse face area, redisplay that when
25874 we clear the cursor. */
25875 if (! NILP (hlinfo->mouse_face_window)
25876 && coords_in_mouse_face_p (w, hpos, vpos)
25877 /* Don't redraw the cursor's spot in mouse face if it is at the
25878 end of a line (on a newline). The cursor appears there, but
25879 mouse highlighting does not. */
25880 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25881 mouse_face_here_p = 1;
25883 /* Maybe clear the display under the cursor. */
25884 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25886 int x, y, left_x;
25887 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25888 int width;
25890 cursor_glyph = get_phys_cursor_glyph (w);
25891 if (cursor_glyph == NULL)
25892 goto mark_cursor_off;
25894 width = cursor_glyph->pixel_width;
25895 left_x = window_box_left_offset (w, TEXT_AREA);
25896 x = w->phys_cursor.x;
25897 if (x < left_x)
25898 width -= left_x - x;
25899 width = min (width, window_box_width (w, TEXT_AREA) - x);
25900 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25901 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25903 if (width > 0)
25904 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25907 /* Erase the cursor by redrawing the character underneath it. */
25908 if (mouse_face_here_p)
25909 hl = DRAW_MOUSE_FACE;
25910 else
25911 hl = DRAW_NORMAL_TEXT;
25912 draw_phys_cursor_glyph (w, cursor_row, hl);
25914 mark_cursor_off:
25915 w->phys_cursor_on_p = 0;
25916 w->phys_cursor_type = NO_CURSOR;
25920 /* EXPORT:
25921 Display or clear cursor of window W. If ON is zero, clear the
25922 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25923 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25925 void
25926 display_and_set_cursor (struct window *w, int on,
25927 int hpos, int vpos, int x, int y)
25929 struct frame *f = XFRAME (w->frame);
25930 int new_cursor_type;
25931 int new_cursor_width;
25932 int active_cursor;
25933 struct glyph_row *glyph_row;
25934 struct glyph *glyph;
25936 /* This is pointless on invisible frames, and dangerous on garbaged
25937 windows and frames; in the latter case, the frame or window may
25938 be in the midst of changing its size, and x and y may be off the
25939 window. */
25940 if (! FRAME_VISIBLE_P (f)
25941 || FRAME_GARBAGED_P (f)
25942 || vpos >= w->current_matrix->nrows
25943 || hpos >= w->current_matrix->matrix_w)
25944 return;
25946 /* If cursor is off and we want it off, return quickly. */
25947 if (!on && !w->phys_cursor_on_p)
25948 return;
25950 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25951 /* If cursor row is not enabled, we don't really know where to
25952 display the cursor. */
25953 if (!glyph_row->enabled_p)
25955 w->phys_cursor_on_p = 0;
25956 return;
25959 glyph = NULL;
25960 if (!glyph_row->exact_window_width_line_p
25961 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25962 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25964 eassert (input_blocked_p ());
25966 /* Set new_cursor_type to the cursor we want to be displayed. */
25967 new_cursor_type = get_window_cursor_type (w, glyph,
25968 &new_cursor_width, &active_cursor);
25970 /* If cursor is currently being shown and we don't want it to be or
25971 it is in the wrong place, or the cursor type is not what we want,
25972 erase it. */
25973 if (w->phys_cursor_on_p
25974 && (!on
25975 || w->phys_cursor.x != x
25976 || w->phys_cursor.y != y
25977 || new_cursor_type != w->phys_cursor_type
25978 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25979 && new_cursor_width != w->phys_cursor_width)))
25980 erase_phys_cursor (w);
25982 /* Don't check phys_cursor_on_p here because that flag is only set
25983 to zero in some cases where we know that the cursor has been
25984 completely erased, to avoid the extra work of erasing the cursor
25985 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25986 still not be visible, or it has only been partly erased. */
25987 if (on)
25989 w->phys_cursor_ascent = glyph_row->ascent;
25990 w->phys_cursor_height = glyph_row->height;
25992 /* Set phys_cursor_.* before x_draw_.* is called because some
25993 of them may need the information. */
25994 w->phys_cursor.x = x;
25995 w->phys_cursor.y = glyph_row->y;
25996 w->phys_cursor.hpos = hpos;
25997 w->phys_cursor.vpos = vpos;
26000 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26001 new_cursor_type, new_cursor_width,
26002 on, active_cursor);
26006 /* Switch the display of W's cursor on or off, according to the value
26007 of ON. */
26009 static void
26010 update_window_cursor (struct window *w, int on)
26012 /* Don't update cursor in windows whose frame is in the process
26013 of being deleted. */
26014 if (w->current_matrix)
26016 int hpos = w->phys_cursor.hpos;
26017 int vpos = w->phys_cursor.vpos;
26018 struct glyph_row *row;
26020 if (vpos >= w->current_matrix->nrows
26021 || hpos >= w->current_matrix->matrix_w)
26022 return;
26024 row = MATRIX_ROW (w->current_matrix, vpos);
26026 /* When the window is hscrolled, cursor hpos can legitimately be
26027 out of bounds, but we draw the cursor at the corresponding
26028 window margin in that case. */
26029 if (!row->reversed_p && hpos < 0)
26030 hpos = 0;
26031 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26032 hpos = row->used[TEXT_AREA] - 1;
26034 block_input ();
26035 display_and_set_cursor (w, on, hpos, vpos,
26036 w->phys_cursor.x, w->phys_cursor.y);
26037 unblock_input ();
26042 /* Call update_window_cursor with parameter ON_P on all leaf windows
26043 in the window tree rooted at W. */
26045 static void
26046 update_cursor_in_window_tree (struct window *w, int on_p)
26048 while (w)
26050 if (WINDOWP (w->contents))
26051 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26052 else
26053 update_window_cursor (w, on_p);
26055 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26060 /* EXPORT:
26061 Display the cursor on window W, or clear it, according to ON_P.
26062 Don't change the cursor's position. */
26064 void
26065 x_update_cursor (struct frame *f, int on_p)
26067 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26071 /* EXPORT:
26072 Clear the cursor of window W to background color, and mark the
26073 cursor as not shown. This is used when the text where the cursor
26074 is about to be rewritten. */
26076 void
26077 x_clear_cursor (struct window *w)
26079 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26080 update_window_cursor (w, 0);
26083 #endif /* HAVE_WINDOW_SYSTEM */
26085 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26086 and MSDOS. */
26087 static void
26088 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26089 int start_hpos, int end_hpos,
26090 enum draw_glyphs_face draw)
26092 #ifdef HAVE_WINDOW_SYSTEM
26093 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26095 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26096 return;
26098 #endif
26099 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26100 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26101 #endif
26104 /* Display the active region described by mouse_face_* according to DRAW. */
26106 static void
26107 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26109 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26110 struct frame *f = XFRAME (WINDOW_FRAME (w));
26112 if (/* If window is in the process of being destroyed, don't bother
26113 to do anything. */
26114 w->current_matrix != NULL
26115 /* Don't update mouse highlight if hidden */
26116 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26117 /* Recognize when we are called to operate on rows that don't exist
26118 anymore. This can happen when a window is split. */
26119 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26121 int phys_cursor_on_p = w->phys_cursor_on_p;
26122 struct glyph_row *row, *first, *last;
26124 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26125 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26127 for (row = first; row <= last && row->enabled_p; ++row)
26129 int start_hpos, end_hpos, start_x;
26131 /* For all but the first row, the highlight starts at column 0. */
26132 if (row == first)
26134 /* R2L rows have BEG and END in reversed order, but the
26135 screen drawing geometry is always left to right. So
26136 we need to mirror the beginning and end of the
26137 highlighted area in R2L rows. */
26138 if (!row->reversed_p)
26140 start_hpos = hlinfo->mouse_face_beg_col;
26141 start_x = hlinfo->mouse_face_beg_x;
26143 else if (row == last)
26145 start_hpos = hlinfo->mouse_face_end_col;
26146 start_x = hlinfo->mouse_face_end_x;
26148 else
26150 start_hpos = 0;
26151 start_x = 0;
26154 else if (row->reversed_p && row == last)
26156 start_hpos = hlinfo->mouse_face_end_col;
26157 start_x = hlinfo->mouse_face_end_x;
26159 else
26161 start_hpos = 0;
26162 start_x = 0;
26165 if (row == last)
26167 if (!row->reversed_p)
26168 end_hpos = hlinfo->mouse_face_end_col;
26169 else if (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 else if (row->reversed_p && row == first)
26179 end_hpos = hlinfo->mouse_face_beg_col;
26180 else
26182 end_hpos = row->used[TEXT_AREA];
26183 if (draw == DRAW_NORMAL_TEXT)
26184 row->fill_line_p = 1; /* Clear to end of line */
26187 if (end_hpos > start_hpos)
26189 draw_row_with_mouse_face (w, start_x, row,
26190 start_hpos, end_hpos, draw);
26192 row->mouse_face_p
26193 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26197 #ifdef HAVE_WINDOW_SYSTEM
26198 /* When we've written over the cursor, arrange for it to
26199 be displayed again. */
26200 if (FRAME_WINDOW_P (f)
26201 && phys_cursor_on_p && !w->phys_cursor_on_p)
26203 int hpos = w->phys_cursor.hpos;
26205 /* When the window is hscrolled, cursor hpos can legitimately be
26206 out of bounds, but we draw the cursor at the corresponding
26207 window margin in that case. */
26208 if (!row->reversed_p && hpos < 0)
26209 hpos = 0;
26210 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26211 hpos = row->used[TEXT_AREA] - 1;
26213 block_input ();
26214 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26215 w->phys_cursor.x, w->phys_cursor.y);
26216 unblock_input ();
26218 #endif /* HAVE_WINDOW_SYSTEM */
26221 #ifdef HAVE_WINDOW_SYSTEM
26222 /* Change the mouse cursor. */
26223 if (FRAME_WINDOW_P (f))
26225 if (draw == DRAW_NORMAL_TEXT
26226 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26227 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26228 else if (draw == DRAW_MOUSE_FACE)
26229 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26230 else
26231 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26233 #endif /* HAVE_WINDOW_SYSTEM */
26236 /* EXPORT:
26237 Clear out the mouse-highlighted active region.
26238 Redraw it un-highlighted first. Value is non-zero if mouse
26239 face was actually drawn unhighlighted. */
26242 clear_mouse_face (Mouse_HLInfo *hlinfo)
26244 int cleared = 0;
26246 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26248 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26249 cleared = 1;
26252 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26253 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26254 hlinfo->mouse_face_window = Qnil;
26255 hlinfo->mouse_face_overlay = Qnil;
26256 return cleared;
26259 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26260 within the mouse face on that window. */
26261 static int
26262 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26264 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26266 /* Quickly resolve the easy cases. */
26267 if (!(WINDOWP (hlinfo->mouse_face_window)
26268 && XWINDOW (hlinfo->mouse_face_window) == w))
26269 return 0;
26270 if (vpos < hlinfo->mouse_face_beg_row
26271 || vpos > hlinfo->mouse_face_end_row)
26272 return 0;
26273 if (vpos > hlinfo->mouse_face_beg_row
26274 && vpos < hlinfo->mouse_face_end_row)
26275 return 1;
26277 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26279 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26281 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26282 return 1;
26284 else if ((vpos == hlinfo->mouse_face_beg_row
26285 && hpos >= hlinfo->mouse_face_beg_col)
26286 || (vpos == hlinfo->mouse_face_end_row
26287 && hpos < hlinfo->mouse_face_end_col))
26288 return 1;
26290 else
26292 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26294 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26295 return 1;
26297 else if ((vpos == hlinfo->mouse_face_beg_row
26298 && hpos <= hlinfo->mouse_face_beg_col)
26299 || (vpos == hlinfo->mouse_face_end_row
26300 && hpos > hlinfo->mouse_face_end_col))
26301 return 1;
26303 return 0;
26307 /* EXPORT:
26308 Non-zero if physical cursor of window W is within mouse face. */
26311 cursor_in_mouse_face_p (struct window *w)
26313 int hpos = w->phys_cursor.hpos;
26314 int vpos = w->phys_cursor.vpos;
26315 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26317 /* When the window is hscrolled, cursor hpos can legitimately be out
26318 of bounds, but we draw the cursor at the corresponding window
26319 margin in that case. */
26320 if (!row->reversed_p && hpos < 0)
26321 hpos = 0;
26322 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26323 hpos = row->used[TEXT_AREA] - 1;
26325 return coords_in_mouse_face_p (w, hpos, vpos);
26330 /* Find the glyph rows START_ROW and END_ROW of window W that display
26331 characters between buffer positions START_CHARPOS and END_CHARPOS
26332 (excluding END_CHARPOS). DISP_STRING is a display string that
26333 covers these buffer positions. This is similar to
26334 row_containing_pos, but is more accurate when bidi reordering makes
26335 buffer positions change non-linearly with glyph rows. */
26336 static void
26337 rows_from_pos_range (struct window *w,
26338 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26339 Lisp_Object disp_string,
26340 struct glyph_row **start, struct glyph_row **end)
26342 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26343 int last_y = window_text_bottom_y (w);
26344 struct glyph_row *row;
26346 *start = NULL;
26347 *end = NULL;
26349 while (!first->enabled_p
26350 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26351 first++;
26353 /* Find the START row. */
26354 for (row = first;
26355 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26356 row++)
26358 /* A row can potentially be the START row if the range of the
26359 characters it displays intersects the range
26360 [START_CHARPOS..END_CHARPOS). */
26361 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26362 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26363 /* See the commentary in row_containing_pos, for the
26364 explanation of the complicated way to check whether
26365 some position is beyond the end of the characters
26366 displayed by a row. */
26367 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26368 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26369 && !row->ends_at_zv_p
26370 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26371 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26372 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26373 && !row->ends_at_zv_p
26374 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26376 /* Found a candidate row. Now make sure at least one of the
26377 glyphs it displays has a charpos from the range
26378 [START_CHARPOS..END_CHARPOS).
26380 This is not obvious because bidi reordering could make
26381 buffer positions of a row be 1,2,3,102,101,100, and if we
26382 want to highlight characters in [50..60), we don't want
26383 this row, even though [50..60) does intersect [1..103),
26384 the range of character positions given by the row's start
26385 and end positions. */
26386 struct glyph *g = row->glyphs[TEXT_AREA];
26387 struct glyph *e = g + row->used[TEXT_AREA];
26389 while (g < e)
26391 if (((BUFFERP (g->object) || INTEGERP (g->object))
26392 && start_charpos <= g->charpos && g->charpos < end_charpos)
26393 /* A glyph that comes from DISP_STRING is by
26394 definition to be highlighted. */
26395 || EQ (g->object, disp_string))
26396 *start = row;
26397 g++;
26399 if (*start)
26400 break;
26404 /* Find the END row. */
26405 if (!*start
26406 /* If the last row is partially visible, start looking for END
26407 from that row, instead of starting from FIRST. */
26408 && !(row->enabled_p
26409 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26410 row = first;
26411 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26413 struct glyph_row *next = row + 1;
26414 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26416 if (!next->enabled_p
26417 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26418 /* The first row >= START whose range of displayed characters
26419 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26420 is the row END + 1. */
26421 || (start_charpos < next_start
26422 && end_charpos < next_start)
26423 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26424 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26425 && !next->ends_at_zv_p
26426 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26427 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26428 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26429 && !next->ends_at_zv_p
26430 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26432 *end = row;
26433 break;
26435 else
26437 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26438 but none of the characters it displays are in the range, it is
26439 also END + 1. */
26440 struct glyph *g = next->glyphs[TEXT_AREA];
26441 struct glyph *s = g;
26442 struct glyph *e = g + next->used[TEXT_AREA];
26444 while (g < e)
26446 if (((BUFFERP (g->object) || INTEGERP (g->object))
26447 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26448 /* If the buffer position of the first glyph in
26449 the row is equal to END_CHARPOS, it means
26450 the last character to be highlighted is the
26451 newline of ROW, and we must consider NEXT as
26452 END, not END+1. */
26453 || (((!next->reversed_p && g == s)
26454 || (next->reversed_p && g == e - 1))
26455 && (g->charpos == end_charpos
26456 /* Special case for when NEXT is an
26457 empty line at ZV. */
26458 || (g->charpos == -1
26459 && !row->ends_at_zv_p
26460 && next_start == end_charpos)))))
26461 /* A glyph that comes from DISP_STRING is by
26462 definition to be highlighted. */
26463 || EQ (g->object, disp_string))
26464 break;
26465 g++;
26467 if (g == e)
26469 *end = row;
26470 break;
26472 /* The first row that ends at ZV must be the last to be
26473 highlighted. */
26474 else if (next->ends_at_zv_p)
26476 *end = next;
26477 break;
26483 /* This function sets the mouse_face_* elements of HLINFO, assuming
26484 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26485 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26486 for the overlay or run of text properties specifying the mouse
26487 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26488 before-string and after-string that must also be highlighted.
26489 DISP_STRING, if non-nil, is a display string that may cover some
26490 or all of the highlighted text. */
26492 static void
26493 mouse_face_from_buffer_pos (Lisp_Object window,
26494 Mouse_HLInfo *hlinfo,
26495 ptrdiff_t mouse_charpos,
26496 ptrdiff_t start_charpos,
26497 ptrdiff_t end_charpos,
26498 Lisp_Object before_string,
26499 Lisp_Object after_string,
26500 Lisp_Object disp_string)
26502 struct window *w = XWINDOW (window);
26503 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26504 struct glyph_row *r1, *r2;
26505 struct glyph *glyph, *end;
26506 ptrdiff_t ignore, pos;
26507 int x;
26509 eassert (NILP (disp_string) || STRINGP (disp_string));
26510 eassert (NILP (before_string) || STRINGP (before_string));
26511 eassert (NILP (after_string) || STRINGP (after_string));
26513 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26514 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26515 if (r1 == NULL)
26516 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26517 /* If the before-string or display-string contains newlines,
26518 rows_from_pos_range skips to its last row. Move back. */
26519 if (!NILP (before_string) || !NILP (disp_string))
26521 struct glyph_row *prev;
26522 while ((prev = r1 - 1, prev >= first)
26523 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26524 && prev->used[TEXT_AREA] > 0)
26526 struct glyph *beg = prev->glyphs[TEXT_AREA];
26527 glyph = beg + prev->used[TEXT_AREA];
26528 while (--glyph >= beg && INTEGERP (glyph->object));
26529 if (glyph < beg
26530 || !(EQ (glyph->object, before_string)
26531 || EQ (glyph->object, disp_string)))
26532 break;
26533 r1 = prev;
26536 if (r2 == NULL)
26538 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26539 hlinfo->mouse_face_past_end = 1;
26541 else if (!NILP (after_string))
26543 /* If the after-string has newlines, advance to its last row. */
26544 struct glyph_row *next;
26545 struct glyph_row *last
26546 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26548 for (next = r2 + 1;
26549 next <= last
26550 && next->used[TEXT_AREA] > 0
26551 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26552 ++next)
26553 r2 = next;
26555 /* The rest of the display engine assumes that mouse_face_beg_row is
26556 either above mouse_face_end_row or identical to it. But with
26557 bidi-reordered continued lines, the row for START_CHARPOS could
26558 be below the row for END_CHARPOS. If so, swap the rows and store
26559 them in correct order. */
26560 if (r1->y > r2->y)
26562 struct glyph_row *tem = r2;
26564 r2 = r1;
26565 r1 = tem;
26568 hlinfo->mouse_face_beg_y = r1->y;
26569 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26570 hlinfo->mouse_face_end_y = r2->y;
26571 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26573 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26574 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26575 could be anywhere in the row and in any order. The strategy
26576 below is to find the leftmost and the rightmost glyph that
26577 belongs to either of these 3 strings, or whose position is
26578 between START_CHARPOS and END_CHARPOS, and highlight all the
26579 glyphs between those two. This may cover more than just the text
26580 between START_CHARPOS and END_CHARPOS if the range of characters
26581 strides the bidi level boundary, e.g. if the beginning is in R2L
26582 text while the end is in L2R text or vice versa. */
26583 if (!r1->reversed_p)
26585 /* This row is in a left to right paragraph. Scan it left to
26586 right. */
26587 glyph = r1->glyphs[TEXT_AREA];
26588 end = glyph + r1->used[TEXT_AREA];
26589 x = r1->x;
26591 /* Skip truncation glyphs at the start of the glyph row. */
26592 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
26593 for (; glyph < end
26594 && INTEGERP (glyph->object)
26595 && glyph->charpos < 0;
26596 ++glyph)
26597 x += glyph->pixel_width;
26599 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26600 or DISP_STRING, and the first glyph from buffer whose
26601 position is between START_CHARPOS and END_CHARPOS. */
26602 for (; glyph < end
26603 && !INTEGERP (glyph->object)
26604 && !EQ (glyph->object, disp_string)
26605 && !(BUFFERP (glyph->object)
26606 && (glyph->charpos >= start_charpos
26607 && glyph->charpos < end_charpos));
26608 ++glyph)
26610 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26611 are present at buffer positions between START_CHARPOS and
26612 END_CHARPOS, or if they come from an overlay. */
26613 if (EQ (glyph->object, before_string))
26615 pos = string_buffer_position (before_string,
26616 start_charpos);
26617 /* If pos == 0, it means before_string came from an
26618 overlay, not from a buffer position. */
26619 if (!pos || (pos >= start_charpos && pos < end_charpos))
26620 break;
26622 else if (EQ (glyph->object, after_string))
26624 pos = string_buffer_position (after_string, end_charpos);
26625 if (!pos || (pos >= start_charpos && pos < end_charpos))
26626 break;
26628 x += glyph->pixel_width;
26630 hlinfo->mouse_face_beg_x = x;
26631 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26633 else
26635 /* This row is in a right to left paragraph. Scan it right to
26636 left. */
26637 struct glyph *g;
26639 end = r1->glyphs[TEXT_AREA] - 1;
26640 glyph = end + r1->used[TEXT_AREA];
26642 /* Skip truncation glyphs at the start of the glyph row. */
26643 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
26644 for (; glyph > end
26645 && INTEGERP (glyph->object)
26646 && glyph->charpos < 0;
26647 --glyph)
26650 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26651 or DISP_STRING, and the first glyph from buffer whose
26652 position is between START_CHARPOS and END_CHARPOS. */
26653 for (; glyph > end
26654 && !INTEGERP (glyph->object)
26655 && !EQ (glyph->object, disp_string)
26656 && !(BUFFERP (glyph->object)
26657 && (glyph->charpos >= start_charpos
26658 && glyph->charpos < end_charpos));
26659 --glyph)
26661 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26662 are present at buffer positions between START_CHARPOS and
26663 END_CHARPOS, or if they come from an overlay. */
26664 if (EQ (glyph->object, before_string))
26666 pos = string_buffer_position (before_string, start_charpos);
26667 /* If pos == 0, it means before_string came from an
26668 overlay, not from a buffer position. */
26669 if (!pos || (pos >= start_charpos && pos < end_charpos))
26670 break;
26672 else if (EQ (glyph->object, after_string))
26674 pos = string_buffer_position (after_string, end_charpos);
26675 if (!pos || (pos >= start_charpos && pos < end_charpos))
26676 break;
26680 glyph++; /* first glyph to the right of the highlighted area */
26681 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26682 x += g->pixel_width;
26683 hlinfo->mouse_face_beg_x = x;
26684 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26687 /* If the highlight ends in a different row, compute GLYPH and END
26688 for the end row. Otherwise, reuse the values computed above for
26689 the row where the highlight begins. */
26690 if (r2 != r1)
26692 if (!r2->reversed_p)
26694 glyph = r2->glyphs[TEXT_AREA];
26695 end = glyph + r2->used[TEXT_AREA];
26696 x = r2->x;
26698 else
26700 end = r2->glyphs[TEXT_AREA] - 1;
26701 glyph = end + r2->used[TEXT_AREA];
26705 if (!r2->reversed_p)
26707 /* Skip truncation and continuation glyphs near the end of the
26708 row, and also blanks and stretch glyphs inserted by
26709 extend_face_to_end_of_line. */
26710 while (end > glyph
26711 && INTEGERP ((end - 1)->object))
26712 --end;
26713 /* Scan the rest of the glyph row from the end, looking for the
26714 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26715 DISP_STRING, or whose position is between START_CHARPOS
26716 and END_CHARPOS */
26717 for (--end;
26718 end > glyph
26719 && !INTEGERP (end->object)
26720 && !EQ (end->object, disp_string)
26721 && !(BUFFERP (end->object)
26722 && (end->charpos >= start_charpos
26723 && end->charpos < end_charpos));
26724 --end)
26726 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26727 are present at buffer positions between START_CHARPOS and
26728 END_CHARPOS, or if they come from an overlay. */
26729 if (EQ (end->object, before_string))
26731 pos = string_buffer_position (before_string, start_charpos);
26732 if (!pos || (pos >= start_charpos && pos < end_charpos))
26733 break;
26735 else if (EQ (end->object, after_string))
26737 pos = string_buffer_position (after_string, end_charpos);
26738 if (!pos || (pos >= start_charpos && pos < end_charpos))
26739 break;
26742 /* Find the X coordinate of the last glyph to be highlighted. */
26743 for (; glyph <= end; ++glyph)
26744 x += glyph->pixel_width;
26746 hlinfo->mouse_face_end_x = x;
26747 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26749 else
26751 /* Skip truncation and continuation glyphs near the end of the
26752 row, and also blanks and stretch glyphs inserted by
26753 extend_face_to_end_of_line. */
26754 x = r2->x;
26755 end++;
26756 while (end < glyph
26757 && INTEGERP (end->object))
26759 x += end->pixel_width;
26760 ++end;
26762 /* Scan the rest of the glyph row from the end, looking for the
26763 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26764 DISP_STRING, or whose position is between START_CHARPOS
26765 and END_CHARPOS */
26766 for ( ;
26767 end < glyph
26768 && !INTEGERP (end->object)
26769 && !EQ (end->object, disp_string)
26770 && !(BUFFERP (end->object)
26771 && (end->charpos >= start_charpos
26772 && end->charpos < end_charpos));
26773 ++end)
26775 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26776 are present at buffer positions between START_CHARPOS and
26777 END_CHARPOS, or if they come from an overlay. */
26778 if (EQ (end->object, before_string))
26780 pos = string_buffer_position (before_string, start_charpos);
26781 if (!pos || (pos >= start_charpos && pos < end_charpos))
26782 break;
26784 else if (EQ (end->object, after_string))
26786 pos = string_buffer_position (after_string, end_charpos);
26787 if (!pos || (pos >= start_charpos && pos < end_charpos))
26788 break;
26790 x += end->pixel_width;
26792 /* If we exited the above loop because we arrived at the last
26793 glyph of the row, and its buffer position is still not in
26794 range, it means the last character in range is the preceding
26795 newline. Bump the end column and x values to get past the
26796 last glyph. */
26797 if (end == glyph
26798 && BUFFERP (end->object)
26799 && (end->charpos < start_charpos
26800 || end->charpos >= end_charpos))
26802 x += end->pixel_width;
26803 ++end;
26805 hlinfo->mouse_face_end_x = x;
26806 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26809 hlinfo->mouse_face_window = window;
26810 hlinfo->mouse_face_face_id
26811 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26812 mouse_charpos + 1,
26813 !hlinfo->mouse_face_hidden, -1);
26814 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26817 /* The following function is not used anymore (replaced with
26818 mouse_face_from_string_pos), but I leave it here for the time
26819 being, in case someone would. */
26821 #if 0 /* not used */
26823 /* Find the position of the glyph for position POS in OBJECT in
26824 window W's current matrix, and return in *X, *Y the pixel
26825 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26827 RIGHT_P non-zero means return the position of the right edge of the
26828 glyph, RIGHT_P zero means return the left edge position.
26830 If no glyph for POS exists in the matrix, return the position of
26831 the glyph with the next smaller position that is in the matrix, if
26832 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26833 exists in the matrix, return the position of the glyph with the
26834 next larger position in OBJECT.
26836 Value is non-zero if a glyph was found. */
26838 static int
26839 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26840 int *hpos, int *vpos, int *x, int *y, int right_p)
26842 int yb = window_text_bottom_y (w);
26843 struct glyph_row *r;
26844 struct glyph *best_glyph = NULL;
26845 struct glyph_row *best_row = NULL;
26846 int best_x = 0;
26848 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26849 r->enabled_p && r->y < yb;
26850 ++r)
26852 struct glyph *g = r->glyphs[TEXT_AREA];
26853 struct glyph *e = g + r->used[TEXT_AREA];
26854 int gx;
26856 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26857 if (EQ (g->object, object))
26859 if (g->charpos == pos)
26861 best_glyph = g;
26862 best_x = gx;
26863 best_row = r;
26864 goto found;
26866 else if (best_glyph == NULL
26867 || ((eabs (g->charpos - pos)
26868 < eabs (best_glyph->charpos - pos))
26869 && (right_p
26870 ? g->charpos < pos
26871 : g->charpos > pos)))
26873 best_glyph = g;
26874 best_x = gx;
26875 best_row = r;
26880 found:
26882 if (best_glyph)
26884 *x = best_x;
26885 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26887 if (right_p)
26889 *x += best_glyph->pixel_width;
26890 ++*hpos;
26893 *y = best_row->y;
26894 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
26897 return best_glyph != NULL;
26899 #endif /* not used */
26901 /* Find the positions of the first and the last glyphs in window W's
26902 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26903 (assumed to be a string), and return in HLINFO's mouse_face_*
26904 members the pixel and column/row coordinates of those glyphs. */
26906 static void
26907 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26908 Lisp_Object object,
26909 ptrdiff_t startpos, ptrdiff_t endpos)
26911 int yb = window_text_bottom_y (w);
26912 struct glyph_row *r;
26913 struct glyph *g, *e;
26914 int gx;
26915 int found = 0;
26917 /* Find the glyph row with at least one position in the range
26918 [STARTPOS..ENDPOS], and the first glyph in that row whose
26919 position belongs to that range. */
26920 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26921 r->enabled_p && r->y < yb;
26922 ++r)
26924 if (!r->reversed_p)
26926 g = r->glyphs[TEXT_AREA];
26927 e = g + r->used[TEXT_AREA];
26928 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26929 if (EQ (g->object, object)
26930 && startpos <= g->charpos && g->charpos <= endpos)
26932 hlinfo->mouse_face_beg_row
26933 = MATRIX_ROW_VPOS (r, w->current_matrix);
26934 hlinfo->mouse_face_beg_y = r->y;
26935 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26936 hlinfo->mouse_face_beg_x = gx;
26937 found = 1;
26938 break;
26941 else
26943 struct glyph *g1;
26945 e = r->glyphs[TEXT_AREA];
26946 g = e + r->used[TEXT_AREA];
26947 for ( ; g > e; --g)
26948 if (EQ ((g-1)->object, object)
26949 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26951 hlinfo->mouse_face_beg_row
26952 = MATRIX_ROW_VPOS (r, w->current_matrix);
26953 hlinfo->mouse_face_beg_y = r->y;
26954 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26955 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26956 gx += g1->pixel_width;
26957 hlinfo->mouse_face_beg_x = gx;
26958 found = 1;
26959 break;
26962 if (found)
26963 break;
26966 if (!found)
26967 return;
26969 /* Starting with the next row, look for the first row which does NOT
26970 include any glyphs whose positions are in the range. */
26971 for (++r; r->enabled_p && r->y < yb; ++r)
26973 g = r->glyphs[TEXT_AREA];
26974 e = g + r->used[TEXT_AREA];
26975 found = 0;
26976 for ( ; g < e; ++g)
26977 if (EQ (g->object, object)
26978 && startpos <= g->charpos && g->charpos <= endpos)
26980 found = 1;
26981 break;
26983 if (!found)
26984 break;
26987 /* The highlighted region ends on the previous row. */
26988 r--;
26990 /* Set the end row and its vertical pixel coordinate. */
26991 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
26992 hlinfo->mouse_face_end_y = r->y;
26994 /* Compute and set the end column and the end column's horizontal
26995 pixel coordinate. */
26996 if (!r->reversed_p)
26998 g = r->glyphs[TEXT_AREA];
26999 e = g + r->used[TEXT_AREA];
27000 for ( ; e > g; --e)
27001 if (EQ ((e-1)->object, object)
27002 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27003 break;
27004 hlinfo->mouse_face_end_col = e - g;
27006 for (gx = r->x; g < e; ++g)
27007 gx += g->pixel_width;
27008 hlinfo->mouse_face_end_x = gx;
27010 else
27012 e = r->glyphs[TEXT_AREA];
27013 g = e + r->used[TEXT_AREA];
27014 for (gx = r->x ; e < g; ++e)
27016 if (EQ (e->object, object)
27017 && startpos <= e->charpos && e->charpos <= endpos)
27018 break;
27019 gx += e->pixel_width;
27021 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27022 hlinfo->mouse_face_end_x = gx;
27026 #ifdef HAVE_WINDOW_SYSTEM
27028 /* See if position X, Y is within a hot-spot of an image. */
27030 static int
27031 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27033 if (!CONSP (hot_spot))
27034 return 0;
27036 if (EQ (XCAR (hot_spot), Qrect))
27038 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27039 Lisp_Object rect = XCDR (hot_spot);
27040 Lisp_Object tem;
27041 if (!CONSP (rect))
27042 return 0;
27043 if (!CONSP (XCAR (rect)))
27044 return 0;
27045 if (!CONSP (XCDR (rect)))
27046 return 0;
27047 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27048 return 0;
27049 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27050 return 0;
27051 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27052 return 0;
27053 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27054 return 0;
27055 return 1;
27057 else if (EQ (XCAR (hot_spot), Qcircle))
27059 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27060 Lisp_Object circ = XCDR (hot_spot);
27061 Lisp_Object lr, lx0, ly0;
27062 if (CONSP (circ)
27063 && CONSP (XCAR (circ))
27064 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27065 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27066 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27068 double r = XFLOATINT (lr);
27069 double dx = XINT (lx0) - x;
27070 double dy = XINT (ly0) - y;
27071 return (dx * dx + dy * dy <= r * r);
27074 else if (EQ (XCAR (hot_spot), Qpoly))
27076 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27077 if (VECTORP (XCDR (hot_spot)))
27079 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27080 Lisp_Object *poly = v->contents;
27081 ptrdiff_t n = v->header.size;
27082 ptrdiff_t i;
27083 int inside = 0;
27084 Lisp_Object lx, ly;
27085 int x0, y0;
27087 /* Need an even number of coordinates, and at least 3 edges. */
27088 if (n < 6 || n & 1)
27089 return 0;
27091 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27092 If count is odd, we are inside polygon. Pixels on edges
27093 may or may not be included depending on actual geometry of the
27094 polygon. */
27095 if ((lx = poly[n-2], !INTEGERP (lx))
27096 || (ly = poly[n-1], !INTEGERP (lx)))
27097 return 0;
27098 x0 = XINT (lx), y0 = XINT (ly);
27099 for (i = 0; i < n; i += 2)
27101 int x1 = x0, y1 = y0;
27102 if ((lx = poly[i], !INTEGERP (lx))
27103 || (ly = poly[i+1], !INTEGERP (ly)))
27104 return 0;
27105 x0 = XINT (lx), y0 = XINT (ly);
27107 /* Does this segment cross the X line? */
27108 if (x0 >= x)
27110 if (x1 >= x)
27111 continue;
27113 else if (x1 < x)
27114 continue;
27115 if (y > y0 && y > y1)
27116 continue;
27117 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27118 inside = !inside;
27120 return inside;
27123 return 0;
27126 Lisp_Object
27127 find_hot_spot (Lisp_Object map, int x, int y)
27129 while (CONSP (map))
27131 if (CONSP (XCAR (map))
27132 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27133 return XCAR (map);
27134 map = XCDR (map);
27137 return Qnil;
27140 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27141 3, 3, 0,
27142 doc: /* Lookup in image map MAP coordinates X and Y.
27143 An image map is an alist where each element has the format (AREA ID PLIST).
27144 An AREA is specified as either a rectangle, a circle, or a polygon:
27145 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27146 pixel coordinates of the upper left and bottom right corners.
27147 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27148 and the radius of the circle; r may be a float or integer.
27149 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27150 vector describes one corner in the polygon.
27151 Returns the alist element for the first matching AREA in MAP. */)
27152 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27154 if (NILP (map))
27155 return Qnil;
27157 CHECK_NUMBER (x);
27158 CHECK_NUMBER (y);
27160 return find_hot_spot (map,
27161 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27162 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27166 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27167 static void
27168 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27170 /* Do not change cursor shape while dragging mouse. */
27171 if (!NILP (do_mouse_tracking))
27172 return;
27174 if (!NILP (pointer))
27176 if (EQ (pointer, Qarrow))
27177 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27178 else if (EQ (pointer, Qhand))
27179 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27180 else if (EQ (pointer, Qtext))
27181 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27182 else if (EQ (pointer, intern ("hdrag")))
27183 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27184 #ifdef HAVE_X_WINDOWS
27185 else if (EQ (pointer, intern ("vdrag")))
27186 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27187 #endif
27188 else if (EQ (pointer, intern ("hourglass")))
27189 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27190 else if (EQ (pointer, Qmodeline))
27191 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27192 else
27193 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27196 if (cursor != No_Cursor)
27197 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27200 #endif /* HAVE_WINDOW_SYSTEM */
27202 /* Take proper action when mouse has moved to the mode or header line
27203 or marginal area AREA of window W, x-position X and y-position Y.
27204 X is relative to the start of the text display area of W, so the
27205 width of bitmap areas and scroll bars must be subtracted to get a
27206 position relative to the start of the mode line. */
27208 static void
27209 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27210 enum window_part area)
27212 struct window *w = XWINDOW (window);
27213 struct frame *f = XFRAME (w->frame);
27214 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27215 #ifdef HAVE_WINDOW_SYSTEM
27216 Display_Info *dpyinfo;
27217 #endif
27218 Cursor cursor = No_Cursor;
27219 Lisp_Object pointer = Qnil;
27220 int dx, dy, width, height;
27221 ptrdiff_t charpos;
27222 Lisp_Object string, object = Qnil;
27223 Lisp_Object pos IF_LINT (= Qnil), help;
27225 Lisp_Object mouse_face;
27226 int original_x_pixel = x;
27227 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27228 struct glyph_row *row IF_LINT (= 0);
27230 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27232 int x0;
27233 struct glyph *end;
27235 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27236 returns them in row/column units! */
27237 string = mode_line_string (w, area, &x, &y, &charpos,
27238 &object, &dx, &dy, &width, &height);
27240 row = (area == ON_MODE_LINE
27241 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27242 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27244 /* Find the glyph under the mouse pointer. */
27245 if (row->mode_line_p && row->enabled_p)
27247 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27248 end = glyph + row->used[TEXT_AREA];
27250 for (x0 = original_x_pixel;
27251 glyph < end && x0 >= glyph->pixel_width;
27252 ++glyph)
27253 x0 -= glyph->pixel_width;
27255 if (glyph >= end)
27256 glyph = NULL;
27259 else
27261 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27262 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27263 returns them in row/column units! */
27264 string = marginal_area_string (w, area, &x, &y, &charpos,
27265 &object, &dx, &dy, &width, &height);
27268 help = Qnil;
27270 #ifdef HAVE_WINDOW_SYSTEM
27271 if (IMAGEP (object))
27273 Lisp_Object image_map, hotspot;
27274 if ((image_map = Fplist_get (XCDR (object), QCmap),
27275 !NILP (image_map))
27276 && (hotspot = find_hot_spot (image_map, dx, dy),
27277 CONSP (hotspot))
27278 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27280 Lisp_Object plist;
27282 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27283 If so, we could look for mouse-enter, mouse-leave
27284 properties in PLIST (and do something...). */
27285 hotspot = XCDR (hotspot);
27286 if (CONSP (hotspot)
27287 && (plist = XCAR (hotspot), CONSP (plist)))
27289 pointer = Fplist_get (plist, Qpointer);
27290 if (NILP (pointer))
27291 pointer = Qhand;
27292 help = Fplist_get (plist, Qhelp_echo);
27293 if (!NILP (help))
27295 help_echo_string = help;
27296 XSETWINDOW (help_echo_window, w);
27297 help_echo_object = w->contents;
27298 help_echo_pos = charpos;
27302 if (NILP (pointer))
27303 pointer = Fplist_get (XCDR (object), QCpointer);
27305 #endif /* HAVE_WINDOW_SYSTEM */
27307 if (STRINGP (string))
27308 pos = make_number (charpos);
27310 /* Set the help text and mouse pointer. If the mouse is on a part
27311 of the mode line without any text (e.g. past the right edge of
27312 the mode line text), use the default help text and pointer. */
27313 if (STRINGP (string) || area == ON_MODE_LINE)
27315 /* Arrange to display the help by setting the global variables
27316 help_echo_string, help_echo_object, and help_echo_pos. */
27317 if (NILP (help))
27319 if (STRINGP (string))
27320 help = Fget_text_property (pos, Qhelp_echo, string);
27322 if (!NILP (help))
27324 help_echo_string = help;
27325 XSETWINDOW (help_echo_window, w);
27326 help_echo_object = string;
27327 help_echo_pos = charpos;
27329 else if (area == ON_MODE_LINE)
27331 Lisp_Object default_help
27332 = buffer_local_value_1 (Qmode_line_default_help_echo,
27333 w->contents);
27335 if (STRINGP (default_help))
27337 help_echo_string = default_help;
27338 XSETWINDOW (help_echo_window, w);
27339 help_echo_object = Qnil;
27340 help_echo_pos = -1;
27345 #ifdef HAVE_WINDOW_SYSTEM
27346 /* Change the mouse pointer according to what is under it. */
27347 if (FRAME_WINDOW_P (f))
27349 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27350 if (STRINGP (string))
27352 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27354 if (NILP (pointer))
27355 pointer = Fget_text_property (pos, Qpointer, string);
27357 /* Change the mouse pointer according to what is under X/Y. */
27358 if (NILP (pointer)
27359 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27361 Lisp_Object map;
27362 map = Fget_text_property (pos, Qlocal_map, string);
27363 if (!KEYMAPP (map))
27364 map = Fget_text_property (pos, Qkeymap, string);
27365 if (!KEYMAPP (map))
27366 cursor = dpyinfo->vertical_scroll_bar_cursor;
27369 else
27370 /* Default mode-line pointer. */
27371 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27373 #endif
27376 /* Change the mouse face according to what is under X/Y. */
27377 if (STRINGP (string))
27379 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27380 if (!NILP (mouse_face)
27381 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27382 && glyph)
27384 Lisp_Object b, e;
27386 struct glyph * tmp_glyph;
27388 int gpos;
27389 int gseq_length;
27390 int total_pixel_width;
27391 ptrdiff_t begpos, endpos, ignore;
27393 int vpos, hpos;
27395 b = Fprevious_single_property_change (make_number (charpos + 1),
27396 Qmouse_face, string, Qnil);
27397 if (NILP (b))
27398 begpos = 0;
27399 else
27400 begpos = XINT (b);
27402 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27403 if (NILP (e))
27404 endpos = SCHARS (string);
27405 else
27406 endpos = XINT (e);
27408 /* Calculate the glyph position GPOS of GLYPH in the
27409 displayed string, relative to the beginning of the
27410 highlighted part of the string.
27412 Note: GPOS is different from CHARPOS. CHARPOS is the
27413 position of GLYPH in the internal string object. A mode
27414 line string format has structures which are converted to
27415 a flattened string by the Emacs Lisp interpreter. The
27416 internal string is an element of those structures. The
27417 displayed string is the flattened string. */
27418 tmp_glyph = row_start_glyph;
27419 while (tmp_glyph < glyph
27420 && (!(EQ (tmp_glyph->object, glyph->object)
27421 && begpos <= tmp_glyph->charpos
27422 && tmp_glyph->charpos < endpos)))
27423 tmp_glyph++;
27424 gpos = glyph - tmp_glyph;
27426 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27427 the highlighted part of the displayed string to which
27428 GLYPH belongs. Note: GSEQ_LENGTH is different from
27429 SCHARS (STRING), because the latter returns the length of
27430 the internal string. */
27431 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27432 tmp_glyph > glyph
27433 && (!(EQ (tmp_glyph->object, glyph->object)
27434 && begpos <= tmp_glyph->charpos
27435 && tmp_glyph->charpos < endpos));
27436 tmp_glyph--)
27438 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27440 /* Calculate the total pixel width of all the glyphs between
27441 the beginning of the highlighted area and GLYPH. */
27442 total_pixel_width = 0;
27443 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27444 total_pixel_width += tmp_glyph->pixel_width;
27446 /* Pre calculation of re-rendering position. Note: X is in
27447 column units here, after the call to mode_line_string or
27448 marginal_area_string. */
27449 hpos = x - gpos;
27450 vpos = (area == ON_MODE_LINE
27451 ? (w->current_matrix)->nrows - 1
27452 : 0);
27454 /* If GLYPH's position is included in the region that is
27455 already drawn in mouse face, we have nothing to do. */
27456 if ( EQ (window, hlinfo->mouse_face_window)
27457 && (!row->reversed_p
27458 ? (hlinfo->mouse_face_beg_col <= hpos
27459 && hpos < hlinfo->mouse_face_end_col)
27460 /* In R2L rows we swap BEG and END, see below. */
27461 : (hlinfo->mouse_face_end_col <= hpos
27462 && hpos < hlinfo->mouse_face_beg_col))
27463 && hlinfo->mouse_face_beg_row == vpos )
27464 return;
27466 if (clear_mouse_face (hlinfo))
27467 cursor = No_Cursor;
27469 if (!row->reversed_p)
27471 hlinfo->mouse_face_beg_col = hpos;
27472 hlinfo->mouse_face_beg_x = original_x_pixel
27473 - (total_pixel_width + dx);
27474 hlinfo->mouse_face_end_col = hpos + gseq_length;
27475 hlinfo->mouse_face_end_x = 0;
27477 else
27479 /* In R2L rows, show_mouse_face expects BEG and END
27480 coordinates to be swapped. */
27481 hlinfo->mouse_face_end_col = hpos;
27482 hlinfo->mouse_face_end_x = original_x_pixel
27483 - (total_pixel_width + dx);
27484 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27485 hlinfo->mouse_face_beg_x = 0;
27488 hlinfo->mouse_face_beg_row = vpos;
27489 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27490 hlinfo->mouse_face_beg_y = 0;
27491 hlinfo->mouse_face_end_y = 0;
27492 hlinfo->mouse_face_past_end = 0;
27493 hlinfo->mouse_face_window = window;
27495 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27496 charpos,
27497 0, 0, 0,
27498 &ignore,
27499 glyph->face_id,
27501 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27503 if (NILP (pointer))
27504 pointer = Qhand;
27506 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27507 clear_mouse_face (hlinfo);
27509 #ifdef HAVE_WINDOW_SYSTEM
27510 if (FRAME_WINDOW_P (f))
27511 define_frame_cursor1 (f, cursor, pointer);
27512 #endif
27516 /* EXPORT:
27517 Take proper action when the mouse has moved to position X, Y on
27518 frame F as regards highlighting characters that have mouse-face
27519 properties. Also de-highlighting chars where the mouse was before.
27520 X and Y can be negative or out of range. */
27522 void
27523 note_mouse_highlight (struct frame *f, int x, int y)
27525 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27526 enum window_part part = ON_NOTHING;
27527 Lisp_Object window;
27528 struct window *w;
27529 Cursor cursor = No_Cursor;
27530 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27531 struct buffer *b;
27533 /* When a menu is active, don't highlight because this looks odd. */
27534 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27535 if (popup_activated ())
27536 return;
27537 #endif
27539 if (NILP (Vmouse_highlight)
27540 || !f->glyphs_initialized_p
27541 || f->pointer_invisible)
27542 return;
27544 hlinfo->mouse_face_mouse_x = x;
27545 hlinfo->mouse_face_mouse_y = y;
27546 hlinfo->mouse_face_mouse_frame = f;
27548 if (hlinfo->mouse_face_defer)
27549 return;
27551 /* Which window is that in? */
27552 window = window_from_coordinates (f, x, y, &part, 1);
27554 /* If displaying active text in another window, clear that. */
27555 if (! EQ (window, hlinfo->mouse_face_window)
27556 /* Also clear if we move out of text area in same window. */
27557 || (!NILP (hlinfo->mouse_face_window)
27558 && !NILP (window)
27559 && part != ON_TEXT
27560 && part != ON_MODE_LINE
27561 && part != ON_HEADER_LINE))
27562 clear_mouse_face (hlinfo);
27564 /* Not on a window -> return. */
27565 if (!WINDOWP (window))
27566 return;
27568 /* Reset help_echo_string. It will get recomputed below. */
27569 help_echo_string = Qnil;
27571 /* Convert to window-relative pixel coordinates. */
27572 w = XWINDOW (window);
27573 frame_to_window_pixel_xy (w, &x, &y);
27575 #ifdef HAVE_WINDOW_SYSTEM
27576 /* Handle tool-bar window differently since it doesn't display a
27577 buffer. */
27578 if (EQ (window, f->tool_bar_window))
27580 note_tool_bar_highlight (f, x, y);
27581 return;
27583 #endif
27585 /* Mouse is on the mode, header line or margin? */
27586 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27587 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27589 note_mode_line_or_margin_highlight (window, x, y, part);
27590 return;
27593 #ifdef HAVE_WINDOW_SYSTEM
27594 if (part == ON_VERTICAL_BORDER)
27596 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27597 help_echo_string = build_string ("drag-mouse-1: resize");
27599 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27600 || part == ON_SCROLL_BAR)
27601 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27602 else
27603 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27604 #endif
27606 /* Are we in a window whose display is up to date?
27607 And verify the buffer's text has not changed. */
27608 b = XBUFFER (w->contents);
27609 if (part == ON_TEXT
27610 && w->window_end_valid
27611 && w->last_modified == BUF_MODIFF (b)
27612 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27614 int hpos, vpos, dx, dy, area = LAST_AREA;
27615 ptrdiff_t pos;
27616 struct glyph *glyph;
27617 Lisp_Object object;
27618 Lisp_Object mouse_face = Qnil, position;
27619 Lisp_Object *overlay_vec = NULL;
27620 ptrdiff_t i, noverlays;
27621 struct buffer *obuf;
27622 ptrdiff_t obegv, ozv;
27623 int same_region;
27625 /* Find the glyph under X/Y. */
27626 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27628 #ifdef HAVE_WINDOW_SYSTEM
27629 /* Look for :pointer property on image. */
27630 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27632 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27633 if (img != NULL && IMAGEP (img->spec))
27635 Lisp_Object image_map, hotspot;
27636 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27637 !NILP (image_map))
27638 && (hotspot = find_hot_spot (image_map,
27639 glyph->slice.img.x + dx,
27640 glyph->slice.img.y + dy),
27641 CONSP (hotspot))
27642 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27644 Lisp_Object plist;
27646 /* Could check XCAR (hotspot) to see if we enter/leave
27647 this hot-spot.
27648 If so, we could look for mouse-enter, mouse-leave
27649 properties in PLIST (and do something...). */
27650 hotspot = XCDR (hotspot);
27651 if (CONSP (hotspot)
27652 && (plist = XCAR (hotspot), CONSP (plist)))
27654 pointer = Fplist_get (plist, Qpointer);
27655 if (NILP (pointer))
27656 pointer = Qhand;
27657 help_echo_string = Fplist_get (plist, Qhelp_echo);
27658 if (!NILP (help_echo_string))
27660 help_echo_window = window;
27661 help_echo_object = glyph->object;
27662 help_echo_pos = glyph->charpos;
27666 if (NILP (pointer))
27667 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27670 #endif /* HAVE_WINDOW_SYSTEM */
27672 /* Clear mouse face if X/Y not over text. */
27673 if (glyph == NULL
27674 || area != TEXT_AREA
27675 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
27676 /* Glyph's OBJECT is an integer for glyphs inserted by the
27677 display engine for its internal purposes, like truncation
27678 and continuation glyphs and blanks beyond the end of
27679 line's text on text terminals. If we are over such a
27680 glyph, we are not over any text. */
27681 || INTEGERP (glyph->object)
27682 /* R2L rows have a stretch glyph at their front, which
27683 stands for no text, whereas L2R rows have no glyphs at
27684 all beyond the end of text. Treat such stretch glyphs
27685 like we do with NULL glyphs in L2R rows. */
27686 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27687 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
27688 && glyph->type == STRETCH_GLYPH
27689 && glyph->avoid_cursor_p))
27691 if (clear_mouse_face (hlinfo))
27692 cursor = No_Cursor;
27693 #ifdef HAVE_WINDOW_SYSTEM
27694 if (FRAME_WINDOW_P (f) && NILP (pointer))
27696 if (area != TEXT_AREA)
27697 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27698 else
27699 pointer = Vvoid_text_area_pointer;
27701 #endif
27702 goto set_cursor;
27705 pos = glyph->charpos;
27706 object = glyph->object;
27707 if (!STRINGP (object) && !BUFFERP (object))
27708 goto set_cursor;
27710 /* If we get an out-of-range value, return now; avoid an error. */
27711 if (BUFFERP (object) && pos > BUF_Z (b))
27712 goto set_cursor;
27714 /* Make the window's buffer temporarily current for
27715 overlays_at and compute_char_face. */
27716 obuf = current_buffer;
27717 current_buffer = b;
27718 obegv = BEGV;
27719 ozv = ZV;
27720 BEGV = BEG;
27721 ZV = Z;
27723 /* Is this char mouse-active or does it have help-echo? */
27724 position = make_number (pos);
27726 if (BUFFERP (object))
27728 /* Put all the overlays we want in a vector in overlay_vec. */
27729 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27730 /* Sort overlays into increasing priority order. */
27731 noverlays = sort_overlays (overlay_vec, noverlays, w);
27733 else
27734 noverlays = 0;
27736 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27738 if (same_region)
27739 cursor = No_Cursor;
27741 /* Check mouse-face highlighting. */
27742 if (! same_region
27743 /* If there exists an overlay with mouse-face overlapping
27744 the one we are currently highlighting, we have to
27745 check if we enter the overlapping overlay, and then
27746 highlight only that. */
27747 || (OVERLAYP (hlinfo->mouse_face_overlay)
27748 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27750 /* Find the highest priority overlay with a mouse-face. */
27751 Lisp_Object overlay = Qnil;
27752 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27754 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27755 if (!NILP (mouse_face))
27756 overlay = overlay_vec[i];
27759 /* If we're highlighting the same overlay as before, there's
27760 no need to do that again. */
27761 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27762 goto check_help_echo;
27763 hlinfo->mouse_face_overlay = overlay;
27765 /* Clear the display of the old active region, if any. */
27766 if (clear_mouse_face (hlinfo))
27767 cursor = No_Cursor;
27769 /* If no overlay applies, get a text property. */
27770 if (NILP (overlay))
27771 mouse_face = Fget_text_property (position, Qmouse_face, object);
27773 /* Next, compute the bounds of the mouse highlighting and
27774 display it. */
27775 if (!NILP (mouse_face) && STRINGP (object))
27777 /* The mouse-highlighting comes from a display string
27778 with a mouse-face. */
27779 Lisp_Object s, e;
27780 ptrdiff_t ignore;
27782 s = Fprevious_single_property_change
27783 (make_number (pos + 1), Qmouse_face, object, Qnil);
27784 e = Fnext_single_property_change
27785 (position, Qmouse_face, object, Qnil);
27786 if (NILP (s))
27787 s = make_number (0);
27788 if (NILP (e))
27789 e = make_number (SCHARS (object) - 1);
27790 mouse_face_from_string_pos (w, hlinfo, object,
27791 XINT (s), XINT (e));
27792 hlinfo->mouse_face_past_end = 0;
27793 hlinfo->mouse_face_window = window;
27794 hlinfo->mouse_face_face_id
27795 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27796 glyph->face_id, 1);
27797 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27798 cursor = No_Cursor;
27800 else
27802 /* The mouse-highlighting, if any, comes from an overlay
27803 or text property in the buffer. */
27804 Lisp_Object buffer IF_LINT (= Qnil);
27805 Lisp_Object disp_string IF_LINT (= Qnil);
27807 if (STRINGP (object))
27809 /* If we are on a display string with no mouse-face,
27810 check if the text under it has one. */
27811 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27812 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27813 pos = string_buffer_position (object, start);
27814 if (pos > 0)
27816 mouse_face = get_char_property_and_overlay
27817 (make_number (pos), Qmouse_face, w->contents, &overlay);
27818 buffer = w->contents;
27819 disp_string = object;
27822 else
27824 buffer = object;
27825 disp_string = Qnil;
27828 if (!NILP (mouse_face))
27830 Lisp_Object before, after;
27831 Lisp_Object before_string, after_string;
27832 /* To correctly find the limits of mouse highlight
27833 in a bidi-reordered buffer, we must not use the
27834 optimization of limiting the search in
27835 previous-single-property-change and
27836 next-single-property-change, because
27837 rows_from_pos_range needs the real start and end
27838 positions to DTRT in this case. That's because
27839 the first row visible in a window does not
27840 necessarily display the character whose position
27841 is the smallest. */
27842 Lisp_Object lim1 =
27843 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27844 ? Fmarker_position (w->start)
27845 : Qnil;
27846 Lisp_Object lim2 =
27847 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27848 ? make_number (BUF_Z (XBUFFER (buffer))
27849 - XFASTINT (w->window_end_pos))
27850 : Qnil;
27852 if (NILP (overlay))
27854 /* Handle the text property case. */
27855 before = Fprevious_single_property_change
27856 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27857 after = Fnext_single_property_change
27858 (make_number (pos), Qmouse_face, buffer, lim2);
27859 before_string = after_string = Qnil;
27861 else
27863 /* Handle the overlay case. */
27864 before = Foverlay_start (overlay);
27865 after = Foverlay_end (overlay);
27866 before_string = Foverlay_get (overlay, Qbefore_string);
27867 after_string = Foverlay_get (overlay, Qafter_string);
27869 if (!STRINGP (before_string)) before_string = Qnil;
27870 if (!STRINGP (after_string)) after_string = Qnil;
27873 mouse_face_from_buffer_pos (window, hlinfo, pos,
27874 NILP (before)
27876 : XFASTINT (before),
27877 NILP (after)
27878 ? BUF_Z (XBUFFER (buffer))
27879 : XFASTINT (after),
27880 before_string, after_string,
27881 disp_string);
27882 cursor = No_Cursor;
27887 check_help_echo:
27889 /* Look for a `help-echo' property. */
27890 if (NILP (help_echo_string)) {
27891 Lisp_Object help, overlay;
27893 /* Check overlays first. */
27894 help = overlay = Qnil;
27895 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27897 overlay = overlay_vec[i];
27898 help = Foverlay_get (overlay, Qhelp_echo);
27901 if (!NILP (help))
27903 help_echo_string = help;
27904 help_echo_window = window;
27905 help_echo_object = overlay;
27906 help_echo_pos = pos;
27908 else
27910 Lisp_Object obj = glyph->object;
27911 ptrdiff_t charpos = glyph->charpos;
27913 /* Try text properties. */
27914 if (STRINGP (obj)
27915 && charpos >= 0
27916 && charpos < SCHARS (obj))
27918 help = Fget_text_property (make_number (charpos),
27919 Qhelp_echo, obj);
27920 if (NILP (help))
27922 /* If the string itself doesn't specify a help-echo,
27923 see if the buffer text ``under'' it does. */
27924 struct glyph_row *r
27925 = MATRIX_ROW (w->current_matrix, vpos);
27926 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27927 ptrdiff_t p = string_buffer_position (obj, start);
27928 if (p > 0)
27930 help = Fget_char_property (make_number (p),
27931 Qhelp_echo, w->contents);
27932 if (!NILP (help))
27934 charpos = p;
27935 obj = w->contents;
27940 else if (BUFFERP (obj)
27941 && charpos >= BEGV
27942 && charpos < ZV)
27943 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27944 obj);
27946 if (!NILP (help))
27948 help_echo_string = help;
27949 help_echo_window = window;
27950 help_echo_object = obj;
27951 help_echo_pos = charpos;
27956 #ifdef HAVE_WINDOW_SYSTEM
27957 /* Look for a `pointer' property. */
27958 if (FRAME_WINDOW_P (f) && NILP (pointer))
27960 /* Check overlays first. */
27961 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27962 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27964 if (NILP (pointer))
27966 Lisp_Object obj = glyph->object;
27967 ptrdiff_t charpos = glyph->charpos;
27969 /* Try text properties. */
27970 if (STRINGP (obj)
27971 && charpos >= 0
27972 && charpos < SCHARS (obj))
27974 pointer = Fget_text_property (make_number (charpos),
27975 Qpointer, obj);
27976 if (NILP (pointer))
27978 /* If the string itself doesn't specify a pointer,
27979 see if the buffer text ``under'' it does. */
27980 struct glyph_row *r
27981 = MATRIX_ROW (w->current_matrix, vpos);
27982 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27983 ptrdiff_t p = string_buffer_position (obj, start);
27984 if (p > 0)
27985 pointer = Fget_char_property (make_number (p),
27986 Qpointer, w->contents);
27989 else if (BUFFERP (obj)
27990 && charpos >= BEGV
27991 && charpos < ZV)
27992 pointer = Fget_text_property (make_number (charpos),
27993 Qpointer, obj);
27996 #endif /* HAVE_WINDOW_SYSTEM */
27998 BEGV = obegv;
27999 ZV = ozv;
28000 current_buffer = obuf;
28003 set_cursor:
28005 #ifdef HAVE_WINDOW_SYSTEM
28006 if (FRAME_WINDOW_P (f))
28007 define_frame_cursor1 (f, cursor, pointer);
28008 #else
28009 /* This is here to prevent a compiler error, about "label at end of
28010 compound statement". */
28011 return;
28012 #endif
28016 /* EXPORT for RIF:
28017 Clear any mouse-face on window W. This function is part of the
28018 redisplay interface, and is called from try_window_id and similar
28019 functions to ensure the mouse-highlight is off. */
28021 void
28022 x_clear_window_mouse_face (struct window *w)
28024 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28025 Lisp_Object window;
28027 block_input ();
28028 XSETWINDOW (window, w);
28029 if (EQ (window, hlinfo->mouse_face_window))
28030 clear_mouse_face (hlinfo);
28031 unblock_input ();
28035 /* EXPORT:
28036 Just discard the mouse face information for frame F, if any.
28037 This is used when the size of F is changed. */
28039 void
28040 cancel_mouse_face (struct frame *f)
28042 Lisp_Object window;
28043 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28045 window = hlinfo->mouse_face_window;
28046 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28048 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28049 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28050 hlinfo->mouse_face_window = Qnil;
28056 /***********************************************************************
28057 Exposure Events
28058 ***********************************************************************/
28060 #ifdef HAVE_WINDOW_SYSTEM
28062 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28063 which intersects rectangle R. R is in window-relative coordinates. */
28065 static void
28066 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28067 enum glyph_row_area area)
28069 struct glyph *first = row->glyphs[area];
28070 struct glyph *end = row->glyphs[area] + row->used[area];
28071 struct glyph *last;
28072 int first_x, start_x, x;
28074 if (area == TEXT_AREA && row->fill_line_p)
28075 /* If row extends face to end of line write the whole line. */
28076 draw_glyphs (w, 0, row, area,
28077 0, row->used[area],
28078 DRAW_NORMAL_TEXT, 0);
28079 else
28081 /* Set START_X to the window-relative start position for drawing glyphs of
28082 AREA. The first glyph of the text area can be partially visible.
28083 The first glyphs of other areas cannot. */
28084 start_x = window_box_left_offset (w, area);
28085 x = start_x;
28086 if (area == TEXT_AREA)
28087 x += row->x;
28089 /* Find the first glyph that must be redrawn. */
28090 while (first < end
28091 && x + first->pixel_width < r->x)
28093 x += first->pixel_width;
28094 ++first;
28097 /* Find the last one. */
28098 last = first;
28099 first_x = x;
28100 while (last < end
28101 && x < r->x + r->width)
28103 x += last->pixel_width;
28104 ++last;
28107 /* Repaint. */
28108 if (last > first)
28109 draw_glyphs (w, first_x - start_x, row, area,
28110 first - row->glyphs[area], last - row->glyphs[area],
28111 DRAW_NORMAL_TEXT, 0);
28116 /* Redraw the parts of the glyph row ROW on window W intersecting
28117 rectangle R. R is in window-relative coordinates. Value is
28118 non-zero if mouse-face was overwritten. */
28120 static int
28121 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28123 eassert (row->enabled_p);
28125 if (row->mode_line_p || w->pseudo_window_p)
28126 draw_glyphs (w, 0, row, TEXT_AREA,
28127 0, row->used[TEXT_AREA],
28128 DRAW_NORMAL_TEXT, 0);
28129 else
28131 if (row->used[LEFT_MARGIN_AREA])
28132 expose_area (w, row, r, LEFT_MARGIN_AREA);
28133 if (row->used[TEXT_AREA])
28134 expose_area (w, row, r, TEXT_AREA);
28135 if (row->used[RIGHT_MARGIN_AREA])
28136 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28137 draw_row_fringe_bitmaps (w, row);
28140 return row->mouse_face_p;
28144 /* Redraw those parts of glyphs rows during expose event handling that
28145 overlap other rows. Redrawing of an exposed line writes over parts
28146 of lines overlapping that exposed line; this function fixes that.
28148 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28149 row in W's current matrix that is exposed and overlaps other rows.
28150 LAST_OVERLAPPING_ROW is the last such row. */
28152 static void
28153 expose_overlaps (struct window *w,
28154 struct glyph_row *first_overlapping_row,
28155 struct glyph_row *last_overlapping_row,
28156 XRectangle *r)
28158 struct glyph_row *row;
28160 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28161 if (row->overlapping_p)
28163 eassert (row->enabled_p && !row->mode_line_p);
28165 row->clip = r;
28166 if (row->used[LEFT_MARGIN_AREA])
28167 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28169 if (row->used[TEXT_AREA])
28170 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28172 if (row->used[RIGHT_MARGIN_AREA])
28173 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28174 row->clip = NULL;
28179 /* Return non-zero if W's cursor intersects rectangle R. */
28181 static int
28182 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28184 XRectangle cr, result;
28185 struct glyph *cursor_glyph;
28186 struct glyph_row *row;
28188 if (w->phys_cursor.vpos >= 0
28189 && w->phys_cursor.vpos < w->current_matrix->nrows
28190 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28191 row->enabled_p)
28192 && row->cursor_in_fringe_p)
28194 /* Cursor is in the fringe. */
28195 cr.x = window_box_right_offset (w,
28196 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28197 ? RIGHT_MARGIN_AREA
28198 : TEXT_AREA));
28199 cr.y = row->y;
28200 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28201 cr.height = row->height;
28202 return x_intersect_rectangles (&cr, r, &result);
28205 cursor_glyph = get_phys_cursor_glyph (w);
28206 if (cursor_glyph)
28208 /* r is relative to W's box, but w->phys_cursor.x is relative
28209 to left edge of W's TEXT area. Adjust it. */
28210 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28211 cr.y = w->phys_cursor.y;
28212 cr.width = cursor_glyph->pixel_width;
28213 cr.height = w->phys_cursor_height;
28214 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28215 I assume the effect is the same -- and this is portable. */
28216 return x_intersect_rectangles (&cr, r, &result);
28218 /* If we don't understand the format, pretend we're not in the hot-spot. */
28219 return 0;
28223 /* EXPORT:
28224 Draw a vertical window border to the right of window W if W doesn't
28225 have vertical scroll bars. */
28227 void
28228 x_draw_vertical_border (struct window *w)
28230 struct frame *f = XFRAME (WINDOW_FRAME (w));
28232 /* We could do better, if we knew what type of scroll-bar the adjacent
28233 windows (on either side) have... But we don't :-(
28234 However, I think this works ok. ++KFS 2003-04-25 */
28236 /* Redraw borders between horizontally adjacent windows. Don't
28237 do it for frames with vertical scroll bars because either the
28238 right scroll bar of a window, or the left scroll bar of its
28239 neighbor will suffice as a border. */
28240 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28241 return;
28243 /* Note: It is necessary to redraw both the left and the right
28244 borders, for when only this single window W is being
28245 redisplayed. */
28246 if (!WINDOW_RIGHTMOST_P (w)
28247 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28249 int x0, x1, y0, y1;
28251 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28252 y1 -= 1;
28254 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28255 x1 -= 1;
28257 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28259 if (!WINDOW_LEFTMOST_P (w)
28260 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28262 int x0, x1, y0, y1;
28264 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28265 y1 -= 1;
28267 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28268 x0 -= 1;
28270 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28275 /* Redraw the part of window W intersection rectangle FR. Pixel
28276 coordinates in FR are frame-relative. Call this function with
28277 input blocked. Value is non-zero if the exposure overwrites
28278 mouse-face. */
28280 static int
28281 expose_window (struct window *w, XRectangle *fr)
28283 struct frame *f = XFRAME (w->frame);
28284 XRectangle wr, r;
28285 int mouse_face_overwritten_p = 0;
28287 /* If window is not yet fully initialized, do nothing. This can
28288 happen when toolkit scroll bars are used and a window is split.
28289 Reconfiguring the scroll bar will generate an expose for a newly
28290 created window. */
28291 if (w->current_matrix == NULL)
28292 return 0;
28294 /* When we're currently updating the window, display and current
28295 matrix usually don't agree. Arrange for a thorough display
28296 later. */
28297 if (w == updated_window)
28299 SET_FRAME_GARBAGED (f);
28300 return 0;
28303 /* Frame-relative pixel rectangle of W. */
28304 wr.x = WINDOW_LEFT_EDGE_X (w);
28305 wr.y = WINDOW_TOP_EDGE_Y (w);
28306 wr.width = WINDOW_TOTAL_WIDTH (w);
28307 wr.height = WINDOW_TOTAL_HEIGHT (w);
28309 if (x_intersect_rectangles (fr, &wr, &r))
28311 int yb = window_text_bottom_y (w);
28312 struct glyph_row *row;
28313 int cursor_cleared_p, phys_cursor_on_p;
28314 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28316 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28317 r.x, r.y, r.width, r.height));
28319 /* Convert to window coordinates. */
28320 r.x -= WINDOW_LEFT_EDGE_X (w);
28321 r.y -= WINDOW_TOP_EDGE_Y (w);
28323 /* Turn off the cursor. */
28324 if (!w->pseudo_window_p
28325 && phys_cursor_in_rect_p (w, &r))
28327 x_clear_cursor (w);
28328 cursor_cleared_p = 1;
28330 else
28331 cursor_cleared_p = 0;
28333 /* If the row containing the cursor extends face to end of line,
28334 then expose_area might overwrite the cursor outside the
28335 rectangle and thus notice_overwritten_cursor might clear
28336 w->phys_cursor_on_p. We remember the original value and
28337 check later if it is changed. */
28338 phys_cursor_on_p = w->phys_cursor_on_p;
28340 /* Update lines intersecting rectangle R. */
28341 first_overlapping_row = last_overlapping_row = NULL;
28342 for (row = w->current_matrix->rows;
28343 row->enabled_p;
28344 ++row)
28346 int y0 = row->y;
28347 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28349 if ((y0 >= r.y && y0 < r.y + r.height)
28350 || (y1 > r.y && y1 < r.y + r.height)
28351 || (r.y >= y0 && r.y < y1)
28352 || (r.y + r.height > y0 && r.y + r.height < y1))
28354 /* A header line may be overlapping, but there is no need
28355 to fix overlapping areas for them. KFS 2005-02-12 */
28356 if (row->overlapping_p && !row->mode_line_p)
28358 if (first_overlapping_row == NULL)
28359 first_overlapping_row = row;
28360 last_overlapping_row = row;
28363 row->clip = fr;
28364 if (expose_line (w, row, &r))
28365 mouse_face_overwritten_p = 1;
28366 row->clip = NULL;
28368 else if (row->overlapping_p)
28370 /* We must redraw a row overlapping the exposed area. */
28371 if (y0 < r.y
28372 ? y0 + row->phys_height > r.y
28373 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28375 if (first_overlapping_row == NULL)
28376 first_overlapping_row = row;
28377 last_overlapping_row = row;
28381 if (y1 >= yb)
28382 break;
28385 /* Display the mode line if there is one. */
28386 if (WINDOW_WANTS_MODELINE_P (w)
28387 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28388 row->enabled_p)
28389 && row->y < r.y + r.height)
28391 if (expose_line (w, row, &r))
28392 mouse_face_overwritten_p = 1;
28395 if (!w->pseudo_window_p)
28397 /* Fix the display of overlapping rows. */
28398 if (first_overlapping_row)
28399 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28400 fr);
28402 /* Draw border between windows. */
28403 x_draw_vertical_border (w);
28405 /* Turn the cursor on again. */
28406 if (cursor_cleared_p
28407 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28408 update_window_cursor (w, 1);
28412 return mouse_face_overwritten_p;
28417 /* Redraw (parts) of all windows in the window tree rooted at W that
28418 intersect R. R contains frame pixel coordinates. Value is
28419 non-zero if the exposure overwrites mouse-face. */
28421 static int
28422 expose_window_tree (struct window *w, XRectangle *r)
28424 struct frame *f = XFRAME (w->frame);
28425 int mouse_face_overwritten_p = 0;
28427 while (w && !FRAME_GARBAGED_P (f))
28429 if (WINDOWP (w->contents))
28430 mouse_face_overwritten_p
28431 |= expose_window_tree (XWINDOW (w->contents), r);
28432 else
28433 mouse_face_overwritten_p |= expose_window (w, r);
28435 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28438 return mouse_face_overwritten_p;
28442 /* EXPORT:
28443 Redisplay an exposed area of frame F. X and Y are the upper-left
28444 corner of the exposed rectangle. W and H are width and height of
28445 the exposed area. All are pixel values. W or H zero means redraw
28446 the entire frame. */
28448 void
28449 expose_frame (struct frame *f, int x, int y, int w, int h)
28451 XRectangle r;
28452 int mouse_face_overwritten_p = 0;
28454 TRACE ((stderr, "expose_frame "));
28456 /* No need to redraw if frame will be redrawn soon. */
28457 if (FRAME_GARBAGED_P (f))
28459 TRACE ((stderr, " garbaged\n"));
28460 return;
28463 /* If basic faces haven't been realized yet, there is no point in
28464 trying to redraw anything. This can happen when we get an expose
28465 event while Emacs is starting, e.g. by moving another window. */
28466 if (FRAME_FACE_CACHE (f) == NULL
28467 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28469 TRACE ((stderr, " no faces\n"));
28470 return;
28473 if (w == 0 || h == 0)
28475 r.x = r.y = 0;
28476 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28477 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28479 else
28481 r.x = x;
28482 r.y = y;
28483 r.width = w;
28484 r.height = h;
28487 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28488 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28490 if (WINDOWP (f->tool_bar_window))
28491 mouse_face_overwritten_p
28492 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28494 #ifdef HAVE_X_WINDOWS
28495 #ifndef MSDOS
28496 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28497 if (WINDOWP (f->menu_bar_window))
28498 mouse_face_overwritten_p
28499 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28500 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28501 #endif
28502 #endif
28504 /* Some window managers support a focus-follows-mouse style with
28505 delayed raising of frames. Imagine a partially obscured frame,
28506 and moving the mouse into partially obscured mouse-face on that
28507 frame. The visible part of the mouse-face will be highlighted,
28508 then the WM raises the obscured frame. With at least one WM, KDE
28509 2.1, Emacs is not getting any event for the raising of the frame
28510 (even tried with SubstructureRedirectMask), only Expose events.
28511 These expose events will draw text normally, i.e. not
28512 highlighted. Which means we must redo the highlight here.
28513 Subsume it under ``we love X''. --gerd 2001-08-15 */
28514 /* Included in Windows version because Windows most likely does not
28515 do the right thing if any third party tool offers
28516 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28517 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28519 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28520 if (f == hlinfo->mouse_face_mouse_frame)
28522 int mouse_x = hlinfo->mouse_face_mouse_x;
28523 int mouse_y = hlinfo->mouse_face_mouse_y;
28524 clear_mouse_face (hlinfo);
28525 note_mouse_highlight (f, mouse_x, mouse_y);
28531 /* EXPORT:
28532 Determine the intersection of two rectangles R1 and R2. Return
28533 the intersection in *RESULT. Value is non-zero if RESULT is not
28534 empty. */
28537 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28539 XRectangle *left, *right;
28540 XRectangle *upper, *lower;
28541 int intersection_p = 0;
28543 /* Rearrange so that R1 is the left-most rectangle. */
28544 if (r1->x < r2->x)
28545 left = r1, right = r2;
28546 else
28547 left = r2, right = r1;
28549 /* X0 of the intersection is right.x0, if this is inside R1,
28550 otherwise there is no intersection. */
28551 if (right->x <= left->x + left->width)
28553 result->x = right->x;
28555 /* The right end of the intersection is the minimum of
28556 the right ends of left and right. */
28557 result->width = (min (left->x + left->width, right->x + right->width)
28558 - result->x);
28560 /* Same game for Y. */
28561 if (r1->y < r2->y)
28562 upper = r1, lower = r2;
28563 else
28564 upper = r2, lower = r1;
28566 /* The upper end of the intersection is lower.y0, if this is inside
28567 of upper. Otherwise, there is no intersection. */
28568 if (lower->y <= upper->y + upper->height)
28570 result->y = lower->y;
28572 /* The lower end of the intersection is the minimum of the lower
28573 ends of upper and lower. */
28574 result->height = (min (lower->y + lower->height,
28575 upper->y + upper->height)
28576 - result->y);
28577 intersection_p = 1;
28581 return intersection_p;
28584 #endif /* HAVE_WINDOW_SYSTEM */
28587 /***********************************************************************
28588 Initialization
28589 ***********************************************************************/
28591 void
28592 syms_of_xdisp (void)
28594 Vwith_echo_area_save_vector = Qnil;
28595 staticpro (&Vwith_echo_area_save_vector);
28597 Vmessage_stack = Qnil;
28598 staticpro (&Vmessage_stack);
28600 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28601 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28603 message_dolog_marker1 = Fmake_marker ();
28604 staticpro (&message_dolog_marker1);
28605 message_dolog_marker2 = Fmake_marker ();
28606 staticpro (&message_dolog_marker2);
28607 message_dolog_marker3 = Fmake_marker ();
28608 staticpro (&message_dolog_marker3);
28610 #ifdef GLYPH_DEBUG
28611 defsubr (&Sdump_frame_glyph_matrix);
28612 defsubr (&Sdump_glyph_matrix);
28613 defsubr (&Sdump_glyph_row);
28614 defsubr (&Sdump_tool_bar_row);
28615 defsubr (&Strace_redisplay);
28616 defsubr (&Strace_to_stderr);
28617 #endif
28618 #ifdef HAVE_WINDOW_SYSTEM
28619 defsubr (&Stool_bar_lines_needed);
28620 defsubr (&Slookup_image_map);
28621 #endif
28622 defsubr (&Sformat_mode_line);
28623 defsubr (&Sinvisible_p);
28624 defsubr (&Scurrent_bidi_paragraph_direction);
28626 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28627 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28628 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28629 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28630 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28631 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28632 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28633 DEFSYM (Qeval, "eval");
28634 DEFSYM (QCdata, ":data");
28635 DEFSYM (Qdisplay, "display");
28636 DEFSYM (Qspace_width, "space-width");
28637 DEFSYM (Qraise, "raise");
28638 DEFSYM (Qslice, "slice");
28639 DEFSYM (Qspace, "space");
28640 DEFSYM (Qmargin, "margin");
28641 DEFSYM (Qpointer, "pointer");
28642 DEFSYM (Qleft_margin, "left-margin");
28643 DEFSYM (Qright_margin, "right-margin");
28644 DEFSYM (Qcenter, "center");
28645 DEFSYM (Qline_height, "line-height");
28646 DEFSYM (QCalign_to, ":align-to");
28647 DEFSYM (QCrelative_width, ":relative-width");
28648 DEFSYM (QCrelative_height, ":relative-height");
28649 DEFSYM (QCeval, ":eval");
28650 DEFSYM (QCpropertize, ":propertize");
28651 DEFSYM (QCfile, ":file");
28652 DEFSYM (Qfontified, "fontified");
28653 DEFSYM (Qfontification_functions, "fontification-functions");
28654 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28655 DEFSYM (Qescape_glyph, "escape-glyph");
28656 DEFSYM (Qnobreak_space, "nobreak-space");
28657 DEFSYM (Qimage, "image");
28658 DEFSYM (Qtext, "text");
28659 DEFSYM (Qboth, "both");
28660 DEFSYM (Qboth_horiz, "both-horiz");
28661 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28662 DEFSYM (QCmap, ":map");
28663 DEFSYM (QCpointer, ":pointer");
28664 DEFSYM (Qrect, "rect");
28665 DEFSYM (Qcircle, "circle");
28666 DEFSYM (Qpoly, "poly");
28667 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28668 DEFSYM (Qgrow_only, "grow-only");
28669 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28670 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28671 DEFSYM (Qposition, "position");
28672 DEFSYM (Qbuffer_position, "buffer-position");
28673 DEFSYM (Qobject, "object");
28674 DEFSYM (Qbar, "bar");
28675 DEFSYM (Qhbar, "hbar");
28676 DEFSYM (Qbox, "box");
28677 DEFSYM (Qhollow, "hollow");
28678 DEFSYM (Qhand, "hand");
28679 DEFSYM (Qarrow, "arrow");
28680 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28682 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28683 Fcons (intern_c_string ("void-variable"), Qnil)),
28684 Qnil);
28685 staticpro (&list_of_error);
28687 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28688 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28689 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28690 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28692 echo_buffer[0] = echo_buffer[1] = Qnil;
28693 staticpro (&echo_buffer[0]);
28694 staticpro (&echo_buffer[1]);
28696 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28697 staticpro (&echo_area_buffer[0]);
28698 staticpro (&echo_area_buffer[1]);
28700 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28701 staticpro (&Vmessages_buffer_name);
28703 mode_line_proptrans_alist = Qnil;
28704 staticpro (&mode_line_proptrans_alist);
28705 mode_line_string_list = Qnil;
28706 staticpro (&mode_line_string_list);
28707 mode_line_string_face = Qnil;
28708 staticpro (&mode_line_string_face);
28709 mode_line_string_face_prop = Qnil;
28710 staticpro (&mode_line_string_face_prop);
28711 Vmode_line_unwind_vector = Qnil;
28712 staticpro (&Vmode_line_unwind_vector);
28714 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28716 help_echo_string = Qnil;
28717 staticpro (&help_echo_string);
28718 help_echo_object = Qnil;
28719 staticpro (&help_echo_object);
28720 help_echo_window = Qnil;
28721 staticpro (&help_echo_window);
28722 previous_help_echo_string = Qnil;
28723 staticpro (&previous_help_echo_string);
28724 help_echo_pos = -1;
28726 DEFSYM (Qright_to_left, "right-to-left");
28727 DEFSYM (Qleft_to_right, "left-to-right");
28729 #ifdef HAVE_WINDOW_SYSTEM
28730 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28731 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28732 For example, if a block cursor is over a tab, it will be drawn as
28733 wide as that tab on the display. */);
28734 x_stretch_cursor_p = 0;
28735 #endif
28737 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28738 doc: /* Non-nil means highlight trailing whitespace.
28739 The face used for trailing whitespace is `trailing-whitespace'. */);
28740 Vshow_trailing_whitespace = Qnil;
28742 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28743 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28744 If the value is t, Emacs highlights non-ASCII chars which have the
28745 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28746 or `escape-glyph' face respectively.
28748 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28749 U+2011 (non-breaking hyphen) are affected.
28751 Any other non-nil value means to display these characters as a escape
28752 glyph followed by an ordinary space or hyphen.
28754 A value of nil means no special handling of these characters. */);
28755 Vnobreak_char_display = Qt;
28757 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28758 doc: /* The pointer shape to show in void text areas.
28759 A value of nil means to show the text pointer. Other options are `arrow',
28760 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28761 Vvoid_text_area_pointer = Qarrow;
28763 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28764 doc: /* Non-nil means don't actually do any redisplay.
28765 This is used for internal purposes. */);
28766 Vinhibit_redisplay = Qnil;
28768 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28769 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28770 Vglobal_mode_string = Qnil;
28772 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28773 doc: /* Marker for where to display an arrow on top of the buffer text.
28774 This must be the beginning of a line in order to work.
28775 See also `overlay-arrow-string'. */);
28776 Voverlay_arrow_position = Qnil;
28778 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28779 doc: /* String to display as an arrow in non-window frames.
28780 See also `overlay-arrow-position'. */);
28781 Voverlay_arrow_string = build_pure_c_string ("=>");
28783 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28784 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28785 The symbols on this list are examined during redisplay to determine
28786 where to display overlay arrows. */);
28787 Voverlay_arrow_variable_list
28788 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28790 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28791 doc: /* The number of lines to try scrolling a window by when point moves out.
28792 If that fails to bring point back on frame, point is centered instead.
28793 If this is zero, point is always centered after it moves off frame.
28794 If you want scrolling to always be a line at a time, you should set
28795 `scroll-conservatively' to a large value rather than set this to 1. */);
28797 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28798 doc: /* Scroll up to this many lines, to bring point back on screen.
28799 If point moves off-screen, redisplay will scroll by up to
28800 `scroll-conservatively' lines in order to bring point just barely
28801 onto the screen again. If that cannot be done, then redisplay
28802 recenters point as usual.
28804 If the value is greater than 100, redisplay will never recenter point,
28805 but will always scroll just enough text to bring point into view, even
28806 if you move far away.
28808 A value of zero means always recenter point if it moves off screen. */);
28809 scroll_conservatively = 0;
28811 DEFVAR_INT ("scroll-margin", scroll_margin,
28812 doc: /* Number of lines of margin at the top and bottom of a window.
28813 Recenter the window whenever point gets within this many lines
28814 of the top or bottom of the window. */);
28815 scroll_margin = 0;
28817 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28818 doc: /* Pixels per inch value for non-window system displays.
28819 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28820 Vdisplay_pixels_per_inch = make_float (72.0);
28822 #ifdef GLYPH_DEBUG
28823 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28824 #endif
28826 DEFVAR_LISP ("truncate-partial-width-windows",
28827 Vtruncate_partial_width_windows,
28828 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28829 For an integer value, truncate lines in each window narrower than the
28830 full frame width, provided the window width is less than that integer;
28831 otherwise, respect the value of `truncate-lines'.
28833 For any other non-nil value, truncate lines in all windows that do
28834 not span the full frame width.
28836 A value of nil means to respect the value of `truncate-lines'.
28838 If `word-wrap' is enabled, you might want to reduce this. */);
28839 Vtruncate_partial_width_windows = make_number (50);
28841 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28842 doc: /* Maximum buffer size for which line number should be displayed.
28843 If the buffer is bigger than this, the line number does not appear
28844 in the mode line. A value of nil means no limit. */);
28845 Vline_number_display_limit = Qnil;
28847 DEFVAR_INT ("line-number-display-limit-width",
28848 line_number_display_limit_width,
28849 doc: /* Maximum line width (in characters) for line number display.
28850 If the average length of the lines near point is bigger than this, then the
28851 line number may be omitted from the mode line. */);
28852 line_number_display_limit_width = 200;
28854 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28855 doc: /* Non-nil means highlight region even in nonselected windows. */);
28856 highlight_nonselected_windows = 0;
28858 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28859 doc: /* Non-nil if more than one frame is visible on this display.
28860 Minibuffer-only frames don't count, but iconified frames do.
28861 This variable is not guaranteed to be accurate except while processing
28862 `frame-title-format' and `icon-title-format'. */);
28864 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28865 doc: /* Template for displaying the title bar of visible frames.
28866 \(Assuming the window manager supports this feature.)
28868 This variable has the same structure as `mode-line-format', except that
28869 the %c and %l constructs are ignored. It is used only on frames for
28870 which no explicit name has been set \(see `modify-frame-parameters'). */);
28872 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28873 doc: /* Template for displaying the title bar of an iconified frame.
28874 \(Assuming the window manager supports this feature.)
28875 This variable has the same structure as `mode-line-format' (which see),
28876 and is used only on frames for which no explicit name has been set
28877 \(see `modify-frame-parameters'). */);
28878 Vicon_title_format
28879 = Vframe_title_format
28880 = listn (CONSTYPE_PURE, 3,
28881 intern_c_string ("multiple-frames"),
28882 build_pure_c_string ("%b"),
28883 listn (CONSTYPE_PURE, 4,
28884 empty_unibyte_string,
28885 intern_c_string ("invocation-name"),
28886 build_pure_c_string ("@"),
28887 intern_c_string ("system-name")));
28889 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28890 doc: /* Maximum number of lines to keep in the message log buffer.
28891 If nil, disable message logging. If t, log messages but don't truncate
28892 the buffer when it becomes large. */);
28893 Vmessage_log_max = make_number (1000);
28895 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28896 doc: /* Functions called before redisplay, if window sizes have changed.
28897 The value should be a list of functions that take one argument.
28898 Just before redisplay, for each frame, if any of its windows have changed
28899 size since the last redisplay, or have been split or deleted,
28900 all the functions in the list are called, with the frame as argument. */);
28901 Vwindow_size_change_functions = Qnil;
28903 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28904 doc: /* List of functions to call before redisplaying a window with scrolling.
28905 Each function is called with two arguments, the window and its new
28906 display-start position. Note that these functions are also called by
28907 `set-window-buffer'. Also note that the value of `window-end' is not
28908 valid when these functions are called.
28910 Warning: Do not use this feature to alter the way the window
28911 is scrolled. It is not designed for that, and such use probably won't
28912 work. */);
28913 Vwindow_scroll_functions = Qnil;
28915 DEFVAR_LISP ("window-text-change-functions",
28916 Vwindow_text_change_functions,
28917 doc: /* Functions to call in redisplay when text in the window might change. */);
28918 Vwindow_text_change_functions = Qnil;
28920 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28921 doc: /* Functions called when redisplay of a window reaches the end trigger.
28922 Each function is called with two arguments, the window and the end trigger value.
28923 See `set-window-redisplay-end-trigger'. */);
28924 Vredisplay_end_trigger_functions = Qnil;
28926 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28927 doc: /* Non-nil means autoselect window with mouse pointer.
28928 If nil, do not autoselect windows.
28929 A positive number means delay autoselection by that many seconds: a
28930 window is autoselected only after the mouse has remained in that
28931 window for the duration of the delay.
28932 A negative number has a similar effect, but causes windows to be
28933 autoselected only after the mouse has stopped moving. \(Because of
28934 the way Emacs compares mouse events, you will occasionally wait twice
28935 that time before the window gets selected.\)
28936 Any other value means to autoselect window instantaneously when the
28937 mouse pointer enters it.
28939 Autoselection selects the minibuffer only if it is active, and never
28940 unselects the minibuffer if it is active.
28942 When customizing this variable make sure that the actual value of
28943 `focus-follows-mouse' matches the behavior of your window manager. */);
28944 Vmouse_autoselect_window = Qnil;
28946 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28947 doc: /* Non-nil means automatically resize tool-bars.
28948 This dynamically changes the tool-bar's height to the minimum height
28949 that is needed to make all tool-bar items visible.
28950 If value is `grow-only', the tool-bar's height is only increased
28951 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28952 Vauto_resize_tool_bars = Qt;
28954 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28955 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28956 auto_raise_tool_bar_buttons_p = 1;
28958 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28959 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28960 make_cursor_line_fully_visible_p = 1;
28962 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28963 doc: /* Border below tool-bar in pixels.
28964 If an integer, use it as the height of the border.
28965 If it is one of `internal-border-width' or `border-width', use the
28966 value of the corresponding frame parameter.
28967 Otherwise, no border is added below the tool-bar. */);
28968 Vtool_bar_border = Qinternal_border_width;
28970 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28971 doc: /* Margin around tool-bar buttons in pixels.
28972 If an integer, use that for both horizontal and vertical margins.
28973 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28974 HORZ specifying the horizontal margin, and VERT specifying the
28975 vertical margin. */);
28976 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28978 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28979 doc: /* Relief thickness of tool-bar buttons. */);
28980 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28982 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28983 doc: /* Tool bar style to use.
28984 It can be one of
28985 image - show images only
28986 text - show text only
28987 both - show both, text below image
28988 both-horiz - show text to the right of the image
28989 text-image-horiz - show text to the left of the image
28990 any other - use system default or image if no system default.
28992 This variable only affects the GTK+ toolkit version of Emacs. */);
28993 Vtool_bar_style = Qnil;
28995 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28996 doc: /* Maximum number of characters a label can have to be shown.
28997 The tool bar style must also show labels for this to have any effect, see
28998 `tool-bar-style'. */);
28999 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29001 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29002 doc: /* List of functions to call to fontify regions of text.
29003 Each function is called with one argument POS. Functions must
29004 fontify a region starting at POS in the current buffer, and give
29005 fontified regions the property `fontified'. */);
29006 Vfontification_functions = Qnil;
29007 Fmake_variable_buffer_local (Qfontification_functions);
29009 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29010 unibyte_display_via_language_environment,
29011 doc: /* Non-nil means display unibyte text according to language environment.
29012 Specifically, this means that raw bytes in the range 160-255 decimal
29013 are displayed by converting them to the equivalent multibyte characters
29014 according to the current language environment. As a result, they are
29015 displayed according to the current fontset.
29017 Note that this variable affects only how these bytes are displayed,
29018 but does not change the fact they are interpreted as raw bytes. */);
29019 unibyte_display_via_language_environment = 0;
29021 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29022 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29023 If a float, it specifies a fraction of the mini-window frame's height.
29024 If an integer, it specifies a number of lines. */);
29025 Vmax_mini_window_height = make_float (0.25);
29027 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29028 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29029 A value of nil means don't automatically resize mini-windows.
29030 A value of t means resize them to fit the text displayed in them.
29031 A value of `grow-only', the default, means let mini-windows grow only;
29032 they return to their normal size when the minibuffer is closed, or the
29033 echo area becomes empty. */);
29034 Vresize_mini_windows = Qgrow_only;
29036 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29037 doc: /* Alist specifying how to blink the cursor off.
29038 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29039 `cursor-type' frame-parameter or variable equals ON-STATE,
29040 comparing using `equal', Emacs uses OFF-STATE to specify
29041 how to blink it off. ON-STATE and OFF-STATE are values for
29042 the `cursor-type' frame parameter.
29044 If a frame's ON-STATE has no entry in this list,
29045 the frame's other specifications determine how to blink the cursor off. */);
29046 Vblink_cursor_alist = Qnil;
29048 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29049 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29050 If non-nil, windows are automatically scrolled horizontally to make
29051 point visible. */);
29052 automatic_hscrolling_p = 1;
29053 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29055 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29056 doc: /* How many columns away from the window edge point is allowed to get
29057 before automatic hscrolling will horizontally scroll the window. */);
29058 hscroll_margin = 5;
29060 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29061 doc: /* How many columns to scroll the window when point gets too close to the edge.
29062 When point is less than `hscroll-margin' columns from the window
29063 edge, automatic hscrolling will scroll the window by the amount of columns
29064 determined by this variable. If its value is a positive integer, scroll that
29065 many columns. If it's a positive floating-point number, it specifies the
29066 fraction of the window's width to scroll. If it's nil or zero, point will be
29067 centered horizontally after the scroll. Any other value, including negative
29068 numbers, are treated as if the value were zero.
29070 Automatic hscrolling always moves point outside the scroll margin, so if
29071 point was more than scroll step columns inside the margin, the window will
29072 scroll more than the value given by the scroll step.
29074 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29075 and `scroll-right' overrides this variable's effect. */);
29076 Vhscroll_step = make_number (0);
29078 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29079 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29080 Bind this around calls to `message' to let it take effect. */);
29081 message_truncate_lines = 0;
29083 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29084 doc: /* Normal hook run to update the menu bar definitions.
29085 Redisplay runs this hook before it redisplays the menu bar.
29086 This is used to update submenus such as Buffers,
29087 whose contents depend on various data. */);
29088 Vmenu_bar_update_hook = Qnil;
29090 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29091 doc: /* Frame for which we are updating a menu.
29092 The enable predicate for a menu binding should check this variable. */);
29093 Vmenu_updating_frame = Qnil;
29095 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29096 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29097 inhibit_menubar_update = 0;
29099 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29100 doc: /* Prefix prepended to all continuation lines at display time.
29101 The value may be a string, an image, or a stretch-glyph; it is
29102 interpreted in the same way as the value of a `display' text property.
29104 This variable is overridden by any `wrap-prefix' text or overlay
29105 property.
29107 To add a prefix to non-continuation lines, use `line-prefix'. */);
29108 Vwrap_prefix = Qnil;
29109 DEFSYM (Qwrap_prefix, "wrap-prefix");
29110 Fmake_variable_buffer_local (Qwrap_prefix);
29112 DEFVAR_LISP ("line-prefix", Vline_prefix,
29113 doc: /* Prefix prepended to all non-continuation lines at display time.
29114 The value may be a string, an image, or a stretch-glyph; it is
29115 interpreted in the same way as the value of a `display' text property.
29117 This variable is overridden by any `line-prefix' text or overlay
29118 property.
29120 To add a prefix to continuation lines, use `wrap-prefix'. */);
29121 Vline_prefix = Qnil;
29122 DEFSYM (Qline_prefix, "line-prefix");
29123 Fmake_variable_buffer_local (Qline_prefix);
29125 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29126 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29127 inhibit_eval_during_redisplay = 0;
29129 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29130 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29131 inhibit_free_realized_faces = 0;
29133 #ifdef GLYPH_DEBUG
29134 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29135 doc: /* Inhibit try_window_id display optimization. */);
29136 inhibit_try_window_id = 0;
29138 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29139 doc: /* Inhibit try_window_reusing display optimization. */);
29140 inhibit_try_window_reusing = 0;
29142 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29143 doc: /* Inhibit try_cursor_movement display optimization. */);
29144 inhibit_try_cursor_movement = 0;
29145 #endif /* GLYPH_DEBUG */
29147 DEFVAR_INT ("overline-margin", overline_margin,
29148 doc: /* Space between overline and text, in pixels.
29149 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29150 margin to the character height. */);
29151 overline_margin = 2;
29153 DEFVAR_INT ("underline-minimum-offset",
29154 underline_minimum_offset,
29155 doc: /* Minimum distance between baseline and underline.
29156 This can improve legibility of underlined text at small font sizes,
29157 particularly when using variable `x-use-underline-position-properties'
29158 with fonts that specify an UNDERLINE_POSITION relatively close to the
29159 baseline. The default value is 1. */);
29160 underline_minimum_offset = 1;
29162 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29163 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29164 This feature only works when on a window system that can change
29165 cursor shapes. */);
29166 display_hourglass_p = 1;
29168 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29169 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29170 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29172 hourglass_atimer = NULL;
29173 hourglass_shown_p = 0;
29175 DEFSYM (Qglyphless_char, "glyphless-char");
29176 DEFSYM (Qhex_code, "hex-code");
29177 DEFSYM (Qempty_box, "empty-box");
29178 DEFSYM (Qthin_space, "thin-space");
29179 DEFSYM (Qzero_width, "zero-width");
29181 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29182 /* Intern this now in case it isn't already done.
29183 Setting this variable twice is harmless.
29184 But don't staticpro it here--that is done in alloc.c. */
29185 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29186 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29188 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29189 doc: /* Char-table defining glyphless characters.
29190 Each element, if non-nil, should be one of the following:
29191 an ASCII acronym string: display this string in a box
29192 `hex-code': display the hexadecimal code of a character in a box
29193 `empty-box': display as an empty box
29194 `thin-space': display as 1-pixel width space
29195 `zero-width': don't display
29196 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29197 display method for graphical terminals and text terminals respectively.
29198 GRAPHICAL and TEXT should each have one of the values listed above.
29200 The char-table has one extra slot to control the display of a character for
29201 which no font is found. This slot only takes effect on graphical terminals.
29202 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29203 `thin-space'. The default is `empty-box'. */);
29204 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29205 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29206 Qempty_box);
29208 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29209 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29210 Vdebug_on_message = Qnil;
29214 /* Initialize this module when Emacs starts. */
29216 void
29217 init_xdisp (void)
29219 current_header_line_height = current_mode_line_height = -1;
29221 CHARPOS (this_line_start_pos) = 0;
29223 if (!noninteractive)
29225 struct window *m = XWINDOW (minibuf_window);
29226 Lisp_Object frame = m->frame;
29227 struct frame *f = XFRAME (frame);
29228 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29229 struct window *r = XWINDOW (root);
29230 int i;
29232 echo_area_window = minibuf_window;
29234 r->top_line = FRAME_TOP_MARGIN (f);
29235 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29236 r->total_cols = FRAME_COLS (f);
29238 m->top_line = FRAME_LINES (f) - 1;
29239 m->total_lines = 1;
29240 m->total_cols = FRAME_COLS (f);
29242 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29243 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29244 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29246 /* The default ellipsis glyphs `...'. */
29247 for (i = 0; i < 3; ++i)
29248 default_invis_vector[i] = make_number ('.');
29252 /* Allocate the buffer for frame titles.
29253 Also used for `format-mode-line'. */
29254 int size = 100;
29255 mode_line_noprop_buf = xmalloc (size);
29256 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29257 mode_line_noprop_ptr = mode_line_noprop_buf;
29258 mode_line_target = MODE_LINE_DISPLAY;
29261 help_echo_showing_p = 0;
29264 /* Platform-independent portion of hourglass implementation. */
29266 /* Cancel a currently active hourglass timer, and start a new one. */
29267 void
29268 start_hourglass (void)
29270 #if defined (HAVE_WINDOW_SYSTEM)
29271 EMACS_TIME delay;
29273 cancel_hourglass ();
29275 if (INTEGERP (Vhourglass_delay)
29276 && XINT (Vhourglass_delay) > 0)
29277 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29278 TYPE_MAXIMUM (time_t)),
29280 else if (FLOATP (Vhourglass_delay)
29281 && XFLOAT_DATA (Vhourglass_delay) > 0)
29282 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29283 else
29284 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29286 #ifdef HAVE_NTGUI
29288 extern void w32_note_current_window (void);
29289 w32_note_current_window ();
29291 #endif /* HAVE_NTGUI */
29293 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29294 show_hourglass, NULL);
29295 #endif
29299 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29300 shown. */
29301 void
29302 cancel_hourglass (void)
29304 #if defined (HAVE_WINDOW_SYSTEM)
29305 if (hourglass_atimer)
29307 cancel_atimer (hourglass_atimer);
29308 hourglass_atimer = NULL;
29311 if (hourglass_shown_p)
29312 hide_hourglass ();
29313 #endif