Merge from emacs-24; up to 2012-12-14T21:27:39Z!rgm@gnu.org
[emacs.git] / src / xdisp.c
blob02f2ae5d9617205b28ba2542fe5015c32b60100f
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_max_ascent, last_height;
585 /* Non-zero if there's a help-echo in the echo area. */
587 int help_echo_showing_p;
589 /* If >= 0, computed, exact values of mode-line and header-line height
590 to use in the macros CURRENT_MODE_LINE_HEIGHT and
591 CURRENT_HEADER_LINE_HEIGHT. */
593 int current_mode_line_height, current_header_line_height;
595 /* The maximum distance to look ahead for text properties. Values
596 that are too small let us call compute_char_face and similar
597 functions too often which is expensive. Values that are too large
598 let us call compute_char_face and alike too often because we
599 might not be interested in text properties that far away. */
601 #define TEXT_PROP_DISTANCE_LIMIT 100
603 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
604 iterator state and later restore it. This is needed because the
605 bidi iterator on bidi.c keeps a stacked cache of its states, which
606 is really a singleton. When we use scratch iterator objects to
607 move around the buffer, we can cause the bidi cache to be pushed or
608 popped, and therefore we need to restore the cache state when we
609 return to the original iterator. */
610 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
611 do { \
612 if (CACHE) \
613 bidi_unshelve_cache (CACHE, 1); \
614 ITCOPY = ITORIG; \
615 CACHE = bidi_shelve_cache (); \
616 } while (0)
618 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
619 do { \
620 if (pITORIG != pITCOPY) \
621 *(pITORIG) = *(pITCOPY); \
622 bidi_unshelve_cache (CACHE, 0); \
623 CACHE = NULL; \
624 } while (0)
626 #ifdef GLYPH_DEBUG
628 /* Non-zero means print traces of redisplay if compiled with
629 GLYPH_DEBUG defined. */
631 int trace_redisplay_p;
633 #endif /* GLYPH_DEBUG */
635 #ifdef DEBUG_TRACE_MOVE
636 /* Non-zero means trace with TRACE_MOVE to stderr. */
637 int trace_move;
639 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
640 #else
641 #define TRACE_MOVE(x) (void) 0
642 #endif
644 static Lisp_Object Qauto_hscroll_mode;
646 /* Buffer being redisplayed -- for redisplay_window_error. */
648 static struct buffer *displayed_buffer;
650 /* Value returned from text property handlers (see below). */
652 enum prop_handled
654 HANDLED_NORMALLY,
655 HANDLED_RECOMPUTE_PROPS,
656 HANDLED_OVERLAY_STRING_CONSUMED,
657 HANDLED_RETURN
660 /* A description of text properties that redisplay is interested
661 in. */
663 struct props
665 /* The name of the property. */
666 Lisp_Object *name;
668 /* A unique index for the property. */
669 enum prop_idx idx;
671 /* A handler function called to set up iterator IT from the property
672 at IT's current position. Value is used to steer handle_stop. */
673 enum prop_handled (*handler) (struct it *it);
676 static enum prop_handled handle_face_prop (struct it *);
677 static enum prop_handled handle_invisible_prop (struct it *);
678 static enum prop_handled handle_display_prop (struct it *);
679 static enum prop_handled handle_composition_prop (struct it *);
680 static enum prop_handled handle_overlay_change (struct it *);
681 static enum prop_handled handle_fontified_prop (struct it *);
683 /* Properties handled by iterators. */
685 static struct props it_props[] =
687 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
688 /* Handle `face' before `display' because some sub-properties of
689 `display' need to know the face. */
690 {&Qface, FACE_PROP_IDX, handle_face_prop},
691 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
692 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
693 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
694 {NULL, 0, NULL}
697 /* Value is the position described by X. If X is a marker, value is
698 the marker_position of X. Otherwise, value is X. */
700 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
702 /* Enumeration returned by some move_it_.* functions internally. */
704 enum move_it_result
706 /* Not used. Undefined value. */
707 MOVE_UNDEFINED,
709 /* Move ended at the requested buffer position or ZV. */
710 MOVE_POS_MATCH_OR_ZV,
712 /* Move ended at the requested X pixel position. */
713 MOVE_X_REACHED,
715 /* Move within a line ended at the end of a line that must be
716 continued. */
717 MOVE_LINE_CONTINUED,
719 /* Move within a line ended at the end of a line that would
720 be displayed truncated. */
721 MOVE_LINE_TRUNCATED,
723 /* Move within a line ended at a line end. */
724 MOVE_NEWLINE_OR_CR
727 /* This counter is used to clear the face cache every once in a while
728 in redisplay_internal. It is incremented for each redisplay.
729 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
730 cleared. */
732 #define CLEAR_FACE_CACHE_COUNT 500
733 static int clear_face_cache_count;
735 /* Similarly for the image cache. */
737 #ifdef HAVE_WINDOW_SYSTEM
738 #define CLEAR_IMAGE_CACHE_COUNT 101
739 static int clear_image_cache_count;
741 /* Null glyph slice */
742 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
743 #endif
745 /* True while redisplay_internal is in progress. */
747 bool redisplaying_p;
749 static Lisp_Object Qinhibit_free_realized_faces;
750 static Lisp_Object Qmode_line_default_help_echo;
752 /* If a string, XTread_socket generates an event to display that string.
753 (The display is done in read_char.) */
755 Lisp_Object help_echo_string;
756 Lisp_Object help_echo_window;
757 Lisp_Object help_echo_object;
758 ptrdiff_t help_echo_pos;
760 /* Temporary variable for XTread_socket. */
762 Lisp_Object previous_help_echo_string;
764 /* Platform-independent portion of hourglass implementation. */
766 /* Non-zero means an hourglass cursor is currently shown. */
767 int hourglass_shown_p;
769 /* If non-null, an asynchronous timer that, when it expires, displays
770 an hourglass cursor on all frames. */
771 struct atimer *hourglass_atimer;
773 /* Name of the face used to display glyphless characters. */
774 Lisp_Object Qglyphless_char;
776 /* Symbol for the purpose of Vglyphless_char_display. */
777 static Lisp_Object Qglyphless_char_display;
779 /* Method symbols for Vglyphless_char_display. */
780 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
782 /* Default pixel width of `thin-space' display method. */
783 #define THIN_SPACE_WIDTH 1
785 /* Default number of seconds to wait before displaying an hourglass
786 cursor. */
787 #define DEFAULT_HOURGLASS_DELAY 1
790 /* Function prototypes. */
792 static void setup_for_ellipsis (struct it *, int);
793 static void set_iterator_to_next (struct it *, int);
794 static void mark_window_display_accurate_1 (struct window *, int);
795 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
796 static int display_prop_string_p (Lisp_Object, Lisp_Object);
797 static int cursor_row_p (struct glyph_row *);
798 static int redisplay_mode_lines (Lisp_Object, int);
799 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
801 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
803 static void handle_line_prefix (struct it *);
805 static void pint2str (char *, int, ptrdiff_t);
806 static void pint2hrstr (char *, int, ptrdiff_t);
807 static struct text_pos run_window_scroll_functions (Lisp_Object,
808 struct text_pos);
809 static void reconsider_clip_changes (struct window *, struct buffer *);
810 static int text_outside_line_unchanged_p (struct window *,
811 ptrdiff_t, ptrdiff_t);
812 static void store_mode_line_noprop_char (char);
813 static int store_mode_line_noprop (const char *, int, int);
814 static void handle_stop (struct it *);
815 static void handle_stop_backwards (struct it *, ptrdiff_t);
816 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
817 static void ensure_echo_area_buffers (void);
818 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
819 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
820 static int with_echo_area_buffer (struct window *, int,
821 int (*) (ptrdiff_t, Lisp_Object),
822 ptrdiff_t, Lisp_Object);
823 static void clear_garbaged_frames (void);
824 static int current_message_1 (ptrdiff_t, Lisp_Object);
825 static void pop_message (void);
826 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
827 static void set_message (Lisp_Object);
828 static int set_message_1 (ptrdiff_t, Lisp_Object);
829 static int display_echo_area (struct window *);
830 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
831 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
832 static Lisp_Object unwind_redisplay (Lisp_Object);
833 static int string_char_and_length (const unsigned char *, int *);
834 static struct text_pos display_prop_end (struct it *, Lisp_Object,
835 struct text_pos);
836 static int compute_window_start_on_continuation_line (struct window *);
837 static void insert_left_trunc_glyphs (struct it *);
838 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
839 Lisp_Object);
840 static void extend_face_to_end_of_line (struct it *);
841 static int append_space_for_newline (struct it *, int);
842 static int cursor_row_fully_visible_p (struct window *, int, int);
843 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
844 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
845 static int trailing_whitespace_p (ptrdiff_t);
846 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
847 static void push_it (struct it *, struct text_pos *);
848 static void iterate_out_of_display_property (struct it *);
849 static void pop_it (struct it *);
850 static void sync_frame_with_window_matrix_rows (struct window *);
851 static void redisplay_internal (void);
852 static int echo_area_display (int);
853 static void redisplay_windows (Lisp_Object);
854 static void redisplay_window (Lisp_Object, int);
855 static Lisp_Object redisplay_window_error (Lisp_Object);
856 static Lisp_Object redisplay_window_0 (Lisp_Object);
857 static Lisp_Object redisplay_window_1 (Lisp_Object);
858 static int set_cursor_from_row (struct window *, struct glyph_row *,
859 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
860 int, int);
861 static int update_menu_bar (struct frame *, int, int);
862 static int try_window_reusing_current_matrix (struct window *);
863 static int try_window_id (struct window *);
864 static int display_line (struct it *);
865 static int display_mode_lines (struct window *);
866 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
867 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
868 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
869 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
870 static void display_menu_bar (struct window *);
871 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
872 ptrdiff_t *);
873 static int display_string (const char *, Lisp_Object, Lisp_Object,
874 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
875 static void compute_line_metrics (struct it *);
876 static void run_redisplay_end_trigger_hook (struct it *);
877 static int get_overlay_strings (struct it *, ptrdiff_t);
878 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
879 static void next_overlay_string (struct it *);
880 static void reseat (struct it *, struct text_pos, int);
881 static void reseat_1 (struct it *, struct text_pos, int);
882 static void back_to_previous_visible_line_start (struct it *);
883 void reseat_at_previous_visible_line_start (struct it *);
884 static void reseat_at_next_visible_line_start (struct it *, int);
885 static int next_element_from_ellipsis (struct it *);
886 static int next_element_from_display_vector (struct it *);
887 static int next_element_from_string (struct it *);
888 static int next_element_from_c_string (struct it *);
889 static int next_element_from_buffer (struct it *);
890 static int next_element_from_composition (struct it *);
891 static int next_element_from_image (struct it *);
892 static int next_element_from_stretch (struct it *);
893 static void load_overlay_strings (struct it *, ptrdiff_t);
894 static int init_from_display_pos (struct it *, struct window *,
895 struct display_pos *);
896 static void reseat_to_string (struct it *, const char *,
897 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
898 static int get_next_display_element (struct it *);
899 static enum move_it_result
900 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
901 enum move_operation_enum);
902 void move_it_vertically_backward (struct it *, int);
903 static void get_visually_first_element (struct it *);
904 static void init_to_row_start (struct it *, struct window *,
905 struct glyph_row *);
906 static int init_to_row_end (struct it *, struct window *,
907 struct glyph_row *);
908 static void back_to_previous_line_start (struct it *);
909 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
910 static struct text_pos string_pos_nchars_ahead (struct text_pos,
911 Lisp_Object, ptrdiff_t);
912 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
913 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
914 static ptrdiff_t number_of_chars (const char *, int);
915 static void compute_stop_pos (struct it *);
916 static void compute_string_pos (struct text_pos *, struct text_pos,
917 Lisp_Object);
918 static int face_before_or_after_it_pos (struct it *, int);
919 static ptrdiff_t next_overlay_change (ptrdiff_t);
920 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
921 Lisp_Object, struct text_pos *, ptrdiff_t, int);
922 static int handle_single_display_spec (struct it *, Lisp_Object,
923 Lisp_Object, Lisp_Object,
924 struct text_pos *, ptrdiff_t, int, int);
925 static int underlying_face_id (struct it *);
926 static int in_ellipses_for_invisible_text_p (struct display_pos *,
927 struct window *);
929 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
930 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
932 #ifdef HAVE_WINDOW_SYSTEM
934 static void x_consider_frame_title (Lisp_Object);
935 static int tool_bar_lines_needed (struct frame *, int *);
936 static void update_tool_bar (struct frame *, int);
937 static void build_desired_tool_bar_string (struct frame *f);
938 static int redisplay_tool_bar (struct frame *);
939 static void display_tool_bar_line (struct it *, int);
940 static void notice_overwritten_cursor (struct window *,
941 enum glyph_row_area,
942 int, int, int, int);
943 static void append_stretch_glyph (struct it *, Lisp_Object,
944 int, int, int);
947 #endif /* HAVE_WINDOW_SYSTEM */
949 static void produce_special_glyphs (struct it *, enum display_element_type);
950 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
951 static int coords_in_mouse_face_p (struct window *, int, int);
955 /***********************************************************************
956 Window display dimensions
957 ***********************************************************************/
959 /* Return the bottom boundary y-position for text lines in window W.
960 This is the first y position at which a line cannot start.
961 It is relative to the top of the window.
963 This is the height of W minus the height of a mode line, if any. */
966 window_text_bottom_y (struct window *w)
968 int height = WINDOW_TOTAL_HEIGHT (w);
970 if (WINDOW_WANTS_MODELINE_P (w))
971 height -= CURRENT_MODE_LINE_HEIGHT (w);
972 return height;
975 /* Return the pixel width of display area AREA of window W. AREA < 0
976 means return the total width of W, not including fringes to
977 the left and right of the window. */
980 window_box_width (struct window *w, int area)
982 int cols = XFASTINT (w->total_cols);
983 int pixels = 0;
985 if (!w->pseudo_window_p)
987 cols -= WINDOW_SCROLL_BAR_COLS (w);
989 if (area == TEXT_AREA)
991 if (INTEGERP (w->left_margin_cols))
992 cols -= XFASTINT (w->left_margin_cols);
993 if (INTEGERP (w->right_margin_cols))
994 cols -= XFASTINT (w->right_margin_cols);
995 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
997 else if (area == LEFT_MARGIN_AREA)
999 cols = (INTEGERP (w->left_margin_cols)
1000 ? XFASTINT (w->left_margin_cols) : 0);
1001 pixels = 0;
1003 else if (area == RIGHT_MARGIN_AREA)
1005 cols = (INTEGERP (w->right_margin_cols)
1006 ? XFASTINT (w->right_margin_cols) : 0);
1007 pixels = 0;
1011 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1015 /* Return the pixel height of the display area of window W, not
1016 including mode lines of W, if any. */
1019 window_box_height (struct window *w)
1021 struct frame *f = XFRAME (w->frame);
1022 int height = WINDOW_TOTAL_HEIGHT (w);
1024 eassert (height >= 0);
1026 /* Note: the code below that determines the mode-line/header-line
1027 height is essentially the same as that contained in the macro
1028 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1029 the appropriate glyph row has its `mode_line_p' flag set,
1030 and if it doesn't, uses estimate_mode_line_height instead. */
1032 if (WINDOW_WANTS_MODELINE_P (w))
1034 struct glyph_row *ml_row
1035 = (w->current_matrix && w->current_matrix->rows
1036 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1037 : 0);
1038 if (ml_row && ml_row->mode_line_p)
1039 height -= ml_row->height;
1040 else
1041 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1044 if (WINDOW_WANTS_HEADER_LINE_P (w))
1046 struct glyph_row *hl_row
1047 = (w->current_matrix && w->current_matrix->rows
1048 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1049 : 0);
1050 if (hl_row && hl_row->mode_line_p)
1051 height -= hl_row->height;
1052 else
1053 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1056 /* With a very small font and a mode-line that's taller than
1057 default, we might end up with a negative height. */
1058 return max (0, height);
1061 /* Return the window-relative coordinate of the left edge of display
1062 area AREA of window W. AREA < 0 means return the left edge of the
1063 whole window, to the right of the left fringe of W. */
1066 window_box_left_offset (struct window *w, int area)
1068 int x;
1070 if (w->pseudo_window_p)
1071 return 0;
1073 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1075 if (area == TEXT_AREA)
1076 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1077 + window_box_width (w, LEFT_MARGIN_AREA));
1078 else if (area == RIGHT_MARGIN_AREA)
1079 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1080 + window_box_width (w, LEFT_MARGIN_AREA)
1081 + window_box_width (w, TEXT_AREA)
1082 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1084 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1085 else if (area == LEFT_MARGIN_AREA
1086 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1087 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1089 return x;
1093 /* Return the window-relative coordinate of the right edge of display
1094 area AREA of window W. AREA < 0 means return the right edge of the
1095 whole window, to the left of the right fringe of W. */
1098 window_box_right_offset (struct window *w, int area)
1100 return window_box_left_offset (w, area) + window_box_width (w, area);
1103 /* Return the frame-relative coordinate of the left edge of display
1104 area AREA of window W. AREA < 0 means return the left edge of the
1105 whole window, to the right of the left fringe of W. */
1108 window_box_left (struct window *w, int area)
1110 struct frame *f = XFRAME (w->frame);
1111 int x;
1113 if (w->pseudo_window_p)
1114 return FRAME_INTERNAL_BORDER_WIDTH (f);
1116 x = (WINDOW_LEFT_EDGE_X (w)
1117 + window_box_left_offset (w, area));
1119 return x;
1123 /* Return the frame-relative coordinate of the right edge of display
1124 area AREA of window W. AREA < 0 means return the right edge of the
1125 whole window, to the left of the right fringe of W. */
1128 window_box_right (struct window *w, int area)
1130 return window_box_left (w, area) + window_box_width (w, area);
1133 /* Get the bounding box of the display area AREA of window W, without
1134 mode lines, in frame-relative coordinates. AREA < 0 means the
1135 whole window, not including the left and right fringes of
1136 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1137 coordinates of the upper-left corner of the box. Return in
1138 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1140 void
1141 window_box (struct window *w, int area, int *box_x, int *box_y,
1142 int *box_width, int *box_height)
1144 if (box_width)
1145 *box_width = window_box_width (w, area);
1146 if (box_height)
1147 *box_height = window_box_height (w);
1148 if (box_x)
1149 *box_x = window_box_left (w, area);
1150 if (box_y)
1152 *box_y = WINDOW_TOP_EDGE_Y (w);
1153 if (WINDOW_WANTS_HEADER_LINE_P (w))
1154 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1159 /* Get the bounding box of the display area AREA of window W, without
1160 mode lines. AREA < 0 means the whole window, not including the
1161 left and right fringe of the window. Return in *TOP_LEFT_X
1162 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1163 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1164 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1165 box. */
1167 static void
1168 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1169 int *bottom_right_x, int *bottom_right_y)
1171 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1172 bottom_right_y);
1173 *bottom_right_x += *top_left_x;
1174 *bottom_right_y += *top_left_y;
1179 /***********************************************************************
1180 Utilities
1181 ***********************************************************************/
1183 /* Return the bottom y-position of the line the iterator IT is in.
1184 This can modify IT's settings. */
1187 line_bottom_y (struct it *it)
1189 int line_height = it->max_ascent + it->max_descent;
1190 int line_top_y = it->current_y;
1192 if (line_height == 0)
1194 if (last_height)
1195 line_height = last_height;
1196 else if (IT_CHARPOS (*it) < ZV)
1198 move_it_by_lines (it, 1);
1199 line_height = (it->max_ascent || it->max_descent
1200 ? it->max_ascent + it->max_descent
1201 : last_height);
1203 else
1205 struct glyph_row *row = it->glyph_row;
1207 /* Use the default character height. */
1208 it->glyph_row = NULL;
1209 it->what = IT_CHARACTER;
1210 it->c = ' ';
1211 it->len = 1;
1212 PRODUCE_GLYPHS (it);
1213 line_height = it->ascent + it->descent;
1214 it->glyph_row = row;
1218 return line_top_y + line_height;
1221 /* Subroutine of pos_visible_p below. Extracts a display string, if
1222 any, from the display spec given as its argument. */
1223 static Lisp_Object
1224 string_from_display_spec (Lisp_Object spec)
1226 if (CONSP (spec))
1228 while (CONSP (spec))
1230 if (STRINGP (XCAR (spec)))
1231 return XCAR (spec);
1232 spec = XCDR (spec);
1235 else if (VECTORP (spec))
1237 ptrdiff_t i;
1239 for (i = 0; i < ASIZE (spec); i++)
1241 if (STRINGP (AREF (spec, i)))
1242 return AREF (spec, i);
1244 return Qnil;
1247 return spec;
1251 /* Limit insanely large values of W->hscroll on frame F to the largest
1252 value that will still prevent first_visible_x and last_visible_x of
1253 'struct it' from overflowing an int. */
1254 static int
1255 window_hscroll_limited (struct window *w, struct frame *f)
1257 ptrdiff_t window_hscroll = w->hscroll;
1258 int window_text_width = window_box_width (w, TEXT_AREA);
1259 int colwidth = FRAME_COLUMN_WIDTH (f);
1261 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1262 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1264 return window_hscroll;
1267 /* Return 1 if position CHARPOS is visible in window W.
1268 CHARPOS < 0 means return info about WINDOW_END position.
1269 If visible, set *X and *Y to pixel coordinates of top left corner.
1270 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1271 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1274 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1275 int *rtop, int *rbot, int *rowh, int *vpos)
1277 struct it it;
1278 void *itdata = bidi_shelve_cache ();
1279 struct text_pos top;
1280 int visible_p = 0;
1281 struct buffer *old_buffer = NULL;
1283 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1284 return visible_p;
1286 if (XBUFFER (w->buffer) != current_buffer)
1288 old_buffer = current_buffer;
1289 set_buffer_internal_1 (XBUFFER (w->buffer));
1292 SET_TEXT_POS_FROM_MARKER (top, w->start);
1293 /* Scrolling a minibuffer window via scroll bar when the echo area
1294 shows long text sometimes resets the minibuffer contents behind
1295 our backs. */
1296 if (CHARPOS (top) > ZV)
1297 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1299 /* Compute exact mode line heights. */
1300 if (WINDOW_WANTS_MODELINE_P (w))
1301 current_mode_line_height
1302 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1303 BVAR (current_buffer, mode_line_format));
1305 if (WINDOW_WANTS_HEADER_LINE_P (w))
1306 current_header_line_height
1307 = display_mode_line (w, HEADER_LINE_FACE_ID,
1308 BVAR (current_buffer, header_line_format));
1310 start_display (&it, w, top);
1311 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1312 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1314 if (charpos >= 0
1315 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1316 && IT_CHARPOS (it) >= charpos)
1317 /* When scanning backwards under bidi iteration, move_it_to
1318 stops at or _before_ CHARPOS, because it stops at or to
1319 the _right_ of the character at CHARPOS. */
1320 || (it.bidi_p && it.bidi_it.scan_dir == -1
1321 && IT_CHARPOS (it) <= charpos)))
1323 /* We have reached CHARPOS, or passed it. How the call to
1324 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1325 or covered by a display property, move_it_to stops at the end
1326 of the invisible text, to the right of CHARPOS. (ii) If
1327 CHARPOS is in a display vector, move_it_to stops on its last
1328 glyph. */
1329 int top_x = it.current_x;
1330 int top_y = it.current_y;
1331 /* Calling line_bottom_y may change it.method, it.position, etc. */
1332 enum it_method it_method = it.method;
1333 int bottom_y = (last_height = 0, line_bottom_y (&it));
1334 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1336 if (top_y < window_top_y)
1337 visible_p = bottom_y > window_top_y;
1338 else if (top_y < it.last_visible_y)
1339 visible_p = 1;
1340 if (bottom_y >= it.last_visible_y
1341 && it.bidi_p && it.bidi_it.scan_dir == -1
1342 && IT_CHARPOS (it) < charpos)
1344 /* When the last line of the window is scanned backwards
1345 under bidi iteration, we could be duped into thinking
1346 that we have passed CHARPOS, when in fact move_it_to
1347 simply stopped short of CHARPOS because it reached
1348 last_visible_y. To see if that's what happened, we call
1349 move_it_to again with a slightly larger vertical limit,
1350 and see if it actually moved vertically; if it did, we
1351 didn't really reach CHARPOS, which is beyond window end. */
1352 struct it save_it = it;
1353 /* Why 10? because we don't know how many canonical lines
1354 will the height of the next line(s) be. So we guess. */
1355 int ten_more_lines =
1356 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1358 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1359 MOVE_TO_POS | MOVE_TO_Y);
1360 if (it.current_y > top_y)
1361 visible_p = 0;
1363 it = save_it;
1365 if (visible_p)
1367 if (it_method == GET_FROM_DISPLAY_VECTOR)
1369 /* We stopped on the last glyph of a display vector.
1370 Try and recompute. Hack alert! */
1371 if (charpos < 2 || top.charpos >= charpos)
1372 top_x = it.glyph_row->x;
1373 else
1375 struct it it2;
1376 start_display (&it2, w, top);
1377 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1378 get_next_display_element (&it2);
1379 PRODUCE_GLYPHS (&it2);
1380 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1381 || it2.current_x > it2.last_visible_x)
1382 top_x = it.glyph_row->x;
1383 else
1385 top_x = it2.current_x;
1386 top_y = it2.current_y;
1390 else if (IT_CHARPOS (it) != charpos)
1392 Lisp_Object cpos = make_number (charpos);
1393 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1394 Lisp_Object string = string_from_display_spec (spec);
1395 int newline_in_string = 0;
1397 if (STRINGP (string))
1399 const char *s = SSDATA (string);
1400 const char *e = s + SBYTES (string);
1401 while (s < e)
1403 if (*s++ == '\n')
1405 newline_in_string = 1;
1406 break;
1410 /* The tricky code below is needed because there's a
1411 discrepancy between move_it_to and how we set cursor
1412 when the display line ends in a newline from a
1413 display string. move_it_to will stop _after_ such
1414 display strings, whereas set_cursor_from_row
1415 conspires with cursor_row_p to place the cursor on
1416 the first glyph produced from the display string. */
1418 /* We have overshoot PT because it is covered by a
1419 display property whose value is a string. If the
1420 string includes embedded newlines, we are also in the
1421 wrong display line. Backtrack to the correct line,
1422 where the display string begins. */
1423 if (newline_in_string)
1425 Lisp_Object startpos, endpos;
1426 EMACS_INT start, end;
1427 struct it it3;
1428 int it3_moved;
1430 /* Find the first and the last buffer positions
1431 covered by the display string. */
1432 endpos =
1433 Fnext_single_char_property_change (cpos, Qdisplay,
1434 Qnil, Qnil);
1435 startpos =
1436 Fprevious_single_char_property_change (endpos, Qdisplay,
1437 Qnil, Qnil);
1438 start = XFASTINT (startpos);
1439 end = XFASTINT (endpos);
1440 /* Move to the last buffer position before the
1441 display property. */
1442 start_display (&it3, w, top);
1443 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1444 /* Move forward one more line if the position before
1445 the display string is a newline or if it is the
1446 rightmost character on a line that is
1447 continued or word-wrapped. */
1448 if (it3.method == GET_FROM_BUFFER
1449 && it3.c == '\n')
1450 move_it_by_lines (&it3, 1);
1451 else if (move_it_in_display_line_to (&it3, -1,
1452 it3.current_x
1453 + it3.pixel_width,
1454 MOVE_TO_X)
1455 == MOVE_LINE_CONTINUED)
1457 move_it_by_lines (&it3, 1);
1458 /* When we are under word-wrap, the #$@%!
1459 move_it_by_lines moves 2 lines, so we need to
1460 fix that up. */
1461 if (it3.line_wrap == WORD_WRAP)
1462 move_it_by_lines (&it3, -1);
1465 /* Record the vertical coordinate of the display
1466 line where we wound up. */
1467 top_y = it3.current_y;
1468 if (it3.bidi_p)
1470 /* When characters are reordered for display,
1471 the character displayed to the left of the
1472 display string could be _after_ the display
1473 property in the logical order. Use the
1474 smallest vertical position of these two. */
1475 start_display (&it3, w, top);
1476 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1477 if (it3.current_y < top_y)
1478 top_y = it3.current_y;
1480 /* Move from the top of the window to the beginning
1481 of the display line where the display string
1482 begins. */
1483 start_display (&it3, w, top);
1484 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1485 /* If it3_moved stays zero after the 'while' loop
1486 below, that means we already were at a newline
1487 before the loop (e.g., the display string begins
1488 with a newline), so we don't need to (and cannot)
1489 inspect the glyphs of it3.glyph_row, because
1490 PRODUCE_GLYPHS will not produce anything for a
1491 newline, and thus it3.glyph_row stays at its
1492 stale content it got at top of the window. */
1493 it3_moved = 0;
1494 /* Finally, advance the iterator until we hit the
1495 first display element whose character position is
1496 CHARPOS, or until the first newline from the
1497 display string, which signals the end of the
1498 display line. */
1499 while (get_next_display_element (&it3))
1501 PRODUCE_GLYPHS (&it3);
1502 if (IT_CHARPOS (it3) == charpos
1503 || ITERATOR_AT_END_OF_LINE_P (&it3))
1504 break;
1505 it3_moved = 1;
1506 set_iterator_to_next (&it3, 0);
1508 top_x = it3.current_x - it3.pixel_width;
1509 /* Normally, we would exit the above loop because we
1510 found the display element whose character
1511 position is CHARPOS. For the contingency that we
1512 didn't, and stopped at the first newline from the
1513 display string, move back over the glyphs
1514 produced from the string, until we find the
1515 rightmost glyph not from the string. */
1516 if (it3_moved
1517 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1519 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1520 + it3.glyph_row->used[TEXT_AREA];
1522 while (EQ ((g - 1)->object, string))
1524 --g;
1525 top_x -= g->pixel_width;
1527 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1528 + it3.glyph_row->used[TEXT_AREA]);
1533 *x = top_x;
1534 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1535 *rtop = max (0, window_top_y - top_y);
1536 *rbot = max (0, bottom_y - it.last_visible_y);
1537 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1538 - max (top_y, window_top_y)));
1539 *vpos = it.vpos;
1542 else
1544 /* We were asked to provide info about WINDOW_END. */
1545 struct it it2;
1546 void *it2data = NULL;
1548 SAVE_IT (it2, it, it2data);
1549 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1550 move_it_by_lines (&it, 1);
1551 if (charpos < IT_CHARPOS (it)
1552 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1554 visible_p = 1;
1555 RESTORE_IT (&it2, &it2, it2data);
1556 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1557 *x = it2.current_x;
1558 *y = it2.current_y + it2.max_ascent - it2.ascent;
1559 *rtop = max (0, -it2.current_y);
1560 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1561 - it.last_visible_y));
1562 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1563 it.last_visible_y)
1564 - max (it2.current_y,
1565 WINDOW_HEADER_LINE_HEIGHT (w))));
1566 *vpos = it2.vpos;
1568 else
1569 bidi_unshelve_cache (it2data, 1);
1571 bidi_unshelve_cache (itdata, 0);
1573 if (old_buffer)
1574 set_buffer_internal_1 (old_buffer);
1576 current_header_line_height = current_mode_line_height = -1;
1578 if (visible_p && w->hscroll > 0)
1579 *x -=
1580 window_hscroll_limited (w, WINDOW_XFRAME (w))
1581 * WINDOW_FRAME_COLUMN_WIDTH (w);
1583 #if 0
1584 /* Debugging code. */
1585 if (visible_p)
1586 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1587 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1588 else
1589 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1590 #endif
1592 return visible_p;
1596 /* Return the next character from STR. Return in *LEN the length of
1597 the character. This is like STRING_CHAR_AND_LENGTH but never
1598 returns an invalid character. If we find one, we return a `?', but
1599 with the length of the invalid character. */
1601 static int
1602 string_char_and_length (const unsigned char *str, int *len)
1604 int c;
1606 c = STRING_CHAR_AND_LENGTH (str, *len);
1607 if (!CHAR_VALID_P (c))
1608 /* We may not change the length here because other places in Emacs
1609 don't use this function, i.e. they silently accept invalid
1610 characters. */
1611 c = '?';
1613 return c;
1618 /* Given a position POS containing a valid character and byte position
1619 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1621 static struct text_pos
1622 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1624 eassert (STRINGP (string) && nchars >= 0);
1626 if (STRING_MULTIBYTE (string))
1628 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1629 int len;
1631 while (nchars--)
1633 string_char_and_length (p, &len);
1634 p += len;
1635 CHARPOS (pos) += 1;
1636 BYTEPOS (pos) += len;
1639 else
1640 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1642 return pos;
1646 /* Value is the text position, i.e. character and byte position,
1647 for character position CHARPOS in STRING. */
1649 static struct text_pos
1650 string_pos (ptrdiff_t charpos, Lisp_Object string)
1652 struct text_pos pos;
1653 eassert (STRINGP (string));
1654 eassert (charpos >= 0);
1655 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1656 return pos;
1660 /* Value is a text position, i.e. character and byte position, for
1661 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1662 means recognize multibyte characters. */
1664 static struct text_pos
1665 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1667 struct text_pos pos;
1669 eassert (s != NULL);
1670 eassert (charpos >= 0);
1672 if (multibyte_p)
1674 int len;
1676 SET_TEXT_POS (pos, 0, 0);
1677 while (charpos--)
1679 string_char_and_length ((const unsigned char *) s, &len);
1680 s += len;
1681 CHARPOS (pos) += 1;
1682 BYTEPOS (pos) += len;
1685 else
1686 SET_TEXT_POS (pos, charpos, charpos);
1688 return pos;
1692 /* Value is the number of characters in C string S. MULTIBYTE_P
1693 non-zero means recognize multibyte characters. */
1695 static ptrdiff_t
1696 number_of_chars (const char *s, int multibyte_p)
1698 ptrdiff_t nchars;
1700 if (multibyte_p)
1702 ptrdiff_t rest = strlen (s);
1703 int len;
1704 const unsigned char *p = (const unsigned char *) s;
1706 for (nchars = 0; rest > 0; ++nchars)
1708 string_char_and_length (p, &len);
1709 rest -= len, p += len;
1712 else
1713 nchars = strlen (s);
1715 return nchars;
1719 /* Compute byte position NEWPOS->bytepos corresponding to
1720 NEWPOS->charpos. POS is a known position in string STRING.
1721 NEWPOS->charpos must be >= POS.charpos. */
1723 static void
1724 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1726 eassert (STRINGP (string));
1727 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1729 if (STRING_MULTIBYTE (string))
1730 *newpos = string_pos_nchars_ahead (pos, string,
1731 CHARPOS (*newpos) - CHARPOS (pos));
1732 else
1733 BYTEPOS (*newpos) = CHARPOS (*newpos);
1736 /* EXPORT:
1737 Return an estimation of the pixel height of mode or header lines on
1738 frame F. FACE_ID specifies what line's height to estimate. */
1741 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1743 #ifdef HAVE_WINDOW_SYSTEM
1744 if (FRAME_WINDOW_P (f))
1746 int height = FONT_HEIGHT (FRAME_FONT (f));
1748 /* This function is called so early when Emacs starts that the face
1749 cache and mode line face are not yet initialized. */
1750 if (FRAME_FACE_CACHE (f))
1752 struct face *face = FACE_FROM_ID (f, face_id);
1753 if (face)
1755 if (face->font)
1756 height = FONT_HEIGHT (face->font);
1757 if (face->box_line_width > 0)
1758 height += 2 * face->box_line_width;
1762 return height;
1764 #endif
1766 return 1;
1769 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1770 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1771 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1772 not force the value into range. */
1774 void
1775 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1776 int *x, int *y, NativeRectangle *bounds, int noclip)
1779 #ifdef HAVE_WINDOW_SYSTEM
1780 if (FRAME_WINDOW_P (f))
1782 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1783 even for negative values. */
1784 if (pix_x < 0)
1785 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1786 if (pix_y < 0)
1787 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1789 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1790 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1792 if (bounds)
1793 STORE_NATIVE_RECT (*bounds,
1794 FRAME_COL_TO_PIXEL_X (f, pix_x),
1795 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1796 FRAME_COLUMN_WIDTH (f) - 1,
1797 FRAME_LINE_HEIGHT (f) - 1);
1799 if (!noclip)
1801 if (pix_x < 0)
1802 pix_x = 0;
1803 else if (pix_x > FRAME_TOTAL_COLS (f))
1804 pix_x = FRAME_TOTAL_COLS (f);
1806 if (pix_y < 0)
1807 pix_y = 0;
1808 else if (pix_y > FRAME_LINES (f))
1809 pix_y = FRAME_LINES (f);
1812 #endif
1814 *x = pix_x;
1815 *y = pix_y;
1819 /* Find the glyph under window-relative coordinates X/Y in window W.
1820 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1821 strings. Return in *HPOS and *VPOS the row and column number of
1822 the glyph found. Return in *AREA the glyph area containing X.
1823 Value is a pointer to the glyph found or null if X/Y is not on
1824 text, or we can't tell because W's current matrix is not up to
1825 date. */
1827 static
1828 struct glyph *
1829 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1830 int *dx, int *dy, int *area)
1832 struct glyph *glyph, *end;
1833 struct glyph_row *row = NULL;
1834 int x0, i;
1836 /* Find row containing Y. Give up if some row is not enabled. */
1837 for (i = 0; i < w->current_matrix->nrows; ++i)
1839 row = MATRIX_ROW (w->current_matrix, i);
1840 if (!row->enabled_p)
1841 return NULL;
1842 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1843 break;
1846 *vpos = i;
1847 *hpos = 0;
1849 /* Give up if Y is not in the window. */
1850 if (i == w->current_matrix->nrows)
1851 return NULL;
1853 /* Get the glyph area containing X. */
1854 if (w->pseudo_window_p)
1856 *area = TEXT_AREA;
1857 x0 = 0;
1859 else
1861 if (x < window_box_left_offset (w, TEXT_AREA))
1863 *area = LEFT_MARGIN_AREA;
1864 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1866 else if (x < window_box_right_offset (w, TEXT_AREA))
1868 *area = TEXT_AREA;
1869 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1871 else
1873 *area = RIGHT_MARGIN_AREA;
1874 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1878 /* Find glyph containing X. */
1879 glyph = row->glyphs[*area];
1880 end = glyph + row->used[*area];
1881 x -= x0;
1882 while (glyph < end && x >= glyph->pixel_width)
1884 x -= glyph->pixel_width;
1885 ++glyph;
1888 if (glyph == end)
1889 return NULL;
1891 if (dx)
1893 *dx = x;
1894 *dy = y - (row->y + row->ascent - glyph->ascent);
1897 *hpos = glyph - row->glyphs[*area];
1898 return glyph;
1901 /* Convert frame-relative x/y to coordinates relative to window W.
1902 Takes pseudo-windows into account. */
1904 static void
1905 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1907 if (w->pseudo_window_p)
1909 /* A pseudo-window is always full-width, and starts at the
1910 left edge of the frame, plus a frame border. */
1911 struct frame *f = XFRAME (w->frame);
1912 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1913 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1915 else
1917 *x -= WINDOW_LEFT_EDGE_X (w);
1918 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1922 #ifdef HAVE_WINDOW_SYSTEM
1924 /* EXPORT:
1925 Return in RECTS[] at most N clipping rectangles for glyph string S.
1926 Return the number of stored rectangles. */
1929 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1931 XRectangle r;
1933 if (n <= 0)
1934 return 0;
1936 if (s->row->full_width_p)
1938 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1939 r.x = WINDOW_LEFT_EDGE_X (s->w);
1940 r.width = WINDOW_TOTAL_WIDTH (s->w);
1942 /* Unless displaying a mode or menu bar line, which are always
1943 fully visible, clip to the visible part of the row. */
1944 if (s->w->pseudo_window_p)
1945 r.height = s->row->visible_height;
1946 else
1947 r.height = s->height;
1949 else
1951 /* This is a text line that may be partially visible. */
1952 r.x = window_box_left (s->w, s->area);
1953 r.width = window_box_width (s->w, s->area);
1954 r.height = s->row->visible_height;
1957 if (s->clip_head)
1958 if (r.x < s->clip_head->x)
1960 if (r.width >= s->clip_head->x - r.x)
1961 r.width -= s->clip_head->x - r.x;
1962 else
1963 r.width = 0;
1964 r.x = s->clip_head->x;
1966 if (s->clip_tail)
1967 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1969 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1970 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1971 else
1972 r.width = 0;
1975 /* If S draws overlapping rows, it's sufficient to use the top and
1976 bottom of the window for clipping because this glyph string
1977 intentionally draws over other lines. */
1978 if (s->for_overlaps)
1980 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1981 r.height = window_text_bottom_y (s->w) - r.y;
1983 /* Alas, the above simple strategy does not work for the
1984 environments with anti-aliased text: if the same text is
1985 drawn onto the same place multiple times, it gets thicker.
1986 If the overlap we are processing is for the erased cursor, we
1987 take the intersection with the rectangle of the cursor. */
1988 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1990 XRectangle rc, r_save = r;
1992 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1993 rc.y = s->w->phys_cursor.y;
1994 rc.width = s->w->phys_cursor_width;
1995 rc.height = s->w->phys_cursor_height;
1997 x_intersect_rectangles (&r_save, &rc, &r);
2000 else
2002 /* Don't use S->y for clipping because it doesn't take partially
2003 visible lines into account. For example, it can be negative for
2004 partially visible lines at the top of a window. */
2005 if (!s->row->full_width_p
2006 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2007 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2008 else
2009 r.y = max (0, s->row->y);
2012 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2014 /* If drawing the cursor, don't let glyph draw outside its
2015 advertised boundaries. Cleartype does this under some circumstances. */
2016 if (s->hl == DRAW_CURSOR)
2018 struct glyph *glyph = s->first_glyph;
2019 int height, max_y;
2021 if (s->x > r.x)
2023 r.width -= s->x - r.x;
2024 r.x = s->x;
2026 r.width = min (r.width, glyph->pixel_width);
2028 /* If r.y is below window bottom, ensure that we still see a cursor. */
2029 height = min (glyph->ascent + glyph->descent,
2030 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2031 max_y = window_text_bottom_y (s->w) - height;
2032 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2033 if (s->ybase - glyph->ascent > max_y)
2035 r.y = max_y;
2036 r.height = height;
2038 else
2040 /* Don't draw cursor glyph taller than our actual glyph. */
2041 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2042 if (height < r.height)
2044 max_y = r.y + r.height;
2045 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2046 r.height = min (max_y - r.y, height);
2051 if (s->row->clip)
2053 XRectangle r_save = r;
2055 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2056 r.width = 0;
2059 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2060 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2062 #ifdef CONVERT_FROM_XRECT
2063 CONVERT_FROM_XRECT (r, *rects);
2064 #else
2065 *rects = r;
2066 #endif
2067 return 1;
2069 else
2071 /* If we are processing overlapping and allowed to return
2072 multiple clipping rectangles, we exclude the row of the glyph
2073 string from the clipping rectangle. This is to avoid drawing
2074 the same text on the environment with anti-aliasing. */
2075 #ifdef CONVERT_FROM_XRECT
2076 XRectangle rs[2];
2077 #else
2078 XRectangle *rs = rects;
2079 #endif
2080 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2082 if (s->for_overlaps & OVERLAPS_PRED)
2084 rs[i] = r;
2085 if (r.y + r.height > row_y)
2087 if (r.y < row_y)
2088 rs[i].height = row_y - r.y;
2089 else
2090 rs[i].height = 0;
2092 i++;
2094 if (s->for_overlaps & OVERLAPS_SUCC)
2096 rs[i] = r;
2097 if (r.y < row_y + s->row->visible_height)
2099 if (r.y + r.height > row_y + s->row->visible_height)
2101 rs[i].y = row_y + s->row->visible_height;
2102 rs[i].height = r.y + r.height - rs[i].y;
2104 else
2105 rs[i].height = 0;
2107 i++;
2110 n = i;
2111 #ifdef CONVERT_FROM_XRECT
2112 for (i = 0; i < n; i++)
2113 CONVERT_FROM_XRECT (rs[i], rects[i]);
2114 #endif
2115 return n;
2119 /* EXPORT:
2120 Return in *NR the clipping rectangle for glyph string S. */
2122 void
2123 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2125 get_glyph_string_clip_rects (s, nr, 1);
2129 /* EXPORT:
2130 Return the position and height of the phys cursor in window W.
2131 Set w->phys_cursor_width to width of phys cursor.
2134 void
2135 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2136 struct glyph *glyph, int *xp, int *yp, int *heightp)
2138 struct frame *f = XFRAME (WINDOW_FRAME (w));
2139 int x, y, wd, h, h0, y0;
2141 /* Compute the width of the rectangle to draw. If on a stretch
2142 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2143 rectangle as wide as the glyph, but use a canonical character
2144 width instead. */
2145 wd = glyph->pixel_width - 1;
2146 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2147 wd++; /* Why? */
2148 #endif
2150 x = w->phys_cursor.x;
2151 if (x < 0)
2153 wd += x;
2154 x = 0;
2157 if (glyph->type == STRETCH_GLYPH
2158 && !x_stretch_cursor_p)
2159 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2160 w->phys_cursor_width = wd;
2162 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2164 /* If y is below window bottom, ensure that we still see a cursor. */
2165 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2167 h = max (h0, glyph->ascent + glyph->descent);
2168 h0 = min (h0, glyph->ascent + glyph->descent);
2170 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2171 if (y < y0)
2173 h = max (h - (y0 - y) + 1, h0);
2174 y = y0 - 1;
2176 else
2178 y0 = window_text_bottom_y (w) - h0;
2179 if (y > y0)
2181 h += y - y0;
2182 y = y0;
2186 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2187 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2188 *heightp = h;
2192 * Remember which glyph the mouse is over.
2195 void
2196 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2198 Lisp_Object window;
2199 struct window *w;
2200 struct glyph_row *r, *gr, *end_row;
2201 enum window_part part;
2202 enum glyph_row_area area;
2203 int x, y, width, height;
2205 /* Try to determine frame pixel position and size of the glyph under
2206 frame pixel coordinates X/Y on frame F. */
2208 if (!f->glyphs_initialized_p
2209 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2210 NILP (window)))
2212 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2213 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2214 goto virtual_glyph;
2217 w = XWINDOW (window);
2218 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2219 height = WINDOW_FRAME_LINE_HEIGHT (w);
2221 x = window_relative_x_coord (w, part, gx);
2222 y = gy - WINDOW_TOP_EDGE_Y (w);
2224 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2225 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2227 if (w->pseudo_window_p)
2229 area = TEXT_AREA;
2230 part = ON_MODE_LINE; /* Don't adjust margin. */
2231 goto text_glyph;
2234 switch (part)
2236 case ON_LEFT_MARGIN:
2237 area = LEFT_MARGIN_AREA;
2238 goto text_glyph;
2240 case ON_RIGHT_MARGIN:
2241 area = RIGHT_MARGIN_AREA;
2242 goto text_glyph;
2244 case ON_HEADER_LINE:
2245 case ON_MODE_LINE:
2246 gr = (part == ON_HEADER_LINE
2247 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2248 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2249 gy = gr->y;
2250 area = TEXT_AREA;
2251 goto text_glyph_row_found;
2253 case ON_TEXT:
2254 area = TEXT_AREA;
2256 text_glyph:
2257 gr = 0; gy = 0;
2258 for (; r <= end_row && r->enabled_p; ++r)
2259 if (r->y + r->height > y)
2261 gr = r; gy = r->y;
2262 break;
2265 text_glyph_row_found:
2266 if (gr && gy <= y)
2268 struct glyph *g = gr->glyphs[area];
2269 struct glyph *end = g + gr->used[area];
2271 height = gr->height;
2272 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2273 if (gx + g->pixel_width > x)
2274 break;
2276 if (g < end)
2278 if (g->type == IMAGE_GLYPH)
2280 /* Don't remember when mouse is over image, as
2281 image may have hot-spots. */
2282 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2283 return;
2285 width = g->pixel_width;
2287 else
2289 /* Use nominal char spacing at end of line. */
2290 x -= gx;
2291 gx += (x / width) * width;
2294 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2295 gx += window_box_left_offset (w, area);
2297 else
2299 /* Use nominal line height at end of window. */
2300 gx = (x / width) * width;
2301 y -= gy;
2302 gy += (y / height) * height;
2304 break;
2306 case ON_LEFT_FRINGE:
2307 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2308 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2309 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2310 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2311 goto row_glyph;
2313 case ON_RIGHT_FRINGE:
2314 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2315 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2316 : window_box_right_offset (w, TEXT_AREA));
2317 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2318 goto row_glyph;
2320 case ON_SCROLL_BAR:
2321 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2323 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2324 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2325 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2326 : 0)));
2327 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2329 row_glyph:
2330 gr = 0, gy = 0;
2331 for (; r <= end_row && r->enabled_p; ++r)
2332 if (r->y + r->height > y)
2334 gr = r; gy = r->y;
2335 break;
2338 if (gr && gy <= y)
2339 height = gr->height;
2340 else
2342 /* Use nominal line height at end of window. */
2343 y -= gy;
2344 gy += (y / height) * height;
2346 break;
2348 default:
2350 virtual_glyph:
2351 /* If there is no glyph under the mouse, then we divide the screen
2352 into a grid of the smallest glyph in the frame, and use that
2353 as our "glyph". */
2355 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2356 round down even for negative values. */
2357 if (gx < 0)
2358 gx -= width - 1;
2359 if (gy < 0)
2360 gy -= height - 1;
2362 gx = (gx / width) * width;
2363 gy = (gy / height) * height;
2365 goto store_rect;
2368 gx += WINDOW_LEFT_EDGE_X (w);
2369 gy += WINDOW_TOP_EDGE_Y (w);
2371 store_rect:
2372 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2374 /* Visible feedback for debugging. */
2375 #if 0
2376 #if HAVE_X_WINDOWS
2377 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2378 f->output_data.x->normal_gc,
2379 gx, gy, width, height);
2380 #endif
2381 #endif
2385 #endif /* HAVE_WINDOW_SYSTEM */
2388 /***********************************************************************
2389 Lisp form evaluation
2390 ***********************************************************************/
2392 /* Error handler for safe_eval and safe_call. */
2394 static Lisp_Object
2395 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2397 add_to_log ("Error during redisplay: %S signaled %S",
2398 Flist (nargs, args), arg);
2399 return Qnil;
2402 /* Call function FUNC with the rest of NARGS - 1 arguments
2403 following. Return the result, or nil if something went
2404 wrong. Prevent redisplay during the evaluation. */
2406 Lisp_Object
2407 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2409 Lisp_Object val;
2411 if (inhibit_eval_during_redisplay)
2412 val = Qnil;
2413 else
2415 va_list ap;
2416 ptrdiff_t i;
2417 ptrdiff_t count = SPECPDL_INDEX ();
2418 struct gcpro gcpro1;
2419 Lisp_Object *args = alloca (nargs * word_size);
2421 args[0] = func;
2422 va_start (ap, func);
2423 for (i = 1; i < nargs; i++)
2424 args[i] = va_arg (ap, Lisp_Object);
2425 va_end (ap);
2427 GCPRO1 (args[0]);
2428 gcpro1.nvars = nargs;
2429 specbind (Qinhibit_redisplay, Qt);
2430 /* Use Qt to ensure debugger does not run,
2431 so there is no possibility of wanting to redisplay. */
2432 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2433 safe_eval_handler);
2434 UNGCPRO;
2435 val = unbind_to (count, val);
2438 return val;
2442 /* Call function FN with one argument ARG.
2443 Return the result, or nil if something went wrong. */
2445 Lisp_Object
2446 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2448 return safe_call (2, fn, arg);
2451 static Lisp_Object Qeval;
2453 Lisp_Object
2454 safe_eval (Lisp_Object sexpr)
2456 return safe_call1 (Qeval, sexpr);
2459 /* Call function FN with two arguments ARG1 and ARG2.
2460 Return the result, or nil if something went wrong. */
2462 Lisp_Object
2463 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2465 return safe_call (3, fn, arg1, arg2);
2470 /***********************************************************************
2471 Debugging
2472 ***********************************************************************/
2474 #if 0
2476 /* Define CHECK_IT to perform sanity checks on iterators.
2477 This is for debugging. It is too slow to do unconditionally. */
2479 static void
2480 check_it (struct it *it)
2482 if (it->method == GET_FROM_STRING)
2484 eassert (STRINGP (it->string));
2485 eassert (IT_STRING_CHARPOS (*it) >= 0);
2487 else
2489 eassert (IT_STRING_CHARPOS (*it) < 0);
2490 if (it->method == GET_FROM_BUFFER)
2492 /* Check that character and byte positions agree. */
2493 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2497 if (it->dpvec)
2498 eassert (it->current.dpvec_index >= 0);
2499 else
2500 eassert (it->current.dpvec_index < 0);
2503 #define CHECK_IT(IT) check_it ((IT))
2505 #else /* not 0 */
2507 #define CHECK_IT(IT) (void) 0
2509 #endif /* not 0 */
2512 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2514 /* Check that the window end of window W is what we expect it
2515 to be---the last row in the current matrix displaying text. */
2517 static void
2518 check_window_end (struct window *w)
2520 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2522 struct glyph_row *row;
2523 eassert ((row = MATRIX_ROW (w->current_matrix,
2524 XFASTINT (w->window_end_vpos)),
2525 !row->enabled_p
2526 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2527 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2531 #define CHECK_WINDOW_END(W) check_window_end ((W))
2533 #else
2535 #define CHECK_WINDOW_END(W) (void) 0
2537 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2539 /* Return mark position if current buffer has the region of non-zero length,
2540 or -1 otherwise. */
2542 static ptrdiff_t
2543 markpos_of_region (void)
2545 if (!NILP (Vtransient_mark_mode)
2546 && !NILP (BVAR (current_buffer, mark_active))
2547 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2549 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2551 if (markpos != PT)
2552 return markpos;
2554 return -1;
2557 /***********************************************************************
2558 Iterator initialization
2559 ***********************************************************************/
2561 /* Initialize IT for displaying current_buffer in window W, starting
2562 at character position CHARPOS. CHARPOS < 0 means that no buffer
2563 position is specified which is useful when the iterator is assigned
2564 a position later. BYTEPOS is the byte position corresponding to
2565 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2567 If ROW is not null, calls to produce_glyphs with IT as parameter
2568 will produce glyphs in that row.
2570 BASE_FACE_ID is the id of a base face to use. It must be one of
2571 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2572 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2573 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2575 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2576 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2577 will be initialized to use the corresponding mode line glyph row of
2578 the desired matrix of W. */
2580 void
2581 init_iterator (struct it *it, struct window *w,
2582 ptrdiff_t charpos, ptrdiff_t bytepos,
2583 struct glyph_row *row, enum face_id base_face_id)
2585 ptrdiff_t markpos;
2586 enum face_id remapped_base_face_id = base_face_id;
2588 /* Some precondition checks. */
2589 eassert (w != NULL && it != NULL);
2590 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2591 && charpos <= ZV));
2593 /* If face attributes have been changed since the last redisplay,
2594 free realized faces now because they depend on face definitions
2595 that might have changed. Don't free faces while there might be
2596 desired matrices pending which reference these faces. */
2597 if (face_change_count && !inhibit_free_realized_faces)
2599 face_change_count = 0;
2600 free_all_realized_faces (Qnil);
2603 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2604 if (! NILP (Vface_remapping_alist))
2605 remapped_base_face_id
2606 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2608 /* Use one of the mode line rows of W's desired matrix if
2609 appropriate. */
2610 if (row == NULL)
2612 if (base_face_id == MODE_LINE_FACE_ID
2613 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2614 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2615 else if (base_face_id == HEADER_LINE_FACE_ID)
2616 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2619 /* Clear IT. */
2620 memset (it, 0, sizeof *it);
2621 it->current.overlay_string_index = -1;
2622 it->current.dpvec_index = -1;
2623 it->base_face_id = remapped_base_face_id;
2624 it->string = Qnil;
2625 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2626 it->paragraph_embedding = L2R;
2627 it->bidi_it.string.lstring = Qnil;
2628 it->bidi_it.string.s = NULL;
2629 it->bidi_it.string.bufpos = 0;
2631 /* The window in which we iterate over current_buffer: */
2632 XSETWINDOW (it->window, w);
2633 it->w = w;
2634 it->f = XFRAME (w->frame);
2636 it->cmp_it.id = -1;
2638 /* Extra space between lines (on window systems only). */
2639 if (base_face_id == DEFAULT_FACE_ID
2640 && FRAME_WINDOW_P (it->f))
2642 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2643 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2644 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2645 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2646 * FRAME_LINE_HEIGHT (it->f));
2647 else if (it->f->extra_line_spacing > 0)
2648 it->extra_line_spacing = it->f->extra_line_spacing;
2649 it->max_extra_line_spacing = 0;
2652 /* If realized faces have been removed, e.g. because of face
2653 attribute changes of named faces, recompute them. When running
2654 in batch mode, the face cache of the initial frame is null. If
2655 we happen to get called, make a dummy face cache. */
2656 if (FRAME_FACE_CACHE (it->f) == NULL)
2657 init_frame_faces (it->f);
2658 if (FRAME_FACE_CACHE (it->f)->used == 0)
2659 recompute_basic_faces (it->f);
2661 /* Current value of the `slice', `space-width', and 'height' properties. */
2662 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2663 it->space_width = Qnil;
2664 it->font_height = Qnil;
2665 it->override_ascent = -1;
2667 /* Are control characters displayed as `^C'? */
2668 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2670 /* -1 means everything between a CR and the following line end
2671 is invisible. >0 means lines indented more than this value are
2672 invisible. */
2673 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2674 ? (clip_to_bounds
2675 (-1, XINT (BVAR (current_buffer, selective_display)),
2676 PTRDIFF_MAX))
2677 : (!NILP (BVAR (current_buffer, selective_display))
2678 ? -1 : 0));
2679 it->selective_display_ellipsis_p
2680 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2682 /* Display table to use. */
2683 it->dp = window_display_table (w);
2685 /* Are multibyte characters enabled in current_buffer? */
2686 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2688 /* If visible region is of non-zero length, set IT->region_beg_charpos
2689 and IT->region_end_charpos to the start and end of a visible region
2690 in window IT->w. Set both to -1 to indicate no region. */
2691 markpos = markpos_of_region ();
2692 if (0 <= markpos
2693 /* Maybe highlight only in selected window. */
2694 && (/* Either show region everywhere. */
2695 highlight_nonselected_windows
2696 /* Or show region in the selected window. */
2697 || w == XWINDOW (selected_window)
2698 /* Or show the region if we are in the mini-buffer and W is
2699 the window the mini-buffer refers to. */
2700 || (MINI_WINDOW_P (XWINDOW (selected_window))
2701 && WINDOWP (minibuf_selected_window)
2702 && w == XWINDOW (minibuf_selected_window))))
2704 it->region_beg_charpos = min (PT, markpos);
2705 it->region_end_charpos = max (PT, markpos);
2707 else
2708 it->region_beg_charpos = it->region_end_charpos = -1;
2710 /* Get the position at which the redisplay_end_trigger hook should
2711 be run, if it is to be run at all. */
2712 if (MARKERP (w->redisplay_end_trigger)
2713 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2714 it->redisplay_end_trigger_charpos
2715 = marker_position (w->redisplay_end_trigger);
2716 else if (INTEGERP (w->redisplay_end_trigger))
2717 it->redisplay_end_trigger_charpos =
2718 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2720 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2722 /* Are lines in the display truncated? */
2723 if (base_face_id != DEFAULT_FACE_ID
2724 || it->w->hscroll
2725 || (! WINDOW_FULL_WIDTH_P (it->w)
2726 && ((!NILP (Vtruncate_partial_width_windows)
2727 && !INTEGERP (Vtruncate_partial_width_windows))
2728 || (INTEGERP (Vtruncate_partial_width_windows)
2729 && (WINDOW_TOTAL_COLS (it->w)
2730 < XINT (Vtruncate_partial_width_windows))))))
2731 it->line_wrap = TRUNCATE;
2732 else if (NILP (BVAR (current_buffer, truncate_lines)))
2733 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2734 ? WINDOW_WRAP : WORD_WRAP;
2735 else
2736 it->line_wrap = TRUNCATE;
2738 /* Get dimensions of truncation and continuation glyphs. These are
2739 displayed as fringe bitmaps under X, but we need them for such
2740 frames when the fringes are turned off. But leave the dimensions
2741 zero for tooltip frames, as these glyphs look ugly there and also
2742 sabotage calculations of tooltip dimensions in x-show-tip. */
2743 #ifdef HAVE_WINDOW_SYSTEM
2744 if (!(FRAME_WINDOW_P (it->f)
2745 && FRAMEP (tip_frame)
2746 && it->f == XFRAME (tip_frame)))
2747 #endif
2749 if (it->line_wrap == TRUNCATE)
2751 /* We will need the truncation glyph. */
2752 eassert (it->glyph_row == NULL);
2753 produce_special_glyphs (it, IT_TRUNCATION);
2754 it->truncation_pixel_width = it->pixel_width;
2756 else
2758 /* We will need the continuation glyph. */
2759 eassert (it->glyph_row == NULL);
2760 produce_special_glyphs (it, IT_CONTINUATION);
2761 it->continuation_pixel_width = it->pixel_width;
2765 /* Reset these values to zero because the produce_special_glyphs
2766 above has changed them. */
2767 it->pixel_width = it->ascent = it->descent = 0;
2768 it->phys_ascent = it->phys_descent = 0;
2770 /* Set this after getting the dimensions of truncation and
2771 continuation glyphs, so that we don't produce glyphs when calling
2772 produce_special_glyphs, above. */
2773 it->glyph_row = row;
2774 it->area = TEXT_AREA;
2776 /* Forget any previous info about this row being reversed. */
2777 if (it->glyph_row)
2778 it->glyph_row->reversed_p = 0;
2780 /* Get the dimensions of the display area. The display area
2781 consists of the visible window area plus a horizontally scrolled
2782 part to the left of the window. All x-values are relative to the
2783 start of this total display area. */
2784 if (base_face_id != DEFAULT_FACE_ID)
2786 /* Mode lines, menu bar in terminal frames. */
2787 it->first_visible_x = 0;
2788 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2790 else
2792 it->first_visible_x =
2793 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2794 it->last_visible_x = (it->first_visible_x
2795 + window_box_width (w, TEXT_AREA));
2797 /* If we truncate lines, leave room for the truncation glyph(s) at
2798 the right margin. Otherwise, leave room for the continuation
2799 glyph(s). Done only if the window has no fringes. Since we
2800 don't know at this point whether there will be any R2L lines in
2801 the window, we reserve space for truncation/continuation glyphs
2802 even if only one of the fringes is absent. */
2803 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2804 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2806 if (it->line_wrap == TRUNCATE)
2807 it->last_visible_x -= it->truncation_pixel_width;
2808 else
2809 it->last_visible_x -= it->continuation_pixel_width;
2812 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2813 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2816 /* Leave room for a border glyph. */
2817 if (!FRAME_WINDOW_P (it->f)
2818 && !WINDOW_RIGHTMOST_P (it->w))
2819 it->last_visible_x -= 1;
2821 it->last_visible_y = window_text_bottom_y (w);
2823 /* For mode lines and alike, arrange for the first glyph having a
2824 left box line if the face specifies a box. */
2825 if (base_face_id != DEFAULT_FACE_ID)
2827 struct face *face;
2829 it->face_id = remapped_base_face_id;
2831 /* If we have a boxed mode line, make the first character appear
2832 with a left box line. */
2833 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2834 if (face->box != FACE_NO_BOX)
2835 it->start_of_box_run_p = 1;
2838 /* If a buffer position was specified, set the iterator there,
2839 getting overlays and face properties from that position. */
2840 if (charpos >= BUF_BEG (current_buffer))
2842 it->end_charpos = ZV;
2843 IT_CHARPOS (*it) = charpos;
2845 /* We will rely on `reseat' to set this up properly, via
2846 handle_face_prop. */
2847 it->face_id = it->base_face_id;
2849 /* Compute byte position if not specified. */
2850 if (bytepos < charpos)
2851 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2852 else
2853 IT_BYTEPOS (*it) = bytepos;
2855 it->start = it->current;
2856 /* Do we need to reorder bidirectional text? Not if this is a
2857 unibyte buffer: by definition, none of the single-byte
2858 characters are strong R2L, so no reordering is needed. And
2859 bidi.c doesn't support unibyte buffers anyway. Also, don't
2860 reorder while we are loading loadup.el, since the tables of
2861 character properties needed for reordering are not yet
2862 available. */
2863 it->bidi_p =
2864 NILP (Vpurify_flag)
2865 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2866 && it->multibyte_p;
2868 /* If we are to reorder bidirectional text, init the bidi
2869 iterator. */
2870 if (it->bidi_p)
2872 /* Note the paragraph direction that this buffer wants to
2873 use. */
2874 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2875 Qleft_to_right))
2876 it->paragraph_embedding = L2R;
2877 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2878 Qright_to_left))
2879 it->paragraph_embedding = R2L;
2880 else
2881 it->paragraph_embedding = NEUTRAL_DIR;
2882 bidi_unshelve_cache (NULL, 0);
2883 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2884 &it->bidi_it);
2887 /* Compute faces etc. */
2888 reseat (it, it->current.pos, 1);
2891 CHECK_IT (it);
2895 /* Initialize IT for the display of window W with window start POS. */
2897 void
2898 start_display (struct it *it, struct window *w, struct text_pos pos)
2900 struct glyph_row *row;
2901 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2903 row = w->desired_matrix->rows + first_vpos;
2904 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2905 it->first_vpos = first_vpos;
2907 /* Don't reseat to previous visible line start if current start
2908 position is in a string or image. */
2909 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2911 int start_at_line_beg_p;
2912 int first_y = it->current_y;
2914 /* If window start is not at a line start, skip forward to POS to
2915 get the correct continuation lines width. */
2916 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2917 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2918 if (!start_at_line_beg_p)
2920 int new_x;
2922 reseat_at_previous_visible_line_start (it);
2923 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2925 new_x = it->current_x + it->pixel_width;
2927 /* If lines are continued, this line may end in the middle
2928 of a multi-glyph character (e.g. a control character
2929 displayed as \003, or in the middle of an overlay
2930 string). In this case move_it_to above will not have
2931 taken us to the start of the continuation line but to the
2932 end of the continued line. */
2933 if (it->current_x > 0
2934 && it->line_wrap != TRUNCATE /* Lines are continued. */
2935 && (/* And glyph doesn't fit on the line. */
2936 new_x > it->last_visible_x
2937 /* Or it fits exactly and we're on a window
2938 system frame. */
2939 || (new_x == it->last_visible_x
2940 && FRAME_WINDOW_P (it->f)
2941 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2942 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2943 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2945 if ((it->current.dpvec_index >= 0
2946 || it->current.overlay_string_index >= 0)
2947 /* If we are on a newline from a display vector or
2948 overlay string, then we are already at the end of
2949 a screen line; no need to go to the next line in
2950 that case, as this line is not really continued.
2951 (If we do go to the next line, C-e will not DTRT.) */
2952 && it->c != '\n')
2954 set_iterator_to_next (it, 1);
2955 move_it_in_display_line_to (it, -1, -1, 0);
2958 it->continuation_lines_width += it->current_x;
2960 /* If the character at POS is displayed via a display
2961 vector, move_it_to above stops at the final glyph of
2962 IT->dpvec. To make the caller redisplay that character
2963 again (a.k.a. start at POS), we need to reset the
2964 dpvec_index to the beginning of IT->dpvec. */
2965 else if (it->current.dpvec_index >= 0)
2966 it->current.dpvec_index = 0;
2968 /* We're starting a new display line, not affected by the
2969 height of the continued line, so clear the appropriate
2970 fields in the iterator structure. */
2971 it->max_ascent = it->max_descent = 0;
2972 it->max_phys_ascent = it->max_phys_descent = 0;
2974 it->current_y = first_y;
2975 it->vpos = 0;
2976 it->current_x = it->hpos = 0;
2982 /* Return 1 if POS is a position in ellipses displayed for invisible
2983 text. W is the window we display, for text property lookup. */
2985 static int
2986 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2988 Lisp_Object prop, window;
2989 int ellipses_p = 0;
2990 ptrdiff_t charpos = CHARPOS (pos->pos);
2992 /* If POS specifies a position in a display vector, this might
2993 be for an ellipsis displayed for invisible text. We won't
2994 get the iterator set up for delivering that ellipsis unless
2995 we make sure that it gets aware of the invisible text. */
2996 if (pos->dpvec_index >= 0
2997 && pos->overlay_string_index < 0
2998 && CHARPOS (pos->string_pos) < 0
2999 && charpos > BEGV
3000 && (XSETWINDOW (window, w),
3001 prop = Fget_char_property (make_number (charpos),
3002 Qinvisible, window),
3003 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3005 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3006 window);
3007 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3010 return ellipses_p;
3014 /* Initialize IT for stepping through current_buffer in window W,
3015 starting at position POS that includes overlay string and display
3016 vector/ control character translation position information. Value
3017 is zero if there are overlay strings with newlines at POS. */
3019 static int
3020 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3022 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3023 int i, overlay_strings_with_newlines = 0;
3025 /* If POS specifies a position in a display vector, this might
3026 be for an ellipsis displayed for invisible text. We won't
3027 get the iterator set up for delivering that ellipsis unless
3028 we make sure that it gets aware of the invisible text. */
3029 if (in_ellipses_for_invisible_text_p (pos, w))
3031 --charpos;
3032 bytepos = 0;
3035 /* Keep in mind: the call to reseat in init_iterator skips invisible
3036 text, so we might end up at a position different from POS. This
3037 is only a problem when POS is a row start after a newline and an
3038 overlay starts there with an after-string, and the overlay has an
3039 invisible property. Since we don't skip invisible text in
3040 display_line and elsewhere immediately after consuming the
3041 newline before the row start, such a POS will not be in a string,
3042 but the call to init_iterator below will move us to the
3043 after-string. */
3044 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3046 /* This only scans the current chunk -- it should scan all chunks.
3047 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3048 to 16 in 22.1 to make this a lesser problem. */
3049 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3051 const char *s = SSDATA (it->overlay_strings[i]);
3052 const char *e = s + SBYTES (it->overlay_strings[i]);
3054 while (s < e && *s != '\n')
3055 ++s;
3057 if (s < e)
3059 overlay_strings_with_newlines = 1;
3060 break;
3064 /* If position is within an overlay string, set up IT to the right
3065 overlay string. */
3066 if (pos->overlay_string_index >= 0)
3068 int relative_index;
3070 /* If the first overlay string happens to have a `display'
3071 property for an image, the iterator will be set up for that
3072 image, and we have to undo that setup first before we can
3073 correct the overlay string index. */
3074 if (it->method == GET_FROM_IMAGE)
3075 pop_it (it);
3077 /* We already have the first chunk of overlay strings in
3078 IT->overlay_strings. Load more until the one for
3079 pos->overlay_string_index is in IT->overlay_strings. */
3080 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3082 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3083 it->current.overlay_string_index = 0;
3084 while (n--)
3086 load_overlay_strings (it, 0);
3087 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3091 it->current.overlay_string_index = pos->overlay_string_index;
3092 relative_index = (it->current.overlay_string_index
3093 % OVERLAY_STRING_CHUNK_SIZE);
3094 it->string = it->overlay_strings[relative_index];
3095 eassert (STRINGP (it->string));
3096 it->current.string_pos = pos->string_pos;
3097 it->method = GET_FROM_STRING;
3098 it->end_charpos = SCHARS (it->string);
3099 /* Set up the bidi iterator for this overlay string. */
3100 if (it->bidi_p)
3102 it->bidi_it.string.lstring = it->string;
3103 it->bidi_it.string.s = NULL;
3104 it->bidi_it.string.schars = SCHARS (it->string);
3105 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3106 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3107 it->bidi_it.string.unibyte = !it->multibyte_p;
3108 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3109 FRAME_WINDOW_P (it->f), &it->bidi_it);
3111 /* Synchronize the state of the bidi iterator with
3112 pos->string_pos. For any string position other than
3113 zero, this will be done automagically when we resume
3114 iteration over the string and get_visually_first_element
3115 is called. But if string_pos is zero, and the string is
3116 to be reordered for display, we need to resync manually,
3117 since it could be that the iteration state recorded in
3118 pos ended at string_pos of 0 moving backwards in string. */
3119 if (CHARPOS (pos->string_pos) == 0)
3121 get_visually_first_element (it);
3122 if (IT_STRING_CHARPOS (*it) != 0)
3123 do {
3124 /* Paranoia. */
3125 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3126 bidi_move_to_visually_next (&it->bidi_it);
3127 } while (it->bidi_it.charpos != 0);
3129 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3130 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3134 if (CHARPOS (pos->string_pos) >= 0)
3136 /* Recorded position is not in an overlay string, but in another
3137 string. This can only be a string from a `display' property.
3138 IT should already be filled with that string. */
3139 it->current.string_pos = pos->string_pos;
3140 eassert (STRINGP (it->string));
3141 if (it->bidi_p)
3142 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3143 FRAME_WINDOW_P (it->f), &it->bidi_it);
3146 /* Restore position in display vector translations, control
3147 character translations or ellipses. */
3148 if (pos->dpvec_index >= 0)
3150 if (it->dpvec == NULL)
3151 get_next_display_element (it);
3152 eassert (it->dpvec && it->current.dpvec_index == 0);
3153 it->current.dpvec_index = pos->dpvec_index;
3156 CHECK_IT (it);
3157 return !overlay_strings_with_newlines;
3161 /* Initialize IT for stepping through current_buffer in window W
3162 starting at ROW->start. */
3164 static void
3165 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3167 init_from_display_pos (it, w, &row->start);
3168 it->start = row->start;
3169 it->continuation_lines_width = row->continuation_lines_width;
3170 CHECK_IT (it);
3174 /* Initialize IT for stepping through current_buffer in window W
3175 starting in the line following ROW, i.e. starting at ROW->end.
3176 Value is zero if there are overlay strings with newlines at ROW's
3177 end position. */
3179 static int
3180 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3182 int success = 0;
3184 if (init_from_display_pos (it, w, &row->end))
3186 if (row->continued_p)
3187 it->continuation_lines_width
3188 = row->continuation_lines_width + row->pixel_width;
3189 CHECK_IT (it);
3190 success = 1;
3193 return success;
3199 /***********************************************************************
3200 Text properties
3201 ***********************************************************************/
3203 /* Called when IT reaches IT->stop_charpos. Handle text property and
3204 overlay changes. Set IT->stop_charpos to the next position where
3205 to stop. */
3207 static void
3208 handle_stop (struct it *it)
3210 enum prop_handled handled;
3211 int handle_overlay_change_p;
3212 struct props *p;
3214 it->dpvec = NULL;
3215 it->current.dpvec_index = -1;
3216 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3217 it->ignore_overlay_strings_at_pos_p = 0;
3218 it->ellipsis_p = 0;
3220 /* Use face of preceding text for ellipsis (if invisible) */
3221 if (it->selective_display_ellipsis_p)
3222 it->saved_face_id = it->face_id;
3226 handled = HANDLED_NORMALLY;
3228 /* Call text property handlers. */
3229 for (p = it_props; p->handler; ++p)
3231 handled = p->handler (it);
3233 if (handled == HANDLED_RECOMPUTE_PROPS)
3234 break;
3235 else if (handled == HANDLED_RETURN)
3237 /* We still want to show before and after strings from
3238 overlays even if the actual buffer text is replaced. */
3239 if (!handle_overlay_change_p
3240 || it->sp > 1
3241 /* Don't call get_overlay_strings_1 if we already
3242 have overlay strings loaded, because doing so
3243 will load them again and push the iterator state
3244 onto the stack one more time, which is not
3245 expected by the rest of the code that processes
3246 overlay strings. */
3247 || (it->current.overlay_string_index < 0
3248 ? !get_overlay_strings_1 (it, 0, 0)
3249 : 0))
3251 if (it->ellipsis_p)
3252 setup_for_ellipsis (it, 0);
3253 /* When handling a display spec, we might load an
3254 empty string. In that case, discard it here. We
3255 used to discard it in handle_single_display_spec,
3256 but that causes get_overlay_strings_1, above, to
3257 ignore overlay strings that we must check. */
3258 if (STRINGP (it->string) && !SCHARS (it->string))
3259 pop_it (it);
3260 return;
3262 else if (STRINGP (it->string) && !SCHARS (it->string))
3263 pop_it (it);
3264 else
3266 it->ignore_overlay_strings_at_pos_p = 1;
3267 it->string_from_display_prop_p = 0;
3268 it->from_disp_prop_p = 0;
3269 handle_overlay_change_p = 0;
3271 handled = HANDLED_RECOMPUTE_PROPS;
3272 break;
3274 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3275 handle_overlay_change_p = 0;
3278 if (handled != HANDLED_RECOMPUTE_PROPS)
3280 /* Don't check for overlay strings below when set to deliver
3281 characters from a display vector. */
3282 if (it->method == GET_FROM_DISPLAY_VECTOR)
3283 handle_overlay_change_p = 0;
3285 /* Handle overlay changes.
3286 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3287 if it finds overlays. */
3288 if (handle_overlay_change_p)
3289 handled = handle_overlay_change (it);
3292 if (it->ellipsis_p)
3294 setup_for_ellipsis (it, 0);
3295 break;
3298 while (handled == HANDLED_RECOMPUTE_PROPS);
3300 /* Determine where to stop next. */
3301 if (handled == HANDLED_NORMALLY)
3302 compute_stop_pos (it);
3306 /* Compute IT->stop_charpos from text property and overlay change
3307 information for IT's current position. */
3309 static void
3310 compute_stop_pos (struct it *it)
3312 register INTERVAL iv, next_iv;
3313 Lisp_Object object, limit, position;
3314 ptrdiff_t charpos, bytepos;
3316 if (STRINGP (it->string))
3318 /* Strings are usually short, so don't limit the search for
3319 properties. */
3320 it->stop_charpos = it->end_charpos;
3321 object = it->string;
3322 limit = Qnil;
3323 charpos = IT_STRING_CHARPOS (*it);
3324 bytepos = IT_STRING_BYTEPOS (*it);
3326 else
3328 ptrdiff_t pos;
3330 /* If end_charpos is out of range for some reason, such as a
3331 misbehaving display function, rationalize it (Bug#5984). */
3332 if (it->end_charpos > ZV)
3333 it->end_charpos = ZV;
3334 it->stop_charpos = it->end_charpos;
3336 /* If next overlay change is in front of the current stop pos
3337 (which is IT->end_charpos), stop there. Note: value of
3338 next_overlay_change is point-max if no overlay change
3339 follows. */
3340 charpos = IT_CHARPOS (*it);
3341 bytepos = IT_BYTEPOS (*it);
3342 pos = next_overlay_change (charpos);
3343 if (pos < it->stop_charpos)
3344 it->stop_charpos = pos;
3346 /* If showing the region, we have to stop at the region
3347 start or end because the face might change there. */
3348 if (it->region_beg_charpos > 0)
3350 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3351 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3352 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3353 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3356 /* Set up variables for computing the stop position from text
3357 property changes. */
3358 XSETBUFFER (object, current_buffer);
3359 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3362 /* Get the interval containing IT's position. Value is a null
3363 interval if there isn't such an interval. */
3364 position = make_number (charpos);
3365 iv = validate_interval_range (object, &position, &position, 0);
3366 if (iv)
3368 Lisp_Object values_here[LAST_PROP_IDX];
3369 struct props *p;
3371 /* Get properties here. */
3372 for (p = it_props; p->handler; ++p)
3373 values_here[p->idx] = textget (iv->plist, *p->name);
3375 /* Look for an interval following iv that has different
3376 properties. */
3377 for (next_iv = next_interval (iv);
3378 (next_iv
3379 && (NILP (limit)
3380 || XFASTINT (limit) > next_iv->position));
3381 next_iv = next_interval (next_iv))
3383 for (p = it_props; p->handler; ++p)
3385 Lisp_Object new_value;
3387 new_value = textget (next_iv->plist, *p->name);
3388 if (!EQ (values_here[p->idx], new_value))
3389 break;
3392 if (p->handler)
3393 break;
3396 if (next_iv)
3398 if (INTEGERP (limit)
3399 && next_iv->position >= XFASTINT (limit))
3400 /* No text property change up to limit. */
3401 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3402 else
3403 /* Text properties change in next_iv. */
3404 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3408 if (it->cmp_it.id < 0)
3410 ptrdiff_t stoppos = it->end_charpos;
3412 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3413 stoppos = -1;
3414 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3415 stoppos, it->string);
3418 eassert (STRINGP (it->string)
3419 || (it->stop_charpos >= BEGV
3420 && it->stop_charpos >= IT_CHARPOS (*it)));
3424 /* Return the position of the next overlay change after POS in
3425 current_buffer. Value is point-max if no overlay change
3426 follows. This is like `next-overlay-change' but doesn't use
3427 xmalloc. */
3429 static ptrdiff_t
3430 next_overlay_change (ptrdiff_t pos)
3432 ptrdiff_t i, noverlays;
3433 ptrdiff_t endpos;
3434 Lisp_Object *overlays;
3436 /* Get all overlays at the given position. */
3437 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3439 /* If any of these overlays ends before endpos,
3440 use its ending point instead. */
3441 for (i = 0; i < noverlays; ++i)
3443 Lisp_Object oend;
3444 ptrdiff_t oendpos;
3446 oend = OVERLAY_END (overlays[i]);
3447 oendpos = OVERLAY_POSITION (oend);
3448 endpos = min (endpos, oendpos);
3451 return endpos;
3454 /* How many characters forward to search for a display property or
3455 display string. Searching too far forward makes the bidi display
3456 sluggish, especially in small windows. */
3457 #define MAX_DISP_SCAN 250
3459 /* Return the character position of a display string at or after
3460 position specified by POSITION. If no display string exists at or
3461 after POSITION, return ZV. A display string is either an overlay
3462 with `display' property whose value is a string, or a `display'
3463 text property whose value is a string. STRING is data about the
3464 string to iterate; if STRING->lstring is nil, we are iterating a
3465 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3466 on a GUI frame. DISP_PROP is set to zero if we searched
3467 MAX_DISP_SCAN characters forward without finding any display
3468 strings, non-zero otherwise. It is set to 2 if the display string
3469 uses any kind of `(space ...)' spec that will produce a stretch of
3470 white space in the text area. */
3471 ptrdiff_t
3472 compute_display_string_pos (struct text_pos *position,
3473 struct bidi_string_data *string,
3474 int frame_window_p, int *disp_prop)
3476 /* OBJECT = nil means current buffer. */
3477 Lisp_Object object =
3478 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3479 Lisp_Object pos, spec, limpos;
3480 int string_p = (string && (STRINGP (string->lstring) || string->s));
3481 ptrdiff_t eob = string_p ? string->schars : ZV;
3482 ptrdiff_t begb = string_p ? 0 : BEGV;
3483 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3484 ptrdiff_t lim =
3485 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3486 struct text_pos tpos;
3487 int rv = 0;
3489 *disp_prop = 1;
3491 if (charpos >= eob
3492 /* We don't support display properties whose values are strings
3493 that have display string properties. */
3494 || string->from_disp_str
3495 /* C strings cannot have display properties. */
3496 || (string->s && !STRINGP (object)))
3498 *disp_prop = 0;
3499 return eob;
3502 /* If the character at CHARPOS is where the display string begins,
3503 return CHARPOS. */
3504 pos = make_number (charpos);
3505 if (STRINGP (object))
3506 bufpos = string->bufpos;
3507 else
3508 bufpos = charpos;
3509 tpos = *position;
3510 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3511 && (charpos <= begb
3512 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3513 object),
3514 spec))
3515 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3516 frame_window_p)))
3518 if (rv == 2)
3519 *disp_prop = 2;
3520 return charpos;
3523 /* Look forward for the first character with a `display' property
3524 that will replace the underlying text when displayed. */
3525 limpos = make_number (lim);
3526 do {
3527 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3528 CHARPOS (tpos) = XFASTINT (pos);
3529 if (CHARPOS (tpos) >= lim)
3531 *disp_prop = 0;
3532 break;
3534 if (STRINGP (object))
3535 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3536 else
3537 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3538 spec = Fget_char_property (pos, Qdisplay, object);
3539 if (!STRINGP (object))
3540 bufpos = CHARPOS (tpos);
3541 } while (NILP (spec)
3542 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3543 bufpos, frame_window_p)));
3544 if (rv == 2)
3545 *disp_prop = 2;
3547 return CHARPOS (tpos);
3550 /* Return the character position of the end of the display string that
3551 started at CHARPOS. If there's no display string at CHARPOS,
3552 return -1. A display string is either an overlay with `display'
3553 property whose value is a string or a `display' text property whose
3554 value is a string. */
3555 ptrdiff_t
3556 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3558 /* OBJECT = nil means current buffer. */
3559 Lisp_Object object =
3560 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3561 Lisp_Object pos = make_number (charpos);
3562 ptrdiff_t eob =
3563 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3565 if (charpos >= eob || (string->s && !STRINGP (object)))
3566 return eob;
3568 /* It could happen that the display property or overlay was removed
3569 since we found it in compute_display_string_pos above. One way
3570 this can happen is if JIT font-lock was called (through
3571 handle_fontified_prop), and jit-lock-functions remove text
3572 properties or overlays from the portion of buffer that includes
3573 CHARPOS. Muse mode is known to do that, for example. In this
3574 case, we return -1 to the caller, to signal that no display
3575 string is actually present at CHARPOS. See bidi_fetch_char for
3576 how this is handled.
3578 An alternative would be to never look for display properties past
3579 it->stop_charpos. But neither compute_display_string_pos nor
3580 bidi_fetch_char that calls it know or care where the next
3581 stop_charpos is. */
3582 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3583 return -1;
3585 /* Look forward for the first character where the `display' property
3586 changes. */
3587 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3589 return XFASTINT (pos);
3594 /***********************************************************************
3595 Fontification
3596 ***********************************************************************/
3598 /* Handle changes in the `fontified' property of the current buffer by
3599 calling hook functions from Qfontification_functions to fontify
3600 regions of text. */
3602 static enum prop_handled
3603 handle_fontified_prop (struct it *it)
3605 Lisp_Object prop, pos;
3606 enum prop_handled handled = HANDLED_NORMALLY;
3608 if (!NILP (Vmemory_full))
3609 return handled;
3611 /* Get the value of the `fontified' property at IT's current buffer
3612 position. (The `fontified' property doesn't have a special
3613 meaning in strings.) If the value is nil, call functions from
3614 Qfontification_functions. */
3615 if (!STRINGP (it->string)
3616 && it->s == NULL
3617 && !NILP (Vfontification_functions)
3618 && !NILP (Vrun_hooks)
3619 && (pos = make_number (IT_CHARPOS (*it)),
3620 prop = Fget_char_property (pos, Qfontified, Qnil),
3621 /* Ignore the special cased nil value always present at EOB since
3622 no amount of fontifying will be able to change it. */
3623 NILP (prop) && IT_CHARPOS (*it) < Z))
3625 ptrdiff_t count = SPECPDL_INDEX ();
3626 Lisp_Object val;
3627 struct buffer *obuf = current_buffer;
3628 int begv = BEGV, zv = ZV;
3629 int old_clip_changed = current_buffer->clip_changed;
3631 val = Vfontification_functions;
3632 specbind (Qfontification_functions, Qnil);
3634 eassert (it->end_charpos == ZV);
3636 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3637 safe_call1 (val, pos);
3638 else
3640 Lisp_Object fns, fn;
3641 struct gcpro gcpro1, gcpro2;
3643 fns = Qnil;
3644 GCPRO2 (val, fns);
3646 for (; CONSP (val); val = XCDR (val))
3648 fn = XCAR (val);
3650 if (EQ (fn, Qt))
3652 /* A value of t indicates this hook has a local
3653 binding; it means to run the global binding too.
3654 In a global value, t should not occur. If it
3655 does, we must ignore it to avoid an endless
3656 loop. */
3657 for (fns = Fdefault_value (Qfontification_functions);
3658 CONSP (fns);
3659 fns = XCDR (fns))
3661 fn = XCAR (fns);
3662 if (!EQ (fn, Qt))
3663 safe_call1 (fn, pos);
3666 else
3667 safe_call1 (fn, pos);
3670 UNGCPRO;
3673 unbind_to (count, Qnil);
3675 /* Fontification functions routinely call `save-restriction'.
3676 Normally, this tags clip_changed, which can confuse redisplay
3677 (see discussion in Bug#6671). Since we don't perform any
3678 special handling of fontification changes in the case where
3679 `save-restriction' isn't called, there's no point doing so in
3680 this case either. So, if the buffer's restrictions are
3681 actually left unchanged, reset clip_changed. */
3682 if (obuf == current_buffer)
3684 if (begv == BEGV && zv == ZV)
3685 current_buffer->clip_changed = old_clip_changed;
3687 /* There isn't much we can reasonably do to protect against
3688 misbehaving fontification, but here's a fig leaf. */
3689 else if (BUFFER_LIVE_P (obuf))
3690 set_buffer_internal_1 (obuf);
3692 /* The fontification code may have added/removed text.
3693 It could do even a lot worse, but let's at least protect against
3694 the most obvious case where only the text past `pos' gets changed',
3695 as is/was done in grep.el where some escapes sequences are turned
3696 into face properties (bug#7876). */
3697 it->end_charpos = ZV;
3699 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3700 something. This avoids an endless loop if they failed to
3701 fontify the text for which reason ever. */
3702 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3703 handled = HANDLED_RECOMPUTE_PROPS;
3706 return handled;
3711 /***********************************************************************
3712 Faces
3713 ***********************************************************************/
3715 /* Set up iterator IT from face properties at its current position.
3716 Called from handle_stop. */
3718 static enum prop_handled
3719 handle_face_prop (struct it *it)
3721 int new_face_id;
3722 ptrdiff_t next_stop;
3724 if (!STRINGP (it->string))
3726 new_face_id
3727 = face_at_buffer_position (it->w,
3728 IT_CHARPOS (*it),
3729 it->region_beg_charpos,
3730 it->region_end_charpos,
3731 &next_stop,
3732 (IT_CHARPOS (*it)
3733 + TEXT_PROP_DISTANCE_LIMIT),
3734 0, it->base_face_id);
3736 /* Is this a start of a run of characters with box face?
3737 Caveat: this can be called for a freshly initialized
3738 iterator; face_id is -1 in this case. We know that the new
3739 face will not change until limit, i.e. if the new face has a
3740 box, all characters up to limit will have one. But, as
3741 usual, we don't know whether limit is really the end. */
3742 if (new_face_id != it->face_id)
3744 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3745 /* If it->face_id is -1, old_face below will be NULL, see
3746 the definition of FACE_FROM_ID. This will happen if this
3747 is the initial call that gets the face. */
3748 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3750 /* If the value of face_id of the iterator is -1, we have to
3751 look in front of IT's position and see whether there is a
3752 face there that's different from new_face_id. */
3753 if (!old_face && IT_CHARPOS (*it) > BEG)
3755 int prev_face_id = face_before_it_pos (it);
3757 old_face = FACE_FROM_ID (it->f, prev_face_id);
3760 /* If the new face has a box, but the old face does not,
3761 this is the start of a run of characters with box face,
3762 i.e. this character has a shadow on the left side. */
3763 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3764 && (old_face == NULL || !old_face->box));
3765 it->face_box_p = new_face->box != FACE_NO_BOX;
3768 else
3770 int base_face_id;
3771 ptrdiff_t bufpos;
3772 int i;
3773 Lisp_Object from_overlay
3774 = (it->current.overlay_string_index >= 0
3775 ? it->string_overlays[it->current.overlay_string_index
3776 % OVERLAY_STRING_CHUNK_SIZE]
3777 : Qnil);
3779 /* See if we got to this string directly or indirectly from
3780 an overlay property. That includes the before-string or
3781 after-string of an overlay, strings in display properties
3782 provided by an overlay, their text properties, etc.
3784 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3785 if (! NILP (from_overlay))
3786 for (i = it->sp - 1; i >= 0; i--)
3788 if (it->stack[i].current.overlay_string_index >= 0)
3789 from_overlay
3790 = it->string_overlays[it->stack[i].current.overlay_string_index
3791 % OVERLAY_STRING_CHUNK_SIZE];
3792 else if (! NILP (it->stack[i].from_overlay))
3793 from_overlay = it->stack[i].from_overlay;
3795 if (!NILP (from_overlay))
3796 break;
3799 if (! NILP (from_overlay))
3801 bufpos = IT_CHARPOS (*it);
3802 /* For a string from an overlay, the base face depends
3803 only on text properties and ignores overlays. */
3804 base_face_id
3805 = face_for_overlay_string (it->w,
3806 IT_CHARPOS (*it),
3807 it->region_beg_charpos,
3808 it->region_end_charpos,
3809 &next_stop,
3810 (IT_CHARPOS (*it)
3811 + TEXT_PROP_DISTANCE_LIMIT),
3813 from_overlay);
3815 else
3817 bufpos = 0;
3819 /* For strings from a `display' property, use the face at
3820 IT's current buffer position as the base face to merge
3821 with, so that overlay strings appear in the same face as
3822 surrounding text, unless they specify their own
3823 faces. */
3824 base_face_id = it->string_from_prefix_prop_p
3825 ? DEFAULT_FACE_ID
3826 : underlying_face_id (it);
3829 new_face_id = face_at_string_position (it->w,
3830 it->string,
3831 IT_STRING_CHARPOS (*it),
3832 bufpos,
3833 it->region_beg_charpos,
3834 it->region_end_charpos,
3835 &next_stop,
3836 base_face_id, 0);
3838 /* Is this a start of a run of characters with box? Caveat:
3839 this can be called for a freshly allocated iterator; face_id
3840 is -1 is this case. We know that the new face will not
3841 change until the next check pos, i.e. if the new face has a
3842 box, all characters up to that position will have a
3843 box. But, as usual, we don't know whether that position
3844 is really the end. */
3845 if (new_face_id != it->face_id)
3847 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3848 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3850 /* If new face has a box but old face hasn't, this is the
3851 start of a run of characters with box, i.e. it has a
3852 shadow on the left side. */
3853 it->start_of_box_run_p
3854 = new_face->box && (old_face == NULL || !old_face->box);
3855 it->face_box_p = new_face->box != FACE_NO_BOX;
3859 it->face_id = new_face_id;
3860 return HANDLED_NORMALLY;
3864 /* Return the ID of the face ``underlying'' IT's current position,
3865 which is in a string. If the iterator is associated with a
3866 buffer, return the face at IT's current buffer position.
3867 Otherwise, use the iterator's base_face_id. */
3869 static int
3870 underlying_face_id (struct it *it)
3872 int face_id = it->base_face_id, i;
3874 eassert (STRINGP (it->string));
3876 for (i = it->sp - 1; i >= 0; --i)
3877 if (NILP (it->stack[i].string))
3878 face_id = it->stack[i].face_id;
3880 return face_id;
3884 /* Compute the face one character before or after the current position
3885 of IT, in the visual order. BEFORE_P non-zero means get the face
3886 in front (to the left in L2R paragraphs, to the right in R2L
3887 paragraphs) of IT's screen position. Value is the ID of the face. */
3889 static int
3890 face_before_or_after_it_pos (struct it *it, int before_p)
3892 int face_id, limit;
3893 ptrdiff_t next_check_charpos;
3894 struct it it_copy;
3895 void *it_copy_data = NULL;
3897 eassert (it->s == NULL);
3899 if (STRINGP (it->string))
3901 ptrdiff_t bufpos, charpos;
3902 int base_face_id;
3904 /* No face change past the end of the string (for the case
3905 we are padding with spaces). No face change before the
3906 string start. */
3907 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3908 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3909 return it->face_id;
3911 if (!it->bidi_p)
3913 /* Set charpos to the position before or after IT's current
3914 position, in the logical order, which in the non-bidi
3915 case is the same as the visual order. */
3916 if (before_p)
3917 charpos = IT_STRING_CHARPOS (*it) - 1;
3918 else if (it->what == IT_COMPOSITION)
3919 /* For composition, we must check the character after the
3920 composition. */
3921 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3922 else
3923 charpos = IT_STRING_CHARPOS (*it) + 1;
3925 else
3927 if (before_p)
3929 /* With bidi iteration, the character before the current
3930 in the visual order cannot be found by simple
3931 iteration, because "reverse" reordering is not
3932 supported. Instead, we need to use the move_it_*
3933 family of functions. */
3934 /* Ignore face changes before the first visible
3935 character on this display line. */
3936 if (it->current_x <= it->first_visible_x)
3937 return it->face_id;
3938 SAVE_IT (it_copy, *it, it_copy_data);
3939 /* Implementation note: Since move_it_in_display_line
3940 works in the iterator geometry, and thinks the first
3941 character is always the leftmost, even in R2L lines,
3942 we don't need to distinguish between the R2L and L2R
3943 cases here. */
3944 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3945 it_copy.current_x - 1, MOVE_TO_X);
3946 charpos = IT_STRING_CHARPOS (it_copy);
3947 RESTORE_IT (it, it, it_copy_data);
3949 else
3951 /* Set charpos to the string position of the character
3952 that comes after IT's current position in the visual
3953 order. */
3954 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3956 it_copy = *it;
3957 while (n--)
3958 bidi_move_to_visually_next (&it_copy.bidi_it);
3960 charpos = it_copy.bidi_it.charpos;
3963 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3965 if (it->current.overlay_string_index >= 0)
3966 bufpos = IT_CHARPOS (*it);
3967 else
3968 bufpos = 0;
3970 base_face_id = underlying_face_id (it);
3972 /* Get the face for ASCII, or unibyte. */
3973 face_id = face_at_string_position (it->w,
3974 it->string,
3975 charpos,
3976 bufpos,
3977 it->region_beg_charpos,
3978 it->region_end_charpos,
3979 &next_check_charpos,
3980 base_face_id, 0);
3982 /* Correct the face for charsets different from ASCII. Do it
3983 for the multibyte case only. The face returned above is
3984 suitable for unibyte text if IT->string is unibyte. */
3985 if (STRING_MULTIBYTE (it->string))
3987 struct text_pos pos1 = string_pos (charpos, it->string);
3988 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3989 int c, len;
3990 struct face *face = FACE_FROM_ID (it->f, face_id);
3992 c = string_char_and_length (p, &len);
3993 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3996 else
3998 struct text_pos pos;
4000 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4001 || (IT_CHARPOS (*it) <= BEGV && before_p))
4002 return it->face_id;
4004 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4005 pos = it->current.pos;
4007 if (!it->bidi_p)
4009 if (before_p)
4010 DEC_TEXT_POS (pos, it->multibyte_p);
4011 else
4013 if (it->what == IT_COMPOSITION)
4015 /* For composition, we must check the position after
4016 the composition. */
4017 pos.charpos += it->cmp_it.nchars;
4018 pos.bytepos += it->len;
4020 else
4021 INC_TEXT_POS (pos, it->multibyte_p);
4024 else
4026 if (before_p)
4028 /* With bidi iteration, the character before the current
4029 in the visual order cannot be found by simple
4030 iteration, because "reverse" reordering is not
4031 supported. Instead, we need to use the move_it_*
4032 family of functions. */
4033 /* Ignore face changes before the first visible
4034 character on this display line. */
4035 if (it->current_x <= it->first_visible_x)
4036 return it->face_id;
4037 SAVE_IT (it_copy, *it, it_copy_data);
4038 /* Implementation note: Since move_it_in_display_line
4039 works in the iterator geometry, and thinks the first
4040 character is always the leftmost, even in R2L lines,
4041 we don't need to distinguish between the R2L and L2R
4042 cases here. */
4043 move_it_in_display_line (&it_copy, ZV,
4044 it_copy.current_x - 1, MOVE_TO_X);
4045 pos = it_copy.current.pos;
4046 RESTORE_IT (it, it, it_copy_data);
4048 else
4050 /* Set charpos to the buffer position of the character
4051 that comes after IT's current position in the visual
4052 order. */
4053 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4055 it_copy = *it;
4056 while (n--)
4057 bidi_move_to_visually_next (&it_copy.bidi_it);
4059 SET_TEXT_POS (pos,
4060 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4063 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4065 /* Determine face for CHARSET_ASCII, or unibyte. */
4066 face_id = face_at_buffer_position (it->w,
4067 CHARPOS (pos),
4068 it->region_beg_charpos,
4069 it->region_end_charpos,
4070 &next_check_charpos,
4071 limit, 0, -1);
4073 /* Correct the face for charsets different from ASCII. Do it
4074 for the multibyte case only. The face returned above is
4075 suitable for unibyte text if current_buffer is unibyte. */
4076 if (it->multibyte_p)
4078 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4079 struct face *face = FACE_FROM_ID (it->f, face_id);
4080 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4084 return face_id;
4089 /***********************************************************************
4090 Invisible text
4091 ***********************************************************************/
4093 /* Set up iterator IT from invisible properties at its current
4094 position. Called from handle_stop. */
4096 static enum prop_handled
4097 handle_invisible_prop (struct it *it)
4099 enum prop_handled handled = HANDLED_NORMALLY;
4100 int invis_p;
4101 Lisp_Object prop;
4103 if (STRINGP (it->string))
4105 Lisp_Object end_charpos, limit, charpos;
4107 /* Get the value of the invisible text property at the
4108 current position. Value will be nil if there is no such
4109 property. */
4110 charpos = make_number (IT_STRING_CHARPOS (*it));
4111 prop = Fget_text_property (charpos, Qinvisible, it->string);
4112 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4114 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4116 /* Record whether we have to display an ellipsis for the
4117 invisible text. */
4118 int display_ellipsis_p = (invis_p == 2);
4119 ptrdiff_t len, endpos;
4121 handled = HANDLED_RECOMPUTE_PROPS;
4123 /* Get the position at which the next visible text can be
4124 found in IT->string, if any. */
4125 endpos = len = SCHARS (it->string);
4126 XSETINT (limit, len);
4129 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4130 it->string, limit);
4131 if (INTEGERP (end_charpos))
4133 endpos = XFASTINT (end_charpos);
4134 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4135 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4136 if (invis_p == 2)
4137 display_ellipsis_p = 1;
4140 while (invis_p && endpos < len);
4142 if (display_ellipsis_p)
4143 it->ellipsis_p = 1;
4145 if (endpos < len)
4147 /* Text at END_CHARPOS is visible. Move IT there. */
4148 struct text_pos old;
4149 ptrdiff_t oldpos;
4151 old = it->current.string_pos;
4152 oldpos = CHARPOS (old);
4153 if (it->bidi_p)
4155 if (it->bidi_it.first_elt
4156 && it->bidi_it.charpos < SCHARS (it->string))
4157 bidi_paragraph_init (it->paragraph_embedding,
4158 &it->bidi_it, 1);
4159 /* Bidi-iterate out of the invisible text. */
4162 bidi_move_to_visually_next (&it->bidi_it);
4164 while (oldpos <= it->bidi_it.charpos
4165 && it->bidi_it.charpos < endpos);
4167 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4168 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4169 if (IT_CHARPOS (*it) >= endpos)
4170 it->prev_stop = endpos;
4172 else
4174 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4175 compute_string_pos (&it->current.string_pos, old, it->string);
4178 else
4180 /* The rest of the string is invisible. If this is an
4181 overlay string, proceed with the next overlay string
4182 or whatever comes and return a character from there. */
4183 if (it->current.overlay_string_index >= 0
4184 && !display_ellipsis_p)
4186 next_overlay_string (it);
4187 /* Don't check for overlay strings when we just
4188 finished processing them. */
4189 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4191 else
4193 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4194 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4199 else
4201 ptrdiff_t newpos, next_stop, start_charpos, tem;
4202 Lisp_Object pos, overlay;
4204 /* First of all, is there invisible text at this position? */
4205 tem = start_charpos = IT_CHARPOS (*it);
4206 pos = make_number (tem);
4207 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4208 &overlay);
4209 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4211 /* If we are on invisible text, skip over it. */
4212 if (invis_p && start_charpos < it->end_charpos)
4214 /* Record whether we have to display an ellipsis for the
4215 invisible text. */
4216 int display_ellipsis_p = invis_p == 2;
4218 handled = HANDLED_RECOMPUTE_PROPS;
4220 /* Loop skipping over invisible text. The loop is left at
4221 ZV or with IT on the first char being visible again. */
4224 /* Try to skip some invisible text. Return value is the
4225 position reached which can be equal to where we start
4226 if there is nothing invisible there. This skips both
4227 over invisible text properties and overlays with
4228 invisible property. */
4229 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4231 /* If we skipped nothing at all we weren't at invisible
4232 text in the first place. If everything to the end of
4233 the buffer was skipped, end the loop. */
4234 if (newpos == tem || newpos >= ZV)
4235 invis_p = 0;
4236 else
4238 /* We skipped some characters but not necessarily
4239 all there are. Check if we ended up on visible
4240 text. Fget_char_property returns the property of
4241 the char before the given position, i.e. if we
4242 get invis_p = 0, this means that the char at
4243 newpos is visible. */
4244 pos = make_number (newpos);
4245 prop = Fget_char_property (pos, Qinvisible, it->window);
4246 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4249 /* If we ended up on invisible text, proceed to
4250 skip starting with next_stop. */
4251 if (invis_p)
4252 tem = next_stop;
4254 /* If there are adjacent invisible texts, don't lose the
4255 second one's ellipsis. */
4256 if (invis_p == 2)
4257 display_ellipsis_p = 1;
4259 while (invis_p);
4261 /* The position newpos is now either ZV or on visible text. */
4262 if (it->bidi_p)
4264 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4265 int on_newline =
4266 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4267 int after_newline =
4268 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4270 /* If the invisible text ends on a newline or on a
4271 character after a newline, we can avoid the costly,
4272 character by character, bidi iteration to NEWPOS, and
4273 instead simply reseat the iterator there. That's
4274 because all bidi reordering information is tossed at
4275 the newline. This is a big win for modes that hide
4276 complete lines, like Outline, Org, etc. */
4277 if (on_newline || after_newline)
4279 struct text_pos tpos;
4280 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4282 SET_TEXT_POS (tpos, newpos, bpos);
4283 reseat_1 (it, tpos, 0);
4284 /* If we reseat on a newline/ZV, we need to prep the
4285 bidi iterator for advancing to the next character
4286 after the newline/EOB, keeping the current paragraph
4287 direction (so that PRODUCE_GLYPHS does TRT wrt
4288 prepending/appending glyphs to a glyph row). */
4289 if (on_newline)
4291 it->bidi_it.first_elt = 0;
4292 it->bidi_it.paragraph_dir = pdir;
4293 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4294 it->bidi_it.nchars = 1;
4295 it->bidi_it.ch_len = 1;
4298 else /* Must use the slow method. */
4300 /* With bidi iteration, the region of invisible text
4301 could start and/or end in the middle of a
4302 non-base embedding level. Therefore, we need to
4303 skip invisible text using the bidi iterator,
4304 starting at IT's current position, until we find
4305 ourselves outside of the invisible text.
4306 Skipping invisible text _after_ bidi iteration
4307 avoids affecting the visual order of the
4308 displayed text when invisible properties are
4309 added or removed. */
4310 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4312 /* If we were `reseat'ed to a new paragraph,
4313 determine the paragraph base direction. We
4314 need to do it now because
4315 next_element_from_buffer may not have a
4316 chance to do it, if we are going to skip any
4317 text at the beginning, which resets the
4318 FIRST_ELT flag. */
4319 bidi_paragraph_init (it->paragraph_embedding,
4320 &it->bidi_it, 1);
4324 bidi_move_to_visually_next (&it->bidi_it);
4326 while (it->stop_charpos <= it->bidi_it.charpos
4327 && it->bidi_it.charpos < newpos);
4328 IT_CHARPOS (*it) = it->bidi_it.charpos;
4329 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4330 /* If we overstepped NEWPOS, record its position in
4331 the iterator, so that we skip invisible text if
4332 later the bidi iteration lands us in the
4333 invisible region again. */
4334 if (IT_CHARPOS (*it) >= newpos)
4335 it->prev_stop = newpos;
4338 else
4340 IT_CHARPOS (*it) = newpos;
4341 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4344 /* If there are before-strings at the start of invisible
4345 text, and the text is invisible because of a text
4346 property, arrange to show before-strings because 20.x did
4347 it that way. (If the text is invisible because of an
4348 overlay property instead of a text property, this is
4349 already handled in the overlay code.) */
4350 if (NILP (overlay)
4351 && get_overlay_strings (it, it->stop_charpos))
4353 handled = HANDLED_RECOMPUTE_PROPS;
4354 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4356 else if (display_ellipsis_p)
4358 /* Make sure that the glyphs of the ellipsis will get
4359 correct `charpos' values. If we would not update
4360 it->position here, the glyphs would belong to the
4361 last visible character _before_ the invisible
4362 text, which confuses `set_cursor_from_row'.
4364 We use the last invisible position instead of the
4365 first because this way the cursor is always drawn on
4366 the first "." of the ellipsis, whenever PT is inside
4367 the invisible text. Otherwise the cursor would be
4368 placed _after_ the ellipsis when the point is after the
4369 first invisible character. */
4370 if (!STRINGP (it->object))
4372 it->position.charpos = newpos - 1;
4373 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4375 it->ellipsis_p = 1;
4376 /* Let the ellipsis display before
4377 considering any properties of the following char.
4378 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4379 handled = HANDLED_RETURN;
4384 return handled;
4388 /* Make iterator IT return `...' next.
4389 Replaces LEN characters from buffer. */
4391 static void
4392 setup_for_ellipsis (struct it *it, int len)
4394 /* Use the display table definition for `...'. Invalid glyphs
4395 will be handled by the method returning elements from dpvec. */
4396 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4398 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4399 it->dpvec = v->contents;
4400 it->dpend = v->contents + v->header.size;
4402 else
4404 /* Default `...'. */
4405 it->dpvec = default_invis_vector;
4406 it->dpend = default_invis_vector + 3;
4409 it->dpvec_char_len = len;
4410 it->current.dpvec_index = 0;
4411 it->dpvec_face_id = -1;
4413 /* Remember the current face id in case glyphs specify faces.
4414 IT's face is restored in set_iterator_to_next.
4415 saved_face_id was set to preceding char's face in handle_stop. */
4416 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4417 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4419 it->method = GET_FROM_DISPLAY_VECTOR;
4420 it->ellipsis_p = 1;
4425 /***********************************************************************
4426 'display' property
4427 ***********************************************************************/
4429 /* Set up iterator IT from `display' property at its current position.
4430 Called from handle_stop.
4431 We return HANDLED_RETURN if some part of the display property
4432 overrides the display of the buffer text itself.
4433 Otherwise we return HANDLED_NORMALLY. */
4435 static enum prop_handled
4436 handle_display_prop (struct it *it)
4438 Lisp_Object propval, object, overlay;
4439 struct text_pos *position;
4440 ptrdiff_t bufpos;
4441 /* Nonzero if some property replaces the display of the text itself. */
4442 int display_replaced_p = 0;
4444 if (STRINGP (it->string))
4446 object = it->string;
4447 position = &it->current.string_pos;
4448 bufpos = CHARPOS (it->current.pos);
4450 else
4452 XSETWINDOW (object, it->w);
4453 position = &it->current.pos;
4454 bufpos = CHARPOS (*position);
4457 /* Reset those iterator values set from display property values. */
4458 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4459 it->space_width = Qnil;
4460 it->font_height = Qnil;
4461 it->voffset = 0;
4463 /* We don't support recursive `display' properties, i.e. string
4464 values that have a string `display' property, that have a string
4465 `display' property etc. */
4466 if (!it->string_from_display_prop_p)
4467 it->area = TEXT_AREA;
4469 propval = get_char_property_and_overlay (make_number (position->charpos),
4470 Qdisplay, object, &overlay);
4471 if (NILP (propval))
4472 return HANDLED_NORMALLY;
4473 /* Now OVERLAY is the overlay that gave us this property, or nil
4474 if it was a text property. */
4476 if (!STRINGP (it->string))
4477 object = it->w->buffer;
4479 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4480 position, bufpos,
4481 FRAME_WINDOW_P (it->f));
4483 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4486 /* Subroutine of handle_display_prop. Returns non-zero if the display
4487 specification in SPEC is a replacing specification, i.e. it would
4488 replace the text covered by `display' property with something else,
4489 such as an image or a display string. If SPEC includes any kind or
4490 `(space ...) specification, the value is 2; this is used by
4491 compute_display_string_pos, which see.
4493 See handle_single_display_spec for documentation of arguments.
4494 frame_window_p is non-zero if the window being redisplayed is on a
4495 GUI frame; this argument is used only if IT is NULL, see below.
4497 IT can be NULL, if this is called by the bidi reordering code
4498 through compute_display_string_pos, which see. In that case, this
4499 function only examines SPEC, but does not otherwise "handle" it, in
4500 the sense that it doesn't set up members of IT from the display
4501 spec. */
4502 static int
4503 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4504 Lisp_Object overlay, struct text_pos *position,
4505 ptrdiff_t bufpos, int frame_window_p)
4507 int replacing_p = 0;
4508 int rv;
4510 if (CONSP (spec)
4511 /* Simple specifications. */
4512 && !EQ (XCAR (spec), Qimage)
4513 && !EQ (XCAR (spec), Qspace)
4514 && !EQ (XCAR (spec), Qwhen)
4515 && !EQ (XCAR (spec), Qslice)
4516 && !EQ (XCAR (spec), Qspace_width)
4517 && !EQ (XCAR (spec), Qheight)
4518 && !EQ (XCAR (spec), Qraise)
4519 /* Marginal area specifications. */
4520 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4521 && !EQ (XCAR (spec), Qleft_fringe)
4522 && !EQ (XCAR (spec), Qright_fringe)
4523 && !NILP (XCAR (spec)))
4525 for (; CONSP (spec); spec = XCDR (spec))
4527 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4528 overlay, position, bufpos,
4529 replacing_p, frame_window_p)))
4531 replacing_p = rv;
4532 /* If some text in a string is replaced, `position' no
4533 longer points to the position of `object'. */
4534 if (!it || STRINGP (object))
4535 break;
4539 else if (VECTORP (spec))
4541 ptrdiff_t i;
4542 for (i = 0; i < ASIZE (spec); ++i)
4543 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4544 overlay, position, bufpos,
4545 replacing_p, frame_window_p)))
4547 replacing_p = rv;
4548 /* If some text in a string is replaced, `position' no
4549 longer points to the position of `object'. */
4550 if (!it || STRINGP (object))
4551 break;
4554 else
4556 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4557 position, bufpos, 0,
4558 frame_window_p)))
4559 replacing_p = rv;
4562 return replacing_p;
4565 /* Value is the position of the end of the `display' property starting
4566 at START_POS in OBJECT. */
4568 static struct text_pos
4569 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4571 Lisp_Object end;
4572 struct text_pos end_pos;
4574 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4575 Qdisplay, object, Qnil);
4576 CHARPOS (end_pos) = XFASTINT (end);
4577 if (STRINGP (object))
4578 compute_string_pos (&end_pos, start_pos, it->string);
4579 else
4580 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4582 return end_pos;
4586 /* Set up IT from a single `display' property specification SPEC. OBJECT
4587 is the object in which the `display' property was found. *POSITION
4588 is the position in OBJECT at which the `display' property was found.
4589 BUFPOS is the buffer position of OBJECT (different from POSITION if
4590 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4591 previously saw a display specification which already replaced text
4592 display with something else, for example an image; we ignore such
4593 properties after the first one has been processed.
4595 OVERLAY is the overlay this `display' property came from,
4596 or nil if it was a text property.
4598 If SPEC is a `space' or `image' specification, and in some other
4599 cases too, set *POSITION to the position where the `display'
4600 property ends.
4602 If IT is NULL, only examine the property specification in SPEC, but
4603 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4604 is intended to be displayed in a window on a GUI frame.
4606 Value is non-zero if something was found which replaces the display
4607 of buffer or string text. */
4609 static int
4610 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4611 Lisp_Object overlay, struct text_pos *position,
4612 ptrdiff_t bufpos, int display_replaced_p,
4613 int frame_window_p)
4615 Lisp_Object form;
4616 Lisp_Object location, value;
4617 struct text_pos start_pos = *position;
4618 int valid_p;
4620 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4621 If the result is non-nil, use VALUE instead of SPEC. */
4622 form = Qt;
4623 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4625 spec = XCDR (spec);
4626 if (!CONSP (spec))
4627 return 0;
4628 form = XCAR (spec);
4629 spec = XCDR (spec);
4632 if (!NILP (form) && !EQ (form, Qt))
4634 ptrdiff_t count = SPECPDL_INDEX ();
4635 struct gcpro gcpro1;
4637 /* Bind `object' to the object having the `display' property, a
4638 buffer or string. Bind `position' to the position in the
4639 object where the property was found, and `buffer-position'
4640 to the current position in the buffer. */
4642 if (NILP (object))
4643 XSETBUFFER (object, current_buffer);
4644 specbind (Qobject, object);
4645 specbind (Qposition, make_number (CHARPOS (*position)));
4646 specbind (Qbuffer_position, make_number (bufpos));
4647 GCPRO1 (form);
4648 form = safe_eval (form);
4649 UNGCPRO;
4650 unbind_to (count, Qnil);
4653 if (NILP (form))
4654 return 0;
4656 /* Handle `(height HEIGHT)' specifications. */
4657 if (CONSP (spec)
4658 && EQ (XCAR (spec), Qheight)
4659 && CONSP (XCDR (spec)))
4661 if (it)
4663 if (!FRAME_WINDOW_P (it->f))
4664 return 0;
4666 it->font_height = XCAR (XCDR (spec));
4667 if (!NILP (it->font_height))
4669 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4670 int new_height = -1;
4672 if (CONSP (it->font_height)
4673 && (EQ (XCAR (it->font_height), Qplus)
4674 || EQ (XCAR (it->font_height), Qminus))
4675 && CONSP (XCDR (it->font_height))
4676 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4678 /* `(+ N)' or `(- N)' where N is an integer. */
4679 int steps = XINT (XCAR (XCDR (it->font_height)));
4680 if (EQ (XCAR (it->font_height), Qplus))
4681 steps = - steps;
4682 it->face_id = smaller_face (it->f, it->face_id, steps);
4684 else if (FUNCTIONP (it->font_height))
4686 /* Call function with current height as argument.
4687 Value is the new height. */
4688 Lisp_Object height;
4689 height = safe_call1 (it->font_height,
4690 face->lface[LFACE_HEIGHT_INDEX]);
4691 if (NUMBERP (height))
4692 new_height = XFLOATINT (height);
4694 else if (NUMBERP (it->font_height))
4696 /* Value is a multiple of the canonical char height. */
4697 struct face *f;
4699 f = FACE_FROM_ID (it->f,
4700 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4701 new_height = (XFLOATINT (it->font_height)
4702 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4704 else
4706 /* Evaluate IT->font_height with `height' bound to the
4707 current specified height to get the new height. */
4708 ptrdiff_t count = SPECPDL_INDEX ();
4710 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4711 value = safe_eval (it->font_height);
4712 unbind_to (count, Qnil);
4714 if (NUMBERP (value))
4715 new_height = XFLOATINT (value);
4718 if (new_height > 0)
4719 it->face_id = face_with_height (it->f, it->face_id, new_height);
4723 return 0;
4726 /* Handle `(space-width WIDTH)'. */
4727 if (CONSP (spec)
4728 && EQ (XCAR (spec), Qspace_width)
4729 && CONSP (XCDR (spec)))
4731 if (it)
4733 if (!FRAME_WINDOW_P (it->f))
4734 return 0;
4736 value = XCAR (XCDR (spec));
4737 if (NUMBERP (value) && XFLOATINT (value) > 0)
4738 it->space_width = value;
4741 return 0;
4744 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4745 if (CONSP (spec)
4746 && EQ (XCAR (spec), Qslice))
4748 Lisp_Object tem;
4750 if (it)
4752 if (!FRAME_WINDOW_P (it->f))
4753 return 0;
4755 if (tem = XCDR (spec), CONSP (tem))
4757 it->slice.x = XCAR (tem);
4758 if (tem = XCDR (tem), CONSP (tem))
4760 it->slice.y = XCAR (tem);
4761 if (tem = XCDR (tem), CONSP (tem))
4763 it->slice.width = XCAR (tem);
4764 if (tem = XCDR (tem), CONSP (tem))
4765 it->slice.height = XCAR (tem);
4771 return 0;
4774 /* Handle `(raise FACTOR)'. */
4775 if (CONSP (spec)
4776 && EQ (XCAR (spec), Qraise)
4777 && CONSP (XCDR (spec)))
4779 if (it)
4781 if (!FRAME_WINDOW_P (it->f))
4782 return 0;
4784 #ifdef HAVE_WINDOW_SYSTEM
4785 value = XCAR (XCDR (spec));
4786 if (NUMBERP (value))
4788 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4789 it->voffset = - (XFLOATINT (value)
4790 * (FONT_HEIGHT (face->font)));
4792 #endif /* HAVE_WINDOW_SYSTEM */
4795 return 0;
4798 /* Don't handle the other kinds of display specifications
4799 inside a string that we got from a `display' property. */
4800 if (it && it->string_from_display_prop_p)
4801 return 0;
4803 /* Characters having this form of property are not displayed, so
4804 we have to find the end of the property. */
4805 if (it)
4807 start_pos = *position;
4808 *position = display_prop_end (it, object, start_pos);
4810 value = Qnil;
4812 /* Stop the scan at that end position--we assume that all
4813 text properties change there. */
4814 if (it)
4815 it->stop_charpos = position->charpos;
4817 /* Handle `(left-fringe BITMAP [FACE])'
4818 and `(right-fringe BITMAP [FACE])'. */
4819 if (CONSP (spec)
4820 && (EQ (XCAR (spec), Qleft_fringe)
4821 || EQ (XCAR (spec), Qright_fringe))
4822 && CONSP (XCDR (spec)))
4824 int fringe_bitmap;
4826 if (it)
4828 if (!FRAME_WINDOW_P (it->f))
4829 /* If we return here, POSITION has been advanced
4830 across the text with this property. */
4832 /* Synchronize the bidi iterator with POSITION. This is
4833 needed because we are not going to push the iterator
4834 on behalf of this display property, so there will be
4835 no pop_it call to do this synchronization for us. */
4836 if (it->bidi_p)
4838 it->position = *position;
4839 iterate_out_of_display_property (it);
4840 *position = it->position;
4842 return 1;
4845 else if (!frame_window_p)
4846 return 1;
4848 #ifdef HAVE_WINDOW_SYSTEM
4849 value = XCAR (XCDR (spec));
4850 if (!SYMBOLP (value)
4851 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4852 /* If we return here, POSITION has been advanced
4853 across the text with this property. */
4855 if (it && it->bidi_p)
4857 it->position = *position;
4858 iterate_out_of_display_property (it);
4859 *position = it->position;
4861 return 1;
4864 if (it)
4866 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4868 if (CONSP (XCDR (XCDR (spec))))
4870 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4871 int face_id2 = lookup_derived_face (it->f, face_name,
4872 FRINGE_FACE_ID, 0);
4873 if (face_id2 >= 0)
4874 face_id = face_id2;
4877 /* Save current settings of IT so that we can restore them
4878 when we are finished with the glyph property value. */
4879 push_it (it, position);
4881 it->area = TEXT_AREA;
4882 it->what = IT_IMAGE;
4883 it->image_id = -1; /* no image */
4884 it->position = start_pos;
4885 it->object = NILP (object) ? it->w->buffer : object;
4886 it->method = GET_FROM_IMAGE;
4887 it->from_overlay = Qnil;
4888 it->face_id = face_id;
4889 it->from_disp_prop_p = 1;
4891 /* Say that we haven't consumed the characters with
4892 `display' property yet. The call to pop_it in
4893 set_iterator_to_next will clean this up. */
4894 *position = start_pos;
4896 if (EQ (XCAR (spec), Qleft_fringe))
4898 it->left_user_fringe_bitmap = fringe_bitmap;
4899 it->left_user_fringe_face_id = face_id;
4901 else
4903 it->right_user_fringe_bitmap = fringe_bitmap;
4904 it->right_user_fringe_face_id = face_id;
4907 #endif /* HAVE_WINDOW_SYSTEM */
4908 return 1;
4911 /* Prepare to handle `((margin left-margin) ...)',
4912 `((margin right-margin) ...)' and `((margin nil) ...)'
4913 prefixes for display specifications. */
4914 location = Qunbound;
4915 if (CONSP (spec) && CONSP (XCAR (spec)))
4917 Lisp_Object tem;
4919 value = XCDR (spec);
4920 if (CONSP (value))
4921 value = XCAR (value);
4923 tem = XCAR (spec);
4924 if (EQ (XCAR (tem), Qmargin)
4925 && (tem = XCDR (tem),
4926 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4927 (NILP (tem)
4928 || EQ (tem, Qleft_margin)
4929 || EQ (tem, Qright_margin))))
4930 location = tem;
4933 if (EQ (location, Qunbound))
4935 location = Qnil;
4936 value = spec;
4939 /* After this point, VALUE is the property after any
4940 margin prefix has been stripped. It must be a string,
4941 an image specification, or `(space ...)'.
4943 LOCATION specifies where to display: `left-margin',
4944 `right-margin' or nil. */
4946 valid_p = (STRINGP (value)
4947 #ifdef HAVE_WINDOW_SYSTEM
4948 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4949 && valid_image_p (value))
4950 #endif /* not HAVE_WINDOW_SYSTEM */
4951 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4953 if (valid_p && !display_replaced_p)
4955 int retval = 1;
4957 if (!it)
4959 /* Callers need to know whether the display spec is any kind
4960 of `(space ...)' spec that is about to affect text-area
4961 display. */
4962 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4963 retval = 2;
4964 return retval;
4967 /* Save current settings of IT so that we can restore them
4968 when we are finished with the glyph property value. */
4969 push_it (it, position);
4970 it->from_overlay = overlay;
4971 it->from_disp_prop_p = 1;
4973 if (NILP (location))
4974 it->area = TEXT_AREA;
4975 else if (EQ (location, Qleft_margin))
4976 it->area = LEFT_MARGIN_AREA;
4977 else
4978 it->area = RIGHT_MARGIN_AREA;
4980 if (STRINGP (value))
4982 it->string = value;
4983 it->multibyte_p = STRING_MULTIBYTE (it->string);
4984 it->current.overlay_string_index = -1;
4985 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4986 it->end_charpos = it->string_nchars = SCHARS (it->string);
4987 it->method = GET_FROM_STRING;
4988 it->stop_charpos = 0;
4989 it->prev_stop = 0;
4990 it->base_level_stop = 0;
4991 it->string_from_display_prop_p = 1;
4992 /* Say that we haven't consumed the characters with
4993 `display' property yet. The call to pop_it in
4994 set_iterator_to_next will clean this up. */
4995 if (BUFFERP (object))
4996 *position = start_pos;
4998 /* Force paragraph direction to be that of the parent
4999 object. If the parent object's paragraph direction is
5000 not yet determined, default to L2R. */
5001 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5002 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5003 else
5004 it->paragraph_embedding = L2R;
5006 /* Set up the bidi iterator for this display string. */
5007 if (it->bidi_p)
5009 it->bidi_it.string.lstring = it->string;
5010 it->bidi_it.string.s = NULL;
5011 it->bidi_it.string.schars = it->end_charpos;
5012 it->bidi_it.string.bufpos = bufpos;
5013 it->bidi_it.string.from_disp_str = 1;
5014 it->bidi_it.string.unibyte = !it->multibyte_p;
5015 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5018 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5020 it->method = GET_FROM_STRETCH;
5021 it->object = value;
5022 *position = it->position = start_pos;
5023 retval = 1 + (it->area == TEXT_AREA);
5025 #ifdef HAVE_WINDOW_SYSTEM
5026 else
5028 it->what = IT_IMAGE;
5029 it->image_id = lookup_image (it->f, value);
5030 it->position = start_pos;
5031 it->object = NILP (object) ? it->w->buffer : object;
5032 it->method = GET_FROM_IMAGE;
5034 /* Say that we haven't consumed the characters with
5035 `display' property yet. The call to pop_it in
5036 set_iterator_to_next will clean this up. */
5037 *position = start_pos;
5039 #endif /* HAVE_WINDOW_SYSTEM */
5041 return retval;
5044 /* Invalid property or property not supported. Restore
5045 POSITION to what it was before. */
5046 *position = start_pos;
5047 return 0;
5050 /* Check if PROP is a display property value whose text should be
5051 treated as intangible. OVERLAY is the overlay from which PROP
5052 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5053 specify the buffer position covered by PROP. */
5056 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5057 ptrdiff_t charpos, ptrdiff_t bytepos)
5059 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5060 struct text_pos position;
5062 SET_TEXT_POS (position, charpos, bytepos);
5063 return handle_display_spec (NULL, prop, Qnil, overlay,
5064 &position, charpos, frame_window_p);
5068 /* Return 1 if PROP is a display sub-property value containing STRING.
5070 Implementation note: this and the following function are really
5071 special cases of handle_display_spec and
5072 handle_single_display_spec, and should ideally use the same code.
5073 Until they do, these two pairs must be consistent and must be
5074 modified in sync. */
5076 static int
5077 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5079 if (EQ (string, prop))
5080 return 1;
5082 /* Skip over `when FORM'. */
5083 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5085 prop = XCDR (prop);
5086 if (!CONSP (prop))
5087 return 0;
5088 /* Actually, the condition following `when' should be eval'ed,
5089 like handle_single_display_spec does, and we should return
5090 zero if it evaluates to nil. However, this function is
5091 called only when the buffer was already displayed and some
5092 glyph in the glyph matrix was found to come from a display
5093 string. Therefore, the condition was already evaluated, and
5094 the result was non-nil, otherwise the display string wouldn't
5095 have been displayed and we would have never been called for
5096 this property. Thus, we can skip the evaluation and assume
5097 its result is non-nil. */
5098 prop = XCDR (prop);
5101 if (CONSP (prop))
5102 /* Skip over `margin LOCATION'. */
5103 if (EQ (XCAR (prop), Qmargin))
5105 prop = XCDR (prop);
5106 if (!CONSP (prop))
5107 return 0;
5109 prop = XCDR (prop);
5110 if (!CONSP (prop))
5111 return 0;
5114 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5118 /* Return 1 if STRING appears in the `display' property PROP. */
5120 static int
5121 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5123 if (CONSP (prop)
5124 && !EQ (XCAR (prop), Qwhen)
5125 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5127 /* A list of sub-properties. */
5128 while (CONSP (prop))
5130 if (single_display_spec_string_p (XCAR (prop), string))
5131 return 1;
5132 prop = XCDR (prop);
5135 else if (VECTORP (prop))
5137 /* A vector of sub-properties. */
5138 ptrdiff_t i;
5139 for (i = 0; i < ASIZE (prop); ++i)
5140 if (single_display_spec_string_p (AREF (prop, i), string))
5141 return 1;
5143 else
5144 return single_display_spec_string_p (prop, string);
5146 return 0;
5149 /* Look for STRING in overlays and text properties in the current
5150 buffer, between character positions FROM and TO (excluding TO).
5151 BACK_P non-zero means look back (in this case, TO is supposed to be
5152 less than FROM).
5153 Value is the first character position where STRING was found, or
5154 zero if it wasn't found before hitting TO.
5156 This function may only use code that doesn't eval because it is
5157 called asynchronously from note_mouse_highlight. */
5159 static ptrdiff_t
5160 string_buffer_position_lim (Lisp_Object string,
5161 ptrdiff_t from, ptrdiff_t to, int back_p)
5163 Lisp_Object limit, prop, pos;
5164 int found = 0;
5166 pos = make_number (max (from, BEGV));
5168 if (!back_p) /* looking forward */
5170 limit = make_number (min (to, ZV));
5171 while (!found && !EQ (pos, limit))
5173 prop = Fget_char_property (pos, Qdisplay, Qnil);
5174 if (!NILP (prop) && display_prop_string_p (prop, string))
5175 found = 1;
5176 else
5177 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5178 limit);
5181 else /* looking back */
5183 limit = make_number (max (to, BEGV));
5184 while (!found && !EQ (pos, limit))
5186 prop = Fget_char_property (pos, Qdisplay, Qnil);
5187 if (!NILP (prop) && display_prop_string_p (prop, string))
5188 found = 1;
5189 else
5190 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5191 limit);
5195 return found ? XINT (pos) : 0;
5198 /* Determine which buffer position in current buffer STRING comes from.
5199 AROUND_CHARPOS is an approximate position where it could come from.
5200 Value is the buffer position or 0 if it couldn't be determined.
5202 This function is necessary because we don't record buffer positions
5203 in glyphs generated from strings (to keep struct glyph small).
5204 This function may only use code that doesn't eval because it is
5205 called asynchronously from note_mouse_highlight. */
5207 static ptrdiff_t
5208 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5210 const int MAX_DISTANCE = 1000;
5211 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5212 around_charpos + MAX_DISTANCE,
5215 if (!found)
5216 found = string_buffer_position_lim (string, around_charpos,
5217 around_charpos - MAX_DISTANCE, 1);
5218 return found;
5223 /***********************************************************************
5224 `composition' property
5225 ***********************************************************************/
5227 /* Set up iterator IT from `composition' property at its current
5228 position. Called from handle_stop. */
5230 static enum prop_handled
5231 handle_composition_prop (struct it *it)
5233 Lisp_Object prop, string;
5234 ptrdiff_t pos, pos_byte, start, end;
5236 if (STRINGP (it->string))
5238 unsigned char *s;
5240 pos = IT_STRING_CHARPOS (*it);
5241 pos_byte = IT_STRING_BYTEPOS (*it);
5242 string = it->string;
5243 s = SDATA (string) + pos_byte;
5244 it->c = STRING_CHAR (s);
5246 else
5248 pos = IT_CHARPOS (*it);
5249 pos_byte = IT_BYTEPOS (*it);
5250 string = Qnil;
5251 it->c = FETCH_CHAR (pos_byte);
5254 /* If there's a valid composition and point is not inside of the
5255 composition (in the case that the composition is from the current
5256 buffer), draw a glyph composed from the composition components. */
5257 if (find_composition (pos, -1, &start, &end, &prop, string)
5258 && COMPOSITION_VALID_P (start, end, prop)
5259 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5261 if (start < pos)
5262 /* As we can't handle this situation (perhaps font-lock added
5263 a new composition), we just return here hoping that next
5264 redisplay will detect this composition much earlier. */
5265 return HANDLED_NORMALLY;
5266 if (start != pos)
5268 if (STRINGP (it->string))
5269 pos_byte = string_char_to_byte (it->string, start);
5270 else
5271 pos_byte = CHAR_TO_BYTE (start);
5273 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5274 prop, string);
5276 if (it->cmp_it.id >= 0)
5278 it->cmp_it.ch = -1;
5279 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5280 it->cmp_it.nglyphs = -1;
5284 return HANDLED_NORMALLY;
5289 /***********************************************************************
5290 Overlay strings
5291 ***********************************************************************/
5293 /* The following structure is used to record overlay strings for
5294 later sorting in load_overlay_strings. */
5296 struct overlay_entry
5298 Lisp_Object overlay;
5299 Lisp_Object string;
5300 EMACS_INT priority;
5301 int after_string_p;
5305 /* Set up iterator IT from overlay strings at its current position.
5306 Called from handle_stop. */
5308 static enum prop_handled
5309 handle_overlay_change (struct it *it)
5311 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5312 return HANDLED_RECOMPUTE_PROPS;
5313 else
5314 return HANDLED_NORMALLY;
5318 /* Set up the next overlay string for delivery by IT, if there is an
5319 overlay string to deliver. Called by set_iterator_to_next when the
5320 end of the current overlay string is reached. If there are more
5321 overlay strings to display, IT->string and
5322 IT->current.overlay_string_index are set appropriately here.
5323 Otherwise IT->string is set to nil. */
5325 static void
5326 next_overlay_string (struct it *it)
5328 ++it->current.overlay_string_index;
5329 if (it->current.overlay_string_index == it->n_overlay_strings)
5331 /* No more overlay strings. Restore IT's settings to what
5332 they were before overlay strings were processed, and
5333 continue to deliver from current_buffer. */
5335 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5336 pop_it (it);
5337 eassert (it->sp > 0
5338 || (NILP (it->string)
5339 && it->method == GET_FROM_BUFFER
5340 && it->stop_charpos >= BEGV
5341 && it->stop_charpos <= it->end_charpos));
5342 it->current.overlay_string_index = -1;
5343 it->n_overlay_strings = 0;
5344 it->overlay_strings_charpos = -1;
5345 /* If there's an empty display string on the stack, pop the
5346 stack, to resync the bidi iterator with IT's position. Such
5347 empty strings are pushed onto the stack in
5348 get_overlay_strings_1. */
5349 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5350 pop_it (it);
5352 /* If we're at the end of the buffer, record that we have
5353 processed the overlay strings there already, so that
5354 next_element_from_buffer doesn't try it again. */
5355 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5356 it->overlay_strings_at_end_processed_p = 1;
5358 else
5360 /* There are more overlay strings to process. If
5361 IT->current.overlay_string_index has advanced to a position
5362 where we must load IT->overlay_strings with more strings, do
5363 it. We must load at the IT->overlay_strings_charpos where
5364 IT->n_overlay_strings was originally computed; when invisible
5365 text is present, this might not be IT_CHARPOS (Bug#7016). */
5366 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5368 if (it->current.overlay_string_index && i == 0)
5369 load_overlay_strings (it, it->overlay_strings_charpos);
5371 /* Initialize IT to deliver display elements from the overlay
5372 string. */
5373 it->string = it->overlay_strings[i];
5374 it->multibyte_p = STRING_MULTIBYTE (it->string);
5375 SET_TEXT_POS (it->current.string_pos, 0, 0);
5376 it->method = GET_FROM_STRING;
5377 it->stop_charpos = 0;
5378 it->end_charpos = SCHARS (it->string);
5379 if (it->cmp_it.stop_pos >= 0)
5380 it->cmp_it.stop_pos = 0;
5381 it->prev_stop = 0;
5382 it->base_level_stop = 0;
5384 /* Set up the bidi iterator for this overlay string. */
5385 if (it->bidi_p)
5387 it->bidi_it.string.lstring = it->string;
5388 it->bidi_it.string.s = NULL;
5389 it->bidi_it.string.schars = SCHARS (it->string);
5390 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5391 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5392 it->bidi_it.string.unibyte = !it->multibyte_p;
5393 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5397 CHECK_IT (it);
5401 /* Compare two overlay_entry structures E1 and E2. Used as a
5402 comparison function for qsort in load_overlay_strings. Overlay
5403 strings for the same position are sorted so that
5405 1. All after-strings come in front of before-strings, except
5406 when they come from the same overlay.
5408 2. Within after-strings, strings are sorted so that overlay strings
5409 from overlays with higher priorities come first.
5411 2. Within before-strings, strings are sorted so that overlay
5412 strings from overlays with higher priorities come last.
5414 Value is analogous to strcmp. */
5417 static int
5418 compare_overlay_entries (const void *e1, const void *e2)
5420 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5421 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5422 int result;
5424 if (entry1->after_string_p != entry2->after_string_p)
5426 /* Let after-strings appear in front of before-strings if
5427 they come from different overlays. */
5428 if (EQ (entry1->overlay, entry2->overlay))
5429 result = entry1->after_string_p ? 1 : -1;
5430 else
5431 result = entry1->after_string_p ? -1 : 1;
5433 else if (entry1->priority != entry2->priority)
5435 if (entry1->after_string_p)
5436 /* After-strings sorted in order of decreasing priority. */
5437 result = entry2->priority < entry1->priority ? -1 : 1;
5438 else
5439 /* Before-strings sorted in order of increasing priority. */
5440 result = entry1->priority < entry2->priority ? -1 : 1;
5442 else
5443 result = 0;
5445 return result;
5449 /* Load the vector IT->overlay_strings with overlay strings from IT's
5450 current buffer position, or from CHARPOS if that is > 0. Set
5451 IT->n_overlays to the total number of overlay strings found.
5453 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5454 a time. On entry into load_overlay_strings,
5455 IT->current.overlay_string_index gives the number of overlay
5456 strings that have already been loaded by previous calls to this
5457 function.
5459 IT->add_overlay_start contains an additional overlay start
5460 position to consider for taking overlay strings from, if non-zero.
5461 This position comes into play when the overlay has an `invisible'
5462 property, and both before and after-strings. When we've skipped to
5463 the end of the overlay, because of its `invisible' property, we
5464 nevertheless want its before-string to appear.
5465 IT->add_overlay_start will contain the overlay start position
5466 in this case.
5468 Overlay strings are sorted so that after-string strings come in
5469 front of before-string strings. Within before and after-strings,
5470 strings are sorted by overlay priority. See also function
5471 compare_overlay_entries. */
5473 static void
5474 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5476 Lisp_Object overlay, window, str, invisible;
5477 struct Lisp_Overlay *ov;
5478 ptrdiff_t start, end;
5479 ptrdiff_t size = 20;
5480 ptrdiff_t n = 0, i, j;
5481 int invis_p;
5482 struct overlay_entry *entries = alloca (size * sizeof *entries);
5483 USE_SAFE_ALLOCA;
5485 if (charpos <= 0)
5486 charpos = IT_CHARPOS (*it);
5488 /* Append the overlay string STRING of overlay OVERLAY to vector
5489 `entries' which has size `size' and currently contains `n'
5490 elements. AFTER_P non-zero means STRING is an after-string of
5491 OVERLAY. */
5492 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5493 do \
5495 Lisp_Object priority; \
5497 if (n == size) \
5499 struct overlay_entry *old = entries; \
5500 SAFE_NALLOCA (entries, 2, size); \
5501 memcpy (entries, old, size * sizeof *entries); \
5502 size *= 2; \
5505 entries[n].string = (STRING); \
5506 entries[n].overlay = (OVERLAY); \
5507 priority = Foverlay_get ((OVERLAY), Qpriority); \
5508 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5509 entries[n].after_string_p = (AFTER_P); \
5510 ++n; \
5512 while (0)
5514 /* Process overlay before the overlay center. */
5515 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5517 XSETMISC (overlay, ov);
5518 eassert (OVERLAYP (overlay));
5519 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5520 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5522 if (end < charpos)
5523 break;
5525 /* Skip this overlay if it doesn't start or end at IT's current
5526 position. */
5527 if (end != charpos && start != charpos)
5528 continue;
5530 /* Skip this overlay if it doesn't apply to IT->w. */
5531 window = Foverlay_get (overlay, Qwindow);
5532 if (WINDOWP (window) && XWINDOW (window) != it->w)
5533 continue;
5535 /* If the text ``under'' the overlay is invisible, both before-
5536 and after-strings from this overlay are visible; start and
5537 end position are indistinguishable. */
5538 invisible = Foverlay_get (overlay, Qinvisible);
5539 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5541 /* If overlay has a non-empty before-string, record it. */
5542 if ((start == charpos || (end == charpos && invis_p))
5543 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5544 && SCHARS (str))
5545 RECORD_OVERLAY_STRING (overlay, str, 0);
5547 /* If overlay has a non-empty after-string, record it. */
5548 if ((end == charpos || (start == charpos && invis_p))
5549 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5550 && SCHARS (str))
5551 RECORD_OVERLAY_STRING (overlay, str, 1);
5554 /* Process overlays after the overlay center. */
5555 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5557 XSETMISC (overlay, ov);
5558 eassert (OVERLAYP (overlay));
5559 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5560 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5562 if (start > charpos)
5563 break;
5565 /* Skip this overlay if it doesn't start or end at IT's current
5566 position. */
5567 if (end != charpos && start != charpos)
5568 continue;
5570 /* Skip this overlay if it doesn't apply to IT->w. */
5571 window = Foverlay_get (overlay, Qwindow);
5572 if (WINDOWP (window) && XWINDOW (window) != it->w)
5573 continue;
5575 /* If the text ``under'' the overlay is invisible, it has a zero
5576 dimension, and both before- and after-strings apply. */
5577 invisible = Foverlay_get (overlay, Qinvisible);
5578 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5580 /* If overlay has a non-empty before-string, record it. */
5581 if ((start == charpos || (end == charpos && invis_p))
5582 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5583 && SCHARS (str))
5584 RECORD_OVERLAY_STRING (overlay, str, 0);
5586 /* If overlay has a non-empty after-string, record it. */
5587 if ((end == charpos || (start == charpos && invis_p))
5588 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5589 && SCHARS (str))
5590 RECORD_OVERLAY_STRING (overlay, str, 1);
5593 #undef RECORD_OVERLAY_STRING
5595 /* Sort entries. */
5596 if (n > 1)
5597 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5599 /* Record number of overlay strings, and where we computed it. */
5600 it->n_overlay_strings = n;
5601 it->overlay_strings_charpos = charpos;
5603 /* IT->current.overlay_string_index is the number of overlay strings
5604 that have already been consumed by IT. Copy some of the
5605 remaining overlay strings to IT->overlay_strings. */
5606 i = 0;
5607 j = it->current.overlay_string_index;
5608 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5610 it->overlay_strings[i] = entries[j].string;
5611 it->string_overlays[i++] = entries[j++].overlay;
5614 CHECK_IT (it);
5615 SAFE_FREE ();
5619 /* Get the first chunk of overlay strings at IT's current buffer
5620 position, or at CHARPOS if that is > 0. Value is non-zero if at
5621 least one overlay string was found. */
5623 static int
5624 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5626 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5627 process. This fills IT->overlay_strings with strings, and sets
5628 IT->n_overlay_strings to the total number of strings to process.
5629 IT->pos.overlay_string_index has to be set temporarily to zero
5630 because load_overlay_strings needs this; it must be set to -1
5631 when no overlay strings are found because a zero value would
5632 indicate a position in the first overlay string. */
5633 it->current.overlay_string_index = 0;
5634 load_overlay_strings (it, charpos);
5636 /* If we found overlay strings, set up IT to deliver display
5637 elements from the first one. Otherwise set up IT to deliver
5638 from current_buffer. */
5639 if (it->n_overlay_strings)
5641 /* Make sure we know settings in current_buffer, so that we can
5642 restore meaningful values when we're done with the overlay
5643 strings. */
5644 if (compute_stop_p)
5645 compute_stop_pos (it);
5646 eassert (it->face_id >= 0);
5648 /* Save IT's settings. They are restored after all overlay
5649 strings have been processed. */
5650 eassert (!compute_stop_p || it->sp == 0);
5652 /* When called from handle_stop, there might be an empty display
5653 string loaded. In that case, don't bother saving it. But
5654 don't use this optimization with the bidi iterator, since we
5655 need the corresponding pop_it call to resync the bidi
5656 iterator's position with IT's position, after we are done
5657 with the overlay strings. (The corresponding call to pop_it
5658 in case of an empty display string is in
5659 next_overlay_string.) */
5660 if (!(!it->bidi_p
5661 && STRINGP (it->string) && !SCHARS (it->string)))
5662 push_it (it, NULL);
5664 /* Set up IT to deliver display elements from the first overlay
5665 string. */
5666 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5667 it->string = it->overlay_strings[0];
5668 it->from_overlay = Qnil;
5669 it->stop_charpos = 0;
5670 eassert (STRINGP (it->string));
5671 it->end_charpos = SCHARS (it->string);
5672 it->prev_stop = 0;
5673 it->base_level_stop = 0;
5674 it->multibyte_p = STRING_MULTIBYTE (it->string);
5675 it->method = GET_FROM_STRING;
5676 it->from_disp_prop_p = 0;
5678 /* Force paragraph direction to be that of the parent
5679 buffer. */
5680 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5681 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5682 else
5683 it->paragraph_embedding = L2R;
5685 /* Set up the bidi iterator for this overlay string. */
5686 if (it->bidi_p)
5688 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5690 it->bidi_it.string.lstring = it->string;
5691 it->bidi_it.string.s = NULL;
5692 it->bidi_it.string.schars = SCHARS (it->string);
5693 it->bidi_it.string.bufpos = pos;
5694 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5695 it->bidi_it.string.unibyte = !it->multibyte_p;
5696 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5698 return 1;
5701 it->current.overlay_string_index = -1;
5702 return 0;
5705 static int
5706 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5708 it->string = Qnil;
5709 it->method = GET_FROM_BUFFER;
5711 (void) get_overlay_strings_1 (it, charpos, 1);
5713 CHECK_IT (it);
5715 /* Value is non-zero if we found at least one overlay string. */
5716 return STRINGP (it->string);
5721 /***********************************************************************
5722 Saving and restoring state
5723 ***********************************************************************/
5725 /* Save current settings of IT on IT->stack. Called, for example,
5726 before setting up IT for an overlay string, to be able to restore
5727 IT's settings to what they were after the overlay string has been
5728 processed. If POSITION is non-NULL, it is the position to save on
5729 the stack instead of IT->position. */
5731 static void
5732 push_it (struct it *it, struct text_pos *position)
5734 struct iterator_stack_entry *p;
5736 eassert (it->sp < IT_STACK_SIZE);
5737 p = it->stack + it->sp;
5739 p->stop_charpos = it->stop_charpos;
5740 p->prev_stop = it->prev_stop;
5741 p->base_level_stop = it->base_level_stop;
5742 p->cmp_it = it->cmp_it;
5743 eassert (it->face_id >= 0);
5744 p->face_id = it->face_id;
5745 p->string = it->string;
5746 p->method = it->method;
5747 p->from_overlay = it->from_overlay;
5748 switch (p->method)
5750 case GET_FROM_IMAGE:
5751 p->u.image.object = it->object;
5752 p->u.image.image_id = it->image_id;
5753 p->u.image.slice = it->slice;
5754 break;
5755 case GET_FROM_STRETCH:
5756 p->u.stretch.object = it->object;
5757 break;
5759 p->position = position ? *position : it->position;
5760 p->current = it->current;
5761 p->end_charpos = it->end_charpos;
5762 p->string_nchars = it->string_nchars;
5763 p->area = it->area;
5764 p->multibyte_p = it->multibyte_p;
5765 p->avoid_cursor_p = it->avoid_cursor_p;
5766 p->space_width = it->space_width;
5767 p->font_height = it->font_height;
5768 p->voffset = it->voffset;
5769 p->string_from_display_prop_p = it->string_from_display_prop_p;
5770 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5771 p->display_ellipsis_p = 0;
5772 p->line_wrap = it->line_wrap;
5773 p->bidi_p = it->bidi_p;
5774 p->paragraph_embedding = it->paragraph_embedding;
5775 p->from_disp_prop_p = it->from_disp_prop_p;
5776 ++it->sp;
5778 /* Save the state of the bidi iterator as well. */
5779 if (it->bidi_p)
5780 bidi_push_it (&it->bidi_it);
5783 static void
5784 iterate_out_of_display_property (struct it *it)
5786 int buffer_p = !STRINGP (it->string);
5787 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5788 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5790 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5792 /* Maybe initialize paragraph direction. If we are at the beginning
5793 of a new paragraph, next_element_from_buffer may not have a
5794 chance to do that. */
5795 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5796 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5797 /* prev_stop can be zero, so check against BEGV as well. */
5798 while (it->bidi_it.charpos >= bob
5799 && it->prev_stop <= it->bidi_it.charpos
5800 && it->bidi_it.charpos < CHARPOS (it->position)
5801 && it->bidi_it.charpos < eob)
5802 bidi_move_to_visually_next (&it->bidi_it);
5803 /* Record the stop_pos we just crossed, for when we cross it
5804 back, maybe. */
5805 if (it->bidi_it.charpos > CHARPOS (it->position))
5806 it->prev_stop = CHARPOS (it->position);
5807 /* If we ended up not where pop_it put us, resync IT's
5808 positional members with the bidi iterator. */
5809 if (it->bidi_it.charpos != CHARPOS (it->position))
5810 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5811 if (buffer_p)
5812 it->current.pos = it->position;
5813 else
5814 it->current.string_pos = it->position;
5817 /* Restore IT's settings from IT->stack. Called, for example, when no
5818 more overlay strings must be processed, and we return to delivering
5819 display elements from a buffer, or when the end of a string from a
5820 `display' property is reached and we return to delivering display
5821 elements from an overlay string, or from a buffer. */
5823 static void
5824 pop_it (struct it *it)
5826 struct iterator_stack_entry *p;
5827 int from_display_prop = it->from_disp_prop_p;
5829 eassert (it->sp > 0);
5830 --it->sp;
5831 p = it->stack + it->sp;
5832 it->stop_charpos = p->stop_charpos;
5833 it->prev_stop = p->prev_stop;
5834 it->base_level_stop = p->base_level_stop;
5835 it->cmp_it = p->cmp_it;
5836 it->face_id = p->face_id;
5837 it->current = p->current;
5838 it->position = p->position;
5839 it->string = p->string;
5840 it->from_overlay = p->from_overlay;
5841 if (NILP (it->string))
5842 SET_TEXT_POS (it->current.string_pos, -1, -1);
5843 it->method = p->method;
5844 switch (it->method)
5846 case GET_FROM_IMAGE:
5847 it->image_id = p->u.image.image_id;
5848 it->object = p->u.image.object;
5849 it->slice = p->u.image.slice;
5850 break;
5851 case GET_FROM_STRETCH:
5852 it->object = p->u.stretch.object;
5853 break;
5854 case GET_FROM_BUFFER:
5855 it->object = it->w->buffer;
5856 break;
5857 case GET_FROM_STRING:
5858 it->object = it->string;
5859 break;
5860 case GET_FROM_DISPLAY_VECTOR:
5861 if (it->s)
5862 it->method = GET_FROM_C_STRING;
5863 else if (STRINGP (it->string))
5864 it->method = GET_FROM_STRING;
5865 else
5867 it->method = GET_FROM_BUFFER;
5868 it->object = it->w->buffer;
5871 it->end_charpos = p->end_charpos;
5872 it->string_nchars = p->string_nchars;
5873 it->area = p->area;
5874 it->multibyte_p = p->multibyte_p;
5875 it->avoid_cursor_p = p->avoid_cursor_p;
5876 it->space_width = p->space_width;
5877 it->font_height = p->font_height;
5878 it->voffset = p->voffset;
5879 it->string_from_display_prop_p = p->string_from_display_prop_p;
5880 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5881 it->line_wrap = p->line_wrap;
5882 it->bidi_p = p->bidi_p;
5883 it->paragraph_embedding = p->paragraph_embedding;
5884 it->from_disp_prop_p = p->from_disp_prop_p;
5885 if (it->bidi_p)
5887 bidi_pop_it (&it->bidi_it);
5888 /* Bidi-iterate until we get out of the portion of text, if any,
5889 covered by a `display' text property or by an overlay with
5890 `display' property. (We cannot just jump there, because the
5891 internal coherency of the bidi iterator state can not be
5892 preserved across such jumps.) We also must determine the
5893 paragraph base direction if the overlay we just processed is
5894 at the beginning of a new paragraph. */
5895 if (from_display_prop
5896 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5897 iterate_out_of_display_property (it);
5899 eassert ((BUFFERP (it->object)
5900 && IT_CHARPOS (*it) == it->bidi_it.charpos
5901 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5902 || (STRINGP (it->object)
5903 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5904 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5905 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5911 /***********************************************************************
5912 Moving over lines
5913 ***********************************************************************/
5915 /* Set IT's current position to the previous line start. */
5917 static void
5918 back_to_previous_line_start (struct it *it)
5920 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5921 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5925 /* Move IT to the next line start.
5927 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5928 we skipped over part of the text (as opposed to moving the iterator
5929 continuously over the text). Otherwise, don't change the value
5930 of *SKIPPED_P.
5932 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5933 iterator on the newline, if it was found.
5935 Newlines may come from buffer text, overlay strings, or strings
5936 displayed via the `display' property. That's the reason we can't
5937 simply use find_next_newline_no_quit.
5939 Note that this function may not skip over invisible text that is so
5940 because of text properties and immediately follows a newline. If
5941 it would, function reseat_at_next_visible_line_start, when called
5942 from set_iterator_to_next, would effectively make invisible
5943 characters following a newline part of the wrong glyph row, which
5944 leads to wrong cursor motion. */
5946 static int
5947 forward_to_next_line_start (struct it *it, int *skipped_p,
5948 struct bidi_it *bidi_it_prev)
5950 ptrdiff_t old_selective;
5951 int newline_found_p, n;
5952 const int MAX_NEWLINE_DISTANCE = 500;
5954 /* If already on a newline, just consume it to avoid unintended
5955 skipping over invisible text below. */
5956 if (it->what == IT_CHARACTER
5957 && it->c == '\n'
5958 && CHARPOS (it->position) == IT_CHARPOS (*it))
5960 if (it->bidi_p && bidi_it_prev)
5961 *bidi_it_prev = it->bidi_it;
5962 set_iterator_to_next (it, 0);
5963 it->c = 0;
5964 return 1;
5967 /* Don't handle selective display in the following. It's (a)
5968 unnecessary because it's done by the caller, and (b) leads to an
5969 infinite recursion because next_element_from_ellipsis indirectly
5970 calls this function. */
5971 old_selective = it->selective;
5972 it->selective = 0;
5974 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5975 from buffer text. */
5976 for (n = newline_found_p = 0;
5977 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5978 n += STRINGP (it->string) ? 0 : 1)
5980 if (!get_next_display_element (it))
5981 return 0;
5982 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5983 if (newline_found_p && it->bidi_p && bidi_it_prev)
5984 *bidi_it_prev = it->bidi_it;
5985 set_iterator_to_next (it, 0);
5988 /* If we didn't find a newline near enough, see if we can use a
5989 short-cut. */
5990 if (!newline_found_p)
5992 ptrdiff_t start = IT_CHARPOS (*it);
5993 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5994 Lisp_Object pos;
5996 eassert (!STRINGP (it->string));
5998 /* If there isn't any `display' property in sight, and no
5999 overlays, we can just use the position of the newline in
6000 buffer text. */
6001 if (it->stop_charpos >= limit
6002 || ((pos = Fnext_single_property_change (make_number (start),
6003 Qdisplay, Qnil,
6004 make_number (limit)),
6005 NILP (pos))
6006 && next_overlay_change (start) == ZV))
6008 if (!it->bidi_p)
6010 IT_CHARPOS (*it) = limit;
6011 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
6013 else
6015 struct bidi_it bprev;
6017 /* Help bidi.c avoid expensive searches for display
6018 properties and overlays, by telling it that there are
6019 none up to `limit'. */
6020 if (it->bidi_it.disp_pos < limit)
6022 it->bidi_it.disp_pos = limit;
6023 it->bidi_it.disp_prop = 0;
6025 do {
6026 bprev = it->bidi_it;
6027 bidi_move_to_visually_next (&it->bidi_it);
6028 } while (it->bidi_it.charpos != limit);
6029 IT_CHARPOS (*it) = limit;
6030 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6031 if (bidi_it_prev)
6032 *bidi_it_prev = bprev;
6034 *skipped_p = newline_found_p = 1;
6036 else
6038 while (get_next_display_element (it)
6039 && !newline_found_p)
6041 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6042 if (newline_found_p && it->bidi_p && bidi_it_prev)
6043 *bidi_it_prev = it->bidi_it;
6044 set_iterator_to_next (it, 0);
6049 it->selective = old_selective;
6050 return newline_found_p;
6054 /* Set IT's current position to the previous visible line start. Skip
6055 invisible text that is so either due to text properties or due to
6056 selective display. Caution: this does not change IT->current_x and
6057 IT->hpos. */
6059 static void
6060 back_to_previous_visible_line_start (struct it *it)
6062 while (IT_CHARPOS (*it) > BEGV)
6064 back_to_previous_line_start (it);
6066 if (IT_CHARPOS (*it) <= BEGV)
6067 break;
6069 /* If selective > 0, then lines indented more than its value are
6070 invisible. */
6071 if (it->selective > 0
6072 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6073 it->selective))
6074 continue;
6076 /* Check the newline before point for invisibility. */
6078 Lisp_Object prop;
6079 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6080 Qinvisible, it->window);
6081 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6082 continue;
6085 if (IT_CHARPOS (*it) <= BEGV)
6086 break;
6089 struct it it2;
6090 void *it2data = NULL;
6091 ptrdiff_t pos;
6092 ptrdiff_t beg, end;
6093 Lisp_Object val, overlay;
6095 SAVE_IT (it2, *it, it2data);
6097 /* If newline is part of a composition, continue from start of composition */
6098 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6099 && beg < IT_CHARPOS (*it))
6100 goto replaced;
6102 /* If newline is replaced by a display property, find start of overlay
6103 or interval and continue search from that point. */
6104 pos = --IT_CHARPOS (it2);
6105 --IT_BYTEPOS (it2);
6106 it2.sp = 0;
6107 bidi_unshelve_cache (NULL, 0);
6108 it2.string_from_display_prop_p = 0;
6109 it2.from_disp_prop_p = 0;
6110 if (handle_display_prop (&it2) == HANDLED_RETURN
6111 && !NILP (val = get_char_property_and_overlay
6112 (make_number (pos), Qdisplay, Qnil, &overlay))
6113 && (OVERLAYP (overlay)
6114 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6115 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6117 RESTORE_IT (it, it, it2data);
6118 goto replaced;
6121 /* Newline is not replaced by anything -- so we are done. */
6122 RESTORE_IT (it, it, it2data);
6123 break;
6125 replaced:
6126 if (beg < BEGV)
6127 beg = BEGV;
6128 IT_CHARPOS (*it) = beg;
6129 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6133 it->continuation_lines_width = 0;
6135 eassert (IT_CHARPOS (*it) >= BEGV);
6136 eassert (IT_CHARPOS (*it) == BEGV
6137 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6138 CHECK_IT (it);
6142 /* Reseat iterator IT at the previous visible line start. Skip
6143 invisible text that is so either due to text properties or due to
6144 selective display. At the end, update IT's overlay information,
6145 face information etc. */
6147 void
6148 reseat_at_previous_visible_line_start (struct it *it)
6150 back_to_previous_visible_line_start (it);
6151 reseat (it, it->current.pos, 1);
6152 CHECK_IT (it);
6156 /* Reseat iterator IT on the next visible line start in the current
6157 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6158 preceding the line start. Skip over invisible text that is so
6159 because of selective display. Compute faces, overlays etc at the
6160 new position. Note that this function does not skip over text that
6161 is invisible because of text properties. */
6163 static void
6164 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6166 int newline_found_p, skipped_p = 0;
6167 struct bidi_it bidi_it_prev;
6169 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6171 /* Skip over lines that are invisible because they are indented
6172 more than the value of IT->selective. */
6173 if (it->selective > 0)
6174 while (IT_CHARPOS (*it) < ZV
6175 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6176 it->selective))
6178 eassert (IT_BYTEPOS (*it) == BEGV
6179 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6180 newline_found_p =
6181 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6184 /* Position on the newline if that's what's requested. */
6185 if (on_newline_p && newline_found_p)
6187 if (STRINGP (it->string))
6189 if (IT_STRING_CHARPOS (*it) > 0)
6191 if (!it->bidi_p)
6193 --IT_STRING_CHARPOS (*it);
6194 --IT_STRING_BYTEPOS (*it);
6196 else
6198 /* We need to restore the bidi iterator to the state
6199 it had on the newline, and resync the IT's
6200 position with that. */
6201 it->bidi_it = bidi_it_prev;
6202 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6203 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6207 else if (IT_CHARPOS (*it) > BEGV)
6209 if (!it->bidi_p)
6211 --IT_CHARPOS (*it);
6212 --IT_BYTEPOS (*it);
6214 else
6216 /* We need to restore the bidi iterator to the state it
6217 had on the newline and resync IT with that. */
6218 it->bidi_it = bidi_it_prev;
6219 IT_CHARPOS (*it) = it->bidi_it.charpos;
6220 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6222 reseat (it, it->current.pos, 0);
6225 else if (skipped_p)
6226 reseat (it, it->current.pos, 0);
6228 CHECK_IT (it);
6233 /***********************************************************************
6234 Changing an iterator's position
6235 ***********************************************************************/
6237 /* Change IT's current position to POS in current_buffer. If FORCE_P
6238 is non-zero, always check for text properties at the new position.
6239 Otherwise, text properties are only looked up if POS >=
6240 IT->check_charpos of a property. */
6242 static void
6243 reseat (struct it *it, struct text_pos pos, int force_p)
6245 ptrdiff_t original_pos = IT_CHARPOS (*it);
6247 reseat_1 (it, pos, 0);
6249 /* Determine where to check text properties. Avoid doing it
6250 where possible because text property lookup is very expensive. */
6251 if (force_p
6252 || CHARPOS (pos) > it->stop_charpos
6253 || CHARPOS (pos) < original_pos)
6255 if (it->bidi_p)
6257 /* For bidi iteration, we need to prime prev_stop and
6258 base_level_stop with our best estimations. */
6259 /* Implementation note: Of course, POS is not necessarily a
6260 stop position, so assigning prev_pos to it is a lie; we
6261 should have called compute_stop_backwards. However, if
6262 the current buffer does not include any R2L characters,
6263 that call would be a waste of cycles, because the
6264 iterator will never move back, and thus never cross this
6265 "fake" stop position. So we delay that backward search
6266 until the time we really need it, in next_element_from_buffer. */
6267 if (CHARPOS (pos) != it->prev_stop)
6268 it->prev_stop = CHARPOS (pos);
6269 if (CHARPOS (pos) < it->base_level_stop)
6270 it->base_level_stop = 0; /* meaning it's unknown */
6271 handle_stop (it);
6273 else
6275 handle_stop (it);
6276 it->prev_stop = it->base_level_stop = 0;
6281 CHECK_IT (it);
6285 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6286 IT->stop_pos to POS, also. */
6288 static void
6289 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6291 /* Don't call this function when scanning a C string. */
6292 eassert (it->s == NULL);
6294 /* POS must be a reasonable value. */
6295 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6297 it->current.pos = it->position = pos;
6298 it->end_charpos = ZV;
6299 it->dpvec = NULL;
6300 it->current.dpvec_index = -1;
6301 it->current.overlay_string_index = -1;
6302 IT_STRING_CHARPOS (*it) = -1;
6303 IT_STRING_BYTEPOS (*it) = -1;
6304 it->string = Qnil;
6305 it->method = GET_FROM_BUFFER;
6306 it->object = it->w->buffer;
6307 it->area = TEXT_AREA;
6308 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6309 it->sp = 0;
6310 it->string_from_display_prop_p = 0;
6311 it->string_from_prefix_prop_p = 0;
6313 it->from_disp_prop_p = 0;
6314 it->face_before_selective_p = 0;
6315 if (it->bidi_p)
6317 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6318 &it->bidi_it);
6319 bidi_unshelve_cache (NULL, 0);
6320 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6321 it->bidi_it.string.s = NULL;
6322 it->bidi_it.string.lstring = Qnil;
6323 it->bidi_it.string.bufpos = 0;
6324 it->bidi_it.string.unibyte = 0;
6327 if (set_stop_p)
6329 it->stop_charpos = CHARPOS (pos);
6330 it->base_level_stop = CHARPOS (pos);
6332 /* This make the information stored in it->cmp_it invalidate. */
6333 it->cmp_it.id = -1;
6337 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6338 If S is non-null, it is a C string to iterate over. Otherwise,
6339 STRING gives a Lisp string to iterate over.
6341 If PRECISION > 0, don't return more then PRECISION number of
6342 characters from the string.
6344 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6345 characters have been returned. FIELD_WIDTH < 0 means an infinite
6346 field width.
6348 MULTIBYTE = 0 means disable processing of multibyte characters,
6349 MULTIBYTE > 0 means enable it,
6350 MULTIBYTE < 0 means use IT->multibyte_p.
6352 IT must be initialized via a prior call to init_iterator before
6353 calling this function. */
6355 static void
6356 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6357 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6358 int multibyte)
6360 /* No region in strings. */
6361 it->region_beg_charpos = it->region_end_charpos = -1;
6363 /* No text property checks performed by default, but see below. */
6364 it->stop_charpos = -1;
6366 /* Set iterator position and end position. */
6367 memset (&it->current, 0, sizeof it->current);
6368 it->current.overlay_string_index = -1;
6369 it->current.dpvec_index = -1;
6370 eassert (charpos >= 0);
6372 /* If STRING is specified, use its multibyteness, otherwise use the
6373 setting of MULTIBYTE, if specified. */
6374 if (multibyte >= 0)
6375 it->multibyte_p = multibyte > 0;
6377 /* Bidirectional reordering of strings is controlled by the default
6378 value of bidi-display-reordering. Don't try to reorder while
6379 loading loadup.el, as the necessary character property tables are
6380 not yet available. */
6381 it->bidi_p =
6382 NILP (Vpurify_flag)
6383 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6385 if (s == NULL)
6387 eassert (STRINGP (string));
6388 it->string = string;
6389 it->s = NULL;
6390 it->end_charpos = it->string_nchars = SCHARS (string);
6391 it->method = GET_FROM_STRING;
6392 it->current.string_pos = string_pos (charpos, string);
6394 if (it->bidi_p)
6396 it->bidi_it.string.lstring = string;
6397 it->bidi_it.string.s = NULL;
6398 it->bidi_it.string.schars = it->end_charpos;
6399 it->bidi_it.string.bufpos = 0;
6400 it->bidi_it.string.from_disp_str = 0;
6401 it->bidi_it.string.unibyte = !it->multibyte_p;
6402 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6403 FRAME_WINDOW_P (it->f), &it->bidi_it);
6406 else
6408 it->s = (const unsigned char *) s;
6409 it->string = Qnil;
6411 /* Note that we use IT->current.pos, not it->current.string_pos,
6412 for displaying C strings. */
6413 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6414 if (it->multibyte_p)
6416 it->current.pos = c_string_pos (charpos, s, 1);
6417 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6419 else
6421 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6422 it->end_charpos = it->string_nchars = strlen (s);
6425 if (it->bidi_p)
6427 it->bidi_it.string.lstring = Qnil;
6428 it->bidi_it.string.s = (const unsigned char *) s;
6429 it->bidi_it.string.schars = it->end_charpos;
6430 it->bidi_it.string.bufpos = 0;
6431 it->bidi_it.string.from_disp_str = 0;
6432 it->bidi_it.string.unibyte = !it->multibyte_p;
6433 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6434 &it->bidi_it);
6436 it->method = GET_FROM_C_STRING;
6439 /* PRECISION > 0 means don't return more than PRECISION characters
6440 from the string. */
6441 if (precision > 0 && it->end_charpos - charpos > precision)
6443 it->end_charpos = it->string_nchars = charpos + precision;
6444 if (it->bidi_p)
6445 it->bidi_it.string.schars = it->end_charpos;
6448 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6449 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6450 FIELD_WIDTH < 0 means infinite field width. This is useful for
6451 padding with `-' at the end of a mode line. */
6452 if (field_width < 0)
6453 field_width = INFINITY;
6454 /* Implementation note: We deliberately don't enlarge
6455 it->bidi_it.string.schars here to fit it->end_charpos, because
6456 the bidi iterator cannot produce characters out of thin air. */
6457 if (field_width > it->end_charpos - charpos)
6458 it->end_charpos = charpos + field_width;
6460 /* Use the standard display table for displaying strings. */
6461 if (DISP_TABLE_P (Vstandard_display_table))
6462 it->dp = XCHAR_TABLE (Vstandard_display_table);
6464 it->stop_charpos = charpos;
6465 it->prev_stop = charpos;
6466 it->base_level_stop = 0;
6467 if (it->bidi_p)
6469 it->bidi_it.first_elt = 1;
6470 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6471 it->bidi_it.disp_pos = -1;
6473 if (s == NULL && it->multibyte_p)
6475 ptrdiff_t endpos = SCHARS (it->string);
6476 if (endpos > it->end_charpos)
6477 endpos = it->end_charpos;
6478 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6479 it->string);
6481 CHECK_IT (it);
6486 /***********************************************************************
6487 Iteration
6488 ***********************************************************************/
6490 /* Map enum it_method value to corresponding next_element_from_* function. */
6492 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6494 next_element_from_buffer,
6495 next_element_from_display_vector,
6496 next_element_from_string,
6497 next_element_from_c_string,
6498 next_element_from_image,
6499 next_element_from_stretch
6502 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6505 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6506 (possibly with the following characters). */
6508 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6509 ((IT)->cmp_it.id >= 0 \
6510 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6511 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6512 END_CHARPOS, (IT)->w, \
6513 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6514 (IT)->string)))
6517 /* Lookup the char-table Vglyphless_char_display for character C (-1
6518 if we want information for no-font case), and return the display
6519 method symbol. By side-effect, update it->what and
6520 it->glyphless_method. This function is called from
6521 get_next_display_element for each character element, and from
6522 x_produce_glyphs when no suitable font was found. */
6524 Lisp_Object
6525 lookup_glyphless_char_display (int c, struct it *it)
6527 Lisp_Object glyphless_method = Qnil;
6529 if (CHAR_TABLE_P (Vglyphless_char_display)
6530 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6532 if (c >= 0)
6534 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6535 if (CONSP (glyphless_method))
6536 glyphless_method = FRAME_WINDOW_P (it->f)
6537 ? XCAR (glyphless_method)
6538 : XCDR (glyphless_method);
6540 else
6541 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6544 retry:
6545 if (NILP (glyphless_method))
6547 if (c >= 0)
6548 /* The default is to display the character by a proper font. */
6549 return Qnil;
6550 /* The default for the no-font case is to display an empty box. */
6551 glyphless_method = Qempty_box;
6553 if (EQ (glyphless_method, Qzero_width))
6555 if (c >= 0)
6556 return glyphless_method;
6557 /* This method can't be used for the no-font case. */
6558 glyphless_method = Qempty_box;
6560 if (EQ (glyphless_method, Qthin_space))
6561 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6562 else if (EQ (glyphless_method, Qempty_box))
6563 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6564 else if (EQ (glyphless_method, Qhex_code))
6565 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6566 else if (STRINGP (glyphless_method))
6567 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6568 else
6570 /* Invalid value. We use the default method. */
6571 glyphless_method = Qnil;
6572 goto retry;
6574 it->what = IT_GLYPHLESS;
6575 return glyphless_method;
6578 /* Load IT's display element fields with information about the next
6579 display element from the current position of IT. Value is zero if
6580 end of buffer (or C string) is reached. */
6582 static struct frame *last_escape_glyph_frame = NULL;
6583 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6584 static int last_escape_glyph_merged_face_id = 0;
6586 struct frame *last_glyphless_glyph_frame = NULL;
6587 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6588 int last_glyphless_glyph_merged_face_id = 0;
6590 static int
6591 get_next_display_element (struct it *it)
6593 /* Non-zero means that we found a display element. Zero means that
6594 we hit the end of what we iterate over. Performance note: the
6595 function pointer `method' used here turns out to be faster than
6596 using a sequence of if-statements. */
6597 int success_p;
6599 get_next:
6600 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6602 if (it->what == IT_CHARACTER)
6604 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6605 and only if (a) the resolved directionality of that character
6606 is R..." */
6607 /* FIXME: Do we need an exception for characters from display
6608 tables? */
6609 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6610 it->c = bidi_mirror_char (it->c);
6611 /* Map via display table or translate control characters.
6612 IT->c, IT->len etc. have been set to the next character by
6613 the function call above. If we have a display table, and it
6614 contains an entry for IT->c, translate it. Don't do this if
6615 IT->c itself comes from a display table, otherwise we could
6616 end up in an infinite recursion. (An alternative could be to
6617 count the recursion depth of this function and signal an
6618 error when a certain maximum depth is reached.) Is it worth
6619 it? */
6620 if (success_p && it->dpvec == NULL)
6622 Lisp_Object dv;
6623 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6624 int nonascii_space_p = 0;
6625 int nonascii_hyphen_p = 0;
6626 int c = it->c; /* This is the character to display. */
6628 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6630 eassert (SINGLE_BYTE_CHAR_P (c));
6631 if (unibyte_display_via_language_environment)
6633 c = DECODE_CHAR (unibyte, c);
6634 if (c < 0)
6635 c = BYTE8_TO_CHAR (it->c);
6637 else
6638 c = BYTE8_TO_CHAR (it->c);
6641 if (it->dp
6642 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6643 VECTORP (dv)))
6645 struct Lisp_Vector *v = XVECTOR (dv);
6647 /* Return the first character from the display table
6648 entry, if not empty. If empty, don't display the
6649 current character. */
6650 if (v->header.size)
6652 it->dpvec_char_len = it->len;
6653 it->dpvec = v->contents;
6654 it->dpend = v->contents + v->header.size;
6655 it->current.dpvec_index = 0;
6656 it->dpvec_face_id = -1;
6657 it->saved_face_id = it->face_id;
6658 it->method = GET_FROM_DISPLAY_VECTOR;
6659 it->ellipsis_p = 0;
6661 else
6663 set_iterator_to_next (it, 0);
6665 goto get_next;
6668 if (! NILP (lookup_glyphless_char_display (c, it)))
6670 if (it->what == IT_GLYPHLESS)
6671 goto done;
6672 /* Don't display this character. */
6673 set_iterator_to_next (it, 0);
6674 goto get_next;
6677 /* If `nobreak-char-display' is non-nil, we display
6678 non-ASCII spaces and hyphens specially. */
6679 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6681 if (c == 0xA0)
6682 nonascii_space_p = 1;
6683 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6684 nonascii_hyphen_p = 1;
6687 /* Translate control characters into `\003' or `^C' form.
6688 Control characters coming from a display table entry are
6689 currently not translated because we use IT->dpvec to hold
6690 the translation. This could easily be changed but I
6691 don't believe that it is worth doing.
6693 The characters handled by `nobreak-char-display' must be
6694 translated too.
6696 Non-printable characters and raw-byte characters are also
6697 translated to octal form. */
6698 if (((c < ' ' || c == 127) /* ASCII control chars */
6699 ? (it->area != TEXT_AREA
6700 /* In mode line, treat \n, \t like other crl chars. */
6701 || (c != '\t'
6702 && it->glyph_row
6703 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6704 || (c != '\n' && c != '\t'))
6705 : (nonascii_space_p
6706 || nonascii_hyphen_p
6707 || CHAR_BYTE8_P (c)
6708 || ! CHAR_PRINTABLE_P (c))))
6710 /* C is a control character, non-ASCII space/hyphen,
6711 raw-byte, or a non-printable character which must be
6712 displayed either as '\003' or as `^C' where the '\\'
6713 and '^' can be defined in the display table. Fill
6714 IT->ctl_chars with glyphs for what we have to
6715 display. Then, set IT->dpvec to these glyphs. */
6716 Lisp_Object gc;
6717 int ctl_len;
6718 int face_id;
6719 int lface_id = 0;
6720 int escape_glyph;
6722 /* Handle control characters with ^. */
6724 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6726 int g;
6728 g = '^'; /* default glyph for Control */
6729 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6730 if (it->dp
6731 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6733 g = GLYPH_CODE_CHAR (gc);
6734 lface_id = GLYPH_CODE_FACE (gc);
6736 if (lface_id)
6738 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6740 else if (it->f == last_escape_glyph_frame
6741 && it->face_id == last_escape_glyph_face_id)
6743 face_id = last_escape_glyph_merged_face_id;
6745 else
6747 /* Merge the escape-glyph face into the current face. */
6748 face_id = merge_faces (it->f, Qescape_glyph, 0,
6749 it->face_id);
6750 last_escape_glyph_frame = it->f;
6751 last_escape_glyph_face_id = it->face_id;
6752 last_escape_glyph_merged_face_id = face_id;
6755 XSETINT (it->ctl_chars[0], g);
6756 XSETINT (it->ctl_chars[1], c ^ 0100);
6757 ctl_len = 2;
6758 goto display_control;
6761 /* Handle non-ascii space in the mode where it only gets
6762 highlighting. */
6764 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6766 /* Merge `nobreak-space' into the current face. */
6767 face_id = merge_faces (it->f, Qnobreak_space, 0,
6768 it->face_id);
6769 XSETINT (it->ctl_chars[0], ' ');
6770 ctl_len = 1;
6771 goto display_control;
6774 /* Handle sequences that start with the "escape glyph". */
6776 /* the default escape glyph is \. */
6777 escape_glyph = '\\';
6779 if (it->dp
6780 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6782 escape_glyph = GLYPH_CODE_CHAR (gc);
6783 lface_id = GLYPH_CODE_FACE (gc);
6785 if (lface_id)
6787 /* The display table specified a face.
6788 Merge it into face_id and also into escape_glyph. */
6789 face_id = merge_faces (it->f, Qt, lface_id,
6790 it->face_id);
6792 else if (it->f == last_escape_glyph_frame
6793 && it->face_id == last_escape_glyph_face_id)
6795 face_id = last_escape_glyph_merged_face_id;
6797 else
6799 /* Merge the escape-glyph face into the current face. */
6800 face_id = merge_faces (it->f, Qescape_glyph, 0,
6801 it->face_id);
6802 last_escape_glyph_frame = it->f;
6803 last_escape_glyph_face_id = it->face_id;
6804 last_escape_glyph_merged_face_id = face_id;
6807 /* Draw non-ASCII hyphen with just highlighting: */
6809 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6811 XSETINT (it->ctl_chars[0], '-');
6812 ctl_len = 1;
6813 goto display_control;
6816 /* Draw non-ASCII space/hyphen with escape glyph: */
6818 if (nonascii_space_p || nonascii_hyphen_p)
6820 XSETINT (it->ctl_chars[0], escape_glyph);
6821 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6822 ctl_len = 2;
6823 goto display_control;
6827 char str[10];
6828 int len, i;
6830 if (CHAR_BYTE8_P (c))
6831 /* Display \200 instead of \17777600. */
6832 c = CHAR_TO_BYTE8 (c);
6833 len = sprintf (str, "%03o", c);
6835 XSETINT (it->ctl_chars[0], escape_glyph);
6836 for (i = 0; i < len; i++)
6837 XSETINT (it->ctl_chars[i + 1], str[i]);
6838 ctl_len = len + 1;
6841 display_control:
6842 /* Set up IT->dpvec and return first character from it. */
6843 it->dpvec_char_len = it->len;
6844 it->dpvec = it->ctl_chars;
6845 it->dpend = it->dpvec + ctl_len;
6846 it->current.dpvec_index = 0;
6847 it->dpvec_face_id = face_id;
6848 it->saved_face_id = it->face_id;
6849 it->method = GET_FROM_DISPLAY_VECTOR;
6850 it->ellipsis_p = 0;
6851 goto get_next;
6853 it->char_to_display = c;
6855 else if (success_p)
6857 it->char_to_display = it->c;
6861 /* Adjust face id for a multibyte character. There are no multibyte
6862 character in unibyte text. */
6863 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6864 && it->multibyte_p
6865 && success_p
6866 && FRAME_WINDOW_P (it->f))
6868 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6870 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6872 /* Automatic composition with glyph-string. */
6873 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6875 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6877 else
6879 ptrdiff_t pos = (it->s ? -1
6880 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6881 : IT_CHARPOS (*it));
6882 int c;
6884 if (it->what == IT_CHARACTER)
6885 c = it->char_to_display;
6886 else
6888 struct composition *cmp = composition_table[it->cmp_it.id];
6889 int i;
6891 c = ' ';
6892 for (i = 0; i < cmp->glyph_len; i++)
6893 /* TAB in a composition means display glyphs with
6894 padding space on the left or right. */
6895 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6896 break;
6898 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6902 done:
6903 /* Is this character the last one of a run of characters with
6904 box? If yes, set IT->end_of_box_run_p to 1. */
6905 if (it->face_box_p
6906 && it->s == NULL)
6908 if (it->method == GET_FROM_STRING && it->sp)
6910 int face_id = underlying_face_id (it);
6911 struct face *face = FACE_FROM_ID (it->f, face_id);
6913 if (face)
6915 if (face->box == FACE_NO_BOX)
6917 /* If the box comes from face properties in a
6918 display string, check faces in that string. */
6919 int string_face_id = face_after_it_pos (it);
6920 it->end_of_box_run_p
6921 = (FACE_FROM_ID (it->f, string_face_id)->box
6922 == FACE_NO_BOX);
6924 /* Otherwise, the box comes from the underlying face.
6925 If this is the last string character displayed, check
6926 the next buffer location. */
6927 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6928 && (it->current.overlay_string_index
6929 == it->n_overlay_strings - 1))
6931 ptrdiff_t ignore;
6932 int next_face_id;
6933 struct text_pos pos = it->current.pos;
6934 INC_TEXT_POS (pos, it->multibyte_p);
6936 next_face_id = face_at_buffer_position
6937 (it->w, CHARPOS (pos), it->region_beg_charpos,
6938 it->region_end_charpos, &ignore,
6939 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6940 -1);
6941 it->end_of_box_run_p
6942 = (FACE_FROM_ID (it->f, next_face_id)->box
6943 == FACE_NO_BOX);
6947 else
6949 int face_id = face_after_it_pos (it);
6950 it->end_of_box_run_p
6951 = (face_id != it->face_id
6952 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6955 /* If we reached the end of the object we've been iterating (e.g., a
6956 display string or an overlay string), and there's something on
6957 IT->stack, proceed with what's on the stack. It doesn't make
6958 sense to return zero if there's unprocessed stuff on the stack,
6959 because otherwise that stuff will never be displayed. */
6960 if (!success_p && it->sp > 0)
6962 set_iterator_to_next (it, 0);
6963 success_p = get_next_display_element (it);
6966 /* Value is 0 if end of buffer or string reached. */
6967 return success_p;
6971 /* Move IT to the next display element.
6973 RESEAT_P non-zero means if called on a newline in buffer text,
6974 skip to the next visible line start.
6976 Functions get_next_display_element and set_iterator_to_next are
6977 separate because I find this arrangement easier to handle than a
6978 get_next_display_element function that also increments IT's
6979 position. The way it is we can first look at an iterator's current
6980 display element, decide whether it fits on a line, and if it does,
6981 increment the iterator position. The other way around we probably
6982 would either need a flag indicating whether the iterator has to be
6983 incremented the next time, or we would have to implement a
6984 decrement position function which would not be easy to write. */
6986 void
6987 set_iterator_to_next (struct it *it, int reseat_p)
6989 /* Reset flags indicating start and end of a sequence of characters
6990 with box. Reset them at the start of this function because
6991 moving the iterator to a new position might set them. */
6992 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6994 switch (it->method)
6996 case GET_FROM_BUFFER:
6997 /* The current display element of IT is a character from
6998 current_buffer. Advance in the buffer, and maybe skip over
6999 invisible lines that are so because of selective display. */
7000 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7001 reseat_at_next_visible_line_start (it, 0);
7002 else if (it->cmp_it.id >= 0)
7004 /* We are currently getting glyphs from a composition. */
7005 int i;
7007 if (! it->bidi_p)
7009 IT_CHARPOS (*it) += it->cmp_it.nchars;
7010 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7011 if (it->cmp_it.to < it->cmp_it.nglyphs)
7013 it->cmp_it.from = it->cmp_it.to;
7015 else
7017 it->cmp_it.id = -1;
7018 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7019 IT_BYTEPOS (*it),
7020 it->end_charpos, Qnil);
7023 else if (! it->cmp_it.reversed_p)
7025 /* Composition created while scanning forward. */
7026 /* Update IT's char/byte positions to point to the first
7027 character of the next grapheme cluster, or to the
7028 character visually after the current composition. */
7029 for (i = 0; i < it->cmp_it.nchars; i++)
7030 bidi_move_to_visually_next (&it->bidi_it);
7031 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7032 IT_CHARPOS (*it) = it->bidi_it.charpos;
7034 if (it->cmp_it.to < it->cmp_it.nglyphs)
7036 /* Proceed to the next grapheme cluster. */
7037 it->cmp_it.from = it->cmp_it.to;
7039 else
7041 /* No more grapheme clusters in this composition.
7042 Find the next stop position. */
7043 ptrdiff_t stop = it->end_charpos;
7044 if (it->bidi_it.scan_dir < 0)
7045 /* Now we are scanning backward and don't know
7046 where to stop. */
7047 stop = -1;
7048 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7049 IT_BYTEPOS (*it), stop, Qnil);
7052 else
7054 /* Composition created while scanning backward. */
7055 /* Update IT's char/byte positions to point to the last
7056 character of the previous grapheme cluster, or the
7057 character visually after the current composition. */
7058 for (i = 0; i < it->cmp_it.nchars; i++)
7059 bidi_move_to_visually_next (&it->bidi_it);
7060 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7061 IT_CHARPOS (*it) = it->bidi_it.charpos;
7062 if (it->cmp_it.from > 0)
7064 /* Proceed to the previous grapheme cluster. */
7065 it->cmp_it.to = it->cmp_it.from;
7067 else
7069 /* No more grapheme clusters in this composition.
7070 Find the next stop position. */
7071 ptrdiff_t stop = it->end_charpos;
7072 if (it->bidi_it.scan_dir < 0)
7073 /* Now we are scanning backward and don't know
7074 where to stop. */
7075 stop = -1;
7076 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7077 IT_BYTEPOS (*it), stop, Qnil);
7081 else
7083 eassert (it->len != 0);
7085 if (!it->bidi_p)
7087 IT_BYTEPOS (*it) += it->len;
7088 IT_CHARPOS (*it) += 1;
7090 else
7092 int prev_scan_dir = it->bidi_it.scan_dir;
7093 /* If this is a new paragraph, determine its base
7094 direction (a.k.a. its base embedding level). */
7095 if (it->bidi_it.new_paragraph)
7096 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7097 bidi_move_to_visually_next (&it->bidi_it);
7098 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7099 IT_CHARPOS (*it) = it->bidi_it.charpos;
7100 if (prev_scan_dir != it->bidi_it.scan_dir)
7102 /* As the scan direction was changed, we must
7103 re-compute the stop position for composition. */
7104 ptrdiff_t stop = it->end_charpos;
7105 if (it->bidi_it.scan_dir < 0)
7106 stop = -1;
7107 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7108 IT_BYTEPOS (*it), stop, Qnil);
7111 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7113 break;
7115 case GET_FROM_C_STRING:
7116 /* Current display element of IT is from a C string. */
7117 if (!it->bidi_p
7118 /* If the string position is beyond string's end, it means
7119 next_element_from_c_string is padding the string with
7120 blanks, in which case we bypass the bidi iterator,
7121 because it cannot deal with such virtual characters. */
7122 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7124 IT_BYTEPOS (*it) += it->len;
7125 IT_CHARPOS (*it) += 1;
7127 else
7129 bidi_move_to_visually_next (&it->bidi_it);
7130 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7131 IT_CHARPOS (*it) = it->bidi_it.charpos;
7133 break;
7135 case GET_FROM_DISPLAY_VECTOR:
7136 /* Current display element of IT is from a display table entry.
7137 Advance in the display table definition. Reset it to null if
7138 end reached, and continue with characters from buffers/
7139 strings. */
7140 ++it->current.dpvec_index;
7142 /* Restore face of the iterator to what they were before the
7143 display vector entry (these entries may contain faces). */
7144 it->face_id = it->saved_face_id;
7146 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7148 int recheck_faces = it->ellipsis_p;
7150 if (it->s)
7151 it->method = GET_FROM_C_STRING;
7152 else if (STRINGP (it->string))
7153 it->method = GET_FROM_STRING;
7154 else
7156 it->method = GET_FROM_BUFFER;
7157 it->object = it->w->buffer;
7160 it->dpvec = NULL;
7161 it->current.dpvec_index = -1;
7163 /* Skip over characters which were displayed via IT->dpvec. */
7164 if (it->dpvec_char_len < 0)
7165 reseat_at_next_visible_line_start (it, 1);
7166 else if (it->dpvec_char_len > 0)
7168 if (it->method == GET_FROM_STRING
7169 && it->n_overlay_strings > 0)
7170 it->ignore_overlay_strings_at_pos_p = 1;
7171 it->len = it->dpvec_char_len;
7172 set_iterator_to_next (it, reseat_p);
7175 /* Maybe recheck faces after display vector */
7176 if (recheck_faces)
7177 it->stop_charpos = IT_CHARPOS (*it);
7179 break;
7181 case GET_FROM_STRING:
7182 /* Current display element is a character from a Lisp string. */
7183 eassert (it->s == NULL && STRINGP (it->string));
7184 /* Don't advance past string end. These conditions are true
7185 when set_iterator_to_next is called at the end of
7186 get_next_display_element, in which case the Lisp string is
7187 already exhausted, and all we want is pop the iterator
7188 stack. */
7189 if (it->current.overlay_string_index >= 0)
7191 /* This is an overlay string, so there's no padding with
7192 spaces, and the number of characters in the string is
7193 where the string ends. */
7194 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7195 goto consider_string_end;
7197 else
7199 /* Not an overlay string. There could be padding, so test
7200 against it->end_charpos . */
7201 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7202 goto consider_string_end;
7204 if (it->cmp_it.id >= 0)
7206 int i;
7208 if (! it->bidi_p)
7210 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7211 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7212 if (it->cmp_it.to < it->cmp_it.nglyphs)
7213 it->cmp_it.from = it->cmp_it.to;
7214 else
7216 it->cmp_it.id = -1;
7217 composition_compute_stop_pos (&it->cmp_it,
7218 IT_STRING_CHARPOS (*it),
7219 IT_STRING_BYTEPOS (*it),
7220 it->end_charpos, it->string);
7223 else if (! it->cmp_it.reversed_p)
7225 for (i = 0; i < it->cmp_it.nchars; i++)
7226 bidi_move_to_visually_next (&it->bidi_it);
7227 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7228 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7230 if (it->cmp_it.to < it->cmp_it.nglyphs)
7231 it->cmp_it.from = it->cmp_it.to;
7232 else
7234 ptrdiff_t stop = it->end_charpos;
7235 if (it->bidi_it.scan_dir < 0)
7236 stop = -1;
7237 composition_compute_stop_pos (&it->cmp_it,
7238 IT_STRING_CHARPOS (*it),
7239 IT_STRING_BYTEPOS (*it), stop,
7240 it->string);
7243 else
7245 for (i = 0; i < it->cmp_it.nchars; i++)
7246 bidi_move_to_visually_next (&it->bidi_it);
7247 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7248 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7249 if (it->cmp_it.from > 0)
7250 it->cmp_it.to = it->cmp_it.from;
7251 else
7253 ptrdiff_t stop = it->end_charpos;
7254 if (it->bidi_it.scan_dir < 0)
7255 stop = -1;
7256 composition_compute_stop_pos (&it->cmp_it,
7257 IT_STRING_CHARPOS (*it),
7258 IT_STRING_BYTEPOS (*it), stop,
7259 it->string);
7263 else
7265 if (!it->bidi_p
7266 /* If the string position is beyond string's end, it
7267 means next_element_from_string is padding the string
7268 with blanks, in which case we bypass the bidi
7269 iterator, because it cannot deal with such virtual
7270 characters. */
7271 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7273 IT_STRING_BYTEPOS (*it) += it->len;
7274 IT_STRING_CHARPOS (*it) += 1;
7276 else
7278 int prev_scan_dir = it->bidi_it.scan_dir;
7280 bidi_move_to_visually_next (&it->bidi_it);
7281 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7282 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7283 if (prev_scan_dir != it->bidi_it.scan_dir)
7285 ptrdiff_t stop = it->end_charpos;
7287 if (it->bidi_it.scan_dir < 0)
7288 stop = -1;
7289 composition_compute_stop_pos (&it->cmp_it,
7290 IT_STRING_CHARPOS (*it),
7291 IT_STRING_BYTEPOS (*it), stop,
7292 it->string);
7297 consider_string_end:
7299 if (it->current.overlay_string_index >= 0)
7301 /* IT->string is an overlay string. Advance to the
7302 next, if there is one. */
7303 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7305 it->ellipsis_p = 0;
7306 next_overlay_string (it);
7307 if (it->ellipsis_p)
7308 setup_for_ellipsis (it, 0);
7311 else
7313 /* IT->string is not an overlay string. If we reached
7314 its end, and there is something on IT->stack, proceed
7315 with what is on the stack. This can be either another
7316 string, this time an overlay string, or a buffer. */
7317 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7318 && it->sp > 0)
7320 pop_it (it);
7321 if (it->method == GET_FROM_STRING)
7322 goto consider_string_end;
7325 break;
7327 case GET_FROM_IMAGE:
7328 case GET_FROM_STRETCH:
7329 /* The position etc with which we have to proceed are on
7330 the stack. The position may be at the end of a string,
7331 if the `display' property takes up the whole string. */
7332 eassert (it->sp > 0);
7333 pop_it (it);
7334 if (it->method == GET_FROM_STRING)
7335 goto consider_string_end;
7336 break;
7338 default:
7339 /* There are no other methods defined, so this should be a bug. */
7340 emacs_abort ();
7343 eassert (it->method != GET_FROM_STRING
7344 || (STRINGP (it->string)
7345 && IT_STRING_CHARPOS (*it) >= 0));
7348 /* Load IT's display element fields with information about the next
7349 display element which comes from a display table entry or from the
7350 result of translating a control character to one of the forms `^C'
7351 or `\003'.
7353 IT->dpvec holds the glyphs to return as characters.
7354 IT->saved_face_id holds the face id before the display vector--it
7355 is restored into IT->face_id in set_iterator_to_next. */
7357 static int
7358 next_element_from_display_vector (struct it *it)
7360 Lisp_Object gc;
7362 /* Precondition. */
7363 eassert (it->dpvec && it->current.dpvec_index >= 0);
7365 it->face_id = it->saved_face_id;
7367 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7368 That seemed totally bogus - so I changed it... */
7369 gc = it->dpvec[it->current.dpvec_index];
7371 if (GLYPH_CODE_P (gc))
7373 it->c = GLYPH_CODE_CHAR (gc);
7374 it->len = CHAR_BYTES (it->c);
7376 /* The entry may contain a face id to use. Such a face id is
7377 the id of a Lisp face, not a realized face. A face id of
7378 zero means no face is specified. */
7379 if (it->dpvec_face_id >= 0)
7380 it->face_id = it->dpvec_face_id;
7381 else
7383 int lface_id = GLYPH_CODE_FACE (gc);
7384 if (lface_id > 0)
7385 it->face_id = merge_faces (it->f, Qt, lface_id,
7386 it->saved_face_id);
7389 else
7390 /* Display table entry is invalid. Return a space. */
7391 it->c = ' ', it->len = 1;
7393 /* Don't change position and object of the iterator here. They are
7394 still the values of the character that had this display table
7395 entry or was translated, and that's what we want. */
7396 it->what = IT_CHARACTER;
7397 return 1;
7400 /* Get the first element of string/buffer in the visual order, after
7401 being reseated to a new position in a string or a buffer. */
7402 static void
7403 get_visually_first_element (struct it *it)
7405 int string_p = STRINGP (it->string) || it->s;
7406 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7407 ptrdiff_t bob = (string_p ? 0 : BEGV);
7409 if (STRINGP (it->string))
7411 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7412 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7414 else
7416 it->bidi_it.charpos = IT_CHARPOS (*it);
7417 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7420 if (it->bidi_it.charpos == eob)
7422 /* Nothing to do, but reset the FIRST_ELT flag, like
7423 bidi_paragraph_init does, because we are not going to
7424 call it. */
7425 it->bidi_it.first_elt = 0;
7427 else if (it->bidi_it.charpos == bob
7428 || (!string_p
7429 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7430 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7432 /* If we are at the beginning of a line/string, we can produce
7433 the next element right away. */
7434 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7435 bidi_move_to_visually_next (&it->bidi_it);
7437 else
7439 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7441 /* We need to prime the bidi iterator starting at the line's or
7442 string's beginning, before we will be able to produce the
7443 next element. */
7444 if (string_p)
7445 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7446 else
7448 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7449 -1);
7450 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7452 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7455 /* Now return to buffer/string position where we were asked
7456 to get the next display element, and produce that. */
7457 bidi_move_to_visually_next (&it->bidi_it);
7459 while (it->bidi_it.bytepos != orig_bytepos
7460 && it->bidi_it.charpos < eob);
7463 /* Adjust IT's position information to where we ended up. */
7464 if (STRINGP (it->string))
7466 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7467 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7469 else
7471 IT_CHARPOS (*it) = it->bidi_it.charpos;
7472 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7475 if (STRINGP (it->string) || !it->s)
7477 ptrdiff_t stop, charpos, bytepos;
7479 if (STRINGP (it->string))
7481 eassert (!it->s);
7482 stop = SCHARS (it->string);
7483 if (stop > it->end_charpos)
7484 stop = it->end_charpos;
7485 charpos = IT_STRING_CHARPOS (*it);
7486 bytepos = IT_STRING_BYTEPOS (*it);
7488 else
7490 stop = it->end_charpos;
7491 charpos = IT_CHARPOS (*it);
7492 bytepos = IT_BYTEPOS (*it);
7494 if (it->bidi_it.scan_dir < 0)
7495 stop = -1;
7496 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7497 it->string);
7501 /* Load IT with the next display element from Lisp string IT->string.
7502 IT->current.string_pos is the current position within the string.
7503 If IT->current.overlay_string_index >= 0, the Lisp string is an
7504 overlay string. */
7506 static int
7507 next_element_from_string (struct it *it)
7509 struct text_pos position;
7511 eassert (STRINGP (it->string));
7512 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7513 eassert (IT_STRING_CHARPOS (*it) >= 0);
7514 position = it->current.string_pos;
7516 /* With bidi reordering, the character to display might not be the
7517 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7518 that we were reseat()ed to a new string, whose paragraph
7519 direction is not known. */
7520 if (it->bidi_p && it->bidi_it.first_elt)
7522 get_visually_first_element (it);
7523 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7526 /* Time to check for invisible text? */
7527 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7529 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7531 if (!(!it->bidi_p
7532 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7533 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7535 /* With bidi non-linear iteration, we could find
7536 ourselves far beyond the last computed stop_charpos,
7537 with several other stop positions in between that we
7538 missed. Scan them all now, in buffer's logical
7539 order, until we find and handle the last stop_charpos
7540 that precedes our current position. */
7541 handle_stop_backwards (it, it->stop_charpos);
7542 return GET_NEXT_DISPLAY_ELEMENT (it);
7544 else
7546 if (it->bidi_p)
7548 /* Take note of the stop position we just moved
7549 across, for when we will move back across it. */
7550 it->prev_stop = it->stop_charpos;
7551 /* If we are at base paragraph embedding level, take
7552 note of the last stop position seen at this
7553 level. */
7554 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7555 it->base_level_stop = it->stop_charpos;
7557 handle_stop (it);
7559 /* Since a handler may have changed IT->method, we must
7560 recurse here. */
7561 return GET_NEXT_DISPLAY_ELEMENT (it);
7564 else if (it->bidi_p
7565 /* If we are before prev_stop, we may have overstepped
7566 on our way backwards a stop_pos, and if so, we need
7567 to handle that stop_pos. */
7568 && IT_STRING_CHARPOS (*it) < it->prev_stop
7569 /* We can sometimes back up for reasons that have nothing
7570 to do with bidi reordering. E.g., compositions. The
7571 code below is only needed when we are above the base
7572 embedding level, so test for that explicitly. */
7573 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7575 /* If we lost track of base_level_stop, we have no better
7576 place for handle_stop_backwards to start from than string
7577 beginning. This happens, e.g., when we were reseated to
7578 the previous screenful of text by vertical-motion. */
7579 if (it->base_level_stop <= 0
7580 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7581 it->base_level_stop = 0;
7582 handle_stop_backwards (it, it->base_level_stop);
7583 return GET_NEXT_DISPLAY_ELEMENT (it);
7587 if (it->current.overlay_string_index >= 0)
7589 /* Get the next character from an overlay string. In overlay
7590 strings, there is no field width or padding with spaces to
7591 do. */
7592 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7594 it->what = IT_EOB;
7595 return 0;
7597 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7598 IT_STRING_BYTEPOS (*it),
7599 it->bidi_it.scan_dir < 0
7600 ? -1
7601 : SCHARS (it->string))
7602 && next_element_from_composition (it))
7604 return 1;
7606 else if (STRING_MULTIBYTE (it->string))
7608 const unsigned char *s = (SDATA (it->string)
7609 + IT_STRING_BYTEPOS (*it));
7610 it->c = string_char_and_length (s, &it->len);
7612 else
7614 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7615 it->len = 1;
7618 else
7620 /* Get the next character from a Lisp string that is not an
7621 overlay string. Such strings come from the mode line, for
7622 example. We may have to pad with spaces, or truncate the
7623 string. See also next_element_from_c_string. */
7624 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7626 it->what = IT_EOB;
7627 return 0;
7629 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7631 /* Pad with spaces. */
7632 it->c = ' ', it->len = 1;
7633 CHARPOS (position) = BYTEPOS (position) = -1;
7635 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7636 IT_STRING_BYTEPOS (*it),
7637 it->bidi_it.scan_dir < 0
7638 ? -1
7639 : it->string_nchars)
7640 && next_element_from_composition (it))
7642 return 1;
7644 else if (STRING_MULTIBYTE (it->string))
7646 const unsigned char *s = (SDATA (it->string)
7647 + IT_STRING_BYTEPOS (*it));
7648 it->c = string_char_and_length (s, &it->len);
7650 else
7652 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7653 it->len = 1;
7657 /* Record what we have and where it came from. */
7658 it->what = IT_CHARACTER;
7659 it->object = it->string;
7660 it->position = position;
7661 return 1;
7665 /* Load IT with next display element from C string IT->s.
7666 IT->string_nchars is the maximum number of characters to return
7667 from the string. IT->end_charpos may be greater than
7668 IT->string_nchars when this function is called, in which case we
7669 may have to return padding spaces. Value is zero if end of string
7670 reached, including padding spaces. */
7672 static int
7673 next_element_from_c_string (struct it *it)
7675 int success_p = 1;
7677 eassert (it->s);
7678 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7679 it->what = IT_CHARACTER;
7680 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7681 it->object = Qnil;
7683 /* With bidi reordering, the character to display might not be the
7684 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7685 we were reseated to a new string, whose paragraph direction is
7686 not known. */
7687 if (it->bidi_p && it->bidi_it.first_elt)
7688 get_visually_first_element (it);
7690 /* IT's position can be greater than IT->string_nchars in case a
7691 field width or precision has been specified when the iterator was
7692 initialized. */
7693 if (IT_CHARPOS (*it) >= it->end_charpos)
7695 /* End of the game. */
7696 it->what = IT_EOB;
7697 success_p = 0;
7699 else if (IT_CHARPOS (*it) >= it->string_nchars)
7701 /* Pad with spaces. */
7702 it->c = ' ', it->len = 1;
7703 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7705 else if (it->multibyte_p)
7706 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7707 else
7708 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7710 return success_p;
7714 /* Set up IT to return characters from an ellipsis, if appropriate.
7715 The definition of the ellipsis glyphs may come from a display table
7716 entry. This function fills IT with the first glyph from the
7717 ellipsis if an ellipsis is to be displayed. */
7719 static int
7720 next_element_from_ellipsis (struct it *it)
7722 if (it->selective_display_ellipsis_p)
7723 setup_for_ellipsis (it, it->len);
7724 else
7726 /* The face at the current position may be different from the
7727 face we find after the invisible text. Remember what it
7728 was in IT->saved_face_id, and signal that it's there by
7729 setting face_before_selective_p. */
7730 it->saved_face_id = it->face_id;
7731 it->method = GET_FROM_BUFFER;
7732 it->object = it->w->buffer;
7733 reseat_at_next_visible_line_start (it, 1);
7734 it->face_before_selective_p = 1;
7737 return GET_NEXT_DISPLAY_ELEMENT (it);
7741 /* Deliver an image display element. The iterator IT is already
7742 filled with image information (done in handle_display_prop). Value
7743 is always 1. */
7746 static int
7747 next_element_from_image (struct it *it)
7749 it->what = IT_IMAGE;
7750 it->ignore_overlay_strings_at_pos_p = 0;
7751 return 1;
7755 /* Fill iterator IT with next display element from a stretch glyph
7756 property. IT->object is the value of the text property. Value is
7757 always 1. */
7759 static int
7760 next_element_from_stretch (struct it *it)
7762 it->what = IT_STRETCH;
7763 return 1;
7766 /* Scan backwards from IT's current position until we find a stop
7767 position, or until BEGV. This is called when we find ourself
7768 before both the last known prev_stop and base_level_stop while
7769 reordering bidirectional text. */
7771 static void
7772 compute_stop_pos_backwards (struct it *it)
7774 const int SCAN_BACK_LIMIT = 1000;
7775 struct text_pos pos;
7776 struct display_pos save_current = it->current;
7777 struct text_pos save_position = it->position;
7778 ptrdiff_t charpos = IT_CHARPOS (*it);
7779 ptrdiff_t where_we_are = charpos;
7780 ptrdiff_t save_stop_pos = it->stop_charpos;
7781 ptrdiff_t save_end_pos = it->end_charpos;
7783 eassert (NILP (it->string) && !it->s);
7784 eassert (it->bidi_p);
7785 it->bidi_p = 0;
7788 it->end_charpos = min (charpos + 1, ZV);
7789 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7790 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7791 reseat_1 (it, pos, 0);
7792 compute_stop_pos (it);
7793 /* We must advance forward, right? */
7794 if (it->stop_charpos <= charpos)
7795 emacs_abort ();
7797 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7799 if (it->stop_charpos <= where_we_are)
7800 it->prev_stop = it->stop_charpos;
7801 else
7802 it->prev_stop = BEGV;
7803 it->bidi_p = 1;
7804 it->current = save_current;
7805 it->position = save_position;
7806 it->stop_charpos = save_stop_pos;
7807 it->end_charpos = save_end_pos;
7810 /* Scan forward from CHARPOS in the current buffer/string, until we
7811 find a stop position > current IT's position. Then handle the stop
7812 position before that. This is called when we bump into a stop
7813 position while reordering bidirectional text. CHARPOS should be
7814 the last previously processed stop_pos (or BEGV/0, if none were
7815 processed yet) whose position is less that IT's current
7816 position. */
7818 static void
7819 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7821 int bufp = !STRINGP (it->string);
7822 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7823 struct display_pos save_current = it->current;
7824 struct text_pos save_position = it->position;
7825 struct text_pos pos1;
7826 ptrdiff_t next_stop;
7828 /* Scan in strict logical order. */
7829 eassert (it->bidi_p);
7830 it->bidi_p = 0;
7833 it->prev_stop = charpos;
7834 if (bufp)
7836 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7837 reseat_1 (it, pos1, 0);
7839 else
7840 it->current.string_pos = string_pos (charpos, it->string);
7841 compute_stop_pos (it);
7842 /* We must advance forward, right? */
7843 if (it->stop_charpos <= it->prev_stop)
7844 emacs_abort ();
7845 charpos = it->stop_charpos;
7847 while (charpos <= where_we_are);
7849 it->bidi_p = 1;
7850 it->current = save_current;
7851 it->position = save_position;
7852 next_stop = it->stop_charpos;
7853 it->stop_charpos = it->prev_stop;
7854 handle_stop (it);
7855 it->stop_charpos = next_stop;
7858 /* Load IT with the next display element from current_buffer. Value
7859 is zero if end of buffer reached. IT->stop_charpos is the next
7860 position at which to stop and check for text properties or buffer
7861 end. */
7863 static int
7864 next_element_from_buffer (struct it *it)
7866 int success_p = 1;
7868 eassert (IT_CHARPOS (*it) >= BEGV);
7869 eassert (NILP (it->string) && !it->s);
7870 eassert (!it->bidi_p
7871 || (EQ (it->bidi_it.string.lstring, Qnil)
7872 && it->bidi_it.string.s == NULL));
7874 /* With bidi reordering, the character to display might not be the
7875 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7876 we were reseat()ed to a new buffer position, which is potentially
7877 a different paragraph. */
7878 if (it->bidi_p && it->bidi_it.first_elt)
7880 get_visually_first_element (it);
7881 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7884 if (IT_CHARPOS (*it) >= it->stop_charpos)
7886 if (IT_CHARPOS (*it) >= it->end_charpos)
7888 int overlay_strings_follow_p;
7890 /* End of the game, except when overlay strings follow that
7891 haven't been returned yet. */
7892 if (it->overlay_strings_at_end_processed_p)
7893 overlay_strings_follow_p = 0;
7894 else
7896 it->overlay_strings_at_end_processed_p = 1;
7897 overlay_strings_follow_p = get_overlay_strings (it, 0);
7900 if (overlay_strings_follow_p)
7901 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7902 else
7904 it->what = IT_EOB;
7905 it->position = it->current.pos;
7906 success_p = 0;
7909 else if (!(!it->bidi_p
7910 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7911 || IT_CHARPOS (*it) == it->stop_charpos))
7913 /* With bidi non-linear iteration, we could find ourselves
7914 far beyond the last computed stop_charpos, with several
7915 other stop positions in between that we missed. Scan
7916 them all now, in buffer's logical order, until we find
7917 and handle the last stop_charpos that precedes our
7918 current position. */
7919 handle_stop_backwards (it, it->stop_charpos);
7920 return GET_NEXT_DISPLAY_ELEMENT (it);
7922 else
7924 if (it->bidi_p)
7926 /* Take note of the stop position we just moved across,
7927 for when we will move back across it. */
7928 it->prev_stop = it->stop_charpos;
7929 /* If we are at base paragraph embedding level, take
7930 note of the last stop position seen at this
7931 level. */
7932 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7933 it->base_level_stop = it->stop_charpos;
7935 handle_stop (it);
7936 return GET_NEXT_DISPLAY_ELEMENT (it);
7939 else if (it->bidi_p
7940 /* If we are before prev_stop, we may have overstepped on
7941 our way backwards a stop_pos, and if so, we need to
7942 handle that stop_pos. */
7943 && IT_CHARPOS (*it) < it->prev_stop
7944 /* We can sometimes back up for reasons that have nothing
7945 to do with bidi reordering. E.g., compositions. The
7946 code below is only needed when we are above the base
7947 embedding level, so test for that explicitly. */
7948 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7950 if (it->base_level_stop <= 0
7951 || IT_CHARPOS (*it) < it->base_level_stop)
7953 /* If we lost track of base_level_stop, we need to find
7954 prev_stop by looking backwards. This happens, e.g., when
7955 we were reseated to the previous screenful of text by
7956 vertical-motion. */
7957 it->base_level_stop = BEGV;
7958 compute_stop_pos_backwards (it);
7959 handle_stop_backwards (it, it->prev_stop);
7961 else
7962 handle_stop_backwards (it, it->base_level_stop);
7963 return GET_NEXT_DISPLAY_ELEMENT (it);
7965 else
7967 /* No face changes, overlays etc. in sight, so just return a
7968 character from current_buffer. */
7969 unsigned char *p;
7970 ptrdiff_t stop;
7972 /* Maybe run the redisplay end trigger hook. Performance note:
7973 This doesn't seem to cost measurable time. */
7974 if (it->redisplay_end_trigger_charpos
7975 && it->glyph_row
7976 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7977 run_redisplay_end_trigger_hook (it);
7979 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7980 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7981 stop)
7982 && next_element_from_composition (it))
7984 return 1;
7987 /* Get the next character, maybe multibyte. */
7988 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7989 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7990 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7991 else
7992 it->c = *p, it->len = 1;
7994 /* Record what we have and where it came from. */
7995 it->what = IT_CHARACTER;
7996 it->object = it->w->buffer;
7997 it->position = it->current.pos;
7999 /* Normally we return the character found above, except when we
8000 really want to return an ellipsis for selective display. */
8001 if (it->selective)
8003 if (it->c == '\n')
8005 /* A value of selective > 0 means hide lines indented more
8006 than that number of columns. */
8007 if (it->selective > 0
8008 && IT_CHARPOS (*it) + 1 < ZV
8009 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8010 IT_BYTEPOS (*it) + 1,
8011 it->selective))
8013 success_p = next_element_from_ellipsis (it);
8014 it->dpvec_char_len = -1;
8017 else if (it->c == '\r' && it->selective == -1)
8019 /* A value of selective == -1 means that everything from the
8020 CR to the end of the line is invisible, with maybe an
8021 ellipsis displayed for it. */
8022 success_p = next_element_from_ellipsis (it);
8023 it->dpvec_char_len = -1;
8028 /* Value is zero if end of buffer reached. */
8029 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8030 return success_p;
8034 /* Run the redisplay end trigger hook for IT. */
8036 static void
8037 run_redisplay_end_trigger_hook (struct it *it)
8039 Lisp_Object args[3];
8041 /* IT->glyph_row should be non-null, i.e. we should be actually
8042 displaying something, or otherwise we should not run the hook. */
8043 eassert (it->glyph_row);
8045 /* Set up hook arguments. */
8046 args[0] = Qredisplay_end_trigger_functions;
8047 args[1] = it->window;
8048 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8049 it->redisplay_end_trigger_charpos = 0;
8051 /* Since we are *trying* to run these functions, don't try to run
8052 them again, even if they get an error. */
8053 wset_redisplay_end_trigger (it->w, Qnil);
8054 Frun_hook_with_args (3, args);
8056 /* Notice if it changed the face of the character we are on. */
8057 handle_face_prop (it);
8061 /* Deliver a composition display element. Unlike the other
8062 next_element_from_XXX, this function is not registered in the array
8063 get_next_element[]. It is called from next_element_from_buffer and
8064 next_element_from_string when necessary. */
8066 static int
8067 next_element_from_composition (struct it *it)
8069 it->what = IT_COMPOSITION;
8070 it->len = it->cmp_it.nbytes;
8071 if (STRINGP (it->string))
8073 if (it->c < 0)
8075 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8076 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8077 return 0;
8079 it->position = it->current.string_pos;
8080 it->object = it->string;
8081 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8082 IT_STRING_BYTEPOS (*it), it->string);
8084 else
8086 if (it->c < 0)
8088 IT_CHARPOS (*it) += it->cmp_it.nchars;
8089 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8090 if (it->bidi_p)
8092 if (it->bidi_it.new_paragraph)
8093 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8094 /* Resync the bidi iterator with IT's new position.
8095 FIXME: this doesn't support bidirectional text. */
8096 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8097 bidi_move_to_visually_next (&it->bidi_it);
8099 return 0;
8101 it->position = it->current.pos;
8102 it->object = it->w->buffer;
8103 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8104 IT_BYTEPOS (*it), Qnil);
8106 return 1;
8111 /***********************************************************************
8112 Moving an iterator without producing glyphs
8113 ***********************************************************************/
8115 /* Check if iterator is at a position corresponding to a valid buffer
8116 position after some move_it_ call. */
8118 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8119 ((it)->method == GET_FROM_STRING \
8120 ? IT_STRING_CHARPOS (*it) == 0 \
8121 : 1)
8124 /* Move iterator IT to a specified buffer or X position within one
8125 line on the display without producing glyphs.
8127 OP should be a bit mask including some or all of these bits:
8128 MOVE_TO_X: Stop upon reaching x-position TO_X.
8129 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8130 Regardless of OP's value, stop upon reaching the end of the display line.
8132 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8133 This means, in particular, that TO_X includes window's horizontal
8134 scroll amount.
8136 The return value has several possible values that
8137 say what condition caused the scan to stop:
8139 MOVE_POS_MATCH_OR_ZV
8140 - when TO_POS or ZV was reached.
8142 MOVE_X_REACHED
8143 -when TO_X was reached before TO_POS or ZV were reached.
8145 MOVE_LINE_CONTINUED
8146 - when we reached the end of the display area and the line must
8147 be continued.
8149 MOVE_LINE_TRUNCATED
8150 - when we reached the end of the display area and the line is
8151 truncated.
8153 MOVE_NEWLINE_OR_CR
8154 - when we stopped at a line end, i.e. a newline or a CR and selective
8155 display is on. */
8157 static enum move_it_result
8158 move_it_in_display_line_to (struct it *it,
8159 ptrdiff_t to_charpos, int to_x,
8160 enum move_operation_enum op)
8162 enum move_it_result result = MOVE_UNDEFINED;
8163 struct glyph_row *saved_glyph_row;
8164 struct it wrap_it, atpos_it, atx_it, ppos_it;
8165 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8166 void *ppos_data = NULL;
8167 int may_wrap = 0;
8168 enum it_method prev_method = it->method;
8169 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8170 int saw_smaller_pos = prev_pos < to_charpos;
8172 /* Don't produce glyphs in produce_glyphs. */
8173 saved_glyph_row = it->glyph_row;
8174 it->glyph_row = NULL;
8176 /* Use wrap_it to save a copy of IT wherever a word wrap could
8177 occur. Use atpos_it to save a copy of IT at the desired buffer
8178 position, if found, so that we can scan ahead and check if the
8179 word later overshoots the window edge. Use atx_it similarly, for
8180 pixel positions. */
8181 wrap_it.sp = -1;
8182 atpos_it.sp = -1;
8183 atx_it.sp = -1;
8185 /* Use ppos_it under bidi reordering to save a copy of IT for the
8186 position > CHARPOS that is the closest to CHARPOS. We restore
8187 that position in IT when we have scanned the entire display line
8188 without finding a match for CHARPOS and all the character
8189 positions are greater than CHARPOS. */
8190 if (it->bidi_p)
8192 SAVE_IT (ppos_it, *it, ppos_data);
8193 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8194 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8195 SAVE_IT (ppos_it, *it, ppos_data);
8198 #define BUFFER_POS_REACHED_P() \
8199 ((op & MOVE_TO_POS) != 0 \
8200 && BUFFERP (it->object) \
8201 && (IT_CHARPOS (*it) == to_charpos \
8202 || ((!it->bidi_p \
8203 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8204 && IT_CHARPOS (*it) > to_charpos) \
8205 || (it->what == IT_COMPOSITION \
8206 && ((IT_CHARPOS (*it) > to_charpos \
8207 && to_charpos >= it->cmp_it.charpos) \
8208 || (IT_CHARPOS (*it) < to_charpos \
8209 && to_charpos <= it->cmp_it.charpos)))) \
8210 && (it->method == GET_FROM_BUFFER \
8211 || (it->method == GET_FROM_DISPLAY_VECTOR \
8212 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8214 /* If there's a line-/wrap-prefix, handle it. */
8215 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8216 && it->current_y < it->last_visible_y)
8217 handle_line_prefix (it);
8219 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8220 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8222 while (1)
8224 int x, i, ascent = 0, descent = 0;
8226 /* Utility macro to reset an iterator with x, ascent, and descent. */
8227 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8228 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8229 (IT)->max_descent = descent)
8231 /* Stop if we move beyond TO_CHARPOS (after an image or a
8232 display string or stretch glyph). */
8233 if ((op & MOVE_TO_POS) != 0
8234 && BUFFERP (it->object)
8235 && it->method == GET_FROM_BUFFER
8236 && (((!it->bidi_p
8237 /* When the iterator is at base embedding level, we
8238 are guaranteed that characters are delivered for
8239 display in strictly increasing order of their
8240 buffer positions. */
8241 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8242 && IT_CHARPOS (*it) > to_charpos)
8243 || (it->bidi_p
8244 && (prev_method == GET_FROM_IMAGE
8245 || prev_method == GET_FROM_STRETCH
8246 || prev_method == GET_FROM_STRING)
8247 /* Passed TO_CHARPOS from left to right. */
8248 && ((prev_pos < to_charpos
8249 && IT_CHARPOS (*it) > to_charpos)
8250 /* Passed TO_CHARPOS from right to left. */
8251 || (prev_pos > to_charpos
8252 && IT_CHARPOS (*it) < to_charpos)))))
8254 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8256 result = MOVE_POS_MATCH_OR_ZV;
8257 break;
8259 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8260 /* If wrap_it is valid, the current position might be in a
8261 word that is wrapped. So, save the iterator in
8262 atpos_it and continue to see if wrapping happens. */
8263 SAVE_IT (atpos_it, *it, atpos_data);
8266 /* Stop when ZV reached.
8267 We used to stop here when TO_CHARPOS reached as well, but that is
8268 too soon if this glyph does not fit on this line. So we handle it
8269 explicitly below. */
8270 if (!get_next_display_element (it))
8272 result = MOVE_POS_MATCH_OR_ZV;
8273 break;
8276 if (it->line_wrap == TRUNCATE)
8278 if (BUFFER_POS_REACHED_P ())
8280 result = MOVE_POS_MATCH_OR_ZV;
8281 break;
8284 else
8286 if (it->line_wrap == WORD_WRAP)
8288 if (IT_DISPLAYING_WHITESPACE (it))
8289 may_wrap = 1;
8290 else if (may_wrap)
8292 /* We have reached a glyph that follows one or more
8293 whitespace characters. If the position is
8294 already found, we are done. */
8295 if (atpos_it.sp >= 0)
8297 RESTORE_IT (it, &atpos_it, atpos_data);
8298 result = MOVE_POS_MATCH_OR_ZV;
8299 goto done;
8301 if (atx_it.sp >= 0)
8303 RESTORE_IT (it, &atx_it, atx_data);
8304 result = MOVE_X_REACHED;
8305 goto done;
8307 /* Otherwise, we can wrap here. */
8308 SAVE_IT (wrap_it, *it, wrap_data);
8309 may_wrap = 0;
8314 /* Remember the line height for the current line, in case
8315 the next element doesn't fit on the line. */
8316 ascent = it->max_ascent;
8317 descent = it->max_descent;
8319 /* The call to produce_glyphs will get the metrics of the
8320 display element IT is loaded with. Record the x-position
8321 before this display element, in case it doesn't fit on the
8322 line. */
8323 x = it->current_x;
8325 PRODUCE_GLYPHS (it);
8327 if (it->area != TEXT_AREA)
8329 prev_method = it->method;
8330 if (it->method == GET_FROM_BUFFER)
8331 prev_pos = IT_CHARPOS (*it);
8332 set_iterator_to_next (it, 1);
8333 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8334 SET_TEXT_POS (this_line_min_pos,
8335 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8336 if (it->bidi_p
8337 && (op & MOVE_TO_POS)
8338 && IT_CHARPOS (*it) > to_charpos
8339 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8340 SAVE_IT (ppos_it, *it, ppos_data);
8341 continue;
8344 /* The number of glyphs we get back in IT->nglyphs will normally
8345 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8346 character on a terminal frame, or (iii) a line end. For the
8347 second case, IT->nglyphs - 1 padding glyphs will be present.
8348 (On X frames, there is only one glyph produced for a
8349 composite character.)
8351 The behavior implemented below means, for continuation lines,
8352 that as many spaces of a TAB as fit on the current line are
8353 displayed there. For terminal frames, as many glyphs of a
8354 multi-glyph character are displayed in the current line, too.
8355 This is what the old redisplay code did, and we keep it that
8356 way. Under X, the whole shape of a complex character must
8357 fit on the line or it will be completely displayed in the
8358 next line.
8360 Note that both for tabs and padding glyphs, all glyphs have
8361 the same width. */
8362 if (it->nglyphs)
8364 /* More than one glyph or glyph doesn't fit on line. All
8365 glyphs have the same width. */
8366 int single_glyph_width = it->pixel_width / it->nglyphs;
8367 int new_x;
8368 int x_before_this_char = x;
8369 int hpos_before_this_char = it->hpos;
8371 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8373 new_x = x + single_glyph_width;
8375 /* We want to leave anything reaching TO_X to the caller. */
8376 if ((op & MOVE_TO_X) && new_x > to_x)
8378 if (BUFFER_POS_REACHED_P ())
8380 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8381 goto buffer_pos_reached;
8382 if (atpos_it.sp < 0)
8384 SAVE_IT (atpos_it, *it, atpos_data);
8385 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8388 else
8390 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8392 it->current_x = x;
8393 result = MOVE_X_REACHED;
8394 break;
8396 if (atx_it.sp < 0)
8398 SAVE_IT (atx_it, *it, atx_data);
8399 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8404 if (/* Lines are continued. */
8405 it->line_wrap != TRUNCATE
8406 && (/* And glyph doesn't fit on the line. */
8407 new_x > it->last_visible_x
8408 /* Or it fits exactly and we're on a window
8409 system frame. */
8410 || (new_x == it->last_visible_x
8411 && FRAME_WINDOW_P (it->f)
8412 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8413 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8414 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8416 if (/* IT->hpos == 0 means the very first glyph
8417 doesn't fit on the line, e.g. a wide image. */
8418 it->hpos == 0
8419 || (new_x == it->last_visible_x
8420 && FRAME_WINDOW_P (it->f)))
8422 ++it->hpos;
8423 it->current_x = new_x;
8425 /* The character's last glyph just barely fits
8426 in this row. */
8427 if (i == it->nglyphs - 1)
8429 /* If this is the destination position,
8430 return a position *before* it in this row,
8431 now that we know it fits in this row. */
8432 if (BUFFER_POS_REACHED_P ())
8434 if (it->line_wrap != WORD_WRAP
8435 || wrap_it.sp < 0)
8437 it->hpos = hpos_before_this_char;
8438 it->current_x = x_before_this_char;
8439 result = MOVE_POS_MATCH_OR_ZV;
8440 break;
8442 if (it->line_wrap == WORD_WRAP
8443 && atpos_it.sp < 0)
8445 SAVE_IT (atpos_it, *it, atpos_data);
8446 atpos_it.current_x = x_before_this_char;
8447 atpos_it.hpos = hpos_before_this_char;
8451 prev_method = it->method;
8452 if (it->method == GET_FROM_BUFFER)
8453 prev_pos = IT_CHARPOS (*it);
8454 set_iterator_to_next (it, 1);
8455 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8456 SET_TEXT_POS (this_line_min_pos,
8457 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8458 /* On graphical terminals, newlines may
8459 "overflow" into the fringe if
8460 overflow-newline-into-fringe is non-nil.
8461 On text terminals, and on graphical
8462 terminals with no right margin, newlines
8463 may overflow into the last glyph on the
8464 display line.*/
8465 if (!FRAME_WINDOW_P (it->f)
8466 || ((it->bidi_p
8467 && it->bidi_it.paragraph_dir == R2L)
8468 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8469 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8470 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8472 if (!get_next_display_element (it))
8474 result = MOVE_POS_MATCH_OR_ZV;
8475 break;
8477 if (BUFFER_POS_REACHED_P ())
8479 if (ITERATOR_AT_END_OF_LINE_P (it))
8480 result = MOVE_POS_MATCH_OR_ZV;
8481 else
8482 result = MOVE_LINE_CONTINUED;
8483 break;
8485 if (ITERATOR_AT_END_OF_LINE_P (it))
8487 result = MOVE_NEWLINE_OR_CR;
8488 break;
8493 else
8494 IT_RESET_X_ASCENT_DESCENT (it);
8496 if (wrap_it.sp >= 0)
8498 RESTORE_IT (it, &wrap_it, wrap_data);
8499 atpos_it.sp = -1;
8500 atx_it.sp = -1;
8503 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8504 IT_CHARPOS (*it)));
8505 result = MOVE_LINE_CONTINUED;
8506 break;
8509 if (BUFFER_POS_REACHED_P ())
8511 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8512 goto buffer_pos_reached;
8513 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8515 SAVE_IT (atpos_it, *it, atpos_data);
8516 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8520 if (new_x > it->first_visible_x)
8522 /* Glyph is visible. Increment number of glyphs that
8523 would be displayed. */
8524 ++it->hpos;
8528 if (result != MOVE_UNDEFINED)
8529 break;
8531 else if (BUFFER_POS_REACHED_P ())
8533 buffer_pos_reached:
8534 IT_RESET_X_ASCENT_DESCENT (it);
8535 result = MOVE_POS_MATCH_OR_ZV;
8536 break;
8538 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8540 /* Stop when TO_X specified and reached. This check is
8541 necessary here because of lines consisting of a line end,
8542 only. The line end will not produce any glyphs and we
8543 would never get MOVE_X_REACHED. */
8544 eassert (it->nglyphs == 0);
8545 result = MOVE_X_REACHED;
8546 break;
8549 /* Is this a line end? If yes, we're done. */
8550 if (ITERATOR_AT_END_OF_LINE_P (it))
8552 /* If we are past TO_CHARPOS, but never saw any character
8553 positions smaller than TO_CHARPOS, return
8554 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8555 did. */
8556 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8558 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8560 if (IT_CHARPOS (ppos_it) < ZV)
8562 RESTORE_IT (it, &ppos_it, ppos_data);
8563 result = MOVE_POS_MATCH_OR_ZV;
8565 else
8566 goto buffer_pos_reached;
8568 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8569 && IT_CHARPOS (*it) > to_charpos)
8570 goto buffer_pos_reached;
8571 else
8572 result = MOVE_NEWLINE_OR_CR;
8574 else
8575 result = MOVE_NEWLINE_OR_CR;
8576 break;
8579 prev_method = it->method;
8580 if (it->method == GET_FROM_BUFFER)
8581 prev_pos = IT_CHARPOS (*it);
8582 /* The current display element has been consumed. Advance
8583 to the next. */
8584 set_iterator_to_next (it, 1);
8585 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8586 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8587 if (IT_CHARPOS (*it) < to_charpos)
8588 saw_smaller_pos = 1;
8589 if (it->bidi_p
8590 && (op & MOVE_TO_POS)
8591 && IT_CHARPOS (*it) >= to_charpos
8592 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8593 SAVE_IT (ppos_it, *it, ppos_data);
8595 /* Stop if lines are truncated and IT's current x-position is
8596 past the right edge of the window now. */
8597 if (it->line_wrap == TRUNCATE
8598 && it->current_x >= it->last_visible_x)
8600 if (!FRAME_WINDOW_P (it->f)
8601 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8602 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8603 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8604 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8606 int at_eob_p = 0;
8608 if ((at_eob_p = !get_next_display_element (it))
8609 || BUFFER_POS_REACHED_P ()
8610 /* If we are past TO_CHARPOS, but never saw any
8611 character positions smaller than TO_CHARPOS,
8612 return MOVE_POS_MATCH_OR_ZV, like the
8613 unidirectional display did. */
8614 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8615 && !saw_smaller_pos
8616 && IT_CHARPOS (*it) > to_charpos))
8618 if (it->bidi_p
8619 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8620 RESTORE_IT (it, &ppos_it, ppos_data);
8621 result = MOVE_POS_MATCH_OR_ZV;
8622 break;
8624 if (ITERATOR_AT_END_OF_LINE_P (it))
8626 result = MOVE_NEWLINE_OR_CR;
8627 break;
8630 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8631 && !saw_smaller_pos
8632 && IT_CHARPOS (*it) > to_charpos)
8634 if (IT_CHARPOS (ppos_it) < ZV)
8635 RESTORE_IT (it, &ppos_it, ppos_data);
8636 result = MOVE_POS_MATCH_OR_ZV;
8637 break;
8639 result = MOVE_LINE_TRUNCATED;
8640 break;
8642 #undef IT_RESET_X_ASCENT_DESCENT
8645 #undef BUFFER_POS_REACHED_P
8647 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8648 restore the saved iterator. */
8649 if (atpos_it.sp >= 0)
8650 RESTORE_IT (it, &atpos_it, atpos_data);
8651 else if (atx_it.sp >= 0)
8652 RESTORE_IT (it, &atx_it, atx_data);
8654 done:
8656 if (atpos_data)
8657 bidi_unshelve_cache (atpos_data, 1);
8658 if (atx_data)
8659 bidi_unshelve_cache (atx_data, 1);
8660 if (wrap_data)
8661 bidi_unshelve_cache (wrap_data, 1);
8662 if (ppos_data)
8663 bidi_unshelve_cache (ppos_data, 1);
8665 /* Restore the iterator settings altered at the beginning of this
8666 function. */
8667 it->glyph_row = saved_glyph_row;
8668 return result;
8671 /* For external use. */
8672 void
8673 move_it_in_display_line (struct it *it,
8674 ptrdiff_t to_charpos, int to_x,
8675 enum move_operation_enum op)
8677 if (it->line_wrap == WORD_WRAP
8678 && (op & MOVE_TO_X))
8680 struct it save_it;
8681 void *save_data = NULL;
8682 int skip;
8684 SAVE_IT (save_it, *it, save_data);
8685 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8686 /* When word-wrap is on, TO_X may lie past the end
8687 of a wrapped line. Then it->current is the
8688 character on the next line, so backtrack to the
8689 space before the wrap point. */
8690 if (skip == MOVE_LINE_CONTINUED)
8692 int prev_x = max (it->current_x - 1, 0);
8693 RESTORE_IT (it, &save_it, save_data);
8694 move_it_in_display_line_to
8695 (it, -1, prev_x, MOVE_TO_X);
8697 else
8698 bidi_unshelve_cache (save_data, 1);
8700 else
8701 move_it_in_display_line_to (it, to_charpos, to_x, op);
8705 /* Move IT forward until it satisfies one or more of the criteria in
8706 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8708 OP is a bit-mask that specifies where to stop, and in particular,
8709 which of those four position arguments makes a difference. See the
8710 description of enum move_operation_enum.
8712 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8713 screen line, this function will set IT to the next position that is
8714 displayed to the right of TO_CHARPOS on the screen. */
8716 void
8717 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8719 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8720 int line_height, line_start_x = 0, reached = 0;
8721 void *backup_data = NULL;
8723 for (;;)
8725 if (op & MOVE_TO_VPOS)
8727 /* If no TO_CHARPOS and no TO_X specified, stop at the
8728 start of the line TO_VPOS. */
8729 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8731 if (it->vpos == to_vpos)
8733 reached = 1;
8734 break;
8736 else
8737 skip = move_it_in_display_line_to (it, -1, -1, 0);
8739 else
8741 /* TO_VPOS >= 0 means stop at TO_X in the line at
8742 TO_VPOS, or at TO_POS, whichever comes first. */
8743 if (it->vpos == to_vpos)
8745 reached = 2;
8746 break;
8749 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8751 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8753 reached = 3;
8754 break;
8756 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8758 /* We have reached TO_X but not in the line we want. */
8759 skip = move_it_in_display_line_to (it, to_charpos,
8760 -1, MOVE_TO_POS);
8761 if (skip == MOVE_POS_MATCH_OR_ZV)
8763 reached = 4;
8764 break;
8769 else if (op & MOVE_TO_Y)
8771 struct it it_backup;
8773 if (it->line_wrap == WORD_WRAP)
8774 SAVE_IT (it_backup, *it, backup_data);
8776 /* TO_Y specified means stop at TO_X in the line containing
8777 TO_Y---or at TO_CHARPOS if this is reached first. The
8778 problem is that we can't really tell whether the line
8779 contains TO_Y before we have completely scanned it, and
8780 this may skip past TO_X. What we do is to first scan to
8781 TO_X.
8783 If TO_X is not specified, use a TO_X of zero. The reason
8784 is to make the outcome of this function more predictable.
8785 If we didn't use TO_X == 0, we would stop at the end of
8786 the line which is probably not what a caller would expect
8787 to happen. */
8788 skip = move_it_in_display_line_to
8789 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8790 (MOVE_TO_X | (op & MOVE_TO_POS)));
8792 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8793 if (skip == MOVE_POS_MATCH_OR_ZV)
8794 reached = 5;
8795 else if (skip == MOVE_X_REACHED)
8797 /* If TO_X was reached, we want to know whether TO_Y is
8798 in the line. We know this is the case if the already
8799 scanned glyphs make the line tall enough. Otherwise,
8800 we must check by scanning the rest of the line. */
8801 line_height = it->max_ascent + it->max_descent;
8802 if (to_y >= it->current_y
8803 && to_y < it->current_y + line_height)
8805 reached = 6;
8806 break;
8808 SAVE_IT (it_backup, *it, backup_data);
8809 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8810 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8811 op & MOVE_TO_POS);
8812 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8813 line_height = it->max_ascent + it->max_descent;
8814 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8816 if (to_y >= it->current_y
8817 && to_y < it->current_y + line_height)
8819 /* If TO_Y is in this line and TO_X was reached
8820 above, we scanned too far. We have to restore
8821 IT's settings to the ones before skipping. But
8822 keep the more accurate values of max_ascent and
8823 max_descent we've found while skipping the rest
8824 of the line, for the sake of callers, such as
8825 pos_visible_p, that need to know the line
8826 height. */
8827 int max_ascent = it->max_ascent;
8828 int max_descent = it->max_descent;
8830 RESTORE_IT (it, &it_backup, backup_data);
8831 it->max_ascent = max_ascent;
8832 it->max_descent = max_descent;
8833 reached = 6;
8835 else
8837 skip = skip2;
8838 if (skip == MOVE_POS_MATCH_OR_ZV)
8839 reached = 7;
8842 else
8844 /* Check whether TO_Y is in this line. */
8845 line_height = it->max_ascent + it->max_descent;
8846 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8848 if (to_y >= it->current_y
8849 && to_y < it->current_y + line_height)
8851 /* When word-wrap is on, TO_X may lie past the end
8852 of a wrapped line. Then it->current is the
8853 character on the next line, so backtrack to the
8854 space before the wrap point. */
8855 if (skip == MOVE_LINE_CONTINUED
8856 && it->line_wrap == WORD_WRAP)
8858 int prev_x = max (it->current_x - 1, 0);
8859 RESTORE_IT (it, &it_backup, backup_data);
8860 skip = move_it_in_display_line_to
8861 (it, -1, prev_x, MOVE_TO_X);
8863 reached = 6;
8867 if (reached)
8868 break;
8870 else if (BUFFERP (it->object)
8871 && (it->method == GET_FROM_BUFFER
8872 || it->method == GET_FROM_STRETCH)
8873 && IT_CHARPOS (*it) >= to_charpos
8874 /* Under bidi iteration, a call to set_iterator_to_next
8875 can scan far beyond to_charpos if the initial
8876 portion of the next line needs to be reordered. In
8877 that case, give move_it_in_display_line_to another
8878 chance below. */
8879 && !(it->bidi_p
8880 && it->bidi_it.scan_dir == -1))
8881 skip = MOVE_POS_MATCH_OR_ZV;
8882 else
8883 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8885 switch (skip)
8887 case MOVE_POS_MATCH_OR_ZV:
8888 reached = 8;
8889 goto out;
8891 case MOVE_NEWLINE_OR_CR:
8892 set_iterator_to_next (it, 1);
8893 it->continuation_lines_width = 0;
8894 break;
8896 case MOVE_LINE_TRUNCATED:
8897 it->continuation_lines_width = 0;
8898 reseat_at_next_visible_line_start (it, 0);
8899 if ((op & MOVE_TO_POS) != 0
8900 && IT_CHARPOS (*it) > to_charpos)
8902 reached = 9;
8903 goto out;
8905 break;
8907 case MOVE_LINE_CONTINUED:
8908 /* For continued lines ending in a tab, some of the glyphs
8909 associated with the tab are displayed on the current
8910 line. Since it->current_x does not include these glyphs,
8911 we use it->last_visible_x instead. */
8912 if (it->c == '\t')
8914 it->continuation_lines_width += it->last_visible_x;
8915 /* When moving by vpos, ensure that the iterator really
8916 advances to the next line (bug#847, bug#969). Fixme:
8917 do we need to do this in other circumstances? */
8918 if (it->current_x != it->last_visible_x
8919 && (op & MOVE_TO_VPOS)
8920 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8922 line_start_x = it->current_x + it->pixel_width
8923 - it->last_visible_x;
8924 set_iterator_to_next (it, 0);
8927 else
8928 it->continuation_lines_width += it->current_x;
8929 break;
8931 default:
8932 emacs_abort ();
8935 /* Reset/increment for the next run. */
8936 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8937 it->current_x = line_start_x;
8938 line_start_x = 0;
8939 it->hpos = 0;
8940 it->current_y += it->max_ascent + it->max_descent;
8941 ++it->vpos;
8942 last_height = it->max_ascent + it->max_descent;
8943 last_max_ascent = it->max_ascent;
8944 it->max_ascent = it->max_descent = 0;
8947 out:
8949 /* On text terminals, we may stop at the end of a line in the middle
8950 of a multi-character glyph. If the glyph itself is continued,
8951 i.e. it is actually displayed on the next line, don't treat this
8952 stopping point as valid; move to the next line instead (unless
8953 that brings us offscreen). */
8954 if (!FRAME_WINDOW_P (it->f)
8955 && op & MOVE_TO_POS
8956 && IT_CHARPOS (*it) == to_charpos
8957 && it->what == IT_CHARACTER
8958 && it->nglyphs > 1
8959 && it->line_wrap == WINDOW_WRAP
8960 && it->current_x == it->last_visible_x - 1
8961 && it->c != '\n'
8962 && it->c != '\t'
8963 && it->vpos < XFASTINT (it->w->window_end_vpos))
8965 it->continuation_lines_width += it->current_x;
8966 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8967 it->current_y += it->max_ascent + it->max_descent;
8968 ++it->vpos;
8969 last_height = it->max_ascent + it->max_descent;
8970 last_max_ascent = it->max_ascent;
8973 if (backup_data)
8974 bidi_unshelve_cache (backup_data, 1);
8976 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8980 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8982 If DY > 0, move IT backward at least that many pixels. DY = 0
8983 means move IT backward to the preceding line start or BEGV. This
8984 function may move over more than DY pixels if IT->current_y - DY
8985 ends up in the middle of a line; in this case IT->current_y will be
8986 set to the top of the line moved to. */
8988 void
8989 move_it_vertically_backward (struct it *it, int dy)
8991 int nlines, h;
8992 struct it it2, it3;
8993 void *it2data = NULL, *it3data = NULL;
8994 ptrdiff_t start_pos;
8996 move_further_back:
8997 eassert (dy >= 0);
8999 start_pos = IT_CHARPOS (*it);
9001 /* Estimate how many newlines we must move back. */
9002 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9004 /* Set the iterator's position that many lines back. */
9005 while (nlines-- && IT_CHARPOS (*it) > BEGV)
9006 back_to_previous_visible_line_start (it);
9008 /* Reseat the iterator here. When moving backward, we don't want
9009 reseat to skip forward over invisible text, set up the iterator
9010 to deliver from overlay strings at the new position etc. So,
9011 use reseat_1 here. */
9012 reseat_1 (it, it->current.pos, 1);
9014 /* We are now surely at a line start. */
9015 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9016 reordering is in effect. */
9017 it->continuation_lines_width = 0;
9019 /* Move forward and see what y-distance we moved. First move to the
9020 start of the next line so that we get its height. We need this
9021 height to be able to tell whether we reached the specified
9022 y-distance. */
9023 SAVE_IT (it2, *it, it2data);
9024 it2.max_ascent = it2.max_descent = 0;
9027 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9028 MOVE_TO_POS | MOVE_TO_VPOS);
9030 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9031 /* If we are in a display string which starts at START_POS,
9032 and that display string includes a newline, and we are
9033 right after that newline (i.e. at the beginning of a
9034 display line), exit the loop, because otherwise we will
9035 infloop, since move_it_to will see that it is already at
9036 START_POS and will not move. */
9037 || (it2.method == GET_FROM_STRING
9038 && IT_CHARPOS (it2) == start_pos
9039 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9040 eassert (IT_CHARPOS (*it) >= BEGV);
9041 SAVE_IT (it3, it2, it3data);
9043 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9044 eassert (IT_CHARPOS (*it) >= BEGV);
9045 /* H is the actual vertical distance from the position in *IT
9046 and the starting position. */
9047 h = it2.current_y - it->current_y;
9048 /* NLINES is the distance in number of lines. */
9049 nlines = it2.vpos - it->vpos;
9051 /* Correct IT's y and vpos position
9052 so that they are relative to the starting point. */
9053 it->vpos -= nlines;
9054 it->current_y -= h;
9056 if (dy == 0)
9058 /* DY == 0 means move to the start of the screen line. The
9059 value of nlines is > 0 if continuation lines were involved,
9060 or if the original IT position was at start of a line. */
9061 RESTORE_IT (it, it, it2data);
9062 if (nlines > 0)
9063 move_it_by_lines (it, nlines);
9064 /* The above code moves us to some position NLINES down,
9065 usually to its first glyph (leftmost in an L2R line), but
9066 that's not necessarily the start of the line, under bidi
9067 reordering. We want to get to the character position
9068 that is immediately after the newline of the previous
9069 line. */
9070 if (it->bidi_p
9071 && !it->continuation_lines_width
9072 && !STRINGP (it->string)
9073 && IT_CHARPOS (*it) > BEGV
9074 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9076 ptrdiff_t nl_pos =
9077 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9079 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9081 bidi_unshelve_cache (it3data, 1);
9083 else
9085 /* The y-position we try to reach, relative to *IT.
9086 Note that H has been subtracted in front of the if-statement. */
9087 int target_y = it->current_y + h - dy;
9088 int y0 = it3.current_y;
9089 int y1;
9090 int line_height;
9092 RESTORE_IT (&it3, &it3, it3data);
9093 y1 = line_bottom_y (&it3);
9094 line_height = y1 - y0;
9095 RESTORE_IT (it, it, it2data);
9096 /* If we did not reach target_y, try to move further backward if
9097 we can. If we moved too far backward, try to move forward. */
9098 if (target_y < it->current_y
9099 /* This is heuristic. In a window that's 3 lines high, with
9100 a line height of 13 pixels each, recentering with point
9101 on the bottom line will try to move -39/2 = 19 pixels
9102 backward. Try to avoid moving into the first line. */
9103 && (it->current_y - target_y
9104 > min (window_box_height (it->w), line_height * 2 / 3))
9105 && IT_CHARPOS (*it) > BEGV)
9107 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9108 target_y - it->current_y));
9109 dy = it->current_y - target_y;
9110 goto move_further_back;
9112 else if (target_y >= it->current_y + line_height
9113 && IT_CHARPOS (*it) < ZV)
9115 /* Should move forward by at least one line, maybe more.
9117 Note: Calling move_it_by_lines can be expensive on
9118 terminal frames, where compute_motion is used (via
9119 vmotion) to do the job, when there are very long lines
9120 and truncate-lines is nil. That's the reason for
9121 treating terminal frames specially here. */
9123 if (!FRAME_WINDOW_P (it->f))
9124 move_it_vertically (it, target_y - (it->current_y + line_height));
9125 else
9129 move_it_by_lines (it, 1);
9131 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9138 /* Move IT by a specified amount of pixel lines DY. DY negative means
9139 move backwards. DY = 0 means move to start of screen line. At the
9140 end, IT will be on the start of a screen line. */
9142 void
9143 move_it_vertically (struct it *it, int dy)
9145 if (dy <= 0)
9146 move_it_vertically_backward (it, -dy);
9147 else
9149 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9150 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9151 MOVE_TO_POS | MOVE_TO_Y);
9152 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9154 /* If buffer ends in ZV without a newline, move to the start of
9155 the line to satisfy the post-condition. */
9156 if (IT_CHARPOS (*it) == ZV
9157 && ZV > BEGV
9158 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9159 move_it_by_lines (it, 0);
9164 /* Move iterator IT past the end of the text line it is in. */
9166 void
9167 move_it_past_eol (struct it *it)
9169 enum move_it_result rc;
9171 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9172 if (rc == MOVE_NEWLINE_OR_CR)
9173 set_iterator_to_next (it, 0);
9177 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9178 negative means move up. DVPOS == 0 means move to the start of the
9179 screen line.
9181 Optimization idea: If we would know that IT->f doesn't use
9182 a face with proportional font, we could be faster for
9183 truncate-lines nil. */
9185 void
9186 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9189 /* The commented-out optimization uses vmotion on terminals. This
9190 gives bad results, because elements like it->what, on which
9191 callers such as pos_visible_p rely, aren't updated. */
9192 /* struct position pos;
9193 if (!FRAME_WINDOW_P (it->f))
9195 struct text_pos textpos;
9197 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9198 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9199 reseat (it, textpos, 1);
9200 it->vpos += pos.vpos;
9201 it->current_y += pos.vpos;
9203 else */
9205 if (dvpos == 0)
9207 /* DVPOS == 0 means move to the start of the screen line. */
9208 move_it_vertically_backward (it, 0);
9209 /* Let next call to line_bottom_y calculate real line height */
9210 last_height = 0;
9212 else if (dvpos > 0)
9214 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9215 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9217 /* Only move to the next buffer position if we ended up in a
9218 string from display property, not in an overlay string
9219 (before-string or after-string). That is because the
9220 latter don't conceal the underlying buffer position, so
9221 we can ask to move the iterator to the exact position we
9222 are interested in. Note that, even if we are already at
9223 IT_CHARPOS (*it), the call below is not a no-op, as it
9224 will detect that we are at the end of the string, pop the
9225 iterator, and compute it->current_x and it->hpos
9226 correctly. */
9227 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9228 -1, -1, -1, MOVE_TO_POS);
9231 else
9233 struct it it2;
9234 void *it2data = NULL;
9235 ptrdiff_t start_charpos, i;
9237 /* Start at the beginning of the screen line containing IT's
9238 position. This may actually move vertically backwards,
9239 in case of overlays, so adjust dvpos accordingly. */
9240 dvpos += it->vpos;
9241 move_it_vertically_backward (it, 0);
9242 dvpos -= it->vpos;
9244 /* Go back -DVPOS visible lines and reseat the iterator there. */
9245 start_charpos = IT_CHARPOS (*it);
9246 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9247 back_to_previous_visible_line_start (it);
9248 reseat (it, it->current.pos, 1);
9250 /* Move further back if we end up in a string or an image. */
9251 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9253 /* First try to move to start of display line. */
9254 dvpos += it->vpos;
9255 move_it_vertically_backward (it, 0);
9256 dvpos -= it->vpos;
9257 if (IT_POS_VALID_AFTER_MOVE_P (it))
9258 break;
9259 /* If start of line is still in string or image,
9260 move further back. */
9261 back_to_previous_visible_line_start (it);
9262 reseat (it, it->current.pos, 1);
9263 dvpos--;
9266 it->current_x = it->hpos = 0;
9268 /* Above call may have moved too far if continuation lines
9269 are involved. Scan forward and see if it did. */
9270 SAVE_IT (it2, *it, it2data);
9271 it2.vpos = it2.current_y = 0;
9272 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9273 it->vpos -= it2.vpos;
9274 it->current_y -= it2.current_y;
9275 it->current_x = it->hpos = 0;
9277 /* If we moved too far back, move IT some lines forward. */
9278 if (it2.vpos > -dvpos)
9280 int delta = it2.vpos + dvpos;
9282 RESTORE_IT (&it2, &it2, it2data);
9283 SAVE_IT (it2, *it, it2data);
9284 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9285 /* Move back again if we got too far ahead. */
9286 if (IT_CHARPOS (*it) >= start_charpos)
9287 RESTORE_IT (it, &it2, it2data);
9288 else
9289 bidi_unshelve_cache (it2data, 1);
9291 else
9292 RESTORE_IT (it, it, it2data);
9296 /* Return 1 if IT points into the middle of a display vector. */
9299 in_display_vector_p (struct it *it)
9301 return (it->method == GET_FROM_DISPLAY_VECTOR
9302 && it->current.dpvec_index > 0
9303 && it->dpvec + it->current.dpvec_index != it->dpend);
9307 /***********************************************************************
9308 Messages
9309 ***********************************************************************/
9312 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9313 to *Messages*. */
9315 void
9316 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9318 Lisp_Object args[3];
9319 Lisp_Object msg, fmt;
9320 char *buffer;
9321 ptrdiff_t len;
9322 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9323 USE_SAFE_ALLOCA;
9325 fmt = msg = Qnil;
9326 GCPRO4 (fmt, msg, arg1, arg2);
9328 args[0] = fmt = build_string (format);
9329 args[1] = arg1;
9330 args[2] = arg2;
9331 msg = Fformat (3, args);
9333 len = SBYTES (msg) + 1;
9334 buffer = SAFE_ALLOCA (len);
9335 memcpy (buffer, SDATA (msg), len);
9337 message_dolog (buffer, len - 1, 1, 0);
9338 SAFE_FREE ();
9340 UNGCPRO;
9344 /* Output a newline in the *Messages* buffer if "needs" one. */
9346 void
9347 message_log_maybe_newline (void)
9349 if (message_log_need_newline)
9350 message_dolog ("", 0, 1, 0);
9354 /* Add a string M of length NBYTES to the message log, optionally
9355 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9356 nonzero, means interpret the contents of M as multibyte. This
9357 function calls low-level routines in order to bypass text property
9358 hooks, etc. which might not be safe to run.
9360 This may GC (insert may run before/after change hooks),
9361 so the buffer M must NOT point to a Lisp string. */
9363 void
9364 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9366 const unsigned char *msg = (const unsigned char *) m;
9368 if (!NILP (Vmemory_full))
9369 return;
9371 if (!NILP (Vmessage_log_max))
9373 struct buffer *oldbuf;
9374 Lisp_Object oldpoint, oldbegv, oldzv;
9375 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9376 ptrdiff_t point_at_end = 0;
9377 ptrdiff_t zv_at_end = 0;
9378 Lisp_Object old_deactivate_mark;
9379 bool shown;
9380 struct gcpro gcpro1;
9382 old_deactivate_mark = Vdeactivate_mark;
9383 oldbuf = current_buffer;
9384 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9385 bset_undo_list (current_buffer, Qt);
9387 oldpoint = message_dolog_marker1;
9388 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9389 oldbegv = message_dolog_marker2;
9390 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9391 oldzv = message_dolog_marker3;
9392 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9393 GCPRO1 (old_deactivate_mark);
9395 if (PT == Z)
9396 point_at_end = 1;
9397 if (ZV == Z)
9398 zv_at_end = 1;
9400 BEGV = BEG;
9401 BEGV_BYTE = BEG_BYTE;
9402 ZV = Z;
9403 ZV_BYTE = Z_BYTE;
9404 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9406 /* Insert the string--maybe converting multibyte to single byte
9407 or vice versa, so that all the text fits the buffer. */
9408 if (multibyte
9409 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9411 ptrdiff_t i;
9412 int c, char_bytes;
9413 char work[1];
9415 /* Convert a multibyte string to single-byte
9416 for the *Message* buffer. */
9417 for (i = 0; i < nbytes; i += char_bytes)
9419 c = string_char_and_length (msg + i, &char_bytes);
9420 work[0] = (ASCII_CHAR_P (c)
9422 : multibyte_char_to_unibyte (c));
9423 insert_1_both (work, 1, 1, 1, 0, 0);
9426 else if (! multibyte
9427 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9429 ptrdiff_t i;
9430 int c, char_bytes;
9431 unsigned char str[MAX_MULTIBYTE_LENGTH];
9432 /* Convert a single-byte string to multibyte
9433 for the *Message* buffer. */
9434 for (i = 0; i < nbytes; i++)
9436 c = msg[i];
9437 MAKE_CHAR_MULTIBYTE (c);
9438 char_bytes = CHAR_STRING (c, str);
9439 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9442 else if (nbytes)
9443 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9445 if (nlflag)
9447 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9448 printmax_t dups;
9450 insert_1_both ("\n", 1, 1, 1, 0, 0);
9452 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9453 this_bol = PT;
9454 this_bol_byte = PT_BYTE;
9456 /* See if this line duplicates the previous one.
9457 If so, combine duplicates. */
9458 if (this_bol > BEG)
9460 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9461 prev_bol = PT;
9462 prev_bol_byte = PT_BYTE;
9464 dups = message_log_check_duplicate (prev_bol_byte,
9465 this_bol_byte);
9466 if (dups)
9468 del_range_both (prev_bol, prev_bol_byte,
9469 this_bol, this_bol_byte, 0);
9470 if (dups > 1)
9472 char dupstr[sizeof " [ times]"
9473 + INT_STRLEN_BOUND (printmax_t)];
9475 /* If you change this format, don't forget to also
9476 change message_log_check_duplicate. */
9477 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9478 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9479 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9484 /* If we have more than the desired maximum number of lines
9485 in the *Messages* buffer now, delete the oldest ones.
9486 This is safe because we don't have undo in this buffer. */
9488 if (NATNUMP (Vmessage_log_max))
9490 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9491 -XFASTINT (Vmessage_log_max) - 1, 0);
9492 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9495 BEGV = marker_position (oldbegv);
9496 BEGV_BYTE = marker_byte_position (oldbegv);
9498 if (zv_at_end)
9500 ZV = Z;
9501 ZV_BYTE = Z_BYTE;
9503 else
9505 ZV = marker_position (oldzv);
9506 ZV_BYTE = marker_byte_position (oldzv);
9509 if (point_at_end)
9510 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9511 else
9512 /* We can't do Fgoto_char (oldpoint) because it will run some
9513 Lisp code. */
9514 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9515 marker_byte_position (oldpoint));
9517 UNGCPRO;
9518 unchain_marker (XMARKER (oldpoint));
9519 unchain_marker (XMARKER (oldbegv));
9520 unchain_marker (XMARKER (oldzv));
9522 shown = buffer_window_count (current_buffer) > 0;
9523 set_buffer_internal (oldbuf);
9524 if (!shown)
9525 windows_or_buffers_changed = old_windows_or_buffers_changed;
9526 message_log_need_newline = !nlflag;
9527 Vdeactivate_mark = old_deactivate_mark;
9532 /* We are at the end of the buffer after just having inserted a newline.
9533 (Note: We depend on the fact we won't be crossing the gap.)
9534 Check to see if the most recent message looks a lot like the previous one.
9535 Return 0 if different, 1 if the new one should just replace it, or a
9536 value N > 1 if we should also append " [N times]". */
9538 static intmax_t
9539 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9541 ptrdiff_t i;
9542 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9543 int seen_dots = 0;
9544 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9545 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9547 for (i = 0; i < len; i++)
9549 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9550 seen_dots = 1;
9551 if (p1[i] != p2[i])
9552 return seen_dots;
9554 p1 += len;
9555 if (*p1 == '\n')
9556 return 2;
9557 if (*p1++ == ' ' && *p1++ == '[')
9559 char *pend;
9560 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9561 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9562 return n + 1;
9564 return 0;
9568 /* Display an echo area message M with a specified length of NBYTES
9569 bytes. The string may include null characters. If M is not a
9570 string, clear out any existing message, and let the mini-buffer
9571 text show through.
9573 This function cancels echoing. */
9575 void
9576 message3 (Lisp_Object m)
9578 struct gcpro gcpro1;
9580 GCPRO1 (m);
9581 clear_message (1,1);
9582 cancel_echoing ();
9584 /* First flush out any partial line written with print. */
9585 message_log_maybe_newline ();
9586 if (STRINGP (m))
9588 ptrdiff_t nbytes = SBYTES (m);
9589 int multibyte = STRING_MULTIBYTE (m);
9590 USE_SAFE_ALLOCA;
9591 char *buffer = SAFE_ALLOCA (nbytes);
9592 memcpy (buffer, SDATA (m), nbytes);
9593 message_dolog (buffer, nbytes, 1, multibyte);
9594 SAFE_FREE ();
9596 message3_nolog (m);
9598 UNGCPRO;
9602 /* The non-logging version of message3.
9603 This does not cancel echoing, because it is used for echoing.
9604 Perhaps we need to make a separate function for echoing
9605 and make this cancel echoing. */
9607 void
9608 message3_nolog (Lisp_Object m)
9610 struct frame *sf = SELECTED_FRAME ();
9612 if (FRAME_INITIAL_P (sf))
9614 if (noninteractive_need_newline)
9615 putc ('\n', stderr);
9616 noninteractive_need_newline = 0;
9617 if (STRINGP (m))
9618 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9619 if (cursor_in_echo_area == 0)
9620 fprintf (stderr, "\n");
9621 fflush (stderr);
9623 /* Error messages get reported properly by cmd_error, so this must be just an
9624 informative message; if the frame hasn't really been initialized yet, just
9625 toss it. */
9626 else if (INTERACTIVE && sf->glyphs_initialized_p)
9628 /* Get the frame containing the mini-buffer
9629 that the selected frame is using. */
9630 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9631 Lisp_Object frame = XWINDOW (mini_window)->frame;
9632 struct frame *f = XFRAME (frame);
9634 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9635 Fmake_frame_visible (frame);
9637 if (STRINGP (m) && SCHARS (m) > 0)
9639 set_message (m);
9640 if (minibuffer_auto_raise)
9641 Fraise_frame (frame);
9642 /* Assume we are not echoing.
9643 (If we are, echo_now will override this.) */
9644 echo_message_buffer = Qnil;
9646 else
9647 clear_message (1, 1);
9649 do_pending_window_change (0);
9650 echo_area_display (1);
9651 do_pending_window_change (0);
9652 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9653 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9658 /* Display a null-terminated echo area message M. If M is 0, clear
9659 out any existing message, and let the mini-buffer text show through.
9661 The buffer M must continue to exist until after the echo area gets
9662 cleared or some other message gets displayed there. Do not pass
9663 text that is stored in a Lisp string. Do not pass text in a buffer
9664 that was alloca'd. */
9666 void
9667 message1 (const char *m)
9669 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9673 /* The non-logging counterpart of message1. */
9675 void
9676 message1_nolog (const char *m)
9678 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9681 /* Display a message M which contains a single %s
9682 which gets replaced with STRING. */
9684 void
9685 message_with_string (const char *m, Lisp_Object string, int log)
9687 CHECK_STRING (string);
9689 if (noninteractive)
9691 if (m)
9693 if (noninteractive_need_newline)
9694 putc ('\n', stderr);
9695 noninteractive_need_newline = 0;
9696 fprintf (stderr, m, SDATA (string));
9697 if (!cursor_in_echo_area)
9698 fprintf (stderr, "\n");
9699 fflush (stderr);
9702 else if (INTERACTIVE)
9704 /* The frame whose minibuffer we're going to display the message on.
9705 It may be larger than the selected frame, so we need
9706 to use its buffer, not the selected frame's buffer. */
9707 Lisp_Object mini_window;
9708 struct frame *f, *sf = SELECTED_FRAME ();
9710 /* Get the frame containing the minibuffer
9711 that the selected frame is using. */
9712 mini_window = FRAME_MINIBUF_WINDOW (sf);
9713 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9715 /* Error messages get reported properly by cmd_error, so this must be
9716 just an informative message; if the frame hasn't really been
9717 initialized yet, just toss it. */
9718 if (f->glyphs_initialized_p)
9720 Lisp_Object args[2], msg;
9721 struct gcpro gcpro1, gcpro2;
9723 args[0] = build_string (m);
9724 args[1] = msg = string;
9725 GCPRO2 (args[0], msg);
9726 gcpro1.nvars = 2;
9728 msg = Fformat (2, args);
9730 if (log)
9731 message3 (msg);
9732 else
9733 message3_nolog (msg);
9735 UNGCPRO;
9737 /* Print should start at the beginning of the message
9738 buffer next time. */
9739 message_buf_print = 0;
9745 /* Dump an informative message to the minibuf. If M is 0, clear out
9746 any existing message, and let the mini-buffer text show through. */
9748 static void
9749 vmessage (const char *m, va_list ap)
9751 if (noninteractive)
9753 if (m)
9755 if (noninteractive_need_newline)
9756 putc ('\n', stderr);
9757 noninteractive_need_newline = 0;
9758 vfprintf (stderr, m, ap);
9759 if (cursor_in_echo_area == 0)
9760 fprintf (stderr, "\n");
9761 fflush (stderr);
9764 else if (INTERACTIVE)
9766 /* The frame whose mini-buffer we're going to display the message
9767 on. It may be larger than the selected frame, so we need to
9768 use its buffer, not the selected frame's buffer. */
9769 Lisp_Object mini_window;
9770 struct frame *f, *sf = SELECTED_FRAME ();
9772 /* Get the frame containing the mini-buffer
9773 that the selected frame is using. */
9774 mini_window = FRAME_MINIBUF_WINDOW (sf);
9775 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9777 /* Error messages get reported properly by cmd_error, so this must be
9778 just an informative message; if the frame hasn't really been
9779 initialized yet, just toss it. */
9780 if (f->glyphs_initialized_p)
9782 if (m)
9784 ptrdiff_t len;
9785 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9786 char *message_buf = alloca (maxsize + 1);
9788 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9790 message3 (make_string (message_buf, len));
9792 else
9793 message1 (0);
9795 /* Print should start at the beginning of the message
9796 buffer next time. */
9797 message_buf_print = 0;
9802 void
9803 message (const char *m, ...)
9805 va_list ap;
9806 va_start (ap, m);
9807 vmessage (m, ap);
9808 va_end (ap);
9812 #if 0
9813 /* The non-logging version of message. */
9815 void
9816 message_nolog (const char *m, ...)
9818 Lisp_Object old_log_max;
9819 va_list ap;
9820 va_start (ap, m);
9821 old_log_max = Vmessage_log_max;
9822 Vmessage_log_max = Qnil;
9823 vmessage (m, ap);
9824 Vmessage_log_max = old_log_max;
9825 va_end (ap);
9827 #endif
9830 /* Display the current message in the current mini-buffer. This is
9831 only called from error handlers in process.c, and is not time
9832 critical. */
9834 void
9835 update_echo_area (void)
9837 if (!NILP (echo_area_buffer[0]))
9839 Lisp_Object string;
9840 string = Fcurrent_message ();
9841 message3 (string);
9846 /* Make sure echo area buffers in `echo_buffers' are live.
9847 If they aren't, make new ones. */
9849 static void
9850 ensure_echo_area_buffers (void)
9852 int i;
9854 for (i = 0; i < 2; ++i)
9855 if (!BUFFERP (echo_buffer[i])
9856 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9858 char name[30];
9859 Lisp_Object old_buffer;
9860 int j;
9862 old_buffer = echo_buffer[i];
9863 echo_buffer[i] = Fget_buffer_create
9864 (make_formatted_string (name, " *Echo Area %d*", i));
9865 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9866 /* to force word wrap in echo area -
9867 it was decided to postpone this*/
9868 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9870 for (j = 0; j < 2; ++j)
9871 if (EQ (old_buffer, echo_area_buffer[j]))
9872 echo_area_buffer[j] = echo_buffer[i];
9877 /* Call FN with args A1..A2 with either the current or last displayed
9878 echo_area_buffer as current buffer.
9880 WHICH zero means use the current message buffer
9881 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9882 from echo_buffer[] and clear it.
9884 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9885 suitable buffer from echo_buffer[] and clear it.
9887 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9888 that the current message becomes the last displayed one, make
9889 choose a suitable buffer for echo_area_buffer[0], and clear it.
9891 Value is what FN returns. */
9893 static int
9894 with_echo_area_buffer (struct window *w, int which,
9895 int (*fn) (ptrdiff_t, Lisp_Object),
9896 ptrdiff_t a1, Lisp_Object a2)
9898 Lisp_Object buffer;
9899 int this_one, the_other, clear_buffer_p, rc;
9900 ptrdiff_t count = SPECPDL_INDEX ();
9902 /* If buffers aren't live, make new ones. */
9903 ensure_echo_area_buffers ();
9905 clear_buffer_p = 0;
9907 if (which == 0)
9908 this_one = 0, the_other = 1;
9909 else if (which > 0)
9910 this_one = 1, the_other = 0;
9911 else
9913 this_one = 0, the_other = 1;
9914 clear_buffer_p = 1;
9916 /* We need a fresh one in case the current echo buffer equals
9917 the one containing the last displayed echo area message. */
9918 if (!NILP (echo_area_buffer[this_one])
9919 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9920 echo_area_buffer[this_one] = Qnil;
9923 /* Choose a suitable buffer from echo_buffer[] is we don't
9924 have one. */
9925 if (NILP (echo_area_buffer[this_one]))
9927 echo_area_buffer[this_one]
9928 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9929 ? echo_buffer[the_other]
9930 : echo_buffer[this_one]);
9931 clear_buffer_p = 1;
9934 buffer = echo_area_buffer[this_one];
9936 /* Don't get confused by reusing the buffer used for echoing
9937 for a different purpose. */
9938 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9939 cancel_echoing ();
9941 record_unwind_protect (unwind_with_echo_area_buffer,
9942 with_echo_area_buffer_unwind_data (w));
9944 /* Make the echo area buffer current. Note that for display
9945 purposes, it is not necessary that the displayed window's buffer
9946 == current_buffer, except for text property lookup. So, let's
9947 only set that buffer temporarily here without doing a full
9948 Fset_window_buffer. We must also change w->pointm, though,
9949 because otherwise an assertions in unshow_buffer fails, and Emacs
9950 aborts. */
9951 set_buffer_internal_1 (XBUFFER (buffer));
9952 if (w)
9954 wset_buffer (w, buffer);
9955 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9958 bset_undo_list (current_buffer, Qt);
9959 bset_read_only (current_buffer, Qnil);
9960 specbind (Qinhibit_read_only, Qt);
9961 specbind (Qinhibit_modification_hooks, Qt);
9963 if (clear_buffer_p && Z > BEG)
9964 del_range (BEG, Z);
9966 eassert (BEGV >= BEG);
9967 eassert (ZV <= Z && ZV >= BEGV);
9969 rc = fn (a1, a2);
9971 eassert (BEGV >= BEG);
9972 eassert (ZV <= Z && ZV >= BEGV);
9974 unbind_to (count, Qnil);
9975 return rc;
9979 /* Save state that should be preserved around the call to the function
9980 FN called in with_echo_area_buffer. */
9982 static Lisp_Object
9983 with_echo_area_buffer_unwind_data (struct window *w)
9985 int i = 0;
9986 Lisp_Object vector, tmp;
9988 /* Reduce consing by keeping one vector in
9989 Vwith_echo_area_save_vector. */
9990 vector = Vwith_echo_area_save_vector;
9991 Vwith_echo_area_save_vector = Qnil;
9993 if (NILP (vector))
9994 vector = Fmake_vector (make_number (7), Qnil);
9996 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9997 ASET (vector, i, Vdeactivate_mark); ++i;
9998 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10000 if (w)
10002 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10003 ASET (vector, i, w->buffer); ++i;
10004 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10005 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10007 else
10009 int end = i + 4;
10010 for (; i < end; ++i)
10011 ASET (vector, i, Qnil);
10014 eassert (i == ASIZE (vector));
10015 return vector;
10019 /* Restore global state from VECTOR which was created by
10020 with_echo_area_buffer_unwind_data. */
10022 static Lisp_Object
10023 unwind_with_echo_area_buffer (Lisp_Object vector)
10025 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10026 Vdeactivate_mark = AREF (vector, 1);
10027 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10029 if (WINDOWP (AREF (vector, 3)))
10031 struct window *w;
10032 Lisp_Object buffer, charpos, bytepos;
10034 w = XWINDOW (AREF (vector, 3));
10035 buffer = AREF (vector, 4);
10036 charpos = AREF (vector, 5);
10037 bytepos = AREF (vector, 6);
10039 wset_buffer (w, buffer);
10040 set_marker_both (w->pointm, buffer,
10041 XFASTINT (charpos), XFASTINT (bytepos));
10044 Vwith_echo_area_save_vector = vector;
10045 return Qnil;
10049 /* Set up the echo area for use by print functions. MULTIBYTE_P
10050 non-zero means we will print multibyte. */
10052 void
10053 setup_echo_area_for_printing (int multibyte_p)
10055 /* If we can't find an echo area any more, exit. */
10056 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10057 Fkill_emacs (Qnil);
10059 ensure_echo_area_buffers ();
10061 if (!message_buf_print)
10063 /* A message has been output since the last time we printed.
10064 Choose a fresh echo area buffer. */
10065 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10066 echo_area_buffer[0] = echo_buffer[1];
10067 else
10068 echo_area_buffer[0] = echo_buffer[0];
10070 /* Switch to that buffer and clear it. */
10071 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10072 bset_truncate_lines (current_buffer, Qnil);
10074 if (Z > BEG)
10076 ptrdiff_t count = SPECPDL_INDEX ();
10077 specbind (Qinhibit_read_only, Qt);
10078 /* Note that undo recording is always disabled. */
10079 del_range (BEG, Z);
10080 unbind_to (count, Qnil);
10082 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10084 /* Set up the buffer for the multibyteness we need. */
10085 if (multibyte_p
10086 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10087 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10089 /* Raise the frame containing the echo area. */
10090 if (minibuffer_auto_raise)
10092 struct frame *sf = SELECTED_FRAME ();
10093 Lisp_Object mini_window;
10094 mini_window = FRAME_MINIBUF_WINDOW (sf);
10095 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10098 message_log_maybe_newline ();
10099 message_buf_print = 1;
10101 else
10103 if (NILP (echo_area_buffer[0]))
10105 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10106 echo_area_buffer[0] = echo_buffer[1];
10107 else
10108 echo_area_buffer[0] = echo_buffer[0];
10111 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10113 /* Someone switched buffers between print requests. */
10114 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10115 bset_truncate_lines (current_buffer, Qnil);
10121 /* Display an echo area message in window W. Value is non-zero if W's
10122 height is changed. If display_last_displayed_message_p is
10123 non-zero, display the message that was last displayed, otherwise
10124 display the current message. */
10126 static int
10127 display_echo_area (struct window *w)
10129 int i, no_message_p, window_height_changed_p;
10131 /* Temporarily disable garbage collections while displaying the echo
10132 area. This is done because a GC can print a message itself.
10133 That message would modify the echo area buffer's contents while a
10134 redisplay of the buffer is going on, and seriously confuse
10135 redisplay. */
10136 ptrdiff_t count = inhibit_garbage_collection ();
10138 /* If there is no message, we must call display_echo_area_1
10139 nevertheless because it resizes the window. But we will have to
10140 reset the echo_area_buffer in question to nil at the end because
10141 with_echo_area_buffer will sets it to an empty buffer. */
10142 i = display_last_displayed_message_p ? 1 : 0;
10143 no_message_p = NILP (echo_area_buffer[i]);
10145 window_height_changed_p
10146 = with_echo_area_buffer (w, display_last_displayed_message_p,
10147 display_echo_area_1,
10148 (intptr_t) w, Qnil);
10150 if (no_message_p)
10151 echo_area_buffer[i] = Qnil;
10153 unbind_to (count, Qnil);
10154 return window_height_changed_p;
10158 /* Helper for display_echo_area. Display the current buffer which
10159 contains the current echo area message in window W, a mini-window,
10160 a pointer to which is passed in A1. A2..A4 are currently not used.
10161 Change the height of W so that all of the message is displayed.
10162 Value is non-zero if height of W was changed. */
10164 static int
10165 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10167 intptr_t i1 = a1;
10168 struct window *w = (struct window *) i1;
10169 Lisp_Object window;
10170 struct text_pos start;
10171 int window_height_changed_p = 0;
10173 /* Do this before displaying, so that we have a large enough glyph
10174 matrix for the display. If we can't get enough space for the
10175 whole text, display the last N lines. That works by setting w->start. */
10176 window_height_changed_p = resize_mini_window (w, 0);
10178 /* Use the starting position chosen by resize_mini_window. */
10179 SET_TEXT_POS_FROM_MARKER (start, w->start);
10181 /* Display. */
10182 clear_glyph_matrix (w->desired_matrix);
10183 XSETWINDOW (window, w);
10184 try_window (window, start, 0);
10186 return window_height_changed_p;
10190 /* Resize the echo area window to exactly the size needed for the
10191 currently displayed message, if there is one. If a mini-buffer
10192 is active, don't shrink it. */
10194 void
10195 resize_echo_area_exactly (void)
10197 if (BUFFERP (echo_area_buffer[0])
10198 && WINDOWP (echo_area_window))
10200 struct window *w = XWINDOW (echo_area_window);
10201 int resized_p;
10202 Lisp_Object resize_exactly;
10204 if (minibuf_level == 0)
10205 resize_exactly = Qt;
10206 else
10207 resize_exactly = Qnil;
10209 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10210 (intptr_t) w, resize_exactly);
10211 if (resized_p)
10213 ++windows_or_buffers_changed;
10214 ++update_mode_lines;
10215 redisplay_internal ();
10221 /* Callback function for with_echo_area_buffer, when used from
10222 resize_echo_area_exactly. A1 contains a pointer to the window to
10223 resize, EXACTLY non-nil means resize the mini-window exactly to the
10224 size of the text displayed. A3 and A4 are not used. Value is what
10225 resize_mini_window returns. */
10227 static int
10228 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10230 intptr_t i1 = a1;
10231 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10235 /* Resize mini-window W to fit the size of its contents. EXACT_P
10236 means size the window exactly to the size needed. Otherwise, it's
10237 only enlarged until W's buffer is empty.
10239 Set W->start to the right place to begin display. If the whole
10240 contents fit, start at the beginning. Otherwise, start so as
10241 to make the end of the contents appear. This is particularly
10242 important for y-or-n-p, but seems desirable generally.
10244 Value is non-zero if the window height has been changed. */
10247 resize_mini_window (struct window *w, int exact_p)
10249 struct frame *f = XFRAME (w->frame);
10250 int window_height_changed_p = 0;
10252 eassert (MINI_WINDOW_P (w));
10254 /* By default, start display at the beginning. */
10255 set_marker_both (w->start, w->buffer,
10256 BUF_BEGV (XBUFFER (w->buffer)),
10257 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10259 /* Don't resize windows while redisplaying a window; it would
10260 confuse redisplay functions when the size of the window they are
10261 displaying changes from under them. Such a resizing can happen,
10262 for instance, when which-func prints a long message while
10263 we are running fontification-functions. We're running these
10264 functions with safe_call which binds inhibit-redisplay to t. */
10265 if (!NILP (Vinhibit_redisplay))
10266 return 0;
10268 /* Nil means don't try to resize. */
10269 if (NILP (Vresize_mini_windows)
10270 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10271 return 0;
10273 if (!FRAME_MINIBUF_ONLY_P (f))
10275 struct it it;
10276 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10277 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10278 int height;
10279 EMACS_INT max_height;
10280 int unit = FRAME_LINE_HEIGHT (f);
10281 struct text_pos start;
10282 struct buffer *old_current_buffer = NULL;
10284 if (current_buffer != XBUFFER (w->buffer))
10286 old_current_buffer = current_buffer;
10287 set_buffer_internal (XBUFFER (w->buffer));
10290 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10292 /* Compute the max. number of lines specified by the user. */
10293 if (FLOATP (Vmax_mini_window_height))
10294 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10295 else if (INTEGERP (Vmax_mini_window_height))
10296 max_height = XINT (Vmax_mini_window_height);
10297 else
10298 max_height = total_height / 4;
10300 /* Correct that max. height if it's bogus. */
10301 max_height = clip_to_bounds (1, max_height, total_height);
10303 /* Find out the height of the text in the window. */
10304 if (it.line_wrap == TRUNCATE)
10305 height = 1;
10306 else
10308 last_height = 0;
10309 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10310 if (it.max_ascent == 0 && it.max_descent == 0)
10311 height = it.current_y + last_height;
10312 else
10313 height = it.current_y + it.max_ascent + it.max_descent;
10314 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10315 height = (height + unit - 1) / unit;
10318 /* Compute a suitable window start. */
10319 if (height > max_height)
10321 height = max_height;
10322 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10323 move_it_vertically_backward (&it, (height - 1) * unit);
10324 start = it.current.pos;
10326 else
10327 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10328 SET_MARKER_FROM_TEXT_POS (w->start, start);
10330 if (EQ (Vresize_mini_windows, Qgrow_only))
10332 /* Let it grow only, until we display an empty message, in which
10333 case the window shrinks again. */
10334 if (height > WINDOW_TOTAL_LINES (w))
10336 int old_height = WINDOW_TOTAL_LINES (w);
10337 freeze_window_starts (f, 1);
10338 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10339 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10341 else if (height < WINDOW_TOTAL_LINES (w)
10342 && (exact_p || BEGV == ZV))
10344 int old_height = WINDOW_TOTAL_LINES (w);
10345 freeze_window_starts (f, 0);
10346 shrink_mini_window (w);
10347 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10350 else
10352 /* Always resize to exact size needed. */
10353 if (height > WINDOW_TOTAL_LINES (w))
10355 int old_height = WINDOW_TOTAL_LINES (w);
10356 freeze_window_starts (f, 1);
10357 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10358 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10360 else if (height < WINDOW_TOTAL_LINES (w))
10362 int old_height = WINDOW_TOTAL_LINES (w);
10363 freeze_window_starts (f, 0);
10364 shrink_mini_window (w);
10366 if (height)
10368 freeze_window_starts (f, 1);
10369 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10372 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10376 if (old_current_buffer)
10377 set_buffer_internal (old_current_buffer);
10380 return window_height_changed_p;
10384 /* Value is the current message, a string, or nil if there is no
10385 current message. */
10387 Lisp_Object
10388 current_message (void)
10390 Lisp_Object msg;
10392 if (!BUFFERP (echo_area_buffer[0]))
10393 msg = Qnil;
10394 else
10396 with_echo_area_buffer (0, 0, current_message_1,
10397 (intptr_t) &msg, Qnil);
10398 if (NILP (msg))
10399 echo_area_buffer[0] = Qnil;
10402 return msg;
10406 static int
10407 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10409 intptr_t i1 = a1;
10410 Lisp_Object *msg = (Lisp_Object *) i1;
10412 if (Z > BEG)
10413 *msg = make_buffer_string (BEG, Z, 1);
10414 else
10415 *msg = Qnil;
10416 return 0;
10420 /* Push the current message on Vmessage_stack for later restoration
10421 by restore_message. Value is non-zero if the current message isn't
10422 empty. This is a relatively infrequent operation, so it's not
10423 worth optimizing. */
10425 bool
10426 push_message (void)
10428 Lisp_Object msg = current_message ();
10429 Vmessage_stack = Fcons (msg, Vmessage_stack);
10430 return STRINGP (msg);
10434 /* Restore message display from the top of Vmessage_stack. */
10436 void
10437 restore_message (void)
10439 eassert (CONSP (Vmessage_stack));
10440 message3_nolog (XCAR (Vmessage_stack));
10444 /* Handler for record_unwind_protect calling pop_message. */
10446 Lisp_Object
10447 pop_message_unwind (Lisp_Object dummy)
10449 pop_message ();
10450 return Qnil;
10453 /* Pop the top-most entry off Vmessage_stack. */
10455 static void
10456 pop_message (void)
10458 eassert (CONSP (Vmessage_stack));
10459 Vmessage_stack = XCDR (Vmessage_stack);
10463 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10464 exits. If the stack is not empty, we have a missing pop_message
10465 somewhere. */
10467 void
10468 check_message_stack (void)
10470 if (!NILP (Vmessage_stack))
10471 emacs_abort ();
10475 /* Truncate to NCHARS what will be displayed in the echo area the next
10476 time we display it---but don't redisplay it now. */
10478 void
10479 truncate_echo_area (ptrdiff_t nchars)
10481 if (nchars == 0)
10482 echo_area_buffer[0] = Qnil;
10483 else if (!noninteractive
10484 && INTERACTIVE
10485 && !NILP (echo_area_buffer[0]))
10487 struct frame *sf = SELECTED_FRAME ();
10488 /* Error messages get reported properly by cmd_error, so this must be
10489 just an informative message; if the frame hasn't really been
10490 initialized yet, just toss it. */
10491 if (sf->glyphs_initialized_p)
10492 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10497 /* Helper function for truncate_echo_area. Truncate the current
10498 message to at most NCHARS characters. */
10500 static int
10501 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10503 if (BEG + nchars < Z)
10504 del_range (BEG + nchars, Z);
10505 if (Z == BEG)
10506 echo_area_buffer[0] = Qnil;
10507 return 0;
10510 /* Set the current message to STRING. */
10512 static void
10513 set_message (Lisp_Object string)
10515 eassert (STRINGP (string));
10517 message_enable_multibyte = STRING_MULTIBYTE (string);
10519 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10520 message_buf_print = 0;
10521 help_echo_showing_p = 0;
10523 if (STRINGP (Vdebug_on_message)
10524 && fast_string_match (Vdebug_on_message, string) >= 0)
10525 call_debugger (list2 (Qerror, string));
10529 /* Helper function for set_message. First argument is ignored and second
10530 argument has the same meaning as for set_message.
10531 This function is called with the echo area buffer being current. */
10533 static int
10534 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10536 eassert (STRINGP (string));
10538 /* Change multibyteness of the echo buffer appropriately. */
10539 if (message_enable_multibyte
10540 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10541 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10543 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10544 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10545 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10547 /* Insert new message at BEG. */
10548 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10550 /* This function takes care of single/multibyte conversion.
10551 We just have to ensure that the echo area buffer has the right
10552 setting of enable_multibyte_characters. */
10553 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10555 return 0;
10559 /* Clear messages. CURRENT_P non-zero means clear the current
10560 message. LAST_DISPLAYED_P non-zero means clear the message
10561 last displayed. */
10563 void
10564 clear_message (int current_p, int last_displayed_p)
10566 if (current_p)
10568 echo_area_buffer[0] = Qnil;
10569 message_cleared_p = 1;
10572 if (last_displayed_p)
10573 echo_area_buffer[1] = Qnil;
10575 message_buf_print = 0;
10578 /* Clear garbaged frames.
10580 This function is used where the old redisplay called
10581 redraw_garbaged_frames which in turn called redraw_frame which in
10582 turn called clear_frame. The call to clear_frame was a source of
10583 flickering. I believe a clear_frame is not necessary. It should
10584 suffice in the new redisplay to invalidate all current matrices,
10585 and ensure a complete redisplay of all windows. */
10587 static void
10588 clear_garbaged_frames (void)
10590 if (frame_garbaged)
10592 Lisp_Object tail, frame;
10593 int changed_count = 0;
10595 FOR_EACH_FRAME (tail, frame)
10597 struct frame *f = XFRAME (frame);
10599 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10601 if (f->resized_p)
10603 redraw_frame (f);
10604 f->force_flush_display_p = 1;
10606 clear_current_matrices (f);
10607 changed_count++;
10608 f->garbaged = 0;
10609 f->resized_p = 0;
10613 frame_garbaged = 0;
10614 if (changed_count)
10615 ++windows_or_buffers_changed;
10620 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10621 is non-zero update selected_frame. Value is non-zero if the
10622 mini-windows height has been changed. */
10624 static int
10625 echo_area_display (int update_frame_p)
10627 Lisp_Object mini_window;
10628 struct window *w;
10629 struct frame *f;
10630 int window_height_changed_p = 0;
10631 struct frame *sf = SELECTED_FRAME ();
10633 mini_window = FRAME_MINIBUF_WINDOW (sf);
10634 w = XWINDOW (mini_window);
10635 f = XFRAME (WINDOW_FRAME (w));
10637 /* Don't display if frame is invisible or not yet initialized. */
10638 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10639 return 0;
10641 #ifdef HAVE_WINDOW_SYSTEM
10642 /* When Emacs starts, selected_frame may be the initial terminal
10643 frame. If we let this through, a message would be displayed on
10644 the terminal. */
10645 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10646 return 0;
10647 #endif /* HAVE_WINDOW_SYSTEM */
10649 /* Redraw garbaged frames. */
10650 clear_garbaged_frames ();
10652 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10654 echo_area_window = mini_window;
10655 window_height_changed_p = display_echo_area (w);
10656 w->must_be_updated_p = 1;
10658 /* Update the display, unless called from redisplay_internal.
10659 Also don't update the screen during redisplay itself. The
10660 update will happen at the end of redisplay, and an update
10661 here could cause confusion. */
10662 if (update_frame_p && !redisplaying_p)
10664 int n = 0;
10666 /* If the display update has been interrupted by pending
10667 input, update mode lines in the frame. Due to the
10668 pending input, it might have been that redisplay hasn't
10669 been called, so that mode lines above the echo area are
10670 garbaged. This looks odd, so we prevent it here. */
10671 if (!display_completed)
10672 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10674 if (window_height_changed_p
10675 /* Don't do this if Emacs is shutting down. Redisplay
10676 needs to run hooks. */
10677 && !NILP (Vrun_hooks))
10679 /* Must update other windows. Likewise as in other
10680 cases, don't let this update be interrupted by
10681 pending input. */
10682 ptrdiff_t count = SPECPDL_INDEX ();
10683 specbind (Qredisplay_dont_pause, Qt);
10684 windows_or_buffers_changed = 1;
10685 redisplay_internal ();
10686 unbind_to (count, Qnil);
10688 else if (FRAME_WINDOW_P (f) && n == 0)
10690 /* Window configuration is the same as before.
10691 Can do with a display update of the echo area,
10692 unless we displayed some mode lines. */
10693 update_single_window (w, 1);
10694 FRAME_RIF (f)->flush_display (f);
10696 else
10697 update_frame (f, 1, 1);
10699 /* If cursor is in the echo area, make sure that the next
10700 redisplay displays the minibuffer, so that the cursor will
10701 be replaced with what the minibuffer wants. */
10702 if (cursor_in_echo_area)
10703 ++windows_or_buffers_changed;
10706 else if (!EQ (mini_window, selected_window))
10707 windows_or_buffers_changed++;
10709 /* Last displayed message is now the current message. */
10710 echo_area_buffer[1] = echo_area_buffer[0];
10711 /* Inform read_char that we're not echoing. */
10712 echo_message_buffer = Qnil;
10714 /* Prevent redisplay optimization in redisplay_internal by resetting
10715 this_line_start_pos. This is done because the mini-buffer now
10716 displays the message instead of its buffer text. */
10717 if (EQ (mini_window, selected_window))
10718 CHARPOS (this_line_start_pos) = 0;
10720 return window_height_changed_p;
10723 /* Nonzero if the current window's buffer is shown in more than one
10724 window and was modified since last redisplay. */
10726 static int
10727 buffer_shared_and_changed (void)
10729 return (buffer_window_count (current_buffer) > 1
10730 && UNCHANGED_MODIFIED < MODIFF);
10733 /* Nonzero if W doesn't reflect the actual state of current buffer due
10734 to its text or overlays change. FIXME: this may be called when
10735 XBUFFER (w->buffer) != current_buffer, which looks suspicious. */
10737 static int
10738 window_outdated (struct window *w)
10740 return (w->last_modified < MODIFF
10741 || w->last_overlay_modified < OVERLAY_MODIFF);
10744 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10745 is enabled and mark of W's buffer was changed since last W's update. */
10747 static int
10748 window_buffer_changed (struct window *w)
10750 struct buffer *b = XBUFFER (w->buffer);
10752 eassert (BUFFER_LIVE_P (b));
10754 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10755 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10756 != (w->region_showing != 0)));
10759 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10761 static int
10762 mode_line_update_needed (struct window *w)
10764 return (w->column_number_displayed != -1
10765 && !(PT == w->last_point && !window_outdated (w))
10766 && (w->column_number_displayed != current_column ()));
10769 /***********************************************************************
10770 Mode Lines and Frame Titles
10771 ***********************************************************************/
10773 /* A buffer for constructing non-propertized mode-line strings and
10774 frame titles in it; allocated from the heap in init_xdisp and
10775 resized as needed in store_mode_line_noprop_char. */
10777 static char *mode_line_noprop_buf;
10779 /* The buffer's end, and a current output position in it. */
10781 static char *mode_line_noprop_buf_end;
10782 static char *mode_line_noprop_ptr;
10784 #define MODE_LINE_NOPROP_LEN(start) \
10785 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10787 static enum {
10788 MODE_LINE_DISPLAY = 0,
10789 MODE_LINE_TITLE,
10790 MODE_LINE_NOPROP,
10791 MODE_LINE_STRING
10792 } mode_line_target;
10794 /* Alist that caches the results of :propertize.
10795 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10796 static Lisp_Object mode_line_proptrans_alist;
10798 /* List of strings making up the mode-line. */
10799 static Lisp_Object mode_line_string_list;
10801 /* Base face property when building propertized mode line string. */
10802 static Lisp_Object mode_line_string_face;
10803 static Lisp_Object mode_line_string_face_prop;
10806 /* Unwind data for mode line strings */
10808 static Lisp_Object Vmode_line_unwind_vector;
10810 static Lisp_Object
10811 format_mode_line_unwind_data (struct frame *target_frame,
10812 struct buffer *obuf,
10813 Lisp_Object owin,
10814 int save_proptrans)
10816 Lisp_Object vector, tmp;
10818 /* Reduce consing by keeping one vector in
10819 Vwith_echo_area_save_vector. */
10820 vector = Vmode_line_unwind_vector;
10821 Vmode_line_unwind_vector = Qnil;
10823 if (NILP (vector))
10824 vector = Fmake_vector (make_number (10), Qnil);
10826 ASET (vector, 0, make_number (mode_line_target));
10827 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10828 ASET (vector, 2, mode_line_string_list);
10829 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10830 ASET (vector, 4, mode_line_string_face);
10831 ASET (vector, 5, mode_line_string_face_prop);
10833 if (obuf)
10834 XSETBUFFER (tmp, obuf);
10835 else
10836 tmp = Qnil;
10837 ASET (vector, 6, tmp);
10838 ASET (vector, 7, owin);
10839 if (target_frame)
10841 /* Similarly to `with-selected-window', if the operation selects
10842 a window on another frame, we must restore that frame's
10843 selected window, and (for a tty) the top-frame. */
10844 ASET (vector, 8, target_frame->selected_window);
10845 if (FRAME_TERMCAP_P (target_frame))
10846 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10849 return vector;
10852 static Lisp_Object
10853 unwind_format_mode_line (Lisp_Object vector)
10855 Lisp_Object old_window = AREF (vector, 7);
10856 Lisp_Object target_frame_window = AREF (vector, 8);
10857 Lisp_Object old_top_frame = AREF (vector, 9);
10859 mode_line_target = XINT (AREF (vector, 0));
10860 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10861 mode_line_string_list = AREF (vector, 2);
10862 if (! EQ (AREF (vector, 3), Qt))
10863 mode_line_proptrans_alist = AREF (vector, 3);
10864 mode_line_string_face = AREF (vector, 4);
10865 mode_line_string_face_prop = AREF (vector, 5);
10867 /* Select window before buffer, since it may change the buffer. */
10868 if (!NILP (old_window))
10870 /* If the operation that we are unwinding had selected a window
10871 on a different frame, reset its frame-selected-window. For a
10872 text terminal, reset its top-frame if necessary. */
10873 if (!NILP (target_frame_window))
10875 Lisp_Object frame
10876 = WINDOW_FRAME (XWINDOW (target_frame_window));
10878 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10879 Fselect_window (target_frame_window, Qt);
10881 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10882 Fselect_frame (old_top_frame, Qt);
10885 Fselect_window (old_window, Qt);
10888 if (!NILP (AREF (vector, 6)))
10890 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10891 ASET (vector, 6, Qnil);
10894 Vmode_line_unwind_vector = vector;
10895 return Qnil;
10899 /* Store a single character C for the frame title in mode_line_noprop_buf.
10900 Re-allocate mode_line_noprop_buf if necessary. */
10902 static void
10903 store_mode_line_noprop_char (char c)
10905 /* If output position has reached the end of the allocated buffer,
10906 increase the buffer's size. */
10907 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10909 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10910 ptrdiff_t size = len;
10911 mode_line_noprop_buf =
10912 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10913 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10914 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10917 *mode_line_noprop_ptr++ = c;
10921 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10922 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10923 characters that yield more columns than PRECISION; PRECISION <= 0
10924 means copy the whole string. Pad with spaces until FIELD_WIDTH
10925 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10926 pad. Called from display_mode_element when it is used to build a
10927 frame title. */
10929 static int
10930 store_mode_line_noprop (const char *string, int field_width, int precision)
10932 const unsigned char *str = (const unsigned char *) string;
10933 int n = 0;
10934 ptrdiff_t dummy, nbytes;
10936 /* Copy at most PRECISION chars from STR. */
10937 nbytes = strlen (string);
10938 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10939 while (nbytes--)
10940 store_mode_line_noprop_char (*str++);
10942 /* Fill up with spaces until FIELD_WIDTH reached. */
10943 while (field_width > 0
10944 && n < field_width)
10946 store_mode_line_noprop_char (' ');
10947 ++n;
10950 return n;
10953 /***********************************************************************
10954 Frame Titles
10955 ***********************************************************************/
10957 #ifdef HAVE_WINDOW_SYSTEM
10959 /* Set the title of FRAME, if it has changed. The title format is
10960 Vicon_title_format if FRAME is iconified, otherwise it is
10961 frame_title_format. */
10963 static void
10964 x_consider_frame_title (Lisp_Object frame)
10966 struct frame *f = XFRAME (frame);
10968 if (FRAME_WINDOW_P (f)
10969 || FRAME_MINIBUF_ONLY_P (f)
10970 || f->explicit_name)
10972 /* Do we have more than one visible frame on this X display? */
10973 Lisp_Object tail, other_frame, fmt;
10974 ptrdiff_t title_start;
10975 char *title;
10976 ptrdiff_t len;
10977 struct it it;
10978 ptrdiff_t count = SPECPDL_INDEX ();
10980 FOR_EACH_FRAME (tail, other_frame)
10982 struct frame *tf = XFRAME (other_frame);
10984 if (tf != f
10985 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10986 && !FRAME_MINIBUF_ONLY_P (tf)
10987 && !EQ (other_frame, tip_frame)
10988 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10989 break;
10992 /* Set global variable indicating that multiple frames exist. */
10993 multiple_frames = CONSP (tail);
10995 /* Switch to the buffer of selected window of the frame. Set up
10996 mode_line_target so that display_mode_element will output into
10997 mode_line_noprop_buf; then display the title. */
10998 record_unwind_protect (unwind_format_mode_line,
10999 format_mode_line_unwind_data
11000 (f, current_buffer, selected_window, 0));
11002 Fselect_window (f->selected_window, Qt);
11003 set_buffer_internal_1
11004 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11005 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11007 mode_line_target = MODE_LINE_TITLE;
11008 title_start = MODE_LINE_NOPROP_LEN (0);
11009 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11010 NULL, DEFAULT_FACE_ID);
11011 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11012 len = MODE_LINE_NOPROP_LEN (title_start);
11013 title = mode_line_noprop_buf + title_start;
11014 unbind_to (count, Qnil);
11016 /* Set the title only if it's changed. This avoids consing in
11017 the common case where it hasn't. (If it turns out that we've
11018 already wasted too much time by walking through the list with
11019 display_mode_element, then we might need to optimize at a
11020 higher level than this.) */
11021 if (! STRINGP (f->name)
11022 || SBYTES (f->name) != len
11023 || memcmp (title, SDATA (f->name), len) != 0)
11024 x_implicitly_set_name (f, make_string (title, len), Qnil);
11028 #endif /* not HAVE_WINDOW_SYSTEM */
11031 /***********************************************************************
11032 Menu Bars
11033 ***********************************************************************/
11036 /* Prepare for redisplay by updating menu-bar item lists when
11037 appropriate. This can call eval. */
11039 void
11040 prepare_menu_bars (void)
11042 int all_windows;
11043 struct gcpro gcpro1, gcpro2;
11044 struct frame *f;
11045 Lisp_Object tooltip_frame;
11047 #ifdef HAVE_WINDOW_SYSTEM
11048 tooltip_frame = tip_frame;
11049 #else
11050 tooltip_frame = Qnil;
11051 #endif
11053 /* Update all frame titles based on their buffer names, etc. We do
11054 this before the menu bars so that the buffer-menu will show the
11055 up-to-date frame titles. */
11056 #ifdef HAVE_WINDOW_SYSTEM
11057 if (windows_or_buffers_changed || update_mode_lines)
11059 Lisp_Object tail, frame;
11061 FOR_EACH_FRAME (tail, frame)
11063 f = XFRAME (frame);
11064 if (!EQ (frame, tooltip_frame)
11065 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11066 x_consider_frame_title (frame);
11069 #endif /* HAVE_WINDOW_SYSTEM */
11071 /* Update the menu bar item lists, if appropriate. This has to be
11072 done before any actual redisplay or generation of display lines. */
11073 all_windows = (update_mode_lines
11074 || buffer_shared_and_changed ()
11075 || windows_or_buffers_changed);
11076 if (all_windows)
11078 Lisp_Object tail, frame;
11079 ptrdiff_t count = SPECPDL_INDEX ();
11080 /* 1 means that update_menu_bar has run its hooks
11081 so any further calls to update_menu_bar shouldn't do so again. */
11082 int menu_bar_hooks_run = 0;
11084 record_unwind_save_match_data ();
11086 FOR_EACH_FRAME (tail, frame)
11088 f = XFRAME (frame);
11090 /* Ignore tooltip frame. */
11091 if (EQ (frame, tooltip_frame))
11092 continue;
11094 /* If a window on this frame changed size, report that to
11095 the user and clear the size-change flag. */
11096 if (FRAME_WINDOW_SIZES_CHANGED (f))
11098 Lisp_Object functions;
11100 /* Clear flag first in case we get an error below. */
11101 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11102 functions = Vwindow_size_change_functions;
11103 GCPRO2 (tail, functions);
11105 while (CONSP (functions))
11107 if (!EQ (XCAR (functions), Qt))
11108 call1 (XCAR (functions), frame);
11109 functions = XCDR (functions);
11111 UNGCPRO;
11114 GCPRO1 (tail);
11115 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11116 #ifdef HAVE_WINDOW_SYSTEM
11117 update_tool_bar (f, 0);
11118 #endif
11119 #ifdef HAVE_NS
11120 if (windows_or_buffers_changed
11121 && FRAME_NS_P (f))
11122 ns_set_doc_edited
11123 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11124 #endif
11125 UNGCPRO;
11128 unbind_to (count, Qnil);
11130 else
11132 struct frame *sf = SELECTED_FRAME ();
11133 update_menu_bar (sf, 1, 0);
11134 #ifdef HAVE_WINDOW_SYSTEM
11135 update_tool_bar (sf, 1);
11136 #endif
11141 /* Update the menu bar item list for frame F. This has to be done
11142 before we start to fill in any display lines, because it can call
11143 eval.
11145 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11147 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11148 already ran the menu bar hooks for this redisplay, so there
11149 is no need to run them again. The return value is the
11150 updated value of this flag, to pass to the next call. */
11152 static int
11153 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11155 Lisp_Object window;
11156 register struct window *w;
11158 /* If called recursively during a menu update, do nothing. This can
11159 happen when, for instance, an activate-menubar-hook causes a
11160 redisplay. */
11161 if (inhibit_menubar_update)
11162 return hooks_run;
11164 window = FRAME_SELECTED_WINDOW (f);
11165 w = XWINDOW (window);
11167 if (FRAME_WINDOW_P (f)
11169 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11170 || defined (HAVE_NS) || defined (USE_GTK)
11171 FRAME_EXTERNAL_MENU_BAR (f)
11172 #else
11173 FRAME_MENU_BAR_LINES (f) > 0
11174 #endif
11175 : FRAME_MENU_BAR_LINES (f) > 0)
11177 /* If the user has switched buffers or windows, we need to
11178 recompute to reflect the new bindings. But we'll
11179 recompute when update_mode_lines is set too; that means
11180 that people can use force-mode-line-update to request
11181 that the menu bar be recomputed. The adverse effect on
11182 the rest of the redisplay algorithm is about the same as
11183 windows_or_buffers_changed anyway. */
11184 if (windows_or_buffers_changed
11185 /* This used to test w->update_mode_line, but we believe
11186 there is no need to recompute the menu in that case. */
11187 || update_mode_lines
11188 || window_buffer_changed (w))
11190 struct buffer *prev = current_buffer;
11191 ptrdiff_t count = SPECPDL_INDEX ();
11193 specbind (Qinhibit_menubar_update, Qt);
11195 set_buffer_internal_1 (XBUFFER (w->buffer));
11196 if (save_match_data)
11197 record_unwind_save_match_data ();
11198 if (NILP (Voverriding_local_map_menu_flag))
11200 specbind (Qoverriding_terminal_local_map, Qnil);
11201 specbind (Qoverriding_local_map, Qnil);
11204 if (!hooks_run)
11206 /* Run the Lucid hook. */
11207 safe_run_hooks (Qactivate_menubar_hook);
11209 /* If it has changed current-menubar from previous value,
11210 really recompute the menu-bar from the value. */
11211 if (! NILP (Vlucid_menu_bar_dirty_flag))
11212 call0 (Qrecompute_lucid_menubar);
11214 safe_run_hooks (Qmenu_bar_update_hook);
11216 hooks_run = 1;
11219 XSETFRAME (Vmenu_updating_frame, f);
11220 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11222 /* Redisplay the menu bar in case we changed it. */
11223 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11224 || defined (HAVE_NS) || defined (USE_GTK)
11225 if (FRAME_WINDOW_P (f))
11227 #if defined (HAVE_NS)
11228 /* All frames on Mac OS share the same menubar. So only
11229 the selected frame should be allowed to set it. */
11230 if (f == SELECTED_FRAME ())
11231 #endif
11232 set_frame_menubar (f, 0, 0);
11234 else
11235 /* On a terminal screen, the menu bar is an ordinary screen
11236 line, and this makes it get updated. */
11237 w->update_mode_line = 1;
11238 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11239 /* In the non-toolkit version, the menu bar is an ordinary screen
11240 line, and this makes it get updated. */
11241 w->update_mode_line = 1;
11242 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11244 unbind_to (count, Qnil);
11245 set_buffer_internal_1 (prev);
11249 return hooks_run;
11254 /***********************************************************************
11255 Output Cursor
11256 ***********************************************************************/
11258 #ifdef HAVE_WINDOW_SYSTEM
11260 /* EXPORT:
11261 Nominal cursor position -- where to draw output.
11262 HPOS and VPOS are window relative glyph matrix coordinates.
11263 X and Y are window relative pixel coordinates. */
11265 struct cursor_pos output_cursor;
11268 /* EXPORT:
11269 Set the global variable output_cursor to CURSOR. All cursor
11270 positions are relative to updated_window. */
11272 void
11273 set_output_cursor (struct cursor_pos *cursor)
11275 output_cursor.hpos = cursor->hpos;
11276 output_cursor.vpos = cursor->vpos;
11277 output_cursor.x = cursor->x;
11278 output_cursor.y = cursor->y;
11282 /* EXPORT for RIF:
11283 Set a nominal cursor position.
11285 HPOS and VPOS are column/row positions in a window glyph matrix. X
11286 and Y are window text area relative pixel positions.
11288 If this is done during an update, updated_window will contain the
11289 window that is being updated and the position is the future output
11290 cursor position for that window. If updated_window is null, use
11291 selected_window and display the cursor at the given position. */
11293 void
11294 x_cursor_to (int vpos, int hpos, int y, int x)
11296 struct window *w;
11298 /* If updated_window is not set, work on selected_window. */
11299 if (updated_window)
11300 w = updated_window;
11301 else
11302 w = XWINDOW (selected_window);
11304 /* Set the output cursor. */
11305 output_cursor.hpos = hpos;
11306 output_cursor.vpos = vpos;
11307 output_cursor.x = x;
11308 output_cursor.y = y;
11310 /* If not called as part of an update, really display the cursor.
11311 This will also set the cursor position of W. */
11312 if (updated_window == NULL)
11314 block_input ();
11315 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11316 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11317 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11318 unblock_input ();
11322 #endif /* HAVE_WINDOW_SYSTEM */
11325 /***********************************************************************
11326 Tool-bars
11327 ***********************************************************************/
11329 #ifdef HAVE_WINDOW_SYSTEM
11331 /* Where the mouse was last time we reported a mouse event. */
11333 FRAME_PTR last_mouse_frame;
11335 /* Tool-bar item index of the item on which a mouse button was pressed
11336 or -1. */
11338 int last_tool_bar_item;
11340 /* Select `frame' temporarily without running all the code in
11341 do_switch_frame.
11342 FIXME: Maybe do_switch_frame should be trimmed down similarly
11343 when `norecord' is set. */
11344 static Lisp_Object
11345 fast_set_selected_frame (Lisp_Object frame)
11347 if (!EQ (selected_frame, frame))
11349 selected_frame = frame;
11350 selected_window = XFRAME (frame)->selected_window;
11352 return Qnil;
11355 /* Update the tool-bar item list for frame F. This has to be done
11356 before we start to fill in any display lines. Called from
11357 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11358 and restore it here. */
11360 static void
11361 update_tool_bar (struct frame *f, int save_match_data)
11363 #if defined (USE_GTK) || defined (HAVE_NS)
11364 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11365 #else
11366 int do_update = WINDOWP (f->tool_bar_window)
11367 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11368 #endif
11370 if (do_update)
11372 Lisp_Object window;
11373 struct window *w;
11375 window = FRAME_SELECTED_WINDOW (f);
11376 w = XWINDOW (window);
11378 /* If the user has switched buffers or windows, we need to
11379 recompute to reflect the new bindings. But we'll
11380 recompute when update_mode_lines is set too; that means
11381 that people can use force-mode-line-update to request
11382 that the menu bar be recomputed. The adverse effect on
11383 the rest of the redisplay algorithm is about the same as
11384 windows_or_buffers_changed anyway. */
11385 if (windows_or_buffers_changed
11386 || w->update_mode_line
11387 || update_mode_lines
11388 || window_buffer_changed (w))
11390 struct buffer *prev = current_buffer;
11391 ptrdiff_t count = SPECPDL_INDEX ();
11392 Lisp_Object frame, new_tool_bar;
11393 int new_n_tool_bar;
11394 struct gcpro gcpro1;
11396 /* Set current_buffer to the buffer of the selected
11397 window of the frame, so that we get the right local
11398 keymaps. */
11399 set_buffer_internal_1 (XBUFFER (w->buffer));
11401 /* Save match data, if we must. */
11402 if (save_match_data)
11403 record_unwind_save_match_data ();
11405 /* Make sure that we don't accidentally use bogus keymaps. */
11406 if (NILP (Voverriding_local_map_menu_flag))
11408 specbind (Qoverriding_terminal_local_map, Qnil);
11409 specbind (Qoverriding_local_map, Qnil);
11412 GCPRO1 (new_tool_bar);
11414 /* We must temporarily set the selected frame to this frame
11415 before calling tool_bar_items, because the calculation of
11416 the tool-bar keymap uses the selected frame (see
11417 `tool-bar-make-keymap' in tool-bar.el). */
11418 eassert (EQ (selected_window,
11419 /* Since we only explicitly preserve selected_frame,
11420 check that selected_window would be redundant. */
11421 XFRAME (selected_frame)->selected_window));
11422 record_unwind_protect (fast_set_selected_frame, selected_frame);
11423 XSETFRAME (frame, f);
11424 fast_set_selected_frame (frame);
11426 /* Build desired tool-bar items from keymaps. */
11427 new_tool_bar
11428 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11429 &new_n_tool_bar);
11431 /* Redisplay the tool-bar if we changed it. */
11432 if (new_n_tool_bar != f->n_tool_bar_items
11433 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11435 /* Redisplay that happens asynchronously due to an expose event
11436 may access f->tool_bar_items. Make sure we update both
11437 variables within BLOCK_INPUT so no such event interrupts. */
11438 block_input ();
11439 fset_tool_bar_items (f, new_tool_bar);
11440 f->n_tool_bar_items = new_n_tool_bar;
11441 w->update_mode_line = 1;
11442 unblock_input ();
11445 UNGCPRO;
11447 unbind_to (count, Qnil);
11448 set_buffer_internal_1 (prev);
11454 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11455 F's desired tool-bar contents. F->tool_bar_items must have
11456 been set up previously by calling prepare_menu_bars. */
11458 static void
11459 build_desired_tool_bar_string (struct frame *f)
11461 int i, size, size_needed;
11462 struct gcpro gcpro1, gcpro2, gcpro3;
11463 Lisp_Object image, plist, props;
11465 image = plist = props = Qnil;
11466 GCPRO3 (image, plist, props);
11468 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11469 Otherwise, make a new string. */
11471 /* The size of the string we might be able to reuse. */
11472 size = (STRINGP (f->desired_tool_bar_string)
11473 ? SCHARS (f->desired_tool_bar_string)
11474 : 0);
11476 /* We need one space in the string for each image. */
11477 size_needed = f->n_tool_bar_items;
11479 /* Reuse f->desired_tool_bar_string, if possible. */
11480 if (size < size_needed || NILP (f->desired_tool_bar_string))
11481 fset_desired_tool_bar_string
11482 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11483 else
11485 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11486 Fremove_text_properties (make_number (0), make_number (size),
11487 props, f->desired_tool_bar_string);
11490 /* Put a `display' property on the string for the images to display,
11491 put a `menu_item' property on tool-bar items with a value that
11492 is the index of the item in F's tool-bar item vector. */
11493 for (i = 0; i < f->n_tool_bar_items; ++i)
11495 #define PROP(IDX) \
11496 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11498 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11499 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11500 int hmargin, vmargin, relief, idx, end;
11502 /* If image is a vector, choose the image according to the
11503 button state. */
11504 image = PROP (TOOL_BAR_ITEM_IMAGES);
11505 if (VECTORP (image))
11507 if (enabled_p)
11508 idx = (selected_p
11509 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11510 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11511 else
11512 idx = (selected_p
11513 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11514 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11516 eassert (ASIZE (image) >= idx);
11517 image = AREF (image, idx);
11519 else
11520 idx = -1;
11522 /* Ignore invalid image specifications. */
11523 if (!valid_image_p (image))
11524 continue;
11526 /* Display the tool-bar button pressed, or depressed. */
11527 plist = Fcopy_sequence (XCDR (image));
11529 /* Compute margin and relief to draw. */
11530 relief = (tool_bar_button_relief >= 0
11531 ? tool_bar_button_relief
11532 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11533 hmargin = vmargin = relief;
11535 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11536 INT_MAX - max (hmargin, vmargin)))
11538 hmargin += XFASTINT (Vtool_bar_button_margin);
11539 vmargin += XFASTINT (Vtool_bar_button_margin);
11541 else if (CONSP (Vtool_bar_button_margin))
11543 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11544 INT_MAX - hmargin))
11545 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11547 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11548 INT_MAX - vmargin))
11549 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11552 if (auto_raise_tool_bar_buttons_p)
11554 /* Add a `:relief' property to the image spec if the item is
11555 selected. */
11556 if (selected_p)
11558 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11559 hmargin -= relief;
11560 vmargin -= relief;
11563 else
11565 /* If image is selected, display it pressed, i.e. with a
11566 negative relief. If it's not selected, display it with a
11567 raised relief. */
11568 plist = Fplist_put (plist, QCrelief,
11569 (selected_p
11570 ? make_number (-relief)
11571 : make_number (relief)));
11572 hmargin -= relief;
11573 vmargin -= relief;
11576 /* Put a margin around the image. */
11577 if (hmargin || vmargin)
11579 if (hmargin == vmargin)
11580 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11581 else
11582 plist = Fplist_put (plist, QCmargin,
11583 Fcons (make_number (hmargin),
11584 make_number (vmargin)));
11587 /* If button is not enabled, and we don't have special images
11588 for the disabled state, make the image appear disabled by
11589 applying an appropriate algorithm to it. */
11590 if (!enabled_p && idx < 0)
11591 plist = Fplist_put (plist, QCconversion, Qdisabled);
11593 /* Put a `display' text property on the string for the image to
11594 display. Put a `menu-item' property on the string that gives
11595 the start of this item's properties in the tool-bar items
11596 vector. */
11597 image = Fcons (Qimage, plist);
11598 props = list4 (Qdisplay, image,
11599 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11601 /* Let the last image hide all remaining spaces in the tool bar
11602 string. The string can be longer than needed when we reuse a
11603 previous string. */
11604 if (i + 1 == f->n_tool_bar_items)
11605 end = SCHARS (f->desired_tool_bar_string);
11606 else
11607 end = i + 1;
11608 Fadd_text_properties (make_number (i), make_number (end),
11609 props, f->desired_tool_bar_string);
11610 #undef PROP
11613 UNGCPRO;
11617 /* Display one line of the tool-bar of frame IT->f.
11619 HEIGHT specifies the desired height of the tool-bar line.
11620 If the actual height of the glyph row is less than HEIGHT, the
11621 row's height is increased to HEIGHT, and the icons are centered
11622 vertically in the new height.
11624 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11625 count a final empty row in case the tool-bar width exactly matches
11626 the window width.
11629 static void
11630 display_tool_bar_line (struct it *it, int height)
11632 struct glyph_row *row = it->glyph_row;
11633 int max_x = it->last_visible_x;
11634 struct glyph *last;
11636 prepare_desired_row (row);
11637 row->y = it->current_y;
11639 /* Note that this isn't made use of if the face hasn't a box,
11640 so there's no need to check the face here. */
11641 it->start_of_box_run_p = 1;
11643 while (it->current_x < max_x)
11645 int x, n_glyphs_before, i, nglyphs;
11646 struct it it_before;
11648 /* Get the next display element. */
11649 if (!get_next_display_element (it))
11651 /* Don't count empty row if we are counting needed tool-bar lines. */
11652 if (height < 0 && !it->hpos)
11653 return;
11654 break;
11657 /* Produce glyphs. */
11658 n_glyphs_before = row->used[TEXT_AREA];
11659 it_before = *it;
11661 PRODUCE_GLYPHS (it);
11663 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11664 i = 0;
11665 x = it_before.current_x;
11666 while (i < nglyphs)
11668 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11670 if (x + glyph->pixel_width > max_x)
11672 /* Glyph doesn't fit on line. Backtrack. */
11673 row->used[TEXT_AREA] = n_glyphs_before;
11674 *it = it_before;
11675 /* If this is the only glyph on this line, it will never fit on the
11676 tool-bar, so skip it. But ensure there is at least one glyph,
11677 so we don't accidentally disable the tool-bar. */
11678 if (n_glyphs_before == 0
11679 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11680 break;
11681 goto out;
11684 ++it->hpos;
11685 x += glyph->pixel_width;
11686 ++i;
11689 /* Stop at line end. */
11690 if (ITERATOR_AT_END_OF_LINE_P (it))
11691 break;
11693 set_iterator_to_next (it, 1);
11696 out:;
11698 row->displays_text_p = row->used[TEXT_AREA] != 0;
11700 /* Use default face for the border below the tool bar.
11702 FIXME: When auto-resize-tool-bars is grow-only, there is
11703 no additional border below the possibly empty tool-bar lines.
11704 So to make the extra empty lines look "normal", we have to
11705 use the tool-bar face for the border too. */
11706 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11707 it->face_id = DEFAULT_FACE_ID;
11709 extend_face_to_end_of_line (it);
11710 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11711 last->right_box_line_p = 1;
11712 if (last == row->glyphs[TEXT_AREA])
11713 last->left_box_line_p = 1;
11715 /* Make line the desired height and center it vertically. */
11716 if ((height -= it->max_ascent + it->max_descent) > 0)
11718 /* Don't add more than one line height. */
11719 height %= FRAME_LINE_HEIGHT (it->f);
11720 it->max_ascent += height / 2;
11721 it->max_descent += (height + 1) / 2;
11724 compute_line_metrics (it);
11726 /* If line is empty, make it occupy the rest of the tool-bar. */
11727 if (!row->displays_text_p)
11729 row->height = row->phys_height = it->last_visible_y - row->y;
11730 row->visible_height = row->height;
11731 row->ascent = row->phys_ascent = 0;
11732 row->extra_line_spacing = 0;
11735 row->full_width_p = 1;
11736 row->continued_p = 0;
11737 row->truncated_on_left_p = 0;
11738 row->truncated_on_right_p = 0;
11740 it->current_x = it->hpos = 0;
11741 it->current_y += row->height;
11742 ++it->vpos;
11743 ++it->glyph_row;
11747 /* Max tool-bar height. */
11749 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11750 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11752 /* Value is the number of screen lines needed to make all tool-bar
11753 items of frame F visible. The number of actual rows needed is
11754 returned in *N_ROWS if non-NULL. */
11756 static int
11757 tool_bar_lines_needed (struct frame *f, int *n_rows)
11759 struct window *w = XWINDOW (f->tool_bar_window);
11760 struct it it;
11761 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11762 the desired matrix, so use (unused) mode-line row as temporary row to
11763 avoid destroying the first tool-bar row. */
11764 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11766 /* Initialize an iterator for iteration over
11767 F->desired_tool_bar_string in the tool-bar window of frame F. */
11768 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11769 it.first_visible_x = 0;
11770 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11771 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11772 it.paragraph_embedding = L2R;
11774 while (!ITERATOR_AT_END_P (&it))
11776 clear_glyph_row (temp_row);
11777 it.glyph_row = temp_row;
11778 display_tool_bar_line (&it, -1);
11780 clear_glyph_row (temp_row);
11782 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11783 if (n_rows)
11784 *n_rows = it.vpos > 0 ? it.vpos : -1;
11786 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11790 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11791 0, 1, 0,
11792 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11793 If FRAME is nil or omitted, use the selected frame. */)
11794 (Lisp_Object frame)
11796 struct frame *f = decode_any_frame (frame);
11797 struct window *w;
11798 int nlines = 0;
11800 if (WINDOWP (f->tool_bar_window)
11801 && (w = XWINDOW (f->tool_bar_window),
11802 WINDOW_TOTAL_LINES (w) > 0))
11804 update_tool_bar (f, 1);
11805 if (f->n_tool_bar_items)
11807 build_desired_tool_bar_string (f);
11808 nlines = tool_bar_lines_needed (f, NULL);
11812 return make_number (nlines);
11816 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11817 height should be changed. */
11819 static int
11820 redisplay_tool_bar (struct frame *f)
11822 struct window *w;
11823 struct it it;
11824 struct glyph_row *row;
11826 #if defined (USE_GTK) || defined (HAVE_NS)
11827 if (FRAME_EXTERNAL_TOOL_BAR (f))
11828 update_frame_tool_bar (f);
11829 return 0;
11830 #endif
11832 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11833 do anything. This means you must start with tool-bar-lines
11834 non-zero to get the auto-sizing effect. Or in other words, you
11835 can turn off tool-bars by specifying tool-bar-lines zero. */
11836 if (!WINDOWP (f->tool_bar_window)
11837 || (w = XWINDOW (f->tool_bar_window),
11838 WINDOW_TOTAL_LINES (w) == 0))
11839 return 0;
11841 /* Set up an iterator for the tool-bar window. */
11842 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11843 it.first_visible_x = 0;
11844 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11845 row = it.glyph_row;
11847 /* Build a string that represents the contents of the tool-bar. */
11848 build_desired_tool_bar_string (f);
11849 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11850 /* FIXME: This should be controlled by a user option. But it
11851 doesn't make sense to have an R2L tool bar if the menu bar cannot
11852 be drawn also R2L, and making the menu bar R2L is tricky due
11853 toolkit-specific code that implements it. If an R2L tool bar is
11854 ever supported, display_tool_bar_line should also be augmented to
11855 call unproduce_glyphs like display_line and display_string
11856 do. */
11857 it.paragraph_embedding = L2R;
11859 if (f->n_tool_bar_rows == 0)
11861 int nlines;
11863 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11864 nlines != WINDOW_TOTAL_LINES (w)))
11866 Lisp_Object frame;
11867 int old_height = WINDOW_TOTAL_LINES (w);
11869 XSETFRAME (frame, f);
11870 Fmodify_frame_parameters (frame,
11871 Fcons (Fcons (Qtool_bar_lines,
11872 make_number (nlines)),
11873 Qnil));
11874 if (WINDOW_TOTAL_LINES (w) != old_height)
11876 clear_glyph_matrix (w->desired_matrix);
11877 fonts_changed_p = 1;
11878 return 1;
11883 /* Display as many lines as needed to display all tool-bar items. */
11885 if (f->n_tool_bar_rows > 0)
11887 int border, rows, height, extra;
11889 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11890 border = XINT (Vtool_bar_border);
11891 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11892 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11893 else if (EQ (Vtool_bar_border, Qborder_width))
11894 border = f->border_width;
11895 else
11896 border = 0;
11897 if (border < 0)
11898 border = 0;
11900 rows = f->n_tool_bar_rows;
11901 height = max (1, (it.last_visible_y - border) / rows);
11902 extra = it.last_visible_y - border - height * rows;
11904 while (it.current_y < it.last_visible_y)
11906 int h = 0;
11907 if (extra > 0 && rows-- > 0)
11909 h = (extra + rows - 1) / rows;
11910 extra -= h;
11912 display_tool_bar_line (&it, height + h);
11915 else
11917 while (it.current_y < it.last_visible_y)
11918 display_tool_bar_line (&it, 0);
11921 /* It doesn't make much sense to try scrolling in the tool-bar
11922 window, so don't do it. */
11923 w->desired_matrix->no_scrolling_p = 1;
11924 w->must_be_updated_p = 1;
11926 if (!NILP (Vauto_resize_tool_bars))
11928 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11929 int change_height_p = 0;
11931 /* If we couldn't display everything, change the tool-bar's
11932 height if there is room for more. */
11933 if (IT_STRING_CHARPOS (it) < it.end_charpos
11934 && it.current_y < max_tool_bar_height)
11935 change_height_p = 1;
11937 row = it.glyph_row - 1;
11939 /* If there are blank lines at the end, except for a partially
11940 visible blank line at the end that is smaller than
11941 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11942 if (!row->displays_text_p
11943 && row->height >= FRAME_LINE_HEIGHT (f))
11944 change_height_p = 1;
11946 /* If row displays tool-bar items, but is partially visible,
11947 change the tool-bar's height. */
11948 if (row->displays_text_p
11949 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11950 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11951 change_height_p = 1;
11953 /* Resize windows as needed by changing the `tool-bar-lines'
11954 frame parameter. */
11955 if (change_height_p)
11957 Lisp_Object frame;
11958 int old_height = WINDOW_TOTAL_LINES (w);
11959 int nrows;
11960 int nlines = tool_bar_lines_needed (f, &nrows);
11962 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11963 && !f->minimize_tool_bar_window_p)
11964 ? (nlines > old_height)
11965 : (nlines != old_height));
11966 f->minimize_tool_bar_window_p = 0;
11968 if (change_height_p)
11970 XSETFRAME (frame, f);
11971 Fmodify_frame_parameters (frame,
11972 Fcons (Fcons (Qtool_bar_lines,
11973 make_number (nlines)),
11974 Qnil));
11975 if (WINDOW_TOTAL_LINES (w) != old_height)
11977 clear_glyph_matrix (w->desired_matrix);
11978 f->n_tool_bar_rows = nrows;
11979 fonts_changed_p = 1;
11980 return 1;
11986 f->minimize_tool_bar_window_p = 0;
11987 return 0;
11991 /* Get information about the tool-bar item which is displayed in GLYPH
11992 on frame F. Return in *PROP_IDX the index where tool-bar item
11993 properties start in F->tool_bar_items. Value is zero if
11994 GLYPH doesn't display a tool-bar item. */
11996 static int
11997 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11999 Lisp_Object prop;
12000 int success_p;
12001 int charpos;
12003 /* This function can be called asynchronously, which means we must
12004 exclude any possibility that Fget_text_property signals an
12005 error. */
12006 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12007 charpos = max (0, charpos);
12009 /* Get the text property `menu-item' at pos. The value of that
12010 property is the start index of this item's properties in
12011 F->tool_bar_items. */
12012 prop = Fget_text_property (make_number (charpos),
12013 Qmenu_item, f->current_tool_bar_string);
12014 if (INTEGERP (prop))
12016 *prop_idx = XINT (prop);
12017 success_p = 1;
12019 else
12020 success_p = 0;
12022 return success_p;
12026 /* Get information about the tool-bar item at position X/Y on frame F.
12027 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12028 the current matrix of the tool-bar window of F, or NULL if not
12029 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12030 item in F->tool_bar_items. Value is
12032 -1 if X/Y is not on a tool-bar item
12033 0 if X/Y is on the same item that was highlighted before.
12034 1 otherwise. */
12036 static int
12037 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12038 int *hpos, int *vpos, int *prop_idx)
12040 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12041 struct window *w = XWINDOW (f->tool_bar_window);
12042 int area;
12044 /* Find the glyph under X/Y. */
12045 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12046 if (*glyph == NULL)
12047 return -1;
12049 /* Get the start of this tool-bar item's properties in
12050 f->tool_bar_items. */
12051 if (!tool_bar_item_info (f, *glyph, prop_idx))
12052 return -1;
12054 /* Is mouse on the highlighted item? */
12055 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12056 && *vpos >= hlinfo->mouse_face_beg_row
12057 && *vpos <= hlinfo->mouse_face_end_row
12058 && (*vpos > hlinfo->mouse_face_beg_row
12059 || *hpos >= hlinfo->mouse_face_beg_col)
12060 && (*vpos < hlinfo->mouse_face_end_row
12061 || *hpos < hlinfo->mouse_face_end_col
12062 || hlinfo->mouse_face_past_end))
12063 return 0;
12065 return 1;
12069 /* EXPORT:
12070 Handle mouse button event on the tool-bar of frame F, at
12071 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12072 0 for button release. MODIFIERS is event modifiers for button
12073 release. */
12075 void
12076 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12077 int modifiers)
12079 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12080 struct window *w = XWINDOW (f->tool_bar_window);
12081 int hpos, vpos, prop_idx;
12082 struct glyph *glyph;
12083 Lisp_Object enabled_p;
12085 /* If not on the highlighted tool-bar item, return. */
12086 frame_to_window_pixel_xy (w, &x, &y);
12087 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12088 return;
12090 /* If item is disabled, do nothing. */
12091 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12092 if (NILP (enabled_p))
12093 return;
12095 if (down_p)
12097 /* Show item in pressed state. */
12098 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12099 last_tool_bar_item = prop_idx;
12101 else
12103 Lisp_Object key, frame;
12104 struct input_event event;
12105 EVENT_INIT (event);
12107 /* Show item in released state. */
12108 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12110 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12112 XSETFRAME (frame, f);
12113 event.kind = TOOL_BAR_EVENT;
12114 event.frame_or_window = frame;
12115 event.arg = frame;
12116 kbd_buffer_store_event (&event);
12118 event.kind = TOOL_BAR_EVENT;
12119 event.frame_or_window = frame;
12120 event.arg = key;
12121 event.modifiers = modifiers;
12122 kbd_buffer_store_event (&event);
12123 last_tool_bar_item = -1;
12128 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12129 tool-bar window-relative coordinates X/Y. Called from
12130 note_mouse_highlight. */
12132 static void
12133 note_tool_bar_highlight (struct frame *f, int x, int y)
12135 Lisp_Object window = f->tool_bar_window;
12136 struct window *w = XWINDOW (window);
12137 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12138 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12139 int hpos, vpos;
12140 struct glyph *glyph;
12141 struct glyph_row *row;
12142 int i;
12143 Lisp_Object enabled_p;
12144 int prop_idx;
12145 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12146 int mouse_down_p, rc;
12148 /* Function note_mouse_highlight is called with negative X/Y
12149 values when mouse moves outside of the frame. */
12150 if (x <= 0 || y <= 0)
12152 clear_mouse_face (hlinfo);
12153 return;
12156 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12157 if (rc < 0)
12159 /* Not on tool-bar item. */
12160 clear_mouse_face (hlinfo);
12161 return;
12163 else if (rc == 0)
12164 /* On same tool-bar item as before. */
12165 goto set_help_echo;
12167 clear_mouse_face (hlinfo);
12169 /* Mouse is down, but on different tool-bar item? */
12170 mouse_down_p = (dpyinfo->grabbed
12171 && f == last_mouse_frame
12172 && FRAME_LIVE_P (f));
12173 if (mouse_down_p
12174 && last_tool_bar_item != prop_idx)
12175 return;
12177 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12179 /* If tool-bar item is not enabled, don't highlight it. */
12180 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12181 if (!NILP (enabled_p))
12183 /* Compute the x-position of the glyph. In front and past the
12184 image is a space. We include this in the highlighted area. */
12185 row = MATRIX_ROW (w->current_matrix, vpos);
12186 for (i = x = 0; i < hpos; ++i)
12187 x += row->glyphs[TEXT_AREA][i].pixel_width;
12189 /* Record this as the current active region. */
12190 hlinfo->mouse_face_beg_col = hpos;
12191 hlinfo->mouse_face_beg_row = vpos;
12192 hlinfo->mouse_face_beg_x = x;
12193 hlinfo->mouse_face_beg_y = row->y;
12194 hlinfo->mouse_face_past_end = 0;
12196 hlinfo->mouse_face_end_col = hpos + 1;
12197 hlinfo->mouse_face_end_row = vpos;
12198 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12199 hlinfo->mouse_face_end_y = row->y;
12200 hlinfo->mouse_face_window = window;
12201 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12203 /* Display it as active. */
12204 show_mouse_face (hlinfo, draw);
12207 set_help_echo:
12209 /* Set help_echo_string to a help string to display for this tool-bar item.
12210 XTread_socket does the rest. */
12211 help_echo_object = help_echo_window = Qnil;
12212 help_echo_pos = -1;
12213 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12214 if (NILP (help_echo_string))
12215 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12218 #endif /* HAVE_WINDOW_SYSTEM */
12222 /************************************************************************
12223 Horizontal scrolling
12224 ************************************************************************/
12226 static int hscroll_window_tree (Lisp_Object);
12227 static int hscroll_windows (Lisp_Object);
12229 /* For all leaf windows in the window tree rooted at WINDOW, set their
12230 hscroll value so that PT is (i) visible in the window, and (ii) so
12231 that it is not within a certain margin at the window's left and
12232 right border. Value is non-zero if any window's hscroll has been
12233 changed. */
12235 static int
12236 hscroll_window_tree (Lisp_Object window)
12238 int hscrolled_p = 0;
12239 int hscroll_relative_p = FLOATP (Vhscroll_step);
12240 int hscroll_step_abs = 0;
12241 double hscroll_step_rel = 0;
12243 if (hscroll_relative_p)
12245 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12246 if (hscroll_step_rel < 0)
12248 hscroll_relative_p = 0;
12249 hscroll_step_abs = 0;
12252 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12254 hscroll_step_abs = XINT (Vhscroll_step);
12255 if (hscroll_step_abs < 0)
12256 hscroll_step_abs = 0;
12258 else
12259 hscroll_step_abs = 0;
12261 while (WINDOWP (window))
12263 struct window *w = XWINDOW (window);
12265 if (WINDOWP (w->hchild))
12266 hscrolled_p |= hscroll_window_tree (w->hchild);
12267 else if (WINDOWP (w->vchild))
12268 hscrolled_p |= hscroll_window_tree (w->vchild);
12269 else if (w->cursor.vpos >= 0)
12271 int h_margin;
12272 int text_area_width;
12273 struct glyph_row *current_cursor_row
12274 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12275 struct glyph_row *desired_cursor_row
12276 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12277 struct glyph_row *cursor_row
12278 = (desired_cursor_row->enabled_p
12279 ? desired_cursor_row
12280 : current_cursor_row);
12281 int row_r2l_p = cursor_row->reversed_p;
12283 text_area_width = window_box_width (w, TEXT_AREA);
12285 /* Scroll when cursor is inside this scroll margin. */
12286 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12288 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12289 /* For left-to-right rows, hscroll when cursor is either
12290 (i) inside the right hscroll margin, or (ii) if it is
12291 inside the left margin and the window is already
12292 hscrolled. */
12293 && ((!row_r2l_p
12294 && ((w->hscroll
12295 && w->cursor.x <= h_margin)
12296 || (cursor_row->enabled_p
12297 && cursor_row->truncated_on_right_p
12298 && (w->cursor.x >= text_area_width - h_margin))))
12299 /* For right-to-left rows, the logic is similar,
12300 except that rules for scrolling to left and right
12301 are reversed. E.g., if cursor.x <= h_margin, we
12302 need to hscroll "to the right" unconditionally,
12303 and that will scroll the screen to the left so as
12304 to reveal the next portion of the row. */
12305 || (row_r2l_p
12306 && ((cursor_row->enabled_p
12307 /* FIXME: It is confusing to set the
12308 truncated_on_right_p flag when R2L rows
12309 are actually truncated on the left. */
12310 && cursor_row->truncated_on_right_p
12311 && w->cursor.x <= h_margin)
12312 || (w->hscroll
12313 && (w->cursor.x >= text_area_width - h_margin))))))
12315 struct it it;
12316 ptrdiff_t hscroll;
12317 struct buffer *saved_current_buffer;
12318 ptrdiff_t pt;
12319 int wanted_x;
12321 /* Find point in a display of infinite width. */
12322 saved_current_buffer = current_buffer;
12323 current_buffer = XBUFFER (w->buffer);
12325 if (w == XWINDOW (selected_window))
12326 pt = PT;
12327 else
12328 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12330 /* Move iterator to pt starting at cursor_row->start in
12331 a line with infinite width. */
12332 init_to_row_start (&it, w, cursor_row);
12333 it.last_visible_x = INFINITY;
12334 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12335 current_buffer = saved_current_buffer;
12337 /* Position cursor in window. */
12338 if (!hscroll_relative_p && hscroll_step_abs == 0)
12339 hscroll = max (0, (it.current_x
12340 - (ITERATOR_AT_END_OF_LINE_P (&it)
12341 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12342 : (text_area_width / 2))))
12343 / FRAME_COLUMN_WIDTH (it.f);
12344 else if ((!row_r2l_p
12345 && w->cursor.x >= text_area_width - h_margin)
12346 || (row_r2l_p && w->cursor.x <= h_margin))
12348 if (hscroll_relative_p)
12349 wanted_x = text_area_width * (1 - hscroll_step_rel)
12350 - h_margin;
12351 else
12352 wanted_x = text_area_width
12353 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12354 - h_margin;
12355 hscroll
12356 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12358 else
12360 if (hscroll_relative_p)
12361 wanted_x = text_area_width * hscroll_step_rel
12362 + h_margin;
12363 else
12364 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12365 + h_margin;
12366 hscroll
12367 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12369 hscroll = max (hscroll, w->min_hscroll);
12371 /* Don't prevent redisplay optimizations if hscroll
12372 hasn't changed, as it will unnecessarily slow down
12373 redisplay. */
12374 if (w->hscroll != hscroll)
12376 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12377 w->hscroll = hscroll;
12378 hscrolled_p = 1;
12383 window = w->next;
12386 /* Value is non-zero if hscroll of any leaf window has been changed. */
12387 return hscrolled_p;
12391 /* Set hscroll so that cursor is visible and not inside horizontal
12392 scroll margins for all windows in the tree rooted at WINDOW. See
12393 also hscroll_window_tree above. Value is non-zero if any window's
12394 hscroll has been changed. If it has, desired matrices on the frame
12395 of WINDOW are cleared. */
12397 static int
12398 hscroll_windows (Lisp_Object window)
12400 int hscrolled_p = hscroll_window_tree (window);
12401 if (hscrolled_p)
12402 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12403 return hscrolled_p;
12408 /************************************************************************
12409 Redisplay
12410 ************************************************************************/
12412 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12413 to a non-zero value. This is sometimes handy to have in a debugger
12414 session. */
12416 #ifdef GLYPH_DEBUG
12418 /* First and last unchanged row for try_window_id. */
12420 static int debug_first_unchanged_at_end_vpos;
12421 static int debug_last_unchanged_at_beg_vpos;
12423 /* Delta vpos and y. */
12425 static int debug_dvpos, debug_dy;
12427 /* Delta in characters and bytes for try_window_id. */
12429 static ptrdiff_t debug_delta, debug_delta_bytes;
12431 /* Values of window_end_pos and window_end_vpos at the end of
12432 try_window_id. */
12434 static ptrdiff_t debug_end_vpos;
12436 /* Append a string to W->desired_matrix->method. FMT is a printf
12437 format string. If trace_redisplay_p is non-zero also printf the
12438 resulting string to stderr. */
12440 static void debug_method_add (struct window *, char const *, ...)
12441 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12443 static void
12444 debug_method_add (struct window *w, char const *fmt, ...)
12446 char *method = w->desired_matrix->method;
12447 int len = strlen (method);
12448 int size = sizeof w->desired_matrix->method;
12449 int remaining = size - len - 1;
12450 va_list ap;
12452 if (len && remaining)
12454 method[len] = '|';
12455 --remaining, ++len;
12458 va_start (ap, fmt);
12459 vsnprintf (method + len, remaining + 1, fmt, ap);
12460 va_end (ap);
12462 if (trace_redisplay_p)
12463 fprintf (stderr, "%p (%s): %s\n",
12465 ((BUFFERP (w->buffer)
12466 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12467 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12468 : "no buffer"),
12469 method + len);
12472 #endif /* GLYPH_DEBUG */
12475 /* Value is non-zero if all changes in window W, which displays
12476 current_buffer, are in the text between START and END. START is a
12477 buffer position, END is given as a distance from Z. Used in
12478 redisplay_internal for display optimization. */
12480 static int
12481 text_outside_line_unchanged_p (struct window *w,
12482 ptrdiff_t start, ptrdiff_t end)
12484 int unchanged_p = 1;
12486 /* If text or overlays have changed, see where. */
12487 if (window_outdated (w))
12489 /* Gap in the line? */
12490 if (GPT < start || Z - GPT < end)
12491 unchanged_p = 0;
12493 /* Changes start in front of the line, or end after it? */
12494 if (unchanged_p
12495 && (BEG_UNCHANGED < start - 1
12496 || END_UNCHANGED < end))
12497 unchanged_p = 0;
12499 /* If selective display, can't optimize if changes start at the
12500 beginning of the line. */
12501 if (unchanged_p
12502 && INTEGERP (BVAR (current_buffer, selective_display))
12503 && XINT (BVAR (current_buffer, selective_display)) > 0
12504 && (BEG_UNCHANGED < start || GPT <= start))
12505 unchanged_p = 0;
12507 /* If there are overlays at the start or end of the line, these
12508 may have overlay strings with newlines in them. A change at
12509 START, for instance, may actually concern the display of such
12510 overlay strings as well, and they are displayed on different
12511 lines. So, quickly rule out this case. (For the future, it
12512 might be desirable to implement something more telling than
12513 just BEG/END_UNCHANGED.) */
12514 if (unchanged_p)
12516 if (BEG + BEG_UNCHANGED == start
12517 && overlay_touches_p (start))
12518 unchanged_p = 0;
12519 if (END_UNCHANGED == end
12520 && overlay_touches_p (Z - end))
12521 unchanged_p = 0;
12524 /* Under bidi reordering, adding or deleting a character in the
12525 beginning of a paragraph, before the first strong directional
12526 character, can change the base direction of the paragraph (unless
12527 the buffer specifies a fixed paragraph direction), which will
12528 require to redisplay the whole paragraph. It might be worthwhile
12529 to find the paragraph limits and widen the range of redisplayed
12530 lines to that, but for now just give up this optimization. */
12531 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12532 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12533 unchanged_p = 0;
12536 return unchanged_p;
12540 /* Do a frame update, taking possible shortcuts into account. This is
12541 the main external entry point for redisplay.
12543 If the last redisplay displayed an echo area message and that message
12544 is no longer requested, we clear the echo area or bring back the
12545 mini-buffer if that is in use. */
12547 void
12548 redisplay (void)
12550 redisplay_internal ();
12554 static Lisp_Object
12555 overlay_arrow_string_or_property (Lisp_Object var)
12557 Lisp_Object val;
12559 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12560 return val;
12562 return Voverlay_arrow_string;
12565 /* Return 1 if there are any overlay-arrows in current_buffer. */
12566 static int
12567 overlay_arrow_in_current_buffer_p (void)
12569 Lisp_Object vlist;
12571 for (vlist = Voverlay_arrow_variable_list;
12572 CONSP (vlist);
12573 vlist = XCDR (vlist))
12575 Lisp_Object var = XCAR (vlist);
12576 Lisp_Object val;
12578 if (!SYMBOLP (var))
12579 continue;
12580 val = find_symbol_value (var);
12581 if (MARKERP (val)
12582 && current_buffer == XMARKER (val)->buffer)
12583 return 1;
12585 return 0;
12589 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12590 has changed. */
12592 static int
12593 overlay_arrows_changed_p (void)
12595 Lisp_Object vlist;
12597 for (vlist = Voverlay_arrow_variable_list;
12598 CONSP (vlist);
12599 vlist = XCDR (vlist))
12601 Lisp_Object var = XCAR (vlist);
12602 Lisp_Object val, pstr;
12604 if (!SYMBOLP (var))
12605 continue;
12606 val = find_symbol_value (var);
12607 if (!MARKERP (val))
12608 continue;
12609 if (! EQ (COERCE_MARKER (val),
12610 Fget (var, Qlast_arrow_position))
12611 || ! (pstr = overlay_arrow_string_or_property (var),
12612 EQ (pstr, Fget (var, Qlast_arrow_string))))
12613 return 1;
12615 return 0;
12618 /* Mark overlay arrows to be updated on next redisplay. */
12620 static void
12621 update_overlay_arrows (int up_to_date)
12623 Lisp_Object vlist;
12625 for (vlist = Voverlay_arrow_variable_list;
12626 CONSP (vlist);
12627 vlist = XCDR (vlist))
12629 Lisp_Object var = XCAR (vlist);
12631 if (!SYMBOLP (var))
12632 continue;
12634 if (up_to_date > 0)
12636 Lisp_Object val = find_symbol_value (var);
12637 Fput (var, Qlast_arrow_position,
12638 COERCE_MARKER (val));
12639 Fput (var, Qlast_arrow_string,
12640 overlay_arrow_string_or_property (var));
12642 else if (up_to_date < 0
12643 || !NILP (Fget (var, Qlast_arrow_position)))
12645 Fput (var, Qlast_arrow_position, Qt);
12646 Fput (var, Qlast_arrow_string, Qt);
12652 /* Return overlay arrow string to display at row.
12653 Return integer (bitmap number) for arrow bitmap in left fringe.
12654 Return nil if no overlay arrow. */
12656 static Lisp_Object
12657 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12659 Lisp_Object vlist;
12661 for (vlist = Voverlay_arrow_variable_list;
12662 CONSP (vlist);
12663 vlist = XCDR (vlist))
12665 Lisp_Object var = XCAR (vlist);
12666 Lisp_Object val;
12668 if (!SYMBOLP (var))
12669 continue;
12671 val = find_symbol_value (var);
12673 if (MARKERP (val)
12674 && current_buffer == XMARKER (val)->buffer
12675 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12677 if (FRAME_WINDOW_P (it->f)
12678 /* FIXME: if ROW->reversed_p is set, this should test
12679 the right fringe, not the left one. */
12680 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12682 #ifdef HAVE_WINDOW_SYSTEM
12683 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12685 int fringe_bitmap;
12686 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12687 return make_number (fringe_bitmap);
12689 #endif
12690 return make_number (-1); /* Use default arrow bitmap. */
12692 return overlay_arrow_string_or_property (var);
12696 return Qnil;
12699 /* Return 1 if point moved out of or into a composition. Otherwise
12700 return 0. PREV_BUF and PREV_PT are the last point buffer and
12701 position. BUF and PT are the current point buffer and position. */
12703 static int
12704 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12705 struct buffer *buf, ptrdiff_t pt)
12707 ptrdiff_t start, end;
12708 Lisp_Object prop;
12709 Lisp_Object buffer;
12711 XSETBUFFER (buffer, buf);
12712 /* Check a composition at the last point if point moved within the
12713 same buffer. */
12714 if (prev_buf == buf)
12716 if (prev_pt == pt)
12717 /* Point didn't move. */
12718 return 0;
12720 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12721 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12722 && COMPOSITION_VALID_P (start, end, prop)
12723 && start < prev_pt && end > prev_pt)
12724 /* The last point was within the composition. Return 1 iff
12725 point moved out of the composition. */
12726 return (pt <= start || pt >= end);
12729 /* Check a composition at the current point. */
12730 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12731 && find_composition (pt, -1, &start, &end, &prop, buffer)
12732 && COMPOSITION_VALID_P (start, end, prop)
12733 && start < pt && end > pt);
12737 /* Reconsider the setting of B->clip_changed which is displayed
12738 in window W. */
12740 static void
12741 reconsider_clip_changes (struct window *w, struct buffer *b)
12743 if (b->clip_changed
12744 && w->window_end_valid
12745 && w->current_matrix->buffer == b
12746 && w->current_matrix->zv == BUF_ZV (b)
12747 && w->current_matrix->begv == BUF_BEGV (b))
12748 b->clip_changed = 0;
12750 /* If display wasn't paused, and W is not a tool bar window, see if
12751 point has been moved into or out of a composition. In that case,
12752 we set b->clip_changed to 1 to force updating the screen. If
12753 b->clip_changed has already been set to 1, we can skip this
12754 check. */
12755 if (!b->clip_changed && BUFFERP (w->buffer) && w->window_end_valid)
12757 ptrdiff_t pt;
12759 if (w == XWINDOW (selected_window))
12760 pt = PT;
12761 else
12762 pt = marker_position (w->pointm);
12764 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12765 || pt != w->last_point)
12766 && check_point_in_composition (w->current_matrix->buffer,
12767 w->last_point,
12768 XBUFFER (w->buffer), pt))
12769 b->clip_changed = 1;
12774 #define STOP_POLLING \
12775 do { if (! polling_stopped_here) stop_polling (); \
12776 polling_stopped_here = 1; } while (0)
12778 #define RESUME_POLLING \
12779 do { if (polling_stopped_here) start_polling (); \
12780 polling_stopped_here = 0; } while (0)
12783 /* Perhaps in the future avoid recentering windows if it
12784 is not necessary; currently that causes some problems. */
12786 static void
12787 redisplay_internal (void)
12789 struct window *w = XWINDOW (selected_window);
12790 struct window *sw;
12791 struct frame *fr;
12792 int pending;
12793 int must_finish = 0;
12794 struct text_pos tlbufpos, tlendpos;
12795 int number_of_visible_frames;
12796 ptrdiff_t count, count1;
12797 struct frame *sf;
12798 int polling_stopped_here = 0;
12799 Lisp_Object tail, frame;
12800 struct backtrace backtrace;
12802 /* Non-zero means redisplay has to consider all windows on all
12803 frames. Zero means, only selected_window is considered. */
12804 int consider_all_windows_p;
12806 /* Non-zero means redisplay has to redisplay the miniwindow. */
12807 int update_miniwindow_p = 0;
12809 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12811 /* No redisplay if running in batch mode or frame is not yet fully
12812 initialized, or redisplay is explicitly turned off by setting
12813 Vinhibit_redisplay. */
12814 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12815 || !NILP (Vinhibit_redisplay))
12816 return;
12818 /* Don't examine these until after testing Vinhibit_redisplay.
12819 When Emacs is shutting down, perhaps because its connection to
12820 X has dropped, we should not look at them at all. */
12821 fr = XFRAME (w->frame);
12822 sf = SELECTED_FRAME ();
12824 if (!fr->glyphs_initialized_p)
12825 return;
12827 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12828 if (popup_activated ())
12829 return;
12830 #endif
12832 /* I don't think this happens but let's be paranoid. */
12833 if (redisplaying_p)
12834 return;
12836 /* Record a function that clears redisplaying_p
12837 when we leave this function. */
12838 count = SPECPDL_INDEX ();
12839 record_unwind_protect (unwind_redisplay, selected_frame);
12840 redisplaying_p = 1;
12841 specbind (Qinhibit_free_realized_faces, Qnil);
12843 /* Record this function, so it appears on the profiler's backtraces. */
12844 backtrace.next = backtrace_list;
12845 backtrace.function = Qredisplay_internal;
12846 backtrace.args = &Qnil;
12847 backtrace.nargs = 0;
12848 backtrace.debug_on_exit = 0;
12849 backtrace_list = &backtrace;
12851 FOR_EACH_FRAME (tail, frame)
12852 XFRAME (frame)->already_hscrolled_p = 0;
12854 retry:
12855 /* Remember the currently selected window. */
12856 sw = w;
12858 pending = 0;
12859 reconsider_clip_changes (w, current_buffer);
12860 last_escape_glyph_frame = NULL;
12861 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12862 last_glyphless_glyph_frame = NULL;
12863 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12865 /* If new fonts have been loaded that make a glyph matrix adjustment
12866 necessary, do it. */
12867 if (fonts_changed_p)
12869 adjust_glyphs (NULL);
12870 ++windows_or_buffers_changed;
12871 fonts_changed_p = 0;
12874 /* If face_change_count is non-zero, init_iterator will free all
12875 realized faces, which includes the faces referenced from current
12876 matrices. So, we can't reuse current matrices in this case. */
12877 if (face_change_count)
12878 ++windows_or_buffers_changed;
12880 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12881 && FRAME_TTY (sf)->previous_frame != sf)
12883 /* Since frames on a single ASCII terminal share the same
12884 display area, displaying a different frame means redisplay
12885 the whole thing. */
12886 windows_or_buffers_changed++;
12887 SET_FRAME_GARBAGED (sf);
12888 #ifndef DOS_NT
12889 set_tty_color_mode (FRAME_TTY (sf), sf);
12890 #endif
12891 FRAME_TTY (sf)->previous_frame = sf;
12894 /* Set the visible flags for all frames. Do this before checking for
12895 resized or garbaged frames; they want to know if their frames are
12896 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12897 number_of_visible_frames = 0;
12899 FOR_EACH_FRAME (tail, frame)
12901 struct frame *f = XFRAME (frame);
12903 if (FRAME_VISIBLE_P (f))
12904 ++number_of_visible_frames;
12905 clear_desired_matrices (f);
12908 /* Notice any pending interrupt request to change frame size. */
12909 do_pending_window_change (1);
12911 /* do_pending_window_change could change the selected_window due to
12912 frame resizing which makes the selected window too small. */
12913 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12915 sw = w;
12916 reconsider_clip_changes (w, current_buffer);
12919 /* Clear frames marked as garbaged. */
12920 clear_garbaged_frames ();
12922 /* Build menubar and tool-bar items. */
12923 if (NILP (Vmemory_full))
12924 prepare_menu_bars ();
12926 if (windows_or_buffers_changed)
12927 update_mode_lines++;
12929 /* Detect case that we need to write or remove a star in the mode line. */
12930 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
12932 w->update_mode_line = 1;
12933 if (buffer_shared_and_changed ())
12934 update_mode_lines++;
12937 /* Avoid invocation of point motion hooks by `current_column' below. */
12938 count1 = SPECPDL_INDEX ();
12939 specbind (Qinhibit_point_motion_hooks, Qt);
12941 if (mode_line_update_needed (w))
12942 w->update_mode_line = 1;
12944 unbind_to (count1, Qnil);
12946 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12948 consider_all_windows_p = (update_mode_lines
12949 || buffer_shared_and_changed ()
12950 || cursor_type_changed);
12952 /* If specs for an arrow have changed, do thorough redisplay
12953 to ensure we remove any arrow that should no longer exist. */
12954 if (overlay_arrows_changed_p ())
12955 consider_all_windows_p = windows_or_buffers_changed = 1;
12957 /* Normally the message* functions will have already displayed and
12958 updated the echo area, but the frame may have been trashed, or
12959 the update may have been preempted, so display the echo area
12960 again here. Checking message_cleared_p captures the case that
12961 the echo area should be cleared. */
12962 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12963 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12964 || (message_cleared_p
12965 && minibuf_level == 0
12966 /* If the mini-window is currently selected, this means the
12967 echo-area doesn't show through. */
12968 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12970 int window_height_changed_p = echo_area_display (0);
12972 if (message_cleared_p)
12973 update_miniwindow_p = 1;
12975 must_finish = 1;
12977 /* If we don't display the current message, don't clear the
12978 message_cleared_p flag, because, if we did, we wouldn't clear
12979 the echo area in the next redisplay which doesn't preserve
12980 the echo area. */
12981 if (!display_last_displayed_message_p)
12982 message_cleared_p = 0;
12984 if (fonts_changed_p)
12985 goto retry;
12986 else if (window_height_changed_p)
12988 consider_all_windows_p = 1;
12989 ++update_mode_lines;
12990 ++windows_or_buffers_changed;
12992 /* If window configuration was changed, frames may have been
12993 marked garbaged. Clear them or we will experience
12994 surprises wrt scrolling. */
12995 clear_garbaged_frames ();
12998 else if (EQ (selected_window, minibuf_window)
12999 && (current_buffer->clip_changed || window_outdated (w))
13000 && resize_mini_window (w, 0))
13002 /* Resized active mini-window to fit the size of what it is
13003 showing if its contents might have changed. */
13004 must_finish = 1;
13005 /* FIXME: this causes all frames to be updated, which seems unnecessary
13006 since only the current frame needs to be considered. This function
13007 needs to be rewritten with two variables, consider_all_windows and
13008 consider_all_frames. */
13009 consider_all_windows_p = 1;
13010 ++windows_or_buffers_changed;
13011 ++update_mode_lines;
13013 /* If window configuration was changed, frames may have been
13014 marked garbaged. Clear them or we will experience
13015 surprises wrt scrolling. */
13016 clear_garbaged_frames ();
13019 /* If showing the region, and mark has changed, we must redisplay
13020 the whole window. The assignment to this_line_start_pos prevents
13021 the optimization directly below this if-statement. */
13022 if (((!NILP (Vtransient_mark_mode)
13023 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13024 != (w->region_showing > 0))
13025 || (w->region_showing
13026 && w->region_showing
13027 != XINT (Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13028 CHARPOS (this_line_start_pos) = 0;
13030 /* Optimize the case that only the line containing the cursor in the
13031 selected window has changed. Variables starting with this_ are
13032 set in display_line and record information about the line
13033 containing the cursor. */
13034 tlbufpos = this_line_start_pos;
13035 tlendpos = this_line_end_pos;
13036 if (!consider_all_windows_p
13037 && CHARPOS (tlbufpos) > 0
13038 && !w->update_mode_line
13039 && !current_buffer->clip_changed
13040 && !current_buffer->prevent_redisplay_optimizations_p
13041 && FRAME_VISIBLE_P (XFRAME (w->frame))
13042 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13043 /* Make sure recorded data applies to current buffer, etc. */
13044 && this_line_buffer == current_buffer
13045 && current_buffer == XBUFFER (w->buffer)
13046 && !w->force_start
13047 && !w->optional_new_start
13048 /* Point must be on the line that we have info recorded about. */
13049 && PT >= CHARPOS (tlbufpos)
13050 && PT <= Z - CHARPOS (tlendpos)
13051 /* All text outside that line, including its final newline,
13052 must be unchanged. */
13053 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13054 CHARPOS (tlendpos)))
13056 if (CHARPOS (tlbufpos) > BEGV
13057 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13058 && (CHARPOS (tlbufpos) == ZV
13059 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13060 /* Former continuation line has disappeared by becoming empty. */
13061 goto cancel;
13062 else if (window_outdated (w) || MINI_WINDOW_P (w))
13064 /* We have to handle the case of continuation around a
13065 wide-column character (see the comment in indent.c around
13066 line 1340).
13068 For instance, in the following case:
13070 -------- Insert --------
13071 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13072 J_I_ ==> J_I_ `^^' are cursors.
13073 ^^ ^^
13074 -------- --------
13076 As we have to redraw the line above, we cannot use this
13077 optimization. */
13079 struct it it;
13080 int line_height_before = this_line_pixel_height;
13082 /* Note that start_display will handle the case that the
13083 line starting at tlbufpos is a continuation line. */
13084 start_display (&it, w, tlbufpos);
13086 /* Implementation note: It this still necessary? */
13087 if (it.current_x != this_line_start_x)
13088 goto cancel;
13090 TRACE ((stderr, "trying display optimization 1\n"));
13091 w->cursor.vpos = -1;
13092 overlay_arrow_seen = 0;
13093 it.vpos = this_line_vpos;
13094 it.current_y = this_line_y;
13095 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13096 display_line (&it);
13098 /* If line contains point, is not continued,
13099 and ends at same distance from eob as before, we win. */
13100 if (w->cursor.vpos >= 0
13101 /* Line is not continued, otherwise this_line_start_pos
13102 would have been set to 0 in display_line. */
13103 && CHARPOS (this_line_start_pos)
13104 /* Line ends as before. */
13105 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13106 /* Line has same height as before. Otherwise other lines
13107 would have to be shifted up or down. */
13108 && this_line_pixel_height == line_height_before)
13110 /* If this is not the window's last line, we must adjust
13111 the charstarts of the lines below. */
13112 if (it.current_y < it.last_visible_y)
13114 struct glyph_row *row
13115 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13116 ptrdiff_t delta, delta_bytes;
13118 /* We used to distinguish between two cases here,
13119 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13120 when the line ends in a newline or the end of the
13121 buffer's accessible portion. But both cases did
13122 the same, so they were collapsed. */
13123 delta = (Z
13124 - CHARPOS (tlendpos)
13125 - MATRIX_ROW_START_CHARPOS (row));
13126 delta_bytes = (Z_BYTE
13127 - BYTEPOS (tlendpos)
13128 - MATRIX_ROW_START_BYTEPOS (row));
13130 increment_matrix_positions (w->current_matrix,
13131 this_line_vpos + 1,
13132 w->current_matrix->nrows,
13133 delta, delta_bytes);
13136 /* If this row displays text now but previously didn't,
13137 or vice versa, w->window_end_vpos may have to be
13138 adjusted. */
13139 if ((it.glyph_row - 1)->displays_text_p)
13141 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13142 wset_window_end_vpos (w, make_number (this_line_vpos));
13144 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13145 && this_line_vpos > 0)
13146 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13147 w->window_end_valid = 0;
13149 /* Update hint: No need to try to scroll in update_window. */
13150 w->desired_matrix->no_scrolling_p = 1;
13152 #ifdef GLYPH_DEBUG
13153 *w->desired_matrix->method = 0;
13154 debug_method_add (w, "optimization 1");
13155 #endif
13156 #ifdef HAVE_WINDOW_SYSTEM
13157 update_window_fringes (w, 0);
13158 #endif
13159 goto update;
13161 else
13162 goto cancel;
13164 else if (/* Cursor position hasn't changed. */
13165 PT == w->last_point
13166 /* Make sure the cursor was last displayed
13167 in this window. Otherwise we have to reposition it. */
13168 && 0 <= w->cursor.vpos
13169 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13171 if (!must_finish)
13173 do_pending_window_change (1);
13174 /* If selected_window changed, redisplay again. */
13175 if (WINDOWP (selected_window)
13176 && (w = XWINDOW (selected_window)) != sw)
13177 goto retry;
13179 /* We used to always goto end_of_redisplay here, but this
13180 isn't enough if we have a blinking cursor. */
13181 if (w->cursor_off_p == w->last_cursor_off_p)
13182 goto end_of_redisplay;
13184 goto update;
13186 /* If highlighting the region, or if the cursor is in the echo area,
13187 then we can't just move the cursor. */
13188 else if (! (!NILP (Vtransient_mark_mode)
13189 && !NILP (BVAR (current_buffer, mark_active)))
13190 && (EQ (selected_window,
13191 BVAR (current_buffer, last_selected_window))
13192 || highlight_nonselected_windows)
13193 && !w->region_showing
13194 && NILP (Vshow_trailing_whitespace)
13195 && !cursor_in_echo_area)
13197 struct it it;
13198 struct glyph_row *row;
13200 /* Skip from tlbufpos to PT and see where it is. Note that
13201 PT may be in invisible text. If so, we will end at the
13202 next visible position. */
13203 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13204 NULL, DEFAULT_FACE_ID);
13205 it.current_x = this_line_start_x;
13206 it.current_y = this_line_y;
13207 it.vpos = this_line_vpos;
13209 /* The call to move_it_to stops in front of PT, but
13210 moves over before-strings. */
13211 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13213 if (it.vpos == this_line_vpos
13214 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13215 row->enabled_p))
13217 eassert (this_line_vpos == it.vpos);
13218 eassert (this_line_y == it.current_y);
13219 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13220 #ifdef GLYPH_DEBUG
13221 *w->desired_matrix->method = 0;
13222 debug_method_add (w, "optimization 3");
13223 #endif
13224 goto update;
13226 else
13227 goto cancel;
13230 cancel:
13231 /* Text changed drastically or point moved off of line. */
13232 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13235 CHARPOS (this_line_start_pos) = 0;
13236 consider_all_windows_p |= buffer_shared_and_changed ();
13237 ++clear_face_cache_count;
13238 #ifdef HAVE_WINDOW_SYSTEM
13239 ++clear_image_cache_count;
13240 #endif
13242 w->region_showing = XINT (Fmarker_position (BVAR (XBUFFER (w->buffer), mark)));
13244 /* Build desired matrices, and update the display. If
13245 consider_all_windows_p is non-zero, do it for all windows on all
13246 frames. Otherwise do it for selected_window, only. */
13248 if (consider_all_windows_p)
13250 FOR_EACH_FRAME (tail, frame)
13251 XFRAME (frame)->updated_p = 0;
13253 FOR_EACH_FRAME (tail, frame)
13255 struct frame *f = XFRAME (frame);
13257 /* We don't have to do anything for unselected terminal
13258 frames. */
13259 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13260 && !EQ (FRAME_TTY (f)->top_frame, frame))
13261 continue;
13263 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13265 /* Mark all the scroll bars to be removed; we'll redeem
13266 the ones we want when we redisplay their windows. */
13267 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13268 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13270 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13271 redisplay_windows (FRAME_ROOT_WINDOW (f));
13273 /* The X error handler may have deleted that frame. */
13274 if (!FRAME_LIVE_P (f))
13275 continue;
13277 /* Any scroll bars which redisplay_windows should have
13278 nuked should now go away. */
13279 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13280 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13282 /* If fonts changed, display again. */
13283 /* ??? rms: I suspect it is a mistake to jump all the way
13284 back to retry here. It should just retry this frame. */
13285 if (fonts_changed_p)
13286 goto retry;
13288 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13290 /* See if we have to hscroll. */
13291 if (!f->already_hscrolled_p)
13293 f->already_hscrolled_p = 1;
13294 if (hscroll_windows (f->root_window))
13295 goto retry;
13298 /* Prevent various kinds of signals during display
13299 update. stdio is not robust about handling
13300 signals, which can cause an apparent I/O
13301 error. */
13302 if (interrupt_input)
13303 unrequest_sigio ();
13304 STOP_POLLING;
13306 /* Update the display. */
13307 set_window_update_flags (XWINDOW (f->root_window), 1);
13308 pending |= update_frame (f, 0, 0);
13309 f->updated_p = 1;
13314 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13316 if (!pending)
13318 /* Do the mark_window_display_accurate after all windows have
13319 been redisplayed because this call resets flags in buffers
13320 which are needed for proper redisplay. */
13321 FOR_EACH_FRAME (tail, frame)
13323 struct frame *f = XFRAME (frame);
13324 if (f->updated_p)
13326 mark_window_display_accurate (f->root_window, 1);
13327 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13328 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13333 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13335 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13336 struct frame *mini_frame;
13338 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13339 /* Use list_of_error, not Qerror, so that
13340 we catch only errors and don't run the debugger. */
13341 internal_condition_case_1 (redisplay_window_1, selected_window,
13342 list_of_error,
13343 redisplay_window_error);
13344 if (update_miniwindow_p)
13345 internal_condition_case_1 (redisplay_window_1, mini_window,
13346 list_of_error,
13347 redisplay_window_error);
13349 /* Compare desired and current matrices, perform output. */
13351 update:
13352 /* If fonts changed, display again. */
13353 if (fonts_changed_p)
13354 goto retry;
13356 /* Prevent various kinds of signals during display update.
13357 stdio is not robust about handling signals,
13358 which can cause an apparent I/O error. */
13359 if (interrupt_input)
13360 unrequest_sigio ();
13361 STOP_POLLING;
13363 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13365 if (hscroll_windows (selected_window))
13366 goto retry;
13368 XWINDOW (selected_window)->must_be_updated_p = 1;
13369 pending = update_frame (sf, 0, 0);
13372 /* We may have called echo_area_display at the top of this
13373 function. If the echo area is on another frame, that may
13374 have put text on a frame other than the selected one, so the
13375 above call to update_frame would not have caught it. Catch
13376 it here. */
13377 mini_window = FRAME_MINIBUF_WINDOW (sf);
13378 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13380 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13382 XWINDOW (mini_window)->must_be_updated_p = 1;
13383 pending |= update_frame (mini_frame, 0, 0);
13384 if (!pending && hscroll_windows (mini_window))
13385 goto retry;
13389 /* If display was paused because of pending input, make sure we do a
13390 thorough update the next time. */
13391 if (pending)
13393 /* Prevent the optimization at the beginning of
13394 redisplay_internal that tries a single-line update of the
13395 line containing the cursor in the selected window. */
13396 CHARPOS (this_line_start_pos) = 0;
13398 /* Let the overlay arrow be updated the next time. */
13399 update_overlay_arrows (0);
13401 /* If we pause after scrolling, some rows in the current
13402 matrices of some windows are not valid. */
13403 if (!WINDOW_FULL_WIDTH_P (w)
13404 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13405 update_mode_lines = 1;
13407 else
13409 if (!consider_all_windows_p)
13411 /* This has already been done above if
13412 consider_all_windows_p is set. */
13413 mark_window_display_accurate_1 (w, 1);
13415 /* Say overlay arrows are up to date. */
13416 update_overlay_arrows (1);
13418 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13419 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13422 update_mode_lines = 0;
13423 windows_or_buffers_changed = 0;
13424 cursor_type_changed = 0;
13427 /* Start SIGIO interrupts coming again. Having them off during the
13428 code above makes it less likely one will discard output, but not
13429 impossible, since there might be stuff in the system buffer here.
13430 But it is much hairier to try to do anything about that. */
13431 if (interrupt_input)
13432 request_sigio ();
13433 RESUME_POLLING;
13435 /* If a frame has become visible which was not before, redisplay
13436 again, so that we display it. Expose events for such a frame
13437 (which it gets when becoming visible) don't call the parts of
13438 redisplay constructing glyphs, so simply exposing a frame won't
13439 display anything in this case. So, we have to display these
13440 frames here explicitly. */
13441 if (!pending)
13443 int new_count = 0;
13445 FOR_EACH_FRAME (tail, frame)
13447 int this_is_visible = 0;
13449 if (XFRAME (frame)->visible)
13450 this_is_visible = 1;
13452 if (this_is_visible)
13453 new_count++;
13456 if (new_count != number_of_visible_frames)
13457 windows_or_buffers_changed++;
13460 /* Change frame size now if a change is pending. */
13461 do_pending_window_change (1);
13463 /* If we just did a pending size change, or have additional
13464 visible frames, or selected_window changed, redisplay again. */
13465 if ((windows_or_buffers_changed && !pending)
13466 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13467 goto retry;
13469 /* Clear the face and image caches.
13471 We used to do this only if consider_all_windows_p. But the cache
13472 needs to be cleared if a timer creates images in the current
13473 buffer (e.g. the test case in Bug#6230). */
13475 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13477 clear_face_cache (0);
13478 clear_face_cache_count = 0;
13481 #ifdef HAVE_WINDOW_SYSTEM
13482 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13484 clear_image_caches (Qnil);
13485 clear_image_cache_count = 0;
13487 #endif /* HAVE_WINDOW_SYSTEM */
13489 end_of_redisplay:
13490 backtrace_list = backtrace.next;
13491 unbind_to (count, Qnil);
13492 RESUME_POLLING;
13496 /* Redisplay, but leave alone any recent echo area message unless
13497 another message has been requested in its place.
13499 This is useful in situations where you need to redisplay but no
13500 user action has occurred, making it inappropriate for the message
13501 area to be cleared. See tracking_off and
13502 wait_reading_process_output for examples of these situations.
13504 FROM_WHERE is an integer saying from where this function was
13505 called. This is useful for debugging. */
13507 void
13508 redisplay_preserve_echo_area (int from_where)
13510 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13512 if (!NILP (echo_area_buffer[1]))
13514 /* We have a previously displayed message, but no current
13515 message. Redisplay the previous message. */
13516 display_last_displayed_message_p = 1;
13517 redisplay_internal ();
13518 display_last_displayed_message_p = 0;
13520 else
13521 redisplay_internal ();
13523 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13524 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13525 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13529 /* Function registered with record_unwind_protect in redisplay_internal.
13530 Clear redisplaying_p. Also select the previously selected frame. */
13532 static Lisp_Object
13533 unwind_redisplay (Lisp_Object old_frame)
13535 redisplaying_p = 0;
13536 return Qnil;
13540 /* Mark the display of leaf window W as accurate or inaccurate.
13541 If ACCURATE_P is non-zero mark display of W as accurate. If
13542 ACCURATE_P is zero, arrange for W to be redisplayed the next
13543 time redisplay_internal is called. */
13545 static void
13546 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13548 struct buffer *b = XBUFFER (w->buffer);
13550 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13551 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13552 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13554 if (accurate_p)
13556 b->clip_changed = 0;
13557 b->prevent_redisplay_optimizations_p = 0;
13559 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13560 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13561 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13562 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13564 w->current_matrix->buffer = b;
13565 w->current_matrix->begv = BUF_BEGV (b);
13566 w->current_matrix->zv = BUF_ZV (b);
13568 w->last_cursor = w->cursor;
13569 w->last_cursor_off_p = w->cursor_off_p;
13571 if (w == XWINDOW (selected_window))
13572 w->last_point = BUF_PT (b);
13573 else
13574 w->last_point = marker_position (w->pointm);
13576 w->window_end_valid = 1;
13577 w->update_mode_line = 0;
13582 /* Mark the display of windows in the window tree rooted at WINDOW as
13583 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13584 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13585 be redisplayed the next time redisplay_internal is called. */
13587 void
13588 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13590 struct window *w;
13592 for (; !NILP (window); window = w->next)
13594 w = XWINDOW (window);
13595 if (!NILP (w->vchild))
13596 mark_window_display_accurate (w->vchild, accurate_p);
13597 else if (!NILP (w->hchild))
13598 mark_window_display_accurate (w->hchild, accurate_p);
13599 else if (BUFFERP (w->buffer))
13600 mark_window_display_accurate_1 (w, accurate_p);
13603 if (accurate_p)
13604 update_overlay_arrows (1);
13605 else
13606 /* Force a thorough redisplay the next time by setting
13607 last_arrow_position and last_arrow_string to t, which is
13608 unequal to any useful value of Voverlay_arrow_... */
13609 update_overlay_arrows (-1);
13613 /* Return value in display table DP (Lisp_Char_Table *) for character
13614 C. Since a display table doesn't have any parent, we don't have to
13615 follow parent. Do not call this function directly but use the
13616 macro DISP_CHAR_VECTOR. */
13618 Lisp_Object
13619 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13621 Lisp_Object val;
13623 if (ASCII_CHAR_P (c))
13625 val = dp->ascii;
13626 if (SUB_CHAR_TABLE_P (val))
13627 val = XSUB_CHAR_TABLE (val)->contents[c];
13629 else
13631 Lisp_Object table;
13633 XSETCHAR_TABLE (table, dp);
13634 val = char_table_ref (table, c);
13636 if (NILP (val))
13637 val = dp->defalt;
13638 return val;
13643 /***********************************************************************
13644 Window Redisplay
13645 ***********************************************************************/
13647 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13649 static void
13650 redisplay_windows (Lisp_Object window)
13652 while (!NILP (window))
13654 struct window *w = XWINDOW (window);
13656 if (!NILP (w->hchild))
13657 redisplay_windows (w->hchild);
13658 else if (!NILP (w->vchild))
13659 redisplay_windows (w->vchild);
13660 else if (!NILP (w->buffer))
13662 displayed_buffer = XBUFFER (w->buffer);
13663 /* Use list_of_error, not Qerror, so that
13664 we catch only errors and don't run the debugger. */
13665 internal_condition_case_1 (redisplay_window_0, window,
13666 list_of_error,
13667 redisplay_window_error);
13670 window = w->next;
13674 static Lisp_Object
13675 redisplay_window_error (Lisp_Object ignore)
13677 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13678 return Qnil;
13681 static Lisp_Object
13682 redisplay_window_0 (Lisp_Object window)
13684 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13685 redisplay_window (window, 0);
13686 return Qnil;
13689 static Lisp_Object
13690 redisplay_window_1 (Lisp_Object window)
13692 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13693 redisplay_window (window, 1);
13694 return Qnil;
13698 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13699 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13700 which positions recorded in ROW differ from current buffer
13701 positions.
13703 Return 0 if cursor is not on this row, 1 otherwise. */
13705 static int
13706 set_cursor_from_row (struct window *w, struct glyph_row *row,
13707 struct glyph_matrix *matrix,
13708 ptrdiff_t delta, ptrdiff_t delta_bytes,
13709 int dy, int dvpos)
13711 struct glyph *glyph = row->glyphs[TEXT_AREA];
13712 struct glyph *end = glyph + row->used[TEXT_AREA];
13713 struct glyph *cursor = NULL;
13714 /* The last known character position in row. */
13715 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13716 int x = row->x;
13717 ptrdiff_t pt_old = PT - delta;
13718 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13719 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13720 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13721 /* A glyph beyond the edge of TEXT_AREA which we should never
13722 touch. */
13723 struct glyph *glyphs_end = end;
13724 /* Non-zero means we've found a match for cursor position, but that
13725 glyph has the avoid_cursor_p flag set. */
13726 int match_with_avoid_cursor = 0;
13727 /* Non-zero means we've seen at least one glyph that came from a
13728 display string. */
13729 int string_seen = 0;
13730 /* Largest and smallest buffer positions seen so far during scan of
13731 glyph row. */
13732 ptrdiff_t bpos_max = pos_before;
13733 ptrdiff_t bpos_min = pos_after;
13734 /* Last buffer position covered by an overlay string with an integer
13735 `cursor' property. */
13736 ptrdiff_t bpos_covered = 0;
13737 /* Non-zero means the display string on which to display the cursor
13738 comes from a text property, not from an overlay. */
13739 int string_from_text_prop = 0;
13741 /* Don't even try doing anything if called for a mode-line or
13742 header-line row, since the rest of the code isn't prepared to
13743 deal with such calamities. */
13744 eassert (!row->mode_line_p);
13745 if (row->mode_line_p)
13746 return 0;
13748 /* Skip over glyphs not having an object at the start and the end of
13749 the row. These are special glyphs like truncation marks on
13750 terminal frames. */
13751 if (row->displays_text_p)
13753 if (!row->reversed_p)
13755 while (glyph < end
13756 && INTEGERP (glyph->object)
13757 && glyph->charpos < 0)
13759 x += glyph->pixel_width;
13760 ++glyph;
13762 while (end > glyph
13763 && INTEGERP ((end - 1)->object)
13764 /* CHARPOS is zero for blanks and stretch glyphs
13765 inserted by extend_face_to_end_of_line. */
13766 && (end - 1)->charpos <= 0)
13767 --end;
13768 glyph_before = glyph - 1;
13769 glyph_after = end;
13771 else
13773 struct glyph *g;
13775 /* If the glyph row is reversed, we need to process it from back
13776 to front, so swap the edge pointers. */
13777 glyphs_end = end = glyph - 1;
13778 glyph += row->used[TEXT_AREA] - 1;
13780 while (glyph > end + 1
13781 && INTEGERP (glyph->object)
13782 && glyph->charpos < 0)
13784 --glyph;
13785 x -= glyph->pixel_width;
13787 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13788 --glyph;
13789 /* By default, in reversed rows we put the cursor on the
13790 rightmost (first in the reading order) glyph. */
13791 for (g = end + 1; g < glyph; g++)
13792 x += g->pixel_width;
13793 while (end < glyph
13794 && INTEGERP ((end + 1)->object)
13795 && (end + 1)->charpos <= 0)
13796 ++end;
13797 glyph_before = glyph + 1;
13798 glyph_after = end;
13801 else if (row->reversed_p)
13803 /* In R2L rows that don't display text, put the cursor on the
13804 rightmost glyph. Case in point: an empty last line that is
13805 part of an R2L paragraph. */
13806 cursor = end - 1;
13807 /* Avoid placing the cursor on the last glyph of the row, where
13808 on terminal frames we hold the vertical border between
13809 adjacent windows. */
13810 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13811 && !WINDOW_RIGHTMOST_P (w)
13812 && cursor == row->glyphs[LAST_AREA] - 1)
13813 cursor--;
13814 x = -1; /* will be computed below, at label compute_x */
13817 /* Step 1: Try to find the glyph whose character position
13818 corresponds to point. If that's not possible, find 2 glyphs
13819 whose character positions are the closest to point, one before
13820 point, the other after it. */
13821 if (!row->reversed_p)
13822 while (/* not marched to end of glyph row */
13823 glyph < end
13824 /* glyph was not inserted by redisplay for internal purposes */
13825 && !INTEGERP (glyph->object))
13827 if (BUFFERP (glyph->object))
13829 ptrdiff_t dpos = glyph->charpos - pt_old;
13831 if (glyph->charpos > bpos_max)
13832 bpos_max = glyph->charpos;
13833 if (glyph->charpos < bpos_min)
13834 bpos_min = glyph->charpos;
13835 if (!glyph->avoid_cursor_p)
13837 /* If we hit point, we've found the glyph on which to
13838 display the cursor. */
13839 if (dpos == 0)
13841 match_with_avoid_cursor = 0;
13842 break;
13844 /* See if we've found a better approximation to
13845 POS_BEFORE or to POS_AFTER. */
13846 if (0 > dpos && dpos > pos_before - pt_old)
13848 pos_before = glyph->charpos;
13849 glyph_before = glyph;
13851 else if (0 < dpos && dpos < pos_after - pt_old)
13853 pos_after = glyph->charpos;
13854 glyph_after = glyph;
13857 else if (dpos == 0)
13858 match_with_avoid_cursor = 1;
13860 else if (STRINGP (glyph->object))
13862 Lisp_Object chprop;
13863 ptrdiff_t glyph_pos = glyph->charpos;
13865 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13866 glyph->object);
13867 if (!NILP (chprop))
13869 /* If the string came from a `display' text property,
13870 look up the buffer position of that property and
13871 use that position to update bpos_max, as if we
13872 actually saw such a position in one of the row's
13873 glyphs. This helps with supporting integer values
13874 of `cursor' property on the display string in
13875 situations where most or all of the row's buffer
13876 text is completely covered by display properties,
13877 so that no glyph with valid buffer positions is
13878 ever seen in the row. */
13879 ptrdiff_t prop_pos =
13880 string_buffer_position_lim (glyph->object, pos_before,
13881 pos_after, 0);
13883 if (prop_pos >= pos_before)
13884 bpos_max = prop_pos - 1;
13886 if (INTEGERP (chprop))
13888 bpos_covered = bpos_max + XINT (chprop);
13889 /* If the `cursor' property covers buffer positions up
13890 to and including point, we should display cursor on
13891 this glyph. Note that, if a `cursor' property on one
13892 of the string's characters has an integer value, we
13893 will break out of the loop below _before_ we get to
13894 the position match above. IOW, integer values of
13895 the `cursor' property override the "exact match for
13896 point" strategy of positioning the cursor. */
13897 /* Implementation note: bpos_max == pt_old when, e.g.,
13898 we are in an empty line, where bpos_max is set to
13899 MATRIX_ROW_START_CHARPOS, see above. */
13900 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13902 cursor = glyph;
13903 break;
13907 string_seen = 1;
13909 x += glyph->pixel_width;
13910 ++glyph;
13912 else if (glyph > end) /* row is reversed */
13913 while (!INTEGERP (glyph->object))
13915 if (BUFFERP (glyph->object))
13917 ptrdiff_t dpos = glyph->charpos - pt_old;
13919 if (glyph->charpos > bpos_max)
13920 bpos_max = glyph->charpos;
13921 if (glyph->charpos < bpos_min)
13922 bpos_min = glyph->charpos;
13923 if (!glyph->avoid_cursor_p)
13925 if (dpos == 0)
13927 match_with_avoid_cursor = 0;
13928 break;
13930 if (0 > dpos && dpos > pos_before - pt_old)
13932 pos_before = glyph->charpos;
13933 glyph_before = glyph;
13935 else if (0 < dpos && dpos < pos_after - pt_old)
13937 pos_after = glyph->charpos;
13938 glyph_after = glyph;
13941 else if (dpos == 0)
13942 match_with_avoid_cursor = 1;
13944 else if (STRINGP (glyph->object))
13946 Lisp_Object chprop;
13947 ptrdiff_t glyph_pos = glyph->charpos;
13949 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13950 glyph->object);
13951 if (!NILP (chprop))
13953 ptrdiff_t prop_pos =
13954 string_buffer_position_lim (glyph->object, pos_before,
13955 pos_after, 0);
13957 if (prop_pos >= pos_before)
13958 bpos_max = prop_pos - 1;
13960 if (INTEGERP (chprop))
13962 bpos_covered = bpos_max + XINT (chprop);
13963 /* If the `cursor' property covers buffer positions up
13964 to and including point, we should display cursor on
13965 this glyph. */
13966 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13968 cursor = glyph;
13969 break;
13972 string_seen = 1;
13974 --glyph;
13975 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13977 x--; /* can't use any pixel_width */
13978 break;
13980 x -= glyph->pixel_width;
13983 /* Step 2: If we didn't find an exact match for point, we need to
13984 look for a proper place to put the cursor among glyphs between
13985 GLYPH_BEFORE and GLYPH_AFTER. */
13986 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13987 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13988 && !(bpos_max < pt_old && pt_old <= bpos_covered))
13990 /* An empty line has a single glyph whose OBJECT is zero and
13991 whose CHARPOS is the position of a newline on that line.
13992 Note that on a TTY, there are more glyphs after that, which
13993 were produced by extend_face_to_end_of_line, but their
13994 CHARPOS is zero or negative. */
13995 int empty_line_p =
13996 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13997 && INTEGERP (glyph->object) && glyph->charpos > 0
13998 /* On a TTY, continued and truncated rows also have a glyph at
13999 their end whose OBJECT is zero and whose CHARPOS is
14000 positive (the continuation and truncation glyphs), but such
14001 rows are obviously not "empty". */
14002 && !(row->continued_p || row->truncated_on_right_p);
14004 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14006 ptrdiff_t ellipsis_pos;
14008 /* Scan back over the ellipsis glyphs. */
14009 if (!row->reversed_p)
14011 ellipsis_pos = (glyph - 1)->charpos;
14012 while (glyph > row->glyphs[TEXT_AREA]
14013 && (glyph - 1)->charpos == ellipsis_pos)
14014 glyph--, x -= glyph->pixel_width;
14015 /* That loop always goes one position too far, including
14016 the glyph before the ellipsis. So scan forward over
14017 that one. */
14018 x += glyph->pixel_width;
14019 glyph++;
14021 else /* row is reversed */
14023 ellipsis_pos = (glyph + 1)->charpos;
14024 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14025 && (glyph + 1)->charpos == ellipsis_pos)
14026 glyph++, x += glyph->pixel_width;
14027 x -= glyph->pixel_width;
14028 glyph--;
14031 else if (match_with_avoid_cursor)
14033 cursor = glyph_after;
14034 x = -1;
14036 else if (string_seen)
14038 int incr = row->reversed_p ? -1 : +1;
14040 /* Need to find the glyph that came out of a string which is
14041 present at point. That glyph is somewhere between
14042 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14043 positioned between POS_BEFORE and POS_AFTER in the
14044 buffer. */
14045 struct glyph *start, *stop;
14046 ptrdiff_t pos = pos_before;
14048 x = -1;
14050 /* If the row ends in a newline from a display string,
14051 reordering could have moved the glyphs belonging to the
14052 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14053 in this case we extend the search to the last glyph in
14054 the row that was not inserted by redisplay. */
14055 if (row->ends_in_newline_from_string_p)
14057 glyph_after = end;
14058 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14061 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14062 correspond to POS_BEFORE and POS_AFTER, respectively. We
14063 need START and STOP in the order that corresponds to the
14064 row's direction as given by its reversed_p flag. If the
14065 directionality of characters between POS_BEFORE and
14066 POS_AFTER is the opposite of the row's base direction,
14067 these characters will have been reordered for display,
14068 and we need to reverse START and STOP. */
14069 if (!row->reversed_p)
14071 start = min (glyph_before, glyph_after);
14072 stop = max (glyph_before, glyph_after);
14074 else
14076 start = max (glyph_before, glyph_after);
14077 stop = min (glyph_before, glyph_after);
14079 for (glyph = start + incr;
14080 row->reversed_p ? glyph > stop : glyph < stop; )
14083 /* Any glyphs that come from the buffer are here because
14084 of bidi reordering. Skip them, and only pay
14085 attention to glyphs that came from some string. */
14086 if (STRINGP (glyph->object))
14088 Lisp_Object str;
14089 ptrdiff_t tem;
14090 /* If the display property covers the newline, we
14091 need to search for it one position farther. */
14092 ptrdiff_t lim = pos_after
14093 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14095 string_from_text_prop = 0;
14096 str = glyph->object;
14097 tem = string_buffer_position_lim (str, pos, lim, 0);
14098 if (tem == 0 /* from overlay */
14099 || pos <= tem)
14101 /* If the string from which this glyph came is
14102 found in the buffer at point, or at position
14103 that is closer to point than pos_after, then
14104 we've found the glyph we've been looking for.
14105 If it comes from an overlay (tem == 0), and
14106 it has the `cursor' property on one of its
14107 glyphs, record that glyph as a candidate for
14108 displaying the cursor. (As in the
14109 unidirectional version, we will display the
14110 cursor on the last candidate we find.) */
14111 if (tem == 0
14112 || tem == pt_old
14113 || (tem - pt_old > 0 && tem < pos_after))
14115 /* The glyphs from this string could have
14116 been reordered. Find the one with the
14117 smallest string position. Or there could
14118 be a character in the string with the
14119 `cursor' property, which means display
14120 cursor on that character's glyph. */
14121 ptrdiff_t strpos = glyph->charpos;
14123 if (tem)
14125 cursor = glyph;
14126 string_from_text_prop = 1;
14128 for ( ;
14129 (row->reversed_p ? glyph > stop : glyph < stop)
14130 && EQ (glyph->object, str);
14131 glyph += incr)
14133 Lisp_Object cprop;
14134 ptrdiff_t gpos = glyph->charpos;
14136 cprop = Fget_char_property (make_number (gpos),
14137 Qcursor,
14138 glyph->object);
14139 if (!NILP (cprop))
14141 cursor = glyph;
14142 break;
14144 if (tem && glyph->charpos < strpos)
14146 strpos = glyph->charpos;
14147 cursor = glyph;
14151 if (tem == pt_old
14152 || (tem - pt_old > 0 && tem < pos_after))
14153 goto compute_x;
14155 if (tem)
14156 pos = tem + 1; /* don't find previous instances */
14158 /* This string is not what we want; skip all of the
14159 glyphs that came from it. */
14160 while ((row->reversed_p ? glyph > stop : glyph < stop)
14161 && EQ (glyph->object, str))
14162 glyph += incr;
14164 else
14165 glyph += incr;
14168 /* If we reached the end of the line, and END was from a string,
14169 the cursor is not on this line. */
14170 if (cursor == NULL
14171 && (row->reversed_p ? glyph <= end : glyph >= end)
14172 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14173 && STRINGP (end->object)
14174 && row->continued_p)
14175 return 0;
14177 /* A truncated row may not include PT among its character positions.
14178 Setting the cursor inside the scroll margin will trigger
14179 recalculation of hscroll in hscroll_window_tree. But if a
14180 display string covers point, defer to the string-handling
14181 code below to figure this out. */
14182 else if (row->truncated_on_left_p && pt_old < bpos_min)
14184 cursor = glyph_before;
14185 x = -1;
14187 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14188 /* Zero-width characters produce no glyphs. */
14189 || (!empty_line_p
14190 && (row->reversed_p
14191 ? glyph_after > glyphs_end
14192 : glyph_after < glyphs_end)))
14194 cursor = glyph_after;
14195 x = -1;
14199 compute_x:
14200 if (cursor != NULL)
14201 glyph = cursor;
14202 else if (glyph == glyphs_end
14203 && pos_before == pos_after
14204 && STRINGP ((row->reversed_p
14205 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14206 : row->glyphs[TEXT_AREA])->object))
14208 /* If all the glyphs of this row came from strings, put the
14209 cursor on the first glyph of the row. This avoids having the
14210 cursor outside of the text area in this very rare and hard
14211 use case. */
14212 glyph =
14213 row->reversed_p
14214 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14215 : row->glyphs[TEXT_AREA];
14217 if (x < 0)
14219 struct glyph *g;
14221 /* Need to compute x that corresponds to GLYPH. */
14222 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14224 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14225 emacs_abort ();
14226 x += g->pixel_width;
14230 /* ROW could be part of a continued line, which, under bidi
14231 reordering, might have other rows whose start and end charpos
14232 occlude point. Only set w->cursor if we found a better
14233 approximation to the cursor position than we have from previously
14234 examined candidate rows belonging to the same continued line. */
14235 if (/* we already have a candidate row */
14236 w->cursor.vpos >= 0
14237 /* that candidate is not the row we are processing */
14238 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14239 /* Make sure cursor.vpos specifies a row whose start and end
14240 charpos occlude point, and it is valid candidate for being a
14241 cursor-row. This is because some callers of this function
14242 leave cursor.vpos at the row where the cursor was displayed
14243 during the last redisplay cycle. */
14244 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14245 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14246 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14248 struct glyph *g1 =
14249 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14251 /* Don't consider glyphs that are outside TEXT_AREA. */
14252 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14253 return 0;
14254 /* Keep the candidate whose buffer position is the closest to
14255 point or has the `cursor' property. */
14256 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14257 w->cursor.hpos >= 0
14258 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14259 && ((BUFFERP (g1->object)
14260 && (g1->charpos == pt_old /* an exact match always wins */
14261 || (BUFFERP (glyph->object)
14262 && eabs (g1->charpos - pt_old)
14263 < eabs (glyph->charpos - pt_old))))
14264 /* previous candidate is a glyph from a string that has
14265 a non-nil `cursor' property */
14266 || (STRINGP (g1->object)
14267 && (!NILP (Fget_char_property (make_number (g1->charpos),
14268 Qcursor, g1->object))
14269 /* previous candidate is from the same display
14270 string as this one, and the display string
14271 came from a text property */
14272 || (EQ (g1->object, glyph->object)
14273 && string_from_text_prop)
14274 /* this candidate is from newline and its
14275 position is not an exact match */
14276 || (INTEGERP (glyph->object)
14277 && glyph->charpos != pt_old)))))
14278 return 0;
14279 /* If this candidate gives an exact match, use that. */
14280 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14281 /* If this candidate is a glyph created for the
14282 terminating newline of a line, and point is on that
14283 newline, it wins because it's an exact match. */
14284 || (!row->continued_p
14285 && INTEGERP (glyph->object)
14286 && glyph->charpos == 0
14287 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14288 /* Otherwise, keep the candidate that comes from a row
14289 spanning less buffer positions. This may win when one or
14290 both candidate positions are on glyphs that came from
14291 display strings, for which we cannot compare buffer
14292 positions. */
14293 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14294 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14295 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14296 return 0;
14298 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14299 w->cursor.x = x;
14300 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14301 w->cursor.y = row->y + dy;
14303 if (w == XWINDOW (selected_window))
14305 if (!row->continued_p
14306 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14307 && row->x == 0)
14309 this_line_buffer = XBUFFER (w->buffer);
14311 CHARPOS (this_line_start_pos)
14312 = MATRIX_ROW_START_CHARPOS (row) + delta;
14313 BYTEPOS (this_line_start_pos)
14314 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14316 CHARPOS (this_line_end_pos)
14317 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14318 BYTEPOS (this_line_end_pos)
14319 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14321 this_line_y = w->cursor.y;
14322 this_line_pixel_height = row->height;
14323 this_line_vpos = w->cursor.vpos;
14324 this_line_start_x = row->x;
14326 else
14327 CHARPOS (this_line_start_pos) = 0;
14330 return 1;
14334 /* Run window scroll functions, if any, for WINDOW with new window
14335 start STARTP. Sets the window start of WINDOW to that position.
14337 We assume that the window's buffer is really current. */
14339 static struct text_pos
14340 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14342 struct window *w = XWINDOW (window);
14343 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14345 if (current_buffer != XBUFFER (w->buffer))
14346 emacs_abort ();
14348 if (!NILP (Vwindow_scroll_functions))
14350 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14351 make_number (CHARPOS (startp)));
14352 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14353 /* In case the hook functions switch buffers. */
14354 set_buffer_internal (XBUFFER (w->buffer));
14357 return startp;
14361 /* Make sure the line containing the cursor is fully visible.
14362 A value of 1 means there is nothing to be done.
14363 (Either the line is fully visible, or it cannot be made so,
14364 or we cannot tell.)
14366 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14367 is higher than window.
14369 A value of 0 means the caller should do scrolling
14370 as if point had gone off the screen. */
14372 static int
14373 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14375 struct glyph_matrix *matrix;
14376 struct glyph_row *row;
14377 int window_height;
14379 if (!make_cursor_line_fully_visible_p)
14380 return 1;
14382 /* It's not always possible to find the cursor, e.g, when a window
14383 is full of overlay strings. Don't do anything in that case. */
14384 if (w->cursor.vpos < 0)
14385 return 1;
14387 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14388 row = MATRIX_ROW (matrix, w->cursor.vpos);
14390 /* If the cursor row is not partially visible, there's nothing to do. */
14391 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14392 return 1;
14394 /* If the row the cursor is in is taller than the window's height,
14395 it's not clear what to do, so do nothing. */
14396 window_height = window_box_height (w);
14397 if (row->height >= window_height)
14399 if (!force_p || MINI_WINDOW_P (w)
14400 || w->vscroll || w->cursor.vpos == 0)
14401 return 1;
14403 return 0;
14407 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14408 non-zero means only WINDOW is redisplayed in redisplay_internal.
14409 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14410 in redisplay_window to bring a partially visible line into view in
14411 the case that only the cursor has moved.
14413 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14414 last screen line's vertical height extends past the end of the screen.
14416 Value is
14418 1 if scrolling succeeded
14420 0 if scrolling didn't find point.
14422 -1 if new fonts have been loaded so that we must interrupt
14423 redisplay, adjust glyph matrices, and try again. */
14425 enum
14427 SCROLLING_SUCCESS,
14428 SCROLLING_FAILED,
14429 SCROLLING_NEED_LARGER_MATRICES
14432 /* If scroll-conservatively is more than this, never recenter.
14434 If you change this, don't forget to update the doc string of
14435 `scroll-conservatively' and the Emacs manual. */
14436 #define SCROLL_LIMIT 100
14438 static int
14439 try_scrolling (Lisp_Object window, int just_this_one_p,
14440 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14441 int temp_scroll_step, int last_line_misfit)
14443 struct window *w = XWINDOW (window);
14444 struct frame *f = XFRAME (w->frame);
14445 struct text_pos pos, startp;
14446 struct it it;
14447 int this_scroll_margin, scroll_max, rc, height;
14448 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14449 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14450 Lisp_Object aggressive;
14451 /* We will never try scrolling more than this number of lines. */
14452 int scroll_limit = SCROLL_LIMIT;
14454 #ifdef GLYPH_DEBUG
14455 debug_method_add (w, "try_scrolling");
14456 #endif
14458 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14460 /* Compute scroll margin height in pixels. We scroll when point is
14461 within this distance from the top or bottom of the window. */
14462 if (scroll_margin > 0)
14463 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14464 * FRAME_LINE_HEIGHT (f);
14465 else
14466 this_scroll_margin = 0;
14468 /* Force arg_scroll_conservatively to have a reasonable value, to
14469 avoid scrolling too far away with slow move_it_* functions. Note
14470 that the user can supply scroll-conservatively equal to
14471 `most-positive-fixnum', which can be larger than INT_MAX. */
14472 if (arg_scroll_conservatively > scroll_limit)
14474 arg_scroll_conservatively = scroll_limit + 1;
14475 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14477 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14478 /* Compute how much we should try to scroll maximally to bring
14479 point into view. */
14480 scroll_max = (max (scroll_step,
14481 max (arg_scroll_conservatively, temp_scroll_step))
14482 * FRAME_LINE_HEIGHT (f));
14483 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14484 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14485 /* We're trying to scroll because of aggressive scrolling but no
14486 scroll_step is set. Choose an arbitrary one. */
14487 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14488 else
14489 scroll_max = 0;
14491 too_near_end:
14493 /* Decide whether to scroll down. */
14494 if (PT > CHARPOS (startp))
14496 int scroll_margin_y;
14498 /* Compute the pixel ypos of the scroll margin, then move IT to
14499 either that ypos or PT, whichever comes first. */
14500 start_display (&it, w, startp);
14501 scroll_margin_y = it.last_visible_y - this_scroll_margin
14502 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14503 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14504 (MOVE_TO_POS | MOVE_TO_Y));
14506 if (PT > CHARPOS (it.current.pos))
14508 int y0 = line_bottom_y (&it);
14509 /* Compute how many pixels below window bottom to stop searching
14510 for PT. This avoids costly search for PT that is far away if
14511 the user limited scrolling by a small number of lines, but
14512 always finds PT if scroll_conservatively is set to a large
14513 number, such as most-positive-fixnum. */
14514 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14515 int y_to_move = it.last_visible_y + slack;
14517 /* Compute the distance from the scroll margin to PT or to
14518 the scroll limit, whichever comes first. This should
14519 include the height of the cursor line, to make that line
14520 fully visible. */
14521 move_it_to (&it, PT, -1, y_to_move,
14522 -1, MOVE_TO_POS | MOVE_TO_Y);
14523 dy = line_bottom_y (&it) - y0;
14525 if (dy > scroll_max)
14526 return SCROLLING_FAILED;
14528 if (dy > 0)
14529 scroll_down_p = 1;
14533 if (scroll_down_p)
14535 /* Point is in or below the bottom scroll margin, so move the
14536 window start down. If scrolling conservatively, move it just
14537 enough down to make point visible. If scroll_step is set,
14538 move it down by scroll_step. */
14539 if (arg_scroll_conservatively)
14540 amount_to_scroll
14541 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14542 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14543 else if (scroll_step || temp_scroll_step)
14544 amount_to_scroll = scroll_max;
14545 else
14547 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14548 height = WINDOW_BOX_TEXT_HEIGHT (w);
14549 if (NUMBERP (aggressive))
14551 double float_amount = XFLOATINT (aggressive) * height;
14552 int aggressive_scroll = float_amount;
14553 if (aggressive_scroll == 0 && float_amount > 0)
14554 aggressive_scroll = 1;
14555 /* Don't let point enter the scroll margin near top of
14556 the window. This could happen if the value of
14557 scroll_up_aggressively is too large and there are
14558 non-zero margins, because scroll_up_aggressively
14559 means put point that fraction of window height
14560 _from_the_bottom_margin_. */
14561 if (aggressive_scroll + 2*this_scroll_margin > height)
14562 aggressive_scroll = height - 2*this_scroll_margin;
14563 amount_to_scroll = dy + aggressive_scroll;
14567 if (amount_to_scroll <= 0)
14568 return SCROLLING_FAILED;
14570 start_display (&it, w, startp);
14571 if (arg_scroll_conservatively <= scroll_limit)
14572 move_it_vertically (&it, amount_to_scroll);
14573 else
14575 /* Extra precision for users who set scroll-conservatively
14576 to a large number: make sure the amount we scroll
14577 the window start is never less than amount_to_scroll,
14578 which was computed as distance from window bottom to
14579 point. This matters when lines at window top and lines
14580 below window bottom have different height. */
14581 struct it it1;
14582 void *it1data = NULL;
14583 /* We use a temporary it1 because line_bottom_y can modify
14584 its argument, if it moves one line down; see there. */
14585 int start_y;
14587 SAVE_IT (it1, it, it1data);
14588 start_y = line_bottom_y (&it1);
14589 do {
14590 RESTORE_IT (&it, &it, it1data);
14591 move_it_by_lines (&it, 1);
14592 SAVE_IT (it1, it, it1data);
14593 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14596 /* If STARTP is unchanged, move it down another screen line. */
14597 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14598 move_it_by_lines (&it, 1);
14599 startp = it.current.pos;
14601 else
14603 struct text_pos scroll_margin_pos = startp;
14605 /* See if point is inside the scroll margin at the top of the
14606 window. */
14607 if (this_scroll_margin)
14609 start_display (&it, w, startp);
14610 move_it_vertically (&it, this_scroll_margin);
14611 scroll_margin_pos = it.current.pos;
14614 if (PT < CHARPOS (scroll_margin_pos))
14616 /* Point is in the scroll margin at the top of the window or
14617 above what is displayed in the window. */
14618 int y0, y_to_move;
14620 /* Compute the vertical distance from PT to the scroll
14621 margin position. Move as far as scroll_max allows, or
14622 one screenful, or 10 screen lines, whichever is largest.
14623 Give up if distance is greater than scroll_max or if we
14624 didn't reach the scroll margin position. */
14625 SET_TEXT_POS (pos, PT, PT_BYTE);
14626 start_display (&it, w, pos);
14627 y0 = it.current_y;
14628 y_to_move = max (it.last_visible_y,
14629 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14630 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14631 y_to_move, -1,
14632 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14633 dy = it.current_y - y0;
14634 if (dy > scroll_max
14635 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14636 return SCROLLING_FAILED;
14638 /* Compute new window start. */
14639 start_display (&it, w, startp);
14641 if (arg_scroll_conservatively)
14642 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14643 max (scroll_step, temp_scroll_step));
14644 else if (scroll_step || temp_scroll_step)
14645 amount_to_scroll = scroll_max;
14646 else
14648 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14649 height = WINDOW_BOX_TEXT_HEIGHT (w);
14650 if (NUMBERP (aggressive))
14652 double float_amount = XFLOATINT (aggressive) * height;
14653 int aggressive_scroll = float_amount;
14654 if (aggressive_scroll == 0 && float_amount > 0)
14655 aggressive_scroll = 1;
14656 /* Don't let point enter the scroll margin near
14657 bottom of the window, if the value of
14658 scroll_down_aggressively happens to be too
14659 large. */
14660 if (aggressive_scroll + 2*this_scroll_margin > height)
14661 aggressive_scroll = height - 2*this_scroll_margin;
14662 amount_to_scroll = dy + aggressive_scroll;
14666 if (amount_to_scroll <= 0)
14667 return SCROLLING_FAILED;
14669 move_it_vertically_backward (&it, amount_to_scroll);
14670 startp = it.current.pos;
14674 /* Run window scroll functions. */
14675 startp = run_window_scroll_functions (window, startp);
14677 /* Display the window. Give up if new fonts are loaded, or if point
14678 doesn't appear. */
14679 if (!try_window (window, startp, 0))
14680 rc = SCROLLING_NEED_LARGER_MATRICES;
14681 else if (w->cursor.vpos < 0)
14683 clear_glyph_matrix (w->desired_matrix);
14684 rc = SCROLLING_FAILED;
14686 else
14688 /* Maybe forget recorded base line for line number display. */
14689 if (!just_this_one_p
14690 || current_buffer->clip_changed
14691 || BEG_UNCHANGED < CHARPOS (startp))
14692 w->base_line_number = 0;
14694 /* If cursor ends up on a partially visible line,
14695 treat that as being off the bottom of the screen. */
14696 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14697 /* It's possible that the cursor is on the first line of the
14698 buffer, which is partially obscured due to a vscroll
14699 (Bug#7537). In that case, avoid looping forever . */
14700 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14702 clear_glyph_matrix (w->desired_matrix);
14703 ++extra_scroll_margin_lines;
14704 goto too_near_end;
14706 rc = SCROLLING_SUCCESS;
14709 return rc;
14713 /* Compute a suitable window start for window W if display of W starts
14714 on a continuation line. Value is non-zero if a new window start
14715 was computed.
14717 The new window start will be computed, based on W's width, starting
14718 from the start of the continued line. It is the start of the
14719 screen line with the minimum distance from the old start W->start. */
14721 static int
14722 compute_window_start_on_continuation_line (struct window *w)
14724 struct text_pos pos, start_pos;
14725 int window_start_changed_p = 0;
14727 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14729 /* If window start is on a continuation line... Window start may be
14730 < BEGV in case there's invisible text at the start of the
14731 buffer (M-x rmail, for example). */
14732 if (CHARPOS (start_pos) > BEGV
14733 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14735 struct it it;
14736 struct glyph_row *row;
14738 /* Handle the case that the window start is out of range. */
14739 if (CHARPOS (start_pos) < BEGV)
14740 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14741 else if (CHARPOS (start_pos) > ZV)
14742 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14744 /* Find the start of the continued line. This should be fast
14745 because scan_buffer is fast (newline cache). */
14746 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14747 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14748 row, DEFAULT_FACE_ID);
14749 reseat_at_previous_visible_line_start (&it);
14751 /* If the line start is "too far" away from the window start,
14752 say it takes too much time to compute a new window start. */
14753 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14754 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14756 int min_distance, distance;
14758 /* Move forward by display lines to find the new window
14759 start. If window width was enlarged, the new start can
14760 be expected to be > the old start. If window width was
14761 decreased, the new window start will be < the old start.
14762 So, we're looking for the display line start with the
14763 minimum distance from the old window start. */
14764 pos = it.current.pos;
14765 min_distance = INFINITY;
14766 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14767 distance < min_distance)
14769 min_distance = distance;
14770 pos = it.current.pos;
14771 move_it_by_lines (&it, 1);
14774 /* Set the window start there. */
14775 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14776 window_start_changed_p = 1;
14780 return window_start_changed_p;
14784 /* Try cursor movement in case text has not changed in window WINDOW,
14785 with window start STARTP. Value is
14787 CURSOR_MOVEMENT_SUCCESS if successful
14789 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14791 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14792 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14793 we want to scroll as if scroll-step were set to 1. See the code.
14795 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14796 which case we have to abort this redisplay, and adjust matrices
14797 first. */
14799 enum
14801 CURSOR_MOVEMENT_SUCCESS,
14802 CURSOR_MOVEMENT_CANNOT_BE_USED,
14803 CURSOR_MOVEMENT_MUST_SCROLL,
14804 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14807 static int
14808 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14810 struct window *w = XWINDOW (window);
14811 struct frame *f = XFRAME (w->frame);
14812 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14814 #ifdef GLYPH_DEBUG
14815 if (inhibit_try_cursor_movement)
14816 return rc;
14817 #endif
14819 /* Previously, there was a check for Lisp integer in the
14820 if-statement below. Now, this field is converted to
14821 ptrdiff_t, thus zero means invalid position in a buffer. */
14822 eassert (w->last_point > 0);
14824 /* Handle case where text has not changed, only point, and it has
14825 not moved off the frame. */
14826 if (/* Point may be in this window. */
14827 PT >= CHARPOS (startp)
14828 /* Selective display hasn't changed. */
14829 && !current_buffer->clip_changed
14830 /* Function force-mode-line-update is used to force a thorough
14831 redisplay. It sets either windows_or_buffers_changed or
14832 update_mode_lines. So don't take a shortcut here for these
14833 cases. */
14834 && !update_mode_lines
14835 && !windows_or_buffers_changed
14836 && !cursor_type_changed
14837 /* Can't use this case if highlighting a region. When a
14838 region exists, cursor movement has to do more than just
14839 set the cursor. */
14840 && markpos_of_region () < 0
14841 && !w->region_showing
14842 && NILP (Vshow_trailing_whitespace)
14843 /* This code is not used for mini-buffer for the sake of the case
14844 of redisplaying to replace an echo area message; since in
14845 that case the mini-buffer contents per se are usually
14846 unchanged. This code is of no real use in the mini-buffer
14847 since the handling of this_line_start_pos, etc., in redisplay
14848 handles the same cases. */
14849 && !EQ (window, minibuf_window)
14850 /* When splitting windows or for new windows, it happens that
14851 redisplay is called with a nil window_end_vpos or one being
14852 larger than the window. This should really be fixed in
14853 window.c. I don't have this on my list, now, so we do
14854 approximately the same as the old redisplay code. --gerd. */
14855 && INTEGERP (w->window_end_vpos)
14856 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14857 && (FRAME_WINDOW_P (f)
14858 || !overlay_arrow_in_current_buffer_p ()))
14860 int this_scroll_margin, top_scroll_margin;
14861 struct glyph_row *row = NULL;
14863 #ifdef GLYPH_DEBUG
14864 debug_method_add (w, "cursor movement");
14865 #endif
14867 /* Scroll if point within this distance from the top or bottom
14868 of the window. This is a pixel value. */
14869 if (scroll_margin > 0)
14871 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14872 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14874 else
14875 this_scroll_margin = 0;
14877 top_scroll_margin = this_scroll_margin;
14878 if (WINDOW_WANTS_HEADER_LINE_P (w))
14879 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14881 /* Start with the row the cursor was displayed during the last
14882 not paused redisplay. Give up if that row is not valid. */
14883 if (w->last_cursor.vpos < 0
14884 || w->last_cursor.vpos >= w->current_matrix->nrows)
14885 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14886 else
14888 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14889 if (row->mode_line_p)
14890 ++row;
14891 if (!row->enabled_p)
14892 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14895 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14897 int scroll_p = 0, must_scroll = 0;
14898 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14900 if (PT > w->last_point)
14902 /* Point has moved forward. */
14903 while (MATRIX_ROW_END_CHARPOS (row) < PT
14904 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14906 eassert (row->enabled_p);
14907 ++row;
14910 /* If the end position of a row equals the start
14911 position of the next row, and PT is at that position,
14912 we would rather display cursor in the next line. */
14913 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14914 && MATRIX_ROW_END_CHARPOS (row) == PT
14915 && row < w->current_matrix->rows
14916 + w->current_matrix->nrows - 1
14917 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14918 && !cursor_row_p (row))
14919 ++row;
14921 /* If within the scroll margin, scroll. Note that
14922 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14923 the next line would be drawn, and that
14924 this_scroll_margin can be zero. */
14925 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14926 || PT > MATRIX_ROW_END_CHARPOS (row)
14927 /* Line is completely visible last line in window
14928 and PT is to be set in the next line. */
14929 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14930 && PT == MATRIX_ROW_END_CHARPOS (row)
14931 && !row->ends_at_zv_p
14932 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14933 scroll_p = 1;
14935 else if (PT < w->last_point)
14937 /* Cursor has to be moved backward. Note that PT >=
14938 CHARPOS (startp) because of the outer if-statement. */
14939 while (!row->mode_line_p
14940 && (MATRIX_ROW_START_CHARPOS (row) > PT
14941 || (MATRIX_ROW_START_CHARPOS (row) == PT
14942 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14943 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14944 row > w->current_matrix->rows
14945 && (row-1)->ends_in_newline_from_string_p))))
14946 && (row->y > top_scroll_margin
14947 || CHARPOS (startp) == BEGV))
14949 eassert (row->enabled_p);
14950 --row;
14953 /* Consider the following case: Window starts at BEGV,
14954 there is invisible, intangible text at BEGV, so that
14955 display starts at some point START > BEGV. It can
14956 happen that we are called with PT somewhere between
14957 BEGV and START. Try to handle that case. */
14958 if (row < w->current_matrix->rows
14959 || row->mode_line_p)
14961 row = w->current_matrix->rows;
14962 if (row->mode_line_p)
14963 ++row;
14966 /* Due to newlines in overlay strings, we may have to
14967 skip forward over overlay strings. */
14968 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14969 && MATRIX_ROW_END_CHARPOS (row) == PT
14970 && !cursor_row_p (row))
14971 ++row;
14973 /* If within the scroll margin, scroll. */
14974 if (row->y < top_scroll_margin
14975 && CHARPOS (startp) != BEGV)
14976 scroll_p = 1;
14978 else
14980 /* Cursor did not move. So don't scroll even if cursor line
14981 is partially visible, as it was so before. */
14982 rc = CURSOR_MOVEMENT_SUCCESS;
14985 if (PT < MATRIX_ROW_START_CHARPOS (row)
14986 || PT > MATRIX_ROW_END_CHARPOS (row))
14988 /* if PT is not in the glyph row, give up. */
14989 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14990 must_scroll = 1;
14992 else if (rc != CURSOR_MOVEMENT_SUCCESS
14993 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14995 struct glyph_row *row1;
14997 /* If rows are bidi-reordered and point moved, back up
14998 until we find a row that does not belong to a
14999 continuation line. This is because we must consider
15000 all rows of a continued line as candidates for the
15001 new cursor positioning, since row start and end
15002 positions change non-linearly with vertical position
15003 in such rows. */
15004 /* FIXME: Revisit this when glyph ``spilling'' in
15005 continuation lines' rows is implemented for
15006 bidi-reordered rows. */
15007 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15008 MATRIX_ROW_CONTINUATION_LINE_P (row);
15009 --row)
15011 /* If we hit the beginning of the displayed portion
15012 without finding the first row of a continued
15013 line, give up. */
15014 if (row <= row1)
15016 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15017 break;
15019 eassert (row->enabled_p);
15022 if (must_scroll)
15024 else if (rc != CURSOR_MOVEMENT_SUCCESS
15025 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15026 /* Make sure this isn't a header line by any chance, since
15027 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15028 && !row->mode_line_p
15029 && make_cursor_line_fully_visible_p)
15031 if (PT == MATRIX_ROW_END_CHARPOS (row)
15032 && !row->ends_at_zv_p
15033 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15034 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15035 else if (row->height > window_box_height (w))
15037 /* If we end up in a partially visible line, let's
15038 make it fully visible, except when it's taller
15039 than the window, in which case we can't do much
15040 about it. */
15041 *scroll_step = 1;
15042 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15044 else
15046 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15047 if (!cursor_row_fully_visible_p (w, 0, 1))
15048 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15049 else
15050 rc = CURSOR_MOVEMENT_SUCCESS;
15053 else if (scroll_p)
15054 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15055 else if (rc != CURSOR_MOVEMENT_SUCCESS
15056 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15058 /* With bidi-reordered rows, there could be more than
15059 one candidate row whose start and end positions
15060 occlude point. We need to let set_cursor_from_row
15061 find the best candidate. */
15062 /* FIXME: Revisit this when glyph ``spilling'' in
15063 continuation lines' rows is implemented for
15064 bidi-reordered rows. */
15065 int rv = 0;
15069 int at_zv_p = 0, exact_match_p = 0;
15071 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15072 && PT <= MATRIX_ROW_END_CHARPOS (row)
15073 && cursor_row_p (row))
15074 rv |= set_cursor_from_row (w, row, w->current_matrix,
15075 0, 0, 0, 0);
15076 /* As soon as we've found the exact match for point,
15077 or the first suitable row whose ends_at_zv_p flag
15078 is set, we are done. */
15079 at_zv_p =
15080 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15081 if (rv && !at_zv_p
15082 && w->cursor.hpos >= 0
15083 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15084 w->cursor.vpos))
15086 struct glyph_row *candidate =
15087 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15088 struct glyph *g =
15089 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15090 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15092 exact_match_p =
15093 (BUFFERP (g->object) && g->charpos == PT)
15094 || (INTEGERP (g->object)
15095 && (g->charpos == PT
15096 || (g->charpos == 0 && endpos - 1 == PT)));
15098 if (rv && (at_zv_p || exact_match_p))
15100 rc = CURSOR_MOVEMENT_SUCCESS;
15101 break;
15103 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15104 break;
15105 ++row;
15107 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15108 || row->continued_p)
15109 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15110 || (MATRIX_ROW_START_CHARPOS (row) == PT
15111 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15112 /* If we didn't find any candidate rows, or exited the
15113 loop before all the candidates were examined, signal
15114 to the caller that this method failed. */
15115 if (rc != CURSOR_MOVEMENT_SUCCESS
15116 && !(rv
15117 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15118 && !row->continued_p))
15119 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15120 else if (rv)
15121 rc = CURSOR_MOVEMENT_SUCCESS;
15123 else
15127 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15129 rc = CURSOR_MOVEMENT_SUCCESS;
15130 break;
15132 ++row;
15134 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15135 && MATRIX_ROW_START_CHARPOS (row) == PT
15136 && cursor_row_p (row));
15141 return rc;
15144 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15145 static
15146 #endif
15147 void
15148 set_vertical_scroll_bar (struct window *w)
15150 ptrdiff_t start, end, whole;
15152 /* Calculate the start and end positions for the current window.
15153 At some point, it would be nice to choose between scrollbars
15154 which reflect the whole buffer size, with special markers
15155 indicating narrowing, and scrollbars which reflect only the
15156 visible region.
15158 Note that mini-buffers sometimes aren't displaying any text. */
15159 if (!MINI_WINDOW_P (w)
15160 || (w == XWINDOW (minibuf_window)
15161 && NILP (echo_area_buffer[0])))
15163 struct buffer *buf = XBUFFER (w->buffer);
15164 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15165 start = marker_position (w->start) - BUF_BEGV (buf);
15166 /* I don't think this is guaranteed to be right. For the
15167 moment, we'll pretend it is. */
15168 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15170 if (end < start)
15171 end = start;
15172 if (whole < (end - start))
15173 whole = end - start;
15175 else
15176 start = end = whole = 0;
15178 /* Indicate what this scroll bar ought to be displaying now. */
15179 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15180 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15181 (w, end - start, whole, start);
15185 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15186 selected_window is redisplayed.
15188 We can return without actually redisplaying the window if
15189 fonts_changed_p. In that case, redisplay_internal will
15190 retry. */
15192 static void
15193 redisplay_window (Lisp_Object window, int just_this_one_p)
15195 struct window *w = XWINDOW (window);
15196 struct frame *f = XFRAME (w->frame);
15197 struct buffer *buffer = XBUFFER (w->buffer);
15198 struct buffer *old = current_buffer;
15199 struct text_pos lpoint, opoint, startp;
15200 int update_mode_line;
15201 int tem;
15202 struct it it;
15203 /* Record it now because it's overwritten. */
15204 int current_matrix_up_to_date_p = 0;
15205 int used_current_matrix_p = 0;
15206 /* This is less strict than current_matrix_up_to_date_p.
15207 It indicates that the buffer contents and narrowing are unchanged. */
15208 int buffer_unchanged_p = 0;
15209 int temp_scroll_step = 0;
15210 ptrdiff_t count = SPECPDL_INDEX ();
15211 int rc;
15212 int centering_position = -1;
15213 int last_line_misfit = 0;
15214 ptrdiff_t beg_unchanged, end_unchanged;
15216 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15217 opoint = lpoint;
15219 /* W must be a leaf window here. */
15220 eassert (!NILP (w->buffer));
15221 #ifdef GLYPH_DEBUG
15222 *w->desired_matrix->method = 0;
15223 #endif
15225 restart:
15226 reconsider_clip_changes (w, buffer);
15228 /* Has the mode line to be updated? */
15229 update_mode_line = (w->update_mode_line
15230 || update_mode_lines
15231 || buffer->clip_changed
15232 || buffer->prevent_redisplay_optimizations_p);
15234 if (MINI_WINDOW_P (w))
15236 if (w == XWINDOW (echo_area_window)
15237 && !NILP (echo_area_buffer[0]))
15239 if (update_mode_line)
15240 /* We may have to update a tty frame's menu bar or a
15241 tool-bar. Example `M-x C-h C-h C-g'. */
15242 goto finish_menu_bars;
15243 else
15244 /* We've already displayed the echo area glyphs in this window. */
15245 goto finish_scroll_bars;
15247 else if ((w != XWINDOW (minibuf_window)
15248 || minibuf_level == 0)
15249 /* When buffer is nonempty, redisplay window normally. */
15250 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15251 /* Quail displays non-mini buffers in minibuffer window.
15252 In that case, redisplay the window normally. */
15253 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15255 /* W is a mini-buffer window, but it's not active, so clear
15256 it. */
15257 int yb = window_text_bottom_y (w);
15258 struct glyph_row *row;
15259 int y;
15261 for (y = 0, row = w->desired_matrix->rows;
15262 y < yb;
15263 y += row->height, ++row)
15264 blank_row (w, row, y);
15265 goto finish_scroll_bars;
15268 clear_glyph_matrix (w->desired_matrix);
15271 /* Otherwise set up data on this window; select its buffer and point
15272 value. */
15273 /* Really select the buffer, for the sake of buffer-local
15274 variables. */
15275 set_buffer_internal_1 (XBUFFER (w->buffer));
15277 current_matrix_up_to_date_p
15278 = (w->window_end_valid
15279 && !current_buffer->clip_changed
15280 && !current_buffer->prevent_redisplay_optimizations_p
15281 && !window_outdated (w));
15283 /* Run the window-bottom-change-functions
15284 if it is possible that the text on the screen has changed
15285 (either due to modification of the text, or any other reason). */
15286 if (!current_matrix_up_to_date_p
15287 && !NILP (Vwindow_text_change_functions))
15289 safe_run_hooks (Qwindow_text_change_functions);
15290 goto restart;
15293 beg_unchanged = BEG_UNCHANGED;
15294 end_unchanged = END_UNCHANGED;
15296 SET_TEXT_POS (opoint, PT, PT_BYTE);
15298 specbind (Qinhibit_point_motion_hooks, Qt);
15300 buffer_unchanged_p
15301 = (w->window_end_valid
15302 && !current_buffer->clip_changed
15303 && !window_outdated (w));
15305 /* When windows_or_buffers_changed is non-zero, we can't rely on
15306 the window end being valid, so set it to nil there. */
15307 if (windows_or_buffers_changed)
15309 /* If window starts on a continuation line, maybe adjust the
15310 window start in case the window's width changed. */
15311 if (XMARKER (w->start)->buffer == current_buffer)
15312 compute_window_start_on_continuation_line (w);
15314 w->window_end_valid = 0;
15317 /* Some sanity checks. */
15318 CHECK_WINDOW_END (w);
15319 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15320 emacs_abort ();
15321 if (BYTEPOS (opoint) < CHARPOS (opoint))
15322 emacs_abort ();
15324 if (mode_line_update_needed (w))
15325 update_mode_line = 1;
15327 /* Point refers normally to the selected window. For any other
15328 window, set up appropriate value. */
15329 if (!EQ (window, selected_window))
15331 ptrdiff_t new_pt = marker_position (w->pointm);
15332 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15333 if (new_pt < BEGV)
15335 new_pt = BEGV;
15336 new_pt_byte = BEGV_BYTE;
15337 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15339 else if (new_pt > (ZV - 1))
15341 new_pt = ZV;
15342 new_pt_byte = ZV_BYTE;
15343 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15346 /* We don't use SET_PT so that the point-motion hooks don't run. */
15347 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15350 /* If any of the character widths specified in the display table
15351 have changed, invalidate the width run cache. It's true that
15352 this may be a bit late to catch such changes, but the rest of
15353 redisplay goes (non-fatally) haywire when the display table is
15354 changed, so why should we worry about doing any better? */
15355 if (current_buffer->width_run_cache)
15357 struct Lisp_Char_Table *disptab = buffer_display_table ();
15359 if (! disptab_matches_widthtab
15360 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15362 invalidate_region_cache (current_buffer,
15363 current_buffer->width_run_cache,
15364 BEG, Z);
15365 recompute_width_table (current_buffer, disptab);
15369 /* If window-start is screwed up, choose a new one. */
15370 if (XMARKER (w->start)->buffer != current_buffer)
15371 goto recenter;
15373 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15375 /* If someone specified a new starting point but did not insist,
15376 check whether it can be used. */
15377 if (w->optional_new_start
15378 && CHARPOS (startp) >= BEGV
15379 && CHARPOS (startp) <= ZV)
15381 w->optional_new_start = 0;
15382 start_display (&it, w, startp);
15383 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15384 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15385 if (IT_CHARPOS (it) == PT)
15386 w->force_start = 1;
15387 /* IT may overshoot PT if text at PT is invisible. */
15388 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15389 w->force_start = 1;
15392 force_start:
15394 /* Handle case where place to start displaying has been specified,
15395 unless the specified location is outside the accessible range. */
15396 if (w->force_start || w->frozen_window_start_p)
15398 /* We set this later on if we have to adjust point. */
15399 int new_vpos = -1;
15401 w->force_start = 0;
15402 w->vscroll = 0;
15403 w->window_end_valid = 0;
15405 /* Forget any recorded base line for line number display. */
15406 if (!buffer_unchanged_p)
15407 w->base_line_number = 0;
15409 /* Redisplay the mode line. Select the buffer properly for that.
15410 Also, run the hook window-scroll-functions
15411 because we have scrolled. */
15412 /* Note, we do this after clearing force_start because
15413 if there's an error, it is better to forget about force_start
15414 than to get into an infinite loop calling the hook functions
15415 and having them get more errors. */
15416 if (!update_mode_line
15417 || ! NILP (Vwindow_scroll_functions))
15419 update_mode_line = 1;
15420 w->update_mode_line = 1;
15421 startp = run_window_scroll_functions (window, startp);
15424 w->last_modified = 0;
15425 w->last_overlay_modified = 0;
15426 if (CHARPOS (startp) < BEGV)
15427 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15428 else if (CHARPOS (startp) > ZV)
15429 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15431 /* Redisplay, then check if cursor has been set during the
15432 redisplay. Give up if new fonts were loaded. */
15433 /* We used to issue a CHECK_MARGINS argument to try_window here,
15434 but this causes scrolling to fail when point begins inside
15435 the scroll margin (bug#148) -- cyd */
15436 if (!try_window (window, startp, 0))
15438 w->force_start = 1;
15439 clear_glyph_matrix (w->desired_matrix);
15440 goto need_larger_matrices;
15443 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15445 /* If point does not appear, try to move point so it does
15446 appear. The desired matrix has been built above, so we
15447 can use it here. */
15448 new_vpos = window_box_height (w) / 2;
15451 if (!cursor_row_fully_visible_p (w, 0, 0))
15453 /* Point does appear, but on a line partly visible at end of window.
15454 Move it back to a fully-visible line. */
15455 new_vpos = window_box_height (w);
15457 else if (w->cursor.vpos >=0)
15459 /* Some people insist on not letting point enter the scroll
15460 margin, even though this part handles windows that didn't
15461 scroll at all. */
15462 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15463 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15464 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15466 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15467 below, which finds the row to move point to, advances by
15468 the Y coordinate of the _next_ row, see the definition of
15469 MATRIX_ROW_BOTTOM_Y. */
15470 if (w->cursor.vpos < margin + header_line)
15471 new_vpos
15472 = pixel_margin + (header_line
15473 ? CURRENT_HEADER_LINE_HEIGHT (w)
15474 : 0) + FRAME_LINE_HEIGHT (f);
15475 else
15477 int window_height = window_box_height (w);
15479 if (header_line)
15480 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15481 if (w->cursor.y >= window_height - pixel_margin)
15482 new_vpos = window_height - pixel_margin;
15486 /* If we need to move point for either of the above reasons,
15487 now actually do it. */
15488 if (new_vpos >= 0)
15490 struct glyph_row *row;
15492 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15493 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15494 ++row;
15496 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15497 MATRIX_ROW_START_BYTEPOS (row));
15499 if (w != XWINDOW (selected_window))
15500 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15501 else if (current_buffer == old)
15502 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15504 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15506 /* If we are highlighting the region, then we just changed
15507 the region, so redisplay to show it. */
15508 if (0 <= markpos_of_region ())
15510 clear_glyph_matrix (w->desired_matrix);
15511 if (!try_window (window, startp, 0))
15512 goto need_larger_matrices;
15516 #ifdef GLYPH_DEBUG
15517 debug_method_add (w, "forced window start");
15518 #endif
15519 goto done;
15522 /* Handle case where text has not changed, only point, and it has
15523 not moved off the frame, and we are not retrying after hscroll.
15524 (current_matrix_up_to_date_p is nonzero when retrying.) */
15525 if (current_matrix_up_to_date_p
15526 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15527 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15529 switch (rc)
15531 case CURSOR_MOVEMENT_SUCCESS:
15532 used_current_matrix_p = 1;
15533 goto done;
15535 case CURSOR_MOVEMENT_MUST_SCROLL:
15536 goto try_to_scroll;
15538 default:
15539 emacs_abort ();
15542 /* If current starting point was originally the beginning of a line
15543 but no longer is, find a new starting point. */
15544 else if (w->start_at_line_beg
15545 && !(CHARPOS (startp) <= BEGV
15546 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15548 #ifdef GLYPH_DEBUG
15549 debug_method_add (w, "recenter 1");
15550 #endif
15551 goto recenter;
15554 /* Try scrolling with try_window_id. Value is > 0 if update has
15555 been done, it is -1 if we know that the same window start will
15556 not work. It is 0 if unsuccessful for some other reason. */
15557 else if ((tem = try_window_id (w)) != 0)
15559 #ifdef GLYPH_DEBUG
15560 debug_method_add (w, "try_window_id %d", tem);
15561 #endif
15563 if (fonts_changed_p)
15564 goto need_larger_matrices;
15565 if (tem > 0)
15566 goto done;
15568 /* Otherwise try_window_id has returned -1 which means that we
15569 don't want the alternative below this comment to execute. */
15571 else if (CHARPOS (startp) >= BEGV
15572 && CHARPOS (startp) <= ZV
15573 && PT >= CHARPOS (startp)
15574 && (CHARPOS (startp) < ZV
15575 /* Avoid starting at end of buffer. */
15576 || CHARPOS (startp) == BEGV
15577 || !window_outdated (w)))
15579 int d1, d2, d3, d4, d5, d6;
15581 /* If first window line is a continuation line, and window start
15582 is inside the modified region, but the first change is before
15583 current window start, we must select a new window start.
15585 However, if this is the result of a down-mouse event (e.g. by
15586 extending the mouse-drag-overlay), we don't want to select a
15587 new window start, since that would change the position under
15588 the mouse, resulting in an unwanted mouse-movement rather
15589 than a simple mouse-click. */
15590 if (!w->start_at_line_beg
15591 && NILP (do_mouse_tracking)
15592 && CHARPOS (startp) > BEGV
15593 && CHARPOS (startp) > BEG + beg_unchanged
15594 && CHARPOS (startp) <= Z - end_unchanged
15595 /* Even if w->start_at_line_beg is nil, a new window may
15596 start at a line_beg, since that's how set_buffer_window
15597 sets it. So, we need to check the return value of
15598 compute_window_start_on_continuation_line. (See also
15599 bug#197). */
15600 && XMARKER (w->start)->buffer == current_buffer
15601 && compute_window_start_on_continuation_line (w)
15602 /* It doesn't make sense to force the window start like we
15603 do at label force_start if it is already known that point
15604 will not be visible in the resulting window, because
15605 doing so will move point from its correct position
15606 instead of scrolling the window to bring point into view.
15607 See bug#9324. */
15608 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15610 w->force_start = 1;
15611 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15612 goto force_start;
15615 #ifdef GLYPH_DEBUG
15616 debug_method_add (w, "same window start");
15617 #endif
15619 /* Try to redisplay starting at same place as before.
15620 If point has not moved off frame, accept the results. */
15621 if (!current_matrix_up_to_date_p
15622 /* Don't use try_window_reusing_current_matrix in this case
15623 because a window scroll function can have changed the
15624 buffer. */
15625 || !NILP (Vwindow_scroll_functions)
15626 || MINI_WINDOW_P (w)
15627 || !(used_current_matrix_p
15628 = try_window_reusing_current_matrix (w)))
15630 IF_DEBUG (debug_method_add (w, "1"));
15631 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15632 /* -1 means we need to scroll.
15633 0 means we need new matrices, but fonts_changed_p
15634 is set in that case, so we will detect it below. */
15635 goto try_to_scroll;
15638 if (fonts_changed_p)
15639 goto need_larger_matrices;
15641 if (w->cursor.vpos >= 0)
15643 if (!just_this_one_p
15644 || current_buffer->clip_changed
15645 || BEG_UNCHANGED < CHARPOS (startp))
15646 /* Forget any recorded base line for line number display. */
15647 w->base_line_number = 0;
15649 if (!cursor_row_fully_visible_p (w, 1, 0))
15651 clear_glyph_matrix (w->desired_matrix);
15652 last_line_misfit = 1;
15654 /* Drop through and scroll. */
15655 else
15656 goto done;
15658 else
15659 clear_glyph_matrix (w->desired_matrix);
15662 try_to_scroll:
15664 w->last_modified = 0;
15665 w->last_overlay_modified = 0;
15667 /* Redisplay the mode line. Select the buffer properly for that. */
15668 if (!update_mode_line)
15670 update_mode_line = 1;
15671 w->update_mode_line = 1;
15674 /* Try to scroll by specified few lines. */
15675 if ((scroll_conservatively
15676 || emacs_scroll_step
15677 || temp_scroll_step
15678 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15679 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15680 && CHARPOS (startp) >= BEGV
15681 && CHARPOS (startp) <= ZV)
15683 /* The function returns -1 if new fonts were loaded, 1 if
15684 successful, 0 if not successful. */
15685 int ss = try_scrolling (window, just_this_one_p,
15686 scroll_conservatively,
15687 emacs_scroll_step,
15688 temp_scroll_step, last_line_misfit);
15689 switch (ss)
15691 case SCROLLING_SUCCESS:
15692 goto done;
15694 case SCROLLING_NEED_LARGER_MATRICES:
15695 goto need_larger_matrices;
15697 case SCROLLING_FAILED:
15698 break;
15700 default:
15701 emacs_abort ();
15705 /* Finally, just choose a place to start which positions point
15706 according to user preferences. */
15708 recenter:
15710 #ifdef GLYPH_DEBUG
15711 debug_method_add (w, "recenter");
15712 #endif
15714 /* Forget any previously recorded base line for line number display. */
15715 if (!buffer_unchanged_p)
15716 w->base_line_number = 0;
15718 /* Determine the window start relative to point. */
15719 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15720 it.current_y = it.last_visible_y;
15721 if (centering_position < 0)
15723 int margin =
15724 scroll_margin > 0
15725 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15726 : 0;
15727 ptrdiff_t margin_pos = CHARPOS (startp);
15728 Lisp_Object aggressive;
15729 int scrolling_up;
15731 /* If there is a scroll margin at the top of the window, find
15732 its character position. */
15733 if (margin
15734 /* Cannot call start_display if startp is not in the
15735 accessible region of the buffer. This can happen when we
15736 have just switched to a different buffer and/or changed
15737 its restriction. In that case, startp is initialized to
15738 the character position 1 (BEGV) because we did not yet
15739 have chance to display the buffer even once. */
15740 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15742 struct it it1;
15743 void *it1data = NULL;
15745 SAVE_IT (it1, it, it1data);
15746 start_display (&it1, w, startp);
15747 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15748 margin_pos = IT_CHARPOS (it1);
15749 RESTORE_IT (&it, &it, it1data);
15751 scrolling_up = PT > margin_pos;
15752 aggressive =
15753 scrolling_up
15754 ? BVAR (current_buffer, scroll_up_aggressively)
15755 : BVAR (current_buffer, scroll_down_aggressively);
15757 if (!MINI_WINDOW_P (w)
15758 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15760 int pt_offset = 0;
15762 /* Setting scroll-conservatively overrides
15763 scroll-*-aggressively. */
15764 if (!scroll_conservatively && NUMBERP (aggressive))
15766 double float_amount = XFLOATINT (aggressive);
15768 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15769 if (pt_offset == 0 && float_amount > 0)
15770 pt_offset = 1;
15771 if (pt_offset && margin > 0)
15772 margin -= 1;
15774 /* Compute how much to move the window start backward from
15775 point so that point will be displayed where the user
15776 wants it. */
15777 if (scrolling_up)
15779 centering_position = it.last_visible_y;
15780 if (pt_offset)
15781 centering_position -= pt_offset;
15782 centering_position -=
15783 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15784 + WINDOW_HEADER_LINE_HEIGHT (w);
15785 /* Don't let point enter the scroll margin near top of
15786 the window. */
15787 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15788 centering_position = margin * FRAME_LINE_HEIGHT (f);
15790 else
15791 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15793 else
15794 /* Set the window start half the height of the window backward
15795 from point. */
15796 centering_position = window_box_height (w) / 2;
15798 move_it_vertically_backward (&it, centering_position);
15800 eassert (IT_CHARPOS (it) >= BEGV);
15802 /* The function move_it_vertically_backward may move over more
15803 than the specified y-distance. If it->w is small, e.g. a
15804 mini-buffer window, we may end up in front of the window's
15805 display area. Start displaying at the start of the line
15806 containing PT in this case. */
15807 if (it.current_y <= 0)
15809 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15810 move_it_vertically_backward (&it, 0);
15811 it.current_y = 0;
15814 it.current_x = it.hpos = 0;
15816 /* Set the window start position here explicitly, to avoid an
15817 infinite loop in case the functions in window-scroll-functions
15818 get errors. */
15819 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15821 /* Run scroll hooks. */
15822 startp = run_window_scroll_functions (window, it.current.pos);
15824 /* Redisplay the window. */
15825 if (!current_matrix_up_to_date_p
15826 || windows_or_buffers_changed
15827 || cursor_type_changed
15828 /* Don't use try_window_reusing_current_matrix in this case
15829 because it can have changed the buffer. */
15830 || !NILP (Vwindow_scroll_functions)
15831 || !just_this_one_p
15832 || MINI_WINDOW_P (w)
15833 || !(used_current_matrix_p
15834 = try_window_reusing_current_matrix (w)))
15835 try_window (window, startp, 0);
15837 /* If new fonts have been loaded (due to fontsets), give up. We
15838 have to start a new redisplay since we need to re-adjust glyph
15839 matrices. */
15840 if (fonts_changed_p)
15841 goto need_larger_matrices;
15843 /* If cursor did not appear assume that the middle of the window is
15844 in the first line of the window. Do it again with the next line.
15845 (Imagine a window of height 100, displaying two lines of height
15846 60. Moving back 50 from it->last_visible_y will end in the first
15847 line.) */
15848 if (w->cursor.vpos < 0)
15850 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15852 clear_glyph_matrix (w->desired_matrix);
15853 move_it_by_lines (&it, 1);
15854 try_window (window, it.current.pos, 0);
15856 else if (PT < IT_CHARPOS (it))
15858 clear_glyph_matrix (w->desired_matrix);
15859 move_it_by_lines (&it, -1);
15860 try_window (window, it.current.pos, 0);
15862 else
15864 /* Not much we can do about it. */
15868 /* Consider the following case: Window starts at BEGV, there is
15869 invisible, intangible text at BEGV, so that display starts at
15870 some point START > BEGV. It can happen that we are called with
15871 PT somewhere between BEGV and START. Try to handle that case. */
15872 if (w->cursor.vpos < 0)
15874 struct glyph_row *row = w->current_matrix->rows;
15875 if (row->mode_line_p)
15876 ++row;
15877 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15880 if (!cursor_row_fully_visible_p (w, 0, 0))
15882 /* If vscroll is enabled, disable it and try again. */
15883 if (w->vscroll)
15885 w->vscroll = 0;
15886 clear_glyph_matrix (w->desired_matrix);
15887 goto recenter;
15890 /* Users who set scroll-conservatively to a large number want
15891 point just above/below the scroll margin. If we ended up
15892 with point's row partially visible, move the window start to
15893 make that row fully visible and out of the margin. */
15894 if (scroll_conservatively > SCROLL_LIMIT)
15896 int margin =
15897 scroll_margin > 0
15898 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15899 : 0;
15900 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15902 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15903 clear_glyph_matrix (w->desired_matrix);
15904 if (1 == try_window (window, it.current.pos,
15905 TRY_WINDOW_CHECK_MARGINS))
15906 goto done;
15909 /* If centering point failed to make the whole line visible,
15910 put point at the top instead. That has to make the whole line
15911 visible, if it can be done. */
15912 if (centering_position == 0)
15913 goto done;
15915 clear_glyph_matrix (w->desired_matrix);
15916 centering_position = 0;
15917 goto recenter;
15920 done:
15922 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15923 w->start_at_line_beg = (CHARPOS (startp) == BEGV
15924 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
15926 /* Display the mode line, if we must. */
15927 if ((update_mode_line
15928 /* If window not full width, must redo its mode line
15929 if (a) the window to its side is being redone and
15930 (b) we do a frame-based redisplay. This is a consequence
15931 of how inverted lines are drawn in frame-based redisplay. */
15932 || (!just_this_one_p
15933 && !FRAME_WINDOW_P (f)
15934 && !WINDOW_FULL_WIDTH_P (w))
15935 /* Line number to display. */
15936 || w->base_line_pos > 0
15937 /* Column number is displayed and different from the one displayed. */
15938 || (w->column_number_displayed != -1
15939 && (w->column_number_displayed != current_column ())))
15940 /* This means that the window has a mode line. */
15941 && (WINDOW_WANTS_MODELINE_P (w)
15942 || WINDOW_WANTS_HEADER_LINE_P (w)))
15944 display_mode_lines (w);
15946 /* If mode line height has changed, arrange for a thorough
15947 immediate redisplay using the correct mode line height. */
15948 if (WINDOW_WANTS_MODELINE_P (w)
15949 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15951 fonts_changed_p = 1;
15952 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15953 = DESIRED_MODE_LINE_HEIGHT (w);
15956 /* If header line height has changed, arrange for a thorough
15957 immediate redisplay using the correct header line height. */
15958 if (WINDOW_WANTS_HEADER_LINE_P (w)
15959 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15961 fonts_changed_p = 1;
15962 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15963 = DESIRED_HEADER_LINE_HEIGHT (w);
15966 if (fonts_changed_p)
15967 goto need_larger_matrices;
15970 if (!line_number_displayed && w->base_line_pos != -1)
15972 w->base_line_pos = 0;
15973 w->base_line_number = 0;
15976 finish_menu_bars:
15978 /* When we reach a frame's selected window, redo the frame's menu bar. */
15979 if (update_mode_line
15980 && EQ (FRAME_SELECTED_WINDOW (f), window))
15982 int redisplay_menu_p = 0;
15984 if (FRAME_WINDOW_P (f))
15986 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15987 || defined (HAVE_NS) || defined (USE_GTK)
15988 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15989 #else
15990 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15991 #endif
15993 else
15994 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15996 if (redisplay_menu_p)
15997 display_menu_bar (w);
15999 #ifdef HAVE_WINDOW_SYSTEM
16000 if (FRAME_WINDOW_P (f))
16002 #if defined (USE_GTK) || defined (HAVE_NS)
16003 if (FRAME_EXTERNAL_TOOL_BAR (f))
16004 redisplay_tool_bar (f);
16005 #else
16006 if (WINDOWP (f->tool_bar_window)
16007 && (FRAME_TOOL_BAR_LINES (f) > 0
16008 || !NILP (Vauto_resize_tool_bars))
16009 && redisplay_tool_bar (f))
16010 ignore_mouse_drag_p = 1;
16011 #endif
16013 #endif
16016 #ifdef HAVE_WINDOW_SYSTEM
16017 if (FRAME_WINDOW_P (f)
16018 && update_window_fringes (w, (just_this_one_p
16019 || (!used_current_matrix_p && !overlay_arrow_seen)
16020 || w->pseudo_window_p)))
16022 update_begin (f);
16023 block_input ();
16024 if (draw_window_fringes (w, 1))
16025 x_draw_vertical_border (w);
16026 unblock_input ();
16027 update_end (f);
16029 #endif /* HAVE_WINDOW_SYSTEM */
16031 /* We go to this label, with fonts_changed_p set,
16032 if it is necessary to try again using larger glyph matrices.
16033 We have to redeem the scroll bar even in this case,
16034 because the loop in redisplay_internal expects that. */
16035 need_larger_matrices:
16037 finish_scroll_bars:
16039 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16041 /* Set the thumb's position and size. */
16042 set_vertical_scroll_bar (w);
16044 /* Note that we actually used the scroll bar attached to this
16045 window, so it shouldn't be deleted at the end of redisplay. */
16046 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16047 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16050 /* Restore current_buffer and value of point in it. The window
16051 update may have changed the buffer, so first make sure `opoint'
16052 is still valid (Bug#6177). */
16053 if (CHARPOS (opoint) < BEGV)
16054 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16055 else if (CHARPOS (opoint) > ZV)
16056 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16057 else
16058 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16060 set_buffer_internal_1 (old);
16061 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16062 shorter. This can be caused by log truncation in *Messages*. */
16063 if (CHARPOS (lpoint) <= ZV)
16064 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16066 unbind_to (count, Qnil);
16070 /* Build the complete desired matrix of WINDOW with a window start
16071 buffer position POS.
16073 Value is 1 if successful. It is zero if fonts were loaded during
16074 redisplay which makes re-adjusting glyph matrices necessary, and -1
16075 if point would appear in the scroll margins.
16076 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16077 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16078 set in FLAGS.) */
16081 try_window (Lisp_Object window, struct text_pos pos, int flags)
16083 struct window *w = XWINDOW (window);
16084 struct it it;
16085 struct glyph_row *last_text_row = NULL;
16086 struct frame *f = XFRAME (w->frame);
16088 /* Make POS the new window start. */
16089 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16091 /* Mark cursor position as unknown. No overlay arrow seen. */
16092 w->cursor.vpos = -1;
16093 overlay_arrow_seen = 0;
16095 /* Initialize iterator and info to start at POS. */
16096 start_display (&it, w, pos);
16098 /* Display all lines of W. */
16099 while (it.current_y < it.last_visible_y)
16101 if (display_line (&it))
16102 last_text_row = it.glyph_row - 1;
16103 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16104 return 0;
16107 /* Don't let the cursor end in the scroll margins. */
16108 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16109 && !MINI_WINDOW_P (w))
16111 int this_scroll_margin;
16113 if (scroll_margin > 0)
16115 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16116 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16118 else
16119 this_scroll_margin = 0;
16121 if ((w->cursor.y >= 0 /* not vscrolled */
16122 && w->cursor.y < this_scroll_margin
16123 && CHARPOS (pos) > BEGV
16124 && IT_CHARPOS (it) < ZV)
16125 /* rms: considering make_cursor_line_fully_visible_p here
16126 seems to give wrong results. We don't want to recenter
16127 when the last line is partly visible, we want to allow
16128 that case to be handled in the usual way. */
16129 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16131 w->cursor.vpos = -1;
16132 clear_glyph_matrix (w->desired_matrix);
16133 return -1;
16137 /* If bottom moved off end of frame, change mode line percentage. */
16138 if (XFASTINT (w->window_end_pos) <= 0
16139 && Z != IT_CHARPOS (it))
16140 w->update_mode_line = 1;
16142 /* Set window_end_pos to the offset of the last character displayed
16143 on the window from the end of current_buffer. Set
16144 window_end_vpos to its row number. */
16145 if (last_text_row)
16147 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16148 w->window_end_bytepos
16149 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16150 wset_window_end_pos
16151 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16152 wset_window_end_vpos
16153 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16154 eassert
16155 (MATRIX_ROW (w->desired_matrix,
16156 XFASTINT (w->window_end_vpos))->displays_text_p);
16158 else
16160 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16161 wset_window_end_pos (w, make_number (Z - ZV));
16162 wset_window_end_vpos (w, make_number (0));
16165 /* But that is not valid info until redisplay finishes. */
16166 w->window_end_valid = 0;
16167 return 1;
16172 /************************************************************************
16173 Window redisplay reusing current matrix when buffer has not changed
16174 ************************************************************************/
16176 /* Try redisplay of window W showing an unchanged buffer with a
16177 different window start than the last time it was displayed by
16178 reusing its current matrix. Value is non-zero if successful.
16179 W->start is the new window start. */
16181 static int
16182 try_window_reusing_current_matrix (struct window *w)
16184 struct frame *f = XFRAME (w->frame);
16185 struct glyph_row *bottom_row;
16186 struct it it;
16187 struct run run;
16188 struct text_pos start, new_start;
16189 int nrows_scrolled, i;
16190 struct glyph_row *last_text_row;
16191 struct glyph_row *last_reused_text_row;
16192 struct glyph_row *start_row;
16193 int start_vpos, min_y, max_y;
16195 #ifdef GLYPH_DEBUG
16196 if (inhibit_try_window_reusing)
16197 return 0;
16198 #endif
16200 if (/* This function doesn't handle terminal frames. */
16201 !FRAME_WINDOW_P (f)
16202 /* Don't try to reuse the display if windows have been split
16203 or such. */
16204 || windows_or_buffers_changed
16205 || cursor_type_changed)
16206 return 0;
16208 /* Can't do this if region may have changed. */
16209 if (0 <= markpos_of_region ()
16210 || w->region_showing
16211 || !NILP (Vshow_trailing_whitespace))
16212 return 0;
16214 /* If top-line visibility has changed, give up. */
16215 if (WINDOW_WANTS_HEADER_LINE_P (w)
16216 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16217 return 0;
16219 /* Give up if old or new display is scrolled vertically. We could
16220 make this function handle this, but right now it doesn't. */
16221 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16222 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16223 return 0;
16225 /* The variable new_start now holds the new window start. The old
16226 start `start' can be determined from the current matrix. */
16227 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16228 start = start_row->minpos;
16229 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16231 /* Clear the desired matrix for the display below. */
16232 clear_glyph_matrix (w->desired_matrix);
16234 if (CHARPOS (new_start) <= CHARPOS (start))
16236 /* Don't use this method if the display starts with an ellipsis
16237 displayed for invisible text. It's not easy to handle that case
16238 below, and it's certainly not worth the effort since this is
16239 not a frequent case. */
16240 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16241 return 0;
16243 IF_DEBUG (debug_method_add (w, "twu1"));
16245 /* Display up to a row that can be reused. The variable
16246 last_text_row is set to the last row displayed that displays
16247 text. Note that it.vpos == 0 if or if not there is a
16248 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16249 start_display (&it, w, new_start);
16250 w->cursor.vpos = -1;
16251 last_text_row = last_reused_text_row = NULL;
16253 while (it.current_y < it.last_visible_y
16254 && !fonts_changed_p)
16256 /* If we have reached into the characters in the START row,
16257 that means the line boundaries have changed. So we
16258 can't start copying with the row START. Maybe it will
16259 work to start copying with the following row. */
16260 while (IT_CHARPOS (it) > CHARPOS (start))
16262 /* Advance to the next row as the "start". */
16263 start_row++;
16264 start = start_row->minpos;
16265 /* If there are no more rows to try, or just one, give up. */
16266 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16267 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16268 || CHARPOS (start) == ZV)
16270 clear_glyph_matrix (w->desired_matrix);
16271 return 0;
16274 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16276 /* If we have reached alignment, we can copy the rest of the
16277 rows. */
16278 if (IT_CHARPOS (it) == CHARPOS (start)
16279 /* Don't accept "alignment" inside a display vector,
16280 since start_row could have started in the middle of
16281 that same display vector (thus their character
16282 positions match), and we have no way of telling if
16283 that is the case. */
16284 && it.current.dpvec_index < 0)
16285 break;
16287 if (display_line (&it))
16288 last_text_row = it.glyph_row - 1;
16292 /* A value of current_y < last_visible_y means that we stopped
16293 at the previous window start, which in turn means that we
16294 have at least one reusable row. */
16295 if (it.current_y < it.last_visible_y)
16297 struct glyph_row *row;
16299 /* IT.vpos always starts from 0; it counts text lines. */
16300 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16302 /* Find PT if not already found in the lines displayed. */
16303 if (w->cursor.vpos < 0)
16305 int dy = it.current_y - start_row->y;
16307 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16308 row = row_containing_pos (w, PT, row, NULL, dy);
16309 if (row)
16310 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16311 dy, nrows_scrolled);
16312 else
16314 clear_glyph_matrix (w->desired_matrix);
16315 return 0;
16319 /* Scroll the display. Do it before the current matrix is
16320 changed. The problem here is that update has not yet
16321 run, i.e. part of the current matrix is not up to date.
16322 scroll_run_hook will clear the cursor, and use the
16323 current matrix to get the height of the row the cursor is
16324 in. */
16325 run.current_y = start_row->y;
16326 run.desired_y = it.current_y;
16327 run.height = it.last_visible_y - it.current_y;
16329 if (run.height > 0 && run.current_y != run.desired_y)
16331 update_begin (f);
16332 FRAME_RIF (f)->update_window_begin_hook (w);
16333 FRAME_RIF (f)->clear_window_mouse_face (w);
16334 FRAME_RIF (f)->scroll_run_hook (w, &run);
16335 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16336 update_end (f);
16339 /* Shift current matrix down by nrows_scrolled lines. */
16340 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16341 rotate_matrix (w->current_matrix,
16342 start_vpos,
16343 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16344 nrows_scrolled);
16346 /* Disable lines that must be updated. */
16347 for (i = 0; i < nrows_scrolled; ++i)
16348 (start_row + i)->enabled_p = 0;
16350 /* Re-compute Y positions. */
16351 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16352 max_y = it.last_visible_y;
16353 for (row = start_row + nrows_scrolled;
16354 row < bottom_row;
16355 ++row)
16357 row->y = it.current_y;
16358 row->visible_height = row->height;
16360 if (row->y < min_y)
16361 row->visible_height -= min_y - row->y;
16362 if (row->y + row->height > max_y)
16363 row->visible_height -= row->y + row->height - max_y;
16364 if (row->fringe_bitmap_periodic_p)
16365 row->redraw_fringe_bitmaps_p = 1;
16367 it.current_y += row->height;
16369 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16370 last_reused_text_row = row;
16371 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16372 break;
16375 /* Disable lines in the current matrix which are now
16376 below the window. */
16377 for (++row; row < bottom_row; ++row)
16378 row->enabled_p = row->mode_line_p = 0;
16381 /* Update window_end_pos etc.; last_reused_text_row is the last
16382 reused row from the current matrix containing text, if any.
16383 The value of last_text_row is the last displayed line
16384 containing text. */
16385 if (last_reused_text_row)
16387 w->window_end_bytepos
16388 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16389 wset_window_end_pos
16390 (w, make_number (Z
16391 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16392 wset_window_end_vpos
16393 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16394 w->current_matrix)));
16396 else if (last_text_row)
16398 w->window_end_bytepos
16399 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16400 wset_window_end_pos
16401 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16402 wset_window_end_vpos
16403 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16404 w->desired_matrix)));
16406 else
16408 /* This window must be completely empty. */
16409 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16410 wset_window_end_pos (w, make_number (Z - ZV));
16411 wset_window_end_vpos (w, make_number (0));
16413 w->window_end_valid = 0;
16415 /* Update hint: don't try scrolling again in update_window. */
16416 w->desired_matrix->no_scrolling_p = 1;
16418 #ifdef GLYPH_DEBUG
16419 debug_method_add (w, "try_window_reusing_current_matrix 1");
16420 #endif
16421 return 1;
16423 else if (CHARPOS (new_start) > CHARPOS (start))
16425 struct glyph_row *pt_row, *row;
16426 struct glyph_row *first_reusable_row;
16427 struct glyph_row *first_row_to_display;
16428 int dy;
16429 int yb = window_text_bottom_y (w);
16431 /* Find the row starting at new_start, if there is one. Don't
16432 reuse a partially visible line at the end. */
16433 first_reusable_row = start_row;
16434 while (first_reusable_row->enabled_p
16435 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16436 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16437 < CHARPOS (new_start)))
16438 ++first_reusable_row;
16440 /* Give up if there is no row to reuse. */
16441 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16442 || !first_reusable_row->enabled_p
16443 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16444 != CHARPOS (new_start)))
16445 return 0;
16447 /* We can reuse fully visible rows beginning with
16448 first_reusable_row to the end of the window. Set
16449 first_row_to_display to the first row that cannot be reused.
16450 Set pt_row to the row containing point, if there is any. */
16451 pt_row = NULL;
16452 for (first_row_to_display = first_reusable_row;
16453 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16454 ++first_row_to_display)
16456 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16457 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16458 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16459 && first_row_to_display->ends_at_zv_p
16460 && pt_row == NULL)))
16461 pt_row = first_row_to_display;
16464 /* Start displaying at the start of first_row_to_display. */
16465 eassert (first_row_to_display->y < yb);
16466 init_to_row_start (&it, w, first_row_to_display);
16468 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16469 - start_vpos);
16470 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16471 - nrows_scrolled);
16472 it.current_y = (first_row_to_display->y - first_reusable_row->y
16473 + WINDOW_HEADER_LINE_HEIGHT (w));
16475 /* Display lines beginning with first_row_to_display in the
16476 desired matrix. Set last_text_row to the last row displayed
16477 that displays text. */
16478 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16479 if (pt_row == NULL)
16480 w->cursor.vpos = -1;
16481 last_text_row = NULL;
16482 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16483 if (display_line (&it))
16484 last_text_row = it.glyph_row - 1;
16486 /* If point is in a reused row, adjust y and vpos of the cursor
16487 position. */
16488 if (pt_row)
16490 w->cursor.vpos -= nrows_scrolled;
16491 w->cursor.y -= first_reusable_row->y - start_row->y;
16494 /* Give up if point isn't in a row displayed or reused. (This
16495 also handles the case where w->cursor.vpos < nrows_scrolled
16496 after the calls to display_line, which can happen with scroll
16497 margins. See bug#1295.) */
16498 if (w->cursor.vpos < 0)
16500 clear_glyph_matrix (w->desired_matrix);
16501 return 0;
16504 /* Scroll the display. */
16505 run.current_y = first_reusable_row->y;
16506 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16507 run.height = it.last_visible_y - run.current_y;
16508 dy = run.current_y - run.desired_y;
16510 if (run.height)
16512 update_begin (f);
16513 FRAME_RIF (f)->update_window_begin_hook (w);
16514 FRAME_RIF (f)->clear_window_mouse_face (w);
16515 FRAME_RIF (f)->scroll_run_hook (w, &run);
16516 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16517 update_end (f);
16520 /* Adjust Y positions of reused rows. */
16521 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16522 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16523 max_y = it.last_visible_y;
16524 for (row = first_reusable_row; row < first_row_to_display; ++row)
16526 row->y -= dy;
16527 row->visible_height = row->height;
16528 if (row->y < min_y)
16529 row->visible_height -= min_y - row->y;
16530 if (row->y + row->height > max_y)
16531 row->visible_height -= row->y + row->height - max_y;
16532 if (row->fringe_bitmap_periodic_p)
16533 row->redraw_fringe_bitmaps_p = 1;
16536 /* Scroll the current matrix. */
16537 eassert (nrows_scrolled > 0);
16538 rotate_matrix (w->current_matrix,
16539 start_vpos,
16540 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16541 -nrows_scrolled);
16543 /* Disable rows not reused. */
16544 for (row -= nrows_scrolled; row < bottom_row; ++row)
16545 row->enabled_p = 0;
16547 /* Point may have moved to a different line, so we cannot assume that
16548 the previous cursor position is valid; locate the correct row. */
16549 if (pt_row)
16551 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16552 row < bottom_row
16553 && PT >= MATRIX_ROW_END_CHARPOS (row)
16554 && !row->ends_at_zv_p;
16555 row++)
16557 w->cursor.vpos++;
16558 w->cursor.y = row->y;
16560 if (row < bottom_row)
16562 /* Can't simply scan the row for point with
16563 bidi-reordered glyph rows. Let set_cursor_from_row
16564 figure out where to put the cursor, and if it fails,
16565 give up. */
16566 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16568 if (!set_cursor_from_row (w, row, w->current_matrix,
16569 0, 0, 0, 0))
16571 clear_glyph_matrix (w->desired_matrix);
16572 return 0;
16575 else
16577 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16578 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16580 for (; glyph < end
16581 && (!BUFFERP (glyph->object)
16582 || glyph->charpos < PT);
16583 glyph++)
16585 w->cursor.hpos++;
16586 w->cursor.x += glyph->pixel_width;
16592 /* Adjust window end. A null value of last_text_row means that
16593 the window end is in reused rows which in turn means that
16594 only its vpos can have changed. */
16595 if (last_text_row)
16597 w->window_end_bytepos
16598 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16599 wset_window_end_pos
16600 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16601 wset_window_end_vpos
16602 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16603 w->desired_matrix)));
16605 else
16607 wset_window_end_vpos
16608 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16611 w->window_end_valid = 0;
16612 w->desired_matrix->no_scrolling_p = 1;
16614 #ifdef GLYPH_DEBUG
16615 debug_method_add (w, "try_window_reusing_current_matrix 2");
16616 #endif
16617 return 1;
16620 return 0;
16625 /************************************************************************
16626 Window redisplay reusing current matrix when buffer has changed
16627 ************************************************************************/
16629 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16630 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16631 ptrdiff_t *, ptrdiff_t *);
16632 static struct glyph_row *
16633 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16634 struct glyph_row *);
16637 /* Return the last row in MATRIX displaying text. If row START is
16638 non-null, start searching with that row. IT gives the dimensions
16639 of the display. Value is null if matrix is empty; otherwise it is
16640 a pointer to the row found. */
16642 static struct glyph_row *
16643 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16644 struct glyph_row *start)
16646 struct glyph_row *row, *row_found;
16648 /* Set row_found to the last row in IT->w's current matrix
16649 displaying text. The loop looks funny but think of partially
16650 visible lines. */
16651 row_found = NULL;
16652 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16653 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16655 eassert (row->enabled_p);
16656 row_found = row;
16657 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16658 break;
16659 ++row;
16662 return row_found;
16666 /* Return the last row in the current matrix of W that is not affected
16667 by changes at the start of current_buffer that occurred since W's
16668 current matrix was built. Value is null if no such row exists.
16670 BEG_UNCHANGED us the number of characters unchanged at the start of
16671 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16672 first changed character in current_buffer. Characters at positions <
16673 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16674 when the current matrix was built. */
16676 static struct glyph_row *
16677 find_last_unchanged_at_beg_row (struct window *w)
16679 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16680 struct glyph_row *row;
16681 struct glyph_row *row_found = NULL;
16682 int yb = window_text_bottom_y (w);
16684 /* Find the last row displaying unchanged text. */
16685 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16686 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16687 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16688 ++row)
16690 if (/* If row ends before first_changed_pos, it is unchanged,
16691 except in some case. */
16692 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16693 /* When row ends in ZV and we write at ZV it is not
16694 unchanged. */
16695 && !row->ends_at_zv_p
16696 /* When first_changed_pos is the end of a continued line,
16697 row is not unchanged because it may be no longer
16698 continued. */
16699 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16700 && (row->continued_p
16701 || row->exact_window_width_line_p))
16702 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16703 needs to be recomputed, so don't consider this row as
16704 unchanged. This happens when the last line was
16705 bidi-reordered and was killed immediately before this
16706 redisplay cycle. In that case, ROW->end stores the
16707 buffer position of the first visual-order character of
16708 the killed text, which is now beyond ZV. */
16709 && CHARPOS (row->end.pos) <= ZV)
16710 row_found = row;
16712 /* Stop if last visible row. */
16713 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16714 break;
16717 return row_found;
16721 /* Find the first glyph row in the current matrix of W that is not
16722 affected by changes at the end of current_buffer since the
16723 time W's current matrix was built.
16725 Return in *DELTA the number of chars by which buffer positions in
16726 unchanged text at the end of current_buffer must be adjusted.
16728 Return in *DELTA_BYTES the corresponding number of bytes.
16730 Value is null if no such row exists, i.e. all rows are affected by
16731 changes. */
16733 static struct glyph_row *
16734 find_first_unchanged_at_end_row (struct window *w,
16735 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16737 struct glyph_row *row;
16738 struct glyph_row *row_found = NULL;
16740 *delta = *delta_bytes = 0;
16742 /* Display must not have been paused, otherwise the current matrix
16743 is not up to date. */
16744 eassert (w->window_end_valid);
16746 /* A value of window_end_pos >= END_UNCHANGED means that the window
16747 end is in the range of changed text. If so, there is no
16748 unchanged row at the end of W's current matrix. */
16749 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16750 return NULL;
16752 /* Set row to the last row in W's current matrix displaying text. */
16753 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16755 /* If matrix is entirely empty, no unchanged row exists. */
16756 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16758 /* The value of row is the last glyph row in the matrix having a
16759 meaningful buffer position in it. The end position of row
16760 corresponds to window_end_pos. This allows us to translate
16761 buffer positions in the current matrix to current buffer
16762 positions for characters not in changed text. */
16763 ptrdiff_t Z_old =
16764 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16765 ptrdiff_t Z_BYTE_old =
16766 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16767 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16768 struct glyph_row *first_text_row
16769 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16771 *delta = Z - Z_old;
16772 *delta_bytes = Z_BYTE - Z_BYTE_old;
16774 /* Set last_unchanged_pos to the buffer position of the last
16775 character in the buffer that has not been changed. Z is the
16776 index + 1 of the last character in current_buffer, i.e. by
16777 subtracting END_UNCHANGED we get the index of the last
16778 unchanged character, and we have to add BEG to get its buffer
16779 position. */
16780 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16781 last_unchanged_pos_old = last_unchanged_pos - *delta;
16783 /* Search backward from ROW for a row displaying a line that
16784 starts at a minimum position >= last_unchanged_pos_old. */
16785 for (; row > first_text_row; --row)
16787 /* This used to abort, but it can happen.
16788 It is ok to just stop the search instead here. KFS. */
16789 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16790 break;
16792 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16793 row_found = row;
16797 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16799 return row_found;
16803 /* Make sure that glyph rows in the current matrix of window W
16804 reference the same glyph memory as corresponding rows in the
16805 frame's frame matrix. This function is called after scrolling W's
16806 current matrix on a terminal frame in try_window_id and
16807 try_window_reusing_current_matrix. */
16809 static void
16810 sync_frame_with_window_matrix_rows (struct window *w)
16812 struct frame *f = XFRAME (w->frame);
16813 struct glyph_row *window_row, *window_row_end, *frame_row;
16815 /* Preconditions: W must be a leaf window and full-width. Its frame
16816 must have a frame matrix. */
16817 eassert (NILP (w->hchild) && NILP (w->vchild));
16818 eassert (WINDOW_FULL_WIDTH_P (w));
16819 eassert (!FRAME_WINDOW_P (f));
16821 /* If W is a full-width window, glyph pointers in W's current matrix
16822 have, by definition, to be the same as glyph pointers in the
16823 corresponding frame matrix. Note that frame matrices have no
16824 marginal areas (see build_frame_matrix). */
16825 window_row = w->current_matrix->rows;
16826 window_row_end = window_row + w->current_matrix->nrows;
16827 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16828 while (window_row < window_row_end)
16830 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16831 struct glyph *end = window_row->glyphs[LAST_AREA];
16833 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16834 frame_row->glyphs[TEXT_AREA] = start;
16835 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16836 frame_row->glyphs[LAST_AREA] = end;
16838 /* Disable frame rows whose corresponding window rows have
16839 been disabled in try_window_id. */
16840 if (!window_row->enabled_p)
16841 frame_row->enabled_p = 0;
16843 ++window_row, ++frame_row;
16848 /* Find the glyph row in window W containing CHARPOS. Consider all
16849 rows between START and END (not inclusive). END null means search
16850 all rows to the end of the display area of W. Value is the row
16851 containing CHARPOS or null. */
16853 struct glyph_row *
16854 row_containing_pos (struct window *w, ptrdiff_t charpos,
16855 struct glyph_row *start, struct glyph_row *end, int dy)
16857 struct glyph_row *row = start;
16858 struct glyph_row *best_row = NULL;
16859 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16860 int last_y;
16862 /* If we happen to start on a header-line, skip that. */
16863 if (row->mode_line_p)
16864 ++row;
16866 if ((end && row >= end) || !row->enabled_p)
16867 return NULL;
16869 last_y = window_text_bottom_y (w) - dy;
16871 while (1)
16873 /* Give up if we have gone too far. */
16874 if (end && row >= end)
16875 return NULL;
16876 /* This formerly returned if they were equal.
16877 I think that both quantities are of a "last plus one" type;
16878 if so, when they are equal, the row is within the screen. -- rms. */
16879 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16880 return NULL;
16882 /* If it is in this row, return this row. */
16883 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16884 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16885 /* The end position of a row equals the start
16886 position of the next row. If CHARPOS is there, we
16887 would rather display it in the next line, except
16888 when this line ends in ZV. */
16889 && !row->ends_at_zv_p
16890 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16891 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16893 struct glyph *g;
16895 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16896 || (!best_row && !row->continued_p))
16897 return row;
16898 /* In bidi-reordered rows, there could be several rows
16899 occluding point, all of them belonging to the same
16900 continued line. We need to find the row which fits
16901 CHARPOS the best. */
16902 for (g = row->glyphs[TEXT_AREA];
16903 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16904 g++)
16906 if (!STRINGP (g->object))
16908 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16910 mindif = eabs (g->charpos - charpos);
16911 best_row = row;
16912 /* Exact match always wins. */
16913 if (mindif == 0)
16914 return best_row;
16919 else if (best_row && !row->continued_p)
16920 return best_row;
16921 ++row;
16926 /* Try to redisplay window W by reusing its existing display. W's
16927 current matrix must be up to date when this function is called,
16928 i.e. window_end_valid must be nonzero.
16930 Value is
16932 1 if display has been updated
16933 0 if otherwise unsuccessful
16934 -1 if redisplay with same window start is known not to succeed
16936 The following steps are performed:
16938 1. Find the last row in the current matrix of W that is not
16939 affected by changes at the start of current_buffer. If no such row
16940 is found, give up.
16942 2. Find the first row in W's current matrix that is not affected by
16943 changes at the end of current_buffer. Maybe there is no such row.
16945 3. Display lines beginning with the row + 1 found in step 1 to the
16946 row found in step 2 or, if step 2 didn't find a row, to the end of
16947 the window.
16949 4. If cursor is not known to appear on the window, give up.
16951 5. If display stopped at the row found in step 2, scroll the
16952 display and current matrix as needed.
16954 6. Maybe display some lines at the end of W, if we must. This can
16955 happen under various circumstances, like a partially visible line
16956 becoming fully visible, or because newly displayed lines are displayed
16957 in smaller font sizes.
16959 7. Update W's window end information. */
16961 static int
16962 try_window_id (struct window *w)
16964 struct frame *f = XFRAME (w->frame);
16965 struct glyph_matrix *current_matrix = w->current_matrix;
16966 struct glyph_matrix *desired_matrix = w->desired_matrix;
16967 struct glyph_row *last_unchanged_at_beg_row;
16968 struct glyph_row *first_unchanged_at_end_row;
16969 struct glyph_row *row;
16970 struct glyph_row *bottom_row;
16971 int bottom_vpos;
16972 struct it it;
16973 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
16974 int dvpos, dy;
16975 struct text_pos start_pos;
16976 struct run run;
16977 int first_unchanged_at_end_vpos = 0;
16978 struct glyph_row *last_text_row, *last_text_row_at_end;
16979 struct text_pos start;
16980 ptrdiff_t first_changed_charpos, last_changed_charpos;
16982 #ifdef GLYPH_DEBUG
16983 if (inhibit_try_window_id)
16984 return 0;
16985 #endif
16987 /* This is handy for debugging. */
16988 #if 0
16989 #define GIVE_UP(X) \
16990 do { \
16991 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16992 return 0; \
16993 } while (0)
16994 #else
16995 #define GIVE_UP(X) return 0
16996 #endif
16998 SET_TEXT_POS_FROM_MARKER (start, w->start);
17000 /* Don't use this for mini-windows because these can show
17001 messages and mini-buffers, and we don't handle that here. */
17002 if (MINI_WINDOW_P (w))
17003 GIVE_UP (1);
17005 /* This flag is used to prevent redisplay optimizations. */
17006 if (windows_or_buffers_changed || cursor_type_changed)
17007 GIVE_UP (2);
17009 /* Verify that narrowing has not changed.
17010 Also verify that we were not told to prevent redisplay optimizations.
17011 It would be nice to further
17012 reduce the number of cases where this prevents try_window_id. */
17013 if (current_buffer->clip_changed
17014 || current_buffer->prevent_redisplay_optimizations_p)
17015 GIVE_UP (3);
17017 /* Window must either use window-based redisplay or be full width. */
17018 if (!FRAME_WINDOW_P (f)
17019 && (!FRAME_LINE_INS_DEL_OK (f)
17020 || !WINDOW_FULL_WIDTH_P (w)))
17021 GIVE_UP (4);
17023 /* Give up if point is known NOT to appear in W. */
17024 if (PT < CHARPOS (start))
17025 GIVE_UP (5);
17027 /* Another way to prevent redisplay optimizations. */
17028 if (w->last_modified == 0)
17029 GIVE_UP (6);
17031 /* Verify that window is not hscrolled. */
17032 if (w->hscroll != 0)
17033 GIVE_UP (7);
17035 /* Verify that display wasn't paused. */
17036 if (!w->window_end_valid)
17037 GIVE_UP (8);
17039 /* Can't use this if highlighting a region because a cursor movement
17040 will do more than just set the cursor. */
17041 if (0 <= markpos_of_region ())
17042 GIVE_UP (9);
17044 /* Likewise if highlighting trailing whitespace. */
17045 if (!NILP (Vshow_trailing_whitespace))
17046 GIVE_UP (11);
17048 /* Likewise if showing a region. */
17049 if (w->region_showing)
17050 GIVE_UP (10);
17052 /* Can't use this if overlay arrow position and/or string have
17053 changed. */
17054 if (overlay_arrows_changed_p ())
17055 GIVE_UP (12);
17057 /* When word-wrap is on, adding a space to the first word of a
17058 wrapped line can change the wrap position, altering the line
17059 above it. It might be worthwhile to handle this more
17060 intelligently, but for now just redisplay from scratch. */
17061 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17062 GIVE_UP (21);
17064 /* Under bidi reordering, adding or deleting a character in the
17065 beginning of a paragraph, before the first strong directional
17066 character, can change the base direction of the paragraph (unless
17067 the buffer specifies a fixed paragraph direction), which will
17068 require to redisplay the whole paragraph. It might be worthwhile
17069 to find the paragraph limits and widen the range of redisplayed
17070 lines to that, but for now just give up this optimization and
17071 redisplay from scratch. */
17072 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17073 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17074 GIVE_UP (22);
17076 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17077 only if buffer has really changed. The reason is that the gap is
17078 initially at Z for freshly visited files. The code below would
17079 set end_unchanged to 0 in that case. */
17080 if (MODIFF > SAVE_MODIFF
17081 /* This seems to happen sometimes after saving a buffer. */
17082 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17084 if (GPT - BEG < BEG_UNCHANGED)
17085 BEG_UNCHANGED = GPT - BEG;
17086 if (Z - GPT < END_UNCHANGED)
17087 END_UNCHANGED = Z - GPT;
17090 /* The position of the first and last character that has been changed. */
17091 first_changed_charpos = BEG + BEG_UNCHANGED;
17092 last_changed_charpos = Z - END_UNCHANGED;
17094 /* If window starts after a line end, and the last change is in
17095 front of that newline, then changes don't affect the display.
17096 This case happens with stealth-fontification. Note that although
17097 the display is unchanged, glyph positions in the matrix have to
17098 be adjusted, of course. */
17099 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17100 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17101 && ((last_changed_charpos < CHARPOS (start)
17102 && CHARPOS (start) == BEGV)
17103 || (last_changed_charpos < CHARPOS (start) - 1
17104 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17106 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17107 struct glyph_row *r0;
17109 /* Compute how many chars/bytes have been added to or removed
17110 from the buffer. */
17111 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17112 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17113 Z_delta = Z - Z_old;
17114 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17116 /* Give up if PT is not in the window. Note that it already has
17117 been checked at the start of try_window_id that PT is not in
17118 front of the window start. */
17119 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17120 GIVE_UP (13);
17122 /* If window start is unchanged, we can reuse the whole matrix
17123 as is, after adjusting glyph positions. No need to compute
17124 the window end again, since its offset from Z hasn't changed. */
17125 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17126 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17127 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17128 /* PT must not be in a partially visible line. */
17129 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17130 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17132 /* Adjust positions in the glyph matrix. */
17133 if (Z_delta || Z_delta_bytes)
17135 struct glyph_row *r1
17136 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17137 increment_matrix_positions (w->current_matrix,
17138 MATRIX_ROW_VPOS (r0, current_matrix),
17139 MATRIX_ROW_VPOS (r1, current_matrix),
17140 Z_delta, Z_delta_bytes);
17143 /* Set the cursor. */
17144 row = row_containing_pos (w, PT, r0, NULL, 0);
17145 if (row)
17146 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17147 else
17148 emacs_abort ();
17149 return 1;
17153 /* Handle the case that changes are all below what is displayed in
17154 the window, and that PT is in the window. This shortcut cannot
17155 be taken if ZV is visible in the window, and text has been added
17156 there that is visible in the window. */
17157 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17158 /* ZV is not visible in the window, or there are no
17159 changes at ZV, actually. */
17160 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17161 || first_changed_charpos == last_changed_charpos))
17163 struct glyph_row *r0;
17165 /* Give up if PT is not in the window. Note that it already has
17166 been checked at the start of try_window_id that PT is not in
17167 front of the window start. */
17168 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17169 GIVE_UP (14);
17171 /* If window start is unchanged, we can reuse the whole matrix
17172 as is, without changing glyph positions since no text has
17173 been added/removed in front of the window end. */
17174 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17175 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17176 /* PT must not be in a partially visible line. */
17177 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17178 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17180 /* We have to compute the window end anew since text
17181 could have been added/removed after it. */
17182 wset_window_end_pos
17183 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17184 w->window_end_bytepos
17185 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17187 /* Set the cursor. */
17188 row = row_containing_pos (w, PT, r0, NULL, 0);
17189 if (row)
17190 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17191 else
17192 emacs_abort ();
17193 return 2;
17197 /* Give up if window start is in the changed area.
17199 The condition used to read
17201 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17203 but why that was tested escapes me at the moment. */
17204 if (CHARPOS (start) >= first_changed_charpos
17205 && CHARPOS (start) <= last_changed_charpos)
17206 GIVE_UP (15);
17208 /* Check that window start agrees with the start of the first glyph
17209 row in its current matrix. Check this after we know the window
17210 start is not in changed text, otherwise positions would not be
17211 comparable. */
17212 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17213 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17214 GIVE_UP (16);
17216 /* Give up if the window ends in strings. Overlay strings
17217 at the end are difficult to handle, so don't try. */
17218 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17219 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17220 GIVE_UP (20);
17222 /* Compute the position at which we have to start displaying new
17223 lines. Some of the lines at the top of the window might be
17224 reusable because they are not displaying changed text. Find the
17225 last row in W's current matrix not affected by changes at the
17226 start of current_buffer. Value is null if changes start in the
17227 first line of window. */
17228 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17229 if (last_unchanged_at_beg_row)
17231 /* Avoid starting to display in the middle of a character, a TAB
17232 for instance. This is easier than to set up the iterator
17233 exactly, and it's not a frequent case, so the additional
17234 effort wouldn't really pay off. */
17235 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17236 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17237 && last_unchanged_at_beg_row > w->current_matrix->rows)
17238 --last_unchanged_at_beg_row;
17240 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17241 GIVE_UP (17);
17243 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17244 GIVE_UP (18);
17245 start_pos = it.current.pos;
17247 /* Start displaying new lines in the desired matrix at the same
17248 vpos we would use in the current matrix, i.e. below
17249 last_unchanged_at_beg_row. */
17250 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17251 current_matrix);
17252 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17253 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17255 eassert (it.hpos == 0 && it.current_x == 0);
17257 else
17259 /* There are no reusable lines at the start of the window.
17260 Start displaying in the first text line. */
17261 start_display (&it, w, start);
17262 it.vpos = it.first_vpos;
17263 start_pos = it.current.pos;
17266 /* Find the first row that is not affected by changes at the end of
17267 the buffer. Value will be null if there is no unchanged row, in
17268 which case we must redisplay to the end of the window. delta
17269 will be set to the value by which buffer positions beginning with
17270 first_unchanged_at_end_row have to be adjusted due to text
17271 changes. */
17272 first_unchanged_at_end_row
17273 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17274 IF_DEBUG (debug_delta = delta);
17275 IF_DEBUG (debug_delta_bytes = delta_bytes);
17277 /* Set stop_pos to the buffer position up to which we will have to
17278 display new lines. If first_unchanged_at_end_row != NULL, this
17279 is the buffer position of the start of the line displayed in that
17280 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17281 that we don't stop at a buffer position. */
17282 stop_pos = 0;
17283 if (first_unchanged_at_end_row)
17285 eassert (last_unchanged_at_beg_row == NULL
17286 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17288 /* If this is a continuation line, move forward to the next one
17289 that isn't. Changes in lines above affect this line.
17290 Caution: this may move first_unchanged_at_end_row to a row
17291 not displaying text. */
17292 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17293 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17294 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17295 < it.last_visible_y))
17296 ++first_unchanged_at_end_row;
17298 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17299 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17300 >= it.last_visible_y))
17301 first_unchanged_at_end_row = NULL;
17302 else
17304 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17305 + delta);
17306 first_unchanged_at_end_vpos
17307 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17308 eassert (stop_pos >= Z - END_UNCHANGED);
17311 else if (last_unchanged_at_beg_row == NULL)
17312 GIVE_UP (19);
17315 #ifdef GLYPH_DEBUG
17317 /* Either there is no unchanged row at the end, or the one we have
17318 now displays text. This is a necessary condition for the window
17319 end pos calculation at the end of this function. */
17320 eassert (first_unchanged_at_end_row == NULL
17321 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17323 debug_last_unchanged_at_beg_vpos
17324 = (last_unchanged_at_beg_row
17325 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17326 : -1);
17327 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17329 #endif /* GLYPH_DEBUG */
17332 /* Display new lines. Set last_text_row to the last new line
17333 displayed which has text on it, i.e. might end up as being the
17334 line where the window_end_vpos is. */
17335 w->cursor.vpos = -1;
17336 last_text_row = NULL;
17337 overlay_arrow_seen = 0;
17338 while (it.current_y < it.last_visible_y
17339 && !fonts_changed_p
17340 && (first_unchanged_at_end_row == NULL
17341 || IT_CHARPOS (it) < stop_pos))
17343 if (display_line (&it))
17344 last_text_row = it.glyph_row - 1;
17347 if (fonts_changed_p)
17348 return -1;
17351 /* Compute differences in buffer positions, y-positions etc. for
17352 lines reused at the bottom of the window. Compute what we can
17353 scroll. */
17354 if (first_unchanged_at_end_row
17355 /* No lines reused because we displayed everything up to the
17356 bottom of the window. */
17357 && it.current_y < it.last_visible_y)
17359 dvpos = (it.vpos
17360 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17361 current_matrix));
17362 dy = it.current_y - first_unchanged_at_end_row->y;
17363 run.current_y = first_unchanged_at_end_row->y;
17364 run.desired_y = run.current_y + dy;
17365 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17367 else
17369 delta = delta_bytes = dvpos = dy
17370 = run.current_y = run.desired_y = run.height = 0;
17371 first_unchanged_at_end_row = NULL;
17373 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17376 /* Find the cursor if not already found. We have to decide whether
17377 PT will appear on this window (it sometimes doesn't, but this is
17378 not a very frequent case.) This decision has to be made before
17379 the current matrix is altered. A value of cursor.vpos < 0 means
17380 that PT is either in one of the lines beginning at
17381 first_unchanged_at_end_row or below the window. Don't care for
17382 lines that might be displayed later at the window end; as
17383 mentioned, this is not a frequent case. */
17384 if (w->cursor.vpos < 0)
17386 /* Cursor in unchanged rows at the top? */
17387 if (PT < CHARPOS (start_pos)
17388 && last_unchanged_at_beg_row)
17390 row = row_containing_pos (w, PT,
17391 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17392 last_unchanged_at_beg_row + 1, 0);
17393 if (row)
17394 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17397 /* Start from first_unchanged_at_end_row looking for PT. */
17398 else if (first_unchanged_at_end_row)
17400 row = row_containing_pos (w, PT - delta,
17401 first_unchanged_at_end_row, NULL, 0);
17402 if (row)
17403 set_cursor_from_row (w, row, w->current_matrix, delta,
17404 delta_bytes, dy, dvpos);
17407 /* Give up if cursor was not found. */
17408 if (w->cursor.vpos < 0)
17410 clear_glyph_matrix (w->desired_matrix);
17411 return -1;
17415 /* Don't let the cursor end in the scroll margins. */
17417 int this_scroll_margin, cursor_height;
17419 this_scroll_margin =
17420 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17421 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17422 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17424 if ((w->cursor.y < this_scroll_margin
17425 && CHARPOS (start) > BEGV)
17426 /* Old redisplay didn't take scroll margin into account at the bottom,
17427 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17428 || (w->cursor.y + (make_cursor_line_fully_visible_p
17429 ? cursor_height + this_scroll_margin
17430 : 1)) > it.last_visible_y)
17432 w->cursor.vpos = -1;
17433 clear_glyph_matrix (w->desired_matrix);
17434 return -1;
17438 /* Scroll the display. Do it before changing the current matrix so
17439 that xterm.c doesn't get confused about where the cursor glyph is
17440 found. */
17441 if (dy && run.height)
17443 update_begin (f);
17445 if (FRAME_WINDOW_P (f))
17447 FRAME_RIF (f)->update_window_begin_hook (w);
17448 FRAME_RIF (f)->clear_window_mouse_face (w);
17449 FRAME_RIF (f)->scroll_run_hook (w, &run);
17450 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17452 else
17454 /* Terminal frame. In this case, dvpos gives the number of
17455 lines to scroll by; dvpos < 0 means scroll up. */
17456 int from_vpos
17457 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17458 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17459 int end = (WINDOW_TOP_EDGE_LINE (w)
17460 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17461 + window_internal_height (w));
17463 #if defined (HAVE_GPM) || defined (MSDOS)
17464 x_clear_window_mouse_face (w);
17465 #endif
17466 /* Perform the operation on the screen. */
17467 if (dvpos > 0)
17469 /* Scroll last_unchanged_at_beg_row to the end of the
17470 window down dvpos lines. */
17471 set_terminal_window (f, end);
17473 /* On dumb terminals delete dvpos lines at the end
17474 before inserting dvpos empty lines. */
17475 if (!FRAME_SCROLL_REGION_OK (f))
17476 ins_del_lines (f, end - dvpos, -dvpos);
17478 /* Insert dvpos empty lines in front of
17479 last_unchanged_at_beg_row. */
17480 ins_del_lines (f, from, dvpos);
17482 else if (dvpos < 0)
17484 /* Scroll up last_unchanged_at_beg_vpos to the end of
17485 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17486 set_terminal_window (f, end);
17488 /* Delete dvpos lines in front of
17489 last_unchanged_at_beg_vpos. ins_del_lines will set
17490 the cursor to the given vpos and emit |dvpos| delete
17491 line sequences. */
17492 ins_del_lines (f, from + dvpos, dvpos);
17494 /* On a dumb terminal insert dvpos empty lines at the
17495 end. */
17496 if (!FRAME_SCROLL_REGION_OK (f))
17497 ins_del_lines (f, end + dvpos, -dvpos);
17500 set_terminal_window (f, 0);
17503 update_end (f);
17506 /* Shift reused rows of the current matrix to the right position.
17507 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17508 text. */
17509 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17510 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17511 if (dvpos < 0)
17513 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17514 bottom_vpos, dvpos);
17515 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17516 bottom_vpos);
17518 else if (dvpos > 0)
17520 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17521 bottom_vpos, dvpos);
17522 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17523 first_unchanged_at_end_vpos + dvpos);
17526 /* For frame-based redisplay, make sure that current frame and window
17527 matrix are in sync with respect to glyph memory. */
17528 if (!FRAME_WINDOW_P (f))
17529 sync_frame_with_window_matrix_rows (w);
17531 /* Adjust buffer positions in reused rows. */
17532 if (delta || delta_bytes)
17533 increment_matrix_positions (current_matrix,
17534 first_unchanged_at_end_vpos + dvpos,
17535 bottom_vpos, delta, delta_bytes);
17537 /* Adjust Y positions. */
17538 if (dy)
17539 shift_glyph_matrix (w, current_matrix,
17540 first_unchanged_at_end_vpos + dvpos,
17541 bottom_vpos, dy);
17543 if (first_unchanged_at_end_row)
17545 first_unchanged_at_end_row += dvpos;
17546 if (first_unchanged_at_end_row->y >= it.last_visible_y
17547 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17548 first_unchanged_at_end_row = NULL;
17551 /* If scrolling up, there may be some lines to display at the end of
17552 the window. */
17553 last_text_row_at_end = NULL;
17554 if (dy < 0)
17556 /* Scrolling up can leave for example a partially visible line
17557 at the end of the window to be redisplayed. */
17558 /* Set last_row to the glyph row in the current matrix where the
17559 window end line is found. It has been moved up or down in
17560 the matrix by dvpos. */
17561 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17562 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17564 /* If last_row is the window end line, it should display text. */
17565 eassert (last_row->displays_text_p);
17567 /* If window end line was partially visible before, begin
17568 displaying at that line. Otherwise begin displaying with the
17569 line following it. */
17570 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17572 init_to_row_start (&it, w, last_row);
17573 it.vpos = last_vpos;
17574 it.current_y = last_row->y;
17576 else
17578 init_to_row_end (&it, w, last_row);
17579 it.vpos = 1 + last_vpos;
17580 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17581 ++last_row;
17584 /* We may start in a continuation line. If so, we have to
17585 get the right continuation_lines_width and current_x. */
17586 it.continuation_lines_width = last_row->continuation_lines_width;
17587 it.hpos = it.current_x = 0;
17589 /* Display the rest of the lines at the window end. */
17590 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17591 while (it.current_y < it.last_visible_y
17592 && !fonts_changed_p)
17594 /* Is it always sure that the display agrees with lines in
17595 the current matrix? I don't think so, so we mark rows
17596 displayed invalid in the current matrix by setting their
17597 enabled_p flag to zero. */
17598 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17599 if (display_line (&it))
17600 last_text_row_at_end = it.glyph_row - 1;
17604 /* Update window_end_pos and window_end_vpos. */
17605 if (first_unchanged_at_end_row
17606 && !last_text_row_at_end)
17608 /* Window end line if one of the preserved rows from the current
17609 matrix. Set row to the last row displaying text in current
17610 matrix starting at first_unchanged_at_end_row, after
17611 scrolling. */
17612 eassert (first_unchanged_at_end_row->displays_text_p);
17613 row = find_last_row_displaying_text (w->current_matrix, &it,
17614 first_unchanged_at_end_row);
17615 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17617 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17618 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17619 wset_window_end_vpos
17620 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17621 eassert (w->window_end_bytepos >= 0);
17622 IF_DEBUG (debug_method_add (w, "A"));
17624 else if (last_text_row_at_end)
17626 wset_window_end_pos
17627 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17628 w->window_end_bytepos
17629 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17630 wset_window_end_vpos
17631 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17632 desired_matrix)));
17633 eassert (w->window_end_bytepos >= 0);
17634 IF_DEBUG (debug_method_add (w, "B"));
17636 else if (last_text_row)
17638 /* We have displayed either to the end of the window or at the
17639 end of the window, i.e. the last row with text is to be found
17640 in the desired matrix. */
17641 wset_window_end_pos
17642 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17643 w->window_end_bytepos
17644 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17645 wset_window_end_vpos
17646 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17647 eassert (w->window_end_bytepos >= 0);
17649 else if (first_unchanged_at_end_row == NULL
17650 && last_text_row == NULL
17651 && last_text_row_at_end == NULL)
17653 /* Displayed to end of window, but no line containing text was
17654 displayed. Lines were deleted at the end of the window. */
17655 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17656 int vpos = XFASTINT (w->window_end_vpos);
17657 struct glyph_row *current_row = current_matrix->rows + vpos;
17658 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17660 for (row = NULL;
17661 row == NULL && vpos >= first_vpos;
17662 --vpos, --current_row, --desired_row)
17664 if (desired_row->enabled_p)
17666 if (desired_row->displays_text_p)
17667 row = desired_row;
17669 else if (current_row->displays_text_p)
17670 row = current_row;
17673 eassert (row != NULL);
17674 wset_window_end_vpos (w, make_number (vpos + 1));
17675 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17676 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17677 eassert (w->window_end_bytepos >= 0);
17678 IF_DEBUG (debug_method_add (w, "C"));
17680 else
17681 emacs_abort ();
17683 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17684 debug_end_vpos = XFASTINT (w->window_end_vpos));
17686 /* Record that display has not been completed. */
17687 w->window_end_valid = 0;
17688 w->desired_matrix->no_scrolling_p = 1;
17689 return 3;
17691 #undef GIVE_UP
17696 /***********************************************************************
17697 More debugging support
17698 ***********************************************************************/
17700 #ifdef GLYPH_DEBUG
17702 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17703 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17704 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17707 /* Dump the contents of glyph matrix MATRIX on stderr.
17709 GLYPHS 0 means don't show glyph contents.
17710 GLYPHS 1 means show glyphs in short form
17711 GLYPHS > 1 means show glyphs in long form. */
17713 void
17714 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17716 int i;
17717 for (i = 0; i < matrix->nrows; ++i)
17718 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17722 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17723 the glyph row and area where the glyph comes from. */
17725 void
17726 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17728 if (glyph->type == CHAR_GLYPH
17729 || glyph->type == GLYPHLESS_GLYPH)
17731 fprintf (stderr,
17732 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17733 glyph - row->glyphs[TEXT_AREA],
17734 (glyph->type == CHAR_GLYPH
17735 ? 'C'
17736 : 'G'),
17737 glyph->charpos,
17738 (BUFFERP (glyph->object)
17739 ? 'B'
17740 : (STRINGP (glyph->object)
17741 ? 'S'
17742 : (INTEGERP (glyph->object)
17743 ? '0'
17744 : '-'))),
17745 glyph->pixel_width,
17746 glyph->u.ch,
17747 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17748 ? glyph->u.ch
17749 : '.'),
17750 glyph->face_id,
17751 glyph->left_box_line_p,
17752 glyph->right_box_line_p);
17754 else if (glyph->type == STRETCH_GLYPH)
17756 fprintf (stderr,
17757 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17758 glyph - row->glyphs[TEXT_AREA],
17759 'S',
17760 glyph->charpos,
17761 (BUFFERP (glyph->object)
17762 ? 'B'
17763 : (STRINGP (glyph->object)
17764 ? 'S'
17765 : (INTEGERP (glyph->object)
17766 ? '0'
17767 : '-'))),
17768 glyph->pixel_width,
17770 ' ',
17771 glyph->face_id,
17772 glyph->left_box_line_p,
17773 glyph->right_box_line_p);
17775 else if (glyph->type == IMAGE_GLYPH)
17777 fprintf (stderr,
17778 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17779 glyph - row->glyphs[TEXT_AREA],
17780 'I',
17781 glyph->charpos,
17782 (BUFFERP (glyph->object)
17783 ? 'B'
17784 : (STRINGP (glyph->object)
17785 ? 'S'
17786 : (INTEGERP (glyph->object)
17787 ? '0'
17788 : '-'))),
17789 glyph->pixel_width,
17790 glyph->u.img_id,
17791 '.',
17792 glyph->face_id,
17793 glyph->left_box_line_p,
17794 glyph->right_box_line_p);
17796 else if (glyph->type == COMPOSITE_GLYPH)
17798 fprintf (stderr,
17799 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17800 glyph - row->glyphs[TEXT_AREA],
17801 '+',
17802 glyph->charpos,
17803 (BUFFERP (glyph->object)
17804 ? 'B'
17805 : (STRINGP (glyph->object)
17806 ? 'S'
17807 : (INTEGERP (glyph->object)
17808 ? '0'
17809 : '-'))),
17810 glyph->pixel_width,
17811 glyph->u.cmp.id);
17812 if (glyph->u.cmp.automatic)
17813 fprintf (stderr,
17814 "[%d-%d]",
17815 glyph->slice.cmp.from, glyph->slice.cmp.to);
17816 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17817 glyph->face_id,
17818 glyph->left_box_line_p,
17819 glyph->right_box_line_p);
17824 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17825 GLYPHS 0 means don't show glyph contents.
17826 GLYPHS 1 means show glyphs in short form
17827 GLYPHS > 1 means show glyphs in long form. */
17829 void
17830 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17832 if (glyphs != 1)
17834 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17835 fprintf (stderr, "==============================================================================\n");
17837 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17838 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17839 vpos,
17840 MATRIX_ROW_START_CHARPOS (row),
17841 MATRIX_ROW_END_CHARPOS (row),
17842 row->used[TEXT_AREA],
17843 row->contains_overlapping_glyphs_p,
17844 row->enabled_p,
17845 row->truncated_on_left_p,
17846 row->truncated_on_right_p,
17847 row->continued_p,
17848 MATRIX_ROW_CONTINUATION_LINE_P (row),
17849 row->displays_text_p,
17850 row->ends_at_zv_p,
17851 row->fill_line_p,
17852 row->ends_in_middle_of_char_p,
17853 row->starts_in_middle_of_char_p,
17854 row->mouse_face_p,
17855 row->x,
17856 row->y,
17857 row->pixel_width,
17858 row->height,
17859 row->visible_height,
17860 row->ascent,
17861 row->phys_ascent);
17862 /* The next 3 lines should align to "Start" in the header. */
17863 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17864 row->end.overlay_string_index,
17865 row->continuation_lines_width);
17866 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17867 CHARPOS (row->start.string_pos),
17868 CHARPOS (row->end.string_pos));
17869 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17870 row->end.dpvec_index);
17873 if (glyphs > 1)
17875 int area;
17877 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17879 struct glyph *glyph = row->glyphs[area];
17880 struct glyph *glyph_end = glyph + row->used[area];
17882 /* Glyph for a line end in text. */
17883 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17884 ++glyph_end;
17886 if (glyph < glyph_end)
17887 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17889 for (; glyph < glyph_end; ++glyph)
17890 dump_glyph (row, glyph, area);
17893 else if (glyphs == 1)
17895 int area;
17897 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17899 char *s = alloca (row->used[area] + 4);
17900 int i;
17902 for (i = 0; i < row->used[area]; ++i)
17904 struct glyph *glyph = row->glyphs[area] + i;
17905 if (i == row->used[area] - 1
17906 && area == TEXT_AREA
17907 && INTEGERP (glyph->object)
17908 && glyph->type == CHAR_GLYPH
17909 && glyph->u.ch == ' ')
17911 strcpy (&s[i], "[\\n]");
17912 i += 4;
17914 else if (glyph->type == CHAR_GLYPH
17915 && glyph->u.ch < 0x80
17916 && glyph->u.ch >= ' ')
17917 s[i] = glyph->u.ch;
17918 else
17919 s[i] = '.';
17922 s[i] = '\0';
17923 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17929 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17930 Sdump_glyph_matrix, 0, 1, "p",
17931 doc: /* Dump the current matrix of the selected window to stderr.
17932 Shows contents of glyph row structures. With non-nil
17933 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17934 glyphs in short form, otherwise show glyphs in long form. */)
17935 (Lisp_Object glyphs)
17937 struct window *w = XWINDOW (selected_window);
17938 struct buffer *buffer = XBUFFER (w->buffer);
17940 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17941 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17942 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17943 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17944 fprintf (stderr, "=============================================\n");
17945 dump_glyph_matrix (w->current_matrix,
17946 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
17947 return Qnil;
17951 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17952 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17953 (void)
17955 struct frame *f = XFRAME (selected_frame);
17956 dump_glyph_matrix (f->current_matrix, 1);
17957 return Qnil;
17961 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17962 doc: /* Dump glyph row ROW to stderr.
17963 GLYPH 0 means don't dump glyphs.
17964 GLYPH 1 means dump glyphs in short form.
17965 GLYPH > 1 or omitted means dump glyphs in long form. */)
17966 (Lisp_Object row, Lisp_Object glyphs)
17968 struct glyph_matrix *matrix;
17969 EMACS_INT vpos;
17971 CHECK_NUMBER (row);
17972 matrix = XWINDOW (selected_window)->current_matrix;
17973 vpos = XINT (row);
17974 if (vpos >= 0 && vpos < matrix->nrows)
17975 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17976 vpos,
17977 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
17978 return Qnil;
17982 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17983 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17984 GLYPH 0 means don't dump glyphs.
17985 GLYPH 1 means dump glyphs in short form.
17986 GLYPH > 1 or omitted means dump glyphs in long form. */)
17987 (Lisp_Object row, Lisp_Object glyphs)
17989 struct frame *sf = SELECTED_FRAME ();
17990 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17991 EMACS_INT vpos;
17993 CHECK_NUMBER (row);
17994 vpos = XINT (row);
17995 if (vpos >= 0 && vpos < m->nrows)
17996 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17997 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
17998 return Qnil;
18002 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18003 doc: /* Toggle tracing of redisplay.
18004 With ARG, turn tracing on if and only if ARG is positive. */)
18005 (Lisp_Object arg)
18007 if (NILP (arg))
18008 trace_redisplay_p = !trace_redisplay_p;
18009 else
18011 arg = Fprefix_numeric_value (arg);
18012 trace_redisplay_p = XINT (arg) > 0;
18015 return Qnil;
18019 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18020 doc: /* Like `format', but print result to stderr.
18021 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18022 (ptrdiff_t nargs, Lisp_Object *args)
18024 Lisp_Object s = Fformat (nargs, args);
18025 fprintf (stderr, "%s", SDATA (s));
18026 return Qnil;
18029 #endif /* GLYPH_DEBUG */
18033 /***********************************************************************
18034 Building Desired Matrix Rows
18035 ***********************************************************************/
18037 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18038 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18040 static struct glyph_row *
18041 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18043 struct frame *f = XFRAME (WINDOW_FRAME (w));
18044 struct buffer *buffer = XBUFFER (w->buffer);
18045 struct buffer *old = current_buffer;
18046 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18047 int arrow_len = SCHARS (overlay_arrow_string);
18048 const unsigned char *arrow_end = arrow_string + arrow_len;
18049 const unsigned char *p;
18050 struct it it;
18051 int multibyte_p;
18052 int n_glyphs_before;
18054 set_buffer_temp (buffer);
18055 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18056 it.glyph_row->used[TEXT_AREA] = 0;
18057 SET_TEXT_POS (it.position, 0, 0);
18059 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18060 p = arrow_string;
18061 while (p < arrow_end)
18063 Lisp_Object face, ilisp;
18065 /* Get the next character. */
18066 if (multibyte_p)
18067 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18068 else
18070 it.c = it.char_to_display = *p, it.len = 1;
18071 if (! ASCII_CHAR_P (it.c))
18072 it.char_to_display = BYTE8_TO_CHAR (it.c);
18074 p += it.len;
18076 /* Get its face. */
18077 ilisp = make_number (p - arrow_string);
18078 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18079 it.face_id = compute_char_face (f, it.char_to_display, face);
18081 /* Compute its width, get its glyphs. */
18082 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18083 SET_TEXT_POS (it.position, -1, -1);
18084 PRODUCE_GLYPHS (&it);
18086 /* If this character doesn't fit any more in the line, we have
18087 to remove some glyphs. */
18088 if (it.current_x > it.last_visible_x)
18090 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18091 break;
18095 set_buffer_temp (old);
18096 return it.glyph_row;
18100 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18101 glyphs to insert is determined by produce_special_glyphs. */
18103 static void
18104 insert_left_trunc_glyphs (struct it *it)
18106 struct it truncate_it;
18107 struct glyph *from, *end, *to, *toend;
18109 eassert (!FRAME_WINDOW_P (it->f)
18110 || (!it->glyph_row->reversed_p
18111 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18112 || (it->glyph_row->reversed_p
18113 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18115 /* Get the truncation glyphs. */
18116 truncate_it = *it;
18117 truncate_it.current_x = 0;
18118 truncate_it.face_id = DEFAULT_FACE_ID;
18119 truncate_it.glyph_row = &scratch_glyph_row;
18120 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18121 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18122 truncate_it.object = make_number (0);
18123 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18125 /* Overwrite glyphs from IT with truncation glyphs. */
18126 if (!it->glyph_row->reversed_p)
18128 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18130 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18131 end = from + tused;
18132 to = it->glyph_row->glyphs[TEXT_AREA];
18133 toend = to + it->glyph_row->used[TEXT_AREA];
18134 if (FRAME_WINDOW_P (it->f))
18136 /* On GUI frames, when variable-size fonts are displayed,
18137 the truncation glyphs may need more pixels than the row's
18138 glyphs they overwrite. We overwrite more glyphs to free
18139 enough screen real estate, and enlarge the stretch glyph
18140 on the right (see display_line), if there is one, to
18141 preserve the screen position of the truncation glyphs on
18142 the right. */
18143 int w = 0;
18144 struct glyph *g = to;
18145 short used;
18147 /* The first glyph could be partially visible, in which case
18148 it->glyph_row->x will be negative. But we want the left
18149 truncation glyphs to be aligned at the left margin of the
18150 window, so we override the x coordinate at which the row
18151 will begin. */
18152 it->glyph_row->x = 0;
18153 while (g < toend && w < it->truncation_pixel_width)
18155 w += g->pixel_width;
18156 ++g;
18158 if (g - to - tused > 0)
18160 memmove (to + tused, g, (toend - g) * sizeof(*g));
18161 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18163 used = it->glyph_row->used[TEXT_AREA];
18164 if (it->glyph_row->truncated_on_right_p
18165 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18166 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18167 == STRETCH_GLYPH)
18169 int extra = w - it->truncation_pixel_width;
18171 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18175 while (from < end)
18176 *to++ = *from++;
18178 /* There may be padding glyphs left over. Overwrite them too. */
18179 if (!FRAME_WINDOW_P (it->f))
18181 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18183 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18184 while (from < end)
18185 *to++ = *from++;
18189 if (to > toend)
18190 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18192 else
18194 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18196 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18197 that back to front. */
18198 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18199 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18200 toend = it->glyph_row->glyphs[TEXT_AREA];
18201 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18202 if (FRAME_WINDOW_P (it->f))
18204 int w = 0;
18205 struct glyph *g = to;
18207 while (g >= toend && w < it->truncation_pixel_width)
18209 w += g->pixel_width;
18210 --g;
18212 if (to - g - tused > 0)
18213 to = g + tused;
18214 if (it->glyph_row->truncated_on_right_p
18215 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18216 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18218 int extra = w - it->truncation_pixel_width;
18220 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18224 while (from >= end && to >= toend)
18225 *to-- = *from--;
18226 if (!FRAME_WINDOW_P (it->f))
18228 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18230 from =
18231 truncate_it.glyph_row->glyphs[TEXT_AREA]
18232 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18233 while (from >= end && to >= toend)
18234 *to-- = *from--;
18237 if (from >= end)
18239 /* Need to free some room before prepending additional
18240 glyphs. */
18241 int move_by = from - end + 1;
18242 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18243 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18245 for ( ; g >= g0; g--)
18246 g[move_by] = *g;
18247 while (from >= end)
18248 *to-- = *from--;
18249 it->glyph_row->used[TEXT_AREA] += move_by;
18254 /* Compute the hash code for ROW. */
18255 unsigned
18256 row_hash (struct glyph_row *row)
18258 int area, k;
18259 unsigned hashval = 0;
18261 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18262 for (k = 0; k < row->used[area]; ++k)
18263 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18264 + row->glyphs[area][k].u.val
18265 + row->glyphs[area][k].face_id
18266 + row->glyphs[area][k].padding_p
18267 + (row->glyphs[area][k].type << 2));
18269 return hashval;
18272 /* Compute the pixel height and width of IT->glyph_row.
18274 Most of the time, ascent and height of a display line will be equal
18275 to the max_ascent and max_height values of the display iterator
18276 structure. This is not the case if
18278 1. We hit ZV without displaying anything. In this case, max_ascent
18279 and max_height will be zero.
18281 2. We have some glyphs that don't contribute to the line height.
18282 (The glyph row flag contributes_to_line_height_p is for future
18283 pixmap extensions).
18285 The first case is easily covered by using default values because in
18286 these cases, the line height does not really matter, except that it
18287 must not be zero. */
18289 static void
18290 compute_line_metrics (struct it *it)
18292 struct glyph_row *row = it->glyph_row;
18294 if (FRAME_WINDOW_P (it->f))
18296 int i, min_y, max_y;
18298 /* The line may consist of one space only, that was added to
18299 place the cursor on it. If so, the row's height hasn't been
18300 computed yet. */
18301 if (row->height == 0)
18303 if (it->max_ascent + it->max_descent == 0)
18304 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18305 row->ascent = it->max_ascent;
18306 row->height = it->max_ascent + it->max_descent;
18307 row->phys_ascent = it->max_phys_ascent;
18308 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18309 row->extra_line_spacing = it->max_extra_line_spacing;
18312 /* Compute the width of this line. */
18313 row->pixel_width = row->x;
18314 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18315 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18317 eassert (row->pixel_width >= 0);
18318 eassert (row->ascent >= 0 && row->height > 0);
18320 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18321 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18323 /* If first line's physical ascent is larger than its logical
18324 ascent, use the physical ascent, and make the row taller.
18325 This makes accented characters fully visible. */
18326 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18327 && row->phys_ascent > row->ascent)
18329 row->height += row->phys_ascent - row->ascent;
18330 row->ascent = row->phys_ascent;
18333 /* Compute how much of the line is visible. */
18334 row->visible_height = row->height;
18336 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18337 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18339 if (row->y < min_y)
18340 row->visible_height -= min_y - row->y;
18341 if (row->y + row->height > max_y)
18342 row->visible_height -= row->y + row->height - max_y;
18344 else
18346 row->pixel_width = row->used[TEXT_AREA];
18347 if (row->continued_p)
18348 row->pixel_width -= it->continuation_pixel_width;
18349 else if (row->truncated_on_right_p)
18350 row->pixel_width -= it->truncation_pixel_width;
18351 row->ascent = row->phys_ascent = 0;
18352 row->height = row->phys_height = row->visible_height = 1;
18353 row->extra_line_spacing = 0;
18356 /* Compute a hash code for this row. */
18357 row->hash = row_hash (row);
18359 it->max_ascent = it->max_descent = 0;
18360 it->max_phys_ascent = it->max_phys_descent = 0;
18364 /* Append one space to the glyph row of iterator IT if doing a
18365 window-based redisplay. The space has the same face as
18366 IT->face_id. Value is non-zero if a space was added.
18368 This function is called to make sure that there is always one glyph
18369 at the end of a glyph row that the cursor can be set on under
18370 window-systems. (If there weren't such a glyph we would not know
18371 how wide and tall a box cursor should be displayed).
18373 At the same time this space let's a nicely handle clearing to the
18374 end of the line if the row ends in italic text. */
18376 static int
18377 append_space_for_newline (struct it *it, int default_face_p)
18379 if (FRAME_WINDOW_P (it->f))
18381 int n = it->glyph_row->used[TEXT_AREA];
18383 if (it->glyph_row->glyphs[TEXT_AREA] + n
18384 < it->glyph_row->glyphs[1 + TEXT_AREA])
18386 /* Save some values that must not be changed.
18387 Must save IT->c and IT->len because otherwise
18388 ITERATOR_AT_END_P wouldn't work anymore after
18389 append_space_for_newline has been called. */
18390 enum display_element_type saved_what = it->what;
18391 int saved_c = it->c, saved_len = it->len;
18392 int saved_char_to_display = it->char_to_display;
18393 int saved_x = it->current_x;
18394 int saved_face_id = it->face_id;
18395 int saved_box_end = it->end_of_box_run_p;
18396 struct text_pos saved_pos;
18397 Lisp_Object saved_object;
18398 struct face *face;
18400 saved_object = it->object;
18401 saved_pos = it->position;
18403 it->what = IT_CHARACTER;
18404 memset (&it->position, 0, sizeof it->position);
18405 it->object = make_number (0);
18406 it->c = it->char_to_display = ' ';
18407 it->len = 1;
18409 /* If the default face was remapped, be sure to use the
18410 remapped face for the appended newline. */
18411 if (default_face_p)
18412 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18413 else if (it->face_before_selective_p)
18414 it->face_id = it->saved_face_id;
18415 face = FACE_FROM_ID (it->f, it->face_id);
18416 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18417 /* In R2L rows, we will prepend a stretch glyph that will
18418 have the end_of_box_run_p flag set for it, so there's no
18419 need for the appended newline glyph to have that flag
18420 set. */
18421 if (it->glyph_row->reversed_p
18422 /* But if the appended newline glyph goes all the way to
18423 the end of the row, there will be no stretch glyph,
18424 so leave the box flag set. */
18425 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18426 it->end_of_box_run_p = 0;
18428 PRODUCE_GLYPHS (it);
18430 it->override_ascent = -1;
18431 it->constrain_row_ascent_descent_p = 0;
18432 it->current_x = saved_x;
18433 it->object = saved_object;
18434 it->position = saved_pos;
18435 it->what = saved_what;
18436 it->face_id = saved_face_id;
18437 it->len = saved_len;
18438 it->c = saved_c;
18439 it->char_to_display = saved_char_to_display;
18440 it->end_of_box_run_p = saved_box_end;
18441 return 1;
18445 return 0;
18449 /* Extend the face of the last glyph in the text area of IT->glyph_row
18450 to the end of the display line. Called from display_line. If the
18451 glyph row is empty, add a space glyph to it so that we know the
18452 face to draw. Set the glyph row flag fill_line_p. If the glyph
18453 row is R2L, prepend a stretch glyph to cover the empty space to the
18454 left of the leftmost glyph. */
18456 static void
18457 extend_face_to_end_of_line (struct it *it)
18459 struct face *face, *default_face;
18460 struct frame *f = it->f;
18462 /* If line is already filled, do nothing. Non window-system frames
18463 get a grace of one more ``pixel'' because their characters are
18464 1-``pixel'' wide, so they hit the equality too early. This grace
18465 is needed only for R2L rows that are not continued, to produce
18466 one extra blank where we could display the cursor. */
18467 if (it->current_x >= it->last_visible_x
18468 + (!FRAME_WINDOW_P (f)
18469 && it->glyph_row->reversed_p
18470 && !it->glyph_row->continued_p))
18471 return;
18473 /* The default face, possibly remapped. */
18474 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18476 /* Face extension extends the background and box of IT->face_id
18477 to the end of the line. If the background equals the background
18478 of the frame, we don't have to do anything. */
18479 if (it->face_before_selective_p)
18480 face = FACE_FROM_ID (f, it->saved_face_id);
18481 else
18482 face = FACE_FROM_ID (f, it->face_id);
18484 if (FRAME_WINDOW_P (f)
18485 && it->glyph_row->displays_text_p
18486 && face->box == FACE_NO_BOX
18487 && face->background == FRAME_BACKGROUND_PIXEL (f)
18488 && !face->stipple
18489 && !it->glyph_row->reversed_p)
18490 return;
18492 /* Set the glyph row flag indicating that the face of the last glyph
18493 in the text area has to be drawn to the end of the text area. */
18494 it->glyph_row->fill_line_p = 1;
18496 /* If current character of IT is not ASCII, make sure we have the
18497 ASCII face. This will be automatically undone the next time
18498 get_next_display_element returns a multibyte character. Note
18499 that the character will always be single byte in unibyte
18500 text. */
18501 if (!ASCII_CHAR_P (it->c))
18503 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18506 if (FRAME_WINDOW_P (f))
18508 /* If the row is empty, add a space with the current face of IT,
18509 so that we know which face to draw. */
18510 if (it->glyph_row->used[TEXT_AREA] == 0)
18512 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18513 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18514 it->glyph_row->used[TEXT_AREA] = 1;
18516 #ifdef HAVE_WINDOW_SYSTEM
18517 if (it->glyph_row->reversed_p)
18519 /* Prepend a stretch glyph to the row, such that the
18520 rightmost glyph will be drawn flushed all the way to the
18521 right margin of the window. The stretch glyph that will
18522 occupy the empty space, if any, to the left of the
18523 glyphs. */
18524 struct font *font = face->font ? face->font : FRAME_FONT (f);
18525 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18526 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18527 struct glyph *g;
18528 int row_width, stretch_ascent, stretch_width;
18529 struct text_pos saved_pos;
18530 int saved_face_id, saved_avoid_cursor, saved_box_start;
18532 for (row_width = 0, g = row_start; g < row_end; g++)
18533 row_width += g->pixel_width;
18534 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18535 if (stretch_width > 0)
18537 stretch_ascent =
18538 (((it->ascent + it->descent)
18539 * FONT_BASE (font)) / FONT_HEIGHT (font));
18540 saved_pos = it->position;
18541 memset (&it->position, 0, sizeof it->position);
18542 saved_avoid_cursor = it->avoid_cursor_p;
18543 it->avoid_cursor_p = 1;
18544 saved_face_id = it->face_id;
18545 saved_box_start = it->start_of_box_run_p;
18546 /* The last row's stretch glyph should get the default
18547 face, to avoid painting the rest of the window with
18548 the region face, if the region ends at ZV. */
18549 if (it->glyph_row->ends_at_zv_p)
18550 it->face_id = default_face->id;
18551 else
18552 it->face_id = face->id;
18553 it->start_of_box_run_p = 0;
18554 append_stretch_glyph (it, make_number (0), stretch_width,
18555 it->ascent + it->descent, stretch_ascent);
18556 it->position = saved_pos;
18557 it->avoid_cursor_p = saved_avoid_cursor;
18558 it->face_id = saved_face_id;
18559 it->start_of_box_run_p = saved_box_start;
18562 #endif /* HAVE_WINDOW_SYSTEM */
18564 else
18566 /* Save some values that must not be changed. */
18567 int saved_x = it->current_x;
18568 struct text_pos saved_pos;
18569 Lisp_Object saved_object;
18570 enum display_element_type saved_what = it->what;
18571 int saved_face_id = it->face_id;
18573 saved_object = it->object;
18574 saved_pos = it->position;
18576 it->what = IT_CHARACTER;
18577 memset (&it->position, 0, sizeof it->position);
18578 it->object = make_number (0);
18579 it->c = it->char_to_display = ' ';
18580 it->len = 1;
18581 /* The last row's blank glyphs should get the default face, to
18582 avoid painting the rest of the window with the region face,
18583 if the region ends at ZV. */
18584 if (it->glyph_row->ends_at_zv_p)
18585 it->face_id = default_face->id;
18586 else
18587 it->face_id = face->id;
18589 PRODUCE_GLYPHS (it);
18591 while (it->current_x <= it->last_visible_x)
18592 PRODUCE_GLYPHS (it);
18594 /* Don't count these blanks really. It would let us insert a left
18595 truncation glyph below and make us set the cursor on them, maybe. */
18596 it->current_x = saved_x;
18597 it->object = saved_object;
18598 it->position = saved_pos;
18599 it->what = saved_what;
18600 it->face_id = saved_face_id;
18605 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18606 trailing whitespace. */
18608 static int
18609 trailing_whitespace_p (ptrdiff_t charpos)
18611 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18612 int c = 0;
18614 while (bytepos < ZV_BYTE
18615 && (c = FETCH_CHAR (bytepos),
18616 c == ' ' || c == '\t'))
18617 ++bytepos;
18619 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18621 if (bytepos != PT_BYTE)
18622 return 1;
18624 return 0;
18628 /* Highlight trailing whitespace, if any, in ROW. */
18630 static void
18631 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18633 int used = row->used[TEXT_AREA];
18635 if (used)
18637 struct glyph *start = row->glyphs[TEXT_AREA];
18638 struct glyph *glyph = start + used - 1;
18640 if (row->reversed_p)
18642 /* Right-to-left rows need to be processed in the opposite
18643 direction, so swap the edge pointers. */
18644 glyph = start;
18645 start = row->glyphs[TEXT_AREA] + used - 1;
18648 /* Skip over glyphs inserted to display the cursor at the
18649 end of a line, for extending the face of the last glyph
18650 to the end of the line on terminals, and for truncation
18651 and continuation glyphs. */
18652 if (!row->reversed_p)
18654 while (glyph >= start
18655 && glyph->type == CHAR_GLYPH
18656 && INTEGERP (glyph->object))
18657 --glyph;
18659 else
18661 while (glyph <= start
18662 && glyph->type == CHAR_GLYPH
18663 && INTEGERP (glyph->object))
18664 ++glyph;
18667 /* If last glyph is a space or stretch, and it's trailing
18668 whitespace, set the face of all trailing whitespace glyphs in
18669 IT->glyph_row to `trailing-whitespace'. */
18670 if ((row->reversed_p ? glyph <= start : glyph >= start)
18671 && BUFFERP (glyph->object)
18672 && (glyph->type == STRETCH_GLYPH
18673 || (glyph->type == CHAR_GLYPH
18674 && glyph->u.ch == ' '))
18675 && trailing_whitespace_p (glyph->charpos))
18677 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18678 if (face_id < 0)
18679 return;
18681 if (!row->reversed_p)
18683 while (glyph >= start
18684 && BUFFERP (glyph->object)
18685 && (glyph->type == STRETCH_GLYPH
18686 || (glyph->type == CHAR_GLYPH
18687 && glyph->u.ch == ' ')))
18688 (glyph--)->face_id = face_id;
18690 else
18692 while (glyph <= start
18693 && BUFFERP (glyph->object)
18694 && (glyph->type == STRETCH_GLYPH
18695 || (glyph->type == CHAR_GLYPH
18696 && glyph->u.ch == ' ')))
18697 (glyph++)->face_id = face_id;
18704 /* Value is non-zero if glyph row ROW should be
18705 used to hold the cursor. */
18707 static int
18708 cursor_row_p (struct glyph_row *row)
18710 int result = 1;
18712 if (PT == CHARPOS (row->end.pos)
18713 || PT == MATRIX_ROW_END_CHARPOS (row))
18715 /* Suppose the row ends on a string.
18716 Unless the row is continued, that means it ends on a newline
18717 in the string. If it's anything other than a display string
18718 (e.g., a before-string from an overlay), we don't want the
18719 cursor there. (This heuristic seems to give the optimal
18720 behavior for the various types of multi-line strings.)
18721 One exception: if the string has `cursor' property on one of
18722 its characters, we _do_ want the cursor there. */
18723 if (CHARPOS (row->end.string_pos) >= 0)
18725 if (row->continued_p)
18726 result = 1;
18727 else
18729 /* Check for `display' property. */
18730 struct glyph *beg = row->glyphs[TEXT_AREA];
18731 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18732 struct glyph *glyph;
18734 result = 0;
18735 for (glyph = end; glyph >= beg; --glyph)
18736 if (STRINGP (glyph->object))
18738 Lisp_Object prop
18739 = Fget_char_property (make_number (PT),
18740 Qdisplay, Qnil);
18741 result =
18742 (!NILP (prop)
18743 && display_prop_string_p (prop, glyph->object));
18744 /* If there's a `cursor' property on one of the
18745 string's characters, this row is a cursor row,
18746 even though this is not a display string. */
18747 if (!result)
18749 Lisp_Object s = glyph->object;
18751 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18753 ptrdiff_t gpos = glyph->charpos;
18755 if (!NILP (Fget_char_property (make_number (gpos),
18756 Qcursor, s)))
18758 result = 1;
18759 break;
18763 break;
18767 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18769 /* If the row ends in middle of a real character,
18770 and the line is continued, we want the cursor here.
18771 That's because CHARPOS (ROW->end.pos) would equal
18772 PT if PT is before the character. */
18773 if (!row->ends_in_ellipsis_p)
18774 result = row->continued_p;
18775 else
18776 /* If the row ends in an ellipsis, then
18777 CHARPOS (ROW->end.pos) will equal point after the
18778 invisible text. We want that position to be displayed
18779 after the ellipsis. */
18780 result = 0;
18782 /* If the row ends at ZV, display the cursor at the end of that
18783 row instead of at the start of the row below. */
18784 else if (row->ends_at_zv_p)
18785 result = 1;
18786 else
18787 result = 0;
18790 return result;
18795 /* Push the property PROP so that it will be rendered at the current
18796 position in IT. Return 1 if PROP was successfully pushed, 0
18797 otherwise. Called from handle_line_prefix to handle the
18798 `line-prefix' and `wrap-prefix' properties. */
18800 static int
18801 push_prefix_prop (struct it *it, Lisp_Object prop)
18803 struct text_pos pos =
18804 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18806 eassert (it->method == GET_FROM_BUFFER
18807 || it->method == GET_FROM_DISPLAY_VECTOR
18808 || it->method == GET_FROM_STRING);
18810 /* We need to save the current buffer/string position, so it will be
18811 restored by pop_it, because iterate_out_of_display_property
18812 depends on that being set correctly, but some situations leave
18813 it->position not yet set when this function is called. */
18814 push_it (it, &pos);
18816 if (STRINGP (prop))
18818 if (SCHARS (prop) == 0)
18820 pop_it (it);
18821 return 0;
18824 it->string = prop;
18825 it->string_from_prefix_prop_p = 1;
18826 it->multibyte_p = STRING_MULTIBYTE (it->string);
18827 it->current.overlay_string_index = -1;
18828 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18829 it->end_charpos = it->string_nchars = SCHARS (it->string);
18830 it->method = GET_FROM_STRING;
18831 it->stop_charpos = 0;
18832 it->prev_stop = 0;
18833 it->base_level_stop = 0;
18835 /* Force paragraph direction to be that of the parent
18836 buffer/string. */
18837 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18838 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18839 else
18840 it->paragraph_embedding = L2R;
18842 /* Set up the bidi iterator for this display string. */
18843 if (it->bidi_p)
18845 it->bidi_it.string.lstring = it->string;
18846 it->bidi_it.string.s = NULL;
18847 it->bidi_it.string.schars = it->end_charpos;
18848 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18849 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18850 it->bidi_it.string.unibyte = !it->multibyte_p;
18851 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18854 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18856 it->method = GET_FROM_STRETCH;
18857 it->object = prop;
18859 #ifdef HAVE_WINDOW_SYSTEM
18860 else if (IMAGEP (prop))
18862 it->what = IT_IMAGE;
18863 it->image_id = lookup_image (it->f, prop);
18864 it->method = GET_FROM_IMAGE;
18866 #endif /* HAVE_WINDOW_SYSTEM */
18867 else
18869 pop_it (it); /* bogus display property, give up */
18870 return 0;
18873 return 1;
18876 /* Return the character-property PROP at the current position in IT. */
18878 static Lisp_Object
18879 get_it_property (struct it *it, Lisp_Object prop)
18881 Lisp_Object position;
18883 if (STRINGP (it->object))
18884 position = make_number (IT_STRING_CHARPOS (*it));
18885 else if (BUFFERP (it->object))
18886 position = make_number (IT_CHARPOS (*it));
18887 else
18888 return Qnil;
18890 return Fget_char_property (position, prop, it->object);
18893 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18895 static void
18896 handle_line_prefix (struct it *it)
18898 Lisp_Object prefix;
18900 if (it->continuation_lines_width > 0)
18902 prefix = get_it_property (it, Qwrap_prefix);
18903 if (NILP (prefix))
18904 prefix = Vwrap_prefix;
18906 else
18908 prefix = get_it_property (it, Qline_prefix);
18909 if (NILP (prefix))
18910 prefix = Vline_prefix;
18912 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18914 /* If the prefix is wider than the window, and we try to wrap
18915 it, it would acquire its own wrap prefix, and so on till the
18916 iterator stack overflows. So, don't wrap the prefix. */
18917 it->line_wrap = TRUNCATE;
18918 it->avoid_cursor_p = 1;
18924 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18925 only for R2L lines from display_line and display_string, when they
18926 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18927 the line/string needs to be continued on the next glyph row. */
18928 static void
18929 unproduce_glyphs (struct it *it, int n)
18931 struct glyph *glyph, *end;
18933 eassert (it->glyph_row);
18934 eassert (it->glyph_row->reversed_p);
18935 eassert (it->area == TEXT_AREA);
18936 eassert (n <= it->glyph_row->used[TEXT_AREA]);
18938 if (n > it->glyph_row->used[TEXT_AREA])
18939 n = it->glyph_row->used[TEXT_AREA];
18940 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18941 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18942 for ( ; glyph < end; glyph++)
18943 glyph[-n] = *glyph;
18946 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18947 and ROW->maxpos. */
18948 static void
18949 find_row_edges (struct it *it, struct glyph_row *row,
18950 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18951 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18953 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18954 lines' rows is implemented for bidi-reordered rows. */
18956 /* ROW->minpos is the value of min_pos, the minimal buffer position
18957 we have in ROW, or ROW->start.pos if that is smaller. */
18958 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18959 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18960 else
18961 /* We didn't find buffer positions smaller than ROW->start, or
18962 didn't find _any_ valid buffer positions in any of the glyphs,
18963 so we must trust the iterator's computed positions. */
18964 row->minpos = row->start.pos;
18965 if (max_pos <= 0)
18967 max_pos = CHARPOS (it->current.pos);
18968 max_bpos = BYTEPOS (it->current.pos);
18971 /* Here are the various use-cases for ending the row, and the
18972 corresponding values for ROW->maxpos:
18974 Line ends in a newline from buffer eol_pos + 1
18975 Line is continued from buffer max_pos + 1
18976 Line is truncated on right it->current.pos
18977 Line ends in a newline from string max_pos + 1(*)
18978 (*) + 1 only when line ends in a forward scan
18979 Line is continued from string max_pos
18980 Line is continued from display vector max_pos
18981 Line is entirely from a string min_pos == max_pos
18982 Line is entirely from a display vector min_pos == max_pos
18983 Line that ends at ZV ZV
18985 If you discover other use-cases, please add them here as
18986 appropriate. */
18987 if (row->ends_at_zv_p)
18988 row->maxpos = it->current.pos;
18989 else if (row->used[TEXT_AREA])
18991 int seen_this_string = 0;
18992 struct glyph_row *r1 = row - 1;
18994 /* Did we see the same display string on the previous row? */
18995 if (STRINGP (it->object)
18996 /* this is not the first row */
18997 && row > it->w->desired_matrix->rows
18998 /* previous row is not the header line */
18999 && !r1->mode_line_p
19000 /* previous row also ends in a newline from a string */
19001 && r1->ends_in_newline_from_string_p)
19003 struct glyph *start, *end;
19005 /* Search for the last glyph of the previous row that came
19006 from buffer or string. Depending on whether the row is
19007 L2R or R2L, we need to process it front to back or the
19008 other way round. */
19009 if (!r1->reversed_p)
19011 start = r1->glyphs[TEXT_AREA];
19012 end = start + r1->used[TEXT_AREA];
19013 /* Glyphs inserted by redisplay have an integer (zero)
19014 as their object. */
19015 while (end > start
19016 && INTEGERP ((end - 1)->object)
19017 && (end - 1)->charpos <= 0)
19018 --end;
19019 if (end > start)
19021 if (EQ ((end - 1)->object, it->object))
19022 seen_this_string = 1;
19024 else
19025 /* If all the glyphs of the previous row were inserted
19026 by redisplay, it means the previous row was
19027 produced from a single newline, which is only
19028 possible if that newline came from the same string
19029 as the one which produced this ROW. */
19030 seen_this_string = 1;
19032 else
19034 end = r1->glyphs[TEXT_AREA] - 1;
19035 start = end + r1->used[TEXT_AREA];
19036 while (end < start
19037 && INTEGERP ((end + 1)->object)
19038 && (end + 1)->charpos <= 0)
19039 ++end;
19040 if (end < start)
19042 if (EQ ((end + 1)->object, it->object))
19043 seen_this_string = 1;
19045 else
19046 seen_this_string = 1;
19049 /* Take note of each display string that covers a newline only
19050 once, the first time we see it. This is for when a display
19051 string includes more than one newline in it. */
19052 if (row->ends_in_newline_from_string_p && !seen_this_string)
19054 /* If we were scanning the buffer forward when we displayed
19055 the string, we want to account for at least one buffer
19056 position that belongs to this row (position covered by
19057 the display string), so that cursor positioning will
19058 consider this row as a candidate when point is at the end
19059 of the visual line represented by this row. This is not
19060 required when scanning back, because max_pos will already
19061 have a much larger value. */
19062 if (CHARPOS (row->end.pos) > max_pos)
19063 INC_BOTH (max_pos, max_bpos);
19064 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19066 else if (CHARPOS (it->eol_pos) > 0)
19067 SET_TEXT_POS (row->maxpos,
19068 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19069 else if (row->continued_p)
19071 /* If max_pos is different from IT's current position, it
19072 means IT->method does not belong to the display element
19073 at max_pos. However, it also means that the display
19074 element at max_pos was displayed in its entirety on this
19075 line, which is equivalent to saying that the next line
19076 starts at the next buffer position. */
19077 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19078 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19079 else
19081 INC_BOTH (max_pos, max_bpos);
19082 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19085 else if (row->truncated_on_right_p)
19086 /* display_line already called reseat_at_next_visible_line_start,
19087 which puts the iterator at the beginning of the next line, in
19088 the logical order. */
19089 row->maxpos = it->current.pos;
19090 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19091 /* A line that is entirely from a string/image/stretch... */
19092 row->maxpos = row->minpos;
19093 else
19094 emacs_abort ();
19096 else
19097 row->maxpos = it->current.pos;
19100 /* Construct the glyph row IT->glyph_row in the desired matrix of
19101 IT->w from text at the current position of IT. See dispextern.h
19102 for an overview of struct it. Value is non-zero if
19103 IT->glyph_row displays text, as opposed to a line displaying ZV
19104 only. */
19106 static int
19107 display_line (struct it *it)
19109 struct glyph_row *row = it->glyph_row;
19110 Lisp_Object overlay_arrow_string;
19111 struct it wrap_it;
19112 void *wrap_data = NULL;
19113 int may_wrap = 0, wrap_x IF_LINT (= 0);
19114 int wrap_row_used = -1;
19115 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19116 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19117 int wrap_row_extra_line_spacing IF_LINT (= 0);
19118 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19119 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19120 int cvpos;
19121 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19122 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19124 /* We always start displaying at hpos zero even if hscrolled. */
19125 eassert (it->hpos == 0 && it->current_x == 0);
19127 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19128 >= it->w->desired_matrix->nrows)
19130 it->w->nrows_scale_factor++;
19131 fonts_changed_p = 1;
19132 return 0;
19135 /* Is IT->w showing the region? */
19136 it->w->region_showing = it->region_beg_charpos > 0 ? -1 : 0;
19138 /* Clear the result glyph row and enable it. */
19139 prepare_desired_row (row);
19141 row->y = it->current_y;
19142 row->start = it->start;
19143 row->continuation_lines_width = it->continuation_lines_width;
19144 row->displays_text_p = 1;
19145 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19146 it->starts_in_middle_of_char_p = 0;
19148 /* Arrange the overlays nicely for our purposes. Usually, we call
19149 display_line on only one line at a time, in which case this
19150 can't really hurt too much, or we call it on lines which appear
19151 one after another in the buffer, in which case all calls to
19152 recenter_overlay_lists but the first will be pretty cheap. */
19153 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19155 /* Move over display elements that are not visible because we are
19156 hscrolled. This may stop at an x-position < IT->first_visible_x
19157 if the first glyph is partially visible or if we hit a line end. */
19158 if (it->current_x < it->first_visible_x)
19160 enum move_it_result move_result;
19162 this_line_min_pos = row->start.pos;
19163 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19164 MOVE_TO_POS | MOVE_TO_X);
19165 /* If we are under a large hscroll, move_it_in_display_line_to
19166 could hit the end of the line without reaching
19167 it->first_visible_x. Pretend that we did reach it. This is
19168 especially important on a TTY, where we will call
19169 extend_face_to_end_of_line, which needs to know how many
19170 blank glyphs to produce. */
19171 if (it->current_x < it->first_visible_x
19172 && (move_result == MOVE_NEWLINE_OR_CR
19173 || move_result == MOVE_POS_MATCH_OR_ZV))
19174 it->current_x = it->first_visible_x;
19176 /* Record the smallest positions seen while we moved over
19177 display elements that are not visible. This is needed by
19178 redisplay_internal for optimizing the case where the cursor
19179 stays inside the same line. The rest of this function only
19180 considers positions that are actually displayed, so
19181 RECORD_MAX_MIN_POS will not otherwise record positions that
19182 are hscrolled to the left of the left edge of the window. */
19183 min_pos = CHARPOS (this_line_min_pos);
19184 min_bpos = BYTEPOS (this_line_min_pos);
19186 else
19188 /* We only do this when not calling `move_it_in_display_line_to'
19189 above, because move_it_in_display_line_to calls
19190 handle_line_prefix itself. */
19191 handle_line_prefix (it);
19194 /* Get the initial row height. This is either the height of the
19195 text hscrolled, if there is any, or zero. */
19196 row->ascent = it->max_ascent;
19197 row->height = it->max_ascent + it->max_descent;
19198 row->phys_ascent = it->max_phys_ascent;
19199 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19200 row->extra_line_spacing = it->max_extra_line_spacing;
19202 /* Utility macro to record max and min buffer positions seen until now. */
19203 #define RECORD_MAX_MIN_POS(IT) \
19204 do \
19206 int composition_p = !STRINGP ((IT)->string) \
19207 && ((IT)->what == IT_COMPOSITION); \
19208 ptrdiff_t current_pos = \
19209 composition_p ? (IT)->cmp_it.charpos \
19210 : IT_CHARPOS (*(IT)); \
19211 ptrdiff_t current_bpos = \
19212 composition_p ? CHAR_TO_BYTE (current_pos) \
19213 : IT_BYTEPOS (*(IT)); \
19214 if (current_pos < min_pos) \
19216 min_pos = current_pos; \
19217 min_bpos = current_bpos; \
19219 if (IT_CHARPOS (*it) > max_pos) \
19221 max_pos = IT_CHARPOS (*it); \
19222 max_bpos = IT_BYTEPOS (*it); \
19225 while (0)
19227 /* Loop generating characters. The loop is left with IT on the next
19228 character to display. */
19229 while (1)
19231 int n_glyphs_before, hpos_before, x_before;
19232 int x, nglyphs;
19233 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19235 /* Retrieve the next thing to display. Value is zero if end of
19236 buffer reached. */
19237 if (!get_next_display_element (it))
19239 /* Maybe add a space at the end of this line that is used to
19240 display the cursor there under X. Set the charpos of the
19241 first glyph of blank lines not corresponding to any text
19242 to -1. */
19243 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19244 row->exact_window_width_line_p = 1;
19245 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19246 || row->used[TEXT_AREA] == 0)
19248 row->glyphs[TEXT_AREA]->charpos = -1;
19249 row->displays_text_p = 0;
19251 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19252 && (!MINI_WINDOW_P (it->w)
19253 || (minibuf_level && EQ (it->window, minibuf_window))))
19254 row->indicate_empty_line_p = 1;
19257 it->continuation_lines_width = 0;
19258 row->ends_at_zv_p = 1;
19259 /* A row that displays right-to-left text must always have
19260 its last face extended all the way to the end of line,
19261 even if this row ends in ZV, because we still write to
19262 the screen left to right. We also need to extend the
19263 last face if the default face is remapped to some
19264 different face, otherwise the functions that clear
19265 portions of the screen will clear with the default face's
19266 background color. */
19267 if (row->reversed_p
19268 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19269 extend_face_to_end_of_line (it);
19270 break;
19273 /* Now, get the metrics of what we want to display. This also
19274 generates glyphs in `row' (which is IT->glyph_row). */
19275 n_glyphs_before = row->used[TEXT_AREA];
19276 x = it->current_x;
19278 /* Remember the line height so far in case the next element doesn't
19279 fit on the line. */
19280 if (it->line_wrap != TRUNCATE)
19282 ascent = it->max_ascent;
19283 descent = it->max_descent;
19284 phys_ascent = it->max_phys_ascent;
19285 phys_descent = it->max_phys_descent;
19287 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19289 if (IT_DISPLAYING_WHITESPACE (it))
19290 may_wrap = 1;
19291 else if (may_wrap)
19293 SAVE_IT (wrap_it, *it, wrap_data);
19294 wrap_x = x;
19295 wrap_row_used = row->used[TEXT_AREA];
19296 wrap_row_ascent = row->ascent;
19297 wrap_row_height = row->height;
19298 wrap_row_phys_ascent = row->phys_ascent;
19299 wrap_row_phys_height = row->phys_height;
19300 wrap_row_extra_line_spacing = row->extra_line_spacing;
19301 wrap_row_min_pos = min_pos;
19302 wrap_row_min_bpos = min_bpos;
19303 wrap_row_max_pos = max_pos;
19304 wrap_row_max_bpos = max_bpos;
19305 may_wrap = 0;
19310 PRODUCE_GLYPHS (it);
19312 /* If this display element was in marginal areas, continue with
19313 the next one. */
19314 if (it->area != TEXT_AREA)
19316 row->ascent = max (row->ascent, it->max_ascent);
19317 row->height = max (row->height, it->max_ascent + it->max_descent);
19318 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19319 row->phys_height = max (row->phys_height,
19320 it->max_phys_ascent + it->max_phys_descent);
19321 row->extra_line_spacing = max (row->extra_line_spacing,
19322 it->max_extra_line_spacing);
19323 set_iterator_to_next (it, 1);
19324 continue;
19327 /* Does the display element fit on the line? If we truncate
19328 lines, we should draw past the right edge of the window. If
19329 we don't truncate, we want to stop so that we can display the
19330 continuation glyph before the right margin. If lines are
19331 continued, there are two possible strategies for characters
19332 resulting in more than 1 glyph (e.g. tabs): Display as many
19333 glyphs as possible in this line and leave the rest for the
19334 continuation line, or display the whole element in the next
19335 line. Original redisplay did the former, so we do it also. */
19336 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19337 hpos_before = it->hpos;
19338 x_before = x;
19340 if (/* Not a newline. */
19341 nglyphs > 0
19342 /* Glyphs produced fit entirely in the line. */
19343 && it->current_x < it->last_visible_x)
19345 it->hpos += nglyphs;
19346 row->ascent = max (row->ascent, it->max_ascent);
19347 row->height = max (row->height, it->max_ascent + it->max_descent);
19348 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19349 row->phys_height = max (row->phys_height,
19350 it->max_phys_ascent + it->max_phys_descent);
19351 row->extra_line_spacing = max (row->extra_line_spacing,
19352 it->max_extra_line_spacing);
19353 if (it->current_x - it->pixel_width < it->first_visible_x)
19354 row->x = x - it->first_visible_x;
19355 /* Record the maximum and minimum buffer positions seen so
19356 far in glyphs that will be displayed by this row. */
19357 if (it->bidi_p)
19358 RECORD_MAX_MIN_POS (it);
19360 else
19362 int i, new_x;
19363 struct glyph *glyph;
19365 for (i = 0; i < nglyphs; ++i, x = new_x)
19367 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19368 new_x = x + glyph->pixel_width;
19370 if (/* Lines are continued. */
19371 it->line_wrap != TRUNCATE
19372 && (/* Glyph doesn't fit on the line. */
19373 new_x > it->last_visible_x
19374 /* Or it fits exactly on a window system frame. */
19375 || (new_x == it->last_visible_x
19376 && FRAME_WINDOW_P (it->f)
19377 && (row->reversed_p
19378 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19379 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19381 /* End of a continued line. */
19383 if (it->hpos == 0
19384 || (new_x == it->last_visible_x
19385 && FRAME_WINDOW_P (it->f)
19386 && (row->reversed_p
19387 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19388 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19390 /* Current glyph is the only one on the line or
19391 fits exactly on the line. We must continue
19392 the line because we can't draw the cursor
19393 after the glyph. */
19394 row->continued_p = 1;
19395 it->current_x = new_x;
19396 it->continuation_lines_width += new_x;
19397 ++it->hpos;
19398 if (i == nglyphs - 1)
19400 /* If line-wrap is on, check if a previous
19401 wrap point was found. */
19402 if (wrap_row_used > 0
19403 /* Even if there is a previous wrap
19404 point, continue the line here as
19405 usual, if (i) the previous character
19406 was a space or tab AND (ii) the
19407 current character is not. */
19408 && (!may_wrap
19409 || IT_DISPLAYING_WHITESPACE (it)))
19410 goto back_to_wrap;
19412 /* Record the maximum and minimum buffer
19413 positions seen so far in glyphs that will be
19414 displayed by this row. */
19415 if (it->bidi_p)
19416 RECORD_MAX_MIN_POS (it);
19417 set_iterator_to_next (it, 1);
19418 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19420 if (!get_next_display_element (it))
19422 row->exact_window_width_line_p = 1;
19423 it->continuation_lines_width = 0;
19424 row->continued_p = 0;
19425 row->ends_at_zv_p = 1;
19427 else if (ITERATOR_AT_END_OF_LINE_P (it))
19429 row->continued_p = 0;
19430 row->exact_window_width_line_p = 1;
19434 else if (it->bidi_p)
19435 RECORD_MAX_MIN_POS (it);
19437 else if (CHAR_GLYPH_PADDING_P (*glyph)
19438 && !FRAME_WINDOW_P (it->f))
19440 /* A padding glyph that doesn't fit on this line.
19441 This means the whole character doesn't fit
19442 on the line. */
19443 if (row->reversed_p)
19444 unproduce_glyphs (it, row->used[TEXT_AREA]
19445 - n_glyphs_before);
19446 row->used[TEXT_AREA] = n_glyphs_before;
19448 /* Fill the rest of the row with continuation
19449 glyphs like in 20.x. */
19450 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19451 < row->glyphs[1 + TEXT_AREA])
19452 produce_special_glyphs (it, IT_CONTINUATION);
19454 row->continued_p = 1;
19455 it->current_x = x_before;
19456 it->continuation_lines_width += x_before;
19458 /* Restore the height to what it was before the
19459 element not fitting on the line. */
19460 it->max_ascent = ascent;
19461 it->max_descent = descent;
19462 it->max_phys_ascent = phys_ascent;
19463 it->max_phys_descent = phys_descent;
19465 else if (wrap_row_used > 0)
19467 back_to_wrap:
19468 if (row->reversed_p)
19469 unproduce_glyphs (it,
19470 row->used[TEXT_AREA] - wrap_row_used);
19471 RESTORE_IT (it, &wrap_it, wrap_data);
19472 it->continuation_lines_width += wrap_x;
19473 row->used[TEXT_AREA] = wrap_row_used;
19474 row->ascent = wrap_row_ascent;
19475 row->height = wrap_row_height;
19476 row->phys_ascent = wrap_row_phys_ascent;
19477 row->phys_height = wrap_row_phys_height;
19478 row->extra_line_spacing = wrap_row_extra_line_spacing;
19479 min_pos = wrap_row_min_pos;
19480 min_bpos = wrap_row_min_bpos;
19481 max_pos = wrap_row_max_pos;
19482 max_bpos = wrap_row_max_bpos;
19483 row->continued_p = 1;
19484 row->ends_at_zv_p = 0;
19485 row->exact_window_width_line_p = 0;
19486 it->continuation_lines_width += x;
19488 /* Make sure that a non-default face is extended
19489 up to the right margin of the window. */
19490 extend_face_to_end_of_line (it);
19492 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19494 /* A TAB that extends past the right edge of the
19495 window. This produces a single glyph on
19496 window system frames. We leave the glyph in
19497 this row and let it fill the row, but don't
19498 consume the TAB. */
19499 if ((row->reversed_p
19500 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19501 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19502 produce_special_glyphs (it, IT_CONTINUATION);
19503 it->continuation_lines_width += it->last_visible_x;
19504 row->ends_in_middle_of_char_p = 1;
19505 row->continued_p = 1;
19506 glyph->pixel_width = it->last_visible_x - x;
19507 it->starts_in_middle_of_char_p = 1;
19509 else
19511 /* Something other than a TAB that draws past
19512 the right edge of the window. Restore
19513 positions to values before the element. */
19514 if (row->reversed_p)
19515 unproduce_glyphs (it, row->used[TEXT_AREA]
19516 - (n_glyphs_before + i));
19517 row->used[TEXT_AREA] = n_glyphs_before + i;
19519 /* Display continuation glyphs. */
19520 it->current_x = x_before;
19521 it->continuation_lines_width += x;
19522 if (!FRAME_WINDOW_P (it->f)
19523 || (row->reversed_p
19524 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19525 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19526 produce_special_glyphs (it, IT_CONTINUATION);
19527 row->continued_p = 1;
19529 extend_face_to_end_of_line (it);
19531 if (nglyphs > 1 && i > 0)
19533 row->ends_in_middle_of_char_p = 1;
19534 it->starts_in_middle_of_char_p = 1;
19537 /* Restore the height to what it was before the
19538 element not fitting on the line. */
19539 it->max_ascent = ascent;
19540 it->max_descent = descent;
19541 it->max_phys_ascent = phys_ascent;
19542 it->max_phys_descent = phys_descent;
19545 break;
19547 else if (new_x > it->first_visible_x)
19549 /* Increment number of glyphs actually displayed. */
19550 ++it->hpos;
19552 /* Record the maximum and minimum buffer positions
19553 seen so far in glyphs that will be displayed by
19554 this row. */
19555 if (it->bidi_p)
19556 RECORD_MAX_MIN_POS (it);
19558 if (x < it->first_visible_x)
19559 /* Glyph is partially visible, i.e. row starts at
19560 negative X position. */
19561 row->x = x - it->first_visible_x;
19563 else
19565 /* Glyph is completely off the left margin of the
19566 window. This should not happen because of the
19567 move_it_in_display_line at the start of this
19568 function, unless the text display area of the
19569 window is empty. */
19570 eassert (it->first_visible_x <= it->last_visible_x);
19573 /* Even if this display element produced no glyphs at all,
19574 we want to record its position. */
19575 if (it->bidi_p && nglyphs == 0)
19576 RECORD_MAX_MIN_POS (it);
19578 row->ascent = max (row->ascent, it->max_ascent);
19579 row->height = max (row->height, it->max_ascent + it->max_descent);
19580 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19581 row->phys_height = max (row->phys_height,
19582 it->max_phys_ascent + it->max_phys_descent);
19583 row->extra_line_spacing = max (row->extra_line_spacing,
19584 it->max_extra_line_spacing);
19586 /* End of this display line if row is continued. */
19587 if (row->continued_p || row->ends_at_zv_p)
19588 break;
19591 at_end_of_line:
19592 /* Is this a line end? If yes, we're also done, after making
19593 sure that a non-default face is extended up to the right
19594 margin of the window. */
19595 if (ITERATOR_AT_END_OF_LINE_P (it))
19597 int used_before = row->used[TEXT_AREA];
19599 row->ends_in_newline_from_string_p = STRINGP (it->object);
19601 /* Add a space at the end of the line that is used to
19602 display the cursor there. */
19603 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19604 append_space_for_newline (it, 0);
19606 /* Extend the face to the end of the line. */
19607 extend_face_to_end_of_line (it);
19609 /* Make sure we have the position. */
19610 if (used_before == 0)
19611 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19613 /* Record the position of the newline, for use in
19614 find_row_edges. */
19615 it->eol_pos = it->current.pos;
19617 /* Consume the line end. This skips over invisible lines. */
19618 set_iterator_to_next (it, 1);
19619 it->continuation_lines_width = 0;
19620 break;
19623 /* Proceed with next display element. Note that this skips
19624 over lines invisible because of selective display. */
19625 set_iterator_to_next (it, 1);
19627 /* If we truncate lines, we are done when the last displayed
19628 glyphs reach past the right margin of the window. */
19629 if (it->line_wrap == TRUNCATE
19630 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19631 ? (it->current_x >= it->last_visible_x)
19632 : (it->current_x > it->last_visible_x)))
19634 /* Maybe add truncation glyphs. */
19635 if (!FRAME_WINDOW_P (it->f)
19636 || (row->reversed_p
19637 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19638 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19640 int i, n;
19642 if (!row->reversed_p)
19644 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19645 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19646 break;
19648 else
19650 for (i = 0; i < row->used[TEXT_AREA]; i++)
19651 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19652 break;
19653 /* Remove any padding glyphs at the front of ROW, to
19654 make room for the truncation glyphs we will be
19655 adding below. The loop below always inserts at
19656 least one truncation glyph, so also remove the
19657 last glyph added to ROW. */
19658 unproduce_glyphs (it, i + 1);
19659 /* Adjust i for the loop below. */
19660 i = row->used[TEXT_AREA] - (i + 1);
19663 it->current_x = x_before;
19664 if (!FRAME_WINDOW_P (it->f))
19666 for (n = row->used[TEXT_AREA]; i < n; ++i)
19668 row->used[TEXT_AREA] = i;
19669 produce_special_glyphs (it, IT_TRUNCATION);
19672 else
19674 row->used[TEXT_AREA] = i;
19675 produce_special_glyphs (it, IT_TRUNCATION);
19678 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19680 /* Don't truncate if we can overflow newline into fringe. */
19681 if (!get_next_display_element (it))
19683 it->continuation_lines_width = 0;
19684 row->ends_at_zv_p = 1;
19685 row->exact_window_width_line_p = 1;
19686 break;
19688 if (ITERATOR_AT_END_OF_LINE_P (it))
19690 row->exact_window_width_line_p = 1;
19691 goto at_end_of_line;
19693 it->current_x = x_before;
19696 row->truncated_on_right_p = 1;
19697 it->continuation_lines_width = 0;
19698 reseat_at_next_visible_line_start (it, 0);
19699 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19700 it->hpos = hpos_before;
19701 break;
19705 if (wrap_data)
19706 bidi_unshelve_cache (wrap_data, 1);
19708 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19709 at the left window margin. */
19710 if (it->first_visible_x
19711 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19713 if (!FRAME_WINDOW_P (it->f)
19714 || (row->reversed_p
19715 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19716 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19717 insert_left_trunc_glyphs (it);
19718 row->truncated_on_left_p = 1;
19721 /* Remember the position at which this line ends.
19723 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19724 cannot be before the call to find_row_edges below, since that is
19725 where these positions are determined. */
19726 row->end = it->current;
19727 if (!it->bidi_p)
19729 row->minpos = row->start.pos;
19730 row->maxpos = row->end.pos;
19732 else
19734 /* ROW->minpos and ROW->maxpos must be the smallest and
19735 `1 + the largest' buffer positions in ROW. But if ROW was
19736 bidi-reordered, these two positions can be anywhere in the
19737 row, so we must determine them now. */
19738 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19741 /* If the start of this line is the overlay arrow-position, then
19742 mark this glyph row as the one containing the overlay arrow.
19743 This is clearly a mess with variable size fonts. It would be
19744 better to let it be displayed like cursors under X. */
19745 if ((row->displays_text_p || !overlay_arrow_seen)
19746 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19747 !NILP (overlay_arrow_string)))
19749 /* Overlay arrow in window redisplay is a fringe bitmap. */
19750 if (STRINGP (overlay_arrow_string))
19752 struct glyph_row *arrow_row
19753 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19754 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19755 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19756 struct glyph *p = row->glyphs[TEXT_AREA];
19757 struct glyph *p2, *end;
19759 /* Copy the arrow glyphs. */
19760 while (glyph < arrow_end)
19761 *p++ = *glyph++;
19763 /* Throw away padding glyphs. */
19764 p2 = p;
19765 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19766 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19767 ++p2;
19768 if (p2 > p)
19770 while (p2 < end)
19771 *p++ = *p2++;
19772 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19775 else
19777 eassert (INTEGERP (overlay_arrow_string));
19778 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19780 overlay_arrow_seen = 1;
19783 /* Highlight trailing whitespace. */
19784 if (!NILP (Vshow_trailing_whitespace))
19785 highlight_trailing_whitespace (it->f, it->glyph_row);
19787 /* Compute pixel dimensions of this line. */
19788 compute_line_metrics (it);
19790 /* Implementation note: No changes in the glyphs of ROW or in their
19791 faces can be done past this point, because compute_line_metrics
19792 computes ROW's hash value and stores it within the glyph_row
19793 structure. */
19795 /* Record whether this row ends inside an ellipsis. */
19796 row->ends_in_ellipsis_p
19797 = (it->method == GET_FROM_DISPLAY_VECTOR
19798 && it->ellipsis_p);
19800 /* Save fringe bitmaps in this row. */
19801 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19802 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19803 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19804 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19806 it->left_user_fringe_bitmap = 0;
19807 it->left_user_fringe_face_id = 0;
19808 it->right_user_fringe_bitmap = 0;
19809 it->right_user_fringe_face_id = 0;
19811 /* Maybe set the cursor. */
19812 cvpos = it->w->cursor.vpos;
19813 if ((cvpos < 0
19814 /* In bidi-reordered rows, keep checking for proper cursor
19815 position even if one has been found already, because buffer
19816 positions in such rows change non-linearly with ROW->VPOS,
19817 when a line is continued. One exception: when we are at ZV,
19818 display cursor on the first suitable glyph row, since all
19819 the empty rows after that also have their position set to ZV. */
19820 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19821 lines' rows is implemented for bidi-reordered rows. */
19822 || (it->bidi_p
19823 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19824 && PT >= MATRIX_ROW_START_CHARPOS (row)
19825 && PT <= MATRIX_ROW_END_CHARPOS (row)
19826 && cursor_row_p (row))
19827 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19829 /* Prepare for the next line. This line starts horizontally at (X
19830 HPOS) = (0 0). Vertical positions are incremented. As a
19831 convenience for the caller, IT->glyph_row is set to the next
19832 row to be used. */
19833 it->current_x = it->hpos = 0;
19834 it->current_y += row->height;
19835 SET_TEXT_POS (it->eol_pos, 0, 0);
19836 ++it->vpos;
19837 ++it->glyph_row;
19838 /* The next row should by default use the same value of the
19839 reversed_p flag as this one. set_iterator_to_next decides when
19840 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19841 the flag accordingly. */
19842 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19843 it->glyph_row->reversed_p = row->reversed_p;
19844 it->start = row->end;
19845 return row->displays_text_p;
19847 #undef RECORD_MAX_MIN_POS
19850 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19851 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19852 doc: /* Return paragraph direction at point in BUFFER.
19853 Value is either `left-to-right' or `right-to-left'.
19854 If BUFFER is omitted or nil, it defaults to the current buffer.
19856 Paragraph direction determines how the text in the paragraph is displayed.
19857 In left-to-right paragraphs, text begins at the left margin of the window
19858 and the reading direction is generally left to right. In right-to-left
19859 paragraphs, text begins at the right margin and is read from right to left.
19861 See also `bidi-paragraph-direction'. */)
19862 (Lisp_Object buffer)
19864 struct buffer *buf = current_buffer;
19865 struct buffer *old = buf;
19867 if (! NILP (buffer))
19869 CHECK_BUFFER (buffer);
19870 buf = XBUFFER (buffer);
19873 if (NILP (BVAR (buf, bidi_display_reordering))
19874 || NILP (BVAR (buf, enable_multibyte_characters))
19875 /* When we are loading loadup.el, the character property tables
19876 needed for bidi iteration are not yet available. */
19877 || !NILP (Vpurify_flag))
19878 return Qleft_to_right;
19879 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19880 return BVAR (buf, bidi_paragraph_direction);
19881 else
19883 /* Determine the direction from buffer text. We could try to
19884 use current_matrix if it is up to date, but this seems fast
19885 enough as it is. */
19886 struct bidi_it itb;
19887 ptrdiff_t pos = BUF_PT (buf);
19888 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19889 int c;
19890 void *itb_data = bidi_shelve_cache ();
19892 set_buffer_temp (buf);
19893 /* bidi_paragraph_init finds the base direction of the paragraph
19894 by searching forward from paragraph start. We need the base
19895 direction of the current or _previous_ paragraph, so we need
19896 to make sure we are within that paragraph. To that end, find
19897 the previous non-empty line. */
19898 if (pos >= ZV && pos > BEGV)
19900 pos--;
19901 bytepos = CHAR_TO_BYTE (pos);
19903 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19904 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19906 while ((c = FETCH_BYTE (bytepos)) == '\n'
19907 || c == ' ' || c == '\t' || c == '\f')
19909 if (bytepos <= BEGV_BYTE)
19910 break;
19911 bytepos--;
19912 pos--;
19914 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19915 bytepos--;
19917 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19918 itb.paragraph_dir = NEUTRAL_DIR;
19919 itb.string.s = NULL;
19920 itb.string.lstring = Qnil;
19921 itb.string.bufpos = 0;
19922 itb.string.unibyte = 0;
19923 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19924 bidi_unshelve_cache (itb_data, 0);
19925 set_buffer_temp (old);
19926 switch (itb.paragraph_dir)
19928 case L2R:
19929 return Qleft_to_right;
19930 break;
19931 case R2L:
19932 return Qright_to_left;
19933 break;
19934 default:
19935 emacs_abort ();
19942 /***********************************************************************
19943 Menu Bar
19944 ***********************************************************************/
19946 /* Redisplay the menu bar in the frame for window W.
19948 The menu bar of X frames that don't have X toolkit support is
19949 displayed in a special window W->frame->menu_bar_window.
19951 The menu bar of terminal frames is treated specially as far as
19952 glyph matrices are concerned. Menu bar lines are not part of
19953 windows, so the update is done directly on the frame matrix rows
19954 for the menu bar. */
19956 static void
19957 display_menu_bar (struct window *w)
19959 struct frame *f = XFRAME (WINDOW_FRAME (w));
19960 struct it it;
19961 Lisp_Object items;
19962 int i;
19964 /* Don't do all this for graphical frames. */
19965 #ifdef HAVE_NTGUI
19966 if (FRAME_W32_P (f))
19967 return;
19968 #endif
19969 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19970 if (FRAME_X_P (f))
19971 return;
19972 #endif
19974 #ifdef HAVE_NS
19975 if (FRAME_NS_P (f))
19976 return;
19977 #endif /* HAVE_NS */
19979 #ifdef USE_X_TOOLKIT
19980 eassert (!FRAME_WINDOW_P (f));
19981 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19982 it.first_visible_x = 0;
19983 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19984 #else /* not USE_X_TOOLKIT */
19985 if (FRAME_WINDOW_P (f))
19987 /* Menu bar lines are displayed in the desired matrix of the
19988 dummy window menu_bar_window. */
19989 struct window *menu_w;
19990 eassert (WINDOWP (f->menu_bar_window));
19991 menu_w = XWINDOW (f->menu_bar_window);
19992 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19993 MENU_FACE_ID);
19994 it.first_visible_x = 0;
19995 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19997 else
19999 /* This is a TTY frame, i.e. character hpos/vpos are used as
20000 pixel x/y. */
20001 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20002 MENU_FACE_ID);
20003 it.first_visible_x = 0;
20004 it.last_visible_x = FRAME_COLS (f);
20006 #endif /* not USE_X_TOOLKIT */
20008 /* FIXME: This should be controlled by a user option. See the
20009 comments in redisplay_tool_bar and display_mode_line about
20010 this. */
20011 it.paragraph_embedding = L2R;
20013 /* Clear all rows of the menu bar. */
20014 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20016 struct glyph_row *row = it.glyph_row + i;
20017 clear_glyph_row (row);
20018 row->enabled_p = 1;
20019 row->full_width_p = 1;
20022 /* Display all items of the menu bar. */
20023 items = FRAME_MENU_BAR_ITEMS (it.f);
20024 for (i = 0; i < ASIZE (items); i += 4)
20026 Lisp_Object string;
20028 /* Stop at nil string. */
20029 string = AREF (items, i + 1);
20030 if (NILP (string))
20031 break;
20033 /* Remember where item was displayed. */
20034 ASET (items, i + 3, make_number (it.hpos));
20036 /* Display the item, pad with one space. */
20037 if (it.current_x < it.last_visible_x)
20038 display_string (NULL, string, Qnil, 0, 0, &it,
20039 SCHARS (string) + 1, 0, 0, -1);
20042 /* Fill out the line with spaces. */
20043 if (it.current_x < it.last_visible_x)
20044 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20046 /* Compute the total height of the lines. */
20047 compute_line_metrics (&it);
20052 /***********************************************************************
20053 Mode Line
20054 ***********************************************************************/
20056 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20057 FORCE is non-zero, redisplay mode lines unconditionally.
20058 Otherwise, redisplay only mode lines that are garbaged. Value is
20059 the number of windows whose mode lines were redisplayed. */
20061 static int
20062 redisplay_mode_lines (Lisp_Object window, int force)
20064 int nwindows = 0;
20066 while (!NILP (window))
20068 struct window *w = XWINDOW (window);
20070 if (WINDOWP (w->hchild))
20071 nwindows += redisplay_mode_lines (w->hchild, force);
20072 else if (WINDOWP (w->vchild))
20073 nwindows += redisplay_mode_lines (w->vchild, force);
20074 else if (force
20075 || FRAME_GARBAGED_P (XFRAME (w->frame))
20076 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20078 struct text_pos lpoint;
20079 struct buffer *old = current_buffer;
20081 /* Set the window's buffer for the mode line display. */
20082 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20083 set_buffer_internal_1 (XBUFFER (w->buffer));
20085 /* Point refers normally to the selected window. For any
20086 other window, set up appropriate value. */
20087 if (!EQ (window, selected_window))
20089 struct text_pos pt;
20091 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20092 if (CHARPOS (pt) < BEGV)
20093 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20094 else if (CHARPOS (pt) > (ZV - 1))
20095 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20096 else
20097 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20100 /* Display mode lines. */
20101 clear_glyph_matrix (w->desired_matrix);
20102 if (display_mode_lines (w))
20104 ++nwindows;
20105 w->must_be_updated_p = 1;
20108 /* Restore old settings. */
20109 set_buffer_internal_1 (old);
20110 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20113 window = w->next;
20116 return nwindows;
20120 /* Display the mode and/or header line of window W. Value is the
20121 sum number of mode lines and header lines displayed. */
20123 static int
20124 display_mode_lines (struct window *w)
20126 Lisp_Object old_selected_window = selected_window;
20127 Lisp_Object old_selected_frame = selected_frame;
20128 Lisp_Object new_frame = w->frame;
20129 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20130 int n = 0;
20132 selected_frame = new_frame;
20133 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20134 or window's point, then we'd need select_window_1 here as well. */
20135 XSETWINDOW (selected_window, w);
20136 XFRAME (new_frame)->selected_window = selected_window;
20138 /* These will be set while the mode line specs are processed. */
20139 line_number_displayed = 0;
20140 w->column_number_displayed = -1;
20142 if (WINDOW_WANTS_MODELINE_P (w))
20144 struct window *sel_w = XWINDOW (old_selected_window);
20146 /* Select mode line face based on the real selected window. */
20147 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20148 BVAR (current_buffer, mode_line_format));
20149 ++n;
20152 if (WINDOW_WANTS_HEADER_LINE_P (w))
20154 display_mode_line (w, HEADER_LINE_FACE_ID,
20155 BVAR (current_buffer, header_line_format));
20156 ++n;
20159 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20160 selected_frame = old_selected_frame;
20161 selected_window = old_selected_window;
20162 return n;
20166 /* Display mode or header line of window W. FACE_ID specifies which
20167 line to display; it is either MODE_LINE_FACE_ID or
20168 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20169 display. Value is the pixel height of the mode/header line
20170 displayed. */
20172 static int
20173 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20175 struct it it;
20176 struct face *face;
20177 ptrdiff_t count = SPECPDL_INDEX ();
20179 init_iterator (&it, w, -1, -1, NULL, face_id);
20180 /* Don't extend on a previously drawn mode-line.
20181 This may happen if called from pos_visible_p. */
20182 it.glyph_row->enabled_p = 0;
20183 prepare_desired_row (it.glyph_row);
20185 it.glyph_row->mode_line_p = 1;
20187 /* FIXME: This should be controlled by a user option. But
20188 supporting such an option is not trivial, since the mode line is
20189 made up of many separate strings. */
20190 it.paragraph_embedding = L2R;
20192 record_unwind_protect (unwind_format_mode_line,
20193 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20195 mode_line_target = MODE_LINE_DISPLAY;
20197 /* Temporarily make frame's keyboard the current kboard so that
20198 kboard-local variables in the mode_line_format will get the right
20199 values. */
20200 push_kboard (FRAME_KBOARD (it.f));
20201 record_unwind_save_match_data ();
20202 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20203 pop_kboard ();
20205 unbind_to (count, Qnil);
20207 /* Fill up with spaces. */
20208 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20210 compute_line_metrics (&it);
20211 it.glyph_row->full_width_p = 1;
20212 it.glyph_row->continued_p = 0;
20213 it.glyph_row->truncated_on_left_p = 0;
20214 it.glyph_row->truncated_on_right_p = 0;
20216 /* Make a 3D mode-line have a shadow at its right end. */
20217 face = FACE_FROM_ID (it.f, face_id);
20218 extend_face_to_end_of_line (&it);
20219 if (face->box != FACE_NO_BOX)
20221 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20222 + it.glyph_row->used[TEXT_AREA] - 1);
20223 last->right_box_line_p = 1;
20226 return it.glyph_row->height;
20229 /* Move element ELT in LIST to the front of LIST.
20230 Return the updated list. */
20232 static Lisp_Object
20233 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20235 register Lisp_Object tail, prev;
20236 register Lisp_Object tem;
20238 tail = list;
20239 prev = Qnil;
20240 while (CONSP (tail))
20242 tem = XCAR (tail);
20244 if (EQ (elt, tem))
20246 /* Splice out the link TAIL. */
20247 if (NILP (prev))
20248 list = XCDR (tail);
20249 else
20250 Fsetcdr (prev, XCDR (tail));
20252 /* Now make it the first. */
20253 Fsetcdr (tail, list);
20254 return tail;
20256 else
20257 prev = tail;
20258 tail = XCDR (tail);
20259 QUIT;
20262 /* Not found--return unchanged LIST. */
20263 return list;
20266 /* Contribute ELT to the mode line for window IT->w. How it
20267 translates into text depends on its data type.
20269 IT describes the display environment in which we display, as usual.
20271 DEPTH is the depth in recursion. It is used to prevent
20272 infinite recursion here.
20274 FIELD_WIDTH is the number of characters the display of ELT should
20275 occupy in the mode line, and PRECISION is the maximum number of
20276 characters to display from ELT's representation. See
20277 display_string for details.
20279 Returns the hpos of the end of the text generated by ELT.
20281 PROPS is a property list to add to any string we encounter.
20283 If RISKY is nonzero, remove (disregard) any properties in any string
20284 we encounter, and ignore :eval and :propertize.
20286 The global variable `mode_line_target' determines whether the
20287 output is passed to `store_mode_line_noprop',
20288 `store_mode_line_string', or `display_string'. */
20290 static int
20291 display_mode_element (struct it *it, int depth, int field_width, int precision,
20292 Lisp_Object elt, Lisp_Object props, int risky)
20294 int n = 0, field, prec;
20295 int literal = 0;
20297 tail_recurse:
20298 if (depth > 100)
20299 elt = build_string ("*too-deep*");
20301 depth++;
20303 switch (XTYPE (elt))
20305 case Lisp_String:
20307 /* A string: output it and check for %-constructs within it. */
20308 unsigned char c;
20309 ptrdiff_t offset = 0;
20311 if (SCHARS (elt) > 0
20312 && (!NILP (props) || risky))
20314 Lisp_Object oprops, aelt;
20315 oprops = Ftext_properties_at (make_number (0), elt);
20317 /* If the starting string's properties are not what
20318 we want, translate the string. Also, if the string
20319 is risky, do that anyway. */
20321 if (NILP (Fequal (props, oprops)) || risky)
20323 /* If the starting string has properties,
20324 merge the specified ones onto the existing ones. */
20325 if (! NILP (oprops) && !risky)
20327 Lisp_Object tem;
20329 oprops = Fcopy_sequence (oprops);
20330 tem = props;
20331 while (CONSP (tem))
20333 oprops = Fplist_put (oprops, XCAR (tem),
20334 XCAR (XCDR (tem)));
20335 tem = XCDR (XCDR (tem));
20337 props = oprops;
20340 aelt = Fassoc (elt, mode_line_proptrans_alist);
20341 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20343 /* AELT is what we want. Move it to the front
20344 without consing. */
20345 elt = XCAR (aelt);
20346 mode_line_proptrans_alist
20347 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20349 else
20351 Lisp_Object tem;
20353 /* If AELT has the wrong props, it is useless.
20354 so get rid of it. */
20355 if (! NILP (aelt))
20356 mode_line_proptrans_alist
20357 = Fdelq (aelt, mode_line_proptrans_alist);
20359 elt = Fcopy_sequence (elt);
20360 Fset_text_properties (make_number (0), Flength (elt),
20361 props, elt);
20362 /* Add this item to mode_line_proptrans_alist. */
20363 mode_line_proptrans_alist
20364 = Fcons (Fcons (elt, props),
20365 mode_line_proptrans_alist);
20366 /* Truncate mode_line_proptrans_alist
20367 to at most 50 elements. */
20368 tem = Fnthcdr (make_number (50),
20369 mode_line_proptrans_alist);
20370 if (! NILP (tem))
20371 XSETCDR (tem, Qnil);
20376 offset = 0;
20378 if (literal)
20380 prec = precision - n;
20381 switch (mode_line_target)
20383 case MODE_LINE_NOPROP:
20384 case MODE_LINE_TITLE:
20385 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20386 break;
20387 case MODE_LINE_STRING:
20388 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20389 break;
20390 case MODE_LINE_DISPLAY:
20391 n += display_string (NULL, elt, Qnil, 0, 0, it,
20392 0, prec, 0, STRING_MULTIBYTE (elt));
20393 break;
20396 break;
20399 /* Handle the non-literal case. */
20401 while ((precision <= 0 || n < precision)
20402 && SREF (elt, offset) != 0
20403 && (mode_line_target != MODE_LINE_DISPLAY
20404 || it->current_x < it->last_visible_x))
20406 ptrdiff_t last_offset = offset;
20408 /* Advance to end of string or next format specifier. */
20409 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20412 if (offset - 1 != last_offset)
20414 ptrdiff_t nchars, nbytes;
20416 /* Output to end of string or up to '%'. Field width
20417 is length of string. Don't output more than
20418 PRECISION allows us. */
20419 offset--;
20421 prec = c_string_width (SDATA (elt) + last_offset,
20422 offset - last_offset, precision - n,
20423 &nchars, &nbytes);
20425 switch (mode_line_target)
20427 case MODE_LINE_NOPROP:
20428 case MODE_LINE_TITLE:
20429 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20430 break;
20431 case MODE_LINE_STRING:
20433 ptrdiff_t bytepos = last_offset;
20434 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20435 ptrdiff_t endpos = (precision <= 0
20436 ? string_byte_to_char (elt, offset)
20437 : charpos + nchars);
20439 n += store_mode_line_string (NULL,
20440 Fsubstring (elt, make_number (charpos),
20441 make_number (endpos)),
20442 0, 0, 0, Qnil);
20444 break;
20445 case MODE_LINE_DISPLAY:
20447 ptrdiff_t bytepos = last_offset;
20448 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20450 if (precision <= 0)
20451 nchars = string_byte_to_char (elt, offset) - charpos;
20452 n += display_string (NULL, elt, Qnil, 0, charpos,
20453 it, 0, nchars, 0,
20454 STRING_MULTIBYTE (elt));
20456 break;
20459 else /* c == '%' */
20461 ptrdiff_t percent_position = offset;
20463 /* Get the specified minimum width. Zero means
20464 don't pad. */
20465 field = 0;
20466 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20467 field = field * 10 + c - '0';
20469 /* Don't pad beyond the total padding allowed. */
20470 if (field_width - n > 0 && field > field_width - n)
20471 field = field_width - n;
20473 /* Note that either PRECISION <= 0 or N < PRECISION. */
20474 prec = precision - n;
20476 if (c == 'M')
20477 n += display_mode_element (it, depth, field, prec,
20478 Vglobal_mode_string, props,
20479 risky);
20480 else if (c != 0)
20482 int multibyte;
20483 ptrdiff_t bytepos, charpos;
20484 const char *spec;
20485 Lisp_Object string;
20487 bytepos = percent_position;
20488 charpos = (STRING_MULTIBYTE (elt)
20489 ? string_byte_to_char (elt, bytepos)
20490 : bytepos);
20491 spec = decode_mode_spec (it->w, c, field, &string);
20492 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20494 switch (mode_line_target)
20496 case MODE_LINE_NOPROP:
20497 case MODE_LINE_TITLE:
20498 n += store_mode_line_noprop (spec, field, prec);
20499 break;
20500 case MODE_LINE_STRING:
20502 Lisp_Object tem = build_string (spec);
20503 props = Ftext_properties_at (make_number (charpos), elt);
20504 /* Should only keep face property in props */
20505 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20507 break;
20508 case MODE_LINE_DISPLAY:
20510 int nglyphs_before, nwritten;
20512 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20513 nwritten = display_string (spec, string, elt,
20514 charpos, 0, it,
20515 field, prec, 0,
20516 multibyte);
20518 /* Assign to the glyphs written above the
20519 string where the `%x' came from, position
20520 of the `%'. */
20521 if (nwritten > 0)
20523 struct glyph *glyph
20524 = (it->glyph_row->glyphs[TEXT_AREA]
20525 + nglyphs_before);
20526 int i;
20528 for (i = 0; i < nwritten; ++i)
20530 glyph[i].object = elt;
20531 glyph[i].charpos = charpos;
20534 n += nwritten;
20537 break;
20540 else /* c == 0 */
20541 break;
20545 break;
20547 case Lisp_Symbol:
20548 /* A symbol: process the value of the symbol recursively
20549 as if it appeared here directly. Avoid error if symbol void.
20550 Special case: if value of symbol is a string, output the string
20551 literally. */
20553 register Lisp_Object tem;
20555 /* If the variable is not marked as risky to set
20556 then its contents are risky to use. */
20557 if (NILP (Fget (elt, Qrisky_local_variable)))
20558 risky = 1;
20560 tem = Fboundp (elt);
20561 if (!NILP (tem))
20563 tem = Fsymbol_value (elt);
20564 /* If value is a string, output that string literally:
20565 don't check for % within it. */
20566 if (STRINGP (tem))
20567 literal = 1;
20569 if (!EQ (tem, elt))
20571 /* Give up right away for nil or t. */
20572 elt = tem;
20573 goto tail_recurse;
20577 break;
20579 case Lisp_Cons:
20581 register Lisp_Object car, tem;
20583 /* A cons cell: five distinct cases.
20584 If first element is :eval or :propertize, do something special.
20585 If first element is a string or a cons, process all the elements
20586 and effectively concatenate them.
20587 If first element is a negative number, truncate displaying cdr to
20588 at most that many characters. If positive, pad (with spaces)
20589 to at least that many characters.
20590 If first element is a symbol, process the cadr or caddr recursively
20591 according to whether the symbol's value is non-nil or nil. */
20592 car = XCAR (elt);
20593 if (EQ (car, QCeval))
20595 /* An element of the form (:eval FORM) means evaluate FORM
20596 and use the result as mode line elements. */
20598 if (risky)
20599 break;
20601 if (CONSP (XCDR (elt)))
20603 Lisp_Object spec;
20604 spec = safe_eval (XCAR (XCDR (elt)));
20605 n += display_mode_element (it, depth, field_width - n,
20606 precision - n, spec, props,
20607 risky);
20610 else if (EQ (car, QCpropertize))
20612 /* An element of the form (:propertize ELT PROPS...)
20613 means display ELT but applying properties PROPS. */
20615 if (risky)
20616 break;
20618 if (CONSP (XCDR (elt)))
20619 n += display_mode_element (it, depth, field_width - n,
20620 precision - n, XCAR (XCDR (elt)),
20621 XCDR (XCDR (elt)), risky);
20623 else if (SYMBOLP (car))
20625 tem = Fboundp (car);
20626 elt = XCDR (elt);
20627 if (!CONSP (elt))
20628 goto invalid;
20629 /* elt is now the cdr, and we know it is a cons cell.
20630 Use its car if CAR has a non-nil value. */
20631 if (!NILP (tem))
20633 tem = Fsymbol_value (car);
20634 if (!NILP (tem))
20636 elt = XCAR (elt);
20637 goto tail_recurse;
20640 /* Symbol's value is nil (or symbol is unbound)
20641 Get the cddr of the original list
20642 and if possible find the caddr and use that. */
20643 elt = XCDR (elt);
20644 if (NILP (elt))
20645 break;
20646 else if (!CONSP (elt))
20647 goto invalid;
20648 elt = XCAR (elt);
20649 goto tail_recurse;
20651 else if (INTEGERP (car))
20653 register int lim = XINT (car);
20654 elt = XCDR (elt);
20655 if (lim < 0)
20657 /* Negative int means reduce maximum width. */
20658 if (precision <= 0)
20659 precision = -lim;
20660 else
20661 precision = min (precision, -lim);
20663 else if (lim > 0)
20665 /* Padding specified. Don't let it be more than
20666 current maximum. */
20667 if (precision > 0)
20668 lim = min (precision, lim);
20670 /* If that's more padding than already wanted, queue it.
20671 But don't reduce padding already specified even if
20672 that is beyond the current truncation point. */
20673 field_width = max (lim, field_width);
20675 goto tail_recurse;
20677 else if (STRINGP (car) || CONSP (car))
20679 Lisp_Object halftail = elt;
20680 int len = 0;
20682 while (CONSP (elt)
20683 && (precision <= 0 || n < precision))
20685 n += display_mode_element (it, depth,
20686 /* Do padding only after the last
20687 element in the list. */
20688 (! CONSP (XCDR (elt))
20689 ? field_width - n
20690 : 0),
20691 precision - n, XCAR (elt),
20692 props, risky);
20693 elt = XCDR (elt);
20694 len++;
20695 if ((len & 1) == 0)
20696 halftail = XCDR (halftail);
20697 /* Check for cycle. */
20698 if (EQ (halftail, elt))
20699 break;
20703 break;
20705 default:
20706 invalid:
20707 elt = build_string ("*invalid*");
20708 goto tail_recurse;
20711 /* Pad to FIELD_WIDTH. */
20712 if (field_width > 0 && n < field_width)
20714 switch (mode_line_target)
20716 case MODE_LINE_NOPROP:
20717 case MODE_LINE_TITLE:
20718 n += store_mode_line_noprop ("", field_width - n, 0);
20719 break;
20720 case MODE_LINE_STRING:
20721 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20722 break;
20723 case MODE_LINE_DISPLAY:
20724 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20725 0, 0, 0);
20726 break;
20730 return n;
20733 /* Store a mode-line string element in mode_line_string_list.
20735 If STRING is non-null, display that C string. Otherwise, the Lisp
20736 string LISP_STRING is displayed.
20738 FIELD_WIDTH is the minimum number of output glyphs to produce.
20739 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20740 with spaces. FIELD_WIDTH <= 0 means don't pad.
20742 PRECISION is the maximum number of characters to output from
20743 STRING. PRECISION <= 0 means don't truncate the string.
20745 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20746 properties to the string.
20748 PROPS are the properties to add to the string.
20749 The mode_line_string_face face property is always added to the string.
20752 static int
20753 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20754 int field_width, int precision, Lisp_Object props)
20756 ptrdiff_t len;
20757 int n = 0;
20759 if (string != NULL)
20761 len = strlen (string);
20762 if (precision > 0 && len > precision)
20763 len = precision;
20764 lisp_string = make_string (string, len);
20765 if (NILP (props))
20766 props = mode_line_string_face_prop;
20767 else if (!NILP (mode_line_string_face))
20769 Lisp_Object face = Fplist_get (props, Qface);
20770 props = Fcopy_sequence (props);
20771 if (NILP (face))
20772 face = mode_line_string_face;
20773 else
20774 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20775 props = Fplist_put (props, Qface, face);
20777 Fadd_text_properties (make_number (0), make_number (len),
20778 props, lisp_string);
20780 else
20782 len = XFASTINT (Flength (lisp_string));
20783 if (precision > 0 && len > precision)
20785 len = precision;
20786 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20787 precision = -1;
20789 if (!NILP (mode_line_string_face))
20791 Lisp_Object face;
20792 if (NILP (props))
20793 props = Ftext_properties_at (make_number (0), lisp_string);
20794 face = Fplist_get (props, Qface);
20795 if (NILP (face))
20796 face = mode_line_string_face;
20797 else
20798 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20799 props = Fcons (Qface, Fcons (face, Qnil));
20800 if (copy_string)
20801 lisp_string = Fcopy_sequence (lisp_string);
20803 if (!NILP (props))
20804 Fadd_text_properties (make_number (0), make_number (len),
20805 props, lisp_string);
20808 if (len > 0)
20810 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20811 n += len;
20814 if (field_width > len)
20816 field_width -= len;
20817 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20818 if (!NILP (props))
20819 Fadd_text_properties (make_number (0), make_number (field_width),
20820 props, lisp_string);
20821 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20822 n += field_width;
20825 return n;
20829 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20830 1, 4, 0,
20831 doc: /* Format a string out of a mode line format specification.
20832 First arg FORMAT specifies the mode line format (see `mode-line-format'
20833 for details) to use.
20835 By default, the format is evaluated for the currently selected window.
20837 Optional second arg FACE specifies the face property to put on all
20838 characters for which no face is specified. The value nil means the
20839 default face. The value t means whatever face the window's mode line
20840 currently uses (either `mode-line' or `mode-line-inactive',
20841 depending on whether the window is the selected window or not).
20842 An integer value means the value string has no text
20843 properties.
20845 Optional third and fourth args WINDOW and BUFFER specify the window
20846 and buffer to use as the context for the formatting (defaults
20847 are the selected window and the WINDOW's buffer). */)
20848 (Lisp_Object format, Lisp_Object face,
20849 Lisp_Object window, Lisp_Object buffer)
20851 struct it it;
20852 int len;
20853 struct window *w;
20854 struct buffer *old_buffer = NULL;
20855 int face_id;
20856 int no_props = INTEGERP (face);
20857 ptrdiff_t count = SPECPDL_INDEX ();
20858 Lisp_Object str;
20859 int string_start = 0;
20861 w = decode_any_window (window);
20862 XSETWINDOW (window, w);
20864 if (NILP (buffer))
20865 buffer = w->buffer;
20866 CHECK_BUFFER (buffer);
20868 /* Make formatting the modeline a non-op when noninteractive, otherwise
20869 there will be problems later caused by a partially initialized frame. */
20870 if (NILP (format) || noninteractive)
20871 return empty_unibyte_string;
20873 if (no_props)
20874 face = Qnil;
20876 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20877 : EQ (face, Qt) ? (EQ (window, selected_window)
20878 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20879 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20880 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20881 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20882 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20883 : DEFAULT_FACE_ID;
20885 old_buffer = current_buffer;
20887 /* Save things including mode_line_proptrans_alist,
20888 and set that to nil so that we don't alter the outer value. */
20889 record_unwind_protect (unwind_format_mode_line,
20890 format_mode_line_unwind_data
20891 (XFRAME (WINDOW_FRAME (w)),
20892 old_buffer, selected_window, 1));
20893 mode_line_proptrans_alist = Qnil;
20895 Fselect_window (window, Qt);
20896 set_buffer_internal_1 (XBUFFER (buffer));
20898 init_iterator (&it, w, -1, -1, NULL, face_id);
20900 if (no_props)
20902 mode_line_target = MODE_LINE_NOPROP;
20903 mode_line_string_face_prop = Qnil;
20904 mode_line_string_list = Qnil;
20905 string_start = MODE_LINE_NOPROP_LEN (0);
20907 else
20909 mode_line_target = MODE_LINE_STRING;
20910 mode_line_string_list = Qnil;
20911 mode_line_string_face = face;
20912 mode_line_string_face_prop
20913 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20916 push_kboard (FRAME_KBOARD (it.f));
20917 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20918 pop_kboard ();
20920 if (no_props)
20922 len = MODE_LINE_NOPROP_LEN (string_start);
20923 str = make_string (mode_line_noprop_buf + string_start, len);
20925 else
20927 mode_line_string_list = Fnreverse (mode_line_string_list);
20928 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20929 empty_unibyte_string);
20932 unbind_to (count, Qnil);
20933 return str;
20936 /* Write a null-terminated, right justified decimal representation of
20937 the positive integer D to BUF using a minimal field width WIDTH. */
20939 static void
20940 pint2str (register char *buf, register int width, register ptrdiff_t d)
20942 register char *p = buf;
20944 if (d <= 0)
20945 *p++ = '0';
20946 else
20948 while (d > 0)
20950 *p++ = d % 10 + '0';
20951 d /= 10;
20955 for (width -= (int) (p - buf); width > 0; --width)
20956 *p++ = ' ';
20957 *p-- = '\0';
20958 while (p > buf)
20960 d = *buf;
20961 *buf++ = *p;
20962 *p-- = d;
20966 /* Write a null-terminated, right justified decimal and "human
20967 readable" representation of the nonnegative integer D to BUF using
20968 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20970 static const char power_letter[] =
20972 0, /* no letter */
20973 'k', /* kilo */
20974 'M', /* mega */
20975 'G', /* giga */
20976 'T', /* tera */
20977 'P', /* peta */
20978 'E', /* exa */
20979 'Z', /* zetta */
20980 'Y' /* yotta */
20983 static void
20984 pint2hrstr (char *buf, int width, ptrdiff_t d)
20986 /* We aim to represent the nonnegative integer D as
20987 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20988 ptrdiff_t quotient = d;
20989 int remainder = 0;
20990 /* -1 means: do not use TENTHS. */
20991 int tenths = -1;
20992 int exponent = 0;
20994 /* Length of QUOTIENT.TENTHS as a string. */
20995 int length;
20997 char * psuffix;
20998 char * p;
21000 if (1000 <= quotient)
21002 /* Scale to the appropriate EXPONENT. */
21005 remainder = quotient % 1000;
21006 quotient /= 1000;
21007 exponent++;
21009 while (1000 <= quotient);
21011 /* Round to nearest and decide whether to use TENTHS or not. */
21012 if (quotient <= 9)
21014 tenths = remainder / 100;
21015 if (50 <= remainder % 100)
21017 if (tenths < 9)
21018 tenths++;
21019 else
21021 quotient++;
21022 if (quotient == 10)
21023 tenths = -1;
21024 else
21025 tenths = 0;
21029 else
21030 if (500 <= remainder)
21032 if (quotient < 999)
21033 quotient++;
21034 else
21036 quotient = 1;
21037 exponent++;
21038 tenths = 0;
21043 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21044 if (tenths == -1 && quotient <= 99)
21045 if (quotient <= 9)
21046 length = 1;
21047 else
21048 length = 2;
21049 else
21050 length = 3;
21051 p = psuffix = buf + max (width, length);
21053 /* Print EXPONENT. */
21054 *psuffix++ = power_letter[exponent];
21055 *psuffix = '\0';
21057 /* Print TENTHS. */
21058 if (tenths >= 0)
21060 *--p = '0' + tenths;
21061 *--p = '.';
21064 /* Print QUOTIENT. */
21067 int digit = quotient % 10;
21068 *--p = '0' + digit;
21070 while ((quotient /= 10) != 0);
21072 /* Print leading spaces. */
21073 while (buf < p)
21074 *--p = ' ';
21077 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21078 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21079 type of CODING_SYSTEM. Return updated pointer into BUF. */
21081 static unsigned char invalid_eol_type[] = "(*invalid*)";
21083 static char *
21084 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21086 Lisp_Object val;
21087 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21088 const unsigned char *eol_str;
21089 int eol_str_len;
21090 /* The EOL conversion we are using. */
21091 Lisp_Object eoltype;
21093 val = CODING_SYSTEM_SPEC (coding_system);
21094 eoltype = Qnil;
21096 if (!VECTORP (val)) /* Not yet decided. */
21098 *buf++ = multibyte ? '-' : ' ';
21099 if (eol_flag)
21100 eoltype = eol_mnemonic_undecided;
21101 /* Don't mention EOL conversion if it isn't decided. */
21103 else
21105 Lisp_Object attrs;
21106 Lisp_Object eolvalue;
21108 attrs = AREF (val, 0);
21109 eolvalue = AREF (val, 2);
21111 *buf++ = multibyte
21112 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21113 : ' ';
21115 if (eol_flag)
21117 /* The EOL conversion that is normal on this system. */
21119 if (NILP (eolvalue)) /* Not yet decided. */
21120 eoltype = eol_mnemonic_undecided;
21121 else if (VECTORP (eolvalue)) /* Not yet decided. */
21122 eoltype = eol_mnemonic_undecided;
21123 else /* eolvalue is Qunix, Qdos, or Qmac. */
21124 eoltype = (EQ (eolvalue, Qunix)
21125 ? eol_mnemonic_unix
21126 : (EQ (eolvalue, Qdos) == 1
21127 ? eol_mnemonic_dos : eol_mnemonic_mac));
21131 if (eol_flag)
21133 /* Mention the EOL conversion if it is not the usual one. */
21134 if (STRINGP (eoltype))
21136 eol_str = SDATA (eoltype);
21137 eol_str_len = SBYTES (eoltype);
21139 else if (CHARACTERP (eoltype))
21141 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21142 int c = XFASTINT (eoltype);
21143 eol_str_len = CHAR_STRING (c, tmp);
21144 eol_str = tmp;
21146 else
21148 eol_str = invalid_eol_type;
21149 eol_str_len = sizeof (invalid_eol_type) - 1;
21151 memcpy (buf, eol_str, eol_str_len);
21152 buf += eol_str_len;
21155 return buf;
21158 /* Return a string for the output of a mode line %-spec for window W,
21159 generated by character C. FIELD_WIDTH > 0 means pad the string
21160 returned with spaces to that value. Return a Lisp string in
21161 *STRING if the resulting string is taken from that Lisp string.
21163 Note we operate on the current buffer for most purposes. */
21165 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21167 static const char *
21168 decode_mode_spec (struct window *w, register int c, int field_width,
21169 Lisp_Object *string)
21171 Lisp_Object obj;
21172 struct frame *f = XFRAME (WINDOW_FRAME (w));
21173 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21174 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21175 produce strings from numerical values, so limit preposterously
21176 large values of FIELD_WIDTH to avoid overrunning the buffer's
21177 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21178 bytes plus the terminating null. */
21179 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21180 struct buffer *b = current_buffer;
21182 obj = Qnil;
21183 *string = Qnil;
21185 switch (c)
21187 case '*':
21188 if (!NILP (BVAR (b, read_only)))
21189 return "%";
21190 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21191 return "*";
21192 return "-";
21194 case '+':
21195 /* This differs from %* only for a modified read-only buffer. */
21196 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21197 return "*";
21198 if (!NILP (BVAR (b, read_only)))
21199 return "%";
21200 return "-";
21202 case '&':
21203 /* This differs from %* in ignoring read-only-ness. */
21204 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21205 return "*";
21206 return "-";
21208 case '%':
21209 return "%";
21211 case '[':
21213 int i;
21214 char *p;
21216 if (command_loop_level > 5)
21217 return "[[[... ";
21218 p = decode_mode_spec_buf;
21219 for (i = 0; i < command_loop_level; i++)
21220 *p++ = '[';
21221 *p = 0;
21222 return decode_mode_spec_buf;
21225 case ']':
21227 int i;
21228 char *p;
21230 if (command_loop_level > 5)
21231 return " ...]]]";
21232 p = decode_mode_spec_buf;
21233 for (i = 0; i < command_loop_level; i++)
21234 *p++ = ']';
21235 *p = 0;
21236 return decode_mode_spec_buf;
21239 case '-':
21241 register int i;
21243 /* Let lots_of_dashes be a string of infinite length. */
21244 if (mode_line_target == MODE_LINE_NOPROP
21245 || mode_line_target == MODE_LINE_STRING)
21246 return "--";
21247 if (field_width <= 0
21248 || field_width > sizeof (lots_of_dashes))
21250 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21251 decode_mode_spec_buf[i] = '-';
21252 decode_mode_spec_buf[i] = '\0';
21253 return decode_mode_spec_buf;
21255 else
21256 return lots_of_dashes;
21259 case 'b':
21260 obj = BVAR (b, name);
21261 break;
21263 case 'c':
21264 /* %c and %l are ignored in `frame-title-format'.
21265 (In redisplay_internal, the frame title is drawn _before_ the
21266 windows are updated, so the stuff which depends on actual
21267 window contents (such as %l) may fail to render properly, or
21268 even crash emacs.) */
21269 if (mode_line_target == MODE_LINE_TITLE)
21270 return "";
21271 else
21273 ptrdiff_t col = current_column ();
21274 w->column_number_displayed = col;
21275 pint2str (decode_mode_spec_buf, width, col);
21276 return decode_mode_spec_buf;
21279 case 'e':
21280 #ifndef SYSTEM_MALLOC
21282 if (NILP (Vmemory_full))
21283 return "";
21284 else
21285 return "!MEM FULL! ";
21287 #else
21288 return "";
21289 #endif
21291 case 'F':
21292 /* %F displays the frame name. */
21293 if (!NILP (f->title))
21294 return SSDATA (f->title);
21295 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21296 return SSDATA (f->name);
21297 return "Emacs";
21299 case 'f':
21300 obj = BVAR (b, filename);
21301 break;
21303 case 'i':
21305 ptrdiff_t size = ZV - BEGV;
21306 pint2str (decode_mode_spec_buf, width, size);
21307 return decode_mode_spec_buf;
21310 case 'I':
21312 ptrdiff_t size = ZV - BEGV;
21313 pint2hrstr (decode_mode_spec_buf, width, size);
21314 return decode_mode_spec_buf;
21317 case 'l':
21319 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21320 ptrdiff_t topline, nlines, height;
21321 ptrdiff_t junk;
21323 /* %c and %l are ignored in `frame-title-format'. */
21324 if (mode_line_target == MODE_LINE_TITLE)
21325 return "";
21327 startpos = marker_position (w->start);
21328 startpos_byte = marker_byte_position (w->start);
21329 height = WINDOW_TOTAL_LINES (w);
21331 /* If we decided that this buffer isn't suitable for line numbers,
21332 don't forget that too fast. */
21333 if (w->base_line_pos == -1)
21334 goto no_value;
21336 /* If the buffer is very big, don't waste time. */
21337 if (INTEGERP (Vline_number_display_limit)
21338 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21340 w->base_line_pos = 0;
21341 w->base_line_number = 0;
21342 goto no_value;
21345 if (w->base_line_number > 0
21346 && w->base_line_pos > 0
21347 && w->base_line_pos <= startpos)
21349 line = w->base_line_number;
21350 linepos = w->base_line_pos;
21351 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21353 else
21355 line = 1;
21356 linepos = BUF_BEGV (b);
21357 linepos_byte = BUF_BEGV_BYTE (b);
21360 /* Count lines from base line to window start position. */
21361 nlines = display_count_lines (linepos_byte,
21362 startpos_byte,
21363 startpos, &junk);
21365 topline = nlines + line;
21367 /* Determine a new base line, if the old one is too close
21368 or too far away, or if we did not have one.
21369 "Too close" means it's plausible a scroll-down would
21370 go back past it. */
21371 if (startpos == BUF_BEGV (b))
21373 w->base_line_number = topline;
21374 w->base_line_pos = BUF_BEGV (b);
21376 else if (nlines < height + 25 || nlines > height * 3 + 50
21377 || linepos == BUF_BEGV (b))
21379 ptrdiff_t limit = BUF_BEGV (b);
21380 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21381 ptrdiff_t position;
21382 ptrdiff_t distance =
21383 (height * 2 + 30) * line_number_display_limit_width;
21385 if (startpos - distance > limit)
21387 limit = startpos - distance;
21388 limit_byte = CHAR_TO_BYTE (limit);
21391 nlines = display_count_lines (startpos_byte,
21392 limit_byte,
21393 - (height * 2 + 30),
21394 &position);
21395 /* If we couldn't find the lines we wanted within
21396 line_number_display_limit_width chars per line,
21397 give up on line numbers for this window. */
21398 if (position == limit_byte && limit == startpos - distance)
21400 w->base_line_pos = -1;
21401 w->base_line_number = 0;
21402 goto no_value;
21405 w->base_line_number = topline - nlines;
21406 w->base_line_pos = BYTE_TO_CHAR (position);
21409 /* Now count lines from the start pos to point. */
21410 nlines = display_count_lines (startpos_byte,
21411 PT_BYTE, PT, &junk);
21413 /* Record that we did display the line number. */
21414 line_number_displayed = 1;
21416 /* Make the string to show. */
21417 pint2str (decode_mode_spec_buf, width, topline + nlines);
21418 return decode_mode_spec_buf;
21419 no_value:
21421 char* p = decode_mode_spec_buf;
21422 int pad = width - 2;
21423 while (pad-- > 0)
21424 *p++ = ' ';
21425 *p++ = '?';
21426 *p++ = '?';
21427 *p = '\0';
21428 return decode_mode_spec_buf;
21431 break;
21433 case 'm':
21434 obj = BVAR (b, mode_name);
21435 break;
21437 case 'n':
21438 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21439 return " Narrow";
21440 break;
21442 case 'p':
21444 ptrdiff_t pos = marker_position (w->start);
21445 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21447 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21449 if (pos <= BUF_BEGV (b))
21450 return "All";
21451 else
21452 return "Bottom";
21454 else if (pos <= BUF_BEGV (b))
21455 return "Top";
21456 else
21458 if (total > 1000000)
21459 /* Do it differently for a large value, to avoid overflow. */
21460 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21461 else
21462 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21463 /* We can't normally display a 3-digit number,
21464 so get us a 2-digit number that is close. */
21465 if (total == 100)
21466 total = 99;
21467 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21468 return decode_mode_spec_buf;
21472 /* Display percentage of size above the bottom of the screen. */
21473 case 'P':
21475 ptrdiff_t toppos = marker_position (w->start);
21476 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21477 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21479 if (botpos >= BUF_ZV (b))
21481 if (toppos <= BUF_BEGV (b))
21482 return "All";
21483 else
21484 return "Bottom";
21486 else
21488 if (total > 1000000)
21489 /* Do it differently for a large value, to avoid overflow. */
21490 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21491 else
21492 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21493 /* We can't normally display a 3-digit number,
21494 so get us a 2-digit number that is close. */
21495 if (total == 100)
21496 total = 99;
21497 if (toppos <= BUF_BEGV (b))
21498 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21499 else
21500 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21501 return decode_mode_spec_buf;
21505 case 's':
21506 /* status of process */
21507 obj = Fget_buffer_process (Fcurrent_buffer ());
21508 if (NILP (obj))
21509 return "no process";
21510 #ifndef MSDOS
21511 obj = Fsymbol_name (Fprocess_status (obj));
21512 #endif
21513 break;
21515 case '@':
21517 ptrdiff_t count = inhibit_garbage_collection ();
21518 Lisp_Object val = call1 (intern ("file-remote-p"),
21519 BVAR (current_buffer, directory));
21520 unbind_to (count, Qnil);
21522 if (NILP (val))
21523 return "-";
21524 else
21525 return "@";
21528 case 't': /* indicate TEXT or BINARY */
21529 return "T";
21531 case 'z':
21532 /* coding-system (not including end-of-line format) */
21533 case 'Z':
21534 /* coding-system (including end-of-line type) */
21536 int eol_flag = (c == 'Z');
21537 char *p = decode_mode_spec_buf;
21539 if (! FRAME_WINDOW_P (f))
21541 /* No need to mention EOL here--the terminal never needs
21542 to do EOL conversion. */
21543 p = decode_mode_spec_coding (CODING_ID_NAME
21544 (FRAME_KEYBOARD_CODING (f)->id),
21545 p, 0);
21546 p = decode_mode_spec_coding (CODING_ID_NAME
21547 (FRAME_TERMINAL_CODING (f)->id),
21548 p, 0);
21550 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21551 p, eol_flag);
21553 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21554 #ifdef subprocesses
21555 obj = Fget_buffer_process (Fcurrent_buffer ());
21556 if (PROCESSP (obj))
21558 p = decode_mode_spec_coding
21559 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21560 p = decode_mode_spec_coding
21561 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21563 #endif /* subprocesses */
21564 #endif /* 0 */
21565 *p = 0;
21566 return decode_mode_spec_buf;
21570 if (STRINGP (obj))
21572 *string = obj;
21573 return SSDATA (obj);
21575 else
21576 return "";
21580 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
21581 means count lines back from START_BYTE. But don't go beyond
21582 LIMIT_BYTE. Return the number of lines thus found (always
21583 nonnegative).
21585 Set *BYTE_POS_PTR to the byte position where we stopped. This is
21586 either the position COUNT lines after/before START_BYTE, if we
21587 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
21588 COUNT lines. */
21590 static ptrdiff_t
21591 display_count_lines (ptrdiff_t start_byte,
21592 ptrdiff_t limit_byte, ptrdiff_t count,
21593 ptrdiff_t *byte_pos_ptr)
21595 register unsigned char *cursor;
21596 unsigned char *base;
21598 register ptrdiff_t ceiling;
21599 register unsigned char *ceiling_addr;
21600 ptrdiff_t orig_count = count;
21602 /* If we are not in selective display mode,
21603 check only for newlines. */
21604 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21605 && !INTEGERP (BVAR (current_buffer, selective_display)));
21607 if (count > 0)
21609 while (start_byte < limit_byte)
21611 ceiling = BUFFER_CEILING_OF (start_byte);
21612 ceiling = min (limit_byte - 1, ceiling);
21613 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21614 base = (cursor = BYTE_POS_ADDR (start_byte));
21615 while (1)
21617 if (selective_display)
21618 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21620 else
21621 while (*cursor != '\n' && ++cursor != ceiling_addr)
21624 if (cursor != ceiling_addr)
21626 if (--count == 0)
21628 start_byte += cursor - base + 1;
21629 *byte_pos_ptr = start_byte;
21630 return orig_count;
21632 else
21633 if (++cursor == ceiling_addr)
21634 break;
21636 else
21637 break;
21639 start_byte += cursor - base;
21642 else
21644 while (start_byte > limit_byte)
21646 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21647 ceiling = max (limit_byte, ceiling);
21648 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21649 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21650 while (1)
21652 if (selective_display)
21653 while (--cursor != ceiling_addr
21654 && *cursor != '\n' && *cursor != 015)
21656 else
21657 while (--cursor != ceiling_addr && *cursor != '\n')
21660 if (cursor != ceiling_addr)
21662 if (++count == 0)
21664 start_byte += cursor - base + 1;
21665 *byte_pos_ptr = start_byte;
21666 /* When scanning backwards, we should
21667 not count the newline posterior to which we stop. */
21668 return - orig_count - 1;
21671 else
21672 break;
21674 /* Here we add 1 to compensate for the last decrement
21675 of CURSOR, which took it past the valid range. */
21676 start_byte += cursor - base + 1;
21680 *byte_pos_ptr = limit_byte;
21682 if (count < 0)
21683 return - orig_count + count;
21684 return orig_count - count;
21690 /***********************************************************************
21691 Displaying strings
21692 ***********************************************************************/
21694 /* Display a NUL-terminated string, starting with index START.
21696 If STRING is non-null, display that C string. Otherwise, the Lisp
21697 string LISP_STRING is displayed. There's a case that STRING is
21698 non-null and LISP_STRING is not nil. It means STRING is a string
21699 data of LISP_STRING. In that case, we display LISP_STRING while
21700 ignoring its text properties.
21702 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21703 FACE_STRING. Display STRING or LISP_STRING with the face at
21704 FACE_STRING_POS in FACE_STRING:
21706 Display the string in the environment given by IT, but use the
21707 standard display table, temporarily.
21709 FIELD_WIDTH is the minimum number of output glyphs to produce.
21710 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21711 with spaces. If STRING has more characters, more than FIELD_WIDTH
21712 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21714 PRECISION is the maximum number of characters to output from
21715 STRING. PRECISION < 0 means don't truncate the string.
21717 This is roughly equivalent to printf format specifiers:
21719 FIELD_WIDTH PRECISION PRINTF
21720 ----------------------------------------
21721 -1 -1 %s
21722 -1 10 %.10s
21723 10 -1 %10s
21724 20 10 %20.10s
21726 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21727 display them, and < 0 means obey the current buffer's value of
21728 enable_multibyte_characters.
21730 Value is the number of columns displayed. */
21732 static int
21733 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21734 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21735 int field_width, int precision, int max_x, int multibyte)
21737 int hpos_at_start = it->hpos;
21738 int saved_face_id = it->face_id;
21739 struct glyph_row *row = it->glyph_row;
21740 ptrdiff_t it_charpos;
21742 /* Initialize the iterator IT for iteration over STRING beginning
21743 with index START. */
21744 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21745 precision, field_width, multibyte);
21746 if (string && STRINGP (lisp_string))
21747 /* LISP_STRING is the one returned by decode_mode_spec. We should
21748 ignore its text properties. */
21749 it->stop_charpos = it->end_charpos;
21751 /* If displaying STRING, set up the face of the iterator from
21752 FACE_STRING, if that's given. */
21753 if (STRINGP (face_string))
21755 ptrdiff_t endptr;
21756 struct face *face;
21758 it->face_id
21759 = face_at_string_position (it->w, face_string, face_string_pos,
21760 0, it->region_beg_charpos,
21761 it->region_end_charpos,
21762 &endptr, it->base_face_id, 0);
21763 face = FACE_FROM_ID (it->f, it->face_id);
21764 it->face_box_p = face->box != FACE_NO_BOX;
21767 /* Set max_x to the maximum allowed X position. Don't let it go
21768 beyond the right edge of the window. */
21769 if (max_x <= 0)
21770 max_x = it->last_visible_x;
21771 else
21772 max_x = min (max_x, it->last_visible_x);
21774 /* Skip over display elements that are not visible. because IT->w is
21775 hscrolled. */
21776 if (it->current_x < it->first_visible_x)
21777 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21778 MOVE_TO_POS | MOVE_TO_X);
21780 row->ascent = it->max_ascent;
21781 row->height = it->max_ascent + it->max_descent;
21782 row->phys_ascent = it->max_phys_ascent;
21783 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21784 row->extra_line_spacing = it->max_extra_line_spacing;
21786 if (STRINGP (it->string))
21787 it_charpos = IT_STRING_CHARPOS (*it);
21788 else
21789 it_charpos = IT_CHARPOS (*it);
21791 /* This condition is for the case that we are called with current_x
21792 past last_visible_x. */
21793 while (it->current_x < max_x)
21795 int x_before, x, n_glyphs_before, i, nglyphs;
21797 /* Get the next display element. */
21798 if (!get_next_display_element (it))
21799 break;
21801 /* Produce glyphs. */
21802 x_before = it->current_x;
21803 n_glyphs_before = row->used[TEXT_AREA];
21804 PRODUCE_GLYPHS (it);
21806 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21807 i = 0;
21808 x = x_before;
21809 while (i < nglyphs)
21811 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21813 if (it->line_wrap != TRUNCATE
21814 && x + glyph->pixel_width > max_x)
21816 /* End of continued line or max_x reached. */
21817 if (CHAR_GLYPH_PADDING_P (*glyph))
21819 /* A wide character is unbreakable. */
21820 if (row->reversed_p)
21821 unproduce_glyphs (it, row->used[TEXT_AREA]
21822 - n_glyphs_before);
21823 row->used[TEXT_AREA] = n_glyphs_before;
21824 it->current_x = x_before;
21826 else
21828 if (row->reversed_p)
21829 unproduce_glyphs (it, row->used[TEXT_AREA]
21830 - (n_glyphs_before + i));
21831 row->used[TEXT_AREA] = n_glyphs_before + i;
21832 it->current_x = x;
21834 break;
21836 else if (x + glyph->pixel_width >= it->first_visible_x)
21838 /* Glyph is at least partially visible. */
21839 ++it->hpos;
21840 if (x < it->first_visible_x)
21841 row->x = x - it->first_visible_x;
21843 else
21845 /* Glyph is off the left margin of the display area.
21846 Should not happen. */
21847 emacs_abort ();
21850 row->ascent = max (row->ascent, it->max_ascent);
21851 row->height = max (row->height, it->max_ascent + it->max_descent);
21852 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21853 row->phys_height = max (row->phys_height,
21854 it->max_phys_ascent + it->max_phys_descent);
21855 row->extra_line_spacing = max (row->extra_line_spacing,
21856 it->max_extra_line_spacing);
21857 x += glyph->pixel_width;
21858 ++i;
21861 /* Stop if max_x reached. */
21862 if (i < nglyphs)
21863 break;
21865 /* Stop at line ends. */
21866 if (ITERATOR_AT_END_OF_LINE_P (it))
21868 it->continuation_lines_width = 0;
21869 break;
21872 set_iterator_to_next (it, 1);
21873 if (STRINGP (it->string))
21874 it_charpos = IT_STRING_CHARPOS (*it);
21875 else
21876 it_charpos = IT_CHARPOS (*it);
21878 /* Stop if truncating at the right edge. */
21879 if (it->line_wrap == TRUNCATE
21880 && it->current_x >= it->last_visible_x)
21882 /* Add truncation mark, but don't do it if the line is
21883 truncated at a padding space. */
21884 if (it_charpos < it->string_nchars)
21886 if (!FRAME_WINDOW_P (it->f))
21888 int ii, n;
21890 if (it->current_x > it->last_visible_x)
21892 if (!row->reversed_p)
21894 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21895 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21896 break;
21898 else
21900 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21901 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21902 break;
21903 unproduce_glyphs (it, ii + 1);
21904 ii = row->used[TEXT_AREA] - (ii + 1);
21906 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21908 row->used[TEXT_AREA] = ii;
21909 produce_special_glyphs (it, IT_TRUNCATION);
21912 produce_special_glyphs (it, IT_TRUNCATION);
21914 row->truncated_on_right_p = 1;
21916 break;
21920 /* Maybe insert a truncation at the left. */
21921 if (it->first_visible_x
21922 && it_charpos > 0)
21924 if (!FRAME_WINDOW_P (it->f)
21925 || (row->reversed_p
21926 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21927 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21928 insert_left_trunc_glyphs (it);
21929 row->truncated_on_left_p = 1;
21932 it->face_id = saved_face_id;
21934 /* Value is number of columns displayed. */
21935 return it->hpos - hpos_at_start;
21940 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21941 appears as an element of LIST or as the car of an element of LIST.
21942 If PROPVAL is a list, compare each element against LIST in that
21943 way, and return 1/2 if any element of PROPVAL is found in LIST.
21944 Otherwise return 0. This function cannot quit.
21945 The return value is 2 if the text is invisible but with an ellipsis
21946 and 1 if it's invisible and without an ellipsis. */
21949 invisible_p (register Lisp_Object propval, Lisp_Object list)
21951 register Lisp_Object tail, proptail;
21953 for (tail = list; CONSP (tail); tail = XCDR (tail))
21955 register Lisp_Object tem;
21956 tem = XCAR (tail);
21957 if (EQ (propval, tem))
21958 return 1;
21959 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21960 return NILP (XCDR (tem)) ? 1 : 2;
21963 if (CONSP (propval))
21965 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21967 Lisp_Object propelt;
21968 propelt = XCAR (proptail);
21969 for (tail = list; CONSP (tail); tail = XCDR (tail))
21971 register Lisp_Object tem;
21972 tem = XCAR (tail);
21973 if (EQ (propelt, tem))
21974 return 1;
21975 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21976 return NILP (XCDR (tem)) ? 1 : 2;
21981 return 0;
21984 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21985 doc: /* Non-nil if the property makes the text invisible.
21986 POS-OR-PROP can be a marker or number, in which case it is taken to be
21987 a position in the current buffer and the value of the `invisible' property
21988 is checked; or it can be some other value, which is then presumed to be the
21989 value of the `invisible' property of the text of interest.
21990 The non-nil value returned can be t for truly invisible text or something
21991 else if the text is replaced by an ellipsis. */)
21992 (Lisp_Object pos_or_prop)
21994 Lisp_Object prop
21995 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21996 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21997 : pos_or_prop);
21998 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21999 return (invis == 0 ? Qnil
22000 : invis == 1 ? Qt
22001 : make_number (invis));
22004 /* Calculate a width or height in pixels from a specification using
22005 the following elements:
22007 SPEC ::=
22008 NUM - a (fractional) multiple of the default font width/height
22009 (NUM) - specifies exactly NUM pixels
22010 UNIT - a fixed number of pixels, see below.
22011 ELEMENT - size of a display element in pixels, see below.
22012 (NUM . SPEC) - equals NUM * SPEC
22013 (+ SPEC SPEC ...) - add pixel values
22014 (- SPEC SPEC ...) - subtract pixel values
22015 (- SPEC) - negate pixel value
22017 NUM ::=
22018 INT or FLOAT - a number constant
22019 SYMBOL - use symbol's (buffer local) variable binding.
22021 UNIT ::=
22022 in - pixels per inch *)
22023 mm - pixels per 1/1000 meter *)
22024 cm - pixels per 1/100 meter *)
22025 width - width of current font in pixels.
22026 height - height of current font in pixels.
22028 *) using the ratio(s) defined in display-pixels-per-inch.
22030 ELEMENT ::=
22032 left-fringe - left fringe width in pixels
22033 right-fringe - right fringe width in pixels
22035 left-margin - left margin width in pixels
22036 right-margin - right margin width in pixels
22038 scroll-bar - scroll-bar area width in pixels
22040 Examples:
22042 Pixels corresponding to 5 inches:
22043 (5 . in)
22045 Total width of non-text areas on left side of window (if scroll-bar is on left):
22046 '(space :width (+ left-fringe left-margin scroll-bar))
22048 Align to first text column (in header line):
22049 '(space :align-to 0)
22051 Align to middle of text area minus half the width of variable `my-image'
22052 containing a loaded image:
22053 '(space :align-to (0.5 . (- text my-image)))
22055 Width of left margin minus width of 1 character in the default font:
22056 '(space :width (- left-margin 1))
22058 Width of left margin minus width of 2 characters in the current font:
22059 '(space :width (- left-margin (2 . width)))
22061 Center 1 character over left-margin (in header line):
22062 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22064 Different ways to express width of left fringe plus left margin minus one pixel:
22065 '(space :width (- (+ left-fringe left-margin) (1)))
22066 '(space :width (+ left-fringe left-margin (- (1))))
22067 '(space :width (+ left-fringe left-margin (-1)))
22071 #define NUMVAL(X) \
22072 ((INTEGERP (X) || FLOATP (X)) \
22073 ? XFLOATINT (X) \
22074 : - 1)
22076 static int
22077 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22078 struct font *font, int width_p, int *align_to)
22080 double pixels;
22082 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22083 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22085 if (NILP (prop))
22086 return OK_PIXELS (0);
22088 eassert (FRAME_LIVE_P (it->f));
22090 if (SYMBOLP (prop))
22092 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22094 char *unit = SSDATA (SYMBOL_NAME (prop));
22096 if (unit[0] == 'i' && unit[1] == 'n')
22097 pixels = 1.0;
22098 else if (unit[0] == 'm' && unit[1] == 'm')
22099 pixels = 25.4;
22100 else if (unit[0] == 'c' && unit[1] == 'm')
22101 pixels = 2.54;
22102 else
22103 pixels = 0;
22104 if (pixels > 0)
22106 double ppi;
22107 #ifdef HAVE_WINDOW_SYSTEM
22108 if (FRAME_WINDOW_P (it->f)
22109 && (ppi = (width_p
22110 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22111 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22112 ppi > 0))
22113 return OK_PIXELS (ppi / pixels);
22114 #endif
22116 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22117 || (CONSP (Vdisplay_pixels_per_inch)
22118 && (ppi = (width_p
22119 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22120 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22121 ppi > 0)))
22122 return OK_PIXELS (ppi / pixels);
22124 return 0;
22128 #ifdef HAVE_WINDOW_SYSTEM
22129 if (EQ (prop, Qheight))
22130 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22131 if (EQ (prop, Qwidth))
22132 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22133 #else
22134 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22135 return OK_PIXELS (1);
22136 #endif
22138 if (EQ (prop, Qtext))
22139 return OK_PIXELS (width_p
22140 ? window_box_width (it->w, TEXT_AREA)
22141 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22143 if (align_to && *align_to < 0)
22145 *res = 0;
22146 if (EQ (prop, Qleft))
22147 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22148 if (EQ (prop, Qright))
22149 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22150 if (EQ (prop, Qcenter))
22151 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22152 + window_box_width (it->w, TEXT_AREA) / 2);
22153 if (EQ (prop, Qleft_fringe))
22154 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22155 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22156 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22157 if (EQ (prop, Qright_fringe))
22158 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22159 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22160 : window_box_right_offset (it->w, TEXT_AREA));
22161 if (EQ (prop, Qleft_margin))
22162 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22163 if (EQ (prop, Qright_margin))
22164 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22165 if (EQ (prop, Qscroll_bar))
22166 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22168 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22169 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22170 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22171 : 0)));
22173 else
22175 if (EQ (prop, Qleft_fringe))
22176 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22177 if (EQ (prop, Qright_fringe))
22178 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22179 if (EQ (prop, Qleft_margin))
22180 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22181 if (EQ (prop, Qright_margin))
22182 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22183 if (EQ (prop, Qscroll_bar))
22184 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22187 prop = buffer_local_value_1 (prop, it->w->buffer);
22188 if (EQ (prop, Qunbound))
22189 prop = Qnil;
22192 if (INTEGERP (prop) || FLOATP (prop))
22194 int base_unit = (width_p
22195 ? FRAME_COLUMN_WIDTH (it->f)
22196 : FRAME_LINE_HEIGHT (it->f));
22197 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22200 if (CONSP (prop))
22202 Lisp_Object car = XCAR (prop);
22203 Lisp_Object cdr = XCDR (prop);
22205 if (SYMBOLP (car))
22207 #ifdef HAVE_WINDOW_SYSTEM
22208 if (FRAME_WINDOW_P (it->f)
22209 && valid_image_p (prop))
22211 ptrdiff_t id = lookup_image (it->f, prop);
22212 struct image *img = IMAGE_FROM_ID (it->f, id);
22214 return OK_PIXELS (width_p ? img->width : img->height);
22216 #endif
22217 if (EQ (car, Qplus) || EQ (car, Qminus))
22219 int first = 1;
22220 double px;
22222 pixels = 0;
22223 while (CONSP (cdr))
22225 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22226 font, width_p, align_to))
22227 return 0;
22228 if (first)
22229 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22230 else
22231 pixels += px;
22232 cdr = XCDR (cdr);
22234 if (EQ (car, Qminus))
22235 pixels = -pixels;
22236 return OK_PIXELS (pixels);
22239 car = buffer_local_value_1 (car, it->w->buffer);
22240 if (EQ (car, Qunbound))
22241 car = Qnil;
22244 if (INTEGERP (car) || FLOATP (car))
22246 double fact;
22247 pixels = XFLOATINT (car);
22248 if (NILP (cdr))
22249 return OK_PIXELS (pixels);
22250 if (calc_pixel_width_or_height (&fact, it, cdr,
22251 font, width_p, align_to))
22252 return OK_PIXELS (pixels * fact);
22253 return 0;
22256 return 0;
22259 return 0;
22263 /***********************************************************************
22264 Glyph Display
22265 ***********************************************************************/
22267 #ifdef HAVE_WINDOW_SYSTEM
22269 #ifdef GLYPH_DEBUG
22271 void
22272 dump_glyph_string (struct glyph_string *s)
22274 fprintf (stderr, "glyph string\n");
22275 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22276 s->x, s->y, s->width, s->height);
22277 fprintf (stderr, " ybase = %d\n", s->ybase);
22278 fprintf (stderr, " hl = %d\n", s->hl);
22279 fprintf (stderr, " left overhang = %d, right = %d\n",
22280 s->left_overhang, s->right_overhang);
22281 fprintf (stderr, " nchars = %d\n", s->nchars);
22282 fprintf (stderr, " extends to end of line = %d\n",
22283 s->extends_to_end_of_line_p);
22284 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22285 fprintf (stderr, " bg width = %d\n", s->background_width);
22288 #endif /* GLYPH_DEBUG */
22290 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22291 of XChar2b structures for S; it can't be allocated in
22292 init_glyph_string because it must be allocated via `alloca'. W
22293 is the window on which S is drawn. ROW and AREA are the glyph row
22294 and area within the row from which S is constructed. START is the
22295 index of the first glyph structure covered by S. HL is a
22296 face-override for drawing S. */
22298 #ifdef HAVE_NTGUI
22299 #define OPTIONAL_HDC(hdc) HDC hdc,
22300 #define DECLARE_HDC(hdc) HDC hdc;
22301 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22302 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22303 #endif
22305 #ifndef OPTIONAL_HDC
22306 #define OPTIONAL_HDC(hdc)
22307 #define DECLARE_HDC(hdc)
22308 #define ALLOCATE_HDC(hdc, f)
22309 #define RELEASE_HDC(hdc, f)
22310 #endif
22312 static void
22313 init_glyph_string (struct glyph_string *s,
22314 OPTIONAL_HDC (hdc)
22315 XChar2b *char2b, struct window *w, struct glyph_row *row,
22316 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22318 memset (s, 0, sizeof *s);
22319 s->w = w;
22320 s->f = XFRAME (w->frame);
22321 #ifdef HAVE_NTGUI
22322 s->hdc = hdc;
22323 #endif
22324 s->display = FRAME_X_DISPLAY (s->f);
22325 s->window = FRAME_X_WINDOW (s->f);
22326 s->char2b = char2b;
22327 s->hl = hl;
22328 s->row = row;
22329 s->area = area;
22330 s->first_glyph = row->glyphs[area] + start;
22331 s->height = row->height;
22332 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22333 s->ybase = s->y + row->ascent;
22337 /* Append the list of glyph strings with head H and tail T to the list
22338 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22340 static void
22341 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22342 struct glyph_string *h, struct glyph_string *t)
22344 if (h)
22346 if (*head)
22347 (*tail)->next = h;
22348 else
22349 *head = h;
22350 h->prev = *tail;
22351 *tail = t;
22356 /* Prepend the list of glyph strings with head H and tail T to the
22357 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22358 result. */
22360 static void
22361 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22362 struct glyph_string *h, struct glyph_string *t)
22364 if (h)
22366 if (*head)
22367 (*head)->prev = t;
22368 else
22369 *tail = t;
22370 t->next = *head;
22371 *head = h;
22376 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22377 Set *HEAD and *TAIL to the resulting list. */
22379 static void
22380 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22381 struct glyph_string *s)
22383 s->next = s->prev = NULL;
22384 append_glyph_string_lists (head, tail, s, s);
22388 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22389 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22390 make sure that X resources for the face returned are allocated.
22391 Value is a pointer to a realized face that is ready for display if
22392 DISPLAY_P is non-zero. */
22394 static struct face *
22395 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22396 XChar2b *char2b, int display_p)
22398 struct face *face = FACE_FROM_ID (f, face_id);
22400 if (face->font)
22402 unsigned code = face->font->driver->encode_char (face->font, c);
22404 if (code != FONT_INVALID_CODE)
22405 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22406 else
22407 STORE_XCHAR2B (char2b, 0, 0);
22410 /* Make sure X resources of the face are allocated. */
22411 #ifdef HAVE_X_WINDOWS
22412 if (display_p)
22413 #endif
22415 eassert (face != NULL);
22416 PREPARE_FACE_FOR_DISPLAY (f, face);
22419 return face;
22423 /* Get face and two-byte form of character glyph GLYPH on frame F.
22424 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22425 a pointer to a realized face that is ready for display. */
22427 static struct face *
22428 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22429 XChar2b *char2b, int *two_byte_p)
22431 struct face *face;
22433 eassert (glyph->type == CHAR_GLYPH);
22434 face = FACE_FROM_ID (f, glyph->face_id);
22436 if (two_byte_p)
22437 *two_byte_p = 0;
22439 if (face->font)
22441 unsigned code;
22443 if (CHAR_BYTE8_P (glyph->u.ch))
22444 code = CHAR_TO_BYTE8 (glyph->u.ch);
22445 else
22446 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22448 if (code != FONT_INVALID_CODE)
22449 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22450 else
22451 STORE_XCHAR2B (char2b, 0, 0);
22454 /* Make sure X resources of the face are allocated. */
22455 eassert (face != NULL);
22456 PREPARE_FACE_FOR_DISPLAY (f, face);
22457 return face;
22461 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22462 Return 1 if FONT has a glyph for C, otherwise return 0. */
22464 static int
22465 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22467 unsigned code;
22469 if (CHAR_BYTE8_P (c))
22470 code = CHAR_TO_BYTE8 (c);
22471 else
22472 code = font->driver->encode_char (font, c);
22474 if (code == FONT_INVALID_CODE)
22475 return 0;
22476 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22477 return 1;
22481 /* Fill glyph string S with composition components specified by S->cmp.
22483 BASE_FACE is the base face of the composition.
22484 S->cmp_from is the index of the first component for S.
22486 OVERLAPS non-zero means S should draw the foreground only, and use
22487 its physical height for clipping. See also draw_glyphs.
22489 Value is the index of a component not in S. */
22491 static int
22492 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22493 int overlaps)
22495 int i;
22496 /* For all glyphs of this composition, starting at the offset
22497 S->cmp_from, until we reach the end of the definition or encounter a
22498 glyph that requires the different face, add it to S. */
22499 struct face *face;
22501 eassert (s);
22503 s->for_overlaps = overlaps;
22504 s->face = NULL;
22505 s->font = NULL;
22506 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22508 int c = COMPOSITION_GLYPH (s->cmp, i);
22510 /* TAB in a composition means display glyphs with padding space
22511 on the left or right. */
22512 if (c != '\t')
22514 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22515 -1, Qnil);
22517 face = get_char_face_and_encoding (s->f, c, face_id,
22518 s->char2b + i, 1);
22519 if (face)
22521 if (! s->face)
22523 s->face = face;
22524 s->font = s->face->font;
22526 else if (s->face != face)
22527 break;
22530 ++s->nchars;
22532 s->cmp_to = i;
22534 if (s->face == NULL)
22536 s->face = base_face->ascii_face;
22537 s->font = s->face->font;
22540 /* All glyph strings for the same composition has the same width,
22541 i.e. the width set for the first component of the composition. */
22542 s->width = s->first_glyph->pixel_width;
22544 /* If the specified font could not be loaded, use the frame's
22545 default font, but record the fact that we couldn't load it in
22546 the glyph string so that we can draw rectangles for the
22547 characters of the glyph string. */
22548 if (s->font == NULL)
22550 s->font_not_found_p = 1;
22551 s->font = FRAME_FONT (s->f);
22554 /* Adjust base line for subscript/superscript text. */
22555 s->ybase += s->first_glyph->voffset;
22557 /* This glyph string must always be drawn with 16-bit functions. */
22558 s->two_byte_p = 1;
22560 return s->cmp_to;
22563 static int
22564 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22565 int start, int end, int overlaps)
22567 struct glyph *glyph, *last;
22568 Lisp_Object lgstring;
22569 int i;
22571 s->for_overlaps = overlaps;
22572 glyph = s->row->glyphs[s->area] + start;
22573 last = s->row->glyphs[s->area] + end;
22574 s->cmp_id = glyph->u.cmp.id;
22575 s->cmp_from = glyph->slice.cmp.from;
22576 s->cmp_to = glyph->slice.cmp.to + 1;
22577 s->face = FACE_FROM_ID (s->f, face_id);
22578 lgstring = composition_gstring_from_id (s->cmp_id);
22579 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22580 glyph++;
22581 while (glyph < last
22582 && glyph->u.cmp.automatic
22583 && glyph->u.cmp.id == s->cmp_id
22584 && s->cmp_to == glyph->slice.cmp.from)
22585 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22587 for (i = s->cmp_from; i < s->cmp_to; i++)
22589 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22590 unsigned code = LGLYPH_CODE (lglyph);
22592 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22594 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22595 return glyph - s->row->glyphs[s->area];
22599 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22600 See the comment of fill_glyph_string for arguments.
22601 Value is the index of the first glyph not in S. */
22604 static int
22605 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22606 int start, int end, int overlaps)
22608 struct glyph *glyph, *last;
22609 int voffset;
22611 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22612 s->for_overlaps = overlaps;
22613 glyph = s->row->glyphs[s->area] + start;
22614 last = s->row->glyphs[s->area] + end;
22615 voffset = glyph->voffset;
22616 s->face = FACE_FROM_ID (s->f, face_id);
22617 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22618 s->nchars = 1;
22619 s->width = glyph->pixel_width;
22620 glyph++;
22621 while (glyph < last
22622 && glyph->type == GLYPHLESS_GLYPH
22623 && glyph->voffset == voffset
22624 && glyph->face_id == face_id)
22626 s->nchars++;
22627 s->width += glyph->pixel_width;
22628 glyph++;
22630 s->ybase += voffset;
22631 return glyph - s->row->glyphs[s->area];
22635 /* Fill glyph string S from a sequence of character glyphs.
22637 FACE_ID is the face id of the string. START is the index of the
22638 first glyph to consider, END is the index of the last + 1.
22639 OVERLAPS non-zero means S should draw the foreground only, and use
22640 its physical height for clipping. See also draw_glyphs.
22642 Value is the index of the first glyph not in S. */
22644 static int
22645 fill_glyph_string (struct glyph_string *s, int face_id,
22646 int start, int end, int overlaps)
22648 struct glyph *glyph, *last;
22649 int voffset;
22650 int glyph_not_available_p;
22652 eassert (s->f == XFRAME (s->w->frame));
22653 eassert (s->nchars == 0);
22654 eassert (start >= 0 && end > start);
22656 s->for_overlaps = overlaps;
22657 glyph = s->row->glyphs[s->area] + start;
22658 last = s->row->glyphs[s->area] + end;
22659 voffset = glyph->voffset;
22660 s->padding_p = glyph->padding_p;
22661 glyph_not_available_p = glyph->glyph_not_available_p;
22663 while (glyph < last
22664 && glyph->type == CHAR_GLYPH
22665 && glyph->voffset == voffset
22666 /* Same face id implies same font, nowadays. */
22667 && glyph->face_id == face_id
22668 && glyph->glyph_not_available_p == glyph_not_available_p)
22670 int two_byte_p;
22672 s->face = get_glyph_face_and_encoding (s->f, glyph,
22673 s->char2b + s->nchars,
22674 &two_byte_p);
22675 s->two_byte_p = two_byte_p;
22676 ++s->nchars;
22677 eassert (s->nchars <= end - start);
22678 s->width += glyph->pixel_width;
22679 if (glyph++->padding_p != s->padding_p)
22680 break;
22683 s->font = s->face->font;
22685 /* If the specified font could not be loaded, use the frame's font,
22686 but record the fact that we couldn't load it in
22687 S->font_not_found_p so that we can draw rectangles for the
22688 characters of the glyph string. */
22689 if (s->font == NULL || glyph_not_available_p)
22691 s->font_not_found_p = 1;
22692 s->font = FRAME_FONT (s->f);
22695 /* Adjust base line for subscript/superscript text. */
22696 s->ybase += voffset;
22698 eassert (s->face && s->face->gc);
22699 return glyph - s->row->glyphs[s->area];
22703 /* Fill glyph string S from image glyph S->first_glyph. */
22705 static void
22706 fill_image_glyph_string (struct glyph_string *s)
22708 eassert (s->first_glyph->type == IMAGE_GLYPH);
22709 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22710 eassert (s->img);
22711 s->slice = s->first_glyph->slice.img;
22712 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22713 s->font = s->face->font;
22714 s->width = s->first_glyph->pixel_width;
22716 /* Adjust base line for subscript/superscript text. */
22717 s->ybase += s->first_glyph->voffset;
22721 /* Fill glyph string S from a sequence of stretch glyphs.
22723 START is the index of the first glyph to consider,
22724 END is the index of the last + 1.
22726 Value is the index of the first glyph not in S. */
22728 static int
22729 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22731 struct glyph *glyph, *last;
22732 int voffset, face_id;
22734 eassert (s->first_glyph->type == STRETCH_GLYPH);
22736 glyph = s->row->glyphs[s->area] + start;
22737 last = s->row->glyphs[s->area] + end;
22738 face_id = glyph->face_id;
22739 s->face = FACE_FROM_ID (s->f, face_id);
22740 s->font = s->face->font;
22741 s->width = glyph->pixel_width;
22742 s->nchars = 1;
22743 voffset = glyph->voffset;
22745 for (++glyph;
22746 (glyph < last
22747 && glyph->type == STRETCH_GLYPH
22748 && glyph->voffset == voffset
22749 && glyph->face_id == face_id);
22750 ++glyph)
22751 s->width += glyph->pixel_width;
22753 /* Adjust base line for subscript/superscript text. */
22754 s->ybase += voffset;
22756 /* The case that face->gc == 0 is handled when drawing the glyph
22757 string by calling PREPARE_FACE_FOR_DISPLAY. */
22758 eassert (s->face);
22759 return glyph - s->row->glyphs[s->area];
22762 static struct font_metrics *
22763 get_per_char_metric (struct font *font, XChar2b *char2b)
22765 static struct font_metrics metrics;
22766 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22768 if (! font || code == FONT_INVALID_CODE)
22769 return NULL;
22770 font->driver->text_extents (font, &code, 1, &metrics);
22771 return &metrics;
22774 /* EXPORT for RIF:
22775 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22776 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22777 assumed to be zero. */
22779 void
22780 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22782 *left = *right = 0;
22784 if (glyph->type == CHAR_GLYPH)
22786 struct face *face;
22787 XChar2b char2b;
22788 struct font_metrics *pcm;
22790 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22791 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22793 if (pcm->rbearing > pcm->width)
22794 *right = pcm->rbearing - pcm->width;
22795 if (pcm->lbearing < 0)
22796 *left = -pcm->lbearing;
22799 else if (glyph->type == COMPOSITE_GLYPH)
22801 if (! glyph->u.cmp.automatic)
22803 struct composition *cmp = composition_table[glyph->u.cmp.id];
22805 if (cmp->rbearing > cmp->pixel_width)
22806 *right = cmp->rbearing - cmp->pixel_width;
22807 if (cmp->lbearing < 0)
22808 *left = - cmp->lbearing;
22810 else
22812 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22813 struct font_metrics metrics;
22815 composition_gstring_width (gstring, glyph->slice.cmp.from,
22816 glyph->slice.cmp.to + 1, &metrics);
22817 if (metrics.rbearing > metrics.width)
22818 *right = metrics.rbearing - metrics.width;
22819 if (metrics.lbearing < 0)
22820 *left = - metrics.lbearing;
22826 /* Return the index of the first glyph preceding glyph string S that
22827 is overwritten by S because of S's left overhang. Value is -1
22828 if no glyphs are overwritten. */
22830 static int
22831 left_overwritten (struct glyph_string *s)
22833 int k;
22835 if (s->left_overhang)
22837 int x = 0, i;
22838 struct glyph *glyphs = s->row->glyphs[s->area];
22839 int first = s->first_glyph - glyphs;
22841 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22842 x -= glyphs[i].pixel_width;
22844 k = i + 1;
22846 else
22847 k = -1;
22849 return k;
22853 /* Return the index of the first glyph preceding glyph string S that
22854 is overwriting S because of its right overhang. Value is -1 if no
22855 glyph in front of S overwrites S. */
22857 static int
22858 left_overwriting (struct glyph_string *s)
22860 int i, k, x;
22861 struct glyph *glyphs = s->row->glyphs[s->area];
22862 int first = s->first_glyph - glyphs;
22864 k = -1;
22865 x = 0;
22866 for (i = first - 1; i >= 0; --i)
22868 int left, right;
22869 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22870 if (x + right > 0)
22871 k = i;
22872 x -= glyphs[i].pixel_width;
22875 return k;
22879 /* Return the index of the last glyph following glyph string S that is
22880 overwritten by S because of S's right overhang. Value is -1 if
22881 no such glyph is found. */
22883 static int
22884 right_overwritten (struct glyph_string *s)
22886 int k = -1;
22888 if (s->right_overhang)
22890 int x = 0, i;
22891 struct glyph *glyphs = s->row->glyphs[s->area];
22892 int first = (s->first_glyph - glyphs
22893 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22894 int end = s->row->used[s->area];
22896 for (i = first; i < end && s->right_overhang > x; ++i)
22897 x += glyphs[i].pixel_width;
22899 k = i;
22902 return k;
22906 /* Return the index of the last glyph following glyph string S that
22907 overwrites S because of its left overhang. Value is negative
22908 if no such glyph is found. */
22910 static int
22911 right_overwriting (struct glyph_string *s)
22913 int i, k, x;
22914 int end = s->row->used[s->area];
22915 struct glyph *glyphs = s->row->glyphs[s->area];
22916 int first = (s->first_glyph - glyphs
22917 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22919 k = -1;
22920 x = 0;
22921 for (i = first; i < end; ++i)
22923 int left, right;
22924 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22925 if (x - left < 0)
22926 k = i;
22927 x += glyphs[i].pixel_width;
22930 return k;
22934 /* Set background width of glyph string S. START is the index of the
22935 first glyph following S. LAST_X is the right-most x-position + 1
22936 in the drawing area. */
22938 static void
22939 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22941 /* If the face of this glyph string has to be drawn to the end of
22942 the drawing area, set S->extends_to_end_of_line_p. */
22944 if (start == s->row->used[s->area]
22945 && s->area == TEXT_AREA
22946 && ((s->row->fill_line_p
22947 && (s->hl == DRAW_NORMAL_TEXT
22948 || s->hl == DRAW_IMAGE_RAISED
22949 || s->hl == DRAW_IMAGE_SUNKEN))
22950 || s->hl == DRAW_MOUSE_FACE))
22951 s->extends_to_end_of_line_p = 1;
22953 /* If S extends its face to the end of the line, set its
22954 background_width to the distance to the right edge of the drawing
22955 area. */
22956 if (s->extends_to_end_of_line_p)
22957 s->background_width = last_x - s->x + 1;
22958 else
22959 s->background_width = s->width;
22963 /* Compute overhangs and x-positions for glyph string S and its
22964 predecessors, or successors. X is the starting x-position for S.
22965 BACKWARD_P non-zero means process predecessors. */
22967 static void
22968 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22970 if (backward_p)
22972 while (s)
22974 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22975 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22976 x -= s->width;
22977 s->x = x;
22978 s = s->prev;
22981 else
22983 while (s)
22985 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22986 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22987 s->x = x;
22988 x += s->width;
22989 s = s->next;
22996 /* The following macros are only called from draw_glyphs below.
22997 They reference the following parameters of that function directly:
22998 `w', `row', `area', and `overlap_p'
22999 as well as the following local variables:
23000 `s', `f', and `hdc' (in W32) */
23002 #ifdef HAVE_NTGUI
23003 /* On W32, silently add local `hdc' variable to argument list of
23004 init_glyph_string. */
23005 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23006 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23007 #else
23008 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23009 init_glyph_string (s, char2b, w, row, area, start, hl)
23010 #endif
23012 /* Add a glyph string for a stretch glyph to the list of strings
23013 between HEAD and TAIL. START is the index of the stretch glyph in
23014 row area AREA of glyph row ROW. END is the index of the last glyph
23015 in that glyph row area. X is the current output position assigned
23016 to the new glyph string constructed. HL overrides that face of the
23017 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23018 is the right-most x-position of the drawing area. */
23020 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23021 and below -- keep them on one line. */
23022 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23023 do \
23025 s = alloca (sizeof *s); \
23026 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23027 START = fill_stretch_glyph_string (s, START, END); \
23028 append_glyph_string (&HEAD, &TAIL, s); \
23029 s->x = (X); \
23031 while (0)
23034 /* Add a glyph string for an image glyph to the list of strings
23035 between HEAD and TAIL. START is the index of the image glyph in
23036 row area AREA of glyph row ROW. END is the index of the last glyph
23037 in that glyph row area. X is the current output position assigned
23038 to the new glyph string constructed. HL overrides that face of the
23039 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23040 is the right-most x-position of the drawing area. */
23042 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23043 do \
23045 s = alloca (sizeof *s); \
23046 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23047 fill_image_glyph_string (s); \
23048 append_glyph_string (&HEAD, &TAIL, s); \
23049 ++START; \
23050 s->x = (X); \
23052 while (0)
23055 /* Add a glyph string for a sequence of character glyphs to the list
23056 of strings between HEAD and TAIL. START is the index of the first
23057 glyph in row area AREA of glyph row ROW that is part of the new
23058 glyph string. END is the index of the last glyph in that glyph row
23059 area. X is the current output position assigned to the new glyph
23060 string constructed. HL overrides that face of the glyph; e.g. it
23061 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23062 right-most x-position of the drawing area. */
23064 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23065 do \
23067 int face_id; \
23068 XChar2b *char2b; \
23070 face_id = (row)->glyphs[area][START].face_id; \
23072 s = alloca (sizeof *s); \
23073 char2b = alloca ((END - START) * sizeof *char2b); \
23074 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23075 append_glyph_string (&HEAD, &TAIL, s); \
23076 s->x = (X); \
23077 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23079 while (0)
23082 /* Add a glyph string for a composite sequence to the list of strings
23083 between HEAD and TAIL. START is the index of the first glyph in
23084 row area AREA of glyph row ROW that is part of the new glyph
23085 string. END is the index of the last glyph in that glyph row area.
23086 X is the current output position assigned to the new glyph string
23087 constructed. HL overrides that face of the glyph; e.g. it is
23088 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23089 x-position of the drawing area. */
23091 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23092 do { \
23093 int face_id = (row)->glyphs[area][START].face_id; \
23094 struct face *base_face = FACE_FROM_ID (f, face_id); \
23095 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23096 struct composition *cmp = composition_table[cmp_id]; \
23097 XChar2b *char2b; \
23098 struct glyph_string *first_s = NULL; \
23099 int n; \
23101 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23103 /* Make glyph_strings for each glyph sequence that is drawable by \
23104 the same face, and append them to HEAD/TAIL. */ \
23105 for (n = 0; n < cmp->glyph_len;) \
23107 s = alloca (sizeof *s); \
23108 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23109 append_glyph_string (&(HEAD), &(TAIL), s); \
23110 s->cmp = cmp; \
23111 s->cmp_from = n; \
23112 s->x = (X); \
23113 if (n == 0) \
23114 first_s = s; \
23115 n = fill_composite_glyph_string (s, base_face, overlaps); \
23118 ++START; \
23119 s = first_s; \
23120 } while (0)
23123 /* Add a glyph string for a glyph-string sequence to the list of strings
23124 between HEAD and TAIL. */
23126 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23127 do { \
23128 int face_id; \
23129 XChar2b *char2b; \
23130 Lisp_Object gstring; \
23132 face_id = (row)->glyphs[area][START].face_id; \
23133 gstring = (composition_gstring_from_id \
23134 ((row)->glyphs[area][START].u.cmp.id)); \
23135 s = alloca (sizeof *s); \
23136 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23137 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23138 append_glyph_string (&(HEAD), &(TAIL), s); \
23139 s->x = (X); \
23140 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23141 } while (0)
23144 /* Add a glyph string for a sequence of glyphless character's glyphs
23145 to the list of strings between HEAD and TAIL. The meanings of
23146 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23148 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23149 do \
23151 int face_id; \
23153 face_id = (row)->glyphs[area][START].face_id; \
23155 s = alloca (sizeof *s); \
23156 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23157 append_glyph_string (&HEAD, &TAIL, s); \
23158 s->x = (X); \
23159 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23160 overlaps); \
23162 while (0)
23165 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23166 of AREA of glyph row ROW on window W between indices START and END.
23167 HL overrides the face for drawing glyph strings, e.g. it is
23168 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23169 x-positions of the drawing area.
23171 This is an ugly monster macro construct because we must use alloca
23172 to allocate glyph strings (because draw_glyphs can be called
23173 asynchronously). */
23175 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23176 do \
23178 HEAD = TAIL = NULL; \
23179 while (START < END) \
23181 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23182 switch (first_glyph->type) \
23184 case CHAR_GLYPH: \
23185 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23186 HL, X, LAST_X); \
23187 break; \
23189 case COMPOSITE_GLYPH: \
23190 if (first_glyph->u.cmp.automatic) \
23191 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23192 HL, X, LAST_X); \
23193 else \
23194 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23195 HL, X, LAST_X); \
23196 break; \
23198 case STRETCH_GLYPH: \
23199 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23200 HL, X, LAST_X); \
23201 break; \
23203 case IMAGE_GLYPH: \
23204 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23205 HL, X, LAST_X); \
23206 break; \
23208 case GLYPHLESS_GLYPH: \
23209 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23210 HL, X, LAST_X); \
23211 break; \
23213 default: \
23214 emacs_abort (); \
23217 if (s) \
23219 set_glyph_string_background_width (s, START, LAST_X); \
23220 (X) += s->width; \
23223 } while (0)
23226 /* Draw glyphs between START and END in AREA of ROW on window W,
23227 starting at x-position X. X is relative to AREA in W. HL is a
23228 face-override with the following meaning:
23230 DRAW_NORMAL_TEXT draw normally
23231 DRAW_CURSOR draw in cursor face
23232 DRAW_MOUSE_FACE draw in mouse face.
23233 DRAW_INVERSE_VIDEO draw in mode line face
23234 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23235 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23237 If OVERLAPS is non-zero, draw only the foreground of characters and
23238 clip to the physical height of ROW. Non-zero value also defines
23239 the overlapping part to be drawn:
23241 OVERLAPS_PRED overlap with preceding rows
23242 OVERLAPS_SUCC overlap with succeeding rows
23243 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23244 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23246 Value is the x-position reached, relative to AREA of W. */
23248 static int
23249 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23250 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23251 enum draw_glyphs_face hl, int overlaps)
23253 struct glyph_string *head, *tail;
23254 struct glyph_string *s;
23255 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23256 int i, j, x_reached, last_x, area_left = 0;
23257 struct frame *f = XFRAME (WINDOW_FRAME (w));
23258 DECLARE_HDC (hdc);
23260 ALLOCATE_HDC (hdc, f);
23262 /* Let's rather be paranoid than getting a SEGV. */
23263 end = min (end, row->used[area]);
23264 start = clip_to_bounds (0, start, end);
23266 /* Translate X to frame coordinates. Set last_x to the right
23267 end of the drawing area. */
23268 if (row->full_width_p)
23270 /* X is relative to the left edge of W, without scroll bars
23271 or fringes. */
23272 area_left = WINDOW_LEFT_EDGE_X (w);
23273 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23275 else
23277 area_left = window_box_left (w, area);
23278 last_x = area_left + window_box_width (w, area);
23280 x += area_left;
23282 /* Build a doubly-linked list of glyph_string structures between
23283 head and tail from what we have to draw. Note that the macro
23284 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23285 the reason we use a separate variable `i'. */
23286 i = start;
23287 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23288 if (tail)
23289 x_reached = tail->x + tail->background_width;
23290 else
23291 x_reached = x;
23293 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23294 the row, redraw some glyphs in front or following the glyph
23295 strings built above. */
23296 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23298 struct glyph_string *h, *t;
23299 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23300 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23301 int check_mouse_face = 0;
23302 int dummy_x = 0;
23304 /* If mouse highlighting is on, we may need to draw adjacent
23305 glyphs using mouse-face highlighting. */
23306 if (area == TEXT_AREA && row->mouse_face_p
23307 && hlinfo->mouse_face_beg_row >= 0
23308 && hlinfo->mouse_face_end_row >= 0)
23310 struct glyph_row *mouse_beg_row, *mouse_end_row;
23312 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23313 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23315 if (row >= mouse_beg_row && row <= mouse_end_row)
23317 check_mouse_face = 1;
23318 mouse_beg_col = (row == mouse_beg_row)
23319 ? hlinfo->mouse_face_beg_col : 0;
23320 mouse_end_col = (row == mouse_end_row)
23321 ? hlinfo->mouse_face_end_col
23322 : row->used[TEXT_AREA];
23326 /* Compute overhangs for all glyph strings. */
23327 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23328 for (s = head; s; s = s->next)
23329 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23331 /* Prepend glyph strings for glyphs in front of the first glyph
23332 string that are overwritten because of the first glyph
23333 string's left overhang. The background of all strings
23334 prepended must be drawn because the first glyph string
23335 draws over it. */
23336 i = left_overwritten (head);
23337 if (i >= 0)
23339 enum draw_glyphs_face overlap_hl;
23341 /* If this row contains mouse highlighting, attempt to draw
23342 the overlapped glyphs with the correct highlight. This
23343 code fails if the overlap encompasses more than one glyph
23344 and mouse-highlight spans only some of these glyphs.
23345 However, making it work perfectly involves a lot more
23346 code, and I don't know if the pathological case occurs in
23347 practice, so we'll stick to this for now. --- cyd */
23348 if (check_mouse_face
23349 && mouse_beg_col < start && mouse_end_col > i)
23350 overlap_hl = DRAW_MOUSE_FACE;
23351 else
23352 overlap_hl = DRAW_NORMAL_TEXT;
23354 j = i;
23355 BUILD_GLYPH_STRINGS (j, start, h, t,
23356 overlap_hl, dummy_x, last_x);
23357 start = i;
23358 compute_overhangs_and_x (t, head->x, 1);
23359 prepend_glyph_string_lists (&head, &tail, h, t);
23360 clip_head = head;
23363 /* Prepend glyph strings for glyphs in front of the first glyph
23364 string that overwrite that glyph string because of their
23365 right overhang. For these strings, only the foreground must
23366 be drawn, because it draws over the glyph string at `head'.
23367 The background must not be drawn because this would overwrite
23368 right overhangs of preceding glyphs for which no glyph
23369 strings exist. */
23370 i = left_overwriting (head);
23371 if (i >= 0)
23373 enum draw_glyphs_face overlap_hl;
23375 if (check_mouse_face
23376 && mouse_beg_col < start && mouse_end_col > i)
23377 overlap_hl = DRAW_MOUSE_FACE;
23378 else
23379 overlap_hl = DRAW_NORMAL_TEXT;
23381 clip_head = head;
23382 BUILD_GLYPH_STRINGS (i, start, h, t,
23383 overlap_hl, dummy_x, last_x);
23384 for (s = h; s; s = s->next)
23385 s->background_filled_p = 1;
23386 compute_overhangs_and_x (t, head->x, 1);
23387 prepend_glyph_string_lists (&head, &tail, h, t);
23390 /* Append glyphs strings for glyphs following the last glyph
23391 string tail that are overwritten by tail. The background of
23392 these strings has to be drawn because tail's foreground draws
23393 over it. */
23394 i = right_overwritten (tail);
23395 if (i >= 0)
23397 enum draw_glyphs_face overlap_hl;
23399 if (check_mouse_face
23400 && mouse_beg_col < i && mouse_end_col > end)
23401 overlap_hl = DRAW_MOUSE_FACE;
23402 else
23403 overlap_hl = DRAW_NORMAL_TEXT;
23405 BUILD_GLYPH_STRINGS (end, i, h, t,
23406 overlap_hl, x, last_x);
23407 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23408 we don't have `end = i;' here. */
23409 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23410 append_glyph_string_lists (&head, &tail, h, t);
23411 clip_tail = tail;
23414 /* Append glyph strings for glyphs following the last glyph
23415 string tail that overwrite tail. The foreground of such
23416 glyphs has to be drawn because it writes into the background
23417 of tail. The background must not be drawn because it could
23418 paint over the foreground of following glyphs. */
23419 i = right_overwriting (tail);
23420 if (i >= 0)
23422 enum draw_glyphs_face overlap_hl;
23423 if (check_mouse_face
23424 && mouse_beg_col < i && mouse_end_col > end)
23425 overlap_hl = DRAW_MOUSE_FACE;
23426 else
23427 overlap_hl = DRAW_NORMAL_TEXT;
23429 clip_tail = tail;
23430 i++; /* We must include the Ith glyph. */
23431 BUILD_GLYPH_STRINGS (end, i, h, t,
23432 overlap_hl, x, last_x);
23433 for (s = h; s; s = s->next)
23434 s->background_filled_p = 1;
23435 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23436 append_glyph_string_lists (&head, &tail, h, t);
23438 if (clip_head || clip_tail)
23439 for (s = head; s; s = s->next)
23441 s->clip_head = clip_head;
23442 s->clip_tail = clip_tail;
23446 /* Draw all strings. */
23447 for (s = head; s; s = s->next)
23448 FRAME_RIF (f)->draw_glyph_string (s);
23450 #ifndef HAVE_NS
23451 /* When focus a sole frame and move horizontally, this sets on_p to 0
23452 causing a failure to erase prev cursor position. */
23453 if (area == TEXT_AREA
23454 && !row->full_width_p
23455 /* When drawing overlapping rows, only the glyph strings'
23456 foreground is drawn, which doesn't erase a cursor
23457 completely. */
23458 && !overlaps)
23460 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23461 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23462 : (tail ? tail->x + tail->background_width : x));
23463 x0 -= area_left;
23464 x1 -= area_left;
23466 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23467 row->y, MATRIX_ROW_BOTTOM_Y (row));
23469 #endif
23471 /* Value is the x-position up to which drawn, relative to AREA of W.
23472 This doesn't include parts drawn because of overhangs. */
23473 if (row->full_width_p)
23474 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23475 else
23476 x_reached -= area_left;
23478 RELEASE_HDC (hdc, f);
23480 return x_reached;
23483 /* Expand row matrix if too narrow. Don't expand if area
23484 is not present. */
23486 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23488 if (!fonts_changed_p \
23489 && (it->glyph_row->glyphs[area] \
23490 < it->glyph_row->glyphs[area + 1])) \
23492 it->w->ncols_scale_factor++; \
23493 fonts_changed_p = 1; \
23497 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23498 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23500 static void
23501 append_glyph (struct it *it)
23503 struct glyph *glyph;
23504 enum glyph_row_area area = it->area;
23506 eassert (it->glyph_row);
23507 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23509 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23510 if (glyph < it->glyph_row->glyphs[area + 1])
23512 /* If the glyph row is reversed, we need to prepend the glyph
23513 rather than append it. */
23514 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23516 struct glyph *g;
23518 /* Make room for the additional glyph. */
23519 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23520 g[1] = *g;
23521 glyph = it->glyph_row->glyphs[area];
23523 glyph->charpos = CHARPOS (it->position);
23524 glyph->object = it->object;
23525 if (it->pixel_width > 0)
23527 glyph->pixel_width = it->pixel_width;
23528 glyph->padding_p = 0;
23530 else
23532 /* Assure at least 1-pixel width. Otherwise, cursor can't
23533 be displayed correctly. */
23534 glyph->pixel_width = 1;
23535 glyph->padding_p = 1;
23537 glyph->ascent = it->ascent;
23538 glyph->descent = it->descent;
23539 glyph->voffset = it->voffset;
23540 glyph->type = CHAR_GLYPH;
23541 glyph->avoid_cursor_p = it->avoid_cursor_p;
23542 glyph->multibyte_p = it->multibyte_p;
23543 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23545 /* In R2L rows, the left and the right box edges need to be
23546 drawn in reverse direction. */
23547 glyph->right_box_line_p = it->start_of_box_run_p;
23548 glyph->left_box_line_p = it->end_of_box_run_p;
23550 else
23552 glyph->left_box_line_p = it->start_of_box_run_p;
23553 glyph->right_box_line_p = it->end_of_box_run_p;
23555 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23556 || it->phys_descent > it->descent);
23557 glyph->glyph_not_available_p = it->glyph_not_available_p;
23558 glyph->face_id = it->face_id;
23559 glyph->u.ch = it->char_to_display;
23560 glyph->slice.img = null_glyph_slice;
23561 glyph->font_type = FONT_TYPE_UNKNOWN;
23562 if (it->bidi_p)
23564 glyph->resolved_level = it->bidi_it.resolved_level;
23565 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23566 emacs_abort ();
23567 glyph->bidi_type = it->bidi_it.type;
23569 else
23571 glyph->resolved_level = 0;
23572 glyph->bidi_type = UNKNOWN_BT;
23574 ++it->glyph_row->used[area];
23576 else
23577 IT_EXPAND_MATRIX_WIDTH (it, area);
23580 /* Store one glyph for the composition IT->cmp_it.id in
23581 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23582 non-null. */
23584 static void
23585 append_composite_glyph (struct it *it)
23587 struct glyph *glyph;
23588 enum glyph_row_area area = it->area;
23590 eassert (it->glyph_row);
23592 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23593 if (glyph < it->glyph_row->glyphs[area + 1])
23595 /* If the glyph row is reversed, we need to prepend the glyph
23596 rather than append it. */
23597 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23599 struct glyph *g;
23601 /* Make room for the new glyph. */
23602 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23603 g[1] = *g;
23604 glyph = it->glyph_row->glyphs[it->area];
23606 glyph->charpos = it->cmp_it.charpos;
23607 glyph->object = it->object;
23608 glyph->pixel_width = it->pixel_width;
23609 glyph->ascent = it->ascent;
23610 glyph->descent = it->descent;
23611 glyph->voffset = it->voffset;
23612 glyph->type = COMPOSITE_GLYPH;
23613 if (it->cmp_it.ch < 0)
23615 glyph->u.cmp.automatic = 0;
23616 glyph->u.cmp.id = it->cmp_it.id;
23617 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23619 else
23621 glyph->u.cmp.automatic = 1;
23622 glyph->u.cmp.id = it->cmp_it.id;
23623 glyph->slice.cmp.from = it->cmp_it.from;
23624 glyph->slice.cmp.to = it->cmp_it.to - 1;
23626 glyph->avoid_cursor_p = it->avoid_cursor_p;
23627 glyph->multibyte_p = it->multibyte_p;
23628 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23630 /* In R2L rows, the left and the right box edges need to be
23631 drawn in reverse direction. */
23632 glyph->right_box_line_p = it->start_of_box_run_p;
23633 glyph->left_box_line_p = it->end_of_box_run_p;
23635 else
23637 glyph->left_box_line_p = it->start_of_box_run_p;
23638 glyph->right_box_line_p = it->end_of_box_run_p;
23640 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23641 || it->phys_descent > it->descent);
23642 glyph->padding_p = 0;
23643 glyph->glyph_not_available_p = 0;
23644 glyph->face_id = it->face_id;
23645 glyph->font_type = FONT_TYPE_UNKNOWN;
23646 if (it->bidi_p)
23648 glyph->resolved_level = it->bidi_it.resolved_level;
23649 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23650 emacs_abort ();
23651 glyph->bidi_type = it->bidi_it.type;
23653 ++it->glyph_row->used[area];
23655 else
23656 IT_EXPAND_MATRIX_WIDTH (it, area);
23660 /* Change IT->ascent and IT->height according to the setting of
23661 IT->voffset. */
23663 static void
23664 take_vertical_position_into_account (struct it *it)
23666 if (it->voffset)
23668 if (it->voffset < 0)
23669 /* Increase the ascent so that we can display the text higher
23670 in the line. */
23671 it->ascent -= it->voffset;
23672 else
23673 /* Increase the descent so that we can display the text lower
23674 in the line. */
23675 it->descent += it->voffset;
23680 /* Produce glyphs/get display metrics for the image IT is loaded with.
23681 See the description of struct display_iterator in dispextern.h for
23682 an overview of struct display_iterator. */
23684 static void
23685 produce_image_glyph (struct it *it)
23687 struct image *img;
23688 struct face *face;
23689 int glyph_ascent, crop;
23690 struct glyph_slice slice;
23692 eassert (it->what == IT_IMAGE);
23694 face = FACE_FROM_ID (it->f, it->face_id);
23695 eassert (face);
23696 /* Make sure X resources of the face is loaded. */
23697 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23699 if (it->image_id < 0)
23701 /* Fringe bitmap. */
23702 it->ascent = it->phys_ascent = 0;
23703 it->descent = it->phys_descent = 0;
23704 it->pixel_width = 0;
23705 it->nglyphs = 0;
23706 return;
23709 img = IMAGE_FROM_ID (it->f, it->image_id);
23710 eassert (img);
23711 /* Make sure X resources of the image is loaded. */
23712 prepare_image_for_display (it->f, img);
23714 slice.x = slice.y = 0;
23715 slice.width = img->width;
23716 slice.height = img->height;
23718 if (INTEGERP (it->slice.x))
23719 slice.x = XINT (it->slice.x);
23720 else if (FLOATP (it->slice.x))
23721 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23723 if (INTEGERP (it->slice.y))
23724 slice.y = XINT (it->slice.y);
23725 else if (FLOATP (it->slice.y))
23726 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23728 if (INTEGERP (it->slice.width))
23729 slice.width = XINT (it->slice.width);
23730 else if (FLOATP (it->slice.width))
23731 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23733 if (INTEGERP (it->slice.height))
23734 slice.height = XINT (it->slice.height);
23735 else if (FLOATP (it->slice.height))
23736 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23738 if (slice.x >= img->width)
23739 slice.x = img->width;
23740 if (slice.y >= img->height)
23741 slice.y = img->height;
23742 if (slice.x + slice.width >= img->width)
23743 slice.width = img->width - slice.x;
23744 if (slice.y + slice.height > img->height)
23745 slice.height = img->height - slice.y;
23747 if (slice.width == 0 || slice.height == 0)
23748 return;
23750 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23752 it->descent = slice.height - glyph_ascent;
23753 if (slice.y == 0)
23754 it->descent += img->vmargin;
23755 if (slice.y + slice.height == img->height)
23756 it->descent += img->vmargin;
23757 it->phys_descent = it->descent;
23759 it->pixel_width = slice.width;
23760 if (slice.x == 0)
23761 it->pixel_width += img->hmargin;
23762 if (slice.x + slice.width == img->width)
23763 it->pixel_width += img->hmargin;
23765 /* It's quite possible for images to have an ascent greater than
23766 their height, so don't get confused in that case. */
23767 if (it->descent < 0)
23768 it->descent = 0;
23770 it->nglyphs = 1;
23772 if (face->box != FACE_NO_BOX)
23774 if (face->box_line_width > 0)
23776 if (slice.y == 0)
23777 it->ascent += face->box_line_width;
23778 if (slice.y + slice.height == img->height)
23779 it->descent += face->box_line_width;
23782 if (it->start_of_box_run_p && slice.x == 0)
23783 it->pixel_width += eabs (face->box_line_width);
23784 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23785 it->pixel_width += eabs (face->box_line_width);
23788 take_vertical_position_into_account (it);
23790 /* Automatically crop wide image glyphs at right edge so we can
23791 draw the cursor on same display row. */
23792 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23793 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23795 it->pixel_width -= crop;
23796 slice.width -= crop;
23799 if (it->glyph_row)
23801 struct glyph *glyph;
23802 enum glyph_row_area area = it->area;
23804 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23805 if (glyph < it->glyph_row->glyphs[area + 1])
23807 glyph->charpos = CHARPOS (it->position);
23808 glyph->object = it->object;
23809 glyph->pixel_width = it->pixel_width;
23810 glyph->ascent = glyph_ascent;
23811 glyph->descent = it->descent;
23812 glyph->voffset = it->voffset;
23813 glyph->type = IMAGE_GLYPH;
23814 glyph->avoid_cursor_p = it->avoid_cursor_p;
23815 glyph->multibyte_p = it->multibyte_p;
23816 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23818 /* In R2L rows, the left and the right box edges need to be
23819 drawn in reverse direction. */
23820 glyph->right_box_line_p = it->start_of_box_run_p;
23821 glyph->left_box_line_p = it->end_of_box_run_p;
23823 else
23825 glyph->left_box_line_p = it->start_of_box_run_p;
23826 glyph->right_box_line_p = it->end_of_box_run_p;
23828 glyph->overlaps_vertically_p = 0;
23829 glyph->padding_p = 0;
23830 glyph->glyph_not_available_p = 0;
23831 glyph->face_id = it->face_id;
23832 glyph->u.img_id = img->id;
23833 glyph->slice.img = slice;
23834 glyph->font_type = FONT_TYPE_UNKNOWN;
23835 if (it->bidi_p)
23837 glyph->resolved_level = it->bidi_it.resolved_level;
23838 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23839 emacs_abort ();
23840 glyph->bidi_type = it->bidi_it.type;
23842 ++it->glyph_row->used[area];
23844 else
23845 IT_EXPAND_MATRIX_WIDTH (it, area);
23850 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23851 of the glyph, WIDTH and HEIGHT are the width and height of the
23852 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23854 static void
23855 append_stretch_glyph (struct it *it, Lisp_Object object,
23856 int width, int height, int ascent)
23858 struct glyph *glyph;
23859 enum glyph_row_area area = it->area;
23861 eassert (ascent >= 0 && ascent <= height);
23863 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23864 if (glyph < it->glyph_row->glyphs[area + 1])
23866 /* If the glyph row is reversed, we need to prepend the glyph
23867 rather than append it. */
23868 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23870 struct glyph *g;
23872 /* Make room for the additional glyph. */
23873 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23874 g[1] = *g;
23875 glyph = it->glyph_row->glyphs[area];
23877 glyph->charpos = CHARPOS (it->position);
23878 glyph->object = object;
23879 glyph->pixel_width = width;
23880 glyph->ascent = ascent;
23881 glyph->descent = height - ascent;
23882 glyph->voffset = it->voffset;
23883 glyph->type = STRETCH_GLYPH;
23884 glyph->avoid_cursor_p = it->avoid_cursor_p;
23885 glyph->multibyte_p = it->multibyte_p;
23886 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23888 /* In R2L rows, the left and the right box edges need to be
23889 drawn in reverse direction. */
23890 glyph->right_box_line_p = it->start_of_box_run_p;
23891 glyph->left_box_line_p = it->end_of_box_run_p;
23893 else
23895 glyph->left_box_line_p = it->start_of_box_run_p;
23896 glyph->right_box_line_p = it->end_of_box_run_p;
23898 glyph->overlaps_vertically_p = 0;
23899 glyph->padding_p = 0;
23900 glyph->glyph_not_available_p = 0;
23901 glyph->face_id = it->face_id;
23902 glyph->u.stretch.ascent = ascent;
23903 glyph->u.stretch.height = height;
23904 glyph->slice.img = null_glyph_slice;
23905 glyph->font_type = FONT_TYPE_UNKNOWN;
23906 if (it->bidi_p)
23908 glyph->resolved_level = it->bidi_it.resolved_level;
23909 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23910 emacs_abort ();
23911 glyph->bidi_type = it->bidi_it.type;
23913 else
23915 glyph->resolved_level = 0;
23916 glyph->bidi_type = UNKNOWN_BT;
23918 ++it->glyph_row->used[area];
23920 else
23921 IT_EXPAND_MATRIX_WIDTH (it, area);
23924 #endif /* HAVE_WINDOW_SYSTEM */
23926 /* Produce a stretch glyph for iterator IT. IT->object is the value
23927 of the glyph property displayed. The value must be a list
23928 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23929 being recognized:
23931 1. `:width WIDTH' specifies that the space should be WIDTH *
23932 canonical char width wide. WIDTH may be an integer or floating
23933 point number.
23935 2. `:relative-width FACTOR' specifies that the width of the stretch
23936 should be computed from the width of the first character having the
23937 `glyph' property, and should be FACTOR times that width.
23939 3. `:align-to HPOS' specifies that the space should be wide enough
23940 to reach HPOS, a value in canonical character units.
23942 Exactly one of the above pairs must be present.
23944 4. `:height HEIGHT' specifies that the height of the stretch produced
23945 should be HEIGHT, measured in canonical character units.
23947 5. `:relative-height FACTOR' specifies that the height of the
23948 stretch should be FACTOR times the height of the characters having
23949 the glyph property.
23951 Either none or exactly one of 4 or 5 must be present.
23953 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23954 of the stretch should be used for the ascent of the stretch.
23955 ASCENT must be in the range 0 <= ASCENT <= 100. */
23957 void
23958 produce_stretch_glyph (struct it *it)
23960 /* (space :width WIDTH :height HEIGHT ...) */
23961 Lisp_Object prop, plist;
23962 int width = 0, height = 0, align_to = -1;
23963 int zero_width_ok_p = 0;
23964 double tem;
23965 struct font *font = NULL;
23967 #ifdef HAVE_WINDOW_SYSTEM
23968 int ascent = 0;
23969 int zero_height_ok_p = 0;
23971 if (FRAME_WINDOW_P (it->f))
23973 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23974 font = face->font ? face->font : FRAME_FONT (it->f);
23975 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23977 #endif
23979 /* List should start with `space'. */
23980 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23981 plist = XCDR (it->object);
23983 /* Compute the width of the stretch. */
23984 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23985 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23987 /* Absolute width `:width WIDTH' specified and valid. */
23988 zero_width_ok_p = 1;
23989 width = (int)tem;
23991 #ifdef HAVE_WINDOW_SYSTEM
23992 else if (FRAME_WINDOW_P (it->f)
23993 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23995 /* Relative width `:relative-width FACTOR' specified and valid.
23996 Compute the width of the characters having the `glyph'
23997 property. */
23998 struct it it2;
23999 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24001 it2 = *it;
24002 if (it->multibyte_p)
24003 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24004 else
24006 it2.c = it2.char_to_display = *p, it2.len = 1;
24007 if (! ASCII_CHAR_P (it2.c))
24008 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24011 it2.glyph_row = NULL;
24012 it2.what = IT_CHARACTER;
24013 x_produce_glyphs (&it2);
24014 width = NUMVAL (prop) * it2.pixel_width;
24016 #endif /* HAVE_WINDOW_SYSTEM */
24017 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24018 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24020 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24021 align_to = (align_to < 0
24023 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24024 else if (align_to < 0)
24025 align_to = window_box_left_offset (it->w, TEXT_AREA);
24026 width = max (0, (int)tem + align_to - it->current_x);
24027 zero_width_ok_p = 1;
24029 else
24030 /* Nothing specified -> width defaults to canonical char width. */
24031 width = FRAME_COLUMN_WIDTH (it->f);
24033 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24034 width = 1;
24036 #ifdef HAVE_WINDOW_SYSTEM
24037 /* Compute height. */
24038 if (FRAME_WINDOW_P (it->f))
24040 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24041 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24043 height = (int)tem;
24044 zero_height_ok_p = 1;
24046 else if (prop = Fplist_get (plist, QCrelative_height),
24047 NUMVAL (prop) > 0)
24048 height = FONT_HEIGHT (font) * NUMVAL (prop);
24049 else
24050 height = FONT_HEIGHT (font);
24052 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24053 height = 1;
24055 /* Compute percentage of height used for ascent. If
24056 `:ascent ASCENT' is present and valid, use that. Otherwise,
24057 derive the ascent from the font in use. */
24058 if (prop = Fplist_get (plist, QCascent),
24059 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24060 ascent = height * NUMVAL (prop) / 100.0;
24061 else if (!NILP (prop)
24062 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24063 ascent = min (max (0, (int)tem), height);
24064 else
24065 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24067 else
24068 #endif /* HAVE_WINDOW_SYSTEM */
24069 height = 1;
24071 if (width > 0 && it->line_wrap != TRUNCATE
24072 && it->current_x + width > it->last_visible_x)
24074 width = it->last_visible_x - it->current_x;
24075 #ifdef HAVE_WINDOW_SYSTEM
24076 /* Subtract one more pixel from the stretch width, but only on
24077 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24078 width -= FRAME_WINDOW_P (it->f);
24079 #endif
24082 if (width > 0 && height > 0 && it->glyph_row)
24084 Lisp_Object o_object = it->object;
24085 Lisp_Object object = it->stack[it->sp - 1].string;
24086 int n = width;
24088 if (!STRINGP (object))
24089 object = it->w->buffer;
24090 #ifdef HAVE_WINDOW_SYSTEM
24091 if (FRAME_WINDOW_P (it->f))
24092 append_stretch_glyph (it, object, width, height, ascent);
24093 else
24094 #endif
24096 it->object = object;
24097 it->char_to_display = ' ';
24098 it->pixel_width = it->len = 1;
24099 while (n--)
24100 tty_append_glyph (it);
24101 it->object = o_object;
24105 it->pixel_width = width;
24106 #ifdef HAVE_WINDOW_SYSTEM
24107 if (FRAME_WINDOW_P (it->f))
24109 it->ascent = it->phys_ascent = ascent;
24110 it->descent = it->phys_descent = height - it->ascent;
24111 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24112 take_vertical_position_into_account (it);
24114 else
24115 #endif
24116 it->nglyphs = width;
24119 /* Get information about special display element WHAT in an
24120 environment described by IT. WHAT is one of IT_TRUNCATION or
24121 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24122 non-null glyph_row member. This function ensures that fields like
24123 face_id, c, len of IT are left untouched. */
24125 static void
24126 produce_special_glyphs (struct it *it, enum display_element_type what)
24128 struct it temp_it;
24129 Lisp_Object gc;
24130 GLYPH glyph;
24132 temp_it = *it;
24133 temp_it.object = make_number (0);
24134 memset (&temp_it.current, 0, sizeof temp_it.current);
24136 if (what == IT_CONTINUATION)
24138 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24139 if (it->bidi_it.paragraph_dir == R2L)
24140 SET_GLYPH_FROM_CHAR (glyph, '/');
24141 else
24142 SET_GLYPH_FROM_CHAR (glyph, '\\');
24143 if (it->dp
24144 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24146 /* FIXME: Should we mirror GC for R2L lines? */
24147 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24148 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24151 else if (what == IT_TRUNCATION)
24153 /* Truncation glyph. */
24154 SET_GLYPH_FROM_CHAR (glyph, '$');
24155 if (it->dp
24156 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24158 /* FIXME: Should we mirror GC for R2L lines? */
24159 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24160 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24163 else
24164 emacs_abort ();
24166 #ifdef HAVE_WINDOW_SYSTEM
24167 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24168 is turned off, we precede the truncation/continuation glyphs by a
24169 stretch glyph whose width is computed such that these special
24170 glyphs are aligned at the window margin, even when very different
24171 fonts are used in different glyph rows. */
24172 if (FRAME_WINDOW_P (temp_it.f)
24173 /* init_iterator calls this with it->glyph_row == NULL, and it
24174 wants only the pixel width of the truncation/continuation
24175 glyphs. */
24176 && temp_it.glyph_row
24177 /* insert_left_trunc_glyphs calls us at the beginning of the
24178 row, and it has its own calculation of the stretch glyph
24179 width. */
24180 && temp_it.glyph_row->used[TEXT_AREA] > 0
24181 && (temp_it.glyph_row->reversed_p
24182 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24183 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24185 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24187 if (stretch_width > 0)
24189 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24190 struct font *font =
24191 face->font ? face->font : FRAME_FONT (temp_it.f);
24192 int stretch_ascent =
24193 (((temp_it.ascent + temp_it.descent)
24194 * FONT_BASE (font)) / FONT_HEIGHT (font));
24196 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24197 temp_it.ascent + temp_it.descent,
24198 stretch_ascent);
24201 #endif
24203 temp_it.dp = NULL;
24204 temp_it.what = IT_CHARACTER;
24205 temp_it.len = 1;
24206 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24207 temp_it.face_id = GLYPH_FACE (glyph);
24208 temp_it.len = CHAR_BYTES (temp_it.c);
24210 PRODUCE_GLYPHS (&temp_it);
24211 it->pixel_width = temp_it.pixel_width;
24212 it->nglyphs = temp_it.pixel_width;
24215 #ifdef HAVE_WINDOW_SYSTEM
24217 /* Calculate line-height and line-spacing properties.
24218 An integer value specifies explicit pixel value.
24219 A float value specifies relative value to current face height.
24220 A cons (float . face-name) specifies relative value to
24221 height of specified face font.
24223 Returns height in pixels, or nil. */
24226 static Lisp_Object
24227 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24228 int boff, int override)
24230 Lisp_Object face_name = Qnil;
24231 int ascent, descent, height;
24233 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24234 return val;
24236 if (CONSP (val))
24238 face_name = XCAR (val);
24239 val = XCDR (val);
24240 if (!NUMBERP (val))
24241 val = make_number (1);
24242 if (NILP (face_name))
24244 height = it->ascent + it->descent;
24245 goto scale;
24249 if (NILP (face_name))
24251 font = FRAME_FONT (it->f);
24252 boff = FRAME_BASELINE_OFFSET (it->f);
24254 else if (EQ (face_name, Qt))
24256 override = 0;
24258 else
24260 int face_id;
24261 struct face *face;
24263 face_id = lookup_named_face (it->f, face_name, 0);
24264 if (face_id < 0)
24265 return make_number (-1);
24267 face = FACE_FROM_ID (it->f, face_id);
24268 font = face->font;
24269 if (font == NULL)
24270 return make_number (-1);
24271 boff = font->baseline_offset;
24272 if (font->vertical_centering)
24273 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24276 ascent = FONT_BASE (font) + boff;
24277 descent = FONT_DESCENT (font) - boff;
24279 if (override)
24281 it->override_ascent = ascent;
24282 it->override_descent = descent;
24283 it->override_boff = boff;
24286 height = ascent + descent;
24288 scale:
24289 if (FLOATP (val))
24290 height = (int)(XFLOAT_DATA (val) * height);
24291 else if (INTEGERP (val))
24292 height *= XINT (val);
24294 return make_number (height);
24298 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24299 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24300 and only if this is for a character for which no font was found.
24302 If the display method (it->glyphless_method) is
24303 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24304 length of the acronym or the hexadecimal string, UPPER_XOFF and
24305 UPPER_YOFF are pixel offsets for the upper part of the string,
24306 LOWER_XOFF and LOWER_YOFF are for the lower part.
24308 For the other display methods, LEN through LOWER_YOFF are zero. */
24310 static void
24311 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24312 short upper_xoff, short upper_yoff,
24313 short lower_xoff, short lower_yoff)
24315 struct glyph *glyph;
24316 enum glyph_row_area area = it->area;
24318 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24319 if (glyph < it->glyph_row->glyphs[area + 1])
24321 /* If the glyph row is reversed, we need to prepend the glyph
24322 rather than append it. */
24323 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24325 struct glyph *g;
24327 /* Make room for the additional glyph. */
24328 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24329 g[1] = *g;
24330 glyph = it->glyph_row->glyphs[area];
24332 glyph->charpos = CHARPOS (it->position);
24333 glyph->object = it->object;
24334 glyph->pixel_width = it->pixel_width;
24335 glyph->ascent = it->ascent;
24336 glyph->descent = it->descent;
24337 glyph->voffset = it->voffset;
24338 glyph->type = GLYPHLESS_GLYPH;
24339 glyph->u.glyphless.method = it->glyphless_method;
24340 glyph->u.glyphless.for_no_font = for_no_font;
24341 glyph->u.glyphless.len = len;
24342 glyph->u.glyphless.ch = it->c;
24343 glyph->slice.glyphless.upper_xoff = upper_xoff;
24344 glyph->slice.glyphless.upper_yoff = upper_yoff;
24345 glyph->slice.glyphless.lower_xoff = lower_xoff;
24346 glyph->slice.glyphless.lower_yoff = lower_yoff;
24347 glyph->avoid_cursor_p = it->avoid_cursor_p;
24348 glyph->multibyte_p = it->multibyte_p;
24349 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24351 /* In R2L rows, the left and the right box edges need to be
24352 drawn in reverse direction. */
24353 glyph->right_box_line_p = it->start_of_box_run_p;
24354 glyph->left_box_line_p = it->end_of_box_run_p;
24356 else
24358 glyph->left_box_line_p = it->start_of_box_run_p;
24359 glyph->right_box_line_p = it->end_of_box_run_p;
24361 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24362 || it->phys_descent > it->descent);
24363 glyph->padding_p = 0;
24364 glyph->glyph_not_available_p = 0;
24365 glyph->face_id = face_id;
24366 glyph->font_type = FONT_TYPE_UNKNOWN;
24367 if (it->bidi_p)
24369 glyph->resolved_level = it->bidi_it.resolved_level;
24370 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24371 emacs_abort ();
24372 glyph->bidi_type = it->bidi_it.type;
24374 ++it->glyph_row->used[area];
24376 else
24377 IT_EXPAND_MATRIX_WIDTH (it, area);
24381 /* Produce a glyph for a glyphless character for iterator IT.
24382 IT->glyphless_method specifies which method to use for displaying
24383 the character. See the description of enum
24384 glyphless_display_method in dispextern.h for the detail.
24386 FOR_NO_FONT is nonzero if and only if this is for a character for
24387 which no font was found. ACRONYM, if non-nil, is an acronym string
24388 for the character. */
24390 static void
24391 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24393 int face_id;
24394 struct face *face;
24395 struct font *font;
24396 int base_width, base_height, width, height;
24397 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24398 int len;
24400 /* Get the metrics of the base font. We always refer to the current
24401 ASCII face. */
24402 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24403 font = face->font ? face->font : FRAME_FONT (it->f);
24404 it->ascent = FONT_BASE (font) + font->baseline_offset;
24405 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24406 base_height = it->ascent + it->descent;
24407 base_width = font->average_width;
24409 /* Get a face ID for the glyph by utilizing a cache (the same way as
24410 done for `escape-glyph' in get_next_display_element). */
24411 if (it->f == last_glyphless_glyph_frame
24412 && it->face_id == last_glyphless_glyph_face_id)
24414 face_id = last_glyphless_glyph_merged_face_id;
24416 else
24418 /* Merge the `glyphless-char' face into the current face. */
24419 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24420 last_glyphless_glyph_frame = it->f;
24421 last_glyphless_glyph_face_id = it->face_id;
24422 last_glyphless_glyph_merged_face_id = face_id;
24425 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24427 it->pixel_width = THIN_SPACE_WIDTH;
24428 len = 0;
24429 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24431 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24433 width = CHAR_WIDTH (it->c);
24434 if (width == 0)
24435 width = 1;
24436 else if (width > 4)
24437 width = 4;
24438 it->pixel_width = base_width * width;
24439 len = 0;
24440 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24442 else
24444 char buf[7];
24445 const char *str;
24446 unsigned int code[6];
24447 int upper_len;
24448 int ascent, descent;
24449 struct font_metrics metrics_upper, metrics_lower;
24451 face = FACE_FROM_ID (it->f, face_id);
24452 font = face->font ? face->font : FRAME_FONT (it->f);
24453 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24455 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24457 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24458 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24459 if (CONSP (acronym))
24460 acronym = XCAR (acronym);
24461 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24463 else
24465 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24466 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24467 str = buf;
24469 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24470 code[len] = font->driver->encode_char (font, str[len]);
24471 upper_len = (len + 1) / 2;
24472 font->driver->text_extents (font, code, upper_len,
24473 &metrics_upper);
24474 font->driver->text_extents (font, code + upper_len, len - upper_len,
24475 &metrics_lower);
24479 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24480 width = max (metrics_upper.width, metrics_lower.width) + 4;
24481 upper_xoff = upper_yoff = 2; /* the typical case */
24482 if (base_width >= width)
24484 /* Align the upper to the left, the lower to the right. */
24485 it->pixel_width = base_width;
24486 lower_xoff = base_width - 2 - metrics_lower.width;
24488 else
24490 /* Center the shorter one. */
24491 it->pixel_width = width;
24492 if (metrics_upper.width >= metrics_lower.width)
24493 lower_xoff = (width - metrics_lower.width) / 2;
24494 else
24496 /* FIXME: This code doesn't look right. It formerly was
24497 missing the "lower_xoff = 0;", which couldn't have
24498 been right since it left lower_xoff uninitialized. */
24499 lower_xoff = 0;
24500 upper_xoff = (width - metrics_upper.width) / 2;
24504 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24505 top, bottom, and between upper and lower strings. */
24506 height = (metrics_upper.ascent + metrics_upper.descent
24507 + metrics_lower.ascent + metrics_lower.descent) + 5;
24508 /* Center vertically.
24509 H:base_height, D:base_descent
24510 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24512 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24513 descent = D - H/2 + h/2;
24514 lower_yoff = descent - 2 - ld;
24515 upper_yoff = lower_yoff - la - 1 - ud; */
24516 ascent = - (it->descent - (base_height + height + 1) / 2);
24517 descent = it->descent - (base_height - height) / 2;
24518 lower_yoff = descent - 2 - metrics_lower.descent;
24519 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24520 - metrics_upper.descent);
24521 /* Don't make the height shorter than the base height. */
24522 if (height > base_height)
24524 it->ascent = ascent;
24525 it->descent = descent;
24529 it->phys_ascent = it->ascent;
24530 it->phys_descent = it->descent;
24531 if (it->glyph_row)
24532 append_glyphless_glyph (it, face_id, for_no_font, len,
24533 upper_xoff, upper_yoff,
24534 lower_xoff, lower_yoff);
24535 it->nglyphs = 1;
24536 take_vertical_position_into_account (it);
24540 /* RIF:
24541 Produce glyphs/get display metrics for the display element IT is
24542 loaded with. See the description of struct it in dispextern.h
24543 for an overview of struct it. */
24545 void
24546 x_produce_glyphs (struct it *it)
24548 int extra_line_spacing = it->extra_line_spacing;
24550 it->glyph_not_available_p = 0;
24552 if (it->what == IT_CHARACTER)
24554 XChar2b char2b;
24555 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24556 struct font *font = face->font;
24557 struct font_metrics *pcm = NULL;
24558 int boff; /* baseline offset */
24560 if (font == NULL)
24562 /* When no suitable font is found, display this character by
24563 the method specified in the first extra slot of
24564 Vglyphless_char_display. */
24565 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24567 eassert (it->what == IT_GLYPHLESS);
24568 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24569 goto done;
24572 boff = font->baseline_offset;
24573 if (font->vertical_centering)
24574 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24576 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24578 int stretched_p;
24580 it->nglyphs = 1;
24582 if (it->override_ascent >= 0)
24584 it->ascent = it->override_ascent;
24585 it->descent = it->override_descent;
24586 boff = it->override_boff;
24588 else
24590 it->ascent = FONT_BASE (font) + boff;
24591 it->descent = FONT_DESCENT (font) - boff;
24594 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24596 pcm = get_per_char_metric (font, &char2b);
24597 if (pcm->width == 0
24598 && pcm->rbearing == 0 && pcm->lbearing == 0)
24599 pcm = NULL;
24602 if (pcm)
24604 it->phys_ascent = pcm->ascent + boff;
24605 it->phys_descent = pcm->descent - boff;
24606 it->pixel_width = pcm->width;
24608 else
24610 it->glyph_not_available_p = 1;
24611 it->phys_ascent = it->ascent;
24612 it->phys_descent = it->descent;
24613 it->pixel_width = font->space_width;
24616 if (it->constrain_row_ascent_descent_p)
24618 if (it->descent > it->max_descent)
24620 it->ascent += it->descent - it->max_descent;
24621 it->descent = it->max_descent;
24623 if (it->ascent > it->max_ascent)
24625 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24626 it->ascent = it->max_ascent;
24628 it->phys_ascent = min (it->phys_ascent, it->ascent);
24629 it->phys_descent = min (it->phys_descent, it->descent);
24630 extra_line_spacing = 0;
24633 /* If this is a space inside a region of text with
24634 `space-width' property, change its width. */
24635 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24636 if (stretched_p)
24637 it->pixel_width *= XFLOATINT (it->space_width);
24639 /* If face has a box, add the box thickness to the character
24640 height. If character has a box line to the left and/or
24641 right, add the box line width to the character's width. */
24642 if (face->box != FACE_NO_BOX)
24644 int thick = face->box_line_width;
24646 if (thick > 0)
24648 it->ascent += thick;
24649 it->descent += thick;
24651 else
24652 thick = -thick;
24654 if (it->start_of_box_run_p)
24655 it->pixel_width += thick;
24656 if (it->end_of_box_run_p)
24657 it->pixel_width += thick;
24660 /* If face has an overline, add the height of the overline
24661 (1 pixel) and a 1 pixel margin to the character height. */
24662 if (face->overline_p)
24663 it->ascent += overline_margin;
24665 if (it->constrain_row_ascent_descent_p)
24667 if (it->ascent > it->max_ascent)
24668 it->ascent = it->max_ascent;
24669 if (it->descent > it->max_descent)
24670 it->descent = it->max_descent;
24673 take_vertical_position_into_account (it);
24675 /* If we have to actually produce glyphs, do it. */
24676 if (it->glyph_row)
24678 if (stretched_p)
24680 /* Translate a space with a `space-width' property
24681 into a stretch glyph. */
24682 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24683 / FONT_HEIGHT (font));
24684 append_stretch_glyph (it, it->object, it->pixel_width,
24685 it->ascent + it->descent, ascent);
24687 else
24688 append_glyph (it);
24690 /* If characters with lbearing or rbearing are displayed
24691 in this line, record that fact in a flag of the
24692 glyph row. This is used to optimize X output code. */
24693 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24694 it->glyph_row->contains_overlapping_glyphs_p = 1;
24696 if (! stretched_p && it->pixel_width == 0)
24697 /* We assure that all visible glyphs have at least 1-pixel
24698 width. */
24699 it->pixel_width = 1;
24701 else if (it->char_to_display == '\n')
24703 /* A newline has no width, but we need the height of the
24704 line. But if previous part of the line sets a height,
24705 don't increase that height */
24707 Lisp_Object height;
24708 Lisp_Object total_height = Qnil;
24710 it->override_ascent = -1;
24711 it->pixel_width = 0;
24712 it->nglyphs = 0;
24714 height = get_it_property (it, Qline_height);
24715 /* Split (line-height total-height) list */
24716 if (CONSP (height)
24717 && CONSP (XCDR (height))
24718 && NILP (XCDR (XCDR (height))))
24720 total_height = XCAR (XCDR (height));
24721 height = XCAR (height);
24723 height = calc_line_height_property (it, height, font, boff, 1);
24725 if (it->override_ascent >= 0)
24727 it->ascent = it->override_ascent;
24728 it->descent = it->override_descent;
24729 boff = it->override_boff;
24731 else
24733 it->ascent = FONT_BASE (font) + boff;
24734 it->descent = FONT_DESCENT (font) - boff;
24737 if (EQ (height, Qt))
24739 if (it->descent > it->max_descent)
24741 it->ascent += it->descent - it->max_descent;
24742 it->descent = it->max_descent;
24744 if (it->ascent > it->max_ascent)
24746 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24747 it->ascent = it->max_ascent;
24749 it->phys_ascent = min (it->phys_ascent, it->ascent);
24750 it->phys_descent = min (it->phys_descent, it->descent);
24751 it->constrain_row_ascent_descent_p = 1;
24752 extra_line_spacing = 0;
24754 else
24756 Lisp_Object spacing;
24758 it->phys_ascent = it->ascent;
24759 it->phys_descent = it->descent;
24761 if ((it->max_ascent > 0 || it->max_descent > 0)
24762 && face->box != FACE_NO_BOX
24763 && face->box_line_width > 0)
24765 it->ascent += face->box_line_width;
24766 it->descent += face->box_line_width;
24768 if (!NILP (height)
24769 && XINT (height) > it->ascent + it->descent)
24770 it->ascent = XINT (height) - it->descent;
24772 if (!NILP (total_height))
24773 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24774 else
24776 spacing = get_it_property (it, Qline_spacing);
24777 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24779 if (INTEGERP (spacing))
24781 extra_line_spacing = XINT (spacing);
24782 if (!NILP (total_height))
24783 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24787 else /* i.e. (it->char_to_display == '\t') */
24789 if (font->space_width > 0)
24791 int tab_width = it->tab_width * font->space_width;
24792 int x = it->current_x + it->continuation_lines_width;
24793 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24795 /* If the distance from the current position to the next tab
24796 stop is less than a space character width, use the
24797 tab stop after that. */
24798 if (next_tab_x - x < font->space_width)
24799 next_tab_x += tab_width;
24801 it->pixel_width = next_tab_x - x;
24802 it->nglyphs = 1;
24803 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24804 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24806 if (it->glyph_row)
24808 append_stretch_glyph (it, it->object, it->pixel_width,
24809 it->ascent + it->descent, it->ascent);
24812 else
24814 it->pixel_width = 0;
24815 it->nglyphs = 1;
24819 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24821 /* A static composition.
24823 Note: A composition is represented as one glyph in the
24824 glyph matrix. There are no padding glyphs.
24826 Important note: pixel_width, ascent, and descent are the
24827 values of what is drawn by draw_glyphs (i.e. the values of
24828 the overall glyphs composed). */
24829 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24830 int boff; /* baseline offset */
24831 struct composition *cmp = composition_table[it->cmp_it.id];
24832 int glyph_len = cmp->glyph_len;
24833 struct font *font = face->font;
24835 it->nglyphs = 1;
24837 /* If we have not yet calculated pixel size data of glyphs of
24838 the composition for the current face font, calculate them
24839 now. Theoretically, we have to check all fonts for the
24840 glyphs, but that requires much time and memory space. So,
24841 here we check only the font of the first glyph. This may
24842 lead to incorrect display, but it's very rare, and C-l
24843 (recenter-top-bottom) can correct the display anyway. */
24844 if (! cmp->font || cmp->font != font)
24846 /* Ascent and descent of the font of the first character
24847 of this composition (adjusted by baseline offset).
24848 Ascent and descent of overall glyphs should not be less
24849 than these, respectively. */
24850 int font_ascent, font_descent, font_height;
24851 /* Bounding box of the overall glyphs. */
24852 int leftmost, rightmost, lowest, highest;
24853 int lbearing, rbearing;
24854 int i, width, ascent, descent;
24855 int left_padded = 0, right_padded = 0;
24856 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24857 XChar2b char2b;
24858 struct font_metrics *pcm;
24859 int font_not_found_p;
24860 ptrdiff_t pos;
24862 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24863 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24864 break;
24865 if (glyph_len < cmp->glyph_len)
24866 right_padded = 1;
24867 for (i = 0; i < glyph_len; i++)
24869 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24870 break;
24871 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24873 if (i > 0)
24874 left_padded = 1;
24876 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24877 : IT_CHARPOS (*it));
24878 /* If no suitable font is found, use the default font. */
24879 font_not_found_p = font == NULL;
24880 if (font_not_found_p)
24882 face = face->ascii_face;
24883 font = face->font;
24885 boff = font->baseline_offset;
24886 if (font->vertical_centering)
24887 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24888 font_ascent = FONT_BASE (font) + boff;
24889 font_descent = FONT_DESCENT (font) - boff;
24890 font_height = FONT_HEIGHT (font);
24892 cmp->font = font;
24894 pcm = NULL;
24895 if (! font_not_found_p)
24897 get_char_face_and_encoding (it->f, c, it->face_id,
24898 &char2b, 0);
24899 pcm = get_per_char_metric (font, &char2b);
24902 /* Initialize the bounding box. */
24903 if (pcm)
24905 width = cmp->glyph_len > 0 ? pcm->width : 0;
24906 ascent = pcm->ascent;
24907 descent = pcm->descent;
24908 lbearing = pcm->lbearing;
24909 rbearing = pcm->rbearing;
24911 else
24913 width = cmp->glyph_len > 0 ? font->space_width : 0;
24914 ascent = FONT_BASE (font);
24915 descent = FONT_DESCENT (font);
24916 lbearing = 0;
24917 rbearing = width;
24920 rightmost = width;
24921 leftmost = 0;
24922 lowest = - descent + boff;
24923 highest = ascent + boff;
24925 if (! font_not_found_p
24926 && font->default_ascent
24927 && CHAR_TABLE_P (Vuse_default_ascent)
24928 && !NILP (Faref (Vuse_default_ascent,
24929 make_number (it->char_to_display))))
24930 highest = font->default_ascent + boff;
24932 /* Draw the first glyph at the normal position. It may be
24933 shifted to right later if some other glyphs are drawn
24934 at the left. */
24935 cmp->offsets[i * 2] = 0;
24936 cmp->offsets[i * 2 + 1] = boff;
24937 cmp->lbearing = lbearing;
24938 cmp->rbearing = rbearing;
24940 /* Set cmp->offsets for the remaining glyphs. */
24941 for (i++; i < glyph_len; i++)
24943 int left, right, btm, top;
24944 int ch = COMPOSITION_GLYPH (cmp, i);
24945 int face_id;
24946 struct face *this_face;
24948 if (ch == '\t')
24949 ch = ' ';
24950 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24951 this_face = FACE_FROM_ID (it->f, face_id);
24952 font = this_face->font;
24954 if (font == NULL)
24955 pcm = NULL;
24956 else
24958 get_char_face_and_encoding (it->f, ch, face_id,
24959 &char2b, 0);
24960 pcm = get_per_char_metric (font, &char2b);
24962 if (! pcm)
24963 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24964 else
24966 width = pcm->width;
24967 ascent = pcm->ascent;
24968 descent = pcm->descent;
24969 lbearing = pcm->lbearing;
24970 rbearing = pcm->rbearing;
24971 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24973 /* Relative composition with or without
24974 alternate chars. */
24975 left = (leftmost + rightmost - width) / 2;
24976 btm = - descent + boff;
24977 if (font->relative_compose
24978 && (! CHAR_TABLE_P (Vignore_relative_composition)
24979 || NILP (Faref (Vignore_relative_composition,
24980 make_number (ch)))))
24983 if (- descent >= font->relative_compose)
24984 /* One extra pixel between two glyphs. */
24985 btm = highest + 1;
24986 else if (ascent <= 0)
24987 /* One extra pixel between two glyphs. */
24988 btm = lowest - 1 - ascent - descent;
24991 else
24993 /* A composition rule is specified by an integer
24994 value that encodes global and new reference
24995 points (GREF and NREF). GREF and NREF are
24996 specified by numbers as below:
24998 0---1---2 -- ascent
25002 9--10--11 -- center
25004 ---3---4---5--- baseline
25006 6---7---8 -- descent
25008 int rule = COMPOSITION_RULE (cmp, i);
25009 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25011 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25012 grefx = gref % 3, nrefx = nref % 3;
25013 grefy = gref / 3, nrefy = nref / 3;
25014 if (xoff)
25015 xoff = font_height * (xoff - 128) / 256;
25016 if (yoff)
25017 yoff = font_height * (yoff - 128) / 256;
25019 left = (leftmost
25020 + grefx * (rightmost - leftmost) / 2
25021 - nrefx * width / 2
25022 + xoff);
25024 btm = ((grefy == 0 ? highest
25025 : grefy == 1 ? 0
25026 : grefy == 2 ? lowest
25027 : (highest + lowest) / 2)
25028 - (nrefy == 0 ? ascent + descent
25029 : nrefy == 1 ? descent - boff
25030 : nrefy == 2 ? 0
25031 : (ascent + descent) / 2)
25032 + yoff);
25035 cmp->offsets[i * 2] = left;
25036 cmp->offsets[i * 2 + 1] = btm + descent;
25038 /* Update the bounding box of the overall glyphs. */
25039 if (width > 0)
25041 right = left + width;
25042 if (left < leftmost)
25043 leftmost = left;
25044 if (right > rightmost)
25045 rightmost = right;
25047 top = btm + descent + ascent;
25048 if (top > highest)
25049 highest = top;
25050 if (btm < lowest)
25051 lowest = btm;
25053 if (cmp->lbearing > left + lbearing)
25054 cmp->lbearing = left + lbearing;
25055 if (cmp->rbearing < left + rbearing)
25056 cmp->rbearing = left + rbearing;
25060 /* If there are glyphs whose x-offsets are negative,
25061 shift all glyphs to the right and make all x-offsets
25062 non-negative. */
25063 if (leftmost < 0)
25065 for (i = 0; i < cmp->glyph_len; i++)
25066 cmp->offsets[i * 2] -= leftmost;
25067 rightmost -= leftmost;
25068 cmp->lbearing -= leftmost;
25069 cmp->rbearing -= leftmost;
25072 if (left_padded && cmp->lbearing < 0)
25074 for (i = 0; i < cmp->glyph_len; i++)
25075 cmp->offsets[i * 2] -= cmp->lbearing;
25076 rightmost -= cmp->lbearing;
25077 cmp->rbearing -= cmp->lbearing;
25078 cmp->lbearing = 0;
25080 if (right_padded && rightmost < cmp->rbearing)
25082 rightmost = cmp->rbearing;
25085 cmp->pixel_width = rightmost;
25086 cmp->ascent = highest;
25087 cmp->descent = - lowest;
25088 if (cmp->ascent < font_ascent)
25089 cmp->ascent = font_ascent;
25090 if (cmp->descent < font_descent)
25091 cmp->descent = font_descent;
25094 if (it->glyph_row
25095 && (cmp->lbearing < 0
25096 || cmp->rbearing > cmp->pixel_width))
25097 it->glyph_row->contains_overlapping_glyphs_p = 1;
25099 it->pixel_width = cmp->pixel_width;
25100 it->ascent = it->phys_ascent = cmp->ascent;
25101 it->descent = it->phys_descent = cmp->descent;
25102 if (face->box != FACE_NO_BOX)
25104 int thick = face->box_line_width;
25106 if (thick > 0)
25108 it->ascent += thick;
25109 it->descent += thick;
25111 else
25112 thick = - thick;
25114 if (it->start_of_box_run_p)
25115 it->pixel_width += thick;
25116 if (it->end_of_box_run_p)
25117 it->pixel_width += thick;
25120 /* If face has an overline, add the height of the overline
25121 (1 pixel) and a 1 pixel margin to the character height. */
25122 if (face->overline_p)
25123 it->ascent += overline_margin;
25125 take_vertical_position_into_account (it);
25126 if (it->ascent < 0)
25127 it->ascent = 0;
25128 if (it->descent < 0)
25129 it->descent = 0;
25131 if (it->glyph_row && cmp->glyph_len > 0)
25132 append_composite_glyph (it);
25134 else if (it->what == IT_COMPOSITION)
25136 /* A dynamic (automatic) composition. */
25137 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25138 Lisp_Object gstring;
25139 struct font_metrics metrics;
25141 it->nglyphs = 1;
25143 gstring = composition_gstring_from_id (it->cmp_it.id);
25144 it->pixel_width
25145 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25146 &metrics);
25147 if (it->glyph_row
25148 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25149 it->glyph_row->contains_overlapping_glyphs_p = 1;
25150 it->ascent = it->phys_ascent = metrics.ascent;
25151 it->descent = it->phys_descent = metrics.descent;
25152 if (face->box != FACE_NO_BOX)
25154 int thick = face->box_line_width;
25156 if (thick > 0)
25158 it->ascent += thick;
25159 it->descent += thick;
25161 else
25162 thick = - thick;
25164 if (it->start_of_box_run_p)
25165 it->pixel_width += thick;
25166 if (it->end_of_box_run_p)
25167 it->pixel_width += thick;
25169 /* If face has an overline, add the height of the overline
25170 (1 pixel) and a 1 pixel margin to the character height. */
25171 if (face->overline_p)
25172 it->ascent += overline_margin;
25173 take_vertical_position_into_account (it);
25174 if (it->ascent < 0)
25175 it->ascent = 0;
25176 if (it->descent < 0)
25177 it->descent = 0;
25179 if (it->glyph_row)
25180 append_composite_glyph (it);
25182 else if (it->what == IT_GLYPHLESS)
25183 produce_glyphless_glyph (it, 0, Qnil);
25184 else if (it->what == IT_IMAGE)
25185 produce_image_glyph (it);
25186 else if (it->what == IT_STRETCH)
25187 produce_stretch_glyph (it);
25189 done:
25190 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25191 because this isn't true for images with `:ascent 100'. */
25192 eassert (it->ascent >= 0 && it->descent >= 0);
25193 if (it->area == TEXT_AREA)
25194 it->current_x += it->pixel_width;
25196 if (extra_line_spacing > 0)
25198 it->descent += extra_line_spacing;
25199 if (extra_line_spacing > it->max_extra_line_spacing)
25200 it->max_extra_line_spacing = extra_line_spacing;
25203 it->max_ascent = max (it->max_ascent, it->ascent);
25204 it->max_descent = max (it->max_descent, it->descent);
25205 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25206 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25209 /* EXPORT for RIF:
25210 Output LEN glyphs starting at START at the nominal cursor position.
25211 Advance the nominal cursor over the text. The global variable
25212 updated_window contains the window being updated, updated_row is
25213 the glyph row being updated, and updated_area is the area of that
25214 row being updated. */
25216 void
25217 x_write_glyphs (struct glyph *start, int len)
25219 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25221 eassert (updated_window && updated_row);
25222 /* When the window is hscrolled, cursor hpos can legitimately be out
25223 of bounds, but we draw the cursor at the corresponding window
25224 margin in that case. */
25225 if (!updated_row->reversed_p && chpos < 0)
25226 chpos = 0;
25227 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25228 chpos = updated_row->used[TEXT_AREA] - 1;
25230 block_input ();
25232 /* Write glyphs. */
25234 hpos = start - updated_row->glyphs[updated_area];
25235 x = draw_glyphs (updated_window, output_cursor.x,
25236 updated_row, updated_area,
25237 hpos, hpos + len,
25238 DRAW_NORMAL_TEXT, 0);
25240 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25241 if (updated_area == TEXT_AREA
25242 && updated_window->phys_cursor_on_p
25243 && updated_window->phys_cursor.vpos == output_cursor.vpos
25244 && chpos >= hpos
25245 && chpos < hpos + len)
25246 updated_window->phys_cursor_on_p = 0;
25248 unblock_input ();
25250 /* Advance the output cursor. */
25251 output_cursor.hpos += len;
25252 output_cursor.x = x;
25256 /* EXPORT for RIF:
25257 Insert LEN glyphs from START at the nominal cursor position. */
25259 void
25260 x_insert_glyphs (struct glyph *start, int len)
25262 struct frame *f;
25263 struct window *w;
25264 int line_height, shift_by_width, shifted_region_width;
25265 struct glyph_row *row;
25266 struct glyph *glyph;
25267 int frame_x, frame_y;
25268 ptrdiff_t hpos;
25270 eassert (updated_window && updated_row);
25271 block_input ();
25272 w = updated_window;
25273 f = XFRAME (WINDOW_FRAME (w));
25275 /* Get the height of the line we are in. */
25276 row = updated_row;
25277 line_height = row->height;
25279 /* Get the width of the glyphs to insert. */
25280 shift_by_width = 0;
25281 for (glyph = start; glyph < start + len; ++glyph)
25282 shift_by_width += glyph->pixel_width;
25284 /* Get the width of the region to shift right. */
25285 shifted_region_width = (window_box_width (w, updated_area)
25286 - output_cursor.x
25287 - shift_by_width);
25289 /* Shift right. */
25290 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25291 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25293 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25294 line_height, shift_by_width);
25296 /* Write the glyphs. */
25297 hpos = start - row->glyphs[updated_area];
25298 draw_glyphs (w, output_cursor.x, row, updated_area,
25299 hpos, hpos + len,
25300 DRAW_NORMAL_TEXT, 0);
25302 /* Advance the output cursor. */
25303 output_cursor.hpos += len;
25304 output_cursor.x += shift_by_width;
25305 unblock_input ();
25309 /* EXPORT for RIF:
25310 Erase the current text line from the nominal cursor position
25311 (inclusive) to pixel column TO_X (exclusive). The idea is that
25312 everything from TO_X onward is already erased.
25314 TO_X is a pixel position relative to updated_area of
25315 updated_window. TO_X == -1 means clear to the end of this area. */
25317 void
25318 x_clear_end_of_line (int to_x)
25320 struct frame *f;
25321 struct window *w = updated_window;
25322 int max_x, min_y, max_y;
25323 int from_x, from_y, to_y;
25325 eassert (updated_window && updated_row);
25326 f = XFRAME (w->frame);
25328 if (updated_row->full_width_p)
25329 max_x = WINDOW_TOTAL_WIDTH (w);
25330 else
25331 max_x = window_box_width (w, updated_area);
25332 max_y = window_text_bottom_y (w);
25334 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25335 of window. For TO_X > 0, truncate to end of drawing area. */
25336 if (to_x == 0)
25337 return;
25338 else if (to_x < 0)
25339 to_x = max_x;
25340 else
25341 to_x = min (to_x, max_x);
25343 to_y = min (max_y, output_cursor.y + updated_row->height);
25345 /* Notice if the cursor will be cleared by this operation. */
25346 if (!updated_row->full_width_p)
25347 notice_overwritten_cursor (w, updated_area,
25348 output_cursor.x, -1,
25349 updated_row->y,
25350 MATRIX_ROW_BOTTOM_Y (updated_row));
25352 from_x = output_cursor.x;
25354 /* Translate to frame coordinates. */
25355 if (updated_row->full_width_p)
25357 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25358 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25360 else
25362 int area_left = window_box_left (w, updated_area);
25363 from_x += area_left;
25364 to_x += area_left;
25367 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25368 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25369 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25371 /* Prevent inadvertently clearing to end of the X window. */
25372 if (to_x > from_x && to_y > from_y)
25374 block_input ();
25375 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25376 to_x - from_x, to_y - from_y);
25377 unblock_input ();
25381 #endif /* HAVE_WINDOW_SYSTEM */
25385 /***********************************************************************
25386 Cursor types
25387 ***********************************************************************/
25389 /* Value is the internal representation of the specified cursor type
25390 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25391 of the bar cursor. */
25393 static enum text_cursor_kinds
25394 get_specified_cursor_type (Lisp_Object arg, int *width)
25396 enum text_cursor_kinds type;
25398 if (NILP (arg))
25399 return NO_CURSOR;
25401 if (EQ (arg, Qbox))
25402 return FILLED_BOX_CURSOR;
25404 if (EQ (arg, Qhollow))
25405 return HOLLOW_BOX_CURSOR;
25407 if (EQ (arg, Qbar))
25409 *width = 2;
25410 return BAR_CURSOR;
25413 if (CONSP (arg)
25414 && EQ (XCAR (arg), Qbar)
25415 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25417 *width = XINT (XCDR (arg));
25418 return BAR_CURSOR;
25421 if (EQ (arg, Qhbar))
25423 *width = 2;
25424 return HBAR_CURSOR;
25427 if (CONSP (arg)
25428 && EQ (XCAR (arg), Qhbar)
25429 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25431 *width = XINT (XCDR (arg));
25432 return HBAR_CURSOR;
25435 /* Treat anything unknown as "hollow box cursor".
25436 It was bad to signal an error; people have trouble fixing
25437 .Xdefaults with Emacs, when it has something bad in it. */
25438 type = HOLLOW_BOX_CURSOR;
25440 return type;
25443 /* Set the default cursor types for specified frame. */
25444 void
25445 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25447 int width = 1;
25448 Lisp_Object tem;
25450 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25451 FRAME_CURSOR_WIDTH (f) = width;
25453 /* By default, set up the blink-off state depending on the on-state. */
25455 tem = Fassoc (arg, Vblink_cursor_alist);
25456 if (!NILP (tem))
25458 FRAME_BLINK_OFF_CURSOR (f)
25459 = get_specified_cursor_type (XCDR (tem), &width);
25460 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25462 else
25463 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25467 #ifdef HAVE_WINDOW_SYSTEM
25469 /* Return the cursor we want to be displayed in window W. Return
25470 width of bar/hbar cursor through WIDTH arg. Return with
25471 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25472 (i.e. if the `system caret' should track this cursor).
25474 In a mini-buffer window, we want the cursor only to appear if we
25475 are reading input from this window. For the selected window, we
25476 want the cursor type given by the frame parameter or buffer local
25477 setting of cursor-type. If explicitly marked off, draw no cursor.
25478 In all other cases, we want a hollow box cursor. */
25480 static enum text_cursor_kinds
25481 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25482 int *active_cursor)
25484 struct frame *f = XFRAME (w->frame);
25485 struct buffer *b = XBUFFER (w->buffer);
25486 int cursor_type = DEFAULT_CURSOR;
25487 Lisp_Object alt_cursor;
25488 int non_selected = 0;
25490 *active_cursor = 1;
25492 /* Echo area */
25493 if (cursor_in_echo_area
25494 && FRAME_HAS_MINIBUF_P (f)
25495 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25497 if (w == XWINDOW (echo_area_window))
25499 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25501 *width = FRAME_CURSOR_WIDTH (f);
25502 return FRAME_DESIRED_CURSOR (f);
25504 else
25505 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25508 *active_cursor = 0;
25509 non_selected = 1;
25512 /* Detect a nonselected window or nonselected frame. */
25513 else if (w != XWINDOW (f->selected_window)
25514 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25516 *active_cursor = 0;
25518 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25519 return NO_CURSOR;
25521 non_selected = 1;
25524 /* Never display a cursor in a window in which cursor-type is nil. */
25525 if (NILP (BVAR (b, cursor_type)))
25526 return NO_CURSOR;
25528 /* Get the normal cursor type for this window. */
25529 if (EQ (BVAR (b, cursor_type), Qt))
25531 cursor_type = FRAME_DESIRED_CURSOR (f);
25532 *width = FRAME_CURSOR_WIDTH (f);
25534 else
25535 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25537 /* Use cursor-in-non-selected-windows instead
25538 for non-selected window or frame. */
25539 if (non_selected)
25541 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25542 if (!EQ (Qt, alt_cursor))
25543 return get_specified_cursor_type (alt_cursor, width);
25544 /* t means modify the normal cursor type. */
25545 if (cursor_type == FILLED_BOX_CURSOR)
25546 cursor_type = HOLLOW_BOX_CURSOR;
25547 else if (cursor_type == BAR_CURSOR && *width > 1)
25548 --*width;
25549 return cursor_type;
25552 /* Use normal cursor if not blinked off. */
25553 if (!w->cursor_off_p)
25555 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25557 if (cursor_type == FILLED_BOX_CURSOR)
25559 /* Using a block cursor on large images can be very annoying.
25560 So use a hollow cursor for "large" images.
25561 If image is not transparent (no mask), also use hollow cursor. */
25562 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25563 if (img != NULL && IMAGEP (img->spec))
25565 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25566 where N = size of default frame font size.
25567 This should cover most of the "tiny" icons people may use. */
25568 if (!img->mask
25569 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25570 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25571 cursor_type = HOLLOW_BOX_CURSOR;
25574 else if (cursor_type != NO_CURSOR)
25576 /* Display current only supports BOX and HOLLOW cursors for images.
25577 So for now, unconditionally use a HOLLOW cursor when cursor is
25578 not a solid box cursor. */
25579 cursor_type = HOLLOW_BOX_CURSOR;
25582 return cursor_type;
25585 /* Cursor is blinked off, so determine how to "toggle" it. */
25587 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25588 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25589 return get_specified_cursor_type (XCDR (alt_cursor), width);
25591 /* Then see if frame has specified a specific blink off cursor type. */
25592 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25594 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25595 return FRAME_BLINK_OFF_CURSOR (f);
25598 #if 0
25599 /* Some people liked having a permanently visible blinking cursor,
25600 while others had very strong opinions against it. So it was
25601 decided to remove it. KFS 2003-09-03 */
25603 /* Finally perform built-in cursor blinking:
25604 filled box <-> hollow box
25605 wide [h]bar <-> narrow [h]bar
25606 narrow [h]bar <-> no cursor
25607 other type <-> no cursor */
25609 if (cursor_type == FILLED_BOX_CURSOR)
25610 return HOLLOW_BOX_CURSOR;
25612 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25614 *width = 1;
25615 return cursor_type;
25617 #endif
25619 return NO_CURSOR;
25623 /* Notice when the text cursor of window W has been completely
25624 overwritten by a drawing operation that outputs glyphs in AREA
25625 starting at X0 and ending at X1 in the line starting at Y0 and
25626 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25627 the rest of the line after X0 has been written. Y coordinates
25628 are window-relative. */
25630 static void
25631 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25632 int x0, int x1, int y0, int y1)
25634 int cx0, cx1, cy0, cy1;
25635 struct glyph_row *row;
25637 if (!w->phys_cursor_on_p)
25638 return;
25639 if (area != TEXT_AREA)
25640 return;
25642 if (w->phys_cursor.vpos < 0
25643 || w->phys_cursor.vpos >= w->current_matrix->nrows
25644 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25645 !(row->enabled_p && row->displays_text_p)))
25646 return;
25648 if (row->cursor_in_fringe_p)
25650 row->cursor_in_fringe_p = 0;
25651 draw_fringe_bitmap (w, row, row->reversed_p);
25652 w->phys_cursor_on_p = 0;
25653 return;
25656 cx0 = w->phys_cursor.x;
25657 cx1 = cx0 + w->phys_cursor_width;
25658 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25659 return;
25661 /* The cursor image will be completely removed from the
25662 screen if the output area intersects the cursor area in
25663 y-direction. When we draw in [y0 y1[, and some part of
25664 the cursor is at y < y0, that part must have been drawn
25665 before. When scrolling, the cursor is erased before
25666 actually scrolling, so we don't come here. When not
25667 scrolling, the rows above the old cursor row must have
25668 changed, and in this case these rows must have written
25669 over the cursor image.
25671 Likewise if part of the cursor is below y1, with the
25672 exception of the cursor being in the first blank row at
25673 the buffer and window end because update_text_area
25674 doesn't draw that row. (Except when it does, but
25675 that's handled in update_text_area.) */
25677 cy0 = w->phys_cursor.y;
25678 cy1 = cy0 + w->phys_cursor_height;
25679 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25680 return;
25682 w->phys_cursor_on_p = 0;
25685 #endif /* HAVE_WINDOW_SYSTEM */
25688 /************************************************************************
25689 Mouse Face
25690 ************************************************************************/
25692 #ifdef HAVE_WINDOW_SYSTEM
25694 /* EXPORT for RIF:
25695 Fix the display of area AREA of overlapping row ROW in window W
25696 with respect to the overlapping part OVERLAPS. */
25698 void
25699 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25700 enum glyph_row_area area, int overlaps)
25702 int i, x;
25704 block_input ();
25706 x = 0;
25707 for (i = 0; i < row->used[area];)
25709 if (row->glyphs[area][i].overlaps_vertically_p)
25711 int start = i, start_x = x;
25715 x += row->glyphs[area][i].pixel_width;
25716 ++i;
25718 while (i < row->used[area]
25719 && row->glyphs[area][i].overlaps_vertically_p);
25721 draw_glyphs (w, start_x, row, area,
25722 start, i,
25723 DRAW_NORMAL_TEXT, overlaps);
25725 else
25727 x += row->glyphs[area][i].pixel_width;
25728 ++i;
25732 unblock_input ();
25736 /* EXPORT:
25737 Draw the cursor glyph of window W in glyph row ROW. See the
25738 comment of draw_glyphs for the meaning of HL. */
25740 void
25741 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25742 enum draw_glyphs_face hl)
25744 /* If cursor hpos is out of bounds, don't draw garbage. This can
25745 happen in mini-buffer windows when switching between echo area
25746 glyphs and mini-buffer. */
25747 if ((row->reversed_p
25748 ? (w->phys_cursor.hpos >= 0)
25749 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25751 int on_p = w->phys_cursor_on_p;
25752 int x1;
25753 int hpos = w->phys_cursor.hpos;
25755 /* When the window is hscrolled, cursor hpos can legitimately be
25756 out of bounds, but we draw the cursor at the corresponding
25757 window margin in that case. */
25758 if (!row->reversed_p && hpos < 0)
25759 hpos = 0;
25760 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25761 hpos = row->used[TEXT_AREA] - 1;
25763 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25764 hl, 0);
25765 w->phys_cursor_on_p = on_p;
25767 if (hl == DRAW_CURSOR)
25768 w->phys_cursor_width = x1 - w->phys_cursor.x;
25769 /* When we erase the cursor, and ROW is overlapped by other
25770 rows, make sure that these overlapping parts of other rows
25771 are redrawn. */
25772 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25774 w->phys_cursor_width = x1 - w->phys_cursor.x;
25776 if (row > w->current_matrix->rows
25777 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25778 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25779 OVERLAPS_ERASED_CURSOR);
25781 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25782 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25783 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25784 OVERLAPS_ERASED_CURSOR);
25790 /* EXPORT:
25791 Erase the image of a cursor of window W from the screen. */
25793 void
25794 erase_phys_cursor (struct window *w)
25796 struct frame *f = XFRAME (w->frame);
25797 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25798 int hpos = w->phys_cursor.hpos;
25799 int vpos = w->phys_cursor.vpos;
25800 int mouse_face_here_p = 0;
25801 struct glyph_matrix *active_glyphs = w->current_matrix;
25802 struct glyph_row *cursor_row;
25803 struct glyph *cursor_glyph;
25804 enum draw_glyphs_face hl;
25806 /* No cursor displayed or row invalidated => nothing to do on the
25807 screen. */
25808 if (w->phys_cursor_type == NO_CURSOR)
25809 goto mark_cursor_off;
25811 /* VPOS >= active_glyphs->nrows means that window has been resized.
25812 Don't bother to erase the cursor. */
25813 if (vpos >= active_glyphs->nrows)
25814 goto mark_cursor_off;
25816 /* If row containing cursor is marked invalid, there is nothing we
25817 can do. */
25818 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25819 if (!cursor_row->enabled_p)
25820 goto mark_cursor_off;
25822 /* If line spacing is > 0, old cursor may only be partially visible in
25823 window after split-window. So adjust visible height. */
25824 cursor_row->visible_height = min (cursor_row->visible_height,
25825 window_text_bottom_y (w) - cursor_row->y);
25827 /* If row is completely invisible, don't attempt to delete a cursor which
25828 isn't there. This can happen if cursor is at top of a window, and
25829 we switch to a buffer with a header line in that window. */
25830 if (cursor_row->visible_height <= 0)
25831 goto mark_cursor_off;
25833 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25834 if (cursor_row->cursor_in_fringe_p)
25836 cursor_row->cursor_in_fringe_p = 0;
25837 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25838 goto mark_cursor_off;
25841 /* This can happen when the new row is shorter than the old one.
25842 In this case, either draw_glyphs or clear_end_of_line
25843 should have cleared the cursor. Note that we wouldn't be
25844 able to erase the cursor in this case because we don't have a
25845 cursor glyph at hand. */
25846 if ((cursor_row->reversed_p
25847 ? (w->phys_cursor.hpos < 0)
25848 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25849 goto mark_cursor_off;
25851 /* When the window is hscrolled, cursor hpos can legitimately be out
25852 of bounds, but we draw the cursor at the corresponding window
25853 margin in that case. */
25854 if (!cursor_row->reversed_p && hpos < 0)
25855 hpos = 0;
25856 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25857 hpos = cursor_row->used[TEXT_AREA] - 1;
25859 /* If the cursor is in the mouse face area, redisplay that when
25860 we clear the cursor. */
25861 if (! NILP (hlinfo->mouse_face_window)
25862 && coords_in_mouse_face_p (w, hpos, vpos)
25863 /* Don't redraw the cursor's spot in mouse face if it is at the
25864 end of a line (on a newline). The cursor appears there, but
25865 mouse highlighting does not. */
25866 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25867 mouse_face_here_p = 1;
25869 /* Maybe clear the display under the cursor. */
25870 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25872 int x, y, left_x;
25873 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25874 int width;
25876 cursor_glyph = get_phys_cursor_glyph (w);
25877 if (cursor_glyph == NULL)
25878 goto mark_cursor_off;
25880 width = cursor_glyph->pixel_width;
25881 left_x = window_box_left_offset (w, TEXT_AREA);
25882 x = w->phys_cursor.x;
25883 if (x < left_x)
25884 width -= left_x - x;
25885 width = min (width, window_box_width (w, TEXT_AREA) - x);
25886 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25887 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25889 if (width > 0)
25890 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25893 /* Erase the cursor by redrawing the character underneath it. */
25894 if (mouse_face_here_p)
25895 hl = DRAW_MOUSE_FACE;
25896 else
25897 hl = DRAW_NORMAL_TEXT;
25898 draw_phys_cursor_glyph (w, cursor_row, hl);
25900 mark_cursor_off:
25901 w->phys_cursor_on_p = 0;
25902 w->phys_cursor_type = NO_CURSOR;
25906 /* EXPORT:
25907 Display or clear cursor of window W. If ON is zero, clear the
25908 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25909 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25911 void
25912 display_and_set_cursor (struct window *w, int on,
25913 int hpos, int vpos, int x, int y)
25915 struct frame *f = XFRAME (w->frame);
25916 int new_cursor_type;
25917 int new_cursor_width;
25918 int active_cursor;
25919 struct glyph_row *glyph_row;
25920 struct glyph *glyph;
25922 /* This is pointless on invisible frames, and dangerous on garbaged
25923 windows and frames; in the latter case, the frame or window may
25924 be in the midst of changing its size, and x and y may be off the
25925 window. */
25926 if (! FRAME_VISIBLE_P (f)
25927 || FRAME_GARBAGED_P (f)
25928 || vpos >= w->current_matrix->nrows
25929 || hpos >= w->current_matrix->matrix_w)
25930 return;
25932 /* If cursor is off and we want it off, return quickly. */
25933 if (!on && !w->phys_cursor_on_p)
25934 return;
25936 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25937 /* If cursor row is not enabled, we don't really know where to
25938 display the cursor. */
25939 if (!glyph_row->enabled_p)
25941 w->phys_cursor_on_p = 0;
25942 return;
25945 glyph = NULL;
25946 if (!glyph_row->exact_window_width_line_p
25947 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25948 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25950 eassert (input_blocked_p ());
25952 /* Set new_cursor_type to the cursor we want to be displayed. */
25953 new_cursor_type = get_window_cursor_type (w, glyph,
25954 &new_cursor_width, &active_cursor);
25956 /* If cursor is currently being shown and we don't want it to be or
25957 it is in the wrong place, or the cursor type is not what we want,
25958 erase it. */
25959 if (w->phys_cursor_on_p
25960 && (!on
25961 || w->phys_cursor.x != x
25962 || w->phys_cursor.y != y
25963 || new_cursor_type != w->phys_cursor_type
25964 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25965 && new_cursor_width != w->phys_cursor_width)))
25966 erase_phys_cursor (w);
25968 /* Don't check phys_cursor_on_p here because that flag is only set
25969 to zero in some cases where we know that the cursor has been
25970 completely erased, to avoid the extra work of erasing the cursor
25971 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25972 still not be visible, or it has only been partly erased. */
25973 if (on)
25975 w->phys_cursor_ascent = glyph_row->ascent;
25976 w->phys_cursor_height = glyph_row->height;
25978 /* Set phys_cursor_.* before x_draw_.* is called because some
25979 of them may need the information. */
25980 w->phys_cursor.x = x;
25981 w->phys_cursor.y = glyph_row->y;
25982 w->phys_cursor.hpos = hpos;
25983 w->phys_cursor.vpos = vpos;
25986 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25987 new_cursor_type, new_cursor_width,
25988 on, active_cursor);
25992 /* Switch the display of W's cursor on or off, according to the value
25993 of ON. */
25995 static void
25996 update_window_cursor (struct window *w, int on)
25998 /* Don't update cursor in windows whose frame is in the process
25999 of being deleted. */
26000 if (w->current_matrix)
26002 int hpos = w->phys_cursor.hpos;
26003 int vpos = w->phys_cursor.vpos;
26004 struct glyph_row *row;
26006 if (vpos >= w->current_matrix->nrows
26007 || hpos >= w->current_matrix->matrix_w)
26008 return;
26010 row = MATRIX_ROW (w->current_matrix, vpos);
26012 /* When the window is hscrolled, cursor hpos can legitimately be
26013 out of bounds, but we draw the cursor at the corresponding
26014 window margin in that case. */
26015 if (!row->reversed_p && hpos < 0)
26016 hpos = 0;
26017 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26018 hpos = row->used[TEXT_AREA] - 1;
26020 block_input ();
26021 display_and_set_cursor (w, on, hpos, vpos,
26022 w->phys_cursor.x, w->phys_cursor.y);
26023 unblock_input ();
26028 /* Call update_window_cursor with parameter ON_P on all leaf windows
26029 in the window tree rooted at W. */
26031 static void
26032 update_cursor_in_window_tree (struct window *w, int on_p)
26034 while (w)
26036 if (!NILP (w->hchild))
26037 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26038 else if (!NILP (w->vchild))
26039 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26040 else
26041 update_window_cursor (w, on_p);
26043 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26048 /* EXPORT:
26049 Display the cursor on window W, or clear it, according to ON_P.
26050 Don't change the cursor's position. */
26052 void
26053 x_update_cursor (struct frame *f, int on_p)
26055 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26059 /* EXPORT:
26060 Clear the cursor of window W to background color, and mark the
26061 cursor as not shown. This is used when the text where the cursor
26062 is about to be rewritten. */
26064 void
26065 x_clear_cursor (struct window *w)
26067 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26068 update_window_cursor (w, 0);
26071 #endif /* HAVE_WINDOW_SYSTEM */
26073 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26074 and MSDOS. */
26075 static void
26076 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26077 int start_hpos, int end_hpos,
26078 enum draw_glyphs_face draw)
26080 #ifdef HAVE_WINDOW_SYSTEM
26081 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26083 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26084 return;
26086 #endif
26087 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26088 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26089 #endif
26092 /* Display the active region described by mouse_face_* according to DRAW. */
26094 static void
26095 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26097 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26098 struct frame *f = XFRAME (WINDOW_FRAME (w));
26100 if (/* If window is in the process of being destroyed, don't bother
26101 to do anything. */
26102 w->current_matrix != NULL
26103 /* Don't update mouse highlight if hidden */
26104 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26105 /* Recognize when we are called to operate on rows that don't exist
26106 anymore. This can happen when a window is split. */
26107 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26109 int phys_cursor_on_p = w->phys_cursor_on_p;
26110 struct glyph_row *row, *first, *last;
26112 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26113 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26115 for (row = first; row <= last && row->enabled_p; ++row)
26117 int start_hpos, end_hpos, start_x;
26119 /* For all but the first row, the highlight starts at column 0. */
26120 if (row == first)
26122 /* R2L rows have BEG and END in reversed order, but the
26123 screen drawing geometry is always left to right. So
26124 we need to mirror the beginning and end of the
26125 highlighted area in R2L rows. */
26126 if (!row->reversed_p)
26128 start_hpos = hlinfo->mouse_face_beg_col;
26129 start_x = hlinfo->mouse_face_beg_x;
26131 else if (row == last)
26133 start_hpos = hlinfo->mouse_face_end_col;
26134 start_x = hlinfo->mouse_face_end_x;
26136 else
26138 start_hpos = 0;
26139 start_x = 0;
26142 else if (row->reversed_p && row == last)
26144 start_hpos = hlinfo->mouse_face_end_col;
26145 start_x = hlinfo->mouse_face_end_x;
26147 else
26149 start_hpos = 0;
26150 start_x = 0;
26153 if (row == last)
26155 if (!row->reversed_p)
26156 end_hpos = hlinfo->mouse_face_end_col;
26157 else if (row == first)
26158 end_hpos = hlinfo->mouse_face_beg_col;
26159 else
26161 end_hpos = row->used[TEXT_AREA];
26162 if (draw == DRAW_NORMAL_TEXT)
26163 row->fill_line_p = 1; /* Clear to end of line */
26166 else if (row->reversed_p && row == first)
26167 end_hpos = hlinfo->mouse_face_beg_col;
26168 else
26170 end_hpos = row->used[TEXT_AREA];
26171 if (draw == DRAW_NORMAL_TEXT)
26172 row->fill_line_p = 1; /* Clear to end of line */
26175 if (end_hpos > start_hpos)
26177 draw_row_with_mouse_face (w, start_x, row,
26178 start_hpos, end_hpos, draw);
26180 row->mouse_face_p
26181 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26185 #ifdef HAVE_WINDOW_SYSTEM
26186 /* When we've written over the cursor, arrange for it to
26187 be displayed again. */
26188 if (FRAME_WINDOW_P (f)
26189 && phys_cursor_on_p && !w->phys_cursor_on_p)
26191 int hpos = w->phys_cursor.hpos;
26193 /* When the window is hscrolled, cursor hpos can legitimately be
26194 out of bounds, but we draw the cursor at the corresponding
26195 window margin in that case. */
26196 if (!row->reversed_p && hpos < 0)
26197 hpos = 0;
26198 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26199 hpos = row->used[TEXT_AREA] - 1;
26201 block_input ();
26202 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26203 w->phys_cursor.x, w->phys_cursor.y);
26204 unblock_input ();
26206 #endif /* HAVE_WINDOW_SYSTEM */
26209 #ifdef HAVE_WINDOW_SYSTEM
26210 /* Change the mouse cursor. */
26211 if (FRAME_WINDOW_P (f))
26213 if (draw == DRAW_NORMAL_TEXT
26214 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26215 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26216 else if (draw == DRAW_MOUSE_FACE)
26217 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26218 else
26219 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26221 #endif /* HAVE_WINDOW_SYSTEM */
26224 /* EXPORT:
26225 Clear out the mouse-highlighted active region.
26226 Redraw it un-highlighted first. Value is non-zero if mouse
26227 face was actually drawn unhighlighted. */
26230 clear_mouse_face (Mouse_HLInfo *hlinfo)
26232 int cleared = 0;
26234 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26236 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26237 cleared = 1;
26240 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26241 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26242 hlinfo->mouse_face_window = Qnil;
26243 hlinfo->mouse_face_overlay = Qnil;
26244 return cleared;
26247 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26248 within the mouse face on that window. */
26249 static int
26250 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26252 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26254 /* Quickly resolve the easy cases. */
26255 if (!(WINDOWP (hlinfo->mouse_face_window)
26256 && XWINDOW (hlinfo->mouse_face_window) == w))
26257 return 0;
26258 if (vpos < hlinfo->mouse_face_beg_row
26259 || vpos > hlinfo->mouse_face_end_row)
26260 return 0;
26261 if (vpos > hlinfo->mouse_face_beg_row
26262 && vpos < hlinfo->mouse_face_end_row)
26263 return 1;
26265 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26267 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26269 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26270 return 1;
26272 else if ((vpos == hlinfo->mouse_face_beg_row
26273 && hpos >= hlinfo->mouse_face_beg_col)
26274 || (vpos == hlinfo->mouse_face_end_row
26275 && hpos < hlinfo->mouse_face_end_col))
26276 return 1;
26278 else
26280 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26282 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26283 return 1;
26285 else if ((vpos == hlinfo->mouse_face_beg_row
26286 && hpos <= hlinfo->mouse_face_beg_col)
26287 || (vpos == hlinfo->mouse_face_end_row
26288 && hpos > hlinfo->mouse_face_end_col))
26289 return 1;
26291 return 0;
26295 /* EXPORT:
26296 Non-zero if physical cursor of window W is within mouse face. */
26299 cursor_in_mouse_face_p (struct window *w)
26301 int hpos = w->phys_cursor.hpos;
26302 int vpos = w->phys_cursor.vpos;
26303 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26305 /* When the window is hscrolled, cursor hpos can legitimately be out
26306 of bounds, but we draw the cursor at the corresponding window
26307 margin in that case. */
26308 if (!row->reversed_p && hpos < 0)
26309 hpos = 0;
26310 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26311 hpos = row->used[TEXT_AREA] - 1;
26313 return coords_in_mouse_face_p (w, hpos, vpos);
26318 /* Find the glyph rows START_ROW and END_ROW of window W that display
26319 characters between buffer positions START_CHARPOS and END_CHARPOS
26320 (excluding END_CHARPOS). DISP_STRING is a display string that
26321 covers these buffer positions. This is similar to
26322 row_containing_pos, but is more accurate when bidi reordering makes
26323 buffer positions change non-linearly with glyph rows. */
26324 static void
26325 rows_from_pos_range (struct window *w,
26326 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26327 Lisp_Object disp_string,
26328 struct glyph_row **start, struct glyph_row **end)
26330 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26331 int last_y = window_text_bottom_y (w);
26332 struct glyph_row *row;
26334 *start = NULL;
26335 *end = NULL;
26337 while (!first->enabled_p
26338 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26339 first++;
26341 /* Find the START row. */
26342 for (row = first;
26343 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26344 row++)
26346 /* A row can potentially be the START row if the range of the
26347 characters it displays intersects the range
26348 [START_CHARPOS..END_CHARPOS). */
26349 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26350 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26351 /* See the commentary in row_containing_pos, for the
26352 explanation of the complicated way to check whether
26353 some position is beyond the end of the characters
26354 displayed by a row. */
26355 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26356 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26357 && !row->ends_at_zv_p
26358 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26359 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26360 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26361 && !row->ends_at_zv_p
26362 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26364 /* Found a candidate row. Now make sure at least one of the
26365 glyphs it displays has a charpos from the range
26366 [START_CHARPOS..END_CHARPOS).
26368 This is not obvious because bidi reordering could make
26369 buffer positions of a row be 1,2,3,102,101,100, and if we
26370 want to highlight characters in [50..60), we don't want
26371 this row, even though [50..60) does intersect [1..103),
26372 the range of character positions given by the row's start
26373 and end positions. */
26374 struct glyph *g = row->glyphs[TEXT_AREA];
26375 struct glyph *e = g + row->used[TEXT_AREA];
26377 while (g < e)
26379 if (((BUFFERP (g->object) || INTEGERP (g->object))
26380 && start_charpos <= g->charpos && g->charpos < end_charpos)
26381 /* A glyph that comes from DISP_STRING is by
26382 definition to be highlighted. */
26383 || EQ (g->object, disp_string))
26384 *start = row;
26385 g++;
26387 if (*start)
26388 break;
26392 /* Find the END row. */
26393 if (!*start
26394 /* If the last row is partially visible, start looking for END
26395 from that row, instead of starting from FIRST. */
26396 && !(row->enabled_p
26397 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26398 row = first;
26399 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26401 struct glyph_row *next = row + 1;
26402 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26404 if (!next->enabled_p
26405 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26406 /* The first row >= START whose range of displayed characters
26407 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26408 is the row END + 1. */
26409 || (start_charpos < next_start
26410 && end_charpos < next_start)
26411 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26412 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26413 && !next->ends_at_zv_p
26414 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26415 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26416 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26417 && !next->ends_at_zv_p
26418 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26420 *end = row;
26421 break;
26423 else
26425 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26426 but none of the characters it displays are in the range, it is
26427 also END + 1. */
26428 struct glyph *g = next->glyphs[TEXT_AREA];
26429 struct glyph *s = g;
26430 struct glyph *e = g + next->used[TEXT_AREA];
26432 while (g < e)
26434 if (((BUFFERP (g->object) || INTEGERP (g->object))
26435 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26436 /* If the buffer position of the first glyph in
26437 the row is equal to END_CHARPOS, it means
26438 the last character to be highlighted is the
26439 newline of ROW, and we must consider NEXT as
26440 END, not END+1. */
26441 || (((!next->reversed_p && g == s)
26442 || (next->reversed_p && g == e - 1))
26443 && (g->charpos == end_charpos
26444 /* Special case for when NEXT is an
26445 empty line at ZV. */
26446 || (g->charpos == -1
26447 && !row->ends_at_zv_p
26448 && next_start == end_charpos)))))
26449 /* A glyph that comes from DISP_STRING is by
26450 definition to be highlighted. */
26451 || EQ (g->object, disp_string))
26452 break;
26453 g++;
26455 if (g == e)
26457 *end = row;
26458 break;
26460 /* The first row that ends at ZV must be the last to be
26461 highlighted. */
26462 else if (next->ends_at_zv_p)
26464 *end = next;
26465 break;
26471 /* This function sets the mouse_face_* elements of HLINFO, assuming
26472 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26473 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26474 for the overlay or run of text properties specifying the mouse
26475 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26476 before-string and after-string that must also be highlighted.
26477 DISP_STRING, if non-nil, is a display string that may cover some
26478 or all of the highlighted text. */
26480 static void
26481 mouse_face_from_buffer_pos (Lisp_Object window,
26482 Mouse_HLInfo *hlinfo,
26483 ptrdiff_t mouse_charpos,
26484 ptrdiff_t start_charpos,
26485 ptrdiff_t end_charpos,
26486 Lisp_Object before_string,
26487 Lisp_Object after_string,
26488 Lisp_Object disp_string)
26490 struct window *w = XWINDOW (window);
26491 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26492 struct glyph_row *r1, *r2;
26493 struct glyph *glyph, *end;
26494 ptrdiff_t ignore, pos;
26495 int x;
26497 eassert (NILP (disp_string) || STRINGP (disp_string));
26498 eassert (NILP (before_string) || STRINGP (before_string));
26499 eassert (NILP (after_string) || STRINGP (after_string));
26501 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26502 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26503 if (r1 == NULL)
26504 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26505 /* If the before-string or display-string contains newlines,
26506 rows_from_pos_range skips to its last row. Move back. */
26507 if (!NILP (before_string) || !NILP (disp_string))
26509 struct glyph_row *prev;
26510 while ((prev = r1 - 1, prev >= first)
26511 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26512 && prev->used[TEXT_AREA] > 0)
26514 struct glyph *beg = prev->glyphs[TEXT_AREA];
26515 glyph = beg + prev->used[TEXT_AREA];
26516 while (--glyph >= beg && INTEGERP (glyph->object));
26517 if (glyph < beg
26518 || !(EQ (glyph->object, before_string)
26519 || EQ (glyph->object, disp_string)))
26520 break;
26521 r1 = prev;
26524 if (r2 == NULL)
26526 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26527 hlinfo->mouse_face_past_end = 1;
26529 else if (!NILP (after_string))
26531 /* If the after-string has newlines, advance to its last row. */
26532 struct glyph_row *next;
26533 struct glyph_row *last
26534 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26536 for (next = r2 + 1;
26537 next <= last
26538 && next->used[TEXT_AREA] > 0
26539 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26540 ++next)
26541 r2 = next;
26543 /* The rest of the display engine assumes that mouse_face_beg_row is
26544 either above mouse_face_end_row or identical to it. But with
26545 bidi-reordered continued lines, the row for START_CHARPOS could
26546 be below the row for END_CHARPOS. If so, swap the rows and store
26547 them in correct order. */
26548 if (r1->y > r2->y)
26550 struct glyph_row *tem = r2;
26552 r2 = r1;
26553 r1 = tem;
26556 hlinfo->mouse_face_beg_y = r1->y;
26557 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26558 hlinfo->mouse_face_end_y = r2->y;
26559 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26561 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26562 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26563 could be anywhere in the row and in any order. The strategy
26564 below is to find the leftmost and the rightmost glyph that
26565 belongs to either of these 3 strings, or whose position is
26566 between START_CHARPOS and END_CHARPOS, and highlight all the
26567 glyphs between those two. This may cover more than just the text
26568 between START_CHARPOS and END_CHARPOS if the range of characters
26569 strides the bidi level boundary, e.g. if the beginning is in R2L
26570 text while the end is in L2R text or vice versa. */
26571 if (!r1->reversed_p)
26573 /* This row is in a left to right paragraph. Scan it left to
26574 right. */
26575 glyph = r1->glyphs[TEXT_AREA];
26576 end = glyph + r1->used[TEXT_AREA];
26577 x = r1->x;
26579 /* Skip truncation glyphs at the start of the glyph row. */
26580 if (r1->displays_text_p)
26581 for (; glyph < end
26582 && INTEGERP (glyph->object)
26583 && glyph->charpos < 0;
26584 ++glyph)
26585 x += glyph->pixel_width;
26587 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26588 or DISP_STRING, and the first glyph from buffer whose
26589 position is between START_CHARPOS and END_CHARPOS. */
26590 for (; glyph < end
26591 && !INTEGERP (glyph->object)
26592 && !EQ (glyph->object, disp_string)
26593 && !(BUFFERP (glyph->object)
26594 && (glyph->charpos >= start_charpos
26595 && glyph->charpos < end_charpos));
26596 ++glyph)
26598 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26599 are present at buffer positions between START_CHARPOS and
26600 END_CHARPOS, or if they come from an overlay. */
26601 if (EQ (glyph->object, before_string))
26603 pos = string_buffer_position (before_string,
26604 start_charpos);
26605 /* If pos == 0, it means before_string came from an
26606 overlay, not from a buffer position. */
26607 if (!pos || (pos >= start_charpos && pos < end_charpos))
26608 break;
26610 else if (EQ (glyph->object, after_string))
26612 pos = string_buffer_position (after_string, end_charpos);
26613 if (!pos || (pos >= start_charpos && pos < end_charpos))
26614 break;
26616 x += glyph->pixel_width;
26618 hlinfo->mouse_face_beg_x = x;
26619 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26621 else
26623 /* This row is in a right to left paragraph. Scan it right to
26624 left. */
26625 struct glyph *g;
26627 end = r1->glyphs[TEXT_AREA] - 1;
26628 glyph = end + r1->used[TEXT_AREA];
26630 /* Skip truncation glyphs at the start of the glyph row. */
26631 if (r1->displays_text_p)
26632 for (; glyph > end
26633 && INTEGERP (glyph->object)
26634 && glyph->charpos < 0;
26635 --glyph)
26638 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26639 or DISP_STRING, and the first glyph from buffer whose
26640 position is between START_CHARPOS and END_CHARPOS. */
26641 for (; glyph > end
26642 && !INTEGERP (glyph->object)
26643 && !EQ (glyph->object, disp_string)
26644 && !(BUFFERP (glyph->object)
26645 && (glyph->charpos >= start_charpos
26646 && glyph->charpos < end_charpos));
26647 --glyph)
26649 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26650 are present at buffer positions between START_CHARPOS and
26651 END_CHARPOS, or if they come from an overlay. */
26652 if (EQ (glyph->object, before_string))
26654 pos = string_buffer_position (before_string, start_charpos);
26655 /* If pos == 0, it means before_string came from an
26656 overlay, not from a buffer position. */
26657 if (!pos || (pos >= start_charpos && pos < end_charpos))
26658 break;
26660 else if (EQ (glyph->object, after_string))
26662 pos = string_buffer_position (after_string, end_charpos);
26663 if (!pos || (pos >= start_charpos && pos < end_charpos))
26664 break;
26668 glyph++; /* first glyph to the right of the highlighted area */
26669 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26670 x += g->pixel_width;
26671 hlinfo->mouse_face_beg_x = x;
26672 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26675 /* If the highlight ends in a different row, compute GLYPH and END
26676 for the end row. Otherwise, reuse the values computed above for
26677 the row where the highlight begins. */
26678 if (r2 != r1)
26680 if (!r2->reversed_p)
26682 glyph = r2->glyphs[TEXT_AREA];
26683 end = glyph + r2->used[TEXT_AREA];
26684 x = r2->x;
26686 else
26688 end = r2->glyphs[TEXT_AREA] - 1;
26689 glyph = end + r2->used[TEXT_AREA];
26693 if (!r2->reversed_p)
26695 /* Skip truncation and continuation glyphs near the end of the
26696 row, and also blanks and stretch glyphs inserted by
26697 extend_face_to_end_of_line. */
26698 while (end > glyph
26699 && INTEGERP ((end - 1)->object))
26700 --end;
26701 /* Scan the rest of the glyph row from the end, looking for the
26702 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26703 DISP_STRING, or whose position is between START_CHARPOS
26704 and END_CHARPOS */
26705 for (--end;
26706 end > glyph
26707 && !INTEGERP (end->object)
26708 && !EQ (end->object, disp_string)
26709 && !(BUFFERP (end->object)
26710 && (end->charpos >= start_charpos
26711 && end->charpos < end_charpos));
26712 --end)
26714 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26715 are present at buffer positions between START_CHARPOS and
26716 END_CHARPOS, or if they come from an overlay. */
26717 if (EQ (end->object, before_string))
26719 pos = string_buffer_position (before_string, start_charpos);
26720 if (!pos || (pos >= start_charpos && pos < end_charpos))
26721 break;
26723 else if (EQ (end->object, after_string))
26725 pos = string_buffer_position (after_string, end_charpos);
26726 if (!pos || (pos >= start_charpos && pos < end_charpos))
26727 break;
26730 /* Find the X coordinate of the last glyph to be highlighted. */
26731 for (; glyph <= end; ++glyph)
26732 x += glyph->pixel_width;
26734 hlinfo->mouse_face_end_x = x;
26735 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26737 else
26739 /* Skip truncation and continuation glyphs near the end of the
26740 row, and also blanks and stretch glyphs inserted by
26741 extend_face_to_end_of_line. */
26742 x = r2->x;
26743 end++;
26744 while (end < glyph
26745 && INTEGERP (end->object))
26747 x += end->pixel_width;
26748 ++end;
26750 /* Scan the rest of the glyph row from the end, looking for the
26751 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26752 DISP_STRING, or whose position is between START_CHARPOS
26753 and END_CHARPOS */
26754 for ( ;
26755 end < glyph
26756 && !INTEGERP (end->object)
26757 && !EQ (end->object, disp_string)
26758 && !(BUFFERP (end->object)
26759 && (end->charpos >= start_charpos
26760 && end->charpos < end_charpos));
26761 ++end)
26763 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26764 are present at buffer positions between START_CHARPOS and
26765 END_CHARPOS, or if they come from an overlay. */
26766 if (EQ (end->object, before_string))
26768 pos = string_buffer_position (before_string, start_charpos);
26769 if (!pos || (pos >= start_charpos && pos < end_charpos))
26770 break;
26772 else if (EQ (end->object, after_string))
26774 pos = string_buffer_position (after_string, end_charpos);
26775 if (!pos || (pos >= start_charpos && pos < end_charpos))
26776 break;
26778 x += end->pixel_width;
26780 /* If we exited the above loop because we arrived at the last
26781 glyph of the row, and its buffer position is still not in
26782 range, it means the last character in range is the preceding
26783 newline. Bump the end column and x values to get past the
26784 last glyph. */
26785 if (end == glyph
26786 && BUFFERP (end->object)
26787 && (end->charpos < start_charpos
26788 || end->charpos >= end_charpos))
26790 x += end->pixel_width;
26791 ++end;
26793 hlinfo->mouse_face_end_x = x;
26794 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26797 hlinfo->mouse_face_window = window;
26798 hlinfo->mouse_face_face_id
26799 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26800 mouse_charpos + 1,
26801 !hlinfo->mouse_face_hidden, -1);
26802 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26805 /* The following function is not used anymore (replaced with
26806 mouse_face_from_string_pos), but I leave it here for the time
26807 being, in case someone would. */
26809 #if 0 /* not used */
26811 /* Find the position of the glyph for position POS in OBJECT in
26812 window W's current matrix, and return in *X, *Y the pixel
26813 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26815 RIGHT_P non-zero means return the position of the right edge of the
26816 glyph, RIGHT_P zero means return the left edge position.
26818 If no glyph for POS exists in the matrix, return the position of
26819 the glyph with the next smaller position that is in the matrix, if
26820 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26821 exists in the matrix, return the position of the glyph with the
26822 next larger position in OBJECT.
26824 Value is non-zero if a glyph was found. */
26826 static int
26827 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26828 int *hpos, int *vpos, int *x, int *y, int right_p)
26830 int yb = window_text_bottom_y (w);
26831 struct glyph_row *r;
26832 struct glyph *best_glyph = NULL;
26833 struct glyph_row *best_row = NULL;
26834 int best_x = 0;
26836 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26837 r->enabled_p && r->y < yb;
26838 ++r)
26840 struct glyph *g = r->glyphs[TEXT_AREA];
26841 struct glyph *e = g + r->used[TEXT_AREA];
26842 int gx;
26844 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26845 if (EQ (g->object, object))
26847 if (g->charpos == pos)
26849 best_glyph = g;
26850 best_x = gx;
26851 best_row = r;
26852 goto found;
26854 else if (best_glyph == NULL
26855 || ((eabs (g->charpos - pos)
26856 < eabs (best_glyph->charpos - pos))
26857 && (right_p
26858 ? g->charpos < pos
26859 : g->charpos > pos)))
26861 best_glyph = g;
26862 best_x = gx;
26863 best_row = r;
26868 found:
26870 if (best_glyph)
26872 *x = best_x;
26873 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26875 if (right_p)
26877 *x += best_glyph->pixel_width;
26878 ++*hpos;
26881 *y = best_row->y;
26882 *vpos = best_row - w->current_matrix->rows;
26885 return best_glyph != NULL;
26887 #endif /* not used */
26889 /* Find the positions of the first and the last glyphs in window W's
26890 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26891 (assumed to be a string), and return in HLINFO's mouse_face_*
26892 members the pixel and column/row coordinates of those glyphs. */
26894 static void
26895 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26896 Lisp_Object object,
26897 ptrdiff_t startpos, ptrdiff_t endpos)
26899 int yb = window_text_bottom_y (w);
26900 struct glyph_row *r;
26901 struct glyph *g, *e;
26902 int gx;
26903 int found = 0;
26905 /* Find the glyph row with at least one position in the range
26906 [STARTPOS..ENDPOS], and the first glyph in that row whose
26907 position belongs to that range. */
26908 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26909 r->enabled_p && r->y < yb;
26910 ++r)
26912 if (!r->reversed_p)
26914 g = r->glyphs[TEXT_AREA];
26915 e = g + r->used[TEXT_AREA];
26916 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26917 if (EQ (g->object, object)
26918 && startpos <= g->charpos && g->charpos <= endpos)
26920 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26921 hlinfo->mouse_face_beg_y = r->y;
26922 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26923 hlinfo->mouse_face_beg_x = gx;
26924 found = 1;
26925 break;
26928 else
26930 struct glyph *g1;
26932 e = r->glyphs[TEXT_AREA];
26933 g = e + r->used[TEXT_AREA];
26934 for ( ; g > e; --g)
26935 if (EQ ((g-1)->object, object)
26936 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26938 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26939 hlinfo->mouse_face_beg_y = r->y;
26940 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26941 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26942 gx += g1->pixel_width;
26943 hlinfo->mouse_face_beg_x = gx;
26944 found = 1;
26945 break;
26948 if (found)
26949 break;
26952 if (!found)
26953 return;
26955 /* Starting with the next row, look for the first row which does NOT
26956 include any glyphs whose positions are in the range. */
26957 for (++r; r->enabled_p && r->y < yb; ++r)
26959 g = r->glyphs[TEXT_AREA];
26960 e = g + r->used[TEXT_AREA];
26961 found = 0;
26962 for ( ; g < e; ++g)
26963 if (EQ (g->object, object)
26964 && startpos <= g->charpos && g->charpos <= endpos)
26966 found = 1;
26967 break;
26969 if (!found)
26970 break;
26973 /* The highlighted region ends on the previous row. */
26974 r--;
26976 /* Set the end row and its vertical pixel coordinate. */
26977 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26978 hlinfo->mouse_face_end_y = r->y;
26980 /* Compute and set the end column and the end column's horizontal
26981 pixel coordinate. */
26982 if (!r->reversed_p)
26984 g = r->glyphs[TEXT_AREA];
26985 e = g + r->used[TEXT_AREA];
26986 for ( ; e > g; --e)
26987 if (EQ ((e-1)->object, object)
26988 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26989 break;
26990 hlinfo->mouse_face_end_col = e - g;
26992 for (gx = r->x; g < e; ++g)
26993 gx += g->pixel_width;
26994 hlinfo->mouse_face_end_x = gx;
26996 else
26998 e = r->glyphs[TEXT_AREA];
26999 g = e + r->used[TEXT_AREA];
27000 for (gx = r->x ; e < g; ++e)
27002 if (EQ (e->object, object)
27003 && startpos <= e->charpos && e->charpos <= endpos)
27004 break;
27005 gx += e->pixel_width;
27007 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27008 hlinfo->mouse_face_end_x = gx;
27012 #ifdef HAVE_WINDOW_SYSTEM
27014 /* See if position X, Y is within a hot-spot of an image. */
27016 static int
27017 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27019 if (!CONSP (hot_spot))
27020 return 0;
27022 if (EQ (XCAR (hot_spot), Qrect))
27024 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27025 Lisp_Object rect = XCDR (hot_spot);
27026 Lisp_Object tem;
27027 if (!CONSP (rect))
27028 return 0;
27029 if (!CONSP (XCAR (rect)))
27030 return 0;
27031 if (!CONSP (XCDR (rect)))
27032 return 0;
27033 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27034 return 0;
27035 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27036 return 0;
27037 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27038 return 0;
27039 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27040 return 0;
27041 return 1;
27043 else if (EQ (XCAR (hot_spot), Qcircle))
27045 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27046 Lisp_Object circ = XCDR (hot_spot);
27047 Lisp_Object lr, lx0, ly0;
27048 if (CONSP (circ)
27049 && CONSP (XCAR (circ))
27050 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27051 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27052 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27054 double r = XFLOATINT (lr);
27055 double dx = XINT (lx0) - x;
27056 double dy = XINT (ly0) - y;
27057 return (dx * dx + dy * dy <= r * r);
27060 else if (EQ (XCAR (hot_spot), Qpoly))
27062 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27063 if (VECTORP (XCDR (hot_spot)))
27065 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27066 Lisp_Object *poly = v->contents;
27067 ptrdiff_t n = v->header.size;
27068 ptrdiff_t i;
27069 int inside = 0;
27070 Lisp_Object lx, ly;
27071 int x0, y0;
27073 /* Need an even number of coordinates, and at least 3 edges. */
27074 if (n < 6 || n & 1)
27075 return 0;
27077 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27078 If count is odd, we are inside polygon. Pixels on edges
27079 may or may not be included depending on actual geometry of the
27080 polygon. */
27081 if ((lx = poly[n-2], !INTEGERP (lx))
27082 || (ly = poly[n-1], !INTEGERP (lx)))
27083 return 0;
27084 x0 = XINT (lx), y0 = XINT (ly);
27085 for (i = 0; i < n; i += 2)
27087 int x1 = x0, y1 = y0;
27088 if ((lx = poly[i], !INTEGERP (lx))
27089 || (ly = poly[i+1], !INTEGERP (ly)))
27090 return 0;
27091 x0 = XINT (lx), y0 = XINT (ly);
27093 /* Does this segment cross the X line? */
27094 if (x0 >= x)
27096 if (x1 >= x)
27097 continue;
27099 else if (x1 < x)
27100 continue;
27101 if (y > y0 && y > y1)
27102 continue;
27103 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27104 inside = !inside;
27106 return inside;
27109 return 0;
27112 Lisp_Object
27113 find_hot_spot (Lisp_Object map, int x, int y)
27115 while (CONSP (map))
27117 if (CONSP (XCAR (map))
27118 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27119 return XCAR (map);
27120 map = XCDR (map);
27123 return Qnil;
27126 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27127 3, 3, 0,
27128 doc: /* Lookup in image map MAP coordinates X and Y.
27129 An image map is an alist where each element has the format (AREA ID PLIST).
27130 An AREA is specified as either a rectangle, a circle, or a polygon:
27131 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27132 pixel coordinates of the upper left and bottom right corners.
27133 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27134 and the radius of the circle; r may be a float or integer.
27135 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27136 vector describes one corner in the polygon.
27137 Returns the alist element for the first matching AREA in MAP. */)
27138 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27140 if (NILP (map))
27141 return Qnil;
27143 CHECK_NUMBER (x);
27144 CHECK_NUMBER (y);
27146 return find_hot_spot (map,
27147 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27148 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27152 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27153 static void
27154 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27156 /* Do not change cursor shape while dragging mouse. */
27157 if (!NILP (do_mouse_tracking))
27158 return;
27160 if (!NILP (pointer))
27162 if (EQ (pointer, Qarrow))
27163 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27164 else if (EQ (pointer, Qhand))
27165 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27166 else if (EQ (pointer, Qtext))
27167 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27168 else if (EQ (pointer, intern ("hdrag")))
27169 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27170 #ifdef HAVE_X_WINDOWS
27171 else if (EQ (pointer, intern ("vdrag")))
27172 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27173 #endif
27174 else if (EQ (pointer, intern ("hourglass")))
27175 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27176 else if (EQ (pointer, Qmodeline))
27177 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27178 else
27179 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27182 if (cursor != No_Cursor)
27183 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27186 #endif /* HAVE_WINDOW_SYSTEM */
27188 /* Take proper action when mouse has moved to the mode or header line
27189 or marginal area AREA of window W, x-position X and y-position Y.
27190 X is relative to the start of the text display area of W, so the
27191 width of bitmap areas and scroll bars must be subtracted to get a
27192 position relative to the start of the mode line. */
27194 static void
27195 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27196 enum window_part area)
27198 struct window *w = XWINDOW (window);
27199 struct frame *f = XFRAME (w->frame);
27200 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27201 #ifdef HAVE_WINDOW_SYSTEM
27202 Display_Info *dpyinfo;
27203 #endif
27204 Cursor cursor = No_Cursor;
27205 Lisp_Object pointer = Qnil;
27206 int dx, dy, width, height;
27207 ptrdiff_t charpos;
27208 Lisp_Object string, object = Qnil;
27209 Lisp_Object pos IF_LINT (= Qnil), help;
27211 Lisp_Object mouse_face;
27212 int original_x_pixel = x;
27213 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27214 struct glyph_row *row IF_LINT (= 0);
27216 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27218 int x0;
27219 struct glyph *end;
27221 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27222 returns them in row/column units! */
27223 string = mode_line_string (w, area, &x, &y, &charpos,
27224 &object, &dx, &dy, &width, &height);
27226 row = (area == ON_MODE_LINE
27227 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27228 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27230 /* Find the glyph under the mouse pointer. */
27231 if (row->mode_line_p && row->enabled_p)
27233 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27234 end = glyph + row->used[TEXT_AREA];
27236 for (x0 = original_x_pixel;
27237 glyph < end && x0 >= glyph->pixel_width;
27238 ++glyph)
27239 x0 -= glyph->pixel_width;
27241 if (glyph >= end)
27242 glyph = NULL;
27245 else
27247 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27248 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27249 returns them in row/column units! */
27250 string = marginal_area_string (w, area, &x, &y, &charpos,
27251 &object, &dx, &dy, &width, &height);
27254 help = Qnil;
27256 #ifdef HAVE_WINDOW_SYSTEM
27257 if (IMAGEP (object))
27259 Lisp_Object image_map, hotspot;
27260 if ((image_map = Fplist_get (XCDR (object), QCmap),
27261 !NILP (image_map))
27262 && (hotspot = find_hot_spot (image_map, dx, dy),
27263 CONSP (hotspot))
27264 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27266 Lisp_Object plist;
27268 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27269 If so, we could look for mouse-enter, mouse-leave
27270 properties in PLIST (and do something...). */
27271 hotspot = XCDR (hotspot);
27272 if (CONSP (hotspot)
27273 && (plist = XCAR (hotspot), CONSP (plist)))
27275 pointer = Fplist_get (plist, Qpointer);
27276 if (NILP (pointer))
27277 pointer = Qhand;
27278 help = Fplist_get (plist, Qhelp_echo);
27279 if (!NILP (help))
27281 help_echo_string = help;
27282 XSETWINDOW (help_echo_window, w);
27283 help_echo_object = w->buffer;
27284 help_echo_pos = charpos;
27288 if (NILP (pointer))
27289 pointer = Fplist_get (XCDR (object), QCpointer);
27291 #endif /* HAVE_WINDOW_SYSTEM */
27293 if (STRINGP (string))
27294 pos = make_number (charpos);
27296 /* Set the help text and mouse pointer. If the mouse is on a part
27297 of the mode line without any text (e.g. past the right edge of
27298 the mode line text), use the default help text and pointer. */
27299 if (STRINGP (string) || area == ON_MODE_LINE)
27301 /* Arrange to display the help by setting the global variables
27302 help_echo_string, help_echo_object, and help_echo_pos. */
27303 if (NILP (help))
27305 if (STRINGP (string))
27306 help = Fget_text_property (pos, Qhelp_echo, string);
27308 if (!NILP (help))
27310 help_echo_string = help;
27311 XSETWINDOW (help_echo_window, w);
27312 help_echo_object = string;
27313 help_echo_pos = charpos;
27315 else if (area == ON_MODE_LINE)
27317 Lisp_Object default_help
27318 = buffer_local_value_1 (Qmode_line_default_help_echo,
27319 w->buffer);
27321 if (STRINGP (default_help))
27323 help_echo_string = default_help;
27324 XSETWINDOW (help_echo_window, w);
27325 help_echo_object = Qnil;
27326 help_echo_pos = -1;
27331 #ifdef HAVE_WINDOW_SYSTEM
27332 /* Change the mouse pointer according to what is under it. */
27333 if (FRAME_WINDOW_P (f))
27335 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27336 if (STRINGP (string))
27338 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27340 if (NILP (pointer))
27341 pointer = Fget_text_property (pos, Qpointer, string);
27343 /* Change the mouse pointer according to what is under X/Y. */
27344 if (NILP (pointer)
27345 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27347 Lisp_Object map;
27348 map = Fget_text_property (pos, Qlocal_map, string);
27349 if (!KEYMAPP (map))
27350 map = Fget_text_property (pos, Qkeymap, string);
27351 if (!KEYMAPP (map))
27352 cursor = dpyinfo->vertical_scroll_bar_cursor;
27355 else
27356 /* Default mode-line pointer. */
27357 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27359 #endif
27362 /* Change the mouse face according to what is under X/Y. */
27363 if (STRINGP (string))
27365 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27366 if (!NILP (mouse_face)
27367 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27368 && glyph)
27370 Lisp_Object b, e;
27372 struct glyph * tmp_glyph;
27374 int gpos;
27375 int gseq_length;
27376 int total_pixel_width;
27377 ptrdiff_t begpos, endpos, ignore;
27379 int vpos, hpos;
27381 b = Fprevious_single_property_change (make_number (charpos + 1),
27382 Qmouse_face, string, Qnil);
27383 if (NILP (b))
27384 begpos = 0;
27385 else
27386 begpos = XINT (b);
27388 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27389 if (NILP (e))
27390 endpos = SCHARS (string);
27391 else
27392 endpos = XINT (e);
27394 /* Calculate the glyph position GPOS of GLYPH in the
27395 displayed string, relative to the beginning of the
27396 highlighted part of the string.
27398 Note: GPOS is different from CHARPOS. CHARPOS is the
27399 position of GLYPH in the internal string object. A mode
27400 line string format has structures which are converted to
27401 a flattened string by the Emacs Lisp interpreter. The
27402 internal string is an element of those structures. The
27403 displayed string is the flattened string. */
27404 tmp_glyph = row_start_glyph;
27405 while (tmp_glyph < glyph
27406 && (!(EQ (tmp_glyph->object, glyph->object)
27407 && begpos <= tmp_glyph->charpos
27408 && tmp_glyph->charpos < endpos)))
27409 tmp_glyph++;
27410 gpos = glyph - tmp_glyph;
27412 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27413 the highlighted part of the displayed string to which
27414 GLYPH belongs. Note: GSEQ_LENGTH is different from
27415 SCHARS (STRING), because the latter returns the length of
27416 the internal string. */
27417 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27418 tmp_glyph > glyph
27419 && (!(EQ (tmp_glyph->object, glyph->object)
27420 && begpos <= tmp_glyph->charpos
27421 && tmp_glyph->charpos < endpos));
27422 tmp_glyph--)
27424 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27426 /* Calculate the total pixel width of all the glyphs between
27427 the beginning of the highlighted area and GLYPH. */
27428 total_pixel_width = 0;
27429 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27430 total_pixel_width += tmp_glyph->pixel_width;
27432 /* Pre calculation of re-rendering position. Note: X is in
27433 column units here, after the call to mode_line_string or
27434 marginal_area_string. */
27435 hpos = x - gpos;
27436 vpos = (area == ON_MODE_LINE
27437 ? (w->current_matrix)->nrows - 1
27438 : 0);
27440 /* If GLYPH's position is included in the region that is
27441 already drawn in mouse face, we have nothing to do. */
27442 if ( EQ (window, hlinfo->mouse_face_window)
27443 && (!row->reversed_p
27444 ? (hlinfo->mouse_face_beg_col <= hpos
27445 && hpos < hlinfo->mouse_face_end_col)
27446 /* In R2L rows we swap BEG and END, see below. */
27447 : (hlinfo->mouse_face_end_col <= hpos
27448 && hpos < hlinfo->mouse_face_beg_col))
27449 && hlinfo->mouse_face_beg_row == vpos )
27450 return;
27452 if (clear_mouse_face (hlinfo))
27453 cursor = No_Cursor;
27455 if (!row->reversed_p)
27457 hlinfo->mouse_face_beg_col = hpos;
27458 hlinfo->mouse_face_beg_x = original_x_pixel
27459 - (total_pixel_width + dx);
27460 hlinfo->mouse_face_end_col = hpos + gseq_length;
27461 hlinfo->mouse_face_end_x = 0;
27463 else
27465 /* In R2L rows, show_mouse_face expects BEG and END
27466 coordinates to be swapped. */
27467 hlinfo->mouse_face_end_col = hpos;
27468 hlinfo->mouse_face_end_x = original_x_pixel
27469 - (total_pixel_width + dx);
27470 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27471 hlinfo->mouse_face_beg_x = 0;
27474 hlinfo->mouse_face_beg_row = vpos;
27475 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27476 hlinfo->mouse_face_beg_y = 0;
27477 hlinfo->mouse_face_end_y = 0;
27478 hlinfo->mouse_face_past_end = 0;
27479 hlinfo->mouse_face_window = window;
27481 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27482 charpos,
27483 0, 0, 0,
27484 &ignore,
27485 glyph->face_id,
27487 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27489 if (NILP (pointer))
27490 pointer = Qhand;
27492 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27493 clear_mouse_face (hlinfo);
27495 #ifdef HAVE_WINDOW_SYSTEM
27496 if (FRAME_WINDOW_P (f))
27497 define_frame_cursor1 (f, cursor, pointer);
27498 #endif
27502 /* EXPORT:
27503 Take proper action when the mouse has moved to position X, Y on
27504 frame F as regards highlighting characters that have mouse-face
27505 properties. Also de-highlighting chars where the mouse was before.
27506 X and Y can be negative or out of range. */
27508 void
27509 note_mouse_highlight (struct frame *f, int x, int y)
27511 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27512 enum window_part part = ON_NOTHING;
27513 Lisp_Object window;
27514 struct window *w;
27515 Cursor cursor = No_Cursor;
27516 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27517 struct buffer *b;
27519 /* When a menu is active, don't highlight because this looks odd. */
27520 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27521 if (popup_activated ())
27522 return;
27523 #endif
27525 if (NILP (Vmouse_highlight)
27526 || !f->glyphs_initialized_p
27527 || f->pointer_invisible)
27528 return;
27530 hlinfo->mouse_face_mouse_x = x;
27531 hlinfo->mouse_face_mouse_y = y;
27532 hlinfo->mouse_face_mouse_frame = f;
27534 if (hlinfo->mouse_face_defer)
27535 return;
27537 /* Which window is that in? */
27538 window = window_from_coordinates (f, x, y, &part, 1);
27540 /* If displaying active text in another window, clear that. */
27541 if (! EQ (window, hlinfo->mouse_face_window)
27542 /* Also clear if we move out of text area in same window. */
27543 || (!NILP (hlinfo->mouse_face_window)
27544 && !NILP (window)
27545 && part != ON_TEXT
27546 && part != ON_MODE_LINE
27547 && part != ON_HEADER_LINE))
27548 clear_mouse_face (hlinfo);
27550 /* Not on a window -> return. */
27551 if (!WINDOWP (window))
27552 return;
27554 /* Reset help_echo_string. It will get recomputed below. */
27555 help_echo_string = Qnil;
27557 /* Convert to window-relative pixel coordinates. */
27558 w = XWINDOW (window);
27559 frame_to_window_pixel_xy (w, &x, &y);
27561 #ifdef HAVE_WINDOW_SYSTEM
27562 /* Handle tool-bar window differently since it doesn't display a
27563 buffer. */
27564 if (EQ (window, f->tool_bar_window))
27566 note_tool_bar_highlight (f, x, y);
27567 return;
27569 #endif
27571 /* Mouse is on the mode, header line or margin? */
27572 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27573 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27575 note_mode_line_or_margin_highlight (window, x, y, part);
27576 return;
27579 #ifdef HAVE_WINDOW_SYSTEM
27580 if (part == ON_VERTICAL_BORDER)
27582 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27583 help_echo_string = build_string ("drag-mouse-1: resize");
27585 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27586 || part == ON_SCROLL_BAR)
27587 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27588 else
27589 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27590 #endif
27592 /* Are we in a window whose display is up to date?
27593 And verify the buffer's text has not changed. */
27594 b = XBUFFER (w->buffer);
27595 if (part == ON_TEXT
27596 && w->window_end_valid
27597 && w->last_modified == BUF_MODIFF (b)
27598 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27600 int hpos, vpos, dx, dy, area = LAST_AREA;
27601 ptrdiff_t pos;
27602 struct glyph *glyph;
27603 Lisp_Object object;
27604 Lisp_Object mouse_face = Qnil, position;
27605 Lisp_Object *overlay_vec = NULL;
27606 ptrdiff_t i, noverlays;
27607 struct buffer *obuf;
27608 ptrdiff_t obegv, ozv;
27609 int same_region;
27611 /* Find the glyph under X/Y. */
27612 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27614 #ifdef HAVE_WINDOW_SYSTEM
27615 /* Look for :pointer property on image. */
27616 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27618 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27619 if (img != NULL && IMAGEP (img->spec))
27621 Lisp_Object image_map, hotspot;
27622 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27623 !NILP (image_map))
27624 && (hotspot = find_hot_spot (image_map,
27625 glyph->slice.img.x + dx,
27626 glyph->slice.img.y + dy),
27627 CONSP (hotspot))
27628 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27630 Lisp_Object plist;
27632 /* Could check XCAR (hotspot) to see if we enter/leave
27633 this hot-spot.
27634 If so, we could look for mouse-enter, mouse-leave
27635 properties in PLIST (and do something...). */
27636 hotspot = XCDR (hotspot);
27637 if (CONSP (hotspot)
27638 && (plist = XCAR (hotspot), CONSP (plist)))
27640 pointer = Fplist_get (plist, Qpointer);
27641 if (NILP (pointer))
27642 pointer = Qhand;
27643 help_echo_string = Fplist_get (plist, Qhelp_echo);
27644 if (!NILP (help_echo_string))
27646 help_echo_window = window;
27647 help_echo_object = glyph->object;
27648 help_echo_pos = glyph->charpos;
27652 if (NILP (pointer))
27653 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27656 #endif /* HAVE_WINDOW_SYSTEM */
27658 /* Clear mouse face if X/Y not over text. */
27659 if (glyph == NULL
27660 || area != TEXT_AREA
27661 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27662 /* Glyph's OBJECT is an integer for glyphs inserted by the
27663 display engine for its internal purposes, like truncation
27664 and continuation glyphs and blanks beyond the end of
27665 line's text on text terminals. If we are over such a
27666 glyph, we are not over any text. */
27667 || INTEGERP (glyph->object)
27668 /* R2L rows have a stretch glyph at their front, which
27669 stands for no text, whereas L2R rows have no glyphs at
27670 all beyond the end of text. Treat such stretch glyphs
27671 like we do with NULL glyphs in L2R rows. */
27672 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27673 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27674 && glyph->type == STRETCH_GLYPH
27675 && glyph->avoid_cursor_p))
27677 if (clear_mouse_face (hlinfo))
27678 cursor = No_Cursor;
27679 #ifdef HAVE_WINDOW_SYSTEM
27680 if (FRAME_WINDOW_P (f) && NILP (pointer))
27682 if (area != TEXT_AREA)
27683 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27684 else
27685 pointer = Vvoid_text_area_pointer;
27687 #endif
27688 goto set_cursor;
27691 pos = glyph->charpos;
27692 object = glyph->object;
27693 if (!STRINGP (object) && !BUFFERP (object))
27694 goto set_cursor;
27696 /* If we get an out-of-range value, return now; avoid an error. */
27697 if (BUFFERP (object) && pos > BUF_Z (b))
27698 goto set_cursor;
27700 /* Make the window's buffer temporarily current for
27701 overlays_at and compute_char_face. */
27702 obuf = current_buffer;
27703 current_buffer = b;
27704 obegv = BEGV;
27705 ozv = ZV;
27706 BEGV = BEG;
27707 ZV = Z;
27709 /* Is this char mouse-active or does it have help-echo? */
27710 position = make_number (pos);
27712 if (BUFFERP (object))
27714 /* Put all the overlays we want in a vector in overlay_vec. */
27715 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27716 /* Sort overlays into increasing priority order. */
27717 noverlays = sort_overlays (overlay_vec, noverlays, w);
27719 else
27720 noverlays = 0;
27722 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27724 if (same_region)
27725 cursor = No_Cursor;
27727 /* Check mouse-face highlighting. */
27728 if (! same_region
27729 /* If there exists an overlay with mouse-face overlapping
27730 the one we are currently highlighting, we have to
27731 check if we enter the overlapping overlay, and then
27732 highlight only that. */
27733 || (OVERLAYP (hlinfo->mouse_face_overlay)
27734 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27736 /* Find the highest priority overlay with a mouse-face. */
27737 Lisp_Object overlay = Qnil;
27738 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27740 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27741 if (!NILP (mouse_face))
27742 overlay = overlay_vec[i];
27745 /* If we're highlighting the same overlay as before, there's
27746 no need to do that again. */
27747 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27748 goto check_help_echo;
27749 hlinfo->mouse_face_overlay = overlay;
27751 /* Clear the display of the old active region, if any. */
27752 if (clear_mouse_face (hlinfo))
27753 cursor = No_Cursor;
27755 /* If no overlay applies, get a text property. */
27756 if (NILP (overlay))
27757 mouse_face = Fget_text_property (position, Qmouse_face, object);
27759 /* Next, compute the bounds of the mouse highlighting and
27760 display it. */
27761 if (!NILP (mouse_face) && STRINGP (object))
27763 /* The mouse-highlighting comes from a display string
27764 with a mouse-face. */
27765 Lisp_Object s, e;
27766 ptrdiff_t ignore;
27768 s = Fprevious_single_property_change
27769 (make_number (pos + 1), Qmouse_face, object, Qnil);
27770 e = Fnext_single_property_change
27771 (position, Qmouse_face, object, Qnil);
27772 if (NILP (s))
27773 s = make_number (0);
27774 if (NILP (e))
27775 e = make_number (SCHARS (object) - 1);
27776 mouse_face_from_string_pos (w, hlinfo, object,
27777 XINT (s), XINT (e));
27778 hlinfo->mouse_face_past_end = 0;
27779 hlinfo->mouse_face_window = window;
27780 hlinfo->mouse_face_face_id
27781 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27782 glyph->face_id, 1);
27783 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27784 cursor = No_Cursor;
27786 else
27788 /* The mouse-highlighting, if any, comes from an overlay
27789 or text property in the buffer. */
27790 Lisp_Object buffer IF_LINT (= Qnil);
27791 Lisp_Object disp_string IF_LINT (= Qnil);
27793 if (STRINGP (object))
27795 /* If we are on a display string with no mouse-face,
27796 check if the text under it has one. */
27797 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27798 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27799 pos = string_buffer_position (object, start);
27800 if (pos > 0)
27802 mouse_face = get_char_property_and_overlay
27803 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27804 buffer = w->buffer;
27805 disp_string = object;
27808 else
27810 buffer = object;
27811 disp_string = Qnil;
27814 if (!NILP (mouse_face))
27816 Lisp_Object before, after;
27817 Lisp_Object before_string, after_string;
27818 /* To correctly find the limits of mouse highlight
27819 in a bidi-reordered buffer, we must not use the
27820 optimization of limiting the search in
27821 previous-single-property-change and
27822 next-single-property-change, because
27823 rows_from_pos_range needs the real start and end
27824 positions to DTRT in this case. That's because
27825 the first row visible in a window does not
27826 necessarily display the character whose position
27827 is the smallest. */
27828 Lisp_Object lim1 =
27829 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27830 ? Fmarker_position (w->start)
27831 : Qnil;
27832 Lisp_Object lim2 =
27833 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27834 ? make_number (BUF_Z (XBUFFER (buffer))
27835 - XFASTINT (w->window_end_pos))
27836 : Qnil;
27838 if (NILP (overlay))
27840 /* Handle the text property case. */
27841 before = Fprevious_single_property_change
27842 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27843 after = Fnext_single_property_change
27844 (make_number (pos), Qmouse_face, buffer, lim2);
27845 before_string = after_string = Qnil;
27847 else
27849 /* Handle the overlay case. */
27850 before = Foverlay_start (overlay);
27851 after = Foverlay_end (overlay);
27852 before_string = Foverlay_get (overlay, Qbefore_string);
27853 after_string = Foverlay_get (overlay, Qafter_string);
27855 if (!STRINGP (before_string)) before_string = Qnil;
27856 if (!STRINGP (after_string)) after_string = Qnil;
27859 mouse_face_from_buffer_pos (window, hlinfo, pos,
27860 NILP (before)
27862 : XFASTINT (before),
27863 NILP (after)
27864 ? BUF_Z (XBUFFER (buffer))
27865 : XFASTINT (after),
27866 before_string, after_string,
27867 disp_string);
27868 cursor = No_Cursor;
27873 check_help_echo:
27875 /* Look for a `help-echo' property. */
27876 if (NILP (help_echo_string)) {
27877 Lisp_Object help, overlay;
27879 /* Check overlays first. */
27880 help = overlay = Qnil;
27881 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27883 overlay = overlay_vec[i];
27884 help = Foverlay_get (overlay, Qhelp_echo);
27887 if (!NILP (help))
27889 help_echo_string = help;
27890 help_echo_window = window;
27891 help_echo_object = overlay;
27892 help_echo_pos = pos;
27894 else
27896 Lisp_Object obj = glyph->object;
27897 ptrdiff_t charpos = glyph->charpos;
27899 /* Try text properties. */
27900 if (STRINGP (obj)
27901 && charpos >= 0
27902 && charpos < SCHARS (obj))
27904 help = Fget_text_property (make_number (charpos),
27905 Qhelp_echo, obj);
27906 if (NILP (help))
27908 /* If the string itself doesn't specify a help-echo,
27909 see if the buffer text ``under'' it does. */
27910 struct glyph_row *r
27911 = MATRIX_ROW (w->current_matrix, vpos);
27912 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27913 ptrdiff_t p = string_buffer_position (obj, start);
27914 if (p > 0)
27916 help = Fget_char_property (make_number (p),
27917 Qhelp_echo, w->buffer);
27918 if (!NILP (help))
27920 charpos = p;
27921 obj = w->buffer;
27926 else if (BUFFERP (obj)
27927 && charpos >= BEGV
27928 && charpos < ZV)
27929 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27930 obj);
27932 if (!NILP (help))
27934 help_echo_string = help;
27935 help_echo_window = window;
27936 help_echo_object = obj;
27937 help_echo_pos = charpos;
27942 #ifdef HAVE_WINDOW_SYSTEM
27943 /* Look for a `pointer' property. */
27944 if (FRAME_WINDOW_P (f) && NILP (pointer))
27946 /* Check overlays first. */
27947 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27948 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27950 if (NILP (pointer))
27952 Lisp_Object obj = glyph->object;
27953 ptrdiff_t charpos = glyph->charpos;
27955 /* Try text properties. */
27956 if (STRINGP (obj)
27957 && charpos >= 0
27958 && charpos < SCHARS (obj))
27960 pointer = Fget_text_property (make_number (charpos),
27961 Qpointer, obj);
27962 if (NILP (pointer))
27964 /* If the string itself doesn't specify a pointer,
27965 see if the buffer text ``under'' it does. */
27966 struct glyph_row *r
27967 = MATRIX_ROW (w->current_matrix, vpos);
27968 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27969 ptrdiff_t p = string_buffer_position (obj, start);
27970 if (p > 0)
27971 pointer = Fget_char_property (make_number (p),
27972 Qpointer, w->buffer);
27975 else if (BUFFERP (obj)
27976 && charpos >= BEGV
27977 && charpos < ZV)
27978 pointer = Fget_text_property (make_number (charpos),
27979 Qpointer, obj);
27982 #endif /* HAVE_WINDOW_SYSTEM */
27984 BEGV = obegv;
27985 ZV = ozv;
27986 current_buffer = obuf;
27989 set_cursor:
27991 #ifdef HAVE_WINDOW_SYSTEM
27992 if (FRAME_WINDOW_P (f))
27993 define_frame_cursor1 (f, cursor, pointer);
27994 #else
27995 /* This is here to prevent a compiler error, about "label at end of
27996 compound statement". */
27997 return;
27998 #endif
28002 /* EXPORT for RIF:
28003 Clear any mouse-face on window W. This function is part of the
28004 redisplay interface, and is called from try_window_id and similar
28005 functions to ensure the mouse-highlight is off. */
28007 void
28008 x_clear_window_mouse_face (struct window *w)
28010 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28011 Lisp_Object window;
28013 block_input ();
28014 XSETWINDOW (window, w);
28015 if (EQ (window, hlinfo->mouse_face_window))
28016 clear_mouse_face (hlinfo);
28017 unblock_input ();
28021 /* EXPORT:
28022 Just discard the mouse face information for frame F, if any.
28023 This is used when the size of F is changed. */
28025 void
28026 cancel_mouse_face (struct frame *f)
28028 Lisp_Object window;
28029 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28031 window = hlinfo->mouse_face_window;
28032 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28034 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28035 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28036 hlinfo->mouse_face_window = Qnil;
28042 /***********************************************************************
28043 Exposure Events
28044 ***********************************************************************/
28046 #ifdef HAVE_WINDOW_SYSTEM
28048 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28049 which intersects rectangle R. R is in window-relative coordinates. */
28051 static void
28052 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28053 enum glyph_row_area area)
28055 struct glyph *first = row->glyphs[area];
28056 struct glyph *end = row->glyphs[area] + row->used[area];
28057 struct glyph *last;
28058 int first_x, start_x, x;
28060 if (area == TEXT_AREA && row->fill_line_p)
28061 /* If row extends face to end of line write the whole line. */
28062 draw_glyphs (w, 0, row, area,
28063 0, row->used[area],
28064 DRAW_NORMAL_TEXT, 0);
28065 else
28067 /* Set START_X to the window-relative start position for drawing glyphs of
28068 AREA. The first glyph of the text area can be partially visible.
28069 The first glyphs of other areas cannot. */
28070 start_x = window_box_left_offset (w, area);
28071 x = start_x;
28072 if (area == TEXT_AREA)
28073 x += row->x;
28075 /* Find the first glyph that must be redrawn. */
28076 while (first < end
28077 && x + first->pixel_width < r->x)
28079 x += first->pixel_width;
28080 ++first;
28083 /* Find the last one. */
28084 last = first;
28085 first_x = x;
28086 while (last < end
28087 && x < r->x + r->width)
28089 x += last->pixel_width;
28090 ++last;
28093 /* Repaint. */
28094 if (last > first)
28095 draw_glyphs (w, first_x - start_x, row, area,
28096 first - row->glyphs[area], last - row->glyphs[area],
28097 DRAW_NORMAL_TEXT, 0);
28102 /* Redraw the parts of the glyph row ROW on window W intersecting
28103 rectangle R. R is in window-relative coordinates. Value is
28104 non-zero if mouse-face was overwritten. */
28106 static int
28107 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28109 eassert (row->enabled_p);
28111 if (row->mode_line_p || w->pseudo_window_p)
28112 draw_glyphs (w, 0, row, TEXT_AREA,
28113 0, row->used[TEXT_AREA],
28114 DRAW_NORMAL_TEXT, 0);
28115 else
28117 if (row->used[LEFT_MARGIN_AREA])
28118 expose_area (w, row, r, LEFT_MARGIN_AREA);
28119 if (row->used[TEXT_AREA])
28120 expose_area (w, row, r, TEXT_AREA);
28121 if (row->used[RIGHT_MARGIN_AREA])
28122 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28123 draw_row_fringe_bitmaps (w, row);
28126 return row->mouse_face_p;
28130 /* Redraw those parts of glyphs rows during expose event handling that
28131 overlap other rows. Redrawing of an exposed line writes over parts
28132 of lines overlapping that exposed line; this function fixes that.
28134 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28135 row in W's current matrix that is exposed and overlaps other rows.
28136 LAST_OVERLAPPING_ROW is the last such row. */
28138 static void
28139 expose_overlaps (struct window *w,
28140 struct glyph_row *first_overlapping_row,
28141 struct glyph_row *last_overlapping_row,
28142 XRectangle *r)
28144 struct glyph_row *row;
28146 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28147 if (row->overlapping_p)
28149 eassert (row->enabled_p && !row->mode_line_p);
28151 row->clip = r;
28152 if (row->used[LEFT_MARGIN_AREA])
28153 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28155 if (row->used[TEXT_AREA])
28156 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28158 if (row->used[RIGHT_MARGIN_AREA])
28159 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28160 row->clip = NULL;
28165 /* Return non-zero if W's cursor intersects rectangle R. */
28167 static int
28168 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28170 XRectangle cr, result;
28171 struct glyph *cursor_glyph;
28172 struct glyph_row *row;
28174 if (w->phys_cursor.vpos >= 0
28175 && w->phys_cursor.vpos < w->current_matrix->nrows
28176 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28177 row->enabled_p)
28178 && row->cursor_in_fringe_p)
28180 /* Cursor is in the fringe. */
28181 cr.x = window_box_right_offset (w,
28182 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28183 ? RIGHT_MARGIN_AREA
28184 : TEXT_AREA));
28185 cr.y = row->y;
28186 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28187 cr.height = row->height;
28188 return x_intersect_rectangles (&cr, r, &result);
28191 cursor_glyph = get_phys_cursor_glyph (w);
28192 if (cursor_glyph)
28194 /* r is relative to W's box, but w->phys_cursor.x is relative
28195 to left edge of W's TEXT area. Adjust it. */
28196 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28197 cr.y = w->phys_cursor.y;
28198 cr.width = cursor_glyph->pixel_width;
28199 cr.height = w->phys_cursor_height;
28200 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28201 I assume the effect is the same -- and this is portable. */
28202 return x_intersect_rectangles (&cr, r, &result);
28204 /* If we don't understand the format, pretend we're not in the hot-spot. */
28205 return 0;
28209 /* EXPORT:
28210 Draw a vertical window border to the right of window W if W doesn't
28211 have vertical scroll bars. */
28213 void
28214 x_draw_vertical_border (struct window *w)
28216 struct frame *f = XFRAME (WINDOW_FRAME (w));
28218 /* We could do better, if we knew what type of scroll-bar the adjacent
28219 windows (on either side) have... But we don't :-(
28220 However, I think this works ok. ++KFS 2003-04-25 */
28222 /* Redraw borders between horizontally adjacent windows. Don't
28223 do it for frames with vertical scroll bars because either the
28224 right scroll bar of a window, or the left scroll bar of its
28225 neighbor will suffice as a border. */
28226 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28227 return;
28229 if (!WINDOW_RIGHTMOST_P (w)
28230 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28232 int x0, x1, y0, y1;
28234 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28235 y1 -= 1;
28237 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28238 x1 -= 1;
28240 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28242 else if (!WINDOW_LEFTMOST_P (w)
28243 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28245 int x0, x1, y0, y1;
28247 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28248 y1 -= 1;
28250 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28251 x0 -= 1;
28253 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28258 /* Redraw the part of window W intersection rectangle FR. Pixel
28259 coordinates in FR are frame-relative. Call this function with
28260 input blocked. Value is non-zero if the exposure overwrites
28261 mouse-face. */
28263 static int
28264 expose_window (struct window *w, XRectangle *fr)
28266 struct frame *f = XFRAME (w->frame);
28267 XRectangle wr, r;
28268 int mouse_face_overwritten_p = 0;
28270 /* If window is not yet fully initialized, do nothing. This can
28271 happen when toolkit scroll bars are used and a window is split.
28272 Reconfiguring the scroll bar will generate an expose for a newly
28273 created window. */
28274 if (w->current_matrix == NULL)
28275 return 0;
28277 /* When we're currently updating the window, display and current
28278 matrix usually don't agree. Arrange for a thorough display
28279 later. */
28280 if (w == updated_window)
28282 SET_FRAME_GARBAGED (f);
28283 return 0;
28286 /* Frame-relative pixel rectangle of W. */
28287 wr.x = WINDOW_LEFT_EDGE_X (w);
28288 wr.y = WINDOW_TOP_EDGE_Y (w);
28289 wr.width = WINDOW_TOTAL_WIDTH (w);
28290 wr.height = WINDOW_TOTAL_HEIGHT (w);
28292 if (x_intersect_rectangles (fr, &wr, &r))
28294 int yb = window_text_bottom_y (w);
28295 struct glyph_row *row;
28296 int cursor_cleared_p, phys_cursor_on_p;
28297 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28299 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28300 r.x, r.y, r.width, r.height));
28302 /* Convert to window coordinates. */
28303 r.x -= WINDOW_LEFT_EDGE_X (w);
28304 r.y -= WINDOW_TOP_EDGE_Y (w);
28306 /* Turn off the cursor. */
28307 if (!w->pseudo_window_p
28308 && phys_cursor_in_rect_p (w, &r))
28310 x_clear_cursor (w);
28311 cursor_cleared_p = 1;
28313 else
28314 cursor_cleared_p = 0;
28316 /* If the row containing the cursor extends face to end of line,
28317 then expose_area might overwrite the cursor outside the
28318 rectangle and thus notice_overwritten_cursor might clear
28319 w->phys_cursor_on_p. We remember the original value and
28320 check later if it is changed. */
28321 phys_cursor_on_p = w->phys_cursor_on_p;
28323 /* Update lines intersecting rectangle R. */
28324 first_overlapping_row = last_overlapping_row = NULL;
28325 for (row = w->current_matrix->rows;
28326 row->enabled_p;
28327 ++row)
28329 int y0 = row->y;
28330 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28332 if ((y0 >= r.y && y0 < r.y + r.height)
28333 || (y1 > r.y && y1 < r.y + r.height)
28334 || (r.y >= y0 && r.y < y1)
28335 || (r.y + r.height > y0 && r.y + r.height < y1))
28337 /* A header line may be overlapping, but there is no need
28338 to fix overlapping areas for them. KFS 2005-02-12 */
28339 if (row->overlapping_p && !row->mode_line_p)
28341 if (first_overlapping_row == NULL)
28342 first_overlapping_row = row;
28343 last_overlapping_row = row;
28346 row->clip = fr;
28347 if (expose_line (w, row, &r))
28348 mouse_face_overwritten_p = 1;
28349 row->clip = NULL;
28351 else if (row->overlapping_p)
28353 /* We must redraw a row overlapping the exposed area. */
28354 if (y0 < r.y
28355 ? y0 + row->phys_height > r.y
28356 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28358 if (first_overlapping_row == NULL)
28359 first_overlapping_row = row;
28360 last_overlapping_row = row;
28364 if (y1 >= yb)
28365 break;
28368 /* Display the mode line if there is one. */
28369 if (WINDOW_WANTS_MODELINE_P (w)
28370 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28371 row->enabled_p)
28372 && row->y < r.y + r.height)
28374 if (expose_line (w, row, &r))
28375 mouse_face_overwritten_p = 1;
28378 if (!w->pseudo_window_p)
28380 /* Fix the display of overlapping rows. */
28381 if (first_overlapping_row)
28382 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28383 fr);
28385 /* Draw border between windows. */
28386 x_draw_vertical_border (w);
28388 /* Turn the cursor on again. */
28389 if (cursor_cleared_p
28390 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28391 update_window_cursor (w, 1);
28395 return mouse_face_overwritten_p;
28400 /* Redraw (parts) of all windows in the window tree rooted at W that
28401 intersect R. R contains frame pixel coordinates. Value is
28402 non-zero if the exposure overwrites mouse-face. */
28404 static int
28405 expose_window_tree (struct window *w, XRectangle *r)
28407 struct frame *f = XFRAME (w->frame);
28408 int mouse_face_overwritten_p = 0;
28410 while (w && !FRAME_GARBAGED_P (f))
28412 if (!NILP (w->hchild))
28413 mouse_face_overwritten_p
28414 |= expose_window_tree (XWINDOW (w->hchild), r);
28415 else if (!NILP (w->vchild))
28416 mouse_face_overwritten_p
28417 |= expose_window_tree (XWINDOW (w->vchild), r);
28418 else
28419 mouse_face_overwritten_p |= expose_window (w, r);
28421 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28424 return mouse_face_overwritten_p;
28428 /* EXPORT:
28429 Redisplay an exposed area of frame F. X and Y are the upper-left
28430 corner of the exposed rectangle. W and H are width and height of
28431 the exposed area. All are pixel values. W or H zero means redraw
28432 the entire frame. */
28434 void
28435 expose_frame (struct frame *f, int x, int y, int w, int h)
28437 XRectangle r;
28438 int mouse_face_overwritten_p = 0;
28440 TRACE ((stderr, "expose_frame "));
28442 /* No need to redraw if frame will be redrawn soon. */
28443 if (FRAME_GARBAGED_P (f))
28445 TRACE ((stderr, " garbaged\n"));
28446 return;
28449 /* If basic faces haven't been realized yet, there is no point in
28450 trying to redraw anything. This can happen when we get an expose
28451 event while Emacs is starting, e.g. by moving another window. */
28452 if (FRAME_FACE_CACHE (f) == NULL
28453 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28455 TRACE ((stderr, " no faces\n"));
28456 return;
28459 if (w == 0 || h == 0)
28461 r.x = r.y = 0;
28462 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28463 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28465 else
28467 r.x = x;
28468 r.y = y;
28469 r.width = w;
28470 r.height = h;
28473 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28474 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28476 if (WINDOWP (f->tool_bar_window))
28477 mouse_face_overwritten_p
28478 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28480 #ifdef HAVE_X_WINDOWS
28481 #ifndef MSDOS
28482 #ifndef USE_X_TOOLKIT
28483 if (WINDOWP (f->menu_bar_window))
28484 mouse_face_overwritten_p
28485 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28486 #endif /* not USE_X_TOOLKIT */
28487 #endif
28488 #endif
28490 /* Some window managers support a focus-follows-mouse style with
28491 delayed raising of frames. Imagine a partially obscured frame,
28492 and moving the mouse into partially obscured mouse-face on that
28493 frame. The visible part of the mouse-face will be highlighted,
28494 then the WM raises the obscured frame. With at least one WM, KDE
28495 2.1, Emacs is not getting any event for the raising of the frame
28496 (even tried with SubstructureRedirectMask), only Expose events.
28497 These expose events will draw text normally, i.e. not
28498 highlighted. Which means we must redo the highlight here.
28499 Subsume it under ``we love X''. --gerd 2001-08-15 */
28500 /* Included in Windows version because Windows most likely does not
28501 do the right thing if any third party tool offers
28502 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28503 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28505 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28506 if (f == hlinfo->mouse_face_mouse_frame)
28508 int mouse_x = hlinfo->mouse_face_mouse_x;
28509 int mouse_y = hlinfo->mouse_face_mouse_y;
28510 clear_mouse_face (hlinfo);
28511 note_mouse_highlight (f, mouse_x, mouse_y);
28517 /* EXPORT:
28518 Determine the intersection of two rectangles R1 and R2. Return
28519 the intersection in *RESULT. Value is non-zero if RESULT is not
28520 empty. */
28523 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28525 XRectangle *left, *right;
28526 XRectangle *upper, *lower;
28527 int intersection_p = 0;
28529 /* Rearrange so that R1 is the left-most rectangle. */
28530 if (r1->x < r2->x)
28531 left = r1, right = r2;
28532 else
28533 left = r2, right = r1;
28535 /* X0 of the intersection is right.x0, if this is inside R1,
28536 otherwise there is no intersection. */
28537 if (right->x <= left->x + left->width)
28539 result->x = right->x;
28541 /* The right end of the intersection is the minimum of
28542 the right ends of left and right. */
28543 result->width = (min (left->x + left->width, right->x + right->width)
28544 - result->x);
28546 /* Same game for Y. */
28547 if (r1->y < r2->y)
28548 upper = r1, lower = r2;
28549 else
28550 upper = r2, lower = r1;
28552 /* The upper end of the intersection is lower.y0, if this is inside
28553 of upper. Otherwise, there is no intersection. */
28554 if (lower->y <= upper->y + upper->height)
28556 result->y = lower->y;
28558 /* The lower end of the intersection is the minimum of the lower
28559 ends of upper and lower. */
28560 result->height = (min (lower->y + lower->height,
28561 upper->y + upper->height)
28562 - result->y);
28563 intersection_p = 1;
28567 return intersection_p;
28570 #endif /* HAVE_WINDOW_SYSTEM */
28573 /***********************************************************************
28574 Initialization
28575 ***********************************************************************/
28577 void
28578 syms_of_xdisp (void)
28580 Vwith_echo_area_save_vector = Qnil;
28581 staticpro (&Vwith_echo_area_save_vector);
28583 Vmessage_stack = Qnil;
28584 staticpro (&Vmessage_stack);
28586 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28587 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28589 message_dolog_marker1 = Fmake_marker ();
28590 staticpro (&message_dolog_marker1);
28591 message_dolog_marker2 = Fmake_marker ();
28592 staticpro (&message_dolog_marker2);
28593 message_dolog_marker3 = Fmake_marker ();
28594 staticpro (&message_dolog_marker3);
28596 #ifdef GLYPH_DEBUG
28597 defsubr (&Sdump_frame_glyph_matrix);
28598 defsubr (&Sdump_glyph_matrix);
28599 defsubr (&Sdump_glyph_row);
28600 defsubr (&Sdump_tool_bar_row);
28601 defsubr (&Strace_redisplay);
28602 defsubr (&Strace_to_stderr);
28603 #endif
28604 #ifdef HAVE_WINDOW_SYSTEM
28605 defsubr (&Stool_bar_lines_needed);
28606 defsubr (&Slookup_image_map);
28607 #endif
28608 defsubr (&Sformat_mode_line);
28609 defsubr (&Sinvisible_p);
28610 defsubr (&Scurrent_bidi_paragraph_direction);
28612 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28613 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28614 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28615 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28616 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28617 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28618 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28619 DEFSYM (Qeval, "eval");
28620 DEFSYM (QCdata, ":data");
28621 DEFSYM (Qdisplay, "display");
28622 DEFSYM (Qspace_width, "space-width");
28623 DEFSYM (Qraise, "raise");
28624 DEFSYM (Qslice, "slice");
28625 DEFSYM (Qspace, "space");
28626 DEFSYM (Qmargin, "margin");
28627 DEFSYM (Qpointer, "pointer");
28628 DEFSYM (Qleft_margin, "left-margin");
28629 DEFSYM (Qright_margin, "right-margin");
28630 DEFSYM (Qcenter, "center");
28631 DEFSYM (Qline_height, "line-height");
28632 DEFSYM (QCalign_to, ":align-to");
28633 DEFSYM (QCrelative_width, ":relative-width");
28634 DEFSYM (QCrelative_height, ":relative-height");
28635 DEFSYM (QCeval, ":eval");
28636 DEFSYM (QCpropertize, ":propertize");
28637 DEFSYM (QCfile, ":file");
28638 DEFSYM (Qfontified, "fontified");
28639 DEFSYM (Qfontification_functions, "fontification-functions");
28640 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28641 DEFSYM (Qescape_glyph, "escape-glyph");
28642 DEFSYM (Qnobreak_space, "nobreak-space");
28643 DEFSYM (Qimage, "image");
28644 DEFSYM (Qtext, "text");
28645 DEFSYM (Qboth, "both");
28646 DEFSYM (Qboth_horiz, "both-horiz");
28647 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28648 DEFSYM (QCmap, ":map");
28649 DEFSYM (QCpointer, ":pointer");
28650 DEFSYM (Qrect, "rect");
28651 DEFSYM (Qcircle, "circle");
28652 DEFSYM (Qpoly, "poly");
28653 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28654 DEFSYM (Qgrow_only, "grow-only");
28655 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28656 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28657 DEFSYM (Qposition, "position");
28658 DEFSYM (Qbuffer_position, "buffer-position");
28659 DEFSYM (Qobject, "object");
28660 DEFSYM (Qbar, "bar");
28661 DEFSYM (Qhbar, "hbar");
28662 DEFSYM (Qbox, "box");
28663 DEFSYM (Qhollow, "hollow");
28664 DEFSYM (Qhand, "hand");
28665 DEFSYM (Qarrow, "arrow");
28666 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28668 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28669 Fcons (intern_c_string ("void-variable"), Qnil)),
28670 Qnil);
28671 staticpro (&list_of_error);
28673 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28674 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28675 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28676 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28678 echo_buffer[0] = echo_buffer[1] = Qnil;
28679 staticpro (&echo_buffer[0]);
28680 staticpro (&echo_buffer[1]);
28682 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28683 staticpro (&echo_area_buffer[0]);
28684 staticpro (&echo_area_buffer[1]);
28686 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28687 staticpro (&Vmessages_buffer_name);
28689 mode_line_proptrans_alist = Qnil;
28690 staticpro (&mode_line_proptrans_alist);
28691 mode_line_string_list = Qnil;
28692 staticpro (&mode_line_string_list);
28693 mode_line_string_face = Qnil;
28694 staticpro (&mode_line_string_face);
28695 mode_line_string_face_prop = Qnil;
28696 staticpro (&mode_line_string_face_prop);
28697 Vmode_line_unwind_vector = Qnil;
28698 staticpro (&Vmode_line_unwind_vector);
28700 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28702 help_echo_string = Qnil;
28703 staticpro (&help_echo_string);
28704 help_echo_object = Qnil;
28705 staticpro (&help_echo_object);
28706 help_echo_window = Qnil;
28707 staticpro (&help_echo_window);
28708 previous_help_echo_string = Qnil;
28709 staticpro (&previous_help_echo_string);
28710 help_echo_pos = -1;
28712 DEFSYM (Qright_to_left, "right-to-left");
28713 DEFSYM (Qleft_to_right, "left-to-right");
28715 #ifdef HAVE_WINDOW_SYSTEM
28716 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28717 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28718 For example, if a block cursor is over a tab, it will be drawn as
28719 wide as that tab on the display. */);
28720 x_stretch_cursor_p = 0;
28721 #endif
28723 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28724 doc: /* Non-nil means highlight trailing whitespace.
28725 The face used for trailing whitespace is `trailing-whitespace'. */);
28726 Vshow_trailing_whitespace = Qnil;
28728 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28729 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28730 If the value is t, Emacs highlights non-ASCII chars which have the
28731 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28732 or `escape-glyph' face respectively.
28734 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28735 U+2011 (non-breaking hyphen) are affected.
28737 Any other non-nil value means to display these characters as a escape
28738 glyph followed by an ordinary space or hyphen.
28740 A value of nil means no special handling of these characters. */);
28741 Vnobreak_char_display = Qt;
28743 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28744 doc: /* The pointer shape to show in void text areas.
28745 A value of nil means to show the text pointer. Other options are `arrow',
28746 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28747 Vvoid_text_area_pointer = Qarrow;
28749 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28750 doc: /* Non-nil means don't actually do any redisplay.
28751 This is used for internal purposes. */);
28752 Vinhibit_redisplay = Qnil;
28754 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28755 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28756 Vglobal_mode_string = Qnil;
28758 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28759 doc: /* Marker for where to display an arrow on top of the buffer text.
28760 This must be the beginning of a line in order to work.
28761 See also `overlay-arrow-string'. */);
28762 Voverlay_arrow_position = Qnil;
28764 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28765 doc: /* String to display as an arrow in non-window frames.
28766 See also `overlay-arrow-position'. */);
28767 Voverlay_arrow_string = build_pure_c_string ("=>");
28769 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28770 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28771 The symbols on this list are examined during redisplay to determine
28772 where to display overlay arrows. */);
28773 Voverlay_arrow_variable_list
28774 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28776 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28777 doc: /* The number of lines to try scrolling a window by when point moves out.
28778 If that fails to bring point back on frame, point is centered instead.
28779 If this is zero, point is always centered after it moves off frame.
28780 If you want scrolling to always be a line at a time, you should set
28781 `scroll-conservatively' to a large value rather than set this to 1. */);
28783 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28784 doc: /* Scroll up to this many lines, to bring point back on screen.
28785 If point moves off-screen, redisplay will scroll by up to
28786 `scroll-conservatively' lines in order to bring point just barely
28787 onto the screen again. If that cannot be done, then redisplay
28788 recenters point as usual.
28790 If the value is greater than 100, redisplay will never recenter point,
28791 but will always scroll just enough text to bring point into view, even
28792 if you move far away.
28794 A value of zero means always recenter point if it moves off screen. */);
28795 scroll_conservatively = 0;
28797 DEFVAR_INT ("scroll-margin", scroll_margin,
28798 doc: /* Number of lines of margin at the top and bottom of a window.
28799 Recenter the window whenever point gets within this many lines
28800 of the top or bottom of the window. */);
28801 scroll_margin = 0;
28803 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28804 doc: /* Pixels per inch value for non-window system displays.
28805 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28806 Vdisplay_pixels_per_inch = make_float (72.0);
28808 #ifdef GLYPH_DEBUG
28809 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28810 #endif
28812 DEFVAR_LISP ("truncate-partial-width-windows",
28813 Vtruncate_partial_width_windows,
28814 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28815 For an integer value, truncate lines in each window narrower than the
28816 full frame width, provided the window width is less than that integer;
28817 otherwise, respect the value of `truncate-lines'.
28819 For any other non-nil value, truncate lines in all windows that do
28820 not span the full frame width.
28822 A value of nil means to respect the value of `truncate-lines'.
28824 If `word-wrap' is enabled, you might want to reduce this. */);
28825 Vtruncate_partial_width_windows = make_number (50);
28827 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28828 doc: /* Maximum buffer size for which line number should be displayed.
28829 If the buffer is bigger than this, the line number does not appear
28830 in the mode line. A value of nil means no limit. */);
28831 Vline_number_display_limit = Qnil;
28833 DEFVAR_INT ("line-number-display-limit-width",
28834 line_number_display_limit_width,
28835 doc: /* Maximum line width (in characters) for line number display.
28836 If the average length of the lines near point is bigger than this, then the
28837 line number may be omitted from the mode line. */);
28838 line_number_display_limit_width = 200;
28840 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28841 doc: /* Non-nil means highlight region even in nonselected windows. */);
28842 highlight_nonselected_windows = 0;
28844 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28845 doc: /* Non-nil if more than one frame is visible on this display.
28846 Minibuffer-only frames don't count, but iconified frames do.
28847 This variable is not guaranteed to be accurate except while processing
28848 `frame-title-format' and `icon-title-format'. */);
28850 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28851 doc: /* Template for displaying the title bar of visible frames.
28852 \(Assuming the window manager supports this feature.)
28854 This variable has the same structure as `mode-line-format', except that
28855 the %c and %l constructs are ignored. It is used only on frames for
28856 which no explicit name has been set \(see `modify-frame-parameters'). */);
28858 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28859 doc: /* Template for displaying the title bar of an iconified frame.
28860 \(Assuming the window manager supports this feature.)
28861 This variable has the same structure as `mode-line-format' (which see),
28862 and is used only on frames for which no explicit name has been set
28863 \(see `modify-frame-parameters'). */);
28864 Vicon_title_format
28865 = Vframe_title_format
28866 = listn (CONSTYPE_PURE, 3,
28867 intern_c_string ("multiple-frames"),
28868 build_pure_c_string ("%b"),
28869 listn (CONSTYPE_PURE, 4,
28870 empty_unibyte_string,
28871 intern_c_string ("invocation-name"),
28872 build_pure_c_string ("@"),
28873 intern_c_string ("system-name")));
28875 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28876 doc: /* Maximum number of lines to keep in the message log buffer.
28877 If nil, disable message logging. If t, log messages but don't truncate
28878 the buffer when it becomes large. */);
28879 Vmessage_log_max = make_number (1000);
28881 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28882 doc: /* Functions called before redisplay, if window sizes have changed.
28883 The value should be a list of functions that take one argument.
28884 Just before redisplay, for each frame, if any of its windows have changed
28885 size since the last redisplay, or have been split or deleted,
28886 all the functions in the list are called, with the frame as argument. */);
28887 Vwindow_size_change_functions = Qnil;
28889 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28890 doc: /* List of functions to call before redisplaying a window with scrolling.
28891 Each function is called with two arguments, the window and its new
28892 display-start position. Note that these functions are also called by
28893 `set-window-buffer'. Also note that the value of `window-end' is not
28894 valid when these functions are called.
28896 Warning: Do not use this feature to alter the way the window
28897 is scrolled. It is not designed for that, and such use probably won't
28898 work. */);
28899 Vwindow_scroll_functions = Qnil;
28901 DEFVAR_LISP ("window-text-change-functions",
28902 Vwindow_text_change_functions,
28903 doc: /* Functions to call in redisplay when text in the window might change. */);
28904 Vwindow_text_change_functions = Qnil;
28906 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28907 doc: /* Functions called when redisplay of a window reaches the end trigger.
28908 Each function is called with two arguments, the window and the end trigger value.
28909 See `set-window-redisplay-end-trigger'. */);
28910 Vredisplay_end_trigger_functions = Qnil;
28912 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28913 doc: /* Non-nil means autoselect window with mouse pointer.
28914 If nil, do not autoselect windows.
28915 A positive number means delay autoselection by that many seconds: a
28916 window is autoselected only after the mouse has remained in that
28917 window for the duration of the delay.
28918 A negative number has a similar effect, but causes windows to be
28919 autoselected only after the mouse has stopped moving. \(Because of
28920 the way Emacs compares mouse events, you will occasionally wait twice
28921 that time before the window gets selected.\)
28922 Any other value means to autoselect window instantaneously when the
28923 mouse pointer enters it.
28925 Autoselection selects the minibuffer only if it is active, and never
28926 unselects the minibuffer if it is active.
28928 When customizing this variable make sure that the actual value of
28929 `focus-follows-mouse' matches the behavior of your window manager. */);
28930 Vmouse_autoselect_window = Qnil;
28932 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28933 doc: /* Non-nil means automatically resize tool-bars.
28934 This dynamically changes the tool-bar's height to the minimum height
28935 that is needed to make all tool-bar items visible.
28936 If value is `grow-only', the tool-bar's height is only increased
28937 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28938 Vauto_resize_tool_bars = Qt;
28940 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28941 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28942 auto_raise_tool_bar_buttons_p = 1;
28944 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28945 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28946 make_cursor_line_fully_visible_p = 1;
28948 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28949 doc: /* Border below tool-bar in pixels.
28950 If an integer, use it as the height of the border.
28951 If it is one of `internal-border-width' or `border-width', use the
28952 value of the corresponding frame parameter.
28953 Otherwise, no border is added below the tool-bar. */);
28954 Vtool_bar_border = Qinternal_border_width;
28956 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28957 doc: /* Margin around tool-bar buttons in pixels.
28958 If an integer, use that for both horizontal and vertical margins.
28959 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28960 HORZ specifying the horizontal margin, and VERT specifying the
28961 vertical margin. */);
28962 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28964 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28965 doc: /* Relief thickness of tool-bar buttons. */);
28966 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28968 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28969 doc: /* Tool bar style to use.
28970 It can be one of
28971 image - show images only
28972 text - show text only
28973 both - show both, text below image
28974 both-horiz - show text to the right of the image
28975 text-image-horiz - show text to the left of the image
28976 any other - use system default or image if no system default.
28978 This variable only affects the GTK+ toolkit version of Emacs. */);
28979 Vtool_bar_style = Qnil;
28981 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28982 doc: /* Maximum number of characters a label can have to be shown.
28983 The tool bar style must also show labels for this to have any effect, see
28984 `tool-bar-style'. */);
28985 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28987 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28988 doc: /* List of functions to call to fontify regions of text.
28989 Each function is called with one argument POS. Functions must
28990 fontify a region starting at POS in the current buffer, and give
28991 fontified regions the property `fontified'. */);
28992 Vfontification_functions = Qnil;
28993 Fmake_variable_buffer_local (Qfontification_functions);
28995 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28996 unibyte_display_via_language_environment,
28997 doc: /* Non-nil means display unibyte text according to language environment.
28998 Specifically, this means that raw bytes in the range 160-255 decimal
28999 are displayed by converting them to the equivalent multibyte characters
29000 according to the current language environment. As a result, they are
29001 displayed according to the current fontset.
29003 Note that this variable affects only how these bytes are displayed,
29004 but does not change the fact they are interpreted as raw bytes. */);
29005 unibyte_display_via_language_environment = 0;
29007 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29008 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29009 If a float, it specifies a fraction of the mini-window frame's height.
29010 If an integer, it specifies a number of lines. */);
29011 Vmax_mini_window_height = make_float (0.25);
29013 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29014 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29015 A value of nil means don't automatically resize mini-windows.
29016 A value of t means resize them to fit the text displayed in them.
29017 A value of `grow-only', the default, means let mini-windows grow only;
29018 they return to their normal size when the minibuffer is closed, or the
29019 echo area becomes empty. */);
29020 Vresize_mini_windows = Qgrow_only;
29022 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29023 doc: /* Alist specifying how to blink the cursor off.
29024 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29025 `cursor-type' frame-parameter or variable equals ON-STATE,
29026 comparing using `equal', Emacs uses OFF-STATE to specify
29027 how to blink it off. ON-STATE and OFF-STATE are values for
29028 the `cursor-type' frame parameter.
29030 If a frame's ON-STATE has no entry in this list,
29031 the frame's other specifications determine how to blink the cursor off. */);
29032 Vblink_cursor_alist = Qnil;
29034 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29035 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29036 If non-nil, windows are automatically scrolled horizontally to make
29037 point visible. */);
29038 automatic_hscrolling_p = 1;
29039 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29041 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29042 doc: /* How many columns away from the window edge point is allowed to get
29043 before automatic hscrolling will horizontally scroll the window. */);
29044 hscroll_margin = 5;
29046 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29047 doc: /* How many columns to scroll the window when point gets too close to the edge.
29048 When point is less than `hscroll-margin' columns from the window
29049 edge, automatic hscrolling will scroll the window by the amount of columns
29050 determined by this variable. If its value is a positive integer, scroll that
29051 many columns. If it's a positive floating-point number, it specifies the
29052 fraction of the window's width to scroll. If it's nil or zero, point will be
29053 centered horizontally after the scroll. Any other value, including negative
29054 numbers, are treated as if the value were zero.
29056 Automatic hscrolling always moves point outside the scroll margin, so if
29057 point was more than scroll step columns inside the margin, the window will
29058 scroll more than the value given by the scroll step.
29060 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29061 and `scroll-right' overrides this variable's effect. */);
29062 Vhscroll_step = make_number (0);
29064 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29065 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29066 Bind this around calls to `message' to let it take effect. */);
29067 message_truncate_lines = 0;
29069 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29070 doc: /* Normal hook run to update the menu bar definitions.
29071 Redisplay runs this hook before it redisplays the menu bar.
29072 This is used to update submenus such as Buffers,
29073 whose contents depend on various data. */);
29074 Vmenu_bar_update_hook = Qnil;
29076 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29077 doc: /* Frame for which we are updating a menu.
29078 The enable predicate for a menu binding should check this variable. */);
29079 Vmenu_updating_frame = Qnil;
29081 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29082 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29083 inhibit_menubar_update = 0;
29085 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29086 doc: /* Prefix prepended to all continuation lines at display time.
29087 The value may be a string, an image, or a stretch-glyph; it is
29088 interpreted in the same way as the value of a `display' text property.
29090 This variable is overridden by any `wrap-prefix' text or overlay
29091 property.
29093 To add a prefix to non-continuation lines, use `line-prefix'. */);
29094 Vwrap_prefix = Qnil;
29095 DEFSYM (Qwrap_prefix, "wrap-prefix");
29096 Fmake_variable_buffer_local (Qwrap_prefix);
29098 DEFVAR_LISP ("line-prefix", Vline_prefix,
29099 doc: /* Prefix prepended to all non-continuation lines at display time.
29100 The value may be a string, an image, or a stretch-glyph; it is
29101 interpreted in the same way as the value of a `display' text property.
29103 This variable is overridden by any `line-prefix' text or overlay
29104 property.
29106 To add a prefix to continuation lines, use `wrap-prefix'. */);
29107 Vline_prefix = Qnil;
29108 DEFSYM (Qline_prefix, "line-prefix");
29109 Fmake_variable_buffer_local (Qline_prefix);
29111 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29112 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29113 inhibit_eval_during_redisplay = 0;
29115 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29116 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29117 inhibit_free_realized_faces = 0;
29119 #ifdef GLYPH_DEBUG
29120 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29121 doc: /* Inhibit try_window_id display optimization. */);
29122 inhibit_try_window_id = 0;
29124 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29125 doc: /* Inhibit try_window_reusing display optimization. */);
29126 inhibit_try_window_reusing = 0;
29128 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29129 doc: /* Inhibit try_cursor_movement display optimization. */);
29130 inhibit_try_cursor_movement = 0;
29131 #endif /* GLYPH_DEBUG */
29133 DEFVAR_INT ("overline-margin", overline_margin,
29134 doc: /* Space between overline and text, in pixels.
29135 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29136 margin to the character height. */);
29137 overline_margin = 2;
29139 DEFVAR_INT ("underline-minimum-offset",
29140 underline_minimum_offset,
29141 doc: /* Minimum distance between baseline and underline.
29142 This can improve legibility of underlined text at small font sizes,
29143 particularly when using variable `x-use-underline-position-properties'
29144 with fonts that specify an UNDERLINE_POSITION relatively close to the
29145 baseline. The default value is 1. */);
29146 underline_minimum_offset = 1;
29148 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29149 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29150 This feature only works when on a window system that can change
29151 cursor shapes. */);
29152 display_hourglass_p = 1;
29154 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29155 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29156 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29158 hourglass_atimer = NULL;
29159 hourglass_shown_p = 0;
29161 DEFSYM (Qglyphless_char, "glyphless-char");
29162 DEFSYM (Qhex_code, "hex-code");
29163 DEFSYM (Qempty_box, "empty-box");
29164 DEFSYM (Qthin_space, "thin-space");
29165 DEFSYM (Qzero_width, "zero-width");
29167 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29168 /* Intern this now in case it isn't already done.
29169 Setting this variable twice is harmless.
29170 But don't staticpro it here--that is done in alloc.c. */
29171 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29172 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29174 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29175 doc: /* Char-table defining glyphless characters.
29176 Each element, if non-nil, should be one of the following:
29177 an ASCII acronym string: display this string in a box
29178 `hex-code': display the hexadecimal code of a character in a box
29179 `empty-box': display as an empty box
29180 `thin-space': display as 1-pixel width space
29181 `zero-width': don't display
29182 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29183 display method for graphical terminals and text terminals respectively.
29184 GRAPHICAL and TEXT should each have one of the values listed above.
29186 The char-table has one extra slot to control the display of a character for
29187 which no font is found. This slot only takes effect on graphical terminals.
29188 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29189 `thin-space'. The default is `empty-box'. */);
29190 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29191 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29192 Qempty_box);
29194 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29195 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29196 Vdebug_on_message = Qnil;
29200 /* Initialize this module when Emacs starts. */
29202 void
29203 init_xdisp (void)
29205 current_header_line_height = current_mode_line_height = -1;
29207 CHARPOS (this_line_start_pos) = 0;
29209 if (!noninteractive)
29211 struct window *m = XWINDOW (minibuf_window);
29212 Lisp_Object frame = m->frame;
29213 struct frame *f = XFRAME (frame);
29214 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29215 struct window *r = XWINDOW (root);
29216 int i;
29218 echo_area_window = minibuf_window;
29220 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29221 wset_total_lines
29222 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29223 wset_total_cols (r, make_number (FRAME_COLS (f)));
29224 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29225 wset_total_lines (m, make_number (1));
29226 wset_total_cols (m, make_number (FRAME_COLS (f)));
29228 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29229 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29230 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29232 /* The default ellipsis glyphs `...'. */
29233 for (i = 0; i < 3; ++i)
29234 default_invis_vector[i] = make_number ('.');
29238 /* Allocate the buffer for frame titles.
29239 Also used for `format-mode-line'. */
29240 int size = 100;
29241 mode_line_noprop_buf = xmalloc (size);
29242 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29243 mode_line_noprop_ptr = mode_line_noprop_buf;
29244 mode_line_target = MODE_LINE_DISPLAY;
29247 help_echo_showing_p = 0;
29250 /* Platform-independent portion of hourglass implementation. */
29252 /* Cancel a currently active hourglass timer, and start a new one. */
29253 void
29254 start_hourglass (void)
29256 #if defined (HAVE_WINDOW_SYSTEM)
29257 EMACS_TIME delay;
29259 cancel_hourglass ();
29261 if (INTEGERP (Vhourglass_delay)
29262 && XINT (Vhourglass_delay) > 0)
29263 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29264 TYPE_MAXIMUM (time_t)),
29266 else if (FLOATP (Vhourglass_delay)
29267 && XFLOAT_DATA (Vhourglass_delay) > 0)
29268 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29269 else
29270 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29272 #ifdef HAVE_NTGUI
29274 extern void w32_note_current_window (void);
29275 w32_note_current_window ();
29277 #endif /* HAVE_NTGUI */
29279 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29280 show_hourglass, NULL);
29281 #endif
29285 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29286 shown. */
29287 void
29288 cancel_hourglass (void)
29290 #if defined (HAVE_WINDOW_SYSTEM)
29291 if (hourglass_atimer)
29293 cancel_atimer (hourglass_atimer);
29294 hourglass_atimer = NULL;
29297 if (hourglass_shown_p)
29298 hide_hourglass ();
29299 #endif