Silence cua-mode compilation
[emacs.git] / src / xdisp.c
blob9f3be44ecfdae30fa99d86a6ab68e8cfacc471dc
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 static void reseat_at_next_visible_line_start (struct it *, int);
885 static int next_element_from_ellipsis (struct it *);
886 static int next_element_from_display_vector (struct it *);
887 static int next_element_from_string (struct it *);
888 static int next_element_from_c_string (struct it *);
889 static int next_element_from_buffer (struct it *);
890 static int next_element_from_composition (struct it *);
891 static int next_element_from_image (struct it *);
892 static int next_element_from_stretch (struct it *);
893 static void load_overlay_strings (struct it *, ptrdiff_t);
894 static int init_from_display_pos (struct it *, struct window *,
895 struct display_pos *);
896 static void reseat_to_string (struct it *, const char *,
897 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
898 static int get_next_display_element (struct it *);
899 static enum move_it_result
900 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
901 enum move_operation_enum);
902 static void get_visually_first_element (struct it *);
903 static void init_to_row_start (struct it *, struct window *,
904 struct glyph_row *);
905 static int init_to_row_end (struct it *, struct window *,
906 struct glyph_row *);
907 static void back_to_previous_line_start (struct it *);
908 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
909 static struct text_pos string_pos_nchars_ahead (struct text_pos,
910 Lisp_Object, ptrdiff_t);
911 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
912 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
913 static ptrdiff_t number_of_chars (const char *, bool);
914 static void compute_stop_pos (struct it *);
915 static void compute_string_pos (struct text_pos *, struct text_pos,
916 Lisp_Object);
917 static int face_before_or_after_it_pos (struct it *, int);
918 static ptrdiff_t next_overlay_change (ptrdiff_t);
919 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
920 Lisp_Object, struct text_pos *, ptrdiff_t, int);
921 static int handle_single_display_spec (struct it *, Lisp_Object,
922 Lisp_Object, Lisp_Object,
923 struct text_pos *, ptrdiff_t, int, int);
924 static int underlying_face_id (struct it *);
925 static int in_ellipses_for_invisible_text_p (struct display_pos *,
926 struct window *);
928 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
929 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
931 #ifdef HAVE_WINDOW_SYSTEM
933 static void x_consider_frame_title (Lisp_Object);
934 static int tool_bar_lines_needed (struct frame *, int *);
935 static void update_tool_bar (struct frame *, int);
936 static void build_desired_tool_bar_string (struct frame *f);
937 static int redisplay_tool_bar (struct frame *);
938 static void display_tool_bar_line (struct it *, int);
939 static void notice_overwritten_cursor (struct window *,
940 enum glyph_row_area,
941 int, int, int, int);
942 static void append_stretch_glyph (struct it *, Lisp_Object,
943 int, int, int);
946 #endif /* HAVE_WINDOW_SYSTEM */
948 static void produce_special_glyphs (struct it *, enum display_element_type);
949 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
950 static int coords_in_mouse_face_p (struct window *, int, int);
954 /***********************************************************************
955 Window display dimensions
956 ***********************************************************************/
958 /* Return the bottom boundary y-position for text lines in window W.
959 This is the first y position at which a line cannot start.
960 It is relative to the top of the window.
962 This is the height of W minus the height of a mode line, if any. */
965 window_text_bottom_y (struct window *w)
967 int height = WINDOW_TOTAL_HEIGHT (w);
969 if (WINDOW_WANTS_MODELINE_P (w))
970 height -= CURRENT_MODE_LINE_HEIGHT (w);
971 return height;
974 /* Return the pixel width of display area AREA of window W. AREA < 0
975 means return the total width of W, not including fringes to
976 the left and right of the window. */
979 window_box_width (struct window *w, int area)
981 int cols = w->total_cols;
982 int pixels = 0;
984 if (!w->pseudo_window_p)
986 cols -= WINDOW_SCROLL_BAR_COLS (w);
988 if (area == TEXT_AREA)
990 if (INTEGERP (w->left_margin_cols))
991 cols -= XFASTINT (w->left_margin_cols);
992 if (INTEGERP (w->right_margin_cols))
993 cols -= XFASTINT (w->right_margin_cols);
994 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
996 else if (area == LEFT_MARGIN_AREA)
998 cols = (INTEGERP (w->left_margin_cols)
999 ? XFASTINT (w->left_margin_cols) : 0);
1000 pixels = 0;
1002 else if (area == RIGHT_MARGIN_AREA)
1004 cols = (INTEGERP (w->right_margin_cols)
1005 ? XFASTINT (w->right_margin_cols) : 0);
1006 pixels = 0;
1010 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1014 /* Return the pixel height of the display area of window W, not
1015 including mode lines of W, if any. */
1018 window_box_height (struct window *w)
1020 struct frame *f = XFRAME (w->frame);
1021 int height = WINDOW_TOTAL_HEIGHT (w);
1023 eassert (height >= 0);
1025 /* Note: the code below that determines the mode-line/header-line
1026 height is essentially the same as that contained in the macro
1027 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1028 the appropriate glyph row has its `mode_line_p' flag set,
1029 and if it doesn't, uses estimate_mode_line_height instead. */
1031 if (WINDOW_WANTS_MODELINE_P (w))
1033 struct glyph_row *ml_row
1034 = (w->current_matrix && w->current_matrix->rows
1035 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1036 : 0);
1037 if (ml_row && ml_row->mode_line_p)
1038 height -= ml_row->height;
1039 else
1040 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1043 if (WINDOW_WANTS_HEADER_LINE_P (w))
1045 struct glyph_row *hl_row
1046 = (w->current_matrix && w->current_matrix->rows
1047 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1048 : 0);
1049 if (hl_row && hl_row->mode_line_p)
1050 height -= hl_row->height;
1051 else
1052 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1055 /* With a very small font and a mode-line that's taller than
1056 default, we might end up with a negative height. */
1057 return max (0, height);
1060 /* Return the window-relative coordinate of the left edge of display
1061 area AREA of window W. AREA < 0 means return the left edge of the
1062 whole window, to the right of the left fringe of W. */
1065 window_box_left_offset (struct window *w, int area)
1067 int x;
1069 if (w->pseudo_window_p)
1070 return 0;
1072 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1074 if (area == TEXT_AREA)
1075 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1076 + window_box_width (w, LEFT_MARGIN_AREA));
1077 else if (area == RIGHT_MARGIN_AREA)
1078 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1079 + window_box_width (w, LEFT_MARGIN_AREA)
1080 + window_box_width (w, TEXT_AREA)
1081 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1083 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1084 else if (area == LEFT_MARGIN_AREA
1085 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1086 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1088 return x;
1092 /* Return the window-relative coordinate of the right edge of display
1093 area AREA of window W. AREA < 0 means return the right edge of the
1094 whole window, to the left of the right fringe of W. */
1097 window_box_right_offset (struct window *w, int area)
1099 return window_box_left_offset (w, area) + window_box_width (w, area);
1102 /* Return the frame-relative coordinate of the left edge of display
1103 area AREA of window W. AREA < 0 means return the left edge of the
1104 whole window, to the right of the left fringe of W. */
1107 window_box_left (struct window *w, int area)
1109 struct frame *f = XFRAME (w->frame);
1110 int x;
1112 if (w->pseudo_window_p)
1113 return FRAME_INTERNAL_BORDER_WIDTH (f);
1115 x = (WINDOW_LEFT_EDGE_X (w)
1116 + window_box_left_offset (w, area));
1118 return x;
1122 /* Return the frame-relative coordinate of the right edge of display
1123 area AREA of window W. AREA < 0 means return the right edge of the
1124 whole window, to the left of the right fringe of W. */
1127 window_box_right (struct window *w, int area)
1129 return window_box_left (w, area) + window_box_width (w, area);
1132 /* Get the bounding box of the display area AREA of window W, without
1133 mode lines, in frame-relative coordinates. AREA < 0 means the
1134 whole window, not including the left and right fringes of
1135 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1136 coordinates of the upper-left corner of the box. Return in
1137 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1139 void
1140 window_box (struct window *w, int area, int *box_x, int *box_y,
1141 int *box_width, int *box_height)
1143 if (box_width)
1144 *box_width = window_box_width (w, area);
1145 if (box_height)
1146 *box_height = window_box_height (w);
1147 if (box_x)
1148 *box_x = window_box_left (w, area);
1149 if (box_y)
1151 *box_y = WINDOW_TOP_EDGE_Y (w);
1152 if (WINDOW_WANTS_HEADER_LINE_P (w))
1153 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1158 /* Get the bounding box of the display area AREA of window W, without
1159 mode lines. AREA < 0 means the whole window, not including the
1160 left and right fringe of the window. Return in *TOP_LEFT_X
1161 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1162 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1163 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1164 box. */
1166 static void
1167 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1168 int *bottom_right_x, int *bottom_right_y)
1170 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1171 bottom_right_y);
1172 *bottom_right_x += *top_left_x;
1173 *bottom_right_y += *top_left_y;
1178 /***********************************************************************
1179 Utilities
1180 ***********************************************************************/
1182 /* Return the bottom y-position of the line the iterator IT is in.
1183 This can modify IT's settings. */
1186 line_bottom_y (struct it *it)
1188 int line_height = it->max_ascent + it->max_descent;
1189 int line_top_y = it->current_y;
1191 if (line_height == 0)
1193 if (last_height)
1194 line_height = last_height;
1195 else if (IT_CHARPOS (*it) < ZV)
1197 move_it_by_lines (it, 1);
1198 line_height = (it->max_ascent || it->max_descent
1199 ? it->max_ascent + it->max_descent
1200 : last_height);
1202 else
1204 struct glyph_row *row = it->glyph_row;
1206 /* Use the default character height. */
1207 it->glyph_row = NULL;
1208 it->what = IT_CHARACTER;
1209 it->c = ' ';
1210 it->len = 1;
1211 PRODUCE_GLYPHS (it);
1212 line_height = it->ascent + it->descent;
1213 it->glyph_row = row;
1217 return line_top_y + line_height;
1220 /* Subroutine of pos_visible_p below. Extracts a display string, if
1221 any, from the display spec given as its argument. */
1222 static Lisp_Object
1223 string_from_display_spec (Lisp_Object spec)
1225 if (CONSP (spec))
1227 while (CONSP (spec))
1229 if (STRINGP (XCAR (spec)))
1230 return XCAR (spec);
1231 spec = XCDR (spec);
1234 else if (VECTORP (spec))
1236 ptrdiff_t i;
1238 for (i = 0; i < ASIZE (spec); i++)
1240 if (STRINGP (AREF (spec, i)))
1241 return AREF (spec, i);
1243 return Qnil;
1246 return spec;
1250 /* Limit insanely large values of W->hscroll on frame F to the largest
1251 value that will still prevent first_visible_x and last_visible_x of
1252 'struct it' from overflowing an int. */
1253 static int
1254 window_hscroll_limited (struct window *w, struct frame *f)
1256 ptrdiff_t window_hscroll = w->hscroll;
1257 int window_text_width = window_box_width (w, TEXT_AREA);
1258 int colwidth = FRAME_COLUMN_WIDTH (f);
1260 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1261 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1263 return window_hscroll;
1266 /* Return 1 if position CHARPOS is visible in window W.
1267 CHARPOS < 0 means return info about WINDOW_END position.
1268 If visible, set *X and *Y to pixel coordinates of top left corner.
1269 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1270 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1273 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1274 int *rtop, int *rbot, int *rowh, int *vpos)
1276 struct it it;
1277 void *itdata = bidi_shelve_cache ();
1278 struct text_pos top;
1279 int visible_p = 0;
1280 struct buffer *old_buffer = NULL;
1282 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1283 return visible_p;
1285 if (XBUFFER (w->contents) != current_buffer)
1287 old_buffer = current_buffer;
1288 set_buffer_internal_1 (XBUFFER (w->contents));
1291 SET_TEXT_POS_FROM_MARKER (top, w->start);
1292 /* Scrolling a minibuffer window via scroll bar when the echo area
1293 shows long text sometimes resets the minibuffer contents behind
1294 our backs. */
1295 if (CHARPOS (top) > ZV)
1296 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1298 /* Compute exact mode line heights. */
1299 if (WINDOW_WANTS_MODELINE_P (w))
1300 current_mode_line_height
1301 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1302 BVAR (current_buffer, mode_line_format));
1304 if (WINDOW_WANTS_HEADER_LINE_P (w))
1305 current_header_line_height
1306 = display_mode_line (w, HEADER_LINE_FACE_ID,
1307 BVAR (current_buffer, header_line_format));
1309 start_display (&it, w, top);
1310 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1311 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1313 if (charpos >= 0
1314 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1315 && IT_CHARPOS (it) >= charpos)
1316 /* When scanning backwards under bidi iteration, move_it_to
1317 stops at or _before_ CHARPOS, because it stops at or to
1318 the _right_ of the character at CHARPOS. */
1319 || (it.bidi_p && it.bidi_it.scan_dir == -1
1320 && IT_CHARPOS (it) <= charpos)))
1322 /* We have reached CHARPOS, or passed it. How the call to
1323 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1324 or covered by a display property, move_it_to stops at the end
1325 of the invisible text, to the right of CHARPOS. (ii) If
1326 CHARPOS is in a display vector, move_it_to stops on its last
1327 glyph. */
1328 int top_x = it.current_x;
1329 int top_y = it.current_y;
1330 /* Calling line_bottom_y may change it.method, it.position, etc. */
1331 enum it_method it_method = it.method;
1332 int bottom_y = (last_height = 0, line_bottom_y (&it));
1333 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1335 if (top_y < window_top_y)
1336 visible_p = bottom_y > window_top_y;
1337 else if (top_y < it.last_visible_y)
1338 visible_p = 1;
1339 if (bottom_y >= it.last_visible_y
1340 && it.bidi_p && it.bidi_it.scan_dir == -1
1341 && IT_CHARPOS (it) < charpos)
1343 /* When the last line of the window is scanned backwards
1344 under bidi iteration, we could be duped into thinking
1345 that we have passed CHARPOS, when in fact move_it_to
1346 simply stopped short of CHARPOS because it reached
1347 last_visible_y. To see if that's what happened, we call
1348 move_it_to again with a slightly larger vertical limit,
1349 and see if it actually moved vertically; if it did, we
1350 didn't really reach CHARPOS, which is beyond window end. */
1351 struct it save_it = it;
1352 /* Why 10? because we don't know how many canonical lines
1353 will the height of the next line(s) be. So we guess. */
1354 int ten_more_lines =
1355 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1357 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1358 MOVE_TO_POS | MOVE_TO_Y);
1359 if (it.current_y > top_y)
1360 visible_p = 0;
1362 it = save_it;
1364 if (visible_p)
1366 if (it_method == GET_FROM_DISPLAY_VECTOR)
1368 /* We stopped on the last glyph of a display vector.
1369 Try and recompute. Hack alert! */
1370 if (charpos < 2 || top.charpos >= charpos)
1371 top_x = it.glyph_row->x;
1372 else
1374 struct it it2, it2_prev;
1375 /* The idea is to get to the previous buffer
1376 position, consume the character there, and use
1377 the pixel coordinates we get after that. But if
1378 the previous buffer position is also displayed
1379 from a display vector, we need to consume all of
1380 the glyphs from that display vector. */
1381 start_display (&it2, w, top);
1382 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1383 /* If we didn't get to CHARPOS - 1, there's some
1384 replacing display property at that position, and
1385 we stopped after it. That is exactly the place
1386 whose coordinates we want. */
1387 if (IT_CHARPOS (it2) != charpos - 1)
1388 it2_prev = it2;
1389 else
1391 /* Iterate until we get out of the display
1392 vector that displays the character at
1393 CHARPOS - 1. */
1394 do {
1395 get_next_display_element (&it2);
1396 PRODUCE_GLYPHS (&it2);
1397 it2_prev = it2;
1398 set_iterator_to_next (&it2, 1);
1399 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1400 && IT_CHARPOS (it2) < charpos);
1402 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1403 || it2_prev.current_x > it2_prev.last_visible_x)
1404 top_x = it.glyph_row->x;
1405 else
1407 top_x = it2_prev.current_x;
1408 top_y = it2_prev.current_y;
1412 else if (IT_CHARPOS (it) != charpos)
1414 Lisp_Object cpos = make_number (charpos);
1415 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1416 Lisp_Object string = string_from_display_spec (spec);
1417 struct text_pos tpos;
1418 int replacing_spec_p;
1419 bool newline_in_string
1420 = (STRINGP (string)
1421 && memchr (SDATA (string), '\n', SBYTES (string)));
1423 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1424 replacing_spec_p
1425 = (!NILP (spec)
1426 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1427 charpos, FRAME_WINDOW_P (it.f)));
1428 /* The tricky code below is needed because there's a
1429 discrepancy between move_it_to and how we set cursor
1430 when PT is at the beginning of a portion of text
1431 covered by a display property or an overlay with a
1432 display property, or the display line ends in a
1433 newline from a display string. move_it_to will stop
1434 _after_ such display strings, whereas
1435 set_cursor_from_row conspires with cursor_row_p to
1436 place the cursor on the first glyph produced from the
1437 display string. */
1439 /* We have overshoot PT because it is covered by a
1440 display property that replaces the text it covers.
1441 If the string includes embedded newlines, we are also
1442 in the wrong display line. Backtrack to the correct
1443 line, where the display property begins. */
1444 if (replacing_spec_p)
1446 Lisp_Object startpos, endpos;
1447 EMACS_INT start, end;
1448 struct it it3;
1449 int it3_moved;
1451 /* Find the first and the last buffer positions
1452 covered by the display string. */
1453 endpos =
1454 Fnext_single_char_property_change (cpos, Qdisplay,
1455 Qnil, Qnil);
1456 startpos =
1457 Fprevious_single_char_property_change (endpos, Qdisplay,
1458 Qnil, Qnil);
1459 start = XFASTINT (startpos);
1460 end = XFASTINT (endpos);
1461 /* Move to the last buffer position before the
1462 display property. */
1463 start_display (&it3, w, top);
1464 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1465 /* Move forward one more line if the position before
1466 the display string is a newline or if it is the
1467 rightmost character on a line that is
1468 continued or word-wrapped. */
1469 if (it3.method == GET_FROM_BUFFER
1470 && (it3.c == '\n'
1471 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1472 move_it_by_lines (&it3, 1);
1473 else if (move_it_in_display_line_to (&it3, -1,
1474 it3.current_x
1475 + it3.pixel_width,
1476 MOVE_TO_X)
1477 == MOVE_LINE_CONTINUED)
1479 move_it_by_lines (&it3, 1);
1480 /* When we are under word-wrap, the #$@%!
1481 move_it_by_lines moves 2 lines, so we need to
1482 fix that up. */
1483 if (it3.line_wrap == WORD_WRAP)
1484 move_it_by_lines (&it3, -1);
1487 /* Record the vertical coordinate of the display
1488 line where we wound up. */
1489 top_y = it3.current_y;
1490 if (it3.bidi_p)
1492 /* When characters are reordered for display,
1493 the character displayed to the left of the
1494 display string could be _after_ the display
1495 property in the logical order. Use the
1496 smallest vertical position of these two. */
1497 start_display (&it3, w, top);
1498 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1499 if (it3.current_y < top_y)
1500 top_y = it3.current_y;
1502 /* Move from the top of the window to the beginning
1503 of the display line where the display string
1504 begins. */
1505 start_display (&it3, w, top);
1506 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1507 /* If it3_moved stays zero after the 'while' loop
1508 below, that means we already were at a newline
1509 before the loop (e.g., the display string begins
1510 with a newline), so we don't need to (and cannot)
1511 inspect the glyphs of it3.glyph_row, because
1512 PRODUCE_GLYPHS will not produce anything for a
1513 newline, and thus it3.glyph_row stays at its
1514 stale content it got at top of the window. */
1515 it3_moved = 0;
1516 /* Finally, advance the iterator until we hit the
1517 first display element whose character position is
1518 CHARPOS, or until the first newline from the
1519 display string, which signals the end of the
1520 display line. */
1521 while (get_next_display_element (&it3))
1523 PRODUCE_GLYPHS (&it3);
1524 if (IT_CHARPOS (it3) == charpos
1525 || ITERATOR_AT_END_OF_LINE_P (&it3))
1526 break;
1527 it3_moved = 1;
1528 set_iterator_to_next (&it3, 0);
1530 top_x = it3.current_x - it3.pixel_width;
1531 /* Normally, we would exit the above loop because we
1532 found the display element whose character
1533 position is CHARPOS. For the contingency that we
1534 didn't, and stopped at the first newline from the
1535 display string, move back over the glyphs
1536 produced from the string, until we find the
1537 rightmost glyph not from the string. */
1538 if (it3_moved
1539 && newline_in_string
1540 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1542 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1543 + it3.glyph_row->used[TEXT_AREA];
1545 while (EQ ((g - 1)->object, string))
1547 --g;
1548 top_x -= g->pixel_width;
1550 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1551 + it3.glyph_row->used[TEXT_AREA]);
1556 *x = top_x;
1557 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1558 *rtop = max (0, window_top_y - top_y);
1559 *rbot = max (0, bottom_y - it.last_visible_y);
1560 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1561 - max (top_y, window_top_y)));
1562 *vpos = it.vpos;
1565 else
1567 /* We were asked to provide info about WINDOW_END. */
1568 struct it it2;
1569 void *it2data = NULL;
1571 SAVE_IT (it2, it, it2data);
1572 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1573 move_it_by_lines (&it, 1);
1574 if (charpos < IT_CHARPOS (it)
1575 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1577 visible_p = 1;
1578 RESTORE_IT (&it2, &it2, it2data);
1579 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1580 *x = it2.current_x;
1581 *y = it2.current_y + it2.max_ascent - it2.ascent;
1582 *rtop = max (0, -it2.current_y);
1583 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1584 - it.last_visible_y));
1585 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1586 it.last_visible_y)
1587 - max (it2.current_y,
1588 WINDOW_HEADER_LINE_HEIGHT (w))));
1589 *vpos = it2.vpos;
1591 else
1592 bidi_unshelve_cache (it2data, 1);
1594 bidi_unshelve_cache (itdata, 0);
1596 if (old_buffer)
1597 set_buffer_internal_1 (old_buffer);
1599 current_header_line_height = current_mode_line_height = -1;
1601 if (visible_p && w->hscroll > 0)
1602 *x -=
1603 window_hscroll_limited (w, WINDOW_XFRAME (w))
1604 * WINDOW_FRAME_COLUMN_WIDTH (w);
1606 #if 0
1607 /* Debugging code. */
1608 if (visible_p)
1609 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1610 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1611 else
1612 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1613 #endif
1615 return visible_p;
1619 /* Return the next character from STR. Return in *LEN the length of
1620 the character. This is like STRING_CHAR_AND_LENGTH but never
1621 returns an invalid character. If we find one, we return a `?', but
1622 with the length of the invalid character. */
1624 static int
1625 string_char_and_length (const unsigned char *str, int *len)
1627 int c;
1629 c = STRING_CHAR_AND_LENGTH (str, *len);
1630 if (!CHAR_VALID_P (c))
1631 /* We may not change the length here because other places in Emacs
1632 don't use this function, i.e. they silently accept invalid
1633 characters. */
1634 c = '?';
1636 return c;
1641 /* Given a position POS containing a valid character and byte position
1642 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1644 static struct text_pos
1645 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1647 eassert (STRINGP (string) && nchars >= 0);
1649 if (STRING_MULTIBYTE (string))
1651 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1652 int len;
1654 while (nchars--)
1656 string_char_and_length (p, &len);
1657 p += len;
1658 CHARPOS (pos) += 1;
1659 BYTEPOS (pos) += len;
1662 else
1663 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1665 return pos;
1669 /* Value is the text position, i.e. character and byte position,
1670 for character position CHARPOS in STRING. */
1672 static struct text_pos
1673 string_pos (ptrdiff_t charpos, Lisp_Object string)
1675 struct text_pos pos;
1676 eassert (STRINGP (string));
1677 eassert (charpos >= 0);
1678 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1679 return pos;
1683 /* Value is a text position, i.e. character and byte position, for
1684 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1685 means recognize multibyte characters. */
1687 static struct text_pos
1688 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1690 struct text_pos pos;
1692 eassert (s != NULL);
1693 eassert (charpos >= 0);
1695 if (multibyte_p)
1697 int len;
1699 SET_TEXT_POS (pos, 0, 0);
1700 while (charpos--)
1702 string_char_and_length ((const unsigned char *) s, &len);
1703 s += len;
1704 CHARPOS (pos) += 1;
1705 BYTEPOS (pos) += len;
1708 else
1709 SET_TEXT_POS (pos, charpos, charpos);
1711 return pos;
1715 /* Value is the number of characters in C string S. MULTIBYTE_P
1716 non-zero means recognize multibyte characters. */
1718 static ptrdiff_t
1719 number_of_chars (const char *s, bool multibyte_p)
1721 ptrdiff_t nchars;
1723 if (multibyte_p)
1725 ptrdiff_t rest = strlen (s);
1726 int len;
1727 const unsigned char *p = (const unsigned char *) s;
1729 for (nchars = 0; rest > 0; ++nchars)
1731 string_char_and_length (p, &len);
1732 rest -= len, p += len;
1735 else
1736 nchars = strlen (s);
1738 return nchars;
1742 /* Compute byte position NEWPOS->bytepos corresponding to
1743 NEWPOS->charpos. POS is a known position in string STRING.
1744 NEWPOS->charpos must be >= POS.charpos. */
1746 static void
1747 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1749 eassert (STRINGP (string));
1750 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1752 if (STRING_MULTIBYTE (string))
1753 *newpos = string_pos_nchars_ahead (pos, string,
1754 CHARPOS (*newpos) - CHARPOS (pos));
1755 else
1756 BYTEPOS (*newpos) = CHARPOS (*newpos);
1759 /* EXPORT:
1760 Return an estimation of the pixel height of mode or header lines on
1761 frame F. FACE_ID specifies what line's height to estimate. */
1764 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1766 #ifdef HAVE_WINDOW_SYSTEM
1767 if (FRAME_WINDOW_P (f))
1769 int height = FONT_HEIGHT (FRAME_FONT (f));
1771 /* This function is called so early when Emacs starts that the face
1772 cache and mode line face are not yet initialized. */
1773 if (FRAME_FACE_CACHE (f))
1775 struct face *face = FACE_FROM_ID (f, face_id);
1776 if (face)
1778 if (face->font)
1779 height = FONT_HEIGHT (face->font);
1780 if (face->box_line_width > 0)
1781 height += 2 * face->box_line_width;
1785 return height;
1787 #endif
1789 return 1;
1792 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1793 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1794 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1795 not force the value into range. */
1797 void
1798 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1799 int *x, int *y, NativeRectangle *bounds, int noclip)
1802 #ifdef HAVE_WINDOW_SYSTEM
1803 if (FRAME_WINDOW_P (f))
1805 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1806 even for negative values. */
1807 if (pix_x < 0)
1808 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1809 if (pix_y < 0)
1810 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1812 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1813 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1815 if (bounds)
1816 STORE_NATIVE_RECT (*bounds,
1817 FRAME_COL_TO_PIXEL_X (f, pix_x),
1818 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1819 FRAME_COLUMN_WIDTH (f) - 1,
1820 FRAME_LINE_HEIGHT (f) - 1);
1822 if (!noclip)
1824 if (pix_x < 0)
1825 pix_x = 0;
1826 else if (pix_x > FRAME_TOTAL_COLS (f))
1827 pix_x = FRAME_TOTAL_COLS (f);
1829 if (pix_y < 0)
1830 pix_y = 0;
1831 else if (pix_y > FRAME_LINES (f))
1832 pix_y = FRAME_LINES (f);
1835 #endif
1837 *x = pix_x;
1838 *y = pix_y;
1842 /* Find the glyph under window-relative coordinates X/Y in window W.
1843 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1844 strings. Return in *HPOS and *VPOS the row and column number of
1845 the glyph found. Return in *AREA the glyph area containing X.
1846 Value is a pointer to the glyph found or null if X/Y is not on
1847 text, or we can't tell because W's current matrix is not up to
1848 date. */
1850 static
1851 struct glyph *
1852 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1853 int *dx, int *dy, int *area)
1855 struct glyph *glyph, *end;
1856 struct glyph_row *row = NULL;
1857 int x0, i;
1859 /* Find row containing Y. Give up if some row is not enabled. */
1860 for (i = 0; i < w->current_matrix->nrows; ++i)
1862 row = MATRIX_ROW (w->current_matrix, i);
1863 if (!row->enabled_p)
1864 return NULL;
1865 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1866 break;
1869 *vpos = i;
1870 *hpos = 0;
1872 /* Give up if Y is not in the window. */
1873 if (i == w->current_matrix->nrows)
1874 return NULL;
1876 /* Get the glyph area containing X. */
1877 if (w->pseudo_window_p)
1879 *area = TEXT_AREA;
1880 x0 = 0;
1882 else
1884 if (x < window_box_left_offset (w, TEXT_AREA))
1886 *area = LEFT_MARGIN_AREA;
1887 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1889 else if (x < window_box_right_offset (w, TEXT_AREA))
1891 *area = TEXT_AREA;
1892 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1894 else
1896 *area = RIGHT_MARGIN_AREA;
1897 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1901 /* Find glyph containing X. */
1902 glyph = row->glyphs[*area];
1903 end = glyph + row->used[*area];
1904 x -= x0;
1905 while (glyph < end && x >= glyph->pixel_width)
1907 x -= glyph->pixel_width;
1908 ++glyph;
1911 if (glyph == end)
1912 return NULL;
1914 if (dx)
1916 *dx = x;
1917 *dy = y - (row->y + row->ascent - glyph->ascent);
1920 *hpos = glyph - row->glyphs[*area];
1921 return glyph;
1924 /* Convert frame-relative x/y to coordinates relative to window W.
1925 Takes pseudo-windows into account. */
1927 static void
1928 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1930 if (w->pseudo_window_p)
1932 /* A pseudo-window is always full-width, and starts at the
1933 left edge of the frame, plus a frame border. */
1934 struct frame *f = XFRAME (w->frame);
1935 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1936 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1938 else
1940 *x -= WINDOW_LEFT_EDGE_X (w);
1941 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1945 #ifdef HAVE_WINDOW_SYSTEM
1947 /* EXPORT:
1948 Return in RECTS[] at most N clipping rectangles for glyph string S.
1949 Return the number of stored rectangles. */
1952 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1954 XRectangle r;
1956 if (n <= 0)
1957 return 0;
1959 if (s->row->full_width_p)
1961 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1962 r.x = WINDOW_LEFT_EDGE_X (s->w);
1963 r.width = WINDOW_TOTAL_WIDTH (s->w);
1965 /* Unless displaying a mode or menu bar line, which are always
1966 fully visible, clip to the visible part of the row. */
1967 if (s->w->pseudo_window_p)
1968 r.height = s->row->visible_height;
1969 else
1970 r.height = s->height;
1972 else
1974 /* This is a text line that may be partially visible. */
1975 r.x = window_box_left (s->w, s->area);
1976 r.width = window_box_width (s->w, s->area);
1977 r.height = s->row->visible_height;
1980 if (s->clip_head)
1981 if (r.x < s->clip_head->x)
1983 if (r.width >= s->clip_head->x - r.x)
1984 r.width -= s->clip_head->x - r.x;
1985 else
1986 r.width = 0;
1987 r.x = s->clip_head->x;
1989 if (s->clip_tail)
1990 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1992 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1993 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1994 else
1995 r.width = 0;
1998 /* If S draws overlapping rows, it's sufficient to use the top and
1999 bottom of the window for clipping because this glyph string
2000 intentionally draws over other lines. */
2001 if (s->for_overlaps)
2003 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2004 r.height = window_text_bottom_y (s->w) - r.y;
2006 /* Alas, the above simple strategy does not work for the
2007 environments with anti-aliased text: if the same text is
2008 drawn onto the same place multiple times, it gets thicker.
2009 If the overlap we are processing is for the erased cursor, we
2010 take the intersection with the rectangle of the cursor. */
2011 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2013 XRectangle rc, r_save = r;
2015 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2016 rc.y = s->w->phys_cursor.y;
2017 rc.width = s->w->phys_cursor_width;
2018 rc.height = s->w->phys_cursor_height;
2020 x_intersect_rectangles (&r_save, &rc, &r);
2023 else
2025 /* Don't use S->y for clipping because it doesn't take partially
2026 visible lines into account. For example, it can be negative for
2027 partially visible lines at the top of a window. */
2028 if (!s->row->full_width_p
2029 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2030 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2031 else
2032 r.y = max (0, s->row->y);
2035 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2037 /* If drawing the cursor, don't let glyph draw outside its
2038 advertised boundaries. Cleartype does this under some circumstances. */
2039 if (s->hl == DRAW_CURSOR)
2041 struct glyph *glyph = s->first_glyph;
2042 int height, max_y;
2044 if (s->x > r.x)
2046 r.width -= s->x - r.x;
2047 r.x = s->x;
2049 r.width = min (r.width, glyph->pixel_width);
2051 /* If r.y is below window bottom, ensure that we still see a cursor. */
2052 height = min (glyph->ascent + glyph->descent,
2053 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2054 max_y = window_text_bottom_y (s->w) - height;
2055 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2056 if (s->ybase - glyph->ascent > max_y)
2058 r.y = max_y;
2059 r.height = height;
2061 else
2063 /* Don't draw cursor glyph taller than our actual glyph. */
2064 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2065 if (height < r.height)
2067 max_y = r.y + r.height;
2068 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2069 r.height = min (max_y - r.y, height);
2074 if (s->row->clip)
2076 XRectangle r_save = r;
2078 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2079 r.width = 0;
2082 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2083 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2085 #ifdef CONVERT_FROM_XRECT
2086 CONVERT_FROM_XRECT (r, *rects);
2087 #else
2088 *rects = r;
2089 #endif
2090 return 1;
2092 else
2094 /* If we are processing overlapping and allowed to return
2095 multiple clipping rectangles, we exclude the row of the glyph
2096 string from the clipping rectangle. This is to avoid drawing
2097 the same text on the environment with anti-aliasing. */
2098 #ifdef CONVERT_FROM_XRECT
2099 XRectangle rs[2];
2100 #else
2101 XRectangle *rs = rects;
2102 #endif
2103 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2105 if (s->for_overlaps & OVERLAPS_PRED)
2107 rs[i] = r;
2108 if (r.y + r.height > row_y)
2110 if (r.y < row_y)
2111 rs[i].height = row_y - r.y;
2112 else
2113 rs[i].height = 0;
2115 i++;
2117 if (s->for_overlaps & OVERLAPS_SUCC)
2119 rs[i] = r;
2120 if (r.y < row_y + s->row->visible_height)
2122 if (r.y + r.height > row_y + s->row->visible_height)
2124 rs[i].y = row_y + s->row->visible_height;
2125 rs[i].height = r.y + r.height - rs[i].y;
2127 else
2128 rs[i].height = 0;
2130 i++;
2133 n = i;
2134 #ifdef CONVERT_FROM_XRECT
2135 for (i = 0; i < n; i++)
2136 CONVERT_FROM_XRECT (rs[i], rects[i]);
2137 #endif
2138 return n;
2142 /* EXPORT:
2143 Return in *NR the clipping rectangle for glyph string S. */
2145 void
2146 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2148 get_glyph_string_clip_rects (s, nr, 1);
2152 /* EXPORT:
2153 Return the position and height of the phys cursor in window W.
2154 Set w->phys_cursor_width to width of phys cursor.
2157 void
2158 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2159 struct glyph *glyph, int *xp, int *yp, int *heightp)
2161 struct frame *f = XFRAME (WINDOW_FRAME (w));
2162 int x, y, wd, h, h0, y0;
2164 /* Compute the width of the rectangle to draw. If on a stretch
2165 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2166 rectangle as wide as the glyph, but use a canonical character
2167 width instead. */
2168 wd = glyph->pixel_width - 1;
2169 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2170 wd++; /* Why? */
2171 #endif
2173 x = w->phys_cursor.x;
2174 if (x < 0)
2176 wd += x;
2177 x = 0;
2180 if (glyph->type == STRETCH_GLYPH
2181 && !x_stretch_cursor_p)
2182 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2183 w->phys_cursor_width = wd;
2185 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2187 /* If y is below window bottom, ensure that we still see a cursor. */
2188 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2190 h = max (h0, glyph->ascent + glyph->descent);
2191 h0 = min (h0, glyph->ascent + glyph->descent);
2193 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2194 if (y < y0)
2196 h = max (h - (y0 - y) + 1, h0);
2197 y = y0 - 1;
2199 else
2201 y0 = window_text_bottom_y (w) - h0;
2202 if (y > y0)
2204 h += y - y0;
2205 y = y0;
2209 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2210 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2211 *heightp = h;
2215 * Remember which glyph the mouse is over.
2218 void
2219 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2221 Lisp_Object window;
2222 struct window *w;
2223 struct glyph_row *r, *gr, *end_row;
2224 enum window_part part;
2225 enum glyph_row_area area;
2226 int x, y, width, height;
2228 /* Try to determine frame pixel position and size of the glyph under
2229 frame pixel coordinates X/Y on frame F. */
2231 if (!f->glyphs_initialized_p
2232 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2233 NILP (window)))
2235 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2236 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2237 goto virtual_glyph;
2240 w = XWINDOW (window);
2241 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2242 height = WINDOW_FRAME_LINE_HEIGHT (w);
2244 x = window_relative_x_coord (w, part, gx);
2245 y = gy - WINDOW_TOP_EDGE_Y (w);
2247 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2248 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2250 if (w->pseudo_window_p)
2252 area = TEXT_AREA;
2253 part = ON_MODE_LINE; /* Don't adjust margin. */
2254 goto text_glyph;
2257 switch (part)
2259 case ON_LEFT_MARGIN:
2260 area = LEFT_MARGIN_AREA;
2261 goto text_glyph;
2263 case ON_RIGHT_MARGIN:
2264 area = RIGHT_MARGIN_AREA;
2265 goto text_glyph;
2267 case ON_HEADER_LINE:
2268 case ON_MODE_LINE:
2269 gr = (part == ON_HEADER_LINE
2270 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2271 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2272 gy = gr->y;
2273 area = TEXT_AREA;
2274 goto text_glyph_row_found;
2276 case ON_TEXT:
2277 area = TEXT_AREA;
2279 text_glyph:
2280 gr = 0; gy = 0;
2281 for (; r <= end_row && r->enabled_p; ++r)
2282 if (r->y + r->height > y)
2284 gr = r; gy = r->y;
2285 break;
2288 text_glyph_row_found:
2289 if (gr && gy <= y)
2291 struct glyph *g = gr->glyphs[area];
2292 struct glyph *end = g + gr->used[area];
2294 height = gr->height;
2295 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2296 if (gx + g->pixel_width > x)
2297 break;
2299 if (g < end)
2301 if (g->type == IMAGE_GLYPH)
2303 /* Don't remember when mouse is over image, as
2304 image may have hot-spots. */
2305 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2306 return;
2308 width = g->pixel_width;
2310 else
2312 /* Use nominal char spacing at end of line. */
2313 x -= gx;
2314 gx += (x / width) * width;
2317 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2318 gx += window_box_left_offset (w, area);
2320 else
2322 /* Use nominal line height at end of window. */
2323 gx = (x / width) * width;
2324 y -= gy;
2325 gy += (y / height) * height;
2327 break;
2329 case ON_LEFT_FRINGE:
2330 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2331 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2332 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2333 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2334 goto row_glyph;
2336 case ON_RIGHT_FRINGE:
2337 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2338 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2339 : window_box_right_offset (w, TEXT_AREA));
2340 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2341 goto row_glyph;
2343 case ON_SCROLL_BAR:
2344 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2346 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2347 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2348 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2349 : 0)));
2350 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2352 row_glyph:
2353 gr = 0, gy = 0;
2354 for (; r <= end_row && r->enabled_p; ++r)
2355 if (r->y + r->height > y)
2357 gr = r; gy = r->y;
2358 break;
2361 if (gr && gy <= y)
2362 height = gr->height;
2363 else
2365 /* Use nominal line height at end of window. */
2366 y -= gy;
2367 gy += (y / height) * height;
2369 break;
2371 default:
2373 virtual_glyph:
2374 /* If there is no glyph under the mouse, then we divide the screen
2375 into a grid of the smallest glyph in the frame, and use that
2376 as our "glyph". */
2378 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2379 round down even for negative values. */
2380 if (gx < 0)
2381 gx -= width - 1;
2382 if (gy < 0)
2383 gy -= height - 1;
2385 gx = (gx / width) * width;
2386 gy = (gy / height) * height;
2388 goto store_rect;
2391 gx += WINDOW_LEFT_EDGE_X (w);
2392 gy += WINDOW_TOP_EDGE_Y (w);
2394 store_rect:
2395 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2397 /* Visible feedback for debugging. */
2398 #if 0
2399 #if HAVE_X_WINDOWS
2400 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2401 f->output_data.x->normal_gc,
2402 gx, gy, width, height);
2403 #endif
2404 #endif
2408 #endif /* HAVE_WINDOW_SYSTEM */
2411 /***********************************************************************
2412 Lisp form evaluation
2413 ***********************************************************************/
2415 /* Error handler for safe_eval and safe_call. */
2417 static Lisp_Object
2418 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2420 add_to_log ("Error during redisplay: %S signaled %S",
2421 Flist (nargs, args), arg);
2422 return Qnil;
2425 /* Call function FUNC with the rest of NARGS - 1 arguments
2426 following. Return the result, or nil if something went
2427 wrong. Prevent redisplay during the evaluation. */
2429 Lisp_Object
2430 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2432 Lisp_Object val;
2434 if (inhibit_eval_during_redisplay)
2435 val = Qnil;
2436 else
2438 va_list ap;
2439 ptrdiff_t i;
2440 ptrdiff_t count = SPECPDL_INDEX ();
2441 struct gcpro gcpro1;
2442 Lisp_Object *args = alloca (nargs * word_size);
2444 args[0] = func;
2445 va_start (ap, func);
2446 for (i = 1; i < nargs; i++)
2447 args[i] = va_arg (ap, Lisp_Object);
2448 va_end (ap);
2450 GCPRO1 (args[0]);
2451 gcpro1.nvars = nargs;
2452 specbind (Qinhibit_redisplay, Qt);
2453 /* Use Qt to ensure debugger does not run,
2454 so there is no possibility of wanting to redisplay. */
2455 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2456 safe_eval_handler);
2457 UNGCPRO;
2458 val = unbind_to (count, val);
2461 return val;
2465 /* Call function FN with one argument ARG.
2466 Return the result, or nil if something went wrong. */
2468 Lisp_Object
2469 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2471 return safe_call (2, fn, arg);
2474 static Lisp_Object Qeval;
2476 Lisp_Object
2477 safe_eval (Lisp_Object sexpr)
2479 return safe_call1 (Qeval, sexpr);
2482 /* Call function FN with two arguments ARG1 and ARG2.
2483 Return the result, or nil if something went wrong. */
2485 Lisp_Object
2486 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2488 return safe_call (3, fn, arg1, arg2);
2493 /***********************************************************************
2494 Debugging
2495 ***********************************************************************/
2497 #if 0
2499 /* Define CHECK_IT to perform sanity checks on iterators.
2500 This is for debugging. It is too slow to do unconditionally. */
2502 static void
2503 check_it (struct it *it)
2505 if (it->method == GET_FROM_STRING)
2507 eassert (STRINGP (it->string));
2508 eassert (IT_STRING_CHARPOS (*it) >= 0);
2510 else
2512 eassert (IT_STRING_CHARPOS (*it) < 0);
2513 if (it->method == GET_FROM_BUFFER)
2515 /* Check that character and byte positions agree. */
2516 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2520 if (it->dpvec)
2521 eassert (it->current.dpvec_index >= 0);
2522 else
2523 eassert (it->current.dpvec_index < 0);
2526 #define CHECK_IT(IT) check_it ((IT))
2528 #else /* not 0 */
2530 #define CHECK_IT(IT) (void) 0
2532 #endif /* not 0 */
2535 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2537 /* Check that the window end of window W is what we expect it
2538 to be---the last row in the current matrix displaying text. */
2540 static void
2541 check_window_end (struct window *w)
2543 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2545 struct glyph_row *row;
2546 eassert ((row = MATRIX_ROW (w->current_matrix,
2547 XFASTINT (w->window_end_vpos)),
2548 !row->enabled_p
2549 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2550 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2554 #define CHECK_WINDOW_END(W) check_window_end ((W))
2556 #else
2558 #define CHECK_WINDOW_END(W) (void) 0
2560 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2562 /* Return mark position if current buffer has the region of non-zero length,
2563 or -1 otherwise. */
2565 static ptrdiff_t
2566 markpos_of_region (void)
2568 if (!NILP (Vtransient_mark_mode)
2569 && !NILP (BVAR (current_buffer, mark_active))
2570 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2572 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2574 if (markpos != PT)
2575 return markpos;
2577 return -1;
2580 /***********************************************************************
2581 Iterator initialization
2582 ***********************************************************************/
2584 /* Initialize IT for displaying current_buffer in window W, starting
2585 at character position CHARPOS. CHARPOS < 0 means that no buffer
2586 position is specified which is useful when the iterator is assigned
2587 a position later. BYTEPOS is the byte position corresponding to
2588 CHARPOS.
2590 If ROW is not null, calls to produce_glyphs with IT as parameter
2591 will produce glyphs in that row.
2593 BASE_FACE_ID is the id of a base face to use. It must be one of
2594 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2595 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2596 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2598 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2599 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2600 will be initialized to use the corresponding mode line glyph row of
2601 the desired matrix of W. */
2603 void
2604 init_iterator (struct it *it, struct window *w,
2605 ptrdiff_t charpos, ptrdiff_t bytepos,
2606 struct glyph_row *row, enum face_id base_face_id)
2608 ptrdiff_t markpos;
2609 enum face_id remapped_base_face_id = base_face_id;
2611 /* Some precondition checks. */
2612 eassert (w != NULL && it != NULL);
2613 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2614 && charpos <= ZV));
2616 /* If face attributes have been changed since the last redisplay,
2617 free realized faces now because they depend on face definitions
2618 that might have changed. Don't free faces while there might be
2619 desired matrices pending which reference these faces. */
2620 if (face_change_count && !inhibit_free_realized_faces)
2622 face_change_count = 0;
2623 free_all_realized_faces (Qnil);
2626 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2627 if (! NILP (Vface_remapping_alist))
2628 remapped_base_face_id
2629 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2631 /* Use one of the mode line rows of W's desired matrix if
2632 appropriate. */
2633 if (row == NULL)
2635 if (base_face_id == MODE_LINE_FACE_ID
2636 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2637 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2638 else if (base_face_id == HEADER_LINE_FACE_ID)
2639 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2642 /* Clear IT. */
2643 memset (it, 0, sizeof *it);
2644 it->current.overlay_string_index = -1;
2645 it->current.dpvec_index = -1;
2646 it->base_face_id = remapped_base_face_id;
2647 it->string = Qnil;
2648 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2649 it->paragraph_embedding = L2R;
2650 it->bidi_it.string.lstring = Qnil;
2651 it->bidi_it.string.s = NULL;
2652 it->bidi_it.string.bufpos = 0;
2654 /* The window in which we iterate over current_buffer: */
2655 XSETWINDOW (it->window, w);
2656 it->w = w;
2657 it->f = XFRAME (w->frame);
2659 it->cmp_it.id = -1;
2661 /* Extra space between lines (on window systems only). */
2662 if (base_face_id == DEFAULT_FACE_ID
2663 && FRAME_WINDOW_P (it->f))
2665 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2666 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2667 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2668 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2669 * FRAME_LINE_HEIGHT (it->f));
2670 else if (it->f->extra_line_spacing > 0)
2671 it->extra_line_spacing = it->f->extra_line_spacing;
2672 it->max_extra_line_spacing = 0;
2675 /* If realized faces have been removed, e.g. because of face
2676 attribute changes of named faces, recompute them. When running
2677 in batch mode, the face cache of the initial frame is null. If
2678 we happen to get called, make a dummy face cache. */
2679 if (FRAME_FACE_CACHE (it->f) == NULL)
2680 init_frame_faces (it->f);
2681 if (FRAME_FACE_CACHE (it->f)->used == 0)
2682 recompute_basic_faces (it->f);
2684 /* Current value of the `slice', `space-width', and 'height' properties. */
2685 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2686 it->space_width = Qnil;
2687 it->font_height = Qnil;
2688 it->override_ascent = -1;
2690 /* Are control characters displayed as `^C'? */
2691 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2693 /* -1 means everything between a CR and the following line end
2694 is invisible. >0 means lines indented more than this value are
2695 invisible. */
2696 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2697 ? (clip_to_bounds
2698 (-1, XINT (BVAR (current_buffer, selective_display)),
2699 PTRDIFF_MAX))
2700 : (!NILP (BVAR (current_buffer, selective_display))
2701 ? -1 : 0));
2702 it->selective_display_ellipsis_p
2703 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2705 /* Display table to use. */
2706 it->dp = window_display_table (w);
2708 /* Are multibyte characters enabled in current_buffer? */
2709 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2711 /* If visible region is of non-zero length, set IT->region_beg_charpos
2712 and IT->region_end_charpos to the start and end of a visible region
2713 in window IT->w. Set both to -1 to indicate no region. */
2714 markpos = markpos_of_region ();
2715 if (markpos >= 0
2716 /* Maybe highlight only in selected window. */
2717 && (/* Either show region everywhere. */
2718 highlight_nonselected_windows
2719 /* Or show region in the selected window. */
2720 || w == XWINDOW (selected_window)
2721 /* Or show the region if we are in the mini-buffer and W is
2722 the window the mini-buffer refers to. */
2723 || (MINI_WINDOW_P (XWINDOW (selected_window))
2724 && WINDOWP (minibuf_selected_window)
2725 && w == XWINDOW (minibuf_selected_window))))
2727 it->region_beg_charpos = min (PT, markpos);
2728 it->region_end_charpos = max (PT, markpos);
2730 else
2731 it->region_beg_charpos = it->region_end_charpos = -1;
2733 /* Get the position at which the redisplay_end_trigger hook should
2734 be run, if it is to be run at all. */
2735 if (MARKERP (w->redisplay_end_trigger)
2736 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2737 it->redisplay_end_trigger_charpos
2738 = marker_position (w->redisplay_end_trigger);
2739 else if (INTEGERP (w->redisplay_end_trigger))
2740 it->redisplay_end_trigger_charpos =
2741 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2743 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2745 /* Are lines in the display truncated? */
2746 if (base_face_id != DEFAULT_FACE_ID
2747 || it->w->hscroll
2748 || (! WINDOW_FULL_WIDTH_P (it->w)
2749 && ((!NILP (Vtruncate_partial_width_windows)
2750 && !INTEGERP (Vtruncate_partial_width_windows))
2751 || (INTEGERP (Vtruncate_partial_width_windows)
2752 && (WINDOW_TOTAL_COLS (it->w)
2753 < XINT (Vtruncate_partial_width_windows))))))
2754 it->line_wrap = TRUNCATE;
2755 else if (NILP (BVAR (current_buffer, truncate_lines)))
2756 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2757 ? WINDOW_WRAP : WORD_WRAP;
2758 else
2759 it->line_wrap = TRUNCATE;
2761 /* Get dimensions of truncation and continuation glyphs. These are
2762 displayed as fringe bitmaps under X, but we need them for such
2763 frames when the fringes are turned off. But leave the dimensions
2764 zero for tooltip frames, as these glyphs look ugly there and also
2765 sabotage calculations of tooltip dimensions in x-show-tip. */
2766 #ifdef HAVE_WINDOW_SYSTEM
2767 if (!(FRAME_WINDOW_P (it->f)
2768 && FRAMEP (tip_frame)
2769 && it->f == XFRAME (tip_frame)))
2770 #endif
2772 if (it->line_wrap == TRUNCATE)
2774 /* We will need the truncation glyph. */
2775 eassert (it->glyph_row == NULL);
2776 produce_special_glyphs (it, IT_TRUNCATION);
2777 it->truncation_pixel_width = it->pixel_width;
2779 else
2781 /* We will need the continuation glyph. */
2782 eassert (it->glyph_row == NULL);
2783 produce_special_glyphs (it, IT_CONTINUATION);
2784 it->continuation_pixel_width = it->pixel_width;
2788 /* Reset these values to zero because the produce_special_glyphs
2789 above has changed them. */
2790 it->pixel_width = it->ascent = it->descent = 0;
2791 it->phys_ascent = it->phys_descent = 0;
2793 /* Set this after getting the dimensions of truncation and
2794 continuation glyphs, so that we don't produce glyphs when calling
2795 produce_special_glyphs, above. */
2796 it->glyph_row = row;
2797 it->area = TEXT_AREA;
2799 /* Forget any previous info about this row being reversed. */
2800 if (it->glyph_row)
2801 it->glyph_row->reversed_p = 0;
2803 /* Get the dimensions of the display area. The display area
2804 consists of the visible window area plus a horizontally scrolled
2805 part to the left of the window. All x-values are relative to the
2806 start of this total display area. */
2807 if (base_face_id != DEFAULT_FACE_ID)
2809 /* Mode lines, menu bar in terminal frames. */
2810 it->first_visible_x = 0;
2811 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2813 else
2815 it->first_visible_x =
2816 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2817 it->last_visible_x = (it->first_visible_x
2818 + window_box_width (w, TEXT_AREA));
2820 /* If we truncate lines, leave room for the truncation glyph(s) at
2821 the right margin. Otherwise, leave room for the continuation
2822 glyph(s). Done only if the window has no fringes. Since we
2823 don't know at this point whether there will be any R2L lines in
2824 the window, we reserve space for truncation/continuation glyphs
2825 even if only one of the fringes is absent. */
2826 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2827 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2829 if (it->line_wrap == TRUNCATE)
2830 it->last_visible_x -= it->truncation_pixel_width;
2831 else
2832 it->last_visible_x -= it->continuation_pixel_width;
2835 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2836 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2839 /* Leave room for a border glyph. */
2840 if (!FRAME_WINDOW_P (it->f)
2841 && !WINDOW_RIGHTMOST_P (it->w))
2842 it->last_visible_x -= 1;
2844 it->last_visible_y = window_text_bottom_y (w);
2846 /* For mode lines and alike, arrange for the first glyph having a
2847 left box line if the face specifies a box. */
2848 if (base_face_id != DEFAULT_FACE_ID)
2850 struct face *face;
2852 it->face_id = remapped_base_face_id;
2854 /* If we have a boxed mode line, make the first character appear
2855 with a left box line. */
2856 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2857 if (face->box != FACE_NO_BOX)
2858 it->start_of_box_run_p = 1;
2861 /* If a buffer position was specified, set the iterator there,
2862 getting overlays and face properties from that position. */
2863 if (charpos >= BUF_BEG (current_buffer))
2865 it->end_charpos = ZV;
2866 eassert (charpos == BYTE_TO_CHAR (bytepos));
2867 IT_CHARPOS (*it) = charpos;
2868 IT_BYTEPOS (*it) = bytepos;
2870 /* We will rely on `reseat' to set this up properly, via
2871 handle_face_prop. */
2872 it->face_id = it->base_face_id;
2874 it->start = it->current;
2875 /* Do we need to reorder bidirectional text? Not if this is a
2876 unibyte buffer: by definition, none of the single-byte
2877 characters are strong R2L, so no reordering is needed. And
2878 bidi.c doesn't support unibyte buffers anyway. Also, don't
2879 reorder while we are loading loadup.el, since the tables of
2880 character properties needed for reordering are not yet
2881 available. */
2882 it->bidi_p =
2883 NILP (Vpurify_flag)
2884 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2885 && it->multibyte_p;
2887 /* If we are to reorder bidirectional text, init the bidi
2888 iterator. */
2889 if (it->bidi_p)
2891 /* Note the paragraph direction that this buffer wants to
2892 use. */
2893 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2894 Qleft_to_right))
2895 it->paragraph_embedding = L2R;
2896 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2897 Qright_to_left))
2898 it->paragraph_embedding = R2L;
2899 else
2900 it->paragraph_embedding = NEUTRAL_DIR;
2901 bidi_unshelve_cache (NULL, 0);
2902 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2903 &it->bidi_it);
2906 /* Compute faces etc. */
2907 reseat (it, it->current.pos, 1);
2910 CHECK_IT (it);
2914 /* Initialize IT for the display of window W with window start POS. */
2916 void
2917 start_display (struct it *it, struct window *w, struct text_pos pos)
2919 struct glyph_row *row;
2920 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2922 row = w->desired_matrix->rows + first_vpos;
2923 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2924 it->first_vpos = first_vpos;
2926 /* Don't reseat to previous visible line start if current start
2927 position is in a string or image. */
2928 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2930 int start_at_line_beg_p;
2931 int first_y = it->current_y;
2933 /* If window start is not at a line start, skip forward to POS to
2934 get the correct continuation lines width. */
2935 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2936 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2937 if (!start_at_line_beg_p)
2939 int new_x;
2941 reseat_at_previous_visible_line_start (it);
2942 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2944 new_x = it->current_x + it->pixel_width;
2946 /* If lines are continued, this line may end in the middle
2947 of a multi-glyph character (e.g. a control character
2948 displayed as \003, or in the middle of an overlay
2949 string). In this case move_it_to above will not have
2950 taken us to the start of the continuation line but to the
2951 end of the continued line. */
2952 if (it->current_x > 0
2953 && it->line_wrap != TRUNCATE /* Lines are continued. */
2954 && (/* And glyph doesn't fit on the line. */
2955 new_x > it->last_visible_x
2956 /* Or it fits exactly and we're on a window
2957 system frame. */
2958 || (new_x == it->last_visible_x
2959 && FRAME_WINDOW_P (it->f)
2960 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2961 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2962 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2964 if ((it->current.dpvec_index >= 0
2965 || it->current.overlay_string_index >= 0)
2966 /* If we are on a newline from a display vector or
2967 overlay string, then we are already at the end of
2968 a screen line; no need to go to the next line in
2969 that case, as this line is not really continued.
2970 (If we do go to the next line, C-e will not DTRT.) */
2971 && it->c != '\n')
2973 set_iterator_to_next (it, 1);
2974 move_it_in_display_line_to (it, -1, -1, 0);
2977 it->continuation_lines_width += it->current_x;
2979 /* If the character at POS is displayed via a display
2980 vector, move_it_to above stops at the final glyph of
2981 IT->dpvec. To make the caller redisplay that character
2982 again (a.k.a. start at POS), we need to reset the
2983 dpvec_index to the beginning of IT->dpvec. */
2984 else if (it->current.dpvec_index >= 0)
2985 it->current.dpvec_index = 0;
2987 /* We're starting a new display line, not affected by the
2988 height of the continued line, so clear the appropriate
2989 fields in the iterator structure. */
2990 it->max_ascent = it->max_descent = 0;
2991 it->max_phys_ascent = it->max_phys_descent = 0;
2993 it->current_y = first_y;
2994 it->vpos = 0;
2995 it->current_x = it->hpos = 0;
3001 /* Return 1 if POS is a position in ellipses displayed for invisible
3002 text. W is the window we display, for text property lookup. */
3004 static int
3005 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3007 Lisp_Object prop, window;
3008 int ellipses_p = 0;
3009 ptrdiff_t charpos = CHARPOS (pos->pos);
3011 /* If POS specifies a position in a display vector, this might
3012 be for an ellipsis displayed for invisible text. We won't
3013 get the iterator set up for delivering that ellipsis unless
3014 we make sure that it gets aware of the invisible text. */
3015 if (pos->dpvec_index >= 0
3016 && pos->overlay_string_index < 0
3017 && CHARPOS (pos->string_pos) < 0
3018 && charpos > BEGV
3019 && (XSETWINDOW (window, w),
3020 prop = Fget_char_property (make_number (charpos),
3021 Qinvisible, window),
3022 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3024 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3025 window);
3026 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3029 return ellipses_p;
3033 /* Initialize IT for stepping through current_buffer in window W,
3034 starting at position POS that includes overlay string and display
3035 vector/ control character translation position information. Value
3036 is zero if there are overlay strings with newlines at POS. */
3038 static int
3039 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3041 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3042 int i, overlay_strings_with_newlines = 0;
3044 /* If POS specifies a position in a display vector, this might
3045 be for an ellipsis displayed for invisible text. We won't
3046 get the iterator set up for delivering that ellipsis unless
3047 we make sure that it gets aware of the invisible text. */
3048 if (in_ellipses_for_invisible_text_p (pos, w))
3050 --charpos;
3051 bytepos = 0;
3054 /* Keep in mind: the call to reseat in init_iterator skips invisible
3055 text, so we might end up at a position different from POS. This
3056 is only a problem when POS is a row start after a newline and an
3057 overlay starts there with an after-string, and the overlay has an
3058 invisible property. Since we don't skip invisible text in
3059 display_line and elsewhere immediately after consuming the
3060 newline before the row start, such a POS will not be in a string,
3061 but the call to init_iterator below will move us to the
3062 after-string. */
3063 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3065 /* This only scans the current chunk -- it should scan all chunks.
3066 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3067 to 16 in 22.1 to make this a lesser problem. */
3068 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3070 const char *s = SSDATA (it->overlay_strings[i]);
3071 const char *e = s + SBYTES (it->overlay_strings[i]);
3073 while (s < e && *s != '\n')
3074 ++s;
3076 if (s < e)
3078 overlay_strings_with_newlines = 1;
3079 break;
3083 /* If position is within an overlay string, set up IT to the right
3084 overlay string. */
3085 if (pos->overlay_string_index >= 0)
3087 int relative_index;
3089 /* If the first overlay string happens to have a `display'
3090 property for an image, the iterator will be set up for that
3091 image, and we have to undo that setup first before we can
3092 correct the overlay string index. */
3093 if (it->method == GET_FROM_IMAGE)
3094 pop_it (it);
3096 /* We already have the first chunk of overlay strings in
3097 IT->overlay_strings. Load more until the one for
3098 pos->overlay_string_index is in IT->overlay_strings. */
3099 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3101 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3102 it->current.overlay_string_index = 0;
3103 while (n--)
3105 load_overlay_strings (it, 0);
3106 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3110 it->current.overlay_string_index = pos->overlay_string_index;
3111 relative_index = (it->current.overlay_string_index
3112 % OVERLAY_STRING_CHUNK_SIZE);
3113 it->string = it->overlay_strings[relative_index];
3114 eassert (STRINGP (it->string));
3115 it->current.string_pos = pos->string_pos;
3116 it->method = GET_FROM_STRING;
3117 it->end_charpos = SCHARS (it->string);
3118 /* Set up the bidi iterator for this overlay string. */
3119 if (it->bidi_p)
3121 it->bidi_it.string.lstring = it->string;
3122 it->bidi_it.string.s = NULL;
3123 it->bidi_it.string.schars = SCHARS (it->string);
3124 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3125 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3126 it->bidi_it.string.unibyte = !it->multibyte_p;
3127 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3128 FRAME_WINDOW_P (it->f), &it->bidi_it);
3130 /* Synchronize the state of the bidi iterator with
3131 pos->string_pos. For any string position other than
3132 zero, this will be done automagically when we resume
3133 iteration over the string and get_visually_first_element
3134 is called. But if string_pos is zero, and the string is
3135 to be reordered for display, we need to resync manually,
3136 since it could be that the iteration state recorded in
3137 pos ended at string_pos of 0 moving backwards in string. */
3138 if (CHARPOS (pos->string_pos) == 0)
3140 get_visually_first_element (it);
3141 if (IT_STRING_CHARPOS (*it) != 0)
3142 do {
3143 /* Paranoia. */
3144 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3145 bidi_move_to_visually_next (&it->bidi_it);
3146 } while (it->bidi_it.charpos != 0);
3148 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3149 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3153 if (CHARPOS (pos->string_pos) >= 0)
3155 /* Recorded position is not in an overlay string, but in another
3156 string. This can only be a string from a `display' property.
3157 IT should already be filled with that string. */
3158 it->current.string_pos = pos->string_pos;
3159 eassert (STRINGP (it->string));
3160 if (it->bidi_p)
3161 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3162 FRAME_WINDOW_P (it->f), &it->bidi_it);
3165 /* Restore position in display vector translations, control
3166 character translations or ellipses. */
3167 if (pos->dpvec_index >= 0)
3169 if (it->dpvec == NULL)
3170 get_next_display_element (it);
3171 eassert (it->dpvec && it->current.dpvec_index == 0);
3172 it->current.dpvec_index = pos->dpvec_index;
3175 CHECK_IT (it);
3176 return !overlay_strings_with_newlines;
3180 /* Initialize IT for stepping through current_buffer in window W
3181 starting at ROW->start. */
3183 static void
3184 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3186 init_from_display_pos (it, w, &row->start);
3187 it->start = row->start;
3188 it->continuation_lines_width = row->continuation_lines_width;
3189 CHECK_IT (it);
3193 /* Initialize IT for stepping through current_buffer in window W
3194 starting in the line following ROW, i.e. starting at ROW->end.
3195 Value is zero if there are overlay strings with newlines at ROW's
3196 end position. */
3198 static int
3199 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3201 int success = 0;
3203 if (init_from_display_pos (it, w, &row->end))
3205 if (row->continued_p)
3206 it->continuation_lines_width
3207 = row->continuation_lines_width + row->pixel_width;
3208 CHECK_IT (it);
3209 success = 1;
3212 return success;
3218 /***********************************************************************
3219 Text properties
3220 ***********************************************************************/
3222 /* Called when IT reaches IT->stop_charpos. Handle text property and
3223 overlay changes. Set IT->stop_charpos to the next position where
3224 to stop. */
3226 static void
3227 handle_stop (struct it *it)
3229 enum prop_handled handled;
3230 int handle_overlay_change_p;
3231 struct props *p;
3233 it->dpvec = NULL;
3234 it->current.dpvec_index = -1;
3235 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3236 it->ignore_overlay_strings_at_pos_p = 0;
3237 it->ellipsis_p = 0;
3239 /* Use face of preceding text for ellipsis (if invisible) */
3240 if (it->selective_display_ellipsis_p)
3241 it->saved_face_id = it->face_id;
3245 handled = HANDLED_NORMALLY;
3247 /* Call text property handlers. */
3248 for (p = it_props; p->handler; ++p)
3250 handled = p->handler (it);
3252 if (handled == HANDLED_RECOMPUTE_PROPS)
3253 break;
3254 else if (handled == HANDLED_RETURN)
3256 /* We still want to show before and after strings from
3257 overlays even if the actual buffer text is replaced. */
3258 if (!handle_overlay_change_p
3259 || it->sp > 1
3260 /* Don't call get_overlay_strings_1 if we already
3261 have overlay strings loaded, because doing so
3262 will load them again and push the iterator state
3263 onto the stack one more time, which is not
3264 expected by the rest of the code that processes
3265 overlay strings. */
3266 || (it->current.overlay_string_index < 0
3267 ? !get_overlay_strings_1 (it, 0, 0)
3268 : 0))
3270 if (it->ellipsis_p)
3271 setup_for_ellipsis (it, 0);
3272 /* When handling a display spec, we might load an
3273 empty string. In that case, discard it here. We
3274 used to discard it in handle_single_display_spec,
3275 but that causes get_overlay_strings_1, above, to
3276 ignore overlay strings that we must check. */
3277 if (STRINGP (it->string) && !SCHARS (it->string))
3278 pop_it (it);
3279 return;
3281 else if (STRINGP (it->string) && !SCHARS (it->string))
3282 pop_it (it);
3283 else
3285 it->ignore_overlay_strings_at_pos_p = 1;
3286 it->string_from_display_prop_p = 0;
3287 it->from_disp_prop_p = 0;
3288 handle_overlay_change_p = 0;
3290 handled = HANDLED_RECOMPUTE_PROPS;
3291 break;
3293 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3294 handle_overlay_change_p = 0;
3297 if (handled != HANDLED_RECOMPUTE_PROPS)
3299 /* Don't check for overlay strings below when set to deliver
3300 characters from a display vector. */
3301 if (it->method == GET_FROM_DISPLAY_VECTOR)
3302 handle_overlay_change_p = 0;
3304 /* Handle overlay changes.
3305 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3306 if it finds overlays. */
3307 if (handle_overlay_change_p)
3308 handled = handle_overlay_change (it);
3311 if (it->ellipsis_p)
3313 setup_for_ellipsis (it, 0);
3314 break;
3317 while (handled == HANDLED_RECOMPUTE_PROPS);
3319 /* Determine where to stop next. */
3320 if (handled == HANDLED_NORMALLY)
3321 compute_stop_pos (it);
3325 /* Compute IT->stop_charpos from text property and overlay change
3326 information for IT's current position. */
3328 static void
3329 compute_stop_pos (struct it *it)
3331 register INTERVAL iv, next_iv;
3332 Lisp_Object object, limit, position;
3333 ptrdiff_t charpos, bytepos;
3335 if (STRINGP (it->string))
3337 /* Strings are usually short, so don't limit the search for
3338 properties. */
3339 it->stop_charpos = it->end_charpos;
3340 object = it->string;
3341 limit = Qnil;
3342 charpos = IT_STRING_CHARPOS (*it);
3343 bytepos = IT_STRING_BYTEPOS (*it);
3345 else
3347 ptrdiff_t pos;
3349 /* If end_charpos is out of range for some reason, such as a
3350 misbehaving display function, rationalize it (Bug#5984). */
3351 if (it->end_charpos > ZV)
3352 it->end_charpos = ZV;
3353 it->stop_charpos = it->end_charpos;
3355 /* If next overlay change is in front of the current stop pos
3356 (which is IT->end_charpos), stop there. Note: value of
3357 next_overlay_change is point-max if no overlay change
3358 follows. */
3359 charpos = IT_CHARPOS (*it);
3360 bytepos = IT_BYTEPOS (*it);
3361 pos = next_overlay_change (charpos);
3362 if (pos < it->stop_charpos)
3363 it->stop_charpos = pos;
3365 /* If showing the region, we have to stop at the region
3366 start or end because the face might change there. */
3367 if (it->region_beg_charpos > 0)
3369 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3370 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3371 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3372 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3375 /* Set up variables for computing the stop position from text
3376 property changes. */
3377 XSETBUFFER (object, current_buffer);
3378 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3381 /* Get the interval containing IT's position. Value is a null
3382 interval if there isn't such an interval. */
3383 position = make_number (charpos);
3384 iv = validate_interval_range (object, &position, &position, 0);
3385 if (iv)
3387 Lisp_Object values_here[LAST_PROP_IDX];
3388 struct props *p;
3390 /* Get properties here. */
3391 for (p = it_props; p->handler; ++p)
3392 values_here[p->idx] = textget (iv->plist, *p->name);
3394 /* Look for an interval following iv that has different
3395 properties. */
3396 for (next_iv = next_interval (iv);
3397 (next_iv
3398 && (NILP (limit)
3399 || XFASTINT (limit) > next_iv->position));
3400 next_iv = next_interval (next_iv))
3402 for (p = it_props; p->handler; ++p)
3404 Lisp_Object new_value;
3406 new_value = textget (next_iv->plist, *p->name);
3407 if (!EQ (values_here[p->idx], new_value))
3408 break;
3411 if (p->handler)
3412 break;
3415 if (next_iv)
3417 if (INTEGERP (limit)
3418 && next_iv->position >= XFASTINT (limit))
3419 /* No text property change up to limit. */
3420 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3421 else
3422 /* Text properties change in next_iv. */
3423 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3427 if (it->cmp_it.id < 0)
3429 ptrdiff_t stoppos = it->end_charpos;
3431 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3432 stoppos = -1;
3433 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3434 stoppos, it->string);
3437 eassert (STRINGP (it->string)
3438 || (it->stop_charpos >= BEGV
3439 && it->stop_charpos >= IT_CHARPOS (*it)));
3443 /* Return the position of the next overlay change after POS in
3444 current_buffer. Value is point-max if no overlay change
3445 follows. This is like `next-overlay-change' but doesn't use
3446 xmalloc. */
3448 static ptrdiff_t
3449 next_overlay_change (ptrdiff_t pos)
3451 ptrdiff_t i, noverlays;
3452 ptrdiff_t endpos;
3453 Lisp_Object *overlays;
3455 /* Get all overlays at the given position. */
3456 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3458 /* If any of these overlays ends before endpos,
3459 use its ending point instead. */
3460 for (i = 0; i < noverlays; ++i)
3462 Lisp_Object oend;
3463 ptrdiff_t oendpos;
3465 oend = OVERLAY_END (overlays[i]);
3466 oendpos = OVERLAY_POSITION (oend);
3467 endpos = min (endpos, oendpos);
3470 return endpos;
3473 /* How many characters forward to search for a display property or
3474 display string. Searching too far forward makes the bidi display
3475 sluggish, especially in small windows. */
3476 #define MAX_DISP_SCAN 250
3478 /* Return the character position of a display string at or after
3479 position specified by POSITION. If no display string exists at or
3480 after POSITION, return ZV. A display string is either an overlay
3481 with `display' property whose value is a string, or a `display'
3482 text property whose value is a string. STRING is data about the
3483 string to iterate; if STRING->lstring is nil, we are iterating a
3484 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3485 on a GUI frame. DISP_PROP is set to zero if we searched
3486 MAX_DISP_SCAN characters forward without finding any display
3487 strings, non-zero otherwise. It is set to 2 if the display string
3488 uses any kind of `(space ...)' spec that will produce a stretch of
3489 white space in the text area. */
3490 ptrdiff_t
3491 compute_display_string_pos (struct text_pos *position,
3492 struct bidi_string_data *string,
3493 int frame_window_p, int *disp_prop)
3495 /* OBJECT = nil means current buffer. */
3496 Lisp_Object object =
3497 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3498 Lisp_Object pos, spec, limpos;
3499 int string_p = (string && (STRINGP (string->lstring) || string->s));
3500 ptrdiff_t eob = string_p ? string->schars : ZV;
3501 ptrdiff_t begb = string_p ? 0 : BEGV;
3502 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3503 ptrdiff_t lim =
3504 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3505 struct text_pos tpos;
3506 int rv = 0;
3508 *disp_prop = 1;
3510 if (charpos >= eob
3511 /* We don't support display properties whose values are strings
3512 that have display string properties. */
3513 || string->from_disp_str
3514 /* C strings cannot have display properties. */
3515 || (string->s && !STRINGP (object)))
3517 *disp_prop = 0;
3518 return eob;
3521 /* If the character at CHARPOS is where the display string begins,
3522 return CHARPOS. */
3523 pos = make_number (charpos);
3524 if (STRINGP (object))
3525 bufpos = string->bufpos;
3526 else
3527 bufpos = charpos;
3528 tpos = *position;
3529 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3530 && (charpos <= begb
3531 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3532 object),
3533 spec))
3534 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3535 frame_window_p)))
3537 if (rv == 2)
3538 *disp_prop = 2;
3539 return charpos;
3542 /* Look forward for the first character with a `display' property
3543 that will replace the underlying text when displayed. */
3544 limpos = make_number (lim);
3545 do {
3546 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3547 CHARPOS (tpos) = XFASTINT (pos);
3548 if (CHARPOS (tpos) >= lim)
3550 *disp_prop = 0;
3551 break;
3553 if (STRINGP (object))
3554 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3555 else
3556 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3557 spec = Fget_char_property (pos, Qdisplay, object);
3558 if (!STRINGP (object))
3559 bufpos = CHARPOS (tpos);
3560 } while (NILP (spec)
3561 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3562 bufpos, frame_window_p)));
3563 if (rv == 2)
3564 *disp_prop = 2;
3566 return CHARPOS (tpos);
3569 /* Return the character position of the end of the display string that
3570 started at CHARPOS. If there's no display string at CHARPOS,
3571 return -1. A display string is either an overlay with `display'
3572 property whose value is a string or a `display' text property whose
3573 value is a string. */
3574 ptrdiff_t
3575 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3577 /* OBJECT = nil means current buffer. */
3578 Lisp_Object object =
3579 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3580 Lisp_Object pos = make_number (charpos);
3581 ptrdiff_t eob =
3582 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3584 if (charpos >= eob || (string->s && !STRINGP (object)))
3585 return eob;
3587 /* It could happen that the display property or overlay was removed
3588 since we found it in compute_display_string_pos above. One way
3589 this can happen is if JIT font-lock was called (through
3590 handle_fontified_prop), and jit-lock-functions remove text
3591 properties or overlays from the portion of buffer that includes
3592 CHARPOS. Muse mode is known to do that, for example. In this
3593 case, we return -1 to the caller, to signal that no display
3594 string is actually present at CHARPOS. See bidi_fetch_char for
3595 how this is handled.
3597 An alternative would be to never look for display properties past
3598 it->stop_charpos. But neither compute_display_string_pos nor
3599 bidi_fetch_char that calls it know or care where the next
3600 stop_charpos is. */
3601 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3602 return -1;
3604 /* Look forward for the first character where the `display' property
3605 changes. */
3606 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3608 return XFASTINT (pos);
3613 /***********************************************************************
3614 Fontification
3615 ***********************************************************************/
3617 /* Handle changes in the `fontified' property of the current buffer by
3618 calling hook functions from Qfontification_functions to fontify
3619 regions of text. */
3621 static enum prop_handled
3622 handle_fontified_prop (struct it *it)
3624 Lisp_Object prop, pos;
3625 enum prop_handled handled = HANDLED_NORMALLY;
3627 if (!NILP (Vmemory_full))
3628 return handled;
3630 /* Get the value of the `fontified' property at IT's current buffer
3631 position. (The `fontified' property doesn't have a special
3632 meaning in strings.) If the value is nil, call functions from
3633 Qfontification_functions. */
3634 if (!STRINGP (it->string)
3635 && it->s == NULL
3636 && !NILP (Vfontification_functions)
3637 && !NILP (Vrun_hooks)
3638 && (pos = make_number (IT_CHARPOS (*it)),
3639 prop = Fget_char_property (pos, Qfontified, Qnil),
3640 /* Ignore the special cased nil value always present at EOB since
3641 no amount of fontifying will be able to change it. */
3642 NILP (prop) && IT_CHARPOS (*it) < Z))
3644 ptrdiff_t count = SPECPDL_INDEX ();
3645 Lisp_Object val;
3646 struct buffer *obuf = current_buffer;
3647 int begv = BEGV, zv = ZV;
3648 int old_clip_changed = current_buffer->clip_changed;
3650 val = Vfontification_functions;
3651 specbind (Qfontification_functions, Qnil);
3653 eassert (it->end_charpos == ZV);
3655 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3656 safe_call1 (val, pos);
3657 else
3659 Lisp_Object fns, fn;
3660 struct gcpro gcpro1, gcpro2;
3662 fns = Qnil;
3663 GCPRO2 (val, fns);
3665 for (; CONSP (val); val = XCDR (val))
3667 fn = XCAR (val);
3669 if (EQ (fn, Qt))
3671 /* A value of t indicates this hook has a local
3672 binding; it means to run the global binding too.
3673 In a global value, t should not occur. If it
3674 does, we must ignore it to avoid an endless
3675 loop. */
3676 for (fns = Fdefault_value (Qfontification_functions);
3677 CONSP (fns);
3678 fns = XCDR (fns))
3680 fn = XCAR (fns);
3681 if (!EQ (fn, Qt))
3682 safe_call1 (fn, pos);
3685 else
3686 safe_call1 (fn, pos);
3689 UNGCPRO;
3692 unbind_to (count, Qnil);
3694 /* Fontification functions routinely call `save-restriction'.
3695 Normally, this tags clip_changed, which can confuse redisplay
3696 (see discussion in Bug#6671). Since we don't perform any
3697 special handling of fontification changes in the case where
3698 `save-restriction' isn't called, there's no point doing so in
3699 this case either. So, if the buffer's restrictions are
3700 actually left unchanged, reset clip_changed. */
3701 if (obuf == current_buffer)
3703 if (begv == BEGV && zv == ZV)
3704 current_buffer->clip_changed = old_clip_changed;
3706 /* There isn't much we can reasonably do to protect against
3707 misbehaving fontification, but here's a fig leaf. */
3708 else if (BUFFER_LIVE_P (obuf))
3709 set_buffer_internal_1 (obuf);
3711 /* The fontification code may have added/removed text.
3712 It could do even a lot worse, but let's at least protect against
3713 the most obvious case where only the text past `pos' gets changed',
3714 as is/was done in grep.el where some escapes sequences are turned
3715 into face properties (bug#7876). */
3716 it->end_charpos = ZV;
3718 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3719 something. This avoids an endless loop if they failed to
3720 fontify the text for which reason ever. */
3721 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3722 handled = HANDLED_RECOMPUTE_PROPS;
3725 return handled;
3730 /***********************************************************************
3731 Faces
3732 ***********************************************************************/
3734 /* Set up iterator IT from face properties at its current position.
3735 Called from handle_stop. */
3737 static enum prop_handled
3738 handle_face_prop (struct it *it)
3740 int new_face_id;
3741 ptrdiff_t next_stop;
3743 if (!STRINGP (it->string))
3745 new_face_id
3746 = face_at_buffer_position (it->w,
3747 IT_CHARPOS (*it),
3748 it->region_beg_charpos,
3749 it->region_end_charpos,
3750 &next_stop,
3751 (IT_CHARPOS (*it)
3752 + TEXT_PROP_DISTANCE_LIMIT),
3753 0, it->base_face_id);
3755 /* Is this a start of a run of characters with box face?
3756 Caveat: this can be called for a freshly initialized
3757 iterator; face_id is -1 in this case. We know that the new
3758 face will not change until limit, i.e. if the new face has a
3759 box, all characters up to limit will have one. But, as
3760 usual, we don't know whether limit is really the end. */
3761 if (new_face_id != it->face_id)
3763 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3764 /* If it->face_id is -1, old_face below will be NULL, see
3765 the definition of FACE_FROM_ID. This will happen if this
3766 is the initial call that gets the face. */
3767 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3769 /* If the value of face_id of the iterator is -1, we have to
3770 look in front of IT's position and see whether there is a
3771 face there that's different from new_face_id. */
3772 if (!old_face && IT_CHARPOS (*it) > BEG)
3774 int prev_face_id = face_before_it_pos (it);
3776 old_face = FACE_FROM_ID (it->f, prev_face_id);
3779 /* If the new face has a box, but the old face does not,
3780 this is the start of a run of characters with box face,
3781 i.e. this character has a shadow on the left side. */
3782 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3783 && (old_face == NULL || !old_face->box));
3784 it->face_box_p = new_face->box != FACE_NO_BOX;
3787 else
3789 int base_face_id;
3790 ptrdiff_t bufpos;
3791 int i;
3792 Lisp_Object from_overlay
3793 = (it->current.overlay_string_index >= 0
3794 ? it->string_overlays[it->current.overlay_string_index
3795 % OVERLAY_STRING_CHUNK_SIZE]
3796 : Qnil);
3798 /* See if we got to this string directly or indirectly from
3799 an overlay property. That includes the before-string or
3800 after-string of an overlay, strings in display properties
3801 provided by an overlay, their text properties, etc.
3803 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3804 if (! NILP (from_overlay))
3805 for (i = it->sp - 1; i >= 0; i--)
3807 if (it->stack[i].current.overlay_string_index >= 0)
3808 from_overlay
3809 = it->string_overlays[it->stack[i].current.overlay_string_index
3810 % OVERLAY_STRING_CHUNK_SIZE];
3811 else if (! NILP (it->stack[i].from_overlay))
3812 from_overlay = it->stack[i].from_overlay;
3814 if (!NILP (from_overlay))
3815 break;
3818 if (! NILP (from_overlay))
3820 bufpos = IT_CHARPOS (*it);
3821 /* For a string from an overlay, the base face depends
3822 only on text properties and ignores overlays. */
3823 base_face_id
3824 = face_for_overlay_string (it->w,
3825 IT_CHARPOS (*it),
3826 it->region_beg_charpos,
3827 it->region_end_charpos,
3828 &next_stop,
3829 (IT_CHARPOS (*it)
3830 + TEXT_PROP_DISTANCE_LIMIT),
3832 from_overlay);
3834 else
3836 bufpos = 0;
3838 /* For strings from a `display' property, use the face at
3839 IT's current buffer position as the base face to merge
3840 with, so that overlay strings appear in the same face as
3841 surrounding text, unless they specify their own
3842 faces. */
3843 base_face_id = it->string_from_prefix_prop_p
3844 ? DEFAULT_FACE_ID
3845 : underlying_face_id (it);
3848 new_face_id = face_at_string_position (it->w,
3849 it->string,
3850 IT_STRING_CHARPOS (*it),
3851 bufpos,
3852 it->region_beg_charpos,
3853 it->region_end_charpos,
3854 &next_stop,
3855 base_face_id, 0);
3857 /* Is this a start of a run of characters with box? Caveat:
3858 this can be called for a freshly allocated iterator; face_id
3859 is -1 is this case. We know that the new face will not
3860 change until the next check pos, i.e. if the new face has a
3861 box, all characters up to that position will have a
3862 box. But, as usual, we don't know whether that position
3863 is really the end. */
3864 if (new_face_id != it->face_id)
3866 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3867 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3869 /* If new face has a box but old face hasn't, this is the
3870 start of a run of characters with box, i.e. it has a
3871 shadow on the left side. */
3872 it->start_of_box_run_p
3873 = new_face->box && (old_face == NULL || !old_face->box);
3874 it->face_box_p = new_face->box != FACE_NO_BOX;
3878 it->face_id = new_face_id;
3879 return HANDLED_NORMALLY;
3883 /* Return the ID of the face ``underlying'' IT's current position,
3884 which is in a string. If the iterator is associated with a
3885 buffer, return the face at IT's current buffer position.
3886 Otherwise, use the iterator's base_face_id. */
3888 static int
3889 underlying_face_id (struct it *it)
3891 int face_id = it->base_face_id, i;
3893 eassert (STRINGP (it->string));
3895 for (i = it->sp - 1; i >= 0; --i)
3896 if (NILP (it->stack[i].string))
3897 face_id = it->stack[i].face_id;
3899 return face_id;
3903 /* Compute the face one character before or after the current position
3904 of IT, in the visual order. BEFORE_P non-zero means get the face
3905 in front (to the left in L2R paragraphs, to the right in R2L
3906 paragraphs) of IT's screen position. Value is the ID of the face. */
3908 static int
3909 face_before_or_after_it_pos (struct it *it, int before_p)
3911 int face_id, limit;
3912 ptrdiff_t next_check_charpos;
3913 struct it it_copy;
3914 void *it_copy_data = NULL;
3916 eassert (it->s == NULL);
3918 if (STRINGP (it->string))
3920 ptrdiff_t bufpos, charpos;
3921 int base_face_id;
3923 /* No face change past the end of the string (for the case
3924 we are padding with spaces). No face change before the
3925 string start. */
3926 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3927 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3928 return it->face_id;
3930 if (!it->bidi_p)
3932 /* Set charpos to the position before or after IT's current
3933 position, in the logical order, which in the non-bidi
3934 case is the same as the visual order. */
3935 if (before_p)
3936 charpos = IT_STRING_CHARPOS (*it) - 1;
3937 else if (it->what == IT_COMPOSITION)
3938 /* For composition, we must check the character after the
3939 composition. */
3940 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3941 else
3942 charpos = IT_STRING_CHARPOS (*it) + 1;
3944 else
3946 if (before_p)
3948 /* With bidi iteration, the character before the current
3949 in the visual order cannot be found by simple
3950 iteration, because "reverse" reordering is not
3951 supported. Instead, we need to use the move_it_*
3952 family of functions. */
3953 /* Ignore face changes before the first visible
3954 character on this display line. */
3955 if (it->current_x <= it->first_visible_x)
3956 return it->face_id;
3957 SAVE_IT (it_copy, *it, it_copy_data);
3958 /* Implementation note: Since move_it_in_display_line
3959 works in the iterator geometry, and thinks the first
3960 character is always the leftmost, even in R2L lines,
3961 we don't need to distinguish between the R2L and L2R
3962 cases here. */
3963 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3964 it_copy.current_x - 1, MOVE_TO_X);
3965 charpos = IT_STRING_CHARPOS (it_copy);
3966 RESTORE_IT (it, it, it_copy_data);
3968 else
3970 /* Set charpos to the string position of the character
3971 that comes after IT's current position in the visual
3972 order. */
3973 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3975 it_copy = *it;
3976 while (n--)
3977 bidi_move_to_visually_next (&it_copy.bidi_it);
3979 charpos = it_copy.bidi_it.charpos;
3982 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3984 if (it->current.overlay_string_index >= 0)
3985 bufpos = IT_CHARPOS (*it);
3986 else
3987 bufpos = 0;
3989 base_face_id = underlying_face_id (it);
3991 /* Get the face for ASCII, or unibyte. */
3992 face_id = face_at_string_position (it->w,
3993 it->string,
3994 charpos,
3995 bufpos,
3996 it->region_beg_charpos,
3997 it->region_end_charpos,
3998 &next_check_charpos,
3999 base_face_id, 0);
4001 /* Correct the face for charsets different from ASCII. Do it
4002 for the multibyte case only. The face returned above is
4003 suitable for unibyte text if IT->string is unibyte. */
4004 if (STRING_MULTIBYTE (it->string))
4006 struct text_pos pos1 = string_pos (charpos, it->string);
4007 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4008 int c, len;
4009 struct face *face = FACE_FROM_ID (it->f, face_id);
4011 c = string_char_and_length (p, &len);
4012 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4015 else
4017 struct text_pos pos;
4019 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4020 || (IT_CHARPOS (*it) <= BEGV && before_p))
4021 return it->face_id;
4023 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4024 pos = it->current.pos;
4026 if (!it->bidi_p)
4028 if (before_p)
4029 DEC_TEXT_POS (pos, it->multibyte_p);
4030 else
4032 if (it->what == IT_COMPOSITION)
4034 /* For composition, we must check the position after
4035 the composition. */
4036 pos.charpos += it->cmp_it.nchars;
4037 pos.bytepos += it->len;
4039 else
4040 INC_TEXT_POS (pos, it->multibyte_p);
4043 else
4045 if (before_p)
4047 /* With bidi iteration, the character before the current
4048 in the visual order cannot be found by simple
4049 iteration, because "reverse" reordering is not
4050 supported. Instead, we need to use the move_it_*
4051 family of functions. */
4052 /* Ignore face changes before the first visible
4053 character on this display line. */
4054 if (it->current_x <= it->first_visible_x)
4055 return it->face_id;
4056 SAVE_IT (it_copy, *it, it_copy_data);
4057 /* Implementation note: Since move_it_in_display_line
4058 works in the iterator geometry, and thinks the first
4059 character is always the leftmost, even in R2L lines,
4060 we don't need to distinguish between the R2L and L2R
4061 cases here. */
4062 move_it_in_display_line (&it_copy, ZV,
4063 it_copy.current_x - 1, MOVE_TO_X);
4064 pos = it_copy.current.pos;
4065 RESTORE_IT (it, it, it_copy_data);
4067 else
4069 /* Set charpos to the buffer position of the character
4070 that comes after IT's current position in the visual
4071 order. */
4072 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4074 it_copy = *it;
4075 while (n--)
4076 bidi_move_to_visually_next (&it_copy.bidi_it);
4078 SET_TEXT_POS (pos,
4079 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4082 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4084 /* Determine face for CHARSET_ASCII, or unibyte. */
4085 face_id = face_at_buffer_position (it->w,
4086 CHARPOS (pos),
4087 it->region_beg_charpos,
4088 it->region_end_charpos,
4089 &next_check_charpos,
4090 limit, 0, -1);
4092 /* Correct the face for charsets different from ASCII. Do it
4093 for the multibyte case only. The face returned above is
4094 suitable for unibyte text if current_buffer is unibyte. */
4095 if (it->multibyte_p)
4097 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4098 struct face *face = FACE_FROM_ID (it->f, face_id);
4099 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4103 return face_id;
4108 /***********************************************************************
4109 Invisible text
4110 ***********************************************************************/
4112 /* Set up iterator IT from invisible properties at its current
4113 position. Called from handle_stop. */
4115 static enum prop_handled
4116 handle_invisible_prop (struct it *it)
4118 enum prop_handled handled = HANDLED_NORMALLY;
4119 int invis_p;
4120 Lisp_Object prop;
4122 if (STRINGP (it->string))
4124 Lisp_Object end_charpos, limit, charpos;
4126 /* Get the value of the invisible text property at the
4127 current position. Value will be nil if there is no such
4128 property. */
4129 charpos = make_number (IT_STRING_CHARPOS (*it));
4130 prop = Fget_text_property (charpos, Qinvisible, it->string);
4131 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4133 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4135 /* Record whether we have to display an ellipsis for the
4136 invisible text. */
4137 int display_ellipsis_p = (invis_p == 2);
4138 ptrdiff_t len, endpos;
4140 handled = HANDLED_RECOMPUTE_PROPS;
4142 /* Get the position at which the next visible text can be
4143 found in IT->string, if any. */
4144 endpos = len = SCHARS (it->string);
4145 XSETINT (limit, len);
4148 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4149 it->string, limit);
4150 if (INTEGERP (end_charpos))
4152 endpos = XFASTINT (end_charpos);
4153 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4154 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4155 if (invis_p == 2)
4156 display_ellipsis_p = 1;
4159 while (invis_p && endpos < len);
4161 if (display_ellipsis_p)
4162 it->ellipsis_p = 1;
4164 if (endpos < len)
4166 /* Text at END_CHARPOS is visible. Move IT there. */
4167 struct text_pos old;
4168 ptrdiff_t oldpos;
4170 old = it->current.string_pos;
4171 oldpos = CHARPOS (old);
4172 if (it->bidi_p)
4174 if (it->bidi_it.first_elt
4175 && it->bidi_it.charpos < SCHARS (it->string))
4176 bidi_paragraph_init (it->paragraph_embedding,
4177 &it->bidi_it, 1);
4178 /* Bidi-iterate out of the invisible text. */
4181 bidi_move_to_visually_next (&it->bidi_it);
4183 while (oldpos <= it->bidi_it.charpos
4184 && it->bidi_it.charpos < endpos);
4186 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4187 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4188 if (IT_CHARPOS (*it) >= endpos)
4189 it->prev_stop = endpos;
4191 else
4193 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4194 compute_string_pos (&it->current.string_pos, old, it->string);
4197 else
4199 /* The rest of the string is invisible. If this is an
4200 overlay string, proceed with the next overlay string
4201 or whatever comes and return a character from there. */
4202 if (it->current.overlay_string_index >= 0
4203 && !display_ellipsis_p)
4205 next_overlay_string (it);
4206 /* Don't check for overlay strings when we just
4207 finished processing them. */
4208 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4210 else
4212 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4213 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4218 else
4220 ptrdiff_t newpos, next_stop, start_charpos, tem;
4221 Lisp_Object pos, overlay;
4223 /* First of all, is there invisible text at this position? */
4224 tem = start_charpos = IT_CHARPOS (*it);
4225 pos = make_number (tem);
4226 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4227 &overlay);
4228 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4230 /* If we are on invisible text, skip over it. */
4231 if (invis_p && start_charpos < it->end_charpos)
4233 /* Record whether we have to display an ellipsis for the
4234 invisible text. */
4235 int display_ellipsis_p = invis_p == 2;
4237 handled = HANDLED_RECOMPUTE_PROPS;
4239 /* Loop skipping over invisible text. The loop is left at
4240 ZV or with IT on the first char being visible again. */
4243 /* Try to skip some invisible text. Return value is the
4244 position reached which can be equal to where we start
4245 if there is nothing invisible there. This skips both
4246 over invisible text properties and overlays with
4247 invisible property. */
4248 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4250 /* If we skipped nothing at all we weren't at invisible
4251 text in the first place. If everything to the end of
4252 the buffer was skipped, end the loop. */
4253 if (newpos == tem || newpos >= ZV)
4254 invis_p = 0;
4255 else
4257 /* We skipped some characters but not necessarily
4258 all there are. Check if we ended up on visible
4259 text. Fget_char_property returns the property of
4260 the char before the given position, i.e. if we
4261 get invis_p = 0, this means that the char at
4262 newpos is visible. */
4263 pos = make_number (newpos);
4264 prop = Fget_char_property (pos, Qinvisible, it->window);
4265 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4268 /* If we ended up on invisible text, proceed to
4269 skip starting with next_stop. */
4270 if (invis_p)
4271 tem = next_stop;
4273 /* If there are adjacent invisible texts, don't lose the
4274 second one's ellipsis. */
4275 if (invis_p == 2)
4276 display_ellipsis_p = 1;
4278 while (invis_p);
4280 /* The position newpos is now either ZV or on visible text. */
4281 if (it->bidi_p)
4283 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4284 int on_newline =
4285 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4286 int after_newline =
4287 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4289 /* If the invisible text ends on a newline or on a
4290 character after a newline, we can avoid the costly,
4291 character by character, bidi iteration to NEWPOS, and
4292 instead simply reseat the iterator there. That's
4293 because all bidi reordering information is tossed at
4294 the newline. This is a big win for modes that hide
4295 complete lines, like Outline, Org, etc. */
4296 if (on_newline || after_newline)
4298 struct text_pos tpos;
4299 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4301 SET_TEXT_POS (tpos, newpos, bpos);
4302 reseat_1 (it, tpos, 0);
4303 /* If we reseat on a newline/ZV, we need to prep the
4304 bidi iterator for advancing to the next character
4305 after the newline/EOB, keeping the current paragraph
4306 direction (so that PRODUCE_GLYPHS does TRT wrt
4307 prepending/appending glyphs to a glyph row). */
4308 if (on_newline)
4310 it->bidi_it.first_elt = 0;
4311 it->bidi_it.paragraph_dir = pdir;
4312 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4313 it->bidi_it.nchars = 1;
4314 it->bidi_it.ch_len = 1;
4317 else /* Must use the slow method. */
4319 /* With bidi iteration, the region of invisible text
4320 could start and/or end in the middle of a
4321 non-base embedding level. Therefore, we need to
4322 skip invisible text using the bidi iterator,
4323 starting at IT's current position, until we find
4324 ourselves outside of the invisible text.
4325 Skipping invisible text _after_ bidi iteration
4326 avoids affecting the visual order of the
4327 displayed text when invisible properties are
4328 added or removed. */
4329 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4331 /* If we were `reseat'ed to a new paragraph,
4332 determine the paragraph base direction. We
4333 need to do it now because
4334 next_element_from_buffer may not have a
4335 chance to do it, if we are going to skip any
4336 text at the beginning, which resets the
4337 FIRST_ELT flag. */
4338 bidi_paragraph_init (it->paragraph_embedding,
4339 &it->bidi_it, 1);
4343 bidi_move_to_visually_next (&it->bidi_it);
4345 while (it->stop_charpos <= it->bidi_it.charpos
4346 && it->bidi_it.charpos < newpos);
4347 IT_CHARPOS (*it) = it->bidi_it.charpos;
4348 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4349 /* If we overstepped NEWPOS, record its position in
4350 the iterator, so that we skip invisible text if
4351 later the bidi iteration lands us in the
4352 invisible region again. */
4353 if (IT_CHARPOS (*it) >= newpos)
4354 it->prev_stop = newpos;
4357 else
4359 IT_CHARPOS (*it) = newpos;
4360 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4363 /* If there are before-strings at the start of invisible
4364 text, and the text is invisible because of a text
4365 property, arrange to show before-strings because 20.x did
4366 it that way. (If the text is invisible because of an
4367 overlay property instead of a text property, this is
4368 already handled in the overlay code.) */
4369 if (NILP (overlay)
4370 && get_overlay_strings (it, it->stop_charpos))
4372 handled = HANDLED_RECOMPUTE_PROPS;
4373 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4375 else if (display_ellipsis_p)
4377 /* Make sure that the glyphs of the ellipsis will get
4378 correct `charpos' values. If we would not update
4379 it->position here, the glyphs would belong to the
4380 last visible character _before_ the invisible
4381 text, which confuses `set_cursor_from_row'.
4383 We use the last invisible position instead of the
4384 first because this way the cursor is always drawn on
4385 the first "." of the ellipsis, whenever PT is inside
4386 the invisible text. Otherwise the cursor would be
4387 placed _after_ the ellipsis when the point is after the
4388 first invisible character. */
4389 if (!STRINGP (it->object))
4391 it->position.charpos = newpos - 1;
4392 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4394 it->ellipsis_p = 1;
4395 /* Let the ellipsis display before
4396 considering any properties of the following char.
4397 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4398 handled = HANDLED_RETURN;
4403 return handled;
4407 /* Make iterator IT return `...' next.
4408 Replaces LEN characters from buffer. */
4410 static void
4411 setup_for_ellipsis (struct it *it, int len)
4413 /* Use the display table definition for `...'. Invalid glyphs
4414 will be handled by the method returning elements from dpvec. */
4415 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4417 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4418 it->dpvec = v->contents;
4419 it->dpend = v->contents + v->header.size;
4421 else
4423 /* Default `...'. */
4424 it->dpvec = default_invis_vector;
4425 it->dpend = default_invis_vector + 3;
4428 it->dpvec_char_len = len;
4429 it->current.dpvec_index = 0;
4430 it->dpvec_face_id = -1;
4432 /* Remember the current face id in case glyphs specify faces.
4433 IT's face is restored in set_iterator_to_next.
4434 saved_face_id was set to preceding char's face in handle_stop. */
4435 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4436 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4438 it->method = GET_FROM_DISPLAY_VECTOR;
4439 it->ellipsis_p = 1;
4444 /***********************************************************************
4445 'display' property
4446 ***********************************************************************/
4448 /* Set up iterator IT from `display' property at its current position.
4449 Called from handle_stop.
4450 We return HANDLED_RETURN if some part of the display property
4451 overrides the display of the buffer text itself.
4452 Otherwise we return HANDLED_NORMALLY. */
4454 static enum prop_handled
4455 handle_display_prop (struct it *it)
4457 Lisp_Object propval, object, overlay;
4458 struct text_pos *position;
4459 ptrdiff_t bufpos;
4460 /* Nonzero if some property replaces the display of the text itself. */
4461 int display_replaced_p = 0;
4463 if (STRINGP (it->string))
4465 object = it->string;
4466 position = &it->current.string_pos;
4467 bufpos = CHARPOS (it->current.pos);
4469 else
4471 XSETWINDOW (object, it->w);
4472 position = &it->current.pos;
4473 bufpos = CHARPOS (*position);
4476 /* Reset those iterator values set from display property values. */
4477 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4478 it->space_width = Qnil;
4479 it->font_height = Qnil;
4480 it->voffset = 0;
4482 /* We don't support recursive `display' properties, i.e. string
4483 values that have a string `display' property, that have a string
4484 `display' property etc. */
4485 if (!it->string_from_display_prop_p)
4486 it->area = TEXT_AREA;
4488 propval = get_char_property_and_overlay (make_number (position->charpos),
4489 Qdisplay, object, &overlay);
4490 if (NILP (propval))
4491 return HANDLED_NORMALLY;
4492 /* Now OVERLAY is the overlay that gave us this property, or nil
4493 if it was a text property. */
4495 if (!STRINGP (it->string))
4496 object = it->w->contents;
4498 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4499 position, bufpos,
4500 FRAME_WINDOW_P (it->f));
4502 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4505 /* Subroutine of handle_display_prop. Returns non-zero if the display
4506 specification in SPEC is a replacing specification, i.e. it would
4507 replace the text covered by `display' property with something else,
4508 such as an image or a display string. If SPEC includes any kind or
4509 `(space ...) specification, the value is 2; this is used by
4510 compute_display_string_pos, which see.
4512 See handle_single_display_spec for documentation of arguments.
4513 frame_window_p is non-zero if the window being redisplayed is on a
4514 GUI frame; this argument is used only if IT is NULL, see below.
4516 IT can be NULL, if this is called by the bidi reordering code
4517 through compute_display_string_pos, which see. In that case, this
4518 function only examines SPEC, but does not otherwise "handle" it, in
4519 the sense that it doesn't set up members of IT from the display
4520 spec. */
4521 static int
4522 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4523 Lisp_Object overlay, struct text_pos *position,
4524 ptrdiff_t bufpos, int frame_window_p)
4526 int replacing_p = 0;
4527 int rv;
4529 if (CONSP (spec)
4530 /* Simple specifications. */
4531 && !EQ (XCAR (spec), Qimage)
4532 && !EQ (XCAR (spec), Qspace)
4533 && !EQ (XCAR (spec), Qwhen)
4534 && !EQ (XCAR (spec), Qslice)
4535 && !EQ (XCAR (spec), Qspace_width)
4536 && !EQ (XCAR (spec), Qheight)
4537 && !EQ (XCAR (spec), Qraise)
4538 /* Marginal area specifications. */
4539 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4540 && !EQ (XCAR (spec), Qleft_fringe)
4541 && !EQ (XCAR (spec), Qright_fringe)
4542 && !NILP (XCAR (spec)))
4544 for (; CONSP (spec); spec = XCDR (spec))
4546 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4547 overlay, position, bufpos,
4548 replacing_p, frame_window_p)))
4550 replacing_p = rv;
4551 /* If some text in a string is replaced, `position' no
4552 longer points to the position of `object'. */
4553 if (!it || STRINGP (object))
4554 break;
4558 else if (VECTORP (spec))
4560 ptrdiff_t i;
4561 for (i = 0; i < ASIZE (spec); ++i)
4562 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4563 overlay, position, bufpos,
4564 replacing_p, frame_window_p)))
4566 replacing_p = rv;
4567 /* If some text in a string is replaced, `position' no
4568 longer points to the position of `object'. */
4569 if (!it || STRINGP (object))
4570 break;
4573 else
4575 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4576 position, bufpos, 0,
4577 frame_window_p)))
4578 replacing_p = rv;
4581 return replacing_p;
4584 /* Value is the position of the end of the `display' property starting
4585 at START_POS in OBJECT. */
4587 static struct text_pos
4588 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4590 Lisp_Object end;
4591 struct text_pos end_pos;
4593 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4594 Qdisplay, object, Qnil);
4595 CHARPOS (end_pos) = XFASTINT (end);
4596 if (STRINGP (object))
4597 compute_string_pos (&end_pos, start_pos, it->string);
4598 else
4599 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4601 return end_pos;
4605 /* Set up IT from a single `display' property specification SPEC. OBJECT
4606 is the object in which the `display' property was found. *POSITION
4607 is the position in OBJECT at which the `display' property was found.
4608 BUFPOS is the buffer position of OBJECT (different from POSITION if
4609 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4610 previously saw a display specification which already replaced text
4611 display with something else, for example an image; we ignore such
4612 properties after the first one has been processed.
4614 OVERLAY is the overlay this `display' property came from,
4615 or nil if it was a text property.
4617 If SPEC is a `space' or `image' specification, and in some other
4618 cases too, set *POSITION to the position where the `display'
4619 property ends.
4621 If IT is NULL, only examine the property specification in SPEC, but
4622 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4623 is intended to be displayed in a window on a GUI frame.
4625 Value is non-zero if something was found which replaces the display
4626 of buffer or string text. */
4628 static int
4629 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4630 Lisp_Object overlay, struct text_pos *position,
4631 ptrdiff_t bufpos, int display_replaced_p,
4632 int frame_window_p)
4634 Lisp_Object form;
4635 Lisp_Object location, value;
4636 struct text_pos start_pos = *position;
4637 int valid_p;
4639 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4640 If the result is non-nil, use VALUE instead of SPEC. */
4641 form = Qt;
4642 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4644 spec = XCDR (spec);
4645 if (!CONSP (spec))
4646 return 0;
4647 form = XCAR (spec);
4648 spec = XCDR (spec);
4651 if (!NILP (form) && !EQ (form, Qt))
4653 ptrdiff_t count = SPECPDL_INDEX ();
4654 struct gcpro gcpro1;
4656 /* Bind `object' to the object having the `display' property, a
4657 buffer or string. Bind `position' to the position in the
4658 object where the property was found, and `buffer-position'
4659 to the current position in the buffer. */
4661 if (NILP (object))
4662 XSETBUFFER (object, current_buffer);
4663 specbind (Qobject, object);
4664 specbind (Qposition, make_number (CHARPOS (*position)));
4665 specbind (Qbuffer_position, make_number (bufpos));
4666 GCPRO1 (form);
4667 form = safe_eval (form);
4668 UNGCPRO;
4669 unbind_to (count, Qnil);
4672 if (NILP (form))
4673 return 0;
4675 /* Handle `(height HEIGHT)' specifications. */
4676 if (CONSP (spec)
4677 && EQ (XCAR (spec), Qheight)
4678 && CONSP (XCDR (spec)))
4680 if (it)
4682 if (!FRAME_WINDOW_P (it->f))
4683 return 0;
4685 it->font_height = XCAR (XCDR (spec));
4686 if (!NILP (it->font_height))
4688 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4689 int new_height = -1;
4691 if (CONSP (it->font_height)
4692 && (EQ (XCAR (it->font_height), Qplus)
4693 || EQ (XCAR (it->font_height), Qminus))
4694 && CONSP (XCDR (it->font_height))
4695 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4697 /* `(+ N)' or `(- N)' where N is an integer. */
4698 int steps = XINT (XCAR (XCDR (it->font_height)));
4699 if (EQ (XCAR (it->font_height), Qplus))
4700 steps = - steps;
4701 it->face_id = smaller_face (it->f, it->face_id, steps);
4703 else if (FUNCTIONP (it->font_height))
4705 /* Call function with current height as argument.
4706 Value is the new height. */
4707 Lisp_Object height;
4708 height = safe_call1 (it->font_height,
4709 face->lface[LFACE_HEIGHT_INDEX]);
4710 if (NUMBERP (height))
4711 new_height = XFLOATINT (height);
4713 else if (NUMBERP (it->font_height))
4715 /* Value is a multiple of the canonical char height. */
4716 struct face *f;
4718 f = FACE_FROM_ID (it->f,
4719 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4720 new_height = (XFLOATINT (it->font_height)
4721 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4723 else
4725 /* Evaluate IT->font_height with `height' bound to the
4726 current specified height to get the new height. */
4727 ptrdiff_t count = SPECPDL_INDEX ();
4729 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4730 value = safe_eval (it->font_height);
4731 unbind_to (count, Qnil);
4733 if (NUMBERP (value))
4734 new_height = XFLOATINT (value);
4737 if (new_height > 0)
4738 it->face_id = face_with_height (it->f, it->face_id, new_height);
4742 return 0;
4745 /* Handle `(space-width WIDTH)'. */
4746 if (CONSP (spec)
4747 && EQ (XCAR (spec), Qspace_width)
4748 && CONSP (XCDR (spec)))
4750 if (it)
4752 if (!FRAME_WINDOW_P (it->f))
4753 return 0;
4755 value = XCAR (XCDR (spec));
4756 if (NUMBERP (value) && XFLOATINT (value) > 0)
4757 it->space_width = value;
4760 return 0;
4763 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4764 if (CONSP (spec)
4765 && EQ (XCAR (spec), Qslice))
4767 Lisp_Object tem;
4769 if (it)
4771 if (!FRAME_WINDOW_P (it->f))
4772 return 0;
4774 if (tem = XCDR (spec), CONSP (tem))
4776 it->slice.x = XCAR (tem);
4777 if (tem = XCDR (tem), CONSP (tem))
4779 it->slice.y = XCAR (tem);
4780 if (tem = XCDR (tem), CONSP (tem))
4782 it->slice.width = XCAR (tem);
4783 if (tem = XCDR (tem), CONSP (tem))
4784 it->slice.height = XCAR (tem);
4790 return 0;
4793 /* Handle `(raise FACTOR)'. */
4794 if (CONSP (spec)
4795 && EQ (XCAR (spec), Qraise)
4796 && CONSP (XCDR (spec)))
4798 if (it)
4800 if (!FRAME_WINDOW_P (it->f))
4801 return 0;
4803 #ifdef HAVE_WINDOW_SYSTEM
4804 value = XCAR (XCDR (spec));
4805 if (NUMBERP (value))
4807 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4808 it->voffset = - (XFLOATINT (value)
4809 * (FONT_HEIGHT (face->font)));
4811 #endif /* HAVE_WINDOW_SYSTEM */
4814 return 0;
4817 /* Don't handle the other kinds of display specifications
4818 inside a string that we got from a `display' property. */
4819 if (it && it->string_from_display_prop_p)
4820 return 0;
4822 /* Characters having this form of property are not displayed, so
4823 we have to find the end of the property. */
4824 if (it)
4826 start_pos = *position;
4827 *position = display_prop_end (it, object, start_pos);
4829 value = Qnil;
4831 /* Stop the scan at that end position--we assume that all
4832 text properties change there. */
4833 if (it)
4834 it->stop_charpos = position->charpos;
4836 /* Handle `(left-fringe BITMAP [FACE])'
4837 and `(right-fringe BITMAP [FACE])'. */
4838 if (CONSP (spec)
4839 && (EQ (XCAR (spec), Qleft_fringe)
4840 || EQ (XCAR (spec), Qright_fringe))
4841 && CONSP (XCDR (spec)))
4843 int fringe_bitmap;
4845 if (it)
4847 if (!FRAME_WINDOW_P (it->f))
4848 /* If we return here, POSITION has been advanced
4849 across the text with this property. */
4851 /* Synchronize the bidi iterator with POSITION. This is
4852 needed because we are not going to push the iterator
4853 on behalf of this display property, so there will be
4854 no pop_it call to do this synchronization for us. */
4855 if (it->bidi_p)
4857 it->position = *position;
4858 iterate_out_of_display_property (it);
4859 *position = it->position;
4861 return 1;
4864 else if (!frame_window_p)
4865 return 1;
4867 #ifdef HAVE_WINDOW_SYSTEM
4868 value = XCAR (XCDR (spec));
4869 if (!SYMBOLP (value)
4870 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4871 /* If we return here, POSITION has been advanced
4872 across the text with this property. */
4874 if (it && it->bidi_p)
4876 it->position = *position;
4877 iterate_out_of_display_property (it);
4878 *position = it->position;
4880 return 1;
4883 if (it)
4885 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4887 if (CONSP (XCDR (XCDR (spec))))
4889 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4890 int face_id2 = lookup_derived_face (it->f, face_name,
4891 FRINGE_FACE_ID, 0);
4892 if (face_id2 >= 0)
4893 face_id = face_id2;
4896 /* Save current settings of IT so that we can restore them
4897 when we are finished with the glyph property value. */
4898 push_it (it, position);
4900 it->area = TEXT_AREA;
4901 it->what = IT_IMAGE;
4902 it->image_id = -1; /* no image */
4903 it->position = start_pos;
4904 it->object = NILP (object) ? it->w->contents : object;
4905 it->method = GET_FROM_IMAGE;
4906 it->from_overlay = Qnil;
4907 it->face_id = face_id;
4908 it->from_disp_prop_p = 1;
4910 /* Say that we haven't consumed the characters with
4911 `display' property yet. The call to pop_it in
4912 set_iterator_to_next will clean this up. */
4913 *position = start_pos;
4915 if (EQ (XCAR (spec), Qleft_fringe))
4917 it->left_user_fringe_bitmap = fringe_bitmap;
4918 it->left_user_fringe_face_id = face_id;
4920 else
4922 it->right_user_fringe_bitmap = fringe_bitmap;
4923 it->right_user_fringe_face_id = face_id;
4926 #endif /* HAVE_WINDOW_SYSTEM */
4927 return 1;
4930 /* Prepare to handle `((margin left-margin) ...)',
4931 `((margin right-margin) ...)' and `((margin nil) ...)'
4932 prefixes for display specifications. */
4933 location = Qunbound;
4934 if (CONSP (spec) && CONSP (XCAR (spec)))
4936 Lisp_Object tem;
4938 value = XCDR (spec);
4939 if (CONSP (value))
4940 value = XCAR (value);
4942 tem = XCAR (spec);
4943 if (EQ (XCAR (tem), Qmargin)
4944 && (tem = XCDR (tem),
4945 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4946 (NILP (tem)
4947 || EQ (tem, Qleft_margin)
4948 || EQ (tem, Qright_margin))))
4949 location = tem;
4952 if (EQ (location, Qunbound))
4954 location = Qnil;
4955 value = spec;
4958 /* After this point, VALUE is the property after any
4959 margin prefix has been stripped. It must be a string,
4960 an image specification, or `(space ...)'.
4962 LOCATION specifies where to display: `left-margin',
4963 `right-margin' or nil. */
4965 valid_p = (STRINGP (value)
4966 #ifdef HAVE_WINDOW_SYSTEM
4967 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4968 && valid_image_p (value))
4969 #endif /* not HAVE_WINDOW_SYSTEM */
4970 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4972 if (valid_p && !display_replaced_p)
4974 int retval = 1;
4976 if (!it)
4978 /* Callers need to know whether the display spec is any kind
4979 of `(space ...)' spec that is about to affect text-area
4980 display. */
4981 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4982 retval = 2;
4983 return retval;
4986 /* Save current settings of IT so that we can restore them
4987 when we are finished with the glyph property value. */
4988 push_it (it, position);
4989 it->from_overlay = overlay;
4990 it->from_disp_prop_p = 1;
4992 if (NILP (location))
4993 it->area = TEXT_AREA;
4994 else if (EQ (location, Qleft_margin))
4995 it->area = LEFT_MARGIN_AREA;
4996 else
4997 it->area = RIGHT_MARGIN_AREA;
4999 if (STRINGP (value))
5001 it->string = value;
5002 it->multibyte_p = STRING_MULTIBYTE (it->string);
5003 it->current.overlay_string_index = -1;
5004 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5005 it->end_charpos = it->string_nchars = SCHARS (it->string);
5006 it->method = GET_FROM_STRING;
5007 it->stop_charpos = 0;
5008 it->prev_stop = 0;
5009 it->base_level_stop = 0;
5010 it->string_from_display_prop_p = 1;
5011 /* Say that we haven't consumed the characters with
5012 `display' property yet. The call to pop_it in
5013 set_iterator_to_next will clean this up. */
5014 if (BUFFERP (object))
5015 *position = start_pos;
5017 /* Force paragraph direction to be that of the parent
5018 object. If the parent object's paragraph direction is
5019 not yet determined, default to L2R. */
5020 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5021 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5022 else
5023 it->paragraph_embedding = L2R;
5025 /* Set up the bidi iterator for this display string. */
5026 if (it->bidi_p)
5028 it->bidi_it.string.lstring = it->string;
5029 it->bidi_it.string.s = NULL;
5030 it->bidi_it.string.schars = it->end_charpos;
5031 it->bidi_it.string.bufpos = bufpos;
5032 it->bidi_it.string.from_disp_str = 1;
5033 it->bidi_it.string.unibyte = !it->multibyte_p;
5034 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5037 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5039 it->method = GET_FROM_STRETCH;
5040 it->object = value;
5041 *position = it->position = start_pos;
5042 retval = 1 + (it->area == TEXT_AREA);
5044 #ifdef HAVE_WINDOW_SYSTEM
5045 else
5047 it->what = IT_IMAGE;
5048 it->image_id = lookup_image (it->f, value);
5049 it->position = start_pos;
5050 it->object = NILP (object) ? it->w->contents : object;
5051 it->method = GET_FROM_IMAGE;
5053 /* Say that we haven't consumed the characters with
5054 `display' property yet. The call to pop_it in
5055 set_iterator_to_next will clean this up. */
5056 *position = start_pos;
5058 #endif /* HAVE_WINDOW_SYSTEM */
5060 return retval;
5063 /* Invalid property or property not supported. Restore
5064 POSITION to what it was before. */
5065 *position = start_pos;
5066 return 0;
5069 /* Check if PROP is a display property value whose text should be
5070 treated as intangible. OVERLAY is the overlay from which PROP
5071 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5072 specify the buffer position covered by PROP. */
5075 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5076 ptrdiff_t charpos, ptrdiff_t bytepos)
5078 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5079 struct text_pos position;
5081 SET_TEXT_POS (position, charpos, bytepos);
5082 return handle_display_spec (NULL, prop, Qnil, overlay,
5083 &position, charpos, frame_window_p);
5087 /* Return 1 if PROP is a display sub-property value containing STRING.
5089 Implementation note: this and the following function are really
5090 special cases of handle_display_spec and
5091 handle_single_display_spec, and should ideally use the same code.
5092 Until they do, these two pairs must be consistent and must be
5093 modified in sync. */
5095 static int
5096 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5098 if (EQ (string, prop))
5099 return 1;
5101 /* Skip over `when FORM'. */
5102 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5104 prop = XCDR (prop);
5105 if (!CONSP (prop))
5106 return 0;
5107 /* Actually, the condition following `when' should be eval'ed,
5108 like handle_single_display_spec does, and we should return
5109 zero if it evaluates to nil. However, this function is
5110 called only when the buffer was already displayed and some
5111 glyph in the glyph matrix was found to come from a display
5112 string. Therefore, the condition was already evaluated, and
5113 the result was non-nil, otherwise the display string wouldn't
5114 have been displayed and we would have never been called for
5115 this property. Thus, we can skip the evaluation and assume
5116 its result is non-nil. */
5117 prop = XCDR (prop);
5120 if (CONSP (prop))
5121 /* Skip over `margin LOCATION'. */
5122 if (EQ (XCAR (prop), Qmargin))
5124 prop = XCDR (prop);
5125 if (!CONSP (prop))
5126 return 0;
5128 prop = XCDR (prop);
5129 if (!CONSP (prop))
5130 return 0;
5133 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5137 /* Return 1 if STRING appears in the `display' property PROP. */
5139 static int
5140 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5142 if (CONSP (prop)
5143 && !EQ (XCAR (prop), Qwhen)
5144 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5146 /* A list of sub-properties. */
5147 while (CONSP (prop))
5149 if (single_display_spec_string_p (XCAR (prop), string))
5150 return 1;
5151 prop = XCDR (prop);
5154 else if (VECTORP (prop))
5156 /* A vector of sub-properties. */
5157 ptrdiff_t i;
5158 for (i = 0; i < ASIZE (prop); ++i)
5159 if (single_display_spec_string_p (AREF (prop, i), string))
5160 return 1;
5162 else
5163 return single_display_spec_string_p (prop, string);
5165 return 0;
5168 /* Look for STRING in overlays and text properties in the current
5169 buffer, between character positions FROM and TO (excluding TO).
5170 BACK_P non-zero means look back (in this case, TO is supposed to be
5171 less than FROM).
5172 Value is the first character position where STRING was found, or
5173 zero if it wasn't found before hitting TO.
5175 This function may only use code that doesn't eval because it is
5176 called asynchronously from note_mouse_highlight. */
5178 static ptrdiff_t
5179 string_buffer_position_lim (Lisp_Object string,
5180 ptrdiff_t from, ptrdiff_t to, int back_p)
5182 Lisp_Object limit, prop, pos;
5183 int found = 0;
5185 pos = make_number (max (from, BEGV));
5187 if (!back_p) /* looking forward */
5189 limit = make_number (min (to, ZV));
5190 while (!found && !EQ (pos, limit))
5192 prop = Fget_char_property (pos, Qdisplay, Qnil);
5193 if (!NILP (prop) && display_prop_string_p (prop, string))
5194 found = 1;
5195 else
5196 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5197 limit);
5200 else /* looking back */
5202 limit = make_number (max (to, BEGV));
5203 while (!found && !EQ (pos, limit))
5205 prop = Fget_char_property (pos, Qdisplay, Qnil);
5206 if (!NILP (prop) && display_prop_string_p (prop, string))
5207 found = 1;
5208 else
5209 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5210 limit);
5214 return found ? XINT (pos) : 0;
5217 /* Determine which buffer position in current buffer STRING comes from.
5218 AROUND_CHARPOS is an approximate position where it could come from.
5219 Value is the buffer position or 0 if it couldn't be determined.
5221 This function is necessary because we don't record buffer positions
5222 in glyphs generated from strings (to keep struct glyph small).
5223 This function may only use code that doesn't eval because it is
5224 called asynchronously from note_mouse_highlight. */
5226 static ptrdiff_t
5227 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5229 const int MAX_DISTANCE = 1000;
5230 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5231 around_charpos + MAX_DISTANCE,
5234 if (!found)
5235 found = string_buffer_position_lim (string, around_charpos,
5236 around_charpos - MAX_DISTANCE, 1);
5237 return found;
5242 /***********************************************************************
5243 `composition' property
5244 ***********************************************************************/
5246 /* Set up iterator IT from `composition' property at its current
5247 position. Called from handle_stop. */
5249 static enum prop_handled
5250 handle_composition_prop (struct it *it)
5252 Lisp_Object prop, string;
5253 ptrdiff_t pos, pos_byte, start, end;
5255 if (STRINGP (it->string))
5257 unsigned char *s;
5259 pos = IT_STRING_CHARPOS (*it);
5260 pos_byte = IT_STRING_BYTEPOS (*it);
5261 string = it->string;
5262 s = SDATA (string) + pos_byte;
5263 it->c = STRING_CHAR (s);
5265 else
5267 pos = IT_CHARPOS (*it);
5268 pos_byte = IT_BYTEPOS (*it);
5269 string = Qnil;
5270 it->c = FETCH_CHAR (pos_byte);
5273 /* If there's a valid composition and point is not inside of the
5274 composition (in the case that the composition is from the current
5275 buffer), draw a glyph composed from the composition components. */
5276 if (find_composition (pos, -1, &start, &end, &prop, string)
5277 && COMPOSITION_VALID_P (start, end, prop)
5278 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5280 if (start < pos)
5281 /* As we can't handle this situation (perhaps font-lock added
5282 a new composition), we just return here hoping that next
5283 redisplay will detect this composition much earlier. */
5284 return HANDLED_NORMALLY;
5285 if (start != pos)
5287 if (STRINGP (it->string))
5288 pos_byte = string_char_to_byte (it->string, start);
5289 else
5290 pos_byte = CHAR_TO_BYTE (start);
5292 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5293 prop, string);
5295 if (it->cmp_it.id >= 0)
5297 it->cmp_it.ch = -1;
5298 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5299 it->cmp_it.nglyphs = -1;
5303 return HANDLED_NORMALLY;
5308 /***********************************************************************
5309 Overlay strings
5310 ***********************************************************************/
5312 /* The following structure is used to record overlay strings for
5313 later sorting in load_overlay_strings. */
5315 struct overlay_entry
5317 Lisp_Object overlay;
5318 Lisp_Object string;
5319 EMACS_INT priority;
5320 int after_string_p;
5324 /* Set up iterator IT from overlay strings at its current position.
5325 Called from handle_stop. */
5327 static enum prop_handled
5328 handle_overlay_change (struct it *it)
5330 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5331 return HANDLED_RECOMPUTE_PROPS;
5332 else
5333 return HANDLED_NORMALLY;
5337 /* Set up the next overlay string for delivery by IT, if there is an
5338 overlay string to deliver. Called by set_iterator_to_next when the
5339 end of the current overlay string is reached. If there are more
5340 overlay strings to display, IT->string and
5341 IT->current.overlay_string_index are set appropriately here.
5342 Otherwise IT->string is set to nil. */
5344 static void
5345 next_overlay_string (struct it *it)
5347 ++it->current.overlay_string_index;
5348 if (it->current.overlay_string_index == it->n_overlay_strings)
5350 /* No more overlay strings. Restore IT's settings to what
5351 they were before overlay strings were processed, and
5352 continue to deliver from current_buffer. */
5354 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5355 pop_it (it);
5356 eassert (it->sp > 0
5357 || (NILP (it->string)
5358 && it->method == GET_FROM_BUFFER
5359 && it->stop_charpos >= BEGV
5360 && it->stop_charpos <= it->end_charpos));
5361 it->current.overlay_string_index = -1;
5362 it->n_overlay_strings = 0;
5363 it->overlay_strings_charpos = -1;
5364 /* If there's an empty display string on the stack, pop the
5365 stack, to resync the bidi iterator with IT's position. Such
5366 empty strings are pushed onto the stack in
5367 get_overlay_strings_1. */
5368 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5369 pop_it (it);
5371 /* If we're at the end of the buffer, record that we have
5372 processed the overlay strings there already, so that
5373 next_element_from_buffer doesn't try it again. */
5374 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5375 it->overlay_strings_at_end_processed_p = 1;
5377 else
5379 /* There are more overlay strings to process. If
5380 IT->current.overlay_string_index has advanced to a position
5381 where we must load IT->overlay_strings with more strings, do
5382 it. We must load at the IT->overlay_strings_charpos where
5383 IT->n_overlay_strings was originally computed; when invisible
5384 text is present, this might not be IT_CHARPOS (Bug#7016). */
5385 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5387 if (it->current.overlay_string_index && i == 0)
5388 load_overlay_strings (it, it->overlay_strings_charpos);
5390 /* Initialize IT to deliver display elements from the overlay
5391 string. */
5392 it->string = it->overlay_strings[i];
5393 it->multibyte_p = STRING_MULTIBYTE (it->string);
5394 SET_TEXT_POS (it->current.string_pos, 0, 0);
5395 it->method = GET_FROM_STRING;
5396 it->stop_charpos = 0;
5397 it->end_charpos = SCHARS (it->string);
5398 if (it->cmp_it.stop_pos >= 0)
5399 it->cmp_it.stop_pos = 0;
5400 it->prev_stop = 0;
5401 it->base_level_stop = 0;
5403 /* Set up the bidi iterator for this overlay string. */
5404 if (it->bidi_p)
5406 it->bidi_it.string.lstring = it->string;
5407 it->bidi_it.string.s = NULL;
5408 it->bidi_it.string.schars = SCHARS (it->string);
5409 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5410 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5411 it->bidi_it.string.unibyte = !it->multibyte_p;
5412 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5416 CHECK_IT (it);
5420 /* Compare two overlay_entry structures E1 and E2. Used as a
5421 comparison function for qsort in load_overlay_strings. Overlay
5422 strings for the same position are sorted so that
5424 1. All after-strings come in front of before-strings, except
5425 when they come from the same overlay.
5427 2. Within after-strings, strings are sorted so that overlay strings
5428 from overlays with higher priorities come first.
5430 2. Within before-strings, strings are sorted so that overlay
5431 strings from overlays with higher priorities come last.
5433 Value is analogous to strcmp. */
5436 static int
5437 compare_overlay_entries (const void *e1, const void *e2)
5439 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5440 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5441 int result;
5443 if (entry1->after_string_p != entry2->after_string_p)
5445 /* Let after-strings appear in front of before-strings if
5446 they come from different overlays. */
5447 if (EQ (entry1->overlay, entry2->overlay))
5448 result = entry1->after_string_p ? 1 : -1;
5449 else
5450 result = entry1->after_string_p ? -1 : 1;
5452 else if (entry1->priority != entry2->priority)
5454 if (entry1->after_string_p)
5455 /* After-strings sorted in order of decreasing priority. */
5456 result = entry2->priority < entry1->priority ? -1 : 1;
5457 else
5458 /* Before-strings sorted in order of increasing priority. */
5459 result = entry1->priority < entry2->priority ? -1 : 1;
5461 else
5462 result = 0;
5464 return result;
5468 /* Load the vector IT->overlay_strings with overlay strings from IT's
5469 current buffer position, or from CHARPOS if that is > 0. Set
5470 IT->n_overlays to the total number of overlay strings found.
5472 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5473 a time. On entry into load_overlay_strings,
5474 IT->current.overlay_string_index gives the number of overlay
5475 strings that have already been loaded by previous calls to this
5476 function.
5478 IT->add_overlay_start contains an additional overlay start
5479 position to consider for taking overlay strings from, if non-zero.
5480 This position comes into play when the overlay has an `invisible'
5481 property, and both before and after-strings. When we've skipped to
5482 the end of the overlay, because of its `invisible' property, we
5483 nevertheless want its before-string to appear.
5484 IT->add_overlay_start will contain the overlay start position
5485 in this case.
5487 Overlay strings are sorted so that after-string strings come in
5488 front of before-string strings. Within before and after-strings,
5489 strings are sorted by overlay priority. See also function
5490 compare_overlay_entries. */
5492 static void
5493 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5495 Lisp_Object overlay, window, str, invisible;
5496 struct Lisp_Overlay *ov;
5497 ptrdiff_t start, end;
5498 ptrdiff_t size = 20;
5499 ptrdiff_t n = 0, i, j;
5500 int invis_p;
5501 struct overlay_entry *entries = alloca (size * sizeof *entries);
5502 USE_SAFE_ALLOCA;
5504 if (charpos <= 0)
5505 charpos = IT_CHARPOS (*it);
5507 /* Append the overlay string STRING of overlay OVERLAY to vector
5508 `entries' which has size `size' and currently contains `n'
5509 elements. AFTER_P non-zero means STRING is an after-string of
5510 OVERLAY. */
5511 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5512 do \
5514 Lisp_Object priority; \
5516 if (n == size) \
5518 struct overlay_entry *old = entries; \
5519 SAFE_NALLOCA (entries, 2, size); \
5520 memcpy (entries, old, size * sizeof *entries); \
5521 size *= 2; \
5524 entries[n].string = (STRING); \
5525 entries[n].overlay = (OVERLAY); \
5526 priority = Foverlay_get ((OVERLAY), Qpriority); \
5527 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5528 entries[n].after_string_p = (AFTER_P); \
5529 ++n; \
5531 while (0)
5533 /* Process overlay before the overlay center. */
5534 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5536 XSETMISC (overlay, ov);
5537 eassert (OVERLAYP (overlay));
5538 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5539 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5541 if (end < charpos)
5542 break;
5544 /* Skip this overlay if it doesn't start or end at IT's current
5545 position. */
5546 if (end != charpos && start != charpos)
5547 continue;
5549 /* Skip this overlay if it doesn't apply to IT->w. */
5550 window = Foverlay_get (overlay, Qwindow);
5551 if (WINDOWP (window) && XWINDOW (window) != it->w)
5552 continue;
5554 /* If the text ``under'' the overlay is invisible, both before-
5555 and after-strings from this overlay are visible; start and
5556 end position are indistinguishable. */
5557 invisible = Foverlay_get (overlay, Qinvisible);
5558 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5560 /* If overlay has a non-empty before-string, record it. */
5561 if ((start == charpos || (end == charpos && invis_p))
5562 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5563 && SCHARS (str))
5564 RECORD_OVERLAY_STRING (overlay, str, 0);
5566 /* If overlay has a non-empty after-string, record it. */
5567 if ((end == charpos || (start == charpos && invis_p))
5568 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5569 && SCHARS (str))
5570 RECORD_OVERLAY_STRING (overlay, str, 1);
5573 /* Process overlays after the overlay center. */
5574 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5576 XSETMISC (overlay, ov);
5577 eassert (OVERLAYP (overlay));
5578 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5579 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5581 if (start > charpos)
5582 break;
5584 /* Skip this overlay if it doesn't start or end at IT's current
5585 position. */
5586 if (end != charpos && start != charpos)
5587 continue;
5589 /* Skip this overlay if it doesn't apply to IT->w. */
5590 window = Foverlay_get (overlay, Qwindow);
5591 if (WINDOWP (window) && XWINDOW (window) != it->w)
5592 continue;
5594 /* If the text ``under'' the overlay is invisible, it has a zero
5595 dimension, and both before- and after-strings apply. */
5596 invisible = Foverlay_get (overlay, Qinvisible);
5597 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5599 /* If overlay has a non-empty before-string, record it. */
5600 if ((start == charpos || (end == charpos && invis_p))
5601 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5602 && SCHARS (str))
5603 RECORD_OVERLAY_STRING (overlay, str, 0);
5605 /* If overlay has a non-empty after-string, record it. */
5606 if ((end == charpos || (start == charpos && invis_p))
5607 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5608 && SCHARS (str))
5609 RECORD_OVERLAY_STRING (overlay, str, 1);
5612 #undef RECORD_OVERLAY_STRING
5614 /* Sort entries. */
5615 if (n > 1)
5616 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5618 /* Record number of overlay strings, and where we computed it. */
5619 it->n_overlay_strings = n;
5620 it->overlay_strings_charpos = charpos;
5622 /* IT->current.overlay_string_index is the number of overlay strings
5623 that have already been consumed by IT. Copy some of the
5624 remaining overlay strings to IT->overlay_strings. */
5625 i = 0;
5626 j = it->current.overlay_string_index;
5627 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5629 it->overlay_strings[i] = entries[j].string;
5630 it->string_overlays[i++] = entries[j++].overlay;
5633 CHECK_IT (it);
5634 SAFE_FREE ();
5638 /* Get the first chunk of overlay strings at IT's current buffer
5639 position, or at CHARPOS if that is > 0. Value is non-zero if at
5640 least one overlay string was found. */
5642 static int
5643 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5645 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5646 process. This fills IT->overlay_strings with strings, and sets
5647 IT->n_overlay_strings to the total number of strings to process.
5648 IT->pos.overlay_string_index has to be set temporarily to zero
5649 because load_overlay_strings needs this; it must be set to -1
5650 when no overlay strings are found because a zero value would
5651 indicate a position in the first overlay string. */
5652 it->current.overlay_string_index = 0;
5653 load_overlay_strings (it, charpos);
5655 /* If we found overlay strings, set up IT to deliver display
5656 elements from the first one. Otherwise set up IT to deliver
5657 from current_buffer. */
5658 if (it->n_overlay_strings)
5660 /* Make sure we know settings in current_buffer, so that we can
5661 restore meaningful values when we're done with the overlay
5662 strings. */
5663 if (compute_stop_p)
5664 compute_stop_pos (it);
5665 eassert (it->face_id >= 0);
5667 /* Save IT's settings. They are restored after all overlay
5668 strings have been processed. */
5669 eassert (!compute_stop_p || it->sp == 0);
5671 /* When called from handle_stop, there might be an empty display
5672 string loaded. In that case, don't bother saving it. But
5673 don't use this optimization with the bidi iterator, since we
5674 need the corresponding pop_it call to resync the bidi
5675 iterator's position with IT's position, after we are done
5676 with the overlay strings. (The corresponding call to pop_it
5677 in case of an empty display string is in
5678 next_overlay_string.) */
5679 if (!(!it->bidi_p
5680 && STRINGP (it->string) && !SCHARS (it->string)))
5681 push_it (it, NULL);
5683 /* Set up IT to deliver display elements from the first overlay
5684 string. */
5685 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5686 it->string = it->overlay_strings[0];
5687 it->from_overlay = Qnil;
5688 it->stop_charpos = 0;
5689 eassert (STRINGP (it->string));
5690 it->end_charpos = SCHARS (it->string);
5691 it->prev_stop = 0;
5692 it->base_level_stop = 0;
5693 it->multibyte_p = STRING_MULTIBYTE (it->string);
5694 it->method = GET_FROM_STRING;
5695 it->from_disp_prop_p = 0;
5697 /* Force paragraph direction to be that of the parent
5698 buffer. */
5699 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5700 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5701 else
5702 it->paragraph_embedding = L2R;
5704 /* Set up the bidi iterator for this overlay string. */
5705 if (it->bidi_p)
5707 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5709 it->bidi_it.string.lstring = it->string;
5710 it->bidi_it.string.s = NULL;
5711 it->bidi_it.string.schars = SCHARS (it->string);
5712 it->bidi_it.string.bufpos = pos;
5713 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5714 it->bidi_it.string.unibyte = !it->multibyte_p;
5715 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5717 return 1;
5720 it->current.overlay_string_index = -1;
5721 return 0;
5724 static int
5725 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5727 it->string = Qnil;
5728 it->method = GET_FROM_BUFFER;
5730 (void) get_overlay_strings_1 (it, charpos, 1);
5732 CHECK_IT (it);
5734 /* Value is non-zero if we found at least one overlay string. */
5735 return STRINGP (it->string);
5740 /***********************************************************************
5741 Saving and restoring state
5742 ***********************************************************************/
5744 /* Save current settings of IT on IT->stack. Called, for example,
5745 before setting up IT for an overlay string, to be able to restore
5746 IT's settings to what they were after the overlay string has been
5747 processed. If POSITION is non-NULL, it is the position to save on
5748 the stack instead of IT->position. */
5750 static void
5751 push_it (struct it *it, struct text_pos *position)
5753 struct iterator_stack_entry *p;
5755 eassert (it->sp < IT_STACK_SIZE);
5756 p = it->stack + it->sp;
5758 p->stop_charpos = it->stop_charpos;
5759 p->prev_stop = it->prev_stop;
5760 p->base_level_stop = it->base_level_stop;
5761 p->cmp_it = it->cmp_it;
5762 eassert (it->face_id >= 0);
5763 p->face_id = it->face_id;
5764 p->string = it->string;
5765 p->method = it->method;
5766 p->from_overlay = it->from_overlay;
5767 switch (p->method)
5769 case GET_FROM_IMAGE:
5770 p->u.image.object = it->object;
5771 p->u.image.image_id = it->image_id;
5772 p->u.image.slice = it->slice;
5773 break;
5774 case GET_FROM_STRETCH:
5775 p->u.stretch.object = it->object;
5776 break;
5778 p->position = position ? *position : it->position;
5779 p->current = it->current;
5780 p->end_charpos = it->end_charpos;
5781 p->string_nchars = it->string_nchars;
5782 p->area = it->area;
5783 p->multibyte_p = it->multibyte_p;
5784 p->avoid_cursor_p = it->avoid_cursor_p;
5785 p->space_width = it->space_width;
5786 p->font_height = it->font_height;
5787 p->voffset = it->voffset;
5788 p->string_from_display_prop_p = it->string_from_display_prop_p;
5789 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5790 p->display_ellipsis_p = 0;
5791 p->line_wrap = it->line_wrap;
5792 p->bidi_p = it->bidi_p;
5793 p->paragraph_embedding = it->paragraph_embedding;
5794 p->from_disp_prop_p = it->from_disp_prop_p;
5795 ++it->sp;
5797 /* Save the state of the bidi iterator as well. */
5798 if (it->bidi_p)
5799 bidi_push_it (&it->bidi_it);
5802 static void
5803 iterate_out_of_display_property (struct it *it)
5805 int buffer_p = !STRINGP (it->string);
5806 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5807 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5809 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5811 /* Maybe initialize paragraph direction. If we are at the beginning
5812 of a new paragraph, next_element_from_buffer may not have a
5813 chance to do that. */
5814 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5815 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5816 /* prev_stop can be zero, so check against BEGV as well. */
5817 while (it->bidi_it.charpos >= bob
5818 && it->prev_stop <= it->bidi_it.charpos
5819 && it->bidi_it.charpos < CHARPOS (it->position)
5820 && it->bidi_it.charpos < eob)
5821 bidi_move_to_visually_next (&it->bidi_it);
5822 /* Record the stop_pos we just crossed, for when we cross it
5823 back, maybe. */
5824 if (it->bidi_it.charpos > CHARPOS (it->position))
5825 it->prev_stop = CHARPOS (it->position);
5826 /* If we ended up not where pop_it put us, resync IT's
5827 positional members with the bidi iterator. */
5828 if (it->bidi_it.charpos != CHARPOS (it->position))
5829 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5830 if (buffer_p)
5831 it->current.pos = it->position;
5832 else
5833 it->current.string_pos = it->position;
5836 /* Restore IT's settings from IT->stack. Called, for example, when no
5837 more overlay strings must be processed, and we return to delivering
5838 display elements from a buffer, or when the end of a string from a
5839 `display' property is reached and we return to delivering display
5840 elements from an overlay string, or from a buffer. */
5842 static void
5843 pop_it (struct it *it)
5845 struct iterator_stack_entry *p;
5846 int from_display_prop = it->from_disp_prop_p;
5848 eassert (it->sp > 0);
5849 --it->sp;
5850 p = it->stack + it->sp;
5851 it->stop_charpos = p->stop_charpos;
5852 it->prev_stop = p->prev_stop;
5853 it->base_level_stop = p->base_level_stop;
5854 it->cmp_it = p->cmp_it;
5855 it->face_id = p->face_id;
5856 it->current = p->current;
5857 it->position = p->position;
5858 it->string = p->string;
5859 it->from_overlay = p->from_overlay;
5860 if (NILP (it->string))
5861 SET_TEXT_POS (it->current.string_pos, -1, -1);
5862 it->method = p->method;
5863 switch (it->method)
5865 case GET_FROM_IMAGE:
5866 it->image_id = p->u.image.image_id;
5867 it->object = p->u.image.object;
5868 it->slice = p->u.image.slice;
5869 break;
5870 case GET_FROM_STRETCH:
5871 it->object = p->u.stretch.object;
5872 break;
5873 case GET_FROM_BUFFER:
5874 it->object = it->w->contents;
5875 break;
5876 case GET_FROM_STRING:
5877 it->object = it->string;
5878 break;
5879 case GET_FROM_DISPLAY_VECTOR:
5880 if (it->s)
5881 it->method = GET_FROM_C_STRING;
5882 else if (STRINGP (it->string))
5883 it->method = GET_FROM_STRING;
5884 else
5886 it->method = GET_FROM_BUFFER;
5887 it->object = it->w->contents;
5890 it->end_charpos = p->end_charpos;
5891 it->string_nchars = p->string_nchars;
5892 it->area = p->area;
5893 it->multibyte_p = p->multibyte_p;
5894 it->avoid_cursor_p = p->avoid_cursor_p;
5895 it->space_width = p->space_width;
5896 it->font_height = p->font_height;
5897 it->voffset = p->voffset;
5898 it->string_from_display_prop_p = p->string_from_display_prop_p;
5899 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5900 it->line_wrap = p->line_wrap;
5901 it->bidi_p = p->bidi_p;
5902 it->paragraph_embedding = p->paragraph_embedding;
5903 it->from_disp_prop_p = p->from_disp_prop_p;
5904 if (it->bidi_p)
5906 bidi_pop_it (&it->bidi_it);
5907 /* Bidi-iterate until we get out of the portion of text, if any,
5908 covered by a `display' text property or by an overlay with
5909 `display' property. (We cannot just jump there, because the
5910 internal coherency of the bidi iterator state can not be
5911 preserved across such jumps.) We also must determine the
5912 paragraph base direction if the overlay we just processed is
5913 at the beginning of a new paragraph. */
5914 if (from_display_prop
5915 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5916 iterate_out_of_display_property (it);
5918 eassert ((BUFFERP (it->object)
5919 && IT_CHARPOS (*it) == it->bidi_it.charpos
5920 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5921 || (STRINGP (it->object)
5922 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5923 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5924 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5930 /***********************************************************************
5931 Moving over lines
5932 ***********************************************************************/
5934 /* Set IT's current position to the previous line start. */
5936 static void
5937 back_to_previous_line_start (struct it *it)
5939 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
5941 DEC_BOTH (cp, bp);
5942 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
5946 /* Move IT to the next line start.
5948 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5949 we skipped over part of the text (as opposed to moving the iterator
5950 continuously over the text). Otherwise, don't change the value
5951 of *SKIPPED_P.
5953 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5954 iterator on the newline, if it was found.
5956 Newlines may come from buffer text, overlay strings, or strings
5957 displayed via the `display' property. That's the reason we can't
5958 simply use find_newline_no_quit.
5960 Note that this function may not skip over invisible text that is so
5961 because of text properties and immediately follows a newline. If
5962 it would, function reseat_at_next_visible_line_start, when called
5963 from set_iterator_to_next, would effectively make invisible
5964 characters following a newline part of the wrong glyph row, which
5965 leads to wrong cursor motion. */
5967 static int
5968 forward_to_next_line_start (struct it *it, int *skipped_p,
5969 struct bidi_it *bidi_it_prev)
5971 ptrdiff_t old_selective;
5972 int newline_found_p, n;
5973 const int MAX_NEWLINE_DISTANCE = 500;
5975 /* If already on a newline, just consume it to avoid unintended
5976 skipping over invisible text below. */
5977 if (it->what == IT_CHARACTER
5978 && it->c == '\n'
5979 && CHARPOS (it->position) == IT_CHARPOS (*it))
5981 if (it->bidi_p && bidi_it_prev)
5982 *bidi_it_prev = it->bidi_it;
5983 set_iterator_to_next (it, 0);
5984 it->c = 0;
5985 return 1;
5988 /* Don't handle selective display in the following. It's (a)
5989 unnecessary because it's done by the caller, and (b) leads to an
5990 infinite recursion because next_element_from_ellipsis indirectly
5991 calls this function. */
5992 old_selective = it->selective;
5993 it->selective = 0;
5995 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5996 from buffer text. */
5997 for (n = newline_found_p = 0;
5998 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5999 n += STRINGP (it->string) ? 0 : 1)
6001 if (!get_next_display_element (it))
6002 return 0;
6003 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6004 if (newline_found_p && it->bidi_p && bidi_it_prev)
6005 *bidi_it_prev = it->bidi_it;
6006 set_iterator_to_next (it, 0);
6009 /* If we didn't find a newline near enough, see if we can use a
6010 short-cut. */
6011 if (!newline_found_p)
6013 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6014 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6015 1, &bytepos);
6016 Lisp_Object pos;
6018 eassert (!STRINGP (it->string));
6020 /* If there isn't any `display' property in sight, and no
6021 overlays, we can just use the position of the newline in
6022 buffer text. */
6023 if (it->stop_charpos >= limit
6024 || ((pos = Fnext_single_property_change (make_number (start),
6025 Qdisplay, Qnil,
6026 make_number (limit)),
6027 NILP (pos))
6028 && next_overlay_change (start) == ZV))
6030 if (!it->bidi_p)
6032 IT_CHARPOS (*it) = limit;
6033 IT_BYTEPOS (*it) = bytepos;
6035 else
6037 struct bidi_it bprev;
6039 /* Help bidi.c avoid expensive searches for display
6040 properties and overlays, by telling it that there are
6041 none up to `limit'. */
6042 if (it->bidi_it.disp_pos < limit)
6044 it->bidi_it.disp_pos = limit;
6045 it->bidi_it.disp_prop = 0;
6047 do {
6048 bprev = it->bidi_it;
6049 bidi_move_to_visually_next (&it->bidi_it);
6050 } while (it->bidi_it.charpos != limit);
6051 IT_CHARPOS (*it) = limit;
6052 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6053 if (bidi_it_prev)
6054 *bidi_it_prev = bprev;
6056 *skipped_p = newline_found_p = 1;
6058 else
6060 while (get_next_display_element (it)
6061 && !newline_found_p)
6063 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6064 if (newline_found_p && it->bidi_p && bidi_it_prev)
6065 *bidi_it_prev = it->bidi_it;
6066 set_iterator_to_next (it, 0);
6071 it->selective = old_selective;
6072 return newline_found_p;
6076 /* Set IT's current position to the previous visible line start. Skip
6077 invisible text that is so either due to text properties or due to
6078 selective display. Caution: this does not change IT->current_x and
6079 IT->hpos. */
6081 static void
6082 back_to_previous_visible_line_start (struct it *it)
6084 while (IT_CHARPOS (*it) > BEGV)
6086 back_to_previous_line_start (it);
6088 if (IT_CHARPOS (*it) <= BEGV)
6089 break;
6091 /* If selective > 0, then lines indented more than its value are
6092 invisible. */
6093 if (it->selective > 0
6094 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6095 it->selective))
6096 continue;
6098 /* Check the newline before point for invisibility. */
6100 Lisp_Object prop;
6101 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6102 Qinvisible, it->window);
6103 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6104 continue;
6107 if (IT_CHARPOS (*it) <= BEGV)
6108 break;
6111 struct it it2;
6112 void *it2data = NULL;
6113 ptrdiff_t pos;
6114 ptrdiff_t beg, end;
6115 Lisp_Object val, overlay;
6117 SAVE_IT (it2, *it, it2data);
6119 /* If newline is part of a composition, continue from start of composition */
6120 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6121 && beg < IT_CHARPOS (*it))
6122 goto replaced;
6124 /* If newline is replaced by a display property, find start of overlay
6125 or interval and continue search from that point. */
6126 pos = --IT_CHARPOS (it2);
6127 --IT_BYTEPOS (it2);
6128 it2.sp = 0;
6129 bidi_unshelve_cache (NULL, 0);
6130 it2.string_from_display_prop_p = 0;
6131 it2.from_disp_prop_p = 0;
6132 if (handle_display_prop (&it2) == HANDLED_RETURN
6133 && !NILP (val = get_char_property_and_overlay
6134 (make_number (pos), Qdisplay, Qnil, &overlay))
6135 && (OVERLAYP (overlay)
6136 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6137 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6139 RESTORE_IT (it, it, it2data);
6140 goto replaced;
6143 /* Newline is not replaced by anything -- so we are done. */
6144 RESTORE_IT (it, it, it2data);
6145 break;
6147 replaced:
6148 if (beg < BEGV)
6149 beg = BEGV;
6150 IT_CHARPOS (*it) = beg;
6151 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6155 it->continuation_lines_width = 0;
6157 eassert (IT_CHARPOS (*it) >= BEGV);
6158 eassert (IT_CHARPOS (*it) == BEGV
6159 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6160 CHECK_IT (it);
6164 /* Reseat iterator IT at the previous visible line start. Skip
6165 invisible text that is so either due to text properties or due to
6166 selective display. At the end, update IT's overlay information,
6167 face information etc. */
6169 void
6170 reseat_at_previous_visible_line_start (struct it *it)
6172 back_to_previous_visible_line_start (it);
6173 reseat (it, it->current.pos, 1);
6174 CHECK_IT (it);
6178 /* Reseat iterator IT on the next visible line start in the current
6179 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6180 preceding the line start. Skip over invisible text that is so
6181 because of selective display. Compute faces, overlays etc at the
6182 new position. Note that this function does not skip over text that
6183 is invisible because of text properties. */
6185 static void
6186 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6188 int newline_found_p, skipped_p = 0;
6189 struct bidi_it bidi_it_prev;
6191 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6193 /* Skip over lines that are invisible because they are indented
6194 more than the value of IT->selective. */
6195 if (it->selective > 0)
6196 while (IT_CHARPOS (*it) < ZV
6197 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6198 it->selective))
6200 eassert (IT_BYTEPOS (*it) == BEGV
6201 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6202 newline_found_p =
6203 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6206 /* Position on the newline if that's what's requested. */
6207 if (on_newline_p && newline_found_p)
6209 if (STRINGP (it->string))
6211 if (IT_STRING_CHARPOS (*it) > 0)
6213 if (!it->bidi_p)
6215 --IT_STRING_CHARPOS (*it);
6216 --IT_STRING_BYTEPOS (*it);
6218 else
6220 /* We need to restore the bidi iterator to the state
6221 it had on the newline, and resync the IT's
6222 position with that. */
6223 it->bidi_it = bidi_it_prev;
6224 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6225 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6229 else if (IT_CHARPOS (*it) > BEGV)
6231 if (!it->bidi_p)
6233 --IT_CHARPOS (*it);
6234 --IT_BYTEPOS (*it);
6236 else
6238 /* We need to restore the bidi iterator to the state it
6239 had on the newline and resync IT with that. */
6240 it->bidi_it = bidi_it_prev;
6241 IT_CHARPOS (*it) = it->bidi_it.charpos;
6242 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6244 reseat (it, it->current.pos, 0);
6247 else if (skipped_p)
6248 reseat (it, it->current.pos, 0);
6250 CHECK_IT (it);
6255 /***********************************************************************
6256 Changing an iterator's position
6257 ***********************************************************************/
6259 /* Change IT's current position to POS in current_buffer. If FORCE_P
6260 is non-zero, always check for text properties at the new position.
6261 Otherwise, text properties are only looked up if POS >=
6262 IT->check_charpos of a property. */
6264 static void
6265 reseat (struct it *it, struct text_pos pos, int force_p)
6267 ptrdiff_t original_pos = IT_CHARPOS (*it);
6269 reseat_1 (it, pos, 0);
6271 /* Determine where to check text properties. Avoid doing it
6272 where possible because text property lookup is very expensive. */
6273 if (force_p
6274 || CHARPOS (pos) > it->stop_charpos
6275 || CHARPOS (pos) < original_pos)
6277 if (it->bidi_p)
6279 /* For bidi iteration, we need to prime prev_stop and
6280 base_level_stop with our best estimations. */
6281 /* Implementation note: Of course, POS is not necessarily a
6282 stop position, so assigning prev_pos to it is a lie; we
6283 should have called compute_stop_backwards. However, if
6284 the current buffer does not include any R2L characters,
6285 that call would be a waste of cycles, because the
6286 iterator will never move back, and thus never cross this
6287 "fake" stop position. So we delay that backward search
6288 until the time we really need it, in next_element_from_buffer. */
6289 if (CHARPOS (pos) != it->prev_stop)
6290 it->prev_stop = CHARPOS (pos);
6291 if (CHARPOS (pos) < it->base_level_stop)
6292 it->base_level_stop = 0; /* meaning it's unknown */
6293 handle_stop (it);
6295 else
6297 handle_stop (it);
6298 it->prev_stop = it->base_level_stop = 0;
6303 CHECK_IT (it);
6307 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6308 IT->stop_pos to POS, also. */
6310 static void
6311 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6313 /* Don't call this function when scanning a C string. */
6314 eassert (it->s == NULL);
6316 /* POS must be a reasonable value. */
6317 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6319 it->current.pos = it->position = pos;
6320 it->end_charpos = ZV;
6321 it->dpvec = NULL;
6322 it->current.dpvec_index = -1;
6323 it->current.overlay_string_index = -1;
6324 IT_STRING_CHARPOS (*it) = -1;
6325 IT_STRING_BYTEPOS (*it) = -1;
6326 it->string = Qnil;
6327 it->method = GET_FROM_BUFFER;
6328 it->object = it->w->contents;
6329 it->area = TEXT_AREA;
6330 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6331 it->sp = 0;
6332 it->string_from_display_prop_p = 0;
6333 it->string_from_prefix_prop_p = 0;
6335 it->from_disp_prop_p = 0;
6336 it->face_before_selective_p = 0;
6337 if (it->bidi_p)
6339 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6340 &it->bidi_it);
6341 bidi_unshelve_cache (NULL, 0);
6342 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6343 it->bidi_it.string.s = NULL;
6344 it->bidi_it.string.lstring = Qnil;
6345 it->bidi_it.string.bufpos = 0;
6346 it->bidi_it.string.unibyte = 0;
6349 if (set_stop_p)
6351 it->stop_charpos = CHARPOS (pos);
6352 it->base_level_stop = CHARPOS (pos);
6354 /* This make the information stored in it->cmp_it invalidate. */
6355 it->cmp_it.id = -1;
6359 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6360 If S is non-null, it is a C string to iterate over. Otherwise,
6361 STRING gives a Lisp string to iterate over.
6363 If PRECISION > 0, don't return more then PRECISION number of
6364 characters from the string.
6366 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6367 characters have been returned. FIELD_WIDTH < 0 means an infinite
6368 field width.
6370 MULTIBYTE = 0 means disable processing of multibyte characters,
6371 MULTIBYTE > 0 means enable it,
6372 MULTIBYTE < 0 means use IT->multibyte_p.
6374 IT must be initialized via a prior call to init_iterator before
6375 calling this function. */
6377 static void
6378 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6379 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6380 int multibyte)
6382 /* No region in strings. */
6383 it->region_beg_charpos = it->region_end_charpos = -1;
6385 /* No text property checks performed by default, but see below. */
6386 it->stop_charpos = -1;
6388 /* Set iterator position and end position. */
6389 memset (&it->current, 0, sizeof it->current);
6390 it->current.overlay_string_index = -1;
6391 it->current.dpvec_index = -1;
6392 eassert (charpos >= 0);
6394 /* If STRING is specified, use its multibyteness, otherwise use the
6395 setting of MULTIBYTE, if specified. */
6396 if (multibyte >= 0)
6397 it->multibyte_p = multibyte > 0;
6399 /* Bidirectional reordering of strings is controlled by the default
6400 value of bidi-display-reordering. Don't try to reorder while
6401 loading loadup.el, as the necessary character property tables are
6402 not yet available. */
6403 it->bidi_p =
6404 NILP (Vpurify_flag)
6405 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6407 if (s == NULL)
6409 eassert (STRINGP (string));
6410 it->string = string;
6411 it->s = NULL;
6412 it->end_charpos = it->string_nchars = SCHARS (string);
6413 it->method = GET_FROM_STRING;
6414 it->current.string_pos = string_pos (charpos, string);
6416 if (it->bidi_p)
6418 it->bidi_it.string.lstring = string;
6419 it->bidi_it.string.s = NULL;
6420 it->bidi_it.string.schars = it->end_charpos;
6421 it->bidi_it.string.bufpos = 0;
6422 it->bidi_it.string.from_disp_str = 0;
6423 it->bidi_it.string.unibyte = !it->multibyte_p;
6424 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6425 FRAME_WINDOW_P (it->f), &it->bidi_it);
6428 else
6430 it->s = (const unsigned char *) s;
6431 it->string = Qnil;
6433 /* Note that we use IT->current.pos, not it->current.string_pos,
6434 for displaying C strings. */
6435 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6436 if (it->multibyte_p)
6438 it->current.pos = c_string_pos (charpos, s, 1);
6439 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6441 else
6443 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6444 it->end_charpos = it->string_nchars = strlen (s);
6447 if (it->bidi_p)
6449 it->bidi_it.string.lstring = Qnil;
6450 it->bidi_it.string.s = (const unsigned char *) s;
6451 it->bidi_it.string.schars = it->end_charpos;
6452 it->bidi_it.string.bufpos = 0;
6453 it->bidi_it.string.from_disp_str = 0;
6454 it->bidi_it.string.unibyte = !it->multibyte_p;
6455 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6456 &it->bidi_it);
6458 it->method = GET_FROM_C_STRING;
6461 /* PRECISION > 0 means don't return more than PRECISION characters
6462 from the string. */
6463 if (precision > 0 && it->end_charpos - charpos > precision)
6465 it->end_charpos = it->string_nchars = charpos + precision;
6466 if (it->bidi_p)
6467 it->bidi_it.string.schars = it->end_charpos;
6470 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6471 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6472 FIELD_WIDTH < 0 means infinite field width. This is useful for
6473 padding with `-' at the end of a mode line. */
6474 if (field_width < 0)
6475 field_width = INFINITY;
6476 /* Implementation note: We deliberately don't enlarge
6477 it->bidi_it.string.schars here to fit it->end_charpos, because
6478 the bidi iterator cannot produce characters out of thin air. */
6479 if (field_width > it->end_charpos - charpos)
6480 it->end_charpos = charpos + field_width;
6482 /* Use the standard display table for displaying strings. */
6483 if (DISP_TABLE_P (Vstandard_display_table))
6484 it->dp = XCHAR_TABLE (Vstandard_display_table);
6486 it->stop_charpos = charpos;
6487 it->prev_stop = charpos;
6488 it->base_level_stop = 0;
6489 if (it->bidi_p)
6491 it->bidi_it.first_elt = 1;
6492 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6493 it->bidi_it.disp_pos = -1;
6495 if (s == NULL && it->multibyte_p)
6497 ptrdiff_t endpos = SCHARS (it->string);
6498 if (endpos > it->end_charpos)
6499 endpos = it->end_charpos;
6500 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6501 it->string);
6503 CHECK_IT (it);
6508 /***********************************************************************
6509 Iteration
6510 ***********************************************************************/
6512 /* Map enum it_method value to corresponding next_element_from_* function. */
6514 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6516 next_element_from_buffer,
6517 next_element_from_display_vector,
6518 next_element_from_string,
6519 next_element_from_c_string,
6520 next_element_from_image,
6521 next_element_from_stretch
6524 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6527 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6528 (possibly with the following characters). */
6530 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6531 ((IT)->cmp_it.id >= 0 \
6532 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6533 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6534 END_CHARPOS, (IT)->w, \
6535 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6536 (IT)->string)))
6539 /* Lookup the char-table Vglyphless_char_display for character C (-1
6540 if we want information for no-font case), and return the display
6541 method symbol. By side-effect, update it->what and
6542 it->glyphless_method. This function is called from
6543 get_next_display_element for each character element, and from
6544 x_produce_glyphs when no suitable font was found. */
6546 Lisp_Object
6547 lookup_glyphless_char_display (int c, struct it *it)
6549 Lisp_Object glyphless_method = Qnil;
6551 if (CHAR_TABLE_P (Vglyphless_char_display)
6552 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6554 if (c >= 0)
6556 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6557 if (CONSP (glyphless_method))
6558 glyphless_method = FRAME_WINDOW_P (it->f)
6559 ? XCAR (glyphless_method)
6560 : XCDR (glyphless_method);
6562 else
6563 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6566 retry:
6567 if (NILP (glyphless_method))
6569 if (c >= 0)
6570 /* The default is to display the character by a proper font. */
6571 return Qnil;
6572 /* The default for the no-font case is to display an empty box. */
6573 glyphless_method = Qempty_box;
6575 if (EQ (glyphless_method, Qzero_width))
6577 if (c >= 0)
6578 return glyphless_method;
6579 /* This method can't be used for the no-font case. */
6580 glyphless_method = Qempty_box;
6582 if (EQ (glyphless_method, Qthin_space))
6583 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6584 else if (EQ (glyphless_method, Qempty_box))
6585 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6586 else if (EQ (glyphless_method, Qhex_code))
6587 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6588 else if (STRINGP (glyphless_method))
6589 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6590 else
6592 /* Invalid value. We use the default method. */
6593 glyphless_method = Qnil;
6594 goto retry;
6596 it->what = IT_GLYPHLESS;
6597 return glyphless_method;
6600 /* Load IT's display element fields with information about the next
6601 display element from the current position of IT. Value is zero if
6602 end of buffer (or C string) is reached. */
6604 static struct frame *last_escape_glyph_frame = NULL;
6605 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6606 static int last_escape_glyph_merged_face_id = 0;
6608 struct frame *last_glyphless_glyph_frame = NULL;
6609 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6610 int last_glyphless_glyph_merged_face_id = 0;
6612 static int
6613 get_next_display_element (struct it *it)
6615 /* Non-zero means that we found a display element. Zero means that
6616 we hit the end of what we iterate over. Performance note: the
6617 function pointer `method' used here turns out to be faster than
6618 using a sequence of if-statements. */
6619 int success_p;
6621 get_next:
6622 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6624 if (it->what == IT_CHARACTER)
6626 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6627 and only if (a) the resolved directionality of that character
6628 is R..." */
6629 /* FIXME: Do we need an exception for characters from display
6630 tables? */
6631 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6632 it->c = bidi_mirror_char (it->c);
6633 /* Map via display table or translate control characters.
6634 IT->c, IT->len etc. have been set to the next character by
6635 the function call above. If we have a display table, and it
6636 contains an entry for IT->c, translate it. Don't do this if
6637 IT->c itself comes from a display table, otherwise we could
6638 end up in an infinite recursion. (An alternative could be to
6639 count the recursion depth of this function and signal an
6640 error when a certain maximum depth is reached.) Is it worth
6641 it? */
6642 if (success_p && it->dpvec == NULL)
6644 Lisp_Object dv;
6645 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6646 int nonascii_space_p = 0;
6647 int nonascii_hyphen_p = 0;
6648 int c = it->c; /* This is the character to display. */
6650 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6652 eassert (SINGLE_BYTE_CHAR_P (c));
6653 if (unibyte_display_via_language_environment)
6655 c = DECODE_CHAR (unibyte, c);
6656 if (c < 0)
6657 c = BYTE8_TO_CHAR (it->c);
6659 else
6660 c = BYTE8_TO_CHAR (it->c);
6663 if (it->dp
6664 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6665 VECTORP (dv)))
6667 struct Lisp_Vector *v = XVECTOR (dv);
6669 /* Return the first character from the display table
6670 entry, if not empty. If empty, don't display the
6671 current character. */
6672 if (v->header.size)
6674 it->dpvec_char_len = it->len;
6675 it->dpvec = v->contents;
6676 it->dpend = v->contents + v->header.size;
6677 it->current.dpvec_index = 0;
6678 it->dpvec_face_id = -1;
6679 it->saved_face_id = it->face_id;
6680 it->method = GET_FROM_DISPLAY_VECTOR;
6681 it->ellipsis_p = 0;
6683 else
6685 set_iterator_to_next (it, 0);
6687 goto get_next;
6690 if (! NILP (lookup_glyphless_char_display (c, it)))
6692 if (it->what == IT_GLYPHLESS)
6693 goto done;
6694 /* Don't display this character. */
6695 set_iterator_to_next (it, 0);
6696 goto get_next;
6699 /* If `nobreak-char-display' is non-nil, we display
6700 non-ASCII spaces and hyphens specially. */
6701 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6703 if (c == 0xA0)
6704 nonascii_space_p = 1;
6705 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6706 nonascii_hyphen_p = 1;
6709 /* Translate control characters into `\003' or `^C' form.
6710 Control characters coming from a display table entry are
6711 currently not translated because we use IT->dpvec to hold
6712 the translation. This could easily be changed but I
6713 don't believe that it is worth doing.
6715 The characters handled by `nobreak-char-display' must be
6716 translated too.
6718 Non-printable characters and raw-byte characters are also
6719 translated to octal form. */
6720 if (((c < ' ' || c == 127) /* ASCII control chars */
6721 ? (it->area != TEXT_AREA
6722 /* In mode line, treat \n, \t like other crl chars. */
6723 || (c != '\t'
6724 && it->glyph_row
6725 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6726 || (c != '\n' && c != '\t'))
6727 : (nonascii_space_p
6728 || nonascii_hyphen_p
6729 || CHAR_BYTE8_P (c)
6730 || ! CHAR_PRINTABLE_P (c))))
6732 /* C is a control character, non-ASCII space/hyphen,
6733 raw-byte, or a non-printable character which must be
6734 displayed either as '\003' or as `^C' where the '\\'
6735 and '^' can be defined in the display table. Fill
6736 IT->ctl_chars with glyphs for what we have to
6737 display. Then, set IT->dpvec to these glyphs. */
6738 Lisp_Object gc;
6739 int ctl_len;
6740 int face_id;
6741 int lface_id = 0;
6742 int escape_glyph;
6744 /* Handle control characters with ^. */
6746 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6748 int g;
6750 g = '^'; /* default glyph for Control */
6751 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6752 if (it->dp
6753 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6755 g = GLYPH_CODE_CHAR (gc);
6756 lface_id = GLYPH_CODE_FACE (gc);
6758 if (lface_id)
6760 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6762 else if (it->f == last_escape_glyph_frame
6763 && it->face_id == last_escape_glyph_face_id)
6765 face_id = last_escape_glyph_merged_face_id;
6767 else
6769 /* Merge the escape-glyph face into the current face. */
6770 face_id = merge_faces (it->f, Qescape_glyph, 0,
6771 it->face_id);
6772 last_escape_glyph_frame = it->f;
6773 last_escape_glyph_face_id = it->face_id;
6774 last_escape_glyph_merged_face_id = face_id;
6777 XSETINT (it->ctl_chars[0], g);
6778 XSETINT (it->ctl_chars[1], c ^ 0100);
6779 ctl_len = 2;
6780 goto display_control;
6783 /* Handle non-ascii space in the mode where it only gets
6784 highlighting. */
6786 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6788 /* Merge `nobreak-space' into the current face. */
6789 face_id = merge_faces (it->f, Qnobreak_space, 0,
6790 it->face_id);
6791 XSETINT (it->ctl_chars[0], ' ');
6792 ctl_len = 1;
6793 goto display_control;
6796 /* Handle sequences that start with the "escape glyph". */
6798 /* the default escape glyph is \. */
6799 escape_glyph = '\\';
6801 if (it->dp
6802 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6804 escape_glyph = GLYPH_CODE_CHAR (gc);
6805 lface_id = GLYPH_CODE_FACE (gc);
6807 if (lface_id)
6809 /* The display table specified a face.
6810 Merge it into face_id and also into escape_glyph. */
6811 face_id = merge_faces (it->f, Qt, lface_id,
6812 it->face_id);
6814 else if (it->f == last_escape_glyph_frame
6815 && it->face_id == last_escape_glyph_face_id)
6817 face_id = last_escape_glyph_merged_face_id;
6819 else
6821 /* Merge the escape-glyph face into the current face. */
6822 face_id = merge_faces (it->f, Qescape_glyph, 0,
6823 it->face_id);
6824 last_escape_glyph_frame = it->f;
6825 last_escape_glyph_face_id = it->face_id;
6826 last_escape_glyph_merged_face_id = face_id;
6829 /* Draw non-ASCII hyphen with just highlighting: */
6831 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6833 XSETINT (it->ctl_chars[0], '-');
6834 ctl_len = 1;
6835 goto display_control;
6838 /* Draw non-ASCII space/hyphen with escape glyph: */
6840 if (nonascii_space_p || nonascii_hyphen_p)
6842 XSETINT (it->ctl_chars[0], escape_glyph);
6843 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6844 ctl_len = 2;
6845 goto display_control;
6849 char str[10];
6850 int len, i;
6852 if (CHAR_BYTE8_P (c))
6853 /* Display \200 instead of \17777600. */
6854 c = CHAR_TO_BYTE8 (c);
6855 len = sprintf (str, "%03o", c);
6857 XSETINT (it->ctl_chars[0], escape_glyph);
6858 for (i = 0; i < len; i++)
6859 XSETINT (it->ctl_chars[i + 1], str[i]);
6860 ctl_len = len + 1;
6863 display_control:
6864 /* Set up IT->dpvec and return first character from it. */
6865 it->dpvec_char_len = it->len;
6866 it->dpvec = it->ctl_chars;
6867 it->dpend = it->dpvec + ctl_len;
6868 it->current.dpvec_index = 0;
6869 it->dpvec_face_id = face_id;
6870 it->saved_face_id = it->face_id;
6871 it->method = GET_FROM_DISPLAY_VECTOR;
6872 it->ellipsis_p = 0;
6873 goto get_next;
6875 it->char_to_display = c;
6877 else if (success_p)
6879 it->char_to_display = it->c;
6883 /* Adjust face id for a multibyte character. There are no multibyte
6884 character in unibyte text. */
6885 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6886 && it->multibyte_p
6887 && success_p
6888 && FRAME_WINDOW_P (it->f))
6890 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6892 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6894 /* Automatic composition with glyph-string. */
6895 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6897 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6899 else
6901 ptrdiff_t pos = (it->s ? -1
6902 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6903 : IT_CHARPOS (*it));
6904 int c;
6906 if (it->what == IT_CHARACTER)
6907 c = it->char_to_display;
6908 else
6910 struct composition *cmp = composition_table[it->cmp_it.id];
6911 int i;
6913 c = ' ';
6914 for (i = 0; i < cmp->glyph_len; i++)
6915 /* TAB in a composition means display glyphs with
6916 padding space on the left or right. */
6917 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6918 break;
6920 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6924 done:
6925 /* Is this character the last one of a run of characters with
6926 box? If yes, set IT->end_of_box_run_p to 1. */
6927 if (it->face_box_p
6928 && it->s == NULL)
6930 if (it->method == GET_FROM_STRING && it->sp)
6932 int face_id = underlying_face_id (it);
6933 struct face *face = FACE_FROM_ID (it->f, face_id);
6935 if (face)
6937 if (face->box == FACE_NO_BOX)
6939 /* If the box comes from face properties in a
6940 display string, check faces in that string. */
6941 int string_face_id = face_after_it_pos (it);
6942 it->end_of_box_run_p
6943 = (FACE_FROM_ID (it->f, string_face_id)->box
6944 == FACE_NO_BOX);
6946 /* Otherwise, the box comes from the underlying face.
6947 If this is the last string character displayed, check
6948 the next buffer location. */
6949 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6950 && (it->current.overlay_string_index
6951 == it->n_overlay_strings - 1))
6953 ptrdiff_t ignore;
6954 int next_face_id;
6955 struct text_pos pos = it->current.pos;
6956 INC_TEXT_POS (pos, it->multibyte_p);
6958 next_face_id = face_at_buffer_position
6959 (it->w, CHARPOS (pos), it->region_beg_charpos,
6960 it->region_end_charpos, &ignore,
6961 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6962 -1);
6963 it->end_of_box_run_p
6964 = (FACE_FROM_ID (it->f, next_face_id)->box
6965 == FACE_NO_BOX);
6969 else
6971 int face_id = face_after_it_pos (it);
6972 it->end_of_box_run_p
6973 = (face_id != it->face_id
6974 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6977 /* If we reached the end of the object we've been iterating (e.g., a
6978 display string or an overlay string), and there's something on
6979 IT->stack, proceed with what's on the stack. It doesn't make
6980 sense to return zero if there's unprocessed stuff on the stack,
6981 because otherwise that stuff will never be displayed. */
6982 if (!success_p && it->sp > 0)
6984 set_iterator_to_next (it, 0);
6985 success_p = get_next_display_element (it);
6988 /* Value is 0 if end of buffer or string reached. */
6989 return success_p;
6993 /* Move IT to the next display element.
6995 RESEAT_P non-zero means if called on a newline in buffer text,
6996 skip to the next visible line start.
6998 Functions get_next_display_element and set_iterator_to_next are
6999 separate because I find this arrangement easier to handle than a
7000 get_next_display_element function that also increments IT's
7001 position. The way it is we can first look at an iterator's current
7002 display element, decide whether it fits on a line, and if it does,
7003 increment the iterator position. The other way around we probably
7004 would either need a flag indicating whether the iterator has to be
7005 incremented the next time, or we would have to implement a
7006 decrement position function which would not be easy to write. */
7008 void
7009 set_iterator_to_next (struct it *it, int reseat_p)
7011 /* Reset flags indicating start and end of a sequence of characters
7012 with box. Reset them at the start of this function because
7013 moving the iterator to a new position might set them. */
7014 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7016 switch (it->method)
7018 case GET_FROM_BUFFER:
7019 /* The current display element of IT is a character from
7020 current_buffer. Advance in the buffer, and maybe skip over
7021 invisible lines that are so because of selective display. */
7022 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7023 reseat_at_next_visible_line_start (it, 0);
7024 else if (it->cmp_it.id >= 0)
7026 /* We are currently getting glyphs from a composition. */
7027 int i;
7029 if (! it->bidi_p)
7031 IT_CHARPOS (*it) += it->cmp_it.nchars;
7032 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7033 if (it->cmp_it.to < it->cmp_it.nglyphs)
7035 it->cmp_it.from = it->cmp_it.to;
7037 else
7039 it->cmp_it.id = -1;
7040 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7041 IT_BYTEPOS (*it),
7042 it->end_charpos, Qnil);
7045 else if (! it->cmp_it.reversed_p)
7047 /* Composition created while scanning forward. */
7048 /* Update IT's char/byte positions to point to the first
7049 character of the next grapheme cluster, or to the
7050 character visually after the current composition. */
7051 for (i = 0; i < it->cmp_it.nchars; i++)
7052 bidi_move_to_visually_next (&it->bidi_it);
7053 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7054 IT_CHARPOS (*it) = it->bidi_it.charpos;
7056 if (it->cmp_it.to < it->cmp_it.nglyphs)
7058 /* Proceed to the next grapheme cluster. */
7059 it->cmp_it.from = it->cmp_it.to;
7061 else
7063 /* No more grapheme clusters in this composition.
7064 Find the next stop position. */
7065 ptrdiff_t stop = it->end_charpos;
7066 if (it->bidi_it.scan_dir < 0)
7067 /* Now we are scanning backward and don't know
7068 where to stop. */
7069 stop = -1;
7070 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7071 IT_BYTEPOS (*it), stop, Qnil);
7074 else
7076 /* Composition created while scanning backward. */
7077 /* Update IT's char/byte positions to point to the last
7078 character of the previous grapheme cluster, or the
7079 character visually after the current composition. */
7080 for (i = 0; i < it->cmp_it.nchars; i++)
7081 bidi_move_to_visually_next (&it->bidi_it);
7082 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7083 IT_CHARPOS (*it) = it->bidi_it.charpos;
7084 if (it->cmp_it.from > 0)
7086 /* Proceed to the previous grapheme cluster. */
7087 it->cmp_it.to = it->cmp_it.from;
7089 else
7091 /* No more grapheme clusters in this composition.
7092 Find the next stop position. */
7093 ptrdiff_t stop = it->end_charpos;
7094 if (it->bidi_it.scan_dir < 0)
7095 /* Now we are scanning backward and don't know
7096 where to stop. */
7097 stop = -1;
7098 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7099 IT_BYTEPOS (*it), stop, Qnil);
7103 else
7105 eassert (it->len != 0);
7107 if (!it->bidi_p)
7109 IT_BYTEPOS (*it) += it->len;
7110 IT_CHARPOS (*it) += 1;
7112 else
7114 int prev_scan_dir = it->bidi_it.scan_dir;
7115 /* If this is a new paragraph, determine its base
7116 direction (a.k.a. its base embedding level). */
7117 if (it->bidi_it.new_paragraph)
7118 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7119 bidi_move_to_visually_next (&it->bidi_it);
7120 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7121 IT_CHARPOS (*it) = it->bidi_it.charpos;
7122 if (prev_scan_dir != it->bidi_it.scan_dir)
7124 /* As the scan direction was changed, we must
7125 re-compute the stop position for composition. */
7126 ptrdiff_t stop = it->end_charpos;
7127 if (it->bidi_it.scan_dir < 0)
7128 stop = -1;
7129 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7130 IT_BYTEPOS (*it), stop, Qnil);
7133 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7135 break;
7137 case GET_FROM_C_STRING:
7138 /* Current display element of IT is from a C string. */
7139 if (!it->bidi_p
7140 /* If the string position is beyond string's end, it means
7141 next_element_from_c_string is padding the string with
7142 blanks, in which case we bypass the bidi iterator,
7143 because it cannot deal with such virtual characters. */
7144 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7146 IT_BYTEPOS (*it) += it->len;
7147 IT_CHARPOS (*it) += 1;
7149 else
7151 bidi_move_to_visually_next (&it->bidi_it);
7152 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7153 IT_CHARPOS (*it) = it->bidi_it.charpos;
7155 break;
7157 case GET_FROM_DISPLAY_VECTOR:
7158 /* Current display element of IT is from a display table entry.
7159 Advance in the display table definition. Reset it to null if
7160 end reached, and continue with characters from buffers/
7161 strings. */
7162 ++it->current.dpvec_index;
7164 /* Restore face of the iterator to what they were before the
7165 display vector entry (these entries may contain faces). */
7166 it->face_id = it->saved_face_id;
7168 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7170 int recheck_faces = it->ellipsis_p;
7172 if (it->s)
7173 it->method = GET_FROM_C_STRING;
7174 else if (STRINGP (it->string))
7175 it->method = GET_FROM_STRING;
7176 else
7178 it->method = GET_FROM_BUFFER;
7179 it->object = it->w->contents;
7182 it->dpvec = NULL;
7183 it->current.dpvec_index = -1;
7185 /* Skip over characters which were displayed via IT->dpvec. */
7186 if (it->dpvec_char_len < 0)
7187 reseat_at_next_visible_line_start (it, 1);
7188 else if (it->dpvec_char_len > 0)
7190 if (it->method == GET_FROM_STRING
7191 && it->current.overlay_string_index >= 0
7192 && it->n_overlay_strings > 0)
7193 it->ignore_overlay_strings_at_pos_p = 1;
7194 it->len = it->dpvec_char_len;
7195 set_iterator_to_next (it, reseat_p);
7198 /* Maybe recheck faces after display vector */
7199 if (recheck_faces)
7200 it->stop_charpos = IT_CHARPOS (*it);
7202 break;
7204 case GET_FROM_STRING:
7205 /* Current display element is a character from a Lisp string. */
7206 eassert (it->s == NULL && STRINGP (it->string));
7207 /* Don't advance past string end. These conditions are true
7208 when set_iterator_to_next is called at the end of
7209 get_next_display_element, in which case the Lisp string is
7210 already exhausted, and all we want is pop the iterator
7211 stack. */
7212 if (it->current.overlay_string_index >= 0)
7214 /* This is an overlay string, so there's no padding with
7215 spaces, and the number of characters in the string is
7216 where the string ends. */
7217 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7218 goto consider_string_end;
7220 else
7222 /* Not an overlay string. There could be padding, so test
7223 against it->end_charpos . */
7224 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7225 goto consider_string_end;
7227 if (it->cmp_it.id >= 0)
7229 int i;
7231 if (! it->bidi_p)
7233 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7234 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7235 if (it->cmp_it.to < it->cmp_it.nglyphs)
7236 it->cmp_it.from = it->cmp_it.to;
7237 else
7239 it->cmp_it.id = -1;
7240 composition_compute_stop_pos (&it->cmp_it,
7241 IT_STRING_CHARPOS (*it),
7242 IT_STRING_BYTEPOS (*it),
7243 it->end_charpos, it->string);
7246 else if (! it->cmp_it.reversed_p)
7248 for (i = 0; i < it->cmp_it.nchars; i++)
7249 bidi_move_to_visually_next (&it->bidi_it);
7250 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7251 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7253 if (it->cmp_it.to < it->cmp_it.nglyphs)
7254 it->cmp_it.from = it->cmp_it.to;
7255 else
7257 ptrdiff_t stop = it->end_charpos;
7258 if (it->bidi_it.scan_dir < 0)
7259 stop = -1;
7260 composition_compute_stop_pos (&it->cmp_it,
7261 IT_STRING_CHARPOS (*it),
7262 IT_STRING_BYTEPOS (*it), stop,
7263 it->string);
7266 else
7268 for (i = 0; i < it->cmp_it.nchars; i++)
7269 bidi_move_to_visually_next (&it->bidi_it);
7270 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7271 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7272 if (it->cmp_it.from > 0)
7273 it->cmp_it.to = it->cmp_it.from;
7274 else
7276 ptrdiff_t stop = it->end_charpos;
7277 if (it->bidi_it.scan_dir < 0)
7278 stop = -1;
7279 composition_compute_stop_pos (&it->cmp_it,
7280 IT_STRING_CHARPOS (*it),
7281 IT_STRING_BYTEPOS (*it), stop,
7282 it->string);
7286 else
7288 if (!it->bidi_p
7289 /* If the string position is beyond string's end, it
7290 means next_element_from_string is padding the string
7291 with blanks, in which case we bypass the bidi
7292 iterator, because it cannot deal with such virtual
7293 characters. */
7294 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7296 IT_STRING_BYTEPOS (*it) += it->len;
7297 IT_STRING_CHARPOS (*it) += 1;
7299 else
7301 int prev_scan_dir = it->bidi_it.scan_dir;
7303 bidi_move_to_visually_next (&it->bidi_it);
7304 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7305 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7306 if (prev_scan_dir != it->bidi_it.scan_dir)
7308 ptrdiff_t stop = it->end_charpos;
7310 if (it->bidi_it.scan_dir < 0)
7311 stop = -1;
7312 composition_compute_stop_pos (&it->cmp_it,
7313 IT_STRING_CHARPOS (*it),
7314 IT_STRING_BYTEPOS (*it), stop,
7315 it->string);
7320 consider_string_end:
7322 if (it->current.overlay_string_index >= 0)
7324 /* IT->string is an overlay string. Advance to the
7325 next, if there is one. */
7326 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7328 it->ellipsis_p = 0;
7329 next_overlay_string (it);
7330 if (it->ellipsis_p)
7331 setup_for_ellipsis (it, 0);
7334 else
7336 /* IT->string is not an overlay string. If we reached
7337 its end, and there is something on IT->stack, proceed
7338 with what is on the stack. This can be either another
7339 string, this time an overlay string, or a buffer. */
7340 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7341 && it->sp > 0)
7343 pop_it (it);
7344 if (it->method == GET_FROM_STRING)
7345 goto consider_string_end;
7348 break;
7350 case GET_FROM_IMAGE:
7351 case GET_FROM_STRETCH:
7352 /* The position etc with which we have to proceed are on
7353 the stack. The position may be at the end of a string,
7354 if the `display' property takes up the whole string. */
7355 eassert (it->sp > 0);
7356 pop_it (it);
7357 if (it->method == GET_FROM_STRING)
7358 goto consider_string_end;
7359 break;
7361 default:
7362 /* There are no other methods defined, so this should be a bug. */
7363 emacs_abort ();
7366 eassert (it->method != GET_FROM_STRING
7367 || (STRINGP (it->string)
7368 && IT_STRING_CHARPOS (*it) >= 0));
7371 /* Load IT's display element fields with information about the next
7372 display element which comes from a display table entry or from the
7373 result of translating a control character to one of the forms `^C'
7374 or `\003'.
7376 IT->dpvec holds the glyphs to return as characters.
7377 IT->saved_face_id holds the face id before the display vector--it
7378 is restored into IT->face_id in set_iterator_to_next. */
7380 static int
7381 next_element_from_display_vector (struct it *it)
7383 Lisp_Object gc;
7385 /* Precondition. */
7386 eassert (it->dpvec && it->current.dpvec_index >= 0);
7388 it->face_id = it->saved_face_id;
7390 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7391 That seemed totally bogus - so I changed it... */
7392 gc = it->dpvec[it->current.dpvec_index];
7394 if (GLYPH_CODE_P (gc))
7396 it->c = GLYPH_CODE_CHAR (gc);
7397 it->len = CHAR_BYTES (it->c);
7399 /* The entry may contain a face id to use. Such a face id is
7400 the id of a Lisp face, not a realized face. A face id of
7401 zero means no face is specified. */
7402 if (it->dpvec_face_id >= 0)
7403 it->face_id = it->dpvec_face_id;
7404 else
7406 int lface_id = GLYPH_CODE_FACE (gc);
7407 if (lface_id > 0)
7408 it->face_id = merge_faces (it->f, Qt, lface_id,
7409 it->saved_face_id);
7412 else
7413 /* Display table entry is invalid. Return a space. */
7414 it->c = ' ', it->len = 1;
7416 /* Don't change position and object of the iterator here. They are
7417 still the values of the character that had this display table
7418 entry or was translated, and that's what we want. */
7419 it->what = IT_CHARACTER;
7420 return 1;
7423 /* Get the first element of string/buffer in the visual order, after
7424 being reseated to a new position in a string or a buffer. */
7425 static void
7426 get_visually_first_element (struct it *it)
7428 int string_p = STRINGP (it->string) || it->s;
7429 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7430 ptrdiff_t bob = (string_p ? 0 : BEGV);
7432 if (STRINGP (it->string))
7434 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7435 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7437 else
7439 it->bidi_it.charpos = IT_CHARPOS (*it);
7440 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7443 if (it->bidi_it.charpos == eob)
7445 /* Nothing to do, but reset the FIRST_ELT flag, like
7446 bidi_paragraph_init does, because we are not going to
7447 call it. */
7448 it->bidi_it.first_elt = 0;
7450 else if (it->bidi_it.charpos == bob
7451 || (!string_p
7452 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7453 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7455 /* If we are at the beginning of a line/string, we can produce
7456 the next element right away. */
7457 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7458 bidi_move_to_visually_next (&it->bidi_it);
7460 else
7462 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7464 /* We need to prime the bidi iterator starting at the line's or
7465 string's beginning, before we will be able to produce the
7466 next element. */
7467 if (string_p)
7468 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7469 else
7470 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7471 IT_BYTEPOS (*it), -1,
7472 &it->bidi_it.bytepos);
7473 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7476 /* Now return to buffer/string position where we were asked
7477 to get the next display element, and produce that. */
7478 bidi_move_to_visually_next (&it->bidi_it);
7480 while (it->bidi_it.bytepos != orig_bytepos
7481 && it->bidi_it.charpos < eob);
7484 /* Adjust IT's position information to where we ended up. */
7485 if (STRINGP (it->string))
7487 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7488 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7490 else
7492 IT_CHARPOS (*it) = it->bidi_it.charpos;
7493 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7496 if (STRINGP (it->string) || !it->s)
7498 ptrdiff_t stop, charpos, bytepos;
7500 if (STRINGP (it->string))
7502 eassert (!it->s);
7503 stop = SCHARS (it->string);
7504 if (stop > it->end_charpos)
7505 stop = it->end_charpos;
7506 charpos = IT_STRING_CHARPOS (*it);
7507 bytepos = IT_STRING_BYTEPOS (*it);
7509 else
7511 stop = it->end_charpos;
7512 charpos = IT_CHARPOS (*it);
7513 bytepos = IT_BYTEPOS (*it);
7515 if (it->bidi_it.scan_dir < 0)
7516 stop = -1;
7517 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7518 it->string);
7522 /* Load IT with the next display element from Lisp string IT->string.
7523 IT->current.string_pos is the current position within the string.
7524 If IT->current.overlay_string_index >= 0, the Lisp string is an
7525 overlay string. */
7527 static int
7528 next_element_from_string (struct it *it)
7530 struct text_pos position;
7532 eassert (STRINGP (it->string));
7533 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7534 eassert (IT_STRING_CHARPOS (*it) >= 0);
7535 position = it->current.string_pos;
7537 /* With bidi reordering, the character to display might not be the
7538 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7539 that we were reseat()ed to a new string, whose paragraph
7540 direction is not known. */
7541 if (it->bidi_p && it->bidi_it.first_elt)
7543 get_visually_first_element (it);
7544 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7547 /* Time to check for invisible text? */
7548 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7550 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7552 if (!(!it->bidi_p
7553 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7554 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7556 /* With bidi non-linear iteration, we could find
7557 ourselves far beyond the last computed stop_charpos,
7558 with several other stop positions in between that we
7559 missed. Scan them all now, in buffer's logical
7560 order, until we find and handle the last stop_charpos
7561 that precedes our current position. */
7562 handle_stop_backwards (it, it->stop_charpos);
7563 return GET_NEXT_DISPLAY_ELEMENT (it);
7565 else
7567 if (it->bidi_p)
7569 /* Take note of the stop position we just moved
7570 across, for when we will move back across it. */
7571 it->prev_stop = it->stop_charpos;
7572 /* If we are at base paragraph embedding level, take
7573 note of the last stop position seen at this
7574 level. */
7575 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7576 it->base_level_stop = it->stop_charpos;
7578 handle_stop (it);
7580 /* Since a handler may have changed IT->method, we must
7581 recurse here. */
7582 return GET_NEXT_DISPLAY_ELEMENT (it);
7585 else if (it->bidi_p
7586 /* If we are before prev_stop, we may have overstepped
7587 on our way backwards a stop_pos, and if so, we need
7588 to handle that stop_pos. */
7589 && IT_STRING_CHARPOS (*it) < it->prev_stop
7590 /* We can sometimes back up for reasons that have nothing
7591 to do with bidi reordering. E.g., compositions. The
7592 code below is only needed when we are above the base
7593 embedding level, so test for that explicitly. */
7594 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7596 /* If we lost track of base_level_stop, we have no better
7597 place for handle_stop_backwards to start from than string
7598 beginning. This happens, e.g., when we were reseated to
7599 the previous screenful of text by vertical-motion. */
7600 if (it->base_level_stop <= 0
7601 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7602 it->base_level_stop = 0;
7603 handle_stop_backwards (it, it->base_level_stop);
7604 return GET_NEXT_DISPLAY_ELEMENT (it);
7608 if (it->current.overlay_string_index >= 0)
7610 /* Get the next character from an overlay string. In overlay
7611 strings, there is no field width or padding with spaces to
7612 do. */
7613 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7615 it->what = IT_EOB;
7616 return 0;
7618 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7619 IT_STRING_BYTEPOS (*it),
7620 it->bidi_it.scan_dir < 0
7621 ? -1
7622 : SCHARS (it->string))
7623 && next_element_from_composition (it))
7625 return 1;
7627 else if (STRING_MULTIBYTE (it->string))
7629 const unsigned char *s = (SDATA (it->string)
7630 + IT_STRING_BYTEPOS (*it));
7631 it->c = string_char_and_length (s, &it->len);
7633 else
7635 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7636 it->len = 1;
7639 else
7641 /* Get the next character from a Lisp string that is not an
7642 overlay string. Such strings come from the mode line, for
7643 example. We may have to pad with spaces, or truncate the
7644 string. See also next_element_from_c_string. */
7645 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7647 it->what = IT_EOB;
7648 return 0;
7650 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7652 /* Pad with spaces. */
7653 it->c = ' ', it->len = 1;
7654 CHARPOS (position) = BYTEPOS (position) = -1;
7656 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7657 IT_STRING_BYTEPOS (*it),
7658 it->bidi_it.scan_dir < 0
7659 ? -1
7660 : it->string_nchars)
7661 && next_element_from_composition (it))
7663 return 1;
7665 else if (STRING_MULTIBYTE (it->string))
7667 const unsigned char *s = (SDATA (it->string)
7668 + IT_STRING_BYTEPOS (*it));
7669 it->c = string_char_and_length (s, &it->len);
7671 else
7673 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7674 it->len = 1;
7678 /* Record what we have and where it came from. */
7679 it->what = IT_CHARACTER;
7680 it->object = it->string;
7681 it->position = position;
7682 return 1;
7686 /* Load IT with next display element from C string IT->s.
7687 IT->string_nchars is the maximum number of characters to return
7688 from the string. IT->end_charpos may be greater than
7689 IT->string_nchars when this function is called, in which case we
7690 may have to return padding spaces. Value is zero if end of string
7691 reached, including padding spaces. */
7693 static int
7694 next_element_from_c_string (struct it *it)
7696 int success_p = 1;
7698 eassert (it->s);
7699 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7700 it->what = IT_CHARACTER;
7701 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7702 it->object = Qnil;
7704 /* With bidi reordering, the character to display might not be the
7705 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7706 we were reseated to a new string, whose paragraph direction is
7707 not known. */
7708 if (it->bidi_p && it->bidi_it.first_elt)
7709 get_visually_first_element (it);
7711 /* IT's position can be greater than IT->string_nchars in case a
7712 field width or precision has been specified when the iterator was
7713 initialized. */
7714 if (IT_CHARPOS (*it) >= it->end_charpos)
7716 /* End of the game. */
7717 it->what = IT_EOB;
7718 success_p = 0;
7720 else if (IT_CHARPOS (*it) >= it->string_nchars)
7722 /* Pad with spaces. */
7723 it->c = ' ', it->len = 1;
7724 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7726 else if (it->multibyte_p)
7727 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7728 else
7729 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7731 return success_p;
7735 /* Set up IT to return characters from an ellipsis, if appropriate.
7736 The definition of the ellipsis glyphs may come from a display table
7737 entry. This function fills IT with the first glyph from the
7738 ellipsis if an ellipsis is to be displayed. */
7740 static int
7741 next_element_from_ellipsis (struct it *it)
7743 if (it->selective_display_ellipsis_p)
7744 setup_for_ellipsis (it, it->len);
7745 else
7747 /* The face at the current position may be different from the
7748 face we find after the invisible text. Remember what it
7749 was in IT->saved_face_id, and signal that it's there by
7750 setting face_before_selective_p. */
7751 it->saved_face_id = it->face_id;
7752 it->method = GET_FROM_BUFFER;
7753 it->object = it->w->contents;
7754 reseat_at_next_visible_line_start (it, 1);
7755 it->face_before_selective_p = 1;
7758 return GET_NEXT_DISPLAY_ELEMENT (it);
7762 /* Deliver an image display element. The iterator IT is already
7763 filled with image information (done in handle_display_prop). Value
7764 is always 1. */
7767 static int
7768 next_element_from_image (struct it *it)
7770 it->what = IT_IMAGE;
7771 it->ignore_overlay_strings_at_pos_p = 0;
7772 return 1;
7776 /* Fill iterator IT with next display element from a stretch glyph
7777 property. IT->object is the value of the text property. Value is
7778 always 1. */
7780 static int
7781 next_element_from_stretch (struct it *it)
7783 it->what = IT_STRETCH;
7784 return 1;
7787 /* Scan backwards from IT's current position until we find a stop
7788 position, or until BEGV. This is called when we find ourself
7789 before both the last known prev_stop and base_level_stop while
7790 reordering bidirectional text. */
7792 static void
7793 compute_stop_pos_backwards (struct it *it)
7795 const int SCAN_BACK_LIMIT = 1000;
7796 struct text_pos pos;
7797 struct display_pos save_current = it->current;
7798 struct text_pos save_position = it->position;
7799 ptrdiff_t charpos = IT_CHARPOS (*it);
7800 ptrdiff_t where_we_are = charpos;
7801 ptrdiff_t save_stop_pos = it->stop_charpos;
7802 ptrdiff_t save_end_pos = it->end_charpos;
7804 eassert (NILP (it->string) && !it->s);
7805 eassert (it->bidi_p);
7806 it->bidi_p = 0;
7809 it->end_charpos = min (charpos + 1, ZV);
7810 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7811 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7812 reseat_1 (it, pos, 0);
7813 compute_stop_pos (it);
7814 /* We must advance forward, right? */
7815 if (it->stop_charpos <= charpos)
7816 emacs_abort ();
7818 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7820 if (it->stop_charpos <= where_we_are)
7821 it->prev_stop = it->stop_charpos;
7822 else
7823 it->prev_stop = BEGV;
7824 it->bidi_p = 1;
7825 it->current = save_current;
7826 it->position = save_position;
7827 it->stop_charpos = save_stop_pos;
7828 it->end_charpos = save_end_pos;
7831 /* Scan forward from CHARPOS in the current buffer/string, until we
7832 find a stop position > current IT's position. Then handle the stop
7833 position before that. This is called when we bump into a stop
7834 position while reordering bidirectional text. CHARPOS should be
7835 the last previously processed stop_pos (or BEGV/0, if none were
7836 processed yet) whose position is less that IT's current
7837 position. */
7839 static void
7840 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7842 int bufp = !STRINGP (it->string);
7843 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7844 struct display_pos save_current = it->current;
7845 struct text_pos save_position = it->position;
7846 struct text_pos pos1;
7847 ptrdiff_t next_stop;
7849 /* Scan in strict logical order. */
7850 eassert (it->bidi_p);
7851 it->bidi_p = 0;
7854 it->prev_stop = charpos;
7855 if (bufp)
7857 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7858 reseat_1 (it, pos1, 0);
7860 else
7861 it->current.string_pos = string_pos (charpos, it->string);
7862 compute_stop_pos (it);
7863 /* We must advance forward, right? */
7864 if (it->stop_charpos <= it->prev_stop)
7865 emacs_abort ();
7866 charpos = it->stop_charpos;
7868 while (charpos <= where_we_are);
7870 it->bidi_p = 1;
7871 it->current = save_current;
7872 it->position = save_position;
7873 next_stop = it->stop_charpos;
7874 it->stop_charpos = it->prev_stop;
7875 handle_stop (it);
7876 it->stop_charpos = next_stop;
7879 /* Load IT with the next display element from current_buffer. Value
7880 is zero if end of buffer reached. IT->stop_charpos is the next
7881 position at which to stop and check for text properties or buffer
7882 end. */
7884 static int
7885 next_element_from_buffer (struct it *it)
7887 int success_p = 1;
7889 eassert (IT_CHARPOS (*it) >= BEGV);
7890 eassert (NILP (it->string) && !it->s);
7891 eassert (!it->bidi_p
7892 || (EQ (it->bidi_it.string.lstring, Qnil)
7893 && it->bidi_it.string.s == NULL));
7895 /* With bidi reordering, the character to display might not be the
7896 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7897 we were reseat()ed to a new buffer position, which is potentially
7898 a different paragraph. */
7899 if (it->bidi_p && it->bidi_it.first_elt)
7901 get_visually_first_element (it);
7902 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7905 if (IT_CHARPOS (*it) >= it->stop_charpos)
7907 if (IT_CHARPOS (*it) >= it->end_charpos)
7909 int overlay_strings_follow_p;
7911 /* End of the game, except when overlay strings follow that
7912 haven't been returned yet. */
7913 if (it->overlay_strings_at_end_processed_p)
7914 overlay_strings_follow_p = 0;
7915 else
7917 it->overlay_strings_at_end_processed_p = 1;
7918 overlay_strings_follow_p = get_overlay_strings (it, 0);
7921 if (overlay_strings_follow_p)
7922 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7923 else
7925 it->what = IT_EOB;
7926 it->position = it->current.pos;
7927 success_p = 0;
7930 else if (!(!it->bidi_p
7931 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7932 || IT_CHARPOS (*it) == it->stop_charpos))
7934 /* With bidi non-linear iteration, we could find ourselves
7935 far beyond the last computed stop_charpos, with several
7936 other stop positions in between that we missed. Scan
7937 them all now, in buffer's logical order, until we find
7938 and handle the last stop_charpos that precedes our
7939 current position. */
7940 handle_stop_backwards (it, it->stop_charpos);
7941 return GET_NEXT_DISPLAY_ELEMENT (it);
7943 else
7945 if (it->bidi_p)
7947 /* Take note of the stop position we just moved across,
7948 for when we will move back across it. */
7949 it->prev_stop = it->stop_charpos;
7950 /* If we are at base paragraph embedding level, take
7951 note of the last stop position seen at this
7952 level. */
7953 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7954 it->base_level_stop = it->stop_charpos;
7956 handle_stop (it);
7957 return GET_NEXT_DISPLAY_ELEMENT (it);
7960 else if (it->bidi_p
7961 /* If we are before prev_stop, we may have overstepped on
7962 our way backwards a stop_pos, and if so, we need to
7963 handle that stop_pos. */
7964 && IT_CHARPOS (*it) < it->prev_stop
7965 /* We can sometimes back up for reasons that have nothing
7966 to do with bidi reordering. E.g., compositions. The
7967 code below is only needed when we are above the base
7968 embedding level, so test for that explicitly. */
7969 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7971 if (it->base_level_stop <= 0
7972 || IT_CHARPOS (*it) < it->base_level_stop)
7974 /* If we lost track of base_level_stop, we need to find
7975 prev_stop by looking backwards. This happens, e.g., when
7976 we were reseated to the previous screenful of text by
7977 vertical-motion. */
7978 it->base_level_stop = BEGV;
7979 compute_stop_pos_backwards (it);
7980 handle_stop_backwards (it, it->prev_stop);
7982 else
7983 handle_stop_backwards (it, it->base_level_stop);
7984 return GET_NEXT_DISPLAY_ELEMENT (it);
7986 else
7988 /* No face changes, overlays etc. in sight, so just return a
7989 character from current_buffer. */
7990 unsigned char *p;
7991 ptrdiff_t stop;
7993 /* Maybe run the redisplay end trigger hook. Performance note:
7994 This doesn't seem to cost measurable time. */
7995 if (it->redisplay_end_trigger_charpos
7996 && it->glyph_row
7997 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7998 run_redisplay_end_trigger_hook (it);
8000 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8001 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8002 stop)
8003 && next_element_from_composition (it))
8005 return 1;
8008 /* Get the next character, maybe multibyte. */
8009 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8010 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8011 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8012 else
8013 it->c = *p, it->len = 1;
8015 /* Record what we have and where it came from. */
8016 it->what = IT_CHARACTER;
8017 it->object = it->w->contents;
8018 it->position = it->current.pos;
8020 /* Normally we return the character found above, except when we
8021 really want to return an ellipsis for selective display. */
8022 if (it->selective)
8024 if (it->c == '\n')
8026 /* A value of selective > 0 means hide lines indented more
8027 than that number of columns. */
8028 if (it->selective > 0
8029 && IT_CHARPOS (*it) + 1 < ZV
8030 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8031 IT_BYTEPOS (*it) + 1,
8032 it->selective))
8034 success_p = next_element_from_ellipsis (it);
8035 it->dpvec_char_len = -1;
8038 else if (it->c == '\r' && it->selective == -1)
8040 /* A value of selective == -1 means that everything from the
8041 CR to the end of the line is invisible, with maybe an
8042 ellipsis displayed for it. */
8043 success_p = next_element_from_ellipsis (it);
8044 it->dpvec_char_len = -1;
8049 /* Value is zero if end of buffer reached. */
8050 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8051 return success_p;
8055 /* Run the redisplay end trigger hook for IT. */
8057 static void
8058 run_redisplay_end_trigger_hook (struct it *it)
8060 Lisp_Object args[3];
8062 /* IT->glyph_row should be non-null, i.e. we should be actually
8063 displaying something, or otherwise we should not run the hook. */
8064 eassert (it->glyph_row);
8066 /* Set up hook arguments. */
8067 args[0] = Qredisplay_end_trigger_functions;
8068 args[1] = it->window;
8069 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8070 it->redisplay_end_trigger_charpos = 0;
8072 /* Since we are *trying* to run these functions, don't try to run
8073 them again, even if they get an error. */
8074 wset_redisplay_end_trigger (it->w, Qnil);
8075 Frun_hook_with_args (3, args);
8077 /* Notice if it changed the face of the character we are on. */
8078 handle_face_prop (it);
8082 /* Deliver a composition display element. Unlike the other
8083 next_element_from_XXX, this function is not registered in the array
8084 get_next_element[]. It is called from next_element_from_buffer and
8085 next_element_from_string when necessary. */
8087 static int
8088 next_element_from_composition (struct it *it)
8090 it->what = IT_COMPOSITION;
8091 it->len = it->cmp_it.nbytes;
8092 if (STRINGP (it->string))
8094 if (it->c < 0)
8096 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8097 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8098 return 0;
8100 it->position = it->current.string_pos;
8101 it->object = it->string;
8102 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8103 IT_STRING_BYTEPOS (*it), it->string);
8105 else
8107 if (it->c < 0)
8109 IT_CHARPOS (*it) += it->cmp_it.nchars;
8110 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8111 if (it->bidi_p)
8113 if (it->bidi_it.new_paragraph)
8114 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8115 /* Resync the bidi iterator with IT's new position.
8116 FIXME: this doesn't support bidirectional text. */
8117 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8118 bidi_move_to_visually_next (&it->bidi_it);
8120 return 0;
8122 it->position = it->current.pos;
8123 it->object = it->w->contents;
8124 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8125 IT_BYTEPOS (*it), Qnil);
8127 return 1;
8132 /***********************************************************************
8133 Moving an iterator without producing glyphs
8134 ***********************************************************************/
8136 /* Check if iterator is at a position corresponding to a valid buffer
8137 position after some move_it_ call. */
8139 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8140 ((it)->method == GET_FROM_STRING \
8141 ? IT_STRING_CHARPOS (*it) == 0 \
8142 : 1)
8145 /* Move iterator IT to a specified buffer or X position within one
8146 line on the display without producing glyphs.
8148 OP should be a bit mask including some or all of these bits:
8149 MOVE_TO_X: Stop upon reaching x-position TO_X.
8150 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8151 Regardless of OP's value, stop upon reaching the end of the display line.
8153 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8154 This means, in particular, that TO_X includes window's horizontal
8155 scroll amount.
8157 The return value has several possible values that
8158 say what condition caused the scan to stop:
8160 MOVE_POS_MATCH_OR_ZV
8161 - when TO_POS or ZV was reached.
8163 MOVE_X_REACHED
8164 -when TO_X was reached before TO_POS or ZV were reached.
8166 MOVE_LINE_CONTINUED
8167 - when we reached the end of the display area and the line must
8168 be continued.
8170 MOVE_LINE_TRUNCATED
8171 - when we reached the end of the display area and the line is
8172 truncated.
8174 MOVE_NEWLINE_OR_CR
8175 - when we stopped at a line end, i.e. a newline or a CR and selective
8176 display is on. */
8178 static enum move_it_result
8179 move_it_in_display_line_to (struct it *it,
8180 ptrdiff_t to_charpos, int to_x,
8181 enum move_operation_enum op)
8183 enum move_it_result result = MOVE_UNDEFINED;
8184 struct glyph_row *saved_glyph_row;
8185 struct it wrap_it, atpos_it, atx_it, ppos_it;
8186 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8187 void *ppos_data = NULL;
8188 int may_wrap = 0;
8189 enum it_method prev_method = it->method;
8190 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8191 int saw_smaller_pos = prev_pos < to_charpos;
8193 /* Don't produce glyphs in produce_glyphs. */
8194 saved_glyph_row = it->glyph_row;
8195 it->glyph_row = NULL;
8197 /* Use wrap_it to save a copy of IT wherever a word wrap could
8198 occur. Use atpos_it to save a copy of IT at the desired buffer
8199 position, if found, so that we can scan ahead and check if the
8200 word later overshoots the window edge. Use atx_it similarly, for
8201 pixel positions. */
8202 wrap_it.sp = -1;
8203 atpos_it.sp = -1;
8204 atx_it.sp = -1;
8206 /* Use ppos_it under bidi reordering to save a copy of IT for the
8207 position > CHARPOS that is the closest to CHARPOS. We restore
8208 that position in IT when we have scanned the entire display line
8209 without finding a match for CHARPOS and all the character
8210 positions are greater than CHARPOS. */
8211 if (it->bidi_p)
8213 SAVE_IT (ppos_it, *it, ppos_data);
8214 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8215 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8216 SAVE_IT (ppos_it, *it, ppos_data);
8219 #define BUFFER_POS_REACHED_P() \
8220 ((op & MOVE_TO_POS) != 0 \
8221 && BUFFERP (it->object) \
8222 && (IT_CHARPOS (*it) == to_charpos \
8223 || ((!it->bidi_p \
8224 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8225 && IT_CHARPOS (*it) > to_charpos) \
8226 || (it->what == IT_COMPOSITION \
8227 && ((IT_CHARPOS (*it) > to_charpos \
8228 && to_charpos >= it->cmp_it.charpos) \
8229 || (IT_CHARPOS (*it) < to_charpos \
8230 && to_charpos <= it->cmp_it.charpos)))) \
8231 && (it->method == GET_FROM_BUFFER \
8232 || (it->method == GET_FROM_DISPLAY_VECTOR \
8233 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8235 /* If there's a line-/wrap-prefix, handle it. */
8236 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8237 && it->current_y < it->last_visible_y)
8238 handle_line_prefix (it);
8240 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8241 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8243 while (1)
8245 int x, i, ascent = 0, descent = 0;
8247 /* Utility macro to reset an iterator with x, ascent, and descent. */
8248 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8249 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8250 (IT)->max_descent = descent)
8252 /* Stop if we move beyond TO_CHARPOS (after an image or a
8253 display string or stretch glyph). */
8254 if ((op & MOVE_TO_POS) != 0
8255 && BUFFERP (it->object)
8256 && it->method == GET_FROM_BUFFER
8257 && (((!it->bidi_p
8258 /* When the iterator is at base embedding level, we
8259 are guaranteed that characters are delivered for
8260 display in strictly increasing order of their
8261 buffer positions. */
8262 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8263 && IT_CHARPOS (*it) > to_charpos)
8264 || (it->bidi_p
8265 && (prev_method == GET_FROM_IMAGE
8266 || prev_method == GET_FROM_STRETCH
8267 || prev_method == GET_FROM_STRING)
8268 /* Passed TO_CHARPOS from left to right. */
8269 && ((prev_pos < to_charpos
8270 && IT_CHARPOS (*it) > to_charpos)
8271 /* Passed TO_CHARPOS from right to left. */
8272 || (prev_pos > to_charpos
8273 && IT_CHARPOS (*it) < to_charpos)))))
8275 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8277 result = MOVE_POS_MATCH_OR_ZV;
8278 break;
8280 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8281 /* If wrap_it is valid, the current position might be in a
8282 word that is wrapped. So, save the iterator in
8283 atpos_it and continue to see if wrapping happens. */
8284 SAVE_IT (atpos_it, *it, atpos_data);
8287 /* Stop when ZV reached.
8288 We used to stop here when TO_CHARPOS reached as well, but that is
8289 too soon if this glyph does not fit on this line. So we handle it
8290 explicitly below. */
8291 if (!get_next_display_element (it))
8293 result = MOVE_POS_MATCH_OR_ZV;
8294 break;
8297 if (it->line_wrap == TRUNCATE)
8299 if (BUFFER_POS_REACHED_P ())
8301 result = MOVE_POS_MATCH_OR_ZV;
8302 break;
8305 else
8307 if (it->line_wrap == WORD_WRAP)
8309 if (IT_DISPLAYING_WHITESPACE (it))
8310 may_wrap = 1;
8311 else if (may_wrap)
8313 /* We have reached a glyph that follows one or more
8314 whitespace characters. If the position is
8315 already found, we are done. */
8316 if (atpos_it.sp >= 0)
8318 RESTORE_IT (it, &atpos_it, atpos_data);
8319 result = MOVE_POS_MATCH_OR_ZV;
8320 goto done;
8322 if (atx_it.sp >= 0)
8324 RESTORE_IT (it, &atx_it, atx_data);
8325 result = MOVE_X_REACHED;
8326 goto done;
8328 /* Otherwise, we can wrap here. */
8329 SAVE_IT (wrap_it, *it, wrap_data);
8330 may_wrap = 0;
8335 /* Remember the line height for the current line, in case
8336 the next element doesn't fit on the line. */
8337 ascent = it->max_ascent;
8338 descent = it->max_descent;
8340 /* The call to produce_glyphs will get the metrics of the
8341 display element IT is loaded with. Record the x-position
8342 before this display element, in case it doesn't fit on the
8343 line. */
8344 x = it->current_x;
8346 PRODUCE_GLYPHS (it);
8348 if (it->area != TEXT_AREA)
8350 prev_method = it->method;
8351 if (it->method == GET_FROM_BUFFER)
8352 prev_pos = IT_CHARPOS (*it);
8353 set_iterator_to_next (it, 1);
8354 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8355 SET_TEXT_POS (this_line_min_pos,
8356 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8357 if (it->bidi_p
8358 && (op & MOVE_TO_POS)
8359 && IT_CHARPOS (*it) > to_charpos
8360 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8361 SAVE_IT (ppos_it, *it, ppos_data);
8362 continue;
8365 /* The number of glyphs we get back in IT->nglyphs will normally
8366 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8367 character on a terminal frame, or (iii) a line end. For the
8368 second case, IT->nglyphs - 1 padding glyphs will be present.
8369 (On X frames, there is only one glyph produced for a
8370 composite character.)
8372 The behavior implemented below means, for continuation lines,
8373 that as many spaces of a TAB as fit on the current line are
8374 displayed there. For terminal frames, as many glyphs of a
8375 multi-glyph character are displayed in the current line, too.
8376 This is what the old redisplay code did, and we keep it that
8377 way. Under X, the whole shape of a complex character must
8378 fit on the line or it will be completely displayed in the
8379 next line.
8381 Note that both for tabs and padding glyphs, all glyphs have
8382 the same width. */
8383 if (it->nglyphs)
8385 /* More than one glyph or glyph doesn't fit on line. All
8386 glyphs have the same width. */
8387 int single_glyph_width = it->pixel_width / it->nglyphs;
8388 int new_x;
8389 int x_before_this_char = x;
8390 int hpos_before_this_char = it->hpos;
8392 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8394 new_x = x + single_glyph_width;
8396 /* We want to leave anything reaching TO_X to the caller. */
8397 if ((op & MOVE_TO_X) && new_x > to_x)
8399 if (BUFFER_POS_REACHED_P ())
8401 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8402 goto buffer_pos_reached;
8403 if (atpos_it.sp < 0)
8405 SAVE_IT (atpos_it, *it, atpos_data);
8406 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8409 else
8411 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8413 it->current_x = x;
8414 result = MOVE_X_REACHED;
8415 break;
8417 if (atx_it.sp < 0)
8419 SAVE_IT (atx_it, *it, atx_data);
8420 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8425 if (/* Lines are continued. */
8426 it->line_wrap != TRUNCATE
8427 && (/* And glyph doesn't fit on the line. */
8428 new_x > it->last_visible_x
8429 /* Or it fits exactly and we're on a window
8430 system frame. */
8431 || (new_x == it->last_visible_x
8432 && FRAME_WINDOW_P (it->f)
8433 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8434 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8435 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8437 if (/* IT->hpos == 0 means the very first glyph
8438 doesn't fit on the line, e.g. a wide image. */
8439 it->hpos == 0
8440 || (new_x == it->last_visible_x
8441 && FRAME_WINDOW_P (it->f)))
8443 ++it->hpos;
8444 it->current_x = new_x;
8446 /* The character's last glyph just barely fits
8447 in this row. */
8448 if (i == it->nglyphs - 1)
8450 /* If this is the destination position,
8451 return a position *before* it in this row,
8452 now that we know it fits in this row. */
8453 if (BUFFER_POS_REACHED_P ())
8455 if (it->line_wrap != WORD_WRAP
8456 || wrap_it.sp < 0)
8458 it->hpos = hpos_before_this_char;
8459 it->current_x = x_before_this_char;
8460 result = MOVE_POS_MATCH_OR_ZV;
8461 break;
8463 if (it->line_wrap == WORD_WRAP
8464 && atpos_it.sp < 0)
8466 SAVE_IT (atpos_it, *it, atpos_data);
8467 atpos_it.current_x = x_before_this_char;
8468 atpos_it.hpos = hpos_before_this_char;
8472 prev_method = it->method;
8473 if (it->method == GET_FROM_BUFFER)
8474 prev_pos = IT_CHARPOS (*it);
8475 set_iterator_to_next (it, 1);
8476 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8477 SET_TEXT_POS (this_line_min_pos,
8478 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8479 /* On graphical terminals, newlines may
8480 "overflow" into the fringe if
8481 overflow-newline-into-fringe is non-nil.
8482 On text terminals, and on graphical
8483 terminals with no right margin, newlines
8484 may overflow into the last glyph on the
8485 display line.*/
8486 if (!FRAME_WINDOW_P (it->f)
8487 || ((it->bidi_p
8488 && it->bidi_it.paragraph_dir == R2L)
8489 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8490 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8491 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8493 if (!get_next_display_element (it))
8495 result = MOVE_POS_MATCH_OR_ZV;
8496 break;
8498 if (BUFFER_POS_REACHED_P ())
8500 if (ITERATOR_AT_END_OF_LINE_P (it))
8501 result = MOVE_POS_MATCH_OR_ZV;
8502 else
8503 result = MOVE_LINE_CONTINUED;
8504 break;
8506 if (ITERATOR_AT_END_OF_LINE_P (it))
8508 result = MOVE_NEWLINE_OR_CR;
8509 break;
8514 else
8515 IT_RESET_X_ASCENT_DESCENT (it);
8517 if (wrap_it.sp >= 0)
8519 RESTORE_IT (it, &wrap_it, wrap_data);
8520 atpos_it.sp = -1;
8521 atx_it.sp = -1;
8524 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8525 IT_CHARPOS (*it)));
8526 result = MOVE_LINE_CONTINUED;
8527 break;
8530 if (BUFFER_POS_REACHED_P ())
8532 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8533 goto buffer_pos_reached;
8534 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8536 SAVE_IT (atpos_it, *it, atpos_data);
8537 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8541 if (new_x > it->first_visible_x)
8543 /* Glyph is visible. Increment number of glyphs that
8544 would be displayed. */
8545 ++it->hpos;
8549 if (result != MOVE_UNDEFINED)
8550 break;
8552 else if (BUFFER_POS_REACHED_P ())
8554 buffer_pos_reached:
8555 IT_RESET_X_ASCENT_DESCENT (it);
8556 result = MOVE_POS_MATCH_OR_ZV;
8557 break;
8559 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8561 /* Stop when TO_X specified and reached. This check is
8562 necessary here because of lines consisting of a line end,
8563 only. The line end will not produce any glyphs and we
8564 would never get MOVE_X_REACHED. */
8565 eassert (it->nglyphs == 0);
8566 result = MOVE_X_REACHED;
8567 break;
8570 /* Is this a line end? If yes, we're done. */
8571 if (ITERATOR_AT_END_OF_LINE_P (it))
8573 /* If we are past TO_CHARPOS, but never saw any character
8574 positions smaller than TO_CHARPOS, return
8575 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8576 did. */
8577 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8579 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8581 if (IT_CHARPOS (ppos_it) < ZV)
8583 RESTORE_IT (it, &ppos_it, ppos_data);
8584 result = MOVE_POS_MATCH_OR_ZV;
8586 else
8587 goto buffer_pos_reached;
8589 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8590 && IT_CHARPOS (*it) > to_charpos)
8591 goto buffer_pos_reached;
8592 else
8593 result = MOVE_NEWLINE_OR_CR;
8595 else
8596 result = MOVE_NEWLINE_OR_CR;
8597 break;
8600 prev_method = it->method;
8601 if (it->method == GET_FROM_BUFFER)
8602 prev_pos = IT_CHARPOS (*it);
8603 /* The current display element has been consumed. Advance
8604 to the next. */
8605 set_iterator_to_next (it, 1);
8606 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8607 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8608 if (IT_CHARPOS (*it) < to_charpos)
8609 saw_smaller_pos = 1;
8610 if (it->bidi_p
8611 && (op & MOVE_TO_POS)
8612 && IT_CHARPOS (*it) >= to_charpos
8613 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8614 SAVE_IT (ppos_it, *it, ppos_data);
8616 /* Stop if lines are truncated and IT's current x-position is
8617 past the right edge of the window now. */
8618 if (it->line_wrap == TRUNCATE
8619 && it->current_x >= it->last_visible_x)
8621 if (!FRAME_WINDOW_P (it->f)
8622 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8623 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8624 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8625 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8627 int at_eob_p = 0;
8629 if ((at_eob_p = !get_next_display_element (it))
8630 || BUFFER_POS_REACHED_P ()
8631 /* If we are past TO_CHARPOS, but never saw any
8632 character positions smaller than TO_CHARPOS,
8633 return MOVE_POS_MATCH_OR_ZV, like the
8634 unidirectional display did. */
8635 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8636 && !saw_smaller_pos
8637 && IT_CHARPOS (*it) > to_charpos))
8639 if (it->bidi_p
8640 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8641 RESTORE_IT (it, &ppos_it, ppos_data);
8642 result = MOVE_POS_MATCH_OR_ZV;
8643 break;
8645 if (ITERATOR_AT_END_OF_LINE_P (it))
8647 result = MOVE_NEWLINE_OR_CR;
8648 break;
8651 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8652 && !saw_smaller_pos
8653 && IT_CHARPOS (*it) > to_charpos)
8655 if (IT_CHARPOS (ppos_it) < ZV)
8656 RESTORE_IT (it, &ppos_it, ppos_data);
8657 result = MOVE_POS_MATCH_OR_ZV;
8658 break;
8660 result = MOVE_LINE_TRUNCATED;
8661 break;
8663 #undef IT_RESET_X_ASCENT_DESCENT
8666 #undef BUFFER_POS_REACHED_P
8668 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8669 restore the saved iterator. */
8670 if (atpos_it.sp >= 0)
8671 RESTORE_IT (it, &atpos_it, atpos_data);
8672 else if (atx_it.sp >= 0)
8673 RESTORE_IT (it, &atx_it, atx_data);
8675 done:
8677 if (atpos_data)
8678 bidi_unshelve_cache (atpos_data, 1);
8679 if (atx_data)
8680 bidi_unshelve_cache (atx_data, 1);
8681 if (wrap_data)
8682 bidi_unshelve_cache (wrap_data, 1);
8683 if (ppos_data)
8684 bidi_unshelve_cache (ppos_data, 1);
8686 /* Restore the iterator settings altered at the beginning of this
8687 function. */
8688 it->glyph_row = saved_glyph_row;
8689 return result;
8692 /* For external use. */
8693 void
8694 move_it_in_display_line (struct it *it,
8695 ptrdiff_t to_charpos, int to_x,
8696 enum move_operation_enum op)
8698 if (it->line_wrap == WORD_WRAP
8699 && (op & MOVE_TO_X))
8701 struct it save_it;
8702 void *save_data = NULL;
8703 int skip;
8705 SAVE_IT (save_it, *it, save_data);
8706 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8707 /* When word-wrap is on, TO_X may lie past the end
8708 of a wrapped line. Then it->current is the
8709 character on the next line, so backtrack to the
8710 space before the wrap point. */
8711 if (skip == MOVE_LINE_CONTINUED)
8713 int prev_x = max (it->current_x - 1, 0);
8714 RESTORE_IT (it, &save_it, save_data);
8715 move_it_in_display_line_to
8716 (it, -1, prev_x, MOVE_TO_X);
8718 else
8719 bidi_unshelve_cache (save_data, 1);
8721 else
8722 move_it_in_display_line_to (it, to_charpos, to_x, op);
8726 /* Move IT forward until it satisfies one or more of the criteria in
8727 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8729 OP is a bit-mask that specifies where to stop, and in particular,
8730 which of those four position arguments makes a difference. See the
8731 description of enum move_operation_enum.
8733 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8734 screen line, this function will set IT to the next position that is
8735 displayed to the right of TO_CHARPOS on the screen. */
8737 void
8738 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8740 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8741 int line_height, line_start_x = 0, reached = 0;
8742 void *backup_data = NULL;
8744 for (;;)
8746 if (op & MOVE_TO_VPOS)
8748 /* If no TO_CHARPOS and no TO_X specified, stop at the
8749 start of the line TO_VPOS. */
8750 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8752 if (it->vpos == to_vpos)
8754 reached = 1;
8755 break;
8757 else
8758 skip = move_it_in_display_line_to (it, -1, -1, 0);
8760 else
8762 /* TO_VPOS >= 0 means stop at TO_X in the line at
8763 TO_VPOS, or at TO_POS, whichever comes first. */
8764 if (it->vpos == to_vpos)
8766 reached = 2;
8767 break;
8770 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8772 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8774 reached = 3;
8775 break;
8777 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8779 /* We have reached TO_X but not in the line we want. */
8780 skip = move_it_in_display_line_to (it, to_charpos,
8781 -1, MOVE_TO_POS);
8782 if (skip == MOVE_POS_MATCH_OR_ZV)
8784 reached = 4;
8785 break;
8790 else if (op & MOVE_TO_Y)
8792 struct it it_backup;
8794 if (it->line_wrap == WORD_WRAP)
8795 SAVE_IT (it_backup, *it, backup_data);
8797 /* TO_Y specified means stop at TO_X in the line containing
8798 TO_Y---or at TO_CHARPOS if this is reached first. The
8799 problem is that we can't really tell whether the line
8800 contains TO_Y before we have completely scanned it, and
8801 this may skip past TO_X. What we do is to first scan to
8802 TO_X.
8804 If TO_X is not specified, use a TO_X of zero. The reason
8805 is to make the outcome of this function more predictable.
8806 If we didn't use TO_X == 0, we would stop at the end of
8807 the line which is probably not what a caller would expect
8808 to happen. */
8809 skip = move_it_in_display_line_to
8810 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8811 (MOVE_TO_X | (op & MOVE_TO_POS)));
8813 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8814 if (skip == MOVE_POS_MATCH_OR_ZV)
8815 reached = 5;
8816 else if (skip == MOVE_X_REACHED)
8818 /* If TO_X was reached, we want to know whether TO_Y is
8819 in the line. We know this is the case if the already
8820 scanned glyphs make the line tall enough. Otherwise,
8821 we must check by scanning the rest of the line. */
8822 line_height = it->max_ascent + it->max_descent;
8823 if (to_y >= it->current_y
8824 && to_y < it->current_y + line_height)
8826 reached = 6;
8827 break;
8829 SAVE_IT (it_backup, *it, backup_data);
8830 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8831 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8832 op & MOVE_TO_POS);
8833 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8834 line_height = it->max_ascent + it->max_descent;
8835 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8837 if (to_y >= it->current_y
8838 && to_y < it->current_y + line_height)
8840 /* If TO_Y is in this line and TO_X was reached
8841 above, we scanned too far. We have to restore
8842 IT's settings to the ones before skipping. But
8843 keep the more accurate values of max_ascent and
8844 max_descent we've found while skipping the rest
8845 of the line, for the sake of callers, such as
8846 pos_visible_p, that need to know the line
8847 height. */
8848 int max_ascent = it->max_ascent;
8849 int max_descent = it->max_descent;
8851 RESTORE_IT (it, &it_backup, backup_data);
8852 it->max_ascent = max_ascent;
8853 it->max_descent = max_descent;
8854 reached = 6;
8856 else
8858 skip = skip2;
8859 if (skip == MOVE_POS_MATCH_OR_ZV)
8860 reached = 7;
8863 else
8865 /* Check whether TO_Y is in this line. */
8866 line_height = it->max_ascent + it->max_descent;
8867 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8869 if (to_y >= it->current_y
8870 && to_y < it->current_y + line_height)
8872 /* When word-wrap is on, TO_X may lie past the end
8873 of a wrapped line. Then it->current is the
8874 character on the next line, so backtrack to the
8875 space before the wrap point. */
8876 if (skip == MOVE_LINE_CONTINUED
8877 && it->line_wrap == WORD_WRAP)
8879 int prev_x = max (it->current_x - 1, 0);
8880 RESTORE_IT (it, &it_backup, backup_data);
8881 skip = move_it_in_display_line_to
8882 (it, -1, prev_x, MOVE_TO_X);
8884 reached = 6;
8888 if (reached)
8889 break;
8891 else if (BUFFERP (it->object)
8892 && (it->method == GET_FROM_BUFFER
8893 || it->method == GET_FROM_STRETCH)
8894 && IT_CHARPOS (*it) >= to_charpos
8895 /* Under bidi iteration, a call to set_iterator_to_next
8896 can scan far beyond to_charpos if the initial
8897 portion of the next line needs to be reordered. In
8898 that case, give move_it_in_display_line_to another
8899 chance below. */
8900 && !(it->bidi_p
8901 && it->bidi_it.scan_dir == -1))
8902 skip = MOVE_POS_MATCH_OR_ZV;
8903 else
8904 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8906 switch (skip)
8908 case MOVE_POS_MATCH_OR_ZV:
8909 reached = 8;
8910 goto out;
8912 case MOVE_NEWLINE_OR_CR:
8913 set_iterator_to_next (it, 1);
8914 it->continuation_lines_width = 0;
8915 break;
8917 case MOVE_LINE_TRUNCATED:
8918 it->continuation_lines_width = 0;
8919 reseat_at_next_visible_line_start (it, 0);
8920 if ((op & MOVE_TO_POS) != 0
8921 && IT_CHARPOS (*it) > to_charpos)
8923 reached = 9;
8924 goto out;
8926 break;
8928 case MOVE_LINE_CONTINUED:
8929 /* For continued lines ending in a tab, some of the glyphs
8930 associated with the tab are displayed on the current
8931 line. Since it->current_x does not include these glyphs,
8932 we use it->last_visible_x instead. */
8933 if (it->c == '\t')
8935 it->continuation_lines_width += it->last_visible_x;
8936 /* When moving by vpos, ensure that the iterator really
8937 advances to the next line (bug#847, bug#969). Fixme:
8938 do we need to do this in other circumstances? */
8939 if (it->current_x != it->last_visible_x
8940 && (op & MOVE_TO_VPOS)
8941 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8943 line_start_x = it->current_x + it->pixel_width
8944 - it->last_visible_x;
8945 set_iterator_to_next (it, 0);
8948 else
8949 it->continuation_lines_width += it->current_x;
8950 break;
8952 default:
8953 emacs_abort ();
8956 /* Reset/increment for the next run. */
8957 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8958 it->current_x = line_start_x;
8959 line_start_x = 0;
8960 it->hpos = 0;
8961 it->current_y += it->max_ascent + it->max_descent;
8962 ++it->vpos;
8963 last_height = it->max_ascent + it->max_descent;
8964 it->max_ascent = it->max_descent = 0;
8967 out:
8969 /* On text terminals, we may stop at the end of a line in the middle
8970 of a multi-character glyph. If the glyph itself is continued,
8971 i.e. it is actually displayed on the next line, don't treat this
8972 stopping point as valid; move to the next line instead (unless
8973 that brings us offscreen). */
8974 if (!FRAME_WINDOW_P (it->f)
8975 && op & MOVE_TO_POS
8976 && IT_CHARPOS (*it) == to_charpos
8977 && it->what == IT_CHARACTER
8978 && it->nglyphs > 1
8979 && it->line_wrap == WINDOW_WRAP
8980 && it->current_x == it->last_visible_x - 1
8981 && it->c != '\n'
8982 && it->c != '\t'
8983 && it->vpos < XFASTINT (it->w->window_end_vpos))
8985 it->continuation_lines_width += it->current_x;
8986 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8987 it->current_y += it->max_ascent + it->max_descent;
8988 ++it->vpos;
8989 last_height = it->max_ascent + it->max_descent;
8992 if (backup_data)
8993 bidi_unshelve_cache (backup_data, 1);
8995 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8999 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9001 If DY > 0, move IT backward at least that many pixels. DY = 0
9002 means move IT backward to the preceding line start or BEGV. This
9003 function may move over more than DY pixels if IT->current_y - DY
9004 ends up in the middle of a line; in this case IT->current_y will be
9005 set to the top of the line moved to. */
9007 void
9008 move_it_vertically_backward (struct it *it, int dy)
9010 int nlines, h;
9011 struct it it2, it3;
9012 void *it2data = NULL, *it3data = NULL;
9013 ptrdiff_t start_pos;
9014 int nchars_per_row
9015 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9016 ptrdiff_t pos_limit;
9018 move_further_back:
9019 eassert (dy >= 0);
9021 start_pos = IT_CHARPOS (*it);
9023 /* Estimate how many newlines we must move back. */
9024 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9025 if (it->line_wrap == TRUNCATE)
9026 pos_limit = BEGV;
9027 else
9028 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9030 /* Set the iterator's position that many lines back. But don't go
9031 back more than NLINES full screen lines -- this wins a day with
9032 buffers which have very long lines. */
9033 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9034 back_to_previous_visible_line_start (it);
9036 /* Reseat the iterator here. When moving backward, we don't want
9037 reseat to skip forward over invisible text, set up the iterator
9038 to deliver from overlay strings at the new position etc. So,
9039 use reseat_1 here. */
9040 reseat_1 (it, it->current.pos, 1);
9042 /* We are now surely at a line start. */
9043 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9044 reordering is in effect. */
9045 it->continuation_lines_width = 0;
9047 /* Move forward and see what y-distance we moved. First move to the
9048 start of the next line so that we get its height. We need this
9049 height to be able to tell whether we reached the specified
9050 y-distance. */
9051 SAVE_IT (it2, *it, it2data);
9052 it2.max_ascent = it2.max_descent = 0;
9055 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9056 MOVE_TO_POS | MOVE_TO_VPOS);
9058 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9059 /* If we are in a display string which starts at START_POS,
9060 and that display string includes a newline, and we are
9061 right after that newline (i.e. at the beginning of a
9062 display line), exit the loop, because otherwise we will
9063 infloop, since move_it_to will see that it is already at
9064 START_POS and will not move. */
9065 || (it2.method == GET_FROM_STRING
9066 && IT_CHARPOS (it2) == start_pos
9067 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9068 eassert (IT_CHARPOS (*it) >= BEGV);
9069 SAVE_IT (it3, it2, it3data);
9071 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9072 eassert (IT_CHARPOS (*it) >= BEGV);
9073 /* H is the actual vertical distance from the position in *IT
9074 and the starting position. */
9075 h = it2.current_y - it->current_y;
9076 /* NLINES is the distance in number of lines. */
9077 nlines = it2.vpos - it->vpos;
9079 /* Correct IT's y and vpos position
9080 so that they are relative to the starting point. */
9081 it->vpos -= nlines;
9082 it->current_y -= h;
9084 if (dy == 0)
9086 /* DY == 0 means move to the start of the screen line. The
9087 value of nlines is > 0 if continuation lines were involved,
9088 or if the original IT position was at start of a line. */
9089 RESTORE_IT (it, it, it2data);
9090 if (nlines > 0)
9091 move_it_by_lines (it, nlines);
9092 /* The above code moves us to some position NLINES down,
9093 usually to its first glyph (leftmost in an L2R line), but
9094 that's not necessarily the start of the line, under bidi
9095 reordering. We want to get to the character position
9096 that is immediately after the newline of the previous
9097 line. */
9098 if (it->bidi_p
9099 && !it->continuation_lines_width
9100 && !STRINGP (it->string)
9101 && IT_CHARPOS (*it) > BEGV
9102 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9104 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9106 DEC_BOTH (cp, bp);
9107 cp = find_newline_no_quit (cp, bp, -1, NULL);
9108 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9110 bidi_unshelve_cache (it3data, 1);
9112 else
9114 /* The y-position we try to reach, relative to *IT.
9115 Note that H has been subtracted in front of the if-statement. */
9116 int target_y = it->current_y + h - dy;
9117 int y0 = it3.current_y;
9118 int y1;
9119 int line_height;
9121 RESTORE_IT (&it3, &it3, it3data);
9122 y1 = line_bottom_y (&it3);
9123 line_height = y1 - y0;
9124 RESTORE_IT (it, it, it2data);
9125 /* If we did not reach target_y, try to move further backward if
9126 we can. If we moved too far backward, try to move forward. */
9127 if (target_y < it->current_y
9128 /* This is heuristic. In a window that's 3 lines high, with
9129 a line height of 13 pixels each, recentering with point
9130 on the bottom line will try to move -39/2 = 19 pixels
9131 backward. Try to avoid moving into the first line. */
9132 && (it->current_y - target_y
9133 > min (window_box_height (it->w), line_height * 2 / 3))
9134 && IT_CHARPOS (*it) > BEGV)
9136 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9137 target_y - it->current_y));
9138 dy = it->current_y - target_y;
9139 goto move_further_back;
9141 else if (target_y >= it->current_y + line_height
9142 && IT_CHARPOS (*it) < ZV)
9144 /* Should move forward by at least one line, maybe more.
9146 Note: Calling move_it_by_lines can be expensive on
9147 terminal frames, where compute_motion is used (via
9148 vmotion) to do the job, when there are very long lines
9149 and truncate-lines is nil. That's the reason for
9150 treating terminal frames specially here. */
9152 if (!FRAME_WINDOW_P (it->f))
9153 move_it_vertically (it, target_y - (it->current_y + line_height));
9154 else
9158 move_it_by_lines (it, 1);
9160 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9167 /* Move IT by a specified amount of pixel lines DY. DY negative means
9168 move backwards. DY = 0 means move to start of screen line. At the
9169 end, IT will be on the start of a screen line. */
9171 void
9172 move_it_vertically (struct it *it, int dy)
9174 if (dy <= 0)
9175 move_it_vertically_backward (it, -dy);
9176 else
9178 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9179 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9180 MOVE_TO_POS | MOVE_TO_Y);
9181 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9183 /* If buffer ends in ZV without a newline, move to the start of
9184 the line to satisfy the post-condition. */
9185 if (IT_CHARPOS (*it) == ZV
9186 && ZV > BEGV
9187 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9188 move_it_by_lines (it, 0);
9193 /* Move iterator IT past the end of the text line it is in. */
9195 void
9196 move_it_past_eol (struct it *it)
9198 enum move_it_result rc;
9200 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9201 if (rc == MOVE_NEWLINE_OR_CR)
9202 set_iterator_to_next (it, 0);
9206 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9207 negative means move up. DVPOS == 0 means move to the start of the
9208 screen line.
9210 Optimization idea: If we would know that IT->f doesn't use
9211 a face with proportional font, we could be faster for
9212 truncate-lines nil. */
9214 void
9215 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9218 /* The commented-out optimization uses vmotion on terminals. This
9219 gives bad results, because elements like it->what, on which
9220 callers such as pos_visible_p rely, aren't updated. */
9221 /* struct position pos;
9222 if (!FRAME_WINDOW_P (it->f))
9224 struct text_pos textpos;
9226 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9227 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9228 reseat (it, textpos, 1);
9229 it->vpos += pos.vpos;
9230 it->current_y += pos.vpos;
9232 else */
9234 if (dvpos == 0)
9236 /* DVPOS == 0 means move to the start of the screen line. */
9237 move_it_vertically_backward (it, 0);
9238 /* Let next call to line_bottom_y calculate real line height */
9239 last_height = 0;
9241 else if (dvpos > 0)
9243 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9244 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9246 /* Only move to the next buffer position if we ended up in a
9247 string from display property, not in an overlay string
9248 (before-string or after-string). That is because the
9249 latter don't conceal the underlying buffer position, so
9250 we can ask to move the iterator to the exact position we
9251 are interested in. Note that, even if we are already at
9252 IT_CHARPOS (*it), the call below is not a no-op, as it
9253 will detect that we are at the end of the string, pop the
9254 iterator, and compute it->current_x and it->hpos
9255 correctly. */
9256 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9257 -1, -1, -1, MOVE_TO_POS);
9260 else
9262 struct it it2;
9263 void *it2data = NULL;
9264 ptrdiff_t start_charpos, i;
9265 int nchars_per_row
9266 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9267 ptrdiff_t pos_limit;
9269 /* Start at the beginning of the screen line containing IT's
9270 position. This may actually move vertically backwards,
9271 in case of overlays, so adjust dvpos accordingly. */
9272 dvpos += it->vpos;
9273 move_it_vertically_backward (it, 0);
9274 dvpos -= it->vpos;
9276 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9277 screen lines, and reseat the iterator there. */
9278 start_charpos = IT_CHARPOS (*it);
9279 if (it->line_wrap == TRUNCATE)
9280 pos_limit = BEGV;
9281 else
9282 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9283 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9284 back_to_previous_visible_line_start (it);
9285 reseat (it, it->current.pos, 1);
9287 /* Move further back if we end up in a string or an image. */
9288 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9290 /* First try to move to start of display line. */
9291 dvpos += it->vpos;
9292 move_it_vertically_backward (it, 0);
9293 dvpos -= it->vpos;
9294 if (IT_POS_VALID_AFTER_MOVE_P (it))
9295 break;
9296 /* If start of line is still in string or image,
9297 move further back. */
9298 back_to_previous_visible_line_start (it);
9299 reseat (it, it->current.pos, 1);
9300 dvpos--;
9303 it->current_x = it->hpos = 0;
9305 /* Above call may have moved too far if continuation lines
9306 are involved. Scan forward and see if it did. */
9307 SAVE_IT (it2, *it, it2data);
9308 it2.vpos = it2.current_y = 0;
9309 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9310 it->vpos -= it2.vpos;
9311 it->current_y -= it2.current_y;
9312 it->current_x = it->hpos = 0;
9314 /* If we moved too far back, move IT some lines forward. */
9315 if (it2.vpos > -dvpos)
9317 int delta = it2.vpos + dvpos;
9319 RESTORE_IT (&it2, &it2, it2data);
9320 SAVE_IT (it2, *it, it2data);
9321 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9322 /* Move back again if we got too far ahead. */
9323 if (IT_CHARPOS (*it) >= start_charpos)
9324 RESTORE_IT (it, &it2, it2data);
9325 else
9326 bidi_unshelve_cache (it2data, 1);
9328 else
9329 RESTORE_IT (it, it, it2data);
9333 /* Return 1 if IT points into the middle of a display vector. */
9336 in_display_vector_p (struct it *it)
9338 return (it->method == GET_FROM_DISPLAY_VECTOR
9339 && it->current.dpvec_index > 0
9340 && it->dpvec + it->current.dpvec_index != it->dpend);
9344 /***********************************************************************
9345 Messages
9346 ***********************************************************************/
9349 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9350 to *Messages*. */
9352 void
9353 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9355 Lisp_Object args[3];
9356 Lisp_Object msg, fmt;
9357 char *buffer;
9358 ptrdiff_t len;
9359 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9360 USE_SAFE_ALLOCA;
9362 fmt = msg = Qnil;
9363 GCPRO4 (fmt, msg, arg1, arg2);
9365 args[0] = fmt = build_string (format);
9366 args[1] = arg1;
9367 args[2] = arg2;
9368 msg = Fformat (3, args);
9370 len = SBYTES (msg) + 1;
9371 buffer = SAFE_ALLOCA (len);
9372 memcpy (buffer, SDATA (msg), len);
9374 message_dolog (buffer, len - 1, 1, 0);
9375 SAFE_FREE ();
9377 UNGCPRO;
9381 /* Output a newline in the *Messages* buffer if "needs" one. */
9383 void
9384 message_log_maybe_newline (void)
9386 if (message_log_need_newline)
9387 message_dolog ("", 0, 1, 0);
9391 /* Add a string M of length NBYTES to the message log, optionally
9392 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9393 true, means interpret the contents of M as multibyte. This
9394 function calls low-level routines in order to bypass text property
9395 hooks, etc. which might not be safe to run.
9397 This may GC (insert may run before/after change hooks),
9398 so the buffer M must NOT point to a Lisp string. */
9400 void
9401 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9403 const unsigned char *msg = (const unsigned char *) m;
9405 if (!NILP (Vmemory_full))
9406 return;
9408 if (!NILP (Vmessage_log_max))
9410 struct buffer *oldbuf;
9411 Lisp_Object oldpoint, oldbegv, oldzv;
9412 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9413 ptrdiff_t point_at_end = 0;
9414 ptrdiff_t zv_at_end = 0;
9415 Lisp_Object old_deactivate_mark;
9416 bool shown;
9417 struct gcpro gcpro1;
9419 old_deactivate_mark = Vdeactivate_mark;
9420 oldbuf = current_buffer;
9421 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9422 bset_undo_list (current_buffer, Qt);
9424 oldpoint = message_dolog_marker1;
9425 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9426 oldbegv = message_dolog_marker2;
9427 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9428 oldzv = message_dolog_marker3;
9429 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9430 GCPRO1 (old_deactivate_mark);
9432 if (PT == Z)
9433 point_at_end = 1;
9434 if (ZV == Z)
9435 zv_at_end = 1;
9437 BEGV = BEG;
9438 BEGV_BYTE = BEG_BYTE;
9439 ZV = Z;
9440 ZV_BYTE = Z_BYTE;
9441 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9443 /* Insert the string--maybe converting multibyte to single byte
9444 or vice versa, so that all the text fits the buffer. */
9445 if (multibyte
9446 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9448 ptrdiff_t i;
9449 int c, char_bytes;
9450 char work[1];
9452 /* Convert a multibyte string to single-byte
9453 for the *Message* buffer. */
9454 for (i = 0; i < nbytes; i += char_bytes)
9456 c = string_char_and_length (msg + i, &char_bytes);
9457 work[0] = (ASCII_CHAR_P (c)
9459 : multibyte_char_to_unibyte (c));
9460 insert_1_both (work, 1, 1, 1, 0, 0);
9463 else if (! multibyte
9464 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9466 ptrdiff_t i;
9467 int c, char_bytes;
9468 unsigned char str[MAX_MULTIBYTE_LENGTH];
9469 /* Convert a single-byte string to multibyte
9470 for the *Message* buffer. */
9471 for (i = 0; i < nbytes; i++)
9473 c = msg[i];
9474 MAKE_CHAR_MULTIBYTE (c);
9475 char_bytes = CHAR_STRING (c, str);
9476 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9479 else if (nbytes)
9480 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9482 if (nlflag)
9484 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9485 printmax_t dups;
9487 insert_1_both ("\n", 1, 1, 1, 0, 0);
9489 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9490 this_bol = PT;
9491 this_bol_byte = PT_BYTE;
9493 /* See if this line duplicates the previous one.
9494 If so, combine duplicates. */
9495 if (this_bol > BEG)
9497 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9498 prev_bol = PT;
9499 prev_bol_byte = PT_BYTE;
9501 dups = message_log_check_duplicate (prev_bol_byte,
9502 this_bol_byte);
9503 if (dups)
9505 del_range_both (prev_bol, prev_bol_byte,
9506 this_bol, this_bol_byte, 0);
9507 if (dups > 1)
9509 char dupstr[sizeof " [ times]"
9510 + INT_STRLEN_BOUND (printmax_t)];
9512 /* If you change this format, don't forget to also
9513 change message_log_check_duplicate. */
9514 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9515 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9516 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9521 /* If we have more than the desired maximum number of lines
9522 in the *Messages* buffer now, delete the oldest ones.
9523 This is safe because we don't have undo in this buffer. */
9525 if (NATNUMP (Vmessage_log_max))
9527 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9528 -XFASTINT (Vmessage_log_max) - 1, 0);
9529 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9532 BEGV = marker_position (oldbegv);
9533 BEGV_BYTE = marker_byte_position (oldbegv);
9535 if (zv_at_end)
9537 ZV = Z;
9538 ZV_BYTE = Z_BYTE;
9540 else
9542 ZV = marker_position (oldzv);
9543 ZV_BYTE = marker_byte_position (oldzv);
9546 if (point_at_end)
9547 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9548 else
9549 /* We can't do Fgoto_char (oldpoint) because it will run some
9550 Lisp code. */
9551 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9552 marker_byte_position (oldpoint));
9554 UNGCPRO;
9555 unchain_marker (XMARKER (oldpoint));
9556 unchain_marker (XMARKER (oldbegv));
9557 unchain_marker (XMARKER (oldzv));
9559 shown = buffer_window_count (current_buffer) > 0;
9560 set_buffer_internal (oldbuf);
9561 /* We called insert_1_both above with its 5th argument (PREPARE)
9562 zero, which prevents insert_1_both from calling
9563 prepare_to_modify_buffer, which in turns prevents us from
9564 incrementing windows_or_buffers_changed even if *Messages* is
9565 shown in some window. So we must manually incrementing
9566 windows_or_buffers_changed here to make up for that. */
9567 if (shown)
9568 windows_or_buffers_changed++;
9569 else
9570 windows_or_buffers_changed = old_windows_or_buffers_changed;
9571 message_log_need_newline = !nlflag;
9572 Vdeactivate_mark = old_deactivate_mark;
9577 /* We are at the end of the buffer after just having inserted a newline.
9578 (Note: We depend on the fact we won't be crossing the gap.)
9579 Check to see if the most recent message looks a lot like the previous one.
9580 Return 0 if different, 1 if the new one should just replace it, or a
9581 value N > 1 if we should also append " [N times]". */
9583 static intmax_t
9584 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9586 ptrdiff_t i;
9587 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9588 int seen_dots = 0;
9589 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9590 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9592 for (i = 0; i < len; i++)
9594 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9595 seen_dots = 1;
9596 if (p1[i] != p2[i])
9597 return seen_dots;
9599 p1 += len;
9600 if (*p1 == '\n')
9601 return 2;
9602 if (*p1++ == ' ' && *p1++ == '[')
9604 char *pend;
9605 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9606 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9607 return n + 1;
9609 return 0;
9613 /* Display an echo area message M with a specified length of NBYTES
9614 bytes. The string may include null characters. If M is not a
9615 string, clear out any existing message, and let the mini-buffer
9616 text show through.
9618 This function cancels echoing. */
9620 void
9621 message3 (Lisp_Object m)
9623 struct gcpro gcpro1;
9625 GCPRO1 (m);
9626 clear_message (1,1);
9627 cancel_echoing ();
9629 /* First flush out any partial line written with print. */
9630 message_log_maybe_newline ();
9631 if (STRINGP (m))
9633 ptrdiff_t nbytes = SBYTES (m);
9634 bool multibyte = STRING_MULTIBYTE (m);
9635 USE_SAFE_ALLOCA;
9636 char *buffer = SAFE_ALLOCA (nbytes);
9637 memcpy (buffer, SDATA (m), nbytes);
9638 message_dolog (buffer, nbytes, 1, multibyte);
9639 SAFE_FREE ();
9641 message3_nolog (m);
9643 UNGCPRO;
9647 /* The non-logging version of message3.
9648 This does not cancel echoing, because it is used for echoing.
9649 Perhaps we need to make a separate function for echoing
9650 and make this cancel echoing. */
9652 void
9653 message3_nolog (Lisp_Object m)
9655 struct frame *sf = SELECTED_FRAME ();
9657 if (FRAME_INITIAL_P (sf))
9659 if (noninteractive_need_newline)
9660 putc ('\n', stderr);
9661 noninteractive_need_newline = 0;
9662 if (STRINGP (m))
9663 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9664 if (cursor_in_echo_area == 0)
9665 fprintf (stderr, "\n");
9666 fflush (stderr);
9668 /* Error messages get reported properly by cmd_error, so this must be just an
9669 informative message; if the frame hasn't really been initialized yet, just
9670 toss it. */
9671 else if (INTERACTIVE && sf->glyphs_initialized_p)
9673 /* Get the frame containing the mini-buffer
9674 that the selected frame is using. */
9675 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9676 Lisp_Object frame = XWINDOW (mini_window)->frame;
9677 struct frame *f = XFRAME (frame);
9679 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9680 Fmake_frame_visible (frame);
9682 if (STRINGP (m) && SCHARS (m) > 0)
9684 set_message (m);
9685 if (minibuffer_auto_raise)
9686 Fraise_frame (frame);
9687 /* Assume we are not echoing.
9688 (If we are, echo_now will override this.) */
9689 echo_message_buffer = Qnil;
9691 else
9692 clear_message (1, 1);
9694 do_pending_window_change (0);
9695 echo_area_display (1);
9696 do_pending_window_change (0);
9697 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9698 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9703 /* Display a null-terminated echo area message M. If M is 0, clear
9704 out any existing message, and let the mini-buffer text show through.
9706 The buffer M must continue to exist until after the echo area gets
9707 cleared or some other message gets displayed there. Do not pass
9708 text that is stored in a Lisp string. Do not pass text in a buffer
9709 that was alloca'd. */
9711 void
9712 message1 (const char *m)
9714 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9718 /* The non-logging counterpart of message1. */
9720 void
9721 message1_nolog (const char *m)
9723 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9726 /* Display a message M which contains a single %s
9727 which gets replaced with STRING. */
9729 void
9730 message_with_string (const char *m, Lisp_Object string, int log)
9732 CHECK_STRING (string);
9734 if (noninteractive)
9736 if (m)
9738 if (noninteractive_need_newline)
9739 putc ('\n', stderr);
9740 noninteractive_need_newline = 0;
9741 fprintf (stderr, m, SDATA (string));
9742 if (!cursor_in_echo_area)
9743 fprintf (stderr, "\n");
9744 fflush (stderr);
9747 else if (INTERACTIVE)
9749 /* The frame whose minibuffer we're going to display the message on.
9750 It may be larger than the selected frame, so we need
9751 to use its buffer, not the selected frame's buffer. */
9752 Lisp_Object mini_window;
9753 struct frame *f, *sf = SELECTED_FRAME ();
9755 /* Get the frame containing the minibuffer
9756 that the selected frame is using. */
9757 mini_window = FRAME_MINIBUF_WINDOW (sf);
9758 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9760 /* Error messages get reported properly by cmd_error, so this must be
9761 just an informative message; if the frame hasn't really been
9762 initialized yet, just toss it. */
9763 if (f->glyphs_initialized_p)
9765 Lisp_Object args[2], msg;
9766 struct gcpro gcpro1, gcpro2;
9768 args[0] = build_string (m);
9769 args[1] = msg = string;
9770 GCPRO2 (args[0], msg);
9771 gcpro1.nvars = 2;
9773 msg = Fformat (2, args);
9775 if (log)
9776 message3 (msg);
9777 else
9778 message3_nolog (msg);
9780 UNGCPRO;
9782 /* Print should start at the beginning of the message
9783 buffer next time. */
9784 message_buf_print = 0;
9790 /* Dump an informative message to the minibuf. If M is 0, clear out
9791 any existing message, and let the mini-buffer text show through. */
9793 static void
9794 vmessage (const char *m, va_list ap)
9796 if (noninteractive)
9798 if (m)
9800 if (noninteractive_need_newline)
9801 putc ('\n', stderr);
9802 noninteractive_need_newline = 0;
9803 vfprintf (stderr, m, ap);
9804 if (cursor_in_echo_area == 0)
9805 fprintf (stderr, "\n");
9806 fflush (stderr);
9809 else if (INTERACTIVE)
9811 /* The frame whose mini-buffer we're going to display the message
9812 on. It may be larger than the selected frame, so we need to
9813 use its buffer, not the selected frame's buffer. */
9814 Lisp_Object mini_window;
9815 struct frame *f, *sf = SELECTED_FRAME ();
9817 /* Get the frame containing the mini-buffer
9818 that the selected frame is using. */
9819 mini_window = FRAME_MINIBUF_WINDOW (sf);
9820 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9822 /* Error messages get reported properly by cmd_error, so this must be
9823 just an informative message; if the frame hasn't really been
9824 initialized yet, just toss it. */
9825 if (f->glyphs_initialized_p)
9827 if (m)
9829 ptrdiff_t len;
9830 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9831 char *message_buf = alloca (maxsize + 1);
9833 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9835 message3 (make_string (message_buf, len));
9837 else
9838 message1 (0);
9840 /* Print should start at the beginning of the message
9841 buffer next time. */
9842 message_buf_print = 0;
9847 void
9848 message (const char *m, ...)
9850 va_list ap;
9851 va_start (ap, m);
9852 vmessage (m, ap);
9853 va_end (ap);
9857 #if 0
9858 /* The non-logging version of message. */
9860 void
9861 message_nolog (const char *m, ...)
9863 Lisp_Object old_log_max;
9864 va_list ap;
9865 va_start (ap, m);
9866 old_log_max = Vmessage_log_max;
9867 Vmessage_log_max = Qnil;
9868 vmessage (m, ap);
9869 Vmessage_log_max = old_log_max;
9870 va_end (ap);
9872 #endif
9875 /* Display the current message in the current mini-buffer. This is
9876 only called from error handlers in process.c, and is not time
9877 critical. */
9879 void
9880 update_echo_area (void)
9882 if (!NILP (echo_area_buffer[0]))
9884 Lisp_Object string;
9885 string = Fcurrent_message ();
9886 message3 (string);
9891 /* Make sure echo area buffers in `echo_buffers' are live.
9892 If they aren't, make new ones. */
9894 static void
9895 ensure_echo_area_buffers (void)
9897 int i;
9899 for (i = 0; i < 2; ++i)
9900 if (!BUFFERP (echo_buffer[i])
9901 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9903 char name[30];
9904 Lisp_Object old_buffer;
9905 int j;
9907 old_buffer = echo_buffer[i];
9908 echo_buffer[i] = Fget_buffer_create
9909 (make_formatted_string (name, " *Echo Area %d*", i));
9910 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9911 /* to force word wrap in echo area -
9912 it was decided to postpone this*/
9913 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9915 for (j = 0; j < 2; ++j)
9916 if (EQ (old_buffer, echo_area_buffer[j]))
9917 echo_area_buffer[j] = echo_buffer[i];
9922 /* Call FN with args A1..A2 with either the current or last displayed
9923 echo_area_buffer as current buffer.
9925 WHICH zero means use the current message buffer
9926 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9927 from echo_buffer[] and clear it.
9929 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9930 suitable buffer from echo_buffer[] and clear it.
9932 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9933 that the current message becomes the last displayed one, make
9934 choose a suitable buffer for echo_area_buffer[0], and clear it.
9936 Value is what FN returns. */
9938 static int
9939 with_echo_area_buffer (struct window *w, int which,
9940 int (*fn) (ptrdiff_t, Lisp_Object),
9941 ptrdiff_t a1, Lisp_Object a2)
9943 Lisp_Object buffer;
9944 int this_one, the_other, clear_buffer_p, rc;
9945 ptrdiff_t count = SPECPDL_INDEX ();
9947 /* If buffers aren't live, make new ones. */
9948 ensure_echo_area_buffers ();
9950 clear_buffer_p = 0;
9952 if (which == 0)
9953 this_one = 0, the_other = 1;
9954 else if (which > 0)
9955 this_one = 1, the_other = 0;
9956 else
9958 this_one = 0, the_other = 1;
9959 clear_buffer_p = 1;
9961 /* We need a fresh one in case the current echo buffer equals
9962 the one containing the last displayed echo area message. */
9963 if (!NILP (echo_area_buffer[this_one])
9964 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9965 echo_area_buffer[this_one] = Qnil;
9968 /* Choose a suitable buffer from echo_buffer[] is we don't
9969 have one. */
9970 if (NILP (echo_area_buffer[this_one]))
9972 echo_area_buffer[this_one]
9973 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9974 ? echo_buffer[the_other]
9975 : echo_buffer[this_one]);
9976 clear_buffer_p = 1;
9979 buffer = echo_area_buffer[this_one];
9981 /* Don't get confused by reusing the buffer used for echoing
9982 for a different purpose. */
9983 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9984 cancel_echoing ();
9986 record_unwind_protect (unwind_with_echo_area_buffer,
9987 with_echo_area_buffer_unwind_data (w));
9989 /* Make the echo area buffer current. Note that for display
9990 purposes, it is not necessary that the displayed window's buffer
9991 == current_buffer, except for text property lookup. So, let's
9992 only set that buffer temporarily here without doing a full
9993 Fset_window_buffer. We must also change w->pointm, though,
9994 because otherwise an assertions in unshow_buffer fails, and Emacs
9995 aborts. */
9996 set_buffer_internal_1 (XBUFFER (buffer));
9997 if (w)
9999 wset_buffer (w, buffer);
10000 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10003 bset_undo_list (current_buffer, Qt);
10004 bset_read_only (current_buffer, Qnil);
10005 specbind (Qinhibit_read_only, Qt);
10006 specbind (Qinhibit_modification_hooks, Qt);
10008 if (clear_buffer_p && Z > BEG)
10009 del_range (BEG, Z);
10011 eassert (BEGV >= BEG);
10012 eassert (ZV <= Z && ZV >= BEGV);
10014 rc = fn (a1, a2);
10016 eassert (BEGV >= BEG);
10017 eassert (ZV <= Z && ZV >= BEGV);
10019 unbind_to (count, Qnil);
10020 return rc;
10024 /* Save state that should be preserved around the call to the function
10025 FN called in with_echo_area_buffer. */
10027 static Lisp_Object
10028 with_echo_area_buffer_unwind_data (struct window *w)
10030 int i = 0;
10031 Lisp_Object vector, tmp;
10033 /* Reduce consing by keeping one vector in
10034 Vwith_echo_area_save_vector. */
10035 vector = Vwith_echo_area_save_vector;
10036 Vwith_echo_area_save_vector = Qnil;
10038 if (NILP (vector))
10039 vector = Fmake_vector (make_number (9), Qnil);
10041 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10042 ASET (vector, i, Vdeactivate_mark); ++i;
10043 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10045 if (w)
10047 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10048 ASET (vector, i, w->contents); ++i;
10049 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10050 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10051 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10052 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10054 else
10056 int end = i + 6;
10057 for (; i < end; ++i)
10058 ASET (vector, i, Qnil);
10061 eassert (i == ASIZE (vector));
10062 return vector;
10066 /* Restore global state from VECTOR which was created by
10067 with_echo_area_buffer_unwind_data. */
10069 static Lisp_Object
10070 unwind_with_echo_area_buffer (Lisp_Object vector)
10072 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10073 Vdeactivate_mark = AREF (vector, 1);
10074 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10076 if (WINDOWP (AREF (vector, 3)))
10078 struct window *w;
10079 Lisp_Object buffer;
10081 w = XWINDOW (AREF (vector, 3));
10082 buffer = AREF (vector, 4);
10084 wset_buffer (w, buffer);
10085 set_marker_both (w->pointm, buffer,
10086 XFASTINT (AREF (vector, 5)),
10087 XFASTINT (AREF (vector, 6)));
10088 set_marker_both (w->start, buffer,
10089 XFASTINT (AREF (vector, 7)),
10090 XFASTINT (AREF (vector, 8)));
10093 Vwith_echo_area_save_vector = vector;
10094 return Qnil;
10098 /* Set up the echo area for use by print functions. MULTIBYTE_P
10099 non-zero means we will print multibyte. */
10101 void
10102 setup_echo_area_for_printing (int multibyte_p)
10104 /* If we can't find an echo area any more, exit. */
10105 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10106 Fkill_emacs (Qnil);
10108 ensure_echo_area_buffers ();
10110 if (!message_buf_print)
10112 /* A message has been output since the last time we printed.
10113 Choose a fresh echo area buffer. */
10114 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10115 echo_area_buffer[0] = echo_buffer[1];
10116 else
10117 echo_area_buffer[0] = echo_buffer[0];
10119 /* Switch to that buffer and clear it. */
10120 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10121 bset_truncate_lines (current_buffer, Qnil);
10123 if (Z > BEG)
10125 ptrdiff_t count = SPECPDL_INDEX ();
10126 specbind (Qinhibit_read_only, Qt);
10127 /* Note that undo recording is always disabled. */
10128 del_range (BEG, Z);
10129 unbind_to (count, Qnil);
10131 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10133 /* Set up the buffer for the multibyteness we need. */
10134 if (multibyte_p
10135 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10136 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10138 /* Raise the frame containing the echo area. */
10139 if (minibuffer_auto_raise)
10141 struct frame *sf = SELECTED_FRAME ();
10142 Lisp_Object mini_window;
10143 mini_window = FRAME_MINIBUF_WINDOW (sf);
10144 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10147 message_log_maybe_newline ();
10148 message_buf_print = 1;
10150 else
10152 if (NILP (echo_area_buffer[0]))
10154 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10155 echo_area_buffer[0] = echo_buffer[1];
10156 else
10157 echo_area_buffer[0] = echo_buffer[0];
10160 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10162 /* Someone switched buffers between print requests. */
10163 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10164 bset_truncate_lines (current_buffer, Qnil);
10170 /* Display an echo area message in window W. Value is non-zero if W's
10171 height is changed. If display_last_displayed_message_p is
10172 non-zero, display the message that was last displayed, otherwise
10173 display the current message. */
10175 static int
10176 display_echo_area (struct window *w)
10178 int i, no_message_p, window_height_changed_p;
10180 /* Temporarily disable garbage collections while displaying the echo
10181 area. This is done because a GC can print a message itself.
10182 That message would modify the echo area buffer's contents while a
10183 redisplay of the buffer is going on, and seriously confuse
10184 redisplay. */
10185 ptrdiff_t count = inhibit_garbage_collection ();
10187 /* If there is no message, we must call display_echo_area_1
10188 nevertheless because it resizes the window. But we will have to
10189 reset the echo_area_buffer in question to nil at the end because
10190 with_echo_area_buffer will sets it to an empty buffer. */
10191 i = display_last_displayed_message_p ? 1 : 0;
10192 no_message_p = NILP (echo_area_buffer[i]);
10194 window_height_changed_p
10195 = with_echo_area_buffer (w, display_last_displayed_message_p,
10196 display_echo_area_1,
10197 (intptr_t) w, Qnil);
10199 if (no_message_p)
10200 echo_area_buffer[i] = Qnil;
10202 unbind_to (count, Qnil);
10203 return window_height_changed_p;
10207 /* Helper for display_echo_area. Display the current buffer which
10208 contains the current echo area message in window W, a mini-window,
10209 a pointer to which is passed in A1. A2..A4 are currently not used.
10210 Change the height of W so that all of the message is displayed.
10211 Value is non-zero if height of W was changed. */
10213 static int
10214 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10216 intptr_t i1 = a1;
10217 struct window *w = (struct window *) i1;
10218 Lisp_Object window;
10219 struct text_pos start;
10220 int window_height_changed_p = 0;
10222 /* Do this before displaying, so that we have a large enough glyph
10223 matrix for the display. If we can't get enough space for the
10224 whole text, display the last N lines. That works by setting w->start. */
10225 window_height_changed_p = resize_mini_window (w, 0);
10227 /* Use the starting position chosen by resize_mini_window. */
10228 SET_TEXT_POS_FROM_MARKER (start, w->start);
10230 /* Display. */
10231 clear_glyph_matrix (w->desired_matrix);
10232 XSETWINDOW (window, w);
10233 try_window (window, start, 0);
10235 return window_height_changed_p;
10239 /* Resize the echo area window to exactly the size needed for the
10240 currently displayed message, if there is one. If a mini-buffer
10241 is active, don't shrink it. */
10243 void
10244 resize_echo_area_exactly (void)
10246 if (BUFFERP (echo_area_buffer[0])
10247 && WINDOWP (echo_area_window))
10249 struct window *w = XWINDOW (echo_area_window);
10250 int resized_p;
10251 Lisp_Object resize_exactly;
10253 if (minibuf_level == 0)
10254 resize_exactly = Qt;
10255 else
10256 resize_exactly = Qnil;
10258 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10259 (intptr_t) w, resize_exactly);
10260 if (resized_p)
10262 ++windows_or_buffers_changed;
10263 ++update_mode_lines;
10264 redisplay_internal ();
10270 /* Callback function for with_echo_area_buffer, when used from
10271 resize_echo_area_exactly. A1 contains a pointer to the window to
10272 resize, EXACTLY non-nil means resize the mini-window exactly to the
10273 size of the text displayed. A3 and A4 are not used. Value is what
10274 resize_mini_window returns. */
10276 static int
10277 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10279 intptr_t i1 = a1;
10280 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10284 /* Resize mini-window W to fit the size of its contents. EXACT_P
10285 means size the window exactly to the size needed. Otherwise, it's
10286 only enlarged until W's buffer is empty.
10288 Set W->start to the right place to begin display. If the whole
10289 contents fit, start at the beginning. Otherwise, start so as
10290 to make the end of the contents appear. This is particularly
10291 important for y-or-n-p, but seems desirable generally.
10293 Value is non-zero if the window height has been changed. */
10296 resize_mini_window (struct window *w, int exact_p)
10298 struct frame *f = XFRAME (w->frame);
10299 int window_height_changed_p = 0;
10301 eassert (MINI_WINDOW_P (w));
10303 /* By default, start display at the beginning. */
10304 set_marker_both (w->start, w->contents,
10305 BUF_BEGV (XBUFFER (w->contents)),
10306 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10308 /* Don't resize windows while redisplaying a window; it would
10309 confuse redisplay functions when the size of the window they are
10310 displaying changes from under them. Such a resizing can happen,
10311 for instance, when which-func prints a long message while
10312 we are running fontification-functions. We're running these
10313 functions with safe_call which binds inhibit-redisplay to t. */
10314 if (!NILP (Vinhibit_redisplay))
10315 return 0;
10317 /* Nil means don't try to resize. */
10318 if (NILP (Vresize_mini_windows)
10319 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10320 return 0;
10322 if (!FRAME_MINIBUF_ONLY_P (f))
10324 struct it it;
10325 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10326 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10327 int height;
10328 EMACS_INT max_height;
10329 int unit = FRAME_LINE_HEIGHT (f);
10330 struct text_pos start;
10331 struct buffer *old_current_buffer = NULL;
10333 if (current_buffer != XBUFFER (w->contents))
10335 old_current_buffer = current_buffer;
10336 set_buffer_internal (XBUFFER (w->contents));
10339 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10341 /* Compute the max. number of lines specified by the user. */
10342 if (FLOATP (Vmax_mini_window_height))
10343 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10344 else if (INTEGERP (Vmax_mini_window_height))
10345 max_height = XINT (Vmax_mini_window_height);
10346 else
10347 max_height = total_height / 4;
10349 /* Correct that max. height if it's bogus. */
10350 max_height = clip_to_bounds (1, max_height, total_height);
10352 /* Find out the height of the text in the window. */
10353 if (it.line_wrap == TRUNCATE)
10354 height = 1;
10355 else
10357 last_height = 0;
10358 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10359 if (it.max_ascent == 0 && it.max_descent == 0)
10360 height = it.current_y + last_height;
10361 else
10362 height = it.current_y + it.max_ascent + it.max_descent;
10363 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10364 height = (height + unit - 1) / unit;
10367 /* Compute a suitable window start. */
10368 if (height > max_height)
10370 height = max_height;
10371 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10372 move_it_vertically_backward (&it, (height - 1) * unit);
10373 start = it.current.pos;
10375 else
10376 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10377 SET_MARKER_FROM_TEXT_POS (w->start, start);
10379 if (EQ (Vresize_mini_windows, Qgrow_only))
10381 /* Let it grow only, until we display an empty message, in which
10382 case the window shrinks again. */
10383 if (height > WINDOW_TOTAL_LINES (w))
10385 int old_height = WINDOW_TOTAL_LINES (w);
10386 freeze_window_starts (f, 1);
10387 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10388 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10390 else if (height < WINDOW_TOTAL_LINES (w)
10391 && (exact_p || BEGV == ZV))
10393 int old_height = WINDOW_TOTAL_LINES (w);
10394 freeze_window_starts (f, 0);
10395 shrink_mini_window (w);
10396 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10399 else
10401 /* Always resize to exact size needed. */
10402 if (height > WINDOW_TOTAL_LINES (w))
10404 int old_height = WINDOW_TOTAL_LINES (w);
10405 freeze_window_starts (f, 1);
10406 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10407 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10409 else if (height < WINDOW_TOTAL_LINES (w))
10411 int old_height = WINDOW_TOTAL_LINES (w);
10412 freeze_window_starts (f, 0);
10413 shrink_mini_window (w);
10415 if (height)
10417 freeze_window_starts (f, 1);
10418 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10421 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10425 if (old_current_buffer)
10426 set_buffer_internal (old_current_buffer);
10429 return window_height_changed_p;
10433 /* Value is the current message, a string, or nil if there is no
10434 current message. */
10436 Lisp_Object
10437 current_message (void)
10439 Lisp_Object msg;
10441 if (!BUFFERP (echo_area_buffer[0]))
10442 msg = Qnil;
10443 else
10445 with_echo_area_buffer (0, 0, current_message_1,
10446 (intptr_t) &msg, Qnil);
10447 if (NILP (msg))
10448 echo_area_buffer[0] = Qnil;
10451 return msg;
10455 static int
10456 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10458 intptr_t i1 = a1;
10459 Lisp_Object *msg = (Lisp_Object *) i1;
10461 if (Z > BEG)
10462 *msg = make_buffer_string (BEG, Z, 1);
10463 else
10464 *msg = Qnil;
10465 return 0;
10469 /* Push the current message on Vmessage_stack for later restoration
10470 by restore_message. Value is non-zero if the current message isn't
10471 empty. This is a relatively infrequent operation, so it's not
10472 worth optimizing. */
10474 bool
10475 push_message (void)
10477 Lisp_Object msg = current_message ();
10478 Vmessage_stack = Fcons (msg, Vmessage_stack);
10479 return STRINGP (msg);
10483 /* Restore message display from the top of Vmessage_stack. */
10485 void
10486 restore_message (void)
10488 eassert (CONSP (Vmessage_stack));
10489 message3_nolog (XCAR (Vmessage_stack));
10493 /* Handler for record_unwind_protect calling pop_message. */
10495 Lisp_Object
10496 pop_message_unwind (Lisp_Object dummy)
10498 pop_message ();
10499 return Qnil;
10502 /* Pop the top-most entry off Vmessage_stack. */
10504 static void
10505 pop_message (void)
10507 eassert (CONSP (Vmessage_stack));
10508 Vmessage_stack = XCDR (Vmessage_stack);
10512 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10513 exits. If the stack is not empty, we have a missing pop_message
10514 somewhere. */
10516 void
10517 check_message_stack (void)
10519 if (!NILP (Vmessage_stack))
10520 emacs_abort ();
10524 /* Truncate to NCHARS what will be displayed in the echo area the next
10525 time we display it---but don't redisplay it now. */
10527 void
10528 truncate_echo_area (ptrdiff_t nchars)
10530 if (nchars == 0)
10531 echo_area_buffer[0] = Qnil;
10532 else if (!noninteractive
10533 && INTERACTIVE
10534 && !NILP (echo_area_buffer[0]))
10536 struct frame *sf = SELECTED_FRAME ();
10537 /* Error messages get reported properly by cmd_error, so this must be
10538 just an informative message; if the frame hasn't really been
10539 initialized yet, just toss it. */
10540 if (sf->glyphs_initialized_p)
10541 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10546 /* Helper function for truncate_echo_area. Truncate the current
10547 message to at most NCHARS characters. */
10549 static int
10550 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10552 if (BEG + nchars < Z)
10553 del_range (BEG + nchars, Z);
10554 if (Z == BEG)
10555 echo_area_buffer[0] = Qnil;
10556 return 0;
10559 /* Set the current message to STRING. */
10561 static void
10562 set_message (Lisp_Object string)
10564 eassert (STRINGP (string));
10566 message_enable_multibyte = STRING_MULTIBYTE (string);
10568 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10569 message_buf_print = 0;
10570 help_echo_showing_p = 0;
10572 if (STRINGP (Vdebug_on_message)
10573 && STRINGP (string)
10574 && fast_string_match (Vdebug_on_message, string) >= 0)
10575 call_debugger (list2 (Qerror, string));
10579 /* Helper function for set_message. First argument is ignored and second
10580 argument has the same meaning as for set_message.
10581 This function is called with the echo area buffer being current. */
10583 static int
10584 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10586 eassert (STRINGP (string));
10588 /* Change multibyteness of the echo buffer appropriately. */
10589 if (message_enable_multibyte
10590 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10591 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10593 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10594 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10595 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10597 /* Insert new message at BEG. */
10598 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10600 /* This function takes care of single/multibyte conversion.
10601 We just have to ensure that the echo area buffer has the right
10602 setting of enable_multibyte_characters. */
10603 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10605 return 0;
10609 /* Clear messages. CURRENT_P non-zero means clear the current
10610 message. LAST_DISPLAYED_P non-zero means clear the message
10611 last displayed. */
10613 void
10614 clear_message (int current_p, int last_displayed_p)
10616 if (current_p)
10618 echo_area_buffer[0] = Qnil;
10619 message_cleared_p = 1;
10622 if (last_displayed_p)
10623 echo_area_buffer[1] = Qnil;
10625 message_buf_print = 0;
10628 /* Clear garbaged frames.
10630 This function is used where the old redisplay called
10631 redraw_garbaged_frames which in turn called redraw_frame which in
10632 turn called clear_frame. The call to clear_frame was a source of
10633 flickering. I believe a clear_frame is not necessary. It should
10634 suffice in the new redisplay to invalidate all current matrices,
10635 and ensure a complete redisplay of all windows. */
10637 static void
10638 clear_garbaged_frames (void)
10640 if (frame_garbaged)
10642 Lisp_Object tail, frame;
10643 int changed_count = 0;
10645 FOR_EACH_FRAME (tail, frame)
10647 struct frame *f = XFRAME (frame);
10649 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10651 if (f->resized_p)
10653 redraw_frame (f);
10654 f->force_flush_display_p = 1;
10656 clear_current_matrices (f);
10657 changed_count++;
10658 f->garbaged = 0;
10659 f->resized_p = 0;
10663 frame_garbaged = 0;
10664 if (changed_count)
10665 ++windows_or_buffers_changed;
10670 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10671 is non-zero update selected_frame. Value is non-zero if the
10672 mini-windows height has been changed. */
10674 static int
10675 echo_area_display (int update_frame_p)
10677 Lisp_Object mini_window;
10678 struct window *w;
10679 struct frame *f;
10680 int window_height_changed_p = 0;
10681 struct frame *sf = SELECTED_FRAME ();
10683 mini_window = FRAME_MINIBUF_WINDOW (sf);
10684 w = XWINDOW (mini_window);
10685 f = XFRAME (WINDOW_FRAME (w));
10687 /* Don't display if frame is invisible or not yet initialized. */
10688 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10689 return 0;
10691 #ifdef HAVE_WINDOW_SYSTEM
10692 /* When Emacs starts, selected_frame may be the initial terminal
10693 frame. If we let this through, a message would be displayed on
10694 the terminal. */
10695 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10696 return 0;
10697 #endif /* HAVE_WINDOW_SYSTEM */
10699 /* Redraw garbaged frames. */
10700 clear_garbaged_frames ();
10702 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10704 echo_area_window = mini_window;
10705 window_height_changed_p = display_echo_area (w);
10706 w->must_be_updated_p = 1;
10708 /* Update the display, unless called from redisplay_internal.
10709 Also don't update the screen during redisplay itself. The
10710 update will happen at the end of redisplay, and an update
10711 here could cause confusion. */
10712 if (update_frame_p && !redisplaying_p)
10714 int n = 0;
10716 /* If the display update has been interrupted by pending
10717 input, update mode lines in the frame. Due to the
10718 pending input, it might have been that redisplay hasn't
10719 been called, so that mode lines above the echo area are
10720 garbaged. This looks odd, so we prevent it here. */
10721 if (!display_completed)
10722 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10724 if (window_height_changed_p
10725 /* Don't do this if Emacs is shutting down. Redisplay
10726 needs to run hooks. */
10727 && !NILP (Vrun_hooks))
10729 /* Must update other windows. Likewise as in other
10730 cases, don't let this update be interrupted by
10731 pending input. */
10732 ptrdiff_t count = SPECPDL_INDEX ();
10733 specbind (Qredisplay_dont_pause, Qt);
10734 windows_or_buffers_changed = 1;
10735 redisplay_internal ();
10736 unbind_to (count, Qnil);
10738 else if (FRAME_WINDOW_P (f) && n == 0)
10740 /* Window configuration is the same as before.
10741 Can do with a display update of the echo area,
10742 unless we displayed some mode lines. */
10743 update_single_window (w, 1);
10744 FRAME_RIF (f)->flush_display (f);
10746 else
10747 update_frame (f, 1, 1);
10749 /* If cursor is in the echo area, make sure that the next
10750 redisplay displays the minibuffer, so that the cursor will
10751 be replaced with what the minibuffer wants. */
10752 if (cursor_in_echo_area)
10753 ++windows_or_buffers_changed;
10756 else if (!EQ (mini_window, selected_window))
10757 windows_or_buffers_changed++;
10759 /* Last displayed message is now the current message. */
10760 echo_area_buffer[1] = echo_area_buffer[0];
10761 /* Inform read_char that we're not echoing. */
10762 echo_message_buffer = Qnil;
10764 /* Prevent redisplay optimization in redisplay_internal by resetting
10765 this_line_start_pos. This is done because the mini-buffer now
10766 displays the message instead of its buffer text. */
10767 if (EQ (mini_window, selected_window))
10768 CHARPOS (this_line_start_pos) = 0;
10770 return window_height_changed_p;
10773 /* Nonzero if the current window's buffer is shown in more than one
10774 window and was modified since last redisplay. */
10776 static int
10777 buffer_shared_and_changed (void)
10779 return (buffer_window_count (current_buffer) > 1
10780 && UNCHANGED_MODIFIED < MODIFF);
10783 /* Nonzero if W doesn't reflect the actual state of current buffer due
10784 to its text or overlays change. FIXME: this may be called when
10785 XBUFFER (w->contents) != current_buffer, which looks suspicious. */
10787 static int
10788 window_outdated (struct window *w)
10790 return (w->last_modified < MODIFF
10791 || w->last_overlay_modified < OVERLAY_MODIFF);
10794 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10795 is enabled and mark of W's buffer was changed since last W's update. */
10797 static int
10798 window_buffer_changed (struct window *w)
10800 struct buffer *b = XBUFFER (w->contents);
10802 eassert (BUFFER_LIVE_P (b));
10804 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10805 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10806 != (w->region_showing != 0)));
10809 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10811 static int
10812 mode_line_update_needed (struct window *w)
10814 return (w->column_number_displayed != -1
10815 && !(PT == w->last_point && !window_outdated (w))
10816 && (w->column_number_displayed != current_column ()));
10819 /***********************************************************************
10820 Mode Lines and Frame Titles
10821 ***********************************************************************/
10823 /* A buffer for constructing non-propertized mode-line strings and
10824 frame titles in it; allocated from the heap in init_xdisp and
10825 resized as needed in store_mode_line_noprop_char. */
10827 static char *mode_line_noprop_buf;
10829 /* The buffer's end, and a current output position in it. */
10831 static char *mode_line_noprop_buf_end;
10832 static char *mode_line_noprop_ptr;
10834 #define MODE_LINE_NOPROP_LEN(start) \
10835 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10837 static enum {
10838 MODE_LINE_DISPLAY = 0,
10839 MODE_LINE_TITLE,
10840 MODE_LINE_NOPROP,
10841 MODE_LINE_STRING
10842 } mode_line_target;
10844 /* Alist that caches the results of :propertize.
10845 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10846 static Lisp_Object mode_line_proptrans_alist;
10848 /* List of strings making up the mode-line. */
10849 static Lisp_Object mode_line_string_list;
10851 /* Base face property when building propertized mode line string. */
10852 static Lisp_Object mode_line_string_face;
10853 static Lisp_Object mode_line_string_face_prop;
10856 /* Unwind data for mode line strings */
10858 static Lisp_Object Vmode_line_unwind_vector;
10860 static Lisp_Object
10861 format_mode_line_unwind_data (struct frame *target_frame,
10862 struct buffer *obuf,
10863 Lisp_Object owin,
10864 int save_proptrans)
10866 Lisp_Object vector, tmp;
10868 /* Reduce consing by keeping one vector in
10869 Vwith_echo_area_save_vector. */
10870 vector = Vmode_line_unwind_vector;
10871 Vmode_line_unwind_vector = Qnil;
10873 if (NILP (vector))
10874 vector = Fmake_vector (make_number (10), Qnil);
10876 ASET (vector, 0, make_number (mode_line_target));
10877 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10878 ASET (vector, 2, mode_line_string_list);
10879 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10880 ASET (vector, 4, mode_line_string_face);
10881 ASET (vector, 5, mode_line_string_face_prop);
10883 if (obuf)
10884 XSETBUFFER (tmp, obuf);
10885 else
10886 tmp = Qnil;
10887 ASET (vector, 6, tmp);
10888 ASET (vector, 7, owin);
10889 if (target_frame)
10891 /* Similarly to `with-selected-window', if the operation selects
10892 a window on another frame, we must restore that frame's
10893 selected window, and (for a tty) the top-frame. */
10894 ASET (vector, 8, target_frame->selected_window);
10895 if (FRAME_TERMCAP_P (target_frame))
10896 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10899 return vector;
10902 static Lisp_Object
10903 unwind_format_mode_line (Lisp_Object vector)
10905 Lisp_Object old_window = AREF (vector, 7);
10906 Lisp_Object target_frame_window = AREF (vector, 8);
10907 Lisp_Object old_top_frame = AREF (vector, 9);
10909 mode_line_target = XINT (AREF (vector, 0));
10910 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10911 mode_line_string_list = AREF (vector, 2);
10912 if (! EQ (AREF (vector, 3), Qt))
10913 mode_line_proptrans_alist = AREF (vector, 3);
10914 mode_line_string_face = AREF (vector, 4);
10915 mode_line_string_face_prop = AREF (vector, 5);
10917 /* Select window before buffer, since it may change the buffer. */
10918 if (!NILP (old_window))
10920 /* If the operation that we are unwinding had selected a window
10921 on a different frame, reset its frame-selected-window. For a
10922 text terminal, reset its top-frame if necessary. */
10923 if (!NILP (target_frame_window))
10925 Lisp_Object frame
10926 = WINDOW_FRAME (XWINDOW (target_frame_window));
10928 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10929 Fselect_window (target_frame_window, Qt);
10931 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10932 Fselect_frame (old_top_frame, Qt);
10935 Fselect_window (old_window, Qt);
10938 if (!NILP (AREF (vector, 6)))
10940 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10941 ASET (vector, 6, Qnil);
10944 Vmode_line_unwind_vector = vector;
10945 return Qnil;
10949 /* Store a single character C for the frame title in mode_line_noprop_buf.
10950 Re-allocate mode_line_noprop_buf if necessary. */
10952 static void
10953 store_mode_line_noprop_char (char c)
10955 /* If output position has reached the end of the allocated buffer,
10956 increase the buffer's size. */
10957 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10959 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10960 ptrdiff_t size = len;
10961 mode_line_noprop_buf =
10962 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10963 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10964 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10967 *mode_line_noprop_ptr++ = c;
10971 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10972 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10973 characters that yield more columns than PRECISION; PRECISION <= 0
10974 means copy the whole string. Pad with spaces until FIELD_WIDTH
10975 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10976 pad. Called from display_mode_element when it is used to build a
10977 frame title. */
10979 static int
10980 store_mode_line_noprop (const char *string, int field_width, int precision)
10982 const unsigned char *str = (const unsigned char *) string;
10983 int n = 0;
10984 ptrdiff_t dummy, nbytes;
10986 /* Copy at most PRECISION chars from STR. */
10987 nbytes = strlen (string);
10988 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10989 while (nbytes--)
10990 store_mode_line_noprop_char (*str++);
10992 /* Fill up with spaces until FIELD_WIDTH reached. */
10993 while (field_width > 0
10994 && n < field_width)
10996 store_mode_line_noprop_char (' ');
10997 ++n;
11000 return n;
11003 /***********************************************************************
11004 Frame Titles
11005 ***********************************************************************/
11007 #ifdef HAVE_WINDOW_SYSTEM
11009 /* Set the title of FRAME, if it has changed. The title format is
11010 Vicon_title_format if FRAME is iconified, otherwise it is
11011 frame_title_format. */
11013 static void
11014 x_consider_frame_title (Lisp_Object frame)
11016 struct frame *f = XFRAME (frame);
11018 if (FRAME_WINDOW_P (f)
11019 || FRAME_MINIBUF_ONLY_P (f)
11020 || f->explicit_name)
11022 /* Do we have more than one visible frame on this X display? */
11023 Lisp_Object tail, other_frame, fmt;
11024 ptrdiff_t title_start;
11025 char *title;
11026 ptrdiff_t len;
11027 struct it it;
11028 ptrdiff_t count = SPECPDL_INDEX ();
11030 FOR_EACH_FRAME (tail, other_frame)
11032 struct frame *tf = XFRAME (other_frame);
11034 if (tf != f
11035 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11036 && !FRAME_MINIBUF_ONLY_P (tf)
11037 && !EQ (other_frame, tip_frame)
11038 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11039 break;
11042 /* Set global variable indicating that multiple frames exist. */
11043 multiple_frames = CONSP (tail);
11045 /* Switch to the buffer of selected window of the frame. Set up
11046 mode_line_target so that display_mode_element will output into
11047 mode_line_noprop_buf; then display the title. */
11048 record_unwind_protect (unwind_format_mode_line,
11049 format_mode_line_unwind_data
11050 (f, current_buffer, selected_window, 0));
11052 Fselect_window (f->selected_window, Qt);
11053 set_buffer_internal_1
11054 (XBUFFER (XWINDOW (f->selected_window)->contents));
11055 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11057 mode_line_target = MODE_LINE_TITLE;
11058 title_start = MODE_LINE_NOPROP_LEN (0);
11059 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11060 NULL, DEFAULT_FACE_ID);
11061 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11062 len = MODE_LINE_NOPROP_LEN (title_start);
11063 title = mode_line_noprop_buf + title_start;
11064 unbind_to (count, Qnil);
11066 /* Set the title only if it's changed. This avoids consing in
11067 the common case where it hasn't. (If it turns out that we've
11068 already wasted too much time by walking through the list with
11069 display_mode_element, then we might need to optimize at a
11070 higher level than this.) */
11071 if (! STRINGP (f->name)
11072 || SBYTES (f->name) != len
11073 || memcmp (title, SDATA (f->name), len) != 0)
11074 x_implicitly_set_name (f, make_string (title, len), Qnil);
11078 #endif /* not HAVE_WINDOW_SYSTEM */
11081 /***********************************************************************
11082 Menu Bars
11083 ***********************************************************************/
11086 /* Prepare for redisplay by updating menu-bar item lists when
11087 appropriate. This can call eval. */
11089 void
11090 prepare_menu_bars (void)
11092 int all_windows;
11093 struct gcpro gcpro1, gcpro2;
11094 struct frame *f;
11095 Lisp_Object tooltip_frame;
11097 #ifdef HAVE_WINDOW_SYSTEM
11098 tooltip_frame = tip_frame;
11099 #else
11100 tooltip_frame = Qnil;
11101 #endif
11103 /* Update all frame titles based on their buffer names, etc. We do
11104 this before the menu bars so that the buffer-menu will show the
11105 up-to-date frame titles. */
11106 #ifdef HAVE_WINDOW_SYSTEM
11107 if (windows_or_buffers_changed || update_mode_lines)
11109 Lisp_Object tail, frame;
11111 FOR_EACH_FRAME (tail, frame)
11113 f = XFRAME (frame);
11114 if (!EQ (frame, tooltip_frame)
11115 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11116 x_consider_frame_title (frame);
11119 #endif /* HAVE_WINDOW_SYSTEM */
11121 /* Update the menu bar item lists, if appropriate. This has to be
11122 done before any actual redisplay or generation of display lines. */
11123 all_windows = (update_mode_lines
11124 || buffer_shared_and_changed ()
11125 || windows_or_buffers_changed);
11126 if (all_windows)
11128 Lisp_Object tail, frame;
11129 ptrdiff_t count = SPECPDL_INDEX ();
11130 /* 1 means that update_menu_bar has run its hooks
11131 so any further calls to update_menu_bar shouldn't do so again. */
11132 int menu_bar_hooks_run = 0;
11134 record_unwind_save_match_data ();
11136 FOR_EACH_FRAME (tail, frame)
11138 f = XFRAME (frame);
11140 /* Ignore tooltip frame. */
11141 if (EQ (frame, tooltip_frame))
11142 continue;
11144 /* If a window on this frame changed size, report that to
11145 the user and clear the size-change flag. */
11146 if (FRAME_WINDOW_SIZES_CHANGED (f))
11148 Lisp_Object functions;
11150 /* Clear flag first in case we get an error below. */
11151 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11152 functions = Vwindow_size_change_functions;
11153 GCPRO2 (tail, functions);
11155 while (CONSP (functions))
11157 if (!EQ (XCAR (functions), Qt))
11158 call1 (XCAR (functions), frame);
11159 functions = XCDR (functions);
11161 UNGCPRO;
11164 GCPRO1 (tail);
11165 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11166 #ifdef HAVE_WINDOW_SYSTEM
11167 update_tool_bar (f, 0);
11168 #endif
11169 #ifdef HAVE_NS
11170 if (windows_or_buffers_changed
11171 && FRAME_NS_P (f))
11172 ns_set_doc_edited
11173 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11174 #endif
11175 UNGCPRO;
11178 unbind_to (count, Qnil);
11180 else
11182 struct frame *sf = SELECTED_FRAME ();
11183 update_menu_bar (sf, 1, 0);
11184 #ifdef HAVE_WINDOW_SYSTEM
11185 update_tool_bar (sf, 1);
11186 #endif
11191 /* Update the menu bar item list for frame F. This has to be done
11192 before we start to fill in any display lines, because it can call
11193 eval.
11195 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11197 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11198 already ran the menu bar hooks for this redisplay, so there
11199 is no need to run them again. The return value is the
11200 updated value of this flag, to pass to the next call. */
11202 static int
11203 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11205 Lisp_Object window;
11206 register struct window *w;
11208 /* If called recursively during a menu update, do nothing. This can
11209 happen when, for instance, an activate-menubar-hook causes a
11210 redisplay. */
11211 if (inhibit_menubar_update)
11212 return hooks_run;
11214 window = FRAME_SELECTED_WINDOW (f);
11215 w = XWINDOW (window);
11217 if (FRAME_WINDOW_P (f)
11219 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11220 || defined (HAVE_NS) || defined (USE_GTK)
11221 FRAME_EXTERNAL_MENU_BAR (f)
11222 #else
11223 FRAME_MENU_BAR_LINES (f) > 0
11224 #endif
11225 : FRAME_MENU_BAR_LINES (f) > 0)
11227 /* If the user has switched buffers or windows, we need to
11228 recompute to reflect the new bindings. But we'll
11229 recompute when update_mode_lines is set too; that means
11230 that people can use force-mode-line-update to request
11231 that the menu bar be recomputed. The adverse effect on
11232 the rest of the redisplay algorithm is about the same as
11233 windows_or_buffers_changed anyway. */
11234 if (windows_or_buffers_changed
11235 /* This used to test w->update_mode_line, but we believe
11236 there is no need to recompute the menu in that case. */
11237 || update_mode_lines
11238 || window_buffer_changed (w))
11240 struct buffer *prev = current_buffer;
11241 ptrdiff_t count = SPECPDL_INDEX ();
11243 specbind (Qinhibit_menubar_update, Qt);
11245 set_buffer_internal_1 (XBUFFER (w->contents));
11246 if (save_match_data)
11247 record_unwind_save_match_data ();
11248 if (NILP (Voverriding_local_map_menu_flag))
11250 specbind (Qoverriding_terminal_local_map, Qnil);
11251 specbind (Qoverriding_local_map, Qnil);
11254 if (!hooks_run)
11256 /* Run the Lucid hook. */
11257 safe_run_hooks (Qactivate_menubar_hook);
11259 /* If it has changed current-menubar from previous value,
11260 really recompute the menu-bar from the value. */
11261 if (! NILP (Vlucid_menu_bar_dirty_flag))
11262 call0 (Qrecompute_lucid_menubar);
11264 safe_run_hooks (Qmenu_bar_update_hook);
11266 hooks_run = 1;
11269 XSETFRAME (Vmenu_updating_frame, f);
11270 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11272 /* Redisplay the menu bar in case we changed it. */
11273 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11274 || defined (HAVE_NS) || defined (USE_GTK)
11275 if (FRAME_WINDOW_P (f))
11277 #if defined (HAVE_NS)
11278 /* All frames on Mac OS share the same menubar. So only
11279 the selected frame should be allowed to set it. */
11280 if (f == SELECTED_FRAME ())
11281 #endif
11282 set_frame_menubar (f, 0, 0);
11284 else
11285 /* On a terminal screen, the menu bar is an ordinary screen
11286 line, and this makes it get updated. */
11287 w->update_mode_line = 1;
11288 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11289 /* In the non-toolkit version, the menu bar is an ordinary screen
11290 line, and this makes it get updated. */
11291 w->update_mode_line = 1;
11292 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11294 unbind_to (count, Qnil);
11295 set_buffer_internal_1 (prev);
11299 return hooks_run;
11304 /***********************************************************************
11305 Output Cursor
11306 ***********************************************************************/
11308 #ifdef HAVE_WINDOW_SYSTEM
11310 /* EXPORT:
11311 Nominal cursor position -- where to draw output.
11312 HPOS and VPOS are window relative glyph matrix coordinates.
11313 X and Y are window relative pixel coordinates. */
11315 struct cursor_pos output_cursor;
11318 /* EXPORT:
11319 Set the global variable output_cursor to CURSOR. All cursor
11320 positions are relative to updated_window. */
11322 void
11323 set_output_cursor (struct cursor_pos *cursor)
11325 output_cursor.hpos = cursor->hpos;
11326 output_cursor.vpos = cursor->vpos;
11327 output_cursor.x = cursor->x;
11328 output_cursor.y = cursor->y;
11332 /* EXPORT for RIF:
11333 Set a nominal cursor position.
11335 HPOS and VPOS are column/row positions in a window glyph matrix. X
11336 and Y are window text area relative pixel positions.
11338 If this is done during an update, updated_window will contain the
11339 window that is being updated and the position is the future output
11340 cursor position for that window. If updated_window is null, use
11341 selected_window and display the cursor at the given position. */
11343 void
11344 x_cursor_to (int vpos, int hpos, int y, int x)
11346 struct window *w;
11348 /* If updated_window is not set, work on selected_window. */
11349 if (updated_window)
11350 w = updated_window;
11351 else
11352 w = XWINDOW (selected_window);
11354 /* Set the output cursor. */
11355 output_cursor.hpos = hpos;
11356 output_cursor.vpos = vpos;
11357 output_cursor.x = x;
11358 output_cursor.y = y;
11360 /* If not called as part of an update, really display the cursor.
11361 This will also set the cursor position of W. */
11362 if (updated_window == NULL)
11364 block_input ();
11365 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11366 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11367 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11368 unblock_input ();
11372 #endif /* HAVE_WINDOW_SYSTEM */
11375 /***********************************************************************
11376 Tool-bars
11377 ***********************************************************************/
11379 #ifdef HAVE_WINDOW_SYSTEM
11381 /* Where the mouse was last time we reported a mouse event. */
11383 FRAME_PTR last_mouse_frame;
11385 /* Tool-bar item index of the item on which a mouse button was pressed
11386 or -1. */
11388 int last_tool_bar_item;
11390 /* Select `frame' temporarily without running all the code in
11391 do_switch_frame.
11392 FIXME: Maybe do_switch_frame should be trimmed down similarly
11393 when `norecord' is set. */
11394 static Lisp_Object
11395 fast_set_selected_frame (Lisp_Object frame)
11397 if (!EQ (selected_frame, frame))
11399 selected_frame = frame;
11400 selected_window = XFRAME (frame)->selected_window;
11402 return Qnil;
11405 /* Update the tool-bar item list for frame F. This has to be done
11406 before we start to fill in any display lines. Called from
11407 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11408 and restore it here. */
11410 static void
11411 update_tool_bar (struct frame *f, int save_match_data)
11413 #if defined (USE_GTK) || defined (HAVE_NS)
11414 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11415 #else
11416 int do_update = WINDOWP (f->tool_bar_window)
11417 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11418 #endif
11420 if (do_update)
11422 Lisp_Object window;
11423 struct window *w;
11425 window = FRAME_SELECTED_WINDOW (f);
11426 w = XWINDOW (window);
11428 /* If the user has switched buffers or windows, we need to
11429 recompute to reflect the new bindings. But we'll
11430 recompute when update_mode_lines is set too; that means
11431 that people can use force-mode-line-update to request
11432 that the menu bar be recomputed. The adverse effect on
11433 the rest of the redisplay algorithm is about the same as
11434 windows_or_buffers_changed anyway. */
11435 if (windows_or_buffers_changed
11436 || w->update_mode_line
11437 || update_mode_lines
11438 || window_buffer_changed (w))
11440 struct buffer *prev = current_buffer;
11441 ptrdiff_t count = SPECPDL_INDEX ();
11442 Lisp_Object frame, new_tool_bar;
11443 int new_n_tool_bar;
11444 struct gcpro gcpro1;
11446 /* Set current_buffer to the buffer of the selected
11447 window of the frame, so that we get the right local
11448 keymaps. */
11449 set_buffer_internal_1 (XBUFFER (w->contents));
11451 /* Save match data, if we must. */
11452 if (save_match_data)
11453 record_unwind_save_match_data ();
11455 /* Make sure that we don't accidentally use bogus keymaps. */
11456 if (NILP (Voverriding_local_map_menu_flag))
11458 specbind (Qoverriding_terminal_local_map, Qnil);
11459 specbind (Qoverriding_local_map, Qnil);
11462 GCPRO1 (new_tool_bar);
11464 /* We must temporarily set the selected frame to this frame
11465 before calling tool_bar_items, because the calculation of
11466 the tool-bar keymap uses the selected frame (see
11467 `tool-bar-make-keymap' in tool-bar.el). */
11468 eassert (EQ (selected_window,
11469 /* Since we only explicitly preserve selected_frame,
11470 check that selected_window would be redundant. */
11471 XFRAME (selected_frame)->selected_window));
11472 record_unwind_protect (fast_set_selected_frame, selected_frame);
11473 XSETFRAME (frame, f);
11474 fast_set_selected_frame (frame);
11476 /* Build desired tool-bar items from keymaps. */
11477 new_tool_bar
11478 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11479 &new_n_tool_bar);
11481 /* Redisplay the tool-bar if we changed it. */
11482 if (new_n_tool_bar != f->n_tool_bar_items
11483 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11485 /* Redisplay that happens asynchronously due to an expose event
11486 may access f->tool_bar_items. Make sure we update both
11487 variables within BLOCK_INPUT so no such event interrupts. */
11488 block_input ();
11489 fset_tool_bar_items (f, new_tool_bar);
11490 f->n_tool_bar_items = new_n_tool_bar;
11491 w->update_mode_line = 1;
11492 unblock_input ();
11495 UNGCPRO;
11497 unbind_to (count, Qnil);
11498 set_buffer_internal_1 (prev);
11504 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11505 F's desired tool-bar contents. F->tool_bar_items must have
11506 been set up previously by calling prepare_menu_bars. */
11508 static void
11509 build_desired_tool_bar_string (struct frame *f)
11511 int i, size, size_needed;
11512 struct gcpro gcpro1, gcpro2, gcpro3;
11513 Lisp_Object image, plist, props;
11515 image = plist = props = Qnil;
11516 GCPRO3 (image, plist, props);
11518 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11519 Otherwise, make a new string. */
11521 /* The size of the string we might be able to reuse. */
11522 size = (STRINGP (f->desired_tool_bar_string)
11523 ? SCHARS (f->desired_tool_bar_string)
11524 : 0);
11526 /* We need one space in the string for each image. */
11527 size_needed = f->n_tool_bar_items;
11529 /* Reuse f->desired_tool_bar_string, if possible. */
11530 if (size < size_needed || NILP (f->desired_tool_bar_string))
11531 fset_desired_tool_bar_string
11532 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11533 else
11535 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11536 Fremove_text_properties (make_number (0), make_number (size),
11537 props, f->desired_tool_bar_string);
11540 /* Put a `display' property on the string for the images to display,
11541 put a `menu_item' property on tool-bar items with a value that
11542 is the index of the item in F's tool-bar item vector. */
11543 for (i = 0; i < f->n_tool_bar_items; ++i)
11545 #define PROP(IDX) \
11546 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11548 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11549 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11550 int hmargin, vmargin, relief, idx, end;
11552 /* If image is a vector, choose the image according to the
11553 button state. */
11554 image = PROP (TOOL_BAR_ITEM_IMAGES);
11555 if (VECTORP (image))
11557 if (enabled_p)
11558 idx = (selected_p
11559 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11560 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11561 else
11562 idx = (selected_p
11563 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11564 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11566 eassert (ASIZE (image) >= idx);
11567 image = AREF (image, idx);
11569 else
11570 idx = -1;
11572 /* Ignore invalid image specifications. */
11573 if (!valid_image_p (image))
11574 continue;
11576 /* Display the tool-bar button pressed, or depressed. */
11577 plist = Fcopy_sequence (XCDR (image));
11579 /* Compute margin and relief to draw. */
11580 relief = (tool_bar_button_relief >= 0
11581 ? tool_bar_button_relief
11582 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11583 hmargin = vmargin = relief;
11585 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11586 INT_MAX - max (hmargin, vmargin)))
11588 hmargin += XFASTINT (Vtool_bar_button_margin);
11589 vmargin += XFASTINT (Vtool_bar_button_margin);
11591 else if (CONSP (Vtool_bar_button_margin))
11593 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11594 INT_MAX - hmargin))
11595 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11597 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11598 INT_MAX - vmargin))
11599 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11602 if (auto_raise_tool_bar_buttons_p)
11604 /* Add a `:relief' property to the image spec if the item is
11605 selected. */
11606 if (selected_p)
11608 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11609 hmargin -= relief;
11610 vmargin -= relief;
11613 else
11615 /* If image is selected, display it pressed, i.e. with a
11616 negative relief. If it's not selected, display it with a
11617 raised relief. */
11618 plist = Fplist_put (plist, QCrelief,
11619 (selected_p
11620 ? make_number (-relief)
11621 : make_number (relief)));
11622 hmargin -= relief;
11623 vmargin -= relief;
11626 /* Put a margin around the image. */
11627 if (hmargin || vmargin)
11629 if (hmargin == vmargin)
11630 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11631 else
11632 plist = Fplist_put (plist, QCmargin,
11633 Fcons (make_number (hmargin),
11634 make_number (vmargin)));
11637 /* If button is not enabled, and we don't have special images
11638 for the disabled state, make the image appear disabled by
11639 applying an appropriate algorithm to it. */
11640 if (!enabled_p && idx < 0)
11641 plist = Fplist_put (plist, QCconversion, Qdisabled);
11643 /* Put a `display' text property on the string for the image to
11644 display. Put a `menu-item' property on the string that gives
11645 the start of this item's properties in the tool-bar items
11646 vector. */
11647 image = Fcons (Qimage, plist);
11648 props = list4 (Qdisplay, image,
11649 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11651 /* Let the last image hide all remaining spaces in the tool bar
11652 string. The string can be longer than needed when we reuse a
11653 previous string. */
11654 if (i + 1 == f->n_tool_bar_items)
11655 end = SCHARS (f->desired_tool_bar_string);
11656 else
11657 end = i + 1;
11658 Fadd_text_properties (make_number (i), make_number (end),
11659 props, f->desired_tool_bar_string);
11660 #undef PROP
11663 UNGCPRO;
11667 /* Display one line of the tool-bar of frame IT->f.
11669 HEIGHT specifies the desired height of the tool-bar line.
11670 If the actual height of the glyph row is less than HEIGHT, the
11671 row's height is increased to HEIGHT, and the icons are centered
11672 vertically in the new height.
11674 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11675 count a final empty row in case the tool-bar width exactly matches
11676 the window width.
11679 static void
11680 display_tool_bar_line (struct it *it, int height)
11682 struct glyph_row *row = it->glyph_row;
11683 int max_x = it->last_visible_x;
11684 struct glyph *last;
11686 prepare_desired_row (row);
11687 row->y = it->current_y;
11689 /* Note that this isn't made use of if the face hasn't a box,
11690 so there's no need to check the face here. */
11691 it->start_of_box_run_p = 1;
11693 while (it->current_x < max_x)
11695 int x, n_glyphs_before, i, nglyphs;
11696 struct it it_before;
11698 /* Get the next display element. */
11699 if (!get_next_display_element (it))
11701 /* Don't count empty row if we are counting needed tool-bar lines. */
11702 if (height < 0 && !it->hpos)
11703 return;
11704 break;
11707 /* Produce glyphs. */
11708 n_glyphs_before = row->used[TEXT_AREA];
11709 it_before = *it;
11711 PRODUCE_GLYPHS (it);
11713 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11714 i = 0;
11715 x = it_before.current_x;
11716 while (i < nglyphs)
11718 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11720 if (x + glyph->pixel_width > max_x)
11722 /* Glyph doesn't fit on line. Backtrack. */
11723 row->used[TEXT_AREA] = n_glyphs_before;
11724 *it = it_before;
11725 /* If this is the only glyph on this line, it will never fit on the
11726 tool-bar, so skip it. But ensure there is at least one glyph,
11727 so we don't accidentally disable the tool-bar. */
11728 if (n_glyphs_before == 0
11729 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11730 break;
11731 goto out;
11734 ++it->hpos;
11735 x += glyph->pixel_width;
11736 ++i;
11739 /* Stop at line end. */
11740 if (ITERATOR_AT_END_OF_LINE_P (it))
11741 break;
11743 set_iterator_to_next (it, 1);
11746 out:;
11748 row->displays_text_p = row->used[TEXT_AREA] != 0;
11750 /* Use default face for the border below the tool bar.
11752 FIXME: When auto-resize-tool-bars is grow-only, there is
11753 no additional border below the possibly empty tool-bar lines.
11754 So to make the extra empty lines look "normal", we have to
11755 use the tool-bar face for the border too. */
11756 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11757 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11758 it->face_id = DEFAULT_FACE_ID;
11760 extend_face_to_end_of_line (it);
11761 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11762 last->right_box_line_p = 1;
11763 if (last == row->glyphs[TEXT_AREA])
11764 last->left_box_line_p = 1;
11766 /* Make line the desired height and center it vertically. */
11767 if ((height -= it->max_ascent + it->max_descent) > 0)
11769 /* Don't add more than one line height. */
11770 height %= FRAME_LINE_HEIGHT (it->f);
11771 it->max_ascent += height / 2;
11772 it->max_descent += (height + 1) / 2;
11775 compute_line_metrics (it);
11777 /* If line is empty, make it occupy the rest of the tool-bar. */
11778 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11780 row->height = row->phys_height = it->last_visible_y - row->y;
11781 row->visible_height = row->height;
11782 row->ascent = row->phys_ascent = 0;
11783 row->extra_line_spacing = 0;
11786 row->full_width_p = 1;
11787 row->continued_p = 0;
11788 row->truncated_on_left_p = 0;
11789 row->truncated_on_right_p = 0;
11791 it->current_x = it->hpos = 0;
11792 it->current_y += row->height;
11793 ++it->vpos;
11794 ++it->glyph_row;
11798 /* Max tool-bar height. */
11800 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11801 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11803 /* Value is the number of screen lines needed to make all tool-bar
11804 items of frame F visible. The number of actual rows needed is
11805 returned in *N_ROWS if non-NULL. */
11807 static int
11808 tool_bar_lines_needed (struct frame *f, int *n_rows)
11810 struct window *w = XWINDOW (f->tool_bar_window);
11811 struct it it;
11812 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11813 the desired matrix, so use (unused) mode-line row as temporary row to
11814 avoid destroying the first tool-bar row. */
11815 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11817 /* Initialize an iterator for iteration over
11818 F->desired_tool_bar_string in the tool-bar window of frame F. */
11819 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11820 it.first_visible_x = 0;
11821 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11822 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11823 it.paragraph_embedding = L2R;
11825 while (!ITERATOR_AT_END_P (&it))
11827 clear_glyph_row (temp_row);
11828 it.glyph_row = temp_row;
11829 display_tool_bar_line (&it, -1);
11831 clear_glyph_row (temp_row);
11833 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11834 if (n_rows)
11835 *n_rows = it.vpos > 0 ? it.vpos : -1;
11837 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11841 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11842 0, 1, 0,
11843 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11844 If FRAME is nil or omitted, use the selected frame. */)
11845 (Lisp_Object frame)
11847 struct frame *f = decode_any_frame (frame);
11848 struct window *w;
11849 int nlines = 0;
11851 if (WINDOWP (f->tool_bar_window)
11852 && (w = XWINDOW (f->tool_bar_window),
11853 WINDOW_TOTAL_LINES (w) > 0))
11855 update_tool_bar (f, 1);
11856 if (f->n_tool_bar_items)
11858 build_desired_tool_bar_string (f);
11859 nlines = tool_bar_lines_needed (f, NULL);
11863 return make_number (nlines);
11867 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11868 height should be changed. */
11870 static int
11871 redisplay_tool_bar (struct frame *f)
11873 struct window *w;
11874 struct it it;
11875 struct glyph_row *row;
11877 #if defined (USE_GTK) || defined (HAVE_NS)
11878 if (FRAME_EXTERNAL_TOOL_BAR (f))
11879 update_frame_tool_bar (f);
11880 return 0;
11881 #endif
11883 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11884 do anything. This means you must start with tool-bar-lines
11885 non-zero to get the auto-sizing effect. Or in other words, you
11886 can turn off tool-bars by specifying tool-bar-lines zero. */
11887 if (!WINDOWP (f->tool_bar_window)
11888 || (w = XWINDOW (f->tool_bar_window),
11889 WINDOW_TOTAL_LINES (w) == 0))
11890 return 0;
11892 /* Set up an iterator for the tool-bar window. */
11893 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11894 it.first_visible_x = 0;
11895 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11896 row = it.glyph_row;
11898 /* Build a string that represents the contents of the tool-bar. */
11899 build_desired_tool_bar_string (f);
11900 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11901 /* FIXME: This should be controlled by a user option. But it
11902 doesn't make sense to have an R2L tool bar if the menu bar cannot
11903 be drawn also R2L, and making the menu bar R2L is tricky due
11904 toolkit-specific code that implements it. If an R2L tool bar is
11905 ever supported, display_tool_bar_line should also be augmented to
11906 call unproduce_glyphs like display_line and display_string
11907 do. */
11908 it.paragraph_embedding = L2R;
11910 if (f->n_tool_bar_rows == 0)
11912 int nlines;
11914 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11915 nlines != WINDOW_TOTAL_LINES (w)))
11917 Lisp_Object frame;
11918 int old_height = WINDOW_TOTAL_LINES (w);
11920 XSETFRAME (frame, f);
11921 Fmodify_frame_parameters (frame,
11922 Fcons (Fcons (Qtool_bar_lines,
11923 make_number (nlines)),
11924 Qnil));
11925 if (WINDOW_TOTAL_LINES (w) != old_height)
11927 clear_glyph_matrix (w->desired_matrix);
11928 fonts_changed_p = 1;
11929 return 1;
11934 /* Display as many lines as needed to display all tool-bar items. */
11936 if (f->n_tool_bar_rows > 0)
11938 int border, rows, height, extra;
11940 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11941 border = XINT (Vtool_bar_border);
11942 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11943 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11944 else if (EQ (Vtool_bar_border, Qborder_width))
11945 border = f->border_width;
11946 else
11947 border = 0;
11948 if (border < 0)
11949 border = 0;
11951 rows = f->n_tool_bar_rows;
11952 height = max (1, (it.last_visible_y - border) / rows);
11953 extra = it.last_visible_y - border - height * rows;
11955 while (it.current_y < it.last_visible_y)
11957 int h = 0;
11958 if (extra > 0 && rows-- > 0)
11960 h = (extra + rows - 1) / rows;
11961 extra -= h;
11963 display_tool_bar_line (&it, height + h);
11966 else
11968 while (it.current_y < it.last_visible_y)
11969 display_tool_bar_line (&it, 0);
11972 /* It doesn't make much sense to try scrolling in the tool-bar
11973 window, so don't do it. */
11974 w->desired_matrix->no_scrolling_p = 1;
11975 w->must_be_updated_p = 1;
11977 if (!NILP (Vauto_resize_tool_bars))
11979 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11980 int change_height_p = 0;
11982 /* If we couldn't display everything, change the tool-bar's
11983 height if there is room for more. */
11984 if (IT_STRING_CHARPOS (it) < it.end_charpos
11985 && it.current_y < max_tool_bar_height)
11986 change_height_p = 1;
11988 row = it.glyph_row - 1;
11990 /* If there are blank lines at the end, except for a partially
11991 visible blank line at the end that is smaller than
11992 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11993 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11994 && row->height >= FRAME_LINE_HEIGHT (f))
11995 change_height_p = 1;
11997 /* If row displays tool-bar items, but is partially visible,
11998 change the tool-bar's height. */
11999 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12000 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12001 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12002 change_height_p = 1;
12004 /* Resize windows as needed by changing the `tool-bar-lines'
12005 frame parameter. */
12006 if (change_height_p)
12008 Lisp_Object frame;
12009 int old_height = WINDOW_TOTAL_LINES (w);
12010 int nrows;
12011 int nlines = tool_bar_lines_needed (f, &nrows);
12013 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12014 && !f->minimize_tool_bar_window_p)
12015 ? (nlines > old_height)
12016 : (nlines != old_height));
12017 f->minimize_tool_bar_window_p = 0;
12019 if (change_height_p)
12021 XSETFRAME (frame, f);
12022 Fmodify_frame_parameters (frame,
12023 Fcons (Fcons (Qtool_bar_lines,
12024 make_number (nlines)),
12025 Qnil));
12026 if (WINDOW_TOTAL_LINES (w) != old_height)
12028 clear_glyph_matrix (w->desired_matrix);
12029 f->n_tool_bar_rows = nrows;
12030 fonts_changed_p = 1;
12031 return 1;
12037 f->minimize_tool_bar_window_p = 0;
12038 return 0;
12042 /* Get information about the tool-bar item which is displayed in GLYPH
12043 on frame F. Return in *PROP_IDX the index where tool-bar item
12044 properties start in F->tool_bar_items. Value is zero if
12045 GLYPH doesn't display a tool-bar item. */
12047 static int
12048 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12050 Lisp_Object prop;
12051 int success_p;
12052 int charpos;
12054 /* This function can be called asynchronously, which means we must
12055 exclude any possibility that Fget_text_property signals an
12056 error. */
12057 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12058 charpos = max (0, charpos);
12060 /* Get the text property `menu-item' at pos. The value of that
12061 property is the start index of this item's properties in
12062 F->tool_bar_items. */
12063 prop = Fget_text_property (make_number (charpos),
12064 Qmenu_item, f->current_tool_bar_string);
12065 if (INTEGERP (prop))
12067 *prop_idx = XINT (prop);
12068 success_p = 1;
12070 else
12071 success_p = 0;
12073 return success_p;
12077 /* Get information about the tool-bar item at position X/Y on frame F.
12078 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12079 the current matrix of the tool-bar window of F, or NULL if not
12080 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12081 item in F->tool_bar_items. Value is
12083 -1 if X/Y is not on a tool-bar item
12084 0 if X/Y is on the same item that was highlighted before.
12085 1 otherwise. */
12087 static int
12088 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12089 int *hpos, int *vpos, int *prop_idx)
12091 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12092 struct window *w = XWINDOW (f->tool_bar_window);
12093 int area;
12095 /* Find the glyph under X/Y. */
12096 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12097 if (*glyph == NULL)
12098 return -1;
12100 /* Get the start of this tool-bar item's properties in
12101 f->tool_bar_items. */
12102 if (!tool_bar_item_info (f, *glyph, prop_idx))
12103 return -1;
12105 /* Is mouse on the highlighted item? */
12106 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12107 && *vpos >= hlinfo->mouse_face_beg_row
12108 && *vpos <= hlinfo->mouse_face_end_row
12109 && (*vpos > hlinfo->mouse_face_beg_row
12110 || *hpos >= hlinfo->mouse_face_beg_col)
12111 && (*vpos < hlinfo->mouse_face_end_row
12112 || *hpos < hlinfo->mouse_face_end_col
12113 || hlinfo->mouse_face_past_end))
12114 return 0;
12116 return 1;
12120 /* EXPORT:
12121 Handle mouse button event on the tool-bar of frame F, at
12122 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12123 0 for button release. MODIFIERS is event modifiers for button
12124 release. */
12126 void
12127 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12128 int modifiers)
12130 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12131 struct window *w = XWINDOW (f->tool_bar_window);
12132 int hpos, vpos, prop_idx;
12133 struct glyph *glyph;
12134 Lisp_Object enabled_p;
12136 /* If not on the highlighted tool-bar item, return. */
12137 frame_to_window_pixel_xy (w, &x, &y);
12138 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12139 return;
12141 /* If item is disabled, do nothing. */
12142 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12143 if (NILP (enabled_p))
12144 return;
12146 if (down_p)
12148 /* Show item in pressed state. */
12149 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12150 last_tool_bar_item = prop_idx;
12152 else
12154 Lisp_Object key, frame;
12155 struct input_event event;
12156 EVENT_INIT (event);
12158 /* Show item in released state. */
12159 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12161 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12163 XSETFRAME (frame, f);
12164 event.kind = TOOL_BAR_EVENT;
12165 event.frame_or_window = frame;
12166 event.arg = frame;
12167 kbd_buffer_store_event (&event);
12169 event.kind = TOOL_BAR_EVENT;
12170 event.frame_or_window = frame;
12171 event.arg = key;
12172 event.modifiers = modifiers;
12173 kbd_buffer_store_event (&event);
12174 last_tool_bar_item = -1;
12179 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12180 tool-bar window-relative coordinates X/Y. Called from
12181 note_mouse_highlight. */
12183 static void
12184 note_tool_bar_highlight (struct frame *f, int x, int y)
12186 Lisp_Object window = f->tool_bar_window;
12187 struct window *w = XWINDOW (window);
12188 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12189 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12190 int hpos, vpos;
12191 struct glyph *glyph;
12192 struct glyph_row *row;
12193 int i;
12194 Lisp_Object enabled_p;
12195 int prop_idx;
12196 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12197 int mouse_down_p, rc;
12199 /* Function note_mouse_highlight is called with negative X/Y
12200 values when mouse moves outside of the frame. */
12201 if (x <= 0 || y <= 0)
12203 clear_mouse_face (hlinfo);
12204 return;
12207 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12208 if (rc < 0)
12210 /* Not on tool-bar item. */
12211 clear_mouse_face (hlinfo);
12212 return;
12214 else if (rc == 0)
12215 /* On same tool-bar item as before. */
12216 goto set_help_echo;
12218 clear_mouse_face (hlinfo);
12220 /* Mouse is down, but on different tool-bar item? */
12221 mouse_down_p = (dpyinfo->grabbed
12222 && f == last_mouse_frame
12223 && FRAME_LIVE_P (f));
12224 if (mouse_down_p
12225 && last_tool_bar_item != prop_idx)
12226 return;
12228 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12230 /* If tool-bar item is not enabled, don't highlight it. */
12231 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12232 if (!NILP (enabled_p))
12234 /* Compute the x-position of the glyph. In front and past the
12235 image is a space. We include this in the highlighted area. */
12236 row = MATRIX_ROW (w->current_matrix, vpos);
12237 for (i = x = 0; i < hpos; ++i)
12238 x += row->glyphs[TEXT_AREA][i].pixel_width;
12240 /* Record this as the current active region. */
12241 hlinfo->mouse_face_beg_col = hpos;
12242 hlinfo->mouse_face_beg_row = vpos;
12243 hlinfo->mouse_face_beg_x = x;
12244 hlinfo->mouse_face_beg_y = row->y;
12245 hlinfo->mouse_face_past_end = 0;
12247 hlinfo->mouse_face_end_col = hpos + 1;
12248 hlinfo->mouse_face_end_row = vpos;
12249 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12250 hlinfo->mouse_face_end_y = row->y;
12251 hlinfo->mouse_face_window = window;
12252 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12254 /* Display it as active. */
12255 show_mouse_face (hlinfo, draw);
12258 set_help_echo:
12260 /* Set help_echo_string to a help string to display for this tool-bar item.
12261 XTread_socket does the rest. */
12262 help_echo_object = help_echo_window = Qnil;
12263 help_echo_pos = -1;
12264 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12265 if (NILP (help_echo_string))
12266 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12269 #endif /* HAVE_WINDOW_SYSTEM */
12273 /************************************************************************
12274 Horizontal scrolling
12275 ************************************************************************/
12277 static int hscroll_window_tree (Lisp_Object);
12278 static int hscroll_windows (Lisp_Object);
12280 /* For all leaf windows in the window tree rooted at WINDOW, set their
12281 hscroll value so that PT is (i) visible in the window, and (ii) so
12282 that it is not within a certain margin at the window's left and
12283 right border. Value is non-zero if any window's hscroll has been
12284 changed. */
12286 static int
12287 hscroll_window_tree (Lisp_Object window)
12289 int hscrolled_p = 0;
12290 int hscroll_relative_p = FLOATP (Vhscroll_step);
12291 int hscroll_step_abs = 0;
12292 double hscroll_step_rel = 0;
12294 if (hscroll_relative_p)
12296 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12297 if (hscroll_step_rel < 0)
12299 hscroll_relative_p = 0;
12300 hscroll_step_abs = 0;
12303 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12305 hscroll_step_abs = XINT (Vhscroll_step);
12306 if (hscroll_step_abs < 0)
12307 hscroll_step_abs = 0;
12309 else
12310 hscroll_step_abs = 0;
12312 while (WINDOWP (window))
12314 struct window *w = XWINDOW (window);
12316 if (WINDOWP (w->contents))
12317 hscrolled_p |= hscroll_window_tree (w->contents);
12318 else if (w->cursor.vpos >= 0)
12320 int h_margin;
12321 int text_area_width;
12322 struct glyph_row *current_cursor_row
12323 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12324 struct glyph_row *desired_cursor_row
12325 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12326 struct glyph_row *cursor_row
12327 = (desired_cursor_row->enabled_p
12328 ? desired_cursor_row
12329 : current_cursor_row);
12330 int row_r2l_p = cursor_row->reversed_p;
12332 text_area_width = window_box_width (w, TEXT_AREA);
12334 /* Scroll when cursor is inside this scroll margin. */
12335 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12337 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12338 /* For left-to-right rows, hscroll when cursor is either
12339 (i) inside the right hscroll margin, or (ii) if it is
12340 inside the left margin and the window is already
12341 hscrolled. */
12342 && ((!row_r2l_p
12343 && ((w->hscroll
12344 && w->cursor.x <= h_margin)
12345 || (cursor_row->enabled_p
12346 && cursor_row->truncated_on_right_p
12347 && (w->cursor.x >= text_area_width - h_margin))))
12348 /* For right-to-left rows, the logic is similar,
12349 except that rules for scrolling to left and right
12350 are reversed. E.g., if cursor.x <= h_margin, we
12351 need to hscroll "to the right" unconditionally,
12352 and that will scroll the screen to the left so as
12353 to reveal the next portion of the row. */
12354 || (row_r2l_p
12355 && ((cursor_row->enabled_p
12356 /* FIXME: It is confusing to set the
12357 truncated_on_right_p flag when R2L rows
12358 are actually truncated on the left. */
12359 && cursor_row->truncated_on_right_p
12360 && w->cursor.x <= h_margin)
12361 || (w->hscroll
12362 && (w->cursor.x >= text_area_width - h_margin))))))
12364 struct it it;
12365 ptrdiff_t hscroll;
12366 struct buffer *saved_current_buffer;
12367 ptrdiff_t pt;
12368 int wanted_x;
12370 /* Find point in a display of infinite width. */
12371 saved_current_buffer = current_buffer;
12372 current_buffer = XBUFFER (w->contents);
12374 if (w == XWINDOW (selected_window))
12375 pt = PT;
12376 else
12377 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12379 /* Move iterator to pt starting at cursor_row->start in
12380 a line with infinite width. */
12381 init_to_row_start (&it, w, cursor_row);
12382 it.last_visible_x = INFINITY;
12383 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12384 current_buffer = saved_current_buffer;
12386 /* Position cursor in window. */
12387 if (!hscroll_relative_p && hscroll_step_abs == 0)
12388 hscroll = max (0, (it.current_x
12389 - (ITERATOR_AT_END_OF_LINE_P (&it)
12390 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12391 : (text_area_width / 2))))
12392 / FRAME_COLUMN_WIDTH (it.f);
12393 else if ((!row_r2l_p
12394 && w->cursor.x >= text_area_width - h_margin)
12395 || (row_r2l_p && w->cursor.x <= h_margin))
12397 if (hscroll_relative_p)
12398 wanted_x = text_area_width * (1 - hscroll_step_rel)
12399 - h_margin;
12400 else
12401 wanted_x = text_area_width
12402 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12403 - h_margin;
12404 hscroll
12405 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12407 else
12409 if (hscroll_relative_p)
12410 wanted_x = text_area_width * hscroll_step_rel
12411 + h_margin;
12412 else
12413 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12414 + h_margin;
12415 hscroll
12416 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12418 hscroll = max (hscroll, w->min_hscroll);
12420 /* Don't prevent redisplay optimizations if hscroll
12421 hasn't changed, as it will unnecessarily slow down
12422 redisplay. */
12423 if (w->hscroll != hscroll)
12425 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12426 w->hscroll = hscroll;
12427 hscrolled_p = 1;
12432 window = w->next;
12435 /* Value is non-zero if hscroll of any leaf window has been changed. */
12436 return hscrolled_p;
12440 /* Set hscroll so that cursor is visible and not inside horizontal
12441 scroll margins for all windows in the tree rooted at WINDOW. See
12442 also hscroll_window_tree above. Value is non-zero if any window's
12443 hscroll has been changed. If it has, desired matrices on the frame
12444 of WINDOW are cleared. */
12446 static int
12447 hscroll_windows (Lisp_Object window)
12449 int hscrolled_p = hscroll_window_tree (window);
12450 if (hscrolled_p)
12451 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12452 return hscrolled_p;
12457 /************************************************************************
12458 Redisplay
12459 ************************************************************************/
12461 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12462 to a non-zero value. This is sometimes handy to have in a debugger
12463 session. */
12465 #ifdef GLYPH_DEBUG
12467 /* First and last unchanged row for try_window_id. */
12469 static int debug_first_unchanged_at_end_vpos;
12470 static int debug_last_unchanged_at_beg_vpos;
12472 /* Delta vpos and y. */
12474 static int debug_dvpos, debug_dy;
12476 /* Delta in characters and bytes for try_window_id. */
12478 static ptrdiff_t debug_delta, debug_delta_bytes;
12480 /* Values of window_end_pos and window_end_vpos at the end of
12481 try_window_id. */
12483 static ptrdiff_t debug_end_vpos;
12485 /* Append a string to W->desired_matrix->method. FMT is a printf
12486 format string. If trace_redisplay_p is non-zero also printf the
12487 resulting string to stderr. */
12489 static void debug_method_add (struct window *, char const *, ...)
12490 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12492 static void
12493 debug_method_add (struct window *w, char const *fmt, ...)
12495 char *method = w->desired_matrix->method;
12496 int len = strlen (method);
12497 int size = sizeof w->desired_matrix->method;
12498 int remaining = size - len - 1;
12499 va_list ap;
12501 if (len && remaining)
12503 method[len] = '|';
12504 --remaining, ++len;
12507 va_start (ap, fmt);
12508 vsnprintf (method + len, remaining + 1, fmt, ap);
12509 va_end (ap);
12511 if (trace_redisplay_p)
12512 fprintf (stderr, "%p (%s): %s\n",
12514 ((BUFFERP (w->contents)
12515 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12516 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12517 : "no buffer"),
12518 method + len);
12521 #endif /* GLYPH_DEBUG */
12524 /* Value is non-zero if all changes in window W, which displays
12525 current_buffer, are in the text between START and END. START is a
12526 buffer position, END is given as a distance from Z. Used in
12527 redisplay_internal for display optimization. */
12529 static int
12530 text_outside_line_unchanged_p (struct window *w,
12531 ptrdiff_t start, ptrdiff_t end)
12533 int unchanged_p = 1;
12535 /* If text or overlays have changed, see where. */
12536 if (window_outdated (w))
12538 /* Gap in the line? */
12539 if (GPT < start || Z - GPT < end)
12540 unchanged_p = 0;
12542 /* Changes start in front of the line, or end after it? */
12543 if (unchanged_p
12544 && (BEG_UNCHANGED < start - 1
12545 || END_UNCHANGED < end))
12546 unchanged_p = 0;
12548 /* If selective display, can't optimize if changes start at the
12549 beginning of the line. */
12550 if (unchanged_p
12551 && INTEGERP (BVAR (current_buffer, selective_display))
12552 && XINT (BVAR (current_buffer, selective_display)) > 0
12553 && (BEG_UNCHANGED < start || GPT <= start))
12554 unchanged_p = 0;
12556 /* If there are overlays at the start or end of the line, these
12557 may have overlay strings with newlines in them. A change at
12558 START, for instance, may actually concern the display of such
12559 overlay strings as well, and they are displayed on different
12560 lines. So, quickly rule out this case. (For the future, it
12561 might be desirable to implement something more telling than
12562 just BEG/END_UNCHANGED.) */
12563 if (unchanged_p)
12565 if (BEG + BEG_UNCHANGED == start
12566 && overlay_touches_p (start))
12567 unchanged_p = 0;
12568 if (END_UNCHANGED == end
12569 && overlay_touches_p (Z - end))
12570 unchanged_p = 0;
12573 /* Under bidi reordering, adding or deleting a character in the
12574 beginning of a paragraph, before the first strong directional
12575 character, can change the base direction of the paragraph (unless
12576 the buffer specifies a fixed paragraph direction), which will
12577 require to redisplay the whole paragraph. It might be worthwhile
12578 to find the paragraph limits and widen the range of redisplayed
12579 lines to that, but for now just give up this optimization. */
12580 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12581 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12582 unchanged_p = 0;
12585 return unchanged_p;
12589 /* Do a frame update, taking possible shortcuts into account. This is
12590 the main external entry point for redisplay.
12592 If the last redisplay displayed an echo area message and that message
12593 is no longer requested, we clear the echo area or bring back the
12594 mini-buffer if that is in use. */
12596 void
12597 redisplay (void)
12599 redisplay_internal ();
12603 static Lisp_Object
12604 overlay_arrow_string_or_property (Lisp_Object var)
12606 Lisp_Object val;
12608 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12609 return val;
12611 return Voverlay_arrow_string;
12614 /* Return 1 if there are any overlay-arrows in current_buffer. */
12615 static int
12616 overlay_arrow_in_current_buffer_p (void)
12618 Lisp_Object vlist;
12620 for (vlist = Voverlay_arrow_variable_list;
12621 CONSP (vlist);
12622 vlist = XCDR (vlist))
12624 Lisp_Object var = XCAR (vlist);
12625 Lisp_Object val;
12627 if (!SYMBOLP (var))
12628 continue;
12629 val = find_symbol_value (var);
12630 if (MARKERP (val)
12631 && current_buffer == XMARKER (val)->buffer)
12632 return 1;
12634 return 0;
12638 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12639 has changed. */
12641 static int
12642 overlay_arrows_changed_p (void)
12644 Lisp_Object vlist;
12646 for (vlist = Voverlay_arrow_variable_list;
12647 CONSP (vlist);
12648 vlist = XCDR (vlist))
12650 Lisp_Object var = XCAR (vlist);
12651 Lisp_Object val, pstr;
12653 if (!SYMBOLP (var))
12654 continue;
12655 val = find_symbol_value (var);
12656 if (!MARKERP (val))
12657 continue;
12658 if (! EQ (COERCE_MARKER (val),
12659 Fget (var, Qlast_arrow_position))
12660 || ! (pstr = overlay_arrow_string_or_property (var),
12661 EQ (pstr, Fget (var, Qlast_arrow_string))))
12662 return 1;
12664 return 0;
12667 /* Mark overlay arrows to be updated on next redisplay. */
12669 static void
12670 update_overlay_arrows (int up_to_date)
12672 Lisp_Object vlist;
12674 for (vlist = Voverlay_arrow_variable_list;
12675 CONSP (vlist);
12676 vlist = XCDR (vlist))
12678 Lisp_Object var = XCAR (vlist);
12680 if (!SYMBOLP (var))
12681 continue;
12683 if (up_to_date > 0)
12685 Lisp_Object val = find_symbol_value (var);
12686 Fput (var, Qlast_arrow_position,
12687 COERCE_MARKER (val));
12688 Fput (var, Qlast_arrow_string,
12689 overlay_arrow_string_or_property (var));
12691 else if (up_to_date < 0
12692 || !NILP (Fget (var, Qlast_arrow_position)))
12694 Fput (var, Qlast_arrow_position, Qt);
12695 Fput (var, Qlast_arrow_string, Qt);
12701 /* Return overlay arrow string to display at row.
12702 Return integer (bitmap number) for arrow bitmap in left fringe.
12703 Return nil if no overlay arrow. */
12705 static Lisp_Object
12706 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12708 Lisp_Object vlist;
12710 for (vlist = Voverlay_arrow_variable_list;
12711 CONSP (vlist);
12712 vlist = XCDR (vlist))
12714 Lisp_Object var = XCAR (vlist);
12715 Lisp_Object val;
12717 if (!SYMBOLP (var))
12718 continue;
12720 val = find_symbol_value (var);
12722 if (MARKERP (val)
12723 && current_buffer == XMARKER (val)->buffer
12724 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12726 if (FRAME_WINDOW_P (it->f)
12727 /* FIXME: if ROW->reversed_p is set, this should test
12728 the right fringe, not the left one. */
12729 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12731 #ifdef HAVE_WINDOW_SYSTEM
12732 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12734 int fringe_bitmap;
12735 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12736 return make_number (fringe_bitmap);
12738 #endif
12739 return make_number (-1); /* Use default arrow bitmap. */
12741 return overlay_arrow_string_or_property (var);
12745 return Qnil;
12748 /* Return 1 if point moved out of or into a composition. Otherwise
12749 return 0. PREV_BUF and PREV_PT are the last point buffer and
12750 position. BUF and PT are the current point buffer and position. */
12752 static int
12753 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12754 struct buffer *buf, ptrdiff_t pt)
12756 ptrdiff_t start, end;
12757 Lisp_Object prop;
12758 Lisp_Object buffer;
12760 XSETBUFFER (buffer, buf);
12761 /* Check a composition at the last point if point moved within the
12762 same buffer. */
12763 if (prev_buf == buf)
12765 if (prev_pt == pt)
12766 /* Point didn't move. */
12767 return 0;
12769 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12770 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12771 && COMPOSITION_VALID_P (start, end, prop)
12772 && start < prev_pt && end > prev_pt)
12773 /* The last point was within the composition. Return 1 iff
12774 point moved out of the composition. */
12775 return (pt <= start || pt >= end);
12778 /* Check a composition at the current point. */
12779 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12780 && find_composition (pt, -1, &start, &end, &prop, buffer)
12781 && COMPOSITION_VALID_P (start, end, prop)
12782 && start < pt && end > pt);
12786 /* Reconsider the setting of B->clip_changed which is displayed
12787 in window W. */
12789 static void
12790 reconsider_clip_changes (struct window *w, struct buffer *b)
12792 if (b->clip_changed
12793 && w->window_end_valid
12794 && w->current_matrix->buffer == b
12795 && w->current_matrix->zv == BUF_ZV (b)
12796 && w->current_matrix->begv == BUF_BEGV (b))
12797 b->clip_changed = 0;
12799 /* If display wasn't paused, and W is not a tool bar window, see if
12800 point has been moved into or out of a composition. In that case,
12801 we set b->clip_changed to 1 to force updating the screen. If
12802 b->clip_changed has already been set to 1, we can skip this
12803 check. */
12804 if (!b->clip_changed && BUFFERP (w->contents) && w->window_end_valid)
12806 ptrdiff_t pt;
12808 if (w == XWINDOW (selected_window))
12809 pt = PT;
12810 else
12811 pt = marker_position (w->pointm);
12813 if ((w->current_matrix->buffer != XBUFFER (w->contents)
12814 || pt != w->last_point)
12815 && check_point_in_composition (w->current_matrix->buffer,
12816 w->last_point,
12817 XBUFFER (w->contents), pt))
12818 b->clip_changed = 1;
12823 #define STOP_POLLING \
12824 do { if (! polling_stopped_here) stop_polling (); \
12825 polling_stopped_here = 1; } while (0)
12827 #define RESUME_POLLING \
12828 do { if (polling_stopped_here) start_polling (); \
12829 polling_stopped_here = 0; } while (0)
12832 /* Perhaps in the future avoid recentering windows if it
12833 is not necessary; currently that causes some problems. */
12835 static void
12836 redisplay_internal (void)
12838 struct window *w = XWINDOW (selected_window);
12839 struct window *sw;
12840 struct frame *fr;
12841 int pending;
12842 int must_finish = 0;
12843 struct text_pos tlbufpos, tlendpos;
12844 int number_of_visible_frames;
12845 ptrdiff_t count, count1;
12846 struct frame *sf;
12847 int polling_stopped_here = 0;
12848 Lisp_Object tail, frame;
12849 struct backtrace backtrace;
12851 /* Non-zero means redisplay has to consider all windows on all
12852 frames. Zero means, only selected_window is considered. */
12853 int consider_all_windows_p;
12855 /* Non-zero means redisplay has to redisplay the miniwindow. */
12856 int update_miniwindow_p = 0;
12858 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12860 /* No redisplay if running in batch mode or frame is not yet fully
12861 initialized, or redisplay is explicitly turned off by setting
12862 Vinhibit_redisplay. */
12863 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12864 || !NILP (Vinhibit_redisplay))
12865 return;
12867 /* Don't examine these until after testing Vinhibit_redisplay.
12868 When Emacs is shutting down, perhaps because its connection to
12869 X has dropped, we should not look at them at all. */
12870 fr = XFRAME (w->frame);
12871 sf = SELECTED_FRAME ();
12873 if (!fr->glyphs_initialized_p)
12874 return;
12876 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12877 if (popup_activated ())
12878 return;
12879 #endif
12881 /* I don't think this happens but let's be paranoid. */
12882 if (redisplaying_p)
12883 return;
12885 /* Record a function that clears redisplaying_p
12886 when we leave this function. */
12887 count = SPECPDL_INDEX ();
12888 record_unwind_protect (unwind_redisplay, selected_frame);
12889 redisplaying_p = 1;
12890 specbind (Qinhibit_free_realized_faces, Qnil);
12892 /* Record this function, so it appears on the profiler's backtraces. */
12893 backtrace.next = backtrace_list;
12894 backtrace.function = Qredisplay_internal;
12895 backtrace.args = &Qnil;
12896 backtrace.nargs = 0;
12897 backtrace.debug_on_exit = 0;
12898 backtrace_list = &backtrace;
12900 FOR_EACH_FRAME (tail, frame)
12901 XFRAME (frame)->already_hscrolled_p = 0;
12903 retry:
12904 /* Remember the currently selected window. */
12905 sw = w;
12907 pending = 0;
12908 reconsider_clip_changes (w, current_buffer);
12909 last_escape_glyph_frame = NULL;
12910 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12911 last_glyphless_glyph_frame = NULL;
12912 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12914 /* If new fonts have been loaded that make a glyph matrix adjustment
12915 necessary, do it. */
12916 if (fonts_changed_p)
12918 adjust_glyphs (NULL);
12919 ++windows_or_buffers_changed;
12920 fonts_changed_p = 0;
12923 /* If face_change_count is non-zero, init_iterator will free all
12924 realized faces, which includes the faces referenced from current
12925 matrices. So, we can't reuse current matrices in this case. */
12926 if (face_change_count)
12927 ++windows_or_buffers_changed;
12929 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12930 && FRAME_TTY (sf)->previous_frame != sf)
12932 /* Since frames on a single ASCII terminal share the same
12933 display area, displaying a different frame means redisplay
12934 the whole thing. */
12935 windows_or_buffers_changed++;
12936 SET_FRAME_GARBAGED (sf);
12937 #ifndef DOS_NT
12938 set_tty_color_mode (FRAME_TTY (sf), sf);
12939 #endif
12940 FRAME_TTY (sf)->previous_frame = sf;
12943 /* Set the visible flags for all frames. Do this before checking for
12944 resized or garbaged frames; they want to know if their frames are
12945 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12946 number_of_visible_frames = 0;
12948 FOR_EACH_FRAME (tail, frame)
12950 struct frame *f = XFRAME (frame);
12952 if (FRAME_VISIBLE_P (f))
12953 ++number_of_visible_frames;
12954 clear_desired_matrices (f);
12957 /* Notice any pending interrupt request to change frame size. */
12958 do_pending_window_change (1);
12960 /* do_pending_window_change could change the selected_window due to
12961 frame resizing which makes the selected window too small. */
12962 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12964 sw = w;
12965 reconsider_clip_changes (w, current_buffer);
12968 /* Clear frames marked as garbaged. */
12969 clear_garbaged_frames ();
12971 /* Build menubar and tool-bar items. */
12972 if (NILP (Vmemory_full))
12973 prepare_menu_bars ();
12975 if (windows_or_buffers_changed)
12976 update_mode_lines++;
12978 /* Detect case that we need to write or remove a star in the mode line. */
12979 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
12981 w->update_mode_line = 1;
12982 if (buffer_shared_and_changed ())
12983 update_mode_lines++;
12986 /* Avoid invocation of point motion hooks by `current_column' below. */
12987 count1 = SPECPDL_INDEX ();
12988 specbind (Qinhibit_point_motion_hooks, Qt);
12990 if (mode_line_update_needed (w))
12991 w->update_mode_line = 1;
12993 unbind_to (count1, Qnil);
12995 consider_all_windows_p = (update_mode_lines
12996 || buffer_shared_and_changed ()
12997 || cursor_type_changed);
12999 /* If specs for an arrow have changed, do thorough redisplay
13000 to ensure we remove any arrow that should no longer exist. */
13001 if (overlay_arrows_changed_p ())
13002 consider_all_windows_p = windows_or_buffers_changed = 1;
13004 /* Normally the message* functions will have already displayed and
13005 updated the echo area, but the frame may have been trashed, or
13006 the update may have been preempted, so display the echo area
13007 again here. Checking message_cleared_p captures the case that
13008 the echo area should be cleared. */
13009 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13010 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13011 || (message_cleared_p
13012 && minibuf_level == 0
13013 /* If the mini-window is currently selected, this means the
13014 echo-area doesn't show through. */
13015 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13017 int window_height_changed_p = echo_area_display (0);
13019 if (message_cleared_p)
13020 update_miniwindow_p = 1;
13022 must_finish = 1;
13024 /* If we don't display the current message, don't clear the
13025 message_cleared_p flag, because, if we did, we wouldn't clear
13026 the echo area in the next redisplay which doesn't preserve
13027 the echo area. */
13028 if (!display_last_displayed_message_p)
13029 message_cleared_p = 0;
13031 if (fonts_changed_p)
13032 goto retry;
13033 else if (window_height_changed_p)
13035 consider_all_windows_p = 1;
13036 ++update_mode_lines;
13037 ++windows_or_buffers_changed;
13039 /* If window configuration was changed, frames may have been
13040 marked garbaged. Clear them or we will experience
13041 surprises wrt scrolling. */
13042 clear_garbaged_frames ();
13045 else if (EQ (selected_window, minibuf_window)
13046 && (current_buffer->clip_changed || window_outdated (w))
13047 && resize_mini_window (w, 0))
13049 /* Resized active mini-window to fit the size of what it is
13050 showing if its contents might have changed. */
13051 must_finish = 1;
13052 /* FIXME: this causes all frames to be updated, which seems unnecessary
13053 since only the current frame needs to be considered. This function
13054 needs to be rewritten with two variables, consider_all_windows and
13055 consider_all_frames. */
13056 consider_all_windows_p = 1;
13057 ++windows_or_buffers_changed;
13058 ++update_mode_lines;
13060 /* If window configuration was changed, frames may have been
13061 marked garbaged. Clear them or we will experience
13062 surprises wrt scrolling. */
13063 clear_garbaged_frames ();
13066 /* If showing the region, and mark has changed, we must redisplay
13067 the whole window. The assignment to this_line_start_pos prevents
13068 the optimization directly below this if-statement. */
13069 if (((!NILP (Vtransient_mark_mode)
13070 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13071 != (w->region_showing > 0))
13072 || (w->region_showing
13073 && w->region_showing
13074 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13075 CHARPOS (this_line_start_pos) = 0;
13077 /* Optimize the case that only the line containing the cursor in the
13078 selected window has changed. Variables starting with this_ are
13079 set in display_line and record information about the line
13080 containing the cursor. */
13081 tlbufpos = this_line_start_pos;
13082 tlendpos = this_line_end_pos;
13083 if (!consider_all_windows_p
13084 && CHARPOS (tlbufpos) > 0
13085 && !w->update_mode_line
13086 && !current_buffer->clip_changed
13087 && !current_buffer->prevent_redisplay_optimizations_p
13088 && FRAME_VISIBLE_P (XFRAME (w->frame))
13089 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13090 /* Make sure recorded data applies to current buffer, etc. */
13091 && this_line_buffer == current_buffer
13092 && current_buffer == XBUFFER (w->contents)
13093 && !w->force_start
13094 && !w->optional_new_start
13095 /* Point must be on the line that we have info recorded about. */
13096 && PT >= CHARPOS (tlbufpos)
13097 && PT <= Z - CHARPOS (tlendpos)
13098 /* All text outside that line, including its final newline,
13099 must be unchanged. */
13100 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13101 CHARPOS (tlendpos)))
13103 if (CHARPOS (tlbufpos) > BEGV
13104 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13105 && (CHARPOS (tlbufpos) == ZV
13106 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13107 /* Former continuation line has disappeared by becoming empty. */
13108 goto cancel;
13109 else if (window_outdated (w) || MINI_WINDOW_P (w))
13111 /* We have to handle the case of continuation around a
13112 wide-column character (see the comment in indent.c around
13113 line 1340).
13115 For instance, in the following case:
13117 -------- Insert --------
13118 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13119 J_I_ ==> J_I_ `^^' are cursors.
13120 ^^ ^^
13121 -------- --------
13123 As we have to redraw the line above, we cannot use this
13124 optimization. */
13126 struct it it;
13127 int line_height_before = this_line_pixel_height;
13129 /* Note that start_display will handle the case that the
13130 line starting at tlbufpos is a continuation line. */
13131 start_display (&it, w, tlbufpos);
13133 /* Implementation note: It this still necessary? */
13134 if (it.current_x != this_line_start_x)
13135 goto cancel;
13137 TRACE ((stderr, "trying display optimization 1\n"));
13138 w->cursor.vpos = -1;
13139 overlay_arrow_seen = 0;
13140 it.vpos = this_line_vpos;
13141 it.current_y = this_line_y;
13142 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13143 display_line (&it);
13145 /* If line contains point, is not continued,
13146 and ends at same distance from eob as before, we win. */
13147 if (w->cursor.vpos >= 0
13148 /* Line is not continued, otherwise this_line_start_pos
13149 would have been set to 0 in display_line. */
13150 && CHARPOS (this_line_start_pos)
13151 /* Line ends as before. */
13152 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13153 /* Line has same height as before. Otherwise other lines
13154 would have to be shifted up or down. */
13155 && this_line_pixel_height == line_height_before)
13157 /* If this is not the window's last line, we must adjust
13158 the charstarts of the lines below. */
13159 if (it.current_y < it.last_visible_y)
13161 struct glyph_row *row
13162 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13163 ptrdiff_t delta, delta_bytes;
13165 /* We used to distinguish between two cases here,
13166 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13167 when the line ends in a newline or the end of the
13168 buffer's accessible portion. But both cases did
13169 the same, so they were collapsed. */
13170 delta = (Z
13171 - CHARPOS (tlendpos)
13172 - MATRIX_ROW_START_CHARPOS (row));
13173 delta_bytes = (Z_BYTE
13174 - BYTEPOS (tlendpos)
13175 - MATRIX_ROW_START_BYTEPOS (row));
13177 increment_matrix_positions (w->current_matrix,
13178 this_line_vpos + 1,
13179 w->current_matrix->nrows,
13180 delta, delta_bytes);
13183 /* If this row displays text now but previously didn't,
13184 or vice versa, w->window_end_vpos may have to be
13185 adjusted. */
13186 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13188 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13189 wset_window_end_vpos (w, make_number (this_line_vpos));
13191 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13192 && this_line_vpos > 0)
13193 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13194 w->window_end_valid = 0;
13196 /* Update hint: No need to try to scroll in update_window. */
13197 w->desired_matrix->no_scrolling_p = 1;
13199 #ifdef GLYPH_DEBUG
13200 *w->desired_matrix->method = 0;
13201 debug_method_add (w, "optimization 1");
13202 #endif
13203 #ifdef HAVE_WINDOW_SYSTEM
13204 update_window_fringes (w, 0);
13205 #endif
13206 goto update;
13208 else
13209 goto cancel;
13211 else if (/* Cursor position hasn't changed. */
13212 PT == w->last_point
13213 /* Make sure the cursor was last displayed
13214 in this window. Otherwise we have to reposition it. */
13215 && 0 <= w->cursor.vpos
13216 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13218 if (!must_finish)
13220 do_pending_window_change (1);
13221 /* If selected_window changed, redisplay again. */
13222 if (WINDOWP (selected_window)
13223 && (w = XWINDOW (selected_window)) != sw)
13224 goto retry;
13226 /* We used to always goto end_of_redisplay here, but this
13227 isn't enough if we have a blinking cursor. */
13228 if (w->cursor_off_p == w->last_cursor_off_p)
13229 goto end_of_redisplay;
13231 goto update;
13233 /* If highlighting the region, or if the cursor is in the echo area,
13234 then we can't just move the cursor. */
13235 else if (! (!NILP (Vtransient_mark_mode)
13236 && !NILP (BVAR (current_buffer, mark_active)))
13237 && (EQ (selected_window,
13238 BVAR (current_buffer, last_selected_window))
13239 || highlight_nonselected_windows)
13240 && !w->region_showing
13241 && NILP (Vshow_trailing_whitespace)
13242 && !cursor_in_echo_area)
13244 struct it it;
13245 struct glyph_row *row;
13247 /* Skip from tlbufpos to PT and see where it is. Note that
13248 PT may be in invisible text. If so, we will end at the
13249 next visible position. */
13250 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13251 NULL, DEFAULT_FACE_ID);
13252 it.current_x = this_line_start_x;
13253 it.current_y = this_line_y;
13254 it.vpos = this_line_vpos;
13256 /* The call to move_it_to stops in front of PT, but
13257 moves over before-strings. */
13258 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13260 if (it.vpos == this_line_vpos
13261 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13262 row->enabled_p))
13264 eassert (this_line_vpos == it.vpos);
13265 eassert (this_line_y == it.current_y);
13266 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13267 #ifdef GLYPH_DEBUG
13268 *w->desired_matrix->method = 0;
13269 debug_method_add (w, "optimization 3");
13270 #endif
13271 goto update;
13273 else
13274 goto cancel;
13277 cancel:
13278 /* Text changed drastically or point moved off of line. */
13279 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13282 CHARPOS (this_line_start_pos) = 0;
13283 consider_all_windows_p |= buffer_shared_and_changed ();
13284 ++clear_face_cache_count;
13285 #ifdef HAVE_WINDOW_SYSTEM
13286 ++clear_image_cache_count;
13287 #endif
13289 /* Build desired matrices, and update the display. If
13290 consider_all_windows_p is non-zero, do it for all windows on all
13291 frames. Otherwise do it for selected_window, only. */
13293 if (consider_all_windows_p)
13295 FOR_EACH_FRAME (tail, frame)
13296 XFRAME (frame)->updated_p = 0;
13298 FOR_EACH_FRAME (tail, frame)
13300 struct frame *f = XFRAME (frame);
13302 /* We don't have to do anything for unselected terminal
13303 frames. */
13304 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13305 && !EQ (FRAME_TTY (f)->top_frame, frame))
13306 continue;
13308 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13310 /* Mark all the scroll bars to be removed; we'll redeem
13311 the ones we want when we redisplay their windows. */
13312 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13313 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13315 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13316 redisplay_windows (FRAME_ROOT_WINDOW (f));
13318 /* The X error handler may have deleted that frame. */
13319 if (!FRAME_LIVE_P (f))
13320 continue;
13322 /* Any scroll bars which redisplay_windows should have
13323 nuked should now go away. */
13324 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13325 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13327 /* If fonts changed, display again. */
13328 /* ??? rms: I suspect it is a mistake to jump all the way
13329 back to retry here. It should just retry this frame. */
13330 if (fonts_changed_p)
13331 goto retry;
13333 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13335 /* See if we have to hscroll. */
13336 if (!f->already_hscrolled_p)
13338 f->already_hscrolled_p = 1;
13339 if (hscroll_windows (f->root_window))
13340 goto retry;
13343 /* Prevent various kinds of signals during display
13344 update. stdio is not robust about handling
13345 signals, which can cause an apparent I/O
13346 error. */
13347 if (interrupt_input)
13348 unrequest_sigio ();
13349 STOP_POLLING;
13351 /* Update the display. */
13352 set_window_update_flags (XWINDOW (f->root_window), 1);
13353 pending |= update_frame (f, 0, 0);
13354 f->updated_p = 1;
13359 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13361 if (!pending)
13363 /* Do the mark_window_display_accurate after all windows have
13364 been redisplayed because this call resets flags in buffers
13365 which are needed for proper redisplay. */
13366 FOR_EACH_FRAME (tail, frame)
13368 struct frame *f = XFRAME (frame);
13369 if (f->updated_p)
13371 mark_window_display_accurate (f->root_window, 1);
13372 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13373 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13378 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13380 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13381 struct frame *mini_frame;
13383 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13384 /* Use list_of_error, not Qerror, so that
13385 we catch only errors and don't run the debugger. */
13386 internal_condition_case_1 (redisplay_window_1, selected_window,
13387 list_of_error,
13388 redisplay_window_error);
13389 if (update_miniwindow_p)
13390 internal_condition_case_1 (redisplay_window_1, mini_window,
13391 list_of_error,
13392 redisplay_window_error);
13394 /* Compare desired and current matrices, perform output. */
13396 update:
13397 /* If fonts changed, display again. */
13398 if (fonts_changed_p)
13399 goto retry;
13401 /* Prevent various kinds of signals during display update.
13402 stdio is not robust about handling signals,
13403 which can cause an apparent I/O error. */
13404 if (interrupt_input)
13405 unrequest_sigio ();
13406 STOP_POLLING;
13408 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13410 if (hscroll_windows (selected_window))
13411 goto retry;
13413 XWINDOW (selected_window)->must_be_updated_p = 1;
13414 pending = update_frame (sf, 0, 0);
13417 /* We may have called echo_area_display at the top of this
13418 function. If the echo area is on another frame, that may
13419 have put text on a frame other than the selected one, so the
13420 above call to update_frame would not have caught it. Catch
13421 it here. */
13422 mini_window = FRAME_MINIBUF_WINDOW (sf);
13423 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13425 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13427 XWINDOW (mini_window)->must_be_updated_p = 1;
13428 pending |= update_frame (mini_frame, 0, 0);
13429 if (!pending && hscroll_windows (mini_window))
13430 goto retry;
13434 /* If display was paused because of pending input, make sure we do a
13435 thorough update the next time. */
13436 if (pending)
13438 /* Prevent the optimization at the beginning of
13439 redisplay_internal that tries a single-line update of the
13440 line containing the cursor in the selected window. */
13441 CHARPOS (this_line_start_pos) = 0;
13443 /* Let the overlay arrow be updated the next time. */
13444 update_overlay_arrows (0);
13446 /* If we pause after scrolling, some rows in the current
13447 matrices of some windows are not valid. */
13448 if (!WINDOW_FULL_WIDTH_P (w)
13449 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13450 update_mode_lines = 1;
13452 else
13454 if (!consider_all_windows_p)
13456 /* This has already been done above if
13457 consider_all_windows_p is set. */
13458 mark_window_display_accurate_1 (w, 1);
13460 /* Say overlay arrows are up to date. */
13461 update_overlay_arrows (1);
13463 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13464 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13467 update_mode_lines = 0;
13468 windows_or_buffers_changed = 0;
13469 cursor_type_changed = 0;
13472 /* Start SIGIO interrupts coming again. Having them off during the
13473 code above makes it less likely one will discard output, but not
13474 impossible, since there might be stuff in the system buffer here.
13475 But it is much hairier to try to do anything about that. */
13476 if (interrupt_input)
13477 request_sigio ();
13478 RESUME_POLLING;
13480 /* If a frame has become visible which was not before, redisplay
13481 again, so that we display it. Expose events for such a frame
13482 (which it gets when becoming visible) don't call the parts of
13483 redisplay constructing glyphs, so simply exposing a frame won't
13484 display anything in this case. So, we have to display these
13485 frames here explicitly. */
13486 if (!pending)
13488 int new_count = 0;
13490 FOR_EACH_FRAME (tail, frame)
13492 int this_is_visible = 0;
13494 if (XFRAME (frame)->visible)
13495 this_is_visible = 1;
13497 if (this_is_visible)
13498 new_count++;
13501 if (new_count != number_of_visible_frames)
13502 windows_or_buffers_changed++;
13505 /* Change frame size now if a change is pending. */
13506 do_pending_window_change (1);
13508 /* If we just did a pending size change, or have additional
13509 visible frames, or selected_window changed, redisplay again. */
13510 if ((windows_or_buffers_changed && !pending)
13511 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13512 goto retry;
13514 /* Clear the face and image caches.
13516 We used to do this only if consider_all_windows_p. But the cache
13517 needs to be cleared if a timer creates images in the current
13518 buffer (e.g. the test case in Bug#6230). */
13520 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13522 clear_face_cache (0);
13523 clear_face_cache_count = 0;
13526 #ifdef HAVE_WINDOW_SYSTEM
13527 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13529 clear_image_caches (Qnil);
13530 clear_image_cache_count = 0;
13532 #endif /* HAVE_WINDOW_SYSTEM */
13534 end_of_redisplay:
13535 backtrace_list = backtrace.next;
13536 unbind_to (count, Qnil);
13537 RESUME_POLLING;
13541 /* Redisplay, but leave alone any recent echo area message unless
13542 another message has been requested in its place.
13544 This is useful in situations where you need to redisplay but no
13545 user action has occurred, making it inappropriate for the message
13546 area to be cleared. See tracking_off and
13547 wait_reading_process_output for examples of these situations.
13549 FROM_WHERE is an integer saying from where this function was
13550 called. This is useful for debugging. */
13552 void
13553 redisplay_preserve_echo_area (int from_where)
13555 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13557 if (!NILP (echo_area_buffer[1]))
13559 /* We have a previously displayed message, but no current
13560 message. Redisplay the previous message. */
13561 display_last_displayed_message_p = 1;
13562 redisplay_internal ();
13563 display_last_displayed_message_p = 0;
13565 else
13566 redisplay_internal ();
13568 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13569 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13570 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13574 /* Function registered with record_unwind_protect in redisplay_internal.
13575 Clear redisplaying_p. Also select the previously selected frame. */
13577 static Lisp_Object
13578 unwind_redisplay (Lisp_Object old_frame)
13580 redisplaying_p = 0;
13581 return Qnil;
13585 /* Mark the display of leaf window W as accurate or inaccurate.
13586 If ACCURATE_P is non-zero mark display of W as accurate. If
13587 ACCURATE_P is zero, arrange for W to be redisplayed the next
13588 time redisplay_internal is called. */
13590 static void
13591 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13593 struct buffer *b = XBUFFER (w->contents);
13595 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13596 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13597 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13599 if (accurate_p)
13601 b->clip_changed = 0;
13602 b->prevent_redisplay_optimizations_p = 0;
13604 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13605 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13606 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13607 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13609 w->current_matrix->buffer = b;
13610 w->current_matrix->begv = BUF_BEGV (b);
13611 w->current_matrix->zv = BUF_ZV (b);
13613 w->last_cursor = w->cursor;
13614 w->last_cursor_off_p = w->cursor_off_p;
13616 if (w == XWINDOW (selected_window))
13617 w->last_point = BUF_PT (b);
13618 else
13619 w->last_point = marker_position (w->pointm);
13621 w->window_end_valid = 1;
13622 w->update_mode_line = 0;
13627 /* Mark the display of windows in the window tree rooted at WINDOW as
13628 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13629 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13630 be redisplayed the next time redisplay_internal is called. */
13632 void
13633 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13635 struct window *w;
13637 for (; !NILP (window); window = w->next)
13639 w = XWINDOW (window);
13640 if (WINDOWP (w->contents))
13641 mark_window_display_accurate (w->contents, accurate_p);
13642 else
13643 mark_window_display_accurate_1 (w, accurate_p);
13646 if (accurate_p)
13647 update_overlay_arrows (1);
13648 else
13649 /* Force a thorough redisplay the next time by setting
13650 last_arrow_position and last_arrow_string to t, which is
13651 unequal to any useful value of Voverlay_arrow_... */
13652 update_overlay_arrows (-1);
13656 /* Return value in display table DP (Lisp_Char_Table *) for character
13657 C. Since a display table doesn't have any parent, we don't have to
13658 follow parent. Do not call this function directly but use the
13659 macro DISP_CHAR_VECTOR. */
13661 Lisp_Object
13662 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13664 Lisp_Object val;
13666 if (ASCII_CHAR_P (c))
13668 val = dp->ascii;
13669 if (SUB_CHAR_TABLE_P (val))
13670 val = XSUB_CHAR_TABLE (val)->contents[c];
13672 else
13674 Lisp_Object table;
13676 XSETCHAR_TABLE (table, dp);
13677 val = char_table_ref (table, c);
13679 if (NILP (val))
13680 val = dp->defalt;
13681 return val;
13686 /***********************************************************************
13687 Window Redisplay
13688 ***********************************************************************/
13690 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13692 static void
13693 redisplay_windows (Lisp_Object window)
13695 while (!NILP (window))
13697 struct window *w = XWINDOW (window);
13699 if (WINDOWP (w->contents))
13700 redisplay_windows (w->contents);
13701 else if (BUFFERP (w->contents))
13703 displayed_buffer = XBUFFER (w->contents);
13704 /* Use list_of_error, not Qerror, so that
13705 we catch only errors and don't run the debugger. */
13706 internal_condition_case_1 (redisplay_window_0, window,
13707 list_of_error,
13708 redisplay_window_error);
13711 window = w->next;
13715 static Lisp_Object
13716 redisplay_window_error (Lisp_Object ignore)
13718 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13719 return Qnil;
13722 static Lisp_Object
13723 redisplay_window_0 (Lisp_Object window)
13725 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13726 redisplay_window (window, 0);
13727 return Qnil;
13730 static Lisp_Object
13731 redisplay_window_1 (Lisp_Object window)
13733 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13734 redisplay_window (window, 1);
13735 return Qnil;
13739 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13740 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13741 which positions recorded in ROW differ from current buffer
13742 positions.
13744 Return 0 if cursor is not on this row, 1 otherwise. */
13746 static int
13747 set_cursor_from_row (struct window *w, struct glyph_row *row,
13748 struct glyph_matrix *matrix,
13749 ptrdiff_t delta, ptrdiff_t delta_bytes,
13750 int dy, int dvpos)
13752 struct glyph *glyph = row->glyphs[TEXT_AREA];
13753 struct glyph *end = glyph + row->used[TEXT_AREA];
13754 struct glyph *cursor = NULL;
13755 /* The last known character position in row. */
13756 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13757 int x = row->x;
13758 ptrdiff_t pt_old = PT - delta;
13759 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13760 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13761 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13762 /* A glyph beyond the edge of TEXT_AREA which we should never
13763 touch. */
13764 struct glyph *glyphs_end = end;
13765 /* Non-zero means we've found a match for cursor position, but that
13766 glyph has the avoid_cursor_p flag set. */
13767 int match_with_avoid_cursor = 0;
13768 /* Non-zero means we've seen at least one glyph that came from a
13769 display string. */
13770 int string_seen = 0;
13771 /* Largest and smallest buffer positions seen so far during scan of
13772 glyph row. */
13773 ptrdiff_t bpos_max = pos_before;
13774 ptrdiff_t bpos_min = pos_after;
13775 /* Last buffer position covered by an overlay string with an integer
13776 `cursor' property. */
13777 ptrdiff_t bpos_covered = 0;
13778 /* Non-zero means the display string on which to display the cursor
13779 comes from a text property, not from an overlay. */
13780 int string_from_text_prop = 0;
13782 /* Don't even try doing anything if called for a mode-line or
13783 header-line row, since the rest of the code isn't prepared to
13784 deal with such calamities. */
13785 eassert (!row->mode_line_p);
13786 if (row->mode_line_p)
13787 return 0;
13789 /* Skip over glyphs not having an object at the start and the end of
13790 the row. These are special glyphs like truncation marks on
13791 terminal frames. */
13792 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13794 if (!row->reversed_p)
13796 while (glyph < end
13797 && INTEGERP (glyph->object)
13798 && glyph->charpos < 0)
13800 x += glyph->pixel_width;
13801 ++glyph;
13803 while (end > glyph
13804 && INTEGERP ((end - 1)->object)
13805 /* CHARPOS is zero for blanks and stretch glyphs
13806 inserted by extend_face_to_end_of_line. */
13807 && (end - 1)->charpos <= 0)
13808 --end;
13809 glyph_before = glyph - 1;
13810 glyph_after = end;
13812 else
13814 struct glyph *g;
13816 /* If the glyph row is reversed, we need to process it from back
13817 to front, so swap the edge pointers. */
13818 glyphs_end = end = glyph - 1;
13819 glyph += row->used[TEXT_AREA] - 1;
13821 while (glyph > end + 1
13822 && INTEGERP (glyph->object)
13823 && glyph->charpos < 0)
13825 --glyph;
13826 x -= glyph->pixel_width;
13828 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13829 --glyph;
13830 /* By default, in reversed rows we put the cursor on the
13831 rightmost (first in the reading order) glyph. */
13832 for (g = end + 1; g < glyph; g++)
13833 x += g->pixel_width;
13834 while (end < glyph
13835 && INTEGERP ((end + 1)->object)
13836 && (end + 1)->charpos <= 0)
13837 ++end;
13838 glyph_before = glyph + 1;
13839 glyph_after = end;
13842 else if (row->reversed_p)
13844 /* In R2L rows that don't display text, put the cursor on the
13845 rightmost glyph. Case in point: an empty last line that is
13846 part of an R2L paragraph. */
13847 cursor = end - 1;
13848 /* Avoid placing the cursor on the last glyph of the row, where
13849 on terminal frames we hold the vertical border between
13850 adjacent windows. */
13851 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13852 && !WINDOW_RIGHTMOST_P (w)
13853 && cursor == row->glyphs[LAST_AREA] - 1)
13854 cursor--;
13855 x = -1; /* will be computed below, at label compute_x */
13858 /* Step 1: Try to find the glyph whose character position
13859 corresponds to point. If that's not possible, find 2 glyphs
13860 whose character positions are the closest to point, one before
13861 point, the other after it. */
13862 if (!row->reversed_p)
13863 while (/* not marched to end of glyph row */
13864 glyph < end
13865 /* glyph was not inserted by redisplay for internal purposes */
13866 && !INTEGERP (glyph->object))
13868 if (BUFFERP (glyph->object))
13870 ptrdiff_t dpos = glyph->charpos - pt_old;
13872 if (glyph->charpos > bpos_max)
13873 bpos_max = glyph->charpos;
13874 if (glyph->charpos < bpos_min)
13875 bpos_min = glyph->charpos;
13876 if (!glyph->avoid_cursor_p)
13878 /* If we hit point, we've found the glyph on which to
13879 display the cursor. */
13880 if (dpos == 0)
13882 match_with_avoid_cursor = 0;
13883 break;
13885 /* See if we've found a better approximation to
13886 POS_BEFORE or to POS_AFTER. */
13887 if (0 > dpos && dpos > pos_before - pt_old)
13889 pos_before = glyph->charpos;
13890 glyph_before = glyph;
13892 else if (0 < dpos && dpos < pos_after - pt_old)
13894 pos_after = glyph->charpos;
13895 glyph_after = glyph;
13898 else if (dpos == 0)
13899 match_with_avoid_cursor = 1;
13901 else if (STRINGP (glyph->object))
13903 Lisp_Object chprop;
13904 ptrdiff_t glyph_pos = glyph->charpos;
13906 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13907 glyph->object);
13908 if (!NILP (chprop))
13910 /* If the string came from a `display' text property,
13911 look up the buffer position of that property and
13912 use that position to update bpos_max, as if we
13913 actually saw such a position in one of the row's
13914 glyphs. This helps with supporting integer values
13915 of `cursor' property on the display string in
13916 situations where most or all of the row's buffer
13917 text is completely covered by display properties,
13918 so that no glyph with valid buffer positions is
13919 ever seen in the row. */
13920 ptrdiff_t prop_pos =
13921 string_buffer_position_lim (glyph->object, pos_before,
13922 pos_after, 0);
13924 if (prop_pos >= pos_before)
13925 bpos_max = prop_pos - 1;
13927 if (INTEGERP (chprop))
13929 bpos_covered = bpos_max + XINT (chprop);
13930 /* If the `cursor' property covers buffer positions up
13931 to and including point, we should display cursor on
13932 this glyph. Note that, if a `cursor' property on one
13933 of the string's characters has an integer value, we
13934 will break out of the loop below _before_ we get to
13935 the position match above. IOW, integer values of
13936 the `cursor' property override the "exact match for
13937 point" strategy of positioning the cursor. */
13938 /* Implementation note: bpos_max == pt_old when, e.g.,
13939 we are in an empty line, where bpos_max is set to
13940 MATRIX_ROW_START_CHARPOS, see above. */
13941 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13943 cursor = glyph;
13944 break;
13948 string_seen = 1;
13950 x += glyph->pixel_width;
13951 ++glyph;
13953 else if (glyph > end) /* row is reversed */
13954 while (!INTEGERP (glyph->object))
13956 if (BUFFERP (glyph->object))
13958 ptrdiff_t dpos = glyph->charpos - pt_old;
13960 if (glyph->charpos > bpos_max)
13961 bpos_max = glyph->charpos;
13962 if (glyph->charpos < bpos_min)
13963 bpos_min = glyph->charpos;
13964 if (!glyph->avoid_cursor_p)
13966 if (dpos == 0)
13968 match_with_avoid_cursor = 0;
13969 break;
13971 if (0 > dpos && dpos > pos_before - pt_old)
13973 pos_before = glyph->charpos;
13974 glyph_before = glyph;
13976 else if (0 < dpos && dpos < pos_after - pt_old)
13978 pos_after = glyph->charpos;
13979 glyph_after = glyph;
13982 else if (dpos == 0)
13983 match_with_avoid_cursor = 1;
13985 else if (STRINGP (glyph->object))
13987 Lisp_Object chprop;
13988 ptrdiff_t glyph_pos = glyph->charpos;
13990 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13991 glyph->object);
13992 if (!NILP (chprop))
13994 ptrdiff_t prop_pos =
13995 string_buffer_position_lim (glyph->object, pos_before,
13996 pos_after, 0);
13998 if (prop_pos >= pos_before)
13999 bpos_max = prop_pos - 1;
14001 if (INTEGERP (chprop))
14003 bpos_covered = bpos_max + XINT (chprop);
14004 /* If the `cursor' property covers buffer positions up
14005 to and including point, we should display cursor on
14006 this glyph. */
14007 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14009 cursor = glyph;
14010 break;
14013 string_seen = 1;
14015 --glyph;
14016 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14018 x--; /* can't use any pixel_width */
14019 break;
14021 x -= glyph->pixel_width;
14024 /* Step 2: If we didn't find an exact match for point, we need to
14025 look for a proper place to put the cursor among glyphs between
14026 GLYPH_BEFORE and GLYPH_AFTER. */
14027 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14028 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14029 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14031 /* An empty line has a single glyph whose OBJECT is zero and
14032 whose CHARPOS is the position of a newline on that line.
14033 Note that on a TTY, there are more glyphs after that, which
14034 were produced by extend_face_to_end_of_line, but their
14035 CHARPOS is zero or negative. */
14036 int empty_line_p =
14037 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14038 && INTEGERP (glyph->object) && glyph->charpos > 0
14039 /* On a TTY, continued and truncated rows also have a glyph at
14040 their end whose OBJECT is zero and whose CHARPOS is
14041 positive (the continuation and truncation glyphs), but such
14042 rows are obviously not "empty". */
14043 && !(row->continued_p || row->truncated_on_right_p);
14045 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14047 ptrdiff_t ellipsis_pos;
14049 /* Scan back over the ellipsis glyphs. */
14050 if (!row->reversed_p)
14052 ellipsis_pos = (glyph - 1)->charpos;
14053 while (glyph > row->glyphs[TEXT_AREA]
14054 && (glyph - 1)->charpos == ellipsis_pos)
14055 glyph--, x -= glyph->pixel_width;
14056 /* That loop always goes one position too far, including
14057 the glyph before the ellipsis. So scan forward over
14058 that one. */
14059 x += glyph->pixel_width;
14060 glyph++;
14062 else /* row is reversed */
14064 ellipsis_pos = (glyph + 1)->charpos;
14065 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14066 && (glyph + 1)->charpos == ellipsis_pos)
14067 glyph++, x += glyph->pixel_width;
14068 x -= glyph->pixel_width;
14069 glyph--;
14072 else if (match_with_avoid_cursor)
14074 cursor = glyph_after;
14075 x = -1;
14077 else if (string_seen)
14079 int incr = row->reversed_p ? -1 : +1;
14081 /* Need to find the glyph that came out of a string which is
14082 present at point. That glyph is somewhere between
14083 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14084 positioned between POS_BEFORE and POS_AFTER in the
14085 buffer. */
14086 struct glyph *start, *stop;
14087 ptrdiff_t pos = pos_before;
14089 x = -1;
14091 /* If the row ends in a newline from a display string,
14092 reordering could have moved the glyphs belonging to the
14093 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14094 in this case we extend the search to the last glyph in
14095 the row that was not inserted by redisplay. */
14096 if (row->ends_in_newline_from_string_p)
14098 glyph_after = end;
14099 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14102 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14103 correspond to POS_BEFORE and POS_AFTER, respectively. We
14104 need START and STOP in the order that corresponds to the
14105 row's direction as given by its reversed_p flag. If the
14106 directionality of characters between POS_BEFORE and
14107 POS_AFTER is the opposite of the row's base direction,
14108 these characters will have been reordered for display,
14109 and we need to reverse START and STOP. */
14110 if (!row->reversed_p)
14112 start = min (glyph_before, glyph_after);
14113 stop = max (glyph_before, glyph_after);
14115 else
14117 start = max (glyph_before, glyph_after);
14118 stop = min (glyph_before, glyph_after);
14120 for (glyph = start + incr;
14121 row->reversed_p ? glyph > stop : glyph < stop; )
14124 /* Any glyphs that come from the buffer are here because
14125 of bidi reordering. Skip them, and only pay
14126 attention to glyphs that came from some string. */
14127 if (STRINGP (glyph->object))
14129 Lisp_Object str;
14130 ptrdiff_t tem;
14131 /* If the display property covers the newline, we
14132 need to search for it one position farther. */
14133 ptrdiff_t lim = pos_after
14134 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14136 string_from_text_prop = 0;
14137 str = glyph->object;
14138 tem = string_buffer_position_lim (str, pos, lim, 0);
14139 if (tem == 0 /* from overlay */
14140 || pos <= tem)
14142 /* If the string from which this glyph came is
14143 found in the buffer at point, or at position
14144 that is closer to point than pos_after, then
14145 we've found the glyph we've been looking for.
14146 If it comes from an overlay (tem == 0), and
14147 it has the `cursor' property on one of its
14148 glyphs, record that glyph as a candidate for
14149 displaying the cursor. (As in the
14150 unidirectional version, we will display the
14151 cursor on the last candidate we find.) */
14152 if (tem == 0
14153 || tem == pt_old
14154 || (tem - pt_old > 0 && tem < pos_after))
14156 /* The glyphs from this string could have
14157 been reordered. Find the one with the
14158 smallest string position. Or there could
14159 be a character in the string with the
14160 `cursor' property, which means display
14161 cursor on that character's glyph. */
14162 ptrdiff_t strpos = glyph->charpos;
14164 if (tem)
14166 cursor = glyph;
14167 string_from_text_prop = 1;
14169 for ( ;
14170 (row->reversed_p ? glyph > stop : glyph < stop)
14171 && EQ (glyph->object, str);
14172 glyph += incr)
14174 Lisp_Object cprop;
14175 ptrdiff_t gpos = glyph->charpos;
14177 cprop = Fget_char_property (make_number (gpos),
14178 Qcursor,
14179 glyph->object);
14180 if (!NILP (cprop))
14182 cursor = glyph;
14183 break;
14185 if (tem && glyph->charpos < strpos)
14187 strpos = glyph->charpos;
14188 cursor = glyph;
14192 if (tem == pt_old
14193 || (tem - pt_old > 0 && tem < pos_after))
14194 goto compute_x;
14196 if (tem)
14197 pos = tem + 1; /* don't find previous instances */
14199 /* This string is not what we want; skip all of the
14200 glyphs that came from it. */
14201 while ((row->reversed_p ? glyph > stop : glyph < stop)
14202 && EQ (glyph->object, str))
14203 glyph += incr;
14205 else
14206 glyph += incr;
14209 /* If we reached the end of the line, and END was from a string,
14210 the cursor is not on this line. */
14211 if (cursor == NULL
14212 && (row->reversed_p ? glyph <= end : glyph >= end)
14213 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14214 && STRINGP (end->object)
14215 && row->continued_p)
14216 return 0;
14218 /* A truncated row may not include PT among its character positions.
14219 Setting the cursor inside the scroll margin will trigger
14220 recalculation of hscroll in hscroll_window_tree. But if a
14221 display string covers point, defer to the string-handling
14222 code below to figure this out. */
14223 else if (row->truncated_on_left_p && pt_old < bpos_min)
14225 cursor = glyph_before;
14226 x = -1;
14228 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14229 /* Zero-width characters produce no glyphs. */
14230 || (!empty_line_p
14231 && (row->reversed_p
14232 ? glyph_after > glyphs_end
14233 : glyph_after < glyphs_end)))
14235 cursor = glyph_after;
14236 x = -1;
14240 compute_x:
14241 if (cursor != NULL)
14242 glyph = cursor;
14243 else if (glyph == glyphs_end
14244 && pos_before == pos_after
14245 && STRINGP ((row->reversed_p
14246 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14247 : row->glyphs[TEXT_AREA])->object))
14249 /* If all the glyphs of this row came from strings, put the
14250 cursor on the first glyph of the row. This avoids having the
14251 cursor outside of the text area in this very rare and hard
14252 use case. */
14253 glyph =
14254 row->reversed_p
14255 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14256 : row->glyphs[TEXT_AREA];
14258 if (x < 0)
14260 struct glyph *g;
14262 /* Need to compute x that corresponds to GLYPH. */
14263 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14265 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14266 emacs_abort ();
14267 x += g->pixel_width;
14271 /* ROW could be part of a continued line, which, under bidi
14272 reordering, might have other rows whose start and end charpos
14273 occlude point. Only set w->cursor if we found a better
14274 approximation to the cursor position than we have from previously
14275 examined candidate rows belonging to the same continued line. */
14276 if (/* we already have a candidate row */
14277 w->cursor.vpos >= 0
14278 /* that candidate is not the row we are processing */
14279 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14280 /* Make sure cursor.vpos specifies a row whose start and end
14281 charpos occlude point, and it is valid candidate for being a
14282 cursor-row. This is because some callers of this function
14283 leave cursor.vpos at the row where the cursor was displayed
14284 during the last redisplay cycle. */
14285 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14286 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14287 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14289 struct glyph *g1 =
14290 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14292 /* Don't consider glyphs that are outside TEXT_AREA. */
14293 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14294 return 0;
14295 /* Keep the candidate whose buffer position is the closest to
14296 point or has the `cursor' property. */
14297 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14298 w->cursor.hpos >= 0
14299 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14300 && ((BUFFERP (g1->object)
14301 && (g1->charpos == pt_old /* an exact match always wins */
14302 || (BUFFERP (glyph->object)
14303 && eabs (g1->charpos - pt_old)
14304 < eabs (glyph->charpos - pt_old))))
14305 /* previous candidate is a glyph from a string that has
14306 a non-nil `cursor' property */
14307 || (STRINGP (g1->object)
14308 && (!NILP (Fget_char_property (make_number (g1->charpos),
14309 Qcursor, g1->object))
14310 /* previous candidate is from the same display
14311 string as this one, and the display string
14312 came from a text property */
14313 || (EQ (g1->object, glyph->object)
14314 && string_from_text_prop)
14315 /* this candidate is from newline and its
14316 position is not an exact match */
14317 || (INTEGERP (glyph->object)
14318 && glyph->charpos != pt_old)))))
14319 return 0;
14320 /* If this candidate gives an exact match, use that. */
14321 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14322 /* If this candidate is a glyph created for the
14323 terminating newline of a line, and point is on that
14324 newline, it wins because it's an exact match. */
14325 || (!row->continued_p
14326 && INTEGERP (glyph->object)
14327 && glyph->charpos == 0
14328 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14329 /* Otherwise, keep the candidate that comes from a row
14330 spanning less buffer positions. This may win when one or
14331 both candidate positions are on glyphs that came from
14332 display strings, for which we cannot compare buffer
14333 positions. */
14334 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14335 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14336 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14337 return 0;
14339 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14340 w->cursor.x = x;
14341 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14342 w->cursor.y = row->y + dy;
14344 if (w == XWINDOW (selected_window))
14346 if (!row->continued_p
14347 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14348 && row->x == 0)
14350 this_line_buffer = XBUFFER (w->contents);
14352 CHARPOS (this_line_start_pos)
14353 = MATRIX_ROW_START_CHARPOS (row) + delta;
14354 BYTEPOS (this_line_start_pos)
14355 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14357 CHARPOS (this_line_end_pos)
14358 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14359 BYTEPOS (this_line_end_pos)
14360 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14362 this_line_y = w->cursor.y;
14363 this_line_pixel_height = row->height;
14364 this_line_vpos = w->cursor.vpos;
14365 this_line_start_x = row->x;
14367 else
14368 CHARPOS (this_line_start_pos) = 0;
14371 return 1;
14375 /* Run window scroll functions, if any, for WINDOW with new window
14376 start STARTP. Sets the window start of WINDOW to that position.
14378 We assume that the window's buffer is really current. */
14380 static struct text_pos
14381 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14383 struct window *w = XWINDOW (window);
14384 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14386 if (current_buffer != XBUFFER (w->contents))
14387 emacs_abort ();
14389 if (!NILP (Vwindow_scroll_functions))
14391 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14392 make_number (CHARPOS (startp)));
14393 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14394 /* In case the hook functions switch buffers. */
14395 set_buffer_internal (XBUFFER (w->contents));
14398 return startp;
14402 /* Make sure the line containing the cursor is fully visible.
14403 A value of 1 means there is nothing to be done.
14404 (Either the line is fully visible, or it cannot be made so,
14405 or we cannot tell.)
14407 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14408 is higher than window.
14410 A value of 0 means the caller should do scrolling
14411 as if point had gone off the screen. */
14413 static int
14414 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14416 struct glyph_matrix *matrix;
14417 struct glyph_row *row;
14418 int window_height;
14420 if (!make_cursor_line_fully_visible_p)
14421 return 1;
14423 /* It's not always possible to find the cursor, e.g, when a window
14424 is full of overlay strings. Don't do anything in that case. */
14425 if (w->cursor.vpos < 0)
14426 return 1;
14428 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14429 row = MATRIX_ROW (matrix, w->cursor.vpos);
14431 /* If the cursor row is not partially visible, there's nothing to do. */
14432 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14433 return 1;
14435 /* If the row the cursor is in is taller than the window's height,
14436 it's not clear what to do, so do nothing. */
14437 window_height = window_box_height (w);
14438 if (row->height >= window_height)
14440 if (!force_p || MINI_WINDOW_P (w)
14441 || w->vscroll || w->cursor.vpos == 0)
14442 return 1;
14444 return 0;
14448 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14449 non-zero means only WINDOW is redisplayed in redisplay_internal.
14450 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14451 in redisplay_window to bring a partially visible line into view in
14452 the case that only the cursor has moved.
14454 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14455 last screen line's vertical height extends past the end of the screen.
14457 Value is
14459 1 if scrolling succeeded
14461 0 if scrolling didn't find point.
14463 -1 if new fonts have been loaded so that we must interrupt
14464 redisplay, adjust glyph matrices, and try again. */
14466 enum
14468 SCROLLING_SUCCESS,
14469 SCROLLING_FAILED,
14470 SCROLLING_NEED_LARGER_MATRICES
14473 /* If scroll-conservatively is more than this, never recenter.
14475 If you change this, don't forget to update the doc string of
14476 `scroll-conservatively' and the Emacs manual. */
14477 #define SCROLL_LIMIT 100
14479 static int
14480 try_scrolling (Lisp_Object window, int just_this_one_p,
14481 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14482 int temp_scroll_step, int last_line_misfit)
14484 struct window *w = XWINDOW (window);
14485 struct frame *f = XFRAME (w->frame);
14486 struct text_pos pos, startp;
14487 struct it it;
14488 int this_scroll_margin, scroll_max, rc, height;
14489 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14490 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14491 Lisp_Object aggressive;
14492 /* We will never try scrolling more than this number of lines. */
14493 int scroll_limit = SCROLL_LIMIT;
14495 #ifdef GLYPH_DEBUG
14496 debug_method_add (w, "try_scrolling");
14497 #endif
14499 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14501 /* Compute scroll margin height in pixels. We scroll when point is
14502 within this distance from the top or bottom of the window. */
14503 if (scroll_margin > 0)
14504 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14505 * FRAME_LINE_HEIGHT (f);
14506 else
14507 this_scroll_margin = 0;
14509 /* Force arg_scroll_conservatively to have a reasonable value, to
14510 avoid scrolling too far away with slow move_it_* functions. Note
14511 that the user can supply scroll-conservatively equal to
14512 `most-positive-fixnum', which can be larger than INT_MAX. */
14513 if (arg_scroll_conservatively > scroll_limit)
14515 arg_scroll_conservatively = scroll_limit + 1;
14516 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14518 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14519 /* Compute how much we should try to scroll maximally to bring
14520 point into view. */
14521 scroll_max = (max (scroll_step,
14522 max (arg_scroll_conservatively, temp_scroll_step))
14523 * FRAME_LINE_HEIGHT (f));
14524 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14525 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14526 /* We're trying to scroll because of aggressive scrolling but no
14527 scroll_step is set. Choose an arbitrary one. */
14528 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14529 else
14530 scroll_max = 0;
14532 too_near_end:
14534 /* Decide whether to scroll down. */
14535 if (PT > CHARPOS (startp))
14537 int scroll_margin_y;
14539 /* Compute the pixel ypos of the scroll margin, then move IT to
14540 either that ypos or PT, whichever comes first. */
14541 start_display (&it, w, startp);
14542 scroll_margin_y = it.last_visible_y - this_scroll_margin
14543 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14544 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14545 (MOVE_TO_POS | MOVE_TO_Y));
14547 if (PT > CHARPOS (it.current.pos))
14549 int y0 = line_bottom_y (&it);
14550 /* Compute how many pixels below window bottom to stop searching
14551 for PT. This avoids costly search for PT that is far away if
14552 the user limited scrolling by a small number of lines, but
14553 always finds PT if scroll_conservatively is set to a large
14554 number, such as most-positive-fixnum. */
14555 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14556 int y_to_move = it.last_visible_y + slack;
14558 /* Compute the distance from the scroll margin to PT or to
14559 the scroll limit, whichever comes first. This should
14560 include the height of the cursor line, to make that line
14561 fully visible. */
14562 move_it_to (&it, PT, -1, y_to_move,
14563 -1, MOVE_TO_POS | MOVE_TO_Y);
14564 dy = line_bottom_y (&it) - y0;
14566 if (dy > scroll_max)
14567 return SCROLLING_FAILED;
14569 if (dy > 0)
14570 scroll_down_p = 1;
14574 if (scroll_down_p)
14576 /* Point is in or below the bottom scroll margin, so move the
14577 window start down. If scrolling conservatively, move it just
14578 enough down to make point visible. If scroll_step is set,
14579 move it down by scroll_step. */
14580 if (arg_scroll_conservatively)
14581 amount_to_scroll
14582 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14583 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14584 else if (scroll_step || temp_scroll_step)
14585 amount_to_scroll = scroll_max;
14586 else
14588 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14589 height = WINDOW_BOX_TEXT_HEIGHT (w);
14590 if (NUMBERP (aggressive))
14592 double float_amount = XFLOATINT (aggressive) * height;
14593 int aggressive_scroll = float_amount;
14594 if (aggressive_scroll == 0 && float_amount > 0)
14595 aggressive_scroll = 1;
14596 /* Don't let point enter the scroll margin near top of
14597 the window. This could happen if the value of
14598 scroll_up_aggressively is too large and there are
14599 non-zero margins, because scroll_up_aggressively
14600 means put point that fraction of window height
14601 _from_the_bottom_margin_. */
14602 if (aggressive_scroll + 2*this_scroll_margin > height)
14603 aggressive_scroll = height - 2*this_scroll_margin;
14604 amount_to_scroll = dy + aggressive_scroll;
14608 if (amount_to_scroll <= 0)
14609 return SCROLLING_FAILED;
14611 start_display (&it, w, startp);
14612 if (arg_scroll_conservatively <= scroll_limit)
14613 move_it_vertically (&it, amount_to_scroll);
14614 else
14616 /* Extra precision for users who set scroll-conservatively
14617 to a large number: make sure the amount we scroll
14618 the window start is never less than amount_to_scroll,
14619 which was computed as distance from window bottom to
14620 point. This matters when lines at window top and lines
14621 below window bottom have different height. */
14622 struct it it1;
14623 void *it1data = NULL;
14624 /* We use a temporary it1 because line_bottom_y can modify
14625 its argument, if it moves one line down; see there. */
14626 int start_y;
14628 SAVE_IT (it1, it, it1data);
14629 start_y = line_bottom_y (&it1);
14630 do {
14631 RESTORE_IT (&it, &it, it1data);
14632 move_it_by_lines (&it, 1);
14633 SAVE_IT (it1, it, it1data);
14634 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14637 /* If STARTP is unchanged, move it down another screen line. */
14638 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14639 move_it_by_lines (&it, 1);
14640 startp = it.current.pos;
14642 else
14644 struct text_pos scroll_margin_pos = startp;
14645 int y_offset = 0;
14647 /* See if point is inside the scroll margin at the top of the
14648 window. */
14649 if (this_scroll_margin)
14651 int y_start;
14653 start_display (&it, w, startp);
14654 y_start = it.current_y;
14655 move_it_vertically (&it, this_scroll_margin);
14656 scroll_margin_pos = it.current.pos;
14657 /* If we didn't move enough before hitting ZV, request
14658 additional amount of scroll, to move point out of the
14659 scroll margin. */
14660 if (IT_CHARPOS (it) == ZV
14661 && it.current_y - y_start < this_scroll_margin)
14662 y_offset = this_scroll_margin - (it.current_y - y_start);
14665 if (PT < CHARPOS (scroll_margin_pos))
14667 /* Point is in the scroll margin at the top of the window or
14668 above what is displayed in the window. */
14669 int y0, y_to_move;
14671 /* Compute the vertical distance from PT to the scroll
14672 margin position. Move as far as scroll_max allows, or
14673 one screenful, or 10 screen lines, whichever is largest.
14674 Give up if distance is greater than scroll_max or if we
14675 didn't reach the scroll margin position. */
14676 SET_TEXT_POS (pos, PT, PT_BYTE);
14677 start_display (&it, w, pos);
14678 y0 = it.current_y;
14679 y_to_move = max (it.last_visible_y,
14680 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14681 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14682 y_to_move, -1,
14683 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14684 dy = it.current_y - y0;
14685 if (dy > scroll_max
14686 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14687 return SCROLLING_FAILED;
14689 /* Additional scroll for when ZV was too close to point. */
14690 dy += y_offset;
14692 /* Compute new window start. */
14693 start_display (&it, w, startp);
14695 if (arg_scroll_conservatively)
14696 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14697 max (scroll_step, temp_scroll_step));
14698 else if (scroll_step || temp_scroll_step)
14699 amount_to_scroll = scroll_max;
14700 else
14702 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14703 height = WINDOW_BOX_TEXT_HEIGHT (w);
14704 if (NUMBERP (aggressive))
14706 double float_amount = XFLOATINT (aggressive) * height;
14707 int aggressive_scroll = float_amount;
14708 if (aggressive_scroll == 0 && float_amount > 0)
14709 aggressive_scroll = 1;
14710 /* Don't let point enter the scroll margin near
14711 bottom of the window, if the value of
14712 scroll_down_aggressively happens to be too
14713 large. */
14714 if (aggressive_scroll + 2*this_scroll_margin > height)
14715 aggressive_scroll = height - 2*this_scroll_margin;
14716 amount_to_scroll = dy + aggressive_scroll;
14720 if (amount_to_scroll <= 0)
14721 return SCROLLING_FAILED;
14723 move_it_vertically_backward (&it, amount_to_scroll);
14724 startp = it.current.pos;
14728 /* Run window scroll functions. */
14729 startp = run_window_scroll_functions (window, startp);
14731 /* Display the window. Give up if new fonts are loaded, or if point
14732 doesn't appear. */
14733 if (!try_window (window, startp, 0))
14734 rc = SCROLLING_NEED_LARGER_MATRICES;
14735 else if (w->cursor.vpos < 0)
14737 clear_glyph_matrix (w->desired_matrix);
14738 rc = SCROLLING_FAILED;
14740 else
14742 /* Maybe forget recorded base line for line number display. */
14743 if (!just_this_one_p
14744 || current_buffer->clip_changed
14745 || BEG_UNCHANGED < CHARPOS (startp))
14746 w->base_line_number = 0;
14748 /* If cursor ends up on a partially visible line,
14749 treat that as being off the bottom of the screen. */
14750 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14751 /* It's possible that the cursor is on the first line of the
14752 buffer, which is partially obscured due to a vscroll
14753 (Bug#7537). In that case, avoid looping forever . */
14754 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14756 clear_glyph_matrix (w->desired_matrix);
14757 ++extra_scroll_margin_lines;
14758 goto too_near_end;
14760 rc = SCROLLING_SUCCESS;
14763 return rc;
14767 /* Compute a suitable window start for window W if display of W starts
14768 on a continuation line. Value is non-zero if a new window start
14769 was computed.
14771 The new window start will be computed, based on W's width, starting
14772 from the start of the continued line. It is the start of the
14773 screen line with the minimum distance from the old start W->start. */
14775 static int
14776 compute_window_start_on_continuation_line (struct window *w)
14778 struct text_pos pos, start_pos;
14779 int window_start_changed_p = 0;
14781 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14783 /* If window start is on a continuation line... Window start may be
14784 < BEGV in case there's invisible text at the start of the
14785 buffer (M-x rmail, for example). */
14786 if (CHARPOS (start_pos) > BEGV
14787 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14789 struct it it;
14790 struct glyph_row *row;
14792 /* Handle the case that the window start is out of range. */
14793 if (CHARPOS (start_pos) < BEGV)
14794 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14795 else if (CHARPOS (start_pos) > ZV)
14796 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14798 /* Find the start of the continued line. This should be fast
14799 because find_newline is fast (newline cache). */
14800 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14801 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14802 row, DEFAULT_FACE_ID);
14803 reseat_at_previous_visible_line_start (&it);
14805 /* If the line start is "too far" away from the window start,
14806 say it takes too much time to compute a new window start. */
14807 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14808 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14810 int min_distance, distance;
14812 /* Move forward by display lines to find the new window
14813 start. If window width was enlarged, the new start can
14814 be expected to be > the old start. If window width was
14815 decreased, the new window start will be < the old start.
14816 So, we're looking for the display line start with the
14817 minimum distance from the old window start. */
14818 pos = it.current.pos;
14819 min_distance = INFINITY;
14820 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14821 distance < min_distance)
14823 min_distance = distance;
14824 pos = it.current.pos;
14825 move_it_by_lines (&it, 1);
14828 /* Set the window start there. */
14829 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14830 window_start_changed_p = 1;
14834 return window_start_changed_p;
14838 /* Try cursor movement in case text has not changed in window WINDOW,
14839 with window start STARTP. Value is
14841 CURSOR_MOVEMENT_SUCCESS if successful
14843 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14845 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14846 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14847 we want to scroll as if scroll-step were set to 1. See the code.
14849 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14850 which case we have to abort this redisplay, and adjust matrices
14851 first. */
14853 enum
14855 CURSOR_MOVEMENT_SUCCESS,
14856 CURSOR_MOVEMENT_CANNOT_BE_USED,
14857 CURSOR_MOVEMENT_MUST_SCROLL,
14858 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14861 static int
14862 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14864 struct window *w = XWINDOW (window);
14865 struct frame *f = XFRAME (w->frame);
14866 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14868 #ifdef GLYPH_DEBUG
14869 if (inhibit_try_cursor_movement)
14870 return rc;
14871 #endif
14873 /* Previously, there was a check for Lisp integer in the
14874 if-statement below. Now, this field is converted to
14875 ptrdiff_t, thus zero means invalid position in a buffer. */
14876 eassert (w->last_point > 0);
14878 /* Handle case where text has not changed, only point, and it has
14879 not moved off the frame. */
14880 if (/* Point may be in this window. */
14881 PT >= CHARPOS (startp)
14882 /* Selective display hasn't changed. */
14883 && !current_buffer->clip_changed
14884 /* Function force-mode-line-update is used to force a thorough
14885 redisplay. It sets either windows_or_buffers_changed or
14886 update_mode_lines. So don't take a shortcut here for these
14887 cases. */
14888 && !update_mode_lines
14889 && !windows_or_buffers_changed
14890 && !cursor_type_changed
14891 /* Can't use this case if highlighting a region. When a
14892 region exists, cursor movement has to do more than just
14893 set the cursor. */
14894 && markpos_of_region () < 0
14895 && !w->region_showing
14896 && NILP (Vshow_trailing_whitespace)
14897 /* This code is not used for mini-buffer for the sake of the case
14898 of redisplaying to replace an echo area message; since in
14899 that case the mini-buffer contents per se are usually
14900 unchanged. This code is of no real use in the mini-buffer
14901 since the handling of this_line_start_pos, etc., in redisplay
14902 handles the same cases. */
14903 && !EQ (window, minibuf_window)
14904 /* When splitting windows or for new windows, it happens that
14905 redisplay is called with a nil window_end_vpos or one being
14906 larger than the window. This should really be fixed in
14907 window.c. I don't have this on my list, now, so we do
14908 approximately the same as the old redisplay code. --gerd. */
14909 && INTEGERP (w->window_end_vpos)
14910 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14911 && (FRAME_WINDOW_P (f)
14912 || !overlay_arrow_in_current_buffer_p ()))
14914 int this_scroll_margin, top_scroll_margin;
14915 struct glyph_row *row = NULL;
14917 #ifdef GLYPH_DEBUG
14918 debug_method_add (w, "cursor movement");
14919 #endif
14921 /* Scroll if point within this distance from the top or bottom
14922 of the window. This is a pixel value. */
14923 if (scroll_margin > 0)
14925 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14926 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14928 else
14929 this_scroll_margin = 0;
14931 top_scroll_margin = this_scroll_margin;
14932 if (WINDOW_WANTS_HEADER_LINE_P (w))
14933 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14935 /* Start with the row the cursor was displayed during the last
14936 not paused redisplay. Give up if that row is not valid. */
14937 if (w->last_cursor.vpos < 0
14938 || w->last_cursor.vpos >= w->current_matrix->nrows)
14939 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14940 else
14942 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14943 if (row->mode_line_p)
14944 ++row;
14945 if (!row->enabled_p)
14946 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14949 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14951 int scroll_p = 0, must_scroll = 0;
14952 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14954 if (PT > w->last_point)
14956 /* Point has moved forward. */
14957 while (MATRIX_ROW_END_CHARPOS (row) < PT
14958 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14960 eassert (row->enabled_p);
14961 ++row;
14964 /* If the end position of a row equals the start
14965 position of the next row, and PT is at that position,
14966 we would rather display cursor in the next line. */
14967 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14968 && MATRIX_ROW_END_CHARPOS (row) == PT
14969 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
14970 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14971 && !cursor_row_p (row))
14972 ++row;
14974 /* If within the scroll margin, scroll. Note that
14975 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14976 the next line would be drawn, and that
14977 this_scroll_margin can be zero. */
14978 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14979 || PT > MATRIX_ROW_END_CHARPOS (row)
14980 /* Line is completely visible last line in window
14981 and PT is to be set in the next line. */
14982 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14983 && PT == MATRIX_ROW_END_CHARPOS (row)
14984 && !row->ends_at_zv_p
14985 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14986 scroll_p = 1;
14988 else if (PT < w->last_point)
14990 /* Cursor has to be moved backward. Note that PT >=
14991 CHARPOS (startp) because of the outer if-statement. */
14992 while (!row->mode_line_p
14993 && (MATRIX_ROW_START_CHARPOS (row) > PT
14994 || (MATRIX_ROW_START_CHARPOS (row) == PT
14995 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14996 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14997 row > w->current_matrix->rows
14998 && (row-1)->ends_in_newline_from_string_p))))
14999 && (row->y > top_scroll_margin
15000 || CHARPOS (startp) == BEGV))
15002 eassert (row->enabled_p);
15003 --row;
15006 /* Consider the following case: Window starts at BEGV,
15007 there is invisible, intangible text at BEGV, so that
15008 display starts at some point START > BEGV. It can
15009 happen that we are called with PT somewhere between
15010 BEGV and START. Try to handle that case. */
15011 if (row < w->current_matrix->rows
15012 || row->mode_line_p)
15014 row = w->current_matrix->rows;
15015 if (row->mode_line_p)
15016 ++row;
15019 /* Due to newlines in overlay strings, we may have to
15020 skip forward over overlay strings. */
15021 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15022 && MATRIX_ROW_END_CHARPOS (row) == PT
15023 && !cursor_row_p (row))
15024 ++row;
15026 /* If within the scroll margin, scroll. */
15027 if (row->y < top_scroll_margin
15028 && CHARPOS (startp) != BEGV)
15029 scroll_p = 1;
15031 else
15033 /* Cursor did not move. So don't scroll even if cursor line
15034 is partially visible, as it was so before. */
15035 rc = CURSOR_MOVEMENT_SUCCESS;
15038 if (PT < MATRIX_ROW_START_CHARPOS (row)
15039 || PT > MATRIX_ROW_END_CHARPOS (row))
15041 /* if PT is not in the glyph row, give up. */
15042 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15043 must_scroll = 1;
15045 else if (rc != CURSOR_MOVEMENT_SUCCESS
15046 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15048 struct glyph_row *row1;
15050 /* If rows are bidi-reordered and point moved, back up
15051 until we find a row that does not belong to a
15052 continuation line. This is because we must consider
15053 all rows of a continued line as candidates for the
15054 new cursor positioning, since row start and end
15055 positions change non-linearly with vertical position
15056 in such rows. */
15057 /* FIXME: Revisit this when glyph ``spilling'' in
15058 continuation lines' rows is implemented for
15059 bidi-reordered rows. */
15060 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15061 MATRIX_ROW_CONTINUATION_LINE_P (row);
15062 --row)
15064 /* If we hit the beginning of the displayed portion
15065 without finding the first row of a continued
15066 line, give up. */
15067 if (row <= row1)
15069 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15070 break;
15072 eassert (row->enabled_p);
15075 if (must_scroll)
15077 else if (rc != CURSOR_MOVEMENT_SUCCESS
15078 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15079 /* Make sure this isn't a header line by any chance, since
15080 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15081 && !row->mode_line_p
15082 && make_cursor_line_fully_visible_p)
15084 if (PT == MATRIX_ROW_END_CHARPOS (row)
15085 && !row->ends_at_zv_p
15086 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15087 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15088 else if (row->height > window_box_height (w))
15090 /* If we end up in a partially visible line, let's
15091 make it fully visible, except when it's taller
15092 than the window, in which case we can't do much
15093 about it. */
15094 *scroll_step = 1;
15095 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15097 else
15099 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15100 if (!cursor_row_fully_visible_p (w, 0, 1))
15101 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15102 else
15103 rc = CURSOR_MOVEMENT_SUCCESS;
15106 else if (scroll_p)
15107 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15108 else if (rc != CURSOR_MOVEMENT_SUCCESS
15109 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15111 /* With bidi-reordered rows, there could be more than
15112 one candidate row whose start and end positions
15113 occlude point. We need to let set_cursor_from_row
15114 find the best candidate. */
15115 /* FIXME: Revisit this when glyph ``spilling'' in
15116 continuation lines' rows is implemented for
15117 bidi-reordered rows. */
15118 int rv = 0;
15122 int at_zv_p = 0, exact_match_p = 0;
15124 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15125 && PT <= MATRIX_ROW_END_CHARPOS (row)
15126 && cursor_row_p (row))
15127 rv |= set_cursor_from_row (w, row, w->current_matrix,
15128 0, 0, 0, 0);
15129 /* As soon as we've found the exact match for point,
15130 or the first suitable row whose ends_at_zv_p flag
15131 is set, we are done. */
15132 at_zv_p =
15133 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15134 if (rv && !at_zv_p
15135 && w->cursor.hpos >= 0
15136 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15137 w->cursor.vpos))
15139 struct glyph_row *candidate =
15140 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15141 struct glyph *g =
15142 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15143 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15145 exact_match_p =
15146 (BUFFERP (g->object) && g->charpos == PT)
15147 || (INTEGERP (g->object)
15148 && (g->charpos == PT
15149 || (g->charpos == 0 && endpos - 1 == PT)));
15151 if (rv && (at_zv_p || exact_match_p))
15153 rc = CURSOR_MOVEMENT_SUCCESS;
15154 break;
15156 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15157 break;
15158 ++row;
15160 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15161 || row->continued_p)
15162 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15163 || (MATRIX_ROW_START_CHARPOS (row) == PT
15164 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15165 /* If we didn't find any candidate rows, or exited the
15166 loop before all the candidates were examined, signal
15167 to the caller that this method failed. */
15168 if (rc != CURSOR_MOVEMENT_SUCCESS
15169 && !(rv
15170 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15171 && !row->continued_p))
15172 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15173 else if (rv)
15174 rc = CURSOR_MOVEMENT_SUCCESS;
15176 else
15180 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15182 rc = CURSOR_MOVEMENT_SUCCESS;
15183 break;
15185 ++row;
15187 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15188 && MATRIX_ROW_START_CHARPOS (row) == PT
15189 && cursor_row_p (row));
15194 return rc;
15197 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15198 static
15199 #endif
15200 void
15201 set_vertical_scroll_bar (struct window *w)
15203 ptrdiff_t start, end, whole;
15205 /* Calculate the start and end positions for the current window.
15206 At some point, it would be nice to choose between scrollbars
15207 which reflect the whole buffer size, with special markers
15208 indicating narrowing, and scrollbars which reflect only the
15209 visible region.
15211 Note that mini-buffers sometimes aren't displaying any text. */
15212 if (!MINI_WINDOW_P (w)
15213 || (w == XWINDOW (minibuf_window)
15214 && NILP (echo_area_buffer[0])))
15216 struct buffer *buf = XBUFFER (w->contents);
15217 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15218 start = marker_position (w->start) - BUF_BEGV (buf);
15219 /* I don't think this is guaranteed to be right. For the
15220 moment, we'll pretend it is. */
15221 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15223 if (end < start)
15224 end = start;
15225 if (whole < (end - start))
15226 whole = end - start;
15228 else
15229 start = end = whole = 0;
15231 /* Indicate what this scroll bar ought to be displaying now. */
15232 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15233 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15234 (w, end - start, whole, start);
15238 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15239 selected_window is redisplayed.
15241 We can return without actually redisplaying the window if
15242 fonts_changed_p. In that case, redisplay_internal will
15243 retry. */
15245 static void
15246 redisplay_window (Lisp_Object window, int just_this_one_p)
15248 struct window *w = XWINDOW (window);
15249 struct frame *f = XFRAME (w->frame);
15250 struct buffer *buffer = XBUFFER (w->contents);
15251 struct buffer *old = current_buffer;
15252 struct text_pos lpoint, opoint, startp;
15253 int update_mode_line;
15254 int tem;
15255 struct it it;
15256 /* Record it now because it's overwritten. */
15257 int current_matrix_up_to_date_p = 0;
15258 int used_current_matrix_p = 0;
15259 /* This is less strict than current_matrix_up_to_date_p.
15260 It indicates that the buffer contents and narrowing are unchanged. */
15261 int buffer_unchanged_p = 0;
15262 int temp_scroll_step = 0;
15263 ptrdiff_t count = SPECPDL_INDEX ();
15264 int rc;
15265 int centering_position = -1;
15266 int last_line_misfit = 0;
15267 ptrdiff_t beg_unchanged, end_unchanged;
15269 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15270 opoint = lpoint;
15272 #ifdef GLYPH_DEBUG
15273 *w->desired_matrix->method = 0;
15274 #endif
15276 /* Make sure that both W's markers are valid. */
15277 eassert (XMARKER (w->start)->buffer == buffer);
15278 eassert (XMARKER (w->pointm)->buffer == buffer);
15280 restart:
15281 reconsider_clip_changes (w, buffer);
15283 /* Has the mode line to be updated? */
15284 update_mode_line = (w->update_mode_line
15285 || update_mode_lines
15286 || buffer->clip_changed
15287 || buffer->prevent_redisplay_optimizations_p);
15289 if (MINI_WINDOW_P (w))
15291 if (w == XWINDOW (echo_area_window)
15292 && !NILP (echo_area_buffer[0]))
15294 if (update_mode_line)
15295 /* We may have to update a tty frame's menu bar or a
15296 tool-bar. Example `M-x C-h C-h C-g'. */
15297 goto finish_menu_bars;
15298 else
15299 /* We've already displayed the echo area glyphs in this window. */
15300 goto finish_scroll_bars;
15302 else if ((w != XWINDOW (minibuf_window)
15303 || minibuf_level == 0)
15304 /* When buffer is nonempty, redisplay window normally. */
15305 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15306 /* Quail displays non-mini buffers in minibuffer window.
15307 In that case, redisplay the window normally. */
15308 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15310 /* W is a mini-buffer window, but it's not active, so clear
15311 it. */
15312 int yb = window_text_bottom_y (w);
15313 struct glyph_row *row;
15314 int y;
15316 for (y = 0, row = w->desired_matrix->rows;
15317 y < yb;
15318 y += row->height, ++row)
15319 blank_row (w, row, y);
15320 goto finish_scroll_bars;
15323 clear_glyph_matrix (w->desired_matrix);
15326 /* Otherwise set up data on this window; select its buffer and point
15327 value. */
15328 /* Really select the buffer, for the sake of buffer-local
15329 variables. */
15330 set_buffer_internal_1 (XBUFFER (w->contents));
15332 current_matrix_up_to_date_p
15333 = (w->window_end_valid
15334 && !current_buffer->clip_changed
15335 && !current_buffer->prevent_redisplay_optimizations_p
15336 && !window_outdated (w));
15338 /* Run the window-bottom-change-functions
15339 if it is possible that the text on the screen has changed
15340 (either due to modification of the text, or any other reason). */
15341 if (!current_matrix_up_to_date_p
15342 && !NILP (Vwindow_text_change_functions))
15344 safe_run_hooks (Qwindow_text_change_functions);
15345 goto restart;
15348 beg_unchanged = BEG_UNCHANGED;
15349 end_unchanged = END_UNCHANGED;
15351 SET_TEXT_POS (opoint, PT, PT_BYTE);
15353 specbind (Qinhibit_point_motion_hooks, Qt);
15355 buffer_unchanged_p
15356 = (w->window_end_valid
15357 && !current_buffer->clip_changed
15358 && !window_outdated (w));
15360 /* When windows_or_buffers_changed is non-zero, we can't rely on
15361 the window end being valid, so set it to nil there. */
15362 if (windows_or_buffers_changed)
15364 /* If window starts on a continuation line, maybe adjust the
15365 window start in case the window's width changed. */
15366 if (XMARKER (w->start)->buffer == current_buffer)
15367 compute_window_start_on_continuation_line (w);
15369 w->window_end_valid = 0;
15372 /* Some sanity checks. */
15373 CHECK_WINDOW_END (w);
15374 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15375 emacs_abort ();
15376 if (BYTEPOS (opoint) < CHARPOS (opoint))
15377 emacs_abort ();
15379 if (mode_line_update_needed (w))
15380 update_mode_line = 1;
15382 /* Point refers normally to the selected window. For any other
15383 window, set up appropriate value. */
15384 if (!EQ (window, selected_window))
15386 ptrdiff_t new_pt = marker_position (w->pointm);
15387 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15388 if (new_pt < BEGV)
15390 new_pt = BEGV;
15391 new_pt_byte = BEGV_BYTE;
15392 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15394 else if (new_pt > (ZV - 1))
15396 new_pt = ZV;
15397 new_pt_byte = ZV_BYTE;
15398 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15401 /* We don't use SET_PT so that the point-motion hooks don't run. */
15402 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15405 /* If any of the character widths specified in the display table
15406 have changed, invalidate the width run cache. It's true that
15407 this may be a bit late to catch such changes, but the rest of
15408 redisplay goes (non-fatally) haywire when the display table is
15409 changed, so why should we worry about doing any better? */
15410 if (current_buffer->width_run_cache)
15412 struct Lisp_Char_Table *disptab = buffer_display_table ();
15414 if (! disptab_matches_widthtab
15415 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15417 invalidate_region_cache (current_buffer,
15418 current_buffer->width_run_cache,
15419 BEG, Z);
15420 recompute_width_table (current_buffer, disptab);
15424 /* If window-start is screwed up, choose a new one. */
15425 if (XMARKER (w->start)->buffer != current_buffer)
15426 goto recenter;
15428 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15430 /* If someone specified a new starting point but did not insist,
15431 check whether it can be used. */
15432 if (w->optional_new_start
15433 && CHARPOS (startp) >= BEGV
15434 && CHARPOS (startp) <= ZV)
15436 w->optional_new_start = 0;
15437 start_display (&it, w, startp);
15438 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15439 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15440 if (IT_CHARPOS (it) == PT)
15441 w->force_start = 1;
15442 /* IT may overshoot PT if text at PT is invisible. */
15443 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15444 w->force_start = 1;
15447 force_start:
15449 /* Handle case where place to start displaying has been specified,
15450 unless the specified location is outside the accessible range. */
15451 if (w->force_start || w->frozen_window_start_p)
15453 /* We set this later on if we have to adjust point. */
15454 int new_vpos = -1;
15456 w->force_start = 0;
15457 w->vscroll = 0;
15458 w->window_end_valid = 0;
15460 /* Forget any recorded base line for line number display. */
15461 if (!buffer_unchanged_p)
15462 w->base_line_number = 0;
15464 /* Redisplay the mode line. Select the buffer properly for that.
15465 Also, run the hook window-scroll-functions
15466 because we have scrolled. */
15467 /* Note, we do this after clearing force_start because
15468 if there's an error, it is better to forget about force_start
15469 than to get into an infinite loop calling the hook functions
15470 and having them get more errors. */
15471 if (!update_mode_line
15472 || ! NILP (Vwindow_scroll_functions))
15474 update_mode_line = 1;
15475 w->update_mode_line = 1;
15476 startp = run_window_scroll_functions (window, startp);
15479 w->last_modified = 0;
15480 w->last_overlay_modified = 0;
15481 if (CHARPOS (startp) < BEGV)
15482 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15483 else if (CHARPOS (startp) > ZV)
15484 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15486 /* Redisplay, then check if cursor has been set during the
15487 redisplay. Give up if new fonts were loaded. */
15488 /* We used to issue a CHECK_MARGINS argument to try_window here,
15489 but this causes scrolling to fail when point begins inside
15490 the scroll margin (bug#148) -- cyd */
15491 if (!try_window (window, startp, 0))
15493 w->force_start = 1;
15494 clear_glyph_matrix (w->desired_matrix);
15495 goto need_larger_matrices;
15498 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15500 /* If point does not appear, try to move point so it does
15501 appear. The desired matrix has been built above, so we
15502 can use it here. */
15503 new_vpos = window_box_height (w) / 2;
15506 if (!cursor_row_fully_visible_p (w, 0, 0))
15508 /* Point does appear, but on a line partly visible at end of window.
15509 Move it back to a fully-visible line. */
15510 new_vpos = window_box_height (w);
15512 else if (w->cursor.vpos >=0)
15514 /* Some people insist on not letting point enter the scroll
15515 margin, even though this part handles windows that didn't
15516 scroll at all. */
15517 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15518 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15519 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15521 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15522 below, which finds the row to move point to, advances by
15523 the Y coordinate of the _next_ row, see the definition of
15524 MATRIX_ROW_BOTTOM_Y. */
15525 if (w->cursor.vpos < margin + header_line)
15526 new_vpos
15527 = pixel_margin + (header_line
15528 ? CURRENT_HEADER_LINE_HEIGHT (w)
15529 : 0) + FRAME_LINE_HEIGHT (f);
15530 else
15532 int window_height = window_box_height (w);
15534 if (header_line)
15535 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15536 if (w->cursor.y >= window_height - pixel_margin)
15537 new_vpos = window_height - pixel_margin;
15541 /* If we need to move point for either of the above reasons,
15542 now actually do it. */
15543 if (new_vpos >= 0)
15545 struct glyph_row *row;
15547 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15548 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15549 ++row;
15551 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15552 MATRIX_ROW_START_BYTEPOS (row));
15554 if (w != XWINDOW (selected_window))
15555 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15556 else if (current_buffer == old)
15557 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15559 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15561 /* If we are highlighting the region, then we just changed
15562 the region, so redisplay to show it. */
15563 if (markpos_of_region () >= 0)
15565 clear_glyph_matrix (w->desired_matrix);
15566 if (!try_window (window, startp, 0))
15567 goto need_larger_matrices;
15571 #ifdef GLYPH_DEBUG
15572 debug_method_add (w, "forced window start");
15573 #endif
15574 goto done;
15577 /* Handle case where text has not changed, only point, and it has
15578 not moved off the frame, and we are not retrying after hscroll.
15579 (current_matrix_up_to_date_p is nonzero when retrying.) */
15580 if (current_matrix_up_to_date_p
15581 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15582 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15584 switch (rc)
15586 case CURSOR_MOVEMENT_SUCCESS:
15587 used_current_matrix_p = 1;
15588 goto done;
15590 case CURSOR_MOVEMENT_MUST_SCROLL:
15591 goto try_to_scroll;
15593 default:
15594 emacs_abort ();
15597 /* If current starting point was originally the beginning of a line
15598 but no longer is, find a new starting point. */
15599 else if (w->start_at_line_beg
15600 && !(CHARPOS (startp) <= BEGV
15601 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15603 #ifdef GLYPH_DEBUG
15604 debug_method_add (w, "recenter 1");
15605 #endif
15606 goto recenter;
15609 /* Try scrolling with try_window_id. Value is > 0 if update has
15610 been done, it is -1 if we know that the same window start will
15611 not work. It is 0 if unsuccessful for some other reason. */
15612 else if ((tem = try_window_id (w)) != 0)
15614 #ifdef GLYPH_DEBUG
15615 debug_method_add (w, "try_window_id %d", tem);
15616 #endif
15618 if (fonts_changed_p)
15619 goto need_larger_matrices;
15620 if (tem > 0)
15621 goto done;
15623 /* Otherwise try_window_id has returned -1 which means that we
15624 don't want the alternative below this comment to execute. */
15626 else if (CHARPOS (startp) >= BEGV
15627 && CHARPOS (startp) <= ZV
15628 && PT >= CHARPOS (startp)
15629 && (CHARPOS (startp) < ZV
15630 /* Avoid starting at end of buffer. */
15631 || CHARPOS (startp) == BEGV
15632 || !window_outdated (w)))
15634 int d1, d2, d3, d4, d5, d6;
15636 /* If first window line is a continuation line, and window start
15637 is inside the modified region, but the first change is before
15638 current window start, we must select a new window start.
15640 However, if this is the result of a down-mouse event (e.g. by
15641 extending the mouse-drag-overlay), we don't want to select a
15642 new window start, since that would change the position under
15643 the mouse, resulting in an unwanted mouse-movement rather
15644 than a simple mouse-click. */
15645 if (!w->start_at_line_beg
15646 && NILP (do_mouse_tracking)
15647 && CHARPOS (startp) > BEGV
15648 && CHARPOS (startp) > BEG + beg_unchanged
15649 && CHARPOS (startp) <= Z - end_unchanged
15650 /* Even if w->start_at_line_beg is nil, a new window may
15651 start at a line_beg, since that's how set_buffer_window
15652 sets it. So, we need to check the return value of
15653 compute_window_start_on_continuation_line. (See also
15654 bug#197). */
15655 && XMARKER (w->start)->buffer == current_buffer
15656 && compute_window_start_on_continuation_line (w)
15657 /* It doesn't make sense to force the window start like we
15658 do at label force_start if it is already known that point
15659 will not be visible in the resulting window, because
15660 doing so will move point from its correct position
15661 instead of scrolling the window to bring point into view.
15662 See bug#9324. */
15663 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15665 w->force_start = 1;
15666 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15667 goto force_start;
15670 #ifdef GLYPH_DEBUG
15671 debug_method_add (w, "same window start");
15672 #endif
15674 /* Try to redisplay starting at same place as before.
15675 If point has not moved off frame, accept the results. */
15676 if (!current_matrix_up_to_date_p
15677 /* Don't use try_window_reusing_current_matrix in this case
15678 because a window scroll function can have changed the
15679 buffer. */
15680 || !NILP (Vwindow_scroll_functions)
15681 || MINI_WINDOW_P (w)
15682 || !(used_current_matrix_p
15683 = try_window_reusing_current_matrix (w)))
15685 IF_DEBUG (debug_method_add (w, "1"));
15686 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15687 /* -1 means we need to scroll.
15688 0 means we need new matrices, but fonts_changed_p
15689 is set in that case, so we will detect it below. */
15690 goto try_to_scroll;
15693 if (fonts_changed_p)
15694 goto need_larger_matrices;
15696 if (w->cursor.vpos >= 0)
15698 if (!just_this_one_p
15699 || current_buffer->clip_changed
15700 || BEG_UNCHANGED < CHARPOS (startp))
15701 /* Forget any recorded base line for line number display. */
15702 w->base_line_number = 0;
15704 if (!cursor_row_fully_visible_p (w, 1, 0))
15706 clear_glyph_matrix (w->desired_matrix);
15707 last_line_misfit = 1;
15709 /* Drop through and scroll. */
15710 else
15711 goto done;
15713 else
15714 clear_glyph_matrix (w->desired_matrix);
15717 try_to_scroll:
15719 w->last_modified = 0;
15720 w->last_overlay_modified = 0;
15722 /* Redisplay the mode line. Select the buffer properly for that. */
15723 if (!update_mode_line)
15725 update_mode_line = 1;
15726 w->update_mode_line = 1;
15729 /* Try to scroll by specified few lines. */
15730 if ((scroll_conservatively
15731 || emacs_scroll_step
15732 || temp_scroll_step
15733 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15734 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15735 && CHARPOS (startp) >= BEGV
15736 && CHARPOS (startp) <= ZV)
15738 /* The function returns -1 if new fonts were loaded, 1 if
15739 successful, 0 if not successful. */
15740 int ss = try_scrolling (window, just_this_one_p,
15741 scroll_conservatively,
15742 emacs_scroll_step,
15743 temp_scroll_step, last_line_misfit);
15744 switch (ss)
15746 case SCROLLING_SUCCESS:
15747 goto done;
15749 case SCROLLING_NEED_LARGER_MATRICES:
15750 goto need_larger_matrices;
15752 case SCROLLING_FAILED:
15753 break;
15755 default:
15756 emacs_abort ();
15760 /* Finally, just choose a place to start which positions point
15761 according to user preferences. */
15763 recenter:
15765 #ifdef GLYPH_DEBUG
15766 debug_method_add (w, "recenter");
15767 #endif
15769 /* Forget any previously recorded base line for line number display. */
15770 if (!buffer_unchanged_p)
15771 w->base_line_number = 0;
15773 /* Determine the window start relative to point. */
15774 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15775 it.current_y = it.last_visible_y;
15776 if (centering_position < 0)
15778 int margin =
15779 scroll_margin > 0
15780 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15781 : 0;
15782 ptrdiff_t margin_pos = CHARPOS (startp);
15783 Lisp_Object aggressive;
15784 int scrolling_up;
15786 /* If there is a scroll margin at the top of the window, find
15787 its character position. */
15788 if (margin
15789 /* Cannot call start_display if startp is not in the
15790 accessible region of the buffer. This can happen when we
15791 have just switched to a different buffer and/or changed
15792 its restriction. In that case, startp is initialized to
15793 the character position 1 (BEGV) because we did not yet
15794 have chance to display the buffer even once. */
15795 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15797 struct it it1;
15798 void *it1data = NULL;
15800 SAVE_IT (it1, it, it1data);
15801 start_display (&it1, w, startp);
15802 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15803 margin_pos = IT_CHARPOS (it1);
15804 RESTORE_IT (&it, &it, it1data);
15806 scrolling_up = PT > margin_pos;
15807 aggressive =
15808 scrolling_up
15809 ? BVAR (current_buffer, scroll_up_aggressively)
15810 : BVAR (current_buffer, scroll_down_aggressively);
15812 if (!MINI_WINDOW_P (w)
15813 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15815 int pt_offset = 0;
15817 /* Setting scroll-conservatively overrides
15818 scroll-*-aggressively. */
15819 if (!scroll_conservatively && NUMBERP (aggressive))
15821 double float_amount = XFLOATINT (aggressive);
15823 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15824 if (pt_offset == 0 && float_amount > 0)
15825 pt_offset = 1;
15826 if (pt_offset && margin > 0)
15827 margin -= 1;
15829 /* Compute how much to move the window start backward from
15830 point so that point will be displayed where the user
15831 wants it. */
15832 if (scrolling_up)
15834 centering_position = it.last_visible_y;
15835 if (pt_offset)
15836 centering_position -= pt_offset;
15837 centering_position -=
15838 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15839 + WINDOW_HEADER_LINE_HEIGHT (w);
15840 /* Don't let point enter the scroll margin near top of
15841 the window. */
15842 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15843 centering_position = margin * FRAME_LINE_HEIGHT (f);
15845 else
15846 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15848 else
15849 /* Set the window start half the height of the window backward
15850 from point. */
15851 centering_position = window_box_height (w) / 2;
15853 move_it_vertically_backward (&it, centering_position);
15855 eassert (IT_CHARPOS (it) >= BEGV);
15857 /* The function move_it_vertically_backward may move over more
15858 than the specified y-distance. If it->w is small, e.g. a
15859 mini-buffer window, we may end up in front of the window's
15860 display area. Start displaying at the start of the line
15861 containing PT in this case. */
15862 if (it.current_y <= 0)
15864 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15865 move_it_vertically_backward (&it, 0);
15866 it.current_y = 0;
15869 it.current_x = it.hpos = 0;
15871 /* Set the window start position here explicitly, to avoid an
15872 infinite loop in case the functions in window-scroll-functions
15873 get errors. */
15874 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15876 /* Run scroll hooks. */
15877 startp = run_window_scroll_functions (window, it.current.pos);
15879 /* Redisplay the window. */
15880 if (!current_matrix_up_to_date_p
15881 || windows_or_buffers_changed
15882 || cursor_type_changed
15883 /* Don't use try_window_reusing_current_matrix in this case
15884 because it can have changed the buffer. */
15885 || !NILP (Vwindow_scroll_functions)
15886 || !just_this_one_p
15887 || MINI_WINDOW_P (w)
15888 || !(used_current_matrix_p
15889 = try_window_reusing_current_matrix (w)))
15890 try_window (window, startp, 0);
15892 /* If new fonts have been loaded (due to fontsets), give up. We
15893 have to start a new redisplay since we need to re-adjust glyph
15894 matrices. */
15895 if (fonts_changed_p)
15896 goto need_larger_matrices;
15898 /* If cursor did not appear assume that the middle of the window is
15899 in the first line of the window. Do it again with the next line.
15900 (Imagine a window of height 100, displaying two lines of height
15901 60. Moving back 50 from it->last_visible_y will end in the first
15902 line.) */
15903 if (w->cursor.vpos < 0)
15905 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15907 clear_glyph_matrix (w->desired_matrix);
15908 move_it_by_lines (&it, 1);
15909 try_window (window, it.current.pos, 0);
15911 else if (PT < IT_CHARPOS (it))
15913 clear_glyph_matrix (w->desired_matrix);
15914 move_it_by_lines (&it, -1);
15915 try_window (window, it.current.pos, 0);
15917 else
15919 /* Not much we can do about it. */
15923 /* Consider the following case: Window starts at BEGV, there is
15924 invisible, intangible text at BEGV, so that display starts at
15925 some point START > BEGV. It can happen that we are called with
15926 PT somewhere between BEGV and START. Try to handle that case. */
15927 if (w->cursor.vpos < 0)
15929 struct glyph_row *row = w->current_matrix->rows;
15930 if (row->mode_line_p)
15931 ++row;
15932 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15935 if (!cursor_row_fully_visible_p (w, 0, 0))
15937 /* If vscroll is enabled, disable it and try again. */
15938 if (w->vscroll)
15940 w->vscroll = 0;
15941 clear_glyph_matrix (w->desired_matrix);
15942 goto recenter;
15945 /* Users who set scroll-conservatively to a large number want
15946 point just above/below the scroll margin. If we ended up
15947 with point's row partially visible, move the window start to
15948 make that row fully visible and out of the margin. */
15949 if (scroll_conservatively > SCROLL_LIMIT)
15951 int margin =
15952 scroll_margin > 0
15953 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15954 : 0;
15955 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15957 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15958 clear_glyph_matrix (w->desired_matrix);
15959 if (1 == try_window (window, it.current.pos,
15960 TRY_WINDOW_CHECK_MARGINS))
15961 goto done;
15964 /* If centering point failed to make the whole line visible,
15965 put point at the top instead. That has to make the whole line
15966 visible, if it can be done. */
15967 if (centering_position == 0)
15968 goto done;
15970 clear_glyph_matrix (w->desired_matrix);
15971 centering_position = 0;
15972 goto recenter;
15975 done:
15977 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15978 w->start_at_line_beg = (CHARPOS (startp) == BEGV
15979 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
15981 /* Display the mode line, if we must. */
15982 if ((update_mode_line
15983 /* If window not full width, must redo its mode line
15984 if (a) the window to its side is being redone and
15985 (b) we do a frame-based redisplay. This is a consequence
15986 of how inverted lines are drawn in frame-based redisplay. */
15987 || (!just_this_one_p
15988 && !FRAME_WINDOW_P (f)
15989 && !WINDOW_FULL_WIDTH_P (w))
15990 /* Line number to display. */
15991 || w->base_line_pos > 0
15992 /* Column number is displayed and different from the one displayed. */
15993 || (w->column_number_displayed != -1
15994 && (w->column_number_displayed != current_column ())))
15995 /* This means that the window has a mode line. */
15996 && (WINDOW_WANTS_MODELINE_P (w)
15997 || WINDOW_WANTS_HEADER_LINE_P (w)))
15999 display_mode_lines (w);
16001 /* If mode line height has changed, arrange for a thorough
16002 immediate redisplay using the correct mode line height. */
16003 if (WINDOW_WANTS_MODELINE_P (w)
16004 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16006 fonts_changed_p = 1;
16007 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16008 = DESIRED_MODE_LINE_HEIGHT (w);
16011 /* If header line height has changed, arrange for a thorough
16012 immediate redisplay using the correct header line height. */
16013 if (WINDOW_WANTS_HEADER_LINE_P (w)
16014 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16016 fonts_changed_p = 1;
16017 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16018 = DESIRED_HEADER_LINE_HEIGHT (w);
16021 if (fonts_changed_p)
16022 goto need_larger_matrices;
16025 if (!line_number_displayed && w->base_line_pos != -1)
16027 w->base_line_pos = 0;
16028 w->base_line_number = 0;
16031 finish_menu_bars:
16033 /* When we reach a frame's selected window, redo the frame's menu bar. */
16034 if (update_mode_line
16035 && EQ (FRAME_SELECTED_WINDOW (f), window))
16037 int redisplay_menu_p = 0;
16039 if (FRAME_WINDOW_P (f))
16041 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16042 || defined (HAVE_NS) || defined (USE_GTK)
16043 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16044 #else
16045 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16046 #endif
16048 else
16049 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16051 if (redisplay_menu_p)
16052 display_menu_bar (w);
16054 #ifdef HAVE_WINDOW_SYSTEM
16055 if (FRAME_WINDOW_P (f))
16057 #if defined (USE_GTK) || defined (HAVE_NS)
16058 if (FRAME_EXTERNAL_TOOL_BAR (f))
16059 redisplay_tool_bar (f);
16060 #else
16061 if (WINDOWP (f->tool_bar_window)
16062 && (FRAME_TOOL_BAR_LINES (f) > 0
16063 || !NILP (Vauto_resize_tool_bars))
16064 && redisplay_tool_bar (f))
16065 ignore_mouse_drag_p = 1;
16066 #endif
16068 #endif
16071 #ifdef HAVE_WINDOW_SYSTEM
16072 if (FRAME_WINDOW_P (f)
16073 && update_window_fringes (w, (just_this_one_p
16074 || (!used_current_matrix_p && !overlay_arrow_seen)
16075 || w->pseudo_window_p)))
16077 update_begin (f);
16078 block_input ();
16079 if (draw_window_fringes (w, 1))
16080 x_draw_vertical_border (w);
16081 unblock_input ();
16082 update_end (f);
16084 #endif /* HAVE_WINDOW_SYSTEM */
16086 /* We go to this label, with fonts_changed_p set,
16087 if it is necessary to try again using larger glyph matrices.
16088 We have to redeem the scroll bar even in this case,
16089 because the loop in redisplay_internal expects that. */
16090 need_larger_matrices:
16092 finish_scroll_bars:
16094 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16096 /* Set the thumb's position and size. */
16097 set_vertical_scroll_bar (w);
16099 /* Note that we actually used the scroll bar attached to this
16100 window, so it shouldn't be deleted at the end of redisplay. */
16101 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16102 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16105 /* Restore current_buffer and value of point in it. The window
16106 update may have changed the buffer, so first make sure `opoint'
16107 is still valid (Bug#6177). */
16108 if (CHARPOS (opoint) < BEGV)
16109 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16110 else if (CHARPOS (opoint) > ZV)
16111 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16112 else
16113 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16115 set_buffer_internal_1 (old);
16116 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16117 shorter. This can be caused by log truncation in *Messages*. */
16118 if (CHARPOS (lpoint) <= ZV)
16119 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16121 unbind_to (count, Qnil);
16125 /* Build the complete desired matrix of WINDOW with a window start
16126 buffer position POS.
16128 Value is 1 if successful. It is zero if fonts were loaded during
16129 redisplay which makes re-adjusting glyph matrices necessary, and -1
16130 if point would appear in the scroll margins.
16131 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16132 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16133 set in FLAGS.) */
16136 try_window (Lisp_Object window, struct text_pos pos, int flags)
16138 struct window *w = XWINDOW (window);
16139 struct it it;
16140 struct glyph_row *last_text_row = NULL;
16141 struct frame *f = XFRAME (w->frame);
16143 /* Make POS the new window start. */
16144 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16146 /* Mark cursor position as unknown. No overlay arrow seen. */
16147 w->cursor.vpos = -1;
16148 overlay_arrow_seen = 0;
16150 /* Initialize iterator and info to start at POS. */
16151 start_display (&it, w, pos);
16153 /* Display all lines of W. */
16154 while (it.current_y < it.last_visible_y)
16156 if (display_line (&it))
16157 last_text_row = it.glyph_row - 1;
16158 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16159 return 0;
16162 /* Don't let the cursor end in the scroll margins. */
16163 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16164 && !MINI_WINDOW_P (w))
16166 int this_scroll_margin;
16168 if (scroll_margin > 0)
16170 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16171 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16173 else
16174 this_scroll_margin = 0;
16176 if ((w->cursor.y >= 0 /* not vscrolled */
16177 && w->cursor.y < this_scroll_margin
16178 && CHARPOS (pos) > BEGV
16179 && IT_CHARPOS (it) < ZV)
16180 /* rms: considering make_cursor_line_fully_visible_p here
16181 seems to give wrong results. We don't want to recenter
16182 when the last line is partly visible, we want to allow
16183 that case to be handled in the usual way. */
16184 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16186 w->cursor.vpos = -1;
16187 clear_glyph_matrix (w->desired_matrix);
16188 return -1;
16192 /* If bottom moved off end of frame, change mode line percentage. */
16193 if (XFASTINT (w->window_end_pos) <= 0
16194 && Z != IT_CHARPOS (it))
16195 w->update_mode_line = 1;
16197 /* Set window_end_pos to the offset of the last character displayed
16198 on the window from the end of current_buffer. Set
16199 window_end_vpos to its row number. */
16200 if (last_text_row)
16202 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16203 w->window_end_bytepos
16204 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16205 wset_window_end_pos
16206 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16207 wset_window_end_vpos
16208 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16209 eassert
16210 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16211 XFASTINT (w->window_end_vpos))));
16213 else
16215 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16216 wset_window_end_pos (w, make_number (Z - ZV));
16217 wset_window_end_vpos (w, make_number (0));
16220 /* But that is not valid info until redisplay finishes. */
16221 w->window_end_valid = 0;
16222 return 1;
16227 /************************************************************************
16228 Window redisplay reusing current matrix when buffer has not changed
16229 ************************************************************************/
16231 /* Try redisplay of window W showing an unchanged buffer with a
16232 different window start than the last time it was displayed by
16233 reusing its current matrix. Value is non-zero if successful.
16234 W->start is the new window start. */
16236 static int
16237 try_window_reusing_current_matrix (struct window *w)
16239 struct frame *f = XFRAME (w->frame);
16240 struct glyph_row *bottom_row;
16241 struct it it;
16242 struct run run;
16243 struct text_pos start, new_start;
16244 int nrows_scrolled, i;
16245 struct glyph_row *last_text_row;
16246 struct glyph_row *last_reused_text_row;
16247 struct glyph_row *start_row;
16248 int start_vpos, min_y, max_y;
16250 #ifdef GLYPH_DEBUG
16251 if (inhibit_try_window_reusing)
16252 return 0;
16253 #endif
16255 if (/* This function doesn't handle terminal frames. */
16256 !FRAME_WINDOW_P (f)
16257 /* Don't try to reuse the display if windows have been split
16258 or such. */
16259 || windows_or_buffers_changed
16260 || cursor_type_changed)
16261 return 0;
16263 /* Can't do this if region may have changed. */
16264 if (markpos_of_region () >= 0
16265 || w->region_showing
16266 || !NILP (Vshow_trailing_whitespace))
16267 return 0;
16269 /* If top-line visibility has changed, give up. */
16270 if (WINDOW_WANTS_HEADER_LINE_P (w)
16271 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16272 return 0;
16274 /* Give up if old or new display is scrolled vertically. We could
16275 make this function handle this, but right now it doesn't. */
16276 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16277 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16278 return 0;
16280 /* The variable new_start now holds the new window start. The old
16281 start `start' can be determined from the current matrix. */
16282 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16283 start = start_row->minpos;
16284 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16286 /* Clear the desired matrix for the display below. */
16287 clear_glyph_matrix (w->desired_matrix);
16289 if (CHARPOS (new_start) <= CHARPOS (start))
16291 /* Don't use this method if the display starts with an ellipsis
16292 displayed for invisible text. It's not easy to handle that case
16293 below, and it's certainly not worth the effort since this is
16294 not a frequent case. */
16295 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16296 return 0;
16298 IF_DEBUG (debug_method_add (w, "twu1"));
16300 /* Display up to a row that can be reused. The variable
16301 last_text_row is set to the last row displayed that displays
16302 text. Note that it.vpos == 0 if or if not there is a
16303 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16304 start_display (&it, w, new_start);
16305 w->cursor.vpos = -1;
16306 last_text_row = last_reused_text_row = NULL;
16308 while (it.current_y < it.last_visible_y
16309 && !fonts_changed_p)
16311 /* If we have reached into the characters in the START row,
16312 that means the line boundaries have changed. So we
16313 can't start copying with the row START. Maybe it will
16314 work to start copying with the following row. */
16315 while (IT_CHARPOS (it) > CHARPOS (start))
16317 /* Advance to the next row as the "start". */
16318 start_row++;
16319 start = start_row->minpos;
16320 /* If there are no more rows to try, or just one, give up. */
16321 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16322 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16323 || CHARPOS (start) == ZV)
16325 clear_glyph_matrix (w->desired_matrix);
16326 return 0;
16329 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16331 /* If we have reached alignment, we can copy the rest of the
16332 rows. */
16333 if (IT_CHARPOS (it) == CHARPOS (start)
16334 /* Don't accept "alignment" inside a display vector,
16335 since start_row could have started in the middle of
16336 that same display vector (thus their character
16337 positions match), and we have no way of telling if
16338 that is the case. */
16339 && it.current.dpvec_index < 0)
16340 break;
16342 if (display_line (&it))
16343 last_text_row = it.glyph_row - 1;
16347 /* A value of current_y < last_visible_y means that we stopped
16348 at the previous window start, which in turn means that we
16349 have at least one reusable row. */
16350 if (it.current_y < it.last_visible_y)
16352 struct glyph_row *row;
16354 /* IT.vpos always starts from 0; it counts text lines. */
16355 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16357 /* Find PT if not already found in the lines displayed. */
16358 if (w->cursor.vpos < 0)
16360 int dy = it.current_y - start_row->y;
16362 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16363 row = row_containing_pos (w, PT, row, NULL, dy);
16364 if (row)
16365 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16366 dy, nrows_scrolled);
16367 else
16369 clear_glyph_matrix (w->desired_matrix);
16370 return 0;
16374 /* Scroll the display. Do it before the current matrix is
16375 changed. The problem here is that update has not yet
16376 run, i.e. part of the current matrix is not up to date.
16377 scroll_run_hook will clear the cursor, and use the
16378 current matrix to get the height of the row the cursor is
16379 in. */
16380 run.current_y = start_row->y;
16381 run.desired_y = it.current_y;
16382 run.height = it.last_visible_y - it.current_y;
16384 if (run.height > 0 && run.current_y != run.desired_y)
16386 update_begin (f);
16387 FRAME_RIF (f)->update_window_begin_hook (w);
16388 FRAME_RIF (f)->clear_window_mouse_face (w);
16389 FRAME_RIF (f)->scroll_run_hook (w, &run);
16390 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16391 update_end (f);
16394 /* Shift current matrix down by nrows_scrolled lines. */
16395 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16396 rotate_matrix (w->current_matrix,
16397 start_vpos,
16398 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16399 nrows_scrolled);
16401 /* Disable lines that must be updated. */
16402 for (i = 0; i < nrows_scrolled; ++i)
16403 (start_row + i)->enabled_p = 0;
16405 /* Re-compute Y positions. */
16406 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16407 max_y = it.last_visible_y;
16408 for (row = start_row + nrows_scrolled;
16409 row < bottom_row;
16410 ++row)
16412 row->y = it.current_y;
16413 row->visible_height = row->height;
16415 if (row->y < min_y)
16416 row->visible_height -= min_y - row->y;
16417 if (row->y + row->height > max_y)
16418 row->visible_height -= row->y + row->height - max_y;
16419 if (row->fringe_bitmap_periodic_p)
16420 row->redraw_fringe_bitmaps_p = 1;
16422 it.current_y += row->height;
16424 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16425 last_reused_text_row = row;
16426 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16427 break;
16430 /* Disable lines in the current matrix which are now
16431 below the window. */
16432 for (++row; row < bottom_row; ++row)
16433 row->enabled_p = row->mode_line_p = 0;
16436 /* Update window_end_pos etc.; last_reused_text_row is the last
16437 reused row from the current matrix containing text, if any.
16438 The value of last_text_row is the last displayed line
16439 containing text. */
16440 if (last_reused_text_row)
16442 w->window_end_bytepos
16443 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16444 wset_window_end_pos
16445 (w, make_number (Z
16446 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16447 wset_window_end_vpos
16448 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16449 w->current_matrix)));
16451 else if (last_text_row)
16453 w->window_end_bytepos
16454 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16455 wset_window_end_pos
16456 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16457 wset_window_end_vpos
16458 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16459 w->desired_matrix)));
16461 else
16463 /* This window must be completely empty. */
16464 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16465 wset_window_end_pos (w, make_number (Z - ZV));
16466 wset_window_end_vpos (w, make_number (0));
16468 w->window_end_valid = 0;
16470 /* Update hint: don't try scrolling again in update_window. */
16471 w->desired_matrix->no_scrolling_p = 1;
16473 #ifdef GLYPH_DEBUG
16474 debug_method_add (w, "try_window_reusing_current_matrix 1");
16475 #endif
16476 return 1;
16478 else if (CHARPOS (new_start) > CHARPOS (start))
16480 struct glyph_row *pt_row, *row;
16481 struct glyph_row *first_reusable_row;
16482 struct glyph_row *first_row_to_display;
16483 int dy;
16484 int yb = window_text_bottom_y (w);
16486 /* Find the row starting at new_start, if there is one. Don't
16487 reuse a partially visible line at the end. */
16488 first_reusable_row = start_row;
16489 while (first_reusable_row->enabled_p
16490 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16491 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16492 < CHARPOS (new_start)))
16493 ++first_reusable_row;
16495 /* Give up if there is no row to reuse. */
16496 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16497 || !first_reusable_row->enabled_p
16498 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16499 != CHARPOS (new_start)))
16500 return 0;
16502 /* We can reuse fully visible rows beginning with
16503 first_reusable_row to the end of the window. Set
16504 first_row_to_display to the first row that cannot be reused.
16505 Set pt_row to the row containing point, if there is any. */
16506 pt_row = NULL;
16507 for (first_row_to_display = first_reusable_row;
16508 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16509 ++first_row_to_display)
16511 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16512 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16513 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16514 && first_row_to_display->ends_at_zv_p
16515 && pt_row == NULL)))
16516 pt_row = first_row_to_display;
16519 /* Start displaying at the start of first_row_to_display. */
16520 eassert (first_row_to_display->y < yb);
16521 init_to_row_start (&it, w, first_row_to_display);
16523 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16524 - start_vpos);
16525 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16526 - nrows_scrolled);
16527 it.current_y = (first_row_to_display->y - first_reusable_row->y
16528 + WINDOW_HEADER_LINE_HEIGHT (w));
16530 /* Display lines beginning with first_row_to_display in the
16531 desired matrix. Set last_text_row to the last row displayed
16532 that displays text. */
16533 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16534 if (pt_row == NULL)
16535 w->cursor.vpos = -1;
16536 last_text_row = NULL;
16537 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16538 if (display_line (&it))
16539 last_text_row = it.glyph_row - 1;
16541 /* If point is in a reused row, adjust y and vpos of the cursor
16542 position. */
16543 if (pt_row)
16545 w->cursor.vpos -= nrows_scrolled;
16546 w->cursor.y -= first_reusable_row->y - start_row->y;
16549 /* Give up if point isn't in a row displayed or reused. (This
16550 also handles the case where w->cursor.vpos < nrows_scrolled
16551 after the calls to display_line, which can happen with scroll
16552 margins. See bug#1295.) */
16553 if (w->cursor.vpos < 0)
16555 clear_glyph_matrix (w->desired_matrix);
16556 return 0;
16559 /* Scroll the display. */
16560 run.current_y = first_reusable_row->y;
16561 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16562 run.height = it.last_visible_y - run.current_y;
16563 dy = run.current_y - run.desired_y;
16565 if (run.height)
16567 update_begin (f);
16568 FRAME_RIF (f)->update_window_begin_hook (w);
16569 FRAME_RIF (f)->clear_window_mouse_face (w);
16570 FRAME_RIF (f)->scroll_run_hook (w, &run);
16571 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16572 update_end (f);
16575 /* Adjust Y positions of reused rows. */
16576 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16577 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16578 max_y = it.last_visible_y;
16579 for (row = first_reusable_row; row < first_row_to_display; ++row)
16581 row->y -= dy;
16582 row->visible_height = row->height;
16583 if (row->y < min_y)
16584 row->visible_height -= min_y - row->y;
16585 if (row->y + row->height > max_y)
16586 row->visible_height -= row->y + row->height - max_y;
16587 if (row->fringe_bitmap_periodic_p)
16588 row->redraw_fringe_bitmaps_p = 1;
16591 /* Scroll the current matrix. */
16592 eassert (nrows_scrolled > 0);
16593 rotate_matrix (w->current_matrix,
16594 start_vpos,
16595 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16596 -nrows_scrolled);
16598 /* Disable rows not reused. */
16599 for (row -= nrows_scrolled; row < bottom_row; ++row)
16600 row->enabled_p = 0;
16602 /* Point may have moved to a different line, so we cannot assume that
16603 the previous cursor position is valid; locate the correct row. */
16604 if (pt_row)
16606 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16607 row < bottom_row
16608 && PT >= MATRIX_ROW_END_CHARPOS (row)
16609 && !row->ends_at_zv_p;
16610 row++)
16612 w->cursor.vpos++;
16613 w->cursor.y = row->y;
16615 if (row < bottom_row)
16617 /* Can't simply scan the row for point with
16618 bidi-reordered glyph rows. Let set_cursor_from_row
16619 figure out where to put the cursor, and if it fails,
16620 give up. */
16621 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16623 if (!set_cursor_from_row (w, row, w->current_matrix,
16624 0, 0, 0, 0))
16626 clear_glyph_matrix (w->desired_matrix);
16627 return 0;
16630 else
16632 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16633 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16635 for (; glyph < end
16636 && (!BUFFERP (glyph->object)
16637 || glyph->charpos < PT);
16638 glyph++)
16640 w->cursor.hpos++;
16641 w->cursor.x += glyph->pixel_width;
16647 /* Adjust window end. A null value of last_text_row means that
16648 the window end is in reused rows which in turn means that
16649 only its vpos can have changed. */
16650 if (last_text_row)
16652 w->window_end_bytepos
16653 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16654 wset_window_end_pos
16655 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16656 wset_window_end_vpos
16657 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16658 w->desired_matrix)));
16660 else
16662 wset_window_end_vpos
16663 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16666 w->window_end_valid = 0;
16667 w->desired_matrix->no_scrolling_p = 1;
16669 #ifdef GLYPH_DEBUG
16670 debug_method_add (w, "try_window_reusing_current_matrix 2");
16671 #endif
16672 return 1;
16675 return 0;
16680 /************************************************************************
16681 Window redisplay reusing current matrix when buffer has changed
16682 ************************************************************************/
16684 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16685 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16686 ptrdiff_t *, ptrdiff_t *);
16687 static struct glyph_row *
16688 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16689 struct glyph_row *);
16692 /* Return the last row in MATRIX displaying text. If row START is
16693 non-null, start searching with that row. IT gives the dimensions
16694 of the display. Value is null if matrix is empty; otherwise it is
16695 a pointer to the row found. */
16697 static struct glyph_row *
16698 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16699 struct glyph_row *start)
16701 struct glyph_row *row, *row_found;
16703 /* Set row_found to the last row in IT->w's current matrix
16704 displaying text. The loop looks funny but think of partially
16705 visible lines. */
16706 row_found = NULL;
16707 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16708 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16710 eassert (row->enabled_p);
16711 row_found = row;
16712 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16713 break;
16714 ++row;
16717 return row_found;
16721 /* Return the last row in the current matrix of W that is not affected
16722 by changes at the start of current_buffer that occurred since W's
16723 current matrix was built. Value is null if no such row exists.
16725 BEG_UNCHANGED us the number of characters unchanged at the start of
16726 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16727 first changed character in current_buffer. Characters at positions <
16728 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16729 when the current matrix was built. */
16731 static struct glyph_row *
16732 find_last_unchanged_at_beg_row (struct window *w)
16734 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16735 struct glyph_row *row;
16736 struct glyph_row *row_found = NULL;
16737 int yb = window_text_bottom_y (w);
16739 /* Find the last row displaying unchanged text. */
16740 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16741 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16742 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16743 ++row)
16745 if (/* If row ends before first_changed_pos, it is unchanged,
16746 except in some case. */
16747 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16748 /* When row ends in ZV and we write at ZV it is not
16749 unchanged. */
16750 && !row->ends_at_zv_p
16751 /* When first_changed_pos is the end of a continued line,
16752 row is not unchanged because it may be no longer
16753 continued. */
16754 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16755 && (row->continued_p
16756 || row->exact_window_width_line_p))
16757 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16758 needs to be recomputed, so don't consider this row as
16759 unchanged. This happens when the last line was
16760 bidi-reordered and was killed immediately before this
16761 redisplay cycle. In that case, ROW->end stores the
16762 buffer position of the first visual-order character of
16763 the killed text, which is now beyond ZV. */
16764 && CHARPOS (row->end.pos) <= ZV)
16765 row_found = row;
16767 /* Stop if last visible row. */
16768 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16769 break;
16772 return row_found;
16776 /* Find the first glyph row in the current matrix of W that is not
16777 affected by changes at the end of current_buffer since the
16778 time W's current matrix was built.
16780 Return in *DELTA the number of chars by which buffer positions in
16781 unchanged text at the end of current_buffer must be adjusted.
16783 Return in *DELTA_BYTES the corresponding number of bytes.
16785 Value is null if no such row exists, i.e. all rows are affected by
16786 changes. */
16788 static struct glyph_row *
16789 find_first_unchanged_at_end_row (struct window *w,
16790 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16792 struct glyph_row *row;
16793 struct glyph_row *row_found = NULL;
16795 *delta = *delta_bytes = 0;
16797 /* Display must not have been paused, otherwise the current matrix
16798 is not up to date. */
16799 eassert (w->window_end_valid);
16801 /* A value of window_end_pos >= END_UNCHANGED means that the window
16802 end is in the range of changed text. If so, there is no
16803 unchanged row at the end of W's current matrix. */
16804 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16805 return NULL;
16807 /* Set row to the last row in W's current matrix displaying text. */
16808 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16810 /* If matrix is entirely empty, no unchanged row exists. */
16811 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16813 /* The value of row is the last glyph row in the matrix having a
16814 meaningful buffer position in it. The end position of row
16815 corresponds to window_end_pos. This allows us to translate
16816 buffer positions in the current matrix to current buffer
16817 positions for characters not in changed text. */
16818 ptrdiff_t Z_old =
16819 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16820 ptrdiff_t Z_BYTE_old =
16821 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16822 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16823 struct glyph_row *first_text_row
16824 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16826 *delta = Z - Z_old;
16827 *delta_bytes = Z_BYTE - Z_BYTE_old;
16829 /* Set last_unchanged_pos to the buffer position of the last
16830 character in the buffer that has not been changed. Z is the
16831 index + 1 of the last character in current_buffer, i.e. by
16832 subtracting END_UNCHANGED we get the index of the last
16833 unchanged character, and we have to add BEG to get its buffer
16834 position. */
16835 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16836 last_unchanged_pos_old = last_unchanged_pos - *delta;
16838 /* Search backward from ROW for a row displaying a line that
16839 starts at a minimum position >= last_unchanged_pos_old. */
16840 for (; row > first_text_row; --row)
16842 /* This used to abort, but it can happen.
16843 It is ok to just stop the search instead here. KFS. */
16844 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16845 break;
16847 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16848 row_found = row;
16852 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16854 return row_found;
16858 /* Make sure that glyph rows in the current matrix of window W
16859 reference the same glyph memory as corresponding rows in the
16860 frame's frame matrix. This function is called after scrolling W's
16861 current matrix on a terminal frame in try_window_id and
16862 try_window_reusing_current_matrix. */
16864 static void
16865 sync_frame_with_window_matrix_rows (struct window *w)
16867 struct frame *f = XFRAME (w->frame);
16868 struct glyph_row *window_row, *window_row_end, *frame_row;
16870 /* Preconditions: W must be a leaf window and full-width. Its frame
16871 must have a frame matrix. */
16872 eassert (BUFFERP (w->contents));
16873 eassert (WINDOW_FULL_WIDTH_P (w));
16874 eassert (!FRAME_WINDOW_P (f));
16876 /* If W is a full-width window, glyph pointers in W's current matrix
16877 have, by definition, to be the same as glyph pointers in the
16878 corresponding frame matrix. Note that frame matrices have no
16879 marginal areas (see build_frame_matrix). */
16880 window_row = w->current_matrix->rows;
16881 window_row_end = window_row + w->current_matrix->nrows;
16882 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16883 while (window_row < window_row_end)
16885 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16886 struct glyph *end = window_row->glyphs[LAST_AREA];
16888 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16889 frame_row->glyphs[TEXT_AREA] = start;
16890 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16891 frame_row->glyphs[LAST_AREA] = end;
16893 /* Disable frame rows whose corresponding window rows have
16894 been disabled in try_window_id. */
16895 if (!window_row->enabled_p)
16896 frame_row->enabled_p = 0;
16898 ++window_row, ++frame_row;
16903 /* Find the glyph row in window W containing CHARPOS. Consider all
16904 rows between START and END (not inclusive). END null means search
16905 all rows to the end of the display area of W. Value is the row
16906 containing CHARPOS or null. */
16908 struct glyph_row *
16909 row_containing_pos (struct window *w, ptrdiff_t charpos,
16910 struct glyph_row *start, struct glyph_row *end, int dy)
16912 struct glyph_row *row = start;
16913 struct glyph_row *best_row = NULL;
16914 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16915 int last_y;
16917 /* If we happen to start on a header-line, skip that. */
16918 if (row->mode_line_p)
16919 ++row;
16921 if ((end && row >= end) || !row->enabled_p)
16922 return NULL;
16924 last_y = window_text_bottom_y (w) - dy;
16926 while (1)
16928 /* Give up if we have gone too far. */
16929 if (end && row >= end)
16930 return NULL;
16931 /* This formerly returned if they were equal.
16932 I think that both quantities are of a "last plus one" type;
16933 if so, when they are equal, the row is within the screen. -- rms. */
16934 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16935 return NULL;
16937 /* If it is in this row, return this row. */
16938 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16939 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16940 /* The end position of a row equals the start
16941 position of the next row. If CHARPOS is there, we
16942 would rather consider it displayed in the next
16943 line, except when this line ends in ZV. */
16944 && !row_for_charpos_p (row, charpos)))
16945 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16947 struct glyph *g;
16949 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
16950 || (!best_row && !row->continued_p))
16951 return row;
16952 /* In bidi-reordered rows, there could be several rows whose
16953 edges surround CHARPOS, all of these rows belonging to
16954 the same continued line. We need to find the row which
16955 fits CHARPOS the best. */
16956 for (g = row->glyphs[TEXT_AREA];
16957 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16958 g++)
16960 if (!STRINGP (g->object))
16962 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16964 mindif = eabs (g->charpos - charpos);
16965 best_row = row;
16966 /* Exact match always wins. */
16967 if (mindif == 0)
16968 return best_row;
16973 else if (best_row && !row->continued_p)
16974 return best_row;
16975 ++row;
16980 /* Try to redisplay window W by reusing its existing display. W's
16981 current matrix must be up to date when this function is called,
16982 i.e. window_end_valid must be nonzero.
16984 Value is
16986 1 if display has been updated
16987 0 if otherwise unsuccessful
16988 -1 if redisplay with same window start is known not to succeed
16990 The following steps are performed:
16992 1. Find the last row in the current matrix of W that is not
16993 affected by changes at the start of current_buffer. If no such row
16994 is found, give up.
16996 2. Find the first row in W's current matrix that is not affected by
16997 changes at the end of current_buffer. Maybe there is no such row.
16999 3. Display lines beginning with the row + 1 found in step 1 to the
17000 row found in step 2 or, if step 2 didn't find a row, to the end of
17001 the window.
17003 4. If cursor is not known to appear on the window, give up.
17005 5. If display stopped at the row found in step 2, scroll the
17006 display and current matrix as needed.
17008 6. Maybe display some lines at the end of W, if we must. This can
17009 happen under various circumstances, like a partially visible line
17010 becoming fully visible, or because newly displayed lines are displayed
17011 in smaller font sizes.
17013 7. Update W's window end information. */
17015 static int
17016 try_window_id (struct window *w)
17018 struct frame *f = XFRAME (w->frame);
17019 struct glyph_matrix *current_matrix = w->current_matrix;
17020 struct glyph_matrix *desired_matrix = w->desired_matrix;
17021 struct glyph_row *last_unchanged_at_beg_row;
17022 struct glyph_row *first_unchanged_at_end_row;
17023 struct glyph_row *row;
17024 struct glyph_row *bottom_row;
17025 int bottom_vpos;
17026 struct it it;
17027 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17028 int dvpos, dy;
17029 struct text_pos start_pos;
17030 struct run run;
17031 int first_unchanged_at_end_vpos = 0;
17032 struct glyph_row *last_text_row, *last_text_row_at_end;
17033 struct text_pos start;
17034 ptrdiff_t first_changed_charpos, last_changed_charpos;
17036 #ifdef GLYPH_DEBUG
17037 if (inhibit_try_window_id)
17038 return 0;
17039 #endif
17041 /* This is handy for debugging. */
17042 #if 0
17043 #define GIVE_UP(X) \
17044 do { \
17045 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17046 return 0; \
17047 } while (0)
17048 #else
17049 #define GIVE_UP(X) return 0
17050 #endif
17052 SET_TEXT_POS_FROM_MARKER (start, w->start);
17054 /* Don't use this for mini-windows because these can show
17055 messages and mini-buffers, and we don't handle that here. */
17056 if (MINI_WINDOW_P (w))
17057 GIVE_UP (1);
17059 /* This flag is used to prevent redisplay optimizations. */
17060 if (windows_or_buffers_changed || cursor_type_changed)
17061 GIVE_UP (2);
17063 /* Verify that narrowing has not changed.
17064 Also verify that we were not told to prevent redisplay optimizations.
17065 It would be nice to further
17066 reduce the number of cases where this prevents try_window_id. */
17067 if (current_buffer->clip_changed
17068 || current_buffer->prevent_redisplay_optimizations_p)
17069 GIVE_UP (3);
17071 /* Window must either use window-based redisplay or be full width. */
17072 if (!FRAME_WINDOW_P (f)
17073 && (!FRAME_LINE_INS_DEL_OK (f)
17074 || !WINDOW_FULL_WIDTH_P (w)))
17075 GIVE_UP (4);
17077 /* Give up if point is known NOT to appear in W. */
17078 if (PT < CHARPOS (start))
17079 GIVE_UP (5);
17081 /* Another way to prevent redisplay optimizations. */
17082 if (w->last_modified == 0)
17083 GIVE_UP (6);
17085 /* Verify that window is not hscrolled. */
17086 if (w->hscroll != 0)
17087 GIVE_UP (7);
17089 /* Verify that display wasn't paused. */
17090 if (!w->window_end_valid)
17091 GIVE_UP (8);
17093 /* Can't use this if highlighting a region because a cursor movement
17094 will do more than just set the cursor. */
17095 if (markpos_of_region () >= 0)
17096 GIVE_UP (9);
17098 /* Likewise if highlighting trailing whitespace. */
17099 if (!NILP (Vshow_trailing_whitespace))
17100 GIVE_UP (11);
17102 /* Likewise if showing a region. */
17103 if (w->region_showing)
17104 GIVE_UP (10);
17106 /* Can't use this if overlay arrow position and/or string have
17107 changed. */
17108 if (overlay_arrows_changed_p ())
17109 GIVE_UP (12);
17111 /* When word-wrap is on, adding a space to the first word of a
17112 wrapped line can change the wrap position, altering the line
17113 above it. It might be worthwhile to handle this more
17114 intelligently, but for now just redisplay from scratch. */
17115 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17116 GIVE_UP (21);
17118 /* Under bidi reordering, adding or deleting a character in the
17119 beginning of a paragraph, before the first strong directional
17120 character, can change the base direction of the paragraph (unless
17121 the buffer specifies a fixed paragraph direction), which will
17122 require to redisplay the whole paragraph. It might be worthwhile
17123 to find the paragraph limits and widen the range of redisplayed
17124 lines to that, but for now just give up this optimization and
17125 redisplay from scratch. */
17126 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17127 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17128 GIVE_UP (22);
17130 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17131 only if buffer has really changed. The reason is that the gap is
17132 initially at Z for freshly visited files. The code below would
17133 set end_unchanged to 0 in that case. */
17134 if (MODIFF > SAVE_MODIFF
17135 /* This seems to happen sometimes after saving a buffer. */
17136 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17138 if (GPT - BEG < BEG_UNCHANGED)
17139 BEG_UNCHANGED = GPT - BEG;
17140 if (Z - GPT < END_UNCHANGED)
17141 END_UNCHANGED = Z - GPT;
17144 /* The position of the first and last character that has been changed. */
17145 first_changed_charpos = BEG + BEG_UNCHANGED;
17146 last_changed_charpos = Z - END_UNCHANGED;
17148 /* If window starts after a line end, and the last change is in
17149 front of that newline, then changes don't affect the display.
17150 This case happens with stealth-fontification. Note that although
17151 the display is unchanged, glyph positions in the matrix have to
17152 be adjusted, of course. */
17153 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17154 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17155 && ((last_changed_charpos < CHARPOS (start)
17156 && CHARPOS (start) == BEGV)
17157 || (last_changed_charpos < CHARPOS (start) - 1
17158 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17160 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17161 struct glyph_row *r0;
17163 /* Compute how many chars/bytes have been added to or removed
17164 from the buffer. */
17165 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17166 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17167 Z_delta = Z - Z_old;
17168 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17170 /* Give up if PT is not in the window. Note that it already has
17171 been checked at the start of try_window_id that PT is not in
17172 front of the window start. */
17173 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17174 GIVE_UP (13);
17176 /* If window start is unchanged, we can reuse the whole matrix
17177 as is, after adjusting glyph positions. No need to compute
17178 the window end again, since its offset from Z hasn't changed. */
17179 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17180 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17181 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17182 /* PT must not be in a partially visible line. */
17183 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17184 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17186 /* Adjust positions in the glyph matrix. */
17187 if (Z_delta || Z_delta_bytes)
17189 struct glyph_row *r1
17190 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17191 increment_matrix_positions (w->current_matrix,
17192 MATRIX_ROW_VPOS (r0, current_matrix),
17193 MATRIX_ROW_VPOS (r1, current_matrix),
17194 Z_delta, Z_delta_bytes);
17197 /* Set the cursor. */
17198 row = row_containing_pos (w, PT, r0, NULL, 0);
17199 if (row)
17200 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17201 else
17202 emacs_abort ();
17203 return 1;
17207 /* Handle the case that changes are all below what is displayed in
17208 the window, and that PT is in the window. This shortcut cannot
17209 be taken if ZV is visible in the window, and text has been added
17210 there that is visible in the window. */
17211 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17212 /* ZV is not visible in the window, or there are no
17213 changes at ZV, actually. */
17214 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17215 || first_changed_charpos == last_changed_charpos))
17217 struct glyph_row *r0;
17219 /* Give up if PT is not in the window. Note that it already has
17220 been checked at the start of try_window_id that PT is not in
17221 front of the window start. */
17222 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17223 GIVE_UP (14);
17225 /* If window start is unchanged, we can reuse the whole matrix
17226 as is, without changing glyph positions since no text has
17227 been added/removed in front of the window end. */
17228 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17229 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17230 /* PT must not be in a partially visible line. */
17231 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17232 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17234 /* We have to compute the window end anew since text
17235 could have been added/removed after it. */
17236 wset_window_end_pos
17237 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17238 w->window_end_bytepos
17239 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17241 /* Set the cursor. */
17242 row = row_containing_pos (w, PT, r0, NULL, 0);
17243 if (row)
17244 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17245 else
17246 emacs_abort ();
17247 return 2;
17251 /* Give up if window start is in the changed area.
17253 The condition used to read
17255 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17257 but why that was tested escapes me at the moment. */
17258 if (CHARPOS (start) >= first_changed_charpos
17259 && CHARPOS (start) <= last_changed_charpos)
17260 GIVE_UP (15);
17262 /* Check that window start agrees with the start of the first glyph
17263 row in its current matrix. Check this after we know the window
17264 start is not in changed text, otherwise positions would not be
17265 comparable. */
17266 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17267 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17268 GIVE_UP (16);
17270 /* Give up if the window ends in strings. Overlay strings
17271 at the end are difficult to handle, so don't try. */
17272 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17273 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17274 GIVE_UP (20);
17276 /* Compute the position at which we have to start displaying new
17277 lines. Some of the lines at the top of the window might be
17278 reusable because they are not displaying changed text. Find the
17279 last row in W's current matrix not affected by changes at the
17280 start of current_buffer. Value is null if changes start in the
17281 first line of window. */
17282 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17283 if (last_unchanged_at_beg_row)
17285 /* Avoid starting to display in the middle of a character, a TAB
17286 for instance. This is easier than to set up the iterator
17287 exactly, and it's not a frequent case, so the additional
17288 effort wouldn't really pay off. */
17289 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17290 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17291 && last_unchanged_at_beg_row > w->current_matrix->rows)
17292 --last_unchanged_at_beg_row;
17294 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17295 GIVE_UP (17);
17297 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17298 GIVE_UP (18);
17299 start_pos = it.current.pos;
17301 /* Start displaying new lines in the desired matrix at the same
17302 vpos we would use in the current matrix, i.e. below
17303 last_unchanged_at_beg_row. */
17304 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17305 current_matrix);
17306 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17307 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17309 eassert (it.hpos == 0 && it.current_x == 0);
17311 else
17313 /* There are no reusable lines at the start of the window.
17314 Start displaying in the first text line. */
17315 start_display (&it, w, start);
17316 it.vpos = it.first_vpos;
17317 start_pos = it.current.pos;
17320 /* Find the first row that is not affected by changes at the end of
17321 the buffer. Value will be null if there is no unchanged row, in
17322 which case we must redisplay to the end of the window. delta
17323 will be set to the value by which buffer positions beginning with
17324 first_unchanged_at_end_row have to be adjusted due to text
17325 changes. */
17326 first_unchanged_at_end_row
17327 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17328 IF_DEBUG (debug_delta = delta);
17329 IF_DEBUG (debug_delta_bytes = delta_bytes);
17331 /* Set stop_pos to the buffer position up to which we will have to
17332 display new lines. If first_unchanged_at_end_row != NULL, this
17333 is the buffer position of the start of the line displayed in that
17334 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17335 that we don't stop at a buffer position. */
17336 stop_pos = 0;
17337 if (first_unchanged_at_end_row)
17339 eassert (last_unchanged_at_beg_row == NULL
17340 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17342 /* If this is a continuation line, move forward to the next one
17343 that isn't. Changes in lines above affect this line.
17344 Caution: this may move first_unchanged_at_end_row to a row
17345 not displaying text. */
17346 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17347 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17348 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17349 < it.last_visible_y))
17350 ++first_unchanged_at_end_row;
17352 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17353 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17354 >= it.last_visible_y))
17355 first_unchanged_at_end_row = NULL;
17356 else
17358 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17359 + delta);
17360 first_unchanged_at_end_vpos
17361 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17362 eassert (stop_pos >= Z - END_UNCHANGED);
17365 else if (last_unchanged_at_beg_row == NULL)
17366 GIVE_UP (19);
17369 #ifdef GLYPH_DEBUG
17371 /* Either there is no unchanged row at the end, or the one we have
17372 now displays text. This is a necessary condition for the window
17373 end pos calculation at the end of this function. */
17374 eassert (first_unchanged_at_end_row == NULL
17375 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17377 debug_last_unchanged_at_beg_vpos
17378 = (last_unchanged_at_beg_row
17379 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17380 : -1);
17381 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17383 #endif /* GLYPH_DEBUG */
17386 /* Display new lines. Set last_text_row to the last new line
17387 displayed which has text on it, i.e. might end up as being the
17388 line where the window_end_vpos is. */
17389 w->cursor.vpos = -1;
17390 last_text_row = NULL;
17391 overlay_arrow_seen = 0;
17392 while (it.current_y < it.last_visible_y
17393 && !fonts_changed_p
17394 && (first_unchanged_at_end_row == NULL
17395 || IT_CHARPOS (it) < stop_pos))
17397 if (display_line (&it))
17398 last_text_row = it.glyph_row - 1;
17401 if (fonts_changed_p)
17402 return -1;
17405 /* Compute differences in buffer positions, y-positions etc. for
17406 lines reused at the bottom of the window. Compute what we can
17407 scroll. */
17408 if (first_unchanged_at_end_row
17409 /* No lines reused because we displayed everything up to the
17410 bottom of the window. */
17411 && it.current_y < it.last_visible_y)
17413 dvpos = (it.vpos
17414 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17415 current_matrix));
17416 dy = it.current_y - first_unchanged_at_end_row->y;
17417 run.current_y = first_unchanged_at_end_row->y;
17418 run.desired_y = run.current_y + dy;
17419 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17421 else
17423 delta = delta_bytes = dvpos = dy
17424 = run.current_y = run.desired_y = run.height = 0;
17425 first_unchanged_at_end_row = NULL;
17427 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17430 /* Find the cursor if not already found. We have to decide whether
17431 PT will appear on this window (it sometimes doesn't, but this is
17432 not a very frequent case.) This decision has to be made before
17433 the current matrix is altered. A value of cursor.vpos < 0 means
17434 that PT is either in one of the lines beginning at
17435 first_unchanged_at_end_row or below the window. Don't care for
17436 lines that might be displayed later at the window end; as
17437 mentioned, this is not a frequent case. */
17438 if (w->cursor.vpos < 0)
17440 /* Cursor in unchanged rows at the top? */
17441 if (PT < CHARPOS (start_pos)
17442 && last_unchanged_at_beg_row)
17444 row = row_containing_pos (w, PT,
17445 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17446 last_unchanged_at_beg_row + 1, 0);
17447 if (row)
17448 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17451 /* Start from first_unchanged_at_end_row looking for PT. */
17452 else if (first_unchanged_at_end_row)
17454 row = row_containing_pos (w, PT - delta,
17455 first_unchanged_at_end_row, NULL, 0);
17456 if (row)
17457 set_cursor_from_row (w, row, w->current_matrix, delta,
17458 delta_bytes, dy, dvpos);
17461 /* Give up if cursor was not found. */
17462 if (w->cursor.vpos < 0)
17464 clear_glyph_matrix (w->desired_matrix);
17465 return -1;
17469 /* Don't let the cursor end in the scroll margins. */
17471 int this_scroll_margin, cursor_height;
17473 this_scroll_margin =
17474 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17475 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17476 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17478 if ((w->cursor.y < this_scroll_margin
17479 && CHARPOS (start) > BEGV)
17480 /* Old redisplay didn't take scroll margin into account at the bottom,
17481 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17482 || (w->cursor.y + (make_cursor_line_fully_visible_p
17483 ? cursor_height + this_scroll_margin
17484 : 1)) > it.last_visible_y)
17486 w->cursor.vpos = -1;
17487 clear_glyph_matrix (w->desired_matrix);
17488 return -1;
17492 /* Scroll the display. Do it before changing the current matrix so
17493 that xterm.c doesn't get confused about where the cursor glyph is
17494 found. */
17495 if (dy && run.height)
17497 update_begin (f);
17499 if (FRAME_WINDOW_P (f))
17501 FRAME_RIF (f)->update_window_begin_hook (w);
17502 FRAME_RIF (f)->clear_window_mouse_face (w);
17503 FRAME_RIF (f)->scroll_run_hook (w, &run);
17504 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17506 else
17508 /* Terminal frame. In this case, dvpos gives the number of
17509 lines to scroll by; dvpos < 0 means scroll up. */
17510 int from_vpos
17511 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17512 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17513 int end = (WINDOW_TOP_EDGE_LINE (w)
17514 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17515 + window_internal_height (w));
17517 #if defined (HAVE_GPM) || defined (MSDOS)
17518 x_clear_window_mouse_face (w);
17519 #endif
17520 /* Perform the operation on the screen. */
17521 if (dvpos > 0)
17523 /* Scroll last_unchanged_at_beg_row to the end of the
17524 window down dvpos lines. */
17525 set_terminal_window (f, end);
17527 /* On dumb terminals delete dvpos lines at the end
17528 before inserting dvpos empty lines. */
17529 if (!FRAME_SCROLL_REGION_OK (f))
17530 ins_del_lines (f, end - dvpos, -dvpos);
17532 /* Insert dvpos empty lines in front of
17533 last_unchanged_at_beg_row. */
17534 ins_del_lines (f, from, dvpos);
17536 else if (dvpos < 0)
17538 /* Scroll up last_unchanged_at_beg_vpos to the end of
17539 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17540 set_terminal_window (f, end);
17542 /* Delete dvpos lines in front of
17543 last_unchanged_at_beg_vpos. ins_del_lines will set
17544 the cursor to the given vpos and emit |dvpos| delete
17545 line sequences. */
17546 ins_del_lines (f, from + dvpos, dvpos);
17548 /* On a dumb terminal insert dvpos empty lines at the
17549 end. */
17550 if (!FRAME_SCROLL_REGION_OK (f))
17551 ins_del_lines (f, end + dvpos, -dvpos);
17554 set_terminal_window (f, 0);
17557 update_end (f);
17560 /* Shift reused rows of the current matrix to the right position.
17561 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17562 text. */
17563 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17564 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17565 if (dvpos < 0)
17567 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17568 bottom_vpos, dvpos);
17569 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17570 bottom_vpos);
17572 else if (dvpos > 0)
17574 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17575 bottom_vpos, dvpos);
17576 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17577 first_unchanged_at_end_vpos + dvpos);
17580 /* For frame-based redisplay, make sure that current frame and window
17581 matrix are in sync with respect to glyph memory. */
17582 if (!FRAME_WINDOW_P (f))
17583 sync_frame_with_window_matrix_rows (w);
17585 /* Adjust buffer positions in reused rows. */
17586 if (delta || delta_bytes)
17587 increment_matrix_positions (current_matrix,
17588 first_unchanged_at_end_vpos + dvpos,
17589 bottom_vpos, delta, delta_bytes);
17591 /* Adjust Y positions. */
17592 if (dy)
17593 shift_glyph_matrix (w, current_matrix,
17594 first_unchanged_at_end_vpos + dvpos,
17595 bottom_vpos, dy);
17597 if (first_unchanged_at_end_row)
17599 first_unchanged_at_end_row += dvpos;
17600 if (first_unchanged_at_end_row->y >= it.last_visible_y
17601 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17602 first_unchanged_at_end_row = NULL;
17605 /* If scrolling up, there may be some lines to display at the end of
17606 the window. */
17607 last_text_row_at_end = NULL;
17608 if (dy < 0)
17610 /* Scrolling up can leave for example a partially visible line
17611 at the end of the window to be redisplayed. */
17612 /* Set last_row to the glyph row in the current matrix where the
17613 window end line is found. It has been moved up or down in
17614 the matrix by dvpos. */
17615 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17616 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17618 /* If last_row is the window end line, it should display text. */
17619 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17621 /* If window end line was partially visible before, begin
17622 displaying at that line. Otherwise begin displaying with the
17623 line following it. */
17624 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17626 init_to_row_start (&it, w, last_row);
17627 it.vpos = last_vpos;
17628 it.current_y = last_row->y;
17630 else
17632 init_to_row_end (&it, w, last_row);
17633 it.vpos = 1 + last_vpos;
17634 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17635 ++last_row;
17638 /* We may start in a continuation line. If so, we have to
17639 get the right continuation_lines_width and current_x. */
17640 it.continuation_lines_width = last_row->continuation_lines_width;
17641 it.hpos = it.current_x = 0;
17643 /* Display the rest of the lines at the window end. */
17644 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17645 while (it.current_y < it.last_visible_y
17646 && !fonts_changed_p)
17648 /* Is it always sure that the display agrees with lines in
17649 the current matrix? I don't think so, so we mark rows
17650 displayed invalid in the current matrix by setting their
17651 enabled_p flag to zero. */
17652 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17653 if (display_line (&it))
17654 last_text_row_at_end = it.glyph_row - 1;
17658 /* Update window_end_pos and window_end_vpos. */
17659 if (first_unchanged_at_end_row
17660 && !last_text_row_at_end)
17662 /* Window end line if one of the preserved rows from the current
17663 matrix. Set row to the last row displaying text in current
17664 matrix starting at first_unchanged_at_end_row, after
17665 scrolling. */
17666 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17667 row = find_last_row_displaying_text (w->current_matrix, &it,
17668 first_unchanged_at_end_row);
17669 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17671 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17672 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17673 wset_window_end_vpos
17674 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17675 eassert (w->window_end_bytepos >= 0);
17676 IF_DEBUG (debug_method_add (w, "A"));
17678 else if (last_text_row_at_end)
17680 wset_window_end_pos
17681 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17682 w->window_end_bytepos
17683 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17684 wset_window_end_vpos
17685 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17686 desired_matrix)));
17687 eassert (w->window_end_bytepos >= 0);
17688 IF_DEBUG (debug_method_add (w, "B"));
17690 else if (last_text_row)
17692 /* We have displayed either to the end of the window or at the
17693 end of the window, i.e. the last row with text is to be found
17694 in the desired matrix. */
17695 wset_window_end_pos
17696 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17697 w->window_end_bytepos
17698 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17699 wset_window_end_vpos
17700 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17701 eassert (w->window_end_bytepos >= 0);
17703 else if (first_unchanged_at_end_row == NULL
17704 && last_text_row == NULL
17705 && last_text_row_at_end == NULL)
17707 /* Displayed to end of window, but no line containing text was
17708 displayed. Lines were deleted at the end of the window. */
17709 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17710 int vpos = XFASTINT (w->window_end_vpos);
17711 struct glyph_row *current_row = current_matrix->rows + vpos;
17712 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17714 for (row = NULL;
17715 row == NULL && vpos >= first_vpos;
17716 --vpos, --current_row, --desired_row)
17718 if (desired_row->enabled_p)
17720 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17721 row = desired_row;
17723 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17724 row = current_row;
17727 eassert (row != NULL);
17728 wset_window_end_vpos (w, make_number (vpos + 1));
17729 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17730 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17731 eassert (w->window_end_bytepos >= 0);
17732 IF_DEBUG (debug_method_add (w, "C"));
17734 else
17735 emacs_abort ();
17737 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17738 debug_end_vpos = XFASTINT (w->window_end_vpos));
17740 /* Record that display has not been completed. */
17741 w->window_end_valid = 0;
17742 w->desired_matrix->no_scrolling_p = 1;
17743 return 3;
17745 #undef GIVE_UP
17750 /***********************************************************************
17751 More debugging support
17752 ***********************************************************************/
17754 #ifdef GLYPH_DEBUG
17756 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17757 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17758 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17761 /* Dump the contents of glyph matrix MATRIX on stderr.
17763 GLYPHS 0 means don't show glyph contents.
17764 GLYPHS 1 means show glyphs in short form
17765 GLYPHS > 1 means show glyphs in long form. */
17767 void
17768 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17770 int i;
17771 for (i = 0; i < matrix->nrows; ++i)
17772 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17776 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17777 the glyph row and area where the glyph comes from. */
17779 void
17780 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17782 if (glyph->type == CHAR_GLYPH
17783 || glyph->type == GLYPHLESS_GLYPH)
17785 fprintf (stderr,
17786 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17787 glyph - row->glyphs[TEXT_AREA],
17788 (glyph->type == CHAR_GLYPH
17789 ? 'C'
17790 : 'G'),
17791 glyph->charpos,
17792 (BUFFERP (glyph->object)
17793 ? 'B'
17794 : (STRINGP (glyph->object)
17795 ? 'S'
17796 : (INTEGERP (glyph->object)
17797 ? '0'
17798 : '-'))),
17799 glyph->pixel_width,
17800 glyph->u.ch,
17801 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17802 ? glyph->u.ch
17803 : '.'),
17804 glyph->face_id,
17805 glyph->left_box_line_p,
17806 glyph->right_box_line_p);
17808 else if (glyph->type == STRETCH_GLYPH)
17810 fprintf (stderr,
17811 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17812 glyph - row->glyphs[TEXT_AREA],
17813 'S',
17814 glyph->charpos,
17815 (BUFFERP (glyph->object)
17816 ? 'B'
17817 : (STRINGP (glyph->object)
17818 ? 'S'
17819 : (INTEGERP (glyph->object)
17820 ? '0'
17821 : '-'))),
17822 glyph->pixel_width,
17824 ' ',
17825 glyph->face_id,
17826 glyph->left_box_line_p,
17827 glyph->right_box_line_p);
17829 else if (glyph->type == IMAGE_GLYPH)
17831 fprintf (stderr,
17832 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17833 glyph - row->glyphs[TEXT_AREA],
17834 'I',
17835 glyph->charpos,
17836 (BUFFERP (glyph->object)
17837 ? 'B'
17838 : (STRINGP (glyph->object)
17839 ? 'S'
17840 : (INTEGERP (glyph->object)
17841 ? '0'
17842 : '-'))),
17843 glyph->pixel_width,
17844 glyph->u.img_id,
17845 '.',
17846 glyph->face_id,
17847 glyph->left_box_line_p,
17848 glyph->right_box_line_p);
17850 else if (glyph->type == COMPOSITE_GLYPH)
17852 fprintf (stderr,
17853 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17854 glyph - row->glyphs[TEXT_AREA],
17855 '+',
17856 glyph->charpos,
17857 (BUFFERP (glyph->object)
17858 ? 'B'
17859 : (STRINGP (glyph->object)
17860 ? 'S'
17861 : (INTEGERP (glyph->object)
17862 ? '0'
17863 : '-'))),
17864 glyph->pixel_width,
17865 glyph->u.cmp.id);
17866 if (glyph->u.cmp.automatic)
17867 fprintf (stderr,
17868 "[%d-%d]",
17869 glyph->slice.cmp.from, glyph->slice.cmp.to);
17870 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17871 glyph->face_id,
17872 glyph->left_box_line_p,
17873 glyph->right_box_line_p);
17878 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17879 GLYPHS 0 means don't show glyph contents.
17880 GLYPHS 1 means show glyphs in short form
17881 GLYPHS > 1 means show glyphs in long form. */
17883 void
17884 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17886 if (glyphs != 1)
17888 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17889 fprintf (stderr, "==============================================================================\n");
17891 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17892 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17893 vpos,
17894 MATRIX_ROW_START_CHARPOS (row),
17895 MATRIX_ROW_END_CHARPOS (row),
17896 row->used[TEXT_AREA],
17897 row->contains_overlapping_glyphs_p,
17898 row->enabled_p,
17899 row->truncated_on_left_p,
17900 row->truncated_on_right_p,
17901 row->continued_p,
17902 MATRIX_ROW_CONTINUATION_LINE_P (row),
17903 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17904 row->ends_at_zv_p,
17905 row->fill_line_p,
17906 row->ends_in_middle_of_char_p,
17907 row->starts_in_middle_of_char_p,
17908 row->mouse_face_p,
17909 row->x,
17910 row->y,
17911 row->pixel_width,
17912 row->height,
17913 row->visible_height,
17914 row->ascent,
17915 row->phys_ascent);
17916 /* The next 3 lines should align to "Start" in the header. */
17917 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17918 row->end.overlay_string_index,
17919 row->continuation_lines_width);
17920 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17921 CHARPOS (row->start.string_pos),
17922 CHARPOS (row->end.string_pos));
17923 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17924 row->end.dpvec_index);
17927 if (glyphs > 1)
17929 int area;
17931 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17933 struct glyph *glyph = row->glyphs[area];
17934 struct glyph *glyph_end = glyph + row->used[area];
17936 /* Glyph for a line end in text. */
17937 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17938 ++glyph_end;
17940 if (glyph < glyph_end)
17941 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17943 for (; glyph < glyph_end; ++glyph)
17944 dump_glyph (row, glyph, area);
17947 else if (glyphs == 1)
17949 int area;
17951 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17953 char *s = alloca (row->used[area] + 4);
17954 int i;
17956 for (i = 0; i < row->used[area]; ++i)
17958 struct glyph *glyph = row->glyphs[area] + i;
17959 if (i == row->used[area] - 1
17960 && area == TEXT_AREA
17961 && INTEGERP (glyph->object)
17962 && glyph->type == CHAR_GLYPH
17963 && glyph->u.ch == ' ')
17965 strcpy (&s[i], "[\\n]");
17966 i += 4;
17968 else if (glyph->type == CHAR_GLYPH
17969 && glyph->u.ch < 0x80
17970 && glyph->u.ch >= ' ')
17971 s[i] = glyph->u.ch;
17972 else
17973 s[i] = '.';
17976 s[i] = '\0';
17977 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17983 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17984 Sdump_glyph_matrix, 0, 1, "p",
17985 doc: /* Dump the current matrix of the selected window to stderr.
17986 Shows contents of glyph row structures. With non-nil
17987 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17988 glyphs in short form, otherwise show glyphs in long form. */)
17989 (Lisp_Object glyphs)
17991 struct window *w = XWINDOW (selected_window);
17992 struct buffer *buffer = XBUFFER (w->contents);
17994 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17995 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17996 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17997 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17998 fprintf (stderr, "=============================================\n");
17999 dump_glyph_matrix (w->current_matrix,
18000 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18001 return Qnil;
18005 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18006 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18007 (void)
18009 struct frame *f = XFRAME (selected_frame);
18010 dump_glyph_matrix (f->current_matrix, 1);
18011 return Qnil;
18015 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18016 doc: /* Dump glyph row ROW to stderr.
18017 GLYPH 0 means don't dump glyphs.
18018 GLYPH 1 means dump glyphs in short form.
18019 GLYPH > 1 or omitted means dump glyphs in long form. */)
18020 (Lisp_Object row, Lisp_Object glyphs)
18022 struct glyph_matrix *matrix;
18023 EMACS_INT vpos;
18025 CHECK_NUMBER (row);
18026 matrix = XWINDOW (selected_window)->current_matrix;
18027 vpos = XINT (row);
18028 if (vpos >= 0 && vpos < matrix->nrows)
18029 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18030 vpos,
18031 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18032 return Qnil;
18036 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18037 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18038 GLYPH 0 means don't dump glyphs.
18039 GLYPH 1 means dump glyphs in short form.
18040 GLYPH > 1 or omitted means dump glyphs in long form. */)
18041 (Lisp_Object row, Lisp_Object glyphs)
18043 struct frame *sf = SELECTED_FRAME ();
18044 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18045 EMACS_INT vpos;
18047 CHECK_NUMBER (row);
18048 vpos = XINT (row);
18049 if (vpos >= 0 && vpos < m->nrows)
18050 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18051 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18052 return Qnil;
18056 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18057 doc: /* Toggle tracing of redisplay.
18058 With ARG, turn tracing on if and only if ARG is positive. */)
18059 (Lisp_Object arg)
18061 if (NILP (arg))
18062 trace_redisplay_p = !trace_redisplay_p;
18063 else
18065 arg = Fprefix_numeric_value (arg);
18066 trace_redisplay_p = XINT (arg) > 0;
18069 return Qnil;
18073 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18074 doc: /* Like `format', but print result to stderr.
18075 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18076 (ptrdiff_t nargs, Lisp_Object *args)
18078 Lisp_Object s = Fformat (nargs, args);
18079 fprintf (stderr, "%s", SDATA (s));
18080 return Qnil;
18083 #endif /* GLYPH_DEBUG */
18087 /***********************************************************************
18088 Building Desired Matrix Rows
18089 ***********************************************************************/
18091 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18092 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18094 static struct glyph_row *
18095 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18097 struct frame *f = XFRAME (WINDOW_FRAME (w));
18098 struct buffer *buffer = XBUFFER (w->contents);
18099 struct buffer *old = current_buffer;
18100 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18101 int arrow_len = SCHARS (overlay_arrow_string);
18102 const unsigned char *arrow_end = arrow_string + arrow_len;
18103 const unsigned char *p;
18104 struct it it;
18105 bool multibyte_p;
18106 int n_glyphs_before;
18108 set_buffer_temp (buffer);
18109 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18110 it.glyph_row->used[TEXT_AREA] = 0;
18111 SET_TEXT_POS (it.position, 0, 0);
18113 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18114 p = arrow_string;
18115 while (p < arrow_end)
18117 Lisp_Object face, ilisp;
18119 /* Get the next character. */
18120 if (multibyte_p)
18121 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18122 else
18124 it.c = it.char_to_display = *p, it.len = 1;
18125 if (! ASCII_CHAR_P (it.c))
18126 it.char_to_display = BYTE8_TO_CHAR (it.c);
18128 p += it.len;
18130 /* Get its face. */
18131 ilisp = make_number (p - arrow_string);
18132 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18133 it.face_id = compute_char_face (f, it.char_to_display, face);
18135 /* Compute its width, get its glyphs. */
18136 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18137 SET_TEXT_POS (it.position, -1, -1);
18138 PRODUCE_GLYPHS (&it);
18140 /* If this character doesn't fit any more in the line, we have
18141 to remove some glyphs. */
18142 if (it.current_x > it.last_visible_x)
18144 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18145 break;
18149 set_buffer_temp (old);
18150 return it.glyph_row;
18154 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18155 glyphs to insert is determined by produce_special_glyphs. */
18157 static void
18158 insert_left_trunc_glyphs (struct it *it)
18160 struct it truncate_it;
18161 struct glyph *from, *end, *to, *toend;
18163 eassert (!FRAME_WINDOW_P (it->f)
18164 || (!it->glyph_row->reversed_p
18165 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18166 || (it->glyph_row->reversed_p
18167 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18169 /* Get the truncation glyphs. */
18170 truncate_it = *it;
18171 truncate_it.current_x = 0;
18172 truncate_it.face_id = DEFAULT_FACE_ID;
18173 truncate_it.glyph_row = &scratch_glyph_row;
18174 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18175 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18176 truncate_it.object = make_number (0);
18177 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18179 /* Overwrite glyphs from IT with truncation glyphs. */
18180 if (!it->glyph_row->reversed_p)
18182 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18184 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18185 end = from + tused;
18186 to = it->glyph_row->glyphs[TEXT_AREA];
18187 toend = to + it->glyph_row->used[TEXT_AREA];
18188 if (FRAME_WINDOW_P (it->f))
18190 /* On GUI frames, when variable-size fonts are displayed,
18191 the truncation glyphs may need more pixels than the row's
18192 glyphs they overwrite. We overwrite more glyphs to free
18193 enough screen real estate, and enlarge the stretch glyph
18194 on the right (see display_line), if there is one, to
18195 preserve the screen position of the truncation glyphs on
18196 the right. */
18197 int w = 0;
18198 struct glyph *g = to;
18199 short used;
18201 /* The first glyph could be partially visible, in which case
18202 it->glyph_row->x will be negative. But we want the left
18203 truncation glyphs to be aligned at the left margin of the
18204 window, so we override the x coordinate at which the row
18205 will begin. */
18206 it->glyph_row->x = 0;
18207 while (g < toend && w < it->truncation_pixel_width)
18209 w += g->pixel_width;
18210 ++g;
18212 if (g - to - tused > 0)
18214 memmove (to + tused, g, (toend - g) * sizeof(*g));
18215 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18217 used = it->glyph_row->used[TEXT_AREA];
18218 if (it->glyph_row->truncated_on_right_p
18219 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18220 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18221 == STRETCH_GLYPH)
18223 int extra = w - it->truncation_pixel_width;
18225 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18229 while (from < end)
18230 *to++ = *from++;
18232 /* There may be padding glyphs left over. Overwrite them too. */
18233 if (!FRAME_WINDOW_P (it->f))
18235 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18237 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18238 while (from < end)
18239 *to++ = *from++;
18243 if (to > toend)
18244 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18246 else
18248 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18250 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18251 that back to front. */
18252 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18253 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18254 toend = it->glyph_row->glyphs[TEXT_AREA];
18255 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18256 if (FRAME_WINDOW_P (it->f))
18258 int w = 0;
18259 struct glyph *g = to;
18261 while (g >= toend && w < it->truncation_pixel_width)
18263 w += g->pixel_width;
18264 --g;
18266 if (to - g - tused > 0)
18267 to = g + tused;
18268 if (it->glyph_row->truncated_on_right_p
18269 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18270 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18272 int extra = w - it->truncation_pixel_width;
18274 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18278 while (from >= end && to >= toend)
18279 *to-- = *from--;
18280 if (!FRAME_WINDOW_P (it->f))
18282 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18284 from =
18285 truncate_it.glyph_row->glyphs[TEXT_AREA]
18286 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18287 while (from >= end && to >= toend)
18288 *to-- = *from--;
18291 if (from >= end)
18293 /* Need to free some room before prepending additional
18294 glyphs. */
18295 int move_by = from - end + 1;
18296 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18297 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18299 for ( ; g >= g0; g--)
18300 g[move_by] = *g;
18301 while (from >= end)
18302 *to-- = *from--;
18303 it->glyph_row->used[TEXT_AREA] += move_by;
18308 /* Compute the hash code for ROW. */
18309 unsigned
18310 row_hash (struct glyph_row *row)
18312 int area, k;
18313 unsigned hashval = 0;
18315 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18316 for (k = 0; k < row->used[area]; ++k)
18317 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18318 + row->glyphs[area][k].u.val
18319 + row->glyphs[area][k].face_id
18320 + row->glyphs[area][k].padding_p
18321 + (row->glyphs[area][k].type << 2));
18323 return hashval;
18326 /* Compute the pixel height and width of IT->glyph_row.
18328 Most of the time, ascent and height of a display line will be equal
18329 to the max_ascent and max_height values of the display iterator
18330 structure. This is not the case if
18332 1. We hit ZV without displaying anything. In this case, max_ascent
18333 and max_height will be zero.
18335 2. We have some glyphs that don't contribute to the line height.
18336 (The glyph row flag contributes_to_line_height_p is for future
18337 pixmap extensions).
18339 The first case is easily covered by using default values because in
18340 these cases, the line height does not really matter, except that it
18341 must not be zero. */
18343 static void
18344 compute_line_metrics (struct it *it)
18346 struct glyph_row *row = it->glyph_row;
18348 if (FRAME_WINDOW_P (it->f))
18350 int i, min_y, max_y;
18352 /* The line may consist of one space only, that was added to
18353 place the cursor on it. If so, the row's height hasn't been
18354 computed yet. */
18355 if (row->height == 0)
18357 if (it->max_ascent + it->max_descent == 0)
18358 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18359 row->ascent = it->max_ascent;
18360 row->height = it->max_ascent + it->max_descent;
18361 row->phys_ascent = it->max_phys_ascent;
18362 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18363 row->extra_line_spacing = it->max_extra_line_spacing;
18366 /* Compute the width of this line. */
18367 row->pixel_width = row->x;
18368 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18369 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18371 eassert (row->pixel_width >= 0);
18372 eassert (row->ascent >= 0 && row->height > 0);
18374 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18375 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18377 /* If first line's physical ascent is larger than its logical
18378 ascent, use the physical ascent, and make the row taller.
18379 This makes accented characters fully visible. */
18380 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18381 && row->phys_ascent > row->ascent)
18383 row->height += row->phys_ascent - row->ascent;
18384 row->ascent = row->phys_ascent;
18387 /* Compute how much of the line is visible. */
18388 row->visible_height = row->height;
18390 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18391 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18393 if (row->y < min_y)
18394 row->visible_height -= min_y - row->y;
18395 if (row->y + row->height > max_y)
18396 row->visible_height -= row->y + row->height - max_y;
18398 else
18400 row->pixel_width = row->used[TEXT_AREA];
18401 if (row->continued_p)
18402 row->pixel_width -= it->continuation_pixel_width;
18403 else if (row->truncated_on_right_p)
18404 row->pixel_width -= it->truncation_pixel_width;
18405 row->ascent = row->phys_ascent = 0;
18406 row->height = row->phys_height = row->visible_height = 1;
18407 row->extra_line_spacing = 0;
18410 /* Compute a hash code for this row. */
18411 row->hash = row_hash (row);
18413 it->max_ascent = it->max_descent = 0;
18414 it->max_phys_ascent = it->max_phys_descent = 0;
18418 /* Append one space to the glyph row of iterator IT if doing a
18419 window-based redisplay. The space has the same face as
18420 IT->face_id. Value is non-zero if a space was added.
18422 This function is called to make sure that there is always one glyph
18423 at the end of a glyph row that the cursor can be set on under
18424 window-systems. (If there weren't such a glyph we would not know
18425 how wide and tall a box cursor should be displayed).
18427 At the same time this space let's a nicely handle clearing to the
18428 end of the line if the row ends in italic text. */
18430 static int
18431 append_space_for_newline (struct it *it, int default_face_p)
18433 if (FRAME_WINDOW_P (it->f))
18435 int n = it->glyph_row->used[TEXT_AREA];
18437 if (it->glyph_row->glyphs[TEXT_AREA] + n
18438 < it->glyph_row->glyphs[1 + TEXT_AREA])
18440 /* Save some values that must not be changed.
18441 Must save IT->c and IT->len because otherwise
18442 ITERATOR_AT_END_P wouldn't work anymore after
18443 append_space_for_newline has been called. */
18444 enum display_element_type saved_what = it->what;
18445 int saved_c = it->c, saved_len = it->len;
18446 int saved_char_to_display = it->char_to_display;
18447 int saved_x = it->current_x;
18448 int saved_face_id = it->face_id;
18449 int saved_box_end = it->end_of_box_run_p;
18450 struct text_pos saved_pos;
18451 Lisp_Object saved_object;
18452 struct face *face;
18454 saved_object = it->object;
18455 saved_pos = it->position;
18457 it->what = IT_CHARACTER;
18458 memset (&it->position, 0, sizeof it->position);
18459 it->object = make_number (0);
18460 it->c = it->char_to_display = ' ';
18461 it->len = 1;
18463 /* If the default face was remapped, be sure to use the
18464 remapped face for the appended newline. */
18465 if (default_face_p)
18466 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18467 else if (it->face_before_selective_p)
18468 it->face_id = it->saved_face_id;
18469 face = FACE_FROM_ID (it->f, it->face_id);
18470 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18471 /* In R2L rows, we will prepend a stretch glyph that will
18472 have the end_of_box_run_p flag set for it, so there's no
18473 need for the appended newline glyph to have that flag
18474 set. */
18475 if (it->glyph_row->reversed_p
18476 /* But if the appended newline glyph goes all the way to
18477 the end of the row, there will be no stretch glyph,
18478 so leave the box flag set. */
18479 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18480 it->end_of_box_run_p = 0;
18482 PRODUCE_GLYPHS (it);
18484 it->override_ascent = -1;
18485 it->constrain_row_ascent_descent_p = 0;
18486 it->current_x = saved_x;
18487 it->object = saved_object;
18488 it->position = saved_pos;
18489 it->what = saved_what;
18490 it->face_id = saved_face_id;
18491 it->len = saved_len;
18492 it->c = saved_c;
18493 it->char_to_display = saved_char_to_display;
18494 it->end_of_box_run_p = saved_box_end;
18495 return 1;
18499 return 0;
18503 /* Extend the face of the last glyph in the text area of IT->glyph_row
18504 to the end of the display line. Called from display_line. If the
18505 glyph row is empty, add a space glyph to it so that we know the
18506 face to draw. Set the glyph row flag fill_line_p. If the glyph
18507 row is R2L, prepend a stretch glyph to cover the empty space to the
18508 left of the leftmost glyph. */
18510 static void
18511 extend_face_to_end_of_line (struct it *it)
18513 struct face *face, *default_face;
18514 struct frame *f = it->f;
18516 /* If line is already filled, do nothing. Non window-system frames
18517 get a grace of one more ``pixel'' because their characters are
18518 1-``pixel'' wide, so they hit the equality too early. This grace
18519 is needed only for R2L rows that are not continued, to produce
18520 one extra blank where we could display the cursor. */
18521 if (it->current_x >= it->last_visible_x
18522 + (!FRAME_WINDOW_P (f)
18523 && it->glyph_row->reversed_p
18524 && !it->glyph_row->continued_p))
18525 return;
18527 /* The default face, possibly remapped. */
18528 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18530 /* Face extension extends the background and box of IT->face_id
18531 to the end of the line. If the background equals the background
18532 of the frame, we don't have to do anything. */
18533 if (it->face_before_selective_p)
18534 face = FACE_FROM_ID (f, it->saved_face_id);
18535 else
18536 face = FACE_FROM_ID (f, it->face_id);
18538 if (FRAME_WINDOW_P (f)
18539 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18540 && face->box == FACE_NO_BOX
18541 && face->background == FRAME_BACKGROUND_PIXEL (f)
18542 && !face->stipple
18543 && !it->glyph_row->reversed_p)
18544 return;
18546 /* Set the glyph row flag indicating that the face of the last glyph
18547 in the text area has to be drawn to the end of the text area. */
18548 it->glyph_row->fill_line_p = 1;
18550 /* If current character of IT is not ASCII, make sure we have the
18551 ASCII face. This will be automatically undone the next time
18552 get_next_display_element returns a multibyte character. Note
18553 that the character will always be single byte in unibyte
18554 text. */
18555 if (!ASCII_CHAR_P (it->c))
18557 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18560 if (FRAME_WINDOW_P (f))
18562 /* If the row is empty, add a space with the current face of IT,
18563 so that we know which face to draw. */
18564 if (it->glyph_row->used[TEXT_AREA] == 0)
18566 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18567 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18568 it->glyph_row->used[TEXT_AREA] = 1;
18570 #ifdef HAVE_WINDOW_SYSTEM
18571 if (it->glyph_row->reversed_p)
18573 /* Prepend a stretch glyph to the row, such that the
18574 rightmost glyph will be drawn flushed all the way to the
18575 right margin of the window. The stretch glyph that will
18576 occupy the empty space, if any, to the left of the
18577 glyphs. */
18578 struct font *font = face->font ? face->font : FRAME_FONT (f);
18579 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18580 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18581 struct glyph *g;
18582 int row_width, stretch_ascent, stretch_width;
18583 struct text_pos saved_pos;
18584 int saved_face_id, saved_avoid_cursor, saved_box_start;
18586 for (row_width = 0, g = row_start; g < row_end; g++)
18587 row_width += g->pixel_width;
18588 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18589 if (stretch_width > 0)
18591 stretch_ascent =
18592 (((it->ascent + it->descent)
18593 * FONT_BASE (font)) / FONT_HEIGHT (font));
18594 saved_pos = it->position;
18595 memset (&it->position, 0, sizeof it->position);
18596 saved_avoid_cursor = it->avoid_cursor_p;
18597 it->avoid_cursor_p = 1;
18598 saved_face_id = it->face_id;
18599 saved_box_start = it->start_of_box_run_p;
18600 /* The last row's stretch glyph should get the default
18601 face, to avoid painting the rest of the window with
18602 the region face, if the region ends at ZV. */
18603 if (it->glyph_row->ends_at_zv_p)
18604 it->face_id = default_face->id;
18605 else
18606 it->face_id = face->id;
18607 it->start_of_box_run_p = 0;
18608 append_stretch_glyph (it, make_number (0), stretch_width,
18609 it->ascent + it->descent, stretch_ascent);
18610 it->position = saved_pos;
18611 it->avoid_cursor_p = saved_avoid_cursor;
18612 it->face_id = saved_face_id;
18613 it->start_of_box_run_p = saved_box_start;
18616 #endif /* HAVE_WINDOW_SYSTEM */
18618 else
18620 /* Save some values that must not be changed. */
18621 int saved_x = it->current_x;
18622 struct text_pos saved_pos;
18623 Lisp_Object saved_object;
18624 enum display_element_type saved_what = it->what;
18625 int saved_face_id = it->face_id;
18627 saved_object = it->object;
18628 saved_pos = it->position;
18630 it->what = IT_CHARACTER;
18631 memset (&it->position, 0, sizeof it->position);
18632 it->object = make_number (0);
18633 it->c = it->char_to_display = ' ';
18634 it->len = 1;
18635 /* The last row's blank glyphs should get the default face, to
18636 avoid painting the rest of the window with the region face,
18637 if the region ends at ZV. */
18638 if (it->glyph_row->ends_at_zv_p)
18639 it->face_id = default_face->id;
18640 else
18641 it->face_id = face->id;
18643 PRODUCE_GLYPHS (it);
18645 while (it->current_x <= it->last_visible_x)
18646 PRODUCE_GLYPHS (it);
18648 /* Don't count these blanks really. It would let us insert a left
18649 truncation glyph below and make us set the cursor on them, maybe. */
18650 it->current_x = saved_x;
18651 it->object = saved_object;
18652 it->position = saved_pos;
18653 it->what = saved_what;
18654 it->face_id = saved_face_id;
18659 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18660 trailing whitespace. */
18662 static int
18663 trailing_whitespace_p (ptrdiff_t charpos)
18665 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18666 int c = 0;
18668 while (bytepos < ZV_BYTE
18669 && (c = FETCH_CHAR (bytepos),
18670 c == ' ' || c == '\t'))
18671 ++bytepos;
18673 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18675 if (bytepos != PT_BYTE)
18676 return 1;
18678 return 0;
18682 /* Highlight trailing whitespace, if any, in ROW. */
18684 static void
18685 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18687 int used = row->used[TEXT_AREA];
18689 if (used)
18691 struct glyph *start = row->glyphs[TEXT_AREA];
18692 struct glyph *glyph = start + used - 1;
18694 if (row->reversed_p)
18696 /* Right-to-left rows need to be processed in the opposite
18697 direction, so swap the edge pointers. */
18698 glyph = start;
18699 start = row->glyphs[TEXT_AREA] + used - 1;
18702 /* Skip over glyphs inserted to display the cursor at the
18703 end of a line, for extending the face of the last glyph
18704 to the end of the line on terminals, and for truncation
18705 and continuation glyphs. */
18706 if (!row->reversed_p)
18708 while (glyph >= start
18709 && glyph->type == CHAR_GLYPH
18710 && INTEGERP (glyph->object))
18711 --glyph;
18713 else
18715 while (glyph <= start
18716 && glyph->type == CHAR_GLYPH
18717 && INTEGERP (glyph->object))
18718 ++glyph;
18721 /* If last glyph is a space or stretch, and it's trailing
18722 whitespace, set the face of all trailing whitespace glyphs in
18723 IT->glyph_row to `trailing-whitespace'. */
18724 if ((row->reversed_p ? glyph <= start : glyph >= start)
18725 && BUFFERP (glyph->object)
18726 && (glyph->type == STRETCH_GLYPH
18727 || (glyph->type == CHAR_GLYPH
18728 && glyph->u.ch == ' '))
18729 && trailing_whitespace_p (glyph->charpos))
18731 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18732 if (face_id < 0)
18733 return;
18735 if (!row->reversed_p)
18737 while (glyph >= start
18738 && BUFFERP (glyph->object)
18739 && (glyph->type == STRETCH_GLYPH
18740 || (glyph->type == CHAR_GLYPH
18741 && glyph->u.ch == ' ')))
18742 (glyph--)->face_id = face_id;
18744 else
18746 while (glyph <= start
18747 && BUFFERP (glyph->object)
18748 && (glyph->type == STRETCH_GLYPH
18749 || (glyph->type == CHAR_GLYPH
18750 && glyph->u.ch == ' ')))
18751 (glyph++)->face_id = face_id;
18758 /* Value is non-zero if glyph row ROW should be
18759 considered to hold the buffer position CHARPOS. */
18761 static int
18762 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18764 int result = 1;
18766 if (charpos == CHARPOS (row->end.pos)
18767 || charpos == MATRIX_ROW_END_CHARPOS (row))
18769 /* Suppose the row ends on a string.
18770 Unless the row is continued, that means it ends on a newline
18771 in the string. If it's anything other than a display string
18772 (e.g., a before-string from an overlay), we don't want the
18773 cursor there. (This heuristic seems to give the optimal
18774 behavior for the various types of multi-line strings.)
18775 One exception: if the string has `cursor' property on one of
18776 its characters, we _do_ want the cursor there. */
18777 if (CHARPOS (row->end.string_pos) >= 0)
18779 if (row->continued_p)
18780 result = 1;
18781 else
18783 /* Check for `display' property. */
18784 struct glyph *beg = row->glyphs[TEXT_AREA];
18785 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18786 struct glyph *glyph;
18788 result = 0;
18789 for (glyph = end; glyph >= beg; --glyph)
18790 if (STRINGP (glyph->object))
18792 Lisp_Object prop
18793 = Fget_char_property (make_number (charpos),
18794 Qdisplay, Qnil);
18795 result =
18796 (!NILP (prop)
18797 && display_prop_string_p (prop, glyph->object));
18798 /* If there's a `cursor' property on one of the
18799 string's characters, this row is a cursor row,
18800 even though this is not a display string. */
18801 if (!result)
18803 Lisp_Object s = glyph->object;
18805 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18807 ptrdiff_t gpos = glyph->charpos;
18809 if (!NILP (Fget_char_property (make_number (gpos),
18810 Qcursor, s)))
18812 result = 1;
18813 break;
18817 break;
18821 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18823 /* If the row ends in middle of a real character,
18824 and the line is continued, we want the cursor here.
18825 That's because CHARPOS (ROW->end.pos) would equal
18826 PT if PT is before the character. */
18827 if (!row->ends_in_ellipsis_p)
18828 result = row->continued_p;
18829 else
18830 /* If the row ends in an ellipsis, then
18831 CHARPOS (ROW->end.pos) will equal point after the
18832 invisible text. We want that position to be displayed
18833 after the ellipsis. */
18834 result = 0;
18836 /* If the row ends at ZV, display the cursor at the end of that
18837 row instead of at the start of the row below. */
18838 else if (row->ends_at_zv_p)
18839 result = 1;
18840 else
18841 result = 0;
18844 return result;
18847 /* Value is non-zero if glyph row ROW should be
18848 used to hold the cursor. */
18850 static int
18851 cursor_row_p (struct glyph_row *row)
18853 return row_for_charpos_p (row, PT);
18858 /* Push the property PROP so that it will be rendered at the current
18859 position in IT. Return 1 if PROP was successfully pushed, 0
18860 otherwise. Called from handle_line_prefix to handle the
18861 `line-prefix' and `wrap-prefix' properties. */
18863 static int
18864 push_prefix_prop (struct it *it, Lisp_Object prop)
18866 struct text_pos pos =
18867 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18869 eassert (it->method == GET_FROM_BUFFER
18870 || it->method == GET_FROM_DISPLAY_VECTOR
18871 || it->method == GET_FROM_STRING);
18873 /* We need to save the current buffer/string position, so it will be
18874 restored by pop_it, because iterate_out_of_display_property
18875 depends on that being set correctly, but some situations leave
18876 it->position not yet set when this function is called. */
18877 push_it (it, &pos);
18879 if (STRINGP (prop))
18881 if (SCHARS (prop) == 0)
18883 pop_it (it);
18884 return 0;
18887 it->string = prop;
18888 it->string_from_prefix_prop_p = 1;
18889 it->multibyte_p = STRING_MULTIBYTE (it->string);
18890 it->current.overlay_string_index = -1;
18891 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18892 it->end_charpos = it->string_nchars = SCHARS (it->string);
18893 it->method = GET_FROM_STRING;
18894 it->stop_charpos = 0;
18895 it->prev_stop = 0;
18896 it->base_level_stop = 0;
18898 /* Force paragraph direction to be that of the parent
18899 buffer/string. */
18900 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18901 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18902 else
18903 it->paragraph_embedding = L2R;
18905 /* Set up the bidi iterator for this display string. */
18906 if (it->bidi_p)
18908 it->bidi_it.string.lstring = it->string;
18909 it->bidi_it.string.s = NULL;
18910 it->bidi_it.string.schars = it->end_charpos;
18911 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18912 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18913 it->bidi_it.string.unibyte = !it->multibyte_p;
18914 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18917 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18919 it->method = GET_FROM_STRETCH;
18920 it->object = prop;
18922 #ifdef HAVE_WINDOW_SYSTEM
18923 else if (IMAGEP (prop))
18925 it->what = IT_IMAGE;
18926 it->image_id = lookup_image (it->f, prop);
18927 it->method = GET_FROM_IMAGE;
18929 #endif /* HAVE_WINDOW_SYSTEM */
18930 else
18932 pop_it (it); /* bogus display property, give up */
18933 return 0;
18936 return 1;
18939 /* Return the character-property PROP at the current position in IT. */
18941 static Lisp_Object
18942 get_it_property (struct it *it, Lisp_Object prop)
18944 Lisp_Object position;
18946 if (STRINGP (it->object))
18947 position = make_number (IT_STRING_CHARPOS (*it));
18948 else if (BUFFERP (it->object))
18949 position = make_number (IT_CHARPOS (*it));
18950 else
18951 return Qnil;
18953 return Fget_char_property (position, prop, it->object);
18956 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18958 static void
18959 handle_line_prefix (struct it *it)
18961 Lisp_Object prefix;
18963 if (it->continuation_lines_width > 0)
18965 prefix = get_it_property (it, Qwrap_prefix);
18966 if (NILP (prefix))
18967 prefix = Vwrap_prefix;
18969 else
18971 prefix = get_it_property (it, Qline_prefix);
18972 if (NILP (prefix))
18973 prefix = Vline_prefix;
18975 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18977 /* If the prefix is wider than the window, and we try to wrap
18978 it, it would acquire its own wrap prefix, and so on till the
18979 iterator stack overflows. So, don't wrap the prefix. */
18980 it->line_wrap = TRUNCATE;
18981 it->avoid_cursor_p = 1;
18987 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18988 only for R2L lines from display_line and display_string, when they
18989 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18990 the line/string needs to be continued on the next glyph row. */
18991 static void
18992 unproduce_glyphs (struct it *it, int n)
18994 struct glyph *glyph, *end;
18996 eassert (it->glyph_row);
18997 eassert (it->glyph_row->reversed_p);
18998 eassert (it->area == TEXT_AREA);
18999 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19001 if (n > it->glyph_row->used[TEXT_AREA])
19002 n = it->glyph_row->used[TEXT_AREA];
19003 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19004 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19005 for ( ; glyph < end; glyph++)
19006 glyph[-n] = *glyph;
19009 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19010 and ROW->maxpos. */
19011 static void
19012 find_row_edges (struct it *it, struct glyph_row *row,
19013 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19014 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19016 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19017 lines' rows is implemented for bidi-reordered rows. */
19019 /* ROW->minpos is the value of min_pos, the minimal buffer position
19020 we have in ROW, or ROW->start.pos if that is smaller. */
19021 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19022 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19023 else
19024 /* We didn't find buffer positions smaller than ROW->start, or
19025 didn't find _any_ valid buffer positions in any of the glyphs,
19026 so we must trust the iterator's computed positions. */
19027 row->minpos = row->start.pos;
19028 if (max_pos <= 0)
19030 max_pos = CHARPOS (it->current.pos);
19031 max_bpos = BYTEPOS (it->current.pos);
19034 /* Here are the various use-cases for ending the row, and the
19035 corresponding values for ROW->maxpos:
19037 Line ends in a newline from buffer eol_pos + 1
19038 Line is continued from buffer max_pos + 1
19039 Line is truncated on right it->current.pos
19040 Line ends in a newline from string max_pos + 1(*)
19041 (*) + 1 only when line ends in a forward scan
19042 Line is continued from string max_pos
19043 Line is continued from display vector max_pos
19044 Line is entirely from a string min_pos == max_pos
19045 Line is entirely from a display vector min_pos == max_pos
19046 Line that ends at ZV ZV
19048 If you discover other use-cases, please add them here as
19049 appropriate. */
19050 if (row->ends_at_zv_p)
19051 row->maxpos = it->current.pos;
19052 else if (row->used[TEXT_AREA])
19054 int seen_this_string = 0;
19055 struct glyph_row *r1 = row - 1;
19057 /* Did we see the same display string on the previous row? */
19058 if (STRINGP (it->object)
19059 /* this is not the first row */
19060 && row > it->w->desired_matrix->rows
19061 /* previous row is not the header line */
19062 && !r1->mode_line_p
19063 /* previous row also ends in a newline from a string */
19064 && r1->ends_in_newline_from_string_p)
19066 struct glyph *start, *end;
19068 /* Search for the last glyph of the previous row that came
19069 from buffer or string. Depending on whether the row is
19070 L2R or R2L, we need to process it front to back or the
19071 other way round. */
19072 if (!r1->reversed_p)
19074 start = r1->glyphs[TEXT_AREA];
19075 end = start + r1->used[TEXT_AREA];
19076 /* Glyphs inserted by redisplay have an integer (zero)
19077 as their object. */
19078 while (end > start
19079 && INTEGERP ((end - 1)->object)
19080 && (end - 1)->charpos <= 0)
19081 --end;
19082 if (end > start)
19084 if (EQ ((end - 1)->object, it->object))
19085 seen_this_string = 1;
19087 else
19088 /* If all the glyphs of the previous row were inserted
19089 by redisplay, it means the previous row was
19090 produced from a single newline, which is only
19091 possible if that newline came from the same string
19092 as the one which produced this ROW. */
19093 seen_this_string = 1;
19095 else
19097 end = r1->glyphs[TEXT_AREA] - 1;
19098 start = end + r1->used[TEXT_AREA];
19099 while (end < start
19100 && INTEGERP ((end + 1)->object)
19101 && (end + 1)->charpos <= 0)
19102 ++end;
19103 if (end < start)
19105 if (EQ ((end + 1)->object, it->object))
19106 seen_this_string = 1;
19108 else
19109 seen_this_string = 1;
19112 /* Take note of each display string that covers a newline only
19113 once, the first time we see it. This is for when a display
19114 string includes more than one newline in it. */
19115 if (row->ends_in_newline_from_string_p && !seen_this_string)
19117 /* If we were scanning the buffer forward when we displayed
19118 the string, we want to account for at least one buffer
19119 position that belongs to this row (position covered by
19120 the display string), so that cursor positioning will
19121 consider this row as a candidate when point is at the end
19122 of the visual line represented by this row. This is not
19123 required when scanning back, because max_pos will already
19124 have a much larger value. */
19125 if (CHARPOS (row->end.pos) > max_pos)
19126 INC_BOTH (max_pos, max_bpos);
19127 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19129 else if (CHARPOS (it->eol_pos) > 0)
19130 SET_TEXT_POS (row->maxpos,
19131 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19132 else if (row->continued_p)
19134 /* If max_pos is different from IT's current position, it
19135 means IT->method does not belong to the display element
19136 at max_pos. However, it also means that the display
19137 element at max_pos was displayed in its entirety on this
19138 line, which is equivalent to saying that the next line
19139 starts at the next buffer position. */
19140 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19141 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19142 else
19144 INC_BOTH (max_pos, max_bpos);
19145 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19148 else if (row->truncated_on_right_p)
19149 /* display_line already called reseat_at_next_visible_line_start,
19150 which puts the iterator at the beginning of the next line, in
19151 the logical order. */
19152 row->maxpos = it->current.pos;
19153 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19154 /* A line that is entirely from a string/image/stretch... */
19155 row->maxpos = row->minpos;
19156 else
19157 emacs_abort ();
19159 else
19160 row->maxpos = it->current.pos;
19163 /* Construct the glyph row IT->glyph_row in the desired matrix of
19164 IT->w from text at the current position of IT. See dispextern.h
19165 for an overview of struct it. Value is non-zero if
19166 IT->glyph_row displays text, as opposed to a line displaying ZV
19167 only. */
19169 static int
19170 display_line (struct it *it)
19172 struct glyph_row *row = it->glyph_row;
19173 Lisp_Object overlay_arrow_string;
19174 struct it wrap_it;
19175 void *wrap_data = NULL;
19176 int may_wrap = 0, wrap_x IF_LINT (= 0);
19177 int wrap_row_used = -1;
19178 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19179 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19180 int wrap_row_extra_line_spacing IF_LINT (= 0);
19181 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19182 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19183 int cvpos;
19184 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19185 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19187 /* We always start displaying at hpos zero even if hscrolled. */
19188 eassert (it->hpos == 0 && it->current_x == 0);
19190 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19191 >= it->w->desired_matrix->nrows)
19193 it->w->nrows_scale_factor++;
19194 fonts_changed_p = 1;
19195 return 0;
19198 /* Is IT->w showing the region? */
19199 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19201 /* Clear the result glyph row and enable it. */
19202 prepare_desired_row (row);
19204 row->y = it->current_y;
19205 row->start = it->start;
19206 row->continuation_lines_width = it->continuation_lines_width;
19207 row->displays_text_p = 1;
19208 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19209 it->starts_in_middle_of_char_p = 0;
19211 /* Arrange the overlays nicely for our purposes. Usually, we call
19212 display_line on only one line at a time, in which case this
19213 can't really hurt too much, or we call it on lines which appear
19214 one after another in the buffer, in which case all calls to
19215 recenter_overlay_lists but the first will be pretty cheap. */
19216 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19218 /* Move over display elements that are not visible because we are
19219 hscrolled. This may stop at an x-position < IT->first_visible_x
19220 if the first glyph is partially visible or if we hit a line end. */
19221 if (it->current_x < it->first_visible_x)
19223 enum move_it_result move_result;
19225 this_line_min_pos = row->start.pos;
19226 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19227 MOVE_TO_POS | MOVE_TO_X);
19228 /* If we are under a large hscroll, move_it_in_display_line_to
19229 could hit the end of the line without reaching
19230 it->first_visible_x. Pretend that we did reach it. This is
19231 especially important on a TTY, where we will call
19232 extend_face_to_end_of_line, which needs to know how many
19233 blank glyphs to produce. */
19234 if (it->current_x < it->first_visible_x
19235 && (move_result == MOVE_NEWLINE_OR_CR
19236 || move_result == MOVE_POS_MATCH_OR_ZV))
19237 it->current_x = it->first_visible_x;
19239 /* Record the smallest positions seen while we moved over
19240 display elements that are not visible. This is needed by
19241 redisplay_internal for optimizing the case where the cursor
19242 stays inside the same line. The rest of this function only
19243 considers positions that are actually displayed, so
19244 RECORD_MAX_MIN_POS will not otherwise record positions that
19245 are hscrolled to the left of the left edge of the window. */
19246 min_pos = CHARPOS (this_line_min_pos);
19247 min_bpos = BYTEPOS (this_line_min_pos);
19249 else
19251 /* We only do this when not calling `move_it_in_display_line_to'
19252 above, because move_it_in_display_line_to calls
19253 handle_line_prefix itself. */
19254 handle_line_prefix (it);
19257 /* Get the initial row height. This is either the height of the
19258 text hscrolled, if there is any, or zero. */
19259 row->ascent = it->max_ascent;
19260 row->height = it->max_ascent + it->max_descent;
19261 row->phys_ascent = it->max_phys_ascent;
19262 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19263 row->extra_line_spacing = it->max_extra_line_spacing;
19265 /* Utility macro to record max and min buffer positions seen until now. */
19266 #define RECORD_MAX_MIN_POS(IT) \
19267 do \
19269 int composition_p = !STRINGP ((IT)->string) \
19270 && ((IT)->what == IT_COMPOSITION); \
19271 ptrdiff_t current_pos = \
19272 composition_p ? (IT)->cmp_it.charpos \
19273 : IT_CHARPOS (*(IT)); \
19274 ptrdiff_t current_bpos = \
19275 composition_p ? CHAR_TO_BYTE (current_pos) \
19276 : IT_BYTEPOS (*(IT)); \
19277 if (current_pos < min_pos) \
19279 min_pos = current_pos; \
19280 min_bpos = current_bpos; \
19282 if (IT_CHARPOS (*it) > max_pos) \
19284 max_pos = IT_CHARPOS (*it); \
19285 max_bpos = IT_BYTEPOS (*it); \
19288 while (0)
19290 /* Loop generating characters. The loop is left with IT on the next
19291 character to display. */
19292 while (1)
19294 int n_glyphs_before, hpos_before, x_before;
19295 int x, nglyphs;
19296 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19298 /* Retrieve the next thing to display. Value is zero if end of
19299 buffer reached. */
19300 if (!get_next_display_element (it))
19302 /* Maybe add a space at the end of this line that is used to
19303 display the cursor there under X. Set the charpos of the
19304 first glyph of blank lines not corresponding to any text
19305 to -1. */
19306 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19307 row->exact_window_width_line_p = 1;
19308 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19309 || row->used[TEXT_AREA] == 0)
19311 row->glyphs[TEXT_AREA]->charpos = -1;
19312 row->displays_text_p = 0;
19314 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19315 && (!MINI_WINDOW_P (it->w)
19316 || (minibuf_level && EQ (it->window, minibuf_window))))
19317 row->indicate_empty_line_p = 1;
19320 it->continuation_lines_width = 0;
19321 row->ends_at_zv_p = 1;
19322 /* A row that displays right-to-left text must always have
19323 its last face extended all the way to the end of line,
19324 even if this row ends in ZV, because we still write to
19325 the screen left to right. We also need to extend the
19326 last face if the default face is remapped to some
19327 different face, otherwise the functions that clear
19328 portions of the screen will clear with the default face's
19329 background color. */
19330 if (row->reversed_p
19331 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19332 extend_face_to_end_of_line (it);
19333 break;
19336 /* Now, get the metrics of what we want to display. This also
19337 generates glyphs in `row' (which is IT->glyph_row). */
19338 n_glyphs_before = row->used[TEXT_AREA];
19339 x = it->current_x;
19341 /* Remember the line height so far in case the next element doesn't
19342 fit on the line. */
19343 if (it->line_wrap != TRUNCATE)
19345 ascent = it->max_ascent;
19346 descent = it->max_descent;
19347 phys_ascent = it->max_phys_ascent;
19348 phys_descent = it->max_phys_descent;
19350 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19352 if (IT_DISPLAYING_WHITESPACE (it))
19353 may_wrap = 1;
19354 else if (may_wrap)
19356 SAVE_IT (wrap_it, *it, wrap_data);
19357 wrap_x = x;
19358 wrap_row_used = row->used[TEXT_AREA];
19359 wrap_row_ascent = row->ascent;
19360 wrap_row_height = row->height;
19361 wrap_row_phys_ascent = row->phys_ascent;
19362 wrap_row_phys_height = row->phys_height;
19363 wrap_row_extra_line_spacing = row->extra_line_spacing;
19364 wrap_row_min_pos = min_pos;
19365 wrap_row_min_bpos = min_bpos;
19366 wrap_row_max_pos = max_pos;
19367 wrap_row_max_bpos = max_bpos;
19368 may_wrap = 0;
19373 PRODUCE_GLYPHS (it);
19375 /* If this display element was in marginal areas, continue with
19376 the next one. */
19377 if (it->area != TEXT_AREA)
19379 row->ascent = max (row->ascent, it->max_ascent);
19380 row->height = max (row->height, it->max_ascent + it->max_descent);
19381 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19382 row->phys_height = max (row->phys_height,
19383 it->max_phys_ascent + it->max_phys_descent);
19384 row->extra_line_spacing = max (row->extra_line_spacing,
19385 it->max_extra_line_spacing);
19386 set_iterator_to_next (it, 1);
19387 continue;
19390 /* Does the display element fit on the line? If we truncate
19391 lines, we should draw past the right edge of the window. If
19392 we don't truncate, we want to stop so that we can display the
19393 continuation glyph before the right margin. If lines are
19394 continued, there are two possible strategies for characters
19395 resulting in more than 1 glyph (e.g. tabs): Display as many
19396 glyphs as possible in this line and leave the rest for the
19397 continuation line, or display the whole element in the next
19398 line. Original redisplay did the former, so we do it also. */
19399 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19400 hpos_before = it->hpos;
19401 x_before = x;
19403 if (/* Not a newline. */
19404 nglyphs > 0
19405 /* Glyphs produced fit entirely in the line. */
19406 && it->current_x < it->last_visible_x)
19408 it->hpos += nglyphs;
19409 row->ascent = max (row->ascent, it->max_ascent);
19410 row->height = max (row->height, it->max_ascent + it->max_descent);
19411 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19412 row->phys_height = max (row->phys_height,
19413 it->max_phys_ascent + it->max_phys_descent);
19414 row->extra_line_spacing = max (row->extra_line_spacing,
19415 it->max_extra_line_spacing);
19416 if (it->current_x - it->pixel_width < it->first_visible_x)
19417 row->x = x - it->first_visible_x;
19418 /* Record the maximum and minimum buffer positions seen so
19419 far in glyphs that will be displayed by this row. */
19420 if (it->bidi_p)
19421 RECORD_MAX_MIN_POS (it);
19423 else
19425 int i, new_x;
19426 struct glyph *glyph;
19428 for (i = 0; i < nglyphs; ++i, x = new_x)
19430 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19431 new_x = x + glyph->pixel_width;
19433 if (/* Lines are continued. */
19434 it->line_wrap != TRUNCATE
19435 && (/* Glyph doesn't fit on the line. */
19436 new_x > it->last_visible_x
19437 /* Or it fits exactly on a window system frame. */
19438 || (new_x == it->last_visible_x
19439 && FRAME_WINDOW_P (it->f)
19440 && (row->reversed_p
19441 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19442 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19444 /* End of a continued line. */
19446 if (it->hpos == 0
19447 || (new_x == it->last_visible_x
19448 && FRAME_WINDOW_P (it->f)
19449 && (row->reversed_p
19450 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19451 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19453 /* Current glyph is the only one on the line or
19454 fits exactly on the line. We must continue
19455 the line because we can't draw the cursor
19456 after the glyph. */
19457 row->continued_p = 1;
19458 it->current_x = new_x;
19459 it->continuation_lines_width += new_x;
19460 ++it->hpos;
19461 if (i == nglyphs - 1)
19463 /* If line-wrap is on, check if a previous
19464 wrap point was found. */
19465 if (wrap_row_used > 0
19466 /* Even if there is a previous wrap
19467 point, continue the line here as
19468 usual, if (i) the previous character
19469 was a space or tab AND (ii) the
19470 current character is not. */
19471 && (!may_wrap
19472 || IT_DISPLAYING_WHITESPACE (it)))
19473 goto back_to_wrap;
19475 /* Record the maximum and minimum buffer
19476 positions seen so far in glyphs that will be
19477 displayed by this row. */
19478 if (it->bidi_p)
19479 RECORD_MAX_MIN_POS (it);
19480 set_iterator_to_next (it, 1);
19481 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19483 if (!get_next_display_element (it))
19485 row->exact_window_width_line_p = 1;
19486 it->continuation_lines_width = 0;
19487 row->continued_p = 0;
19488 row->ends_at_zv_p = 1;
19490 else if (ITERATOR_AT_END_OF_LINE_P (it))
19492 row->continued_p = 0;
19493 row->exact_window_width_line_p = 1;
19497 else if (it->bidi_p)
19498 RECORD_MAX_MIN_POS (it);
19500 else if (CHAR_GLYPH_PADDING_P (*glyph)
19501 && !FRAME_WINDOW_P (it->f))
19503 /* A padding glyph that doesn't fit on this line.
19504 This means the whole character doesn't fit
19505 on the line. */
19506 if (row->reversed_p)
19507 unproduce_glyphs (it, row->used[TEXT_AREA]
19508 - n_glyphs_before);
19509 row->used[TEXT_AREA] = n_glyphs_before;
19511 /* Fill the rest of the row with continuation
19512 glyphs like in 20.x. */
19513 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19514 < row->glyphs[1 + TEXT_AREA])
19515 produce_special_glyphs (it, IT_CONTINUATION);
19517 row->continued_p = 1;
19518 it->current_x = x_before;
19519 it->continuation_lines_width += x_before;
19521 /* Restore the height to what it was before the
19522 element not fitting on the line. */
19523 it->max_ascent = ascent;
19524 it->max_descent = descent;
19525 it->max_phys_ascent = phys_ascent;
19526 it->max_phys_descent = phys_descent;
19528 else if (wrap_row_used > 0)
19530 back_to_wrap:
19531 if (row->reversed_p)
19532 unproduce_glyphs (it,
19533 row->used[TEXT_AREA] - wrap_row_used);
19534 RESTORE_IT (it, &wrap_it, wrap_data);
19535 it->continuation_lines_width += wrap_x;
19536 row->used[TEXT_AREA] = wrap_row_used;
19537 row->ascent = wrap_row_ascent;
19538 row->height = wrap_row_height;
19539 row->phys_ascent = wrap_row_phys_ascent;
19540 row->phys_height = wrap_row_phys_height;
19541 row->extra_line_spacing = wrap_row_extra_line_spacing;
19542 min_pos = wrap_row_min_pos;
19543 min_bpos = wrap_row_min_bpos;
19544 max_pos = wrap_row_max_pos;
19545 max_bpos = wrap_row_max_bpos;
19546 row->continued_p = 1;
19547 row->ends_at_zv_p = 0;
19548 row->exact_window_width_line_p = 0;
19549 it->continuation_lines_width += x;
19551 /* Make sure that a non-default face is extended
19552 up to the right margin of the window. */
19553 extend_face_to_end_of_line (it);
19555 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19557 /* A TAB that extends past the right edge of the
19558 window. This produces a single glyph on
19559 window system frames. We leave the glyph in
19560 this row and let it fill the row, but don't
19561 consume the TAB. */
19562 if ((row->reversed_p
19563 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19564 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19565 produce_special_glyphs (it, IT_CONTINUATION);
19566 it->continuation_lines_width += it->last_visible_x;
19567 row->ends_in_middle_of_char_p = 1;
19568 row->continued_p = 1;
19569 glyph->pixel_width = it->last_visible_x - x;
19570 it->starts_in_middle_of_char_p = 1;
19572 else
19574 /* Something other than a TAB that draws past
19575 the right edge of the window. Restore
19576 positions to values before the element. */
19577 if (row->reversed_p)
19578 unproduce_glyphs (it, row->used[TEXT_AREA]
19579 - (n_glyphs_before + i));
19580 row->used[TEXT_AREA] = n_glyphs_before + i;
19582 /* Display continuation glyphs. */
19583 it->current_x = x_before;
19584 it->continuation_lines_width += x;
19585 if (!FRAME_WINDOW_P (it->f)
19586 || (row->reversed_p
19587 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19588 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19589 produce_special_glyphs (it, IT_CONTINUATION);
19590 row->continued_p = 1;
19592 extend_face_to_end_of_line (it);
19594 if (nglyphs > 1 && i > 0)
19596 row->ends_in_middle_of_char_p = 1;
19597 it->starts_in_middle_of_char_p = 1;
19600 /* Restore the height to what it was before the
19601 element not fitting on the line. */
19602 it->max_ascent = ascent;
19603 it->max_descent = descent;
19604 it->max_phys_ascent = phys_ascent;
19605 it->max_phys_descent = phys_descent;
19608 break;
19610 else if (new_x > it->first_visible_x)
19612 /* Increment number of glyphs actually displayed. */
19613 ++it->hpos;
19615 /* Record the maximum and minimum buffer positions
19616 seen so far in glyphs that will be displayed by
19617 this row. */
19618 if (it->bidi_p)
19619 RECORD_MAX_MIN_POS (it);
19621 if (x < it->first_visible_x)
19622 /* Glyph is partially visible, i.e. row starts at
19623 negative X position. */
19624 row->x = x - it->first_visible_x;
19626 else
19628 /* Glyph is completely off the left margin of the
19629 window. This should not happen because of the
19630 move_it_in_display_line at the start of this
19631 function, unless the text display area of the
19632 window is empty. */
19633 eassert (it->first_visible_x <= it->last_visible_x);
19636 /* Even if this display element produced no glyphs at all,
19637 we want to record its position. */
19638 if (it->bidi_p && nglyphs == 0)
19639 RECORD_MAX_MIN_POS (it);
19641 row->ascent = max (row->ascent, it->max_ascent);
19642 row->height = max (row->height, it->max_ascent + it->max_descent);
19643 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19644 row->phys_height = max (row->phys_height,
19645 it->max_phys_ascent + it->max_phys_descent);
19646 row->extra_line_spacing = max (row->extra_line_spacing,
19647 it->max_extra_line_spacing);
19649 /* End of this display line if row is continued. */
19650 if (row->continued_p || row->ends_at_zv_p)
19651 break;
19654 at_end_of_line:
19655 /* Is this a line end? If yes, we're also done, after making
19656 sure that a non-default face is extended up to the right
19657 margin of the window. */
19658 if (ITERATOR_AT_END_OF_LINE_P (it))
19660 int used_before = row->used[TEXT_AREA];
19662 row->ends_in_newline_from_string_p = STRINGP (it->object);
19664 /* Add a space at the end of the line that is used to
19665 display the cursor there. */
19666 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19667 append_space_for_newline (it, 0);
19669 /* Extend the face to the end of the line. */
19670 extend_face_to_end_of_line (it);
19672 /* Make sure we have the position. */
19673 if (used_before == 0)
19674 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19676 /* Record the position of the newline, for use in
19677 find_row_edges. */
19678 it->eol_pos = it->current.pos;
19680 /* Consume the line end. This skips over invisible lines. */
19681 set_iterator_to_next (it, 1);
19682 it->continuation_lines_width = 0;
19683 break;
19686 /* Proceed with next display element. Note that this skips
19687 over lines invisible because of selective display. */
19688 set_iterator_to_next (it, 1);
19690 /* If we truncate lines, we are done when the last displayed
19691 glyphs reach past the right margin of the window. */
19692 if (it->line_wrap == TRUNCATE
19693 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19694 ? (it->current_x >= it->last_visible_x)
19695 : (it->current_x > it->last_visible_x)))
19697 /* Maybe add truncation glyphs. */
19698 if (!FRAME_WINDOW_P (it->f)
19699 || (row->reversed_p
19700 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19701 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19703 int i, n;
19705 if (!row->reversed_p)
19707 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19708 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19709 break;
19711 else
19713 for (i = 0; i < row->used[TEXT_AREA]; i++)
19714 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19715 break;
19716 /* Remove any padding glyphs at the front of ROW, to
19717 make room for the truncation glyphs we will be
19718 adding below. The loop below always inserts at
19719 least one truncation glyph, so also remove the
19720 last glyph added to ROW. */
19721 unproduce_glyphs (it, i + 1);
19722 /* Adjust i for the loop below. */
19723 i = row->used[TEXT_AREA] - (i + 1);
19726 it->current_x = x_before;
19727 if (!FRAME_WINDOW_P (it->f))
19729 for (n = row->used[TEXT_AREA]; i < n; ++i)
19731 row->used[TEXT_AREA] = i;
19732 produce_special_glyphs (it, IT_TRUNCATION);
19735 else
19737 row->used[TEXT_AREA] = i;
19738 produce_special_glyphs (it, IT_TRUNCATION);
19741 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19743 /* Don't truncate if we can overflow newline into fringe. */
19744 if (!get_next_display_element (it))
19746 it->continuation_lines_width = 0;
19747 row->ends_at_zv_p = 1;
19748 row->exact_window_width_line_p = 1;
19749 break;
19751 if (ITERATOR_AT_END_OF_LINE_P (it))
19753 row->exact_window_width_line_p = 1;
19754 goto at_end_of_line;
19756 it->current_x = x_before;
19759 row->truncated_on_right_p = 1;
19760 it->continuation_lines_width = 0;
19761 reseat_at_next_visible_line_start (it, 0);
19762 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19763 it->hpos = hpos_before;
19764 break;
19768 if (wrap_data)
19769 bidi_unshelve_cache (wrap_data, 1);
19771 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19772 at the left window margin. */
19773 if (it->first_visible_x
19774 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19776 if (!FRAME_WINDOW_P (it->f)
19777 || (row->reversed_p
19778 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19779 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19780 insert_left_trunc_glyphs (it);
19781 row->truncated_on_left_p = 1;
19784 /* Remember the position at which this line ends.
19786 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19787 cannot be before the call to find_row_edges below, since that is
19788 where these positions are determined. */
19789 row->end = it->current;
19790 if (!it->bidi_p)
19792 row->minpos = row->start.pos;
19793 row->maxpos = row->end.pos;
19795 else
19797 /* ROW->minpos and ROW->maxpos must be the smallest and
19798 `1 + the largest' buffer positions in ROW. But if ROW was
19799 bidi-reordered, these two positions can be anywhere in the
19800 row, so we must determine them now. */
19801 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19804 /* If the start of this line is the overlay arrow-position, then
19805 mark this glyph row as the one containing the overlay arrow.
19806 This is clearly a mess with variable size fonts. It would be
19807 better to let it be displayed like cursors under X. */
19808 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19809 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19810 !NILP (overlay_arrow_string)))
19812 /* Overlay arrow in window redisplay is a fringe bitmap. */
19813 if (STRINGP (overlay_arrow_string))
19815 struct glyph_row *arrow_row
19816 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19817 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19818 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19819 struct glyph *p = row->glyphs[TEXT_AREA];
19820 struct glyph *p2, *end;
19822 /* Copy the arrow glyphs. */
19823 while (glyph < arrow_end)
19824 *p++ = *glyph++;
19826 /* Throw away padding glyphs. */
19827 p2 = p;
19828 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19829 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19830 ++p2;
19831 if (p2 > p)
19833 while (p2 < end)
19834 *p++ = *p2++;
19835 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19838 else
19840 eassert (INTEGERP (overlay_arrow_string));
19841 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19843 overlay_arrow_seen = 1;
19846 /* Highlight trailing whitespace. */
19847 if (!NILP (Vshow_trailing_whitespace))
19848 highlight_trailing_whitespace (it->f, it->glyph_row);
19850 /* Compute pixel dimensions of this line. */
19851 compute_line_metrics (it);
19853 /* Implementation note: No changes in the glyphs of ROW or in their
19854 faces can be done past this point, because compute_line_metrics
19855 computes ROW's hash value and stores it within the glyph_row
19856 structure. */
19858 /* Record whether this row ends inside an ellipsis. */
19859 row->ends_in_ellipsis_p
19860 = (it->method == GET_FROM_DISPLAY_VECTOR
19861 && it->ellipsis_p);
19863 /* Save fringe bitmaps in this row. */
19864 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19865 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19866 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19867 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19869 it->left_user_fringe_bitmap = 0;
19870 it->left_user_fringe_face_id = 0;
19871 it->right_user_fringe_bitmap = 0;
19872 it->right_user_fringe_face_id = 0;
19874 /* Maybe set the cursor. */
19875 cvpos = it->w->cursor.vpos;
19876 if ((cvpos < 0
19877 /* In bidi-reordered rows, keep checking for proper cursor
19878 position even if one has been found already, because buffer
19879 positions in such rows change non-linearly with ROW->VPOS,
19880 when a line is continued. One exception: when we are at ZV,
19881 display cursor on the first suitable glyph row, since all
19882 the empty rows after that also have their position set to ZV. */
19883 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19884 lines' rows is implemented for bidi-reordered rows. */
19885 || (it->bidi_p
19886 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19887 && PT >= MATRIX_ROW_START_CHARPOS (row)
19888 && PT <= MATRIX_ROW_END_CHARPOS (row)
19889 && cursor_row_p (row))
19890 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19892 /* Prepare for the next line. This line starts horizontally at (X
19893 HPOS) = (0 0). Vertical positions are incremented. As a
19894 convenience for the caller, IT->glyph_row is set to the next
19895 row to be used. */
19896 it->current_x = it->hpos = 0;
19897 it->current_y += row->height;
19898 SET_TEXT_POS (it->eol_pos, 0, 0);
19899 ++it->vpos;
19900 ++it->glyph_row;
19901 /* The next row should by default use the same value of the
19902 reversed_p flag as this one. set_iterator_to_next decides when
19903 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19904 the flag accordingly. */
19905 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19906 it->glyph_row->reversed_p = row->reversed_p;
19907 it->start = row->end;
19908 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19910 #undef RECORD_MAX_MIN_POS
19913 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19914 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19915 doc: /* Return paragraph direction at point in BUFFER.
19916 Value is either `left-to-right' or `right-to-left'.
19917 If BUFFER is omitted or nil, it defaults to the current buffer.
19919 Paragraph direction determines how the text in the paragraph is displayed.
19920 In left-to-right paragraphs, text begins at the left margin of the window
19921 and the reading direction is generally left to right. In right-to-left
19922 paragraphs, text begins at the right margin and is read from right to left.
19924 See also `bidi-paragraph-direction'. */)
19925 (Lisp_Object buffer)
19927 struct buffer *buf = current_buffer;
19928 struct buffer *old = buf;
19930 if (! NILP (buffer))
19932 CHECK_BUFFER (buffer);
19933 buf = XBUFFER (buffer);
19936 if (NILP (BVAR (buf, bidi_display_reordering))
19937 || NILP (BVAR (buf, enable_multibyte_characters))
19938 /* When we are loading loadup.el, the character property tables
19939 needed for bidi iteration are not yet available. */
19940 || !NILP (Vpurify_flag))
19941 return Qleft_to_right;
19942 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19943 return BVAR (buf, bidi_paragraph_direction);
19944 else
19946 /* Determine the direction from buffer text. We could try to
19947 use current_matrix if it is up to date, but this seems fast
19948 enough as it is. */
19949 struct bidi_it itb;
19950 ptrdiff_t pos = BUF_PT (buf);
19951 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19952 int c;
19953 void *itb_data = bidi_shelve_cache ();
19955 set_buffer_temp (buf);
19956 /* bidi_paragraph_init finds the base direction of the paragraph
19957 by searching forward from paragraph start. We need the base
19958 direction of the current or _previous_ paragraph, so we need
19959 to make sure we are within that paragraph. To that end, find
19960 the previous non-empty line. */
19961 if (pos >= ZV && pos > BEGV)
19962 DEC_BOTH (pos, bytepos);
19963 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19964 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19966 while ((c = FETCH_BYTE (bytepos)) == '\n'
19967 || c == ' ' || c == '\t' || c == '\f')
19969 if (bytepos <= BEGV_BYTE)
19970 break;
19971 bytepos--;
19972 pos--;
19974 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19975 bytepos--;
19977 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19978 itb.paragraph_dir = NEUTRAL_DIR;
19979 itb.string.s = NULL;
19980 itb.string.lstring = Qnil;
19981 itb.string.bufpos = 0;
19982 itb.string.unibyte = 0;
19983 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19984 bidi_unshelve_cache (itb_data, 0);
19985 set_buffer_temp (old);
19986 switch (itb.paragraph_dir)
19988 case L2R:
19989 return Qleft_to_right;
19990 break;
19991 case R2L:
19992 return Qright_to_left;
19993 break;
19994 default:
19995 emacs_abort ();
20002 /***********************************************************************
20003 Menu Bar
20004 ***********************************************************************/
20006 /* Redisplay the menu bar in the frame for window W.
20008 The menu bar of X frames that don't have X toolkit support is
20009 displayed in a special window W->frame->menu_bar_window.
20011 The menu bar of terminal frames is treated specially as far as
20012 glyph matrices are concerned. Menu bar lines are not part of
20013 windows, so the update is done directly on the frame matrix rows
20014 for the menu bar. */
20016 static void
20017 display_menu_bar (struct window *w)
20019 struct frame *f = XFRAME (WINDOW_FRAME (w));
20020 struct it it;
20021 Lisp_Object items;
20022 int i;
20024 /* Don't do all this for graphical frames. */
20025 #ifdef HAVE_NTGUI
20026 if (FRAME_W32_P (f))
20027 return;
20028 #endif
20029 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20030 if (FRAME_X_P (f))
20031 return;
20032 #endif
20034 #ifdef HAVE_NS
20035 if (FRAME_NS_P (f))
20036 return;
20037 #endif /* HAVE_NS */
20039 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20040 eassert (!FRAME_WINDOW_P (f));
20041 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20042 it.first_visible_x = 0;
20043 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20044 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20045 if (FRAME_WINDOW_P (f))
20047 /* Menu bar lines are displayed in the desired matrix of the
20048 dummy window menu_bar_window. */
20049 struct window *menu_w;
20050 menu_w = XWINDOW (f->menu_bar_window);
20051 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20052 MENU_FACE_ID);
20053 it.first_visible_x = 0;
20054 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20056 else
20057 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20059 /* This is a TTY frame, i.e. character hpos/vpos are used as
20060 pixel x/y. */
20061 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20062 MENU_FACE_ID);
20063 it.first_visible_x = 0;
20064 it.last_visible_x = FRAME_COLS (f);
20067 /* FIXME: This should be controlled by a user option. See the
20068 comments in redisplay_tool_bar and display_mode_line about
20069 this. */
20070 it.paragraph_embedding = L2R;
20072 /* Clear all rows of the menu bar. */
20073 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20075 struct glyph_row *row = it.glyph_row + i;
20076 clear_glyph_row (row);
20077 row->enabled_p = 1;
20078 row->full_width_p = 1;
20081 /* Display all items of the menu bar. */
20082 items = FRAME_MENU_BAR_ITEMS (it.f);
20083 for (i = 0; i < ASIZE (items); i += 4)
20085 Lisp_Object string;
20087 /* Stop at nil string. */
20088 string = AREF (items, i + 1);
20089 if (NILP (string))
20090 break;
20092 /* Remember where item was displayed. */
20093 ASET (items, i + 3, make_number (it.hpos));
20095 /* Display the item, pad with one space. */
20096 if (it.current_x < it.last_visible_x)
20097 display_string (NULL, string, Qnil, 0, 0, &it,
20098 SCHARS (string) + 1, 0, 0, -1);
20101 /* Fill out the line with spaces. */
20102 if (it.current_x < it.last_visible_x)
20103 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20105 /* Compute the total height of the lines. */
20106 compute_line_metrics (&it);
20111 /***********************************************************************
20112 Mode Line
20113 ***********************************************************************/
20115 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20116 FORCE is non-zero, redisplay mode lines unconditionally.
20117 Otherwise, redisplay only mode lines that are garbaged. Value is
20118 the number of windows whose mode lines were redisplayed. */
20120 static int
20121 redisplay_mode_lines (Lisp_Object window, int force)
20123 int nwindows = 0;
20125 while (!NILP (window))
20127 struct window *w = XWINDOW (window);
20129 if (WINDOWP (w->contents))
20130 nwindows += redisplay_mode_lines (w->contents, force);
20131 else if (force
20132 || FRAME_GARBAGED_P (XFRAME (w->frame))
20133 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20135 struct text_pos lpoint;
20136 struct buffer *old = current_buffer;
20138 /* Set the window's buffer for the mode line display. */
20139 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20140 set_buffer_internal_1 (XBUFFER (w->contents));
20142 /* Point refers normally to the selected window. For any
20143 other window, set up appropriate value. */
20144 if (!EQ (window, selected_window))
20146 struct text_pos pt;
20148 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20149 if (CHARPOS (pt) < BEGV)
20150 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20151 else if (CHARPOS (pt) > (ZV - 1))
20152 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20153 else
20154 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20157 /* Display mode lines. */
20158 clear_glyph_matrix (w->desired_matrix);
20159 if (display_mode_lines (w))
20161 ++nwindows;
20162 w->must_be_updated_p = 1;
20165 /* Restore old settings. */
20166 set_buffer_internal_1 (old);
20167 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20170 window = w->next;
20173 return nwindows;
20177 /* Display the mode and/or header line of window W. Value is the
20178 sum number of mode lines and header lines displayed. */
20180 static int
20181 display_mode_lines (struct window *w)
20183 Lisp_Object old_selected_window = selected_window;
20184 Lisp_Object old_selected_frame = selected_frame;
20185 Lisp_Object new_frame = w->frame;
20186 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20187 int n = 0;
20189 selected_frame = new_frame;
20190 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20191 or window's point, then we'd need select_window_1 here as well. */
20192 XSETWINDOW (selected_window, w);
20193 XFRAME (new_frame)->selected_window = selected_window;
20195 /* These will be set while the mode line specs are processed. */
20196 line_number_displayed = 0;
20197 w->column_number_displayed = -1;
20199 if (WINDOW_WANTS_MODELINE_P (w))
20201 struct window *sel_w = XWINDOW (old_selected_window);
20203 /* Select mode line face based on the real selected window. */
20204 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20205 BVAR (current_buffer, mode_line_format));
20206 ++n;
20209 if (WINDOW_WANTS_HEADER_LINE_P (w))
20211 display_mode_line (w, HEADER_LINE_FACE_ID,
20212 BVAR (current_buffer, header_line_format));
20213 ++n;
20216 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20217 selected_frame = old_selected_frame;
20218 selected_window = old_selected_window;
20219 return n;
20223 /* Display mode or header line of window W. FACE_ID specifies which
20224 line to display; it is either MODE_LINE_FACE_ID or
20225 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20226 display. Value is the pixel height of the mode/header line
20227 displayed. */
20229 static int
20230 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20232 struct it it;
20233 struct face *face;
20234 ptrdiff_t count = SPECPDL_INDEX ();
20236 init_iterator (&it, w, -1, -1, NULL, face_id);
20237 /* Don't extend on a previously drawn mode-line.
20238 This may happen if called from pos_visible_p. */
20239 it.glyph_row->enabled_p = 0;
20240 prepare_desired_row (it.glyph_row);
20242 it.glyph_row->mode_line_p = 1;
20244 /* FIXME: This should be controlled by a user option. But
20245 supporting such an option is not trivial, since the mode line is
20246 made up of many separate strings. */
20247 it.paragraph_embedding = L2R;
20249 record_unwind_protect (unwind_format_mode_line,
20250 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20252 mode_line_target = MODE_LINE_DISPLAY;
20254 /* Temporarily make frame's keyboard the current kboard so that
20255 kboard-local variables in the mode_line_format will get the right
20256 values. */
20257 push_kboard (FRAME_KBOARD (it.f));
20258 record_unwind_save_match_data ();
20259 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20260 pop_kboard ();
20262 unbind_to (count, Qnil);
20264 /* Fill up with spaces. */
20265 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20267 compute_line_metrics (&it);
20268 it.glyph_row->full_width_p = 1;
20269 it.glyph_row->continued_p = 0;
20270 it.glyph_row->truncated_on_left_p = 0;
20271 it.glyph_row->truncated_on_right_p = 0;
20273 /* Make a 3D mode-line have a shadow at its right end. */
20274 face = FACE_FROM_ID (it.f, face_id);
20275 extend_face_to_end_of_line (&it);
20276 if (face->box != FACE_NO_BOX)
20278 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20279 + it.glyph_row->used[TEXT_AREA] - 1);
20280 last->right_box_line_p = 1;
20283 return it.glyph_row->height;
20286 /* Move element ELT in LIST to the front of LIST.
20287 Return the updated list. */
20289 static Lisp_Object
20290 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20292 register Lisp_Object tail, prev;
20293 register Lisp_Object tem;
20295 tail = list;
20296 prev = Qnil;
20297 while (CONSP (tail))
20299 tem = XCAR (tail);
20301 if (EQ (elt, tem))
20303 /* Splice out the link TAIL. */
20304 if (NILP (prev))
20305 list = XCDR (tail);
20306 else
20307 Fsetcdr (prev, XCDR (tail));
20309 /* Now make it the first. */
20310 Fsetcdr (tail, list);
20311 return tail;
20313 else
20314 prev = tail;
20315 tail = XCDR (tail);
20316 QUIT;
20319 /* Not found--return unchanged LIST. */
20320 return list;
20323 /* Contribute ELT to the mode line for window IT->w. How it
20324 translates into text depends on its data type.
20326 IT describes the display environment in which we display, as usual.
20328 DEPTH is the depth in recursion. It is used to prevent
20329 infinite recursion here.
20331 FIELD_WIDTH is the number of characters the display of ELT should
20332 occupy in the mode line, and PRECISION is the maximum number of
20333 characters to display from ELT's representation. See
20334 display_string for details.
20336 Returns the hpos of the end of the text generated by ELT.
20338 PROPS is a property list to add to any string we encounter.
20340 If RISKY is nonzero, remove (disregard) any properties in any string
20341 we encounter, and ignore :eval and :propertize.
20343 The global variable `mode_line_target' determines whether the
20344 output is passed to `store_mode_line_noprop',
20345 `store_mode_line_string', or `display_string'. */
20347 static int
20348 display_mode_element (struct it *it, int depth, int field_width, int precision,
20349 Lisp_Object elt, Lisp_Object props, int risky)
20351 int n = 0, field, prec;
20352 int literal = 0;
20354 tail_recurse:
20355 if (depth > 100)
20356 elt = build_string ("*too-deep*");
20358 depth++;
20360 switch (XTYPE (elt))
20362 case Lisp_String:
20364 /* A string: output it and check for %-constructs within it. */
20365 unsigned char c;
20366 ptrdiff_t offset = 0;
20368 if (SCHARS (elt) > 0
20369 && (!NILP (props) || risky))
20371 Lisp_Object oprops, aelt;
20372 oprops = Ftext_properties_at (make_number (0), elt);
20374 /* If the starting string's properties are not what
20375 we want, translate the string. Also, if the string
20376 is risky, do that anyway. */
20378 if (NILP (Fequal (props, oprops)) || risky)
20380 /* If the starting string has properties,
20381 merge the specified ones onto the existing ones. */
20382 if (! NILP (oprops) && !risky)
20384 Lisp_Object tem;
20386 oprops = Fcopy_sequence (oprops);
20387 tem = props;
20388 while (CONSP (tem))
20390 oprops = Fplist_put (oprops, XCAR (tem),
20391 XCAR (XCDR (tem)));
20392 tem = XCDR (XCDR (tem));
20394 props = oprops;
20397 aelt = Fassoc (elt, mode_line_proptrans_alist);
20398 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20400 /* AELT is what we want. Move it to the front
20401 without consing. */
20402 elt = XCAR (aelt);
20403 mode_line_proptrans_alist
20404 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20406 else
20408 Lisp_Object tem;
20410 /* If AELT has the wrong props, it is useless.
20411 so get rid of it. */
20412 if (! NILP (aelt))
20413 mode_line_proptrans_alist
20414 = Fdelq (aelt, mode_line_proptrans_alist);
20416 elt = Fcopy_sequence (elt);
20417 Fset_text_properties (make_number (0), Flength (elt),
20418 props, elt);
20419 /* Add this item to mode_line_proptrans_alist. */
20420 mode_line_proptrans_alist
20421 = Fcons (Fcons (elt, props),
20422 mode_line_proptrans_alist);
20423 /* Truncate mode_line_proptrans_alist
20424 to at most 50 elements. */
20425 tem = Fnthcdr (make_number (50),
20426 mode_line_proptrans_alist);
20427 if (! NILP (tem))
20428 XSETCDR (tem, Qnil);
20433 offset = 0;
20435 if (literal)
20437 prec = precision - n;
20438 switch (mode_line_target)
20440 case MODE_LINE_NOPROP:
20441 case MODE_LINE_TITLE:
20442 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20443 break;
20444 case MODE_LINE_STRING:
20445 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20446 break;
20447 case MODE_LINE_DISPLAY:
20448 n += display_string (NULL, elt, Qnil, 0, 0, it,
20449 0, prec, 0, STRING_MULTIBYTE (elt));
20450 break;
20453 break;
20456 /* Handle the non-literal case. */
20458 while ((precision <= 0 || n < precision)
20459 && SREF (elt, offset) != 0
20460 && (mode_line_target != MODE_LINE_DISPLAY
20461 || it->current_x < it->last_visible_x))
20463 ptrdiff_t last_offset = offset;
20465 /* Advance to end of string or next format specifier. */
20466 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20469 if (offset - 1 != last_offset)
20471 ptrdiff_t nchars, nbytes;
20473 /* Output to end of string or up to '%'. Field width
20474 is length of string. Don't output more than
20475 PRECISION allows us. */
20476 offset--;
20478 prec = c_string_width (SDATA (elt) + last_offset,
20479 offset - last_offset, precision - n,
20480 &nchars, &nbytes);
20482 switch (mode_line_target)
20484 case MODE_LINE_NOPROP:
20485 case MODE_LINE_TITLE:
20486 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20487 break;
20488 case MODE_LINE_STRING:
20490 ptrdiff_t bytepos = last_offset;
20491 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20492 ptrdiff_t endpos = (precision <= 0
20493 ? string_byte_to_char (elt, offset)
20494 : charpos + nchars);
20496 n += store_mode_line_string (NULL,
20497 Fsubstring (elt, make_number (charpos),
20498 make_number (endpos)),
20499 0, 0, 0, Qnil);
20501 break;
20502 case MODE_LINE_DISPLAY:
20504 ptrdiff_t bytepos = last_offset;
20505 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20507 if (precision <= 0)
20508 nchars = string_byte_to_char (elt, offset) - charpos;
20509 n += display_string (NULL, elt, Qnil, 0, charpos,
20510 it, 0, nchars, 0,
20511 STRING_MULTIBYTE (elt));
20513 break;
20516 else /* c == '%' */
20518 ptrdiff_t percent_position = offset;
20520 /* Get the specified minimum width. Zero means
20521 don't pad. */
20522 field = 0;
20523 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20524 field = field * 10 + c - '0';
20526 /* Don't pad beyond the total padding allowed. */
20527 if (field_width - n > 0 && field > field_width - n)
20528 field = field_width - n;
20530 /* Note that either PRECISION <= 0 or N < PRECISION. */
20531 prec = precision - n;
20533 if (c == 'M')
20534 n += display_mode_element (it, depth, field, prec,
20535 Vglobal_mode_string, props,
20536 risky);
20537 else if (c != 0)
20539 bool multibyte;
20540 ptrdiff_t bytepos, charpos;
20541 const char *spec;
20542 Lisp_Object string;
20544 bytepos = percent_position;
20545 charpos = (STRING_MULTIBYTE (elt)
20546 ? string_byte_to_char (elt, bytepos)
20547 : bytepos);
20548 spec = decode_mode_spec (it->w, c, field, &string);
20549 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20551 switch (mode_line_target)
20553 case MODE_LINE_NOPROP:
20554 case MODE_LINE_TITLE:
20555 n += store_mode_line_noprop (spec, field, prec);
20556 break;
20557 case MODE_LINE_STRING:
20559 Lisp_Object tem = build_string (spec);
20560 props = Ftext_properties_at (make_number (charpos), elt);
20561 /* Should only keep face property in props */
20562 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20564 break;
20565 case MODE_LINE_DISPLAY:
20567 int nglyphs_before, nwritten;
20569 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20570 nwritten = display_string (spec, string, elt,
20571 charpos, 0, it,
20572 field, prec, 0,
20573 multibyte);
20575 /* Assign to the glyphs written above the
20576 string where the `%x' came from, position
20577 of the `%'. */
20578 if (nwritten > 0)
20580 struct glyph *glyph
20581 = (it->glyph_row->glyphs[TEXT_AREA]
20582 + nglyphs_before);
20583 int i;
20585 for (i = 0; i < nwritten; ++i)
20587 glyph[i].object = elt;
20588 glyph[i].charpos = charpos;
20591 n += nwritten;
20594 break;
20597 else /* c == 0 */
20598 break;
20602 break;
20604 case Lisp_Symbol:
20605 /* A symbol: process the value of the symbol recursively
20606 as if it appeared here directly. Avoid error if symbol void.
20607 Special case: if value of symbol is a string, output the string
20608 literally. */
20610 register Lisp_Object tem;
20612 /* If the variable is not marked as risky to set
20613 then its contents are risky to use. */
20614 if (NILP (Fget (elt, Qrisky_local_variable)))
20615 risky = 1;
20617 tem = Fboundp (elt);
20618 if (!NILP (tem))
20620 tem = Fsymbol_value (elt);
20621 /* If value is a string, output that string literally:
20622 don't check for % within it. */
20623 if (STRINGP (tem))
20624 literal = 1;
20626 if (!EQ (tem, elt))
20628 /* Give up right away for nil or t. */
20629 elt = tem;
20630 goto tail_recurse;
20634 break;
20636 case Lisp_Cons:
20638 register Lisp_Object car, tem;
20640 /* A cons cell: five distinct cases.
20641 If first element is :eval or :propertize, do something special.
20642 If first element is a string or a cons, process all the elements
20643 and effectively concatenate them.
20644 If first element is a negative number, truncate displaying cdr to
20645 at most that many characters. If positive, pad (with spaces)
20646 to at least that many characters.
20647 If first element is a symbol, process the cadr or caddr recursively
20648 according to whether the symbol's value is non-nil or nil. */
20649 car = XCAR (elt);
20650 if (EQ (car, QCeval))
20652 /* An element of the form (:eval FORM) means evaluate FORM
20653 and use the result as mode line elements. */
20655 if (risky)
20656 break;
20658 if (CONSP (XCDR (elt)))
20660 Lisp_Object spec;
20661 spec = safe_eval (XCAR (XCDR (elt)));
20662 n += display_mode_element (it, depth, field_width - n,
20663 precision - n, spec, props,
20664 risky);
20667 else if (EQ (car, QCpropertize))
20669 /* An element of the form (:propertize ELT PROPS...)
20670 means display ELT but applying properties PROPS. */
20672 if (risky)
20673 break;
20675 if (CONSP (XCDR (elt)))
20676 n += display_mode_element (it, depth, field_width - n,
20677 precision - n, XCAR (XCDR (elt)),
20678 XCDR (XCDR (elt)), risky);
20680 else if (SYMBOLP (car))
20682 tem = Fboundp (car);
20683 elt = XCDR (elt);
20684 if (!CONSP (elt))
20685 goto invalid;
20686 /* elt is now the cdr, and we know it is a cons cell.
20687 Use its car if CAR has a non-nil value. */
20688 if (!NILP (tem))
20690 tem = Fsymbol_value (car);
20691 if (!NILP (tem))
20693 elt = XCAR (elt);
20694 goto tail_recurse;
20697 /* Symbol's value is nil (or symbol is unbound)
20698 Get the cddr of the original list
20699 and if possible find the caddr and use that. */
20700 elt = XCDR (elt);
20701 if (NILP (elt))
20702 break;
20703 else if (!CONSP (elt))
20704 goto invalid;
20705 elt = XCAR (elt);
20706 goto tail_recurse;
20708 else if (INTEGERP (car))
20710 register int lim = XINT (car);
20711 elt = XCDR (elt);
20712 if (lim < 0)
20714 /* Negative int means reduce maximum width. */
20715 if (precision <= 0)
20716 precision = -lim;
20717 else
20718 precision = min (precision, -lim);
20720 else if (lim > 0)
20722 /* Padding specified. Don't let it be more than
20723 current maximum. */
20724 if (precision > 0)
20725 lim = min (precision, lim);
20727 /* If that's more padding than already wanted, queue it.
20728 But don't reduce padding already specified even if
20729 that is beyond the current truncation point. */
20730 field_width = max (lim, field_width);
20732 goto tail_recurse;
20734 else if (STRINGP (car) || CONSP (car))
20736 Lisp_Object halftail = elt;
20737 int len = 0;
20739 while (CONSP (elt)
20740 && (precision <= 0 || n < precision))
20742 n += display_mode_element (it, depth,
20743 /* Do padding only after the last
20744 element in the list. */
20745 (! CONSP (XCDR (elt))
20746 ? field_width - n
20747 : 0),
20748 precision - n, XCAR (elt),
20749 props, risky);
20750 elt = XCDR (elt);
20751 len++;
20752 if ((len & 1) == 0)
20753 halftail = XCDR (halftail);
20754 /* Check for cycle. */
20755 if (EQ (halftail, elt))
20756 break;
20760 break;
20762 default:
20763 invalid:
20764 elt = build_string ("*invalid*");
20765 goto tail_recurse;
20768 /* Pad to FIELD_WIDTH. */
20769 if (field_width > 0 && n < field_width)
20771 switch (mode_line_target)
20773 case MODE_LINE_NOPROP:
20774 case MODE_LINE_TITLE:
20775 n += store_mode_line_noprop ("", field_width - n, 0);
20776 break;
20777 case MODE_LINE_STRING:
20778 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20779 break;
20780 case MODE_LINE_DISPLAY:
20781 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20782 0, 0, 0);
20783 break;
20787 return n;
20790 /* Store a mode-line string element in mode_line_string_list.
20792 If STRING is non-null, display that C string. Otherwise, the Lisp
20793 string LISP_STRING is displayed.
20795 FIELD_WIDTH is the minimum number of output glyphs to produce.
20796 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20797 with spaces. FIELD_WIDTH <= 0 means don't pad.
20799 PRECISION is the maximum number of characters to output from
20800 STRING. PRECISION <= 0 means don't truncate the string.
20802 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20803 properties to the string.
20805 PROPS are the properties to add to the string.
20806 The mode_line_string_face face property is always added to the string.
20809 static int
20810 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20811 int field_width, int precision, Lisp_Object props)
20813 ptrdiff_t len;
20814 int n = 0;
20816 if (string != NULL)
20818 len = strlen (string);
20819 if (precision > 0 && len > precision)
20820 len = precision;
20821 lisp_string = make_string (string, len);
20822 if (NILP (props))
20823 props = mode_line_string_face_prop;
20824 else if (!NILP (mode_line_string_face))
20826 Lisp_Object face = Fplist_get (props, Qface);
20827 props = Fcopy_sequence (props);
20828 if (NILP (face))
20829 face = mode_line_string_face;
20830 else
20831 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20832 props = Fplist_put (props, Qface, face);
20834 Fadd_text_properties (make_number (0), make_number (len),
20835 props, lisp_string);
20837 else
20839 len = XFASTINT (Flength (lisp_string));
20840 if (precision > 0 && len > precision)
20842 len = precision;
20843 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20844 precision = -1;
20846 if (!NILP (mode_line_string_face))
20848 Lisp_Object face;
20849 if (NILP (props))
20850 props = Ftext_properties_at (make_number (0), lisp_string);
20851 face = Fplist_get (props, Qface);
20852 if (NILP (face))
20853 face = mode_line_string_face;
20854 else
20855 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20856 props = Fcons (Qface, Fcons (face, Qnil));
20857 if (copy_string)
20858 lisp_string = Fcopy_sequence (lisp_string);
20860 if (!NILP (props))
20861 Fadd_text_properties (make_number (0), make_number (len),
20862 props, lisp_string);
20865 if (len > 0)
20867 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20868 n += len;
20871 if (field_width > len)
20873 field_width -= len;
20874 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20875 if (!NILP (props))
20876 Fadd_text_properties (make_number (0), make_number (field_width),
20877 props, lisp_string);
20878 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20879 n += field_width;
20882 return n;
20886 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20887 1, 4, 0,
20888 doc: /* Format a string out of a mode line format specification.
20889 First arg FORMAT specifies the mode line format (see `mode-line-format'
20890 for details) to use.
20892 By default, the format is evaluated for the currently selected window.
20894 Optional second arg FACE specifies the face property to put on all
20895 characters for which no face is specified. The value nil means the
20896 default face. The value t means whatever face the window's mode line
20897 currently uses (either `mode-line' or `mode-line-inactive',
20898 depending on whether the window is the selected window or not).
20899 An integer value means the value string has no text
20900 properties.
20902 Optional third and fourth args WINDOW and BUFFER specify the window
20903 and buffer to use as the context for the formatting (defaults
20904 are the selected window and the WINDOW's buffer). */)
20905 (Lisp_Object format, Lisp_Object face,
20906 Lisp_Object window, Lisp_Object buffer)
20908 struct it it;
20909 int len;
20910 struct window *w;
20911 struct buffer *old_buffer = NULL;
20912 int face_id;
20913 int no_props = INTEGERP (face);
20914 ptrdiff_t count = SPECPDL_INDEX ();
20915 Lisp_Object str;
20916 int string_start = 0;
20918 w = decode_any_window (window);
20919 XSETWINDOW (window, w);
20921 if (NILP (buffer))
20922 buffer = w->contents;
20923 CHECK_BUFFER (buffer);
20925 /* Make formatting the modeline a non-op when noninteractive, otherwise
20926 there will be problems later caused by a partially initialized frame. */
20927 if (NILP (format) || noninteractive)
20928 return empty_unibyte_string;
20930 if (no_props)
20931 face = Qnil;
20933 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20934 : EQ (face, Qt) ? (EQ (window, selected_window)
20935 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20936 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20937 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20938 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20939 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20940 : DEFAULT_FACE_ID;
20942 old_buffer = current_buffer;
20944 /* Save things including mode_line_proptrans_alist,
20945 and set that to nil so that we don't alter the outer value. */
20946 record_unwind_protect (unwind_format_mode_line,
20947 format_mode_line_unwind_data
20948 (XFRAME (WINDOW_FRAME (w)),
20949 old_buffer, selected_window, 1));
20950 mode_line_proptrans_alist = Qnil;
20952 Fselect_window (window, Qt);
20953 set_buffer_internal_1 (XBUFFER (buffer));
20955 init_iterator (&it, w, -1, -1, NULL, face_id);
20957 if (no_props)
20959 mode_line_target = MODE_LINE_NOPROP;
20960 mode_line_string_face_prop = Qnil;
20961 mode_line_string_list = Qnil;
20962 string_start = MODE_LINE_NOPROP_LEN (0);
20964 else
20966 mode_line_target = MODE_LINE_STRING;
20967 mode_line_string_list = Qnil;
20968 mode_line_string_face = face;
20969 mode_line_string_face_prop
20970 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20973 push_kboard (FRAME_KBOARD (it.f));
20974 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20975 pop_kboard ();
20977 if (no_props)
20979 len = MODE_LINE_NOPROP_LEN (string_start);
20980 str = make_string (mode_line_noprop_buf + string_start, len);
20982 else
20984 mode_line_string_list = Fnreverse (mode_line_string_list);
20985 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20986 empty_unibyte_string);
20989 unbind_to (count, Qnil);
20990 return str;
20993 /* Write a null-terminated, right justified decimal representation of
20994 the positive integer D to BUF using a minimal field width WIDTH. */
20996 static void
20997 pint2str (register char *buf, register int width, register ptrdiff_t d)
20999 register char *p = buf;
21001 if (d <= 0)
21002 *p++ = '0';
21003 else
21005 while (d > 0)
21007 *p++ = d % 10 + '0';
21008 d /= 10;
21012 for (width -= (int) (p - buf); width > 0; --width)
21013 *p++ = ' ';
21014 *p-- = '\0';
21015 while (p > buf)
21017 d = *buf;
21018 *buf++ = *p;
21019 *p-- = d;
21023 /* Write a null-terminated, right justified decimal and "human
21024 readable" representation of the nonnegative integer D to BUF using
21025 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21027 static const char power_letter[] =
21029 0, /* no letter */
21030 'k', /* kilo */
21031 'M', /* mega */
21032 'G', /* giga */
21033 'T', /* tera */
21034 'P', /* peta */
21035 'E', /* exa */
21036 'Z', /* zetta */
21037 'Y' /* yotta */
21040 static void
21041 pint2hrstr (char *buf, int width, ptrdiff_t d)
21043 /* We aim to represent the nonnegative integer D as
21044 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21045 ptrdiff_t quotient = d;
21046 int remainder = 0;
21047 /* -1 means: do not use TENTHS. */
21048 int tenths = -1;
21049 int exponent = 0;
21051 /* Length of QUOTIENT.TENTHS as a string. */
21052 int length;
21054 char * psuffix;
21055 char * p;
21057 if (quotient >= 1000)
21059 /* Scale to the appropriate EXPONENT. */
21062 remainder = quotient % 1000;
21063 quotient /= 1000;
21064 exponent++;
21066 while (quotient >= 1000);
21068 /* Round to nearest and decide whether to use TENTHS or not. */
21069 if (quotient <= 9)
21071 tenths = remainder / 100;
21072 if (remainder % 100 >= 50)
21074 if (tenths < 9)
21075 tenths++;
21076 else
21078 quotient++;
21079 if (quotient == 10)
21080 tenths = -1;
21081 else
21082 tenths = 0;
21086 else
21087 if (remainder >= 500)
21089 if (quotient < 999)
21090 quotient++;
21091 else
21093 quotient = 1;
21094 exponent++;
21095 tenths = 0;
21100 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21101 if (tenths == -1 && quotient <= 99)
21102 if (quotient <= 9)
21103 length = 1;
21104 else
21105 length = 2;
21106 else
21107 length = 3;
21108 p = psuffix = buf + max (width, length);
21110 /* Print EXPONENT. */
21111 *psuffix++ = power_letter[exponent];
21112 *psuffix = '\0';
21114 /* Print TENTHS. */
21115 if (tenths >= 0)
21117 *--p = '0' + tenths;
21118 *--p = '.';
21121 /* Print QUOTIENT. */
21124 int digit = quotient % 10;
21125 *--p = '0' + digit;
21127 while ((quotient /= 10) != 0);
21129 /* Print leading spaces. */
21130 while (buf < p)
21131 *--p = ' ';
21134 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21135 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21136 type of CODING_SYSTEM. Return updated pointer into BUF. */
21138 static unsigned char invalid_eol_type[] = "(*invalid*)";
21140 static char *
21141 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21143 Lisp_Object val;
21144 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21145 const unsigned char *eol_str;
21146 int eol_str_len;
21147 /* The EOL conversion we are using. */
21148 Lisp_Object eoltype;
21150 val = CODING_SYSTEM_SPEC (coding_system);
21151 eoltype = Qnil;
21153 if (!VECTORP (val)) /* Not yet decided. */
21155 *buf++ = multibyte ? '-' : ' ';
21156 if (eol_flag)
21157 eoltype = eol_mnemonic_undecided;
21158 /* Don't mention EOL conversion if it isn't decided. */
21160 else
21162 Lisp_Object attrs;
21163 Lisp_Object eolvalue;
21165 attrs = AREF (val, 0);
21166 eolvalue = AREF (val, 2);
21168 *buf++ = multibyte
21169 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21170 : ' ';
21172 if (eol_flag)
21174 /* The EOL conversion that is normal on this system. */
21176 if (NILP (eolvalue)) /* Not yet decided. */
21177 eoltype = eol_mnemonic_undecided;
21178 else if (VECTORP (eolvalue)) /* Not yet decided. */
21179 eoltype = eol_mnemonic_undecided;
21180 else /* eolvalue is Qunix, Qdos, or Qmac. */
21181 eoltype = (EQ (eolvalue, Qunix)
21182 ? eol_mnemonic_unix
21183 : (EQ (eolvalue, Qdos) == 1
21184 ? eol_mnemonic_dos : eol_mnemonic_mac));
21188 if (eol_flag)
21190 /* Mention the EOL conversion if it is not the usual one. */
21191 if (STRINGP (eoltype))
21193 eol_str = SDATA (eoltype);
21194 eol_str_len = SBYTES (eoltype);
21196 else if (CHARACTERP (eoltype))
21198 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21199 int c = XFASTINT (eoltype);
21200 eol_str_len = CHAR_STRING (c, tmp);
21201 eol_str = tmp;
21203 else
21205 eol_str = invalid_eol_type;
21206 eol_str_len = sizeof (invalid_eol_type) - 1;
21208 memcpy (buf, eol_str, eol_str_len);
21209 buf += eol_str_len;
21212 return buf;
21215 /* Return a string for the output of a mode line %-spec for window W,
21216 generated by character C. FIELD_WIDTH > 0 means pad the string
21217 returned with spaces to that value. Return a Lisp string in
21218 *STRING if the resulting string is taken from that Lisp string.
21220 Note we operate on the current buffer for most purposes. */
21222 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21224 static const char *
21225 decode_mode_spec (struct window *w, register int c, int field_width,
21226 Lisp_Object *string)
21228 Lisp_Object obj;
21229 struct frame *f = XFRAME (WINDOW_FRAME (w));
21230 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21231 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21232 produce strings from numerical values, so limit preposterously
21233 large values of FIELD_WIDTH to avoid overrunning the buffer's
21234 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21235 bytes plus the terminating null. */
21236 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21237 struct buffer *b = current_buffer;
21239 obj = Qnil;
21240 *string = Qnil;
21242 switch (c)
21244 case '*':
21245 if (!NILP (BVAR (b, read_only)))
21246 return "%";
21247 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21248 return "*";
21249 return "-";
21251 case '+':
21252 /* This differs from %* only for a modified read-only buffer. */
21253 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21254 return "*";
21255 if (!NILP (BVAR (b, read_only)))
21256 return "%";
21257 return "-";
21259 case '&':
21260 /* This differs from %* in ignoring read-only-ness. */
21261 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21262 return "*";
21263 return "-";
21265 case '%':
21266 return "%";
21268 case '[':
21270 int i;
21271 char *p;
21273 if (command_loop_level > 5)
21274 return "[[[... ";
21275 p = decode_mode_spec_buf;
21276 for (i = 0; i < command_loop_level; i++)
21277 *p++ = '[';
21278 *p = 0;
21279 return decode_mode_spec_buf;
21282 case ']':
21284 int i;
21285 char *p;
21287 if (command_loop_level > 5)
21288 return " ...]]]";
21289 p = decode_mode_spec_buf;
21290 for (i = 0; i < command_loop_level; i++)
21291 *p++ = ']';
21292 *p = 0;
21293 return decode_mode_spec_buf;
21296 case '-':
21298 register int i;
21300 /* Let lots_of_dashes be a string of infinite length. */
21301 if (mode_line_target == MODE_LINE_NOPROP
21302 || mode_line_target == MODE_LINE_STRING)
21303 return "--";
21304 if (field_width <= 0
21305 || field_width > sizeof (lots_of_dashes))
21307 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21308 decode_mode_spec_buf[i] = '-';
21309 decode_mode_spec_buf[i] = '\0';
21310 return decode_mode_spec_buf;
21312 else
21313 return lots_of_dashes;
21316 case 'b':
21317 obj = BVAR (b, name);
21318 break;
21320 case 'c':
21321 /* %c and %l are ignored in `frame-title-format'.
21322 (In redisplay_internal, the frame title is drawn _before_ the
21323 windows are updated, so the stuff which depends on actual
21324 window contents (such as %l) may fail to render properly, or
21325 even crash emacs.) */
21326 if (mode_line_target == MODE_LINE_TITLE)
21327 return "";
21328 else
21330 ptrdiff_t col = current_column ();
21331 w->column_number_displayed = col;
21332 pint2str (decode_mode_spec_buf, width, col);
21333 return decode_mode_spec_buf;
21336 case 'e':
21337 #ifndef SYSTEM_MALLOC
21339 if (NILP (Vmemory_full))
21340 return "";
21341 else
21342 return "!MEM FULL! ";
21344 #else
21345 return "";
21346 #endif
21348 case 'F':
21349 /* %F displays the frame name. */
21350 if (!NILP (f->title))
21351 return SSDATA (f->title);
21352 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21353 return SSDATA (f->name);
21354 return "Emacs";
21356 case 'f':
21357 obj = BVAR (b, filename);
21358 break;
21360 case 'i':
21362 ptrdiff_t size = ZV - BEGV;
21363 pint2str (decode_mode_spec_buf, width, size);
21364 return decode_mode_spec_buf;
21367 case 'I':
21369 ptrdiff_t size = ZV - BEGV;
21370 pint2hrstr (decode_mode_spec_buf, width, size);
21371 return decode_mode_spec_buf;
21374 case 'l':
21376 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21377 ptrdiff_t topline, nlines, height;
21378 ptrdiff_t junk;
21380 /* %c and %l are ignored in `frame-title-format'. */
21381 if (mode_line_target == MODE_LINE_TITLE)
21382 return "";
21384 startpos = marker_position (w->start);
21385 startpos_byte = marker_byte_position (w->start);
21386 height = WINDOW_TOTAL_LINES (w);
21388 /* If we decided that this buffer isn't suitable for line numbers,
21389 don't forget that too fast. */
21390 if (w->base_line_pos == -1)
21391 goto no_value;
21393 /* If the buffer is very big, don't waste time. */
21394 if (INTEGERP (Vline_number_display_limit)
21395 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21397 w->base_line_pos = 0;
21398 w->base_line_number = 0;
21399 goto no_value;
21402 if (w->base_line_number > 0
21403 && w->base_line_pos > 0
21404 && w->base_line_pos <= startpos)
21406 line = w->base_line_number;
21407 linepos = w->base_line_pos;
21408 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21410 else
21412 line = 1;
21413 linepos = BUF_BEGV (b);
21414 linepos_byte = BUF_BEGV_BYTE (b);
21417 /* Count lines from base line to window start position. */
21418 nlines = display_count_lines (linepos_byte,
21419 startpos_byte,
21420 startpos, &junk);
21422 topline = nlines + line;
21424 /* Determine a new base line, if the old one is too close
21425 or too far away, or if we did not have one.
21426 "Too close" means it's plausible a scroll-down would
21427 go back past it. */
21428 if (startpos == BUF_BEGV (b))
21430 w->base_line_number = topline;
21431 w->base_line_pos = BUF_BEGV (b);
21433 else if (nlines < height + 25 || nlines > height * 3 + 50
21434 || linepos == BUF_BEGV (b))
21436 ptrdiff_t limit = BUF_BEGV (b);
21437 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21438 ptrdiff_t position;
21439 ptrdiff_t distance =
21440 (height * 2 + 30) * line_number_display_limit_width;
21442 if (startpos - distance > limit)
21444 limit = startpos - distance;
21445 limit_byte = CHAR_TO_BYTE (limit);
21448 nlines = display_count_lines (startpos_byte,
21449 limit_byte,
21450 - (height * 2 + 30),
21451 &position);
21452 /* If we couldn't find the lines we wanted within
21453 line_number_display_limit_width chars per line,
21454 give up on line numbers for this window. */
21455 if (position == limit_byte && limit == startpos - distance)
21457 w->base_line_pos = -1;
21458 w->base_line_number = 0;
21459 goto no_value;
21462 w->base_line_number = topline - nlines;
21463 w->base_line_pos = BYTE_TO_CHAR (position);
21466 /* Now count lines from the start pos to point. */
21467 nlines = display_count_lines (startpos_byte,
21468 PT_BYTE, PT, &junk);
21470 /* Record that we did display the line number. */
21471 line_number_displayed = 1;
21473 /* Make the string to show. */
21474 pint2str (decode_mode_spec_buf, width, topline + nlines);
21475 return decode_mode_spec_buf;
21476 no_value:
21478 char* p = decode_mode_spec_buf;
21479 int pad = width - 2;
21480 while (pad-- > 0)
21481 *p++ = ' ';
21482 *p++ = '?';
21483 *p++ = '?';
21484 *p = '\0';
21485 return decode_mode_spec_buf;
21488 break;
21490 case 'm':
21491 obj = BVAR (b, mode_name);
21492 break;
21494 case 'n':
21495 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21496 return " Narrow";
21497 break;
21499 case 'p':
21501 ptrdiff_t pos = marker_position (w->start);
21502 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21504 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21506 if (pos <= BUF_BEGV (b))
21507 return "All";
21508 else
21509 return "Bottom";
21511 else if (pos <= BUF_BEGV (b))
21512 return "Top";
21513 else
21515 if (total > 1000000)
21516 /* Do it differently for a large value, to avoid overflow. */
21517 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21518 else
21519 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21520 /* We can't normally display a 3-digit number,
21521 so get us a 2-digit number that is close. */
21522 if (total == 100)
21523 total = 99;
21524 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21525 return decode_mode_spec_buf;
21529 /* Display percentage of size above the bottom of the screen. */
21530 case 'P':
21532 ptrdiff_t toppos = marker_position (w->start);
21533 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21534 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21536 if (botpos >= BUF_ZV (b))
21538 if (toppos <= BUF_BEGV (b))
21539 return "All";
21540 else
21541 return "Bottom";
21543 else
21545 if (total > 1000000)
21546 /* Do it differently for a large value, to avoid overflow. */
21547 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21548 else
21549 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21550 /* We can't normally display a 3-digit number,
21551 so get us a 2-digit number that is close. */
21552 if (total == 100)
21553 total = 99;
21554 if (toppos <= BUF_BEGV (b))
21555 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21556 else
21557 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21558 return decode_mode_spec_buf;
21562 case 's':
21563 /* status of process */
21564 obj = Fget_buffer_process (Fcurrent_buffer ());
21565 if (NILP (obj))
21566 return "no process";
21567 #ifndef MSDOS
21568 obj = Fsymbol_name (Fprocess_status (obj));
21569 #endif
21570 break;
21572 case '@':
21574 ptrdiff_t count = inhibit_garbage_collection ();
21575 Lisp_Object val = call1 (intern ("file-remote-p"),
21576 BVAR (current_buffer, directory));
21577 unbind_to (count, Qnil);
21579 if (NILP (val))
21580 return "-";
21581 else
21582 return "@";
21585 case 'z':
21586 /* coding-system (not including end-of-line format) */
21587 case 'Z':
21588 /* coding-system (including end-of-line type) */
21590 int eol_flag = (c == 'Z');
21591 char *p = decode_mode_spec_buf;
21593 if (! FRAME_WINDOW_P (f))
21595 /* No need to mention EOL here--the terminal never needs
21596 to do EOL conversion. */
21597 p = decode_mode_spec_coding (CODING_ID_NAME
21598 (FRAME_KEYBOARD_CODING (f)->id),
21599 p, 0);
21600 p = decode_mode_spec_coding (CODING_ID_NAME
21601 (FRAME_TERMINAL_CODING (f)->id),
21602 p, 0);
21604 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21605 p, eol_flag);
21607 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21608 #ifdef subprocesses
21609 obj = Fget_buffer_process (Fcurrent_buffer ());
21610 if (PROCESSP (obj))
21612 p = decode_mode_spec_coding
21613 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21614 p = decode_mode_spec_coding
21615 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21617 #endif /* subprocesses */
21618 #endif /* 0 */
21619 *p = 0;
21620 return decode_mode_spec_buf;
21624 if (STRINGP (obj))
21626 *string = obj;
21627 return SSDATA (obj);
21629 else
21630 return "";
21634 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
21635 means count lines back from START_BYTE. But don't go beyond
21636 LIMIT_BYTE. Return the number of lines thus found (always
21637 nonnegative).
21639 Set *BYTE_POS_PTR to the byte position where we stopped. This is
21640 either the position COUNT lines after/before START_BYTE, if we
21641 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
21642 COUNT lines. */
21644 static ptrdiff_t
21645 display_count_lines (ptrdiff_t start_byte,
21646 ptrdiff_t limit_byte, ptrdiff_t count,
21647 ptrdiff_t *byte_pos_ptr)
21649 register unsigned char *cursor;
21650 unsigned char *base;
21652 register ptrdiff_t ceiling;
21653 register unsigned char *ceiling_addr;
21654 ptrdiff_t orig_count = count;
21656 /* If we are not in selective display mode,
21657 check only for newlines. */
21658 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21659 && !INTEGERP (BVAR (current_buffer, selective_display)));
21661 if (count > 0)
21663 while (start_byte < limit_byte)
21665 ceiling = BUFFER_CEILING_OF (start_byte);
21666 ceiling = min (limit_byte - 1, ceiling);
21667 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21668 base = (cursor = BYTE_POS_ADDR (start_byte));
21672 if (selective_display)
21674 while (*cursor != '\n' && *cursor != 015
21675 && ++cursor != ceiling_addr)
21676 continue;
21677 if (cursor == ceiling_addr)
21678 break;
21680 else
21682 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
21683 if (! cursor)
21684 break;
21687 cursor++;
21689 if (--count == 0)
21691 start_byte += cursor - base;
21692 *byte_pos_ptr = start_byte;
21693 return orig_count;
21696 while (cursor < ceiling_addr);
21698 start_byte += ceiling_addr - base;
21701 else
21703 while (start_byte > limit_byte)
21705 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21706 ceiling = max (limit_byte, ceiling);
21707 ceiling_addr = BYTE_POS_ADDR (ceiling);
21708 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21709 while (1)
21711 if (selective_display)
21713 while (--cursor >= ceiling_addr
21714 && *cursor != '\n' && *cursor != 015)
21715 continue;
21716 if (cursor < ceiling_addr)
21717 break;
21719 else
21721 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
21722 if (! cursor)
21723 break;
21726 if (++count == 0)
21728 start_byte += cursor - base + 1;
21729 *byte_pos_ptr = start_byte;
21730 /* When scanning backwards, we should
21731 not count the newline posterior to which we stop. */
21732 return - orig_count - 1;
21735 start_byte += ceiling_addr - base;
21739 *byte_pos_ptr = limit_byte;
21741 if (count < 0)
21742 return - orig_count + count;
21743 return orig_count - count;
21749 /***********************************************************************
21750 Displaying strings
21751 ***********************************************************************/
21753 /* Display a NUL-terminated string, starting with index START.
21755 If STRING is non-null, display that C string. Otherwise, the Lisp
21756 string LISP_STRING is displayed. There's a case that STRING is
21757 non-null and LISP_STRING is not nil. It means STRING is a string
21758 data of LISP_STRING. In that case, we display LISP_STRING while
21759 ignoring its text properties.
21761 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21762 FACE_STRING. Display STRING or LISP_STRING with the face at
21763 FACE_STRING_POS in FACE_STRING:
21765 Display the string in the environment given by IT, but use the
21766 standard display table, temporarily.
21768 FIELD_WIDTH is the minimum number of output glyphs to produce.
21769 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21770 with spaces. If STRING has more characters, more than FIELD_WIDTH
21771 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21773 PRECISION is the maximum number of characters to output from
21774 STRING. PRECISION < 0 means don't truncate the string.
21776 This is roughly equivalent to printf format specifiers:
21778 FIELD_WIDTH PRECISION PRINTF
21779 ----------------------------------------
21780 -1 -1 %s
21781 -1 10 %.10s
21782 10 -1 %10s
21783 20 10 %20.10s
21785 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21786 display them, and < 0 means obey the current buffer's value of
21787 enable_multibyte_characters.
21789 Value is the number of columns displayed. */
21791 static int
21792 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21793 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21794 int field_width, int precision, int max_x, int multibyte)
21796 int hpos_at_start = it->hpos;
21797 int saved_face_id = it->face_id;
21798 struct glyph_row *row = it->glyph_row;
21799 ptrdiff_t it_charpos;
21801 /* Initialize the iterator IT for iteration over STRING beginning
21802 with index START. */
21803 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21804 precision, field_width, multibyte);
21805 if (string && STRINGP (lisp_string))
21806 /* LISP_STRING is the one returned by decode_mode_spec. We should
21807 ignore its text properties. */
21808 it->stop_charpos = it->end_charpos;
21810 /* If displaying STRING, set up the face of the iterator from
21811 FACE_STRING, if that's given. */
21812 if (STRINGP (face_string))
21814 ptrdiff_t endptr;
21815 struct face *face;
21817 it->face_id
21818 = face_at_string_position (it->w, face_string, face_string_pos,
21819 0, it->region_beg_charpos,
21820 it->region_end_charpos,
21821 &endptr, it->base_face_id, 0);
21822 face = FACE_FROM_ID (it->f, it->face_id);
21823 it->face_box_p = face->box != FACE_NO_BOX;
21826 /* Set max_x to the maximum allowed X position. Don't let it go
21827 beyond the right edge of the window. */
21828 if (max_x <= 0)
21829 max_x = it->last_visible_x;
21830 else
21831 max_x = min (max_x, it->last_visible_x);
21833 /* Skip over display elements that are not visible. because IT->w is
21834 hscrolled. */
21835 if (it->current_x < it->first_visible_x)
21836 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21837 MOVE_TO_POS | MOVE_TO_X);
21839 row->ascent = it->max_ascent;
21840 row->height = it->max_ascent + it->max_descent;
21841 row->phys_ascent = it->max_phys_ascent;
21842 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21843 row->extra_line_spacing = it->max_extra_line_spacing;
21845 if (STRINGP (it->string))
21846 it_charpos = IT_STRING_CHARPOS (*it);
21847 else
21848 it_charpos = IT_CHARPOS (*it);
21850 /* This condition is for the case that we are called with current_x
21851 past last_visible_x. */
21852 while (it->current_x < max_x)
21854 int x_before, x, n_glyphs_before, i, nglyphs;
21856 /* Get the next display element. */
21857 if (!get_next_display_element (it))
21858 break;
21860 /* Produce glyphs. */
21861 x_before = it->current_x;
21862 n_glyphs_before = row->used[TEXT_AREA];
21863 PRODUCE_GLYPHS (it);
21865 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21866 i = 0;
21867 x = x_before;
21868 while (i < nglyphs)
21870 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21872 if (it->line_wrap != TRUNCATE
21873 && x + glyph->pixel_width > max_x)
21875 /* End of continued line or max_x reached. */
21876 if (CHAR_GLYPH_PADDING_P (*glyph))
21878 /* A wide character is unbreakable. */
21879 if (row->reversed_p)
21880 unproduce_glyphs (it, row->used[TEXT_AREA]
21881 - n_glyphs_before);
21882 row->used[TEXT_AREA] = n_glyphs_before;
21883 it->current_x = x_before;
21885 else
21887 if (row->reversed_p)
21888 unproduce_glyphs (it, row->used[TEXT_AREA]
21889 - (n_glyphs_before + i));
21890 row->used[TEXT_AREA] = n_glyphs_before + i;
21891 it->current_x = x;
21893 break;
21895 else if (x + glyph->pixel_width >= it->first_visible_x)
21897 /* Glyph is at least partially visible. */
21898 ++it->hpos;
21899 if (x < it->first_visible_x)
21900 row->x = x - it->first_visible_x;
21902 else
21904 /* Glyph is off the left margin of the display area.
21905 Should not happen. */
21906 emacs_abort ();
21909 row->ascent = max (row->ascent, it->max_ascent);
21910 row->height = max (row->height, it->max_ascent + it->max_descent);
21911 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21912 row->phys_height = max (row->phys_height,
21913 it->max_phys_ascent + it->max_phys_descent);
21914 row->extra_line_spacing = max (row->extra_line_spacing,
21915 it->max_extra_line_spacing);
21916 x += glyph->pixel_width;
21917 ++i;
21920 /* Stop if max_x reached. */
21921 if (i < nglyphs)
21922 break;
21924 /* Stop at line ends. */
21925 if (ITERATOR_AT_END_OF_LINE_P (it))
21927 it->continuation_lines_width = 0;
21928 break;
21931 set_iterator_to_next (it, 1);
21932 if (STRINGP (it->string))
21933 it_charpos = IT_STRING_CHARPOS (*it);
21934 else
21935 it_charpos = IT_CHARPOS (*it);
21937 /* Stop if truncating at the right edge. */
21938 if (it->line_wrap == TRUNCATE
21939 && it->current_x >= it->last_visible_x)
21941 /* Add truncation mark, but don't do it if the line is
21942 truncated at a padding space. */
21943 if (it_charpos < it->string_nchars)
21945 if (!FRAME_WINDOW_P (it->f))
21947 int ii, n;
21949 if (it->current_x > it->last_visible_x)
21951 if (!row->reversed_p)
21953 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21954 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21955 break;
21957 else
21959 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21960 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21961 break;
21962 unproduce_glyphs (it, ii + 1);
21963 ii = row->used[TEXT_AREA] - (ii + 1);
21965 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21967 row->used[TEXT_AREA] = ii;
21968 produce_special_glyphs (it, IT_TRUNCATION);
21971 produce_special_glyphs (it, IT_TRUNCATION);
21973 row->truncated_on_right_p = 1;
21975 break;
21979 /* Maybe insert a truncation at the left. */
21980 if (it->first_visible_x
21981 && it_charpos > 0)
21983 if (!FRAME_WINDOW_P (it->f)
21984 || (row->reversed_p
21985 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21986 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21987 insert_left_trunc_glyphs (it);
21988 row->truncated_on_left_p = 1;
21991 it->face_id = saved_face_id;
21993 /* Value is number of columns displayed. */
21994 return it->hpos - hpos_at_start;
21999 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22000 appears as an element of LIST or as the car of an element of LIST.
22001 If PROPVAL is a list, compare each element against LIST in that
22002 way, and return 1/2 if any element of PROPVAL is found in LIST.
22003 Otherwise return 0. This function cannot quit.
22004 The return value is 2 if the text is invisible but with an ellipsis
22005 and 1 if it's invisible and without an ellipsis. */
22008 invisible_p (register Lisp_Object propval, Lisp_Object list)
22010 register Lisp_Object tail, proptail;
22012 for (tail = list; CONSP (tail); tail = XCDR (tail))
22014 register Lisp_Object tem;
22015 tem = XCAR (tail);
22016 if (EQ (propval, tem))
22017 return 1;
22018 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22019 return NILP (XCDR (tem)) ? 1 : 2;
22022 if (CONSP (propval))
22024 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22026 Lisp_Object propelt;
22027 propelt = XCAR (proptail);
22028 for (tail = list; CONSP (tail); tail = XCDR (tail))
22030 register Lisp_Object tem;
22031 tem = XCAR (tail);
22032 if (EQ (propelt, tem))
22033 return 1;
22034 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22035 return NILP (XCDR (tem)) ? 1 : 2;
22040 return 0;
22043 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22044 doc: /* Non-nil if the property makes the text invisible.
22045 POS-OR-PROP can be a marker or number, in which case it is taken to be
22046 a position in the current buffer and the value of the `invisible' property
22047 is checked; or it can be some other value, which is then presumed to be the
22048 value of the `invisible' property of the text of interest.
22049 The non-nil value returned can be t for truly invisible text or something
22050 else if the text is replaced by an ellipsis. */)
22051 (Lisp_Object pos_or_prop)
22053 Lisp_Object prop
22054 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22055 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22056 : pos_or_prop);
22057 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22058 return (invis == 0 ? Qnil
22059 : invis == 1 ? Qt
22060 : make_number (invis));
22063 /* Calculate a width or height in pixels from a specification using
22064 the following elements:
22066 SPEC ::=
22067 NUM - a (fractional) multiple of the default font width/height
22068 (NUM) - specifies exactly NUM pixels
22069 UNIT - a fixed number of pixels, see below.
22070 ELEMENT - size of a display element in pixels, see below.
22071 (NUM . SPEC) - equals NUM * SPEC
22072 (+ SPEC SPEC ...) - add pixel values
22073 (- SPEC SPEC ...) - subtract pixel values
22074 (- SPEC) - negate pixel value
22076 NUM ::=
22077 INT or FLOAT - a number constant
22078 SYMBOL - use symbol's (buffer local) variable binding.
22080 UNIT ::=
22081 in - pixels per inch *)
22082 mm - pixels per 1/1000 meter *)
22083 cm - pixels per 1/100 meter *)
22084 width - width of current font in pixels.
22085 height - height of current font in pixels.
22087 *) using the ratio(s) defined in display-pixels-per-inch.
22089 ELEMENT ::=
22091 left-fringe - left fringe width in pixels
22092 right-fringe - right fringe width in pixels
22094 left-margin - left margin width in pixels
22095 right-margin - right margin width in pixels
22097 scroll-bar - scroll-bar area width in pixels
22099 Examples:
22101 Pixels corresponding to 5 inches:
22102 (5 . in)
22104 Total width of non-text areas on left side of window (if scroll-bar is on left):
22105 '(space :width (+ left-fringe left-margin scroll-bar))
22107 Align to first text column (in header line):
22108 '(space :align-to 0)
22110 Align to middle of text area minus half the width of variable `my-image'
22111 containing a loaded image:
22112 '(space :align-to (0.5 . (- text my-image)))
22114 Width of left margin minus width of 1 character in the default font:
22115 '(space :width (- left-margin 1))
22117 Width of left margin minus width of 2 characters in the current font:
22118 '(space :width (- left-margin (2 . width)))
22120 Center 1 character over left-margin (in header line):
22121 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22123 Different ways to express width of left fringe plus left margin minus one pixel:
22124 '(space :width (- (+ left-fringe left-margin) (1)))
22125 '(space :width (+ left-fringe left-margin (- (1))))
22126 '(space :width (+ left-fringe left-margin (-1)))
22130 static int
22131 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22132 struct font *font, int width_p, int *align_to)
22134 double pixels;
22136 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22137 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22139 if (NILP (prop))
22140 return OK_PIXELS (0);
22142 eassert (FRAME_LIVE_P (it->f));
22144 if (SYMBOLP (prop))
22146 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22148 char *unit = SSDATA (SYMBOL_NAME (prop));
22150 if (unit[0] == 'i' && unit[1] == 'n')
22151 pixels = 1.0;
22152 else if (unit[0] == 'm' && unit[1] == 'm')
22153 pixels = 25.4;
22154 else if (unit[0] == 'c' && unit[1] == 'm')
22155 pixels = 2.54;
22156 else
22157 pixels = 0;
22158 if (pixels > 0)
22160 double ppi = (width_p ? FRAME_RES_X (it->f)
22161 : FRAME_RES_Y (it->f));
22163 if (ppi > 0)
22164 return OK_PIXELS (ppi / pixels);
22165 return 0;
22169 #ifdef HAVE_WINDOW_SYSTEM
22170 if (EQ (prop, Qheight))
22171 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22172 if (EQ (prop, Qwidth))
22173 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22174 #else
22175 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22176 return OK_PIXELS (1);
22177 #endif
22179 if (EQ (prop, Qtext))
22180 return OK_PIXELS (width_p
22181 ? window_box_width (it->w, TEXT_AREA)
22182 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22184 if (align_to && *align_to < 0)
22186 *res = 0;
22187 if (EQ (prop, Qleft))
22188 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22189 if (EQ (prop, Qright))
22190 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22191 if (EQ (prop, Qcenter))
22192 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22193 + window_box_width (it->w, TEXT_AREA) / 2);
22194 if (EQ (prop, Qleft_fringe))
22195 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22196 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22197 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22198 if (EQ (prop, Qright_fringe))
22199 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22200 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22201 : window_box_right_offset (it->w, TEXT_AREA));
22202 if (EQ (prop, Qleft_margin))
22203 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22204 if (EQ (prop, Qright_margin))
22205 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22206 if (EQ (prop, Qscroll_bar))
22207 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22209 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22210 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22211 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22212 : 0)));
22214 else
22216 if (EQ (prop, Qleft_fringe))
22217 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22218 if (EQ (prop, Qright_fringe))
22219 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22220 if (EQ (prop, Qleft_margin))
22221 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22222 if (EQ (prop, Qright_margin))
22223 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22224 if (EQ (prop, Qscroll_bar))
22225 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22228 prop = buffer_local_value_1 (prop, it->w->contents);
22229 if (EQ (prop, Qunbound))
22230 prop = Qnil;
22233 if (INTEGERP (prop) || FLOATP (prop))
22235 int base_unit = (width_p
22236 ? FRAME_COLUMN_WIDTH (it->f)
22237 : FRAME_LINE_HEIGHT (it->f));
22238 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22241 if (CONSP (prop))
22243 Lisp_Object car = XCAR (prop);
22244 Lisp_Object cdr = XCDR (prop);
22246 if (SYMBOLP (car))
22248 #ifdef HAVE_WINDOW_SYSTEM
22249 if (FRAME_WINDOW_P (it->f)
22250 && valid_image_p (prop))
22252 ptrdiff_t id = lookup_image (it->f, prop);
22253 struct image *img = IMAGE_FROM_ID (it->f, id);
22255 return OK_PIXELS (width_p ? img->width : img->height);
22257 #endif
22258 if (EQ (car, Qplus) || EQ (car, Qminus))
22260 int first = 1;
22261 double px;
22263 pixels = 0;
22264 while (CONSP (cdr))
22266 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22267 font, width_p, align_to))
22268 return 0;
22269 if (first)
22270 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22271 else
22272 pixels += px;
22273 cdr = XCDR (cdr);
22275 if (EQ (car, Qminus))
22276 pixels = -pixels;
22277 return OK_PIXELS (pixels);
22280 car = buffer_local_value_1 (car, it->w->contents);
22281 if (EQ (car, Qunbound))
22282 car = Qnil;
22285 if (INTEGERP (car) || FLOATP (car))
22287 double fact;
22288 pixels = XFLOATINT (car);
22289 if (NILP (cdr))
22290 return OK_PIXELS (pixels);
22291 if (calc_pixel_width_or_height (&fact, it, cdr,
22292 font, width_p, align_to))
22293 return OK_PIXELS (pixels * fact);
22294 return 0;
22297 return 0;
22300 return 0;
22304 /***********************************************************************
22305 Glyph Display
22306 ***********************************************************************/
22308 #ifdef HAVE_WINDOW_SYSTEM
22310 #ifdef GLYPH_DEBUG
22312 void
22313 dump_glyph_string (struct glyph_string *s)
22315 fprintf (stderr, "glyph string\n");
22316 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22317 s->x, s->y, s->width, s->height);
22318 fprintf (stderr, " ybase = %d\n", s->ybase);
22319 fprintf (stderr, " hl = %d\n", s->hl);
22320 fprintf (stderr, " left overhang = %d, right = %d\n",
22321 s->left_overhang, s->right_overhang);
22322 fprintf (stderr, " nchars = %d\n", s->nchars);
22323 fprintf (stderr, " extends to end of line = %d\n",
22324 s->extends_to_end_of_line_p);
22325 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22326 fprintf (stderr, " bg width = %d\n", s->background_width);
22329 #endif /* GLYPH_DEBUG */
22331 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22332 of XChar2b structures for S; it can't be allocated in
22333 init_glyph_string because it must be allocated via `alloca'. W
22334 is the window on which S is drawn. ROW and AREA are the glyph row
22335 and area within the row from which S is constructed. START is the
22336 index of the first glyph structure covered by S. HL is a
22337 face-override for drawing S. */
22339 #ifdef HAVE_NTGUI
22340 #define OPTIONAL_HDC(hdc) HDC hdc,
22341 #define DECLARE_HDC(hdc) HDC hdc;
22342 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22343 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22344 #endif
22346 #ifndef OPTIONAL_HDC
22347 #define OPTIONAL_HDC(hdc)
22348 #define DECLARE_HDC(hdc)
22349 #define ALLOCATE_HDC(hdc, f)
22350 #define RELEASE_HDC(hdc, f)
22351 #endif
22353 static void
22354 init_glyph_string (struct glyph_string *s,
22355 OPTIONAL_HDC (hdc)
22356 XChar2b *char2b, struct window *w, struct glyph_row *row,
22357 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22359 memset (s, 0, sizeof *s);
22360 s->w = w;
22361 s->f = XFRAME (w->frame);
22362 #ifdef HAVE_NTGUI
22363 s->hdc = hdc;
22364 #endif
22365 s->display = FRAME_X_DISPLAY (s->f);
22366 s->window = FRAME_X_WINDOW (s->f);
22367 s->char2b = char2b;
22368 s->hl = hl;
22369 s->row = row;
22370 s->area = area;
22371 s->first_glyph = row->glyphs[area] + start;
22372 s->height = row->height;
22373 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22374 s->ybase = s->y + row->ascent;
22378 /* Append the list of glyph strings with head H and tail T to the list
22379 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22381 static void
22382 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22383 struct glyph_string *h, struct glyph_string *t)
22385 if (h)
22387 if (*head)
22388 (*tail)->next = h;
22389 else
22390 *head = h;
22391 h->prev = *tail;
22392 *tail = t;
22397 /* Prepend the list of glyph strings with head H and tail T to the
22398 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22399 result. */
22401 static void
22402 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22403 struct glyph_string *h, struct glyph_string *t)
22405 if (h)
22407 if (*head)
22408 (*head)->prev = t;
22409 else
22410 *tail = t;
22411 t->next = *head;
22412 *head = h;
22417 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22418 Set *HEAD and *TAIL to the resulting list. */
22420 static void
22421 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22422 struct glyph_string *s)
22424 s->next = s->prev = NULL;
22425 append_glyph_string_lists (head, tail, s, s);
22429 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22430 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22431 make sure that X resources for the face returned are allocated.
22432 Value is a pointer to a realized face that is ready for display if
22433 DISPLAY_P is non-zero. */
22435 static struct face *
22436 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22437 XChar2b *char2b, int display_p)
22439 struct face *face = FACE_FROM_ID (f, face_id);
22440 unsigned code = 0;
22442 if (face->font)
22444 code = face->font->driver->encode_char (face->font, c);
22446 if (code == FONT_INVALID_CODE)
22447 code = 0;
22449 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22451 /* Make sure X resources of the face are allocated. */
22452 #ifdef HAVE_X_WINDOWS
22453 if (display_p)
22454 #endif
22456 eassert (face != NULL);
22457 PREPARE_FACE_FOR_DISPLAY (f, face);
22460 return face;
22464 /* Get face and two-byte form of character glyph GLYPH on frame F.
22465 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22466 a pointer to a realized face that is ready for display. */
22468 static struct face *
22469 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22470 XChar2b *char2b, int *two_byte_p)
22472 struct face *face;
22473 unsigned code = 0;
22475 eassert (glyph->type == CHAR_GLYPH);
22476 face = FACE_FROM_ID (f, glyph->face_id);
22478 /* Make sure X resources of the face are allocated. */
22479 eassert (face != NULL);
22480 PREPARE_FACE_FOR_DISPLAY (f, face);
22482 if (two_byte_p)
22483 *two_byte_p = 0;
22485 if (face->font)
22487 if (CHAR_BYTE8_P (glyph->u.ch))
22488 code = CHAR_TO_BYTE8 (glyph->u.ch);
22489 else
22490 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22492 if (code == FONT_INVALID_CODE)
22493 code = 0;
22496 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22497 return face;
22501 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22502 Return 1 if FONT has a glyph for C, otherwise return 0. */
22504 static int
22505 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22507 unsigned code;
22509 if (CHAR_BYTE8_P (c))
22510 code = CHAR_TO_BYTE8 (c);
22511 else
22512 code = font->driver->encode_char (font, c);
22514 if (code == FONT_INVALID_CODE)
22515 return 0;
22516 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22517 return 1;
22521 /* Fill glyph string S with composition components specified by S->cmp.
22523 BASE_FACE is the base face of the composition.
22524 S->cmp_from is the index of the first component for S.
22526 OVERLAPS non-zero means S should draw the foreground only, and use
22527 its physical height for clipping. See also draw_glyphs.
22529 Value is the index of a component not in S. */
22531 static int
22532 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22533 int overlaps)
22535 int i;
22536 /* For all glyphs of this composition, starting at the offset
22537 S->cmp_from, until we reach the end of the definition or encounter a
22538 glyph that requires the different face, add it to S. */
22539 struct face *face;
22541 eassert (s);
22543 s->for_overlaps = overlaps;
22544 s->face = NULL;
22545 s->font = NULL;
22546 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22548 int c = COMPOSITION_GLYPH (s->cmp, i);
22550 /* TAB in a composition means display glyphs with padding space
22551 on the left or right. */
22552 if (c != '\t')
22554 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22555 -1, Qnil);
22557 face = get_char_face_and_encoding (s->f, c, face_id,
22558 s->char2b + i, 1);
22559 if (face)
22561 if (! s->face)
22563 s->face = face;
22564 s->font = s->face->font;
22566 else if (s->face != face)
22567 break;
22570 ++s->nchars;
22572 s->cmp_to = i;
22574 if (s->face == NULL)
22576 s->face = base_face->ascii_face;
22577 s->font = s->face->font;
22580 /* All glyph strings for the same composition has the same width,
22581 i.e. the width set for the first component of the composition. */
22582 s->width = s->first_glyph->pixel_width;
22584 /* If the specified font could not be loaded, use the frame's
22585 default font, but record the fact that we couldn't load it in
22586 the glyph string so that we can draw rectangles for the
22587 characters of the glyph string. */
22588 if (s->font == NULL)
22590 s->font_not_found_p = 1;
22591 s->font = FRAME_FONT (s->f);
22594 /* Adjust base line for subscript/superscript text. */
22595 s->ybase += s->first_glyph->voffset;
22597 /* This glyph string must always be drawn with 16-bit functions. */
22598 s->two_byte_p = 1;
22600 return s->cmp_to;
22603 static int
22604 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22605 int start, int end, int overlaps)
22607 struct glyph *glyph, *last;
22608 Lisp_Object lgstring;
22609 int i;
22611 s->for_overlaps = overlaps;
22612 glyph = s->row->glyphs[s->area] + start;
22613 last = s->row->glyphs[s->area] + end;
22614 s->cmp_id = glyph->u.cmp.id;
22615 s->cmp_from = glyph->slice.cmp.from;
22616 s->cmp_to = glyph->slice.cmp.to + 1;
22617 s->face = FACE_FROM_ID (s->f, face_id);
22618 lgstring = composition_gstring_from_id (s->cmp_id);
22619 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22620 glyph++;
22621 while (glyph < last
22622 && glyph->u.cmp.automatic
22623 && glyph->u.cmp.id == s->cmp_id
22624 && s->cmp_to == glyph->slice.cmp.from)
22625 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22627 for (i = s->cmp_from; i < s->cmp_to; i++)
22629 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22630 unsigned code = LGLYPH_CODE (lglyph);
22632 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22634 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22635 return glyph - s->row->glyphs[s->area];
22639 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22640 See the comment of fill_glyph_string for arguments.
22641 Value is the index of the first glyph not in S. */
22644 static int
22645 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22646 int start, int end, int overlaps)
22648 struct glyph *glyph, *last;
22649 int voffset;
22651 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22652 s->for_overlaps = overlaps;
22653 glyph = s->row->glyphs[s->area] + start;
22654 last = s->row->glyphs[s->area] + end;
22655 voffset = glyph->voffset;
22656 s->face = FACE_FROM_ID (s->f, face_id);
22657 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22658 s->nchars = 1;
22659 s->width = glyph->pixel_width;
22660 glyph++;
22661 while (glyph < last
22662 && glyph->type == GLYPHLESS_GLYPH
22663 && glyph->voffset == voffset
22664 && glyph->face_id == face_id)
22666 s->nchars++;
22667 s->width += glyph->pixel_width;
22668 glyph++;
22670 s->ybase += voffset;
22671 return glyph - s->row->glyphs[s->area];
22675 /* Fill glyph string S from a sequence of character glyphs.
22677 FACE_ID is the face id of the string. START is the index of the
22678 first glyph to consider, END is the index of the last + 1.
22679 OVERLAPS non-zero means S should draw the foreground only, and use
22680 its physical height for clipping. See also draw_glyphs.
22682 Value is the index of the first glyph not in S. */
22684 static int
22685 fill_glyph_string (struct glyph_string *s, int face_id,
22686 int start, int end, int overlaps)
22688 struct glyph *glyph, *last;
22689 int voffset;
22690 int glyph_not_available_p;
22692 eassert (s->f == XFRAME (s->w->frame));
22693 eassert (s->nchars == 0);
22694 eassert (start >= 0 && end > start);
22696 s->for_overlaps = overlaps;
22697 glyph = s->row->glyphs[s->area] + start;
22698 last = s->row->glyphs[s->area] + end;
22699 voffset = glyph->voffset;
22700 s->padding_p = glyph->padding_p;
22701 glyph_not_available_p = glyph->glyph_not_available_p;
22703 while (glyph < last
22704 && glyph->type == CHAR_GLYPH
22705 && glyph->voffset == voffset
22706 /* Same face id implies same font, nowadays. */
22707 && glyph->face_id == face_id
22708 && glyph->glyph_not_available_p == glyph_not_available_p)
22710 int two_byte_p;
22712 s->face = get_glyph_face_and_encoding (s->f, glyph,
22713 s->char2b + s->nchars,
22714 &two_byte_p);
22715 s->two_byte_p = two_byte_p;
22716 ++s->nchars;
22717 eassert (s->nchars <= end - start);
22718 s->width += glyph->pixel_width;
22719 if (glyph++->padding_p != s->padding_p)
22720 break;
22723 s->font = s->face->font;
22725 /* If the specified font could not be loaded, use the frame's font,
22726 but record the fact that we couldn't load it in
22727 S->font_not_found_p so that we can draw rectangles for the
22728 characters of the glyph string. */
22729 if (s->font == NULL || glyph_not_available_p)
22731 s->font_not_found_p = 1;
22732 s->font = FRAME_FONT (s->f);
22735 /* Adjust base line for subscript/superscript text. */
22736 s->ybase += voffset;
22738 eassert (s->face && s->face->gc);
22739 return glyph - s->row->glyphs[s->area];
22743 /* Fill glyph string S from image glyph S->first_glyph. */
22745 static void
22746 fill_image_glyph_string (struct glyph_string *s)
22748 eassert (s->first_glyph->type == IMAGE_GLYPH);
22749 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22750 eassert (s->img);
22751 s->slice = s->first_glyph->slice.img;
22752 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22753 s->font = s->face->font;
22754 s->width = s->first_glyph->pixel_width;
22756 /* Adjust base line for subscript/superscript text. */
22757 s->ybase += s->first_glyph->voffset;
22761 /* Fill glyph string S from a sequence of stretch glyphs.
22763 START is the index of the first glyph to consider,
22764 END is the index of the last + 1.
22766 Value is the index of the first glyph not in S. */
22768 static int
22769 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22771 struct glyph *glyph, *last;
22772 int voffset, face_id;
22774 eassert (s->first_glyph->type == STRETCH_GLYPH);
22776 glyph = s->row->glyphs[s->area] + start;
22777 last = s->row->glyphs[s->area] + end;
22778 face_id = glyph->face_id;
22779 s->face = FACE_FROM_ID (s->f, face_id);
22780 s->font = s->face->font;
22781 s->width = glyph->pixel_width;
22782 s->nchars = 1;
22783 voffset = glyph->voffset;
22785 for (++glyph;
22786 (glyph < last
22787 && glyph->type == STRETCH_GLYPH
22788 && glyph->voffset == voffset
22789 && glyph->face_id == face_id);
22790 ++glyph)
22791 s->width += glyph->pixel_width;
22793 /* Adjust base line for subscript/superscript text. */
22794 s->ybase += voffset;
22796 /* The case that face->gc == 0 is handled when drawing the glyph
22797 string by calling PREPARE_FACE_FOR_DISPLAY. */
22798 eassert (s->face);
22799 return glyph - s->row->glyphs[s->area];
22802 static struct font_metrics *
22803 get_per_char_metric (struct font *font, XChar2b *char2b)
22805 static struct font_metrics metrics;
22806 unsigned code;
22808 if (! font)
22809 return NULL;
22810 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22811 if (code == FONT_INVALID_CODE)
22812 return NULL;
22813 font->driver->text_extents (font, &code, 1, &metrics);
22814 return &metrics;
22817 /* EXPORT for RIF:
22818 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22819 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22820 assumed to be zero. */
22822 void
22823 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22825 *left = *right = 0;
22827 if (glyph->type == CHAR_GLYPH)
22829 struct face *face;
22830 XChar2b char2b;
22831 struct font_metrics *pcm;
22833 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22834 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22836 if (pcm->rbearing > pcm->width)
22837 *right = pcm->rbearing - pcm->width;
22838 if (pcm->lbearing < 0)
22839 *left = -pcm->lbearing;
22842 else if (glyph->type == COMPOSITE_GLYPH)
22844 if (! glyph->u.cmp.automatic)
22846 struct composition *cmp = composition_table[glyph->u.cmp.id];
22848 if (cmp->rbearing > cmp->pixel_width)
22849 *right = cmp->rbearing - cmp->pixel_width;
22850 if (cmp->lbearing < 0)
22851 *left = - cmp->lbearing;
22853 else
22855 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22856 struct font_metrics metrics;
22858 composition_gstring_width (gstring, glyph->slice.cmp.from,
22859 glyph->slice.cmp.to + 1, &metrics);
22860 if (metrics.rbearing > metrics.width)
22861 *right = metrics.rbearing - metrics.width;
22862 if (metrics.lbearing < 0)
22863 *left = - metrics.lbearing;
22869 /* Return the index of the first glyph preceding glyph string S that
22870 is overwritten by S because of S's left overhang. Value is -1
22871 if no glyphs are overwritten. */
22873 static int
22874 left_overwritten (struct glyph_string *s)
22876 int k;
22878 if (s->left_overhang)
22880 int x = 0, i;
22881 struct glyph *glyphs = s->row->glyphs[s->area];
22882 int first = s->first_glyph - glyphs;
22884 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22885 x -= glyphs[i].pixel_width;
22887 k = i + 1;
22889 else
22890 k = -1;
22892 return k;
22896 /* Return the index of the first glyph preceding glyph string S that
22897 is overwriting S because of its right overhang. Value is -1 if no
22898 glyph in front of S overwrites S. */
22900 static int
22901 left_overwriting (struct glyph_string *s)
22903 int i, k, x;
22904 struct glyph *glyphs = s->row->glyphs[s->area];
22905 int first = s->first_glyph - glyphs;
22907 k = -1;
22908 x = 0;
22909 for (i = first - 1; i >= 0; --i)
22911 int left, right;
22912 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22913 if (x + right > 0)
22914 k = i;
22915 x -= glyphs[i].pixel_width;
22918 return k;
22922 /* Return the index of the last glyph following glyph string S that is
22923 overwritten by S because of S's right overhang. Value is -1 if
22924 no such glyph is found. */
22926 static int
22927 right_overwritten (struct glyph_string *s)
22929 int k = -1;
22931 if (s->right_overhang)
22933 int x = 0, i;
22934 struct glyph *glyphs = s->row->glyphs[s->area];
22935 int first = (s->first_glyph - glyphs
22936 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22937 int end = s->row->used[s->area];
22939 for (i = first; i < end && s->right_overhang > x; ++i)
22940 x += glyphs[i].pixel_width;
22942 k = i;
22945 return k;
22949 /* Return the index of the last glyph following glyph string S that
22950 overwrites S because of its left overhang. Value is negative
22951 if no such glyph is found. */
22953 static int
22954 right_overwriting (struct glyph_string *s)
22956 int i, k, x;
22957 int end = s->row->used[s->area];
22958 struct glyph *glyphs = s->row->glyphs[s->area];
22959 int first = (s->first_glyph - glyphs
22960 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22962 k = -1;
22963 x = 0;
22964 for (i = first; i < end; ++i)
22966 int left, right;
22967 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22968 if (x - left < 0)
22969 k = i;
22970 x += glyphs[i].pixel_width;
22973 return k;
22977 /* Set background width of glyph string S. START is the index of the
22978 first glyph following S. LAST_X is the right-most x-position + 1
22979 in the drawing area. */
22981 static void
22982 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22984 /* If the face of this glyph string has to be drawn to the end of
22985 the drawing area, set S->extends_to_end_of_line_p. */
22987 if (start == s->row->used[s->area]
22988 && s->area == TEXT_AREA
22989 && ((s->row->fill_line_p
22990 && (s->hl == DRAW_NORMAL_TEXT
22991 || s->hl == DRAW_IMAGE_RAISED
22992 || s->hl == DRAW_IMAGE_SUNKEN))
22993 || s->hl == DRAW_MOUSE_FACE))
22994 s->extends_to_end_of_line_p = 1;
22996 /* If S extends its face to the end of the line, set its
22997 background_width to the distance to the right edge of the drawing
22998 area. */
22999 if (s->extends_to_end_of_line_p)
23000 s->background_width = last_x - s->x + 1;
23001 else
23002 s->background_width = s->width;
23006 /* Compute overhangs and x-positions for glyph string S and its
23007 predecessors, or successors. X is the starting x-position for S.
23008 BACKWARD_P non-zero means process predecessors. */
23010 static void
23011 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23013 if (backward_p)
23015 while (s)
23017 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23018 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23019 x -= s->width;
23020 s->x = x;
23021 s = s->prev;
23024 else
23026 while (s)
23028 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23029 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23030 s->x = x;
23031 x += s->width;
23032 s = s->next;
23039 /* The following macros are only called from draw_glyphs below.
23040 They reference the following parameters of that function directly:
23041 `w', `row', `area', and `overlap_p'
23042 as well as the following local variables:
23043 `s', `f', and `hdc' (in W32) */
23045 #ifdef HAVE_NTGUI
23046 /* On W32, silently add local `hdc' variable to argument list of
23047 init_glyph_string. */
23048 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23049 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23050 #else
23051 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23052 init_glyph_string (s, char2b, w, row, area, start, hl)
23053 #endif
23055 /* Add a glyph string for a stretch glyph to the list of strings
23056 between HEAD and TAIL. START is the index of the stretch glyph in
23057 row area AREA of glyph row ROW. END is the index of the last glyph
23058 in that glyph row area. X is the current output position assigned
23059 to the new glyph string constructed. HL overrides that face of the
23060 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23061 is the right-most x-position of the drawing area. */
23063 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23064 and below -- keep them on one line. */
23065 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23066 do \
23068 s = alloca (sizeof *s); \
23069 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23070 START = fill_stretch_glyph_string (s, START, END); \
23071 append_glyph_string (&HEAD, &TAIL, s); \
23072 s->x = (X); \
23074 while (0)
23077 /* Add a glyph string for an image glyph to the list of strings
23078 between HEAD and TAIL. START is the index of the image glyph in
23079 row area AREA of glyph row ROW. END is the index of the last glyph
23080 in that glyph row area. X is the current output position assigned
23081 to the new glyph string constructed. HL overrides that face of the
23082 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23083 is the right-most x-position of the drawing area. */
23085 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23086 do \
23088 s = alloca (sizeof *s); \
23089 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23090 fill_image_glyph_string (s); \
23091 append_glyph_string (&HEAD, &TAIL, s); \
23092 ++START; \
23093 s->x = (X); \
23095 while (0)
23098 /* Add a glyph string for a sequence of character glyphs to the list
23099 of strings between HEAD and TAIL. START is the index of the first
23100 glyph in row area AREA of glyph row ROW that is part of the new
23101 glyph string. END is the index of the last glyph in that glyph row
23102 area. X is the current output position assigned to the new glyph
23103 string constructed. HL overrides that face of the glyph; e.g. it
23104 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23105 right-most x-position of the drawing area. */
23107 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23108 do \
23110 int face_id; \
23111 XChar2b *char2b; \
23113 face_id = (row)->glyphs[area][START].face_id; \
23115 s = alloca (sizeof *s); \
23116 char2b = alloca ((END - START) * sizeof *char2b); \
23117 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23118 append_glyph_string (&HEAD, &TAIL, s); \
23119 s->x = (X); \
23120 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23122 while (0)
23125 /* Add a glyph string for a composite sequence to the list of strings
23126 between HEAD and TAIL. START is the index of the first glyph in
23127 row area AREA of glyph row ROW that is part of the new glyph
23128 string. END is the index of the last glyph in that glyph row area.
23129 X is the current output position assigned to the new glyph string
23130 constructed. HL overrides that face of the glyph; e.g. it is
23131 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23132 x-position of the drawing area. */
23134 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23135 do { \
23136 int face_id = (row)->glyphs[area][START].face_id; \
23137 struct face *base_face = FACE_FROM_ID (f, face_id); \
23138 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23139 struct composition *cmp = composition_table[cmp_id]; \
23140 XChar2b *char2b; \
23141 struct glyph_string *first_s = NULL; \
23142 int n; \
23144 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23146 /* Make glyph_strings for each glyph sequence that is drawable by \
23147 the same face, and append them to HEAD/TAIL. */ \
23148 for (n = 0; n < cmp->glyph_len;) \
23150 s = alloca (sizeof *s); \
23151 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23152 append_glyph_string (&(HEAD), &(TAIL), s); \
23153 s->cmp = cmp; \
23154 s->cmp_from = n; \
23155 s->x = (X); \
23156 if (n == 0) \
23157 first_s = s; \
23158 n = fill_composite_glyph_string (s, base_face, overlaps); \
23161 ++START; \
23162 s = first_s; \
23163 } while (0)
23166 /* Add a glyph string for a glyph-string sequence to the list of strings
23167 between HEAD and TAIL. */
23169 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23170 do { \
23171 int face_id; \
23172 XChar2b *char2b; \
23173 Lisp_Object gstring; \
23175 face_id = (row)->glyphs[area][START].face_id; \
23176 gstring = (composition_gstring_from_id \
23177 ((row)->glyphs[area][START].u.cmp.id)); \
23178 s = alloca (sizeof *s); \
23179 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23180 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23181 append_glyph_string (&(HEAD), &(TAIL), s); \
23182 s->x = (X); \
23183 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23184 } while (0)
23187 /* Add a glyph string for a sequence of glyphless character's glyphs
23188 to the list of strings between HEAD and TAIL. The meanings of
23189 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23191 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23192 do \
23194 int face_id; \
23196 face_id = (row)->glyphs[area][START].face_id; \
23198 s = alloca (sizeof *s); \
23199 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23200 append_glyph_string (&HEAD, &TAIL, s); \
23201 s->x = (X); \
23202 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23203 overlaps); \
23205 while (0)
23208 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23209 of AREA of glyph row ROW on window W between indices START and END.
23210 HL overrides the face for drawing glyph strings, e.g. it is
23211 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23212 x-positions of the drawing area.
23214 This is an ugly monster macro construct because we must use alloca
23215 to allocate glyph strings (because draw_glyphs can be called
23216 asynchronously). */
23218 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23219 do \
23221 HEAD = TAIL = NULL; \
23222 while (START < END) \
23224 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23225 switch (first_glyph->type) \
23227 case CHAR_GLYPH: \
23228 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23229 HL, X, LAST_X); \
23230 break; \
23232 case COMPOSITE_GLYPH: \
23233 if (first_glyph->u.cmp.automatic) \
23234 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23235 HL, X, LAST_X); \
23236 else \
23237 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23238 HL, X, LAST_X); \
23239 break; \
23241 case STRETCH_GLYPH: \
23242 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23243 HL, X, LAST_X); \
23244 break; \
23246 case IMAGE_GLYPH: \
23247 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23248 HL, X, LAST_X); \
23249 break; \
23251 case GLYPHLESS_GLYPH: \
23252 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23253 HL, X, LAST_X); \
23254 break; \
23256 default: \
23257 emacs_abort (); \
23260 if (s) \
23262 set_glyph_string_background_width (s, START, LAST_X); \
23263 (X) += s->width; \
23266 } while (0)
23269 /* Draw glyphs between START and END in AREA of ROW on window W,
23270 starting at x-position X. X is relative to AREA in W. HL is a
23271 face-override with the following meaning:
23273 DRAW_NORMAL_TEXT draw normally
23274 DRAW_CURSOR draw in cursor face
23275 DRAW_MOUSE_FACE draw in mouse face.
23276 DRAW_INVERSE_VIDEO draw in mode line face
23277 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23278 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23280 If OVERLAPS is non-zero, draw only the foreground of characters and
23281 clip to the physical height of ROW. Non-zero value also defines
23282 the overlapping part to be drawn:
23284 OVERLAPS_PRED overlap with preceding rows
23285 OVERLAPS_SUCC overlap with succeeding rows
23286 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23287 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23289 Value is the x-position reached, relative to AREA of W. */
23291 static int
23292 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23293 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23294 enum draw_glyphs_face hl, int overlaps)
23296 struct glyph_string *head, *tail;
23297 struct glyph_string *s;
23298 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23299 int i, j, x_reached, last_x, area_left = 0;
23300 struct frame *f = XFRAME (WINDOW_FRAME (w));
23301 DECLARE_HDC (hdc);
23303 ALLOCATE_HDC (hdc, f);
23305 /* Let's rather be paranoid than getting a SEGV. */
23306 end = min (end, row->used[area]);
23307 start = clip_to_bounds (0, start, end);
23309 /* Translate X to frame coordinates. Set last_x to the right
23310 end of the drawing area. */
23311 if (row->full_width_p)
23313 /* X is relative to the left edge of W, without scroll bars
23314 or fringes. */
23315 area_left = WINDOW_LEFT_EDGE_X (w);
23316 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23318 else
23320 area_left = window_box_left (w, area);
23321 last_x = area_left + window_box_width (w, area);
23323 x += area_left;
23325 /* Build a doubly-linked list of glyph_string structures between
23326 head and tail from what we have to draw. Note that the macro
23327 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23328 the reason we use a separate variable `i'. */
23329 i = start;
23330 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23331 if (tail)
23332 x_reached = tail->x + tail->background_width;
23333 else
23334 x_reached = x;
23336 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23337 the row, redraw some glyphs in front or following the glyph
23338 strings built above. */
23339 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23341 struct glyph_string *h, *t;
23342 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23343 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23344 int check_mouse_face = 0;
23345 int dummy_x = 0;
23347 /* If mouse highlighting is on, we may need to draw adjacent
23348 glyphs using mouse-face highlighting. */
23349 if (area == TEXT_AREA && row->mouse_face_p
23350 && hlinfo->mouse_face_beg_row >= 0
23351 && hlinfo->mouse_face_end_row >= 0)
23353 struct glyph_row *mouse_beg_row, *mouse_end_row;
23355 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23356 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23358 if (row >= mouse_beg_row && row <= mouse_end_row)
23360 check_mouse_face = 1;
23361 mouse_beg_col = (row == mouse_beg_row)
23362 ? hlinfo->mouse_face_beg_col : 0;
23363 mouse_end_col = (row == mouse_end_row)
23364 ? hlinfo->mouse_face_end_col
23365 : row->used[TEXT_AREA];
23369 /* Compute overhangs for all glyph strings. */
23370 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23371 for (s = head; s; s = s->next)
23372 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23374 /* Prepend glyph strings for glyphs in front of the first glyph
23375 string that are overwritten because of the first glyph
23376 string's left overhang. The background of all strings
23377 prepended must be drawn because the first glyph string
23378 draws over it. */
23379 i = left_overwritten (head);
23380 if (i >= 0)
23382 enum draw_glyphs_face overlap_hl;
23384 /* If this row contains mouse highlighting, attempt to draw
23385 the overlapped glyphs with the correct highlight. This
23386 code fails if the overlap encompasses more than one glyph
23387 and mouse-highlight spans only some of these glyphs.
23388 However, making it work perfectly involves a lot more
23389 code, and I don't know if the pathological case occurs in
23390 practice, so we'll stick to this for now. --- cyd */
23391 if (check_mouse_face
23392 && mouse_beg_col < start && mouse_end_col > i)
23393 overlap_hl = DRAW_MOUSE_FACE;
23394 else
23395 overlap_hl = DRAW_NORMAL_TEXT;
23397 j = i;
23398 BUILD_GLYPH_STRINGS (j, start, h, t,
23399 overlap_hl, dummy_x, last_x);
23400 start = i;
23401 compute_overhangs_and_x (t, head->x, 1);
23402 prepend_glyph_string_lists (&head, &tail, h, t);
23403 clip_head = head;
23406 /* Prepend glyph strings for glyphs in front of the first glyph
23407 string that overwrite that glyph string because of their
23408 right overhang. For these strings, only the foreground must
23409 be drawn, because it draws over the glyph string at `head'.
23410 The background must not be drawn because this would overwrite
23411 right overhangs of preceding glyphs for which no glyph
23412 strings exist. */
23413 i = left_overwriting (head);
23414 if (i >= 0)
23416 enum draw_glyphs_face overlap_hl;
23418 if (check_mouse_face
23419 && mouse_beg_col < start && mouse_end_col > i)
23420 overlap_hl = DRAW_MOUSE_FACE;
23421 else
23422 overlap_hl = DRAW_NORMAL_TEXT;
23424 clip_head = head;
23425 BUILD_GLYPH_STRINGS (i, start, h, t,
23426 overlap_hl, dummy_x, last_x);
23427 for (s = h; s; s = s->next)
23428 s->background_filled_p = 1;
23429 compute_overhangs_and_x (t, head->x, 1);
23430 prepend_glyph_string_lists (&head, &tail, h, t);
23433 /* Append glyphs strings for glyphs following the last glyph
23434 string tail that are overwritten by tail. The background of
23435 these strings has to be drawn because tail's foreground draws
23436 over it. */
23437 i = right_overwritten (tail);
23438 if (i >= 0)
23440 enum draw_glyphs_face overlap_hl;
23442 if (check_mouse_face
23443 && mouse_beg_col < i && mouse_end_col > end)
23444 overlap_hl = DRAW_MOUSE_FACE;
23445 else
23446 overlap_hl = DRAW_NORMAL_TEXT;
23448 BUILD_GLYPH_STRINGS (end, i, h, t,
23449 overlap_hl, x, last_x);
23450 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23451 we don't have `end = i;' here. */
23452 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23453 append_glyph_string_lists (&head, &tail, h, t);
23454 clip_tail = tail;
23457 /* Append glyph strings for glyphs following the last glyph
23458 string tail that overwrite tail. The foreground of such
23459 glyphs has to be drawn because it writes into the background
23460 of tail. The background must not be drawn because it could
23461 paint over the foreground of following glyphs. */
23462 i = right_overwriting (tail);
23463 if (i >= 0)
23465 enum draw_glyphs_face overlap_hl;
23466 if (check_mouse_face
23467 && mouse_beg_col < i && mouse_end_col > end)
23468 overlap_hl = DRAW_MOUSE_FACE;
23469 else
23470 overlap_hl = DRAW_NORMAL_TEXT;
23472 clip_tail = tail;
23473 i++; /* We must include the Ith glyph. */
23474 BUILD_GLYPH_STRINGS (end, i, h, t,
23475 overlap_hl, x, last_x);
23476 for (s = h; s; s = s->next)
23477 s->background_filled_p = 1;
23478 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23479 append_glyph_string_lists (&head, &tail, h, t);
23481 if (clip_head || clip_tail)
23482 for (s = head; s; s = s->next)
23484 s->clip_head = clip_head;
23485 s->clip_tail = clip_tail;
23489 /* Draw all strings. */
23490 for (s = head; s; s = s->next)
23491 FRAME_RIF (f)->draw_glyph_string (s);
23493 #ifndef HAVE_NS
23494 /* When focus a sole frame and move horizontally, this sets on_p to 0
23495 causing a failure to erase prev cursor position. */
23496 if (area == TEXT_AREA
23497 && !row->full_width_p
23498 /* When drawing overlapping rows, only the glyph strings'
23499 foreground is drawn, which doesn't erase a cursor
23500 completely. */
23501 && !overlaps)
23503 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23504 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23505 : (tail ? tail->x + tail->background_width : x));
23506 x0 -= area_left;
23507 x1 -= area_left;
23509 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23510 row->y, MATRIX_ROW_BOTTOM_Y (row));
23512 #endif
23514 /* Value is the x-position up to which drawn, relative to AREA of W.
23515 This doesn't include parts drawn because of overhangs. */
23516 if (row->full_width_p)
23517 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23518 else
23519 x_reached -= area_left;
23521 RELEASE_HDC (hdc, f);
23523 return x_reached;
23526 /* Expand row matrix if too narrow. Don't expand if area
23527 is not present. */
23529 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23531 if (!fonts_changed_p \
23532 && (it->glyph_row->glyphs[area] \
23533 < it->glyph_row->glyphs[area + 1])) \
23535 it->w->ncols_scale_factor++; \
23536 fonts_changed_p = 1; \
23540 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23541 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23543 static void
23544 append_glyph (struct it *it)
23546 struct glyph *glyph;
23547 enum glyph_row_area area = it->area;
23549 eassert (it->glyph_row);
23550 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23552 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23553 if (glyph < it->glyph_row->glyphs[area + 1])
23555 /* If the glyph row is reversed, we need to prepend the glyph
23556 rather than append it. */
23557 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23559 struct glyph *g;
23561 /* Make room for the additional glyph. */
23562 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23563 g[1] = *g;
23564 glyph = it->glyph_row->glyphs[area];
23566 glyph->charpos = CHARPOS (it->position);
23567 glyph->object = it->object;
23568 if (it->pixel_width > 0)
23570 glyph->pixel_width = it->pixel_width;
23571 glyph->padding_p = 0;
23573 else
23575 /* Assure at least 1-pixel width. Otherwise, cursor can't
23576 be displayed correctly. */
23577 glyph->pixel_width = 1;
23578 glyph->padding_p = 1;
23580 glyph->ascent = it->ascent;
23581 glyph->descent = it->descent;
23582 glyph->voffset = it->voffset;
23583 glyph->type = CHAR_GLYPH;
23584 glyph->avoid_cursor_p = it->avoid_cursor_p;
23585 glyph->multibyte_p = it->multibyte_p;
23586 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23588 /* In R2L rows, the left and the right box edges need to be
23589 drawn in reverse direction. */
23590 glyph->right_box_line_p = it->start_of_box_run_p;
23591 glyph->left_box_line_p = it->end_of_box_run_p;
23593 else
23595 glyph->left_box_line_p = it->start_of_box_run_p;
23596 glyph->right_box_line_p = it->end_of_box_run_p;
23598 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23599 || it->phys_descent > it->descent);
23600 glyph->glyph_not_available_p = it->glyph_not_available_p;
23601 glyph->face_id = it->face_id;
23602 glyph->u.ch = it->char_to_display;
23603 glyph->slice.img = null_glyph_slice;
23604 glyph->font_type = FONT_TYPE_UNKNOWN;
23605 if (it->bidi_p)
23607 glyph->resolved_level = it->bidi_it.resolved_level;
23608 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23609 emacs_abort ();
23610 glyph->bidi_type = it->bidi_it.type;
23612 else
23614 glyph->resolved_level = 0;
23615 glyph->bidi_type = UNKNOWN_BT;
23617 ++it->glyph_row->used[area];
23619 else
23620 IT_EXPAND_MATRIX_WIDTH (it, area);
23623 /* Store one glyph for the composition IT->cmp_it.id in
23624 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23625 non-null. */
23627 static void
23628 append_composite_glyph (struct it *it)
23630 struct glyph *glyph;
23631 enum glyph_row_area area = it->area;
23633 eassert (it->glyph_row);
23635 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23636 if (glyph < it->glyph_row->glyphs[area + 1])
23638 /* If the glyph row is reversed, we need to prepend the glyph
23639 rather than append it. */
23640 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23642 struct glyph *g;
23644 /* Make room for the new glyph. */
23645 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23646 g[1] = *g;
23647 glyph = it->glyph_row->glyphs[it->area];
23649 glyph->charpos = it->cmp_it.charpos;
23650 glyph->object = it->object;
23651 glyph->pixel_width = it->pixel_width;
23652 glyph->ascent = it->ascent;
23653 glyph->descent = it->descent;
23654 glyph->voffset = it->voffset;
23655 glyph->type = COMPOSITE_GLYPH;
23656 if (it->cmp_it.ch < 0)
23658 glyph->u.cmp.automatic = 0;
23659 glyph->u.cmp.id = it->cmp_it.id;
23660 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23662 else
23664 glyph->u.cmp.automatic = 1;
23665 glyph->u.cmp.id = it->cmp_it.id;
23666 glyph->slice.cmp.from = it->cmp_it.from;
23667 glyph->slice.cmp.to = it->cmp_it.to - 1;
23669 glyph->avoid_cursor_p = it->avoid_cursor_p;
23670 glyph->multibyte_p = it->multibyte_p;
23671 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23673 /* In R2L rows, the left and the right box edges need to be
23674 drawn in reverse direction. */
23675 glyph->right_box_line_p = it->start_of_box_run_p;
23676 glyph->left_box_line_p = it->end_of_box_run_p;
23678 else
23680 glyph->left_box_line_p = it->start_of_box_run_p;
23681 glyph->right_box_line_p = it->end_of_box_run_p;
23683 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23684 || it->phys_descent > it->descent);
23685 glyph->padding_p = 0;
23686 glyph->glyph_not_available_p = 0;
23687 glyph->face_id = it->face_id;
23688 glyph->font_type = FONT_TYPE_UNKNOWN;
23689 if (it->bidi_p)
23691 glyph->resolved_level = it->bidi_it.resolved_level;
23692 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23693 emacs_abort ();
23694 glyph->bidi_type = it->bidi_it.type;
23696 ++it->glyph_row->used[area];
23698 else
23699 IT_EXPAND_MATRIX_WIDTH (it, area);
23703 /* Change IT->ascent and IT->height according to the setting of
23704 IT->voffset. */
23706 static void
23707 take_vertical_position_into_account (struct it *it)
23709 if (it->voffset)
23711 if (it->voffset < 0)
23712 /* Increase the ascent so that we can display the text higher
23713 in the line. */
23714 it->ascent -= it->voffset;
23715 else
23716 /* Increase the descent so that we can display the text lower
23717 in the line. */
23718 it->descent += it->voffset;
23723 /* Produce glyphs/get display metrics for the image IT is loaded with.
23724 See the description of struct display_iterator in dispextern.h for
23725 an overview of struct display_iterator. */
23727 static void
23728 produce_image_glyph (struct it *it)
23730 struct image *img;
23731 struct face *face;
23732 int glyph_ascent, crop;
23733 struct glyph_slice slice;
23735 eassert (it->what == IT_IMAGE);
23737 face = FACE_FROM_ID (it->f, it->face_id);
23738 eassert (face);
23739 /* Make sure X resources of the face is loaded. */
23740 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23742 if (it->image_id < 0)
23744 /* Fringe bitmap. */
23745 it->ascent = it->phys_ascent = 0;
23746 it->descent = it->phys_descent = 0;
23747 it->pixel_width = 0;
23748 it->nglyphs = 0;
23749 return;
23752 img = IMAGE_FROM_ID (it->f, it->image_id);
23753 eassert (img);
23754 /* Make sure X resources of the image is loaded. */
23755 prepare_image_for_display (it->f, img);
23757 slice.x = slice.y = 0;
23758 slice.width = img->width;
23759 slice.height = img->height;
23761 if (INTEGERP (it->slice.x))
23762 slice.x = XINT (it->slice.x);
23763 else if (FLOATP (it->slice.x))
23764 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23766 if (INTEGERP (it->slice.y))
23767 slice.y = XINT (it->slice.y);
23768 else if (FLOATP (it->slice.y))
23769 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23771 if (INTEGERP (it->slice.width))
23772 slice.width = XINT (it->slice.width);
23773 else if (FLOATP (it->slice.width))
23774 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23776 if (INTEGERP (it->slice.height))
23777 slice.height = XINT (it->slice.height);
23778 else if (FLOATP (it->slice.height))
23779 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23781 if (slice.x >= img->width)
23782 slice.x = img->width;
23783 if (slice.y >= img->height)
23784 slice.y = img->height;
23785 if (slice.x + slice.width >= img->width)
23786 slice.width = img->width - slice.x;
23787 if (slice.y + slice.height > img->height)
23788 slice.height = img->height - slice.y;
23790 if (slice.width == 0 || slice.height == 0)
23791 return;
23793 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23795 it->descent = slice.height - glyph_ascent;
23796 if (slice.y == 0)
23797 it->descent += img->vmargin;
23798 if (slice.y + slice.height == img->height)
23799 it->descent += img->vmargin;
23800 it->phys_descent = it->descent;
23802 it->pixel_width = slice.width;
23803 if (slice.x == 0)
23804 it->pixel_width += img->hmargin;
23805 if (slice.x + slice.width == img->width)
23806 it->pixel_width += img->hmargin;
23808 /* It's quite possible for images to have an ascent greater than
23809 their height, so don't get confused in that case. */
23810 if (it->descent < 0)
23811 it->descent = 0;
23813 it->nglyphs = 1;
23815 if (face->box != FACE_NO_BOX)
23817 if (face->box_line_width > 0)
23819 if (slice.y == 0)
23820 it->ascent += face->box_line_width;
23821 if (slice.y + slice.height == img->height)
23822 it->descent += face->box_line_width;
23825 if (it->start_of_box_run_p && slice.x == 0)
23826 it->pixel_width += eabs (face->box_line_width);
23827 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23828 it->pixel_width += eabs (face->box_line_width);
23831 take_vertical_position_into_account (it);
23833 /* Automatically crop wide image glyphs at right edge so we can
23834 draw the cursor on same display row. */
23835 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23836 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23838 it->pixel_width -= crop;
23839 slice.width -= crop;
23842 if (it->glyph_row)
23844 struct glyph *glyph;
23845 enum glyph_row_area area = it->area;
23847 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23848 if (glyph < it->glyph_row->glyphs[area + 1])
23850 glyph->charpos = CHARPOS (it->position);
23851 glyph->object = it->object;
23852 glyph->pixel_width = it->pixel_width;
23853 glyph->ascent = glyph_ascent;
23854 glyph->descent = it->descent;
23855 glyph->voffset = it->voffset;
23856 glyph->type = IMAGE_GLYPH;
23857 glyph->avoid_cursor_p = it->avoid_cursor_p;
23858 glyph->multibyte_p = it->multibyte_p;
23859 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23861 /* In R2L rows, the left and the right box edges need to be
23862 drawn in reverse direction. */
23863 glyph->right_box_line_p = it->start_of_box_run_p;
23864 glyph->left_box_line_p = it->end_of_box_run_p;
23866 else
23868 glyph->left_box_line_p = it->start_of_box_run_p;
23869 glyph->right_box_line_p = it->end_of_box_run_p;
23871 glyph->overlaps_vertically_p = 0;
23872 glyph->padding_p = 0;
23873 glyph->glyph_not_available_p = 0;
23874 glyph->face_id = it->face_id;
23875 glyph->u.img_id = img->id;
23876 glyph->slice.img = slice;
23877 glyph->font_type = FONT_TYPE_UNKNOWN;
23878 if (it->bidi_p)
23880 glyph->resolved_level = it->bidi_it.resolved_level;
23881 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23882 emacs_abort ();
23883 glyph->bidi_type = it->bidi_it.type;
23885 ++it->glyph_row->used[area];
23887 else
23888 IT_EXPAND_MATRIX_WIDTH (it, area);
23893 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23894 of the glyph, WIDTH and HEIGHT are the width and height of the
23895 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23897 static void
23898 append_stretch_glyph (struct it *it, Lisp_Object object,
23899 int width, int height, int ascent)
23901 struct glyph *glyph;
23902 enum glyph_row_area area = it->area;
23904 eassert (ascent >= 0 && ascent <= height);
23906 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23907 if (glyph < it->glyph_row->glyphs[area + 1])
23909 /* If the glyph row is reversed, we need to prepend the glyph
23910 rather than append it. */
23911 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23913 struct glyph *g;
23915 /* Make room for the additional glyph. */
23916 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23917 g[1] = *g;
23918 glyph = it->glyph_row->glyphs[area];
23920 glyph->charpos = CHARPOS (it->position);
23921 glyph->object = object;
23922 glyph->pixel_width = width;
23923 glyph->ascent = ascent;
23924 glyph->descent = height - ascent;
23925 glyph->voffset = it->voffset;
23926 glyph->type = STRETCH_GLYPH;
23927 glyph->avoid_cursor_p = it->avoid_cursor_p;
23928 glyph->multibyte_p = it->multibyte_p;
23929 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23931 /* In R2L rows, the left and the right box edges need to be
23932 drawn in reverse direction. */
23933 glyph->right_box_line_p = it->start_of_box_run_p;
23934 glyph->left_box_line_p = it->end_of_box_run_p;
23936 else
23938 glyph->left_box_line_p = it->start_of_box_run_p;
23939 glyph->right_box_line_p = it->end_of_box_run_p;
23941 glyph->overlaps_vertically_p = 0;
23942 glyph->padding_p = 0;
23943 glyph->glyph_not_available_p = 0;
23944 glyph->face_id = it->face_id;
23945 glyph->u.stretch.ascent = ascent;
23946 glyph->u.stretch.height = height;
23947 glyph->slice.img = null_glyph_slice;
23948 glyph->font_type = FONT_TYPE_UNKNOWN;
23949 if (it->bidi_p)
23951 glyph->resolved_level = it->bidi_it.resolved_level;
23952 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23953 emacs_abort ();
23954 glyph->bidi_type = it->bidi_it.type;
23956 else
23958 glyph->resolved_level = 0;
23959 glyph->bidi_type = UNKNOWN_BT;
23961 ++it->glyph_row->used[area];
23963 else
23964 IT_EXPAND_MATRIX_WIDTH (it, area);
23967 #endif /* HAVE_WINDOW_SYSTEM */
23969 /* Produce a stretch glyph for iterator IT. IT->object is the value
23970 of the glyph property displayed. The value must be a list
23971 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23972 being recognized:
23974 1. `:width WIDTH' specifies that the space should be WIDTH *
23975 canonical char width wide. WIDTH may be an integer or floating
23976 point number.
23978 2. `:relative-width FACTOR' specifies that the width of the stretch
23979 should be computed from the width of the first character having the
23980 `glyph' property, and should be FACTOR times that width.
23982 3. `:align-to HPOS' specifies that the space should be wide enough
23983 to reach HPOS, a value in canonical character units.
23985 Exactly one of the above pairs must be present.
23987 4. `:height HEIGHT' specifies that the height of the stretch produced
23988 should be HEIGHT, measured in canonical character units.
23990 5. `:relative-height FACTOR' specifies that the height of the
23991 stretch should be FACTOR times the height of the characters having
23992 the glyph property.
23994 Either none or exactly one of 4 or 5 must be present.
23996 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23997 of the stretch should be used for the ascent of the stretch.
23998 ASCENT must be in the range 0 <= ASCENT <= 100. */
24000 void
24001 produce_stretch_glyph (struct it *it)
24003 /* (space :width WIDTH :height HEIGHT ...) */
24004 Lisp_Object prop, plist;
24005 int width = 0, height = 0, align_to = -1;
24006 int zero_width_ok_p = 0;
24007 double tem;
24008 struct font *font = NULL;
24010 #ifdef HAVE_WINDOW_SYSTEM
24011 int ascent = 0;
24012 int zero_height_ok_p = 0;
24014 if (FRAME_WINDOW_P (it->f))
24016 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24017 font = face->font ? face->font : FRAME_FONT (it->f);
24018 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24020 #endif
24022 /* List should start with `space'. */
24023 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24024 plist = XCDR (it->object);
24026 /* Compute the width of the stretch. */
24027 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24028 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24030 /* Absolute width `:width WIDTH' specified and valid. */
24031 zero_width_ok_p = 1;
24032 width = (int)tem;
24034 #ifdef HAVE_WINDOW_SYSTEM
24035 else if (FRAME_WINDOW_P (it->f)
24036 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24038 /* Relative width `:relative-width FACTOR' specified and valid.
24039 Compute the width of the characters having the `glyph'
24040 property. */
24041 struct it it2;
24042 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24044 it2 = *it;
24045 if (it->multibyte_p)
24046 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24047 else
24049 it2.c = it2.char_to_display = *p, it2.len = 1;
24050 if (! ASCII_CHAR_P (it2.c))
24051 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24054 it2.glyph_row = NULL;
24055 it2.what = IT_CHARACTER;
24056 x_produce_glyphs (&it2);
24057 width = NUMVAL (prop) * it2.pixel_width;
24059 #endif /* HAVE_WINDOW_SYSTEM */
24060 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24061 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24063 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24064 align_to = (align_to < 0
24066 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24067 else if (align_to < 0)
24068 align_to = window_box_left_offset (it->w, TEXT_AREA);
24069 width = max (0, (int)tem + align_to - it->current_x);
24070 zero_width_ok_p = 1;
24072 else
24073 /* Nothing specified -> width defaults to canonical char width. */
24074 width = FRAME_COLUMN_WIDTH (it->f);
24076 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24077 width = 1;
24079 #ifdef HAVE_WINDOW_SYSTEM
24080 /* Compute height. */
24081 if (FRAME_WINDOW_P (it->f))
24083 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24084 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24086 height = (int)tem;
24087 zero_height_ok_p = 1;
24089 else if (prop = Fplist_get (plist, QCrelative_height),
24090 NUMVAL (prop) > 0)
24091 height = FONT_HEIGHT (font) * NUMVAL (prop);
24092 else
24093 height = FONT_HEIGHT (font);
24095 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24096 height = 1;
24098 /* Compute percentage of height used for ascent. If
24099 `:ascent ASCENT' is present and valid, use that. Otherwise,
24100 derive the ascent from the font in use. */
24101 if (prop = Fplist_get (plist, QCascent),
24102 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24103 ascent = height * NUMVAL (prop) / 100.0;
24104 else if (!NILP (prop)
24105 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24106 ascent = min (max (0, (int)tem), height);
24107 else
24108 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24110 else
24111 #endif /* HAVE_WINDOW_SYSTEM */
24112 height = 1;
24114 if (width > 0 && it->line_wrap != TRUNCATE
24115 && it->current_x + width > it->last_visible_x)
24117 width = it->last_visible_x - it->current_x;
24118 #ifdef HAVE_WINDOW_SYSTEM
24119 /* Subtract one more pixel from the stretch width, but only on
24120 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24121 width -= FRAME_WINDOW_P (it->f);
24122 #endif
24125 if (width > 0 && height > 0 && it->glyph_row)
24127 Lisp_Object o_object = it->object;
24128 Lisp_Object object = it->stack[it->sp - 1].string;
24129 int n = width;
24131 if (!STRINGP (object))
24132 object = it->w->contents;
24133 #ifdef HAVE_WINDOW_SYSTEM
24134 if (FRAME_WINDOW_P (it->f))
24135 append_stretch_glyph (it, object, width, height, ascent);
24136 else
24137 #endif
24139 it->object = object;
24140 it->char_to_display = ' ';
24141 it->pixel_width = it->len = 1;
24142 while (n--)
24143 tty_append_glyph (it);
24144 it->object = o_object;
24148 it->pixel_width = width;
24149 #ifdef HAVE_WINDOW_SYSTEM
24150 if (FRAME_WINDOW_P (it->f))
24152 it->ascent = it->phys_ascent = ascent;
24153 it->descent = it->phys_descent = height - it->ascent;
24154 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24155 take_vertical_position_into_account (it);
24157 else
24158 #endif
24159 it->nglyphs = width;
24162 /* Get information about special display element WHAT in an
24163 environment described by IT. WHAT is one of IT_TRUNCATION or
24164 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24165 non-null glyph_row member. This function ensures that fields like
24166 face_id, c, len of IT are left untouched. */
24168 static void
24169 produce_special_glyphs (struct it *it, enum display_element_type what)
24171 struct it temp_it;
24172 Lisp_Object gc;
24173 GLYPH glyph;
24175 temp_it = *it;
24176 temp_it.object = make_number (0);
24177 memset (&temp_it.current, 0, sizeof temp_it.current);
24179 if (what == IT_CONTINUATION)
24181 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24182 if (it->bidi_it.paragraph_dir == R2L)
24183 SET_GLYPH_FROM_CHAR (glyph, '/');
24184 else
24185 SET_GLYPH_FROM_CHAR (glyph, '\\');
24186 if (it->dp
24187 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24189 /* FIXME: Should we mirror GC for R2L lines? */
24190 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24191 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24194 else if (what == IT_TRUNCATION)
24196 /* Truncation glyph. */
24197 SET_GLYPH_FROM_CHAR (glyph, '$');
24198 if (it->dp
24199 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24201 /* FIXME: Should we mirror GC for R2L lines? */
24202 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24203 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24206 else
24207 emacs_abort ();
24209 #ifdef HAVE_WINDOW_SYSTEM
24210 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24211 is turned off, we precede the truncation/continuation glyphs by a
24212 stretch glyph whose width is computed such that these special
24213 glyphs are aligned at the window margin, even when very different
24214 fonts are used in different glyph rows. */
24215 if (FRAME_WINDOW_P (temp_it.f)
24216 /* init_iterator calls this with it->glyph_row == NULL, and it
24217 wants only the pixel width of the truncation/continuation
24218 glyphs. */
24219 && temp_it.glyph_row
24220 /* insert_left_trunc_glyphs calls us at the beginning of the
24221 row, and it has its own calculation of the stretch glyph
24222 width. */
24223 && temp_it.glyph_row->used[TEXT_AREA] > 0
24224 && (temp_it.glyph_row->reversed_p
24225 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24226 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24228 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24230 if (stretch_width > 0)
24232 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24233 struct font *font =
24234 face->font ? face->font : FRAME_FONT (temp_it.f);
24235 int stretch_ascent =
24236 (((temp_it.ascent + temp_it.descent)
24237 * FONT_BASE (font)) / FONT_HEIGHT (font));
24239 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24240 temp_it.ascent + temp_it.descent,
24241 stretch_ascent);
24244 #endif
24246 temp_it.dp = NULL;
24247 temp_it.what = IT_CHARACTER;
24248 temp_it.len = 1;
24249 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24250 temp_it.face_id = GLYPH_FACE (glyph);
24251 temp_it.len = CHAR_BYTES (temp_it.c);
24253 PRODUCE_GLYPHS (&temp_it);
24254 it->pixel_width = temp_it.pixel_width;
24255 it->nglyphs = temp_it.pixel_width;
24258 #ifdef HAVE_WINDOW_SYSTEM
24260 /* Calculate line-height and line-spacing properties.
24261 An integer value specifies explicit pixel value.
24262 A float value specifies relative value to current face height.
24263 A cons (float . face-name) specifies relative value to
24264 height of specified face font.
24266 Returns height in pixels, or nil. */
24269 static Lisp_Object
24270 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24271 int boff, int override)
24273 Lisp_Object face_name = Qnil;
24274 int ascent, descent, height;
24276 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24277 return val;
24279 if (CONSP (val))
24281 face_name = XCAR (val);
24282 val = XCDR (val);
24283 if (!NUMBERP (val))
24284 val = make_number (1);
24285 if (NILP (face_name))
24287 height = it->ascent + it->descent;
24288 goto scale;
24292 if (NILP (face_name))
24294 font = FRAME_FONT (it->f);
24295 boff = FRAME_BASELINE_OFFSET (it->f);
24297 else if (EQ (face_name, Qt))
24299 override = 0;
24301 else
24303 int face_id;
24304 struct face *face;
24306 face_id = lookup_named_face (it->f, face_name, 0);
24307 if (face_id < 0)
24308 return make_number (-1);
24310 face = FACE_FROM_ID (it->f, face_id);
24311 font = face->font;
24312 if (font == NULL)
24313 return make_number (-1);
24314 boff = font->baseline_offset;
24315 if (font->vertical_centering)
24316 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24319 ascent = FONT_BASE (font) + boff;
24320 descent = FONT_DESCENT (font) - boff;
24322 if (override)
24324 it->override_ascent = ascent;
24325 it->override_descent = descent;
24326 it->override_boff = boff;
24329 height = ascent + descent;
24331 scale:
24332 if (FLOATP (val))
24333 height = (int)(XFLOAT_DATA (val) * height);
24334 else if (INTEGERP (val))
24335 height *= XINT (val);
24337 return make_number (height);
24341 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24342 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24343 and only if this is for a character for which no font was found.
24345 If the display method (it->glyphless_method) is
24346 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24347 length of the acronym or the hexadecimal string, UPPER_XOFF and
24348 UPPER_YOFF are pixel offsets for the upper part of the string,
24349 LOWER_XOFF and LOWER_YOFF are for the lower part.
24351 For the other display methods, LEN through LOWER_YOFF are zero. */
24353 static void
24354 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24355 short upper_xoff, short upper_yoff,
24356 short lower_xoff, short lower_yoff)
24358 struct glyph *glyph;
24359 enum glyph_row_area area = it->area;
24361 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24362 if (glyph < it->glyph_row->glyphs[area + 1])
24364 /* If the glyph row is reversed, we need to prepend the glyph
24365 rather than append it. */
24366 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24368 struct glyph *g;
24370 /* Make room for the additional glyph. */
24371 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24372 g[1] = *g;
24373 glyph = it->glyph_row->glyphs[area];
24375 glyph->charpos = CHARPOS (it->position);
24376 glyph->object = it->object;
24377 glyph->pixel_width = it->pixel_width;
24378 glyph->ascent = it->ascent;
24379 glyph->descent = it->descent;
24380 glyph->voffset = it->voffset;
24381 glyph->type = GLYPHLESS_GLYPH;
24382 glyph->u.glyphless.method = it->glyphless_method;
24383 glyph->u.glyphless.for_no_font = for_no_font;
24384 glyph->u.glyphless.len = len;
24385 glyph->u.glyphless.ch = it->c;
24386 glyph->slice.glyphless.upper_xoff = upper_xoff;
24387 glyph->slice.glyphless.upper_yoff = upper_yoff;
24388 glyph->slice.glyphless.lower_xoff = lower_xoff;
24389 glyph->slice.glyphless.lower_yoff = lower_yoff;
24390 glyph->avoid_cursor_p = it->avoid_cursor_p;
24391 glyph->multibyte_p = it->multibyte_p;
24392 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24394 /* In R2L rows, the left and the right box edges need to be
24395 drawn in reverse direction. */
24396 glyph->right_box_line_p = it->start_of_box_run_p;
24397 glyph->left_box_line_p = it->end_of_box_run_p;
24399 else
24401 glyph->left_box_line_p = it->start_of_box_run_p;
24402 glyph->right_box_line_p = it->end_of_box_run_p;
24404 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24405 || it->phys_descent > it->descent);
24406 glyph->padding_p = 0;
24407 glyph->glyph_not_available_p = 0;
24408 glyph->face_id = face_id;
24409 glyph->font_type = FONT_TYPE_UNKNOWN;
24410 if (it->bidi_p)
24412 glyph->resolved_level = it->bidi_it.resolved_level;
24413 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24414 emacs_abort ();
24415 glyph->bidi_type = it->bidi_it.type;
24417 ++it->glyph_row->used[area];
24419 else
24420 IT_EXPAND_MATRIX_WIDTH (it, area);
24424 /* Produce a glyph for a glyphless character for iterator IT.
24425 IT->glyphless_method specifies which method to use for displaying
24426 the character. See the description of enum
24427 glyphless_display_method in dispextern.h for the detail.
24429 FOR_NO_FONT is nonzero if and only if this is for a character for
24430 which no font was found. ACRONYM, if non-nil, is an acronym string
24431 for the character. */
24433 static void
24434 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24436 int face_id;
24437 struct face *face;
24438 struct font *font;
24439 int base_width, base_height, width, height;
24440 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24441 int len;
24443 /* Get the metrics of the base font. We always refer to the current
24444 ASCII face. */
24445 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24446 font = face->font ? face->font : FRAME_FONT (it->f);
24447 it->ascent = FONT_BASE (font) + font->baseline_offset;
24448 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24449 base_height = it->ascent + it->descent;
24450 base_width = font->average_width;
24452 /* Get a face ID for the glyph by utilizing a cache (the same way as
24453 done for `escape-glyph' in get_next_display_element). */
24454 if (it->f == last_glyphless_glyph_frame
24455 && it->face_id == last_glyphless_glyph_face_id)
24457 face_id = last_glyphless_glyph_merged_face_id;
24459 else
24461 /* Merge the `glyphless-char' face into the current face. */
24462 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24463 last_glyphless_glyph_frame = it->f;
24464 last_glyphless_glyph_face_id = it->face_id;
24465 last_glyphless_glyph_merged_face_id = face_id;
24468 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24470 it->pixel_width = THIN_SPACE_WIDTH;
24471 len = 0;
24472 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24474 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24476 width = CHAR_WIDTH (it->c);
24477 if (width == 0)
24478 width = 1;
24479 else if (width > 4)
24480 width = 4;
24481 it->pixel_width = base_width * width;
24482 len = 0;
24483 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24485 else
24487 char buf[7];
24488 const char *str;
24489 unsigned int code[6];
24490 int upper_len;
24491 int ascent, descent;
24492 struct font_metrics metrics_upper, metrics_lower;
24494 face = FACE_FROM_ID (it->f, face_id);
24495 font = face->font ? face->font : FRAME_FONT (it->f);
24496 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24498 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24500 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24501 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24502 if (CONSP (acronym))
24503 acronym = XCAR (acronym);
24504 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24506 else
24508 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24509 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24510 str = buf;
24512 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24513 code[len] = font->driver->encode_char (font, str[len]);
24514 upper_len = (len + 1) / 2;
24515 font->driver->text_extents (font, code, upper_len,
24516 &metrics_upper);
24517 font->driver->text_extents (font, code + upper_len, len - upper_len,
24518 &metrics_lower);
24522 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24523 width = max (metrics_upper.width, metrics_lower.width) + 4;
24524 upper_xoff = upper_yoff = 2; /* the typical case */
24525 if (base_width >= width)
24527 /* Align the upper to the left, the lower to the right. */
24528 it->pixel_width = base_width;
24529 lower_xoff = base_width - 2 - metrics_lower.width;
24531 else
24533 /* Center the shorter one. */
24534 it->pixel_width = width;
24535 if (metrics_upper.width >= metrics_lower.width)
24536 lower_xoff = (width - metrics_lower.width) / 2;
24537 else
24539 /* FIXME: This code doesn't look right. It formerly was
24540 missing the "lower_xoff = 0;", which couldn't have
24541 been right since it left lower_xoff uninitialized. */
24542 lower_xoff = 0;
24543 upper_xoff = (width - metrics_upper.width) / 2;
24547 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24548 top, bottom, and between upper and lower strings. */
24549 height = (metrics_upper.ascent + metrics_upper.descent
24550 + metrics_lower.ascent + metrics_lower.descent) + 5;
24551 /* Center vertically.
24552 H:base_height, D:base_descent
24553 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24555 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24556 descent = D - H/2 + h/2;
24557 lower_yoff = descent - 2 - ld;
24558 upper_yoff = lower_yoff - la - 1 - ud; */
24559 ascent = - (it->descent - (base_height + height + 1) / 2);
24560 descent = it->descent - (base_height - height) / 2;
24561 lower_yoff = descent - 2 - metrics_lower.descent;
24562 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24563 - metrics_upper.descent);
24564 /* Don't make the height shorter than the base height. */
24565 if (height > base_height)
24567 it->ascent = ascent;
24568 it->descent = descent;
24572 it->phys_ascent = it->ascent;
24573 it->phys_descent = it->descent;
24574 if (it->glyph_row)
24575 append_glyphless_glyph (it, face_id, for_no_font, len,
24576 upper_xoff, upper_yoff,
24577 lower_xoff, lower_yoff);
24578 it->nglyphs = 1;
24579 take_vertical_position_into_account (it);
24583 /* RIF:
24584 Produce glyphs/get display metrics for the display element IT is
24585 loaded with. See the description of struct it in dispextern.h
24586 for an overview of struct it. */
24588 void
24589 x_produce_glyphs (struct it *it)
24591 int extra_line_spacing = it->extra_line_spacing;
24593 it->glyph_not_available_p = 0;
24595 if (it->what == IT_CHARACTER)
24597 XChar2b char2b;
24598 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24599 struct font *font = face->font;
24600 struct font_metrics *pcm = NULL;
24601 int boff; /* baseline offset */
24603 if (font == NULL)
24605 /* When no suitable font is found, display this character by
24606 the method specified in the first extra slot of
24607 Vglyphless_char_display. */
24608 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24610 eassert (it->what == IT_GLYPHLESS);
24611 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24612 goto done;
24615 boff = font->baseline_offset;
24616 if (font->vertical_centering)
24617 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24619 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24621 int stretched_p;
24623 it->nglyphs = 1;
24625 if (it->override_ascent >= 0)
24627 it->ascent = it->override_ascent;
24628 it->descent = it->override_descent;
24629 boff = it->override_boff;
24631 else
24633 it->ascent = FONT_BASE (font) + boff;
24634 it->descent = FONT_DESCENT (font) - boff;
24637 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24639 pcm = get_per_char_metric (font, &char2b);
24640 if (pcm->width == 0
24641 && pcm->rbearing == 0 && pcm->lbearing == 0)
24642 pcm = NULL;
24645 if (pcm)
24647 it->phys_ascent = pcm->ascent + boff;
24648 it->phys_descent = pcm->descent - boff;
24649 it->pixel_width = pcm->width;
24651 else
24653 it->glyph_not_available_p = 1;
24654 it->phys_ascent = it->ascent;
24655 it->phys_descent = it->descent;
24656 it->pixel_width = font->space_width;
24659 if (it->constrain_row_ascent_descent_p)
24661 if (it->descent > it->max_descent)
24663 it->ascent += it->descent - it->max_descent;
24664 it->descent = it->max_descent;
24666 if (it->ascent > it->max_ascent)
24668 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24669 it->ascent = it->max_ascent;
24671 it->phys_ascent = min (it->phys_ascent, it->ascent);
24672 it->phys_descent = min (it->phys_descent, it->descent);
24673 extra_line_spacing = 0;
24676 /* If this is a space inside a region of text with
24677 `space-width' property, change its width. */
24678 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24679 if (stretched_p)
24680 it->pixel_width *= XFLOATINT (it->space_width);
24682 /* If face has a box, add the box thickness to the character
24683 height. If character has a box line to the left and/or
24684 right, add the box line width to the character's width. */
24685 if (face->box != FACE_NO_BOX)
24687 int thick = face->box_line_width;
24689 if (thick > 0)
24691 it->ascent += thick;
24692 it->descent += thick;
24694 else
24695 thick = -thick;
24697 if (it->start_of_box_run_p)
24698 it->pixel_width += thick;
24699 if (it->end_of_box_run_p)
24700 it->pixel_width += thick;
24703 /* If face has an overline, add the height of the overline
24704 (1 pixel) and a 1 pixel margin to the character height. */
24705 if (face->overline_p)
24706 it->ascent += overline_margin;
24708 if (it->constrain_row_ascent_descent_p)
24710 if (it->ascent > it->max_ascent)
24711 it->ascent = it->max_ascent;
24712 if (it->descent > it->max_descent)
24713 it->descent = it->max_descent;
24716 take_vertical_position_into_account (it);
24718 /* If we have to actually produce glyphs, do it. */
24719 if (it->glyph_row)
24721 if (stretched_p)
24723 /* Translate a space with a `space-width' property
24724 into a stretch glyph. */
24725 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24726 / FONT_HEIGHT (font));
24727 append_stretch_glyph (it, it->object, it->pixel_width,
24728 it->ascent + it->descent, ascent);
24730 else
24731 append_glyph (it);
24733 /* If characters with lbearing or rbearing are displayed
24734 in this line, record that fact in a flag of the
24735 glyph row. This is used to optimize X output code. */
24736 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24737 it->glyph_row->contains_overlapping_glyphs_p = 1;
24739 if (! stretched_p && it->pixel_width == 0)
24740 /* We assure that all visible glyphs have at least 1-pixel
24741 width. */
24742 it->pixel_width = 1;
24744 else if (it->char_to_display == '\n')
24746 /* A newline has no width, but we need the height of the
24747 line. But if previous part of the line sets a height,
24748 don't increase that height */
24750 Lisp_Object height;
24751 Lisp_Object total_height = Qnil;
24753 it->override_ascent = -1;
24754 it->pixel_width = 0;
24755 it->nglyphs = 0;
24757 height = get_it_property (it, Qline_height);
24758 /* Split (line-height total-height) list */
24759 if (CONSP (height)
24760 && CONSP (XCDR (height))
24761 && NILP (XCDR (XCDR (height))))
24763 total_height = XCAR (XCDR (height));
24764 height = XCAR (height);
24766 height = calc_line_height_property (it, height, font, boff, 1);
24768 if (it->override_ascent >= 0)
24770 it->ascent = it->override_ascent;
24771 it->descent = it->override_descent;
24772 boff = it->override_boff;
24774 else
24776 it->ascent = FONT_BASE (font) + boff;
24777 it->descent = FONT_DESCENT (font) - boff;
24780 if (EQ (height, Qt))
24782 if (it->descent > it->max_descent)
24784 it->ascent += it->descent - it->max_descent;
24785 it->descent = it->max_descent;
24787 if (it->ascent > it->max_ascent)
24789 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24790 it->ascent = it->max_ascent;
24792 it->phys_ascent = min (it->phys_ascent, it->ascent);
24793 it->phys_descent = min (it->phys_descent, it->descent);
24794 it->constrain_row_ascent_descent_p = 1;
24795 extra_line_spacing = 0;
24797 else
24799 Lisp_Object spacing;
24801 it->phys_ascent = it->ascent;
24802 it->phys_descent = it->descent;
24804 if ((it->max_ascent > 0 || it->max_descent > 0)
24805 && face->box != FACE_NO_BOX
24806 && face->box_line_width > 0)
24808 it->ascent += face->box_line_width;
24809 it->descent += face->box_line_width;
24811 if (!NILP (height)
24812 && XINT (height) > it->ascent + it->descent)
24813 it->ascent = XINT (height) - it->descent;
24815 if (!NILP (total_height))
24816 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24817 else
24819 spacing = get_it_property (it, Qline_spacing);
24820 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24822 if (INTEGERP (spacing))
24824 extra_line_spacing = XINT (spacing);
24825 if (!NILP (total_height))
24826 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24830 else /* i.e. (it->char_to_display == '\t') */
24832 if (font->space_width > 0)
24834 int tab_width = it->tab_width * font->space_width;
24835 int x = it->current_x + it->continuation_lines_width;
24836 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24838 /* If the distance from the current position to the next tab
24839 stop is less than a space character width, use the
24840 tab stop after that. */
24841 if (next_tab_x - x < font->space_width)
24842 next_tab_x += tab_width;
24844 it->pixel_width = next_tab_x - x;
24845 it->nglyphs = 1;
24846 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24847 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24849 if (it->glyph_row)
24851 append_stretch_glyph (it, it->object, it->pixel_width,
24852 it->ascent + it->descent, it->ascent);
24855 else
24857 it->pixel_width = 0;
24858 it->nglyphs = 1;
24862 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24864 /* A static composition.
24866 Note: A composition is represented as one glyph in the
24867 glyph matrix. There are no padding glyphs.
24869 Important note: pixel_width, ascent, and descent are the
24870 values of what is drawn by draw_glyphs (i.e. the values of
24871 the overall glyphs composed). */
24872 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24873 int boff; /* baseline offset */
24874 struct composition *cmp = composition_table[it->cmp_it.id];
24875 int glyph_len = cmp->glyph_len;
24876 struct font *font = face->font;
24878 it->nglyphs = 1;
24880 /* If we have not yet calculated pixel size data of glyphs of
24881 the composition for the current face font, calculate them
24882 now. Theoretically, we have to check all fonts for the
24883 glyphs, but that requires much time and memory space. So,
24884 here we check only the font of the first glyph. This may
24885 lead to incorrect display, but it's very rare, and C-l
24886 (recenter-top-bottom) can correct the display anyway. */
24887 if (! cmp->font || cmp->font != font)
24889 /* Ascent and descent of the font of the first character
24890 of this composition (adjusted by baseline offset).
24891 Ascent and descent of overall glyphs should not be less
24892 than these, respectively. */
24893 int font_ascent, font_descent, font_height;
24894 /* Bounding box of the overall glyphs. */
24895 int leftmost, rightmost, lowest, highest;
24896 int lbearing, rbearing;
24897 int i, width, ascent, descent;
24898 int left_padded = 0, right_padded = 0;
24899 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24900 XChar2b char2b;
24901 struct font_metrics *pcm;
24902 int font_not_found_p;
24903 ptrdiff_t pos;
24905 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24906 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24907 break;
24908 if (glyph_len < cmp->glyph_len)
24909 right_padded = 1;
24910 for (i = 0; i < glyph_len; i++)
24912 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24913 break;
24914 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24916 if (i > 0)
24917 left_padded = 1;
24919 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24920 : IT_CHARPOS (*it));
24921 /* If no suitable font is found, use the default font. */
24922 font_not_found_p = font == NULL;
24923 if (font_not_found_p)
24925 face = face->ascii_face;
24926 font = face->font;
24928 boff = font->baseline_offset;
24929 if (font->vertical_centering)
24930 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24931 font_ascent = FONT_BASE (font) + boff;
24932 font_descent = FONT_DESCENT (font) - boff;
24933 font_height = FONT_HEIGHT (font);
24935 cmp->font = font;
24937 pcm = NULL;
24938 if (! font_not_found_p)
24940 get_char_face_and_encoding (it->f, c, it->face_id,
24941 &char2b, 0);
24942 pcm = get_per_char_metric (font, &char2b);
24945 /* Initialize the bounding box. */
24946 if (pcm)
24948 width = cmp->glyph_len > 0 ? pcm->width : 0;
24949 ascent = pcm->ascent;
24950 descent = pcm->descent;
24951 lbearing = pcm->lbearing;
24952 rbearing = pcm->rbearing;
24954 else
24956 width = cmp->glyph_len > 0 ? font->space_width : 0;
24957 ascent = FONT_BASE (font);
24958 descent = FONT_DESCENT (font);
24959 lbearing = 0;
24960 rbearing = width;
24963 rightmost = width;
24964 leftmost = 0;
24965 lowest = - descent + boff;
24966 highest = ascent + boff;
24968 if (! font_not_found_p
24969 && font->default_ascent
24970 && CHAR_TABLE_P (Vuse_default_ascent)
24971 && !NILP (Faref (Vuse_default_ascent,
24972 make_number (it->char_to_display))))
24973 highest = font->default_ascent + boff;
24975 /* Draw the first glyph at the normal position. It may be
24976 shifted to right later if some other glyphs are drawn
24977 at the left. */
24978 cmp->offsets[i * 2] = 0;
24979 cmp->offsets[i * 2 + 1] = boff;
24980 cmp->lbearing = lbearing;
24981 cmp->rbearing = rbearing;
24983 /* Set cmp->offsets for the remaining glyphs. */
24984 for (i++; i < glyph_len; i++)
24986 int left, right, btm, top;
24987 int ch = COMPOSITION_GLYPH (cmp, i);
24988 int face_id;
24989 struct face *this_face;
24991 if (ch == '\t')
24992 ch = ' ';
24993 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24994 this_face = FACE_FROM_ID (it->f, face_id);
24995 font = this_face->font;
24997 if (font == NULL)
24998 pcm = NULL;
24999 else
25001 get_char_face_and_encoding (it->f, ch, face_id,
25002 &char2b, 0);
25003 pcm = get_per_char_metric (font, &char2b);
25005 if (! pcm)
25006 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25007 else
25009 width = pcm->width;
25010 ascent = pcm->ascent;
25011 descent = pcm->descent;
25012 lbearing = pcm->lbearing;
25013 rbearing = pcm->rbearing;
25014 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25016 /* Relative composition with or without
25017 alternate chars. */
25018 left = (leftmost + rightmost - width) / 2;
25019 btm = - descent + boff;
25020 if (font->relative_compose
25021 && (! CHAR_TABLE_P (Vignore_relative_composition)
25022 || NILP (Faref (Vignore_relative_composition,
25023 make_number (ch)))))
25026 if (- descent >= font->relative_compose)
25027 /* One extra pixel between two glyphs. */
25028 btm = highest + 1;
25029 else if (ascent <= 0)
25030 /* One extra pixel between two glyphs. */
25031 btm = lowest - 1 - ascent - descent;
25034 else
25036 /* A composition rule is specified by an integer
25037 value that encodes global and new reference
25038 points (GREF and NREF). GREF and NREF are
25039 specified by numbers as below:
25041 0---1---2 -- ascent
25045 9--10--11 -- center
25047 ---3---4---5--- baseline
25049 6---7---8 -- descent
25051 int rule = COMPOSITION_RULE (cmp, i);
25052 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25054 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25055 grefx = gref % 3, nrefx = nref % 3;
25056 grefy = gref / 3, nrefy = nref / 3;
25057 if (xoff)
25058 xoff = font_height * (xoff - 128) / 256;
25059 if (yoff)
25060 yoff = font_height * (yoff - 128) / 256;
25062 left = (leftmost
25063 + grefx * (rightmost - leftmost) / 2
25064 - nrefx * width / 2
25065 + xoff);
25067 btm = ((grefy == 0 ? highest
25068 : grefy == 1 ? 0
25069 : grefy == 2 ? lowest
25070 : (highest + lowest) / 2)
25071 - (nrefy == 0 ? ascent + descent
25072 : nrefy == 1 ? descent - boff
25073 : nrefy == 2 ? 0
25074 : (ascent + descent) / 2)
25075 + yoff);
25078 cmp->offsets[i * 2] = left;
25079 cmp->offsets[i * 2 + 1] = btm + descent;
25081 /* Update the bounding box of the overall glyphs. */
25082 if (width > 0)
25084 right = left + width;
25085 if (left < leftmost)
25086 leftmost = left;
25087 if (right > rightmost)
25088 rightmost = right;
25090 top = btm + descent + ascent;
25091 if (top > highest)
25092 highest = top;
25093 if (btm < lowest)
25094 lowest = btm;
25096 if (cmp->lbearing > left + lbearing)
25097 cmp->lbearing = left + lbearing;
25098 if (cmp->rbearing < left + rbearing)
25099 cmp->rbearing = left + rbearing;
25103 /* If there are glyphs whose x-offsets are negative,
25104 shift all glyphs to the right and make all x-offsets
25105 non-negative. */
25106 if (leftmost < 0)
25108 for (i = 0; i < cmp->glyph_len; i++)
25109 cmp->offsets[i * 2] -= leftmost;
25110 rightmost -= leftmost;
25111 cmp->lbearing -= leftmost;
25112 cmp->rbearing -= leftmost;
25115 if (left_padded && cmp->lbearing < 0)
25117 for (i = 0; i < cmp->glyph_len; i++)
25118 cmp->offsets[i * 2] -= cmp->lbearing;
25119 rightmost -= cmp->lbearing;
25120 cmp->rbearing -= cmp->lbearing;
25121 cmp->lbearing = 0;
25123 if (right_padded && rightmost < cmp->rbearing)
25125 rightmost = cmp->rbearing;
25128 cmp->pixel_width = rightmost;
25129 cmp->ascent = highest;
25130 cmp->descent = - lowest;
25131 if (cmp->ascent < font_ascent)
25132 cmp->ascent = font_ascent;
25133 if (cmp->descent < font_descent)
25134 cmp->descent = font_descent;
25137 if (it->glyph_row
25138 && (cmp->lbearing < 0
25139 || cmp->rbearing > cmp->pixel_width))
25140 it->glyph_row->contains_overlapping_glyphs_p = 1;
25142 it->pixel_width = cmp->pixel_width;
25143 it->ascent = it->phys_ascent = cmp->ascent;
25144 it->descent = it->phys_descent = cmp->descent;
25145 if (face->box != FACE_NO_BOX)
25147 int thick = face->box_line_width;
25149 if (thick > 0)
25151 it->ascent += thick;
25152 it->descent += thick;
25154 else
25155 thick = - thick;
25157 if (it->start_of_box_run_p)
25158 it->pixel_width += thick;
25159 if (it->end_of_box_run_p)
25160 it->pixel_width += thick;
25163 /* If face has an overline, add the height of the overline
25164 (1 pixel) and a 1 pixel margin to the character height. */
25165 if (face->overline_p)
25166 it->ascent += overline_margin;
25168 take_vertical_position_into_account (it);
25169 if (it->ascent < 0)
25170 it->ascent = 0;
25171 if (it->descent < 0)
25172 it->descent = 0;
25174 if (it->glyph_row && cmp->glyph_len > 0)
25175 append_composite_glyph (it);
25177 else if (it->what == IT_COMPOSITION)
25179 /* A dynamic (automatic) composition. */
25180 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25181 Lisp_Object gstring;
25182 struct font_metrics metrics;
25184 it->nglyphs = 1;
25186 gstring = composition_gstring_from_id (it->cmp_it.id);
25187 it->pixel_width
25188 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25189 &metrics);
25190 if (it->glyph_row
25191 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25192 it->glyph_row->contains_overlapping_glyphs_p = 1;
25193 it->ascent = it->phys_ascent = metrics.ascent;
25194 it->descent = it->phys_descent = metrics.descent;
25195 if (face->box != FACE_NO_BOX)
25197 int thick = face->box_line_width;
25199 if (thick > 0)
25201 it->ascent += thick;
25202 it->descent += thick;
25204 else
25205 thick = - thick;
25207 if (it->start_of_box_run_p)
25208 it->pixel_width += thick;
25209 if (it->end_of_box_run_p)
25210 it->pixel_width += thick;
25212 /* If face has an overline, add the height of the overline
25213 (1 pixel) and a 1 pixel margin to the character height. */
25214 if (face->overline_p)
25215 it->ascent += overline_margin;
25216 take_vertical_position_into_account (it);
25217 if (it->ascent < 0)
25218 it->ascent = 0;
25219 if (it->descent < 0)
25220 it->descent = 0;
25222 if (it->glyph_row)
25223 append_composite_glyph (it);
25225 else if (it->what == IT_GLYPHLESS)
25226 produce_glyphless_glyph (it, 0, Qnil);
25227 else if (it->what == IT_IMAGE)
25228 produce_image_glyph (it);
25229 else if (it->what == IT_STRETCH)
25230 produce_stretch_glyph (it);
25232 done:
25233 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25234 because this isn't true for images with `:ascent 100'. */
25235 eassert (it->ascent >= 0 && it->descent >= 0);
25236 if (it->area == TEXT_AREA)
25237 it->current_x += it->pixel_width;
25239 if (extra_line_spacing > 0)
25241 it->descent += extra_line_spacing;
25242 if (extra_line_spacing > it->max_extra_line_spacing)
25243 it->max_extra_line_spacing = extra_line_spacing;
25246 it->max_ascent = max (it->max_ascent, it->ascent);
25247 it->max_descent = max (it->max_descent, it->descent);
25248 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25249 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25252 /* EXPORT for RIF:
25253 Output LEN glyphs starting at START at the nominal cursor position.
25254 Advance the nominal cursor over the text. The global variable
25255 updated_window contains the window being updated, updated_row is
25256 the glyph row being updated, and updated_area is the area of that
25257 row being updated. */
25259 void
25260 x_write_glyphs (struct glyph *start, int len)
25262 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25264 eassert (updated_window && updated_row);
25265 /* When the window is hscrolled, cursor hpos can legitimately be out
25266 of bounds, but we draw the cursor at the corresponding window
25267 margin in that case. */
25268 if (!updated_row->reversed_p && chpos < 0)
25269 chpos = 0;
25270 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25271 chpos = updated_row->used[TEXT_AREA] - 1;
25273 block_input ();
25275 /* Write glyphs. */
25277 hpos = start - updated_row->glyphs[updated_area];
25278 x = draw_glyphs (updated_window, output_cursor.x,
25279 updated_row, updated_area,
25280 hpos, hpos + len,
25281 DRAW_NORMAL_TEXT, 0);
25283 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25284 if (updated_area == TEXT_AREA
25285 && updated_window->phys_cursor_on_p
25286 && updated_window->phys_cursor.vpos == output_cursor.vpos
25287 && chpos >= hpos
25288 && chpos < hpos + len)
25289 updated_window->phys_cursor_on_p = 0;
25291 unblock_input ();
25293 /* Advance the output cursor. */
25294 output_cursor.hpos += len;
25295 output_cursor.x = x;
25299 /* EXPORT for RIF:
25300 Insert LEN glyphs from START at the nominal cursor position. */
25302 void
25303 x_insert_glyphs (struct glyph *start, int len)
25305 struct frame *f;
25306 struct window *w;
25307 int line_height, shift_by_width, shifted_region_width;
25308 struct glyph_row *row;
25309 struct glyph *glyph;
25310 int frame_x, frame_y;
25311 ptrdiff_t hpos;
25313 eassert (updated_window && updated_row);
25314 block_input ();
25315 w = updated_window;
25316 f = XFRAME (WINDOW_FRAME (w));
25318 /* Get the height of the line we are in. */
25319 row = updated_row;
25320 line_height = row->height;
25322 /* Get the width of the glyphs to insert. */
25323 shift_by_width = 0;
25324 for (glyph = start; glyph < start + len; ++glyph)
25325 shift_by_width += glyph->pixel_width;
25327 /* Get the width of the region to shift right. */
25328 shifted_region_width = (window_box_width (w, updated_area)
25329 - output_cursor.x
25330 - shift_by_width);
25332 /* Shift right. */
25333 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25334 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25336 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25337 line_height, shift_by_width);
25339 /* Write the glyphs. */
25340 hpos = start - row->glyphs[updated_area];
25341 draw_glyphs (w, output_cursor.x, row, updated_area,
25342 hpos, hpos + len,
25343 DRAW_NORMAL_TEXT, 0);
25345 /* Advance the output cursor. */
25346 output_cursor.hpos += len;
25347 output_cursor.x += shift_by_width;
25348 unblock_input ();
25352 /* EXPORT for RIF:
25353 Erase the current text line from the nominal cursor position
25354 (inclusive) to pixel column TO_X (exclusive). The idea is that
25355 everything from TO_X onward is already erased.
25357 TO_X is a pixel position relative to updated_area of
25358 updated_window. TO_X == -1 means clear to the end of this area. */
25360 void
25361 x_clear_end_of_line (int to_x)
25363 struct frame *f;
25364 struct window *w = updated_window;
25365 int max_x, min_y, max_y;
25366 int from_x, from_y, to_y;
25368 eassert (updated_window && updated_row);
25369 f = XFRAME (w->frame);
25371 if (updated_row->full_width_p)
25372 max_x = WINDOW_TOTAL_WIDTH (w);
25373 else
25374 max_x = window_box_width (w, updated_area);
25375 max_y = window_text_bottom_y (w);
25377 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25378 of window. For TO_X > 0, truncate to end of drawing area. */
25379 if (to_x == 0)
25380 return;
25381 else if (to_x < 0)
25382 to_x = max_x;
25383 else
25384 to_x = min (to_x, max_x);
25386 to_y = min (max_y, output_cursor.y + updated_row->height);
25388 /* Notice if the cursor will be cleared by this operation. */
25389 if (!updated_row->full_width_p)
25390 notice_overwritten_cursor (w, updated_area,
25391 output_cursor.x, -1,
25392 updated_row->y,
25393 MATRIX_ROW_BOTTOM_Y (updated_row));
25395 from_x = output_cursor.x;
25397 /* Translate to frame coordinates. */
25398 if (updated_row->full_width_p)
25400 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25401 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25403 else
25405 int area_left = window_box_left (w, updated_area);
25406 from_x += area_left;
25407 to_x += area_left;
25410 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25411 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25412 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25414 /* Prevent inadvertently clearing to end of the X window. */
25415 if (to_x > from_x && to_y > from_y)
25417 block_input ();
25418 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25419 to_x - from_x, to_y - from_y);
25420 unblock_input ();
25424 #endif /* HAVE_WINDOW_SYSTEM */
25428 /***********************************************************************
25429 Cursor types
25430 ***********************************************************************/
25432 /* Value is the internal representation of the specified cursor type
25433 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25434 of the bar cursor. */
25436 static enum text_cursor_kinds
25437 get_specified_cursor_type (Lisp_Object arg, int *width)
25439 enum text_cursor_kinds type;
25441 if (NILP (arg))
25442 return NO_CURSOR;
25444 if (EQ (arg, Qbox))
25445 return FILLED_BOX_CURSOR;
25447 if (EQ (arg, Qhollow))
25448 return HOLLOW_BOX_CURSOR;
25450 if (EQ (arg, Qbar))
25452 *width = 2;
25453 return BAR_CURSOR;
25456 if (CONSP (arg)
25457 && EQ (XCAR (arg), Qbar)
25458 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25460 *width = XINT (XCDR (arg));
25461 return BAR_CURSOR;
25464 if (EQ (arg, Qhbar))
25466 *width = 2;
25467 return HBAR_CURSOR;
25470 if (CONSP (arg)
25471 && EQ (XCAR (arg), Qhbar)
25472 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25474 *width = XINT (XCDR (arg));
25475 return HBAR_CURSOR;
25478 /* Treat anything unknown as "hollow box cursor".
25479 It was bad to signal an error; people have trouble fixing
25480 .Xdefaults with Emacs, when it has something bad in it. */
25481 type = HOLLOW_BOX_CURSOR;
25483 return type;
25486 /* Set the default cursor types for specified frame. */
25487 void
25488 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25490 int width = 1;
25491 Lisp_Object tem;
25493 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25494 FRAME_CURSOR_WIDTH (f) = width;
25496 /* By default, set up the blink-off state depending on the on-state. */
25498 tem = Fassoc (arg, Vblink_cursor_alist);
25499 if (!NILP (tem))
25501 FRAME_BLINK_OFF_CURSOR (f)
25502 = get_specified_cursor_type (XCDR (tem), &width);
25503 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25505 else
25506 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25510 #ifdef HAVE_WINDOW_SYSTEM
25512 /* Return the cursor we want to be displayed in window W. Return
25513 width of bar/hbar cursor through WIDTH arg. Return with
25514 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25515 (i.e. if the `system caret' should track this cursor).
25517 In a mini-buffer window, we want the cursor only to appear if we
25518 are reading input from this window. For the selected window, we
25519 want the cursor type given by the frame parameter or buffer local
25520 setting of cursor-type. If explicitly marked off, draw no cursor.
25521 In all other cases, we want a hollow box cursor. */
25523 static enum text_cursor_kinds
25524 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25525 int *active_cursor)
25527 struct frame *f = XFRAME (w->frame);
25528 struct buffer *b = XBUFFER (w->contents);
25529 int cursor_type = DEFAULT_CURSOR;
25530 Lisp_Object alt_cursor;
25531 int non_selected = 0;
25533 *active_cursor = 1;
25535 /* Echo area */
25536 if (cursor_in_echo_area
25537 && FRAME_HAS_MINIBUF_P (f)
25538 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25540 if (w == XWINDOW (echo_area_window))
25542 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25544 *width = FRAME_CURSOR_WIDTH (f);
25545 return FRAME_DESIRED_CURSOR (f);
25547 else
25548 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25551 *active_cursor = 0;
25552 non_selected = 1;
25555 /* Detect a nonselected window or nonselected frame. */
25556 else if (w != XWINDOW (f->selected_window)
25557 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25559 *active_cursor = 0;
25561 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25562 return NO_CURSOR;
25564 non_selected = 1;
25567 /* Never display a cursor in a window in which cursor-type is nil. */
25568 if (NILP (BVAR (b, cursor_type)))
25569 return NO_CURSOR;
25571 /* Get the normal cursor type for this window. */
25572 if (EQ (BVAR (b, cursor_type), Qt))
25574 cursor_type = FRAME_DESIRED_CURSOR (f);
25575 *width = FRAME_CURSOR_WIDTH (f);
25577 else
25578 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25580 /* Use cursor-in-non-selected-windows instead
25581 for non-selected window or frame. */
25582 if (non_selected)
25584 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25585 if (!EQ (Qt, alt_cursor))
25586 return get_specified_cursor_type (alt_cursor, width);
25587 /* t means modify the normal cursor type. */
25588 if (cursor_type == FILLED_BOX_CURSOR)
25589 cursor_type = HOLLOW_BOX_CURSOR;
25590 else if (cursor_type == BAR_CURSOR && *width > 1)
25591 --*width;
25592 return cursor_type;
25595 /* Use normal cursor if not blinked off. */
25596 if (!w->cursor_off_p)
25598 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25600 if (cursor_type == FILLED_BOX_CURSOR)
25602 /* Using a block cursor on large images can be very annoying.
25603 So use a hollow cursor for "large" images.
25604 If image is not transparent (no mask), also use hollow cursor. */
25605 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25606 if (img != NULL && IMAGEP (img->spec))
25608 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25609 where N = size of default frame font size.
25610 This should cover most of the "tiny" icons people may use. */
25611 if (!img->mask
25612 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25613 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25614 cursor_type = HOLLOW_BOX_CURSOR;
25617 else if (cursor_type != NO_CURSOR)
25619 /* Display current only supports BOX and HOLLOW cursors for images.
25620 So for now, unconditionally use a HOLLOW cursor when cursor is
25621 not a solid box cursor. */
25622 cursor_type = HOLLOW_BOX_CURSOR;
25625 return cursor_type;
25628 /* Cursor is blinked off, so determine how to "toggle" it. */
25630 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25631 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25632 return get_specified_cursor_type (XCDR (alt_cursor), width);
25634 /* Then see if frame has specified a specific blink off cursor type. */
25635 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25637 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25638 return FRAME_BLINK_OFF_CURSOR (f);
25641 #if 0
25642 /* Some people liked having a permanently visible blinking cursor,
25643 while others had very strong opinions against it. So it was
25644 decided to remove it. KFS 2003-09-03 */
25646 /* Finally perform built-in cursor blinking:
25647 filled box <-> hollow box
25648 wide [h]bar <-> narrow [h]bar
25649 narrow [h]bar <-> no cursor
25650 other type <-> no cursor */
25652 if (cursor_type == FILLED_BOX_CURSOR)
25653 return HOLLOW_BOX_CURSOR;
25655 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25657 *width = 1;
25658 return cursor_type;
25660 #endif
25662 return NO_CURSOR;
25666 /* Notice when the text cursor of window W has been completely
25667 overwritten by a drawing operation that outputs glyphs in AREA
25668 starting at X0 and ending at X1 in the line starting at Y0 and
25669 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25670 the rest of the line after X0 has been written. Y coordinates
25671 are window-relative. */
25673 static void
25674 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25675 int x0, int x1, int y0, int y1)
25677 int cx0, cx1, cy0, cy1;
25678 struct glyph_row *row;
25680 if (!w->phys_cursor_on_p)
25681 return;
25682 if (area != TEXT_AREA)
25683 return;
25685 if (w->phys_cursor.vpos < 0
25686 || w->phys_cursor.vpos >= w->current_matrix->nrows
25687 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25688 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
25689 return;
25691 if (row->cursor_in_fringe_p)
25693 row->cursor_in_fringe_p = 0;
25694 draw_fringe_bitmap (w, row, row->reversed_p);
25695 w->phys_cursor_on_p = 0;
25696 return;
25699 cx0 = w->phys_cursor.x;
25700 cx1 = cx0 + w->phys_cursor_width;
25701 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25702 return;
25704 /* The cursor image will be completely removed from the
25705 screen if the output area intersects the cursor area in
25706 y-direction. When we draw in [y0 y1[, and some part of
25707 the cursor is at y < y0, that part must have been drawn
25708 before. When scrolling, the cursor is erased before
25709 actually scrolling, so we don't come here. When not
25710 scrolling, the rows above the old cursor row must have
25711 changed, and in this case these rows must have written
25712 over the cursor image.
25714 Likewise if part of the cursor is below y1, with the
25715 exception of the cursor being in the first blank row at
25716 the buffer and window end because update_text_area
25717 doesn't draw that row. (Except when it does, but
25718 that's handled in update_text_area.) */
25720 cy0 = w->phys_cursor.y;
25721 cy1 = cy0 + w->phys_cursor_height;
25722 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25723 return;
25725 w->phys_cursor_on_p = 0;
25728 #endif /* HAVE_WINDOW_SYSTEM */
25731 /************************************************************************
25732 Mouse Face
25733 ************************************************************************/
25735 #ifdef HAVE_WINDOW_SYSTEM
25737 /* EXPORT for RIF:
25738 Fix the display of area AREA of overlapping row ROW in window W
25739 with respect to the overlapping part OVERLAPS. */
25741 void
25742 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25743 enum glyph_row_area area, int overlaps)
25745 int i, x;
25747 block_input ();
25749 x = 0;
25750 for (i = 0; i < row->used[area];)
25752 if (row->glyphs[area][i].overlaps_vertically_p)
25754 int start = i, start_x = x;
25758 x += row->glyphs[area][i].pixel_width;
25759 ++i;
25761 while (i < row->used[area]
25762 && row->glyphs[area][i].overlaps_vertically_p);
25764 draw_glyphs (w, start_x, row, area,
25765 start, i,
25766 DRAW_NORMAL_TEXT, overlaps);
25768 else
25770 x += row->glyphs[area][i].pixel_width;
25771 ++i;
25775 unblock_input ();
25779 /* EXPORT:
25780 Draw the cursor glyph of window W in glyph row ROW. See the
25781 comment of draw_glyphs for the meaning of HL. */
25783 void
25784 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25785 enum draw_glyphs_face hl)
25787 /* If cursor hpos is out of bounds, don't draw garbage. This can
25788 happen in mini-buffer windows when switching between echo area
25789 glyphs and mini-buffer. */
25790 if ((row->reversed_p
25791 ? (w->phys_cursor.hpos >= 0)
25792 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25794 int on_p = w->phys_cursor_on_p;
25795 int x1;
25796 int hpos = w->phys_cursor.hpos;
25798 /* When the window is hscrolled, cursor hpos can legitimately be
25799 out of bounds, but we draw the cursor at the corresponding
25800 window margin in that case. */
25801 if (!row->reversed_p && hpos < 0)
25802 hpos = 0;
25803 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25804 hpos = row->used[TEXT_AREA] - 1;
25806 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25807 hl, 0);
25808 w->phys_cursor_on_p = on_p;
25810 if (hl == DRAW_CURSOR)
25811 w->phys_cursor_width = x1 - w->phys_cursor.x;
25812 /* When we erase the cursor, and ROW is overlapped by other
25813 rows, make sure that these overlapping parts of other rows
25814 are redrawn. */
25815 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25817 w->phys_cursor_width = x1 - w->phys_cursor.x;
25819 if (row > w->current_matrix->rows
25820 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25821 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25822 OVERLAPS_ERASED_CURSOR);
25824 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25825 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25826 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25827 OVERLAPS_ERASED_CURSOR);
25833 /* EXPORT:
25834 Erase the image of a cursor of window W from the screen. */
25836 void
25837 erase_phys_cursor (struct window *w)
25839 struct frame *f = XFRAME (w->frame);
25840 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25841 int hpos = w->phys_cursor.hpos;
25842 int vpos = w->phys_cursor.vpos;
25843 int mouse_face_here_p = 0;
25844 struct glyph_matrix *active_glyphs = w->current_matrix;
25845 struct glyph_row *cursor_row;
25846 struct glyph *cursor_glyph;
25847 enum draw_glyphs_face hl;
25849 /* No cursor displayed or row invalidated => nothing to do on the
25850 screen. */
25851 if (w->phys_cursor_type == NO_CURSOR)
25852 goto mark_cursor_off;
25854 /* VPOS >= active_glyphs->nrows means that window has been resized.
25855 Don't bother to erase the cursor. */
25856 if (vpos >= active_glyphs->nrows)
25857 goto mark_cursor_off;
25859 /* If row containing cursor is marked invalid, there is nothing we
25860 can do. */
25861 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25862 if (!cursor_row->enabled_p)
25863 goto mark_cursor_off;
25865 /* If line spacing is > 0, old cursor may only be partially visible in
25866 window after split-window. So adjust visible height. */
25867 cursor_row->visible_height = min (cursor_row->visible_height,
25868 window_text_bottom_y (w) - cursor_row->y);
25870 /* If row is completely invisible, don't attempt to delete a cursor which
25871 isn't there. This can happen if cursor is at top of a window, and
25872 we switch to a buffer with a header line in that window. */
25873 if (cursor_row->visible_height <= 0)
25874 goto mark_cursor_off;
25876 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25877 if (cursor_row->cursor_in_fringe_p)
25879 cursor_row->cursor_in_fringe_p = 0;
25880 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25881 goto mark_cursor_off;
25884 /* This can happen when the new row is shorter than the old one.
25885 In this case, either draw_glyphs or clear_end_of_line
25886 should have cleared the cursor. Note that we wouldn't be
25887 able to erase the cursor in this case because we don't have a
25888 cursor glyph at hand. */
25889 if ((cursor_row->reversed_p
25890 ? (w->phys_cursor.hpos < 0)
25891 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25892 goto mark_cursor_off;
25894 /* When the window is hscrolled, cursor hpos can legitimately be out
25895 of bounds, but we draw the cursor at the corresponding window
25896 margin in that case. */
25897 if (!cursor_row->reversed_p && hpos < 0)
25898 hpos = 0;
25899 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25900 hpos = cursor_row->used[TEXT_AREA] - 1;
25902 /* If the cursor is in the mouse face area, redisplay that when
25903 we clear the cursor. */
25904 if (! NILP (hlinfo->mouse_face_window)
25905 && coords_in_mouse_face_p (w, hpos, vpos)
25906 /* Don't redraw the cursor's spot in mouse face if it is at the
25907 end of a line (on a newline). The cursor appears there, but
25908 mouse highlighting does not. */
25909 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25910 mouse_face_here_p = 1;
25912 /* Maybe clear the display under the cursor. */
25913 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25915 int x, y, left_x;
25916 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25917 int width;
25919 cursor_glyph = get_phys_cursor_glyph (w);
25920 if (cursor_glyph == NULL)
25921 goto mark_cursor_off;
25923 width = cursor_glyph->pixel_width;
25924 left_x = window_box_left_offset (w, TEXT_AREA);
25925 x = w->phys_cursor.x;
25926 if (x < left_x)
25927 width -= left_x - x;
25928 width = min (width, window_box_width (w, TEXT_AREA) - x);
25929 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25930 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25932 if (width > 0)
25933 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25936 /* Erase the cursor by redrawing the character underneath it. */
25937 if (mouse_face_here_p)
25938 hl = DRAW_MOUSE_FACE;
25939 else
25940 hl = DRAW_NORMAL_TEXT;
25941 draw_phys_cursor_glyph (w, cursor_row, hl);
25943 mark_cursor_off:
25944 w->phys_cursor_on_p = 0;
25945 w->phys_cursor_type = NO_CURSOR;
25949 /* EXPORT:
25950 Display or clear cursor of window W. If ON is zero, clear the
25951 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25952 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25954 void
25955 display_and_set_cursor (struct window *w, int on,
25956 int hpos, int vpos, int x, int y)
25958 struct frame *f = XFRAME (w->frame);
25959 int new_cursor_type;
25960 int new_cursor_width;
25961 int active_cursor;
25962 struct glyph_row *glyph_row;
25963 struct glyph *glyph;
25965 /* This is pointless on invisible frames, and dangerous on garbaged
25966 windows and frames; in the latter case, the frame or window may
25967 be in the midst of changing its size, and x and y may be off the
25968 window. */
25969 if (! FRAME_VISIBLE_P (f)
25970 || FRAME_GARBAGED_P (f)
25971 || vpos >= w->current_matrix->nrows
25972 || hpos >= w->current_matrix->matrix_w)
25973 return;
25975 /* If cursor is off and we want it off, return quickly. */
25976 if (!on && !w->phys_cursor_on_p)
25977 return;
25979 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25980 /* If cursor row is not enabled, we don't really know where to
25981 display the cursor. */
25982 if (!glyph_row->enabled_p)
25984 w->phys_cursor_on_p = 0;
25985 return;
25988 glyph = NULL;
25989 if (!glyph_row->exact_window_width_line_p
25990 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25991 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25993 eassert (input_blocked_p ());
25995 /* Set new_cursor_type to the cursor we want to be displayed. */
25996 new_cursor_type = get_window_cursor_type (w, glyph,
25997 &new_cursor_width, &active_cursor);
25999 /* If cursor is currently being shown and we don't want it to be or
26000 it is in the wrong place, or the cursor type is not what we want,
26001 erase it. */
26002 if (w->phys_cursor_on_p
26003 && (!on
26004 || w->phys_cursor.x != x
26005 || w->phys_cursor.y != y
26006 || new_cursor_type != w->phys_cursor_type
26007 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26008 && new_cursor_width != w->phys_cursor_width)))
26009 erase_phys_cursor (w);
26011 /* Don't check phys_cursor_on_p here because that flag is only set
26012 to zero in some cases where we know that the cursor has been
26013 completely erased, to avoid the extra work of erasing the cursor
26014 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26015 still not be visible, or it has only been partly erased. */
26016 if (on)
26018 w->phys_cursor_ascent = glyph_row->ascent;
26019 w->phys_cursor_height = glyph_row->height;
26021 /* Set phys_cursor_.* before x_draw_.* is called because some
26022 of them may need the information. */
26023 w->phys_cursor.x = x;
26024 w->phys_cursor.y = glyph_row->y;
26025 w->phys_cursor.hpos = hpos;
26026 w->phys_cursor.vpos = vpos;
26029 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26030 new_cursor_type, new_cursor_width,
26031 on, active_cursor);
26035 /* Switch the display of W's cursor on or off, according to the value
26036 of ON. */
26038 static void
26039 update_window_cursor (struct window *w, int on)
26041 /* Don't update cursor in windows whose frame is in the process
26042 of being deleted. */
26043 if (w->current_matrix)
26045 int hpos = w->phys_cursor.hpos;
26046 int vpos = w->phys_cursor.vpos;
26047 struct glyph_row *row;
26049 if (vpos >= w->current_matrix->nrows
26050 || hpos >= w->current_matrix->matrix_w)
26051 return;
26053 row = MATRIX_ROW (w->current_matrix, vpos);
26055 /* When the window is hscrolled, cursor hpos can legitimately be
26056 out of bounds, but we draw the cursor at the corresponding
26057 window margin in that case. */
26058 if (!row->reversed_p && hpos < 0)
26059 hpos = 0;
26060 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26061 hpos = row->used[TEXT_AREA] - 1;
26063 block_input ();
26064 display_and_set_cursor (w, on, hpos, vpos,
26065 w->phys_cursor.x, w->phys_cursor.y);
26066 unblock_input ();
26071 /* Call update_window_cursor with parameter ON_P on all leaf windows
26072 in the window tree rooted at W. */
26074 static void
26075 update_cursor_in_window_tree (struct window *w, int on_p)
26077 while (w)
26079 if (WINDOWP (w->contents))
26080 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26081 else
26082 update_window_cursor (w, on_p);
26084 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26089 /* EXPORT:
26090 Display the cursor on window W, or clear it, according to ON_P.
26091 Don't change the cursor's position. */
26093 void
26094 x_update_cursor (struct frame *f, int on_p)
26096 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26100 /* EXPORT:
26101 Clear the cursor of window W to background color, and mark the
26102 cursor as not shown. This is used when the text where the cursor
26103 is about to be rewritten. */
26105 void
26106 x_clear_cursor (struct window *w)
26108 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26109 update_window_cursor (w, 0);
26112 #endif /* HAVE_WINDOW_SYSTEM */
26114 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26115 and MSDOS. */
26116 static void
26117 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26118 int start_hpos, int end_hpos,
26119 enum draw_glyphs_face draw)
26121 #ifdef HAVE_WINDOW_SYSTEM
26122 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26124 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26125 return;
26127 #endif
26128 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26129 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26130 #endif
26133 /* Display the active region described by mouse_face_* according to DRAW. */
26135 static void
26136 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26138 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26139 struct frame *f = XFRAME (WINDOW_FRAME (w));
26141 if (/* If window is in the process of being destroyed, don't bother
26142 to do anything. */
26143 w->current_matrix != NULL
26144 /* Don't update mouse highlight if hidden */
26145 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26146 /* Recognize when we are called to operate on rows that don't exist
26147 anymore. This can happen when a window is split. */
26148 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26150 int phys_cursor_on_p = w->phys_cursor_on_p;
26151 struct glyph_row *row, *first, *last;
26153 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26154 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26156 for (row = first; row <= last && row->enabled_p; ++row)
26158 int start_hpos, end_hpos, start_x;
26160 /* For all but the first row, the highlight starts at column 0. */
26161 if (row == first)
26163 /* R2L rows have BEG and END in reversed order, but the
26164 screen drawing geometry is always left to right. So
26165 we need to mirror the beginning and end of the
26166 highlighted area in R2L rows. */
26167 if (!row->reversed_p)
26169 start_hpos = hlinfo->mouse_face_beg_col;
26170 start_x = hlinfo->mouse_face_beg_x;
26172 else if (row == last)
26174 start_hpos = hlinfo->mouse_face_end_col;
26175 start_x = hlinfo->mouse_face_end_x;
26177 else
26179 start_hpos = 0;
26180 start_x = 0;
26183 else if (row->reversed_p && row == last)
26185 start_hpos = hlinfo->mouse_face_end_col;
26186 start_x = hlinfo->mouse_face_end_x;
26188 else
26190 start_hpos = 0;
26191 start_x = 0;
26194 if (row == last)
26196 if (!row->reversed_p)
26197 end_hpos = hlinfo->mouse_face_end_col;
26198 else if (row == first)
26199 end_hpos = hlinfo->mouse_face_beg_col;
26200 else
26202 end_hpos = row->used[TEXT_AREA];
26203 if (draw == DRAW_NORMAL_TEXT)
26204 row->fill_line_p = 1; /* Clear to end of line */
26207 else if (row->reversed_p && row == first)
26208 end_hpos = hlinfo->mouse_face_beg_col;
26209 else
26211 end_hpos = row->used[TEXT_AREA];
26212 if (draw == DRAW_NORMAL_TEXT)
26213 row->fill_line_p = 1; /* Clear to end of line */
26216 if (end_hpos > start_hpos)
26218 draw_row_with_mouse_face (w, start_x, row,
26219 start_hpos, end_hpos, draw);
26221 row->mouse_face_p
26222 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26226 #ifdef HAVE_WINDOW_SYSTEM
26227 /* When we've written over the cursor, arrange for it to
26228 be displayed again. */
26229 if (FRAME_WINDOW_P (f)
26230 && phys_cursor_on_p && !w->phys_cursor_on_p)
26232 int hpos = w->phys_cursor.hpos;
26234 /* When the window is hscrolled, cursor hpos can legitimately be
26235 out of bounds, but we draw the cursor at the corresponding
26236 window margin in that case. */
26237 if (!row->reversed_p && hpos < 0)
26238 hpos = 0;
26239 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26240 hpos = row->used[TEXT_AREA] - 1;
26242 block_input ();
26243 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26244 w->phys_cursor.x, w->phys_cursor.y);
26245 unblock_input ();
26247 #endif /* HAVE_WINDOW_SYSTEM */
26250 #ifdef HAVE_WINDOW_SYSTEM
26251 /* Change the mouse cursor. */
26252 if (FRAME_WINDOW_P (f))
26254 if (draw == DRAW_NORMAL_TEXT
26255 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26256 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26257 else if (draw == DRAW_MOUSE_FACE)
26258 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26259 else
26260 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26262 #endif /* HAVE_WINDOW_SYSTEM */
26265 /* EXPORT:
26266 Clear out the mouse-highlighted active region.
26267 Redraw it un-highlighted first. Value is non-zero if mouse
26268 face was actually drawn unhighlighted. */
26271 clear_mouse_face (Mouse_HLInfo *hlinfo)
26273 int cleared = 0;
26275 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26277 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26278 cleared = 1;
26281 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26282 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26283 hlinfo->mouse_face_window = Qnil;
26284 hlinfo->mouse_face_overlay = Qnil;
26285 return cleared;
26288 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26289 within the mouse face on that window. */
26290 static int
26291 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26293 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26295 /* Quickly resolve the easy cases. */
26296 if (!(WINDOWP (hlinfo->mouse_face_window)
26297 && XWINDOW (hlinfo->mouse_face_window) == w))
26298 return 0;
26299 if (vpos < hlinfo->mouse_face_beg_row
26300 || vpos > hlinfo->mouse_face_end_row)
26301 return 0;
26302 if (vpos > hlinfo->mouse_face_beg_row
26303 && vpos < hlinfo->mouse_face_end_row)
26304 return 1;
26306 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26308 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26310 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26311 return 1;
26313 else if ((vpos == hlinfo->mouse_face_beg_row
26314 && hpos >= hlinfo->mouse_face_beg_col)
26315 || (vpos == hlinfo->mouse_face_end_row
26316 && hpos < hlinfo->mouse_face_end_col))
26317 return 1;
26319 else
26321 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26323 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26324 return 1;
26326 else if ((vpos == hlinfo->mouse_face_beg_row
26327 && hpos <= hlinfo->mouse_face_beg_col)
26328 || (vpos == hlinfo->mouse_face_end_row
26329 && hpos > hlinfo->mouse_face_end_col))
26330 return 1;
26332 return 0;
26336 /* EXPORT:
26337 Non-zero if physical cursor of window W is within mouse face. */
26340 cursor_in_mouse_face_p (struct window *w)
26342 int hpos = w->phys_cursor.hpos;
26343 int vpos = w->phys_cursor.vpos;
26344 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26346 /* When the window is hscrolled, cursor hpos can legitimately be out
26347 of bounds, but we draw the cursor at the corresponding window
26348 margin in that case. */
26349 if (!row->reversed_p && hpos < 0)
26350 hpos = 0;
26351 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26352 hpos = row->used[TEXT_AREA] - 1;
26354 return coords_in_mouse_face_p (w, hpos, vpos);
26359 /* Find the glyph rows START_ROW and END_ROW of window W that display
26360 characters between buffer positions START_CHARPOS and END_CHARPOS
26361 (excluding END_CHARPOS). DISP_STRING is a display string that
26362 covers these buffer positions. This is similar to
26363 row_containing_pos, but is more accurate when bidi reordering makes
26364 buffer positions change non-linearly with glyph rows. */
26365 static void
26366 rows_from_pos_range (struct window *w,
26367 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26368 Lisp_Object disp_string,
26369 struct glyph_row **start, struct glyph_row **end)
26371 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26372 int last_y = window_text_bottom_y (w);
26373 struct glyph_row *row;
26375 *start = NULL;
26376 *end = NULL;
26378 while (!first->enabled_p
26379 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26380 first++;
26382 /* Find the START row. */
26383 for (row = first;
26384 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26385 row++)
26387 /* A row can potentially be the START row if the range of the
26388 characters it displays intersects the range
26389 [START_CHARPOS..END_CHARPOS). */
26390 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26391 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26392 /* See the commentary in row_containing_pos, for the
26393 explanation of the complicated way to check whether
26394 some position is beyond the end of the characters
26395 displayed by a row. */
26396 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26397 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26398 && !row->ends_at_zv_p
26399 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26400 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26401 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26402 && !row->ends_at_zv_p
26403 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26405 /* Found a candidate row. Now make sure at least one of the
26406 glyphs it displays has a charpos from the range
26407 [START_CHARPOS..END_CHARPOS).
26409 This is not obvious because bidi reordering could make
26410 buffer positions of a row be 1,2,3,102,101,100, and if we
26411 want to highlight characters in [50..60), we don't want
26412 this row, even though [50..60) does intersect [1..103),
26413 the range of character positions given by the row's start
26414 and end positions. */
26415 struct glyph *g = row->glyphs[TEXT_AREA];
26416 struct glyph *e = g + row->used[TEXT_AREA];
26418 while (g < e)
26420 if (((BUFFERP (g->object) || INTEGERP (g->object))
26421 && start_charpos <= g->charpos && g->charpos < end_charpos)
26422 /* A glyph that comes from DISP_STRING is by
26423 definition to be highlighted. */
26424 || EQ (g->object, disp_string))
26425 *start = row;
26426 g++;
26428 if (*start)
26429 break;
26433 /* Find the END row. */
26434 if (!*start
26435 /* If the last row is partially visible, start looking for END
26436 from that row, instead of starting from FIRST. */
26437 && !(row->enabled_p
26438 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26439 row = first;
26440 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26442 struct glyph_row *next = row + 1;
26443 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26445 if (!next->enabled_p
26446 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26447 /* The first row >= START whose range of displayed characters
26448 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26449 is the row END + 1. */
26450 || (start_charpos < next_start
26451 && end_charpos < next_start)
26452 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26453 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26454 && !next->ends_at_zv_p
26455 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26456 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26457 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26458 && !next->ends_at_zv_p
26459 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26461 *end = row;
26462 break;
26464 else
26466 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26467 but none of the characters it displays are in the range, it is
26468 also END + 1. */
26469 struct glyph *g = next->glyphs[TEXT_AREA];
26470 struct glyph *s = g;
26471 struct glyph *e = g + next->used[TEXT_AREA];
26473 while (g < e)
26475 if (((BUFFERP (g->object) || INTEGERP (g->object))
26476 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26477 /* If the buffer position of the first glyph in
26478 the row is equal to END_CHARPOS, it means
26479 the last character to be highlighted is the
26480 newline of ROW, and we must consider NEXT as
26481 END, not END+1. */
26482 || (((!next->reversed_p && g == s)
26483 || (next->reversed_p && g == e - 1))
26484 && (g->charpos == end_charpos
26485 /* Special case for when NEXT is an
26486 empty line at ZV. */
26487 || (g->charpos == -1
26488 && !row->ends_at_zv_p
26489 && next_start == end_charpos)))))
26490 /* A glyph that comes from DISP_STRING is by
26491 definition to be highlighted. */
26492 || EQ (g->object, disp_string))
26493 break;
26494 g++;
26496 if (g == e)
26498 *end = row;
26499 break;
26501 /* The first row that ends at ZV must be the last to be
26502 highlighted. */
26503 else if (next->ends_at_zv_p)
26505 *end = next;
26506 break;
26512 /* This function sets the mouse_face_* elements of HLINFO, assuming
26513 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26514 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26515 for the overlay or run of text properties specifying the mouse
26516 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26517 before-string and after-string that must also be highlighted.
26518 DISP_STRING, if non-nil, is a display string that may cover some
26519 or all of the highlighted text. */
26521 static void
26522 mouse_face_from_buffer_pos (Lisp_Object window,
26523 Mouse_HLInfo *hlinfo,
26524 ptrdiff_t mouse_charpos,
26525 ptrdiff_t start_charpos,
26526 ptrdiff_t end_charpos,
26527 Lisp_Object before_string,
26528 Lisp_Object after_string,
26529 Lisp_Object disp_string)
26531 struct window *w = XWINDOW (window);
26532 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26533 struct glyph_row *r1, *r2;
26534 struct glyph *glyph, *end;
26535 ptrdiff_t ignore, pos;
26536 int x;
26538 eassert (NILP (disp_string) || STRINGP (disp_string));
26539 eassert (NILP (before_string) || STRINGP (before_string));
26540 eassert (NILP (after_string) || STRINGP (after_string));
26542 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26543 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26544 if (r1 == NULL)
26545 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26546 /* If the before-string or display-string contains newlines,
26547 rows_from_pos_range skips to its last row. Move back. */
26548 if (!NILP (before_string) || !NILP (disp_string))
26550 struct glyph_row *prev;
26551 while ((prev = r1 - 1, prev >= first)
26552 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26553 && prev->used[TEXT_AREA] > 0)
26555 struct glyph *beg = prev->glyphs[TEXT_AREA];
26556 glyph = beg + prev->used[TEXT_AREA];
26557 while (--glyph >= beg && INTEGERP (glyph->object));
26558 if (glyph < beg
26559 || !(EQ (glyph->object, before_string)
26560 || EQ (glyph->object, disp_string)))
26561 break;
26562 r1 = prev;
26565 if (r2 == NULL)
26567 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26568 hlinfo->mouse_face_past_end = 1;
26570 else if (!NILP (after_string))
26572 /* If the after-string has newlines, advance to its last row. */
26573 struct glyph_row *next;
26574 struct glyph_row *last
26575 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26577 for (next = r2 + 1;
26578 next <= last
26579 && next->used[TEXT_AREA] > 0
26580 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26581 ++next)
26582 r2 = next;
26584 /* The rest of the display engine assumes that mouse_face_beg_row is
26585 either above mouse_face_end_row or identical to it. But with
26586 bidi-reordered continued lines, the row for START_CHARPOS could
26587 be below the row for END_CHARPOS. If so, swap the rows and store
26588 them in correct order. */
26589 if (r1->y > r2->y)
26591 struct glyph_row *tem = r2;
26593 r2 = r1;
26594 r1 = tem;
26597 hlinfo->mouse_face_beg_y = r1->y;
26598 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26599 hlinfo->mouse_face_end_y = r2->y;
26600 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26602 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26603 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26604 could be anywhere in the row and in any order. The strategy
26605 below is to find the leftmost and the rightmost glyph that
26606 belongs to either of these 3 strings, or whose position is
26607 between START_CHARPOS and END_CHARPOS, and highlight all the
26608 glyphs between those two. This may cover more than just the text
26609 between START_CHARPOS and END_CHARPOS if the range of characters
26610 strides the bidi level boundary, e.g. if the beginning is in R2L
26611 text while the end is in L2R text or vice versa. */
26612 if (!r1->reversed_p)
26614 /* This row is in a left to right paragraph. Scan it left to
26615 right. */
26616 glyph = r1->glyphs[TEXT_AREA];
26617 end = glyph + r1->used[TEXT_AREA];
26618 x = r1->x;
26620 /* Skip truncation glyphs at the start of the glyph row. */
26621 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
26622 for (; glyph < end
26623 && INTEGERP (glyph->object)
26624 && glyph->charpos < 0;
26625 ++glyph)
26626 x += glyph->pixel_width;
26628 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26629 or DISP_STRING, and the first glyph from buffer whose
26630 position is between START_CHARPOS and END_CHARPOS. */
26631 for (; glyph < end
26632 && !INTEGERP (glyph->object)
26633 && !EQ (glyph->object, disp_string)
26634 && !(BUFFERP (glyph->object)
26635 && (glyph->charpos >= start_charpos
26636 && glyph->charpos < end_charpos));
26637 ++glyph)
26639 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26640 are present at buffer positions between START_CHARPOS and
26641 END_CHARPOS, or if they come from an overlay. */
26642 if (EQ (glyph->object, before_string))
26644 pos = string_buffer_position (before_string,
26645 start_charpos);
26646 /* If pos == 0, it means before_string came from an
26647 overlay, not from a buffer position. */
26648 if (!pos || (pos >= start_charpos && pos < end_charpos))
26649 break;
26651 else if (EQ (glyph->object, after_string))
26653 pos = string_buffer_position (after_string, end_charpos);
26654 if (!pos || (pos >= start_charpos && pos < end_charpos))
26655 break;
26657 x += glyph->pixel_width;
26659 hlinfo->mouse_face_beg_x = x;
26660 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26662 else
26664 /* This row is in a right to left paragraph. Scan it right to
26665 left. */
26666 struct glyph *g;
26668 end = r1->glyphs[TEXT_AREA] - 1;
26669 glyph = end + r1->used[TEXT_AREA];
26671 /* Skip truncation glyphs at the start of the glyph row. */
26672 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
26673 for (; glyph > end
26674 && INTEGERP (glyph->object)
26675 && glyph->charpos < 0;
26676 --glyph)
26679 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26680 or DISP_STRING, and the first glyph from buffer whose
26681 position is between START_CHARPOS and END_CHARPOS. */
26682 for (; glyph > end
26683 && !INTEGERP (glyph->object)
26684 && !EQ (glyph->object, disp_string)
26685 && !(BUFFERP (glyph->object)
26686 && (glyph->charpos >= start_charpos
26687 && glyph->charpos < end_charpos));
26688 --glyph)
26690 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26691 are present at buffer positions between START_CHARPOS and
26692 END_CHARPOS, or if they come from an overlay. */
26693 if (EQ (glyph->object, before_string))
26695 pos = string_buffer_position (before_string, start_charpos);
26696 /* If pos == 0, it means before_string came from an
26697 overlay, not from a buffer position. */
26698 if (!pos || (pos >= start_charpos && pos < end_charpos))
26699 break;
26701 else if (EQ (glyph->object, after_string))
26703 pos = string_buffer_position (after_string, end_charpos);
26704 if (!pos || (pos >= start_charpos && pos < end_charpos))
26705 break;
26709 glyph++; /* first glyph to the right of the highlighted area */
26710 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26711 x += g->pixel_width;
26712 hlinfo->mouse_face_beg_x = x;
26713 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26716 /* If the highlight ends in a different row, compute GLYPH and END
26717 for the end row. Otherwise, reuse the values computed above for
26718 the row where the highlight begins. */
26719 if (r2 != r1)
26721 if (!r2->reversed_p)
26723 glyph = r2->glyphs[TEXT_AREA];
26724 end = glyph + r2->used[TEXT_AREA];
26725 x = r2->x;
26727 else
26729 end = r2->glyphs[TEXT_AREA] - 1;
26730 glyph = end + r2->used[TEXT_AREA];
26734 if (!r2->reversed_p)
26736 /* Skip truncation and continuation glyphs near the end of the
26737 row, and also blanks and stretch glyphs inserted by
26738 extend_face_to_end_of_line. */
26739 while (end > glyph
26740 && INTEGERP ((end - 1)->object))
26741 --end;
26742 /* Scan the rest of the glyph row from the end, looking for the
26743 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26744 DISP_STRING, or whose position is between START_CHARPOS
26745 and END_CHARPOS */
26746 for (--end;
26747 end > glyph
26748 && !INTEGERP (end->object)
26749 && !EQ (end->object, disp_string)
26750 && !(BUFFERP (end->object)
26751 && (end->charpos >= start_charpos
26752 && end->charpos < end_charpos));
26753 --end)
26755 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26756 are present at buffer positions between START_CHARPOS and
26757 END_CHARPOS, or if they come from an overlay. */
26758 if (EQ (end->object, before_string))
26760 pos = string_buffer_position (before_string, start_charpos);
26761 if (!pos || (pos >= start_charpos && pos < end_charpos))
26762 break;
26764 else if (EQ (end->object, after_string))
26766 pos = string_buffer_position (after_string, end_charpos);
26767 if (!pos || (pos >= start_charpos && pos < end_charpos))
26768 break;
26771 /* Find the X coordinate of the last glyph to be highlighted. */
26772 for (; glyph <= end; ++glyph)
26773 x += glyph->pixel_width;
26775 hlinfo->mouse_face_end_x = x;
26776 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26778 else
26780 /* Skip truncation and continuation glyphs near the end of the
26781 row, and also blanks and stretch glyphs inserted by
26782 extend_face_to_end_of_line. */
26783 x = r2->x;
26784 end++;
26785 while (end < glyph
26786 && INTEGERP (end->object))
26788 x += end->pixel_width;
26789 ++end;
26791 /* Scan the rest of the glyph row from the end, looking for the
26792 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26793 DISP_STRING, or whose position is between START_CHARPOS
26794 and END_CHARPOS */
26795 for ( ;
26796 end < glyph
26797 && !INTEGERP (end->object)
26798 && !EQ (end->object, disp_string)
26799 && !(BUFFERP (end->object)
26800 && (end->charpos >= start_charpos
26801 && end->charpos < end_charpos));
26802 ++end)
26804 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26805 are present at buffer positions between START_CHARPOS and
26806 END_CHARPOS, or if they come from an overlay. */
26807 if (EQ (end->object, before_string))
26809 pos = string_buffer_position (before_string, start_charpos);
26810 if (!pos || (pos >= start_charpos && pos < end_charpos))
26811 break;
26813 else if (EQ (end->object, after_string))
26815 pos = string_buffer_position (after_string, end_charpos);
26816 if (!pos || (pos >= start_charpos && pos < end_charpos))
26817 break;
26819 x += end->pixel_width;
26821 /* If we exited the above loop because we arrived at the last
26822 glyph of the row, and its buffer position is still not in
26823 range, it means the last character in range is the preceding
26824 newline. Bump the end column and x values to get past the
26825 last glyph. */
26826 if (end == glyph
26827 && BUFFERP (end->object)
26828 && (end->charpos < start_charpos
26829 || end->charpos >= end_charpos))
26831 x += end->pixel_width;
26832 ++end;
26834 hlinfo->mouse_face_end_x = x;
26835 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26838 hlinfo->mouse_face_window = window;
26839 hlinfo->mouse_face_face_id
26840 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26841 mouse_charpos + 1,
26842 !hlinfo->mouse_face_hidden, -1);
26843 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26846 /* The following function is not used anymore (replaced with
26847 mouse_face_from_string_pos), but I leave it here for the time
26848 being, in case someone would. */
26850 #if 0 /* not used */
26852 /* Find the position of the glyph for position POS in OBJECT in
26853 window W's current matrix, and return in *X, *Y the pixel
26854 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26856 RIGHT_P non-zero means return the position of the right edge of the
26857 glyph, RIGHT_P zero means return the left edge position.
26859 If no glyph for POS exists in the matrix, return the position of
26860 the glyph with the next smaller position that is in the matrix, if
26861 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26862 exists in the matrix, return the position of the glyph with the
26863 next larger position in OBJECT.
26865 Value is non-zero if a glyph was found. */
26867 static int
26868 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26869 int *hpos, int *vpos, int *x, int *y, int right_p)
26871 int yb = window_text_bottom_y (w);
26872 struct glyph_row *r;
26873 struct glyph *best_glyph = NULL;
26874 struct glyph_row *best_row = NULL;
26875 int best_x = 0;
26877 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26878 r->enabled_p && r->y < yb;
26879 ++r)
26881 struct glyph *g = r->glyphs[TEXT_AREA];
26882 struct glyph *e = g + r->used[TEXT_AREA];
26883 int gx;
26885 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26886 if (EQ (g->object, object))
26888 if (g->charpos == pos)
26890 best_glyph = g;
26891 best_x = gx;
26892 best_row = r;
26893 goto found;
26895 else if (best_glyph == NULL
26896 || ((eabs (g->charpos - pos)
26897 < eabs (best_glyph->charpos - pos))
26898 && (right_p
26899 ? g->charpos < pos
26900 : g->charpos > pos)))
26902 best_glyph = g;
26903 best_x = gx;
26904 best_row = r;
26909 found:
26911 if (best_glyph)
26913 *x = best_x;
26914 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26916 if (right_p)
26918 *x += best_glyph->pixel_width;
26919 ++*hpos;
26922 *y = best_row->y;
26923 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
26926 return best_glyph != NULL;
26928 #endif /* not used */
26930 /* Find the positions of the first and the last glyphs in window W's
26931 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26932 (assumed to be a string), and return in HLINFO's mouse_face_*
26933 members the pixel and column/row coordinates of those glyphs. */
26935 static void
26936 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26937 Lisp_Object object,
26938 ptrdiff_t startpos, ptrdiff_t endpos)
26940 int yb = window_text_bottom_y (w);
26941 struct glyph_row *r;
26942 struct glyph *g, *e;
26943 int gx;
26944 int found = 0;
26946 /* Find the glyph row with at least one position in the range
26947 [STARTPOS..ENDPOS], and the first glyph in that row whose
26948 position belongs to that range. */
26949 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26950 r->enabled_p && r->y < yb;
26951 ++r)
26953 if (!r->reversed_p)
26955 g = r->glyphs[TEXT_AREA];
26956 e = g + r->used[TEXT_AREA];
26957 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26958 if (EQ (g->object, object)
26959 && startpos <= g->charpos && g->charpos <= endpos)
26961 hlinfo->mouse_face_beg_row
26962 = MATRIX_ROW_VPOS (r, w->current_matrix);
26963 hlinfo->mouse_face_beg_y = r->y;
26964 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26965 hlinfo->mouse_face_beg_x = gx;
26966 found = 1;
26967 break;
26970 else
26972 struct glyph *g1;
26974 e = r->glyphs[TEXT_AREA];
26975 g = e + r->used[TEXT_AREA];
26976 for ( ; g > e; --g)
26977 if (EQ ((g-1)->object, object)
26978 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26980 hlinfo->mouse_face_beg_row
26981 = MATRIX_ROW_VPOS (r, w->current_matrix);
26982 hlinfo->mouse_face_beg_y = r->y;
26983 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26984 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26985 gx += g1->pixel_width;
26986 hlinfo->mouse_face_beg_x = gx;
26987 found = 1;
26988 break;
26991 if (found)
26992 break;
26995 if (!found)
26996 return;
26998 /* Starting with the next row, look for the first row which does NOT
26999 include any glyphs whose positions are in the range. */
27000 for (++r; r->enabled_p && r->y < yb; ++r)
27002 g = r->glyphs[TEXT_AREA];
27003 e = g + r->used[TEXT_AREA];
27004 found = 0;
27005 for ( ; g < e; ++g)
27006 if (EQ (g->object, object)
27007 && startpos <= g->charpos && g->charpos <= endpos)
27009 found = 1;
27010 break;
27012 if (!found)
27013 break;
27016 /* The highlighted region ends on the previous row. */
27017 r--;
27019 /* Set the end row and its vertical pixel coordinate. */
27020 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27021 hlinfo->mouse_face_end_y = r->y;
27023 /* Compute and set the end column and the end column's horizontal
27024 pixel coordinate. */
27025 if (!r->reversed_p)
27027 g = r->glyphs[TEXT_AREA];
27028 e = g + r->used[TEXT_AREA];
27029 for ( ; e > g; --e)
27030 if (EQ ((e-1)->object, object)
27031 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27032 break;
27033 hlinfo->mouse_face_end_col = e - g;
27035 for (gx = r->x; g < e; ++g)
27036 gx += g->pixel_width;
27037 hlinfo->mouse_face_end_x = gx;
27039 else
27041 e = r->glyphs[TEXT_AREA];
27042 g = e + r->used[TEXT_AREA];
27043 for (gx = r->x ; e < g; ++e)
27045 if (EQ (e->object, object)
27046 && startpos <= e->charpos && e->charpos <= endpos)
27047 break;
27048 gx += e->pixel_width;
27050 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27051 hlinfo->mouse_face_end_x = gx;
27055 #ifdef HAVE_WINDOW_SYSTEM
27057 /* See if position X, Y is within a hot-spot of an image. */
27059 static int
27060 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27062 if (!CONSP (hot_spot))
27063 return 0;
27065 if (EQ (XCAR (hot_spot), Qrect))
27067 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27068 Lisp_Object rect = XCDR (hot_spot);
27069 Lisp_Object tem;
27070 if (!CONSP (rect))
27071 return 0;
27072 if (!CONSP (XCAR (rect)))
27073 return 0;
27074 if (!CONSP (XCDR (rect)))
27075 return 0;
27076 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27077 return 0;
27078 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27079 return 0;
27080 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27081 return 0;
27082 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27083 return 0;
27084 return 1;
27086 else if (EQ (XCAR (hot_spot), Qcircle))
27088 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27089 Lisp_Object circ = XCDR (hot_spot);
27090 Lisp_Object lr, lx0, ly0;
27091 if (CONSP (circ)
27092 && CONSP (XCAR (circ))
27093 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27094 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27095 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27097 double r = XFLOATINT (lr);
27098 double dx = XINT (lx0) - x;
27099 double dy = XINT (ly0) - y;
27100 return (dx * dx + dy * dy <= r * r);
27103 else if (EQ (XCAR (hot_spot), Qpoly))
27105 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27106 if (VECTORP (XCDR (hot_spot)))
27108 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27109 Lisp_Object *poly = v->contents;
27110 ptrdiff_t n = v->header.size;
27111 ptrdiff_t i;
27112 int inside = 0;
27113 Lisp_Object lx, ly;
27114 int x0, y0;
27116 /* Need an even number of coordinates, and at least 3 edges. */
27117 if (n < 6 || n & 1)
27118 return 0;
27120 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27121 If count is odd, we are inside polygon. Pixels on edges
27122 may or may not be included depending on actual geometry of the
27123 polygon. */
27124 if ((lx = poly[n-2], !INTEGERP (lx))
27125 || (ly = poly[n-1], !INTEGERP (lx)))
27126 return 0;
27127 x0 = XINT (lx), y0 = XINT (ly);
27128 for (i = 0; i < n; i += 2)
27130 int x1 = x0, y1 = y0;
27131 if ((lx = poly[i], !INTEGERP (lx))
27132 || (ly = poly[i+1], !INTEGERP (ly)))
27133 return 0;
27134 x0 = XINT (lx), y0 = XINT (ly);
27136 /* Does this segment cross the X line? */
27137 if (x0 >= x)
27139 if (x1 >= x)
27140 continue;
27142 else if (x1 < x)
27143 continue;
27144 if (y > y0 && y > y1)
27145 continue;
27146 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27147 inside = !inside;
27149 return inside;
27152 return 0;
27155 Lisp_Object
27156 find_hot_spot (Lisp_Object map, int x, int y)
27158 while (CONSP (map))
27160 if (CONSP (XCAR (map))
27161 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27162 return XCAR (map);
27163 map = XCDR (map);
27166 return Qnil;
27169 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27170 3, 3, 0,
27171 doc: /* Lookup in image map MAP coordinates X and Y.
27172 An image map is an alist where each element has the format (AREA ID PLIST).
27173 An AREA is specified as either a rectangle, a circle, or a polygon:
27174 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27175 pixel coordinates of the upper left and bottom right corners.
27176 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27177 and the radius of the circle; r may be a float or integer.
27178 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27179 vector describes one corner in the polygon.
27180 Returns the alist element for the first matching AREA in MAP. */)
27181 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27183 if (NILP (map))
27184 return Qnil;
27186 CHECK_NUMBER (x);
27187 CHECK_NUMBER (y);
27189 return find_hot_spot (map,
27190 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27191 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27195 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27196 static void
27197 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27199 /* Do not change cursor shape while dragging mouse. */
27200 if (!NILP (do_mouse_tracking))
27201 return;
27203 if (!NILP (pointer))
27205 if (EQ (pointer, Qarrow))
27206 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27207 else if (EQ (pointer, Qhand))
27208 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27209 else if (EQ (pointer, Qtext))
27210 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27211 else if (EQ (pointer, intern ("hdrag")))
27212 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27213 #ifdef HAVE_X_WINDOWS
27214 else if (EQ (pointer, intern ("vdrag")))
27215 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27216 #endif
27217 else if (EQ (pointer, intern ("hourglass")))
27218 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27219 else if (EQ (pointer, Qmodeline))
27220 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27221 else
27222 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27225 if (cursor != No_Cursor)
27226 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27229 #endif /* HAVE_WINDOW_SYSTEM */
27231 /* Take proper action when mouse has moved to the mode or header line
27232 or marginal area AREA of window W, x-position X and y-position Y.
27233 X is relative to the start of the text display area of W, so the
27234 width of bitmap areas and scroll bars must be subtracted to get a
27235 position relative to the start of the mode line. */
27237 static void
27238 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27239 enum window_part area)
27241 struct window *w = XWINDOW (window);
27242 struct frame *f = XFRAME (w->frame);
27243 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27244 #ifdef HAVE_WINDOW_SYSTEM
27245 Display_Info *dpyinfo;
27246 #endif
27247 Cursor cursor = No_Cursor;
27248 Lisp_Object pointer = Qnil;
27249 int dx, dy, width, height;
27250 ptrdiff_t charpos;
27251 Lisp_Object string, object = Qnil;
27252 Lisp_Object pos IF_LINT (= Qnil), help;
27254 Lisp_Object mouse_face;
27255 int original_x_pixel = x;
27256 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27257 struct glyph_row *row IF_LINT (= 0);
27259 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27261 int x0;
27262 struct glyph *end;
27264 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27265 returns them in row/column units! */
27266 string = mode_line_string (w, area, &x, &y, &charpos,
27267 &object, &dx, &dy, &width, &height);
27269 row = (area == ON_MODE_LINE
27270 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27271 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27273 /* Find the glyph under the mouse pointer. */
27274 if (row->mode_line_p && row->enabled_p)
27276 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27277 end = glyph + row->used[TEXT_AREA];
27279 for (x0 = original_x_pixel;
27280 glyph < end && x0 >= glyph->pixel_width;
27281 ++glyph)
27282 x0 -= glyph->pixel_width;
27284 if (glyph >= end)
27285 glyph = NULL;
27288 else
27290 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27291 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27292 returns them in row/column units! */
27293 string = marginal_area_string (w, area, &x, &y, &charpos,
27294 &object, &dx, &dy, &width, &height);
27297 help = Qnil;
27299 #ifdef HAVE_WINDOW_SYSTEM
27300 if (IMAGEP (object))
27302 Lisp_Object image_map, hotspot;
27303 if ((image_map = Fplist_get (XCDR (object), QCmap),
27304 !NILP (image_map))
27305 && (hotspot = find_hot_spot (image_map, dx, dy),
27306 CONSP (hotspot))
27307 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27309 Lisp_Object plist;
27311 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27312 If so, we could look for mouse-enter, mouse-leave
27313 properties in PLIST (and do something...). */
27314 hotspot = XCDR (hotspot);
27315 if (CONSP (hotspot)
27316 && (plist = XCAR (hotspot), CONSP (plist)))
27318 pointer = Fplist_get (plist, Qpointer);
27319 if (NILP (pointer))
27320 pointer = Qhand;
27321 help = Fplist_get (plist, Qhelp_echo);
27322 if (!NILP (help))
27324 help_echo_string = help;
27325 XSETWINDOW (help_echo_window, w);
27326 help_echo_object = w->contents;
27327 help_echo_pos = charpos;
27331 if (NILP (pointer))
27332 pointer = Fplist_get (XCDR (object), QCpointer);
27334 #endif /* HAVE_WINDOW_SYSTEM */
27336 if (STRINGP (string))
27337 pos = make_number (charpos);
27339 /* Set the help text and mouse pointer. If the mouse is on a part
27340 of the mode line without any text (e.g. past the right edge of
27341 the mode line text), use the default help text and pointer. */
27342 if (STRINGP (string) || area == ON_MODE_LINE)
27344 /* Arrange to display the help by setting the global variables
27345 help_echo_string, help_echo_object, and help_echo_pos. */
27346 if (NILP (help))
27348 if (STRINGP (string))
27349 help = Fget_text_property (pos, Qhelp_echo, string);
27351 if (!NILP (help))
27353 help_echo_string = help;
27354 XSETWINDOW (help_echo_window, w);
27355 help_echo_object = string;
27356 help_echo_pos = charpos;
27358 else if (area == ON_MODE_LINE)
27360 Lisp_Object default_help
27361 = buffer_local_value_1 (Qmode_line_default_help_echo,
27362 w->contents);
27364 if (STRINGP (default_help))
27366 help_echo_string = default_help;
27367 XSETWINDOW (help_echo_window, w);
27368 help_echo_object = Qnil;
27369 help_echo_pos = -1;
27374 #ifdef HAVE_WINDOW_SYSTEM
27375 /* Change the mouse pointer according to what is under it. */
27376 if (FRAME_WINDOW_P (f))
27378 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27379 if (STRINGP (string))
27381 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27383 if (NILP (pointer))
27384 pointer = Fget_text_property (pos, Qpointer, string);
27386 /* Change the mouse pointer according to what is under X/Y. */
27387 if (NILP (pointer)
27388 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27390 Lisp_Object map;
27391 map = Fget_text_property (pos, Qlocal_map, string);
27392 if (!KEYMAPP (map))
27393 map = Fget_text_property (pos, Qkeymap, string);
27394 if (!KEYMAPP (map))
27395 cursor = dpyinfo->vertical_scroll_bar_cursor;
27398 else
27399 /* Default mode-line pointer. */
27400 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27402 #endif
27405 /* Change the mouse face according to what is under X/Y. */
27406 if (STRINGP (string))
27408 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27409 if (!NILP (mouse_face)
27410 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27411 && glyph)
27413 Lisp_Object b, e;
27415 struct glyph * tmp_glyph;
27417 int gpos;
27418 int gseq_length;
27419 int total_pixel_width;
27420 ptrdiff_t begpos, endpos, ignore;
27422 int vpos, hpos;
27424 b = Fprevious_single_property_change (make_number (charpos + 1),
27425 Qmouse_face, string, Qnil);
27426 if (NILP (b))
27427 begpos = 0;
27428 else
27429 begpos = XINT (b);
27431 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27432 if (NILP (e))
27433 endpos = SCHARS (string);
27434 else
27435 endpos = XINT (e);
27437 /* Calculate the glyph position GPOS of GLYPH in the
27438 displayed string, relative to the beginning of the
27439 highlighted part of the string.
27441 Note: GPOS is different from CHARPOS. CHARPOS is the
27442 position of GLYPH in the internal string object. A mode
27443 line string format has structures which are converted to
27444 a flattened string by the Emacs Lisp interpreter. The
27445 internal string is an element of those structures. The
27446 displayed string is the flattened string. */
27447 tmp_glyph = row_start_glyph;
27448 while (tmp_glyph < glyph
27449 && (!(EQ (tmp_glyph->object, glyph->object)
27450 && begpos <= tmp_glyph->charpos
27451 && tmp_glyph->charpos < endpos)))
27452 tmp_glyph++;
27453 gpos = glyph - tmp_glyph;
27455 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27456 the highlighted part of the displayed string to which
27457 GLYPH belongs. Note: GSEQ_LENGTH is different from
27458 SCHARS (STRING), because the latter returns the length of
27459 the internal string. */
27460 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27461 tmp_glyph > glyph
27462 && (!(EQ (tmp_glyph->object, glyph->object)
27463 && begpos <= tmp_glyph->charpos
27464 && tmp_glyph->charpos < endpos));
27465 tmp_glyph--)
27467 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27469 /* Calculate the total pixel width of all the glyphs between
27470 the beginning of the highlighted area and GLYPH. */
27471 total_pixel_width = 0;
27472 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27473 total_pixel_width += tmp_glyph->pixel_width;
27475 /* Pre calculation of re-rendering position. Note: X is in
27476 column units here, after the call to mode_line_string or
27477 marginal_area_string. */
27478 hpos = x - gpos;
27479 vpos = (area == ON_MODE_LINE
27480 ? (w->current_matrix)->nrows - 1
27481 : 0);
27483 /* If GLYPH's position is included in the region that is
27484 already drawn in mouse face, we have nothing to do. */
27485 if ( EQ (window, hlinfo->mouse_face_window)
27486 && (!row->reversed_p
27487 ? (hlinfo->mouse_face_beg_col <= hpos
27488 && hpos < hlinfo->mouse_face_end_col)
27489 /* In R2L rows we swap BEG and END, see below. */
27490 : (hlinfo->mouse_face_end_col <= hpos
27491 && hpos < hlinfo->mouse_face_beg_col))
27492 && hlinfo->mouse_face_beg_row == vpos )
27493 return;
27495 if (clear_mouse_face (hlinfo))
27496 cursor = No_Cursor;
27498 if (!row->reversed_p)
27500 hlinfo->mouse_face_beg_col = hpos;
27501 hlinfo->mouse_face_beg_x = original_x_pixel
27502 - (total_pixel_width + dx);
27503 hlinfo->mouse_face_end_col = hpos + gseq_length;
27504 hlinfo->mouse_face_end_x = 0;
27506 else
27508 /* In R2L rows, show_mouse_face expects BEG and END
27509 coordinates to be swapped. */
27510 hlinfo->mouse_face_end_col = hpos;
27511 hlinfo->mouse_face_end_x = original_x_pixel
27512 - (total_pixel_width + dx);
27513 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27514 hlinfo->mouse_face_beg_x = 0;
27517 hlinfo->mouse_face_beg_row = vpos;
27518 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27519 hlinfo->mouse_face_beg_y = 0;
27520 hlinfo->mouse_face_end_y = 0;
27521 hlinfo->mouse_face_past_end = 0;
27522 hlinfo->mouse_face_window = window;
27524 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27525 charpos,
27526 0, 0, 0,
27527 &ignore,
27528 glyph->face_id,
27530 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27532 if (NILP (pointer))
27533 pointer = Qhand;
27535 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27536 clear_mouse_face (hlinfo);
27538 #ifdef HAVE_WINDOW_SYSTEM
27539 if (FRAME_WINDOW_P (f))
27540 define_frame_cursor1 (f, cursor, pointer);
27541 #endif
27545 /* EXPORT:
27546 Take proper action when the mouse has moved to position X, Y on
27547 frame F as regards highlighting characters that have mouse-face
27548 properties. Also de-highlighting chars where the mouse was before.
27549 X and Y can be negative or out of range. */
27551 void
27552 note_mouse_highlight (struct frame *f, int x, int y)
27554 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27555 enum window_part part = ON_NOTHING;
27556 Lisp_Object window;
27557 struct window *w;
27558 Cursor cursor = No_Cursor;
27559 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27560 struct buffer *b;
27562 /* When a menu is active, don't highlight because this looks odd. */
27563 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27564 if (popup_activated ())
27565 return;
27566 #endif
27568 if (NILP (Vmouse_highlight)
27569 || !f->glyphs_initialized_p
27570 || f->pointer_invisible)
27571 return;
27573 hlinfo->mouse_face_mouse_x = x;
27574 hlinfo->mouse_face_mouse_y = y;
27575 hlinfo->mouse_face_mouse_frame = f;
27577 if (hlinfo->mouse_face_defer)
27578 return;
27580 /* Which window is that in? */
27581 window = window_from_coordinates (f, x, y, &part, 1);
27583 /* If displaying active text in another window, clear that. */
27584 if (! EQ (window, hlinfo->mouse_face_window)
27585 /* Also clear if we move out of text area in same window. */
27586 || (!NILP (hlinfo->mouse_face_window)
27587 && !NILP (window)
27588 && part != ON_TEXT
27589 && part != ON_MODE_LINE
27590 && part != ON_HEADER_LINE))
27591 clear_mouse_face (hlinfo);
27593 /* Not on a window -> return. */
27594 if (!WINDOWP (window))
27595 return;
27597 /* Reset help_echo_string. It will get recomputed below. */
27598 help_echo_string = Qnil;
27600 /* Convert to window-relative pixel coordinates. */
27601 w = XWINDOW (window);
27602 frame_to_window_pixel_xy (w, &x, &y);
27604 #ifdef HAVE_WINDOW_SYSTEM
27605 /* Handle tool-bar window differently since it doesn't display a
27606 buffer. */
27607 if (EQ (window, f->tool_bar_window))
27609 note_tool_bar_highlight (f, x, y);
27610 return;
27612 #endif
27614 /* Mouse is on the mode, header line or margin? */
27615 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27616 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27618 note_mode_line_or_margin_highlight (window, x, y, part);
27619 return;
27622 #ifdef HAVE_WINDOW_SYSTEM
27623 if (part == ON_VERTICAL_BORDER)
27625 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27626 help_echo_string = build_string ("drag-mouse-1: resize");
27628 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27629 || part == ON_SCROLL_BAR)
27630 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27631 else
27632 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27633 #endif
27635 /* Are we in a window whose display is up to date?
27636 And verify the buffer's text has not changed. */
27637 b = XBUFFER (w->contents);
27638 if (part == ON_TEXT
27639 && w->window_end_valid
27640 && w->last_modified == BUF_MODIFF (b)
27641 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27643 int hpos, vpos, dx, dy, area = LAST_AREA;
27644 ptrdiff_t pos;
27645 struct glyph *glyph;
27646 Lisp_Object object;
27647 Lisp_Object mouse_face = Qnil, position;
27648 Lisp_Object *overlay_vec = NULL;
27649 ptrdiff_t i, noverlays;
27650 struct buffer *obuf;
27651 ptrdiff_t obegv, ozv;
27652 int same_region;
27654 /* Find the glyph under X/Y. */
27655 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27657 #ifdef HAVE_WINDOW_SYSTEM
27658 /* Look for :pointer property on image. */
27659 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27661 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27662 if (img != NULL && IMAGEP (img->spec))
27664 Lisp_Object image_map, hotspot;
27665 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27666 !NILP (image_map))
27667 && (hotspot = find_hot_spot (image_map,
27668 glyph->slice.img.x + dx,
27669 glyph->slice.img.y + dy),
27670 CONSP (hotspot))
27671 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27673 Lisp_Object plist;
27675 /* Could check XCAR (hotspot) to see if we enter/leave
27676 this hot-spot.
27677 If so, we could look for mouse-enter, mouse-leave
27678 properties in PLIST (and do something...). */
27679 hotspot = XCDR (hotspot);
27680 if (CONSP (hotspot)
27681 && (plist = XCAR (hotspot), CONSP (plist)))
27683 pointer = Fplist_get (plist, Qpointer);
27684 if (NILP (pointer))
27685 pointer = Qhand;
27686 help_echo_string = Fplist_get (plist, Qhelp_echo);
27687 if (!NILP (help_echo_string))
27689 help_echo_window = window;
27690 help_echo_object = glyph->object;
27691 help_echo_pos = glyph->charpos;
27695 if (NILP (pointer))
27696 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27699 #endif /* HAVE_WINDOW_SYSTEM */
27701 /* Clear mouse face if X/Y not over text. */
27702 if (glyph == NULL
27703 || area != TEXT_AREA
27704 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
27705 /* Glyph's OBJECT is an integer for glyphs inserted by the
27706 display engine for its internal purposes, like truncation
27707 and continuation glyphs and blanks beyond the end of
27708 line's text on text terminals. If we are over such a
27709 glyph, we are not over any text. */
27710 || INTEGERP (glyph->object)
27711 /* R2L rows have a stretch glyph at their front, which
27712 stands for no text, whereas L2R rows have no glyphs at
27713 all beyond the end of text. Treat such stretch glyphs
27714 like we do with NULL glyphs in L2R rows. */
27715 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27716 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
27717 && glyph->type == STRETCH_GLYPH
27718 && glyph->avoid_cursor_p))
27720 if (clear_mouse_face (hlinfo))
27721 cursor = No_Cursor;
27722 #ifdef HAVE_WINDOW_SYSTEM
27723 if (FRAME_WINDOW_P (f) && NILP (pointer))
27725 if (area != TEXT_AREA)
27726 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27727 else
27728 pointer = Vvoid_text_area_pointer;
27730 #endif
27731 goto set_cursor;
27734 pos = glyph->charpos;
27735 object = glyph->object;
27736 if (!STRINGP (object) && !BUFFERP (object))
27737 goto set_cursor;
27739 /* If we get an out-of-range value, return now; avoid an error. */
27740 if (BUFFERP (object) && pos > BUF_Z (b))
27741 goto set_cursor;
27743 /* Make the window's buffer temporarily current for
27744 overlays_at and compute_char_face. */
27745 obuf = current_buffer;
27746 current_buffer = b;
27747 obegv = BEGV;
27748 ozv = ZV;
27749 BEGV = BEG;
27750 ZV = Z;
27752 /* Is this char mouse-active or does it have help-echo? */
27753 position = make_number (pos);
27755 if (BUFFERP (object))
27757 /* Put all the overlays we want in a vector in overlay_vec. */
27758 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27759 /* Sort overlays into increasing priority order. */
27760 noverlays = sort_overlays (overlay_vec, noverlays, w);
27762 else
27763 noverlays = 0;
27765 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27767 if (same_region)
27768 cursor = No_Cursor;
27770 /* Check mouse-face highlighting. */
27771 if (! same_region
27772 /* If there exists an overlay with mouse-face overlapping
27773 the one we are currently highlighting, we have to
27774 check if we enter the overlapping overlay, and then
27775 highlight only that. */
27776 || (OVERLAYP (hlinfo->mouse_face_overlay)
27777 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27779 /* Find the highest priority overlay with a mouse-face. */
27780 Lisp_Object overlay = Qnil;
27781 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27783 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27784 if (!NILP (mouse_face))
27785 overlay = overlay_vec[i];
27788 /* If we're highlighting the same overlay as before, there's
27789 no need to do that again. */
27790 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27791 goto check_help_echo;
27792 hlinfo->mouse_face_overlay = overlay;
27794 /* Clear the display of the old active region, if any. */
27795 if (clear_mouse_face (hlinfo))
27796 cursor = No_Cursor;
27798 /* If no overlay applies, get a text property. */
27799 if (NILP (overlay))
27800 mouse_face = Fget_text_property (position, Qmouse_face, object);
27802 /* Next, compute the bounds of the mouse highlighting and
27803 display it. */
27804 if (!NILP (mouse_face) && STRINGP (object))
27806 /* The mouse-highlighting comes from a display string
27807 with a mouse-face. */
27808 Lisp_Object s, e;
27809 ptrdiff_t ignore;
27811 s = Fprevious_single_property_change
27812 (make_number (pos + 1), Qmouse_face, object, Qnil);
27813 e = Fnext_single_property_change
27814 (position, Qmouse_face, object, Qnil);
27815 if (NILP (s))
27816 s = make_number (0);
27817 if (NILP (e))
27818 e = make_number (SCHARS (object) - 1);
27819 mouse_face_from_string_pos (w, hlinfo, object,
27820 XINT (s), XINT (e));
27821 hlinfo->mouse_face_past_end = 0;
27822 hlinfo->mouse_face_window = window;
27823 hlinfo->mouse_face_face_id
27824 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27825 glyph->face_id, 1);
27826 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27827 cursor = No_Cursor;
27829 else
27831 /* The mouse-highlighting, if any, comes from an overlay
27832 or text property in the buffer. */
27833 Lisp_Object buffer IF_LINT (= Qnil);
27834 Lisp_Object disp_string IF_LINT (= Qnil);
27836 if (STRINGP (object))
27838 /* If we are on a display string with no mouse-face,
27839 check if the text under it has one. */
27840 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27841 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27842 pos = string_buffer_position (object, start);
27843 if (pos > 0)
27845 mouse_face = get_char_property_and_overlay
27846 (make_number (pos), Qmouse_face, w->contents, &overlay);
27847 buffer = w->contents;
27848 disp_string = object;
27851 else
27853 buffer = object;
27854 disp_string = Qnil;
27857 if (!NILP (mouse_face))
27859 Lisp_Object before, after;
27860 Lisp_Object before_string, after_string;
27861 /* To correctly find the limits of mouse highlight
27862 in a bidi-reordered buffer, we must not use the
27863 optimization of limiting the search in
27864 previous-single-property-change and
27865 next-single-property-change, because
27866 rows_from_pos_range needs the real start and end
27867 positions to DTRT in this case. That's because
27868 the first row visible in a window does not
27869 necessarily display the character whose position
27870 is the smallest. */
27871 Lisp_Object lim1 =
27872 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27873 ? Fmarker_position (w->start)
27874 : Qnil;
27875 Lisp_Object lim2 =
27876 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27877 ? make_number (BUF_Z (XBUFFER (buffer))
27878 - XFASTINT (w->window_end_pos))
27879 : Qnil;
27881 if (NILP (overlay))
27883 /* Handle the text property case. */
27884 before = Fprevious_single_property_change
27885 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27886 after = Fnext_single_property_change
27887 (make_number (pos), Qmouse_face, buffer, lim2);
27888 before_string = after_string = Qnil;
27890 else
27892 /* Handle the overlay case. */
27893 before = Foverlay_start (overlay);
27894 after = Foverlay_end (overlay);
27895 before_string = Foverlay_get (overlay, Qbefore_string);
27896 after_string = Foverlay_get (overlay, Qafter_string);
27898 if (!STRINGP (before_string)) before_string = Qnil;
27899 if (!STRINGP (after_string)) after_string = Qnil;
27902 mouse_face_from_buffer_pos (window, hlinfo, pos,
27903 NILP (before)
27905 : XFASTINT (before),
27906 NILP (after)
27907 ? BUF_Z (XBUFFER (buffer))
27908 : XFASTINT (after),
27909 before_string, after_string,
27910 disp_string);
27911 cursor = No_Cursor;
27916 check_help_echo:
27918 /* Look for a `help-echo' property. */
27919 if (NILP (help_echo_string)) {
27920 Lisp_Object help, overlay;
27922 /* Check overlays first. */
27923 help = overlay = Qnil;
27924 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27926 overlay = overlay_vec[i];
27927 help = Foverlay_get (overlay, Qhelp_echo);
27930 if (!NILP (help))
27932 help_echo_string = help;
27933 help_echo_window = window;
27934 help_echo_object = overlay;
27935 help_echo_pos = pos;
27937 else
27939 Lisp_Object obj = glyph->object;
27940 ptrdiff_t charpos = glyph->charpos;
27942 /* Try text properties. */
27943 if (STRINGP (obj)
27944 && charpos >= 0
27945 && charpos < SCHARS (obj))
27947 help = Fget_text_property (make_number (charpos),
27948 Qhelp_echo, obj);
27949 if (NILP (help))
27951 /* If the string itself doesn't specify a help-echo,
27952 see if the buffer text ``under'' it does. */
27953 struct glyph_row *r
27954 = MATRIX_ROW (w->current_matrix, vpos);
27955 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27956 ptrdiff_t p = string_buffer_position (obj, start);
27957 if (p > 0)
27959 help = Fget_char_property (make_number (p),
27960 Qhelp_echo, w->contents);
27961 if (!NILP (help))
27963 charpos = p;
27964 obj = w->contents;
27969 else if (BUFFERP (obj)
27970 && charpos >= BEGV
27971 && charpos < ZV)
27972 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27973 obj);
27975 if (!NILP (help))
27977 help_echo_string = help;
27978 help_echo_window = window;
27979 help_echo_object = obj;
27980 help_echo_pos = charpos;
27985 #ifdef HAVE_WINDOW_SYSTEM
27986 /* Look for a `pointer' property. */
27987 if (FRAME_WINDOW_P (f) && NILP (pointer))
27989 /* Check overlays first. */
27990 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27991 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27993 if (NILP (pointer))
27995 Lisp_Object obj = glyph->object;
27996 ptrdiff_t charpos = glyph->charpos;
27998 /* Try text properties. */
27999 if (STRINGP (obj)
28000 && charpos >= 0
28001 && charpos < SCHARS (obj))
28003 pointer = Fget_text_property (make_number (charpos),
28004 Qpointer, obj);
28005 if (NILP (pointer))
28007 /* If the string itself doesn't specify a pointer,
28008 see if the buffer text ``under'' it does. */
28009 struct glyph_row *r
28010 = MATRIX_ROW (w->current_matrix, vpos);
28011 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28012 ptrdiff_t p = string_buffer_position (obj, start);
28013 if (p > 0)
28014 pointer = Fget_char_property (make_number (p),
28015 Qpointer, w->contents);
28018 else if (BUFFERP (obj)
28019 && charpos >= BEGV
28020 && charpos < ZV)
28021 pointer = Fget_text_property (make_number (charpos),
28022 Qpointer, obj);
28025 #endif /* HAVE_WINDOW_SYSTEM */
28027 BEGV = obegv;
28028 ZV = ozv;
28029 current_buffer = obuf;
28032 set_cursor:
28034 #ifdef HAVE_WINDOW_SYSTEM
28035 if (FRAME_WINDOW_P (f))
28036 define_frame_cursor1 (f, cursor, pointer);
28037 #else
28038 /* This is here to prevent a compiler error, about "label at end of
28039 compound statement". */
28040 return;
28041 #endif
28045 /* EXPORT for RIF:
28046 Clear any mouse-face on window W. This function is part of the
28047 redisplay interface, and is called from try_window_id and similar
28048 functions to ensure the mouse-highlight is off. */
28050 void
28051 x_clear_window_mouse_face (struct window *w)
28053 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28054 Lisp_Object window;
28056 block_input ();
28057 XSETWINDOW (window, w);
28058 if (EQ (window, hlinfo->mouse_face_window))
28059 clear_mouse_face (hlinfo);
28060 unblock_input ();
28064 /* EXPORT:
28065 Just discard the mouse face information for frame F, if any.
28066 This is used when the size of F is changed. */
28068 void
28069 cancel_mouse_face (struct frame *f)
28071 Lisp_Object window;
28072 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28074 window = hlinfo->mouse_face_window;
28075 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28077 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28078 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28079 hlinfo->mouse_face_window = Qnil;
28085 /***********************************************************************
28086 Exposure Events
28087 ***********************************************************************/
28089 #ifdef HAVE_WINDOW_SYSTEM
28091 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28092 which intersects rectangle R. R is in window-relative coordinates. */
28094 static void
28095 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28096 enum glyph_row_area area)
28098 struct glyph *first = row->glyphs[area];
28099 struct glyph *end = row->glyphs[area] + row->used[area];
28100 struct glyph *last;
28101 int first_x, start_x, x;
28103 if (area == TEXT_AREA && row->fill_line_p)
28104 /* If row extends face to end of line write the whole line. */
28105 draw_glyphs (w, 0, row, area,
28106 0, row->used[area],
28107 DRAW_NORMAL_TEXT, 0);
28108 else
28110 /* Set START_X to the window-relative start position for drawing glyphs of
28111 AREA. The first glyph of the text area can be partially visible.
28112 The first glyphs of other areas cannot. */
28113 start_x = window_box_left_offset (w, area);
28114 x = start_x;
28115 if (area == TEXT_AREA)
28116 x += row->x;
28118 /* Find the first glyph that must be redrawn. */
28119 while (first < end
28120 && x + first->pixel_width < r->x)
28122 x += first->pixel_width;
28123 ++first;
28126 /* Find the last one. */
28127 last = first;
28128 first_x = x;
28129 while (last < end
28130 && x < r->x + r->width)
28132 x += last->pixel_width;
28133 ++last;
28136 /* Repaint. */
28137 if (last > first)
28138 draw_glyphs (w, first_x - start_x, row, area,
28139 first - row->glyphs[area], last - row->glyphs[area],
28140 DRAW_NORMAL_TEXT, 0);
28145 /* Redraw the parts of the glyph row ROW on window W intersecting
28146 rectangle R. R is in window-relative coordinates. Value is
28147 non-zero if mouse-face was overwritten. */
28149 static int
28150 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28152 eassert (row->enabled_p);
28154 if (row->mode_line_p || w->pseudo_window_p)
28155 draw_glyphs (w, 0, row, TEXT_AREA,
28156 0, row->used[TEXT_AREA],
28157 DRAW_NORMAL_TEXT, 0);
28158 else
28160 if (row->used[LEFT_MARGIN_AREA])
28161 expose_area (w, row, r, LEFT_MARGIN_AREA);
28162 if (row->used[TEXT_AREA])
28163 expose_area (w, row, r, TEXT_AREA);
28164 if (row->used[RIGHT_MARGIN_AREA])
28165 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28166 draw_row_fringe_bitmaps (w, row);
28169 return row->mouse_face_p;
28173 /* Redraw those parts of glyphs rows during expose event handling that
28174 overlap other rows. Redrawing of an exposed line writes over parts
28175 of lines overlapping that exposed line; this function fixes that.
28177 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28178 row in W's current matrix that is exposed and overlaps other rows.
28179 LAST_OVERLAPPING_ROW is the last such row. */
28181 static void
28182 expose_overlaps (struct window *w,
28183 struct glyph_row *first_overlapping_row,
28184 struct glyph_row *last_overlapping_row,
28185 XRectangle *r)
28187 struct glyph_row *row;
28189 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28190 if (row->overlapping_p)
28192 eassert (row->enabled_p && !row->mode_line_p);
28194 row->clip = r;
28195 if (row->used[LEFT_MARGIN_AREA])
28196 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28198 if (row->used[TEXT_AREA])
28199 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28201 if (row->used[RIGHT_MARGIN_AREA])
28202 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28203 row->clip = NULL;
28208 /* Return non-zero if W's cursor intersects rectangle R. */
28210 static int
28211 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28213 XRectangle cr, result;
28214 struct glyph *cursor_glyph;
28215 struct glyph_row *row;
28217 if (w->phys_cursor.vpos >= 0
28218 && w->phys_cursor.vpos < w->current_matrix->nrows
28219 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28220 row->enabled_p)
28221 && row->cursor_in_fringe_p)
28223 /* Cursor is in the fringe. */
28224 cr.x = window_box_right_offset (w,
28225 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28226 ? RIGHT_MARGIN_AREA
28227 : TEXT_AREA));
28228 cr.y = row->y;
28229 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28230 cr.height = row->height;
28231 return x_intersect_rectangles (&cr, r, &result);
28234 cursor_glyph = get_phys_cursor_glyph (w);
28235 if (cursor_glyph)
28237 /* r is relative to W's box, but w->phys_cursor.x is relative
28238 to left edge of W's TEXT area. Adjust it. */
28239 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28240 cr.y = w->phys_cursor.y;
28241 cr.width = cursor_glyph->pixel_width;
28242 cr.height = w->phys_cursor_height;
28243 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28244 I assume the effect is the same -- and this is portable. */
28245 return x_intersect_rectangles (&cr, r, &result);
28247 /* If we don't understand the format, pretend we're not in the hot-spot. */
28248 return 0;
28252 /* EXPORT:
28253 Draw a vertical window border to the right of window W if W doesn't
28254 have vertical scroll bars. */
28256 void
28257 x_draw_vertical_border (struct window *w)
28259 struct frame *f = XFRAME (WINDOW_FRAME (w));
28261 /* We could do better, if we knew what type of scroll-bar the adjacent
28262 windows (on either side) have... But we don't :-(
28263 However, I think this works ok. ++KFS 2003-04-25 */
28265 /* Redraw borders between horizontally adjacent windows. Don't
28266 do it for frames with vertical scroll bars because either the
28267 right scroll bar of a window, or the left scroll bar of its
28268 neighbor will suffice as a border. */
28269 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28270 return;
28272 /* Note: It is necessary to redraw both the left and the right
28273 borders, for when only this single window W is being
28274 redisplayed. */
28275 if (!WINDOW_RIGHTMOST_P (w)
28276 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28278 int x0, x1, y0, y1;
28280 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28281 y1 -= 1;
28283 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28284 x1 -= 1;
28286 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28288 if (!WINDOW_LEFTMOST_P (w)
28289 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28291 int x0, x1, y0, y1;
28293 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28294 y1 -= 1;
28296 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28297 x0 -= 1;
28299 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28304 /* Redraw the part of window W intersection rectangle FR. Pixel
28305 coordinates in FR are frame-relative. Call this function with
28306 input blocked. Value is non-zero if the exposure overwrites
28307 mouse-face. */
28309 static int
28310 expose_window (struct window *w, XRectangle *fr)
28312 struct frame *f = XFRAME (w->frame);
28313 XRectangle wr, r;
28314 int mouse_face_overwritten_p = 0;
28316 /* If window is not yet fully initialized, do nothing. This can
28317 happen when toolkit scroll bars are used and a window is split.
28318 Reconfiguring the scroll bar will generate an expose for a newly
28319 created window. */
28320 if (w->current_matrix == NULL)
28321 return 0;
28323 /* When we're currently updating the window, display and current
28324 matrix usually don't agree. Arrange for a thorough display
28325 later. */
28326 if (w == updated_window)
28328 SET_FRAME_GARBAGED (f);
28329 return 0;
28332 /* Frame-relative pixel rectangle of W. */
28333 wr.x = WINDOW_LEFT_EDGE_X (w);
28334 wr.y = WINDOW_TOP_EDGE_Y (w);
28335 wr.width = WINDOW_TOTAL_WIDTH (w);
28336 wr.height = WINDOW_TOTAL_HEIGHT (w);
28338 if (x_intersect_rectangles (fr, &wr, &r))
28340 int yb = window_text_bottom_y (w);
28341 struct glyph_row *row;
28342 int cursor_cleared_p, phys_cursor_on_p;
28343 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28345 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28346 r.x, r.y, r.width, r.height));
28348 /* Convert to window coordinates. */
28349 r.x -= WINDOW_LEFT_EDGE_X (w);
28350 r.y -= WINDOW_TOP_EDGE_Y (w);
28352 /* Turn off the cursor. */
28353 if (!w->pseudo_window_p
28354 && phys_cursor_in_rect_p (w, &r))
28356 x_clear_cursor (w);
28357 cursor_cleared_p = 1;
28359 else
28360 cursor_cleared_p = 0;
28362 /* If the row containing the cursor extends face to end of line,
28363 then expose_area might overwrite the cursor outside the
28364 rectangle and thus notice_overwritten_cursor might clear
28365 w->phys_cursor_on_p. We remember the original value and
28366 check later if it is changed. */
28367 phys_cursor_on_p = w->phys_cursor_on_p;
28369 /* Update lines intersecting rectangle R. */
28370 first_overlapping_row = last_overlapping_row = NULL;
28371 for (row = w->current_matrix->rows;
28372 row->enabled_p;
28373 ++row)
28375 int y0 = row->y;
28376 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28378 if ((y0 >= r.y && y0 < r.y + r.height)
28379 || (y1 > r.y && y1 < r.y + r.height)
28380 || (r.y >= y0 && r.y < y1)
28381 || (r.y + r.height > y0 && r.y + r.height < y1))
28383 /* A header line may be overlapping, but there is no need
28384 to fix overlapping areas for them. KFS 2005-02-12 */
28385 if (row->overlapping_p && !row->mode_line_p)
28387 if (first_overlapping_row == NULL)
28388 first_overlapping_row = row;
28389 last_overlapping_row = row;
28392 row->clip = fr;
28393 if (expose_line (w, row, &r))
28394 mouse_face_overwritten_p = 1;
28395 row->clip = NULL;
28397 else if (row->overlapping_p)
28399 /* We must redraw a row overlapping the exposed area. */
28400 if (y0 < r.y
28401 ? y0 + row->phys_height > r.y
28402 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28404 if (first_overlapping_row == NULL)
28405 first_overlapping_row = row;
28406 last_overlapping_row = row;
28410 if (y1 >= yb)
28411 break;
28414 /* Display the mode line if there is one. */
28415 if (WINDOW_WANTS_MODELINE_P (w)
28416 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28417 row->enabled_p)
28418 && row->y < r.y + r.height)
28420 if (expose_line (w, row, &r))
28421 mouse_face_overwritten_p = 1;
28424 if (!w->pseudo_window_p)
28426 /* Fix the display of overlapping rows. */
28427 if (first_overlapping_row)
28428 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28429 fr);
28431 /* Draw border between windows. */
28432 x_draw_vertical_border (w);
28434 /* Turn the cursor on again. */
28435 if (cursor_cleared_p
28436 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28437 update_window_cursor (w, 1);
28441 return mouse_face_overwritten_p;
28446 /* Redraw (parts) of all windows in the window tree rooted at W that
28447 intersect R. R contains frame pixel coordinates. Value is
28448 non-zero if the exposure overwrites mouse-face. */
28450 static int
28451 expose_window_tree (struct window *w, XRectangle *r)
28453 struct frame *f = XFRAME (w->frame);
28454 int mouse_face_overwritten_p = 0;
28456 while (w && !FRAME_GARBAGED_P (f))
28458 if (WINDOWP (w->contents))
28459 mouse_face_overwritten_p
28460 |= expose_window_tree (XWINDOW (w->contents), r);
28461 else
28462 mouse_face_overwritten_p |= expose_window (w, r);
28464 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28467 return mouse_face_overwritten_p;
28471 /* EXPORT:
28472 Redisplay an exposed area of frame F. X and Y are the upper-left
28473 corner of the exposed rectangle. W and H are width and height of
28474 the exposed area. All are pixel values. W or H zero means redraw
28475 the entire frame. */
28477 void
28478 expose_frame (struct frame *f, int x, int y, int w, int h)
28480 XRectangle r;
28481 int mouse_face_overwritten_p = 0;
28483 TRACE ((stderr, "expose_frame "));
28485 /* No need to redraw if frame will be redrawn soon. */
28486 if (FRAME_GARBAGED_P (f))
28488 TRACE ((stderr, " garbaged\n"));
28489 return;
28492 /* If basic faces haven't been realized yet, there is no point in
28493 trying to redraw anything. This can happen when we get an expose
28494 event while Emacs is starting, e.g. by moving another window. */
28495 if (FRAME_FACE_CACHE (f) == NULL
28496 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28498 TRACE ((stderr, " no faces\n"));
28499 return;
28502 if (w == 0 || h == 0)
28504 r.x = r.y = 0;
28505 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28506 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28508 else
28510 r.x = x;
28511 r.y = y;
28512 r.width = w;
28513 r.height = h;
28516 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28517 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28519 if (WINDOWP (f->tool_bar_window))
28520 mouse_face_overwritten_p
28521 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28523 #ifdef HAVE_X_WINDOWS
28524 #ifndef MSDOS
28525 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28526 if (WINDOWP (f->menu_bar_window))
28527 mouse_face_overwritten_p
28528 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28529 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28530 #endif
28531 #endif
28533 /* Some window managers support a focus-follows-mouse style with
28534 delayed raising of frames. Imagine a partially obscured frame,
28535 and moving the mouse into partially obscured mouse-face on that
28536 frame. The visible part of the mouse-face will be highlighted,
28537 then the WM raises the obscured frame. With at least one WM, KDE
28538 2.1, Emacs is not getting any event for the raising of the frame
28539 (even tried with SubstructureRedirectMask), only Expose events.
28540 These expose events will draw text normally, i.e. not
28541 highlighted. Which means we must redo the highlight here.
28542 Subsume it under ``we love X''. --gerd 2001-08-15 */
28543 /* Included in Windows version because Windows most likely does not
28544 do the right thing if any third party tool offers
28545 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28546 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28548 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28549 if (f == hlinfo->mouse_face_mouse_frame)
28551 int mouse_x = hlinfo->mouse_face_mouse_x;
28552 int mouse_y = hlinfo->mouse_face_mouse_y;
28553 clear_mouse_face (hlinfo);
28554 note_mouse_highlight (f, mouse_x, mouse_y);
28560 /* EXPORT:
28561 Determine the intersection of two rectangles R1 and R2. Return
28562 the intersection in *RESULT. Value is non-zero if RESULT is not
28563 empty. */
28566 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28568 XRectangle *left, *right;
28569 XRectangle *upper, *lower;
28570 int intersection_p = 0;
28572 /* Rearrange so that R1 is the left-most rectangle. */
28573 if (r1->x < r2->x)
28574 left = r1, right = r2;
28575 else
28576 left = r2, right = r1;
28578 /* X0 of the intersection is right.x0, if this is inside R1,
28579 otherwise there is no intersection. */
28580 if (right->x <= left->x + left->width)
28582 result->x = right->x;
28584 /* The right end of the intersection is the minimum of
28585 the right ends of left and right. */
28586 result->width = (min (left->x + left->width, right->x + right->width)
28587 - result->x);
28589 /* Same game for Y. */
28590 if (r1->y < r2->y)
28591 upper = r1, lower = r2;
28592 else
28593 upper = r2, lower = r1;
28595 /* The upper end of the intersection is lower.y0, if this is inside
28596 of upper. Otherwise, there is no intersection. */
28597 if (lower->y <= upper->y + upper->height)
28599 result->y = lower->y;
28601 /* The lower end of the intersection is the minimum of the lower
28602 ends of upper and lower. */
28603 result->height = (min (lower->y + lower->height,
28604 upper->y + upper->height)
28605 - result->y);
28606 intersection_p = 1;
28610 return intersection_p;
28613 #endif /* HAVE_WINDOW_SYSTEM */
28616 /***********************************************************************
28617 Initialization
28618 ***********************************************************************/
28620 void
28621 syms_of_xdisp (void)
28623 Vwith_echo_area_save_vector = Qnil;
28624 staticpro (&Vwith_echo_area_save_vector);
28626 Vmessage_stack = Qnil;
28627 staticpro (&Vmessage_stack);
28629 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28630 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28632 message_dolog_marker1 = Fmake_marker ();
28633 staticpro (&message_dolog_marker1);
28634 message_dolog_marker2 = Fmake_marker ();
28635 staticpro (&message_dolog_marker2);
28636 message_dolog_marker3 = Fmake_marker ();
28637 staticpro (&message_dolog_marker3);
28639 #ifdef GLYPH_DEBUG
28640 defsubr (&Sdump_frame_glyph_matrix);
28641 defsubr (&Sdump_glyph_matrix);
28642 defsubr (&Sdump_glyph_row);
28643 defsubr (&Sdump_tool_bar_row);
28644 defsubr (&Strace_redisplay);
28645 defsubr (&Strace_to_stderr);
28646 #endif
28647 #ifdef HAVE_WINDOW_SYSTEM
28648 defsubr (&Stool_bar_lines_needed);
28649 defsubr (&Slookup_image_map);
28650 #endif
28651 defsubr (&Sformat_mode_line);
28652 defsubr (&Sinvisible_p);
28653 defsubr (&Scurrent_bidi_paragraph_direction);
28655 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28656 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28657 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28658 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28659 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28660 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28661 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28662 DEFSYM (Qeval, "eval");
28663 DEFSYM (QCdata, ":data");
28664 DEFSYM (Qdisplay, "display");
28665 DEFSYM (Qspace_width, "space-width");
28666 DEFSYM (Qraise, "raise");
28667 DEFSYM (Qslice, "slice");
28668 DEFSYM (Qspace, "space");
28669 DEFSYM (Qmargin, "margin");
28670 DEFSYM (Qpointer, "pointer");
28671 DEFSYM (Qleft_margin, "left-margin");
28672 DEFSYM (Qright_margin, "right-margin");
28673 DEFSYM (Qcenter, "center");
28674 DEFSYM (Qline_height, "line-height");
28675 DEFSYM (QCalign_to, ":align-to");
28676 DEFSYM (QCrelative_width, ":relative-width");
28677 DEFSYM (QCrelative_height, ":relative-height");
28678 DEFSYM (QCeval, ":eval");
28679 DEFSYM (QCpropertize, ":propertize");
28680 DEFSYM (QCfile, ":file");
28681 DEFSYM (Qfontified, "fontified");
28682 DEFSYM (Qfontification_functions, "fontification-functions");
28683 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28684 DEFSYM (Qescape_glyph, "escape-glyph");
28685 DEFSYM (Qnobreak_space, "nobreak-space");
28686 DEFSYM (Qimage, "image");
28687 DEFSYM (Qtext, "text");
28688 DEFSYM (Qboth, "both");
28689 DEFSYM (Qboth_horiz, "both-horiz");
28690 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28691 DEFSYM (QCmap, ":map");
28692 DEFSYM (QCpointer, ":pointer");
28693 DEFSYM (Qrect, "rect");
28694 DEFSYM (Qcircle, "circle");
28695 DEFSYM (Qpoly, "poly");
28696 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28697 DEFSYM (Qgrow_only, "grow-only");
28698 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28699 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28700 DEFSYM (Qposition, "position");
28701 DEFSYM (Qbuffer_position, "buffer-position");
28702 DEFSYM (Qobject, "object");
28703 DEFSYM (Qbar, "bar");
28704 DEFSYM (Qhbar, "hbar");
28705 DEFSYM (Qbox, "box");
28706 DEFSYM (Qhollow, "hollow");
28707 DEFSYM (Qhand, "hand");
28708 DEFSYM (Qarrow, "arrow");
28709 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28711 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28712 Fcons (intern_c_string ("void-variable"), Qnil)),
28713 Qnil);
28714 staticpro (&list_of_error);
28716 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28717 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28718 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28719 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28721 echo_buffer[0] = echo_buffer[1] = Qnil;
28722 staticpro (&echo_buffer[0]);
28723 staticpro (&echo_buffer[1]);
28725 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28726 staticpro (&echo_area_buffer[0]);
28727 staticpro (&echo_area_buffer[1]);
28729 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28730 staticpro (&Vmessages_buffer_name);
28732 mode_line_proptrans_alist = Qnil;
28733 staticpro (&mode_line_proptrans_alist);
28734 mode_line_string_list = Qnil;
28735 staticpro (&mode_line_string_list);
28736 mode_line_string_face = Qnil;
28737 staticpro (&mode_line_string_face);
28738 mode_line_string_face_prop = Qnil;
28739 staticpro (&mode_line_string_face_prop);
28740 Vmode_line_unwind_vector = Qnil;
28741 staticpro (&Vmode_line_unwind_vector);
28743 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28745 help_echo_string = Qnil;
28746 staticpro (&help_echo_string);
28747 help_echo_object = Qnil;
28748 staticpro (&help_echo_object);
28749 help_echo_window = Qnil;
28750 staticpro (&help_echo_window);
28751 previous_help_echo_string = Qnil;
28752 staticpro (&previous_help_echo_string);
28753 help_echo_pos = -1;
28755 DEFSYM (Qright_to_left, "right-to-left");
28756 DEFSYM (Qleft_to_right, "left-to-right");
28758 #ifdef HAVE_WINDOW_SYSTEM
28759 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28760 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28761 For example, if a block cursor is over a tab, it will be drawn as
28762 wide as that tab on the display. */);
28763 x_stretch_cursor_p = 0;
28764 #endif
28766 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28767 doc: /* Non-nil means highlight trailing whitespace.
28768 The face used for trailing whitespace is `trailing-whitespace'. */);
28769 Vshow_trailing_whitespace = Qnil;
28771 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28772 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28773 If the value is t, Emacs highlights non-ASCII chars which have the
28774 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28775 or `escape-glyph' face respectively.
28777 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28778 U+2011 (non-breaking hyphen) are affected.
28780 Any other non-nil value means to display these characters as a escape
28781 glyph followed by an ordinary space or hyphen.
28783 A value of nil means no special handling of these characters. */);
28784 Vnobreak_char_display = Qt;
28786 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28787 doc: /* The pointer shape to show in void text areas.
28788 A value of nil means to show the text pointer. Other options are `arrow',
28789 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28790 Vvoid_text_area_pointer = Qarrow;
28792 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28793 doc: /* Non-nil means don't actually do any redisplay.
28794 This is used for internal purposes. */);
28795 Vinhibit_redisplay = Qnil;
28797 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28798 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28799 Vglobal_mode_string = Qnil;
28801 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28802 doc: /* Marker for where to display an arrow on top of the buffer text.
28803 This must be the beginning of a line in order to work.
28804 See also `overlay-arrow-string'. */);
28805 Voverlay_arrow_position = Qnil;
28807 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28808 doc: /* String to display as an arrow in non-window frames.
28809 See also `overlay-arrow-position'. */);
28810 Voverlay_arrow_string = build_pure_c_string ("=>");
28812 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28813 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28814 The symbols on this list are examined during redisplay to determine
28815 where to display overlay arrows. */);
28816 Voverlay_arrow_variable_list
28817 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28819 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28820 doc: /* The number of lines to try scrolling a window by when point moves out.
28821 If that fails to bring point back on frame, point is centered instead.
28822 If this is zero, point is always centered after it moves off frame.
28823 If you want scrolling to always be a line at a time, you should set
28824 `scroll-conservatively' to a large value rather than set this to 1. */);
28826 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28827 doc: /* Scroll up to this many lines, to bring point back on screen.
28828 If point moves off-screen, redisplay will scroll by up to
28829 `scroll-conservatively' lines in order to bring point just barely
28830 onto the screen again. If that cannot be done, then redisplay
28831 recenters point as usual.
28833 If the value is greater than 100, redisplay will never recenter point,
28834 but will always scroll just enough text to bring point into view, even
28835 if you move far away.
28837 A value of zero means always recenter point if it moves off screen. */);
28838 scroll_conservatively = 0;
28840 DEFVAR_INT ("scroll-margin", scroll_margin,
28841 doc: /* Number of lines of margin at the top and bottom of a window.
28842 Recenter the window whenever point gets within this many lines
28843 of the top or bottom of the window. */);
28844 scroll_margin = 0;
28846 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28847 doc: /* Pixels per inch value for non-window system displays.
28848 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28849 Vdisplay_pixels_per_inch = make_float (72.0);
28851 #ifdef GLYPH_DEBUG
28852 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28853 #endif
28855 DEFVAR_LISP ("truncate-partial-width-windows",
28856 Vtruncate_partial_width_windows,
28857 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28858 For an integer value, truncate lines in each window narrower than the
28859 full frame width, provided the window width is less than that integer;
28860 otherwise, respect the value of `truncate-lines'.
28862 For any other non-nil value, truncate lines in all windows that do
28863 not span the full frame width.
28865 A value of nil means to respect the value of `truncate-lines'.
28867 If `word-wrap' is enabled, you might want to reduce this. */);
28868 Vtruncate_partial_width_windows = make_number (50);
28870 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28871 doc: /* Maximum buffer size for which line number should be displayed.
28872 If the buffer is bigger than this, the line number does not appear
28873 in the mode line. A value of nil means no limit. */);
28874 Vline_number_display_limit = Qnil;
28876 DEFVAR_INT ("line-number-display-limit-width",
28877 line_number_display_limit_width,
28878 doc: /* Maximum line width (in characters) for line number display.
28879 If the average length of the lines near point is bigger than this, then the
28880 line number may be omitted from the mode line. */);
28881 line_number_display_limit_width = 200;
28883 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28884 doc: /* Non-nil means highlight region even in nonselected windows. */);
28885 highlight_nonselected_windows = 0;
28887 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28888 doc: /* Non-nil if more than one frame is visible on this display.
28889 Minibuffer-only frames don't count, but iconified frames do.
28890 This variable is not guaranteed to be accurate except while processing
28891 `frame-title-format' and `icon-title-format'. */);
28893 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28894 doc: /* Template for displaying the title bar of visible frames.
28895 \(Assuming the window manager supports this feature.)
28897 This variable has the same structure as `mode-line-format', except that
28898 the %c and %l constructs are ignored. It is used only on frames for
28899 which no explicit name has been set \(see `modify-frame-parameters'). */);
28901 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28902 doc: /* Template for displaying the title bar of an iconified frame.
28903 \(Assuming the window manager supports this feature.)
28904 This variable has the same structure as `mode-line-format' (which see),
28905 and is used only on frames for which no explicit name has been set
28906 \(see `modify-frame-parameters'). */);
28907 Vicon_title_format
28908 = Vframe_title_format
28909 = listn (CONSTYPE_PURE, 3,
28910 intern_c_string ("multiple-frames"),
28911 build_pure_c_string ("%b"),
28912 listn (CONSTYPE_PURE, 4,
28913 empty_unibyte_string,
28914 intern_c_string ("invocation-name"),
28915 build_pure_c_string ("@"),
28916 intern_c_string ("system-name")));
28918 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28919 doc: /* Maximum number of lines to keep in the message log buffer.
28920 If nil, disable message logging. If t, log messages but don't truncate
28921 the buffer when it becomes large. */);
28922 Vmessage_log_max = make_number (1000);
28924 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28925 doc: /* Functions called before redisplay, if window sizes have changed.
28926 The value should be a list of functions that take one argument.
28927 Just before redisplay, for each frame, if any of its windows have changed
28928 size since the last redisplay, or have been split or deleted,
28929 all the functions in the list are called, with the frame as argument. */);
28930 Vwindow_size_change_functions = Qnil;
28932 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28933 doc: /* List of functions to call before redisplaying a window with scrolling.
28934 Each function is called with two arguments, the window and its new
28935 display-start position. Note that these functions are also called by
28936 `set-window-buffer'. Also note that the value of `window-end' is not
28937 valid when these functions are called.
28939 Warning: Do not use this feature to alter the way the window
28940 is scrolled. It is not designed for that, and such use probably won't
28941 work. */);
28942 Vwindow_scroll_functions = Qnil;
28944 DEFVAR_LISP ("window-text-change-functions",
28945 Vwindow_text_change_functions,
28946 doc: /* Functions to call in redisplay when text in the window might change. */);
28947 Vwindow_text_change_functions = Qnil;
28949 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28950 doc: /* Functions called when redisplay of a window reaches the end trigger.
28951 Each function is called with two arguments, the window and the end trigger value.
28952 See `set-window-redisplay-end-trigger'. */);
28953 Vredisplay_end_trigger_functions = Qnil;
28955 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28956 doc: /* Non-nil means autoselect window with mouse pointer.
28957 If nil, do not autoselect windows.
28958 A positive number means delay autoselection by that many seconds: a
28959 window is autoselected only after the mouse has remained in that
28960 window for the duration of the delay.
28961 A negative number has a similar effect, but causes windows to be
28962 autoselected only after the mouse has stopped moving. \(Because of
28963 the way Emacs compares mouse events, you will occasionally wait twice
28964 that time before the window gets selected.\)
28965 Any other value means to autoselect window instantaneously when the
28966 mouse pointer enters it.
28968 Autoselection selects the minibuffer only if it is active, and never
28969 unselects the minibuffer if it is active.
28971 When customizing this variable make sure that the actual value of
28972 `focus-follows-mouse' matches the behavior of your window manager. */);
28973 Vmouse_autoselect_window = Qnil;
28975 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28976 doc: /* Non-nil means automatically resize tool-bars.
28977 This dynamically changes the tool-bar's height to the minimum height
28978 that is needed to make all tool-bar items visible.
28979 If value is `grow-only', the tool-bar's height is only increased
28980 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28981 Vauto_resize_tool_bars = Qt;
28983 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28984 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28985 auto_raise_tool_bar_buttons_p = 1;
28987 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28988 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28989 make_cursor_line_fully_visible_p = 1;
28991 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28992 doc: /* Border below tool-bar in pixels.
28993 If an integer, use it as the height of the border.
28994 If it is one of `internal-border-width' or `border-width', use the
28995 value of the corresponding frame parameter.
28996 Otherwise, no border is added below the tool-bar. */);
28997 Vtool_bar_border = Qinternal_border_width;
28999 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29000 doc: /* Margin around tool-bar buttons in pixels.
29001 If an integer, use that for both horizontal and vertical margins.
29002 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29003 HORZ specifying the horizontal margin, and VERT specifying the
29004 vertical margin. */);
29005 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29007 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29008 doc: /* Relief thickness of tool-bar buttons. */);
29009 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29011 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29012 doc: /* Tool bar style to use.
29013 It can be one of
29014 image - show images only
29015 text - show text only
29016 both - show both, text below image
29017 both-horiz - show text to the right of the image
29018 text-image-horiz - show text to the left of the image
29019 any other - use system default or image if no system default.
29021 This variable only affects the GTK+ toolkit version of Emacs. */);
29022 Vtool_bar_style = Qnil;
29024 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29025 doc: /* Maximum number of characters a label can have to be shown.
29026 The tool bar style must also show labels for this to have any effect, see
29027 `tool-bar-style'. */);
29028 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29030 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29031 doc: /* List of functions to call to fontify regions of text.
29032 Each function is called with one argument POS. Functions must
29033 fontify a region starting at POS in the current buffer, and give
29034 fontified regions the property `fontified'. */);
29035 Vfontification_functions = Qnil;
29036 Fmake_variable_buffer_local (Qfontification_functions);
29038 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29039 unibyte_display_via_language_environment,
29040 doc: /* Non-nil means display unibyte text according to language environment.
29041 Specifically, this means that raw bytes in the range 160-255 decimal
29042 are displayed by converting them to the equivalent multibyte characters
29043 according to the current language environment. As a result, they are
29044 displayed according to the current fontset.
29046 Note that this variable affects only how these bytes are displayed,
29047 but does not change the fact they are interpreted as raw bytes. */);
29048 unibyte_display_via_language_environment = 0;
29050 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29051 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29052 If a float, it specifies a fraction of the mini-window frame's height.
29053 If an integer, it specifies a number of lines. */);
29054 Vmax_mini_window_height = make_float (0.25);
29056 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29057 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29058 A value of nil means don't automatically resize mini-windows.
29059 A value of t means resize them to fit the text displayed in them.
29060 A value of `grow-only', the default, means let mini-windows grow only;
29061 they return to their normal size when the minibuffer is closed, or the
29062 echo area becomes empty. */);
29063 Vresize_mini_windows = Qgrow_only;
29065 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29066 doc: /* Alist specifying how to blink the cursor off.
29067 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29068 `cursor-type' frame-parameter or variable equals ON-STATE,
29069 comparing using `equal', Emacs uses OFF-STATE to specify
29070 how to blink it off. ON-STATE and OFF-STATE are values for
29071 the `cursor-type' frame parameter.
29073 If a frame's ON-STATE has no entry in this list,
29074 the frame's other specifications determine how to blink the cursor off. */);
29075 Vblink_cursor_alist = Qnil;
29077 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29078 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29079 If non-nil, windows are automatically scrolled horizontally to make
29080 point visible. */);
29081 automatic_hscrolling_p = 1;
29082 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29084 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29085 doc: /* How many columns away from the window edge point is allowed to get
29086 before automatic hscrolling will horizontally scroll the window. */);
29087 hscroll_margin = 5;
29089 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29090 doc: /* How many columns to scroll the window when point gets too close to the edge.
29091 When point is less than `hscroll-margin' columns from the window
29092 edge, automatic hscrolling will scroll the window by the amount of columns
29093 determined by this variable. If its value is a positive integer, scroll that
29094 many columns. If it's a positive floating-point number, it specifies the
29095 fraction of the window's width to scroll. If it's nil or zero, point will be
29096 centered horizontally after the scroll. Any other value, including negative
29097 numbers, are treated as if the value were zero.
29099 Automatic hscrolling always moves point outside the scroll margin, so if
29100 point was more than scroll step columns inside the margin, the window will
29101 scroll more than the value given by the scroll step.
29103 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29104 and `scroll-right' overrides this variable's effect. */);
29105 Vhscroll_step = make_number (0);
29107 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29108 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29109 Bind this around calls to `message' to let it take effect. */);
29110 message_truncate_lines = 0;
29112 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29113 doc: /* Normal hook run to update the menu bar definitions.
29114 Redisplay runs this hook before it redisplays the menu bar.
29115 This is used to update submenus such as Buffers,
29116 whose contents depend on various data. */);
29117 Vmenu_bar_update_hook = Qnil;
29119 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29120 doc: /* Frame for which we are updating a menu.
29121 The enable predicate for a menu binding should check this variable. */);
29122 Vmenu_updating_frame = Qnil;
29124 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29125 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29126 inhibit_menubar_update = 0;
29128 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29129 doc: /* Prefix prepended to all continuation lines at display time.
29130 The value may be a string, an image, or a stretch-glyph; it is
29131 interpreted in the same way as the value of a `display' text property.
29133 This variable is overridden by any `wrap-prefix' text or overlay
29134 property.
29136 To add a prefix to non-continuation lines, use `line-prefix'. */);
29137 Vwrap_prefix = Qnil;
29138 DEFSYM (Qwrap_prefix, "wrap-prefix");
29139 Fmake_variable_buffer_local (Qwrap_prefix);
29141 DEFVAR_LISP ("line-prefix", Vline_prefix,
29142 doc: /* Prefix prepended to all non-continuation lines at display time.
29143 The value may be a string, an image, or a stretch-glyph; it is
29144 interpreted in the same way as the value of a `display' text property.
29146 This variable is overridden by any `line-prefix' text or overlay
29147 property.
29149 To add a prefix to continuation lines, use `wrap-prefix'. */);
29150 Vline_prefix = Qnil;
29151 DEFSYM (Qline_prefix, "line-prefix");
29152 Fmake_variable_buffer_local (Qline_prefix);
29154 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29155 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29156 inhibit_eval_during_redisplay = 0;
29158 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29159 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29160 inhibit_free_realized_faces = 0;
29162 #ifdef GLYPH_DEBUG
29163 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29164 doc: /* Inhibit try_window_id display optimization. */);
29165 inhibit_try_window_id = 0;
29167 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29168 doc: /* Inhibit try_window_reusing display optimization. */);
29169 inhibit_try_window_reusing = 0;
29171 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29172 doc: /* Inhibit try_cursor_movement display optimization. */);
29173 inhibit_try_cursor_movement = 0;
29174 #endif /* GLYPH_DEBUG */
29176 DEFVAR_INT ("overline-margin", overline_margin,
29177 doc: /* Space between overline and text, in pixels.
29178 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29179 margin to the character height. */);
29180 overline_margin = 2;
29182 DEFVAR_INT ("underline-minimum-offset",
29183 underline_minimum_offset,
29184 doc: /* Minimum distance between baseline and underline.
29185 This can improve legibility of underlined text at small font sizes,
29186 particularly when using variable `x-use-underline-position-properties'
29187 with fonts that specify an UNDERLINE_POSITION relatively close to the
29188 baseline. The default value is 1. */);
29189 underline_minimum_offset = 1;
29191 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29192 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29193 This feature only works when on a window system that can change
29194 cursor shapes. */);
29195 display_hourglass_p = 1;
29197 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29198 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29199 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29201 hourglass_atimer = NULL;
29202 hourglass_shown_p = 0;
29204 DEFSYM (Qglyphless_char, "glyphless-char");
29205 DEFSYM (Qhex_code, "hex-code");
29206 DEFSYM (Qempty_box, "empty-box");
29207 DEFSYM (Qthin_space, "thin-space");
29208 DEFSYM (Qzero_width, "zero-width");
29210 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29211 /* Intern this now in case it isn't already done.
29212 Setting this variable twice is harmless.
29213 But don't staticpro it here--that is done in alloc.c. */
29214 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29215 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29217 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29218 doc: /* Char-table defining glyphless characters.
29219 Each element, if non-nil, should be one of the following:
29220 an ASCII acronym string: display this string in a box
29221 `hex-code': display the hexadecimal code of a character in a box
29222 `empty-box': display as an empty box
29223 `thin-space': display as 1-pixel width space
29224 `zero-width': don't display
29225 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29226 display method for graphical terminals and text terminals respectively.
29227 GRAPHICAL and TEXT should each have one of the values listed above.
29229 The char-table has one extra slot to control the display of a character for
29230 which no font is found. This slot only takes effect on graphical terminals.
29231 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29232 `thin-space'. The default is `empty-box'. */);
29233 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29234 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29235 Qempty_box);
29237 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29238 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29239 Vdebug_on_message = Qnil;
29243 /* Initialize this module when Emacs starts. */
29245 void
29246 init_xdisp (void)
29248 current_header_line_height = current_mode_line_height = -1;
29250 CHARPOS (this_line_start_pos) = 0;
29252 if (!noninteractive)
29254 struct window *m = XWINDOW (minibuf_window);
29255 Lisp_Object frame = m->frame;
29256 struct frame *f = XFRAME (frame);
29257 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29258 struct window *r = XWINDOW (root);
29259 int i;
29261 echo_area_window = minibuf_window;
29263 r->top_line = FRAME_TOP_MARGIN (f);
29264 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29265 r->total_cols = FRAME_COLS (f);
29267 m->top_line = FRAME_LINES (f) - 1;
29268 m->total_lines = 1;
29269 m->total_cols = FRAME_COLS (f);
29271 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29272 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29273 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29275 /* The default ellipsis glyphs `...'. */
29276 for (i = 0; i < 3; ++i)
29277 default_invis_vector[i] = make_number ('.');
29281 /* Allocate the buffer for frame titles.
29282 Also used for `format-mode-line'. */
29283 int size = 100;
29284 mode_line_noprop_buf = xmalloc (size);
29285 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29286 mode_line_noprop_ptr = mode_line_noprop_buf;
29287 mode_line_target = MODE_LINE_DISPLAY;
29290 help_echo_showing_p = 0;
29293 /* Platform-independent portion of hourglass implementation. */
29295 /* Cancel a currently active hourglass timer, and start a new one. */
29296 void
29297 start_hourglass (void)
29299 #if defined (HAVE_WINDOW_SYSTEM)
29300 EMACS_TIME delay;
29302 cancel_hourglass ();
29304 if (INTEGERP (Vhourglass_delay)
29305 && XINT (Vhourglass_delay) > 0)
29306 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29307 TYPE_MAXIMUM (time_t)),
29309 else if (FLOATP (Vhourglass_delay)
29310 && XFLOAT_DATA (Vhourglass_delay) > 0)
29311 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29312 else
29313 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29315 #ifdef HAVE_NTGUI
29317 extern void w32_note_current_window (void);
29318 w32_note_current_window ();
29320 #endif /* HAVE_NTGUI */
29322 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29323 show_hourglass, NULL);
29324 #endif
29328 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29329 shown. */
29330 void
29331 cancel_hourglass (void)
29333 #if defined (HAVE_WINDOW_SYSTEM)
29334 if (hourglass_atimer)
29336 cancel_atimer (hourglass_atimer);
29337 hourglass_atimer = NULL;
29340 if (hourglass_shown_p)
29341 hide_hourglass ();
29342 #endif