Prevent redisplay and keystroke echo during menu navigation.
[emacs.git] / src / xdisp.c
blob139218ae1ddcfb82515ff4dc34a387d08943f6bb
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 #ifndef FRAME_X_OUTPUT
317 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
318 #endif
320 #define INFINITY 10000000
322 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
323 Lisp_Object Qwindow_scroll_functions;
324 static Lisp_Object Qwindow_text_change_functions;
325 static Lisp_Object Qredisplay_end_trigger_functions;
326 Lisp_Object Qinhibit_point_motion_hooks;
327 static Lisp_Object QCeval, QCpropertize;
328 Lisp_Object QCfile, QCdata;
329 static Lisp_Object Qfontified;
330 static Lisp_Object Qgrow_only;
331 static Lisp_Object Qinhibit_eval_during_redisplay;
332 static Lisp_Object Qbuffer_position, Qposition, Qobject;
333 static Lisp_Object Qright_to_left, Qleft_to_right;
335 /* Cursor shapes. */
336 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338 /* Pointer shapes. */
339 static Lisp_Object Qarrow, Qhand;
340 Lisp_Object Qtext;
342 /* Holds the list (error). */
343 static Lisp_Object list_of_error;
345 static Lisp_Object Qfontification_functions;
347 static Lisp_Object Qwrap_prefix;
348 static Lisp_Object Qline_prefix;
349 static Lisp_Object Qredisplay_internal;
351 /* Non-nil means don't actually do any redisplay. */
353 Lisp_Object Qinhibit_redisplay;
355 /* Names of text properties relevant for redisplay. */
357 Lisp_Object Qdisplay;
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
368 #ifdef HAVE_WINDOW_SYSTEM
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x)
381 #else /* !HAVE_WINDOW_SYSTEM */
382 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
383 #endif /* HAVE_WINDOW_SYSTEM */
385 /* Test if the display element loaded in IT, or the underlying buffer
386 or string character, is a space or a TAB character. This is used
387 to determine where word wrapping can occur. */
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
391 || ((STRINGP (it->string) \
392 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
393 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
394 || (it->s \
395 && (it->s[IT_BYTEPOS (*it)] == ' ' \
396 || it->s[IT_BYTEPOS (*it)] == '\t')) \
397 || (IT_BYTEPOS (*it) < ZV_BYTE \
398 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
399 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
401 /* Name of the face used to highlight trailing whitespace. */
403 static Lisp_Object Qtrailing_whitespace;
405 /* Name and number of the face used to highlight escape glyphs. */
407 static Lisp_Object Qescape_glyph;
409 /* Name and number of the face used to highlight non-breaking spaces. */
411 static Lisp_Object Qnobreak_space;
413 /* The symbol `image' which is the car of the lists used to represent
414 images in Lisp. Also a tool bar style. */
416 Lisp_Object Qimage;
418 /* The image map types. */
419 Lisp_Object QCmap;
420 static Lisp_Object QCpointer;
421 static Lisp_Object Qrect, Qcircle, Qpoly;
423 /* Tool bar styles */
424 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
426 /* Non-zero means print newline to stdout before next mini-buffer
427 message. */
429 int noninteractive_need_newline;
431 /* Non-zero means print newline to message log before next message. */
433 static int message_log_need_newline;
435 /* Three markers that message_dolog uses.
436 It could allocate them itself, but that causes trouble
437 in handling memory-full errors. */
438 static Lisp_Object message_dolog_marker1;
439 static Lisp_Object message_dolog_marker2;
440 static Lisp_Object message_dolog_marker3;
442 /* The buffer position of the first character appearing entirely or
443 partially on the line of the selected window which contains the
444 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
445 redisplay optimization in redisplay_internal. */
447 static struct text_pos this_line_start_pos;
449 /* Number of characters past the end of the line above, including the
450 terminating newline. */
452 static struct text_pos this_line_end_pos;
454 /* The vertical positions and the height of this line. */
456 static int this_line_vpos;
457 static int this_line_y;
458 static int this_line_pixel_height;
460 /* X position at which this display line starts. Usually zero;
461 negative if first character is partially visible. */
463 static int this_line_start_x;
465 /* The smallest character position seen by move_it_* functions as they
466 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
467 hscrolled lines, see display_line. */
469 static struct text_pos this_line_min_pos;
471 /* Buffer that this_line_.* variables are referring to. */
473 static struct buffer *this_line_buffer;
476 /* Values of those variables at last redisplay are stored as
477 properties on `overlay-arrow-position' symbol. However, if
478 Voverlay_arrow_position is a marker, last-arrow-position is its
479 numerical position. */
481 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
483 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
484 properties on a symbol in overlay-arrow-variable-list. */
486 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
488 Lisp_Object Qmenu_bar_update_hook;
490 /* Nonzero if an overlay arrow has been displayed in this window. */
492 static int overlay_arrow_seen;
494 /* Vector containing glyphs for an ellipsis `...'. */
496 static Lisp_Object default_invis_vector[3];
498 /* This is the window where the echo area message was displayed. It
499 is always a mini-buffer window, but it may not be the same window
500 currently active as a mini-buffer. */
502 Lisp_Object echo_area_window;
504 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
505 pushes the current message and the value of
506 message_enable_multibyte on the stack, the function restore_message
507 pops the stack and displays MESSAGE again. */
509 static Lisp_Object Vmessage_stack;
511 /* Nonzero means multibyte characters were enabled when the echo area
512 message was specified. */
514 static int message_enable_multibyte;
516 /* Nonzero if we should redraw the mode lines on the next redisplay. */
518 int update_mode_lines;
520 /* Nonzero if window sizes or contents have changed since last
521 redisplay that finished. */
523 int windows_or_buffers_changed;
525 /* Nonzero means a frame's cursor type has been changed. */
527 static int cursor_type_changed;
529 /* Nonzero after display_mode_line if %l was used and it displayed a
530 line number. */
532 static int line_number_displayed;
534 /* The name of the *Messages* buffer, a string. */
536 static Lisp_Object Vmessages_buffer_name;
538 /* Current, index 0, and last displayed echo area message. Either
539 buffers from echo_buffers, or nil to indicate no message. */
541 Lisp_Object echo_area_buffer[2];
543 /* The buffers referenced from echo_area_buffer. */
545 static Lisp_Object echo_buffer[2];
547 /* A vector saved used in with_area_buffer to reduce consing. */
549 static Lisp_Object Vwith_echo_area_save_vector;
551 /* Non-zero means display_echo_area should display the last echo area
552 message again. Set by redisplay_preserve_echo_area. */
554 static int display_last_displayed_message_p;
556 /* Nonzero if echo area is being used by print; zero if being used by
557 message. */
559 static int message_buf_print;
561 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
563 static Lisp_Object Qinhibit_menubar_update;
564 static Lisp_Object Qmessage_truncate_lines;
566 /* Set to 1 in clear_message to make redisplay_internal aware
567 of an emptied echo area. */
569 static int message_cleared_p;
571 /* A scratch glyph row with contents used for generating truncation
572 glyphs. Also used in direct_output_for_insert. */
574 #define MAX_SCRATCH_GLYPHS 100
575 static struct glyph_row scratch_glyph_row;
576 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
578 /* Ascent and height of the last line processed by move_it_to. */
580 static int last_height;
582 /* Non-zero if there's a help-echo in the echo area. */
584 int help_echo_showing_p;
586 /* If >= 0, computed, exact values of mode-line and header-line height
587 to use in the macros CURRENT_MODE_LINE_HEIGHT and
588 CURRENT_HEADER_LINE_HEIGHT. */
590 int current_mode_line_height, current_header_line_height;
592 /* The maximum distance to look ahead for text properties. Values
593 that are too small let us call compute_char_face and similar
594 functions too often which is expensive. Values that are too large
595 let us call compute_char_face and alike too often because we
596 might not be interested in text properties that far away. */
598 #define TEXT_PROP_DISTANCE_LIMIT 100
600 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
601 iterator state and later restore it. This is needed because the
602 bidi iterator on bidi.c keeps a stacked cache of its states, which
603 is really a singleton. When we use scratch iterator objects to
604 move around the buffer, we can cause the bidi cache to be pushed or
605 popped, and therefore we need to restore the cache state when we
606 return to the original iterator. */
607 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
608 do { \
609 if (CACHE) \
610 bidi_unshelve_cache (CACHE, 1); \
611 ITCOPY = ITORIG; \
612 CACHE = bidi_shelve_cache (); \
613 } while (0)
615 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
616 do { \
617 if (pITORIG != pITCOPY) \
618 *(pITORIG) = *(pITCOPY); \
619 bidi_unshelve_cache (CACHE, 0); \
620 CACHE = NULL; \
621 } while (0)
623 #ifdef GLYPH_DEBUG
625 /* Non-zero means print traces of redisplay if compiled with
626 GLYPH_DEBUG defined. */
628 int trace_redisplay_p;
630 #endif /* GLYPH_DEBUG */
632 #ifdef DEBUG_TRACE_MOVE
633 /* Non-zero means trace with TRACE_MOVE to stderr. */
634 int trace_move;
636 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
637 #else
638 #define TRACE_MOVE(x) (void) 0
639 #endif
641 static Lisp_Object Qauto_hscroll_mode;
643 /* Buffer being redisplayed -- for redisplay_window_error. */
645 static struct buffer *displayed_buffer;
647 /* Value returned from text property handlers (see below). */
649 enum prop_handled
651 HANDLED_NORMALLY,
652 HANDLED_RECOMPUTE_PROPS,
653 HANDLED_OVERLAY_STRING_CONSUMED,
654 HANDLED_RETURN
657 /* A description of text properties that redisplay is interested
658 in. */
660 struct props
662 /* The name of the property. */
663 Lisp_Object *name;
665 /* A unique index for the property. */
666 enum prop_idx idx;
668 /* A handler function called to set up iterator IT from the property
669 at IT's current position. Value is used to steer handle_stop. */
670 enum prop_handled (*handler) (struct it *it);
673 static enum prop_handled handle_face_prop (struct it *);
674 static enum prop_handled handle_invisible_prop (struct it *);
675 static enum prop_handled handle_display_prop (struct it *);
676 static enum prop_handled handle_composition_prop (struct it *);
677 static enum prop_handled handle_overlay_change (struct it *);
678 static enum prop_handled handle_fontified_prop (struct it *);
680 /* Properties handled by iterators. */
682 static struct props it_props[] =
684 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
685 /* Handle `face' before `display' because some sub-properties of
686 `display' need to know the face. */
687 {&Qface, FACE_PROP_IDX, handle_face_prop},
688 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
689 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
690 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
691 {NULL, 0, NULL}
694 /* Value is the position described by X. If X is a marker, value is
695 the marker_position of X. Otherwise, value is X. */
697 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
699 /* Enumeration returned by some move_it_.* functions internally. */
701 enum move_it_result
703 /* Not used. Undefined value. */
704 MOVE_UNDEFINED,
706 /* Move ended at the requested buffer position or ZV. */
707 MOVE_POS_MATCH_OR_ZV,
709 /* Move ended at the requested X pixel position. */
710 MOVE_X_REACHED,
712 /* Move within a line ended at the end of a line that must be
713 continued. */
714 MOVE_LINE_CONTINUED,
716 /* Move within a line ended at the end of a line that would
717 be displayed truncated. */
718 MOVE_LINE_TRUNCATED,
720 /* Move within a line ended at a line end. */
721 MOVE_NEWLINE_OR_CR
724 /* This counter is used to clear the face cache every once in a while
725 in redisplay_internal. It is incremented for each redisplay.
726 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
727 cleared. */
729 #define CLEAR_FACE_CACHE_COUNT 500
730 static int clear_face_cache_count;
732 /* Similarly for the image cache. */
734 #ifdef HAVE_WINDOW_SYSTEM
735 #define CLEAR_IMAGE_CACHE_COUNT 101
736 static int clear_image_cache_count;
738 /* Null glyph slice */
739 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
740 #endif
742 /* True while redisplay_internal is in progress. */
744 bool redisplaying_p;
746 static Lisp_Object Qinhibit_free_realized_faces;
747 static Lisp_Object Qmode_line_default_help_echo;
749 /* If a string, XTread_socket generates an event to display that string.
750 (The display is done in read_char.) */
752 Lisp_Object help_echo_string;
753 Lisp_Object help_echo_window;
754 Lisp_Object help_echo_object;
755 ptrdiff_t help_echo_pos;
757 /* Temporary variable for XTread_socket. */
759 Lisp_Object previous_help_echo_string;
761 /* Platform-independent portion of hourglass implementation. */
763 #ifdef HAVE_WINDOW_SYSTEM
765 /* Non-zero means an hourglass cursor is currently shown. */
766 int hourglass_shown_p;
768 /* If non-null, an asynchronous timer that, when it expires, displays
769 an hourglass cursor on all frames. */
770 struct atimer *hourglass_atimer;
772 #endif /* HAVE_WINDOW_SYSTEM */
774 /* Name of the face used to display glyphless characters. */
775 Lisp_Object Qglyphless_char;
777 /* Symbol for the purpose of Vglyphless_char_display. */
778 static Lisp_Object Qglyphless_char_display;
780 /* Method symbols for Vglyphless_char_display. */
781 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
783 /* Default number of seconds to wait before displaying an hourglass
784 cursor. */
785 #define DEFAULT_HOURGLASS_DELAY 1
787 #ifdef HAVE_WINDOW_SYSTEM
789 /* Default pixel width of `thin-space' display method. */
790 #define THIN_SPACE_WIDTH 1
792 #endif /* HAVE_WINDOW_SYSTEM */
794 /* Function prototypes. */
796 static void setup_for_ellipsis (struct it *, int);
797 static void set_iterator_to_next (struct it *, int);
798 static void mark_window_display_accurate_1 (struct window *, int);
799 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
800 static int display_prop_string_p (Lisp_Object, Lisp_Object);
801 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
802 static int cursor_row_p (struct glyph_row *);
803 static int redisplay_mode_lines (Lisp_Object, int);
804 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
806 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
808 static void handle_line_prefix (struct it *);
810 static void pint2str (char *, int, ptrdiff_t);
811 static void pint2hrstr (char *, int, ptrdiff_t);
812 static struct text_pos run_window_scroll_functions (Lisp_Object,
813 struct text_pos);
814 static int text_outside_line_unchanged_p (struct window *,
815 ptrdiff_t, ptrdiff_t);
816 static void store_mode_line_noprop_char (char);
817 static int store_mode_line_noprop (const char *, int, int);
818 static void handle_stop (struct it *);
819 static void handle_stop_backwards (struct it *, ptrdiff_t);
820 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
821 static void ensure_echo_area_buffers (void);
822 static void unwind_with_echo_area_buffer (Lisp_Object);
823 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
824 static int with_echo_area_buffer (struct window *, int,
825 int (*) (ptrdiff_t, Lisp_Object),
826 ptrdiff_t, Lisp_Object);
827 static void clear_garbaged_frames (void);
828 static int current_message_1 (ptrdiff_t, Lisp_Object);
829 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
830 static void set_message (Lisp_Object);
831 static int set_message_1 (ptrdiff_t, Lisp_Object);
832 static int display_echo_area (struct window *);
833 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
834 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
835 static void unwind_redisplay (void);
836 static int string_char_and_length (const unsigned char *, int *);
837 static struct text_pos display_prop_end (struct it *, Lisp_Object,
838 struct text_pos);
839 static int compute_window_start_on_continuation_line (struct window *);
840 static void insert_left_trunc_glyphs (struct it *);
841 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
842 Lisp_Object);
843 static void extend_face_to_end_of_line (struct it *);
844 static int append_space_for_newline (struct it *, int);
845 static int cursor_row_fully_visible_p (struct window *, int, int);
846 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
847 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
848 static int trailing_whitespace_p (ptrdiff_t);
849 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
850 static void push_it (struct it *, struct text_pos *);
851 static void iterate_out_of_display_property (struct it *);
852 static void pop_it (struct it *);
853 static void sync_frame_with_window_matrix_rows (struct window *);
854 static void redisplay_internal (void);
855 static int echo_area_display (int);
856 static void redisplay_windows (Lisp_Object);
857 static void redisplay_window (Lisp_Object, int);
858 static Lisp_Object redisplay_window_error (Lisp_Object);
859 static Lisp_Object redisplay_window_0 (Lisp_Object);
860 static Lisp_Object redisplay_window_1 (Lisp_Object);
861 static int set_cursor_from_row (struct window *, struct glyph_row *,
862 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
863 int, int);
864 static int update_menu_bar (struct frame *, int, int);
865 static int try_window_reusing_current_matrix (struct window *);
866 static int try_window_id (struct window *);
867 static int display_line (struct it *);
868 static int display_mode_lines (struct window *);
869 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
870 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
871 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
872 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
873 static void display_menu_bar (struct window *);
874 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
875 ptrdiff_t *);
876 static int display_string (const char *, Lisp_Object, Lisp_Object,
877 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
878 static void compute_line_metrics (struct it *);
879 static void run_redisplay_end_trigger_hook (struct it *);
880 static int get_overlay_strings (struct it *, ptrdiff_t);
881 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
882 static void next_overlay_string (struct it *);
883 static void reseat (struct it *, struct text_pos, int);
884 static void reseat_1 (struct it *, struct text_pos, int);
885 static void back_to_previous_visible_line_start (struct it *);
886 static void reseat_at_next_visible_line_start (struct it *, int);
887 static int next_element_from_ellipsis (struct it *);
888 static int next_element_from_display_vector (struct it *);
889 static int next_element_from_string (struct it *);
890 static int next_element_from_c_string (struct it *);
891 static int next_element_from_buffer (struct it *);
892 static int next_element_from_composition (struct it *);
893 static int next_element_from_image (struct it *);
894 static int next_element_from_stretch (struct it *);
895 static void load_overlay_strings (struct it *, ptrdiff_t);
896 static int init_from_display_pos (struct it *, struct window *,
897 struct display_pos *);
898 static void reseat_to_string (struct it *, const char *,
899 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
900 static int get_next_display_element (struct it *);
901 static enum move_it_result
902 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
903 enum move_operation_enum);
904 static void get_visually_first_element (struct it *);
905 static void init_to_row_start (struct it *, struct window *,
906 struct glyph_row *);
907 static int init_to_row_end (struct it *, struct window *,
908 struct glyph_row *);
909 static void back_to_previous_line_start (struct it *);
910 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
911 static struct text_pos string_pos_nchars_ahead (struct text_pos,
912 Lisp_Object, ptrdiff_t);
913 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
914 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
915 static ptrdiff_t number_of_chars (const char *, bool);
916 static void compute_stop_pos (struct it *);
917 static void compute_string_pos (struct text_pos *, struct text_pos,
918 Lisp_Object);
919 static int face_before_or_after_it_pos (struct it *, int);
920 static ptrdiff_t next_overlay_change (ptrdiff_t);
921 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
922 Lisp_Object, struct text_pos *, ptrdiff_t, int);
923 static int handle_single_display_spec (struct it *, Lisp_Object,
924 Lisp_Object, Lisp_Object,
925 struct text_pos *, ptrdiff_t, int, int);
926 static int underlying_face_id (struct it *);
927 static int in_ellipses_for_invisible_text_p (struct display_pos *,
928 struct window *);
930 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
931 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
933 #ifdef HAVE_WINDOW_SYSTEM
935 static void x_consider_frame_title (Lisp_Object);
936 static int tool_bar_lines_needed (struct frame *, int *);
937 static void update_tool_bar (struct frame *, int);
938 static void build_desired_tool_bar_string (struct frame *f);
939 static int redisplay_tool_bar (struct frame *);
940 static void display_tool_bar_line (struct it *, int);
941 static void notice_overwritten_cursor (struct window *,
942 enum glyph_row_area,
943 int, int, int, int);
944 static void append_stretch_glyph (struct it *, Lisp_Object,
945 int, int, int);
948 #endif /* HAVE_WINDOW_SYSTEM */
950 static void produce_special_glyphs (struct it *, enum display_element_type);
951 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
952 static int coords_in_mouse_face_p (struct window *, int, int);
956 /***********************************************************************
957 Window display dimensions
958 ***********************************************************************/
960 /* Return the bottom boundary y-position for text lines in window W.
961 This is the first y position at which a line cannot start.
962 It is relative to the top of the window.
964 This is the height of W minus the height of a mode line, if any. */
967 window_text_bottom_y (struct window *w)
969 int height = WINDOW_TOTAL_HEIGHT (w);
971 if (WINDOW_WANTS_MODELINE_P (w))
972 height -= CURRENT_MODE_LINE_HEIGHT (w);
973 return height;
976 /* Return the pixel width of display area AREA of window W.
977 ANY_AREA means return the total width of W, not including
978 fringes to the left and right of the window. */
981 window_box_width (struct window *w, enum glyph_row_area area)
983 int cols = w->total_cols;
984 int pixels = 0;
986 if (!w->pseudo_window_p)
988 cols -= WINDOW_SCROLL_BAR_COLS (w);
990 if (area == TEXT_AREA)
992 cols -= max (0, w->left_margin_cols);
993 cols -= max (0, w->right_margin_cols);
994 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
996 else if (area == LEFT_MARGIN_AREA)
998 cols = max (0, w->left_margin_cols);
999 pixels = 0;
1001 else if (area == RIGHT_MARGIN_AREA)
1003 cols = max (0, w->right_margin_cols);
1004 pixels = 0;
1008 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1012 /* Return the pixel height of the display area of window W, not
1013 including mode lines of W, if any. */
1016 window_box_height (struct window *w)
1018 struct frame *f = XFRAME (w->frame);
1019 int height = WINDOW_TOTAL_HEIGHT (w);
1021 eassert (height >= 0);
1023 /* Note: the code below that determines the mode-line/header-line
1024 height is essentially the same as that contained in the macro
1025 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1026 the appropriate glyph row has its `mode_line_p' flag set,
1027 and if it doesn't, uses estimate_mode_line_height instead. */
1029 if (WINDOW_WANTS_MODELINE_P (w))
1031 struct glyph_row *ml_row
1032 = (w->current_matrix && w->current_matrix->rows
1033 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1034 : 0);
1035 if (ml_row && ml_row->mode_line_p)
1036 height -= ml_row->height;
1037 else
1038 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1041 if (WINDOW_WANTS_HEADER_LINE_P (w))
1043 struct glyph_row *hl_row
1044 = (w->current_matrix && w->current_matrix->rows
1045 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1046 : 0);
1047 if (hl_row && hl_row->mode_line_p)
1048 height -= hl_row->height;
1049 else
1050 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1053 /* With a very small font and a mode-line that's taller than
1054 default, we might end up with a negative height. */
1055 return max (0, height);
1058 /* Return the window-relative coordinate of the left edge of display
1059 area AREA of window W. ANY_AREA means return the left edge of the
1060 whole window, to the right of the left fringe of W. */
1063 window_box_left_offset (struct window *w, enum glyph_row_area area)
1065 int x;
1067 if (w->pseudo_window_p)
1068 return 0;
1070 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1072 if (area == TEXT_AREA)
1073 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1074 + window_box_width (w, LEFT_MARGIN_AREA));
1075 else if (area == RIGHT_MARGIN_AREA)
1076 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1077 + window_box_width (w, LEFT_MARGIN_AREA)
1078 + window_box_width (w, TEXT_AREA)
1079 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1081 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1082 else if (area == LEFT_MARGIN_AREA
1083 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1084 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1086 return x;
1090 /* Return the window-relative coordinate of the right edge of display
1091 area AREA of window W. ANY_AREA means return the right edge of the
1092 whole window, to the left of the right fringe of W. */
1095 window_box_right_offset (struct window *w, enum glyph_row_area area)
1097 return window_box_left_offset (w, area) + window_box_width (w, area);
1100 /* Return the frame-relative coordinate of the left edge of display
1101 area AREA of window W. ANY_AREA means return the left edge of the
1102 whole window, to the right of the left fringe of W. */
1105 window_box_left (struct window *w, enum glyph_row_area area)
1107 struct frame *f = XFRAME (w->frame);
1108 int x;
1110 if (w->pseudo_window_p)
1111 return FRAME_INTERNAL_BORDER_WIDTH (f);
1113 x = (WINDOW_LEFT_EDGE_X (w)
1114 + window_box_left_offset (w, area));
1116 return x;
1120 /* Return the frame-relative coordinate of the right edge of display
1121 area AREA of window W. ANY_AREA means return the right edge of the
1122 whole window, to the left of the right fringe of W. */
1125 window_box_right (struct window *w, enum glyph_row_area area)
1127 return window_box_left (w, area) + window_box_width (w, area);
1130 /* Get the bounding box of the display area AREA of window W, without
1131 mode lines, in frame-relative coordinates. ANY_AREA means the
1132 whole window, not including the left and right fringes of
1133 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1134 coordinates of the upper-left corner of the box. Return in
1135 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1137 void
1138 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1139 int *box_y, int *box_width, int *box_height)
1141 if (box_width)
1142 *box_width = window_box_width (w, area);
1143 if (box_height)
1144 *box_height = window_box_height (w);
1145 if (box_x)
1146 *box_x = window_box_left (w, area);
1147 if (box_y)
1149 *box_y = WINDOW_TOP_EDGE_Y (w);
1150 if (WINDOW_WANTS_HEADER_LINE_P (w))
1151 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1155 #ifdef HAVE_WINDOW_SYSTEM
1157 /* Get the bounding box of the display area AREA of window W, without
1158 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1159 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1160 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1161 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1162 box. */
1164 static void
1165 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1166 int *bottom_right_x, int *bottom_right_y)
1168 window_box (w, ANY_AREA, top_left_x, top_left_y,
1169 bottom_right_x, bottom_right_y);
1170 *bottom_right_x += *top_left_x;
1171 *bottom_right_y += *top_left_y;
1174 #endif /* HAVE_WINDOW_SYSTEM */
1176 /***********************************************************************
1177 Utilities
1178 ***********************************************************************/
1180 /* Return the bottom y-position of the line the iterator IT is in.
1181 This can modify IT's settings. */
1184 line_bottom_y (struct it *it)
1186 int line_height = it->max_ascent + it->max_descent;
1187 int line_top_y = it->current_y;
1189 if (line_height == 0)
1191 if (last_height)
1192 line_height = last_height;
1193 else if (IT_CHARPOS (*it) < ZV)
1195 move_it_by_lines (it, 1);
1196 line_height = (it->max_ascent || it->max_descent
1197 ? it->max_ascent + it->max_descent
1198 : last_height);
1200 else
1202 struct glyph_row *row = it->glyph_row;
1204 /* Use the default character height. */
1205 it->glyph_row = NULL;
1206 it->what = IT_CHARACTER;
1207 it->c = ' ';
1208 it->len = 1;
1209 PRODUCE_GLYPHS (it);
1210 line_height = it->ascent + it->descent;
1211 it->glyph_row = row;
1215 return line_top_y + line_height;
1218 DEFUN ("line-pixel-height", Fline_pixel_height,
1219 Sline_pixel_height, 0, 0, 0,
1220 doc: /* Return height in pixels of text line in the selected window.
1222 Value is the height in pixels of the line at point. */)
1223 (void)
1225 struct it it;
1226 struct text_pos pt;
1227 struct window *w = XWINDOW (selected_window);
1229 SET_TEXT_POS (pt, PT, PT_BYTE);
1230 start_display (&it, w, pt);
1231 it.vpos = it.current_y = 0;
1232 last_height = 0;
1233 return make_number (line_bottom_y (&it));
1236 /* Return the default pixel height of text lines in window W. The
1237 value is the canonical height of the W frame's default font, plus
1238 any extra space required by the line-spacing variable or frame
1239 parameter.
1241 Implementation note: this ignores any line-spacing text properties
1242 put on the newline characters. This is because those properties
1243 only affect the _screen_ line ending in the newline (i.e., in a
1244 continued line, only the last screen line will be affected), which
1245 means only a small number of lines in a buffer can ever use this
1246 feature. Since this function is used to compute the default pixel
1247 equivalent of text lines in a window, we can safely ignore those
1248 few lines. For the same reasons, we ignore the line-height
1249 properties. */
1251 default_line_pixel_height (struct window *w)
1253 struct frame *f = WINDOW_XFRAME (w);
1254 int height = FRAME_LINE_HEIGHT (f);
1256 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1258 struct buffer *b = XBUFFER (w->contents);
1259 Lisp_Object val = BVAR (b, extra_line_spacing);
1261 if (NILP (val))
1262 val = BVAR (&buffer_defaults, extra_line_spacing);
1263 if (!NILP (val))
1265 if (RANGED_INTEGERP (0, val, INT_MAX))
1266 height += XFASTINT (val);
1267 else if (FLOATP (val))
1269 int addon = XFLOAT_DATA (val) * height + 0.5;
1271 if (addon >= 0)
1272 height += addon;
1275 else
1276 height += f->extra_line_spacing;
1279 return height;
1282 /* Subroutine of pos_visible_p below. Extracts a display string, if
1283 any, from the display spec given as its argument. */
1284 static Lisp_Object
1285 string_from_display_spec (Lisp_Object spec)
1287 if (CONSP (spec))
1289 while (CONSP (spec))
1291 if (STRINGP (XCAR (spec)))
1292 return XCAR (spec);
1293 spec = XCDR (spec);
1296 else if (VECTORP (spec))
1298 ptrdiff_t i;
1300 for (i = 0; i < ASIZE (spec); i++)
1302 if (STRINGP (AREF (spec, i)))
1303 return AREF (spec, i);
1305 return Qnil;
1308 return spec;
1312 /* Limit insanely large values of W->hscroll on frame F to the largest
1313 value that will still prevent first_visible_x and last_visible_x of
1314 'struct it' from overflowing an int. */
1315 static int
1316 window_hscroll_limited (struct window *w, struct frame *f)
1318 ptrdiff_t window_hscroll = w->hscroll;
1319 int window_text_width = window_box_width (w, TEXT_AREA);
1320 int colwidth = FRAME_COLUMN_WIDTH (f);
1322 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1323 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1325 return window_hscroll;
1328 /* Return 1 if position CHARPOS is visible in window W.
1329 CHARPOS < 0 means return info about WINDOW_END position.
1330 If visible, set *X and *Y to pixel coordinates of top left corner.
1331 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1332 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1335 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1336 int *rtop, int *rbot, int *rowh, int *vpos)
1338 struct it it;
1339 void *itdata = bidi_shelve_cache ();
1340 struct text_pos top;
1341 int visible_p = 0;
1342 struct buffer *old_buffer = NULL;
1344 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1345 return visible_p;
1347 if (XBUFFER (w->contents) != current_buffer)
1349 old_buffer = current_buffer;
1350 set_buffer_internal_1 (XBUFFER (w->contents));
1353 SET_TEXT_POS_FROM_MARKER (top, w->start);
1354 /* Scrolling a minibuffer window via scroll bar when the echo area
1355 shows long text sometimes resets the minibuffer contents behind
1356 our backs. */
1357 if (CHARPOS (top) > ZV)
1358 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1360 /* Compute exact mode line heights. */
1361 if (WINDOW_WANTS_MODELINE_P (w))
1362 current_mode_line_height
1363 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1364 BVAR (current_buffer, mode_line_format));
1366 if (WINDOW_WANTS_HEADER_LINE_P (w))
1367 current_header_line_height
1368 = display_mode_line (w, HEADER_LINE_FACE_ID,
1369 BVAR (current_buffer, header_line_format));
1371 start_display (&it, w, top);
1372 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1373 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1375 if (charpos >= 0
1376 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1377 && IT_CHARPOS (it) >= charpos)
1378 /* When scanning backwards under bidi iteration, move_it_to
1379 stops at or _before_ CHARPOS, because it stops at or to
1380 the _right_ of the character at CHARPOS. */
1381 || (it.bidi_p && it.bidi_it.scan_dir == -1
1382 && IT_CHARPOS (it) <= charpos)))
1384 /* We have reached CHARPOS, or passed it. How the call to
1385 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1386 or covered by a display property, move_it_to stops at the end
1387 of the invisible text, to the right of CHARPOS. (ii) If
1388 CHARPOS is in a display vector, move_it_to stops on its last
1389 glyph. */
1390 int top_x = it.current_x;
1391 int top_y = it.current_y;
1392 /* Calling line_bottom_y may change it.method, it.position, etc. */
1393 enum it_method it_method = it.method;
1394 int bottom_y = (last_height = 0, line_bottom_y (&it));
1395 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1397 if (top_y < window_top_y)
1398 visible_p = bottom_y > window_top_y;
1399 else if (top_y < it.last_visible_y)
1400 visible_p = 1;
1401 if (bottom_y >= it.last_visible_y
1402 && it.bidi_p && it.bidi_it.scan_dir == -1
1403 && IT_CHARPOS (it) < charpos)
1405 /* When the last line of the window is scanned backwards
1406 under bidi iteration, we could be duped into thinking
1407 that we have passed CHARPOS, when in fact move_it_to
1408 simply stopped short of CHARPOS because it reached
1409 last_visible_y. To see if that's what happened, we call
1410 move_it_to again with a slightly larger vertical limit,
1411 and see if it actually moved vertically; if it did, we
1412 didn't really reach CHARPOS, which is beyond window end. */
1413 struct it save_it = it;
1414 /* Why 10? because we don't know how many canonical lines
1415 will the height of the next line(s) be. So we guess. */
1416 int ten_more_lines = 10 * default_line_pixel_height (w);
1418 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1419 MOVE_TO_POS | MOVE_TO_Y);
1420 if (it.current_y > top_y)
1421 visible_p = 0;
1423 it = save_it;
1425 if (visible_p)
1427 if (it_method == GET_FROM_DISPLAY_VECTOR)
1429 /* We stopped on the last glyph of a display vector.
1430 Try and recompute. Hack alert! */
1431 if (charpos < 2 || top.charpos >= charpos)
1432 top_x = it.glyph_row->x;
1433 else
1435 struct it it2, it2_prev;
1436 /* The idea is to get to the previous buffer
1437 position, consume the character there, and use
1438 the pixel coordinates we get after that. But if
1439 the previous buffer position is also displayed
1440 from a display vector, we need to consume all of
1441 the glyphs from that display vector. */
1442 start_display (&it2, w, top);
1443 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1444 /* If we didn't get to CHARPOS - 1, there's some
1445 replacing display property at that position, and
1446 we stopped after it. That is exactly the place
1447 whose coordinates we want. */
1448 if (IT_CHARPOS (it2) != charpos - 1)
1449 it2_prev = it2;
1450 else
1452 /* Iterate until we get out of the display
1453 vector that displays the character at
1454 CHARPOS - 1. */
1455 do {
1456 get_next_display_element (&it2);
1457 PRODUCE_GLYPHS (&it2);
1458 it2_prev = it2;
1459 set_iterator_to_next (&it2, 1);
1460 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1461 && IT_CHARPOS (it2) < charpos);
1463 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1464 || it2_prev.current_x > it2_prev.last_visible_x)
1465 top_x = it.glyph_row->x;
1466 else
1468 top_x = it2_prev.current_x;
1469 top_y = it2_prev.current_y;
1473 else if (IT_CHARPOS (it) != charpos)
1475 Lisp_Object cpos = make_number (charpos);
1476 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1477 Lisp_Object string = string_from_display_spec (spec);
1478 struct text_pos tpos;
1479 int replacing_spec_p;
1480 bool newline_in_string
1481 = (STRINGP (string)
1482 && memchr (SDATA (string), '\n', SBYTES (string)));
1484 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1485 replacing_spec_p
1486 = (!NILP (spec)
1487 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1488 charpos, FRAME_WINDOW_P (it.f)));
1489 /* The tricky code below is needed because there's a
1490 discrepancy between move_it_to and how we set cursor
1491 when PT is at the beginning of a portion of text
1492 covered by a display property or an overlay with a
1493 display property, or the display line ends in a
1494 newline from a display string. move_it_to will stop
1495 _after_ such display strings, whereas
1496 set_cursor_from_row conspires with cursor_row_p to
1497 place the cursor on the first glyph produced from the
1498 display string. */
1500 /* We have overshoot PT because it is covered by a
1501 display property that replaces the text it covers.
1502 If the string includes embedded newlines, we are also
1503 in the wrong display line. Backtrack to the correct
1504 line, where the display property begins. */
1505 if (replacing_spec_p)
1507 Lisp_Object startpos, endpos;
1508 EMACS_INT start, end;
1509 struct it it3;
1510 int it3_moved;
1512 /* Find the first and the last buffer positions
1513 covered by the display string. */
1514 endpos =
1515 Fnext_single_char_property_change (cpos, Qdisplay,
1516 Qnil, Qnil);
1517 startpos =
1518 Fprevious_single_char_property_change (endpos, Qdisplay,
1519 Qnil, Qnil);
1520 start = XFASTINT (startpos);
1521 end = XFASTINT (endpos);
1522 /* Move to the last buffer position before the
1523 display property. */
1524 start_display (&it3, w, top);
1525 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1526 /* Move forward one more line if the position before
1527 the display string is a newline or if it is the
1528 rightmost character on a line that is
1529 continued or word-wrapped. */
1530 if (it3.method == GET_FROM_BUFFER
1531 && (it3.c == '\n'
1532 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1533 move_it_by_lines (&it3, 1);
1534 else if (move_it_in_display_line_to (&it3, -1,
1535 it3.current_x
1536 + it3.pixel_width,
1537 MOVE_TO_X)
1538 == MOVE_LINE_CONTINUED)
1540 move_it_by_lines (&it3, 1);
1541 /* When we are under word-wrap, the #$@%!
1542 move_it_by_lines moves 2 lines, so we need to
1543 fix that up. */
1544 if (it3.line_wrap == WORD_WRAP)
1545 move_it_by_lines (&it3, -1);
1548 /* Record the vertical coordinate of the display
1549 line where we wound up. */
1550 top_y = it3.current_y;
1551 if (it3.bidi_p)
1553 /* When characters are reordered for display,
1554 the character displayed to the left of the
1555 display string could be _after_ the display
1556 property in the logical order. Use the
1557 smallest vertical position of these two. */
1558 start_display (&it3, w, top);
1559 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1560 if (it3.current_y < top_y)
1561 top_y = it3.current_y;
1563 /* Move from the top of the window to the beginning
1564 of the display line where the display string
1565 begins. */
1566 start_display (&it3, w, top);
1567 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1568 /* If it3_moved stays zero after the 'while' loop
1569 below, that means we already were at a newline
1570 before the loop (e.g., the display string begins
1571 with a newline), so we don't need to (and cannot)
1572 inspect the glyphs of it3.glyph_row, because
1573 PRODUCE_GLYPHS will not produce anything for a
1574 newline, and thus it3.glyph_row stays at its
1575 stale content it got at top of the window. */
1576 it3_moved = 0;
1577 /* Finally, advance the iterator until we hit the
1578 first display element whose character position is
1579 CHARPOS, or until the first newline from the
1580 display string, which signals the end of the
1581 display line. */
1582 while (get_next_display_element (&it3))
1584 PRODUCE_GLYPHS (&it3);
1585 if (IT_CHARPOS (it3) == charpos
1586 || ITERATOR_AT_END_OF_LINE_P (&it3))
1587 break;
1588 it3_moved = 1;
1589 set_iterator_to_next (&it3, 0);
1591 top_x = it3.current_x - it3.pixel_width;
1592 /* Normally, we would exit the above loop because we
1593 found the display element whose character
1594 position is CHARPOS. For the contingency that we
1595 didn't, and stopped at the first newline from the
1596 display string, move back over the glyphs
1597 produced from the string, until we find the
1598 rightmost glyph not from the string. */
1599 if (it3_moved
1600 && newline_in_string
1601 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1603 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1604 + it3.glyph_row->used[TEXT_AREA];
1606 while (EQ ((g - 1)->object, string))
1608 --g;
1609 top_x -= g->pixel_width;
1611 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1612 + it3.glyph_row->used[TEXT_AREA]);
1617 *x = top_x;
1618 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1619 *rtop = max (0, window_top_y - top_y);
1620 *rbot = max (0, bottom_y - it.last_visible_y);
1621 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1622 - max (top_y, window_top_y)));
1623 *vpos = it.vpos;
1626 else
1628 /* We were asked to provide info about WINDOW_END. */
1629 struct it it2;
1630 void *it2data = NULL;
1632 SAVE_IT (it2, it, it2data);
1633 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1634 move_it_by_lines (&it, 1);
1635 if (charpos < IT_CHARPOS (it)
1636 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1638 visible_p = 1;
1639 RESTORE_IT (&it2, &it2, it2data);
1640 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1641 *x = it2.current_x;
1642 *y = it2.current_y + it2.max_ascent - it2.ascent;
1643 *rtop = max (0, -it2.current_y);
1644 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1645 - it.last_visible_y));
1646 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1647 it.last_visible_y)
1648 - max (it2.current_y,
1649 WINDOW_HEADER_LINE_HEIGHT (w))));
1650 *vpos = it2.vpos;
1652 else
1653 bidi_unshelve_cache (it2data, 1);
1655 bidi_unshelve_cache (itdata, 0);
1657 if (old_buffer)
1658 set_buffer_internal_1 (old_buffer);
1660 current_header_line_height = current_mode_line_height = -1;
1662 if (visible_p && w->hscroll > 0)
1663 *x -=
1664 window_hscroll_limited (w, WINDOW_XFRAME (w))
1665 * WINDOW_FRAME_COLUMN_WIDTH (w);
1667 #if 0
1668 /* Debugging code. */
1669 if (visible_p)
1670 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1671 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1672 else
1673 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1674 #endif
1676 return visible_p;
1680 /* Return the next character from STR. Return in *LEN the length of
1681 the character. This is like STRING_CHAR_AND_LENGTH but never
1682 returns an invalid character. If we find one, we return a `?', but
1683 with the length of the invalid character. */
1685 static int
1686 string_char_and_length (const unsigned char *str, int *len)
1688 int c;
1690 c = STRING_CHAR_AND_LENGTH (str, *len);
1691 if (!CHAR_VALID_P (c))
1692 /* We may not change the length here because other places in Emacs
1693 don't use this function, i.e. they silently accept invalid
1694 characters. */
1695 c = '?';
1697 return c;
1702 /* Given a position POS containing a valid character and byte position
1703 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1705 static struct text_pos
1706 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1708 eassert (STRINGP (string) && nchars >= 0);
1710 if (STRING_MULTIBYTE (string))
1712 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1713 int len;
1715 while (nchars--)
1717 string_char_and_length (p, &len);
1718 p += len;
1719 CHARPOS (pos) += 1;
1720 BYTEPOS (pos) += len;
1723 else
1724 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1726 return pos;
1730 /* Value is the text position, i.e. character and byte position,
1731 for character position CHARPOS in STRING. */
1733 static struct text_pos
1734 string_pos (ptrdiff_t charpos, Lisp_Object string)
1736 struct text_pos pos;
1737 eassert (STRINGP (string));
1738 eassert (charpos >= 0);
1739 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1740 return pos;
1744 /* Value is a text position, i.e. character and byte position, for
1745 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1746 means recognize multibyte characters. */
1748 static struct text_pos
1749 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1751 struct text_pos pos;
1753 eassert (s != NULL);
1754 eassert (charpos >= 0);
1756 if (multibyte_p)
1758 int len;
1760 SET_TEXT_POS (pos, 0, 0);
1761 while (charpos--)
1763 string_char_and_length ((const unsigned char *) s, &len);
1764 s += len;
1765 CHARPOS (pos) += 1;
1766 BYTEPOS (pos) += len;
1769 else
1770 SET_TEXT_POS (pos, charpos, charpos);
1772 return pos;
1776 /* Value is the number of characters in C string S. MULTIBYTE_P
1777 non-zero means recognize multibyte characters. */
1779 static ptrdiff_t
1780 number_of_chars (const char *s, bool multibyte_p)
1782 ptrdiff_t nchars;
1784 if (multibyte_p)
1786 ptrdiff_t rest = strlen (s);
1787 int len;
1788 const unsigned char *p = (const unsigned char *) s;
1790 for (nchars = 0; rest > 0; ++nchars)
1792 string_char_and_length (p, &len);
1793 rest -= len, p += len;
1796 else
1797 nchars = strlen (s);
1799 return nchars;
1803 /* Compute byte position NEWPOS->bytepos corresponding to
1804 NEWPOS->charpos. POS is a known position in string STRING.
1805 NEWPOS->charpos must be >= POS.charpos. */
1807 static void
1808 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1810 eassert (STRINGP (string));
1811 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1813 if (STRING_MULTIBYTE (string))
1814 *newpos = string_pos_nchars_ahead (pos, string,
1815 CHARPOS (*newpos) - CHARPOS (pos));
1816 else
1817 BYTEPOS (*newpos) = CHARPOS (*newpos);
1820 /* EXPORT:
1821 Return an estimation of the pixel height of mode or header lines on
1822 frame F. FACE_ID specifies what line's height to estimate. */
1825 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1827 #ifdef HAVE_WINDOW_SYSTEM
1828 if (FRAME_WINDOW_P (f))
1830 int height = FONT_HEIGHT (FRAME_FONT (f));
1832 /* This function is called so early when Emacs starts that the face
1833 cache and mode line face are not yet initialized. */
1834 if (FRAME_FACE_CACHE (f))
1836 struct face *face = FACE_FROM_ID (f, face_id);
1837 if (face)
1839 if (face->font)
1840 height = FONT_HEIGHT (face->font);
1841 if (face->box_line_width > 0)
1842 height += 2 * face->box_line_width;
1846 return height;
1848 #endif
1850 return 1;
1853 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1854 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1855 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1856 not force the value into range. */
1858 void
1859 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1860 int *x, int *y, NativeRectangle *bounds, int noclip)
1863 #ifdef HAVE_WINDOW_SYSTEM
1864 if (FRAME_WINDOW_P (f))
1866 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1867 even for negative values. */
1868 if (pix_x < 0)
1869 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1870 if (pix_y < 0)
1871 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1873 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1874 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1876 if (bounds)
1877 STORE_NATIVE_RECT (*bounds,
1878 FRAME_COL_TO_PIXEL_X (f, pix_x),
1879 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1880 FRAME_COLUMN_WIDTH (f) - 1,
1881 FRAME_LINE_HEIGHT (f) - 1);
1883 if (!noclip)
1885 if (pix_x < 0)
1886 pix_x = 0;
1887 else if (pix_x > FRAME_TOTAL_COLS (f))
1888 pix_x = FRAME_TOTAL_COLS (f);
1890 if (pix_y < 0)
1891 pix_y = 0;
1892 else if (pix_y > FRAME_LINES (f))
1893 pix_y = FRAME_LINES (f);
1896 #endif
1898 *x = pix_x;
1899 *y = pix_y;
1903 /* Find the glyph under window-relative coordinates X/Y in window W.
1904 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1905 strings. Return in *HPOS and *VPOS the row and column number of
1906 the glyph found. Return in *AREA the glyph area containing X.
1907 Value is a pointer to the glyph found or null if X/Y is not on
1908 text, or we can't tell because W's current matrix is not up to
1909 date. */
1911 static
1912 struct glyph *
1913 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1914 int *dx, int *dy, int *area)
1916 struct glyph *glyph, *end;
1917 struct glyph_row *row = NULL;
1918 int x0, i;
1920 /* Find row containing Y. Give up if some row is not enabled. */
1921 for (i = 0; i < w->current_matrix->nrows; ++i)
1923 row = MATRIX_ROW (w->current_matrix, i);
1924 if (!row->enabled_p)
1925 return NULL;
1926 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1927 break;
1930 *vpos = i;
1931 *hpos = 0;
1933 /* Give up if Y is not in the window. */
1934 if (i == w->current_matrix->nrows)
1935 return NULL;
1937 /* Get the glyph area containing X. */
1938 if (w->pseudo_window_p)
1940 *area = TEXT_AREA;
1941 x0 = 0;
1943 else
1945 if (x < window_box_left_offset (w, TEXT_AREA))
1947 *area = LEFT_MARGIN_AREA;
1948 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1950 else if (x < window_box_right_offset (w, TEXT_AREA))
1952 *area = TEXT_AREA;
1953 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1955 else
1957 *area = RIGHT_MARGIN_AREA;
1958 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1962 /* Find glyph containing X. */
1963 glyph = row->glyphs[*area];
1964 end = glyph + row->used[*area];
1965 x -= x0;
1966 while (glyph < end && x >= glyph->pixel_width)
1968 x -= glyph->pixel_width;
1969 ++glyph;
1972 if (glyph == end)
1973 return NULL;
1975 if (dx)
1977 *dx = x;
1978 *dy = y - (row->y + row->ascent - glyph->ascent);
1981 *hpos = glyph - row->glyphs[*area];
1982 return glyph;
1985 /* Convert frame-relative x/y to coordinates relative to window W.
1986 Takes pseudo-windows into account. */
1988 static void
1989 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1991 if (w->pseudo_window_p)
1993 /* A pseudo-window is always full-width, and starts at the
1994 left edge of the frame, plus a frame border. */
1995 struct frame *f = XFRAME (w->frame);
1996 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1997 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1999 else
2001 *x -= WINDOW_LEFT_EDGE_X (w);
2002 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2006 #ifdef HAVE_WINDOW_SYSTEM
2008 /* EXPORT:
2009 Return in RECTS[] at most N clipping rectangles for glyph string S.
2010 Return the number of stored rectangles. */
2013 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2015 XRectangle r;
2017 if (n <= 0)
2018 return 0;
2020 if (s->row->full_width_p)
2022 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2023 r.x = WINDOW_LEFT_EDGE_X (s->w);
2024 r.width = WINDOW_TOTAL_WIDTH (s->w);
2026 /* Unless displaying a mode or menu bar line, which are always
2027 fully visible, clip to the visible part of the row. */
2028 if (s->w->pseudo_window_p)
2029 r.height = s->row->visible_height;
2030 else
2031 r.height = s->height;
2033 else
2035 /* This is a text line that may be partially visible. */
2036 r.x = window_box_left (s->w, s->area);
2037 r.width = window_box_width (s->w, s->area);
2038 r.height = s->row->visible_height;
2041 if (s->clip_head)
2042 if (r.x < s->clip_head->x)
2044 if (r.width >= s->clip_head->x - r.x)
2045 r.width -= s->clip_head->x - r.x;
2046 else
2047 r.width = 0;
2048 r.x = s->clip_head->x;
2050 if (s->clip_tail)
2051 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2053 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2054 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2055 else
2056 r.width = 0;
2059 /* If S draws overlapping rows, it's sufficient to use the top and
2060 bottom of the window for clipping because this glyph string
2061 intentionally draws over other lines. */
2062 if (s->for_overlaps)
2064 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2065 r.height = window_text_bottom_y (s->w) - r.y;
2067 /* Alas, the above simple strategy does not work for the
2068 environments with anti-aliased text: if the same text is
2069 drawn onto the same place multiple times, it gets thicker.
2070 If the overlap we are processing is for the erased cursor, we
2071 take the intersection with the rectangle of the cursor. */
2072 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2074 XRectangle rc, r_save = r;
2076 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2077 rc.y = s->w->phys_cursor.y;
2078 rc.width = s->w->phys_cursor_width;
2079 rc.height = s->w->phys_cursor_height;
2081 x_intersect_rectangles (&r_save, &rc, &r);
2084 else
2086 /* Don't use S->y for clipping because it doesn't take partially
2087 visible lines into account. For example, it can be negative for
2088 partially visible lines at the top of a window. */
2089 if (!s->row->full_width_p
2090 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2091 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2092 else
2093 r.y = max (0, s->row->y);
2096 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2098 /* If drawing the cursor, don't let glyph draw outside its
2099 advertised boundaries. Cleartype does this under some circumstances. */
2100 if (s->hl == DRAW_CURSOR)
2102 struct glyph *glyph = s->first_glyph;
2103 int height, max_y;
2105 if (s->x > r.x)
2107 r.width -= s->x - r.x;
2108 r.x = s->x;
2110 r.width = min (r.width, glyph->pixel_width);
2112 /* If r.y is below window bottom, ensure that we still see a cursor. */
2113 height = min (glyph->ascent + glyph->descent,
2114 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2115 max_y = window_text_bottom_y (s->w) - height;
2116 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2117 if (s->ybase - glyph->ascent > max_y)
2119 r.y = max_y;
2120 r.height = height;
2122 else
2124 /* Don't draw cursor glyph taller than our actual glyph. */
2125 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2126 if (height < r.height)
2128 max_y = r.y + r.height;
2129 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2130 r.height = min (max_y - r.y, height);
2135 if (s->row->clip)
2137 XRectangle r_save = r;
2139 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2140 r.width = 0;
2143 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2144 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2146 #ifdef CONVERT_FROM_XRECT
2147 CONVERT_FROM_XRECT (r, *rects);
2148 #else
2149 *rects = r;
2150 #endif
2151 return 1;
2153 else
2155 /* If we are processing overlapping and allowed to return
2156 multiple clipping rectangles, we exclude the row of the glyph
2157 string from the clipping rectangle. This is to avoid drawing
2158 the same text on the environment with anti-aliasing. */
2159 #ifdef CONVERT_FROM_XRECT
2160 XRectangle rs[2];
2161 #else
2162 XRectangle *rs = rects;
2163 #endif
2164 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2166 if (s->for_overlaps & OVERLAPS_PRED)
2168 rs[i] = r;
2169 if (r.y + r.height > row_y)
2171 if (r.y < row_y)
2172 rs[i].height = row_y - r.y;
2173 else
2174 rs[i].height = 0;
2176 i++;
2178 if (s->for_overlaps & OVERLAPS_SUCC)
2180 rs[i] = r;
2181 if (r.y < row_y + s->row->visible_height)
2183 if (r.y + r.height > row_y + s->row->visible_height)
2185 rs[i].y = row_y + s->row->visible_height;
2186 rs[i].height = r.y + r.height - rs[i].y;
2188 else
2189 rs[i].height = 0;
2191 i++;
2194 n = i;
2195 #ifdef CONVERT_FROM_XRECT
2196 for (i = 0; i < n; i++)
2197 CONVERT_FROM_XRECT (rs[i], rects[i]);
2198 #endif
2199 return n;
2203 /* EXPORT:
2204 Return in *NR the clipping rectangle for glyph string S. */
2206 void
2207 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2209 get_glyph_string_clip_rects (s, nr, 1);
2213 /* EXPORT:
2214 Return the position and height of the phys cursor in window W.
2215 Set w->phys_cursor_width to width of phys cursor.
2218 void
2219 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2220 struct glyph *glyph, int *xp, int *yp, int *heightp)
2222 struct frame *f = XFRAME (WINDOW_FRAME (w));
2223 int x, y, wd, h, h0, y0;
2225 /* Compute the width of the rectangle to draw. If on a stretch
2226 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2227 rectangle as wide as the glyph, but use a canonical character
2228 width instead. */
2229 wd = glyph->pixel_width - 1;
2230 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2231 wd++; /* Why? */
2232 #endif
2234 x = w->phys_cursor.x;
2235 if (x < 0)
2237 wd += x;
2238 x = 0;
2241 if (glyph->type == STRETCH_GLYPH
2242 && !x_stretch_cursor_p)
2243 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2244 w->phys_cursor_width = wd;
2246 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2248 /* If y is below window bottom, ensure that we still see a cursor. */
2249 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2251 h = max (h0, glyph->ascent + glyph->descent);
2252 h0 = min (h0, glyph->ascent + glyph->descent);
2254 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2255 if (y < y0)
2257 h = max (h - (y0 - y) + 1, h0);
2258 y = y0 - 1;
2260 else
2262 y0 = window_text_bottom_y (w) - h0;
2263 if (y > y0)
2265 h += y - y0;
2266 y = y0;
2270 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2271 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2272 *heightp = h;
2276 * Remember which glyph the mouse is over.
2279 void
2280 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2282 Lisp_Object window;
2283 struct window *w;
2284 struct glyph_row *r, *gr, *end_row;
2285 enum window_part part;
2286 enum glyph_row_area area;
2287 int x, y, width, height;
2289 /* Try to determine frame pixel position and size of the glyph under
2290 frame pixel coordinates X/Y on frame F. */
2292 if (!f->glyphs_initialized_p
2293 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2294 NILP (window)))
2296 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2297 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2298 goto virtual_glyph;
2301 w = XWINDOW (window);
2302 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2303 height = WINDOW_FRAME_LINE_HEIGHT (w);
2305 x = window_relative_x_coord (w, part, gx);
2306 y = gy - WINDOW_TOP_EDGE_Y (w);
2308 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2309 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2311 if (w->pseudo_window_p)
2313 area = TEXT_AREA;
2314 part = ON_MODE_LINE; /* Don't adjust margin. */
2315 goto text_glyph;
2318 switch (part)
2320 case ON_LEFT_MARGIN:
2321 area = LEFT_MARGIN_AREA;
2322 goto text_glyph;
2324 case ON_RIGHT_MARGIN:
2325 area = RIGHT_MARGIN_AREA;
2326 goto text_glyph;
2328 case ON_HEADER_LINE:
2329 case ON_MODE_LINE:
2330 gr = (part == ON_HEADER_LINE
2331 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2332 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2333 gy = gr->y;
2334 area = TEXT_AREA;
2335 goto text_glyph_row_found;
2337 case ON_TEXT:
2338 area = TEXT_AREA;
2340 text_glyph:
2341 gr = 0; gy = 0;
2342 for (; r <= end_row && r->enabled_p; ++r)
2343 if (r->y + r->height > y)
2345 gr = r; gy = r->y;
2346 break;
2349 text_glyph_row_found:
2350 if (gr && gy <= y)
2352 struct glyph *g = gr->glyphs[area];
2353 struct glyph *end = g + gr->used[area];
2355 height = gr->height;
2356 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2357 if (gx + g->pixel_width > x)
2358 break;
2360 if (g < end)
2362 if (g->type == IMAGE_GLYPH)
2364 /* Don't remember when mouse is over image, as
2365 image may have hot-spots. */
2366 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2367 return;
2369 width = g->pixel_width;
2371 else
2373 /* Use nominal char spacing at end of line. */
2374 x -= gx;
2375 gx += (x / width) * width;
2378 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2379 gx += window_box_left_offset (w, area);
2381 else
2383 /* Use nominal line height at end of window. */
2384 gx = (x / width) * width;
2385 y -= gy;
2386 gy += (y / height) * height;
2388 break;
2390 case ON_LEFT_FRINGE:
2391 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2392 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2393 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2394 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2395 goto row_glyph;
2397 case ON_RIGHT_FRINGE:
2398 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2399 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2400 : window_box_right_offset (w, TEXT_AREA));
2401 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2402 goto row_glyph;
2404 case ON_SCROLL_BAR:
2405 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2407 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2408 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2409 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2410 : 0)));
2411 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2413 row_glyph:
2414 gr = 0, gy = 0;
2415 for (; r <= end_row && r->enabled_p; ++r)
2416 if (r->y + r->height > y)
2418 gr = r; gy = r->y;
2419 break;
2422 if (gr && gy <= y)
2423 height = gr->height;
2424 else
2426 /* Use nominal line height at end of window. */
2427 y -= gy;
2428 gy += (y / height) * height;
2430 break;
2432 default:
2434 virtual_glyph:
2435 /* If there is no glyph under the mouse, then we divide the screen
2436 into a grid of the smallest glyph in the frame, and use that
2437 as our "glyph". */
2439 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2440 round down even for negative values. */
2441 if (gx < 0)
2442 gx -= width - 1;
2443 if (gy < 0)
2444 gy -= height - 1;
2446 gx = (gx / width) * width;
2447 gy = (gy / height) * height;
2449 goto store_rect;
2452 gx += WINDOW_LEFT_EDGE_X (w);
2453 gy += WINDOW_TOP_EDGE_Y (w);
2455 store_rect:
2456 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2458 /* Visible feedback for debugging. */
2459 #if 0
2460 #if HAVE_X_WINDOWS
2461 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2462 f->output_data.x->normal_gc,
2463 gx, gy, width, height);
2464 #endif
2465 #endif
2469 #endif /* HAVE_WINDOW_SYSTEM */
2471 static void
2472 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2474 eassert (w);
2475 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2476 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2477 w->window_end_vpos
2478 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2481 /***********************************************************************
2482 Lisp form evaluation
2483 ***********************************************************************/
2485 /* Error handler for safe_eval and safe_call. */
2487 static Lisp_Object
2488 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2490 add_to_log ("Error during redisplay: %S signaled %S",
2491 Flist (nargs, args), arg);
2492 return Qnil;
2495 /* Call function FUNC with the rest of NARGS - 1 arguments
2496 following. Return the result, or nil if something went
2497 wrong. Prevent redisplay during the evaluation. */
2499 Lisp_Object
2500 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2502 Lisp_Object val;
2504 if (inhibit_eval_during_redisplay)
2505 val = Qnil;
2506 else
2508 va_list ap;
2509 ptrdiff_t i;
2510 ptrdiff_t count = SPECPDL_INDEX ();
2511 struct gcpro gcpro1;
2512 Lisp_Object *args = alloca (nargs * word_size);
2514 args[0] = func;
2515 va_start (ap, func);
2516 for (i = 1; i < nargs; i++)
2517 args[i] = va_arg (ap, Lisp_Object);
2518 va_end (ap);
2520 GCPRO1 (args[0]);
2521 gcpro1.nvars = nargs;
2522 specbind (Qinhibit_redisplay, Qt);
2523 /* Use Qt to ensure debugger does not run,
2524 so there is no possibility of wanting to redisplay. */
2525 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2526 safe_eval_handler);
2527 UNGCPRO;
2528 val = unbind_to (count, val);
2531 return val;
2535 /* Call function FN with one argument ARG.
2536 Return the result, or nil if something went wrong. */
2538 Lisp_Object
2539 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2541 return safe_call (2, fn, arg);
2544 static Lisp_Object Qeval;
2546 Lisp_Object
2547 safe_eval (Lisp_Object sexpr)
2549 return safe_call1 (Qeval, sexpr);
2552 /* Call function FN with two arguments ARG1 and ARG2.
2553 Return the result, or nil if something went wrong. */
2555 Lisp_Object
2556 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2558 return safe_call (3, fn, arg1, arg2);
2563 /***********************************************************************
2564 Debugging
2565 ***********************************************************************/
2567 #if 0
2569 /* Define CHECK_IT to perform sanity checks on iterators.
2570 This is for debugging. It is too slow to do unconditionally. */
2572 static void
2573 check_it (struct it *it)
2575 if (it->method == GET_FROM_STRING)
2577 eassert (STRINGP (it->string));
2578 eassert (IT_STRING_CHARPOS (*it) >= 0);
2580 else
2582 eassert (IT_STRING_CHARPOS (*it) < 0);
2583 if (it->method == GET_FROM_BUFFER)
2585 /* Check that character and byte positions agree. */
2586 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2590 if (it->dpvec)
2591 eassert (it->current.dpvec_index >= 0);
2592 else
2593 eassert (it->current.dpvec_index < 0);
2596 #define CHECK_IT(IT) check_it ((IT))
2598 #else /* not 0 */
2600 #define CHECK_IT(IT) (void) 0
2602 #endif /* not 0 */
2605 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2607 /* Check that the window end of window W is what we expect it
2608 to be---the last row in the current matrix displaying text. */
2610 static void
2611 check_window_end (struct window *w)
2613 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2615 struct glyph_row *row;
2616 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2617 !row->enabled_p
2618 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2619 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2623 #define CHECK_WINDOW_END(W) check_window_end ((W))
2625 #else
2627 #define CHECK_WINDOW_END(W) (void) 0
2629 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2631 /* Return mark position if current buffer has the region of non-zero length,
2632 or -1 otherwise. */
2634 static ptrdiff_t
2635 markpos_of_region (void)
2637 if (!NILP (Vtransient_mark_mode)
2638 && !NILP (BVAR (current_buffer, mark_active))
2639 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2641 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2643 if (markpos != PT)
2644 return markpos;
2646 return -1;
2649 /***********************************************************************
2650 Iterator initialization
2651 ***********************************************************************/
2653 /* Initialize IT for displaying current_buffer in window W, starting
2654 at character position CHARPOS. CHARPOS < 0 means that no buffer
2655 position is specified which is useful when the iterator is assigned
2656 a position later. BYTEPOS is the byte position corresponding to
2657 CHARPOS.
2659 If ROW is not null, calls to produce_glyphs with IT as parameter
2660 will produce glyphs in that row.
2662 BASE_FACE_ID is the id of a base face to use. It must be one of
2663 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2664 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2665 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2667 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2668 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2669 will be initialized to use the corresponding mode line glyph row of
2670 the desired matrix of W. */
2672 void
2673 init_iterator (struct it *it, struct window *w,
2674 ptrdiff_t charpos, ptrdiff_t bytepos,
2675 struct glyph_row *row, enum face_id base_face_id)
2677 ptrdiff_t markpos;
2678 enum face_id remapped_base_face_id = base_face_id;
2680 /* Some precondition checks. */
2681 eassert (w != NULL && it != NULL);
2682 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2683 && charpos <= ZV));
2685 /* If face attributes have been changed since the last redisplay,
2686 free realized faces now because they depend on face definitions
2687 that might have changed. Don't free faces while there might be
2688 desired matrices pending which reference these faces. */
2689 if (face_change_count && !inhibit_free_realized_faces)
2691 face_change_count = 0;
2692 free_all_realized_faces (Qnil);
2695 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2696 if (! NILP (Vface_remapping_alist))
2697 remapped_base_face_id
2698 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2700 /* Use one of the mode line rows of W's desired matrix if
2701 appropriate. */
2702 if (row == NULL)
2704 if (base_face_id == MODE_LINE_FACE_ID
2705 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2706 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2707 else if (base_face_id == HEADER_LINE_FACE_ID)
2708 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2711 /* Clear IT. */
2712 memset (it, 0, sizeof *it);
2713 it->current.overlay_string_index = -1;
2714 it->current.dpvec_index = -1;
2715 it->base_face_id = remapped_base_face_id;
2716 it->string = Qnil;
2717 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2718 it->paragraph_embedding = L2R;
2719 it->bidi_it.string.lstring = Qnil;
2720 it->bidi_it.string.s = NULL;
2721 it->bidi_it.string.bufpos = 0;
2722 it->bidi_it.w = w;
2724 /* The window in which we iterate over current_buffer: */
2725 XSETWINDOW (it->window, w);
2726 it->w = w;
2727 it->f = XFRAME (w->frame);
2729 it->cmp_it.id = -1;
2731 /* Extra space between lines (on window systems only). */
2732 if (base_face_id == DEFAULT_FACE_ID
2733 && FRAME_WINDOW_P (it->f))
2735 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2736 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2737 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2738 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2739 * FRAME_LINE_HEIGHT (it->f));
2740 else if (it->f->extra_line_spacing > 0)
2741 it->extra_line_spacing = it->f->extra_line_spacing;
2742 it->max_extra_line_spacing = 0;
2745 /* If realized faces have been removed, e.g. because of face
2746 attribute changes of named faces, recompute them. When running
2747 in batch mode, the face cache of the initial frame is null. If
2748 we happen to get called, make a dummy face cache. */
2749 if (FRAME_FACE_CACHE (it->f) == NULL)
2750 init_frame_faces (it->f);
2751 if (FRAME_FACE_CACHE (it->f)->used == 0)
2752 recompute_basic_faces (it->f);
2754 /* Current value of the `slice', `space-width', and 'height' properties. */
2755 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2756 it->space_width = Qnil;
2757 it->font_height = Qnil;
2758 it->override_ascent = -1;
2760 /* Are control characters displayed as `^C'? */
2761 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2763 /* -1 means everything between a CR and the following line end
2764 is invisible. >0 means lines indented more than this value are
2765 invisible. */
2766 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2767 ? (clip_to_bounds
2768 (-1, XINT (BVAR (current_buffer, selective_display)),
2769 PTRDIFF_MAX))
2770 : (!NILP (BVAR (current_buffer, selective_display))
2771 ? -1 : 0));
2772 it->selective_display_ellipsis_p
2773 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2775 /* Display table to use. */
2776 it->dp = window_display_table (w);
2778 /* Are multibyte characters enabled in current_buffer? */
2779 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2781 /* If visible region is of non-zero length, set IT->region_beg_charpos
2782 and IT->region_end_charpos to the start and end of a visible region
2783 in window IT->w. Set both to -1 to indicate no region. */
2784 markpos = markpos_of_region ();
2785 if (markpos >= 0
2786 /* Maybe highlight only in selected window. */
2787 && (/* Either show region everywhere. */
2788 highlight_nonselected_windows
2789 /* Or show region in the selected window. */
2790 || w == XWINDOW (selected_window)
2791 /* Or show the region if we are in the mini-buffer and W is
2792 the window the mini-buffer refers to. */
2793 || (MINI_WINDOW_P (XWINDOW (selected_window))
2794 && WINDOWP (minibuf_selected_window)
2795 && w == XWINDOW (minibuf_selected_window))))
2797 it->region_beg_charpos = min (PT, markpos);
2798 it->region_end_charpos = max (PT, markpos);
2800 else
2801 it->region_beg_charpos = it->region_end_charpos = -1;
2803 /* Get the position at which the redisplay_end_trigger hook should
2804 be run, if it is to be run at all. */
2805 if (MARKERP (w->redisplay_end_trigger)
2806 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2807 it->redisplay_end_trigger_charpos
2808 = marker_position (w->redisplay_end_trigger);
2809 else if (INTEGERP (w->redisplay_end_trigger))
2810 it->redisplay_end_trigger_charpos =
2811 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2813 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2815 /* Are lines in the display truncated? */
2816 if (base_face_id != DEFAULT_FACE_ID
2817 || it->w->hscroll
2818 || (! WINDOW_FULL_WIDTH_P (it->w)
2819 && ((!NILP (Vtruncate_partial_width_windows)
2820 && !INTEGERP (Vtruncate_partial_width_windows))
2821 || (INTEGERP (Vtruncate_partial_width_windows)
2822 && (WINDOW_TOTAL_COLS (it->w)
2823 < XINT (Vtruncate_partial_width_windows))))))
2824 it->line_wrap = TRUNCATE;
2825 else if (NILP (BVAR (current_buffer, truncate_lines)))
2826 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2827 ? WINDOW_WRAP : WORD_WRAP;
2828 else
2829 it->line_wrap = TRUNCATE;
2831 /* Get dimensions of truncation and continuation glyphs. These are
2832 displayed as fringe bitmaps under X, but we need them for such
2833 frames when the fringes are turned off. But leave the dimensions
2834 zero for tooltip frames, as these glyphs look ugly there and also
2835 sabotage calculations of tooltip dimensions in x-show-tip. */
2836 #ifdef HAVE_WINDOW_SYSTEM
2837 if (!(FRAME_WINDOW_P (it->f)
2838 && FRAMEP (tip_frame)
2839 && it->f == XFRAME (tip_frame)))
2840 #endif
2842 if (it->line_wrap == TRUNCATE)
2844 /* We will need the truncation glyph. */
2845 eassert (it->glyph_row == NULL);
2846 produce_special_glyphs (it, IT_TRUNCATION);
2847 it->truncation_pixel_width = it->pixel_width;
2849 else
2851 /* We will need the continuation glyph. */
2852 eassert (it->glyph_row == NULL);
2853 produce_special_glyphs (it, IT_CONTINUATION);
2854 it->continuation_pixel_width = it->pixel_width;
2858 /* Reset these values to zero because the produce_special_glyphs
2859 above has changed them. */
2860 it->pixel_width = it->ascent = it->descent = 0;
2861 it->phys_ascent = it->phys_descent = 0;
2863 /* Set this after getting the dimensions of truncation and
2864 continuation glyphs, so that we don't produce glyphs when calling
2865 produce_special_glyphs, above. */
2866 it->glyph_row = row;
2867 it->area = TEXT_AREA;
2869 /* Forget any previous info about this row being reversed. */
2870 if (it->glyph_row)
2871 it->glyph_row->reversed_p = 0;
2873 /* Get the dimensions of the display area. The display area
2874 consists of the visible window area plus a horizontally scrolled
2875 part to the left of the window. All x-values are relative to the
2876 start of this total display area. */
2877 if (base_face_id != DEFAULT_FACE_ID)
2879 /* Mode lines, menu bar in terminal frames. */
2880 it->first_visible_x = 0;
2881 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2883 else
2885 it->first_visible_x =
2886 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2887 it->last_visible_x = (it->first_visible_x
2888 + window_box_width (w, TEXT_AREA));
2890 /* If we truncate lines, leave room for the truncation glyph(s) at
2891 the right margin. Otherwise, leave room for the continuation
2892 glyph(s). Done only if the window has no fringes. Since we
2893 don't know at this point whether there will be any R2L lines in
2894 the window, we reserve space for truncation/continuation glyphs
2895 even if only one of the fringes is absent. */
2896 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2897 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2899 if (it->line_wrap == TRUNCATE)
2900 it->last_visible_x -= it->truncation_pixel_width;
2901 else
2902 it->last_visible_x -= it->continuation_pixel_width;
2905 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2906 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2909 /* Leave room for a border glyph. */
2910 if (!FRAME_WINDOW_P (it->f)
2911 && !WINDOW_RIGHTMOST_P (it->w))
2912 it->last_visible_x -= 1;
2914 it->last_visible_y = window_text_bottom_y (w);
2916 /* For mode lines and alike, arrange for the first glyph having a
2917 left box line if the face specifies a box. */
2918 if (base_face_id != DEFAULT_FACE_ID)
2920 struct face *face;
2922 it->face_id = remapped_base_face_id;
2924 /* If we have a boxed mode line, make the first character appear
2925 with a left box line. */
2926 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2927 if (face->box != FACE_NO_BOX)
2928 it->start_of_box_run_p = 1;
2931 /* If a buffer position was specified, set the iterator there,
2932 getting overlays and face properties from that position. */
2933 if (charpos >= BUF_BEG (current_buffer))
2935 it->end_charpos = ZV;
2936 eassert (charpos == BYTE_TO_CHAR (bytepos));
2937 IT_CHARPOS (*it) = charpos;
2938 IT_BYTEPOS (*it) = bytepos;
2940 /* We will rely on `reseat' to set this up properly, via
2941 handle_face_prop. */
2942 it->face_id = it->base_face_id;
2944 it->start = it->current;
2945 /* Do we need to reorder bidirectional text? Not if this is a
2946 unibyte buffer: by definition, none of the single-byte
2947 characters are strong R2L, so no reordering is needed. And
2948 bidi.c doesn't support unibyte buffers anyway. Also, don't
2949 reorder while we are loading loadup.el, since the tables of
2950 character properties needed for reordering are not yet
2951 available. */
2952 it->bidi_p =
2953 NILP (Vpurify_flag)
2954 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2955 && it->multibyte_p;
2957 /* If we are to reorder bidirectional text, init the bidi
2958 iterator. */
2959 if (it->bidi_p)
2961 /* Note the paragraph direction that this buffer wants to
2962 use. */
2963 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2964 Qleft_to_right))
2965 it->paragraph_embedding = L2R;
2966 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2967 Qright_to_left))
2968 it->paragraph_embedding = R2L;
2969 else
2970 it->paragraph_embedding = NEUTRAL_DIR;
2971 bidi_unshelve_cache (NULL, 0);
2972 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2973 &it->bidi_it);
2976 /* Compute faces etc. */
2977 reseat (it, it->current.pos, 1);
2980 CHECK_IT (it);
2984 /* Initialize IT for the display of window W with window start POS. */
2986 void
2987 start_display (struct it *it, struct window *w, struct text_pos pos)
2989 struct glyph_row *row;
2990 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2992 row = w->desired_matrix->rows + first_vpos;
2993 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2994 it->first_vpos = first_vpos;
2996 /* Don't reseat to previous visible line start if current start
2997 position is in a string or image. */
2998 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3000 int start_at_line_beg_p;
3001 int first_y = it->current_y;
3003 /* If window start is not at a line start, skip forward to POS to
3004 get the correct continuation lines width. */
3005 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3006 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3007 if (!start_at_line_beg_p)
3009 int new_x;
3011 reseat_at_previous_visible_line_start (it);
3012 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3014 new_x = it->current_x + it->pixel_width;
3016 /* If lines are continued, this line may end in the middle
3017 of a multi-glyph character (e.g. a control character
3018 displayed as \003, or in the middle of an overlay
3019 string). In this case move_it_to above will not have
3020 taken us to the start of the continuation line but to the
3021 end of the continued line. */
3022 if (it->current_x > 0
3023 && it->line_wrap != TRUNCATE /* Lines are continued. */
3024 && (/* And glyph doesn't fit on the line. */
3025 new_x > it->last_visible_x
3026 /* Or it fits exactly and we're on a window
3027 system frame. */
3028 || (new_x == it->last_visible_x
3029 && FRAME_WINDOW_P (it->f)
3030 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3031 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3032 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3034 if ((it->current.dpvec_index >= 0
3035 || it->current.overlay_string_index >= 0)
3036 /* If we are on a newline from a display vector or
3037 overlay string, then we are already at the end of
3038 a screen line; no need to go to the next line in
3039 that case, as this line is not really continued.
3040 (If we do go to the next line, C-e will not DTRT.) */
3041 && it->c != '\n')
3043 set_iterator_to_next (it, 1);
3044 move_it_in_display_line_to (it, -1, -1, 0);
3047 it->continuation_lines_width += it->current_x;
3049 /* If the character at POS is displayed via a display
3050 vector, move_it_to above stops at the final glyph of
3051 IT->dpvec. To make the caller redisplay that character
3052 again (a.k.a. start at POS), we need to reset the
3053 dpvec_index to the beginning of IT->dpvec. */
3054 else if (it->current.dpvec_index >= 0)
3055 it->current.dpvec_index = 0;
3057 /* We're starting a new display line, not affected by the
3058 height of the continued line, so clear the appropriate
3059 fields in the iterator structure. */
3060 it->max_ascent = it->max_descent = 0;
3061 it->max_phys_ascent = it->max_phys_descent = 0;
3063 it->current_y = first_y;
3064 it->vpos = 0;
3065 it->current_x = it->hpos = 0;
3071 /* Return 1 if POS is a position in ellipses displayed for invisible
3072 text. W is the window we display, for text property lookup. */
3074 static int
3075 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3077 Lisp_Object prop, window;
3078 int ellipses_p = 0;
3079 ptrdiff_t charpos = CHARPOS (pos->pos);
3081 /* If POS specifies a position in a display vector, this might
3082 be for an ellipsis displayed for invisible text. We won't
3083 get the iterator set up for delivering that ellipsis unless
3084 we make sure that it gets aware of the invisible text. */
3085 if (pos->dpvec_index >= 0
3086 && pos->overlay_string_index < 0
3087 && CHARPOS (pos->string_pos) < 0
3088 && charpos > BEGV
3089 && (XSETWINDOW (window, w),
3090 prop = Fget_char_property (make_number (charpos),
3091 Qinvisible, window),
3092 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3094 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3095 window);
3096 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3099 return ellipses_p;
3103 /* Initialize IT for stepping through current_buffer in window W,
3104 starting at position POS that includes overlay string and display
3105 vector/ control character translation position information. Value
3106 is zero if there are overlay strings with newlines at POS. */
3108 static int
3109 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3111 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3112 int i, overlay_strings_with_newlines = 0;
3114 /* If POS specifies a position in a display vector, this might
3115 be for an ellipsis displayed for invisible text. We won't
3116 get the iterator set up for delivering that ellipsis unless
3117 we make sure that it gets aware of the invisible text. */
3118 if (in_ellipses_for_invisible_text_p (pos, w))
3120 --charpos;
3121 bytepos = 0;
3124 /* Keep in mind: the call to reseat in init_iterator skips invisible
3125 text, so we might end up at a position different from POS. This
3126 is only a problem when POS is a row start after a newline and an
3127 overlay starts there with an after-string, and the overlay has an
3128 invisible property. Since we don't skip invisible text in
3129 display_line and elsewhere immediately after consuming the
3130 newline before the row start, such a POS will not be in a string,
3131 but the call to init_iterator below will move us to the
3132 after-string. */
3133 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3135 /* This only scans the current chunk -- it should scan all chunks.
3136 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3137 to 16 in 22.1 to make this a lesser problem. */
3138 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3140 const char *s = SSDATA (it->overlay_strings[i]);
3141 const char *e = s + SBYTES (it->overlay_strings[i]);
3143 while (s < e && *s != '\n')
3144 ++s;
3146 if (s < e)
3148 overlay_strings_with_newlines = 1;
3149 break;
3153 /* If position is within an overlay string, set up IT to the right
3154 overlay string. */
3155 if (pos->overlay_string_index >= 0)
3157 int relative_index;
3159 /* If the first overlay string happens to have a `display'
3160 property for an image, the iterator will be set up for that
3161 image, and we have to undo that setup first before we can
3162 correct the overlay string index. */
3163 if (it->method == GET_FROM_IMAGE)
3164 pop_it (it);
3166 /* We already have the first chunk of overlay strings in
3167 IT->overlay_strings. Load more until the one for
3168 pos->overlay_string_index is in IT->overlay_strings. */
3169 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3171 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3172 it->current.overlay_string_index = 0;
3173 while (n--)
3175 load_overlay_strings (it, 0);
3176 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3180 it->current.overlay_string_index = pos->overlay_string_index;
3181 relative_index = (it->current.overlay_string_index
3182 % OVERLAY_STRING_CHUNK_SIZE);
3183 it->string = it->overlay_strings[relative_index];
3184 eassert (STRINGP (it->string));
3185 it->current.string_pos = pos->string_pos;
3186 it->method = GET_FROM_STRING;
3187 it->end_charpos = SCHARS (it->string);
3188 /* Set up the bidi iterator for this overlay string. */
3189 if (it->bidi_p)
3191 it->bidi_it.string.lstring = it->string;
3192 it->bidi_it.string.s = NULL;
3193 it->bidi_it.string.schars = SCHARS (it->string);
3194 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3195 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3196 it->bidi_it.string.unibyte = !it->multibyte_p;
3197 it->bidi_it.w = it->w;
3198 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3199 FRAME_WINDOW_P (it->f), &it->bidi_it);
3201 /* Synchronize the state of the bidi iterator with
3202 pos->string_pos. For any string position other than
3203 zero, this will be done automagically when we resume
3204 iteration over the string and get_visually_first_element
3205 is called. But if string_pos is zero, and the string is
3206 to be reordered for display, we need to resync manually,
3207 since it could be that the iteration state recorded in
3208 pos ended at string_pos of 0 moving backwards in string. */
3209 if (CHARPOS (pos->string_pos) == 0)
3211 get_visually_first_element (it);
3212 if (IT_STRING_CHARPOS (*it) != 0)
3213 do {
3214 /* Paranoia. */
3215 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3216 bidi_move_to_visually_next (&it->bidi_it);
3217 } while (it->bidi_it.charpos != 0);
3219 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3220 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3224 if (CHARPOS (pos->string_pos) >= 0)
3226 /* Recorded position is not in an overlay string, but in another
3227 string. This can only be a string from a `display' property.
3228 IT should already be filled with that string. */
3229 it->current.string_pos = pos->string_pos;
3230 eassert (STRINGP (it->string));
3231 if (it->bidi_p)
3232 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3233 FRAME_WINDOW_P (it->f), &it->bidi_it);
3236 /* Restore position in display vector translations, control
3237 character translations or ellipses. */
3238 if (pos->dpvec_index >= 0)
3240 if (it->dpvec == NULL)
3241 get_next_display_element (it);
3242 eassert (it->dpvec && it->current.dpvec_index == 0);
3243 it->current.dpvec_index = pos->dpvec_index;
3246 CHECK_IT (it);
3247 return !overlay_strings_with_newlines;
3251 /* Initialize IT for stepping through current_buffer in window W
3252 starting at ROW->start. */
3254 static void
3255 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3257 init_from_display_pos (it, w, &row->start);
3258 it->start = row->start;
3259 it->continuation_lines_width = row->continuation_lines_width;
3260 CHECK_IT (it);
3264 /* Initialize IT for stepping through current_buffer in window W
3265 starting in the line following ROW, i.e. starting at ROW->end.
3266 Value is zero if there are overlay strings with newlines at ROW's
3267 end position. */
3269 static int
3270 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3272 int success = 0;
3274 if (init_from_display_pos (it, w, &row->end))
3276 if (row->continued_p)
3277 it->continuation_lines_width
3278 = row->continuation_lines_width + row->pixel_width;
3279 CHECK_IT (it);
3280 success = 1;
3283 return success;
3289 /***********************************************************************
3290 Text properties
3291 ***********************************************************************/
3293 /* Called when IT reaches IT->stop_charpos. Handle text property and
3294 overlay changes. Set IT->stop_charpos to the next position where
3295 to stop. */
3297 static void
3298 handle_stop (struct it *it)
3300 enum prop_handled handled;
3301 int handle_overlay_change_p;
3302 struct props *p;
3304 it->dpvec = NULL;
3305 it->current.dpvec_index = -1;
3306 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3307 it->ignore_overlay_strings_at_pos_p = 0;
3308 it->ellipsis_p = 0;
3310 /* Use face of preceding text for ellipsis (if invisible) */
3311 if (it->selective_display_ellipsis_p)
3312 it->saved_face_id = it->face_id;
3316 handled = HANDLED_NORMALLY;
3318 /* Call text property handlers. */
3319 for (p = it_props; p->handler; ++p)
3321 handled = p->handler (it);
3323 if (handled == HANDLED_RECOMPUTE_PROPS)
3324 break;
3325 else if (handled == HANDLED_RETURN)
3327 /* We still want to show before and after strings from
3328 overlays even if the actual buffer text is replaced. */
3329 if (!handle_overlay_change_p
3330 || it->sp > 1
3331 /* Don't call get_overlay_strings_1 if we already
3332 have overlay strings loaded, because doing so
3333 will load them again and push the iterator state
3334 onto the stack one more time, which is not
3335 expected by the rest of the code that processes
3336 overlay strings. */
3337 || (it->current.overlay_string_index < 0
3338 ? !get_overlay_strings_1 (it, 0, 0)
3339 : 0))
3341 if (it->ellipsis_p)
3342 setup_for_ellipsis (it, 0);
3343 /* When handling a display spec, we might load an
3344 empty string. In that case, discard it here. We
3345 used to discard it in handle_single_display_spec,
3346 but that causes get_overlay_strings_1, above, to
3347 ignore overlay strings that we must check. */
3348 if (STRINGP (it->string) && !SCHARS (it->string))
3349 pop_it (it);
3350 return;
3352 else if (STRINGP (it->string) && !SCHARS (it->string))
3353 pop_it (it);
3354 else
3356 it->ignore_overlay_strings_at_pos_p = 1;
3357 it->string_from_display_prop_p = 0;
3358 it->from_disp_prop_p = 0;
3359 handle_overlay_change_p = 0;
3361 handled = HANDLED_RECOMPUTE_PROPS;
3362 break;
3364 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3365 handle_overlay_change_p = 0;
3368 if (handled != HANDLED_RECOMPUTE_PROPS)
3370 /* Don't check for overlay strings below when set to deliver
3371 characters from a display vector. */
3372 if (it->method == GET_FROM_DISPLAY_VECTOR)
3373 handle_overlay_change_p = 0;
3375 /* Handle overlay changes.
3376 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3377 if it finds overlays. */
3378 if (handle_overlay_change_p)
3379 handled = handle_overlay_change (it);
3382 if (it->ellipsis_p)
3384 setup_for_ellipsis (it, 0);
3385 break;
3388 while (handled == HANDLED_RECOMPUTE_PROPS);
3390 /* Determine where to stop next. */
3391 if (handled == HANDLED_NORMALLY)
3392 compute_stop_pos (it);
3396 /* Compute IT->stop_charpos from text property and overlay change
3397 information for IT's current position. */
3399 static void
3400 compute_stop_pos (struct it *it)
3402 register INTERVAL iv, next_iv;
3403 Lisp_Object object, limit, position;
3404 ptrdiff_t charpos, bytepos;
3406 if (STRINGP (it->string))
3408 /* Strings are usually short, so don't limit the search for
3409 properties. */
3410 it->stop_charpos = it->end_charpos;
3411 object = it->string;
3412 limit = Qnil;
3413 charpos = IT_STRING_CHARPOS (*it);
3414 bytepos = IT_STRING_BYTEPOS (*it);
3416 else
3418 ptrdiff_t pos;
3420 /* If end_charpos is out of range for some reason, such as a
3421 misbehaving display function, rationalize it (Bug#5984). */
3422 if (it->end_charpos > ZV)
3423 it->end_charpos = ZV;
3424 it->stop_charpos = it->end_charpos;
3426 /* If next overlay change is in front of the current stop pos
3427 (which is IT->end_charpos), stop there. Note: value of
3428 next_overlay_change is point-max if no overlay change
3429 follows. */
3430 charpos = IT_CHARPOS (*it);
3431 bytepos = IT_BYTEPOS (*it);
3432 pos = next_overlay_change (charpos);
3433 if (pos < it->stop_charpos)
3434 it->stop_charpos = pos;
3436 /* If showing the region, we have to stop at the region
3437 start or end because the face might change there. */
3438 if (it->region_beg_charpos > 0)
3440 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3441 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3442 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3443 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3446 /* Set up variables for computing the stop position from text
3447 property changes. */
3448 XSETBUFFER (object, current_buffer);
3449 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3452 /* Get the interval containing IT's position. Value is a null
3453 interval if there isn't such an interval. */
3454 position = make_number (charpos);
3455 iv = validate_interval_range (object, &position, &position, 0);
3456 if (iv)
3458 Lisp_Object values_here[LAST_PROP_IDX];
3459 struct props *p;
3461 /* Get properties here. */
3462 for (p = it_props; p->handler; ++p)
3463 values_here[p->idx] = textget (iv->plist, *p->name);
3465 /* Look for an interval following iv that has different
3466 properties. */
3467 for (next_iv = next_interval (iv);
3468 (next_iv
3469 && (NILP (limit)
3470 || XFASTINT (limit) > next_iv->position));
3471 next_iv = next_interval (next_iv))
3473 for (p = it_props; p->handler; ++p)
3475 Lisp_Object new_value;
3477 new_value = textget (next_iv->plist, *p->name);
3478 if (!EQ (values_here[p->idx], new_value))
3479 break;
3482 if (p->handler)
3483 break;
3486 if (next_iv)
3488 if (INTEGERP (limit)
3489 && next_iv->position >= XFASTINT (limit))
3490 /* No text property change up to limit. */
3491 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3492 else
3493 /* Text properties change in next_iv. */
3494 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3498 if (it->cmp_it.id < 0)
3500 ptrdiff_t stoppos = it->end_charpos;
3502 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3503 stoppos = -1;
3504 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3505 stoppos, it->string);
3508 eassert (STRINGP (it->string)
3509 || (it->stop_charpos >= BEGV
3510 && it->stop_charpos >= IT_CHARPOS (*it)));
3514 /* Return the position of the next overlay change after POS in
3515 current_buffer. Value is point-max if no overlay change
3516 follows. This is like `next-overlay-change' but doesn't use
3517 xmalloc. */
3519 static ptrdiff_t
3520 next_overlay_change (ptrdiff_t pos)
3522 ptrdiff_t i, noverlays;
3523 ptrdiff_t endpos;
3524 Lisp_Object *overlays;
3526 /* Get all overlays at the given position. */
3527 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3529 /* If any of these overlays ends before endpos,
3530 use its ending point instead. */
3531 for (i = 0; i < noverlays; ++i)
3533 Lisp_Object oend;
3534 ptrdiff_t oendpos;
3536 oend = OVERLAY_END (overlays[i]);
3537 oendpos = OVERLAY_POSITION (oend);
3538 endpos = min (endpos, oendpos);
3541 return endpos;
3544 /* How many characters forward to search for a display property or
3545 display string. Searching too far forward makes the bidi display
3546 sluggish, especially in small windows. */
3547 #define MAX_DISP_SCAN 250
3549 /* Return the character position of a display string at or after
3550 position specified by POSITION. If no display string exists at or
3551 after POSITION, return ZV. A display string is either an overlay
3552 with `display' property whose value is a string, or a `display'
3553 text property whose value is a string. STRING is data about the
3554 string to iterate; if STRING->lstring is nil, we are iterating a
3555 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3556 on a GUI frame. DISP_PROP is set to zero if we searched
3557 MAX_DISP_SCAN characters forward without finding any display
3558 strings, non-zero otherwise. It is set to 2 if the display string
3559 uses any kind of `(space ...)' spec that will produce a stretch of
3560 white space in the text area. */
3561 ptrdiff_t
3562 compute_display_string_pos (struct text_pos *position,
3563 struct bidi_string_data *string,
3564 struct window *w,
3565 int frame_window_p, int *disp_prop)
3567 /* OBJECT = nil means current buffer. */
3568 Lisp_Object object, object1;
3569 Lisp_Object pos, spec, limpos;
3570 int string_p = (string && (STRINGP (string->lstring) || string->s));
3571 ptrdiff_t eob = string_p ? string->schars : ZV;
3572 ptrdiff_t begb = string_p ? 0 : BEGV;
3573 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3574 ptrdiff_t lim =
3575 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3576 struct text_pos tpos;
3577 int rv = 0;
3579 if (string && STRINGP (string->lstring))
3580 object1 = object = string->lstring;
3581 else if (w && !string_p)
3583 XSETWINDOW (object, w);
3584 object1 = Qnil;
3586 else
3587 object1 = object = Qnil;
3589 *disp_prop = 1;
3591 if (charpos >= eob
3592 /* We don't support display properties whose values are strings
3593 that have display string properties. */
3594 || string->from_disp_str
3595 /* C strings cannot have display properties. */
3596 || (string->s && !STRINGP (object)))
3598 *disp_prop = 0;
3599 return eob;
3602 /* If the character at CHARPOS is where the display string begins,
3603 return CHARPOS. */
3604 pos = make_number (charpos);
3605 if (STRINGP (object))
3606 bufpos = string->bufpos;
3607 else
3608 bufpos = charpos;
3609 tpos = *position;
3610 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3611 && (charpos <= begb
3612 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3613 object),
3614 spec))
3615 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3616 frame_window_p)))
3618 if (rv == 2)
3619 *disp_prop = 2;
3620 return charpos;
3623 /* Look forward for the first character with a `display' property
3624 that will replace the underlying text when displayed. */
3625 limpos = make_number (lim);
3626 do {
3627 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3628 CHARPOS (tpos) = XFASTINT (pos);
3629 if (CHARPOS (tpos) >= lim)
3631 *disp_prop = 0;
3632 break;
3634 if (STRINGP (object))
3635 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3636 else
3637 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3638 spec = Fget_char_property (pos, Qdisplay, object);
3639 if (!STRINGP (object))
3640 bufpos = CHARPOS (tpos);
3641 } while (NILP (spec)
3642 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3643 bufpos, frame_window_p)));
3644 if (rv == 2)
3645 *disp_prop = 2;
3647 return CHARPOS (tpos);
3650 /* Return the character position of the end of the display string that
3651 started at CHARPOS. If there's no display string at CHARPOS,
3652 return -1. A display string is either an overlay with `display'
3653 property whose value is a string or a `display' text property whose
3654 value is a string. */
3655 ptrdiff_t
3656 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3658 /* OBJECT = nil means current buffer. */
3659 Lisp_Object object =
3660 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3661 Lisp_Object pos = make_number (charpos);
3662 ptrdiff_t eob =
3663 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3665 if (charpos >= eob || (string->s && !STRINGP (object)))
3666 return eob;
3668 /* It could happen that the display property or overlay was removed
3669 since we found it in compute_display_string_pos above. One way
3670 this can happen is if JIT font-lock was called (through
3671 handle_fontified_prop), and jit-lock-functions remove text
3672 properties or overlays from the portion of buffer that includes
3673 CHARPOS. Muse mode is known to do that, for example. In this
3674 case, we return -1 to the caller, to signal that no display
3675 string is actually present at CHARPOS. See bidi_fetch_char for
3676 how this is handled.
3678 An alternative would be to never look for display properties past
3679 it->stop_charpos. But neither compute_display_string_pos nor
3680 bidi_fetch_char that calls it know or care where the next
3681 stop_charpos is. */
3682 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3683 return -1;
3685 /* Look forward for the first character where the `display' property
3686 changes. */
3687 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3689 return XFASTINT (pos);
3694 /***********************************************************************
3695 Fontification
3696 ***********************************************************************/
3698 /* Handle changes in the `fontified' property of the current buffer by
3699 calling hook functions from Qfontification_functions to fontify
3700 regions of text. */
3702 static enum prop_handled
3703 handle_fontified_prop (struct it *it)
3705 Lisp_Object prop, pos;
3706 enum prop_handled handled = HANDLED_NORMALLY;
3708 if (!NILP (Vmemory_full))
3709 return handled;
3711 /* Get the value of the `fontified' property at IT's current buffer
3712 position. (The `fontified' property doesn't have a special
3713 meaning in strings.) If the value is nil, call functions from
3714 Qfontification_functions. */
3715 if (!STRINGP (it->string)
3716 && it->s == NULL
3717 && !NILP (Vfontification_functions)
3718 && !NILP (Vrun_hooks)
3719 && (pos = make_number (IT_CHARPOS (*it)),
3720 prop = Fget_char_property (pos, Qfontified, Qnil),
3721 /* Ignore the special cased nil value always present at EOB since
3722 no amount of fontifying will be able to change it. */
3723 NILP (prop) && IT_CHARPOS (*it) < Z))
3725 ptrdiff_t count = SPECPDL_INDEX ();
3726 Lisp_Object val;
3727 struct buffer *obuf = current_buffer;
3728 int begv = BEGV, zv = ZV;
3729 int old_clip_changed = current_buffer->clip_changed;
3731 val = Vfontification_functions;
3732 specbind (Qfontification_functions, Qnil);
3734 eassert (it->end_charpos == ZV);
3736 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3737 safe_call1 (val, pos);
3738 else
3740 Lisp_Object fns, fn;
3741 struct gcpro gcpro1, gcpro2;
3743 fns = Qnil;
3744 GCPRO2 (val, fns);
3746 for (; CONSP (val); val = XCDR (val))
3748 fn = XCAR (val);
3750 if (EQ (fn, Qt))
3752 /* A value of t indicates this hook has a local
3753 binding; it means to run the global binding too.
3754 In a global value, t should not occur. If it
3755 does, we must ignore it to avoid an endless
3756 loop. */
3757 for (fns = Fdefault_value (Qfontification_functions);
3758 CONSP (fns);
3759 fns = XCDR (fns))
3761 fn = XCAR (fns);
3762 if (!EQ (fn, Qt))
3763 safe_call1 (fn, pos);
3766 else
3767 safe_call1 (fn, pos);
3770 UNGCPRO;
3773 unbind_to (count, Qnil);
3775 /* Fontification functions routinely call `save-restriction'.
3776 Normally, this tags clip_changed, which can confuse redisplay
3777 (see discussion in Bug#6671). Since we don't perform any
3778 special handling of fontification changes in the case where
3779 `save-restriction' isn't called, there's no point doing so in
3780 this case either. So, if the buffer's restrictions are
3781 actually left unchanged, reset clip_changed. */
3782 if (obuf == current_buffer)
3784 if (begv == BEGV && zv == ZV)
3785 current_buffer->clip_changed = old_clip_changed;
3787 /* There isn't much we can reasonably do to protect against
3788 misbehaving fontification, but here's a fig leaf. */
3789 else if (BUFFER_LIVE_P (obuf))
3790 set_buffer_internal_1 (obuf);
3792 /* The fontification code may have added/removed text.
3793 It could do even a lot worse, but let's at least protect against
3794 the most obvious case where only the text past `pos' gets changed',
3795 as is/was done in grep.el where some escapes sequences are turned
3796 into face properties (bug#7876). */
3797 it->end_charpos = ZV;
3799 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3800 something. This avoids an endless loop if they failed to
3801 fontify the text for which reason ever. */
3802 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3803 handled = HANDLED_RECOMPUTE_PROPS;
3806 return handled;
3811 /***********************************************************************
3812 Faces
3813 ***********************************************************************/
3815 /* Set up iterator IT from face properties at its current position.
3816 Called from handle_stop. */
3818 static enum prop_handled
3819 handle_face_prop (struct it *it)
3821 int new_face_id;
3822 ptrdiff_t next_stop;
3824 if (!STRINGP (it->string))
3826 new_face_id
3827 = face_at_buffer_position (it->w,
3828 IT_CHARPOS (*it),
3829 it->region_beg_charpos,
3830 it->region_end_charpos,
3831 &next_stop,
3832 (IT_CHARPOS (*it)
3833 + TEXT_PROP_DISTANCE_LIMIT),
3834 0, it->base_face_id);
3836 /* Is this a start of a run of characters with box face?
3837 Caveat: this can be called for a freshly initialized
3838 iterator; face_id is -1 in this case. We know that the new
3839 face will not change until limit, i.e. if the new face has a
3840 box, all characters up to limit will have one. But, as
3841 usual, we don't know whether limit is really the end. */
3842 if (new_face_id != it->face_id)
3844 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3845 /* If it->face_id is -1, old_face below will be NULL, see
3846 the definition of FACE_FROM_ID. This will happen if this
3847 is the initial call that gets the face. */
3848 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3850 /* If the value of face_id of the iterator is -1, we have to
3851 look in front of IT's position and see whether there is a
3852 face there that's different from new_face_id. */
3853 if (!old_face && IT_CHARPOS (*it) > BEG)
3855 int prev_face_id = face_before_it_pos (it);
3857 old_face = FACE_FROM_ID (it->f, prev_face_id);
3860 /* If the new face has a box, but the old face does not,
3861 this is the start of a run of characters with box face,
3862 i.e. this character has a shadow on the left side. */
3863 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3864 && (old_face == NULL || !old_face->box));
3865 it->face_box_p = new_face->box != FACE_NO_BOX;
3868 else
3870 int base_face_id;
3871 ptrdiff_t bufpos;
3872 int i;
3873 Lisp_Object from_overlay
3874 = (it->current.overlay_string_index >= 0
3875 ? it->string_overlays[it->current.overlay_string_index
3876 % OVERLAY_STRING_CHUNK_SIZE]
3877 : Qnil);
3879 /* See if we got to this string directly or indirectly from
3880 an overlay property. That includes the before-string or
3881 after-string of an overlay, strings in display properties
3882 provided by an overlay, their text properties, etc.
3884 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3885 if (! NILP (from_overlay))
3886 for (i = it->sp - 1; i >= 0; i--)
3888 if (it->stack[i].current.overlay_string_index >= 0)
3889 from_overlay
3890 = it->string_overlays[it->stack[i].current.overlay_string_index
3891 % OVERLAY_STRING_CHUNK_SIZE];
3892 else if (! NILP (it->stack[i].from_overlay))
3893 from_overlay = it->stack[i].from_overlay;
3895 if (!NILP (from_overlay))
3896 break;
3899 if (! NILP (from_overlay))
3901 bufpos = IT_CHARPOS (*it);
3902 /* For a string from an overlay, the base face depends
3903 only on text properties and ignores overlays. */
3904 base_face_id
3905 = face_for_overlay_string (it->w,
3906 IT_CHARPOS (*it),
3907 it->region_beg_charpos,
3908 it->region_end_charpos,
3909 &next_stop,
3910 (IT_CHARPOS (*it)
3911 + TEXT_PROP_DISTANCE_LIMIT),
3913 from_overlay);
3915 else
3917 bufpos = 0;
3919 /* For strings from a `display' property, use the face at
3920 IT's current buffer position as the base face to merge
3921 with, so that overlay strings appear in the same face as
3922 surrounding text, unless they specify their own faces.
3923 For strings from wrap-prefix and line-prefix properties,
3924 use the default face, possibly remapped via
3925 Vface_remapping_alist. */
3926 base_face_id = it->string_from_prefix_prop_p
3927 ? (!NILP (Vface_remapping_alist)
3928 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3929 : DEFAULT_FACE_ID)
3930 : underlying_face_id (it);
3933 new_face_id = face_at_string_position (it->w,
3934 it->string,
3935 IT_STRING_CHARPOS (*it),
3936 bufpos,
3937 it->region_beg_charpos,
3938 it->region_end_charpos,
3939 &next_stop,
3940 base_face_id, 0);
3942 /* Is this a start of a run of characters with box? Caveat:
3943 this can be called for a freshly allocated iterator; face_id
3944 is -1 is this case. We know that the new face will not
3945 change until the next check pos, i.e. if the new face has a
3946 box, all characters up to that position will have a
3947 box. But, as usual, we don't know whether that position
3948 is really the end. */
3949 if (new_face_id != it->face_id)
3951 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3952 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3954 /* If new face has a box but old face hasn't, this is the
3955 start of a run of characters with box, i.e. it has a
3956 shadow on the left side. */
3957 it->start_of_box_run_p
3958 = new_face->box && (old_face == NULL || !old_face->box);
3959 it->face_box_p = new_face->box != FACE_NO_BOX;
3963 it->face_id = new_face_id;
3964 return HANDLED_NORMALLY;
3968 /* Return the ID of the face ``underlying'' IT's current position,
3969 which is in a string. If the iterator is associated with a
3970 buffer, return the face at IT's current buffer position.
3971 Otherwise, use the iterator's base_face_id. */
3973 static int
3974 underlying_face_id (struct it *it)
3976 int face_id = it->base_face_id, i;
3978 eassert (STRINGP (it->string));
3980 for (i = it->sp - 1; i >= 0; --i)
3981 if (NILP (it->stack[i].string))
3982 face_id = it->stack[i].face_id;
3984 return face_id;
3988 /* Compute the face one character before or after the current position
3989 of IT, in the visual order. BEFORE_P non-zero means get the face
3990 in front (to the left in L2R paragraphs, to the right in R2L
3991 paragraphs) of IT's screen position. Value is the ID of the face. */
3993 static int
3994 face_before_or_after_it_pos (struct it *it, int before_p)
3996 int face_id, limit;
3997 ptrdiff_t next_check_charpos;
3998 struct it it_copy;
3999 void *it_copy_data = NULL;
4001 eassert (it->s == NULL);
4003 if (STRINGP (it->string))
4005 ptrdiff_t bufpos, charpos;
4006 int base_face_id;
4008 /* No face change past the end of the string (for the case
4009 we are padding with spaces). No face change before the
4010 string start. */
4011 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4012 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4013 return it->face_id;
4015 if (!it->bidi_p)
4017 /* Set charpos to the position before or after IT's current
4018 position, in the logical order, which in the non-bidi
4019 case is the same as the visual order. */
4020 if (before_p)
4021 charpos = IT_STRING_CHARPOS (*it) - 1;
4022 else if (it->what == IT_COMPOSITION)
4023 /* For composition, we must check the character after the
4024 composition. */
4025 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4026 else
4027 charpos = IT_STRING_CHARPOS (*it) + 1;
4029 else
4031 if (before_p)
4033 /* With bidi iteration, the character before the current
4034 in the visual order cannot be found by simple
4035 iteration, because "reverse" reordering is not
4036 supported. Instead, we need to use the move_it_*
4037 family of functions. */
4038 /* Ignore face changes before the first visible
4039 character on this display line. */
4040 if (it->current_x <= it->first_visible_x)
4041 return it->face_id;
4042 SAVE_IT (it_copy, *it, it_copy_data);
4043 /* Implementation note: Since move_it_in_display_line
4044 works in the iterator geometry, and thinks the first
4045 character is always the leftmost, even in R2L lines,
4046 we don't need to distinguish between the R2L and L2R
4047 cases here. */
4048 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4049 it_copy.current_x - 1, MOVE_TO_X);
4050 charpos = IT_STRING_CHARPOS (it_copy);
4051 RESTORE_IT (it, it, it_copy_data);
4053 else
4055 /* Set charpos to the string position of the character
4056 that comes after IT's current position in the visual
4057 order. */
4058 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4060 it_copy = *it;
4061 while (n--)
4062 bidi_move_to_visually_next (&it_copy.bidi_it);
4064 charpos = it_copy.bidi_it.charpos;
4067 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4069 if (it->current.overlay_string_index >= 0)
4070 bufpos = IT_CHARPOS (*it);
4071 else
4072 bufpos = 0;
4074 base_face_id = underlying_face_id (it);
4076 /* Get the face for ASCII, or unibyte. */
4077 face_id = face_at_string_position (it->w,
4078 it->string,
4079 charpos,
4080 bufpos,
4081 it->region_beg_charpos,
4082 it->region_end_charpos,
4083 &next_check_charpos,
4084 base_face_id, 0);
4086 /* Correct the face for charsets different from ASCII. Do it
4087 for the multibyte case only. The face returned above is
4088 suitable for unibyte text if IT->string is unibyte. */
4089 if (STRING_MULTIBYTE (it->string))
4091 struct text_pos pos1 = string_pos (charpos, it->string);
4092 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4093 int c, len;
4094 struct face *face = FACE_FROM_ID (it->f, face_id);
4096 c = string_char_and_length (p, &len);
4097 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4100 else
4102 struct text_pos pos;
4104 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4105 || (IT_CHARPOS (*it) <= BEGV && before_p))
4106 return it->face_id;
4108 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4109 pos = it->current.pos;
4111 if (!it->bidi_p)
4113 if (before_p)
4114 DEC_TEXT_POS (pos, it->multibyte_p);
4115 else
4117 if (it->what == IT_COMPOSITION)
4119 /* For composition, we must check the position after
4120 the composition. */
4121 pos.charpos += it->cmp_it.nchars;
4122 pos.bytepos += it->len;
4124 else
4125 INC_TEXT_POS (pos, it->multibyte_p);
4128 else
4130 if (before_p)
4132 /* With bidi iteration, the character before the current
4133 in the visual order cannot be found by simple
4134 iteration, because "reverse" reordering is not
4135 supported. Instead, we need to use the move_it_*
4136 family of functions. */
4137 /* Ignore face changes before the first visible
4138 character on this display line. */
4139 if (it->current_x <= it->first_visible_x)
4140 return it->face_id;
4141 SAVE_IT (it_copy, *it, it_copy_data);
4142 /* Implementation note: Since move_it_in_display_line
4143 works in the iterator geometry, and thinks the first
4144 character is always the leftmost, even in R2L lines,
4145 we don't need to distinguish between the R2L and L2R
4146 cases here. */
4147 move_it_in_display_line (&it_copy, ZV,
4148 it_copy.current_x - 1, MOVE_TO_X);
4149 pos = it_copy.current.pos;
4150 RESTORE_IT (it, it, it_copy_data);
4152 else
4154 /* Set charpos to the buffer position of the character
4155 that comes after IT's current position in the visual
4156 order. */
4157 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4159 it_copy = *it;
4160 while (n--)
4161 bidi_move_to_visually_next (&it_copy.bidi_it);
4163 SET_TEXT_POS (pos,
4164 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4167 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4169 /* Determine face for CHARSET_ASCII, or unibyte. */
4170 face_id = face_at_buffer_position (it->w,
4171 CHARPOS (pos),
4172 it->region_beg_charpos,
4173 it->region_end_charpos,
4174 &next_check_charpos,
4175 limit, 0, -1);
4177 /* Correct the face for charsets different from ASCII. Do it
4178 for the multibyte case only. The face returned above is
4179 suitable for unibyte text if current_buffer is unibyte. */
4180 if (it->multibyte_p)
4182 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4183 struct face *face = FACE_FROM_ID (it->f, face_id);
4184 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4188 return face_id;
4193 /***********************************************************************
4194 Invisible text
4195 ***********************************************************************/
4197 /* Set up iterator IT from invisible properties at its current
4198 position. Called from handle_stop. */
4200 static enum prop_handled
4201 handle_invisible_prop (struct it *it)
4203 enum prop_handled handled = HANDLED_NORMALLY;
4204 int invis_p;
4205 Lisp_Object prop;
4207 if (STRINGP (it->string))
4209 Lisp_Object end_charpos, limit, charpos;
4211 /* Get the value of the invisible text property at the
4212 current position. Value will be nil if there is no such
4213 property. */
4214 charpos = make_number (IT_STRING_CHARPOS (*it));
4215 prop = Fget_text_property (charpos, Qinvisible, it->string);
4216 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4218 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4220 /* Record whether we have to display an ellipsis for the
4221 invisible text. */
4222 int display_ellipsis_p = (invis_p == 2);
4223 ptrdiff_t len, endpos;
4225 handled = HANDLED_RECOMPUTE_PROPS;
4227 /* Get the position at which the next visible text can be
4228 found in IT->string, if any. */
4229 endpos = len = SCHARS (it->string);
4230 XSETINT (limit, len);
4233 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4234 it->string, limit);
4235 if (INTEGERP (end_charpos))
4237 endpos = XFASTINT (end_charpos);
4238 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4239 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4240 if (invis_p == 2)
4241 display_ellipsis_p = 1;
4244 while (invis_p && endpos < len);
4246 if (display_ellipsis_p)
4247 it->ellipsis_p = 1;
4249 if (endpos < len)
4251 /* Text at END_CHARPOS is visible. Move IT there. */
4252 struct text_pos old;
4253 ptrdiff_t oldpos;
4255 old = it->current.string_pos;
4256 oldpos = CHARPOS (old);
4257 if (it->bidi_p)
4259 if (it->bidi_it.first_elt
4260 && it->bidi_it.charpos < SCHARS (it->string))
4261 bidi_paragraph_init (it->paragraph_embedding,
4262 &it->bidi_it, 1);
4263 /* Bidi-iterate out of the invisible text. */
4266 bidi_move_to_visually_next (&it->bidi_it);
4268 while (oldpos <= it->bidi_it.charpos
4269 && it->bidi_it.charpos < endpos);
4271 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4272 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4273 if (IT_CHARPOS (*it) >= endpos)
4274 it->prev_stop = endpos;
4276 else
4278 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4279 compute_string_pos (&it->current.string_pos, old, it->string);
4282 else
4284 /* The rest of the string is invisible. If this is an
4285 overlay string, proceed with the next overlay string
4286 or whatever comes and return a character from there. */
4287 if (it->current.overlay_string_index >= 0
4288 && !display_ellipsis_p)
4290 next_overlay_string (it);
4291 /* Don't check for overlay strings when we just
4292 finished processing them. */
4293 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4295 else
4297 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4298 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4303 else
4305 ptrdiff_t newpos, next_stop, start_charpos, tem;
4306 Lisp_Object pos, overlay;
4308 /* First of all, is there invisible text at this position? */
4309 tem = start_charpos = IT_CHARPOS (*it);
4310 pos = make_number (tem);
4311 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4312 &overlay);
4313 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4315 /* If we are on invisible text, skip over it. */
4316 if (invis_p && start_charpos < it->end_charpos)
4318 /* Record whether we have to display an ellipsis for the
4319 invisible text. */
4320 int display_ellipsis_p = invis_p == 2;
4322 handled = HANDLED_RECOMPUTE_PROPS;
4324 /* Loop skipping over invisible text. The loop is left at
4325 ZV or with IT on the first char being visible again. */
4328 /* Try to skip some invisible text. Return value is the
4329 position reached which can be equal to where we start
4330 if there is nothing invisible there. This skips both
4331 over invisible text properties and overlays with
4332 invisible property. */
4333 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4335 /* If we skipped nothing at all we weren't at invisible
4336 text in the first place. If everything to the end of
4337 the buffer was skipped, end the loop. */
4338 if (newpos == tem || newpos >= ZV)
4339 invis_p = 0;
4340 else
4342 /* We skipped some characters but not necessarily
4343 all there are. Check if we ended up on visible
4344 text. Fget_char_property returns the property of
4345 the char before the given position, i.e. if we
4346 get invis_p = 0, this means that the char at
4347 newpos is visible. */
4348 pos = make_number (newpos);
4349 prop = Fget_char_property (pos, Qinvisible, it->window);
4350 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4353 /* If we ended up on invisible text, proceed to
4354 skip starting with next_stop. */
4355 if (invis_p)
4356 tem = next_stop;
4358 /* If there are adjacent invisible texts, don't lose the
4359 second one's ellipsis. */
4360 if (invis_p == 2)
4361 display_ellipsis_p = 1;
4363 while (invis_p);
4365 /* The position newpos is now either ZV or on visible text. */
4366 if (it->bidi_p)
4368 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4369 int on_newline =
4370 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4371 int after_newline =
4372 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4374 /* If the invisible text ends on a newline or on a
4375 character after a newline, we can avoid the costly,
4376 character by character, bidi iteration to NEWPOS, and
4377 instead simply reseat the iterator there. That's
4378 because all bidi reordering information is tossed at
4379 the newline. This is a big win for modes that hide
4380 complete lines, like Outline, Org, etc. */
4381 if (on_newline || after_newline)
4383 struct text_pos tpos;
4384 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4386 SET_TEXT_POS (tpos, newpos, bpos);
4387 reseat_1 (it, tpos, 0);
4388 /* If we reseat on a newline/ZV, we need to prep the
4389 bidi iterator for advancing to the next character
4390 after the newline/EOB, keeping the current paragraph
4391 direction (so that PRODUCE_GLYPHS does TRT wrt
4392 prepending/appending glyphs to a glyph row). */
4393 if (on_newline)
4395 it->bidi_it.first_elt = 0;
4396 it->bidi_it.paragraph_dir = pdir;
4397 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4398 it->bidi_it.nchars = 1;
4399 it->bidi_it.ch_len = 1;
4402 else /* Must use the slow method. */
4404 /* With bidi iteration, the region of invisible text
4405 could start and/or end in the middle of a
4406 non-base embedding level. Therefore, we need to
4407 skip invisible text using the bidi iterator,
4408 starting at IT's current position, until we find
4409 ourselves outside of the invisible text.
4410 Skipping invisible text _after_ bidi iteration
4411 avoids affecting the visual order of the
4412 displayed text when invisible properties are
4413 added or removed. */
4414 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4416 /* If we were `reseat'ed to a new paragraph,
4417 determine the paragraph base direction. We
4418 need to do it now because
4419 next_element_from_buffer may not have a
4420 chance to do it, if we are going to skip any
4421 text at the beginning, which resets the
4422 FIRST_ELT flag. */
4423 bidi_paragraph_init (it->paragraph_embedding,
4424 &it->bidi_it, 1);
4428 bidi_move_to_visually_next (&it->bidi_it);
4430 while (it->stop_charpos <= it->bidi_it.charpos
4431 && it->bidi_it.charpos < newpos);
4432 IT_CHARPOS (*it) = it->bidi_it.charpos;
4433 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4434 /* If we overstepped NEWPOS, record its position in
4435 the iterator, so that we skip invisible text if
4436 later the bidi iteration lands us in the
4437 invisible region again. */
4438 if (IT_CHARPOS (*it) >= newpos)
4439 it->prev_stop = newpos;
4442 else
4444 IT_CHARPOS (*it) = newpos;
4445 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4448 /* If there are before-strings at the start of invisible
4449 text, and the text is invisible because of a text
4450 property, arrange to show before-strings because 20.x did
4451 it that way. (If the text is invisible because of an
4452 overlay property instead of a text property, this is
4453 already handled in the overlay code.) */
4454 if (NILP (overlay)
4455 && get_overlay_strings (it, it->stop_charpos))
4457 handled = HANDLED_RECOMPUTE_PROPS;
4458 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4460 else if (display_ellipsis_p)
4462 /* Make sure that the glyphs of the ellipsis will get
4463 correct `charpos' values. If we would not update
4464 it->position here, the glyphs would belong to the
4465 last visible character _before_ the invisible
4466 text, which confuses `set_cursor_from_row'.
4468 We use the last invisible position instead of the
4469 first because this way the cursor is always drawn on
4470 the first "." of the ellipsis, whenever PT is inside
4471 the invisible text. Otherwise the cursor would be
4472 placed _after_ the ellipsis when the point is after the
4473 first invisible character. */
4474 if (!STRINGP (it->object))
4476 it->position.charpos = newpos - 1;
4477 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4479 it->ellipsis_p = 1;
4480 /* Let the ellipsis display before
4481 considering any properties of the following char.
4482 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4483 handled = HANDLED_RETURN;
4488 return handled;
4492 /* Make iterator IT return `...' next.
4493 Replaces LEN characters from buffer. */
4495 static void
4496 setup_for_ellipsis (struct it *it, int len)
4498 /* Use the display table definition for `...'. Invalid glyphs
4499 will be handled by the method returning elements from dpvec. */
4500 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4502 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4503 it->dpvec = v->contents;
4504 it->dpend = v->contents + v->header.size;
4506 else
4508 /* Default `...'. */
4509 it->dpvec = default_invis_vector;
4510 it->dpend = default_invis_vector + 3;
4513 it->dpvec_char_len = len;
4514 it->current.dpvec_index = 0;
4515 it->dpvec_face_id = -1;
4517 /* Remember the current face id in case glyphs specify faces.
4518 IT's face is restored in set_iterator_to_next.
4519 saved_face_id was set to preceding char's face in handle_stop. */
4520 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4521 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4523 it->method = GET_FROM_DISPLAY_VECTOR;
4524 it->ellipsis_p = 1;
4529 /***********************************************************************
4530 'display' property
4531 ***********************************************************************/
4533 /* Set up iterator IT from `display' property at its current position.
4534 Called from handle_stop.
4535 We return HANDLED_RETURN if some part of the display property
4536 overrides the display of the buffer text itself.
4537 Otherwise we return HANDLED_NORMALLY. */
4539 static enum prop_handled
4540 handle_display_prop (struct it *it)
4542 Lisp_Object propval, object, overlay;
4543 struct text_pos *position;
4544 ptrdiff_t bufpos;
4545 /* Nonzero if some property replaces the display of the text itself. */
4546 int display_replaced_p = 0;
4548 if (STRINGP (it->string))
4550 object = it->string;
4551 position = &it->current.string_pos;
4552 bufpos = CHARPOS (it->current.pos);
4554 else
4556 XSETWINDOW (object, it->w);
4557 position = &it->current.pos;
4558 bufpos = CHARPOS (*position);
4561 /* Reset those iterator values set from display property values. */
4562 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4563 it->space_width = Qnil;
4564 it->font_height = Qnil;
4565 it->voffset = 0;
4567 /* We don't support recursive `display' properties, i.e. string
4568 values that have a string `display' property, that have a string
4569 `display' property etc. */
4570 if (!it->string_from_display_prop_p)
4571 it->area = TEXT_AREA;
4573 propval = get_char_property_and_overlay (make_number (position->charpos),
4574 Qdisplay, object, &overlay);
4575 if (NILP (propval))
4576 return HANDLED_NORMALLY;
4577 /* Now OVERLAY is the overlay that gave us this property, or nil
4578 if it was a text property. */
4580 if (!STRINGP (it->string))
4581 object = it->w->contents;
4583 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4584 position, bufpos,
4585 FRAME_WINDOW_P (it->f));
4587 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4590 /* Subroutine of handle_display_prop. Returns non-zero if the display
4591 specification in SPEC is a replacing specification, i.e. it would
4592 replace the text covered by `display' property with something else,
4593 such as an image or a display string. If SPEC includes any kind or
4594 `(space ...) specification, the value is 2; this is used by
4595 compute_display_string_pos, which see.
4597 See handle_single_display_spec for documentation of arguments.
4598 frame_window_p is non-zero if the window being redisplayed is on a
4599 GUI frame; this argument is used only if IT is NULL, see below.
4601 IT can be NULL, if this is called by the bidi reordering code
4602 through compute_display_string_pos, which see. In that case, this
4603 function only examines SPEC, but does not otherwise "handle" it, in
4604 the sense that it doesn't set up members of IT from the display
4605 spec. */
4606 static int
4607 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4608 Lisp_Object overlay, struct text_pos *position,
4609 ptrdiff_t bufpos, int frame_window_p)
4611 int replacing_p = 0;
4612 int rv;
4614 if (CONSP (spec)
4615 /* Simple specifications. */
4616 && !EQ (XCAR (spec), Qimage)
4617 && !EQ (XCAR (spec), Qspace)
4618 && !EQ (XCAR (spec), Qwhen)
4619 && !EQ (XCAR (spec), Qslice)
4620 && !EQ (XCAR (spec), Qspace_width)
4621 && !EQ (XCAR (spec), Qheight)
4622 && !EQ (XCAR (spec), Qraise)
4623 /* Marginal area specifications. */
4624 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4625 && !EQ (XCAR (spec), Qleft_fringe)
4626 && !EQ (XCAR (spec), Qright_fringe)
4627 && !NILP (XCAR (spec)))
4629 for (; CONSP (spec); spec = XCDR (spec))
4631 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4632 overlay, position, bufpos,
4633 replacing_p, frame_window_p)))
4635 replacing_p = rv;
4636 /* If some text in a string is replaced, `position' no
4637 longer points to the position of `object'. */
4638 if (!it || STRINGP (object))
4639 break;
4643 else if (VECTORP (spec))
4645 ptrdiff_t i;
4646 for (i = 0; i < ASIZE (spec); ++i)
4647 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4648 overlay, position, bufpos,
4649 replacing_p, frame_window_p)))
4651 replacing_p = rv;
4652 /* If some text in a string is replaced, `position' no
4653 longer points to the position of `object'. */
4654 if (!it || STRINGP (object))
4655 break;
4658 else
4660 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4661 position, bufpos, 0,
4662 frame_window_p)))
4663 replacing_p = rv;
4666 return replacing_p;
4669 /* Value is the position of the end of the `display' property starting
4670 at START_POS in OBJECT. */
4672 static struct text_pos
4673 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4675 Lisp_Object end;
4676 struct text_pos end_pos;
4678 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4679 Qdisplay, object, Qnil);
4680 CHARPOS (end_pos) = XFASTINT (end);
4681 if (STRINGP (object))
4682 compute_string_pos (&end_pos, start_pos, it->string);
4683 else
4684 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4686 return end_pos;
4690 /* Set up IT from a single `display' property specification SPEC. OBJECT
4691 is the object in which the `display' property was found. *POSITION
4692 is the position in OBJECT at which the `display' property was found.
4693 BUFPOS is the buffer position of OBJECT (different from POSITION if
4694 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4695 previously saw a display specification which already replaced text
4696 display with something else, for example an image; we ignore such
4697 properties after the first one has been processed.
4699 OVERLAY is the overlay this `display' property came from,
4700 or nil if it was a text property.
4702 If SPEC is a `space' or `image' specification, and in some other
4703 cases too, set *POSITION to the position where the `display'
4704 property ends.
4706 If IT is NULL, only examine the property specification in SPEC, but
4707 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4708 is intended to be displayed in a window on a GUI frame.
4710 Value is non-zero if something was found which replaces the display
4711 of buffer or string text. */
4713 static int
4714 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4715 Lisp_Object overlay, struct text_pos *position,
4716 ptrdiff_t bufpos, int display_replaced_p,
4717 int frame_window_p)
4719 Lisp_Object form;
4720 Lisp_Object location, value;
4721 struct text_pos start_pos = *position;
4722 int valid_p;
4724 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4725 If the result is non-nil, use VALUE instead of SPEC. */
4726 form = Qt;
4727 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4729 spec = XCDR (spec);
4730 if (!CONSP (spec))
4731 return 0;
4732 form = XCAR (spec);
4733 spec = XCDR (spec);
4736 if (!NILP (form) && !EQ (form, Qt))
4738 ptrdiff_t count = SPECPDL_INDEX ();
4739 struct gcpro gcpro1;
4741 /* Bind `object' to the object having the `display' property, a
4742 buffer or string. Bind `position' to the position in the
4743 object where the property was found, and `buffer-position'
4744 to the current position in the buffer. */
4746 if (NILP (object))
4747 XSETBUFFER (object, current_buffer);
4748 specbind (Qobject, object);
4749 specbind (Qposition, make_number (CHARPOS (*position)));
4750 specbind (Qbuffer_position, make_number (bufpos));
4751 GCPRO1 (form);
4752 form = safe_eval (form);
4753 UNGCPRO;
4754 unbind_to (count, Qnil);
4757 if (NILP (form))
4758 return 0;
4760 /* Handle `(height HEIGHT)' specifications. */
4761 if (CONSP (spec)
4762 && EQ (XCAR (spec), Qheight)
4763 && CONSP (XCDR (spec)))
4765 if (it)
4767 if (!FRAME_WINDOW_P (it->f))
4768 return 0;
4770 it->font_height = XCAR (XCDR (spec));
4771 if (!NILP (it->font_height))
4773 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4774 int new_height = -1;
4776 if (CONSP (it->font_height)
4777 && (EQ (XCAR (it->font_height), Qplus)
4778 || EQ (XCAR (it->font_height), Qminus))
4779 && CONSP (XCDR (it->font_height))
4780 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4782 /* `(+ N)' or `(- N)' where N is an integer. */
4783 int steps = XINT (XCAR (XCDR (it->font_height)));
4784 if (EQ (XCAR (it->font_height), Qplus))
4785 steps = - steps;
4786 it->face_id = smaller_face (it->f, it->face_id, steps);
4788 else if (FUNCTIONP (it->font_height))
4790 /* Call function with current height as argument.
4791 Value is the new height. */
4792 Lisp_Object height;
4793 height = safe_call1 (it->font_height,
4794 face->lface[LFACE_HEIGHT_INDEX]);
4795 if (NUMBERP (height))
4796 new_height = XFLOATINT (height);
4798 else if (NUMBERP (it->font_height))
4800 /* Value is a multiple of the canonical char height. */
4801 struct face *f;
4803 f = FACE_FROM_ID (it->f,
4804 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4805 new_height = (XFLOATINT (it->font_height)
4806 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4808 else
4810 /* Evaluate IT->font_height with `height' bound to the
4811 current specified height to get the new height. */
4812 ptrdiff_t count = SPECPDL_INDEX ();
4814 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4815 value = safe_eval (it->font_height);
4816 unbind_to (count, Qnil);
4818 if (NUMBERP (value))
4819 new_height = XFLOATINT (value);
4822 if (new_height > 0)
4823 it->face_id = face_with_height (it->f, it->face_id, new_height);
4827 return 0;
4830 /* Handle `(space-width WIDTH)'. */
4831 if (CONSP (spec)
4832 && EQ (XCAR (spec), Qspace_width)
4833 && CONSP (XCDR (spec)))
4835 if (it)
4837 if (!FRAME_WINDOW_P (it->f))
4838 return 0;
4840 value = XCAR (XCDR (spec));
4841 if (NUMBERP (value) && XFLOATINT (value) > 0)
4842 it->space_width = value;
4845 return 0;
4848 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4849 if (CONSP (spec)
4850 && EQ (XCAR (spec), Qslice))
4852 Lisp_Object tem;
4854 if (it)
4856 if (!FRAME_WINDOW_P (it->f))
4857 return 0;
4859 if (tem = XCDR (spec), CONSP (tem))
4861 it->slice.x = XCAR (tem);
4862 if (tem = XCDR (tem), CONSP (tem))
4864 it->slice.y = XCAR (tem);
4865 if (tem = XCDR (tem), CONSP (tem))
4867 it->slice.width = XCAR (tem);
4868 if (tem = XCDR (tem), CONSP (tem))
4869 it->slice.height = XCAR (tem);
4875 return 0;
4878 /* Handle `(raise FACTOR)'. */
4879 if (CONSP (spec)
4880 && EQ (XCAR (spec), Qraise)
4881 && CONSP (XCDR (spec)))
4883 if (it)
4885 if (!FRAME_WINDOW_P (it->f))
4886 return 0;
4888 #ifdef HAVE_WINDOW_SYSTEM
4889 value = XCAR (XCDR (spec));
4890 if (NUMBERP (value))
4892 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4893 it->voffset = - (XFLOATINT (value)
4894 * (FONT_HEIGHT (face->font)));
4896 #endif /* HAVE_WINDOW_SYSTEM */
4899 return 0;
4902 /* Don't handle the other kinds of display specifications
4903 inside a string that we got from a `display' property. */
4904 if (it && it->string_from_display_prop_p)
4905 return 0;
4907 /* Characters having this form of property are not displayed, so
4908 we have to find the end of the property. */
4909 if (it)
4911 start_pos = *position;
4912 *position = display_prop_end (it, object, start_pos);
4914 value = Qnil;
4916 /* Stop the scan at that end position--we assume that all
4917 text properties change there. */
4918 if (it)
4919 it->stop_charpos = position->charpos;
4921 /* Handle `(left-fringe BITMAP [FACE])'
4922 and `(right-fringe BITMAP [FACE])'. */
4923 if (CONSP (spec)
4924 && (EQ (XCAR (spec), Qleft_fringe)
4925 || EQ (XCAR (spec), Qright_fringe))
4926 && CONSP (XCDR (spec)))
4928 int fringe_bitmap;
4930 if (it)
4932 if (!FRAME_WINDOW_P (it->f))
4933 /* If we return here, POSITION has been advanced
4934 across the text with this property. */
4936 /* Synchronize the bidi iterator with POSITION. This is
4937 needed because we are not going to push the iterator
4938 on behalf of this display property, so there will be
4939 no pop_it call to do this synchronization for us. */
4940 if (it->bidi_p)
4942 it->position = *position;
4943 iterate_out_of_display_property (it);
4944 *position = it->position;
4946 return 1;
4949 else if (!frame_window_p)
4950 return 1;
4952 #ifdef HAVE_WINDOW_SYSTEM
4953 value = XCAR (XCDR (spec));
4954 if (!SYMBOLP (value)
4955 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4956 /* If we return here, POSITION has been advanced
4957 across the text with this property. */
4959 if (it && it->bidi_p)
4961 it->position = *position;
4962 iterate_out_of_display_property (it);
4963 *position = it->position;
4965 return 1;
4968 if (it)
4970 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4972 if (CONSP (XCDR (XCDR (spec))))
4974 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4975 int face_id2 = lookup_derived_face (it->f, face_name,
4976 FRINGE_FACE_ID, 0);
4977 if (face_id2 >= 0)
4978 face_id = face_id2;
4981 /* Save current settings of IT so that we can restore them
4982 when we are finished with the glyph property value. */
4983 push_it (it, position);
4985 it->area = TEXT_AREA;
4986 it->what = IT_IMAGE;
4987 it->image_id = -1; /* no image */
4988 it->position = start_pos;
4989 it->object = NILP (object) ? it->w->contents : object;
4990 it->method = GET_FROM_IMAGE;
4991 it->from_overlay = Qnil;
4992 it->face_id = face_id;
4993 it->from_disp_prop_p = 1;
4995 /* Say that we haven't consumed the characters with
4996 `display' property yet. The call to pop_it in
4997 set_iterator_to_next will clean this up. */
4998 *position = start_pos;
5000 if (EQ (XCAR (spec), Qleft_fringe))
5002 it->left_user_fringe_bitmap = fringe_bitmap;
5003 it->left_user_fringe_face_id = face_id;
5005 else
5007 it->right_user_fringe_bitmap = fringe_bitmap;
5008 it->right_user_fringe_face_id = face_id;
5011 #endif /* HAVE_WINDOW_SYSTEM */
5012 return 1;
5015 /* Prepare to handle `((margin left-margin) ...)',
5016 `((margin right-margin) ...)' and `((margin nil) ...)'
5017 prefixes for display specifications. */
5018 location = Qunbound;
5019 if (CONSP (spec) && CONSP (XCAR (spec)))
5021 Lisp_Object tem;
5023 value = XCDR (spec);
5024 if (CONSP (value))
5025 value = XCAR (value);
5027 tem = XCAR (spec);
5028 if (EQ (XCAR (tem), Qmargin)
5029 && (tem = XCDR (tem),
5030 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5031 (NILP (tem)
5032 || EQ (tem, Qleft_margin)
5033 || EQ (tem, Qright_margin))))
5034 location = tem;
5037 if (EQ (location, Qunbound))
5039 location = Qnil;
5040 value = spec;
5043 /* After this point, VALUE is the property after any
5044 margin prefix has been stripped. It must be a string,
5045 an image specification, or `(space ...)'.
5047 LOCATION specifies where to display: `left-margin',
5048 `right-margin' or nil. */
5050 valid_p = (STRINGP (value)
5051 #ifdef HAVE_WINDOW_SYSTEM
5052 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5053 && valid_image_p (value))
5054 #endif /* not HAVE_WINDOW_SYSTEM */
5055 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5057 if (valid_p && !display_replaced_p)
5059 int retval = 1;
5061 if (!it)
5063 /* Callers need to know whether the display spec is any kind
5064 of `(space ...)' spec that is about to affect text-area
5065 display. */
5066 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5067 retval = 2;
5068 return retval;
5071 /* Save current settings of IT so that we can restore them
5072 when we are finished with the glyph property value. */
5073 push_it (it, position);
5074 it->from_overlay = overlay;
5075 it->from_disp_prop_p = 1;
5077 if (NILP (location))
5078 it->area = TEXT_AREA;
5079 else if (EQ (location, Qleft_margin))
5080 it->area = LEFT_MARGIN_AREA;
5081 else
5082 it->area = RIGHT_MARGIN_AREA;
5084 if (STRINGP (value))
5086 it->string = value;
5087 it->multibyte_p = STRING_MULTIBYTE (it->string);
5088 it->current.overlay_string_index = -1;
5089 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5090 it->end_charpos = it->string_nchars = SCHARS (it->string);
5091 it->method = GET_FROM_STRING;
5092 it->stop_charpos = 0;
5093 it->prev_stop = 0;
5094 it->base_level_stop = 0;
5095 it->string_from_display_prop_p = 1;
5096 /* Say that we haven't consumed the characters with
5097 `display' property yet. The call to pop_it in
5098 set_iterator_to_next will clean this up. */
5099 if (BUFFERP (object))
5100 *position = start_pos;
5102 /* Force paragraph direction to be that of the parent
5103 object. If the parent object's paragraph direction is
5104 not yet determined, default to L2R. */
5105 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5106 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5107 else
5108 it->paragraph_embedding = L2R;
5110 /* Set up the bidi iterator for this display string. */
5111 if (it->bidi_p)
5113 it->bidi_it.string.lstring = it->string;
5114 it->bidi_it.string.s = NULL;
5115 it->bidi_it.string.schars = it->end_charpos;
5116 it->bidi_it.string.bufpos = bufpos;
5117 it->bidi_it.string.from_disp_str = 1;
5118 it->bidi_it.string.unibyte = !it->multibyte_p;
5119 it->bidi_it.w = it->w;
5120 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5123 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5125 it->method = GET_FROM_STRETCH;
5126 it->object = value;
5127 *position = it->position = start_pos;
5128 retval = 1 + (it->area == TEXT_AREA);
5130 #ifdef HAVE_WINDOW_SYSTEM
5131 else
5133 it->what = IT_IMAGE;
5134 it->image_id = lookup_image (it->f, value);
5135 it->position = start_pos;
5136 it->object = NILP (object) ? it->w->contents : object;
5137 it->method = GET_FROM_IMAGE;
5139 /* Say that we haven't consumed the characters with
5140 `display' property yet. The call to pop_it in
5141 set_iterator_to_next will clean this up. */
5142 *position = start_pos;
5144 #endif /* HAVE_WINDOW_SYSTEM */
5146 return retval;
5149 /* Invalid property or property not supported. Restore
5150 POSITION to what it was before. */
5151 *position = start_pos;
5152 return 0;
5155 /* Check if PROP is a display property value whose text should be
5156 treated as intangible. OVERLAY is the overlay from which PROP
5157 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5158 specify the buffer position covered by PROP. */
5161 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5162 ptrdiff_t charpos, ptrdiff_t bytepos)
5164 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5165 struct text_pos position;
5167 SET_TEXT_POS (position, charpos, bytepos);
5168 return handle_display_spec (NULL, prop, Qnil, overlay,
5169 &position, charpos, frame_window_p);
5173 /* Return 1 if PROP is a display sub-property value containing STRING.
5175 Implementation note: this and the following function are really
5176 special cases of handle_display_spec and
5177 handle_single_display_spec, and should ideally use the same code.
5178 Until they do, these two pairs must be consistent and must be
5179 modified in sync. */
5181 static int
5182 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5184 if (EQ (string, prop))
5185 return 1;
5187 /* Skip over `when FORM'. */
5188 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5190 prop = XCDR (prop);
5191 if (!CONSP (prop))
5192 return 0;
5193 /* Actually, the condition following `when' should be eval'ed,
5194 like handle_single_display_spec does, and we should return
5195 zero if it evaluates to nil. However, this function is
5196 called only when the buffer was already displayed and some
5197 glyph in the glyph matrix was found to come from a display
5198 string. Therefore, the condition was already evaluated, and
5199 the result was non-nil, otherwise the display string wouldn't
5200 have been displayed and we would have never been called for
5201 this property. Thus, we can skip the evaluation and assume
5202 its result is non-nil. */
5203 prop = XCDR (prop);
5206 if (CONSP (prop))
5207 /* Skip over `margin LOCATION'. */
5208 if (EQ (XCAR (prop), Qmargin))
5210 prop = XCDR (prop);
5211 if (!CONSP (prop))
5212 return 0;
5214 prop = XCDR (prop);
5215 if (!CONSP (prop))
5216 return 0;
5219 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5223 /* Return 1 if STRING appears in the `display' property PROP. */
5225 static int
5226 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5228 if (CONSP (prop)
5229 && !EQ (XCAR (prop), Qwhen)
5230 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5232 /* A list of sub-properties. */
5233 while (CONSP (prop))
5235 if (single_display_spec_string_p (XCAR (prop), string))
5236 return 1;
5237 prop = XCDR (prop);
5240 else if (VECTORP (prop))
5242 /* A vector of sub-properties. */
5243 ptrdiff_t i;
5244 for (i = 0; i < ASIZE (prop); ++i)
5245 if (single_display_spec_string_p (AREF (prop, i), string))
5246 return 1;
5248 else
5249 return single_display_spec_string_p (prop, string);
5251 return 0;
5254 /* Look for STRING in overlays and text properties in the current
5255 buffer, between character positions FROM and TO (excluding TO).
5256 BACK_P non-zero means look back (in this case, TO is supposed to be
5257 less than FROM).
5258 Value is the first character position where STRING was found, or
5259 zero if it wasn't found before hitting TO.
5261 This function may only use code that doesn't eval because it is
5262 called asynchronously from note_mouse_highlight. */
5264 static ptrdiff_t
5265 string_buffer_position_lim (Lisp_Object string,
5266 ptrdiff_t from, ptrdiff_t to, int back_p)
5268 Lisp_Object limit, prop, pos;
5269 int found = 0;
5271 pos = make_number (max (from, BEGV));
5273 if (!back_p) /* looking forward */
5275 limit = make_number (min (to, ZV));
5276 while (!found && !EQ (pos, limit))
5278 prop = Fget_char_property (pos, Qdisplay, Qnil);
5279 if (!NILP (prop) && display_prop_string_p (prop, string))
5280 found = 1;
5281 else
5282 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5283 limit);
5286 else /* looking back */
5288 limit = make_number (max (to, BEGV));
5289 while (!found && !EQ (pos, limit))
5291 prop = Fget_char_property (pos, Qdisplay, Qnil);
5292 if (!NILP (prop) && display_prop_string_p (prop, string))
5293 found = 1;
5294 else
5295 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5296 limit);
5300 return found ? XINT (pos) : 0;
5303 /* Determine which buffer position in current buffer STRING comes from.
5304 AROUND_CHARPOS is an approximate position where it could come from.
5305 Value is the buffer position or 0 if it couldn't be determined.
5307 This function is necessary because we don't record buffer positions
5308 in glyphs generated from strings (to keep struct glyph small).
5309 This function may only use code that doesn't eval because it is
5310 called asynchronously from note_mouse_highlight. */
5312 static ptrdiff_t
5313 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5315 const int MAX_DISTANCE = 1000;
5316 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5317 around_charpos + MAX_DISTANCE,
5320 if (!found)
5321 found = string_buffer_position_lim (string, around_charpos,
5322 around_charpos - MAX_DISTANCE, 1);
5323 return found;
5328 /***********************************************************************
5329 `composition' property
5330 ***********************************************************************/
5332 /* Set up iterator IT from `composition' property at its current
5333 position. Called from handle_stop. */
5335 static enum prop_handled
5336 handle_composition_prop (struct it *it)
5338 Lisp_Object prop, string;
5339 ptrdiff_t pos, pos_byte, start, end;
5341 if (STRINGP (it->string))
5343 unsigned char *s;
5345 pos = IT_STRING_CHARPOS (*it);
5346 pos_byte = IT_STRING_BYTEPOS (*it);
5347 string = it->string;
5348 s = SDATA (string) + pos_byte;
5349 it->c = STRING_CHAR (s);
5351 else
5353 pos = IT_CHARPOS (*it);
5354 pos_byte = IT_BYTEPOS (*it);
5355 string = Qnil;
5356 it->c = FETCH_CHAR (pos_byte);
5359 /* If there's a valid composition and point is not inside of the
5360 composition (in the case that the composition is from the current
5361 buffer), draw a glyph composed from the composition components. */
5362 if (find_composition (pos, -1, &start, &end, &prop, string)
5363 && composition_valid_p (start, end, prop)
5364 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5366 if (start < pos)
5367 /* As we can't handle this situation (perhaps font-lock added
5368 a new composition), we just return here hoping that next
5369 redisplay will detect this composition much earlier. */
5370 return HANDLED_NORMALLY;
5371 if (start != pos)
5373 if (STRINGP (it->string))
5374 pos_byte = string_char_to_byte (it->string, start);
5375 else
5376 pos_byte = CHAR_TO_BYTE (start);
5378 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5379 prop, string);
5381 if (it->cmp_it.id >= 0)
5383 it->cmp_it.ch = -1;
5384 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5385 it->cmp_it.nglyphs = -1;
5389 return HANDLED_NORMALLY;
5394 /***********************************************************************
5395 Overlay strings
5396 ***********************************************************************/
5398 /* The following structure is used to record overlay strings for
5399 later sorting in load_overlay_strings. */
5401 struct overlay_entry
5403 Lisp_Object overlay;
5404 Lisp_Object string;
5405 EMACS_INT priority;
5406 int after_string_p;
5410 /* Set up iterator IT from overlay strings at its current position.
5411 Called from handle_stop. */
5413 static enum prop_handled
5414 handle_overlay_change (struct it *it)
5416 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5417 return HANDLED_RECOMPUTE_PROPS;
5418 else
5419 return HANDLED_NORMALLY;
5423 /* Set up the next overlay string for delivery by IT, if there is an
5424 overlay string to deliver. Called by set_iterator_to_next when the
5425 end of the current overlay string is reached. If there are more
5426 overlay strings to display, IT->string and
5427 IT->current.overlay_string_index are set appropriately here.
5428 Otherwise IT->string is set to nil. */
5430 static void
5431 next_overlay_string (struct it *it)
5433 ++it->current.overlay_string_index;
5434 if (it->current.overlay_string_index == it->n_overlay_strings)
5436 /* No more overlay strings. Restore IT's settings to what
5437 they were before overlay strings were processed, and
5438 continue to deliver from current_buffer. */
5440 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5441 pop_it (it);
5442 eassert (it->sp > 0
5443 || (NILP (it->string)
5444 && it->method == GET_FROM_BUFFER
5445 && it->stop_charpos >= BEGV
5446 && it->stop_charpos <= it->end_charpos));
5447 it->current.overlay_string_index = -1;
5448 it->n_overlay_strings = 0;
5449 it->overlay_strings_charpos = -1;
5450 /* If there's an empty display string on the stack, pop the
5451 stack, to resync the bidi iterator with IT's position. Such
5452 empty strings are pushed onto the stack in
5453 get_overlay_strings_1. */
5454 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5455 pop_it (it);
5457 /* If we're at the end of the buffer, record that we have
5458 processed the overlay strings there already, so that
5459 next_element_from_buffer doesn't try it again. */
5460 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5461 it->overlay_strings_at_end_processed_p = 1;
5463 else
5465 /* There are more overlay strings to process. If
5466 IT->current.overlay_string_index has advanced to a position
5467 where we must load IT->overlay_strings with more strings, do
5468 it. We must load at the IT->overlay_strings_charpos where
5469 IT->n_overlay_strings was originally computed; when invisible
5470 text is present, this might not be IT_CHARPOS (Bug#7016). */
5471 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5473 if (it->current.overlay_string_index && i == 0)
5474 load_overlay_strings (it, it->overlay_strings_charpos);
5476 /* Initialize IT to deliver display elements from the overlay
5477 string. */
5478 it->string = it->overlay_strings[i];
5479 it->multibyte_p = STRING_MULTIBYTE (it->string);
5480 SET_TEXT_POS (it->current.string_pos, 0, 0);
5481 it->method = GET_FROM_STRING;
5482 it->stop_charpos = 0;
5483 it->end_charpos = SCHARS (it->string);
5484 if (it->cmp_it.stop_pos >= 0)
5485 it->cmp_it.stop_pos = 0;
5486 it->prev_stop = 0;
5487 it->base_level_stop = 0;
5489 /* Set up the bidi iterator for this overlay string. */
5490 if (it->bidi_p)
5492 it->bidi_it.string.lstring = it->string;
5493 it->bidi_it.string.s = NULL;
5494 it->bidi_it.string.schars = SCHARS (it->string);
5495 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5496 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5497 it->bidi_it.string.unibyte = !it->multibyte_p;
5498 it->bidi_it.w = it->w;
5499 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5503 CHECK_IT (it);
5507 /* Compare two overlay_entry structures E1 and E2. Used as a
5508 comparison function for qsort in load_overlay_strings. Overlay
5509 strings for the same position are sorted so that
5511 1. All after-strings come in front of before-strings, except
5512 when they come from the same overlay.
5514 2. Within after-strings, strings are sorted so that overlay strings
5515 from overlays with higher priorities come first.
5517 2. Within before-strings, strings are sorted so that overlay
5518 strings from overlays with higher priorities come last.
5520 Value is analogous to strcmp. */
5523 static int
5524 compare_overlay_entries (const void *e1, const void *e2)
5526 struct overlay_entry const *entry1 = e1;
5527 struct overlay_entry const *entry2 = e2;
5528 int result;
5530 if (entry1->after_string_p != entry2->after_string_p)
5532 /* Let after-strings appear in front of before-strings if
5533 they come from different overlays. */
5534 if (EQ (entry1->overlay, entry2->overlay))
5535 result = entry1->after_string_p ? 1 : -1;
5536 else
5537 result = entry1->after_string_p ? -1 : 1;
5539 else if (entry1->priority != entry2->priority)
5541 if (entry1->after_string_p)
5542 /* After-strings sorted in order of decreasing priority. */
5543 result = entry2->priority < entry1->priority ? -1 : 1;
5544 else
5545 /* Before-strings sorted in order of increasing priority. */
5546 result = entry1->priority < entry2->priority ? -1 : 1;
5548 else
5549 result = 0;
5551 return result;
5555 /* Load the vector IT->overlay_strings with overlay strings from IT's
5556 current buffer position, or from CHARPOS if that is > 0. Set
5557 IT->n_overlays to the total number of overlay strings found.
5559 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5560 a time. On entry into load_overlay_strings,
5561 IT->current.overlay_string_index gives the number of overlay
5562 strings that have already been loaded by previous calls to this
5563 function.
5565 IT->add_overlay_start contains an additional overlay start
5566 position to consider for taking overlay strings from, if non-zero.
5567 This position comes into play when the overlay has an `invisible'
5568 property, and both before and after-strings. When we've skipped to
5569 the end of the overlay, because of its `invisible' property, we
5570 nevertheless want its before-string to appear.
5571 IT->add_overlay_start will contain the overlay start position
5572 in this case.
5574 Overlay strings are sorted so that after-string strings come in
5575 front of before-string strings. Within before and after-strings,
5576 strings are sorted by overlay priority. See also function
5577 compare_overlay_entries. */
5579 static void
5580 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5582 Lisp_Object overlay, window, str, invisible;
5583 struct Lisp_Overlay *ov;
5584 ptrdiff_t start, end;
5585 ptrdiff_t size = 20;
5586 ptrdiff_t n = 0, i, j;
5587 int invis_p;
5588 struct overlay_entry *entries = alloca (size * sizeof *entries);
5589 USE_SAFE_ALLOCA;
5591 if (charpos <= 0)
5592 charpos = IT_CHARPOS (*it);
5594 /* Append the overlay string STRING of overlay OVERLAY to vector
5595 `entries' which has size `size' and currently contains `n'
5596 elements. AFTER_P non-zero means STRING is an after-string of
5597 OVERLAY. */
5598 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5599 do \
5601 Lisp_Object priority; \
5603 if (n == size) \
5605 struct overlay_entry *old = entries; \
5606 SAFE_NALLOCA (entries, 2, size); \
5607 memcpy (entries, old, size * sizeof *entries); \
5608 size *= 2; \
5611 entries[n].string = (STRING); \
5612 entries[n].overlay = (OVERLAY); \
5613 priority = Foverlay_get ((OVERLAY), Qpriority); \
5614 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5615 entries[n].after_string_p = (AFTER_P); \
5616 ++n; \
5618 while (0)
5620 /* Process overlay before the overlay center. */
5621 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5623 XSETMISC (overlay, ov);
5624 eassert (OVERLAYP (overlay));
5625 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5626 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5628 if (end < charpos)
5629 break;
5631 /* Skip this overlay if it doesn't start or end at IT's current
5632 position. */
5633 if (end != charpos && start != charpos)
5634 continue;
5636 /* Skip this overlay if it doesn't apply to IT->w. */
5637 window = Foverlay_get (overlay, Qwindow);
5638 if (WINDOWP (window) && XWINDOW (window) != it->w)
5639 continue;
5641 /* If the text ``under'' the overlay is invisible, both before-
5642 and after-strings from this overlay are visible; start and
5643 end position are indistinguishable. */
5644 invisible = Foverlay_get (overlay, Qinvisible);
5645 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5647 /* If overlay has a non-empty before-string, record it. */
5648 if ((start == charpos || (end == charpos && invis_p))
5649 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5650 && SCHARS (str))
5651 RECORD_OVERLAY_STRING (overlay, str, 0);
5653 /* If overlay has a non-empty after-string, record it. */
5654 if ((end == charpos || (start == charpos && invis_p))
5655 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5656 && SCHARS (str))
5657 RECORD_OVERLAY_STRING (overlay, str, 1);
5660 /* Process overlays after the overlay center. */
5661 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5663 XSETMISC (overlay, ov);
5664 eassert (OVERLAYP (overlay));
5665 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5666 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5668 if (start > charpos)
5669 break;
5671 /* Skip this overlay if it doesn't start or end at IT's current
5672 position. */
5673 if (end != charpos && start != charpos)
5674 continue;
5676 /* Skip this overlay if it doesn't apply to IT->w. */
5677 window = Foverlay_get (overlay, Qwindow);
5678 if (WINDOWP (window) && XWINDOW (window) != it->w)
5679 continue;
5681 /* If the text ``under'' the overlay is invisible, it has a zero
5682 dimension, and both before- and after-strings apply. */
5683 invisible = Foverlay_get (overlay, Qinvisible);
5684 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5686 /* If overlay has a non-empty before-string, record it. */
5687 if ((start == charpos || (end == charpos && invis_p))
5688 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5689 && SCHARS (str))
5690 RECORD_OVERLAY_STRING (overlay, str, 0);
5692 /* If overlay has a non-empty after-string, record it. */
5693 if ((end == charpos || (start == charpos && invis_p))
5694 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5695 && SCHARS (str))
5696 RECORD_OVERLAY_STRING (overlay, str, 1);
5699 #undef RECORD_OVERLAY_STRING
5701 /* Sort entries. */
5702 if (n > 1)
5703 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5705 /* Record number of overlay strings, and where we computed it. */
5706 it->n_overlay_strings = n;
5707 it->overlay_strings_charpos = charpos;
5709 /* IT->current.overlay_string_index is the number of overlay strings
5710 that have already been consumed by IT. Copy some of the
5711 remaining overlay strings to IT->overlay_strings. */
5712 i = 0;
5713 j = it->current.overlay_string_index;
5714 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5716 it->overlay_strings[i] = entries[j].string;
5717 it->string_overlays[i++] = entries[j++].overlay;
5720 CHECK_IT (it);
5721 SAFE_FREE ();
5725 /* Get the first chunk of overlay strings at IT's current buffer
5726 position, or at CHARPOS if that is > 0. Value is non-zero if at
5727 least one overlay string was found. */
5729 static int
5730 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5732 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5733 process. This fills IT->overlay_strings with strings, and sets
5734 IT->n_overlay_strings to the total number of strings to process.
5735 IT->pos.overlay_string_index has to be set temporarily to zero
5736 because load_overlay_strings needs this; it must be set to -1
5737 when no overlay strings are found because a zero value would
5738 indicate a position in the first overlay string. */
5739 it->current.overlay_string_index = 0;
5740 load_overlay_strings (it, charpos);
5742 /* If we found overlay strings, set up IT to deliver display
5743 elements from the first one. Otherwise set up IT to deliver
5744 from current_buffer. */
5745 if (it->n_overlay_strings)
5747 /* Make sure we know settings in current_buffer, so that we can
5748 restore meaningful values when we're done with the overlay
5749 strings. */
5750 if (compute_stop_p)
5751 compute_stop_pos (it);
5752 eassert (it->face_id >= 0);
5754 /* Save IT's settings. They are restored after all overlay
5755 strings have been processed. */
5756 eassert (!compute_stop_p || it->sp == 0);
5758 /* When called from handle_stop, there might be an empty display
5759 string loaded. In that case, don't bother saving it. But
5760 don't use this optimization with the bidi iterator, since we
5761 need the corresponding pop_it call to resync the bidi
5762 iterator's position with IT's position, after we are done
5763 with the overlay strings. (The corresponding call to pop_it
5764 in case of an empty display string is in
5765 next_overlay_string.) */
5766 if (!(!it->bidi_p
5767 && STRINGP (it->string) && !SCHARS (it->string)))
5768 push_it (it, NULL);
5770 /* Set up IT to deliver display elements from the first overlay
5771 string. */
5772 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5773 it->string = it->overlay_strings[0];
5774 it->from_overlay = Qnil;
5775 it->stop_charpos = 0;
5776 eassert (STRINGP (it->string));
5777 it->end_charpos = SCHARS (it->string);
5778 it->prev_stop = 0;
5779 it->base_level_stop = 0;
5780 it->multibyte_p = STRING_MULTIBYTE (it->string);
5781 it->method = GET_FROM_STRING;
5782 it->from_disp_prop_p = 0;
5784 /* Force paragraph direction to be that of the parent
5785 buffer. */
5786 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5787 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5788 else
5789 it->paragraph_embedding = L2R;
5791 /* Set up the bidi iterator for this overlay string. */
5792 if (it->bidi_p)
5794 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5796 it->bidi_it.string.lstring = it->string;
5797 it->bidi_it.string.s = NULL;
5798 it->bidi_it.string.schars = SCHARS (it->string);
5799 it->bidi_it.string.bufpos = pos;
5800 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5801 it->bidi_it.string.unibyte = !it->multibyte_p;
5802 it->bidi_it.w = it->w;
5803 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5805 return 1;
5808 it->current.overlay_string_index = -1;
5809 return 0;
5812 static int
5813 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5815 it->string = Qnil;
5816 it->method = GET_FROM_BUFFER;
5818 (void) get_overlay_strings_1 (it, charpos, 1);
5820 CHECK_IT (it);
5822 /* Value is non-zero if we found at least one overlay string. */
5823 return STRINGP (it->string);
5828 /***********************************************************************
5829 Saving and restoring state
5830 ***********************************************************************/
5832 /* Save current settings of IT on IT->stack. Called, for example,
5833 before setting up IT for an overlay string, to be able to restore
5834 IT's settings to what they were after the overlay string has been
5835 processed. If POSITION is non-NULL, it is the position to save on
5836 the stack instead of IT->position. */
5838 static void
5839 push_it (struct it *it, struct text_pos *position)
5841 struct iterator_stack_entry *p;
5843 eassert (it->sp < IT_STACK_SIZE);
5844 p = it->stack + it->sp;
5846 p->stop_charpos = it->stop_charpos;
5847 p->prev_stop = it->prev_stop;
5848 p->base_level_stop = it->base_level_stop;
5849 p->cmp_it = it->cmp_it;
5850 eassert (it->face_id >= 0);
5851 p->face_id = it->face_id;
5852 p->string = it->string;
5853 p->method = it->method;
5854 p->from_overlay = it->from_overlay;
5855 switch (p->method)
5857 case GET_FROM_IMAGE:
5858 p->u.image.object = it->object;
5859 p->u.image.image_id = it->image_id;
5860 p->u.image.slice = it->slice;
5861 break;
5862 case GET_FROM_STRETCH:
5863 p->u.stretch.object = it->object;
5864 break;
5866 p->position = position ? *position : it->position;
5867 p->current = it->current;
5868 p->end_charpos = it->end_charpos;
5869 p->string_nchars = it->string_nchars;
5870 p->area = it->area;
5871 p->multibyte_p = it->multibyte_p;
5872 p->avoid_cursor_p = it->avoid_cursor_p;
5873 p->space_width = it->space_width;
5874 p->font_height = it->font_height;
5875 p->voffset = it->voffset;
5876 p->string_from_display_prop_p = it->string_from_display_prop_p;
5877 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5878 p->display_ellipsis_p = 0;
5879 p->line_wrap = it->line_wrap;
5880 p->bidi_p = it->bidi_p;
5881 p->paragraph_embedding = it->paragraph_embedding;
5882 p->from_disp_prop_p = it->from_disp_prop_p;
5883 ++it->sp;
5885 /* Save the state of the bidi iterator as well. */
5886 if (it->bidi_p)
5887 bidi_push_it (&it->bidi_it);
5890 static void
5891 iterate_out_of_display_property (struct it *it)
5893 int buffer_p = !STRINGP (it->string);
5894 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5895 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5897 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5899 /* Maybe initialize paragraph direction. If we are at the beginning
5900 of a new paragraph, next_element_from_buffer may not have a
5901 chance to do that. */
5902 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5903 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5904 /* prev_stop can be zero, so check against BEGV as well. */
5905 while (it->bidi_it.charpos >= bob
5906 && it->prev_stop <= it->bidi_it.charpos
5907 && it->bidi_it.charpos < CHARPOS (it->position)
5908 && it->bidi_it.charpos < eob)
5909 bidi_move_to_visually_next (&it->bidi_it);
5910 /* Record the stop_pos we just crossed, for when we cross it
5911 back, maybe. */
5912 if (it->bidi_it.charpos > CHARPOS (it->position))
5913 it->prev_stop = CHARPOS (it->position);
5914 /* If we ended up not where pop_it put us, resync IT's
5915 positional members with the bidi iterator. */
5916 if (it->bidi_it.charpos != CHARPOS (it->position))
5917 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5918 if (buffer_p)
5919 it->current.pos = it->position;
5920 else
5921 it->current.string_pos = it->position;
5924 /* Restore IT's settings from IT->stack. Called, for example, when no
5925 more overlay strings must be processed, and we return to delivering
5926 display elements from a buffer, or when the end of a string from a
5927 `display' property is reached and we return to delivering display
5928 elements from an overlay string, or from a buffer. */
5930 static void
5931 pop_it (struct it *it)
5933 struct iterator_stack_entry *p;
5934 int from_display_prop = it->from_disp_prop_p;
5936 eassert (it->sp > 0);
5937 --it->sp;
5938 p = it->stack + it->sp;
5939 it->stop_charpos = p->stop_charpos;
5940 it->prev_stop = p->prev_stop;
5941 it->base_level_stop = p->base_level_stop;
5942 it->cmp_it = p->cmp_it;
5943 it->face_id = p->face_id;
5944 it->current = p->current;
5945 it->position = p->position;
5946 it->string = p->string;
5947 it->from_overlay = p->from_overlay;
5948 if (NILP (it->string))
5949 SET_TEXT_POS (it->current.string_pos, -1, -1);
5950 it->method = p->method;
5951 switch (it->method)
5953 case GET_FROM_IMAGE:
5954 it->image_id = p->u.image.image_id;
5955 it->object = p->u.image.object;
5956 it->slice = p->u.image.slice;
5957 break;
5958 case GET_FROM_STRETCH:
5959 it->object = p->u.stretch.object;
5960 break;
5961 case GET_FROM_BUFFER:
5962 it->object = it->w->contents;
5963 break;
5964 case GET_FROM_STRING:
5965 it->object = it->string;
5966 break;
5967 case GET_FROM_DISPLAY_VECTOR:
5968 if (it->s)
5969 it->method = GET_FROM_C_STRING;
5970 else if (STRINGP (it->string))
5971 it->method = GET_FROM_STRING;
5972 else
5974 it->method = GET_FROM_BUFFER;
5975 it->object = it->w->contents;
5978 it->end_charpos = p->end_charpos;
5979 it->string_nchars = p->string_nchars;
5980 it->area = p->area;
5981 it->multibyte_p = p->multibyte_p;
5982 it->avoid_cursor_p = p->avoid_cursor_p;
5983 it->space_width = p->space_width;
5984 it->font_height = p->font_height;
5985 it->voffset = p->voffset;
5986 it->string_from_display_prop_p = p->string_from_display_prop_p;
5987 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5988 it->line_wrap = p->line_wrap;
5989 it->bidi_p = p->bidi_p;
5990 it->paragraph_embedding = p->paragraph_embedding;
5991 it->from_disp_prop_p = p->from_disp_prop_p;
5992 if (it->bidi_p)
5994 bidi_pop_it (&it->bidi_it);
5995 /* Bidi-iterate until we get out of the portion of text, if any,
5996 covered by a `display' text property or by an overlay with
5997 `display' property. (We cannot just jump there, because the
5998 internal coherency of the bidi iterator state can not be
5999 preserved across such jumps.) We also must determine the
6000 paragraph base direction if the overlay we just processed is
6001 at the beginning of a new paragraph. */
6002 if (from_display_prop
6003 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6004 iterate_out_of_display_property (it);
6006 eassert ((BUFFERP (it->object)
6007 && IT_CHARPOS (*it) == it->bidi_it.charpos
6008 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6009 || (STRINGP (it->object)
6010 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6011 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6012 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6018 /***********************************************************************
6019 Moving over lines
6020 ***********************************************************************/
6022 /* Set IT's current position to the previous line start. */
6024 static void
6025 back_to_previous_line_start (struct it *it)
6027 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6029 DEC_BOTH (cp, bp);
6030 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6034 /* Move IT to the next line start.
6036 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6037 we skipped over part of the text (as opposed to moving the iterator
6038 continuously over the text). Otherwise, don't change the value
6039 of *SKIPPED_P.
6041 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6042 iterator on the newline, if it was found.
6044 Newlines may come from buffer text, overlay strings, or strings
6045 displayed via the `display' property. That's the reason we can't
6046 simply use find_newline_no_quit.
6048 Note that this function may not skip over invisible text that is so
6049 because of text properties and immediately follows a newline. If
6050 it would, function reseat_at_next_visible_line_start, when called
6051 from set_iterator_to_next, would effectively make invisible
6052 characters following a newline part of the wrong glyph row, which
6053 leads to wrong cursor motion. */
6055 static int
6056 forward_to_next_line_start (struct it *it, int *skipped_p,
6057 struct bidi_it *bidi_it_prev)
6059 ptrdiff_t old_selective;
6060 int newline_found_p, n;
6061 const int MAX_NEWLINE_DISTANCE = 500;
6063 /* If already on a newline, just consume it to avoid unintended
6064 skipping over invisible text below. */
6065 if (it->what == IT_CHARACTER
6066 && it->c == '\n'
6067 && CHARPOS (it->position) == IT_CHARPOS (*it))
6069 if (it->bidi_p && bidi_it_prev)
6070 *bidi_it_prev = it->bidi_it;
6071 set_iterator_to_next (it, 0);
6072 it->c = 0;
6073 return 1;
6076 /* Don't handle selective display in the following. It's (a)
6077 unnecessary because it's done by the caller, and (b) leads to an
6078 infinite recursion because next_element_from_ellipsis indirectly
6079 calls this function. */
6080 old_selective = it->selective;
6081 it->selective = 0;
6083 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6084 from buffer text. */
6085 for (n = newline_found_p = 0;
6086 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6087 n += STRINGP (it->string) ? 0 : 1)
6089 if (!get_next_display_element (it))
6090 return 0;
6091 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6092 if (newline_found_p && it->bidi_p && bidi_it_prev)
6093 *bidi_it_prev = it->bidi_it;
6094 set_iterator_to_next (it, 0);
6097 /* If we didn't find a newline near enough, see if we can use a
6098 short-cut. */
6099 if (!newline_found_p)
6101 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6102 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6103 1, &bytepos);
6104 Lisp_Object pos;
6106 eassert (!STRINGP (it->string));
6108 /* If there isn't any `display' property in sight, and no
6109 overlays, we can just use the position of the newline in
6110 buffer text. */
6111 if (it->stop_charpos >= limit
6112 || ((pos = Fnext_single_property_change (make_number (start),
6113 Qdisplay, Qnil,
6114 make_number (limit)),
6115 NILP (pos))
6116 && next_overlay_change (start) == ZV))
6118 if (!it->bidi_p)
6120 IT_CHARPOS (*it) = limit;
6121 IT_BYTEPOS (*it) = bytepos;
6123 else
6125 struct bidi_it bprev;
6127 /* Help bidi.c avoid expensive searches for display
6128 properties and overlays, by telling it that there are
6129 none up to `limit'. */
6130 if (it->bidi_it.disp_pos < limit)
6132 it->bidi_it.disp_pos = limit;
6133 it->bidi_it.disp_prop = 0;
6135 do {
6136 bprev = it->bidi_it;
6137 bidi_move_to_visually_next (&it->bidi_it);
6138 } while (it->bidi_it.charpos != limit);
6139 IT_CHARPOS (*it) = limit;
6140 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6141 if (bidi_it_prev)
6142 *bidi_it_prev = bprev;
6144 *skipped_p = newline_found_p = 1;
6146 else
6148 while (get_next_display_element (it)
6149 && !newline_found_p)
6151 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6152 if (newline_found_p && it->bidi_p && bidi_it_prev)
6153 *bidi_it_prev = it->bidi_it;
6154 set_iterator_to_next (it, 0);
6159 it->selective = old_selective;
6160 return newline_found_p;
6164 /* Set IT's current position to the previous visible line start. Skip
6165 invisible text that is so either due to text properties or due to
6166 selective display. Caution: this does not change IT->current_x and
6167 IT->hpos. */
6169 static void
6170 back_to_previous_visible_line_start (struct it *it)
6172 while (IT_CHARPOS (*it) > BEGV)
6174 back_to_previous_line_start (it);
6176 if (IT_CHARPOS (*it) <= BEGV)
6177 break;
6179 /* If selective > 0, then lines indented more than its value are
6180 invisible. */
6181 if (it->selective > 0
6182 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6183 it->selective))
6184 continue;
6186 /* Check the newline before point for invisibility. */
6188 Lisp_Object prop;
6189 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6190 Qinvisible, it->window);
6191 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6192 continue;
6195 if (IT_CHARPOS (*it) <= BEGV)
6196 break;
6199 struct it it2;
6200 void *it2data = NULL;
6201 ptrdiff_t pos;
6202 ptrdiff_t beg, end;
6203 Lisp_Object val, overlay;
6205 SAVE_IT (it2, *it, it2data);
6207 /* If newline is part of a composition, continue from start of composition */
6208 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6209 && beg < IT_CHARPOS (*it))
6210 goto replaced;
6212 /* If newline is replaced by a display property, find start of overlay
6213 or interval and continue search from that point. */
6214 pos = --IT_CHARPOS (it2);
6215 --IT_BYTEPOS (it2);
6216 it2.sp = 0;
6217 bidi_unshelve_cache (NULL, 0);
6218 it2.string_from_display_prop_p = 0;
6219 it2.from_disp_prop_p = 0;
6220 if (handle_display_prop (&it2) == HANDLED_RETURN
6221 && !NILP (val = get_char_property_and_overlay
6222 (make_number (pos), Qdisplay, Qnil, &overlay))
6223 && (OVERLAYP (overlay)
6224 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6225 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6227 RESTORE_IT (it, it, it2data);
6228 goto replaced;
6231 /* Newline is not replaced by anything -- so we are done. */
6232 RESTORE_IT (it, it, it2data);
6233 break;
6235 replaced:
6236 if (beg < BEGV)
6237 beg = BEGV;
6238 IT_CHARPOS (*it) = beg;
6239 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6243 it->continuation_lines_width = 0;
6245 eassert (IT_CHARPOS (*it) >= BEGV);
6246 eassert (IT_CHARPOS (*it) == BEGV
6247 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6248 CHECK_IT (it);
6252 /* Reseat iterator IT at the previous visible line start. Skip
6253 invisible text that is so either due to text properties or due to
6254 selective display. At the end, update IT's overlay information,
6255 face information etc. */
6257 void
6258 reseat_at_previous_visible_line_start (struct it *it)
6260 back_to_previous_visible_line_start (it);
6261 reseat (it, it->current.pos, 1);
6262 CHECK_IT (it);
6266 /* Reseat iterator IT on the next visible line start in the current
6267 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6268 preceding the line start. Skip over invisible text that is so
6269 because of selective display. Compute faces, overlays etc at the
6270 new position. Note that this function does not skip over text that
6271 is invisible because of text properties. */
6273 static void
6274 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6276 int newline_found_p, skipped_p = 0;
6277 struct bidi_it bidi_it_prev;
6279 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6281 /* Skip over lines that are invisible because they are indented
6282 more than the value of IT->selective. */
6283 if (it->selective > 0)
6284 while (IT_CHARPOS (*it) < ZV
6285 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6286 it->selective))
6288 eassert (IT_BYTEPOS (*it) == BEGV
6289 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6290 newline_found_p =
6291 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6294 /* Position on the newline if that's what's requested. */
6295 if (on_newline_p && newline_found_p)
6297 if (STRINGP (it->string))
6299 if (IT_STRING_CHARPOS (*it) > 0)
6301 if (!it->bidi_p)
6303 --IT_STRING_CHARPOS (*it);
6304 --IT_STRING_BYTEPOS (*it);
6306 else
6308 /* We need to restore the bidi iterator to the state
6309 it had on the newline, and resync the IT's
6310 position with that. */
6311 it->bidi_it = bidi_it_prev;
6312 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6313 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6317 else if (IT_CHARPOS (*it) > BEGV)
6319 if (!it->bidi_p)
6321 --IT_CHARPOS (*it);
6322 --IT_BYTEPOS (*it);
6324 else
6326 /* We need to restore the bidi iterator to the state it
6327 had on the newline and resync IT with that. */
6328 it->bidi_it = bidi_it_prev;
6329 IT_CHARPOS (*it) = it->bidi_it.charpos;
6330 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6332 reseat (it, it->current.pos, 0);
6335 else if (skipped_p)
6336 reseat (it, it->current.pos, 0);
6338 CHECK_IT (it);
6343 /***********************************************************************
6344 Changing an iterator's position
6345 ***********************************************************************/
6347 /* Change IT's current position to POS in current_buffer. If FORCE_P
6348 is non-zero, always check for text properties at the new position.
6349 Otherwise, text properties are only looked up if POS >=
6350 IT->check_charpos of a property. */
6352 static void
6353 reseat (struct it *it, struct text_pos pos, int force_p)
6355 ptrdiff_t original_pos = IT_CHARPOS (*it);
6357 reseat_1 (it, pos, 0);
6359 /* Determine where to check text properties. Avoid doing it
6360 where possible because text property lookup is very expensive. */
6361 if (force_p
6362 || CHARPOS (pos) > it->stop_charpos
6363 || CHARPOS (pos) < original_pos)
6365 if (it->bidi_p)
6367 /* For bidi iteration, we need to prime prev_stop and
6368 base_level_stop with our best estimations. */
6369 /* Implementation note: Of course, POS is not necessarily a
6370 stop position, so assigning prev_pos to it is a lie; we
6371 should have called compute_stop_backwards. However, if
6372 the current buffer does not include any R2L characters,
6373 that call would be a waste of cycles, because the
6374 iterator will never move back, and thus never cross this
6375 "fake" stop position. So we delay that backward search
6376 until the time we really need it, in next_element_from_buffer. */
6377 if (CHARPOS (pos) != it->prev_stop)
6378 it->prev_stop = CHARPOS (pos);
6379 if (CHARPOS (pos) < it->base_level_stop)
6380 it->base_level_stop = 0; /* meaning it's unknown */
6381 handle_stop (it);
6383 else
6385 handle_stop (it);
6386 it->prev_stop = it->base_level_stop = 0;
6391 CHECK_IT (it);
6395 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6396 IT->stop_pos to POS, also. */
6398 static void
6399 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6401 /* Don't call this function when scanning a C string. */
6402 eassert (it->s == NULL);
6404 /* POS must be a reasonable value. */
6405 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6407 it->current.pos = it->position = pos;
6408 it->end_charpos = ZV;
6409 it->dpvec = NULL;
6410 it->current.dpvec_index = -1;
6411 it->current.overlay_string_index = -1;
6412 IT_STRING_CHARPOS (*it) = -1;
6413 IT_STRING_BYTEPOS (*it) = -1;
6414 it->string = Qnil;
6415 it->method = GET_FROM_BUFFER;
6416 it->object = it->w->contents;
6417 it->area = TEXT_AREA;
6418 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6419 it->sp = 0;
6420 it->string_from_display_prop_p = 0;
6421 it->string_from_prefix_prop_p = 0;
6423 it->from_disp_prop_p = 0;
6424 it->face_before_selective_p = 0;
6425 if (it->bidi_p)
6427 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6428 &it->bidi_it);
6429 bidi_unshelve_cache (NULL, 0);
6430 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6431 it->bidi_it.string.s = NULL;
6432 it->bidi_it.string.lstring = Qnil;
6433 it->bidi_it.string.bufpos = 0;
6434 it->bidi_it.string.unibyte = 0;
6435 it->bidi_it.w = it->w;
6438 if (set_stop_p)
6440 it->stop_charpos = CHARPOS (pos);
6441 it->base_level_stop = CHARPOS (pos);
6443 /* This make the information stored in it->cmp_it invalidate. */
6444 it->cmp_it.id = -1;
6448 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6449 If S is non-null, it is a C string to iterate over. Otherwise,
6450 STRING gives a Lisp string to iterate over.
6452 If PRECISION > 0, don't return more then PRECISION number of
6453 characters from the string.
6455 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6456 characters have been returned. FIELD_WIDTH < 0 means an infinite
6457 field width.
6459 MULTIBYTE = 0 means disable processing of multibyte characters,
6460 MULTIBYTE > 0 means enable it,
6461 MULTIBYTE < 0 means use IT->multibyte_p.
6463 IT must be initialized via a prior call to init_iterator before
6464 calling this function. */
6466 static void
6467 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6468 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6469 int multibyte)
6471 /* No region in strings. */
6472 it->region_beg_charpos = it->region_end_charpos = -1;
6474 /* No text property checks performed by default, but see below. */
6475 it->stop_charpos = -1;
6477 /* Set iterator position and end position. */
6478 memset (&it->current, 0, sizeof it->current);
6479 it->current.overlay_string_index = -1;
6480 it->current.dpvec_index = -1;
6481 eassert (charpos >= 0);
6483 /* If STRING is specified, use its multibyteness, otherwise use the
6484 setting of MULTIBYTE, if specified. */
6485 if (multibyte >= 0)
6486 it->multibyte_p = multibyte > 0;
6488 /* Bidirectional reordering of strings is controlled by the default
6489 value of bidi-display-reordering. Don't try to reorder while
6490 loading loadup.el, as the necessary character property tables are
6491 not yet available. */
6492 it->bidi_p =
6493 NILP (Vpurify_flag)
6494 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6496 if (s == NULL)
6498 eassert (STRINGP (string));
6499 it->string = string;
6500 it->s = NULL;
6501 it->end_charpos = it->string_nchars = SCHARS (string);
6502 it->method = GET_FROM_STRING;
6503 it->current.string_pos = string_pos (charpos, string);
6505 if (it->bidi_p)
6507 it->bidi_it.string.lstring = string;
6508 it->bidi_it.string.s = NULL;
6509 it->bidi_it.string.schars = it->end_charpos;
6510 it->bidi_it.string.bufpos = 0;
6511 it->bidi_it.string.from_disp_str = 0;
6512 it->bidi_it.string.unibyte = !it->multibyte_p;
6513 it->bidi_it.w = it->w;
6514 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6515 FRAME_WINDOW_P (it->f), &it->bidi_it);
6518 else
6520 it->s = (const unsigned char *) s;
6521 it->string = Qnil;
6523 /* Note that we use IT->current.pos, not it->current.string_pos,
6524 for displaying C strings. */
6525 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6526 if (it->multibyte_p)
6528 it->current.pos = c_string_pos (charpos, s, 1);
6529 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6531 else
6533 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6534 it->end_charpos = it->string_nchars = strlen (s);
6537 if (it->bidi_p)
6539 it->bidi_it.string.lstring = Qnil;
6540 it->bidi_it.string.s = (const unsigned char *) s;
6541 it->bidi_it.string.schars = it->end_charpos;
6542 it->bidi_it.string.bufpos = 0;
6543 it->bidi_it.string.from_disp_str = 0;
6544 it->bidi_it.string.unibyte = !it->multibyte_p;
6545 it->bidi_it.w = it->w;
6546 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6547 &it->bidi_it);
6549 it->method = GET_FROM_C_STRING;
6552 /* PRECISION > 0 means don't return more than PRECISION characters
6553 from the string. */
6554 if (precision > 0 && it->end_charpos - charpos > precision)
6556 it->end_charpos = it->string_nchars = charpos + precision;
6557 if (it->bidi_p)
6558 it->bidi_it.string.schars = it->end_charpos;
6561 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6562 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6563 FIELD_WIDTH < 0 means infinite field width. This is useful for
6564 padding with `-' at the end of a mode line. */
6565 if (field_width < 0)
6566 field_width = INFINITY;
6567 /* Implementation note: We deliberately don't enlarge
6568 it->bidi_it.string.schars here to fit it->end_charpos, because
6569 the bidi iterator cannot produce characters out of thin air. */
6570 if (field_width > it->end_charpos - charpos)
6571 it->end_charpos = charpos + field_width;
6573 /* Use the standard display table for displaying strings. */
6574 if (DISP_TABLE_P (Vstandard_display_table))
6575 it->dp = XCHAR_TABLE (Vstandard_display_table);
6577 it->stop_charpos = charpos;
6578 it->prev_stop = charpos;
6579 it->base_level_stop = 0;
6580 if (it->bidi_p)
6582 it->bidi_it.first_elt = 1;
6583 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6584 it->bidi_it.disp_pos = -1;
6586 if (s == NULL && it->multibyte_p)
6588 ptrdiff_t endpos = SCHARS (it->string);
6589 if (endpos > it->end_charpos)
6590 endpos = it->end_charpos;
6591 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6592 it->string);
6594 CHECK_IT (it);
6599 /***********************************************************************
6600 Iteration
6601 ***********************************************************************/
6603 /* Map enum it_method value to corresponding next_element_from_* function. */
6605 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6607 next_element_from_buffer,
6608 next_element_from_display_vector,
6609 next_element_from_string,
6610 next_element_from_c_string,
6611 next_element_from_image,
6612 next_element_from_stretch
6615 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6618 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6619 (possibly with the following characters). */
6621 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6622 ((IT)->cmp_it.id >= 0 \
6623 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6624 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6625 END_CHARPOS, (IT)->w, \
6626 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6627 (IT)->string)))
6630 /* Lookup the char-table Vglyphless_char_display for character C (-1
6631 if we want information for no-font case), and return the display
6632 method symbol. By side-effect, update it->what and
6633 it->glyphless_method. This function is called from
6634 get_next_display_element for each character element, and from
6635 x_produce_glyphs when no suitable font was found. */
6637 Lisp_Object
6638 lookup_glyphless_char_display (int c, struct it *it)
6640 Lisp_Object glyphless_method = Qnil;
6642 if (CHAR_TABLE_P (Vglyphless_char_display)
6643 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6645 if (c >= 0)
6647 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6648 if (CONSP (glyphless_method))
6649 glyphless_method = FRAME_WINDOW_P (it->f)
6650 ? XCAR (glyphless_method)
6651 : XCDR (glyphless_method);
6653 else
6654 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6657 retry:
6658 if (NILP (glyphless_method))
6660 if (c >= 0)
6661 /* The default is to display the character by a proper font. */
6662 return Qnil;
6663 /* The default for the no-font case is to display an empty box. */
6664 glyphless_method = Qempty_box;
6666 if (EQ (glyphless_method, Qzero_width))
6668 if (c >= 0)
6669 return glyphless_method;
6670 /* This method can't be used for the no-font case. */
6671 glyphless_method = Qempty_box;
6673 if (EQ (glyphless_method, Qthin_space))
6674 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6675 else if (EQ (glyphless_method, Qempty_box))
6676 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6677 else if (EQ (glyphless_method, Qhex_code))
6678 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6679 else if (STRINGP (glyphless_method))
6680 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6681 else
6683 /* Invalid value. We use the default method. */
6684 glyphless_method = Qnil;
6685 goto retry;
6687 it->what = IT_GLYPHLESS;
6688 return glyphless_method;
6691 /* Load IT's display element fields with information about the next
6692 display element from the current position of IT. Value is zero if
6693 end of buffer (or C string) is reached. */
6695 static struct frame *last_escape_glyph_frame = NULL;
6696 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6697 static int last_escape_glyph_merged_face_id = 0;
6699 struct frame *last_glyphless_glyph_frame = NULL;
6700 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6701 int last_glyphless_glyph_merged_face_id = 0;
6703 static int
6704 get_next_display_element (struct it *it)
6706 /* Non-zero means that we found a display element. Zero means that
6707 we hit the end of what we iterate over. Performance note: the
6708 function pointer `method' used here turns out to be faster than
6709 using a sequence of if-statements. */
6710 int success_p;
6712 get_next:
6713 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6715 if (it->what == IT_CHARACTER)
6717 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6718 and only if (a) the resolved directionality of that character
6719 is R..." */
6720 /* FIXME: Do we need an exception for characters from display
6721 tables? */
6722 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6723 it->c = bidi_mirror_char (it->c);
6724 /* Map via display table or translate control characters.
6725 IT->c, IT->len etc. have been set to the next character by
6726 the function call above. If we have a display table, and it
6727 contains an entry for IT->c, translate it. Don't do this if
6728 IT->c itself comes from a display table, otherwise we could
6729 end up in an infinite recursion. (An alternative could be to
6730 count the recursion depth of this function and signal an
6731 error when a certain maximum depth is reached.) Is it worth
6732 it? */
6733 if (success_p && it->dpvec == NULL)
6735 Lisp_Object dv;
6736 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6737 int nonascii_space_p = 0;
6738 int nonascii_hyphen_p = 0;
6739 int c = it->c; /* This is the character to display. */
6741 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6743 eassert (SINGLE_BYTE_CHAR_P (c));
6744 if (unibyte_display_via_language_environment)
6746 c = DECODE_CHAR (unibyte, c);
6747 if (c < 0)
6748 c = BYTE8_TO_CHAR (it->c);
6750 else
6751 c = BYTE8_TO_CHAR (it->c);
6754 if (it->dp
6755 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6756 VECTORP (dv)))
6758 struct Lisp_Vector *v = XVECTOR (dv);
6760 /* Return the first character from the display table
6761 entry, if not empty. If empty, don't display the
6762 current character. */
6763 if (v->header.size)
6765 it->dpvec_char_len = it->len;
6766 it->dpvec = v->contents;
6767 it->dpend = v->contents + v->header.size;
6768 it->current.dpvec_index = 0;
6769 it->dpvec_face_id = -1;
6770 it->saved_face_id = it->face_id;
6771 it->method = GET_FROM_DISPLAY_VECTOR;
6772 it->ellipsis_p = 0;
6774 else
6776 set_iterator_to_next (it, 0);
6778 goto get_next;
6781 if (! NILP (lookup_glyphless_char_display (c, it)))
6783 if (it->what == IT_GLYPHLESS)
6784 goto done;
6785 /* Don't display this character. */
6786 set_iterator_to_next (it, 0);
6787 goto get_next;
6790 /* If `nobreak-char-display' is non-nil, we display
6791 non-ASCII spaces and hyphens specially. */
6792 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6794 if (c == 0xA0)
6795 nonascii_space_p = 1;
6796 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6797 nonascii_hyphen_p = 1;
6800 /* Translate control characters into `\003' or `^C' form.
6801 Control characters coming from a display table entry are
6802 currently not translated because we use IT->dpvec to hold
6803 the translation. This could easily be changed but I
6804 don't believe that it is worth doing.
6806 The characters handled by `nobreak-char-display' must be
6807 translated too.
6809 Non-printable characters and raw-byte characters are also
6810 translated to octal form. */
6811 if (((c < ' ' || c == 127) /* ASCII control chars */
6812 ? (it->area != TEXT_AREA
6813 /* In mode line, treat \n, \t like other crl chars. */
6814 || (c != '\t'
6815 && it->glyph_row
6816 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6817 || (c != '\n' && c != '\t'))
6818 : (nonascii_space_p
6819 || nonascii_hyphen_p
6820 || CHAR_BYTE8_P (c)
6821 || ! CHAR_PRINTABLE_P (c))))
6823 /* C is a control character, non-ASCII space/hyphen,
6824 raw-byte, or a non-printable character which must be
6825 displayed either as '\003' or as `^C' where the '\\'
6826 and '^' can be defined in the display table. Fill
6827 IT->ctl_chars with glyphs for what we have to
6828 display. Then, set IT->dpvec to these glyphs. */
6829 Lisp_Object gc;
6830 int ctl_len;
6831 int face_id;
6832 int lface_id = 0;
6833 int escape_glyph;
6835 /* Handle control characters with ^. */
6837 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6839 int g;
6841 g = '^'; /* default glyph for Control */
6842 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6843 if (it->dp
6844 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6846 g = GLYPH_CODE_CHAR (gc);
6847 lface_id = GLYPH_CODE_FACE (gc);
6849 if (lface_id)
6851 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6853 else if (it->f == last_escape_glyph_frame
6854 && it->face_id == last_escape_glyph_face_id)
6856 face_id = last_escape_glyph_merged_face_id;
6858 else
6860 /* Merge the escape-glyph face into the current face. */
6861 face_id = merge_faces (it->f, Qescape_glyph, 0,
6862 it->face_id);
6863 last_escape_glyph_frame = it->f;
6864 last_escape_glyph_face_id = it->face_id;
6865 last_escape_glyph_merged_face_id = face_id;
6868 XSETINT (it->ctl_chars[0], g);
6869 XSETINT (it->ctl_chars[1], c ^ 0100);
6870 ctl_len = 2;
6871 goto display_control;
6874 /* Handle non-ascii space in the mode where it only gets
6875 highlighting. */
6877 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6879 /* Merge `nobreak-space' into the current face. */
6880 face_id = merge_faces (it->f, Qnobreak_space, 0,
6881 it->face_id);
6882 XSETINT (it->ctl_chars[0], ' ');
6883 ctl_len = 1;
6884 goto display_control;
6887 /* Handle sequences that start with the "escape glyph". */
6889 /* the default escape glyph is \. */
6890 escape_glyph = '\\';
6892 if (it->dp
6893 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6895 escape_glyph = GLYPH_CODE_CHAR (gc);
6896 lface_id = GLYPH_CODE_FACE (gc);
6898 if (lface_id)
6900 /* The display table specified a face.
6901 Merge it into face_id and also into escape_glyph. */
6902 face_id = merge_faces (it->f, Qt, lface_id,
6903 it->face_id);
6905 else if (it->f == last_escape_glyph_frame
6906 && it->face_id == last_escape_glyph_face_id)
6908 face_id = last_escape_glyph_merged_face_id;
6910 else
6912 /* Merge the escape-glyph face into the current face. */
6913 face_id = merge_faces (it->f, Qescape_glyph, 0,
6914 it->face_id);
6915 last_escape_glyph_frame = it->f;
6916 last_escape_glyph_face_id = it->face_id;
6917 last_escape_glyph_merged_face_id = face_id;
6920 /* Draw non-ASCII hyphen with just highlighting: */
6922 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6924 XSETINT (it->ctl_chars[0], '-');
6925 ctl_len = 1;
6926 goto display_control;
6929 /* Draw non-ASCII space/hyphen with escape glyph: */
6931 if (nonascii_space_p || nonascii_hyphen_p)
6933 XSETINT (it->ctl_chars[0], escape_glyph);
6934 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6935 ctl_len = 2;
6936 goto display_control;
6940 char str[10];
6941 int len, i;
6943 if (CHAR_BYTE8_P (c))
6944 /* Display \200 instead of \17777600. */
6945 c = CHAR_TO_BYTE8 (c);
6946 len = sprintf (str, "%03o", c);
6948 XSETINT (it->ctl_chars[0], escape_glyph);
6949 for (i = 0; i < len; i++)
6950 XSETINT (it->ctl_chars[i + 1], str[i]);
6951 ctl_len = len + 1;
6954 display_control:
6955 /* Set up IT->dpvec and return first character from it. */
6956 it->dpvec_char_len = it->len;
6957 it->dpvec = it->ctl_chars;
6958 it->dpend = it->dpvec + ctl_len;
6959 it->current.dpvec_index = 0;
6960 it->dpvec_face_id = face_id;
6961 it->saved_face_id = it->face_id;
6962 it->method = GET_FROM_DISPLAY_VECTOR;
6963 it->ellipsis_p = 0;
6964 goto get_next;
6966 it->char_to_display = c;
6968 else if (success_p)
6970 it->char_to_display = it->c;
6974 /* Adjust face id for a multibyte character. There are no multibyte
6975 character in unibyte text. */
6976 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6977 && it->multibyte_p
6978 && success_p
6979 && FRAME_WINDOW_P (it->f))
6981 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6983 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6985 /* Automatic composition with glyph-string. */
6986 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6988 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6990 else
6992 ptrdiff_t pos = (it->s ? -1
6993 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6994 : IT_CHARPOS (*it));
6995 int c;
6997 if (it->what == IT_CHARACTER)
6998 c = it->char_to_display;
6999 else
7001 struct composition *cmp = composition_table[it->cmp_it.id];
7002 int i;
7004 c = ' ';
7005 for (i = 0; i < cmp->glyph_len; i++)
7006 /* TAB in a composition means display glyphs with
7007 padding space on the left or right. */
7008 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7009 break;
7011 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7015 done:
7016 /* Is this character the last one of a run of characters with
7017 box? If yes, set IT->end_of_box_run_p to 1. */
7018 if (it->face_box_p
7019 && it->s == NULL)
7021 if (it->method == GET_FROM_STRING && it->sp)
7023 int face_id = underlying_face_id (it);
7024 struct face *face = FACE_FROM_ID (it->f, face_id);
7026 if (face)
7028 if (face->box == FACE_NO_BOX)
7030 /* If the box comes from face properties in a
7031 display string, check faces in that string. */
7032 int string_face_id = face_after_it_pos (it);
7033 it->end_of_box_run_p
7034 = (FACE_FROM_ID (it->f, string_face_id)->box
7035 == FACE_NO_BOX);
7037 /* Otherwise, the box comes from the underlying face.
7038 If this is the last string character displayed, check
7039 the next buffer location. */
7040 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7041 && (it->current.overlay_string_index
7042 == it->n_overlay_strings - 1))
7044 ptrdiff_t ignore;
7045 int next_face_id;
7046 struct text_pos pos = it->current.pos;
7047 INC_TEXT_POS (pos, it->multibyte_p);
7049 next_face_id = face_at_buffer_position
7050 (it->w, CHARPOS (pos), it->region_beg_charpos,
7051 it->region_end_charpos, &ignore,
7052 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7053 -1);
7054 it->end_of_box_run_p
7055 = (FACE_FROM_ID (it->f, next_face_id)->box
7056 == FACE_NO_BOX);
7060 /* next_element_from_display_vector sets this flag according to
7061 faces of the display vector glyphs, see there. */
7062 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7064 int face_id = face_after_it_pos (it);
7065 it->end_of_box_run_p
7066 = (face_id != it->face_id
7067 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7070 /* If we reached the end of the object we've been iterating (e.g., a
7071 display string or an overlay string), and there's something on
7072 IT->stack, proceed with what's on the stack. It doesn't make
7073 sense to return zero if there's unprocessed stuff on the stack,
7074 because otherwise that stuff will never be displayed. */
7075 if (!success_p && it->sp > 0)
7077 set_iterator_to_next (it, 0);
7078 success_p = get_next_display_element (it);
7081 /* Value is 0 if end of buffer or string reached. */
7082 return success_p;
7086 /* Move IT to the next display element.
7088 RESEAT_P non-zero means if called on a newline in buffer text,
7089 skip to the next visible line start.
7091 Functions get_next_display_element and set_iterator_to_next are
7092 separate because I find this arrangement easier to handle than a
7093 get_next_display_element function that also increments IT's
7094 position. The way it is we can first look at an iterator's current
7095 display element, decide whether it fits on a line, and if it does,
7096 increment the iterator position. The other way around we probably
7097 would either need a flag indicating whether the iterator has to be
7098 incremented the next time, or we would have to implement a
7099 decrement position function which would not be easy to write. */
7101 void
7102 set_iterator_to_next (struct it *it, int reseat_p)
7104 /* Reset flags indicating start and end of a sequence of characters
7105 with box. Reset them at the start of this function because
7106 moving the iterator to a new position might set them. */
7107 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7109 switch (it->method)
7111 case GET_FROM_BUFFER:
7112 /* The current display element of IT is a character from
7113 current_buffer. Advance in the buffer, and maybe skip over
7114 invisible lines that are so because of selective display. */
7115 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7116 reseat_at_next_visible_line_start (it, 0);
7117 else if (it->cmp_it.id >= 0)
7119 /* We are currently getting glyphs from a composition. */
7120 int i;
7122 if (! it->bidi_p)
7124 IT_CHARPOS (*it) += it->cmp_it.nchars;
7125 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7126 if (it->cmp_it.to < it->cmp_it.nglyphs)
7128 it->cmp_it.from = it->cmp_it.to;
7130 else
7132 it->cmp_it.id = -1;
7133 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7134 IT_BYTEPOS (*it),
7135 it->end_charpos, Qnil);
7138 else if (! it->cmp_it.reversed_p)
7140 /* Composition created while scanning forward. */
7141 /* Update IT's char/byte positions to point to the first
7142 character of the next grapheme cluster, or to the
7143 character visually after the current composition. */
7144 for (i = 0; i < it->cmp_it.nchars; i++)
7145 bidi_move_to_visually_next (&it->bidi_it);
7146 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7147 IT_CHARPOS (*it) = it->bidi_it.charpos;
7149 if (it->cmp_it.to < it->cmp_it.nglyphs)
7151 /* Proceed to the next grapheme cluster. */
7152 it->cmp_it.from = it->cmp_it.to;
7154 else
7156 /* No more grapheme clusters in this composition.
7157 Find the next stop position. */
7158 ptrdiff_t stop = it->end_charpos;
7159 if (it->bidi_it.scan_dir < 0)
7160 /* Now we are scanning backward and don't know
7161 where to stop. */
7162 stop = -1;
7163 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7164 IT_BYTEPOS (*it), stop, Qnil);
7167 else
7169 /* Composition created while scanning backward. */
7170 /* Update IT's char/byte positions to point to the last
7171 character of the previous grapheme cluster, or the
7172 character visually after the current composition. */
7173 for (i = 0; i < it->cmp_it.nchars; i++)
7174 bidi_move_to_visually_next (&it->bidi_it);
7175 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7176 IT_CHARPOS (*it) = it->bidi_it.charpos;
7177 if (it->cmp_it.from > 0)
7179 /* Proceed to the previous grapheme cluster. */
7180 it->cmp_it.to = it->cmp_it.from;
7182 else
7184 /* No more grapheme clusters in this composition.
7185 Find the next stop position. */
7186 ptrdiff_t stop = it->end_charpos;
7187 if (it->bidi_it.scan_dir < 0)
7188 /* Now we are scanning backward and don't know
7189 where to stop. */
7190 stop = -1;
7191 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7192 IT_BYTEPOS (*it), stop, Qnil);
7196 else
7198 eassert (it->len != 0);
7200 if (!it->bidi_p)
7202 IT_BYTEPOS (*it) += it->len;
7203 IT_CHARPOS (*it) += 1;
7205 else
7207 int prev_scan_dir = it->bidi_it.scan_dir;
7208 /* If this is a new paragraph, determine its base
7209 direction (a.k.a. its base embedding level). */
7210 if (it->bidi_it.new_paragraph)
7211 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7212 bidi_move_to_visually_next (&it->bidi_it);
7213 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7214 IT_CHARPOS (*it) = it->bidi_it.charpos;
7215 if (prev_scan_dir != it->bidi_it.scan_dir)
7217 /* As the scan direction was changed, we must
7218 re-compute the stop position for composition. */
7219 ptrdiff_t stop = it->end_charpos;
7220 if (it->bidi_it.scan_dir < 0)
7221 stop = -1;
7222 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7223 IT_BYTEPOS (*it), stop, Qnil);
7226 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7228 break;
7230 case GET_FROM_C_STRING:
7231 /* Current display element of IT is from a C string. */
7232 if (!it->bidi_p
7233 /* If the string position is beyond string's end, it means
7234 next_element_from_c_string is padding the string with
7235 blanks, in which case we bypass the bidi iterator,
7236 because it cannot deal with such virtual characters. */
7237 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7239 IT_BYTEPOS (*it) += it->len;
7240 IT_CHARPOS (*it) += 1;
7242 else
7244 bidi_move_to_visually_next (&it->bidi_it);
7245 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7246 IT_CHARPOS (*it) = it->bidi_it.charpos;
7248 break;
7250 case GET_FROM_DISPLAY_VECTOR:
7251 /* Current display element of IT is from a display table entry.
7252 Advance in the display table definition. Reset it to null if
7253 end reached, and continue with characters from buffers/
7254 strings. */
7255 ++it->current.dpvec_index;
7257 /* Restore face of the iterator to what they were before the
7258 display vector entry (these entries may contain faces). */
7259 it->face_id = it->saved_face_id;
7261 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7263 int recheck_faces = it->ellipsis_p;
7265 if (it->s)
7266 it->method = GET_FROM_C_STRING;
7267 else if (STRINGP (it->string))
7268 it->method = GET_FROM_STRING;
7269 else
7271 it->method = GET_FROM_BUFFER;
7272 it->object = it->w->contents;
7275 it->dpvec = NULL;
7276 it->current.dpvec_index = -1;
7278 /* Skip over characters which were displayed via IT->dpvec. */
7279 if (it->dpvec_char_len < 0)
7280 reseat_at_next_visible_line_start (it, 1);
7281 else if (it->dpvec_char_len > 0)
7283 if (it->method == GET_FROM_STRING
7284 && it->current.overlay_string_index >= 0
7285 && it->n_overlay_strings > 0)
7286 it->ignore_overlay_strings_at_pos_p = 1;
7287 it->len = it->dpvec_char_len;
7288 set_iterator_to_next (it, reseat_p);
7291 /* Maybe recheck faces after display vector */
7292 if (recheck_faces)
7293 it->stop_charpos = IT_CHARPOS (*it);
7295 break;
7297 case GET_FROM_STRING:
7298 /* Current display element is a character from a Lisp string. */
7299 eassert (it->s == NULL && STRINGP (it->string));
7300 /* Don't advance past string end. These conditions are true
7301 when set_iterator_to_next is called at the end of
7302 get_next_display_element, in which case the Lisp string is
7303 already exhausted, and all we want is pop the iterator
7304 stack. */
7305 if (it->current.overlay_string_index >= 0)
7307 /* This is an overlay string, so there's no padding with
7308 spaces, and the number of characters in the string is
7309 where the string ends. */
7310 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7311 goto consider_string_end;
7313 else
7315 /* Not an overlay string. There could be padding, so test
7316 against it->end_charpos . */
7317 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7318 goto consider_string_end;
7320 if (it->cmp_it.id >= 0)
7322 int i;
7324 if (! it->bidi_p)
7326 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7327 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7328 if (it->cmp_it.to < it->cmp_it.nglyphs)
7329 it->cmp_it.from = it->cmp_it.to;
7330 else
7332 it->cmp_it.id = -1;
7333 composition_compute_stop_pos (&it->cmp_it,
7334 IT_STRING_CHARPOS (*it),
7335 IT_STRING_BYTEPOS (*it),
7336 it->end_charpos, it->string);
7339 else if (! it->cmp_it.reversed_p)
7341 for (i = 0; i < it->cmp_it.nchars; i++)
7342 bidi_move_to_visually_next (&it->bidi_it);
7343 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7344 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7346 if (it->cmp_it.to < it->cmp_it.nglyphs)
7347 it->cmp_it.from = it->cmp_it.to;
7348 else
7350 ptrdiff_t stop = it->end_charpos;
7351 if (it->bidi_it.scan_dir < 0)
7352 stop = -1;
7353 composition_compute_stop_pos (&it->cmp_it,
7354 IT_STRING_CHARPOS (*it),
7355 IT_STRING_BYTEPOS (*it), stop,
7356 it->string);
7359 else
7361 for (i = 0; i < it->cmp_it.nchars; i++)
7362 bidi_move_to_visually_next (&it->bidi_it);
7363 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7364 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7365 if (it->cmp_it.from > 0)
7366 it->cmp_it.to = it->cmp_it.from;
7367 else
7369 ptrdiff_t stop = it->end_charpos;
7370 if (it->bidi_it.scan_dir < 0)
7371 stop = -1;
7372 composition_compute_stop_pos (&it->cmp_it,
7373 IT_STRING_CHARPOS (*it),
7374 IT_STRING_BYTEPOS (*it), stop,
7375 it->string);
7379 else
7381 if (!it->bidi_p
7382 /* If the string position is beyond string's end, it
7383 means next_element_from_string is padding the string
7384 with blanks, in which case we bypass the bidi
7385 iterator, because it cannot deal with such virtual
7386 characters. */
7387 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7389 IT_STRING_BYTEPOS (*it) += it->len;
7390 IT_STRING_CHARPOS (*it) += 1;
7392 else
7394 int prev_scan_dir = it->bidi_it.scan_dir;
7396 bidi_move_to_visually_next (&it->bidi_it);
7397 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7398 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7399 if (prev_scan_dir != it->bidi_it.scan_dir)
7401 ptrdiff_t stop = it->end_charpos;
7403 if (it->bidi_it.scan_dir < 0)
7404 stop = -1;
7405 composition_compute_stop_pos (&it->cmp_it,
7406 IT_STRING_CHARPOS (*it),
7407 IT_STRING_BYTEPOS (*it), stop,
7408 it->string);
7413 consider_string_end:
7415 if (it->current.overlay_string_index >= 0)
7417 /* IT->string is an overlay string. Advance to the
7418 next, if there is one. */
7419 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7421 it->ellipsis_p = 0;
7422 next_overlay_string (it);
7423 if (it->ellipsis_p)
7424 setup_for_ellipsis (it, 0);
7427 else
7429 /* IT->string is not an overlay string. If we reached
7430 its end, and there is something on IT->stack, proceed
7431 with what is on the stack. This can be either another
7432 string, this time an overlay string, or a buffer. */
7433 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7434 && it->sp > 0)
7436 pop_it (it);
7437 if (it->method == GET_FROM_STRING)
7438 goto consider_string_end;
7441 break;
7443 case GET_FROM_IMAGE:
7444 case GET_FROM_STRETCH:
7445 /* The position etc with which we have to proceed are on
7446 the stack. The position may be at the end of a string,
7447 if the `display' property takes up the whole string. */
7448 eassert (it->sp > 0);
7449 pop_it (it);
7450 if (it->method == GET_FROM_STRING)
7451 goto consider_string_end;
7452 break;
7454 default:
7455 /* There are no other methods defined, so this should be a bug. */
7456 emacs_abort ();
7459 eassert (it->method != GET_FROM_STRING
7460 || (STRINGP (it->string)
7461 && IT_STRING_CHARPOS (*it) >= 0));
7464 /* Load IT's display element fields with information about the next
7465 display element which comes from a display table entry or from the
7466 result of translating a control character to one of the forms `^C'
7467 or `\003'.
7469 IT->dpvec holds the glyphs to return as characters.
7470 IT->saved_face_id holds the face id before the display vector--it
7471 is restored into IT->face_id in set_iterator_to_next. */
7473 static int
7474 next_element_from_display_vector (struct it *it)
7476 Lisp_Object gc;
7477 int prev_face_id = it->face_id;
7478 int next_face_id;
7480 /* Precondition. */
7481 eassert (it->dpvec && it->current.dpvec_index >= 0);
7483 it->face_id = it->saved_face_id;
7485 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7486 That seemed totally bogus - so I changed it... */
7487 gc = it->dpvec[it->current.dpvec_index];
7489 if (GLYPH_CODE_P (gc))
7491 struct face *this_face, *prev_face, *next_face;
7493 it->c = GLYPH_CODE_CHAR (gc);
7494 it->len = CHAR_BYTES (it->c);
7496 /* The entry may contain a face id to use. Such a face id is
7497 the id of a Lisp face, not a realized face. A face id of
7498 zero means no face is specified. */
7499 if (it->dpvec_face_id >= 0)
7500 it->face_id = it->dpvec_face_id;
7501 else
7503 int lface_id = GLYPH_CODE_FACE (gc);
7504 if (lface_id > 0)
7505 it->face_id = merge_faces (it->f, Qt, lface_id,
7506 it->saved_face_id);
7509 /* Glyphs in the display vector could have the box face, so we
7510 need to set the related flags in the iterator, as
7511 appropriate. */
7512 this_face = FACE_FROM_ID (it->f, it->face_id);
7513 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7515 /* Is this character the first character of a box-face run? */
7516 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7517 && (!prev_face
7518 || prev_face->box == FACE_NO_BOX));
7520 /* For the last character of the box-face run, we need to look
7521 either at the next glyph from the display vector, or at the
7522 face we saw before the display vector. */
7523 next_face_id = it->saved_face_id;
7524 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7526 if (it->dpvec_face_id >= 0)
7527 next_face_id = it->dpvec_face_id;
7528 else
7530 int lface_id =
7531 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7533 if (lface_id > 0)
7534 next_face_id = merge_faces (it->f, Qt, lface_id,
7535 it->saved_face_id);
7538 next_face = FACE_FROM_ID (it->f, next_face_id);
7539 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7540 && (!next_face
7541 || next_face->box == FACE_NO_BOX));
7542 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7544 else
7545 /* Display table entry is invalid. Return a space. */
7546 it->c = ' ', it->len = 1;
7548 /* Don't change position and object of the iterator here. They are
7549 still the values of the character that had this display table
7550 entry or was translated, and that's what we want. */
7551 it->what = IT_CHARACTER;
7552 return 1;
7555 /* Get the first element of string/buffer in the visual order, after
7556 being reseated to a new position in a string or a buffer. */
7557 static void
7558 get_visually_first_element (struct it *it)
7560 int string_p = STRINGP (it->string) || it->s;
7561 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7562 ptrdiff_t bob = (string_p ? 0 : BEGV);
7564 if (STRINGP (it->string))
7566 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7567 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7569 else
7571 it->bidi_it.charpos = IT_CHARPOS (*it);
7572 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7575 if (it->bidi_it.charpos == eob)
7577 /* Nothing to do, but reset the FIRST_ELT flag, like
7578 bidi_paragraph_init does, because we are not going to
7579 call it. */
7580 it->bidi_it.first_elt = 0;
7582 else if (it->bidi_it.charpos == bob
7583 || (!string_p
7584 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7585 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7587 /* If we are at the beginning of a line/string, we can produce
7588 the next element right away. */
7589 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7590 bidi_move_to_visually_next (&it->bidi_it);
7592 else
7594 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7596 /* We need to prime the bidi iterator starting at the line's or
7597 string's beginning, before we will be able to produce the
7598 next element. */
7599 if (string_p)
7600 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7601 else
7602 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7603 IT_BYTEPOS (*it), -1,
7604 &it->bidi_it.bytepos);
7605 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7608 /* Now return to buffer/string position where we were asked
7609 to get the next display element, and produce that. */
7610 bidi_move_to_visually_next (&it->bidi_it);
7612 while (it->bidi_it.bytepos != orig_bytepos
7613 && it->bidi_it.charpos < eob);
7616 /* Adjust IT's position information to where we ended up. */
7617 if (STRINGP (it->string))
7619 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7620 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7622 else
7624 IT_CHARPOS (*it) = it->bidi_it.charpos;
7625 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7628 if (STRINGP (it->string) || !it->s)
7630 ptrdiff_t stop, charpos, bytepos;
7632 if (STRINGP (it->string))
7634 eassert (!it->s);
7635 stop = SCHARS (it->string);
7636 if (stop > it->end_charpos)
7637 stop = it->end_charpos;
7638 charpos = IT_STRING_CHARPOS (*it);
7639 bytepos = IT_STRING_BYTEPOS (*it);
7641 else
7643 stop = it->end_charpos;
7644 charpos = IT_CHARPOS (*it);
7645 bytepos = IT_BYTEPOS (*it);
7647 if (it->bidi_it.scan_dir < 0)
7648 stop = -1;
7649 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7650 it->string);
7654 /* Load IT with the next display element from Lisp string IT->string.
7655 IT->current.string_pos is the current position within the string.
7656 If IT->current.overlay_string_index >= 0, the Lisp string is an
7657 overlay string. */
7659 static int
7660 next_element_from_string (struct it *it)
7662 struct text_pos position;
7664 eassert (STRINGP (it->string));
7665 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7666 eassert (IT_STRING_CHARPOS (*it) >= 0);
7667 position = it->current.string_pos;
7669 /* With bidi reordering, the character to display might not be the
7670 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7671 that we were reseat()ed to a new string, whose paragraph
7672 direction is not known. */
7673 if (it->bidi_p && it->bidi_it.first_elt)
7675 get_visually_first_element (it);
7676 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7679 /* Time to check for invisible text? */
7680 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7682 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7684 if (!(!it->bidi_p
7685 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7686 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7688 /* With bidi non-linear iteration, we could find
7689 ourselves far beyond the last computed stop_charpos,
7690 with several other stop positions in between that we
7691 missed. Scan them all now, in buffer's logical
7692 order, until we find and handle the last stop_charpos
7693 that precedes our current position. */
7694 handle_stop_backwards (it, it->stop_charpos);
7695 return GET_NEXT_DISPLAY_ELEMENT (it);
7697 else
7699 if (it->bidi_p)
7701 /* Take note of the stop position we just moved
7702 across, for when we will move back across it. */
7703 it->prev_stop = it->stop_charpos;
7704 /* If we are at base paragraph embedding level, take
7705 note of the last stop position seen at this
7706 level. */
7707 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7708 it->base_level_stop = it->stop_charpos;
7710 handle_stop (it);
7712 /* Since a handler may have changed IT->method, we must
7713 recurse here. */
7714 return GET_NEXT_DISPLAY_ELEMENT (it);
7717 else if (it->bidi_p
7718 /* If we are before prev_stop, we may have overstepped
7719 on our way backwards a stop_pos, and if so, we need
7720 to handle that stop_pos. */
7721 && IT_STRING_CHARPOS (*it) < it->prev_stop
7722 /* We can sometimes back up for reasons that have nothing
7723 to do with bidi reordering. E.g., compositions. The
7724 code below is only needed when we are above the base
7725 embedding level, so test for that explicitly. */
7726 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7728 /* If we lost track of base_level_stop, we have no better
7729 place for handle_stop_backwards to start from than string
7730 beginning. This happens, e.g., when we were reseated to
7731 the previous screenful of text by vertical-motion. */
7732 if (it->base_level_stop <= 0
7733 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7734 it->base_level_stop = 0;
7735 handle_stop_backwards (it, it->base_level_stop);
7736 return GET_NEXT_DISPLAY_ELEMENT (it);
7740 if (it->current.overlay_string_index >= 0)
7742 /* Get the next character from an overlay string. In overlay
7743 strings, there is no field width or padding with spaces to
7744 do. */
7745 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7747 it->what = IT_EOB;
7748 return 0;
7750 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7751 IT_STRING_BYTEPOS (*it),
7752 it->bidi_it.scan_dir < 0
7753 ? -1
7754 : SCHARS (it->string))
7755 && next_element_from_composition (it))
7757 return 1;
7759 else if (STRING_MULTIBYTE (it->string))
7761 const unsigned char *s = (SDATA (it->string)
7762 + IT_STRING_BYTEPOS (*it));
7763 it->c = string_char_and_length (s, &it->len);
7765 else
7767 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7768 it->len = 1;
7771 else
7773 /* Get the next character from a Lisp string that is not an
7774 overlay string. Such strings come from the mode line, for
7775 example. We may have to pad with spaces, or truncate the
7776 string. See also next_element_from_c_string. */
7777 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7779 it->what = IT_EOB;
7780 return 0;
7782 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7784 /* Pad with spaces. */
7785 it->c = ' ', it->len = 1;
7786 CHARPOS (position) = BYTEPOS (position) = -1;
7788 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7789 IT_STRING_BYTEPOS (*it),
7790 it->bidi_it.scan_dir < 0
7791 ? -1
7792 : it->string_nchars)
7793 && next_element_from_composition (it))
7795 return 1;
7797 else if (STRING_MULTIBYTE (it->string))
7799 const unsigned char *s = (SDATA (it->string)
7800 + IT_STRING_BYTEPOS (*it));
7801 it->c = string_char_and_length (s, &it->len);
7803 else
7805 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7806 it->len = 1;
7810 /* Record what we have and where it came from. */
7811 it->what = IT_CHARACTER;
7812 it->object = it->string;
7813 it->position = position;
7814 return 1;
7818 /* Load IT with next display element from C string IT->s.
7819 IT->string_nchars is the maximum number of characters to return
7820 from the string. IT->end_charpos may be greater than
7821 IT->string_nchars when this function is called, in which case we
7822 may have to return padding spaces. Value is zero if end of string
7823 reached, including padding spaces. */
7825 static int
7826 next_element_from_c_string (struct it *it)
7828 int success_p = 1;
7830 eassert (it->s);
7831 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7832 it->what = IT_CHARACTER;
7833 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7834 it->object = Qnil;
7836 /* With bidi reordering, the character to display might not be the
7837 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7838 we were reseated to a new string, whose paragraph direction is
7839 not known. */
7840 if (it->bidi_p && it->bidi_it.first_elt)
7841 get_visually_first_element (it);
7843 /* IT's position can be greater than IT->string_nchars in case a
7844 field width or precision has been specified when the iterator was
7845 initialized. */
7846 if (IT_CHARPOS (*it) >= it->end_charpos)
7848 /* End of the game. */
7849 it->what = IT_EOB;
7850 success_p = 0;
7852 else if (IT_CHARPOS (*it) >= it->string_nchars)
7854 /* Pad with spaces. */
7855 it->c = ' ', it->len = 1;
7856 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7858 else if (it->multibyte_p)
7859 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7860 else
7861 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7863 return success_p;
7867 /* Set up IT to return characters from an ellipsis, if appropriate.
7868 The definition of the ellipsis glyphs may come from a display table
7869 entry. This function fills IT with the first glyph from the
7870 ellipsis if an ellipsis is to be displayed. */
7872 static int
7873 next_element_from_ellipsis (struct it *it)
7875 if (it->selective_display_ellipsis_p)
7876 setup_for_ellipsis (it, it->len);
7877 else
7879 /* The face at the current position may be different from the
7880 face we find after the invisible text. Remember what it
7881 was in IT->saved_face_id, and signal that it's there by
7882 setting face_before_selective_p. */
7883 it->saved_face_id = it->face_id;
7884 it->method = GET_FROM_BUFFER;
7885 it->object = it->w->contents;
7886 reseat_at_next_visible_line_start (it, 1);
7887 it->face_before_selective_p = 1;
7890 return GET_NEXT_DISPLAY_ELEMENT (it);
7894 /* Deliver an image display element. The iterator IT is already
7895 filled with image information (done in handle_display_prop). Value
7896 is always 1. */
7899 static int
7900 next_element_from_image (struct it *it)
7902 it->what = IT_IMAGE;
7903 it->ignore_overlay_strings_at_pos_p = 0;
7904 return 1;
7908 /* Fill iterator IT with next display element from a stretch glyph
7909 property. IT->object is the value of the text property. Value is
7910 always 1. */
7912 static int
7913 next_element_from_stretch (struct it *it)
7915 it->what = IT_STRETCH;
7916 return 1;
7919 /* Scan backwards from IT's current position until we find a stop
7920 position, or until BEGV. This is called when we find ourself
7921 before both the last known prev_stop and base_level_stop while
7922 reordering bidirectional text. */
7924 static void
7925 compute_stop_pos_backwards (struct it *it)
7927 const int SCAN_BACK_LIMIT = 1000;
7928 struct text_pos pos;
7929 struct display_pos save_current = it->current;
7930 struct text_pos save_position = it->position;
7931 ptrdiff_t charpos = IT_CHARPOS (*it);
7932 ptrdiff_t where_we_are = charpos;
7933 ptrdiff_t save_stop_pos = it->stop_charpos;
7934 ptrdiff_t save_end_pos = it->end_charpos;
7936 eassert (NILP (it->string) && !it->s);
7937 eassert (it->bidi_p);
7938 it->bidi_p = 0;
7941 it->end_charpos = min (charpos + 1, ZV);
7942 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7943 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7944 reseat_1 (it, pos, 0);
7945 compute_stop_pos (it);
7946 /* We must advance forward, right? */
7947 if (it->stop_charpos <= charpos)
7948 emacs_abort ();
7950 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7952 if (it->stop_charpos <= where_we_are)
7953 it->prev_stop = it->stop_charpos;
7954 else
7955 it->prev_stop = BEGV;
7956 it->bidi_p = 1;
7957 it->current = save_current;
7958 it->position = save_position;
7959 it->stop_charpos = save_stop_pos;
7960 it->end_charpos = save_end_pos;
7963 /* Scan forward from CHARPOS in the current buffer/string, until we
7964 find a stop position > current IT's position. Then handle the stop
7965 position before that. This is called when we bump into a stop
7966 position while reordering bidirectional text. CHARPOS should be
7967 the last previously processed stop_pos (or BEGV/0, if none were
7968 processed yet) whose position is less that IT's current
7969 position. */
7971 static void
7972 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7974 int bufp = !STRINGP (it->string);
7975 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7976 struct display_pos save_current = it->current;
7977 struct text_pos save_position = it->position;
7978 struct text_pos pos1;
7979 ptrdiff_t next_stop;
7981 /* Scan in strict logical order. */
7982 eassert (it->bidi_p);
7983 it->bidi_p = 0;
7986 it->prev_stop = charpos;
7987 if (bufp)
7989 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7990 reseat_1 (it, pos1, 0);
7992 else
7993 it->current.string_pos = string_pos (charpos, it->string);
7994 compute_stop_pos (it);
7995 /* We must advance forward, right? */
7996 if (it->stop_charpos <= it->prev_stop)
7997 emacs_abort ();
7998 charpos = it->stop_charpos;
8000 while (charpos <= where_we_are);
8002 it->bidi_p = 1;
8003 it->current = save_current;
8004 it->position = save_position;
8005 next_stop = it->stop_charpos;
8006 it->stop_charpos = it->prev_stop;
8007 handle_stop (it);
8008 it->stop_charpos = next_stop;
8011 /* Load IT with the next display element from current_buffer. Value
8012 is zero if end of buffer reached. IT->stop_charpos is the next
8013 position at which to stop and check for text properties or buffer
8014 end. */
8016 static int
8017 next_element_from_buffer (struct it *it)
8019 int success_p = 1;
8021 eassert (IT_CHARPOS (*it) >= BEGV);
8022 eassert (NILP (it->string) && !it->s);
8023 eassert (!it->bidi_p
8024 || (EQ (it->bidi_it.string.lstring, Qnil)
8025 && it->bidi_it.string.s == NULL));
8027 /* With bidi reordering, the character to display might not be the
8028 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8029 we were reseat()ed to a new buffer position, which is potentially
8030 a different paragraph. */
8031 if (it->bidi_p && it->bidi_it.first_elt)
8033 get_visually_first_element (it);
8034 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8037 if (IT_CHARPOS (*it) >= it->stop_charpos)
8039 if (IT_CHARPOS (*it) >= it->end_charpos)
8041 int overlay_strings_follow_p;
8043 /* End of the game, except when overlay strings follow that
8044 haven't been returned yet. */
8045 if (it->overlay_strings_at_end_processed_p)
8046 overlay_strings_follow_p = 0;
8047 else
8049 it->overlay_strings_at_end_processed_p = 1;
8050 overlay_strings_follow_p = get_overlay_strings (it, 0);
8053 if (overlay_strings_follow_p)
8054 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8055 else
8057 it->what = IT_EOB;
8058 it->position = it->current.pos;
8059 success_p = 0;
8062 else if (!(!it->bidi_p
8063 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8064 || IT_CHARPOS (*it) == it->stop_charpos))
8066 /* With bidi non-linear iteration, we could find ourselves
8067 far beyond the last computed stop_charpos, with several
8068 other stop positions in between that we missed. Scan
8069 them all now, in buffer's logical order, until we find
8070 and handle the last stop_charpos that precedes our
8071 current position. */
8072 handle_stop_backwards (it, it->stop_charpos);
8073 return GET_NEXT_DISPLAY_ELEMENT (it);
8075 else
8077 if (it->bidi_p)
8079 /* Take note of the stop position we just moved across,
8080 for when we will move back across it. */
8081 it->prev_stop = it->stop_charpos;
8082 /* If we are at base paragraph embedding level, take
8083 note of the last stop position seen at this
8084 level. */
8085 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8086 it->base_level_stop = it->stop_charpos;
8088 handle_stop (it);
8089 return GET_NEXT_DISPLAY_ELEMENT (it);
8092 else if (it->bidi_p
8093 /* If we are before prev_stop, we may have overstepped on
8094 our way backwards a stop_pos, and if so, we need to
8095 handle that stop_pos. */
8096 && IT_CHARPOS (*it) < it->prev_stop
8097 /* We can sometimes back up for reasons that have nothing
8098 to do with bidi reordering. E.g., compositions. The
8099 code below is only needed when we are above the base
8100 embedding level, so test for that explicitly. */
8101 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8103 if (it->base_level_stop <= 0
8104 || IT_CHARPOS (*it) < it->base_level_stop)
8106 /* If we lost track of base_level_stop, we need to find
8107 prev_stop by looking backwards. This happens, e.g., when
8108 we were reseated to the previous screenful of text by
8109 vertical-motion. */
8110 it->base_level_stop = BEGV;
8111 compute_stop_pos_backwards (it);
8112 handle_stop_backwards (it, it->prev_stop);
8114 else
8115 handle_stop_backwards (it, it->base_level_stop);
8116 return GET_NEXT_DISPLAY_ELEMENT (it);
8118 else
8120 /* No face changes, overlays etc. in sight, so just return a
8121 character from current_buffer. */
8122 unsigned char *p;
8123 ptrdiff_t stop;
8125 /* Maybe run the redisplay end trigger hook. Performance note:
8126 This doesn't seem to cost measurable time. */
8127 if (it->redisplay_end_trigger_charpos
8128 && it->glyph_row
8129 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8130 run_redisplay_end_trigger_hook (it);
8132 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8133 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8134 stop)
8135 && next_element_from_composition (it))
8137 return 1;
8140 /* Get the next character, maybe multibyte. */
8141 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8142 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8143 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8144 else
8145 it->c = *p, it->len = 1;
8147 /* Record what we have and where it came from. */
8148 it->what = IT_CHARACTER;
8149 it->object = it->w->contents;
8150 it->position = it->current.pos;
8152 /* Normally we return the character found above, except when we
8153 really want to return an ellipsis for selective display. */
8154 if (it->selective)
8156 if (it->c == '\n')
8158 /* A value of selective > 0 means hide lines indented more
8159 than that number of columns. */
8160 if (it->selective > 0
8161 && IT_CHARPOS (*it) + 1 < ZV
8162 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8163 IT_BYTEPOS (*it) + 1,
8164 it->selective))
8166 success_p = next_element_from_ellipsis (it);
8167 it->dpvec_char_len = -1;
8170 else if (it->c == '\r' && it->selective == -1)
8172 /* A value of selective == -1 means that everything from the
8173 CR to the end of the line is invisible, with maybe an
8174 ellipsis displayed for it. */
8175 success_p = next_element_from_ellipsis (it);
8176 it->dpvec_char_len = -1;
8181 /* Value is zero if end of buffer reached. */
8182 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8183 return success_p;
8187 /* Run the redisplay end trigger hook for IT. */
8189 static void
8190 run_redisplay_end_trigger_hook (struct it *it)
8192 Lisp_Object args[3];
8194 /* IT->glyph_row should be non-null, i.e. we should be actually
8195 displaying something, or otherwise we should not run the hook. */
8196 eassert (it->glyph_row);
8198 /* Set up hook arguments. */
8199 args[0] = Qredisplay_end_trigger_functions;
8200 args[1] = it->window;
8201 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8202 it->redisplay_end_trigger_charpos = 0;
8204 /* Since we are *trying* to run these functions, don't try to run
8205 them again, even if they get an error. */
8206 wset_redisplay_end_trigger (it->w, Qnil);
8207 Frun_hook_with_args (3, args);
8209 /* Notice if it changed the face of the character we are on. */
8210 handle_face_prop (it);
8214 /* Deliver a composition display element. Unlike the other
8215 next_element_from_XXX, this function is not registered in the array
8216 get_next_element[]. It is called from next_element_from_buffer and
8217 next_element_from_string when necessary. */
8219 static int
8220 next_element_from_composition (struct it *it)
8222 it->what = IT_COMPOSITION;
8223 it->len = it->cmp_it.nbytes;
8224 if (STRINGP (it->string))
8226 if (it->c < 0)
8228 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8229 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8230 return 0;
8232 it->position = it->current.string_pos;
8233 it->object = it->string;
8234 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8235 IT_STRING_BYTEPOS (*it), it->string);
8237 else
8239 if (it->c < 0)
8241 IT_CHARPOS (*it) += it->cmp_it.nchars;
8242 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8243 if (it->bidi_p)
8245 if (it->bidi_it.new_paragraph)
8246 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8247 /* Resync the bidi iterator with IT's new position.
8248 FIXME: this doesn't support bidirectional text. */
8249 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8250 bidi_move_to_visually_next (&it->bidi_it);
8252 return 0;
8254 it->position = it->current.pos;
8255 it->object = it->w->contents;
8256 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8257 IT_BYTEPOS (*it), Qnil);
8259 return 1;
8264 /***********************************************************************
8265 Moving an iterator without producing glyphs
8266 ***********************************************************************/
8268 /* Check if iterator is at a position corresponding to a valid buffer
8269 position after some move_it_ call. */
8271 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8272 ((it)->method == GET_FROM_STRING \
8273 ? IT_STRING_CHARPOS (*it) == 0 \
8274 : 1)
8277 /* Move iterator IT to a specified buffer or X position within one
8278 line on the display without producing glyphs.
8280 OP should be a bit mask including some or all of these bits:
8281 MOVE_TO_X: Stop upon reaching x-position TO_X.
8282 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8283 Regardless of OP's value, stop upon reaching the end of the display line.
8285 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8286 This means, in particular, that TO_X includes window's horizontal
8287 scroll amount.
8289 The return value has several possible values that
8290 say what condition caused the scan to stop:
8292 MOVE_POS_MATCH_OR_ZV
8293 - when TO_POS or ZV was reached.
8295 MOVE_X_REACHED
8296 -when TO_X was reached before TO_POS or ZV were reached.
8298 MOVE_LINE_CONTINUED
8299 - when we reached the end of the display area and the line must
8300 be continued.
8302 MOVE_LINE_TRUNCATED
8303 - when we reached the end of the display area and the line is
8304 truncated.
8306 MOVE_NEWLINE_OR_CR
8307 - when we stopped at a line end, i.e. a newline or a CR and selective
8308 display is on. */
8310 static enum move_it_result
8311 move_it_in_display_line_to (struct it *it,
8312 ptrdiff_t to_charpos, int to_x,
8313 enum move_operation_enum op)
8315 enum move_it_result result = MOVE_UNDEFINED;
8316 struct glyph_row *saved_glyph_row;
8317 struct it wrap_it, atpos_it, atx_it, ppos_it;
8318 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8319 void *ppos_data = NULL;
8320 int may_wrap = 0;
8321 enum it_method prev_method = it->method;
8322 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8323 int saw_smaller_pos = prev_pos < to_charpos;
8325 /* Don't produce glyphs in produce_glyphs. */
8326 saved_glyph_row = it->glyph_row;
8327 it->glyph_row = NULL;
8329 /* Use wrap_it to save a copy of IT wherever a word wrap could
8330 occur. Use atpos_it to save a copy of IT at the desired buffer
8331 position, if found, so that we can scan ahead and check if the
8332 word later overshoots the window edge. Use atx_it similarly, for
8333 pixel positions. */
8334 wrap_it.sp = -1;
8335 atpos_it.sp = -1;
8336 atx_it.sp = -1;
8338 /* Use ppos_it under bidi reordering to save a copy of IT for the
8339 position > CHARPOS that is the closest to CHARPOS. We restore
8340 that position in IT when we have scanned the entire display line
8341 without finding a match for CHARPOS and all the character
8342 positions are greater than CHARPOS. */
8343 if (it->bidi_p)
8345 SAVE_IT (ppos_it, *it, ppos_data);
8346 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8347 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8348 SAVE_IT (ppos_it, *it, ppos_data);
8351 #define BUFFER_POS_REACHED_P() \
8352 ((op & MOVE_TO_POS) != 0 \
8353 && BUFFERP (it->object) \
8354 && (IT_CHARPOS (*it) == to_charpos \
8355 || ((!it->bidi_p \
8356 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8357 && IT_CHARPOS (*it) > to_charpos) \
8358 || (it->what == IT_COMPOSITION \
8359 && ((IT_CHARPOS (*it) > to_charpos \
8360 && to_charpos >= it->cmp_it.charpos) \
8361 || (IT_CHARPOS (*it) < to_charpos \
8362 && to_charpos <= it->cmp_it.charpos)))) \
8363 && (it->method == GET_FROM_BUFFER \
8364 || (it->method == GET_FROM_DISPLAY_VECTOR \
8365 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8367 /* If there's a line-/wrap-prefix, handle it. */
8368 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8369 && it->current_y < it->last_visible_y)
8370 handle_line_prefix (it);
8372 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8373 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8375 while (1)
8377 int x, i, ascent = 0, descent = 0;
8379 /* Utility macro to reset an iterator with x, ascent, and descent. */
8380 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8381 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8382 (IT)->max_descent = descent)
8384 /* Stop if we move beyond TO_CHARPOS (after an image or a
8385 display string or stretch glyph). */
8386 if ((op & MOVE_TO_POS) != 0
8387 && BUFFERP (it->object)
8388 && it->method == GET_FROM_BUFFER
8389 && (((!it->bidi_p
8390 /* When the iterator is at base embedding level, we
8391 are guaranteed that characters are delivered for
8392 display in strictly increasing order of their
8393 buffer positions. */
8394 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8395 && IT_CHARPOS (*it) > to_charpos)
8396 || (it->bidi_p
8397 && (prev_method == GET_FROM_IMAGE
8398 || prev_method == GET_FROM_STRETCH
8399 || prev_method == GET_FROM_STRING)
8400 /* Passed TO_CHARPOS from left to right. */
8401 && ((prev_pos < to_charpos
8402 && IT_CHARPOS (*it) > to_charpos)
8403 /* Passed TO_CHARPOS from right to left. */
8404 || (prev_pos > to_charpos
8405 && IT_CHARPOS (*it) < to_charpos)))))
8407 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8409 result = MOVE_POS_MATCH_OR_ZV;
8410 break;
8412 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8413 /* If wrap_it is valid, the current position might be in a
8414 word that is wrapped. So, save the iterator in
8415 atpos_it and continue to see if wrapping happens. */
8416 SAVE_IT (atpos_it, *it, atpos_data);
8419 /* Stop when ZV reached.
8420 We used to stop here when TO_CHARPOS reached as well, but that is
8421 too soon if this glyph does not fit on this line. So we handle it
8422 explicitly below. */
8423 if (!get_next_display_element (it))
8425 result = MOVE_POS_MATCH_OR_ZV;
8426 break;
8429 if (it->line_wrap == TRUNCATE)
8431 if (BUFFER_POS_REACHED_P ())
8433 result = MOVE_POS_MATCH_OR_ZV;
8434 break;
8437 else
8439 if (it->line_wrap == WORD_WRAP)
8441 if (IT_DISPLAYING_WHITESPACE (it))
8442 may_wrap = 1;
8443 else if (may_wrap)
8445 /* We have reached a glyph that follows one or more
8446 whitespace characters. If the position is
8447 already found, we are done. */
8448 if (atpos_it.sp >= 0)
8450 RESTORE_IT (it, &atpos_it, atpos_data);
8451 result = MOVE_POS_MATCH_OR_ZV;
8452 goto done;
8454 if (atx_it.sp >= 0)
8456 RESTORE_IT (it, &atx_it, atx_data);
8457 result = MOVE_X_REACHED;
8458 goto done;
8460 /* Otherwise, we can wrap here. */
8461 SAVE_IT (wrap_it, *it, wrap_data);
8462 may_wrap = 0;
8467 /* Remember the line height for the current line, in case
8468 the next element doesn't fit on the line. */
8469 ascent = it->max_ascent;
8470 descent = it->max_descent;
8472 /* The call to produce_glyphs will get the metrics of the
8473 display element IT is loaded with. Record the x-position
8474 before this display element, in case it doesn't fit on the
8475 line. */
8476 x = it->current_x;
8478 PRODUCE_GLYPHS (it);
8480 if (it->area != TEXT_AREA)
8482 prev_method = it->method;
8483 if (it->method == GET_FROM_BUFFER)
8484 prev_pos = IT_CHARPOS (*it);
8485 set_iterator_to_next (it, 1);
8486 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8487 SET_TEXT_POS (this_line_min_pos,
8488 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8489 if (it->bidi_p
8490 && (op & MOVE_TO_POS)
8491 && IT_CHARPOS (*it) > to_charpos
8492 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8493 SAVE_IT (ppos_it, *it, ppos_data);
8494 continue;
8497 /* The number of glyphs we get back in IT->nglyphs will normally
8498 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8499 character on a terminal frame, or (iii) a line end. For the
8500 second case, IT->nglyphs - 1 padding glyphs will be present.
8501 (On X frames, there is only one glyph produced for a
8502 composite character.)
8504 The behavior implemented below means, for continuation lines,
8505 that as many spaces of a TAB as fit on the current line are
8506 displayed there. For terminal frames, as many glyphs of a
8507 multi-glyph character are displayed in the current line, too.
8508 This is what the old redisplay code did, and we keep it that
8509 way. Under X, the whole shape of a complex character must
8510 fit on the line or it will be completely displayed in the
8511 next line.
8513 Note that both for tabs and padding glyphs, all glyphs have
8514 the same width. */
8515 if (it->nglyphs)
8517 /* More than one glyph or glyph doesn't fit on line. All
8518 glyphs have the same width. */
8519 int single_glyph_width = it->pixel_width / it->nglyphs;
8520 int new_x;
8521 int x_before_this_char = x;
8522 int hpos_before_this_char = it->hpos;
8524 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8526 new_x = x + single_glyph_width;
8528 /* We want to leave anything reaching TO_X to the caller. */
8529 if ((op & MOVE_TO_X) && new_x > to_x)
8531 if (BUFFER_POS_REACHED_P ())
8533 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8534 goto buffer_pos_reached;
8535 if (atpos_it.sp < 0)
8537 SAVE_IT (atpos_it, *it, atpos_data);
8538 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8541 else
8543 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8545 it->current_x = x;
8546 result = MOVE_X_REACHED;
8547 break;
8549 if (atx_it.sp < 0)
8551 SAVE_IT (atx_it, *it, atx_data);
8552 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8557 if (/* Lines are continued. */
8558 it->line_wrap != TRUNCATE
8559 && (/* And glyph doesn't fit on the line. */
8560 new_x > it->last_visible_x
8561 /* Or it fits exactly and we're on a window
8562 system frame. */
8563 || (new_x == it->last_visible_x
8564 && FRAME_WINDOW_P (it->f)
8565 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8566 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8567 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8569 if (/* IT->hpos == 0 means the very first glyph
8570 doesn't fit on the line, e.g. a wide image. */
8571 it->hpos == 0
8572 || (new_x == it->last_visible_x
8573 && FRAME_WINDOW_P (it->f)))
8575 ++it->hpos;
8576 it->current_x = new_x;
8578 /* The character's last glyph just barely fits
8579 in this row. */
8580 if (i == it->nglyphs - 1)
8582 /* If this is the destination position,
8583 return a position *before* it in this row,
8584 now that we know it fits in this row. */
8585 if (BUFFER_POS_REACHED_P ())
8587 if (it->line_wrap != WORD_WRAP
8588 || wrap_it.sp < 0)
8590 it->hpos = hpos_before_this_char;
8591 it->current_x = x_before_this_char;
8592 result = MOVE_POS_MATCH_OR_ZV;
8593 break;
8595 if (it->line_wrap == WORD_WRAP
8596 && atpos_it.sp < 0)
8598 SAVE_IT (atpos_it, *it, atpos_data);
8599 atpos_it.current_x = x_before_this_char;
8600 atpos_it.hpos = hpos_before_this_char;
8604 prev_method = it->method;
8605 if (it->method == GET_FROM_BUFFER)
8606 prev_pos = IT_CHARPOS (*it);
8607 set_iterator_to_next (it, 1);
8608 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8609 SET_TEXT_POS (this_line_min_pos,
8610 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8611 /* On graphical terminals, newlines may
8612 "overflow" into the fringe if
8613 overflow-newline-into-fringe is non-nil.
8614 On text terminals, and on graphical
8615 terminals with no right margin, newlines
8616 may overflow into the last glyph on the
8617 display line.*/
8618 if (!FRAME_WINDOW_P (it->f)
8619 || ((it->bidi_p
8620 && it->bidi_it.paragraph_dir == R2L)
8621 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8622 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8623 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8625 if (!get_next_display_element (it))
8627 result = MOVE_POS_MATCH_OR_ZV;
8628 break;
8630 if (BUFFER_POS_REACHED_P ())
8632 if (ITERATOR_AT_END_OF_LINE_P (it))
8633 result = MOVE_POS_MATCH_OR_ZV;
8634 else
8635 result = MOVE_LINE_CONTINUED;
8636 break;
8638 if (ITERATOR_AT_END_OF_LINE_P (it)
8639 && (it->line_wrap != WORD_WRAP
8640 || wrap_it.sp < 0))
8642 result = MOVE_NEWLINE_OR_CR;
8643 break;
8648 else
8649 IT_RESET_X_ASCENT_DESCENT (it);
8651 if (wrap_it.sp >= 0)
8653 RESTORE_IT (it, &wrap_it, wrap_data);
8654 atpos_it.sp = -1;
8655 atx_it.sp = -1;
8658 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8659 IT_CHARPOS (*it)));
8660 result = MOVE_LINE_CONTINUED;
8661 break;
8664 if (BUFFER_POS_REACHED_P ())
8666 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8667 goto buffer_pos_reached;
8668 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8670 SAVE_IT (atpos_it, *it, atpos_data);
8671 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8675 if (new_x > it->first_visible_x)
8677 /* Glyph is visible. Increment number of glyphs that
8678 would be displayed. */
8679 ++it->hpos;
8683 if (result != MOVE_UNDEFINED)
8684 break;
8686 else if (BUFFER_POS_REACHED_P ())
8688 buffer_pos_reached:
8689 IT_RESET_X_ASCENT_DESCENT (it);
8690 result = MOVE_POS_MATCH_OR_ZV;
8691 break;
8693 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8695 /* Stop when TO_X specified and reached. This check is
8696 necessary here because of lines consisting of a line end,
8697 only. The line end will not produce any glyphs and we
8698 would never get MOVE_X_REACHED. */
8699 eassert (it->nglyphs == 0);
8700 result = MOVE_X_REACHED;
8701 break;
8704 /* Is this a line end? If yes, we're done. */
8705 if (ITERATOR_AT_END_OF_LINE_P (it))
8707 /* If we are past TO_CHARPOS, but never saw any character
8708 positions smaller than TO_CHARPOS, return
8709 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8710 did. */
8711 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8713 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8715 if (IT_CHARPOS (ppos_it) < ZV)
8717 RESTORE_IT (it, &ppos_it, ppos_data);
8718 result = MOVE_POS_MATCH_OR_ZV;
8720 else
8721 goto buffer_pos_reached;
8723 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8724 && IT_CHARPOS (*it) > to_charpos)
8725 goto buffer_pos_reached;
8726 else
8727 result = MOVE_NEWLINE_OR_CR;
8729 else
8730 result = MOVE_NEWLINE_OR_CR;
8731 break;
8734 prev_method = it->method;
8735 if (it->method == GET_FROM_BUFFER)
8736 prev_pos = IT_CHARPOS (*it);
8737 /* The current display element has been consumed. Advance
8738 to the next. */
8739 set_iterator_to_next (it, 1);
8740 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8741 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8742 if (IT_CHARPOS (*it) < to_charpos)
8743 saw_smaller_pos = 1;
8744 if (it->bidi_p
8745 && (op & MOVE_TO_POS)
8746 && IT_CHARPOS (*it) >= to_charpos
8747 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8748 SAVE_IT (ppos_it, *it, ppos_data);
8750 /* Stop if lines are truncated and IT's current x-position is
8751 past the right edge of the window now. */
8752 if (it->line_wrap == TRUNCATE
8753 && it->current_x >= it->last_visible_x)
8755 if (!FRAME_WINDOW_P (it->f)
8756 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8757 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8758 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8759 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8761 int at_eob_p = 0;
8763 if ((at_eob_p = !get_next_display_element (it))
8764 || BUFFER_POS_REACHED_P ()
8765 /* If we are past TO_CHARPOS, but never saw any
8766 character positions smaller than TO_CHARPOS,
8767 return MOVE_POS_MATCH_OR_ZV, like the
8768 unidirectional display did. */
8769 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8770 && !saw_smaller_pos
8771 && IT_CHARPOS (*it) > to_charpos))
8773 if (it->bidi_p
8774 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8775 RESTORE_IT (it, &ppos_it, ppos_data);
8776 result = MOVE_POS_MATCH_OR_ZV;
8777 break;
8779 if (ITERATOR_AT_END_OF_LINE_P (it))
8781 result = MOVE_NEWLINE_OR_CR;
8782 break;
8785 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8786 && !saw_smaller_pos
8787 && IT_CHARPOS (*it) > to_charpos)
8789 if (IT_CHARPOS (ppos_it) < ZV)
8790 RESTORE_IT (it, &ppos_it, ppos_data);
8791 result = MOVE_POS_MATCH_OR_ZV;
8792 break;
8794 result = MOVE_LINE_TRUNCATED;
8795 break;
8797 #undef IT_RESET_X_ASCENT_DESCENT
8800 #undef BUFFER_POS_REACHED_P
8802 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8803 restore the saved iterator. */
8804 if (atpos_it.sp >= 0)
8805 RESTORE_IT (it, &atpos_it, atpos_data);
8806 else if (atx_it.sp >= 0)
8807 RESTORE_IT (it, &atx_it, atx_data);
8809 done:
8811 if (atpos_data)
8812 bidi_unshelve_cache (atpos_data, 1);
8813 if (atx_data)
8814 bidi_unshelve_cache (atx_data, 1);
8815 if (wrap_data)
8816 bidi_unshelve_cache (wrap_data, 1);
8817 if (ppos_data)
8818 bidi_unshelve_cache (ppos_data, 1);
8820 /* Restore the iterator settings altered at the beginning of this
8821 function. */
8822 it->glyph_row = saved_glyph_row;
8823 return result;
8826 /* For external use. */
8827 void
8828 move_it_in_display_line (struct it *it,
8829 ptrdiff_t to_charpos, int to_x,
8830 enum move_operation_enum op)
8832 if (it->line_wrap == WORD_WRAP
8833 && (op & MOVE_TO_X))
8835 struct it save_it;
8836 void *save_data = NULL;
8837 int skip;
8839 SAVE_IT (save_it, *it, save_data);
8840 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8841 /* When word-wrap is on, TO_X may lie past the end
8842 of a wrapped line. Then it->current is the
8843 character on the next line, so backtrack to the
8844 space before the wrap point. */
8845 if (skip == MOVE_LINE_CONTINUED)
8847 int prev_x = max (it->current_x - 1, 0);
8848 RESTORE_IT (it, &save_it, save_data);
8849 move_it_in_display_line_to
8850 (it, -1, prev_x, MOVE_TO_X);
8852 else
8853 bidi_unshelve_cache (save_data, 1);
8855 else
8856 move_it_in_display_line_to (it, to_charpos, to_x, op);
8860 /* Move IT forward until it satisfies one or more of the criteria in
8861 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8863 OP is a bit-mask that specifies where to stop, and in particular,
8864 which of those four position arguments makes a difference. See the
8865 description of enum move_operation_enum.
8867 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8868 screen line, this function will set IT to the next position that is
8869 displayed to the right of TO_CHARPOS on the screen. */
8871 void
8872 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8874 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8875 int line_height, line_start_x = 0, reached = 0;
8876 void *backup_data = NULL;
8878 for (;;)
8880 if (op & MOVE_TO_VPOS)
8882 /* If no TO_CHARPOS and no TO_X specified, stop at the
8883 start of the line TO_VPOS. */
8884 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8886 if (it->vpos == to_vpos)
8888 reached = 1;
8889 break;
8891 else
8892 skip = move_it_in_display_line_to (it, -1, -1, 0);
8894 else
8896 /* TO_VPOS >= 0 means stop at TO_X in the line at
8897 TO_VPOS, or at TO_POS, whichever comes first. */
8898 if (it->vpos == to_vpos)
8900 reached = 2;
8901 break;
8904 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8906 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8908 reached = 3;
8909 break;
8911 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8913 /* We have reached TO_X but not in the line we want. */
8914 skip = move_it_in_display_line_to (it, to_charpos,
8915 -1, MOVE_TO_POS);
8916 if (skip == MOVE_POS_MATCH_OR_ZV)
8918 reached = 4;
8919 break;
8924 else if (op & MOVE_TO_Y)
8926 struct it it_backup;
8928 if (it->line_wrap == WORD_WRAP)
8929 SAVE_IT (it_backup, *it, backup_data);
8931 /* TO_Y specified means stop at TO_X in the line containing
8932 TO_Y---or at TO_CHARPOS if this is reached first. The
8933 problem is that we can't really tell whether the line
8934 contains TO_Y before we have completely scanned it, and
8935 this may skip past TO_X. What we do is to first scan to
8936 TO_X.
8938 If TO_X is not specified, use a TO_X of zero. The reason
8939 is to make the outcome of this function more predictable.
8940 If we didn't use TO_X == 0, we would stop at the end of
8941 the line which is probably not what a caller would expect
8942 to happen. */
8943 skip = move_it_in_display_line_to
8944 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8945 (MOVE_TO_X | (op & MOVE_TO_POS)));
8947 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8948 if (skip == MOVE_POS_MATCH_OR_ZV)
8949 reached = 5;
8950 else if (skip == MOVE_X_REACHED)
8952 /* If TO_X was reached, we want to know whether TO_Y is
8953 in the line. We know this is the case if the already
8954 scanned glyphs make the line tall enough. Otherwise,
8955 we must check by scanning the rest of the line. */
8956 line_height = it->max_ascent + it->max_descent;
8957 if (to_y >= it->current_y
8958 && to_y < it->current_y + line_height)
8960 reached = 6;
8961 break;
8963 SAVE_IT (it_backup, *it, backup_data);
8964 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8965 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8966 op & MOVE_TO_POS);
8967 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8968 line_height = it->max_ascent + it->max_descent;
8969 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8971 if (to_y >= it->current_y
8972 && to_y < it->current_y + line_height)
8974 /* If TO_Y is in this line and TO_X was reached
8975 above, we scanned too far. We have to restore
8976 IT's settings to the ones before skipping. But
8977 keep the more accurate values of max_ascent and
8978 max_descent we've found while skipping the rest
8979 of the line, for the sake of callers, such as
8980 pos_visible_p, that need to know the line
8981 height. */
8982 int max_ascent = it->max_ascent;
8983 int max_descent = it->max_descent;
8985 RESTORE_IT (it, &it_backup, backup_data);
8986 it->max_ascent = max_ascent;
8987 it->max_descent = max_descent;
8988 reached = 6;
8990 else
8992 skip = skip2;
8993 if (skip == MOVE_POS_MATCH_OR_ZV)
8994 reached = 7;
8997 else
8999 /* Check whether TO_Y is in this line. */
9000 line_height = it->max_ascent + it->max_descent;
9001 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9003 if (to_y >= it->current_y
9004 && to_y < it->current_y + line_height)
9006 /* When word-wrap is on, TO_X may lie past the end
9007 of a wrapped line. Then it->current is the
9008 character on the next line, so backtrack to the
9009 space before the wrap point. */
9010 if (skip == MOVE_LINE_CONTINUED
9011 && it->line_wrap == WORD_WRAP)
9013 int prev_x = max (it->current_x - 1, 0);
9014 RESTORE_IT (it, &it_backup, backup_data);
9015 skip = move_it_in_display_line_to
9016 (it, -1, prev_x, MOVE_TO_X);
9018 reached = 6;
9022 if (reached)
9023 break;
9025 else if (BUFFERP (it->object)
9026 && (it->method == GET_FROM_BUFFER
9027 || it->method == GET_FROM_STRETCH)
9028 && IT_CHARPOS (*it) >= to_charpos
9029 /* Under bidi iteration, a call to set_iterator_to_next
9030 can scan far beyond to_charpos if the initial
9031 portion of the next line needs to be reordered. In
9032 that case, give move_it_in_display_line_to another
9033 chance below. */
9034 && !(it->bidi_p
9035 && it->bidi_it.scan_dir == -1))
9036 skip = MOVE_POS_MATCH_OR_ZV;
9037 else
9038 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9040 switch (skip)
9042 case MOVE_POS_MATCH_OR_ZV:
9043 reached = 8;
9044 goto out;
9046 case MOVE_NEWLINE_OR_CR:
9047 set_iterator_to_next (it, 1);
9048 it->continuation_lines_width = 0;
9049 break;
9051 case MOVE_LINE_TRUNCATED:
9052 it->continuation_lines_width = 0;
9053 reseat_at_next_visible_line_start (it, 0);
9054 if ((op & MOVE_TO_POS) != 0
9055 && IT_CHARPOS (*it) > to_charpos)
9057 reached = 9;
9058 goto out;
9060 break;
9062 case MOVE_LINE_CONTINUED:
9063 /* For continued lines ending in a tab, some of the glyphs
9064 associated with the tab are displayed on the current
9065 line. Since it->current_x does not include these glyphs,
9066 we use it->last_visible_x instead. */
9067 if (it->c == '\t')
9069 it->continuation_lines_width += it->last_visible_x;
9070 /* When moving by vpos, ensure that the iterator really
9071 advances to the next line (bug#847, bug#969). Fixme:
9072 do we need to do this in other circumstances? */
9073 if (it->current_x != it->last_visible_x
9074 && (op & MOVE_TO_VPOS)
9075 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9077 line_start_x = it->current_x + it->pixel_width
9078 - it->last_visible_x;
9079 set_iterator_to_next (it, 0);
9082 else
9083 it->continuation_lines_width += it->current_x;
9084 break;
9086 default:
9087 emacs_abort ();
9090 /* Reset/increment for the next run. */
9091 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9092 it->current_x = line_start_x;
9093 line_start_x = 0;
9094 it->hpos = 0;
9095 it->current_y += it->max_ascent + it->max_descent;
9096 ++it->vpos;
9097 last_height = it->max_ascent + it->max_descent;
9098 it->max_ascent = it->max_descent = 0;
9101 out:
9103 /* On text terminals, we may stop at the end of a line in the middle
9104 of a multi-character glyph. If the glyph itself is continued,
9105 i.e. it is actually displayed on the next line, don't treat this
9106 stopping point as valid; move to the next line instead (unless
9107 that brings us offscreen). */
9108 if (!FRAME_WINDOW_P (it->f)
9109 && op & MOVE_TO_POS
9110 && IT_CHARPOS (*it) == to_charpos
9111 && it->what == IT_CHARACTER
9112 && it->nglyphs > 1
9113 && it->line_wrap == WINDOW_WRAP
9114 && it->current_x == it->last_visible_x - 1
9115 && it->c != '\n'
9116 && it->c != '\t'
9117 && it->vpos < it->w->window_end_vpos)
9119 it->continuation_lines_width += it->current_x;
9120 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9121 it->current_y += it->max_ascent + it->max_descent;
9122 ++it->vpos;
9123 last_height = it->max_ascent + it->max_descent;
9126 if (backup_data)
9127 bidi_unshelve_cache (backup_data, 1);
9129 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9133 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9135 If DY > 0, move IT backward at least that many pixels. DY = 0
9136 means move IT backward to the preceding line start or BEGV. This
9137 function may move over more than DY pixels if IT->current_y - DY
9138 ends up in the middle of a line; in this case IT->current_y will be
9139 set to the top of the line moved to. */
9141 void
9142 move_it_vertically_backward (struct it *it, int dy)
9144 int nlines, h;
9145 struct it it2, it3;
9146 void *it2data = NULL, *it3data = NULL;
9147 ptrdiff_t start_pos;
9148 int nchars_per_row
9149 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9150 ptrdiff_t pos_limit;
9152 move_further_back:
9153 eassert (dy >= 0);
9155 start_pos = IT_CHARPOS (*it);
9157 /* Estimate how many newlines we must move back. */
9158 nlines = max (1, dy / default_line_pixel_height (it->w));
9159 if (it->line_wrap == TRUNCATE)
9160 pos_limit = BEGV;
9161 else
9162 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9164 /* Set the iterator's position that many lines back. But don't go
9165 back more than NLINES full screen lines -- this wins a day with
9166 buffers which have very long lines. */
9167 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9168 back_to_previous_visible_line_start (it);
9170 /* Reseat the iterator here. When moving backward, we don't want
9171 reseat to skip forward over invisible text, set up the iterator
9172 to deliver from overlay strings at the new position etc. So,
9173 use reseat_1 here. */
9174 reseat_1 (it, it->current.pos, 1);
9176 /* We are now surely at a line start. */
9177 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9178 reordering is in effect. */
9179 it->continuation_lines_width = 0;
9181 /* Move forward and see what y-distance we moved. First move to the
9182 start of the next line so that we get its height. We need this
9183 height to be able to tell whether we reached the specified
9184 y-distance. */
9185 SAVE_IT (it2, *it, it2data);
9186 it2.max_ascent = it2.max_descent = 0;
9189 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9190 MOVE_TO_POS | MOVE_TO_VPOS);
9192 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9193 /* If we are in a display string which starts at START_POS,
9194 and that display string includes a newline, and we are
9195 right after that newline (i.e. at the beginning of a
9196 display line), exit the loop, because otherwise we will
9197 infloop, since move_it_to will see that it is already at
9198 START_POS and will not move. */
9199 || (it2.method == GET_FROM_STRING
9200 && IT_CHARPOS (it2) == start_pos
9201 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9202 eassert (IT_CHARPOS (*it) >= BEGV);
9203 SAVE_IT (it3, it2, it3data);
9205 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9206 eassert (IT_CHARPOS (*it) >= BEGV);
9207 /* H is the actual vertical distance from the position in *IT
9208 and the starting position. */
9209 h = it2.current_y - it->current_y;
9210 /* NLINES is the distance in number of lines. */
9211 nlines = it2.vpos - it->vpos;
9213 /* Correct IT's y and vpos position
9214 so that they are relative to the starting point. */
9215 it->vpos -= nlines;
9216 it->current_y -= h;
9218 if (dy == 0)
9220 /* DY == 0 means move to the start of the screen line. The
9221 value of nlines is > 0 if continuation lines were involved,
9222 or if the original IT position was at start of a line. */
9223 RESTORE_IT (it, it, it2data);
9224 if (nlines > 0)
9225 move_it_by_lines (it, nlines);
9226 /* The above code moves us to some position NLINES down,
9227 usually to its first glyph (leftmost in an L2R line), but
9228 that's not necessarily the start of the line, under bidi
9229 reordering. We want to get to the character position
9230 that is immediately after the newline of the previous
9231 line. */
9232 if (it->bidi_p
9233 && !it->continuation_lines_width
9234 && !STRINGP (it->string)
9235 && IT_CHARPOS (*it) > BEGV
9236 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9238 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9240 DEC_BOTH (cp, bp);
9241 cp = find_newline_no_quit (cp, bp, -1, NULL);
9242 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9244 bidi_unshelve_cache (it3data, 1);
9246 else
9248 /* The y-position we try to reach, relative to *IT.
9249 Note that H has been subtracted in front of the if-statement. */
9250 int target_y = it->current_y + h - dy;
9251 int y0 = it3.current_y;
9252 int y1;
9253 int line_height;
9255 RESTORE_IT (&it3, &it3, it3data);
9256 y1 = line_bottom_y (&it3);
9257 line_height = y1 - y0;
9258 RESTORE_IT (it, it, it2data);
9259 /* If we did not reach target_y, try to move further backward if
9260 we can. If we moved too far backward, try to move forward. */
9261 if (target_y < it->current_y
9262 /* This is heuristic. In a window that's 3 lines high, with
9263 a line height of 13 pixels each, recentering with point
9264 on the bottom line will try to move -39/2 = 19 pixels
9265 backward. Try to avoid moving into the first line. */
9266 && (it->current_y - target_y
9267 > min (window_box_height (it->w), line_height * 2 / 3))
9268 && IT_CHARPOS (*it) > BEGV)
9270 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9271 target_y - it->current_y));
9272 dy = it->current_y - target_y;
9273 goto move_further_back;
9275 else if (target_y >= it->current_y + line_height
9276 && IT_CHARPOS (*it) < ZV)
9278 /* Should move forward by at least one line, maybe more.
9280 Note: Calling move_it_by_lines can be expensive on
9281 terminal frames, where compute_motion is used (via
9282 vmotion) to do the job, when there are very long lines
9283 and truncate-lines is nil. That's the reason for
9284 treating terminal frames specially here. */
9286 if (!FRAME_WINDOW_P (it->f))
9287 move_it_vertically (it, target_y - (it->current_y + line_height));
9288 else
9292 move_it_by_lines (it, 1);
9294 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9301 /* Move IT by a specified amount of pixel lines DY. DY negative means
9302 move backwards. DY = 0 means move to start of screen line. At the
9303 end, IT will be on the start of a screen line. */
9305 void
9306 move_it_vertically (struct it *it, int dy)
9308 if (dy <= 0)
9309 move_it_vertically_backward (it, -dy);
9310 else
9312 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9313 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9314 MOVE_TO_POS | MOVE_TO_Y);
9315 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9317 /* If buffer ends in ZV without a newline, move to the start of
9318 the line to satisfy the post-condition. */
9319 if (IT_CHARPOS (*it) == ZV
9320 && ZV > BEGV
9321 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9322 move_it_by_lines (it, 0);
9327 /* Move iterator IT past the end of the text line it is in. */
9329 void
9330 move_it_past_eol (struct it *it)
9332 enum move_it_result rc;
9334 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9335 if (rc == MOVE_NEWLINE_OR_CR)
9336 set_iterator_to_next (it, 0);
9340 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9341 negative means move up. DVPOS == 0 means move to the start of the
9342 screen line.
9344 Optimization idea: If we would know that IT->f doesn't use
9345 a face with proportional font, we could be faster for
9346 truncate-lines nil. */
9348 void
9349 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9352 /* The commented-out optimization uses vmotion on terminals. This
9353 gives bad results, because elements like it->what, on which
9354 callers such as pos_visible_p rely, aren't updated. */
9355 /* struct position pos;
9356 if (!FRAME_WINDOW_P (it->f))
9358 struct text_pos textpos;
9360 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9361 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9362 reseat (it, textpos, 1);
9363 it->vpos += pos.vpos;
9364 it->current_y += pos.vpos;
9366 else */
9368 if (dvpos == 0)
9370 /* DVPOS == 0 means move to the start of the screen line. */
9371 move_it_vertically_backward (it, 0);
9372 /* Let next call to line_bottom_y calculate real line height */
9373 last_height = 0;
9375 else if (dvpos > 0)
9377 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9378 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9380 /* Only move to the next buffer position if we ended up in a
9381 string from display property, not in an overlay string
9382 (before-string or after-string). That is because the
9383 latter don't conceal the underlying buffer position, so
9384 we can ask to move the iterator to the exact position we
9385 are interested in. Note that, even if we are already at
9386 IT_CHARPOS (*it), the call below is not a no-op, as it
9387 will detect that we are at the end of the string, pop the
9388 iterator, and compute it->current_x and it->hpos
9389 correctly. */
9390 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9391 -1, -1, -1, MOVE_TO_POS);
9394 else
9396 struct it it2;
9397 void *it2data = NULL;
9398 ptrdiff_t start_charpos, i;
9399 int nchars_per_row
9400 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9401 ptrdiff_t pos_limit;
9403 /* Start at the beginning of the screen line containing IT's
9404 position. This may actually move vertically backwards,
9405 in case of overlays, so adjust dvpos accordingly. */
9406 dvpos += it->vpos;
9407 move_it_vertically_backward (it, 0);
9408 dvpos -= it->vpos;
9410 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9411 screen lines, and reseat the iterator there. */
9412 start_charpos = IT_CHARPOS (*it);
9413 if (it->line_wrap == TRUNCATE)
9414 pos_limit = BEGV;
9415 else
9416 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9417 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9418 back_to_previous_visible_line_start (it);
9419 reseat (it, it->current.pos, 1);
9421 /* Move further back if we end up in a string or an image. */
9422 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9424 /* First try to move to start of display line. */
9425 dvpos += it->vpos;
9426 move_it_vertically_backward (it, 0);
9427 dvpos -= it->vpos;
9428 if (IT_POS_VALID_AFTER_MOVE_P (it))
9429 break;
9430 /* If start of line is still in string or image,
9431 move further back. */
9432 back_to_previous_visible_line_start (it);
9433 reseat (it, it->current.pos, 1);
9434 dvpos--;
9437 it->current_x = it->hpos = 0;
9439 /* Above call may have moved too far if continuation lines
9440 are involved. Scan forward and see if it did. */
9441 SAVE_IT (it2, *it, it2data);
9442 it2.vpos = it2.current_y = 0;
9443 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9444 it->vpos -= it2.vpos;
9445 it->current_y -= it2.current_y;
9446 it->current_x = it->hpos = 0;
9448 /* If we moved too far back, move IT some lines forward. */
9449 if (it2.vpos > -dvpos)
9451 int delta = it2.vpos + dvpos;
9453 RESTORE_IT (&it2, &it2, it2data);
9454 SAVE_IT (it2, *it, it2data);
9455 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9456 /* Move back again if we got too far ahead. */
9457 if (IT_CHARPOS (*it) >= start_charpos)
9458 RESTORE_IT (it, &it2, it2data);
9459 else
9460 bidi_unshelve_cache (it2data, 1);
9462 else
9463 RESTORE_IT (it, it, it2data);
9467 /* Return 1 if IT points into the middle of a display vector. */
9470 in_display_vector_p (struct it *it)
9472 return (it->method == GET_FROM_DISPLAY_VECTOR
9473 && it->current.dpvec_index > 0
9474 && it->dpvec + it->current.dpvec_index != it->dpend);
9478 /***********************************************************************
9479 Messages
9480 ***********************************************************************/
9483 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9484 to *Messages*. */
9486 void
9487 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9489 Lisp_Object args[3];
9490 Lisp_Object msg, fmt;
9491 char *buffer;
9492 ptrdiff_t len;
9493 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9494 USE_SAFE_ALLOCA;
9496 fmt = msg = Qnil;
9497 GCPRO4 (fmt, msg, arg1, arg2);
9499 args[0] = fmt = build_string (format);
9500 args[1] = arg1;
9501 args[2] = arg2;
9502 msg = Fformat (3, args);
9504 len = SBYTES (msg) + 1;
9505 buffer = SAFE_ALLOCA (len);
9506 memcpy (buffer, SDATA (msg), len);
9508 message_dolog (buffer, len - 1, 1, 0);
9509 SAFE_FREE ();
9511 UNGCPRO;
9515 /* Output a newline in the *Messages* buffer if "needs" one. */
9517 void
9518 message_log_maybe_newline (void)
9520 if (message_log_need_newline)
9521 message_dolog ("", 0, 1, 0);
9525 /* Add a string M of length NBYTES to the message log, optionally
9526 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9527 true, means interpret the contents of M as multibyte. This
9528 function calls low-level routines in order to bypass text property
9529 hooks, etc. which might not be safe to run.
9531 This may GC (insert may run before/after change hooks),
9532 so the buffer M must NOT point to a Lisp string. */
9534 void
9535 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9537 const unsigned char *msg = (const unsigned char *) m;
9539 if (!NILP (Vmemory_full))
9540 return;
9542 if (!NILP (Vmessage_log_max))
9544 struct buffer *oldbuf;
9545 Lisp_Object oldpoint, oldbegv, oldzv;
9546 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9547 ptrdiff_t point_at_end = 0;
9548 ptrdiff_t zv_at_end = 0;
9549 Lisp_Object old_deactivate_mark;
9550 bool shown;
9551 struct gcpro gcpro1;
9553 old_deactivate_mark = Vdeactivate_mark;
9554 oldbuf = current_buffer;
9555 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9556 bset_undo_list (current_buffer, Qt);
9558 oldpoint = message_dolog_marker1;
9559 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9560 oldbegv = message_dolog_marker2;
9561 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9562 oldzv = message_dolog_marker3;
9563 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9564 GCPRO1 (old_deactivate_mark);
9566 if (PT == Z)
9567 point_at_end = 1;
9568 if (ZV == Z)
9569 zv_at_end = 1;
9571 BEGV = BEG;
9572 BEGV_BYTE = BEG_BYTE;
9573 ZV = Z;
9574 ZV_BYTE = Z_BYTE;
9575 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9577 /* Insert the string--maybe converting multibyte to single byte
9578 or vice versa, so that all the text fits the buffer. */
9579 if (multibyte
9580 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9582 ptrdiff_t i;
9583 int c, char_bytes;
9584 char work[1];
9586 /* Convert a multibyte string to single-byte
9587 for the *Message* buffer. */
9588 for (i = 0; i < nbytes; i += char_bytes)
9590 c = string_char_and_length (msg + i, &char_bytes);
9591 work[0] = (ASCII_CHAR_P (c)
9593 : multibyte_char_to_unibyte (c));
9594 insert_1_both (work, 1, 1, 1, 0, 0);
9597 else if (! multibyte
9598 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9600 ptrdiff_t i;
9601 int c, char_bytes;
9602 unsigned char str[MAX_MULTIBYTE_LENGTH];
9603 /* Convert a single-byte string to multibyte
9604 for the *Message* buffer. */
9605 for (i = 0; i < nbytes; i++)
9607 c = msg[i];
9608 MAKE_CHAR_MULTIBYTE (c);
9609 char_bytes = CHAR_STRING (c, str);
9610 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9613 else if (nbytes)
9614 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9616 if (nlflag)
9618 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9619 printmax_t dups;
9621 insert_1_both ("\n", 1, 1, 1, 0, 0);
9623 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9624 this_bol = PT;
9625 this_bol_byte = PT_BYTE;
9627 /* See if this line duplicates the previous one.
9628 If so, combine duplicates. */
9629 if (this_bol > BEG)
9631 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9632 prev_bol = PT;
9633 prev_bol_byte = PT_BYTE;
9635 dups = message_log_check_duplicate (prev_bol_byte,
9636 this_bol_byte);
9637 if (dups)
9639 del_range_both (prev_bol, prev_bol_byte,
9640 this_bol, this_bol_byte, 0);
9641 if (dups > 1)
9643 char dupstr[sizeof " [ times]"
9644 + INT_STRLEN_BOUND (printmax_t)];
9646 /* If you change this format, don't forget to also
9647 change message_log_check_duplicate. */
9648 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9649 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9650 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9655 /* If we have more than the desired maximum number of lines
9656 in the *Messages* buffer now, delete the oldest ones.
9657 This is safe because we don't have undo in this buffer. */
9659 if (NATNUMP (Vmessage_log_max))
9661 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9662 -XFASTINT (Vmessage_log_max) - 1, 0);
9663 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9666 BEGV = marker_position (oldbegv);
9667 BEGV_BYTE = marker_byte_position (oldbegv);
9669 if (zv_at_end)
9671 ZV = Z;
9672 ZV_BYTE = Z_BYTE;
9674 else
9676 ZV = marker_position (oldzv);
9677 ZV_BYTE = marker_byte_position (oldzv);
9680 if (point_at_end)
9681 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9682 else
9683 /* We can't do Fgoto_char (oldpoint) because it will run some
9684 Lisp code. */
9685 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9686 marker_byte_position (oldpoint));
9688 UNGCPRO;
9689 unchain_marker (XMARKER (oldpoint));
9690 unchain_marker (XMARKER (oldbegv));
9691 unchain_marker (XMARKER (oldzv));
9693 shown = buffer_window_count (current_buffer) > 0;
9694 set_buffer_internal (oldbuf);
9695 /* We called insert_1_both above with its 5th argument (PREPARE)
9696 zero, which prevents insert_1_both from calling
9697 prepare_to_modify_buffer, which in turns prevents us from
9698 incrementing windows_or_buffers_changed even if *Messages* is
9699 shown in some window. So we must manually incrementing
9700 windows_or_buffers_changed here to make up for that. */
9701 if (shown)
9702 windows_or_buffers_changed++;
9703 else
9704 windows_or_buffers_changed = old_windows_or_buffers_changed;
9705 message_log_need_newline = !nlflag;
9706 Vdeactivate_mark = old_deactivate_mark;
9711 /* We are at the end of the buffer after just having inserted a newline.
9712 (Note: We depend on the fact we won't be crossing the gap.)
9713 Check to see if the most recent message looks a lot like the previous one.
9714 Return 0 if different, 1 if the new one should just replace it, or a
9715 value N > 1 if we should also append " [N times]". */
9717 static intmax_t
9718 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9720 ptrdiff_t i;
9721 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9722 int seen_dots = 0;
9723 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9724 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9726 for (i = 0; i < len; i++)
9728 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9729 seen_dots = 1;
9730 if (p1[i] != p2[i])
9731 return seen_dots;
9733 p1 += len;
9734 if (*p1 == '\n')
9735 return 2;
9736 if (*p1++ == ' ' && *p1++ == '[')
9738 char *pend;
9739 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9740 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9741 return n + 1;
9743 return 0;
9747 /* Display an echo area message M with a specified length of NBYTES
9748 bytes. The string may include null characters. If M is not a
9749 string, clear out any existing message, and let the mini-buffer
9750 text show through.
9752 This function cancels echoing. */
9754 void
9755 message3 (Lisp_Object m)
9757 struct gcpro gcpro1;
9759 GCPRO1 (m);
9760 clear_message (1,1);
9761 cancel_echoing ();
9763 /* First flush out any partial line written with print. */
9764 message_log_maybe_newline ();
9765 if (STRINGP (m))
9767 ptrdiff_t nbytes = SBYTES (m);
9768 bool multibyte = STRING_MULTIBYTE (m);
9769 USE_SAFE_ALLOCA;
9770 char *buffer = SAFE_ALLOCA (nbytes);
9771 memcpy (buffer, SDATA (m), nbytes);
9772 message_dolog (buffer, nbytes, 1, multibyte);
9773 SAFE_FREE ();
9775 message3_nolog (m);
9777 UNGCPRO;
9781 /* The non-logging version of message3.
9782 This does not cancel echoing, because it is used for echoing.
9783 Perhaps we need to make a separate function for echoing
9784 and make this cancel echoing. */
9786 void
9787 message3_nolog (Lisp_Object m)
9789 struct frame *sf = SELECTED_FRAME ();
9791 if (FRAME_INITIAL_P (sf))
9793 if (noninteractive_need_newline)
9794 putc ('\n', stderr);
9795 noninteractive_need_newline = 0;
9796 if (STRINGP (m))
9797 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9798 if (cursor_in_echo_area == 0)
9799 fprintf (stderr, "\n");
9800 fflush (stderr);
9802 /* Error messages get reported properly by cmd_error, so this must be just an
9803 informative message; if the frame hasn't really been initialized yet, just
9804 toss it. */
9805 else if (INTERACTIVE && sf->glyphs_initialized_p)
9807 /* Get the frame containing the mini-buffer
9808 that the selected frame is using. */
9809 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9810 Lisp_Object frame = XWINDOW (mini_window)->frame;
9811 struct frame *f = XFRAME (frame);
9813 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9814 Fmake_frame_visible (frame);
9816 if (STRINGP (m) && SCHARS (m) > 0)
9818 set_message (m);
9819 if (minibuffer_auto_raise)
9820 Fraise_frame (frame);
9821 /* Assume we are not echoing.
9822 (If we are, echo_now will override this.) */
9823 echo_message_buffer = Qnil;
9825 else
9826 clear_message (1, 1);
9828 do_pending_window_change (0);
9829 echo_area_display (1);
9830 do_pending_window_change (0);
9831 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9832 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9837 /* Display a null-terminated echo area message M. If M is 0, clear
9838 out any existing message, and let the mini-buffer text show through.
9840 The buffer M must continue to exist until after the echo area gets
9841 cleared or some other message gets displayed there. Do not pass
9842 text that is stored in a Lisp string. Do not pass text in a buffer
9843 that was alloca'd. */
9845 void
9846 message1 (const char *m)
9848 message3 (m ? build_unibyte_string (m) : Qnil);
9852 /* The non-logging counterpart of message1. */
9854 void
9855 message1_nolog (const char *m)
9857 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9860 /* Display a message M which contains a single %s
9861 which gets replaced with STRING. */
9863 void
9864 message_with_string (const char *m, Lisp_Object string, int log)
9866 CHECK_STRING (string);
9868 if (noninteractive)
9870 if (m)
9872 if (noninteractive_need_newline)
9873 putc ('\n', stderr);
9874 noninteractive_need_newline = 0;
9875 fprintf (stderr, m, SDATA (string));
9876 if (!cursor_in_echo_area)
9877 fprintf (stderr, "\n");
9878 fflush (stderr);
9881 else if (INTERACTIVE)
9883 /* The frame whose minibuffer we're going to display the message on.
9884 It may be larger than the selected frame, so we need
9885 to use its buffer, not the selected frame's buffer. */
9886 Lisp_Object mini_window;
9887 struct frame *f, *sf = SELECTED_FRAME ();
9889 /* Get the frame containing the minibuffer
9890 that the selected frame is using. */
9891 mini_window = FRAME_MINIBUF_WINDOW (sf);
9892 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9894 /* Error messages get reported properly by cmd_error, so this must be
9895 just an informative message; if the frame hasn't really been
9896 initialized yet, just toss it. */
9897 if (f->glyphs_initialized_p)
9899 Lisp_Object args[2], msg;
9900 struct gcpro gcpro1, gcpro2;
9902 args[0] = build_string (m);
9903 args[1] = msg = string;
9904 GCPRO2 (args[0], msg);
9905 gcpro1.nvars = 2;
9907 msg = Fformat (2, args);
9909 if (log)
9910 message3 (msg);
9911 else
9912 message3_nolog (msg);
9914 UNGCPRO;
9916 /* Print should start at the beginning of the message
9917 buffer next time. */
9918 message_buf_print = 0;
9924 /* Dump an informative message to the minibuf. If M is 0, clear out
9925 any existing message, and let the mini-buffer text show through. */
9927 static void
9928 vmessage (const char *m, va_list ap)
9930 if (noninteractive)
9932 if (m)
9934 if (noninteractive_need_newline)
9935 putc ('\n', stderr);
9936 noninteractive_need_newline = 0;
9937 vfprintf (stderr, m, ap);
9938 if (cursor_in_echo_area == 0)
9939 fprintf (stderr, "\n");
9940 fflush (stderr);
9943 else if (INTERACTIVE)
9945 /* The frame whose mini-buffer we're going to display the message
9946 on. It may be larger than the selected frame, so we need to
9947 use its buffer, not the selected frame's buffer. */
9948 Lisp_Object mini_window;
9949 struct frame *f, *sf = SELECTED_FRAME ();
9951 /* Get the frame containing the mini-buffer
9952 that the selected frame is using. */
9953 mini_window = FRAME_MINIBUF_WINDOW (sf);
9954 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9956 /* Error messages get reported properly by cmd_error, so this must be
9957 just an informative message; if the frame hasn't really been
9958 initialized yet, just toss it. */
9959 if (f->glyphs_initialized_p)
9961 if (m)
9963 ptrdiff_t len;
9964 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9965 char *message_buf = alloca (maxsize + 1);
9967 len = doprnt (message_buf, maxsize, m, 0, ap);
9969 message3 (make_string (message_buf, len));
9971 else
9972 message1 (0);
9974 /* Print should start at the beginning of the message
9975 buffer next time. */
9976 message_buf_print = 0;
9981 void
9982 message (const char *m, ...)
9984 va_list ap;
9985 va_start (ap, m);
9986 vmessage (m, ap);
9987 va_end (ap);
9991 #if 0
9992 /* The non-logging version of message. */
9994 void
9995 message_nolog (const char *m, ...)
9997 Lisp_Object old_log_max;
9998 va_list ap;
9999 va_start (ap, m);
10000 old_log_max = Vmessage_log_max;
10001 Vmessage_log_max = Qnil;
10002 vmessage (m, ap);
10003 Vmessage_log_max = old_log_max;
10004 va_end (ap);
10006 #endif
10009 /* Display the current message in the current mini-buffer. This is
10010 only called from error handlers in process.c, and is not time
10011 critical. */
10013 void
10014 update_echo_area (void)
10016 if (!NILP (echo_area_buffer[0]))
10018 Lisp_Object string;
10019 string = Fcurrent_message ();
10020 message3 (string);
10025 /* Make sure echo area buffers in `echo_buffers' are live.
10026 If they aren't, make new ones. */
10028 static void
10029 ensure_echo_area_buffers (void)
10031 int i;
10033 for (i = 0; i < 2; ++i)
10034 if (!BUFFERP (echo_buffer[i])
10035 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10037 char name[30];
10038 Lisp_Object old_buffer;
10039 int j;
10041 old_buffer = echo_buffer[i];
10042 echo_buffer[i] = Fget_buffer_create
10043 (make_formatted_string (name, " *Echo Area %d*", i));
10044 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10045 /* to force word wrap in echo area -
10046 it was decided to postpone this*/
10047 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10049 for (j = 0; j < 2; ++j)
10050 if (EQ (old_buffer, echo_area_buffer[j]))
10051 echo_area_buffer[j] = echo_buffer[i];
10056 /* Call FN with args A1..A2 with either the current or last displayed
10057 echo_area_buffer as current buffer.
10059 WHICH zero means use the current message buffer
10060 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10061 from echo_buffer[] and clear it.
10063 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10064 suitable buffer from echo_buffer[] and clear it.
10066 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10067 that the current message becomes the last displayed one, make
10068 choose a suitable buffer for echo_area_buffer[0], and clear it.
10070 Value is what FN returns. */
10072 static int
10073 with_echo_area_buffer (struct window *w, int which,
10074 int (*fn) (ptrdiff_t, Lisp_Object),
10075 ptrdiff_t a1, Lisp_Object a2)
10077 Lisp_Object buffer;
10078 int this_one, the_other, clear_buffer_p, rc;
10079 ptrdiff_t count = SPECPDL_INDEX ();
10081 /* If buffers aren't live, make new ones. */
10082 ensure_echo_area_buffers ();
10084 clear_buffer_p = 0;
10086 if (which == 0)
10087 this_one = 0, the_other = 1;
10088 else if (which > 0)
10089 this_one = 1, the_other = 0;
10090 else
10092 this_one = 0, the_other = 1;
10093 clear_buffer_p = 1;
10095 /* We need a fresh one in case the current echo buffer equals
10096 the one containing the last displayed echo area message. */
10097 if (!NILP (echo_area_buffer[this_one])
10098 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10099 echo_area_buffer[this_one] = Qnil;
10102 /* Choose a suitable buffer from echo_buffer[] is we don't
10103 have one. */
10104 if (NILP (echo_area_buffer[this_one]))
10106 echo_area_buffer[this_one]
10107 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10108 ? echo_buffer[the_other]
10109 : echo_buffer[this_one]);
10110 clear_buffer_p = 1;
10113 buffer = echo_area_buffer[this_one];
10115 /* Don't get confused by reusing the buffer used for echoing
10116 for a different purpose. */
10117 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10118 cancel_echoing ();
10120 record_unwind_protect (unwind_with_echo_area_buffer,
10121 with_echo_area_buffer_unwind_data (w));
10123 /* Make the echo area buffer current. Note that for display
10124 purposes, it is not necessary that the displayed window's buffer
10125 == current_buffer, except for text property lookup. So, let's
10126 only set that buffer temporarily here without doing a full
10127 Fset_window_buffer. We must also change w->pointm, though,
10128 because otherwise an assertions in unshow_buffer fails, and Emacs
10129 aborts. */
10130 set_buffer_internal_1 (XBUFFER (buffer));
10131 if (w)
10133 wset_buffer (w, buffer);
10134 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10137 bset_undo_list (current_buffer, Qt);
10138 bset_read_only (current_buffer, Qnil);
10139 specbind (Qinhibit_read_only, Qt);
10140 specbind (Qinhibit_modification_hooks, Qt);
10142 if (clear_buffer_p && Z > BEG)
10143 del_range (BEG, Z);
10145 eassert (BEGV >= BEG);
10146 eassert (ZV <= Z && ZV >= BEGV);
10148 rc = fn (a1, a2);
10150 eassert (BEGV >= BEG);
10151 eassert (ZV <= Z && ZV >= BEGV);
10153 unbind_to (count, Qnil);
10154 return rc;
10158 /* Save state that should be preserved around the call to the function
10159 FN called in with_echo_area_buffer. */
10161 static Lisp_Object
10162 with_echo_area_buffer_unwind_data (struct window *w)
10164 int i = 0;
10165 Lisp_Object vector, tmp;
10167 /* Reduce consing by keeping one vector in
10168 Vwith_echo_area_save_vector. */
10169 vector = Vwith_echo_area_save_vector;
10170 Vwith_echo_area_save_vector = Qnil;
10172 if (NILP (vector))
10173 vector = Fmake_vector (make_number (9), Qnil);
10175 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10176 ASET (vector, i, Vdeactivate_mark); ++i;
10177 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10179 if (w)
10181 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10182 ASET (vector, i, w->contents); ++i;
10183 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10184 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10185 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10186 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10188 else
10190 int end = i + 6;
10191 for (; i < end; ++i)
10192 ASET (vector, i, Qnil);
10195 eassert (i == ASIZE (vector));
10196 return vector;
10200 /* Restore global state from VECTOR which was created by
10201 with_echo_area_buffer_unwind_data. */
10203 static void
10204 unwind_with_echo_area_buffer (Lisp_Object vector)
10206 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10207 Vdeactivate_mark = AREF (vector, 1);
10208 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10210 if (WINDOWP (AREF (vector, 3)))
10212 struct window *w;
10213 Lisp_Object buffer;
10215 w = XWINDOW (AREF (vector, 3));
10216 buffer = AREF (vector, 4);
10218 wset_buffer (w, buffer);
10219 set_marker_both (w->pointm, buffer,
10220 XFASTINT (AREF (vector, 5)),
10221 XFASTINT (AREF (vector, 6)));
10222 set_marker_both (w->start, buffer,
10223 XFASTINT (AREF (vector, 7)),
10224 XFASTINT (AREF (vector, 8)));
10227 Vwith_echo_area_save_vector = vector;
10231 /* Set up the echo area for use by print functions. MULTIBYTE_P
10232 non-zero means we will print multibyte. */
10234 void
10235 setup_echo_area_for_printing (int multibyte_p)
10237 /* If we can't find an echo area any more, exit. */
10238 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10239 Fkill_emacs (Qnil);
10241 ensure_echo_area_buffers ();
10243 if (!message_buf_print)
10245 /* A message has been output since the last time we printed.
10246 Choose a fresh echo area buffer. */
10247 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10248 echo_area_buffer[0] = echo_buffer[1];
10249 else
10250 echo_area_buffer[0] = echo_buffer[0];
10252 /* Switch to that buffer and clear it. */
10253 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10254 bset_truncate_lines (current_buffer, Qnil);
10256 if (Z > BEG)
10258 ptrdiff_t count = SPECPDL_INDEX ();
10259 specbind (Qinhibit_read_only, Qt);
10260 /* Note that undo recording is always disabled. */
10261 del_range (BEG, Z);
10262 unbind_to (count, Qnil);
10264 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10266 /* Set up the buffer for the multibyteness we need. */
10267 if (multibyte_p
10268 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10269 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10271 /* Raise the frame containing the echo area. */
10272 if (minibuffer_auto_raise)
10274 struct frame *sf = SELECTED_FRAME ();
10275 Lisp_Object mini_window;
10276 mini_window = FRAME_MINIBUF_WINDOW (sf);
10277 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10280 message_log_maybe_newline ();
10281 message_buf_print = 1;
10283 else
10285 if (NILP (echo_area_buffer[0]))
10287 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10288 echo_area_buffer[0] = echo_buffer[1];
10289 else
10290 echo_area_buffer[0] = echo_buffer[0];
10293 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10295 /* Someone switched buffers between print requests. */
10296 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10297 bset_truncate_lines (current_buffer, Qnil);
10303 /* Display an echo area message in window W. Value is non-zero if W's
10304 height is changed. If display_last_displayed_message_p is
10305 non-zero, display the message that was last displayed, otherwise
10306 display the current message. */
10308 static int
10309 display_echo_area (struct window *w)
10311 int i, no_message_p, window_height_changed_p;
10313 /* Temporarily disable garbage collections while displaying the echo
10314 area. This is done because a GC can print a message itself.
10315 That message would modify the echo area buffer's contents while a
10316 redisplay of the buffer is going on, and seriously confuse
10317 redisplay. */
10318 ptrdiff_t count = inhibit_garbage_collection ();
10320 /* If there is no message, we must call display_echo_area_1
10321 nevertheless because it resizes the window. But we will have to
10322 reset the echo_area_buffer in question to nil at the end because
10323 with_echo_area_buffer will sets it to an empty buffer. */
10324 i = display_last_displayed_message_p ? 1 : 0;
10325 no_message_p = NILP (echo_area_buffer[i]);
10327 window_height_changed_p
10328 = with_echo_area_buffer (w, display_last_displayed_message_p,
10329 display_echo_area_1,
10330 (intptr_t) w, Qnil);
10332 if (no_message_p)
10333 echo_area_buffer[i] = Qnil;
10335 unbind_to (count, Qnil);
10336 return window_height_changed_p;
10340 /* Helper for display_echo_area. Display the current buffer which
10341 contains the current echo area message in window W, a mini-window,
10342 a pointer to which is passed in A1. A2..A4 are currently not used.
10343 Change the height of W so that all of the message is displayed.
10344 Value is non-zero if height of W was changed. */
10346 static int
10347 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10349 intptr_t i1 = a1;
10350 struct window *w = (struct window *) i1;
10351 Lisp_Object window;
10352 struct text_pos start;
10353 int window_height_changed_p = 0;
10355 /* Do this before displaying, so that we have a large enough glyph
10356 matrix for the display. If we can't get enough space for the
10357 whole text, display the last N lines. That works by setting w->start. */
10358 window_height_changed_p = resize_mini_window (w, 0);
10360 /* Use the starting position chosen by resize_mini_window. */
10361 SET_TEXT_POS_FROM_MARKER (start, w->start);
10363 /* Display. */
10364 clear_glyph_matrix (w->desired_matrix);
10365 XSETWINDOW (window, w);
10366 try_window (window, start, 0);
10368 return window_height_changed_p;
10372 /* Resize the echo area window to exactly the size needed for the
10373 currently displayed message, if there is one. If a mini-buffer
10374 is active, don't shrink it. */
10376 void
10377 resize_echo_area_exactly (void)
10379 if (BUFFERP (echo_area_buffer[0])
10380 && WINDOWP (echo_area_window))
10382 struct window *w = XWINDOW (echo_area_window);
10383 int resized_p;
10384 Lisp_Object resize_exactly;
10386 if (minibuf_level == 0)
10387 resize_exactly = Qt;
10388 else
10389 resize_exactly = Qnil;
10391 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10392 (intptr_t) w, resize_exactly);
10393 if (resized_p)
10395 ++windows_or_buffers_changed;
10396 ++update_mode_lines;
10397 redisplay_internal ();
10403 /* Callback function for with_echo_area_buffer, when used from
10404 resize_echo_area_exactly. A1 contains a pointer to the window to
10405 resize, EXACTLY non-nil means resize the mini-window exactly to the
10406 size of the text displayed. A3 and A4 are not used. Value is what
10407 resize_mini_window returns. */
10409 static int
10410 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10412 intptr_t i1 = a1;
10413 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10417 /* Resize mini-window W to fit the size of its contents. EXACT_P
10418 means size the window exactly to the size needed. Otherwise, it's
10419 only enlarged until W's buffer is empty.
10421 Set W->start to the right place to begin display. If the whole
10422 contents fit, start at the beginning. Otherwise, start so as
10423 to make the end of the contents appear. This is particularly
10424 important for y-or-n-p, but seems desirable generally.
10426 Value is non-zero if the window height has been changed. */
10429 resize_mini_window (struct window *w, int exact_p)
10431 struct frame *f = XFRAME (w->frame);
10432 int window_height_changed_p = 0;
10434 eassert (MINI_WINDOW_P (w));
10436 /* By default, start display at the beginning. */
10437 set_marker_both (w->start, w->contents,
10438 BUF_BEGV (XBUFFER (w->contents)),
10439 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10441 /* Don't resize windows while redisplaying a window; it would
10442 confuse redisplay functions when the size of the window they are
10443 displaying changes from under them. Such a resizing can happen,
10444 for instance, when which-func prints a long message while
10445 we are running fontification-functions. We're running these
10446 functions with safe_call which binds inhibit-redisplay to t. */
10447 if (!NILP (Vinhibit_redisplay))
10448 return 0;
10450 /* Nil means don't try to resize. */
10451 if (NILP (Vresize_mini_windows)
10452 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10453 return 0;
10455 if (!FRAME_MINIBUF_ONLY_P (f))
10457 struct it it;
10458 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10459 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10460 int height;
10461 EMACS_INT max_height;
10462 int unit = FRAME_LINE_HEIGHT (f);
10463 struct text_pos start;
10464 struct buffer *old_current_buffer = NULL;
10466 if (current_buffer != XBUFFER (w->contents))
10468 old_current_buffer = current_buffer;
10469 set_buffer_internal (XBUFFER (w->contents));
10472 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10474 /* Compute the max. number of lines specified by the user. */
10475 if (FLOATP (Vmax_mini_window_height))
10476 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10477 else if (INTEGERP (Vmax_mini_window_height))
10478 max_height = XINT (Vmax_mini_window_height);
10479 else
10480 max_height = total_height / 4;
10482 /* Correct that max. height if it's bogus. */
10483 max_height = clip_to_bounds (1, max_height, total_height);
10485 /* Find out the height of the text in the window. */
10486 if (it.line_wrap == TRUNCATE)
10487 height = 1;
10488 else
10490 last_height = 0;
10491 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10492 if (it.max_ascent == 0 && it.max_descent == 0)
10493 height = it.current_y + last_height;
10494 else
10495 height = it.current_y + it.max_ascent + it.max_descent;
10496 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10497 height = (height + unit - 1) / unit;
10500 /* Compute a suitable window start. */
10501 if (height > max_height)
10503 height = max_height;
10504 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10505 move_it_vertically_backward (&it, (height - 1) * unit);
10506 start = it.current.pos;
10508 else
10509 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10510 SET_MARKER_FROM_TEXT_POS (w->start, start);
10512 if (EQ (Vresize_mini_windows, Qgrow_only))
10514 /* Let it grow only, until we display an empty message, in which
10515 case the window shrinks again. */
10516 if (height > WINDOW_TOTAL_LINES (w))
10518 int old_height = WINDOW_TOTAL_LINES (w);
10520 FRAME_WINDOWS_FROZEN (f) = 1;
10521 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10522 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10524 else if (height < WINDOW_TOTAL_LINES (w)
10525 && (exact_p || BEGV == ZV))
10527 int old_height = WINDOW_TOTAL_LINES (w);
10529 FRAME_WINDOWS_FROZEN (f) = 0;
10530 shrink_mini_window (w);
10531 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10534 else
10536 /* Always resize to exact size needed. */
10537 if (height > WINDOW_TOTAL_LINES (w))
10539 int old_height = WINDOW_TOTAL_LINES (w);
10541 FRAME_WINDOWS_FROZEN (f) = 1;
10542 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10543 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10545 else if (height < WINDOW_TOTAL_LINES (w))
10547 int old_height = WINDOW_TOTAL_LINES (w);
10549 FRAME_WINDOWS_FROZEN (f) = 0;
10550 shrink_mini_window (w);
10552 if (height)
10554 FRAME_WINDOWS_FROZEN (f) = 1;
10555 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10558 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10562 if (old_current_buffer)
10563 set_buffer_internal (old_current_buffer);
10566 return window_height_changed_p;
10570 /* Value is the current message, a string, or nil if there is no
10571 current message. */
10573 Lisp_Object
10574 current_message (void)
10576 Lisp_Object msg;
10578 if (!BUFFERP (echo_area_buffer[0]))
10579 msg = Qnil;
10580 else
10582 with_echo_area_buffer (0, 0, current_message_1,
10583 (intptr_t) &msg, Qnil);
10584 if (NILP (msg))
10585 echo_area_buffer[0] = Qnil;
10588 return msg;
10592 static int
10593 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10595 intptr_t i1 = a1;
10596 Lisp_Object *msg = (Lisp_Object *) i1;
10598 if (Z > BEG)
10599 *msg = make_buffer_string (BEG, Z, 1);
10600 else
10601 *msg = Qnil;
10602 return 0;
10606 /* Push the current message on Vmessage_stack for later restoration
10607 by restore_message. Value is non-zero if the current message isn't
10608 empty. This is a relatively infrequent operation, so it's not
10609 worth optimizing. */
10611 bool
10612 push_message (void)
10614 Lisp_Object msg = current_message ();
10615 Vmessage_stack = Fcons (msg, Vmessage_stack);
10616 return STRINGP (msg);
10620 /* Restore message display from the top of Vmessage_stack. */
10622 void
10623 restore_message (void)
10625 eassert (CONSP (Vmessage_stack));
10626 message3_nolog (XCAR (Vmessage_stack));
10630 /* Handler for unwind-protect calling pop_message. */
10632 void
10633 pop_message_unwind (void)
10635 /* Pop the top-most entry off Vmessage_stack. */
10636 eassert (CONSP (Vmessage_stack));
10637 Vmessage_stack = XCDR (Vmessage_stack);
10641 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10642 exits. If the stack is not empty, we have a missing pop_message
10643 somewhere. */
10645 void
10646 check_message_stack (void)
10648 if (!NILP (Vmessage_stack))
10649 emacs_abort ();
10653 /* Truncate to NCHARS what will be displayed in the echo area the next
10654 time we display it---but don't redisplay it now. */
10656 void
10657 truncate_echo_area (ptrdiff_t nchars)
10659 if (nchars == 0)
10660 echo_area_buffer[0] = Qnil;
10661 else if (!noninteractive
10662 && INTERACTIVE
10663 && !NILP (echo_area_buffer[0]))
10665 struct frame *sf = SELECTED_FRAME ();
10666 /* Error messages get reported properly by cmd_error, so this must be
10667 just an informative message; if the frame hasn't really been
10668 initialized yet, just toss it. */
10669 if (sf->glyphs_initialized_p)
10670 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10675 /* Helper function for truncate_echo_area. Truncate the current
10676 message to at most NCHARS characters. */
10678 static int
10679 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10681 if (BEG + nchars < Z)
10682 del_range (BEG + nchars, Z);
10683 if (Z == BEG)
10684 echo_area_buffer[0] = Qnil;
10685 return 0;
10688 /* Set the current message to STRING. */
10690 static void
10691 set_message (Lisp_Object string)
10693 eassert (STRINGP (string));
10695 message_enable_multibyte = STRING_MULTIBYTE (string);
10697 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10698 message_buf_print = 0;
10699 help_echo_showing_p = 0;
10701 if (STRINGP (Vdebug_on_message)
10702 && STRINGP (string)
10703 && fast_string_match (Vdebug_on_message, string) >= 0)
10704 call_debugger (list2 (Qerror, string));
10708 /* Helper function for set_message. First argument is ignored and second
10709 argument has the same meaning as for set_message.
10710 This function is called with the echo area buffer being current. */
10712 static int
10713 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10715 eassert (STRINGP (string));
10717 /* Change multibyteness of the echo buffer appropriately. */
10718 if (message_enable_multibyte
10719 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10720 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10722 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10723 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10724 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10726 /* Insert new message at BEG. */
10727 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10729 /* This function takes care of single/multibyte conversion.
10730 We just have to ensure that the echo area buffer has the right
10731 setting of enable_multibyte_characters. */
10732 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10734 return 0;
10738 /* Clear messages. CURRENT_P non-zero means clear the current
10739 message. LAST_DISPLAYED_P non-zero means clear the message
10740 last displayed. */
10742 void
10743 clear_message (int current_p, int last_displayed_p)
10745 if (current_p)
10747 echo_area_buffer[0] = Qnil;
10748 message_cleared_p = 1;
10751 if (last_displayed_p)
10752 echo_area_buffer[1] = Qnil;
10754 message_buf_print = 0;
10757 /* Clear garbaged frames.
10759 This function is used where the old redisplay called
10760 redraw_garbaged_frames which in turn called redraw_frame which in
10761 turn called clear_frame. The call to clear_frame was a source of
10762 flickering. I believe a clear_frame is not necessary. It should
10763 suffice in the new redisplay to invalidate all current matrices,
10764 and ensure a complete redisplay of all windows. */
10766 static void
10767 clear_garbaged_frames (void)
10769 if (frame_garbaged)
10771 Lisp_Object tail, frame;
10772 int changed_count = 0;
10774 FOR_EACH_FRAME (tail, frame)
10776 struct frame *f = XFRAME (frame);
10778 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10780 if (f->resized_p)
10782 redraw_frame (f);
10783 f->force_flush_display_p = 1;
10785 clear_current_matrices (f);
10786 changed_count++;
10787 f->garbaged = 0;
10788 f->resized_p = 0;
10792 frame_garbaged = 0;
10793 if (changed_count)
10794 ++windows_or_buffers_changed;
10799 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10800 is non-zero update selected_frame. Value is non-zero if the
10801 mini-windows height has been changed. */
10803 static int
10804 echo_area_display (int update_frame_p)
10806 Lisp_Object mini_window;
10807 struct window *w;
10808 struct frame *f;
10809 int window_height_changed_p = 0;
10810 struct frame *sf = SELECTED_FRAME ();
10812 mini_window = FRAME_MINIBUF_WINDOW (sf);
10813 w = XWINDOW (mini_window);
10814 f = XFRAME (WINDOW_FRAME (w));
10816 /* Don't display if frame is invisible or not yet initialized. */
10817 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10818 return 0;
10820 #ifdef HAVE_WINDOW_SYSTEM
10821 /* When Emacs starts, selected_frame may be the initial terminal
10822 frame. If we let this through, a message would be displayed on
10823 the terminal. */
10824 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10825 return 0;
10826 #endif /* HAVE_WINDOW_SYSTEM */
10828 /* Redraw garbaged frames. */
10829 clear_garbaged_frames ();
10831 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10833 echo_area_window = mini_window;
10834 window_height_changed_p = display_echo_area (w);
10835 w->must_be_updated_p = 1;
10837 /* Update the display, unless called from redisplay_internal.
10838 Also don't update the screen during redisplay itself. The
10839 update will happen at the end of redisplay, and an update
10840 here could cause confusion. */
10841 if (update_frame_p && !redisplaying_p)
10843 int n = 0;
10845 /* If the display update has been interrupted by pending
10846 input, update mode lines in the frame. Due to the
10847 pending input, it might have been that redisplay hasn't
10848 been called, so that mode lines above the echo area are
10849 garbaged. This looks odd, so we prevent it here. */
10850 if (!display_completed)
10851 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10853 if (window_height_changed_p
10854 /* Don't do this if Emacs is shutting down. Redisplay
10855 needs to run hooks. */
10856 && !NILP (Vrun_hooks))
10858 /* Must update other windows. Likewise as in other
10859 cases, don't let this update be interrupted by
10860 pending input. */
10861 ptrdiff_t count = SPECPDL_INDEX ();
10862 specbind (Qredisplay_dont_pause, Qt);
10863 windows_or_buffers_changed = 1;
10864 redisplay_internal ();
10865 unbind_to (count, Qnil);
10867 else if (FRAME_WINDOW_P (f) && n == 0)
10869 /* Window configuration is the same as before.
10870 Can do with a display update of the echo area,
10871 unless we displayed some mode lines. */
10872 update_single_window (w, 1);
10873 FRAME_RIF (f)->flush_display (f);
10875 else
10876 update_frame (f, 1, 1);
10878 /* If cursor is in the echo area, make sure that the next
10879 redisplay displays the minibuffer, so that the cursor will
10880 be replaced with what the minibuffer wants. */
10881 if (cursor_in_echo_area)
10882 ++windows_or_buffers_changed;
10885 else if (!EQ (mini_window, selected_window))
10886 windows_or_buffers_changed++;
10888 /* Last displayed message is now the current message. */
10889 echo_area_buffer[1] = echo_area_buffer[0];
10890 /* Inform read_char that we're not echoing. */
10891 echo_message_buffer = Qnil;
10893 /* Prevent redisplay optimization in redisplay_internal by resetting
10894 this_line_start_pos. This is done because the mini-buffer now
10895 displays the message instead of its buffer text. */
10896 if (EQ (mini_window, selected_window))
10897 CHARPOS (this_line_start_pos) = 0;
10899 return window_height_changed_p;
10902 /* Nonzero if the current window's buffer is shown in more than one
10903 window and was modified since last redisplay. */
10905 static int
10906 buffer_shared_and_changed (void)
10908 return (buffer_window_count (current_buffer) > 1
10909 && UNCHANGED_MODIFIED < MODIFF);
10912 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10913 is enabled and mark of W's buffer was changed since last W's update. */
10915 static int
10916 window_buffer_changed (struct window *w)
10918 struct buffer *b = XBUFFER (w->contents);
10920 eassert (BUFFER_LIVE_P (b));
10922 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10923 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10924 != (w->region_showing != 0)));
10927 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10929 static int
10930 mode_line_update_needed (struct window *w)
10932 return (w->column_number_displayed != -1
10933 && !(PT == w->last_point && !window_outdated (w))
10934 && (w->column_number_displayed != current_column ()));
10937 /* Nonzero if window start of W is frozen and may not be changed during
10938 redisplay. */
10940 static bool
10941 window_frozen_p (struct window *w)
10943 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10945 Lisp_Object window;
10947 XSETWINDOW (window, w);
10948 if (MINI_WINDOW_P (w))
10949 return 0;
10950 else if (EQ (window, selected_window))
10951 return 0;
10952 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10953 && EQ (window, Vminibuf_scroll_window))
10954 /* This special window can't be frozen too. */
10955 return 0;
10956 else
10957 return 1;
10959 return 0;
10962 /***********************************************************************
10963 Mode Lines and Frame Titles
10964 ***********************************************************************/
10966 /* A buffer for constructing non-propertized mode-line strings and
10967 frame titles in it; allocated from the heap in init_xdisp and
10968 resized as needed in store_mode_line_noprop_char. */
10970 static char *mode_line_noprop_buf;
10972 /* The buffer's end, and a current output position in it. */
10974 static char *mode_line_noprop_buf_end;
10975 static char *mode_line_noprop_ptr;
10977 #define MODE_LINE_NOPROP_LEN(start) \
10978 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10980 static enum {
10981 MODE_LINE_DISPLAY = 0,
10982 MODE_LINE_TITLE,
10983 MODE_LINE_NOPROP,
10984 MODE_LINE_STRING
10985 } mode_line_target;
10987 /* Alist that caches the results of :propertize.
10988 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10989 static Lisp_Object mode_line_proptrans_alist;
10991 /* List of strings making up the mode-line. */
10992 static Lisp_Object mode_line_string_list;
10994 /* Base face property when building propertized mode line string. */
10995 static Lisp_Object mode_line_string_face;
10996 static Lisp_Object mode_line_string_face_prop;
10999 /* Unwind data for mode line strings */
11001 static Lisp_Object Vmode_line_unwind_vector;
11003 static Lisp_Object
11004 format_mode_line_unwind_data (struct frame *target_frame,
11005 struct buffer *obuf,
11006 Lisp_Object owin,
11007 int save_proptrans)
11009 Lisp_Object vector, tmp;
11011 /* Reduce consing by keeping one vector in
11012 Vwith_echo_area_save_vector. */
11013 vector = Vmode_line_unwind_vector;
11014 Vmode_line_unwind_vector = Qnil;
11016 if (NILP (vector))
11017 vector = Fmake_vector (make_number (10), Qnil);
11019 ASET (vector, 0, make_number (mode_line_target));
11020 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11021 ASET (vector, 2, mode_line_string_list);
11022 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11023 ASET (vector, 4, mode_line_string_face);
11024 ASET (vector, 5, mode_line_string_face_prop);
11026 if (obuf)
11027 XSETBUFFER (tmp, obuf);
11028 else
11029 tmp = Qnil;
11030 ASET (vector, 6, tmp);
11031 ASET (vector, 7, owin);
11032 if (target_frame)
11034 /* Similarly to `with-selected-window', if the operation selects
11035 a window on another frame, we must restore that frame's
11036 selected window, and (for a tty) the top-frame. */
11037 ASET (vector, 8, target_frame->selected_window);
11038 if (FRAME_TERMCAP_P (target_frame))
11039 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11042 return vector;
11045 static void
11046 unwind_format_mode_line (Lisp_Object vector)
11048 Lisp_Object old_window = AREF (vector, 7);
11049 Lisp_Object target_frame_window = AREF (vector, 8);
11050 Lisp_Object old_top_frame = AREF (vector, 9);
11052 mode_line_target = XINT (AREF (vector, 0));
11053 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11054 mode_line_string_list = AREF (vector, 2);
11055 if (! EQ (AREF (vector, 3), Qt))
11056 mode_line_proptrans_alist = AREF (vector, 3);
11057 mode_line_string_face = AREF (vector, 4);
11058 mode_line_string_face_prop = AREF (vector, 5);
11060 /* Select window before buffer, since it may change the buffer. */
11061 if (!NILP (old_window))
11063 /* If the operation that we are unwinding had selected a window
11064 on a different frame, reset its frame-selected-window. For a
11065 text terminal, reset its top-frame if necessary. */
11066 if (!NILP (target_frame_window))
11068 Lisp_Object frame
11069 = WINDOW_FRAME (XWINDOW (target_frame_window));
11071 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11072 Fselect_window (target_frame_window, Qt);
11074 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11075 Fselect_frame (old_top_frame, Qt);
11078 Fselect_window (old_window, Qt);
11081 if (!NILP (AREF (vector, 6)))
11083 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11084 ASET (vector, 6, Qnil);
11087 Vmode_line_unwind_vector = vector;
11091 /* Store a single character C for the frame title in mode_line_noprop_buf.
11092 Re-allocate mode_line_noprop_buf if necessary. */
11094 static void
11095 store_mode_line_noprop_char (char c)
11097 /* If output position has reached the end of the allocated buffer,
11098 increase the buffer's size. */
11099 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11101 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11102 ptrdiff_t size = len;
11103 mode_line_noprop_buf =
11104 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11105 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11106 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11109 *mode_line_noprop_ptr++ = c;
11113 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11114 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11115 characters that yield more columns than PRECISION; PRECISION <= 0
11116 means copy the whole string. Pad with spaces until FIELD_WIDTH
11117 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11118 pad. Called from display_mode_element when it is used to build a
11119 frame title. */
11121 static int
11122 store_mode_line_noprop (const char *string, int field_width, int precision)
11124 const unsigned char *str = (const unsigned char *) string;
11125 int n = 0;
11126 ptrdiff_t dummy, nbytes;
11128 /* Copy at most PRECISION chars from STR. */
11129 nbytes = strlen (string);
11130 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11131 while (nbytes--)
11132 store_mode_line_noprop_char (*str++);
11134 /* Fill up with spaces until FIELD_WIDTH reached. */
11135 while (field_width > 0
11136 && n < field_width)
11138 store_mode_line_noprop_char (' ');
11139 ++n;
11142 return n;
11145 /***********************************************************************
11146 Frame Titles
11147 ***********************************************************************/
11149 #ifdef HAVE_WINDOW_SYSTEM
11151 /* Set the title of FRAME, if it has changed. The title format is
11152 Vicon_title_format if FRAME is iconified, otherwise it is
11153 frame_title_format. */
11155 static void
11156 x_consider_frame_title (Lisp_Object frame)
11158 struct frame *f = XFRAME (frame);
11160 if (FRAME_WINDOW_P (f)
11161 || FRAME_MINIBUF_ONLY_P (f)
11162 || f->explicit_name)
11164 /* Do we have more than one visible frame on this X display? */
11165 Lisp_Object tail, other_frame, fmt;
11166 ptrdiff_t title_start;
11167 char *title;
11168 ptrdiff_t len;
11169 struct it it;
11170 ptrdiff_t count = SPECPDL_INDEX ();
11172 FOR_EACH_FRAME (tail, other_frame)
11174 struct frame *tf = XFRAME (other_frame);
11176 if (tf != f
11177 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11178 && !FRAME_MINIBUF_ONLY_P (tf)
11179 && !EQ (other_frame, tip_frame)
11180 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11181 break;
11184 /* Set global variable indicating that multiple frames exist. */
11185 multiple_frames = CONSP (tail);
11187 /* Switch to the buffer of selected window of the frame. Set up
11188 mode_line_target so that display_mode_element will output into
11189 mode_line_noprop_buf; then display the title. */
11190 record_unwind_protect (unwind_format_mode_line,
11191 format_mode_line_unwind_data
11192 (f, current_buffer, selected_window, 0));
11194 Fselect_window (f->selected_window, Qt);
11195 set_buffer_internal_1
11196 (XBUFFER (XWINDOW (f->selected_window)->contents));
11197 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11199 mode_line_target = MODE_LINE_TITLE;
11200 title_start = MODE_LINE_NOPROP_LEN (0);
11201 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11202 NULL, DEFAULT_FACE_ID);
11203 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11204 len = MODE_LINE_NOPROP_LEN (title_start);
11205 title = mode_line_noprop_buf + title_start;
11206 unbind_to (count, Qnil);
11208 /* Set the title only if it's changed. This avoids consing in
11209 the common case where it hasn't. (If it turns out that we've
11210 already wasted too much time by walking through the list with
11211 display_mode_element, then we might need to optimize at a
11212 higher level than this.) */
11213 if (! STRINGP (f->name)
11214 || SBYTES (f->name) != len
11215 || memcmp (title, SDATA (f->name), len) != 0)
11216 x_implicitly_set_name (f, make_string (title, len), Qnil);
11220 #endif /* not HAVE_WINDOW_SYSTEM */
11223 /***********************************************************************
11224 Menu Bars
11225 ***********************************************************************/
11228 /* Prepare for redisplay by updating menu-bar item lists when
11229 appropriate. This can call eval. */
11231 void
11232 prepare_menu_bars (void)
11234 int all_windows;
11235 struct gcpro gcpro1, gcpro2;
11236 struct frame *f;
11237 Lisp_Object tooltip_frame;
11239 #ifdef HAVE_WINDOW_SYSTEM
11240 tooltip_frame = tip_frame;
11241 #else
11242 tooltip_frame = Qnil;
11243 #endif
11245 /* Update all frame titles based on their buffer names, etc. We do
11246 this before the menu bars so that the buffer-menu will show the
11247 up-to-date frame titles. */
11248 #ifdef HAVE_WINDOW_SYSTEM
11249 if (windows_or_buffers_changed || update_mode_lines)
11251 Lisp_Object tail, frame;
11253 FOR_EACH_FRAME (tail, frame)
11255 f = XFRAME (frame);
11256 if (!EQ (frame, tooltip_frame)
11257 && (FRAME_ICONIFIED_P (f)
11258 || FRAME_VISIBLE_P (f) == 1
11259 /* Exclude TTY frames that are obscured because they
11260 are not the top frame on their console. This is
11261 because x_consider_frame_title actually switches
11262 to the frame, which for TTY frames means it is
11263 marked as garbaged, and will be completely
11264 redrawn on the next redisplay cycle. This causes
11265 TTY frames to be completely redrawn, when there
11266 are more than one of them, even though nothing
11267 should be changed on display. */
11268 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11269 x_consider_frame_title (frame);
11272 #endif /* HAVE_WINDOW_SYSTEM */
11274 /* Update the menu bar item lists, if appropriate. This has to be
11275 done before any actual redisplay or generation of display lines. */
11276 all_windows = (update_mode_lines
11277 || buffer_shared_and_changed ()
11278 || windows_or_buffers_changed);
11279 if (all_windows)
11281 Lisp_Object tail, frame;
11282 ptrdiff_t count = SPECPDL_INDEX ();
11283 /* 1 means that update_menu_bar has run its hooks
11284 so any further calls to update_menu_bar shouldn't do so again. */
11285 int menu_bar_hooks_run = 0;
11287 record_unwind_save_match_data ();
11289 FOR_EACH_FRAME (tail, frame)
11291 f = XFRAME (frame);
11293 /* Ignore tooltip frame. */
11294 if (EQ (frame, tooltip_frame))
11295 continue;
11297 /* If a window on this frame changed size, report that to
11298 the user and clear the size-change flag. */
11299 if (FRAME_WINDOW_SIZES_CHANGED (f))
11301 Lisp_Object functions;
11303 /* Clear flag first in case we get an error below. */
11304 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11305 functions = Vwindow_size_change_functions;
11306 GCPRO2 (tail, functions);
11308 while (CONSP (functions))
11310 if (!EQ (XCAR (functions), Qt))
11311 call1 (XCAR (functions), frame);
11312 functions = XCDR (functions);
11314 UNGCPRO;
11317 GCPRO1 (tail);
11318 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11319 #ifdef HAVE_WINDOW_SYSTEM
11320 update_tool_bar (f, 0);
11321 #endif
11322 #ifdef HAVE_NS
11323 if (windows_or_buffers_changed
11324 && FRAME_NS_P (f))
11325 ns_set_doc_edited
11326 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11327 #endif
11328 UNGCPRO;
11331 unbind_to (count, Qnil);
11333 else
11335 struct frame *sf = SELECTED_FRAME ();
11336 update_menu_bar (sf, 1, 0);
11337 #ifdef HAVE_WINDOW_SYSTEM
11338 update_tool_bar (sf, 1);
11339 #endif
11344 /* Update the menu bar item list for frame F. This has to be done
11345 before we start to fill in any display lines, because it can call
11346 eval.
11348 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11350 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11351 already ran the menu bar hooks for this redisplay, so there
11352 is no need to run them again. The return value is the
11353 updated value of this flag, to pass to the next call. */
11355 static int
11356 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11358 Lisp_Object window;
11359 register struct window *w;
11361 /* If called recursively during a menu update, do nothing. This can
11362 happen when, for instance, an activate-menubar-hook causes a
11363 redisplay. */
11364 if (inhibit_menubar_update)
11365 return hooks_run;
11367 window = FRAME_SELECTED_WINDOW (f);
11368 w = XWINDOW (window);
11370 if (FRAME_WINDOW_P (f)
11372 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11373 || defined (HAVE_NS) || defined (USE_GTK)
11374 FRAME_EXTERNAL_MENU_BAR (f)
11375 #else
11376 FRAME_MENU_BAR_LINES (f) > 0
11377 #endif
11378 : FRAME_MENU_BAR_LINES (f) > 0)
11380 /* If the user has switched buffers or windows, we need to
11381 recompute to reflect the new bindings. But we'll
11382 recompute when update_mode_lines is set too; that means
11383 that people can use force-mode-line-update to request
11384 that the menu bar be recomputed. The adverse effect on
11385 the rest of the redisplay algorithm is about the same as
11386 windows_or_buffers_changed anyway. */
11387 if (windows_or_buffers_changed
11388 /* This used to test w->update_mode_line, but we believe
11389 there is no need to recompute the menu in that case. */
11390 || update_mode_lines
11391 || window_buffer_changed (w))
11393 struct buffer *prev = current_buffer;
11394 ptrdiff_t count = SPECPDL_INDEX ();
11396 specbind (Qinhibit_menubar_update, Qt);
11398 set_buffer_internal_1 (XBUFFER (w->contents));
11399 if (save_match_data)
11400 record_unwind_save_match_data ();
11401 if (NILP (Voverriding_local_map_menu_flag))
11403 specbind (Qoverriding_terminal_local_map, Qnil);
11404 specbind (Qoverriding_local_map, Qnil);
11407 if (!hooks_run)
11409 /* Run the Lucid hook. */
11410 safe_run_hooks (Qactivate_menubar_hook);
11412 /* If it has changed current-menubar from previous value,
11413 really recompute the menu-bar from the value. */
11414 if (! NILP (Vlucid_menu_bar_dirty_flag))
11415 call0 (Qrecompute_lucid_menubar);
11417 safe_run_hooks (Qmenu_bar_update_hook);
11419 hooks_run = 1;
11422 XSETFRAME (Vmenu_updating_frame, f);
11423 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11425 /* Redisplay the menu bar in case we changed it. */
11426 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11427 || defined (HAVE_NS) || defined (USE_GTK)
11428 if (FRAME_WINDOW_P (f))
11430 #if defined (HAVE_NS)
11431 /* All frames on Mac OS share the same menubar. So only
11432 the selected frame should be allowed to set it. */
11433 if (f == SELECTED_FRAME ())
11434 #endif
11435 set_frame_menubar (f, 0, 0);
11437 else
11438 /* On a terminal screen, the menu bar is an ordinary screen
11439 line, and this makes it get updated. */
11440 w->update_mode_line = 1;
11441 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11442 /* In the non-toolkit version, the menu bar is an ordinary screen
11443 line, and this makes it get updated. */
11444 w->update_mode_line = 1;
11445 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11447 unbind_to (count, Qnil);
11448 set_buffer_internal_1 (prev);
11452 return hooks_run;
11455 /***********************************************************************
11456 Tool-bars
11457 ***********************************************************************/
11459 #ifdef HAVE_WINDOW_SYSTEM
11461 /* Where the mouse was last time we reported a mouse event. */
11463 struct frame *last_mouse_frame;
11465 /* Tool-bar item index of the item on which a mouse button was pressed
11466 or -1. */
11468 int last_tool_bar_item;
11470 /* Select `frame' temporarily without running all the code in
11471 do_switch_frame.
11472 FIXME: Maybe do_switch_frame should be trimmed down similarly
11473 when `norecord' is set. */
11474 static void
11475 fast_set_selected_frame (Lisp_Object frame)
11477 if (!EQ (selected_frame, frame))
11479 selected_frame = frame;
11480 selected_window = XFRAME (frame)->selected_window;
11484 /* Update the tool-bar item list for frame F. This has to be done
11485 before we start to fill in any display lines. Called from
11486 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11487 and restore it here. */
11489 static void
11490 update_tool_bar (struct frame *f, int save_match_data)
11492 #if defined (USE_GTK) || defined (HAVE_NS)
11493 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11494 #else
11495 int do_update = WINDOWP (f->tool_bar_window)
11496 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11497 #endif
11499 if (do_update)
11501 Lisp_Object window;
11502 struct window *w;
11504 window = FRAME_SELECTED_WINDOW (f);
11505 w = XWINDOW (window);
11507 /* If the user has switched buffers or windows, we need to
11508 recompute to reflect the new bindings. But we'll
11509 recompute when update_mode_lines is set too; that means
11510 that people can use force-mode-line-update to request
11511 that the menu bar be recomputed. The adverse effect on
11512 the rest of the redisplay algorithm is about the same as
11513 windows_or_buffers_changed anyway. */
11514 if (windows_or_buffers_changed
11515 || w->update_mode_line
11516 || update_mode_lines
11517 || window_buffer_changed (w))
11519 struct buffer *prev = current_buffer;
11520 ptrdiff_t count = SPECPDL_INDEX ();
11521 Lisp_Object frame, new_tool_bar;
11522 int new_n_tool_bar;
11523 struct gcpro gcpro1;
11525 /* Set current_buffer to the buffer of the selected
11526 window of the frame, so that we get the right local
11527 keymaps. */
11528 set_buffer_internal_1 (XBUFFER (w->contents));
11530 /* Save match data, if we must. */
11531 if (save_match_data)
11532 record_unwind_save_match_data ();
11534 /* Make sure that we don't accidentally use bogus keymaps. */
11535 if (NILP (Voverriding_local_map_menu_flag))
11537 specbind (Qoverriding_terminal_local_map, Qnil);
11538 specbind (Qoverriding_local_map, Qnil);
11541 GCPRO1 (new_tool_bar);
11543 /* We must temporarily set the selected frame to this frame
11544 before calling tool_bar_items, because the calculation of
11545 the tool-bar keymap uses the selected frame (see
11546 `tool-bar-make-keymap' in tool-bar.el). */
11547 eassert (EQ (selected_window,
11548 /* Since we only explicitly preserve selected_frame,
11549 check that selected_window would be redundant. */
11550 XFRAME (selected_frame)->selected_window));
11551 record_unwind_protect (fast_set_selected_frame, selected_frame);
11552 XSETFRAME (frame, f);
11553 fast_set_selected_frame (frame);
11555 /* Build desired tool-bar items from keymaps. */
11556 new_tool_bar
11557 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11558 &new_n_tool_bar);
11560 /* Redisplay the tool-bar if we changed it. */
11561 if (new_n_tool_bar != f->n_tool_bar_items
11562 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11564 /* Redisplay that happens asynchronously due to an expose event
11565 may access f->tool_bar_items. Make sure we update both
11566 variables within BLOCK_INPUT so no such event interrupts. */
11567 block_input ();
11568 fset_tool_bar_items (f, new_tool_bar);
11569 f->n_tool_bar_items = new_n_tool_bar;
11570 w->update_mode_line = 1;
11571 unblock_input ();
11574 UNGCPRO;
11576 unbind_to (count, Qnil);
11577 set_buffer_internal_1 (prev);
11583 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11584 F's desired tool-bar contents. F->tool_bar_items must have
11585 been set up previously by calling prepare_menu_bars. */
11587 static void
11588 build_desired_tool_bar_string (struct frame *f)
11590 int i, size, size_needed;
11591 struct gcpro gcpro1, gcpro2, gcpro3;
11592 Lisp_Object image, plist, props;
11594 image = plist = props = Qnil;
11595 GCPRO3 (image, plist, props);
11597 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11598 Otherwise, make a new string. */
11600 /* The size of the string we might be able to reuse. */
11601 size = (STRINGP (f->desired_tool_bar_string)
11602 ? SCHARS (f->desired_tool_bar_string)
11603 : 0);
11605 /* We need one space in the string for each image. */
11606 size_needed = f->n_tool_bar_items;
11608 /* Reuse f->desired_tool_bar_string, if possible. */
11609 if (size < size_needed || NILP (f->desired_tool_bar_string))
11610 fset_desired_tool_bar_string
11611 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11612 else
11614 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11615 Fremove_text_properties (make_number (0), make_number (size),
11616 props, f->desired_tool_bar_string);
11619 /* Put a `display' property on the string for the images to display,
11620 put a `menu_item' property on tool-bar items with a value that
11621 is the index of the item in F's tool-bar item vector. */
11622 for (i = 0; i < f->n_tool_bar_items; ++i)
11624 #define PROP(IDX) \
11625 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11627 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11628 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11629 int hmargin, vmargin, relief, idx, end;
11631 /* If image is a vector, choose the image according to the
11632 button state. */
11633 image = PROP (TOOL_BAR_ITEM_IMAGES);
11634 if (VECTORP (image))
11636 if (enabled_p)
11637 idx = (selected_p
11638 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11639 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11640 else
11641 idx = (selected_p
11642 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11643 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11645 eassert (ASIZE (image) >= idx);
11646 image = AREF (image, idx);
11648 else
11649 idx = -1;
11651 /* Ignore invalid image specifications. */
11652 if (!valid_image_p (image))
11653 continue;
11655 /* Display the tool-bar button pressed, or depressed. */
11656 plist = Fcopy_sequence (XCDR (image));
11658 /* Compute margin and relief to draw. */
11659 relief = (tool_bar_button_relief >= 0
11660 ? tool_bar_button_relief
11661 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11662 hmargin = vmargin = relief;
11664 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11665 INT_MAX - max (hmargin, vmargin)))
11667 hmargin += XFASTINT (Vtool_bar_button_margin);
11668 vmargin += XFASTINT (Vtool_bar_button_margin);
11670 else if (CONSP (Vtool_bar_button_margin))
11672 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11673 INT_MAX - hmargin))
11674 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11676 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11677 INT_MAX - vmargin))
11678 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11681 if (auto_raise_tool_bar_buttons_p)
11683 /* Add a `:relief' property to the image spec if the item is
11684 selected. */
11685 if (selected_p)
11687 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11688 hmargin -= relief;
11689 vmargin -= relief;
11692 else
11694 /* If image is selected, display it pressed, i.e. with a
11695 negative relief. If it's not selected, display it with a
11696 raised relief. */
11697 plist = Fplist_put (plist, QCrelief,
11698 (selected_p
11699 ? make_number (-relief)
11700 : make_number (relief)));
11701 hmargin -= relief;
11702 vmargin -= relief;
11705 /* Put a margin around the image. */
11706 if (hmargin || vmargin)
11708 if (hmargin == vmargin)
11709 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11710 else
11711 plist = Fplist_put (plist, QCmargin,
11712 Fcons (make_number (hmargin),
11713 make_number (vmargin)));
11716 /* If button is not enabled, and we don't have special images
11717 for the disabled state, make the image appear disabled by
11718 applying an appropriate algorithm to it. */
11719 if (!enabled_p && idx < 0)
11720 plist = Fplist_put (plist, QCconversion, Qdisabled);
11722 /* Put a `display' text property on the string for the image to
11723 display. Put a `menu-item' property on the string that gives
11724 the start of this item's properties in the tool-bar items
11725 vector. */
11726 image = Fcons (Qimage, plist);
11727 props = list4 (Qdisplay, image,
11728 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11730 /* Let the last image hide all remaining spaces in the tool bar
11731 string. The string can be longer than needed when we reuse a
11732 previous string. */
11733 if (i + 1 == f->n_tool_bar_items)
11734 end = SCHARS (f->desired_tool_bar_string);
11735 else
11736 end = i + 1;
11737 Fadd_text_properties (make_number (i), make_number (end),
11738 props, f->desired_tool_bar_string);
11739 #undef PROP
11742 UNGCPRO;
11746 /* Display one line of the tool-bar of frame IT->f.
11748 HEIGHT specifies the desired height of the tool-bar line.
11749 If the actual height of the glyph row is less than HEIGHT, the
11750 row's height is increased to HEIGHT, and the icons are centered
11751 vertically in the new height.
11753 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11754 count a final empty row in case the tool-bar width exactly matches
11755 the window width.
11758 static void
11759 display_tool_bar_line (struct it *it, int height)
11761 struct glyph_row *row = it->glyph_row;
11762 int max_x = it->last_visible_x;
11763 struct glyph *last;
11765 prepare_desired_row (row);
11766 row->y = it->current_y;
11768 /* Note that this isn't made use of if the face hasn't a box,
11769 so there's no need to check the face here. */
11770 it->start_of_box_run_p = 1;
11772 while (it->current_x < max_x)
11774 int x, n_glyphs_before, i, nglyphs;
11775 struct it it_before;
11777 /* Get the next display element. */
11778 if (!get_next_display_element (it))
11780 /* Don't count empty row if we are counting needed tool-bar lines. */
11781 if (height < 0 && !it->hpos)
11782 return;
11783 break;
11786 /* Produce glyphs. */
11787 n_glyphs_before = row->used[TEXT_AREA];
11788 it_before = *it;
11790 PRODUCE_GLYPHS (it);
11792 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11793 i = 0;
11794 x = it_before.current_x;
11795 while (i < nglyphs)
11797 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11799 if (x + glyph->pixel_width > max_x)
11801 /* Glyph doesn't fit on line. Backtrack. */
11802 row->used[TEXT_AREA] = n_glyphs_before;
11803 *it = it_before;
11804 /* If this is the only glyph on this line, it will never fit on the
11805 tool-bar, so skip it. But ensure there is at least one glyph,
11806 so we don't accidentally disable the tool-bar. */
11807 if (n_glyphs_before == 0
11808 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11809 break;
11810 goto out;
11813 ++it->hpos;
11814 x += glyph->pixel_width;
11815 ++i;
11818 /* Stop at line end. */
11819 if (ITERATOR_AT_END_OF_LINE_P (it))
11820 break;
11822 set_iterator_to_next (it, 1);
11825 out:;
11827 row->displays_text_p = row->used[TEXT_AREA] != 0;
11829 /* Use default face for the border below the tool bar.
11831 FIXME: When auto-resize-tool-bars is grow-only, there is
11832 no additional border below the possibly empty tool-bar lines.
11833 So to make the extra empty lines look "normal", we have to
11834 use the tool-bar face for the border too. */
11835 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11836 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11837 it->face_id = DEFAULT_FACE_ID;
11839 extend_face_to_end_of_line (it);
11840 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11841 last->right_box_line_p = 1;
11842 if (last == row->glyphs[TEXT_AREA])
11843 last->left_box_line_p = 1;
11845 /* Make line the desired height and center it vertically. */
11846 if ((height -= it->max_ascent + it->max_descent) > 0)
11848 /* Don't add more than one line height. */
11849 height %= FRAME_LINE_HEIGHT (it->f);
11850 it->max_ascent += height / 2;
11851 it->max_descent += (height + 1) / 2;
11854 compute_line_metrics (it);
11856 /* If line is empty, make it occupy the rest of the tool-bar. */
11857 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11859 row->height = row->phys_height = it->last_visible_y - row->y;
11860 row->visible_height = row->height;
11861 row->ascent = row->phys_ascent = 0;
11862 row->extra_line_spacing = 0;
11865 row->full_width_p = 1;
11866 row->continued_p = 0;
11867 row->truncated_on_left_p = 0;
11868 row->truncated_on_right_p = 0;
11870 it->current_x = it->hpos = 0;
11871 it->current_y += row->height;
11872 ++it->vpos;
11873 ++it->glyph_row;
11877 /* Max tool-bar height. */
11879 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11880 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11882 /* Value is the number of screen lines needed to make all tool-bar
11883 items of frame F visible. The number of actual rows needed is
11884 returned in *N_ROWS if non-NULL. */
11886 static int
11887 tool_bar_lines_needed (struct frame *f, int *n_rows)
11889 struct window *w = XWINDOW (f->tool_bar_window);
11890 struct it it;
11891 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11892 the desired matrix, so use (unused) mode-line row as temporary row to
11893 avoid destroying the first tool-bar row. */
11894 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11896 /* Initialize an iterator for iteration over
11897 F->desired_tool_bar_string in the tool-bar window of frame F. */
11898 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11899 it.first_visible_x = 0;
11900 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11901 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11902 it.paragraph_embedding = L2R;
11904 while (!ITERATOR_AT_END_P (&it))
11906 clear_glyph_row (temp_row);
11907 it.glyph_row = temp_row;
11908 display_tool_bar_line (&it, -1);
11910 clear_glyph_row (temp_row);
11912 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11913 if (n_rows)
11914 *n_rows = it.vpos > 0 ? it.vpos : -1;
11916 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11920 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11921 0, 1, 0,
11922 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11923 If FRAME is nil or omitted, use the selected frame. */)
11924 (Lisp_Object frame)
11926 struct frame *f = decode_any_frame (frame);
11927 struct window *w;
11928 int nlines = 0;
11930 if (WINDOWP (f->tool_bar_window)
11931 && (w = XWINDOW (f->tool_bar_window),
11932 WINDOW_TOTAL_LINES (w) > 0))
11934 update_tool_bar (f, 1);
11935 if (f->n_tool_bar_items)
11937 build_desired_tool_bar_string (f);
11938 nlines = tool_bar_lines_needed (f, NULL);
11942 return make_number (nlines);
11946 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11947 height should be changed. */
11949 static int
11950 redisplay_tool_bar (struct frame *f)
11952 struct window *w;
11953 struct it it;
11954 struct glyph_row *row;
11956 #if defined (USE_GTK) || defined (HAVE_NS)
11957 if (FRAME_EXTERNAL_TOOL_BAR (f))
11958 update_frame_tool_bar (f);
11959 return 0;
11960 #endif
11962 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11963 do anything. This means you must start with tool-bar-lines
11964 non-zero to get the auto-sizing effect. Or in other words, you
11965 can turn off tool-bars by specifying tool-bar-lines zero. */
11966 if (!WINDOWP (f->tool_bar_window)
11967 || (w = XWINDOW (f->tool_bar_window),
11968 WINDOW_TOTAL_LINES (w) == 0))
11969 return 0;
11971 /* Set up an iterator for the tool-bar window. */
11972 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11973 it.first_visible_x = 0;
11974 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11975 row = it.glyph_row;
11977 /* Build a string that represents the contents of the tool-bar. */
11978 build_desired_tool_bar_string (f);
11979 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11980 /* FIXME: This should be controlled by a user option. But it
11981 doesn't make sense to have an R2L tool bar if the menu bar cannot
11982 be drawn also R2L, and making the menu bar R2L is tricky due
11983 toolkit-specific code that implements it. If an R2L tool bar is
11984 ever supported, display_tool_bar_line should also be augmented to
11985 call unproduce_glyphs like display_line and display_string
11986 do. */
11987 it.paragraph_embedding = L2R;
11989 if (f->n_tool_bar_rows == 0)
11991 int nlines;
11993 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11994 nlines != WINDOW_TOTAL_LINES (w)))
11996 Lisp_Object frame;
11997 int old_height = WINDOW_TOTAL_LINES (w);
11999 XSETFRAME (frame, f);
12000 Fmodify_frame_parameters (frame,
12001 list1 (Fcons (Qtool_bar_lines,
12002 make_number (nlines))));
12003 if (WINDOW_TOTAL_LINES (w) != old_height)
12005 clear_glyph_matrix (w->desired_matrix);
12006 fonts_changed_p = 1;
12007 return 1;
12012 /* Display as many lines as needed to display all tool-bar items. */
12014 if (f->n_tool_bar_rows > 0)
12016 int border, rows, height, extra;
12018 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12019 border = XINT (Vtool_bar_border);
12020 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12021 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12022 else if (EQ (Vtool_bar_border, Qborder_width))
12023 border = f->border_width;
12024 else
12025 border = 0;
12026 if (border < 0)
12027 border = 0;
12029 rows = f->n_tool_bar_rows;
12030 height = max (1, (it.last_visible_y - border) / rows);
12031 extra = it.last_visible_y - border - height * rows;
12033 while (it.current_y < it.last_visible_y)
12035 int h = 0;
12036 if (extra > 0 && rows-- > 0)
12038 h = (extra + rows - 1) / rows;
12039 extra -= h;
12041 display_tool_bar_line (&it, height + h);
12044 else
12046 while (it.current_y < it.last_visible_y)
12047 display_tool_bar_line (&it, 0);
12050 /* It doesn't make much sense to try scrolling in the tool-bar
12051 window, so don't do it. */
12052 w->desired_matrix->no_scrolling_p = 1;
12053 w->must_be_updated_p = 1;
12055 if (!NILP (Vauto_resize_tool_bars))
12057 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12058 int change_height_p = 0;
12060 /* If we couldn't display everything, change the tool-bar's
12061 height if there is room for more. */
12062 if (IT_STRING_CHARPOS (it) < it.end_charpos
12063 && it.current_y < max_tool_bar_height)
12064 change_height_p = 1;
12066 row = it.glyph_row - 1;
12068 /* If there are blank lines at the end, except for a partially
12069 visible blank line at the end that is smaller than
12070 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12071 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12072 && row->height >= FRAME_LINE_HEIGHT (f))
12073 change_height_p = 1;
12075 /* If row displays tool-bar items, but is partially visible,
12076 change the tool-bar's height. */
12077 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12078 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12079 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12080 change_height_p = 1;
12082 /* Resize windows as needed by changing the `tool-bar-lines'
12083 frame parameter. */
12084 if (change_height_p)
12086 Lisp_Object frame;
12087 int old_height = WINDOW_TOTAL_LINES (w);
12088 int nrows;
12089 int nlines = tool_bar_lines_needed (f, &nrows);
12091 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12092 && !f->minimize_tool_bar_window_p)
12093 ? (nlines > old_height)
12094 : (nlines != old_height));
12095 f->minimize_tool_bar_window_p = 0;
12097 if (change_height_p)
12099 XSETFRAME (frame, f);
12100 Fmodify_frame_parameters (frame,
12101 list1 (Fcons (Qtool_bar_lines,
12102 make_number (nlines))));
12103 if (WINDOW_TOTAL_LINES (w) != old_height)
12105 clear_glyph_matrix (w->desired_matrix);
12106 f->n_tool_bar_rows = nrows;
12107 fonts_changed_p = 1;
12108 return 1;
12114 f->minimize_tool_bar_window_p = 0;
12115 return 0;
12119 /* Get information about the tool-bar item which is displayed in GLYPH
12120 on frame F. Return in *PROP_IDX the index where tool-bar item
12121 properties start in F->tool_bar_items. Value is zero if
12122 GLYPH doesn't display a tool-bar item. */
12124 static int
12125 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12127 Lisp_Object prop;
12128 int success_p;
12129 int charpos;
12131 /* This function can be called asynchronously, which means we must
12132 exclude any possibility that Fget_text_property signals an
12133 error. */
12134 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12135 charpos = max (0, charpos);
12137 /* Get the text property `menu-item' at pos. The value of that
12138 property is the start index of this item's properties in
12139 F->tool_bar_items. */
12140 prop = Fget_text_property (make_number (charpos),
12141 Qmenu_item, f->current_tool_bar_string);
12142 if (INTEGERP (prop))
12144 *prop_idx = XINT (prop);
12145 success_p = 1;
12147 else
12148 success_p = 0;
12150 return success_p;
12154 /* Get information about the tool-bar item at position X/Y on frame F.
12155 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12156 the current matrix of the tool-bar window of F, or NULL if not
12157 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12158 item in F->tool_bar_items. Value is
12160 -1 if X/Y is not on a tool-bar item
12161 0 if X/Y is on the same item that was highlighted before.
12162 1 otherwise. */
12164 static int
12165 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12166 int *hpos, int *vpos, int *prop_idx)
12168 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12169 struct window *w = XWINDOW (f->tool_bar_window);
12170 int area;
12172 /* Find the glyph under X/Y. */
12173 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12174 if (*glyph == NULL)
12175 return -1;
12177 /* Get the start of this tool-bar item's properties in
12178 f->tool_bar_items. */
12179 if (!tool_bar_item_info (f, *glyph, prop_idx))
12180 return -1;
12182 /* Is mouse on the highlighted item? */
12183 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12184 && *vpos >= hlinfo->mouse_face_beg_row
12185 && *vpos <= hlinfo->mouse_face_end_row
12186 && (*vpos > hlinfo->mouse_face_beg_row
12187 || *hpos >= hlinfo->mouse_face_beg_col)
12188 && (*vpos < hlinfo->mouse_face_end_row
12189 || *hpos < hlinfo->mouse_face_end_col
12190 || hlinfo->mouse_face_past_end))
12191 return 0;
12193 return 1;
12197 /* EXPORT:
12198 Handle mouse button event on the tool-bar of frame F, at
12199 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12200 0 for button release. MODIFIERS is event modifiers for button
12201 release. */
12203 void
12204 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12205 int modifiers)
12207 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12208 struct window *w = XWINDOW (f->tool_bar_window);
12209 int hpos, vpos, prop_idx;
12210 struct glyph *glyph;
12211 Lisp_Object enabled_p;
12212 int ts;
12214 /* If not on the highlighted tool-bar item, and mouse-highlight is
12215 non-nil, return. This is so we generate the tool-bar button
12216 click only when the mouse button is released on the same item as
12217 where it was pressed. However, when mouse-highlight is disabled,
12218 generate the click when the button is released regardless of the
12219 highlight, since tool-bar items are not highlighted in that
12220 case. */
12221 frame_to_window_pixel_xy (w, &x, &y);
12222 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12223 if (ts == -1
12224 || (ts != 0 && !NILP (Vmouse_highlight)))
12225 return;
12227 /* When mouse-highlight is off, generate the click for the item
12228 where the button was pressed, disregarding where it was
12229 released. */
12230 if (NILP (Vmouse_highlight) && !down_p)
12231 prop_idx = last_tool_bar_item;
12233 /* If item is disabled, do nothing. */
12234 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12235 if (NILP (enabled_p))
12236 return;
12238 if (down_p)
12240 /* Show item in pressed state. */
12241 if (!NILP (Vmouse_highlight))
12242 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12243 last_tool_bar_item = prop_idx;
12245 else
12247 Lisp_Object key, frame;
12248 struct input_event event;
12249 EVENT_INIT (event);
12251 /* Show item in released state. */
12252 if (!NILP (Vmouse_highlight))
12253 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12255 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12257 XSETFRAME (frame, f);
12258 event.kind = TOOL_BAR_EVENT;
12259 event.frame_or_window = frame;
12260 event.arg = frame;
12261 kbd_buffer_store_event (&event);
12263 event.kind = TOOL_BAR_EVENT;
12264 event.frame_or_window = frame;
12265 event.arg = key;
12266 event.modifiers = modifiers;
12267 kbd_buffer_store_event (&event);
12268 last_tool_bar_item = -1;
12273 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12274 tool-bar window-relative coordinates X/Y. Called from
12275 note_mouse_highlight. */
12277 static void
12278 note_tool_bar_highlight (struct frame *f, int x, int y)
12280 Lisp_Object window = f->tool_bar_window;
12281 struct window *w = XWINDOW (window);
12282 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12283 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12284 int hpos, vpos;
12285 struct glyph *glyph;
12286 struct glyph_row *row;
12287 int i;
12288 Lisp_Object enabled_p;
12289 int prop_idx;
12290 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12291 int mouse_down_p, rc;
12293 /* Function note_mouse_highlight is called with negative X/Y
12294 values when mouse moves outside of the frame. */
12295 if (x <= 0 || y <= 0)
12297 clear_mouse_face (hlinfo);
12298 return;
12301 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12302 if (rc < 0)
12304 /* Not on tool-bar item. */
12305 clear_mouse_face (hlinfo);
12306 return;
12308 else if (rc == 0)
12309 /* On same tool-bar item as before. */
12310 goto set_help_echo;
12312 clear_mouse_face (hlinfo);
12314 /* Mouse is down, but on different tool-bar item? */
12315 mouse_down_p = (dpyinfo->grabbed
12316 && f == last_mouse_frame
12317 && FRAME_LIVE_P (f));
12318 if (mouse_down_p
12319 && last_tool_bar_item != prop_idx)
12320 return;
12322 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12324 /* If tool-bar item is not enabled, don't highlight it. */
12325 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12326 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12328 /* Compute the x-position of the glyph. In front and past the
12329 image is a space. We include this in the highlighted area. */
12330 row = MATRIX_ROW (w->current_matrix, vpos);
12331 for (i = x = 0; i < hpos; ++i)
12332 x += row->glyphs[TEXT_AREA][i].pixel_width;
12334 /* Record this as the current active region. */
12335 hlinfo->mouse_face_beg_col = hpos;
12336 hlinfo->mouse_face_beg_row = vpos;
12337 hlinfo->mouse_face_beg_x = x;
12338 hlinfo->mouse_face_past_end = 0;
12340 hlinfo->mouse_face_end_col = hpos + 1;
12341 hlinfo->mouse_face_end_row = vpos;
12342 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12343 hlinfo->mouse_face_window = window;
12344 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12346 /* Display it as active. */
12347 show_mouse_face (hlinfo, draw);
12350 set_help_echo:
12352 /* Set help_echo_string to a help string to display for this tool-bar item.
12353 XTread_socket does the rest. */
12354 help_echo_object = help_echo_window = Qnil;
12355 help_echo_pos = -1;
12356 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12357 if (NILP (help_echo_string))
12358 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12361 #endif /* HAVE_WINDOW_SYSTEM */
12365 /************************************************************************
12366 Horizontal scrolling
12367 ************************************************************************/
12369 static int hscroll_window_tree (Lisp_Object);
12370 static int hscroll_windows (Lisp_Object);
12372 /* For all leaf windows in the window tree rooted at WINDOW, set their
12373 hscroll value so that PT is (i) visible in the window, and (ii) so
12374 that it is not within a certain margin at the window's left and
12375 right border. Value is non-zero if any window's hscroll has been
12376 changed. */
12378 static int
12379 hscroll_window_tree (Lisp_Object window)
12381 int hscrolled_p = 0;
12382 int hscroll_relative_p = FLOATP (Vhscroll_step);
12383 int hscroll_step_abs = 0;
12384 double hscroll_step_rel = 0;
12386 if (hscroll_relative_p)
12388 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12389 if (hscroll_step_rel < 0)
12391 hscroll_relative_p = 0;
12392 hscroll_step_abs = 0;
12395 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12397 hscroll_step_abs = XINT (Vhscroll_step);
12398 if (hscroll_step_abs < 0)
12399 hscroll_step_abs = 0;
12401 else
12402 hscroll_step_abs = 0;
12404 while (WINDOWP (window))
12406 struct window *w = XWINDOW (window);
12408 if (WINDOWP (w->contents))
12409 hscrolled_p |= hscroll_window_tree (w->contents);
12410 else if (w->cursor.vpos >= 0)
12412 int h_margin;
12413 int text_area_width;
12414 struct glyph_row *current_cursor_row
12415 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12416 struct glyph_row *desired_cursor_row
12417 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12418 struct glyph_row *cursor_row
12419 = (desired_cursor_row->enabled_p
12420 ? desired_cursor_row
12421 : current_cursor_row);
12422 int row_r2l_p = cursor_row->reversed_p;
12424 text_area_width = window_box_width (w, TEXT_AREA);
12426 /* Scroll when cursor is inside this scroll margin. */
12427 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12429 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12430 /* For left-to-right rows, hscroll when cursor is either
12431 (i) inside the right hscroll margin, or (ii) if it is
12432 inside the left margin and the window is already
12433 hscrolled. */
12434 && ((!row_r2l_p
12435 && ((w->hscroll
12436 && w->cursor.x <= h_margin)
12437 || (cursor_row->enabled_p
12438 && cursor_row->truncated_on_right_p
12439 && (w->cursor.x >= text_area_width - h_margin))))
12440 /* For right-to-left rows, the logic is similar,
12441 except that rules for scrolling to left and right
12442 are reversed. E.g., if cursor.x <= h_margin, we
12443 need to hscroll "to the right" unconditionally,
12444 and that will scroll the screen to the left so as
12445 to reveal the next portion of the row. */
12446 || (row_r2l_p
12447 && ((cursor_row->enabled_p
12448 /* FIXME: It is confusing to set the
12449 truncated_on_right_p flag when R2L rows
12450 are actually truncated on the left. */
12451 && cursor_row->truncated_on_right_p
12452 && w->cursor.x <= h_margin)
12453 || (w->hscroll
12454 && (w->cursor.x >= text_area_width - h_margin))))))
12456 struct it it;
12457 ptrdiff_t hscroll;
12458 struct buffer *saved_current_buffer;
12459 ptrdiff_t pt;
12460 int wanted_x;
12462 /* Find point in a display of infinite width. */
12463 saved_current_buffer = current_buffer;
12464 current_buffer = XBUFFER (w->contents);
12466 if (w == XWINDOW (selected_window))
12467 pt = PT;
12468 else
12469 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12471 /* Move iterator to pt starting at cursor_row->start in
12472 a line with infinite width. */
12473 init_to_row_start (&it, w, cursor_row);
12474 it.last_visible_x = INFINITY;
12475 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12476 current_buffer = saved_current_buffer;
12478 /* Position cursor in window. */
12479 if (!hscroll_relative_p && hscroll_step_abs == 0)
12480 hscroll = max (0, (it.current_x
12481 - (ITERATOR_AT_END_OF_LINE_P (&it)
12482 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12483 : (text_area_width / 2))))
12484 / FRAME_COLUMN_WIDTH (it.f);
12485 else if ((!row_r2l_p
12486 && w->cursor.x >= text_area_width - h_margin)
12487 || (row_r2l_p && w->cursor.x <= h_margin))
12489 if (hscroll_relative_p)
12490 wanted_x = text_area_width * (1 - hscroll_step_rel)
12491 - h_margin;
12492 else
12493 wanted_x = text_area_width
12494 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12495 - h_margin;
12496 hscroll
12497 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12499 else
12501 if (hscroll_relative_p)
12502 wanted_x = text_area_width * hscroll_step_rel
12503 + h_margin;
12504 else
12505 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12506 + h_margin;
12507 hscroll
12508 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12510 hscroll = max (hscroll, w->min_hscroll);
12512 /* Don't prevent redisplay optimizations if hscroll
12513 hasn't changed, as it will unnecessarily slow down
12514 redisplay. */
12515 if (w->hscroll != hscroll)
12517 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12518 w->hscroll = hscroll;
12519 hscrolled_p = 1;
12524 window = w->next;
12527 /* Value is non-zero if hscroll of any leaf window has been changed. */
12528 return hscrolled_p;
12532 /* Set hscroll so that cursor is visible and not inside horizontal
12533 scroll margins for all windows in the tree rooted at WINDOW. See
12534 also hscroll_window_tree above. Value is non-zero if any window's
12535 hscroll has been changed. If it has, desired matrices on the frame
12536 of WINDOW are cleared. */
12538 static int
12539 hscroll_windows (Lisp_Object window)
12541 int hscrolled_p = hscroll_window_tree (window);
12542 if (hscrolled_p)
12543 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12544 return hscrolled_p;
12549 /************************************************************************
12550 Redisplay
12551 ************************************************************************/
12553 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12554 to a non-zero value. This is sometimes handy to have in a debugger
12555 session. */
12557 #ifdef GLYPH_DEBUG
12559 /* First and last unchanged row for try_window_id. */
12561 static int debug_first_unchanged_at_end_vpos;
12562 static int debug_last_unchanged_at_beg_vpos;
12564 /* Delta vpos and y. */
12566 static int debug_dvpos, debug_dy;
12568 /* Delta in characters and bytes for try_window_id. */
12570 static ptrdiff_t debug_delta, debug_delta_bytes;
12572 /* Values of window_end_pos and window_end_vpos at the end of
12573 try_window_id. */
12575 static ptrdiff_t debug_end_vpos;
12577 /* Append a string to W->desired_matrix->method. FMT is a printf
12578 format string. If trace_redisplay_p is non-zero also printf the
12579 resulting string to stderr. */
12581 static void debug_method_add (struct window *, char const *, ...)
12582 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12584 static void
12585 debug_method_add (struct window *w, char const *fmt, ...)
12587 void *ptr = w;
12588 char *method = w->desired_matrix->method;
12589 int len = strlen (method);
12590 int size = sizeof w->desired_matrix->method;
12591 int remaining = size - len - 1;
12592 va_list ap;
12594 if (len && remaining)
12596 method[len] = '|';
12597 --remaining, ++len;
12600 va_start (ap, fmt);
12601 vsnprintf (method + len, remaining + 1, fmt, ap);
12602 va_end (ap);
12604 if (trace_redisplay_p)
12605 fprintf (stderr, "%p (%s): %s\n",
12606 ptr,
12607 ((BUFFERP (w->contents)
12608 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12609 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12610 : "no buffer"),
12611 method + len);
12614 #endif /* GLYPH_DEBUG */
12617 /* Value is non-zero if all changes in window W, which displays
12618 current_buffer, are in the text between START and END. START is a
12619 buffer position, END is given as a distance from Z. Used in
12620 redisplay_internal for display optimization. */
12622 static int
12623 text_outside_line_unchanged_p (struct window *w,
12624 ptrdiff_t start, ptrdiff_t end)
12626 int unchanged_p = 1;
12628 /* If text or overlays have changed, see where. */
12629 if (window_outdated (w))
12631 /* Gap in the line? */
12632 if (GPT < start || Z - GPT < end)
12633 unchanged_p = 0;
12635 /* Changes start in front of the line, or end after it? */
12636 if (unchanged_p
12637 && (BEG_UNCHANGED < start - 1
12638 || END_UNCHANGED < end))
12639 unchanged_p = 0;
12641 /* If selective display, can't optimize if changes start at the
12642 beginning of the line. */
12643 if (unchanged_p
12644 && INTEGERP (BVAR (current_buffer, selective_display))
12645 && XINT (BVAR (current_buffer, selective_display)) > 0
12646 && (BEG_UNCHANGED < start || GPT <= start))
12647 unchanged_p = 0;
12649 /* If there are overlays at the start or end of the line, these
12650 may have overlay strings with newlines in them. A change at
12651 START, for instance, may actually concern the display of such
12652 overlay strings as well, and they are displayed on different
12653 lines. So, quickly rule out this case. (For the future, it
12654 might be desirable to implement something more telling than
12655 just BEG/END_UNCHANGED.) */
12656 if (unchanged_p)
12658 if (BEG + BEG_UNCHANGED == start
12659 && overlay_touches_p (start))
12660 unchanged_p = 0;
12661 if (END_UNCHANGED == end
12662 && overlay_touches_p (Z - end))
12663 unchanged_p = 0;
12666 /* Under bidi reordering, adding or deleting a character in the
12667 beginning of a paragraph, before the first strong directional
12668 character, can change the base direction of the paragraph (unless
12669 the buffer specifies a fixed paragraph direction), which will
12670 require to redisplay the whole paragraph. It might be worthwhile
12671 to find the paragraph limits and widen the range of redisplayed
12672 lines to that, but for now just give up this optimization. */
12673 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12674 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12675 unchanged_p = 0;
12678 return unchanged_p;
12682 /* Do a frame update, taking possible shortcuts into account. This is
12683 the main external entry point for redisplay.
12685 If the last redisplay displayed an echo area message and that message
12686 is no longer requested, we clear the echo area or bring back the
12687 mini-buffer if that is in use. */
12689 void
12690 redisplay (void)
12692 redisplay_internal ();
12696 static Lisp_Object
12697 overlay_arrow_string_or_property (Lisp_Object var)
12699 Lisp_Object val;
12701 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12702 return val;
12704 return Voverlay_arrow_string;
12707 /* Return 1 if there are any overlay-arrows in current_buffer. */
12708 static int
12709 overlay_arrow_in_current_buffer_p (void)
12711 Lisp_Object vlist;
12713 for (vlist = Voverlay_arrow_variable_list;
12714 CONSP (vlist);
12715 vlist = XCDR (vlist))
12717 Lisp_Object var = XCAR (vlist);
12718 Lisp_Object val;
12720 if (!SYMBOLP (var))
12721 continue;
12722 val = find_symbol_value (var);
12723 if (MARKERP (val)
12724 && current_buffer == XMARKER (val)->buffer)
12725 return 1;
12727 return 0;
12731 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12732 has changed. */
12734 static int
12735 overlay_arrows_changed_p (void)
12737 Lisp_Object vlist;
12739 for (vlist = Voverlay_arrow_variable_list;
12740 CONSP (vlist);
12741 vlist = XCDR (vlist))
12743 Lisp_Object var = XCAR (vlist);
12744 Lisp_Object val, pstr;
12746 if (!SYMBOLP (var))
12747 continue;
12748 val = find_symbol_value (var);
12749 if (!MARKERP (val))
12750 continue;
12751 if (! EQ (COERCE_MARKER (val),
12752 Fget (var, Qlast_arrow_position))
12753 || ! (pstr = overlay_arrow_string_or_property (var),
12754 EQ (pstr, Fget (var, Qlast_arrow_string))))
12755 return 1;
12757 return 0;
12760 /* Mark overlay arrows to be updated on next redisplay. */
12762 static void
12763 update_overlay_arrows (int up_to_date)
12765 Lisp_Object vlist;
12767 for (vlist = Voverlay_arrow_variable_list;
12768 CONSP (vlist);
12769 vlist = XCDR (vlist))
12771 Lisp_Object var = XCAR (vlist);
12773 if (!SYMBOLP (var))
12774 continue;
12776 if (up_to_date > 0)
12778 Lisp_Object val = find_symbol_value (var);
12779 Fput (var, Qlast_arrow_position,
12780 COERCE_MARKER (val));
12781 Fput (var, Qlast_arrow_string,
12782 overlay_arrow_string_or_property (var));
12784 else if (up_to_date < 0
12785 || !NILP (Fget (var, Qlast_arrow_position)))
12787 Fput (var, Qlast_arrow_position, Qt);
12788 Fput (var, Qlast_arrow_string, Qt);
12794 /* Return overlay arrow string to display at row.
12795 Return integer (bitmap number) for arrow bitmap in left fringe.
12796 Return nil if no overlay arrow. */
12798 static Lisp_Object
12799 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12801 Lisp_Object vlist;
12803 for (vlist = Voverlay_arrow_variable_list;
12804 CONSP (vlist);
12805 vlist = XCDR (vlist))
12807 Lisp_Object var = XCAR (vlist);
12808 Lisp_Object val;
12810 if (!SYMBOLP (var))
12811 continue;
12813 val = find_symbol_value (var);
12815 if (MARKERP (val)
12816 && current_buffer == XMARKER (val)->buffer
12817 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12819 if (FRAME_WINDOW_P (it->f)
12820 /* FIXME: if ROW->reversed_p is set, this should test
12821 the right fringe, not the left one. */
12822 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12824 #ifdef HAVE_WINDOW_SYSTEM
12825 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12827 int fringe_bitmap;
12828 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12829 return make_number (fringe_bitmap);
12831 #endif
12832 return make_number (-1); /* Use default arrow bitmap. */
12834 return overlay_arrow_string_or_property (var);
12838 return Qnil;
12841 /* Return 1 if point moved out of or into a composition. Otherwise
12842 return 0. PREV_BUF and PREV_PT are the last point buffer and
12843 position. BUF and PT are the current point buffer and position. */
12845 static int
12846 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12847 struct buffer *buf, ptrdiff_t pt)
12849 ptrdiff_t start, end;
12850 Lisp_Object prop;
12851 Lisp_Object buffer;
12853 XSETBUFFER (buffer, buf);
12854 /* Check a composition at the last point if point moved within the
12855 same buffer. */
12856 if (prev_buf == buf)
12858 if (prev_pt == pt)
12859 /* Point didn't move. */
12860 return 0;
12862 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12863 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12864 && composition_valid_p (start, end, prop)
12865 && start < prev_pt && end > prev_pt)
12866 /* The last point was within the composition. Return 1 iff
12867 point moved out of the composition. */
12868 return (pt <= start || pt >= end);
12871 /* Check a composition at the current point. */
12872 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12873 && find_composition (pt, -1, &start, &end, &prop, buffer)
12874 && composition_valid_p (start, end, prop)
12875 && start < pt && end > pt);
12878 /* Reconsider the clip changes of buffer which is displayed in W. */
12880 static void
12881 reconsider_clip_changes (struct window *w)
12883 struct buffer *b = XBUFFER (w->contents);
12885 if (b->clip_changed
12886 && w->window_end_valid
12887 && w->current_matrix->buffer == b
12888 && w->current_matrix->zv == BUF_ZV (b)
12889 && w->current_matrix->begv == BUF_BEGV (b))
12890 b->clip_changed = 0;
12892 /* If display wasn't paused, and W is not a tool bar window, see if
12893 point has been moved into or out of a composition. In that case,
12894 we set b->clip_changed to 1 to force updating the screen. If
12895 b->clip_changed has already been set to 1, we can skip this
12896 check. */
12897 if (!b->clip_changed && w->window_end_valid)
12899 ptrdiff_t pt = (w == XWINDOW (selected_window)
12900 ? PT : marker_position (w->pointm));
12902 if ((w->current_matrix->buffer != b || pt != w->last_point)
12903 && check_point_in_composition (w->current_matrix->buffer,
12904 w->last_point, b, pt))
12905 b->clip_changed = 1;
12909 #define STOP_POLLING \
12910 do { if (! polling_stopped_here) stop_polling (); \
12911 polling_stopped_here = 1; } while (0)
12913 #define RESUME_POLLING \
12914 do { if (polling_stopped_here) start_polling (); \
12915 polling_stopped_here = 0; } while (0)
12918 /* Perhaps in the future avoid recentering windows if it
12919 is not necessary; currently that causes some problems. */
12921 static void
12922 redisplay_internal (void)
12924 struct window *w = XWINDOW (selected_window);
12925 struct window *sw;
12926 struct frame *fr;
12927 int pending;
12928 bool must_finish = 0, match_p;
12929 struct text_pos tlbufpos, tlendpos;
12930 int number_of_visible_frames;
12931 ptrdiff_t count;
12932 struct frame *sf;
12933 int polling_stopped_here = 0;
12934 Lisp_Object tail, frame;
12936 /* Non-zero means redisplay has to consider all windows on all
12937 frames. Zero means, only selected_window is considered. */
12938 int consider_all_windows_p;
12940 /* Non-zero means redisplay has to redisplay the miniwindow. */
12941 int update_miniwindow_p = 0;
12943 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12945 /* No redisplay if running in batch mode or frame is not yet fully
12946 initialized, or redisplay is explicitly turned off by setting
12947 Vinhibit_redisplay. */
12948 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12949 || !NILP (Vinhibit_redisplay))
12950 return;
12952 /* Don't examine these until after testing Vinhibit_redisplay.
12953 When Emacs is shutting down, perhaps because its connection to
12954 X has dropped, we should not look at them at all. */
12955 fr = XFRAME (w->frame);
12956 sf = SELECTED_FRAME ();
12958 if (!fr->glyphs_initialized_p)
12959 return;
12961 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12962 if (popup_activated ())
12963 return;
12964 #endif
12966 /* I don't think this happens but let's be paranoid. */
12967 if (redisplaying_p)
12968 return;
12970 /* Record a function that clears redisplaying_p
12971 when we leave this function. */
12972 count = SPECPDL_INDEX ();
12973 record_unwind_protect_void (unwind_redisplay);
12974 redisplaying_p = 1;
12975 specbind (Qinhibit_free_realized_faces, Qnil);
12977 /* Record this function, so it appears on the profiler's backtraces. */
12978 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12980 FOR_EACH_FRAME (tail, frame)
12981 XFRAME (frame)->already_hscrolled_p = 0;
12983 retry:
12984 /* Remember the currently selected window. */
12985 sw = w;
12987 pending = 0;
12988 last_escape_glyph_frame = NULL;
12989 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12990 last_glyphless_glyph_frame = NULL;
12991 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12993 /* If new fonts have been loaded that make a glyph matrix adjustment
12994 necessary, do it. */
12995 if (fonts_changed_p)
12997 adjust_glyphs (NULL);
12998 ++windows_or_buffers_changed;
12999 fonts_changed_p = 0;
13002 /* If face_change_count is non-zero, init_iterator will free all
13003 realized faces, which includes the faces referenced from current
13004 matrices. So, we can't reuse current matrices in this case. */
13005 if (face_change_count)
13006 ++windows_or_buffers_changed;
13008 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13009 && FRAME_TTY (sf)->previous_frame != sf)
13011 /* Since frames on a single ASCII terminal share the same
13012 display area, displaying a different frame means redisplay
13013 the whole thing. */
13014 windows_or_buffers_changed++;
13015 SET_FRAME_GARBAGED (sf);
13016 #ifndef DOS_NT
13017 set_tty_color_mode (FRAME_TTY (sf), sf);
13018 #endif
13019 FRAME_TTY (sf)->previous_frame = sf;
13022 /* Set the visible flags for all frames. Do this before checking for
13023 resized or garbaged frames; they want to know if their frames are
13024 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13025 number_of_visible_frames = 0;
13027 FOR_EACH_FRAME (tail, frame)
13029 struct frame *f = XFRAME (frame);
13031 if (FRAME_VISIBLE_P (f))
13032 ++number_of_visible_frames;
13033 clear_desired_matrices (f);
13036 /* Notice any pending interrupt request to change frame size. */
13037 do_pending_window_change (1);
13039 /* do_pending_window_change could change the selected_window due to
13040 frame resizing which makes the selected window too small. */
13041 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13042 sw = w;
13044 /* Clear frames marked as garbaged. */
13045 clear_garbaged_frames ();
13047 /* Build menubar and tool-bar items. */
13048 if (NILP (Vmemory_full))
13049 prepare_menu_bars ();
13051 if (windows_or_buffers_changed)
13052 update_mode_lines++;
13054 reconsider_clip_changes (w);
13056 /* In most cases selected window displays current buffer. */
13057 match_p = XBUFFER (w->contents) == current_buffer;
13058 if (match_p)
13060 ptrdiff_t count1;
13062 /* Detect case that we need to write or remove a star in the mode line. */
13063 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13065 w->update_mode_line = 1;
13066 if (buffer_shared_and_changed ())
13067 update_mode_lines++;
13070 /* Avoid invocation of point motion hooks by `current_column' below. */
13071 count1 = SPECPDL_INDEX ();
13072 specbind (Qinhibit_point_motion_hooks, Qt);
13074 if (mode_line_update_needed (w))
13075 w->update_mode_line = 1;
13077 unbind_to (count1, Qnil);
13080 consider_all_windows_p = (update_mode_lines
13081 || buffer_shared_and_changed ()
13082 || cursor_type_changed);
13084 /* If specs for an arrow have changed, do thorough redisplay
13085 to ensure we remove any arrow that should no longer exist. */
13086 if (overlay_arrows_changed_p ())
13087 consider_all_windows_p = windows_or_buffers_changed = 1;
13089 /* Normally the message* functions will have already displayed and
13090 updated the echo area, but the frame may have been trashed, or
13091 the update may have been preempted, so display the echo area
13092 again here. Checking message_cleared_p captures the case that
13093 the echo area should be cleared. */
13094 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13095 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13096 || (message_cleared_p
13097 && minibuf_level == 0
13098 /* If the mini-window is currently selected, this means the
13099 echo-area doesn't show through. */
13100 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13102 int window_height_changed_p = echo_area_display (0);
13104 if (message_cleared_p)
13105 update_miniwindow_p = 1;
13107 must_finish = 1;
13109 /* If we don't display the current message, don't clear the
13110 message_cleared_p flag, because, if we did, we wouldn't clear
13111 the echo area in the next redisplay which doesn't preserve
13112 the echo area. */
13113 if (!display_last_displayed_message_p)
13114 message_cleared_p = 0;
13116 if (fonts_changed_p)
13117 goto retry;
13118 else if (window_height_changed_p)
13120 consider_all_windows_p = 1;
13121 ++update_mode_lines;
13122 ++windows_or_buffers_changed;
13124 /* If window configuration was changed, frames may have been
13125 marked garbaged. Clear them or we will experience
13126 surprises wrt scrolling. */
13127 clear_garbaged_frames ();
13130 else if (EQ (selected_window, minibuf_window)
13131 && (current_buffer->clip_changed || window_outdated (w))
13132 && resize_mini_window (w, 0))
13134 /* Resized active mini-window to fit the size of what it is
13135 showing if its contents might have changed. */
13136 must_finish = 1;
13137 /* FIXME: this causes all frames to be updated, which seems unnecessary
13138 since only the current frame needs to be considered. This function
13139 needs to be rewritten with two variables, consider_all_windows and
13140 consider_all_frames. */
13141 consider_all_windows_p = 1;
13142 ++windows_or_buffers_changed;
13143 ++update_mode_lines;
13145 /* If window configuration was changed, frames may have been
13146 marked garbaged. Clear them or we will experience
13147 surprises wrt scrolling. */
13148 clear_garbaged_frames ();
13151 /* If showing the region, and mark has changed, we must redisplay
13152 the whole window. The assignment to this_line_start_pos prevents
13153 the optimization directly below this if-statement. */
13154 if (((!NILP (Vtransient_mark_mode)
13155 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13156 != (w->region_showing > 0))
13157 || (w->region_showing
13158 && w->region_showing
13159 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13160 CHARPOS (this_line_start_pos) = 0;
13162 /* Optimize the case that only the line containing the cursor in the
13163 selected window has changed. Variables starting with this_ are
13164 set in display_line and record information about the line
13165 containing the cursor. */
13166 tlbufpos = this_line_start_pos;
13167 tlendpos = this_line_end_pos;
13168 if (!consider_all_windows_p
13169 && CHARPOS (tlbufpos) > 0
13170 && !w->update_mode_line
13171 && !current_buffer->clip_changed
13172 && !current_buffer->prevent_redisplay_optimizations_p
13173 && FRAME_VISIBLE_P (XFRAME (w->frame))
13174 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13175 /* Make sure recorded data applies to current buffer, etc. */
13176 && this_line_buffer == current_buffer
13177 && match_p
13178 && !w->force_start
13179 && !w->optional_new_start
13180 /* Point must be on the line that we have info recorded about. */
13181 && PT >= CHARPOS (tlbufpos)
13182 && PT <= Z - CHARPOS (tlendpos)
13183 /* All text outside that line, including its final newline,
13184 must be unchanged. */
13185 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13186 CHARPOS (tlendpos)))
13188 if (CHARPOS (tlbufpos) > BEGV
13189 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13190 && (CHARPOS (tlbufpos) == ZV
13191 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13192 /* Former continuation line has disappeared by becoming empty. */
13193 goto cancel;
13194 else if (window_outdated (w) || MINI_WINDOW_P (w))
13196 /* We have to handle the case of continuation around a
13197 wide-column character (see the comment in indent.c around
13198 line 1340).
13200 For instance, in the following case:
13202 -------- Insert --------
13203 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13204 J_I_ ==> J_I_ `^^' are cursors.
13205 ^^ ^^
13206 -------- --------
13208 As we have to redraw the line above, we cannot use this
13209 optimization. */
13211 struct it it;
13212 int line_height_before = this_line_pixel_height;
13214 /* Note that start_display will handle the case that the
13215 line starting at tlbufpos is a continuation line. */
13216 start_display (&it, w, tlbufpos);
13218 /* Implementation note: It this still necessary? */
13219 if (it.current_x != this_line_start_x)
13220 goto cancel;
13222 TRACE ((stderr, "trying display optimization 1\n"));
13223 w->cursor.vpos = -1;
13224 overlay_arrow_seen = 0;
13225 it.vpos = this_line_vpos;
13226 it.current_y = this_line_y;
13227 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13228 display_line (&it);
13230 /* If line contains point, is not continued,
13231 and ends at same distance from eob as before, we win. */
13232 if (w->cursor.vpos >= 0
13233 /* Line is not continued, otherwise this_line_start_pos
13234 would have been set to 0 in display_line. */
13235 && CHARPOS (this_line_start_pos)
13236 /* Line ends as before. */
13237 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13238 /* Line has same height as before. Otherwise other lines
13239 would have to be shifted up or down. */
13240 && this_line_pixel_height == line_height_before)
13242 /* If this is not the window's last line, we must adjust
13243 the charstarts of the lines below. */
13244 if (it.current_y < it.last_visible_y)
13246 struct glyph_row *row
13247 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13248 ptrdiff_t delta, delta_bytes;
13250 /* We used to distinguish between two cases here,
13251 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13252 when the line ends in a newline or the end of the
13253 buffer's accessible portion. But both cases did
13254 the same, so they were collapsed. */
13255 delta = (Z
13256 - CHARPOS (tlendpos)
13257 - MATRIX_ROW_START_CHARPOS (row));
13258 delta_bytes = (Z_BYTE
13259 - BYTEPOS (tlendpos)
13260 - MATRIX_ROW_START_BYTEPOS (row));
13262 increment_matrix_positions (w->current_matrix,
13263 this_line_vpos + 1,
13264 w->current_matrix->nrows,
13265 delta, delta_bytes);
13268 /* If this row displays text now but previously didn't,
13269 or vice versa, w->window_end_vpos may have to be
13270 adjusted. */
13271 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13273 if (w->window_end_vpos < this_line_vpos)
13274 w->window_end_vpos = this_line_vpos;
13276 else if (w->window_end_vpos == this_line_vpos
13277 && this_line_vpos > 0)
13278 w->window_end_vpos = this_line_vpos - 1;
13279 w->window_end_valid = 0;
13281 /* Update hint: No need to try to scroll in update_window. */
13282 w->desired_matrix->no_scrolling_p = 1;
13284 #ifdef GLYPH_DEBUG
13285 *w->desired_matrix->method = 0;
13286 debug_method_add (w, "optimization 1");
13287 #endif
13288 #ifdef HAVE_WINDOW_SYSTEM
13289 update_window_fringes (w, 0);
13290 #endif
13291 goto update;
13293 else
13294 goto cancel;
13296 else if (/* Cursor position hasn't changed. */
13297 PT == w->last_point
13298 /* Make sure the cursor was last displayed
13299 in this window. Otherwise we have to reposition it. */
13300 && 0 <= w->cursor.vpos
13301 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13303 if (!must_finish)
13305 do_pending_window_change (1);
13306 /* If selected_window changed, redisplay again. */
13307 if (WINDOWP (selected_window)
13308 && (w = XWINDOW (selected_window)) != sw)
13309 goto retry;
13311 /* We used to always goto end_of_redisplay here, but this
13312 isn't enough if we have a blinking cursor. */
13313 if (w->cursor_off_p == w->last_cursor_off_p)
13314 goto end_of_redisplay;
13316 goto update;
13318 /* If highlighting the region, or if the cursor is in the echo area,
13319 then we can't just move the cursor. */
13320 else if (! (!NILP (Vtransient_mark_mode)
13321 && !NILP (BVAR (current_buffer, mark_active)))
13322 && (EQ (selected_window,
13323 BVAR (current_buffer, last_selected_window))
13324 || highlight_nonselected_windows)
13325 && !w->region_showing
13326 && NILP (Vshow_trailing_whitespace)
13327 && !cursor_in_echo_area)
13329 struct it it;
13330 struct glyph_row *row;
13332 /* Skip from tlbufpos to PT and see where it is. Note that
13333 PT may be in invisible text. If so, we will end at the
13334 next visible position. */
13335 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13336 NULL, DEFAULT_FACE_ID);
13337 it.current_x = this_line_start_x;
13338 it.current_y = this_line_y;
13339 it.vpos = this_line_vpos;
13341 /* The call to move_it_to stops in front of PT, but
13342 moves over before-strings. */
13343 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13345 if (it.vpos == this_line_vpos
13346 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13347 row->enabled_p))
13349 eassert (this_line_vpos == it.vpos);
13350 eassert (this_line_y == it.current_y);
13351 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13352 #ifdef GLYPH_DEBUG
13353 *w->desired_matrix->method = 0;
13354 debug_method_add (w, "optimization 3");
13355 #endif
13356 goto update;
13358 else
13359 goto cancel;
13362 cancel:
13363 /* Text changed drastically or point moved off of line. */
13364 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13367 CHARPOS (this_line_start_pos) = 0;
13368 consider_all_windows_p |= buffer_shared_and_changed ();
13369 ++clear_face_cache_count;
13370 #ifdef HAVE_WINDOW_SYSTEM
13371 ++clear_image_cache_count;
13372 #endif
13374 /* Build desired matrices, and update the display. If
13375 consider_all_windows_p is non-zero, do it for all windows on all
13376 frames. Otherwise do it for selected_window, only. */
13378 if (consider_all_windows_p)
13380 FOR_EACH_FRAME (tail, frame)
13381 XFRAME (frame)->updated_p = 0;
13383 FOR_EACH_FRAME (tail, frame)
13385 struct frame *f = XFRAME (frame);
13387 /* We don't have to do anything for unselected terminal
13388 frames. */
13389 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13390 && !EQ (FRAME_TTY (f)->top_frame, frame))
13391 continue;
13393 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13395 /* Mark all the scroll bars to be removed; we'll redeem
13396 the ones we want when we redisplay their windows. */
13397 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13398 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13400 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13401 redisplay_windows (FRAME_ROOT_WINDOW (f));
13403 /* The X error handler may have deleted that frame. */
13404 if (!FRAME_LIVE_P (f))
13405 continue;
13407 /* Any scroll bars which redisplay_windows should have
13408 nuked should now go away. */
13409 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13410 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13412 /* If fonts changed, display again. */
13413 /* ??? rms: I suspect it is a mistake to jump all the way
13414 back to retry here. It should just retry this frame. */
13415 if (fonts_changed_p)
13416 goto retry;
13418 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13420 /* See if we have to hscroll. */
13421 if (!f->already_hscrolled_p)
13423 f->already_hscrolled_p = 1;
13424 if (hscroll_windows (f->root_window))
13425 goto retry;
13428 /* Prevent various kinds of signals during display
13429 update. stdio is not robust about handling
13430 signals, which can cause an apparent I/O
13431 error. */
13432 if (interrupt_input)
13433 unrequest_sigio ();
13434 STOP_POLLING;
13436 /* Update the display. */
13437 set_window_update_flags (XWINDOW (f->root_window), 1);
13438 pending |= update_frame (f, 0, 0);
13439 f->updated_p = 1;
13444 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13446 if (!pending)
13448 /* Do the mark_window_display_accurate after all windows have
13449 been redisplayed because this call resets flags in buffers
13450 which are needed for proper redisplay. */
13451 FOR_EACH_FRAME (tail, frame)
13453 struct frame *f = XFRAME (frame);
13454 if (f->updated_p)
13456 mark_window_display_accurate (f->root_window, 1);
13457 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13458 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13463 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13465 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13466 struct frame *mini_frame;
13468 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13469 /* Use list_of_error, not Qerror, so that
13470 we catch only errors and don't run the debugger. */
13471 internal_condition_case_1 (redisplay_window_1, selected_window,
13472 list_of_error,
13473 redisplay_window_error);
13474 if (update_miniwindow_p)
13475 internal_condition_case_1 (redisplay_window_1, mini_window,
13476 list_of_error,
13477 redisplay_window_error);
13479 /* Compare desired and current matrices, perform output. */
13481 update:
13482 /* If fonts changed, display again. */
13483 if (fonts_changed_p)
13484 goto retry;
13486 /* Prevent various kinds of signals during display update.
13487 stdio is not robust about handling signals,
13488 which can cause an apparent I/O error. */
13489 if (interrupt_input)
13490 unrequest_sigio ();
13491 STOP_POLLING;
13493 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13495 if (hscroll_windows (selected_window))
13496 goto retry;
13498 XWINDOW (selected_window)->must_be_updated_p = 1;
13499 pending = update_frame (sf, 0, 0);
13502 /* We may have called echo_area_display at the top of this
13503 function. If the echo area is on another frame, that may
13504 have put text on a frame other than the selected one, so the
13505 above call to update_frame would not have caught it. Catch
13506 it here. */
13507 mini_window = FRAME_MINIBUF_WINDOW (sf);
13508 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13510 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13512 XWINDOW (mini_window)->must_be_updated_p = 1;
13513 pending |= update_frame (mini_frame, 0, 0);
13514 if (!pending && hscroll_windows (mini_window))
13515 goto retry;
13519 /* If display was paused because of pending input, make sure we do a
13520 thorough update the next time. */
13521 if (pending)
13523 /* Prevent the optimization at the beginning of
13524 redisplay_internal that tries a single-line update of the
13525 line containing the cursor in the selected window. */
13526 CHARPOS (this_line_start_pos) = 0;
13528 /* Let the overlay arrow be updated the next time. */
13529 update_overlay_arrows (0);
13531 /* If we pause after scrolling, some rows in the current
13532 matrices of some windows are not valid. */
13533 if (!WINDOW_FULL_WIDTH_P (w)
13534 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13535 update_mode_lines = 1;
13537 else
13539 if (!consider_all_windows_p)
13541 /* This has already been done above if
13542 consider_all_windows_p is set. */
13543 mark_window_display_accurate_1 (w, 1);
13545 /* Say overlay arrows are up to date. */
13546 update_overlay_arrows (1);
13548 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13549 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13552 update_mode_lines = 0;
13553 windows_or_buffers_changed = 0;
13554 cursor_type_changed = 0;
13557 /* Start SIGIO interrupts coming again. Having them off during the
13558 code above makes it less likely one will discard output, but not
13559 impossible, since there might be stuff in the system buffer here.
13560 But it is much hairier to try to do anything about that. */
13561 if (interrupt_input)
13562 request_sigio ();
13563 RESUME_POLLING;
13565 /* If a frame has become visible which was not before, redisplay
13566 again, so that we display it. Expose events for such a frame
13567 (which it gets when becoming visible) don't call the parts of
13568 redisplay constructing glyphs, so simply exposing a frame won't
13569 display anything in this case. So, we have to display these
13570 frames here explicitly. */
13571 if (!pending)
13573 int new_count = 0;
13575 FOR_EACH_FRAME (tail, frame)
13577 int this_is_visible = 0;
13579 if (XFRAME (frame)->visible)
13580 this_is_visible = 1;
13582 if (this_is_visible)
13583 new_count++;
13586 if (new_count != number_of_visible_frames)
13587 windows_or_buffers_changed++;
13590 /* Change frame size now if a change is pending. */
13591 do_pending_window_change (1);
13593 /* If we just did a pending size change, or have additional
13594 visible frames, or selected_window changed, redisplay again. */
13595 if ((windows_or_buffers_changed && !pending)
13596 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13597 goto retry;
13599 /* Clear the face and image caches.
13601 We used to do this only if consider_all_windows_p. But the cache
13602 needs to be cleared if a timer creates images in the current
13603 buffer (e.g. the test case in Bug#6230). */
13605 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13607 clear_face_cache (0);
13608 clear_face_cache_count = 0;
13611 #ifdef HAVE_WINDOW_SYSTEM
13612 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13614 clear_image_caches (Qnil);
13615 clear_image_cache_count = 0;
13617 #endif /* HAVE_WINDOW_SYSTEM */
13619 end_of_redisplay:
13620 unbind_to (count, Qnil);
13621 RESUME_POLLING;
13625 /* Redisplay, but leave alone any recent echo area message unless
13626 another message has been requested in its place.
13628 This is useful in situations where you need to redisplay but no
13629 user action has occurred, making it inappropriate for the message
13630 area to be cleared. See tracking_off and
13631 wait_reading_process_output for examples of these situations.
13633 FROM_WHERE is an integer saying from where this function was
13634 called. This is useful for debugging. */
13636 void
13637 redisplay_preserve_echo_area (int from_where)
13639 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13641 if (!NILP (echo_area_buffer[1]))
13643 /* We have a previously displayed message, but no current
13644 message. Redisplay the previous message. */
13645 display_last_displayed_message_p = 1;
13646 redisplay_internal ();
13647 display_last_displayed_message_p = 0;
13649 else
13650 redisplay_internal ();
13652 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13653 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13654 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13658 /* Function registered with record_unwind_protect in redisplay_internal. */
13660 static void
13661 unwind_redisplay (void)
13663 redisplaying_p = 0;
13667 /* Mark the display of leaf window W as accurate or inaccurate.
13668 If ACCURATE_P is non-zero mark display of W as accurate. If
13669 ACCURATE_P is zero, arrange for W to be redisplayed the next
13670 time redisplay_internal is called. */
13672 static void
13673 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13675 struct buffer *b = XBUFFER (w->contents);
13677 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13678 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13679 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13681 if (accurate_p)
13683 b->clip_changed = 0;
13684 b->prevent_redisplay_optimizations_p = 0;
13686 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13687 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13688 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13689 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13691 w->current_matrix->buffer = b;
13692 w->current_matrix->begv = BUF_BEGV (b);
13693 w->current_matrix->zv = BUF_ZV (b);
13695 w->last_cursor_vpos = w->cursor.vpos;
13696 w->last_cursor_off_p = w->cursor_off_p;
13698 if (w == XWINDOW (selected_window))
13699 w->last_point = BUF_PT (b);
13700 else
13701 w->last_point = marker_position (w->pointm);
13703 w->window_end_valid = 1;
13704 w->update_mode_line = 0;
13709 /* Mark the display of windows in the window tree rooted at WINDOW as
13710 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13711 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13712 be redisplayed the next time redisplay_internal is called. */
13714 void
13715 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13717 struct window *w;
13719 for (; !NILP (window); window = w->next)
13721 w = XWINDOW (window);
13722 if (WINDOWP (w->contents))
13723 mark_window_display_accurate (w->contents, accurate_p);
13724 else
13725 mark_window_display_accurate_1 (w, accurate_p);
13728 if (accurate_p)
13729 update_overlay_arrows (1);
13730 else
13731 /* Force a thorough redisplay the next time by setting
13732 last_arrow_position and last_arrow_string to t, which is
13733 unequal to any useful value of Voverlay_arrow_... */
13734 update_overlay_arrows (-1);
13738 /* Return value in display table DP (Lisp_Char_Table *) for character
13739 C. Since a display table doesn't have any parent, we don't have to
13740 follow parent. Do not call this function directly but use the
13741 macro DISP_CHAR_VECTOR. */
13743 Lisp_Object
13744 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13746 Lisp_Object val;
13748 if (ASCII_CHAR_P (c))
13750 val = dp->ascii;
13751 if (SUB_CHAR_TABLE_P (val))
13752 val = XSUB_CHAR_TABLE (val)->contents[c];
13754 else
13756 Lisp_Object table;
13758 XSETCHAR_TABLE (table, dp);
13759 val = char_table_ref (table, c);
13761 if (NILP (val))
13762 val = dp->defalt;
13763 return val;
13768 /***********************************************************************
13769 Window Redisplay
13770 ***********************************************************************/
13772 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13774 static void
13775 redisplay_windows (Lisp_Object window)
13777 while (!NILP (window))
13779 struct window *w = XWINDOW (window);
13781 if (WINDOWP (w->contents))
13782 redisplay_windows (w->contents);
13783 else if (BUFFERP (w->contents))
13785 displayed_buffer = XBUFFER (w->contents);
13786 /* Use list_of_error, not Qerror, so that
13787 we catch only errors and don't run the debugger. */
13788 internal_condition_case_1 (redisplay_window_0, window,
13789 list_of_error,
13790 redisplay_window_error);
13793 window = w->next;
13797 static Lisp_Object
13798 redisplay_window_error (Lisp_Object ignore)
13800 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13801 return Qnil;
13804 static Lisp_Object
13805 redisplay_window_0 (Lisp_Object window)
13807 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13808 redisplay_window (window, 0);
13809 return Qnil;
13812 static Lisp_Object
13813 redisplay_window_1 (Lisp_Object window)
13815 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13816 redisplay_window (window, 1);
13817 return Qnil;
13821 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13822 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13823 which positions recorded in ROW differ from current buffer
13824 positions.
13826 Return 0 if cursor is not on this row, 1 otherwise. */
13828 static int
13829 set_cursor_from_row (struct window *w, struct glyph_row *row,
13830 struct glyph_matrix *matrix,
13831 ptrdiff_t delta, ptrdiff_t delta_bytes,
13832 int dy, int dvpos)
13834 struct glyph *glyph = row->glyphs[TEXT_AREA];
13835 struct glyph *end = glyph + row->used[TEXT_AREA];
13836 struct glyph *cursor = NULL;
13837 /* The last known character position in row. */
13838 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13839 int x = row->x;
13840 ptrdiff_t pt_old = PT - delta;
13841 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13842 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13843 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13844 /* A glyph beyond the edge of TEXT_AREA which we should never
13845 touch. */
13846 struct glyph *glyphs_end = end;
13847 /* Non-zero means we've found a match for cursor position, but that
13848 glyph has the avoid_cursor_p flag set. */
13849 int match_with_avoid_cursor = 0;
13850 /* Non-zero means we've seen at least one glyph that came from a
13851 display string. */
13852 int string_seen = 0;
13853 /* Largest and smallest buffer positions seen so far during scan of
13854 glyph row. */
13855 ptrdiff_t bpos_max = pos_before;
13856 ptrdiff_t bpos_min = pos_after;
13857 /* Last buffer position covered by an overlay string with an integer
13858 `cursor' property. */
13859 ptrdiff_t bpos_covered = 0;
13860 /* Non-zero means the display string on which to display the cursor
13861 comes from a text property, not from an overlay. */
13862 int string_from_text_prop = 0;
13864 /* Don't even try doing anything if called for a mode-line or
13865 header-line row, since the rest of the code isn't prepared to
13866 deal with such calamities. */
13867 eassert (!row->mode_line_p);
13868 if (row->mode_line_p)
13869 return 0;
13871 /* Skip over glyphs not having an object at the start and the end of
13872 the row. These are special glyphs like truncation marks on
13873 terminal frames. */
13874 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13876 if (!row->reversed_p)
13878 while (glyph < end
13879 && INTEGERP (glyph->object)
13880 && glyph->charpos < 0)
13882 x += glyph->pixel_width;
13883 ++glyph;
13885 while (end > glyph
13886 && INTEGERP ((end - 1)->object)
13887 /* CHARPOS is zero for blanks and stretch glyphs
13888 inserted by extend_face_to_end_of_line. */
13889 && (end - 1)->charpos <= 0)
13890 --end;
13891 glyph_before = glyph - 1;
13892 glyph_after = end;
13894 else
13896 struct glyph *g;
13898 /* If the glyph row is reversed, we need to process it from back
13899 to front, so swap the edge pointers. */
13900 glyphs_end = end = glyph - 1;
13901 glyph += row->used[TEXT_AREA] - 1;
13903 while (glyph > end + 1
13904 && INTEGERP (glyph->object)
13905 && glyph->charpos < 0)
13907 --glyph;
13908 x -= glyph->pixel_width;
13910 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13911 --glyph;
13912 /* By default, in reversed rows we put the cursor on the
13913 rightmost (first in the reading order) glyph. */
13914 for (g = end + 1; g < glyph; g++)
13915 x += g->pixel_width;
13916 while (end < glyph
13917 && INTEGERP ((end + 1)->object)
13918 && (end + 1)->charpos <= 0)
13919 ++end;
13920 glyph_before = glyph + 1;
13921 glyph_after = end;
13924 else if (row->reversed_p)
13926 /* In R2L rows that don't display text, put the cursor on the
13927 rightmost glyph. Case in point: an empty last line that is
13928 part of an R2L paragraph. */
13929 cursor = end - 1;
13930 /* Avoid placing the cursor on the last glyph of the row, where
13931 on terminal frames we hold the vertical border between
13932 adjacent windows. */
13933 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13934 && !WINDOW_RIGHTMOST_P (w)
13935 && cursor == row->glyphs[LAST_AREA] - 1)
13936 cursor--;
13937 x = -1; /* will be computed below, at label compute_x */
13940 /* Step 1: Try to find the glyph whose character position
13941 corresponds to point. If that's not possible, find 2 glyphs
13942 whose character positions are the closest to point, one before
13943 point, the other after it. */
13944 if (!row->reversed_p)
13945 while (/* not marched to end of glyph row */
13946 glyph < end
13947 /* glyph was not inserted by redisplay for internal purposes */
13948 && !INTEGERP (glyph->object))
13950 if (BUFFERP (glyph->object))
13952 ptrdiff_t dpos = glyph->charpos - pt_old;
13954 if (glyph->charpos > bpos_max)
13955 bpos_max = glyph->charpos;
13956 if (glyph->charpos < bpos_min)
13957 bpos_min = glyph->charpos;
13958 if (!glyph->avoid_cursor_p)
13960 /* If we hit point, we've found the glyph on which to
13961 display the cursor. */
13962 if (dpos == 0)
13964 match_with_avoid_cursor = 0;
13965 break;
13967 /* See if we've found a better approximation to
13968 POS_BEFORE or to POS_AFTER. */
13969 if (0 > dpos && dpos > pos_before - pt_old)
13971 pos_before = glyph->charpos;
13972 glyph_before = glyph;
13974 else if (0 < dpos && dpos < pos_after - pt_old)
13976 pos_after = glyph->charpos;
13977 glyph_after = glyph;
13980 else if (dpos == 0)
13981 match_with_avoid_cursor = 1;
13983 else if (STRINGP (glyph->object))
13985 Lisp_Object chprop;
13986 ptrdiff_t glyph_pos = glyph->charpos;
13988 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13989 glyph->object);
13990 if (!NILP (chprop))
13992 /* If the string came from a `display' text property,
13993 look up the buffer position of that property and
13994 use that position to update bpos_max, as if we
13995 actually saw such a position in one of the row's
13996 glyphs. This helps with supporting integer values
13997 of `cursor' property on the display string in
13998 situations where most or all of the row's buffer
13999 text is completely covered by display properties,
14000 so that no glyph with valid buffer positions is
14001 ever seen in the row. */
14002 ptrdiff_t prop_pos =
14003 string_buffer_position_lim (glyph->object, pos_before,
14004 pos_after, 0);
14006 if (prop_pos >= pos_before)
14007 bpos_max = prop_pos - 1;
14009 if (INTEGERP (chprop))
14011 bpos_covered = bpos_max + XINT (chprop);
14012 /* If the `cursor' property covers buffer positions up
14013 to and including point, we should display cursor on
14014 this glyph. Note that, if a `cursor' property on one
14015 of the string's characters has an integer value, we
14016 will break out of the loop below _before_ we get to
14017 the position match above. IOW, integer values of
14018 the `cursor' property override the "exact match for
14019 point" strategy of positioning the cursor. */
14020 /* Implementation note: bpos_max == pt_old when, e.g.,
14021 we are in an empty line, where bpos_max is set to
14022 MATRIX_ROW_START_CHARPOS, see above. */
14023 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14025 cursor = glyph;
14026 break;
14030 string_seen = 1;
14032 x += glyph->pixel_width;
14033 ++glyph;
14035 else if (glyph > end) /* row is reversed */
14036 while (!INTEGERP (glyph->object))
14038 if (BUFFERP (glyph->object))
14040 ptrdiff_t dpos = glyph->charpos - pt_old;
14042 if (glyph->charpos > bpos_max)
14043 bpos_max = glyph->charpos;
14044 if (glyph->charpos < bpos_min)
14045 bpos_min = glyph->charpos;
14046 if (!glyph->avoid_cursor_p)
14048 if (dpos == 0)
14050 match_with_avoid_cursor = 0;
14051 break;
14053 if (0 > dpos && dpos > pos_before - pt_old)
14055 pos_before = glyph->charpos;
14056 glyph_before = glyph;
14058 else if (0 < dpos && dpos < pos_after - pt_old)
14060 pos_after = glyph->charpos;
14061 glyph_after = glyph;
14064 else if (dpos == 0)
14065 match_with_avoid_cursor = 1;
14067 else if (STRINGP (glyph->object))
14069 Lisp_Object chprop;
14070 ptrdiff_t glyph_pos = glyph->charpos;
14072 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14073 glyph->object);
14074 if (!NILP (chprop))
14076 ptrdiff_t prop_pos =
14077 string_buffer_position_lim (glyph->object, pos_before,
14078 pos_after, 0);
14080 if (prop_pos >= pos_before)
14081 bpos_max = prop_pos - 1;
14083 if (INTEGERP (chprop))
14085 bpos_covered = bpos_max + XINT (chprop);
14086 /* If the `cursor' property covers buffer positions up
14087 to and including point, we should display cursor on
14088 this glyph. */
14089 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14091 cursor = glyph;
14092 break;
14095 string_seen = 1;
14097 --glyph;
14098 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14100 x--; /* can't use any pixel_width */
14101 break;
14103 x -= glyph->pixel_width;
14106 /* Step 2: If we didn't find an exact match for point, we need to
14107 look for a proper place to put the cursor among glyphs between
14108 GLYPH_BEFORE and GLYPH_AFTER. */
14109 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14110 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14111 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14113 /* An empty line has a single glyph whose OBJECT is zero and
14114 whose CHARPOS is the position of a newline on that line.
14115 Note that on a TTY, there are more glyphs after that, which
14116 were produced by extend_face_to_end_of_line, but their
14117 CHARPOS is zero or negative. */
14118 int empty_line_p =
14119 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14120 && INTEGERP (glyph->object) && glyph->charpos > 0
14121 /* On a TTY, continued and truncated rows also have a glyph at
14122 their end whose OBJECT is zero and whose CHARPOS is
14123 positive (the continuation and truncation glyphs), but such
14124 rows are obviously not "empty". */
14125 && !(row->continued_p || row->truncated_on_right_p);
14127 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14129 ptrdiff_t ellipsis_pos;
14131 /* Scan back over the ellipsis glyphs. */
14132 if (!row->reversed_p)
14134 ellipsis_pos = (glyph - 1)->charpos;
14135 while (glyph > row->glyphs[TEXT_AREA]
14136 && (glyph - 1)->charpos == ellipsis_pos)
14137 glyph--, x -= glyph->pixel_width;
14138 /* That loop always goes one position too far, including
14139 the glyph before the ellipsis. So scan forward over
14140 that one. */
14141 x += glyph->pixel_width;
14142 glyph++;
14144 else /* row is reversed */
14146 ellipsis_pos = (glyph + 1)->charpos;
14147 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14148 && (glyph + 1)->charpos == ellipsis_pos)
14149 glyph++, x += glyph->pixel_width;
14150 x -= glyph->pixel_width;
14151 glyph--;
14154 else if (match_with_avoid_cursor)
14156 cursor = glyph_after;
14157 x = -1;
14159 else if (string_seen)
14161 int incr = row->reversed_p ? -1 : +1;
14163 /* Need to find the glyph that came out of a string which is
14164 present at point. That glyph is somewhere between
14165 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14166 positioned between POS_BEFORE and POS_AFTER in the
14167 buffer. */
14168 struct glyph *start, *stop;
14169 ptrdiff_t pos = pos_before;
14171 x = -1;
14173 /* If the row ends in a newline from a display string,
14174 reordering could have moved the glyphs belonging to the
14175 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14176 in this case we extend the search to the last glyph in
14177 the row that was not inserted by redisplay. */
14178 if (row->ends_in_newline_from_string_p)
14180 glyph_after = end;
14181 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14184 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14185 correspond to POS_BEFORE and POS_AFTER, respectively. We
14186 need START and STOP in the order that corresponds to the
14187 row's direction as given by its reversed_p flag. If the
14188 directionality of characters between POS_BEFORE and
14189 POS_AFTER is the opposite of the row's base direction,
14190 these characters will have been reordered for display,
14191 and we need to reverse START and STOP. */
14192 if (!row->reversed_p)
14194 start = min (glyph_before, glyph_after);
14195 stop = max (glyph_before, glyph_after);
14197 else
14199 start = max (glyph_before, glyph_after);
14200 stop = min (glyph_before, glyph_after);
14202 for (glyph = start + incr;
14203 row->reversed_p ? glyph > stop : glyph < stop; )
14206 /* Any glyphs that come from the buffer are here because
14207 of bidi reordering. Skip them, and only pay
14208 attention to glyphs that came from some string. */
14209 if (STRINGP (glyph->object))
14211 Lisp_Object str;
14212 ptrdiff_t tem;
14213 /* If the display property covers the newline, we
14214 need to search for it one position farther. */
14215 ptrdiff_t lim = pos_after
14216 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14218 string_from_text_prop = 0;
14219 str = glyph->object;
14220 tem = string_buffer_position_lim (str, pos, lim, 0);
14221 if (tem == 0 /* from overlay */
14222 || pos <= tem)
14224 /* If the string from which this glyph came is
14225 found in the buffer at point, or at position
14226 that is closer to point than pos_after, then
14227 we've found the glyph we've been looking for.
14228 If it comes from an overlay (tem == 0), and
14229 it has the `cursor' property on one of its
14230 glyphs, record that glyph as a candidate for
14231 displaying the cursor. (As in the
14232 unidirectional version, we will display the
14233 cursor on the last candidate we find.) */
14234 if (tem == 0
14235 || tem == pt_old
14236 || (tem - pt_old > 0 && tem < pos_after))
14238 /* The glyphs from this string could have
14239 been reordered. Find the one with the
14240 smallest string position. Or there could
14241 be a character in the string with the
14242 `cursor' property, which means display
14243 cursor on that character's glyph. */
14244 ptrdiff_t strpos = glyph->charpos;
14246 if (tem)
14248 cursor = glyph;
14249 string_from_text_prop = 1;
14251 for ( ;
14252 (row->reversed_p ? glyph > stop : glyph < stop)
14253 && EQ (glyph->object, str);
14254 glyph += incr)
14256 Lisp_Object cprop;
14257 ptrdiff_t gpos = glyph->charpos;
14259 cprop = Fget_char_property (make_number (gpos),
14260 Qcursor,
14261 glyph->object);
14262 if (!NILP (cprop))
14264 cursor = glyph;
14265 break;
14267 if (tem && glyph->charpos < strpos)
14269 strpos = glyph->charpos;
14270 cursor = glyph;
14274 if (tem == pt_old
14275 || (tem - pt_old > 0 && tem < pos_after))
14276 goto compute_x;
14278 if (tem)
14279 pos = tem + 1; /* don't find previous instances */
14281 /* This string is not what we want; skip all of the
14282 glyphs that came from it. */
14283 while ((row->reversed_p ? glyph > stop : glyph < stop)
14284 && EQ (glyph->object, str))
14285 glyph += incr;
14287 else
14288 glyph += incr;
14291 /* If we reached the end of the line, and END was from a string,
14292 the cursor is not on this line. */
14293 if (cursor == NULL
14294 && (row->reversed_p ? glyph <= end : glyph >= end)
14295 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14296 && STRINGP (end->object)
14297 && row->continued_p)
14298 return 0;
14300 /* A truncated row may not include PT among its character positions.
14301 Setting the cursor inside the scroll margin will trigger
14302 recalculation of hscroll in hscroll_window_tree. But if a
14303 display string covers point, defer to the string-handling
14304 code below to figure this out. */
14305 else if (row->truncated_on_left_p && pt_old < bpos_min)
14307 cursor = glyph_before;
14308 x = -1;
14310 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14311 /* Zero-width characters produce no glyphs. */
14312 || (!empty_line_p
14313 && (row->reversed_p
14314 ? glyph_after > glyphs_end
14315 : glyph_after < glyphs_end)))
14317 cursor = glyph_after;
14318 x = -1;
14322 compute_x:
14323 if (cursor != NULL)
14324 glyph = cursor;
14325 else if (glyph == glyphs_end
14326 && pos_before == pos_after
14327 && STRINGP ((row->reversed_p
14328 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14329 : row->glyphs[TEXT_AREA])->object))
14331 /* If all the glyphs of this row came from strings, put the
14332 cursor on the first glyph of the row. This avoids having the
14333 cursor outside of the text area in this very rare and hard
14334 use case. */
14335 glyph =
14336 row->reversed_p
14337 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14338 : row->glyphs[TEXT_AREA];
14340 if (x < 0)
14342 struct glyph *g;
14344 /* Need to compute x that corresponds to GLYPH. */
14345 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14347 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14348 emacs_abort ();
14349 x += g->pixel_width;
14353 /* ROW could be part of a continued line, which, under bidi
14354 reordering, might have other rows whose start and end charpos
14355 occlude point. Only set w->cursor if we found a better
14356 approximation to the cursor position than we have from previously
14357 examined candidate rows belonging to the same continued line. */
14358 if (/* we already have a candidate row */
14359 w->cursor.vpos >= 0
14360 /* that candidate is not the row we are processing */
14361 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14362 /* Make sure cursor.vpos specifies a row whose start and end
14363 charpos occlude point, and it is valid candidate for being a
14364 cursor-row. This is because some callers of this function
14365 leave cursor.vpos at the row where the cursor was displayed
14366 during the last redisplay cycle. */
14367 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14368 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14369 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14371 struct glyph *g1 =
14372 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14374 /* Don't consider glyphs that are outside TEXT_AREA. */
14375 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14376 return 0;
14377 /* Keep the candidate whose buffer position is the closest to
14378 point or has the `cursor' property. */
14379 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14380 w->cursor.hpos >= 0
14381 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14382 && ((BUFFERP (g1->object)
14383 && (g1->charpos == pt_old /* an exact match always wins */
14384 || (BUFFERP (glyph->object)
14385 && eabs (g1->charpos - pt_old)
14386 < eabs (glyph->charpos - pt_old))))
14387 /* previous candidate is a glyph from a string that has
14388 a non-nil `cursor' property */
14389 || (STRINGP (g1->object)
14390 && (!NILP (Fget_char_property (make_number (g1->charpos),
14391 Qcursor, g1->object))
14392 /* previous candidate is from the same display
14393 string as this one, and the display string
14394 came from a text property */
14395 || (EQ (g1->object, glyph->object)
14396 && string_from_text_prop)
14397 /* this candidate is from newline and its
14398 position is not an exact match */
14399 || (INTEGERP (glyph->object)
14400 && glyph->charpos != pt_old)))))
14401 return 0;
14402 /* If this candidate gives an exact match, use that. */
14403 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14404 /* If this candidate is a glyph created for the
14405 terminating newline of a line, and point is on that
14406 newline, it wins because it's an exact match. */
14407 || (!row->continued_p
14408 && INTEGERP (glyph->object)
14409 && glyph->charpos == 0
14410 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14411 /* Otherwise, keep the candidate that comes from a row
14412 spanning less buffer positions. This may win when one or
14413 both candidate positions are on glyphs that came from
14414 display strings, for which we cannot compare buffer
14415 positions. */
14416 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14417 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14418 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14419 return 0;
14421 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14422 w->cursor.x = x;
14423 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14424 w->cursor.y = row->y + dy;
14426 if (w == XWINDOW (selected_window))
14428 if (!row->continued_p
14429 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14430 && row->x == 0)
14432 this_line_buffer = XBUFFER (w->contents);
14434 CHARPOS (this_line_start_pos)
14435 = MATRIX_ROW_START_CHARPOS (row) + delta;
14436 BYTEPOS (this_line_start_pos)
14437 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14439 CHARPOS (this_line_end_pos)
14440 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14441 BYTEPOS (this_line_end_pos)
14442 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14444 this_line_y = w->cursor.y;
14445 this_line_pixel_height = row->height;
14446 this_line_vpos = w->cursor.vpos;
14447 this_line_start_x = row->x;
14449 else
14450 CHARPOS (this_line_start_pos) = 0;
14453 return 1;
14457 /* Run window scroll functions, if any, for WINDOW with new window
14458 start STARTP. Sets the window start of WINDOW to that position.
14460 We assume that the window's buffer is really current. */
14462 static struct text_pos
14463 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14465 struct window *w = XWINDOW (window);
14466 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14468 eassert (current_buffer == XBUFFER (w->contents));
14470 if (!NILP (Vwindow_scroll_functions))
14472 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14473 make_number (CHARPOS (startp)));
14474 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14475 /* In case the hook functions switch buffers. */
14476 set_buffer_internal (XBUFFER (w->contents));
14479 return startp;
14483 /* Make sure the line containing the cursor is fully visible.
14484 A value of 1 means there is nothing to be done.
14485 (Either the line is fully visible, or it cannot be made so,
14486 or we cannot tell.)
14488 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14489 is higher than window.
14491 A value of 0 means the caller should do scrolling
14492 as if point had gone off the screen. */
14494 static int
14495 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14497 struct glyph_matrix *matrix;
14498 struct glyph_row *row;
14499 int window_height;
14501 if (!make_cursor_line_fully_visible_p)
14502 return 1;
14504 /* It's not always possible to find the cursor, e.g, when a window
14505 is full of overlay strings. Don't do anything in that case. */
14506 if (w->cursor.vpos < 0)
14507 return 1;
14509 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14510 row = MATRIX_ROW (matrix, w->cursor.vpos);
14512 /* If the cursor row is not partially visible, there's nothing to do. */
14513 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14514 return 1;
14516 /* If the row the cursor is in is taller than the window's height,
14517 it's not clear what to do, so do nothing. */
14518 window_height = window_box_height (w);
14519 if (row->height >= window_height)
14521 if (!force_p || MINI_WINDOW_P (w)
14522 || w->vscroll || w->cursor.vpos == 0)
14523 return 1;
14525 return 0;
14529 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14530 non-zero means only WINDOW is redisplayed in redisplay_internal.
14531 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14532 in redisplay_window to bring a partially visible line into view in
14533 the case that only the cursor has moved.
14535 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14536 last screen line's vertical height extends past the end of the screen.
14538 Value is
14540 1 if scrolling succeeded
14542 0 if scrolling didn't find point.
14544 -1 if new fonts have been loaded so that we must interrupt
14545 redisplay, adjust glyph matrices, and try again. */
14547 enum
14549 SCROLLING_SUCCESS,
14550 SCROLLING_FAILED,
14551 SCROLLING_NEED_LARGER_MATRICES
14554 /* If scroll-conservatively is more than this, never recenter.
14556 If you change this, don't forget to update the doc string of
14557 `scroll-conservatively' and the Emacs manual. */
14558 #define SCROLL_LIMIT 100
14560 static int
14561 try_scrolling (Lisp_Object window, int just_this_one_p,
14562 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14563 int temp_scroll_step, int last_line_misfit)
14565 struct window *w = XWINDOW (window);
14566 struct frame *f = XFRAME (w->frame);
14567 struct text_pos pos, startp;
14568 struct it it;
14569 int this_scroll_margin, scroll_max, rc, height;
14570 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14571 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14572 Lisp_Object aggressive;
14573 /* We will never try scrolling more than this number of lines. */
14574 int scroll_limit = SCROLL_LIMIT;
14575 int frame_line_height = default_line_pixel_height (w);
14576 int window_total_lines
14577 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14579 #ifdef GLYPH_DEBUG
14580 debug_method_add (w, "try_scrolling");
14581 #endif
14583 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14585 /* Compute scroll margin height in pixels. We scroll when point is
14586 within this distance from the top or bottom of the window. */
14587 if (scroll_margin > 0)
14588 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14589 * frame_line_height;
14590 else
14591 this_scroll_margin = 0;
14593 /* Force arg_scroll_conservatively to have a reasonable value, to
14594 avoid scrolling too far away with slow move_it_* functions. Note
14595 that the user can supply scroll-conservatively equal to
14596 `most-positive-fixnum', which can be larger than INT_MAX. */
14597 if (arg_scroll_conservatively > scroll_limit)
14599 arg_scroll_conservatively = scroll_limit + 1;
14600 scroll_max = scroll_limit * frame_line_height;
14602 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14603 /* Compute how much we should try to scroll maximally to bring
14604 point into view. */
14605 scroll_max = (max (scroll_step,
14606 max (arg_scroll_conservatively, temp_scroll_step))
14607 * frame_line_height);
14608 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14609 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14610 /* We're trying to scroll because of aggressive scrolling but no
14611 scroll_step is set. Choose an arbitrary one. */
14612 scroll_max = 10 * frame_line_height;
14613 else
14614 scroll_max = 0;
14616 too_near_end:
14618 /* Decide whether to scroll down. */
14619 if (PT > CHARPOS (startp))
14621 int scroll_margin_y;
14623 /* Compute the pixel ypos of the scroll margin, then move IT to
14624 either that ypos or PT, whichever comes first. */
14625 start_display (&it, w, startp);
14626 scroll_margin_y = it.last_visible_y - this_scroll_margin
14627 - frame_line_height * extra_scroll_margin_lines;
14628 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14629 (MOVE_TO_POS | MOVE_TO_Y));
14631 if (PT > CHARPOS (it.current.pos))
14633 int y0 = line_bottom_y (&it);
14634 /* Compute how many pixels below window bottom to stop searching
14635 for PT. This avoids costly search for PT that is far away if
14636 the user limited scrolling by a small number of lines, but
14637 always finds PT if scroll_conservatively is set to a large
14638 number, such as most-positive-fixnum. */
14639 int slack = max (scroll_max, 10 * frame_line_height);
14640 int y_to_move = it.last_visible_y + slack;
14642 /* Compute the distance from the scroll margin to PT or to
14643 the scroll limit, whichever comes first. This should
14644 include the height of the cursor line, to make that line
14645 fully visible. */
14646 move_it_to (&it, PT, -1, y_to_move,
14647 -1, MOVE_TO_POS | MOVE_TO_Y);
14648 dy = line_bottom_y (&it) - y0;
14650 if (dy > scroll_max)
14651 return SCROLLING_FAILED;
14653 if (dy > 0)
14654 scroll_down_p = 1;
14658 if (scroll_down_p)
14660 /* Point is in or below the bottom scroll margin, so move the
14661 window start down. If scrolling conservatively, move it just
14662 enough down to make point visible. If scroll_step is set,
14663 move it down by scroll_step. */
14664 if (arg_scroll_conservatively)
14665 amount_to_scroll
14666 = min (max (dy, frame_line_height),
14667 frame_line_height * arg_scroll_conservatively);
14668 else if (scroll_step || temp_scroll_step)
14669 amount_to_scroll = scroll_max;
14670 else
14672 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14673 height = WINDOW_BOX_TEXT_HEIGHT (w);
14674 if (NUMBERP (aggressive))
14676 double float_amount = XFLOATINT (aggressive) * height;
14677 int aggressive_scroll = float_amount;
14678 if (aggressive_scroll == 0 && float_amount > 0)
14679 aggressive_scroll = 1;
14680 /* Don't let point enter the scroll margin near top of
14681 the window. This could happen if the value of
14682 scroll_up_aggressively is too large and there are
14683 non-zero margins, because scroll_up_aggressively
14684 means put point that fraction of window height
14685 _from_the_bottom_margin_. */
14686 if (aggressive_scroll + 2*this_scroll_margin > height)
14687 aggressive_scroll = height - 2*this_scroll_margin;
14688 amount_to_scroll = dy + aggressive_scroll;
14692 if (amount_to_scroll <= 0)
14693 return SCROLLING_FAILED;
14695 start_display (&it, w, startp);
14696 if (arg_scroll_conservatively <= scroll_limit)
14697 move_it_vertically (&it, amount_to_scroll);
14698 else
14700 /* Extra precision for users who set scroll-conservatively
14701 to a large number: make sure the amount we scroll
14702 the window start is never less than amount_to_scroll,
14703 which was computed as distance from window bottom to
14704 point. This matters when lines at window top and lines
14705 below window bottom have different height. */
14706 struct it it1;
14707 void *it1data = NULL;
14708 /* We use a temporary it1 because line_bottom_y can modify
14709 its argument, if it moves one line down; see there. */
14710 int start_y;
14712 SAVE_IT (it1, it, it1data);
14713 start_y = line_bottom_y (&it1);
14714 do {
14715 RESTORE_IT (&it, &it, it1data);
14716 move_it_by_lines (&it, 1);
14717 SAVE_IT (it1, it, it1data);
14718 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14721 /* If STARTP is unchanged, move it down another screen line. */
14722 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14723 move_it_by_lines (&it, 1);
14724 startp = it.current.pos;
14726 else
14728 struct text_pos scroll_margin_pos = startp;
14729 int y_offset = 0;
14731 /* See if point is inside the scroll margin at the top of the
14732 window. */
14733 if (this_scroll_margin)
14735 int y_start;
14737 start_display (&it, w, startp);
14738 y_start = it.current_y;
14739 move_it_vertically (&it, this_scroll_margin);
14740 scroll_margin_pos = it.current.pos;
14741 /* If we didn't move enough before hitting ZV, request
14742 additional amount of scroll, to move point out of the
14743 scroll margin. */
14744 if (IT_CHARPOS (it) == ZV
14745 && it.current_y - y_start < this_scroll_margin)
14746 y_offset = this_scroll_margin - (it.current_y - y_start);
14749 if (PT < CHARPOS (scroll_margin_pos))
14751 /* Point is in the scroll margin at the top of the window or
14752 above what is displayed in the window. */
14753 int y0, y_to_move;
14755 /* Compute the vertical distance from PT to the scroll
14756 margin position. Move as far as scroll_max allows, or
14757 one screenful, or 10 screen lines, whichever is largest.
14758 Give up if distance is greater than scroll_max or if we
14759 didn't reach the scroll margin position. */
14760 SET_TEXT_POS (pos, PT, PT_BYTE);
14761 start_display (&it, w, pos);
14762 y0 = it.current_y;
14763 y_to_move = max (it.last_visible_y,
14764 max (scroll_max, 10 * frame_line_height));
14765 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14766 y_to_move, -1,
14767 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14768 dy = it.current_y - y0;
14769 if (dy > scroll_max
14770 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14771 return SCROLLING_FAILED;
14773 /* Additional scroll for when ZV was too close to point. */
14774 dy += y_offset;
14776 /* Compute new window start. */
14777 start_display (&it, w, startp);
14779 if (arg_scroll_conservatively)
14780 amount_to_scroll = max (dy, frame_line_height *
14781 max (scroll_step, temp_scroll_step));
14782 else if (scroll_step || temp_scroll_step)
14783 amount_to_scroll = scroll_max;
14784 else
14786 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14787 height = WINDOW_BOX_TEXT_HEIGHT (w);
14788 if (NUMBERP (aggressive))
14790 double float_amount = XFLOATINT (aggressive) * height;
14791 int aggressive_scroll = float_amount;
14792 if (aggressive_scroll == 0 && float_amount > 0)
14793 aggressive_scroll = 1;
14794 /* Don't let point enter the scroll margin near
14795 bottom of the window, if the value of
14796 scroll_down_aggressively happens to be too
14797 large. */
14798 if (aggressive_scroll + 2*this_scroll_margin > height)
14799 aggressive_scroll = height - 2*this_scroll_margin;
14800 amount_to_scroll = dy + aggressive_scroll;
14804 if (amount_to_scroll <= 0)
14805 return SCROLLING_FAILED;
14807 move_it_vertically_backward (&it, amount_to_scroll);
14808 startp = it.current.pos;
14812 /* Run window scroll functions. */
14813 startp = run_window_scroll_functions (window, startp);
14815 /* Display the window. Give up if new fonts are loaded, or if point
14816 doesn't appear. */
14817 if (!try_window (window, startp, 0))
14818 rc = SCROLLING_NEED_LARGER_MATRICES;
14819 else if (w->cursor.vpos < 0)
14821 clear_glyph_matrix (w->desired_matrix);
14822 rc = SCROLLING_FAILED;
14824 else
14826 /* Maybe forget recorded base line for line number display. */
14827 if (!just_this_one_p
14828 || current_buffer->clip_changed
14829 || BEG_UNCHANGED < CHARPOS (startp))
14830 w->base_line_number = 0;
14832 /* If cursor ends up on a partially visible line,
14833 treat that as being off the bottom of the screen. */
14834 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14835 /* It's possible that the cursor is on the first line of the
14836 buffer, which is partially obscured due to a vscroll
14837 (Bug#7537). In that case, avoid looping forever . */
14838 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14840 clear_glyph_matrix (w->desired_matrix);
14841 ++extra_scroll_margin_lines;
14842 goto too_near_end;
14844 rc = SCROLLING_SUCCESS;
14847 return rc;
14851 /* Compute a suitable window start for window W if display of W starts
14852 on a continuation line. Value is non-zero if a new window start
14853 was computed.
14855 The new window start will be computed, based on W's width, starting
14856 from the start of the continued line. It is the start of the
14857 screen line with the minimum distance from the old start W->start. */
14859 static int
14860 compute_window_start_on_continuation_line (struct window *w)
14862 struct text_pos pos, start_pos;
14863 int window_start_changed_p = 0;
14865 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14867 /* If window start is on a continuation line... Window start may be
14868 < BEGV in case there's invisible text at the start of the
14869 buffer (M-x rmail, for example). */
14870 if (CHARPOS (start_pos) > BEGV
14871 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14873 struct it it;
14874 struct glyph_row *row;
14876 /* Handle the case that the window start is out of range. */
14877 if (CHARPOS (start_pos) < BEGV)
14878 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14879 else if (CHARPOS (start_pos) > ZV)
14880 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14882 /* Find the start of the continued line. This should be fast
14883 because find_newline is fast (newline cache). */
14884 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14885 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14886 row, DEFAULT_FACE_ID);
14887 reseat_at_previous_visible_line_start (&it);
14889 /* If the line start is "too far" away from the window start,
14890 say it takes too much time to compute a new window start. */
14891 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14892 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14894 int min_distance, distance;
14896 /* Move forward by display lines to find the new window
14897 start. If window width was enlarged, the new start can
14898 be expected to be > the old start. If window width was
14899 decreased, the new window start will be < the old start.
14900 So, we're looking for the display line start with the
14901 minimum distance from the old window start. */
14902 pos = it.current.pos;
14903 min_distance = INFINITY;
14904 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14905 distance < min_distance)
14907 min_distance = distance;
14908 pos = it.current.pos;
14909 if (it.line_wrap == WORD_WRAP)
14911 /* Under WORD_WRAP, move_it_by_lines is likely to
14912 overshoot and stop not at the first, but the
14913 second character from the left margin. So in
14914 that case, we need a more tight control on the X
14915 coordinate of the iterator than move_it_by_lines
14916 promises in its contract. The method is to first
14917 go to the last (rightmost) visible character of a
14918 line, then move to the leftmost character on the
14919 next line in a separate call. */
14920 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14921 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14922 move_it_to (&it, ZV, 0,
14923 it.current_y + it.max_ascent + it.max_descent, -1,
14924 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14926 else
14927 move_it_by_lines (&it, 1);
14930 /* Set the window start there. */
14931 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14932 window_start_changed_p = 1;
14936 return window_start_changed_p;
14940 /* Try cursor movement in case text has not changed in window WINDOW,
14941 with window start STARTP. Value is
14943 CURSOR_MOVEMENT_SUCCESS if successful
14945 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14947 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14948 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14949 we want to scroll as if scroll-step were set to 1. See the code.
14951 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14952 which case we have to abort this redisplay, and adjust matrices
14953 first. */
14955 enum
14957 CURSOR_MOVEMENT_SUCCESS,
14958 CURSOR_MOVEMENT_CANNOT_BE_USED,
14959 CURSOR_MOVEMENT_MUST_SCROLL,
14960 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14963 static int
14964 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14966 struct window *w = XWINDOW (window);
14967 struct frame *f = XFRAME (w->frame);
14968 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14970 #ifdef GLYPH_DEBUG
14971 if (inhibit_try_cursor_movement)
14972 return rc;
14973 #endif
14975 /* Previously, there was a check for Lisp integer in the
14976 if-statement below. Now, this field is converted to
14977 ptrdiff_t, thus zero means invalid position in a buffer. */
14978 eassert (w->last_point > 0);
14979 /* Likewise there was a check whether window_end_vpos is nil or larger
14980 than the window. Now window_end_vpos is int and so never nil, but
14981 let's leave eassert to check whether it fits in the window. */
14982 eassert (w->window_end_vpos < w->current_matrix->nrows);
14984 /* Handle case where text has not changed, only point, and it has
14985 not moved off the frame. */
14986 if (/* Point may be in this window. */
14987 PT >= CHARPOS (startp)
14988 /* Selective display hasn't changed. */
14989 && !current_buffer->clip_changed
14990 /* Function force-mode-line-update is used to force a thorough
14991 redisplay. It sets either windows_or_buffers_changed or
14992 update_mode_lines. So don't take a shortcut here for these
14993 cases. */
14994 && !update_mode_lines
14995 && !windows_or_buffers_changed
14996 && !cursor_type_changed
14997 /* Can't use this case if highlighting a region. When a
14998 region exists, cursor movement has to do more than just
14999 set the cursor. */
15000 && markpos_of_region () < 0
15001 && !w->region_showing
15002 && NILP (Vshow_trailing_whitespace)
15003 /* This code is not used for mini-buffer for the sake of the case
15004 of redisplaying to replace an echo area message; since in
15005 that case the mini-buffer contents per se are usually
15006 unchanged. This code is of no real use in the mini-buffer
15007 since the handling of this_line_start_pos, etc., in redisplay
15008 handles the same cases. */
15009 && !EQ (window, minibuf_window)
15010 && (FRAME_WINDOW_P (f)
15011 || !overlay_arrow_in_current_buffer_p ()))
15013 int this_scroll_margin, top_scroll_margin;
15014 struct glyph_row *row = NULL;
15015 int frame_line_height = default_line_pixel_height (w);
15016 int window_total_lines
15017 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15019 #ifdef GLYPH_DEBUG
15020 debug_method_add (w, "cursor movement");
15021 #endif
15023 /* Scroll if point within this distance from the top or bottom
15024 of the window. This is a pixel value. */
15025 if (scroll_margin > 0)
15027 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15028 this_scroll_margin *= frame_line_height;
15030 else
15031 this_scroll_margin = 0;
15033 top_scroll_margin = this_scroll_margin;
15034 if (WINDOW_WANTS_HEADER_LINE_P (w))
15035 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15037 /* Start with the row the cursor was displayed during the last
15038 not paused redisplay. Give up if that row is not valid. */
15039 if (w->last_cursor_vpos < 0
15040 || w->last_cursor_vpos >= w->current_matrix->nrows)
15041 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15042 else
15044 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15045 if (row->mode_line_p)
15046 ++row;
15047 if (!row->enabled_p)
15048 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15051 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15053 int scroll_p = 0, must_scroll = 0;
15054 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15056 if (PT > w->last_point)
15058 /* Point has moved forward. */
15059 while (MATRIX_ROW_END_CHARPOS (row) < PT
15060 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15062 eassert (row->enabled_p);
15063 ++row;
15066 /* If the end position of a row equals the start
15067 position of the next row, and PT is at that position,
15068 we would rather display cursor in the next line. */
15069 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15070 && MATRIX_ROW_END_CHARPOS (row) == PT
15071 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15072 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15073 && !cursor_row_p (row))
15074 ++row;
15076 /* If within the scroll margin, scroll. Note that
15077 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15078 the next line would be drawn, and that
15079 this_scroll_margin can be zero. */
15080 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15081 || PT > MATRIX_ROW_END_CHARPOS (row)
15082 /* Line is completely visible last line in window
15083 and PT is to be set in the next line. */
15084 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15085 && PT == MATRIX_ROW_END_CHARPOS (row)
15086 && !row->ends_at_zv_p
15087 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15088 scroll_p = 1;
15090 else if (PT < w->last_point)
15092 /* Cursor has to be moved backward. Note that PT >=
15093 CHARPOS (startp) because of the outer if-statement. */
15094 while (!row->mode_line_p
15095 && (MATRIX_ROW_START_CHARPOS (row) > PT
15096 || (MATRIX_ROW_START_CHARPOS (row) == PT
15097 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15098 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15099 row > w->current_matrix->rows
15100 && (row-1)->ends_in_newline_from_string_p))))
15101 && (row->y > top_scroll_margin
15102 || CHARPOS (startp) == BEGV))
15104 eassert (row->enabled_p);
15105 --row;
15108 /* Consider the following case: Window starts at BEGV,
15109 there is invisible, intangible text at BEGV, so that
15110 display starts at some point START > BEGV. It can
15111 happen that we are called with PT somewhere between
15112 BEGV and START. Try to handle that case. */
15113 if (row < w->current_matrix->rows
15114 || row->mode_line_p)
15116 row = w->current_matrix->rows;
15117 if (row->mode_line_p)
15118 ++row;
15121 /* Due to newlines in overlay strings, we may have to
15122 skip forward over overlay strings. */
15123 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15124 && MATRIX_ROW_END_CHARPOS (row) == PT
15125 && !cursor_row_p (row))
15126 ++row;
15128 /* If within the scroll margin, scroll. */
15129 if (row->y < top_scroll_margin
15130 && CHARPOS (startp) != BEGV)
15131 scroll_p = 1;
15133 else
15135 /* Cursor did not move. So don't scroll even if cursor line
15136 is partially visible, as it was so before. */
15137 rc = CURSOR_MOVEMENT_SUCCESS;
15140 if (PT < MATRIX_ROW_START_CHARPOS (row)
15141 || PT > MATRIX_ROW_END_CHARPOS (row))
15143 /* if PT is not in the glyph row, give up. */
15144 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15145 must_scroll = 1;
15147 else if (rc != CURSOR_MOVEMENT_SUCCESS
15148 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15150 struct glyph_row *row1;
15152 /* If rows are bidi-reordered and point moved, back up
15153 until we find a row that does not belong to a
15154 continuation line. This is because we must consider
15155 all rows of a continued line as candidates for the
15156 new cursor positioning, since row start and end
15157 positions change non-linearly with vertical position
15158 in such rows. */
15159 /* FIXME: Revisit this when glyph ``spilling'' in
15160 continuation lines' rows is implemented for
15161 bidi-reordered rows. */
15162 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15163 MATRIX_ROW_CONTINUATION_LINE_P (row);
15164 --row)
15166 /* If we hit the beginning of the displayed portion
15167 without finding the first row of a continued
15168 line, give up. */
15169 if (row <= row1)
15171 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15172 break;
15174 eassert (row->enabled_p);
15177 if (must_scroll)
15179 else if (rc != CURSOR_MOVEMENT_SUCCESS
15180 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15181 /* Make sure this isn't a header line by any chance, since
15182 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15183 && !row->mode_line_p
15184 && make_cursor_line_fully_visible_p)
15186 if (PT == MATRIX_ROW_END_CHARPOS (row)
15187 && !row->ends_at_zv_p
15188 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15189 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15190 else if (row->height > window_box_height (w))
15192 /* If we end up in a partially visible line, let's
15193 make it fully visible, except when it's taller
15194 than the window, in which case we can't do much
15195 about it. */
15196 *scroll_step = 1;
15197 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15199 else
15201 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15202 if (!cursor_row_fully_visible_p (w, 0, 1))
15203 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15204 else
15205 rc = CURSOR_MOVEMENT_SUCCESS;
15208 else if (scroll_p)
15209 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15210 else if (rc != CURSOR_MOVEMENT_SUCCESS
15211 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15213 /* With bidi-reordered rows, there could be more than
15214 one candidate row whose start and end positions
15215 occlude point. We need to let set_cursor_from_row
15216 find the best candidate. */
15217 /* FIXME: Revisit this when glyph ``spilling'' in
15218 continuation lines' rows is implemented for
15219 bidi-reordered rows. */
15220 int rv = 0;
15224 int at_zv_p = 0, exact_match_p = 0;
15226 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15227 && PT <= MATRIX_ROW_END_CHARPOS (row)
15228 && cursor_row_p (row))
15229 rv |= set_cursor_from_row (w, row, w->current_matrix,
15230 0, 0, 0, 0);
15231 /* As soon as we've found the exact match for point,
15232 or the first suitable row whose ends_at_zv_p flag
15233 is set, we are done. */
15234 at_zv_p =
15235 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15236 if (rv && !at_zv_p
15237 && w->cursor.hpos >= 0
15238 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15239 w->cursor.vpos))
15241 struct glyph_row *candidate =
15242 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15243 struct glyph *g =
15244 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15245 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15247 exact_match_p =
15248 (BUFFERP (g->object) && g->charpos == PT)
15249 || (INTEGERP (g->object)
15250 && (g->charpos == PT
15251 || (g->charpos == 0 && endpos - 1 == PT)));
15253 if (rv && (at_zv_p || exact_match_p))
15255 rc = CURSOR_MOVEMENT_SUCCESS;
15256 break;
15258 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15259 break;
15260 ++row;
15262 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15263 || row->continued_p)
15264 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15265 || (MATRIX_ROW_START_CHARPOS (row) == PT
15266 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15267 /* If we didn't find any candidate rows, or exited the
15268 loop before all the candidates were examined, signal
15269 to the caller that this method failed. */
15270 if (rc != CURSOR_MOVEMENT_SUCCESS
15271 && !(rv
15272 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15273 && !row->continued_p))
15274 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15275 else if (rv)
15276 rc = CURSOR_MOVEMENT_SUCCESS;
15278 else
15282 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15284 rc = CURSOR_MOVEMENT_SUCCESS;
15285 break;
15287 ++row;
15289 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15290 && MATRIX_ROW_START_CHARPOS (row) == PT
15291 && cursor_row_p (row));
15296 return rc;
15299 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15300 static
15301 #endif
15302 void
15303 set_vertical_scroll_bar (struct window *w)
15305 ptrdiff_t start, end, whole;
15307 /* Calculate the start and end positions for the current window.
15308 At some point, it would be nice to choose between scrollbars
15309 which reflect the whole buffer size, with special markers
15310 indicating narrowing, and scrollbars which reflect only the
15311 visible region.
15313 Note that mini-buffers sometimes aren't displaying any text. */
15314 if (!MINI_WINDOW_P (w)
15315 || (w == XWINDOW (minibuf_window)
15316 && NILP (echo_area_buffer[0])))
15318 struct buffer *buf = XBUFFER (w->contents);
15319 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15320 start = marker_position (w->start) - BUF_BEGV (buf);
15321 /* I don't think this is guaranteed to be right. For the
15322 moment, we'll pretend it is. */
15323 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15325 if (end < start)
15326 end = start;
15327 if (whole < (end - start))
15328 whole = end - start;
15330 else
15331 start = end = whole = 0;
15333 /* Indicate what this scroll bar ought to be displaying now. */
15334 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15335 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15336 (w, end - start, whole, start);
15340 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15341 selected_window is redisplayed.
15343 We can return without actually redisplaying the window if
15344 fonts_changed_p. In that case, redisplay_internal will
15345 retry. */
15347 static void
15348 redisplay_window (Lisp_Object window, int just_this_one_p)
15350 struct window *w = XWINDOW (window);
15351 struct frame *f = XFRAME (w->frame);
15352 struct buffer *buffer = XBUFFER (w->contents);
15353 struct buffer *old = current_buffer;
15354 struct text_pos lpoint, opoint, startp;
15355 int update_mode_line;
15356 int tem;
15357 struct it it;
15358 /* Record it now because it's overwritten. */
15359 int current_matrix_up_to_date_p = 0;
15360 int used_current_matrix_p = 0;
15361 /* This is less strict than current_matrix_up_to_date_p.
15362 It indicates that the buffer contents and narrowing are unchanged. */
15363 int buffer_unchanged_p = 0;
15364 int temp_scroll_step = 0;
15365 ptrdiff_t count = SPECPDL_INDEX ();
15366 int rc;
15367 int centering_position = -1;
15368 int last_line_misfit = 0;
15369 ptrdiff_t beg_unchanged, end_unchanged;
15370 int frame_line_height;
15372 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15373 opoint = lpoint;
15375 #ifdef GLYPH_DEBUG
15376 *w->desired_matrix->method = 0;
15377 #endif
15379 /* Make sure that both W's markers are valid. */
15380 eassert (XMARKER (w->start)->buffer == buffer);
15381 eassert (XMARKER (w->pointm)->buffer == buffer);
15383 restart:
15384 reconsider_clip_changes (w);
15385 frame_line_height = default_line_pixel_height (w);
15387 /* Has the mode line to be updated? */
15388 update_mode_line = (w->update_mode_line
15389 || update_mode_lines
15390 || buffer->clip_changed
15391 || buffer->prevent_redisplay_optimizations_p);
15393 if (MINI_WINDOW_P (w))
15395 if (w == XWINDOW (echo_area_window)
15396 && !NILP (echo_area_buffer[0]))
15398 if (update_mode_line)
15399 /* We may have to update a tty frame's menu bar or a
15400 tool-bar. Example `M-x C-h C-h C-g'. */
15401 goto finish_menu_bars;
15402 else
15403 /* We've already displayed the echo area glyphs in this window. */
15404 goto finish_scroll_bars;
15406 else if ((w != XWINDOW (minibuf_window)
15407 || minibuf_level == 0)
15408 /* When buffer is nonempty, redisplay window normally. */
15409 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15410 /* Quail displays non-mini buffers in minibuffer window.
15411 In that case, redisplay the window normally. */
15412 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15414 /* W is a mini-buffer window, but it's not active, so clear
15415 it. */
15416 int yb = window_text_bottom_y (w);
15417 struct glyph_row *row;
15418 int y;
15420 for (y = 0, row = w->desired_matrix->rows;
15421 y < yb;
15422 y += row->height, ++row)
15423 blank_row (w, row, y);
15424 goto finish_scroll_bars;
15427 clear_glyph_matrix (w->desired_matrix);
15430 /* Otherwise set up data on this window; select its buffer and point
15431 value. */
15432 /* Really select the buffer, for the sake of buffer-local
15433 variables. */
15434 set_buffer_internal_1 (XBUFFER (w->contents));
15436 current_matrix_up_to_date_p
15437 = (w->window_end_valid
15438 && !current_buffer->clip_changed
15439 && !current_buffer->prevent_redisplay_optimizations_p
15440 && !window_outdated (w));
15442 /* Run the window-bottom-change-functions
15443 if it is possible that the text on the screen has changed
15444 (either due to modification of the text, or any other reason). */
15445 if (!current_matrix_up_to_date_p
15446 && !NILP (Vwindow_text_change_functions))
15448 safe_run_hooks (Qwindow_text_change_functions);
15449 goto restart;
15452 beg_unchanged = BEG_UNCHANGED;
15453 end_unchanged = END_UNCHANGED;
15455 SET_TEXT_POS (opoint, PT, PT_BYTE);
15457 specbind (Qinhibit_point_motion_hooks, Qt);
15459 buffer_unchanged_p
15460 = (w->window_end_valid
15461 && !current_buffer->clip_changed
15462 && !window_outdated (w));
15464 /* When windows_or_buffers_changed is non-zero, we can't rely
15465 on the window end being valid, so set it to zero there. */
15466 if (windows_or_buffers_changed)
15468 /* If window starts on a continuation line, maybe adjust the
15469 window start in case the window's width changed. */
15470 if (XMARKER (w->start)->buffer == current_buffer)
15471 compute_window_start_on_continuation_line (w);
15473 w->window_end_valid = 0;
15474 /* If so, we also can't rely on current matrix
15475 and should not fool try_cursor_movement below. */
15476 current_matrix_up_to_date_p = 0;
15479 /* Some sanity checks. */
15480 CHECK_WINDOW_END (w);
15481 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15482 emacs_abort ();
15483 if (BYTEPOS (opoint) < CHARPOS (opoint))
15484 emacs_abort ();
15486 if (mode_line_update_needed (w))
15487 update_mode_line = 1;
15489 /* Point refers normally to the selected window. For any other
15490 window, set up appropriate value. */
15491 if (!EQ (window, selected_window))
15493 ptrdiff_t new_pt = marker_position (w->pointm);
15494 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15495 if (new_pt < BEGV)
15497 new_pt = BEGV;
15498 new_pt_byte = BEGV_BYTE;
15499 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15501 else if (new_pt > (ZV - 1))
15503 new_pt = ZV;
15504 new_pt_byte = ZV_BYTE;
15505 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15508 /* We don't use SET_PT so that the point-motion hooks don't run. */
15509 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15512 /* If any of the character widths specified in the display table
15513 have changed, invalidate the width run cache. It's true that
15514 this may be a bit late to catch such changes, but the rest of
15515 redisplay goes (non-fatally) haywire when the display table is
15516 changed, so why should we worry about doing any better? */
15517 if (current_buffer->width_run_cache)
15519 struct Lisp_Char_Table *disptab = buffer_display_table ();
15521 if (! disptab_matches_widthtab
15522 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15524 invalidate_region_cache (current_buffer,
15525 current_buffer->width_run_cache,
15526 BEG, Z);
15527 recompute_width_table (current_buffer, disptab);
15531 /* If window-start is screwed up, choose a new one. */
15532 if (XMARKER (w->start)->buffer != current_buffer)
15533 goto recenter;
15535 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15537 /* If someone specified a new starting point but did not insist,
15538 check whether it can be used. */
15539 if (w->optional_new_start
15540 && CHARPOS (startp) >= BEGV
15541 && CHARPOS (startp) <= ZV)
15543 w->optional_new_start = 0;
15544 start_display (&it, w, startp);
15545 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15546 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15547 if (IT_CHARPOS (it) == PT)
15548 w->force_start = 1;
15549 /* IT may overshoot PT if text at PT is invisible. */
15550 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15551 w->force_start = 1;
15554 force_start:
15556 /* Handle case where place to start displaying has been specified,
15557 unless the specified location is outside the accessible range. */
15558 if (w->force_start || window_frozen_p (w))
15560 /* We set this later on if we have to adjust point. */
15561 int new_vpos = -1;
15563 w->force_start = 0;
15564 w->vscroll = 0;
15565 w->window_end_valid = 0;
15567 /* Forget any recorded base line for line number display. */
15568 if (!buffer_unchanged_p)
15569 w->base_line_number = 0;
15571 /* Redisplay the mode line. Select the buffer properly for that.
15572 Also, run the hook window-scroll-functions
15573 because we have scrolled. */
15574 /* Note, we do this after clearing force_start because
15575 if there's an error, it is better to forget about force_start
15576 than to get into an infinite loop calling the hook functions
15577 and having them get more errors. */
15578 if (!update_mode_line
15579 || ! NILP (Vwindow_scroll_functions))
15581 update_mode_line = 1;
15582 w->update_mode_line = 1;
15583 startp = run_window_scroll_functions (window, startp);
15586 if (CHARPOS (startp) < BEGV)
15587 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15588 else if (CHARPOS (startp) > ZV)
15589 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15591 /* Redisplay, then check if cursor has been set during the
15592 redisplay. Give up if new fonts were loaded. */
15593 /* We used to issue a CHECK_MARGINS argument to try_window here,
15594 but this causes scrolling to fail when point begins inside
15595 the scroll margin (bug#148) -- cyd */
15596 if (!try_window (window, startp, 0))
15598 w->force_start = 1;
15599 clear_glyph_matrix (w->desired_matrix);
15600 goto need_larger_matrices;
15603 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15605 /* If point does not appear, try to move point so it does
15606 appear. The desired matrix has been built above, so we
15607 can use it here. */
15608 new_vpos = window_box_height (w) / 2;
15611 if (!cursor_row_fully_visible_p (w, 0, 0))
15613 /* Point does appear, but on a line partly visible at end of window.
15614 Move it back to a fully-visible line. */
15615 new_vpos = window_box_height (w);
15617 else if (w->cursor.vpos >=0)
15619 /* Some people insist on not letting point enter the scroll
15620 margin, even though this part handles windows that didn't
15621 scroll at all. */
15622 int window_total_lines
15623 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15624 int margin = min (scroll_margin, window_total_lines / 4);
15625 int pixel_margin = margin * frame_line_height;
15626 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15628 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15629 below, which finds the row to move point to, advances by
15630 the Y coordinate of the _next_ row, see the definition of
15631 MATRIX_ROW_BOTTOM_Y. */
15632 if (w->cursor.vpos < margin + header_line)
15634 w->cursor.vpos = -1;
15635 clear_glyph_matrix (w->desired_matrix);
15636 goto try_to_scroll;
15638 else
15640 int window_height = window_box_height (w);
15642 if (header_line)
15643 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15644 if (w->cursor.y >= window_height - pixel_margin)
15646 w->cursor.vpos = -1;
15647 clear_glyph_matrix (w->desired_matrix);
15648 goto try_to_scroll;
15653 /* If we need to move point for either of the above reasons,
15654 now actually do it. */
15655 if (new_vpos >= 0)
15657 struct glyph_row *row;
15659 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15660 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15661 ++row;
15663 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15664 MATRIX_ROW_START_BYTEPOS (row));
15666 if (w != XWINDOW (selected_window))
15667 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15668 else if (current_buffer == old)
15669 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15671 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15673 /* If we are highlighting the region, then we just changed
15674 the region, so redisplay to show it. */
15675 if (markpos_of_region () >= 0)
15677 clear_glyph_matrix (w->desired_matrix);
15678 if (!try_window (window, startp, 0))
15679 goto need_larger_matrices;
15683 #ifdef GLYPH_DEBUG
15684 debug_method_add (w, "forced window start");
15685 #endif
15686 goto done;
15689 /* Handle case where text has not changed, only point, and it has
15690 not moved off the frame, and we are not retrying after hscroll.
15691 (current_matrix_up_to_date_p is nonzero when retrying.) */
15692 if (current_matrix_up_to_date_p
15693 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15694 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15696 switch (rc)
15698 case CURSOR_MOVEMENT_SUCCESS:
15699 used_current_matrix_p = 1;
15700 goto done;
15702 case CURSOR_MOVEMENT_MUST_SCROLL:
15703 goto try_to_scroll;
15705 default:
15706 emacs_abort ();
15709 /* If current starting point was originally the beginning of a line
15710 but no longer is, find a new starting point. */
15711 else if (w->start_at_line_beg
15712 && !(CHARPOS (startp) <= BEGV
15713 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15715 #ifdef GLYPH_DEBUG
15716 debug_method_add (w, "recenter 1");
15717 #endif
15718 goto recenter;
15721 /* Try scrolling with try_window_id. Value is > 0 if update has
15722 been done, it is -1 if we know that the same window start will
15723 not work. It is 0 if unsuccessful for some other reason. */
15724 else if ((tem = try_window_id (w)) != 0)
15726 #ifdef GLYPH_DEBUG
15727 debug_method_add (w, "try_window_id %d", tem);
15728 #endif
15730 if (fonts_changed_p)
15731 goto need_larger_matrices;
15732 if (tem > 0)
15733 goto done;
15735 /* Otherwise try_window_id has returned -1 which means that we
15736 don't want the alternative below this comment to execute. */
15738 else if (CHARPOS (startp) >= BEGV
15739 && CHARPOS (startp) <= ZV
15740 && PT >= CHARPOS (startp)
15741 && (CHARPOS (startp) < ZV
15742 /* Avoid starting at end of buffer. */
15743 || CHARPOS (startp) == BEGV
15744 || !window_outdated (w)))
15746 int d1, d2, d3, d4, d5, d6;
15748 /* If first window line is a continuation line, and window start
15749 is inside the modified region, but the first change is before
15750 current window start, we must select a new window start.
15752 However, if this is the result of a down-mouse event (e.g. by
15753 extending the mouse-drag-overlay), we don't want to select a
15754 new window start, since that would change the position under
15755 the mouse, resulting in an unwanted mouse-movement rather
15756 than a simple mouse-click. */
15757 if (!w->start_at_line_beg
15758 && NILP (do_mouse_tracking)
15759 && CHARPOS (startp) > BEGV
15760 && CHARPOS (startp) > BEG + beg_unchanged
15761 && CHARPOS (startp) <= Z - end_unchanged
15762 /* Even if w->start_at_line_beg is nil, a new window may
15763 start at a line_beg, since that's how set_buffer_window
15764 sets it. So, we need to check the return value of
15765 compute_window_start_on_continuation_line. (See also
15766 bug#197). */
15767 && XMARKER (w->start)->buffer == current_buffer
15768 && compute_window_start_on_continuation_line (w)
15769 /* It doesn't make sense to force the window start like we
15770 do at label force_start if it is already known that point
15771 will not be visible in the resulting window, because
15772 doing so will move point from its correct position
15773 instead of scrolling the window to bring point into view.
15774 See bug#9324. */
15775 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15777 w->force_start = 1;
15778 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15779 goto force_start;
15782 #ifdef GLYPH_DEBUG
15783 debug_method_add (w, "same window start");
15784 #endif
15786 /* Try to redisplay starting at same place as before.
15787 If point has not moved off frame, accept the results. */
15788 if (!current_matrix_up_to_date_p
15789 /* Don't use try_window_reusing_current_matrix in this case
15790 because a window scroll function can have changed the
15791 buffer. */
15792 || !NILP (Vwindow_scroll_functions)
15793 || MINI_WINDOW_P (w)
15794 || !(used_current_matrix_p
15795 = try_window_reusing_current_matrix (w)))
15797 IF_DEBUG (debug_method_add (w, "1"));
15798 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15799 /* -1 means we need to scroll.
15800 0 means we need new matrices, but fonts_changed_p
15801 is set in that case, so we will detect it below. */
15802 goto try_to_scroll;
15805 if (fonts_changed_p)
15806 goto need_larger_matrices;
15808 if (w->cursor.vpos >= 0)
15810 if (!just_this_one_p
15811 || current_buffer->clip_changed
15812 || BEG_UNCHANGED < CHARPOS (startp))
15813 /* Forget any recorded base line for line number display. */
15814 w->base_line_number = 0;
15816 if (!cursor_row_fully_visible_p (w, 1, 0))
15818 clear_glyph_matrix (w->desired_matrix);
15819 last_line_misfit = 1;
15821 /* Drop through and scroll. */
15822 else
15823 goto done;
15825 else
15826 clear_glyph_matrix (w->desired_matrix);
15829 try_to_scroll:
15831 /* Redisplay the mode line. Select the buffer properly for that. */
15832 if (!update_mode_line)
15834 update_mode_line = 1;
15835 w->update_mode_line = 1;
15838 /* Try to scroll by specified few lines. */
15839 if ((scroll_conservatively
15840 || emacs_scroll_step
15841 || temp_scroll_step
15842 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15843 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15844 && CHARPOS (startp) >= BEGV
15845 && CHARPOS (startp) <= ZV)
15847 /* The function returns -1 if new fonts were loaded, 1 if
15848 successful, 0 if not successful. */
15849 int ss = try_scrolling (window, just_this_one_p,
15850 scroll_conservatively,
15851 emacs_scroll_step,
15852 temp_scroll_step, last_line_misfit);
15853 switch (ss)
15855 case SCROLLING_SUCCESS:
15856 goto done;
15858 case SCROLLING_NEED_LARGER_MATRICES:
15859 goto need_larger_matrices;
15861 case SCROLLING_FAILED:
15862 break;
15864 default:
15865 emacs_abort ();
15869 /* Finally, just choose a place to start which positions point
15870 according to user preferences. */
15872 recenter:
15874 #ifdef GLYPH_DEBUG
15875 debug_method_add (w, "recenter");
15876 #endif
15878 /* Forget any previously recorded base line for line number display. */
15879 if (!buffer_unchanged_p)
15880 w->base_line_number = 0;
15882 /* Determine the window start relative to point. */
15883 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15884 it.current_y = it.last_visible_y;
15885 if (centering_position < 0)
15887 int window_total_lines
15888 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15889 int margin =
15890 scroll_margin > 0
15891 ? min (scroll_margin, window_total_lines / 4)
15892 : 0;
15893 ptrdiff_t margin_pos = CHARPOS (startp);
15894 Lisp_Object aggressive;
15895 int scrolling_up;
15897 /* If there is a scroll margin at the top of the window, find
15898 its character position. */
15899 if (margin
15900 /* Cannot call start_display if startp is not in the
15901 accessible region of the buffer. This can happen when we
15902 have just switched to a different buffer and/or changed
15903 its restriction. In that case, startp is initialized to
15904 the character position 1 (BEGV) because we did not yet
15905 have chance to display the buffer even once. */
15906 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15908 struct it it1;
15909 void *it1data = NULL;
15911 SAVE_IT (it1, it, it1data);
15912 start_display (&it1, w, startp);
15913 move_it_vertically (&it1, margin * frame_line_height);
15914 margin_pos = IT_CHARPOS (it1);
15915 RESTORE_IT (&it, &it, it1data);
15917 scrolling_up = PT > margin_pos;
15918 aggressive =
15919 scrolling_up
15920 ? BVAR (current_buffer, scroll_up_aggressively)
15921 : BVAR (current_buffer, scroll_down_aggressively);
15923 if (!MINI_WINDOW_P (w)
15924 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15926 int pt_offset = 0;
15928 /* Setting scroll-conservatively overrides
15929 scroll-*-aggressively. */
15930 if (!scroll_conservatively && NUMBERP (aggressive))
15932 double float_amount = XFLOATINT (aggressive);
15934 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15935 if (pt_offset == 0 && float_amount > 0)
15936 pt_offset = 1;
15937 if (pt_offset && margin > 0)
15938 margin -= 1;
15940 /* Compute how much to move the window start backward from
15941 point so that point will be displayed where the user
15942 wants it. */
15943 if (scrolling_up)
15945 centering_position = it.last_visible_y;
15946 if (pt_offset)
15947 centering_position -= pt_offset;
15948 centering_position -=
15949 frame_line_height * (1 + margin + (last_line_misfit != 0))
15950 + WINDOW_HEADER_LINE_HEIGHT (w);
15951 /* Don't let point enter the scroll margin near top of
15952 the window. */
15953 if (centering_position < margin * frame_line_height)
15954 centering_position = margin * frame_line_height;
15956 else
15957 centering_position = margin * frame_line_height + pt_offset;
15959 else
15960 /* Set the window start half the height of the window backward
15961 from point. */
15962 centering_position = window_box_height (w) / 2;
15964 move_it_vertically_backward (&it, centering_position);
15966 eassert (IT_CHARPOS (it) >= BEGV);
15968 /* The function move_it_vertically_backward may move over more
15969 than the specified y-distance. If it->w is small, e.g. a
15970 mini-buffer window, we may end up in front of the window's
15971 display area. Start displaying at the start of the line
15972 containing PT in this case. */
15973 if (it.current_y <= 0)
15975 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15976 move_it_vertically_backward (&it, 0);
15977 it.current_y = 0;
15980 it.current_x = it.hpos = 0;
15982 /* Set the window start position here explicitly, to avoid an
15983 infinite loop in case the functions in window-scroll-functions
15984 get errors. */
15985 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15987 /* Run scroll hooks. */
15988 startp = run_window_scroll_functions (window, it.current.pos);
15990 /* Redisplay the window. */
15991 if (!current_matrix_up_to_date_p
15992 || windows_or_buffers_changed
15993 || cursor_type_changed
15994 /* Don't use try_window_reusing_current_matrix in this case
15995 because it can have changed the buffer. */
15996 || !NILP (Vwindow_scroll_functions)
15997 || !just_this_one_p
15998 || MINI_WINDOW_P (w)
15999 || !(used_current_matrix_p
16000 = try_window_reusing_current_matrix (w)))
16001 try_window (window, startp, 0);
16003 /* If new fonts have been loaded (due to fontsets), give up. We
16004 have to start a new redisplay since we need to re-adjust glyph
16005 matrices. */
16006 if (fonts_changed_p)
16007 goto need_larger_matrices;
16009 /* If cursor did not appear assume that the middle of the window is
16010 in the first line of the window. Do it again with the next line.
16011 (Imagine a window of height 100, displaying two lines of height
16012 60. Moving back 50 from it->last_visible_y will end in the first
16013 line.) */
16014 if (w->cursor.vpos < 0)
16016 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16018 clear_glyph_matrix (w->desired_matrix);
16019 move_it_by_lines (&it, 1);
16020 try_window (window, it.current.pos, 0);
16022 else if (PT < IT_CHARPOS (it))
16024 clear_glyph_matrix (w->desired_matrix);
16025 move_it_by_lines (&it, -1);
16026 try_window (window, it.current.pos, 0);
16028 else
16030 /* Not much we can do about it. */
16034 /* Consider the following case: Window starts at BEGV, there is
16035 invisible, intangible text at BEGV, so that display starts at
16036 some point START > BEGV. It can happen that we are called with
16037 PT somewhere between BEGV and START. Try to handle that case. */
16038 if (w->cursor.vpos < 0)
16040 struct glyph_row *row = w->current_matrix->rows;
16041 if (row->mode_line_p)
16042 ++row;
16043 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16046 if (!cursor_row_fully_visible_p (w, 0, 0))
16048 /* If vscroll is enabled, disable it and try again. */
16049 if (w->vscroll)
16051 w->vscroll = 0;
16052 clear_glyph_matrix (w->desired_matrix);
16053 goto recenter;
16056 /* Users who set scroll-conservatively to a large number want
16057 point just above/below the scroll margin. If we ended up
16058 with point's row partially visible, move the window start to
16059 make that row fully visible and out of the margin. */
16060 if (scroll_conservatively > SCROLL_LIMIT)
16062 int window_total_lines
16063 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16064 int margin =
16065 scroll_margin > 0
16066 ? min (scroll_margin, window_total_lines / 4)
16067 : 0;
16068 int move_down = w->cursor.vpos >= window_total_lines / 2;
16070 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16071 clear_glyph_matrix (w->desired_matrix);
16072 if (1 == try_window (window, it.current.pos,
16073 TRY_WINDOW_CHECK_MARGINS))
16074 goto done;
16077 /* If centering point failed to make the whole line visible,
16078 put point at the top instead. That has to make the whole line
16079 visible, if it can be done. */
16080 if (centering_position == 0)
16081 goto done;
16083 clear_glyph_matrix (w->desired_matrix);
16084 centering_position = 0;
16085 goto recenter;
16088 done:
16090 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16091 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16092 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16094 /* Display the mode line, if we must. */
16095 if ((update_mode_line
16096 /* If window not full width, must redo its mode line
16097 if (a) the window to its side is being redone and
16098 (b) we do a frame-based redisplay. This is a consequence
16099 of how inverted lines are drawn in frame-based redisplay. */
16100 || (!just_this_one_p
16101 && !FRAME_WINDOW_P (f)
16102 && !WINDOW_FULL_WIDTH_P (w))
16103 /* Line number to display. */
16104 || w->base_line_pos > 0
16105 /* Column number is displayed and different from the one displayed. */
16106 || (w->column_number_displayed != -1
16107 && (w->column_number_displayed != current_column ())))
16108 /* This means that the window has a mode line. */
16109 && (WINDOW_WANTS_MODELINE_P (w)
16110 || WINDOW_WANTS_HEADER_LINE_P (w)))
16112 display_mode_lines (w);
16114 /* If mode line height has changed, arrange for a thorough
16115 immediate redisplay using the correct mode line height. */
16116 if (WINDOW_WANTS_MODELINE_P (w)
16117 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16119 fonts_changed_p = 1;
16120 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16121 = DESIRED_MODE_LINE_HEIGHT (w);
16124 /* If header line height has changed, arrange for a thorough
16125 immediate redisplay using the correct header line height. */
16126 if (WINDOW_WANTS_HEADER_LINE_P (w)
16127 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16129 fonts_changed_p = 1;
16130 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16131 = DESIRED_HEADER_LINE_HEIGHT (w);
16134 if (fonts_changed_p)
16135 goto need_larger_matrices;
16138 if (!line_number_displayed && w->base_line_pos != -1)
16140 w->base_line_pos = 0;
16141 w->base_line_number = 0;
16144 finish_menu_bars:
16146 /* When we reach a frame's selected window, redo the frame's menu bar. */
16147 if (update_mode_line
16148 && EQ (FRAME_SELECTED_WINDOW (f), window))
16150 int redisplay_menu_p = 0;
16152 if (FRAME_WINDOW_P (f))
16154 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16155 || defined (HAVE_NS) || defined (USE_GTK)
16156 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16157 #else
16158 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16159 #endif
16161 else
16162 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16164 if (redisplay_menu_p)
16165 display_menu_bar (w);
16167 #ifdef HAVE_WINDOW_SYSTEM
16168 if (FRAME_WINDOW_P (f))
16170 #if defined (USE_GTK) || defined (HAVE_NS)
16171 if (FRAME_EXTERNAL_TOOL_BAR (f))
16172 redisplay_tool_bar (f);
16173 #else
16174 if (WINDOWP (f->tool_bar_window)
16175 && (FRAME_TOOL_BAR_LINES (f) > 0
16176 || !NILP (Vauto_resize_tool_bars))
16177 && redisplay_tool_bar (f))
16178 ignore_mouse_drag_p = 1;
16179 #endif
16181 #endif
16184 #ifdef HAVE_WINDOW_SYSTEM
16185 if (FRAME_WINDOW_P (f)
16186 && update_window_fringes (w, (just_this_one_p
16187 || (!used_current_matrix_p && !overlay_arrow_seen)
16188 || w->pseudo_window_p)))
16190 update_begin (f);
16191 block_input ();
16192 if (draw_window_fringes (w, 1))
16193 x_draw_vertical_border (w);
16194 unblock_input ();
16195 update_end (f);
16197 #endif /* HAVE_WINDOW_SYSTEM */
16199 /* We go to this label, with fonts_changed_p set,
16200 if it is necessary to try again using larger glyph matrices.
16201 We have to redeem the scroll bar even in this case,
16202 because the loop in redisplay_internal expects that. */
16203 need_larger_matrices:
16205 finish_scroll_bars:
16207 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16209 /* Set the thumb's position and size. */
16210 set_vertical_scroll_bar (w);
16212 /* Note that we actually used the scroll bar attached to this
16213 window, so it shouldn't be deleted at the end of redisplay. */
16214 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16215 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16218 /* Restore current_buffer and value of point in it. The window
16219 update may have changed the buffer, so first make sure `opoint'
16220 is still valid (Bug#6177). */
16221 if (CHARPOS (opoint) < BEGV)
16222 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16223 else if (CHARPOS (opoint) > ZV)
16224 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16225 else
16226 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16228 set_buffer_internal_1 (old);
16229 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16230 shorter. This can be caused by log truncation in *Messages*. */
16231 if (CHARPOS (lpoint) <= ZV)
16232 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16234 unbind_to (count, Qnil);
16238 /* Build the complete desired matrix of WINDOW with a window start
16239 buffer position POS.
16241 Value is 1 if successful. It is zero if fonts were loaded during
16242 redisplay which makes re-adjusting glyph matrices necessary, and -1
16243 if point would appear in the scroll margins.
16244 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16245 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16246 set in FLAGS.) */
16249 try_window (Lisp_Object window, struct text_pos pos, int flags)
16251 struct window *w = XWINDOW (window);
16252 struct it it;
16253 struct glyph_row *last_text_row = NULL;
16254 struct frame *f = XFRAME (w->frame);
16255 int frame_line_height = default_line_pixel_height (w);
16257 /* Make POS the new window start. */
16258 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16260 /* Mark cursor position as unknown. No overlay arrow seen. */
16261 w->cursor.vpos = -1;
16262 overlay_arrow_seen = 0;
16264 /* Initialize iterator and info to start at POS. */
16265 start_display (&it, w, pos);
16267 /* Display all lines of W. */
16268 while (it.current_y < it.last_visible_y)
16270 if (display_line (&it))
16271 last_text_row = it.glyph_row - 1;
16272 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16273 return 0;
16276 /* Don't let the cursor end in the scroll margins. */
16277 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16278 && !MINI_WINDOW_P (w))
16280 int this_scroll_margin;
16281 int window_total_lines
16282 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16284 if (scroll_margin > 0)
16286 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16287 this_scroll_margin *= frame_line_height;
16289 else
16290 this_scroll_margin = 0;
16292 if ((w->cursor.y >= 0 /* not vscrolled */
16293 && w->cursor.y < this_scroll_margin
16294 && CHARPOS (pos) > BEGV
16295 && IT_CHARPOS (it) < ZV)
16296 /* rms: considering make_cursor_line_fully_visible_p here
16297 seems to give wrong results. We don't want to recenter
16298 when the last line is partly visible, we want to allow
16299 that case to be handled in the usual way. */
16300 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16302 w->cursor.vpos = -1;
16303 clear_glyph_matrix (w->desired_matrix);
16304 return -1;
16308 /* If bottom moved off end of frame, change mode line percentage. */
16309 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16310 w->update_mode_line = 1;
16312 /* Set window_end_pos to the offset of the last character displayed
16313 on the window from the end of current_buffer. Set
16314 window_end_vpos to its row number. */
16315 if (last_text_row)
16317 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16318 adjust_window_ends (w, last_text_row, 0);
16319 eassert
16320 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16321 w->window_end_vpos)));
16323 else
16325 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16326 w->window_end_pos = Z - ZV;
16327 w->window_end_vpos = 0;
16330 /* But that is not valid info until redisplay finishes. */
16331 w->window_end_valid = 0;
16332 return 1;
16337 /************************************************************************
16338 Window redisplay reusing current matrix when buffer has not changed
16339 ************************************************************************/
16341 /* Try redisplay of window W showing an unchanged buffer with a
16342 different window start than the last time it was displayed by
16343 reusing its current matrix. Value is non-zero if successful.
16344 W->start is the new window start. */
16346 static int
16347 try_window_reusing_current_matrix (struct window *w)
16349 struct frame *f = XFRAME (w->frame);
16350 struct glyph_row *bottom_row;
16351 struct it it;
16352 struct run run;
16353 struct text_pos start, new_start;
16354 int nrows_scrolled, i;
16355 struct glyph_row *last_text_row;
16356 struct glyph_row *last_reused_text_row;
16357 struct glyph_row *start_row;
16358 int start_vpos, min_y, max_y;
16360 #ifdef GLYPH_DEBUG
16361 if (inhibit_try_window_reusing)
16362 return 0;
16363 #endif
16365 if (/* This function doesn't handle terminal frames. */
16366 !FRAME_WINDOW_P (f)
16367 /* Don't try to reuse the display if windows have been split
16368 or such. */
16369 || windows_or_buffers_changed
16370 || cursor_type_changed)
16371 return 0;
16373 /* Can't do this if region may have changed. */
16374 if (markpos_of_region () >= 0
16375 || w->region_showing
16376 || !NILP (Vshow_trailing_whitespace))
16377 return 0;
16379 /* If top-line visibility has changed, give up. */
16380 if (WINDOW_WANTS_HEADER_LINE_P (w)
16381 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16382 return 0;
16384 /* Give up if old or new display is scrolled vertically. We could
16385 make this function handle this, but right now it doesn't. */
16386 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16387 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16388 return 0;
16390 /* The variable new_start now holds the new window start. The old
16391 start `start' can be determined from the current matrix. */
16392 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16393 start = start_row->minpos;
16394 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16396 /* Clear the desired matrix for the display below. */
16397 clear_glyph_matrix (w->desired_matrix);
16399 if (CHARPOS (new_start) <= CHARPOS (start))
16401 /* Don't use this method if the display starts with an ellipsis
16402 displayed for invisible text. It's not easy to handle that case
16403 below, and it's certainly not worth the effort since this is
16404 not a frequent case. */
16405 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16406 return 0;
16408 IF_DEBUG (debug_method_add (w, "twu1"));
16410 /* Display up to a row that can be reused. The variable
16411 last_text_row is set to the last row displayed that displays
16412 text. Note that it.vpos == 0 if or if not there is a
16413 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16414 start_display (&it, w, new_start);
16415 w->cursor.vpos = -1;
16416 last_text_row = last_reused_text_row = NULL;
16418 while (it.current_y < it.last_visible_y
16419 && !fonts_changed_p)
16421 /* If we have reached into the characters in the START row,
16422 that means the line boundaries have changed. So we
16423 can't start copying with the row START. Maybe it will
16424 work to start copying with the following row. */
16425 while (IT_CHARPOS (it) > CHARPOS (start))
16427 /* Advance to the next row as the "start". */
16428 start_row++;
16429 start = start_row->minpos;
16430 /* If there are no more rows to try, or just one, give up. */
16431 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16432 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16433 || CHARPOS (start) == ZV)
16435 clear_glyph_matrix (w->desired_matrix);
16436 return 0;
16439 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16441 /* If we have reached alignment, we can copy the rest of the
16442 rows. */
16443 if (IT_CHARPOS (it) == CHARPOS (start)
16444 /* Don't accept "alignment" inside a display vector,
16445 since start_row could have started in the middle of
16446 that same display vector (thus their character
16447 positions match), and we have no way of telling if
16448 that is the case. */
16449 && it.current.dpvec_index < 0)
16450 break;
16452 if (display_line (&it))
16453 last_text_row = it.glyph_row - 1;
16457 /* A value of current_y < last_visible_y means that we stopped
16458 at the previous window start, which in turn means that we
16459 have at least one reusable row. */
16460 if (it.current_y < it.last_visible_y)
16462 struct glyph_row *row;
16464 /* IT.vpos always starts from 0; it counts text lines. */
16465 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16467 /* Find PT if not already found in the lines displayed. */
16468 if (w->cursor.vpos < 0)
16470 int dy = it.current_y - start_row->y;
16472 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16473 row = row_containing_pos (w, PT, row, NULL, dy);
16474 if (row)
16475 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16476 dy, nrows_scrolled);
16477 else
16479 clear_glyph_matrix (w->desired_matrix);
16480 return 0;
16484 /* Scroll the display. Do it before the current matrix is
16485 changed. The problem here is that update has not yet
16486 run, i.e. part of the current matrix is not up to date.
16487 scroll_run_hook will clear the cursor, and use the
16488 current matrix to get the height of the row the cursor is
16489 in. */
16490 run.current_y = start_row->y;
16491 run.desired_y = it.current_y;
16492 run.height = it.last_visible_y - it.current_y;
16494 if (run.height > 0 && run.current_y != run.desired_y)
16496 update_begin (f);
16497 FRAME_RIF (f)->update_window_begin_hook (w);
16498 FRAME_RIF (f)->clear_window_mouse_face (w);
16499 FRAME_RIF (f)->scroll_run_hook (w, &run);
16500 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16501 update_end (f);
16504 /* Shift current matrix down by nrows_scrolled lines. */
16505 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16506 rotate_matrix (w->current_matrix,
16507 start_vpos,
16508 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16509 nrows_scrolled);
16511 /* Disable lines that must be updated. */
16512 for (i = 0; i < nrows_scrolled; ++i)
16513 (start_row + i)->enabled_p = 0;
16515 /* Re-compute Y positions. */
16516 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16517 max_y = it.last_visible_y;
16518 for (row = start_row + nrows_scrolled;
16519 row < bottom_row;
16520 ++row)
16522 row->y = it.current_y;
16523 row->visible_height = row->height;
16525 if (row->y < min_y)
16526 row->visible_height -= min_y - row->y;
16527 if (row->y + row->height > max_y)
16528 row->visible_height -= row->y + row->height - max_y;
16529 if (row->fringe_bitmap_periodic_p)
16530 row->redraw_fringe_bitmaps_p = 1;
16532 it.current_y += row->height;
16534 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16535 last_reused_text_row = row;
16536 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16537 break;
16540 /* Disable lines in the current matrix which are now
16541 below the window. */
16542 for (++row; row < bottom_row; ++row)
16543 row->enabled_p = row->mode_line_p = 0;
16546 /* Update window_end_pos etc.; last_reused_text_row is the last
16547 reused row from the current matrix containing text, if any.
16548 The value of last_text_row is the last displayed line
16549 containing text. */
16550 if (last_reused_text_row)
16551 adjust_window_ends (w, last_reused_text_row, 1);
16552 else if (last_text_row)
16553 adjust_window_ends (w, last_text_row, 0);
16554 else
16556 /* This window must be completely empty. */
16557 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16558 w->window_end_pos = Z - ZV;
16559 w->window_end_vpos = 0;
16561 w->window_end_valid = 0;
16563 /* Update hint: don't try scrolling again in update_window. */
16564 w->desired_matrix->no_scrolling_p = 1;
16566 #ifdef GLYPH_DEBUG
16567 debug_method_add (w, "try_window_reusing_current_matrix 1");
16568 #endif
16569 return 1;
16571 else if (CHARPOS (new_start) > CHARPOS (start))
16573 struct glyph_row *pt_row, *row;
16574 struct glyph_row *first_reusable_row;
16575 struct glyph_row *first_row_to_display;
16576 int dy;
16577 int yb = window_text_bottom_y (w);
16579 /* Find the row starting at new_start, if there is one. Don't
16580 reuse a partially visible line at the end. */
16581 first_reusable_row = start_row;
16582 while (first_reusable_row->enabled_p
16583 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16584 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16585 < CHARPOS (new_start)))
16586 ++first_reusable_row;
16588 /* Give up if there is no row to reuse. */
16589 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16590 || !first_reusable_row->enabled_p
16591 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16592 != CHARPOS (new_start)))
16593 return 0;
16595 /* We can reuse fully visible rows beginning with
16596 first_reusable_row to the end of the window. Set
16597 first_row_to_display to the first row that cannot be reused.
16598 Set pt_row to the row containing point, if there is any. */
16599 pt_row = NULL;
16600 for (first_row_to_display = first_reusable_row;
16601 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16602 ++first_row_to_display)
16604 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16605 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16606 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16607 && first_row_to_display->ends_at_zv_p
16608 && pt_row == NULL)))
16609 pt_row = first_row_to_display;
16612 /* Start displaying at the start of first_row_to_display. */
16613 eassert (first_row_to_display->y < yb);
16614 init_to_row_start (&it, w, first_row_to_display);
16616 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16617 - start_vpos);
16618 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16619 - nrows_scrolled);
16620 it.current_y = (first_row_to_display->y - first_reusable_row->y
16621 + WINDOW_HEADER_LINE_HEIGHT (w));
16623 /* Display lines beginning with first_row_to_display in the
16624 desired matrix. Set last_text_row to the last row displayed
16625 that displays text. */
16626 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16627 if (pt_row == NULL)
16628 w->cursor.vpos = -1;
16629 last_text_row = NULL;
16630 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16631 if (display_line (&it))
16632 last_text_row = it.glyph_row - 1;
16634 /* If point is in a reused row, adjust y and vpos of the cursor
16635 position. */
16636 if (pt_row)
16638 w->cursor.vpos -= nrows_scrolled;
16639 w->cursor.y -= first_reusable_row->y - start_row->y;
16642 /* Give up if point isn't in a row displayed or reused. (This
16643 also handles the case where w->cursor.vpos < nrows_scrolled
16644 after the calls to display_line, which can happen with scroll
16645 margins. See bug#1295.) */
16646 if (w->cursor.vpos < 0)
16648 clear_glyph_matrix (w->desired_matrix);
16649 return 0;
16652 /* Scroll the display. */
16653 run.current_y = first_reusable_row->y;
16654 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16655 run.height = it.last_visible_y - run.current_y;
16656 dy = run.current_y - run.desired_y;
16658 if (run.height)
16660 update_begin (f);
16661 FRAME_RIF (f)->update_window_begin_hook (w);
16662 FRAME_RIF (f)->clear_window_mouse_face (w);
16663 FRAME_RIF (f)->scroll_run_hook (w, &run);
16664 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16665 update_end (f);
16668 /* Adjust Y positions of reused rows. */
16669 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16670 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16671 max_y = it.last_visible_y;
16672 for (row = first_reusable_row; row < first_row_to_display; ++row)
16674 row->y -= dy;
16675 row->visible_height = row->height;
16676 if (row->y < min_y)
16677 row->visible_height -= min_y - row->y;
16678 if (row->y + row->height > max_y)
16679 row->visible_height -= row->y + row->height - max_y;
16680 if (row->fringe_bitmap_periodic_p)
16681 row->redraw_fringe_bitmaps_p = 1;
16684 /* Scroll the current matrix. */
16685 eassert (nrows_scrolled > 0);
16686 rotate_matrix (w->current_matrix,
16687 start_vpos,
16688 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16689 -nrows_scrolled);
16691 /* Disable rows not reused. */
16692 for (row -= nrows_scrolled; row < bottom_row; ++row)
16693 row->enabled_p = 0;
16695 /* Point may have moved to a different line, so we cannot assume that
16696 the previous cursor position is valid; locate the correct row. */
16697 if (pt_row)
16699 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16700 row < bottom_row
16701 && PT >= MATRIX_ROW_END_CHARPOS (row)
16702 && !row->ends_at_zv_p;
16703 row++)
16705 w->cursor.vpos++;
16706 w->cursor.y = row->y;
16708 if (row < bottom_row)
16710 /* Can't simply scan the row for point with
16711 bidi-reordered glyph rows. Let set_cursor_from_row
16712 figure out where to put the cursor, and if it fails,
16713 give up. */
16714 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16716 if (!set_cursor_from_row (w, row, w->current_matrix,
16717 0, 0, 0, 0))
16719 clear_glyph_matrix (w->desired_matrix);
16720 return 0;
16723 else
16725 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16726 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16728 for (; glyph < end
16729 && (!BUFFERP (glyph->object)
16730 || glyph->charpos < PT);
16731 glyph++)
16733 w->cursor.hpos++;
16734 w->cursor.x += glyph->pixel_width;
16740 /* Adjust window end. A null value of last_text_row means that
16741 the window end is in reused rows which in turn means that
16742 only its vpos can have changed. */
16743 if (last_text_row)
16744 adjust_window_ends (w, last_text_row, 0);
16745 else
16746 w->window_end_vpos -= nrows_scrolled;
16748 w->window_end_valid = 0;
16749 w->desired_matrix->no_scrolling_p = 1;
16751 #ifdef GLYPH_DEBUG
16752 debug_method_add (w, "try_window_reusing_current_matrix 2");
16753 #endif
16754 return 1;
16757 return 0;
16762 /************************************************************************
16763 Window redisplay reusing current matrix when buffer has changed
16764 ************************************************************************/
16766 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16767 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16768 ptrdiff_t *, ptrdiff_t *);
16769 static struct glyph_row *
16770 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16771 struct glyph_row *);
16774 /* Return the last row in MATRIX displaying text. If row START is
16775 non-null, start searching with that row. IT gives the dimensions
16776 of the display. Value is null if matrix is empty; otherwise it is
16777 a pointer to the row found. */
16779 static struct glyph_row *
16780 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16781 struct glyph_row *start)
16783 struct glyph_row *row, *row_found;
16785 /* Set row_found to the last row in IT->w's current matrix
16786 displaying text. The loop looks funny but think of partially
16787 visible lines. */
16788 row_found = NULL;
16789 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16790 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16792 eassert (row->enabled_p);
16793 row_found = row;
16794 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16795 break;
16796 ++row;
16799 return row_found;
16803 /* Return the last row in the current matrix of W that is not affected
16804 by changes at the start of current_buffer that occurred since W's
16805 current matrix was built. Value is null if no such row exists.
16807 BEG_UNCHANGED us the number of characters unchanged at the start of
16808 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16809 first changed character in current_buffer. Characters at positions <
16810 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16811 when the current matrix was built. */
16813 static struct glyph_row *
16814 find_last_unchanged_at_beg_row (struct window *w)
16816 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16817 struct glyph_row *row;
16818 struct glyph_row *row_found = NULL;
16819 int yb = window_text_bottom_y (w);
16821 /* Find the last row displaying unchanged text. */
16822 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16823 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16824 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16825 ++row)
16827 if (/* If row ends before first_changed_pos, it is unchanged,
16828 except in some case. */
16829 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16830 /* When row ends in ZV and we write at ZV it is not
16831 unchanged. */
16832 && !row->ends_at_zv_p
16833 /* When first_changed_pos is the end of a continued line,
16834 row is not unchanged because it may be no longer
16835 continued. */
16836 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16837 && (row->continued_p
16838 || row->exact_window_width_line_p))
16839 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16840 needs to be recomputed, so don't consider this row as
16841 unchanged. This happens when the last line was
16842 bidi-reordered and was killed immediately before this
16843 redisplay cycle. In that case, ROW->end stores the
16844 buffer position of the first visual-order character of
16845 the killed text, which is now beyond ZV. */
16846 && CHARPOS (row->end.pos) <= ZV)
16847 row_found = row;
16849 /* Stop if last visible row. */
16850 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16851 break;
16854 return row_found;
16858 /* Find the first glyph row in the current matrix of W that is not
16859 affected by changes at the end of current_buffer since the
16860 time W's current matrix was built.
16862 Return in *DELTA the number of chars by which buffer positions in
16863 unchanged text at the end of current_buffer must be adjusted.
16865 Return in *DELTA_BYTES the corresponding number of bytes.
16867 Value is null if no such row exists, i.e. all rows are affected by
16868 changes. */
16870 static struct glyph_row *
16871 find_first_unchanged_at_end_row (struct window *w,
16872 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16874 struct glyph_row *row;
16875 struct glyph_row *row_found = NULL;
16877 *delta = *delta_bytes = 0;
16879 /* Display must not have been paused, otherwise the current matrix
16880 is not up to date. */
16881 eassert (w->window_end_valid);
16883 /* A value of window_end_pos >= END_UNCHANGED means that the window
16884 end is in the range of changed text. If so, there is no
16885 unchanged row at the end of W's current matrix. */
16886 if (w->window_end_pos >= END_UNCHANGED)
16887 return NULL;
16889 /* Set row to the last row in W's current matrix displaying text. */
16890 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16892 /* If matrix is entirely empty, no unchanged row exists. */
16893 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16895 /* The value of row is the last glyph row in the matrix having a
16896 meaningful buffer position in it. The end position of row
16897 corresponds to window_end_pos. This allows us to translate
16898 buffer positions in the current matrix to current buffer
16899 positions for characters not in changed text. */
16900 ptrdiff_t Z_old =
16901 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16902 ptrdiff_t Z_BYTE_old =
16903 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16904 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16905 struct glyph_row *first_text_row
16906 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16908 *delta = Z - Z_old;
16909 *delta_bytes = Z_BYTE - Z_BYTE_old;
16911 /* Set last_unchanged_pos to the buffer position of the last
16912 character in the buffer that has not been changed. Z is the
16913 index + 1 of the last character in current_buffer, i.e. by
16914 subtracting END_UNCHANGED we get the index of the last
16915 unchanged character, and we have to add BEG to get its buffer
16916 position. */
16917 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16918 last_unchanged_pos_old = last_unchanged_pos - *delta;
16920 /* Search backward from ROW for a row displaying a line that
16921 starts at a minimum position >= last_unchanged_pos_old. */
16922 for (; row > first_text_row; --row)
16924 /* This used to abort, but it can happen.
16925 It is ok to just stop the search instead here. KFS. */
16926 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16927 break;
16929 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16930 row_found = row;
16934 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16936 return row_found;
16940 /* Make sure that glyph rows in the current matrix of window W
16941 reference the same glyph memory as corresponding rows in the
16942 frame's frame matrix. This function is called after scrolling W's
16943 current matrix on a terminal frame in try_window_id and
16944 try_window_reusing_current_matrix. */
16946 static void
16947 sync_frame_with_window_matrix_rows (struct window *w)
16949 struct frame *f = XFRAME (w->frame);
16950 struct glyph_row *window_row, *window_row_end, *frame_row;
16952 /* Preconditions: W must be a leaf window and full-width. Its frame
16953 must have a frame matrix. */
16954 eassert (BUFFERP (w->contents));
16955 eassert (WINDOW_FULL_WIDTH_P (w));
16956 eassert (!FRAME_WINDOW_P (f));
16958 /* If W is a full-width window, glyph pointers in W's current matrix
16959 have, by definition, to be the same as glyph pointers in the
16960 corresponding frame matrix. Note that frame matrices have no
16961 marginal areas (see build_frame_matrix). */
16962 window_row = w->current_matrix->rows;
16963 window_row_end = window_row + w->current_matrix->nrows;
16964 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16965 while (window_row < window_row_end)
16967 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16968 struct glyph *end = window_row->glyphs[LAST_AREA];
16970 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16971 frame_row->glyphs[TEXT_AREA] = start;
16972 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16973 frame_row->glyphs[LAST_AREA] = end;
16975 /* Disable frame rows whose corresponding window rows have
16976 been disabled in try_window_id. */
16977 if (!window_row->enabled_p)
16978 frame_row->enabled_p = 0;
16980 ++window_row, ++frame_row;
16985 /* Find the glyph row in window W containing CHARPOS. Consider all
16986 rows between START and END (not inclusive). END null means search
16987 all rows to the end of the display area of W. Value is the row
16988 containing CHARPOS or null. */
16990 struct glyph_row *
16991 row_containing_pos (struct window *w, ptrdiff_t charpos,
16992 struct glyph_row *start, struct glyph_row *end, int dy)
16994 struct glyph_row *row = start;
16995 struct glyph_row *best_row = NULL;
16996 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16997 int last_y;
16999 /* If we happen to start on a header-line, skip that. */
17000 if (row->mode_line_p)
17001 ++row;
17003 if ((end && row >= end) || !row->enabled_p)
17004 return NULL;
17006 last_y = window_text_bottom_y (w) - dy;
17008 while (1)
17010 /* Give up if we have gone too far. */
17011 if (end && row >= end)
17012 return NULL;
17013 /* This formerly returned if they were equal.
17014 I think that both quantities are of a "last plus one" type;
17015 if so, when they are equal, the row is within the screen. -- rms. */
17016 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17017 return NULL;
17019 /* If it is in this row, return this row. */
17020 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17021 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17022 /* The end position of a row equals the start
17023 position of the next row. If CHARPOS is there, we
17024 would rather consider it displayed in the next
17025 line, except when this line ends in ZV. */
17026 && !row_for_charpos_p (row, charpos)))
17027 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17029 struct glyph *g;
17031 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17032 || (!best_row && !row->continued_p))
17033 return row;
17034 /* In bidi-reordered rows, there could be several rows whose
17035 edges surround CHARPOS, all of these rows belonging to
17036 the same continued line. We need to find the row which
17037 fits CHARPOS the best. */
17038 for (g = row->glyphs[TEXT_AREA];
17039 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17040 g++)
17042 if (!STRINGP (g->object))
17044 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17046 mindif = eabs (g->charpos - charpos);
17047 best_row = row;
17048 /* Exact match always wins. */
17049 if (mindif == 0)
17050 return best_row;
17055 else if (best_row && !row->continued_p)
17056 return best_row;
17057 ++row;
17062 /* Try to redisplay window W by reusing its existing display. W's
17063 current matrix must be up to date when this function is called,
17064 i.e. window_end_valid must be nonzero.
17066 Value is
17068 1 if display has been updated
17069 0 if otherwise unsuccessful
17070 -1 if redisplay with same window start is known not to succeed
17072 The following steps are performed:
17074 1. Find the last row in the current matrix of W that is not
17075 affected by changes at the start of current_buffer. If no such row
17076 is found, give up.
17078 2. Find the first row in W's current matrix that is not affected by
17079 changes at the end of current_buffer. Maybe there is no such row.
17081 3. Display lines beginning with the row + 1 found in step 1 to the
17082 row found in step 2 or, if step 2 didn't find a row, to the end of
17083 the window.
17085 4. If cursor is not known to appear on the window, give up.
17087 5. If display stopped at the row found in step 2, scroll the
17088 display and current matrix as needed.
17090 6. Maybe display some lines at the end of W, if we must. This can
17091 happen under various circumstances, like a partially visible line
17092 becoming fully visible, or because newly displayed lines are displayed
17093 in smaller font sizes.
17095 7. Update W's window end information. */
17097 static int
17098 try_window_id (struct window *w)
17100 struct frame *f = XFRAME (w->frame);
17101 struct glyph_matrix *current_matrix = w->current_matrix;
17102 struct glyph_matrix *desired_matrix = w->desired_matrix;
17103 struct glyph_row *last_unchanged_at_beg_row;
17104 struct glyph_row *first_unchanged_at_end_row;
17105 struct glyph_row *row;
17106 struct glyph_row *bottom_row;
17107 int bottom_vpos;
17108 struct it it;
17109 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17110 int dvpos, dy;
17111 struct text_pos start_pos;
17112 struct run run;
17113 int first_unchanged_at_end_vpos = 0;
17114 struct glyph_row *last_text_row, *last_text_row_at_end;
17115 struct text_pos start;
17116 ptrdiff_t first_changed_charpos, last_changed_charpos;
17118 #ifdef GLYPH_DEBUG
17119 if (inhibit_try_window_id)
17120 return 0;
17121 #endif
17123 /* This is handy for debugging. */
17124 #if 0
17125 #define GIVE_UP(X) \
17126 do { \
17127 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17128 return 0; \
17129 } while (0)
17130 #else
17131 #define GIVE_UP(X) return 0
17132 #endif
17134 SET_TEXT_POS_FROM_MARKER (start, w->start);
17136 /* Don't use this for mini-windows because these can show
17137 messages and mini-buffers, and we don't handle that here. */
17138 if (MINI_WINDOW_P (w))
17139 GIVE_UP (1);
17141 /* This flag is used to prevent redisplay optimizations. */
17142 if (windows_or_buffers_changed || cursor_type_changed)
17143 GIVE_UP (2);
17145 /* Verify that narrowing has not changed.
17146 Also verify that we were not told to prevent redisplay optimizations.
17147 It would be nice to further
17148 reduce the number of cases where this prevents try_window_id. */
17149 if (current_buffer->clip_changed
17150 || current_buffer->prevent_redisplay_optimizations_p)
17151 GIVE_UP (3);
17153 /* Window must either use window-based redisplay or be full width. */
17154 if (!FRAME_WINDOW_P (f)
17155 && (!FRAME_LINE_INS_DEL_OK (f)
17156 || !WINDOW_FULL_WIDTH_P (w)))
17157 GIVE_UP (4);
17159 /* Give up if point is known NOT to appear in W. */
17160 if (PT < CHARPOS (start))
17161 GIVE_UP (5);
17163 /* Another way to prevent redisplay optimizations. */
17164 if (w->last_modified == 0)
17165 GIVE_UP (6);
17167 /* Verify that window is not hscrolled. */
17168 if (w->hscroll != 0)
17169 GIVE_UP (7);
17171 /* Verify that display wasn't paused. */
17172 if (!w->window_end_valid)
17173 GIVE_UP (8);
17175 /* Can't use this if highlighting a region because a cursor movement
17176 will do more than just set the cursor. */
17177 if (markpos_of_region () >= 0)
17178 GIVE_UP (9);
17180 /* Likewise if highlighting trailing whitespace. */
17181 if (!NILP (Vshow_trailing_whitespace))
17182 GIVE_UP (11);
17184 /* Likewise if showing a region. */
17185 if (w->region_showing)
17186 GIVE_UP (10);
17188 /* Can't use this if overlay arrow position and/or string have
17189 changed. */
17190 if (overlay_arrows_changed_p ())
17191 GIVE_UP (12);
17193 /* When word-wrap is on, adding a space to the first word of a
17194 wrapped line can change the wrap position, altering the line
17195 above it. It might be worthwhile to handle this more
17196 intelligently, but for now just redisplay from scratch. */
17197 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17198 GIVE_UP (21);
17200 /* Under bidi reordering, adding or deleting a character in the
17201 beginning of a paragraph, before the first strong directional
17202 character, can change the base direction of the paragraph (unless
17203 the buffer specifies a fixed paragraph direction), which will
17204 require to redisplay the whole paragraph. It might be worthwhile
17205 to find the paragraph limits and widen the range of redisplayed
17206 lines to that, but for now just give up this optimization and
17207 redisplay from scratch. */
17208 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17209 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17210 GIVE_UP (22);
17212 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17213 only if buffer has really changed. The reason is that the gap is
17214 initially at Z for freshly visited files. The code below would
17215 set end_unchanged to 0 in that case. */
17216 if (MODIFF > SAVE_MODIFF
17217 /* This seems to happen sometimes after saving a buffer. */
17218 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17220 if (GPT - BEG < BEG_UNCHANGED)
17221 BEG_UNCHANGED = GPT - BEG;
17222 if (Z - GPT < END_UNCHANGED)
17223 END_UNCHANGED = Z - GPT;
17226 /* The position of the first and last character that has been changed. */
17227 first_changed_charpos = BEG + BEG_UNCHANGED;
17228 last_changed_charpos = Z - END_UNCHANGED;
17230 /* If window starts after a line end, and the last change is in
17231 front of that newline, then changes don't affect the display.
17232 This case happens with stealth-fontification. Note that although
17233 the display is unchanged, glyph positions in the matrix have to
17234 be adjusted, of course. */
17235 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17236 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17237 && ((last_changed_charpos < CHARPOS (start)
17238 && CHARPOS (start) == BEGV)
17239 || (last_changed_charpos < CHARPOS (start) - 1
17240 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17242 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17243 struct glyph_row *r0;
17245 /* Compute how many chars/bytes have been added to or removed
17246 from the buffer. */
17247 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17248 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17249 Z_delta = Z - Z_old;
17250 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17252 /* Give up if PT is not in the window. Note that it already has
17253 been checked at the start of try_window_id that PT is not in
17254 front of the window start. */
17255 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17256 GIVE_UP (13);
17258 /* If window start is unchanged, we can reuse the whole matrix
17259 as is, after adjusting glyph positions. No need to compute
17260 the window end again, since its offset from Z hasn't changed. */
17261 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17262 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17263 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17264 /* PT must not be in a partially visible line. */
17265 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17266 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17268 /* Adjust positions in the glyph matrix. */
17269 if (Z_delta || Z_delta_bytes)
17271 struct glyph_row *r1
17272 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17273 increment_matrix_positions (w->current_matrix,
17274 MATRIX_ROW_VPOS (r0, current_matrix),
17275 MATRIX_ROW_VPOS (r1, current_matrix),
17276 Z_delta, Z_delta_bytes);
17279 /* Set the cursor. */
17280 row = row_containing_pos (w, PT, r0, NULL, 0);
17281 if (row)
17282 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17283 else
17284 emacs_abort ();
17285 return 1;
17289 /* Handle the case that changes are all below what is displayed in
17290 the window, and that PT is in the window. This shortcut cannot
17291 be taken if ZV is visible in the window, and text has been added
17292 there that is visible in the window. */
17293 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17294 /* ZV is not visible in the window, or there are no
17295 changes at ZV, actually. */
17296 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17297 || first_changed_charpos == last_changed_charpos))
17299 struct glyph_row *r0;
17301 /* Give up if PT is not in the window. Note that it already has
17302 been checked at the start of try_window_id that PT is not in
17303 front of the window start. */
17304 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17305 GIVE_UP (14);
17307 /* If window start is unchanged, we can reuse the whole matrix
17308 as is, without changing glyph positions since no text has
17309 been added/removed in front of the window end. */
17310 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17311 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17312 /* PT must not be in a partially visible line. */
17313 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17314 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17316 /* We have to compute the window end anew since text
17317 could have been added/removed after it. */
17318 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17319 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17321 /* Set the cursor. */
17322 row = row_containing_pos (w, PT, r0, NULL, 0);
17323 if (row)
17324 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17325 else
17326 emacs_abort ();
17327 return 2;
17331 /* Give up if window start is in the changed area.
17333 The condition used to read
17335 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17337 but why that was tested escapes me at the moment. */
17338 if (CHARPOS (start) >= first_changed_charpos
17339 && CHARPOS (start) <= last_changed_charpos)
17340 GIVE_UP (15);
17342 /* Check that window start agrees with the start of the first glyph
17343 row in its current matrix. Check this after we know the window
17344 start is not in changed text, otherwise positions would not be
17345 comparable. */
17346 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17347 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17348 GIVE_UP (16);
17350 /* Give up if the window ends in strings. Overlay strings
17351 at the end are difficult to handle, so don't try. */
17352 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17353 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17354 GIVE_UP (20);
17356 /* Compute the position at which we have to start displaying new
17357 lines. Some of the lines at the top of the window might be
17358 reusable because they are not displaying changed text. Find the
17359 last row in W's current matrix not affected by changes at the
17360 start of current_buffer. Value is null if changes start in the
17361 first line of window. */
17362 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17363 if (last_unchanged_at_beg_row)
17365 /* Avoid starting to display in the middle of a character, a TAB
17366 for instance. This is easier than to set up the iterator
17367 exactly, and it's not a frequent case, so the additional
17368 effort wouldn't really pay off. */
17369 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17370 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17371 && last_unchanged_at_beg_row > w->current_matrix->rows)
17372 --last_unchanged_at_beg_row;
17374 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17375 GIVE_UP (17);
17377 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17378 GIVE_UP (18);
17379 start_pos = it.current.pos;
17381 /* Start displaying new lines in the desired matrix at the same
17382 vpos we would use in the current matrix, i.e. below
17383 last_unchanged_at_beg_row. */
17384 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17385 current_matrix);
17386 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17387 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17389 eassert (it.hpos == 0 && it.current_x == 0);
17391 else
17393 /* There are no reusable lines at the start of the window.
17394 Start displaying in the first text line. */
17395 start_display (&it, w, start);
17396 it.vpos = it.first_vpos;
17397 start_pos = it.current.pos;
17400 /* Find the first row that is not affected by changes at the end of
17401 the buffer. Value will be null if there is no unchanged row, in
17402 which case we must redisplay to the end of the window. delta
17403 will be set to the value by which buffer positions beginning with
17404 first_unchanged_at_end_row have to be adjusted due to text
17405 changes. */
17406 first_unchanged_at_end_row
17407 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17408 IF_DEBUG (debug_delta = delta);
17409 IF_DEBUG (debug_delta_bytes = delta_bytes);
17411 /* Set stop_pos to the buffer position up to which we will have to
17412 display new lines. If first_unchanged_at_end_row != NULL, this
17413 is the buffer position of the start of the line displayed in that
17414 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17415 that we don't stop at a buffer position. */
17416 stop_pos = 0;
17417 if (first_unchanged_at_end_row)
17419 eassert (last_unchanged_at_beg_row == NULL
17420 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17422 /* If this is a continuation line, move forward to the next one
17423 that isn't. Changes in lines above affect this line.
17424 Caution: this may move first_unchanged_at_end_row to a row
17425 not displaying text. */
17426 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17427 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17428 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17429 < it.last_visible_y))
17430 ++first_unchanged_at_end_row;
17432 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17433 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17434 >= it.last_visible_y))
17435 first_unchanged_at_end_row = NULL;
17436 else
17438 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17439 + delta);
17440 first_unchanged_at_end_vpos
17441 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17442 eassert (stop_pos >= Z - END_UNCHANGED);
17445 else if (last_unchanged_at_beg_row == NULL)
17446 GIVE_UP (19);
17449 #ifdef GLYPH_DEBUG
17451 /* Either there is no unchanged row at the end, or the one we have
17452 now displays text. This is a necessary condition for the window
17453 end pos calculation at the end of this function. */
17454 eassert (first_unchanged_at_end_row == NULL
17455 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17457 debug_last_unchanged_at_beg_vpos
17458 = (last_unchanged_at_beg_row
17459 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17460 : -1);
17461 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17463 #endif /* GLYPH_DEBUG */
17466 /* Display new lines. Set last_text_row to the last new line
17467 displayed which has text on it, i.e. might end up as being the
17468 line where the window_end_vpos is. */
17469 w->cursor.vpos = -1;
17470 last_text_row = NULL;
17471 overlay_arrow_seen = 0;
17472 while (it.current_y < it.last_visible_y
17473 && !fonts_changed_p
17474 && (first_unchanged_at_end_row == NULL
17475 || IT_CHARPOS (it) < stop_pos))
17477 if (display_line (&it))
17478 last_text_row = it.glyph_row - 1;
17481 if (fonts_changed_p)
17482 return -1;
17485 /* Compute differences in buffer positions, y-positions etc. for
17486 lines reused at the bottom of the window. Compute what we can
17487 scroll. */
17488 if (first_unchanged_at_end_row
17489 /* No lines reused because we displayed everything up to the
17490 bottom of the window. */
17491 && it.current_y < it.last_visible_y)
17493 dvpos = (it.vpos
17494 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17495 current_matrix));
17496 dy = it.current_y - first_unchanged_at_end_row->y;
17497 run.current_y = first_unchanged_at_end_row->y;
17498 run.desired_y = run.current_y + dy;
17499 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17501 else
17503 delta = delta_bytes = dvpos = dy
17504 = run.current_y = run.desired_y = run.height = 0;
17505 first_unchanged_at_end_row = NULL;
17507 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17510 /* Find the cursor if not already found. We have to decide whether
17511 PT will appear on this window (it sometimes doesn't, but this is
17512 not a very frequent case.) This decision has to be made before
17513 the current matrix is altered. A value of cursor.vpos < 0 means
17514 that PT is either in one of the lines beginning at
17515 first_unchanged_at_end_row or below the window. Don't care for
17516 lines that might be displayed later at the window end; as
17517 mentioned, this is not a frequent case. */
17518 if (w->cursor.vpos < 0)
17520 /* Cursor in unchanged rows at the top? */
17521 if (PT < CHARPOS (start_pos)
17522 && last_unchanged_at_beg_row)
17524 row = row_containing_pos (w, PT,
17525 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17526 last_unchanged_at_beg_row + 1, 0);
17527 if (row)
17528 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17531 /* Start from first_unchanged_at_end_row looking for PT. */
17532 else if (first_unchanged_at_end_row)
17534 row = row_containing_pos (w, PT - delta,
17535 first_unchanged_at_end_row, NULL, 0);
17536 if (row)
17537 set_cursor_from_row (w, row, w->current_matrix, delta,
17538 delta_bytes, dy, dvpos);
17541 /* Give up if cursor was not found. */
17542 if (w->cursor.vpos < 0)
17544 clear_glyph_matrix (w->desired_matrix);
17545 return -1;
17549 /* Don't let the cursor end in the scroll margins. */
17551 int this_scroll_margin, cursor_height;
17552 int frame_line_height = default_line_pixel_height (w);
17553 int window_total_lines
17554 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17556 this_scroll_margin =
17557 max (0, min (scroll_margin, window_total_lines / 4));
17558 this_scroll_margin *= frame_line_height;
17559 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17561 if ((w->cursor.y < this_scroll_margin
17562 && CHARPOS (start) > BEGV)
17563 /* Old redisplay didn't take scroll margin into account at the bottom,
17564 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17565 || (w->cursor.y + (make_cursor_line_fully_visible_p
17566 ? cursor_height + this_scroll_margin
17567 : 1)) > it.last_visible_y)
17569 w->cursor.vpos = -1;
17570 clear_glyph_matrix (w->desired_matrix);
17571 return -1;
17575 /* Scroll the display. Do it before changing the current matrix so
17576 that xterm.c doesn't get confused about where the cursor glyph is
17577 found. */
17578 if (dy && run.height)
17580 update_begin (f);
17582 if (FRAME_WINDOW_P (f))
17584 FRAME_RIF (f)->update_window_begin_hook (w);
17585 FRAME_RIF (f)->clear_window_mouse_face (w);
17586 FRAME_RIF (f)->scroll_run_hook (w, &run);
17587 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17589 else
17591 /* Terminal frame. In this case, dvpos gives the number of
17592 lines to scroll by; dvpos < 0 means scroll up. */
17593 int from_vpos
17594 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17595 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17596 int end = (WINDOW_TOP_EDGE_LINE (w)
17597 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17598 + window_internal_height (w));
17600 #if defined (HAVE_GPM) || defined (MSDOS)
17601 x_clear_window_mouse_face (w);
17602 #endif
17603 /* Perform the operation on the screen. */
17604 if (dvpos > 0)
17606 /* Scroll last_unchanged_at_beg_row to the end of the
17607 window down dvpos lines. */
17608 set_terminal_window (f, end);
17610 /* On dumb terminals delete dvpos lines at the end
17611 before inserting dvpos empty lines. */
17612 if (!FRAME_SCROLL_REGION_OK (f))
17613 ins_del_lines (f, end - dvpos, -dvpos);
17615 /* Insert dvpos empty lines in front of
17616 last_unchanged_at_beg_row. */
17617 ins_del_lines (f, from, dvpos);
17619 else if (dvpos < 0)
17621 /* Scroll up last_unchanged_at_beg_vpos to the end of
17622 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17623 set_terminal_window (f, end);
17625 /* Delete dvpos lines in front of
17626 last_unchanged_at_beg_vpos. ins_del_lines will set
17627 the cursor to the given vpos and emit |dvpos| delete
17628 line sequences. */
17629 ins_del_lines (f, from + dvpos, dvpos);
17631 /* On a dumb terminal insert dvpos empty lines at the
17632 end. */
17633 if (!FRAME_SCROLL_REGION_OK (f))
17634 ins_del_lines (f, end + dvpos, -dvpos);
17637 set_terminal_window (f, 0);
17640 update_end (f);
17643 /* Shift reused rows of the current matrix to the right position.
17644 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17645 text. */
17646 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17647 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17648 if (dvpos < 0)
17650 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17651 bottom_vpos, dvpos);
17652 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17653 bottom_vpos);
17655 else if (dvpos > 0)
17657 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17658 bottom_vpos, dvpos);
17659 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17660 first_unchanged_at_end_vpos + dvpos);
17663 /* For frame-based redisplay, make sure that current frame and window
17664 matrix are in sync with respect to glyph memory. */
17665 if (!FRAME_WINDOW_P (f))
17666 sync_frame_with_window_matrix_rows (w);
17668 /* Adjust buffer positions in reused rows. */
17669 if (delta || delta_bytes)
17670 increment_matrix_positions (current_matrix,
17671 first_unchanged_at_end_vpos + dvpos,
17672 bottom_vpos, delta, delta_bytes);
17674 /* Adjust Y positions. */
17675 if (dy)
17676 shift_glyph_matrix (w, current_matrix,
17677 first_unchanged_at_end_vpos + dvpos,
17678 bottom_vpos, dy);
17680 if (first_unchanged_at_end_row)
17682 first_unchanged_at_end_row += dvpos;
17683 if (first_unchanged_at_end_row->y >= it.last_visible_y
17684 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17685 first_unchanged_at_end_row = NULL;
17688 /* If scrolling up, there may be some lines to display at the end of
17689 the window. */
17690 last_text_row_at_end = NULL;
17691 if (dy < 0)
17693 /* Scrolling up can leave for example a partially visible line
17694 at the end of the window to be redisplayed. */
17695 /* Set last_row to the glyph row in the current matrix where the
17696 window end line is found. It has been moved up or down in
17697 the matrix by dvpos. */
17698 int last_vpos = w->window_end_vpos + dvpos;
17699 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17701 /* If last_row is the window end line, it should display text. */
17702 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17704 /* If window end line was partially visible before, begin
17705 displaying at that line. Otherwise begin displaying with the
17706 line following it. */
17707 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17709 init_to_row_start (&it, w, last_row);
17710 it.vpos = last_vpos;
17711 it.current_y = last_row->y;
17713 else
17715 init_to_row_end (&it, w, last_row);
17716 it.vpos = 1 + last_vpos;
17717 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17718 ++last_row;
17721 /* We may start in a continuation line. If so, we have to
17722 get the right continuation_lines_width and current_x. */
17723 it.continuation_lines_width = last_row->continuation_lines_width;
17724 it.hpos = it.current_x = 0;
17726 /* Display the rest of the lines at the window end. */
17727 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17728 while (it.current_y < it.last_visible_y
17729 && !fonts_changed_p)
17731 /* Is it always sure that the display agrees with lines in
17732 the current matrix? I don't think so, so we mark rows
17733 displayed invalid in the current matrix by setting their
17734 enabled_p flag to zero. */
17735 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17736 if (display_line (&it))
17737 last_text_row_at_end = it.glyph_row - 1;
17741 /* Update window_end_pos and window_end_vpos. */
17742 if (first_unchanged_at_end_row && !last_text_row_at_end)
17744 /* Window end line if one of the preserved rows from the current
17745 matrix. Set row to the last row displaying text in current
17746 matrix starting at first_unchanged_at_end_row, after
17747 scrolling. */
17748 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17749 row = find_last_row_displaying_text (w->current_matrix, &it,
17750 first_unchanged_at_end_row);
17751 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17752 adjust_window_ends (w, row, 1);
17753 eassert (w->window_end_bytepos >= 0);
17754 IF_DEBUG (debug_method_add (w, "A"));
17756 else if (last_text_row_at_end)
17758 adjust_window_ends (w, last_text_row_at_end, 0);
17759 eassert (w->window_end_bytepos >= 0);
17760 IF_DEBUG (debug_method_add (w, "B"));
17762 else if (last_text_row)
17764 /* We have displayed either to the end of the window or at the
17765 end of the window, i.e. the last row with text is to be found
17766 in the desired matrix. */
17767 adjust_window_ends (w, last_text_row, 0);
17768 eassert (w->window_end_bytepos >= 0);
17770 else if (first_unchanged_at_end_row == NULL
17771 && last_text_row == NULL
17772 && last_text_row_at_end == NULL)
17774 /* Displayed to end of window, but no line containing text was
17775 displayed. Lines were deleted at the end of the window. */
17776 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17777 int vpos = w->window_end_vpos;
17778 struct glyph_row *current_row = current_matrix->rows + vpos;
17779 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17781 for (row = NULL;
17782 row == NULL && vpos >= first_vpos;
17783 --vpos, --current_row, --desired_row)
17785 if (desired_row->enabled_p)
17787 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17788 row = desired_row;
17790 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17791 row = current_row;
17794 eassert (row != NULL);
17795 w->window_end_vpos = vpos + 1;
17796 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17797 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17798 eassert (w->window_end_bytepos >= 0);
17799 IF_DEBUG (debug_method_add (w, "C"));
17801 else
17802 emacs_abort ();
17804 IF_DEBUG (debug_end_pos = w->window_end_pos;
17805 debug_end_vpos = w->window_end_vpos);
17807 /* Record that display has not been completed. */
17808 w->window_end_valid = 0;
17809 w->desired_matrix->no_scrolling_p = 1;
17810 return 3;
17812 #undef GIVE_UP
17817 /***********************************************************************
17818 More debugging support
17819 ***********************************************************************/
17821 #ifdef GLYPH_DEBUG
17823 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17824 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17825 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17828 /* Dump the contents of glyph matrix MATRIX on stderr.
17830 GLYPHS 0 means don't show glyph contents.
17831 GLYPHS 1 means show glyphs in short form
17832 GLYPHS > 1 means show glyphs in long form. */
17834 void
17835 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17837 int i;
17838 for (i = 0; i < matrix->nrows; ++i)
17839 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17843 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17844 the glyph row and area where the glyph comes from. */
17846 void
17847 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17849 if (glyph->type == CHAR_GLYPH
17850 || glyph->type == GLYPHLESS_GLYPH)
17852 fprintf (stderr,
17853 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17854 glyph - row->glyphs[TEXT_AREA],
17855 (glyph->type == CHAR_GLYPH
17856 ? 'C'
17857 : 'G'),
17858 glyph->charpos,
17859 (BUFFERP (glyph->object)
17860 ? 'B'
17861 : (STRINGP (glyph->object)
17862 ? 'S'
17863 : (INTEGERP (glyph->object)
17864 ? '0'
17865 : '-'))),
17866 glyph->pixel_width,
17867 glyph->u.ch,
17868 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17869 ? glyph->u.ch
17870 : '.'),
17871 glyph->face_id,
17872 glyph->left_box_line_p,
17873 glyph->right_box_line_p);
17875 else if (glyph->type == STRETCH_GLYPH)
17877 fprintf (stderr,
17878 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17879 glyph - row->glyphs[TEXT_AREA],
17880 'S',
17881 glyph->charpos,
17882 (BUFFERP (glyph->object)
17883 ? 'B'
17884 : (STRINGP (glyph->object)
17885 ? 'S'
17886 : (INTEGERP (glyph->object)
17887 ? '0'
17888 : '-'))),
17889 glyph->pixel_width,
17891 ' ',
17892 glyph->face_id,
17893 glyph->left_box_line_p,
17894 glyph->right_box_line_p);
17896 else if (glyph->type == IMAGE_GLYPH)
17898 fprintf (stderr,
17899 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17900 glyph - row->glyphs[TEXT_AREA],
17901 'I',
17902 glyph->charpos,
17903 (BUFFERP (glyph->object)
17904 ? 'B'
17905 : (STRINGP (glyph->object)
17906 ? 'S'
17907 : (INTEGERP (glyph->object)
17908 ? '0'
17909 : '-'))),
17910 glyph->pixel_width,
17911 glyph->u.img_id,
17912 '.',
17913 glyph->face_id,
17914 glyph->left_box_line_p,
17915 glyph->right_box_line_p);
17917 else if (glyph->type == COMPOSITE_GLYPH)
17919 fprintf (stderr,
17920 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17921 glyph - row->glyphs[TEXT_AREA],
17922 '+',
17923 glyph->charpos,
17924 (BUFFERP (glyph->object)
17925 ? 'B'
17926 : (STRINGP (glyph->object)
17927 ? 'S'
17928 : (INTEGERP (glyph->object)
17929 ? '0'
17930 : '-'))),
17931 glyph->pixel_width,
17932 glyph->u.cmp.id);
17933 if (glyph->u.cmp.automatic)
17934 fprintf (stderr,
17935 "[%d-%d]",
17936 glyph->slice.cmp.from, glyph->slice.cmp.to);
17937 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17938 glyph->face_id,
17939 glyph->left_box_line_p,
17940 glyph->right_box_line_p);
17945 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17946 GLYPHS 0 means don't show glyph contents.
17947 GLYPHS 1 means show glyphs in short form
17948 GLYPHS > 1 means show glyphs in long form. */
17950 void
17951 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17953 if (glyphs != 1)
17955 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17956 fprintf (stderr, "==============================================================================\n");
17958 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17959 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17960 vpos,
17961 MATRIX_ROW_START_CHARPOS (row),
17962 MATRIX_ROW_END_CHARPOS (row),
17963 row->used[TEXT_AREA],
17964 row->contains_overlapping_glyphs_p,
17965 row->enabled_p,
17966 row->truncated_on_left_p,
17967 row->truncated_on_right_p,
17968 row->continued_p,
17969 MATRIX_ROW_CONTINUATION_LINE_P (row),
17970 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17971 row->ends_at_zv_p,
17972 row->fill_line_p,
17973 row->ends_in_middle_of_char_p,
17974 row->starts_in_middle_of_char_p,
17975 row->mouse_face_p,
17976 row->x,
17977 row->y,
17978 row->pixel_width,
17979 row->height,
17980 row->visible_height,
17981 row->ascent,
17982 row->phys_ascent);
17983 /* The next 3 lines should align to "Start" in the header. */
17984 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17985 row->end.overlay_string_index,
17986 row->continuation_lines_width);
17987 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17988 CHARPOS (row->start.string_pos),
17989 CHARPOS (row->end.string_pos));
17990 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17991 row->end.dpvec_index);
17994 if (glyphs > 1)
17996 int area;
17998 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18000 struct glyph *glyph = row->glyphs[area];
18001 struct glyph *glyph_end = glyph + row->used[area];
18003 /* Glyph for a line end in text. */
18004 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18005 ++glyph_end;
18007 if (glyph < glyph_end)
18008 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18010 for (; glyph < glyph_end; ++glyph)
18011 dump_glyph (row, glyph, area);
18014 else if (glyphs == 1)
18016 int area;
18018 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18020 char *s = alloca (row->used[area] + 4);
18021 int i;
18023 for (i = 0; i < row->used[area]; ++i)
18025 struct glyph *glyph = row->glyphs[area] + i;
18026 if (i == row->used[area] - 1
18027 && area == TEXT_AREA
18028 && INTEGERP (glyph->object)
18029 && glyph->type == CHAR_GLYPH
18030 && glyph->u.ch == ' ')
18032 strcpy (&s[i], "[\\n]");
18033 i += 4;
18035 else if (glyph->type == CHAR_GLYPH
18036 && glyph->u.ch < 0x80
18037 && glyph->u.ch >= ' ')
18038 s[i] = glyph->u.ch;
18039 else
18040 s[i] = '.';
18043 s[i] = '\0';
18044 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18050 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18051 Sdump_glyph_matrix, 0, 1, "p",
18052 doc: /* Dump the current matrix of the selected window to stderr.
18053 Shows contents of glyph row structures. With non-nil
18054 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18055 glyphs in short form, otherwise show glyphs in long form. */)
18056 (Lisp_Object glyphs)
18058 struct window *w = XWINDOW (selected_window);
18059 struct buffer *buffer = XBUFFER (w->contents);
18061 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18062 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18063 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18064 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18065 fprintf (stderr, "=============================================\n");
18066 dump_glyph_matrix (w->current_matrix,
18067 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18068 return Qnil;
18072 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18073 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18074 (void)
18076 struct frame *f = XFRAME (selected_frame);
18077 dump_glyph_matrix (f->current_matrix, 1);
18078 return Qnil;
18082 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18083 doc: /* Dump glyph row ROW to stderr.
18084 GLYPH 0 means don't dump glyphs.
18085 GLYPH 1 means dump glyphs in short form.
18086 GLYPH > 1 or omitted means dump glyphs in long form. */)
18087 (Lisp_Object row, Lisp_Object glyphs)
18089 struct glyph_matrix *matrix;
18090 EMACS_INT vpos;
18092 CHECK_NUMBER (row);
18093 matrix = XWINDOW (selected_window)->current_matrix;
18094 vpos = XINT (row);
18095 if (vpos >= 0 && vpos < matrix->nrows)
18096 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18097 vpos,
18098 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18099 return Qnil;
18103 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18104 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18105 GLYPH 0 means don't dump glyphs.
18106 GLYPH 1 means dump glyphs in short form.
18107 GLYPH > 1 or omitted means dump glyphs in long form. */)
18108 (Lisp_Object row, Lisp_Object glyphs)
18110 struct frame *sf = SELECTED_FRAME ();
18111 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18112 EMACS_INT vpos;
18114 CHECK_NUMBER (row);
18115 vpos = XINT (row);
18116 if (vpos >= 0 && vpos < m->nrows)
18117 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18118 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18119 return Qnil;
18123 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18124 doc: /* Toggle tracing of redisplay.
18125 With ARG, turn tracing on if and only if ARG is positive. */)
18126 (Lisp_Object arg)
18128 if (NILP (arg))
18129 trace_redisplay_p = !trace_redisplay_p;
18130 else
18132 arg = Fprefix_numeric_value (arg);
18133 trace_redisplay_p = XINT (arg) > 0;
18136 return Qnil;
18140 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18141 doc: /* Like `format', but print result to stderr.
18142 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18143 (ptrdiff_t nargs, Lisp_Object *args)
18145 Lisp_Object s = Fformat (nargs, args);
18146 fprintf (stderr, "%s", SDATA (s));
18147 return Qnil;
18150 #endif /* GLYPH_DEBUG */
18154 /***********************************************************************
18155 Building Desired Matrix Rows
18156 ***********************************************************************/
18158 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18159 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18161 static struct glyph_row *
18162 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18164 struct frame *f = XFRAME (WINDOW_FRAME (w));
18165 struct buffer *buffer = XBUFFER (w->contents);
18166 struct buffer *old = current_buffer;
18167 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18168 int arrow_len = SCHARS (overlay_arrow_string);
18169 const unsigned char *arrow_end = arrow_string + arrow_len;
18170 const unsigned char *p;
18171 struct it it;
18172 bool multibyte_p;
18173 int n_glyphs_before;
18175 set_buffer_temp (buffer);
18176 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18177 it.glyph_row->used[TEXT_AREA] = 0;
18178 SET_TEXT_POS (it.position, 0, 0);
18180 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18181 p = arrow_string;
18182 while (p < arrow_end)
18184 Lisp_Object face, ilisp;
18186 /* Get the next character. */
18187 if (multibyte_p)
18188 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18189 else
18191 it.c = it.char_to_display = *p, it.len = 1;
18192 if (! ASCII_CHAR_P (it.c))
18193 it.char_to_display = BYTE8_TO_CHAR (it.c);
18195 p += it.len;
18197 /* Get its face. */
18198 ilisp = make_number (p - arrow_string);
18199 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18200 it.face_id = compute_char_face (f, it.char_to_display, face);
18202 /* Compute its width, get its glyphs. */
18203 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18204 SET_TEXT_POS (it.position, -1, -1);
18205 PRODUCE_GLYPHS (&it);
18207 /* If this character doesn't fit any more in the line, we have
18208 to remove some glyphs. */
18209 if (it.current_x > it.last_visible_x)
18211 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18212 break;
18216 set_buffer_temp (old);
18217 return it.glyph_row;
18221 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18222 glyphs to insert is determined by produce_special_glyphs. */
18224 static void
18225 insert_left_trunc_glyphs (struct it *it)
18227 struct it truncate_it;
18228 struct glyph *from, *end, *to, *toend;
18230 eassert (!FRAME_WINDOW_P (it->f)
18231 || (!it->glyph_row->reversed_p
18232 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18233 || (it->glyph_row->reversed_p
18234 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18236 /* Get the truncation glyphs. */
18237 truncate_it = *it;
18238 truncate_it.current_x = 0;
18239 truncate_it.face_id = DEFAULT_FACE_ID;
18240 truncate_it.glyph_row = &scratch_glyph_row;
18241 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18242 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18243 truncate_it.object = make_number (0);
18244 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18246 /* Overwrite glyphs from IT with truncation glyphs. */
18247 if (!it->glyph_row->reversed_p)
18249 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18251 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18252 end = from + tused;
18253 to = it->glyph_row->glyphs[TEXT_AREA];
18254 toend = to + it->glyph_row->used[TEXT_AREA];
18255 if (FRAME_WINDOW_P (it->f))
18257 /* On GUI frames, when variable-size fonts are displayed,
18258 the truncation glyphs may need more pixels than the row's
18259 glyphs they overwrite. We overwrite more glyphs to free
18260 enough screen real estate, and enlarge the stretch glyph
18261 on the right (see display_line), if there is one, to
18262 preserve the screen position of the truncation glyphs on
18263 the right. */
18264 int w = 0;
18265 struct glyph *g = to;
18266 short used;
18268 /* The first glyph could be partially visible, in which case
18269 it->glyph_row->x will be negative. But we want the left
18270 truncation glyphs to be aligned at the left margin of the
18271 window, so we override the x coordinate at which the row
18272 will begin. */
18273 it->glyph_row->x = 0;
18274 while (g < toend && w < it->truncation_pixel_width)
18276 w += g->pixel_width;
18277 ++g;
18279 if (g - to - tused > 0)
18281 memmove (to + tused, g, (toend - g) * sizeof(*g));
18282 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18284 used = it->glyph_row->used[TEXT_AREA];
18285 if (it->glyph_row->truncated_on_right_p
18286 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18287 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18288 == STRETCH_GLYPH)
18290 int extra = w - it->truncation_pixel_width;
18292 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18296 while (from < end)
18297 *to++ = *from++;
18299 /* There may be padding glyphs left over. Overwrite them too. */
18300 if (!FRAME_WINDOW_P (it->f))
18302 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18304 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18305 while (from < end)
18306 *to++ = *from++;
18310 if (to > toend)
18311 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18313 else
18315 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18317 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18318 that back to front. */
18319 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18320 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18321 toend = it->glyph_row->glyphs[TEXT_AREA];
18322 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18323 if (FRAME_WINDOW_P (it->f))
18325 int w = 0;
18326 struct glyph *g = to;
18328 while (g >= toend && w < it->truncation_pixel_width)
18330 w += g->pixel_width;
18331 --g;
18333 if (to - g - tused > 0)
18334 to = g + tused;
18335 if (it->glyph_row->truncated_on_right_p
18336 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18337 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18339 int extra = w - it->truncation_pixel_width;
18341 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18345 while (from >= end && to >= toend)
18346 *to-- = *from--;
18347 if (!FRAME_WINDOW_P (it->f))
18349 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18351 from =
18352 truncate_it.glyph_row->glyphs[TEXT_AREA]
18353 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18354 while (from >= end && to >= toend)
18355 *to-- = *from--;
18358 if (from >= end)
18360 /* Need to free some room before prepending additional
18361 glyphs. */
18362 int move_by = from - end + 1;
18363 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18364 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18366 for ( ; g >= g0; g--)
18367 g[move_by] = *g;
18368 while (from >= end)
18369 *to-- = *from--;
18370 it->glyph_row->used[TEXT_AREA] += move_by;
18375 /* Compute the hash code for ROW. */
18376 unsigned
18377 row_hash (struct glyph_row *row)
18379 int area, k;
18380 unsigned hashval = 0;
18382 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18383 for (k = 0; k < row->used[area]; ++k)
18384 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18385 + row->glyphs[area][k].u.val
18386 + row->glyphs[area][k].face_id
18387 + row->glyphs[area][k].padding_p
18388 + (row->glyphs[area][k].type << 2));
18390 return hashval;
18393 /* Compute the pixel height and width of IT->glyph_row.
18395 Most of the time, ascent and height of a display line will be equal
18396 to the max_ascent and max_height values of the display iterator
18397 structure. This is not the case if
18399 1. We hit ZV without displaying anything. In this case, max_ascent
18400 and max_height will be zero.
18402 2. We have some glyphs that don't contribute to the line height.
18403 (The glyph row flag contributes_to_line_height_p is for future
18404 pixmap extensions).
18406 The first case is easily covered by using default values because in
18407 these cases, the line height does not really matter, except that it
18408 must not be zero. */
18410 static void
18411 compute_line_metrics (struct it *it)
18413 struct glyph_row *row = it->glyph_row;
18415 if (FRAME_WINDOW_P (it->f))
18417 int i, min_y, max_y;
18419 /* The line may consist of one space only, that was added to
18420 place the cursor on it. If so, the row's height hasn't been
18421 computed yet. */
18422 if (row->height == 0)
18424 if (it->max_ascent + it->max_descent == 0)
18425 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18426 row->ascent = it->max_ascent;
18427 row->height = it->max_ascent + it->max_descent;
18428 row->phys_ascent = it->max_phys_ascent;
18429 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18430 row->extra_line_spacing = it->max_extra_line_spacing;
18433 /* Compute the width of this line. */
18434 row->pixel_width = row->x;
18435 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18436 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18438 eassert (row->pixel_width >= 0);
18439 eassert (row->ascent >= 0 && row->height > 0);
18441 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18442 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18444 /* If first line's physical ascent is larger than its logical
18445 ascent, use the physical ascent, and make the row taller.
18446 This makes accented characters fully visible. */
18447 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18448 && row->phys_ascent > row->ascent)
18450 row->height += row->phys_ascent - row->ascent;
18451 row->ascent = row->phys_ascent;
18454 /* Compute how much of the line is visible. */
18455 row->visible_height = row->height;
18457 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18458 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18460 if (row->y < min_y)
18461 row->visible_height -= min_y - row->y;
18462 if (row->y + row->height > max_y)
18463 row->visible_height -= row->y + row->height - max_y;
18465 else
18467 row->pixel_width = row->used[TEXT_AREA];
18468 if (row->continued_p)
18469 row->pixel_width -= it->continuation_pixel_width;
18470 else if (row->truncated_on_right_p)
18471 row->pixel_width -= it->truncation_pixel_width;
18472 row->ascent = row->phys_ascent = 0;
18473 row->height = row->phys_height = row->visible_height = 1;
18474 row->extra_line_spacing = 0;
18477 /* Compute a hash code for this row. */
18478 row->hash = row_hash (row);
18480 it->max_ascent = it->max_descent = 0;
18481 it->max_phys_ascent = it->max_phys_descent = 0;
18485 /* Append one space to the glyph row of iterator IT if doing a
18486 window-based redisplay. The space has the same face as
18487 IT->face_id. Value is non-zero if a space was added.
18489 This function is called to make sure that there is always one glyph
18490 at the end of a glyph row that the cursor can be set on under
18491 window-systems. (If there weren't such a glyph we would not know
18492 how wide and tall a box cursor should be displayed).
18494 At the same time this space let's a nicely handle clearing to the
18495 end of the line if the row ends in italic text. */
18497 static int
18498 append_space_for_newline (struct it *it, int default_face_p)
18500 if (FRAME_WINDOW_P (it->f))
18502 int n = it->glyph_row->used[TEXT_AREA];
18504 if (it->glyph_row->glyphs[TEXT_AREA] + n
18505 < it->glyph_row->glyphs[1 + TEXT_AREA])
18507 /* Save some values that must not be changed.
18508 Must save IT->c and IT->len because otherwise
18509 ITERATOR_AT_END_P wouldn't work anymore after
18510 append_space_for_newline has been called. */
18511 enum display_element_type saved_what = it->what;
18512 int saved_c = it->c, saved_len = it->len;
18513 int saved_char_to_display = it->char_to_display;
18514 int saved_x = it->current_x;
18515 int saved_face_id = it->face_id;
18516 int saved_box_end = it->end_of_box_run_p;
18517 struct text_pos saved_pos;
18518 Lisp_Object saved_object;
18519 struct face *face;
18521 saved_object = it->object;
18522 saved_pos = it->position;
18524 it->what = IT_CHARACTER;
18525 memset (&it->position, 0, sizeof it->position);
18526 it->object = make_number (0);
18527 it->c = it->char_to_display = ' ';
18528 it->len = 1;
18530 /* If the default face was remapped, be sure to use the
18531 remapped face for the appended newline. */
18532 if (default_face_p)
18533 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18534 else if (it->face_before_selective_p)
18535 it->face_id = it->saved_face_id;
18536 face = FACE_FROM_ID (it->f, it->face_id);
18537 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18538 /* In R2L rows, we will prepend a stretch glyph that will
18539 have the end_of_box_run_p flag set for it, so there's no
18540 need for the appended newline glyph to have that flag
18541 set. */
18542 if (it->glyph_row->reversed_p
18543 /* But if the appended newline glyph goes all the way to
18544 the end of the row, there will be no stretch glyph,
18545 so leave the box flag set. */
18546 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18547 it->end_of_box_run_p = 0;
18549 PRODUCE_GLYPHS (it);
18551 it->override_ascent = -1;
18552 it->constrain_row_ascent_descent_p = 0;
18553 it->current_x = saved_x;
18554 it->object = saved_object;
18555 it->position = saved_pos;
18556 it->what = saved_what;
18557 it->face_id = saved_face_id;
18558 it->len = saved_len;
18559 it->c = saved_c;
18560 it->char_to_display = saved_char_to_display;
18561 it->end_of_box_run_p = saved_box_end;
18562 return 1;
18566 return 0;
18570 /* Extend the face of the last glyph in the text area of IT->glyph_row
18571 to the end of the display line. Called from display_line. If the
18572 glyph row is empty, add a space glyph to it so that we know the
18573 face to draw. Set the glyph row flag fill_line_p. If the glyph
18574 row is R2L, prepend a stretch glyph to cover the empty space to the
18575 left of the leftmost glyph. */
18577 static void
18578 extend_face_to_end_of_line (struct it *it)
18580 struct face *face, *default_face;
18581 struct frame *f = it->f;
18583 /* If line is already filled, do nothing. Non window-system frames
18584 get a grace of one more ``pixel'' because their characters are
18585 1-``pixel'' wide, so they hit the equality too early. This grace
18586 is needed only for R2L rows that are not continued, to produce
18587 one extra blank where we could display the cursor. */
18588 if (it->current_x >= it->last_visible_x
18589 + (!FRAME_WINDOW_P (f)
18590 && it->glyph_row->reversed_p
18591 && !it->glyph_row->continued_p))
18592 return;
18594 /* The default face, possibly remapped. */
18595 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18597 /* Face extension extends the background and box of IT->face_id
18598 to the end of the line. If the background equals the background
18599 of the frame, we don't have to do anything. */
18600 if (it->face_before_selective_p)
18601 face = FACE_FROM_ID (f, it->saved_face_id);
18602 else
18603 face = FACE_FROM_ID (f, it->face_id);
18605 if (FRAME_WINDOW_P (f)
18606 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18607 && face->box == FACE_NO_BOX
18608 && face->background == FRAME_BACKGROUND_PIXEL (f)
18609 && !face->stipple
18610 && !it->glyph_row->reversed_p)
18611 return;
18613 /* Set the glyph row flag indicating that the face of the last glyph
18614 in the text area has to be drawn to the end of the text area. */
18615 it->glyph_row->fill_line_p = 1;
18617 /* If current character of IT is not ASCII, make sure we have the
18618 ASCII face. This will be automatically undone the next time
18619 get_next_display_element returns a multibyte character. Note
18620 that the character will always be single byte in unibyte
18621 text. */
18622 if (!ASCII_CHAR_P (it->c))
18624 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18627 if (FRAME_WINDOW_P (f))
18629 /* If the row is empty, add a space with the current face of IT,
18630 so that we know which face to draw. */
18631 if (it->glyph_row->used[TEXT_AREA] == 0)
18633 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18634 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18635 it->glyph_row->used[TEXT_AREA] = 1;
18637 #ifdef HAVE_WINDOW_SYSTEM
18638 if (it->glyph_row->reversed_p)
18640 /* Prepend a stretch glyph to the row, such that the
18641 rightmost glyph will be drawn flushed all the way to the
18642 right margin of the window. The stretch glyph that will
18643 occupy the empty space, if any, to the left of the
18644 glyphs. */
18645 struct font *font = face->font ? face->font : FRAME_FONT (f);
18646 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18647 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18648 struct glyph *g;
18649 int row_width, stretch_ascent, stretch_width;
18650 struct text_pos saved_pos;
18651 int saved_face_id, saved_avoid_cursor, saved_box_start;
18653 for (row_width = 0, g = row_start; g < row_end; g++)
18654 row_width += g->pixel_width;
18655 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18656 if (stretch_width > 0)
18658 stretch_ascent =
18659 (((it->ascent + it->descent)
18660 * FONT_BASE (font)) / FONT_HEIGHT (font));
18661 saved_pos = it->position;
18662 memset (&it->position, 0, sizeof it->position);
18663 saved_avoid_cursor = it->avoid_cursor_p;
18664 it->avoid_cursor_p = 1;
18665 saved_face_id = it->face_id;
18666 saved_box_start = it->start_of_box_run_p;
18667 /* The last row's stretch glyph should get the default
18668 face, to avoid painting the rest of the window with
18669 the region face, if the region ends at ZV. */
18670 if (it->glyph_row->ends_at_zv_p)
18671 it->face_id = default_face->id;
18672 else
18673 it->face_id = face->id;
18674 it->start_of_box_run_p = 0;
18675 append_stretch_glyph (it, make_number (0), stretch_width,
18676 it->ascent + it->descent, stretch_ascent);
18677 it->position = saved_pos;
18678 it->avoid_cursor_p = saved_avoid_cursor;
18679 it->face_id = saved_face_id;
18680 it->start_of_box_run_p = saved_box_start;
18683 #endif /* HAVE_WINDOW_SYSTEM */
18685 else
18687 /* Save some values that must not be changed. */
18688 int saved_x = it->current_x;
18689 struct text_pos saved_pos;
18690 Lisp_Object saved_object;
18691 enum display_element_type saved_what = it->what;
18692 int saved_face_id = it->face_id;
18694 saved_object = it->object;
18695 saved_pos = it->position;
18697 it->what = IT_CHARACTER;
18698 memset (&it->position, 0, sizeof it->position);
18699 it->object = make_number (0);
18700 it->c = it->char_to_display = ' ';
18701 it->len = 1;
18702 /* The last row's blank glyphs should get the default face, to
18703 avoid painting the rest of the window with the region face,
18704 if the region ends at ZV. */
18705 if (it->glyph_row->ends_at_zv_p)
18706 it->face_id = default_face->id;
18707 else
18708 it->face_id = face->id;
18710 PRODUCE_GLYPHS (it);
18712 while (it->current_x <= it->last_visible_x)
18713 PRODUCE_GLYPHS (it);
18715 /* Don't count these blanks really. It would let us insert a left
18716 truncation glyph below and make us set the cursor on them, maybe. */
18717 it->current_x = saved_x;
18718 it->object = saved_object;
18719 it->position = saved_pos;
18720 it->what = saved_what;
18721 it->face_id = saved_face_id;
18726 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18727 trailing whitespace. */
18729 static int
18730 trailing_whitespace_p (ptrdiff_t charpos)
18732 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18733 int c = 0;
18735 while (bytepos < ZV_BYTE
18736 && (c = FETCH_CHAR (bytepos),
18737 c == ' ' || c == '\t'))
18738 ++bytepos;
18740 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18742 if (bytepos != PT_BYTE)
18743 return 1;
18745 return 0;
18749 /* Highlight trailing whitespace, if any, in ROW. */
18751 static void
18752 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18754 int used = row->used[TEXT_AREA];
18756 if (used)
18758 struct glyph *start = row->glyphs[TEXT_AREA];
18759 struct glyph *glyph = start + used - 1;
18761 if (row->reversed_p)
18763 /* Right-to-left rows need to be processed in the opposite
18764 direction, so swap the edge pointers. */
18765 glyph = start;
18766 start = row->glyphs[TEXT_AREA] + used - 1;
18769 /* Skip over glyphs inserted to display the cursor at the
18770 end of a line, for extending the face of the last glyph
18771 to the end of the line on terminals, and for truncation
18772 and continuation glyphs. */
18773 if (!row->reversed_p)
18775 while (glyph >= start
18776 && glyph->type == CHAR_GLYPH
18777 && INTEGERP (glyph->object))
18778 --glyph;
18780 else
18782 while (glyph <= start
18783 && glyph->type == CHAR_GLYPH
18784 && INTEGERP (glyph->object))
18785 ++glyph;
18788 /* If last glyph is a space or stretch, and it's trailing
18789 whitespace, set the face of all trailing whitespace glyphs in
18790 IT->glyph_row to `trailing-whitespace'. */
18791 if ((row->reversed_p ? glyph <= start : glyph >= start)
18792 && BUFFERP (glyph->object)
18793 && (glyph->type == STRETCH_GLYPH
18794 || (glyph->type == CHAR_GLYPH
18795 && glyph->u.ch == ' '))
18796 && trailing_whitespace_p (glyph->charpos))
18798 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18799 if (face_id < 0)
18800 return;
18802 if (!row->reversed_p)
18804 while (glyph >= start
18805 && BUFFERP (glyph->object)
18806 && (glyph->type == STRETCH_GLYPH
18807 || (glyph->type == CHAR_GLYPH
18808 && glyph->u.ch == ' ')))
18809 (glyph--)->face_id = face_id;
18811 else
18813 while (glyph <= start
18814 && BUFFERP (glyph->object)
18815 && (glyph->type == STRETCH_GLYPH
18816 || (glyph->type == CHAR_GLYPH
18817 && glyph->u.ch == ' ')))
18818 (glyph++)->face_id = face_id;
18825 /* Value is non-zero if glyph row ROW should be
18826 considered to hold the buffer position CHARPOS. */
18828 static int
18829 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18831 int result = 1;
18833 if (charpos == CHARPOS (row->end.pos)
18834 || charpos == MATRIX_ROW_END_CHARPOS (row))
18836 /* Suppose the row ends on a string.
18837 Unless the row is continued, that means it ends on a newline
18838 in the string. If it's anything other than a display string
18839 (e.g., a before-string from an overlay), we don't want the
18840 cursor there. (This heuristic seems to give the optimal
18841 behavior for the various types of multi-line strings.)
18842 One exception: if the string has `cursor' property on one of
18843 its characters, we _do_ want the cursor there. */
18844 if (CHARPOS (row->end.string_pos) >= 0)
18846 if (row->continued_p)
18847 result = 1;
18848 else
18850 /* Check for `display' property. */
18851 struct glyph *beg = row->glyphs[TEXT_AREA];
18852 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18853 struct glyph *glyph;
18855 result = 0;
18856 for (glyph = end; glyph >= beg; --glyph)
18857 if (STRINGP (glyph->object))
18859 Lisp_Object prop
18860 = Fget_char_property (make_number (charpos),
18861 Qdisplay, Qnil);
18862 result =
18863 (!NILP (prop)
18864 && display_prop_string_p (prop, glyph->object));
18865 /* If there's a `cursor' property on one of the
18866 string's characters, this row is a cursor row,
18867 even though this is not a display string. */
18868 if (!result)
18870 Lisp_Object s = glyph->object;
18872 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18874 ptrdiff_t gpos = glyph->charpos;
18876 if (!NILP (Fget_char_property (make_number (gpos),
18877 Qcursor, s)))
18879 result = 1;
18880 break;
18884 break;
18888 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18890 /* If the row ends in middle of a real character,
18891 and the line is continued, we want the cursor here.
18892 That's because CHARPOS (ROW->end.pos) would equal
18893 PT if PT is before the character. */
18894 if (!row->ends_in_ellipsis_p)
18895 result = row->continued_p;
18896 else
18897 /* If the row ends in an ellipsis, then
18898 CHARPOS (ROW->end.pos) will equal point after the
18899 invisible text. We want that position to be displayed
18900 after the ellipsis. */
18901 result = 0;
18903 /* If the row ends at ZV, display the cursor at the end of that
18904 row instead of at the start of the row below. */
18905 else if (row->ends_at_zv_p)
18906 result = 1;
18907 else
18908 result = 0;
18911 return result;
18914 /* Value is non-zero if glyph row ROW should be
18915 used to hold the cursor. */
18917 static int
18918 cursor_row_p (struct glyph_row *row)
18920 return row_for_charpos_p (row, PT);
18925 /* Push the property PROP so that it will be rendered at the current
18926 position in IT. Return 1 if PROP was successfully pushed, 0
18927 otherwise. Called from handle_line_prefix to handle the
18928 `line-prefix' and `wrap-prefix' properties. */
18930 static int
18931 push_prefix_prop (struct it *it, Lisp_Object prop)
18933 struct text_pos pos =
18934 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18936 eassert (it->method == GET_FROM_BUFFER
18937 || it->method == GET_FROM_DISPLAY_VECTOR
18938 || it->method == GET_FROM_STRING);
18940 /* We need to save the current buffer/string position, so it will be
18941 restored by pop_it, because iterate_out_of_display_property
18942 depends on that being set correctly, but some situations leave
18943 it->position not yet set when this function is called. */
18944 push_it (it, &pos);
18946 if (STRINGP (prop))
18948 if (SCHARS (prop) == 0)
18950 pop_it (it);
18951 return 0;
18954 it->string = prop;
18955 it->string_from_prefix_prop_p = 1;
18956 it->multibyte_p = STRING_MULTIBYTE (it->string);
18957 it->current.overlay_string_index = -1;
18958 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18959 it->end_charpos = it->string_nchars = SCHARS (it->string);
18960 it->method = GET_FROM_STRING;
18961 it->stop_charpos = 0;
18962 it->prev_stop = 0;
18963 it->base_level_stop = 0;
18965 /* Force paragraph direction to be that of the parent
18966 buffer/string. */
18967 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18968 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18969 else
18970 it->paragraph_embedding = L2R;
18972 /* Set up the bidi iterator for this display string. */
18973 if (it->bidi_p)
18975 it->bidi_it.string.lstring = it->string;
18976 it->bidi_it.string.s = NULL;
18977 it->bidi_it.string.schars = it->end_charpos;
18978 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18979 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18980 it->bidi_it.string.unibyte = !it->multibyte_p;
18981 it->bidi_it.w = it->w;
18982 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18985 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18987 it->method = GET_FROM_STRETCH;
18988 it->object = prop;
18990 #ifdef HAVE_WINDOW_SYSTEM
18991 else if (IMAGEP (prop))
18993 it->what = IT_IMAGE;
18994 it->image_id = lookup_image (it->f, prop);
18995 it->method = GET_FROM_IMAGE;
18997 #endif /* HAVE_WINDOW_SYSTEM */
18998 else
19000 pop_it (it); /* bogus display property, give up */
19001 return 0;
19004 return 1;
19007 /* Return the character-property PROP at the current position in IT. */
19009 static Lisp_Object
19010 get_it_property (struct it *it, Lisp_Object prop)
19012 Lisp_Object position, object = it->object;
19014 if (STRINGP (object))
19015 position = make_number (IT_STRING_CHARPOS (*it));
19016 else if (BUFFERP (object))
19018 position = make_number (IT_CHARPOS (*it));
19019 object = it->window;
19021 else
19022 return Qnil;
19024 return Fget_char_property (position, prop, object);
19027 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19029 static void
19030 handle_line_prefix (struct it *it)
19032 Lisp_Object prefix;
19034 if (it->continuation_lines_width > 0)
19036 prefix = get_it_property (it, Qwrap_prefix);
19037 if (NILP (prefix))
19038 prefix = Vwrap_prefix;
19040 else
19042 prefix = get_it_property (it, Qline_prefix);
19043 if (NILP (prefix))
19044 prefix = Vline_prefix;
19046 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19048 /* If the prefix is wider than the window, and we try to wrap
19049 it, it would acquire its own wrap prefix, and so on till the
19050 iterator stack overflows. So, don't wrap the prefix. */
19051 it->line_wrap = TRUNCATE;
19052 it->avoid_cursor_p = 1;
19058 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19059 only for R2L lines from display_line and display_string, when they
19060 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19061 the line/string needs to be continued on the next glyph row. */
19062 static void
19063 unproduce_glyphs (struct it *it, int n)
19065 struct glyph *glyph, *end;
19067 eassert (it->glyph_row);
19068 eassert (it->glyph_row->reversed_p);
19069 eassert (it->area == TEXT_AREA);
19070 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19072 if (n > it->glyph_row->used[TEXT_AREA])
19073 n = it->glyph_row->used[TEXT_AREA];
19074 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19075 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19076 for ( ; glyph < end; glyph++)
19077 glyph[-n] = *glyph;
19080 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19081 and ROW->maxpos. */
19082 static void
19083 find_row_edges (struct it *it, struct glyph_row *row,
19084 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19085 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19087 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19088 lines' rows is implemented for bidi-reordered rows. */
19090 /* ROW->minpos is the value of min_pos, the minimal buffer position
19091 we have in ROW, or ROW->start.pos if that is smaller. */
19092 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19093 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19094 else
19095 /* We didn't find buffer positions smaller than ROW->start, or
19096 didn't find _any_ valid buffer positions in any of the glyphs,
19097 so we must trust the iterator's computed positions. */
19098 row->minpos = row->start.pos;
19099 if (max_pos <= 0)
19101 max_pos = CHARPOS (it->current.pos);
19102 max_bpos = BYTEPOS (it->current.pos);
19105 /* Here are the various use-cases for ending the row, and the
19106 corresponding values for ROW->maxpos:
19108 Line ends in a newline from buffer eol_pos + 1
19109 Line is continued from buffer max_pos + 1
19110 Line is truncated on right it->current.pos
19111 Line ends in a newline from string max_pos + 1(*)
19112 (*) + 1 only when line ends in a forward scan
19113 Line is continued from string max_pos
19114 Line is continued from display vector max_pos
19115 Line is entirely from a string min_pos == max_pos
19116 Line is entirely from a display vector min_pos == max_pos
19117 Line that ends at ZV ZV
19119 If you discover other use-cases, please add them here as
19120 appropriate. */
19121 if (row->ends_at_zv_p)
19122 row->maxpos = it->current.pos;
19123 else if (row->used[TEXT_AREA])
19125 int seen_this_string = 0;
19126 struct glyph_row *r1 = row - 1;
19128 /* Did we see the same display string on the previous row? */
19129 if (STRINGP (it->object)
19130 /* this is not the first row */
19131 && row > it->w->desired_matrix->rows
19132 /* previous row is not the header line */
19133 && !r1->mode_line_p
19134 /* previous row also ends in a newline from a string */
19135 && r1->ends_in_newline_from_string_p)
19137 struct glyph *start, *end;
19139 /* Search for the last glyph of the previous row that came
19140 from buffer or string. Depending on whether the row is
19141 L2R or R2L, we need to process it front to back or the
19142 other way round. */
19143 if (!r1->reversed_p)
19145 start = r1->glyphs[TEXT_AREA];
19146 end = start + r1->used[TEXT_AREA];
19147 /* Glyphs inserted by redisplay have an integer (zero)
19148 as their object. */
19149 while (end > start
19150 && INTEGERP ((end - 1)->object)
19151 && (end - 1)->charpos <= 0)
19152 --end;
19153 if (end > start)
19155 if (EQ ((end - 1)->object, it->object))
19156 seen_this_string = 1;
19158 else
19159 /* If all the glyphs of the previous row were inserted
19160 by redisplay, it means the previous row was
19161 produced from a single newline, which is only
19162 possible if that newline came from the same string
19163 as the one which produced this ROW. */
19164 seen_this_string = 1;
19166 else
19168 end = r1->glyphs[TEXT_AREA] - 1;
19169 start = end + r1->used[TEXT_AREA];
19170 while (end < start
19171 && INTEGERP ((end + 1)->object)
19172 && (end + 1)->charpos <= 0)
19173 ++end;
19174 if (end < start)
19176 if (EQ ((end + 1)->object, it->object))
19177 seen_this_string = 1;
19179 else
19180 seen_this_string = 1;
19183 /* Take note of each display string that covers a newline only
19184 once, the first time we see it. This is for when a display
19185 string includes more than one newline in it. */
19186 if (row->ends_in_newline_from_string_p && !seen_this_string)
19188 /* If we were scanning the buffer forward when we displayed
19189 the string, we want to account for at least one buffer
19190 position that belongs to this row (position covered by
19191 the display string), so that cursor positioning will
19192 consider this row as a candidate when point is at the end
19193 of the visual line represented by this row. This is not
19194 required when scanning back, because max_pos will already
19195 have a much larger value. */
19196 if (CHARPOS (row->end.pos) > max_pos)
19197 INC_BOTH (max_pos, max_bpos);
19198 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19200 else if (CHARPOS (it->eol_pos) > 0)
19201 SET_TEXT_POS (row->maxpos,
19202 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19203 else if (row->continued_p)
19205 /* If max_pos is different from IT's current position, it
19206 means IT->method does not belong to the display element
19207 at max_pos. However, it also means that the display
19208 element at max_pos was displayed in its entirety on this
19209 line, which is equivalent to saying that the next line
19210 starts at the next buffer position. */
19211 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19212 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19213 else
19215 INC_BOTH (max_pos, max_bpos);
19216 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19219 else if (row->truncated_on_right_p)
19220 /* display_line already called reseat_at_next_visible_line_start,
19221 which puts the iterator at the beginning of the next line, in
19222 the logical order. */
19223 row->maxpos = it->current.pos;
19224 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19225 /* A line that is entirely from a string/image/stretch... */
19226 row->maxpos = row->minpos;
19227 else
19228 emacs_abort ();
19230 else
19231 row->maxpos = it->current.pos;
19234 /* Construct the glyph row IT->glyph_row in the desired matrix of
19235 IT->w from text at the current position of IT. See dispextern.h
19236 for an overview of struct it. Value is non-zero if
19237 IT->glyph_row displays text, as opposed to a line displaying ZV
19238 only. */
19240 static int
19241 display_line (struct it *it)
19243 struct glyph_row *row = it->glyph_row;
19244 Lisp_Object overlay_arrow_string;
19245 struct it wrap_it;
19246 void *wrap_data = NULL;
19247 int may_wrap = 0, wrap_x IF_LINT (= 0);
19248 int wrap_row_used = -1;
19249 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19250 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19251 int wrap_row_extra_line_spacing IF_LINT (= 0);
19252 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19253 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19254 int cvpos;
19255 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19256 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19258 /* We always start displaying at hpos zero even if hscrolled. */
19259 eassert (it->hpos == 0 && it->current_x == 0);
19261 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19262 >= it->w->desired_matrix->nrows)
19264 it->w->nrows_scale_factor++;
19265 fonts_changed_p = 1;
19266 return 0;
19269 /* Is IT->w showing the region? */
19270 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19272 /* Clear the result glyph row and enable it. */
19273 prepare_desired_row (row);
19275 row->y = it->current_y;
19276 row->start = it->start;
19277 row->continuation_lines_width = it->continuation_lines_width;
19278 row->displays_text_p = 1;
19279 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19280 it->starts_in_middle_of_char_p = 0;
19282 /* Arrange the overlays nicely for our purposes. Usually, we call
19283 display_line on only one line at a time, in which case this
19284 can't really hurt too much, or we call it on lines which appear
19285 one after another in the buffer, in which case all calls to
19286 recenter_overlay_lists but the first will be pretty cheap. */
19287 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19289 /* Move over display elements that are not visible because we are
19290 hscrolled. This may stop at an x-position < IT->first_visible_x
19291 if the first glyph is partially visible or if we hit a line end. */
19292 if (it->current_x < it->first_visible_x)
19294 enum move_it_result move_result;
19296 this_line_min_pos = row->start.pos;
19297 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19298 MOVE_TO_POS | MOVE_TO_X);
19299 /* If we are under a large hscroll, move_it_in_display_line_to
19300 could hit the end of the line without reaching
19301 it->first_visible_x. Pretend that we did reach it. This is
19302 especially important on a TTY, where we will call
19303 extend_face_to_end_of_line, which needs to know how many
19304 blank glyphs to produce. */
19305 if (it->current_x < it->first_visible_x
19306 && (move_result == MOVE_NEWLINE_OR_CR
19307 || move_result == MOVE_POS_MATCH_OR_ZV))
19308 it->current_x = it->first_visible_x;
19310 /* Record the smallest positions seen while we moved over
19311 display elements that are not visible. This is needed by
19312 redisplay_internal for optimizing the case where the cursor
19313 stays inside the same line. The rest of this function only
19314 considers positions that are actually displayed, so
19315 RECORD_MAX_MIN_POS will not otherwise record positions that
19316 are hscrolled to the left of the left edge of the window. */
19317 min_pos = CHARPOS (this_line_min_pos);
19318 min_bpos = BYTEPOS (this_line_min_pos);
19320 else
19322 /* We only do this when not calling `move_it_in_display_line_to'
19323 above, because move_it_in_display_line_to calls
19324 handle_line_prefix itself. */
19325 handle_line_prefix (it);
19328 /* Get the initial row height. This is either the height of the
19329 text hscrolled, if there is any, or zero. */
19330 row->ascent = it->max_ascent;
19331 row->height = it->max_ascent + it->max_descent;
19332 row->phys_ascent = it->max_phys_ascent;
19333 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19334 row->extra_line_spacing = it->max_extra_line_spacing;
19336 /* Utility macro to record max and min buffer positions seen until now. */
19337 #define RECORD_MAX_MIN_POS(IT) \
19338 do \
19340 int composition_p = !STRINGP ((IT)->string) \
19341 && ((IT)->what == IT_COMPOSITION); \
19342 ptrdiff_t current_pos = \
19343 composition_p ? (IT)->cmp_it.charpos \
19344 : IT_CHARPOS (*(IT)); \
19345 ptrdiff_t current_bpos = \
19346 composition_p ? CHAR_TO_BYTE (current_pos) \
19347 : IT_BYTEPOS (*(IT)); \
19348 if (current_pos < min_pos) \
19350 min_pos = current_pos; \
19351 min_bpos = current_bpos; \
19353 if (IT_CHARPOS (*it) > max_pos) \
19355 max_pos = IT_CHARPOS (*it); \
19356 max_bpos = IT_BYTEPOS (*it); \
19359 while (0)
19361 /* Loop generating characters. The loop is left with IT on the next
19362 character to display. */
19363 while (1)
19365 int n_glyphs_before, hpos_before, x_before;
19366 int x, nglyphs;
19367 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19369 /* Retrieve the next thing to display. Value is zero if end of
19370 buffer reached. */
19371 if (!get_next_display_element (it))
19373 /* Maybe add a space at the end of this line that is used to
19374 display the cursor there under X. Set the charpos of the
19375 first glyph of blank lines not corresponding to any text
19376 to -1. */
19377 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19378 row->exact_window_width_line_p = 1;
19379 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19380 || row->used[TEXT_AREA] == 0)
19382 row->glyphs[TEXT_AREA]->charpos = -1;
19383 row->displays_text_p = 0;
19385 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19386 && (!MINI_WINDOW_P (it->w)
19387 || (minibuf_level && EQ (it->window, minibuf_window))))
19388 row->indicate_empty_line_p = 1;
19391 it->continuation_lines_width = 0;
19392 row->ends_at_zv_p = 1;
19393 /* A row that displays right-to-left text must always have
19394 its last face extended all the way to the end of line,
19395 even if this row ends in ZV, because we still write to
19396 the screen left to right. We also need to extend the
19397 last face if the default face is remapped to some
19398 different face, otherwise the functions that clear
19399 portions of the screen will clear with the default face's
19400 background color. */
19401 if (row->reversed_p
19402 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19403 extend_face_to_end_of_line (it);
19404 break;
19407 /* Now, get the metrics of what we want to display. This also
19408 generates glyphs in `row' (which is IT->glyph_row). */
19409 n_glyphs_before = row->used[TEXT_AREA];
19410 x = it->current_x;
19412 /* Remember the line height so far in case the next element doesn't
19413 fit on the line. */
19414 if (it->line_wrap != TRUNCATE)
19416 ascent = it->max_ascent;
19417 descent = it->max_descent;
19418 phys_ascent = it->max_phys_ascent;
19419 phys_descent = it->max_phys_descent;
19421 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19423 if (IT_DISPLAYING_WHITESPACE (it))
19424 may_wrap = 1;
19425 else if (may_wrap)
19427 SAVE_IT (wrap_it, *it, wrap_data);
19428 wrap_x = x;
19429 wrap_row_used = row->used[TEXT_AREA];
19430 wrap_row_ascent = row->ascent;
19431 wrap_row_height = row->height;
19432 wrap_row_phys_ascent = row->phys_ascent;
19433 wrap_row_phys_height = row->phys_height;
19434 wrap_row_extra_line_spacing = row->extra_line_spacing;
19435 wrap_row_min_pos = min_pos;
19436 wrap_row_min_bpos = min_bpos;
19437 wrap_row_max_pos = max_pos;
19438 wrap_row_max_bpos = max_bpos;
19439 may_wrap = 0;
19444 PRODUCE_GLYPHS (it);
19446 /* If this display element was in marginal areas, continue with
19447 the next one. */
19448 if (it->area != TEXT_AREA)
19450 row->ascent = max (row->ascent, it->max_ascent);
19451 row->height = max (row->height, it->max_ascent + it->max_descent);
19452 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19453 row->phys_height = max (row->phys_height,
19454 it->max_phys_ascent + it->max_phys_descent);
19455 row->extra_line_spacing = max (row->extra_line_spacing,
19456 it->max_extra_line_spacing);
19457 set_iterator_to_next (it, 1);
19458 continue;
19461 /* Does the display element fit on the line? If we truncate
19462 lines, we should draw past the right edge of the window. If
19463 we don't truncate, we want to stop so that we can display the
19464 continuation glyph before the right margin. If lines are
19465 continued, there are two possible strategies for characters
19466 resulting in more than 1 glyph (e.g. tabs): Display as many
19467 glyphs as possible in this line and leave the rest for the
19468 continuation line, or display the whole element in the next
19469 line. Original redisplay did the former, so we do it also. */
19470 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19471 hpos_before = it->hpos;
19472 x_before = x;
19474 if (/* Not a newline. */
19475 nglyphs > 0
19476 /* Glyphs produced fit entirely in the line. */
19477 && it->current_x < it->last_visible_x)
19479 it->hpos += nglyphs;
19480 row->ascent = max (row->ascent, it->max_ascent);
19481 row->height = max (row->height, it->max_ascent + it->max_descent);
19482 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19483 row->phys_height = max (row->phys_height,
19484 it->max_phys_ascent + it->max_phys_descent);
19485 row->extra_line_spacing = max (row->extra_line_spacing,
19486 it->max_extra_line_spacing);
19487 if (it->current_x - it->pixel_width < it->first_visible_x)
19488 row->x = x - it->first_visible_x;
19489 /* Record the maximum and minimum buffer positions seen so
19490 far in glyphs that will be displayed by this row. */
19491 if (it->bidi_p)
19492 RECORD_MAX_MIN_POS (it);
19494 else
19496 int i, new_x;
19497 struct glyph *glyph;
19499 for (i = 0; i < nglyphs; ++i, x = new_x)
19501 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19502 new_x = x + glyph->pixel_width;
19504 if (/* Lines are continued. */
19505 it->line_wrap != TRUNCATE
19506 && (/* Glyph doesn't fit on the line. */
19507 new_x > it->last_visible_x
19508 /* Or it fits exactly on a window system frame. */
19509 || (new_x == it->last_visible_x
19510 && FRAME_WINDOW_P (it->f)
19511 && (row->reversed_p
19512 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19513 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19515 /* End of a continued line. */
19517 if (it->hpos == 0
19518 || (new_x == it->last_visible_x
19519 && FRAME_WINDOW_P (it->f)
19520 && (row->reversed_p
19521 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19522 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19524 /* Current glyph is the only one on the line or
19525 fits exactly on the line. We must continue
19526 the line because we can't draw the cursor
19527 after the glyph. */
19528 row->continued_p = 1;
19529 it->current_x = new_x;
19530 it->continuation_lines_width += new_x;
19531 ++it->hpos;
19532 if (i == nglyphs - 1)
19534 /* If line-wrap is on, check if a previous
19535 wrap point was found. */
19536 if (wrap_row_used > 0
19537 /* Even if there is a previous wrap
19538 point, continue the line here as
19539 usual, if (i) the previous character
19540 was a space or tab AND (ii) the
19541 current character is not. */
19542 && (!may_wrap
19543 || IT_DISPLAYING_WHITESPACE (it)))
19544 goto back_to_wrap;
19546 /* Record the maximum and minimum buffer
19547 positions seen so far in glyphs that will be
19548 displayed by this row. */
19549 if (it->bidi_p)
19550 RECORD_MAX_MIN_POS (it);
19551 set_iterator_to_next (it, 1);
19552 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19554 if (!get_next_display_element (it))
19556 row->exact_window_width_line_p = 1;
19557 it->continuation_lines_width = 0;
19558 row->continued_p = 0;
19559 row->ends_at_zv_p = 1;
19561 else if (ITERATOR_AT_END_OF_LINE_P (it))
19563 row->continued_p = 0;
19564 row->exact_window_width_line_p = 1;
19568 else if (it->bidi_p)
19569 RECORD_MAX_MIN_POS (it);
19571 else if (CHAR_GLYPH_PADDING_P (*glyph)
19572 && !FRAME_WINDOW_P (it->f))
19574 /* A padding glyph that doesn't fit on this line.
19575 This means the whole character doesn't fit
19576 on the line. */
19577 if (row->reversed_p)
19578 unproduce_glyphs (it, row->used[TEXT_AREA]
19579 - n_glyphs_before);
19580 row->used[TEXT_AREA] = n_glyphs_before;
19582 /* Fill the rest of the row with continuation
19583 glyphs like in 20.x. */
19584 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19585 < row->glyphs[1 + TEXT_AREA])
19586 produce_special_glyphs (it, IT_CONTINUATION);
19588 row->continued_p = 1;
19589 it->current_x = x_before;
19590 it->continuation_lines_width += x_before;
19592 /* Restore the height to what it was before the
19593 element not fitting on the line. */
19594 it->max_ascent = ascent;
19595 it->max_descent = descent;
19596 it->max_phys_ascent = phys_ascent;
19597 it->max_phys_descent = phys_descent;
19599 else if (wrap_row_used > 0)
19601 back_to_wrap:
19602 if (row->reversed_p)
19603 unproduce_glyphs (it,
19604 row->used[TEXT_AREA] - wrap_row_used);
19605 RESTORE_IT (it, &wrap_it, wrap_data);
19606 it->continuation_lines_width += wrap_x;
19607 row->used[TEXT_AREA] = wrap_row_used;
19608 row->ascent = wrap_row_ascent;
19609 row->height = wrap_row_height;
19610 row->phys_ascent = wrap_row_phys_ascent;
19611 row->phys_height = wrap_row_phys_height;
19612 row->extra_line_spacing = wrap_row_extra_line_spacing;
19613 min_pos = wrap_row_min_pos;
19614 min_bpos = wrap_row_min_bpos;
19615 max_pos = wrap_row_max_pos;
19616 max_bpos = wrap_row_max_bpos;
19617 row->continued_p = 1;
19618 row->ends_at_zv_p = 0;
19619 row->exact_window_width_line_p = 0;
19620 it->continuation_lines_width += x;
19622 /* Make sure that a non-default face is extended
19623 up to the right margin of the window. */
19624 extend_face_to_end_of_line (it);
19626 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19628 /* A TAB that extends past the right edge of the
19629 window. This produces a single glyph on
19630 window system frames. We leave the glyph in
19631 this row and let it fill the row, but don't
19632 consume the TAB. */
19633 if ((row->reversed_p
19634 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19635 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19636 produce_special_glyphs (it, IT_CONTINUATION);
19637 it->continuation_lines_width += it->last_visible_x;
19638 row->ends_in_middle_of_char_p = 1;
19639 row->continued_p = 1;
19640 glyph->pixel_width = it->last_visible_x - x;
19641 it->starts_in_middle_of_char_p = 1;
19643 else
19645 /* Something other than a TAB that draws past
19646 the right edge of the window. Restore
19647 positions to values before the element. */
19648 if (row->reversed_p)
19649 unproduce_glyphs (it, row->used[TEXT_AREA]
19650 - (n_glyphs_before + i));
19651 row->used[TEXT_AREA] = n_glyphs_before + i;
19653 /* Display continuation glyphs. */
19654 it->current_x = x_before;
19655 it->continuation_lines_width += x;
19656 if (!FRAME_WINDOW_P (it->f)
19657 || (row->reversed_p
19658 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19659 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19660 produce_special_glyphs (it, IT_CONTINUATION);
19661 row->continued_p = 1;
19663 extend_face_to_end_of_line (it);
19665 if (nglyphs > 1 && i > 0)
19667 row->ends_in_middle_of_char_p = 1;
19668 it->starts_in_middle_of_char_p = 1;
19671 /* Restore the height to what it was before the
19672 element not fitting on the line. */
19673 it->max_ascent = ascent;
19674 it->max_descent = descent;
19675 it->max_phys_ascent = phys_ascent;
19676 it->max_phys_descent = phys_descent;
19679 break;
19681 else if (new_x > it->first_visible_x)
19683 /* Increment number of glyphs actually displayed. */
19684 ++it->hpos;
19686 /* Record the maximum and minimum buffer positions
19687 seen so far in glyphs that will be displayed by
19688 this row. */
19689 if (it->bidi_p)
19690 RECORD_MAX_MIN_POS (it);
19692 if (x < it->first_visible_x)
19693 /* Glyph is partially visible, i.e. row starts at
19694 negative X position. */
19695 row->x = x - it->first_visible_x;
19697 else
19699 /* Glyph is completely off the left margin of the
19700 window. This should not happen because of the
19701 move_it_in_display_line at the start of this
19702 function, unless the text display area of the
19703 window is empty. */
19704 eassert (it->first_visible_x <= it->last_visible_x);
19707 /* Even if this display element produced no glyphs at all,
19708 we want to record its position. */
19709 if (it->bidi_p && nglyphs == 0)
19710 RECORD_MAX_MIN_POS (it);
19712 row->ascent = max (row->ascent, it->max_ascent);
19713 row->height = max (row->height, it->max_ascent + it->max_descent);
19714 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19715 row->phys_height = max (row->phys_height,
19716 it->max_phys_ascent + it->max_phys_descent);
19717 row->extra_line_spacing = max (row->extra_line_spacing,
19718 it->max_extra_line_spacing);
19720 /* End of this display line if row is continued. */
19721 if (row->continued_p || row->ends_at_zv_p)
19722 break;
19725 at_end_of_line:
19726 /* Is this a line end? If yes, we're also done, after making
19727 sure that a non-default face is extended up to the right
19728 margin of the window. */
19729 if (ITERATOR_AT_END_OF_LINE_P (it))
19731 int used_before = row->used[TEXT_AREA];
19733 row->ends_in_newline_from_string_p = STRINGP (it->object);
19735 /* Add a space at the end of the line that is used to
19736 display the cursor there. */
19737 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19738 append_space_for_newline (it, 0);
19740 /* Extend the face to the end of the line. */
19741 extend_face_to_end_of_line (it);
19743 /* Make sure we have the position. */
19744 if (used_before == 0)
19745 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19747 /* Record the position of the newline, for use in
19748 find_row_edges. */
19749 it->eol_pos = it->current.pos;
19751 /* Consume the line end. This skips over invisible lines. */
19752 set_iterator_to_next (it, 1);
19753 it->continuation_lines_width = 0;
19754 break;
19757 /* Proceed with next display element. Note that this skips
19758 over lines invisible because of selective display. */
19759 set_iterator_to_next (it, 1);
19761 /* If we truncate lines, we are done when the last displayed
19762 glyphs reach past the right margin of the window. */
19763 if (it->line_wrap == TRUNCATE
19764 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19765 ? (it->current_x >= it->last_visible_x)
19766 : (it->current_x > it->last_visible_x)))
19768 /* Maybe add truncation glyphs. */
19769 if (!FRAME_WINDOW_P (it->f)
19770 || (row->reversed_p
19771 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19772 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19774 int i, n;
19776 if (!row->reversed_p)
19778 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19779 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19780 break;
19782 else
19784 for (i = 0; i < row->used[TEXT_AREA]; i++)
19785 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19786 break;
19787 /* Remove any padding glyphs at the front of ROW, to
19788 make room for the truncation glyphs we will be
19789 adding below. The loop below always inserts at
19790 least one truncation glyph, so also remove the
19791 last glyph added to ROW. */
19792 unproduce_glyphs (it, i + 1);
19793 /* Adjust i for the loop below. */
19794 i = row->used[TEXT_AREA] - (i + 1);
19797 it->current_x = x_before;
19798 if (!FRAME_WINDOW_P (it->f))
19800 for (n = row->used[TEXT_AREA]; i < n; ++i)
19802 row->used[TEXT_AREA] = i;
19803 produce_special_glyphs (it, IT_TRUNCATION);
19806 else
19808 row->used[TEXT_AREA] = i;
19809 produce_special_glyphs (it, IT_TRUNCATION);
19812 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19814 /* Don't truncate if we can overflow newline into fringe. */
19815 if (!get_next_display_element (it))
19817 it->continuation_lines_width = 0;
19818 row->ends_at_zv_p = 1;
19819 row->exact_window_width_line_p = 1;
19820 break;
19822 if (ITERATOR_AT_END_OF_LINE_P (it))
19824 row->exact_window_width_line_p = 1;
19825 goto at_end_of_line;
19827 it->current_x = x_before;
19830 row->truncated_on_right_p = 1;
19831 it->continuation_lines_width = 0;
19832 reseat_at_next_visible_line_start (it, 0);
19833 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19834 it->hpos = hpos_before;
19835 break;
19839 if (wrap_data)
19840 bidi_unshelve_cache (wrap_data, 1);
19842 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19843 at the left window margin. */
19844 if (it->first_visible_x
19845 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19847 if (!FRAME_WINDOW_P (it->f)
19848 || (row->reversed_p
19849 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19850 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19851 insert_left_trunc_glyphs (it);
19852 row->truncated_on_left_p = 1;
19855 /* Remember the position at which this line ends.
19857 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19858 cannot be before the call to find_row_edges below, since that is
19859 where these positions are determined. */
19860 row->end = it->current;
19861 if (!it->bidi_p)
19863 row->minpos = row->start.pos;
19864 row->maxpos = row->end.pos;
19866 else
19868 /* ROW->minpos and ROW->maxpos must be the smallest and
19869 `1 + the largest' buffer positions in ROW. But if ROW was
19870 bidi-reordered, these two positions can be anywhere in the
19871 row, so we must determine them now. */
19872 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19875 /* If the start of this line is the overlay arrow-position, then
19876 mark this glyph row as the one containing the overlay arrow.
19877 This is clearly a mess with variable size fonts. It would be
19878 better to let it be displayed like cursors under X. */
19879 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19880 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19881 !NILP (overlay_arrow_string)))
19883 /* Overlay arrow in window redisplay is a fringe bitmap. */
19884 if (STRINGP (overlay_arrow_string))
19886 struct glyph_row *arrow_row
19887 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19888 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19889 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19890 struct glyph *p = row->glyphs[TEXT_AREA];
19891 struct glyph *p2, *end;
19893 /* Copy the arrow glyphs. */
19894 while (glyph < arrow_end)
19895 *p++ = *glyph++;
19897 /* Throw away padding glyphs. */
19898 p2 = p;
19899 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19900 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19901 ++p2;
19902 if (p2 > p)
19904 while (p2 < end)
19905 *p++ = *p2++;
19906 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19909 else
19911 eassert (INTEGERP (overlay_arrow_string));
19912 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19914 overlay_arrow_seen = 1;
19917 /* Highlight trailing whitespace. */
19918 if (!NILP (Vshow_trailing_whitespace))
19919 highlight_trailing_whitespace (it->f, it->glyph_row);
19921 /* Compute pixel dimensions of this line. */
19922 compute_line_metrics (it);
19924 /* Implementation note: No changes in the glyphs of ROW or in their
19925 faces can be done past this point, because compute_line_metrics
19926 computes ROW's hash value and stores it within the glyph_row
19927 structure. */
19929 /* Record whether this row ends inside an ellipsis. */
19930 row->ends_in_ellipsis_p
19931 = (it->method == GET_FROM_DISPLAY_VECTOR
19932 && it->ellipsis_p);
19934 /* Save fringe bitmaps in this row. */
19935 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19936 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19937 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19938 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19940 it->left_user_fringe_bitmap = 0;
19941 it->left_user_fringe_face_id = 0;
19942 it->right_user_fringe_bitmap = 0;
19943 it->right_user_fringe_face_id = 0;
19945 /* Maybe set the cursor. */
19946 cvpos = it->w->cursor.vpos;
19947 if ((cvpos < 0
19948 /* In bidi-reordered rows, keep checking for proper cursor
19949 position even if one has been found already, because buffer
19950 positions in such rows change non-linearly with ROW->VPOS,
19951 when a line is continued. One exception: when we are at ZV,
19952 display cursor on the first suitable glyph row, since all
19953 the empty rows after that also have their position set to ZV. */
19954 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19955 lines' rows is implemented for bidi-reordered rows. */
19956 || (it->bidi_p
19957 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19958 && PT >= MATRIX_ROW_START_CHARPOS (row)
19959 && PT <= MATRIX_ROW_END_CHARPOS (row)
19960 && cursor_row_p (row))
19961 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19963 /* Prepare for the next line. This line starts horizontally at (X
19964 HPOS) = (0 0). Vertical positions are incremented. As a
19965 convenience for the caller, IT->glyph_row is set to the next
19966 row to be used. */
19967 it->current_x = it->hpos = 0;
19968 it->current_y += row->height;
19969 SET_TEXT_POS (it->eol_pos, 0, 0);
19970 ++it->vpos;
19971 ++it->glyph_row;
19972 /* The next row should by default use the same value of the
19973 reversed_p flag as this one. set_iterator_to_next decides when
19974 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19975 the flag accordingly. */
19976 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19977 it->glyph_row->reversed_p = row->reversed_p;
19978 it->start = row->end;
19979 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19981 #undef RECORD_MAX_MIN_POS
19984 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19985 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19986 doc: /* Return paragraph direction at point in BUFFER.
19987 Value is either `left-to-right' or `right-to-left'.
19988 If BUFFER is omitted or nil, it defaults to the current buffer.
19990 Paragraph direction determines how the text in the paragraph is displayed.
19991 In left-to-right paragraphs, text begins at the left margin of the window
19992 and the reading direction is generally left to right. In right-to-left
19993 paragraphs, text begins at the right margin and is read from right to left.
19995 See also `bidi-paragraph-direction'. */)
19996 (Lisp_Object buffer)
19998 struct buffer *buf = current_buffer;
19999 struct buffer *old = buf;
20001 if (! NILP (buffer))
20003 CHECK_BUFFER (buffer);
20004 buf = XBUFFER (buffer);
20007 if (NILP (BVAR (buf, bidi_display_reordering))
20008 || NILP (BVAR (buf, enable_multibyte_characters))
20009 /* When we are loading loadup.el, the character property tables
20010 needed for bidi iteration are not yet available. */
20011 || !NILP (Vpurify_flag))
20012 return Qleft_to_right;
20013 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20014 return BVAR (buf, bidi_paragraph_direction);
20015 else
20017 /* Determine the direction from buffer text. We could try to
20018 use current_matrix if it is up to date, but this seems fast
20019 enough as it is. */
20020 struct bidi_it itb;
20021 ptrdiff_t pos = BUF_PT (buf);
20022 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20023 int c;
20024 void *itb_data = bidi_shelve_cache ();
20026 set_buffer_temp (buf);
20027 /* bidi_paragraph_init finds the base direction of the paragraph
20028 by searching forward from paragraph start. We need the base
20029 direction of the current or _previous_ paragraph, so we need
20030 to make sure we are within that paragraph. To that end, find
20031 the previous non-empty line. */
20032 if (pos >= ZV && pos > BEGV)
20033 DEC_BOTH (pos, bytepos);
20034 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20035 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20037 while ((c = FETCH_BYTE (bytepos)) == '\n'
20038 || c == ' ' || c == '\t' || c == '\f')
20040 if (bytepos <= BEGV_BYTE)
20041 break;
20042 bytepos--;
20043 pos--;
20045 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20046 bytepos--;
20048 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20049 itb.paragraph_dir = NEUTRAL_DIR;
20050 itb.string.s = NULL;
20051 itb.string.lstring = Qnil;
20052 itb.string.bufpos = 0;
20053 itb.string.unibyte = 0;
20054 /* We have no window to use here for ignoring window-specific
20055 overlays. Using NULL for window pointer will cause
20056 compute_display_string_pos to use the current buffer. */
20057 itb.w = NULL;
20058 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20059 bidi_unshelve_cache (itb_data, 0);
20060 set_buffer_temp (old);
20061 switch (itb.paragraph_dir)
20063 case L2R:
20064 return Qleft_to_right;
20065 break;
20066 case R2L:
20067 return Qright_to_left;
20068 break;
20069 default:
20070 emacs_abort ();
20075 DEFUN ("move-point-visually", Fmove_point_visually,
20076 Smove_point_visually, 1, 1, 0,
20077 doc: /* Move point in the visual order in the specified DIRECTION.
20078 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20079 left.
20081 Value is the new character position of point. */)
20082 (Lisp_Object direction)
20084 struct window *w = XWINDOW (selected_window);
20085 struct buffer *b = XBUFFER (w->contents);
20086 struct glyph_row *row;
20087 int dir;
20088 Lisp_Object paragraph_dir;
20090 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20091 (!(ROW)->continued_p \
20092 && INTEGERP ((GLYPH)->object) \
20093 && (GLYPH)->type == CHAR_GLYPH \
20094 && (GLYPH)->u.ch == ' ' \
20095 && (GLYPH)->charpos >= 0 \
20096 && !(GLYPH)->avoid_cursor_p)
20098 CHECK_NUMBER (direction);
20099 dir = XINT (direction);
20100 if (dir > 0)
20101 dir = 1;
20102 else
20103 dir = -1;
20105 /* If current matrix is up-to-date, we can use the information
20106 recorded in the glyphs, at least as long as the goal is on the
20107 screen. */
20108 if (w->window_end_valid
20109 && !windows_or_buffers_changed
20110 && b
20111 && !b->clip_changed
20112 && !b->prevent_redisplay_optimizations_p
20113 && !window_outdated (w)
20114 && w->cursor.vpos >= 0
20115 && w->cursor.vpos < w->current_matrix->nrows
20116 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20118 struct glyph *g = row->glyphs[TEXT_AREA];
20119 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20120 struct glyph *gpt = g + w->cursor.hpos;
20122 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20124 if (BUFFERP (g->object) && g->charpos != PT)
20126 SET_PT (g->charpos);
20127 w->cursor.vpos = -1;
20128 return make_number (PT);
20130 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20132 ptrdiff_t new_pos;
20134 if (BUFFERP (gpt->object))
20136 new_pos = PT;
20137 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20138 new_pos += (row->reversed_p ? -dir : dir);
20139 else
20140 new_pos -= (row->reversed_p ? -dir : dir);;
20142 else if (BUFFERP (g->object))
20143 new_pos = g->charpos;
20144 else
20145 break;
20146 SET_PT (new_pos);
20147 w->cursor.vpos = -1;
20148 return make_number (PT);
20150 else if (ROW_GLYPH_NEWLINE_P (row, g))
20152 /* Glyphs inserted at the end of a non-empty line for
20153 positioning the cursor have zero charpos, so we must
20154 deduce the value of point by other means. */
20155 if (g->charpos > 0)
20156 SET_PT (g->charpos);
20157 else if (row->ends_at_zv_p && PT != ZV)
20158 SET_PT (ZV);
20159 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20160 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20161 else
20162 break;
20163 w->cursor.vpos = -1;
20164 return make_number (PT);
20167 if (g == e || INTEGERP (g->object))
20169 if (row->truncated_on_left_p || row->truncated_on_right_p)
20170 goto simulate_display;
20171 if (!row->reversed_p)
20172 row += dir;
20173 else
20174 row -= dir;
20175 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20176 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20177 goto simulate_display;
20179 if (dir > 0)
20181 if (row->reversed_p && !row->continued_p)
20183 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20184 w->cursor.vpos = -1;
20185 return make_number (PT);
20187 g = row->glyphs[TEXT_AREA];
20188 e = g + row->used[TEXT_AREA];
20189 for ( ; g < e; g++)
20191 if (BUFFERP (g->object)
20192 /* Empty lines have only one glyph, which stands
20193 for the newline, and whose charpos is the
20194 buffer position of the newline. */
20195 || ROW_GLYPH_NEWLINE_P (row, g)
20196 /* When the buffer ends in a newline, the line at
20197 EOB also has one glyph, but its charpos is -1. */
20198 || (row->ends_at_zv_p
20199 && !row->reversed_p
20200 && INTEGERP (g->object)
20201 && g->type == CHAR_GLYPH
20202 && g->u.ch == ' '))
20204 if (g->charpos > 0)
20205 SET_PT (g->charpos);
20206 else if (!row->reversed_p
20207 && row->ends_at_zv_p
20208 && PT != ZV)
20209 SET_PT (ZV);
20210 else
20211 continue;
20212 w->cursor.vpos = -1;
20213 return make_number (PT);
20217 else
20219 if (!row->reversed_p && !row->continued_p)
20221 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20222 w->cursor.vpos = -1;
20223 return make_number (PT);
20225 e = row->glyphs[TEXT_AREA];
20226 g = e + row->used[TEXT_AREA] - 1;
20227 for ( ; g >= e; g--)
20229 if (BUFFERP (g->object)
20230 || (ROW_GLYPH_NEWLINE_P (row, g)
20231 && g->charpos > 0)
20232 /* Empty R2L lines on GUI frames have the buffer
20233 position of the newline stored in the stretch
20234 glyph. */
20235 || g->type == STRETCH_GLYPH
20236 || (row->ends_at_zv_p
20237 && row->reversed_p
20238 && INTEGERP (g->object)
20239 && g->type == CHAR_GLYPH
20240 && g->u.ch == ' '))
20242 if (g->charpos > 0)
20243 SET_PT (g->charpos);
20244 else if (row->reversed_p
20245 && row->ends_at_zv_p
20246 && PT != ZV)
20247 SET_PT (ZV);
20248 else
20249 continue;
20250 w->cursor.vpos = -1;
20251 return make_number (PT);
20258 simulate_display:
20260 /* If we wind up here, we failed to move by using the glyphs, so we
20261 need to simulate display instead. */
20263 if (b)
20264 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20265 else
20266 paragraph_dir = Qleft_to_right;
20267 if (EQ (paragraph_dir, Qright_to_left))
20268 dir = -dir;
20269 if (PT <= BEGV && dir < 0)
20270 xsignal0 (Qbeginning_of_buffer);
20271 else if (PT >= ZV && dir > 0)
20272 xsignal0 (Qend_of_buffer);
20273 else
20275 struct text_pos pt;
20276 struct it it;
20277 int pt_x, target_x, pixel_width, pt_vpos;
20278 bool at_eol_p;
20279 bool overshoot_expected = false;
20280 bool target_is_eol_p = false;
20282 /* Setup the arena. */
20283 SET_TEXT_POS (pt, PT, PT_BYTE);
20284 start_display (&it, w, pt);
20286 if (it.cmp_it.id < 0
20287 && it.method == GET_FROM_STRING
20288 && it.area == TEXT_AREA
20289 && it.string_from_display_prop_p
20290 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20291 overshoot_expected = true;
20293 /* Find the X coordinate of point. We start from the beginning
20294 of this or previous line to make sure we are before point in
20295 the logical order (since the move_it_* functions can only
20296 move forward). */
20297 reseat_at_previous_visible_line_start (&it);
20298 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20299 if (IT_CHARPOS (it) != PT)
20300 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20301 -1, -1, -1, MOVE_TO_POS);
20302 pt_x = it.current_x;
20303 pt_vpos = it.vpos;
20304 if (dir > 0 || overshoot_expected)
20306 struct glyph_row *row = it.glyph_row;
20308 /* When point is at beginning of line, we don't have
20309 information about the glyph there loaded into struct
20310 it. Calling get_next_display_element fixes that. */
20311 if (pt_x == 0)
20312 get_next_display_element (&it);
20313 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20314 it.glyph_row = NULL;
20315 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20316 it.glyph_row = row;
20317 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20318 it, lest it will become out of sync with it's buffer
20319 position. */
20320 it.current_x = pt_x;
20322 else
20323 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20324 pixel_width = it.pixel_width;
20325 if (overshoot_expected && at_eol_p)
20326 pixel_width = 0;
20327 else if (pixel_width <= 0)
20328 pixel_width = 1;
20330 /* If there's a display string at point, we are actually at the
20331 glyph to the left of point, so we need to correct the X
20332 coordinate. */
20333 if (overshoot_expected)
20334 pt_x += pixel_width;
20336 /* Compute target X coordinate, either to the left or to the
20337 right of point. On TTY frames, all characters have the same
20338 pixel width of 1, so we can use that. On GUI frames we don't
20339 have an easy way of getting at the pixel width of the
20340 character to the left of point, so we use a different method
20341 of getting to that place. */
20342 if (dir > 0)
20343 target_x = pt_x + pixel_width;
20344 else
20345 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20347 /* Target X coordinate could be one line above or below the line
20348 of point, in which case we need to adjust the target X
20349 coordinate. Also, if moving to the left, we need to begin at
20350 the left edge of the point's screen line. */
20351 if (dir < 0)
20353 if (pt_x > 0)
20355 start_display (&it, w, pt);
20356 reseat_at_previous_visible_line_start (&it);
20357 it.current_x = it.current_y = it.hpos = 0;
20358 if (pt_vpos != 0)
20359 move_it_by_lines (&it, pt_vpos);
20361 else
20363 move_it_by_lines (&it, -1);
20364 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20365 target_is_eol_p = true;
20368 else
20370 if (at_eol_p
20371 || (target_x >= it.last_visible_x
20372 && it.line_wrap != TRUNCATE))
20374 if (pt_x > 0)
20375 move_it_by_lines (&it, 0);
20376 move_it_by_lines (&it, 1);
20377 target_x = 0;
20381 /* Move to the target X coordinate. */
20382 #ifdef HAVE_WINDOW_SYSTEM
20383 /* On GUI frames, as we don't know the X coordinate of the
20384 character to the left of point, moving point to the left
20385 requires walking, one grapheme cluster at a time, until we
20386 find ourself at a place immediately to the left of the
20387 character at point. */
20388 if (FRAME_WINDOW_P (it.f) && dir < 0)
20390 struct text_pos new_pos = it.current.pos;
20391 enum move_it_result rc = MOVE_X_REACHED;
20393 while (it.current_x + it.pixel_width <= target_x
20394 && rc == MOVE_X_REACHED)
20396 int new_x = it.current_x + it.pixel_width;
20398 new_pos = it.current.pos;
20399 if (new_x == it.current_x)
20400 new_x++;
20401 rc = move_it_in_display_line_to (&it, ZV, new_x,
20402 MOVE_TO_POS | MOVE_TO_X);
20403 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20404 break;
20406 /* If we ended up on a composed character inside
20407 bidi-reordered text (e.g., Hebrew text with diacritics),
20408 the iterator gives us the buffer position of the last (in
20409 logical order) character of the composed grapheme cluster,
20410 which is not what we want. So we cheat: we compute the
20411 character position of the character that follows (in the
20412 logical order) the one where the above loop stopped. That
20413 character will appear on display to the left of point. */
20414 if (it.bidi_p
20415 && it.bidi_it.scan_dir == -1
20416 && new_pos.charpos - IT_CHARPOS (it) > 1)
20418 new_pos.charpos = IT_CHARPOS (it) + 1;
20419 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20421 it.current.pos = new_pos;
20423 else
20424 #endif
20425 if (it.current_x != target_x)
20426 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20428 /* When lines are truncated, the above loop will stop at the
20429 window edge. But we want to get to the end of line, even if
20430 it is beyond the window edge; automatic hscroll will then
20431 scroll the window to show point as appropriate. */
20432 if (target_is_eol_p && it.line_wrap == TRUNCATE
20433 && get_next_display_element (&it))
20435 struct text_pos new_pos = it.current.pos;
20437 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20439 set_iterator_to_next (&it, 0);
20440 if (it.method == GET_FROM_BUFFER)
20441 new_pos = it.current.pos;
20442 if (!get_next_display_element (&it))
20443 break;
20446 it.current.pos = new_pos;
20449 /* If we ended up in a display string that covers point, move to
20450 buffer position to the right in the visual order. */
20451 if (dir > 0)
20453 while (IT_CHARPOS (it) == PT)
20455 set_iterator_to_next (&it, 0);
20456 if (!get_next_display_element (&it))
20457 break;
20461 /* Move point to that position. */
20462 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20465 return make_number (PT);
20467 #undef ROW_GLYPH_NEWLINE_P
20471 /***********************************************************************
20472 Menu Bar
20473 ***********************************************************************/
20475 /* Redisplay the menu bar in the frame for window W.
20477 The menu bar of X frames that don't have X toolkit support is
20478 displayed in a special window W->frame->menu_bar_window.
20480 The menu bar of terminal frames is treated specially as far as
20481 glyph matrices are concerned. Menu bar lines are not part of
20482 windows, so the update is done directly on the frame matrix rows
20483 for the menu bar. */
20485 static void
20486 display_menu_bar (struct window *w)
20488 struct frame *f = XFRAME (WINDOW_FRAME (w));
20489 struct it it;
20490 Lisp_Object items;
20491 int i;
20493 /* Don't do all this for graphical frames. */
20494 #ifdef HAVE_NTGUI
20495 if (FRAME_W32_P (f))
20496 return;
20497 #endif
20498 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20499 if (FRAME_X_P (f))
20500 return;
20501 #endif
20503 #ifdef HAVE_NS
20504 if (FRAME_NS_P (f))
20505 return;
20506 #endif /* HAVE_NS */
20508 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20509 eassert (!FRAME_WINDOW_P (f));
20510 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20511 it.first_visible_x = 0;
20512 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20513 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20514 if (FRAME_WINDOW_P (f))
20516 /* Menu bar lines are displayed in the desired matrix of the
20517 dummy window menu_bar_window. */
20518 struct window *menu_w;
20519 menu_w = XWINDOW (f->menu_bar_window);
20520 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20521 MENU_FACE_ID);
20522 it.first_visible_x = 0;
20523 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20525 else
20526 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20528 /* This is a TTY frame, i.e. character hpos/vpos are used as
20529 pixel x/y. */
20530 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20531 MENU_FACE_ID);
20532 it.first_visible_x = 0;
20533 it.last_visible_x = FRAME_COLS (f);
20536 /* FIXME: This should be controlled by a user option. See the
20537 comments in redisplay_tool_bar and display_mode_line about
20538 this. */
20539 it.paragraph_embedding = L2R;
20541 /* Clear all rows of the menu bar. */
20542 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20544 struct glyph_row *row = it.glyph_row + i;
20545 clear_glyph_row (row);
20546 row->enabled_p = 1;
20547 row->full_width_p = 1;
20550 /* Display all items of the menu bar. */
20551 items = FRAME_MENU_BAR_ITEMS (it.f);
20552 for (i = 0; i < ASIZE (items); i += 4)
20554 Lisp_Object string;
20556 /* Stop at nil string. */
20557 string = AREF (items, i + 1);
20558 if (NILP (string))
20559 break;
20561 /* Remember where item was displayed. */
20562 ASET (items, i + 3, make_number (it.hpos));
20564 /* Display the item, pad with one space. */
20565 if (it.current_x < it.last_visible_x)
20566 display_string (NULL, string, Qnil, 0, 0, &it,
20567 SCHARS (string) + 1, 0, 0, -1);
20570 /* Fill out the line with spaces. */
20571 if (it.current_x < it.last_visible_x)
20572 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20574 /* Compute the total height of the lines. */
20575 compute_line_metrics (&it);
20578 #ifdef HAVE_MENUS
20579 /* Deep copy of a glyph row, including the glyphs. */
20580 static void
20581 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20583 int area, i, sum_used = 0;
20584 struct glyph *pointers[1 + LAST_AREA];
20586 /* Save glyph pointers of TO. */
20587 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20589 /* Do a structure assignment. */
20590 *to = *from;
20592 /* Restore original pointers of TO. */
20593 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20595 /* Count how many glyphs to copy and update glyph pointers. */
20596 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
20598 if (area > LEFT_MARGIN_AREA)
20600 eassert (from->glyphs[area] - from->glyphs[area - 1]
20601 == from->used[area - 1]);
20602 to->glyphs[area] = to->glyphs[area - 1] + to->used[area - 1];
20604 sum_used += from->used[area];
20607 /* Copy the glyphs. */
20608 eassert (sum_used <= to->glyphs[LAST_AREA] - to->glyphs[LEFT_MARGIN_AREA]);
20609 for (i = 0; i < sum_used; i++)
20610 to->glyphs[LEFT_MARGIN_AREA][i] = from->glyphs[LEFT_MARGIN_AREA][i];
20613 /* Display one menu item on a TTY, by overwriting the glyphs in the
20614 desired glyph matrix with glyphs produced from the menu item text.
20615 Called from term.c to display TTY drop-down menus one item at a
20616 time.
20618 ITEM_TEXT is the menu item text as a C string.
20620 FACE_ID is the face ID to be used for this menu item. FACE_ID
20621 could specify one of 3 faces: a face for an enabled item, a face
20622 for a disabled item, or a face for a selected item.
20624 X and Y are coordinates of the first glyph in the desired matrix to
20625 be overwritten by the menu item. Since this is a TTY, Y is the
20626 zero-based number of the glyph row and X is the zero-based glyph
20627 number in the row, starting from left, where to start displaying
20628 the item.
20630 SUBMENU non-zero means this menu item drops down a submenu, which
20631 should be indicated by displaying a proper visual cue after the
20632 item text. */
20634 void
20635 display_tty_menu_item (const char *item_text, int width, int face_id,
20636 int x, int y, int submenu)
20638 struct it it;
20639 struct frame *f = SELECTED_FRAME ();
20640 struct window *w = XWINDOW (f->selected_window);
20641 int saved_used, saved_truncated, saved_width, saved_reversed;
20642 struct glyph_row *row;
20643 size_t item_len = strlen (item_text);
20645 eassert (FRAME_TERMCAP_P (f));
20647 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
20648 it.first_visible_x = 0;
20649 it.last_visible_x = FRAME_COLS (f) - 1;
20650 row = it.glyph_row;
20651 /* Start with the row contents from the current matrix. */
20652 deep_copy_glyph_row (row, f->current_matrix->rows + y);
20653 saved_width = row->full_width_p;
20654 row->full_width_p = 1;
20655 saved_reversed = row->reversed_p;
20656 row->reversed_p = 0;
20657 row->enabled_p = 1;
20659 /* Arrange for the menu item glyphs to start at (X,Y) and have the
20660 desired face. */
20661 it.current_x = it.hpos = x;
20662 it.current_y = it.vpos = y;
20663 saved_used = row->used[TEXT_AREA];
20664 saved_truncated = row->truncated_on_right_p;
20665 row->used[TEXT_AREA] = x;
20666 it.face_id = face_id;
20667 it.line_wrap = TRUNCATE;
20669 /* FIXME: This should be controlled by a user option. See the
20670 comments in redisplay_tool_bar and display_mode_line about this.
20671 Also, if paragraph_embedding could ever be R2L, changes will be
20672 needed to avoid shifting to the right the row characters in
20673 term.c:append_glyph. */
20674 it.paragraph_embedding = L2R;
20676 /* Pad with a space on the left. */
20677 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
20678 width--;
20679 /* Display the menu item, pad with spaces to WIDTH. */
20680 if (submenu)
20682 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20683 item_len, 0, FRAME_COLS (f) - 1, -1);
20684 width -= item_len;
20685 /* Indicate with " >" that there's a submenu. */
20686 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
20687 FRAME_COLS (f) - 1, -1);
20689 else
20690 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20691 width, 0, FRAME_COLS (f) - 1, -1);
20693 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
20694 row->truncated_on_right_p = saved_truncated;
20695 row->hash = row_hash (row);
20696 row->full_width_p = saved_width;
20697 row->reversed_p = saved_reversed;
20699 #endif /* HAVE_MENUS */
20701 /***********************************************************************
20702 Mode Line
20703 ***********************************************************************/
20705 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20706 FORCE is non-zero, redisplay mode lines unconditionally.
20707 Otherwise, redisplay only mode lines that are garbaged. Value is
20708 the number of windows whose mode lines were redisplayed. */
20710 static int
20711 redisplay_mode_lines (Lisp_Object window, int force)
20713 int nwindows = 0;
20715 while (!NILP (window))
20717 struct window *w = XWINDOW (window);
20719 if (WINDOWP (w->contents))
20720 nwindows += redisplay_mode_lines (w->contents, force);
20721 else if (force
20722 || FRAME_GARBAGED_P (XFRAME (w->frame))
20723 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20725 struct text_pos lpoint;
20726 struct buffer *old = current_buffer;
20728 /* Set the window's buffer for the mode line display. */
20729 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20730 set_buffer_internal_1 (XBUFFER (w->contents));
20732 /* Point refers normally to the selected window. For any
20733 other window, set up appropriate value. */
20734 if (!EQ (window, selected_window))
20736 struct text_pos pt;
20738 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20739 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20742 /* Display mode lines. */
20743 clear_glyph_matrix (w->desired_matrix);
20744 if (display_mode_lines (w))
20746 ++nwindows;
20747 w->must_be_updated_p = 1;
20750 /* Restore old settings. */
20751 set_buffer_internal_1 (old);
20752 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20755 window = w->next;
20758 return nwindows;
20762 /* Display the mode and/or header line of window W. Value is the
20763 sum number of mode lines and header lines displayed. */
20765 static int
20766 display_mode_lines (struct window *w)
20768 Lisp_Object old_selected_window = selected_window;
20769 Lisp_Object old_selected_frame = selected_frame;
20770 Lisp_Object new_frame = w->frame;
20771 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20772 int n = 0;
20774 selected_frame = new_frame;
20775 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20776 or window's point, then we'd need select_window_1 here as well. */
20777 XSETWINDOW (selected_window, w);
20778 XFRAME (new_frame)->selected_window = selected_window;
20780 /* These will be set while the mode line specs are processed. */
20781 line_number_displayed = 0;
20782 w->column_number_displayed = -1;
20784 if (WINDOW_WANTS_MODELINE_P (w))
20786 struct window *sel_w = XWINDOW (old_selected_window);
20788 /* Select mode line face based on the real selected window. */
20789 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20790 BVAR (current_buffer, mode_line_format));
20791 ++n;
20794 if (WINDOW_WANTS_HEADER_LINE_P (w))
20796 display_mode_line (w, HEADER_LINE_FACE_ID,
20797 BVAR (current_buffer, header_line_format));
20798 ++n;
20801 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20802 selected_frame = old_selected_frame;
20803 selected_window = old_selected_window;
20804 return n;
20808 /* Display mode or header line of window W. FACE_ID specifies which
20809 line to display; it is either MODE_LINE_FACE_ID or
20810 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20811 display. Value is the pixel height of the mode/header line
20812 displayed. */
20814 static int
20815 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20817 struct it it;
20818 struct face *face;
20819 ptrdiff_t count = SPECPDL_INDEX ();
20821 init_iterator (&it, w, -1, -1, NULL, face_id);
20822 /* Don't extend on a previously drawn mode-line.
20823 This may happen if called from pos_visible_p. */
20824 it.glyph_row->enabled_p = 0;
20825 prepare_desired_row (it.glyph_row);
20827 it.glyph_row->mode_line_p = 1;
20829 /* FIXME: This should be controlled by a user option. But
20830 supporting such an option is not trivial, since the mode line is
20831 made up of many separate strings. */
20832 it.paragraph_embedding = L2R;
20834 record_unwind_protect (unwind_format_mode_line,
20835 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20837 mode_line_target = MODE_LINE_DISPLAY;
20839 /* Temporarily make frame's keyboard the current kboard so that
20840 kboard-local variables in the mode_line_format will get the right
20841 values. */
20842 push_kboard (FRAME_KBOARD (it.f));
20843 record_unwind_save_match_data ();
20844 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20845 pop_kboard ();
20847 unbind_to (count, Qnil);
20849 /* Fill up with spaces. */
20850 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20852 compute_line_metrics (&it);
20853 it.glyph_row->full_width_p = 1;
20854 it.glyph_row->continued_p = 0;
20855 it.glyph_row->truncated_on_left_p = 0;
20856 it.glyph_row->truncated_on_right_p = 0;
20858 /* Make a 3D mode-line have a shadow at its right end. */
20859 face = FACE_FROM_ID (it.f, face_id);
20860 extend_face_to_end_of_line (&it);
20861 if (face->box != FACE_NO_BOX)
20863 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20864 + it.glyph_row->used[TEXT_AREA] - 1);
20865 last->right_box_line_p = 1;
20868 return it.glyph_row->height;
20871 /* Move element ELT in LIST to the front of LIST.
20872 Return the updated list. */
20874 static Lisp_Object
20875 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20877 register Lisp_Object tail, prev;
20878 register Lisp_Object tem;
20880 tail = list;
20881 prev = Qnil;
20882 while (CONSP (tail))
20884 tem = XCAR (tail);
20886 if (EQ (elt, tem))
20888 /* Splice out the link TAIL. */
20889 if (NILP (prev))
20890 list = XCDR (tail);
20891 else
20892 Fsetcdr (prev, XCDR (tail));
20894 /* Now make it the first. */
20895 Fsetcdr (tail, list);
20896 return tail;
20898 else
20899 prev = tail;
20900 tail = XCDR (tail);
20901 QUIT;
20904 /* Not found--return unchanged LIST. */
20905 return list;
20908 /* Contribute ELT to the mode line for window IT->w. How it
20909 translates into text depends on its data type.
20911 IT describes the display environment in which we display, as usual.
20913 DEPTH is the depth in recursion. It is used to prevent
20914 infinite recursion here.
20916 FIELD_WIDTH is the number of characters the display of ELT should
20917 occupy in the mode line, and PRECISION is the maximum number of
20918 characters to display from ELT's representation. See
20919 display_string for details.
20921 Returns the hpos of the end of the text generated by ELT.
20923 PROPS is a property list to add to any string we encounter.
20925 If RISKY is nonzero, remove (disregard) any properties in any string
20926 we encounter, and ignore :eval and :propertize.
20928 The global variable `mode_line_target' determines whether the
20929 output is passed to `store_mode_line_noprop',
20930 `store_mode_line_string', or `display_string'. */
20932 static int
20933 display_mode_element (struct it *it, int depth, int field_width, int precision,
20934 Lisp_Object elt, Lisp_Object props, int risky)
20936 int n = 0, field, prec;
20937 int literal = 0;
20939 tail_recurse:
20940 if (depth > 100)
20941 elt = build_string ("*too-deep*");
20943 depth++;
20945 switch (XTYPE (elt))
20947 case Lisp_String:
20949 /* A string: output it and check for %-constructs within it. */
20950 unsigned char c;
20951 ptrdiff_t offset = 0;
20953 if (SCHARS (elt) > 0
20954 && (!NILP (props) || risky))
20956 Lisp_Object oprops, aelt;
20957 oprops = Ftext_properties_at (make_number (0), elt);
20959 /* If the starting string's properties are not what
20960 we want, translate the string. Also, if the string
20961 is risky, do that anyway. */
20963 if (NILP (Fequal (props, oprops)) || risky)
20965 /* If the starting string has properties,
20966 merge the specified ones onto the existing ones. */
20967 if (! NILP (oprops) && !risky)
20969 Lisp_Object tem;
20971 oprops = Fcopy_sequence (oprops);
20972 tem = props;
20973 while (CONSP (tem))
20975 oprops = Fplist_put (oprops, XCAR (tem),
20976 XCAR (XCDR (tem)));
20977 tem = XCDR (XCDR (tem));
20979 props = oprops;
20982 aelt = Fassoc (elt, mode_line_proptrans_alist);
20983 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20985 /* AELT is what we want. Move it to the front
20986 without consing. */
20987 elt = XCAR (aelt);
20988 mode_line_proptrans_alist
20989 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20991 else
20993 Lisp_Object tem;
20995 /* If AELT has the wrong props, it is useless.
20996 so get rid of it. */
20997 if (! NILP (aelt))
20998 mode_line_proptrans_alist
20999 = Fdelq (aelt, mode_line_proptrans_alist);
21001 elt = Fcopy_sequence (elt);
21002 Fset_text_properties (make_number (0), Flength (elt),
21003 props, elt);
21004 /* Add this item to mode_line_proptrans_alist. */
21005 mode_line_proptrans_alist
21006 = Fcons (Fcons (elt, props),
21007 mode_line_proptrans_alist);
21008 /* Truncate mode_line_proptrans_alist
21009 to at most 50 elements. */
21010 tem = Fnthcdr (make_number (50),
21011 mode_line_proptrans_alist);
21012 if (! NILP (tem))
21013 XSETCDR (tem, Qnil);
21018 offset = 0;
21020 if (literal)
21022 prec = precision - n;
21023 switch (mode_line_target)
21025 case MODE_LINE_NOPROP:
21026 case MODE_LINE_TITLE:
21027 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21028 break;
21029 case MODE_LINE_STRING:
21030 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21031 break;
21032 case MODE_LINE_DISPLAY:
21033 n += display_string (NULL, elt, Qnil, 0, 0, it,
21034 0, prec, 0, STRING_MULTIBYTE (elt));
21035 break;
21038 break;
21041 /* Handle the non-literal case. */
21043 while ((precision <= 0 || n < precision)
21044 && SREF (elt, offset) != 0
21045 && (mode_line_target != MODE_LINE_DISPLAY
21046 || it->current_x < it->last_visible_x))
21048 ptrdiff_t last_offset = offset;
21050 /* Advance to end of string or next format specifier. */
21051 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21054 if (offset - 1 != last_offset)
21056 ptrdiff_t nchars, nbytes;
21058 /* Output to end of string or up to '%'. Field width
21059 is length of string. Don't output more than
21060 PRECISION allows us. */
21061 offset--;
21063 prec = c_string_width (SDATA (elt) + last_offset,
21064 offset - last_offset, precision - n,
21065 &nchars, &nbytes);
21067 switch (mode_line_target)
21069 case MODE_LINE_NOPROP:
21070 case MODE_LINE_TITLE:
21071 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21072 break;
21073 case MODE_LINE_STRING:
21075 ptrdiff_t bytepos = last_offset;
21076 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21077 ptrdiff_t endpos = (precision <= 0
21078 ? string_byte_to_char (elt, offset)
21079 : charpos + nchars);
21081 n += store_mode_line_string (NULL,
21082 Fsubstring (elt, make_number (charpos),
21083 make_number (endpos)),
21084 0, 0, 0, Qnil);
21086 break;
21087 case MODE_LINE_DISPLAY:
21089 ptrdiff_t bytepos = last_offset;
21090 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21092 if (precision <= 0)
21093 nchars = string_byte_to_char (elt, offset) - charpos;
21094 n += display_string (NULL, elt, Qnil, 0, charpos,
21095 it, 0, nchars, 0,
21096 STRING_MULTIBYTE (elt));
21098 break;
21101 else /* c == '%' */
21103 ptrdiff_t percent_position = offset;
21105 /* Get the specified minimum width. Zero means
21106 don't pad. */
21107 field = 0;
21108 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21109 field = field * 10 + c - '0';
21111 /* Don't pad beyond the total padding allowed. */
21112 if (field_width - n > 0 && field > field_width - n)
21113 field = field_width - n;
21115 /* Note that either PRECISION <= 0 or N < PRECISION. */
21116 prec = precision - n;
21118 if (c == 'M')
21119 n += display_mode_element (it, depth, field, prec,
21120 Vglobal_mode_string, props,
21121 risky);
21122 else if (c != 0)
21124 bool multibyte;
21125 ptrdiff_t bytepos, charpos;
21126 const char *spec;
21127 Lisp_Object string;
21129 bytepos = percent_position;
21130 charpos = (STRING_MULTIBYTE (elt)
21131 ? string_byte_to_char (elt, bytepos)
21132 : bytepos);
21133 spec = decode_mode_spec (it->w, c, field, &string);
21134 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21136 switch (mode_line_target)
21138 case MODE_LINE_NOPROP:
21139 case MODE_LINE_TITLE:
21140 n += store_mode_line_noprop (spec, field, prec);
21141 break;
21142 case MODE_LINE_STRING:
21144 Lisp_Object tem = build_string (spec);
21145 props = Ftext_properties_at (make_number (charpos), elt);
21146 /* Should only keep face property in props */
21147 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21149 break;
21150 case MODE_LINE_DISPLAY:
21152 int nglyphs_before, nwritten;
21154 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21155 nwritten = display_string (spec, string, elt,
21156 charpos, 0, it,
21157 field, prec, 0,
21158 multibyte);
21160 /* Assign to the glyphs written above the
21161 string where the `%x' came from, position
21162 of the `%'. */
21163 if (nwritten > 0)
21165 struct glyph *glyph
21166 = (it->glyph_row->glyphs[TEXT_AREA]
21167 + nglyphs_before);
21168 int i;
21170 for (i = 0; i < nwritten; ++i)
21172 glyph[i].object = elt;
21173 glyph[i].charpos = charpos;
21176 n += nwritten;
21179 break;
21182 else /* c == 0 */
21183 break;
21187 break;
21189 case Lisp_Symbol:
21190 /* A symbol: process the value of the symbol recursively
21191 as if it appeared here directly. Avoid error if symbol void.
21192 Special case: if value of symbol is a string, output the string
21193 literally. */
21195 register Lisp_Object tem;
21197 /* If the variable is not marked as risky to set
21198 then its contents are risky to use. */
21199 if (NILP (Fget (elt, Qrisky_local_variable)))
21200 risky = 1;
21202 tem = Fboundp (elt);
21203 if (!NILP (tem))
21205 tem = Fsymbol_value (elt);
21206 /* If value is a string, output that string literally:
21207 don't check for % within it. */
21208 if (STRINGP (tem))
21209 literal = 1;
21211 if (!EQ (tem, elt))
21213 /* Give up right away for nil or t. */
21214 elt = tem;
21215 goto tail_recurse;
21219 break;
21221 case Lisp_Cons:
21223 register Lisp_Object car, tem;
21225 /* A cons cell: five distinct cases.
21226 If first element is :eval or :propertize, do something special.
21227 If first element is a string or a cons, process all the elements
21228 and effectively concatenate them.
21229 If first element is a negative number, truncate displaying cdr to
21230 at most that many characters. If positive, pad (with spaces)
21231 to at least that many characters.
21232 If first element is a symbol, process the cadr or caddr recursively
21233 according to whether the symbol's value is non-nil or nil. */
21234 car = XCAR (elt);
21235 if (EQ (car, QCeval))
21237 /* An element of the form (:eval FORM) means evaluate FORM
21238 and use the result as mode line elements. */
21240 if (risky)
21241 break;
21243 if (CONSP (XCDR (elt)))
21245 Lisp_Object spec;
21246 spec = safe_eval (XCAR (XCDR (elt)));
21247 n += display_mode_element (it, depth, field_width - n,
21248 precision - n, spec, props,
21249 risky);
21252 else if (EQ (car, QCpropertize))
21254 /* An element of the form (:propertize ELT PROPS...)
21255 means display ELT but applying properties PROPS. */
21257 if (risky)
21258 break;
21260 if (CONSP (XCDR (elt)))
21261 n += display_mode_element (it, depth, field_width - n,
21262 precision - n, XCAR (XCDR (elt)),
21263 XCDR (XCDR (elt)), risky);
21265 else if (SYMBOLP (car))
21267 tem = Fboundp (car);
21268 elt = XCDR (elt);
21269 if (!CONSP (elt))
21270 goto invalid;
21271 /* elt is now the cdr, and we know it is a cons cell.
21272 Use its car if CAR has a non-nil value. */
21273 if (!NILP (tem))
21275 tem = Fsymbol_value (car);
21276 if (!NILP (tem))
21278 elt = XCAR (elt);
21279 goto tail_recurse;
21282 /* Symbol's value is nil (or symbol is unbound)
21283 Get the cddr of the original list
21284 and if possible find the caddr and use that. */
21285 elt = XCDR (elt);
21286 if (NILP (elt))
21287 break;
21288 else if (!CONSP (elt))
21289 goto invalid;
21290 elt = XCAR (elt);
21291 goto tail_recurse;
21293 else if (INTEGERP (car))
21295 register int lim = XINT (car);
21296 elt = XCDR (elt);
21297 if (lim < 0)
21299 /* Negative int means reduce maximum width. */
21300 if (precision <= 0)
21301 precision = -lim;
21302 else
21303 precision = min (precision, -lim);
21305 else if (lim > 0)
21307 /* Padding specified. Don't let it be more than
21308 current maximum. */
21309 if (precision > 0)
21310 lim = min (precision, lim);
21312 /* If that's more padding than already wanted, queue it.
21313 But don't reduce padding already specified even if
21314 that is beyond the current truncation point. */
21315 field_width = max (lim, field_width);
21317 goto tail_recurse;
21319 else if (STRINGP (car) || CONSP (car))
21321 Lisp_Object halftail = elt;
21322 int len = 0;
21324 while (CONSP (elt)
21325 && (precision <= 0 || n < precision))
21327 n += display_mode_element (it, depth,
21328 /* Do padding only after the last
21329 element in the list. */
21330 (! CONSP (XCDR (elt))
21331 ? field_width - n
21332 : 0),
21333 precision - n, XCAR (elt),
21334 props, risky);
21335 elt = XCDR (elt);
21336 len++;
21337 if ((len & 1) == 0)
21338 halftail = XCDR (halftail);
21339 /* Check for cycle. */
21340 if (EQ (halftail, elt))
21341 break;
21345 break;
21347 default:
21348 invalid:
21349 elt = build_string ("*invalid*");
21350 goto tail_recurse;
21353 /* Pad to FIELD_WIDTH. */
21354 if (field_width > 0 && n < field_width)
21356 switch (mode_line_target)
21358 case MODE_LINE_NOPROP:
21359 case MODE_LINE_TITLE:
21360 n += store_mode_line_noprop ("", field_width - n, 0);
21361 break;
21362 case MODE_LINE_STRING:
21363 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21364 break;
21365 case MODE_LINE_DISPLAY:
21366 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21367 0, 0, 0);
21368 break;
21372 return n;
21375 /* Store a mode-line string element in mode_line_string_list.
21377 If STRING is non-null, display that C string. Otherwise, the Lisp
21378 string LISP_STRING is displayed.
21380 FIELD_WIDTH is the minimum number of output glyphs to produce.
21381 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21382 with spaces. FIELD_WIDTH <= 0 means don't pad.
21384 PRECISION is the maximum number of characters to output from
21385 STRING. PRECISION <= 0 means don't truncate the string.
21387 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21388 properties to the string.
21390 PROPS are the properties to add to the string.
21391 The mode_line_string_face face property is always added to the string.
21394 static int
21395 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21396 int field_width, int precision, Lisp_Object props)
21398 ptrdiff_t len;
21399 int n = 0;
21401 if (string != NULL)
21403 len = strlen (string);
21404 if (precision > 0 && len > precision)
21405 len = precision;
21406 lisp_string = make_string (string, len);
21407 if (NILP (props))
21408 props = mode_line_string_face_prop;
21409 else if (!NILP (mode_line_string_face))
21411 Lisp_Object face = Fplist_get (props, Qface);
21412 props = Fcopy_sequence (props);
21413 if (NILP (face))
21414 face = mode_line_string_face;
21415 else
21416 face = list2 (face, mode_line_string_face);
21417 props = Fplist_put (props, Qface, face);
21419 Fadd_text_properties (make_number (0), make_number (len),
21420 props, lisp_string);
21422 else
21424 len = XFASTINT (Flength (lisp_string));
21425 if (precision > 0 && len > precision)
21427 len = precision;
21428 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21429 precision = -1;
21431 if (!NILP (mode_line_string_face))
21433 Lisp_Object face;
21434 if (NILP (props))
21435 props = Ftext_properties_at (make_number (0), lisp_string);
21436 face = Fplist_get (props, Qface);
21437 if (NILP (face))
21438 face = mode_line_string_face;
21439 else
21440 face = list2 (face, mode_line_string_face);
21441 props = list2 (Qface, face);
21442 if (copy_string)
21443 lisp_string = Fcopy_sequence (lisp_string);
21445 if (!NILP (props))
21446 Fadd_text_properties (make_number (0), make_number (len),
21447 props, lisp_string);
21450 if (len > 0)
21452 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21453 n += len;
21456 if (field_width > len)
21458 field_width -= len;
21459 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21460 if (!NILP (props))
21461 Fadd_text_properties (make_number (0), make_number (field_width),
21462 props, lisp_string);
21463 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21464 n += field_width;
21467 return n;
21471 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21472 1, 4, 0,
21473 doc: /* Format a string out of a mode line format specification.
21474 First arg FORMAT specifies the mode line format (see `mode-line-format'
21475 for details) to use.
21477 By default, the format is evaluated for the currently selected window.
21479 Optional second arg FACE specifies the face property to put on all
21480 characters for which no face is specified. The value nil means the
21481 default face. The value t means whatever face the window's mode line
21482 currently uses (either `mode-line' or `mode-line-inactive',
21483 depending on whether the window is the selected window or not).
21484 An integer value means the value string has no text
21485 properties.
21487 Optional third and fourth args WINDOW and BUFFER specify the window
21488 and buffer to use as the context for the formatting (defaults
21489 are the selected window and the WINDOW's buffer). */)
21490 (Lisp_Object format, Lisp_Object face,
21491 Lisp_Object window, Lisp_Object buffer)
21493 struct it it;
21494 int len;
21495 struct window *w;
21496 struct buffer *old_buffer = NULL;
21497 int face_id;
21498 int no_props = INTEGERP (face);
21499 ptrdiff_t count = SPECPDL_INDEX ();
21500 Lisp_Object str;
21501 int string_start = 0;
21503 w = decode_any_window (window);
21504 XSETWINDOW (window, w);
21506 if (NILP (buffer))
21507 buffer = w->contents;
21508 CHECK_BUFFER (buffer);
21510 /* Make formatting the modeline a non-op when noninteractive, otherwise
21511 there will be problems later caused by a partially initialized frame. */
21512 if (NILP (format) || noninteractive)
21513 return empty_unibyte_string;
21515 if (no_props)
21516 face = Qnil;
21518 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21519 : EQ (face, Qt) ? (EQ (window, selected_window)
21520 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21521 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21522 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21523 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21524 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21525 : DEFAULT_FACE_ID;
21527 old_buffer = current_buffer;
21529 /* Save things including mode_line_proptrans_alist,
21530 and set that to nil so that we don't alter the outer value. */
21531 record_unwind_protect (unwind_format_mode_line,
21532 format_mode_line_unwind_data
21533 (XFRAME (WINDOW_FRAME (w)),
21534 old_buffer, selected_window, 1));
21535 mode_line_proptrans_alist = Qnil;
21537 Fselect_window (window, Qt);
21538 set_buffer_internal_1 (XBUFFER (buffer));
21540 init_iterator (&it, w, -1, -1, NULL, face_id);
21542 if (no_props)
21544 mode_line_target = MODE_LINE_NOPROP;
21545 mode_line_string_face_prop = Qnil;
21546 mode_line_string_list = Qnil;
21547 string_start = MODE_LINE_NOPROP_LEN (0);
21549 else
21551 mode_line_target = MODE_LINE_STRING;
21552 mode_line_string_list = Qnil;
21553 mode_line_string_face = face;
21554 mode_line_string_face_prop
21555 = NILP (face) ? Qnil : list2 (Qface, face);
21558 push_kboard (FRAME_KBOARD (it.f));
21559 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21560 pop_kboard ();
21562 if (no_props)
21564 len = MODE_LINE_NOPROP_LEN (string_start);
21565 str = make_string (mode_line_noprop_buf + string_start, len);
21567 else
21569 mode_line_string_list = Fnreverse (mode_line_string_list);
21570 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21571 empty_unibyte_string);
21574 unbind_to (count, Qnil);
21575 return str;
21578 /* Write a null-terminated, right justified decimal representation of
21579 the positive integer D to BUF using a minimal field width WIDTH. */
21581 static void
21582 pint2str (register char *buf, register int width, register ptrdiff_t d)
21584 register char *p = buf;
21586 if (d <= 0)
21587 *p++ = '0';
21588 else
21590 while (d > 0)
21592 *p++ = d % 10 + '0';
21593 d /= 10;
21597 for (width -= (int) (p - buf); width > 0; --width)
21598 *p++ = ' ';
21599 *p-- = '\0';
21600 while (p > buf)
21602 d = *buf;
21603 *buf++ = *p;
21604 *p-- = d;
21608 /* Write a null-terminated, right justified decimal and "human
21609 readable" representation of the nonnegative integer D to BUF using
21610 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21612 static const char power_letter[] =
21614 0, /* no letter */
21615 'k', /* kilo */
21616 'M', /* mega */
21617 'G', /* giga */
21618 'T', /* tera */
21619 'P', /* peta */
21620 'E', /* exa */
21621 'Z', /* zetta */
21622 'Y' /* yotta */
21625 static void
21626 pint2hrstr (char *buf, int width, ptrdiff_t d)
21628 /* We aim to represent the nonnegative integer D as
21629 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21630 ptrdiff_t quotient = d;
21631 int remainder = 0;
21632 /* -1 means: do not use TENTHS. */
21633 int tenths = -1;
21634 int exponent = 0;
21636 /* Length of QUOTIENT.TENTHS as a string. */
21637 int length;
21639 char * psuffix;
21640 char * p;
21642 if (quotient >= 1000)
21644 /* Scale to the appropriate EXPONENT. */
21647 remainder = quotient % 1000;
21648 quotient /= 1000;
21649 exponent++;
21651 while (quotient >= 1000);
21653 /* Round to nearest and decide whether to use TENTHS or not. */
21654 if (quotient <= 9)
21656 tenths = remainder / 100;
21657 if (remainder % 100 >= 50)
21659 if (tenths < 9)
21660 tenths++;
21661 else
21663 quotient++;
21664 if (quotient == 10)
21665 tenths = -1;
21666 else
21667 tenths = 0;
21671 else
21672 if (remainder >= 500)
21674 if (quotient < 999)
21675 quotient++;
21676 else
21678 quotient = 1;
21679 exponent++;
21680 tenths = 0;
21685 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21686 if (tenths == -1 && quotient <= 99)
21687 if (quotient <= 9)
21688 length = 1;
21689 else
21690 length = 2;
21691 else
21692 length = 3;
21693 p = psuffix = buf + max (width, length);
21695 /* Print EXPONENT. */
21696 *psuffix++ = power_letter[exponent];
21697 *psuffix = '\0';
21699 /* Print TENTHS. */
21700 if (tenths >= 0)
21702 *--p = '0' + tenths;
21703 *--p = '.';
21706 /* Print QUOTIENT. */
21709 int digit = quotient % 10;
21710 *--p = '0' + digit;
21712 while ((quotient /= 10) != 0);
21714 /* Print leading spaces. */
21715 while (buf < p)
21716 *--p = ' ';
21719 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21720 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21721 type of CODING_SYSTEM. Return updated pointer into BUF. */
21723 static unsigned char invalid_eol_type[] = "(*invalid*)";
21725 static char *
21726 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21728 Lisp_Object val;
21729 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21730 const unsigned char *eol_str;
21731 int eol_str_len;
21732 /* The EOL conversion we are using. */
21733 Lisp_Object eoltype;
21735 val = CODING_SYSTEM_SPEC (coding_system);
21736 eoltype = Qnil;
21738 if (!VECTORP (val)) /* Not yet decided. */
21740 *buf++ = multibyte ? '-' : ' ';
21741 if (eol_flag)
21742 eoltype = eol_mnemonic_undecided;
21743 /* Don't mention EOL conversion if it isn't decided. */
21745 else
21747 Lisp_Object attrs;
21748 Lisp_Object eolvalue;
21750 attrs = AREF (val, 0);
21751 eolvalue = AREF (val, 2);
21753 *buf++ = multibyte
21754 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21755 : ' ';
21757 if (eol_flag)
21759 /* The EOL conversion that is normal on this system. */
21761 if (NILP (eolvalue)) /* Not yet decided. */
21762 eoltype = eol_mnemonic_undecided;
21763 else if (VECTORP (eolvalue)) /* Not yet decided. */
21764 eoltype = eol_mnemonic_undecided;
21765 else /* eolvalue is Qunix, Qdos, or Qmac. */
21766 eoltype = (EQ (eolvalue, Qunix)
21767 ? eol_mnemonic_unix
21768 : (EQ (eolvalue, Qdos) == 1
21769 ? eol_mnemonic_dos : eol_mnemonic_mac));
21773 if (eol_flag)
21775 /* Mention the EOL conversion if it is not the usual one. */
21776 if (STRINGP (eoltype))
21778 eol_str = SDATA (eoltype);
21779 eol_str_len = SBYTES (eoltype);
21781 else if (CHARACTERP (eoltype))
21783 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21784 int c = XFASTINT (eoltype);
21785 eol_str_len = CHAR_STRING (c, tmp);
21786 eol_str = tmp;
21788 else
21790 eol_str = invalid_eol_type;
21791 eol_str_len = sizeof (invalid_eol_type) - 1;
21793 memcpy (buf, eol_str, eol_str_len);
21794 buf += eol_str_len;
21797 return buf;
21800 /* Return a string for the output of a mode line %-spec for window W,
21801 generated by character C. FIELD_WIDTH > 0 means pad the string
21802 returned with spaces to that value. Return a Lisp string in
21803 *STRING if the resulting string is taken from that Lisp string.
21805 Note we operate on the current buffer for most purposes. */
21807 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21809 static const char *
21810 decode_mode_spec (struct window *w, register int c, int field_width,
21811 Lisp_Object *string)
21813 Lisp_Object obj;
21814 struct frame *f = XFRAME (WINDOW_FRAME (w));
21815 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21816 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21817 produce strings from numerical values, so limit preposterously
21818 large values of FIELD_WIDTH to avoid overrunning the buffer's
21819 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21820 bytes plus the terminating null. */
21821 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21822 struct buffer *b = current_buffer;
21824 obj = Qnil;
21825 *string = Qnil;
21827 switch (c)
21829 case '*':
21830 if (!NILP (BVAR (b, read_only)))
21831 return "%";
21832 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21833 return "*";
21834 return "-";
21836 case '+':
21837 /* This differs from %* only for a modified read-only buffer. */
21838 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21839 return "*";
21840 if (!NILP (BVAR (b, read_only)))
21841 return "%";
21842 return "-";
21844 case '&':
21845 /* This differs from %* in ignoring read-only-ness. */
21846 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21847 return "*";
21848 return "-";
21850 case '%':
21851 return "%";
21853 case '[':
21855 int i;
21856 char *p;
21858 if (command_loop_level > 5)
21859 return "[[[... ";
21860 p = decode_mode_spec_buf;
21861 for (i = 0; i < command_loop_level; i++)
21862 *p++ = '[';
21863 *p = 0;
21864 return decode_mode_spec_buf;
21867 case ']':
21869 int i;
21870 char *p;
21872 if (command_loop_level > 5)
21873 return " ...]]]";
21874 p = decode_mode_spec_buf;
21875 for (i = 0; i < command_loop_level; i++)
21876 *p++ = ']';
21877 *p = 0;
21878 return decode_mode_spec_buf;
21881 case '-':
21883 register int i;
21885 /* Let lots_of_dashes be a string of infinite length. */
21886 if (mode_line_target == MODE_LINE_NOPROP
21887 || mode_line_target == MODE_LINE_STRING)
21888 return "--";
21889 if (field_width <= 0
21890 || field_width > sizeof (lots_of_dashes))
21892 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21893 decode_mode_spec_buf[i] = '-';
21894 decode_mode_spec_buf[i] = '\0';
21895 return decode_mode_spec_buf;
21897 else
21898 return lots_of_dashes;
21901 case 'b':
21902 obj = BVAR (b, name);
21903 break;
21905 case 'c':
21906 /* %c and %l are ignored in `frame-title-format'.
21907 (In redisplay_internal, the frame title is drawn _before_ the
21908 windows are updated, so the stuff which depends on actual
21909 window contents (such as %l) may fail to render properly, or
21910 even crash emacs.) */
21911 if (mode_line_target == MODE_LINE_TITLE)
21912 return "";
21913 else
21915 ptrdiff_t col = current_column ();
21916 w->column_number_displayed = col;
21917 pint2str (decode_mode_spec_buf, width, col);
21918 return decode_mode_spec_buf;
21921 case 'e':
21922 #ifndef SYSTEM_MALLOC
21924 if (NILP (Vmemory_full))
21925 return "";
21926 else
21927 return "!MEM FULL! ";
21929 #else
21930 return "";
21931 #endif
21933 case 'F':
21934 /* %F displays the frame name. */
21935 if (!NILP (f->title))
21936 return SSDATA (f->title);
21937 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21938 return SSDATA (f->name);
21939 return "Emacs";
21941 case 'f':
21942 obj = BVAR (b, filename);
21943 break;
21945 case 'i':
21947 ptrdiff_t size = ZV - BEGV;
21948 pint2str (decode_mode_spec_buf, width, size);
21949 return decode_mode_spec_buf;
21952 case 'I':
21954 ptrdiff_t size = ZV - BEGV;
21955 pint2hrstr (decode_mode_spec_buf, width, size);
21956 return decode_mode_spec_buf;
21959 case 'l':
21961 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21962 ptrdiff_t topline, nlines, height;
21963 ptrdiff_t junk;
21965 /* %c and %l are ignored in `frame-title-format'. */
21966 if (mode_line_target == MODE_LINE_TITLE)
21967 return "";
21969 startpos = marker_position (w->start);
21970 startpos_byte = marker_byte_position (w->start);
21971 height = WINDOW_TOTAL_LINES (w);
21973 /* If we decided that this buffer isn't suitable for line numbers,
21974 don't forget that too fast. */
21975 if (w->base_line_pos == -1)
21976 goto no_value;
21978 /* If the buffer is very big, don't waste time. */
21979 if (INTEGERP (Vline_number_display_limit)
21980 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21982 w->base_line_pos = 0;
21983 w->base_line_number = 0;
21984 goto no_value;
21987 if (w->base_line_number > 0
21988 && w->base_line_pos > 0
21989 && w->base_line_pos <= startpos)
21991 line = w->base_line_number;
21992 linepos = w->base_line_pos;
21993 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21995 else
21997 line = 1;
21998 linepos = BUF_BEGV (b);
21999 linepos_byte = BUF_BEGV_BYTE (b);
22002 /* Count lines from base line to window start position. */
22003 nlines = display_count_lines (linepos_byte,
22004 startpos_byte,
22005 startpos, &junk);
22007 topline = nlines + line;
22009 /* Determine a new base line, if the old one is too close
22010 or too far away, or if we did not have one.
22011 "Too close" means it's plausible a scroll-down would
22012 go back past it. */
22013 if (startpos == BUF_BEGV (b))
22015 w->base_line_number = topline;
22016 w->base_line_pos = BUF_BEGV (b);
22018 else if (nlines < height + 25 || nlines > height * 3 + 50
22019 || linepos == BUF_BEGV (b))
22021 ptrdiff_t limit = BUF_BEGV (b);
22022 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22023 ptrdiff_t position;
22024 ptrdiff_t distance =
22025 (height * 2 + 30) * line_number_display_limit_width;
22027 if (startpos - distance > limit)
22029 limit = startpos - distance;
22030 limit_byte = CHAR_TO_BYTE (limit);
22033 nlines = display_count_lines (startpos_byte,
22034 limit_byte,
22035 - (height * 2 + 30),
22036 &position);
22037 /* If we couldn't find the lines we wanted within
22038 line_number_display_limit_width chars per line,
22039 give up on line numbers for this window. */
22040 if (position == limit_byte && limit == startpos - distance)
22042 w->base_line_pos = -1;
22043 w->base_line_number = 0;
22044 goto no_value;
22047 w->base_line_number = topline - nlines;
22048 w->base_line_pos = BYTE_TO_CHAR (position);
22051 /* Now count lines from the start pos to point. */
22052 nlines = display_count_lines (startpos_byte,
22053 PT_BYTE, PT, &junk);
22055 /* Record that we did display the line number. */
22056 line_number_displayed = 1;
22058 /* Make the string to show. */
22059 pint2str (decode_mode_spec_buf, width, topline + nlines);
22060 return decode_mode_spec_buf;
22061 no_value:
22063 char* p = decode_mode_spec_buf;
22064 int pad = width - 2;
22065 while (pad-- > 0)
22066 *p++ = ' ';
22067 *p++ = '?';
22068 *p++ = '?';
22069 *p = '\0';
22070 return decode_mode_spec_buf;
22073 break;
22075 case 'm':
22076 obj = BVAR (b, mode_name);
22077 break;
22079 case 'n':
22080 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22081 return " Narrow";
22082 break;
22084 case 'p':
22086 ptrdiff_t pos = marker_position (w->start);
22087 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22089 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22091 if (pos <= BUF_BEGV (b))
22092 return "All";
22093 else
22094 return "Bottom";
22096 else if (pos <= BUF_BEGV (b))
22097 return "Top";
22098 else
22100 if (total > 1000000)
22101 /* Do it differently for a large value, to avoid overflow. */
22102 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22103 else
22104 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22105 /* We can't normally display a 3-digit number,
22106 so get us a 2-digit number that is close. */
22107 if (total == 100)
22108 total = 99;
22109 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22110 return decode_mode_spec_buf;
22114 /* Display percentage of size above the bottom of the screen. */
22115 case 'P':
22117 ptrdiff_t toppos = marker_position (w->start);
22118 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22119 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22121 if (botpos >= BUF_ZV (b))
22123 if (toppos <= BUF_BEGV (b))
22124 return "All";
22125 else
22126 return "Bottom";
22128 else
22130 if (total > 1000000)
22131 /* Do it differently for a large value, to avoid overflow. */
22132 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22133 else
22134 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22135 /* We can't normally display a 3-digit number,
22136 so get us a 2-digit number that is close. */
22137 if (total == 100)
22138 total = 99;
22139 if (toppos <= BUF_BEGV (b))
22140 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22141 else
22142 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22143 return decode_mode_spec_buf;
22147 case 's':
22148 /* status of process */
22149 obj = Fget_buffer_process (Fcurrent_buffer ());
22150 if (NILP (obj))
22151 return "no process";
22152 #ifndef MSDOS
22153 obj = Fsymbol_name (Fprocess_status (obj));
22154 #endif
22155 break;
22157 case '@':
22159 ptrdiff_t count = inhibit_garbage_collection ();
22160 Lisp_Object val = call1 (intern ("file-remote-p"),
22161 BVAR (current_buffer, directory));
22162 unbind_to (count, Qnil);
22164 if (NILP (val))
22165 return "-";
22166 else
22167 return "@";
22170 case 'z':
22171 /* coding-system (not including end-of-line format) */
22172 case 'Z':
22173 /* coding-system (including end-of-line type) */
22175 int eol_flag = (c == 'Z');
22176 char *p = decode_mode_spec_buf;
22178 if (! FRAME_WINDOW_P (f))
22180 /* No need to mention EOL here--the terminal never needs
22181 to do EOL conversion. */
22182 p = decode_mode_spec_coding (CODING_ID_NAME
22183 (FRAME_KEYBOARD_CODING (f)->id),
22184 p, 0);
22185 p = decode_mode_spec_coding (CODING_ID_NAME
22186 (FRAME_TERMINAL_CODING (f)->id),
22187 p, 0);
22189 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22190 p, eol_flag);
22192 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22193 #ifdef subprocesses
22194 obj = Fget_buffer_process (Fcurrent_buffer ());
22195 if (PROCESSP (obj))
22197 p = decode_mode_spec_coding
22198 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22199 p = decode_mode_spec_coding
22200 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22202 #endif /* subprocesses */
22203 #endif /* 0 */
22204 *p = 0;
22205 return decode_mode_spec_buf;
22209 if (STRINGP (obj))
22211 *string = obj;
22212 return SSDATA (obj);
22214 else
22215 return "";
22219 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22220 means count lines back from START_BYTE. But don't go beyond
22221 LIMIT_BYTE. Return the number of lines thus found (always
22222 nonnegative).
22224 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22225 either the position COUNT lines after/before START_BYTE, if we
22226 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22227 COUNT lines. */
22229 static ptrdiff_t
22230 display_count_lines (ptrdiff_t start_byte,
22231 ptrdiff_t limit_byte, ptrdiff_t count,
22232 ptrdiff_t *byte_pos_ptr)
22234 register unsigned char *cursor;
22235 unsigned char *base;
22237 register ptrdiff_t ceiling;
22238 register unsigned char *ceiling_addr;
22239 ptrdiff_t orig_count = count;
22241 /* If we are not in selective display mode,
22242 check only for newlines. */
22243 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22244 && !INTEGERP (BVAR (current_buffer, selective_display)));
22246 if (count > 0)
22248 while (start_byte < limit_byte)
22250 ceiling = BUFFER_CEILING_OF (start_byte);
22251 ceiling = min (limit_byte - 1, ceiling);
22252 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22253 base = (cursor = BYTE_POS_ADDR (start_byte));
22257 if (selective_display)
22259 while (*cursor != '\n' && *cursor != 015
22260 && ++cursor != ceiling_addr)
22261 continue;
22262 if (cursor == ceiling_addr)
22263 break;
22265 else
22267 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22268 if (! cursor)
22269 break;
22272 cursor++;
22274 if (--count == 0)
22276 start_byte += cursor - base;
22277 *byte_pos_ptr = start_byte;
22278 return orig_count;
22281 while (cursor < ceiling_addr);
22283 start_byte += ceiling_addr - base;
22286 else
22288 while (start_byte > limit_byte)
22290 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22291 ceiling = max (limit_byte, ceiling);
22292 ceiling_addr = BYTE_POS_ADDR (ceiling);
22293 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22294 while (1)
22296 if (selective_display)
22298 while (--cursor >= ceiling_addr
22299 && *cursor != '\n' && *cursor != 015)
22300 continue;
22301 if (cursor < ceiling_addr)
22302 break;
22304 else
22306 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22307 if (! cursor)
22308 break;
22311 if (++count == 0)
22313 start_byte += cursor - base + 1;
22314 *byte_pos_ptr = start_byte;
22315 /* When scanning backwards, we should
22316 not count the newline posterior to which we stop. */
22317 return - orig_count - 1;
22320 start_byte += ceiling_addr - base;
22324 *byte_pos_ptr = limit_byte;
22326 if (count < 0)
22327 return - orig_count + count;
22328 return orig_count - count;
22334 /***********************************************************************
22335 Displaying strings
22336 ***********************************************************************/
22338 /* Display a NUL-terminated string, starting with index START.
22340 If STRING is non-null, display that C string. Otherwise, the Lisp
22341 string LISP_STRING is displayed. There's a case that STRING is
22342 non-null and LISP_STRING is not nil. It means STRING is a string
22343 data of LISP_STRING. In that case, we display LISP_STRING while
22344 ignoring its text properties.
22346 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22347 FACE_STRING. Display STRING or LISP_STRING with the face at
22348 FACE_STRING_POS in FACE_STRING:
22350 Display the string in the environment given by IT, but use the
22351 standard display table, temporarily.
22353 FIELD_WIDTH is the minimum number of output glyphs to produce.
22354 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22355 with spaces. If STRING has more characters, more than FIELD_WIDTH
22356 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22358 PRECISION is the maximum number of characters to output from
22359 STRING. PRECISION < 0 means don't truncate the string.
22361 This is roughly equivalent to printf format specifiers:
22363 FIELD_WIDTH PRECISION PRINTF
22364 ----------------------------------------
22365 -1 -1 %s
22366 -1 10 %.10s
22367 10 -1 %10s
22368 20 10 %20.10s
22370 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22371 display them, and < 0 means obey the current buffer's value of
22372 enable_multibyte_characters.
22374 Value is the number of columns displayed. */
22376 static int
22377 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22378 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22379 int field_width, int precision, int max_x, int multibyte)
22381 int hpos_at_start = it->hpos;
22382 int saved_face_id = it->face_id;
22383 struct glyph_row *row = it->glyph_row;
22384 ptrdiff_t it_charpos;
22386 /* Initialize the iterator IT for iteration over STRING beginning
22387 with index START. */
22388 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22389 precision, field_width, multibyte);
22390 if (string && STRINGP (lisp_string))
22391 /* LISP_STRING is the one returned by decode_mode_spec. We should
22392 ignore its text properties. */
22393 it->stop_charpos = it->end_charpos;
22395 /* If displaying STRING, set up the face of the iterator from
22396 FACE_STRING, if that's given. */
22397 if (STRINGP (face_string))
22399 ptrdiff_t endptr;
22400 struct face *face;
22402 it->face_id
22403 = face_at_string_position (it->w, face_string, face_string_pos,
22404 0, it->region_beg_charpos,
22405 it->region_end_charpos,
22406 &endptr, it->base_face_id, 0);
22407 face = FACE_FROM_ID (it->f, it->face_id);
22408 it->face_box_p = face->box != FACE_NO_BOX;
22411 /* Set max_x to the maximum allowed X position. Don't let it go
22412 beyond the right edge of the window. */
22413 if (max_x <= 0)
22414 max_x = it->last_visible_x;
22415 else
22416 max_x = min (max_x, it->last_visible_x);
22418 /* Skip over display elements that are not visible. because IT->w is
22419 hscrolled. */
22420 if (it->current_x < it->first_visible_x)
22421 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22422 MOVE_TO_POS | MOVE_TO_X);
22424 row->ascent = it->max_ascent;
22425 row->height = it->max_ascent + it->max_descent;
22426 row->phys_ascent = it->max_phys_ascent;
22427 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22428 row->extra_line_spacing = it->max_extra_line_spacing;
22430 if (STRINGP (it->string))
22431 it_charpos = IT_STRING_CHARPOS (*it);
22432 else
22433 it_charpos = IT_CHARPOS (*it);
22435 /* This condition is for the case that we are called with current_x
22436 past last_visible_x. */
22437 while (it->current_x < max_x)
22439 int x_before, x, n_glyphs_before, i, nglyphs;
22441 /* Get the next display element. */
22442 if (!get_next_display_element (it))
22443 break;
22445 /* Produce glyphs. */
22446 x_before = it->current_x;
22447 n_glyphs_before = row->used[TEXT_AREA];
22448 PRODUCE_GLYPHS (it);
22450 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22451 i = 0;
22452 x = x_before;
22453 while (i < nglyphs)
22455 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22457 if (it->line_wrap != TRUNCATE
22458 && x + glyph->pixel_width > max_x)
22460 /* End of continued line or max_x reached. */
22461 if (CHAR_GLYPH_PADDING_P (*glyph))
22463 /* A wide character is unbreakable. */
22464 if (row->reversed_p)
22465 unproduce_glyphs (it, row->used[TEXT_AREA]
22466 - n_glyphs_before);
22467 row->used[TEXT_AREA] = n_glyphs_before;
22468 it->current_x = x_before;
22470 else
22472 if (row->reversed_p)
22473 unproduce_glyphs (it, row->used[TEXT_AREA]
22474 - (n_glyphs_before + i));
22475 row->used[TEXT_AREA] = n_glyphs_before + i;
22476 it->current_x = x;
22478 break;
22480 else if (x + glyph->pixel_width >= it->first_visible_x)
22482 /* Glyph is at least partially visible. */
22483 ++it->hpos;
22484 if (x < it->first_visible_x)
22485 row->x = x - it->first_visible_x;
22487 else
22489 /* Glyph is off the left margin of the display area.
22490 Should not happen. */
22491 emacs_abort ();
22494 row->ascent = max (row->ascent, it->max_ascent);
22495 row->height = max (row->height, it->max_ascent + it->max_descent);
22496 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22497 row->phys_height = max (row->phys_height,
22498 it->max_phys_ascent + it->max_phys_descent);
22499 row->extra_line_spacing = max (row->extra_line_spacing,
22500 it->max_extra_line_spacing);
22501 x += glyph->pixel_width;
22502 ++i;
22505 /* Stop if max_x reached. */
22506 if (i < nglyphs)
22507 break;
22509 /* Stop at line ends. */
22510 if (ITERATOR_AT_END_OF_LINE_P (it))
22512 it->continuation_lines_width = 0;
22513 break;
22516 set_iterator_to_next (it, 1);
22517 if (STRINGP (it->string))
22518 it_charpos = IT_STRING_CHARPOS (*it);
22519 else
22520 it_charpos = IT_CHARPOS (*it);
22522 /* Stop if truncating at the right edge. */
22523 if (it->line_wrap == TRUNCATE
22524 && it->current_x >= it->last_visible_x)
22526 /* Add truncation mark, but don't do it if the line is
22527 truncated at a padding space. */
22528 if (it_charpos < it->string_nchars)
22530 if (!FRAME_WINDOW_P (it->f))
22532 int ii, n;
22534 if (it->current_x > it->last_visible_x)
22536 if (!row->reversed_p)
22538 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22539 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22540 break;
22542 else
22544 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22545 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22546 break;
22547 unproduce_glyphs (it, ii + 1);
22548 ii = row->used[TEXT_AREA] - (ii + 1);
22550 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22552 row->used[TEXT_AREA] = ii;
22553 produce_special_glyphs (it, IT_TRUNCATION);
22556 produce_special_glyphs (it, IT_TRUNCATION);
22558 row->truncated_on_right_p = 1;
22560 break;
22564 /* Maybe insert a truncation at the left. */
22565 if (it->first_visible_x
22566 && it_charpos > 0)
22568 if (!FRAME_WINDOW_P (it->f)
22569 || (row->reversed_p
22570 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22571 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22572 insert_left_trunc_glyphs (it);
22573 row->truncated_on_left_p = 1;
22576 it->face_id = saved_face_id;
22578 /* Value is number of columns displayed. */
22579 return it->hpos - hpos_at_start;
22584 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22585 appears as an element of LIST or as the car of an element of LIST.
22586 If PROPVAL is a list, compare each element against LIST in that
22587 way, and return 1/2 if any element of PROPVAL is found in LIST.
22588 Otherwise return 0. This function cannot quit.
22589 The return value is 2 if the text is invisible but with an ellipsis
22590 and 1 if it's invisible and without an ellipsis. */
22593 invisible_p (register Lisp_Object propval, Lisp_Object list)
22595 register Lisp_Object tail, proptail;
22597 for (tail = list; CONSP (tail); tail = XCDR (tail))
22599 register Lisp_Object tem;
22600 tem = XCAR (tail);
22601 if (EQ (propval, tem))
22602 return 1;
22603 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22604 return NILP (XCDR (tem)) ? 1 : 2;
22607 if (CONSP (propval))
22609 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22611 Lisp_Object propelt;
22612 propelt = XCAR (proptail);
22613 for (tail = list; CONSP (tail); tail = XCDR (tail))
22615 register Lisp_Object tem;
22616 tem = XCAR (tail);
22617 if (EQ (propelt, tem))
22618 return 1;
22619 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22620 return NILP (XCDR (tem)) ? 1 : 2;
22625 return 0;
22628 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22629 doc: /* Non-nil if the property makes the text invisible.
22630 POS-OR-PROP can be a marker or number, in which case it is taken to be
22631 a position in the current buffer and the value of the `invisible' property
22632 is checked; or it can be some other value, which is then presumed to be the
22633 value of the `invisible' property of the text of interest.
22634 The non-nil value returned can be t for truly invisible text or something
22635 else if the text is replaced by an ellipsis. */)
22636 (Lisp_Object pos_or_prop)
22638 Lisp_Object prop
22639 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22640 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22641 : pos_or_prop);
22642 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22643 return (invis == 0 ? Qnil
22644 : invis == 1 ? Qt
22645 : make_number (invis));
22648 /* Calculate a width or height in pixels from a specification using
22649 the following elements:
22651 SPEC ::=
22652 NUM - a (fractional) multiple of the default font width/height
22653 (NUM) - specifies exactly NUM pixels
22654 UNIT - a fixed number of pixels, see below.
22655 ELEMENT - size of a display element in pixels, see below.
22656 (NUM . SPEC) - equals NUM * SPEC
22657 (+ SPEC SPEC ...) - add pixel values
22658 (- SPEC SPEC ...) - subtract pixel values
22659 (- SPEC) - negate pixel value
22661 NUM ::=
22662 INT or FLOAT - a number constant
22663 SYMBOL - use symbol's (buffer local) variable binding.
22665 UNIT ::=
22666 in - pixels per inch *)
22667 mm - pixels per 1/1000 meter *)
22668 cm - pixels per 1/100 meter *)
22669 width - width of current font in pixels.
22670 height - height of current font in pixels.
22672 *) using the ratio(s) defined in display-pixels-per-inch.
22674 ELEMENT ::=
22676 left-fringe - left fringe width in pixels
22677 right-fringe - right fringe width in pixels
22679 left-margin - left margin width in pixels
22680 right-margin - right margin width in pixels
22682 scroll-bar - scroll-bar area width in pixels
22684 Examples:
22686 Pixels corresponding to 5 inches:
22687 (5 . in)
22689 Total width of non-text areas on left side of window (if scroll-bar is on left):
22690 '(space :width (+ left-fringe left-margin scroll-bar))
22692 Align to first text column (in header line):
22693 '(space :align-to 0)
22695 Align to middle of text area minus half the width of variable `my-image'
22696 containing a loaded image:
22697 '(space :align-to (0.5 . (- text my-image)))
22699 Width of left margin minus width of 1 character in the default font:
22700 '(space :width (- left-margin 1))
22702 Width of left margin minus width of 2 characters in the current font:
22703 '(space :width (- left-margin (2 . width)))
22705 Center 1 character over left-margin (in header line):
22706 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22708 Different ways to express width of left fringe plus left margin minus one pixel:
22709 '(space :width (- (+ left-fringe left-margin) (1)))
22710 '(space :width (+ left-fringe left-margin (- (1))))
22711 '(space :width (+ left-fringe left-margin (-1)))
22715 static int
22716 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22717 struct font *font, int width_p, int *align_to)
22719 double pixels;
22721 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22722 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22724 if (NILP (prop))
22725 return OK_PIXELS (0);
22727 eassert (FRAME_LIVE_P (it->f));
22729 if (SYMBOLP (prop))
22731 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22733 char *unit = SSDATA (SYMBOL_NAME (prop));
22735 if (unit[0] == 'i' && unit[1] == 'n')
22736 pixels = 1.0;
22737 else if (unit[0] == 'm' && unit[1] == 'm')
22738 pixels = 25.4;
22739 else if (unit[0] == 'c' && unit[1] == 'm')
22740 pixels = 2.54;
22741 else
22742 pixels = 0;
22743 if (pixels > 0)
22745 double ppi = (width_p ? FRAME_RES_X (it->f)
22746 : FRAME_RES_Y (it->f));
22748 if (ppi > 0)
22749 return OK_PIXELS (ppi / pixels);
22750 return 0;
22754 #ifdef HAVE_WINDOW_SYSTEM
22755 if (EQ (prop, Qheight))
22756 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22757 if (EQ (prop, Qwidth))
22758 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22759 #else
22760 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22761 return OK_PIXELS (1);
22762 #endif
22764 if (EQ (prop, Qtext))
22765 return OK_PIXELS (width_p
22766 ? window_box_width (it->w, TEXT_AREA)
22767 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22769 if (align_to && *align_to < 0)
22771 *res = 0;
22772 if (EQ (prop, Qleft))
22773 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22774 if (EQ (prop, Qright))
22775 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22776 if (EQ (prop, Qcenter))
22777 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22778 + window_box_width (it->w, TEXT_AREA) / 2);
22779 if (EQ (prop, Qleft_fringe))
22780 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22781 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22782 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22783 if (EQ (prop, Qright_fringe))
22784 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22785 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22786 : window_box_right_offset (it->w, TEXT_AREA));
22787 if (EQ (prop, Qleft_margin))
22788 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22789 if (EQ (prop, Qright_margin))
22790 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22791 if (EQ (prop, Qscroll_bar))
22792 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22794 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22795 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22796 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22797 : 0)));
22799 else
22801 if (EQ (prop, Qleft_fringe))
22802 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22803 if (EQ (prop, Qright_fringe))
22804 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22805 if (EQ (prop, Qleft_margin))
22806 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22807 if (EQ (prop, Qright_margin))
22808 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22809 if (EQ (prop, Qscroll_bar))
22810 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22813 prop = buffer_local_value_1 (prop, it->w->contents);
22814 if (EQ (prop, Qunbound))
22815 prop = Qnil;
22818 if (INTEGERP (prop) || FLOATP (prop))
22820 int base_unit = (width_p
22821 ? FRAME_COLUMN_WIDTH (it->f)
22822 : FRAME_LINE_HEIGHT (it->f));
22823 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22826 if (CONSP (prop))
22828 Lisp_Object car = XCAR (prop);
22829 Lisp_Object cdr = XCDR (prop);
22831 if (SYMBOLP (car))
22833 #ifdef HAVE_WINDOW_SYSTEM
22834 if (FRAME_WINDOW_P (it->f)
22835 && valid_image_p (prop))
22837 ptrdiff_t id = lookup_image (it->f, prop);
22838 struct image *img = IMAGE_FROM_ID (it->f, id);
22840 return OK_PIXELS (width_p ? img->width : img->height);
22842 #endif
22843 if (EQ (car, Qplus) || EQ (car, Qminus))
22845 int first = 1;
22846 double px;
22848 pixels = 0;
22849 while (CONSP (cdr))
22851 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22852 font, width_p, align_to))
22853 return 0;
22854 if (first)
22855 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22856 else
22857 pixels += px;
22858 cdr = XCDR (cdr);
22860 if (EQ (car, Qminus))
22861 pixels = -pixels;
22862 return OK_PIXELS (pixels);
22865 car = buffer_local_value_1 (car, it->w->contents);
22866 if (EQ (car, Qunbound))
22867 car = Qnil;
22870 if (INTEGERP (car) || FLOATP (car))
22872 double fact;
22873 pixels = XFLOATINT (car);
22874 if (NILP (cdr))
22875 return OK_PIXELS (pixels);
22876 if (calc_pixel_width_or_height (&fact, it, cdr,
22877 font, width_p, align_to))
22878 return OK_PIXELS (pixels * fact);
22879 return 0;
22882 return 0;
22885 return 0;
22889 /***********************************************************************
22890 Glyph Display
22891 ***********************************************************************/
22893 #ifdef HAVE_WINDOW_SYSTEM
22895 #ifdef GLYPH_DEBUG
22897 void
22898 dump_glyph_string (struct glyph_string *s)
22900 fprintf (stderr, "glyph string\n");
22901 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22902 s->x, s->y, s->width, s->height);
22903 fprintf (stderr, " ybase = %d\n", s->ybase);
22904 fprintf (stderr, " hl = %d\n", s->hl);
22905 fprintf (stderr, " left overhang = %d, right = %d\n",
22906 s->left_overhang, s->right_overhang);
22907 fprintf (stderr, " nchars = %d\n", s->nchars);
22908 fprintf (stderr, " extends to end of line = %d\n",
22909 s->extends_to_end_of_line_p);
22910 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22911 fprintf (stderr, " bg width = %d\n", s->background_width);
22914 #endif /* GLYPH_DEBUG */
22916 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22917 of XChar2b structures for S; it can't be allocated in
22918 init_glyph_string because it must be allocated via `alloca'. W
22919 is the window on which S is drawn. ROW and AREA are the glyph row
22920 and area within the row from which S is constructed. START is the
22921 index of the first glyph structure covered by S. HL is a
22922 face-override for drawing S. */
22924 #ifdef HAVE_NTGUI
22925 #define OPTIONAL_HDC(hdc) HDC hdc,
22926 #define DECLARE_HDC(hdc) HDC hdc;
22927 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22928 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22929 #endif
22931 #ifndef OPTIONAL_HDC
22932 #define OPTIONAL_HDC(hdc)
22933 #define DECLARE_HDC(hdc)
22934 #define ALLOCATE_HDC(hdc, f)
22935 #define RELEASE_HDC(hdc, f)
22936 #endif
22938 static void
22939 init_glyph_string (struct glyph_string *s,
22940 OPTIONAL_HDC (hdc)
22941 XChar2b *char2b, struct window *w, struct glyph_row *row,
22942 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22944 memset (s, 0, sizeof *s);
22945 s->w = w;
22946 s->f = XFRAME (w->frame);
22947 #ifdef HAVE_NTGUI
22948 s->hdc = hdc;
22949 #endif
22950 s->display = FRAME_X_DISPLAY (s->f);
22951 s->window = FRAME_X_WINDOW (s->f);
22952 s->char2b = char2b;
22953 s->hl = hl;
22954 s->row = row;
22955 s->area = area;
22956 s->first_glyph = row->glyphs[area] + start;
22957 s->height = row->height;
22958 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22959 s->ybase = s->y + row->ascent;
22963 /* Append the list of glyph strings with head H and tail T to the list
22964 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22966 static void
22967 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22968 struct glyph_string *h, struct glyph_string *t)
22970 if (h)
22972 if (*head)
22973 (*tail)->next = h;
22974 else
22975 *head = h;
22976 h->prev = *tail;
22977 *tail = t;
22982 /* Prepend the list of glyph strings with head H and tail T to the
22983 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22984 result. */
22986 static void
22987 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22988 struct glyph_string *h, struct glyph_string *t)
22990 if (h)
22992 if (*head)
22993 (*head)->prev = t;
22994 else
22995 *tail = t;
22996 t->next = *head;
22997 *head = h;
23002 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23003 Set *HEAD and *TAIL to the resulting list. */
23005 static void
23006 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23007 struct glyph_string *s)
23009 s->next = s->prev = NULL;
23010 append_glyph_string_lists (head, tail, s, s);
23014 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23015 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23016 make sure that X resources for the face returned are allocated.
23017 Value is a pointer to a realized face that is ready for display if
23018 DISPLAY_P is non-zero. */
23020 static struct face *
23021 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23022 XChar2b *char2b, int display_p)
23024 struct face *face = FACE_FROM_ID (f, face_id);
23025 unsigned code = 0;
23027 if (face->font)
23029 code = face->font->driver->encode_char (face->font, c);
23031 if (code == FONT_INVALID_CODE)
23032 code = 0;
23034 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23036 /* Make sure X resources of the face are allocated. */
23037 #ifdef HAVE_X_WINDOWS
23038 if (display_p)
23039 #endif
23041 eassert (face != NULL);
23042 PREPARE_FACE_FOR_DISPLAY (f, face);
23045 return face;
23049 /* Get face and two-byte form of character glyph GLYPH on frame F.
23050 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23051 a pointer to a realized face that is ready for display. */
23053 static struct face *
23054 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23055 XChar2b *char2b, int *two_byte_p)
23057 struct face *face;
23058 unsigned code = 0;
23060 eassert (glyph->type == CHAR_GLYPH);
23061 face = FACE_FROM_ID (f, glyph->face_id);
23063 /* Make sure X resources of the face are allocated. */
23064 eassert (face != NULL);
23065 PREPARE_FACE_FOR_DISPLAY (f, face);
23067 if (two_byte_p)
23068 *two_byte_p = 0;
23070 if (face->font)
23072 if (CHAR_BYTE8_P (glyph->u.ch))
23073 code = CHAR_TO_BYTE8 (glyph->u.ch);
23074 else
23075 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23077 if (code == FONT_INVALID_CODE)
23078 code = 0;
23081 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23082 return face;
23086 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23087 Return 1 if FONT has a glyph for C, otherwise return 0. */
23089 static int
23090 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23092 unsigned code;
23094 if (CHAR_BYTE8_P (c))
23095 code = CHAR_TO_BYTE8 (c);
23096 else
23097 code = font->driver->encode_char (font, c);
23099 if (code == FONT_INVALID_CODE)
23100 return 0;
23101 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23102 return 1;
23106 /* Fill glyph string S with composition components specified by S->cmp.
23108 BASE_FACE is the base face of the composition.
23109 S->cmp_from is the index of the first component for S.
23111 OVERLAPS non-zero means S should draw the foreground only, and use
23112 its physical height for clipping. See also draw_glyphs.
23114 Value is the index of a component not in S. */
23116 static int
23117 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23118 int overlaps)
23120 int i;
23121 /* For all glyphs of this composition, starting at the offset
23122 S->cmp_from, until we reach the end of the definition or encounter a
23123 glyph that requires the different face, add it to S. */
23124 struct face *face;
23126 eassert (s);
23128 s->for_overlaps = overlaps;
23129 s->face = NULL;
23130 s->font = NULL;
23131 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23133 int c = COMPOSITION_GLYPH (s->cmp, i);
23135 /* TAB in a composition means display glyphs with padding space
23136 on the left or right. */
23137 if (c != '\t')
23139 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23140 -1, Qnil);
23142 face = get_char_face_and_encoding (s->f, c, face_id,
23143 s->char2b + i, 1);
23144 if (face)
23146 if (! s->face)
23148 s->face = face;
23149 s->font = s->face->font;
23151 else if (s->face != face)
23152 break;
23155 ++s->nchars;
23157 s->cmp_to = i;
23159 if (s->face == NULL)
23161 s->face = base_face->ascii_face;
23162 s->font = s->face->font;
23165 /* All glyph strings for the same composition has the same width,
23166 i.e. the width set for the first component of the composition. */
23167 s->width = s->first_glyph->pixel_width;
23169 /* If the specified font could not be loaded, use the frame's
23170 default font, but record the fact that we couldn't load it in
23171 the glyph string so that we can draw rectangles for the
23172 characters of the glyph string. */
23173 if (s->font == NULL)
23175 s->font_not_found_p = 1;
23176 s->font = FRAME_FONT (s->f);
23179 /* Adjust base line for subscript/superscript text. */
23180 s->ybase += s->first_glyph->voffset;
23182 /* This glyph string must always be drawn with 16-bit functions. */
23183 s->two_byte_p = 1;
23185 return s->cmp_to;
23188 static int
23189 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23190 int start, int end, int overlaps)
23192 struct glyph *glyph, *last;
23193 Lisp_Object lgstring;
23194 int i;
23196 s->for_overlaps = overlaps;
23197 glyph = s->row->glyphs[s->area] + start;
23198 last = s->row->glyphs[s->area] + end;
23199 s->cmp_id = glyph->u.cmp.id;
23200 s->cmp_from = glyph->slice.cmp.from;
23201 s->cmp_to = glyph->slice.cmp.to + 1;
23202 s->face = FACE_FROM_ID (s->f, face_id);
23203 lgstring = composition_gstring_from_id (s->cmp_id);
23204 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23205 glyph++;
23206 while (glyph < last
23207 && glyph->u.cmp.automatic
23208 && glyph->u.cmp.id == s->cmp_id
23209 && s->cmp_to == glyph->slice.cmp.from)
23210 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23212 for (i = s->cmp_from; i < s->cmp_to; i++)
23214 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23215 unsigned code = LGLYPH_CODE (lglyph);
23217 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23219 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23220 return glyph - s->row->glyphs[s->area];
23224 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23225 See the comment of fill_glyph_string for arguments.
23226 Value is the index of the first glyph not in S. */
23229 static int
23230 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23231 int start, int end, int overlaps)
23233 struct glyph *glyph, *last;
23234 int voffset;
23236 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23237 s->for_overlaps = overlaps;
23238 glyph = s->row->glyphs[s->area] + start;
23239 last = s->row->glyphs[s->area] + end;
23240 voffset = glyph->voffset;
23241 s->face = FACE_FROM_ID (s->f, face_id);
23242 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23243 s->nchars = 1;
23244 s->width = glyph->pixel_width;
23245 glyph++;
23246 while (glyph < last
23247 && glyph->type == GLYPHLESS_GLYPH
23248 && glyph->voffset == voffset
23249 && glyph->face_id == face_id)
23251 s->nchars++;
23252 s->width += glyph->pixel_width;
23253 glyph++;
23255 s->ybase += voffset;
23256 return glyph - s->row->glyphs[s->area];
23260 /* Fill glyph string S from a sequence of character glyphs.
23262 FACE_ID is the face id of the string. START is the index of the
23263 first glyph to consider, END is the index of the last + 1.
23264 OVERLAPS non-zero means S should draw the foreground only, and use
23265 its physical height for clipping. See also draw_glyphs.
23267 Value is the index of the first glyph not in S. */
23269 static int
23270 fill_glyph_string (struct glyph_string *s, int face_id,
23271 int start, int end, int overlaps)
23273 struct glyph *glyph, *last;
23274 int voffset;
23275 int glyph_not_available_p;
23277 eassert (s->f == XFRAME (s->w->frame));
23278 eassert (s->nchars == 0);
23279 eassert (start >= 0 && end > start);
23281 s->for_overlaps = overlaps;
23282 glyph = s->row->glyphs[s->area] + start;
23283 last = s->row->glyphs[s->area] + end;
23284 voffset = glyph->voffset;
23285 s->padding_p = glyph->padding_p;
23286 glyph_not_available_p = glyph->glyph_not_available_p;
23288 while (glyph < last
23289 && glyph->type == CHAR_GLYPH
23290 && glyph->voffset == voffset
23291 /* Same face id implies same font, nowadays. */
23292 && glyph->face_id == face_id
23293 && glyph->glyph_not_available_p == glyph_not_available_p)
23295 int two_byte_p;
23297 s->face = get_glyph_face_and_encoding (s->f, glyph,
23298 s->char2b + s->nchars,
23299 &two_byte_p);
23300 s->two_byte_p = two_byte_p;
23301 ++s->nchars;
23302 eassert (s->nchars <= end - start);
23303 s->width += glyph->pixel_width;
23304 if (glyph++->padding_p != s->padding_p)
23305 break;
23308 s->font = s->face->font;
23310 /* If the specified font could not be loaded, use the frame's font,
23311 but record the fact that we couldn't load it in
23312 S->font_not_found_p so that we can draw rectangles for the
23313 characters of the glyph string. */
23314 if (s->font == NULL || glyph_not_available_p)
23316 s->font_not_found_p = 1;
23317 s->font = FRAME_FONT (s->f);
23320 /* Adjust base line for subscript/superscript text. */
23321 s->ybase += voffset;
23323 eassert (s->face && s->face->gc);
23324 return glyph - s->row->glyphs[s->area];
23328 /* Fill glyph string S from image glyph S->first_glyph. */
23330 static void
23331 fill_image_glyph_string (struct glyph_string *s)
23333 eassert (s->first_glyph->type == IMAGE_GLYPH);
23334 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23335 eassert (s->img);
23336 s->slice = s->first_glyph->slice.img;
23337 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23338 s->font = s->face->font;
23339 s->width = s->first_glyph->pixel_width;
23341 /* Adjust base line for subscript/superscript text. */
23342 s->ybase += s->first_glyph->voffset;
23346 /* Fill glyph string S from a sequence of stretch glyphs.
23348 START is the index of the first glyph to consider,
23349 END is the index of the last + 1.
23351 Value is the index of the first glyph not in S. */
23353 static int
23354 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23356 struct glyph *glyph, *last;
23357 int voffset, face_id;
23359 eassert (s->first_glyph->type == STRETCH_GLYPH);
23361 glyph = s->row->glyphs[s->area] + start;
23362 last = s->row->glyphs[s->area] + end;
23363 face_id = glyph->face_id;
23364 s->face = FACE_FROM_ID (s->f, face_id);
23365 s->font = s->face->font;
23366 s->width = glyph->pixel_width;
23367 s->nchars = 1;
23368 voffset = glyph->voffset;
23370 for (++glyph;
23371 (glyph < last
23372 && glyph->type == STRETCH_GLYPH
23373 && glyph->voffset == voffset
23374 && glyph->face_id == face_id);
23375 ++glyph)
23376 s->width += glyph->pixel_width;
23378 /* Adjust base line for subscript/superscript text. */
23379 s->ybase += voffset;
23381 /* The case that face->gc == 0 is handled when drawing the glyph
23382 string by calling PREPARE_FACE_FOR_DISPLAY. */
23383 eassert (s->face);
23384 return glyph - s->row->glyphs[s->area];
23387 static struct font_metrics *
23388 get_per_char_metric (struct font *font, XChar2b *char2b)
23390 static struct font_metrics metrics;
23391 unsigned code;
23393 if (! font)
23394 return NULL;
23395 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23396 if (code == FONT_INVALID_CODE)
23397 return NULL;
23398 font->driver->text_extents (font, &code, 1, &metrics);
23399 return &metrics;
23402 /* EXPORT for RIF:
23403 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23404 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23405 assumed to be zero. */
23407 void
23408 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23410 *left = *right = 0;
23412 if (glyph->type == CHAR_GLYPH)
23414 struct face *face;
23415 XChar2b char2b;
23416 struct font_metrics *pcm;
23418 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23419 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23421 if (pcm->rbearing > pcm->width)
23422 *right = pcm->rbearing - pcm->width;
23423 if (pcm->lbearing < 0)
23424 *left = -pcm->lbearing;
23427 else if (glyph->type == COMPOSITE_GLYPH)
23429 if (! glyph->u.cmp.automatic)
23431 struct composition *cmp = composition_table[glyph->u.cmp.id];
23433 if (cmp->rbearing > cmp->pixel_width)
23434 *right = cmp->rbearing - cmp->pixel_width;
23435 if (cmp->lbearing < 0)
23436 *left = - cmp->lbearing;
23438 else
23440 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23441 struct font_metrics metrics;
23443 composition_gstring_width (gstring, glyph->slice.cmp.from,
23444 glyph->slice.cmp.to + 1, &metrics);
23445 if (metrics.rbearing > metrics.width)
23446 *right = metrics.rbearing - metrics.width;
23447 if (metrics.lbearing < 0)
23448 *left = - metrics.lbearing;
23454 /* Return the index of the first glyph preceding glyph string S that
23455 is overwritten by S because of S's left overhang. Value is -1
23456 if no glyphs are overwritten. */
23458 static int
23459 left_overwritten (struct glyph_string *s)
23461 int k;
23463 if (s->left_overhang)
23465 int x = 0, i;
23466 struct glyph *glyphs = s->row->glyphs[s->area];
23467 int first = s->first_glyph - glyphs;
23469 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23470 x -= glyphs[i].pixel_width;
23472 k = i + 1;
23474 else
23475 k = -1;
23477 return k;
23481 /* Return the index of the first glyph preceding glyph string S that
23482 is overwriting S because of its right overhang. Value is -1 if no
23483 glyph in front of S overwrites S. */
23485 static int
23486 left_overwriting (struct glyph_string *s)
23488 int i, k, x;
23489 struct glyph *glyphs = s->row->glyphs[s->area];
23490 int first = s->first_glyph - glyphs;
23492 k = -1;
23493 x = 0;
23494 for (i = first - 1; i >= 0; --i)
23496 int left, right;
23497 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23498 if (x + right > 0)
23499 k = i;
23500 x -= glyphs[i].pixel_width;
23503 return k;
23507 /* Return the index of the last glyph following glyph string S that is
23508 overwritten by S because of S's right overhang. Value is -1 if
23509 no such glyph is found. */
23511 static int
23512 right_overwritten (struct glyph_string *s)
23514 int k = -1;
23516 if (s->right_overhang)
23518 int x = 0, i;
23519 struct glyph *glyphs = s->row->glyphs[s->area];
23520 int first = (s->first_glyph - glyphs
23521 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23522 int end = s->row->used[s->area];
23524 for (i = first; i < end && s->right_overhang > x; ++i)
23525 x += glyphs[i].pixel_width;
23527 k = i;
23530 return k;
23534 /* Return the index of the last glyph following glyph string S that
23535 overwrites S because of its left overhang. Value is negative
23536 if no such glyph is found. */
23538 static int
23539 right_overwriting (struct glyph_string *s)
23541 int i, k, x;
23542 int end = s->row->used[s->area];
23543 struct glyph *glyphs = s->row->glyphs[s->area];
23544 int first = (s->first_glyph - glyphs
23545 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23547 k = -1;
23548 x = 0;
23549 for (i = first; i < end; ++i)
23551 int left, right;
23552 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23553 if (x - left < 0)
23554 k = i;
23555 x += glyphs[i].pixel_width;
23558 return k;
23562 /* Set background width of glyph string S. START is the index of the
23563 first glyph following S. LAST_X is the right-most x-position + 1
23564 in the drawing area. */
23566 static void
23567 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23569 /* If the face of this glyph string has to be drawn to the end of
23570 the drawing area, set S->extends_to_end_of_line_p. */
23572 if (start == s->row->used[s->area]
23573 && s->area == TEXT_AREA
23574 && ((s->row->fill_line_p
23575 && (s->hl == DRAW_NORMAL_TEXT
23576 || s->hl == DRAW_IMAGE_RAISED
23577 || s->hl == DRAW_IMAGE_SUNKEN))
23578 || s->hl == DRAW_MOUSE_FACE))
23579 s->extends_to_end_of_line_p = 1;
23581 /* If S extends its face to the end of the line, set its
23582 background_width to the distance to the right edge of the drawing
23583 area. */
23584 if (s->extends_to_end_of_line_p)
23585 s->background_width = last_x - s->x + 1;
23586 else
23587 s->background_width = s->width;
23591 /* Compute overhangs and x-positions for glyph string S and its
23592 predecessors, or successors. X is the starting x-position for S.
23593 BACKWARD_P non-zero means process predecessors. */
23595 static void
23596 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23598 if (backward_p)
23600 while (s)
23602 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23603 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23604 x -= s->width;
23605 s->x = x;
23606 s = s->prev;
23609 else
23611 while (s)
23613 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23614 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23615 s->x = x;
23616 x += s->width;
23617 s = s->next;
23624 /* The following macros are only called from draw_glyphs below.
23625 They reference the following parameters of that function directly:
23626 `w', `row', `area', and `overlap_p'
23627 as well as the following local variables:
23628 `s', `f', and `hdc' (in W32) */
23630 #ifdef HAVE_NTGUI
23631 /* On W32, silently add local `hdc' variable to argument list of
23632 init_glyph_string. */
23633 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23634 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23635 #else
23636 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23637 init_glyph_string (s, char2b, w, row, area, start, hl)
23638 #endif
23640 /* Add a glyph string for a stretch glyph to the list of strings
23641 between HEAD and TAIL. START is the index of the stretch glyph in
23642 row area AREA of glyph row ROW. END is the index of the last glyph
23643 in that glyph row area. X is the current output position assigned
23644 to the new glyph string constructed. HL overrides that face of the
23645 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23646 is the right-most x-position of the drawing area. */
23648 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23649 and below -- keep them on one line. */
23650 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23651 do \
23653 s = alloca (sizeof *s); \
23654 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23655 START = fill_stretch_glyph_string (s, START, END); \
23656 append_glyph_string (&HEAD, &TAIL, s); \
23657 s->x = (X); \
23659 while (0)
23662 /* Add a glyph string for an image glyph to the list of strings
23663 between HEAD and TAIL. START is the index of the image glyph in
23664 row area AREA of glyph row ROW. END is the index of the last glyph
23665 in that glyph row area. X is the current output position assigned
23666 to the new glyph string constructed. HL overrides that face of the
23667 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23668 is the right-most x-position of the drawing area. */
23670 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23671 do \
23673 s = alloca (sizeof *s); \
23674 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23675 fill_image_glyph_string (s); \
23676 append_glyph_string (&HEAD, &TAIL, s); \
23677 ++START; \
23678 s->x = (X); \
23680 while (0)
23683 /* Add a glyph string for a sequence of character glyphs to the list
23684 of strings between HEAD and TAIL. START is the index of the first
23685 glyph in row area AREA of glyph row ROW that is part of the new
23686 glyph string. END is the index of the last glyph in that glyph row
23687 area. X is the current output position assigned to the new glyph
23688 string constructed. HL overrides that face of the glyph; e.g. it
23689 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23690 right-most x-position of the drawing area. */
23692 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23693 do \
23695 int face_id; \
23696 XChar2b *char2b; \
23698 face_id = (row)->glyphs[area][START].face_id; \
23700 s = alloca (sizeof *s); \
23701 char2b = alloca ((END - START) * sizeof *char2b); \
23702 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23703 append_glyph_string (&HEAD, &TAIL, s); \
23704 s->x = (X); \
23705 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23707 while (0)
23710 /* Add a glyph string for a composite sequence to the list of strings
23711 between HEAD and TAIL. START is the index of the first glyph in
23712 row area AREA of glyph row ROW that is part of the new glyph
23713 string. END is the index of the last glyph in that glyph row area.
23714 X is the current output position assigned to the new glyph string
23715 constructed. HL overrides that face of the glyph; e.g. it is
23716 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23717 x-position of the drawing area. */
23719 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23720 do { \
23721 int face_id = (row)->glyphs[area][START].face_id; \
23722 struct face *base_face = FACE_FROM_ID (f, face_id); \
23723 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23724 struct composition *cmp = composition_table[cmp_id]; \
23725 XChar2b *char2b; \
23726 struct glyph_string *first_s = NULL; \
23727 int n; \
23729 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23731 /* Make glyph_strings for each glyph sequence that is drawable by \
23732 the same face, and append them to HEAD/TAIL. */ \
23733 for (n = 0; n < cmp->glyph_len;) \
23735 s = alloca (sizeof *s); \
23736 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23737 append_glyph_string (&(HEAD), &(TAIL), s); \
23738 s->cmp = cmp; \
23739 s->cmp_from = n; \
23740 s->x = (X); \
23741 if (n == 0) \
23742 first_s = s; \
23743 n = fill_composite_glyph_string (s, base_face, overlaps); \
23746 ++START; \
23747 s = first_s; \
23748 } while (0)
23751 /* Add a glyph string for a glyph-string sequence to the list of strings
23752 between HEAD and TAIL. */
23754 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23755 do { \
23756 int face_id; \
23757 XChar2b *char2b; \
23758 Lisp_Object gstring; \
23760 face_id = (row)->glyphs[area][START].face_id; \
23761 gstring = (composition_gstring_from_id \
23762 ((row)->glyphs[area][START].u.cmp.id)); \
23763 s = alloca (sizeof *s); \
23764 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23765 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23766 append_glyph_string (&(HEAD), &(TAIL), s); \
23767 s->x = (X); \
23768 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23769 } while (0)
23772 /* Add a glyph string for a sequence of glyphless character's glyphs
23773 to the list of strings between HEAD and TAIL. The meanings of
23774 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23776 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23777 do \
23779 int face_id; \
23781 face_id = (row)->glyphs[area][START].face_id; \
23783 s = alloca (sizeof *s); \
23784 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23785 append_glyph_string (&HEAD, &TAIL, s); \
23786 s->x = (X); \
23787 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23788 overlaps); \
23790 while (0)
23793 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23794 of AREA of glyph row ROW on window W between indices START and END.
23795 HL overrides the face for drawing glyph strings, e.g. it is
23796 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23797 x-positions of the drawing area.
23799 This is an ugly monster macro construct because we must use alloca
23800 to allocate glyph strings (because draw_glyphs can be called
23801 asynchronously). */
23803 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23804 do \
23806 HEAD = TAIL = NULL; \
23807 while (START < END) \
23809 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23810 switch (first_glyph->type) \
23812 case CHAR_GLYPH: \
23813 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23814 HL, X, LAST_X); \
23815 break; \
23817 case COMPOSITE_GLYPH: \
23818 if (first_glyph->u.cmp.automatic) \
23819 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23820 HL, X, LAST_X); \
23821 else \
23822 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23823 HL, X, LAST_X); \
23824 break; \
23826 case STRETCH_GLYPH: \
23827 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23828 HL, X, LAST_X); \
23829 break; \
23831 case IMAGE_GLYPH: \
23832 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23833 HL, X, LAST_X); \
23834 break; \
23836 case GLYPHLESS_GLYPH: \
23837 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23838 HL, X, LAST_X); \
23839 break; \
23841 default: \
23842 emacs_abort (); \
23845 if (s) \
23847 set_glyph_string_background_width (s, START, LAST_X); \
23848 (X) += s->width; \
23851 } while (0)
23854 /* Draw glyphs between START and END in AREA of ROW on window W,
23855 starting at x-position X. X is relative to AREA in W. HL is a
23856 face-override with the following meaning:
23858 DRAW_NORMAL_TEXT draw normally
23859 DRAW_CURSOR draw in cursor face
23860 DRAW_MOUSE_FACE draw in mouse face.
23861 DRAW_INVERSE_VIDEO draw in mode line face
23862 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23863 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23865 If OVERLAPS is non-zero, draw only the foreground of characters and
23866 clip to the physical height of ROW. Non-zero value also defines
23867 the overlapping part to be drawn:
23869 OVERLAPS_PRED overlap with preceding rows
23870 OVERLAPS_SUCC overlap with succeeding rows
23871 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23872 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23874 Value is the x-position reached, relative to AREA of W. */
23876 static int
23877 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23878 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23879 enum draw_glyphs_face hl, int overlaps)
23881 struct glyph_string *head, *tail;
23882 struct glyph_string *s;
23883 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23884 int i, j, x_reached, last_x, area_left = 0;
23885 struct frame *f = XFRAME (WINDOW_FRAME (w));
23886 DECLARE_HDC (hdc);
23888 ALLOCATE_HDC (hdc, f);
23890 /* Let's rather be paranoid than getting a SEGV. */
23891 end = min (end, row->used[area]);
23892 start = clip_to_bounds (0, start, end);
23894 /* Translate X to frame coordinates. Set last_x to the right
23895 end of the drawing area. */
23896 if (row->full_width_p)
23898 /* X is relative to the left edge of W, without scroll bars
23899 or fringes. */
23900 area_left = WINDOW_LEFT_EDGE_X (w);
23901 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23903 else
23905 area_left = window_box_left (w, area);
23906 last_x = area_left + window_box_width (w, area);
23908 x += area_left;
23910 /* Build a doubly-linked list of glyph_string structures between
23911 head and tail from what we have to draw. Note that the macro
23912 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23913 the reason we use a separate variable `i'. */
23914 i = start;
23915 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23916 if (tail)
23917 x_reached = tail->x + tail->background_width;
23918 else
23919 x_reached = x;
23921 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23922 the row, redraw some glyphs in front or following the glyph
23923 strings built above. */
23924 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23926 struct glyph_string *h, *t;
23927 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23928 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23929 int check_mouse_face = 0;
23930 int dummy_x = 0;
23932 /* If mouse highlighting is on, we may need to draw adjacent
23933 glyphs using mouse-face highlighting. */
23934 if (area == TEXT_AREA && row->mouse_face_p
23935 && hlinfo->mouse_face_beg_row >= 0
23936 && hlinfo->mouse_face_end_row >= 0)
23938 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23940 if (row_vpos >= hlinfo->mouse_face_beg_row
23941 && row_vpos <= hlinfo->mouse_face_end_row)
23943 check_mouse_face = 1;
23944 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23945 ? hlinfo->mouse_face_beg_col : 0;
23946 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23947 ? hlinfo->mouse_face_end_col
23948 : row->used[TEXT_AREA];
23952 /* Compute overhangs for all glyph strings. */
23953 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23954 for (s = head; s; s = s->next)
23955 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23957 /* Prepend glyph strings for glyphs in front of the first glyph
23958 string that are overwritten because of the first glyph
23959 string's left overhang. The background of all strings
23960 prepended must be drawn because the first glyph string
23961 draws over it. */
23962 i = left_overwritten (head);
23963 if (i >= 0)
23965 enum draw_glyphs_face overlap_hl;
23967 /* If this row contains mouse highlighting, attempt to draw
23968 the overlapped glyphs with the correct highlight. This
23969 code fails if the overlap encompasses more than one glyph
23970 and mouse-highlight spans only some of these glyphs.
23971 However, making it work perfectly involves a lot more
23972 code, and I don't know if the pathological case occurs in
23973 practice, so we'll stick to this for now. --- cyd */
23974 if (check_mouse_face
23975 && mouse_beg_col < start && mouse_end_col > i)
23976 overlap_hl = DRAW_MOUSE_FACE;
23977 else
23978 overlap_hl = DRAW_NORMAL_TEXT;
23980 j = i;
23981 BUILD_GLYPH_STRINGS (j, start, h, t,
23982 overlap_hl, dummy_x, last_x);
23983 start = i;
23984 compute_overhangs_and_x (t, head->x, 1);
23985 prepend_glyph_string_lists (&head, &tail, h, t);
23986 clip_head = head;
23989 /* Prepend glyph strings for glyphs in front of the first glyph
23990 string that overwrite that glyph string because of their
23991 right overhang. For these strings, only the foreground must
23992 be drawn, because it draws over the glyph string at `head'.
23993 The background must not be drawn because this would overwrite
23994 right overhangs of preceding glyphs for which no glyph
23995 strings exist. */
23996 i = left_overwriting (head);
23997 if (i >= 0)
23999 enum draw_glyphs_face overlap_hl;
24001 if (check_mouse_face
24002 && mouse_beg_col < start && mouse_end_col > i)
24003 overlap_hl = DRAW_MOUSE_FACE;
24004 else
24005 overlap_hl = DRAW_NORMAL_TEXT;
24007 clip_head = head;
24008 BUILD_GLYPH_STRINGS (i, start, h, t,
24009 overlap_hl, dummy_x, last_x);
24010 for (s = h; s; s = s->next)
24011 s->background_filled_p = 1;
24012 compute_overhangs_and_x (t, head->x, 1);
24013 prepend_glyph_string_lists (&head, &tail, h, t);
24016 /* Append glyphs strings for glyphs following the last glyph
24017 string tail that are overwritten by tail. The background of
24018 these strings has to be drawn because tail's foreground draws
24019 over it. */
24020 i = right_overwritten (tail);
24021 if (i >= 0)
24023 enum draw_glyphs_face overlap_hl;
24025 if (check_mouse_face
24026 && mouse_beg_col < i && mouse_end_col > end)
24027 overlap_hl = DRAW_MOUSE_FACE;
24028 else
24029 overlap_hl = DRAW_NORMAL_TEXT;
24031 BUILD_GLYPH_STRINGS (end, i, h, t,
24032 overlap_hl, x, last_x);
24033 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24034 we don't have `end = i;' here. */
24035 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24036 append_glyph_string_lists (&head, &tail, h, t);
24037 clip_tail = tail;
24040 /* Append glyph strings for glyphs following the last glyph
24041 string tail that overwrite tail. The foreground of such
24042 glyphs has to be drawn because it writes into the background
24043 of tail. The background must not be drawn because it could
24044 paint over the foreground of following glyphs. */
24045 i = right_overwriting (tail);
24046 if (i >= 0)
24048 enum draw_glyphs_face overlap_hl;
24049 if (check_mouse_face
24050 && mouse_beg_col < i && mouse_end_col > end)
24051 overlap_hl = DRAW_MOUSE_FACE;
24052 else
24053 overlap_hl = DRAW_NORMAL_TEXT;
24055 clip_tail = tail;
24056 i++; /* We must include the Ith glyph. */
24057 BUILD_GLYPH_STRINGS (end, i, h, t,
24058 overlap_hl, x, last_x);
24059 for (s = h; s; s = s->next)
24060 s->background_filled_p = 1;
24061 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24062 append_glyph_string_lists (&head, &tail, h, t);
24064 if (clip_head || clip_tail)
24065 for (s = head; s; s = s->next)
24067 s->clip_head = clip_head;
24068 s->clip_tail = clip_tail;
24072 /* Draw all strings. */
24073 for (s = head; s; s = s->next)
24074 FRAME_RIF (f)->draw_glyph_string (s);
24076 #ifndef HAVE_NS
24077 /* When focus a sole frame and move horizontally, this sets on_p to 0
24078 causing a failure to erase prev cursor position. */
24079 if (area == TEXT_AREA
24080 && !row->full_width_p
24081 /* When drawing overlapping rows, only the glyph strings'
24082 foreground is drawn, which doesn't erase a cursor
24083 completely. */
24084 && !overlaps)
24086 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24087 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24088 : (tail ? tail->x + tail->background_width : x));
24089 x0 -= area_left;
24090 x1 -= area_left;
24092 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24093 row->y, MATRIX_ROW_BOTTOM_Y (row));
24095 #endif
24097 /* Value is the x-position up to which drawn, relative to AREA of W.
24098 This doesn't include parts drawn because of overhangs. */
24099 if (row->full_width_p)
24100 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24101 else
24102 x_reached -= area_left;
24104 RELEASE_HDC (hdc, f);
24106 return x_reached;
24109 /* Expand row matrix if too narrow. Don't expand if area
24110 is not present. */
24112 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24114 if (!fonts_changed_p \
24115 && (it->glyph_row->glyphs[area] \
24116 < it->glyph_row->glyphs[area + 1])) \
24118 it->w->ncols_scale_factor++; \
24119 fonts_changed_p = 1; \
24123 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24124 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24126 static void
24127 append_glyph (struct it *it)
24129 struct glyph *glyph;
24130 enum glyph_row_area area = it->area;
24132 eassert (it->glyph_row);
24133 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24135 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24136 if (glyph < it->glyph_row->glyphs[area + 1])
24138 /* If the glyph row is reversed, we need to prepend the glyph
24139 rather than append it. */
24140 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24142 struct glyph *g;
24144 /* Make room for the additional glyph. */
24145 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24146 g[1] = *g;
24147 glyph = it->glyph_row->glyphs[area];
24149 glyph->charpos = CHARPOS (it->position);
24150 glyph->object = it->object;
24151 if (it->pixel_width > 0)
24153 glyph->pixel_width = it->pixel_width;
24154 glyph->padding_p = 0;
24156 else
24158 /* Assure at least 1-pixel width. Otherwise, cursor can't
24159 be displayed correctly. */
24160 glyph->pixel_width = 1;
24161 glyph->padding_p = 1;
24163 glyph->ascent = it->ascent;
24164 glyph->descent = it->descent;
24165 glyph->voffset = it->voffset;
24166 glyph->type = CHAR_GLYPH;
24167 glyph->avoid_cursor_p = it->avoid_cursor_p;
24168 glyph->multibyte_p = it->multibyte_p;
24169 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24171 /* In R2L rows, the left and the right box edges need to be
24172 drawn in reverse direction. */
24173 glyph->right_box_line_p = it->start_of_box_run_p;
24174 glyph->left_box_line_p = it->end_of_box_run_p;
24176 else
24178 glyph->left_box_line_p = it->start_of_box_run_p;
24179 glyph->right_box_line_p = it->end_of_box_run_p;
24181 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24182 || it->phys_descent > it->descent);
24183 glyph->glyph_not_available_p = it->glyph_not_available_p;
24184 glyph->face_id = it->face_id;
24185 glyph->u.ch = it->char_to_display;
24186 glyph->slice.img = null_glyph_slice;
24187 glyph->font_type = FONT_TYPE_UNKNOWN;
24188 if (it->bidi_p)
24190 glyph->resolved_level = it->bidi_it.resolved_level;
24191 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24192 emacs_abort ();
24193 glyph->bidi_type = it->bidi_it.type;
24195 else
24197 glyph->resolved_level = 0;
24198 glyph->bidi_type = UNKNOWN_BT;
24200 ++it->glyph_row->used[area];
24202 else
24203 IT_EXPAND_MATRIX_WIDTH (it, area);
24206 /* Store one glyph for the composition IT->cmp_it.id in
24207 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24208 non-null. */
24210 static void
24211 append_composite_glyph (struct it *it)
24213 struct glyph *glyph;
24214 enum glyph_row_area area = it->area;
24216 eassert (it->glyph_row);
24218 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24219 if (glyph < it->glyph_row->glyphs[area + 1])
24221 /* If the glyph row is reversed, we need to prepend the glyph
24222 rather than append it. */
24223 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24225 struct glyph *g;
24227 /* Make room for the new glyph. */
24228 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24229 g[1] = *g;
24230 glyph = it->glyph_row->glyphs[it->area];
24232 glyph->charpos = it->cmp_it.charpos;
24233 glyph->object = it->object;
24234 glyph->pixel_width = it->pixel_width;
24235 glyph->ascent = it->ascent;
24236 glyph->descent = it->descent;
24237 glyph->voffset = it->voffset;
24238 glyph->type = COMPOSITE_GLYPH;
24239 if (it->cmp_it.ch < 0)
24241 glyph->u.cmp.automatic = 0;
24242 glyph->u.cmp.id = it->cmp_it.id;
24243 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24245 else
24247 glyph->u.cmp.automatic = 1;
24248 glyph->u.cmp.id = it->cmp_it.id;
24249 glyph->slice.cmp.from = it->cmp_it.from;
24250 glyph->slice.cmp.to = it->cmp_it.to - 1;
24252 glyph->avoid_cursor_p = it->avoid_cursor_p;
24253 glyph->multibyte_p = it->multibyte_p;
24254 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24256 /* In R2L rows, the left and the right box edges need to be
24257 drawn in reverse direction. */
24258 glyph->right_box_line_p = it->start_of_box_run_p;
24259 glyph->left_box_line_p = it->end_of_box_run_p;
24261 else
24263 glyph->left_box_line_p = it->start_of_box_run_p;
24264 glyph->right_box_line_p = it->end_of_box_run_p;
24266 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24267 || it->phys_descent > it->descent);
24268 glyph->padding_p = 0;
24269 glyph->glyph_not_available_p = 0;
24270 glyph->face_id = it->face_id;
24271 glyph->font_type = FONT_TYPE_UNKNOWN;
24272 if (it->bidi_p)
24274 glyph->resolved_level = it->bidi_it.resolved_level;
24275 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24276 emacs_abort ();
24277 glyph->bidi_type = it->bidi_it.type;
24279 ++it->glyph_row->used[area];
24281 else
24282 IT_EXPAND_MATRIX_WIDTH (it, area);
24286 /* Change IT->ascent and IT->height according to the setting of
24287 IT->voffset. */
24289 static void
24290 take_vertical_position_into_account (struct it *it)
24292 if (it->voffset)
24294 if (it->voffset < 0)
24295 /* Increase the ascent so that we can display the text higher
24296 in the line. */
24297 it->ascent -= it->voffset;
24298 else
24299 /* Increase the descent so that we can display the text lower
24300 in the line. */
24301 it->descent += it->voffset;
24306 /* Produce glyphs/get display metrics for the image IT is loaded with.
24307 See the description of struct display_iterator in dispextern.h for
24308 an overview of struct display_iterator. */
24310 static void
24311 produce_image_glyph (struct it *it)
24313 struct image *img;
24314 struct face *face;
24315 int glyph_ascent, crop;
24316 struct glyph_slice slice;
24318 eassert (it->what == IT_IMAGE);
24320 face = FACE_FROM_ID (it->f, it->face_id);
24321 eassert (face);
24322 /* Make sure X resources of the face is loaded. */
24323 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24325 if (it->image_id < 0)
24327 /* Fringe bitmap. */
24328 it->ascent = it->phys_ascent = 0;
24329 it->descent = it->phys_descent = 0;
24330 it->pixel_width = 0;
24331 it->nglyphs = 0;
24332 return;
24335 img = IMAGE_FROM_ID (it->f, it->image_id);
24336 eassert (img);
24337 /* Make sure X resources of the image is loaded. */
24338 prepare_image_for_display (it->f, img);
24340 slice.x = slice.y = 0;
24341 slice.width = img->width;
24342 slice.height = img->height;
24344 if (INTEGERP (it->slice.x))
24345 slice.x = XINT (it->slice.x);
24346 else if (FLOATP (it->slice.x))
24347 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24349 if (INTEGERP (it->slice.y))
24350 slice.y = XINT (it->slice.y);
24351 else if (FLOATP (it->slice.y))
24352 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24354 if (INTEGERP (it->slice.width))
24355 slice.width = XINT (it->slice.width);
24356 else if (FLOATP (it->slice.width))
24357 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24359 if (INTEGERP (it->slice.height))
24360 slice.height = XINT (it->slice.height);
24361 else if (FLOATP (it->slice.height))
24362 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24364 if (slice.x >= img->width)
24365 slice.x = img->width;
24366 if (slice.y >= img->height)
24367 slice.y = img->height;
24368 if (slice.x + slice.width >= img->width)
24369 slice.width = img->width - slice.x;
24370 if (slice.y + slice.height > img->height)
24371 slice.height = img->height - slice.y;
24373 if (slice.width == 0 || slice.height == 0)
24374 return;
24376 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24378 it->descent = slice.height - glyph_ascent;
24379 if (slice.y == 0)
24380 it->descent += img->vmargin;
24381 if (slice.y + slice.height == img->height)
24382 it->descent += img->vmargin;
24383 it->phys_descent = it->descent;
24385 it->pixel_width = slice.width;
24386 if (slice.x == 0)
24387 it->pixel_width += img->hmargin;
24388 if (slice.x + slice.width == img->width)
24389 it->pixel_width += img->hmargin;
24391 /* It's quite possible for images to have an ascent greater than
24392 their height, so don't get confused in that case. */
24393 if (it->descent < 0)
24394 it->descent = 0;
24396 it->nglyphs = 1;
24398 if (face->box != FACE_NO_BOX)
24400 if (face->box_line_width > 0)
24402 if (slice.y == 0)
24403 it->ascent += face->box_line_width;
24404 if (slice.y + slice.height == img->height)
24405 it->descent += face->box_line_width;
24408 if (it->start_of_box_run_p && slice.x == 0)
24409 it->pixel_width += eabs (face->box_line_width);
24410 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24411 it->pixel_width += eabs (face->box_line_width);
24414 take_vertical_position_into_account (it);
24416 /* Automatically crop wide image glyphs at right edge so we can
24417 draw the cursor on same display row. */
24418 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24419 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24421 it->pixel_width -= crop;
24422 slice.width -= crop;
24425 if (it->glyph_row)
24427 struct glyph *glyph;
24428 enum glyph_row_area area = it->area;
24430 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24431 if (glyph < it->glyph_row->glyphs[area + 1])
24433 glyph->charpos = CHARPOS (it->position);
24434 glyph->object = it->object;
24435 glyph->pixel_width = it->pixel_width;
24436 glyph->ascent = glyph_ascent;
24437 glyph->descent = it->descent;
24438 glyph->voffset = it->voffset;
24439 glyph->type = IMAGE_GLYPH;
24440 glyph->avoid_cursor_p = it->avoid_cursor_p;
24441 glyph->multibyte_p = it->multibyte_p;
24442 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24444 /* In R2L rows, the left and the right box edges need to be
24445 drawn in reverse direction. */
24446 glyph->right_box_line_p = it->start_of_box_run_p;
24447 glyph->left_box_line_p = it->end_of_box_run_p;
24449 else
24451 glyph->left_box_line_p = it->start_of_box_run_p;
24452 glyph->right_box_line_p = it->end_of_box_run_p;
24454 glyph->overlaps_vertically_p = 0;
24455 glyph->padding_p = 0;
24456 glyph->glyph_not_available_p = 0;
24457 glyph->face_id = it->face_id;
24458 glyph->u.img_id = img->id;
24459 glyph->slice.img = slice;
24460 glyph->font_type = FONT_TYPE_UNKNOWN;
24461 if (it->bidi_p)
24463 glyph->resolved_level = it->bidi_it.resolved_level;
24464 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24465 emacs_abort ();
24466 glyph->bidi_type = it->bidi_it.type;
24468 ++it->glyph_row->used[area];
24470 else
24471 IT_EXPAND_MATRIX_WIDTH (it, area);
24476 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24477 of the glyph, WIDTH and HEIGHT are the width and height of the
24478 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24480 static void
24481 append_stretch_glyph (struct it *it, Lisp_Object object,
24482 int width, int height, int ascent)
24484 struct glyph *glyph;
24485 enum glyph_row_area area = it->area;
24487 eassert (ascent >= 0 && ascent <= height);
24489 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24490 if (glyph < it->glyph_row->glyphs[area + 1])
24492 /* If the glyph row is reversed, we need to prepend the glyph
24493 rather than append it. */
24494 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24496 struct glyph *g;
24498 /* Make room for the additional glyph. */
24499 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24500 g[1] = *g;
24501 glyph = it->glyph_row->glyphs[area];
24503 glyph->charpos = CHARPOS (it->position);
24504 glyph->object = object;
24505 glyph->pixel_width = width;
24506 glyph->ascent = ascent;
24507 glyph->descent = height - ascent;
24508 glyph->voffset = it->voffset;
24509 glyph->type = STRETCH_GLYPH;
24510 glyph->avoid_cursor_p = it->avoid_cursor_p;
24511 glyph->multibyte_p = it->multibyte_p;
24512 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24514 /* In R2L rows, the left and the right box edges need to be
24515 drawn in reverse direction. */
24516 glyph->right_box_line_p = it->start_of_box_run_p;
24517 glyph->left_box_line_p = it->end_of_box_run_p;
24519 else
24521 glyph->left_box_line_p = it->start_of_box_run_p;
24522 glyph->right_box_line_p = it->end_of_box_run_p;
24524 glyph->overlaps_vertically_p = 0;
24525 glyph->padding_p = 0;
24526 glyph->glyph_not_available_p = 0;
24527 glyph->face_id = it->face_id;
24528 glyph->u.stretch.ascent = ascent;
24529 glyph->u.stretch.height = height;
24530 glyph->slice.img = null_glyph_slice;
24531 glyph->font_type = FONT_TYPE_UNKNOWN;
24532 if (it->bidi_p)
24534 glyph->resolved_level = it->bidi_it.resolved_level;
24535 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24536 emacs_abort ();
24537 glyph->bidi_type = it->bidi_it.type;
24539 else
24541 glyph->resolved_level = 0;
24542 glyph->bidi_type = UNKNOWN_BT;
24544 ++it->glyph_row->used[area];
24546 else
24547 IT_EXPAND_MATRIX_WIDTH (it, area);
24550 #endif /* HAVE_WINDOW_SYSTEM */
24552 /* Produce a stretch glyph for iterator IT. IT->object is the value
24553 of the glyph property displayed. The value must be a list
24554 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24555 being recognized:
24557 1. `:width WIDTH' specifies that the space should be WIDTH *
24558 canonical char width wide. WIDTH may be an integer or floating
24559 point number.
24561 2. `:relative-width FACTOR' specifies that the width of the stretch
24562 should be computed from the width of the first character having the
24563 `glyph' property, and should be FACTOR times that width.
24565 3. `:align-to HPOS' specifies that the space should be wide enough
24566 to reach HPOS, a value in canonical character units.
24568 Exactly one of the above pairs must be present.
24570 4. `:height HEIGHT' specifies that the height of the stretch produced
24571 should be HEIGHT, measured in canonical character units.
24573 5. `:relative-height FACTOR' specifies that the height of the
24574 stretch should be FACTOR times the height of the characters having
24575 the glyph property.
24577 Either none or exactly one of 4 or 5 must be present.
24579 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24580 of the stretch should be used for the ascent of the stretch.
24581 ASCENT must be in the range 0 <= ASCENT <= 100. */
24583 void
24584 produce_stretch_glyph (struct it *it)
24586 /* (space :width WIDTH :height HEIGHT ...) */
24587 Lisp_Object prop, plist;
24588 int width = 0, height = 0, align_to = -1;
24589 int zero_width_ok_p = 0;
24590 double tem;
24591 struct font *font = NULL;
24593 #ifdef HAVE_WINDOW_SYSTEM
24594 int ascent = 0;
24595 int zero_height_ok_p = 0;
24597 if (FRAME_WINDOW_P (it->f))
24599 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24600 font = face->font ? face->font : FRAME_FONT (it->f);
24601 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24603 #endif
24605 /* List should start with `space'. */
24606 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24607 plist = XCDR (it->object);
24609 /* Compute the width of the stretch. */
24610 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24611 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24613 /* Absolute width `:width WIDTH' specified and valid. */
24614 zero_width_ok_p = 1;
24615 width = (int)tem;
24617 #ifdef HAVE_WINDOW_SYSTEM
24618 else if (FRAME_WINDOW_P (it->f)
24619 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24621 /* Relative width `:relative-width FACTOR' specified and valid.
24622 Compute the width of the characters having the `glyph'
24623 property. */
24624 struct it it2;
24625 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24627 it2 = *it;
24628 if (it->multibyte_p)
24629 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24630 else
24632 it2.c = it2.char_to_display = *p, it2.len = 1;
24633 if (! ASCII_CHAR_P (it2.c))
24634 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24637 it2.glyph_row = NULL;
24638 it2.what = IT_CHARACTER;
24639 x_produce_glyphs (&it2);
24640 width = NUMVAL (prop) * it2.pixel_width;
24642 #endif /* HAVE_WINDOW_SYSTEM */
24643 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24644 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24646 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24647 align_to = (align_to < 0
24649 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24650 else if (align_to < 0)
24651 align_to = window_box_left_offset (it->w, TEXT_AREA);
24652 width = max (0, (int)tem + align_to - it->current_x);
24653 zero_width_ok_p = 1;
24655 else
24656 /* Nothing specified -> width defaults to canonical char width. */
24657 width = FRAME_COLUMN_WIDTH (it->f);
24659 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24660 width = 1;
24662 #ifdef HAVE_WINDOW_SYSTEM
24663 /* Compute height. */
24664 if (FRAME_WINDOW_P (it->f))
24666 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24667 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24669 height = (int)tem;
24670 zero_height_ok_p = 1;
24672 else if (prop = Fplist_get (plist, QCrelative_height),
24673 NUMVAL (prop) > 0)
24674 height = FONT_HEIGHT (font) * NUMVAL (prop);
24675 else
24676 height = FONT_HEIGHT (font);
24678 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24679 height = 1;
24681 /* Compute percentage of height used for ascent. If
24682 `:ascent ASCENT' is present and valid, use that. Otherwise,
24683 derive the ascent from the font in use. */
24684 if (prop = Fplist_get (plist, QCascent),
24685 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24686 ascent = height * NUMVAL (prop) / 100.0;
24687 else if (!NILP (prop)
24688 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24689 ascent = min (max (0, (int)tem), height);
24690 else
24691 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24693 else
24694 #endif /* HAVE_WINDOW_SYSTEM */
24695 height = 1;
24697 if (width > 0 && it->line_wrap != TRUNCATE
24698 && it->current_x + width > it->last_visible_x)
24700 width = it->last_visible_x - it->current_x;
24701 #ifdef HAVE_WINDOW_SYSTEM
24702 /* Subtract one more pixel from the stretch width, but only on
24703 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24704 width -= FRAME_WINDOW_P (it->f);
24705 #endif
24708 if (width > 0 && height > 0 && it->glyph_row)
24710 Lisp_Object o_object = it->object;
24711 Lisp_Object object = it->stack[it->sp - 1].string;
24712 int n = width;
24714 if (!STRINGP (object))
24715 object = it->w->contents;
24716 #ifdef HAVE_WINDOW_SYSTEM
24717 if (FRAME_WINDOW_P (it->f))
24718 append_stretch_glyph (it, object, width, height, ascent);
24719 else
24720 #endif
24722 it->object = object;
24723 it->char_to_display = ' ';
24724 it->pixel_width = it->len = 1;
24725 while (n--)
24726 tty_append_glyph (it);
24727 it->object = o_object;
24731 it->pixel_width = width;
24732 #ifdef HAVE_WINDOW_SYSTEM
24733 if (FRAME_WINDOW_P (it->f))
24735 it->ascent = it->phys_ascent = ascent;
24736 it->descent = it->phys_descent = height - it->ascent;
24737 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24738 take_vertical_position_into_account (it);
24740 else
24741 #endif
24742 it->nglyphs = width;
24745 /* Get information about special display element WHAT in an
24746 environment described by IT. WHAT is one of IT_TRUNCATION or
24747 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24748 non-null glyph_row member. This function ensures that fields like
24749 face_id, c, len of IT are left untouched. */
24751 static void
24752 produce_special_glyphs (struct it *it, enum display_element_type what)
24754 struct it temp_it;
24755 Lisp_Object gc;
24756 GLYPH glyph;
24758 temp_it = *it;
24759 temp_it.object = make_number (0);
24760 memset (&temp_it.current, 0, sizeof temp_it.current);
24762 if (what == IT_CONTINUATION)
24764 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24765 if (it->bidi_it.paragraph_dir == R2L)
24766 SET_GLYPH_FROM_CHAR (glyph, '/');
24767 else
24768 SET_GLYPH_FROM_CHAR (glyph, '\\');
24769 if (it->dp
24770 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24772 /* FIXME: Should we mirror GC for R2L lines? */
24773 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24774 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24777 else if (what == IT_TRUNCATION)
24779 /* Truncation glyph. */
24780 SET_GLYPH_FROM_CHAR (glyph, '$');
24781 if (it->dp
24782 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24784 /* FIXME: Should we mirror GC for R2L lines? */
24785 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24786 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24789 else
24790 emacs_abort ();
24792 #ifdef HAVE_WINDOW_SYSTEM
24793 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24794 is turned off, we precede the truncation/continuation glyphs by a
24795 stretch glyph whose width is computed such that these special
24796 glyphs are aligned at the window margin, even when very different
24797 fonts are used in different glyph rows. */
24798 if (FRAME_WINDOW_P (temp_it.f)
24799 /* init_iterator calls this with it->glyph_row == NULL, and it
24800 wants only the pixel width of the truncation/continuation
24801 glyphs. */
24802 && temp_it.glyph_row
24803 /* insert_left_trunc_glyphs calls us at the beginning of the
24804 row, and it has its own calculation of the stretch glyph
24805 width. */
24806 && temp_it.glyph_row->used[TEXT_AREA] > 0
24807 && (temp_it.glyph_row->reversed_p
24808 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24809 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24811 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24813 if (stretch_width > 0)
24815 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24816 struct font *font =
24817 face->font ? face->font : FRAME_FONT (temp_it.f);
24818 int stretch_ascent =
24819 (((temp_it.ascent + temp_it.descent)
24820 * FONT_BASE (font)) / FONT_HEIGHT (font));
24822 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24823 temp_it.ascent + temp_it.descent,
24824 stretch_ascent);
24827 #endif
24829 temp_it.dp = NULL;
24830 temp_it.what = IT_CHARACTER;
24831 temp_it.len = 1;
24832 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24833 temp_it.face_id = GLYPH_FACE (glyph);
24834 temp_it.len = CHAR_BYTES (temp_it.c);
24836 PRODUCE_GLYPHS (&temp_it);
24837 it->pixel_width = temp_it.pixel_width;
24838 it->nglyphs = temp_it.pixel_width;
24841 #ifdef HAVE_WINDOW_SYSTEM
24843 /* Calculate line-height and line-spacing properties.
24844 An integer value specifies explicit pixel value.
24845 A float value specifies relative value to current face height.
24846 A cons (float . face-name) specifies relative value to
24847 height of specified face font.
24849 Returns height in pixels, or nil. */
24852 static Lisp_Object
24853 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24854 int boff, int override)
24856 Lisp_Object face_name = Qnil;
24857 int ascent, descent, height;
24859 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24860 return val;
24862 if (CONSP (val))
24864 face_name = XCAR (val);
24865 val = XCDR (val);
24866 if (!NUMBERP (val))
24867 val = make_number (1);
24868 if (NILP (face_name))
24870 height = it->ascent + it->descent;
24871 goto scale;
24875 if (NILP (face_name))
24877 font = FRAME_FONT (it->f);
24878 boff = FRAME_BASELINE_OFFSET (it->f);
24880 else if (EQ (face_name, Qt))
24882 override = 0;
24884 else
24886 int face_id;
24887 struct face *face;
24889 face_id = lookup_named_face (it->f, face_name, 0);
24890 if (face_id < 0)
24891 return make_number (-1);
24893 face = FACE_FROM_ID (it->f, face_id);
24894 font = face->font;
24895 if (font == NULL)
24896 return make_number (-1);
24897 boff = font->baseline_offset;
24898 if (font->vertical_centering)
24899 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24902 ascent = FONT_BASE (font) + boff;
24903 descent = FONT_DESCENT (font) - boff;
24905 if (override)
24907 it->override_ascent = ascent;
24908 it->override_descent = descent;
24909 it->override_boff = boff;
24912 height = ascent + descent;
24914 scale:
24915 if (FLOATP (val))
24916 height = (int)(XFLOAT_DATA (val) * height);
24917 else if (INTEGERP (val))
24918 height *= XINT (val);
24920 return make_number (height);
24924 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24925 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24926 and only if this is for a character for which no font was found.
24928 If the display method (it->glyphless_method) is
24929 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24930 length of the acronym or the hexadecimal string, UPPER_XOFF and
24931 UPPER_YOFF are pixel offsets for the upper part of the string,
24932 LOWER_XOFF and LOWER_YOFF are for the lower part.
24934 For the other display methods, LEN through LOWER_YOFF are zero. */
24936 static void
24937 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24938 short upper_xoff, short upper_yoff,
24939 short lower_xoff, short lower_yoff)
24941 struct glyph *glyph;
24942 enum glyph_row_area area = it->area;
24944 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24945 if (glyph < it->glyph_row->glyphs[area + 1])
24947 /* If the glyph row is reversed, we need to prepend the glyph
24948 rather than append it. */
24949 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24951 struct glyph *g;
24953 /* Make room for the additional glyph. */
24954 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24955 g[1] = *g;
24956 glyph = it->glyph_row->glyphs[area];
24958 glyph->charpos = CHARPOS (it->position);
24959 glyph->object = it->object;
24960 glyph->pixel_width = it->pixel_width;
24961 glyph->ascent = it->ascent;
24962 glyph->descent = it->descent;
24963 glyph->voffset = it->voffset;
24964 glyph->type = GLYPHLESS_GLYPH;
24965 glyph->u.glyphless.method = it->glyphless_method;
24966 glyph->u.glyphless.for_no_font = for_no_font;
24967 glyph->u.glyphless.len = len;
24968 glyph->u.glyphless.ch = it->c;
24969 glyph->slice.glyphless.upper_xoff = upper_xoff;
24970 glyph->slice.glyphless.upper_yoff = upper_yoff;
24971 glyph->slice.glyphless.lower_xoff = lower_xoff;
24972 glyph->slice.glyphless.lower_yoff = lower_yoff;
24973 glyph->avoid_cursor_p = it->avoid_cursor_p;
24974 glyph->multibyte_p = it->multibyte_p;
24975 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24977 /* In R2L rows, the left and the right box edges need to be
24978 drawn in reverse direction. */
24979 glyph->right_box_line_p = it->start_of_box_run_p;
24980 glyph->left_box_line_p = it->end_of_box_run_p;
24982 else
24984 glyph->left_box_line_p = it->start_of_box_run_p;
24985 glyph->right_box_line_p = it->end_of_box_run_p;
24987 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24988 || it->phys_descent > it->descent);
24989 glyph->padding_p = 0;
24990 glyph->glyph_not_available_p = 0;
24991 glyph->face_id = face_id;
24992 glyph->font_type = FONT_TYPE_UNKNOWN;
24993 if (it->bidi_p)
24995 glyph->resolved_level = it->bidi_it.resolved_level;
24996 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24997 emacs_abort ();
24998 glyph->bidi_type = it->bidi_it.type;
25000 ++it->glyph_row->used[area];
25002 else
25003 IT_EXPAND_MATRIX_WIDTH (it, area);
25007 /* Produce a glyph for a glyphless character for iterator IT.
25008 IT->glyphless_method specifies which method to use for displaying
25009 the character. See the description of enum
25010 glyphless_display_method in dispextern.h for the detail.
25012 FOR_NO_FONT is nonzero if and only if this is for a character for
25013 which no font was found. ACRONYM, if non-nil, is an acronym string
25014 for the character. */
25016 static void
25017 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25019 int face_id;
25020 struct face *face;
25021 struct font *font;
25022 int base_width, base_height, width, height;
25023 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25024 int len;
25026 /* Get the metrics of the base font. We always refer to the current
25027 ASCII face. */
25028 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25029 font = face->font ? face->font : FRAME_FONT (it->f);
25030 it->ascent = FONT_BASE (font) + font->baseline_offset;
25031 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25032 base_height = it->ascent + it->descent;
25033 base_width = font->average_width;
25035 /* Get a face ID for the glyph by utilizing a cache (the same way as
25036 done for `escape-glyph' in get_next_display_element). */
25037 if (it->f == last_glyphless_glyph_frame
25038 && it->face_id == last_glyphless_glyph_face_id)
25040 face_id = last_glyphless_glyph_merged_face_id;
25042 else
25044 /* Merge the `glyphless-char' face into the current face. */
25045 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
25046 last_glyphless_glyph_frame = it->f;
25047 last_glyphless_glyph_face_id = it->face_id;
25048 last_glyphless_glyph_merged_face_id = face_id;
25051 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25053 it->pixel_width = THIN_SPACE_WIDTH;
25054 len = 0;
25055 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25057 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25059 width = CHAR_WIDTH (it->c);
25060 if (width == 0)
25061 width = 1;
25062 else if (width > 4)
25063 width = 4;
25064 it->pixel_width = base_width * width;
25065 len = 0;
25066 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25068 else
25070 char buf[7];
25071 const char *str;
25072 unsigned int code[6];
25073 int upper_len;
25074 int ascent, descent;
25075 struct font_metrics metrics_upper, metrics_lower;
25077 face = FACE_FROM_ID (it->f, face_id);
25078 font = face->font ? face->font : FRAME_FONT (it->f);
25079 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25081 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25083 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25084 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25085 if (CONSP (acronym))
25086 acronym = XCAR (acronym);
25087 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25089 else
25091 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25092 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25093 str = buf;
25095 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25096 code[len] = font->driver->encode_char (font, str[len]);
25097 upper_len = (len + 1) / 2;
25098 font->driver->text_extents (font, code, upper_len,
25099 &metrics_upper);
25100 font->driver->text_extents (font, code + upper_len, len - upper_len,
25101 &metrics_lower);
25105 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25106 width = max (metrics_upper.width, metrics_lower.width) + 4;
25107 upper_xoff = upper_yoff = 2; /* the typical case */
25108 if (base_width >= width)
25110 /* Align the upper to the left, the lower to the right. */
25111 it->pixel_width = base_width;
25112 lower_xoff = base_width - 2 - metrics_lower.width;
25114 else
25116 /* Center the shorter one. */
25117 it->pixel_width = width;
25118 if (metrics_upper.width >= metrics_lower.width)
25119 lower_xoff = (width - metrics_lower.width) / 2;
25120 else
25122 /* FIXME: This code doesn't look right. It formerly was
25123 missing the "lower_xoff = 0;", which couldn't have
25124 been right since it left lower_xoff uninitialized. */
25125 lower_xoff = 0;
25126 upper_xoff = (width - metrics_upper.width) / 2;
25130 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25131 top, bottom, and between upper and lower strings. */
25132 height = (metrics_upper.ascent + metrics_upper.descent
25133 + metrics_lower.ascent + metrics_lower.descent) + 5;
25134 /* Center vertically.
25135 H:base_height, D:base_descent
25136 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25138 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25139 descent = D - H/2 + h/2;
25140 lower_yoff = descent - 2 - ld;
25141 upper_yoff = lower_yoff - la - 1 - ud; */
25142 ascent = - (it->descent - (base_height + height + 1) / 2);
25143 descent = it->descent - (base_height - height) / 2;
25144 lower_yoff = descent - 2 - metrics_lower.descent;
25145 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25146 - metrics_upper.descent);
25147 /* Don't make the height shorter than the base height. */
25148 if (height > base_height)
25150 it->ascent = ascent;
25151 it->descent = descent;
25155 it->phys_ascent = it->ascent;
25156 it->phys_descent = it->descent;
25157 if (it->glyph_row)
25158 append_glyphless_glyph (it, face_id, for_no_font, len,
25159 upper_xoff, upper_yoff,
25160 lower_xoff, lower_yoff);
25161 it->nglyphs = 1;
25162 take_vertical_position_into_account (it);
25166 /* RIF:
25167 Produce glyphs/get display metrics for the display element IT is
25168 loaded with. See the description of struct it in dispextern.h
25169 for an overview of struct it. */
25171 void
25172 x_produce_glyphs (struct it *it)
25174 int extra_line_spacing = it->extra_line_spacing;
25176 it->glyph_not_available_p = 0;
25178 if (it->what == IT_CHARACTER)
25180 XChar2b char2b;
25181 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25182 struct font *font = face->font;
25183 struct font_metrics *pcm = NULL;
25184 int boff; /* baseline offset */
25186 if (font == NULL)
25188 /* When no suitable font is found, display this character by
25189 the method specified in the first extra slot of
25190 Vglyphless_char_display. */
25191 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25193 eassert (it->what == IT_GLYPHLESS);
25194 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25195 goto done;
25198 boff = font->baseline_offset;
25199 if (font->vertical_centering)
25200 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25202 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25204 int stretched_p;
25206 it->nglyphs = 1;
25208 if (it->override_ascent >= 0)
25210 it->ascent = it->override_ascent;
25211 it->descent = it->override_descent;
25212 boff = it->override_boff;
25214 else
25216 it->ascent = FONT_BASE (font) + boff;
25217 it->descent = FONT_DESCENT (font) - boff;
25220 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25222 pcm = get_per_char_metric (font, &char2b);
25223 if (pcm->width == 0
25224 && pcm->rbearing == 0 && pcm->lbearing == 0)
25225 pcm = NULL;
25228 if (pcm)
25230 it->phys_ascent = pcm->ascent + boff;
25231 it->phys_descent = pcm->descent - boff;
25232 it->pixel_width = pcm->width;
25234 else
25236 it->glyph_not_available_p = 1;
25237 it->phys_ascent = it->ascent;
25238 it->phys_descent = it->descent;
25239 it->pixel_width = font->space_width;
25242 if (it->constrain_row_ascent_descent_p)
25244 if (it->descent > it->max_descent)
25246 it->ascent += it->descent - it->max_descent;
25247 it->descent = it->max_descent;
25249 if (it->ascent > it->max_ascent)
25251 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25252 it->ascent = it->max_ascent;
25254 it->phys_ascent = min (it->phys_ascent, it->ascent);
25255 it->phys_descent = min (it->phys_descent, it->descent);
25256 extra_line_spacing = 0;
25259 /* If this is a space inside a region of text with
25260 `space-width' property, change its width. */
25261 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25262 if (stretched_p)
25263 it->pixel_width *= XFLOATINT (it->space_width);
25265 /* If face has a box, add the box thickness to the character
25266 height. If character has a box line to the left and/or
25267 right, add the box line width to the character's width. */
25268 if (face->box != FACE_NO_BOX)
25270 int thick = face->box_line_width;
25272 if (thick > 0)
25274 it->ascent += thick;
25275 it->descent += thick;
25277 else
25278 thick = -thick;
25280 if (it->start_of_box_run_p)
25281 it->pixel_width += thick;
25282 if (it->end_of_box_run_p)
25283 it->pixel_width += thick;
25286 /* If face has an overline, add the height of the overline
25287 (1 pixel) and a 1 pixel margin to the character height. */
25288 if (face->overline_p)
25289 it->ascent += overline_margin;
25291 if (it->constrain_row_ascent_descent_p)
25293 if (it->ascent > it->max_ascent)
25294 it->ascent = it->max_ascent;
25295 if (it->descent > it->max_descent)
25296 it->descent = it->max_descent;
25299 take_vertical_position_into_account (it);
25301 /* If we have to actually produce glyphs, do it. */
25302 if (it->glyph_row)
25304 if (stretched_p)
25306 /* Translate a space with a `space-width' property
25307 into a stretch glyph. */
25308 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25309 / FONT_HEIGHT (font));
25310 append_stretch_glyph (it, it->object, it->pixel_width,
25311 it->ascent + it->descent, ascent);
25313 else
25314 append_glyph (it);
25316 /* If characters with lbearing or rbearing are displayed
25317 in this line, record that fact in a flag of the
25318 glyph row. This is used to optimize X output code. */
25319 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25320 it->glyph_row->contains_overlapping_glyphs_p = 1;
25322 if (! stretched_p && it->pixel_width == 0)
25323 /* We assure that all visible glyphs have at least 1-pixel
25324 width. */
25325 it->pixel_width = 1;
25327 else if (it->char_to_display == '\n')
25329 /* A newline has no width, but we need the height of the
25330 line. But if previous part of the line sets a height,
25331 don't increase that height */
25333 Lisp_Object height;
25334 Lisp_Object total_height = Qnil;
25336 it->override_ascent = -1;
25337 it->pixel_width = 0;
25338 it->nglyphs = 0;
25340 height = get_it_property (it, Qline_height);
25341 /* Split (line-height total-height) list */
25342 if (CONSP (height)
25343 && CONSP (XCDR (height))
25344 && NILP (XCDR (XCDR (height))))
25346 total_height = XCAR (XCDR (height));
25347 height = XCAR (height);
25349 height = calc_line_height_property (it, height, font, boff, 1);
25351 if (it->override_ascent >= 0)
25353 it->ascent = it->override_ascent;
25354 it->descent = it->override_descent;
25355 boff = it->override_boff;
25357 else
25359 it->ascent = FONT_BASE (font) + boff;
25360 it->descent = FONT_DESCENT (font) - boff;
25363 if (EQ (height, Qt))
25365 if (it->descent > it->max_descent)
25367 it->ascent += it->descent - it->max_descent;
25368 it->descent = it->max_descent;
25370 if (it->ascent > it->max_ascent)
25372 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25373 it->ascent = it->max_ascent;
25375 it->phys_ascent = min (it->phys_ascent, it->ascent);
25376 it->phys_descent = min (it->phys_descent, it->descent);
25377 it->constrain_row_ascent_descent_p = 1;
25378 extra_line_spacing = 0;
25380 else
25382 Lisp_Object spacing;
25384 it->phys_ascent = it->ascent;
25385 it->phys_descent = it->descent;
25387 if ((it->max_ascent > 0 || it->max_descent > 0)
25388 && face->box != FACE_NO_BOX
25389 && face->box_line_width > 0)
25391 it->ascent += face->box_line_width;
25392 it->descent += face->box_line_width;
25394 if (!NILP (height)
25395 && XINT (height) > it->ascent + it->descent)
25396 it->ascent = XINT (height) - it->descent;
25398 if (!NILP (total_height))
25399 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25400 else
25402 spacing = get_it_property (it, Qline_spacing);
25403 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25405 if (INTEGERP (spacing))
25407 extra_line_spacing = XINT (spacing);
25408 if (!NILP (total_height))
25409 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25413 else /* i.e. (it->char_to_display == '\t') */
25415 if (font->space_width > 0)
25417 int tab_width = it->tab_width * font->space_width;
25418 int x = it->current_x + it->continuation_lines_width;
25419 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25421 /* If the distance from the current position to the next tab
25422 stop is less than a space character width, use the
25423 tab stop after that. */
25424 if (next_tab_x - x < font->space_width)
25425 next_tab_x += tab_width;
25427 it->pixel_width = next_tab_x - x;
25428 it->nglyphs = 1;
25429 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25430 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25432 if (it->glyph_row)
25434 append_stretch_glyph (it, it->object, it->pixel_width,
25435 it->ascent + it->descent, it->ascent);
25438 else
25440 it->pixel_width = 0;
25441 it->nglyphs = 1;
25445 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25447 /* A static composition.
25449 Note: A composition is represented as one glyph in the
25450 glyph matrix. There are no padding glyphs.
25452 Important note: pixel_width, ascent, and descent are the
25453 values of what is drawn by draw_glyphs (i.e. the values of
25454 the overall glyphs composed). */
25455 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25456 int boff; /* baseline offset */
25457 struct composition *cmp = composition_table[it->cmp_it.id];
25458 int glyph_len = cmp->glyph_len;
25459 struct font *font = face->font;
25461 it->nglyphs = 1;
25463 /* If we have not yet calculated pixel size data of glyphs of
25464 the composition for the current face font, calculate them
25465 now. Theoretically, we have to check all fonts for the
25466 glyphs, but that requires much time and memory space. So,
25467 here we check only the font of the first glyph. This may
25468 lead to incorrect display, but it's very rare, and C-l
25469 (recenter-top-bottom) can correct the display anyway. */
25470 if (! cmp->font || cmp->font != font)
25472 /* Ascent and descent of the font of the first character
25473 of this composition (adjusted by baseline offset).
25474 Ascent and descent of overall glyphs should not be less
25475 than these, respectively. */
25476 int font_ascent, font_descent, font_height;
25477 /* Bounding box of the overall glyphs. */
25478 int leftmost, rightmost, lowest, highest;
25479 int lbearing, rbearing;
25480 int i, width, ascent, descent;
25481 int left_padded = 0, right_padded = 0;
25482 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25483 XChar2b char2b;
25484 struct font_metrics *pcm;
25485 int font_not_found_p;
25486 ptrdiff_t pos;
25488 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25489 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25490 break;
25491 if (glyph_len < cmp->glyph_len)
25492 right_padded = 1;
25493 for (i = 0; i < glyph_len; i++)
25495 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25496 break;
25497 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25499 if (i > 0)
25500 left_padded = 1;
25502 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25503 : IT_CHARPOS (*it));
25504 /* If no suitable font is found, use the default font. */
25505 font_not_found_p = font == NULL;
25506 if (font_not_found_p)
25508 face = face->ascii_face;
25509 font = face->font;
25511 boff = font->baseline_offset;
25512 if (font->vertical_centering)
25513 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25514 font_ascent = FONT_BASE (font) + boff;
25515 font_descent = FONT_DESCENT (font) - boff;
25516 font_height = FONT_HEIGHT (font);
25518 cmp->font = font;
25520 pcm = NULL;
25521 if (! font_not_found_p)
25523 get_char_face_and_encoding (it->f, c, it->face_id,
25524 &char2b, 0);
25525 pcm = get_per_char_metric (font, &char2b);
25528 /* Initialize the bounding box. */
25529 if (pcm)
25531 width = cmp->glyph_len > 0 ? pcm->width : 0;
25532 ascent = pcm->ascent;
25533 descent = pcm->descent;
25534 lbearing = pcm->lbearing;
25535 rbearing = pcm->rbearing;
25537 else
25539 width = cmp->glyph_len > 0 ? font->space_width : 0;
25540 ascent = FONT_BASE (font);
25541 descent = FONT_DESCENT (font);
25542 lbearing = 0;
25543 rbearing = width;
25546 rightmost = width;
25547 leftmost = 0;
25548 lowest = - descent + boff;
25549 highest = ascent + boff;
25551 if (! font_not_found_p
25552 && font->default_ascent
25553 && CHAR_TABLE_P (Vuse_default_ascent)
25554 && !NILP (Faref (Vuse_default_ascent,
25555 make_number (it->char_to_display))))
25556 highest = font->default_ascent + boff;
25558 /* Draw the first glyph at the normal position. It may be
25559 shifted to right later if some other glyphs are drawn
25560 at the left. */
25561 cmp->offsets[i * 2] = 0;
25562 cmp->offsets[i * 2 + 1] = boff;
25563 cmp->lbearing = lbearing;
25564 cmp->rbearing = rbearing;
25566 /* Set cmp->offsets for the remaining glyphs. */
25567 for (i++; i < glyph_len; i++)
25569 int left, right, btm, top;
25570 int ch = COMPOSITION_GLYPH (cmp, i);
25571 int face_id;
25572 struct face *this_face;
25574 if (ch == '\t')
25575 ch = ' ';
25576 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25577 this_face = FACE_FROM_ID (it->f, face_id);
25578 font = this_face->font;
25580 if (font == NULL)
25581 pcm = NULL;
25582 else
25584 get_char_face_and_encoding (it->f, ch, face_id,
25585 &char2b, 0);
25586 pcm = get_per_char_metric (font, &char2b);
25588 if (! pcm)
25589 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25590 else
25592 width = pcm->width;
25593 ascent = pcm->ascent;
25594 descent = pcm->descent;
25595 lbearing = pcm->lbearing;
25596 rbearing = pcm->rbearing;
25597 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25599 /* Relative composition with or without
25600 alternate chars. */
25601 left = (leftmost + rightmost - width) / 2;
25602 btm = - descent + boff;
25603 if (font->relative_compose
25604 && (! CHAR_TABLE_P (Vignore_relative_composition)
25605 || NILP (Faref (Vignore_relative_composition,
25606 make_number (ch)))))
25609 if (- descent >= font->relative_compose)
25610 /* One extra pixel between two glyphs. */
25611 btm = highest + 1;
25612 else if (ascent <= 0)
25613 /* One extra pixel between two glyphs. */
25614 btm = lowest - 1 - ascent - descent;
25617 else
25619 /* A composition rule is specified by an integer
25620 value that encodes global and new reference
25621 points (GREF and NREF). GREF and NREF are
25622 specified by numbers as below:
25624 0---1---2 -- ascent
25628 9--10--11 -- center
25630 ---3---4---5--- baseline
25632 6---7---8 -- descent
25634 int rule = COMPOSITION_RULE (cmp, i);
25635 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25637 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25638 grefx = gref % 3, nrefx = nref % 3;
25639 grefy = gref / 3, nrefy = nref / 3;
25640 if (xoff)
25641 xoff = font_height * (xoff - 128) / 256;
25642 if (yoff)
25643 yoff = font_height * (yoff - 128) / 256;
25645 left = (leftmost
25646 + grefx * (rightmost - leftmost) / 2
25647 - nrefx * width / 2
25648 + xoff);
25650 btm = ((grefy == 0 ? highest
25651 : grefy == 1 ? 0
25652 : grefy == 2 ? lowest
25653 : (highest + lowest) / 2)
25654 - (nrefy == 0 ? ascent + descent
25655 : nrefy == 1 ? descent - boff
25656 : nrefy == 2 ? 0
25657 : (ascent + descent) / 2)
25658 + yoff);
25661 cmp->offsets[i * 2] = left;
25662 cmp->offsets[i * 2 + 1] = btm + descent;
25664 /* Update the bounding box of the overall glyphs. */
25665 if (width > 0)
25667 right = left + width;
25668 if (left < leftmost)
25669 leftmost = left;
25670 if (right > rightmost)
25671 rightmost = right;
25673 top = btm + descent + ascent;
25674 if (top > highest)
25675 highest = top;
25676 if (btm < lowest)
25677 lowest = btm;
25679 if (cmp->lbearing > left + lbearing)
25680 cmp->lbearing = left + lbearing;
25681 if (cmp->rbearing < left + rbearing)
25682 cmp->rbearing = left + rbearing;
25686 /* If there are glyphs whose x-offsets are negative,
25687 shift all glyphs to the right and make all x-offsets
25688 non-negative. */
25689 if (leftmost < 0)
25691 for (i = 0; i < cmp->glyph_len; i++)
25692 cmp->offsets[i * 2] -= leftmost;
25693 rightmost -= leftmost;
25694 cmp->lbearing -= leftmost;
25695 cmp->rbearing -= leftmost;
25698 if (left_padded && cmp->lbearing < 0)
25700 for (i = 0; i < cmp->glyph_len; i++)
25701 cmp->offsets[i * 2] -= cmp->lbearing;
25702 rightmost -= cmp->lbearing;
25703 cmp->rbearing -= cmp->lbearing;
25704 cmp->lbearing = 0;
25706 if (right_padded && rightmost < cmp->rbearing)
25708 rightmost = cmp->rbearing;
25711 cmp->pixel_width = rightmost;
25712 cmp->ascent = highest;
25713 cmp->descent = - lowest;
25714 if (cmp->ascent < font_ascent)
25715 cmp->ascent = font_ascent;
25716 if (cmp->descent < font_descent)
25717 cmp->descent = font_descent;
25720 if (it->glyph_row
25721 && (cmp->lbearing < 0
25722 || cmp->rbearing > cmp->pixel_width))
25723 it->glyph_row->contains_overlapping_glyphs_p = 1;
25725 it->pixel_width = cmp->pixel_width;
25726 it->ascent = it->phys_ascent = cmp->ascent;
25727 it->descent = it->phys_descent = cmp->descent;
25728 if (face->box != FACE_NO_BOX)
25730 int thick = face->box_line_width;
25732 if (thick > 0)
25734 it->ascent += thick;
25735 it->descent += thick;
25737 else
25738 thick = - thick;
25740 if (it->start_of_box_run_p)
25741 it->pixel_width += thick;
25742 if (it->end_of_box_run_p)
25743 it->pixel_width += thick;
25746 /* If face has an overline, add the height of the overline
25747 (1 pixel) and a 1 pixel margin to the character height. */
25748 if (face->overline_p)
25749 it->ascent += overline_margin;
25751 take_vertical_position_into_account (it);
25752 if (it->ascent < 0)
25753 it->ascent = 0;
25754 if (it->descent < 0)
25755 it->descent = 0;
25757 if (it->glyph_row && cmp->glyph_len > 0)
25758 append_composite_glyph (it);
25760 else if (it->what == IT_COMPOSITION)
25762 /* A dynamic (automatic) composition. */
25763 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25764 Lisp_Object gstring;
25765 struct font_metrics metrics;
25767 it->nglyphs = 1;
25769 gstring = composition_gstring_from_id (it->cmp_it.id);
25770 it->pixel_width
25771 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25772 &metrics);
25773 if (it->glyph_row
25774 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25775 it->glyph_row->contains_overlapping_glyphs_p = 1;
25776 it->ascent = it->phys_ascent = metrics.ascent;
25777 it->descent = it->phys_descent = metrics.descent;
25778 if (face->box != FACE_NO_BOX)
25780 int thick = face->box_line_width;
25782 if (thick > 0)
25784 it->ascent += thick;
25785 it->descent += thick;
25787 else
25788 thick = - thick;
25790 if (it->start_of_box_run_p)
25791 it->pixel_width += thick;
25792 if (it->end_of_box_run_p)
25793 it->pixel_width += thick;
25795 /* If face has an overline, add the height of the overline
25796 (1 pixel) and a 1 pixel margin to the character height. */
25797 if (face->overline_p)
25798 it->ascent += overline_margin;
25799 take_vertical_position_into_account (it);
25800 if (it->ascent < 0)
25801 it->ascent = 0;
25802 if (it->descent < 0)
25803 it->descent = 0;
25805 if (it->glyph_row)
25806 append_composite_glyph (it);
25808 else if (it->what == IT_GLYPHLESS)
25809 produce_glyphless_glyph (it, 0, Qnil);
25810 else if (it->what == IT_IMAGE)
25811 produce_image_glyph (it);
25812 else if (it->what == IT_STRETCH)
25813 produce_stretch_glyph (it);
25815 done:
25816 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25817 because this isn't true for images with `:ascent 100'. */
25818 eassert (it->ascent >= 0 && it->descent >= 0);
25819 if (it->area == TEXT_AREA)
25820 it->current_x += it->pixel_width;
25822 if (extra_line_spacing > 0)
25824 it->descent += extra_line_spacing;
25825 if (extra_line_spacing > it->max_extra_line_spacing)
25826 it->max_extra_line_spacing = extra_line_spacing;
25829 it->max_ascent = max (it->max_ascent, it->ascent);
25830 it->max_descent = max (it->max_descent, it->descent);
25831 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25832 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25835 /* EXPORT for RIF:
25836 Output LEN glyphs starting at START at the nominal cursor position.
25837 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25838 being updated, and UPDATED_AREA is the area of that row being updated. */
25840 void
25841 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25842 struct glyph *start, enum glyph_row_area updated_area, int len)
25844 int x, hpos, chpos = w->phys_cursor.hpos;
25846 eassert (updated_row);
25847 /* When the window is hscrolled, cursor hpos can legitimately be out
25848 of bounds, but we draw the cursor at the corresponding window
25849 margin in that case. */
25850 if (!updated_row->reversed_p && chpos < 0)
25851 chpos = 0;
25852 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25853 chpos = updated_row->used[TEXT_AREA] - 1;
25855 block_input ();
25857 /* Write glyphs. */
25859 hpos = start - updated_row->glyphs[updated_area];
25860 x = draw_glyphs (w, w->output_cursor.x,
25861 updated_row, updated_area,
25862 hpos, hpos + len,
25863 DRAW_NORMAL_TEXT, 0);
25865 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25866 if (updated_area == TEXT_AREA
25867 && w->phys_cursor_on_p
25868 && w->phys_cursor.vpos == w->output_cursor.vpos
25869 && chpos >= hpos
25870 && chpos < hpos + len)
25871 w->phys_cursor_on_p = 0;
25873 unblock_input ();
25875 /* Advance the output cursor. */
25876 w->output_cursor.hpos += len;
25877 w->output_cursor.x = x;
25881 /* EXPORT for RIF:
25882 Insert LEN glyphs from START at the nominal cursor position. */
25884 void
25885 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25886 struct glyph *start, enum glyph_row_area updated_area, int len)
25888 struct frame *f;
25889 int line_height, shift_by_width, shifted_region_width;
25890 struct glyph_row *row;
25891 struct glyph *glyph;
25892 int frame_x, frame_y;
25893 ptrdiff_t hpos;
25895 eassert (updated_row);
25896 block_input ();
25897 f = XFRAME (WINDOW_FRAME (w));
25899 /* Get the height of the line we are in. */
25900 row = updated_row;
25901 line_height = row->height;
25903 /* Get the width of the glyphs to insert. */
25904 shift_by_width = 0;
25905 for (glyph = start; glyph < start + len; ++glyph)
25906 shift_by_width += glyph->pixel_width;
25908 /* Get the width of the region to shift right. */
25909 shifted_region_width = (window_box_width (w, updated_area)
25910 - w->output_cursor.x
25911 - shift_by_width);
25913 /* Shift right. */
25914 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25915 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25917 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25918 line_height, shift_by_width);
25920 /* Write the glyphs. */
25921 hpos = start - row->glyphs[updated_area];
25922 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25923 hpos, hpos + len,
25924 DRAW_NORMAL_TEXT, 0);
25926 /* Advance the output cursor. */
25927 w->output_cursor.hpos += len;
25928 w->output_cursor.x += shift_by_width;
25929 unblock_input ();
25933 /* EXPORT for RIF:
25934 Erase the current text line from the nominal cursor position
25935 (inclusive) to pixel column TO_X (exclusive). The idea is that
25936 everything from TO_X onward is already erased.
25938 TO_X is a pixel position relative to UPDATED_AREA of currently
25939 updated window W. TO_X == -1 means clear to the end of this area. */
25941 void
25942 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25943 enum glyph_row_area updated_area, int to_x)
25945 struct frame *f;
25946 int max_x, min_y, max_y;
25947 int from_x, from_y, to_y;
25949 eassert (updated_row);
25950 f = XFRAME (w->frame);
25952 if (updated_row->full_width_p)
25953 max_x = WINDOW_TOTAL_WIDTH (w);
25954 else
25955 max_x = window_box_width (w, updated_area);
25956 max_y = window_text_bottom_y (w);
25958 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25959 of window. For TO_X > 0, truncate to end of drawing area. */
25960 if (to_x == 0)
25961 return;
25962 else if (to_x < 0)
25963 to_x = max_x;
25964 else
25965 to_x = min (to_x, max_x);
25967 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25969 /* Notice if the cursor will be cleared by this operation. */
25970 if (!updated_row->full_width_p)
25971 notice_overwritten_cursor (w, updated_area,
25972 w->output_cursor.x, -1,
25973 updated_row->y,
25974 MATRIX_ROW_BOTTOM_Y (updated_row));
25976 from_x = w->output_cursor.x;
25978 /* Translate to frame coordinates. */
25979 if (updated_row->full_width_p)
25981 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25982 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25984 else
25986 int area_left = window_box_left (w, updated_area);
25987 from_x += area_left;
25988 to_x += area_left;
25991 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25992 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25993 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25995 /* Prevent inadvertently clearing to end of the X window. */
25996 if (to_x > from_x && to_y > from_y)
25998 block_input ();
25999 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26000 to_x - from_x, to_y - from_y);
26001 unblock_input ();
26005 #endif /* HAVE_WINDOW_SYSTEM */
26009 /***********************************************************************
26010 Cursor types
26011 ***********************************************************************/
26013 /* Value is the internal representation of the specified cursor type
26014 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26015 of the bar cursor. */
26017 static enum text_cursor_kinds
26018 get_specified_cursor_type (Lisp_Object arg, int *width)
26020 enum text_cursor_kinds type;
26022 if (NILP (arg))
26023 return NO_CURSOR;
26025 if (EQ (arg, Qbox))
26026 return FILLED_BOX_CURSOR;
26028 if (EQ (arg, Qhollow))
26029 return HOLLOW_BOX_CURSOR;
26031 if (EQ (arg, Qbar))
26033 *width = 2;
26034 return BAR_CURSOR;
26037 if (CONSP (arg)
26038 && EQ (XCAR (arg), Qbar)
26039 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26041 *width = XINT (XCDR (arg));
26042 return BAR_CURSOR;
26045 if (EQ (arg, Qhbar))
26047 *width = 2;
26048 return HBAR_CURSOR;
26051 if (CONSP (arg)
26052 && EQ (XCAR (arg), Qhbar)
26053 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26055 *width = XINT (XCDR (arg));
26056 return HBAR_CURSOR;
26059 /* Treat anything unknown as "hollow box cursor".
26060 It was bad to signal an error; people have trouble fixing
26061 .Xdefaults with Emacs, when it has something bad in it. */
26062 type = HOLLOW_BOX_CURSOR;
26064 return type;
26067 /* Set the default cursor types for specified frame. */
26068 void
26069 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26071 int width = 1;
26072 Lisp_Object tem;
26074 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26075 FRAME_CURSOR_WIDTH (f) = width;
26077 /* By default, set up the blink-off state depending on the on-state. */
26079 tem = Fassoc (arg, Vblink_cursor_alist);
26080 if (!NILP (tem))
26082 FRAME_BLINK_OFF_CURSOR (f)
26083 = get_specified_cursor_type (XCDR (tem), &width);
26084 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26086 else
26087 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26089 /* Make sure the cursor gets redrawn. */
26090 cursor_type_changed = 1;
26094 #ifdef HAVE_WINDOW_SYSTEM
26096 /* Return the cursor we want to be displayed in window W. Return
26097 width of bar/hbar cursor through WIDTH arg. Return with
26098 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26099 (i.e. if the `system caret' should track this cursor).
26101 In a mini-buffer window, we want the cursor only to appear if we
26102 are reading input from this window. For the selected window, we
26103 want the cursor type given by the frame parameter or buffer local
26104 setting of cursor-type. If explicitly marked off, draw no cursor.
26105 In all other cases, we want a hollow box cursor. */
26107 static enum text_cursor_kinds
26108 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26109 int *active_cursor)
26111 struct frame *f = XFRAME (w->frame);
26112 struct buffer *b = XBUFFER (w->contents);
26113 int cursor_type = DEFAULT_CURSOR;
26114 Lisp_Object alt_cursor;
26115 int non_selected = 0;
26117 *active_cursor = 1;
26119 /* Echo area */
26120 if (cursor_in_echo_area
26121 && FRAME_HAS_MINIBUF_P (f)
26122 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26124 if (w == XWINDOW (echo_area_window))
26126 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26128 *width = FRAME_CURSOR_WIDTH (f);
26129 return FRAME_DESIRED_CURSOR (f);
26131 else
26132 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26135 *active_cursor = 0;
26136 non_selected = 1;
26139 /* Detect a nonselected window or nonselected frame. */
26140 else if (w != XWINDOW (f->selected_window)
26141 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26143 *active_cursor = 0;
26145 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26146 return NO_CURSOR;
26148 non_selected = 1;
26151 /* Never display a cursor in a window in which cursor-type is nil. */
26152 if (NILP (BVAR (b, cursor_type)))
26153 return NO_CURSOR;
26155 /* Get the normal cursor type for this window. */
26156 if (EQ (BVAR (b, cursor_type), Qt))
26158 cursor_type = FRAME_DESIRED_CURSOR (f);
26159 *width = FRAME_CURSOR_WIDTH (f);
26161 else
26162 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26164 /* Use cursor-in-non-selected-windows instead
26165 for non-selected window or frame. */
26166 if (non_selected)
26168 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26169 if (!EQ (Qt, alt_cursor))
26170 return get_specified_cursor_type (alt_cursor, width);
26171 /* t means modify the normal cursor type. */
26172 if (cursor_type == FILLED_BOX_CURSOR)
26173 cursor_type = HOLLOW_BOX_CURSOR;
26174 else if (cursor_type == BAR_CURSOR && *width > 1)
26175 --*width;
26176 return cursor_type;
26179 /* Use normal cursor if not blinked off. */
26180 if (!w->cursor_off_p)
26182 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26184 if (cursor_type == FILLED_BOX_CURSOR)
26186 /* Using a block cursor on large images can be very annoying.
26187 So use a hollow cursor for "large" images.
26188 If image is not transparent (no mask), also use hollow cursor. */
26189 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26190 if (img != NULL && IMAGEP (img->spec))
26192 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26193 where N = size of default frame font size.
26194 This should cover most of the "tiny" icons people may use. */
26195 if (!img->mask
26196 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26197 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26198 cursor_type = HOLLOW_BOX_CURSOR;
26201 else if (cursor_type != NO_CURSOR)
26203 /* Display current only supports BOX and HOLLOW cursors for images.
26204 So for now, unconditionally use a HOLLOW cursor when cursor is
26205 not a solid box cursor. */
26206 cursor_type = HOLLOW_BOX_CURSOR;
26209 return cursor_type;
26212 /* Cursor is blinked off, so determine how to "toggle" it. */
26214 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26215 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26216 return get_specified_cursor_type (XCDR (alt_cursor), width);
26218 /* Then see if frame has specified a specific blink off cursor type. */
26219 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26221 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26222 return FRAME_BLINK_OFF_CURSOR (f);
26225 #if 0
26226 /* Some people liked having a permanently visible blinking cursor,
26227 while others had very strong opinions against it. So it was
26228 decided to remove it. KFS 2003-09-03 */
26230 /* Finally perform built-in cursor blinking:
26231 filled box <-> hollow box
26232 wide [h]bar <-> narrow [h]bar
26233 narrow [h]bar <-> no cursor
26234 other type <-> no cursor */
26236 if (cursor_type == FILLED_BOX_CURSOR)
26237 return HOLLOW_BOX_CURSOR;
26239 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26241 *width = 1;
26242 return cursor_type;
26244 #endif
26246 return NO_CURSOR;
26250 /* Notice when the text cursor of window W has been completely
26251 overwritten by a drawing operation that outputs glyphs in AREA
26252 starting at X0 and ending at X1 in the line starting at Y0 and
26253 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26254 the rest of the line after X0 has been written. Y coordinates
26255 are window-relative. */
26257 static void
26258 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26259 int x0, int x1, int y0, int y1)
26261 int cx0, cx1, cy0, cy1;
26262 struct glyph_row *row;
26264 if (!w->phys_cursor_on_p)
26265 return;
26266 if (area != TEXT_AREA)
26267 return;
26269 if (w->phys_cursor.vpos < 0
26270 || w->phys_cursor.vpos >= w->current_matrix->nrows
26271 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26272 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26273 return;
26275 if (row->cursor_in_fringe_p)
26277 row->cursor_in_fringe_p = 0;
26278 draw_fringe_bitmap (w, row, row->reversed_p);
26279 w->phys_cursor_on_p = 0;
26280 return;
26283 cx0 = w->phys_cursor.x;
26284 cx1 = cx0 + w->phys_cursor_width;
26285 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26286 return;
26288 /* The cursor image will be completely removed from the
26289 screen if the output area intersects the cursor area in
26290 y-direction. When we draw in [y0 y1[, and some part of
26291 the cursor is at y < y0, that part must have been drawn
26292 before. When scrolling, the cursor is erased before
26293 actually scrolling, so we don't come here. When not
26294 scrolling, the rows above the old cursor row must have
26295 changed, and in this case these rows must have written
26296 over the cursor image.
26298 Likewise if part of the cursor is below y1, with the
26299 exception of the cursor being in the first blank row at
26300 the buffer and window end because update_text_area
26301 doesn't draw that row. (Except when it does, but
26302 that's handled in update_text_area.) */
26304 cy0 = w->phys_cursor.y;
26305 cy1 = cy0 + w->phys_cursor_height;
26306 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26307 return;
26309 w->phys_cursor_on_p = 0;
26312 #endif /* HAVE_WINDOW_SYSTEM */
26315 /************************************************************************
26316 Mouse Face
26317 ************************************************************************/
26319 #ifdef HAVE_WINDOW_SYSTEM
26321 /* EXPORT for RIF:
26322 Fix the display of area AREA of overlapping row ROW in window W
26323 with respect to the overlapping part OVERLAPS. */
26325 void
26326 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26327 enum glyph_row_area area, int overlaps)
26329 int i, x;
26331 block_input ();
26333 x = 0;
26334 for (i = 0; i < row->used[area];)
26336 if (row->glyphs[area][i].overlaps_vertically_p)
26338 int start = i, start_x = x;
26342 x += row->glyphs[area][i].pixel_width;
26343 ++i;
26345 while (i < row->used[area]
26346 && row->glyphs[area][i].overlaps_vertically_p);
26348 draw_glyphs (w, start_x, row, area,
26349 start, i,
26350 DRAW_NORMAL_TEXT, overlaps);
26352 else
26354 x += row->glyphs[area][i].pixel_width;
26355 ++i;
26359 unblock_input ();
26363 /* EXPORT:
26364 Draw the cursor glyph of window W in glyph row ROW. See the
26365 comment of draw_glyphs for the meaning of HL. */
26367 void
26368 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26369 enum draw_glyphs_face hl)
26371 /* If cursor hpos is out of bounds, don't draw garbage. This can
26372 happen in mini-buffer windows when switching between echo area
26373 glyphs and mini-buffer. */
26374 if ((row->reversed_p
26375 ? (w->phys_cursor.hpos >= 0)
26376 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26378 int on_p = w->phys_cursor_on_p;
26379 int x1;
26380 int hpos = w->phys_cursor.hpos;
26382 /* When the window is hscrolled, cursor hpos can legitimately be
26383 out of bounds, but we draw the cursor at the corresponding
26384 window margin in that case. */
26385 if (!row->reversed_p && hpos < 0)
26386 hpos = 0;
26387 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26388 hpos = row->used[TEXT_AREA] - 1;
26390 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26391 hl, 0);
26392 w->phys_cursor_on_p = on_p;
26394 if (hl == DRAW_CURSOR)
26395 w->phys_cursor_width = x1 - w->phys_cursor.x;
26396 /* When we erase the cursor, and ROW is overlapped by other
26397 rows, make sure that these overlapping parts of other rows
26398 are redrawn. */
26399 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26401 w->phys_cursor_width = x1 - w->phys_cursor.x;
26403 if (row > w->current_matrix->rows
26404 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26405 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26406 OVERLAPS_ERASED_CURSOR);
26408 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26409 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26410 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26411 OVERLAPS_ERASED_CURSOR);
26417 /* EXPORT:
26418 Erase the image of a cursor of window W from the screen. */
26420 void
26421 erase_phys_cursor (struct window *w)
26423 struct frame *f = XFRAME (w->frame);
26424 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26425 int hpos = w->phys_cursor.hpos;
26426 int vpos = w->phys_cursor.vpos;
26427 int mouse_face_here_p = 0;
26428 struct glyph_matrix *active_glyphs = w->current_matrix;
26429 struct glyph_row *cursor_row;
26430 struct glyph *cursor_glyph;
26431 enum draw_glyphs_face hl;
26433 /* No cursor displayed or row invalidated => nothing to do on the
26434 screen. */
26435 if (w->phys_cursor_type == NO_CURSOR)
26436 goto mark_cursor_off;
26438 /* VPOS >= active_glyphs->nrows means that window has been resized.
26439 Don't bother to erase the cursor. */
26440 if (vpos >= active_glyphs->nrows)
26441 goto mark_cursor_off;
26443 /* If row containing cursor is marked invalid, there is nothing we
26444 can do. */
26445 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26446 if (!cursor_row->enabled_p)
26447 goto mark_cursor_off;
26449 /* If line spacing is > 0, old cursor may only be partially visible in
26450 window after split-window. So adjust visible height. */
26451 cursor_row->visible_height = min (cursor_row->visible_height,
26452 window_text_bottom_y (w) - cursor_row->y);
26454 /* If row is completely invisible, don't attempt to delete a cursor which
26455 isn't there. This can happen if cursor is at top of a window, and
26456 we switch to a buffer with a header line in that window. */
26457 if (cursor_row->visible_height <= 0)
26458 goto mark_cursor_off;
26460 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26461 if (cursor_row->cursor_in_fringe_p)
26463 cursor_row->cursor_in_fringe_p = 0;
26464 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26465 goto mark_cursor_off;
26468 /* This can happen when the new row is shorter than the old one.
26469 In this case, either draw_glyphs or clear_end_of_line
26470 should have cleared the cursor. Note that we wouldn't be
26471 able to erase the cursor in this case because we don't have a
26472 cursor glyph at hand. */
26473 if ((cursor_row->reversed_p
26474 ? (w->phys_cursor.hpos < 0)
26475 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26476 goto mark_cursor_off;
26478 /* When the window is hscrolled, cursor hpos can legitimately be out
26479 of bounds, but we draw the cursor at the corresponding window
26480 margin in that case. */
26481 if (!cursor_row->reversed_p && hpos < 0)
26482 hpos = 0;
26483 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26484 hpos = cursor_row->used[TEXT_AREA] - 1;
26486 /* If the cursor is in the mouse face area, redisplay that when
26487 we clear the cursor. */
26488 if (! NILP (hlinfo->mouse_face_window)
26489 && coords_in_mouse_face_p (w, hpos, vpos)
26490 /* Don't redraw the cursor's spot in mouse face if it is at the
26491 end of a line (on a newline). The cursor appears there, but
26492 mouse highlighting does not. */
26493 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26494 mouse_face_here_p = 1;
26496 /* Maybe clear the display under the cursor. */
26497 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26499 int x, y, left_x;
26500 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26501 int width;
26503 cursor_glyph = get_phys_cursor_glyph (w);
26504 if (cursor_glyph == NULL)
26505 goto mark_cursor_off;
26507 width = cursor_glyph->pixel_width;
26508 left_x = window_box_left_offset (w, TEXT_AREA);
26509 x = w->phys_cursor.x;
26510 if (x < left_x)
26511 width -= left_x - x;
26512 width = min (width, window_box_width (w, TEXT_AREA) - x);
26513 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26514 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26516 if (width > 0)
26517 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26520 /* Erase the cursor by redrawing the character underneath it. */
26521 if (mouse_face_here_p)
26522 hl = DRAW_MOUSE_FACE;
26523 else
26524 hl = DRAW_NORMAL_TEXT;
26525 draw_phys_cursor_glyph (w, cursor_row, hl);
26527 mark_cursor_off:
26528 w->phys_cursor_on_p = 0;
26529 w->phys_cursor_type = NO_CURSOR;
26533 /* EXPORT:
26534 Display or clear cursor of window W. If ON is zero, clear the
26535 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26536 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26538 void
26539 display_and_set_cursor (struct window *w, bool on,
26540 int hpos, int vpos, int x, int y)
26542 struct frame *f = XFRAME (w->frame);
26543 int new_cursor_type;
26544 int new_cursor_width;
26545 int active_cursor;
26546 struct glyph_row *glyph_row;
26547 struct glyph *glyph;
26549 /* This is pointless on invisible frames, and dangerous on garbaged
26550 windows and frames; in the latter case, the frame or window may
26551 be in the midst of changing its size, and x and y may be off the
26552 window. */
26553 if (! FRAME_VISIBLE_P (f)
26554 || FRAME_GARBAGED_P (f)
26555 || vpos >= w->current_matrix->nrows
26556 || hpos >= w->current_matrix->matrix_w)
26557 return;
26559 /* If cursor is off and we want it off, return quickly. */
26560 if (!on && !w->phys_cursor_on_p)
26561 return;
26563 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26564 /* If cursor row is not enabled, we don't really know where to
26565 display the cursor. */
26566 if (!glyph_row->enabled_p)
26568 w->phys_cursor_on_p = 0;
26569 return;
26572 glyph = NULL;
26573 if (!glyph_row->exact_window_width_line_p
26574 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26575 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26577 eassert (input_blocked_p ());
26579 /* Set new_cursor_type to the cursor we want to be displayed. */
26580 new_cursor_type = get_window_cursor_type (w, glyph,
26581 &new_cursor_width, &active_cursor);
26583 /* If cursor is currently being shown and we don't want it to be or
26584 it is in the wrong place, or the cursor type is not what we want,
26585 erase it. */
26586 if (w->phys_cursor_on_p
26587 && (!on
26588 || w->phys_cursor.x != x
26589 || w->phys_cursor.y != y
26590 || new_cursor_type != w->phys_cursor_type
26591 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26592 && new_cursor_width != w->phys_cursor_width)))
26593 erase_phys_cursor (w);
26595 /* Don't check phys_cursor_on_p here because that flag is only set
26596 to zero in some cases where we know that the cursor has been
26597 completely erased, to avoid the extra work of erasing the cursor
26598 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26599 still not be visible, or it has only been partly erased. */
26600 if (on)
26602 w->phys_cursor_ascent = glyph_row->ascent;
26603 w->phys_cursor_height = glyph_row->height;
26605 /* Set phys_cursor_.* before x_draw_.* is called because some
26606 of them may need the information. */
26607 w->phys_cursor.x = x;
26608 w->phys_cursor.y = glyph_row->y;
26609 w->phys_cursor.hpos = hpos;
26610 w->phys_cursor.vpos = vpos;
26613 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26614 new_cursor_type, new_cursor_width,
26615 on, active_cursor);
26619 /* Switch the display of W's cursor on or off, according to the value
26620 of ON. */
26622 static void
26623 update_window_cursor (struct window *w, bool on)
26625 /* Don't update cursor in windows whose frame is in the process
26626 of being deleted. */
26627 if (w->current_matrix)
26629 int hpos = w->phys_cursor.hpos;
26630 int vpos = w->phys_cursor.vpos;
26631 struct glyph_row *row;
26633 if (vpos >= w->current_matrix->nrows
26634 || hpos >= w->current_matrix->matrix_w)
26635 return;
26637 row = MATRIX_ROW (w->current_matrix, vpos);
26639 /* When the window is hscrolled, cursor hpos can legitimately be
26640 out of bounds, but we draw the cursor at the corresponding
26641 window margin in that case. */
26642 if (!row->reversed_p && hpos < 0)
26643 hpos = 0;
26644 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26645 hpos = row->used[TEXT_AREA] - 1;
26647 block_input ();
26648 display_and_set_cursor (w, on, hpos, vpos,
26649 w->phys_cursor.x, w->phys_cursor.y);
26650 unblock_input ();
26655 /* Call update_window_cursor with parameter ON_P on all leaf windows
26656 in the window tree rooted at W. */
26658 static void
26659 update_cursor_in_window_tree (struct window *w, bool on_p)
26661 while (w)
26663 if (WINDOWP (w->contents))
26664 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26665 else
26666 update_window_cursor (w, on_p);
26668 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26673 /* EXPORT:
26674 Display the cursor on window W, or clear it, according to ON_P.
26675 Don't change the cursor's position. */
26677 void
26678 x_update_cursor (struct frame *f, bool on_p)
26680 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26684 /* EXPORT:
26685 Clear the cursor of window W to background color, and mark the
26686 cursor as not shown. This is used when the text where the cursor
26687 is about to be rewritten. */
26689 void
26690 x_clear_cursor (struct window *w)
26692 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26693 update_window_cursor (w, 0);
26696 #endif /* HAVE_WINDOW_SYSTEM */
26698 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26699 and MSDOS. */
26700 static void
26701 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26702 int start_hpos, int end_hpos,
26703 enum draw_glyphs_face draw)
26705 #ifdef HAVE_WINDOW_SYSTEM
26706 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26708 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26709 return;
26711 #endif
26712 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26713 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26714 #endif
26717 /* Display the active region described by mouse_face_* according to DRAW. */
26719 static void
26720 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26722 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26723 struct frame *f = XFRAME (WINDOW_FRAME (w));
26725 if (/* If window is in the process of being destroyed, don't bother
26726 to do anything. */
26727 w->current_matrix != NULL
26728 /* Don't update mouse highlight if hidden */
26729 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26730 /* Recognize when we are called to operate on rows that don't exist
26731 anymore. This can happen when a window is split. */
26732 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26734 int phys_cursor_on_p = w->phys_cursor_on_p;
26735 struct glyph_row *row, *first, *last;
26737 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26738 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26740 for (row = first; row <= last && row->enabled_p; ++row)
26742 int start_hpos, end_hpos, start_x;
26744 /* For all but the first row, the highlight starts at column 0. */
26745 if (row == first)
26747 /* R2L rows have BEG and END in reversed order, but the
26748 screen drawing geometry is always left to right. So
26749 we need to mirror the beginning and end of the
26750 highlighted area in R2L rows. */
26751 if (!row->reversed_p)
26753 start_hpos = hlinfo->mouse_face_beg_col;
26754 start_x = hlinfo->mouse_face_beg_x;
26756 else if (row == last)
26758 start_hpos = hlinfo->mouse_face_end_col;
26759 start_x = hlinfo->mouse_face_end_x;
26761 else
26763 start_hpos = 0;
26764 start_x = 0;
26767 else if (row->reversed_p && row == last)
26769 start_hpos = hlinfo->mouse_face_end_col;
26770 start_x = hlinfo->mouse_face_end_x;
26772 else
26774 start_hpos = 0;
26775 start_x = 0;
26778 if (row == last)
26780 if (!row->reversed_p)
26781 end_hpos = hlinfo->mouse_face_end_col;
26782 else if (row == first)
26783 end_hpos = hlinfo->mouse_face_beg_col;
26784 else
26786 end_hpos = row->used[TEXT_AREA];
26787 if (draw == DRAW_NORMAL_TEXT)
26788 row->fill_line_p = 1; /* Clear to end of line */
26791 else if (row->reversed_p && row == first)
26792 end_hpos = hlinfo->mouse_face_beg_col;
26793 else
26795 end_hpos = row->used[TEXT_AREA];
26796 if (draw == DRAW_NORMAL_TEXT)
26797 row->fill_line_p = 1; /* Clear to end of line */
26800 if (end_hpos > start_hpos)
26802 draw_row_with_mouse_face (w, start_x, row,
26803 start_hpos, end_hpos, draw);
26805 row->mouse_face_p
26806 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26810 #ifdef HAVE_WINDOW_SYSTEM
26811 /* When we've written over the cursor, arrange for it to
26812 be displayed again. */
26813 if (FRAME_WINDOW_P (f)
26814 && phys_cursor_on_p && !w->phys_cursor_on_p)
26816 int hpos = w->phys_cursor.hpos;
26818 /* When the window is hscrolled, cursor hpos can legitimately be
26819 out of bounds, but we draw the cursor at the corresponding
26820 window margin in that case. */
26821 if (!row->reversed_p && hpos < 0)
26822 hpos = 0;
26823 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26824 hpos = row->used[TEXT_AREA] - 1;
26826 block_input ();
26827 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26828 w->phys_cursor.x, w->phys_cursor.y);
26829 unblock_input ();
26831 #endif /* HAVE_WINDOW_SYSTEM */
26834 #ifdef HAVE_WINDOW_SYSTEM
26835 /* Change the mouse cursor. */
26836 if (FRAME_WINDOW_P (f))
26838 if (draw == DRAW_NORMAL_TEXT
26839 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26840 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26841 else if (draw == DRAW_MOUSE_FACE)
26842 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26843 else
26844 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26846 #endif /* HAVE_WINDOW_SYSTEM */
26849 /* EXPORT:
26850 Clear out the mouse-highlighted active region.
26851 Redraw it un-highlighted first. Value is non-zero if mouse
26852 face was actually drawn unhighlighted. */
26855 clear_mouse_face (Mouse_HLInfo *hlinfo)
26857 int cleared = 0;
26859 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26861 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26862 cleared = 1;
26865 reset_mouse_highlight (hlinfo);
26866 return cleared;
26869 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26870 within the mouse face on that window. */
26871 static int
26872 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26874 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26876 /* Quickly resolve the easy cases. */
26877 if (!(WINDOWP (hlinfo->mouse_face_window)
26878 && XWINDOW (hlinfo->mouse_face_window) == w))
26879 return 0;
26880 if (vpos < hlinfo->mouse_face_beg_row
26881 || vpos > hlinfo->mouse_face_end_row)
26882 return 0;
26883 if (vpos > hlinfo->mouse_face_beg_row
26884 && vpos < hlinfo->mouse_face_end_row)
26885 return 1;
26887 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26889 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26891 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26892 return 1;
26894 else if ((vpos == hlinfo->mouse_face_beg_row
26895 && hpos >= hlinfo->mouse_face_beg_col)
26896 || (vpos == hlinfo->mouse_face_end_row
26897 && hpos < hlinfo->mouse_face_end_col))
26898 return 1;
26900 else
26902 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26904 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26905 return 1;
26907 else if ((vpos == hlinfo->mouse_face_beg_row
26908 && hpos <= hlinfo->mouse_face_beg_col)
26909 || (vpos == hlinfo->mouse_face_end_row
26910 && hpos > hlinfo->mouse_face_end_col))
26911 return 1;
26913 return 0;
26917 /* EXPORT:
26918 Non-zero if physical cursor of window W is within mouse face. */
26921 cursor_in_mouse_face_p (struct window *w)
26923 int hpos = w->phys_cursor.hpos;
26924 int vpos = w->phys_cursor.vpos;
26925 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26927 /* When the window is hscrolled, cursor hpos can legitimately be out
26928 of bounds, but we draw the cursor at the corresponding window
26929 margin in that case. */
26930 if (!row->reversed_p && hpos < 0)
26931 hpos = 0;
26932 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26933 hpos = row->used[TEXT_AREA] - 1;
26935 return coords_in_mouse_face_p (w, hpos, vpos);
26940 /* Find the glyph rows START_ROW and END_ROW of window W that display
26941 characters between buffer positions START_CHARPOS and END_CHARPOS
26942 (excluding END_CHARPOS). DISP_STRING is a display string that
26943 covers these buffer positions. This is similar to
26944 row_containing_pos, but is more accurate when bidi reordering makes
26945 buffer positions change non-linearly with glyph rows. */
26946 static void
26947 rows_from_pos_range (struct window *w,
26948 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26949 Lisp_Object disp_string,
26950 struct glyph_row **start, struct glyph_row **end)
26952 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26953 int last_y = window_text_bottom_y (w);
26954 struct glyph_row *row;
26956 *start = NULL;
26957 *end = NULL;
26959 while (!first->enabled_p
26960 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26961 first++;
26963 /* Find the START row. */
26964 for (row = first;
26965 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26966 row++)
26968 /* A row can potentially be the START row if the range of the
26969 characters it displays intersects the range
26970 [START_CHARPOS..END_CHARPOS). */
26971 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26972 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26973 /* See the commentary in row_containing_pos, for the
26974 explanation of the complicated way to check whether
26975 some position is beyond the end of the characters
26976 displayed by a row. */
26977 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26978 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26979 && !row->ends_at_zv_p
26980 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26981 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26982 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26983 && !row->ends_at_zv_p
26984 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26986 /* Found a candidate row. Now make sure at least one of the
26987 glyphs it displays has a charpos from the range
26988 [START_CHARPOS..END_CHARPOS).
26990 This is not obvious because bidi reordering could make
26991 buffer positions of a row be 1,2,3,102,101,100, and if we
26992 want to highlight characters in [50..60), we don't want
26993 this row, even though [50..60) does intersect [1..103),
26994 the range of character positions given by the row's start
26995 and end positions. */
26996 struct glyph *g = row->glyphs[TEXT_AREA];
26997 struct glyph *e = g + row->used[TEXT_AREA];
26999 while (g < e)
27001 if (((BUFFERP (g->object) || INTEGERP (g->object))
27002 && start_charpos <= g->charpos && g->charpos < end_charpos)
27003 /* A glyph that comes from DISP_STRING is by
27004 definition to be highlighted. */
27005 || EQ (g->object, disp_string))
27006 *start = row;
27007 g++;
27009 if (*start)
27010 break;
27014 /* Find the END row. */
27015 if (!*start
27016 /* If the last row is partially visible, start looking for END
27017 from that row, instead of starting from FIRST. */
27018 && !(row->enabled_p
27019 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27020 row = first;
27021 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27023 struct glyph_row *next = row + 1;
27024 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27026 if (!next->enabled_p
27027 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27028 /* The first row >= START whose range of displayed characters
27029 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27030 is the row END + 1. */
27031 || (start_charpos < next_start
27032 && end_charpos < next_start)
27033 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27034 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27035 && !next->ends_at_zv_p
27036 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27037 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27038 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27039 && !next->ends_at_zv_p
27040 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27042 *end = row;
27043 break;
27045 else
27047 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27048 but none of the characters it displays are in the range, it is
27049 also END + 1. */
27050 struct glyph *g = next->glyphs[TEXT_AREA];
27051 struct glyph *s = g;
27052 struct glyph *e = g + next->used[TEXT_AREA];
27054 while (g < e)
27056 if (((BUFFERP (g->object) || INTEGERP (g->object))
27057 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27058 /* If the buffer position of the first glyph in
27059 the row is equal to END_CHARPOS, it means
27060 the last character to be highlighted is the
27061 newline of ROW, and we must consider NEXT as
27062 END, not END+1. */
27063 || (((!next->reversed_p && g == s)
27064 || (next->reversed_p && g == e - 1))
27065 && (g->charpos == end_charpos
27066 /* Special case for when NEXT is an
27067 empty line at ZV. */
27068 || (g->charpos == -1
27069 && !row->ends_at_zv_p
27070 && next_start == end_charpos)))))
27071 /* A glyph that comes from DISP_STRING is by
27072 definition to be highlighted. */
27073 || EQ (g->object, disp_string))
27074 break;
27075 g++;
27077 if (g == e)
27079 *end = row;
27080 break;
27082 /* The first row that ends at ZV must be the last to be
27083 highlighted. */
27084 else if (next->ends_at_zv_p)
27086 *end = next;
27087 break;
27093 /* This function sets the mouse_face_* elements of HLINFO, assuming
27094 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27095 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27096 for the overlay or run of text properties specifying the mouse
27097 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27098 before-string and after-string that must also be highlighted.
27099 DISP_STRING, if non-nil, is a display string that may cover some
27100 or all of the highlighted text. */
27102 static void
27103 mouse_face_from_buffer_pos (Lisp_Object window,
27104 Mouse_HLInfo *hlinfo,
27105 ptrdiff_t mouse_charpos,
27106 ptrdiff_t start_charpos,
27107 ptrdiff_t end_charpos,
27108 Lisp_Object before_string,
27109 Lisp_Object after_string,
27110 Lisp_Object disp_string)
27112 struct window *w = XWINDOW (window);
27113 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27114 struct glyph_row *r1, *r2;
27115 struct glyph *glyph, *end;
27116 ptrdiff_t ignore, pos;
27117 int x;
27119 eassert (NILP (disp_string) || STRINGP (disp_string));
27120 eassert (NILP (before_string) || STRINGP (before_string));
27121 eassert (NILP (after_string) || STRINGP (after_string));
27123 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27124 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27125 if (r1 == NULL)
27126 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27127 /* If the before-string or display-string contains newlines,
27128 rows_from_pos_range skips to its last row. Move back. */
27129 if (!NILP (before_string) || !NILP (disp_string))
27131 struct glyph_row *prev;
27132 while ((prev = r1 - 1, prev >= first)
27133 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27134 && prev->used[TEXT_AREA] > 0)
27136 struct glyph *beg = prev->glyphs[TEXT_AREA];
27137 glyph = beg + prev->used[TEXT_AREA];
27138 while (--glyph >= beg && INTEGERP (glyph->object));
27139 if (glyph < beg
27140 || !(EQ (glyph->object, before_string)
27141 || EQ (glyph->object, disp_string)))
27142 break;
27143 r1 = prev;
27146 if (r2 == NULL)
27148 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27149 hlinfo->mouse_face_past_end = 1;
27151 else if (!NILP (after_string))
27153 /* If the after-string has newlines, advance to its last row. */
27154 struct glyph_row *next;
27155 struct glyph_row *last
27156 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27158 for (next = r2 + 1;
27159 next <= last
27160 && next->used[TEXT_AREA] > 0
27161 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27162 ++next)
27163 r2 = next;
27165 /* The rest of the display engine assumes that mouse_face_beg_row is
27166 either above mouse_face_end_row or identical to it. But with
27167 bidi-reordered continued lines, the row for START_CHARPOS could
27168 be below the row for END_CHARPOS. If so, swap the rows and store
27169 them in correct order. */
27170 if (r1->y > r2->y)
27172 struct glyph_row *tem = r2;
27174 r2 = r1;
27175 r1 = tem;
27178 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27179 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27181 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27182 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27183 could be anywhere in the row and in any order. The strategy
27184 below is to find the leftmost and the rightmost glyph that
27185 belongs to either of these 3 strings, or whose position is
27186 between START_CHARPOS and END_CHARPOS, and highlight all the
27187 glyphs between those two. This may cover more than just the text
27188 between START_CHARPOS and END_CHARPOS if the range of characters
27189 strides the bidi level boundary, e.g. if the beginning is in R2L
27190 text while the end is in L2R text or vice versa. */
27191 if (!r1->reversed_p)
27193 /* This row is in a left to right paragraph. Scan it left to
27194 right. */
27195 glyph = r1->glyphs[TEXT_AREA];
27196 end = glyph + r1->used[TEXT_AREA];
27197 x = r1->x;
27199 /* Skip truncation glyphs at the start of the glyph row. */
27200 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27201 for (; glyph < end
27202 && INTEGERP (glyph->object)
27203 && glyph->charpos < 0;
27204 ++glyph)
27205 x += glyph->pixel_width;
27207 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27208 or DISP_STRING, and the first glyph from buffer whose
27209 position is between START_CHARPOS and END_CHARPOS. */
27210 for (; glyph < end
27211 && !INTEGERP (glyph->object)
27212 && !EQ (glyph->object, disp_string)
27213 && !(BUFFERP (glyph->object)
27214 && (glyph->charpos >= start_charpos
27215 && glyph->charpos < end_charpos));
27216 ++glyph)
27218 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27219 are present at buffer positions between START_CHARPOS and
27220 END_CHARPOS, or if they come from an overlay. */
27221 if (EQ (glyph->object, before_string))
27223 pos = string_buffer_position (before_string,
27224 start_charpos);
27225 /* If pos == 0, it means before_string came from an
27226 overlay, not from a buffer position. */
27227 if (!pos || (pos >= start_charpos && pos < end_charpos))
27228 break;
27230 else if (EQ (glyph->object, after_string))
27232 pos = string_buffer_position (after_string, end_charpos);
27233 if (!pos || (pos >= start_charpos && pos < end_charpos))
27234 break;
27236 x += glyph->pixel_width;
27238 hlinfo->mouse_face_beg_x = x;
27239 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27241 else
27243 /* This row is in a right to left paragraph. Scan it right to
27244 left. */
27245 struct glyph *g;
27247 end = r1->glyphs[TEXT_AREA] - 1;
27248 glyph = end + r1->used[TEXT_AREA];
27250 /* Skip truncation glyphs at the start of the glyph row. */
27251 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27252 for (; glyph > end
27253 && INTEGERP (glyph->object)
27254 && glyph->charpos < 0;
27255 --glyph)
27258 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27259 or DISP_STRING, and the first glyph from buffer whose
27260 position is between START_CHARPOS and END_CHARPOS. */
27261 for (; glyph > end
27262 && !INTEGERP (glyph->object)
27263 && !EQ (glyph->object, disp_string)
27264 && !(BUFFERP (glyph->object)
27265 && (glyph->charpos >= start_charpos
27266 && glyph->charpos < end_charpos));
27267 --glyph)
27269 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27270 are present at buffer positions between START_CHARPOS and
27271 END_CHARPOS, or if they come from an overlay. */
27272 if (EQ (glyph->object, before_string))
27274 pos = string_buffer_position (before_string, start_charpos);
27275 /* If pos == 0, it means before_string came from an
27276 overlay, not from a buffer position. */
27277 if (!pos || (pos >= start_charpos && pos < end_charpos))
27278 break;
27280 else if (EQ (glyph->object, after_string))
27282 pos = string_buffer_position (after_string, end_charpos);
27283 if (!pos || (pos >= start_charpos && pos < end_charpos))
27284 break;
27288 glyph++; /* first glyph to the right of the highlighted area */
27289 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27290 x += g->pixel_width;
27291 hlinfo->mouse_face_beg_x = x;
27292 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27295 /* If the highlight ends in a different row, compute GLYPH and END
27296 for the end row. Otherwise, reuse the values computed above for
27297 the row where the highlight begins. */
27298 if (r2 != r1)
27300 if (!r2->reversed_p)
27302 glyph = r2->glyphs[TEXT_AREA];
27303 end = glyph + r2->used[TEXT_AREA];
27304 x = r2->x;
27306 else
27308 end = r2->glyphs[TEXT_AREA] - 1;
27309 glyph = end + r2->used[TEXT_AREA];
27313 if (!r2->reversed_p)
27315 /* Skip truncation and continuation glyphs near the end of the
27316 row, and also blanks and stretch glyphs inserted by
27317 extend_face_to_end_of_line. */
27318 while (end > glyph
27319 && INTEGERP ((end - 1)->object))
27320 --end;
27321 /* Scan the rest of the glyph row from the end, looking for the
27322 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27323 DISP_STRING, or whose position is between START_CHARPOS
27324 and END_CHARPOS */
27325 for (--end;
27326 end > glyph
27327 && !INTEGERP (end->object)
27328 && !EQ (end->object, disp_string)
27329 && !(BUFFERP (end->object)
27330 && (end->charpos >= start_charpos
27331 && end->charpos < end_charpos));
27332 --end)
27334 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27335 are present at buffer positions between START_CHARPOS and
27336 END_CHARPOS, or if they come from an overlay. */
27337 if (EQ (end->object, before_string))
27339 pos = string_buffer_position (before_string, start_charpos);
27340 if (!pos || (pos >= start_charpos && pos < end_charpos))
27341 break;
27343 else if (EQ (end->object, after_string))
27345 pos = string_buffer_position (after_string, end_charpos);
27346 if (!pos || (pos >= start_charpos && pos < end_charpos))
27347 break;
27350 /* Find the X coordinate of the last glyph to be highlighted. */
27351 for (; glyph <= end; ++glyph)
27352 x += glyph->pixel_width;
27354 hlinfo->mouse_face_end_x = x;
27355 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27357 else
27359 /* Skip truncation and continuation glyphs near the end of the
27360 row, and also blanks and stretch glyphs inserted by
27361 extend_face_to_end_of_line. */
27362 x = r2->x;
27363 end++;
27364 while (end < glyph
27365 && INTEGERP (end->object))
27367 x += end->pixel_width;
27368 ++end;
27370 /* Scan the rest of the glyph row from the end, looking for the
27371 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27372 DISP_STRING, or whose position is between START_CHARPOS
27373 and END_CHARPOS */
27374 for ( ;
27375 end < glyph
27376 && !INTEGERP (end->object)
27377 && !EQ (end->object, disp_string)
27378 && !(BUFFERP (end->object)
27379 && (end->charpos >= start_charpos
27380 && end->charpos < end_charpos));
27381 ++end)
27383 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27384 are present at buffer positions between START_CHARPOS and
27385 END_CHARPOS, or if they come from an overlay. */
27386 if (EQ (end->object, before_string))
27388 pos = string_buffer_position (before_string, start_charpos);
27389 if (!pos || (pos >= start_charpos && pos < end_charpos))
27390 break;
27392 else if (EQ (end->object, after_string))
27394 pos = string_buffer_position (after_string, end_charpos);
27395 if (!pos || (pos >= start_charpos && pos < end_charpos))
27396 break;
27398 x += end->pixel_width;
27400 /* If we exited the above loop because we arrived at the last
27401 glyph of the row, and its buffer position is still not in
27402 range, it means the last character in range is the preceding
27403 newline. Bump the end column and x values to get past the
27404 last glyph. */
27405 if (end == glyph
27406 && BUFFERP (end->object)
27407 && (end->charpos < start_charpos
27408 || end->charpos >= end_charpos))
27410 x += end->pixel_width;
27411 ++end;
27413 hlinfo->mouse_face_end_x = x;
27414 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27417 hlinfo->mouse_face_window = window;
27418 hlinfo->mouse_face_face_id
27419 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27420 mouse_charpos + 1,
27421 !hlinfo->mouse_face_hidden, -1);
27422 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27425 /* The following function is not used anymore (replaced with
27426 mouse_face_from_string_pos), but I leave it here for the time
27427 being, in case someone would. */
27429 #if 0 /* not used */
27431 /* Find the position of the glyph for position POS in OBJECT in
27432 window W's current matrix, and return in *X, *Y the pixel
27433 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27435 RIGHT_P non-zero means return the position of the right edge of the
27436 glyph, RIGHT_P zero means return the left edge position.
27438 If no glyph for POS exists in the matrix, return the position of
27439 the glyph with the next smaller position that is in the matrix, if
27440 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27441 exists in the matrix, return the position of the glyph with the
27442 next larger position in OBJECT.
27444 Value is non-zero if a glyph was found. */
27446 static int
27447 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27448 int *hpos, int *vpos, int *x, int *y, int right_p)
27450 int yb = window_text_bottom_y (w);
27451 struct glyph_row *r;
27452 struct glyph *best_glyph = NULL;
27453 struct glyph_row *best_row = NULL;
27454 int best_x = 0;
27456 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27457 r->enabled_p && r->y < yb;
27458 ++r)
27460 struct glyph *g = r->glyphs[TEXT_AREA];
27461 struct glyph *e = g + r->used[TEXT_AREA];
27462 int gx;
27464 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27465 if (EQ (g->object, object))
27467 if (g->charpos == pos)
27469 best_glyph = g;
27470 best_x = gx;
27471 best_row = r;
27472 goto found;
27474 else if (best_glyph == NULL
27475 || ((eabs (g->charpos - pos)
27476 < eabs (best_glyph->charpos - pos))
27477 && (right_p
27478 ? g->charpos < pos
27479 : g->charpos > pos)))
27481 best_glyph = g;
27482 best_x = gx;
27483 best_row = r;
27488 found:
27490 if (best_glyph)
27492 *x = best_x;
27493 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27495 if (right_p)
27497 *x += best_glyph->pixel_width;
27498 ++*hpos;
27501 *y = best_row->y;
27502 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27505 return best_glyph != NULL;
27507 #endif /* not used */
27509 /* Find the positions of the first and the last glyphs in window W's
27510 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27511 (assumed to be a string), and return in HLINFO's mouse_face_*
27512 members the pixel and column/row coordinates of those glyphs. */
27514 static void
27515 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27516 Lisp_Object object,
27517 ptrdiff_t startpos, ptrdiff_t endpos)
27519 int yb = window_text_bottom_y (w);
27520 struct glyph_row *r;
27521 struct glyph *g, *e;
27522 int gx;
27523 int found = 0;
27525 /* Find the glyph row with at least one position in the range
27526 [STARTPOS..ENDPOS], and the first glyph in that row whose
27527 position belongs to that range. */
27528 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27529 r->enabled_p && r->y < yb;
27530 ++r)
27532 if (!r->reversed_p)
27534 g = r->glyphs[TEXT_AREA];
27535 e = g + r->used[TEXT_AREA];
27536 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27537 if (EQ (g->object, object)
27538 && startpos <= g->charpos && g->charpos <= endpos)
27540 hlinfo->mouse_face_beg_row
27541 = MATRIX_ROW_VPOS (r, w->current_matrix);
27542 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27543 hlinfo->mouse_face_beg_x = gx;
27544 found = 1;
27545 break;
27548 else
27550 struct glyph *g1;
27552 e = r->glyphs[TEXT_AREA];
27553 g = e + r->used[TEXT_AREA];
27554 for ( ; g > e; --g)
27555 if (EQ ((g-1)->object, object)
27556 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27558 hlinfo->mouse_face_beg_row
27559 = MATRIX_ROW_VPOS (r, w->current_matrix);
27560 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27561 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27562 gx += g1->pixel_width;
27563 hlinfo->mouse_face_beg_x = gx;
27564 found = 1;
27565 break;
27568 if (found)
27569 break;
27572 if (!found)
27573 return;
27575 /* Starting with the next row, look for the first row which does NOT
27576 include any glyphs whose positions are in the range. */
27577 for (++r; r->enabled_p && r->y < yb; ++r)
27579 g = r->glyphs[TEXT_AREA];
27580 e = g + r->used[TEXT_AREA];
27581 found = 0;
27582 for ( ; g < e; ++g)
27583 if (EQ (g->object, object)
27584 && startpos <= g->charpos && g->charpos <= endpos)
27586 found = 1;
27587 break;
27589 if (!found)
27590 break;
27593 /* The highlighted region ends on the previous row. */
27594 r--;
27596 /* Set the end row. */
27597 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27599 /* Compute and set the end column and the end column's horizontal
27600 pixel coordinate. */
27601 if (!r->reversed_p)
27603 g = r->glyphs[TEXT_AREA];
27604 e = g + r->used[TEXT_AREA];
27605 for ( ; e > g; --e)
27606 if (EQ ((e-1)->object, object)
27607 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27608 break;
27609 hlinfo->mouse_face_end_col = e - g;
27611 for (gx = r->x; g < e; ++g)
27612 gx += g->pixel_width;
27613 hlinfo->mouse_face_end_x = gx;
27615 else
27617 e = r->glyphs[TEXT_AREA];
27618 g = e + r->used[TEXT_AREA];
27619 for (gx = r->x ; e < g; ++e)
27621 if (EQ (e->object, object)
27622 && startpos <= e->charpos && e->charpos <= endpos)
27623 break;
27624 gx += e->pixel_width;
27626 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27627 hlinfo->mouse_face_end_x = gx;
27631 #ifdef HAVE_WINDOW_SYSTEM
27633 /* See if position X, Y is within a hot-spot of an image. */
27635 static int
27636 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27638 if (!CONSP (hot_spot))
27639 return 0;
27641 if (EQ (XCAR (hot_spot), Qrect))
27643 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27644 Lisp_Object rect = XCDR (hot_spot);
27645 Lisp_Object tem;
27646 if (!CONSP (rect))
27647 return 0;
27648 if (!CONSP (XCAR (rect)))
27649 return 0;
27650 if (!CONSP (XCDR (rect)))
27651 return 0;
27652 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27653 return 0;
27654 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27655 return 0;
27656 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27657 return 0;
27658 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27659 return 0;
27660 return 1;
27662 else if (EQ (XCAR (hot_spot), Qcircle))
27664 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27665 Lisp_Object circ = XCDR (hot_spot);
27666 Lisp_Object lr, lx0, ly0;
27667 if (CONSP (circ)
27668 && CONSP (XCAR (circ))
27669 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27670 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27671 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27673 double r = XFLOATINT (lr);
27674 double dx = XINT (lx0) - x;
27675 double dy = XINT (ly0) - y;
27676 return (dx * dx + dy * dy <= r * r);
27679 else if (EQ (XCAR (hot_spot), Qpoly))
27681 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27682 if (VECTORP (XCDR (hot_spot)))
27684 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27685 Lisp_Object *poly = v->contents;
27686 ptrdiff_t n = v->header.size;
27687 ptrdiff_t i;
27688 int inside = 0;
27689 Lisp_Object lx, ly;
27690 int x0, y0;
27692 /* Need an even number of coordinates, and at least 3 edges. */
27693 if (n < 6 || n & 1)
27694 return 0;
27696 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27697 If count is odd, we are inside polygon. Pixels on edges
27698 may or may not be included depending on actual geometry of the
27699 polygon. */
27700 if ((lx = poly[n-2], !INTEGERP (lx))
27701 || (ly = poly[n-1], !INTEGERP (lx)))
27702 return 0;
27703 x0 = XINT (lx), y0 = XINT (ly);
27704 for (i = 0; i < n; i += 2)
27706 int x1 = x0, y1 = y0;
27707 if ((lx = poly[i], !INTEGERP (lx))
27708 || (ly = poly[i+1], !INTEGERP (ly)))
27709 return 0;
27710 x0 = XINT (lx), y0 = XINT (ly);
27712 /* Does this segment cross the X line? */
27713 if (x0 >= x)
27715 if (x1 >= x)
27716 continue;
27718 else if (x1 < x)
27719 continue;
27720 if (y > y0 && y > y1)
27721 continue;
27722 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27723 inside = !inside;
27725 return inside;
27728 return 0;
27731 Lisp_Object
27732 find_hot_spot (Lisp_Object map, int x, int y)
27734 while (CONSP (map))
27736 if (CONSP (XCAR (map))
27737 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27738 return XCAR (map);
27739 map = XCDR (map);
27742 return Qnil;
27745 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27746 3, 3, 0,
27747 doc: /* Lookup in image map MAP coordinates X and Y.
27748 An image map is an alist where each element has the format (AREA ID PLIST).
27749 An AREA is specified as either a rectangle, a circle, or a polygon:
27750 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27751 pixel coordinates of the upper left and bottom right corners.
27752 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27753 and the radius of the circle; r may be a float or integer.
27754 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27755 vector describes one corner in the polygon.
27756 Returns the alist element for the first matching AREA in MAP. */)
27757 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27759 if (NILP (map))
27760 return Qnil;
27762 CHECK_NUMBER (x);
27763 CHECK_NUMBER (y);
27765 return find_hot_spot (map,
27766 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27767 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27771 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27772 static void
27773 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27775 /* Do not change cursor shape while dragging mouse. */
27776 if (!NILP (do_mouse_tracking))
27777 return;
27779 if (!NILP (pointer))
27781 if (EQ (pointer, Qarrow))
27782 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27783 else if (EQ (pointer, Qhand))
27784 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27785 else if (EQ (pointer, Qtext))
27786 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27787 else if (EQ (pointer, intern ("hdrag")))
27788 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27789 #ifdef HAVE_X_WINDOWS
27790 else if (EQ (pointer, intern ("vdrag")))
27791 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27792 #endif
27793 else if (EQ (pointer, intern ("hourglass")))
27794 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27795 else if (EQ (pointer, Qmodeline))
27796 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27797 else
27798 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27801 if (cursor != No_Cursor)
27802 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27805 #endif /* HAVE_WINDOW_SYSTEM */
27807 /* Take proper action when mouse has moved to the mode or header line
27808 or marginal area AREA of window W, x-position X and y-position Y.
27809 X is relative to the start of the text display area of W, so the
27810 width of bitmap areas and scroll bars must be subtracted to get a
27811 position relative to the start of the mode line. */
27813 static void
27814 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27815 enum window_part area)
27817 struct window *w = XWINDOW (window);
27818 struct frame *f = XFRAME (w->frame);
27819 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27820 #ifdef HAVE_WINDOW_SYSTEM
27821 Display_Info *dpyinfo;
27822 #endif
27823 Cursor cursor = No_Cursor;
27824 Lisp_Object pointer = Qnil;
27825 int dx, dy, width, height;
27826 ptrdiff_t charpos;
27827 Lisp_Object string, object = Qnil;
27828 Lisp_Object pos IF_LINT (= Qnil), help;
27830 Lisp_Object mouse_face;
27831 int original_x_pixel = x;
27832 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27833 struct glyph_row *row IF_LINT (= 0);
27835 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27837 int x0;
27838 struct glyph *end;
27840 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27841 returns them in row/column units! */
27842 string = mode_line_string (w, area, &x, &y, &charpos,
27843 &object, &dx, &dy, &width, &height);
27845 row = (area == ON_MODE_LINE
27846 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27847 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27849 /* Find the glyph under the mouse pointer. */
27850 if (row->mode_line_p && row->enabled_p)
27852 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27853 end = glyph + row->used[TEXT_AREA];
27855 for (x0 = original_x_pixel;
27856 glyph < end && x0 >= glyph->pixel_width;
27857 ++glyph)
27858 x0 -= glyph->pixel_width;
27860 if (glyph >= end)
27861 glyph = NULL;
27864 else
27866 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27867 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27868 returns them in row/column units! */
27869 string = marginal_area_string (w, area, &x, &y, &charpos,
27870 &object, &dx, &dy, &width, &height);
27873 help = Qnil;
27875 #ifdef HAVE_WINDOW_SYSTEM
27876 if (IMAGEP (object))
27878 Lisp_Object image_map, hotspot;
27879 if ((image_map = Fplist_get (XCDR (object), QCmap),
27880 !NILP (image_map))
27881 && (hotspot = find_hot_spot (image_map, dx, dy),
27882 CONSP (hotspot))
27883 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27885 Lisp_Object plist;
27887 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27888 If so, we could look for mouse-enter, mouse-leave
27889 properties in PLIST (and do something...). */
27890 hotspot = XCDR (hotspot);
27891 if (CONSP (hotspot)
27892 && (plist = XCAR (hotspot), CONSP (plist)))
27894 pointer = Fplist_get (plist, Qpointer);
27895 if (NILP (pointer))
27896 pointer = Qhand;
27897 help = Fplist_get (plist, Qhelp_echo);
27898 if (!NILP (help))
27900 help_echo_string = help;
27901 XSETWINDOW (help_echo_window, w);
27902 help_echo_object = w->contents;
27903 help_echo_pos = charpos;
27907 if (NILP (pointer))
27908 pointer = Fplist_get (XCDR (object), QCpointer);
27910 #endif /* HAVE_WINDOW_SYSTEM */
27912 if (STRINGP (string))
27913 pos = make_number (charpos);
27915 /* Set the help text and mouse pointer. If the mouse is on a part
27916 of the mode line without any text (e.g. past the right edge of
27917 the mode line text), use the default help text and pointer. */
27918 if (STRINGP (string) || area == ON_MODE_LINE)
27920 /* Arrange to display the help by setting the global variables
27921 help_echo_string, help_echo_object, and help_echo_pos. */
27922 if (NILP (help))
27924 if (STRINGP (string))
27925 help = Fget_text_property (pos, Qhelp_echo, string);
27927 if (!NILP (help))
27929 help_echo_string = help;
27930 XSETWINDOW (help_echo_window, w);
27931 help_echo_object = string;
27932 help_echo_pos = charpos;
27934 else if (area == ON_MODE_LINE)
27936 Lisp_Object default_help
27937 = buffer_local_value_1 (Qmode_line_default_help_echo,
27938 w->contents);
27940 if (STRINGP (default_help))
27942 help_echo_string = default_help;
27943 XSETWINDOW (help_echo_window, w);
27944 help_echo_object = Qnil;
27945 help_echo_pos = -1;
27950 #ifdef HAVE_WINDOW_SYSTEM
27951 /* Change the mouse pointer according to what is under it. */
27952 if (FRAME_WINDOW_P (f))
27954 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27955 if (STRINGP (string))
27957 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27959 if (NILP (pointer))
27960 pointer = Fget_text_property (pos, Qpointer, string);
27962 /* Change the mouse pointer according to what is under X/Y. */
27963 if (NILP (pointer)
27964 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27966 Lisp_Object map;
27967 map = Fget_text_property (pos, Qlocal_map, string);
27968 if (!KEYMAPP (map))
27969 map = Fget_text_property (pos, Qkeymap, string);
27970 if (!KEYMAPP (map))
27971 cursor = dpyinfo->vertical_scroll_bar_cursor;
27974 else
27975 /* Default mode-line pointer. */
27976 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27978 #endif
27981 /* Change the mouse face according to what is under X/Y. */
27982 if (STRINGP (string))
27984 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27985 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27986 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27987 && glyph)
27989 Lisp_Object b, e;
27991 struct glyph * tmp_glyph;
27993 int gpos;
27994 int gseq_length;
27995 int total_pixel_width;
27996 ptrdiff_t begpos, endpos, ignore;
27998 int vpos, hpos;
28000 b = Fprevious_single_property_change (make_number (charpos + 1),
28001 Qmouse_face, string, Qnil);
28002 if (NILP (b))
28003 begpos = 0;
28004 else
28005 begpos = XINT (b);
28007 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28008 if (NILP (e))
28009 endpos = SCHARS (string);
28010 else
28011 endpos = XINT (e);
28013 /* Calculate the glyph position GPOS of GLYPH in the
28014 displayed string, relative to the beginning of the
28015 highlighted part of the string.
28017 Note: GPOS is different from CHARPOS. CHARPOS is the
28018 position of GLYPH in the internal string object. A mode
28019 line string format has structures which are converted to
28020 a flattened string by the Emacs Lisp interpreter. The
28021 internal string is an element of those structures. The
28022 displayed string is the flattened string. */
28023 tmp_glyph = row_start_glyph;
28024 while (tmp_glyph < glyph
28025 && (!(EQ (tmp_glyph->object, glyph->object)
28026 && begpos <= tmp_glyph->charpos
28027 && tmp_glyph->charpos < endpos)))
28028 tmp_glyph++;
28029 gpos = glyph - tmp_glyph;
28031 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28032 the highlighted part of the displayed string to which
28033 GLYPH belongs. Note: GSEQ_LENGTH is different from
28034 SCHARS (STRING), because the latter returns the length of
28035 the internal string. */
28036 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28037 tmp_glyph > glyph
28038 && (!(EQ (tmp_glyph->object, glyph->object)
28039 && begpos <= tmp_glyph->charpos
28040 && tmp_glyph->charpos < endpos));
28041 tmp_glyph--)
28043 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28045 /* Calculate the total pixel width of all the glyphs between
28046 the beginning of the highlighted area and GLYPH. */
28047 total_pixel_width = 0;
28048 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28049 total_pixel_width += tmp_glyph->pixel_width;
28051 /* Pre calculation of re-rendering position. Note: X is in
28052 column units here, after the call to mode_line_string or
28053 marginal_area_string. */
28054 hpos = x - gpos;
28055 vpos = (area == ON_MODE_LINE
28056 ? (w->current_matrix)->nrows - 1
28057 : 0);
28059 /* If GLYPH's position is included in the region that is
28060 already drawn in mouse face, we have nothing to do. */
28061 if ( EQ (window, hlinfo->mouse_face_window)
28062 && (!row->reversed_p
28063 ? (hlinfo->mouse_face_beg_col <= hpos
28064 && hpos < hlinfo->mouse_face_end_col)
28065 /* In R2L rows we swap BEG and END, see below. */
28066 : (hlinfo->mouse_face_end_col <= hpos
28067 && hpos < hlinfo->mouse_face_beg_col))
28068 && hlinfo->mouse_face_beg_row == vpos )
28069 return;
28071 if (clear_mouse_face (hlinfo))
28072 cursor = No_Cursor;
28074 if (!row->reversed_p)
28076 hlinfo->mouse_face_beg_col = hpos;
28077 hlinfo->mouse_face_beg_x = original_x_pixel
28078 - (total_pixel_width + dx);
28079 hlinfo->mouse_face_end_col = hpos + gseq_length;
28080 hlinfo->mouse_face_end_x = 0;
28082 else
28084 /* In R2L rows, show_mouse_face expects BEG and END
28085 coordinates to be swapped. */
28086 hlinfo->mouse_face_end_col = hpos;
28087 hlinfo->mouse_face_end_x = original_x_pixel
28088 - (total_pixel_width + dx);
28089 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28090 hlinfo->mouse_face_beg_x = 0;
28093 hlinfo->mouse_face_beg_row = vpos;
28094 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28095 hlinfo->mouse_face_past_end = 0;
28096 hlinfo->mouse_face_window = window;
28098 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28099 charpos,
28100 0, 0, 0,
28101 &ignore,
28102 glyph->face_id,
28104 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28106 if (NILP (pointer))
28107 pointer = Qhand;
28109 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28110 clear_mouse_face (hlinfo);
28112 #ifdef HAVE_WINDOW_SYSTEM
28113 if (FRAME_WINDOW_P (f))
28114 define_frame_cursor1 (f, cursor, pointer);
28115 #endif
28119 /* EXPORT:
28120 Take proper action when the mouse has moved to position X, Y on
28121 frame F with regards to highlighting portions of display that have
28122 mouse-face properties. Also de-highlight portions of display where
28123 the mouse was before, set the mouse pointer shape as appropriate
28124 for the mouse coordinates, and activate help echo (tooltips).
28125 X and Y can be negative or out of range. */
28127 void
28128 note_mouse_highlight (struct frame *f, int x, int y)
28130 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28131 enum window_part part = ON_NOTHING;
28132 Lisp_Object window;
28133 struct window *w;
28134 Cursor cursor = No_Cursor;
28135 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28136 struct buffer *b;
28138 /* When a menu is active, don't highlight because this looks odd. */
28139 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28140 if (popup_activated ())
28141 return;
28142 #endif
28144 if (!f->glyphs_initialized_p
28145 || f->pointer_invisible)
28146 return;
28148 hlinfo->mouse_face_mouse_x = x;
28149 hlinfo->mouse_face_mouse_y = y;
28150 hlinfo->mouse_face_mouse_frame = f;
28152 if (hlinfo->mouse_face_defer)
28153 return;
28155 /* Which window is that in? */
28156 window = window_from_coordinates (f, x, y, &part, 1);
28158 /* If displaying active text in another window, clear that. */
28159 if (! EQ (window, hlinfo->mouse_face_window)
28160 /* Also clear if we move out of text area in same window. */
28161 || (!NILP (hlinfo->mouse_face_window)
28162 && !NILP (window)
28163 && part != ON_TEXT
28164 && part != ON_MODE_LINE
28165 && part != ON_HEADER_LINE))
28166 clear_mouse_face (hlinfo);
28168 /* Not on a window -> return. */
28169 if (!WINDOWP (window))
28170 return;
28172 /* Reset help_echo_string. It will get recomputed below. */
28173 help_echo_string = Qnil;
28175 /* Convert to window-relative pixel coordinates. */
28176 w = XWINDOW (window);
28177 frame_to_window_pixel_xy (w, &x, &y);
28179 #ifdef HAVE_WINDOW_SYSTEM
28180 /* Handle tool-bar window differently since it doesn't display a
28181 buffer. */
28182 if (EQ (window, f->tool_bar_window))
28184 note_tool_bar_highlight (f, x, y);
28185 return;
28187 #endif
28189 /* Mouse is on the mode, header line or margin? */
28190 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28191 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28193 note_mode_line_or_margin_highlight (window, x, y, part);
28194 return;
28197 #ifdef HAVE_WINDOW_SYSTEM
28198 if (part == ON_VERTICAL_BORDER)
28200 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28201 help_echo_string = build_string ("drag-mouse-1: resize");
28203 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28204 || part == ON_SCROLL_BAR)
28205 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28206 else
28207 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28208 #endif
28210 /* Are we in a window whose display is up to date?
28211 And verify the buffer's text has not changed. */
28212 b = XBUFFER (w->contents);
28213 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28215 int hpos, vpos, dx, dy, area = LAST_AREA;
28216 ptrdiff_t pos;
28217 struct glyph *glyph;
28218 Lisp_Object object;
28219 Lisp_Object mouse_face = Qnil, position;
28220 Lisp_Object *overlay_vec = NULL;
28221 ptrdiff_t i, noverlays;
28222 struct buffer *obuf;
28223 ptrdiff_t obegv, ozv;
28224 int same_region;
28226 /* Find the glyph under X/Y. */
28227 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28229 #ifdef HAVE_WINDOW_SYSTEM
28230 /* Look for :pointer property on image. */
28231 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28233 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28234 if (img != NULL && IMAGEP (img->spec))
28236 Lisp_Object image_map, hotspot;
28237 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28238 !NILP (image_map))
28239 && (hotspot = find_hot_spot (image_map,
28240 glyph->slice.img.x + dx,
28241 glyph->slice.img.y + dy),
28242 CONSP (hotspot))
28243 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28245 Lisp_Object plist;
28247 /* Could check XCAR (hotspot) to see if we enter/leave
28248 this hot-spot.
28249 If so, we could look for mouse-enter, mouse-leave
28250 properties in PLIST (and do something...). */
28251 hotspot = XCDR (hotspot);
28252 if (CONSP (hotspot)
28253 && (plist = XCAR (hotspot), CONSP (plist)))
28255 pointer = Fplist_get (plist, Qpointer);
28256 if (NILP (pointer))
28257 pointer = Qhand;
28258 help_echo_string = Fplist_get (plist, Qhelp_echo);
28259 if (!NILP (help_echo_string))
28261 help_echo_window = window;
28262 help_echo_object = glyph->object;
28263 help_echo_pos = glyph->charpos;
28267 if (NILP (pointer))
28268 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28271 #endif /* HAVE_WINDOW_SYSTEM */
28273 /* Clear mouse face if X/Y not over text. */
28274 if (glyph == NULL
28275 || area != TEXT_AREA
28276 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28277 /* Glyph's OBJECT is an integer for glyphs inserted by the
28278 display engine for its internal purposes, like truncation
28279 and continuation glyphs and blanks beyond the end of
28280 line's text on text terminals. If we are over such a
28281 glyph, we are not over any text. */
28282 || INTEGERP (glyph->object)
28283 /* R2L rows have a stretch glyph at their front, which
28284 stands for no text, whereas L2R rows have no glyphs at
28285 all beyond the end of text. Treat such stretch glyphs
28286 like we do with NULL glyphs in L2R rows. */
28287 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28288 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28289 && glyph->type == STRETCH_GLYPH
28290 && glyph->avoid_cursor_p))
28292 if (clear_mouse_face (hlinfo))
28293 cursor = No_Cursor;
28294 #ifdef HAVE_WINDOW_SYSTEM
28295 if (FRAME_WINDOW_P (f) && NILP (pointer))
28297 if (area != TEXT_AREA)
28298 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28299 else
28300 pointer = Vvoid_text_area_pointer;
28302 #endif
28303 goto set_cursor;
28306 pos = glyph->charpos;
28307 object = glyph->object;
28308 if (!STRINGP (object) && !BUFFERP (object))
28309 goto set_cursor;
28311 /* If we get an out-of-range value, return now; avoid an error. */
28312 if (BUFFERP (object) && pos > BUF_Z (b))
28313 goto set_cursor;
28315 /* Make the window's buffer temporarily current for
28316 overlays_at and compute_char_face. */
28317 obuf = current_buffer;
28318 current_buffer = b;
28319 obegv = BEGV;
28320 ozv = ZV;
28321 BEGV = BEG;
28322 ZV = Z;
28324 /* Is this char mouse-active or does it have help-echo? */
28325 position = make_number (pos);
28327 if (BUFFERP (object))
28329 /* Put all the overlays we want in a vector in overlay_vec. */
28330 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28331 /* Sort overlays into increasing priority order. */
28332 noverlays = sort_overlays (overlay_vec, noverlays, w);
28334 else
28335 noverlays = 0;
28337 if (NILP (Vmouse_highlight))
28339 clear_mouse_face (hlinfo);
28340 goto check_help_echo;
28343 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28345 if (same_region)
28346 cursor = No_Cursor;
28348 /* Check mouse-face highlighting. */
28349 if (! same_region
28350 /* If there exists an overlay with mouse-face overlapping
28351 the one we are currently highlighting, we have to
28352 check if we enter the overlapping overlay, and then
28353 highlight only that. */
28354 || (OVERLAYP (hlinfo->mouse_face_overlay)
28355 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28357 /* Find the highest priority overlay with a mouse-face. */
28358 Lisp_Object overlay = Qnil;
28359 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28361 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28362 if (!NILP (mouse_face))
28363 overlay = overlay_vec[i];
28366 /* If we're highlighting the same overlay as before, there's
28367 no need to do that again. */
28368 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28369 goto check_help_echo;
28370 hlinfo->mouse_face_overlay = overlay;
28372 /* Clear the display of the old active region, if any. */
28373 if (clear_mouse_face (hlinfo))
28374 cursor = No_Cursor;
28376 /* If no overlay applies, get a text property. */
28377 if (NILP (overlay))
28378 mouse_face = Fget_text_property (position, Qmouse_face, object);
28380 /* Next, compute the bounds of the mouse highlighting and
28381 display it. */
28382 if (!NILP (mouse_face) && STRINGP (object))
28384 /* The mouse-highlighting comes from a display string
28385 with a mouse-face. */
28386 Lisp_Object s, e;
28387 ptrdiff_t ignore;
28389 s = Fprevious_single_property_change
28390 (make_number (pos + 1), Qmouse_face, object, Qnil);
28391 e = Fnext_single_property_change
28392 (position, Qmouse_face, object, Qnil);
28393 if (NILP (s))
28394 s = make_number (0);
28395 if (NILP (e))
28396 e = make_number (SCHARS (object) - 1);
28397 mouse_face_from_string_pos (w, hlinfo, object,
28398 XINT (s), XINT (e));
28399 hlinfo->mouse_face_past_end = 0;
28400 hlinfo->mouse_face_window = window;
28401 hlinfo->mouse_face_face_id
28402 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28403 glyph->face_id, 1);
28404 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28405 cursor = No_Cursor;
28407 else
28409 /* The mouse-highlighting, if any, comes from an overlay
28410 or text property in the buffer. */
28411 Lisp_Object buffer IF_LINT (= Qnil);
28412 Lisp_Object disp_string IF_LINT (= Qnil);
28414 if (STRINGP (object))
28416 /* If we are on a display string with no mouse-face,
28417 check if the text under it has one. */
28418 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28419 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28420 pos = string_buffer_position (object, start);
28421 if (pos > 0)
28423 mouse_face = get_char_property_and_overlay
28424 (make_number (pos), Qmouse_face, w->contents, &overlay);
28425 buffer = w->contents;
28426 disp_string = object;
28429 else
28431 buffer = object;
28432 disp_string = Qnil;
28435 if (!NILP (mouse_face))
28437 Lisp_Object before, after;
28438 Lisp_Object before_string, after_string;
28439 /* To correctly find the limits of mouse highlight
28440 in a bidi-reordered buffer, we must not use the
28441 optimization of limiting the search in
28442 previous-single-property-change and
28443 next-single-property-change, because
28444 rows_from_pos_range needs the real start and end
28445 positions to DTRT in this case. That's because
28446 the first row visible in a window does not
28447 necessarily display the character whose position
28448 is the smallest. */
28449 Lisp_Object lim1 =
28450 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28451 ? Fmarker_position (w->start)
28452 : Qnil;
28453 Lisp_Object lim2 =
28454 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28455 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28456 : Qnil;
28458 if (NILP (overlay))
28460 /* Handle the text property case. */
28461 before = Fprevious_single_property_change
28462 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28463 after = Fnext_single_property_change
28464 (make_number (pos), Qmouse_face, buffer, lim2);
28465 before_string = after_string = Qnil;
28467 else
28469 /* Handle the overlay case. */
28470 before = Foverlay_start (overlay);
28471 after = Foverlay_end (overlay);
28472 before_string = Foverlay_get (overlay, Qbefore_string);
28473 after_string = Foverlay_get (overlay, Qafter_string);
28475 if (!STRINGP (before_string)) before_string = Qnil;
28476 if (!STRINGP (after_string)) after_string = Qnil;
28479 mouse_face_from_buffer_pos (window, hlinfo, pos,
28480 NILP (before)
28482 : XFASTINT (before),
28483 NILP (after)
28484 ? BUF_Z (XBUFFER (buffer))
28485 : XFASTINT (after),
28486 before_string, after_string,
28487 disp_string);
28488 cursor = No_Cursor;
28493 check_help_echo:
28495 /* Look for a `help-echo' property. */
28496 if (NILP (help_echo_string)) {
28497 Lisp_Object help, overlay;
28499 /* Check overlays first. */
28500 help = overlay = Qnil;
28501 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28503 overlay = overlay_vec[i];
28504 help = Foverlay_get (overlay, Qhelp_echo);
28507 if (!NILP (help))
28509 help_echo_string = help;
28510 help_echo_window = window;
28511 help_echo_object = overlay;
28512 help_echo_pos = pos;
28514 else
28516 Lisp_Object obj = glyph->object;
28517 ptrdiff_t charpos = glyph->charpos;
28519 /* Try text properties. */
28520 if (STRINGP (obj)
28521 && charpos >= 0
28522 && charpos < SCHARS (obj))
28524 help = Fget_text_property (make_number (charpos),
28525 Qhelp_echo, obj);
28526 if (NILP (help))
28528 /* If the string itself doesn't specify a help-echo,
28529 see if the buffer text ``under'' it does. */
28530 struct glyph_row *r
28531 = MATRIX_ROW (w->current_matrix, vpos);
28532 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28533 ptrdiff_t p = string_buffer_position (obj, start);
28534 if (p > 0)
28536 help = Fget_char_property (make_number (p),
28537 Qhelp_echo, w->contents);
28538 if (!NILP (help))
28540 charpos = p;
28541 obj = w->contents;
28546 else if (BUFFERP (obj)
28547 && charpos >= BEGV
28548 && charpos < ZV)
28549 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28550 obj);
28552 if (!NILP (help))
28554 help_echo_string = help;
28555 help_echo_window = window;
28556 help_echo_object = obj;
28557 help_echo_pos = charpos;
28562 #ifdef HAVE_WINDOW_SYSTEM
28563 /* Look for a `pointer' property. */
28564 if (FRAME_WINDOW_P (f) && NILP (pointer))
28566 /* Check overlays first. */
28567 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28568 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28570 if (NILP (pointer))
28572 Lisp_Object obj = glyph->object;
28573 ptrdiff_t charpos = glyph->charpos;
28575 /* Try text properties. */
28576 if (STRINGP (obj)
28577 && charpos >= 0
28578 && charpos < SCHARS (obj))
28580 pointer = Fget_text_property (make_number (charpos),
28581 Qpointer, obj);
28582 if (NILP (pointer))
28584 /* If the string itself doesn't specify a pointer,
28585 see if the buffer text ``under'' it does. */
28586 struct glyph_row *r
28587 = MATRIX_ROW (w->current_matrix, vpos);
28588 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28589 ptrdiff_t p = string_buffer_position (obj, start);
28590 if (p > 0)
28591 pointer = Fget_char_property (make_number (p),
28592 Qpointer, w->contents);
28595 else if (BUFFERP (obj)
28596 && charpos >= BEGV
28597 && charpos < ZV)
28598 pointer = Fget_text_property (make_number (charpos),
28599 Qpointer, obj);
28602 #endif /* HAVE_WINDOW_SYSTEM */
28604 BEGV = obegv;
28605 ZV = ozv;
28606 current_buffer = obuf;
28609 set_cursor:
28611 #ifdef HAVE_WINDOW_SYSTEM
28612 if (FRAME_WINDOW_P (f))
28613 define_frame_cursor1 (f, cursor, pointer);
28614 #else
28615 /* This is here to prevent a compiler error, about "label at end of
28616 compound statement". */
28617 return;
28618 #endif
28622 /* EXPORT for RIF:
28623 Clear any mouse-face on window W. This function is part of the
28624 redisplay interface, and is called from try_window_id and similar
28625 functions to ensure the mouse-highlight is off. */
28627 void
28628 x_clear_window_mouse_face (struct window *w)
28630 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28631 Lisp_Object window;
28633 block_input ();
28634 XSETWINDOW (window, w);
28635 if (EQ (window, hlinfo->mouse_face_window))
28636 clear_mouse_face (hlinfo);
28637 unblock_input ();
28641 /* EXPORT:
28642 Just discard the mouse face information for frame F, if any.
28643 This is used when the size of F is changed. */
28645 void
28646 cancel_mouse_face (struct frame *f)
28648 Lisp_Object window;
28649 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28651 window = hlinfo->mouse_face_window;
28652 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28653 reset_mouse_highlight (hlinfo);
28658 /***********************************************************************
28659 Exposure Events
28660 ***********************************************************************/
28662 #ifdef HAVE_WINDOW_SYSTEM
28664 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28665 which intersects rectangle R. R is in window-relative coordinates. */
28667 static void
28668 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28669 enum glyph_row_area area)
28671 struct glyph *first = row->glyphs[area];
28672 struct glyph *end = row->glyphs[area] + row->used[area];
28673 struct glyph *last;
28674 int first_x, start_x, x;
28676 if (area == TEXT_AREA && row->fill_line_p)
28677 /* If row extends face to end of line write the whole line. */
28678 draw_glyphs (w, 0, row, area,
28679 0, row->used[area],
28680 DRAW_NORMAL_TEXT, 0);
28681 else
28683 /* Set START_X to the window-relative start position for drawing glyphs of
28684 AREA. The first glyph of the text area can be partially visible.
28685 The first glyphs of other areas cannot. */
28686 start_x = window_box_left_offset (w, area);
28687 x = start_x;
28688 if (area == TEXT_AREA)
28689 x += row->x;
28691 /* Find the first glyph that must be redrawn. */
28692 while (first < end
28693 && x + first->pixel_width < r->x)
28695 x += first->pixel_width;
28696 ++first;
28699 /* Find the last one. */
28700 last = first;
28701 first_x = x;
28702 while (last < end
28703 && x < r->x + r->width)
28705 x += last->pixel_width;
28706 ++last;
28709 /* Repaint. */
28710 if (last > first)
28711 draw_glyphs (w, first_x - start_x, row, area,
28712 first - row->glyphs[area], last - row->glyphs[area],
28713 DRAW_NORMAL_TEXT, 0);
28718 /* Redraw the parts of the glyph row ROW on window W intersecting
28719 rectangle R. R is in window-relative coordinates. Value is
28720 non-zero if mouse-face was overwritten. */
28722 static int
28723 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28725 eassert (row->enabled_p);
28727 if (row->mode_line_p || w->pseudo_window_p)
28728 draw_glyphs (w, 0, row, TEXT_AREA,
28729 0, row->used[TEXT_AREA],
28730 DRAW_NORMAL_TEXT, 0);
28731 else
28733 if (row->used[LEFT_MARGIN_AREA])
28734 expose_area (w, row, r, LEFT_MARGIN_AREA);
28735 if (row->used[TEXT_AREA])
28736 expose_area (w, row, r, TEXT_AREA);
28737 if (row->used[RIGHT_MARGIN_AREA])
28738 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28739 draw_row_fringe_bitmaps (w, row);
28742 return row->mouse_face_p;
28746 /* Redraw those parts of glyphs rows during expose event handling that
28747 overlap other rows. Redrawing of an exposed line writes over parts
28748 of lines overlapping that exposed line; this function fixes that.
28750 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28751 row in W's current matrix that is exposed and overlaps other rows.
28752 LAST_OVERLAPPING_ROW is the last such row. */
28754 static void
28755 expose_overlaps (struct window *w,
28756 struct glyph_row *first_overlapping_row,
28757 struct glyph_row *last_overlapping_row,
28758 XRectangle *r)
28760 struct glyph_row *row;
28762 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28763 if (row->overlapping_p)
28765 eassert (row->enabled_p && !row->mode_line_p);
28767 row->clip = r;
28768 if (row->used[LEFT_MARGIN_AREA])
28769 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28771 if (row->used[TEXT_AREA])
28772 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28774 if (row->used[RIGHT_MARGIN_AREA])
28775 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28776 row->clip = NULL;
28781 /* Return non-zero if W's cursor intersects rectangle R. */
28783 static int
28784 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28786 XRectangle cr, result;
28787 struct glyph *cursor_glyph;
28788 struct glyph_row *row;
28790 if (w->phys_cursor.vpos >= 0
28791 && w->phys_cursor.vpos < w->current_matrix->nrows
28792 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28793 row->enabled_p)
28794 && row->cursor_in_fringe_p)
28796 /* Cursor is in the fringe. */
28797 cr.x = window_box_right_offset (w,
28798 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28799 ? RIGHT_MARGIN_AREA
28800 : TEXT_AREA));
28801 cr.y = row->y;
28802 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28803 cr.height = row->height;
28804 return x_intersect_rectangles (&cr, r, &result);
28807 cursor_glyph = get_phys_cursor_glyph (w);
28808 if (cursor_glyph)
28810 /* r is relative to W's box, but w->phys_cursor.x is relative
28811 to left edge of W's TEXT area. Adjust it. */
28812 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28813 cr.y = w->phys_cursor.y;
28814 cr.width = cursor_glyph->pixel_width;
28815 cr.height = w->phys_cursor_height;
28816 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28817 I assume the effect is the same -- and this is portable. */
28818 return x_intersect_rectangles (&cr, r, &result);
28820 /* If we don't understand the format, pretend we're not in the hot-spot. */
28821 return 0;
28825 /* EXPORT:
28826 Draw a vertical window border to the right of window W if W doesn't
28827 have vertical scroll bars. */
28829 void
28830 x_draw_vertical_border (struct window *w)
28832 struct frame *f = XFRAME (WINDOW_FRAME (w));
28834 /* We could do better, if we knew what type of scroll-bar the adjacent
28835 windows (on either side) have... But we don't :-(
28836 However, I think this works ok. ++KFS 2003-04-25 */
28838 /* Redraw borders between horizontally adjacent windows. Don't
28839 do it for frames with vertical scroll bars because either the
28840 right scroll bar of a window, or the left scroll bar of its
28841 neighbor will suffice as a border. */
28842 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28843 return;
28845 /* Note: It is necessary to redraw both the left and the right
28846 borders, for when only this single window W is being
28847 redisplayed. */
28848 if (!WINDOW_RIGHTMOST_P (w)
28849 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28851 int x0, x1, y0, y1;
28853 window_box_edges (w, &x0, &y0, &x1, &y1);
28854 y1 -= 1;
28856 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28857 x1 -= 1;
28859 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28861 if (!WINDOW_LEFTMOST_P (w)
28862 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28864 int x0, x1, y0, y1;
28866 window_box_edges (w, &x0, &y0, &x1, &y1);
28867 y1 -= 1;
28869 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28870 x0 -= 1;
28872 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28877 /* Redraw the part of window W intersection rectangle FR. Pixel
28878 coordinates in FR are frame-relative. Call this function with
28879 input blocked. Value is non-zero if the exposure overwrites
28880 mouse-face. */
28882 static int
28883 expose_window (struct window *w, XRectangle *fr)
28885 struct frame *f = XFRAME (w->frame);
28886 XRectangle wr, r;
28887 int mouse_face_overwritten_p = 0;
28889 /* If window is not yet fully initialized, do nothing. This can
28890 happen when toolkit scroll bars are used and a window is split.
28891 Reconfiguring the scroll bar will generate an expose for a newly
28892 created window. */
28893 if (w->current_matrix == NULL)
28894 return 0;
28896 /* When we're currently updating the window, display and current
28897 matrix usually don't agree. Arrange for a thorough display
28898 later. */
28899 if (w->must_be_updated_p)
28901 SET_FRAME_GARBAGED (f);
28902 return 0;
28905 /* Frame-relative pixel rectangle of W. */
28906 wr.x = WINDOW_LEFT_EDGE_X (w);
28907 wr.y = WINDOW_TOP_EDGE_Y (w);
28908 wr.width = WINDOW_TOTAL_WIDTH (w);
28909 wr.height = WINDOW_TOTAL_HEIGHT (w);
28911 if (x_intersect_rectangles (fr, &wr, &r))
28913 int yb = window_text_bottom_y (w);
28914 struct glyph_row *row;
28915 int cursor_cleared_p, phys_cursor_on_p;
28916 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28918 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28919 r.x, r.y, r.width, r.height));
28921 /* Convert to window coordinates. */
28922 r.x -= WINDOW_LEFT_EDGE_X (w);
28923 r.y -= WINDOW_TOP_EDGE_Y (w);
28925 /* Turn off the cursor. */
28926 if (!w->pseudo_window_p
28927 && phys_cursor_in_rect_p (w, &r))
28929 x_clear_cursor (w);
28930 cursor_cleared_p = 1;
28932 else
28933 cursor_cleared_p = 0;
28935 /* If the row containing the cursor extends face to end of line,
28936 then expose_area might overwrite the cursor outside the
28937 rectangle and thus notice_overwritten_cursor might clear
28938 w->phys_cursor_on_p. We remember the original value and
28939 check later if it is changed. */
28940 phys_cursor_on_p = w->phys_cursor_on_p;
28942 /* Update lines intersecting rectangle R. */
28943 first_overlapping_row = last_overlapping_row = NULL;
28944 for (row = w->current_matrix->rows;
28945 row->enabled_p;
28946 ++row)
28948 int y0 = row->y;
28949 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28951 if ((y0 >= r.y && y0 < r.y + r.height)
28952 || (y1 > r.y && y1 < r.y + r.height)
28953 || (r.y >= y0 && r.y < y1)
28954 || (r.y + r.height > y0 && r.y + r.height < y1))
28956 /* A header line may be overlapping, but there is no need
28957 to fix overlapping areas for them. KFS 2005-02-12 */
28958 if (row->overlapping_p && !row->mode_line_p)
28960 if (first_overlapping_row == NULL)
28961 first_overlapping_row = row;
28962 last_overlapping_row = row;
28965 row->clip = fr;
28966 if (expose_line (w, row, &r))
28967 mouse_face_overwritten_p = 1;
28968 row->clip = NULL;
28970 else if (row->overlapping_p)
28972 /* We must redraw a row overlapping the exposed area. */
28973 if (y0 < r.y
28974 ? y0 + row->phys_height > r.y
28975 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28977 if (first_overlapping_row == NULL)
28978 first_overlapping_row = row;
28979 last_overlapping_row = row;
28983 if (y1 >= yb)
28984 break;
28987 /* Display the mode line if there is one. */
28988 if (WINDOW_WANTS_MODELINE_P (w)
28989 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28990 row->enabled_p)
28991 && row->y < r.y + r.height)
28993 if (expose_line (w, row, &r))
28994 mouse_face_overwritten_p = 1;
28997 if (!w->pseudo_window_p)
28999 /* Fix the display of overlapping rows. */
29000 if (first_overlapping_row)
29001 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29002 fr);
29004 /* Draw border between windows. */
29005 x_draw_vertical_border (w);
29007 /* Turn the cursor on again. */
29008 if (cursor_cleared_p
29009 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29010 update_window_cursor (w, 1);
29014 return mouse_face_overwritten_p;
29019 /* Redraw (parts) of all windows in the window tree rooted at W that
29020 intersect R. R contains frame pixel coordinates. Value is
29021 non-zero if the exposure overwrites mouse-face. */
29023 static int
29024 expose_window_tree (struct window *w, XRectangle *r)
29026 struct frame *f = XFRAME (w->frame);
29027 int mouse_face_overwritten_p = 0;
29029 while (w && !FRAME_GARBAGED_P (f))
29031 if (WINDOWP (w->contents))
29032 mouse_face_overwritten_p
29033 |= expose_window_tree (XWINDOW (w->contents), r);
29034 else
29035 mouse_face_overwritten_p |= expose_window (w, r);
29037 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29040 return mouse_face_overwritten_p;
29044 /* EXPORT:
29045 Redisplay an exposed area of frame F. X and Y are the upper-left
29046 corner of the exposed rectangle. W and H are width and height of
29047 the exposed area. All are pixel values. W or H zero means redraw
29048 the entire frame. */
29050 void
29051 expose_frame (struct frame *f, int x, int y, int w, int h)
29053 XRectangle r;
29054 int mouse_face_overwritten_p = 0;
29056 TRACE ((stderr, "expose_frame "));
29058 /* No need to redraw if frame will be redrawn soon. */
29059 if (FRAME_GARBAGED_P (f))
29061 TRACE ((stderr, " garbaged\n"));
29062 return;
29065 /* If basic faces haven't been realized yet, there is no point in
29066 trying to redraw anything. This can happen when we get an expose
29067 event while Emacs is starting, e.g. by moving another window. */
29068 if (FRAME_FACE_CACHE (f) == NULL
29069 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29071 TRACE ((stderr, " no faces\n"));
29072 return;
29075 if (w == 0 || h == 0)
29077 r.x = r.y = 0;
29078 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29079 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29081 else
29083 r.x = x;
29084 r.y = y;
29085 r.width = w;
29086 r.height = h;
29089 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29090 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29092 if (WINDOWP (f->tool_bar_window))
29093 mouse_face_overwritten_p
29094 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29096 #ifdef HAVE_X_WINDOWS
29097 #ifndef MSDOS
29098 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29099 if (WINDOWP (f->menu_bar_window))
29100 mouse_face_overwritten_p
29101 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29102 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29103 #endif
29104 #endif
29106 /* Some window managers support a focus-follows-mouse style with
29107 delayed raising of frames. Imagine a partially obscured frame,
29108 and moving the mouse into partially obscured mouse-face on that
29109 frame. The visible part of the mouse-face will be highlighted,
29110 then the WM raises the obscured frame. With at least one WM, KDE
29111 2.1, Emacs is not getting any event for the raising of the frame
29112 (even tried with SubstructureRedirectMask), only Expose events.
29113 These expose events will draw text normally, i.e. not
29114 highlighted. Which means we must redo the highlight here.
29115 Subsume it under ``we love X''. --gerd 2001-08-15 */
29116 /* Included in Windows version because Windows most likely does not
29117 do the right thing if any third party tool offers
29118 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29119 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29121 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29122 if (f == hlinfo->mouse_face_mouse_frame)
29124 int mouse_x = hlinfo->mouse_face_mouse_x;
29125 int mouse_y = hlinfo->mouse_face_mouse_y;
29126 clear_mouse_face (hlinfo);
29127 note_mouse_highlight (f, mouse_x, mouse_y);
29133 /* EXPORT:
29134 Determine the intersection of two rectangles R1 and R2. Return
29135 the intersection in *RESULT. Value is non-zero if RESULT is not
29136 empty. */
29139 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29141 XRectangle *left, *right;
29142 XRectangle *upper, *lower;
29143 int intersection_p = 0;
29145 /* Rearrange so that R1 is the left-most rectangle. */
29146 if (r1->x < r2->x)
29147 left = r1, right = r2;
29148 else
29149 left = r2, right = r1;
29151 /* X0 of the intersection is right.x0, if this is inside R1,
29152 otherwise there is no intersection. */
29153 if (right->x <= left->x + left->width)
29155 result->x = right->x;
29157 /* The right end of the intersection is the minimum of
29158 the right ends of left and right. */
29159 result->width = (min (left->x + left->width, right->x + right->width)
29160 - result->x);
29162 /* Same game for Y. */
29163 if (r1->y < r2->y)
29164 upper = r1, lower = r2;
29165 else
29166 upper = r2, lower = r1;
29168 /* The upper end of the intersection is lower.y0, if this is inside
29169 of upper. Otherwise, there is no intersection. */
29170 if (lower->y <= upper->y + upper->height)
29172 result->y = lower->y;
29174 /* The lower end of the intersection is the minimum of the lower
29175 ends of upper and lower. */
29176 result->height = (min (lower->y + lower->height,
29177 upper->y + upper->height)
29178 - result->y);
29179 intersection_p = 1;
29183 return intersection_p;
29186 #endif /* HAVE_WINDOW_SYSTEM */
29189 /***********************************************************************
29190 Initialization
29191 ***********************************************************************/
29193 void
29194 syms_of_xdisp (void)
29196 Vwith_echo_area_save_vector = Qnil;
29197 staticpro (&Vwith_echo_area_save_vector);
29199 Vmessage_stack = Qnil;
29200 staticpro (&Vmessage_stack);
29202 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29203 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29205 message_dolog_marker1 = Fmake_marker ();
29206 staticpro (&message_dolog_marker1);
29207 message_dolog_marker2 = Fmake_marker ();
29208 staticpro (&message_dolog_marker2);
29209 message_dolog_marker3 = Fmake_marker ();
29210 staticpro (&message_dolog_marker3);
29212 #ifdef GLYPH_DEBUG
29213 defsubr (&Sdump_frame_glyph_matrix);
29214 defsubr (&Sdump_glyph_matrix);
29215 defsubr (&Sdump_glyph_row);
29216 defsubr (&Sdump_tool_bar_row);
29217 defsubr (&Strace_redisplay);
29218 defsubr (&Strace_to_stderr);
29219 #endif
29220 #ifdef HAVE_WINDOW_SYSTEM
29221 defsubr (&Stool_bar_lines_needed);
29222 defsubr (&Slookup_image_map);
29223 #endif
29224 defsubr (&Sline_pixel_height);
29225 defsubr (&Sformat_mode_line);
29226 defsubr (&Sinvisible_p);
29227 defsubr (&Scurrent_bidi_paragraph_direction);
29228 defsubr (&Smove_point_visually);
29230 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29231 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29232 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29233 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29234 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29235 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29236 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29237 DEFSYM (Qeval, "eval");
29238 DEFSYM (QCdata, ":data");
29239 DEFSYM (Qdisplay, "display");
29240 DEFSYM (Qspace_width, "space-width");
29241 DEFSYM (Qraise, "raise");
29242 DEFSYM (Qslice, "slice");
29243 DEFSYM (Qspace, "space");
29244 DEFSYM (Qmargin, "margin");
29245 DEFSYM (Qpointer, "pointer");
29246 DEFSYM (Qleft_margin, "left-margin");
29247 DEFSYM (Qright_margin, "right-margin");
29248 DEFSYM (Qcenter, "center");
29249 DEFSYM (Qline_height, "line-height");
29250 DEFSYM (QCalign_to, ":align-to");
29251 DEFSYM (QCrelative_width, ":relative-width");
29252 DEFSYM (QCrelative_height, ":relative-height");
29253 DEFSYM (QCeval, ":eval");
29254 DEFSYM (QCpropertize, ":propertize");
29255 DEFSYM (QCfile, ":file");
29256 DEFSYM (Qfontified, "fontified");
29257 DEFSYM (Qfontification_functions, "fontification-functions");
29258 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29259 DEFSYM (Qescape_glyph, "escape-glyph");
29260 DEFSYM (Qnobreak_space, "nobreak-space");
29261 DEFSYM (Qimage, "image");
29262 DEFSYM (Qtext, "text");
29263 DEFSYM (Qboth, "both");
29264 DEFSYM (Qboth_horiz, "both-horiz");
29265 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29266 DEFSYM (QCmap, ":map");
29267 DEFSYM (QCpointer, ":pointer");
29268 DEFSYM (Qrect, "rect");
29269 DEFSYM (Qcircle, "circle");
29270 DEFSYM (Qpoly, "poly");
29271 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29272 DEFSYM (Qgrow_only, "grow-only");
29273 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29274 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29275 DEFSYM (Qposition, "position");
29276 DEFSYM (Qbuffer_position, "buffer-position");
29277 DEFSYM (Qobject, "object");
29278 DEFSYM (Qbar, "bar");
29279 DEFSYM (Qhbar, "hbar");
29280 DEFSYM (Qbox, "box");
29281 DEFSYM (Qhollow, "hollow");
29282 DEFSYM (Qhand, "hand");
29283 DEFSYM (Qarrow, "arrow");
29284 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29286 list_of_error = list1 (list2 (intern_c_string ("error"),
29287 intern_c_string ("void-variable")));
29288 staticpro (&list_of_error);
29290 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29291 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29292 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29293 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29295 echo_buffer[0] = echo_buffer[1] = Qnil;
29296 staticpro (&echo_buffer[0]);
29297 staticpro (&echo_buffer[1]);
29299 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29300 staticpro (&echo_area_buffer[0]);
29301 staticpro (&echo_area_buffer[1]);
29303 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29304 staticpro (&Vmessages_buffer_name);
29306 mode_line_proptrans_alist = Qnil;
29307 staticpro (&mode_line_proptrans_alist);
29308 mode_line_string_list = Qnil;
29309 staticpro (&mode_line_string_list);
29310 mode_line_string_face = Qnil;
29311 staticpro (&mode_line_string_face);
29312 mode_line_string_face_prop = Qnil;
29313 staticpro (&mode_line_string_face_prop);
29314 Vmode_line_unwind_vector = Qnil;
29315 staticpro (&Vmode_line_unwind_vector);
29317 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29319 help_echo_string = Qnil;
29320 staticpro (&help_echo_string);
29321 help_echo_object = Qnil;
29322 staticpro (&help_echo_object);
29323 help_echo_window = Qnil;
29324 staticpro (&help_echo_window);
29325 previous_help_echo_string = Qnil;
29326 staticpro (&previous_help_echo_string);
29327 help_echo_pos = -1;
29329 DEFSYM (Qright_to_left, "right-to-left");
29330 DEFSYM (Qleft_to_right, "left-to-right");
29332 #ifdef HAVE_WINDOW_SYSTEM
29333 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29334 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29335 For example, if a block cursor is over a tab, it will be drawn as
29336 wide as that tab on the display. */);
29337 x_stretch_cursor_p = 0;
29338 #endif
29340 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29341 doc: /* Non-nil means highlight trailing whitespace.
29342 The face used for trailing whitespace is `trailing-whitespace'. */);
29343 Vshow_trailing_whitespace = Qnil;
29345 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29346 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29347 If the value is t, Emacs highlights non-ASCII chars which have the
29348 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29349 or `escape-glyph' face respectively.
29351 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29352 U+2011 (non-breaking hyphen) are affected.
29354 Any other non-nil value means to display these characters as a escape
29355 glyph followed by an ordinary space or hyphen.
29357 A value of nil means no special handling of these characters. */);
29358 Vnobreak_char_display = Qt;
29360 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29361 doc: /* The pointer shape to show in void text areas.
29362 A value of nil means to show the text pointer. Other options are `arrow',
29363 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29364 Vvoid_text_area_pointer = Qarrow;
29366 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29367 doc: /* Non-nil means don't actually do any redisplay.
29368 This is used for internal purposes. */);
29369 Vinhibit_redisplay = Qnil;
29371 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29372 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29373 Vglobal_mode_string = Qnil;
29375 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29376 doc: /* Marker for where to display an arrow on top of the buffer text.
29377 This must be the beginning of a line in order to work.
29378 See also `overlay-arrow-string'. */);
29379 Voverlay_arrow_position = Qnil;
29381 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29382 doc: /* String to display as an arrow in non-window frames.
29383 See also `overlay-arrow-position'. */);
29384 Voverlay_arrow_string = build_pure_c_string ("=>");
29386 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29387 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29388 The symbols on this list are examined during redisplay to determine
29389 where to display overlay arrows. */);
29390 Voverlay_arrow_variable_list
29391 = list1 (intern_c_string ("overlay-arrow-position"));
29393 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29394 doc: /* The number of lines to try scrolling a window by when point moves out.
29395 If that fails to bring point back on frame, point is centered instead.
29396 If this is zero, point is always centered after it moves off frame.
29397 If you want scrolling to always be a line at a time, you should set
29398 `scroll-conservatively' to a large value rather than set this to 1. */);
29400 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29401 doc: /* Scroll up to this many lines, to bring point back on screen.
29402 If point moves off-screen, redisplay will scroll by up to
29403 `scroll-conservatively' lines in order to bring point just barely
29404 onto the screen again. If that cannot be done, then redisplay
29405 recenters point as usual.
29407 If the value is greater than 100, redisplay will never recenter point,
29408 but will always scroll just enough text to bring point into view, even
29409 if you move far away.
29411 A value of zero means always recenter point if it moves off screen. */);
29412 scroll_conservatively = 0;
29414 DEFVAR_INT ("scroll-margin", scroll_margin,
29415 doc: /* Number of lines of margin at the top and bottom of a window.
29416 Recenter the window whenever point gets within this many lines
29417 of the top or bottom of the window. */);
29418 scroll_margin = 0;
29420 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29421 doc: /* Pixels per inch value for non-window system displays.
29422 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29423 Vdisplay_pixels_per_inch = make_float (72.0);
29425 #ifdef GLYPH_DEBUG
29426 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29427 #endif
29429 DEFVAR_LISP ("truncate-partial-width-windows",
29430 Vtruncate_partial_width_windows,
29431 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29432 For an integer value, truncate lines in each window narrower than the
29433 full frame width, provided the window width is less than that integer;
29434 otherwise, respect the value of `truncate-lines'.
29436 For any other non-nil value, truncate lines in all windows that do
29437 not span the full frame width.
29439 A value of nil means to respect the value of `truncate-lines'.
29441 If `word-wrap' is enabled, you might want to reduce this. */);
29442 Vtruncate_partial_width_windows = make_number (50);
29444 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29445 doc: /* Maximum buffer size for which line number should be displayed.
29446 If the buffer is bigger than this, the line number does not appear
29447 in the mode line. A value of nil means no limit. */);
29448 Vline_number_display_limit = Qnil;
29450 DEFVAR_INT ("line-number-display-limit-width",
29451 line_number_display_limit_width,
29452 doc: /* Maximum line width (in characters) for line number display.
29453 If the average length of the lines near point is bigger than this, then the
29454 line number may be omitted from the mode line. */);
29455 line_number_display_limit_width = 200;
29457 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29458 doc: /* Non-nil means highlight region even in nonselected windows. */);
29459 highlight_nonselected_windows = 0;
29461 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29462 doc: /* Non-nil if more than one frame is visible on this display.
29463 Minibuffer-only frames don't count, but iconified frames do.
29464 This variable is not guaranteed to be accurate except while processing
29465 `frame-title-format' and `icon-title-format'. */);
29467 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29468 doc: /* Template for displaying the title bar of visible frames.
29469 \(Assuming the window manager supports this feature.)
29471 This variable has the same structure as `mode-line-format', except that
29472 the %c and %l constructs are ignored. It is used only on frames for
29473 which no explicit name has been set \(see `modify-frame-parameters'). */);
29475 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29476 doc: /* Template for displaying the title bar of an iconified frame.
29477 \(Assuming the window manager supports this feature.)
29478 This variable has the same structure as `mode-line-format' (which see),
29479 and is used only on frames for which no explicit name has been set
29480 \(see `modify-frame-parameters'). */);
29481 Vicon_title_format
29482 = Vframe_title_format
29483 = listn (CONSTYPE_PURE, 3,
29484 intern_c_string ("multiple-frames"),
29485 build_pure_c_string ("%b"),
29486 listn (CONSTYPE_PURE, 4,
29487 empty_unibyte_string,
29488 intern_c_string ("invocation-name"),
29489 build_pure_c_string ("@"),
29490 intern_c_string ("system-name")));
29492 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29493 doc: /* Maximum number of lines to keep in the message log buffer.
29494 If nil, disable message logging. If t, log messages but don't truncate
29495 the buffer when it becomes large. */);
29496 Vmessage_log_max = make_number (1000);
29498 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29499 doc: /* Functions called before redisplay, if window sizes have changed.
29500 The value should be a list of functions that take one argument.
29501 Just before redisplay, for each frame, if any of its windows have changed
29502 size since the last redisplay, or have been split or deleted,
29503 all the functions in the list are called, with the frame as argument. */);
29504 Vwindow_size_change_functions = Qnil;
29506 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29507 doc: /* List of functions to call before redisplaying a window with scrolling.
29508 Each function is called with two arguments, the window and its new
29509 display-start position. Note that these functions are also called by
29510 `set-window-buffer'. Also note that the value of `window-end' is not
29511 valid when these functions are called.
29513 Warning: Do not use this feature to alter the way the window
29514 is scrolled. It is not designed for that, and such use probably won't
29515 work. */);
29516 Vwindow_scroll_functions = Qnil;
29518 DEFVAR_LISP ("window-text-change-functions",
29519 Vwindow_text_change_functions,
29520 doc: /* Functions to call in redisplay when text in the window might change. */);
29521 Vwindow_text_change_functions = Qnil;
29523 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29524 doc: /* Functions called when redisplay of a window reaches the end trigger.
29525 Each function is called with two arguments, the window and the end trigger value.
29526 See `set-window-redisplay-end-trigger'. */);
29527 Vredisplay_end_trigger_functions = Qnil;
29529 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29530 doc: /* Non-nil means autoselect window with mouse pointer.
29531 If nil, do not autoselect windows.
29532 A positive number means delay autoselection by that many seconds: a
29533 window is autoselected only after the mouse has remained in that
29534 window for the duration of the delay.
29535 A negative number has a similar effect, but causes windows to be
29536 autoselected only after the mouse has stopped moving. \(Because of
29537 the way Emacs compares mouse events, you will occasionally wait twice
29538 that time before the window gets selected.\)
29539 Any other value means to autoselect window instantaneously when the
29540 mouse pointer enters it.
29542 Autoselection selects the minibuffer only if it is active, and never
29543 unselects the minibuffer if it is active.
29545 When customizing this variable make sure that the actual value of
29546 `focus-follows-mouse' matches the behavior of your window manager. */);
29547 Vmouse_autoselect_window = Qnil;
29549 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29550 doc: /* Non-nil means automatically resize tool-bars.
29551 This dynamically changes the tool-bar's height to the minimum height
29552 that is needed to make all tool-bar items visible.
29553 If value is `grow-only', the tool-bar's height is only increased
29554 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29555 Vauto_resize_tool_bars = Qt;
29557 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29558 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29559 auto_raise_tool_bar_buttons_p = 1;
29561 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29562 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29563 make_cursor_line_fully_visible_p = 1;
29565 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29566 doc: /* Border below tool-bar in pixels.
29567 If an integer, use it as the height of the border.
29568 If it is one of `internal-border-width' or `border-width', use the
29569 value of the corresponding frame parameter.
29570 Otherwise, no border is added below the tool-bar. */);
29571 Vtool_bar_border = Qinternal_border_width;
29573 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29574 doc: /* Margin around tool-bar buttons in pixels.
29575 If an integer, use that for both horizontal and vertical margins.
29576 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29577 HORZ specifying the horizontal margin, and VERT specifying the
29578 vertical margin. */);
29579 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29581 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29582 doc: /* Relief thickness of tool-bar buttons. */);
29583 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29585 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29586 doc: /* Tool bar style to use.
29587 It can be one of
29588 image - show images only
29589 text - show text only
29590 both - show both, text below image
29591 both-horiz - show text to the right of the image
29592 text-image-horiz - show text to the left of the image
29593 any other - use system default or image if no system default.
29595 This variable only affects the GTK+ toolkit version of Emacs. */);
29596 Vtool_bar_style = Qnil;
29598 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29599 doc: /* Maximum number of characters a label can have to be shown.
29600 The tool bar style must also show labels for this to have any effect, see
29601 `tool-bar-style'. */);
29602 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29604 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29605 doc: /* List of functions to call to fontify regions of text.
29606 Each function is called with one argument POS. Functions must
29607 fontify a region starting at POS in the current buffer, and give
29608 fontified regions the property `fontified'. */);
29609 Vfontification_functions = Qnil;
29610 Fmake_variable_buffer_local (Qfontification_functions);
29612 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29613 unibyte_display_via_language_environment,
29614 doc: /* Non-nil means display unibyte text according to language environment.
29615 Specifically, this means that raw bytes in the range 160-255 decimal
29616 are displayed by converting them to the equivalent multibyte characters
29617 according to the current language environment. As a result, they are
29618 displayed according to the current fontset.
29620 Note that this variable affects only how these bytes are displayed,
29621 but does not change the fact they are interpreted as raw bytes. */);
29622 unibyte_display_via_language_environment = 0;
29624 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29625 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29626 If a float, it specifies a fraction of the mini-window frame's height.
29627 If an integer, it specifies a number of lines. */);
29628 Vmax_mini_window_height = make_float (0.25);
29630 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29631 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29632 A value of nil means don't automatically resize mini-windows.
29633 A value of t means resize them to fit the text displayed in them.
29634 A value of `grow-only', the default, means let mini-windows grow only;
29635 they return to their normal size when the minibuffer is closed, or the
29636 echo area becomes empty. */);
29637 Vresize_mini_windows = Qgrow_only;
29639 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29640 doc: /* Alist specifying how to blink the cursor off.
29641 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29642 `cursor-type' frame-parameter or variable equals ON-STATE,
29643 comparing using `equal', Emacs uses OFF-STATE to specify
29644 how to blink it off. ON-STATE and OFF-STATE are values for
29645 the `cursor-type' frame parameter.
29647 If a frame's ON-STATE has no entry in this list,
29648 the frame's other specifications determine how to blink the cursor off. */);
29649 Vblink_cursor_alist = Qnil;
29651 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29652 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29653 If non-nil, windows are automatically scrolled horizontally to make
29654 point visible. */);
29655 automatic_hscrolling_p = 1;
29656 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29658 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29659 doc: /* How many columns away from the window edge point is allowed to get
29660 before automatic hscrolling will horizontally scroll the window. */);
29661 hscroll_margin = 5;
29663 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29664 doc: /* How many columns to scroll the window when point gets too close to the edge.
29665 When point is less than `hscroll-margin' columns from the window
29666 edge, automatic hscrolling will scroll the window by the amount of columns
29667 determined by this variable. If its value is a positive integer, scroll that
29668 many columns. If it's a positive floating-point number, it specifies the
29669 fraction of the window's width to scroll. If it's nil or zero, point will be
29670 centered horizontally after the scroll. Any other value, including negative
29671 numbers, are treated as if the value were zero.
29673 Automatic hscrolling always moves point outside the scroll margin, so if
29674 point was more than scroll step columns inside the margin, the window will
29675 scroll more than the value given by the scroll step.
29677 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29678 and `scroll-right' overrides this variable's effect. */);
29679 Vhscroll_step = make_number (0);
29681 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29682 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29683 Bind this around calls to `message' to let it take effect. */);
29684 message_truncate_lines = 0;
29686 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29687 doc: /* Normal hook run to update the menu bar definitions.
29688 Redisplay runs this hook before it redisplays the menu bar.
29689 This is used to update submenus such as Buffers,
29690 whose contents depend on various data. */);
29691 Vmenu_bar_update_hook = Qnil;
29693 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29694 doc: /* Frame for which we are updating a menu.
29695 The enable predicate for a menu binding should check this variable. */);
29696 Vmenu_updating_frame = Qnil;
29698 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29699 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29700 inhibit_menubar_update = 0;
29702 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29703 doc: /* Prefix prepended to all continuation lines at display time.
29704 The value may be a string, an image, or a stretch-glyph; it is
29705 interpreted in the same way as the value of a `display' text property.
29707 This variable is overridden by any `wrap-prefix' text or overlay
29708 property.
29710 To add a prefix to non-continuation lines, use `line-prefix'. */);
29711 Vwrap_prefix = Qnil;
29712 DEFSYM (Qwrap_prefix, "wrap-prefix");
29713 Fmake_variable_buffer_local (Qwrap_prefix);
29715 DEFVAR_LISP ("line-prefix", Vline_prefix,
29716 doc: /* Prefix prepended to all non-continuation lines at display time.
29717 The value may be a string, an image, or a stretch-glyph; it is
29718 interpreted in the same way as the value of a `display' text property.
29720 This variable is overridden by any `line-prefix' text or overlay
29721 property.
29723 To add a prefix to continuation lines, use `wrap-prefix'. */);
29724 Vline_prefix = Qnil;
29725 DEFSYM (Qline_prefix, "line-prefix");
29726 Fmake_variable_buffer_local (Qline_prefix);
29728 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29729 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29730 inhibit_eval_during_redisplay = 0;
29732 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29733 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29734 inhibit_free_realized_faces = 0;
29736 #ifdef GLYPH_DEBUG
29737 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29738 doc: /* Inhibit try_window_id display optimization. */);
29739 inhibit_try_window_id = 0;
29741 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29742 doc: /* Inhibit try_window_reusing display optimization. */);
29743 inhibit_try_window_reusing = 0;
29745 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29746 doc: /* Inhibit try_cursor_movement display optimization. */);
29747 inhibit_try_cursor_movement = 0;
29748 #endif /* GLYPH_DEBUG */
29750 DEFVAR_INT ("overline-margin", overline_margin,
29751 doc: /* Space between overline and text, in pixels.
29752 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29753 margin to the character height. */);
29754 overline_margin = 2;
29756 DEFVAR_INT ("underline-minimum-offset",
29757 underline_minimum_offset,
29758 doc: /* Minimum distance between baseline and underline.
29759 This can improve legibility of underlined text at small font sizes,
29760 particularly when using variable `x-use-underline-position-properties'
29761 with fonts that specify an UNDERLINE_POSITION relatively close to the
29762 baseline. The default value is 1. */);
29763 underline_minimum_offset = 1;
29765 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29766 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29767 This feature only works when on a window system that can change
29768 cursor shapes. */);
29769 display_hourglass_p = 1;
29771 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29772 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29773 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29775 #ifdef HAVE_WINDOW_SYSTEM
29776 hourglass_atimer = NULL;
29777 hourglass_shown_p = 0;
29778 #endif /* HAVE_WINDOW_SYSTEM */
29780 DEFSYM (Qglyphless_char, "glyphless-char");
29781 DEFSYM (Qhex_code, "hex-code");
29782 DEFSYM (Qempty_box, "empty-box");
29783 DEFSYM (Qthin_space, "thin-space");
29784 DEFSYM (Qzero_width, "zero-width");
29786 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29787 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29789 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29790 doc: /* Char-table defining glyphless characters.
29791 Each element, if non-nil, should be one of the following:
29792 an ASCII acronym string: display this string in a box
29793 `hex-code': display the hexadecimal code of a character in a box
29794 `empty-box': display as an empty box
29795 `thin-space': display as 1-pixel width space
29796 `zero-width': don't display
29797 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29798 display method for graphical terminals and text terminals respectively.
29799 GRAPHICAL and TEXT should each have one of the values listed above.
29801 The char-table has one extra slot to control the display of a character for
29802 which no font is found. This slot only takes effect on graphical terminals.
29803 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29804 `thin-space'. The default is `empty-box'. */);
29805 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29806 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29807 Qempty_box);
29809 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29810 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29811 Vdebug_on_message = Qnil;
29815 /* Initialize this module when Emacs starts. */
29817 void
29818 init_xdisp (void)
29820 current_header_line_height = current_mode_line_height = -1;
29822 CHARPOS (this_line_start_pos) = 0;
29824 if (!noninteractive)
29826 struct window *m = XWINDOW (minibuf_window);
29827 Lisp_Object frame = m->frame;
29828 struct frame *f = XFRAME (frame);
29829 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29830 struct window *r = XWINDOW (root);
29831 int i;
29833 echo_area_window = minibuf_window;
29835 r->top_line = FRAME_TOP_MARGIN (f);
29836 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29837 r->total_cols = FRAME_COLS (f);
29839 m->top_line = FRAME_LINES (f) - 1;
29840 m->total_lines = 1;
29841 m->total_cols = FRAME_COLS (f);
29843 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29844 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29845 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29847 /* The default ellipsis glyphs `...'. */
29848 for (i = 0; i < 3; ++i)
29849 default_invis_vector[i] = make_number ('.');
29853 /* Allocate the buffer for frame titles.
29854 Also used for `format-mode-line'. */
29855 int size = 100;
29856 mode_line_noprop_buf = xmalloc (size);
29857 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29858 mode_line_noprop_ptr = mode_line_noprop_buf;
29859 mode_line_target = MODE_LINE_DISPLAY;
29862 help_echo_showing_p = 0;
29865 #ifdef HAVE_WINDOW_SYSTEM
29867 /* Platform-independent portion of hourglass implementation. */
29869 /* Cancel a currently active hourglass timer, and start a new one. */
29870 void
29871 start_hourglass (void)
29873 struct timespec delay;
29875 cancel_hourglass ();
29877 if (INTEGERP (Vhourglass_delay)
29878 && XINT (Vhourglass_delay) > 0)
29879 delay = make_timespec (min (XINT (Vhourglass_delay),
29880 TYPE_MAXIMUM (time_t)),
29882 else if (FLOATP (Vhourglass_delay)
29883 && XFLOAT_DATA (Vhourglass_delay) > 0)
29884 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29885 else
29886 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29888 #ifdef HAVE_NTGUI
29890 extern void w32_note_current_window (void);
29891 w32_note_current_window ();
29893 #endif /* HAVE_NTGUI */
29895 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29896 show_hourglass, NULL);
29900 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29901 shown. */
29902 void
29903 cancel_hourglass (void)
29905 if (hourglass_atimer)
29907 cancel_atimer (hourglass_atimer);
29908 hourglass_atimer = NULL;
29911 if (hourglass_shown_p)
29912 hide_hourglass ();
29915 #endif /* HAVE_WINDOW_SYSTEM */