* process.c (allocate_pty) [PTY_OPEN]: Set fd's FD_CLOEXEC flag.
[emacs.git] / src / xdisp.c
blobea1cd7dd2bcc7d4d0ae8c71eb29d3a1cf5f5ad6e
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 /* Non-zero means an hourglass cursor is currently shown. */
764 int hourglass_shown_p;
766 /* If non-null, an asynchronous timer that, when it expires, displays
767 an hourglass cursor on all frames. */
768 struct atimer *hourglass_atimer;
770 /* Name of the face used to display glyphless characters. */
771 Lisp_Object Qglyphless_char;
773 /* Symbol for the purpose of Vglyphless_char_display. */
774 static Lisp_Object Qglyphless_char_display;
776 /* Method symbols for Vglyphless_char_display. */
777 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
779 /* Default pixel width of `thin-space' display method. */
780 #define THIN_SPACE_WIDTH 1
782 /* Default number of seconds to wait before displaying an hourglass
783 cursor. */
784 #define DEFAULT_HOURGLASS_DELAY 1
787 /* Function prototypes. */
789 static void setup_for_ellipsis (struct it *, int);
790 static void set_iterator_to_next (struct it *, int);
791 static void mark_window_display_accurate_1 (struct window *, int);
792 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
793 static int display_prop_string_p (Lisp_Object, Lisp_Object);
794 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
795 static int cursor_row_p (struct glyph_row *);
796 static int redisplay_mode_lines (Lisp_Object, int);
797 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
799 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
801 static void handle_line_prefix (struct it *);
803 static void pint2str (char *, int, ptrdiff_t);
804 static void pint2hrstr (char *, int, ptrdiff_t);
805 static struct text_pos run_window_scroll_functions (Lisp_Object,
806 struct text_pos);
807 static int text_outside_line_unchanged_p (struct window *,
808 ptrdiff_t, ptrdiff_t);
809 static void store_mode_line_noprop_char (char);
810 static int store_mode_line_noprop (const char *, int, int);
811 static void handle_stop (struct it *);
812 static void handle_stop_backwards (struct it *, ptrdiff_t);
813 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
814 static void ensure_echo_area_buffers (void);
815 static void unwind_with_echo_area_buffer (Lisp_Object);
816 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
817 static int with_echo_area_buffer (struct window *, int,
818 int (*) (ptrdiff_t, Lisp_Object),
819 ptrdiff_t, Lisp_Object);
820 static void clear_garbaged_frames (void);
821 static int current_message_1 (ptrdiff_t, Lisp_Object);
822 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
823 static void set_message (Lisp_Object);
824 static int set_message_1 (ptrdiff_t, Lisp_Object);
825 static int display_echo_area (struct window *);
826 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
827 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
828 static void unwind_redisplay (void);
829 static int string_char_and_length (const unsigned char *, int *);
830 static struct text_pos display_prop_end (struct it *, Lisp_Object,
831 struct text_pos);
832 static int compute_window_start_on_continuation_line (struct window *);
833 static void insert_left_trunc_glyphs (struct it *);
834 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
835 Lisp_Object);
836 static void extend_face_to_end_of_line (struct it *);
837 static int append_space_for_newline (struct it *, int);
838 static int cursor_row_fully_visible_p (struct window *, int, int);
839 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
840 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
841 static int trailing_whitespace_p (ptrdiff_t);
842 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
843 static void push_it (struct it *, struct text_pos *);
844 static void iterate_out_of_display_property (struct it *);
845 static void pop_it (struct it *);
846 static void sync_frame_with_window_matrix_rows (struct window *);
847 static void redisplay_internal (void);
848 static int echo_area_display (int);
849 static void redisplay_windows (Lisp_Object);
850 static void redisplay_window (Lisp_Object, int);
851 static Lisp_Object redisplay_window_error (Lisp_Object);
852 static Lisp_Object redisplay_window_0 (Lisp_Object);
853 static Lisp_Object redisplay_window_1 (Lisp_Object);
854 static int set_cursor_from_row (struct window *, struct glyph_row *,
855 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
856 int, int);
857 static int update_menu_bar (struct frame *, int, int);
858 static int try_window_reusing_current_matrix (struct window *);
859 static int try_window_id (struct window *);
860 static int display_line (struct it *);
861 static int display_mode_lines (struct window *);
862 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
863 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
864 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
865 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
866 static void display_menu_bar (struct window *);
867 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
868 ptrdiff_t *);
869 static int display_string (const char *, Lisp_Object, Lisp_Object,
870 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
871 static void compute_line_metrics (struct it *);
872 static void run_redisplay_end_trigger_hook (struct it *);
873 static int get_overlay_strings (struct it *, ptrdiff_t);
874 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
875 static void next_overlay_string (struct it *);
876 static void reseat (struct it *, struct text_pos, int);
877 static void reseat_1 (struct it *, struct text_pos, int);
878 static void back_to_previous_visible_line_start (struct it *);
879 static void reseat_at_next_visible_line_start (struct it *, int);
880 static int next_element_from_ellipsis (struct it *);
881 static int next_element_from_display_vector (struct it *);
882 static int next_element_from_string (struct it *);
883 static int next_element_from_c_string (struct it *);
884 static int next_element_from_buffer (struct it *);
885 static int next_element_from_composition (struct it *);
886 static int next_element_from_image (struct it *);
887 static int next_element_from_stretch (struct it *);
888 static void load_overlay_strings (struct it *, ptrdiff_t);
889 static int init_from_display_pos (struct it *, struct window *,
890 struct display_pos *);
891 static void reseat_to_string (struct it *, const char *,
892 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
893 static int get_next_display_element (struct it *);
894 static enum move_it_result
895 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
896 enum move_operation_enum);
897 static void get_visually_first_element (struct it *);
898 static void init_to_row_start (struct it *, struct window *,
899 struct glyph_row *);
900 static int init_to_row_end (struct it *, struct window *,
901 struct glyph_row *);
902 static void back_to_previous_line_start (struct it *);
903 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
904 static struct text_pos string_pos_nchars_ahead (struct text_pos,
905 Lisp_Object, ptrdiff_t);
906 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
907 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
908 static ptrdiff_t number_of_chars (const char *, bool);
909 static void compute_stop_pos (struct it *);
910 static void compute_string_pos (struct text_pos *, struct text_pos,
911 Lisp_Object);
912 static int face_before_or_after_it_pos (struct it *, int);
913 static ptrdiff_t next_overlay_change (ptrdiff_t);
914 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
915 Lisp_Object, struct text_pos *, ptrdiff_t, int);
916 static int handle_single_display_spec (struct it *, Lisp_Object,
917 Lisp_Object, Lisp_Object,
918 struct text_pos *, ptrdiff_t, int, int);
919 static int underlying_face_id (struct it *);
920 static int in_ellipses_for_invisible_text_p (struct display_pos *,
921 struct window *);
923 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
924 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
926 #ifdef HAVE_WINDOW_SYSTEM
928 static void x_consider_frame_title (Lisp_Object);
929 static int tool_bar_lines_needed (struct frame *, int *);
930 static void update_tool_bar (struct frame *, int);
931 static void build_desired_tool_bar_string (struct frame *f);
932 static int redisplay_tool_bar (struct frame *);
933 static void display_tool_bar_line (struct it *, int);
934 static void notice_overwritten_cursor (struct window *,
935 enum glyph_row_area,
936 int, int, int, int);
937 static void append_stretch_glyph (struct it *, Lisp_Object,
938 int, int, int);
941 #endif /* HAVE_WINDOW_SYSTEM */
943 static void produce_special_glyphs (struct it *, enum display_element_type);
944 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
945 static int coords_in_mouse_face_p (struct window *, int, int);
949 /***********************************************************************
950 Window display dimensions
951 ***********************************************************************/
953 /* Return the bottom boundary y-position for text lines in window W.
954 This is the first y position at which a line cannot start.
955 It is relative to the top of the window.
957 This is the height of W minus the height of a mode line, if any. */
960 window_text_bottom_y (struct window *w)
962 int height = WINDOW_TOTAL_HEIGHT (w);
964 if (WINDOW_WANTS_MODELINE_P (w))
965 height -= CURRENT_MODE_LINE_HEIGHT (w);
966 return height;
969 /* Return the pixel width of display area AREA of window W. AREA < 0
970 means return the total width of W, not including fringes to
971 the left and right of the window. */
974 window_box_width (struct window *w, int area)
976 int cols = w->total_cols;
977 int pixels = 0;
979 if (!w->pseudo_window_p)
981 cols -= WINDOW_SCROLL_BAR_COLS (w);
983 if (area == TEXT_AREA)
985 cols -= max (0, w->left_margin_cols);
986 cols -= max (0, w->right_margin_cols);
987 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
989 else if (area == LEFT_MARGIN_AREA)
991 cols = max (0, w->left_margin_cols);
992 pixels = 0;
994 else if (area == RIGHT_MARGIN_AREA)
996 cols = max (0, w->right_margin_cols);
997 pixels = 0;
1001 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1005 /* Return the pixel height of the display area of window W, not
1006 including mode lines of W, if any. */
1009 window_box_height (struct window *w)
1011 struct frame *f = XFRAME (w->frame);
1012 int height = WINDOW_TOTAL_HEIGHT (w);
1014 eassert (height >= 0);
1016 /* Note: the code below that determines the mode-line/header-line
1017 height is essentially the same as that contained in the macro
1018 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1019 the appropriate glyph row has its `mode_line_p' flag set,
1020 and if it doesn't, uses estimate_mode_line_height instead. */
1022 if (WINDOW_WANTS_MODELINE_P (w))
1024 struct glyph_row *ml_row
1025 = (w->current_matrix && w->current_matrix->rows
1026 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1027 : 0);
1028 if (ml_row && ml_row->mode_line_p)
1029 height -= ml_row->height;
1030 else
1031 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1034 if (WINDOW_WANTS_HEADER_LINE_P (w))
1036 struct glyph_row *hl_row
1037 = (w->current_matrix && w->current_matrix->rows
1038 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1039 : 0);
1040 if (hl_row && hl_row->mode_line_p)
1041 height -= hl_row->height;
1042 else
1043 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1046 /* With a very small font and a mode-line that's taller than
1047 default, we might end up with a negative height. */
1048 return max (0, height);
1051 /* Return the window-relative coordinate of the left edge of display
1052 area AREA of window W. AREA < 0 means return the left edge of the
1053 whole window, to the right of the left fringe of W. */
1056 window_box_left_offset (struct window *w, int area)
1058 int x;
1060 if (w->pseudo_window_p)
1061 return 0;
1063 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1065 if (area == TEXT_AREA)
1066 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1067 + window_box_width (w, LEFT_MARGIN_AREA));
1068 else if (area == RIGHT_MARGIN_AREA)
1069 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1070 + window_box_width (w, LEFT_MARGIN_AREA)
1071 + window_box_width (w, TEXT_AREA)
1072 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1074 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1075 else if (area == LEFT_MARGIN_AREA
1076 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1077 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1079 return x;
1083 /* Return the window-relative coordinate of the right edge of display
1084 area AREA of window W. AREA < 0 means return the right edge of the
1085 whole window, to the left of the right fringe of W. */
1088 window_box_right_offset (struct window *w, int area)
1090 return window_box_left_offset (w, area) + window_box_width (w, area);
1093 /* Return the frame-relative coordinate of the left edge of display
1094 area AREA of window W. AREA < 0 means return the left edge of the
1095 whole window, to the right of the left fringe of W. */
1098 window_box_left (struct window *w, int area)
1100 struct frame *f = XFRAME (w->frame);
1101 int x;
1103 if (w->pseudo_window_p)
1104 return FRAME_INTERNAL_BORDER_WIDTH (f);
1106 x = (WINDOW_LEFT_EDGE_X (w)
1107 + window_box_left_offset (w, area));
1109 return x;
1113 /* Return the frame-relative coordinate of the right edge of display
1114 area AREA of window W. AREA < 0 means return the right edge of the
1115 whole window, to the left of the right fringe of W. */
1118 window_box_right (struct window *w, int area)
1120 return window_box_left (w, area) + window_box_width (w, area);
1123 /* Get the bounding box of the display area AREA of window W, without
1124 mode lines, in frame-relative coordinates. AREA < 0 means the
1125 whole window, not including the left and right fringes of
1126 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1127 coordinates of the upper-left corner of the box. Return in
1128 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1130 void
1131 window_box (struct window *w, int area, int *box_x, int *box_y,
1132 int *box_width, int *box_height)
1134 if (box_width)
1135 *box_width = window_box_width (w, area);
1136 if (box_height)
1137 *box_height = window_box_height (w);
1138 if (box_x)
1139 *box_x = window_box_left (w, area);
1140 if (box_y)
1142 *box_y = WINDOW_TOP_EDGE_Y (w);
1143 if (WINDOW_WANTS_HEADER_LINE_P (w))
1144 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1149 /* Get the bounding box of the display area AREA of window W, without
1150 mode lines. AREA < 0 means the whole window, not including the
1151 left and right fringe of the window. Return in *TOP_LEFT_X
1152 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1153 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1154 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1155 box. */
1157 static void
1158 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1159 int *bottom_right_x, int *bottom_right_y)
1161 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1162 bottom_right_y);
1163 *bottom_right_x += *top_left_x;
1164 *bottom_right_y += *top_left_y;
1169 /***********************************************************************
1170 Utilities
1171 ***********************************************************************/
1173 /* Return the bottom y-position of the line the iterator IT is in.
1174 This can modify IT's settings. */
1177 line_bottom_y (struct it *it)
1179 int line_height = it->max_ascent + it->max_descent;
1180 int line_top_y = it->current_y;
1182 if (line_height == 0)
1184 if (last_height)
1185 line_height = last_height;
1186 else if (IT_CHARPOS (*it) < ZV)
1188 move_it_by_lines (it, 1);
1189 line_height = (it->max_ascent || it->max_descent
1190 ? it->max_ascent + it->max_descent
1191 : last_height);
1193 else
1195 struct glyph_row *row = it->glyph_row;
1197 /* Use the default character height. */
1198 it->glyph_row = NULL;
1199 it->what = IT_CHARACTER;
1200 it->c = ' ';
1201 it->len = 1;
1202 PRODUCE_GLYPHS (it);
1203 line_height = it->ascent + it->descent;
1204 it->glyph_row = row;
1208 return line_top_y + line_height;
1211 DEFUN ("line-pixel-height", Fline_pixel_height,
1212 Sline_pixel_height, 0, 0, 0,
1213 doc: /* Return height in pixels of text line in the selected window.
1215 Value is the height in pixels of the line at point. */)
1216 (void)
1218 struct it it;
1219 struct text_pos pt;
1220 struct window *w = XWINDOW (selected_window);
1222 SET_TEXT_POS (pt, PT, PT_BYTE);
1223 start_display (&it, w, pt);
1224 it.vpos = it.current_y = 0;
1225 last_height = 0;
1226 return make_number (line_bottom_y (&it));
1229 /* Return the default pixel height of text lines in window W. The
1230 value is the canonical height of the W frame's default font, plus
1231 any extra space required by the line-spacing variable or frame
1232 parameter.
1234 Implementation note: this ignores any line-spacing text properties
1235 put on the newline characters. This is because those properties
1236 only affect the _screen_ line ending in the newline (i.e., in a
1237 continued line, only the last screen line will be affected), which
1238 means only a small number of lines in a buffer can ever use this
1239 feature. Since this function is used to compute the default pixel
1240 equivalent of text lines in a window, we can safely ignore those
1241 few lines. For the same reasons, we ignore the line-height
1242 properties. */
1244 default_line_pixel_height (struct window *w)
1246 struct frame *f = WINDOW_XFRAME (w);
1247 int height = FRAME_LINE_HEIGHT (f);
1249 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1251 struct buffer *b = XBUFFER (w->contents);
1252 Lisp_Object val = BVAR (b, extra_line_spacing);
1254 if (NILP (val))
1255 val = BVAR (&buffer_defaults, extra_line_spacing);
1256 if (!NILP (val))
1258 if (RANGED_INTEGERP (0, val, INT_MAX))
1259 height += XFASTINT (val);
1260 else if (FLOATP (val))
1262 int addon = XFLOAT_DATA (val) * height + 0.5;
1264 if (addon >= 0)
1265 height += addon;
1268 else
1269 height += f->extra_line_spacing;
1272 return height;
1275 /* Subroutine of pos_visible_p below. Extracts a display string, if
1276 any, from the display spec given as its argument. */
1277 static Lisp_Object
1278 string_from_display_spec (Lisp_Object spec)
1280 if (CONSP (spec))
1282 while (CONSP (spec))
1284 if (STRINGP (XCAR (spec)))
1285 return XCAR (spec);
1286 spec = XCDR (spec);
1289 else if (VECTORP (spec))
1291 ptrdiff_t i;
1293 for (i = 0; i < ASIZE (spec); i++)
1295 if (STRINGP (AREF (spec, i)))
1296 return AREF (spec, i);
1298 return Qnil;
1301 return spec;
1305 /* Limit insanely large values of W->hscroll on frame F to the largest
1306 value that will still prevent first_visible_x and last_visible_x of
1307 'struct it' from overflowing an int. */
1308 static int
1309 window_hscroll_limited (struct window *w, struct frame *f)
1311 ptrdiff_t window_hscroll = w->hscroll;
1312 int window_text_width = window_box_width (w, TEXT_AREA);
1313 int colwidth = FRAME_COLUMN_WIDTH (f);
1315 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1316 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1318 return window_hscroll;
1321 /* Return 1 if position CHARPOS is visible in window W.
1322 CHARPOS < 0 means return info about WINDOW_END position.
1323 If visible, set *X and *Y to pixel coordinates of top left corner.
1324 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1325 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1328 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1329 int *rtop, int *rbot, int *rowh, int *vpos)
1331 struct it it;
1332 void *itdata = bidi_shelve_cache ();
1333 struct text_pos top;
1334 int visible_p = 0;
1335 struct buffer *old_buffer = NULL;
1337 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1338 return visible_p;
1340 if (XBUFFER (w->contents) != current_buffer)
1342 old_buffer = current_buffer;
1343 set_buffer_internal_1 (XBUFFER (w->contents));
1346 SET_TEXT_POS_FROM_MARKER (top, w->start);
1347 /* Scrolling a minibuffer window via scroll bar when the echo area
1348 shows long text sometimes resets the minibuffer contents behind
1349 our backs. */
1350 if (CHARPOS (top) > ZV)
1351 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1353 /* Compute exact mode line heights. */
1354 if (WINDOW_WANTS_MODELINE_P (w))
1355 current_mode_line_height
1356 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1357 BVAR (current_buffer, mode_line_format));
1359 if (WINDOW_WANTS_HEADER_LINE_P (w))
1360 current_header_line_height
1361 = display_mode_line (w, HEADER_LINE_FACE_ID,
1362 BVAR (current_buffer, header_line_format));
1364 start_display (&it, w, top);
1365 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1366 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1368 if (charpos >= 0
1369 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1370 && IT_CHARPOS (it) >= charpos)
1371 /* When scanning backwards under bidi iteration, move_it_to
1372 stops at or _before_ CHARPOS, because it stops at or to
1373 the _right_ of the character at CHARPOS. */
1374 || (it.bidi_p && it.bidi_it.scan_dir == -1
1375 && IT_CHARPOS (it) <= charpos)))
1377 /* We have reached CHARPOS, or passed it. How the call to
1378 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1379 or covered by a display property, move_it_to stops at the end
1380 of the invisible text, to the right of CHARPOS. (ii) If
1381 CHARPOS is in a display vector, move_it_to stops on its last
1382 glyph. */
1383 int top_x = it.current_x;
1384 int top_y = it.current_y;
1385 /* Calling line_bottom_y may change it.method, it.position, etc. */
1386 enum it_method it_method = it.method;
1387 int bottom_y = (last_height = 0, line_bottom_y (&it));
1388 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1390 if (top_y < window_top_y)
1391 visible_p = bottom_y > window_top_y;
1392 else if (top_y < it.last_visible_y)
1393 visible_p = 1;
1394 if (bottom_y >= it.last_visible_y
1395 && it.bidi_p && it.bidi_it.scan_dir == -1
1396 && IT_CHARPOS (it) < charpos)
1398 /* When the last line of the window is scanned backwards
1399 under bidi iteration, we could be duped into thinking
1400 that we have passed CHARPOS, when in fact move_it_to
1401 simply stopped short of CHARPOS because it reached
1402 last_visible_y. To see if that's what happened, we call
1403 move_it_to again with a slightly larger vertical limit,
1404 and see if it actually moved vertically; if it did, we
1405 didn't really reach CHARPOS, which is beyond window end. */
1406 struct it save_it = it;
1407 /* Why 10? because we don't know how many canonical lines
1408 will the height of the next line(s) be. So we guess. */
1409 int ten_more_lines = 10 * default_line_pixel_height (w);
1411 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1412 MOVE_TO_POS | MOVE_TO_Y);
1413 if (it.current_y > top_y)
1414 visible_p = 0;
1416 it = save_it;
1418 if (visible_p)
1420 if (it_method == GET_FROM_DISPLAY_VECTOR)
1422 /* We stopped on the last glyph of a display vector.
1423 Try and recompute. Hack alert! */
1424 if (charpos < 2 || top.charpos >= charpos)
1425 top_x = it.glyph_row->x;
1426 else
1428 struct it it2, it2_prev;
1429 /* The idea is to get to the previous buffer
1430 position, consume the character there, and use
1431 the pixel coordinates we get after that. But if
1432 the previous buffer position is also displayed
1433 from a display vector, we need to consume all of
1434 the glyphs from that display vector. */
1435 start_display (&it2, w, top);
1436 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1437 /* If we didn't get to CHARPOS - 1, there's some
1438 replacing display property at that position, and
1439 we stopped after it. That is exactly the place
1440 whose coordinates we want. */
1441 if (IT_CHARPOS (it2) != charpos - 1)
1442 it2_prev = it2;
1443 else
1445 /* Iterate until we get out of the display
1446 vector that displays the character at
1447 CHARPOS - 1. */
1448 do {
1449 get_next_display_element (&it2);
1450 PRODUCE_GLYPHS (&it2);
1451 it2_prev = it2;
1452 set_iterator_to_next (&it2, 1);
1453 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1454 && IT_CHARPOS (it2) < charpos);
1456 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1457 || it2_prev.current_x > it2_prev.last_visible_x)
1458 top_x = it.glyph_row->x;
1459 else
1461 top_x = it2_prev.current_x;
1462 top_y = it2_prev.current_y;
1466 else if (IT_CHARPOS (it) != charpos)
1468 Lisp_Object cpos = make_number (charpos);
1469 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1470 Lisp_Object string = string_from_display_spec (spec);
1471 struct text_pos tpos;
1472 int replacing_spec_p;
1473 bool newline_in_string
1474 = (STRINGP (string)
1475 && memchr (SDATA (string), '\n', SBYTES (string)));
1477 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1478 replacing_spec_p
1479 = (!NILP (spec)
1480 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1481 charpos, FRAME_WINDOW_P (it.f)));
1482 /* The tricky code below is needed because there's a
1483 discrepancy between move_it_to and how we set cursor
1484 when PT is at the beginning of a portion of text
1485 covered by a display property or an overlay with a
1486 display property, or the display line ends in a
1487 newline from a display string. move_it_to will stop
1488 _after_ such display strings, whereas
1489 set_cursor_from_row conspires with cursor_row_p to
1490 place the cursor on the first glyph produced from the
1491 display string. */
1493 /* We have overshoot PT because it is covered by a
1494 display property that replaces the text it covers.
1495 If the string includes embedded newlines, we are also
1496 in the wrong display line. Backtrack to the correct
1497 line, where the display property begins. */
1498 if (replacing_spec_p)
1500 Lisp_Object startpos, endpos;
1501 EMACS_INT start, end;
1502 struct it it3;
1503 int it3_moved;
1505 /* Find the first and the last buffer positions
1506 covered by the display string. */
1507 endpos =
1508 Fnext_single_char_property_change (cpos, Qdisplay,
1509 Qnil, Qnil);
1510 startpos =
1511 Fprevious_single_char_property_change (endpos, Qdisplay,
1512 Qnil, Qnil);
1513 start = XFASTINT (startpos);
1514 end = XFASTINT (endpos);
1515 /* Move to the last buffer position before the
1516 display property. */
1517 start_display (&it3, w, top);
1518 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1519 /* Move forward one more line if the position before
1520 the display string is a newline or if it is the
1521 rightmost character on a line that is
1522 continued or word-wrapped. */
1523 if (it3.method == GET_FROM_BUFFER
1524 && (it3.c == '\n'
1525 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1526 move_it_by_lines (&it3, 1);
1527 else if (move_it_in_display_line_to (&it3, -1,
1528 it3.current_x
1529 + it3.pixel_width,
1530 MOVE_TO_X)
1531 == MOVE_LINE_CONTINUED)
1533 move_it_by_lines (&it3, 1);
1534 /* When we are under word-wrap, the #$@%!
1535 move_it_by_lines moves 2 lines, so we need to
1536 fix that up. */
1537 if (it3.line_wrap == WORD_WRAP)
1538 move_it_by_lines (&it3, -1);
1541 /* Record the vertical coordinate of the display
1542 line where we wound up. */
1543 top_y = it3.current_y;
1544 if (it3.bidi_p)
1546 /* When characters are reordered for display,
1547 the character displayed to the left of the
1548 display string could be _after_ the display
1549 property in the logical order. Use the
1550 smallest vertical position of these two. */
1551 start_display (&it3, w, top);
1552 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1553 if (it3.current_y < top_y)
1554 top_y = it3.current_y;
1556 /* Move from the top of the window to the beginning
1557 of the display line where the display string
1558 begins. */
1559 start_display (&it3, w, top);
1560 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1561 /* If it3_moved stays zero after the 'while' loop
1562 below, that means we already were at a newline
1563 before the loop (e.g., the display string begins
1564 with a newline), so we don't need to (and cannot)
1565 inspect the glyphs of it3.glyph_row, because
1566 PRODUCE_GLYPHS will not produce anything for a
1567 newline, and thus it3.glyph_row stays at its
1568 stale content it got at top of the window. */
1569 it3_moved = 0;
1570 /* Finally, advance the iterator until we hit the
1571 first display element whose character position is
1572 CHARPOS, or until the first newline from the
1573 display string, which signals the end of the
1574 display line. */
1575 while (get_next_display_element (&it3))
1577 PRODUCE_GLYPHS (&it3);
1578 if (IT_CHARPOS (it3) == charpos
1579 || ITERATOR_AT_END_OF_LINE_P (&it3))
1580 break;
1581 it3_moved = 1;
1582 set_iterator_to_next (&it3, 0);
1584 top_x = it3.current_x - it3.pixel_width;
1585 /* Normally, we would exit the above loop because we
1586 found the display element whose character
1587 position is CHARPOS. For the contingency that we
1588 didn't, and stopped at the first newline from the
1589 display string, move back over the glyphs
1590 produced from the string, until we find the
1591 rightmost glyph not from the string. */
1592 if (it3_moved
1593 && newline_in_string
1594 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1596 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1597 + it3.glyph_row->used[TEXT_AREA];
1599 while (EQ ((g - 1)->object, string))
1601 --g;
1602 top_x -= g->pixel_width;
1604 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1605 + it3.glyph_row->used[TEXT_AREA]);
1610 *x = top_x;
1611 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1612 *rtop = max (0, window_top_y - top_y);
1613 *rbot = max (0, bottom_y - it.last_visible_y);
1614 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1615 - max (top_y, window_top_y)));
1616 *vpos = it.vpos;
1619 else
1621 /* We were asked to provide info about WINDOW_END. */
1622 struct it it2;
1623 void *it2data = NULL;
1625 SAVE_IT (it2, it, it2data);
1626 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1627 move_it_by_lines (&it, 1);
1628 if (charpos < IT_CHARPOS (it)
1629 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1631 visible_p = 1;
1632 RESTORE_IT (&it2, &it2, it2data);
1633 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1634 *x = it2.current_x;
1635 *y = it2.current_y + it2.max_ascent - it2.ascent;
1636 *rtop = max (0, -it2.current_y);
1637 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1638 - it.last_visible_y));
1639 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1640 it.last_visible_y)
1641 - max (it2.current_y,
1642 WINDOW_HEADER_LINE_HEIGHT (w))));
1643 *vpos = it2.vpos;
1645 else
1646 bidi_unshelve_cache (it2data, 1);
1648 bidi_unshelve_cache (itdata, 0);
1650 if (old_buffer)
1651 set_buffer_internal_1 (old_buffer);
1653 current_header_line_height = current_mode_line_height = -1;
1655 if (visible_p && w->hscroll > 0)
1656 *x -=
1657 window_hscroll_limited (w, WINDOW_XFRAME (w))
1658 * WINDOW_FRAME_COLUMN_WIDTH (w);
1660 #if 0
1661 /* Debugging code. */
1662 if (visible_p)
1663 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1664 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1665 else
1666 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1667 #endif
1669 return visible_p;
1673 /* Return the next character from STR. Return in *LEN the length of
1674 the character. This is like STRING_CHAR_AND_LENGTH but never
1675 returns an invalid character. If we find one, we return a `?', but
1676 with the length of the invalid character. */
1678 static int
1679 string_char_and_length (const unsigned char *str, int *len)
1681 int c;
1683 c = STRING_CHAR_AND_LENGTH (str, *len);
1684 if (!CHAR_VALID_P (c))
1685 /* We may not change the length here because other places in Emacs
1686 don't use this function, i.e. they silently accept invalid
1687 characters. */
1688 c = '?';
1690 return c;
1695 /* Given a position POS containing a valid character and byte position
1696 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1698 static struct text_pos
1699 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1701 eassert (STRINGP (string) && nchars >= 0);
1703 if (STRING_MULTIBYTE (string))
1705 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1706 int len;
1708 while (nchars--)
1710 string_char_and_length (p, &len);
1711 p += len;
1712 CHARPOS (pos) += 1;
1713 BYTEPOS (pos) += len;
1716 else
1717 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1719 return pos;
1723 /* Value is the text position, i.e. character and byte position,
1724 for character position CHARPOS in STRING. */
1726 static struct text_pos
1727 string_pos (ptrdiff_t charpos, Lisp_Object string)
1729 struct text_pos pos;
1730 eassert (STRINGP (string));
1731 eassert (charpos >= 0);
1732 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1733 return pos;
1737 /* Value is a text position, i.e. character and byte position, for
1738 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1739 means recognize multibyte characters. */
1741 static struct text_pos
1742 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1744 struct text_pos pos;
1746 eassert (s != NULL);
1747 eassert (charpos >= 0);
1749 if (multibyte_p)
1751 int len;
1753 SET_TEXT_POS (pos, 0, 0);
1754 while (charpos--)
1756 string_char_and_length ((const unsigned char *) s, &len);
1757 s += len;
1758 CHARPOS (pos) += 1;
1759 BYTEPOS (pos) += len;
1762 else
1763 SET_TEXT_POS (pos, charpos, charpos);
1765 return pos;
1769 /* Value is the number of characters in C string S. MULTIBYTE_P
1770 non-zero means recognize multibyte characters. */
1772 static ptrdiff_t
1773 number_of_chars (const char *s, bool multibyte_p)
1775 ptrdiff_t nchars;
1777 if (multibyte_p)
1779 ptrdiff_t rest = strlen (s);
1780 int len;
1781 const unsigned char *p = (const unsigned char *) s;
1783 for (nchars = 0; rest > 0; ++nchars)
1785 string_char_and_length (p, &len);
1786 rest -= len, p += len;
1789 else
1790 nchars = strlen (s);
1792 return nchars;
1796 /* Compute byte position NEWPOS->bytepos corresponding to
1797 NEWPOS->charpos. POS is a known position in string STRING.
1798 NEWPOS->charpos must be >= POS.charpos. */
1800 static void
1801 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1803 eassert (STRINGP (string));
1804 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1806 if (STRING_MULTIBYTE (string))
1807 *newpos = string_pos_nchars_ahead (pos, string,
1808 CHARPOS (*newpos) - CHARPOS (pos));
1809 else
1810 BYTEPOS (*newpos) = CHARPOS (*newpos);
1813 /* EXPORT:
1814 Return an estimation of the pixel height of mode or header lines on
1815 frame F. FACE_ID specifies what line's height to estimate. */
1818 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1820 #ifdef HAVE_WINDOW_SYSTEM
1821 if (FRAME_WINDOW_P (f))
1823 int height = FONT_HEIGHT (FRAME_FONT (f));
1825 /* This function is called so early when Emacs starts that the face
1826 cache and mode line face are not yet initialized. */
1827 if (FRAME_FACE_CACHE (f))
1829 struct face *face = FACE_FROM_ID (f, face_id);
1830 if (face)
1832 if (face->font)
1833 height = FONT_HEIGHT (face->font);
1834 if (face->box_line_width > 0)
1835 height += 2 * face->box_line_width;
1839 return height;
1841 #endif
1843 return 1;
1846 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1847 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1848 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1849 not force the value into range. */
1851 void
1852 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1853 int *x, int *y, NativeRectangle *bounds, int noclip)
1856 #ifdef HAVE_WINDOW_SYSTEM
1857 if (FRAME_WINDOW_P (f))
1859 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1860 even for negative values. */
1861 if (pix_x < 0)
1862 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1863 if (pix_y < 0)
1864 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1866 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1867 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1869 if (bounds)
1870 STORE_NATIVE_RECT (*bounds,
1871 FRAME_COL_TO_PIXEL_X (f, pix_x),
1872 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1873 FRAME_COLUMN_WIDTH (f) - 1,
1874 FRAME_LINE_HEIGHT (f) - 1);
1876 if (!noclip)
1878 if (pix_x < 0)
1879 pix_x = 0;
1880 else if (pix_x > FRAME_TOTAL_COLS (f))
1881 pix_x = FRAME_TOTAL_COLS (f);
1883 if (pix_y < 0)
1884 pix_y = 0;
1885 else if (pix_y > FRAME_LINES (f))
1886 pix_y = FRAME_LINES (f);
1889 #endif
1891 *x = pix_x;
1892 *y = pix_y;
1896 /* Find the glyph under window-relative coordinates X/Y in window W.
1897 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1898 strings. Return in *HPOS and *VPOS the row and column number of
1899 the glyph found. Return in *AREA the glyph area containing X.
1900 Value is a pointer to the glyph found or null if X/Y is not on
1901 text, or we can't tell because W's current matrix is not up to
1902 date. */
1904 static
1905 struct glyph *
1906 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1907 int *dx, int *dy, int *area)
1909 struct glyph *glyph, *end;
1910 struct glyph_row *row = NULL;
1911 int x0, i;
1913 /* Find row containing Y. Give up if some row is not enabled. */
1914 for (i = 0; i < w->current_matrix->nrows; ++i)
1916 row = MATRIX_ROW (w->current_matrix, i);
1917 if (!row->enabled_p)
1918 return NULL;
1919 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1920 break;
1923 *vpos = i;
1924 *hpos = 0;
1926 /* Give up if Y is not in the window. */
1927 if (i == w->current_matrix->nrows)
1928 return NULL;
1930 /* Get the glyph area containing X. */
1931 if (w->pseudo_window_p)
1933 *area = TEXT_AREA;
1934 x0 = 0;
1936 else
1938 if (x < window_box_left_offset (w, TEXT_AREA))
1940 *area = LEFT_MARGIN_AREA;
1941 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1943 else if (x < window_box_right_offset (w, TEXT_AREA))
1945 *area = TEXT_AREA;
1946 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1948 else
1950 *area = RIGHT_MARGIN_AREA;
1951 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1955 /* Find glyph containing X. */
1956 glyph = row->glyphs[*area];
1957 end = glyph + row->used[*area];
1958 x -= x0;
1959 while (glyph < end && x >= glyph->pixel_width)
1961 x -= glyph->pixel_width;
1962 ++glyph;
1965 if (glyph == end)
1966 return NULL;
1968 if (dx)
1970 *dx = x;
1971 *dy = y - (row->y + row->ascent - glyph->ascent);
1974 *hpos = glyph - row->glyphs[*area];
1975 return glyph;
1978 /* Convert frame-relative x/y to coordinates relative to window W.
1979 Takes pseudo-windows into account. */
1981 static void
1982 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1984 if (w->pseudo_window_p)
1986 /* A pseudo-window is always full-width, and starts at the
1987 left edge of the frame, plus a frame border. */
1988 struct frame *f = XFRAME (w->frame);
1989 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1990 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1992 else
1994 *x -= WINDOW_LEFT_EDGE_X (w);
1995 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1999 #ifdef HAVE_WINDOW_SYSTEM
2001 /* EXPORT:
2002 Return in RECTS[] at most N clipping rectangles for glyph string S.
2003 Return the number of stored rectangles. */
2006 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2008 XRectangle r;
2010 if (n <= 0)
2011 return 0;
2013 if (s->row->full_width_p)
2015 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2016 r.x = WINDOW_LEFT_EDGE_X (s->w);
2017 r.width = WINDOW_TOTAL_WIDTH (s->w);
2019 /* Unless displaying a mode or menu bar line, which are always
2020 fully visible, clip to the visible part of the row. */
2021 if (s->w->pseudo_window_p)
2022 r.height = s->row->visible_height;
2023 else
2024 r.height = s->height;
2026 else
2028 /* This is a text line that may be partially visible. */
2029 r.x = window_box_left (s->w, s->area);
2030 r.width = window_box_width (s->w, s->area);
2031 r.height = s->row->visible_height;
2034 if (s->clip_head)
2035 if (r.x < s->clip_head->x)
2037 if (r.width >= s->clip_head->x - r.x)
2038 r.width -= s->clip_head->x - r.x;
2039 else
2040 r.width = 0;
2041 r.x = s->clip_head->x;
2043 if (s->clip_tail)
2044 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2046 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2047 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2048 else
2049 r.width = 0;
2052 /* If S draws overlapping rows, it's sufficient to use the top and
2053 bottom of the window for clipping because this glyph string
2054 intentionally draws over other lines. */
2055 if (s->for_overlaps)
2057 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2058 r.height = window_text_bottom_y (s->w) - r.y;
2060 /* Alas, the above simple strategy does not work for the
2061 environments with anti-aliased text: if the same text is
2062 drawn onto the same place multiple times, it gets thicker.
2063 If the overlap we are processing is for the erased cursor, we
2064 take the intersection with the rectangle of the cursor. */
2065 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2067 XRectangle rc, r_save = r;
2069 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2070 rc.y = s->w->phys_cursor.y;
2071 rc.width = s->w->phys_cursor_width;
2072 rc.height = s->w->phys_cursor_height;
2074 x_intersect_rectangles (&r_save, &rc, &r);
2077 else
2079 /* Don't use S->y for clipping because it doesn't take partially
2080 visible lines into account. For example, it can be negative for
2081 partially visible lines at the top of a window. */
2082 if (!s->row->full_width_p
2083 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2084 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2085 else
2086 r.y = max (0, s->row->y);
2089 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2091 /* If drawing the cursor, don't let glyph draw outside its
2092 advertised boundaries. Cleartype does this under some circumstances. */
2093 if (s->hl == DRAW_CURSOR)
2095 struct glyph *glyph = s->first_glyph;
2096 int height, max_y;
2098 if (s->x > r.x)
2100 r.width -= s->x - r.x;
2101 r.x = s->x;
2103 r.width = min (r.width, glyph->pixel_width);
2105 /* If r.y is below window bottom, ensure that we still see a cursor. */
2106 height = min (glyph->ascent + glyph->descent,
2107 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2108 max_y = window_text_bottom_y (s->w) - height;
2109 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2110 if (s->ybase - glyph->ascent > max_y)
2112 r.y = max_y;
2113 r.height = height;
2115 else
2117 /* Don't draw cursor glyph taller than our actual glyph. */
2118 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2119 if (height < r.height)
2121 max_y = r.y + r.height;
2122 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2123 r.height = min (max_y - r.y, height);
2128 if (s->row->clip)
2130 XRectangle r_save = r;
2132 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2133 r.width = 0;
2136 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2137 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2139 #ifdef CONVERT_FROM_XRECT
2140 CONVERT_FROM_XRECT (r, *rects);
2141 #else
2142 *rects = r;
2143 #endif
2144 return 1;
2146 else
2148 /* If we are processing overlapping and allowed to return
2149 multiple clipping rectangles, we exclude the row of the glyph
2150 string from the clipping rectangle. This is to avoid drawing
2151 the same text on the environment with anti-aliasing. */
2152 #ifdef CONVERT_FROM_XRECT
2153 XRectangle rs[2];
2154 #else
2155 XRectangle *rs = rects;
2156 #endif
2157 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2159 if (s->for_overlaps & OVERLAPS_PRED)
2161 rs[i] = r;
2162 if (r.y + r.height > row_y)
2164 if (r.y < row_y)
2165 rs[i].height = row_y - r.y;
2166 else
2167 rs[i].height = 0;
2169 i++;
2171 if (s->for_overlaps & OVERLAPS_SUCC)
2173 rs[i] = r;
2174 if (r.y < row_y + s->row->visible_height)
2176 if (r.y + r.height > row_y + s->row->visible_height)
2178 rs[i].y = row_y + s->row->visible_height;
2179 rs[i].height = r.y + r.height - rs[i].y;
2181 else
2182 rs[i].height = 0;
2184 i++;
2187 n = i;
2188 #ifdef CONVERT_FROM_XRECT
2189 for (i = 0; i < n; i++)
2190 CONVERT_FROM_XRECT (rs[i], rects[i]);
2191 #endif
2192 return n;
2196 /* EXPORT:
2197 Return in *NR the clipping rectangle for glyph string S. */
2199 void
2200 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2202 get_glyph_string_clip_rects (s, nr, 1);
2206 /* EXPORT:
2207 Return the position and height of the phys cursor in window W.
2208 Set w->phys_cursor_width to width of phys cursor.
2211 void
2212 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2213 struct glyph *glyph, int *xp, int *yp, int *heightp)
2215 struct frame *f = XFRAME (WINDOW_FRAME (w));
2216 int x, y, wd, h, h0, y0;
2218 /* Compute the width of the rectangle to draw. If on a stretch
2219 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2220 rectangle as wide as the glyph, but use a canonical character
2221 width instead. */
2222 wd = glyph->pixel_width - 1;
2223 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2224 wd++; /* Why? */
2225 #endif
2227 x = w->phys_cursor.x;
2228 if (x < 0)
2230 wd += x;
2231 x = 0;
2234 if (glyph->type == STRETCH_GLYPH
2235 && !x_stretch_cursor_p)
2236 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2237 w->phys_cursor_width = wd;
2239 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2241 /* If y is below window bottom, ensure that we still see a cursor. */
2242 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2244 h = max (h0, glyph->ascent + glyph->descent);
2245 h0 = min (h0, glyph->ascent + glyph->descent);
2247 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2248 if (y < y0)
2250 h = max (h - (y0 - y) + 1, h0);
2251 y = y0 - 1;
2253 else
2255 y0 = window_text_bottom_y (w) - h0;
2256 if (y > y0)
2258 h += y - y0;
2259 y = y0;
2263 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2264 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2265 *heightp = h;
2269 * Remember which glyph the mouse is over.
2272 void
2273 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2275 Lisp_Object window;
2276 struct window *w;
2277 struct glyph_row *r, *gr, *end_row;
2278 enum window_part part;
2279 enum glyph_row_area area;
2280 int x, y, width, height;
2282 /* Try to determine frame pixel position and size of the glyph under
2283 frame pixel coordinates X/Y on frame F. */
2285 if (!f->glyphs_initialized_p
2286 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2287 NILP (window)))
2289 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2290 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2291 goto virtual_glyph;
2294 w = XWINDOW (window);
2295 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2296 height = WINDOW_FRAME_LINE_HEIGHT (w);
2298 x = window_relative_x_coord (w, part, gx);
2299 y = gy - WINDOW_TOP_EDGE_Y (w);
2301 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2302 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2304 if (w->pseudo_window_p)
2306 area = TEXT_AREA;
2307 part = ON_MODE_LINE; /* Don't adjust margin. */
2308 goto text_glyph;
2311 switch (part)
2313 case ON_LEFT_MARGIN:
2314 area = LEFT_MARGIN_AREA;
2315 goto text_glyph;
2317 case ON_RIGHT_MARGIN:
2318 area = RIGHT_MARGIN_AREA;
2319 goto text_glyph;
2321 case ON_HEADER_LINE:
2322 case ON_MODE_LINE:
2323 gr = (part == ON_HEADER_LINE
2324 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2325 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2326 gy = gr->y;
2327 area = TEXT_AREA;
2328 goto text_glyph_row_found;
2330 case ON_TEXT:
2331 area = TEXT_AREA;
2333 text_glyph:
2334 gr = 0; gy = 0;
2335 for (; r <= end_row && r->enabled_p; ++r)
2336 if (r->y + r->height > y)
2338 gr = r; gy = r->y;
2339 break;
2342 text_glyph_row_found:
2343 if (gr && gy <= y)
2345 struct glyph *g = gr->glyphs[area];
2346 struct glyph *end = g + gr->used[area];
2348 height = gr->height;
2349 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2350 if (gx + g->pixel_width > x)
2351 break;
2353 if (g < end)
2355 if (g->type == IMAGE_GLYPH)
2357 /* Don't remember when mouse is over image, as
2358 image may have hot-spots. */
2359 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2360 return;
2362 width = g->pixel_width;
2364 else
2366 /* Use nominal char spacing at end of line. */
2367 x -= gx;
2368 gx += (x / width) * width;
2371 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2372 gx += window_box_left_offset (w, area);
2374 else
2376 /* Use nominal line height at end of window. */
2377 gx = (x / width) * width;
2378 y -= gy;
2379 gy += (y / height) * height;
2381 break;
2383 case ON_LEFT_FRINGE:
2384 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2385 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2386 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2387 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2388 goto row_glyph;
2390 case ON_RIGHT_FRINGE:
2391 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2392 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2393 : window_box_right_offset (w, TEXT_AREA));
2394 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2395 goto row_glyph;
2397 case ON_SCROLL_BAR:
2398 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2400 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2401 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2402 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2403 : 0)));
2404 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2406 row_glyph:
2407 gr = 0, gy = 0;
2408 for (; r <= end_row && r->enabled_p; ++r)
2409 if (r->y + r->height > y)
2411 gr = r; gy = r->y;
2412 break;
2415 if (gr && gy <= y)
2416 height = gr->height;
2417 else
2419 /* Use nominal line height at end of window. */
2420 y -= gy;
2421 gy += (y / height) * height;
2423 break;
2425 default:
2427 virtual_glyph:
2428 /* If there is no glyph under the mouse, then we divide the screen
2429 into a grid of the smallest glyph in the frame, and use that
2430 as our "glyph". */
2432 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2433 round down even for negative values. */
2434 if (gx < 0)
2435 gx -= width - 1;
2436 if (gy < 0)
2437 gy -= height - 1;
2439 gx = (gx / width) * width;
2440 gy = (gy / height) * height;
2442 goto store_rect;
2445 gx += WINDOW_LEFT_EDGE_X (w);
2446 gy += WINDOW_TOP_EDGE_Y (w);
2448 store_rect:
2449 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2451 /* Visible feedback for debugging. */
2452 #if 0
2453 #if HAVE_X_WINDOWS
2454 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2455 f->output_data.x->normal_gc,
2456 gx, gy, width, height);
2457 #endif
2458 #endif
2462 #endif /* HAVE_WINDOW_SYSTEM */
2464 static void
2465 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2467 eassert (w);
2468 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2469 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2470 w->window_end_vpos
2471 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2474 /***********************************************************************
2475 Lisp form evaluation
2476 ***********************************************************************/
2478 /* Error handler for safe_eval and safe_call. */
2480 static Lisp_Object
2481 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2483 add_to_log ("Error during redisplay: %S signaled %S",
2484 Flist (nargs, args), arg);
2485 return Qnil;
2488 /* Call function FUNC with the rest of NARGS - 1 arguments
2489 following. Return the result, or nil if something went
2490 wrong. Prevent redisplay during the evaluation. */
2492 Lisp_Object
2493 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2495 Lisp_Object val;
2497 if (inhibit_eval_during_redisplay)
2498 val = Qnil;
2499 else
2501 va_list ap;
2502 ptrdiff_t i;
2503 ptrdiff_t count = SPECPDL_INDEX ();
2504 struct gcpro gcpro1;
2505 Lisp_Object *args = alloca (nargs * word_size);
2507 args[0] = func;
2508 va_start (ap, func);
2509 for (i = 1; i < nargs; i++)
2510 args[i] = va_arg (ap, Lisp_Object);
2511 va_end (ap);
2513 GCPRO1 (args[0]);
2514 gcpro1.nvars = nargs;
2515 specbind (Qinhibit_redisplay, Qt);
2516 /* Use Qt to ensure debugger does not run,
2517 so there is no possibility of wanting to redisplay. */
2518 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2519 safe_eval_handler);
2520 UNGCPRO;
2521 val = unbind_to (count, val);
2524 return val;
2528 /* Call function FN with one argument ARG.
2529 Return the result, or nil if something went wrong. */
2531 Lisp_Object
2532 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2534 return safe_call (2, fn, arg);
2537 static Lisp_Object Qeval;
2539 Lisp_Object
2540 safe_eval (Lisp_Object sexpr)
2542 return safe_call1 (Qeval, sexpr);
2545 /* Call function FN with two arguments ARG1 and ARG2.
2546 Return the result, or nil if something went wrong. */
2548 Lisp_Object
2549 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2551 return safe_call (3, fn, arg1, arg2);
2556 /***********************************************************************
2557 Debugging
2558 ***********************************************************************/
2560 #if 0
2562 /* Define CHECK_IT to perform sanity checks on iterators.
2563 This is for debugging. It is too slow to do unconditionally. */
2565 static void
2566 check_it (struct it *it)
2568 if (it->method == GET_FROM_STRING)
2570 eassert (STRINGP (it->string));
2571 eassert (IT_STRING_CHARPOS (*it) >= 0);
2573 else
2575 eassert (IT_STRING_CHARPOS (*it) < 0);
2576 if (it->method == GET_FROM_BUFFER)
2578 /* Check that character and byte positions agree. */
2579 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2583 if (it->dpvec)
2584 eassert (it->current.dpvec_index >= 0);
2585 else
2586 eassert (it->current.dpvec_index < 0);
2589 #define CHECK_IT(IT) check_it ((IT))
2591 #else /* not 0 */
2593 #define CHECK_IT(IT) (void) 0
2595 #endif /* not 0 */
2598 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2600 /* Check that the window end of window W is what we expect it
2601 to be---the last row in the current matrix displaying text. */
2603 static void
2604 check_window_end (struct window *w)
2606 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2608 struct glyph_row *row;
2609 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2610 !row->enabled_p
2611 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2612 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2616 #define CHECK_WINDOW_END(W) check_window_end ((W))
2618 #else
2620 #define CHECK_WINDOW_END(W) (void) 0
2622 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2624 /* Return mark position if current buffer has the region of non-zero length,
2625 or -1 otherwise. */
2627 static ptrdiff_t
2628 markpos_of_region (void)
2630 if (!NILP (Vtransient_mark_mode)
2631 && !NILP (BVAR (current_buffer, mark_active))
2632 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2634 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2636 if (markpos != PT)
2637 return markpos;
2639 return -1;
2642 /***********************************************************************
2643 Iterator initialization
2644 ***********************************************************************/
2646 /* Initialize IT for displaying current_buffer in window W, starting
2647 at character position CHARPOS. CHARPOS < 0 means that no buffer
2648 position is specified which is useful when the iterator is assigned
2649 a position later. BYTEPOS is the byte position corresponding to
2650 CHARPOS.
2652 If ROW is not null, calls to produce_glyphs with IT as parameter
2653 will produce glyphs in that row.
2655 BASE_FACE_ID is the id of a base face to use. It must be one of
2656 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2657 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2658 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2660 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2661 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2662 will be initialized to use the corresponding mode line glyph row of
2663 the desired matrix of W. */
2665 void
2666 init_iterator (struct it *it, struct window *w,
2667 ptrdiff_t charpos, ptrdiff_t bytepos,
2668 struct glyph_row *row, enum face_id base_face_id)
2670 ptrdiff_t markpos;
2671 enum face_id remapped_base_face_id = base_face_id;
2673 /* Some precondition checks. */
2674 eassert (w != NULL && it != NULL);
2675 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2676 && charpos <= ZV));
2678 /* If face attributes have been changed since the last redisplay,
2679 free realized faces now because they depend on face definitions
2680 that might have changed. Don't free faces while there might be
2681 desired matrices pending which reference these faces. */
2682 if (face_change_count && !inhibit_free_realized_faces)
2684 face_change_count = 0;
2685 free_all_realized_faces (Qnil);
2688 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2689 if (! NILP (Vface_remapping_alist))
2690 remapped_base_face_id
2691 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2693 /* Use one of the mode line rows of W's desired matrix if
2694 appropriate. */
2695 if (row == NULL)
2697 if (base_face_id == MODE_LINE_FACE_ID
2698 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2699 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2700 else if (base_face_id == HEADER_LINE_FACE_ID)
2701 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2704 /* Clear IT. */
2705 memset (it, 0, sizeof *it);
2706 it->current.overlay_string_index = -1;
2707 it->current.dpvec_index = -1;
2708 it->base_face_id = remapped_base_face_id;
2709 it->string = Qnil;
2710 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2711 it->paragraph_embedding = L2R;
2712 it->bidi_it.string.lstring = Qnil;
2713 it->bidi_it.string.s = NULL;
2714 it->bidi_it.string.bufpos = 0;
2715 it->bidi_it.w = w;
2717 /* The window in which we iterate over current_buffer: */
2718 XSETWINDOW (it->window, w);
2719 it->w = w;
2720 it->f = XFRAME (w->frame);
2722 it->cmp_it.id = -1;
2724 /* Extra space between lines (on window systems only). */
2725 if (base_face_id == DEFAULT_FACE_ID
2726 && FRAME_WINDOW_P (it->f))
2728 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2729 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2730 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2731 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2732 * FRAME_LINE_HEIGHT (it->f));
2733 else if (it->f->extra_line_spacing > 0)
2734 it->extra_line_spacing = it->f->extra_line_spacing;
2735 it->max_extra_line_spacing = 0;
2738 /* If realized faces have been removed, e.g. because of face
2739 attribute changes of named faces, recompute them. When running
2740 in batch mode, the face cache of the initial frame is null. If
2741 we happen to get called, make a dummy face cache. */
2742 if (FRAME_FACE_CACHE (it->f) == NULL)
2743 init_frame_faces (it->f);
2744 if (FRAME_FACE_CACHE (it->f)->used == 0)
2745 recompute_basic_faces (it->f);
2747 /* Current value of the `slice', `space-width', and 'height' properties. */
2748 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2749 it->space_width = Qnil;
2750 it->font_height = Qnil;
2751 it->override_ascent = -1;
2753 /* Are control characters displayed as `^C'? */
2754 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2756 /* -1 means everything between a CR and the following line end
2757 is invisible. >0 means lines indented more than this value are
2758 invisible. */
2759 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2760 ? (clip_to_bounds
2761 (-1, XINT (BVAR (current_buffer, selective_display)),
2762 PTRDIFF_MAX))
2763 : (!NILP (BVAR (current_buffer, selective_display))
2764 ? -1 : 0));
2765 it->selective_display_ellipsis_p
2766 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2768 /* Display table to use. */
2769 it->dp = window_display_table (w);
2771 /* Are multibyte characters enabled in current_buffer? */
2772 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2774 /* If visible region is of non-zero length, set IT->region_beg_charpos
2775 and IT->region_end_charpos to the start and end of a visible region
2776 in window IT->w. Set both to -1 to indicate no region. */
2777 markpos = markpos_of_region ();
2778 if (markpos >= 0
2779 /* Maybe highlight only in selected window. */
2780 && (/* Either show region everywhere. */
2781 highlight_nonselected_windows
2782 /* Or show region in the selected window. */
2783 || w == XWINDOW (selected_window)
2784 /* Or show the region if we are in the mini-buffer and W is
2785 the window the mini-buffer refers to. */
2786 || (MINI_WINDOW_P (XWINDOW (selected_window))
2787 && WINDOWP (minibuf_selected_window)
2788 && w == XWINDOW (minibuf_selected_window))))
2790 it->region_beg_charpos = min (PT, markpos);
2791 it->region_end_charpos = max (PT, markpos);
2793 else
2794 it->region_beg_charpos = it->region_end_charpos = -1;
2796 /* Get the position at which the redisplay_end_trigger hook should
2797 be run, if it is to be run at all. */
2798 if (MARKERP (w->redisplay_end_trigger)
2799 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2800 it->redisplay_end_trigger_charpos
2801 = marker_position (w->redisplay_end_trigger);
2802 else if (INTEGERP (w->redisplay_end_trigger))
2803 it->redisplay_end_trigger_charpos =
2804 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2806 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2808 /* Are lines in the display truncated? */
2809 if (base_face_id != DEFAULT_FACE_ID
2810 || it->w->hscroll
2811 || (! WINDOW_FULL_WIDTH_P (it->w)
2812 && ((!NILP (Vtruncate_partial_width_windows)
2813 && !INTEGERP (Vtruncate_partial_width_windows))
2814 || (INTEGERP (Vtruncate_partial_width_windows)
2815 && (WINDOW_TOTAL_COLS (it->w)
2816 < XINT (Vtruncate_partial_width_windows))))))
2817 it->line_wrap = TRUNCATE;
2818 else if (NILP (BVAR (current_buffer, truncate_lines)))
2819 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2820 ? WINDOW_WRAP : WORD_WRAP;
2821 else
2822 it->line_wrap = TRUNCATE;
2824 /* Get dimensions of truncation and continuation glyphs. These are
2825 displayed as fringe bitmaps under X, but we need them for such
2826 frames when the fringes are turned off. But leave the dimensions
2827 zero for tooltip frames, as these glyphs look ugly there and also
2828 sabotage calculations of tooltip dimensions in x-show-tip. */
2829 #ifdef HAVE_WINDOW_SYSTEM
2830 if (!(FRAME_WINDOW_P (it->f)
2831 && FRAMEP (tip_frame)
2832 && it->f == XFRAME (tip_frame)))
2833 #endif
2835 if (it->line_wrap == TRUNCATE)
2837 /* We will need the truncation glyph. */
2838 eassert (it->glyph_row == NULL);
2839 produce_special_glyphs (it, IT_TRUNCATION);
2840 it->truncation_pixel_width = it->pixel_width;
2842 else
2844 /* We will need the continuation glyph. */
2845 eassert (it->glyph_row == NULL);
2846 produce_special_glyphs (it, IT_CONTINUATION);
2847 it->continuation_pixel_width = it->pixel_width;
2851 /* Reset these values to zero because the produce_special_glyphs
2852 above has changed them. */
2853 it->pixel_width = it->ascent = it->descent = 0;
2854 it->phys_ascent = it->phys_descent = 0;
2856 /* Set this after getting the dimensions of truncation and
2857 continuation glyphs, so that we don't produce glyphs when calling
2858 produce_special_glyphs, above. */
2859 it->glyph_row = row;
2860 it->area = TEXT_AREA;
2862 /* Forget any previous info about this row being reversed. */
2863 if (it->glyph_row)
2864 it->glyph_row->reversed_p = 0;
2866 /* Get the dimensions of the display area. The display area
2867 consists of the visible window area plus a horizontally scrolled
2868 part to the left of the window. All x-values are relative to the
2869 start of this total display area. */
2870 if (base_face_id != DEFAULT_FACE_ID)
2872 /* Mode lines, menu bar in terminal frames. */
2873 it->first_visible_x = 0;
2874 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2876 else
2878 it->first_visible_x =
2879 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2880 it->last_visible_x = (it->first_visible_x
2881 + window_box_width (w, TEXT_AREA));
2883 /* If we truncate lines, leave room for the truncation glyph(s) at
2884 the right margin. Otherwise, leave room for the continuation
2885 glyph(s). Done only if the window has no fringes. Since we
2886 don't know at this point whether there will be any R2L lines in
2887 the window, we reserve space for truncation/continuation glyphs
2888 even if only one of the fringes is absent. */
2889 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2890 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2892 if (it->line_wrap == TRUNCATE)
2893 it->last_visible_x -= it->truncation_pixel_width;
2894 else
2895 it->last_visible_x -= it->continuation_pixel_width;
2898 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2899 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2902 /* Leave room for a border glyph. */
2903 if (!FRAME_WINDOW_P (it->f)
2904 && !WINDOW_RIGHTMOST_P (it->w))
2905 it->last_visible_x -= 1;
2907 it->last_visible_y = window_text_bottom_y (w);
2909 /* For mode lines and alike, arrange for the first glyph having a
2910 left box line if the face specifies a box. */
2911 if (base_face_id != DEFAULT_FACE_ID)
2913 struct face *face;
2915 it->face_id = remapped_base_face_id;
2917 /* If we have a boxed mode line, make the first character appear
2918 with a left box line. */
2919 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2920 if (face->box != FACE_NO_BOX)
2921 it->start_of_box_run_p = 1;
2924 /* If a buffer position was specified, set the iterator there,
2925 getting overlays and face properties from that position. */
2926 if (charpos >= BUF_BEG (current_buffer))
2928 it->end_charpos = ZV;
2929 eassert (charpos == BYTE_TO_CHAR (bytepos));
2930 IT_CHARPOS (*it) = charpos;
2931 IT_BYTEPOS (*it) = bytepos;
2933 /* We will rely on `reseat' to set this up properly, via
2934 handle_face_prop. */
2935 it->face_id = it->base_face_id;
2937 it->start = it->current;
2938 /* Do we need to reorder bidirectional text? Not if this is a
2939 unibyte buffer: by definition, none of the single-byte
2940 characters are strong R2L, so no reordering is needed. And
2941 bidi.c doesn't support unibyte buffers anyway. Also, don't
2942 reorder while we are loading loadup.el, since the tables of
2943 character properties needed for reordering are not yet
2944 available. */
2945 it->bidi_p =
2946 NILP (Vpurify_flag)
2947 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2948 && it->multibyte_p;
2950 /* If we are to reorder bidirectional text, init the bidi
2951 iterator. */
2952 if (it->bidi_p)
2954 /* Note the paragraph direction that this buffer wants to
2955 use. */
2956 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2957 Qleft_to_right))
2958 it->paragraph_embedding = L2R;
2959 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2960 Qright_to_left))
2961 it->paragraph_embedding = R2L;
2962 else
2963 it->paragraph_embedding = NEUTRAL_DIR;
2964 bidi_unshelve_cache (NULL, 0);
2965 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2966 &it->bidi_it);
2969 /* Compute faces etc. */
2970 reseat (it, it->current.pos, 1);
2973 CHECK_IT (it);
2977 /* Initialize IT for the display of window W with window start POS. */
2979 void
2980 start_display (struct it *it, struct window *w, struct text_pos pos)
2982 struct glyph_row *row;
2983 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2985 row = w->desired_matrix->rows + first_vpos;
2986 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2987 it->first_vpos = first_vpos;
2989 /* Don't reseat to previous visible line start if current start
2990 position is in a string or image. */
2991 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2993 int start_at_line_beg_p;
2994 int first_y = it->current_y;
2996 /* If window start is not at a line start, skip forward to POS to
2997 get the correct continuation lines width. */
2998 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2999 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3000 if (!start_at_line_beg_p)
3002 int new_x;
3004 reseat_at_previous_visible_line_start (it);
3005 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3007 new_x = it->current_x + it->pixel_width;
3009 /* If lines are continued, this line may end in the middle
3010 of a multi-glyph character (e.g. a control character
3011 displayed as \003, or in the middle of an overlay
3012 string). In this case move_it_to above will not have
3013 taken us to the start of the continuation line but to the
3014 end of the continued line. */
3015 if (it->current_x > 0
3016 && it->line_wrap != TRUNCATE /* Lines are continued. */
3017 && (/* And glyph doesn't fit on the line. */
3018 new_x > it->last_visible_x
3019 /* Or it fits exactly and we're on a window
3020 system frame. */
3021 || (new_x == it->last_visible_x
3022 && FRAME_WINDOW_P (it->f)
3023 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3024 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3025 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3027 if ((it->current.dpvec_index >= 0
3028 || it->current.overlay_string_index >= 0)
3029 /* If we are on a newline from a display vector or
3030 overlay string, then we are already at the end of
3031 a screen line; no need to go to the next line in
3032 that case, as this line is not really continued.
3033 (If we do go to the next line, C-e will not DTRT.) */
3034 && it->c != '\n')
3036 set_iterator_to_next (it, 1);
3037 move_it_in_display_line_to (it, -1, -1, 0);
3040 it->continuation_lines_width += it->current_x;
3042 /* If the character at POS is displayed via a display
3043 vector, move_it_to above stops at the final glyph of
3044 IT->dpvec. To make the caller redisplay that character
3045 again (a.k.a. start at POS), we need to reset the
3046 dpvec_index to the beginning of IT->dpvec. */
3047 else if (it->current.dpvec_index >= 0)
3048 it->current.dpvec_index = 0;
3050 /* We're starting a new display line, not affected by the
3051 height of the continued line, so clear the appropriate
3052 fields in the iterator structure. */
3053 it->max_ascent = it->max_descent = 0;
3054 it->max_phys_ascent = it->max_phys_descent = 0;
3056 it->current_y = first_y;
3057 it->vpos = 0;
3058 it->current_x = it->hpos = 0;
3064 /* Return 1 if POS is a position in ellipses displayed for invisible
3065 text. W is the window we display, for text property lookup. */
3067 static int
3068 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3070 Lisp_Object prop, window;
3071 int ellipses_p = 0;
3072 ptrdiff_t charpos = CHARPOS (pos->pos);
3074 /* If POS specifies a position in a display vector, this might
3075 be for an ellipsis displayed for invisible text. We won't
3076 get the iterator set up for delivering that ellipsis unless
3077 we make sure that it gets aware of the invisible text. */
3078 if (pos->dpvec_index >= 0
3079 && pos->overlay_string_index < 0
3080 && CHARPOS (pos->string_pos) < 0
3081 && charpos > BEGV
3082 && (XSETWINDOW (window, w),
3083 prop = Fget_char_property (make_number (charpos),
3084 Qinvisible, window),
3085 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3087 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3088 window);
3089 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3092 return ellipses_p;
3096 /* Initialize IT for stepping through current_buffer in window W,
3097 starting at position POS that includes overlay string and display
3098 vector/ control character translation position information. Value
3099 is zero if there are overlay strings with newlines at POS. */
3101 static int
3102 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3104 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3105 int i, overlay_strings_with_newlines = 0;
3107 /* If POS specifies a position in a display vector, this might
3108 be for an ellipsis displayed for invisible text. We won't
3109 get the iterator set up for delivering that ellipsis unless
3110 we make sure that it gets aware of the invisible text. */
3111 if (in_ellipses_for_invisible_text_p (pos, w))
3113 --charpos;
3114 bytepos = 0;
3117 /* Keep in mind: the call to reseat in init_iterator skips invisible
3118 text, so we might end up at a position different from POS. This
3119 is only a problem when POS is a row start after a newline and an
3120 overlay starts there with an after-string, and the overlay has an
3121 invisible property. Since we don't skip invisible text in
3122 display_line and elsewhere immediately after consuming the
3123 newline before the row start, such a POS will not be in a string,
3124 but the call to init_iterator below will move us to the
3125 after-string. */
3126 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3128 /* This only scans the current chunk -- it should scan all chunks.
3129 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3130 to 16 in 22.1 to make this a lesser problem. */
3131 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3133 const char *s = SSDATA (it->overlay_strings[i]);
3134 const char *e = s + SBYTES (it->overlay_strings[i]);
3136 while (s < e && *s != '\n')
3137 ++s;
3139 if (s < e)
3141 overlay_strings_with_newlines = 1;
3142 break;
3146 /* If position is within an overlay string, set up IT to the right
3147 overlay string. */
3148 if (pos->overlay_string_index >= 0)
3150 int relative_index;
3152 /* If the first overlay string happens to have a `display'
3153 property for an image, the iterator will be set up for that
3154 image, and we have to undo that setup first before we can
3155 correct the overlay string index. */
3156 if (it->method == GET_FROM_IMAGE)
3157 pop_it (it);
3159 /* We already have the first chunk of overlay strings in
3160 IT->overlay_strings. Load more until the one for
3161 pos->overlay_string_index is in IT->overlay_strings. */
3162 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3164 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3165 it->current.overlay_string_index = 0;
3166 while (n--)
3168 load_overlay_strings (it, 0);
3169 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3173 it->current.overlay_string_index = pos->overlay_string_index;
3174 relative_index = (it->current.overlay_string_index
3175 % OVERLAY_STRING_CHUNK_SIZE);
3176 it->string = it->overlay_strings[relative_index];
3177 eassert (STRINGP (it->string));
3178 it->current.string_pos = pos->string_pos;
3179 it->method = GET_FROM_STRING;
3180 it->end_charpos = SCHARS (it->string);
3181 /* Set up the bidi iterator for this overlay string. */
3182 if (it->bidi_p)
3184 it->bidi_it.string.lstring = it->string;
3185 it->bidi_it.string.s = NULL;
3186 it->bidi_it.string.schars = SCHARS (it->string);
3187 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3188 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3189 it->bidi_it.string.unibyte = !it->multibyte_p;
3190 it->bidi_it.w = it->w;
3191 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3192 FRAME_WINDOW_P (it->f), &it->bidi_it);
3194 /* Synchronize the state of the bidi iterator with
3195 pos->string_pos. For any string position other than
3196 zero, this will be done automagically when we resume
3197 iteration over the string and get_visually_first_element
3198 is called. But if string_pos is zero, and the string is
3199 to be reordered for display, we need to resync manually,
3200 since it could be that the iteration state recorded in
3201 pos ended at string_pos of 0 moving backwards in string. */
3202 if (CHARPOS (pos->string_pos) == 0)
3204 get_visually_first_element (it);
3205 if (IT_STRING_CHARPOS (*it) != 0)
3206 do {
3207 /* Paranoia. */
3208 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3209 bidi_move_to_visually_next (&it->bidi_it);
3210 } while (it->bidi_it.charpos != 0);
3212 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3213 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3217 if (CHARPOS (pos->string_pos) >= 0)
3219 /* Recorded position is not in an overlay string, but in another
3220 string. This can only be a string from a `display' property.
3221 IT should already be filled with that string. */
3222 it->current.string_pos = pos->string_pos;
3223 eassert (STRINGP (it->string));
3224 if (it->bidi_p)
3225 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3226 FRAME_WINDOW_P (it->f), &it->bidi_it);
3229 /* Restore position in display vector translations, control
3230 character translations or ellipses. */
3231 if (pos->dpvec_index >= 0)
3233 if (it->dpvec == NULL)
3234 get_next_display_element (it);
3235 eassert (it->dpvec && it->current.dpvec_index == 0);
3236 it->current.dpvec_index = pos->dpvec_index;
3239 CHECK_IT (it);
3240 return !overlay_strings_with_newlines;
3244 /* Initialize IT for stepping through current_buffer in window W
3245 starting at ROW->start. */
3247 static void
3248 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3250 init_from_display_pos (it, w, &row->start);
3251 it->start = row->start;
3252 it->continuation_lines_width = row->continuation_lines_width;
3253 CHECK_IT (it);
3257 /* Initialize IT for stepping through current_buffer in window W
3258 starting in the line following ROW, i.e. starting at ROW->end.
3259 Value is zero if there are overlay strings with newlines at ROW's
3260 end position. */
3262 static int
3263 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3265 int success = 0;
3267 if (init_from_display_pos (it, w, &row->end))
3269 if (row->continued_p)
3270 it->continuation_lines_width
3271 = row->continuation_lines_width + row->pixel_width;
3272 CHECK_IT (it);
3273 success = 1;
3276 return success;
3282 /***********************************************************************
3283 Text properties
3284 ***********************************************************************/
3286 /* Called when IT reaches IT->stop_charpos. Handle text property and
3287 overlay changes. Set IT->stop_charpos to the next position where
3288 to stop. */
3290 static void
3291 handle_stop (struct it *it)
3293 enum prop_handled handled;
3294 int handle_overlay_change_p;
3295 struct props *p;
3297 it->dpvec = NULL;
3298 it->current.dpvec_index = -1;
3299 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3300 it->ignore_overlay_strings_at_pos_p = 0;
3301 it->ellipsis_p = 0;
3303 /* Use face of preceding text for ellipsis (if invisible) */
3304 if (it->selective_display_ellipsis_p)
3305 it->saved_face_id = it->face_id;
3309 handled = HANDLED_NORMALLY;
3311 /* Call text property handlers. */
3312 for (p = it_props; p->handler; ++p)
3314 handled = p->handler (it);
3316 if (handled == HANDLED_RECOMPUTE_PROPS)
3317 break;
3318 else if (handled == HANDLED_RETURN)
3320 /* We still want to show before and after strings from
3321 overlays even if the actual buffer text is replaced. */
3322 if (!handle_overlay_change_p
3323 || it->sp > 1
3324 /* Don't call get_overlay_strings_1 if we already
3325 have overlay strings loaded, because doing so
3326 will load them again and push the iterator state
3327 onto the stack one more time, which is not
3328 expected by the rest of the code that processes
3329 overlay strings. */
3330 || (it->current.overlay_string_index < 0
3331 ? !get_overlay_strings_1 (it, 0, 0)
3332 : 0))
3334 if (it->ellipsis_p)
3335 setup_for_ellipsis (it, 0);
3336 /* When handling a display spec, we might load an
3337 empty string. In that case, discard it here. We
3338 used to discard it in handle_single_display_spec,
3339 but that causes get_overlay_strings_1, above, to
3340 ignore overlay strings that we must check. */
3341 if (STRINGP (it->string) && !SCHARS (it->string))
3342 pop_it (it);
3343 return;
3345 else if (STRINGP (it->string) && !SCHARS (it->string))
3346 pop_it (it);
3347 else
3349 it->ignore_overlay_strings_at_pos_p = 1;
3350 it->string_from_display_prop_p = 0;
3351 it->from_disp_prop_p = 0;
3352 handle_overlay_change_p = 0;
3354 handled = HANDLED_RECOMPUTE_PROPS;
3355 break;
3357 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3358 handle_overlay_change_p = 0;
3361 if (handled != HANDLED_RECOMPUTE_PROPS)
3363 /* Don't check for overlay strings below when set to deliver
3364 characters from a display vector. */
3365 if (it->method == GET_FROM_DISPLAY_VECTOR)
3366 handle_overlay_change_p = 0;
3368 /* Handle overlay changes.
3369 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3370 if it finds overlays. */
3371 if (handle_overlay_change_p)
3372 handled = handle_overlay_change (it);
3375 if (it->ellipsis_p)
3377 setup_for_ellipsis (it, 0);
3378 break;
3381 while (handled == HANDLED_RECOMPUTE_PROPS);
3383 /* Determine where to stop next. */
3384 if (handled == HANDLED_NORMALLY)
3385 compute_stop_pos (it);
3389 /* Compute IT->stop_charpos from text property and overlay change
3390 information for IT's current position. */
3392 static void
3393 compute_stop_pos (struct it *it)
3395 register INTERVAL iv, next_iv;
3396 Lisp_Object object, limit, position;
3397 ptrdiff_t charpos, bytepos;
3399 if (STRINGP (it->string))
3401 /* Strings are usually short, so don't limit the search for
3402 properties. */
3403 it->stop_charpos = it->end_charpos;
3404 object = it->string;
3405 limit = Qnil;
3406 charpos = IT_STRING_CHARPOS (*it);
3407 bytepos = IT_STRING_BYTEPOS (*it);
3409 else
3411 ptrdiff_t pos;
3413 /* If end_charpos is out of range for some reason, such as a
3414 misbehaving display function, rationalize it (Bug#5984). */
3415 if (it->end_charpos > ZV)
3416 it->end_charpos = ZV;
3417 it->stop_charpos = it->end_charpos;
3419 /* If next overlay change is in front of the current stop pos
3420 (which is IT->end_charpos), stop there. Note: value of
3421 next_overlay_change is point-max if no overlay change
3422 follows. */
3423 charpos = IT_CHARPOS (*it);
3424 bytepos = IT_BYTEPOS (*it);
3425 pos = next_overlay_change (charpos);
3426 if (pos < it->stop_charpos)
3427 it->stop_charpos = pos;
3429 /* If showing the region, we have to stop at the region
3430 start or end because the face might change there. */
3431 if (it->region_beg_charpos > 0)
3433 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3434 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3435 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3436 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3439 /* Set up variables for computing the stop position from text
3440 property changes. */
3441 XSETBUFFER (object, current_buffer);
3442 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3445 /* Get the interval containing IT's position. Value is a null
3446 interval if there isn't such an interval. */
3447 position = make_number (charpos);
3448 iv = validate_interval_range (object, &position, &position, 0);
3449 if (iv)
3451 Lisp_Object values_here[LAST_PROP_IDX];
3452 struct props *p;
3454 /* Get properties here. */
3455 for (p = it_props; p->handler; ++p)
3456 values_here[p->idx] = textget (iv->plist, *p->name);
3458 /* Look for an interval following iv that has different
3459 properties. */
3460 for (next_iv = next_interval (iv);
3461 (next_iv
3462 && (NILP (limit)
3463 || XFASTINT (limit) > next_iv->position));
3464 next_iv = next_interval (next_iv))
3466 for (p = it_props; p->handler; ++p)
3468 Lisp_Object new_value;
3470 new_value = textget (next_iv->plist, *p->name);
3471 if (!EQ (values_here[p->idx], new_value))
3472 break;
3475 if (p->handler)
3476 break;
3479 if (next_iv)
3481 if (INTEGERP (limit)
3482 && next_iv->position >= XFASTINT (limit))
3483 /* No text property change up to limit. */
3484 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3485 else
3486 /* Text properties change in next_iv. */
3487 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3491 if (it->cmp_it.id < 0)
3493 ptrdiff_t stoppos = it->end_charpos;
3495 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3496 stoppos = -1;
3497 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3498 stoppos, it->string);
3501 eassert (STRINGP (it->string)
3502 || (it->stop_charpos >= BEGV
3503 && it->stop_charpos >= IT_CHARPOS (*it)));
3507 /* Return the position of the next overlay change after POS in
3508 current_buffer. Value is point-max if no overlay change
3509 follows. This is like `next-overlay-change' but doesn't use
3510 xmalloc. */
3512 static ptrdiff_t
3513 next_overlay_change (ptrdiff_t pos)
3515 ptrdiff_t i, noverlays;
3516 ptrdiff_t endpos;
3517 Lisp_Object *overlays;
3519 /* Get all overlays at the given position. */
3520 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3522 /* If any of these overlays ends before endpos,
3523 use its ending point instead. */
3524 for (i = 0; i < noverlays; ++i)
3526 Lisp_Object oend;
3527 ptrdiff_t oendpos;
3529 oend = OVERLAY_END (overlays[i]);
3530 oendpos = OVERLAY_POSITION (oend);
3531 endpos = min (endpos, oendpos);
3534 return endpos;
3537 /* How many characters forward to search for a display property or
3538 display string. Searching too far forward makes the bidi display
3539 sluggish, especially in small windows. */
3540 #define MAX_DISP_SCAN 250
3542 /* Return the character position of a display string at or after
3543 position specified by POSITION. If no display string exists at or
3544 after POSITION, return ZV. A display string is either an overlay
3545 with `display' property whose value is a string, or a `display'
3546 text property whose value is a string. STRING is data about the
3547 string to iterate; if STRING->lstring is nil, we are iterating a
3548 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3549 on a GUI frame. DISP_PROP is set to zero if we searched
3550 MAX_DISP_SCAN characters forward without finding any display
3551 strings, non-zero otherwise. It is set to 2 if the display string
3552 uses any kind of `(space ...)' spec that will produce a stretch of
3553 white space in the text area. */
3554 ptrdiff_t
3555 compute_display_string_pos (struct text_pos *position,
3556 struct bidi_string_data *string,
3557 struct window *w,
3558 int frame_window_p, int *disp_prop)
3560 /* OBJECT = nil means current buffer. */
3561 Lisp_Object object, object1;
3562 Lisp_Object pos, spec, limpos;
3563 int string_p = (string && (STRINGP (string->lstring) || string->s));
3564 ptrdiff_t eob = string_p ? string->schars : ZV;
3565 ptrdiff_t begb = string_p ? 0 : BEGV;
3566 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3567 ptrdiff_t lim =
3568 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3569 struct text_pos tpos;
3570 int rv = 0;
3572 if (string && STRINGP (string->lstring))
3573 object1 = object = string->lstring;
3574 else if (w && !string_p)
3576 XSETWINDOW (object, w);
3577 object1 = Qnil;
3579 else
3580 object1 = object = Qnil;
3582 *disp_prop = 1;
3584 if (charpos >= eob
3585 /* We don't support display properties whose values are strings
3586 that have display string properties. */
3587 || string->from_disp_str
3588 /* C strings cannot have display properties. */
3589 || (string->s && !STRINGP (object)))
3591 *disp_prop = 0;
3592 return eob;
3595 /* If the character at CHARPOS is where the display string begins,
3596 return CHARPOS. */
3597 pos = make_number (charpos);
3598 if (STRINGP (object))
3599 bufpos = string->bufpos;
3600 else
3601 bufpos = charpos;
3602 tpos = *position;
3603 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3604 && (charpos <= begb
3605 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3606 object),
3607 spec))
3608 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3609 frame_window_p)))
3611 if (rv == 2)
3612 *disp_prop = 2;
3613 return charpos;
3616 /* Look forward for the first character with a `display' property
3617 that will replace the underlying text when displayed. */
3618 limpos = make_number (lim);
3619 do {
3620 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3621 CHARPOS (tpos) = XFASTINT (pos);
3622 if (CHARPOS (tpos) >= lim)
3624 *disp_prop = 0;
3625 break;
3627 if (STRINGP (object))
3628 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3629 else
3630 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3631 spec = Fget_char_property (pos, Qdisplay, object);
3632 if (!STRINGP (object))
3633 bufpos = CHARPOS (tpos);
3634 } while (NILP (spec)
3635 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3636 bufpos, frame_window_p)));
3637 if (rv == 2)
3638 *disp_prop = 2;
3640 return CHARPOS (tpos);
3643 /* Return the character position of the end of the display string that
3644 started at CHARPOS. If there's no display string at CHARPOS,
3645 return -1. A display string is either an overlay with `display'
3646 property whose value is a string or a `display' text property whose
3647 value is a string. */
3648 ptrdiff_t
3649 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3651 /* OBJECT = nil means current buffer. */
3652 Lisp_Object object =
3653 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3654 Lisp_Object pos = make_number (charpos);
3655 ptrdiff_t eob =
3656 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3658 if (charpos >= eob || (string->s && !STRINGP (object)))
3659 return eob;
3661 /* It could happen that the display property or overlay was removed
3662 since we found it in compute_display_string_pos above. One way
3663 this can happen is if JIT font-lock was called (through
3664 handle_fontified_prop), and jit-lock-functions remove text
3665 properties or overlays from the portion of buffer that includes
3666 CHARPOS. Muse mode is known to do that, for example. In this
3667 case, we return -1 to the caller, to signal that no display
3668 string is actually present at CHARPOS. See bidi_fetch_char for
3669 how this is handled.
3671 An alternative would be to never look for display properties past
3672 it->stop_charpos. But neither compute_display_string_pos nor
3673 bidi_fetch_char that calls it know or care where the next
3674 stop_charpos is. */
3675 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3676 return -1;
3678 /* Look forward for the first character where the `display' property
3679 changes. */
3680 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3682 return XFASTINT (pos);
3687 /***********************************************************************
3688 Fontification
3689 ***********************************************************************/
3691 /* Handle changes in the `fontified' property of the current buffer by
3692 calling hook functions from Qfontification_functions to fontify
3693 regions of text. */
3695 static enum prop_handled
3696 handle_fontified_prop (struct it *it)
3698 Lisp_Object prop, pos;
3699 enum prop_handled handled = HANDLED_NORMALLY;
3701 if (!NILP (Vmemory_full))
3702 return handled;
3704 /* Get the value of the `fontified' property at IT's current buffer
3705 position. (The `fontified' property doesn't have a special
3706 meaning in strings.) If the value is nil, call functions from
3707 Qfontification_functions. */
3708 if (!STRINGP (it->string)
3709 && it->s == NULL
3710 && !NILP (Vfontification_functions)
3711 && !NILP (Vrun_hooks)
3712 && (pos = make_number (IT_CHARPOS (*it)),
3713 prop = Fget_char_property (pos, Qfontified, Qnil),
3714 /* Ignore the special cased nil value always present at EOB since
3715 no amount of fontifying will be able to change it. */
3716 NILP (prop) && IT_CHARPOS (*it) < Z))
3718 ptrdiff_t count = SPECPDL_INDEX ();
3719 Lisp_Object val;
3720 struct buffer *obuf = current_buffer;
3721 int begv = BEGV, zv = ZV;
3722 int old_clip_changed = current_buffer->clip_changed;
3724 val = Vfontification_functions;
3725 specbind (Qfontification_functions, Qnil);
3727 eassert (it->end_charpos == ZV);
3729 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3730 safe_call1 (val, pos);
3731 else
3733 Lisp_Object fns, fn;
3734 struct gcpro gcpro1, gcpro2;
3736 fns = Qnil;
3737 GCPRO2 (val, fns);
3739 for (; CONSP (val); val = XCDR (val))
3741 fn = XCAR (val);
3743 if (EQ (fn, Qt))
3745 /* A value of t indicates this hook has a local
3746 binding; it means to run the global binding too.
3747 In a global value, t should not occur. If it
3748 does, we must ignore it to avoid an endless
3749 loop. */
3750 for (fns = Fdefault_value (Qfontification_functions);
3751 CONSP (fns);
3752 fns = XCDR (fns))
3754 fn = XCAR (fns);
3755 if (!EQ (fn, Qt))
3756 safe_call1 (fn, pos);
3759 else
3760 safe_call1 (fn, pos);
3763 UNGCPRO;
3766 unbind_to (count, Qnil);
3768 /* Fontification functions routinely call `save-restriction'.
3769 Normally, this tags clip_changed, which can confuse redisplay
3770 (see discussion in Bug#6671). Since we don't perform any
3771 special handling of fontification changes in the case where
3772 `save-restriction' isn't called, there's no point doing so in
3773 this case either. So, if the buffer's restrictions are
3774 actually left unchanged, reset clip_changed. */
3775 if (obuf == current_buffer)
3777 if (begv == BEGV && zv == ZV)
3778 current_buffer->clip_changed = old_clip_changed;
3780 /* There isn't much we can reasonably do to protect against
3781 misbehaving fontification, but here's a fig leaf. */
3782 else if (BUFFER_LIVE_P (obuf))
3783 set_buffer_internal_1 (obuf);
3785 /* The fontification code may have added/removed text.
3786 It could do even a lot worse, but let's at least protect against
3787 the most obvious case where only the text past `pos' gets changed',
3788 as is/was done in grep.el where some escapes sequences are turned
3789 into face properties (bug#7876). */
3790 it->end_charpos = ZV;
3792 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3793 something. This avoids an endless loop if they failed to
3794 fontify the text for which reason ever. */
3795 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3796 handled = HANDLED_RECOMPUTE_PROPS;
3799 return handled;
3804 /***********************************************************************
3805 Faces
3806 ***********************************************************************/
3808 /* Set up iterator IT from face properties at its current position.
3809 Called from handle_stop. */
3811 static enum prop_handled
3812 handle_face_prop (struct it *it)
3814 int new_face_id;
3815 ptrdiff_t next_stop;
3817 if (!STRINGP (it->string))
3819 new_face_id
3820 = face_at_buffer_position (it->w,
3821 IT_CHARPOS (*it),
3822 it->region_beg_charpos,
3823 it->region_end_charpos,
3824 &next_stop,
3825 (IT_CHARPOS (*it)
3826 + TEXT_PROP_DISTANCE_LIMIT),
3827 0, it->base_face_id);
3829 /* Is this a start of a run of characters with box face?
3830 Caveat: this can be called for a freshly initialized
3831 iterator; face_id is -1 in this case. We know that the new
3832 face will not change until limit, i.e. if the new face has a
3833 box, all characters up to limit will have one. But, as
3834 usual, we don't know whether limit is really the end. */
3835 if (new_face_id != it->face_id)
3837 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3838 /* If it->face_id is -1, old_face below will be NULL, see
3839 the definition of FACE_FROM_ID. This will happen if this
3840 is the initial call that gets the face. */
3841 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3843 /* If the value of face_id of the iterator is -1, we have to
3844 look in front of IT's position and see whether there is a
3845 face there that's different from new_face_id. */
3846 if (!old_face && IT_CHARPOS (*it) > BEG)
3848 int prev_face_id = face_before_it_pos (it);
3850 old_face = FACE_FROM_ID (it->f, prev_face_id);
3853 /* If the new face has a box, but the old face does not,
3854 this is the start of a run of characters with box face,
3855 i.e. this character has a shadow on the left side. */
3856 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3857 && (old_face == NULL || !old_face->box));
3858 it->face_box_p = new_face->box != FACE_NO_BOX;
3861 else
3863 int base_face_id;
3864 ptrdiff_t bufpos;
3865 int i;
3866 Lisp_Object from_overlay
3867 = (it->current.overlay_string_index >= 0
3868 ? it->string_overlays[it->current.overlay_string_index
3869 % OVERLAY_STRING_CHUNK_SIZE]
3870 : Qnil);
3872 /* See if we got to this string directly or indirectly from
3873 an overlay property. That includes the before-string or
3874 after-string of an overlay, strings in display properties
3875 provided by an overlay, their text properties, etc.
3877 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3878 if (! NILP (from_overlay))
3879 for (i = it->sp - 1; i >= 0; i--)
3881 if (it->stack[i].current.overlay_string_index >= 0)
3882 from_overlay
3883 = it->string_overlays[it->stack[i].current.overlay_string_index
3884 % OVERLAY_STRING_CHUNK_SIZE];
3885 else if (! NILP (it->stack[i].from_overlay))
3886 from_overlay = it->stack[i].from_overlay;
3888 if (!NILP (from_overlay))
3889 break;
3892 if (! NILP (from_overlay))
3894 bufpos = IT_CHARPOS (*it);
3895 /* For a string from an overlay, the base face depends
3896 only on text properties and ignores overlays. */
3897 base_face_id
3898 = face_for_overlay_string (it->w,
3899 IT_CHARPOS (*it),
3900 it->region_beg_charpos,
3901 it->region_end_charpos,
3902 &next_stop,
3903 (IT_CHARPOS (*it)
3904 + TEXT_PROP_DISTANCE_LIMIT),
3906 from_overlay);
3908 else
3910 bufpos = 0;
3912 /* For strings from a `display' property, use the face at
3913 IT's current buffer position as the base face to merge
3914 with, so that overlay strings appear in the same face as
3915 surrounding text, unless they specify their own
3916 faces. */
3917 base_face_id = it->string_from_prefix_prop_p
3918 ? DEFAULT_FACE_ID
3919 : underlying_face_id (it);
3922 new_face_id = face_at_string_position (it->w,
3923 it->string,
3924 IT_STRING_CHARPOS (*it),
3925 bufpos,
3926 it->region_beg_charpos,
3927 it->region_end_charpos,
3928 &next_stop,
3929 base_face_id, 0);
3931 /* Is this a start of a run of characters with box? Caveat:
3932 this can be called for a freshly allocated iterator; face_id
3933 is -1 is this case. We know that the new face will not
3934 change until the next check pos, i.e. if the new face has a
3935 box, all characters up to that position will have a
3936 box. But, as usual, we don't know whether that position
3937 is really the end. */
3938 if (new_face_id != it->face_id)
3940 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3941 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3943 /* If new face has a box but old face hasn't, this is the
3944 start of a run of characters with box, i.e. it has a
3945 shadow on the left side. */
3946 it->start_of_box_run_p
3947 = new_face->box && (old_face == NULL || !old_face->box);
3948 it->face_box_p = new_face->box != FACE_NO_BOX;
3952 it->face_id = new_face_id;
3953 return HANDLED_NORMALLY;
3957 /* Return the ID of the face ``underlying'' IT's current position,
3958 which is in a string. If the iterator is associated with a
3959 buffer, return the face at IT's current buffer position.
3960 Otherwise, use the iterator's base_face_id. */
3962 static int
3963 underlying_face_id (struct it *it)
3965 int face_id = it->base_face_id, i;
3967 eassert (STRINGP (it->string));
3969 for (i = it->sp - 1; i >= 0; --i)
3970 if (NILP (it->stack[i].string))
3971 face_id = it->stack[i].face_id;
3973 return face_id;
3977 /* Compute the face one character before or after the current position
3978 of IT, in the visual order. BEFORE_P non-zero means get the face
3979 in front (to the left in L2R paragraphs, to the right in R2L
3980 paragraphs) of IT's screen position. Value is the ID of the face. */
3982 static int
3983 face_before_or_after_it_pos (struct it *it, int before_p)
3985 int face_id, limit;
3986 ptrdiff_t next_check_charpos;
3987 struct it it_copy;
3988 void *it_copy_data = NULL;
3990 eassert (it->s == NULL);
3992 if (STRINGP (it->string))
3994 ptrdiff_t bufpos, charpos;
3995 int base_face_id;
3997 /* No face change past the end of the string (for the case
3998 we are padding with spaces). No face change before the
3999 string start. */
4000 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4001 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4002 return it->face_id;
4004 if (!it->bidi_p)
4006 /* Set charpos to the position before or after IT's current
4007 position, in the logical order, which in the non-bidi
4008 case is the same as the visual order. */
4009 if (before_p)
4010 charpos = IT_STRING_CHARPOS (*it) - 1;
4011 else if (it->what == IT_COMPOSITION)
4012 /* For composition, we must check the character after the
4013 composition. */
4014 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4015 else
4016 charpos = IT_STRING_CHARPOS (*it) + 1;
4018 else
4020 if (before_p)
4022 /* With bidi iteration, the character before the current
4023 in the visual order cannot be found by simple
4024 iteration, because "reverse" reordering is not
4025 supported. Instead, we need to use the move_it_*
4026 family of functions. */
4027 /* Ignore face changes before the first visible
4028 character on this display line. */
4029 if (it->current_x <= it->first_visible_x)
4030 return it->face_id;
4031 SAVE_IT (it_copy, *it, it_copy_data);
4032 /* Implementation note: Since move_it_in_display_line
4033 works in the iterator geometry, and thinks the first
4034 character is always the leftmost, even in R2L lines,
4035 we don't need to distinguish between the R2L and L2R
4036 cases here. */
4037 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4038 it_copy.current_x - 1, MOVE_TO_X);
4039 charpos = IT_STRING_CHARPOS (it_copy);
4040 RESTORE_IT (it, it, it_copy_data);
4042 else
4044 /* Set charpos to the string position of the character
4045 that comes after IT's current position in the visual
4046 order. */
4047 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4049 it_copy = *it;
4050 while (n--)
4051 bidi_move_to_visually_next (&it_copy.bidi_it);
4053 charpos = it_copy.bidi_it.charpos;
4056 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4058 if (it->current.overlay_string_index >= 0)
4059 bufpos = IT_CHARPOS (*it);
4060 else
4061 bufpos = 0;
4063 base_face_id = underlying_face_id (it);
4065 /* Get the face for ASCII, or unibyte. */
4066 face_id = face_at_string_position (it->w,
4067 it->string,
4068 charpos,
4069 bufpos,
4070 it->region_beg_charpos,
4071 it->region_end_charpos,
4072 &next_check_charpos,
4073 base_face_id, 0);
4075 /* Correct the face for charsets different from ASCII. Do it
4076 for the multibyte case only. The face returned above is
4077 suitable for unibyte text if IT->string is unibyte. */
4078 if (STRING_MULTIBYTE (it->string))
4080 struct text_pos pos1 = string_pos (charpos, it->string);
4081 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4082 int c, len;
4083 struct face *face = FACE_FROM_ID (it->f, face_id);
4085 c = string_char_and_length (p, &len);
4086 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4089 else
4091 struct text_pos pos;
4093 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4094 || (IT_CHARPOS (*it) <= BEGV && before_p))
4095 return it->face_id;
4097 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4098 pos = it->current.pos;
4100 if (!it->bidi_p)
4102 if (before_p)
4103 DEC_TEXT_POS (pos, it->multibyte_p);
4104 else
4106 if (it->what == IT_COMPOSITION)
4108 /* For composition, we must check the position after
4109 the composition. */
4110 pos.charpos += it->cmp_it.nchars;
4111 pos.bytepos += it->len;
4113 else
4114 INC_TEXT_POS (pos, it->multibyte_p);
4117 else
4119 if (before_p)
4121 /* With bidi iteration, the character before the current
4122 in the visual order cannot be found by simple
4123 iteration, because "reverse" reordering is not
4124 supported. Instead, we need to use the move_it_*
4125 family of functions. */
4126 /* Ignore face changes before the first visible
4127 character on this display line. */
4128 if (it->current_x <= it->first_visible_x)
4129 return it->face_id;
4130 SAVE_IT (it_copy, *it, it_copy_data);
4131 /* Implementation note: Since move_it_in_display_line
4132 works in the iterator geometry, and thinks the first
4133 character is always the leftmost, even in R2L lines,
4134 we don't need to distinguish between the R2L and L2R
4135 cases here. */
4136 move_it_in_display_line (&it_copy, ZV,
4137 it_copy.current_x - 1, MOVE_TO_X);
4138 pos = it_copy.current.pos;
4139 RESTORE_IT (it, it, it_copy_data);
4141 else
4143 /* Set charpos to the buffer position of the character
4144 that comes after IT's current position in the visual
4145 order. */
4146 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4148 it_copy = *it;
4149 while (n--)
4150 bidi_move_to_visually_next (&it_copy.bidi_it);
4152 SET_TEXT_POS (pos,
4153 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4156 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4158 /* Determine face for CHARSET_ASCII, or unibyte. */
4159 face_id = face_at_buffer_position (it->w,
4160 CHARPOS (pos),
4161 it->region_beg_charpos,
4162 it->region_end_charpos,
4163 &next_check_charpos,
4164 limit, 0, -1);
4166 /* Correct the face for charsets different from ASCII. Do it
4167 for the multibyte case only. The face returned above is
4168 suitable for unibyte text if current_buffer is unibyte. */
4169 if (it->multibyte_p)
4171 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4172 struct face *face = FACE_FROM_ID (it->f, face_id);
4173 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4177 return face_id;
4182 /***********************************************************************
4183 Invisible text
4184 ***********************************************************************/
4186 /* Set up iterator IT from invisible properties at its current
4187 position. Called from handle_stop. */
4189 static enum prop_handled
4190 handle_invisible_prop (struct it *it)
4192 enum prop_handled handled = HANDLED_NORMALLY;
4193 int invis_p;
4194 Lisp_Object prop;
4196 if (STRINGP (it->string))
4198 Lisp_Object end_charpos, limit, charpos;
4200 /* Get the value of the invisible text property at the
4201 current position. Value will be nil if there is no such
4202 property. */
4203 charpos = make_number (IT_STRING_CHARPOS (*it));
4204 prop = Fget_text_property (charpos, Qinvisible, it->string);
4205 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4207 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4209 /* Record whether we have to display an ellipsis for the
4210 invisible text. */
4211 int display_ellipsis_p = (invis_p == 2);
4212 ptrdiff_t len, endpos;
4214 handled = HANDLED_RECOMPUTE_PROPS;
4216 /* Get the position at which the next visible text can be
4217 found in IT->string, if any. */
4218 endpos = len = SCHARS (it->string);
4219 XSETINT (limit, len);
4222 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4223 it->string, limit);
4224 if (INTEGERP (end_charpos))
4226 endpos = XFASTINT (end_charpos);
4227 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4228 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4229 if (invis_p == 2)
4230 display_ellipsis_p = 1;
4233 while (invis_p && endpos < len);
4235 if (display_ellipsis_p)
4236 it->ellipsis_p = 1;
4238 if (endpos < len)
4240 /* Text at END_CHARPOS is visible. Move IT there. */
4241 struct text_pos old;
4242 ptrdiff_t oldpos;
4244 old = it->current.string_pos;
4245 oldpos = CHARPOS (old);
4246 if (it->bidi_p)
4248 if (it->bidi_it.first_elt
4249 && it->bidi_it.charpos < SCHARS (it->string))
4250 bidi_paragraph_init (it->paragraph_embedding,
4251 &it->bidi_it, 1);
4252 /* Bidi-iterate out of the invisible text. */
4255 bidi_move_to_visually_next (&it->bidi_it);
4257 while (oldpos <= it->bidi_it.charpos
4258 && it->bidi_it.charpos < endpos);
4260 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4261 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4262 if (IT_CHARPOS (*it) >= endpos)
4263 it->prev_stop = endpos;
4265 else
4267 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4268 compute_string_pos (&it->current.string_pos, old, it->string);
4271 else
4273 /* The rest of the string is invisible. If this is an
4274 overlay string, proceed with the next overlay string
4275 or whatever comes and return a character from there. */
4276 if (it->current.overlay_string_index >= 0
4277 && !display_ellipsis_p)
4279 next_overlay_string (it);
4280 /* Don't check for overlay strings when we just
4281 finished processing them. */
4282 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4284 else
4286 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4287 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4292 else
4294 ptrdiff_t newpos, next_stop, start_charpos, tem;
4295 Lisp_Object pos, overlay;
4297 /* First of all, is there invisible text at this position? */
4298 tem = start_charpos = IT_CHARPOS (*it);
4299 pos = make_number (tem);
4300 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4301 &overlay);
4302 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4304 /* If we are on invisible text, skip over it. */
4305 if (invis_p && start_charpos < it->end_charpos)
4307 /* Record whether we have to display an ellipsis for the
4308 invisible text. */
4309 int display_ellipsis_p = invis_p == 2;
4311 handled = HANDLED_RECOMPUTE_PROPS;
4313 /* Loop skipping over invisible text. The loop is left at
4314 ZV or with IT on the first char being visible again. */
4317 /* Try to skip some invisible text. Return value is the
4318 position reached which can be equal to where we start
4319 if there is nothing invisible there. This skips both
4320 over invisible text properties and overlays with
4321 invisible property. */
4322 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4324 /* If we skipped nothing at all we weren't at invisible
4325 text in the first place. If everything to the end of
4326 the buffer was skipped, end the loop. */
4327 if (newpos == tem || newpos >= ZV)
4328 invis_p = 0;
4329 else
4331 /* We skipped some characters but not necessarily
4332 all there are. Check if we ended up on visible
4333 text. Fget_char_property returns the property of
4334 the char before the given position, i.e. if we
4335 get invis_p = 0, this means that the char at
4336 newpos is visible. */
4337 pos = make_number (newpos);
4338 prop = Fget_char_property (pos, Qinvisible, it->window);
4339 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4342 /* If we ended up on invisible text, proceed to
4343 skip starting with next_stop. */
4344 if (invis_p)
4345 tem = next_stop;
4347 /* If there are adjacent invisible texts, don't lose the
4348 second one's ellipsis. */
4349 if (invis_p == 2)
4350 display_ellipsis_p = 1;
4352 while (invis_p);
4354 /* The position newpos is now either ZV or on visible text. */
4355 if (it->bidi_p)
4357 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4358 int on_newline =
4359 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4360 int after_newline =
4361 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4363 /* If the invisible text ends on a newline or on a
4364 character after a newline, we can avoid the costly,
4365 character by character, bidi iteration to NEWPOS, and
4366 instead simply reseat the iterator there. That's
4367 because all bidi reordering information is tossed at
4368 the newline. This is a big win for modes that hide
4369 complete lines, like Outline, Org, etc. */
4370 if (on_newline || after_newline)
4372 struct text_pos tpos;
4373 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4375 SET_TEXT_POS (tpos, newpos, bpos);
4376 reseat_1 (it, tpos, 0);
4377 /* If we reseat on a newline/ZV, we need to prep the
4378 bidi iterator for advancing to the next character
4379 after the newline/EOB, keeping the current paragraph
4380 direction (so that PRODUCE_GLYPHS does TRT wrt
4381 prepending/appending glyphs to a glyph row). */
4382 if (on_newline)
4384 it->bidi_it.first_elt = 0;
4385 it->bidi_it.paragraph_dir = pdir;
4386 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4387 it->bidi_it.nchars = 1;
4388 it->bidi_it.ch_len = 1;
4391 else /* Must use the slow method. */
4393 /* With bidi iteration, the region of invisible text
4394 could start and/or end in the middle of a
4395 non-base embedding level. Therefore, we need to
4396 skip invisible text using the bidi iterator,
4397 starting at IT's current position, until we find
4398 ourselves outside of the invisible text.
4399 Skipping invisible text _after_ bidi iteration
4400 avoids affecting the visual order of the
4401 displayed text when invisible properties are
4402 added or removed. */
4403 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4405 /* If we were `reseat'ed to a new paragraph,
4406 determine the paragraph base direction. We
4407 need to do it now because
4408 next_element_from_buffer may not have a
4409 chance to do it, if we are going to skip any
4410 text at the beginning, which resets the
4411 FIRST_ELT flag. */
4412 bidi_paragraph_init (it->paragraph_embedding,
4413 &it->bidi_it, 1);
4417 bidi_move_to_visually_next (&it->bidi_it);
4419 while (it->stop_charpos <= it->bidi_it.charpos
4420 && it->bidi_it.charpos < newpos);
4421 IT_CHARPOS (*it) = it->bidi_it.charpos;
4422 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4423 /* If we overstepped NEWPOS, record its position in
4424 the iterator, so that we skip invisible text if
4425 later the bidi iteration lands us in the
4426 invisible region again. */
4427 if (IT_CHARPOS (*it) >= newpos)
4428 it->prev_stop = newpos;
4431 else
4433 IT_CHARPOS (*it) = newpos;
4434 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4437 /* If there are before-strings at the start of invisible
4438 text, and the text is invisible because of a text
4439 property, arrange to show before-strings because 20.x did
4440 it that way. (If the text is invisible because of an
4441 overlay property instead of a text property, this is
4442 already handled in the overlay code.) */
4443 if (NILP (overlay)
4444 && get_overlay_strings (it, it->stop_charpos))
4446 handled = HANDLED_RECOMPUTE_PROPS;
4447 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4449 else if (display_ellipsis_p)
4451 /* Make sure that the glyphs of the ellipsis will get
4452 correct `charpos' values. If we would not update
4453 it->position here, the glyphs would belong to the
4454 last visible character _before_ the invisible
4455 text, which confuses `set_cursor_from_row'.
4457 We use the last invisible position instead of the
4458 first because this way the cursor is always drawn on
4459 the first "." of the ellipsis, whenever PT is inside
4460 the invisible text. Otherwise the cursor would be
4461 placed _after_ the ellipsis when the point is after the
4462 first invisible character. */
4463 if (!STRINGP (it->object))
4465 it->position.charpos = newpos - 1;
4466 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4468 it->ellipsis_p = 1;
4469 /* Let the ellipsis display before
4470 considering any properties of the following char.
4471 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4472 handled = HANDLED_RETURN;
4477 return handled;
4481 /* Make iterator IT return `...' next.
4482 Replaces LEN characters from buffer. */
4484 static void
4485 setup_for_ellipsis (struct it *it, int len)
4487 /* Use the display table definition for `...'. Invalid glyphs
4488 will be handled by the method returning elements from dpvec. */
4489 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4491 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4492 it->dpvec = v->contents;
4493 it->dpend = v->contents + v->header.size;
4495 else
4497 /* Default `...'. */
4498 it->dpvec = default_invis_vector;
4499 it->dpend = default_invis_vector + 3;
4502 it->dpvec_char_len = len;
4503 it->current.dpvec_index = 0;
4504 it->dpvec_face_id = -1;
4506 /* Remember the current face id in case glyphs specify faces.
4507 IT's face is restored in set_iterator_to_next.
4508 saved_face_id was set to preceding char's face in handle_stop. */
4509 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4510 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4512 it->method = GET_FROM_DISPLAY_VECTOR;
4513 it->ellipsis_p = 1;
4518 /***********************************************************************
4519 'display' property
4520 ***********************************************************************/
4522 /* Set up iterator IT from `display' property at its current position.
4523 Called from handle_stop.
4524 We return HANDLED_RETURN if some part of the display property
4525 overrides the display of the buffer text itself.
4526 Otherwise we return HANDLED_NORMALLY. */
4528 static enum prop_handled
4529 handle_display_prop (struct it *it)
4531 Lisp_Object propval, object, overlay;
4532 struct text_pos *position;
4533 ptrdiff_t bufpos;
4534 /* Nonzero if some property replaces the display of the text itself. */
4535 int display_replaced_p = 0;
4537 if (STRINGP (it->string))
4539 object = it->string;
4540 position = &it->current.string_pos;
4541 bufpos = CHARPOS (it->current.pos);
4543 else
4545 XSETWINDOW (object, it->w);
4546 position = &it->current.pos;
4547 bufpos = CHARPOS (*position);
4550 /* Reset those iterator values set from display property values. */
4551 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4552 it->space_width = Qnil;
4553 it->font_height = Qnil;
4554 it->voffset = 0;
4556 /* We don't support recursive `display' properties, i.e. string
4557 values that have a string `display' property, that have a string
4558 `display' property etc. */
4559 if (!it->string_from_display_prop_p)
4560 it->area = TEXT_AREA;
4562 propval = get_char_property_and_overlay (make_number (position->charpos),
4563 Qdisplay, object, &overlay);
4564 if (NILP (propval))
4565 return HANDLED_NORMALLY;
4566 /* Now OVERLAY is the overlay that gave us this property, or nil
4567 if it was a text property. */
4569 if (!STRINGP (it->string))
4570 object = it->w->contents;
4572 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4573 position, bufpos,
4574 FRAME_WINDOW_P (it->f));
4576 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4579 /* Subroutine of handle_display_prop. Returns non-zero if the display
4580 specification in SPEC is a replacing specification, i.e. it would
4581 replace the text covered by `display' property with something else,
4582 such as an image or a display string. If SPEC includes any kind or
4583 `(space ...) specification, the value is 2; this is used by
4584 compute_display_string_pos, which see.
4586 See handle_single_display_spec for documentation of arguments.
4587 frame_window_p is non-zero if the window being redisplayed is on a
4588 GUI frame; this argument is used only if IT is NULL, see below.
4590 IT can be NULL, if this is called by the bidi reordering code
4591 through compute_display_string_pos, which see. In that case, this
4592 function only examines SPEC, but does not otherwise "handle" it, in
4593 the sense that it doesn't set up members of IT from the display
4594 spec. */
4595 static int
4596 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4597 Lisp_Object overlay, struct text_pos *position,
4598 ptrdiff_t bufpos, int frame_window_p)
4600 int replacing_p = 0;
4601 int rv;
4603 if (CONSP (spec)
4604 /* Simple specifications. */
4605 && !EQ (XCAR (spec), Qimage)
4606 && !EQ (XCAR (spec), Qspace)
4607 && !EQ (XCAR (spec), Qwhen)
4608 && !EQ (XCAR (spec), Qslice)
4609 && !EQ (XCAR (spec), Qspace_width)
4610 && !EQ (XCAR (spec), Qheight)
4611 && !EQ (XCAR (spec), Qraise)
4612 /* Marginal area specifications. */
4613 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4614 && !EQ (XCAR (spec), Qleft_fringe)
4615 && !EQ (XCAR (spec), Qright_fringe)
4616 && !NILP (XCAR (spec)))
4618 for (; CONSP (spec); spec = XCDR (spec))
4620 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4621 overlay, position, bufpos,
4622 replacing_p, frame_window_p)))
4624 replacing_p = rv;
4625 /* If some text in a string is replaced, `position' no
4626 longer points to the position of `object'. */
4627 if (!it || STRINGP (object))
4628 break;
4632 else if (VECTORP (spec))
4634 ptrdiff_t i;
4635 for (i = 0; i < ASIZE (spec); ++i)
4636 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4637 overlay, position, bufpos,
4638 replacing_p, frame_window_p)))
4640 replacing_p = rv;
4641 /* If some text in a string is replaced, `position' no
4642 longer points to the position of `object'. */
4643 if (!it || STRINGP (object))
4644 break;
4647 else
4649 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4650 position, bufpos, 0,
4651 frame_window_p)))
4652 replacing_p = rv;
4655 return replacing_p;
4658 /* Value is the position of the end of the `display' property starting
4659 at START_POS in OBJECT. */
4661 static struct text_pos
4662 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4664 Lisp_Object end;
4665 struct text_pos end_pos;
4667 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4668 Qdisplay, object, Qnil);
4669 CHARPOS (end_pos) = XFASTINT (end);
4670 if (STRINGP (object))
4671 compute_string_pos (&end_pos, start_pos, it->string);
4672 else
4673 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4675 return end_pos;
4679 /* Set up IT from a single `display' property specification SPEC. OBJECT
4680 is the object in which the `display' property was found. *POSITION
4681 is the position in OBJECT at which the `display' property was found.
4682 BUFPOS is the buffer position of OBJECT (different from POSITION if
4683 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4684 previously saw a display specification which already replaced text
4685 display with something else, for example an image; we ignore such
4686 properties after the first one has been processed.
4688 OVERLAY is the overlay this `display' property came from,
4689 or nil if it was a text property.
4691 If SPEC is a `space' or `image' specification, and in some other
4692 cases too, set *POSITION to the position where the `display'
4693 property ends.
4695 If IT is NULL, only examine the property specification in SPEC, but
4696 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4697 is intended to be displayed in a window on a GUI frame.
4699 Value is non-zero if something was found which replaces the display
4700 of buffer or string text. */
4702 static int
4703 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4704 Lisp_Object overlay, struct text_pos *position,
4705 ptrdiff_t bufpos, int display_replaced_p,
4706 int frame_window_p)
4708 Lisp_Object form;
4709 Lisp_Object location, value;
4710 struct text_pos start_pos = *position;
4711 int valid_p;
4713 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4714 If the result is non-nil, use VALUE instead of SPEC. */
4715 form = Qt;
4716 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4718 spec = XCDR (spec);
4719 if (!CONSP (spec))
4720 return 0;
4721 form = XCAR (spec);
4722 spec = XCDR (spec);
4725 if (!NILP (form) && !EQ (form, Qt))
4727 ptrdiff_t count = SPECPDL_INDEX ();
4728 struct gcpro gcpro1;
4730 /* Bind `object' to the object having the `display' property, a
4731 buffer or string. Bind `position' to the position in the
4732 object where the property was found, and `buffer-position'
4733 to the current position in the buffer. */
4735 if (NILP (object))
4736 XSETBUFFER (object, current_buffer);
4737 specbind (Qobject, object);
4738 specbind (Qposition, make_number (CHARPOS (*position)));
4739 specbind (Qbuffer_position, make_number (bufpos));
4740 GCPRO1 (form);
4741 form = safe_eval (form);
4742 UNGCPRO;
4743 unbind_to (count, Qnil);
4746 if (NILP (form))
4747 return 0;
4749 /* Handle `(height HEIGHT)' specifications. */
4750 if (CONSP (spec)
4751 && EQ (XCAR (spec), Qheight)
4752 && CONSP (XCDR (spec)))
4754 if (it)
4756 if (!FRAME_WINDOW_P (it->f))
4757 return 0;
4759 it->font_height = XCAR (XCDR (spec));
4760 if (!NILP (it->font_height))
4762 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4763 int new_height = -1;
4765 if (CONSP (it->font_height)
4766 && (EQ (XCAR (it->font_height), Qplus)
4767 || EQ (XCAR (it->font_height), Qminus))
4768 && CONSP (XCDR (it->font_height))
4769 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4771 /* `(+ N)' or `(- N)' where N is an integer. */
4772 int steps = XINT (XCAR (XCDR (it->font_height)));
4773 if (EQ (XCAR (it->font_height), Qplus))
4774 steps = - steps;
4775 it->face_id = smaller_face (it->f, it->face_id, steps);
4777 else if (FUNCTIONP (it->font_height))
4779 /* Call function with current height as argument.
4780 Value is the new height. */
4781 Lisp_Object height;
4782 height = safe_call1 (it->font_height,
4783 face->lface[LFACE_HEIGHT_INDEX]);
4784 if (NUMBERP (height))
4785 new_height = XFLOATINT (height);
4787 else if (NUMBERP (it->font_height))
4789 /* Value is a multiple of the canonical char height. */
4790 struct face *f;
4792 f = FACE_FROM_ID (it->f,
4793 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4794 new_height = (XFLOATINT (it->font_height)
4795 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4797 else
4799 /* Evaluate IT->font_height with `height' bound to the
4800 current specified height to get the new height. */
4801 ptrdiff_t count = SPECPDL_INDEX ();
4803 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4804 value = safe_eval (it->font_height);
4805 unbind_to (count, Qnil);
4807 if (NUMBERP (value))
4808 new_height = XFLOATINT (value);
4811 if (new_height > 0)
4812 it->face_id = face_with_height (it->f, it->face_id, new_height);
4816 return 0;
4819 /* Handle `(space-width WIDTH)'. */
4820 if (CONSP (spec)
4821 && EQ (XCAR (spec), Qspace_width)
4822 && CONSP (XCDR (spec)))
4824 if (it)
4826 if (!FRAME_WINDOW_P (it->f))
4827 return 0;
4829 value = XCAR (XCDR (spec));
4830 if (NUMBERP (value) && XFLOATINT (value) > 0)
4831 it->space_width = value;
4834 return 0;
4837 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4838 if (CONSP (spec)
4839 && EQ (XCAR (spec), Qslice))
4841 Lisp_Object tem;
4843 if (it)
4845 if (!FRAME_WINDOW_P (it->f))
4846 return 0;
4848 if (tem = XCDR (spec), CONSP (tem))
4850 it->slice.x = XCAR (tem);
4851 if (tem = XCDR (tem), CONSP (tem))
4853 it->slice.y = XCAR (tem);
4854 if (tem = XCDR (tem), CONSP (tem))
4856 it->slice.width = XCAR (tem);
4857 if (tem = XCDR (tem), CONSP (tem))
4858 it->slice.height = XCAR (tem);
4864 return 0;
4867 /* Handle `(raise FACTOR)'. */
4868 if (CONSP (spec)
4869 && EQ (XCAR (spec), Qraise)
4870 && CONSP (XCDR (spec)))
4872 if (it)
4874 if (!FRAME_WINDOW_P (it->f))
4875 return 0;
4877 #ifdef HAVE_WINDOW_SYSTEM
4878 value = XCAR (XCDR (spec));
4879 if (NUMBERP (value))
4881 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4882 it->voffset = - (XFLOATINT (value)
4883 * (FONT_HEIGHT (face->font)));
4885 #endif /* HAVE_WINDOW_SYSTEM */
4888 return 0;
4891 /* Don't handle the other kinds of display specifications
4892 inside a string that we got from a `display' property. */
4893 if (it && it->string_from_display_prop_p)
4894 return 0;
4896 /* Characters having this form of property are not displayed, so
4897 we have to find the end of the property. */
4898 if (it)
4900 start_pos = *position;
4901 *position = display_prop_end (it, object, start_pos);
4903 value = Qnil;
4905 /* Stop the scan at that end position--we assume that all
4906 text properties change there. */
4907 if (it)
4908 it->stop_charpos = position->charpos;
4910 /* Handle `(left-fringe BITMAP [FACE])'
4911 and `(right-fringe BITMAP [FACE])'. */
4912 if (CONSP (spec)
4913 && (EQ (XCAR (spec), Qleft_fringe)
4914 || EQ (XCAR (spec), Qright_fringe))
4915 && CONSP (XCDR (spec)))
4917 int fringe_bitmap;
4919 if (it)
4921 if (!FRAME_WINDOW_P (it->f))
4922 /* If we return here, POSITION has been advanced
4923 across the text with this property. */
4925 /* Synchronize the bidi iterator with POSITION. This is
4926 needed because we are not going to push the iterator
4927 on behalf of this display property, so there will be
4928 no pop_it call to do this synchronization for us. */
4929 if (it->bidi_p)
4931 it->position = *position;
4932 iterate_out_of_display_property (it);
4933 *position = it->position;
4935 return 1;
4938 else if (!frame_window_p)
4939 return 1;
4941 #ifdef HAVE_WINDOW_SYSTEM
4942 value = XCAR (XCDR (spec));
4943 if (!SYMBOLP (value)
4944 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4945 /* If we return here, POSITION has been advanced
4946 across the text with this property. */
4948 if (it && it->bidi_p)
4950 it->position = *position;
4951 iterate_out_of_display_property (it);
4952 *position = it->position;
4954 return 1;
4957 if (it)
4959 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4961 if (CONSP (XCDR (XCDR (spec))))
4963 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4964 int face_id2 = lookup_derived_face (it->f, face_name,
4965 FRINGE_FACE_ID, 0);
4966 if (face_id2 >= 0)
4967 face_id = face_id2;
4970 /* Save current settings of IT so that we can restore them
4971 when we are finished with the glyph property value. */
4972 push_it (it, position);
4974 it->area = TEXT_AREA;
4975 it->what = IT_IMAGE;
4976 it->image_id = -1; /* no image */
4977 it->position = start_pos;
4978 it->object = NILP (object) ? it->w->contents : object;
4979 it->method = GET_FROM_IMAGE;
4980 it->from_overlay = Qnil;
4981 it->face_id = face_id;
4982 it->from_disp_prop_p = 1;
4984 /* Say that we haven't consumed the characters with
4985 `display' property yet. The call to pop_it in
4986 set_iterator_to_next will clean this up. */
4987 *position = start_pos;
4989 if (EQ (XCAR (spec), Qleft_fringe))
4991 it->left_user_fringe_bitmap = fringe_bitmap;
4992 it->left_user_fringe_face_id = face_id;
4994 else
4996 it->right_user_fringe_bitmap = fringe_bitmap;
4997 it->right_user_fringe_face_id = face_id;
5000 #endif /* HAVE_WINDOW_SYSTEM */
5001 return 1;
5004 /* Prepare to handle `((margin left-margin) ...)',
5005 `((margin right-margin) ...)' and `((margin nil) ...)'
5006 prefixes for display specifications. */
5007 location = Qunbound;
5008 if (CONSP (spec) && CONSP (XCAR (spec)))
5010 Lisp_Object tem;
5012 value = XCDR (spec);
5013 if (CONSP (value))
5014 value = XCAR (value);
5016 tem = XCAR (spec);
5017 if (EQ (XCAR (tem), Qmargin)
5018 && (tem = XCDR (tem),
5019 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5020 (NILP (tem)
5021 || EQ (tem, Qleft_margin)
5022 || EQ (tem, Qright_margin))))
5023 location = tem;
5026 if (EQ (location, Qunbound))
5028 location = Qnil;
5029 value = spec;
5032 /* After this point, VALUE is the property after any
5033 margin prefix has been stripped. It must be a string,
5034 an image specification, or `(space ...)'.
5036 LOCATION specifies where to display: `left-margin',
5037 `right-margin' or nil. */
5039 valid_p = (STRINGP (value)
5040 #ifdef HAVE_WINDOW_SYSTEM
5041 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5042 && valid_image_p (value))
5043 #endif /* not HAVE_WINDOW_SYSTEM */
5044 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5046 if (valid_p && !display_replaced_p)
5048 int retval = 1;
5050 if (!it)
5052 /* Callers need to know whether the display spec is any kind
5053 of `(space ...)' spec that is about to affect text-area
5054 display. */
5055 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5056 retval = 2;
5057 return retval;
5060 /* Save current settings of IT so that we can restore them
5061 when we are finished with the glyph property value. */
5062 push_it (it, position);
5063 it->from_overlay = overlay;
5064 it->from_disp_prop_p = 1;
5066 if (NILP (location))
5067 it->area = TEXT_AREA;
5068 else if (EQ (location, Qleft_margin))
5069 it->area = LEFT_MARGIN_AREA;
5070 else
5071 it->area = RIGHT_MARGIN_AREA;
5073 if (STRINGP (value))
5075 it->string = value;
5076 it->multibyte_p = STRING_MULTIBYTE (it->string);
5077 it->current.overlay_string_index = -1;
5078 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5079 it->end_charpos = it->string_nchars = SCHARS (it->string);
5080 it->method = GET_FROM_STRING;
5081 it->stop_charpos = 0;
5082 it->prev_stop = 0;
5083 it->base_level_stop = 0;
5084 it->string_from_display_prop_p = 1;
5085 /* Say that we haven't consumed the characters with
5086 `display' property yet. The call to pop_it in
5087 set_iterator_to_next will clean this up. */
5088 if (BUFFERP (object))
5089 *position = start_pos;
5091 /* Force paragraph direction to be that of the parent
5092 object. If the parent object's paragraph direction is
5093 not yet determined, default to L2R. */
5094 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5095 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5096 else
5097 it->paragraph_embedding = L2R;
5099 /* Set up the bidi iterator for this display string. */
5100 if (it->bidi_p)
5102 it->bidi_it.string.lstring = it->string;
5103 it->bidi_it.string.s = NULL;
5104 it->bidi_it.string.schars = it->end_charpos;
5105 it->bidi_it.string.bufpos = bufpos;
5106 it->bidi_it.string.from_disp_str = 1;
5107 it->bidi_it.string.unibyte = !it->multibyte_p;
5108 it->bidi_it.w = it->w;
5109 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5112 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5114 it->method = GET_FROM_STRETCH;
5115 it->object = value;
5116 *position = it->position = start_pos;
5117 retval = 1 + (it->area == TEXT_AREA);
5119 #ifdef HAVE_WINDOW_SYSTEM
5120 else
5122 it->what = IT_IMAGE;
5123 it->image_id = lookup_image (it->f, value);
5124 it->position = start_pos;
5125 it->object = NILP (object) ? it->w->contents : object;
5126 it->method = GET_FROM_IMAGE;
5128 /* Say that we haven't consumed the characters with
5129 `display' property yet. The call to pop_it in
5130 set_iterator_to_next will clean this up. */
5131 *position = start_pos;
5133 #endif /* HAVE_WINDOW_SYSTEM */
5135 return retval;
5138 /* Invalid property or property not supported. Restore
5139 POSITION to what it was before. */
5140 *position = start_pos;
5141 return 0;
5144 /* Check if PROP is a display property value whose text should be
5145 treated as intangible. OVERLAY is the overlay from which PROP
5146 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5147 specify the buffer position covered by PROP. */
5150 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5151 ptrdiff_t charpos, ptrdiff_t bytepos)
5153 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5154 struct text_pos position;
5156 SET_TEXT_POS (position, charpos, bytepos);
5157 return handle_display_spec (NULL, prop, Qnil, overlay,
5158 &position, charpos, frame_window_p);
5162 /* Return 1 if PROP is a display sub-property value containing STRING.
5164 Implementation note: this and the following function are really
5165 special cases of handle_display_spec and
5166 handle_single_display_spec, and should ideally use the same code.
5167 Until they do, these two pairs must be consistent and must be
5168 modified in sync. */
5170 static int
5171 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5173 if (EQ (string, prop))
5174 return 1;
5176 /* Skip over `when FORM'. */
5177 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5179 prop = XCDR (prop);
5180 if (!CONSP (prop))
5181 return 0;
5182 /* Actually, the condition following `when' should be eval'ed,
5183 like handle_single_display_spec does, and we should return
5184 zero if it evaluates to nil. However, this function is
5185 called only when the buffer was already displayed and some
5186 glyph in the glyph matrix was found to come from a display
5187 string. Therefore, the condition was already evaluated, and
5188 the result was non-nil, otherwise the display string wouldn't
5189 have been displayed and we would have never been called for
5190 this property. Thus, we can skip the evaluation and assume
5191 its result is non-nil. */
5192 prop = XCDR (prop);
5195 if (CONSP (prop))
5196 /* Skip over `margin LOCATION'. */
5197 if (EQ (XCAR (prop), Qmargin))
5199 prop = XCDR (prop);
5200 if (!CONSP (prop))
5201 return 0;
5203 prop = XCDR (prop);
5204 if (!CONSP (prop))
5205 return 0;
5208 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5212 /* Return 1 if STRING appears in the `display' property PROP. */
5214 static int
5215 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5217 if (CONSP (prop)
5218 && !EQ (XCAR (prop), Qwhen)
5219 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5221 /* A list of sub-properties. */
5222 while (CONSP (prop))
5224 if (single_display_spec_string_p (XCAR (prop), string))
5225 return 1;
5226 prop = XCDR (prop);
5229 else if (VECTORP (prop))
5231 /* A vector of sub-properties. */
5232 ptrdiff_t i;
5233 for (i = 0; i < ASIZE (prop); ++i)
5234 if (single_display_spec_string_p (AREF (prop, i), string))
5235 return 1;
5237 else
5238 return single_display_spec_string_p (prop, string);
5240 return 0;
5243 /* Look for STRING in overlays and text properties in the current
5244 buffer, between character positions FROM and TO (excluding TO).
5245 BACK_P non-zero means look back (in this case, TO is supposed to be
5246 less than FROM).
5247 Value is the first character position where STRING was found, or
5248 zero if it wasn't found before hitting TO.
5250 This function may only use code that doesn't eval because it is
5251 called asynchronously from note_mouse_highlight. */
5253 static ptrdiff_t
5254 string_buffer_position_lim (Lisp_Object string,
5255 ptrdiff_t from, ptrdiff_t to, int back_p)
5257 Lisp_Object limit, prop, pos;
5258 int found = 0;
5260 pos = make_number (max (from, BEGV));
5262 if (!back_p) /* looking forward */
5264 limit = make_number (min (to, ZV));
5265 while (!found && !EQ (pos, limit))
5267 prop = Fget_char_property (pos, Qdisplay, Qnil);
5268 if (!NILP (prop) && display_prop_string_p (prop, string))
5269 found = 1;
5270 else
5271 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5272 limit);
5275 else /* looking back */
5277 limit = make_number (max (to, BEGV));
5278 while (!found && !EQ (pos, limit))
5280 prop = Fget_char_property (pos, Qdisplay, Qnil);
5281 if (!NILP (prop) && display_prop_string_p (prop, string))
5282 found = 1;
5283 else
5284 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5285 limit);
5289 return found ? XINT (pos) : 0;
5292 /* Determine which buffer position in current buffer STRING comes from.
5293 AROUND_CHARPOS is an approximate position where it could come from.
5294 Value is the buffer position or 0 if it couldn't be determined.
5296 This function is necessary because we don't record buffer positions
5297 in glyphs generated from strings (to keep struct glyph small).
5298 This function may only use code that doesn't eval because it is
5299 called asynchronously from note_mouse_highlight. */
5301 static ptrdiff_t
5302 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5304 const int MAX_DISTANCE = 1000;
5305 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5306 around_charpos + MAX_DISTANCE,
5309 if (!found)
5310 found = string_buffer_position_lim (string, around_charpos,
5311 around_charpos - MAX_DISTANCE, 1);
5312 return found;
5317 /***********************************************************************
5318 `composition' property
5319 ***********************************************************************/
5321 /* Set up iterator IT from `composition' property at its current
5322 position. Called from handle_stop. */
5324 static enum prop_handled
5325 handle_composition_prop (struct it *it)
5327 Lisp_Object prop, string;
5328 ptrdiff_t pos, pos_byte, start, end;
5330 if (STRINGP (it->string))
5332 unsigned char *s;
5334 pos = IT_STRING_CHARPOS (*it);
5335 pos_byte = IT_STRING_BYTEPOS (*it);
5336 string = it->string;
5337 s = SDATA (string) + pos_byte;
5338 it->c = STRING_CHAR (s);
5340 else
5342 pos = IT_CHARPOS (*it);
5343 pos_byte = IT_BYTEPOS (*it);
5344 string = Qnil;
5345 it->c = FETCH_CHAR (pos_byte);
5348 /* If there's a valid composition and point is not inside of the
5349 composition (in the case that the composition is from the current
5350 buffer), draw a glyph composed from the composition components. */
5351 if (find_composition (pos, -1, &start, &end, &prop, string)
5352 && composition_valid_p (start, end, prop)
5353 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5355 if (start < pos)
5356 /* As we can't handle this situation (perhaps font-lock added
5357 a new composition), we just return here hoping that next
5358 redisplay will detect this composition much earlier. */
5359 return HANDLED_NORMALLY;
5360 if (start != pos)
5362 if (STRINGP (it->string))
5363 pos_byte = string_char_to_byte (it->string, start);
5364 else
5365 pos_byte = CHAR_TO_BYTE (start);
5367 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5368 prop, string);
5370 if (it->cmp_it.id >= 0)
5372 it->cmp_it.ch = -1;
5373 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5374 it->cmp_it.nglyphs = -1;
5378 return HANDLED_NORMALLY;
5383 /***********************************************************************
5384 Overlay strings
5385 ***********************************************************************/
5387 /* The following structure is used to record overlay strings for
5388 later sorting in load_overlay_strings. */
5390 struct overlay_entry
5392 Lisp_Object overlay;
5393 Lisp_Object string;
5394 EMACS_INT priority;
5395 int after_string_p;
5399 /* Set up iterator IT from overlay strings at its current position.
5400 Called from handle_stop. */
5402 static enum prop_handled
5403 handle_overlay_change (struct it *it)
5405 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5406 return HANDLED_RECOMPUTE_PROPS;
5407 else
5408 return HANDLED_NORMALLY;
5412 /* Set up the next overlay string for delivery by IT, if there is an
5413 overlay string to deliver. Called by set_iterator_to_next when the
5414 end of the current overlay string is reached. If there are more
5415 overlay strings to display, IT->string and
5416 IT->current.overlay_string_index are set appropriately here.
5417 Otherwise IT->string is set to nil. */
5419 static void
5420 next_overlay_string (struct it *it)
5422 ++it->current.overlay_string_index;
5423 if (it->current.overlay_string_index == it->n_overlay_strings)
5425 /* No more overlay strings. Restore IT's settings to what
5426 they were before overlay strings were processed, and
5427 continue to deliver from current_buffer. */
5429 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5430 pop_it (it);
5431 eassert (it->sp > 0
5432 || (NILP (it->string)
5433 && it->method == GET_FROM_BUFFER
5434 && it->stop_charpos >= BEGV
5435 && it->stop_charpos <= it->end_charpos));
5436 it->current.overlay_string_index = -1;
5437 it->n_overlay_strings = 0;
5438 it->overlay_strings_charpos = -1;
5439 /* If there's an empty display string on the stack, pop the
5440 stack, to resync the bidi iterator with IT's position. Such
5441 empty strings are pushed onto the stack in
5442 get_overlay_strings_1. */
5443 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5444 pop_it (it);
5446 /* If we're at the end of the buffer, record that we have
5447 processed the overlay strings there already, so that
5448 next_element_from_buffer doesn't try it again. */
5449 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5450 it->overlay_strings_at_end_processed_p = 1;
5452 else
5454 /* There are more overlay strings to process. If
5455 IT->current.overlay_string_index has advanced to a position
5456 where we must load IT->overlay_strings with more strings, do
5457 it. We must load at the IT->overlay_strings_charpos where
5458 IT->n_overlay_strings was originally computed; when invisible
5459 text is present, this might not be IT_CHARPOS (Bug#7016). */
5460 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5462 if (it->current.overlay_string_index && i == 0)
5463 load_overlay_strings (it, it->overlay_strings_charpos);
5465 /* Initialize IT to deliver display elements from the overlay
5466 string. */
5467 it->string = it->overlay_strings[i];
5468 it->multibyte_p = STRING_MULTIBYTE (it->string);
5469 SET_TEXT_POS (it->current.string_pos, 0, 0);
5470 it->method = GET_FROM_STRING;
5471 it->stop_charpos = 0;
5472 it->end_charpos = SCHARS (it->string);
5473 if (it->cmp_it.stop_pos >= 0)
5474 it->cmp_it.stop_pos = 0;
5475 it->prev_stop = 0;
5476 it->base_level_stop = 0;
5478 /* Set up the bidi iterator for this overlay string. */
5479 if (it->bidi_p)
5481 it->bidi_it.string.lstring = it->string;
5482 it->bidi_it.string.s = NULL;
5483 it->bidi_it.string.schars = SCHARS (it->string);
5484 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5485 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5486 it->bidi_it.string.unibyte = !it->multibyte_p;
5487 it->bidi_it.w = it->w;
5488 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5492 CHECK_IT (it);
5496 /* Compare two overlay_entry structures E1 and E2. Used as a
5497 comparison function for qsort in load_overlay_strings. Overlay
5498 strings for the same position are sorted so that
5500 1. All after-strings come in front of before-strings, except
5501 when they come from the same overlay.
5503 2. Within after-strings, strings are sorted so that overlay strings
5504 from overlays with higher priorities come first.
5506 2. Within before-strings, strings are sorted so that overlay
5507 strings from overlays with higher priorities come last.
5509 Value is analogous to strcmp. */
5512 static int
5513 compare_overlay_entries (const void *e1, const void *e2)
5515 struct overlay_entry const *entry1 = e1;
5516 struct overlay_entry const *entry2 = e2;
5517 int result;
5519 if (entry1->after_string_p != entry2->after_string_p)
5521 /* Let after-strings appear in front of before-strings if
5522 they come from different overlays. */
5523 if (EQ (entry1->overlay, entry2->overlay))
5524 result = entry1->after_string_p ? 1 : -1;
5525 else
5526 result = entry1->after_string_p ? -1 : 1;
5528 else if (entry1->priority != entry2->priority)
5530 if (entry1->after_string_p)
5531 /* After-strings sorted in order of decreasing priority. */
5532 result = entry2->priority < entry1->priority ? -1 : 1;
5533 else
5534 /* Before-strings sorted in order of increasing priority. */
5535 result = entry1->priority < entry2->priority ? -1 : 1;
5537 else
5538 result = 0;
5540 return result;
5544 /* Load the vector IT->overlay_strings with overlay strings from IT's
5545 current buffer position, or from CHARPOS if that is > 0. Set
5546 IT->n_overlays to the total number of overlay strings found.
5548 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5549 a time. On entry into load_overlay_strings,
5550 IT->current.overlay_string_index gives the number of overlay
5551 strings that have already been loaded by previous calls to this
5552 function.
5554 IT->add_overlay_start contains an additional overlay start
5555 position to consider for taking overlay strings from, if non-zero.
5556 This position comes into play when the overlay has an `invisible'
5557 property, and both before and after-strings. When we've skipped to
5558 the end of the overlay, because of its `invisible' property, we
5559 nevertheless want its before-string to appear.
5560 IT->add_overlay_start will contain the overlay start position
5561 in this case.
5563 Overlay strings are sorted so that after-string strings come in
5564 front of before-string strings. Within before and after-strings,
5565 strings are sorted by overlay priority. See also function
5566 compare_overlay_entries. */
5568 static void
5569 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5571 Lisp_Object overlay, window, str, invisible;
5572 struct Lisp_Overlay *ov;
5573 ptrdiff_t start, end;
5574 ptrdiff_t size = 20;
5575 ptrdiff_t n = 0, i, j;
5576 int invis_p;
5577 struct overlay_entry *entries = alloca (size * sizeof *entries);
5578 USE_SAFE_ALLOCA;
5580 if (charpos <= 0)
5581 charpos = IT_CHARPOS (*it);
5583 /* Append the overlay string STRING of overlay OVERLAY to vector
5584 `entries' which has size `size' and currently contains `n'
5585 elements. AFTER_P non-zero means STRING is an after-string of
5586 OVERLAY. */
5587 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5588 do \
5590 Lisp_Object priority; \
5592 if (n == size) \
5594 struct overlay_entry *old = entries; \
5595 SAFE_NALLOCA (entries, 2, size); \
5596 memcpy (entries, old, size * sizeof *entries); \
5597 size *= 2; \
5600 entries[n].string = (STRING); \
5601 entries[n].overlay = (OVERLAY); \
5602 priority = Foverlay_get ((OVERLAY), Qpriority); \
5603 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5604 entries[n].after_string_p = (AFTER_P); \
5605 ++n; \
5607 while (0)
5609 /* Process overlay before the overlay center. */
5610 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5612 XSETMISC (overlay, ov);
5613 eassert (OVERLAYP (overlay));
5614 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5615 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5617 if (end < charpos)
5618 break;
5620 /* Skip this overlay if it doesn't start or end at IT's current
5621 position. */
5622 if (end != charpos && start != charpos)
5623 continue;
5625 /* Skip this overlay if it doesn't apply to IT->w. */
5626 window = Foverlay_get (overlay, Qwindow);
5627 if (WINDOWP (window) && XWINDOW (window) != it->w)
5628 continue;
5630 /* If the text ``under'' the overlay is invisible, both before-
5631 and after-strings from this overlay are visible; start and
5632 end position are indistinguishable. */
5633 invisible = Foverlay_get (overlay, Qinvisible);
5634 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5636 /* If overlay has a non-empty before-string, record it. */
5637 if ((start == charpos || (end == charpos && invis_p))
5638 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5639 && SCHARS (str))
5640 RECORD_OVERLAY_STRING (overlay, str, 0);
5642 /* If overlay has a non-empty after-string, record it. */
5643 if ((end == charpos || (start == charpos && invis_p))
5644 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5645 && SCHARS (str))
5646 RECORD_OVERLAY_STRING (overlay, str, 1);
5649 /* Process overlays after the overlay center. */
5650 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5652 XSETMISC (overlay, ov);
5653 eassert (OVERLAYP (overlay));
5654 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5655 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5657 if (start > charpos)
5658 break;
5660 /* Skip this overlay if it doesn't start or end at IT's current
5661 position. */
5662 if (end != charpos && start != charpos)
5663 continue;
5665 /* Skip this overlay if it doesn't apply to IT->w. */
5666 window = Foverlay_get (overlay, Qwindow);
5667 if (WINDOWP (window) && XWINDOW (window) != it->w)
5668 continue;
5670 /* If the text ``under'' the overlay is invisible, it has a zero
5671 dimension, and both before- and after-strings apply. */
5672 invisible = Foverlay_get (overlay, Qinvisible);
5673 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5675 /* If overlay has a non-empty before-string, record it. */
5676 if ((start == charpos || (end == charpos && invis_p))
5677 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5678 && SCHARS (str))
5679 RECORD_OVERLAY_STRING (overlay, str, 0);
5681 /* If overlay has a non-empty after-string, record it. */
5682 if ((end == charpos || (start == charpos && invis_p))
5683 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5684 && SCHARS (str))
5685 RECORD_OVERLAY_STRING (overlay, str, 1);
5688 #undef RECORD_OVERLAY_STRING
5690 /* Sort entries. */
5691 if (n > 1)
5692 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5694 /* Record number of overlay strings, and where we computed it. */
5695 it->n_overlay_strings = n;
5696 it->overlay_strings_charpos = charpos;
5698 /* IT->current.overlay_string_index is the number of overlay strings
5699 that have already been consumed by IT. Copy some of the
5700 remaining overlay strings to IT->overlay_strings. */
5701 i = 0;
5702 j = it->current.overlay_string_index;
5703 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5705 it->overlay_strings[i] = entries[j].string;
5706 it->string_overlays[i++] = entries[j++].overlay;
5709 CHECK_IT (it);
5710 SAFE_FREE ();
5714 /* Get the first chunk of overlay strings at IT's current buffer
5715 position, or at CHARPOS if that is > 0. Value is non-zero if at
5716 least one overlay string was found. */
5718 static int
5719 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5721 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5722 process. This fills IT->overlay_strings with strings, and sets
5723 IT->n_overlay_strings to the total number of strings to process.
5724 IT->pos.overlay_string_index has to be set temporarily to zero
5725 because load_overlay_strings needs this; it must be set to -1
5726 when no overlay strings are found because a zero value would
5727 indicate a position in the first overlay string. */
5728 it->current.overlay_string_index = 0;
5729 load_overlay_strings (it, charpos);
5731 /* If we found overlay strings, set up IT to deliver display
5732 elements from the first one. Otherwise set up IT to deliver
5733 from current_buffer. */
5734 if (it->n_overlay_strings)
5736 /* Make sure we know settings in current_buffer, so that we can
5737 restore meaningful values when we're done with the overlay
5738 strings. */
5739 if (compute_stop_p)
5740 compute_stop_pos (it);
5741 eassert (it->face_id >= 0);
5743 /* Save IT's settings. They are restored after all overlay
5744 strings have been processed. */
5745 eassert (!compute_stop_p || it->sp == 0);
5747 /* When called from handle_stop, there might be an empty display
5748 string loaded. In that case, don't bother saving it. But
5749 don't use this optimization with the bidi iterator, since we
5750 need the corresponding pop_it call to resync the bidi
5751 iterator's position with IT's position, after we are done
5752 with the overlay strings. (The corresponding call to pop_it
5753 in case of an empty display string is in
5754 next_overlay_string.) */
5755 if (!(!it->bidi_p
5756 && STRINGP (it->string) && !SCHARS (it->string)))
5757 push_it (it, NULL);
5759 /* Set up IT to deliver display elements from the first overlay
5760 string. */
5761 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5762 it->string = it->overlay_strings[0];
5763 it->from_overlay = Qnil;
5764 it->stop_charpos = 0;
5765 eassert (STRINGP (it->string));
5766 it->end_charpos = SCHARS (it->string);
5767 it->prev_stop = 0;
5768 it->base_level_stop = 0;
5769 it->multibyte_p = STRING_MULTIBYTE (it->string);
5770 it->method = GET_FROM_STRING;
5771 it->from_disp_prop_p = 0;
5773 /* Force paragraph direction to be that of the parent
5774 buffer. */
5775 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5776 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5777 else
5778 it->paragraph_embedding = L2R;
5780 /* Set up the bidi iterator for this overlay string. */
5781 if (it->bidi_p)
5783 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5785 it->bidi_it.string.lstring = it->string;
5786 it->bidi_it.string.s = NULL;
5787 it->bidi_it.string.schars = SCHARS (it->string);
5788 it->bidi_it.string.bufpos = pos;
5789 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5790 it->bidi_it.string.unibyte = !it->multibyte_p;
5791 it->bidi_it.w = it->w;
5792 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5794 return 1;
5797 it->current.overlay_string_index = -1;
5798 return 0;
5801 static int
5802 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5804 it->string = Qnil;
5805 it->method = GET_FROM_BUFFER;
5807 (void) get_overlay_strings_1 (it, charpos, 1);
5809 CHECK_IT (it);
5811 /* Value is non-zero if we found at least one overlay string. */
5812 return STRINGP (it->string);
5817 /***********************************************************************
5818 Saving and restoring state
5819 ***********************************************************************/
5821 /* Save current settings of IT on IT->stack. Called, for example,
5822 before setting up IT for an overlay string, to be able to restore
5823 IT's settings to what they were after the overlay string has been
5824 processed. If POSITION is non-NULL, it is the position to save on
5825 the stack instead of IT->position. */
5827 static void
5828 push_it (struct it *it, struct text_pos *position)
5830 struct iterator_stack_entry *p;
5832 eassert (it->sp < IT_STACK_SIZE);
5833 p = it->stack + it->sp;
5835 p->stop_charpos = it->stop_charpos;
5836 p->prev_stop = it->prev_stop;
5837 p->base_level_stop = it->base_level_stop;
5838 p->cmp_it = it->cmp_it;
5839 eassert (it->face_id >= 0);
5840 p->face_id = it->face_id;
5841 p->string = it->string;
5842 p->method = it->method;
5843 p->from_overlay = it->from_overlay;
5844 switch (p->method)
5846 case GET_FROM_IMAGE:
5847 p->u.image.object = it->object;
5848 p->u.image.image_id = it->image_id;
5849 p->u.image.slice = it->slice;
5850 break;
5851 case GET_FROM_STRETCH:
5852 p->u.stretch.object = it->object;
5853 break;
5855 p->position = position ? *position : it->position;
5856 p->current = it->current;
5857 p->end_charpos = it->end_charpos;
5858 p->string_nchars = it->string_nchars;
5859 p->area = it->area;
5860 p->multibyte_p = it->multibyte_p;
5861 p->avoid_cursor_p = it->avoid_cursor_p;
5862 p->space_width = it->space_width;
5863 p->font_height = it->font_height;
5864 p->voffset = it->voffset;
5865 p->string_from_display_prop_p = it->string_from_display_prop_p;
5866 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5867 p->display_ellipsis_p = 0;
5868 p->line_wrap = it->line_wrap;
5869 p->bidi_p = it->bidi_p;
5870 p->paragraph_embedding = it->paragraph_embedding;
5871 p->from_disp_prop_p = it->from_disp_prop_p;
5872 ++it->sp;
5874 /* Save the state of the bidi iterator as well. */
5875 if (it->bidi_p)
5876 bidi_push_it (&it->bidi_it);
5879 static void
5880 iterate_out_of_display_property (struct it *it)
5882 int buffer_p = !STRINGP (it->string);
5883 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5884 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5886 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5888 /* Maybe initialize paragraph direction. If we are at the beginning
5889 of a new paragraph, next_element_from_buffer may not have a
5890 chance to do that. */
5891 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5892 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5893 /* prev_stop can be zero, so check against BEGV as well. */
5894 while (it->bidi_it.charpos >= bob
5895 && it->prev_stop <= it->bidi_it.charpos
5896 && it->bidi_it.charpos < CHARPOS (it->position)
5897 && it->bidi_it.charpos < eob)
5898 bidi_move_to_visually_next (&it->bidi_it);
5899 /* Record the stop_pos we just crossed, for when we cross it
5900 back, maybe. */
5901 if (it->bidi_it.charpos > CHARPOS (it->position))
5902 it->prev_stop = CHARPOS (it->position);
5903 /* If we ended up not where pop_it put us, resync IT's
5904 positional members with the bidi iterator. */
5905 if (it->bidi_it.charpos != CHARPOS (it->position))
5906 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5907 if (buffer_p)
5908 it->current.pos = it->position;
5909 else
5910 it->current.string_pos = it->position;
5913 /* Restore IT's settings from IT->stack. Called, for example, when no
5914 more overlay strings must be processed, and we return to delivering
5915 display elements from a buffer, or when the end of a string from a
5916 `display' property is reached and we return to delivering display
5917 elements from an overlay string, or from a buffer. */
5919 static void
5920 pop_it (struct it *it)
5922 struct iterator_stack_entry *p;
5923 int from_display_prop = it->from_disp_prop_p;
5925 eassert (it->sp > 0);
5926 --it->sp;
5927 p = it->stack + it->sp;
5928 it->stop_charpos = p->stop_charpos;
5929 it->prev_stop = p->prev_stop;
5930 it->base_level_stop = p->base_level_stop;
5931 it->cmp_it = p->cmp_it;
5932 it->face_id = p->face_id;
5933 it->current = p->current;
5934 it->position = p->position;
5935 it->string = p->string;
5936 it->from_overlay = p->from_overlay;
5937 if (NILP (it->string))
5938 SET_TEXT_POS (it->current.string_pos, -1, -1);
5939 it->method = p->method;
5940 switch (it->method)
5942 case GET_FROM_IMAGE:
5943 it->image_id = p->u.image.image_id;
5944 it->object = p->u.image.object;
5945 it->slice = p->u.image.slice;
5946 break;
5947 case GET_FROM_STRETCH:
5948 it->object = p->u.stretch.object;
5949 break;
5950 case GET_FROM_BUFFER:
5951 it->object = it->w->contents;
5952 break;
5953 case GET_FROM_STRING:
5954 it->object = it->string;
5955 break;
5956 case GET_FROM_DISPLAY_VECTOR:
5957 if (it->s)
5958 it->method = GET_FROM_C_STRING;
5959 else if (STRINGP (it->string))
5960 it->method = GET_FROM_STRING;
5961 else
5963 it->method = GET_FROM_BUFFER;
5964 it->object = it->w->contents;
5967 it->end_charpos = p->end_charpos;
5968 it->string_nchars = p->string_nchars;
5969 it->area = p->area;
5970 it->multibyte_p = p->multibyte_p;
5971 it->avoid_cursor_p = p->avoid_cursor_p;
5972 it->space_width = p->space_width;
5973 it->font_height = p->font_height;
5974 it->voffset = p->voffset;
5975 it->string_from_display_prop_p = p->string_from_display_prop_p;
5976 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5977 it->line_wrap = p->line_wrap;
5978 it->bidi_p = p->bidi_p;
5979 it->paragraph_embedding = p->paragraph_embedding;
5980 it->from_disp_prop_p = p->from_disp_prop_p;
5981 if (it->bidi_p)
5983 bidi_pop_it (&it->bidi_it);
5984 /* Bidi-iterate until we get out of the portion of text, if any,
5985 covered by a `display' text property or by an overlay with
5986 `display' property. (We cannot just jump there, because the
5987 internal coherency of the bidi iterator state can not be
5988 preserved across such jumps.) We also must determine the
5989 paragraph base direction if the overlay we just processed is
5990 at the beginning of a new paragraph. */
5991 if (from_display_prop
5992 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5993 iterate_out_of_display_property (it);
5995 eassert ((BUFFERP (it->object)
5996 && IT_CHARPOS (*it) == it->bidi_it.charpos
5997 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5998 || (STRINGP (it->object)
5999 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6000 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6001 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6007 /***********************************************************************
6008 Moving over lines
6009 ***********************************************************************/
6011 /* Set IT's current position to the previous line start. */
6013 static void
6014 back_to_previous_line_start (struct it *it)
6016 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6018 DEC_BOTH (cp, bp);
6019 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6023 /* Move IT to the next line start.
6025 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6026 we skipped over part of the text (as opposed to moving the iterator
6027 continuously over the text). Otherwise, don't change the value
6028 of *SKIPPED_P.
6030 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6031 iterator on the newline, if it was found.
6033 Newlines may come from buffer text, overlay strings, or strings
6034 displayed via the `display' property. That's the reason we can't
6035 simply use find_newline_no_quit.
6037 Note that this function may not skip over invisible text that is so
6038 because of text properties and immediately follows a newline. If
6039 it would, function reseat_at_next_visible_line_start, when called
6040 from set_iterator_to_next, would effectively make invisible
6041 characters following a newline part of the wrong glyph row, which
6042 leads to wrong cursor motion. */
6044 static int
6045 forward_to_next_line_start (struct it *it, int *skipped_p,
6046 struct bidi_it *bidi_it_prev)
6048 ptrdiff_t old_selective;
6049 int newline_found_p, n;
6050 const int MAX_NEWLINE_DISTANCE = 500;
6052 /* If already on a newline, just consume it to avoid unintended
6053 skipping over invisible text below. */
6054 if (it->what == IT_CHARACTER
6055 && it->c == '\n'
6056 && CHARPOS (it->position) == IT_CHARPOS (*it))
6058 if (it->bidi_p && bidi_it_prev)
6059 *bidi_it_prev = it->bidi_it;
6060 set_iterator_to_next (it, 0);
6061 it->c = 0;
6062 return 1;
6065 /* Don't handle selective display in the following. It's (a)
6066 unnecessary because it's done by the caller, and (b) leads to an
6067 infinite recursion because next_element_from_ellipsis indirectly
6068 calls this function. */
6069 old_selective = it->selective;
6070 it->selective = 0;
6072 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6073 from buffer text. */
6074 for (n = newline_found_p = 0;
6075 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6076 n += STRINGP (it->string) ? 0 : 1)
6078 if (!get_next_display_element (it))
6079 return 0;
6080 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6081 if (newline_found_p && it->bidi_p && bidi_it_prev)
6082 *bidi_it_prev = it->bidi_it;
6083 set_iterator_to_next (it, 0);
6086 /* If we didn't find a newline near enough, see if we can use a
6087 short-cut. */
6088 if (!newline_found_p)
6090 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6091 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6092 1, &bytepos);
6093 Lisp_Object pos;
6095 eassert (!STRINGP (it->string));
6097 /* If there isn't any `display' property in sight, and no
6098 overlays, we can just use the position of the newline in
6099 buffer text. */
6100 if (it->stop_charpos >= limit
6101 || ((pos = Fnext_single_property_change (make_number (start),
6102 Qdisplay, Qnil,
6103 make_number (limit)),
6104 NILP (pos))
6105 && next_overlay_change (start) == ZV))
6107 if (!it->bidi_p)
6109 IT_CHARPOS (*it) = limit;
6110 IT_BYTEPOS (*it) = bytepos;
6112 else
6114 struct bidi_it bprev;
6116 /* Help bidi.c avoid expensive searches for display
6117 properties and overlays, by telling it that there are
6118 none up to `limit'. */
6119 if (it->bidi_it.disp_pos < limit)
6121 it->bidi_it.disp_pos = limit;
6122 it->bidi_it.disp_prop = 0;
6124 do {
6125 bprev = it->bidi_it;
6126 bidi_move_to_visually_next (&it->bidi_it);
6127 } while (it->bidi_it.charpos != limit);
6128 IT_CHARPOS (*it) = limit;
6129 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6130 if (bidi_it_prev)
6131 *bidi_it_prev = bprev;
6133 *skipped_p = newline_found_p = 1;
6135 else
6137 while (get_next_display_element (it)
6138 && !newline_found_p)
6140 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6141 if (newline_found_p && it->bidi_p && bidi_it_prev)
6142 *bidi_it_prev = it->bidi_it;
6143 set_iterator_to_next (it, 0);
6148 it->selective = old_selective;
6149 return newline_found_p;
6153 /* Set IT's current position to the previous visible line start. Skip
6154 invisible text that is so either due to text properties or due to
6155 selective display. Caution: this does not change IT->current_x and
6156 IT->hpos. */
6158 static void
6159 back_to_previous_visible_line_start (struct it *it)
6161 while (IT_CHARPOS (*it) > BEGV)
6163 back_to_previous_line_start (it);
6165 if (IT_CHARPOS (*it) <= BEGV)
6166 break;
6168 /* If selective > 0, then lines indented more than its value are
6169 invisible. */
6170 if (it->selective > 0
6171 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6172 it->selective))
6173 continue;
6175 /* Check the newline before point for invisibility. */
6177 Lisp_Object prop;
6178 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6179 Qinvisible, it->window);
6180 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6181 continue;
6184 if (IT_CHARPOS (*it) <= BEGV)
6185 break;
6188 struct it it2;
6189 void *it2data = NULL;
6190 ptrdiff_t pos;
6191 ptrdiff_t beg, end;
6192 Lisp_Object val, overlay;
6194 SAVE_IT (it2, *it, it2data);
6196 /* If newline is part of a composition, continue from start of composition */
6197 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6198 && beg < IT_CHARPOS (*it))
6199 goto replaced;
6201 /* If newline is replaced by a display property, find start of overlay
6202 or interval and continue search from that point. */
6203 pos = --IT_CHARPOS (it2);
6204 --IT_BYTEPOS (it2);
6205 it2.sp = 0;
6206 bidi_unshelve_cache (NULL, 0);
6207 it2.string_from_display_prop_p = 0;
6208 it2.from_disp_prop_p = 0;
6209 if (handle_display_prop (&it2) == HANDLED_RETURN
6210 && !NILP (val = get_char_property_and_overlay
6211 (make_number (pos), Qdisplay, Qnil, &overlay))
6212 && (OVERLAYP (overlay)
6213 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6214 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6216 RESTORE_IT (it, it, it2data);
6217 goto replaced;
6220 /* Newline is not replaced by anything -- so we are done. */
6221 RESTORE_IT (it, it, it2data);
6222 break;
6224 replaced:
6225 if (beg < BEGV)
6226 beg = BEGV;
6227 IT_CHARPOS (*it) = beg;
6228 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6232 it->continuation_lines_width = 0;
6234 eassert (IT_CHARPOS (*it) >= BEGV);
6235 eassert (IT_CHARPOS (*it) == BEGV
6236 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6237 CHECK_IT (it);
6241 /* Reseat iterator IT at the previous visible line start. Skip
6242 invisible text that is so either due to text properties or due to
6243 selective display. At the end, update IT's overlay information,
6244 face information etc. */
6246 void
6247 reseat_at_previous_visible_line_start (struct it *it)
6249 back_to_previous_visible_line_start (it);
6250 reseat (it, it->current.pos, 1);
6251 CHECK_IT (it);
6255 /* Reseat iterator IT on the next visible line start in the current
6256 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6257 preceding the line start. Skip over invisible text that is so
6258 because of selective display. Compute faces, overlays etc at the
6259 new position. Note that this function does not skip over text that
6260 is invisible because of text properties. */
6262 static void
6263 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6265 int newline_found_p, skipped_p = 0;
6266 struct bidi_it bidi_it_prev;
6268 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6270 /* Skip over lines that are invisible because they are indented
6271 more than the value of IT->selective. */
6272 if (it->selective > 0)
6273 while (IT_CHARPOS (*it) < ZV
6274 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6275 it->selective))
6277 eassert (IT_BYTEPOS (*it) == BEGV
6278 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6279 newline_found_p =
6280 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6283 /* Position on the newline if that's what's requested. */
6284 if (on_newline_p && newline_found_p)
6286 if (STRINGP (it->string))
6288 if (IT_STRING_CHARPOS (*it) > 0)
6290 if (!it->bidi_p)
6292 --IT_STRING_CHARPOS (*it);
6293 --IT_STRING_BYTEPOS (*it);
6295 else
6297 /* We need to restore the bidi iterator to the state
6298 it had on the newline, and resync the IT's
6299 position with that. */
6300 it->bidi_it = bidi_it_prev;
6301 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6302 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6306 else if (IT_CHARPOS (*it) > BEGV)
6308 if (!it->bidi_p)
6310 --IT_CHARPOS (*it);
6311 --IT_BYTEPOS (*it);
6313 else
6315 /* We need to restore the bidi iterator to the state it
6316 had on the newline and resync IT with that. */
6317 it->bidi_it = bidi_it_prev;
6318 IT_CHARPOS (*it) = it->bidi_it.charpos;
6319 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6321 reseat (it, it->current.pos, 0);
6324 else if (skipped_p)
6325 reseat (it, it->current.pos, 0);
6327 CHECK_IT (it);
6332 /***********************************************************************
6333 Changing an iterator's position
6334 ***********************************************************************/
6336 /* Change IT's current position to POS in current_buffer. If FORCE_P
6337 is non-zero, always check for text properties at the new position.
6338 Otherwise, text properties are only looked up if POS >=
6339 IT->check_charpos of a property. */
6341 static void
6342 reseat (struct it *it, struct text_pos pos, int force_p)
6344 ptrdiff_t original_pos = IT_CHARPOS (*it);
6346 reseat_1 (it, pos, 0);
6348 /* Determine where to check text properties. Avoid doing it
6349 where possible because text property lookup is very expensive. */
6350 if (force_p
6351 || CHARPOS (pos) > it->stop_charpos
6352 || CHARPOS (pos) < original_pos)
6354 if (it->bidi_p)
6356 /* For bidi iteration, we need to prime prev_stop and
6357 base_level_stop with our best estimations. */
6358 /* Implementation note: Of course, POS is not necessarily a
6359 stop position, so assigning prev_pos to it is a lie; we
6360 should have called compute_stop_backwards. However, if
6361 the current buffer does not include any R2L characters,
6362 that call would be a waste of cycles, because the
6363 iterator will never move back, and thus never cross this
6364 "fake" stop position. So we delay that backward search
6365 until the time we really need it, in next_element_from_buffer. */
6366 if (CHARPOS (pos) != it->prev_stop)
6367 it->prev_stop = CHARPOS (pos);
6368 if (CHARPOS (pos) < it->base_level_stop)
6369 it->base_level_stop = 0; /* meaning it's unknown */
6370 handle_stop (it);
6372 else
6374 handle_stop (it);
6375 it->prev_stop = it->base_level_stop = 0;
6380 CHECK_IT (it);
6384 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6385 IT->stop_pos to POS, also. */
6387 static void
6388 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6390 /* Don't call this function when scanning a C string. */
6391 eassert (it->s == NULL);
6393 /* POS must be a reasonable value. */
6394 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6396 it->current.pos = it->position = pos;
6397 it->end_charpos = ZV;
6398 it->dpvec = NULL;
6399 it->current.dpvec_index = -1;
6400 it->current.overlay_string_index = -1;
6401 IT_STRING_CHARPOS (*it) = -1;
6402 IT_STRING_BYTEPOS (*it) = -1;
6403 it->string = Qnil;
6404 it->method = GET_FROM_BUFFER;
6405 it->object = it->w->contents;
6406 it->area = TEXT_AREA;
6407 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6408 it->sp = 0;
6409 it->string_from_display_prop_p = 0;
6410 it->string_from_prefix_prop_p = 0;
6412 it->from_disp_prop_p = 0;
6413 it->face_before_selective_p = 0;
6414 if (it->bidi_p)
6416 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6417 &it->bidi_it);
6418 bidi_unshelve_cache (NULL, 0);
6419 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6420 it->bidi_it.string.s = NULL;
6421 it->bidi_it.string.lstring = Qnil;
6422 it->bidi_it.string.bufpos = 0;
6423 it->bidi_it.string.unibyte = 0;
6424 it->bidi_it.w = it->w;
6427 if (set_stop_p)
6429 it->stop_charpos = CHARPOS (pos);
6430 it->base_level_stop = CHARPOS (pos);
6432 /* This make the information stored in it->cmp_it invalidate. */
6433 it->cmp_it.id = -1;
6437 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6438 If S is non-null, it is a C string to iterate over. Otherwise,
6439 STRING gives a Lisp string to iterate over.
6441 If PRECISION > 0, don't return more then PRECISION number of
6442 characters from the string.
6444 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6445 characters have been returned. FIELD_WIDTH < 0 means an infinite
6446 field width.
6448 MULTIBYTE = 0 means disable processing of multibyte characters,
6449 MULTIBYTE > 0 means enable it,
6450 MULTIBYTE < 0 means use IT->multibyte_p.
6452 IT must be initialized via a prior call to init_iterator before
6453 calling this function. */
6455 static void
6456 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6457 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6458 int multibyte)
6460 /* No region in strings. */
6461 it->region_beg_charpos = it->region_end_charpos = -1;
6463 /* No text property checks performed by default, but see below. */
6464 it->stop_charpos = -1;
6466 /* Set iterator position and end position. */
6467 memset (&it->current, 0, sizeof it->current);
6468 it->current.overlay_string_index = -1;
6469 it->current.dpvec_index = -1;
6470 eassert (charpos >= 0);
6472 /* If STRING is specified, use its multibyteness, otherwise use the
6473 setting of MULTIBYTE, if specified. */
6474 if (multibyte >= 0)
6475 it->multibyte_p = multibyte > 0;
6477 /* Bidirectional reordering of strings is controlled by the default
6478 value of bidi-display-reordering. Don't try to reorder while
6479 loading loadup.el, as the necessary character property tables are
6480 not yet available. */
6481 it->bidi_p =
6482 NILP (Vpurify_flag)
6483 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6485 if (s == NULL)
6487 eassert (STRINGP (string));
6488 it->string = string;
6489 it->s = NULL;
6490 it->end_charpos = it->string_nchars = SCHARS (string);
6491 it->method = GET_FROM_STRING;
6492 it->current.string_pos = string_pos (charpos, string);
6494 if (it->bidi_p)
6496 it->bidi_it.string.lstring = string;
6497 it->bidi_it.string.s = NULL;
6498 it->bidi_it.string.schars = it->end_charpos;
6499 it->bidi_it.string.bufpos = 0;
6500 it->bidi_it.string.from_disp_str = 0;
6501 it->bidi_it.string.unibyte = !it->multibyte_p;
6502 it->bidi_it.w = it->w;
6503 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6504 FRAME_WINDOW_P (it->f), &it->bidi_it);
6507 else
6509 it->s = (const unsigned char *) s;
6510 it->string = Qnil;
6512 /* Note that we use IT->current.pos, not it->current.string_pos,
6513 for displaying C strings. */
6514 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6515 if (it->multibyte_p)
6517 it->current.pos = c_string_pos (charpos, s, 1);
6518 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6520 else
6522 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6523 it->end_charpos = it->string_nchars = strlen (s);
6526 if (it->bidi_p)
6528 it->bidi_it.string.lstring = Qnil;
6529 it->bidi_it.string.s = (const unsigned char *) s;
6530 it->bidi_it.string.schars = it->end_charpos;
6531 it->bidi_it.string.bufpos = 0;
6532 it->bidi_it.string.from_disp_str = 0;
6533 it->bidi_it.string.unibyte = !it->multibyte_p;
6534 it->bidi_it.w = it->w;
6535 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6536 &it->bidi_it);
6538 it->method = GET_FROM_C_STRING;
6541 /* PRECISION > 0 means don't return more than PRECISION characters
6542 from the string. */
6543 if (precision > 0 && it->end_charpos - charpos > precision)
6545 it->end_charpos = it->string_nchars = charpos + precision;
6546 if (it->bidi_p)
6547 it->bidi_it.string.schars = it->end_charpos;
6550 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6551 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6552 FIELD_WIDTH < 0 means infinite field width. This is useful for
6553 padding with `-' at the end of a mode line. */
6554 if (field_width < 0)
6555 field_width = INFINITY;
6556 /* Implementation note: We deliberately don't enlarge
6557 it->bidi_it.string.schars here to fit it->end_charpos, because
6558 the bidi iterator cannot produce characters out of thin air. */
6559 if (field_width > it->end_charpos - charpos)
6560 it->end_charpos = charpos + field_width;
6562 /* Use the standard display table for displaying strings. */
6563 if (DISP_TABLE_P (Vstandard_display_table))
6564 it->dp = XCHAR_TABLE (Vstandard_display_table);
6566 it->stop_charpos = charpos;
6567 it->prev_stop = charpos;
6568 it->base_level_stop = 0;
6569 if (it->bidi_p)
6571 it->bidi_it.first_elt = 1;
6572 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6573 it->bidi_it.disp_pos = -1;
6575 if (s == NULL && it->multibyte_p)
6577 ptrdiff_t endpos = SCHARS (it->string);
6578 if (endpos > it->end_charpos)
6579 endpos = it->end_charpos;
6580 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6581 it->string);
6583 CHECK_IT (it);
6588 /***********************************************************************
6589 Iteration
6590 ***********************************************************************/
6592 /* Map enum it_method value to corresponding next_element_from_* function. */
6594 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6596 next_element_from_buffer,
6597 next_element_from_display_vector,
6598 next_element_from_string,
6599 next_element_from_c_string,
6600 next_element_from_image,
6601 next_element_from_stretch
6604 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6607 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6608 (possibly with the following characters). */
6610 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6611 ((IT)->cmp_it.id >= 0 \
6612 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6613 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6614 END_CHARPOS, (IT)->w, \
6615 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6616 (IT)->string)))
6619 /* Lookup the char-table Vglyphless_char_display for character C (-1
6620 if we want information for no-font case), and return the display
6621 method symbol. By side-effect, update it->what and
6622 it->glyphless_method. This function is called from
6623 get_next_display_element for each character element, and from
6624 x_produce_glyphs when no suitable font was found. */
6626 Lisp_Object
6627 lookup_glyphless_char_display (int c, struct it *it)
6629 Lisp_Object glyphless_method = Qnil;
6631 if (CHAR_TABLE_P (Vglyphless_char_display)
6632 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6634 if (c >= 0)
6636 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6637 if (CONSP (glyphless_method))
6638 glyphless_method = FRAME_WINDOW_P (it->f)
6639 ? XCAR (glyphless_method)
6640 : XCDR (glyphless_method);
6642 else
6643 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6646 retry:
6647 if (NILP (glyphless_method))
6649 if (c >= 0)
6650 /* The default is to display the character by a proper font. */
6651 return Qnil;
6652 /* The default for the no-font case is to display an empty box. */
6653 glyphless_method = Qempty_box;
6655 if (EQ (glyphless_method, Qzero_width))
6657 if (c >= 0)
6658 return glyphless_method;
6659 /* This method can't be used for the no-font case. */
6660 glyphless_method = Qempty_box;
6662 if (EQ (glyphless_method, Qthin_space))
6663 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6664 else if (EQ (glyphless_method, Qempty_box))
6665 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6666 else if (EQ (glyphless_method, Qhex_code))
6667 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6668 else if (STRINGP (glyphless_method))
6669 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6670 else
6672 /* Invalid value. We use the default method. */
6673 glyphless_method = Qnil;
6674 goto retry;
6676 it->what = IT_GLYPHLESS;
6677 return glyphless_method;
6680 /* Load IT's display element fields with information about the next
6681 display element from the current position of IT. Value is zero if
6682 end of buffer (or C string) is reached. */
6684 static struct frame *last_escape_glyph_frame = NULL;
6685 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6686 static int last_escape_glyph_merged_face_id = 0;
6688 struct frame *last_glyphless_glyph_frame = NULL;
6689 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6690 int last_glyphless_glyph_merged_face_id = 0;
6692 static int
6693 get_next_display_element (struct it *it)
6695 /* Non-zero means that we found a display element. Zero means that
6696 we hit the end of what we iterate over. Performance note: the
6697 function pointer `method' used here turns out to be faster than
6698 using a sequence of if-statements. */
6699 int success_p;
6701 get_next:
6702 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6704 if (it->what == IT_CHARACTER)
6706 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6707 and only if (a) the resolved directionality of that character
6708 is R..." */
6709 /* FIXME: Do we need an exception for characters from display
6710 tables? */
6711 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6712 it->c = bidi_mirror_char (it->c);
6713 /* Map via display table or translate control characters.
6714 IT->c, IT->len etc. have been set to the next character by
6715 the function call above. If we have a display table, and it
6716 contains an entry for IT->c, translate it. Don't do this if
6717 IT->c itself comes from a display table, otherwise we could
6718 end up in an infinite recursion. (An alternative could be to
6719 count the recursion depth of this function and signal an
6720 error when a certain maximum depth is reached.) Is it worth
6721 it? */
6722 if (success_p && it->dpvec == NULL)
6724 Lisp_Object dv;
6725 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6726 int nonascii_space_p = 0;
6727 int nonascii_hyphen_p = 0;
6728 int c = it->c; /* This is the character to display. */
6730 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6732 eassert (SINGLE_BYTE_CHAR_P (c));
6733 if (unibyte_display_via_language_environment)
6735 c = DECODE_CHAR (unibyte, c);
6736 if (c < 0)
6737 c = BYTE8_TO_CHAR (it->c);
6739 else
6740 c = BYTE8_TO_CHAR (it->c);
6743 if (it->dp
6744 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6745 VECTORP (dv)))
6747 struct Lisp_Vector *v = XVECTOR (dv);
6749 /* Return the first character from the display table
6750 entry, if not empty. If empty, don't display the
6751 current character. */
6752 if (v->header.size)
6754 it->dpvec_char_len = it->len;
6755 it->dpvec = v->contents;
6756 it->dpend = v->contents + v->header.size;
6757 it->current.dpvec_index = 0;
6758 it->dpvec_face_id = -1;
6759 it->saved_face_id = it->face_id;
6760 it->method = GET_FROM_DISPLAY_VECTOR;
6761 it->ellipsis_p = 0;
6763 else
6765 set_iterator_to_next (it, 0);
6767 goto get_next;
6770 if (! NILP (lookup_glyphless_char_display (c, it)))
6772 if (it->what == IT_GLYPHLESS)
6773 goto done;
6774 /* Don't display this character. */
6775 set_iterator_to_next (it, 0);
6776 goto get_next;
6779 /* If `nobreak-char-display' is non-nil, we display
6780 non-ASCII spaces and hyphens specially. */
6781 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6783 if (c == 0xA0)
6784 nonascii_space_p = 1;
6785 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6786 nonascii_hyphen_p = 1;
6789 /* Translate control characters into `\003' or `^C' form.
6790 Control characters coming from a display table entry are
6791 currently not translated because we use IT->dpvec to hold
6792 the translation. This could easily be changed but I
6793 don't believe that it is worth doing.
6795 The characters handled by `nobreak-char-display' must be
6796 translated too.
6798 Non-printable characters and raw-byte characters are also
6799 translated to octal form. */
6800 if (((c < ' ' || c == 127) /* ASCII control chars */
6801 ? (it->area != TEXT_AREA
6802 /* In mode line, treat \n, \t like other crl chars. */
6803 || (c != '\t'
6804 && it->glyph_row
6805 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6806 || (c != '\n' && c != '\t'))
6807 : (nonascii_space_p
6808 || nonascii_hyphen_p
6809 || CHAR_BYTE8_P (c)
6810 || ! CHAR_PRINTABLE_P (c))))
6812 /* C is a control character, non-ASCII space/hyphen,
6813 raw-byte, or a non-printable character which must be
6814 displayed either as '\003' or as `^C' where the '\\'
6815 and '^' can be defined in the display table. Fill
6816 IT->ctl_chars with glyphs for what we have to
6817 display. Then, set IT->dpvec to these glyphs. */
6818 Lisp_Object gc;
6819 int ctl_len;
6820 int face_id;
6821 int lface_id = 0;
6822 int escape_glyph;
6824 /* Handle control characters with ^. */
6826 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6828 int g;
6830 g = '^'; /* default glyph for Control */
6831 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6832 if (it->dp
6833 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6835 g = GLYPH_CODE_CHAR (gc);
6836 lface_id = GLYPH_CODE_FACE (gc);
6838 if (lface_id)
6840 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6842 else if (it->f == last_escape_glyph_frame
6843 && it->face_id == last_escape_glyph_face_id)
6845 face_id = last_escape_glyph_merged_face_id;
6847 else
6849 /* Merge the escape-glyph face into the current face. */
6850 face_id = merge_faces (it->f, Qescape_glyph, 0,
6851 it->face_id);
6852 last_escape_glyph_frame = it->f;
6853 last_escape_glyph_face_id = it->face_id;
6854 last_escape_glyph_merged_face_id = face_id;
6857 XSETINT (it->ctl_chars[0], g);
6858 XSETINT (it->ctl_chars[1], c ^ 0100);
6859 ctl_len = 2;
6860 goto display_control;
6863 /* Handle non-ascii space in the mode where it only gets
6864 highlighting. */
6866 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6868 /* Merge `nobreak-space' into the current face. */
6869 face_id = merge_faces (it->f, Qnobreak_space, 0,
6870 it->face_id);
6871 XSETINT (it->ctl_chars[0], ' ');
6872 ctl_len = 1;
6873 goto display_control;
6876 /* Handle sequences that start with the "escape glyph". */
6878 /* the default escape glyph is \. */
6879 escape_glyph = '\\';
6881 if (it->dp
6882 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6884 escape_glyph = GLYPH_CODE_CHAR (gc);
6885 lface_id = GLYPH_CODE_FACE (gc);
6887 if (lface_id)
6889 /* The display table specified a face.
6890 Merge it into face_id and also into escape_glyph. */
6891 face_id = merge_faces (it->f, Qt, lface_id,
6892 it->face_id);
6894 else if (it->f == last_escape_glyph_frame
6895 && it->face_id == last_escape_glyph_face_id)
6897 face_id = last_escape_glyph_merged_face_id;
6899 else
6901 /* Merge the escape-glyph face into the current face. */
6902 face_id = merge_faces (it->f, Qescape_glyph, 0,
6903 it->face_id);
6904 last_escape_glyph_frame = it->f;
6905 last_escape_glyph_face_id = it->face_id;
6906 last_escape_glyph_merged_face_id = face_id;
6909 /* Draw non-ASCII hyphen with just highlighting: */
6911 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6913 XSETINT (it->ctl_chars[0], '-');
6914 ctl_len = 1;
6915 goto display_control;
6918 /* Draw non-ASCII space/hyphen with escape glyph: */
6920 if (nonascii_space_p || nonascii_hyphen_p)
6922 XSETINT (it->ctl_chars[0], escape_glyph);
6923 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6924 ctl_len = 2;
6925 goto display_control;
6929 char str[10];
6930 int len, i;
6932 if (CHAR_BYTE8_P (c))
6933 /* Display \200 instead of \17777600. */
6934 c = CHAR_TO_BYTE8 (c);
6935 len = sprintf (str, "%03o", c);
6937 XSETINT (it->ctl_chars[0], escape_glyph);
6938 for (i = 0; i < len; i++)
6939 XSETINT (it->ctl_chars[i + 1], str[i]);
6940 ctl_len = len + 1;
6943 display_control:
6944 /* Set up IT->dpvec and return first character from it. */
6945 it->dpvec_char_len = it->len;
6946 it->dpvec = it->ctl_chars;
6947 it->dpend = it->dpvec + ctl_len;
6948 it->current.dpvec_index = 0;
6949 it->dpvec_face_id = face_id;
6950 it->saved_face_id = it->face_id;
6951 it->method = GET_FROM_DISPLAY_VECTOR;
6952 it->ellipsis_p = 0;
6953 goto get_next;
6955 it->char_to_display = c;
6957 else if (success_p)
6959 it->char_to_display = it->c;
6963 /* Adjust face id for a multibyte character. There are no multibyte
6964 character in unibyte text. */
6965 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6966 && it->multibyte_p
6967 && success_p
6968 && FRAME_WINDOW_P (it->f))
6970 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6972 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6974 /* Automatic composition with glyph-string. */
6975 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6977 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6979 else
6981 ptrdiff_t pos = (it->s ? -1
6982 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6983 : IT_CHARPOS (*it));
6984 int c;
6986 if (it->what == IT_CHARACTER)
6987 c = it->char_to_display;
6988 else
6990 struct composition *cmp = composition_table[it->cmp_it.id];
6991 int i;
6993 c = ' ';
6994 for (i = 0; i < cmp->glyph_len; i++)
6995 /* TAB in a composition means display glyphs with
6996 padding space on the left or right. */
6997 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6998 break;
7000 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7004 done:
7005 /* Is this character the last one of a run of characters with
7006 box? If yes, set IT->end_of_box_run_p to 1. */
7007 if (it->face_box_p
7008 && it->s == NULL)
7010 if (it->method == GET_FROM_STRING && it->sp)
7012 int face_id = underlying_face_id (it);
7013 struct face *face = FACE_FROM_ID (it->f, face_id);
7015 if (face)
7017 if (face->box == FACE_NO_BOX)
7019 /* If the box comes from face properties in a
7020 display string, check faces in that string. */
7021 int string_face_id = face_after_it_pos (it);
7022 it->end_of_box_run_p
7023 = (FACE_FROM_ID (it->f, string_face_id)->box
7024 == FACE_NO_BOX);
7026 /* Otherwise, the box comes from the underlying face.
7027 If this is the last string character displayed, check
7028 the next buffer location. */
7029 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7030 && (it->current.overlay_string_index
7031 == it->n_overlay_strings - 1))
7033 ptrdiff_t ignore;
7034 int next_face_id;
7035 struct text_pos pos = it->current.pos;
7036 INC_TEXT_POS (pos, it->multibyte_p);
7038 next_face_id = face_at_buffer_position
7039 (it->w, CHARPOS (pos), it->region_beg_charpos,
7040 it->region_end_charpos, &ignore,
7041 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7042 -1);
7043 it->end_of_box_run_p
7044 = (FACE_FROM_ID (it->f, next_face_id)->box
7045 == FACE_NO_BOX);
7049 else
7051 int face_id = face_after_it_pos (it);
7052 it->end_of_box_run_p
7053 = (face_id != it->face_id
7054 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7057 /* If we reached the end of the object we've been iterating (e.g., a
7058 display string or an overlay string), and there's something on
7059 IT->stack, proceed with what's on the stack. It doesn't make
7060 sense to return zero if there's unprocessed stuff on the stack,
7061 because otherwise that stuff will never be displayed. */
7062 if (!success_p && it->sp > 0)
7064 set_iterator_to_next (it, 0);
7065 success_p = get_next_display_element (it);
7068 /* Value is 0 if end of buffer or string reached. */
7069 return success_p;
7073 /* Move IT to the next display element.
7075 RESEAT_P non-zero means if called on a newline in buffer text,
7076 skip to the next visible line start.
7078 Functions get_next_display_element and set_iterator_to_next are
7079 separate because I find this arrangement easier to handle than a
7080 get_next_display_element function that also increments IT's
7081 position. The way it is we can first look at an iterator's current
7082 display element, decide whether it fits on a line, and if it does,
7083 increment the iterator position. The other way around we probably
7084 would either need a flag indicating whether the iterator has to be
7085 incremented the next time, or we would have to implement a
7086 decrement position function which would not be easy to write. */
7088 void
7089 set_iterator_to_next (struct it *it, int reseat_p)
7091 /* Reset flags indicating start and end of a sequence of characters
7092 with box. Reset them at the start of this function because
7093 moving the iterator to a new position might set them. */
7094 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7096 switch (it->method)
7098 case GET_FROM_BUFFER:
7099 /* The current display element of IT is a character from
7100 current_buffer. Advance in the buffer, and maybe skip over
7101 invisible lines that are so because of selective display. */
7102 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7103 reseat_at_next_visible_line_start (it, 0);
7104 else if (it->cmp_it.id >= 0)
7106 /* We are currently getting glyphs from a composition. */
7107 int i;
7109 if (! it->bidi_p)
7111 IT_CHARPOS (*it) += it->cmp_it.nchars;
7112 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7113 if (it->cmp_it.to < it->cmp_it.nglyphs)
7115 it->cmp_it.from = it->cmp_it.to;
7117 else
7119 it->cmp_it.id = -1;
7120 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7121 IT_BYTEPOS (*it),
7122 it->end_charpos, Qnil);
7125 else if (! it->cmp_it.reversed_p)
7127 /* Composition created while scanning forward. */
7128 /* Update IT's char/byte positions to point to the first
7129 character of the next grapheme cluster, or to the
7130 character visually after the current composition. */
7131 for (i = 0; i < it->cmp_it.nchars; i++)
7132 bidi_move_to_visually_next (&it->bidi_it);
7133 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7134 IT_CHARPOS (*it) = it->bidi_it.charpos;
7136 if (it->cmp_it.to < it->cmp_it.nglyphs)
7138 /* Proceed to the next grapheme cluster. */
7139 it->cmp_it.from = it->cmp_it.to;
7141 else
7143 /* No more grapheme clusters in this composition.
7144 Find the next stop position. */
7145 ptrdiff_t stop = it->end_charpos;
7146 if (it->bidi_it.scan_dir < 0)
7147 /* Now we are scanning backward and don't know
7148 where to stop. */
7149 stop = -1;
7150 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7151 IT_BYTEPOS (*it), stop, Qnil);
7154 else
7156 /* Composition created while scanning backward. */
7157 /* Update IT's char/byte positions to point to the last
7158 character of the previous grapheme cluster, or the
7159 character visually after the current composition. */
7160 for (i = 0; i < it->cmp_it.nchars; i++)
7161 bidi_move_to_visually_next (&it->bidi_it);
7162 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7163 IT_CHARPOS (*it) = it->bidi_it.charpos;
7164 if (it->cmp_it.from > 0)
7166 /* Proceed to the previous grapheme cluster. */
7167 it->cmp_it.to = it->cmp_it.from;
7169 else
7171 /* No more grapheme clusters in this composition.
7172 Find the next stop position. */
7173 ptrdiff_t stop = it->end_charpos;
7174 if (it->bidi_it.scan_dir < 0)
7175 /* Now we are scanning backward and don't know
7176 where to stop. */
7177 stop = -1;
7178 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7179 IT_BYTEPOS (*it), stop, Qnil);
7183 else
7185 eassert (it->len != 0);
7187 if (!it->bidi_p)
7189 IT_BYTEPOS (*it) += it->len;
7190 IT_CHARPOS (*it) += 1;
7192 else
7194 int prev_scan_dir = it->bidi_it.scan_dir;
7195 /* If this is a new paragraph, determine its base
7196 direction (a.k.a. its base embedding level). */
7197 if (it->bidi_it.new_paragraph)
7198 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7199 bidi_move_to_visually_next (&it->bidi_it);
7200 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7201 IT_CHARPOS (*it) = it->bidi_it.charpos;
7202 if (prev_scan_dir != it->bidi_it.scan_dir)
7204 /* As the scan direction was changed, we must
7205 re-compute the stop position for composition. */
7206 ptrdiff_t stop = it->end_charpos;
7207 if (it->bidi_it.scan_dir < 0)
7208 stop = -1;
7209 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7210 IT_BYTEPOS (*it), stop, Qnil);
7213 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7215 break;
7217 case GET_FROM_C_STRING:
7218 /* Current display element of IT is from a C string. */
7219 if (!it->bidi_p
7220 /* If the string position is beyond string's end, it means
7221 next_element_from_c_string is padding the string with
7222 blanks, in which case we bypass the bidi iterator,
7223 because it cannot deal with such virtual characters. */
7224 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7226 IT_BYTEPOS (*it) += it->len;
7227 IT_CHARPOS (*it) += 1;
7229 else
7231 bidi_move_to_visually_next (&it->bidi_it);
7232 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7233 IT_CHARPOS (*it) = it->bidi_it.charpos;
7235 break;
7237 case GET_FROM_DISPLAY_VECTOR:
7238 /* Current display element of IT is from a display table entry.
7239 Advance in the display table definition. Reset it to null if
7240 end reached, and continue with characters from buffers/
7241 strings. */
7242 ++it->current.dpvec_index;
7244 /* Restore face of the iterator to what they were before the
7245 display vector entry (these entries may contain faces). */
7246 it->face_id = it->saved_face_id;
7248 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7250 int recheck_faces = it->ellipsis_p;
7252 if (it->s)
7253 it->method = GET_FROM_C_STRING;
7254 else if (STRINGP (it->string))
7255 it->method = GET_FROM_STRING;
7256 else
7258 it->method = GET_FROM_BUFFER;
7259 it->object = it->w->contents;
7262 it->dpvec = NULL;
7263 it->current.dpvec_index = -1;
7265 /* Skip over characters which were displayed via IT->dpvec. */
7266 if (it->dpvec_char_len < 0)
7267 reseat_at_next_visible_line_start (it, 1);
7268 else if (it->dpvec_char_len > 0)
7270 if (it->method == GET_FROM_STRING
7271 && it->current.overlay_string_index >= 0
7272 && it->n_overlay_strings > 0)
7273 it->ignore_overlay_strings_at_pos_p = 1;
7274 it->len = it->dpvec_char_len;
7275 set_iterator_to_next (it, reseat_p);
7278 /* Maybe recheck faces after display vector */
7279 if (recheck_faces)
7280 it->stop_charpos = IT_CHARPOS (*it);
7282 break;
7284 case GET_FROM_STRING:
7285 /* Current display element is a character from a Lisp string. */
7286 eassert (it->s == NULL && STRINGP (it->string));
7287 /* Don't advance past string end. These conditions are true
7288 when set_iterator_to_next is called at the end of
7289 get_next_display_element, in which case the Lisp string is
7290 already exhausted, and all we want is pop the iterator
7291 stack. */
7292 if (it->current.overlay_string_index >= 0)
7294 /* This is an overlay string, so there's no padding with
7295 spaces, and the number of characters in the string is
7296 where the string ends. */
7297 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7298 goto consider_string_end;
7300 else
7302 /* Not an overlay string. There could be padding, so test
7303 against it->end_charpos . */
7304 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7305 goto consider_string_end;
7307 if (it->cmp_it.id >= 0)
7309 int i;
7311 if (! it->bidi_p)
7313 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7314 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7315 if (it->cmp_it.to < it->cmp_it.nglyphs)
7316 it->cmp_it.from = it->cmp_it.to;
7317 else
7319 it->cmp_it.id = -1;
7320 composition_compute_stop_pos (&it->cmp_it,
7321 IT_STRING_CHARPOS (*it),
7322 IT_STRING_BYTEPOS (*it),
7323 it->end_charpos, it->string);
7326 else if (! it->cmp_it.reversed_p)
7328 for (i = 0; i < it->cmp_it.nchars; i++)
7329 bidi_move_to_visually_next (&it->bidi_it);
7330 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7331 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7333 if (it->cmp_it.to < it->cmp_it.nglyphs)
7334 it->cmp_it.from = it->cmp_it.to;
7335 else
7337 ptrdiff_t stop = it->end_charpos;
7338 if (it->bidi_it.scan_dir < 0)
7339 stop = -1;
7340 composition_compute_stop_pos (&it->cmp_it,
7341 IT_STRING_CHARPOS (*it),
7342 IT_STRING_BYTEPOS (*it), stop,
7343 it->string);
7346 else
7348 for (i = 0; i < it->cmp_it.nchars; i++)
7349 bidi_move_to_visually_next (&it->bidi_it);
7350 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7351 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7352 if (it->cmp_it.from > 0)
7353 it->cmp_it.to = it->cmp_it.from;
7354 else
7356 ptrdiff_t stop = it->end_charpos;
7357 if (it->bidi_it.scan_dir < 0)
7358 stop = -1;
7359 composition_compute_stop_pos (&it->cmp_it,
7360 IT_STRING_CHARPOS (*it),
7361 IT_STRING_BYTEPOS (*it), stop,
7362 it->string);
7366 else
7368 if (!it->bidi_p
7369 /* If the string position is beyond string's end, it
7370 means next_element_from_string is padding the string
7371 with blanks, in which case we bypass the bidi
7372 iterator, because it cannot deal with such virtual
7373 characters. */
7374 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7376 IT_STRING_BYTEPOS (*it) += it->len;
7377 IT_STRING_CHARPOS (*it) += 1;
7379 else
7381 int prev_scan_dir = it->bidi_it.scan_dir;
7383 bidi_move_to_visually_next (&it->bidi_it);
7384 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7385 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7386 if (prev_scan_dir != it->bidi_it.scan_dir)
7388 ptrdiff_t stop = it->end_charpos;
7390 if (it->bidi_it.scan_dir < 0)
7391 stop = -1;
7392 composition_compute_stop_pos (&it->cmp_it,
7393 IT_STRING_CHARPOS (*it),
7394 IT_STRING_BYTEPOS (*it), stop,
7395 it->string);
7400 consider_string_end:
7402 if (it->current.overlay_string_index >= 0)
7404 /* IT->string is an overlay string. Advance to the
7405 next, if there is one. */
7406 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7408 it->ellipsis_p = 0;
7409 next_overlay_string (it);
7410 if (it->ellipsis_p)
7411 setup_for_ellipsis (it, 0);
7414 else
7416 /* IT->string is not an overlay string. If we reached
7417 its end, and there is something on IT->stack, proceed
7418 with what is on the stack. This can be either another
7419 string, this time an overlay string, or a buffer. */
7420 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7421 && it->sp > 0)
7423 pop_it (it);
7424 if (it->method == GET_FROM_STRING)
7425 goto consider_string_end;
7428 break;
7430 case GET_FROM_IMAGE:
7431 case GET_FROM_STRETCH:
7432 /* The position etc with which we have to proceed are on
7433 the stack. The position may be at the end of a string,
7434 if the `display' property takes up the whole string. */
7435 eassert (it->sp > 0);
7436 pop_it (it);
7437 if (it->method == GET_FROM_STRING)
7438 goto consider_string_end;
7439 break;
7441 default:
7442 /* There are no other methods defined, so this should be a bug. */
7443 emacs_abort ();
7446 eassert (it->method != GET_FROM_STRING
7447 || (STRINGP (it->string)
7448 && IT_STRING_CHARPOS (*it) >= 0));
7451 /* Load IT's display element fields with information about the next
7452 display element which comes from a display table entry or from the
7453 result of translating a control character to one of the forms `^C'
7454 or `\003'.
7456 IT->dpvec holds the glyphs to return as characters.
7457 IT->saved_face_id holds the face id before the display vector--it
7458 is restored into IT->face_id in set_iterator_to_next. */
7460 static int
7461 next_element_from_display_vector (struct it *it)
7463 Lisp_Object gc;
7464 int prev_face_id = it->face_id;
7465 int next_face_id;
7467 /* Precondition. */
7468 eassert (it->dpvec && it->current.dpvec_index >= 0);
7470 it->face_id = it->saved_face_id;
7472 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7473 That seemed totally bogus - so I changed it... */
7474 gc = it->dpvec[it->current.dpvec_index];
7476 if (GLYPH_CODE_P (gc))
7478 struct face *this_face, *prev_face, *next_face;
7480 it->c = GLYPH_CODE_CHAR (gc);
7481 it->len = CHAR_BYTES (it->c);
7483 /* The entry may contain a face id to use. Such a face id is
7484 the id of a Lisp face, not a realized face. A face id of
7485 zero means no face is specified. */
7486 if (it->dpvec_face_id >= 0)
7487 it->face_id = it->dpvec_face_id;
7488 else
7490 int lface_id = GLYPH_CODE_FACE (gc);
7491 if (lface_id > 0)
7492 it->face_id = merge_faces (it->f, Qt, lface_id,
7493 it->saved_face_id);
7496 /* Glyphs in the display vector could have the box face, so we
7497 need to set the related flags in the iterator, as
7498 appropriate. */
7499 this_face = FACE_FROM_ID (it->f, it->face_id);
7500 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7502 /* Is this character the first character of a box-face run? */
7503 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7504 && (!prev_face
7505 || prev_face->box == FACE_NO_BOX));
7507 /* For the last character of the box-face run, we need to look
7508 either at the next glyph from the display vector, or at the
7509 face we saw before the display vector. */
7510 next_face_id = it->saved_face_id;
7511 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7513 if (it->dpvec_face_id >= 0)
7514 next_face_id = it->dpvec_face_id;
7515 else
7517 int lface_id =
7518 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7520 if (lface_id > 0)
7521 next_face_id = merge_faces (it->f, Qt, lface_id,
7522 it->saved_face_id);
7525 next_face = FACE_FROM_ID (it->f, next_face_id);
7526 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7527 && (!next_face
7528 || next_face->box == FACE_NO_BOX));
7529 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7531 else
7532 /* Display table entry is invalid. Return a space. */
7533 it->c = ' ', it->len = 1;
7535 /* Don't change position and object of the iterator here. They are
7536 still the values of the character that had this display table
7537 entry or was translated, and that's what we want. */
7538 it->what = IT_CHARACTER;
7539 return 1;
7542 /* Get the first element of string/buffer in the visual order, after
7543 being reseated to a new position in a string or a buffer. */
7544 static void
7545 get_visually_first_element (struct it *it)
7547 int string_p = STRINGP (it->string) || it->s;
7548 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7549 ptrdiff_t bob = (string_p ? 0 : BEGV);
7551 if (STRINGP (it->string))
7553 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7554 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7556 else
7558 it->bidi_it.charpos = IT_CHARPOS (*it);
7559 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7562 if (it->bidi_it.charpos == eob)
7564 /* Nothing to do, but reset the FIRST_ELT flag, like
7565 bidi_paragraph_init does, because we are not going to
7566 call it. */
7567 it->bidi_it.first_elt = 0;
7569 else if (it->bidi_it.charpos == bob
7570 || (!string_p
7571 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7572 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7574 /* If we are at the beginning of a line/string, we can produce
7575 the next element right away. */
7576 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7577 bidi_move_to_visually_next (&it->bidi_it);
7579 else
7581 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7583 /* We need to prime the bidi iterator starting at the line's or
7584 string's beginning, before we will be able to produce the
7585 next element. */
7586 if (string_p)
7587 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7588 else
7589 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7590 IT_BYTEPOS (*it), -1,
7591 &it->bidi_it.bytepos);
7592 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7595 /* Now return to buffer/string position where we were asked
7596 to get the next display element, and produce that. */
7597 bidi_move_to_visually_next (&it->bidi_it);
7599 while (it->bidi_it.bytepos != orig_bytepos
7600 && it->bidi_it.charpos < eob);
7603 /* Adjust IT's position information to where we ended up. */
7604 if (STRINGP (it->string))
7606 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7607 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7609 else
7611 IT_CHARPOS (*it) = it->bidi_it.charpos;
7612 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7615 if (STRINGP (it->string) || !it->s)
7617 ptrdiff_t stop, charpos, bytepos;
7619 if (STRINGP (it->string))
7621 eassert (!it->s);
7622 stop = SCHARS (it->string);
7623 if (stop > it->end_charpos)
7624 stop = it->end_charpos;
7625 charpos = IT_STRING_CHARPOS (*it);
7626 bytepos = IT_STRING_BYTEPOS (*it);
7628 else
7630 stop = it->end_charpos;
7631 charpos = IT_CHARPOS (*it);
7632 bytepos = IT_BYTEPOS (*it);
7634 if (it->bidi_it.scan_dir < 0)
7635 stop = -1;
7636 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7637 it->string);
7641 /* Load IT with the next display element from Lisp string IT->string.
7642 IT->current.string_pos is the current position within the string.
7643 If IT->current.overlay_string_index >= 0, the Lisp string is an
7644 overlay string. */
7646 static int
7647 next_element_from_string (struct it *it)
7649 struct text_pos position;
7651 eassert (STRINGP (it->string));
7652 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7653 eassert (IT_STRING_CHARPOS (*it) >= 0);
7654 position = it->current.string_pos;
7656 /* With bidi reordering, the character to display might not be the
7657 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7658 that we were reseat()ed to a new string, whose paragraph
7659 direction is not known. */
7660 if (it->bidi_p && it->bidi_it.first_elt)
7662 get_visually_first_element (it);
7663 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7666 /* Time to check for invisible text? */
7667 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7669 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7671 if (!(!it->bidi_p
7672 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7673 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7675 /* With bidi non-linear iteration, we could find
7676 ourselves far beyond the last computed stop_charpos,
7677 with several other stop positions in between that we
7678 missed. Scan them all now, in buffer's logical
7679 order, until we find and handle the last stop_charpos
7680 that precedes our current position. */
7681 handle_stop_backwards (it, it->stop_charpos);
7682 return GET_NEXT_DISPLAY_ELEMENT (it);
7684 else
7686 if (it->bidi_p)
7688 /* Take note of the stop position we just moved
7689 across, for when we will move back across it. */
7690 it->prev_stop = it->stop_charpos;
7691 /* If we are at base paragraph embedding level, take
7692 note of the last stop position seen at this
7693 level. */
7694 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7695 it->base_level_stop = it->stop_charpos;
7697 handle_stop (it);
7699 /* Since a handler may have changed IT->method, we must
7700 recurse here. */
7701 return GET_NEXT_DISPLAY_ELEMENT (it);
7704 else if (it->bidi_p
7705 /* If we are before prev_stop, we may have overstepped
7706 on our way backwards a stop_pos, and if so, we need
7707 to handle that stop_pos. */
7708 && IT_STRING_CHARPOS (*it) < it->prev_stop
7709 /* We can sometimes back up for reasons that have nothing
7710 to do with bidi reordering. E.g., compositions. The
7711 code below is only needed when we are above the base
7712 embedding level, so test for that explicitly. */
7713 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7715 /* If we lost track of base_level_stop, we have no better
7716 place for handle_stop_backwards to start from than string
7717 beginning. This happens, e.g., when we were reseated to
7718 the previous screenful of text by vertical-motion. */
7719 if (it->base_level_stop <= 0
7720 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7721 it->base_level_stop = 0;
7722 handle_stop_backwards (it, it->base_level_stop);
7723 return GET_NEXT_DISPLAY_ELEMENT (it);
7727 if (it->current.overlay_string_index >= 0)
7729 /* Get the next character from an overlay string. In overlay
7730 strings, there is no field width or padding with spaces to
7731 do. */
7732 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7734 it->what = IT_EOB;
7735 return 0;
7737 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7738 IT_STRING_BYTEPOS (*it),
7739 it->bidi_it.scan_dir < 0
7740 ? -1
7741 : SCHARS (it->string))
7742 && next_element_from_composition (it))
7744 return 1;
7746 else if (STRING_MULTIBYTE (it->string))
7748 const unsigned char *s = (SDATA (it->string)
7749 + IT_STRING_BYTEPOS (*it));
7750 it->c = string_char_and_length (s, &it->len);
7752 else
7754 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7755 it->len = 1;
7758 else
7760 /* Get the next character from a Lisp string that is not an
7761 overlay string. Such strings come from the mode line, for
7762 example. We may have to pad with spaces, or truncate the
7763 string. See also next_element_from_c_string. */
7764 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7766 it->what = IT_EOB;
7767 return 0;
7769 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7771 /* Pad with spaces. */
7772 it->c = ' ', it->len = 1;
7773 CHARPOS (position) = BYTEPOS (position) = -1;
7775 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7776 IT_STRING_BYTEPOS (*it),
7777 it->bidi_it.scan_dir < 0
7778 ? -1
7779 : it->string_nchars)
7780 && next_element_from_composition (it))
7782 return 1;
7784 else if (STRING_MULTIBYTE (it->string))
7786 const unsigned char *s = (SDATA (it->string)
7787 + IT_STRING_BYTEPOS (*it));
7788 it->c = string_char_and_length (s, &it->len);
7790 else
7792 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7793 it->len = 1;
7797 /* Record what we have and where it came from. */
7798 it->what = IT_CHARACTER;
7799 it->object = it->string;
7800 it->position = position;
7801 return 1;
7805 /* Load IT with next display element from C string IT->s.
7806 IT->string_nchars is the maximum number of characters to return
7807 from the string. IT->end_charpos may be greater than
7808 IT->string_nchars when this function is called, in which case we
7809 may have to return padding spaces. Value is zero if end of string
7810 reached, including padding spaces. */
7812 static int
7813 next_element_from_c_string (struct it *it)
7815 int success_p = 1;
7817 eassert (it->s);
7818 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7819 it->what = IT_CHARACTER;
7820 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7821 it->object = Qnil;
7823 /* With bidi reordering, the character to display might not be the
7824 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7825 we were reseated to a new string, whose paragraph direction is
7826 not known. */
7827 if (it->bidi_p && it->bidi_it.first_elt)
7828 get_visually_first_element (it);
7830 /* IT's position can be greater than IT->string_nchars in case a
7831 field width or precision has been specified when the iterator was
7832 initialized. */
7833 if (IT_CHARPOS (*it) >= it->end_charpos)
7835 /* End of the game. */
7836 it->what = IT_EOB;
7837 success_p = 0;
7839 else if (IT_CHARPOS (*it) >= it->string_nchars)
7841 /* Pad with spaces. */
7842 it->c = ' ', it->len = 1;
7843 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7845 else if (it->multibyte_p)
7846 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7847 else
7848 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7850 return success_p;
7854 /* Set up IT to return characters from an ellipsis, if appropriate.
7855 The definition of the ellipsis glyphs may come from a display table
7856 entry. This function fills IT with the first glyph from the
7857 ellipsis if an ellipsis is to be displayed. */
7859 static int
7860 next_element_from_ellipsis (struct it *it)
7862 if (it->selective_display_ellipsis_p)
7863 setup_for_ellipsis (it, it->len);
7864 else
7866 /* The face at the current position may be different from the
7867 face we find after the invisible text. Remember what it
7868 was in IT->saved_face_id, and signal that it's there by
7869 setting face_before_selective_p. */
7870 it->saved_face_id = it->face_id;
7871 it->method = GET_FROM_BUFFER;
7872 it->object = it->w->contents;
7873 reseat_at_next_visible_line_start (it, 1);
7874 it->face_before_selective_p = 1;
7877 return GET_NEXT_DISPLAY_ELEMENT (it);
7881 /* Deliver an image display element. The iterator IT is already
7882 filled with image information (done in handle_display_prop). Value
7883 is always 1. */
7886 static int
7887 next_element_from_image (struct it *it)
7889 it->what = IT_IMAGE;
7890 it->ignore_overlay_strings_at_pos_p = 0;
7891 return 1;
7895 /* Fill iterator IT with next display element from a stretch glyph
7896 property. IT->object is the value of the text property. Value is
7897 always 1. */
7899 static int
7900 next_element_from_stretch (struct it *it)
7902 it->what = IT_STRETCH;
7903 return 1;
7906 /* Scan backwards from IT's current position until we find a stop
7907 position, or until BEGV. This is called when we find ourself
7908 before both the last known prev_stop and base_level_stop while
7909 reordering bidirectional text. */
7911 static void
7912 compute_stop_pos_backwards (struct it *it)
7914 const int SCAN_BACK_LIMIT = 1000;
7915 struct text_pos pos;
7916 struct display_pos save_current = it->current;
7917 struct text_pos save_position = it->position;
7918 ptrdiff_t charpos = IT_CHARPOS (*it);
7919 ptrdiff_t where_we_are = charpos;
7920 ptrdiff_t save_stop_pos = it->stop_charpos;
7921 ptrdiff_t save_end_pos = it->end_charpos;
7923 eassert (NILP (it->string) && !it->s);
7924 eassert (it->bidi_p);
7925 it->bidi_p = 0;
7928 it->end_charpos = min (charpos + 1, ZV);
7929 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7930 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7931 reseat_1 (it, pos, 0);
7932 compute_stop_pos (it);
7933 /* We must advance forward, right? */
7934 if (it->stop_charpos <= charpos)
7935 emacs_abort ();
7937 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7939 if (it->stop_charpos <= where_we_are)
7940 it->prev_stop = it->stop_charpos;
7941 else
7942 it->prev_stop = BEGV;
7943 it->bidi_p = 1;
7944 it->current = save_current;
7945 it->position = save_position;
7946 it->stop_charpos = save_stop_pos;
7947 it->end_charpos = save_end_pos;
7950 /* Scan forward from CHARPOS in the current buffer/string, until we
7951 find a stop position > current IT's position. Then handle the stop
7952 position before that. This is called when we bump into a stop
7953 position while reordering bidirectional text. CHARPOS should be
7954 the last previously processed stop_pos (or BEGV/0, if none were
7955 processed yet) whose position is less that IT's current
7956 position. */
7958 static void
7959 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7961 int bufp = !STRINGP (it->string);
7962 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7963 struct display_pos save_current = it->current;
7964 struct text_pos save_position = it->position;
7965 struct text_pos pos1;
7966 ptrdiff_t next_stop;
7968 /* Scan in strict logical order. */
7969 eassert (it->bidi_p);
7970 it->bidi_p = 0;
7973 it->prev_stop = charpos;
7974 if (bufp)
7976 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7977 reseat_1 (it, pos1, 0);
7979 else
7980 it->current.string_pos = string_pos (charpos, it->string);
7981 compute_stop_pos (it);
7982 /* We must advance forward, right? */
7983 if (it->stop_charpos <= it->prev_stop)
7984 emacs_abort ();
7985 charpos = it->stop_charpos;
7987 while (charpos <= where_we_are);
7989 it->bidi_p = 1;
7990 it->current = save_current;
7991 it->position = save_position;
7992 next_stop = it->stop_charpos;
7993 it->stop_charpos = it->prev_stop;
7994 handle_stop (it);
7995 it->stop_charpos = next_stop;
7998 /* Load IT with the next display element from current_buffer. Value
7999 is zero if end of buffer reached. IT->stop_charpos is the next
8000 position at which to stop and check for text properties or buffer
8001 end. */
8003 static int
8004 next_element_from_buffer (struct it *it)
8006 int success_p = 1;
8008 eassert (IT_CHARPOS (*it) >= BEGV);
8009 eassert (NILP (it->string) && !it->s);
8010 eassert (!it->bidi_p
8011 || (EQ (it->bidi_it.string.lstring, Qnil)
8012 && it->bidi_it.string.s == NULL));
8014 /* With bidi reordering, the character to display might not be the
8015 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8016 we were reseat()ed to a new buffer position, which is potentially
8017 a different paragraph. */
8018 if (it->bidi_p && it->bidi_it.first_elt)
8020 get_visually_first_element (it);
8021 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8024 if (IT_CHARPOS (*it) >= it->stop_charpos)
8026 if (IT_CHARPOS (*it) >= it->end_charpos)
8028 int overlay_strings_follow_p;
8030 /* End of the game, except when overlay strings follow that
8031 haven't been returned yet. */
8032 if (it->overlay_strings_at_end_processed_p)
8033 overlay_strings_follow_p = 0;
8034 else
8036 it->overlay_strings_at_end_processed_p = 1;
8037 overlay_strings_follow_p = get_overlay_strings (it, 0);
8040 if (overlay_strings_follow_p)
8041 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8042 else
8044 it->what = IT_EOB;
8045 it->position = it->current.pos;
8046 success_p = 0;
8049 else if (!(!it->bidi_p
8050 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8051 || IT_CHARPOS (*it) == it->stop_charpos))
8053 /* With bidi non-linear iteration, we could find ourselves
8054 far beyond the last computed stop_charpos, with several
8055 other stop positions in between that we missed. Scan
8056 them all now, in buffer's logical order, until we find
8057 and handle the last stop_charpos that precedes our
8058 current position. */
8059 handle_stop_backwards (it, it->stop_charpos);
8060 return GET_NEXT_DISPLAY_ELEMENT (it);
8062 else
8064 if (it->bidi_p)
8066 /* Take note of the stop position we just moved across,
8067 for when we will move back across it. */
8068 it->prev_stop = it->stop_charpos;
8069 /* If we are at base paragraph embedding level, take
8070 note of the last stop position seen at this
8071 level. */
8072 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8073 it->base_level_stop = it->stop_charpos;
8075 handle_stop (it);
8076 return GET_NEXT_DISPLAY_ELEMENT (it);
8079 else if (it->bidi_p
8080 /* If we are before prev_stop, we may have overstepped on
8081 our way backwards a stop_pos, and if so, we need to
8082 handle that stop_pos. */
8083 && IT_CHARPOS (*it) < it->prev_stop
8084 /* We can sometimes back up for reasons that have nothing
8085 to do with bidi reordering. E.g., compositions. The
8086 code below is only needed when we are above the base
8087 embedding level, so test for that explicitly. */
8088 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8090 if (it->base_level_stop <= 0
8091 || IT_CHARPOS (*it) < it->base_level_stop)
8093 /* If we lost track of base_level_stop, we need to find
8094 prev_stop by looking backwards. This happens, e.g., when
8095 we were reseated to the previous screenful of text by
8096 vertical-motion. */
8097 it->base_level_stop = BEGV;
8098 compute_stop_pos_backwards (it);
8099 handle_stop_backwards (it, it->prev_stop);
8101 else
8102 handle_stop_backwards (it, it->base_level_stop);
8103 return GET_NEXT_DISPLAY_ELEMENT (it);
8105 else
8107 /* No face changes, overlays etc. in sight, so just return a
8108 character from current_buffer. */
8109 unsigned char *p;
8110 ptrdiff_t stop;
8112 /* Maybe run the redisplay end trigger hook. Performance note:
8113 This doesn't seem to cost measurable time. */
8114 if (it->redisplay_end_trigger_charpos
8115 && it->glyph_row
8116 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8117 run_redisplay_end_trigger_hook (it);
8119 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8120 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8121 stop)
8122 && next_element_from_composition (it))
8124 return 1;
8127 /* Get the next character, maybe multibyte. */
8128 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8129 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8130 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8131 else
8132 it->c = *p, it->len = 1;
8134 /* Record what we have and where it came from. */
8135 it->what = IT_CHARACTER;
8136 it->object = it->w->contents;
8137 it->position = it->current.pos;
8139 /* Normally we return the character found above, except when we
8140 really want to return an ellipsis for selective display. */
8141 if (it->selective)
8143 if (it->c == '\n')
8145 /* A value of selective > 0 means hide lines indented more
8146 than that number of columns. */
8147 if (it->selective > 0
8148 && IT_CHARPOS (*it) + 1 < ZV
8149 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8150 IT_BYTEPOS (*it) + 1,
8151 it->selective))
8153 success_p = next_element_from_ellipsis (it);
8154 it->dpvec_char_len = -1;
8157 else if (it->c == '\r' && it->selective == -1)
8159 /* A value of selective == -1 means that everything from the
8160 CR to the end of the line is invisible, with maybe an
8161 ellipsis displayed for it. */
8162 success_p = next_element_from_ellipsis (it);
8163 it->dpvec_char_len = -1;
8168 /* Value is zero if end of buffer reached. */
8169 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8170 return success_p;
8174 /* Run the redisplay end trigger hook for IT. */
8176 static void
8177 run_redisplay_end_trigger_hook (struct it *it)
8179 Lisp_Object args[3];
8181 /* IT->glyph_row should be non-null, i.e. we should be actually
8182 displaying something, or otherwise we should not run the hook. */
8183 eassert (it->glyph_row);
8185 /* Set up hook arguments. */
8186 args[0] = Qredisplay_end_trigger_functions;
8187 args[1] = it->window;
8188 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8189 it->redisplay_end_trigger_charpos = 0;
8191 /* Since we are *trying* to run these functions, don't try to run
8192 them again, even if they get an error. */
8193 wset_redisplay_end_trigger (it->w, Qnil);
8194 Frun_hook_with_args (3, args);
8196 /* Notice if it changed the face of the character we are on. */
8197 handle_face_prop (it);
8201 /* Deliver a composition display element. Unlike the other
8202 next_element_from_XXX, this function is not registered in the array
8203 get_next_element[]. It is called from next_element_from_buffer and
8204 next_element_from_string when necessary. */
8206 static int
8207 next_element_from_composition (struct it *it)
8209 it->what = IT_COMPOSITION;
8210 it->len = it->cmp_it.nbytes;
8211 if (STRINGP (it->string))
8213 if (it->c < 0)
8215 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8216 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8217 return 0;
8219 it->position = it->current.string_pos;
8220 it->object = it->string;
8221 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8222 IT_STRING_BYTEPOS (*it), it->string);
8224 else
8226 if (it->c < 0)
8228 IT_CHARPOS (*it) += it->cmp_it.nchars;
8229 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8230 if (it->bidi_p)
8232 if (it->bidi_it.new_paragraph)
8233 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8234 /* Resync the bidi iterator with IT's new position.
8235 FIXME: this doesn't support bidirectional text. */
8236 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8237 bidi_move_to_visually_next (&it->bidi_it);
8239 return 0;
8241 it->position = it->current.pos;
8242 it->object = it->w->contents;
8243 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8244 IT_BYTEPOS (*it), Qnil);
8246 return 1;
8251 /***********************************************************************
8252 Moving an iterator without producing glyphs
8253 ***********************************************************************/
8255 /* Check if iterator is at a position corresponding to a valid buffer
8256 position after some move_it_ call. */
8258 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8259 ((it)->method == GET_FROM_STRING \
8260 ? IT_STRING_CHARPOS (*it) == 0 \
8261 : 1)
8264 /* Move iterator IT to a specified buffer or X position within one
8265 line on the display without producing glyphs.
8267 OP should be a bit mask including some or all of these bits:
8268 MOVE_TO_X: Stop upon reaching x-position TO_X.
8269 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8270 Regardless of OP's value, stop upon reaching the end of the display line.
8272 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8273 This means, in particular, that TO_X includes window's horizontal
8274 scroll amount.
8276 The return value has several possible values that
8277 say what condition caused the scan to stop:
8279 MOVE_POS_MATCH_OR_ZV
8280 - when TO_POS or ZV was reached.
8282 MOVE_X_REACHED
8283 -when TO_X was reached before TO_POS or ZV were reached.
8285 MOVE_LINE_CONTINUED
8286 - when we reached the end of the display area and the line must
8287 be continued.
8289 MOVE_LINE_TRUNCATED
8290 - when we reached the end of the display area and the line is
8291 truncated.
8293 MOVE_NEWLINE_OR_CR
8294 - when we stopped at a line end, i.e. a newline or a CR and selective
8295 display is on. */
8297 static enum move_it_result
8298 move_it_in_display_line_to (struct it *it,
8299 ptrdiff_t to_charpos, int to_x,
8300 enum move_operation_enum op)
8302 enum move_it_result result = MOVE_UNDEFINED;
8303 struct glyph_row *saved_glyph_row;
8304 struct it wrap_it, atpos_it, atx_it, ppos_it;
8305 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8306 void *ppos_data = NULL;
8307 int may_wrap = 0;
8308 enum it_method prev_method = it->method;
8309 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8310 int saw_smaller_pos = prev_pos < to_charpos;
8312 /* Don't produce glyphs in produce_glyphs. */
8313 saved_glyph_row = it->glyph_row;
8314 it->glyph_row = NULL;
8316 /* Use wrap_it to save a copy of IT wherever a word wrap could
8317 occur. Use atpos_it to save a copy of IT at the desired buffer
8318 position, if found, so that we can scan ahead and check if the
8319 word later overshoots the window edge. Use atx_it similarly, for
8320 pixel positions. */
8321 wrap_it.sp = -1;
8322 atpos_it.sp = -1;
8323 atx_it.sp = -1;
8325 /* Use ppos_it under bidi reordering to save a copy of IT for the
8326 position > CHARPOS that is the closest to CHARPOS. We restore
8327 that position in IT when we have scanned the entire display line
8328 without finding a match for CHARPOS and all the character
8329 positions are greater than CHARPOS. */
8330 if (it->bidi_p)
8332 SAVE_IT (ppos_it, *it, ppos_data);
8333 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8334 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8335 SAVE_IT (ppos_it, *it, ppos_data);
8338 #define BUFFER_POS_REACHED_P() \
8339 ((op & MOVE_TO_POS) != 0 \
8340 && BUFFERP (it->object) \
8341 && (IT_CHARPOS (*it) == to_charpos \
8342 || ((!it->bidi_p \
8343 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8344 && IT_CHARPOS (*it) > to_charpos) \
8345 || (it->what == IT_COMPOSITION \
8346 && ((IT_CHARPOS (*it) > to_charpos \
8347 && to_charpos >= it->cmp_it.charpos) \
8348 || (IT_CHARPOS (*it) < to_charpos \
8349 && to_charpos <= it->cmp_it.charpos)))) \
8350 && (it->method == GET_FROM_BUFFER \
8351 || (it->method == GET_FROM_DISPLAY_VECTOR \
8352 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8354 /* If there's a line-/wrap-prefix, handle it. */
8355 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8356 && it->current_y < it->last_visible_y)
8357 handle_line_prefix (it);
8359 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8360 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8362 while (1)
8364 int x, i, ascent = 0, descent = 0;
8366 /* Utility macro to reset an iterator with x, ascent, and descent. */
8367 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8368 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8369 (IT)->max_descent = descent)
8371 /* Stop if we move beyond TO_CHARPOS (after an image or a
8372 display string or stretch glyph). */
8373 if ((op & MOVE_TO_POS) != 0
8374 && BUFFERP (it->object)
8375 && it->method == GET_FROM_BUFFER
8376 && (((!it->bidi_p
8377 /* When the iterator is at base embedding level, we
8378 are guaranteed that characters are delivered for
8379 display in strictly increasing order of their
8380 buffer positions. */
8381 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8382 && IT_CHARPOS (*it) > to_charpos)
8383 || (it->bidi_p
8384 && (prev_method == GET_FROM_IMAGE
8385 || prev_method == GET_FROM_STRETCH
8386 || prev_method == GET_FROM_STRING)
8387 /* Passed TO_CHARPOS from left to right. */
8388 && ((prev_pos < to_charpos
8389 && IT_CHARPOS (*it) > to_charpos)
8390 /* Passed TO_CHARPOS from right to left. */
8391 || (prev_pos > to_charpos
8392 && IT_CHARPOS (*it) < to_charpos)))))
8394 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8396 result = MOVE_POS_MATCH_OR_ZV;
8397 break;
8399 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8400 /* If wrap_it is valid, the current position might be in a
8401 word that is wrapped. So, save the iterator in
8402 atpos_it and continue to see if wrapping happens. */
8403 SAVE_IT (atpos_it, *it, atpos_data);
8406 /* Stop when ZV reached.
8407 We used to stop here when TO_CHARPOS reached as well, but that is
8408 too soon if this glyph does not fit on this line. So we handle it
8409 explicitly below. */
8410 if (!get_next_display_element (it))
8412 result = MOVE_POS_MATCH_OR_ZV;
8413 break;
8416 if (it->line_wrap == TRUNCATE)
8418 if (BUFFER_POS_REACHED_P ())
8420 result = MOVE_POS_MATCH_OR_ZV;
8421 break;
8424 else
8426 if (it->line_wrap == WORD_WRAP)
8428 if (IT_DISPLAYING_WHITESPACE (it))
8429 may_wrap = 1;
8430 else if (may_wrap)
8432 /* We have reached a glyph that follows one or more
8433 whitespace characters. If the position is
8434 already found, we are done. */
8435 if (atpos_it.sp >= 0)
8437 RESTORE_IT (it, &atpos_it, atpos_data);
8438 result = MOVE_POS_MATCH_OR_ZV;
8439 goto done;
8441 if (atx_it.sp >= 0)
8443 RESTORE_IT (it, &atx_it, atx_data);
8444 result = MOVE_X_REACHED;
8445 goto done;
8447 /* Otherwise, we can wrap here. */
8448 SAVE_IT (wrap_it, *it, wrap_data);
8449 may_wrap = 0;
8454 /* Remember the line height for the current line, in case
8455 the next element doesn't fit on the line. */
8456 ascent = it->max_ascent;
8457 descent = it->max_descent;
8459 /* The call to produce_glyphs will get the metrics of the
8460 display element IT is loaded with. Record the x-position
8461 before this display element, in case it doesn't fit on the
8462 line. */
8463 x = it->current_x;
8465 PRODUCE_GLYPHS (it);
8467 if (it->area != TEXT_AREA)
8469 prev_method = it->method;
8470 if (it->method == GET_FROM_BUFFER)
8471 prev_pos = IT_CHARPOS (*it);
8472 set_iterator_to_next (it, 1);
8473 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8474 SET_TEXT_POS (this_line_min_pos,
8475 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8476 if (it->bidi_p
8477 && (op & MOVE_TO_POS)
8478 && IT_CHARPOS (*it) > to_charpos
8479 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8480 SAVE_IT (ppos_it, *it, ppos_data);
8481 continue;
8484 /* The number of glyphs we get back in IT->nglyphs will normally
8485 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8486 character on a terminal frame, or (iii) a line end. For the
8487 second case, IT->nglyphs - 1 padding glyphs will be present.
8488 (On X frames, there is only one glyph produced for a
8489 composite character.)
8491 The behavior implemented below means, for continuation lines,
8492 that as many spaces of a TAB as fit on the current line are
8493 displayed there. For terminal frames, as many glyphs of a
8494 multi-glyph character are displayed in the current line, too.
8495 This is what the old redisplay code did, and we keep it that
8496 way. Under X, the whole shape of a complex character must
8497 fit on the line or it will be completely displayed in the
8498 next line.
8500 Note that both for tabs and padding glyphs, all glyphs have
8501 the same width. */
8502 if (it->nglyphs)
8504 /* More than one glyph or glyph doesn't fit on line. All
8505 glyphs have the same width. */
8506 int single_glyph_width = it->pixel_width / it->nglyphs;
8507 int new_x;
8508 int x_before_this_char = x;
8509 int hpos_before_this_char = it->hpos;
8511 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8513 new_x = x + single_glyph_width;
8515 /* We want to leave anything reaching TO_X to the caller. */
8516 if ((op & MOVE_TO_X) && new_x > to_x)
8518 if (BUFFER_POS_REACHED_P ())
8520 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8521 goto buffer_pos_reached;
8522 if (atpos_it.sp < 0)
8524 SAVE_IT (atpos_it, *it, atpos_data);
8525 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8528 else
8530 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8532 it->current_x = x;
8533 result = MOVE_X_REACHED;
8534 break;
8536 if (atx_it.sp < 0)
8538 SAVE_IT (atx_it, *it, atx_data);
8539 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8544 if (/* Lines are continued. */
8545 it->line_wrap != TRUNCATE
8546 && (/* And glyph doesn't fit on the line. */
8547 new_x > it->last_visible_x
8548 /* Or it fits exactly and we're on a window
8549 system frame. */
8550 || (new_x == it->last_visible_x
8551 && FRAME_WINDOW_P (it->f)
8552 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8553 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8554 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8556 if (/* IT->hpos == 0 means the very first glyph
8557 doesn't fit on the line, e.g. a wide image. */
8558 it->hpos == 0
8559 || (new_x == it->last_visible_x
8560 && FRAME_WINDOW_P (it->f)))
8562 ++it->hpos;
8563 it->current_x = new_x;
8565 /* The character's last glyph just barely fits
8566 in this row. */
8567 if (i == it->nglyphs - 1)
8569 /* If this is the destination position,
8570 return a position *before* it in this row,
8571 now that we know it fits in this row. */
8572 if (BUFFER_POS_REACHED_P ())
8574 if (it->line_wrap != WORD_WRAP
8575 || wrap_it.sp < 0)
8577 it->hpos = hpos_before_this_char;
8578 it->current_x = x_before_this_char;
8579 result = MOVE_POS_MATCH_OR_ZV;
8580 break;
8582 if (it->line_wrap == WORD_WRAP
8583 && atpos_it.sp < 0)
8585 SAVE_IT (atpos_it, *it, atpos_data);
8586 atpos_it.current_x = x_before_this_char;
8587 atpos_it.hpos = hpos_before_this_char;
8591 prev_method = it->method;
8592 if (it->method == GET_FROM_BUFFER)
8593 prev_pos = IT_CHARPOS (*it);
8594 set_iterator_to_next (it, 1);
8595 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8596 SET_TEXT_POS (this_line_min_pos,
8597 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8598 /* On graphical terminals, newlines may
8599 "overflow" into the fringe if
8600 overflow-newline-into-fringe is non-nil.
8601 On text terminals, and on graphical
8602 terminals with no right margin, newlines
8603 may overflow into the last glyph on the
8604 display line.*/
8605 if (!FRAME_WINDOW_P (it->f)
8606 || ((it->bidi_p
8607 && it->bidi_it.paragraph_dir == R2L)
8608 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8609 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8610 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8612 if (!get_next_display_element (it))
8614 result = MOVE_POS_MATCH_OR_ZV;
8615 break;
8617 if (BUFFER_POS_REACHED_P ())
8619 if (ITERATOR_AT_END_OF_LINE_P (it))
8620 result = MOVE_POS_MATCH_OR_ZV;
8621 else
8622 result = MOVE_LINE_CONTINUED;
8623 break;
8625 if (ITERATOR_AT_END_OF_LINE_P (it)
8626 && (it->line_wrap != WORD_WRAP
8627 || wrap_it.sp < 0))
8629 result = MOVE_NEWLINE_OR_CR;
8630 break;
8635 else
8636 IT_RESET_X_ASCENT_DESCENT (it);
8638 if (wrap_it.sp >= 0)
8640 RESTORE_IT (it, &wrap_it, wrap_data);
8641 atpos_it.sp = -1;
8642 atx_it.sp = -1;
8645 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8646 IT_CHARPOS (*it)));
8647 result = MOVE_LINE_CONTINUED;
8648 break;
8651 if (BUFFER_POS_REACHED_P ())
8653 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8654 goto buffer_pos_reached;
8655 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8657 SAVE_IT (atpos_it, *it, atpos_data);
8658 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8662 if (new_x > it->first_visible_x)
8664 /* Glyph is visible. Increment number of glyphs that
8665 would be displayed. */
8666 ++it->hpos;
8670 if (result != MOVE_UNDEFINED)
8671 break;
8673 else if (BUFFER_POS_REACHED_P ())
8675 buffer_pos_reached:
8676 IT_RESET_X_ASCENT_DESCENT (it);
8677 result = MOVE_POS_MATCH_OR_ZV;
8678 break;
8680 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8682 /* Stop when TO_X specified and reached. This check is
8683 necessary here because of lines consisting of a line end,
8684 only. The line end will not produce any glyphs and we
8685 would never get MOVE_X_REACHED. */
8686 eassert (it->nglyphs == 0);
8687 result = MOVE_X_REACHED;
8688 break;
8691 /* Is this a line end? If yes, we're done. */
8692 if (ITERATOR_AT_END_OF_LINE_P (it))
8694 /* If we are past TO_CHARPOS, but never saw any character
8695 positions smaller than TO_CHARPOS, return
8696 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8697 did. */
8698 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8700 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8702 if (IT_CHARPOS (ppos_it) < ZV)
8704 RESTORE_IT (it, &ppos_it, ppos_data);
8705 result = MOVE_POS_MATCH_OR_ZV;
8707 else
8708 goto buffer_pos_reached;
8710 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8711 && IT_CHARPOS (*it) > to_charpos)
8712 goto buffer_pos_reached;
8713 else
8714 result = MOVE_NEWLINE_OR_CR;
8716 else
8717 result = MOVE_NEWLINE_OR_CR;
8718 break;
8721 prev_method = it->method;
8722 if (it->method == GET_FROM_BUFFER)
8723 prev_pos = IT_CHARPOS (*it);
8724 /* The current display element has been consumed. Advance
8725 to the next. */
8726 set_iterator_to_next (it, 1);
8727 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8728 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8729 if (IT_CHARPOS (*it) < to_charpos)
8730 saw_smaller_pos = 1;
8731 if (it->bidi_p
8732 && (op & MOVE_TO_POS)
8733 && IT_CHARPOS (*it) >= to_charpos
8734 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8735 SAVE_IT (ppos_it, *it, ppos_data);
8737 /* Stop if lines are truncated and IT's current x-position is
8738 past the right edge of the window now. */
8739 if (it->line_wrap == TRUNCATE
8740 && it->current_x >= it->last_visible_x)
8742 if (!FRAME_WINDOW_P (it->f)
8743 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8744 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8745 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8746 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8748 int at_eob_p = 0;
8750 if ((at_eob_p = !get_next_display_element (it))
8751 || BUFFER_POS_REACHED_P ()
8752 /* If we are past TO_CHARPOS, but never saw any
8753 character positions smaller than TO_CHARPOS,
8754 return MOVE_POS_MATCH_OR_ZV, like the
8755 unidirectional display did. */
8756 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8757 && !saw_smaller_pos
8758 && IT_CHARPOS (*it) > to_charpos))
8760 if (it->bidi_p
8761 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8762 RESTORE_IT (it, &ppos_it, ppos_data);
8763 result = MOVE_POS_MATCH_OR_ZV;
8764 break;
8766 if (ITERATOR_AT_END_OF_LINE_P (it))
8768 result = MOVE_NEWLINE_OR_CR;
8769 break;
8772 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8773 && !saw_smaller_pos
8774 && IT_CHARPOS (*it) > to_charpos)
8776 if (IT_CHARPOS (ppos_it) < ZV)
8777 RESTORE_IT (it, &ppos_it, ppos_data);
8778 result = MOVE_POS_MATCH_OR_ZV;
8779 break;
8781 result = MOVE_LINE_TRUNCATED;
8782 break;
8784 #undef IT_RESET_X_ASCENT_DESCENT
8787 #undef BUFFER_POS_REACHED_P
8789 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8790 restore the saved iterator. */
8791 if (atpos_it.sp >= 0)
8792 RESTORE_IT (it, &atpos_it, atpos_data);
8793 else if (atx_it.sp >= 0)
8794 RESTORE_IT (it, &atx_it, atx_data);
8796 done:
8798 if (atpos_data)
8799 bidi_unshelve_cache (atpos_data, 1);
8800 if (atx_data)
8801 bidi_unshelve_cache (atx_data, 1);
8802 if (wrap_data)
8803 bidi_unshelve_cache (wrap_data, 1);
8804 if (ppos_data)
8805 bidi_unshelve_cache (ppos_data, 1);
8807 /* Restore the iterator settings altered at the beginning of this
8808 function. */
8809 it->glyph_row = saved_glyph_row;
8810 return result;
8813 /* For external use. */
8814 void
8815 move_it_in_display_line (struct it *it,
8816 ptrdiff_t to_charpos, int to_x,
8817 enum move_operation_enum op)
8819 if (it->line_wrap == WORD_WRAP
8820 && (op & MOVE_TO_X))
8822 struct it save_it;
8823 void *save_data = NULL;
8824 int skip;
8826 SAVE_IT (save_it, *it, save_data);
8827 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8828 /* When word-wrap is on, TO_X may lie past the end
8829 of a wrapped line. Then it->current is the
8830 character on the next line, so backtrack to the
8831 space before the wrap point. */
8832 if (skip == MOVE_LINE_CONTINUED)
8834 int prev_x = max (it->current_x - 1, 0);
8835 RESTORE_IT (it, &save_it, save_data);
8836 move_it_in_display_line_to
8837 (it, -1, prev_x, MOVE_TO_X);
8839 else
8840 bidi_unshelve_cache (save_data, 1);
8842 else
8843 move_it_in_display_line_to (it, to_charpos, to_x, op);
8847 /* Move IT forward until it satisfies one or more of the criteria in
8848 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8850 OP is a bit-mask that specifies where to stop, and in particular,
8851 which of those four position arguments makes a difference. See the
8852 description of enum move_operation_enum.
8854 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8855 screen line, this function will set IT to the next position that is
8856 displayed to the right of TO_CHARPOS on the screen. */
8858 void
8859 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8861 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8862 int line_height, line_start_x = 0, reached = 0;
8863 void *backup_data = NULL;
8865 for (;;)
8867 if (op & MOVE_TO_VPOS)
8869 /* If no TO_CHARPOS and no TO_X specified, stop at the
8870 start of the line TO_VPOS. */
8871 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8873 if (it->vpos == to_vpos)
8875 reached = 1;
8876 break;
8878 else
8879 skip = move_it_in_display_line_to (it, -1, -1, 0);
8881 else
8883 /* TO_VPOS >= 0 means stop at TO_X in the line at
8884 TO_VPOS, or at TO_POS, whichever comes first. */
8885 if (it->vpos == to_vpos)
8887 reached = 2;
8888 break;
8891 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8893 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8895 reached = 3;
8896 break;
8898 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8900 /* We have reached TO_X but not in the line we want. */
8901 skip = move_it_in_display_line_to (it, to_charpos,
8902 -1, MOVE_TO_POS);
8903 if (skip == MOVE_POS_MATCH_OR_ZV)
8905 reached = 4;
8906 break;
8911 else if (op & MOVE_TO_Y)
8913 struct it it_backup;
8915 if (it->line_wrap == WORD_WRAP)
8916 SAVE_IT (it_backup, *it, backup_data);
8918 /* TO_Y specified means stop at TO_X in the line containing
8919 TO_Y---or at TO_CHARPOS if this is reached first. The
8920 problem is that we can't really tell whether the line
8921 contains TO_Y before we have completely scanned it, and
8922 this may skip past TO_X. What we do is to first scan to
8923 TO_X.
8925 If TO_X is not specified, use a TO_X of zero. The reason
8926 is to make the outcome of this function more predictable.
8927 If we didn't use TO_X == 0, we would stop at the end of
8928 the line which is probably not what a caller would expect
8929 to happen. */
8930 skip = move_it_in_display_line_to
8931 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8932 (MOVE_TO_X | (op & MOVE_TO_POS)));
8934 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8935 if (skip == MOVE_POS_MATCH_OR_ZV)
8936 reached = 5;
8937 else if (skip == MOVE_X_REACHED)
8939 /* If TO_X was reached, we want to know whether TO_Y is
8940 in the line. We know this is the case if the already
8941 scanned glyphs make the line tall enough. Otherwise,
8942 we must check by scanning the rest of the line. */
8943 line_height = it->max_ascent + it->max_descent;
8944 if (to_y >= it->current_y
8945 && to_y < it->current_y + line_height)
8947 reached = 6;
8948 break;
8950 SAVE_IT (it_backup, *it, backup_data);
8951 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8952 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8953 op & MOVE_TO_POS);
8954 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8955 line_height = it->max_ascent + it->max_descent;
8956 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8958 if (to_y >= it->current_y
8959 && to_y < it->current_y + line_height)
8961 /* If TO_Y is in this line and TO_X was reached
8962 above, we scanned too far. We have to restore
8963 IT's settings to the ones before skipping. But
8964 keep the more accurate values of max_ascent and
8965 max_descent we've found while skipping the rest
8966 of the line, for the sake of callers, such as
8967 pos_visible_p, that need to know the line
8968 height. */
8969 int max_ascent = it->max_ascent;
8970 int max_descent = it->max_descent;
8972 RESTORE_IT (it, &it_backup, backup_data);
8973 it->max_ascent = max_ascent;
8974 it->max_descent = max_descent;
8975 reached = 6;
8977 else
8979 skip = skip2;
8980 if (skip == MOVE_POS_MATCH_OR_ZV)
8981 reached = 7;
8984 else
8986 /* Check whether TO_Y is in this line. */
8987 line_height = it->max_ascent + it->max_descent;
8988 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8990 if (to_y >= it->current_y
8991 && to_y < it->current_y + line_height)
8993 /* When word-wrap is on, TO_X may lie past the end
8994 of a wrapped line. Then it->current is the
8995 character on the next line, so backtrack to the
8996 space before the wrap point. */
8997 if (skip == MOVE_LINE_CONTINUED
8998 && it->line_wrap == WORD_WRAP)
9000 int prev_x = max (it->current_x - 1, 0);
9001 RESTORE_IT (it, &it_backup, backup_data);
9002 skip = move_it_in_display_line_to
9003 (it, -1, prev_x, MOVE_TO_X);
9005 reached = 6;
9009 if (reached)
9010 break;
9012 else if (BUFFERP (it->object)
9013 && (it->method == GET_FROM_BUFFER
9014 || it->method == GET_FROM_STRETCH)
9015 && IT_CHARPOS (*it) >= to_charpos
9016 /* Under bidi iteration, a call to set_iterator_to_next
9017 can scan far beyond to_charpos if the initial
9018 portion of the next line needs to be reordered. In
9019 that case, give move_it_in_display_line_to another
9020 chance below. */
9021 && !(it->bidi_p
9022 && it->bidi_it.scan_dir == -1))
9023 skip = MOVE_POS_MATCH_OR_ZV;
9024 else
9025 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9027 switch (skip)
9029 case MOVE_POS_MATCH_OR_ZV:
9030 reached = 8;
9031 goto out;
9033 case MOVE_NEWLINE_OR_CR:
9034 set_iterator_to_next (it, 1);
9035 it->continuation_lines_width = 0;
9036 break;
9038 case MOVE_LINE_TRUNCATED:
9039 it->continuation_lines_width = 0;
9040 reseat_at_next_visible_line_start (it, 0);
9041 if ((op & MOVE_TO_POS) != 0
9042 && IT_CHARPOS (*it) > to_charpos)
9044 reached = 9;
9045 goto out;
9047 break;
9049 case MOVE_LINE_CONTINUED:
9050 /* For continued lines ending in a tab, some of the glyphs
9051 associated with the tab are displayed on the current
9052 line. Since it->current_x does not include these glyphs,
9053 we use it->last_visible_x instead. */
9054 if (it->c == '\t')
9056 it->continuation_lines_width += it->last_visible_x;
9057 /* When moving by vpos, ensure that the iterator really
9058 advances to the next line (bug#847, bug#969). Fixme:
9059 do we need to do this in other circumstances? */
9060 if (it->current_x != it->last_visible_x
9061 && (op & MOVE_TO_VPOS)
9062 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9064 line_start_x = it->current_x + it->pixel_width
9065 - it->last_visible_x;
9066 set_iterator_to_next (it, 0);
9069 else
9070 it->continuation_lines_width += it->current_x;
9071 break;
9073 default:
9074 emacs_abort ();
9077 /* Reset/increment for the next run. */
9078 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9079 it->current_x = line_start_x;
9080 line_start_x = 0;
9081 it->hpos = 0;
9082 it->current_y += it->max_ascent + it->max_descent;
9083 ++it->vpos;
9084 last_height = it->max_ascent + it->max_descent;
9085 it->max_ascent = it->max_descent = 0;
9088 out:
9090 /* On text terminals, we may stop at the end of a line in the middle
9091 of a multi-character glyph. If the glyph itself is continued,
9092 i.e. it is actually displayed on the next line, don't treat this
9093 stopping point as valid; move to the next line instead (unless
9094 that brings us offscreen). */
9095 if (!FRAME_WINDOW_P (it->f)
9096 && op & MOVE_TO_POS
9097 && IT_CHARPOS (*it) == to_charpos
9098 && it->what == IT_CHARACTER
9099 && it->nglyphs > 1
9100 && it->line_wrap == WINDOW_WRAP
9101 && it->current_x == it->last_visible_x - 1
9102 && it->c != '\n'
9103 && it->c != '\t'
9104 && it->vpos < it->w->window_end_vpos)
9106 it->continuation_lines_width += it->current_x;
9107 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9108 it->current_y += it->max_ascent + it->max_descent;
9109 ++it->vpos;
9110 last_height = it->max_ascent + it->max_descent;
9113 if (backup_data)
9114 bidi_unshelve_cache (backup_data, 1);
9116 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9120 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9122 If DY > 0, move IT backward at least that many pixels. DY = 0
9123 means move IT backward to the preceding line start or BEGV. This
9124 function may move over more than DY pixels if IT->current_y - DY
9125 ends up in the middle of a line; in this case IT->current_y will be
9126 set to the top of the line moved to. */
9128 void
9129 move_it_vertically_backward (struct it *it, int dy)
9131 int nlines, h;
9132 struct it it2, it3;
9133 void *it2data = NULL, *it3data = NULL;
9134 ptrdiff_t start_pos;
9135 int nchars_per_row
9136 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9137 ptrdiff_t pos_limit;
9139 move_further_back:
9140 eassert (dy >= 0);
9142 start_pos = IT_CHARPOS (*it);
9144 /* Estimate how many newlines we must move back. */
9145 nlines = max (1, dy / default_line_pixel_height (it->w));
9146 if (it->line_wrap == TRUNCATE)
9147 pos_limit = BEGV;
9148 else
9149 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9151 /* Set the iterator's position that many lines back. But don't go
9152 back more than NLINES full screen lines -- this wins a day with
9153 buffers which have very long lines. */
9154 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9155 back_to_previous_visible_line_start (it);
9157 /* Reseat the iterator here. When moving backward, we don't want
9158 reseat to skip forward over invisible text, set up the iterator
9159 to deliver from overlay strings at the new position etc. So,
9160 use reseat_1 here. */
9161 reseat_1 (it, it->current.pos, 1);
9163 /* We are now surely at a line start. */
9164 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9165 reordering is in effect. */
9166 it->continuation_lines_width = 0;
9168 /* Move forward and see what y-distance we moved. First move to the
9169 start of the next line so that we get its height. We need this
9170 height to be able to tell whether we reached the specified
9171 y-distance. */
9172 SAVE_IT (it2, *it, it2data);
9173 it2.max_ascent = it2.max_descent = 0;
9176 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9177 MOVE_TO_POS | MOVE_TO_VPOS);
9179 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9180 /* If we are in a display string which starts at START_POS,
9181 and that display string includes a newline, and we are
9182 right after that newline (i.e. at the beginning of a
9183 display line), exit the loop, because otherwise we will
9184 infloop, since move_it_to will see that it is already at
9185 START_POS and will not move. */
9186 || (it2.method == GET_FROM_STRING
9187 && IT_CHARPOS (it2) == start_pos
9188 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9189 eassert (IT_CHARPOS (*it) >= BEGV);
9190 SAVE_IT (it3, it2, it3data);
9192 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9193 eassert (IT_CHARPOS (*it) >= BEGV);
9194 /* H is the actual vertical distance from the position in *IT
9195 and the starting position. */
9196 h = it2.current_y - it->current_y;
9197 /* NLINES is the distance in number of lines. */
9198 nlines = it2.vpos - it->vpos;
9200 /* Correct IT's y and vpos position
9201 so that they are relative to the starting point. */
9202 it->vpos -= nlines;
9203 it->current_y -= h;
9205 if (dy == 0)
9207 /* DY == 0 means move to the start of the screen line. The
9208 value of nlines is > 0 if continuation lines were involved,
9209 or if the original IT position was at start of a line. */
9210 RESTORE_IT (it, it, it2data);
9211 if (nlines > 0)
9212 move_it_by_lines (it, nlines);
9213 /* The above code moves us to some position NLINES down,
9214 usually to its first glyph (leftmost in an L2R line), but
9215 that's not necessarily the start of the line, under bidi
9216 reordering. We want to get to the character position
9217 that is immediately after the newline of the previous
9218 line. */
9219 if (it->bidi_p
9220 && !it->continuation_lines_width
9221 && !STRINGP (it->string)
9222 && IT_CHARPOS (*it) > BEGV
9223 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9225 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9227 DEC_BOTH (cp, bp);
9228 cp = find_newline_no_quit (cp, bp, -1, NULL);
9229 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9231 bidi_unshelve_cache (it3data, 1);
9233 else
9235 /* The y-position we try to reach, relative to *IT.
9236 Note that H has been subtracted in front of the if-statement. */
9237 int target_y = it->current_y + h - dy;
9238 int y0 = it3.current_y;
9239 int y1;
9240 int line_height;
9242 RESTORE_IT (&it3, &it3, it3data);
9243 y1 = line_bottom_y (&it3);
9244 line_height = y1 - y0;
9245 RESTORE_IT (it, it, it2data);
9246 /* If we did not reach target_y, try to move further backward if
9247 we can. If we moved too far backward, try to move forward. */
9248 if (target_y < it->current_y
9249 /* This is heuristic. In a window that's 3 lines high, with
9250 a line height of 13 pixels each, recentering with point
9251 on the bottom line will try to move -39/2 = 19 pixels
9252 backward. Try to avoid moving into the first line. */
9253 && (it->current_y - target_y
9254 > min (window_box_height (it->w), line_height * 2 / 3))
9255 && IT_CHARPOS (*it) > BEGV)
9257 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9258 target_y - it->current_y));
9259 dy = it->current_y - target_y;
9260 goto move_further_back;
9262 else if (target_y >= it->current_y + line_height
9263 && IT_CHARPOS (*it) < ZV)
9265 /* Should move forward by at least one line, maybe more.
9267 Note: Calling move_it_by_lines can be expensive on
9268 terminal frames, where compute_motion is used (via
9269 vmotion) to do the job, when there are very long lines
9270 and truncate-lines is nil. That's the reason for
9271 treating terminal frames specially here. */
9273 if (!FRAME_WINDOW_P (it->f))
9274 move_it_vertically (it, target_y - (it->current_y + line_height));
9275 else
9279 move_it_by_lines (it, 1);
9281 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9288 /* Move IT by a specified amount of pixel lines DY. DY negative means
9289 move backwards. DY = 0 means move to start of screen line. At the
9290 end, IT will be on the start of a screen line. */
9292 void
9293 move_it_vertically (struct it *it, int dy)
9295 if (dy <= 0)
9296 move_it_vertically_backward (it, -dy);
9297 else
9299 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9300 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9301 MOVE_TO_POS | MOVE_TO_Y);
9302 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9304 /* If buffer ends in ZV without a newline, move to the start of
9305 the line to satisfy the post-condition. */
9306 if (IT_CHARPOS (*it) == ZV
9307 && ZV > BEGV
9308 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9309 move_it_by_lines (it, 0);
9314 /* Move iterator IT past the end of the text line it is in. */
9316 void
9317 move_it_past_eol (struct it *it)
9319 enum move_it_result rc;
9321 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9322 if (rc == MOVE_NEWLINE_OR_CR)
9323 set_iterator_to_next (it, 0);
9327 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9328 negative means move up. DVPOS == 0 means move to the start of the
9329 screen line.
9331 Optimization idea: If we would know that IT->f doesn't use
9332 a face with proportional font, we could be faster for
9333 truncate-lines nil. */
9335 void
9336 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9339 /* The commented-out optimization uses vmotion on terminals. This
9340 gives bad results, because elements like it->what, on which
9341 callers such as pos_visible_p rely, aren't updated. */
9342 /* struct position pos;
9343 if (!FRAME_WINDOW_P (it->f))
9345 struct text_pos textpos;
9347 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9348 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9349 reseat (it, textpos, 1);
9350 it->vpos += pos.vpos;
9351 it->current_y += pos.vpos;
9353 else */
9355 if (dvpos == 0)
9357 /* DVPOS == 0 means move to the start of the screen line. */
9358 move_it_vertically_backward (it, 0);
9359 /* Let next call to line_bottom_y calculate real line height */
9360 last_height = 0;
9362 else if (dvpos > 0)
9364 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9365 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9367 /* Only move to the next buffer position if we ended up in a
9368 string from display property, not in an overlay string
9369 (before-string or after-string). That is because the
9370 latter don't conceal the underlying buffer position, so
9371 we can ask to move the iterator to the exact position we
9372 are interested in. Note that, even if we are already at
9373 IT_CHARPOS (*it), the call below is not a no-op, as it
9374 will detect that we are at the end of the string, pop the
9375 iterator, and compute it->current_x and it->hpos
9376 correctly. */
9377 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9378 -1, -1, -1, MOVE_TO_POS);
9381 else
9383 struct it it2;
9384 void *it2data = NULL;
9385 ptrdiff_t start_charpos, i;
9386 int nchars_per_row
9387 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9388 ptrdiff_t pos_limit;
9390 /* Start at the beginning of the screen line containing IT's
9391 position. This may actually move vertically backwards,
9392 in case of overlays, so adjust dvpos accordingly. */
9393 dvpos += it->vpos;
9394 move_it_vertically_backward (it, 0);
9395 dvpos -= it->vpos;
9397 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9398 screen lines, and reseat the iterator there. */
9399 start_charpos = IT_CHARPOS (*it);
9400 if (it->line_wrap == TRUNCATE)
9401 pos_limit = BEGV;
9402 else
9403 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9404 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9405 back_to_previous_visible_line_start (it);
9406 reseat (it, it->current.pos, 1);
9408 /* Move further back if we end up in a string or an image. */
9409 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9411 /* First try to move to start of display line. */
9412 dvpos += it->vpos;
9413 move_it_vertically_backward (it, 0);
9414 dvpos -= it->vpos;
9415 if (IT_POS_VALID_AFTER_MOVE_P (it))
9416 break;
9417 /* If start of line is still in string or image,
9418 move further back. */
9419 back_to_previous_visible_line_start (it);
9420 reseat (it, it->current.pos, 1);
9421 dvpos--;
9424 it->current_x = it->hpos = 0;
9426 /* Above call may have moved too far if continuation lines
9427 are involved. Scan forward and see if it did. */
9428 SAVE_IT (it2, *it, it2data);
9429 it2.vpos = it2.current_y = 0;
9430 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9431 it->vpos -= it2.vpos;
9432 it->current_y -= it2.current_y;
9433 it->current_x = it->hpos = 0;
9435 /* If we moved too far back, move IT some lines forward. */
9436 if (it2.vpos > -dvpos)
9438 int delta = it2.vpos + dvpos;
9440 RESTORE_IT (&it2, &it2, it2data);
9441 SAVE_IT (it2, *it, it2data);
9442 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9443 /* Move back again if we got too far ahead. */
9444 if (IT_CHARPOS (*it) >= start_charpos)
9445 RESTORE_IT (it, &it2, it2data);
9446 else
9447 bidi_unshelve_cache (it2data, 1);
9449 else
9450 RESTORE_IT (it, it, it2data);
9454 /* Return 1 if IT points into the middle of a display vector. */
9457 in_display_vector_p (struct it *it)
9459 return (it->method == GET_FROM_DISPLAY_VECTOR
9460 && it->current.dpvec_index > 0
9461 && it->dpvec + it->current.dpvec_index != it->dpend);
9465 /***********************************************************************
9466 Messages
9467 ***********************************************************************/
9470 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9471 to *Messages*. */
9473 void
9474 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9476 Lisp_Object args[3];
9477 Lisp_Object msg, fmt;
9478 char *buffer;
9479 ptrdiff_t len;
9480 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9481 USE_SAFE_ALLOCA;
9483 fmt = msg = Qnil;
9484 GCPRO4 (fmt, msg, arg1, arg2);
9486 args[0] = fmt = build_string (format);
9487 args[1] = arg1;
9488 args[2] = arg2;
9489 msg = Fformat (3, args);
9491 len = SBYTES (msg) + 1;
9492 buffer = SAFE_ALLOCA (len);
9493 memcpy (buffer, SDATA (msg), len);
9495 message_dolog (buffer, len - 1, 1, 0);
9496 SAFE_FREE ();
9498 UNGCPRO;
9502 /* Output a newline in the *Messages* buffer if "needs" one. */
9504 void
9505 message_log_maybe_newline (void)
9507 if (message_log_need_newline)
9508 message_dolog ("", 0, 1, 0);
9512 /* Add a string M of length NBYTES to the message log, optionally
9513 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9514 true, means interpret the contents of M as multibyte. This
9515 function calls low-level routines in order to bypass text property
9516 hooks, etc. which might not be safe to run.
9518 This may GC (insert may run before/after change hooks),
9519 so the buffer M must NOT point to a Lisp string. */
9521 void
9522 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9524 const unsigned char *msg = (const unsigned char *) m;
9526 if (!NILP (Vmemory_full))
9527 return;
9529 if (!NILP (Vmessage_log_max))
9531 struct buffer *oldbuf;
9532 Lisp_Object oldpoint, oldbegv, oldzv;
9533 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9534 ptrdiff_t point_at_end = 0;
9535 ptrdiff_t zv_at_end = 0;
9536 Lisp_Object old_deactivate_mark;
9537 bool shown;
9538 struct gcpro gcpro1;
9540 old_deactivate_mark = Vdeactivate_mark;
9541 oldbuf = current_buffer;
9542 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9543 bset_undo_list (current_buffer, Qt);
9545 oldpoint = message_dolog_marker1;
9546 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9547 oldbegv = message_dolog_marker2;
9548 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9549 oldzv = message_dolog_marker3;
9550 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9551 GCPRO1 (old_deactivate_mark);
9553 if (PT == Z)
9554 point_at_end = 1;
9555 if (ZV == Z)
9556 zv_at_end = 1;
9558 BEGV = BEG;
9559 BEGV_BYTE = BEG_BYTE;
9560 ZV = Z;
9561 ZV_BYTE = Z_BYTE;
9562 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9564 /* Insert the string--maybe converting multibyte to single byte
9565 or vice versa, so that all the text fits the buffer. */
9566 if (multibyte
9567 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9569 ptrdiff_t i;
9570 int c, char_bytes;
9571 char work[1];
9573 /* Convert a multibyte string to single-byte
9574 for the *Message* buffer. */
9575 for (i = 0; i < nbytes; i += char_bytes)
9577 c = string_char_and_length (msg + i, &char_bytes);
9578 work[0] = (ASCII_CHAR_P (c)
9580 : multibyte_char_to_unibyte (c));
9581 insert_1_both (work, 1, 1, 1, 0, 0);
9584 else if (! multibyte
9585 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9587 ptrdiff_t i;
9588 int c, char_bytes;
9589 unsigned char str[MAX_MULTIBYTE_LENGTH];
9590 /* Convert a single-byte string to multibyte
9591 for the *Message* buffer. */
9592 for (i = 0; i < nbytes; i++)
9594 c = msg[i];
9595 MAKE_CHAR_MULTIBYTE (c);
9596 char_bytes = CHAR_STRING (c, str);
9597 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9600 else if (nbytes)
9601 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9603 if (nlflag)
9605 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9606 printmax_t dups;
9608 insert_1_both ("\n", 1, 1, 1, 0, 0);
9610 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9611 this_bol = PT;
9612 this_bol_byte = PT_BYTE;
9614 /* See if this line duplicates the previous one.
9615 If so, combine duplicates. */
9616 if (this_bol > BEG)
9618 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9619 prev_bol = PT;
9620 prev_bol_byte = PT_BYTE;
9622 dups = message_log_check_duplicate (prev_bol_byte,
9623 this_bol_byte);
9624 if (dups)
9626 del_range_both (prev_bol, prev_bol_byte,
9627 this_bol, this_bol_byte, 0);
9628 if (dups > 1)
9630 char dupstr[sizeof " [ times]"
9631 + INT_STRLEN_BOUND (printmax_t)];
9633 /* If you change this format, don't forget to also
9634 change message_log_check_duplicate. */
9635 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9636 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9637 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9642 /* If we have more than the desired maximum number of lines
9643 in the *Messages* buffer now, delete the oldest ones.
9644 This is safe because we don't have undo in this buffer. */
9646 if (NATNUMP (Vmessage_log_max))
9648 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9649 -XFASTINT (Vmessage_log_max) - 1, 0);
9650 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9653 BEGV = marker_position (oldbegv);
9654 BEGV_BYTE = marker_byte_position (oldbegv);
9656 if (zv_at_end)
9658 ZV = Z;
9659 ZV_BYTE = Z_BYTE;
9661 else
9663 ZV = marker_position (oldzv);
9664 ZV_BYTE = marker_byte_position (oldzv);
9667 if (point_at_end)
9668 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9669 else
9670 /* We can't do Fgoto_char (oldpoint) because it will run some
9671 Lisp code. */
9672 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9673 marker_byte_position (oldpoint));
9675 UNGCPRO;
9676 unchain_marker (XMARKER (oldpoint));
9677 unchain_marker (XMARKER (oldbegv));
9678 unchain_marker (XMARKER (oldzv));
9680 shown = buffer_window_count (current_buffer) > 0;
9681 set_buffer_internal (oldbuf);
9682 /* We called insert_1_both above with its 5th argument (PREPARE)
9683 zero, which prevents insert_1_both from calling
9684 prepare_to_modify_buffer, which in turns prevents us from
9685 incrementing windows_or_buffers_changed even if *Messages* is
9686 shown in some window. So we must manually incrementing
9687 windows_or_buffers_changed here to make up for that. */
9688 if (shown)
9689 windows_or_buffers_changed++;
9690 else
9691 windows_or_buffers_changed = old_windows_or_buffers_changed;
9692 message_log_need_newline = !nlflag;
9693 Vdeactivate_mark = old_deactivate_mark;
9698 /* We are at the end of the buffer after just having inserted a newline.
9699 (Note: We depend on the fact we won't be crossing the gap.)
9700 Check to see if the most recent message looks a lot like the previous one.
9701 Return 0 if different, 1 if the new one should just replace it, or a
9702 value N > 1 if we should also append " [N times]". */
9704 static intmax_t
9705 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9707 ptrdiff_t i;
9708 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9709 int seen_dots = 0;
9710 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9711 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9713 for (i = 0; i < len; i++)
9715 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9716 seen_dots = 1;
9717 if (p1[i] != p2[i])
9718 return seen_dots;
9720 p1 += len;
9721 if (*p1 == '\n')
9722 return 2;
9723 if (*p1++ == ' ' && *p1++ == '[')
9725 char *pend;
9726 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9727 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9728 return n + 1;
9730 return 0;
9734 /* Display an echo area message M with a specified length of NBYTES
9735 bytes. The string may include null characters. If M is not a
9736 string, clear out any existing message, and let the mini-buffer
9737 text show through.
9739 This function cancels echoing. */
9741 void
9742 message3 (Lisp_Object m)
9744 struct gcpro gcpro1;
9746 GCPRO1 (m);
9747 clear_message (1,1);
9748 cancel_echoing ();
9750 /* First flush out any partial line written with print. */
9751 message_log_maybe_newline ();
9752 if (STRINGP (m))
9754 ptrdiff_t nbytes = SBYTES (m);
9755 bool multibyte = STRING_MULTIBYTE (m);
9756 USE_SAFE_ALLOCA;
9757 char *buffer = SAFE_ALLOCA (nbytes);
9758 memcpy (buffer, SDATA (m), nbytes);
9759 message_dolog (buffer, nbytes, 1, multibyte);
9760 SAFE_FREE ();
9762 message3_nolog (m);
9764 UNGCPRO;
9768 /* The non-logging version of message3.
9769 This does not cancel echoing, because it is used for echoing.
9770 Perhaps we need to make a separate function for echoing
9771 and make this cancel echoing. */
9773 void
9774 message3_nolog (Lisp_Object m)
9776 struct frame *sf = SELECTED_FRAME ();
9778 if (FRAME_INITIAL_P (sf))
9780 if (noninteractive_need_newline)
9781 putc ('\n', stderr);
9782 noninteractive_need_newline = 0;
9783 if (STRINGP (m))
9784 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9785 if (cursor_in_echo_area == 0)
9786 fprintf (stderr, "\n");
9787 fflush (stderr);
9789 /* Error messages get reported properly by cmd_error, so this must be just an
9790 informative message; if the frame hasn't really been initialized yet, just
9791 toss it. */
9792 else if (INTERACTIVE && sf->glyphs_initialized_p)
9794 /* Get the frame containing the mini-buffer
9795 that the selected frame is using. */
9796 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9797 Lisp_Object frame = XWINDOW (mini_window)->frame;
9798 struct frame *f = XFRAME (frame);
9800 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9801 Fmake_frame_visible (frame);
9803 if (STRINGP (m) && SCHARS (m) > 0)
9805 set_message (m);
9806 if (minibuffer_auto_raise)
9807 Fraise_frame (frame);
9808 /* Assume we are not echoing.
9809 (If we are, echo_now will override this.) */
9810 echo_message_buffer = Qnil;
9812 else
9813 clear_message (1, 1);
9815 do_pending_window_change (0);
9816 echo_area_display (1);
9817 do_pending_window_change (0);
9818 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9819 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9824 /* Display a null-terminated echo area message M. If M is 0, clear
9825 out any existing message, and let the mini-buffer text show through.
9827 The buffer M must continue to exist until after the echo area gets
9828 cleared or some other message gets displayed there. Do not pass
9829 text that is stored in a Lisp string. Do not pass text in a buffer
9830 that was alloca'd. */
9832 void
9833 message1 (const char *m)
9835 message3 (m ? build_unibyte_string (m) : Qnil);
9839 /* The non-logging counterpart of message1. */
9841 void
9842 message1_nolog (const char *m)
9844 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9847 /* Display a message M which contains a single %s
9848 which gets replaced with STRING. */
9850 void
9851 message_with_string (const char *m, Lisp_Object string, int log)
9853 CHECK_STRING (string);
9855 if (noninteractive)
9857 if (m)
9859 if (noninteractive_need_newline)
9860 putc ('\n', stderr);
9861 noninteractive_need_newline = 0;
9862 fprintf (stderr, m, SDATA (string));
9863 if (!cursor_in_echo_area)
9864 fprintf (stderr, "\n");
9865 fflush (stderr);
9868 else if (INTERACTIVE)
9870 /* The frame whose minibuffer we're going to display the message on.
9871 It may be larger than the selected frame, so we need
9872 to use its buffer, not the selected frame's buffer. */
9873 Lisp_Object mini_window;
9874 struct frame *f, *sf = SELECTED_FRAME ();
9876 /* Get the frame containing the minibuffer
9877 that the selected frame is using. */
9878 mini_window = FRAME_MINIBUF_WINDOW (sf);
9879 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9881 /* Error messages get reported properly by cmd_error, so this must be
9882 just an informative message; if the frame hasn't really been
9883 initialized yet, just toss it. */
9884 if (f->glyphs_initialized_p)
9886 Lisp_Object args[2], msg;
9887 struct gcpro gcpro1, gcpro2;
9889 args[0] = build_string (m);
9890 args[1] = msg = string;
9891 GCPRO2 (args[0], msg);
9892 gcpro1.nvars = 2;
9894 msg = Fformat (2, args);
9896 if (log)
9897 message3 (msg);
9898 else
9899 message3_nolog (msg);
9901 UNGCPRO;
9903 /* Print should start at the beginning of the message
9904 buffer next time. */
9905 message_buf_print = 0;
9911 /* Dump an informative message to the minibuf. If M is 0, clear out
9912 any existing message, and let the mini-buffer text show through. */
9914 static void
9915 vmessage (const char *m, va_list ap)
9917 if (noninteractive)
9919 if (m)
9921 if (noninteractive_need_newline)
9922 putc ('\n', stderr);
9923 noninteractive_need_newline = 0;
9924 vfprintf (stderr, m, ap);
9925 if (cursor_in_echo_area == 0)
9926 fprintf (stderr, "\n");
9927 fflush (stderr);
9930 else if (INTERACTIVE)
9932 /* The frame whose mini-buffer we're going to display the message
9933 on. It may be larger than the selected frame, so we need to
9934 use its buffer, not the selected frame's buffer. */
9935 Lisp_Object mini_window;
9936 struct frame *f, *sf = SELECTED_FRAME ();
9938 /* Get the frame containing the mini-buffer
9939 that the selected frame is using. */
9940 mini_window = FRAME_MINIBUF_WINDOW (sf);
9941 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9943 /* Error messages get reported properly by cmd_error, so this must be
9944 just an informative message; if the frame hasn't really been
9945 initialized yet, just toss it. */
9946 if (f->glyphs_initialized_p)
9948 if (m)
9950 ptrdiff_t len;
9951 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9952 char *message_buf = alloca (maxsize + 1);
9954 len = doprnt (message_buf, maxsize, m, 0, ap);
9956 message3 (make_string (message_buf, len));
9958 else
9959 message1 (0);
9961 /* Print should start at the beginning of the message
9962 buffer next time. */
9963 message_buf_print = 0;
9968 void
9969 message (const char *m, ...)
9971 va_list ap;
9972 va_start (ap, m);
9973 vmessage (m, ap);
9974 va_end (ap);
9978 #if 0
9979 /* The non-logging version of message. */
9981 void
9982 message_nolog (const char *m, ...)
9984 Lisp_Object old_log_max;
9985 va_list ap;
9986 va_start (ap, m);
9987 old_log_max = Vmessage_log_max;
9988 Vmessage_log_max = Qnil;
9989 vmessage (m, ap);
9990 Vmessage_log_max = old_log_max;
9991 va_end (ap);
9993 #endif
9996 /* Display the current message in the current mini-buffer. This is
9997 only called from error handlers in process.c, and is not time
9998 critical. */
10000 void
10001 update_echo_area (void)
10003 if (!NILP (echo_area_buffer[0]))
10005 Lisp_Object string;
10006 string = Fcurrent_message ();
10007 message3 (string);
10012 /* Make sure echo area buffers in `echo_buffers' are live.
10013 If they aren't, make new ones. */
10015 static void
10016 ensure_echo_area_buffers (void)
10018 int i;
10020 for (i = 0; i < 2; ++i)
10021 if (!BUFFERP (echo_buffer[i])
10022 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10024 char name[30];
10025 Lisp_Object old_buffer;
10026 int j;
10028 old_buffer = echo_buffer[i];
10029 echo_buffer[i] = Fget_buffer_create
10030 (make_formatted_string (name, " *Echo Area %d*", i));
10031 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10032 /* to force word wrap in echo area -
10033 it was decided to postpone this*/
10034 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10036 for (j = 0; j < 2; ++j)
10037 if (EQ (old_buffer, echo_area_buffer[j]))
10038 echo_area_buffer[j] = echo_buffer[i];
10043 /* Call FN with args A1..A2 with either the current or last displayed
10044 echo_area_buffer as current buffer.
10046 WHICH zero means use the current message buffer
10047 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10048 from echo_buffer[] and clear it.
10050 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10051 suitable buffer from echo_buffer[] and clear it.
10053 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10054 that the current message becomes the last displayed one, make
10055 choose a suitable buffer for echo_area_buffer[0], and clear it.
10057 Value is what FN returns. */
10059 static int
10060 with_echo_area_buffer (struct window *w, int which,
10061 int (*fn) (ptrdiff_t, Lisp_Object),
10062 ptrdiff_t a1, Lisp_Object a2)
10064 Lisp_Object buffer;
10065 int this_one, the_other, clear_buffer_p, rc;
10066 ptrdiff_t count = SPECPDL_INDEX ();
10068 /* If buffers aren't live, make new ones. */
10069 ensure_echo_area_buffers ();
10071 clear_buffer_p = 0;
10073 if (which == 0)
10074 this_one = 0, the_other = 1;
10075 else if (which > 0)
10076 this_one = 1, the_other = 0;
10077 else
10079 this_one = 0, the_other = 1;
10080 clear_buffer_p = 1;
10082 /* We need a fresh one in case the current echo buffer equals
10083 the one containing the last displayed echo area message. */
10084 if (!NILP (echo_area_buffer[this_one])
10085 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10086 echo_area_buffer[this_one] = Qnil;
10089 /* Choose a suitable buffer from echo_buffer[] is we don't
10090 have one. */
10091 if (NILP (echo_area_buffer[this_one]))
10093 echo_area_buffer[this_one]
10094 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10095 ? echo_buffer[the_other]
10096 : echo_buffer[this_one]);
10097 clear_buffer_p = 1;
10100 buffer = echo_area_buffer[this_one];
10102 /* Don't get confused by reusing the buffer used for echoing
10103 for a different purpose. */
10104 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10105 cancel_echoing ();
10107 record_unwind_protect (unwind_with_echo_area_buffer,
10108 with_echo_area_buffer_unwind_data (w));
10110 /* Make the echo area buffer current. Note that for display
10111 purposes, it is not necessary that the displayed window's buffer
10112 == current_buffer, except for text property lookup. So, let's
10113 only set that buffer temporarily here without doing a full
10114 Fset_window_buffer. We must also change w->pointm, though,
10115 because otherwise an assertions in unshow_buffer fails, and Emacs
10116 aborts. */
10117 set_buffer_internal_1 (XBUFFER (buffer));
10118 if (w)
10120 wset_buffer (w, buffer);
10121 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10124 bset_undo_list (current_buffer, Qt);
10125 bset_read_only (current_buffer, Qnil);
10126 specbind (Qinhibit_read_only, Qt);
10127 specbind (Qinhibit_modification_hooks, Qt);
10129 if (clear_buffer_p && Z > BEG)
10130 del_range (BEG, Z);
10132 eassert (BEGV >= BEG);
10133 eassert (ZV <= Z && ZV >= BEGV);
10135 rc = fn (a1, a2);
10137 eassert (BEGV >= BEG);
10138 eassert (ZV <= Z && ZV >= BEGV);
10140 unbind_to (count, Qnil);
10141 return rc;
10145 /* Save state that should be preserved around the call to the function
10146 FN called in with_echo_area_buffer. */
10148 static Lisp_Object
10149 with_echo_area_buffer_unwind_data (struct window *w)
10151 int i = 0;
10152 Lisp_Object vector, tmp;
10154 /* Reduce consing by keeping one vector in
10155 Vwith_echo_area_save_vector. */
10156 vector = Vwith_echo_area_save_vector;
10157 Vwith_echo_area_save_vector = Qnil;
10159 if (NILP (vector))
10160 vector = Fmake_vector (make_number (9), Qnil);
10162 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10163 ASET (vector, i, Vdeactivate_mark); ++i;
10164 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10166 if (w)
10168 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10169 ASET (vector, i, w->contents); ++i;
10170 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10171 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10172 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10173 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10175 else
10177 int end = i + 6;
10178 for (; i < end; ++i)
10179 ASET (vector, i, Qnil);
10182 eassert (i == ASIZE (vector));
10183 return vector;
10187 /* Restore global state from VECTOR which was created by
10188 with_echo_area_buffer_unwind_data. */
10190 static void
10191 unwind_with_echo_area_buffer (Lisp_Object vector)
10193 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10194 Vdeactivate_mark = AREF (vector, 1);
10195 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10197 if (WINDOWP (AREF (vector, 3)))
10199 struct window *w;
10200 Lisp_Object buffer;
10202 w = XWINDOW (AREF (vector, 3));
10203 buffer = AREF (vector, 4);
10205 wset_buffer (w, buffer);
10206 set_marker_both (w->pointm, buffer,
10207 XFASTINT (AREF (vector, 5)),
10208 XFASTINT (AREF (vector, 6)));
10209 set_marker_both (w->start, buffer,
10210 XFASTINT (AREF (vector, 7)),
10211 XFASTINT (AREF (vector, 8)));
10214 Vwith_echo_area_save_vector = vector;
10218 /* Set up the echo area for use by print functions. MULTIBYTE_P
10219 non-zero means we will print multibyte. */
10221 void
10222 setup_echo_area_for_printing (int multibyte_p)
10224 /* If we can't find an echo area any more, exit. */
10225 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10226 Fkill_emacs (Qnil);
10228 ensure_echo_area_buffers ();
10230 if (!message_buf_print)
10232 /* A message has been output since the last time we printed.
10233 Choose a fresh echo area buffer. */
10234 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10235 echo_area_buffer[0] = echo_buffer[1];
10236 else
10237 echo_area_buffer[0] = echo_buffer[0];
10239 /* Switch to that buffer and clear it. */
10240 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10241 bset_truncate_lines (current_buffer, Qnil);
10243 if (Z > BEG)
10245 ptrdiff_t count = SPECPDL_INDEX ();
10246 specbind (Qinhibit_read_only, Qt);
10247 /* Note that undo recording is always disabled. */
10248 del_range (BEG, Z);
10249 unbind_to (count, Qnil);
10251 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10253 /* Set up the buffer for the multibyteness we need. */
10254 if (multibyte_p
10255 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10256 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10258 /* Raise the frame containing the echo area. */
10259 if (minibuffer_auto_raise)
10261 struct frame *sf = SELECTED_FRAME ();
10262 Lisp_Object mini_window;
10263 mini_window = FRAME_MINIBUF_WINDOW (sf);
10264 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10267 message_log_maybe_newline ();
10268 message_buf_print = 1;
10270 else
10272 if (NILP (echo_area_buffer[0]))
10274 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10275 echo_area_buffer[0] = echo_buffer[1];
10276 else
10277 echo_area_buffer[0] = echo_buffer[0];
10280 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10282 /* Someone switched buffers between print requests. */
10283 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10284 bset_truncate_lines (current_buffer, Qnil);
10290 /* Display an echo area message in window W. Value is non-zero if W's
10291 height is changed. If display_last_displayed_message_p is
10292 non-zero, display the message that was last displayed, otherwise
10293 display the current message. */
10295 static int
10296 display_echo_area (struct window *w)
10298 int i, no_message_p, window_height_changed_p;
10300 /* Temporarily disable garbage collections while displaying the echo
10301 area. This is done because a GC can print a message itself.
10302 That message would modify the echo area buffer's contents while a
10303 redisplay of the buffer is going on, and seriously confuse
10304 redisplay. */
10305 ptrdiff_t count = inhibit_garbage_collection ();
10307 /* If there is no message, we must call display_echo_area_1
10308 nevertheless because it resizes the window. But we will have to
10309 reset the echo_area_buffer in question to nil at the end because
10310 with_echo_area_buffer will sets it to an empty buffer. */
10311 i = display_last_displayed_message_p ? 1 : 0;
10312 no_message_p = NILP (echo_area_buffer[i]);
10314 window_height_changed_p
10315 = with_echo_area_buffer (w, display_last_displayed_message_p,
10316 display_echo_area_1,
10317 (intptr_t) w, Qnil);
10319 if (no_message_p)
10320 echo_area_buffer[i] = Qnil;
10322 unbind_to (count, Qnil);
10323 return window_height_changed_p;
10327 /* Helper for display_echo_area. Display the current buffer which
10328 contains the current echo area message in window W, a mini-window,
10329 a pointer to which is passed in A1. A2..A4 are currently not used.
10330 Change the height of W so that all of the message is displayed.
10331 Value is non-zero if height of W was changed. */
10333 static int
10334 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10336 intptr_t i1 = a1;
10337 struct window *w = (struct window *) i1;
10338 Lisp_Object window;
10339 struct text_pos start;
10340 int window_height_changed_p = 0;
10342 /* Do this before displaying, so that we have a large enough glyph
10343 matrix for the display. If we can't get enough space for the
10344 whole text, display the last N lines. That works by setting w->start. */
10345 window_height_changed_p = resize_mini_window (w, 0);
10347 /* Use the starting position chosen by resize_mini_window. */
10348 SET_TEXT_POS_FROM_MARKER (start, w->start);
10350 /* Display. */
10351 clear_glyph_matrix (w->desired_matrix);
10352 XSETWINDOW (window, w);
10353 try_window (window, start, 0);
10355 return window_height_changed_p;
10359 /* Resize the echo area window to exactly the size needed for the
10360 currently displayed message, if there is one. If a mini-buffer
10361 is active, don't shrink it. */
10363 void
10364 resize_echo_area_exactly (void)
10366 if (BUFFERP (echo_area_buffer[0])
10367 && WINDOWP (echo_area_window))
10369 struct window *w = XWINDOW (echo_area_window);
10370 int resized_p;
10371 Lisp_Object resize_exactly;
10373 if (minibuf_level == 0)
10374 resize_exactly = Qt;
10375 else
10376 resize_exactly = Qnil;
10378 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10379 (intptr_t) w, resize_exactly);
10380 if (resized_p)
10382 ++windows_or_buffers_changed;
10383 ++update_mode_lines;
10384 redisplay_internal ();
10390 /* Callback function for with_echo_area_buffer, when used from
10391 resize_echo_area_exactly. A1 contains a pointer to the window to
10392 resize, EXACTLY non-nil means resize the mini-window exactly to the
10393 size of the text displayed. A3 and A4 are not used. Value is what
10394 resize_mini_window returns. */
10396 static int
10397 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10399 intptr_t i1 = a1;
10400 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10404 /* Resize mini-window W to fit the size of its contents. EXACT_P
10405 means size the window exactly to the size needed. Otherwise, it's
10406 only enlarged until W's buffer is empty.
10408 Set W->start to the right place to begin display. If the whole
10409 contents fit, start at the beginning. Otherwise, start so as
10410 to make the end of the contents appear. This is particularly
10411 important for y-or-n-p, but seems desirable generally.
10413 Value is non-zero if the window height has been changed. */
10416 resize_mini_window (struct window *w, int exact_p)
10418 struct frame *f = XFRAME (w->frame);
10419 int window_height_changed_p = 0;
10421 eassert (MINI_WINDOW_P (w));
10423 /* By default, start display at the beginning. */
10424 set_marker_both (w->start, w->contents,
10425 BUF_BEGV (XBUFFER (w->contents)),
10426 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10428 /* Don't resize windows while redisplaying a window; it would
10429 confuse redisplay functions when the size of the window they are
10430 displaying changes from under them. Such a resizing can happen,
10431 for instance, when which-func prints a long message while
10432 we are running fontification-functions. We're running these
10433 functions with safe_call which binds inhibit-redisplay to t. */
10434 if (!NILP (Vinhibit_redisplay))
10435 return 0;
10437 /* Nil means don't try to resize. */
10438 if (NILP (Vresize_mini_windows)
10439 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10440 return 0;
10442 if (!FRAME_MINIBUF_ONLY_P (f))
10444 struct it it;
10445 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10446 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10447 int height;
10448 EMACS_INT max_height;
10449 int unit = FRAME_LINE_HEIGHT (f);
10450 struct text_pos start;
10451 struct buffer *old_current_buffer = NULL;
10453 if (current_buffer != XBUFFER (w->contents))
10455 old_current_buffer = current_buffer;
10456 set_buffer_internal (XBUFFER (w->contents));
10459 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10461 /* Compute the max. number of lines specified by the user. */
10462 if (FLOATP (Vmax_mini_window_height))
10463 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10464 else if (INTEGERP (Vmax_mini_window_height))
10465 max_height = XINT (Vmax_mini_window_height);
10466 else
10467 max_height = total_height / 4;
10469 /* Correct that max. height if it's bogus. */
10470 max_height = clip_to_bounds (1, max_height, total_height);
10472 /* Find out the height of the text in the window. */
10473 if (it.line_wrap == TRUNCATE)
10474 height = 1;
10475 else
10477 last_height = 0;
10478 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10479 if (it.max_ascent == 0 && it.max_descent == 0)
10480 height = it.current_y + last_height;
10481 else
10482 height = it.current_y + it.max_ascent + it.max_descent;
10483 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10484 height = (height + unit - 1) / unit;
10487 /* Compute a suitable window start. */
10488 if (height > max_height)
10490 height = max_height;
10491 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10492 move_it_vertically_backward (&it, (height - 1) * unit);
10493 start = it.current.pos;
10495 else
10496 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10497 SET_MARKER_FROM_TEXT_POS (w->start, start);
10499 if (EQ (Vresize_mini_windows, Qgrow_only))
10501 /* Let it grow only, until we display an empty message, in which
10502 case the window shrinks again. */
10503 if (height > WINDOW_TOTAL_LINES (w))
10505 int old_height = WINDOW_TOTAL_LINES (w);
10507 FRAME_WINDOWS_FROZEN (f) = 1;
10508 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10509 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10511 else if (height < WINDOW_TOTAL_LINES (w)
10512 && (exact_p || BEGV == ZV))
10514 int old_height = WINDOW_TOTAL_LINES (w);
10516 FRAME_WINDOWS_FROZEN (f) = 0;
10517 shrink_mini_window (w);
10518 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10521 else
10523 /* Always resize to exact size needed. */
10524 if (height > WINDOW_TOTAL_LINES (w))
10526 int old_height = WINDOW_TOTAL_LINES (w);
10528 FRAME_WINDOWS_FROZEN (f) = 1;
10529 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10530 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10532 else if (height < WINDOW_TOTAL_LINES (w))
10534 int old_height = WINDOW_TOTAL_LINES (w);
10536 FRAME_WINDOWS_FROZEN (f) = 0;
10537 shrink_mini_window (w);
10539 if (height)
10541 FRAME_WINDOWS_FROZEN (f) = 1;
10542 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10545 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10549 if (old_current_buffer)
10550 set_buffer_internal (old_current_buffer);
10553 return window_height_changed_p;
10557 /* Value is the current message, a string, or nil if there is no
10558 current message. */
10560 Lisp_Object
10561 current_message (void)
10563 Lisp_Object msg;
10565 if (!BUFFERP (echo_area_buffer[0]))
10566 msg = Qnil;
10567 else
10569 with_echo_area_buffer (0, 0, current_message_1,
10570 (intptr_t) &msg, Qnil);
10571 if (NILP (msg))
10572 echo_area_buffer[0] = Qnil;
10575 return msg;
10579 static int
10580 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10582 intptr_t i1 = a1;
10583 Lisp_Object *msg = (Lisp_Object *) i1;
10585 if (Z > BEG)
10586 *msg = make_buffer_string (BEG, Z, 1);
10587 else
10588 *msg = Qnil;
10589 return 0;
10593 /* Push the current message on Vmessage_stack for later restoration
10594 by restore_message. Value is non-zero if the current message isn't
10595 empty. This is a relatively infrequent operation, so it's not
10596 worth optimizing. */
10598 bool
10599 push_message (void)
10601 Lisp_Object msg = current_message ();
10602 Vmessage_stack = Fcons (msg, Vmessage_stack);
10603 return STRINGP (msg);
10607 /* Restore message display from the top of Vmessage_stack. */
10609 void
10610 restore_message (void)
10612 eassert (CONSP (Vmessage_stack));
10613 message3_nolog (XCAR (Vmessage_stack));
10617 /* Handler for unwind-protect calling pop_message. */
10619 void
10620 pop_message_unwind (void)
10622 /* Pop the top-most entry off Vmessage_stack. */
10623 eassert (CONSP (Vmessage_stack));
10624 Vmessage_stack = XCDR (Vmessage_stack);
10628 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10629 exits. If the stack is not empty, we have a missing pop_message
10630 somewhere. */
10632 void
10633 check_message_stack (void)
10635 if (!NILP (Vmessage_stack))
10636 emacs_abort ();
10640 /* Truncate to NCHARS what will be displayed in the echo area the next
10641 time we display it---but don't redisplay it now. */
10643 void
10644 truncate_echo_area (ptrdiff_t nchars)
10646 if (nchars == 0)
10647 echo_area_buffer[0] = Qnil;
10648 else if (!noninteractive
10649 && INTERACTIVE
10650 && !NILP (echo_area_buffer[0]))
10652 struct frame *sf = SELECTED_FRAME ();
10653 /* Error messages get reported properly by cmd_error, so this must be
10654 just an informative message; if the frame hasn't really been
10655 initialized yet, just toss it. */
10656 if (sf->glyphs_initialized_p)
10657 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10662 /* Helper function for truncate_echo_area. Truncate the current
10663 message to at most NCHARS characters. */
10665 static int
10666 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10668 if (BEG + nchars < Z)
10669 del_range (BEG + nchars, Z);
10670 if (Z == BEG)
10671 echo_area_buffer[0] = Qnil;
10672 return 0;
10675 /* Set the current message to STRING. */
10677 static void
10678 set_message (Lisp_Object string)
10680 eassert (STRINGP (string));
10682 message_enable_multibyte = STRING_MULTIBYTE (string);
10684 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10685 message_buf_print = 0;
10686 help_echo_showing_p = 0;
10688 if (STRINGP (Vdebug_on_message)
10689 && STRINGP (string)
10690 && fast_string_match (Vdebug_on_message, string) >= 0)
10691 call_debugger (list2 (Qerror, string));
10695 /* Helper function for set_message. First argument is ignored and second
10696 argument has the same meaning as for set_message.
10697 This function is called with the echo area buffer being current. */
10699 static int
10700 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10702 eassert (STRINGP (string));
10704 /* Change multibyteness of the echo buffer appropriately. */
10705 if (message_enable_multibyte
10706 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10707 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10709 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10710 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10711 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10713 /* Insert new message at BEG. */
10714 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10716 /* This function takes care of single/multibyte conversion.
10717 We just have to ensure that the echo area buffer has the right
10718 setting of enable_multibyte_characters. */
10719 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10721 return 0;
10725 /* Clear messages. CURRENT_P non-zero means clear the current
10726 message. LAST_DISPLAYED_P non-zero means clear the message
10727 last displayed. */
10729 void
10730 clear_message (int current_p, int last_displayed_p)
10732 if (current_p)
10734 echo_area_buffer[0] = Qnil;
10735 message_cleared_p = 1;
10738 if (last_displayed_p)
10739 echo_area_buffer[1] = Qnil;
10741 message_buf_print = 0;
10744 /* Clear garbaged frames.
10746 This function is used where the old redisplay called
10747 redraw_garbaged_frames which in turn called redraw_frame which in
10748 turn called clear_frame. The call to clear_frame was a source of
10749 flickering. I believe a clear_frame is not necessary. It should
10750 suffice in the new redisplay to invalidate all current matrices,
10751 and ensure a complete redisplay of all windows. */
10753 static void
10754 clear_garbaged_frames (void)
10756 if (frame_garbaged)
10758 Lisp_Object tail, frame;
10759 int changed_count = 0;
10761 FOR_EACH_FRAME (tail, frame)
10763 struct frame *f = XFRAME (frame);
10765 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10767 if (f->resized_p)
10769 redraw_frame (f);
10770 f->force_flush_display_p = 1;
10772 clear_current_matrices (f);
10773 changed_count++;
10774 f->garbaged = 0;
10775 f->resized_p = 0;
10779 frame_garbaged = 0;
10780 if (changed_count)
10781 ++windows_or_buffers_changed;
10786 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10787 is non-zero update selected_frame. Value is non-zero if the
10788 mini-windows height has been changed. */
10790 static int
10791 echo_area_display (int update_frame_p)
10793 Lisp_Object mini_window;
10794 struct window *w;
10795 struct frame *f;
10796 int window_height_changed_p = 0;
10797 struct frame *sf = SELECTED_FRAME ();
10799 mini_window = FRAME_MINIBUF_WINDOW (sf);
10800 w = XWINDOW (mini_window);
10801 f = XFRAME (WINDOW_FRAME (w));
10803 /* Don't display if frame is invisible or not yet initialized. */
10804 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10805 return 0;
10807 #ifdef HAVE_WINDOW_SYSTEM
10808 /* When Emacs starts, selected_frame may be the initial terminal
10809 frame. If we let this through, a message would be displayed on
10810 the terminal. */
10811 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10812 return 0;
10813 #endif /* HAVE_WINDOW_SYSTEM */
10815 /* Redraw garbaged frames. */
10816 clear_garbaged_frames ();
10818 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10820 echo_area_window = mini_window;
10821 window_height_changed_p = display_echo_area (w);
10822 w->must_be_updated_p = 1;
10824 /* Update the display, unless called from redisplay_internal.
10825 Also don't update the screen during redisplay itself. The
10826 update will happen at the end of redisplay, and an update
10827 here could cause confusion. */
10828 if (update_frame_p && !redisplaying_p)
10830 int n = 0;
10832 /* If the display update has been interrupted by pending
10833 input, update mode lines in the frame. Due to the
10834 pending input, it might have been that redisplay hasn't
10835 been called, so that mode lines above the echo area are
10836 garbaged. This looks odd, so we prevent it here. */
10837 if (!display_completed)
10838 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10840 if (window_height_changed_p
10841 /* Don't do this if Emacs is shutting down. Redisplay
10842 needs to run hooks. */
10843 && !NILP (Vrun_hooks))
10845 /* Must update other windows. Likewise as in other
10846 cases, don't let this update be interrupted by
10847 pending input. */
10848 ptrdiff_t count = SPECPDL_INDEX ();
10849 specbind (Qredisplay_dont_pause, Qt);
10850 windows_or_buffers_changed = 1;
10851 redisplay_internal ();
10852 unbind_to (count, Qnil);
10854 else if (FRAME_WINDOW_P (f) && n == 0)
10856 /* Window configuration is the same as before.
10857 Can do with a display update of the echo area,
10858 unless we displayed some mode lines. */
10859 update_single_window (w, 1);
10860 FRAME_RIF (f)->flush_display (f);
10862 else
10863 update_frame (f, 1, 1);
10865 /* If cursor is in the echo area, make sure that the next
10866 redisplay displays the minibuffer, so that the cursor will
10867 be replaced with what the minibuffer wants. */
10868 if (cursor_in_echo_area)
10869 ++windows_or_buffers_changed;
10872 else if (!EQ (mini_window, selected_window))
10873 windows_or_buffers_changed++;
10875 /* Last displayed message is now the current message. */
10876 echo_area_buffer[1] = echo_area_buffer[0];
10877 /* Inform read_char that we're not echoing. */
10878 echo_message_buffer = Qnil;
10880 /* Prevent redisplay optimization in redisplay_internal by resetting
10881 this_line_start_pos. This is done because the mini-buffer now
10882 displays the message instead of its buffer text. */
10883 if (EQ (mini_window, selected_window))
10884 CHARPOS (this_line_start_pos) = 0;
10886 return window_height_changed_p;
10889 /* Nonzero if the current window's buffer is shown in more than one
10890 window and was modified since last redisplay. */
10892 static int
10893 buffer_shared_and_changed (void)
10895 return (buffer_window_count (current_buffer) > 1
10896 && UNCHANGED_MODIFIED < MODIFF);
10899 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10900 is enabled and mark of W's buffer was changed since last W's update. */
10902 static int
10903 window_buffer_changed (struct window *w)
10905 struct buffer *b = XBUFFER (w->contents);
10907 eassert (BUFFER_LIVE_P (b));
10909 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10910 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10911 != (w->region_showing != 0)));
10914 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10916 static int
10917 mode_line_update_needed (struct window *w)
10919 return (w->column_number_displayed != -1
10920 && !(PT == w->last_point && !window_outdated (w))
10921 && (w->column_number_displayed != current_column ()));
10924 /* Nonzero if window start of W is frozen and may not be changed during
10925 redisplay. */
10927 static bool
10928 window_frozen_p (struct window *w)
10930 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10932 Lisp_Object window;
10934 XSETWINDOW (window, w);
10935 if (MINI_WINDOW_P (w))
10936 return 0;
10937 else if (EQ (window, selected_window))
10938 return 0;
10939 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10940 && EQ (window, Vminibuf_scroll_window))
10941 /* This special window can't be frozen too. */
10942 return 0;
10943 else
10944 return 1;
10946 return 0;
10949 /***********************************************************************
10950 Mode Lines and Frame Titles
10951 ***********************************************************************/
10953 /* A buffer for constructing non-propertized mode-line strings and
10954 frame titles in it; allocated from the heap in init_xdisp and
10955 resized as needed in store_mode_line_noprop_char. */
10957 static char *mode_line_noprop_buf;
10959 /* The buffer's end, and a current output position in it. */
10961 static char *mode_line_noprop_buf_end;
10962 static char *mode_line_noprop_ptr;
10964 #define MODE_LINE_NOPROP_LEN(start) \
10965 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10967 static enum {
10968 MODE_LINE_DISPLAY = 0,
10969 MODE_LINE_TITLE,
10970 MODE_LINE_NOPROP,
10971 MODE_LINE_STRING
10972 } mode_line_target;
10974 /* Alist that caches the results of :propertize.
10975 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10976 static Lisp_Object mode_line_proptrans_alist;
10978 /* List of strings making up the mode-line. */
10979 static Lisp_Object mode_line_string_list;
10981 /* Base face property when building propertized mode line string. */
10982 static Lisp_Object mode_line_string_face;
10983 static Lisp_Object mode_line_string_face_prop;
10986 /* Unwind data for mode line strings */
10988 static Lisp_Object Vmode_line_unwind_vector;
10990 static Lisp_Object
10991 format_mode_line_unwind_data (struct frame *target_frame,
10992 struct buffer *obuf,
10993 Lisp_Object owin,
10994 int save_proptrans)
10996 Lisp_Object vector, tmp;
10998 /* Reduce consing by keeping one vector in
10999 Vwith_echo_area_save_vector. */
11000 vector = Vmode_line_unwind_vector;
11001 Vmode_line_unwind_vector = Qnil;
11003 if (NILP (vector))
11004 vector = Fmake_vector (make_number (10), Qnil);
11006 ASET (vector, 0, make_number (mode_line_target));
11007 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11008 ASET (vector, 2, mode_line_string_list);
11009 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11010 ASET (vector, 4, mode_line_string_face);
11011 ASET (vector, 5, mode_line_string_face_prop);
11013 if (obuf)
11014 XSETBUFFER (tmp, obuf);
11015 else
11016 tmp = Qnil;
11017 ASET (vector, 6, tmp);
11018 ASET (vector, 7, owin);
11019 if (target_frame)
11021 /* Similarly to `with-selected-window', if the operation selects
11022 a window on another frame, we must restore that frame's
11023 selected window, and (for a tty) the top-frame. */
11024 ASET (vector, 8, target_frame->selected_window);
11025 if (FRAME_TERMCAP_P (target_frame))
11026 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11029 return vector;
11032 static void
11033 unwind_format_mode_line (Lisp_Object vector)
11035 Lisp_Object old_window = AREF (vector, 7);
11036 Lisp_Object target_frame_window = AREF (vector, 8);
11037 Lisp_Object old_top_frame = AREF (vector, 9);
11039 mode_line_target = XINT (AREF (vector, 0));
11040 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11041 mode_line_string_list = AREF (vector, 2);
11042 if (! EQ (AREF (vector, 3), Qt))
11043 mode_line_proptrans_alist = AREF (vector, 3);
11044 mode_line_string_face = AREF (vector, 4);
11045 mode_line_string_face_prop = AREF (vector, 5);
11047 /* Select window before buffer, since it may change the buffer. */
11048 if (!NILP (old_window))
11050 /* If the operation that we are unwinding had selected a window
11051 on a different frame, reset its frame-selected-window. For a
11052 text terminal, reset its top-frame if necessary. */
11053 if (!NILP (target_frame_window))
11055 Lisp_Object frame
11056 = WINDOW_FRAME (XWINDOW (target_frame_window));
11058 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11059 Fselect_window (target_frame_window, Qt);
11061 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11062 Fselect_frame (old_top_frame, Qt);
11065 Fselect_window (old_window, Qt);
11068 if (!NILP (AREF (vector, 6)))
11070 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11071 ASET (vector, 6, Qnil);
11074 Vmode_line_unwind_vector = vector;
11078 /* Store a single character C for the frame title in mode_line_noprop_buf.
11079 Re-allocate mode_line_noprop_buf if necessary. */
11081 static void
11082 store_mode_line_noprop_char (char c)
11084 /* If output position has reached the end of the allocated buffer,
11085 increase the buffer's size. */
11086 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11088 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11089 ptrdiff_t size = len;
11090 mode_line_noprop_buf =
11091 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11092 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11093 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11096 *mode_line_noprop_ptr++ = c;
11100 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11101 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11102 characters that yield more columns than PRECISION; PRECISION <= 0
11103 means copy the whole string. Pad with spaces until FIELD_WIDTH
11104 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11105 pad. Called from display_mode_element when it is used to build a
11106 frame title. */
11108 static int
11109 store_mode_line_noprop (const char *string, int field_width, int precision)
11111 const unsigned char *str = (const unsigned char *) string;
11112 int n = 0;
11113 ptrdiff_t dummy, nbytes;
11115 /* Copy at most PRECISION chars from STR. */
11116 nbytes = strlen (string);
11117 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11118 while (nbytes--)
11119 store_mode_line_noprop_char (*str++);
11121 /* Fill up with spaces until FIELD_WIDTH reached. */
11122 while (field_width > 0
11123 && n < field_width)
11125 store_mode_line_noprop_char (' ');
11126 ++n;
11129 return n;
11132 /***********************************************************************
11133 Frame Titles
11134 ***********************************************************************/
11136 #ifdef HAVE_WINDOW_SYSTEM
11138 /* Set the title of FRAME, if it has changed. The title format is
11139 Vicon_title_format if FRAME is iconified, otherwise it is
11140 frame_title_format. */
11142 static void
11143 x_consider_frame_title (Lisp_Object frame)
11145 struct frame *f = XFRAME (frame);
11147 if (FRAME_WINDOW_P (f)
11148 || FRAME_MINIBUF_ONLY_P (f)
11149 || f->explicit_name)
11151 /* Do we have more than one visible frame on this X display? */
11152 Lisp_Object tail, other_frame, fmt;
11153 ptrdiff_t title_start;
11154 char *title;
11155 ptrdiff_t len;
11156 struct it it;
11157 ptrdiff_t count = SPECPDL_INDEX ();
11159 FOR_EACH_FRAME (tail, other_frame)
11161 struct frame *tf = XFRAME (other_frame);
11163 if (tf != f
11164 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11165 && !FRAME_MINIBUF_ONLY_P (tf)
11166 && !EQ (other_frame, tip_frame)
11167 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11168 break;
11171 /* Set global variable indicating that multiple frames exist. */
11172 multiple_frames = CONSP (tail);
11174 /* Switch to the buffer of selected window of the frame. Set up
11175 mode_line_target so that display_mode_element will output into
11176 mode_line_noprop_buf; then display the title. */
11177 record_unwind_protect (unwind_format_mode_line,
11178 format_mode_line_unwind_data
11179 (f, current_buffer, selected_window, 0));
11181 Fselect_window (f->selected_window, Qt);
11182 set_buffer_internal_1
11183 (XBUFFER (XWINDOW (f->selected_window)->contents));
11184 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11186 mode_line_target = MODE_LINE_TITLE;
11187 title_start = MODE_LINE_NOPROP_LEN (0);
11188 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11189 NULL, DEFAULT_FACE_ID);
11190 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11191 len = MODE_LINE_NOPROP_LEN (title_start);
11192 title = mode_line_noprop_buf + title_start;
11193 unbind_to (count, Qnil);
11195 /* Set the title only if it's changed. This avoids consing in
11196 the common case where it hasn't. (If it turns out that we've
11197 already wasted too much time by walking through the list with
11198 display_mode_element, then we might need to optimize at a
11199 higher level than this.) */
11200 if (! STRINGP (f->name)
11201 || SBYTES (f->name) != len
11202 || memcmp (title, SDATA (f->name), len) != 0)
11203 x_implicitly_set_name (f, make_string (title, len), Qnil);
11207 #endif /* not HAVE_WINDOW_SYSTEM */
11210 /***********************************************************************
11211 Menu Bars
11212 ***********************************************************************/
11215 /* Prepare for redisplay by updating menu-bar item lists when
11216 appropriate. This can call eval. */
11218 void
11219 prepare_menu_bars (void)
11221 int all_windows;
11222 struct gcpro gcpro1, gcpro2;
11223 struct frame *f;
11224 Lisp_Object tooltip_frame;
11226 #ifdef HAVE_WINDOW_SYSTEM
11227 tooltip_frame = tip_frame;
11228 #else
11229 tooltip_frame = Qnil;
11230 #endif
11232 /* Update all frame titles based on their buffer names, etc. We do
11233 this before the menu bars so that the buffer-menu will show the
11234 up-to-date frame titles. */
11235 #ifdef HAVE_WINDOW_SYSTEM
11236 if (windows_or_buffers_changed || update_mode_lines)
11238 Lisp_Object tail, frame;
11240 FOR_EACH_FRAME (tail, frame)
11242 f = XFRAME (frame);
11243 if (!EQ (frame, tooltip_frame)
11244 && (FRAME_ICONIFIED_P (f)
11245 || FRAME_VISIBLE_P (f) == 1
11246 /* Exclude TTY frames that are obscured because they
11247 are not the top frame on their console. This is
11248 because x_consider_frame_title actually switches
11249 to the frame, which for TTY frames means it is
11250 marked as garbaged, and will be completely
11251 redrawn on the next redisplay cycle. This causes
11252 TTY frames to be completely redrawn, when there
11253 are more than one of them, even though nothing
11254 should be changed on display. */
11255 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11256 x_consider_frame_title (frame);
11259 #endif /* HAVE_WINDOW_SYSTEM */
11261 /* Update the menu bar item lists, if appropriate. This has to be
11262 done before any actual redisplay or generation of display lines. */
11263 all_windows = (update_mode_lines
11264 || buffer_shared_and_changed ()
11265 || windows_or_buffers_changed);
11266 if (all_windows)
11268 Lisp_Object tail, frame;
11269 ptrdiff_t count = SPECPDL_INDEX ();
11270 /* 1 means that update_menu_bar has run its hooks
11271 so any further calls to update_menu_bar shouldn't do so again. */
11272 int menu_bar_hooks_run = 0;
11274 record_unwind_save_match_data ();
11276 FOR_EACH_FRAME (tail, frame)
11278 f = XFRAME (frame);
11280 /* Ignore tooltip frame. */
11281 if (EQ (frame, tooltip_frame))
11282 continue;
11284 /* If a window on this frame changed size, report that to
11285 the user and clear the size-change flag. */
11286 if (FRAME_WINDOW_SIZES_CHANGED (f))
11288 Lisp_Object functions;
11290 /* Clear flag first in case we get an error below. */
11291 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11292 functions = Vwindow_size_change_functions;
11293 GCPRO2 (tail, functions);
11295 while (CONSP (functions))
11297 if (!EQ (XCAR (functions), Qt))
11298 call1 (XCAR (functions), frame);
11299 functions = XCDR (functions);
11301 UNGCPRO;
11304 GCPRO1 (tail);
11305 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11306 #ifdef HAVE_WINDOW_SYSTEM
11307 update_tool_bar (f, 0);
11308 #endif
11309 #ifdef HAVE_NS
11310 if (windows_or_buffers_changed
11311 && FRAME_NS_P (f))
11312 ns_set_doc_edited
11313 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11314 #endif
11315 UNGCPRO;
11318 unbind_to (count, Qnil);
11320 else
11322 struct frame *sf = SELECTED_FRAME ();
11323 update_menu_bar (sf, 1, 0);
11324 #ifdef HAVE_WINDOW_SYSTEM
11325 update_tool_bar (sf, 1);
11326 #endif
11331 /* Update the menu bar item list for frame F. This has to be done
11332 before we start to fill in any display lines, because it can call
11333 eval.
11335 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11337 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11338 already ran the menu bar hooks for this redisplay, so there
11339 is no need to run them again. The return value is the
11340 updated value of this flag, to pass to the next call. */
11342 static int
11343 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11345 Lisp_Object window;
11346 register struct window *w;
11348 /* If called recursively during a menu update, do nothing. This can
11349 happen when, for instance, an activate-menubar-hook causes a
11350 redisplay. */
11351 if (inhibit_menubar_update)
11352 return hooks_run;
11354 window = FRAME_SELECTED_WINDOW (f);
11355 w = XWINDOW (window);
11357 if (FRAME_WINDOW_P (f)
11359 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11360 || defined (HAVE_NS) || defined (USE_GTK)
11361 FRAME_EXTERNAL_MENU_BAR (f)
11362 #else
11363 FRAME_MENU_BAR_LINES (f) > 0
11364 #endif
11365 : FRAME_MENU_BAR_LINES (f) > 0)
11367 /* If the user has switched buffers or windows, we need to
11368 recompute to reflect the new bindings. But we'll
11369 recompute when update_mode_lines is set too; that means
11370 that people can use force-mode-line-update to request
11371 that the menu bar be recomputed. The adverse effect on
11372 the rest of the redisplay algorithm is about the same as
11373 windows_or_buffers_changed anyway. */
11374 if (windows_or_buffers_changed
11375 /* This used to test w->update_mode_line, but we believe
11376 there is no need to recompute the menu in that case. */
11377 || update_mode_lines
11378 || window_buffer_changed (w))
11380 struct buffer *prev = current_buffer;
11381 ptrdiff_t count = SPECPDL_INDEX ();
11383 specbind (Qinhibit_menubar_update, Qt);
11385 set_buffer_internal_1 (XBUFFER (w->contents));
11386 if (save_match_data)
11387 record_unwind_save_match_data ();
11388 if (NILP (Voverriding_local_map_menu_flag))
11390 specbind (Qoverriding_terminal_local_map, Qnil);
11391 specbind (Qoverriding_local_map, Qnil);
11394 if (!hooks_run)
11396 /* Run the Lucid hook. */
11397 safe_run_hooks (Qactivate_menubar_hook);
11399 /* If it has changed current-menubar from previous value,
11400 really recompute the menu-bar from the value. */
11401 if (! NILP (Vlucid_menu_bar_dirty_flag))
11402 call0 (Qrecompute_lucid_menubar);
11404 safe_run_hooks (Qmenu_bar_update_hook);
11406 hooks_run = 1;
11409 XSETFRAME (Vmenu_updating_frame, f);
11410 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11412 /* Redisplay the menu bar in case we changed it. */
11413 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11414 || defined (HAVE_NS) || defined (USE_GTK)
11415 if (FRAME_WINDOW_P (f))
11417 #if defined (HAVE_NS)
11418 /* All frames on Mac OS share the same menubar. So only
11419 the selected frame should be allowed to set it. */
11420 if (f == SELECTED_FRAME ())
11421 #endif
11422 set_frame_menubar (f, 0, 0);
11424 else
11425 /* On a terminal screen, the menu bar is an ordinary screen
11426 line, and this makes it get updated. */
11427 w->update_mode_line = 1;
11428 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11429 /* In the non-toolkit version, the menu bar is an ordinary screen
11430 line, and this makes it get updated. */
11431 w->update_mode_line = 1;
11432 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11434 unbind_to (count, Qnil);
11435 set_buffer_internal_1 (prev);
11439 return hooks_run;
11444 /***********************************************************************
11445 Output Cursor
11446 ***********************************************************************/
11448 #ifdef HAVE_WINDOW_SYSTEM
11450 /* EXPORT:
11451 Nominal cursor position -- where to draw output.
11452 HPOS and VPOS are window relative glyph matrix coordinates.
11453 X and Y are window relative pixel coordinates. */
11455 struct cursor_pos output_cursor;
11458 /* EXPORT:
11459 Set the global variable output_cursor to CURSOR. All cursor
11460 positions are relative to currently updated window. */
11462 void
11463 set_output_cursor (struct cursor_pos *cursor)
11465 output_cursor.hpos = cursor->hpos;
11466 output_cursor.vpos = cursor->vpos;
11467 output_cursor.x = cursor->x;
11468 output_cursor.y = cursor->y;
11472 /* EXPORT for RIF:
11473 Set a nominal cursor position.
11475 HPOS and VPOS are column/row positions in a window glyph matrix.
11476 X and Y are window text area relative pixel positions.
11478 This is always done during window update, so the position is the
11479 future output cursor position for currently updated window W.
11480 NOTE: W is used only to check whether this function is called
11481 in a consistent manner via the redisplay interface. */
11483 void
11484 x_cursor_to (struct window *w, int vpos, int hpos, int y, int x)
11486 eassert (w);
11488 /* Set the output cursor. */
11489 output_cursor.hpos = hpos;
11490 output_cursor.vpos = vpos;
11491 output_cursor.x = x;
11492 output_cursor.y = y;
11495 #endif /* HAVE_WINDOW_SYSTEM */
11498 /***********************************************************************
11499 Tool-bars
11500 ***********************************************************************/
11502 #ifdef HAVE_WINDOW_SYSTEM
11504 /* Where the mouse was last time we reported a mouse event. */
11506 struct frame *last_mouse_frame;
11508 /* Tool-bar item index of the item on which a mouse button was pressed
11509 or -1. */
11511 int last_tool_bar_item;
11513 /* Select `frame' temporarily without running all the code in
11514 do_switch_frame.
11515 FIXME: Maybe do_switch_frame should be trimmed down similarly
11516 when `norecord' is set. */
11517 static void
11518 fast_set_selected_frame (Lisp_Object frame)
11520 if (!EQ (selected_frame, frame))
11522 selected_frame = frame;
11523 selected_window = XFRAME (frame)->selected_window;
11527 /* Update the tool-bar item list for frame F. This has to be done
11528 before we start to fill in any display lines. Called from
11529 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11530 and restore it here. */
11532 static void
11533 update_tool_bar (struct frame *f, int save_match_data)
11535 #if defined (USE_GTK) || defined (HAVE_NS)
11536 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11537 #else
11538 int do_update = WINDOWP (f->tool_bar_window)
11539 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11540 #endif
11542 if (do_update)
11544 Lisp_Object window;
11545 struct window *w;
11547 window = FRAME_SELECTED_WINDOW (f);
11548 w = XWINDOW (window);
11550 /* If the user has switched buffers or windows, we need to
11551 recompute to reflect the new bindings. But we'll
11552 recompute when update_mode_lines is set too; that means
11553 that people can use force-mode-line-update to request
11554 that the menu bar be recomputed. The adverse effect on
11555 the rest of the redisplay algorithm is about the same as
11556 windows_or_buffers_changed anyway. */
11557 if (windows_or_buffers_changed
11558 || w->update_mode_line
11559 || update_mode_lines
11560 || window_buffer_changed (w))
11562 struct buffer *prev = current_buffer;
11563 ptrdiff_t count = SPECPDL_INDEX ();
11564 Lisp_Object frame, new_tool_bar;
11565 int new_n_tool_bar;
11566 struct gcpro gcpro1;
11568 /* Set current_buffer to the buffer of the selected
11569 window of the frame, so that we get the right local
11570 keymaps. */
11571 set_buffer_internal_1 (XBUFFER (w->contents));
11573 /* Save match data, if we must. */
11574 if (save_match_data)
11575 record_unwind_save_match_data ();
11577 /* Make sure that we don't accidentally use bogus keymaps. */
11578 if (NILP (Voverriding_local_map_menu_flag))
11580 specbind (Qoverriding_terminal_local_map, Qnil);
11581 specbind (Qoverriding_local_map, Qnil);
11584 GCPRO1 (new_tool_bar);
11586 /* We must temporarily set the selected frame to this frame
11587 before calling tool_bar_items, because the calculation of
11588 the tool-bar keymap uses the selected frame (see
11589 `tool-bar-make-keymap' in tool-bar.el). */
11590 eassert (EQ (selected_window,
11591 /* Since we only explicitly preserve selected_frame,
11592 check that selected_window would be redundant. */
11593 XFRAME (selected_frame)->selected_window));
11594 record_unwind_protect (fast_set_selected_frame, selected_frame);
11595 XSETFRAME (frame, f);
11596 fast_set_selected_frame (frame);
11598 /* Build desired tool-bar items from keymaps. */
11599 new_tool_bar
11600 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11601 &new_n_tool_bar);
11603 /* Redisplay the tool-bar if we changed it. */
11604 if (new_n_tool_bar != f->n_tool_bar_items
11605 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11607 /* Redisplay that happens asynchronously due to an expose event
11608 may access f->tool_bar_items. Make sure we update both
11609 variables within BLOCK_INPUT so no such event interrupts. */
11610 block_input ();
11611 fset_tool_bar_items (f, new_tool_bar);
11612 f->n_tool_bar_items = new_n_tool_bar;
11613 w->update_mode_line = 1;
11614 unblock_input ();
11617 UNGCPRO;
11619 unbind_to (count, Qnil);
11620 set_buffer_internal_1 (prev);
11626 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11627 F's desired tool-bar contents. F->tool_bar_items must have
11628 been set up previously by calling prepare_menu_bars. */
11630 static void
11631 build_desired_tool_bar_string (struct frame *f)
11633 int i, size, size_needed;
11634 struct gcpro gcpro1, gcpro2, gcpro3;
11635 Lisp_Object image, plist, props;
11637 image = plist = props = Qnil;
11638 GCPRO3 (image, plist, props);
11640 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11641 Otherwise, make a new string. */
11643 /* The size of the string we might be able to reuse. */
11644 size = (STRINGP (f->desired_tool_bar_string)
11645 ? SCHARS (f->desired_tool_bar_string)
11646 : 0);
11648 /* We need one space in the string for each image. */
11649 size_needed = f->n_tool_bar_items;
11651 /* Reuse f->desired_tool_bar_string, if possible. */
11652 if (size < size_needed || NILP (f->desired_tool_bar_string))
11653 fset_desired_tool_bar_string
11654 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11655 else
11657 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11658 Fremove_text_properties (make_number (0), make_number (size),
11659 props, f->desired_tool_bar_string);
11662 /* Put a `display' property on the string for the images to display,
11663 put a `menu_item' property on tool-bar items with a value that
11664 is the index of the item in F's tool-bar item vector. */
11665 for (i = 0; i < f->n_tool_bar_items; ++i)
11667 #define PROP(IDX) \
11668 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11670 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11671 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11672 int hmargin, vmargin, relief, idx, end;
11674 /* If image is a vector, choose the image according to the
11675 button state. */
11676 image = PROP (TOOL_BAR_ITEM_IMAGES);
11677 if (VECTORP (image))
11679 if (enabled_p)
11680 idx = (selected_p
11681 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11682 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11683 else
11684 idx = (selected_p
11685 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11686 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11688 eassert (ASIZE (image) >= idx);
11689 image = AREF (image, idx);
11691 else
11692 idx = -1;
11694 /* Ignore invalid image specifications. */
11695 if (!valid_image_p (image))
11696 continue;
11698 /* Display the tool-bar button pressed, or depressed. */
11699 plist = Fcopy_sequence (XCDR (image));
11701 /* Compute margin and relief to draw. */
11702 relief = (tool_bar_button_relief >= 0
11703 ? tool_bar_button_relief
11704 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11705 hmargin = vmargin = relief;
11707 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11708 INT_MAX - max (hmargin, vmargin)))
11710 hmargin += XFASTINT (Vtool_bar_button_margin);
11711 vmargin += XFASTINT (Vtool_bar_button_margin);
11713 else if (CONSP (Vtool_bar_button_margin))
11715 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11716 INT_MAX - hmargin))
11717 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11719 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11720 INT_MAX - vmargin))
11721 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11724 if (auto_raise_tool_bar_buttons_p)
11726 /* Add a `:relief' property to the image spec if the item is
11727 selected. */
11728 if (selected_p)
11730 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11731 hmargin -= relief;
11732 vmargin -= relief;
11735 else
11737 /* If image is selected, display it pressed, i.e. with a
11738 negative relief. If it's not selected, display it with a
11739 raised relief. */
11740 plist = Fplist_put (plist, QCrelief,
11741 (selected_p
11742 ? make_number (-relief)
11743 : make_number (relief)));
11744 hmargin -= relief;
11745 vmargin -= relief;
11748 /* Put a margin around the image. */
11749 if (hmargin || vmargin)
11751 if (hmargin == vmargin)
11752 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11753 else
11754 plist = Fplist_put (plist, QCmargin,
11755 Fcons (make_number (hmargin),
11756 make_number (vmargin)));
11759 /* If button is not enabled, and we don't have special images
11760 for the disabled state, make the image appear disabled by
11761 applying an appropriate algorithm to it. */
11762 if (!enabled_p && idx < 0)
11763 plist = Fplist_put (plist, QCconversion, Qdisabled);
11765 /* Put a `display' text property on the string for the image to
11766 display. Put a `menu-item' property on the string that gives
11767 the start of this item's properties in the tool-bar items
11768 vector. */
11769 image = Fcons (Qimage, plist);
11770 props = list4 (Qdisplay, image,
11771 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11773 /* Let the last image hide all remaining spaces in the tool bar
11774 string. The string can be longer than needed when we reuse a
11775 previous string. */
11776 if (i + 1 == f->n_tool_bar_items)
11777 end = SCHARS (f->desired_tool_bar_string);
11778 else
11779 end = i + 1;
11780 Fadd_text_properties (make_number (i), make_number (end),
11781 props, f->desired_tool_bar_string);
11782 #undef PROP
11785 UNGCPRO;
11789 /* Display one line of the tool-bar of frame IT->f.
11791 HEIGHT specifies the desired height of the tool-bar line.
11792 If the actual height of the glyph row is less than HEIGHT, the
11793 row's height is increased to HEIGHT, and the icons are centered
11794 vertically in the new height.
11796 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11797 count a final empty row in case the tool-bar width exactly matches
11798 the window width.
11801 static void
11802 display_tool_bar_line (struct it *it, int height)
11804 struct glyph_row *row = it->glyph_row;
11805 int max_x = it->last_visible_x;
11806 struct glyph *last;
11808 prepare_desired_row (row);
11809 row->y = it->current_y;
11811 /* Note that this isn't made use of if the face hasn't a box,
11812 so there's no need to check the face here. */
11813 it->start_of_box_run_p = 1;
11815 while (it->current_x < max_x)
11817 int x, n_glyphs_before, i, nglyphs;
11818 struct it it_before;
11820 /* Get the next display element. */
11821 if (!get_next_display_element (it))
11823 /* Don't count empty row if we are counting needed tool-bar lines. */
11824 if (height < 0 && !it->hpos)
11825 return;
11826 break;
11829 /* Produce glyphs. */
11830 n_glyphs_before = row->used[TEXT_AREA];
11831 it_before = *it;
11833 PRODUCE_GLYPHS (it);
11835 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11836 i = 0;
11837 x = it_before.current_x;
11838 while (i < nglyphs)
11840 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11842 if (x + glyph->pixel_width > max_x)
11844 /* Glyph doesn't fit on line. Backtrack. */
11845 row->used[TEXT_AREA] = n_glyphs_before;
11846 *it = it_before;
11847 /* If this is the only glyph on this line, it will never fit on the
11848 tool-bar, so skip it. But ensure there is at least one glyph,
11849 so we don't accidentally disable the tool-bar. */
11850 if (n_glyphs_before == 0
11851 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11852 break;
11853 goto out;
11856 ++it->hpos;
11857 x += glyph->pixel_width;
11858 ++i;
11861 /* Stop at line end. */
11862 if (ITERATOR_AT_END_OF_LINE_P (it))
11863 break;
11865 set_iterator_to_next (it, 1);
11868 out:;
11870 row->displays_text_p = row->used[TEXT_AREA] != 0;
11872 /* Use default face for the border below the tool bar.
11874 FIXME: When auto-resize-tool-bars is grow-only, there is
11875 no additional border below the possibly empty tool-bar lines.
11876 So to make the extra empty lines look "normal", we have to
11877 use the tool-bar face for the border too. */
11878 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11879 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11880 it->face_id = DEFAULT_FACE_ID;
11882 extend_face_to_end_of_line (it);
11883 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11884 last->right_box_line_p = 1;
11885 if (last == row->glyphs[TEXT_AREA])
11886 last->left_box_line_p = 1;
11888 /* Make line the desired height and center it vertically. */
11889 if ((height -= it->max_ascent + it->max_descent) > 0)
11891 /* Don't add more than one line height. */
11892 height %= FRAME_LINE_HEIGHT (it->f);
11893 it->max_ascent += height / 2;
11894 it->max_descent += (height + 1) / 2;
11897 compute_line_metrics (it);
11899 /* If line is empty, make it occupy the rest of the tool-bar. */
11900 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11902 row->height = row->phys_height = it->last_visible_y - row->y;
11903 row->visible_height = row->height;
11904 row->ascent = row->phys_ascent = 0;
11905 row->extra_line_spacing = 0;
11908 row->full_width_p = 1;
11909 row->continued_p = 0;
11910 row->truncated_on_left_p = 0;
11911 row->truncated_on_right_p = 0;
11913 it->current_x = it->hpos = 0;
11914 it->current_y += row->height;
11915 ++it->vpos;
11916 ++it->glyph_row;
11920 /* Max tool-bar height. */
11922 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11923 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11925 /* Value is the number of screen lines needed to make all tool-bar
11926 items of frame F visible. The number of actual rows needed is
11927 returned in *N_ROWS if non-NULL. */
11929 static int
11930 tool_bar_lines_needed (struct frame *f, int *n_rows)
11932 struct window *w = XWINDOW (f->tool_bar_window);
11933 struct it it;
11934 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11935 the desired matrix, so use (unused) mode-line row as temporary row to
11936 avoid destroying the first tool-bar row. */
11937 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11939 /* Initialize an iterator for iteration over
11940 F->desired_tool_bar_string in the tool-bar window of frame F. */
11941 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11942 it.first_visible_x = 0;
11943 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11944 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11945 it.paragraph_embedding = L2R;
11947 while (!ITERATOR_AT_END_P (&it))
11949 clear_glyph_row (temp_row);
11950 it.glyph_row = temp_row;
11951 display_tool_bar_line (&it, -1);
11953 clear_glyph_row (temp_row);
11955 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11956 if (n_rows)
11957 *n_rows = it.vpos > 0 ? it.vpos : -1;
11959 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11963 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11964 0, 1, 0,
11965 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11966 If FRAME is nil or omitted, use the selected frame. */)
11967 (Lisp_Object frame)
11969 struct frame *f = decode_any_frame (frame);
11970 struct window *w;
11971 int nlines = 0;
11973 if (WINDOWP (f->tool_bar_window)
11974 && (w = XWINDOW (f->tool_bar_window),
11975 WINDOW_TOTAL_LINES (w) > 0))
11977 update_tool_bar (f, 1);
11978 if (f->n_tool_bar_items)
11980 build_desired_tool_bar_string (f);
11981 nlines = tool_bar_lines_needed (f, NULL);
11985 return make_number (nlines);
11989 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11990 height should be changed. */
11992 static int
11993 redisplay_tool_bar (struct frame *f)
11995 struct window *w;
11996 struct it it;
11997 struct glyph_row *row;
11999 #if defined (USE_GTK) || defined (HAVE_NS)
12000 if (FRAME_EXTERNAL_TOOL_BAR (f))
12001 update_frame_tool_bar (f);
12002 return 0;
12003 #endif
12005 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12006 do anything. This means you must start with tool-bar-lines
12007 non-zero to get the auto-sizing effect. Or in other words, you
12008 can turn off tool-bars by specifying tool-bar-lines zero. */
12009 if (!WINDOWP (f->tool_bar_window)
12010 || (w = XWINDOW (f->tool_bar_window),
12011 WINDOW_TOTAL_LINES (w) == 0))
12012 return 0;
12014 /* Set up an iterator for the tool-bar window. */
12015 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12016 it.first_visible_x = 0;
12017 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12018 row = it.glyph_row;
12020 /* Build a string that represents the contents of the tool-bar. */
12021 build_desired_tool_bar_string (f);
12022 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12023 /* FIXME: This should be controlled by a user option. But it
12024 doesn't make sense to have an R2L tool bar if the menu bar cannot
12025 be drawn also R2L, and making the menu bar R2L is tricky due
12026 toolkit-specific code that implements it. If an R2L tool bar is
12027 ever supported, display_tool_bar_line should also be augmented to
12028 call unproduce_glyphs like display_line and display_string
12029 do. */
12030 it.paragraph_embedding = L2R;
12032 if (f->n_tool_bar_rows == 0)
12034 int nlines;
12036 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12037 nlines != WINDOW_TOTAL_LINES (w)))
12039 Lisp_Object frame;
12040 int old_height = WINDOW_TOTAL_LINES (w);
12042 XSETFRAME (frame, f);
12043 Fmodify_frame_parameters (frame,
12044 list1 (Fcons (Qtool_bar_lines,
12045 make_number (nlines))));
12046 if (WINDOW_TOTAL_LINES (w) != old_height)
12048 clear_glyph_matrix (w->desired_matrix);
12049 fonts_changed_p = 1;
12050 return 1;
12055 /* Display as many lines as needed to display all tool-bar items. */
12057 if (f->n_tool_bar_rows > 0)
12059 int border, rows, height, extra;
12061 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12062 border = XINT (Vtool_bar_border);
12063 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12064 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12065 else if (EQ (Vtool_bar_border, Qborder_width))
12066 border = f->border_width;
12067 else
12068 border = 0;
12069 if (border < 0)
12070 border = 0;
12072 rows = f->n_tool_bar_rows;
12073 height = max (1, (it.last_visible_y - border) / rows);
12074 extra = it.last_visible_y - border - height * rows;
12076 while (it.current_y < it.last_visible_y)
12078 int h = 0;
12079 if (extra > 0 && rows-- > 0)
12081 h = (extra + rows - 1) / rows;
12082 extra -= h;
12084 display_tool_bar_line (&it, height + h);
12087 else
12089 while (it.current_y < it.last_visible_y)
12090 display_tool_bar_line (&it, 0);
12093 /* It doesn't make much sense to try scrolling in the tool-bar
12094 window, so don't do it. */
12095 w->desired_matrix->no_scrolling_p = 1;
12096 w->must_be_updated_p = 1;
12098 if (!NILP (Vauto_resize_tool_bars))
12100 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12101 int change_height_p = 0;
12103 /* If we couldn't display everything, change the tool-bar's
12104 height if there is room for more. */
12105 if (IT_STRING_CHARPOS (it) < it.end_charpos
12106 && it.current_y < max_tool_bar_height)
12107 change_height_p = 1;
12109 row = it.glyph_row - 1;
12111 /* If there are blank lines at the end, except for a partially
12112 visible blank line at the end that is smaller than
12113 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12114 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12115 && row->height >= FRAME_LINE_HEIGHT (f))
12116 change_height_p = 1;
12118 /* If row displays tool-bar items, but is partially visible,
12119 change the tool-bar's height. */
12120 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12121 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12122 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12123 change_height_p = 1;
12125 /* Resize windows as needed by changing the `tool-bar-lines'
12126 frame parameter. */
12127 if (change_height_p)
12129 Lisp_Object frame;
12130 int old_height = WINDOW_TOTAL_LINES (w);
12131 int nrows;
12132 int nlines = tool_bar_lines_needed (f, &nrows);
12134 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12135 && !f->minimize_tool_bar_window_p)
12136 ? (nlines > old_height)
12137 : (nlines != old_height));
12138 f->minimize_tool_bar_window_p = 0;
12140 if (change_height_p)
12142 XSETFRAME (frame, f);
12143 Fmodify_frame_parameters (frame,
12144 list1 (Fcons (Qtool_bar_lines,
12145 make_number (nlines))));
12146 if (WINDOW_TOTAL_LINES (w) != old_height)
12148 clear_glyph_matrix (w->desired_matrix);
12149 f->n_tool_bar_rows = nrows;
12150 fonts_changed_p = 1;
12151 return 1;
12157 f->minimize_tool_bar_window_p = 0;
12158 return 0;
12162 /* Get information about the tool-bar item which is displayed in GLYPH
12163 on frame F. Return in *PROP_IDX the index where tool-bar item
12164 properties start in F->tool_bar_items. Value is zero if
12165 GLYPH doesn't display a tool-bar item. */
12167 static int
12168 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12170 Lisp_Object prop;
12171 int success_p;
12172 int charpos;
12174 /* This function can be called asynchronously, which means we must
12175 exclude any possibility that Fget_text_property signals an
12176 error. */
12177 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12178 charpos = max (0, charpos);
12180 /* Get the text property `menu-item' at pos. The value of that
12181 property is the start index of this item's properties in
12182 F->tool_bar_items. */
12183 prop = Fget_text_property (make_number (charpos),
12184 Qmenu_item, f->current_tool_bar_string);
12185 if (INTEGERP (prop))
12187 *prop_idx = XINT (prop);
12188 success_p = 1;
12190 else
12191 success_p = 0;
12193 return success_p;
12197 /* Get information about the tool-bar item at position X/Y on frame F.
12198 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12199 the current matrix of the tool-bar window of F, or NULL if not
12200 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12201 item in F->tool_bar_items. Value is
12203 -1 if X/Y is not on a tool-bar item
12204 0 if X/Y is on the same item that was highlighted before.
12205 1 otherwise. */
12207 static int
12208 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12209 int *hpos, int *vpos, int *prop_idx)
12211 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12212 struct window *w = XWINDOW (f->tool_bar_window);
12213 int area;
12215 /* Find the glyph under X/Y. */
12216 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12217 if (*glyph == NULL)
12218 return -1;
12220 /* Get the start of this tool-bar item's properties in
12221 f->tool_bar_items. */
12222 if (!tool_bar_item_info (f, *glyph, prop_idx))
12223 return -1;
12225 /* Is mouse on the highlighted item? */
12226 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12227 && *vpos >= hlinfo->mouse_face_beg_row
12228 && *vpos <= hlinfo->mouse_face_end_row
12229 && (*vpos > hlinfo->mouse_face_beg_row
12230 || *hpos >= hlinfo->mouse_face_beg_col)
12231 && (*vpos < hlinfo->mouse_face_end_row
12232 || *hpos < hlinfo->mouse_face_end_col
12233 || hlinfo->mouse_face_past_end))
12234 return 0;
12236 return 1;
12240 /* EXPORT:
12241 Handle mouse button event on the tool-bar of frame F, at
12242 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12243 0 for button release. MODIFIERS is event modifiers for button
12244 release. */
12246 void
12247 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12248 int modifiers)
12250 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12251 struct window *w = XWINDOW (f->tool_bar_window);
12252 int hpos, vpos, prop_idx;
12253 struct glyph *glyph;
12254 Lisp_Object enabled_p;
12255 int ts;
12257 /* If not on the highlighted tool-bar item, and mouse-highlight is
12258 non-nil, return. This is so we generate the tool-bar button
12259 click only when the mouse button is released on the same item as
12260 where it was pressed. However, when mouse-highlight is disabled,
12261 generate the click when the button is released regardless of the
12262 highlight, since tool-bar items are not highlighted in that
12263 case. */
12264 frame_to_window_pixel_xy (w, &x, &y);
12265 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12266 if (ts == -1
12267 || (ts != 0 && !NILP (Vmouse_highlight)))
12268 return;
12270 /* When mouse-highlight is off, generate the click for the item
12271 where the button was pressed, disregarding where it was
12272 released. */
12273 if (NILP (Vmouse_highlight) && !down_p)
12274 prop_idx = last_tool_bar_item;
12276 /* If item is disabled, do nothing. */
12277 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12278 if (NILP (enabled_p))
12279 return;
12281 if (down_p)
12283 /* Show item in pressed state. */
12284 if (!NILP (Vmouse_highlight))
12285 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12286 last_tool_bar_item = prop_idx;
12288 else
12290 Lisp_Object key, frame;
12291 struct input_event event;
12292 EVENT_INIT (event);
12294 /* Show item in released state. */
12295 if (!NILP (Vmouse_highlight))
12296 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12298 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12300 XSETFRAME (frame, f);
12301 event.kind = TOOL_BAR_EVENT;
12302 event.frame_or_window = frame;
12303 event.arg = frame;
12304 kbd_buffer_store_event (&event);
12306 event.kind = TOOL_BAR_EVENT;
12307 event.frame_or_window = frame;
12308 event.arg = key;
12309 event.modifiers = modifiers;
12310 kbd_buffer_store_event (&event);
12311 last_tool_bar_item = -1;
12316 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12317 tool-bar window-relative coordinates X/Y. Called from
12318 note_mouse_highlight. */
12320 static void
12321 note_tool_bar_highlight (struct frame *f, int x, int y)
12323 Lisp_Object window = f->tool_bar_window;
12324 struct window *w = XWINDOW (window);
12325 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12326 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12327 int hpos, vpos;
12328 struct glyph *glyph;
12329 struct glyph_row *row;
12330 int i;
12331 Lisp_Object enabled_p;
12332 int prop_idx;
12333 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12334 int mouse_down_p, rc;
12336 /* Function note_mouse_highlight is called with negative X/Y
12337 values when mouse moves outside of the frame. */
12338 if (x <= 0 || y <= 0)
12340 clear_mouse_face (hlinfo);
12341 return;
12344 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12345 if (rc < 0)
12347 /* Not on tool-bar item. */
12348 clear_mouse_face (hlinfo);
12349 return;
12351 else if (rc == 0)
12352 /* On same tool-bar item as before. */
12353 goto set_help_echo;
12355 clear_mouse_face (hlinfo);
12357 /* Mouse is down, but on different tool-bar item? */
12358 mouse_down_p = (dpyinfo->grabbed
12359 && f == last_mouse_frame
12360 && FRAME_LIVE_P (f));
12361 if (mouse_down_p
12362 && last_tool_bar_item != prop_idx)
12363 return;
12365 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12367 /* If tool-bar item is not enabled, don't highlight it. */
12368 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12369 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12371 /* Compute the x-position of the glyph. In front and past the
12372 image is a space. We include this in the highlighted area. */
12373 row = MATRIX_ROW (w->current_matrix, vpos);
12374 for (i = x = 0; i < hpos; ++i)
12375 x += row->glyphs[TEXT_AREA][i].pixel_width;
12377 /* Record this as the current active region. */
12378 hlinfo->mouse_face_beg_col = hpos;
12379 hlinfo->mouse_face_beg_row = vpos;
12380 hlinfo->mouse_face_beg_x = x;
12381 hlinfo->mouse_face_beg_y = row->y;
12382 hlinfo->mouse_face_past_end = 0;
12384 hlinfo->mouse_face_end_col = hpos + 1;
12385 hlinfo->mouse_face_end_row = vpos;
12386 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12387 hlinfo->mouse_face_end_y = row->y;
12388 hlinfo->mouse_face_window = window;
12389 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12391 /* Display it as active. */
12392 show_mouse_face (hlinfo, draw);
12395 set_help_echo:
12397 /* Set help_echo_string to a help string to display for this tool-bar item.
12398 XTread_socket does the rest. */
12399 help_echo_object = help_echo_window = Qnil;
12400 help_echo_pos = -1;
12401 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12402 if (NILP (help_echo_string))
12403 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12406 #endif /* HAVE_WINDOW_SYSTEM */
12410 /************************************************************************
12411 Horizontal scrolling
12412 ************************************************************************/
12414 static int hscroll_window_tree (Lisp_Object);
12415 static int hscroll_windows (Lisp_Object);
12417 /* For all leaf windows in the window tree rooted at WINDOW, set their
12418 hscroll value so that PT is (i) visible in the window, and (ii) so
12419 that it is not within a certain margin at the window's left and
12420 right border. Value is non-zero if any window's hscroll has been
12421 changed. */
12423 static int
12424 hscroll_window_tree (Lisp_Object window)
12426 int hscrolled_p = 0;
12427 int hscroll_relative_p = FLOATP (Vhscroll_step);
12428 int hscroll_step_abs = 0;
12429 double hscroll_step_rel = 0;
12431 if (hscroll_relative_p)
12433 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12434 if (hscroll_step_rel < 0)
12436 hscroll_relative_p = 0;
12437 hscroll_step_abs = 0;
12440 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12442 hscroll_step_abs = XINT (Vhscroll_step);
12443 if (hscroll_step_abs < 0)
12444 hscroll_step_abs = 0;
12446 else
12447 hscroll_step_abs = 0;
12449 while (WINDOWP (window))
12451 struct window *w = XWINDOW (window);
12453 if (WINDOWP (w->contents))
12454 hscrolled_p |= hscroll_window_tree (w->contents);
12455 else if (w->cursor.vpos >= 0)
12457 int h_margin;
12458 int text_area_width;
12459 struct glyph_row *current_cursor_row
12460 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12461 struct glyph_row *desired_cursor_row
12462 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12463 struct glyph_row *cursor_row
12464 = (desired_cursor_row->enabled_p
12465 ? desired_cursor_row
12466 : current_cursor_row);
12467 int row_r2l_p = cursor_row->reversed_p;
12469 text_area_width = window_box_width (w, TEXT_AREA);
12471 /* Scroll when cursor is inside this scroll margin. */
12472 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12474 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12475 /* For left-to-right rows, hscroll when cursor is either
12476 (i) inside the right hscroll margin, or (ii) if it is
12477 inside the left margin and the window is already
12478 hscrolled. */
12479 && ((!row_r2l_p
12480 && ((w->hscroll
12481 && w->cursor.x <= h_margin)
12482 || (cursor_row->enabled_p
12483 && cursor_row->truncated_on_right_p
12484 && (w->cursor.x >= text_area_width - h_margin))))
12485 /* For right-to-left rows, the logic is similar,
12486 except that rules for scrolling to left and right
12487 are reversed. E.g., if cursor.x <= h_margin, we
12488 need to hscroll "to the right" unconditionally,
12489 and that will scroll the screen to the left so as
12490 to reveal the next portion of the row. */
12491 || (row_r2l_p
12492 && ((cursor_row->enabled_p
12493 /* FIXME: It is confusing to set the
12494 truncated_on_right_p flag when R2L rows
12495 are actually truncated on the left. */
12496 && cursor_row->truncated_on_right_p
12497 && w->cursor.x <= h_margin)
12498 || (w->hscroll
12499 && (w->cursor.x >= text_area_width - h_margin))))))
12501 struct it it;
12502 ptrdiff_t hscroll;
12503 struct buffer *saved_current_buffer;
12504 ptrdiff_t pt;
12505 int wanted_x;
12507 /* Find point in a display of infinite width. */
12508 saved_current_buffer = current_buffer;
12509 current_buffer = XBUFFER (w->contents);
12511 if (w == XWINDOW (selected_window))
12512 pt = PT;
12513 else
12514 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12516 /* Move iterator to pt starting at cursor_row->start in
12517 a line with infinite width. */
12518 init_to_row_start (&it, w, cursor_row);
12519 it.last_visible_x = INFINITY;
12520 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12521 current_buffer = saved_current_buffer;
12523 /* Position cursor in window. */
12524 if (!hscroll_relative_p && hscroll_step_abs == 0)
12525 hscroll = max (0, (it.current_x
12526 - (ITERATOR_AT_END_OF_LINE_P (&it)
12527 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12528 : (text_area_width / 2))))
12529 / FRAME_COLUMN_WIDTH (it.f);
12530 else if ((!row_r2l_p
12531 && w->cursor.x >= text_area_width - h_margin)
12532 || (row_r2l_p && w->cursor.x <= h_margin))
12534 if (hscroll_relative_p)
12535 wanted_x = text_area_width * (1 - hscroll_step_rel)
12536 - h_margin;
12537 else
12538 wanted_x = text_area_width
12539 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12540 - h_margin;
12541 hscroll
12542 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12544 else
12546 if (hscroll_relative_p)
12547 wanted_x = text_area_width * hscroll_step_rel
12548 + h_margin;
12549 else
12550 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12551 + h_margin;
12552 hscroll
12553 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12555 hscroll = max (hscroll, w->min_hscroll);
12557 /* Don't prevent redisplay optimizations if hscroll
12558 hasn't changed, as it will unnecessarily slow down
12559 redisplay. */
12560 if (w->hscroll != hscroll)
12562 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12563 w->hscroll = hscroll;
12564 hscrolled_p = 1;
12569 window = w->next;
12572 /* Value is non-zero if hscroll of any leaf window has been changed. */
12573 return hscrolled_p;
12577 /* Set hscroll so that cursor is visible and not inside horizontal
12578 scroll margins for all windows in the tree rooted at WINDOW. See
12579 also hscroll_window_tree above. Value is non-zero if any window's
12580 hscroll has been changed. If it has, desired matrices on the frame
12581 of WINDOW are cleared. */
12583 static int
12584 hscroll_windows (Lisp_Object window)
12586 int hscrolled_p = hscroll_window_tree (window);
12587 if (hscrolled_p)
12588 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12589 return hscrolled_p;
12594 /************************************************************************
12595 Redisplay
12596 ************************************************************************/
12598 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12599 to a non-zero value. This is sometimes handy to have in a debugger
12600 session. */
12602 #ifdef GLYPH_DEBUG
12604 /* First and last unchanged row for try_window_id. */
12606 static int debug_first_unchanged_at_end_vpos;
12607 static int debug_last_unchanged_at_beg_vpos;
12609 /* Delta vpos and y. */
12611 static int debug_dvpos, debug_dy;
12613 /* Delta in characters and bytes for try_window_id. */
12615 static ptrdiff_t debug_delta, debug_delta_bytes;
12617 /* Values of window_end_pos and window_end_vpos at the end of
12618 try_window_id. */
12620 static ptrdiff_t debug_end_vpos;
12622 /* Append a string to W->desired_matrix->method. FMT is a printf
12623 format string. If trace_redisplay_p is non-zero also printf the
12624 resulting string to stderr. */
12626 static void debug_method_add (struct window *, char const *, ...)
12627 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12629 static void
12630 debug_method_add (struct window *w, char const *fmt, ...)
12632 void *ptr = w;
12633 char *method = w->desired_matrix->method;
12634 int len = strlen (method);
12635 int size = sizeof w->desired_matrix->method;
12636 int remaining = size - len - 1;
12637 va_list ap;
12639 if (len && remaining)
12641 method[len] = '|';
12642 --remaining, ++len;
12645 va_start (ap, fmt);
12646 vsnprintf (method + len, remaining + 1, fmt, ap);
12647 va_end (ap);
12649 if (trace_redisplay_p)
12650 fprintf (stderr, "%p (%s): %s\n",
12651 ptr,
12652 ((BUFFERP (w->contents)
12653 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12654 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12655 : "no buffer"),
12656 method + len);
12659 #endif /* GLYPH_DEBUG */
12662 /* Value is non-zero if all changes in window W, which displays
12663 current_buffer, are in the text between START and END. START is a
12664 buffer position, END is given as a distance from Z. Used in
12665 redisplay_internal for display optimization. */
12667 static int
12668 text_outside_line_unchanged_p (struct window *w,
12669 ptrdiff_t start, ptrdiff_t end)
12671 int unchanged_p = 1;
12673 /* If text or overlays have changed, see where. */
12674 if (window_outdated (w))
12676 /* Gap in the line? */
12677 if (GPT < start || Z - GPT < end)
12678 unchanged_p = 0;
12680 /* Changes start in front of the line, or end after it? */
12681 if (unchanged_p
12682 && (BEG_UNCHANGED < start - 1
12683 || END_UNCHANGED < end))
12684 unchanged_p = 0;
12686 /* If selective display, can't optimize if changes start at the
12687 beginning of the line. */
12688 if (unchanged_p
12689 && INTEGERP (BVAR (current_buffer, selective_display))
12690 && XINT (BVAR (current_buffer, selective_display)) > 0
12691 && (BEG_UNCHANGED < start || GPT <= start))
12692 unchanged_p = 0;
12694 /* If there are overlays at the start or end of the line, these
12695 may have overlay strings with newlines in them. A change at
12696 START, for instance, may actually concern the display of such
12697 overlay strings as well, and they are displayed on different
12698 lines. So, quickly rule out this case. (For the future, it
12699 might be desirable to implement something more telling than
12700 just BEG/END_UNCHANGED.) */
12701 if (unchanged_p)
12703 if (BEG + BEG_UNCHANGED == start
12704 && overlay_touches_p (start))
12705 unchanged_p = 0;
12706 if (END_UNCHANGED == end
12707 && overlay_touches_p (Z - end))
12708 unchanged_p = 0;
12711 /* Under bidi reordering, adding or deleting a character in the
12712 beginning of a paragraph, before the first strong directional
12713 character, can change the base direction of the paragraph (unless
12714 the buffer specifies a fixed paragraph direction), which will
12715 require to redisplay the whole paragraph. It might be worthwhile
12716 to find the paragraph limits and widen the range of redisplayed
12717 lines to that, but for now just give up this optimization. */
12718 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12719 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12720 unchanged_p = 0;
12723 return unchanged_p;
12727 /* Do a frame update, taking possible shortcuts into account. This is
12728 the main external entry point for redisplay.
12730 If the last redisplay displayed an echo area message and that message
12731 is no longer requested, we clear the echo area or bring back the
12732 mini-buffer if that is in use. */
12734 void
12735 redisplay (void)
12737 redisplay_internal ();
12741 static Lisp_Object
12742 overlay_arrow_string_or_property (Lisp_Object var)
12744 Lisp_Object val;
12746 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12747 return val;
12749 return Voverlay_arrow_string;
12752 /* Return 1 if there are any overlay-arrows in current_buffer. */
12753 static int
12754 overlay_arrow_in_current_buffer_p (void)
12756 Lisp_Object vlist;
12758 for (vlist = Voverlay_arrow_variable_list;
12759 CONSP (vlist);
12760 vlist = XCDR (vlist))
12762 Lisp_Object var = XCAR (vlist);
12763 Lisp_Object val;
12765 if (!SYMBOLP (var))
12766 continue;
12767 val = find_symbol_value (var);
12768 if (MARKERP (val)
12769 && current_buffer == XMARKER (val)->buffer)
12770 return 1;
12772 return 0;
12776 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12777 has changed. */
12779 static int
12780 overlay_arrows_changed_p (void)
12782 Lisp_Object vlist;
12784 for (vlist = Voverlay_arrow_variable_list;
12785 CONSP (vlist);
12786 vlist = XCDR (vlist))
12788 Lisp_Object var = XCAR (vlist);
12789 Lisp_Object val, pstr;
12791 if (!SYMBOLP (var))
12792 continue;
12793 val = find_symbol_value (var);
12794 if (!MARKERP (val))
12795 continue;
12796 if (! EQ (COERCE_MARKER (val),
12797 Fget (var, Qlast_arrow_position))
12798 || ! (pstr = overlay_arrow_string_or_property (var),
12799 EQ (pstr, Fget (var, Qlast_arrow_string))))
12800 return 1;
12802 return 0;
12805 /* Mark overlay arrows to be updated on next redisplay. */
12807 static void
12808 update_overlay_arrows (int up_to_date)
12810 Lisp_Object vlist;
12812 for (vlist = Voverlay_arrow_variable_list;
12813 CONSP (vlist);
12814 vlist = XCDR (vlist))
12816 Lisp_Object var = XCAR (vlist);
12818 if (!SYMBOLP (var))
12819 continue;
12821 if (up_to_date > 0)
12823 Lisp_Object val = find_symbol_value (var);
12824 Fput (var, Qlast_arrow_position,
12825 COERCE_MARKER (val));
12826 Fput (var, Qlast_arrow_string,
12827 overlay_arrow_string_or_property (var));
12829 else if (up_to_date < 0
12830 || !NILP (Fget (var, Qlast_arrow_position)))
12832 Fput (var, Qlast_arrow_position, Qt);
12833 Fput (var, Qlast_arrow_string, Qt);
12839 /* Return overlay arrow string to display at row.
12840 Return integer (bitmap number) for arrow bitmap in left fringe.
12841 Return nil if no overlay arrow. */
12843 static Lisp_Object
12844 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12846 Lisp_Object vlist;
12848 for (vlist = Voverlay_arrow_variable_list;
12849 CONSP (vlist);
12850 vlist = XCDR (vlist))
12852 Lisp_Object var = XCAR (vlist);
12853 Lisp_Object val;
12855 if (!SYMBOLP (var))
12856 continue;
12858 val = find_symbol_value (var);
12860 if (MARKERP (val)
12861 && current_buffer == XMARKER (val)->buffer
12862 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12864 if (FRAME_WINDOW_P (it->f)
12865 /* FIXME: if ROW->reversed_p is set, this should test
12866 the right fringe, not the left one. */
12867 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12869 #ifdef HAVE_WINDOW_SYSTEM
12870 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12872 int fringe_bitmap;
12873 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12874 return make_number (fringe_bitmap);
12876 #endif
12877 return make_number (-1); /* Use default arrow bitmap. */
12879 return overlay_arrow_string_or_property (var);
12883 return Qnil;
12886 /* Return 1 if point moved out of or into a composition. Otherwise
12887 return 0. PREV_BUF and PREV_PT are the last point buffer and
12888 position. BUF and PT are the current point buffer and position. */
12890 static int
12891 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12892 struct buffer *buf, ptrdiff_t pt)
12894 ptrdiff_t start, end;
12895 Lisp_Object prop;
12896 Lisp_Object buffer;
12898 XSETBUFFER (buffer, buf);
12899 /* Check a composition at the last point if point moved within the
12900 same buffer. */
12901 if (prev_buf == buf)
12903 if (prev_pt == pt)
12904 /* Point didn't move. */
12905 return 0;
12907 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12908 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12909 && composition_valid_p (start, end, prop)
12910 && start < prev_pt && end > prev_pt)
12911 /* The last point was within the composition. Return 1 iff
12912 point moved out of the composition. */
12913 return (pt <= start || pt >= end);
12916 /* Check a composition at the current point. */
12917 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12918 && find_composition (pt, -1, &start, &end, &prop, buffer)
12919 && composition_valid_p (start, end, prop)
12920 && start < pt && end > pt);
12923 /* Reconsider the clip changes of buffer which is displayed in W. */
12925 static void
12926 reconsider_clip_changes (struct window *w)
12928 struct buffer *b = XBUFFER (w->contents);
12930 if (b->clip_changed
12931 && w->window_end_valid
12932 && w->current_matrix->buffer == b
12933 && w->current_matrix->zv == BUF_ZV (b)
12934 && w->current_matrix->begv == BUF_BEGV (b))
12935 b->clip_changed = 0;
12937 /* If display wasn't paused, and W is not a tool bar window, see if
12938 point has been moved into or out of a composition. In that case,
12939 we set b->clip_changed to 1 to force updating the screen. If
12940 b->clip_changed has already been set to 1, we can skip this
12941 check. */
12942 if (!b->clip_changed && w->window_end_valid)
12944 ptrdiff_t pt = (w == XWINDOW (selected_window)
12945 ? PT : marker_position (w->pointm));
12947 if ((w->current_matrix->buffer != b || pt != w->last_point)
12948 && check_point_in_composition (w->current_matrix->buffer,
12949 w->last_point, b, pt))
12950 b->clip_changed = 1;
12954 #define STOP_POLLING \
12955 do { if (! polling_stopped_here) stop_polling (); \
12956 polling_stopped_here = 1; } while (0)
12958 #define RESUME_POLLING \
12959 do { if (polling_stopped_here) start_polling (); \
12960 polling_stopped_here = 0; } while (0)
12963 /* Perhaps in the future avoid recentering windows if it
12964 is not necessary; currently that causes some problems. */
12966 static void
12967 redisplay_internal (void)
12969 struct window *w = XWINDOW (selected_window);
12970 struct window *sw;
12971 struct frame *fr;
12972 int pending;
12973 bool must_finish = 0, match_p;
12974 struct text_pos tlbufpos, tlendpos;
12975 int number_of_visible_frames;
12976 ptrdiff_t count;
12977 struct frame *sf;
12978 int polling_stopped_here = 0;
12979 Lisp_Object tail, frame;
12981 /* Non-zero means redisplay has to consider all windows on all
12982 frames. Zero means, only selected_window is considered. */
12983 int consider_all_windows_p;
12985 /* Non-zero means redisplay has to redisplay the miniwindow. */
12986 int update_miniwindow_p = 0;
12988 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12990 /* No redisplay if running in batch mode or frame is not yet fully
12991 initialized, or redisplay is explicitly turned off by setting
12992 Vinhibit_redisplay. */
12993 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12994 || !NILP (Vinhibit_redisplay))
12995 return;
12997 /* Don't examine these until after testing Vinhibit_redisplay.
12998 When Emacs is shutting down, perhaps because its connection to
12999 X has dropped, we should not look at them at all. */
13000 fr = XFRAME (w->frame);
13001 sf = SELECTED_FRAME ();
13003 if (!fr->glyphs_initialized_p)
13004 return;
13006 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13007 if (popup_activated ())
13008 return;
13009 #endif
13011 /* I don't think this happens but let's be paranoid. */
13012 if (redisplaying_p)
13013 return;
13015 /* Record a function that clears redisplaying_p
13016 when we leave this function. */
13017 count = SPECPDL_INDEX ();
13018 record_unwind_protect_void (unwind_redisplay);
13019 redisplaying_p = 1;
13020 specbind (Qinhibit_free_realized_faces, Qnil);
13022 /* Record this function, so it appears on the profiler's backtraces. */
13023 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13025 FOR_EACH_FRAME (tail, frame)
13026 XFRAME (frame)->already_hscrolled_p = 0;
13028 retry:
13029 /* Remember the currently selected window. */
13030 sw = w;
13032 pending = 0;
13033 last_escape_glyph_frame = NULL;
13034 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13035 last_glyphless_glyph_frame = NULL;
13036 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13038 /* If new fonts have been loaded that make a glyph matrix adjustment
13039 necessary, do it. */
13040 if (fonts_changed_p)
13042 adjust_glyphs (NULL);
13043 ++windows_or_buffers_changed;
13044 fonts_changed_p = 0;
13047 /* If face_change_count is non-zero, init_iterator will free all
13048 realized faces, which includes the faces referenced from current
13049 matrices. So, we can't reuse current matrices in this case. */
13050 if (face_change_count)
13051 ++windows_or_buffers_changed;
13053 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13054 && FRAME_TTY (sf)->previous_frame != sf)
13056 /* Since frames on a single ASCII terminal share the same
13057 display area, displaying a different frame means redisplay
13058 the whole thing. */
13059 windows_or_buffers_changed++;
13060 SET_FRAME_GARBAGED (sf);
13061 #ifndef DOS_NT
13062 set_tty_color_mode (FRAME_TTY (sf), sf);
13063 #endif
13064 FRAME_TTY (sf)->previous_frame = sf;
13067 /* Set the visible flags for all frames. Do this before checking for
13068 resized or garbaged frames; they want to know if their frames are
13069 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13070 number_of_visible_frames = 0;
13072 FOR_EACH_FRAME (tail, frame)
13074 struct frame *f = XFRAME (frame);
13076 if (FRAME_VISIBLE_P (f))
13077 ++number_of_visible_frames;
13078 clear_desired_matrices (f);
13081 /* Notice any pending interrupt request to change frame size. */
13082 do_pending_window_change (1);
13084 /* do_pending_window_change could change the selected_window due to
13085 frame resizing which makes the selected window too small. */
13086 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13087 sw = w;
13089 /* Clear frames marked as garbaged. */
13090 clear_garbaged_frames ();
13092 /* Build menubar and tool-bar items. */
13093 if (NILP (Vmemory_full))
13094 prepare_menu_bars ();
13096 if (windows_or_buffers_changed)
13097 update_mode_lines++;
13099 reconsider_clip_changes (w);
13101 /* In most cases selected window displays current buffer. */
13102 match_p = XBUFFER (w->contents) == current_buffer;
13103 if (match_p)
13105 ptrdiff_t count1;
13107 /* Detect case that we need to write or remove a star in the mode line. */
13108 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13110 w->update_mode_line = 1;
13111 if (buffer_shared_and_changed ())
13112 update_mode_lines++;
13115 /* Avoid invocation of point motion hooks by `current_column' below. */
13116 count1 = SPECPDL_INDEX ();
13117 specbind (Qinhibit_point_motion_hooks, Qt);
13119 if (mode_line_update_needed (w))
13120 w->update_mode_line = 1;
13122 unbind_to (count1, Qnil);
13125 consider_all_windows_p = (update_mode_lines
13126 || buffer_shared_and_changed ()
13127 || cursor_type_changed);
13129 /* If specs for an arrow have changed, do thorough redisplay
13130 to ensure we remove any arrow that should no longer exist. */
13131 if (overlay_arrows_changed_p ())
13132 consider_all_windows_p = windows_or_buffers_changed = 1;
13134 /* Normally the message* functions will have already displayed and
13135 updated the echo area, but the frame may have been trashed, or
13136 the update may have been preempted, so display the echo area
13137 again here. Checking message_cleared_p captures the case that
13138 the echo area should be cleared. */
13139 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13140 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13141 || (message_cleared_p
13142 && minibuf_level == 0
13143 /* If the mini-window is currently selected, this means the
13144 echo-area doesn't show through. */
13145 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13147 int window_height_changed_p = echo_area_display (0);
13149 if (message_cleared_p)
13150 update_miniwindow_p = 1;
13152 must_finish = 1;
13154 /* If we don't display the current message, don't clear the
13155 message_cleared_p flag, because, if we did, we wouldn't clear
13156 the echo area in the next redisplay which doesn't preserve
13157 the echo area. */
13158 if (!display_last_displayed_message_p)
13159 message_cleared_p = 0;
13161 if (fonts_changed_p)
13162 goto retry;
13163 else if (window_height_changed_p)
13165 consider_all_windows_p = 1;
13166 ++update_mode_lines;
13167 ++windows_or_buffers_changed;
13169 /* If window configuration was changed, frames may have been
13170 marked garbaged. Clear them or we will experience
13171 surprises wrt scrolling. */
13172 clear_garbaged_frames ();
13175 else if (EQ (selected_window, minibuf_window)
13176 && (current_buffer->clip_changed || window_outdated (w))
13177 && resize_mini_window (w, 0))
13179 /* Resized active mini-window to fit the size of what it is
13180 showing if its contents might have changed. */
13181 must_finish = 1;
13182 /* FIXME: this causes all frames to be updated, which seems unnecessary
13183 since only the current frame needs to be considered. This function
13184 needs to be rewritten with two variables, consider_all_windows and
13185 consider_all_frames. */
13186 consider_all_windows_p = 1;
13187 ++windows_or_buffers_changed;
13188 ++update_mode_lines;
13190 /* If window configuration was changed, frames may have been
13191 marked garbaged. Clear them or we will experience
13192 surprises wrt scrolling. */
13193 clear_garbaged_frames ();
13196 /* If showing the region, and mark has changed, we must redisplay
13197 the whole window. The assignment to this_line_start_pos prevents
13198 the optimization directly below this if-statement. */
13199 if (((!NILP (Vtransient_mark_mode)
13200 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13201 != (w->region_showing > 0))
13202 || (w->region_showing
13203 && w->region_showing
13204 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13205 CHARPOS (this_line_start_pos) = 0;
13207 /* Optimize the case that only the line containing the cursor in the
13208 selected window has changed. Variables starting with this_ are
13209 set in display_line and record information about the line
13210 containing the cursor. */
13211 tlbufpos = this_line_start_pos;
13212 tlendpos = this_line_end_pos;
13213 if (!consider_all_windows_p
13214 && CHARPOS (tlbufpos) > 0
13215 && !w->update_mode_line
13216 && !current_buffer->clip_changed
13217 && !current_buffer->prevent_redisplay_optimizations_p
13218 && FRAME_VISIBLE_P (XFRAME (w->frame))
13219 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13220 /* Make sure recorded data applies to current buffer, etc. */
13221 && this_line_buffer == current_buffer
13222 && match_p
13223 && !w->force_start
13224 && !w->optional_new_start
13225 /* Point must be on the line that we have info recorded about. */
13226 && PT >= CHARPOS (tlbufpos)
13227 && PT <= Z - CHARPOS (tlendpos)
13228 /* All text outside that line, including its final newline,
13229 must be unchanged. */
13230 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13231 CHARPOS (tlendpos)))
13233 if (CHARPOS (tlbufpos) > BEGV
13234 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13235 && (CHARPOS (tlbufpos) == ZV
13236 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13237 /* Former continuation line has disappeared by becoming empty. */
13238 goto cancel;
13239 else if (window_outdated (w) || MINI_WINDOW_P (w))
13241 /* We have to handle the case of continuation around a
13242 wide-column character (see the comment in indent.c around
13243 line 1340).
13245 For instance, in the following case:
13247 -------- Insert --------
13248 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13249 J_I_ ==> J_I_ `^^' are cursors.
13250 ^^ ^^
13251 -------- --------
13253 As we have to redraw the line above, we cannot use this
13254 optimization. */
13256 struct it it;
13257 int line_height_before = this_line_pixel_height;
13259 /* Note that start_display will handle the case that the
13260 line starting at tlbufpos is a continuation line. */
13261 start_display (&it, w, tlbufpos);
13263 /* Implementation note: It this still necessary? */
13264 if (it.current_x != this_line_start_x)
13265 goto cancel;
13267 TRACE ((stderr, "trying display optimization 1\n"));
13268 w->cursor.vpos = -1;
13269 overlay_arrow_seen = 0;
13270 it.vpos = this_line_vpos;
13271 it.current_y = this_line_y;
13272 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13273 display_line (&it);
13275 /* If line contains point, is not continued,
13276 and ends at same distance from eob as before, we win. */
13277 if (w->cursor.vpos >= 0
13278 /* Line is not continued, otherwise this_line_start_pos
13279 would have been set to 0 in display_line. */
13280 && CHARPOS (this_line_start_pos)
13281 /* Line ends as before. */
13282 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13283 /* Line has same height as before. Otherwise other lines
13284 would have to be shifted up or down. */
13285 && this_line_pixel_height == line_height_before)
13287 /* If this is not the window's last line, we must adjust
13288 the charstarts of the lines below. */
13289 if (it.current_y < it.last_visible_y)
13291 struct glyph_row *row
13292 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13293 ptrdiff_t delta, delta_bytes;
13295 /* We used to distinguish between two cases here,
13296 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13297 when the line ends in a newline or the end of the
13298 buffer's accessible portion. But both cases did
13299 the same, so they were collapsed. */
13300 delta = (Z
13301 - CHARPOS (tlendpos)
13302 - MATRIX_ROW_START_CHARPOS (row));
13303 delta_bytes = (Z_BYTE
13304 - BYTEPOS (tlendpos)
13305 - MATRIX_ROW_START_BYTEPOS (row));
13307 increment_matrix_positions (w->current_matrix,
13308 this_line_vpos + 1,
13309 w->current_matrix->nrows,
13310 delta, delta_bytes);
13313 /* If this row displays text now but previously didn't,
13314 or vice versa, w->window_end_vpos may have to be
13315 adjusted. */
13316 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13318 if (w->window_end_vpos < this_line_vpos)
13319 w->window_end_vpos = this_line_vpos;
13321 else if (w->window_end_vpos == this_line_vpos
13322 && this_line_vpos > 0)
13323 w->window_end_vpos = this_line_vpos - 1;
13324 w->window_end_valid = 0;
13326 /* Update hint: No need to try to scroll in update_window. */
13327 w->desired_matrix->no_scrolling_p = 1;
13329 #ifdef GLYPH_DEBUG
13330 *w->desired_matrix->method = 0;
13331 debug_method_add (w, "optimization 1");
13332 #endif
13333 #ifdef HAVE_WINDOW_SYSTEM
13334 update_window_fringes (w, 0);
13335 #endif
13336 goto update;
13338 else
13339 goto cancel;
13341 else if (/* Cursor position hasn't changed. */
13342 PT == w->last_point
13343 /* Make sure the cursor was last displayed
13344 in this window. Otherwise we have to reposition it. */
13345 && 0 <= w->cursor.vpos
13346 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13348 if (!must_finish)
13350 do_pending_window_change (1);
13351 /* If selected_window changed, redisplay again. */
13352 if (WINDOWP (selected_window)
13353 && (w = XWINDOW (selected_window)) != sw)
13354 goto retry;
13356 /* We used to always goto end_of_redisplay here, but this
13357 isn't enough if we have a blinking cursor. */
13358 if (w->cursor_off_p == w->last_cursor_off_p)
13359 goto end_of_redisplay;
13361 goto update;
13363 /* If highlighting the region, or if the cursor is in the echo area,
13364 then we can't just move the cursor. */
13365 else if (! (!NILP (Vtransient_mark_mode)
13366 && !NILP (BVAR (current_buffer, mark_active)))
13367 && (EQ (selected_window,
13368 BVAR (current_buffer, last_selected_window))
13369 || highlight_nonselected_windows)
13370 && !w->region_showing
13371 && NILP (Vshow_trailing_whitespace)
13372 && !cursor_in_echo_area)
13374 struct it it;
13375 struct glyph_row *row;
13377 /* Skip from tlbufpos to PT and see where it is. Note that
13378 PT may be in invisible text. If so, we will end at the
13379 next visible position. */
13380 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13381 NULL, DEFAULT_FACE_ID);
13382 it.current_x = this_line_start_x;
13383 it.current_y = this_line_y;
13384 it.vpos = this_line_vpos;
13386 /* The call to move_it_to stops in front of PT, but
13387 moves over before-strings. */
13388 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13390 if (it.vpos == this_line_vpos
13391 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13392 row->enabled_p))
13394 eassert (this_line_vpos == it.vpos);
13395 eassert (this_line_y == it.current_y);
13396 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13397 #ifdef GLYPH_DEBUG
13398 *w->desired_matrix->method = 0;
13399 debug_method_add (w, "optimization 3");
13400 #endif
13401 goto update;
13403 else
13404 goto cancel;
13407 cancel:
13408 /* Text changed drastically or point moved off of line. */
13409 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13412 CHARPOS (this_line_start_pos) = 0;
13413 consider_all_windows_p |= buffer_shared_and_changed ();
13414 ++clear_face_cache_count;
13415 #ifdef HAVE_WINDOW_SYSTEM
13416 ++clear_image_cache_count;
13417 #endif
13419 /* Build desired matrices, and update the display. If
13420 consider_all_windows_p is non-zero, do it for all windows on all
13421 frames. Otherwise do it for selected_window, only. */
13423 if (consider_all_windows_p)
13425 FOR_EACH_FRAME (tail, frame)
13426 XFRAME (frame)->updated_p = 0;
13428 FOR_EACH_FRAME (tail, frame)
13430 struct frame *f = XFRAME (frame);
13432 /* We don't have to do anything for unselected terminal
13433 frames. */
13434 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13435 && !EQ (FRAME_TTY (f)->top_frame, frame))
13436 continue;
13438 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13440 /* Mark all the scroll bars to be removed; we'll redeem
13441 the ones we want when we redisplay their windows. */
13442 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13443 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13445 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13446 redisplay_windows (FRAME_ROOT_WINDOW (f));
13448 /* The X error handler may have deleted that frame. */
13449 if (!FRAME_LIVE_P (f))
13450 continue;
13452 /* Any scroll bars which redisplay_windows should have
13453 nuked should now go away. */
13454 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13455 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13457 /* If fonts changed, display again. */
13458 /* ??? rms: I suspect it is a mistake to jump all the way
13459 back to retry here. It should just retry this frame. */
13460 if (fonts_changed_p)
13461 goto retry;
13463 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13465 /* See if we have to hscroll. */
13466 if (!f->already_hscrolled_p)
13468 f->already_hscrolled_p = 1;
13469 if (hscroll_windows (f->root_window))
13470 goto retry;
13473 /* Prevent various kinds of signals during display
13474 update. stdio is not robust about handling
13475 signals, which can cause an apparent I/O
13476 error. */
13477 if (interrupt_input)
13478 unrequest_sigio ();
13479 STOP_POLLING;
13481 /* Update the display. */
13482 set_window_update_flags (XWINDOW (f->root_window), 1);
13483 pending |= update_frame (f, 0, 0);
13484 f->updated_p = 1;
13489 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13491 if (!pending)
13493 /* Do the mark_window_display_accurate after all windows have
13494 been redisplayed because this call resets flags in buffers
13495 which are needed for proper redisplay. */
13496 FOR_EACH_FRAME (tail, frame)
13498 struct frame *f = XFRAME (frame);
13499 if (f->updated_p)
13501 mark_window_display_accurate (f->root_window, 1);
13502 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13503 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13508 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13510 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13511 struct frame *mini_frame;
13513 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13514 /* Use list_of_error, not Qerror, so that
13515 we catch only errors and don't run the debugger. */
13516 internal_condition_case_1 (redisplay_window_1, selected_window,
13517 list_of_error,
13518 redisplay_window_error);
13519 if (update_miniwindow_p)
13520 internal_condition_case_1 (redisplay_window_1, mini_window,
13521 list_of_error,
13522 redisplay_window_error);
13524 /* Compare desired and current matrices, perform output. */
13526 update:
13527 /* If fonts changed, display again. */
13528 if (fonts_changed_p)
13529 goto retry;
13531 /* Prevent various kinds of signals during display update.
13532 stdio is not robust about handling signals,
13533 which can cause an apparent I/O error. */
13534 if (interrupt_input)
13535 unrequest_sigio ();
13536 STOP_POLLING;
13538 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13540 if (hscroll_windows (selected_window))
13541 goto retry;
13543 XWINDOW (selected_window)->must_be_updated_p = 1;
13544 pending = update_frame (sf, 0, 0);
13547 /* We may have called echo_area_display at the top of this
13548 function. If the echo area is on another frame, that may
13549 have put text on a frame other than the selected one, so the
13550 above call to update_frame would not have caught it. Catch
13551 it here. */
13552 mini_window = FRAME_MINIBUF_WINDOW (sf);
13553 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13555 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13557 XWINDOW (mini_window)->must_be_updated_p = 1;
13558 pending |= update_frame (mini_frame, 0, 0);
13559 if (!pending && hscroll_windows (mini_window))
13560 goto retry;
13564 /* If display was paused because of pending input, make sure we do a
13565 thorough update the next time. */
13566 if (pending)
13568 /* Prevent the optimization at the beginning of
13569 redisplay_internal that tries a single-line update of the
13570 line containing the cursor in the selected window. */
13571 CHARPOS (this_line_start_pos) = 0;
13573 /* Let the overlay arrow be updated the next time. */
13574 update_overlay_arrows (0);
13576 /* If we pause after scrolling, some rows in the current
13577 matrices of some windows are not valid. */
13578 if (!WINDOW_FULL_WIDTH_P (w)
13579 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13580 update_mode_lines = 1;
13582 else
13584 if (!consider_all_windows_p)
13586 /* This has already been done above if
13587 consider_all_windows_p is set. */
13588 mark_window_display_accurate_1 (w, 1);
13590 /* Say overlay arrows are up to date. */
13591 update_overlay_arrows (1);
13593 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13594 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13597 update_mode_lines = 0;
13598 windows_or_buffers_changed = 0;
13599 cursor_type_changed = 0;
13602 /* Start SIGIO interrupts coming again. Having them off during the
13603 code above makes it less likely one will discard output, but not
13604 impossible, since there might be stuff in the system buffer here.
13605 But it is much hairier to try to do anything about that. */
13606 if (interrupt_input)
13607 request_sigio ();
13608 RESUME_POLLING;
13610 /* If a frame has become visible which was not before, redisplay
13611 again, so that we display it. Expose events for such a frame
13612 (which it gets when becoming visible) don't call the parts of
13613 redisplay constructing glyphs, so simply exposing a frame won't
13614 display anything in this case. So, we have to display these
13615 frames here explicitly. */
13616 if (!pending)
13618 int new_count = 0;
13620 FOR_EACH_FRAME (tail, frame)
13622 int this_is_visible = 0;
13624 if (XFRAME (frame)->visible)
13625 this_is_visible = 1;
13627 if (this_is_visible)
13628 new_count++;
13631 if (new_count != number_of_visible_frames)
13632 windows_or_buffers_changed++;
13635 /* Change frame size now if a change is pending. */
13636 do_pending_window_change (1);
13638 /* If we just did a pending size change, or have additional
13639 visible frames, or selected_window changed, redisplay again. */
13640 if ((windows_or_buffers_changed && !pending)
13641 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13642 goto retry;
13644 /* Clear the face and image caches.
13646 We used to do this only if consider_all_windows_p. But the cache
13647 needs to be cleared if a timer creates images in the current
13648 buffer (e.g. the test case in Bug#6230). */
13650 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13652 clear_face_cache (0);
13653 clear_face_cache_count = 0;
13656 #ifdef HAVE_WINDOW_SYSTEM
13657 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13659 clear_image_caches (Qnil);
13660 clear_image_cache_count = 0;
13662 #endif /* HAVE_WINDOW_SYSTEM */
13664 end_of_redisplay:
13665 unbind_to (count, Qnil);
13666 RESUME_POLLING;
13670 /* Redisplay, but leave alone any recent echo area message unless
13671 another message has been requested in its place.
13673 This is useful in situations where you need to redisplay but no
13674 user action has occurred, making it inappropriate for the message
13675 area to be cleared. See tracking_off and
13676 wait_reading_process_output for examples of these situations.
13678 FROM_WHERE is an integer saying from where this function was
13679 called. This is useful for debugging. */
13681 void
13682 redisplay_preserve_echo_area (int from_where)
13684 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13686 if (!NILP (echo_area_buffer[1]))
13688 /* We have a previously displayed message, but no current
13689 message. Redisplay the previous message. */
13690 display_last_displayed_message_p = 1;
13691 redisplay_internal ();
13692 display_last_displayed_message_p = 0;
13694 else
13695 redisplay_internal ();
13697 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13698 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13699 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13703 /* Function registered with record_unwind_protect in redisplay_internal. */
13705 static void
13706 unwind_redisplay (void)
13708 redisplaying_p = 0;
13712 /* Mark the display of leaf window W as accurate or inaccurate.
13713 If ACCURATE_P is non-zero mark display of W as accurate. If
13714 ACCURATE_P is zero, arrange for W to be redisplayed the next
13715 time redisplay_internal is called. */
13717 static void
13718 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13720 struct buffer *b = XBUFFER (w->contents);
13722 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13723 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13724 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13726 if (accurate_p)
13728 b->clip_changed = 0;
13729 b->prevent_redisplay_optimizations_p = 0;
13731 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13732 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13733 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13734 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13736 w->current_matrix->buffer = b;
13737 w->current_matrix->begv = BUF_BEGV (b);
13738 w->current_matrix->zv = BUF_ZV (b);
13740 w->last_cursor = w->cursor;
13741 w->last_cursor_off_p = w->cursor_off_p;
13743 if (w == XWINDOW (selected_window))
13744 w->last_point = BUF_PT (b);
13745 else
13746 w->last_point = marker_position (w->pointm);
13748 w->window_end_valid = 1;
13749 w->update_mode_line = 0;
13754 /* Mark the display of windows in the window tree rooted at WINDOW as
13755 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13756 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13757 be redisplayed the next time redisplay_internal is called. */
13759 void
13760 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13762 struct window *w;
13764 for (; !NILP (window); window = w->next)
13766 w = XWINDOW (window);
13767 if (WINDOWP (w->contents))
13768 mark_window_display_accurate (w->contents, accurate_p);
13769 else
13770 mark_window_display_accurate_1 (w, accurate_p);
13773 if (accurate_p)
13774 update_overlay_arrows (1);
13775 else
13776 /* Force a thorough redisplay the next time by setting
13777 last_arrow_position and last_arrow_string to t, which is
13778 unequal to any useful value of Voverlay_arrow_... */
13779 update_overlay_arrows (-1);
13783 /* Return value in display table DP (Lisp_Char_Table *) for character
13784 C. Since a display table doesn't have any parent, we don't have to
13785 follow parent. Do not call this function directly but use the
13786 macro DISP_CHAR_VECTOR. */
13788 Lisp_Object
13789 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13791 Lisp_Object val;
13793 if (ASCII_CHAR_P (c))
13795 val = dp->ascii;
13796 if (SUB_CHAR_TABLE_P (val))
13797 val = XSUB_CHAR_TABLE (val)->contents[c];
13799 else
13801 Lisp_Object table;
13803 XSETCHAR_TABLE (table, dp);
13804 val = char_table_ref (table, c);
13806 if (NILP (val))
13807 val = dp->defalt;
13808 return val;
13813 /***********************************************************************
13814 Window Redisplay
13815 ***********************************************************************/
13817 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13819 static void
13820 redisplay_windows (Lisp_Object window)
13822 while (!NILP (window))
13824 struct window *w = XWINDOW (window);
13826 if (WINDOWP (w->contents))
13827 redisplay_windows (w->contents);
13828 else if (BUFFERP (w->contents))
13830 displayed_buffer = XBUFFER (w->contents);
13831 /* Use list_of_error, not Qerror, so that
13832 we catch only errors and don't run the debugger. */
13833 internal_condition_case_1 (redisplay_window_0, window,
13834 list_of_error,
13835 redisplay_window_error);
13838 window = w->next;
13842 static Lisp_Object
13843 redisplay_window_error (Lisp_Object ignore)
13845 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13846 return Qnil;
13849 static Lisp_Object
13850 redisplay_window_0 (Lisp_Object window)
13852 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13853 redisplay_window (window, 0);
13854 return Qnil;
13857 static Lisp_Object
13858 redisplay_window_1 (Lisp_Object window)
13860 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13861 redisplay_window (window, 1);
13862 return Qnil;
13866 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13867 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13868 which positions recorded in ROW differ from current buffer
13869 positions.
13871 Return 0 if cursor is not on this row, 1 otherwise. */
13873 static int
13874 set_cursor_from_row (struct window *w, struct glyph_row *row,
13875 struct glyph_matrix *matrix,
13876 ptrdiff_t delta, ptrdiff_t delta_bytes,
13877 int dy, int dvpos)
13879 struct glyph *glyph = row->glyphs[TEXT_AREA];
13880 struct glyph *end = glyph + row->used[TEXT_AREA];
13881 struct glyph *cursor = NULL;
13882 /* The last known character position in row. */
13883 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13884 int x = row->x;
13885 ptrdiff_t pt_old = PT - delta;
13886 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13887 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13888 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13889 /* A glyph beyond the edge of TEXT_AREA which we should never
13890 touch. */
13891 struct glyph *glyphs_end = end;
13892 /* Non-zero means we've found a match for cursor position, but that
13893 glyph has the avoid_cursor_p flag set. */
13894 int match_with_avoid_cursor = 0;
13895 /* Non-zero means we've seen at least one glyph that came from a
13896 display string. */
13897 int string_seen = 0;
13898 /* Largest and smallest buffer positions seen so far during scan of
13899 glyph row. */
13900 ptrdiff_t bpos_max = pos_before;
13901 ptrdiff_t bpos_min = pos_after;
13902 /* Last buffer position covered by an overlay string with an integer
13903 `cursor' property. */
13904 ptrdiff_t bpos_covered = 0;
13905 /* Non-zero means the display string on which to display the cursor
13906 comes from a text property, not from an overlay. */
13907 int string_from_text_prop = 0;
13909 /* Don't even try doing anything if called for a mode-line or
13910 header-line row, since the rest of the code isn't prepared to
13911 deal with such calamities. */
13912 eassert (!row->mode_line_p);
13913 if (row->mode_line_p)
13914 return 0;
13916 /* Skip over glyphs not having an object at the start and the end of
13917 the row. These are special glyphs like truncation marks on
13918 terminal frames. */
13919 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13921 if (!row->reversed_p)
13923 while (glyph < end
13924 && INTEGERP (glyph->object)
13925 && glyph->charpos < 0)
13927 x += glyph->pixel_width;
13928 ++glyph;
13930 while (end > glyph
13931 && INTEGERP ((end - 1)->object)
13932 /* CHARPOS is zero for blanks and stretch glyphs
13933 inserted by extend_face_to_end_of_line. */
13934 && (end - 1)->charpos <= 0)
13935 --end;
13936 glyph_before = glyph - 1;
13937 glyph_after = end;
13939 else
13941 struct glyph *g;
13943 /* If the glyph row is reversed, we need to process it from back
13944 to front, so swap the edge pointers. */
13945 glyphs_end = end = glyph - 1;
13946 glyph += row->used[TEXT_AREA] - 1;
13948 while (glyph > end + 1
13949 && INTEGERP (glyph->object)
13950 && glyph->charpos < 0)
13952 --glyph;
13953 x -= glyph->pixel_width;
13955 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13956 --glyph;
13957 /* By default, in reversed rows we put the cursor on the
13958 rightmost (first in the reading order) glyph. */
13959 for (g = end + 1; g < glyph; g++)
13960 x += g->pixel_width;
13961 while (end < glyph
13962 && INTEGERP ((end + 1)->object)
13963 && (end + 1)->charpos <= 0)
13964 ++end;
13965 glyph_before = glyph + 1;
13966 glyph_after = end;
13969 else if (row->reversed_p)
13971 /* In R2L rows that don't display text, put the cursor on the
13972 rightmost glyph. Case in point: an empty last line that is
13973 part of an R2L paragraph. */
13974 cursor = end - 1;
13975 /* Avoid placing the cursor on the last glyph of the row, where
13976 on terminal frames we hold the vertical border between
13977 adjacent windows. */
13978 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13979 && !WINDOW_RIGHTMOST_P (w)
13980 && cursor == row->glyphs[LAST_AREA] - 1)
13981 cursor--;
13982 x = -1; /* will be computed below, at label compute_x */
13985 /* Step 1: Try to find the glyph whose character position
13986 corresponds to point. If that's not possible, find 2 glyphs
13987 whose character positions are the closest to point, one before
13988 point, the other after it. */
13989 if (!row->reversed_p)
13990 while (/* not marched to end of glyph row */
13991 glyph < end
13992 /* glyph was not inserted by redisplay for internal purposes */
13993 && !INTEGERP (glyph->object))
13995 if (BUFFERP (glyph->object))
13997 ptrdiff_t dpos = glyph->charpos - pt_old;
13999 if (glyph->charpos > bpos_max)
14000 bpos_max = glyph->charpos;
14001 if (glyph->charpos < bpos_min)
14002 bpos_min = glyph->charpos;
14003 if (!glyph->avoid_cursor_p)
14005 /* If we hit point, we've found the glyph on which to
14006 display the cursor. */
14007 if (dpos == 0)
14009 match_with_avoid_cursor = 0;
14010 break;
14012 /* See if we've found a better approximation to
14013 POS_BEFORE or to POS_AFTER. */
14014 if (0 > dpos && dpos > pos_before - pt_old)
14016 pos_before = glyph->charpos;
14017 glyph_before = glyph;
14019 else if (0 < dpos && dpos < pos_after - pt_old)
14021 pos_after = glyph->charpos;
14022 glyph_after = glyph;
14025 else if (dpos == 0)
14026 match_with_avoid_cursor = 1;
14028 else if (STRINGP (glyph->object))
14030 Lisp_Object chprop;
14031 ptrdiff_t glyph_pos = glyph->charpos;
14033 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14034 glyph->object);
14035 if (!NILP (chprop))
14037 /* If the string came from a `display' text property,
14038 look up the buffer position of that property and
14039 use that position to update bpos_max, as if we
14040 actually saw such a position in one of the row's
14041 glyphs. This helps with supporting integer values
14042 of `cursor' property on the display string in
14043 situations where most or all of the row's buffer
14044 text is completely covered by display properties,
14045 so that no glyph with valid buffer positions is
14046 ever seen in the row. */
14047 ptrdiff_t prop_pos =
14048 string_buffer_position_lim (glyph->object, pos_before,
14049 pos_after, 0);
14051 if (prop_pos >= pos_before)
14052 bpos_max = prop_pos - 1;
14054 if (INTEGERP (chprop))
14056 bpos_covered = bpos_max + XINT (chprop);
14057 /* If the `cursor' property covers buffer positions up
14058 to and including point, we should display cursor on
14059 this glyph. Note that, if a `cursor' property on one
14060 of the string's characters has an integer value, we
14061 will break out of the loop below _before_ we get to
14062 the position match above. IOW, integer values of
14063 the `cursor' property override the "exact match for
14064 point" strategy of positioning the cursor. */
14065 /* Implementation note: bpos_max == pt_old when, e.g.,
14066 we are in an empty line, where bpos_max is set to
14067 MATRIX_ROW_START_CHARPOS, see above. */
14068 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14070 cursor = glyph;
14071 break;
14075 string_seen = 1;
14077 x += glyph->pixel_width;
14078 ++glyph;
14080 else if (glyph > end) /* row is reversed */
14081 while (!INTEGERP (glyph->object))
14083 if (BUFFERP (glyph->object))
14085 ptrdiff_t dpos = glyph->charpos - pt_old;
14087 if (glyph->charpos > bpos_max)
14088 bpos_max = glyph->charpos;
14089 if (glyph->charpos < bpos_min)
14090 bpos_min = glyph->charpos;
14091 if (!glyph->avoid_cursor_p)
14093 if (dpos == 0)
14095 match_with_avoid_cursor = 0;
14096 break;
14098 if (0 > dpos && dpos > pos_before - pt_old)
14100 pos_before = glyph->charpos;
14101 glyph_before = glyph;
14103 else if (0 < dpos && dpos < pos_after - pt_old)
14105 pos_after = glyph->charpos;
14106 glyph_after = glyph;
14109 else if (dpos == 0)
14110 match_with_avoid_cursor = 1;
14112 else if (STRINGP (glyph->object))
14114 Lisp_Object chprop;
14115 ptrdiff_t glyph_pos = glyph->charpos;
14117 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14118 glyph->object);
14119 if (!NILP (chprop))
14121 ptrdiff_t prop_pos =
14122 string_buffer_position_lim (glyph->object, pos_before,
14123 pos_after, 0);
14125 if (prop_pos >= pos_before)
14126 bpos_max = prop_pos - 1;
14128 if (INTEGERP (chprop))
14130 bpos_covered = bpos_max + XINT (chprop);
14131 /* If the `cursor' property covers buffer positions up
14132 to and including point, we should display cursor on
14133 this glyph. */
14134 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14136 cursor = glyph;
14137 break;
14140 string_seen = 1;
14142 --glyph;
14143 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14145 x--; /* can't use any pixel_width */
14146 break;
14148 x -= glyph->pixel_width;
14151 /* Step 2: If we didn't find an exact match for point, we need to
14152 look for a proper place to put the cursor among glyphs between
14153 GLYPH_BEFORE and GLYPH_AFTER. */
14154 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14155 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14156 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14158 /* An empty line has a single glyph whose OBJECT is zero and
14159 whose CHARPOS is the position of a newline on that line.
14160 Note that on a TTY, there are more glyphs after that, which
14161 were produced by extend_face_to_end_of_line, but their
14162 CHARPOS is zero or negative. */
14163 int empty_line_p =
14164 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14165 && INTEGERP (glyph->object) && glyph->charpos > 0
14166 /* On a TTY, continued and truncated rows also have a glyph at
14167 their end whose OBJECT is zero and whose CHARPOS is
14168 positive (the continuation and truncation glyphs), but such
14169 rows are obviously not "empty". */
14170 && !(row->continued_p || row->truncated_on_right_p);
14172 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14174 ptrdiff_t ellipsis_pos;
14176 /* Scan back over the ellipsis glyphs. */
14177 if (!row->reversed_p)
14179 ellipsis_pos = (glyph - 1)->charpos;
14180 while (glyph > row->glyphs[TEXT_AREA]
14181 && (glyph - 1)->charpos == ellipsis_pos)
14182 glyph--, x -= glyph->pixel_width;
14183 /* That loop always goes one position too far, including
14184 the glyph before the ellipsis. So scan forward over
14185 that one. */
14186 x += glyph->pixel_width;
14187 glyph++;
14189 else /* row is reversed */
14191 ellipsis_pos = (glyph + 1)->charpos;
14192 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14193 && (glyph + 1)->charpos == ellipsis_pos)
14194 glyph++, x += glyph->pixel_width;
14195 x -= glyph->pixel_width;
14196 glyph--;
14199 else if (match_with_avoid_cursor)
14201 cursor = glyph_after;
14202 x = -1;
14204 else if (string_seen)
14206 int incr = row->reversed_p ? -1 : +1;
14208 /* Need to find the glyph that came out of a string which is
14209 present at point. That glyph is somewhere between
14210 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14211 positioned between POS_BEFORE and POS_AFTER in the
14212 buffer. */
14213 struct glyph *start, *stop;
14214 ptrdiff_t pos = pos_before;
14216 x = -1;
14218 /* If the row ends in a newline from a display string,
14219 reordering could have moved the glyphs belonging to the
14220 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14221 in this case we extend the search to the last glyph in
14222 the row that was not inserted by redisplay. */
14223 if (row->ends_in_newline_from_string_p)
14225 glyph_after = end;
14226 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14229 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14230 correspond to POS_BEFORE and POS_AFTER, respectively. We
14231 need START and STOP in the order that corresponds to the
14232 row's direction as given by its reversed_p flag. If the
14233 directionality of characters between POS_BEFORE and
14234 POS_AFTER is the opposite of the row's base direction,
14235 these characters will have been reordered for display,
14236 and we need to reverse START and STOP. */
14237 if (!row->reversed_p)
14239 start = min (glyph_before, glyph_after);
14240 stop = max (glyph_before, glyph_after);
14242 else
14244 start = max (glyph_before, glyph_after);
14245 stop = min (glyph_before, glyph_after);
14247 for (glyph = start + incr;
14248 row->reversed_p ? glyph > stop : glyph < stop; )
14251 /* Any glyphs that come from the buffer are here because
14252 of bidi reordering. Skip them, and only pay
14253 attention to glyphs that came from some string. */
14254 if (STRINGP (glyph->object))
14256 Lisp_Object str;
14257 ptrdiff_t tem;
14258 /* If the display property covers the newline, we
14259 need to search for it one position farther. */
14260 ptrdiff_t lim = pos_after
14261 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14263 string_from_text_prop = 0;
14264 str = glyph->object;
14265 tem = string_buffer_position_lim (str, pos, lim, 0);
14266 if (tem == 0 /* from overlay */
14267 || pos <= tem)
14269 /* If the string from which this glyph came is
14270 found in the buffer at point, or at position
14271 that is closer to point than pos_after, then
14272 we've found the glyph we've been looking for.
14273 If it comes from an overlay (tem == 0), and
14274 it has the `cursor' property on one of its
14275 glyphs, record that glyph as a candidate for
14276 displaying the cursor. (As in the
14277 unidirectional version, we will display the
14278 cursor on the last candidate we find.) */
14279 if (tem == 0
14280 || tem == pt_old
14281 || (tem - pt_old > 0 && tem < pos_after))
14283 /* The glyphs from this string could have
14284 been reordered. Find the one with the
14285 smallest string position. Or there could
14286 be a character in the string with the
14287 `cursor' property, which means display
14288 cursor on that character's glyph. */
14289 ptrdiff_t strpos = glyph->charpos;
14291 if (tem)
14293 cursor = glyph;
14294 string_from_text_prop = 1;
14296 for ( ;
14297 (row->reversed_p ? glyph > stop : glyph < stop)
14298 && EQ (glyph->object, str);
14299 glyph += incr)
14301 Lisp_Object cprop;
14302 ptrdiff_t gpos = glyph->charpos;
14304 cprop = Fget_char_property (make_number (gpos),
14305 Qcursor,
14306 glyph->object);
14307 if (!NILP (cprop))
14309 cursor = glyph;
14310 break;
14312 if (tem && glyph->charpos < strpos)
14314 strpos = glyph->charpos;
14315 cursor = glyph;
14319 if (tem == pt_old
14320 || (tem - pt_old > 0 && tem < pos_after))
14321 goto compute_x;
14323 if (tem)
14324 pos = tem + 1; /* don't find previous instances */
14326 /* This string is not what we want; skip all of the
14327 glyphs that came from it. */
14328 while ((row->reversed_p ? glyph > stop : glyph < stop)
14329 && EQ (glyph->object, str))
14330 glyph += incr;
14332 else
14333 glyph += incr;
14336 /* If we reached the end of the line, and END was from a string,
14337 the cursor is not on this line. */
14338 if (cursor == NULL
14339 && (row->reversed_p ? glyph <= end : glyph >= end)
14340 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14341 && STRINGP (end->object)
14342 && row->continued_p)
14343 return 0;
14345 /* A truncated row may not include PT among its character positions.
14346 Setting the cursor inside the scroll margin will trigger
14347 recalculation of hscroll in hscroll_window_tree. But if a
14348 display string covers point, defer to the string-handling
14349 code below to figure this out. */
14350 else if (row->truncated_on_left_p && pt_old < bpos_min)
14352 cursor = glyph_before;
14353 x = -1;
14355 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14356 /* Zero-width characters produce no glyphs. */
14357 || (!empty_line_p
14358 && (row->reversed_p
14359 ? glyph_after > glyphs_end
14360 : glyph_after < glyphs_end)))
14362 cursor = glyph_after;
14363 x = -1;
14367 compute_x:
14368 if (cursor != NULL)
14369 glyph = cursor;
14370 else if (glyph == glyphs_end
14371 && pos_before == pos_after
14372 && STRINGP ((row->reversed_p
14373 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14374 : row->glyphs[TEXT_AREA])->object))
14376 /* If all the glyphs of this row came from strings, put the
14377 cursor on the first glyph of the row. This avoids having the
14378 cursor outside of the text area in this very rare and hard
14379 use case. */
14380 glyph =
14381 row->reversed_p
14382 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14383 : row->glyphs[TEXT_AREA];
14385 if (x < 0)
14387 struct glyph *g;
14389 /* Need to compute x that corresponds to GLYPH. */
14390 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14392 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14393 emacs_abort ();
14394 x += g->pixel_width;
14398 /* ROW could be part of a continued line, which, under bidi
14399 reordering, might have other rows whose start and end charpos
14400 occlude point. Only set w->cursor if we found a better
14401 approximation to the cursor position than we have from previously
14402 examined candidate rows belonging to the same continued line. */
14403 if (/* we already have a candidate row */
14404 w->cursor.vpos >= 0
14405 /* that candidate is not the row we are processing */
14406 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14407 /* Make sure cursor.vpos specifies a row whose start and end
14408 charpos occlude point, and it is valid candidate for being a
14409 cursor-row. This is because some callers of this function
14410 leave cursor.vpos at the row where the cursor was displayed
14411 during the last redisplay cycle. */
14412 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14413 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14414 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14416 struct glyph *g1 =
14417 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14419 /* Don't consider glyphs that are outside TEXT_AREA. */
14420 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14421 return 0;
14422 /* Keep the candidate whose buffer position is the closest to
14423 point or has the `cursor' property. */
14424 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14425 w->cursor.hpos >= 0
14426 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14427 && ((BUFFERP (g1->object)
14428 && (g1->charpos == pt_old /* an exact match always wins */
14429 || (BUFFERP (glyph->object)
14430 && eabs (g1->charpos - pt_old)
14431 < eabs (glyph->charpos - pt_old))))
14432 /* previous candidate is a glyph from a string that has
14433 a non-nil `cursor' property */
14434 || (STRINGP (g1->object)
14435 && (!NILP (Fget_char_property (make_number (g1->charpos),
14436 Qcursor, g1->object))
14437 /* previous candidate is from the same display
14438 string as this one, and the display string
14439 came from a text property */
14440 || (EQ (g1->object, glyph->object)
14441 && string_from_text_prop)
14442 /* this candidate is from newline and its
14443 position is not an exact match */
14444 || (INTEGERP (glyph->object)
14445 && glyph->charpos != pt_old)))))
14446 return 0;
14447 /* If this candidate gives an exact match, use that. */
14448 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14449 /* If this candidate is a glyph created for the
14450 terminating newline of a line, and point is on that
14451 newline, it wins because it's an exact match. */
14452 || (!row->continued_p
14453 && INTEGERP (glyph->object)
14454 && glyph->charpos == 0
14455 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14456 /* Otherwise, keep the candidate that comes from a row
14457 spanning less buffer positions. This may win when one or
14458 both candidate positions are on glyphs that came from
14459 display strings, for which we cannot compare buffer
14460 positions. */
14461 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14462 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14463 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14464 return 0;
14466 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14467 w->cursor.x = x;
14468 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14469 w->cursor.y = row->y + dy;
14471 if (w == XWINDOW (selected_window))
14473 if (!row->continued_p
14474 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14475 && row->x == 0)
14477 this_line_buffer = XBUFFER (w->contents);
14479 CHARPOS (this_line_start_pos)
14480 = MATRIX_ROW_START_CHARPOS (row) + delta;
14481 BYTEPOS (this_line_start_pos)
14482 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14484 CHARPOS (this_line_end_pos)
14485 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14486 BYTEPOS (this_line_end_pos)
14487 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14489 this_line_y = w->cursor.y;
14490 this_line_pixel_height = row->height;
14491 this_line_vpos = w->cursor.vpos;
14492 this_line_start_x = row->x;
14494 else
14495 CHARPOS (this_line_start_pos) = 0;
14498 return 1;
14502 /* Run window scroll functions, if any, for WINDOW with new window
14503 start STARTP. Sets the window start of WINDOW to that position.
14505 We assume that the window's buffer is really current. */
14507 static struct text_pos
14508 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14510 struct window *w = XWINDOW (window);
14511 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14513 eassert (current_buffer == XBUFFER (w->contents));
14515 if (!NILP (Vwindow_scroll_functions))
14517 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14518 make_number (CHARPOS (startp)));
14519 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14520 /* In case the hook functions switch buffers. */
14521 set_buffer_internal (XBUFFER (w->contents));
14524 return startp;
14528 /* Make sure the line containing the cursor is fully visible.
14529 A value of 1 means there is nothing to be done.
14530 (Either the line is fully visible, or it cannot be made so,
14531 or we cannot tell.)
14533 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14534 is higher than window.
14536 A value of 0 means the caller should do scrolling
14537 as if point had gone off the screen. */
14539 static int
14540 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14542 struct glyph_matrix *matrix;
14543 struct glyph_row *row;
14544 int window_height;
14546 if (!make_cursor_line_fully_visible_p)
14547 return 1;
14549 /* It's not always possible to find the cursor, e.g, when a window
14550 is full of overlay strings. Don't do anything in that case. */
14551 if (w->cursor.vpos < 0)
14552 return 1;
14554 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14555 row = MATRIX_ROW (matrix, w->cursor.vpos);
14557 /* If the cursor row is not partially visible, there's nothing to do. */
14558 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14559 return 1;
14561 /* If the row the cursor is in is taller than the window's height,
14562 it's not clear what to do, so do nothing. */
14563 window_height = window_box_height (w);
14564 if (row->height >= window_height)
14566 if (!force_p || MINI_WINDOW_P (w)
14567 || w->vscroll || w->cursor.vpos == 0)
14568 return 1;
14570 return 0;
14574 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14575 non-zero means only WINDOW is redisplayed in redisplay_internal.
14576 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14577 in redisplay_window to bring a partially visible line into view in
14578 the case that only the cursor has moved.
14580 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14581 last screen line's vertical height extends past the end of the screen.
14583 Value is
14585 1 if scrolling succeeded
14587 0 if scrolling didn't find point.
14589 -1 if new fonts have been loaded so that we must interrupt
14590 redisplay, adjust glyph matrices, and try again. */
14592 enum
14594 SCROLLING_SUCCESS,
14595 SCROLLING_FAILED,
14596 SCROLLING_NEED_LARGER_MATRICES
14599 /* If scroll-conservatively is more than this, never recenter.
14601 If you change this, don't forget to update the doc string of
14602 `scroll-conservatively' and the Emacs manual. */
14603 #define SCROLL_LIMIT 100
14605 static int
14606 try_scrolling (Lisp_Object window, int just_this_one_p,
14607 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14608 int temp_scroll_step, int last_line_misfit)
14610 struct window *w = XWINDOW (window);
14611 struct frame *f = XFRAME (w->frame);
14612 struct text_pos pos, startp;
14613 struct it it;
14614 int this_scroll_margin, scroll_max, rc, height;
14615 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14616 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14617 Lisp_Object aggressive;
14618 /* We will never try scrolling more than this number of lines. */
14619 int scroll_limit = SCROLL_LIMIT;
14620 int frame_line_height = default_line_pixel_height (w);
14621 int window_total_lines
14622 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14624 #ifdef GLYPH_DEBUG
14625 debug_method_add (w, "try_scrolling");
14626 #endif
14628 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14630 /* Compute scroll margin height in pixels. We scroll when point is
14631 within this distance from the top or bottom of the window. */
14632 if (scroll_margin > 0)
14633 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14634 * frame_line_height;
14635 else
14636 this_scroll_margin = 0;
14638 /* Force arg_scroll_conservatively to have a reasonable value, to
14639 avoid scrolling too far away with slow move_it_* functions. Note
14640 that the user can supply scroll-conservatively equal to
14641 `most-positive-fixnum', which can be larger than INT_MAX. */
14642 if (arg_scroll_conservatively > scroll_limit)
14644 arg_scroll_conservatively = scroll_limit + 1;
14645 scroll_max = scroll_limit * frame_line_height;
14647 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14648 /* Compute how much we should try to scroll maximally to bring
14649 point into view. */
14650 scroll_max = (max (scroll_step,
14651 max (arg_scroll_conservatively, temp_scroll_step))
14652 * frame_line_height);
14653 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14654 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14655 /* We're trying to scroll because of aggressive scrolling but no
14656 scroll_step is set. Choose an arbitrary one. */
14657 scroll_max = 10 * frame_line_height;
14658 else
14659 scroll_max = 0;
14661 too_near_end:
14663 /* Decide whether to scroll down. */
14664 if (PT > CHARPOS (startp))
14666 int scroll_margin_y;
14668 /* Compute the pixel ypos of the scroll margin, then move IT to
14669 either that ypos or PT, whichever comes first. */
14670 start_display (&it, w, startp);
14671 scroll_margin_y = it.last_visible_y - this_scroll_margin
14672 - frame_line_height * extra_scroll_margin_lines;
14673 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14674 (MOVE_TO_POS | MOVE_TO_Y));
14676 if (PT > CHARPOS (it.current.pos))
14678 int y0 = line_bottom_y (&it);
14679 /* Compute how many pixels below window bottom to stop searching
14680 for PT. This avoids costly search for PT that is far away if
14681 the user limited scrolling by a small number of lines, but
14682 always finds PT if scroll_conservatively is set to a large
14683 number, such as most-positive-fixnum. */
14684 int slack = max (scroll_max, 10 * frame_line_height);
14685 int y_to_move = it.last_visible_y + slack;
14687 /* Compute the distance from the scroll margin to PT or to
14688 the scroll limit, whichever comes first. This should
14689 include the height of the cursor line, to make that line
14690 fully visible. */
14691 move_it_to (&it, PT, -1, y_to_move,
14692 -1, MOVE_TO_POS | MOVE_TO_Y);
14693 dy = line_bottom_y (&it) - y0;
14695 if (dy > scroll_max)
14696 return SCROLLING_FAILED;
14698 if (dy > 0)
14699 scroll_down_p = 1;
14703 if (scroll_down_p)
14705 /* Point is in or below the bottom scroll margin, so move the
14706 window start down. If scrolling conservatively, move it just
14707 enough down to make point visible. If scroll_step is set,
14708 move it down by scroll_step. */
14709 if (arg_scroll_conservatively)
14710 amount_to_scroll
14711 = min (max (dy, frame_line_height),
14712 frame_line_height * arg_scroll_conservatively);
14713 else if (scroll_step || temp_scroll_step)
14714 amount_to_scroll = scroll_max;
14715 else
14717 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14718 height = WINDOW_BOX_TEXT_HEIGHT (w);
14719 if (NUMBERP (aggressive))
14721 double float_amount = XFLOATINT (aggressive) * height;
14722 int aggressive_scroll = float_amount;
14723 if (aggressive_scroll == 0 && float_amount > 0)
14724 aggressive_scroll = 1;
14725 /* Don't let point enter the scroll margin near top of
14726 the window. This could happen if the value of
14727 scroll_up_aggressively is too large and there are
14728 non-zero margins, because scroll_up_aggressively
14729 means put point that fraction of window height
14730 _from_the_bottom_margin_. */
14731 if (aggressive_scroll + 2*this_scroll_margin > height)
14732 aggressive_scroll = height - 2*this_scroll_margin;
14733 amount_to_scroll = dy + aggressive_scroll;
14737 if (amount_to_scroll <= 0)
14738 return SCROLLING_FAILED;
14740 start_display (&it, w, startp);
14741 if (arg_scroll_conservatively <= scroll_limit)
14742 move_it_vertically (&it, amount_to_scroll);
14743 else
14745 /* Extra precision for users who set scroll-conservatively
14746 to a large number: make sure the amount we scroll
14747 the window start is never less than amount_to_scroll,
14748 which was computed as distance from window bottom to
14749 point. This matters when lines at window top and lines
14750 below window bottom have different height. */
14751 struct it it1;
14752 void *it1data = NULL;
14753 /* We use a temporary it1 because line_bottom_y can modify
14754 its argument, if it moves one line down; see there. */
14755 int start_y;
14757 SAVE_IT (it1, it, it1data);
14758 start_y = line_bottom_y (&it1);
14759 do {
14760 RESTORE_IT (&it, &it, it1data);
14761 move_it_by_lines (&it, 1);
14762 SAVE_IT (it1, it, it1data);
14763 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14766 /* If STARTP is unchanged, move it down another screen line. */
14767 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14768 move_it_by_lines (&it, 1);
14769 startp = it.current.pos;
14771 else
14773 struct text_pos scroll_margin_pos = startp;
14774 int y_offset = 0;
14776 /* See if point is inside the scroll margin at the top of the
14777 window. */
14778 if (this_scroll_margin)
14780 int y_start;
14782 start_display (&it, w, startp);
14783 y_start = it.current_y;
14784 move_it_vertically (&it, this_scroll_margin);
14785 scroll_margin_pos = it.current.pos;
14786 /* If we didn't move enough before hitting ZV, request
14787 additional amount of scroll, to move point out of the
14788 scroll margin. */
14789 if (IT_CHARPOS (it) == ZV
14790 && it.current_y - y_start < this_scroll_margin)
14791 y_offset = this_scroll_margin - (it.current_y - y_start);
14794 if (PT < CHARPOS (scroll_margin_pos))
14796 /* Point is in the scroll margin at the top of the window or
14797 above what is displayed in the window. */
14798 int y0, y_to_move;
14800 /* Compute the vertical distance from PT to the scroll
14801 margin position. Move as far as scroll_max allows, or
14802 one screenful, or 10 screen lines, whichever is largest.
14803 Give up if distance is greater than scroll_max or if we
14804 didn't reach the scroll margin position. */
14805 SET_TEXT_POS (pos, PT, PT_BYTE);
14806 start_display (&it, w, pos);
14807 y0 = it.current_y;
14808 y_to_move = max (it.last_visible_y,
14809 max (scroll_max, 10 * frame_line_height));
14810 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14811 y_to_move, -1,
14812 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14813 dy = it.current_y - y0;
14814 if (dy > scroll_max
14815 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14816 return SCROLLING_FAILED;
14818 /* Additional scroll for when ZV was too close to point. */
14819 dy += y_offset;
14821 /* Compute new window start. */
14822 start_display (&it, w, startp);
14824 if (arg_scroll_conservatively)
14825 amount_to_scroll = max (dy, frame_line_height *
14826 max (scroll_step, temp_scroll_step));
14827 else if (scroll_step || temp_scroll_step)
14828 amount_to_scroll = scroll_max;
14829 else
14831 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14832 height = WINDOW_BOX_TEXT_HEIGHT (w);
14833 if (NUMBERP (aggressive))
14835 double float_amount = XFLOATINT (aggressive) * height;
14836 int aggressive_scroll = float_amount;
14837 if (aggressive_scroll == 0 && float_amount > 0)
14838 aggressive_scroll = 1;
14839 /* Don't let point enter the scroll margin near
14840 bottom of the window, if the value of
14841 scroll_down_aggressively happens to be too
14842 large. */
14843 if (aggressive_scroll + 2*this_scroll_margin > height)
14844 aggressive_scroll = height - 2*this_scroll_margin;
14845 amount_to_scroll = dy + aggressive_scroll;
14849 if (amount_to_scroll <= 0)
14850 return SCROLLING_FAILED;
14852 move_it_vertically_backward (&it, amount_to_scroll);
14853 startp = it.current.pos;
14857 /* Run window scroll functions. */
14858 startp = run_window_scroll_functions (window, startp);
14860 /* Display the window. Give up if new fonts are loaded, or if point
14861 doesn't appear. */
14862 if (!try_window (window, startp, 0))
14863 rc = SCROLLING_NEED_LARGER_MATRICES;
14864 else if (w->cursor.vpos < 0)
14866 clear_glyph_matrix (w->desired_matrix);
14867 rc = SCROLLING_FAILED;
14869 else
14871 /* Maybe forget recorded base line for line number display. */
14872 if (!just_this_one_p
14873 || current_buffer->clip_changed
14874 || BEG_UNCHANGED < CHARPOS (startp))
14875 w->base_line_number = 0;
14877 /* If cursor ends up on a partially visible line,
14878 treat that as being off the bottom of the screen. */
14879 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14880 /* It's possible that the cursor is on the first line of the
14881 buffer, which is partially obscured due to a vscroll
14882 (Bug#7537). In that case, avoid looping forever . */
14883 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14885 clear_glyph_matrix (w->desired_matrix);
14886 ++extra_scroll_margin_lines;
14887 goto too_near_end;
14889 rc = SCROLLING_SUCCESS;
14892 return rc;
14896 /* Compute a suitable window start for window W if display of W starts
14897 on a continuation line. Value is non-zero if a new window start
14898 was computed.
14900 The new window start will be computed, based on W's width, starting
14901 from the start of the continued line. It is the start of the
14902 screen line with the minimum distance from the old start W->start. */
14904 static int
14905 compute_window_start_on_continuation_line (struct window *w)
14907 struct text_pos pos, start_pos;
14908 int window_start_changed_p = 0;
14910 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14912 /* If window start is on a continuation line... Window start may be
14913 < BEGV in case there's invisible text at the start of the
14914 buffer (M-x rmail, for example). */
14915 if (CHARPOS (start_pos) > BEGV
14916 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14918 struct it it;
14919 struct glyph_row *row;
14921 /* Handle the case that the window start is out of range. */
14922 if (CHARPOS (start_pos) < BEGV)
14923 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14924 else if (CHARPOS (start_pos) > ZV)
14925 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14927 /* Find the start of the continued line. This should be fast
14928 because find_newline is fast (newline cache). */
14929 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14930 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14931 row, DEFAULT_FACE_ID);
14932 reseat_at_previous_visible_line_start (&it);
14934 /* If the line start is "too far" away from the window start,
14935 say it takes too much time to compute a new window start. */
14936 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14937 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14939 int min_distance, distance;
14941 /* Move forward by display lines to find the new window
14942 start. If window width was enlarged, the new start can
14943 be expected to be > the old start. If window width was
14944 decreased, the new window start will be < the old start.
14945 So, we're looking for the display line start with the
14946 minimum distance from the old window start. */
14947 pos = it.current.pos;
14948 min_distance = INFINITY;
14949 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14950 distance < min_distance)
14952 min_distance = distance;
14953 pos = it.current.pos;
14954 if (it.line_wrap == WORD_WRAP)
14956 /* Under WORD_WRAP, move_it_by_lines is likely to
14957 overshoot and stop not at the first, but the
14958 second character from the left margin. So in
14959 that case, we need a more tight control on the X
14960 coordinate of the iterator than move_it_by_lines
14961 promises in its contract. The method is to first
14962 go to the last (rightmost) visible character of a
14963 line, then move to the leftmost character on the
14964 next line in a separate call. */
14965 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14966 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14967 move_it_to (&it, ZV, 0,
14968 it.current_y + it.max_ascent + it.max_descent, -1,
14969 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14971 else
14972 move_it_by_lines (&it, 1);
14975 /* Set the window start there. */
14976 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14977 window_start_changed_p = 1;
14981 return window_start_changed_p;
14985 /* Try cursor movement in case text has not changed in window WINDOW,
14986 with window start STARTP. Value is
14988 CURSOR_MOVEMENT_SUCCESS if successful
14990 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14992 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14993 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14994 we want to scroll as if scroll-step were set to 1. See the code.
14996 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14997 which case we have to abort this redisplay, and adjust matrices
14998 first. */
15000 enum
15002 CURSOR_MOVEMENT_SUCCESS,
15003 CURSOR_MOVEMENT_CANNOT_BE_USED,
15004 CURSOR_MOVEMENT_MUST_SCROLL,
15005 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15008 static int
15009 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15011 struct window *w = XWINDOW (window);
15012 struct frame *f = XFRAME (w->frame);
15013 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15015 #ifdef GLYPH_DEBUG
15016 if (inhibit_try_cursor_movement)
15017 return rc;
15018 #endif
15020 /* Previously, there was a check for Lisp integer in the
15021 if-statement below. Now, this field is converted to
15022 ptrdiff_t, thus zero means invalid position in a buffer. */
15023 eassert (w->last_point > 0);
15024 /* Likewise there was a check whether window_end_vpos is nil or larger
15025 than the window. Now window_end_vpos is int and so never nil, but
15026 let's leave eassert to check whether it fits in the window. */
15027 eassert (w->window_end_vpos < w->current_matrix->nrows);
15029 /* Handle case where text has not changed, only point, and it has
15030 not moved off the frame. */
15031 if (/* Point may be in this window. */
15032 PT >= CHARPOS (startp)
15033 /* Selective display hasn't changed. */
15034 && !current_buffer->clip_changed
15035 /* Function force-mode-line-update is used to force a thorough
15036 redisplay. It sets either windows_or_buffers_changed or
15037 update_mode_lines. So don't take a shortcut here for these
15038 cases. */
15039 && !update_mode_lines
15040 && !windows_or_buffers_changed
15041 && !cursor_type_changed
15042 /* Can't use this case if highlighting a region. When a
15043 region exists, cursor movement has to do more than just
15044 set the cursor. */
15045 && markpos_of_region () < 0
15046 && !w->region_showing
15047 && NILP (Vshow_trailing_whitespace)
15048 /* This code is not used for mini-buffer for the sake of the case
15049 of redisplaying to replace an echo area message; since in
15050 that case the mini-buffer contents per se are usually
15051 unchanged. This code is of no real use in the mini-buffer
15052 since the handling of this_line_start_pos, etc., in redisplay
15053 handles the same cases. */
15054 && !EQ (window, minibuf_window)
15055 && (FRAME_WINDOW_P (f)
15056 || !overlay_arrow_in_current_buffer_p ()))
15058 int this_scroll_margin, top_scroll_margin;
15059 struct glyph_row *row = NULL;
15060 int frame_line_height = default_line_pixel_height (w);
15061 int window_total_lines
15062 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15064 #ifdef GLYPH_DEBUG
15065 debug_method_add (w, "cursor movement");
15066 #endif
15068 /* Scroll if point within this distance from the top or bottom
15069 of the window. This is a pixel value. */
15070 if (scroll_margin > 0)
15072 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15073 this_scroll_margin *= frame_line_height;
15075 else
15076 this_scroll_margin = 0;
15078 top_scroll_margin = this_scroll_margin;
15079 if (WINDOW_WANTS_HEADER_LINE_P (w))
15080 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15082 /* Start with the row the cursor was displayed during the last
15083 not paused redisplay. Give up if that row is not valid. */
15084 if (w->last_cursor.vpos < 0
15085 || w->last_cursor.vpos >= w->current_matrix->nrows)
15086 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15087 else
15089 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15090 if (row->mode_line_p)
15091 ++row;
15092 if (!row->enabled_p)
15093 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15096 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15098 int scroll_p = 0, must_scroll = 0;
15099 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15101 if (PT > w->last_point)
15103 /* Point has moved forward. */
15104 while (MATRIX_ROW_END_CHARPOS (row) < PT
15105 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15107 eassert (row->enabled_p);
15108 ++row;
15111 /* If the end position of a row equals the start
15112 position of the next row, and PT is at that position,
15113 we would rather display cursor in the next line. */
15114 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15115 && MATRIX_ROW_END_CHARPOS (row) == PT
15116 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15117 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15118 && !cursor_row_p (row))
15119 ++row;
15121 /* If within the scroll margin, scroll. Note that
15122 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15123 the next line would be drawn, and that
15124 this_scroll_margin can be zero. */
15125 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15126 || PT > MATRIX_ROW_END_CHARPOS (row)
15127 /* Line is completely visible last line in window
15128 and PT is to be set in the next line. */
15129 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15130 && PT == MATRIX_ROW_END_CHARPOS (row)
15131 && !row->ends_at_zv_p
15132 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15133 scroll_p = 1;
15135 else if (PT < w->last_point)
15137 /* Cursor has to be moved backward. Note that PT >=
15138 CHARPOS (startp) because of the outer if-statement. */
15139 while (!row->mode_line_p
15140 && (MATRIX_ROW_START_CHARPOS (row) > PT
15141 || (MATRIX_ROW_START_CHARPOS (row) == PT
15142 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15143 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15144 row > w->current_matrix->rows
15145 && (row-1)->ends_in_newline_from_string_p))))
15146 && (row->y > top_scroll_margin
15147 || CHARPOS (startp) == BEGV))
15149 eassert (row->enabled_p);
15150 --row;
15153 /* Consider the following case: Window starts at BEGV,
15154 there is invisible, intangible text at BEGV, so that
15155 display starts at some point START > BEGV. It can
15156 happen that we are called with PT somewhere between
15157 BEGV and START. Try to handle that case. */
15158 if (row < w->current_matrix->rows
15159 || row->mode_line_p)
15161 row = w->current_matrix->rows;
15162 if (row->mode_line_p)
15163 ++row;
15166 /* Due to newlines in overlay strings, we may have to
15167 skip forward over overlay strings. */
15168 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15169 && MATRIX_ROW_END_CHARPOS (row) == PT
15170 && !cursor_row_p (row))
15171 ++row;
15173 /* If within the scroll margin, scroll. */
15174 if (row->y < top_scroll_margin
15175 && CHARPOS (startp) != BEGV)
15176 scroll_p = 1;
15178 else
15180 /* Cursor did not move. So don't scroll even if cursor line
15181 is partially visible, as it was so before. */
15182 rc = CURSOR_MOVEMENT_SUCCESS;
15185 if (PT < MATRIX_ROW_START_CHARPOS (row)
15186 || PT > MATRIX_ROW_END_CHARPOS (row))
15188 /* if PT is not in the glyph row, give up. */
15189 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15190 must_scroll = 1;
15192 else if (rc != CURSOR_MOVEMENT_SUCCESS
15193 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15195 struct glyph_row *row1;
15197 /* If rows are bidi-reordered and point moved, back up
15198 until we find a row that does not belong to a
15199 continuation line. This is because we must consider
15200 all rows of a continued line as candidates for the
15201 new cursor positioning, since row start and end
15202 positions change non-linearly with vertical position
15203 in such rows. */
15204 /* FIXME: Revisit this when glyph ``spilling'' in
15205 continuation lines' rows is implemented for
15206 bidi-reordered rows. */
15207 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15208 MATRIX_ROW_CONTINUATION_LINE_P (row);
15209 --row)
15211 /* If we hit the beginning of the displayed portion
15212 without finding the first row of a continued
15213 line, give up. */
15214 if (row <= row1)
15216 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15217 break;
15219 eassert (row->enabled_p);
15222 if (must_scroll)
15224 else if (rc != CURSOR_MOVEMENT_SUCCESS
15225 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15226 /* Make sure this isn't a header line by any chance, since
15227 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15228 && !row->mode_line_p
15229 && make_cursor_line_fully_visible_p)
15231 if (PT == MATRIX_ROW_END_CHARPOS (row)
15232 && !row->ends_at_zv_p
15233 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15234 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15235 else if (row->height > window_box_height (w))
15237 /* If we end up in a partially visible line, let's
15238 make it fully visible, except when it's taller
15239 than the window, in which case we can't do much
15240 about it. */
15241 *scroll_step = 1;
15242 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15244 else
15246 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15247 if (!cursor_row_fully_visible_p (w, 0, 1))
15248 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15249 else
15250 rc = CURSOR_MOVEMENT_SUCCESS;
15253 else if (scroll_p)
15254 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15255 else if (rc != CURSOR_MOVEMENT_SUCCESS
15256 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15258 /* With bidi-reordered rows, there could be more than
15259 one candidate row whose start and end positions
15260 occlude point. We need to let set_cursor_from_row
15261 find the best candidate. */
15262 /* FIXME: Revisit this when glyph ``spilling'' in
15263 continuation lines' rows is implemented for
15264 bidi-reordered rows. */
15265 int rv = 0;
15269 int at_zv_p = 0, exact_match_p = 0;
15271 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15272 && PT <= MATRIX_ROW_END_CHARPOS (row)
15273 && cursor_row_p (row))
15274 rv |= set_cursor_from_row (w, row, w->current_matrix,
15275 0, 0, 0, 0);
15276 /* As soon as we've found the exact match for point,
15277 or the first suitable row whose ends_at_zv_p flag
15278 is set, we are done. */
15279 at_zv_p =
15280 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15281 if (rv && !at_zv_p
15282 && w->cursor.hpos >= 0
15283 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15284 w->cursor.vpos))
15286 struct glyph_row *candidate =
15287 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15288 struct glyph *g =
15289 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15290 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15292 exact_match_p =
15293 (BUFFERP (g->object) && g->charpos == PT)
15294 || (INTEGERP (g->object)
15295 && (g->charpos == PT
15296 || (g->charpos == 0 && endpos - 1 == PT)));
15298 if (rv && (at_zv_p || exact_match_p))
15300 rc = CURSOR_MOVEMENT_SUCCESS;
15301 break;
15303 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15304 break;
15305 ++row;
15307 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15308 || row->continued_p)
15309 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15310 || (MATRIX_ROW_START_CHARPOS (row) == PT
15311 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15312 /* If we didn't find any candidate rows, or exited the
15313 loop before all the candidates were examined, signal
15314 to the caller that this method failed. */
15315 if (rc != CURSOR_MOVEMENT_SUCCESS
15316 && !(rv
15317 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15318 && !row->continued_p))
15319 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15320 else if (rv)
15321 rc = CURSOR_MOVEMENT_SUCCESS;
15323 else
15327 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15329 rc = CURSOR_MOVEMENT_SUCCESS;
15330 break;
15332 ++row;
15334 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15335 && MATRIX_ROW_START_CHARPOS (row) == PT
15336 && cursor_row_p (row));
15341 return rc;
15344 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15345 static
15346 #endif
15347 void
15348 set_vertical_scroll_bar (struct window *w)
15350 ptrdiff_t start, end, whole;
15352 /* Calculate the start and end positions for the current window.
15353 At some point, it would be nice to choose between scrollbars
15354 which reflect the whole buffer size, with special markers
15355 indicating narrowing, and scrollbars which reflect only the
15356 visible region.
15358 Note that mini-buffers sometimes aren't displaying any text. */
15359 if (!MINI_WINDOW_P (w)
15360 || (w == XWINDOW (minibuf_window)
15361 && NILP (echo_area_buffer[0])))
15363 struct buffer *buf = XBUFFER (w->contents);
15364 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15365 start = marker_position (w->start) - BUF_BEGV (buf);
15366 /* I don't think this is guaranteed to be right. For the
15367 moment, we'll pretend it is. */
15368 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15370 if (end < start)
15371 end = start;
15372 if (whole < (end - start))
15373 whole = end - start;
15375 else
15376 start = end = whole = 0;
15378 /* Indicate what this scroll bar ought to be displaying now. */
15379 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15380 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15381 (w, end - start, whole, start);
15385 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15386 selected_window is redisplayed.
15388 We can return without actually redisplaying the window if
15389 fonts_changed_p. In that case, redisplay_internal will
15390 retry. */
15392 static void
15393 redisplay_window (Lisp_Object window, int just_this_one_p)
15395 struct window *w = XWINDOW (window);
15396 struct frame *f = XFRAME (w->frame);
15397 struct buffer *buffer = XBUFFER (w->contents);
15398 struct buffer *old = current_buffer;
15399 struct text_pos lpoint, opoint, startp;
15400 int update_mode_line;
15401 int tem;
15402 struct it it;
15403 /* Record it now because it's overwritten. */
15404 int current_matrix_up_to_date_p = 0;
15405 int used_current_matrix_p = 0;
15406 /* This is less strict than current_matrix_up_to_date_p.
15407 It indicates that the buffer contents and narrowing are unchanged. */
15408 int buffer_unchanged_p = 0;
15409 int temp_scroll_step = 0;
15410 ptrdiff_t count = SPECPDL_INDEX ();
15411 int rc;
15412 int centering_position = -1;
15413 int last_line_misfit = 0;
15414 ptrdiff_t beg_unchanged, end_unchanged;
15415 int frame_line_height;
15417 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15418 opoint = lpoint;
15420 #ifdef GLYPH_DEBUG
15421 *w->desired_matrix->method = 0;
15422 #endif
15424 /* Make sure that both W's markers are valid. */
15425 eassert (XMARKER (w->start)->buffer == buffer);
15426 eassert (XMARKER (w->pointm)->buffer == buffer);
15428 restart:
15429 reconsider_clip_changes (w);
15430 frame_line_height = default_line_pixel_height (w);
15432 /* Has the mode line to be updated? */
15433 update_mode_line = (w->update_mode_line
15434 || update_mode_lines
15435 || buffer->clip_changed
15436 || buffer->prevent_redisplay_optimizations_p);
15438 if (MINI_WINDOW_P (w))
15440 if (w == XWINDOW (echo_area_window)
15441 && !NILP (echo_area_buffer[0]))
15443 if (update_mode_line)
15444 /* We may have to update a tty frame's menu bar or a
15445 tool-bar. Example `M-x C-h C-h C-g'. */
15446 goto finish_menu_bars;
15447 else
15448 /* We've already displayed the echo area glyphs in this window. */
15449 goto finish_scroll_bars;
15451 else if ((w != XWINDOW (minibuf_window)
15452 || minibuf_level == 0)
15453 /* When buffer is nonempty, redisplay window normally. */
15454 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15455 /* Quail displays non-mini buffers in minibuffer window.
15456 In that case, redisplay the window normally. */
15457 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15459 /* W is a mini-buffer window, but it's not active, so clear
15460 it. */
15461 int yb = window_text_bottom_y (w);
15462 struct glyph_row *row;
15463 int y;
15465 for (y = 0, row = w->desired_matrix->rows;
15466 y < yb;
15467 y += row->height, ++row)
15468 blank_row (w, row, y);
15469 goto finish_scroll_bars;
15472 clear_glyph_matrix (w->desired_matrix);
15475 /* Otherwise set up data on this window; select its buffer and point
15476 value. */
15477 /* Really select the buffer, for the sake of buffer-local
15478 variables. */
15479 set_buffer_internal_1 (XBUFFER (w->contents));
15481 current_matrix_up_to_date_p
15482 = (w->window_end_valid
15483 && !current_buffer->clip_changed
15484 && !current_buffer->prevent_redisplay_optimizations_p
15485 && !window_outdated (w));
15487 /* Run the window-bottom-change-functions
15488 if it is possible that the text on the screen has changed
15489 (either due to modification of the text, or any other reason). */
15490 if (!current_matrix_up_to_date_p
15491 && !NILP (Vwindow_text_change_functions))
15493 safe_run_hooks (Qwindow_text_change_functions);
15494 goto restart;
15497 beg_unchanged = BEG_UNCHANGED;
15498 end_unchanged = END_UNCHANGED;
15500 SET_TEXT_POS (opoint, PT, PT_BYTE);
15502 specbind (Qinhibit_point_motion_hooks, Qt);
15504 buffer_unchanged_p
15505 = (w->window_end_valid
15506 && !current_buffer->clip_changed
15507 && !window_outdated (w));
15509 /* When windows_or_buffers_changed is non-zero, we can't rely
15510 on the window end being valid, so set it to zero there. */
15511 if (windows_or_buffers_changed)
15513 /* If window starts on a continuation line, maybe adjust the
15514 window start in case the window's width changed. */
15515 if (XMARKER (w->start)->buffer == current_buffer)
15516 compute_window_start_on_continuation_line (w);
15518 w->window_end_valid = 0;
15519 /* If so, we also can't rely on current matrix
15520 and should not fool try_cursor_movement below. */
15521 current_matrix_up_to_date_p = 0;
15524 /* Some sanity checks. */
15525 CHECK_WINDOW_END (w);
15526 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15527 emacs_abort ();
15528 if (BYTEPOS (opoint) < CHARPOS (opoint))
15529 emacs_abort ();
15531 if (mode_line_update_needed (w))
15532 update_mode_line = 1;
15534 /* Point refers normally to the selected window. For any other
15535 window, set up appropriate value. */
15536 if (!EQ (window, selected_window))
15538 ptrdiff_t new_pt = marker_position (w->pointm);
15539 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15540 if (new_pt < BEGV)
15542 new_pt = BEGV;
15543 new_pt_byte = BEGV_BYTE;
15544 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15546 else if (new_pt > (ZV - 1))
15548 new_pt = ZV;
15549 new_pt_byte = ZV_BYTE;
15550 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15553 /* We don't use SET_PT so that the point-motion hooks don't run. */
15554 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15557 /* If any of the character widths specified in the display table
15558 have changed, invalidate the width run cache. It's true that
15559 this may be a bit late to catch such changes, but the rest of
15560 redisplay goes (non-fatally) haywire when the display table is
15561 changed, so why should we worry about doing any better? */
15562 if (current_buffer->width_run_cache)
15564 struct Lisp_Char_Table *disptab = buffer_display_table ();
15566 if (! disptab_matches_widthtab
15567 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15569 invalidate_region_cache (current_buffer,
15570 current_buffer->width_run_cache,
15571 BEG, Z);
15572 recompute_width_table (current_buffer, disptab);
15576 /* If window-start is screwed up, choose a new one. */
15577 if (XMARKER (w->start)->buffer != current_buffer)
15578 goto recenter;
15580 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15582 /* If someone specified a new starting point but did not insist,
15583 check whether it can be used. */
15584 if (w->optional_new_start
15585 && CHARPOS (startp) >= BEGV
15586 && CHARPOS (startp) <= ZV)
15588 w->optional_new_start = 0;
15589 start_display (&it, w, startp);
15590 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15591 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15592 if (IT_CHARPOS (it) == PT)
15593 w->force_start = 1;
15594 /* IT may overshoot PT if text at PT is invisible. */
15595 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15596 w->force_start = 1;
15599 force_start:
15601 /* Handle case where place to start displaying has been specified,
15602 unless the specified location is outside the accessible range. */
15603 if (w->force_start || window_frozen_p (w))
15605 /* We set this later on if we have to adjust point. */
15606 int new_vpos = -1;
15608 w->force_start = 0;
15609 w->vscroll = 0;
15610 w->window_end_valid = 0;
15612 /* Forget any recorded base line for line number display. */
15613 if (!buffer_unchanged_p)
15614 w->base_line_number = 0;
15616 /* Redisplay the mode line. Select the buffer properly for that.
15617 Also, run the hook window-scroll-functions
15618 because we have scrolled. */
15619 /* Note, we do this after clearing force_start because
15620 if there's an error, it is better to forget about force_start
15621 than to get into an infinite loop calling the hook functions
15622 and having them get more errors. */
15623 if (!update_mode_line
15624 || ! NILP (Vwindow_scroll_functions))
15626 update_mode_line = 1;
15627 w->update_mode_line = 1;
15628 startp = run_window_scroll_functions (window, startp);
15631 if (CHARPOS (startp) < BEGV)
15632 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15633 else if (CHARPOS (startp) > ZV)
15634 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15636 /* Redisplay, then check if cursor has been set during the
15637 redisplay. Give up if new fonts were loaded. */
15638 /* We used to issue a CHECK_MARGINS argument to try_window here,
15639 but this causes scrolling to fail when point begins inside
15640 the scroll margin (bug#148) -- cyd */
15641 if (!try_window (window, startp, 0))
15643 w->force_start = 1;
15644 clear_glyph_matrix (w->desired_matrix);
15645 goto need_larger_matrices;
15648 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15650 /* If point does not appear, try to move point so it does
15651 appear. The desired matrix has been built above, so we
15652 can use it here. */
15653 new_vpos = window_box_height (w) / 2;
15656 if (!cursor_row_fully_visible_p (w, 0, 0))
15658 /* Point does appear, but on a line partly visible at end of window.
15659 Move it back to a fully-visible line. */
15660 new_vpos = window_box_height (w);
15662 else if (w->cursor.vpos >=0)
15664 /* Some people insist on not letting point enter the scroll
15665 margin, even though this part handles windows that didn't
15666 scroll at all. */
15667 int window_total_lines
15668 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15669 int margin = min (scroll_margin, window_total_lines / 4);
15670 int pixel_margin = margin * frame_line_height;
15671 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15673 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15674 below, which finds the row to move point to, advances by
15675 the Y coordinate of the _next_ row, see the definition of
15676 MATRIX_ROW_BOTTOM_Y. */
15677 if (w->cursor.vpos < margin + header_line)
15679 w->cursor.vpos = -1;
15680 clear_glyph_matrix (w->desired_matrix);
15681 goto try_to_scroll;
15683 else
15685 int window_height = window_box_height (w);
15687 if (header_line)
15688 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15689 if (w->cursor.y >= window_height - pixel_margin)
15691 w->cursor.vpos = -1;
15692 clear_glyph_matrix (w->desired_matrix);
15693 goto try_to_scroll;
15698 /* If we need to move point for either of the above reasons,
15699 now actually do it. */
15700 if (new_vpos >= 0)
15702 struct glyph_row *row;
15704 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15705 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15706 ++row;
15708 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15709 MATRIX_ROW_START_BYTEPOS (row));
15711 if (w != XWINDOW (selected_window))
15712 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15713 else if (current_buffer == old)
15714 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15716 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15718 /* If we are highlighting the region, then we just changed
15719 the region, so redisplay to show it. */
15720 if (markpos_of_region () >= 0)
15722 clear_glyph_matrix (w->desired_matrix);
15723 if (!try_window (window, startp, 0))
15724 goto need_larger_matrices;
15728 #ifdef GLYPH_DEBUG
15729 debug_method_add (w, "forced window start");
15730 #endif
15731 goto done;
15734 /* Handle case where text has not changed, only point, and it has
15735 not moved off the frame, and we are not retrying after hscroll.
15736 (current_matrix_up_to_date_p is nonzero when retrying.) */
15737 if (current_matrix_up_to_date_p
15738 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15739 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15741 switch (rc)
15743 case CURSOR_MOVEMENT_SUCCESS:
15744 used_current_matrix_p = 1;
15745 goto done;
15747 case CURSOR_MOVEMENT_MUST_SCROLL:
15748 goto try_to_scroll;
15750 default:
15751 emacs_abort ();
15754 /* If current starting point was originally the beginning of a line
15755 but no longer is, find a new starting point. */
15756 else if (w->start_at_line_beg
15757 && !(CHARPOS (startp) <= BEGV
15758 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15760 #ifdef GLYPH_DEBUG
15761 debug_method_add (w, "recenter 1");
15762 #endif
15763 goto recenter;
15766 /* Try scrolling with try_window_id. Value is > 0 if update has
15767 been done, it is -1 if we know that the same window start will
15768 not work. It is 0 if unsuccessful for some other reason. */
15769 else if ((tem = try_window_id (w)) != 0)
15771 #ifdef GLYPH_DEBUG
15772 debug_method_add (w, "try_window_id %d", tem);
15773 #endif
15775 if (fonts_changed_p)
15776 goto need_larger_matrices;
15777 if (tem > 0)
15778 goto done;
15780 /* Otherwise try_window_id has returned -1 which means that we
15781 don't want the alternative below this comment to execute. */
15783 else if (CHARPOS (startp) >= BEGV
15784 && CHARPOS (startp) <= ZV
15785 && PT >= CHARPOS (startp)
15786 && (CHARPOS (startp) < ZV
15787 /* Avoid starting at end of buffer. */
15788 || CHARPOS (startp) == BEGV
15789 || !window_outdated (w)))
15791 int d1, d2, d3, d4, d5, d6;
15793 /* If first window line is a continuation line, and window start
15794 is inside the modified region, but the first change is before
15795 current window start, we must select a new window start.
15797 However, if this is the result of a down-mouse event (e.g. by
15798 extending the mouse-drag-overlay), we don't want to select a
15799 new window start, since that would change the position under
15800 the mouse, resulting in an unwanted mouse-movement rather
15801 than a simple mouse-click. */
15802 if (!w->start_at_line_beg
15803 && NILP (do_mouse_tracking)
15804 && CHARPOS (startp) > BEGV
15805 && CHARPOS (startp) > BEG + beg_unchanged
15806 && CHARPOS (startp) <= Z - end_unchanged
15807 /* Even if w->start_at_line_beg is nil, a new window may
15808 start at a line_beg, since that's how set_buffer_window
15809 sets it. So, we need to check the return value of
15810 compute_window_start_on_continuation_line. (See also
15811 bug#197). */
15812 && XMARKER (w->start)->buffer == current_buffer
15813 && compute_window_start_on_continuation_line (w)
15814 /* It doesn't make sense to force the window start like we
15815 do at label force_start if it is already known that point
15816 will not be visible in the resulting window, because
15817 doing so will move point from its correct position
15818 instead of scrolling the window to bring point into view.
15819 See bug#9324. */
15820 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15822 w->force_start = 1;
15823 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15824 goto force_start;
15827 #ifdef GLYPH_DEBUG
15828 debug_method_add (w, "same window start");
15829 #endif
15831 /* Try to redisplay starting at same place as before.
15832 If point has not moved off frame, accept the results. */
15833 if (!current_matrix_up_to_date_p
15834 /* Don't use try_window_reusing_current_matrix in this case
15835 because a window scroll function can have changed the
15836 buffer. */
15837 || !NILP (Vwindow_scroll_functions)
15838 || MINI_WINDOW_P (w)
15839 || !(used_current_matrix_p
15840 = try_window_reusing_current_matrix (w)))
15842 IF_DEBUG (debug_method_add (w, "1"));
15843 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15844 /* -1 means we need to scroll.
15845 0 means we need new matrices, but fonts_changed_p
15846 is set in that case, so we will detect it below. */
15847 goto try_to_scroll;
15850 if (fonts_changed_p)
15851 goto need_larger_matrices;
15853 if (w->cursor.vpos >= 0)
15855 if (!just_this_one_p
15856 || current_buffer->clip_changed
15857 || BEG_UNCHANGED < CHARPOS (startp))
15858 /* Forget any recorded base line for line number display. */
15859 w->base_line_number = 0;
15861 if (!cursor_row_fully_visible_p (w, 1, 0))
15863 clear_glyph_matrix (w->desired_matrix);
15864 last_line_misfit = 1;
15866 /* Drop through and scroll. */
15867 else
15868 goto done;
15870 else
15871 clear_glyph_matrix (w->desired_matrix);
15874 try_to_scroll:
15876 /* Redisplay the mode line. Select the buffer properly for that. */
15877 if (!update_mode_line)
15879 update_mode_line = 1;
15880 w->update_mode_line = 1;
15883 /* Try to scroll by specified few lines. */
15884 if ((scroll_conservatively
15885 || emacs_scroll_step
15886 || temp_scroll_step
15887 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15888 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15889 && CHARPOS (startp) >= BEGV
15890 && CHARPOS (startp) <= ZV)
15892 /* The function returns -1 if new fonts were loaded, 1 if
15893 successful, 0 if not successful. */
15894 int ss = try_scrolling (window, just_this_one_p,
15895 scroll_conservatively,
15896 emacs_scroll_step,
15897 temp_scroll_step, last_line_misfit);
15898 switch (ss)
15900 case SCROLLING_SUCCESS:
15901 goto done;
15903 case SCROLLING_NEED_LARGER_MATRICES:
15904 goto need_larger_matrices;
15906 case SCROLLING_FAILED:
15907 break;
15909 default:
15910 emacs_abort ();
15914 /* Finally, just choose a place to start which positions point
15915 according to user preferences. */
15917 recenter:
15919 #ifdef GLYPH_DEBUG
15920 debug_method_add (w, "recenter");
15921 #endif
15923 /* Forget any previously recorded base line for line number display. */
15924 if (!buffer_unchanged_p)
15925 w->base_line_number = 0;
15927 /* Determine the window start relative to point. */
15928 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15929 it.current_y = it.last_visible_y;
15930 if (centering_position < 0)
15932 int window_total_lines
15933 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15934 int margin =
15935 scroll_margin > 0
15936 ? min (scroll_margin, window_total_lines / 4)
15937 : 0;
15938 ptrdiff_t margin_pos = CHARPOS (startp);
15939 Lisp_Object aggressive;
15940 int scrolling_up;
15942 /* If there is a scroll margin at the top of the window, find
15943 its character position. */
15944 if (margin
15945 /* Cannot call start_display if startp is not in the
15946 accessible region of the buffer. This can happen when we
15947 have just switched to a different buffer and/or changed
15948 its restriction. In that case, startp is initialized to
15949 the character position 1 (BEGV) because we did not yet
15950 have chance to display the buffer even once. */
15951 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15953 struct it it1;
15954 void *it1data = NULL;
15956 SAVE_IT (it1, it, it1data);
15957 start_display (&it1, w, startp);
15958 move_it_vertically (&it1, margin * frame_line_height);
15959 margin_pos = IT_CHARPOS (it1);
15960 RESTORE_IT (&it, &it, it1data);
15962 scrolling_up = PT > margin_pos;
15963 aggressive =
15964 scrolling_up
15965 ? BVAR (current_buffer, scroll_up_aggressively)
15966 : BVAR (current_buffer, scroll_down_aggressively);
15968 if (!MINI_WINDOW_P (w)
15969 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15971 int pt_offset = 0;
15973 /* Setting scroll-conservatively overrides
15974 scroll-*-aggressively. */
15975 if (!scroll_conservatively && NUMBERP (aggressive))
15977 double float_amount = XFLOATINT (aggressive);
15979 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15980 if (pt_offset == 0 && float_amount > 0)
15981 pt_offset = 1;
15982 if (pt_offset && margin > 0)
15983 margin -= 1;
15985 /* Compute how much to move the window start backward from
15986 point so that point will be displayed where the user
15987 wants it. */
15988 if (scrolling_up)
15990 centering_position = it.last_visible_y;
15991 if (pt_offset)
15992 centering_position -= pt_offset;
15993 centering_position -=
15994 frame_line_height * (1 + margin + (last_line_misfit != 0))
15995 + WINDOW_HEADER_LINE_HEIGHT (w);
15996 /* Don't let point enter the scroll margin near top of
15997 the window. */
15998 if (centering_position < margin * frame_line_height)
15999 centering_position = margin * frame_line_height;
16001 else
16002 centering_position = margin * frame_line_height + pt_offset;
16004 else
16005 /* Set the window start half the height of the window backward
16006 from point. */
16007 centering_position = window_box_height (w) / 2;
16009 move_it_vertically_backward (&it, centering_position);
16011 eassert (IT_CHARPOS (it) >= BEGV);
16013 /* The function move_it_vertically_backward may move over more
16014 than the specified y-distance. If it->w is small, e.g. a
16015 mini-buffer window, we may end up in front of the window's
16016 display area. Start displaying at the start of the line
16017 containing PT in this case. */
16018 if (it.current_y <= 0)
16020 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16021 move_it_vertically_backward (&it, 0);
16022 it.current_y = 0;
16025 it.current_x = it.hpos = 0;
16027 /* Set the window start position here explicitly, to avoid an
16028 infinite loop in case the functions in window-scroll-functions
16029 get errors. */
16030 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16032 /* Run scroll hooks. */
16033 startp = run_window_scroll_functions (window, it.current.pos);
16035 /* Redisplay the window. */
16036 if (!current_matrix_up_to_date_p
16037 || windows_or_buffers_changed
16038 || cursor_type_changed
16039 /* Don't use try_window_reusing_current_matrix in this case
16040 because it can have changed the buffer. */
16041 || !NILP (Vwindow_scroll_functions)
16042 || !just_this_one_p
16043 || MINI_WINDOW_P (w)
16044 || !(used_current_matrix_p
16045 = try_window_reusing_current_matrix (w)))
16046 try_window (window, startp, 0);
16048 /* If new fonts have been loaded (due to fontsets), give up. We
16049 have to start a new redisplay since we need to re-adjust glyph
16050 matrices. */
16051 if (fonts_changed_p)
16052 goto need_larger_matrices;
16054 /* If cursor did not appear assume that the middle of the window is
16055 in the first line of the window. Do it again with the next line.
16056 (Imagine a window of height 100, displaying two lines of height
16057 60. Moving back 50 from it->last_visible_y will end in the first
16058 line.) */
16059 if (w->cursor.vpos < 0)
16061 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16063 clear_glyph_matrix (w->desired_matrix);
16064 move_it_by_lines (&it, 1);
16065 try_window (window, it.current.pos, 0);
16067 else if (PT < IT_CHARPOS (it))
16069 clear_glyph_matrix (w->desired_matrix);
16070 move_it_by_lines (&it, -1);
16071 try_window (window, it.current.pos, 0);
16073 else
16075 /* Not much we can do about it. */
16079 /* Consider the following case: Window starts at BEGV, there is
16080 invisible, intangible text at BEGV, so that display starts at
16081 some point START > BEGV. It can happen that we are called with
16082 PT somewhere between BEGV and START. Try to handle that case. */
16083 if (w->cursor.vpos < 0)
16085 struct glyph_row *row = w->current_matrix->rows;
16086 if (row->mode_line_p)
16087 ++row;
16088 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16091 if (!cursor_row_fully_visible_p (w, 0, 0))
16093 /* If vscroll is enabled, disable it and try again. */
16094 if (w->vscroll)
16096 w->vscroll = 0;
16097 clear_glyph_matrix (w->desired_matrix);
16098 goto recenter;
16101 /* Users who set scroll-conservatively to a large number want
16102 point just above/below the scroll margin. If we ended up
16103 with point's row partially visible, move the window start to
16104 make that row fully visible and out of the margin. */
16105 if (scroll_conservatively > SCROLL_LIMIT)
16107 int window_total_lines
16108 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16109 int margin =
16110 scroll_margin > 0
16111 ? min (scroll_margin, window_total_lines / 4)
16112 : 0;
16113 int move_down = w->cursor.vpos >= window_total_lines / 2;
16115 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16116 clear_glyph_matrix (w->desired_matrix);
16117 if (1 == try_window (window, it.current.pos,
16118 TRY_WINDOW_CHECK_MARGINS))
16119 goto done;
16122 /* If centering point failed to make the whole line visible,
16123 put point at the top instead. That has to make the whole line
16124 visible, if it can be done. */
16125 if (centering_position == 0)
16126 goto done;
16128 clear_glyph_matrix (w->desired_matrix);
16129 centering_position = 0;
16130 goto recenter;
16133 done:
16135 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16136 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16137 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16139 /* Display the mode line, if we must. */
16140 if ((update_mode_line
16141 /* If window not full width, must redo its mode line
16142 if (a) the window to its side is being redone and
16143 (b) we do a frame-based redisplay. This is a consequence
16144 of how inverted lines are drawn in frame-based redisplay. */
16145 || (!just_this_one_p
16146 && !FRAME_WINDOW_P (f)
16147 && !WINDOW_FULL_WIDTH_P (w))
16148 /* Line number to display. */
16149 || w->base_line_pos > 0
16150 /* Column number is displayed and different from the one displayed. */
16151 || (w->column_number_displayed != -1
16152 && (w->column_number_displayed != current_column ())))
16153 /* This means that the window has a mode line. */
16154 && (WINDOW_WANTS_MODELINE_P (w)
16155 || WINDOW_WANTS_HEADER_LINE_P (w)))
16157 display_mode_lines (w);
16159 /* If mode line height has changed, arrange for a thorough
16160 immediate redisplay using the correct mode line height. */
16161 if (WINDOW_WANTS_MODELINE_P (w)
16162 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16164 fonts_changed_p = 1;
16165 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16166 = DESIRED_MODE_LINE_HEIGHT (w);
16169 /* If header line height has changed, arrange for a thorough
16170 immediate redisplay using the correct header line height. */
16171 if (WINDOW_WANTS_HEADER_LINE_P (w)
16172 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16174 fonts_changed_p = 1;
16175 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16176 = DESIRED_HEADER_LINE_HEIGHT (w);
16179 if (fonts_changed_p)
16180 goto need_larger_matrices;
16183 if (!line_number_displayed && w->base_line_pos != -1)
16185 w->base_line_pos = 0;
16186 w->base_line_number = 0;
16189 finish_menu_bars:
16191 /* When we reach a frame's selected window, redo the frame's menu bar. */
16192 if (update_mode_line
16193 && EQ (FRAME_SELECTED_WINDOW (f), window))
16195 int redisplay_menu_p = 0;
16197 if (FRAME_WINDOW_P (f))
16199 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16200 || defined (HAVE_NS) || defined (USE_GTK)
16201 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16202 #else
16203 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16204 #endif
16206 else
16207 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16209 if (redisplay_menu_p)
16210 display_menu_bar (w);
16212 #ifdef HAVE_WINDOW_SYSTEM
16213 if (FRAME_WINDOW_P (f))
16215 #if defined (USE_GTK) || defined (HAVE_NS)
16216 if (FRAME_EXTERNAL_TOOL_BAR (f))
16217 redisplay_tool_bar (f);
16218 #else
16219 if (WINDOWP (f->tool_bar_window)
16220 && (FRAME_TOOL_BAR_LINES (f) > 0
16221 || !NILP (Vauto_resize_tool_bars))
16222 && redisplay_tool_bar (f))
16223 ignore_mouse_drag_p = 1;
16224 #endif
16226 #endif
16229 #ifdef HAVE_WINDOW_SYSTEM
16230 if (FRAME_WINDOW_P (f)
16231 && update_window_fringes (w, (just_this_one_p
16232 || (!used_current_matrix_p && !overlay_arrow_seen)
16233 || w->pseudo_window_p)))
16235 update_begin (f);
16236 block_input ();
16237 if (draw_window_fringes (w, 1))
16238 x_draw_vertical_border (w);
16239 unblock_input ();
16240 update_end (f);
16242 #endif /* HAVE_WINDOW_SYSTEM */
16244 /* We go to this label, with fonts_changed_p set,
16245 if it is necessary to try again using larger glyph matrices.
16246 We have to redeem the scroll bar even in this case,
16247 because the loop in redisplay_internal expects that. */
16248 need_larger_matrices:
16250 finish_scroll_bars:
16252 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16254 /* Set the thumb's position and size. */
16255 set_vertical_scroll_bar (w);
16257 /* Note that we actually used the scroll bar attached to this
16258 window, so it shouldn't be deleted at the end of redisplay. */
16259 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16260 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16263 /* Restore current_buffer and value of point in it. The window
16264 update may have changed the buffer, so first make sure `opoint'
16265 is still valid (Bug#6177). */
16266 if (CHARPOS (opoint) < BEGV)
16267 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16268 else if (CHARPOS (opoint) > ZV)
16269 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16270 else
16271 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16273 set_buffer_internal_1 (old);
16274 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16275 shorter. This can be caused by log truncation in *Messages*. */
16276 if (CHARPOS (lpoint) <= ZV)
16277 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16279 unbind_to (count, Qnil);
16283 /* Build the complete desired matrix of WINDOW with a window start
16284 buffer position POS.
16286 Value is 1 if successful. It is zero if fonts were loaded during
16287 redisplay which makes re-adjusting glyph matrices necessary, and -1
16288 if point would appear in the scroll margins.
16289 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16290 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16291 set in FLAGS.) */
16294 try_window (Lisp_Object window, struct text_pos pos, int flags)
16296 struct window *w = XWINDOW (window);
16297 struct it it;
16298 struct glyph_row *last_text_row = NULL;
16299 struct frame *f = XFRAME (w->frame);
16300 int frame_line_height = default_line_pixel_height (w);
16302 /* Make POS the new window start. */
16303 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16305 /* Mark cursor position as unknown. No overlay arrow seen. */
16306 w->cursor.vpos = -1;
16307 overlay_arrow_seen = 0;
16309 /* Initialize iterator and info to start at POS. */
16310 start_display (&it, w, pos);
16312 /* Display all lines of W. */
16313 while (it.current_y < it.last_visible_y)
16315 if (display_line (&it))
16316 last_text_row = it.glyph_row - 1;
16317 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16318 return 0;
16321 /* Don't let the cursor end in the scroll margins. */
16322 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16323 && !MINI_WINDOW_P (w))
16325 int this_scroll_margin;
16326 int window_total_lines
16327 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16329 if (scroll_margin > 0)
16331 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16332 this_scroll_margin *= frame_line_height;
16334 else
16335 this_scroll_margin = 0;
16337 if ((w->cursor.y >= 0 /* not vscrolled */
16338 && w->cursor.y < this_scroll_margin
16339 && CHARPOS (pos) > BEGV
16340 && IT_CHARPOS (it) < ZV)
16341 /* rms: considering make_cursor_line_fully_visible_p here
16342 seems to give wrong results. We don't want to recenter
16343 when the last line is partly visible, we want to allow
16344 that case to be handled in the usual way. */
16345 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16347 w->cursor.vpos = -1;
16348 clear_glyph_matrix (w->desired_matrix);
16349 return -1;
16353 /* If bottom moved off end of frame, change mode line percentage. */
16354 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16355 w->update_mode_line = 1;
16357 /* Set window_end_pos to the offset of the last character displayed
16358 on the window from the end of current_buffer. Set
16359 window_end_vpos to its row number. */
16360 if (last_text_row)
16362 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16363 adjust_window_ends (w, last_text_row, 0);
16364 eassert
16365 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16366 w->window_end_vpos)));
16368 else
16370 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16371 w->window_end_pos = Z - ZV;
16372 w->window_end_vpos = 0;
16375 /* But that is not valid info until redisplay finishes. */
16376 w->window_end_valid = 0;
16377 return 1;
16382 /************************************************************************
16383 Window redisplay reusing current matrix when buffer has not changed
16384 ************************************************************************/
16386 /* Try redisplay of window W showing an unchanged buffer with a
16387 different window start than the last time it was displayed by
16388 reusing its current matrix. Value is non-zero if successful.
16389 W->start is the new window start. */
16391 static int
16392 try_window_reusing_current_matrix (struct window *w)
16394 struct frame *f = XFRAME (w->frame);
16395 struct glyph_row *bottom_row;
16396 struct it it;
16397 struct run run;
16398 struct text_pos start, new_start;
16399 int nrows_scrolled, i;
16400 struct glyph_row *last_text_row;
16401 struct glyph_row *last_reused_text_row;
16402 struct glyph_row *start_row;
16403 int start_vpos, min_y, max_y;
16405 #ifdef GLYPH_DEBUG
16406 if (inhibit_try_window_reusing)
16407 return 0;
16408 #endif
16410 if (/* This function doesn't handle terminal frames. */
16411 !FRAME_WINDOW_P (f)
16412 /* Don't try to reuse the display if windows have been split
16413 or such. */
16414 || windows_or_buffers_changed
16415 || cursor_type_changed)
16416 return 0;
16418 /* Can't do this if region may have changed. */
16419 if (markpos_of_region () >= 0
16420 || w->region_showing
16421 || !NILP (Vshow_trailing_whitespace))
16422 return 0;
16424 /* If top-line visibility has changed, give up. */
16425 if (WINDOW_WANTS_HEADER_LINE_P (w)
16426 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16427 return 0;
16429 /* Give up if old or new display is scrolled vertically. We could
16430 make this function handle this, but right now it doesn't. */
16431 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16432 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16433 return 0;
16435 /* The variable new_start now holds the new window start. The old
16436 start `start' can be determined from the current matrix. */
16437 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16438 start = start_row->minpos;
16439 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16441 /* Clear the desired matrix for the display below. */
16442 clear_glyph_matrix (w->desired_matrix);
16444 if (CHARPOS (new_start) <= CHARPOS (start))
16446 /* Don't use this method if the display starts with an ellipsis
16447 displayed for invisible text. It's not easy to handle that case
16448 below, and it's certainly not worth the effort since this is
16449 not a frequent case. */
16450 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16451 return 0;
16453 IF_DEBUG (debug_method_add (w, "twu1"));
16455 /* Display up to a row that can be reused. The variable
16456 last_text_row is set to the last row displayed that displays
16457 text. Note that it.vpos == 0 if or if not there is a
16458 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16459 start_display (&it, w, new_start);
16460 w->cursor.vpos = -1;
16461 last_text_row = last_reused_text_row = NULL;
16463 while (it.current_y < it.last_visible_y
16464 && !fonts_changed_p)
16466 /* If we have reached into the characters in the START row,
16467 that means the line boundaries have changed. So we
16468 can't start copying with the row START. Maybe it will
16469 work to start copying with the following row. */
16470 while (IT_CHARPOS (it) > CHARPOS (start))
16472 /* Advance to the next row as the "start". */
16473 start_row++;
16474 start = start_row->minpos;
16475 /* If there are no more rows to try, or just one, give up. */
16476 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16477 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16478 || CHARPOS (start) == ZV)
16480 clear_glyph_matrix (w->desired_matrix);
16481 return 0;
16484 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16486 /* If we have reached alignment, we can copy the rest of the
16487 rows. */
16488 if (IT_CHARPOS (it) == CHARPOS (start)
16489 /* Don't accept "alignment" inside a display vector,
16490 since start_row could have started in the middle of
16491 that same display vector (thus their character
16492 positions match), and we have no way of telling if
16493 that is the case. */
16494 && it.current.dpvec_index < 0)
16495 break;
16497 if (display_line (&it))
16498 last_text_row = it.glyph_row - 1;
16502 /* A value of current_y < last_visible_y means that we stopped
16503 at the previous window start, which in turn means that we
16504 have at least one reusable row. */
16505 if (it.current_y < it.last_visible_y)
16507 struct glyph_row *row;
16509 /* IT.vpos always starts from 0; it counts text lines. */
16510 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16512 /* Find PT if not already found in the lines displayed. */
16513 if (w->cursor.vpos < 0)
16515 int dy = it.current_y - start_row->y;
16517 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16518 row = row_containing_pos (w, PT, row, NULL, dy);
16519 if (row)
16520 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16521 dy, nrows_scrolled);
16522 else
16524 clear_glyph_matrix (w->desired_matrix);
16525 return 0;
16529 /* Scroll the display. Do it before the current matrix is
16530 changed. The problem here is that update has not yet
16531 run, i.e. part of the current matrix is not up to date.
16532 scroll_run_hook will clear the cursor, and use the
16533 current matrix to get the height of the row the cursor is
16534 in. */
16535 run.current_y = start_row->y;
16536 run.desired_y = it.current_y;
16537 run.height = it.last_visible_y - it.current_y;
16539 if (run.height > 0 && run.current_y != run.desired_y)
16541 update_begin (f);
16542 FRAME_RIF (f)->update_window_begin_hook (w);
16543 FRAME_RIF (f)->clear_window_mouse_face (w);
16544 FRAME_RIF (f)->scroll_run_hook (w, &run);
16545 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16546 update_end (f);
16549 /* Shift current matrix down by nrows_scrolled lines. */
16550 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16551 rotate_matrix (w->current_matrix,
16552 start_vpos,
16553 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16554 nrows_scrolled);
16556 /* Disable lines that must be updated. */
16557 for (i = 0; i < nrows_scrolled; ++i)
16558 (start_row + i)->enabled_p = 0;
16560 /* Re-compute Y positions. */
16561 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16562 max_y = it.last_visible_y;
16563 for (row = start_row + nrows_scrolled;
16564 row < bottom_row;
16565 ++row)
16567 row->y = it.current_y;
16568 row->visible_height = row->height;
16570 if (row->y < min_y)
16571 row->visible_height -= min_y - row->y;
16572 if (row->y + row->height > max_y)
16573 row->visible_height -= row->y + row->height - max_y;
16574 if (row->fringe_bitmap_periodic_p)
16575 row->redraw_fringe_bitmaps_p = 1;
16577 it.current_y += row->height;
16579 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16580 last_reused_text_row = row;
16581 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16582 break;
16585 /* Disable lines in the current matrix which are now
16586 below the window. */
16587 for (++row; row < bottom_row; ++row)
16588 row->enabled_p = row->mode_line_p = 0;
16591 /* Update window_end_pos etc.; last_reused_text_row is the last
16592 reused row from the current matrix containing text, if any.
16593 The value of last_text_row is the last displayed line
16594 containing text. */
16595 if (last_reused_text_row)
16596 adjust_window_ends (w, last_reused_text_row, 1);
16597 else if (last_text_row)
16598 adjust_window_ends (w, last_text_row, 0);
16599 else
16601 /* This window must be completely empty. */
16602 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16603 w->window_end_pos = Z - ZV;
16604 w->window_end_vpos = 0;
16606 w->window_end_valid = 0;
16608 /* Update hint: don't try scrolling again in update_window. */
16609 w->desired_matrix->no_scrolling_p = 1;
16611 #ifdef GLYPH_DEBUG
16612 debug_method_add (w, "try_window_reusing_current_matrix 1");
16613 #endif
16614 return 1;
16616 else if (CHARPOS (new_start) > CHARPOS (start))
16618 struct glyph_row *pt_row, *row;
16619 struct glyph_row *first_reusable_row;
16620 struct glyph_row *first_row_to_display;
16621 int dy;
16622 int yb = window_text_bottom_y (w);
16624 /* Find the row starting at new_start, if there is one. Don't
16625 reuse a partially visible line at the end. */
16626 first_reusable_row = start_row;
16627 while (first_reusable_row->enabled_p
16628 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16629 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16630 < CHARPOS (new_start)))
16631 ++first_reusable_row;
16633 /* Give up if there is no row to reuse. */
16634 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16635 || !first_reusable_row->enabled_p
16636 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16637 != CHARPOS (new_start)))
16638 return 0;
16640 /* We can reuse fully visible rows beginning with
16641 first_reusable_row to the end of the window. Set
16642 first_row_to_display to the first row that cannot be reused.
16643 Set pt_row to the row containing point, if there is any. */
16644 pt_row = NULL;
16645 for (first_row_to_display = first_reusable_row;
16646 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16647 ++first_row_to_display)
16649 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16650 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16651 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16652 && first_row_to_display->ends_at_zv_p
16653 && pt_row == NULL)))
16654 pt_row = first_row_to_display;
16657 /* Start displaying at the start of first_row_to_display. */
16658 eassert (first_row_to_display->y < yb);
16659 init_to_row_start (&it, w, first_row_to_display);
16661 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16662 - start_vpos);
16663 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16664 - nrows_scrolled);
16665 it.current_y = (first_row_to_display->y - first_reusable_row->y
16666 + WINDOW_HEADER_LINE_HEIGHT (w));
16668 /* Display lines beginning with first_row_to_display in the
16669 desired matrix. Set last_text_row to the last row displayed
16670 that displays text. */
16671 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16672 if (pt_row == NULL)
16673 w->cursor.vpos = -1;
16674 last_text_row = NULL;
16675 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16676 if (display_line (&it))
16677 last_text_row = it.glyph_row - 1;
16679 /* If point is in a reused row, adjust y and vpos of the cursor
16680 position. */
16681 if (pt_row)
16683 w->cursor.vpos -= nrows_scrolled;
16684 w->cursor.y -= first_reusable_row->y - start_row->y;
16687 /* Give up if point isn't in a row displayed or reused. (This
16688 also handles the case where w->cursor.vpos < nrows_scrolled
16689 after the calls to display_line, which can happen with scroll
16690 margins. See bug#1295.) */
16691 if (w->cursor.vpos < 0)
16693 clear_glyph_matrix (w->desired_matrix);
16694 return 0;
16697 /* Scroll the display. */
16698 run.current_y = first_reusable_row->y;
16699 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16700 run.height = it.last_visible_y - run.current_y;
16701 dy = run.current_y - run.desired_y;
16703 if (run.height)
16705 update_begin (f);
16706 FRAME_RIF (f)->update_window_begin_hook (w);
16707 FRAME_RIF (f)->clear_window_mouse_face (w);
16708 FRAME_RIF (f)->scroll_run_hook (w, &run);
16709 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16710 update_end (f);
16713 /* Adjust Y positions of reused rows. */
16714 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16715 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16716 max_y = it.last_visible_y;
16717 for (row = first_reusable_row; row < first_row_to_display; ++row)
16719 row->y -= dy;
16720 row->visible_height = row->height;
16721 if (row->y < min_y)
16722 row->visible_height -= min_y - row->y;
16723 if (row->y + row->height > max_y)
16724 row->visible_height -= row->y + row->height - max_y;
16725 if (row->fringe_bitmap_periodic_p)
16726 row->redraw_fringe_bitmaps_p = 1;
16729 /* Scroll the current matrix. */
16730 eassert (nrows_scrolled > 0);
16731 rotate_matrix (w->current_matrix,
16732 start_vpos,
16733 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16734 -nrows_scrolled);
16736 /* Disable rows not reused. */
16737 for (row -= nrows_scrolled; row < bottom_row; ++row)
16738 row->enabled_p = 0;
16740 /* Point may have moved to a different line, so we cannot assume that
16741 the previous cursor position is valid; locate the correct row. */
16742 if (pt_row)
16744 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16745 row < bottom_row
16746 && PT >= MATRIX_ROW_END_CHARPOS (row)
16747 && !row->ends_at_zv_p;
16748 row++)
16750 w->cursor.vpos++;
16751 w->cursor.y = row->y;
16753 if (row < bottom_row)
16755 /* Can't simply scan the row for point with
16756 bidi-reordered glyph rows. Let set_cursor_from_row
16757 figure out where to put the cursor, and if it fails,
16758 give up. */
16759 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16761 if (!set_cursor_from_row (w, row, w->current_matrix,
16762 0, 0, 0, 0))
16764 clear_glyph_matrix (w->desired_matrix);
16765 return 0;
16768 else
16770 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16771 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16773 for (; glyph < end
16774 && (!BUFFERP (glyph->object)
16775 || glyph->charpos < PT);
16776 glyph++)
16778 w->cursor.hpos++;
16779 w->cursor.x += glyph->pixel_width;
16785 /* Adjust window end. A null value of last_text_row means that
16786 the window end is in reused rows which in turn means that
16787 only its vpos can have changed. */
16788 if (last_text_row)
16789 adjust_window_ends (w, last_text_row, 0);
16790 else
16791 w->window_end_vpos -= nrows_scrolled;
16793 w->window_end_valid = 0;
16794 w->desired_matrix->no_scrolling_p = 1;
16796 #ifdef GLYPH_DEBUG
16797 debug_method_add (w, "try_window_reusing_current_matrix 2");
16798 #endif
16799 return 1;
16802 return 0;
16807 /************************************************************************
16808 Window redisplay reusing current matrix when buffer has changed
16809 ************************************************************************/
16811 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16812 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16813 ptrdiff_t *, ptrdiff_t *);
16814 static struct glyph_row *
16815 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16816 struct glyph_row *);
16819 /* Return the last row in MATRIX displaying text. If row START is
16820 non-null, start searching with that row. IT gives the dimensions
16821 of the display. Value is null if matrix is empty; otherwise it is
16822 a pointer to the row found. */
16824 static struct glyph_row *
16825 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16826 struct glyph_row *start)
16828 struct glyph_row *row, *row_found;
16830 /* Set row_found to the last row in IT->w's current matrix
16831 displaying text. The loop looks funny but think of partially
16832 visible lines. */
16833 row_found = NULL;
16834 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16835 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16837 eassert (row->enabled_p);
16838 row_found = row;
16839 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16840 break;
16841 ++row;
16844 return row_found;
16848 /* Return the last row in the current matrix of W that is not affected
16849 by changes at the start of current_buffer that occurred since W's
16850 current matrix was built. Value is null if no such row exists.
16852 BEG_UNCHANGED us the number of characters unchanged at the start of
16853 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16854 first changed character in current_buffer. Characters at positions <
16855 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16856 when the current matrix was built. */
16858 static struct glyph_row *
16859 find_last_unchanged_at_beg_row (struct window *w)
16861 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16862 struct glyph_row *row;
16863 struct glyph_row *row_found = NULL;
16864 int yb = window_text_bottom_y (w);
16866 /* Find the last row displaying unchanged text. */
16867 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16868 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16869 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16870 ++row)
16872 if (/* If row ends before first_changed_pos, it is unchanged,
16873 except in some case. */
16874 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16875 /* When row ends in ZV and we write at ZV it is not
16876 unchanged. */
16877 && !row->ends_at_zv_p
16878 /* When first_changed_pos is the end of a continued line,
16879 row is not unchanged because it may be no longer
16880 continued. */
16881 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16882 && (row->continued_p
16883 || row->exact_window_width_line_p))
16884 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16885 needs to be recomputed, so don't consider this row as
16886 unchanged. This happens when the last line was
16887 bidi-reordered and was killed immediately before this
16888 redisplay cycle. In that case, ROW->end stores the
16889 buffer position of the first visual-order character of
16890 the killed text, which is now beyond ZV. */
16891 && CHARPOS (row->end.pos) <= ZV)
16892 row_found = row;
16894 /* Stop if last visible row. */
16895 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16896 break;
16899 return row_found;
16903 /* Find the first glyph row in the current matrix of W that is not
16904 affected by changes at the end of current_buffer since the
16905 time W's current matrix was built.
16907 Return in *DELTA the number of chars by which buffer positions in
16908 unchanged text at the end of current_buffer must be adjusted.
16910 Return in *DELTA_BYTES the corresponding number of bytes.
16912 Value is null if no such row exists, i.e. all rows are affected by
16913 changes. */
16915 static struct glyph_row *
16916 find_first_unchanged_at_end_row (struct window *w,
16917 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16919 struct glyph_row *row;
16920 struct glyph_row *row_found = NULL;
16922 *delta = *delta_bytes = 0;
16924 /* Display must not have been paused, otherwise the current matrix
16925 is not up to date. */
16926 eassert (w->window_end_valid);
16928 /* A value of window_end_pos >= END_UNCHANGED means that the window
16929 end is in the range of changed text. If so, there is no
16930 unchanged row at the end of W's current matrix. */
16931 if (w->window_end_pos >= END_UNCHANGED)
16932 return NULL;
16934 /* Set row to the last row in W's current matrix displaying text. */
16935 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16937 /* If matrix is entirely empty, no unchanged row exists. */
16938 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16940 /* The value of row is the last glyph row in the matrix having a
16941 meaningful buffer position in it. The end position of row
16942 corresponds to window_end_pos. This allows us to translate
16943 buffer positions in the current matrix to current buffer
16944 positions for characters not in changed text. */
16945 ptrdiff_t Z_old =
16946 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16947 ptrdiff_t Z_BYTE_old =
16948 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16949 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16950 struct glyph_row *first_text_row
16951 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16953 *delta = Z - Z_old;
16954 *delta_bytes = Z_BYTE - Z_BYTE_old;
16956 /* Set last_unchanged_pos to the buffer position of the last
16957 character in the buffer that has not been changed. Z is the
16958 index + 1 of the last character in current_buffer, i.e. by
16959 subtracting END_UNCHANGED we get the index of the last
16960 unchanged character, and we have to add BEG to get its buffer
16961 position. */
16962 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16963 last_unchanged_pos_old = last_unchanged_pos - *delta;
16965 /* Search backward from ROW for a row displaying a line that
16966 starts at a minimum position >= last_unchanged_pos_old. */
16967 for (; row > first_text_row; --row)
16969 /* This used to abort, but it can happen.
16970 It is ok to just stop the search instead here. KFS. */
16971 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16972 break;
16974 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16975 row_found = row;
16979 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16981 return row_found;
16985 /* Make sure that glyph rows in the current matrix of window W
16986 reference the same glyph memory as corresponding rows in the
16987 frame's frame matrix. This function is called after scrolling W's
16988 current matrix on a terminal frame in try_window_id and
16989 try_window_reusing_current_matrix. */
16991 static void
16992 sync_frame_with_window_matrix_rows (struct window *w)
16994 struct frame *f = XFRAME (w->frame);
16995 struct glyph_row *window_row, *window_row_end, *frame_row;
16997 /* Preconditions: W must be a leaf window and full-width. Its frame
16998 must have a frame matrix. */
16999 eassert (BUFFERP (w->contents));
17000 eassert (WINDOW_FULL_WIDTH_P (w));
17001 eassert (!FRAME_WINDOW_P (f));
17003 /* If W is a full-width window, glyph pointers in W's current matrix
17004 have, by definition, to be the same as glyph pointers in the
17005 corresponding frame matrix. Note that frame matrices have no
17006 marginal areas (see build_frame_matrix). */
17007 window_row = w->current_matrix->rows;
17008 window_row_end = window_row + w->current_matrix->nrows;
17009 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17010 while (window_row < window_row_end)
17012 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17013 struct glyph *end = window_row->glyphs[LAST_AREA];
17015 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17016 frame_row->glyphs[TEXT_AREA] = start;
17017 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17018 frame_row->glyphs[LAST_AREA] = end;
17020 /* Disable frame rows whose corresponding window rows have
17021 been disabled in try_window_id. */
17022 if (!window_row->enabled_p)
17023 frame_row->enabled_p = 0;
17025 ++window_row, ++frame_row;
17030 /* Find the glyph row in window W containing CHARPOS. Consider all
17031 rows between START and END (not inclusive). END null means search
17032 all rows to the end of the display area of W. Value is the row
17033 containing CHARPOS or null. */
17035 struct glyph_row *
17036 row_containing_pos (struct window *w, ptrdiff_t charpos,
17037 struct glyph_row *start, struct glyph_row *end, int dy)
17039 struct glyph_row *row = start;
17040 struct glyph_row *best_row = NULL;
17041 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17042 int last_y;
17044 /* If we happen to start on a header-line, skip that. */
17045 if (row->mode_line_p)
17046 ++row;
17048 if ((end && row >= end) || !row->enabled_p)
17049 return NULL;
17051 last_y = window_text_bottom_y (w) - dy;
17053 while (1)
17055 /* Give up if we have gone too far. */
17056 if (end && row >= end)
17057 return NULL;
17058 /* This formerly returned if they were equal.
17059 I think that both quantities are of a "last plus one" type;
17060 if so, when they are equal, the row is within the screen. -- rms. */
17061 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17062 return NULL;
17064 /* If it is in this row, return this row. */
17065 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17066 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17067 /* The end position of a row equals the start
17068 position of the next row. If CHARPOS is there, we
17069 would rather consider it displayed in the next
17070 line, except when this line ends in ZV. */
17071 && !row_for_charpos_p (row, charpos)))
17072 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17074 struct glyph *g;
17076 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17077 || (!best_row && !row->continued_p))
17078 return row;
17079 /* In bidi-reordered rows, there could be several rows whose
17080 edges surround CHARPOS, all of these rows belonging to
17081 the same continued line. We need to find the row which
17082 fits CHARPOS the best. */
17083 for (g = row->glyphs[TEXT_AREA];
17084 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17085 g++)
17087 if (!STRINGP (g->object))
17089 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17091 mindif = eabs (g->charpos - charpos);
17092 best_row = row;
17093 /* Exact match always wins. */
17094 if (mindif == 0)
17095 return best_row;
17100 else if (best_row && !row->continued_p)
17101 return best_row;
17102 ++row;
17107 /* Try to redisplay window W by reusing its existing display. W's
17108 current matrix must be up to date when this function is called,
17109 i.e. window_end_valid must be nonzero.
17111 Value is
17113 1 if display has been updated
17114 0 if otherwise unsuccessful
17115 -1 if redisplay with same window start is known not to succeed
17117 The following steps are performed:
17119 1. Find the last row in the current matrix of W that is not
17120 affected by changes at the start of current_buffer. If no such row
17121 is found, give up.
17123 2. Find the first row in W's current matrix that is not affected by
17124 changes at the end of current_buffer. Maybe there is no such row.
17126 3. Display lines beginning with the row + 1 found in step 1 to the
17127 row found in step 2 or, if step 2 didn't find a row, to the end of
17128 the window.
17130 4. If cursor is not known to appear on the window, give up.
17132 5. If display stopped at the row found in step 2, scroll the
17133 display and current matrix as needed.
17135 6. Maybe display some lines at the end of W, if we must. This can
17136 happen under various circumstances, like a partially visible line
17137 becoming fully visible, or because newly displayed lines are displayed
17138 in smaller font sizes.
17140 7. Update W's window end information. */
17142 static int
17143 try_window_id (struct window *w)
17145 struct frame *f = XFRAME (w->frame);
17146 struct glyph_matrix *current_matrix = w->current_matrix;
17147 struct glyph_matrix *desired_matrix = w->desired_matrix;
17148 struct glyph_row *last_unchanged_at_beg_row;
17149 struct glyph_row *first_unchanged_at_end_row;
17150 struct glyph_row *row;
17151 struct glyph_row *bottom_row;
17152 int bottom_vpos;
17153 struct it it;
17154 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17155 int dvpos, dy;
17156 struct text_pos start_pos;
17157 struct run run;
17158 int first_unchanged_at_end_vpos = 0;
17159 struct glyph_row *last_text_row, *last_text_row_at_end;
17160 struct text_pos start;
17161 ptrdiff_t first_changed_charpos, last_changed_charpos;
17163 #ifdef GLYPH_DEBUG
17164 if (inhibit_try_window_id)
17165 return 0;
17166 #endif
17168 /* This is handy for debugging. */
17169 #if 0
17170 #define GIVE_UP(X) \
17171 do { \
17172 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17173 return 0; \
17174 } while (0)
17175 #else
17176 #define GIVE_UP(X) return 0
17177 #endif
17179 SET_TEXT_POS_FROM_MARKER (start, w->start);
17181 /* Don't use this for mini-windows because these can show
17182 messages and mini-buffers, and we don't handle that here. */
17183 if (MINI_WINDOW_P (w))
17184 GIVE_UP (1);
17186 /* This flag is used to prevent redisplay optimizations. */
17187 if (windows_or_buffers_changed || cursor_type_changed)
17188 GIVE_UP (2);
17190 /* Verify that narrowing has not changed.
17191 Also verify that we were not told to prevent redisplay optimizations.
17192 It would be nice to further
17193 reduce the number of cases where this prevents try_window_id. */
17194 if (current_buffer->clip_changed
17195 || current_buffer->prevent_redisplay_optimizations_p)
17196 GIVE_UP (3);
17198 /* Window must either use window-based redisplay or be full width. */
17199 if (!FRAME_WINDOW_P (f)
17200 && (!FRAME_LINE_INS_DEL_OK (f)
17201 || !WINDOW_FULL_WIDTH_P (w)))
17202 GIVE_UP (4);
17204 /* Give up if point is known NOT to appear in W. */
17205 if (PT < CHARPOS (start))
17206 GIVE_UP (5);
17208 /* Another way to prevent redisplay optimizations. */
17209 if (w->last_modified == 0)
17210 GIVE_UP (6);
17212 /* Verify that window is not hscrolled. */
17213 if (w->hscroll != 0)
17214 GIVE_UP (7);
17216 /* Verify that display wasn't paused. */
17217 if (!w->window_end_valid)
17218 GIVE_UP (8);
17220 /* Can't use this if highlighting a region because a cursor movement
17221 will do more than just set the cursor. */
17222 if (markpos_of_region () >= 0)
17223 GIVE_UP (9);
17225 /* Likewise if highlighting trailing whitespace. */
17226 if (!NILP (Vshow_trailing_whitespace))
17227 GIVE_UP (11);
17229 /* Likewise if showing a region. */
17230 if (w->region_showing)
17231 GIVE_UP (10);
17233 /* Can't use this if overlay arrow position and/or string have
17234 changed. */
17235 if (overlay_arrows_changed_p ())
17236 GIVE_UP (12);
17238 /* When word-wrap is on, adding a space to the first word of a
17239 wrapped line can change the wrap position, altering the line
17240 above it. It might be worthwhile to handle this more
17241 intelligently, but for now just redisplay from scratch. */
17242 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17243 GIVE_UP (21);
17245 /* Under bidi reordering, adding or deleting a character in the
17246 beginning of a paragraph, before the first strong directional
17247 character, can change the base direction of the paragraph (unless
17248 the buffer specifies a fixed paragraph direction), which will
17249 require to redisplay the whole paragraph. It might be worthwhile
17250 to find the paragraph limits and widen the range of redisplayed
17251 lines to that, but for now just give up this optimization and
17252 redisplay from scratch. */
17253 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17254 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17255 GIVE_UP (22);
17257 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17258 only if buffer has really changed. The reason is that the gap is
17259 initially at Z for freshly visited files. The code below would
17260 set end_unchanged to 0 in that case. */
17261 if (MODIFF > SAVE_MODIFF
17262 /* This seems to happen sometimes after saving a buffer. */
17263 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17265 if (GPT - BEG < BEG_UNCHANGED)
17266 BEG_UNCHANGED = GPT - BEG;
17267 if (Z - GPT < END_UNCHANGED)
17268 END_UNCHANGED = Z - GPT;
17271 /* The position of the first and last character that has been changed. */
17272 first_changed_charpos = BEG + BEG_UNCHANGED;
17273 last_changed_charpos = Z - END_UNCHANGED;
17275 /* If window starts after a line end, and the last change is in
17276 front of that newline, then changes don't affect the display.
17277 This case happens with stealth-fontification. Note that although
17278 the display is unchanged, glyph positions in the matrix have to
17279 be adjusted, of course. */
17280 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17281 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17282 && ((last_changed_charpos < CHARPOS (start)
17283 && CHARPOS (start) == BEGV)
17284 || (last_changed_charpos < CHARPOS (start) - 1
17285 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17287 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17288 struct glyph_row *r0;
17290 /* Compute how many chars/bytes have been added to or removed
17291 from the buffer. */
17292 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17293 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17294 Z_delta = Z - Z_old;
17295 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17297 /* Give up if PT is not in the window. Note that it already has
17298 been checked at the start of try_window_id that PT is not in
17299 front of the window start. */
17300 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17301 GIVE_UP (13);
17303 /* If window start is unchanged, we can reuse the whole matrix
17304 as is, after adjusting glyph positions. No need to compute
17305 the window end again, since its offset from Z hasn't changed. */
17306 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17307 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17308 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17309 /* PT must not be in a partially visible line. */
17310 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17311 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17313 /* Adjust positions in the glyph matrix. */
17314 if (Z_delta || Z_delta_bytes)
17316 struct glyph_row *r1
17317 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17318 increment_matrix_positions (w->current_matrix,
17319 MATRIX_ROW_VPOS (r0, current_matrix),
17320 MATRIX_ROW_VPOS (r1, current_matrix),
17321 Z_delta, Z_delta_bytes);
17324 /* Set the cursor. */
17325 row = row_containing_pos (w, PT, r0, NULL, 0);
17326 if (row)
17327 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17328 else
17329 emacs_abort ();
17330 return 1;
17334 /* Handle the case that changes are all below what is displayed in
17335 the window, and that PT is in the window. This shortcut cannot
17336 be taken if ZV is visible in the window, and text has been added
17337 there that is visible in the window. */
17338 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17339 /* ZV is not visible in the window, or there are no
17340 changes at ZV, actually. */
17341 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17342 || first_changed_charpos == last_changed_charpos))
17344 struct glyph_row *r0;
17346 /* Give up if PT is not in the window. Note that it already has
17347 been checked at the start of try_window_id that PT is not in
17348 front of the window start. */
17349 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17350 GIVE_UP (14);
17352 /* If window start is unchanged, we can reuse the whole matrix
17353 as is, without changing glyph positions since no text has
17354 been added/removed in front of the window end. */
17355 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17356 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17357 /* PT must not be in a partially visible line. */
17358 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17359 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17361 /* We have to compute the window end anew since text
17362 could have been added/removed after it. */
17363 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17364 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17366 /* Set the cursor. */
17367 row = row_containing_pos (w, PT, r0, NULL, 0);
17368 if (row)
17369 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17370 else
17371 emacs_abort ();
17372 return 2;
17376 /* Give up if window start is in the changed area.
17378 The condition used to read
17380 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17382 but why that was tested escapes me at the moment. */
17383 if (CHARPOS (start) >= first_changed_charpos
17384 && CHARPOS (start) <= last_changed_charpos)
17385 GIVE_UP (15);
17387 /* Check that window start agrees with the start of the first glyph
17388 row in its current matrix. Check this after we know the window
17389 start is not in changed text, otherwise positions would not be
17390 comparable. */
17391 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17392 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17393 GIVE_UP (16);
17395 /* Give up if the window ends in strings. Overlay strings
17396 at the end are difficult to handle, so don't try. */
17397 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17398 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17399 GIVE_UP (20);
17401 /* Compute the position at which we have to start displaying new
17402 lines. Some of the lines at the top of the window might be
17403 reusable because they are not displaying changed text. Find the
17404 last row in W's current matrix not affected by changes at the
17405 start of current_buffer. Value is null if changes start in the
17406 first line of window. */
17407 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17408 if (last_unchanged_at_beg_row)
17410 /* Avoid starting to display in the middle of a character, a TAB
17411 for instance. This is easier than to set up the iterator
17412 exactly, and it's not a frequent case, so the additional
17413 effort wouldn't really pay off. */
17414 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17415 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17416 && last_unchanged_at_beg_row > w->current_matrix->rows)
17417 --last_unchanged_at_beg_row;
17419 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17420 GIVE_UP (17);
17422 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17423 GIVE_UP (18);
17424 start_pos = it.current.pos;
17426 /* Start displaying new lines in the desired matrix at the same
17427 vpos we would use in the current matrix, i.e. below
17428 last_unchanged_at_beg_row. */
17429 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17430 current_matrix);
17431 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17432 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17434 eassert (it.hpos == 0 && it.current_x == 0);
17436 else
17438 /* There are no reusable lines at the start of the window.
17439 Start displaying in the first text line. */
17440 start_display (&it, w, start);
17441 it.vpos = it.first_vpos;
17442 start_pos = it.current.pos;
17445 /* Find the first row that is not affected by changes at the end of
17446 the buffer. Value will be null if there is no unchanged row, in
17447 which case we must redisplay to the end of the window. delta
17448 will be set to the value by which buffer positions beginning with
17449 first_unchanged_at_end_row have to be adjusted due to text
17450 changes. */
17451 first_unchanged_at_end_row
17452 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17453 IF_DEBUG (debug_delta = delta);
17454 IF_DEBUG (debug_delta_bytes = delta_bytes);
17456 /* Set stop_pos to the buffer position up to which we will have to
17457 display new lines. If first_unchanged_at_end_row != NULL, this
17458 is the buffer position of the start of the line displayed in that
17459 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17460 that we don't stop at a buffer position. */
17461 stop_pos = 0;
17462 if (first_unchanged_at_end_row)
17464 eassert (last_unchanged_at_beg_row == NULL
17465 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17467 /* If this is a continuation line, move forward to the next one
17468 that isn't. Changes in lines above affect this line.
17469 Caution: this may move first_unchanged_at_end_row to a row
17470 not displaying text. */
17471 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17472 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17473 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17474 < it.last_visible_y))
17475 ++first_unchanged_at_end_row;
17477 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17478 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17479 >= it.last_visible_y))
17480 first_unchanged_at_end_row = NULL;
17481 else
17483 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17484 + delta);
17485 first_unchanged_at_end_vpos
17486 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17487 eassert (stop_pos >= Z - END_UNCHANGED);
17490 else if (last_unchanged_at_beg_row == NULL)
17491 GIVE_UP (19);
17494 #ifdef GLYPH_DEBUG
17496 /* Either there is no unchanged row at the end, or the one we have
17497 now displays text. This is a necessary condition for the window
17498 end pos calculation at the end of this function. */
17499 eassert (first_unchanged_at_end_row == NULL
17500 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17502 debug_last_unchanged_at_beg_vpos
17503 = (last_unchanged_at_beg_row
17504 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17505 : -1);
17506 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17508 #endif /* GLYPH_DEBUG */
17511 /* Display new lines. Set last_text_row to the last new line
17512 displayed which has text on it, i.e. might end up as being the
17513 line where the window_end_vpos is. */
17514 w->cursor.vpos = -1;
17515 last_text_row = NULL;
17516 overlay_arrow_seen = 0;
17517 while (it.current_y < it.last_visible_y
17518 && !fonts_changed_p
17519 && (first_unchanged_at_end_row == NULL
17520 || IT_CHARPOS (it) < stop_pos))
17522 if (display_line (&it))
17523 last_text_row = it.glyph_row - 1;
17526 if (fonts_changed_p)
17527 return -1;
17530 /* Compute differences in buffer positions, y-positions etc. for
17531 lines reused at the bottom of the window. Compute what we can
17532 scroll. */
17533 if (first_unchanged_at_end_row
17534 /* No lines reused because we displayed everything up to the
17535 bottom of the window. */
17536 && it.current_y < it.last_visible_y)
17538 dvpos = (it.vpos
17539 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17540 current_matrix));
17541 dy = it.current_y - first_unchanged_at_end_row->y;
17542 run.current_y = first_unchanged_at_end_row->y;
17543 run.desired_y = run.current_y + dy;
17544 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17546 else
17548 delta = delta_bytes = dvpos = dy
17549 = run.current_y = run.desired_y = run.height = 0;
17550 first_unchanged_at_end_row = NULL;
17552 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17555 /* Find the cursor if not already found. We have to decide whether
17556 PT will appear on this window (it sometimes doesn't, but this is
17557 not a very frequent case.) This decision has to be made before
17558 the current matrix is altered. A value of cursor.vpos < 0 means
17559 that PT is either in one of the lines beginning at
17560 first_unchanged_at_end_row or below the window. Don't care for
17561 lines that might be displayed later at the window end; as
17562 mentioned, this is not a frequent case. */
17563 if (w->cursor.vpos < 0)
17565 /* Cursor in unchanged rows at the top? */
17566 if (PT < CHARPOS (start_pos)
17567 && last_unchanged_at_beg_row)
17569 row = row_containing_pos (w, PT,
17570 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17571 last_unchanged_at_beg_row + 1, 0);
17572 if (row)
17573 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17576 /* Start from first_unchanged_at_end_row looking for PT. */
17577 else if (first_unchanged_at_end_row)
17579 row = row_containing_pos (w, PT - delta,
17580 first_unchanged_at_end_row, NULL, 0);
17581 if (row)
17582 set_cursor_from_row (w, row, w->current_matrix, delta,
17583 delta_bytes, dy, dvpos);
17586 /* Give up if cursor was not found. */
17587 if (w->cursor.vpos < 0)
17589 clear_glyph_matrix (w->desired_matrix);
17590 return -1;
17594 /* Don't let the cursor end in the scroll margins. */
17596 int this_scroll_margin, cursor_height;
17597 int frame_line_height = default_line_pixel_height (w);
17598 int window_total_lines
17599 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17601 this_scroll_margin =
17602 max (0, min (scroll_margin, window_total_lines / 4));
17603 this_scroll_margin *= frame_line_height;
17604 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17606 if ((w->cursor.y < this_scroll_margin
17607 && CHARPOS (start) > BEGV)
17608 /* Old redisplay didn't take scroll margin into account at the bottom,
17609 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17610 || (w->cursor.y + (make_cursor_line_fully_visible_p
17611 ? cursor_height + this_scroll_margin
17612 : 1)) > it.last_visible_y)
17614 w->cursor.vpos = -1;
17615 clear_glyph_matrix (w->desired_matrix);
17616 return -1;
17620 /* Scroll the display. Do it before changing the current matrix so
17621 that xterm.c doesn't get confused about where the cursor glyph is
17622 found. */
17623 if (dy && run.height)
17625 update_begin (f);
17627 if (FRAME_WINDOW_P (f))
17629 FRAME_RIF (f)->update_window_begin_hook (w);
17630 FRAME_RIF (f)->clear_window_mouse_face (w);
17631 FRAME_RIF (f)->scroll_run_hook (w, &run);
17632 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17634 else
17636 /* Terminal frame. In this case, dvpos gives the number of
17637 lines to scroll by; dvpos < 0 means scroll up. */
17638 int from_vpos
17639 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17640 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17641 int end = (WINDOW_TOP_EDGE_LINE (w)
17642 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17643 + window_internal_height (w));
17645 #if defined (HAVE_GPM) || defined (MSDOS)
17646 x_clear_window_mouse_face (w);
17647 #endif
17648 /* Perform the operation on the screen. */
17649 if (dvpos > 0)
17651 /* Scroll last_unchanged_at_beg_row to the end of the
17652 window down dvpos lines. */
17653 set_terminal_window (f, end);
17655 /* On dumb terminals delete dvpos lines at the end
17656 before inserting dvpos empty lines. */
17657 if (!FRAME_SCROLL_REGION_OK (f))
17658 ins_del_lines (f, end - dvpos, -dvpos);
17660 /* Insert dvpos empty lines in front of
17661 last_unchanged_at_beg_row. */
17662 ins_del_lines (f, from, dvpos);
17664 else if (dvpos < 0)
17666 /* Scroll up last_unchanged_at_beg_vpos to the end of
17667 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17668 set_terminal_window (f, end);
17670 /* Delete dvpos lines in front of
17671 last_unchanged_at_beg_vpos. ins_del_lines will set
17672 the cursor to the given vpos and emit |dvpos| delete
17673 line sequences. */
17674 ins_del_lines (f, from + dvpos, dvpos);
17676 /* On a dumb terminal insert dvpos empty lines at the
17677 end. */
17678 if (!FRAME_SCROLL_REGION_OK (f))
17679 ins_del_lines (f, end + dvpos, -dvpos);
17682 set_terminal_window (f, 0);
17685 update_end (f);
17688 /* Shift reused rows of the current matrix to the right position.
17689 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17690 text. */
17691 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17692 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17693 if (dvpos < 0)
17695 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17696 bottom_vpos, dvpos);
17697 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17698 bottom_vpos);
17700 else if (dvpos > 0)
17702 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17703 bottom_vpos, dvpos);
17704 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17705 first_unchanged_at_end_vpos + dvpos);
17708 /* For frame-based redisplay, make sure that current frame and window
17709 matrix are in sync with respect to glyph memory. */
17710 if (!FRAME_WINDOW_P (f))
17711 sync_frame_with_window_matrix_rows (w);
17713 /* Adjust buffer positions in reused rows. */
17714 if (delta || delta_bytes)
17715 increment_matrix_positions (current_matrix,
17716 first_unchanged_at_end_vpos + dvpos,
17717 bottom_vpos, delta, delta_bytes);
17719 /* Adjust Y positions. */
17720 if (dy)
17721 shift_glyph_matrix (w, current_matrix,
17722 first_unchanged_at_end_vpos + dvpos,
17723 bottom_vpos, dy);
17725 if (first_unchanged_at_end_row)
17727 first_unchanged_at_end_row += dvpos;
17728 if (first_unchanged_at_end_row->y >= it.last_visible_y
17729 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17730 first_unchanged_at_end_row = NULL;
17733 /* If scrolling up, there may be some lines to display at the end of
17734 the window. */
17735 last_text_row_at_end = NULL;
17736 if (dy < 0)
17738 /* Scrolling up can leave for example a partially visible line
17739 at the end of the window to be redisplayed. */
17740 /* Set last_row to the glyph row in the current matrix where the
17741 window end line is found. It has been moved up or down in
17742 the matrix by dvpos. */
17743 int last_vpos = w->window_end_vpos + dvpos;
17744 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17746 /* If last_row is the window end line, it should display text. */
17747 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17749 /* If window end line was partially visible before, begin
17750 displaying at that line. Otherwise begin displaying with the
17751 line following it. */
17752 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17754 init_to_row_start (&it, w, last_row);
17755 it.vpos = last_vpos;
17756 it.current_y = last_row->y;
17758 else
17760 init_to_row_end (&it, w, last_row);
17761 it.vpos = 1 + last_vpos;
17762 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17763 ++last_row;
17766 /* We may start in a continuation line. If so, we have to
17767 get the right continuation_lines_width and current_x. */
17768 it.continuation_lines_width = last_row->continuation_lines_width;
17769 it.hpos = it.current_x = 0;
17771 /* Display the rest of the lines at the window end. */
17772 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17773 while (it.current_y < it.last_visible_y
17774 && !fonts_changed_p)
17776 /* Is it always sure that the display agrees with lines in
17777 the current matrix? I don't think so, so we mark rows
17778 displayed invalid in the current matrix by setting their
17779 enabled_p flag to zero. */
17780 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17781 if (display_line (&it))
17782 last_text_row_at_end = it.glyph_row - 1;
17786 /* Update window_end_pos and window_end_vpos. */
17787 if (first_unchanged_at_end_row && !last_text_row_at_end)
17789 /* Window end line if one of the preserved rows from the current
17790 matrix. Set row to the last row displaying text in current
17791 matrix starting at first_unchanged_at_end_row, after
17792 scrolling. */
17793 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17794 row = find_last_row_displaying_text (w->current_matrix, &it,
17795 first_unchanged_at_end_row);
17796 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17797 adjust_window_ends (w, row, 1);
17798 eassert (w->window_end_bytepos >= 0);
17799 IF_DEBUG (debug_method_add (w, "A"));
17801 else if (last_text_row_at_end)
17803 adjust_window_ends (w, last_text_row_at_end, 0);
17804 eassert (w->window_end_bytepos >= 0);
17805 IF_DEBUG (debug_method_add (w, "B"));
17807 else if (last_text_row)
17809 /* We have displayed either to the end of the window or at the
17810 end of the window, i.e. the last row with text is to be found
17811 in the desired matrix. */
17812 adjust_window_ends (w, last_text_row, 0);
17813 eassert (w->window_end_bytepos >= 0);
17815 else if (first_unchanged_at_end_row == NULL
17816 && last_text_row == NULL
17817 && last_text_row_at_end == NULL)
17819 /* Displayed to end of window, but no line containing text was
17820 displayed. Lines were deleted at the end of the window. */
17821 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17822 int vpos = w->window_end_vpos;
17823 struct glyph_row *current_row = current_matrix->rows + vpos;
17824 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17826 for (row = NULL;
17827 row == NULL && vpos >= first_vpos;
17828 --vpos, --current_row, --desired_row)
17830 if (desired_row->enabled_p)
17832 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17833 row = desired_row;
17835 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17836 row = current_row;
17839 eassert (row != NULL);
17840 w->window_end_vpos = vpos + 1;
17841 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17842 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17843 eassert (w->window_end_bytepos >= 0);
17844 IF_DEBUG (debug_method_add (w, "C"));
17846 else
17847 emacs_abort ();
17849 IF_DEBUG (debug_end_pos = w->window_end_pos;
17850 debug_end_vpos = w->window_end_vpos);
17852 /* Record that display has not been completed. */
17853 w->window_end_valid = 0;
17854 w->desired_matrix->no_scrolling_p = 1;
17855 return 3;
17857 #undef GIVE_UP
17862 /***********************************************************************
17863 More debugging support
17864 ***********************************************************************/
17866 #ifdef GLYPH_DEBUG
17868 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17869 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17870 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17873 /* Dump the contents of glyph matrix MATRIX on stderr.
17875 GLYPHS 0 means don't show glyph contents.
17876 GLYPHS 1 means show glyphs in short form
17877 GLYPHS > 1 means show glyphs in long form. */
17879 void
17880 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17882 int i;
17883 for (i = 0; i < matrix->nrows; ++i)
17884 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17888 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17889 the glyph row and area where the glyph comes from. */
17891 void
17892 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17894 if (glyph->type == CHAR_GLYPH
17895 || glyph->type == GLYPHLESS_GLYPH)
17897 fprintf (stderr,
17898 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17899 glyph - row->glyphs[TEXT_AREA],
17900 (glyph->type == CHAR_GLYPH
17901 ? 'C'
17902 : 'G'),
17903 glyph->charpos,
17904 (BUFFERP (glyph->object)
17905 ? 'B'
17906 : (STRINGP (glyph->object)
17907 ? 'S'
17908 : (INTEGERP (glyph->object)
17909 ? '0'
17910 : '-'))),
17911 glyph->pixel_width,
17912 glyph->u.ch,
17913 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17914 ? glyph->u.ch
17915 : '.'),
17916 glyph->face_id,
17917 glyph->left_box_line_p,
17918 glyph->right_box_line_p);
17920 else if (glyph->type == STRETCH_GLYPH)
17922 fprintf (stderr,
17923 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17924 glyph - row->glyphs[TEXT_AREA],
17925 'S',
17926 glyph->charpos,
17927 (BUFFERP (glyph->object)
17928 ? 'B'
17929 : (STRINGP (glyph->object)
17930 ? 'S'
17931 : (INTEGERP (glyph->object)
17932 ? '0'
17933 : '-'))),
17934 glyph->pixel_width,
17936 ' ',
17937 glyph->face_id,
17938 glyph->left_box_line_p,
17939 glyph->right_box_line_p);
17941 else if (glyph->type == IMAGE_GLYPH)
17943 fprintf (stderr,
17944 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17945 glyph - row->glyphs[TEXT_AREA],
17946 'I',
17947 glyph->charpos,
17948 (BUFFERP (glyph->object)
17949 ? 'B'
17950 : (STRINGP (glyph->object)
17951 ? 'S'
17952 : (INTEGERP (glyph->object)
17953 ? '0'
17954 : '-'))),
17955 glyph->pixel_width,
17956 glyph->u.img_id,
17957 '.',
17958 glyph->face_id,
17959 glyph->left_box_line_p,
17960 glyph->right_box_line_p);
17962 else if (glyph->type == COMPOSITE_GLYPH)
17964 fprintf (stderr,
17965 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17966 glyph - row->glyphs[TEXT_AREA],
17967 '+',
17968 glyph->charpos,
17969 (BUFFERP (glyph->object)
17970 ? 'B'
17971 : (STRINGP (glyph->object)
17972 ? 'S'
17973 : (INTEGERP (glyph->object)
17974 ? '0'
17975 : '-'))),
17976 glyph->pixel_width,
17977 glyph->u.cmp.id);
17978 if (glyph->u.cmp.automatic)
17979 fprintf (stderr,
17980 "[%d-%d]",
17981 glyph->slice.cmp.from, glyph->slice.cmp.to);
17982 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17983 glyph->face_id,
17984 glyph->left_box_line_p,
17985 glyph->right_box_line_p);
17990 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17991 GLYPHS 0 means don't show glyph contents.
17992 GLYPHS 1 means show glyphs in short form
17993 GLYPHS > 1 means show glyphs in long form. */
17995 void
17996 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17998 if (glyphs != 1)
18000 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18001 fprintf (stderr, "==============================================================================\n");
18003 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18004 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18005 vpos,
18006 MATRIX_ROW_START_CHARPOS (row),
18007 MATRIX_ROW_END_CHARPOS (row),
18008 row->used[TEXT_AREA],
18009 row->contains_overlapping_glyphs_p,
18010 row->enabled_p,
18011 row->truncated_on_left_p,
18012 row->truncated_on_right_p,
18013 row->continued_p,
18014 MATRIX_ROW_CONTINUATION_LINE_P (row),
18015 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18016 row->ends_at_zv_p,
18017 row->fill_line_p,
18018 row->ends_in_middle_of_char_p,
18019 row->starts_in_middle_of_char_p,
18020 row->mouse_face_p,
18021 row->x,
18022 row->y,
18023 row->pixel_width,
18024 row->height,
18025 row->visible_height,
18026 row->ascent,
18027 row->phys_ascent);
18028 /* The next 3 lines should align to "Start" in the header. */
18029 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18030 row->end.overlay_string_index,
18031 row->continuation_lines_width);
18032 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18033 CHARPOS (row->start.string_pos),
18034 CHARPOS (row->end.string_pos));
18035 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18036 row->end.dpvec_index);
18039 if (glyphs > 1)
18041 int area;
18043 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18045 struct glyph *glyph = row->glyphs[area];
18046 struct glyph *glyph_end = glyph + row->used[area];
18048 /* Glyph for a line end in text. */
18049 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18050 ++glyph_end;
18052 if (glyph < glyph_end)
18053 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18055 for (; glyph < glyph_end; ++glyph)
18056 dump_glyph (row, glyph, area);
18059 else if (glyphs == 1)
18061 int area;
18063 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18065 char *s = alloca (row->used[area] + 4);
18066 int i;
18068 for (i = 0; i < row->used[area]; ++i)
18070 struct glyph *glyph = row->glyphs[area] + i;
18071 if (i == row->used[area] - 1
18072 && area == TEXT_AREA
18073 && INTEGERP (glyph->object)
18074 && glyph->type == CHAR_GLYPH
18075 && glyph->u.ch == ' ')
18077 strcpy (&s[i], "[\\n]");
18078 i += 4;
18080 else if (glyph->type == CHAR_GLYPH
18081 && glyph->u.ch < 0x80
18082 && glyph->u.ch >= ' ')
18083 s[i] = glyph->u.ch;
18084 else
18085 s[i] = '.';
18088 s[i] = '\0';
18089 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18095 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18096 Sdump_glyph_matrix, 0, 1, "p",
18097 doc: /* Dump the current matrix of the selected window to stderr.
18098 Shows contents of glyph row structures. With non-nil
18099 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18100 glyphs in short form, otherwise show glyphs in long form. */)
18101 (Lisp_Object glyphs)
18103 struct window *w = XWINDOW (selected_window);
18104 struct buffer *buffer = XBUFFER (w->contents);
18106 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18107 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18108 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18109 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18110 fprintf (stderr, "=============================================\n");
18111 dump_glyph_matrix (w->current_matrix,
18112 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18113 return Qnil;
18117 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18118 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18119 (void)
18121 struct frame *f = XFRAME (selected_frame);
18122 dump_glyph_matrix (f->current_matrix, 1);
18123 return Qnil;
18127 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18128 doc: /* Dump glyph row ROW to stderr.
18129 GLYPH 0 means don't dump glyphs.
18130 GLYPH 1 means dump glyphs in short form.
18131 GLYPH > 1 or omitted means dump glyphs in long form. */)
18132 (Lisp_Object row, Lisp_Object glyphs)
18134 struct glyph_matrix *matrix;
18135 EMACS_INT vpos;
18137 CHECK_NUMBER (row);
18138 matrix = XWINDOW (selected_window)->current_matrix;
18139 vpos = XINT (row);
18140 if (vpos >= 0 && vpos < matrix->nrows)
18141 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18142 vpos,
18143 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18144 return Qnil;
18148 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18149 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18150 GLYPH 0 means don't dump glyphs.
18151 GLYPH 1 means dump glyphs in short form.
18152 GLYPH > 1 or omitted means dump glyphs in long form. */)
18153 (Lisp_Object row, Lisp_Object glyphs)
18155 struct frame *sf = SELECTED_FRAME ();
18156 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18157 EMACS_INT vpos;
18159 CHECK_NUMBER (row);
18160 vpos = XINT (row);
18161 if (vpos >= 0 && vpos < m->nrows)
18162 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18163 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18164 return Qnil;
18168 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18169 doc: /* Toggle tracing of redisplay.
18170 With ARG, turn tracing on if and only if ARG is positive. */)
18171 (Lisp_Object arg)
18173 if (NILP (arg))
18174 trace_redisplay_p = !trace_redisplay_p;
18175 else
18177 arg = Fprefix_numeric_value (arg);
18178 trace_redisplay_p = XINT (arg) > 0;
18181 return Qnil;
18185 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18186 doc: /* Like `format', but print result to stderr.
18187 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18188 (ptrdiff_t nargs, Lisp_Object *args)
18190 Lisp_Object s = Fformat (nargs, args);
18191 fprintf (stderr, "%s", SDATA (s));
18192 return Qnil;
18195 #endif /* GLYPH_DEBUG */
18199 /***********************************************************************
18200 Building Desired Matrix Rows
18201 ***********************************************************************/
18203 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18204 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18206 static struct glyph_row *
18207 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18209 struct frame *f = XFRAME (WINDOW_FRAME (w));
18210 struct buffer *buffer = XBUFFER (w->contents);
18211 struct buffer *old = current_buffer;
18212 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18213 int arrow_len = SCHARS (overlay_arrow_string);
18214 const unsigned char *arrow_end = arrow_string + arrow_len;
18215 const unsigned char *p;
18216 struct it it;
18217 bool multibyte_p;
18218 int n_glyphs_before;
18220 set_buffer_temp (buffer);
18221 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18222 it.glyph_row->used[TEXT_AREA] = 0;
18223 SET_TEXT_POS (it.position, 0, 0);
18225 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18226 p = arrow_string;
18227 while (p < arrow_end)
18229 Lisp_Object face, ilisp;
18231 /* Get the next character. */
18232 if (multibyte_p)
18233 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18234 else
18236 it.c = it.char_to_display = *p, it.len = 1;
18237 if (! ASCII_CHAR_P (it.c))
18238 it.char_to_display = BYTE8_TO_CHAR (it.c);
18240 p += it.len;
18242 /* Get its face. */
18243 ilisp = make_number (p - arrow_string);
18244 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18245 it.face_id = compute_char_face (f, it.char_to_display, face);
18247 /* Compute its width, get its glyphs. */
18248 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18249 SET_TEXT_POS (it.position, -1, -1);
18250 PRODUCE_GLYPHS (&it);
18252 /* If this character doesn't fit any more in the line, we have
18253 to remove some glyphs. */
18254 if (it.current_x > it.last_visible_x)
18256 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18257 break;
18261 set_buffer_temp (old);
18262 return it.glyph_row;
18266 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18267 glyphs to insert is determined by produce_special_glyphs. */
18269 static void
18270 insert_left_trunc_glyphs (struct it *it)
18272 struct it truncate_it;
18273 struct glyph *from, *end, *to, *toend;
18275 eassert (!FRAME_WINDOW_P (it->f)
18276 || (!it->glyph_row->reversed_p
18277 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18278 || (it->glyph_row->reversed_p
18279 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18281 /* Get the truncation glyphs. */
18282 truncate_it = *it;
18283 truncate_it.current_x = 0;
18284 truncate_it.face_id = DEFAULT_FACE_ID;
18285 truncate_it.glyph_row = &scratch_glyph_row;
18286 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18287 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18288 truncate_it.object = make_number (0);
18289 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18291 /* Overwrite glyphs from IT with truncation glyphs. */
18292 if (!it->glyph_row->reversed_p)
18294 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18296 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18297 end = from + tused;
18298 to = it->glyph_row->glyphs[TEXT_AREA];
18299 toend = to + it->glyph_row->used[TEXT_AREA];
18300 if (FRAME_WINDOW_P (it->f))
18302 /* On GUI frames, when variable-size fonts are displayed,
18303 the truncation glyphs may need more pixels than the row's
18304 glyphs they overwrite. We overwrite more glyphs to free
18305 enough screen real estate, and enlarge the stretch glyph
18306 on the right (see display_line), if there is one, to
18307 preserve the screen position of the truncation glyphs on
18308 the right. */
18309 int w = 0;
18310 struct glyph *g = to;
18311 short used;
18313 /* The first glyph could be partially visible, in which case
18314 it->glyph_row->x will be negative. But we want the left
18315 truncation glyphs to be aligned at the left margin of the
18316 window, so we override the x coordinate at which the row
18317 will begin. */
18318 it->glyph_row->x = 0;
18319 while (g < toend && w < it->truncation_pixel_width)
18321 w += g->pixel_width;
18322 ++g;
18324 if (g - to - tused > 0)
18326 memmove (to + tused, g, (toend - g) * sizeof(*g));
18327 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18329 used = it->glyph_row->used[TEXT_AREA];
18330 if (it->glyph_row->truncated_on_right_p
18331 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18332 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18333 == STRETCH_GLYPH)
18335 int extra = w - it->truncation_pixel_width;
18337 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18341 while (from < end)
18342 *to++ = *from++;
18344 /* There may be padding glyphs left over. Overwrite them too. */
18345 if (!FRAME_WINDOW_P (it->f))
18347 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18349 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18350 while (from < end)
18351 *to++ = *from++;
18355 if (to > toend)
18356 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18358 else
18360 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18362 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18363 that back to front. */
18364 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18365 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18366 toend = it->glyph_row->glyphs[TEXT_AREA];
18367 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18368 if (FRAME_WINDOW_P (it->f))
18370 int w = 0;
18371 struct glyph *g = to;
18373 while (g >= toend && w < it->truncation_pixel_width)
18375 w += g->pixel_width;
18376 --g;
18378 if (to - g - tused > 0)
18379 to = g + tused;
18380 if (it->glyph_row->truncated_on_right_p
18381 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18382 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18384 int extra = w - it->truncation_pixel_width;
18386 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18390 while (from >= end && to >= toend)
18391 *to-- = *from--;
18392 if (!FRAME_WINDOW_P (it->f))
18394 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18396 from =
18397 truncate_it.glyph_row->glyphs[TEXT_AREA]
18398 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18399 while (from >= end && to >= toend)
18400 *to-- = *from--;
18403 if (from >= end)
18405 /* Need to free some room before prepending additional
18406 glyphs. */
18407 int move_by = from - end + 1;
18408 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18409 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18411 for ( ; g >= g0; g--)
18412 g[move_by] = *g;
18413 while (from >= end)
18414 *to-- = *from--;
18415 it->glyph_row->used[TEXT_AREA] += move_by;
18420 /* Compute the hash code for ROW. */
18421 unsigned
18422 row_hash (struct glyph_row *row)
18424 int area, k;
18425 unsigned hashval = 0;
18427 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18428 for (k = 0; k < row->used[area]; ++k)
18429 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18430 + row->glyphs[area][k].u.val
18431 + row->glyphs[area][k].face_id
18432 + row->glyphs[area][k].padding_p
18433 + (row->glyphs[area][k].type << 2));
18435 return hashval;
18438 /* Compute the pixel height and width of IT->glyph_row.
18440 Most of the time, ascent and height of a display line will be equal
18441 to the max_ascent and max_height values of the display iterator
18442 structure. This is not the case if
18444 1. We hit ZV without displaying anything. In this case, max_ascent
18445 and max_height will be zero.
18447 2. We have some glyphs that don't contribute to the line height.
18448 (The glyph row flag contributes_to_line_height_p is for future
18449 pixmap extensions).
18451 The first case is easily covered by using default values because in
18452 these cases, the line height does not really matter, except that it
18453 must not be zero. */
18455 static void
18456 compute_line_metrics (struct it *it)
18458 struct glyph_row *row = it->glyph_row;
18460 if (FRAME_WINDOW_P (it->f))
18462 int i, min_y, max_y;
18464 /* The line may consist of one space only, that was added to
18465 place the cursor on it. If so, the row's height hasn't been
18466 computed yet. */
18467 if (row->height == 0)
18469 if (it->max_ascent + it->max_descent == 0)
18470 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18471 row->ascent = it->max_ascent;
18472 row->height = it->max_ascent + it->max_descent;
18473 row->phys_ascent = it->max_phys_ascent;
18474 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18475 row->extra_line_spacing = it->max_extra_line_spacing;
18478 /* Compute the width of this line. */
18479 row->pixel_width = row->x;
18480 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18481 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18483 eassert (row->pixel_width >= 0);
18484 eassert (row->ascent >= 0 && row->height > 0);
18486 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18487 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18489 /* If first line's physical ascent is larger than its logical
18490 ascent, use the physical ascent, and make the row taller.
18491 This makes accented characters fully visible. */
18492 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18493 && row->phys_ascent > row->ascent)
18495 row->height += row->phys_ascent - row->ascent;
18496 row->ascent = row->phys_ascent;
18499 /* Compute how much of the line is visible. */
18500 row->visible_height = row->height;
18502 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18503 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18505 if (row->y < min_y)
18506 row->visible_height -= min_y - row->y;
18507 if (row->y + row->height > max_y)
18508 row->visible_height -= row->y + row->height - max_y;
18510 else
18512 row->pixel_width = row->used[TEXT_AREA];
18513 if (row->continued_p)
18514 row->pixel_width -= it->continuation_pixel_width;
18515 else if (row->truncated_on_right_p)
18516 row->pixel_width -= it->truncation_pixel_width;
18517 row->ascent = row->phys_ascent = 0;
18518 row->height = row->phys_height = row->visible_height = 1;
18519 row->extra_line_spacing = 0;
18522 /* Compute a hash code for this row. */
18523 row->hash = row_hash (row);
18525 it->max_ascent = it->max_descent = 0;
18526 it->max_phys_ascent = it->max_phys_descent = 0;
18530 /* Append one space to the glyph row of iterator IT if doing a
18531 window-based redisplay. The space has the same face as
18532 IT->face_id. Value is non-zero if a space was added.
18534 This function is called to make sure that there is always one glyph
18535 at the end of a glyph row that the cursor can be set on under
18536 window-systems. (If there weren't such a glyph we would not know
18537 how wide and tall a box cursor should be displayed).
18539 At the same time this space let's a nicely handle clearing to the
18540 end of the line if the row ends in italic text. */
18542 static int
18543 append_space_for_newline (struct it *it, int default_face_p)
18545 if (FRAME_WINDOW_P (it->f))
18547 int n = it->glyph_row->used[TEXT_AREA];
18549 if (it->glyph_row->glyphs[TEXT_AREA] + n
18550 < it->glyph_row->glyphs[1 + TEXT_AREA])
18552 /* Save some values that must not be changed.
18553 Must save IT->c and IT->len because otherwise
18554 ITERATOR_AT_END_P wouldn't work anymore after
18555 append_space_for_newline has been called. */
18556 enum display_element_type saved_what = it->what;
18557 int saved_c = it->c, saved_len = it->len;
18558 int saved_char_to_display = it->char_to_display;
18559 int saved_x = it->current_x;
18560 int saved_face_id = it->face_id;
18561 int saved_box_end = it->end_of_box_run_p;
18562 struct text_pos saved_pos;
18563 Lisp_Object saved_object;
18564 struct face *face;
18566 saved_object = it->object;
18567 saved_pos = it->position;
18569 it->what = IT_CHARACTER;
18570 memset (&it->position, 0, sizeof it->position);
18571 it->object = make_number (0);
18572 it->c = it->char_to_display = ' ';
18573 it->len = 1;
18575 /* If the default face was remapped, be sure to use the
18576 remapped face for the appended newline. */
18577 if (default_face_p)
18578 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18579 else if (it->face_before_selective_p)
18580 it->face_id = it->saved_face_id;
18581 face = FACE_FROM_ID (it->f, it->face_id);
18582 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18583 /* In R2L rows, we will prepend a stretch glyph that will
18584 have the end_of_box_run_p flag set for it, so there's no
18585 need for the appended newline glyph to have that flag
18586 set. */
18587 if (it->glyph_row->reversed_p
18588 /* But if the appended newline glyph goes all the way to
18589 the end of the row, there will be no stretch glyph,
18590 so leave the box flag set. */
18591 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18592 it->end_of_box_run_p = 0;
18594 PRODUCE_GLYPHS (it);
18596 it->override_ascent = -1;
18597 it->constrain_row_ascent_descent_p = 0;
18598 it->current_x = saved_x;
18599 it->object = saved_object;
18600 it->position = saved_pos;
18601 it->what = saved_what;
18602 it->face_id = saved_face_id;
18603 it->len = saved_len;
18604 it->c = saved_c;
18605 it->char_to_display = saved_char_to_display;
18606 it->end_of_box_run_p = saved_box_end;
18607 return 1;
18611 return 0;
18615 /* Extend the face of the last glyph in the text area of IT->glyph_row
18616 to the end of the display line. Called from display_line. If the
18617 glyph row is empty, add a space glyph to it so that we know the
18618 face to draw. Set the glyph row flag fill_line_p. If the glyph
18619 row is R2L, prepend a stretch glyph to cover the empty space to the
18620 left of the leftmost glyph. */
18622 static void
18623 extend_face_to_end_of_line (struct it *it)
18625 struct face *face, *default_face;
18626 struct frame *f = it->f;
18628 /* If line is already filled, do nothing. Non window-system frames
18629 get a grace of one more ``pixel'' because their characters are
18630 1-``pixel'' wide, so they hit the equality too early. This grace
18631 is needed only for R2L rows that are not continued, to produce
18632 one extra blank where we could display the cursor. */
18633 if (it->current_x >= it->last_visible_x
18634 + (!FRAME_WINDOW_P (f)
18635 && it->glyph_row->reversed_p
18636 && !it->glyph_row->continued_p))
18637 return;
18639 /* The default face, possibly remapped. */
18640 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18642 /* Face extension extends the background and box of IT->face_id
18643 to the end of the line. If the background equals the background
18644 of the frame, we don't have to do anything. */
18645 if (it->face_before_selective_p)
18646 face = FACE_FROM_ID (f, it->saved_face_id);
18647 else
18648 face = FACE_FROM_ID (f, it->face_id);
18650 if (FRAME_WINDOW_P (f)
18651 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18652 && face->box == FACE_NO_BOX
18653 && face->background == FRAME_BACKGROUND_PIXEL (f)
18654 && !face->stipple
18655 && !it->glyph_row->reversed_p)
18656 return;
18658 /* Set the glyph row flag indicating that the face of the last glyph
18659 in the text area has to be drawn to the end of the text area. */
18660 it->glyph_row->fill_line_p = 1;
18662 /* If current character of IT is not ASCII, make sure we have the
18663 ASCII face. This will be automatically undone the next time
18664 get_next_display_element returns a multibyte character. Note
18665 that the character will always be single byte in unibyte
18666 text. */
18667 if (!ASCII_CHAR_P (it->c))
18669 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18672 if (FRAME_WINDOW_P (f))
18674 /* If the row is empty, add a space with the current face of IT,
18675 so that we know which face to draw. */
18676 if (it->glyph_row->used[TEXT_AREA] == 0)
18678 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18679 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18680 it->glyph_row->used[TEXT_AREA] = 1;
18682 #ifdef HAVE_WINDOW_SYSTEM
18683 if (it->glyph_row->reversed_p)
18685 /* Prepend a stretch glyph to the row, such that the
18686 rightmost glyph will be drawn flushed all the way to the
18687 right margin of the window. The stretch glyph that will
18688 occupy the empty space, if any, to the left of the
18689 glyphs. */
18690 struct font *font = face->font ? face->font : FRAME_FONT (f);
18691 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18692 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18693 struct glyph *g;
18694 int row_width, stretch_ascent, stretch_width;
18695 struct text_pos saved_pos;
18696 int saved_face_id, saved_avoid_cursor, saved_box_start;
18698 for (row_width = 0, g = row_start; g < row_end; g++)
18699 row_width += g->pixel_width;
18700 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18701 if (stretch_width > 0)
18703 stretch_ascent =
18704 (((it->ascent + it->descent)
18705 * FONT_BASE (font)) / FONT_HEIGHT (font));
18706 saved_pos = it->position;
18707 memset (&it->position, 0, sizeof it->position);
18708 saved_avoid_cursor = it->avoid_cursor_p;
18709 it->avoid_cursor_p = 1;
18710 saved_face_id = it->face_id;
18711 saved_box_start = it->start_of_box_run_p;
18712 /* The last row's stretch glyph should get the default
18713 face, to avoid painting the rest of the window with
18714 the region face, if the region ends at ZV. */
18715 if (it->glyph_row->ends_at_zv_p)
18716 it->face_id = default_face->id;
18717 else
18718 it->face_id = face->id;
18719 it->start_of_box_run_p = 0;
18720 append_stretch_glyph (it, make_number (0), stretch_width,
18721 it->ascent + it->descent, stretch_ascent);
18722 it->position = saved_pos;
18723 it->avoid_cursor_p = saved_avoid_cursor;
18724 it->face_id = saved_face_id;
18725 it->start_of_box_run_p = saved_box_start;
18728 #endif /* HAVE_WINDOW_SYSTEM */
18730 else
18732 /* Save some values that must not be changed. */
18733 int saved_x = it->current_x;
18734 struct text_pos saved_pos;
18735 Lisp_Object saved_object;
18736 enum display_element_type saved_what = it->what;
18737 int saved_face_id = it->face_id;
18739 saved_object = it->object;
18740 saved_pos = it->position;
18742 it->what = IT_CHARACTER;
18743 memset (&it->position, 0, sizeof it->position);
18744 it->object = make_number (0);
18745 it->c = it->char_to_display = ' ';
18746 it->len = 1;
18747 /* The last row's blank glyphs should get the default face, to
18748 avoid painting the rest of the window with the region face,
18749 if the region ends at ZV. */
18750 if (it->glyph_row->ends_at_zv_p)
18751 it->face_id = default_face->id;
18752 else
18753 it->face_id = face->id;
18755 PRODUCE_GLYPHS (it);
18757 while (it->current_x <= it->last_visible_x)
18758 PRODUCE_GLYPHS (it);
18760 /* Don't count these blanks really. It would let us insert a left
18761 truncation glyph below and make us set the cursor on them, maybe. */
18762 it->current_x = saved_x;
18763 it->object = saved_object;
18764 it->position = saved_pos;
18765 it->what = saved_what;
18766 it->face_id = saved_face_id;
18771 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18772 trailing whitespace. */
18774 static int
18775 trailing_whitespace_p (ptrdiff_t charpos)
18777 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18778 int c = 0;
18780 while (bytepos < ZV_BYTE
18781 && (c = FETCH_CHAR (bytepos),
18782 c == ' ' || c == '\t'))
18783 ++bytepos;
18785 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18787 if (bytepos != PT_BYTE)
18788 return 1;
18790 return 0;
18794 /* Highlight trailing whitespace, if any, in ROW. */
18796 static void
18797 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18799 int used = row->used[TEXT_AREA];
18801 if (used)
18803 struct glyph *start = row->glyphs[TEXT_AREA];
18804 struct glyph *glyph = start + used - 1;
18806 if (row->reversed_p)
18808 /* Right-to-left rows need to be processed in the opposite
18809 direction, so swap the edge pointers. */
18810 glyph = start;
18811 start = row->glyphs[TEXT_AREA] + used - 1;
18814 /* Skip over glyphs inserted to display the cursor at the
18815 end of a line, for extending the face of the last glyph
18816 to the end of the line on terminals, and for truncation
18817 and continuation glyphs. */
18818 if (!row->reversed_p)
18820 while (glyph >= start
18821 && glyph->type == CHAR_GLYPH
18822 && INTEGERP (glyph->object))
18823 --glyph;
18825 else
18827 while (glyph <= start
18828 && glyph->type == CHAR_GLYPH
18829 && INTEGERP (glyph->object))
18830 ++glyph;
18833 /* If last glyph is a space or stretch, and it's trailing
18834 whitespace, set the face of all trailing whitespace glyphs in
18835 IT->glyph_row to `trailing-whitespace'. */
18836 if ((row->reversed_p ? glyph <= start : glyph >= start)
18837 && BUFFERP (glyph->object)
18838 && (glyph->type == STRETCH_GLYPH
18839 || (glyph->type == CHAR_GLYPH
18840 && glyph->u.ch == ' '))
18841 && trailing_whitespace_p (glyph->charpos))
18843 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18844 if (face_id < 0)
18845 return;
18847 if (!row->reversed_p)
18849 while (glyph >= start
18850 && BUFFERP (glyph->object)
18851 && (glyph->type == STRETCH_GLYPH
18852 || (glyph->type == CHAR_GLYPH
18853 && glyph->u.ch == ' ')))
18854 (glyph--)->face_id = face_id;
18856 else
18858 while (glyph <= start
18859 && BUFFERP (glyph->object)
18860 && (glyph->type == STRETCH_GLYPH
18861 || (glyph->type == CHAR_GLYPH
18862 && glyph->u.ch == ' ')))
18863 (glyph++)->face_id = face_id;
18870 /* Value is non-zero if glyph row ROW should be
18871 considered to hold the buffer position CHARPOS. */
18873 static int
18874 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18876 int result = 1;
18878 if (charpos == CHARPOS (row->end.pos)
18879 || charpos == MATRIX_ROW_END_CHARPOS (row))
18881 /* Suppose the row ends on a string.
18882 Unless the row is continued, that means it ends on a newline
18883 in the string. If it's anything other than a display string
18884 (e.g., a before-string from an overlay), we don't want the
18885 cursor there. (This heuristic seems to give the optimal
18886 behavior for the various types of multi-line strings.)
18887 One exception: if the string has `cursor' property on one of
18888 its characters, we _do_ want the cursor there. */
18889 if (CHARPOS (row->end.string_pos) >= 0)
18891 if (row->continued_p)
18892 result = 1;
18893 else
18895 /* Check for `display' property. */
18896 struct glyph *beg = row->glyphs[TEXT_AREA];
18897 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18898 struct glyph *glyph;
18900 result = 0;
18901 for (glyph = end; glyph >= beg; --glyph)
18902 if (STRINGP (glyph->object))
18904 Lisp_Object prop
18905 = Fget_char_property (make_number (charpos),
18906 Qdisplay, Qnil);
18907 result =
18908 (!NILP (prop)
18909 && display_prop_string_p (prop, glyph->object));
18910 /* If there's a `cursor' property on one of the
18911 string's characters, this row is a cursor row,
18912 even though this is not a display string. */
18913 if (!result)
18915 Lisp_Object s = glyph->object;
18917 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18919 ptrdiff_t gpos = glyph->charpos;
18921 if (!NILP (Fget_char_property (make_number (gpos),
18922 Qcursor, s)))
18924 result = 1;
18925 break;
18929 break;
18933 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18935 /* If the row ends in middle of a real character,
18936 and the line is continued, we want the cursor here.
18937 That's because CHARPOS (ROW->end.pos) would equal
18938 PT if PT is before the character. */
18939 if (!row->ends_in_ellipsis_p)
18940 result = row->continued_p;
18941 else
18942 /* If the row ends in an ellipsis, then
18943 CHARPOS (ROW->end.pos) will equal point after the
18944 invisible text. We want that position to be displayed
18945 after the ellipsis. */
18946 result = 0;
18948 /* If the row ends at ZV, display the cursor at the end of that
18949 row instead of at the start of the row below. */
18950 else if (row->ends_at_zv_p)
18951 result = 1;
18952 else
18953 result = 0;
18956 return result;
18959 /* Value is non-zero if glyph row ROW should be
18960 used to hold the cursor. */
18962 static int
18963 cursor_row_p (struct glyph_row *row)
18965 return row_for_charpos_p (row, PT);
18970 /* Push the property PROP so that it will be rendered at the current
18971 position in IT. Return 1 if PROP was successfully pushed, 0
18972 otherwise. Called from handle_line_prefix to handle the
18973 `line-prefix' and `wrap-prefix' properties. */
18975 static int
18976 push_prefix_prop (struct it *it, Lisp_Object prop)
18978 struct text_pos pos =
18979 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18981 eassert (it->method == GET_FROM_BUFFER
18982 || it->method == GET_FROM_DISPLAY_VECTOR
18983 || it->method == GET_FROM_STRING);
18985 /* We need to save the current buffer/string position, so it will be
18986 restored by pop_it, because iterate_out_of_display_property
18987 depends on that being set correctly, but some situations leave
18988 it->position not yet set when this function is called. */
18989 push_it (it, &pos);
18991 if (STRINGP (prop))
18993 if (SCHARS (prop) == 0)
18995 pop_it (it);
18996 return 0;
18999 it->string = prop;
19000 it->string_from_prefix_prop_p = 1;
19001 it->multibyte_p = STRING_MULTIBYTE (it->string);
19002 it->current.overlay_string_index = -1;
19003 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19004 it->end_charpos = it->string_nchars = SCHARS (it->string);
19005 it->method = GET_FROM_STRING;
19006 it->stop_charpos = 0;
19007 it->prev_stop = 0;
19008 it->base_level_stop = 0;
19010 /* Force paragraph direction to be that of the parent
19011 buffer/string. */
19012 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19013 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19014 else
19015 it->paragraph_embedding = L2R;
19017 /* Set up the bidi iterator for this display string. */
19018 if (it->bidi_p)
19020 it->bidi_it.string.lstring = it->string;
19021 it->bidi_it.string.s = NULL;
19022 it->bidi_it.string.schars = it->end_charpos;
19023 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19024 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19025 it->bidi_it.string.unibyte = !it->multibyte_p;
19026 it->bidi_it.w = it->w;
19027 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19030 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19032 it->method = GET_FROM_STRETCH;
19033 it->object = prop;
19035 #ifdef HAVE_WINDOW_SYSTEM
19036 else if (IMAGEP (prop))
19038 it->what = IT_IMAGE;
19039 it->image_id = lookup_image (it->f, prop);
19040 it->method = GET_FROM_IMAGE;
19042 #endif /* HAVE_WINDOW_SYSTEM */
19043 else
19045 pop_it (it); /* bogus display property, give up */
19046 return 0;
19049 return 1;
19052 /* Return the character-property PROP at the current position in IT. */
19054 static Lisp_Object
19055 get_it_property (struct it *it, Lisp_Object prop)
19057 Lisp_Object position, object = it->object;
19059 if (STRINGP (object))
19060 position = make_number (IT_STRING_CHARPOS (*it));
19061 else if (BUFFERP (object))
19063 position = make_number (IT_CHARPOS (*it));
19064 object = it->window;
19066 else
19067 return Qnil;
19069 return Fget_char_property (position, prop, object);
19072 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19074 static void
19075 handle_line_prefix (struct it *it)
19077 Lisp_Object prefix;
19079 if (it->continuation_lines_width > 0)
19081 prefix = get_it_property (it, Qwrap_prefix);
19082 if (NILP (prefix))
19083 prefix = Vwrap_prefix;
19085 else
19087 prefix = get_it_property (it, Qline_prefix);
19088 if (NILP (prefix))
19089 prefix = Vline_prefix;
19091 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19093 /* If the prefix is wider than the window, and we try to wrap
19094 it, it would acquire its own wrap prefix, and so on till the
19095 iterator stack overflows. So, don't wrap the prefix. */
19096 it->line_wrap = TRUNCATE;
19097 it->avoid_cursor_p = 1;
19103 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19104 only for R2L lines from display_line and display_string, when they
19105 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19106 the line/string needs to be continued on the next glyph row. */
19107 static void
19108 unproduce_glyphs (struct it *it, int n)
19110 struct glyph *glyph, *end;
19112 eassert (it->glyph_row);
19113 eassert (it->glyph_row->reversed_p);
19114 eassert (it->area == TEXT_AREA);
19115 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19117 if (n > it->glyph_row->used[TEXT_AREA])
19118 n = it->glyph_row->used[TEXT_AREA];
19119 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19120 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19121 for ( ; glyph < end; glyph++)
19122 glyph[-n] = *glyph;
19125 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19126 and ROW->maxpos. */
19127 static void
19128 find_row_edges (struct it *it, struct glyph_row *row,
19129 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19130 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19132 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19133 lines' rows is implemented for bidi-reordered rows. */
19135 /* ROW->minpos is the value of min_pos, the minimal buffer position
19136 we have in ROW, or ROW->start.pos if that is smaller. */
19137 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19138 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19139 else
19140 /* We didn't find buffer positions smaller than ROW->start, or
19141 didn't find _any_ valid buffer positions in any of the glyphs,
19142 so we must trust the iterator's computed positions. */
19143 row->minpos = row->start.pos;
19144 if (max_pos <= 0)
19146 max_pos = CHARPOS (it->current.pos);
19147 max_bpos = BYTEPOS (it->current.pos);
19150 /* Here are the various use-cases for ending the row, and the
19151 corresponding values for ROW->maxpos:
19153 Line ends in a newline from buffer eol_pos + 1
19154 Line is continued from buffer max_pos + 1
19155 Line is truncated on right it->current.pos
19156 Line ends in a newline from string max_pos + 1(*)
19157 (*) + 1 only when line ends in a forward scan
19158 Line is continued from string max_pos
19159 Line is continued from display vector max_pos
19160 Line is entirely from a string min_pos == max_pos
19161 Line is entirely from a display vector min_pos == max_pos
19162 Line that ends at ZV ZV
19164 If you discover other use-cases, please add them here as
19165 appropriate. */
19166 if (row->ends_at_zv_p)
19167 row->maxpos = it->current.pos;
19168 else if (row->used[TEXT_AREA])
19170 int seen_this_string = 0;
19171 struct glyph_row *r1 = row - 1;
19173 /* Did we see the same display string on the previous row? */
19174 if (STRINGP (it->object)
19175 /* this is not the first row */
19176 && row > it->w->desired_matrix->rows
19177 /* previous row is not the header line */
19178 && !r1->mode_line_p
19179 /* previous row also ends in a newline from a string */
19180 && r1->ends_in_newline_from_string_p)
19182 struct glyph *start, *end;
19184 /* Search for the last glyph of the previous row that came
19185 from buffer or string. Depending on whether the row is
19186 L2R or R2L, we need to process it front to back or the
19187 other way round. */
19188 if (!r1->reversed_p)
19190 start = r1->glyphs[TEXT_AREA];
19191 end = start + r1->used[TEXT_AREA];
19192 /* Glyphs inserted by redisplay have an integer (zero)
19193 as their object. */
19194 while (end > start
19195 && INTEGERP ((end - 1)->object)
19196 && (end - 1)->charpos <= 0)
19197 --end;
19198 if (end > start)
19200 if (EQ ((end - 1)->object, it->object))
19201 seen_this_string = 1;
19203 else
19204 /* If all the glyphs of the previous row were inserted
19205 by redisplay, it means the previous row was
19206 produced from a single newline, which is only
19207 possible if that newline came from the same string
19208 as the one which produced this ROW. */
19209 seen_this_string = 1;
19211 else
19213 end = r1->glyphs[TEXT_AREA] - 1;
19214 start = end + r1->used[TEXT_AREA];
19215 while (end < start
19216 && INTEGERP ((end + 1)->object)
19217 && (end + 1)->charpos <= 0)
19218 ++end;
19219 if (end < start)
19221 if (EQ ((end + 1)->object, it->object))
19222 seen_this_string = 1;
19224 else
19225 seen_this_string = 1;
19228 /* Take note of each display string that covers a newline only
19229 once, the first time we see it. This is for when a display
19230 string includes more than one newline in it. */
19231 if (row->ends_in_newline_from_string_p && !seen_this_string)
19233 /* If we were scanning the buffer forward when we displayed
19234 the string, we want to account for at least one buffer
19235 position that belongs to this row (position covered by
19236 the display string), so that cursor positioning will
19237 consider this row as a candidate when point is at the end
19238 of the visual line represented by this row. This is not
19239 required when scanning back, because max_pos will already
19240 have a much larger value. */
19241 if (CHARPOS (row->end.pos) > max_pos)
19242 INC_BOTH (max_pos, max_bpos);
19243 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19245 else if (CHARPOS (it->eol_pos) > 0)
19246 SET_TEXT_POS (row->maxpos,
19247 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19248 else if (row->continued_p)
19250 /* If max_pos is different from IT's current position, it
19251 means IT->method does not belong to the display element
19252 at max_pos. However, it also means that the display
19253 element at max_pos was displayed in its entirety on this
19254 line, which is equivalent to saying that the next line
19255 starts at the next buffer position. */
19256 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19257 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19258 else
19260 INC_BOTH (max_pos, max_bpos);
19261 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19264 else if (row->truncated_on_right_p)
19265 /* display_line already called reseat_at_next_visible_line_start,
19266 which puts the iterator at the beginning of the next line, in
19267 the logical order. */
19268 row->maxpos = it->current.pos;
19269 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19270 /* A line that is entirely from a string/image/stretch... */
19271 row->maxpos = row->minpos;
19272 else
19273 emacs_abort ();
19275 else
19276 row->maxpos = it->current.pos;
19279 /* Construct the glyph row IT->glyph_row in the desired matrix of
19280 IT->w from text at the current position of IT. See dispextern.h
19281 for an overview of struct it. Value is non-zero if
19282 IT->glyph_row displays text, as opposed to a line displaying ZV
19283 only. */
19285 static int
19286 display_line (struct it *it)
19288 struct glyph_row *row = it->glyph_row;
19289 Lisp_Object overlay_arrow_string;
19290 struct it wrap_it;
19291 void *wrap_data = NULL;
19292 int may_wrap = 0, wrap_x IF_LINT (= 0);
19293 int wrap_row_used = -1;
19294 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19295 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19296 int wrap_row_extra_line_spacing IF_LINT (= 0);
19297 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19298 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19299 int cvpos;
19300 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19301 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19303 /* We always start displaying at hpos zero even if hscrolled. */
19304 eassert (it->hpos == 0 && it->current_x == 0);
19306 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19307 >= it->w->desired_matrix->nrows)
19309 it->w->nrows_scale_factor++;
19310 fonts_changed_p = 1;
19311 return 0;
19314 /* Is IT->w showing the region? */
19315 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19317 /* Clear the result glyph row and enable it. */
19318 prepare_desired_row (row);
19320 row->y = it->current_y;
19321 row->start = it->start;
19322 row->continuation_lines_width = it->continuation_lines_width;
19323 row->displays_text_p = 1;
19324 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19325 it->starts_in_middle_of_char_p = 0;
19327 /* Arrange the overlays nicely for our purposes. Usually, we call
19328 display_line on only one line at a time, in which case this
19329 can't really hurt too much, or we call it on lines which appear
19330 one after another in the buffer, in which case all calls to
19331 recenter_overlay_lists but the first will be pretty cheap. */
19332 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19334 /* Move over display elements that are not visible because we are
19335 hscrolled. This may stop at an x-position < IT->first_visible_x
19336 if the first glyph is partially visible or if we hit a line end. */
19337 if (it->current_x < it->first_visible_x)
19339 enum move_it_result move_result;
19341 this_line_min_pos = row->start.pos;
19342 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19343 MOVE_TO_POS | MOVE_TO_X);
19344 /* If we are under a large hscroll, move_it_in_display_line_to
19345 could hit the end of the line without reaching
19346 it->first_visible_x. Pretend that we did reach it. This is
19347 especially important on a TTY, where we will call
19348 extend_face_to_end_of_line, which needs to know how many
19349 blank glyphs to produce. */
19350 if (it->current_x < it->first_visible_x
19351 && (move_result == MOVE_NEWLINE_OR_CR
19352 || move_result == MOVE_POS_MATCH_OR_ZV))
19353 it->current_x = it->first_visible_x;
19355 /* Record the smallest positions seen while we moved over
19356 display elements that are not visible. This is needed by
19357 redisplay_internal for optimizing the case where the cursor
19358 stays inside the same line. The rest of this function only
19359 considers positions that are actually displayed, so
19360 RECORD_MAX_MIN_POS will not otherwise record positions that
19361 are hscrolled to the left of the left edge of the window. */
19362 min_pos = CHARPOS (this_line_min_pos);
19363 min_bpos = BYTEPOS (this_line_min_pos);
19365 else
19367 /* We only do this when not calling `move_it_in_display_line_to'
19368 above, because move_it_in_display_line_to calls
19369 handle_line_prefix itself. */
19370 handle_line_prefix (it);
19373 /* Get the initial row height. This is either the height of the
19374 text hscrolled, if there is any, or zero. */
19375 row->ascent = it->max_ascent;
19376 row->height = it->max_ascent + it->max_descent;
19377 row->phys_ascent = it->max_phys_ascent;
19378 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19379 row->extra_line_spacing = it->max_extra_line_spacing;
19381 /* Utility macro to record max and min buffer positions seen until now. */
19382 #define RECORD_MAX_MIN_POS(IT) \
19383 do \
19385 int composition_p = !STRINGP ((IT)->string) \
19386 && ((IT)->what == IT_COMPOSITION); \
19387 ptrdiff_t current_pos = \
19388 composition_p ? (IT)->cmp_it.charpos \
19389 : IT_CHARPOS (*(IT)); \
19390 ptrdiff_t current_bpos = \
19391 composition_p ? CHAR_TO_BYTE (current_pos) \
19392 : IT_BYTEPOS (*(IT)); \
19393 if (current_pos < min_pos) \
19395 min_pos = current_pos; \
19396 min_bpos = current_bpos; \
19398 if (IT_CHARPOS (*it) > max_pos) \
19400 max_pos = IT_CHARPOS (*it); \
19401 max_bpos = IT_BYTEPOS (*it); \
19404 while (0)
19406 /* Loop generating characters. The loop is left with IT on the next
19407 character to display. */
19408 while (1)
19410 int n_glyphs_before, hpos_before, x_before;
19411 int x, nglyphs;
19412 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19414 /* Retrieve the next thing to display. Value is zero if end of
19415 buffer reached. */
19416 if (!get_next_display_element (it))
19418 /* Maybe add a space at the end of this line that is used to
19419 display the cursor there under X. Set the charpos of the
19420 first glyph of blank lines not corresponding to any text
19421 to -1. */
19422 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19423 row->exact_window_width_line_p = 1;
19424 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19425 || row->used[TEXT_AREA] == 0)
19427 row->glyphs[TEXT_AREA]->charpos = -1;
19428 row->displays_text_p = 0;
19430 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19431 && (!MINI_WINDOW_P (it->w)
19432 || (minibuf_level && EQ (it->window, minibuf_window))))
19433 row->indicate_empty_line_p = 1;
19436 it->continuation_lines_width = 0;
19437 row->ends_at_zv_p = 1;
19438 /* A row that displays right-to-left text must always have
19439 its last face extended all the way to the end of line,
19440 even if this row ends in ZV, because we still write to
19441 the screen left to right. We also need to extend the
19442 last face if the default face is remapped to some
19443 different face, otherwise the functions that clear
19444 portions of the screen will clear with the default face's
19445 background color. */
19446 if (row->reversed_p
19447 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19448 extend_face_to_end_of_line (it);
19449 break;
19452 /* Now, get the metrics of what we want to display. This also
19453 generates glyphs in `row' (which is IT->glyph_row). */
19454 n_glyphs_before = row->used[TEXT_AREA];
19455 x = it->current_x;
19457 /* Remember the line height so far in case the next element doesn't
19458 fit on the line. */
19459 if (it->line_wrap != TRUNCATE)
19461 ascent = it->max_ascent;
19462 descent = it->max_descent;
19463 phys_ascent = it->max_phys_ascent;
19464 phys_descent = it->max_phys_descent;
19466 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19468 if (IT_DISPLAYING_WHITESPACE (it))
19469 may_wrap = 1;
19470 else if (may_wrap)
19472 SAVE_IT (wrap_it, *it, wrap_data);
19473 wrap_x = x;
19474 wrap_row_used = row->used[TEXT_AREA];
19475 wrap_row_ascent = row->ascent;
19476 wrap_row_height = row->height;
19477 wrap_row_phys_ascent = row->phys_ascent;
19478 wrap_row_phys_height = row->phys_height;
19479 wrap_row_extra_line_spacing = row->extra_line_spacing;
19480 wrap_row_min_pos = min_pos;
19481 wrap_row_min_bpos = min_bpos;
19482 wrap_row_max_pos = max_pos;
19483 wrap_row_max_bpos = max_bpos;
19484 may_wrap = 0;
19489 PRODUCE_GLYPHS (it);
19491 /* If this display element was in marginal areas, continue with
19492 the next one. */
19493 if (it->area != TEXT_AREA)
19495 row->ascent = max (row->ascent, it->max_ascent);
19496 row->height = max (row->height, it->max_ascent + it->max_descent);
19497 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19498 row->phys_height = max (row->phys_height,
19499 it->max_phys_ascent + it->max_phys_descent);
19500 row->extra_line_spacing = max (row->extra_line_spacing,
19501 it->max_extra_line_spacing);
19502 set_iterator_to_next (it, 1);
19503 continue;
19506 /* Does the display element fit on the line? If we truncate
19507 lines, we should draw past the right edge of the window. If
19508 we don't truncate, we want to stop so that we can display the
19509 continuation glyph before the right margin. If lines are
19510 continued, there are two possible strategies for characters
19511 resulting in more than 1 glyph (e.g. tabs): Display as many
19512 glyphs as possible in this line and leave the rest for the
19513 continuation line, or display the whole element in the next
19514 line. Original redisplay did the former, so we do it also. */
19515 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19516 hpos_before = it->hpos;
19517 x_before = x;
19519 if (/* Not a newline. */
19520 nglyphs > 0
19521 /* Glyphs produced fit entirely in the line. */
19522 && it->current_x < it->last_visible_x)
19524 it->hpos += nglyphs;
19525 row->ascent = max (row->ascent, it->max_ascent);
19526 row->height = max (row->height, it->max_ascent + it->max_descent);
19527 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19528 row->phys_height = max (row->phys_height,
19529 it->max_phys_ascent + it->max_phys_descent);
19530 row->extra_line_spacing = max (row->extra_line_spacing,
19531 it->max_extra_line_spacing);
19532 if (it->current_x - it->pixel_width < it->first_visible_x)
19533 row->x = x - it->first_visible_x;
19534 /* Record the maximum and minimum buffer positions seen so
19535 far in glyphs that will be displayed by this row. */
19536 if (it->bidi_p)
19537 RECORD_MAX_MIN_POS (it);
19539 else
19541 int i, new_x;
19542 struct glyph *glyph;
19544 for (i = 0; i < nglyphs; ++i, x = new_x)
19546 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19547 new_x = x + glyph->pixel_width;
19549 if (/* Lines are continued. */
19550 it->line_wrap != TRUNCATE
19551 && (/* Glyph doesn't fit on the line. */
19552 new_x > it->last_visible_x
19553 /* Or it fits exactly on a window system frame. */
19554 || (new_x == it->last_visible_x
19555 && FRAME_WINDOW_P (it->f)
19556 && (row->reversed_p
19557 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19558 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19560 /* End of a continued line. */
19562 if (it->hpos == 0
19563 || (new_x == it->last_visible_x
19564 && FRAME_WINDOW_P (it->f)
19565 && (row->reversed_p
19566 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19567 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19569 /* Current glyph is the only one on the line or
19570 fits exactly on the line. We must continue
19571 the line because we can't draw the cursor
19572 after the glyph. */
19573 row->continued_p = 1;
19574 it->current_x = new_x;
19575 it->continuation_lines_width += new_x;
19576 ++it->hpos;
19577 if (i == nglyphs - 1)
19579 /* If line-wrap is on, check if a previous
19580 wrap point was found. */
19581 if (wrap_row_used > 0
19582 /* Even if there is a previous wrap
19583 point, continue the line here as
19584 usual, if (i) the previous character
19585 was a space or tab AND (ii) the
19586 current character is not. */
19587 && (!may_wrap
19588 || IT_DISPLAYING_WHITESPACE (it)))
19589 goto back_to_wrap;
19591 /* Record the maximum and minimum buffer
19592 positions seen so far in glyphs that will be
19593 displayed by this row. */
19594 if (it->bidi_p)
19595 RECORD_MAX_MIN_POS (it);
19596 set_iterator_to_next (it, 1);
19597 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19599 if (!get_next_display_element (it))
19601 row->exact_window_width_line_p = 1;
19602 it->continuation_lines_width = 0;
19603 row->continued_p = 0;
19604 row->ends_at_zv_p = 1;
19606 else if (ITERATOR_AT_END_OF_LINE_P (it))
19608 row->continued_p = 0;
19609 row->exact_window_width_line_p = 1;
19613 else if (it->bidi_p)
19614 RECORD_MAX_MIN_POS (it);
19616 else if (CHAR_GLYPH_PADDING_P (*glyph)
19617 && !FRAME_WINDOW_P (it->f))
19619 /* A padding glyph that doesn't fit on this line.
19620 This means the whole character doesn't fit
19621 on the line. */
19622 if (row->reversed_p)
19623 unproduce_glyphs (it, row->used[TEXT_AREA]
19624 - n_glyphs_before);
19625 row->used[TEXT_AREA] = n_glyphs_before;
19627 /* Fill the rest of the row with continuation
19628 glyphs like in 20.x. */
19629 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19630 < row->glyphs[1 + TEXT_AREA])
19631 produce_special_glyphs (it, IT_CONTINUATION);
19633 row->continued_p = 1;
19634 it->current_x = x_before;
19635 it->continuation_lines_width += x_before;
19637 /* Restore the height to what it was before the
19638 element not fitting on the line. */
19639 it->max_ascent = ascent;
19640 it->max_descent = descent;
19641 it->max_phys_ascent = phys_ascent;
19642 it->max_phys_descent = phys_descent;
19644 else if (wrap_row_used > 0)
19646 back_to_wrap:
19647 if (row->reversed_p)
19648 unproduce_glyphs (it,
19649 row->used[TEXT_AREA] - wrap_row_used);
19650 RESTORE_IT (it, &wrap_it, wrap_data);
19651 it->continuation_lines_width += wrap_x;
19652 row->used[TEXT_AREA] = wrap_row_used;
19653 row->ascent = wrap_row_ascent;
19654 row->height = wrap_row_height;
19655 row->phys_ascent = wrap_row_phys_ascent;
19656 row->phys_height = wrap_row_phys_height;
19657 row->extra_line_spacing = wrap_row_extra_line_spacing;
19658 min_pos = wrap_row_min_pos;
19659 min_bpos = wrap_row_min_bpos;
19660 max_pos = wrap_row_max_pos;
19661 max_bpos = wrap_row_max_bpos;
19662 row->continued_p = 1;
19663 row->ends_at_zv_p = 0;
19664 row->exact_window_width_line_p = 0;
19665 it->continuation_lines_width += x;
19667 /* Make sure that a non-default face is extended
19668 up to the right margin of the window. */
19669 extend_face_to_end_of_line (it);
19671 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19673 /* A TAB that extends past the right edge of the
19674 window. This produces a single glyph on
19675 window system frames. We leave the glyph in
19676 this row and let it fill the row, but don't
19677 consume the TAB. */
19678 if ((row->reversed_p
19679 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19680 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19681 produce_special_glyphs (it, IT_CONTINUATION);
19682 it->continuation_lines_width += it->last_visible_x;
19683 row->ends_in_middle_of_char_p = 1;
19684 row->continued_p = 1;
19685 glyph->pixel_width = it->last_visible_x - x;
19686 it->starts_in_middle_of_char_p = 1;
19688 else
19690 /* Something other than a TAB that draws past
19691 the right edge of the window. Restore
19692 positions to values before the element. */
19693 if (row->reversed_p)
19694 unproduce_glyphs (it, row->used[TEXT_AREA]
19695 - (n_glyphs_before + i));
19696 row->used[TEXT_AREA] = n_glyphs_before + i;
19698 /* Display continuation glyphs. */
19699 it->current_x = x_before;
19700 it->continuation_lines_width += x;
19701 if (!FRAME_WINDOW_P (it->f)
19702 || (row->reversed_p
19703 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19704 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19705 produce_special_glyphs (it, IT_CONTINUATION);
19706 row->continued_p = 1;
19708 extend_face_to_end_of_line (it);
19710 if (nglyphs > 1 && i > 0)
19712 row->ends_in_middle_of_char_p = 1;
19713 it->starts_in_middle_of_char_p = 1;
19716 /* Restore the height to what it was before the
19717 element not fitting on the line. */
19718 it->max_ascent = ascent;
19719 it->max_descent = descent;
19720 it->max_phys_ascent = phys_ascent;
19721 it->max_phys_descent = phys_descent;
19724 break;
19726 else if (new_x > it->first_visible_x)
19728 /* Increment number of glyphs actually displayed. */
19729 ++it->hpos;
19731 /* Record the maximum and minimum buffer positions
19732 seen so far in glyphs that will be displayed by
19733 this row. */
19734 if (it->bidi_p)
19735 RECORD_MAX_MIN_POS (it);
19737 if (x < it->first_visible_x)
19738 /* Glyph is partially visible, i.e. row starts at
19739 negative X position. */
19740 row->x = x - it->first_visible_x;
19742 else
19744 /* Glyph is completely off the left margin of the
19745 window. This should not happen because of the
19746 move_it_in_display_line at the start of this
19747 function, unless the text display area of the
19748 window is empty. */
19749 eassert (it->first_visible_x <= it->last_visible_x);
19752 /* Even if this display element produced no glyphs at all,
19753 we want to record its position. */
19754 if (it->bidi_p && nglyphs == 0)
19755 RECORD_MAX_MIN_POS (it);
19757 row->ascent = max (row->ascent, it->max_ascent);
19758 row->height = max (row->height, it->max_ascent + it->max_descent);
19759 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19760 row->phys_height = max (row->phys_height,
19761 it->max_phys_ascent + it->max_phys_descent);
19762 row->extra_line_spacing = max (row->extra_line_spacing,
19763 it->max_extra_line_spacing);
19765 /* End of this display line if row is continued. */
19766 if (row->continued_p || row->ends_at_zv_p)
19767 break;
19770 at_end_of_line:
19771 /* Is this a line end? If yes, we're also done, after making
19772 sure that a non-default face is extended up to the right
19773 margin of the window. */
19774 if (ITERATOR_AT_END_OF_LINE_P (it))
19776 int used_before = row->used[TEXT_AREA];
19778 row->ends_in_newline_from_string_p = STRINGP (it->object);
19780 /* Add a space at the end of the line that is used to
19781 display the cursor there. */
19782 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19783 append_space_for_newline (it, 0);
19785 /* Extend the face to the end of the line. */
19786 extend_face_to_end_of_line (it);
19788 /* Make sure we have the position. */
19789 if (used_before == 0)
19790 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19792 /* Record the position of the newline, for use in
19793 find_row_edges. */
19794 it->eol_pos = it->current.pos;
19796 /* Consume the line end. This skips over invisible lines. */
19797 set_iterator_to_next (it, 1);
19798 it->continuation_lines_width = 0;
19799 break;
19802 /* Proceed with next display element. Note that this skips
19803 over lines invisible because of selective display. */
19804 set_iterator_to_next (it, 1);
19806 /* If we truncate lines, we are done when the last displayed
19807 glyphs reach past the right margin of the window. */
19808 if (it->line_wrap == TRUNCATE
19809 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19810 ? (it->current_x >= it->last_visible_x)
19811 : (it->current_x > it->last_visible_x)))
19813 /* Maybe add truncation glyphs. */
19814 if (!FRAME_WINDOW_P (it->f)
19815 || (row->reversed_p
19816 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19817 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19819 int i, n;
19821 if (!row->reversed_p)
19823 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19824 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19825 break;
19827 else
19829 for (i = 0; i < row->used[TEXT_AREA]; i++)
19830 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19831 break;
19832 /* Remove any padding glyphs at the front of ROW, to
19833 make room for the truncation glyphs we will be
19834 adding below. The loop below always inserts at
19835 least one truncation glyph, so also remove the
19836 last glyph added to ROW. */
19837 unproduce_glyphs (it, i + 1);
19838 /* Adjust i for the loop below. */
19839 i = row->used[TEXT_AREA] - (i + 1);
19842 it->current_x = x_before;
19843 if (!FRAME_WINDOW_P (it->f))
19845 for (n = row->used[TEXT_AREA]; i < n; ++i)
19847 row->used[TEXT_AREA] = i;
19848 produce_special_glyphs (it, IT_TRUNCATION);
19851 else
19853 row->used[TEXT_AREA] = i;
19854 produce_special_glyphs (it, IT_TRUNCATION);
19857 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19859 /* Don't truncate if we can overflow newline into fringe. */
19860 if (!get_next_display_element (it))
19862 it->continuation_lines_width = 0;
19863 row->ends_at_zv_p = 1;
19864 row->exact_window_width_line_p = 1;
19865 break;
19867 if (ITERATOR_AT_END_OF_LINE_P (it))
19869 row->exact_window_width_line_p = 1;
19870 goto at_end_of_line;
19872 it->current_x = x_before;
19875 row->truncated_on_right_p = 1;
19876 it->continuation_lines_width = 0;
19877 reseat_at_next_visible_line_start (it, 0);
19878 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19879 it->hpos = hpos_before;
19880 break;
19884 if (wrap_data)
19885 bidi_unshelve_cache (wrap_data, 1);
19887 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19888 at the left window margin. */
19889 if (it->first_visible_x
19890 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19892 if (!FRAME_WINDOW_P (it->f)
19893 || (row->reversed_p
19894 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19895 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19896 insert_left_trunc_glyphs (it);
19897 row->truncated_on_left_p = 1;
19900 /* Remember the position at which this line ends.
19902 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19903 cannot be before the call to find_row_edges below, since that is
19904 where these positions are determined. */
19905 row->end = it->current;
19906 if (!it->bidi_p)
19908 row->minpos = row->start.pos;
19909 row->maxpos = row->end.pos;
19911 else
19913 /* ROW->minpos and ROW->maxpos must be the smallest and
19914 `1 + the largest' buffer positions in ROW. But if ROW was
19915 bidi-reordered, these two positions can be anywhere in the
19916 row, so we must determine them now. */
19917 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19920 /* If the start of this line is the overlay arrow-position, then
19921 mark this glyph row as the one containing the overlay arrow.
19922 This is clearly a mess with variable size fonts. It would be
19923 better to let it be displayed like cursors under X. */
19924 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19925 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19926 !NILP (overlay_arrow_string)))
19928 /* Overlay arrow in window redisplay is a fringe bitmap. */
19929 if (STRINGP (overlay_arrow_string))
19931 struct glyph_row *arrow_row
19932 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19933 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19934 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19935 struct glyph *p = row->glyphs[TEXT_AREA];
19936 struct glyph *p2, *end;
19938 /* Copy the arrow glyphs. */
19939 while (glyph < arrow_end)
19940 *p++ = *glyph++;
19942 /* Throw away padding glyphs. */
19943 p2 = p;
19944 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19945 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19946 ++p2;
19947 if (p2 > p)
19949 while (p2 < end)
19950 *p++ = *p2++;
19951 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19954 else
19956 eassert (INTEGERP (overlay_arrow_string));
19957 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19959 overlay_arrow_seen = 1;
19962 /* Highlight trailing whitespace. */
19963 if (!NILP (Vshow_trailing_whitespace))
19964 highlight_trailing_whitespace (it->f, it->glyph_row);
19966 /* Compute pixel dimensions of this line. */
19967 compute_line_metrics (it);
19969 /* Implementation note: No changes in the glyphs of ROW or in their
19970 faces can be done past this point, because compute_line_metrics
19971 computes ROW's hash value and stores it within the glyph_row
19972 structure. */
19974 /* Record whether this row ends inside an ellipsis. */
19975 row->ends_in_ellipsis_p
19976 = (it->method == GET_FROM_DISPLAY_VECTOR
19977 && it->ellipsis_p);
19979 /* Save fringe bitmaps in this row. */
19980 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19981 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19982 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19983 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19985 it->left_user_fringe_bitmap = 0;
19986 it->left_user_fringe_face_id = 0;
19987 it->right_user_fringe_bitmap = 0;
19988 it->right_user_fringe_face_id = 0;
19990 /* Maybe set the cursor. */
19991 cvpos = it->w->cursor.vpos;
19992 if ((cvpos < 0
19993 /* In bidi-reordered rows, keep checking for proper cursor
19994 position even if one has been found already, because buffer
19995 positions in such rows change non-linearly with ROW->VPOS,
19996 when a line is continued. One exception: when we are at ZV,
19997 display cursor on the first suitable glyph row, since all
19998 the empty rows after that also have their position set to ZV. */
19999 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20000 lines' rows is implemented for bidi-reordered rows. */
20001 || (it->bidi_p
20002 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20003 && PT >= MATRIX_ROW_START_CHARPOS (row)
20004 && PT <= MATRIX_ROW_END_CHARPOS (row)
20005 && cursor_row_p (row))
20006 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20008 /* Prepare for the next line. This line starts horizontally at (X
20009 HPOS) = (0 0). Vertical positions are incremented. As a
20010 convenience for the caller, IT->glyph_row is set to the next
20011 row to be used. */
20012 it->current_x = it->hpos = 0;
20013 it->current_y += row->height;
20014 SET_TEXT_POS (it->eol_pos, 0, 0);
20015 ++it->vpos;
20016 ++it->glyph_row;
20017 /* The next row should by default use the same value of the
20018 reversed_p flag as this one. set_iterator_to_next decides when
20019 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20020 the flag accordingly. */
20021 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20022 it->glyph_row->reversed_p = row->reversed_p;
20023 it->start = row->end;
20024 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20026 #undef RECORD_MAX_MIN_POS
20029 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20030 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20031 doc: /* Return paragraph direction at point in BUFFER.
20032 Value is either `left-to-right' or `right-to-left'.
20033 If BUFFER is omitted or nil, it defaults to the current buffer.
20035 Paragraph direction determines how the text in the paragraph is displayed.
20036 In left-to-right paragraphs, text begins at the left margin of the window
20037 and the reading direction is generally left to right. In right-to-left
20038 paragraphs, text begins at the right margin and is read from right to left.
20040 See also `bidi-paragraph-direction'. */)
20041 (Lisp_Object buffer)
20043 struct buffer *buf = current_buffer;
20044 struct buffer *old = buf;
20046 if (! NILP (buffer))
20048 CHECK_BUFFER (buffer);
20049 buf = XBUFFER (buffer);
20052 if (NILP (BVAR (buf, bidi_display_reordering))
20053 || NILP (BVAR (buf, enable_multibyte_characters))
20054 /* When we are loading loadup.el, the character property tables
20055 needed for bidi iteration are not yet available. */
20056 || !NILP (Vpurify_flag))
20057 return Qleft_to_right;
20058 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20059 return BVAR (buf, bidi_paragraph_direction);
20060 else
20062 /* Determine the direction from buffer text. We could try to
20063 use current_matrix if it is up to date, but this seems fast
20064 enough as it is. */
20065 struct bidi_it itb;
20066 ptrdiff_t pos = BUF_PT (buf);
20067 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20068 int c;
20069 void *itb_data = bidi_shelve_cache ();
20071 set_buffer_temp (buf);
20072 /* bidi_paragraph_init finds the base direction of the paragraph
20073 by searching forward from paragraph start. We need the base
20074 direction of the current or _previous_ paragraph, so we need
20075 to make sure we are within that paragraph. To that end, find
20076 the previous non-empty line. */
20077 if (pos >= ZV && pos > BEGV)
20078 DEC_BOTH (pos, bytepos);
20079 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20080 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20082 while ((c = FETCH_BYTE (bytepos)) == '\n'
20083 || c == ' ' || c == '\t' || c == '\f')
20085 if (bytepos <= BEGV_BYTE)
20086 break;
20087 bytepos--;
20088 pos--;
20090 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20091 bytepos--;
20093 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20094 itb.paragraph_dir = NEUTRAL_DIR;
20095 itb.string.s = NULL;
20096 itb.string.lstring = Qnil;
20097 itb.string.bufpos = 0;
20098 itb.string.unibyte = 0;
20099 /* We have no window to use here for ignoring window-specific
20100 overlays. Using NULL for window pointer will cause
20101 compute_display_string_pos to use the current buffer. */
20102 itb.w = NULL;
20103 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20104 bidi_unshelve_cache (itb_data, 0);
20105 set_buffer_temp (old);
20106 switch (itb.paragraph_dir)
20108 case L2R:
20109 return Qleft_to_right;
20110 break;
20111 case R2L:
20112 return Qright_to_left;
20113 break;
20114 default:
20115 emacs_abort ();
20120 DEFUN ("move-point-visually", Fmove_point_visually,
20121 Smove_point_visually, 1, 1, 0,
20122 doc: /* Move point in the visual order in the specified DIRECTION.
20123 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20124 left.
20126 Value is the new character position of point. */)
20127 (Lisp_Object direction)
20129 struct window *w = XWINDOW (selected_window);
20130 struct buffer *b = XBUFFER (w->contents);
20131 struct glyph_row *row;
20132 int dir;
20133 Lisp_Object paragraph_dir;
20135 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20136 (!(ROW)->continued_p \
20137 && INTEGERP ((GLYPH)->object) \
20138 && (GLYPH)->type == CHAR_GLYPH \
20139 && (GLYPH)->u.ch == ' ' \
20140 && (GLYPH)->charpos >= 0 \
20141 && !(GLYPH)->avoid_cursor_p)
20143 CHECK_NUMBER (direction);
20144 dir = XINT (direction);
20145 if (dir > 0)
20146 dir = 1;
20147 else
20148 dir = -1;
20150 /* If current matrix is up-to-date, we can use the information
20151 recorded in the glyphs, at least as long as the goal is on the
20152 screen. */
20153 if (w->window_end_valid
20154 && !windows_or_buffers_changed
20155 && b
20156 && !b->clip_changed
20157 && !b->prevent_redisplay_optimizations_p
20158 && !window_outdated (w)
20159 && w->cursor.vpos >= 0
20160 && w->cursor.vpos < w->current_matrix->nrows
20161 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20163 struct glyph *g = row->glyphs[TEXT_AREA];
20164 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20165 struct glyph *gpt = g + w->cursor.hpos;
20167 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20169 if (BUFFERP (g->object) && g->charpos != PT)
20171 SET_PT (g->charpos);
20172 w->cursor.vpos = -1;
20173 return make_number (PT);
20175 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20177 ptrdiff_t new_pos;
20179 if (BUFFERP (gpt->object))
20181 new_pos = PT;
20182 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20183 new_pos += (row->reversed_p ? -dir : dir);
20184 else
20185 new_pos -= (row->reversed_p ? -dir : dir);;
20187 else if (BUFFERP (g->object))
20188 new_pos = g->charpos;
20189 else
20190 break;
20191 SET_PT (new_pos);
20192 w->cursor.vpos = -1;
20193 return make_number (PT);
20195 else if (ROW_GLYPH_NEWLINE_P (row, g))
20197 /* Glyphs inserted at the end of a non-empty line for
20198 positioning the cursor have zero charpos, so we must
20199 deduce the value of point by other means. */
20200 if (g->charpos > 0)
20201 SET_PT (g->charpos);
20202 else if (row->ends_at_zv_p && PT != ZV)
20203 SET_PT (ZV);
20204 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20205 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20206 else
20207 break;
20208 w->cursor.vpos = -1;
20209 return make_number (PT);
20212 if (g == e || INTEGERP (g->object))
20214 if (row->truncated_on_left_p || row->truncated_on_right_p)
20215 goto simulate_display;
20216 if (!row->reversed_p)
20217 row += dir;
20218 else
20219 row -= dir;
20220 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20221 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20222 goto simulate_display;
20224 if (dir > 0)
20226 if (row->reversed_p && !row->continued_p)
20228 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20229 w->cursor.vpos = -1;
20230 return make_number (PT);
20232 g = row->glyphs[TEXT_AREA];
20233 e = g + row->used[TEXT_AREA];
20234 for ( ; g < e; g++)
20236 if (BUFFERP (g->object)
20237 /* Empty lines have only one glyph, which stands
20238 for the newline, and whose charpos is the
20239 buffer position of the newline. */
20240 || ROW_GLYPH_NEWLINE_P (row, g)
20241 /* When the buffer ends in a newline, the line at
20242 EOB also has one glyph, but its charpos is -1. */
20243 || (row->ends_at_zv_p
20244 && !row->reversed_p
20245 && INTEGERP (g->object)
20246 && g->type == CHAR_GLYPH
20247 && g->u.ch == ' '))
20249 if (g->charpos > 0)
20250 SET_PT (g->charpos);
20251 else if (!row->reversed_p
20252 && row->ends_at_zv_p
20253 && PT != ZV)
20254 SET_PT (ZV);
20255 else
20256 continue;
20257 w->cursor.vpos = -1;
20258 return make_number (PT);
20262 else
20264 if (!row->reversed_p && !row->continued_p)
20266 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20267 w->cursor.vpos = -1;
20268 return make_number (PT);
20270 e = row->glyphs[TEXT_AREA];
20271 g = e + row->used[TEXT_AREA] - 1;
20272 for ( ; g >= e; g--)
20274 if (BUFFERP (g->object)
20275 || (ROW_GLYPH_NEWLINE_P (row, g)
20276 && g->charpos > 0)
20277 /* Empty R2L lines on GUI frames have the buffer
20278 position of the newline stored in the stretch
20279 glyph. */
20280 || g->type == STRETCH_GLYPH
20281 || (row->ends_at_zv_p
20282 && row->reversed_p
20283 && INTEGERP (g->object)
20284 && g->type == CHAR_GLYPH
20285 && g->u.ch == ' '))
20287 if (g->charpos > 0)
20288 SET_PT (g->charpos);
20289 else if (row->reversed_p
20290 && row->ends_at_zv_p
20291 && PT != ZV)
20292 SET_PT (ZV);
20293 else
20294 continue;
20295 w->cursor.vpos = -1;
20296 return make_number (PT);
20303 simulate_display:
20305 /* If we wind up here, we failed to move by using the glyphs, so we
20306 need to simulate display instead. */
20308 if (b)
20309 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20310 else
20311 paragraph_dir = Qleft_to_right;
20312 if (EQ (paragraph_dir, Qright_to_left))
20313 dir = -dir;
20314 if (PT <= BEGV && dir < 0)
20315 xsignal0 (Qbeginning_of_buffer);
20316 else if (PT >= ZV && dir > 0)
20317 xsignal0 (Qend_of_buffer);
20318 else
20320 struct text_pos pt;
20321 struct it it;
20322 int pt_x, target_x, pixel_width, pt_vpos;
20323 bool at_eol_p;
20324 bool overshoot_expected = false;
20325 bool target_is_eol_p = false;
20327 /* Setup the arena. */
20328 SET_TEXT_POS (pt, PT, PT_BYTE);
20329 start_display (&it, w, pt);
20331 if (it.cmp_it.id < 0
20332 && it.method == GET_FROM_STRING
20333 && it.area == TEXT_AREA
20334 && it.string_from_display_prop_p
20335 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20336 overshoot_expected = true;
20338 /* Find the X coordinate of point. We start from the beginning
20339 of this or previous line to make sure we are before point in
20340 the logical order (since the move_it_* functions can only
20341 move forward). */
20342 reseat_at_previous_visible_line_start (&it);
20343 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20344 if (IT_CHARPOS (it) != PT)
20345 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20346 -1, -1, -1, MOVE_TO_POS);
20347 pt_x = it.current_x;
20348 pt_vpos = it.vpos;
20349 if (dir > 0 || overshoot_expected)
20351 struct glyph_row *row = it.glyph_row;
20353 /* When point is at beginning of line, we don't have
20354 information about the glyph there loaded into struct
20355 it. Calling get_next_display_element fixes that. */
20356 if (pt_x == 0)
20357 get_next_display_element (&it);
20358 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20359 it.glyph_row = NULL;
20360 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20361 it.glyph_row = row;
20362 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20363 it, lest it will become out of sync with it's buffer
20364 position. */
20365 it.current_x = pt_x;
20367 else
20368 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20369 pixel_width = it.pixel_width;
20370 if (overshoot_expected && at_eol_p)
20371 pixel_width = 0;
20372 else if (pixel_width <= 0)
20373 pixel_width = 1;
20375 /* If there's a display string at point, we are actually at the
20376 glyph to the left of point, so we need to correct the X
20377 coordinate. */
20378 if (overshoot_expected)
20379 pt_x += pixel_width;
20381 /* Compute target X coordinate, either to the left or to the
20382 right of point. On TTY frames, all characters have the same
20383 pixel width of 1, so we can use that. On GUI frames we don't
20384 have an easy way of getting at the pixel width of the
20385 character to the left of point, so we use a different method
20386 of getting to that place. */
20387 if (dir > 0)
20388 target_x = pt_x + pixel_width;
20389 else
20390 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20392 /* Target X coordinate could be one line above or below the line
20393 of point, in which case we need to adjust the target X
20394 coordinate. Also, if moving to the left, we need to begin at
20395 the left edge of the point's screen line. */
20396 if (dir < 0)
20398 if (pt_x > 0)
20400 start_display (&it, w, pt);
20401 reseat_at_previous_visible_line_start (&it);
20402 it.current_x = it.current_y = it.hpos = 0;
20403 if (pt_vpos != 0)
20404 move_it_by_lines (&it, pt_vpos);
20406 else
20408 move_it_by_lines (&it, -1);
20409 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20410 target_is_eol_p = true;
20413 else
20415 if (at_eol_p
20416 || (target_x >= it.last_visible_x
20417 && it.line_wrap != TRUNCATE))
20419 if (pt_x > 0)
20420 move_it_by_lines (&it, 0);
20421 move_it_by_lines (&it, 1);
20422 target_x = 0;
20426 /* Move to the target X coordinate. */
20427 #ifdef HAVE_WINDOW_SYSTEM
20428 /* On GUI frames, as we don't know the X coordinate of the
20429 character to the left of point, moving point to the left
20430 requires walking, one grapheme cluster at a time, until we
20431 find ourself at a place immediately to the left of the
20432 character at point. */
20433 if (FRAME_WINDOW_P (it.f) && dir < 0)
20435 struct text_pos new_pos = it.current.pos;
20436 enum move_it_result rc = MOVE_X_REACHED;
20438 while (it.current_x + it.pixel_width <= target_x
20439 && rc == MOVE_X_REACHED)
20441 int new_x = it.current_x + it.pixel_width;
20443 new_pos = it.current.pos;
20444 if (new_x == it.current_x)
20445 new_x++;
20446 rc = move_it_in_display_line_to (&it, ZV, new_x,
20447 MOVE_TO_POS | MOVE_TO_X);
20448 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20449 break;
20451 /* If we ended up on a composed character inside
20452 bidi-reordered text (e.g., Hebrew text with diacritics),
20453 the iterator gives us the buffer position of the last (in
20454 logical order) character of the composed grapheme cluster,
20455 which is not what we want. So we cheat: we compute the
20456 character position of the character that follows (in the
20457 logical order) the one where the above loop stopped. That
20458 character will appear on display to the left of point. */
20459 if (it.bidi_p
20460 && it.bidi_it.scan_dir == -1
20461 && new_pos.charpos - IT_CHARPOS (it) > 1)
20463 new_pos.charpos = IT_CHARPOS (it) + 1;
20464 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20466 it.current.pos = new_pos;
20468 else
20469 #endif
20470 if (it.current_x != target_x)
20471 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20473 /* When lines are truncated, the above loop will stop at the
20474 window edge. But we want to get to the end of line, even if
20475 it is beyond the window edge; automatic hscroll will then
20476 scroll the window to show point as appropriate. */
20477 if (target_is_eol_p && it.line_wrap == TRUNCATE
20478 && get_next_display_element (&it))
20480 struct text_pos new_pos = it.current.pos;
20482 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20484 set_iterator_to_next (&it, 0);
20485 if (it.method == GET_FROM_BUFFER)
20486 new_pos = it.current.pos;
20487 if (!get_next_display_element (&it))
20488 break;
20491 it.current.pos = new_pos;
20494 /* If we ended up in a display string that covers point, move to
20495 buffer position to the right in the visual order. */
20496 if (dir > 0)
20498 while (IT_CHARPOS (it) == PT)
20500 set_iterator_to_next (&it, 0);
20501 if (!get_next_display_element (&it))
20502 break;
20506 /* Move point to that position. */
20507 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20510 return make_number (PT);
20512 #undef ROW_GLYPH_NEWLINE_P
20516 /***********************************************************************
20517 Menu Bar
20518 ***********************************************************************/
20520 /* Redisplay the menu bar in the frame for window W.
20522 The menu bar of X frames that don't have X toolkit support is
20523 displayed in a special window W->frame->menu_bar_window.
20525 The menu bar of terminal frames is treated specially as far as
20526 glyph matrices are concerned. Menu bar lines are not part of
20527 windows, so the update is done directly on the frame matrix rows
20528 for the menu bar. */
20530 static void
20531 display_menu_bar (struct window *w)
20533 struct frame *f = XFRAME (WINDOW_FRAME (w));
20534 struct it it;
20535 Lisp_Object items;
20536 int i;
20538 /* Don't do all this for graphical frames. */
20539 #ifdef HAVE_NTGUI
20540 if (FRAME_W32_P (f))
20541 return;
20542 #endif
20543 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20544 if (FRAME_X_P (f))
20545 return;
20546 #endif
20548 #ifdef HAVE_NS
20549 if (FRAME_NS_P (f))
20550 return;
20551 #endif /* HAVE_NS */
20553 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20554 eassert (!FRAME_WINDOW_P (f));
20555 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20556 it.first_visible_x = 0;
20557 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20558 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20559 if (FRAME_WINDOW_P (f))
20561 /* Menu bar lines are displayed in the desired matrix of the
20562 dummy window menu_bar_window. */
20563 struct window *menu_w;
20564 menu_w = XWINDOW (f->menu_bar_window);
20565 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20566 MENU_FACE_ID);
20567 it.first_visible_x = 0;
20568 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20570 else
20571 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20573 /* This is a TTY frame, i.e. character hpos/vpos are used as
20574 pixel x/y. */
20575 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20576 MENU_FACE_ID);
20577 it.first_visible_x = 0;
20578 it.last_visible_x = FRAME_COLS (f);
20581 /* FIXME: This should be controlled by a user option. See the
20582 comments in redisplay_tool_bar and display_mode_line about
20583 this. */
20584 it.paragraph_embedding = L2R;
20586 /* Clear all rows of the menu bar. */
20587 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20589 struct glyph_row *row = it.glyph_row + i;
20590 clear_glyph_row (row);
20591 row->enabled_p = 1;
20592 row->full_width_p = 1;
20595 /* Display all items of the menu bar. */
20596 items = FRAME_MENU_BAR_ITEMS (it.f);
20597 for (i = 0; i < ASIZE (items); i += 4)
20599 Lisp_Object string;
20601 /* Stop at nil string. */
20602 string = AREF (items, i + 1);
20603 if (NILP (string))
20604 break;
20606 /* Remember where item was displayed. */
20607 ASET (items, i + 3, make_number (it.hpos));
20609 /* Display the item, pad with one space. */
20610 if (it.current_x < it.last_visible_x)
20611 display_string (NULL, string, Qnil, 0, 0, &it,
20612 SCHARS (string) + 1, 0, 0, -1);
20615 /* Fill out the line with spaces. */
20616 if (it.current_x < it.last_visible_x)
20617 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20619 /* Compute the total height of the lines. */
20620 compute_line_metrics (&it);
20625 /***********************************************************************
20626 Mode Line
20627 ***********************************************************************/
20629 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20630 FORCE is non-zero, redisplay mode lines unconditionally.
20631 Otherwise, redisplay only mode lines that are garbaged. Value is
20632 the number of windows whose mode lines were redisplayed. */
20634 static int
20635 redisplay_mode_lines (Lisp_Object window, int force)
20637 int nwindows = 0;
20639 while (!NILP (window))
20641 struct window *w = XWINDOW (window);
20643 if (WINDOWP (w->contents))
20644 nwindows += redisplay_mode_lines (w->contents, force);
20645 else if (force
20646 || FRAME_GARBAGED_P (XFRAME (w->frame))
20647 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20649 struct text_pos lpoint;
20650 struct buffer *old = current_buffer;
20652 /* Set the window's buffer for the mode line display. */
20653 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20654 set_buffer_internal_1 (XBUFFER (w->contents));
20656 /* Point refers normally to the selected window. For any
20657 other window, set up appropriate value. */
20658 if (!EQ (window, selected_window))
20660 struct text_pos pt;
20662 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20663 if (CHARPOS (pt) < BEGV)
20664 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20665 else if (CHARPOS (pt) > (ZV - 1))
20666 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20667 else
20668 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20671 /* Display mode lines. */
20672 clear_glyph_matrix (w->desired_matrix);
20673 if (display_mode_lines (w))
20675 ++nwindows;
20676 w->must_be_updated_p = 1;
20679 /* Restore old settings. */
20680 set_buffer_internal_1 (old);
20681 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20684 window = w->next;
20687 return nwindows;
20691 /* Display the mode and/or header line of window W. Value is the
20692 sum number of mode lines and header lines displayed. */
20694 static int
20695 display_mode_lines (struct window *w)
20697 Lisp_Object old_selected_window = selected_window;
20698 Lisp_Object old_selected_frame = selected_frame;
20699 Lisp_Object new_frame = w->frame;
20700 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20701 int n = 0;
20703 selected_frame = new_frame;
20704 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20705 or window's point, then we'd need select_window_1 here as well. */
20706 XSETWINDOW (selected_window, w);
20707 XFRAME (new_frame)->selected_window = selected_window;
20709 /* These will be set while the mode line specs are processed. */
20710 line_number_displayed = 0;
20711 w->column_number_displayed = -1;
20713 if (WINDOW_WANTS_MODELINE_P (w))
20715 struct window *sel_w = XWINDOW (old_selected_window);
20717 /* Select mode line face based on the real selected window. */
20718 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20719 BVAR (current_buffer, mode_line_format));
20720 ++n;
20723 if (WINDOW_WANTS_HEADER_LINE_P (w))
20725 display_mode_line (w, HEADER_LINE_FACE_ID,
20726 BVAR (current_buffer, header_line_format));
20727 ++n;
20730 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20731 selected_frame = old_selected_frame;
20732 selected_window = old_selected_window;
20733 return n;
20737 /* Display mode or header line of window W. FACE_ID specifies which
20738 line to display; it is either MODE_LINE_FACE_ID or
20739 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20740 display. Value is the pixel height of the mode/header line
20741 displayed. */
20743 static int
20744 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20746 struct it it;
20747 struct face *face;
20748 ptrdiff_t count = SPECPDL_INDEX ();
20750 init_iterator (&it, w, -1, -1, NULL, face_id);
20751 /* Don't extend on a previously drawn mode-line.
20752 This may happen if called from pos_visible_p. */
20753 it.glyph_row->enabled_p = 0;
20754 prepare_desired_row (it.glyph_row);
20756 it.glyph_row->mode_line_p = 1;
20758 /* FIXME: This should be controlled by a user option. But
20759 supporting such an option is not trivial, since the mode line is
20760 made up of many separate strings. */
20761 it.paragraph_embedding = L2R;
20763 record_unwind_protect (unwind_format_mode_line,
20764 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20766 mode_line_target = MODE_LINE_DISPLAY;
20768 /* Temporarily make frame's keyboard the current kboard so that
20769 kboard-local variables in the mode_line_format will get the right
20770 values. */
20771 push_kboard (FRAME_KBOARD (it.f));
20772 record_unwind_save_match_data ();
20773 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20774 pop_kboard ();
20776 unbind_to (count, Qnil);
20778 /* Fill up with spaces. */
20779 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20781 compute_line_metrics (&it);
20782 it.glyph_row->full_width_p = 1;
20783 it.glyph_row->continued_p = 0;
20784 it.glyph_row->truncated_on_left_p = 0;
20785 it.glyph_row->truncated_on_right_p = 0;
20787 /* Make a 3D mode-line have a shadow at its right end. */
20788 face = FACE_FROM_ID (it.f, face_id);
20789 extend_face_to_end_of_line (&it);
20790 if (face->box != FACE_NO_BOX)
20792 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20793 + it.glyph_row->used[TEXT_AREA] - 1);
20794 last->right_box_line_p = 1;
20797 return it.glyph_row->height;
20800 /* Move element ELT in LIST to the front of LIST.
20801 Return the updated list. */
20803 static Lisp_Object
20804 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20806 register Lisp_Object tail, prev;
20807 register Lisp_Object tem;
20809 tail = list;
20810 prev = Qnil;
20811 while (CONSP (tail))
20813 tem = XCAR (tail);
20815 if (EQ (elt, tem))
20817 /* Splice out the link TAIL. */
20818 if (NILP (prev))
20819 list = XCDR (tail);
20820 else
20821 Fsetcdr (prev, XCDR (tail));
20823 /* Now make it the first. */
20824 Fsetcdr (tail, list);
20825 return tail;
20827 else
20828 prev = tail;
20829 tail = XCDR (tail);
20830 QUIT;
20833 /* Not found--return unchanged LIST. */
20834 return list;
20837 /* Contribute ELT to the mode line for window IT->w. How it
20838 translates into text depends on its data type.
20840 IT describes the display environment in which we display, as usual.
20842 DEPTH is the depth in recursion. It is used to prevent
20843 infinite recursion here.
20845 FIELD_WIDTH is the number of characters the display of ELT should
20846 occupy in the mode line, and PRECISION is the maximum number of
20847 characters to display from ELT's representation. See
20848 display_string for details.
20850 Returns the hpos of the end of the text generated by ELT.
20852 PROPS is a property list to add to any string we encounter.
20854 If RISKY is nonzero, remove (disregard) any properties in any string
20855 we encounter, and ignore :eval and :propertize.
20857 The global variable `mode_line_target' determines whether the
20858 output is passed to `store_mode_line_noprop',
20859 `store_mode_line_string', or `display_string'. */
20861 static int
20862 display_mode_element (struct it *it, int depth, int field_width, int precision,
20863 Lisp_Object elt, Lisp_Object props, int risky)
20865 int n = 0, field, prec;
20866 int literal = 0;
20868 tail_recurse:
20869 if (depth > 100)
20870 elt = build_string ("*too-deep*");
20872 depth++;
20874 switch (XTYPE (elt))
20876 case Lisp_String:
20878 /* A string: output it and check for %-constructs within it. */
20879 unsigned char c;
20880 ptrdiff_t offset = 0;
20882 if (SCHARS (elt) > 0
20883 && (!NILP (props) || risky))
20885 Lisp_Object oprops, aelt;
20886 oprops = Ftext_properties_at (make_number (0), elt);
20888 /* If the starting string's properties are not what
20889 we want, translate the string. Also, if the string
20890 is risky, do that anyway. */
20892 if (NILP (Fequal (props, oprops)) || risky)
20894 /* If the starting string has properties,
20895 merge the specified ones onto the existing ones. */
20896 if (! NILP (oprops) && !risky)
20898 Lisp_Object tem;
20900 oprops = Fcopy_sequence (oprops);
20901 tem = props;
20902 while (CONSP (tem))
20904 oprops = Fplist_put (oprops, XCAR (tem),
20905 XCAR (XCDR (tem)));
20906 tem = XCDR (XCDR (tem));
20908 props = oprops;
20911 aelt = Fassoc (elt, mode_line_proptrans_alist);
20912 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20914 /* AELT is what we want. Move it to the front
20915 without consing. */
20916 elt = XCAR (aelt);
20917 mode_line_proptrans_alist
20918 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20920 else
20922 Lisp_Object tem;
20924 /* If AELT has the wrong props, it is useless.
20925 so get rid of it. */
20926 if (! NILP (aelt))
20927 mode_line_proptrans_alist
20928 = Fdelq (aelt, mode_line_proptrans_alist);
20930 elt = Fcopy_sequence (elt);
20931 Fset_text_properties (make_number (0), Flength (elt),
20932 props, elt);
20933 /* Add this item to mode_line_proptrans_alist. */
20934 mode_line_proptrans_alist
20935 = Fcons (Fcons (elt, props),
20936 mode_line_proptrans_alist);
20937 /* Truncate mode_line_proptrans_alist
20938 to at most 50 elements. */
20939 tem = Fnthcdr (make_number (50),
20940 mode_line_proptrans_alist);
20941 if (! NILP (tem))
20942 XSETCDR (tem, Qnil);
20947 offset = 0;
20949 if (literal)
20951 prec = precision - n;
20952 switch (mode_line_target)
20954 case MODE_LINE_NOPROP:
20955 case MODE_LINE_TITLE:
20956 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20957 break;
20958 case MODE_LINE_STRING:
20959 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20960 break;
20961 case MODE_LINE_DISPLAY:
20962 n += display_string (NULL, elt, Qnil, 0, 0, it,
20963 0, prec, 0, STRING_MULTIBYTE (elt));
20964 break;
20967 break;
20970 /* Handle the non-literal case. */
20972 while ((precision <= 0 || n < precision)
20973 && SREF (elt, offset) != 0
20974 && (mode_line_target != MODE_LINE_DISPLAY
20975 || it->current_x < it->last_visible_x))
20977 ptrdiff_t last_offset = offset;
20979 /* Advance to end of string or next format specifier. */
20980 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20983 if (offset - 1 != last_offset)
20985 ptrdiff_t nchars, nbytes;
20987 /* Output to end of string or up to '%'. Field width
20988 is length of string. Don't output more than
20989 PRECISION allows us. */
20990 offset--;
20992 prec = c_string_width (SDATA (elt) + last_offset,
20993 offset - last_offset, precision - n,
20994 &nchars, &nbytes);
20996 switch (mode_line_target)
20998 case MODE_LINE_NOPROP:
20999 case MODE_LINE_TITLE:
21000 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21001 break;
21002 case MODE_LINE_STRING:
21004 ptrdiff_t bytepos = last_offset;
21005 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21006 ptrdiff_t endpos = (precision <= 0
21007 ? string_byte_to_char (elt, offset)
21008 : charpos + nchars);
21010 n += store_mode_line_string (NULL,
21011 Fsubstring (elt, make_number (charpos),
21012 make_number (endpos)),
21013 0, 0, 0, Qnil);
21015 break;
21016 case MODE_LINE_DISPLAY:
21018 ptrdiff_t bytepos = last_offset;
21019 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21021 if (precision <= 0)
21022 nchars = string_byte_to_char (elt, offset) - charpos;
21023 n += display_string (NULL, elt, Qnil, 0, charpos,
21024 it, 0, nchars, 0,
21025 STRING_MULTIBYTE (elt));
21027 break;
21030 else /* c == '%' */
21032 ptrdiff_t percent_position = offset;
21034 /* Get the specified minimum width. Zero means
21035 don't pad. */
21036 field = 0;
21037 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21038 field = field * 10 + c - '0';
21040 /* Don't pad beyond the total padding allowed. */
21041 if (field_width - n > 0 && field > field_width - n)
21042 field = field_width - n;
21044 /* Note that either PRECISION <= 0 or N < PRECISION. */
21045 prec = precision - n;
21047 if (c == 'M')
21048 n += display_mode_element (it, depth, field, prec,
21049 Vglobal_mode_string, props,
21050 risky);
21051 else if (c != 0)
21053 bool multibyte;
21054 ptrdiff_t bytepos, charpos;
21055 const char *spec;
21056 Lisp_Object string;
21058 bytepos = percent_position;
21059 charpos = (STRING_MULTIBYTE (elt)
21060 ? string_byte_to_char (elt, bytepos)
21061 : bytepos);
21062 spec = decode_mode_spec (it->w, c, field, &string);
21063 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21065 switch (mode_line_target)
21067 case MODE_LINE_NOPROP:
21068 case MODE_LINE_TITLE:
21069 n += store_mode_line_noprop (spec, field, prec);
21070 break;
21071 case MODE_LINE_STRING:
21073 Lisp_Object tem = build_string (spec);
21074 props = Ftext_properties_at (make_number (charpos), elt);
21075 /* Should only keep face property in props */
21076 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21078 break;
21079 case MODE_LINE_DISPLAY:
21081 int nglyphs_before, nwritten;
21083 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21084 nwritten = display_string (spec, string, elt,
21085 charpos, 0, it,
21086 field, prec, 0,
21087 multibyte);
21089 /* Assign to the glyphs written above the
21090 string where the `%x' came from, position
21091 of the `%'. */
21092 if (nwritten > 0)
21094 struct glyph *glyph
21095 = (it->glyph_row->glyphs[TEXT_AREA]
21096 + nglyphs_before);
21097 int i;
21099 for (i = 0; i < nwritten; ++i)
21101 glyph[i].object = elt;
21102 glyph[i].charpos = charpos;
21105 n += nwritten;
21108 break;
21111 else /* c == 0 */
21112 break;
21116 break;
21118 case Lisp_Symbol:
21119 /* A symbol: process the value of the symbol recursively
21120 as if it appeared here directly. Avoid error if symbol void.
21121 Special case: if value of symbol is a string, output the string
21122 literally. */
21124 register Lisp_Object tem;
21126 /* If the variable is not marked as risky to set
21127 then its contents are risky to use. */
21128 if (NILP (Fget (elt, Qrisky_local_variable)))
21129 risky = 1;
21131 tem = Fboundp (elt);
21132 if (!NILP (tem))
21134 tem = Fsymbol_value (elt);
21135 /* If value is a string, output that string literally:
21136 don't check for % within it. */
21137 if (STRINGP (tem))
21138 literal = 1;
21140 if (!EQ (tem, elt))
21142 /* Give up right away for nil or t. */
21143 elt = tem;
21144 goto tail_recurse;
21148 break;
21150 case Lisp_Cons:
21152 register Lisp_Object car, tem;
21154 /* A cons cell: five distinct cases.
21155 If first element is :eval or :propertize, do something special.
21156 If first element is a string or a cons, process all the elements
21157 and effectively concatenate them.
21158 If first element is a negative number, truncate displaying cdr to
21159 at most that many characters. If positive, pad (with spaces)
21160 to at least that many characters.
21161 If first element is a symbol, process the cadr or caddr recursively
21162 according to whether the symbol's value is non-nil or nil. */
21163 car = XCAR (elt);
21164 if (EQ (car, QCeval))
21166 /* An element of the form (:eval FORM) means evaluate FORM
21167 and use the result as mode line elements. */
21169 if (risky)
21170 break;
21172 if (CONSP (XCDR (elt)))
21174 Lisp_Object spec;
21175 spec = safe_eval (XCAR (XCDR (elt)));
21176 n += display_mode_element (it, depth, field_width - n,
21177 precision - n, spec, props,
21178 risky);
21181 else if (EQ (car, QCpropertize))
21183 /* An element of the form (:propertize ELT PROPS...)
21184 means display ELT but applying properties PROPS. */
21186 if (risky)
21187 break;
21189 if (CONSP (XCDR (elt)))
21190 n += display_mode_element (it, depth, field_width - n,
21191 precision - n, XCAR (XCDR (elt)),
21192 XCDR (XCDR (elt)), risky);
21194 else if (SYMBOLP (car))
21196 tem = Fboundp (car);
21197 elt = XCDR (elt);
21198 if (!CONSP (elt))
21199 goto invalid;
21200 /* elt is now the cdr, and we know it is a cons cell.
21201 Use its car if CAR has a non-nil value. */
21202 if (!NILP (tem))
21204 tem = Fsymbol_value (car);
21205 if (!NILP (tem))
21207 elt = XCAR (elt);
21208 goto tail_recurse;
21211 /* Symbol's value is nil (or symbol is unbound)
21212 Get the cddr of the original list
21213 and if possible find the caddr and use that. */
21214 elt = XCDR (elt);
21215 if (NILP (elt))
21216 break;
21217 else if (!CONSP (elt))
21218 goto invalid;
21219 elt = XCAR (elt);
21220 goto tail_recurse;
21222 else if (INTEGERP (car))
21224 register int lim = XINT (car);
21225 elt = XCDR (elt);
21226 if (lim < 0)
21228 /* Negative int means reduce maximum width. */
21229 if (precision <= 0)
21230 precision = -lim;
21231 else
21232 precision = min (precision, -lim);
21234 else if (lim > 0)
21236 /* Padding specified. Don't let it be more than
21237 current maximum. */
21238 if (precision > 0)
21239 lim = min (precision, lim);
21241 /* If that's more padding than already wanted, queue it.
21242 But don't reduce padding already specified even if
21243 that is beyond the current truncation point. */
21244 field_width = max (lim, field_width);
21246 goto tail_recurse;
21248 else if (STRINGP (car) || CONSP (car))
21250 Lisp_Object halftail = elt;
21251 int len = 0;
21253 while (CONSP (elt)
21254 && (precision <= 0 || n < precision))
21256 n += display_mode_element (it, depth,
21257 /* Do padding only after the last
21258 element in the list. */
21259 (! CONSP (XCDR (elt))
21260 ? field_width - n
21261 : 0),
21262 precision - n, XCAR (elt),
21263 props, risky);
21264 elt = XCDR (elt);
21265 len++;
21266 if ((len & 1) == 0)
21267 halftail = XCDR (halftail);
21268 /* Check for cycle. */
21269 if (EQ (halftail, elt))
21270 break;
21274 break;
21276 default:
21277 invalid:
21278 elt = build_string ("*invalid*");
21279 goto tail_recurse;
21282 /* Pad to FIELD_WIDTH. */
21283 if (field_width > 0 && n < field_width)
21285 switch (mode_line_target)
21287 case MODE_LINE_NOPROP:
21288 case MODE_LINE_TITLE:
21289 n += store_mode_line_noprop ("", field_width - n, 0);
21290 break;
21291 case MODE_LINE_STRING:
21292 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21293 break;
21294 case MODE_LINE_DISPLAY:
21295 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21296 0, 0, 0);
21297 break;
21301 return n;
21304 /* Store a mode-line string element in mode_line_string_list.
21306 If STRING is non-null, display that C string. Otherwise, the Lisp
21307 string LISP_STRING is displayed.
21309 FIELD_WIDTH is the minimum number of output glyphs to produce.
21310 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21311 with spaces. FIELD_WIDTH <= 0 means don't pad.
21313 PRECISION is the maximum number of characters to output from
21314 STRING. PRECISION <= 0 means don't truncate the string.
21316 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21317 properties to the string.
21319 PROPS are the properties to add to the string.
21320 The mode_line_string_face face property is always added to the string.
21323 static int
21324 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21325 int field_width, int precision, Lisp_Object props)
21327 ptrdiff_t len;
21328 int n = 0;
21330 if (string != NULL)
21332 len = strlen (string);
21333 if (precision > 0 && len > precision)
21334 len = precision;
21335 lisp_string = make_string (string, len);
21336 if (NILP (props))
21337 props = mode_line_string_face_prop;
21338 else if (!NILP (mode_line_string_face))
21340 Lisp_Object face = Fplist_get (props, Qface);
21341 props = Fcopy_sequence (props);
21342 if (NILP (face))
21343 face = mode_line_string_face;
21344 else
21345 face = list2 (face, mode_line_string_face);
21346 props = Fplist_put (props, Qface, face);
21348 Fadd_text_properties (make_number (0), make_number (len),
21349 props, lisp_string);
21351 else
21353 len = XFASTINT (Flength (lisp_string));
21354 if (precision > 0 && len > precision)
21356 len = precision;
21357 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21358 precision = -1;
21360 if (!NILP (mode_line_string_face))
21362 Lisp_Object face;
21363 if (NILP (props))
21364 props = Ftext_properties_at (make_number (0), lisp_string);
21365 face = Fplist_get (props, Qface);
21366 if (NILP (face))
21367 face = mode_line_string_face;
21368 else
21369 face = list2 (face, mode_line_string_face);
21370 props = list2 (Qface, face);
21371 if (copy_string)
21372 lisp_string = Fcopy_sequence (lisp_string);
21374 if (!NILP (props))
21375 Fadd_text_properties (make_number (0), make_number (len),
21376 props, lisp_string);
21379 if (len > 0)
21381 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21382 n += len;
21385 if (field_width > len)
21387 field_width -= len;
21388 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21389 if (!NILP (props))
21390 Fadd_text_properties (make_number (0), make_number (field_width),
21391 props, lisp_string);
21392 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21393 n += field_width;
21396 return n;
21400 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21401 1, 4, 0,
21402 doc: /* Format a string out of a mode line format specification.
21403 First arg FORMAT specifies the mode line format (see `mode-line-format'
21404 for details) to use.
21406 By default, the format is evaluated for the currently selected window.
21408 Optional second arg FACE specifies the face property to put on all
21409 characters for which no face is specified. The value nil means the
21410 default face. The value t means whatever face the window's mode line
21411 currently uses (either `mode-line' or `mode-line-inactive',
21412 depending on whether the window is the selected window or not).
21413 An integer value means the value string has no text
21414 properties.
21416 Optional third and fourth args WINDOW and BUFFER specify the window
21417 and buffer to use as the context for the formatting (defaults
21418 are the selected window and the WINDOW's buffer). */)
21419 (Lisp_Object format, Lisp_Object face,
21420 Lisp_Object window, Lisp_Object buffer)
21422 struct it it;
21423 int len;
21424 struct window *w;
21425 struct buffer *old_buffer = NULL;
21426 int face_id;
21427 int no_props = INTEGERP (face);
21428 ptrdiff_t count = SPECPDL_INDEX ();
21429 Lisp_Object str;
21430 int string_start = 0;
21432 w = decode_any_window (window);
21433 XSETWINDOW (window, w);
21435 if (NILP (buffer))
21436 buffer = w->contents;
21437 CHECK_BUFFER (buffer);
21439 /* Make formatting the modeline a non-op when noninteractive, otherwise
21440 there will be problems later caused by a partially initialized frame. */
21441 if (NILP (format) || noninteractive)
21442 return empty_unibyte_string;
21444 if (no_props)
21445 face = Qnil;
21447 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21448 : EQ (face, Qt) ? (EQ (window, selected_window)
21449 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21450 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21451 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21452 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21453 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21454 : DEFAULT_FACE_ID;
21456 old_buffer = current_buffer;
21458 /* Save things including mode_line_proptrans_alist,
21459 and set that to nil so that we don't alter the outer value. */
21460 record_unwind_protect (unwind_format_mode_line,
21461 format_mode_line_unwind_data
21462 (XFRAME (WINDOW_FRAME (w)),
21463 old_buffer, selected_window, 1));
21464 mode_line_proptrans_alist = Qnil;
21466 Fselect_window (window, Qt);
21467 set_buffer_internal_1 (XBUFFER (buffer));
21469 init_iterator (&it, w, -1, -1, NULL, face_id);
21471 if (no_props)
21473 mode_line_target = MODE_LINE_NOPROP;
21474 mode_line_string_face_prop = Qnil;
21475 mode_line_string_list = Qnil;
21476 string_start = MODE_LINE_NOPROP_LEN (0);
21478 else
21480 mode_line_target = MODE_LINE_STRING;
21481 mode_line_string_list = Qnil;
21482 mode_line_string_face = face;
21483 mode_line_string_face_prop
21484 = NILP (face) ? Qnil : list2 (Qface, face);
21487 push_kboard (FRAME_KBOARD (it.f));
21488 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21489 pop_kboard ();
21491 if (no_props)
21493 len = MODE_LINE_NOPROP_LEN (string_start);
21494 str = make_string (mode_line_noprop_buf + string_start, len);
21496 else
21498 mode_line_string_list = Fnreverse (mode_line_string_list);
21499 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21500 empty_unibyte_string);
21503 unbind_to (count, Qnil);
21504 return str;
21507 /* Write a null-terminated, right justified decimal representation of
21508 the positive integer D to BUF using a minimal field width WIDTH. */
21510 static void
21511 pint2str (register char *buf, register int width, register ptrdiff_t d)
21513 register char *p = buf;
21515 if (d <= 0)
21516 *p++ = '0';
21517 else
21519 while (d > 0)
21521 *p++ = d % 10 + '0';
21522 d /= 10;
21526 for (width -= (int) (p - buf); width > 0; --width)
21527 *p++ = ' ';
21528 *p-- = '\0';
21529 while (p > buf)
21531 d = *buf;
21532 *buf++ = *p;
21533 *p-- = d;
21537 /* Write a null-terminated, right justified decimal and "human
21538 readable" representation of the nonnegative integer D to BUF using
21539 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21541 static const char power_letter[] =
21543 0, /* no letter */
21544 'k', /* kilo */
21545 'M', /* mega */
21546 'G', /* giga */
21547 'T', /* tera */
21548 'P', /* peta */
21549 'E', /* exa */
21550 'Z', /* zetta */
21551 'Y' /* yotta */
21554 static void
21555 pint2hrstr (char *buf, int width, ptrdiff_t d)
21557 /* We aim to represent the nonnegative integer D as
21558 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21559 ptrdiff_t quotient = d;
21560 int remainder = 0;
21561 /* -1 means: do not use TENTHS. */
21562 int tenths = -1;
21563 int exponent = 0;
21565 /* Length of QUOTIENT.TENTHS as a string. */
21566 int length;
21568 char * psuffix;
21569 char * p;
21571 if (quotient >= 1000)
21573 /* Scale to the appropriate EXPONENT. */
21576 remainder = quotient % 1000;
21577 quotient /= 1000;
21578 exponent++;
21580 while (quotient >= 1000);
21582 /* Round to nearest and decide whether to use TENTHS or not. */
21583 if (quotient <= 9)
21585 tenths = remainder / 100;
21586 if (remainder % 100 >= 50)
21588 if (tenths < 9)
21589 tenths++;
21590 else
21592 quotient++;
21593 if (quotient == 10)
21594 tenths = -1;
21595 else
21596 tenths = 0;
21600 else
21601 if (remainder >= 500)
21603 if (quotient < 999)
21604 quotient++;
21605 else
21607 quotient = 1;
21608 exponent++;
21609 tenths = 0;
21614 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21615 if (tenths == -1 && quotient <= 99)
21616 if (quotient <= 9)
21617 length = 1;
21618 else
21619 length = 2;
21620 else
21621 length = 3;
21622 p = psuffix = buf + max (width, length);
21624 /* Print EXPONENT. */
21625 *psuffix++ = power_letter[exponent];
21626 *psuffix = '\0';
21628 /* Print TENTHS. */
21629 if (tenths >= 0)
21631 *--p = '0' + tenths;
21632 *--p = '.';
21635 /* Print QUOTIENT. */
21638 int digit = quotient % 10;
21639 *--p = '0' + digit;
21641 while ((quotient /= 10) != 0);
21643 /* Print leading spaces. */
21644 while (buf < p)
21645 *--p = ' ';
21648 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21649 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21650 type of CODING_SYSTEM. Return updated pointer into BUF. */
21652 static unsigned char invalid_eol_type[] = "(*invalid*)";
21654 static char *
21655 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21657 Lisp_Object val;
21658 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21659 const unsigned char *eol_str;
21660 int eol_str_len;
21661 /* The EOL conversion we are using. */
21662 Lisp_Object eoltype;
21664 val = CODING_SYSTEM_SPEC (coding_system);
21665 eoltype = Qnil;
21667 if (!VECTORP (val)) /* Not yet decided. */
21669 *buf++ = multibyte ? '-' : ' ';
21670 if (eol_flag)
21671 eoltype = eol_mnemonic_undecided;
21672 /* Don't mention EOL conversion if it isn't decided. */
21674 else
21676 Lisp_Object attrs;
21677 Lisp_Object eolvalue;
21679 attrs = AREF (val, 0);
21680 eolvalue = AREF (val, 2);
21682 *buf++ = multibyte
21683 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21684 : ' ';
21686 if (eol_flag)
21688 /* The EOL conversion that is normal on this system. */
21690 if (NILP (eolvalue)) /* Not yet decided. */
21691 eoltype = eol_mnemonic_undecided;
21692 else if (VECTORP (eolvalue)) /* Not yet decided. */
21693 eoltype = eol_mnemonic_undecided;
21694 else /* eolvalue is Qunix, Qdos, or Qmac. */
21695 eoltype = (EQ (eolvalue, Qunix)
21696 ? eol_mnemonic_unix
21697 : (EQ (eolvalue, Qdos) == 1
21698 ? eol_mnemonic_dos : eol_mnemonic_mac));
21702 if (eol_flag)
21704 /* Mention the EOL conversion if it is not the usual one. */
21705 if (STRINGP (eoltype))
21707 eol_str = SDATA (eoltype);
21708 eol_str_len = SBYTES (eoltype);
21710 else if (CHARACTERP (eoltype))
21712 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21713 int c = XFASTINT (eoltype);
21714 eol_str_len = CHAR_STRING (c, tmp);
21715 eol_str = tmp;
21717 else
21719 eol_str = invalid_eol_type;
21720 eol_str_len = sizeof (invalid_eol_type) - 1;
21722 memcpy (buf, eol_str, eol_str_len);
21723 buf += eol_str_len;
21726 return buf;
21729 /* Return a string for the output of a mode line %-spec for window W,
21730 generated by character C. FIELD_WIDTH > 0 means pad the string
21731 returned with spaces to that value. Return a Lisp string in
21732 *STRING if the resulting string is taken from that Lisp string.
21734 Note we operate on the current buffer for most purposes. */
21736 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21738 static const char *
21739 decode_mode_spec (struct window *w, register int c, int field_width,
21740 Lisp_Object *string)
21742 Lisp_Object obj;
21743 struct frame *f = XFRAME (WINDOW_FRAME (w));
21744 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21745 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21746 produce strings from numerical values, so limit preposterously
21747 large values of FIELD_WIDTH to avoid overrunning the buffer's
21748 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21749 bytes plus the terminating null. */
21750 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21751 struct buffer *b = current_buffer;
21753 obj = Qnil;
21754 *string = Qnil;
21756 switch (c)
21758 case '*':
21759 if (!NILP (BVAR (b, read_only)))
21760 return "%";
21761 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21762 return "*";
21763 return "-";
21765 case '+':
21766 /* This differs from %* only for a modified read-only buffer. */
21767 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21768 return "*";
21769 if (!NILP (BVAR (b, read_only)))
21770 return "%";
21771 return "-";
21773 case '&':
21774 /* This differs from %* in ignoring read-only-ness. */
21775 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21776 return "*";
21777 return "-";
21779 case '%':
21780 return "%";
21782 case '[':
21784 int i;
21785 char *p;
21787 if (command_loop_level > 5)
21788 return "[[[... ";
21789 p = decode_mode_spec_buf;
21790 for (i = 0; i < command_loop_level; i++)
21791 *p++ = '[';
21792 *p = 0;
21793 return decode_mode_spec_buf;
21796 case ']':
21798 int i;
21799 char *p;
21801 if (command_loop_level > 5)
21802 return " ...]]]";
21803 p = decode_mode_spec_buf;
21804 for (i = 0; i < command_loop_level; i++)
21805 *p++ = ']';
21806 *p = 0;
21807 return decode_mode_spec_buf;
21810 case '-':
21812 register int i;
21814 /* Let lots_of_dashes be a string of infinite length. */
21815 if (mode_line_target == MODE_LINE_NOPROP
21816 || mode_line_target == MODE_LINE_STRING)
21817 return "--";
21818 if (field_width <= 0
21819 || field_width > sizeof (lots_of_dashes))
21821 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21822 decode_mode_spec_buf[i] = '-';
21823 decode_mode_spec_buf[i] = '\0';
21824 return decode_mode_spec_buf;
21826 else
21827 return lots_of_dashes;
21830 case 'b':
21831 obj = BVAR (b, name);
21832 break;
21834 case 'c':
21835 /* %c and %l are ignored in `frame-title-format'.
21836 (In redisplay_internal, the frame title is drawn _before_ the
21837 windows are updated, so the stuff which depends on actual
21838 window contents (such as %l) may fail to render properly, or
21839 even crash emacs.) */
21840 if (mode_line_target == MODE_LINE_TITLE)
21841 return "";
21842 else
21844 ptrdiff_t col = current_column ();
21845 w->column_number_displayed = col;
21846 pint2str (decode_mode_spec_buf, width, col);
21847 return decode_mode_spec_buf;
21850 case 'e':
21851 #ifndef SYSTEM_MALLOC
21853 if (NILP (Vmemory_full))
21854 return "";
21855 else
21856 return "!MEM FULL! ";
21858 #else
21859 return "";
21860 #endif
21862 case 'F':
21863 /* %F displays the frame name. */
21864 if (!NILP (f->title))
21865 return SSDATA (f->title);
21866 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21867 return SSDATA (f->name);
21868 return "Emacs";
21870 case 'f':
21871 obj = BVAR (b, filename);
21872 break;
21874 case 'i':
21876 ptrdiff_t size = ZV - BEGV;
21877 pint2str (decode_mode_spec_buf, width, size);
21878 return decode_mode_spec_buf;
21881 case 'I':
21883 ptrdiff_t size = ZV - BEGV;
21884 pint2hrstr (decode_mode_spec_buf, width, size);
21885 return decode_mode_spec_buf;
21888 case 'l':
21890 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21891 ptrdiff_t topline, nlines, height;
21892 ptrdiff_t junk;
21894 /* %c and %l are ignored in `frame-title-format'. */
21895 if (mode_line_target == MODE_LINE_TITLE)
21896 return "";
21898 startpos = marker_position (w->start);
21899 startpos_byte = marker_byte_position (w->start);
21900 height = WINDOW_TOTAL_LINES (w);
21902 /* If we decided that this buffer isn't suitable for line numbers,
21903 don't forget that too fast. */
21904 if (w->base_line_pos == -1)
21905 goto no_value;
21907 /* If the buffer is very big, don't waste time. */
21908 if (INTEGERP (Vline_number_display_limit)
21909 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21911 w->base_line_pos = 0;
21912 w->base_line_number = 0;
21913 goto no_value;
21916 if (w->base_line_number > 0
21917 && w->base_line_pos > 0
21918 && w->base_line_pos <= startpos)
21920 line = w->base_line_number;
21921 linepos = w->base_line_pos;
21922 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21924 else
21926 line = 1;
21927 linepos = BUF_BEGV (b);
21928 linepos_byte = BUF_BEGV_BYTE (b);
21931 /* Count lines from base line to window start position. */
21932 nlines = display_count_lines (linepos_byte,
21933 startpos_byte,
21934 startpos, &junk);
21936 topline = nlines + line;
21938 /* Determine a new base line, if the old one is too close
21939 or too far away, or if we did not have one.
21940 "Too close" means it's plausible a scroll-down would
21941 go back past it. */
21942 if (startpos == BUF_BEGV (b))
21944 w->base_line_number = topline;
21945 w->base_line_pos = BUF_BEGV (b);
21947 else if (nlines < height + 25 || nlines > height * 3 + 50
21948 || linepos == BUF_BEGV (b))
21950 ptrdiff_t limit = BUF_BEGV (b);
21951 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21952 ptrdiff_t position;
21953 ptrdiff_t distance =
21954 (height * 2 + 30) * line_number_display_limit_width;
21956 if (startpos - distance > limit)
21958 limit = startpos - distance;
21959 limit_byte = CHAR_TO_BYTE (limit);
21962 nlines = display_count_lines (startpos_byte,
21963 limit_byte,
21964 - (height * 2 + 30),
21965 &position);
21966 /* If we couldn't find the lines we wanted within
21967 line_number_display_limit_width chars per line,
21968 give up on line numbers for this window. */
21969 if (position == limit_byte && limit == startpos - distance)
21971 w->base_line_pos = -1;
21972 w->base_line_number = 0;
21973 goto no_value;
21976 w->base_line_number = topline - nlines;
21977 w->base_line_pos = BYTE_TO_CHAR (position);
21980 /* Now count lines from the start pos to point. */
21981 nlines = display_count_lines (startpos_byte,
21982 PT_BYTE, PT, &junk);
21984 /* Record that we did display the line number. */
21985 line_number_displayed = 1;
21987 /* Make the string to show. */
21988 pint2str (decode_mode_spec_buf, width, topline + nlines);
21989 return decode_mode_spec_buf;
21990 no_value:
21992 char* p = decode_mode_spec_buf;
21993 int pad = width - 2;
21994 while (pad-- > 0)
21995 *p++ = ' ';
21996 *p++ = '?';
21997 *p++ = '?';
21998 *p = '\0';
21999 return decode_mode_spec_buf;
22002 break;
22004 case 'm':
22005 obj = BVAR (b, mode_name);
22006 break;
22008 case 'n':
22009 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22010 return " Narrow";
22011 break;
22013 case 'p':
22015 ptrdiff_t pos = marker_position (w->start);
22016 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22018 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22020 if (pos <= BUF_BEGV (b))
22021 return "All";
22022 else
22023 return "Bottom";
22025 else if (pos <= BUF_BEGV (b))
22026 return "Top";
22027 else
22029 if (total > 1000000)
22030 /* Do it differently for a large value, to avoid overflow. */
22031 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22032 else
22033 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22034 /* We can't normally display a 3-digit number,
22035 so get us a 2-digit number that is close. */
22036 if (total == 100)
22037 total = 99;
22038 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22039 return decode_mode_spec_buf;
22043 /* Display percentage of size above the bottom of the screen. */
22044 case 'P':
22046 ptrdiff_t toppos = marker_position (w->start);
22047 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22048 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22050 if (botpos >= BUF_ZV (b))
22052 if (toppos <= BUF_BEGV (b))
22053 return "All";
22054 else
22055 return "Bottom";
22057 else
22059 if (total > 1000000)
22060 /* Do it differently for a large value, to avoid overflow. */
22061 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22062 else
22063 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22064 /* We can't normally display a 3-digit number,
22065 so get us a 2-digit number that is close. */
22066 if (total == 100)
22067 total = 99;
22068 if (toppos <= BUF_BEGV (b))
22069 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22070 else
22071 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22072 return decode_mode_spec_buf;
22076 case 's':
22077 /* status of process */
22078 obj = Fget_buffer_process (Fcurrent_buffer ());
22079 if (NILP (obj))
22080 return "no process";
22081 #ifndef MSDOS
22082 obj = Fsymbol_name (Fprocess_status (obj));
22083 #endif
22084 break;
22086 case '@':
22088 ptrdiff_t count = inhibit_garbage_collection ();
22089 Lisp_Object val = call1 (intern ("file-remote-p"),
22090 BVAR (current_buffer, directory));
22091 unbind_to (count, Qnil);
22093 if (NILP (val))
22094 return "-";
22095 else
22096 return "@";
22099 case 'z':
22100 /* coding-system (not including end-of-line format) */
22101 case 'Z':
22102 /* coding-system (including end-of-line type) */
22104 int eol_flag = (c == 'Z');
22105 char *p = decode_mode_spec_buf;
22107 if (! FRAME_WINDOW_P (f))
22109 /* No need to mention EOL here--the terminal never needs
22110 to do EOL conversion. */
22111 p = decode_mode_spec_coding (CODING_ID_NAME
22112 (FRAME_KEYBOARD_CODING (f)->id),
22113 p, 0);
22114 p = decode_mode_spec_coding (CODING_ID_NAME
22115 (FRAME_TERMINAL_CODING (f)->id),
22116 p, 0);
22118 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22119 p, eol_flag);
22121 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22122 #ifdef subprocesses
22123 obj = Fget_buffer_process (Fcurrent_buffer ());
22124 if (PROCESSP (obj))
22126 p = decode_mode_spec_coding
22127 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22128 p = decode_mode_spec_coding
22129 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22131 #endif /* subprocesses */
22132 #endif /* 0 */
22133 *p = 0;
22134 return decode_mode_spec_buf;
22138 if (STRINGP (obj))
22140 *string = obj;
22141 return SSDATA (obj);
22143 else
22144 return "";
22148 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22149 means count lines back from START_BYTE. But don't go beyond
22150 LIMIT_BYTE. Return the number of lines thus found (always
22151 nonnegative).
22153 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22154 either the position COUNT lines after/before START_BYTE, if we
22155 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22156 COUNT lines. */
22158 static ptrdiff_t
22159 display_count_lines (ptrdiff_t start_byte,
22160 ptrdiff_t limit_byte, ptrdiff_t count,
22161 ptrdiff_t *byte_pos_ptr)
22163 register unsigned char *cursor;
22164 unsigned char *base;
22166 register ptrdiff_t ceiling;
22167 register unsigned char *ceiling_addr;
22168 ptrdiff_t orig_count = count;
22170 /* If we are not in selective display mode,
22171 check only for newlines. */
22172 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22173 && !INTEGERP (BVAR (current_buffer, selective_display)));
22175 if (count > 0)
22177 while (start_byte < limit_byte)
22179 ceiling = BUFFER_CEILING_OF (start_byte);
22180 ceiling = min (limit_byte - 1, ceiling);
22181 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22182 base = (cursor = BYTE_POS_ADDR (start_byte));
22186 if (selective_display)
22188 while (*cursor != '\n' && *cursor != 015
22189 && ++cursor != ceiling_addr)
22190 continue;
22191 if (cursor == ceiling_addr)
22192 break;
22194 else
22196 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22197 if (! cursor)
22198 break;
22201 cursor++;
22203 if (--count == 0)
22205 start_byte += cursor - base;
22206 *byte_pos_ptr = start_byte;
22207 return orig_count;
22210 while (cursor < ceiling_addr);
22212 start_byte += ceiling_addr - base;
22215 else
22217 while (start_byte > limit_byte)
22219 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22220 ceiling = max (limit_byte, ceiling);
22221 ceiling_addr = BYTE_POS_ADDR (ceiling);
22222 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22223 while (1)
22225 if (selective_display)
22227 while (--cursor >= ceiling_addr
22228 && *cursor != '\n' && *cursor != 015)
22229 continue;
22230 if (cursor < ceiling_addr)
22231 break;
22233 else
22235 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22236 if (! cursor)
22237 break;
22240 if (++count == 0)
22242 start_byte += cursor - base + 1;
22243 *byte_pos_ptr = start_byte;
22244 /* When scanning backwards, we should
22245 not count the newline posterior to which we stop. */
22246 return - orig_count - 1;
22249 start_byte += ceiling_addr - base;
22253 *byte_pos_ptr = limit_byte;
22255 if (count < 0)
22256 return - orig_count + count;
22257 return orig_count - count;
22263 /***********************************************************************
22264 Displaying strings
22265 ***********************************************************************/
22267 /* Display a NUL-terminated string, starting with index START.
22269 If STRING is non-null, display that C string. Otherwise, the Lisp
22270 string LISP_STRING is displayed. There's a case that STRING is
22271 non-null and LISP_STRING is not nil. It means STRING is a string
22272 data of LISP_STRING. In that case, we display LISP_STRING while
22273 ignoring its text properties.
22275 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22276 FACE_STRING. Display STRING or LISP_STRING with the face at
22277 FACE_STRING_POS in FACE_STRING:
22279 Display the string in the environment given by IT, but use the
22280 standard display table, temporarily.
22282 FIELD_WIDTH is the minimum number of output glyphs to produce.
22283 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22284 with spaces. If STRING has more characters, more than FIELD_WIDTH
22285 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22287 PRECISION is the maximum number of characters to output from
22288 STRING. PRECISION < 0 means don't truncate the string.
22290 This is roughly equivalent to printf format specifiers:
22292 FIELD_WIDTH PRECISION PRINTF
22293 ----------------------------------------
22294 -1 -1 %s
22295 -1 10 %.10s
22296 10 -1 %10s
22297 20 10 %20.10s
22299 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22300 display them, and < 0 means obey the current buffer's value of
22301 enable_multibyte_characters.
22303 Value is the number of columns displayed. */
22305 static int
22306 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22307 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22308 int field_width, int precision, int max_x, int multibyte)
22310 int hpos_at_start = it->hpos;
22311 int saved_face_id = it->face_id;
22312 struct glyph_row *row = it->glyph_row;
22313 ptrdiff_t it_charpos;
22315 /* Initialize the iterator IT for iteration over STRING beginning
22316 with index START. */
22317 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22318 precision, field_width, multibyte);
22319 if (string && STRINGP (lisp_string))
22320 /* LISP_STRING is the one returned by decode_mode_spec. We should
22321 ignore its text properties. */
22322 it->stop_charpos = it->end_charpos;
22324 /* If displaying STRING, set up the face of the iterator from
22325 FACE_STRING, if that's given. */
22326 if (STRINGP (face_string))
22328 ptrdiff_t endptr;
22329 struct face *face;
22331 it->face_id
22332 = face_at_string_position (it->w, face_string, face_string_pos,
22333 0, it->region_beg_charpos,
22334 it->region_end_charpos,
22335 &endptr, it->base_face_id, 0);
22336 face = FACE_FROM_ID (it->f, it->face_id);
22337 it->face_box_p = face->box != FACE_NO_BOX;
22340 /* Set max_x to the maximum allowed X position. Don't let it go
22341 beyond the right edge of the window. */
22342 if (max_x <= 0)
22343 max_x = it->last_visible_x;
22344 else
22345 max_x = min (max_x, it->last_visible_x);
22347 /* Skip over display elements that are not visible. because IT->w is
22348 hscrolled. */
22349 if (it->current_x < it->first_visible_x)
22350 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22351 MOVE_TO_POS | MOVE_TO_X);
22353 row->ascent = it->max_ascent;
22354 row->height = it->max_ascent + it->max_descent;
22355 row->phys_ascent = it->max_phys_ascent;
22356 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22357 row->extra_line_spacing = it->max_extra_line_spacing;
22359 if (STRINGP (it->string))
22360 it_charpos = IT_STRING_CHARPOS (*it);
22361 else
22362 it_charpos = IT_CHARPOS (*it);
22364 /* This condition is for the case that we are called with current_x
22365 past last_visible_x. */
22366 while (it->current_x < max_x)
22368 int x_before, x, n_glyphs_before, i, nglyphs;
22370 /* Get the next display element. */
22371 if (!get_next_display_element (it))
22372 break;
22374 /* Produce glyphs. */
22375 x_before = it->current_x;
22376 n_glyphs_before = row->used[TEXT_AREA];
22377 PRODUCE_GLYPHS (it);
22379 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22380 i = 0;
22381 x = x_before;
22382 while (i < nglyphs)
22384 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22386 if (it->line_wrap != TRUNCATE
22387 && x + glyph->pixel_width > max_x)
22389 /* End of continued line or max_x reached. */
22390 if (CHAR_GLYPH_PADDING_P (*glyph))
22392 /* A wide character is unbreakable. */
22393 if (row->reversed_p)
22394 unproduce_glyphs (it, row->used[TEXT_AREA]
22395 - n_glyphs_before);
22396 row->used[TEXT_AREA] = n_glyphs_before;
22397 it->current_x = x_before;
22399 else
22401 if (row->reversed_p)
22402 unproduce_glyphs (it, row->used[TEXT_AREA]
22403 - (n_glyphs_before + i));
22404 row->used[TEXT_AREA] = n_glyphs_before + i;
22405 it->current_x = x;
22407 break;
22409 else if (x + glyph->pixel_width >= it->first_visible_x)
22411 /* Glyph is at least partially visible. */
22412 ++it->hpos;
22413 if (x < it->first_visible_x)
22414 row->x = x - it->first_visible_x;
22416 else
22418 /* Glyph is off the left margin of the display area.
22419 Should not happen. */
22420 emacs_abort ();
22423 row->ascent = max (row->ascent, it->max_ascent);
22424 row->height = max (row->height, it->max_ascent + it->max_descent);
22425 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22426 row->phys_height = max (row->phys_height,
22427 it->max_phys_ascent + it->max_phys_descent);
22428 row->extra_line_spacing = max (row->extra_line_spacing,
22429 it->max_extra_line_spacing);
22430 x += glyph->pixel_width;
22431 ++i;
22434 /* Stop if max_x reached. */
22435 if (i < nglyphs)
22436 break;
22438 /* Stop at line ends. */
22439 if (ITERATOR_AT_END_OF_LINE_P (it))
22441 it->continuation_lines_width = 0;
22442 break;
22445 set_iterator_to_next (it, 1);
22446 if (STRINGP (it->string))
22447 it_charpos = IT_STRING_CHARPOS (*it);
22448 else
22449 it_charpos = IT_CHARPOS (*it);
22451 /* Stop if truncating at the right edge. */
22452 if (it->line_wrap == TRUNCATE
22453 && it->current_x >= it->last_visible_x)
22455 /* Add truncation mark, but don't do it if the line is
22456 truncated at a padding space. */
22457 if (it_charpos < it->string_nchars)
22459 if (!FRAME_WINDOW_P (it->f))
22461 int ii, n;
22463 if (it->current_x > it->last_visible_x)
22465 if (!row->reversed_p)
22467 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22468 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22469 break;
22471 else
22473 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22474 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22475 break;
22476 unproduce_glyphs (it, ii + 1);
22477 ii = row->used[TEXT_AREA] - (ii + 1);
22479 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22481 row->used[TEXT_AREA] = ii;
22482 produce_special_glyphs (it, IT_TRUNCATION);
22485 produce_special_glyphs (it, IT_TRUNCATION);
22487 row->truncated_on_right_p = 1;
22489 break;
22493 /* Maybe insert a truncation at the left. */
22494 if (it->first_visible_x
22495 && it_charpos > 0)
22497 if (!FRAME_WINDOW_P (it->f)
22498 || (row->reversed_p
22499 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22500 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22501 insert_left_trunc_glyphs (it);
22502 row->truncated_on_left_p = 1;
22505 it->face_id = saved_face_id;
22507 /* Value is number of columns displayed. */
22508 return it->hpos - hpos_at_start;
22513 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22514 appears as an element of LIST or as the car of an element of LIST.
22515 If PROPVAL is a list, compare each element against LIST in that
22516 way, and return 1/2 if any element of PROPVAL is found in LIST.
22517 Otherwise return 0. This function cannot quit.
22518 The return value is 2 if the text is invisible but with an ellipsis
22519 and 1 if it's invisible and without an ellipsis. */
22522 invisible_p (register Lisp_Object propval, Lisp_Object list)
22524 register Lisp_Object tail, proptail;
22526 for (tail = list; CONSP (tail); tail = XCDR (tail))
22528 register Lisp_Object tem;
22529 tem = XCAR (tail);
22530 if (EQ (propval, tem))
22531 return 1;
22532 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22533 return NILP (XCDR (tem)) ? 1 : 2;
22536 if (CONSP (propval))
22538 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22540 Lisp_Object propelt;
22541 propelt = XCAR (proptail);
22542 for (tail = list; CONSP (tail); tail = XCDR (tail))
22544 register Lisp_Object tem;
22545 tem = XCAR (tail);
22546 if (EQ (propelt, tem))
22547 return 1;
22548 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22549 return NILP (XCDR (tem)) ? 1 : 2;
22554 return 0;
22557 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22558 doc: /* Non-nil if the property makes the text invisible.
22559 POS-OR-PROP can be a marker or number, in which case it is taken to be
22560 a position in the current buffer and the value of the `invisible' property
22561 is checked; or it can be some other value, which is then presumed to be the
22562 value of the `invisible' property of the text of interest.
22563 The non-nil value returned can be t for truly invisible text or something
22564 else if the text is replaced by an ellipsis. */)
22565 (Lisp_Object pos_or_prop)
22567 Lisp_Object prop
22568 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22569 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22570 : pos_or_prop);
22571 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22572 return (invis == 0 ? Qnil
22573 : invis == 1 ? Qt
22574 : make_number (invis));
22577 /* Calculate a width or height in pixels from a specification using
22578 the following elements:
22580 SPEC ::=
22581 NUM - a (fractional) multiple of the default font width/height
22582 (NUM) - specifies exactly NUM pixels
22583 UNIT - a fixed number of pixels, see below.
22584 ELEMENT - size of a display element in pixels, see below.
22585 (NUM . SPEC) - equals NUM * SPEC
22586 (+ SPEC SPEC ...) - add pixel values
22587 (- SPEC SPEC ...) - subtract pixel values
22588 (- SPEC) - negate pixel value
22590 NUM ::=
22591 INT or FLOAT - a number constant
22592 SYMBOL - use symbol's (buffer local) variable binding.
22594 UNIT ::=
22595 in - pixels per inch *)
22596 mm - pixels per 1/1000 meter *)
22597 cm - pixels per 1/100 meter *)
22598 width - width of current font in pixels.
22599 height - height of current font in pixels.
22601 *) using the ratio(s) defined in display-pixels-per-inch.
22603 ELEMENT ::=
22605 left-fringe - left fringe width in pixels
22606 right-fringe - right fringe width in pixels
22608 left-margin - left margin width in pixels
22609 right-margin - right margin width in pixels
22611 scroll-bar - scroll-bar area width in pixels
22613 Examples:
22615 Pixels corresponding to 5 inches:
22616 (5 . in)
22618 Total width of non-text areas on left side of window (if scroll-bar is on left):
22619 '(space :width (+ left-fringe left-margin scroll-bar))
22621 Align to first text column (in header line):
22622 '(space :align-to 0)
22624 Align to middle of text area minus half the width of variable `my-image'
22625 containing a loaded image:
22626 '(space :align-to (0.5 . (- text my-image)))
22628 Width of left margin minus width of 1 character in the default font:
22629 '(space :width (- left-margin 1))
22631 Width of left margin minus width of 2 characters in the current font:
22632 '(space :width (- left-margin (2 . width)))
22634 Center 1 character over left-margin (in header line):
22635 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22637 Different ways to express width of left fringe plus left margin minus one pixel:
22638 '(space :width (- (+ left-fringe left-margin) (1)))
22639 '(space :width (+ left-fringe left-margin (- (1))))
22640 '(space :width (+ left-fringe left-margin (-1)))
22644 static int
22645 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22646 struct font *font, int width_p, int *align_to)
22648 double pixels;
22650 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22651 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22653 if (NILP (prop))
22654 return OK_PIXELS (0);
22656 eassert (FRAME_LIVE_P (it->f));
22658 if (SYMBOLP (prop))
22660 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22662 char *unit = SSDATA (SYMBOL_NAME (prop));
22664 if (unit[0] == 'i' && unit[1] == 'n')
22665 pixels = 1.0;
22666 else if (unit[0] == 'm' && unit[1] == 'm')
22667 pixels = 25.4;
22668 else if (unit[0] == 'c' && unit[1] == 'm')
22669 pixels = 2.54;
22670 else
22671 pixels = 0;
22672 if (pixels > 0)
22674 double ppi = (width_p ? FRAME_RES_X (it->f)
22675 : FRAME_RES_Y (it->f));
22677 if (ppi > 0)
22678 return OK_PIXELS (ppi / pixels);
22679 return 0;
22683 #ifdef HAVE_WINDOW_SYSTEM
22684 if (EQ (prop, Qheight))
22685 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22686 if (EQ (prop, Qwidth))
22687 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22688 #else
22689 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22690 return OK_PIXELS (1);
22691 #endif
22693 if (EQ (prop, Qtext))
22694 return OK_PIXELS (width_p
22695 ? window_box_width (it->w, TEXT_AREA)
22696 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22698 if (align_to && *align_to < 0)
22700 *res = 0;
22701 if (EQ (prop, Qleft))
22702 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22703 if (EQ (prop, Qright))
22704 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22705 if (EQ (prop, Qcenter))
22706 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22707 + window_box_width (it->w, TEXT_AREA) / 2);
22708 if (EQ (prop, Qleft_fringe))
22709 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22710 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22711 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22712 if (EQ (prop, Qright_fringe))
22713 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22714 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22715 : window_box_right_offset (it->w, TEXT_AREA));
22716 if (EQ (prop, Qleft_margin))
22717 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22718 if (EQ (prop, Qright_margin))
22719 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22720 if (EQ (prop, Qscroll_bar))
22721 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22723 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22724 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22725 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22726 : 0)));
22728 else
22730 if (EQ (prop, Qleft_fringe))
22731 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22732 if (EQ (prop, Qright_fringe))
22733 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22734 if (EQ (prop, Qleft_margin))
22735 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22736 if (EQ (prop, Qright_margin))
22737 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22738 if (EQ (prop, Qscroll_bar))
22739 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22742 prop = buffer_local_value_1 (prop, it->w->contents);
22743 if (EQ (prop, Qunbound))
22744 prop = Qnil;
22747 if (INTEGERP (prop) || FLOATP (prop))
22749 int base_unit = (width_p
22750 ? FRAME_COLUMN_WIDTH (it->f)
22751 : FRAME_LINE_HEIGHT (it->f));
22752 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22755 if (CONSP (prop))
22757 Lisp_Object car = XCAR (prop);
22758 Lisp_Object cdr = XCDR (prop);
22760 if (SYMBOLP (car))
22762 #ifdef HAVE_WINDOW_SYSTEM
22763 if (FRAME_WINDOW_P (it->f)
22764 && valid_image_p (prop))
22766 ptrdiff_t id = lookup_image (it->f, prop);
22767 struct image *img = IMAGE_FROM_ID (it->f, id);
22769 return OK_PIXELS (width_p ? img->width : img->height);
22771 #endif
22772 if (EQ (car, Qplus) || EQ (car, Qminus))
22774 int first = 1;
22775 double px;
22777 pixels = 0;
22778 while (CONSP (cdr))
22780 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22781 font, width_p, align_to))
22782 return 0;
22783 if (first)
22784 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22785 else
22786 pixels += px;
22787 cdr = XCDR (cdr);
22789 if (EQ (car, Qminus))
22790 pixels = -pixels;
22791 return OK_PIXELS (pixels);
22794 car = buffer_local_value_1 (car, it->w->contents);
22795 if (EQ (car, Qunbound))
22796 car = Qnil;
22799 if (INTEGERP (car) || FLOATP (car))
22801 double fact;
22802 pixels = XFLOATINT (car);
22803 if (NILP (cdr))
22804 return OK_PIXELS (pixels);
22805 if (calc_pixel_width_or_height (&fact, it, cdr,
22806 font, width_p, align_to))
22807 return OK_PIXELS (pixels * fact);
22808 return 0;
22811 return 0;
22814 return 0;
22818 /***********************************************************************
22819 Glyph Display
22820 ***********************************************************************/
22822 #ifdef HAVE_WINDOW_SYSTEM
22824 #ifdef GLYPH_DEBUG
22826 void
22827 dump_glyph_string (struct glyph_string *s)
22829 fprintf (stderr, "glyph string\n");
22830 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22831 s->x, s->y, s->width, s->height);
22832 fprintf (stderr, " ybase = %d\n", s->ybase);
22833 fprintf (stderr, " hl = %d\n", s->hl);
22834 fprintf (stderr, " left overhang = %d, right = %d\n",
22835 s->left_overhang, s->right_overhang);
22836 fprintf (stderr, " nchars = %d\n", s->nchars);
22837 fprintf (stderr, " extends to end of line = %d\n",
22838 s->extends_to_end_of_line_p);
22839 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22840 fprintf (stderr, " bg width = %d\n", s->background_width);
22843 #endif /* GLYPH_DEBUG */
22845 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22846 of XChar2b structures for S; it can't be allocated in
22847 init_glyph_string because it must be allocated via `alloca'. W
22848 is the window on which S is drawn. ROW and AREA are the glyph row
22849 and area within the row from which S is constructed. START is the
22850 index of the first glyph structure covered by S. HL is a
22851 face-override for drawing S. */
22853 #ifdef HAVE_NTGUI
22854 #define OPTIONAL_HDC(hdc) HDC hdc,
22855 #define DECLARE_HDC(hdc) HDC hdc;
22856 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22857 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22858 #endif
22860 #ifndef OPTIONAL_HDC
22861 #define OPTIONAL_HDC(hdc)
22862 #define DECLARE_HDC(hdc)
22863 #define ALLOCATE_HDC(hdc, f)
22864 #define RELEASE_HDC(hdc, f)
22865 #endif
22867 static void
22868 init_glyph_string (struct glyph_string *s,
22869 OPTIONAL_HDC (hdc)
22870 XChar2b *char2b, struct window *w, struct glyph_row *row,
22871 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22873 memset (s, 0, sizeof *s);
22874 s->w = w;
22875 s->f = XFRAME (w->frame);
22876 #ifdef HAVE_NTGUI
22877 s->hdc = hdc;
22878 #endif
22879 s->display = FRAME_X_DISPLAY (s->f);
22880 s->window = FRAME_X_WINDOW (s->f);
22881 s->char2b = char2b;
22882 s->hl = hl;
22883 s->row = row;
22884 s->area = area;
22885 s->first_glyph = row->glyphs[area] + start;
22886 s->height = row->height;
22887 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22888 s->ybase = s->y + row->ascent;
22892 /* Append the list of glyph strings with head H and tail T to the list
22893 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22895 static void
22896 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22897 struct glyph_string *h, struct glyph_string *t)
22899 if (h)
22901 if (*head)
22902 (*tail)->next = h;
22903 else
22904 *head = h;
22905 h->prev = *tail;
22906 *tail = t;
22911 /* Prepend the list of glyph strings with head H and tail T to the
22912 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22913 result. */
22915 static void
22916 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22917 struct glyph_string *h, struct glyph_string *t)
22919 if (h)
22921 if (*head)
22922 (*head)->prev = t;
22923 else
22924 *tail = t;
22925 t->next = *head;
22926 *head = h;
22931 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22932 Set *HEAD and *TAIL to the resulting list. */
22934 static void
22935 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22936 struct glyph_string *s)
22938 s->next = s->prev = NULL;
22939 append_glyph_string_lists (head, tail, s, s);
22943 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22944 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22945 make sure that X resources for the face returned are allocated.
22946 Value is a pointer to a realized face that is ready for display if
22947 DISPLAY_P is non-zero. */
22949 static struct face *
22950 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22951 XChar2b *char2b, int display_p)
22953 struct face *face = FACE_FROM_ID (f, face_id);
22954 unsigned code = 0;
22956 if (face->font)
22958 code = face->font->driver->encode_char (face->font, c);
22960 if (code == FONT_INVALID_CODE)
22961 code = 0;
22963 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22965 /* Make sure X resources of the face are allocated. */
22966 #ifdef HAVE_X_WINDOWS
22967 if (display_p)
22968 #endif
22970 eassert (face != NULL);
22971 PREPARE_FACE_FOR_DISPLAY (f, face);
22974 return face;
22978 /* Get face and two-byte form of character glyph GLYPH on frame F.
22979 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22980 a pointer to a realized face that is ready for display. */
22982 static struct face *
22983 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22984 XChar2b *char2b, int *two_byte_p)
22986 struct face *face;
22987 unsigned code = 0;
22989 eassert (glyph->type == CHAR_GLYPH);
22990 face = FACE_FROM_ID (f, glyph->face_id);
22992 /* Make sure X resources of the face are allocated. */
22993 eassert (face != NULL);
22994 PREPARE_FACE_FOR_DISPLAY (f, face);
22996 if (two_byte_p)
22997 *two_byte_p = 0;
22999 if (face->font)
23001 if (CHAR_BYTE8_P (glyph->u.ch))
23002 code = CHAR_TO_BYTE8 (glyph->u.ch);
23003 else
23004 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23006 if (code == FONT_INVALID_CODE)
23007 code = 0;
23010 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23011 return face;
23015 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23016 Return 1 if FONT has a glyph for C, otherwise return 0. */
23018 static int
23019 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23021 unsigned code;
23023 if (CHAR_BYTE8_P (c))
23024 code = CHAR_TO_BYTE8 (c);
23025 else
23026 code = font->driver->encode_char (font, c);
23028 if (code == FONT_INVALID_CODE)
23029 return 0;
23030 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23031 return 1;
23035 /* Fill glyph string S with composition components specified by S->cmp.
23037 BASE_FACE is the base face of the composition.
23038 S->cmp_from is the index of the first component for S.
23040 OVERLAPS non-zero means S should draw the foreground only, and use
23041 its physical height for clipping. See also draw_glyphs.
23043 Value is the index of a component not in S. */
23045 static int
23046 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23047 int overlaps)
23049 int i;
23050 /* For all glyphs of this composition, starting at the offset
23051 S->cmp_from, until we reach the end of the definition or encounter a
23052 glyph that requires the different face, add it to S. */
23053 struct face *face;
23055 eassert (s);
23057 s->for_overlaps = overlaps;
23058 s->face = NULL;
23059 s->font = NULL;
23060 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23062 int c = COMPOSITION_GLYPH (s->cmp, i);
23064 /* TAB in a composition means display glyphs with padding space
23065 on the left or right. */
23066 if (c != '\t')
23068 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23069 -1, Qnil);
23071 face = get_char_face_and_encoding (s->f, c, face_id,
23072 s->char2b + i, 1);
23073 if (face)
23075 if (! s->face)
23077 s->face = face;
23078 s->font = s->face->font;
23080 else if (s->face != face)
23081 break;
23084 ++s->nchars;
23086 s->cmp_to = i;
23088 if (s->face == NULL)
23090 s->face = base_face->ascii_face;
23091 s->font = s->face->font;
23094 /* All glyph strings for the same composition has the same width,
23095 i.e. the width set for the first component of the composition. */
23096 s->width = s->first_glyph->pixel_width;
23098 /* If the specified font could not be loaded, use the frame's
23099 default font, but record the fact that we couldn't load it in
23100 the glyph string so that we can draw rectangles for the
23101 characters of the glyph string. */
23102 if (s->font == NULL)
23104 s->font_not_found_p = 1;
23105 s->font = FRAME_FONT (s->f);
23108 /* Adjust base line for subscript/superscript text. */
23109 s->ybase += s->first_glyph->voffset;
23111 /* This glyph string must always be drawn with 16-bit functions. */
23112 s->two_byte_p = 1;
23114 return s->cmp_to;
23117 static int
23118 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23119 int start, int end, int overlaps)
23121 struct glyph *glyph, *last;
23122 Lisp_Object lgstring;
23123 int i;
23125 s->for_overlaps = overlaps;
23126 glyph = s->row->glyphs[s->area] + start;
23127 last = s->row->glyphs[s->area] + end;
23128 s->cmp_id = glyph->u.cmp.id;
23129 s->cmp_from = glyph->slice.cmp.from;
23130 s->cmp_to = glyph->slice.cmp.to + 1;
23131 s->face = FACE_FROM_ID (s->f, face_id);
23132 lgstring = composition_gstring_from_id (s->cmp_id);
23133 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23134 glyph++;
23135 while (glyph < last
23136 && glyph->u.cmp.automatic
23137 && glyph->u.cmp.id == s->cmp_id
23138 && s->cmp_to == glyph->slice.cmp.from)
23139 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23141 for (i = s->cmp_from; i < s->cmp_to; i++)
23143 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23144 unsigned code = LGLYPH_CODE (lglyph);
23146 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23148 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23149 return glyph - s->row->glyphs[s->area];
23153 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23154 See the comment of fill_glyph_string for arguments.
23155 Value is the index of the first glyph not in S. */
23158 static int
23159 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23160 int start, int end, int overlaps)
23162 struct glyph *glyph, *last;
23163 int voffset;
23165 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23166 s->for_overlaps = overlaps;
23167 glyph = s->row->glyphs[s->area] + start;
23168 last = s->row->glyphs[s->area] + end;
23169 voffset = glyph->voffset;
23170 s->face = FACE_FROM_ID (s->f, face_id);
23171 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23172 s->nchars = 1;
23173 s->width = glyph->pixel_width;
23174 glyph++;
23175 while (glyph < last
23176 && glyph->type == GLYPHLESS_GLYPH
23177 && glyph->voffset == voffset
23178 && glyph->face_id == face_id)
23180 s->nchars++;
23181 s->width += glyph->pixel_width;
23182 glyph++;
23184 s->ybase += voffset;
23185 return glyph - s->row->glyphs[s->area];
23189 /* Fill glyph string S from a sequence of character glyphs.
23191 FACE_ID is the face id of the string. START is the index of the
23192 first glyph to consider, END is the index of the last + 1.
23193 OVERLAPS non-zero means S should draw the foreground only, and use
23194 its physical height for clipping. See also draw_glyphs.
23196 Value is the index of the first glyph not in S. */
23198 static int
23199 fill_glyph_string (struct glyph_string *s, int face_id,
23200 int start, int end, int overlaps)
23202 struct glyph *glyph, *last;
23203 int voffset;
23204 int glyph_not_available_p;
23206 eassert (s->f == XFRAME (s->w->frame));
23207 eassert (s->nchars == 0);
23208 eassert (start >= 0 && end > start);
23210 s->for_overlaps = overlaps;
23211 glyph = s->row->glyphs[s->area] + start;
23212 last = s->row->glyphs[s->area] + end;
23213 voffset = glyph->voffset;
23214 s->padding_p = glyph->padding_p;
23215 glyph_not_available_p = glyph->glyph_not_available_p;
23217 while (glyph < last
23218 && glyph->type == CHAR_GLYPH
23219 && glyph->voffset == voffset
23220 /* Same face id implies same font, nowadays. */
23221 && glyph->face_id == face_id
23222 && glyph->glyph_not_available_p == glyph_not_available_p)
23224 int two_byte_p;
23226 s->face = get_glyph_face_and_encoding (s->f, glyph,
23227 s->char2b + s->nchars,
23228 &two_byte_p);
23229 s->two_byte_p = two_byte_p;
23230 ++s->nchars;
23231 eassert (s->nchars <= end - start);
23232 s->width += glyph->pixel_width;
23233 if (glyph++->padding_p != s->padding_p)
23234 break;
23237 s->font = s->face->font;
23239 /* If the specified font could not be loaded, use the frame's font,
23240 but record the fact that we couldn't load it in
23241 S->font_not_found_p so that we can draw rectangles for the
23242 characters of the glyph string. */
23243 if (s->font == NULL || glyph_not_available_p)
23245 s->font_not_found_p = 1;
23246 s->font = FRAME_FONT (s->f);
23249 /* Adjust base line for subscript/superscript text. */
23250 s->ybase += voffset;
23252 eassert (s->face && s->face->gc);
23253 return glyph - s->row->glyphs[s->area];
23257 /* Fill glyph string S from image glyph S->first_glyph. */
23259 static void
23260 fill_image_glyph_string (struct glyph_string *s)
23262 eassert (s->first_glyph->type == IMAGE_GLYPH);
23263 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23264 eassert (s->img);
23265 s->slice = s->first_glyph->slice.img;
23266 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23267 s->font = s->face->font;
23268 s->width = s->first_glyph->pixel_width;
23270 /* Adjust base line for subscript/superscript text. */
23271 s->ybase += s->first_glyph->voffset;
23275 /* Fill glyph string S from a sequence of stretch glyphs.
23277 START is the index of the first glyph to consider,
23278 END is the index of the last + 1.
23280 Value is the index of the first glyph not in S. */
23282 static int
23283 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23285 struct glyph *glyph, *last;
23286 int voffset, face_id;
23288 eassert (s->first_glyph->type == STRETCH_GLYPH);
23290 glyph = s->row->glyphs[s->area] + start;
23291 last = s->row->glyphs[s->area] + end;
23292 face_id = glyph->face_id;
23293 s->face = FACE_FROM_ID (s->f, face_id);
23294 s->font = s->face->font;
23295 s->width = glyph->pixel_width;
23296 s->nchars = 1;
23297 voffset = glyph->voffset;
23299 for (++glyph;
23300 (glyph < last
23301 && glyph->type == STRETCH_GLYPH
23302 && glyph->voffset == voffset
23303 && glyph->face_id == face_id);
23304 ++glyph)
23305 s->width += glyph->pixel_width;
23307 /* Adjust base line for subscript/superscript text. */
23308 s->ybase += voffset;
23310 /* The case that face->gc == 0 is handled when drawing the glyph
23311 string by calling PREPARE_FACE_FOR_DISPLAY. */
23312 eassert (s->face);
23313 return glyph - s->row->glyphs[s->area];
23316 static struct font_metrics *
23317 get_per_char_metric (struct font *font, XChar2b *char2b)
23319 static struct font_metrics metrics;
23320 unsigned code;
23322 if (! font)
23323 return NULL;
23324 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23325 if (code == FONT_INVALID_CODE)
23326 return NULL;
23327 font->driver->text_extents (font, &code, 1, &metrics);
23328 return &metrics;
23331 /* EXPORT for RIF:
23332 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23333 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23334 assumed to be zero. */
23336 void
23337 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23339 *left = *right = 0;
23341 if (glyph->type == CHAR_GLYPH)
23343 struct face *face;
23344 XChar2b char2b;
23345 struct font_metrics *pcm;
23347 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23348 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23350 if (pcm->rbearing > pcm->width)
23351 *right = pcm->rbearing - pcm->width;
23352 if (pcm->lbearing < 0)
23353 *left = -pcm->lbearing;
23356 else if (glyph->type == COMPOSITE_GLYPH)
23358 if (! glyph->u.cmp.automatic)
23360 struct composition *cmp = composition_table[glyph->u.cmp.id];
23362 if (cmp->rbearing > cmp->pixel_width)
23363 *right = cmp->rbearing - cmp->pixel_width;
23364 if (cmp->lbearing < 0)
23365 *left = - cmp->lbearing;
23367 else
23369 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23370 struct font_metrics metrics;
23372 composition_gstring_width (gstring, glyph->slice.cmp.from,
23373 glyph->slice.cmp.to + 1, &metrics);
23374 if (metrics.rbearing > metrics.width)
23375 *right = metrics.rbearing - metrics.width;
23376 if (metrics.lbearing < 0)
23377 *left = - metrics.lbearing;
23383 /* Return the index of the first glyph preceding glyph string S that
23384 is overwritten by S because of S's left overhang. Value is -1
23385 if no glyphs are overwritten. */
23387 static int
23388 left_overwritten (struct glyph_string *s)
23390 int k;
23392 if (s->left_overhang)
23394 int x = 0, i;
23395 struct glyph *glyphs = s->row->glyphs[s->area];
23396 int first = s->first_glyph - glyphs;
23398 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23399 x -= glyphs[i].pixel_width;
23401 k = i + 1;
23403 else
23404 k = -1;
23406 return k;
23410 /* Return the index of the first glyph preceding glyph string S that
23411 is overwriting S because of its right overhang. Value is -1 if no
23412 glyph in front of S overwrites S. */
23414 static int
23415 left_overwriting (struct glyph_string *s)
23417 int i, k, x;
23418 struct glyph *glyphs = s->row->glyphs[s->area];
23419 int first = s->first_glyph - glyphs;
23421 k = -1;
23422 x = 0;
23423 for (i = first - 1; i >= 0; --i)
23425 int left, right;
23426 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23427 if (x + right > 0)
23428 k = i;
23429 x -= glyphs[i].pixel_width;
23432 return k;
23436 /* Return the index of the last glyph following glyph string S that is
23437 overwritten by S because of S's right overhang. Value is -1 if
23438 no such glyph is found. */
23440 static int
23441 right_overwritten (struct glyph_string *s)
23443 int k = -1;
23445 if (s->right_overhang)
23447 int x = 0, i;
23448 struct glyph *glyphs = s->row->glyphs[s->area];
23449 int first = (s->first_glyph - glyphs
23450 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23451 int end = s->row->used[s->area];
23453 for (i = first; i < end && s->right_overhang > x; ++i)
23454 x += glyphs[i].pixel_width;
23456 k = i;
23459 return k;
23463 /* Return the index of the last glyph following glyph string S that
23464 overwrites S because of its left overhang. Value is negative
23465 if no such glyph is found. */
23467 static int
23468 right_overwriting (struct glyph_string *s)
23470 int i, k, x;
23471 int end = s->row->used[s->area];
23472 struct glyph *glyphs = s->row->glyphs[s->area];
23473 int first = (s->first_glyph - glyphs
23474 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23476 k = -1;
23477 x = 0;
23478 for (i = first; i < end; ++i)
23480 int left, right;
23481 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23482 if (x - left < 0)
23483 k = i;
23484 x += glyphs[i].pixel_width;
23487 return k;
23491 /* Set background width of glyph string S. START is the index of the
23492 first glyph following S. LAST_X is the right-most x-position + 1
23493 in the drawing area. */
23495 static void
23496 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23498 /* If the face of this glyph string has to be drawn to the end of
23499 the drawing area, set S->extends_to_end_of_line_p. */
23501 if (start == s->row->used[s->area]
23502 && s->area == TEXT_AREA
23503 && ((s->row->fill_line_p
23504 && (s->hl == DRAW_NORMAL_TEXT
23505 || s->hl == DRAW_IMAGE_RAISED
23506 || s->hl == DRAW_IMAGE_SUNKEN))
23507 || s->hl == DRAW_MOUSE_FACE))
23508 s->extends_to_end_of_line_p = 1;
23510 /* If S extends its face to the end of the line, set its
23511 background_width to the distance to the right edge of the drawing
23512 area. */
23513 if (s->extends_to_end_of_line_p)
23514 s->background_width = last_x - s->x + 1;
23515 else
23516 s->background_width = s->width;
23520 /* Compute overhangs and x-positions for glyph string S and its
23521 predecessors, or successors. X is the starting x-position for S.
23522 BACKWARD_P non-zero means process predecessors. */
23524 static void
23525 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23527 if (backward_p)
23529 while (s)
23531 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23532 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23533 x -= s->width;
23534 s->x = x;
23535 s = s->prev;
23538 else
23540 while (s)
23542 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23543 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23544 s->x = x;
23545 x += s->width;
23546 s = s->next;
23553 /* The following macros are only called from draw_glyphs below.
23554 They reference the following parameters of that function directly:
23555 `w', `row', `area', and `overlap_p'
23556 as well as the following local variables:
23557 `s', `f', and `hdc' (in W32) */
23559 #ifdef HAVE_NTGUI
23560 /* On W32, silently add local `hdc' variable to argument list of
23561 init_glyph_string. */
23562 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23563 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23564 #else
23565 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23566 init_glyph_string (s, char2b, w, row, area, start, hl)
23567 #endif
23569 /* Add a glyph string for a stretch glyph to the list of strings
23570 between HEAD and TAIL. START is the index of the stretch glyph in
23571 row area AREA of glyph row ROW. END is the index of the last glyph
23572 in that glyph row area. X is the current output position assigned
23573 to the new glyph string constructed. HL overrides that face of the
23574 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23575 is the right-most x-position of the drawing area. */
23577 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23578 and below -- keep them on one line. */
23579 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23580 do \
23582 s = alloca (sizeof *s); \
23583 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23584 START = fill_stretch_glyph_string (s, START, END); \
23585 append_glyph_string (&HEAD, &TAIL, s); \
23586 s->x = (X); \
23588 while (0)
23591 /* Add a glyph string for an image glyph to the list of strings
23592 between HEAD and TAIL. START is the index of the image glyph in
23593 row area AREA of glyph row ROW. END is the index of the last glyph
23594 in that glyph row area. X is the current output position assigned
23595 to the new glyph string constructed. HL overrides that face of the
23596 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23597 is the right-most x-position of the drawing area. */
23599 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23600 do \
23602 s = alloca (sizeof *s); \
23603 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23604 fill_image_glyph_string (s); \
23605 append_glyph_string (&HEAD, &TAIL, s); \
23606 ++START; \
23607 s->x = (X); \
23609 while (0)
23612 /* Add a glyph string for a sequence of character glyphs to the list
23613 of strings between HEAD and TAIL. START is the index of the first
23614 glyph in row area AREA of glyph row ROW that is part of the new
23615 glyph string. END is the index of the last glyph in that glyph row
23616 area. X is the current output position assigned to the new glyph
23617 string constructed. HL overrides that face of the glyph; e.g. it
23618 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23619 right-most x-position of the drawing area. */
23621 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23622 do \
23624 int face_id; \
23625 XChar2b *char2b; \
23627 face_id = (row)->glyphs[area][START].face_id; \
23629 s = alloca (sizeof *s); \
23630 char2b = alloca ((END - START) * sizeof *char2b); \
23631 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23632 append_glyph_string (&HEAD, &TAIL, s); \
23633 s->x = (X); \
23634 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23636 while (0)
23639 /* Add a glyph string for a composite sequence to the list of strings
23640 between HEAD and TAIL. START is the index of the first glyph in
23641 row area AREA of glyph row ROW that is part of the new glyph
23642 string. END is the index of the last glyph in that glyph row area.
23643 X is the current output position assigned to the new glyph string
23644 constructed. HL overrides that face of the glyph; e.g. it is
23645 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23646 x-position of the drawing area. */
23648 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23649 do { \
23650 int face_id = (row)->glyphs[area][START].face_id; \
23651 struct face *base_face = FACE_FROM_ID (f, face_id); \
23652 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23653 struct composition *cmp = composition_table[cmp_id]; \
23654 XChar2b *char2b; \
23655 struct glyph_string *first_s = NULL; \
23656 int n; \
23658 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23660 /* Make glyph_strings for each glyph sequence that is drawable by \
23661 the same face, and append them to HEAD/TAIL. */ \
23662 for (n = 0; n < cmp->glyph_len;) \
23664 s = alloca (sizeof *s); \
23665 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23666 append_glyph_string (&(HEAD), &(TAIL), s); \
23667 s->cmp = cmp; \
23668 s->cmp_from = n; \
23669 s->x = (X); \
23670 if (n == 0) \
23671 first_s = s; \
23672 n = fill_composite_glyph_string (s, base_face, overlaps); \
23675 ++START; \
23676 s = first_s; \
23677 } while (0)
23680 /* Add a glyph string for a glyph-string sequence to the list of strings
23681 between HEAD and TAIL. */
23683 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23684 do { \
23685 int face_id; \
23686 XChar2b *char2b; \
23687 Lisp_Object gstring; \
23689 face_id = (row)->glyphs[area][START].face_id; \
23690 gstring = (composition_gstring_from_id \
23691 ((row)->glyphs[area][START].u.cmp.id)); \
23692 s = alloca (sizeof *s); \
23693 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23694 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23695 append_glyph_string (&(HEAD), &(TAIL), s); \
23696 s->x = (X); \
23697 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23698 } while (0)
23701 /* Add a glyph string for a sequence of glyphless character's glyphs
23702 to the list of strings between HEAD and TAIL. The meanings of
23703 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23705 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23706 do \
23708 int face_id; \
23710 face_id = (row)->glyphs[area][START].face_id; \
23712 s = alloca (sizeof *s); \
23713 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23714 append_glyph_string (&HEAD, &TAIL, s); \
23715 s->x = (X); \
23716 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23717 overlaps); \
23719 while (0)
23722 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23723 of AREA of glyph row ROW on window W between indices START and END.
23724 HL overrides the face for drawing glyph strings, e.g. it is
23725 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23726 x-positions of the drawing area.
23728 This is an ugly monster macro construct because we must use alloca
23729 to allocate glyph strings (because draw_glyphs can be called
23730 asynchronously). */
23732 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23733 do \
23735 HEAD = TAIL = NULL; \
23736 while (START < END) \
23738 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23739 switch (first_glyph->type) \
23741 case CHAR_GLYPH: \
23742 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23743 HL, X, LAST_X); \
23744 break; \
23746 case COMPOSITE_GLYPH: \
23747 if (first_glyph->u.cmp.automatic) \
23748 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23749 HL, X, LAST_X); \
23750 else \
23751 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23752 HL, X, LAST_X); \
23753 break; \
23755 case STRETCH_GLYPH: \
23756 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23757 HL, X, LAST_X); \
23758 break; \
23760 case IMAGE_GLYPH: \
23761 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23762 HL, X, LAST_X); \
23763 break; \
23765 case GLYPHLESS_GLYPH: \
23766 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23767 HL, X, LAST_X); \
23768 break; \
23770 default: \
23771 emacs_abort (); \
23774 if (s) \
23776 set_glyph_string_background_width (s, START, LAST_X); \
23777 (X) += s->width; \
23780 } while (0)
23783 /* Draw glyphs between START and END in AREA of ROW on window W,
23784 starting at x-position X. X is relative to AREA in W. HL is a
23785 face-override with the following meaning:
23787 DRAW_NORMAL_TEXT draw normally
23788 DRAW_CURSOR draw in cursor face
23789 DRAW_MOUSE_FACE draw in mouse face.
23790 DRAW_INVERSE_VIDEO draw in mode line face
23791 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23792 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23794 If OVERLAPS is non-zero, draw only the foreground of characters and
23795 clip to the physical height of ROW. Non-zero value also defines
23796 the overlapping part to be drawn:
23798 OVERLAPS_PRED overlap with preceding rows
23799 OVERLAPS_SUCC overlap with succeeding rows
23800 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23801 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23803 Value is the x-position reached, relative to AREA of W. */
23805 static int
23806 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23807 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23808 enum draw_glyphs_face hl, int overlaps)
23810 struct glyph_string *head, *tail;
23811 struct glyph_string *s;
23812 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23813 int i, j, x_reached, last_x, area_left = 0;
23814 struct frame *f = XFRAME (WINDOW_FRAME (w));
23815 DECLARE_HDC (hdc);
23817 ALLOCATE_HDC (hdc, f);
23819 /* Let's rather be paranoid than getting a SEGV. */
23820 end = min (end, row->used[area]);
23821 start = clip_to_bounds (0, start, end);
23823 /* Translate X to frame coordinates. Set last_x to the right
23824 end of the drawing area. */
23825 if (row->full_width_p)
23827 /* X is relative to the left edge of W, without scroll bars
23828 or fringes. */
23829 area_left = WINDOW_LEFT_EDGE_X (w);
23830 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23832 else
23834 area_left = window_box_left (w, area);
23835 last_x = area_left + window_box_width (w, area);
23837 x += area_left;
23839 /* Build a doubly-linked list of glyph_string structures between
23840 head and tail from what we have to draw. Note that the macro
23841 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23842 the reason we use a separate variable `i'. */
23843 i = start;
23844 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23845 if (tail)
23846 x_reached = tail->x + tail->background_width;
23847 else
23848 x_reached = x;
23850 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23851 the row, redraw some glyphs in front or following the glyph
23852 strings built above. */
23853 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23855 struct glyph_string *h, *t;
23856 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23857 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23858 int check_mouse_face = 0;
23859 int dummy_x = 0;
23861 /* If mouse highlighting is on, we may need to draw adjacent
23862 glyphs using mouse-face highlighting. */
23863 if (area == TEXT_AREA && row->mouse_face_p
23864 && hlinfo->mouse_face_beg_row >= 0
23865 && hlinfo->mouse_face_end_row >= 0)
23867 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23869 if (row_vpos >= hlinfo->mouse_face_beg_row
23870 && row_vpos <= hlinfo->mouse_face_end_row)
23872 check_mouse_face = 1;
23873 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23874 ? hlinfo->mouse_face_beg_col : 0;
23875 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23876 ? hlinfo->mouse_face_end_col
23877 : row->used[TEXT_AREA];
23881 /* Compute overhangs for all glyph strings. */
23882 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23883 for (s = head; s; s = s->next)
23884 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23886 /* Prepend glyph strings for glyphs in front of the first glyph
23887 string that are overwritten because of the first glyph
23888 string's left overhang. The background of all strings
23889 prepended must be drawn because the first glyph string
23890 draws over it. */
23891 i = left_overwritten (head);
23892 if (i >= 0)
23894 enum draw_glyphs_face overlap_hl;
23896 /* If this row contains mouse highlighting, attempt to draw
23897 the overlapped glyphs with the correct highlight. This
23898 code fails if the overlap encompasses more than one glyph
23899 and mouse-highlight spans only some of these glyphs.
23900 However, making it work perfectly involves a lot more
23901 code, and I don't know if the pathological case occurs in
23902 practice, so we'll stick to this for now. --- cyd */
23903 if (check_mouse_face
23904 && mouse_beg_col < start && mouse_end_col > i)
23905 overlap_hl = DRAW_MOUSE_FACE;
23906 else
23907 overlap_hl = DRAW_NORMAL_TEXT;
23909 j = i;
23910 BUILD_GLYPH_STRINGS (j, start, h, t,
23911 overlap_hl, dummy_x, last_x);
23912 start = i;
23913 compute_overhangs_and_x (t, head->x, 1);
23914 prepend_glyph_string_lists (&head, &tail, h, t);
23915 clip_head = head;
23918 /* Prepend glyph strings for glyphs in front of the first glyph
23919 string that overwrite that glyph string because of their
23920 right overhang. For these strings, only the foreground must
23921 be drawn, because it draws over the glyph string at `head'.
23922 The background must not be drawn because this would overwrite
23923 right overhangs of preceding glyphs for which no glyph
23924 strings exist. */
23925 i = left_overwriting (head);
23926 if (i >= 0)
23928 enum draw_glyphs_face overlap_hl;
23930 if (check_mouse_face
23931 && mouse_beg_col < start && mouse_end_col > i)
23932 overlap_hl = DRAW_MOUSE_FACE;
23933 else
23934 overlap_hl = DRAW_NORMAL_TEXT;
23936 clip_head = head;
23937 BUILD_GLYPH_STRINGS (i, start, h, t,
23938 overlap_hl, dummy_x, last_x);
23939 for (s = h; s; s = s->next)
23940 s->background_filled_p = 1;
23941 compute_overhangs_and_x (t, head->x, 1);
23942 prepend_glyph_string_lists (&head, &tail, h, t);
23945 /* Append glyphs strings for glyphs following the last glyph
23946 string tail that are overwritten by tail. The background of
23947 these strings has to be drawn because tail's foreground draws
23948 over it. */
23949 i = right_overwritten (tail);
23950 if (i >= 0)
23952 enum draw_glyphs_face overlap_hl;
23954 if (check_mouse_face
23955 && mouse_beg_col < i && mouse_end_col > end)
23956 overlap_hl = DRAW_MOUSE_FACE;
23957 else
23958 overlap_hl = DRAW_NORMAL_TEXT;
23960 BUILD_GLYPH_STRINGS (end, i, h, t,
23961 overlap_hl, x, last_x);
23962 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23963 we don't have `end = i;' here. */
23964 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23965 append_glyph_string_lists (&head, &tail, h, t);
23966 clip_tail = tail;
23969 /* Append glyph strings for glyphs following the last glyph
23970 string tail that overwrite tail. The foreground of such
23971 glyphs has to be drawn because it writes into the background
23972 of tail. The background must not be drawn because it could
23973 paint over the foreground of following glyphs. */
23974 i = right_overwriting (tail);
23975 if (i >= 0)
23977 enum draw_glyphs_face overlap_hl;
23978 if (check_mouse_face
23979 && mouse_beg_col < i && mouse_end_col > end)
23980 overlap_hl = DRAW_MOUSE_FACE;
23981 else
23982 overlap_hl = DRAW_NORMAL_TEXT;
23984 clip_tail = tail;
23985 i++; /* We must include the Ith glyph. */
23986 BUILD_GLYPH_STRINGS (end, i, h, t,
23987 overlap_hl, x, last_x);
23988 for (s = h; s; s = s->next)
23989 s->background_filled_p = 1;
23990 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23991 append_glyph_string_lists (&head, &tail, h, t);
23993 if (clip_head || clip_tail)
23994 for (s = head; s; s = s->next)
23996 s->clip_head = clip_head;
23997 s->clip_tail = clip_tail;
24001 /* Draw all strings. */
24002 for (s = head; s; s = s->next)
24003 FRAME_RIF (f)->draw_glyph_string (s);
24005 #ifndef HAVE_NS
24006 /* When focus a sole frame and move horizontally, this sets on_p to 0
24007 causing a failure to erase prev cursor position. */
24008 if (area == TEXT_AREA
24009 && !row->full_width_p
24010 /* When drawing overlapping rows, only the glyph strings'
24011 foreground is drawn, which doesn't erase a cursor
24012 completely. */
24013 && !overlaps)
24015 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24016 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24017 : (tail ? tail->x + tail->background_width : x));
24018 x0 -= area_left;
24019 x1 -= area_left;
24021 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24022 row->y, MATRIX_ROW_BOTTOM_Y (row));
24024 #endif
24026 /* Value is the x-position up to which drawn, relative to AREA of W.
24027 This doesn't include parts drawn because of overhangs. */
24028 if (row->full_width_p)
24029 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24030 else
24031 x_reached -= area_left;
24033 RELEASE_HDC (hdc, f);
24035 return x_reached;
24038 /* Expand row matrix if too narrow. Don't expand if area
24039 is not present. */
24041 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24043 if (!fonts_changed_p \
24044 && (it->glyph_row->glyphs[area] \
24045 < it->glyph_row->glyphs[area + 1])) \
24047 it->w->ncols_scale_factor++; \
24048 fonts_changed_p = 1; \
24052 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24053 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24055 static void
24056 append_glyph (struct it *it)
24058 struct glyph *glyph;
24059 enum glyph_row_area area = it->area;
24061 eassert (it->glyph_row);
24062 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24064 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24065 if (glyph < it->glyph_row->glyphs[area + 1])
24067 /* If the glyph row is reversed, we need to prepend the glyph
24068 rather than append it. */
24069 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24071 struct glyph *g;
24073 /* Make room for the additional glyph. */
24074 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24075 g[1] = *g;
24076 glyph = it->glyph_row->glyphs[area];
24078 glyph->charpos = CHARPOS (it->position);
24079 glyph->object = it->object;
24080 if (it->pixel_width > 0)
24082 glyph->pixel_width = it->pixel_width;
24083 glyph->padding_p = 0;
24085 else
24087 /* Assure at least 1-pixel width. Otherwise, cursor can't
24088 be displayed correctly. */
24089 glyph->pixel_width = 1;
24090 glyph->padding_p = 1;
24092 glyph->ascent = it->ascent;
24093 glyph->descent = it->descent;
24094 glyph->voffset = it->voffset;
24095 glyph->type = CHAR_GLYPH;
24096 glyph->avoid_cursor_p = it->avoid_cursor_p;
24097 glyph->multibyte_p = it->multibyte_p;
24098 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24100 /* In R2L rows, the left and the right box edges need to be
24101 drawn in reverse direction. */
24102 glyph->right_box_line_p = it->start_of_box_run_p;
24103 glyph->left_box_line_p = it->end_of_box_run_p;
24105 else
24107 glyph->left_box_line_p = it->start_of_box_run_p;
24108 glyph->right_box_line_p = it->end_of_box_run_p;
24110 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24111 || it->phys_descent > it->descent);
24112 glyph->glyph_not_available_p = it->glyph_not_available_p;
24113 glyph->face_id = it->face_id;
24114 glyph->u.ch = it->char_to_display;
24115 glyph->slice.img = null_glyph_slice;
24116 glyph->font_type = FONT_TYPE_UNKNOWN;
24117 if (it->bidi_p)
24119 glyph->resolved_level = it->bidi_it.resolved_level;
24120 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24121 emacs_abort ();
24122 glyph->bidi_type = it->bidi_it.type;
24124 else
24126 glyph->resolved_level = 0;
24127 glyph->bidi_type = UNKNOWN_BT;
24129 ++it->glyph_row->used[area];
24131 else
24132 IT_EXPAND_MATRIX_WIDTH (it, area);
24135 /* Store one glyph for the composition IT->cmp_it.id in
24136 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24137 non-null. */
24139 static void
24140 append_composite_glyph (struct it *it)
24142 struct glyph *glyph;
24143 enum glyph_row_area area = it->area;
24145 eassert (it->glyph_row);
24147 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24148 if (glyph < it->glyph_row->glyphs[area + 1])
24150 /* If the glyph row is reversed, we need to prepend the glyph
24151 rather than append it. */
24152 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24154 struct glyph *g;
24156 /* Make room for the new glyph. */
24157 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24158 g[1] = *g;
24159 glyph = it->glyph_row->glyphs[it->area];
24161 glyph->charpos = it->cmp_it.charpos;
24162 glyph->object = it->object;
24163 glyph->pixel_width = it->pixel_width;
24164 glyph->ascent = it->ascent;
24165 glyph->descent = it->descent;
24166 glyph->voffset = it->voffset;
24167 glyph->type = COMPOSITE_GLYPH;
24168 if (it->cmp_it.ch < 0)
24170 glyph->u.cmp.automatic = 0;
24171 glyph->u.cmp.id = it->cmp_it.id;
24172 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24174 else
24176 glyph->u.cmp.automatic = 1;
24177 glyph->u.cmp.id = it->cmp_it.id;
24178 glyph->slice.cmp.from = it->cmp_it.from;
24179 glyph->slice.cmp.to = it->cmp_it.to - 1;
24181 glyph->avoid_cursor_p = it->avoid_cursor_p;
24182 glyph->multibyte_p = it->multibyte_p;
24183 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24185 /* In R2L rows, the left and the right box edges need to be
24186 drawn in reverse direction. */
24187 glyph->right_box_line_p = it->start_of_box_run_p;
24188 glyph->left_box_line_p = it->end_of_box_run_p;
24190 else
24192 glyph->left_box_line_p = it->start_of_box_run_p;
24193 glyph->right_box_line_p = it->end_of_box_run_p;
24195 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24196 || it->phys_descent > it->descent);
24197 glyph->padding_p = 0;
24198 glyph->glyph_not_available_p = 0;
24199 glyph->face_id = it->face_id;
24200 glyph->font_type = FONT_TYPE_UNKNOWN;
24201 if (it->bidi_p)
24203 glyph->resolved_level = it->bidi_it.resolved_level;
24204 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24205 emacs_abort ();
24206 glyph->bidi_type = it->bidi_it.type;
24208 ++it->glyph_row->used[area];
24210 else
24211 IT_EXPAND_MATRIX_WIDTH (it, area);
24215 /* Change IT->ascent and IT->height according to the setting of
24216 IT->voffset. */
24218 static void
24219 take_vertical_position_into_account (struct it *it)
24221 if (it->voffset)
24223 if (it->voffset < 0)
24224 /* Increase the ascent so that we can display the text higher
24225 in the line. */
24226 it->ascent -= it->voffset;
24227 else
24228 /* Increase the descent so that we can display the text lower
24229 in the line. */
24230 it->descent += it->voffset;
24235 /* Produce glyphs/get display metrics for the image IT is loaded with.
24236 See the description of struct display_iterator in dispextern.h for
24237 an overview of struct display_iterator. */
24239 static void
24240 produce_image_glyph (struct it *it)
24242 struct image *img;
24243 struct face *face;
24244 int glyph_ascent, crop;
24245 struct glyph_slice slice;
24247 eassert (it->what == IT_IMAGE);
24249 face = FACE_FROM_ID (it->f, it->face_id);
24250 eassert (face);
24251 /* Make sure X resources of the face is loaded. */
24252 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24254 if (it->image_id < 0)
24256 /* Fringe bitmap. */
24257 it->ascent = it->phys_ascent = 0;
24258 it->descent = it->phys_descent = 0;
24259 it->pixel_width = 0;
24260 it->nglyphs = 0;
24261 return;
24264 img = IMAGE_FROM_ID (it->f, it->image_id);
24265 eassert (img);
24266 /* Make sure X resources of the image is loaded. */
24267 prepare_image_for_display (it->f, img);
24269 slice.x = slice.y = 0;
24270 slice.width = img->width;
24271 slice.height = img->height;
24273 if (INTEGERP (it->slice.x))
24274 slice.x = XINT (it->slice.x);
24275 else if (FLOATP (it->slice.x))
24276 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24278 if (INTEGERP (it->slice.y))
24279 slice.y = XINT (it->slice.y);
24280 else if (FLOATP (it->slice.y))
24281 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24283 if (INTEGERP (it->slice.width))
24284 slice.width = XINT (it->slice.width);
24285 else if (FLOATP (it->slice.width))
24286 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24288 if (INTEGERP (it->slice.height))
24289 slice.height = XINT (it->slice.height);
24290 else if (FLOATP (it->slice.height))
24291 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24293 if (slice.x >= img->width)
24294 slice.x = img->width;
24295 if (slice.y >= img->height)
24296 slice.y = img->height;
24297 if (slice.x + slice.width >= img->width)
24298 slice.width = img->width - slice.x;
24299 if (slice.y + slice.height > img->height)
24300 slice.height = img->height - slice.y;
24302 if (slice.width == 0 || slice.height == 0)
24303 return;
24305 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24307 it->descent = slice.height - glyph_ascent;
24308 if (slice.y == 0)
24309 it->descent += img->vmargin;
24310 if (slice.y + slice.height == img->height)
24311 it->descent += img->vmargin;
24312 it->phys_descent = it->descent;
24314 it->pixel_width = slice.width;
24315 if (slice.x == 0)
24316 it->pixel_width += img->hmargin;
24317 if (slice.x + slice.width == img->width)
24318 it->pixel_width += img->hmargin;
24320 /* It's quite possible for images to have an ascent greater than
24321 their height, so don't get confused in that case. */
24322 if (it->descent < 0)
24323 it->descent = 0;
24325 it->nglyphs = 1;
24327 if (face->box != FACE_NO_BOX)
24329 if (face->box_line_width > 0)
24331 if (slice.y == 0)
24332 it->ascent += face->box_line_width;
24333 if (slice.y + slice.height == img->height)
24334 it->descent += face->box_line_width;
24337 if (it->start_of_box_run_p && slice.x == 0)
24338 it->pixel_width += eabs (face->box_line_width);
24339 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24340 it->pixel_width += eabs (face->box_line_width);
24343 take_vertical_position_into_account (it);
24345 /* Automatically crop wide image glyphs at right edge so we can
24346 draw the cursor on same display row. */
24347 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24348 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24350 it->pixel_width -= crop;
24351 slice.width -= crop;
24354 if (it->glyph_row)
24356 struct glyph *glyph;
24357 enum glyph_row_area area = it->area;
24359 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24360 if (glyph < it->glyph_row->glyphs[area + 1])
24362 glyph->charpos = CHARPOS (it->position);
24363 glyph->object = it->object;
24364 glyph->pixel_width = it->pixel_width;
24365 glyph->ascent = glyph_ascent;
24366 glyph->descent = it->descent;
24367 glyph->voffset = it->voffset;
24368 glyph->type = IMAGE_GLYPH;
24369 glyph->avoid_cursor_p = it->avoid_cursor_p;
24370 glyph->multibyte_p = it->multibyte_p;
24371 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24373 /* In R2L rows, the left and the right box edges need to be
24374 drawn in reverse direction. */
24375 glyph->right_box_line_p = it->start_of_box_run_p;
24376 glyph->left_box_line_p = it->end_of_box_run_p;
24378 else
24380 glyph->left_box_line_p = it->start_of_box_run_p;
24381 glyph->right_box_line_p = it->end_of_box_run_p;
24383 glyph->overlaps_vertically_p = 0;
24384 glyph->padding_p = 0;
24385 glyph->glyph_not_available_p = 0;
24386 glyph->face_id = it->face_id;
24387 glyph->u.img_id = img->id;
24388 glyph->slice.img = slice;
24389 glyph->font_type = FONT_TYPE_UNKNOWN;
24390 if (it->bidi_p)
24392 glyph->resolved_level = it->bidi_it.resolved_level;
24393 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24394 emacs_abort ();
24395 glyph->bidi_type = it->bidi_it.type;
24397 ++it->glyph_row->used[area];
24399 else
24400 IT_EXPAND_MATRIX_WIDTH (it, area);
24405 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24406 of the glyph, WIDTH and HEIGHT are the width and height of the
24407 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24409 static void
24410 append_stretch_glyph (struct it *it, Lisp_Object object,
24411 int width, int height, int ascent)
24413 struct glyph *glyph;
24414 enum glyph_row_area area = it->area;
24416 eassert (ascent >= 0 && ascent <= height);
24418 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24419 if (glyph < it->glyph_row->glyphs[area + 1])
24421 /* If the glyph row is reversed, we need to prepend the glyph
24422 rather than append it. */
24423 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24425 struct glyph *g;
24427 /* Make room for the additional glyph. */
24428 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24429 g[1] = *g;
24430 glyph = it->glyph_row->glyphs[area];
24432 glyph->charpos = CHARPOS (it->position);
24433 glyph->object = object;
24434 glyph->pixel_width = width;
24435 glyph->ascent = ascent;
24436 glyph->descent = height - ascent;
24437 glyph->voffset = it->voffset;
24438 glyph->type = STRETCH_GLYPH;
24439 glyph->avoid_cursor_p = it->avoid_cursor_p;
24440 glyph->multibyte_p = it->multibyte_p;
24441 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24443 /* In R2L rows, the left and the right box edges need to be
24444 drawn in reverse direction. */
24445 glyph->right_box_line_p = it->start_of_box_run_p;
24446 glyph->left_box_line_p = it->end_of_box_run_p;
24448 else
24450 glyph->left_box_line_p = it->start_of_box_run_p;
24451 glyph->right_box_line_p = it->end_of_box_run_p;
24453 glyph->overlaps_vertically_p = 0;
24454 glyph->padding_p = 0;
24455 glyph->glyph_not_available_p = 0;
24456 glyph->face_id = it->face_id;
24457 glyph->u.stretch.ascent = ascent;
24458 glyph->u.stretch.height = height;
24459 glyph->slice.img = null_glyph_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 else
24470 glyph->resolved_level = 0;
24471 glyph->bidi_type = UNKNOWN_BT;
24473 ++it->glyph_row->used[area];
24475 else
24476 IT_EXPAND_MATRIX_WIDTH (it, area);
24479 #endif /* HAVE_WINDOW_SYSTEM */
24481 /* Produce a stretch glyph for iterator IT. IT->object is the value
24482 of the glyph property displayed. The value must be a list
24483 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24484 being recognized:
24486 1. `:width WIDTH' specifies that the space should be WIDTH *
24487 canonical char width wide. WIDTH may be an integer or floating
24488 point number.
24490 2. `:relative-width FACTOR' specifies that the width of the stretch
24491 should be computed from the width of the first character having the
24492 `glyph' property, and should be FACTOR times that width.
24494 3. `:align-to HPOS' specifies that the space should be wide enough
24495 to reach HPOS, a value in canonical character units.
24497 Exactly one of the above pairs must be present.
24499 4. `:height HEIGHT' specifies that the height of the stretch produced
24500 should be HEIGHT, measured in canonical character units.
24502 5. `:relative-height FACTOR' specifies that the height of the
24503 stretch should be FACTOR times the height of the characters having
24504 the glyph property.
24506 Either none or exactly one of 4 or 5 must be present.
24508 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24509 of the stretch should be used for the ascent of the stretch.
24510 ASCENT must be in the range 0 <= ASCENT <= 100. */
24512 void
24513 produce_stretch_glyph (struct it *it)
24515 /* (space :width WIDTH :height HEIGHT ...) */
24516 Lisp_Object prop, plist;
24517 int width = 0, height = 0, align_to = -1;
24518 int zero_width_ok_p = 0;
24519 double tem;
24520 struct font *font = NULL;
24522 #ifdef HAVE_WINDOW_SYSTEM
24523 int ascent = 0;
24524 int zero_height_ok_p = 0;
24526 if (FRAME_WINDOW_P (it->f))
24528 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24529 font = face->font ? face->font : FRAME_FONT (it->f);
24530 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24532 #endif
24534 /* List should start with `space'. */
24535 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24536 plist = XCDR (it->object);
24538 /* Compute the width of the stretch. */
24539 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24540 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24542 /* Absolute width `:width WIDTH' specified and valid. */
24543 zero_width_ok_p = 1;
24544 width = (int)tem;
24546 #ifdef HAVE_WINDOW_SYSTEM
24547 else if (FRAME_WINDOW_P (it->f)
24548 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24550 /* Relative width `:relative-width FACTOR' specified and valid.
24551 Compute the width of the characters having the `glyph'
24552 property. */
24553 struct it it2;
24554 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24556 it2 = *it;
24557 if (it->multibyte_p)
24558 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24559 else
24561 it2.c = it2.char_to_display = *p, it2.len = 1;
24562 if (! ASCII_CHAR_P (it2.c))
24563 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24566 it2.glyph_row = NULL;
24567 it2.what = IT_CHARACTER;
24568 x_produce_glyphs (&it2);
24569 width = NUMVAL (prop) * it2.pixel_width;
24571 #endif /* HAVE_WINDOW_SYSTEM */
24572 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24573 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24575 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24576 align_to = (align_to < 0
24578 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24579 else if (align_to < 0)
24580 align_to = window_box_left_offset (it->w, TEXT_AREA);
24581 width = max (0, (int)tem + align_to - it->current_x);
24582 zero_width_ok_p = 1;
24584 else
24585 /* Nothing specified -> width defaults to canonical char width. */
24586 width = FRAME_COLUMN_WIDTH (it->f);
24588 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24589 width = 1;
24591 #ifdef HAVE_WINDOW_SYSTEM
24592 /* Compute height. */
24593 if (FRAME_WINDOW_P (it->f))
24595 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24596 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24598 height = (int)tem;
24599 zero_height_ok_p = 1;
24601 else if (prop = Fplist_get (plist, QCrelative_height),
24602 NUMVAL (prop) > 0)
24603 height = FONT_HEIGHT (font) * NUMVAL (prop);
24604 else
24605 height = FONT_HEIGHT (font);
24607 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24608 height = 1;
24610 /* Compute percentage of height used for ascent. If
24611 `:ascent ASCENT' is present and valid, use that. Otherwise,
24612 derive the ascent from the font in use. */
24613 if (prop = Fplist_get (plist, QCascent),
24614 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24615 ascent = height * NUMVAL (prop) / 100.0;
24616 else if (!NILP (prop)
24617 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24618 ascent = min (max (0, (int)tem), height);
24619 else
24620 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24622 else
24623 #endif /* HAVE_WINDOW_SYSTEM */
24624 height = 1;
24626 if (width > 0 && it->line_wrap != TRUNCATE
24627 && it->current_x + width > it->last_visible_x)
24629 width = it->last_visible_x - it->current_x;
24630 #ifdef HAVE_WINDOW_SYSTEM
24631 /* Subtract one more pixel from the stretch width, but only on
24632 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24633 width -= FRAME_WINDOW_P (it->f);
24634 #endif
24637 if (width > 0 && height > 0 && it->glyph_row)
24639 Lisp_Object o_object = it->object;
24640 Lisp_Object object = it->stack[it->sp - 1].string;
24641 int n = width;
24643 if (!STRINGP (object))
24644 object = it->w->contents;
24645 #ifdef HAVE_WINDOW_SYSTEM
24646 if (FRAME_WINDOW_P (it->f))
24647 append_stretch_glyph (it, object, width, height, ascent);
24648 else
24649 #endif
24651 it->object = object;
24652 it->char_to_display = ' ';
24653 it->pixel_width = it->len = 1;
24654 while (n--)
24655 tty_append_glyph (it);
24656 it->object = o_object;
24660 it->pixel_width = width;
24661 #ifdef HAVE_WINDOW_SYSTEM
24662 if (FRAME_WINDOW_P (it->f))
24664 it->ascent = it->phys_ascent = ascent;
24665 it->descent = it->phys_descent = height - it->ascent;
24666 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24667 take_vertical_position_into_account (it);
24669 else
24670 #endif
24671 it->nglyphs = width;
24674 /* Get information about special display element WHAT in an
24675 environment described by IT. WHAT is one of IT_TRUNCATION or
24676 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24677 non-null glyph_row member. This function ensures that fields like
24678 face_id, c, len of IT are left untouched. */
24680 static void
24681 produce_special_glyphs (struct it *it, enum display_element_type what)
24683 struct it temp_it;
24684 Lisp_Object gc;
24685 GLYPH glyph;
24687 temp_it = *it;
24688 temp_it.object = make_number (0);
24689 memset (&temp_it.current, 0, sizeof temp_it.current);
24691 if (what == IT_CONTINUATION)
24693 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24694 if (it->bidi_it.paragraph_dir == R2L)
24695 SET_GLYPH_FROM_CHAR (glyph, '/');
24696 else
24697 SET_GLYPH_FROM_CHAR (glyph, '\\');
24698 if (it->dp
24699 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24701 /* FIXME: Should we mirror GC for R2L lines? */
24702 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24703 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24706 else if (what == IT_TRUNCATION)
24708 /* Truncation glyph. */
24709 SET_GLYPH_FROM_CHAR (glyph, '$');
24710 if (it->dp
24711 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24713 /* FIXME: Should we mirror GC for R2L lines? */
24714 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24715 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24718 else
24719 emacs_abort ();
24721 #ifdef HAVE_WINDOW_SYSTEM
24722 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24723 is turned off, we precede the truncation/continuation glyphs by a
24724 stretch glyph whose width is computed such that these special
24725 glyphs are aligned at the window margin, even when very different
24726 fonts are used in different glyph rows. */
24727 if (FRAME_WINDOW_P (temp_it.f)
24728 /* init_iterator calls this with it->glyph_row == NULL, and it
24729 wants only the pixel width of the truncation/continuation
24730 glyphs. */
24731 && temp_it.glyph_row
24732 /* insert_left_trunc_glyphs calls us at the beginning of the
24733 row, and it has its own calculation of the stretch glyph
24734 width. */
24735 && temp_it.glyph_row->used[TEXT_AREA] > 0
24736 && (temp_it.glyph_row->reversed_p
24737 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24738 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24740 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24742 if (stretch_width > 0)
24744 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24745 struct font *font =
24746 face->font ? face->font : FRAME_FONT (temp_it.f);
24747 int stretch_ascent =
24748 (((temp_it.ascent + temp_it.descent)
24749 * FONT_BASE (font)) / FONT_HEIGHT (font));
24751 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24752 temp_it.ascent + temp_it.descent,
24753 stretch_ascent);
24756 #endif
24758 temp_it.dp = NULL;
24759 temp_it.what = IT_CHARACTER;
24760 temp_it.len = 1;
24761 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24762 temp_it.face_id = GLYPH_FACE (glyph);
24763 temp_it.len = CHAR_BYTES (temp_it.c);
24765 PRODUCE_GLYPHS (&temp_it);
24766 it->pixel_width = temp_it.pixel_width;
24767 it->nglyphs = temp_it.pixel_width;
24770 #ifdef HAVE_WINDOW_SYSTEM
24772 /* Calculate line-height and line-spacing properties.
24773 An integer value specifies explicit pixel value.
24774 A float value specifies relative value to current face height.
24775 A cons (float . face-name) specifies relative value to
24776 height of specified face font.
24778 Returns height in pixels, or nil. */
24781 static Lisp_Object
24782 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24783 int boff, int override)
24785 Lisp_Object face_name = Qnil;
24786 int ascent, descent, height;
24788 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24789 return val;
24791 if (CONSP (val))
24793 face_name = XCAR (val);
24794 val = XCDR (val);
24795 if (!NUMBERP (val))
24796 val = make_number (1);
24797 if (NILP (face_name))
24799 height = it->ascent + it->descent;
24800 goto scale;
24804 if (NILP (face_name))
24806 font = FRAME_FONT (it->f);
24807 boff = FRAME_BASELINE_OFFSET (it->f);
24809 else if (EQ (face_name, Qt))
24811 override = 0;
24813 else
24815 int face_id;
24816 struct face *face;
24818 face_id = lookup_named_face (it->f, face_name, 0);
24819 if (face_id < 0)
24820 return make_number (-1);
24822 face = FACE_FROM_ID (it->f, face_id);
24823 font = face->font;
24824 if (font == NULL)
24825 return make_number (-1);
24826 boff = font->baseline_offset;
24827 if (font->vertical_centering)
24828 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24831 ascent = FONT_BASE (font) + boff;
24832 descent = FONT_DESCENT (font) - boff;
24834 if (override)
24836 it->override_ascent = ascent;
24837 it->override_descent = descent;
24838 it->override_boff = boff;
24841 height = ascent + descent;
24843 scale:
24844 if (FLOATP (val))
24845 height = (int)(XFLOAT_DATA (val) * height);
24846 else if (INTEGERP (val))
24847 height *= XINT (val);
24849 return make_number (height);
24853 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24854 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24855 and only if this is for a character for which no font was found.
24857 If the display method (it->glyphless_method) is
24858 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24859 length of the acronym or the hexadecimal string, UPPER_XOFF and
24860 UPPER_YOFF are pixel offsets for the upper part of the string,
24861 LOWER_XOFF and LOWER_YOFF are for the lower part.
24863 For the other display methods, LEN through LOWER_YOFF are zero. */
24865 static void
24866 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24867 short upper_xoff, short upper_yoff,
24868 short lower_xoff, short lower_yoff)
24870 struct glyph *glyph;
24871 enum glyph_row_area area = it->area;
24873 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24874 if (glyph < it->glyph_row->glyphs[area + 1])
24876 /* If the glyph row is reversed, we need to prepend the glyph
24877 rather than append it. */
24878 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24880 struct glyph *g;
24882 /* Make room for the additional glyph. */
24883 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24884 g[1] = *g;
24885 glyph = it->glyph_row->glyphs[area];
24887 glyph->charpos = CHARPOS (it->position);
24888 glyph->object = it->object;
24889 glyph->pixel_width = it->pixel_width;
24890 glyph->ascent = it->ascent;
24891 glyph->descent = it->descent;
24892 glyph->voffset = it->voffset;
24893 glyph->type = GLYPHLESS_GLYPH;
24894 glyph->u.glyphless.method = it->glyphless_method;
24895 glyph->u.glyphless.for_no_font = for_no_font;
24896 glyph->u.glyphless.len = len;
24897 glyph->u.glyphless.ch = it->c;
24898 glyph->slice.glyphless.upper_xoff = upper_xoff;
24899 glyph->slice.glyphless.upper_yoff = upper_yoff;
24900 glyph->slice.glyphless.lower_xoff = lower_xoff;
24901 glyph->slice.glyphless.lower_yoff = lower_yoff;
24902 glyph->avoid_cursor_p = it->avoid_cursor_p;
24903 glyph->multibyte_p = it->multibyte_p;
24904 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24906 /* In R2L rows, the left and the right box edges need to be
24907 drawn in reverse direction. */
24908 glyph->right_box_line_p = it->start_of_box_run_p;
24909 glyph->left_box_line_p = it->end_of_box_run_p;
24911 else
24913 glyph->left_box_line_p = it->start_of_box_run_p;
24914 glyph->right_box_line_p = it->end_of_box_run_p;
24916 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24917 || it->phys_descent > it->descent);
24918 glyph->padding_p = 0;
24919 glyph->glyph_not_available_p = 0;
24920 glyph->face_id = face_id;
24921 glyph->font_type = FONT_TYPE_UNKNOWN;
24922 if (it->bidi_p)
24924 glyph->resolved_level = it->bidi_it.resolved_level;
24925 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24926 emacs_abort ();
24927 glyph->bidi_type = it->bidi_it.type;
24929 ++it->glyph_row->used[area];
24931 else
24932 IT_EXPAND_MATRIX_WIDTH (it, area);
24936 /* Produce a glyph for a glyphless character for iterator IT.
24937 IT->glyphless_method specifies which method to use for displaying
24938 the character. See the description of enum
24939 glyphless_display_method in dispextern.h for the detail.
24941 FOR_NO_FONT is nonzero if and only if this is for a character for
24942 which no font was found. ACRONYM, if non-nil, is an acronym string
24943 for the character. */
24945 static void
24946 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24948 int face_id;
24949 struct face *face;
24950 struct font *font;
24951 int base_width, base_height, width, height;
24952 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24953 int len;
24955 /* Get the metrics of the base font. We always refer to the current
24956 ASCII face. */
24957 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24958 font = face->font ? face->font : FRAME_FONT (it->f);
24959 it->ascent = FONT_BASE (font) + font->baseline_offset;
24960 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24961 base_height = it->ascent + it->descent;
24962 base_width = font->average_width;
24964 /* Get a face ID for the glyph by utilizing a cache (the same way as
24965 done for `escape-glyph' in get_next_display_element). */
24966 if (it->f == last_glyphless_glyph_frame
24967 && it->face_id == last_glyphless_glyph_face_id)
24969 face_id = last_glyphless_glyph_merged_face_id;
24971 else
24973 /* Merge the `glyphless-char' face into the current face. */
24974 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24975 last_glyphless_glyph_frame = it->f;
24976 last_glyphless_glyph_face_id = it->face_id;
24977 last_glyphless_glyph_merged_face_id = face_id;
24980 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24982 it->pixel_width = THIN_SPACE_WIDTH;
24983 len = 0;
24984 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24986 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24988 width = CHAR_WIDTH (it->c);
24989 if (width == 0)
24990 width = 1;
24991 else if (width > 4)
24992 width = 4;
24993 it->pixel_width = base_width * width;
24994 len = 0;
24995 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24997 else
24999 char buf[7];
25000 const char *str;
25001 unsigned int code[6];
25002 int upper_len;
25003 int ascent, descent;
25004 struct font_metrics metrics_upper, metrics_lower;
25006 face = FACE_FROM_ID (it->f, face_id);
25007 font = face->font ? face->font : FRAME_FONT (it->f);
25008 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25010 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25012 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25013 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25014 if (CONSP (acronym))
25015 acronym = XCAR (acronym);
25016 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25018 else
25020 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25021 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25022 str = buf;
25024 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25025 code[len] = font->driver->encode_char (font, str[len]);
25026 upper_len = (len + 1) / 2;
25027 font->driver->text_extents (font, code, upper_len,
25028 &metrics_upper);
25029 font->driver->text_extents (font, code + upper_len, len - upper_len,
25030 &metrics_lower);
25034 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25035 width = max (metrics_upper.width, metrics_lower.width) + 4;
25036 upper_xoff = upper_yoff = 2; /* the typical case */
25037 if (base_width >= width)
25039 /* Align the upper to the left, the lower to the right. */
25040 it->pixel_width = base_width;
25041 lower_xoff = base_width - 2 - metrics_lower.width;
25043 else
25045 /* Center the shorter one. */
25046 it->pixel_width = width;
25047 if (metrics_upper.width >= metrics_lower.width)
25048 lower_xoff = (width - metrics_lower.width) / 2;
25049 else
25051 /* FIXME: This code doesn't look right. It formerly was
25052 missing the "lower_xoff = 0;", which couldn't have
25053 been right since it left lower_xoff uninitialized. */
25054 lower_xoff = 0;
25055 upper_xoff = (width - metrics_upper.width) / 2;
25059 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25060 top, bottom, and between upper and lower strings. */
25061 height = (metrics_upper.ascent + metrics_upper.descent
25062 + metrics_lower.ascent + metrics_lower.descent) + 5;
25063 /* Center vertically.
25064 H:base_height, D:base_descent
25065 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25067 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25068 descent = D - H/2 + h/2;
25069 lower_yoff = descent - 2 - ld;
25070 upper_yoff = lower_yoff - la - 1 - ud; */
25071 ascent = - (it->descent - (base_height + height + 1) / 2);
25072 descent = it->descent - (base_height - height) / 2;
25073 lower_yoff = descent - 2 - metrics_lower.descent;
25074 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25075 - metrics_upper.descent);
25076 /* Don't make the height shorter than the base height. */
25077 if (height > base_height)
25079 it->ascent = ascent;
25080 it->descent = descent;
25084 it->phys_ascent = it->ascent;
25085 it->phys_descent = it->descent;
25086 if (it->glyph_row)
25087 append_glyphless_glyph (it, face_id, for_no_font, len,
25088 upper_xoff, upper_yoff,
25089 lower_xoff, lower_yoff);
25090 it->nglyphs = 1;
25091 take_vertical_position_into_account (it);
25095 /* RIF:
25096 Produce glyphs/get display metrics for the display element IT is
25097 loaded with. See the description of struct it in dispextern.h
25098 for an overview of struct it. */
25100 void
25101 x_produce_glyphs (struct it *it)
25103 int extra_line_spacing = it->extra_line_spacing;
25105 it->glyph_not_available_p = 0;
25107 if (it->what == IT_CHARACTER)
25109 XChar2b char2b;
25110 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25111 struct font *font = face->font;
25112 struct font_metrics *pcm = NULL;
25113 int boff; /* baseline offset */
25115 if (font == NULL)
25117 /* When no suitable font is found, display this character by
25118 the method specified in the first extra slot of
25119 Vglyphless_char_display. */
25120 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25122 eassert (it->what == IT_GLYPHLESS);
25123 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25124 goto done;
25127 boff = font->baseline_offset;
25128 if (font->vertical_centering)
25129 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25131 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25133 int stretched_p;
25135 it->nglyphs = 1;
25137 if (it->override_ascent >= 0)
25139 it->ascent = it->override_ascent;
25140 it->descent = it->override_descent;
25141 boff = it->override_boff;
25143 else
25145 it->ascent = FONT_BASE (font) + boff;
25146 it->descent = FONT_DESCENT (font) - boff;
25149 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25151 pcm = get_per_char_metric (font, &char2b);
25152 if (pcm->width == 0
25153 && pcm->rbearing == 0 && pcm->lbearing == 0)
25154 pcm = NULL;
25157 if (pcm)
25159 it->phys_ascent = pcm->ascent + boff;
25160 it->phys_descent = pcm->descent - boff;
25161 it->pixel_width = pcm->width;
25163 else
25165 it->glyph_not_available_p = 1;
25166 it->phys_ascent = it->ascent;
25167 it->phys_descent = it->descent;
25168 it->pixel_width = font->space_width;
25171 if (it->constrain_row_ascent_descent_p)
25173 if (it->descent > it->max_descent)
25175 it->ascent += it->descent - it->max_descent;
25176 it->descent = it->max_descent;
25178 if (it->ascent > it->max_ascent)
25180 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25181 it->ascent = it->max_ascent;
25183 it->phys_ascent = min (it->phys_ascent, it->ascent);
25184 it->phys_descent = min (it->phys_descent, it->descent);
25185 extra_line_spacing = 0;
25188 /* If this is a space inside a region of text with
25189 `space-width' property, change its width. */
25190 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25191 if (stretched_p)
25192 it->pixel_width *= XFLOATINT (it->space_width);
25194 /* If face has a box, add the box thickness to the character
25195 height. If character has a box line to the left and/or
25196 right, add the box line width to the character's width. */
25197 if (face->box != FACE_NO_BOX)
25199 int thick = face->box_line_width;
25201 if (thick > 0)
25203 it->ascent += thick;
25204 it->descent += thick;
25206 else
25207 thick = -thick;
25209 if (it->start_of_box_run_p)
25210 it->pixel_width += thick;
25211 if (it->end_of_box_run_p)
25212 it->pixel_width += thick;
25215 /* If face has an overline, add the height of the overline
25216 (1 pixel) and a 1 pixel margin to the character height. */
25217 if (face->overline_p)
25218 it->ascent += overline_margin;
25220 if (it->constrain_row_ascent_descent_p)
25222 if (it->ascent > it->max_ascent)
25223 it->ascent = it->max_ascent;
25224 if (it->descent > it->max_descent)
25225 it->descent = it->max_descent;
25228 take_vertical_position_into_account (it);
25230 /* If we have to actually produce glyphs, do it. */
25231 if (it->glyph_row)
25233 if (stretched_p)
25235 /* Translate a space with a `space-width' property
25236 into a stretch glyph. */
25237 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25238 / FONT_HEIGHT (font));
25239 append_stretch_glyph (it, it->object, it->pixel_width,
25240 it->ascent + it->descent, ascent);
25242 else
25243 append_glyph (it);
25245 /* If characters with lbearing or rbearing are displayed
25246 in this line, record that fact in a flag of the
25247 glyph row. This is used to optimize X output code. */
25248 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25249 it->glyph_row->contains_overlapping_glyphs_p = 1;
25251 if (! stretched_p && it->pixel_width == 0)
25252 /* We assure that all visible glyphs have at least 1-pixel
25253 width. */
25254 it->pixel_width = 1;
25256 else if (it->char_to_display == '\n')
25258 /* A newline has no width, but we need the height of the
25259 line. But if previous part of the line sets a height,
25260 don't increase that height */
25262 Lisp_Object height;
25263 Lisp_Object total_height = Qnil;
25265 it->override_ascent = -1;
25266 it->pixel_width = 0;
25267 it->nglyphs = 0;
25269 height = get_it_property (it, Qline_height);
25270 /* Split (line-height total-height) list */
25271 if (CONSP (height)
25272 && CONSP (XCDR (height))
25273 && NILP (XCDR (XCDR (height))))
25275 total_height = XCAR (XCDR (height));
25276 height = XCAR (height);
25278 height = calc_line_height_property (it, height, font, boff, 1);
25280 if (it->override_ascent >= 0)
25282 it->ascent = it->override_ascent;
25283 it->descent = it->override_descent;
25284 boff = it->override_boff;
25286 else
25288 it->ascent = FONT_BASE (font) + boff;
25289 it->descent = FONT_DESCENT (font) - boff;
25292 if (EQ (height, Qt))
25294 if (it->descent > it->max_descent)
25296 it->ascent += it->descent - it->max_descent;
25297 it->descent = it->max_descent;
25299 if (it->ascent > it->max_ascent)
25301 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25302 it->ascent = it->max_ascent;
25304 it->phys_ascent = min (it->phys_ascent, it->ascent);
25305 it->phys_descent = min (it->phys_descent, it->descent);
25306 it->constrain_row_ascent_descent_p = 1;
25307 extra_line_spacing = 0;
25309 else
25311 Lisp_Object spacing;
25313 it->phys_ascent = it->ascent;
25314 it->phys_descent = it->descent;
25316 if ((it->max_ascent > 0 || it->max_descent > 0)
25317 && face->box != FACE_NO_BOX
25318 && face->box_line_width > 0)
25320 it->ascent += face->box_line_width;
25321 it->descent += face->box_line_width;
25323 if (!NILP (height)
25324 && XINT (height) > it->ascent + it->descent)
25325 it->ascent = XINT (height) - it->descent;
25327 if (!NILP (total_height))
25328 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25329 else
25331 spacing = get_it_property (it, Qline_spacing);
25332 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25334 if (INTEGERP (spacing))
25336 extra_line_spacing = XINT (spacing);
25337 if (!NILP (total_height))
25338 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25342 else /* i.e. (it->char_to_display == '\t') */
25344 if (font->space_width > 0)
25346 int tab_width = it->tab_width * font->space_width;
25347 int x = it->current_x + it->continuation_lines_width;
25348 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25350 /* If the distance from the current position to the next tab
25351 stop is less than a space character width, use the
25352 tab stop after that. */
25353 if (next_tab_x - x < font->space_width)
25354 next_tab_x += tab_width;
25356 it->pixel_width = next_tab_x - x;
25357 it->nglyphs = 1;
25358 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25359 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25361 if (it->glyph_row)
25363 append_stretch_glyph (it, it->object, it->pixel_width,
25364 it->ascent + it->descent, it->ascent);
25367 else
25369 it->pixel_width = 0;
25370 it->nglyphs = 1;
25374 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25376 /* A static composition.
25378 Note: A composition is represented as one glyph in the
25379 glyph matrix. There are no padding glyphs.
25381 Important note: pixel_width, ascent, and descent are the
25382 values of what is drawn by draw_glyphs (i.e. the values of
25383 the overall glyphs composed). */
25384 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25385 int boff; /* baseline offset */
25386 struct composition *cmp = composition_table[it->cmp_it.id];
25387 int glyph_len = cmp->glyph_len;
25388 struct font *font = face->font;
25390 it->nglyphs = 1;
25392 /* If we have not yet calculated pixel size data of glyphs of
25393 the composition for the current face font, calculate them
25394 now. Theoretically, we have to check all fonts for the
25395 glyphs, but that requires much time and memory space. So,
25396 here we check only the font of the first glyph. This may
25397 lead to incorrect display, but it's very rare, and C-l
25398 (recenter-top-bottom) can correct the display anyway. */
25399 if (! cmp->font || cmp->font != font)
25401 /* Ascent and descent of the font of the first character
25402 of this composition (adjusted by baseline offset).
25403 Ascent and descent of overall glyphs should not be less
25404 than these, respectively. */
25405 int font_ascent, font_descent, font_height;
25406 /* Bounding box of the overall glyphs. */
25407 int leftmost, rightmost, lowest, highest;
25408 int lbearing, rbearing;
25409 int i, width, ascent, descent;
25410 int left_padded = 0, right_padded = 0;
25411 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25412 XChar2b char2b;
25413 struct font_metrics *pcm;
25414 int font_not_found_p;
25415 ptrdiff_t pos;
25417 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25418 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25419 break;
25420 if (glyph_len < cmp->glyph_len)
25421 right_padded = 1;
25422 for (i = 0; i < glyph_len; i++)
25424 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25425 break;
25426 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25428 if (i > 0)
25429 left_padded = 1;
25431 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25432 : IT_CHARPOS (*it));
25433 /* If no suitable font is found, use the default font. */
25434 font_not_found_p = font == NULL;
25435 if (font_not_found_p)
25437 face = face->ascii_face;
25438 font = face->font;
25440 boff = font->baseline_offset;
25441 if (font->vertical_centering)
25442 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25443 font_ascent = FONT_BASE (font) + boff;
25444 font_descent = FONT_DESCENT (font) - boff;
25445 font_height = FONT_HEIGHT (font);
25447 cmp->font = font;
25449 pcm = NULL;
25450 if (! font_not_found_p)
25452 get_char_face_and_encoding (it->f, c, it->face_id,
25453 &char2b, 0);
25454 pcm = get_per_char_metric (font, &char2b);
25457 /* Initialize the bounding box. */
25458 if (pcm)
25460 width = cmp->glyph_len > 0 ? pcm->width : 0;
25461 ascent = pcm->ascent;
25462 descent = pcm->descent;
25463 lbearing = pcm->lbearing;
25464 rbearing = pcm->rbearing;
25466 else
25468 width = cmp->glyph_len > 0 ? font->space_width : 0;
25469 ascent = FONT_BASE (font);
25470 descent = FONT_DESCENT (font);
25471 lbearing = 0;
25472 rbearing = width;
25475 rightmost = width;
25476 leftmost = 0;
25477 lowest = - descent + boff;
25478 highest = ascent + boff;
25480 if (! font_not_found_p
25481 && font->default_ascent
25482 && CHAR_TABLE_P (Vuse_default_ascent)
25483 && !NILP (Faref (Vuse_default_ascent,
25484 make_number (it->char_to_display))))
25485 highest = font->default_ascent + boff;
25487 /* Draw the first glyph at the normal position. It may be
25488 shifted to right later if some other glyphs are drawn
25489 at the left. */
25490 cmp->offsets[i * 2] = 0;
25491 cmp->offsets[i * 2 + 1] = boff;
25492 cmp->lbearing = lbearing;
25493 cmp->rbearing = rbearing;
25495 /* Set cmp->offsets for the remaining glyphs. */
25496 for (i++; i < glyph_len; i++)
25498 int left, right, btm, top;
25499 int ch = COMPOSITION_GLYPH (cmp, i);
25500 int face_id;
25501 struct face *this_face;
25503 if (ch == '\t')
25504 ch = ' ';
25505 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25506 this_face = FACE_FROM_ID (it->f, face_id);
25507 font = this_face->font;
25509 if (font == NULL)
25510 pcm = NULL;
25511 else
25513 get_char_face_and_encoding (it->f, ch, face_id,
25514 &char2b, 0);
25515 pcm = get_per_char_metric (font, &char2b);
25517 if (! pcm)
25518 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25519 else
25521 width = pcm->width;
25522 ascent = pcm->ascent;
25523 descent = pcm->descent;
25524 lbearing = pcm->lbearing;
25525 rbearing = pcm->rbearing;
25526 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25528 /* Relative composition with or without
25529 alternate chars. */
25530 left = (leftmost + rightmost - width) / 2;
25531 btm = - descent + boff;
25532 if (font->relative_compose
25533 && (! CHAR_TABLE_P (Vignore_relative_composition)
25534 || NILP (Faref (Vignore_relative_composition,
25535 make_number (ch)))))
25538 if (- descent >= font->relative_compose)
25539 /* One extra pixel between two glyphs. */
25540 btm = highest + 1;
25541 else if (ascent <= 0)
25542 /* One extra pixel between two glyphs. */
25543 btm = lowest - 1 - ascent - descent;
25546 else
25548 /* A composition rule is specified by an integer
25549 value that encodes global and new reference
25550 points (GREF and NREF). GREF and NREF are
25551 specified by numbers as below:
25553 0---1---2 -- ascent
25557 9--10--11 -- center
25559 ---3---4---5--- baseline
25561 6---7---8 -- descent
25563 int rule = COMPOSITION_RULE (cmp, i);
25564 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25566 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25567 grefx = gref % 3, nrefx = nref % 3;
25568 grefy = gref / 3, nrefy = nref / 3;
25569 if (xoff)
25570 xoff = font_height * (xoff - 128) / 256;
25571 if (yoff)
25572 yoff = font_height * (yoff - 128) / 256;
25574 left = (leftmost
25575 + grefx * (rightmost - leftmost) / 2
25576 - nrefx * width / 2
25577 + xoff);
25579 btm = ((grefy == 0 ? highest
25580 : grefy == 1 ? 0
25581 : grefy == 2 ? lowest
25582 : (highest + lowest) / 2)
25583 - (nrefy == 0 ? ascent + descent
25584 : nrefy == 1 ? descent - boff
25585 : nrefy == 2 ? 0
25586 : (ascent + descent) / 2)
25587 + yoff);
25590 cmp->offsets[i * 2] = left;
25591 cmp->offsets[i * 2 + 1] = btm + descent;
25593 /* Update the bounding box of the overall glyphs. */
25594 if (width > 0)
25596 right = left + width;
25597 if (left < leftmost)
25598 leftmost = left;
25599 if (right > rightmost)
25600 rightmost = right;
25602 top = btm + descent + ascent;
25603 if (top > highest)
25604 highest = top;
25605 if (btm < lowest)
25606 lowest = btm;
25608 if (cmp->lbearing > left + lbearing)
25609 cmp->lbearing = left + lbearing;
25610 if (cmp->rbearing < left + rbearing)
25611 cmp->rbearing = left + rbearing;
25615 /* If there are glyphs whose x-offsets are negative,
25616 shift all glyphs to the right and make all x-offsets
25617 non-negative. */
25618 if (leftmost < 0)
25620 for (i = 0; i < cmp->glyph_len; i++)
25621 cmp->offsets[i * 2] -= leftmost;
25622 rightmost -= leftmost;
25623 cmp->lbearing -= leftmost;
25624 cmp->rbearing -= leftmost;
25627 if (left_padded && cmp->lbearing < 0)
25629 for (i = 0; i < cmp->glyph_len; i++)
25630 cmp->offsets[i * 2] -= cmp->lbearing;
25631 rightmost -= cmp->lbearing;
25632 cmp->rbearing -= cmp->lbearing;
25633 cmp->lbearing = 0;
25635 if (right_padded && rightmost < cmp->rbearing)
25637 rightmost = cmp->rbearing;
25640 cmp->pixel_width = rightmost;
25641 cmp->ascent = highest;
25642 cmp->descent = - lowest;
25643 if (cmp->ascent < font_ascent)
25644 cmp->ascent = font_ascent;
25645 if (cmp->descent < font_descent)
25646 cmp->descent = font_descent;
25649 if (it->glyph_row
25650 && (cmp->lbearing < 0
25651 || cmp->rbearing > cmp->pixel_width))
25652 it->glyph_row->contains_overlapping_glyphs_p = 1;
25654 it->pixel_width = cmp->pixel_width;
25655 it->ascent = it->phys_ascent = cmp->ascent;
25656 it->descent = it->phys_descent = cmp->descent;
25657 if (face->box != FACE_NO_BOX)
25659 int thick = face->box_line_width;
25661 if (thick > 0)
25663 it->ascent += thick;
25664 it->descent += thick;
25666 else
25667 thick = - thick;
25669 if (it->start_of_box_run_p)
25670 it->pixel_width += thick;
25671 if (it->end_of_box_run_p)
25672 it->pixel_width += thick;
25675 /* If face has an overline, add the height of the overline
25676 (1 pixel) and a 1 pixel margin to the character height. */
25677 if (face->overline_p)
25678 it->ascent += overline_margin;
25680 take_vertical_position_into_account (it);
25681 if (it->ascent < 0)
25682 it->ascent = 0;
25683 if (it->descent < 0)
25684 it->descent = 0;
25686 if (it->glyph_row && cmp->glyph_len > 0)
25687 append_composite_glyph (it);
25689 else if (it->what == IT_COMPOSITION)
25691 /* A dynamic (automatic) composition. */
25692 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25693 Lisp_Object gstring;
25694 struct font_metrics metrics;
25696 it->nglyphs = 1;
25698 gstring = composition_gstring_from_id (it->cmp_it.id);
25699 it->pixel_width
25700 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25701 &metrics);
25702 if (it->glyph_row
25703 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25704 it->glyph_row->contains_overlapping_glyphs_p = 1;
25705 it->ascent = it->phys_ascent = metrics.ascent;
25706 it->descent = it->phys_descent = metrics.descent;
25707 if (face->box != FACE_NO_BOX)
25709 int thick = face->box_line_width;
25711 if (thick > 0)
25713 it->ascent += thick;
25714 it->descent += thick;
25716 else
25717 thick = - thick;
25719 if (it->start_of_box_run_p)
25720 it->pixel_width += thick;
25721 if (it->end_of_box_run_p)
25722 it->pixel_width += thick;
25724 /* If face has an overline, add the height of the overline
25725 (1 pixel) and a 1 pixel margin to the character height. */
25726 if (face->overline_p)
25727 it->ascent += overline_margin;
25728 take_vertical_position_into_account (it);
25729 if (it->ascent < 0)
25730 it->ascent = 0;
25731 if (it->descent < 0)
25732 it->descent = 0;
25734 if (it->glyph_row)
25735 append_composite_glyph (it);
25737 else if (it->what == IT_GLYPHLESS)
25738 produce_glyphless_glyph (it, 0, Qnil);
25739 else if (it->what == IT_IMAGE)
25740 produce_image_glyph (it);
25741 else if (it->what == IT_STRETCH)
25742 produce_stretch_glyph (it);
25744 done:
25745 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25746 because this isn't true for images with `:ascent 100'. */
25747 eassert (it->ascent >= 0 && it->descent >= 0);
25748 if (it->area == TEXT_AREA)
25749 it->current_x += it->pixel_width;
25751 if (extra_line_spacing > 0)
25753 it->descent += extra_line_spacing;
25754 if (extra_line_spacing > it->max_extra_line_spacing)
25755 it->max_extra_line_spacing = extra_line_spacing;
25758 it->max_ascent = max (it->max_ascent, it->ascent);
25759 it->max_descent = max (it->max_descent, it->descent);
25760 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25761 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25764 /* EXPORT for RIF:
25765 Output LEN glyphs starting at START at the nominal cursor position.
25766 Advance the nominal cursor over the text. The global variable
25767 updated_row is the glyph row being updated, and updated_area is the
25768 area of that row being updated. */
25770 void
25771 x_write_glyphs (struct window *w, struct glyph *start, int len)
25773 int x, hpos, chpos = w->phys_cursor.hpos;
25775 eassert (updated_row);
25776 /* When the window is hscrolled, cursor hpos can legitimately be out
25777 of bounds, but we draw the cursor at the corresponding window
25778 margin in that case. */
25779 if (!updated_row->reversed_p && chpos < 0)
25780 chpos = 0;
25781 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25782 chpos = updated_row->used[TEXT_AREA] - 1;
25784 block_input ();
25786 /* Write glyphs. */
25788 hpos = start - updated_row->glyphs[updated_area];
25789 x = draw_glyphs (w, output_cursor.x,
25790 updated_row, updated_area,
25791 hpos, hpos + len,
25792 DRAW_NORMAL_TEXT, 0);
25794 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25795 if (updated_area == TEXT_AREA
25796 && w->phys_cursor_on_p
25797 && w->phys_cursor.vpos == output_cursor.vpos
25798 && chpos >= hpos
25799 && chpos < hpos + len)
25800 w->phys_cursor_on_p = 0;
25802 unblock_input ();
25804 /* Advance the output cursor. */
25805 output_cursor.hpos += len;
25806 output_cursor.x = x;
25810 /* EXPORT for RIF:
25811 Insert LEN glyphs from START at the nominal cursor position. */
25813 void
25814 x_insert_glyphs (struct window *w, struct glyph *start, int len)
25816 struct frame *f;
25817 int line_height, shift_by_width, shifted_region_width;
25818 struct glyph_row *row;
25819 struct glyph *glyph;
25820 int frame_x, frame_y;
25821 ptrdiff_t hpos;
25823 eassert (updated_row);
25824 block_input ();
25825 f = XFRAME (WINDOW_FRAME (w));
25827 /* Get the height of the line we are in. */
25828 row = updated_row;
25829 line_height = row->height;
25831 /* Get the width of the glyphs to insert. */
25832 shift_by_width = 0;
25833 for (glyph = start; glyph < start + len; ++glyph)
25834 shift_by_width += glyph->pixel_width;
25836 /* Get the width of the region to shift right. */
25837 shifted_region_width = (window_box_width (w, updated_area)
25838 - output_cursor.x
25839 - shift_by_width);
25841 /* Shift right. */
25842 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25843 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25845 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25846 line_height, shift_by_width);
25848 /* Write the glyphs. */
25849 hpos = start - row->glyphs[updated_area];
25850 draw_glyphs (w, output_cursor.x, row, updated_area,
25851 hpos, hpos + len,
25852 DRAW_NORMAL_TEXT, 0);
25854 /* Advance the output cursor. */
25855 output_cursor.hpos += len;
25856 output_cursor.x += shift_by_width;
25857 unblock_input ();
25861 /* EXPORT for RIF:
25862 Erase the current text line from the nominal cursor position
25863 (inclusive) to pixel column TO_X (exclusive). The idea is that
25864 everything from TO_X onward is already erased.
25866 TO_X is a pixel position relative to updated_area of currently
25867 updated window W. TO_X == -1 means clear to the end of this area. */
25869 void
25870 x_clear_end_of_line (struct window *w, int to_x)
25872 struct frame *f;
25873 int max_x, min_y, max_y;
25874 int from_x, from_y, to_y;
25876 eassert (updated_row);
25877 f = XFRAME (w->frame);
25879 if (updated_row->full_width_p)
25880 max_x = WINDOW_TOTAL_WIDTH (w);
25881 else
25882 max_x = window_box_width (w, updated_area);
25883 max_y = window_text_bottom_y (w);
25885 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25886 of window. For TO_X > 0, truncate to end of drawing area. */
25887 if (to_x == 0)
25888 return;
25889 else if (to_x < 0)
25890 to_x = max_x;
25891 else
25892 to_x = min (to_x, max_x);
25894 to_y = min (max_y, output_cursor.y + updated_row->height);
25896 /* Notice if the cursor will be cleared by this operation. */
25897 if (!updated_row->full_width_p)
25898 notice_overwritten_cursor (w, updated_area,
25899 output_cursor.x, -1,
25900 updated_row->y,
25901 MATRIX_ROW_BOTTOM_Y (updated_row));
25903 from_x = output_cursor.x;
25905 /* Translate to frame coordinates. */
25906 if (updated_row->full_width_p)
25908 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25909 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25911 else
25913 int area_left = window_box_left (w, updated_area);
25914 from_x += area_left;
25915 to_x += area_left;
25918 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25919 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25920 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25922 /* Prevent inadvertently clearing to end of the X window. */
25923 if (to_x > from_x && to_y > from_y)
25925 block_input ();
25926 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25927 to_x - from_x, to_y - from_y);
25928 unblock_input ();
25932 #endif /* HAVE_WINDOW_SYSTEM */
25936 /***********************************************************************
25937 Cursor types
25938 ***********************************************************************/
25940 /* Value is the internal representation of the specified cursor type
25941 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25942 of the bar cursor. */
25944 static enum text_cursor_kinds
25945 get_specified_cursor_type (Lisp_Object arg, int *width)
25947 enum text_cursor_kinds type;
25949 if (NILP (arg))
25950 return NO_CURSOR;
25952 if (EQ (arg, Qbox))
25953 return FILLED_BOX_CURSOR;
25955 if (EQ (arg, Qhollow))
25956 return HOLLOW_BOX_CURSOR;
25958 if (EQ (arg, Qbar))
25960 *width = 2;
25961 return BAR_CURSOR;
25964 if (CONSP (arg)
25965 && EQ (XCAR (arg), Qbar)
25966 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25968 *width = XINT (XCDR (arg));
25969 return BAR_CURSOR;
25972 if (EQ (arg, Qhbar))
25974 *width = 2;
25975 return HBAR_CURSOR;
25978 if (CONSP (arg)
25979 && EQ (XCAR (arg), Qhbar)
25980 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25982 *width = XINT (XCDR (arg));
25983 return HBAR_CURSOR;
25986 /* Treat anything unknown as "hollow box cursor".
25987 It was bad to signal an error; people have trouble fixing
25988 .Xdefaults with Emacs, when it has something bad in it. */
25989 type = HOLLOW_BOX_CURSOR;
25991 return type;
25994 /* Set the default cursor types for specified frame. */
25995 void
25996 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25998 int width = 1;
25999 Lisp_Object tem;
26001 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26002 FRAME_CURSOR_WIDTH (f) = width;
26004 /* By default, set up the blink-off state depending on the on-state. */
26006 tem = Fassoc (arg, Vblink_cursor_alist);
26007 if (!NILP (tem))
26009 FRAME_BLINK_OFF_CURSOR (f)
26010 = get_specified_cursor_type (XCDR (tem), &width);
26011 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26013 else
26014 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26016 /* Make sure the cursor gets redrawn. */
26017 cursor_type_changed = 1;
26021 #ifdef HAVE_WINDOW_SYSTEM
26023 /* Return the cursor we want to be displayed in window W. Return
26024 width of bar/hbar cursor through WIDTH arg. Return with
26025 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26026 (i.e. if the `system caret' should track this cursor).
26028 In a mini-buffer window, we want the cursor only to appear if we
26029 are reading input from this window. For the selected window, we
26030 want the cursor type given by the frame parameter or buffer local
26031 setting of cursor-type. If explicitly marked off, draw no cursor.
26032 In all other cases, we want a hollow box cursor. */
26034 static enum text_cursor_kinds
26035 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26036 int *active_cursor)
26038 struct frame *f = XFRAME (w->frame);
26039 struct buffer *b = XBUFFER (w->contents);
26040 int cursor_type = DEFAULT_CURSOR;
26041 Lisp_Object alt_cursor;
26042 int non_selected = 0;
26044 *active_cursor = 1;
26046 /* Echo area */
26047 if (cursor_in_echo_area
26048 && FRAME_HAS_MINIBUF_P (f)
26049 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26051 if (w == XWINDOW (echo_area_window))
26053 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26055 *width = FRAME_CURSOR_WIDTH (f);
26056 return FRAME_DESIRED_CURSOR (f);
26058 else
26059 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26062 *active_cursor = 0;
26063 non_selected = 1;
26066 /* Detect a nonselected window or nonselected frame. */
26067 else if (w != XWINDOW (f->selected_window)
26068 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26070 *active_cursor = 0;
26072 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26073 return NO_CURSOR;
26075 non_selected = 1;
26078 /* Never display a cursor in a window in which cursor-type is nil. */
26079 if (NILP (BVAR (b, cursor_type)))
26080 return NO_CURSOR;
26082 /* Get the normal cursor type for this window. */
26083 if (EQ (BVAR (b, cursor_type), Qt))
26085 cursor_type = FRAME_DESIRED_CURSOR (f);
26086 *width = FRAME_CURSOR_WIDTH (f);
26088 else
26089 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26091 /* Use cursor-in-non-selected-windows instead
26092 for non-selected window or frame. */
26093 if (non_selected)
26095 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26096 if (!EQ (Qt, alt_cursor))
26097 return get_specified_cursor_type (alt_cursor, width);
26098 /* t means modify the normal cursor type. */
26099 if (cursor_type == FILLED_BOX_CURSOR)
26100 cursor_type = HOLLOW_BOX_CURSOR;
26101 else if (cursor_type == BAR_CURSOR && *width > 1)
26102 --*width;
26103 return cursor_type;
26106 /* Use normal cursor if not blinked off. */
26107 if (!w->cursor_off_p)
26109 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26111 if (cursor_type == FILLED_BOX_CURSOR)
26113 /* Using a block cursor on large images can be very annoying.
26114 So use a hollow cursor for "large" images.
26115 If image is not transparent (no mask), also use hollow cursor. */
26116 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26117 if (img != NULL && IMAGEP (img->spec))
26119 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26120 where N = size of default frame font size.
26121 This should cover most of the "tiny" icons people may use. */
26122 if (!img->mask
26123 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26124 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26125 cursor_type = HOLLOW_BOX_CURSOR;
26128 else if (cursor_type != NO_CURSOR)
26130 /* Display current only supports BOX and HOLLOW cursors for images.
26131 So for now, unconditionally use a HOLLOW cursor when cursor is
26132 not a solid box cursor. */
26133 cursor_type = HOLLOW_BOX_CURSOR;
26136 return cursor_type;
26139 /* Cursor is blinked off, so determine how to "toggle" it. */
26141 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26142 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26143 return get_specified_cursor_type (XCDR (alt_cursor), width);
26145 /* Then see if frame has specified a specific blink off cursor type. */
26146 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26148 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26149 return FRAME_BLINK_OFF_CURSOR (f);
26152 #if 0
26153 /* Some people liked having a permanently visible blinking cursor,
26154 while others had very strong opinions against it. So it was
26155 decided to remove it. KFS 2003-09-03 */
26157 /* Finally perform built-in cursor blinking:
26158 filled box <-> hollow box
26159 wide [h]bar <-> narrow [h]bar
26160 narrow [h]bar <-> no cursor
26161 other type <-> no cursor */
26163 if (cursor_type == FILLED_BOX_CURSOR)
26164 return HOLLOW_BOX_CURSOR;
26166 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26168 *width = 1;
26169 return cursor_type;
26171 #endif
26173 return NO_CURSOR;
26177 /* Notice when the text cursor of window W has been completely
26178 overwritten by a drawing operation that outputs glyphs in AREA
26179 starting at X0 and ending at X1 in the line starting at Y0 and
26180 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26181 the rest of the line after X0 has been written. Y coordinates
26182 are window-relative. */
26184 static void
26185 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26186 int x0, int x1, int y0, int y1)
26188 int cx0, cx1, cy0, cy1;
26189 struct glyph_row *row;
26191 if (!w->phys_cursor_on_p)
26192 return;
26193 if (area != TEXT_AREA)
26194 return;
26196 if (w->phys_cursor.vpos < 0
26197 || w->phys_cursor.vpos >= w->current_matrix->nrows
26198 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26199 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26200 return;
26202 if (row->cursor_in_fringe_p)
26204 row->cursor_in_fringe_p = 0;
26205 draw_fringe_bitmap (w, row, row->reversed_p);
26206 w->phys_cursor_on_p = 0;
26207 return;
26210 cx0 = w->phys_cursor.x;
26211 cx1 = cx0 + w->phys_cursor_width;
26212 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26213 return;
26215 /* The cursor image will be completely removed from the
26216 screen if the output area intersects the cursor area in
26217 y-direction. When we draw in [y0 y1[, and some part of
26218 the cursor is at y < y0, that part must have been drawn
26219 before. When scrolling, the cursor is erased before
26220 actually scrolling, so we don't come here. When not
26221 scrolling, the rows above the old cursor row must have
26222 changed, and in this case these rows must have written
26223 over the cursor image.
26225 Likewise if part of the cursor is below y1, with the
26226 exception of the cursor being in the first blank row at
26227 the buffer and window end because update_text_area
26228 doesn't draw that row. (Except when it does, but
26229 that's handled in update_text_area.) */
26231 cy0 = w->phys_cursor.y;
26232 cy1 = cy0 + w->phys_cursor_height;
26233 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26234 return;
26236 w->phys_cursor_on_p = 0;
26239 #endif /* HAVE_WINDOW_SYSTEM */
26242 /************************************************************************
26243 Mouse Face
26244 ************************************************************************/
26246 #ifdef HAVE_WINDOW_SYSTEM
26248 /* EXPORT for RIF:
26249 Fix the display of area AREA of overlapping row ROW in window W
26250 with respect to the overlapping part OVERLAPS. */
26252 void
26253 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26254 enum glyph_row_area area, int overlaps)
26256 int i, x;
26258 block_input ();
26260 x = 0;
26261 for (i = 0; i < row->used[area];)
26263 if (row->glyphs[area][i].overlaps_vertically_p)
26265 int start = i, start_x = x;
26269 x += row->glyphs[area][i].pixel_width;
26270 ++i;
26272 while (i < row->used[area]
26273 && row->glyphs[area][i].overlaps_vertically_p);
26275 draw_glyphs (w, start_x, row, area,
26276 start, i,
26277 DRAW_NORMAL_TEXT, overlaps);
26279 else
26281 x += row->glyphs[area][i].pixel_width;
26282 ++i;
26286 unblock_input ();
26290 /* EXPORT:
26291 Draw the cursor glyph of window W in glyph row ROW. See the
26292 comment of draw_glyphs for the meaning of HL. */
26294 void
26295 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26296 enum draw_glyphs_face hl)
26298 /* If cursor hpos is out of bounds, don't draw garbage. This can
26299 happen in mini-buffer windows when switching between echo area
26300 glyphs and mini-buffer. */
26301 if ((row->reversed_p
26302 ? (w->phys_cursor.hpos >= 0)
26303 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26305 int on_p = w->phys_cursor_on_p;
26306 int x1;
26307 int hpos = w->phys_cursor.hpos;
26309 /* When the window is hscrolled, cursor hpos can legitimately be
26310 out of bounds, but we draw the cursor at the corresponding
26311 window margin in that case. */
26312 if (!row->reversed_p && hpos < 0)
26313 hpos = 0;
26314 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26315 hpos = row->used[TEXT_AREA] - 1;
26317 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26318 hl, 0);
26319 w->phys_cursor_on_p = on_p;
26321 if (hl == DRAW_CURSOR)
26322 w->phys_cursor_width = x1 - w->phys_cursor.x;
26323 /* When we erase the cursor, and ROW is overlapped by other
26324 rows, make sure that these overlapping parts of other rows
26325 are redrawn. */
26326 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26328 w->phys_cursor_width = x1 - w->phys_cursor.x;
26330 if (row > w->current_matrix->rows
26331 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26332 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26333 OVERLAPS_ERASED_CURSOR);
26335 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26336 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26337 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26338 OVERLAPS_ERASED_CURSOR);
26344 /* EXPORT:
26345 Erase the image of a cursor of window W from the screen. */
26347 void
26348 erase_phys_cursor (struct window *w)
26350 struct frame *f = XFRAME (w->frame);
26351 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26352 int hpos = w->phys_cursor.hpos;
26353 int vpos = w->phys_cursor.vpos;
26354 int mouse_face_here_p = 0;
26355 struct glyph_matrix *active_glyphs = w->current_matrix;
26356 struct glyph_row *cursor_row;
26357 struct glyph *cursor_glyph;
26358 enum draw_glyphs_face hl;
26360 /* No cursor displayed or row invalidated => nothing to do on the
26361 screen. */
26362 if (w->phys_cursor_type == NO_CURSOR)
26363 goto mark_cursor_off;
26365 /* VPOS >= active_glyphs->nrows means that window has been resized.
26366 Don't bother to erase the cursor. */
26367 if (vpos >= active_glyphs->nrows)
26368 goto mark_cursor_off;
26370 /* If row containing cursor is marked invalid, there is nothing we
26371 can do. */
26372 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26373 if (!cursor_row->enabled_p)
26374 goto mark_cursor_off;
26376 /* If line spacing is > 0, old cursor may only be partially visible in
26377 window after split-window. So adjust visible height. */
26378 cursor_row->visible_height = min (cursor_row->visible_height,
26379 window_text_bottom_y (w) - cursor_row->y);
26381 /* If row is completely invisible, don't attempt to delete a cursor which
26382 isn't there. This can happen if cursor is at top of a window, and
26383 we switch to a buffer with a header line in that window. */
26384 if (cursor_row->visible_height <= 0)
26385 goto mark_cursor_off;
26387 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26388 if (cursor_row->cursor_in_fringe_p)
26390 cursor_row->cursor_in_fringe_p = 0;
26391 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26392 goto mark_cursor_off;
26395 /* This can happen when the new row is shorter than the old one.
26396 In this case, either draw_glyphs or clear_end_of_line
26397 should have cleared the cursor. Note that we wouldn't be
26398 able to erase the cursor in this case because we don't have a
26399 cursor glyph at hand. */
26400 if ((cursor_row->reversed_p
26401 ? (w->phys_cursor.hpos < 0)
26402 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26403 goto mark_cursor_off;
26405 /* When the window is hscrolled, cursor hpos can legitimately be out
26406 of bounds, but we draw the cursor at the corresponding window
26407 margin in that case. */
26408 if (!cursor_row->reversed_p && hpos < 0)
26409 hpos = 0;
26410 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26411 hpos = cursor_row->used[TEXT_AREA] - 1;
26413 /* If the cursor is in the mouse face area, redisplay that when
26414 we clear the cursor. */
26415 if (! NILP (hlinfo->mouse_face_window)
26416 && coords_in_mouse_face_p (w, hpos, vpos)
26417 /* Don't redraw the cursor's spot in mouse face if it is at the
26418 end of a line (on a newline). The cursor appears there, but
26419 mouse highlighting does not. */
26420 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26421 mouse_face_here_p = 1;
26423 /* Maybe clear the display under the cursor. */
26424 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26426 int x, y, left_x;
26427 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26428 int width;
26430 cursor_glyph = get_phys_cursor_glyph (w);
26431 if (cursor_glyph == NULL)
26432 goto mark_cursor_off;
26434 width = cursor_glyph->pixel_width;
26435 left_x = window_box_left_offset (w, TEXT_AREA);
26436 x = w->phys_cursor.x;
26437 if (x < left_x)
26438 width -= left_x - x;
26439 width = min (width, window_box_width (w, TEXT_AREA) - x);
26440 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26441 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26443 if (width > 0)
26444 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26447 /* Erase the cursor by redrawing the character underneath it. */
26448 if (mouse_face_here_p)
26449 hl = DRAW_MOUSE_FACE;
26450 else
26451 hl = DRAW_NORMAL_TEXT;
26452 draw_phys_cursor_glyph (w, cursor_row, hl);
26454 mark_cursor_off:
26455 w->phys_cursor_on_p = 0;
26456 w->phys_cursor_type = NO_CURSOR;
26460 /* EXPORT:
26461 Display or clear cursor of window W. If ON is zero, clear the
26462 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26463 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26465 void
26466 display_and_set_cursor (struct window *w, int on,
26467 int hpos, int vpos, int x, int y)
26469 struct frame *f = XFRAME (w->frame);
26470 int new_cursor_type;
26471 int new_cursor_width;
26472 int active_cursor;
26473 struct glyph_row *glyph_row;
26474 struct glyph *glyph;
26476 /* This is pointless on invisible frames, and dangerous on garbaged
26477 windows and frames; in the latter case, the frame or window may
26478 be in the midst of changing its size, and x and y may be off the
26479 window. */
26480 if (! FRAME_VISIBLE_P (f)
26481 || FRAME_GARBAGED_P (f)
26482 || vpos >= w->current_matrix->nrows
26483 || hpos >= w->current_matrix->matrix_w)
26484 return;
26486 /* If cursor is off and we want it off, return quickly. */
26487 if (!on && !w->phys_cursor_on_p)
26488 return;
26490 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26491 /* If cursor row is not enabled, we don't really know where to
26492 display the cursor. */
26493 if (!glyph_row->enabled_p)
26495 w->phys_cursor_on_p = 0;
26496 return;
26499 glyph = NULL;
26500 if (!glyph_row->exact_window_width_line_p
26501 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26502 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26504 eassert (input_blocked_p ());
26506 /* Set new_cursor_type to the cursor we want to be displayed. */
26507 new_cursor_type = get_window_cursor_type (w, glyph,
26508 &new_cursor_width, &active_cursor);
26510 /* If cursor is currently being shown and we don't want it to be or
26511 it is in the wrong place, or the cursor type is not what we want,
26512 erase it. */
26513 if (w->phys_cursor_on_p
26514 && (!on
26515 || w->phys_cursor.x != x
26516 || w->phys_cursor.y != y
26517 || new_cursor_type != w->phys_cursor_type
26518 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26519 && new_cursor_width != w->phys_cursor_width)))
26520 erase_phys_cursor (w);
26522 /* Don't check phys_cursor_on_p here because that flag is only set
26523 to zero in some cases where we know that the cursor has been
26524 completely erased, to avoid the extra work of erasing the cursor
26525 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26526 still not be visible, or it has only been partly erased. */
26527 if (on)
26529 w->phys_cursor_ascent = glyph_row->ascent;
26530 w->phys_cursor_height = glyph_row->height;
26532 /* Set phys_cursor_.* before x_draw_.* is called because some
26533 of them may need the information. */
26534 w->phys_cursor.x = x;
26535 w->phys_cursor.y = glyph_row->y;
26536 w->phys_cursor.hpos = hpos;
26537 w->phys_cursor.vpos = vpos;
26540 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26541 new_cursor_type, new_cursor_width,
26542 on, active_cursor);
26546 /* Switch the display of W's cursor on or off, according to the value
26547 of ON. */
26549 static void
26550 update_window_cursor (struct window *w, int on)
26552 /* Don't update cursor in windows whose frame is in the process
26553 of being deleted. */
26554 if (w->current_matrix)
26556 int hpos = w->phys_cursor.hpos;
26557 int vpos = w->phys_cursor.vpos;
26558 struct glyph_row *row;
26560 if (vpos >= w->current_matrix->nrows
26561 || hpos >= w->current_matrix->matrix_w)
26562 return;
26564 row = MATRIX_ROW (w->current_matrix, vpos);
26566 /* When the window is hscrolled, cursor hpos can legitimately be
26567 out of bounds, but we draw the cursor at the corresponding
26568 window margin in that case. */
26569 if (!row->reversed_p && hpos < 0)
26570 hpos = 0;
26571 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26572 hpos = row->used[TEXT_AREA] - 1;
26574 block_input ();
26575 display_and_set_cursor (w, on, hpos, vpos,
26576 w->phys_cursor.x, w->phys_cursor.y);
26577 unblock_input ();
26582 /* Call update_window_cursor with parameter ON_P on all leaf windows
26583 in the window tree rooted at W. */
26585 static void
26586 update_cursor_in_window_tree (struct window *w, int on_p)
26588 while (w)
26590 if (WINDOWP (w->contents))
26591 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26592 else
26593 update_window_cursor (w, on_p);
26595 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26600 /* EXPORT:
26601 Display the cursor on window W, or clear it, according to ON_P.
26602 Don't change the cursor's position. */
26604 void
26605 x_update_cursor (struct frame *f, int on_p)
26607 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26611 /* EXPORT:
26612 Clear the cursor of window W to background color, and mark the
26613 cursor as not shown. This is used when the text where the cursor
26614 is about to be rewritten. */
26616 void
26617 x_clear_cursor (struct window *w)
26619 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26620 update_window_cursor (w, 0);
26623 #endif /* HAVE_WINDOW_SYSTEM */
26625 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26626 and MSDOS. */
26627 static void
26628 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26629 int start_hpos, int end_hpos,
26630 enum draw_glyphs_face draw)
26632 #ifdef HAVE_WINDOW_SYSTEM
26633 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26635 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26636 return;
26638 #endif
26639 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26640 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26641 #endif
26644 /* Display the active region described by mouse_face_* according to DRAW. */
26646 static void
26647 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26649 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26650 struct frame *f = XFRAME (WINDOW_FRAME (w));
26652 if (/* If window is in the process of being destroyed, don't bother
26653 to do anything. */
26654 w->current_matrix != NULL
26655 /* Don't update mouse highlight if hidden */
26656 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26657 /* Recognize when we are called to operate on rows that don't exist
26658 anymore. This can happen when a window is split. */
26659 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26661 int phys_cursor_on_p = w->phys_cursor_on_p;
26662 struct glyph_row *row, *first, *last;
26664 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26665 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26667 for (row = first; row <= last && row->enabled_p; ++row)
26669 int start_hpos, end_hpos, start_x;
26671 /* For all but the first row, the highlight starts at column 0. */
26672 if (row == first)
26674 /* R2L rows have BEG and END in reversed order, but the
26675 screen drawing geometry is always left to right. So
26676 we need to mirror the beginning and end of the
26677 highlighted area in R2L rows. */
26678 if (!row->reversed_p)
26680 start_hpos = hlinfo->mouse_face_beg_col;
26681 start_x = hlinfo->mouse_face_beg_x;
26683 else if (row == last)
26685 start_hpos = hlinfo->mouse_face_end_col;
26686 start_x = hlinfo->mouse_face_end_x;
26688 else
26690 start_hpos = 0;
26691 start_x = 0;
26694 else if (row->reversed_p && row == last)
26696 start_hpos = hlinfo->mouse_face_end_col;
26697 start_x = hlinfo->mouse_face_end_x;
26699 else
26701 start_hpos = 0;
26702 start_x = 0;
26705 if (row == last)
26707 if (!row->reversed_p)
26708 end_hpos = hlinfo->mouse_face_end_col;
26709 else if (row == first)
26710 end_hpos = hlinfo->mouse_face_beg_col;
26711 else
26713 end_hpos = row->used[TEXT_AREA];
26714 if (draw == DRAW_NORMAL_TEXT)
26715 row->fill_line_p = 1; /* Clear to end of line */
26718 else if (row->reversed_p && row == first)
26719 end_hpos = hlinfo->mouse_face_beg_col;
26720 else
26722 end_hpos = row->used[TEXT_AREA];
26723 if (draw == DRAW_NORMAL_TEXT)
26724 row->fill_line_p = 1; /* Clear to end of line */
26727 if (end_hpos > start_hpos)
26729 draw_row_with_mouse_face (w, start_x, row,
26730 start_hpos, end_hpos, draw);
26732 row->mouse_face_p
26733 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26737 #ifdef HAVE_WINDOW_SYSTEM
26738 /* When we've written over the cursor, arrange for it to
26739 be displayed again. */
26740 if (FRAME_WINDOW_P (f)
26741 && phys_cursor_on_p && !w->phys_cursor_on_p)
26743 int hpos = w->phys_cursor.hpos;
26745 /* When the window is hscrolled, cursor hpos can legitimately be
26746 out of bounds, but we draw the cursor at the corresponding
26747 window margin in that case. */
26748 if (!row->reversed_p && hpos < 0)
26749 hpos = 0;
26750 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26751 hpos = row->used[TEXT_AREA] - 1;
26753 block_input ();
26754 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26755 w->phys_cursor.x, w->phys_cursor.y);
26756 unblock_input ();
26758 #endif /* HAVE_WINDOW_SYSTEM */
26761 #ifdef HAVE_WINDOW_SYSTEM
26762 /* Change the mouse cursor. */
26763 if (FRAME_WINDOW_P (f))
26765 if (draw == DRAW_NORMAL_TEXT
26766 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26767 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26768 else if (draw == DRAW_MOUSE_FACE)
26769 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26770 else
26771 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26773 #endif /* HAVE_WINDOW_SYSTEM */
26776 /* EXPORT:
26777 Clear out the mouse-highlighted active region.
26778 Redraw it un-highlighted first. Value is non-zero if mouse
26779 face was actually drawn unhighlighted. */
26782 clear_mouse_face (Mouse_HLInfo *hlinfo)
26784 int cleared = 0;
26786 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26788 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26789 cleared = 1;
26792 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26793 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26794 hlinfo->mouse_face_window = Qnil;
26795 hlinfo->mouse_face_overlay = Qnil;
26796 return cleared;
26799 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26800 within the mouse face on that window. */
26801 static int
26802 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26804 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26806 /* Quickly resolve the easy cases. */
26807 if (!(WINDOWP (hlinfo->mouse_face_window)
26808 && XWINDOW (hlinfo->mouse_face_window) == w))
26809 return 0;
26810 if (vpos < hlinfo->mouse_face_beg_row
26811 || vpos > hlinfo->mouse_face_end_row)
26812 return 0;
26813 if (vpos > hlinfo->mouse_face_beg_row
26814 && vpos < hlinfo->mouse_face_end_row)
26815 return 1;
26817 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26819 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26821 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26822 return 1;
26824 else if ((vpos == hlinfo->mouse_face_beg_row
26825 && hpos >= hlinfo->mouse_face_beg_col)
26826 || (vpos == hlinfo->mouse_face_end_row
26827 && hpos < hlinfo->mouse_face_end_col))
26828 return 1;
26830 else
26832 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26834 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26835 return 1;
26837 else if ((vpos == hlinfo->mouse_face_beg_row
26838 && hpos <= hlinfo->mouse_face_beg_col)
26839 || (vpos == hlinfo->mouse_face_end_row
26840 && hpos > hlinfo->mouse_face_end_col))
26841 return 1;
26843 return 0;
26847 /* EXPORT:
26848 Non-zero if physical cursor of window W is within mouse face. */
26851 cursor_in_mouse_face_p (struct window *w)
26853 int hpos = w->phys_cursor.hpos;
26854 int vpos = w->phys_cursor.vpos;
26855 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26857 /* When the window is hscrolled, cursor hpos can legitimately be out
26858 of bounds, but we draw the cursor at the corresponding window
26859 margin in that case. */
26860 if (!row->reversed_p && hpos < 0)
26861 hpos = 0;
26862 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26863 hpos = row->used[TEXT_AREA] - 1;
26865 return coords_in_mouse_face_p (w, hpos, vpos);
26870 /* Find the glyph rows START_ROW and END_ROW of window W that display
26871 characters between buffer positions START_CHARPOS and END_CHARPOS
26872 (excluding END_CHARPOS). DISP_STRING is a display string that
26873 covers these buffer positions. This is similar to
26874 row_containing_pos, but is more accurate when bidi reordering makes
26875 buffer positions change non-linearly with glyph rows. */
26876 static void
26877 rows_from_pos_range (struct window *w,
26878 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26879 Lisp_Object disp_string,
26880 struct glyph_row **start, struct glyph_row **end)
26882 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26883 int last_y = window_text_bottom_y (w);
26884 struct glyph_row *row;
26886 *start = NULL;
26887 *end = NULL;
26889 while (!first->enabled_p
26890 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26891 first++;
26893 /* Find the START row. */
26894 for (row = first;
26895 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26896 row++)
26898 /* A row can potentially be the START row if the range of the
26899 characters it displays intersects the range
26900 [START_CHARPOS..END_CHARPOS). */
26901 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26902 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26903 /* See the commentary in row_containing_pos, for the
26904 explanation of the complicated way to check whether
26905 some position is beyond the end of the characters
26906 displayed by a row. */
26907 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26908 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26909 && !row->ends_at_zv_p
26910 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26911 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26912 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26913 && !row->ends_at_zv_p
26914 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26916 /* Found a candidate row. Now make sure at least one of the
26917 glyphs it displays has a charpos from the range
26918 [START_CHARPOS..END_CHARPOS).
26920 This is not obvious because bidi reordering could make
26921 buffer positions of a row be 1,2,3,102,101,100, and if we
26922 want to highlight characters in [50..60), we don't want
26923 this row, even though [50..60) does intersect [1..103),
26924 the range of character positions given by the row's start
26925 and end positions. */
26926 struct glyph *g = row->glyphs[TEXT_AREA];
26927 struct glyph *e = g + row->used[TEXT_AREA];
26929 while (g < e)
26931 if (((BUFFERP (g->object) || INTEGERP (g->object))
26932 && start_charpos <= g->charpos && g->charpos < end_charpos)
26933 /* A glyph that comes from DISP_STRING is by
26934 definition to be highlighted. */
26935 || EQ (g->object, disp_string))
26936 *start = row;
26937 g++;
26939 if (*start)
26940 break;
26944 /* Find the END row. */
26945 if (!*start
26946 /* If the last row is partially visible, start looking for END
26947 from that row, instead of starting from FIRST. */
26948 && !(row->enabled_p
26949 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26950 row = first;
26951 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26953 struct glyph_row *next = row + 1;
26954 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26956 if (!next->enabled_p
26957 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26958 /* The first row >= START whose range of displayed characters
26959 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26960 is the row END + 1. */
26961 || (start_charpos < next_start
26962 && end_charpos < next_start)
26963 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26964 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26965 && !next->ends_at_zv_p
26966 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26967 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26968 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26969 && !next->ends_at_zv_p
26970 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26972 *end = row;
26973 break;
26975 else
26977 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26978 but none of the characters it displays are in the range, it is
26979 also END + 1. */
26980 struct glyph *g = next->glyphs[TEXT_AREA];
26981 struct glyph *s = g;
26982 struct glyph *e = g + next->used[TEXT_AREA];
26984 while (g < e)
26986 if (((BUFFERP (g->object) || INTEGERP (g->object))
26987 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26988 /* If the buffer position of the first glyph in
26989 the row is equal to END_CHARPOS, it means
26990 the last character to be highlighted is the
26991 newline of ROW, and we must consider NEXT as
26992 END, not END+1. */
26993 || (((!next->reversed_p && g == s)
26994 || (next->reversed_p && g == e - 1))
26995 && (g->charpos == end_charpos
26996 /* Special case for when NEXT is an
26997 empty line at ZV. */
26998 || (g->charpos == -1
26999 && !row->ends_at_zv_p
27000 && next_start == end_charpos)))))
27001 /* A glyph that comes from DISP_STRING is by
27002 definition to be highlighted. */
27003 || EQ (g->object, disp_string))
27004 break;
27005 g++;
27007 if (g == e)
27009 *end = row;
27010 break;
27012 /* The first row that ends at ZV must be the last to be
27013 highlighted. */
27014 else if (next->ends_at_zv_p)
27016 *end = next;
27017 break;
27023 /* This function sets the mouse_face_* elements of HLINFO, assuming
27024 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27025 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27026 for the overlay or run of text properties specifying the mouse
27027 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27028 before-string and after-string that must also be highlighted.
27029 DISP_STRING, if non-nil, is a display string that may cover some
27030 or all of the highlighted text. */
27032 static void
27033 mouse_face_from_buffer_pos (Lisp_Object window,
27034 Mouse_HLInfo *hlinfo,
27035 ptrdiff_t mouse_charpos,
27036 ptrdiff_t start_charpos,
27037 ptrdiff_t end_charpos,
27038 Lisp_Object before_string,
27039 Lisp_Object after_string,
27040 Lisp_Object disp_string)
27042 struct window *w = XWINDOW (window);
27043 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27044 struct glyph_row *r1, *r2;
27045 struct glyph *glyph, *end;
27046 ptrdiff_t ignore, pos;
27047 int x;
27049 eassert (NILP (disp_string) || STRINGP (disp_string));
27050 eassert (NILP (before_string) || STRINGP (before_string));
27051 eassert (NILP (after_string) || STRINGP (after_string));
27053 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27054 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27055 if (r1 == NULL)
27056 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27057 /* If the before-string or display-string contains newlines,
27058 rows_from_pos_range skips to its last row. Move back. */
27059 if (!NILP (before_string) || !NILP (disp_string))
27061 struct glyph_row *prev;
27062 while ((prev = r1 - 1, prev >= first)
27063 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27064 && prev->used[TEXT_AREA] > 0)
27066 struct glyph *beg = prev->glyphs[TEXT_AREA];
27067 glyph = beg + prev->used[TEXT_AREA];
27068 while (--glyph >= beg && INTEGERP (glyph->object));
27069 if (glyph < beg
27070 || !(EQ (glyph->object, before_string)
27071 || EQ (glyph->object, disp_string)))
27072 break;
27073 r1 = prev;
27076 if (r2 == NULL)
27078 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27079 hlinfo->mouse_face_past_end = 1;
27081 else if (!NILP (after_string))
27083 /* If the after-string has newlines, advance to its last row. */
27084 struct glyph_row *next;
27085 struct glyph_row *last
27086 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27088 for (next = r2 + 1;
27089 next <= last
27090 && next->used[TEXT_AREA] > 0
27091 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27092 ++next)
27093 r2 = next;
27095 /* The rest of the display engine assumes that mouse_face_beg_row is
27096 either above mouse_face_end_row or identical to it. But with
27097 bidi-reordered continued lines, the row for START_CHARPOS could
27098 be below the row for END_CHARPOS. If so, swap the rows and store
27099 them in correct order. */
27100 if (r1->y > r2->y)
27102 struct glyph_row *tem = r2;
27104 r2 = r1;
27105 r1 = tem;
27108 hlinfo->mouse_face_beg_y = r1->y;
27109 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27110 hlinfo->mouse_face_end_y = r2->y;
27111 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27113 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27114 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27115 could be anywhere in the row and in any order. The strategy
27116 below is to find the leftmost and the rightmost glyph that
27117 belongs to either of these 3 strings, or whose position is
27118 between START_CHARPOS and END_CHARPOS, and highlight all the
27119 glyphs between those two. This may cover more than just the text
27120 between START_CHARPOS and END_CHARPOS if the range of characters
27121 strides the bidi level boundary, e.g. if the beginning is in R2L
27122 text while the end is in L2R text or vice versa. */
27123 if (!r1->reversed_p)
27125 /* This row is in a left to right paragraph. Scan it left to
27126 right. */
27127 glyph = r1->glyphs[TEXT_AREA];
27128 end = glyph + r1->used[TEXT_AREA];
27129 x = r1->x;
27131 /* Skip truncation glyphs at the start of the glyph row. */
27132 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27133 for (; glyph < end
27134 && INTEGERP (glyph->object)
27135 && glyph->charpos < 0;
27136 ++glyph)
27137 x += glyph->pixel_width;
27139 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27140 or DISP_STRING, and the first glyph from buffer whose
27141 position is between START_CHARPOS and END_CHARPOS. */
27142 for (; glyph < end
27143 && !INTEGERP (glyph->object)
27144 && !EQ (glyph->object, disp_string)
27145 && !(BUFFERP (glyph->object)
27146 && (glyph->charpos >= start_charpos
27147 && glyph->charpos < end_charpos));
27148 ++glyph)
27150 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27151 are present at buffer positions between START_CHARPOS and
27152 END_CHARPOS, or if they come from an overlay. */
27153 if (EQ (glyph->object, before_string))
27155 pos = string_buffer_position (before_string,
27156 start_charpos);
27157 /* If pos == 0, it means before_string came from an
27158 overlay, not from a buffer position. */
27159 if (!pos || (pos >= start_charpos && pos < end_charpos))
27160 break;
27162 else if (EQ (glyph->object, after_string))
27164 pos = string_buffer_position (after_string, end_charpos);
27165 if (!pos || (pos >= start_charpos && pos < end_charpos))
27166 break;
27168 x += glyph->pixel_width;
27170 hlinfo->mouse_face_beg_x = x;
27171 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27173 else
27175 /* This row is in a right to left paragraph. Scan it right to
27176 left. */
27177 struct glyph *g;
27179 end = r1->glyphs[TEXT_AREA] - 1;
27180 glyph = end + r1->used[TEXT_AREA];
27182 /* Skip truncation glyphs at the start of the glyph row. */
27183 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27184 for (; glyph > end
27185 && INTEGERP (glyph->object)
27186 && glyph->charpos < 0;
27187 --glyph)
27190 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27191 or DISP_STRING, and the first glyph from buffer whose
27192 position is between START_CHARPOS and END_CHARPOS. */
27193 for (; glyph > end
27194 && !INTEGERP (glyph->object)
27195 && !EQ (glyph->object, disp_string)
27196 && !(BUFFERP (glyph->object)
27197 && (glyph->charpos >= start_charpos
27198 && glyph->charpos < end_charpos));
27199 --glyph)
27201 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27202 are present at buffer positions between START_CHARPOS and
27203 END_CHARPOS, or if they come from an overlay. */
27204 if (EQ (glyph->object, before_string))
27206 pos = string_buffer_position (before_string, start_charpos);
27207 /* If pos == 0, it means before_string came from an
27208 overlay, not from a buffer position. */
27209 if (!pos || (pos >= start_charpos && pos < end_charpos))
27210 break;
27212 else if (EQ (glyph->object, after_string))
27214 pos = string_buffer_position (after_string, end_charpos);
27215 if (!pos || (pos >= start_charpos && pos < end_charpos))
27216 break;
27220 glyph++; /* first glyph to the right of the highlighted area */
27221 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27222 x += g->pixel_width;
27223 hlinfo->mouse_face_beg_x = x;
27224 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27227 /* If the highlight ends in a different row, compute GLYPH and END
27228 for the end row. Otherwise, reuse the values computed above for
27229 the row where the highlight begins. */
27230 if (r2 != r1)
27232 if (!r2->reversed_p)
27234 glyph = r2->glyphs[TEXT_AREA];
27235 end = glyph + r2->used[TEXT_AREA];
27236 x = r2->x;
27238 else
27240 end = r2->glyphs[TEXT_AREA] - 1;
27241 glyph = end + r2->used[TEXT_AREA];
27245 if (!r2->reversed_p)
27247 /* Skip truncation and continuation glyphs near the end of the
27248 row, and also blanks and stretch glyphs inserted by
27249 extend_face_to_end_of_line. */
27250 while (end > glyph
27251 && INTEGERP ((end - 1)->object))
27252 --end;
27253 /* Scan the rest of the glyph row from the end, looking for the
27254 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27255 DISP_STRING, or whose position is between START_CHARPOS
27256 and END_CHARPOS */
27257 for (--end;
27258 end > glyph
27259 && !INTEGERP (end->object)
27260 && !EQ (end->object, disp_string)
27261 && !(BUFFERP (end->object)
27262 && (end->charpos >= start_charpos
27263 && end->charpos < end_charpos));
27264 --end)
27266 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27267 are present at buffer positions between START_CHARPOS and
27268 END_CHARPOS, or if they come from an overlay. */
27269 if (EQ (end->object, before_string))
27271 pos = string_buffer_position (before_string, start_charpos);
27272 if (!pos || (pos >= start_charpos && pos < end_charpos))
27273 break;
27275 else if (EQ (end->object, after_string))
27277 pos = string_buffer_position (after_string, end_charpos);
27278 if (!pos || (pos >= start_charpos && pos < end_charpos))
27279 break;
27282 /* Find the X coordinate of the last glyph to be highlighted. */
27283 for (; glyph <= end; ++glyph)
27284 x += glyph->pixel_width;
27286 hlinfo->mouse_face_end_x = x;
27287 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27289 else
27291 /* Skip truncation and continuation glyphs near the end of the
27292 row, and also blanks and stretch glyphs inserted by
27293 extend_face_to_end_of_line. */
27294 x = r2->x;
27295 end++;
27296 while (end < glyph
27297 && INTEGERP (end->object))
27299 x += end->pixel_width;
27300 ++end;
27302 /* Scan the rest of the glyph row from the end, looking for the
27303 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27304 DISP_STRING, or whose position is between START_CHARPOS
27305 and END_CHARPOS */
27306 for ( ;
27307 end < glyph
27308 && !INTEGERP (end->object)
27309 && !EQ (end->object, disp_string)
27310 && !(BUFFERP (end->object)
27311 && (end->charpos >= start_charpos
27312 && end->charpos < end_charpos));
27313 ++end)
27315 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27316 are present at buffer positions between START_CHARPOS and
27317 END_CHARPOS, or if they come from an overlay. */
27318 if (EQ (end->object, before_string))
27320 pos = string_buffer_position (before_string, start_charpos);
27321 if (!pos || (pos >= start_charpos && pos < end_charpos))
27322 break;
27324 else if (EQ (end->object, after_string))
27326 pos = string_buffer_position (after_string, end_charpos);
27327 if (!pos || (pos >= start_charpos && pos < end_charpos))
27328 break;
27330 x += end->pixel_width;
27332 /* If we exited the above loop because we arrived at the last
27333 glyph of the row, and its buffer position is still not in
27334 range, it means the last character in range is the preceding
27335 newline. Bump the end column and x values to get past the
27336 last glyph. */
27337 if (end == glyph
27338 && BUFFERP (end->object)
27339 && (end->charpos < start_charpos
27340 || end->charpos >= end_charpos))
27342 x += end->pixel_width;
27343 ++end;
27345 hlinfo->mouse_face_end_x = x;
27346 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27349 hlinfo->mouse_face_window = window;
27350 hlinfo->mouse_face_face_id
27351 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27352 mouse_charpos + 1,
27353 !hlinfo->mouse_face_hidden, -1);
27354 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27357 /* The following function is not used anymore (replaced with
27358 mouse_face_from_string_pos), but I leave it here for the time
27359 being, in case someone would. */
27361 #if 0 /* not used */
27363 /* Find the position of the glyph for position POS in OBJECT in
27364 window W's current matrix, and return in *X, *Y the pixel
27365 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27367 RIGHT_P non-zero means return the position of the right edge of the
27368 glyph, RIGHT_P zero means return the left edge position.
27370 If no glyph for POS exists in the matrix, return the position of
27371 the glyph with the next smaller position that is in the matrix, if
27372 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27373 exists in the matrix, return the position of the glyph with the
27374 next larger position in OBJECT.
27376 Value is non-zero if a glyph was found. */
27378 static int
27379 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27380 int *hpos, int *vpos, int *x, int *y, int right_p)
27382 int yb = window_text_bottom_y (w);
27383 struct glyph_row *r;
27384 struct glyph *best_glyph = NULL;
27385 struct glyph_row *best_row = NULL;
27386 int best_x = 0;
27388 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27389 r->enabled_p && r->y < yb;
27390 ++r)
27392 struct glyph *g = r->glyphs[TEXT_AREA];
27393 struct glyph *e = g + r->used[TEXT_AREA];
27394 int gx;
27396 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27397 if (EQ (g->object, object))
27399 if (g->charpos == pos)
27401 best_glyph = g;
27402 best_x = gx;
27403 best_row = r;
27404 goto found;
27406 else if (best_glyph == NULL
27407 || ((eabs (g->charpos - pos)
27408 < eabs (best_glyph->charpos - pos))
27409 && (right_p
27410 ? g->charpos < pos
27411 : g->charpos > pos)))
27413 best_glyph = g;
27414 best_x = gx;
27415 best_row = r;
27420 found:
27422 if (best_glyph)
27424 *x = best_x;
27425 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27427 if (right_p)
27429 *x += best_glyph->pixel_width;
27430 ++*hpos;
27433 *y = best_row->y;
27434 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27437 return best_glyph != NULL;
27439 #endif /* not used */
27441 /* Find the positions of the first and the last glyphs in window W's
27442 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27443 (assumed to be a string), and return in HLINFO's mouse_face_*
27444 members the pixel and column/row coordinates of those glyphs. */
27446 static void
27447 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27448 Lisp_Object object,
27449 ptrdiff_t startpos, ptrdiff_t endpos)
27451 int yb = window_text_bottom_y (w);
27452 struct glyph_row *r;
27453 struct glyph *g, *e;
27454 int gx;
27455 int found = 0;
27457 /* Find the glyph row with at least one position in the range
27458 [STARTPOS..ENDPOS], and the first glyph in that row whose
27459 position belongs to that range. */
27460 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27461 r->enabled_p && r->y < yb;
27462 ++r)
27464 if (!r->reversed_p)
27466 g = r->glyphs[TEXT_AREA];
27467 e = g + r->used[TEXT_AREA];
27468 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27469 if (EQ (g->object, object)
27470 && startpos <= g->charpos && g->charpos <= endpos)
27472 hlinfo->mouse_face_beg_row
27473 = MATRIX_ROW_VPOS (r, w->current_matrix);
27474 hlinfo->mouse_face_beg_y = r->y;
27475 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27476 hlinfo->mouse_face_beg_x = gx;
27477 found = 1;
27478 break;
27481 else
27483 struct glyph *g1;
27485 e = r->glyphs[TEXT_AREA];
27486 g = e + r->used[TEXT_AREA];
27487 for ( ; g > e; --g)
27488 if (EQ ((g-1)->object, object)
27489 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27491 hlinfo->mouse_face_beg_row
27492 = MATRIX_ROW_VPOS (r, w->current_matrix);
27493 hlinfo->mouse_face_beg_y = r->y;
27494 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27495 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27496 gx += g1->pixel_width;
27497 hlinfo->mouse_face_beg_x = gx;
27498 found = 1;
27499 break;
27502 if (found)
27503 break;
27506 if (!found)
27507 return;
27509 /* Starting with the next row, look for the first row which does NOT
27510 include any glyphs whose positions are in the range. */
27511 for (++r; r->enabled_p && r->y < yb; ++r)
27513 g = r->glyphs[TEXT_AREA];
27514 e = g + r->used[TEXT_AREA];
27515 found = 0;
27516 for ( ; g < e; ++g)
27517 if (EQ (g->object, object)
27518 && startpos <= g->charpos && g->charpos <= endpos)
27520 found = 1;
27521 break;
27523 if (!found)
27524 break;
27527 /* The highlighted region ends on the previous row. */
27528 r--;
27530 /* Set the end row and its vertical pixel coordinate. */
27531 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27532 hlinfo->mouse_face_end_y = r->y;
27534 /* Compute and set the end column and the end column's horizontal
27535 pixel coordinate. */
27536 if (!r->reversed_p)
27538 g = r->glyphs[TEXT_AREA];
27539 e = g + r->used[TEXT_AREA];
27540 for ( ; e > g; --e)
27541 if (EQ ((e-1)->object, object)
27542 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27543 break;
27544 hlinfo->mouse_face_end_col = e - g;
27546 for (gx = r->x; g < e; ++g)
27547 gx += g->pixel_width;
27548 hlinfo->mouse_face_end_x = gx;
27550 else
27552 e = r->glyphs[TEXT_AREA];
27553 g = e + r->used[TEXT_AREA];
27554 for (gx = r->x ; e < g; ++e)
27556 if (EQ (e->object, object)
27557 && startpos <= e->charpos && e->charpos <= endpos)
27558 break;
27559 gx += e->pixel_width;
27561 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27562 hlinfo->mouse_face_end_x = gx;
27566 #ifdef HAVE_WINDOW_SYSTEM
27568 /* See if position X, Y is within a hot-spot of an image. */
27570 static int
27571 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27573 if (!CONSP (hot_spot))
27574 return 0;
27576 if (EQ (XCAR (hot_spot), Qrect))
27578 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27579 Lisp_Object rect = XCDR (hot_spot);
27580 Lisp_Object tem;
27581 if (!CONSP (rect))
27582 return 0;
27583 if (!CONSP (XCAR (rect)))
27584 return 0;
27585 if (!CONSP (XCDR (rect)))
27586 return 0;
27587 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27588 return 0;
27589 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27590 return 0;
27591 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27592 return 0;
27593 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27594 return 0;
27595 return 1;
27597 else if (EQ (XCAR (hot_spot), Qcircle))
27599 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27600 Lisp_Object circ = XCDR (hot_spot);
27601 Lisp_Object lr, lx0, ly0;
27602 if (CONSP (circ)
27603 && CONSP (XCAR (circ))
27604 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27605 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27606 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27608 double r = XFLOATINT (lr);
27609 double dx = XINT (lx0) - x;
27610 double dy = XINT (ly0) - y;
27611 return (dx * dx + dy * dy <= r * r);
27614 else if (EQ (XCAR (hot_spot), Qpoly))
27616 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27617 if (VECTORP (XCDR (hot_spot)))
27619 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27620 Lisp_Object *poly = v->contents;
27621 ptrdiff_t n = v->header.size;
27622 ptrdiff_t i;
27623 int inside = 0;
27624 Lisp_Object lx, ly;
27625 int x0, y0;
27627 /* Need an even number of coordinates, and at least 3 edges. */
27628 if (n < 6 || n & 1)
27629 return 0;
27631 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27632 If count is odd, we are inside polygon. Pixels on edges
27633 may or may not be included depending on actual geometry of the
27634 polygon. */
27635 if ((lx = poly[n-2], !INTEGERP (lx))
27636 || (ly = poly[n-1], !INTEGERP (lx)))
27637 return 0;
27638 x0 = XINT (lx), y0 = XINT (ly);
27639 for (i = 0; i < n; i += 2)
27641 int x1 = x0, y1 = y0;
27642 if ((lx = poly[i], !INTEGERP (lx))
27643 || (ly = poly[i+1], !INTEGERP (ly)))
27644 return 0;
27645 x0 = XINT (lx), y0 = XINT (ly);
27647 /* Does this segment cross the X line? */
27648 if (x0 >= x)
27650 if (x1 >= x)
27651 continue;
27653 else if (x1 < x)
27654 continue;
27655 if (y > y0 && y > y1)
27656 continue;
27657 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27658 inside = !inside;
27660 return inside;
27663 return 0;
27666 Lisp_Object
27667 find_hot_spot (Lisp_Object map, int x, int y)
27669 while (CONSP (map))
27671 if (CONSP (XCAR (map))
27672 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27673 return XCAR (map);
27674 map = XCDR (map);
27677 return Qnil;
27680 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27681 3, 3, 0,
27682 doc: /* Lookup in image map MAP coordinates X and Y.
27683 An image map is an alist where each element has the format (AREA ID PLIST).
27684 An AREA is specified as either a rectangle, a circle, or a polygon:
27685 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27686 pixel coordinates of the upper left and bottom right corners.
27687 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27688 and the radius of the circle; r may be a float or integer.
27689 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27690 vector describes one corner in the polygon.
27691 Returns the alist element for the first matching AREA in MAP. */)
27692 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27694 if (NILP (map))
27695 return Qnil;
27697 CHECK_NUMBER (x);
27698 CHECK_NUMBER (y);
27700 return find_hot_spot (map,
27701 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27702 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27706 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27707 static void
27708 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27710 /* Do not change cursor shape while dragging mouse. */
27711 if (!NILP (do_mouse_tracking))
27712 return;
27714 if (!NILP (pointer))
27716 if (EQ (pointer, Qarrow))
27717 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27718 else if (EQ (pointer, Qhand))
27719 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27720 else if (EQ (pointer, Qtext))
27721 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27722 else if (EQ (pointer, intern ("hdrag")))
27723 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27724 #ifdef HAVE_X_WINDOWS
27725 else if (EQ (pointer, intern ("vdrag")))
27726 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27727 #endif
27728 else if (EQ (pointer, intern ("hourglass")))
27729 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27730 else if (EQ (pointer, Qmodeline))
27731 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27732 else
27733 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27736 if (cursor != No_Cursor)
27737 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27740 #endif /* HAVE_WINDOW_SYSTEM */
27742 /* Take proper action when mouse has moved to the mode or header line
27743 or marginal area AREA of window W, x-position X and y-position Y.
27744 X is relative to the start of the text display area of W, so the
27745 width of bitmap areas and scroll bars must be subtracted to get a
27746 position relative to the start of the mode line. */
27748 static void
27749 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27750 enum window_part area)
27752 struct window *w = XWINDOW (window);
27753 struct frame *f = XFRAME (w->frame);
27754 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27755 #ifdef HAVE_WINDOW_SYSTEM
27756 Display_Info *dpyinfo;
27757 #endif
27758 Cursor cursor = No_Cursor;
27759 Lisp_Object pointer = Qnil;
27760 int dx, dy, width, height;
27761 ptrdiff_t charpos;
27762 Lisp_Object string, object = Qnil;
27763 Lisp_Object pos IF_LINT (= Qnil), help;
27765 Lisp_Object mouse_face;
27766 int original_x_pixel = x;
27767 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27768 struct glyph_row *row IF_LINT (= 0);
27770 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27772 int x0;
27773 struct glyph *end;
27775 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27776 returns them in row/column units! */
27777 string = mode_line_string (w, area, &x, &y, &charpos,
27778 &object, &dx, &dy, &width, &height);
27780 row = (area == ON_MODE_LINE
27781 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27782 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27784 /* Find the glyph under the mouse pointer. */
27785 if (row->mode_line_p && row->enabled_p)
27787 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27788 end = glyph + row->used[TEXT_AREA];
27790 for (x0 = original_x_pixel;
27791 glyph < end && x0 >= glyph->pixel_width;
27792 ++glyph)
27793 x0 -= glyph->pixel_width;
27795 if (glyph >= end)
27796 glyph = NULL;
27799 else
27801 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27802 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27803 returns them in row/column units! */
27804 string = marginal_area_string (w, area, &x, &y, &charpos,
27805 &object, &dx, &dy, &width, &height);
27808 help = Qnil;
27810 #ifdef HAVE_WINDOW_SYSTEM
27811 if (IMAGEP (object))
27813 Lisp_Object image_map, hotspot;
27814 if ((image_map = Fplist_get (XCDR (object), QCmap),
27815 !NILP (image_map))
27816 && (hotspot = find_hot_spot (image_map, dx, dy),
27817 CONSP (hotspot))
27818 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27820 Lisp_Object plist;
27822 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27823 If so, we could look for mouse-enter, mouse-leave
27824 properties in PLIST (and do something...). */
27825 hotspot = XCDR (hotspot);
27826 if (CONSP (hotspot)
27827 && (plist = XCAR (hotspot), CONSP (plist)))
27829 pointer = Fplist_get (plist, Qpointer);
27830 if (NILP (pointer))
27831 pointer = Qhand;
27832 help = Fplist_get (plist, Qhelp_echo);
27833 if (!NILP (help))
27835 help_echo_string = help;
27836 XSETWINDOW (help_echo_window, w);
27837 help_echo_object = w->contents;
27838 help_echo_pos = charpos;
27842 if (NILP (pointer))
27843 pointer = Fplist_get (XCDR (object), QCpointer);
27845 #endif /* HAVE_WINDOW_SYSTEM */
27847 if (STRINGP (string))
27848 pos = make_number (charpos);
27850 /* Set the help text and mouse pointer. If the mouse is on a part
27851 of the mode line without any text (e.g. past the right edge of
27852 the mode line text), use the default help text and pointer. */
27853 if (STRINGP (string) || area == ON_MODE_LINE)
27855 /* Arrange to display the help by setting the global variables
27856 help_echo_string, help_echo_object, and help_echo_pos. */
27857 if (NILP (help))
27859 if (STRINGP (string))
27860 help = Fget_text_property (pos, Qhelp_echo, string);
27862 if (!NILP (help))
27864 help_echo_string = help;
27865 XSETWINDOW (help_echo_window, w);
27866 help_echo_object = string;
27867 help_echo_pos = charpos;
27869 else if (area == ON_MODE_LINE)
27871 Lisp_Object default_help
27872 = buffer_local_value_1 (Qmode_line_default_help_echo,
27873 w->contents);
27875 if (STRINGP (default_help))
27877 help_echo_string = default_help;
27878 XSETWINDOW (help_echo_window, w);
27879 help_echo_object = Qnil;
27880 help_echo_pos = -1;
27885 #ifdef HAVE_WINDOW_SYSTEM
27886 /* Change the mouse pointer according to what is under it. */
27887 if (FRAME_WINDOW_P (f))
27889 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27890 if (STRINGP (string))
27892 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27894 if (NILP (pointer))
27895 pointer = Fget_text_property (pos, Qpointer, string);
27897 /* Change the mouse pointer according to what is under X/Y. */
27898 if (NILP (pointer)
27899 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27901 Lisp_Object map;
27902 map = Fget_text_property (pos, Qlocal_map, string);
27903 if (!KEYMAPP (map))
27904 map = Fget_text_property (pos, Qkeymap, string);
27905 if (!KEYMAPP (map))
27906 cursor = dpyinfo->vertical_scroll_bar_cursor;
27909 else
27910 /* Default mode-line pointer. */
27911 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27913 #endif
27916 /* Change the mouse face according to what is under X/Y. */
27917 if (STRINGP (string))
27919 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27920 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27921 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27922 && glyph)
27924 Lisp_Object b, e;
27926 struct glyph * tmp_glyph;
27928 int gpos;
27929 int gseq_length;
27930 int total_pixel_width;
27931 ptrdiff_t begpos, endpos, ignore;
27933 int vpos, hpos;
27935 b = Fprevious_single_property_change (make_number (charpos + 1),
27936 Qmouse_face, string, Qnil);
27937 if (NILP (b))
27938 begpos = 0;
27939 else
27940 begpos = XINT (b);
27942 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27943 if (NILP (e))
27944 endpos = SCHARS (string);
27945 else
27946 endpos = XINT (e);
27948 /* Calculate the glyph position GPOS of GLYPH in the
27949 displayed string, relative to the beginning of the
27950 highlighted part of the string.
27952 Note: GPOS is different from CHARPOS. CHARPOS is the
27953 position of GLYPH in the internal string object. A mode
27954 line string format has structures which are converted to
27955 a flattened string by the Emacs Lisp interpreter. The
27956 internal string is an element of those structures. The
27957 displayed string is the flattened string. */
27958 tmp_glyph = row_start_glyph;
27959 while (tmp_glyph < glyph
27960 && (!(EQ (tmp_glyph->object, glyph->object)
27961 && begpos <= tmp_glyph->charpos
27962 && tmp_glyph->charpos < endpos)))
27963 tmp_glyph++;
27964 gpos = glyph - tmp_glyph;
27966 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27967 the highlighted part of the displayed string to which
27968 GLYPH belongs. Note: GSEQ_LENGTH is different from
27969 SCHARS (STRING), because the latter returns the length of
27970 the internal string. */
27971 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27972 tmp_glyph > glyph
27973 && (!(EQ (tmp_glyph->object, glyph->object)
27974 && begpos <= tmp_glyph->charpos
27975 && tmp_glyph->charpos < endpos));
27976 tmp_glyph--)
27978 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27980 /* Calculate the total pixel width of all the glyphs between
27981 the beginning of the highlighted area and GLYPH. */
27982 total_pixel_width = 0;
27983 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27984 total_pixel_width += tmp_glyph->pixel_width;
27986 /* Pre calculation of re-rendering position. Note: X is in
27987 column units here, after the call to mode_line_string or
27988 marginal_area_string. */
27989 hpos = x - gpos;
27990 vpos = (area == ON_MODE_LINE
27991 ? (w->current_matrix)->nrows - 1
27992 : 0);
27994 /* If GLYPH's position is included in the region that is
27995 already drawn in mouse face, we have nothing to do. */
27996 if ( EQ (window, hlinfo->mouse_face_window)
27997 && (!row->reversed_p
27998 ? (hlinfo->mouse_face_beg_col <= hpos
27999 && hpos < hlinfo->mouse_face_end_col)
28000 /* In R2L rows we swap BEG and END, see below. */
28001 : (hlinfo->mouse_face_end_col <= hpos
28002 && hpos < hlinfo->mouse_face_beg_col))
28003 && hlinfo->mouse_face_beg_row == vpos )
28004 return;
28006 if (clear_mouse_face (hlinfo))
28007 cursor = No_Cursor;
28009 if (!row->reversed_p)
28011 hlinfo->mouse_face_beg_col = hpos;
28012 hlinfo->mouse_face_beg_x = original_x_pixel
28013 - (total_pixel_width + dx);
28014 hlinfo->mouse_face_end_col = hpos + gseq_length;
28015 hlinfo->mouse_face_end_x = 0;
28017 else
28019 /* In R2L rows, show_mouse_face expects BEG and END
28020 coordinates to be swapped. */
28021 hlinfo->mouse_face_end_col = hpos;
28022 hlinfo->mouse_face_end_x = original_x_pixel
28023 - (total_pixel_width + dx);
28024 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28025 hlinfo->mouse_face_beg_x = 0;
28028 hlinfo->mouse_face_beg_row = vpos;
28029 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28030 hlinfo->mouse_face_beg_y = 0;
28031 hlinfo->mouse_face_end_y = 0;
28032 hlinfo->mouse_face_past_end = 0;
28033 hlinfo->mouse_face_window = window;
28035 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28036 charpos,
28037 0, 0, 0,
28038 &ignore,
28039 glyph->face_id,
28041 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28043 if (NILP (pointer))
28044 pointer = Qhand;
28046 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28047 clear_mouse_face (hlinfo);
28049 #ifdef HAVE_WINDOW_SYSTEM
28050 if (FRAME_WINDOW_P (f))
28051 define_frame_cursor1 (f, cursor, pointer);
28052 #endif
28056 /* EXPORT:
28057 Take proper action when the mouse has moved to position X, Y on
28058 frame F with regards to highlighting portions of display that have
28059 mouse-face properties. Also de-highlight portions of display where
28060 the mouse was before, set the mouse pointer shape as appropriate
28061 for the mouse coordinates, and activate help echo (tooltips).
28062 X and Y can be negative or out of range. */
28064 void
28065 note_mouse_highlight (struct frame *f, int x, int y)
28067 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28068 enum window_part part = ON_NOTHING;
28069 Lisp_Object window;
28070 struct window *w;
28071 Cursor cursor = No_Cursor;
28072 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28073 struct buffer *b;
28075 /* When a menu is active, don't highlight because this looks odd. */
28076 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28077 if (popup_activated ())
28078 return;
28079 #endif
28081 if (!f->glyphs_initialized_p
28082 || f->pointer_invisible)
28083 return;
28085 hlinfo->mouse_face_mouse_x = x;
28086 hlinfo->mouse_face_mouse_y = y;
28087 hlinfo->mouse_face_mouse_frame = f;
28089 if (hlinfo->mouse_face_defer)
28090 return;
28092 /* Which window is that in? */
28093 window = window_from_coordinates (f, x, y, &part, 1);
28095 /* If displaying active text in another window, clear that. */
28096 if (! EQ (window, hlinfo->mouse_face_window)
28097 /* Also clear if we move out of text area in same window. */
28098 || (!NILP (hlinfo->mouse_face_window)
28099 && !NILP (window)
28100 && part != ON_TEXT
28101 && part != ON_MODE_LINE
28102 && part != ON_HEADER_LINE))
28103 clear_mouse_face (hlinfo);
28105 /* Not on a window -> return. */
28106 if (!WINDOWP (window))
28107 return;
28109 /* Reset help_echo_string. It will get recomputed below. */
28110 help_echo_string = Qnil;
28112 /* Convert to window-relative pixel coordinates. */
28113 w = XWINDOW (window);
28114 frame_to_window_pixel_xy (w, &x, &y);
28116 #ifdef HAVE_WINDOW_SYSTEM
28117 /* Handle tool-bar window differently since it doesn't display a
28118 buffer. */
28119 if (EQ (window, f->tool_bar_window))
28121 note_tool_bar_highlight (f, x, y);
28122 return;
28124 #endif
28126 /* Mouse is on the mode, header line or margin? */
28127 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28128 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28130 note_mode_line_or_margin_highlight (window, x, y, part);
28131 return;
28134 #ifdef HAVE_WINDOW_SYSTEM
28135 if (part == ON_VERTICAL_BORDER)
28137 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28138 help_echo_string = build_string ("drag-mouse-1: resize");
28140 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28141 || part == ON_SCROLL_BAR)
28142 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28143 else
28144 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28145 #endif
28147 /* Are we in a window whose display is up to date?
28148 And verify the buffer's text has not changed. */
28149 b = XBUFFER (w->contents);
28150 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28152 int hpos, vpos, dx, dy, area = LAST_AREA;
28153 ptrdiff_t pos;
28154 struct glyph *glyph;
28155 Lisp_Object object;
28156 Lisp_Object mouse_face = Qnil, position;
28157 Lisp_Object *overlay_vec = NULL;
28158 ptrdiff_t i, noverlays;
28159 struct buffer *obuf;
28160 ptrdiff_t obegv, ozv;
28161 int same_region;
28163 /* Find the glyph under X/Y. */
28164 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28166 #ifdef HAVE_WINDOW_SYSTEM
28167 /* Look for :pointer property on image. */
28168 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28170 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28171 if (img != NULL && IMAGEP (img->spec))
28173 Lisp_Object image_map, hotspot;
28174 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28175 !NILP (image_map))
28176 && (hotspot = find_hot_spot (image_map,
28177 glyph->slice.img.x + dx,
28178 glyph->slice.img.y + dy),
28179 CONSP (hotspot))
28180 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28182 Lisp_Object plist;
28184 /* Could check XCAR (hotspot) to see if we enter/leave
28185 this hot-spot.
28186 If so, we could look for mouse-enter, mouse-leave
28187 properties in PLIST (and do something...). */
28188 hotspot = XCDR (hotspot);
28189 if (CONSP (hotspot)
28190 && (plist = XCAR (hotspot), CONSP (plist)))
28192 pointer = Fplist_get (plist, Qpointer);
28193 if (NILP (pointer))
28194 pointer = Qhand;
28195 help_echo_string = Fplist_get (plist, Qhelp_echo);
28196 if (!NILP (help_echo_string))
28198 help_echo_window = window;
28199 help_echo_object = glyph->object;
28200 help_echo_pos = glyph->charpos;
28204 if (NILP (pointer))
28205 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28208 #endif /* HAVE_WINDOW_SYSTEM */
28210 /* Clear mouse face if X/Y not over text. */
28211 if (glyph == NULL
28212 || area != TEXT_AREA
28213 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28214 /* Glyph's OBJECT is an integer for glyphs inserted by the
28215 display engine for its internal purposes, like truncation
28216 and continuation glyphs and blanks beyond the end of
28217 line's text on text terminals. If we are over such a
28218 glyph, we are not over any text. */
28219 || INTEGERP (glyph->object)
28220 /* R2L rows have a stretch glyph at their front, which
28221 stands for no text, whereas L2R rows have no glyphs at
28222 all beyond the end of text. Treat such stretch glyphs
28223 like we do with NULL glyphs in L2R rows. */
28224 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28225 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28226 && glyph->type == STRETCH_GLYPH
28227 && glyph->avoid_cursor_p))
28229 if (clear_mouse_face (hlinfo))
28230 cursor = No_Cursor;
28231 #ifdef HAVE_WINDOW_SYSTEM
28232 if (FRAME_WINDOW_P (f) && NILP (pointer))
28234 if (area != TEXT_AREA)
28235 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28236 else
28237 pointer = Vvoid_text_area_pointer;
28239 #endif
28240 goto set_cursor;
28243 pos = glyph->charpos;
28244 object = glyph->object;
28245 if (!STRINGP (object) && !BUFFERP (object))
28246 goto set_cursor;
28248 /* If we get an out-of-range value, return now; avoid an error. */
28249 if (BUFFERP (object) && pos > BUF_Z (b))
28250 goto set_cursor;
28252 /* Make the window's buffer temporarily current for
28253 overlays_at and compute_char_face. */
28254 obuf = current_buffer;
28255 current_buffer = b;
28256 obegv = BEGV;
28257 ozv = ZV;
28258 BEGV = BEG;
28259 ZV = Z;
28261 /* Is this char mouse-active or does it have help-echo? */
28262 position = make_number (pos);
28264 if (BUFFERP (object))
28266 /* Put all the overlays we want in a vector in overlay_vec. */
28267 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28268 /* Sort overlays into increasing priority order. */
28269 noverlays = sort_overlays (overlay_vec, noverlays, w);
28271 else
28272 noverlays = 0;
28274 if (NILP (Vmouse_highlight))
28276 clear_mouse_face (hlinfo);
28277 goto check_help_echo;
28280 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28282 if (same_region)
28283 cursor = No_Cursor;
28285 /* Check mouse-face highlighting. */
28286 if (! same_region
28287 /* If there exists an overlay with mouse-face overlapping
28288 the one we are currently highlighting, we have to
28289 check if we enter the overlapping overlay, and then
28290 highlight only that. */
28291 || (OVERLAYP (hlinfo->mouse_face_overlay)
28292 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28294 /* Find the highest priority overlay with a mouse-face. */
28295 Lisp_Object overlay = Qnil;
28296 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28298 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28299 if (!NILP (mouse_face))
28300 overlay = overlay_vec[i];
28303 /* If we're highlighting the same overlay as before, there's
28304 no need to do that again. */
28305 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28306 goto check_help_echo;
28307 hlinfo->mouse_face_overlay = overlay;
28309 /* Clear the display of the old active region, if any. */
28310 if (clear_mouse_face (hlinfo))
28311 cursor = No_Cursor;
28313 /* If no overlay applies, get a text property. */
28314 if (NILP (overlay))
28315 mouse_face = Fget_text_property (position, Qmouse_face, object);
28317 /* Next, compute the bounds of the mouse highlighting and
28318 display it. */
28319 if (!NILP (mouse_face) && STRINGP (object))
28321 /* The mouse-highlighting comes from a display string
28322 with a mouse-face. */
28323 Lisp_Object s, e;
28324 ptrdiff_t ignore;
28326 s = Fprevious_single_property_change
28327 (make_number (pos + 1), Qmouse_face, object, Qnil);
28328 e = Fnext_single_property_change
28329 (position, Qmouse_face, object, Qnil);
28330 if (NILP (s))
28331 s = make_number (0);
28332 if (NILP (e))
28333 e = make_number (SCHARS (object) - 1);
28334 mouse_face_from_string_pos (w, hlinfo, object,
28335 XINT (s), XINT (e));
28336 hlinfo->mouse_face_past_end = 0;
28337 hlinfo->mouse_face_window = window;
28338 hlinfo->mouse_face_face_id
28339 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28340 glyph->face_id, 1);
28341 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28342 cursor = No_Cursor;
28344 else
28346 /* The mouse-highlighting, if any, comes from an overlay
28347 or text property in the buffer. */
28348 Lisp_Object buffer IF_LINT (= Qnil);
28349 Lisp_Object disp_string IF_LINT (= Qnil);
28351 if (STRINGP (object))
28353 /* If we are on a display string with no mouse-face,
28354 check if the text under it has one. */
28355 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28356 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28357 pos = string_buffer_position (object, start);
28358 if (pos > 0)
28360 mouse_face = get_char_property_and_overlay
28361 (make_number (pos), Qmouse_face, w->contents, &overlay);
28362 buffer = w->contents;
28363 disp_string = object;
28366 else
28368 buffer = object;
28369 disp_string = Qnil;
28372 if (!NILP (mouse_face))
28374 Lisp_Object before, after;
28375 Lisp_Object before_string, after_string;
28376 /* To correctly find the limits of mouse highlight
28377 in a bidi-reordered buffer, we must not use the
28378 optimization of limiting the search in
28379 previous-single-property-change and
28380 next-single-property-change, because
28381 rows_from_pos_range needs the real start and end
28382 positions to DTRT in this case. That's because
28383 the first row visible in a window does not
28384 necessarily display the character whose position
28385 is the smallest. */
28386 Lisp_Object lim1 =
28387 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28388 ? Fmarker_position (w->start)
28389 : Qnil;
28390 Lisp_Object lim2 =
28391 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28392 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28393 : Qnil;
28395 if (NILP (overlay))
28397 /* Handle the text property case. */
28398 before = Fprevious_single_property_change
28399 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28400 after = Fnext_single_property_change
28401 (make_number (pos), Qmouse_face, buffer, lim2);
28402 before_string = after_string = Qnil;
28404 else
28406 /* Handle the overlay case. */
28407 before = Foverlay_start (overlay);
28408 after = Foverlay_end (overlay);
28409 before_string = Foverlay_get (overlay, Qbefore_string);
28410 after_string = Foverlay_get (overlay, Qafter_string);
28412 if (!STRINGP (before_string)) before_string = Qnil;
28413 if (!STRINGP (after_string)) after_string = Qnil;
28416 mouse_face_from_buffer_pos (window, hlinfo, pos,
28417 NILP (before)
28419 : XFASTINT (before),
28420 NILP (after)
28421 ? BUF_Z (XBUFFER (buffer))
28422 : XFASTINT (after),
28423 before_string, after_string,
28424 disp_string);
28425 cursor = No_Cursor;
28430 check_help_echo:
28432 /* Look for a `help-echo' property. */
28433 if (NILP (help_echo_string)) {
28434 Lisp_Object help, overlay;
28436 /* Check overlays first. */
28437 help = overlay = Qnil;
28438 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28440 overlay = overlay_vec[i];
28441 help = Foverlay_get (overlay, Qhelp_echo);
28444 if (!NILP (help))
28446 help_echo_string = help;
28447 help_echo_window = window;
28448 help_echo_object = overlay;
28449 help_echo_pos = pos;
28451 else
28453 Lisp_Object obj = glyph->object;
28454 ptrdiff_t charpos = glyph->charpos;
28456 /* Try text properties. */
28457 if (STRINGP (obj)
28458 && charpos >= 0
28459 && charpos < SCHARS (obj))
28461 help = Fget_text_property (make_number (charpos),
28462 Qhelp_echo, obj);
28463 if (NILP (help))
28465 /* If the string itself doesn't specify a help-echo,
28466 see if the buffer text ``under'' it does. */
28467 struct glyph_row *r
28468 = MATRIX_ROW (w->current_matrix, vpos);
28469 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28470 ptrdiff_t p = string_buffer_position (obj, start);
28471 if (p > 0)
28473 help = Fget_char_property (make_number (p),
28474 Qhelp_echo, w->contents);
28475 if (!NILP (help))
28477 charpos = p;
28478 obj = w->contents;
28483 else if (BUFFERP (obj)
28484 && charpos >= BEGV
28485 && charpos < ZV)
28486 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28487 obj);
28489 if (!NILP (help))
28491 help_echo_string = help;
28492 help_echo_window = window;
28493 help_echo_object = obj;
28494 help_echo_pos = charpos;
28499 #ifdef HAVE_WINDOW_SYSTEM
28500 /* Look for a `pointer' property. */
28501 if (FRAME_WINDOW_P (f) && NILP (pointer))
28503 /* Check overlays first. */
28504 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28505 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28507 if (NILP (pointer))
28509 Lisp_Object obj = glyph->object;
28510 ptrdiff_t charpos = glyph->charpos;
28512 /* Try text properties. */
28513 if (STRINGP (obj)
28514 && charpos >= 0
28515 && charpos < SCHARS (obj))
28517 pointer = Fget_text_property (make_number (charpos),
28518 Qpointer, obj);
28519 if (NILP (pointer))
28521 /* If the string itself doesn't specify a pointer,
28522 see if the buffer text ``under'' it does. */
28523 struct glyph_row *r
28524 = MATRIX_ROW (w->current_matrix, vpos);
28525 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28526 ptrdiff_t p = string_buffer_position (obj, start);
28527 if (p > 0)
28528 pointer = Fget_char_property (make_number (p),
28529 Qpointer, w->contents);
28532 else if (BUFFERP (obj)
28533 && charpos >= BEGV
28534 && charpos < ZV)
28535 pointer = Fget_text_property (make_number (charpos),
28536 Qpointer, obj);
28539 #endif /* HAVE_WINDOW_SYSTEM */
28541 BEGV = obegv;
28542 ZV = ozv;
28543 current_buffer = obuf;
28546 set_cursor:
28548 #ifdef HAVE_WINDOW_SYSTEM
28549 if (FRAME_WINDOW_P (f))
28550 define_frame_cursor1 (f, cursor, pointer);
28551 #else
28552 /* This is here to prevent a compiler error, about "label at end of
28553 compound statement". */
28554 return;
28555 #endif
28559 /* EXPORT for RIF:
28560 Clear any mouse-face on window W. This function is part of the
28561 redisplay interface, and is called from try_window_id and similar
28562 functions to ensure the mouse-highlight is off. */
28564 void
28565 x_clear_window_mouse_face (struct window *w)
28567 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28568 Lisp_Object window;
28570 block_input ();
28571 XSETWINDOW (window, w);
28572 if (EQ (window, hlinfo->mouse_face_window))
28573 clear_mouse_face (hlinfo);
28574 unblock_input ();
28578 /* EXPORT:
28579 Just discard the mouse face information for frame F, if any.
28580 This is used when the size of F is changed. */
28582 void
28583 cancel_mouse_face (struct frame *f)
28585 Lisp_Object window;
28586 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28588 window = hlinfo->mouse_face_window;
28589 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28591 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28592 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28593 hlinfo->mouse_face_window = Qnil;
28599 /***********************************************************************
28600 Exposure Events
28601 ***********************************************************************/
28603 #ifdef HAVE_WINDOW_SYSTEM
28605 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28606 which intersects rectangle R. R is in window-relative coordinates. */
28608 static void
28609 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28610 enum glyph_row_area area)
28612 struct glyph *first = row->glyphs[area];
28613 struct glyph *end = row->glyphs[area] + row->used[area];
28614 struct glyph *last;
28615 int first_x, start_x, x;
28617 if (area == TEXT_AREA && row->fill_line_p)
28618 /* If row extends face to end of line write the whole line. */
28619 draw_glyphs (w, 0, row, area,
28620 0, row->used[area],
28621 DRAW_NORMAL_TEXT, 0);
28622 else
28624 /* Set START_X to the window-relative start position for drawing glyphs of
28625 AREA. The first glyph of the text area can be partially visible.
28626 The first glyphs of other areas cannot. */
28627 start_x = window_box_left_offset (w, area);
28628 x = start_x;
28629 if (area == TEXT_AREA)
28630 x += row->x;
28632 /* Find the first glyph that must be redrawn. */
28633 while (first < end
28634 && x + first->pixel_width < r->x)
28636 x += first->pixel_width;
28637 ++first;
28640 /* Find the last one. */
28641 last = first;
28642 first_x = x;
28643 while (last < end
28644 && x < r->x + r->width)
28646 x += last->pixel_width;
28647 ++last;
28650 /* Repaint. */
28651 if (last > first)
28652 draw_glyphs (w, first_x - start_x, row, area,
28653 first - row->glyphs[area], last - row->glyphs[area],
28654 DRAW_NORMAL_TEXT, 0);
28659 /* Redraw the parts of the glyph row ROW on window W intersecting
28660 rectangle R. R is in window-relative coordinates. Value is
28661 non-zero if mouse-face was overwritten. */
28663 static int
28664 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28666 eassert (row->enabled_p);
28668 if (row->mode_line_p || w->pseudo_window_p)
28669 draw_glyphs (w, 0, row, TEXT_AREA,
28670 0, row->used[TEXT_AREA],
28671 DRAW_NORMAL_TEXT, 0);
28672 else
28674 if (row->used[LEFT_MARGIN_AREA])
28675 expose_area (w, row, r, LEFT_MARGIN_AREA);
28676 if (row->used[TEXT_AREA])
28677 expose_area (w, row, r, TEXT_AREA);
28678 if (row->used[RIGHT_MARGIN_AREA])
28679 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28680 draw_row_fringe_bitmaps (w, row);
28683 return row->mouse_face_p;
28687 /* Redraw those parts of glyphs rows during expose event handling that
28688 overlap other rows. Redrawing of an exposed line writes over parts
28689 of lines overlapping that exposed line; this function fixes that.
28691 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28692 row in W's current matrix that is exposed and overlaps other rows.
28693 LAST_OVERLAPPING_ROW is the last such row. */
28695 static void
28696 expose_overlaps (struct window *w,
28697 struct glyph_row *first_overlapping_row,
28698 struct glyph_row *last_overlapping_row,
28699 XRectangle *r)
28701 struct glyph_row *row;
28703 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28704 if (row->overlapping_p)
28706 eassert (row->enabled_p && !row->mode_line_p);
28708 row->clip = r;
28709 if (row->used[LEFT_MARGIN_AREA])
28710 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28712 if (row->used[TEXT_AREA])
28713 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28715 if (row->used[RIGHT_MARGIN_AREA])
28716 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28717 row->clip = NULL;
28722 /* Return non-zero if W's cursor intersects rectangle R. */
28724 static int
28725 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28727 XRectangle cr, result;
28728 struct glyph *cursor_glyph;
28729 struct glyph_row *row;
28731 if (w->phys_cursor.vpos >= 0
28732 && w->phys_cursor.vpos < w->current_matrix->nrows
28733 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28734 row->enabled_p)
28735 && row->cursor_in_fringe_p)
28737 /* Cursor is in the fringe. */
28738 cr.x = window_box_right_offset (w,
28739 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28740 ? RIGHT_MARGIN_AREA
28741 : TEXT_AREA));
28742 cr.y = row->y;
28743 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28744 cr.height = row->height;
28745 return x_intersect_rectangles (&cr, r, &result);
28748 cursor_glyph = get_phys_cursor_glyph (w);
28749 if (cursor_glyph)
28751 /* r is relative to W's box, but w->phys_cursor.x is relative
28752 to left edge of W's TEXT area. Adjust it. */
28753 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28754 cr.y = w->phys_cursor.y;
28755 cr.width = cursor_glyph->pixel_width;
28756 cr.height = w->phys_cursor_height;
28757 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28758 I assume the effect is the same -- and this is portable. */
28759 return x_intersect_rectangles (&cr, r, &result);
28761 /* If we don't understand the format, pretend we're not in the hot-spot. */
28762 return 0;
28766 /* EXPORT:
28767 Draw a vertical window border to the right of window W if W doesn't
28768 have vertical scroll bars. */
28770 void
28771 x_draw_vertical_border (struct window *w)
28773 struct frame *f = XFRAME (WINDOW_FRAME (w));
28775 /* We could do better, if we knew what type of scroll-bar the adjacent
28776 windows (on either side) have... But we don't :-(
28777 However, I think this works ok. ++KFS 2003-04-25 */
28779 /* Redraw borders between horizontally adjacent windows. Don't
28780 do it for frames with vertical scroll bars because either the
28781 right scroll bar of a window, or the left scroll bar of its
28782 neighbor will suffice as a border. */
28783 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28784 return;
28786 /* Note: It is necessary to redraw both the left and the right
28787 borders, for when only this single window W is being
28788 redisplayed. */
28789 if (!WINDOW_RIGHTMOST_P (w)
28790 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28792 int x0, x1, y0, y1;
28794 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28795 y1 -= 1;
28797 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28798 x1 -= 1;
28800 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28802 if (!WINDOW_LEFTMOST_P (w)
28803 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28805 int x0, x1, y0, y1;
28807 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28808 y1 -= 1;
28810 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28811 x0 -= 1;
28813 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28818 /* Redraw the part of window W intersection rectangle FR. Pixel
28819 coordinates in FR are frame-relative. Call this function with
28820 input blocked. Value is non-zero if the exposure overwrites
28821 mouse-face. */
28823 static int
28824 expose_window (struct window *w, XRectangle *fr)
28826 struct frame *f = XFRAME (w->frame);
28827 XRectangle wr, r;
28828 int mouse_face_overwritten_p = 0;
28830 /* If window is not yet fully initialized, do nothing. This can
28831 happen when toolkit scroll bars are used and a window is split.
28832 Reconfiguring the scroll bar will generate an expose for a newly
28833 created window. */
28834 if (w->current_matrix == NULL)
28835 return 0;
28837 /* When we're currently updating the window, display and current
28838 matrix usually don't agree. Arrange for a thorough display
28839 later. */
28840 if (w->must_be_updated_p)
28842 SET_FRAME_GARBAGED (f);
28843 return 0;
28846 /* Frame-relative pixel rectangle of W. */
28847 wr.x = WINDOW_LEFT_EDGE_X (w);
28848 wr.y = WINDOW_TOP_EDGE_Y (w);
28849 wr.width = WINDOW_TOTAL_WIDTH (w);
28850 wr.height = WINDOW_TOTAL_HEIGHT (w);
28852 if (x_intersect_rectangles (fr, &wr, &r))
28854 int yb = window_text_bottom_y (w);
28855 struct glyph_row *row;
28856 int cursor_cleared_p, phys_cursor_on_p;
28857 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28859 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28860 r.x, r.y, r.width, r.height));
28862 /* Convert to window coordinates. */
28863 r.x -= WINDOW_LEFT_EDGE_X (w);
28864 r.y -= WINDOW_TOP_EDGE_Y (w);
28866 /* Turn off the cursor. */
28867 if (!w->pseudo_window_p
28868 && phys_cursor_in_rect_p (w, &r))
28870 x_clear_cursor (w);
28871 cursor_cleared_p = 1;
28873 else
28874 cursor_cleared_p = 0;
28876 /* If the row containing the cursor extends face to end of line,
28877 then expose_area might overwrite the cursor outside the
28878 rectangle and thus notice_overwritten_cursor might clear
28879 w->phys_cursor_on_p. We remember the original value and
28880 check later if it is changed. */
28881 phys_cursor_on_p = w->phys_cursor_on_p;
28883 /* Update lines intersecting rectangle R. */
28884 first_overlapping_row = last_overlapping_row = NULL;
28885 for (row = w->current_matrix->rows;
28886 row->enabled_p;
28887 ++row)
28889 int y0 = row->y;
28890 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28892 if ((y0 >= r.y && y0 < r.y + r.height)
28893 || (y1 > r.y && y1 < r.y + r.height)
28894 || (r.y >= y0 && r.y < y1)
28895 || (r.y + r.height > y0 && r.y + r.height < y1))
28897 /* A header line may be overlapping, but there is no need
28898 to fix overlapping areas for them. KFS 2005-02-12 */
28899 if (row->overlapping_p && !row->mode_line_p)
28901 if (first_overlapping_row == NULL)
28902 first_overlapping_row = row;
28903 last_overlapping_row = row;
28906 row->clip = fr;
28907 if (expose_line (w, row, &r))
28908 mouse_face_overwritten_p = 1;
28909 row->clip = NULL;
28911 else if (row->overlapping_p)
28913 /* We must redraw a row overlapping the exposed area. */
28914 if (y0 < r.y
28915 ? y0 + row->phys_height > r.y
28916 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28918 if (first_overlapping_row == NULL)
28919 first_overlapping_row = row;
28920 last_overlapping_row = row;
28924 if (y1 >= yb)
28925 break;
28928 /* Display the mode line if there is one. */
28929 if (WINDOW_WANTS_MODELINE_P (w)
28930 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28931 row->enabled_p)
28932 && row->y < r.y + r.height)
28934 if (expose_line (w, row, &r))
28935 mouse_face_overwritten_p = 1;
28938 if (!w->pseudo_window_p)
28940 /* Fix the display of overlapping rows. */
28941 if (first_overlapping_row)
28942 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28943 fr);
28945 /* Draw border between windows. */
28946 x_draw_vertical_border (w);
28948 /* Turn the cursor on again. */
28949 if (cursor_cleared_p
28950 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28951 update_window_cursor (w, 1);
28955 return mouse_face_overwritten_p;
28960 /* Redraw (parts) of all windows in the window tree rooted at W that
28961 intersect R. R contains frame pixel coordinates. Value is
28962 non-zero if the exposure overwrites mouse-face. */
28964 static int
28965 expose_window_tree (struct window *w, XRectangle *r)
28967 struct frame *f = XFRAME (w->frame);
28968 int mouse_face_overwritten_p = 0;
28970 while (w && !FRAME_GARBAGED_P (f))
28972 if (WINDOWP (w->contents))
28973 mouse_face_overwritten_p
28974 |= expose_window_tree (XWINDOW (w->contents), r);
28975 else
28976 mouse_face_overwritten_p |= expose_window (w, r);
28978 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28981 return mouse_face_overwritten_p;
28985 /* EXPORT:
28986 Redisplay an exposed area of frame F. X and Y are the upper-left
28987 corner of the exposed rectangle. W and H are width and height of
28988 the exposed area. All are pixel values. W or H zero means redraw
28989 the entire frame. */
28991 void
28992 expose_frame (struct frame *f, int x, int y, int w, int h)
28994 XRectangle r;
28995 int mouse_face_overwritten_p = 0;
28997 TRACE ((stderr, "expose_frame "));
28999 /* No need to redraw if frame will be redrawn soon. */
29000 if (FRAME_GARBAGED_P (f))
29002 TRACE ((stderr, " garbaged\n"));
29003 return;
29006 /* If basic faces haven't been realized yet, there is no point in
29007 trying to redraw anything. This can happen when we get an expose
29008 event while Emacs is starting, e.g. by moving another window. */
29009 if (FRAME_FACE_CACHE (f) == NULL
29010 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29012 TRACE ((stderr, " no faces\n"));
29013 return;
29016 if (w == 0 || h == 0)
29018 r.x = r.y = 0;
29019 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29020 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29022 else
29024 r.x = x;
29025 r.y = y;
29026 r.width = w;
29027 r.height = h;
29030 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29031 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29033 if (WINDOWP (f->tool_bar_window))
29034 mouse_face_overwritten_p
29035 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29037 #ifdef HAVE_X_WINDOWS
29038 #ifndef MSDOS
29039 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29040 if (WINDOWP (f->menu_bar_window))
29041 mouse_face_overwritten_p
29042 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29043 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29044 #endif
29045 #endif
29047 /* Some window managers support a focus-follows-mouse style with
29048 delayed raising of frames. Imagine a partially obscured frame,
29049 and moving the mouse into partially obscured mouse-face on that
29050 frame. The visible part of the mouse-face will be highlighted,
29051 then the WM raises the obscured frame. With at least one WM, KDE
29052 2.1, Emacs is not getting any event for the raising of the frame
29053 (even tried with SubstructureRedirectMask), only Expose events.
29054 These expose events will draw text normally, i.e. not
29055 highlighted. Which means we must redo the highlight here.
29056 Subsume it under ``we love X''. --gerd 2001-08-15 */
29057 /* Included in Windows version because Windows most likely does not
29058 do the right thing if any third party tool offers
29059 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29060 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29062 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29063 if (f == hlinfo->mouse_face_mouse_frame)
29065 int mouse_x = hlinfo->mouse_face_mouse_x;
29066 int mouse_y = hlinfo->mouse_face_mouse_y;
29067 clear_mouse_face (hlinfo);
29068 note_mouse_highlight (f, mouse_x, mouse_y);
29074 /* EXPORT:
29075 Determine the intersection of two rectangles R1 and R2. Return
29076 the intersection in *RESULT. Value is non-zero if RESULT is not
29077 empty. */
29080 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29082 XRectangle *left, *right;
29083 XRectangle *upper, *lower;
29084 int intersection_p = 0;
29086 /* Rearrange so that R1 is the left-most rectangle. */
29087 if (r1->x < r2->x)
29088 left = r1, right = r2;
29089 else
29090 left = r2, right = r1;
29092 /* X0 of the intersection is right.x0, if this is inside R1,
29093 otherwise there is no intersection. */
29094 if (right->x <= left->x + left->width)
29096 result->x = right->x;
29098 /* The right end of the intersection is the minimum of
29099 the right ends of left and right. */
29100 result->width = (min (left->x + left->width, right->x + right->width)
29101 - result->x);
29103 /* Same game for Y. */
29104 if (r1->y < r2->y)
29105 upper = r1, lower = r2;
29106 else
29107 upper = r2, lower = r1;
29109 /* The upper end of the intersection is lower.y0, if this is inside
29110 of upper. Otherwise, there is no intersection. */
29111 if (lower->y <= upper->y + upper->height)
29113 result->y = lower->y;
29115 /* The lower end of the intersection is the minimum of the lower
29116 ends of upper and lower. */
29117 result->height = (min (lower->y + lower->height,
29118 upper->y + upper->height)
29119 - result->y);
29120 intersection_p = 1;
29124 return intersection_p;
29127 #endif /* HAVE_WINDOW_SYSTEM */
29130 /***********************************************************************
29131 Initialization
29132 ***********************************************************************/
29134 void
29135 syms_of_xdisp (void)
29137 Vwith_echo_area_save_vector = Qnil;
29138 staticpro (&Vwith_echo_area_save_vector);
29140 Vmessage_stack = Qnil;
29141 staticpro (&Vmessage_stack);
29143 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29144 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29146 message_dolog_marker1 = Fmake_marker ();
29147 staticpro (&message_dolog_marker1);
29148 message_dolog_marker2 = Fmake_marker ();
29149 staticpro (&message_dolog_marker2);
29150 message_dolog_marker3 = Fmake_marker ();
29151 staticpro (&message_dolog_marker3);
29153 #ifdef GLYPH_DEBUG
29154 defsubr (&Sdump_frame_glyph_matrix);
29155 defsubr (&Sdump_glyph_matrix);
29156 defsubr (&Sdump_glyph_row);
29157 defsubr (&Sdump_tool_bar_row);
29158 defsubr (&Strace_redisplay);
29159 defsubr (&Strace_to_stderr);
29160 #endif
29161 #ifdef HAVE_WINDOW_SYSTEM
29162 defsubr (&Stool_bar_lines_needed);
29163 defsubr (&Slookup_image_map);
29164 #endif
29165 defsubr (&Sline_pixel_height);
29166 defsubr (&Sformat_mode_line);
29167 defsubr (&Sinvisible_p);
29168 defsubr (&Scurrent_bidi_paragraph_direction);
29169 defsubr (&Smove_point_visually);
29171 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29172 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29173 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29174 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29175 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29176 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29177 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29178 DEFSYM (Qeval, "eval");
29179 DEFSYM (QCdata, ":data");
29180 DEFSYM (Qdisplay, "display");
29181 DEFSYM (Qspace_width, "space-width");
29182 DEFSYM (Qraise, "raise");
29183 DEFSYM (Qslice, "slice");
29184 DEFSYM (Qspace, "space");
29185 DEFSYM (Qmargin, "margin");
29186 DEFSYM (Qpointer, "pointer");
29187 DEFSYM (Qleft_margin, "left-margin");
29188 DEFSYM (Qright_margin, "right-margin");
29189 DEFSYM (Qcenter, "center");
29190 DEFSYM (Qline_height, "line-height");
29191 DEFSYM (QCalign_to, ":align-to");
29192 DEFSYM (QCrelative_width, ":relative-width");
29193 DEFSYM (QCrelative_height, ":relative-height");
29194 DEFSYM (QCeval, ":eval");
29195 DEFSYM (QCpropertize, ":propertize");
29196 DEFSYM (QCfile, ":file");
29197 DEFSYM (Qfontified, "fontified");
29198 DEFSYM (Qfontification_functions, "fontification-functions");
29199 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29200 DEFSYM (Qescape_glyph, "escape-glyph");
29201 DEFSYM (Qnobreak_space, "nobreak-space");
29202 DEFSYM (Qimage, "image");
29203 DEFSYM (Qtext, "text");
29204 DEFSYM (Qboth, "both");
29205 DEFSYM (Qboth_horiz, "both-horiz");
29206 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29207 DEFSYM (QCmap, ":map");
29208 DEFSYM (QCpointer, ":pointer");
29209 DEFSYM (Qrect, "rect");
29210 DEFSYM (Qcircle, "circle");
29211 DEFSYM (Qpoly, "poly");
29212 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29213 DEFSYM (Qgrow_only, "grow-only");
29214 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29215 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29216 DEFSYM (Qposition, "position");
29217 DEFSYM (Qbuffer_position, "buffer-position");
29218 DEFSYM (Qobject, "object");
29219 DEFSYM (Qbar, "bar");
29220 DEFSYM (Qhbar, "hbar");
29221 DEFSYM (Qbox, "box");
29222 DEFSYM (Qhollow, "hollow");
29223 DEFSYM (Qhand, "hand");
29224 DEFSYM (Qarrow, "arrow");
29225 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29227 list_of_error = list1 (list2 (intern_c_string ("error"),
29228 intern_c_string ("void-variable")));
29229 staticpro (&list_of_error);
29231 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29232 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29233 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29234 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29236 echo_buffer[0] = echo_buffer[1] = Qnil;
29237 staticpro (&echo_buffer[0]);
29238 staticpro (&echo_buffer[1]);
29240 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29241 staticpro (&echo_area_buffer[0]);
29242 staticpro (&echo_area_buffer[1]);
29244 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29245 staticpro (&Vmessages_buffer_name);
29247 mode_line_proptrans_alist = Qnil;
29248 staticpro (&mode_line_proptrans_alist);
29249 mode_line_string_list = Qnil;
29250 staticpro (&mode_line_string_list);
29251 mode_line_string_face = Qnil;
29252 staticpro (&mode_line_string_face);
29253 mode_line_string_face_prop = Qnil;
29254 staticpro (&mode_line_string_face_prop);
29255 Vmode_line_unwind_vector = Qnil;
29256 staticpro (&Vmode_line_unwind_vector);
29258 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29260 help_echo_string = Qnil;
29261 staticpro (&help_echo_string);
29262 help_echo_object = Qnil;
29263 staticpro (&help_echo_object);
29264 help_echo_window = Qnil;
29265 staticpro (&help_echo_window);
29266 previous_help_echo_string = Qnil;
29267 staticpro (&previous_help_echo_string);
29268 help_echo_pos = -1;
29270 DEFSYM (Qright_to_left, "right-to-left");
29271 DEFSYM (Qleft_to_right, "left-to-right");
29273 #ifdef HAVE_WINDOW_SYSTEM
29274 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29275 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29276 For example, if a block cursor is over a tab, it will be drawn as
29277 wide as that tab on the display. */);
29278 x_stretch_cursor_p = 0;
29279 #endif
29281 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29282 doc: /* Non-nil means highlight trailing whitespace.
29283 The face used for trailing whitespace is `trailing-whitespace'. */);
29284 Vshow_trailing_whitespace = Qnil;
29286 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29287 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29288 If the value is t, Emacs highlights non-ASCII chars which have the
29289 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29290 or `escape-glyph' face respectively.
29292 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29293 U+2011 (non-breaking hyphen) are affected.
29295 Any other non-nil value means to display these characters as a escape
29296 glyph followed by an ordinary space or hyphen.
29298 A value of nil means no special handling of these characters. */);
29299 Vnobreak_char_display = Qt;
29301 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29302 doc: /* The pointer shape to show in void text areas.
29303 A value of nil means to show the text pointer. Other options are `arrow',
29304 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29305 Vvoid_text_area_pointer = Qarrow;
29307 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29308 doc: /* Non-nil means don't actually do any redisplay.
29309 This is used for internal purposes. */);
29310 Vinhibit_redisplay = Qnil;
29312 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29313 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29314 Vglobal_mode_string = Qnil;
29316 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29317 doc: /* Marker for where to display an arrow on top of the buffer text.
29318 This must be the beginning of a line in order to work.
29319 See also `overlay-arrow-string'. */);
29320 Voverlay_arrow_position = Qnil;
29322 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29323 doc: /* String to display as an arrow in non-window frames.
29324 See also `overlay-arrow-position'. */);
29325 Voverlay_arrow_string = build_pure_c_string ("=>");
29327 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29328 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29329 The symbols on this list are examined during redisplay to determine
29330 where to display overlay arrows. */);
29331 Voverlay_arrow_variable_list
29332 = list1 (intern_c_string ("overlay-arrow-position"));
29334 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29335 doc: /* The number of lines to try scrolling a window by when point moves out.
29336 If that fails to bring point back on frame, point is centered instead.
29337 If this is zero, point is always centered after it moves off frame.
29338 If you want scrolling to always be a line at a time, you should set
29339 `scroll-conservatively' to a large value rather than set this to 1. */);
29341 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29342 doc: /* Scroll up to this many lines, to bring point back on screen.
29343 If point moves off-screen, redisplay will scroll by up to
29344 `scroll-conservatively' lines in order to bring point just barely
29345 onto the screen again. If that cannot be done, then redisplay
29346 recenters point as usual.
29348 If the value is greater than 100, redisplay will never recenter point,
29349 but will always scroll just enough text to bring point into view, even
29350 if you move far away.
29352 A value of zero means always recenter point if it moves off screen. */);
29353 scroll_conservatively = 0;
29355 DEFVAR_INT ("scroll-margin", scroll_margin,
29356 doc: /* Number of lines of margin at the top and bottom of a window.
29357 Recenter the window whenever point gets within this many lines
29358 of the top or bottom of the window. */);
29359 scroll_margin = 0;
29361 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29362 doc: /* Pixels per inch value for non-window system displays.
29363 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29364 Vdisplay_pixels_per_inch = make_float (72.0);
29366 #ifdef GLYPH_DEBUG
29367 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29368 #endif
29370 DEFVAR_LISP ("truncate-partial-width-windows",
29371 Vtruncate_partial_width_windows,
29372 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29373 For an integer value, truncate lines in each window narrower than the
29374 full frame width, provided the window width is less than that integer;
29375 otherwise, respect the value of `truncate-lines'.
29377 For any other non-nil value, truncate lines in all windows that do
29378 not span the full frame width.
29380 A value of nil means to respect the value of `truncate-lines'.
29382 If `word-wrap' is enabled, you might want to reduce this. */);
29383 Vtruncate_partial_width_windows = make_number (50);
29385 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29386 doc: /* Maximum buffer size for which line number should be displayed.
29387 If the buffer is bigger than this, the line number does not appear
29388 in the mode line. A value of nil means no limit. */);
29389 Vline_number_display_limit = Qnil;
29391 DEFVAR_INT ("line-number-display-limit-width",
29392 line_number_display_limit_width,
29393 doc: /* Maximum line width (in characters) for line number display.
29394 If the average length of the lines near point is bigger than this, then the
29395 line number may be omitted from the mode line. */);
29396 line_number_display_limit_width = 200;
29398 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29399 doc: /* Non-nil means highlight region even in nonselected windows. */);
29400 highlight_nonselected_windows = 0;
29402 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29403 doc: /* Non-nil if more than one frame is visible on this display.
29404 Minibuffer-only frames don't count, but iconified frames do.
29405 This variable is not guaranteed to be accurate except while processing
29406 `frame-title-format' and `icon-title-format'. */);
29408 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29409 doc: /* Template for displaying the title bar of visible frames.
29410 \(Assuming the window manager supports this feature.)
29412 This variable has the same structure as `mode-line-format', except that
29413 the %c and %l constructs are ignored. It is used only on frames for
29414 which no explicit name has been set \(see `modify-frame-parameters'). */);
29416 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29417 doc: /* Template for displaying the title bar of an iconified frame.
29418 \(Assuming the window manager supports this feature.)
29419 This variable has the same structure as `mode-line-format' (which see),
29420 and is used only on frames for which no explicit name has been set
29421 \(see `modify-frame-parameters'). */);
29422 Vicon_title_format
29423 = Vframe_title_format
29424 = listn (CONSTYPE_PURE, 3,
29425 intern_c_string ("multiple-frames"),
29426 build_pure_c_string ("%b"),
29427 listn (CONSTYPE_PURE, 4,
29428 empty_unibyte_string,
29429 intern_c_string ("invocation-name"),
29430 build_pure_c_string ("@"),
29431 intern_c_string ("system-name")));
29433 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29434 doc: /* Maximum number of lines to keep in the message log buffer.
29435 If nil, disable message logging. If t, log messages but don't truncate
29436 the buffer when it becomes large. */);
29437 Vmessage_log_max = make_number (1000);
29439 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29440 doc: /* Functions called before redisplay, if window sizes have changed.
29441 The value should be a list of functions that take one argument.
29442 Just before redisplay, for each frame, if any of its windows have changed
29443 size since the last redisplay, or have been split or deleted,
29444 all the functions in the list are called, with the frame as argument. */);
29445 Vwindow_size_change_functions = Qnil;
29447 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29448 doc: /* List of functions to call before redisplaying a window with scrolling.
29449 Each function is called with two arguments, the window and its new
29450 display-start position. Note that these functions are also called by
29451 `set-window-buffer'. Also note that the value of `window-end' is not
29452 valid when these functions are called.
29454 Warning: Do not use this feature to alter the way the window
29455 is scrolled. It is not designed for that, and such use probably won't
29456 work. */);
29457 Vwindow_scroll_functions = Qnil;
29459 DEFVAR_LISP ("window-text-change-functions",
29460 Vwindow_text_change_functions,
29461 doc: /* Functions to call in redisplay when text in the window might change. */);
29462 Vwindow_text_change_functions = Qnil;
29464 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29465 doc: /* Functions called when redisplay of a window reaches the end trigger.
29466 Each function is called with two arguments, the window and the end trigger value.
29467 See `set-window-redisplay-end-trigger'. */);
29468 Vredisplay_end_trigger_functions = Qnil;
29470 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29471 doc: /* Non-nil means autoselect window with mouse pointer.
29472 If nil, do not autoselect windows.
29473 A positive number means delay autoselection by that many seconds: a
29474 window is autoselected only after the mouse has remained in that
29475 window for the duration of the delay.
29476 A negative number has a similar effect, but causes windows to be
29477 autoselected only after the mouse has stopped moving. \(Because of
29478 the way Emacs compares mouse events, you will occasionally wait twice
29479 that time before the window gets selected.\)
29480 Any other value means to autoselect window instantaneously when the
29481 mouse pointer enters it.
29483 Autoselection selects the minibuffer only if it is active, and never
29484 unselects the minibuffer if it is active.
29486 When customizing this variable make sure that the actual value of
29487 `focus-follows-mouse' matches the behavior of your window manager. */);
29488 Vmouse_autoselect_window = Qnil;
29490 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29491 doc: /* Non-nil means automatically resize tool-bars.
29492 This dynamically changes the tool-bar's height to the minimum height
29493 that is needed to make all tool-bar items visible.
29494 If value is `grow-only', the tool-bar's height is only increased
29495 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29496 Vauto_resize_tool_bars = Qt;
29498 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29499 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29500 auto_raise_tool_bar_buttons_p = 1;
29502 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29503 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29504 make_cursor_line_fully_visible_p = 1;
29506 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29507 doc: /* Border below tool-bar in pixels.
29508 If an integer, use it as the height of the border.
29509 If it is one of `internal-border-width' or `border-width', use the
29510 value of the corresponding frame parameter.
29511 Otherwise, no border is added below the tool-bar. */);
29512 Vtool_bar_border = Qinternal_border_width;
29514 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29515 doc: /* Margin around tool-bar buttons in pixels.
29516 If an integer, use that for both horizontal and vertical margins.
29517 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29518 HORZ specifying the horizontal margin, and VERT specifying the
29519 vertical margin. */);
29520 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29522 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29523 doc: /* Relief thickness of tool-bar buttons. */);
29524 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29526 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29527 doc: /* Tool bar style to use.
29528 It can be one of
29529 image - show images only
29530 text - show text only
29531 both - show both, text below image
29532 both-horiz - show text to the right of the image
29533 text-image-horiz - show text to the left of the image
29534 any other - use system default or image if no system default.
29536 This variable only affects the GTK+ toolkit version of Emacs. */);
29537 Vtool_bar_style = Qnil;
29539 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29540 doc: /* Maximum number of characters a label can have to be shown.
29541 The tool bar style must also show labels for this to have any effect, see
29542 `tool-bar-style'. */);
29543 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29545 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29546 doc: /* List of functions to call to fontify regions of text.
29547 Each function is called with one argument POS. Functions must
29548 fontify a region starting at POS in the current buffer, and give
29549 fontified regions the property `fontified'. */);
29550 Vfontification_functions = Qnil;
29551 Fmake_variable_buffer_local (Qfontification_functions);
29553 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29554 unibyte_display_via_language_environment,
29555 doc: /* Non-nil means display unibyte text according to language environment.
29556 Specifically, this means that raw bytes in the range 160-255 decimal
29557 are displayed by converting them to the equivalent multibyte characters
29558 according to the current language environment. As a result, they are
29559 displayed according to the current fontset.
29561 Note that this variable affects only how these bytes are displayed,
29562 but does not change the fact they are interpreted as raw bytes. */);
29563 unibyte_display_via_language_environment = 0;
29565 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29566 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29567 If a float, it specifies a fraction of the mini-window frame's height.
29568 If an integer, it specifies a number of lines. */);
29569 Vmax_mini_window_height = make_float (0.25);
29571 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29572 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29573 A value of nil means don't automatically resize mini-windows.
29574 A value of t means resize them to fit the text displayed in them.
29575 A value of `grow-only', the default, means let mini-windows grow only;
29576 they return to their normal size when the minibuffer is closed, or the
29577 echo area becomes empty. */);
29578 Vresize_mini_windows = Qgrow_only;
29580 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29581 doc: /* Alist specifying how to blink the cursor off.
29582 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29583 `cursor-type' frame-parameter or variable equals ON-STATE,
29584 comparing using `equal', Emacs uses OFF-STATE to specify
29585 how to blink it off. ON-STATE and OFF-STATE are values for
29586 the `cursor-type' frame parameter.
29588 If a frame's ON-STATE has no entry in this list,
29589 the frame's other specifications determine how to blink the cursor off. */);
29590 Vblink_cursor_alist = Qnil;
29592 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29593 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29594 If non-nil, windows are automatically scrolled horizontally to make
29595 point visible. */);
29596 automatic_hscrolling_p = 1;
29597 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29599 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29600 doc: /* How many columns away from the window edge point is allowed to get
29601 before automatic hscrolling will horizontally scroll the window. */);
29602 hscroll_margin = 5;
29604 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29605 doc: /* How many columns to scroll the window when point gets too close to the edge.
29606 When point is less than `hscroll-margin' columns from the window
29607 edge, automatic hscrolling will scroll the window by the amount of columns
29608 determined by this variable. If its value is a positive integer, scroll that
29609 many columns. If it's a positive floating-point number, it specifies the
29610 fraction of the window's width to scroll. If it's nil or zero, point will be
29611 centered horizontally after the scroll. Any other value, including negative
29612 numbers, are treated as if the value were zero.
29614 Automatic hscrolling always moves point outside the scroll margin, so if
29615 point was more than scroll step columns inside the margin, the window will
29616 scroll more than the value given by the scroll step.
29618 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29619 and `scroll-right' overrides this variable's effect. */);
29620 Vhscroll_step = make_number (0);
29622 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29623 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29624 Bind this around calls to `message' to let it take effect. */);
29625 message_truncate_lines = 0;
29627 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29628 doc: /* Normal hook run to update the menu bar definitions.
29629 Redisplay runs this hook before it redisplays the menu bar.
29630 This is used to update submenus such as Buffers,
29631 whose contents depend on various data. */);
29632 Vmenu_bar_update_hook = Qnil;
29634 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29635 doc: /* Frame for which we are updating a menu.
29636 The enable predicate for a menu binding should check this variable. */);
29637 Vmenu_updating_frame = Qnil;
29639 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29640 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29641 inhibit_menubar_update = 0;
29643 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29644 doc: /* Prefix prepended to all continuation lines at display time.
29645 The value may be a string, an image, or a stretch-glyph; it is
29646 interpreted in the same way as the value of a `display' text property.
29648 This variable is overridden by any `wrap-prefix' text or overlay
29649 property.
29651 To add a prefix to non-continuation lines, use `line-prefix'. */);
29652 Vwrap_prefix = Qnil;
29653 DEFSYM (Qwrap_prefix, "wrap-prefix");
29654 Fmake_variable_buffer_local (Qwrap_prefix);
29656 DEFVAR_LISP ("line-prefix", Vline_prefix,
29657 doc: /* Prefix prepended to all non-continuation lines at display time.
29658 The value may be a string, an image, or a stretch-glyph; it is
29659 interpreted in the same way as the value of a `display' text property.
29661 This variable is overridden by any `line-prefix' text or overlay
29662 property.
29664 To add a prefix to continuation lines, use `wrap-prefix'. */);
29665 Vline_prefix = Qnil;
29666 DEFSYM (Qline_prefix, "line-prefix");
29667 Fmake_variable_buffer_local (Qline_prefix);
29669 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29670 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29671 inhibit_eval_during_redisplay = 0;
29673 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29674 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29675 inhibit_free_realized_faces = 0;
29677 #ifdef GLYPH_DEBUG
29678 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29679 doc: /* Inhibit try_window_id display optimization. */);
29680 inhibit_try_window_id = 0;
29682 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29683 doc: /* Inhibit try_window_reusing display optimization. */);
29684 inhibit_try_window_reusing = 0;
29686 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29687 doc: /* Inhibit try_cursor_movement display optimization. */);
29688 inhibit_try_cursor_movement = 0;
29689 #endif /* GLYPH_DEBUG */
29691 DEFVAR_INT ("overline-margin", overline_margin,
29692 doc: /* Space between overline and text, in pixels.
29693 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29694 margin to the character height. */);
29695 overline_margin = 2;
29697 DEFVAR_INT ("underline-minimum-offset",
29698 underline_minimum_offset,
29699 doc: /* Minimum distance between baseline and underline.
29700 This can improve legibility of underlined text at small font sizes,
29701 particularly when using variable `x-use-underline-position-properties'
29702 with fonts that specify an UNDERLINE_POSITION relatively close to the
29703 baseline. The default value is 1. */);
29704 underline_minimum_offset = 1;
29706 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29707 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29708 This feature only works when on a window system that can change
29709 cursor shapes. */);
29710 display_hourglass_p = 1;
29712 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29713 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29714 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29716 hourglass_atimer = NULL;
29717 hourglass_shown_p = 0;
29719 DEFSYM (Qglyphless_char, "glyphless-char");
29720 DEFSYM (Qhex_code, "hex-code");
29721 DEFSYM (Qempty_box, "empty-box");
29722 DEFSYM (Qthin_space, "thin-space");
29723 DEFSYM (Qzero_width, "zero-width");
29725 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29726 /* Intern this now in case it isn't already done.
29727 Setting this variable twice is harmless.
29728 But don't staticpro it here--that is done in alloc.c. */
29729 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29730 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29732 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29733 doc: /* Char-table defining glyphless characters.
29734 Each element, if non-nil, should be one of the following:
29735 an ASCII acronym string: display this string in a box
29736 `hex-code': display the hexadecimal code of a character in a box
29737 `empty-box': display as an empty box
29738 `thin-space': display as 1-pixel width space
29739 `zero-width': don't display
29740 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29741 display method for graphical terminals and text terminals respectively.
29742 GRAPHICAL and TEXT should each have one of the values listed above.
29744 The char-table has one extra slot to control the display of a character for
29745 which no font is found. This slot only takes effect on graphical terminals.
29746 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29747 `thin-space'. The default is `empty-box'. */);
29748 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29749 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29750 Qempty_box);
29752 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29753 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29754 Vdebug_on_message = Qnil;
29758 /* Initialize this module when Emacs starts. */
29760 void
29761 init_xdisp (void)
29763 current_header_line_height = current_mode_line_height = -1;
29765 CHARPOS (this_line_start_pos) = 0;
29767 if (!noninteractive)
29769 struct window *m = XWINDOW (minibuf_window);
29770 Lisp_Object frame = m->frame;
29771 struct frame *f = XFRAME (frame);
29772 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29773 struct window *r = XWINDOW (root);
29774 int i;
29776 echo_area_window = minibuf_window;
29778 r->top_line = FRAME_TOP_MARGIN (f);
29779 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29780 r->total_cols = FRAME_COLS (f);
29782 m->top_line = FRAME_LINES (f) - 1;
29783 m->total_lines = 1;
29784 m->total_cols = FRAME_COLS (f);
29786 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29787 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29788 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29790 /* The default ellipsis glyphs `...'. */
29791 for (i = 0; i < 3; ++i)
29792 default_invis_vector[i] = make_number ('.');
29796 /* Allocate the buffer for frame titles.
29797 Also used for `format-mode-line'. */
29798 int size = 100;
29799 mode_line_noprop_buf = xmalloc (size);
29800 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29801 mode_line_noprop_ptr = mode_line_noprop_buf;
29802 mode_line_target = MODE_LINE_DISPLAY;
29805 help_echo_showing_p = 0;
29808 /* Platform-independent portion of hourglass implementation. */
29810 /* Cancel a currently active hourglass timer, and start a new one. */
29811 void
29812 start_hourglass (void)
29814 #if defined (HAVE_WINDOW_SYSTEM)
29815 EMACS_TIME delay;
29817 cancel_hourglass ();
29819 if (INTEGERP (Vhourglass_delay)
29820 && XINT (Vhourglass_delay) > 0)
29821 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29822 TYPE_MAXIMUM (time_t)),
29824 else if (FLOATP (Vhourglass_delay)
29825 && XFLOAT_DATA (Vhourglass_delay) > 0)
29826 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29827 else
29828 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29830 #ifdef HAVE_NTGUI
29832 extern void w32_note_current_window (void);
29833 w32_note_current_window ();
29835 #endif /* HAVE_NTGUI */
29837 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29838 show_hourglass, NULL);
29839 #endif
29843 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29844 shown. */
29845 void
29846 cancel_hourglass (void)
29848 #if defined (HAVE_WINDOW_SYSTEM)
29849 if (hourglass_atimer)
29851 cancel_atimer (hourglass_atimer);
29852 hourglass_atimer = NULL;
29855 if (hourglass_shown_p)
29856 hide_hourglass ();
29857 #endif